xref: /titanic_52/usr/src/uts/sun4v/io/cnex.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
1*1ae08745Sheppo /*
2*1ae08745Sheppo  * CDDL HEADER START
3*1ae08745Sheppo  *
4*1ae08745Sheppo  * The contents of this file are subject to the terms of the
5*1ae08745Sheppo  * Common Development and Distribution License (the "License").
6*1ae08745Sheppo  * You may not use this file except in compliance with the License.
7*1ae08745Sheppo  *
8*1ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
10*1ae08745Sheppo  * See the License for the specific language governing permissions
11*1ae08745Sheppo  * and limitations under the License.
12*1ae08745Sheppo  *
13*1ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
14*1ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
16*1ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
17*1ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1ae08745Sheppo  *
19*1ae08745Sheppo  * CDDL HEADER END
20*1ae08745Sheppo  */
21*1ae08745Sheppo /*
22*1ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1ae08745Sheppo  * Use is subject to license terms.
24*1ae08745Sheppo  */
25*1ae08745Sheppo 
26*1ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*1ae08745Sheppo 
28*1ae08745Sheppo /*
29*1ae08745Sheppo  * Logical domain channel devices are devices implemented entirely
30*1ae08745Sheppo  * in software; cnex is the nexus for channel-devices. They use
31*1ae08745Sheppo  * the HV channel interfaces via the LDC transport module to send
32*1ae08745Sheppo  * and receive data and to register callbacks.
33*1ae08745Sheppo  */
34*1ae08745Sheppo 
35*1ae08745Sheppo #include <sys/types.h>
36*1ae08745Sheppo #include <sys/cmn_err.h>
37*1ae08745Sheppo #include <sys/conf.h>
38*1ae08745Sheppo #include <sys/ddi.h>
39*1ae08745Sheppo #include <sys/ddi_impldefs.h>
40*1ae08745Sheppo #include <sys/devops.h>
41*1ae08745Sheppo #include <sys/instance.h>
42*1ae08745Sheppo #include <sys/modctl.h>
43*1ae08745Sheppo #include <sys/open.h>
44*1ae08745Sheppo #include <sys/stat.h>
45*1ae08745Sheppo #include <sys/sunddi.h>
46*1ae08745Sheppo #include <sys/sunndi.h>
47*1ae08745Sheppo #include <sys/systm.h>
48*1ae08745Sheppo #include <sys/mkdev.h>
49*1ae08745Sheppo #include <sys/machsystm.h>
50*1ae08745Sheppo #include <sys/intr.h>
51*1ae08745Sheppo #include <sys/ddi_intr_impl.h>
52*1ae08745Sheppo #include <sys/ivintr.h>
53*1ae08745Sheppo #include <sys/hypervisor_api.h>
54*1ae08745Sheppo #include <sys/ldc.h>
55*1ae08745Sheppo #include <sys/cnex.h>
56*1ae08745Sheppo #include <sys/mach_descrip.h>
57*1ae08745Sheppo 
58*1ae08745Sheppo /*
59*1ae08745Sheppo  * Internal functions/information
60*1ae08745Sheppo  */
61*1ae08745Sheppo static struct cnex_pil_map cnex_class_to_pil[] = {
62*1ae08745Sheppo 	{LDC_DEV_GENERIC,	PIL_3},
63*1ae08745Sheppo 	{LDC_DEV_BLK,		PIL_4},
64*1ae08745Sheppo 	{LDC_DEV_BLK_SVC,	PIL_3},
65*1ae08745Sheppo 	{LDC_DEV_NT,		PIL_6},
66*1ae08745Sheppo 	{LDC_DEV_NT_SVC,	PIL_4},
67*1ae08745Sheppo 	{LDC_DEV_SERIAL,	PIL_6}
68*1ae08745Sheppo };
69*1ae08745Sheppo #define	CNEX_MAX_DEVS (sizeof (cnex_class_to_pil) / \
70*1ae08745Sheppo 				sizeof (cnex_class_to_pil[0]))
71*1ae08745Sheppo 
72*1ae08745Sheppo #define	SUN4V_REG_SPEC2CFG_HDL(x)	((x >> 32) & ~(0xfull << 28))
73*1ae08745Sheppo 
74*1ae08745Sheppo static hrtime_t cnex_pending_tmout = 2ull * NANOSEC; /* 2 secs in nsecs */
75*1ae08745Sheppo static void *cnex_state;
76*1ae08745Sheppo 
77*1ae08745Sheppo static void cnex_intr_redist(void *arg);
78*1ae08745Sheppo static uint_t cnex_intr_wrapper(caddr_t arg);
79*1ae08745Sheppo 
80*1ae08745Sheppo /*
81*1ae08745Sheppo  * Debug info
82*1ae08745Sheppo  */
83*1ae08745Sheppo #ifdef DEBUG
84*1ae08745Sheppo 
85*1ae08745Sheppo /*
86*1ae08745Sheppo  * Print debug messages
87*1ae08745Sheppo  *
88*1ae08745Sheppo  * set cnexdbg to 0xf for enabling all msgs
89*1ae08745Sheppo  * 0x8 - Errors
90*1ae08745Sheppo  * 0x4 - Warnings
91*1ae08745Sheppo  * 0x2 - All debug messages
92*1ae08745Sheppo  * 0x1 - Minimal debug messages
93*1ae08745Sheppo  */
94*1ae08745Sheppo 
95*1ae08745Sheppo int cnexdbg = 0x8;
96*1ae08745Sheppo 
97*1ae08745Sheppo static void
98*1ae08745Sheppo cnexdebug(const char *fmt, ...)
99*1ae08745Sheppo {
100*1ae08745Sheppo 	char buf[512];
101*1ae08745Sheppo 	va_list ap;
102*1ae08745Sheppo 
103*1ae08745Sheppo 	va_start(ap, fmt);
104*1ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
105*1ae08745Sheppo 	va_end(ap);
106*1ae08745Sheppo 
107*1ae08745Sheppo 	cmn_err(CE_CONT, "%s\n", buf);
108*1ae08745Sheppo }
109*1ae08745Sheppo 
110*1ae08745Sheppo #define	D1		\
111*1ae08745Sheppo if (cnexdbg & 0x01)	\
112*1ae08745Sheppo 	cnexdebug
113*1ae08745Sheppo 
114*1ae08745Sheppo #define	D2		\
115*1ae08745Sheppo if (cnexdbg & 0x02)	\
116*1ae08745Sheppo 	cnexdebug
117*1ae08745Sheppo 
118*1ae08745Sheppo #define	DWARN		\
119*1ae08745Sheppo if (cnexdbg & 0x04)	\
120*1ae08745Sheppo 	cnexdebug
121*1ae08745Sheppo 
122*1ae08745Sheppo #define	DERR		\
123*1ae08745Sheppo if (cnexdbg & 0x08)	\
124*1ae08745Sheppo 	cnexdebug
125*1ae08745Sheppo 
126*1ae08745Sheppo #else
127*1ae08745Sheppo 
128*1ae08745Sheppo #define	D1
129*1ae08745Sheppo #define	D2
130*1ae08745Sheppo #define	DWARN
131*1ae08745Sheppo #define	DERR
132*1ae08745Sheppo 
133*1ae08745Sheppo #endif
134*1ae08745Sheppo 
135*1ae08745Sheppo /*
136*1ae08745Sheppo  * Config information
137*1ae08745Sheppo  */
138*1ae08745Sheppo static int cnex_attach(dev_info_t *, ddi_attach_cmd_t);
139*1ae08745Sheppo static int cnex_detach(dev_info_t *, ddi_detach_cmd_t);
140*1ae08745Sheppo static int cnex_open(dev_t *, int, int, cred_t *);
141*1ae08745Sheppo static int cnex_close(dev_t, int, int, cred_t *);
142*1ae08745Sheppo static int cnex_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
143*1ae08745Sheppo static int cnex_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
144*1ae08745Sheppo     void *);
145*1ae08745Sheppo 
146*1ae08745Sheppo static struct bus_ops cnex_bus_ops = {
147*1ae08745Sheppo 	BUSO_REV,
148*1ae08745Sheppo 	nullbusmap,		/* bus_map */
149*1ae08745Sheppo 	NULL,			/* bus_get_intrspec */
150*1ae08745Sheppo 	NULL,			/* bus_add_intrspec */
151*1ae08745Sheppo 	NULL,			/* bus_remove_intrspec */
152*1ae08745Sheppo 	i_ddi_map_fault,	/* bus_map_fault */
153*1ae08745Sheppo 	ddi_no_dma_map,		/* bus_dma_map */
154*1ae08745Sheppo 	ddi_no_dma_allochdl,	/* bus_dma_allochdl */
155*1ae08745Sheppo 	NULL,			/* bus_dma_freehdl */
156*1ae08745Sheppo 	NULL,			/* bus_dma_bindhdl */
157*1ae08745Sheppo 	NULL,			/* bus_dma_unbindhdl */
158*1ae08745Sheppo 	NULL,			/* bus_dma_flush */
159*1ae08745Sheppo 	NULL,			/* bus_dma_win */
160*1ae08745Sheppo 	NULL,			/* bus_dma_ctl */
161*1ae08745Sheppo 	cnex_ctl,		/* bus_ctl */
162*1ae08745Sheppo 	ddi_bus_prop_op,	/* bus_prop_op */
163*1ae08745Sheppo 	0,			/* bus_get_eventcookie */
164*1ae08745Sheppo 	0,			/* bus_add_eventcall */
165*1ae08745Sheppo 	0,			/* bus_remove_eventcall	*/
166*1ae08745Sheppo 	0,			/* bus_post_event */
167*1ae08745Sheppo 	NULL,			/* bus_intr_ctl */
168*1ae08745Sheppo 	NULL,			/* bus_config */
169*1ae08745Sheppo 	NULL,			/* bus_unconfig */
170*1ae08745Sheppo 	NULL,			/* bus_fm_init */
171*1ae08745Sheppo 	NULL,			/* bus_fm_fini */
172*1ae08745Sheppo 	NULL,			/* bus_fm_access_enter */
173*1ae08745Sheppo 	NULL,			/* bus_fm_access_exit */
174*1ae08745Sheppo 	NULL,			/* bus_power */
175*1ae08745Sheppo 	NULL			/* bus_intr_op */
176*1ae08745Sheppo };
177*1ae08745Sheppo 
178*1ae08745Sheppo static struct cb_ops cnex_cb_ops = {
179*1ae08745Sheppo 	cnex_open,			/* open */
180*1ae08745Sheppo 	cnex_close,			/* close */
181*1ae08745Sheppo 	nodev,				/* strategy */
182*1ae08745Sheppo 	nodev,				/* print */
183*1ae08745Sheppo 	nodev,				/* dump */
184*1ae08745Sheppo 	nodev,				/* read */
185*1ae08745Sheppo 	nodev,				/* write */
186*1ae08745Sheppo 	cnex_ioctl,			/* ioctl */
187*1ae08745Sheppo 	nodev,				/* devmap */
188*1ae08745Sheppo 	nodev,				/* mmap */
189*1ae08745Sheppo 	nodev,				/* segmap */
190*1ae08745Sheppo 	nochpoll,			/* poll */
191*1ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
192*1ae08745Sheppo 	0,				/* streamtab  */
193*1ae08745Sheppo 	D_MP | D_NEW | D_HOTPLUG	/* Driver compatibility flag */
194*1ae08745Sheppo };
195*1ae08745Sheppo 
196*1ae08745Sheppo static struct dev_ops cnex_ops = {
197*1ae08745Sheppo 	DEVO_REV,		/* devo_rev, */
198*1ae08745Sheppo 	0,			/* refcnt  */
199*1ae08745Sheppo 	ddi_getinfo_1to1,	/* info */
200*1ae08745Sheppo 	nulldev,		/* identify */
201*1ae08745Sheppo 	nulldev,		/* probe */
202*1ae08745Sheppo 	cnex_attach,		/* attach */
203*1ae08745Sheppo 	cnex_detach,		/* detach */
204*1ae08745Sheppo 	nodev,			/* reset */
205*1ae08745Sheppo 	&cnex_cb_ops,		/* driver operations */
206*1ae08745Sheppo 	&cnex_bus_ops,		/* bus operations */
207*1ae08745Sheppo 	nulldev			/* power */
208*1ae08745Sheppo };
209*1ae08745Sheppo 
210*1ae08745Sheppo /*
211*1ae08745Sheppo  * Module linkage information for the kernel.
212*1ae08745Sheppo  */
213*1ae08745Sheppo static struct modldrv modldrv = {
214*1ae08745Sheppo 	&mod_driverops,
215*1ae08745Sheppo 	"sun4v channel-devices nexus driver v%I%",
216*1ae08745Sheppo 	&cnex_ops,
217*1ae08745Sheppo };
218*1ae08745Sheppo 
219*1ae08745Sheppo static struct modlinkage modlinkage = {
220*1ae08745Sheppo 	MODREV_1, (void *)&modldrv, NULL
221*1ae08745Sheppo };
222*1ae08745Sheppo 
223*1ae08745Sheppo int
224*1ae08745Sheppo _init(void)
225*1ae08745Sheppo {
226*1ae08745Sheppo 	int err;
227*1ae08745Sheppo 
228*1ae08745Sheppo 	if ((err = ddi_soft_state_init(&cnex_state,
229*1ae08745Sheppo 		sizeof (cnex_soft_state_t), 0)) != 0) {
230*1ae08745Sheppo 		return (err);
231*1ae08745Sheppo 	}
232*1ae08745Sheppo 	if ((err = mod_install(&modlinkage)) != 0) {
233*1ae08745Sheppo 		ddi_soft_state_fini(&cnex_state);
234*1ae08745Sheppo 		return (err);
235*1ae08745Sheppo 	}
236*1ae08745Sheppo 	return (0);
237*1ae08745Sheppo }
238*1ae08745Sheppo 
239*1ae08745Sheppo int
240*1ae08745Sheppo _fini(void)
241*1ae08745Sheppo {
242*1ae08745Sheppo 	int err;
243*1ae08745Sheppo 
244*1ae08745Sheppo 	if ((err = mod_remove(&modlinkage)) != 0)
245*1ae08745Sheppo 		return (err);
246*1ae08745Sheppo 	ddi_soft_state_fini(&cnex_state);
247*1ae08745Sheppo 	return (0);
248*1ae08745Sheppo }
249*1ae08745Sheppo 
250*1ae08745Sheppo int
251*1ae08745Sheppo _info(struct modinfo *modinfop)
252*1ae08745Sheppo {
253*1ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
254*1ae08745Sheppo }
255*1ae08745Sheppo 
256*1ae08745Sheppo /*
257*1ae08745Sheppo  * Callback function invoked by the interrupt redistribution
258*1ae08745Sheppo  * framework. This will redirect interrupts at CPUs that are
259*1ae08745Sheppo  * currently available in the system.
260*1ae08745Sheppo  */
261*1ae08745Sheppo static void
262*1ae08745Sheppo cnex_intr_redist(void *arg)
263*1ae08745Sheppo {
264*1ae08745Sheppo 	cnex_ldc_t		*cldcp;
265*1ae08745Sheppo 	cnex_soft_state_t	*cnex_ssp = arg;
266*1ae08745Sheppo 	int			intr_state;
267*1ae08745Sheppo 	hrtime_t 		start;
268*1ae08745Sheppo 	uint64_t		cpuid;
269*1ae08745Sheppo 	int 			rv;
270*1ae08745Sheppo 
271*1ae08745Sheppo 	ASSERT(cnex_ssp != NULL);
272*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
273*1ae08745Sheppo 
274*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
275*1ae08745Sheppo 	while (cldcp != NULL) {
276*1ae08745Sheppo 
277*1ae08745Sheppo 		mutex_enter(&cldcp->lock);
278*1ae08745Sheppo 
279*1ae08745Sheppo 		if (cldcp->tx.hdlr) {
280*1ae08745Sheppo 			/*
281*1ae08745Sheppo 			 * Don't do anything for disabled interrupts.
282*1ae08745Sheppo 			 */
283*1ae08745Sheppo 			rv = hvldc_intr_getvalid(cnex_ssp->cfghdl,
284*1ae08745Sheppo 			    cldcp->tx.ino, &intr_state);
285*1ae08745Sheppo 			if (rv) {
286*1ae08745Sheppo 				DWARN("cnex_intr_redist: tx ino=0x%llx, "
287*1ae08745Sheppo 				    "can't get valid\n", cldcp->tx.ino);
288*1ae08745Sheppo 				mutex_exit(&cldcp->lock);
289*1ae08745Sheppo 				mutex_exit(&cnex_ssp->clist_lock);
290*1ae08745Sheppo 				return;
291*1ae08745Sheppo 			}
292*1ae08745Sheppo 			if (intr_state == HV_INTR_NOTVALID) {
293*1ae08745Sheppo 				cldcp = cldcp->next;
294*1ae08745Sheppo 				continue;
295*1ae08745Sheppo 			}
296*1ae08745Sheppo 
297*1ae08745Sheppo 			cpuid = intr_dist_cpuid();
298*1ae08745Sheppo 
299*1ae08745Sheppo 			/* disable interrupts */
300*1ae08745Sheppo 			rv = hvldc_intr_setvalid(cnex_ssp->cfghdl,
301*1ae08745Sheppo 			    cldcp->tx.ino, HV_INTR_NOTVALID);
302*1ae08745Sheppo 			if (rv) {
303*1ae08745Sheppo 				DWARN("cnex_intr_redist: tx ino=0x%llx, "
304*1ae08745Sheppo 				    "can't set valid\n", cldcp->tx.ino);
305*1ae08745Sheppo 				mutex_exit(&cldcp->lock);
306*1ae08745Sheppo 				mutex_exit(&cnex_ssp->clist_lock);
307*1ae08745Sheppo 				return;
308*1ae08745Sheppo 			}
309*1ae08745Sheppo 
310*1ae08745Sheppo 			/*
311*1ae08745Sheppo 			 * Make a best effort to wait for pending interrupts
312*1ae08745Sheppo 			 * to finish. There is not much we can do if we timeout.
313*1ae08745Sheppo 			 */
314*1ae08745Sheppo 			start = gethrtime();
315*1ae08745Sheppo 
316*1ae08745Sheppo 			do {
317*1ae08745Sheppo 				rv = hvldc_intr_getstate(cnex_ssp->cfghdl,
318*1ae08745Sheppo 				    cldcp->tx.ino, &intr_state);
319*1ae08745Sheppo 				if (rv) {
320*1ae08745Sheppo 					DWARN("cnex_intr_redist: tx ino=0x%llx,"
321*1ae08745Sheppo 					    "can't get state\n", cldcp->tx.ino);
322*1ae08745Sheppo 					mutex_exit(&cldcp->lock);
323*1ae08745Sheppo 					mutex_exit(&cnex_ssp->clist_lock);
324*1ae08745Sheppo 					return;
325*1ae08745Sheppo 				}
326*1ae08745Sheppo 
327*1ae08745Sheppo 				if ((gethrtime() - start) > cnex_pending_tmout)
328*1ae08745Sheppo 					break;
329*1ae08745Sheppo 
330*1ae08745Sheppo 			} while (!panicstr &&
331*1ae08745Sheppo 			    intr_state == HV_INTR_DELIVERED_STATE);
332*1ae08745Sheppo 
333*1ae08745Sheppo 			(void) hvldc_intr_settarget(cnex_ssp->cfghdl,
334*1ae08745Sheppo 			    cldcp->tx.ino, cpuid);
335*1ae08745Sheppo 			(void) hvldc_intr_setvalid(cnex_ssp->cfghdl,
336*1ae08745Sheppo 			    cldcp->tx.ino, HV_INTR_VALID);
337*1ae08745Sheppo 		}
338*1ae08745Sheppo 
339*1ae08745Sheppo 		if (cldcp->rx.hdlr) {
340*1ae08745Sheppo 			/*
341*1ae08745Sheppo 			 * Don't do anything for disabled interrupts.
342*1ae08745Sheppo 			 */
343*1ae08745Sheppo 			rv = hvldc_intr_getvalid(cnex_ssp->cfghdl,
344*1ae08745Sheppo 			    cldcp->rx.ino, &intr_state);
345*1ae08745Sheppo 			if (rv) {
346*1ae08745Sheppo 				DWARN("cnex_intr_redist: rx ino=0x%llx, "
347*1ae08745Sheppo 				    "can't get valid\n", cldcp->rx.ino);
348*1ae08745Sheppo 				mutex_exit(&cldcp->lock);
349*1ae08745Sheppo 				mutex_exit(&cnex_ssp->clist_lock);
350*1ae08745Sheppo 				return;
351*1ae08745Sheppo 			}
352*1ae08745Sheppo 			if (intr_state == HV_INTR_NOTVALID) {
353*1ae08745Sheppo 				cldcp = cldcp->next;
354*1ae08745Sheppo 				continue;
355*1ae08745Sheppo 			}
356*1ae08745Sheppo 
357*1ae08745Sheppo 			cpuid = intr_dist_cpuid();
358*1ae08745Sheppo 
359*1ae08745Sheppo 			/* disable interrupts */
360*1ae08745Sheppo 			rv = hvldc_intr_setvalid(cnex_ssp->cfghdl,
361*1ae08745Sheppo 			    cldcp->rx.ino, HV_INTR_NOTVALID);
362*1ae08745Sheppo 			if (rv) {
363*1ae08745Sheppo 				DWARN("cnex_intr_redist: rx ino=0x%llx, "
364*1ae08745Sheppo 				    "can't set valid\n", cldcp->rx.ino);
365*1ae08745Sheppo 				mutex_exit(&cldcp->lock);
366*1ae08745Sheppo 				mutex_exit(&cnex_ssp->clist_lock);
367*1ae08745Sheppo 				return;
368*1ae08745Sheppo 			}
369*1ae08745Sheppo 
370*1ae08745Sheppo 			/*
371*1ae08745Sheppo 			 * Make a best effort to wait for pending interrupts
372*1ae08745Sheppo 			 * to finish. There is not much we can do if we timeout.
373*1ae08745Sheppo 			 */
374*1ae08745Sheppo 			start = gethrtime();
375*1ae08745Sheppo 
376*1ae08745Sheppo 			do {
377*1ae08745Sheppo 				rv = hvldc_intr_getstate(cnex_ssp->cfghdl,
378*1ae08745Sheppo 				    cldcp->rx.ino, &intr_state);
379*1ae08745Sheppo 				if (rv) {
380*1ae08745Sheppo 					DWARN("cnex_intr_redist: rx ino=0x%llx,"
381*1ae08745Sheppo 					    "can't set state\n", cldcp->rx.ino);
382*1ae08745Sheppo 					mutex_exit(&cldcp->lock);
383*1ae08745Sheppo 					mutex_exit(&cnex_ssp->clist_lock);
384*1ae08745Sheppo 					return;
385*1ae08745Sheppo 				}
386*1ae08745Sheppo 
387*1ae08745Sheppo 				if ((gethrtime() - start) > cnex_pending_tmout)
388*1ae08745Sheppo 					break;
389*1ae08745Sheppo 
390*1ae08745Sheppo 			} while (!panicstr &&
391*1ae08745Sheppo 			    intr_state == HV_INTR_DELIVERED_STATE);
392*1ae08745Sheppo 
393*1ae08745Sheppo 			(void) hvldc_intr_settarget(cnex_ssp->cfghdl,
394*1ae08745Sheppo 			    cldcp->rx.ino, cpuid);
395*1ae08745Sheppo 			(void) hvldc_intr_setvalid(cnex_ssp->cfghdl,
396*1ae08745Sheppo 			    cldcp->rx.ino, HV_INTR_VALID);
397*1ae08745Sheppo 		}
398*1ae08745Sheppo 
399*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
400*1ae08745Sheppo 
401*1ae08745Sheppo 		/* next channel */
402*1ae08745Sheppo 		cldcp = cldcp->next;
403*1ae08745Sheppo 	}
404*1ae08745Sheppo 
405*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
406*1ae08745Sheppo }
407*1ae08745Sheppo 
408*1ae08745Sheppo /*
409*1ae08745Sheppo  * Exported interface to register a LDC endpoint with
410*1ae08745Sheppo  * the channel nexus
411*1ae08745Sheppo  */
412*1ae08745Sheppo static int
413*1ae08745Sheppo cnex_reg_chan(dev_info_t *dip, uint64_t id, ldc_dev_t devclass)
414*1ae08745Sheppo {
415*1ae08745Sheppo 	int		idx;
416*1ae08745Sheppo 	cnex_ldc_t	*cldcp;
417*1ae08745Sheppo 	int		listsz, num_nodes, num_channels;
418*1ae08745Sheppo 	md_t		*mdp = NULL;
419*1ae08745Sheppo 	mde_cookie_t	rootnode, *listp = NULL;
420*1ae08745Sheppo 	uint64_t	tmp_id, rxino, txino;
421*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
422*1ae08745Sheppo 	int		status, instance;
423*1ae08745Sheppo 
424*1ae08745Sheppo 	/* Get device instance and structure */
425*1ae08745Sheppo 	instance = ddi_get_instance(dip);
426*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
427*1ae08745Sheppo 
428*1ae08745Sheppo 	/* Check to see if channel is already registered */
429*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
430*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
431*1ae08745Sheppo 	while (cldcp) {
432*1ae08745Sheppo 		if (cldcp->id == id) {
433*1ae08745Sheppo 			DWARN("cnex_reg_chan: channel 0x%llx exists\n", id);
434*1ae08745Sheppo 			mutex_exit(&cnex_ssp->clist_lock);
435*1ae08745Sheppo 			return (EINVAL);
436*1ae08745Sheppo 		}
437*1ae08745Sheppo 		cldcp = cldcp->next;
438*1ae08745Sheppo 	}
439*1ae08745Sheppo 
440*1ae08745Sheppo 	/* Get the Tx/Rx inos from the MD */
441*1ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
442*1ae08745Sheppo 		DWARN("cnex_reg_chan: cannot init MD\n");
443*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
444*1ae08745Sheppo 		return (ENXIO);
445*1ae08745Sheppo 	}
446*1ae08745Sheppo 	num_nodes = md_node_count(mdp);
447*1ae08745Sheppo 	ASSERT(num_nodes > 0);
448*1ae08745Sheppo 
449*1ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
450*1ae08745Sheppo 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
451*1ae08745Sheppo 
452*1ae08745Sheppo 	rootnode = md_root_node(mdp);
453*1ae08745Sheppo 
454*1ae08745Sheppo 	/* search for all channel_endpoint nodes */
455*1ae08745Sheppo 	num_channels = md_scan_dag(mdp, rootnode,
456*1ae08745Sheppo 	    md_find_name(mdp, "channel-endpoint"),
457*1ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
458*1ae08745Sheppo 	if (num_channels <= 0) {
459*1ae08745Sheppo 		DWARN("cnex_reg_chan: invalid channel id\n");
460*1ae08745Sheppo 		kmem_free(listp, listsz);
461*1ae08745Sheppo 		(void) md_fini_handle(mdp);
462*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
463*1ae08745Sheppo 		return (EINVAL);
464*1ae08745Sheppo 	}
465*1ae08745Sheppo 
466*1ae08745Sheppo 	for (idx = 0; idx < num_channels; idx++) {
467*1ae08745Sheppo 
468*1ae08745Sheppo 		/* Get the channel ID */
469*1ae08745Sheppo 		status = md_get_prop_val(mdp, listp[idx], "id", &tmp_id);
470*1ae08745Sheppo 		if (status) {
471*1ae08745Sheppo 			DWARN("cnex_reg_chan: cannot read LDC ID\n");
472*1ae08745Sheppo 			kmem_free(listp, listsz);
473*1ae08745Sheppo 			(void) md_fini_handle(mdp);
474*1ae08745Sheppo 			mutex_exit(&cnex_ssp->clist_lock);
475*1ae08745Sheppo 			return (ENXIO);
476*1ae08745Sheppo 		}
477*1ae08745Sheppo 		if (tmp_id != id)
478*1ae08745Sheppo 			continue;
479*1ae08745Sheppo 
480*1ae08745Sheppo 		/* Get the Tx and Rx ino */
481*1ae08745Sheppo 		status = md_get_prop_val(mdp, listp[idx], "tx-ino", &txino);
482*1ae08745Sheppo 		if (status) {
483*1ae08745Sheppo 			DWARN("cnex_reg_chan: cannot read Tx ino\n");
484*1ae08745Sheppo 			kmem_free(listp, listsz);
485*1ae08745Sheppo 			(void) md_fini_handle(mdp);
486*1ae08745Sheppo 			mutex_exit(&cnex_ssp->clist_lock);
487*1ae08745Sheppo 			return (ENXIO);
488*1ae08745Sheppo 		}
489*1ae08745Sheppo 		status = md_get_prop_val(mdp, listp[idx], "rx-ino", &rxino);
490*1ae08745Sheppo 		if (status) {
491*1ae08745Sheppo 			DWARN("cnex_reg_chan: cannot read Rx ino\n");
492*1ae08745Sheppo 			kmem_free(listp, listsz);
493*1ae08745Sheppo 			(void) md_fini_handle(mdp);
494*1ae08745Sheppo 			mutex_exit(&cnex_ssp->clist_lock);
495*1ae08745Sheppo 			return (ENXIO);
496*1ae08745Sheppo 		}
497*1ae08745Sheppo 	}
498*1ae08745Sheppo 	kmem_free(listp, listsz);
499*1ae08745Sheppo 	(void) md_fini_handle(mdp);
500*1ae08745Sheppo 
501*1ae08745Sheppo 	/* Allocate a new channel structure */
502*1ae08745Sheppo 	cldcp = kmem_zalloc(sizeof (*cldcp), KM_SLEEP);
503*1ae08745Sheppo 
504*1ae08745Sheppo 	/* Initialize the channel */
505*1ae08745Sheppo 	mutex_init(&cldcp->lock, NULL, MUTEX_DRIVER, NULL);
506*1ae08745Sheppo 
507*1ae08745Sheppo 	cldcp->id = id;
508*1ae08745Sheppo 	cldcp->tx.ino = txino;
509*1ae08745Sheppo 	cldcp->rx.ino = rxino;
510*1ae08745Sheppo 	cldcp->devclass = devclass;
511*1ae08745Sheppo 
512*1ae08745Sheppo 	/* add channel to nexus channel list */
513*1ae08745Sheppo 	cldcp->next = cnex_ssp->clist;
514*1ae08745Sheppo 	cnex_ssp->clist = cldcp;
515*1ae08745Sheppo 
516*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
517*1ae08745Sheppo 
518*1ae08745Sheppo 	return (0);
519*1ae08745Sheppo }
520*1ae08745Sheppo 
521*1ae08745Sheppo /*
522*1ae08745Sheppo  * Add Tx/Rx interrupt handler for the channel
523*1ae08745Sheppo  */
524*1ae08745Sheppo static int
525*1ae08745Sheppo cnex_add_intr(dev_info_t *dip, uint64_t id, cnex_intrtype_t itype,
526*1ae08745Sheppo     uint_t (*hdlr)(), caddr_t arg1, caddr_t arg2)
527*1ae08745Sheppo {
528*1ae08745Sheppo 	int		rv, idx, pil;
529*1ae08745Sheppo 	cnex_ldc_t	*cldcp;
530*1ae08745Sheppo 	cnex_intr_t	*iinfo;
531*1ae08745Sheppo 	uint64_t	cpuid;
532*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
533*1ae08745Sheppo 	int		instance;
534*1ae08745Sheppo 
535*1ae08745Sheppo 	/* Get device instance and structure */
536*1ae08745Sheppo 	instance = ddi_get_instance(dip);
537*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
538*1ae08745Sheppo 
539*1ae08745Sheppo 	/* get channel info */
540*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
541*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
542*1ae08745Sheppo 	while (cldcp) {
543*1ae08745Sheppo 		if (cldcp->id == id)
544*1ae08745Sheppo 			break;
545*1ae08745Sheppo 		cldcp = cldcp->next;
546*1ae08745Sheppo 	}
547*1ae08745Sheppo 	if (cldcp == NULL) {
548*1ae08745Sheppo 		DWARN("cnex_add_intr: channel 0x%llx does not exist\n", id);
549*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
550*1ae08745Sheppo 		return (EINVAL);
551*1ae08745Sheppo 	}
552*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
553*1ae08745Sheppo 
554*1ae08745Sheppo 	/* get channel lock */
555*1ae08745Sheppo 	mutex_enter(&cldcp->lock);
556*1ae08745Sheppo 
557*1ae08745Sheppo 	/* get interrupt type */
558*1ae08745Sheppo 	if (itype == CNEX_TX_INTR) {
559*1ae08745Sheppo 		iinfo = &(cldcp->tx);
560*1ae08745Sheppo 	} else if (itype == CNEX_RX_INTR) {
561*1ae08745Sheppo 		iinfo = &(cldcp->rx);
562*1ae08745Sheppo 	} else {
563*1ae08745Sheppo 		DWARN("cnex_add_intr: invalid interrupt type\n", id);
564*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
565*1ae08745Sheppo 		return (EINVAL);
566*1ae08745Sheppo 	}
567*1ae08745Sheppo 
568*1ae08745Sheppo 	/* check if a handler is already added */
569*1ae08745Sheppo 	if (iinfo->hdlr != 0) {
570*1ae08745Sheppo 		DWARN("cnex_add_intr: interrupt handler exists\n");
571*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
572*1ae08745Sheppo 		return (EINVAL);
573*1ae08745Sheppo 	}
574*1ae08745Sheppo 
575*1ae08745Sheppo 	/* save interrupt handler info */
576*1ae08745Sheppo 	iinfo->hdlr = hdlr;
577*1ae08745Sheppo 	iinfo->arg1 = arg1;
578*1ae08745Sheppo 	iinfo->arg2 = arg2;
579*1ae08745Sheppo 
580*1ae08745Sheppo 	iinfo->ssp = cnex_ssp;
581*1ae08745Sheppo 
582*1ae08745Sheppo 	/*
583*1ae08745Sheppo 	 * FIXME - generate the interrupt cookie
584*1ae08745Sheppo 	 * using the interrupt registry
585*1ae08745Sheppo 	 */
586*1ae08745Sheppo 	iinfo->icookie = cnex_ssp->cfghdl | iinfo->ino;
587*1ae08745Sheppo 
588*1ae08745Sheppo 	D1("cnex_add_intr: add hdlr, cfghdl=0x%llx, ino=0x%llx, "
589*1ae08745Sheppo 	    "cookie=0x%llx\n", cnex_ssp->cfghdl, iinfo->ino, iinfo->icookie);
590*1ae08745Sheppo 
591*1ae08745Sheppo 	/* Pick a PIL on the basis of the channel's devclass */
592*1ae08745Sheppo 	for (idx = 0, pil = PIL_3; idx < CNEX_MAX_DEVS; idx++) {
593*1ae08745Sheppo 		if (cldcp->devclass == cnex_class_to_pil[idx].devclass) {
594*1ae08745Sheppo 			pil = cnex_class_to_pil[idx].pil;
595*1ae08745Sheppo 			break;
596*1ae08745Sheppo 		}
597*1ae08745Sheppo 	}
598*1ae08745Sheppo 
599*1ae08745Sheppo 	/* add interrupt to solaris ivec table */
600*1ae08745Sheppo 	VERIFY(add_ivintr(iinfo->icookie, pil, cnex_intr_wrapper,
601*1ae08745Sheppo 		(caddr_t)iinfo, NULL) == 0);
602*1ae08745Sheppo 
603*1ae08745Sheppo 	/* set the cookie in the HV */
604*1ae08745Sheppo 	rv = hvldc_intr_setcookie(cnex_ssp->cfghdl, iinfo->ino, iinfo->icookie);
605*1ae08745Sheppo 
606*1ae08745Sheppo 	/* pick next CPU in the domain for this channel */
607*1ae08745Sheppo 	cpuid = intr_dist_cpuid();
608*1ae08745Sheppo 
609*1ae08745Sheppo 	/* set the target CPU and then enable interrupts */
610*1ae08745Sheppo 	rv = hvldc_intr_settarget(cnex_ssp->cfghdl, iinfo->ino, cpuid);
611*1ae08745Sheppo 	if (rv) {
612*1ae08745Sheppo 		DWARN("cnex_add_intr: ino=0x%llx, cannot set target cpu\n",
613*1ae08745Sheppo 		    iinfo->ino);
614*1ae08745Sheppo 		goto hv_error;
615*1ae08745Sheppo 	}
616*1ae08745Sheppo 	rv = hvldc_intr_setstate(cnex_ssp->cfghdl, iinfo->ino,
617*1ae08745Sheppo 	    HV_INTR_IDLE_STATE);
618*1ae08745Sheppo 	if (rv) {
619*1ae08745Sheppo 		DWARN("cnex_add_intr: ino=0x%llx, cannot set state\n",
620*1ae08745Sheppo 		    iinfo->ino);
621*1ae08745Sheppo 		goto hv_error;
622*1ae08745Sheppo 	}
623*1ae08745Sheppo 	rv = hvldc_intr_setvalid(cnex_ssp->cfghdl, iinfo->ino, HV_INTR_VALID);
624*1ae08745Sheppo 	if (rv) {
625*1ae08745Sheppo 		DWARN("cnex_add_intr: ino=0x%llx, cannot set valid\n",
626*1ae08745Sheppo 		    iinfo->ino);
627*1ae08745Sheppo 		goto hv_error;
628*1ae08745Sheppo 	}
629*1ae08745Sheppo 
630*1ae08745Sheppo 	mutex_exit(&cldcp->lock);
631*1ae08745Sheppo 	return (0);
632*1ae08745Sheppo 
633*1ae08745Sheppo hv_error:
634*1ae08745Sheppo 	(void) rem_ivintr(iinfo->icookie, NULL);
635*1ae08745Sheppo 	mutex_exit(&cldcp->lock);
636*1ae08745Sheppo 	return (ENXIO);
637*1ae08745Sheppo }
638*1ae08745Sheppo 
639*1ae08745Sheppo 
640*1ae08745Sheppo /*
641*1ae08745Sheppo  * Exported interface to unregister a LDC endpoint with
642*1ae08745Sheppo  * the channel nexus
643*1ae08745Sheppo  */
644*1ae08745Sheppo static int
645*1ae08745Sheppo cnex_unreg_chan(dev_info_t *dip, uint64_t id)
646*1ae08745Sheppo {
647*1ae08745Sheppo 	cnex_ldc_t	*cldcp, *prev_cldcp;
648*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
649*1ae08745Sheppo 	int		instance;
650*1ae08745Sheppo 
651*1ae08745Sheppo 	/* Get device instance and structure */
652*1ae08745Sheppo 	instance = ddi_get_instance(dip);
653*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
654*1ae08745Sheppo 
655*1ae08745Sheppo 	/* find and remove channel from list */
656*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
657*1ae08745Sheppo 	prev_cldcp = NULL;
658*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
659*1ae08745Sheppo 	while (cldcp) {
660*1ae08745Sheppo 		if (cldcp->id == id)
661*1ae08745Sheppo 			break;
662*1ae08745Sheppo 		prev_cldcp = cldcp;
663*1ae08745Sheppo 		cldcp = cldcp->next;
664*1ae08745Sheppo 	}
665*1ae08745Sheppo 
666*1ae08745Sheppo 	if (cldcp == 0) {
667*1ae08745Sheppo 		DWARN("cnex_unreg_chan: invalid channel %d\n", id);
668*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
669*1ae08745Sheppo 		return (EINVAL);
670*1ae08745Sheppo 	}
671*1ae08745Sheppo 
672*1ae08745Sheppo 	if (cldcp->tx.hdlr || cldcp->rx.hdlr) {
673*1ae08745Sheppo 		DWARN("cnex_unreg_chan: handlers still exist\n");
674*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
675*1ae08745Sheppo 		return (ENXIO);
676*1ae08745Sheppo 	}
677*1ae08745Sheppo 
678*1ae08745Sheppo 	if (prev_cldcp)
679*1ae08745Sheppo 		prev_cldcp->next = cldcp->next;
680*1ae08745Sheppo 	else
681*1ae08745Sheppo 		cnex_ssp->clist = cldcp->next;
682*1ae08745Sheppo 
683*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
684*1ae08745Sheppo 
685*1ae08745Sheppo 	/* destroy mutex */
686*1ae08745Sheppo 	mutex_destroy(&cldcp->lock);
687*1ae08745Sheppo 
688*1ae08745Sheppo 	/* free channel */
689*1ae08745Sheppo 	kmem_free(cldcp, sizeof (*cldcp));
690*1ae08745Sheppo 
691*1ae08745Sheppo 	return (0);
692*1ae08745Sheppo }
693*1ae08745Sheppo 
694*1ae08745Sheppo /*
695*1ae08745Sheppo  * Remove Tx/Rx interrupt handler for the channel
696*1ae08745Sheppo  */
697*1ae08745Sheppo static int
698*1ae08745Sheppo cnex_rem_intr(dev_info_t *dip, uint64_t id, cnex_intrtype_t itype)
699*1ae08745Sheppo {
700*1ae08745Sheppo 	int			rv;
701*1ae08745Sheppo 	cnex_ldc_t		*cldcp;
702*1ae08745Sheppo 	cnex_intr_t		*iinfo;
703*1ae08745Sheppo 	cnex_soft_state_t	*cnex_ssp;
704*1ae08745Sheppo 	hrtime_t 		start;
705*1ae08745Sheppo 	int			instance, istate;
706*1ae08745Sheppo 
707*1ae08745Sheppo 	/* Get device instance and structure */
708*1ae08745Sheppo 	instance = ddi_get_instance(dip);
709*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
710*1ae08745Sheppo 
711*1ae08745Sheppo 	/* get channel info */
712*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
713*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
714*1ae08745Sheppo 	while (cldcp) {
715*1ae08745Sheppo 		if (cldcp->id == id)
716*1ae08745Sheppo 			break;
717*1ae08745Sheppo 		cldcp = cldcp->next;
718*1ae08745Sheppo 	}
719*1ae08745Sheppo 	if (cldcp == NULL) {
720*1ae08745Sheppo 		DWARN("cnex_rem_intr: channel 0x%llx does not exist\n", id);
721*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
722*1ae08745Sheppo 		return (EINVAL);
723*1ae08745Sheppo 	}
724*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
725*1ae08745Sheppo 
726*1ae08745Sheppo 	/* get rid of the channel intr handler */
727*1ae08745Sheppo 	mutex_enter(&cldcp->lock);
728*1ae08745Sheppo 
729*1ae08745Sheppo 	/* get interrupt type */
730*1ae08745Sheppo 	if (itype == CNEX_TX_INTR) {
731*1ae08745Sheppo 		iinfo = &(cldcp->tx);
732*1ae08745Sheppo 	} else if (itype == CNEX_RX_INTR) {
733*1ae08745Sheppo 		iinfo = &(cldcp->rx);
734*1ae08745Sheppo 	} else {
735*1ae08745Sheppo 		DWARN("cnex_rem_intr: invalid interrupt type\n");
736*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
737*1ae08745Sheppo 		return (EINVAL);
738*1ae08745Sheppo 	}
739*1ae08745Sheppo 
740*1ae08745Sheppo 	D1("cnex_rem_intr: interrupt ino=0x%x\n", iinfo->ino);
741*1ae08745Sheppo 
742*1ae08745Sheppo 	/* check if a handler is already added */
743*1ae08745Sheppo 	if (iinfo->hdlr == 0) {
744*1ae08745Sheppo 		DWARN("cnex_rem_intr: interrupt handler does not exist\n");
745*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
746*1ae08745Sheppo 		return (EINVAL);
747*1ae08745Sheppo 	}
748*1ae08745Sheppo 
749*1ae08745Sheppo 	D1("cnex_rem_intr: set intr to invalid ino=0x%x\n", iinfo->ino);
750*1ae08745Sheppo 	rv = hvldc_intr_setvalid(cnex_ssp->cfghdl,
751*1ae08745Sheppo 	    iinfo->ino, HV_INTR_NOTVALID);
752*1ae08745Sheppo 	if (rv) {
753*1ae08745Sheppo 		DWARN("cnex_rem_intr: cannot set valid ino=%x\n", iinfo->ino);
754*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
755*1ae08745Sheppo 		return (ENXIO);
756*1ae08745Sheppo 	}
757*1ae08745Sheppo 
758*1ae08745Sheppo 	/*
759*1ae08745Sheppo 	 * Make a best effort to wait for pending interrupts
760*1ae08745Sheppo 	 * to finish. There is not much we can do if we timeout.
761*1ae08745Sheppo 	 */
762*1ae08745Sheppo 	start = gethrtime();
763*1ae08745Sheppo 	do {
764*1ae08745Sheppo 		rv = hvldc_intr_getstate(cnex_ssp->cfghdl, iinfo->ino, &istate);
765*1ae08745Sheppo 		if (rv) {
766*1ae08745Sheppo 			DWARN("cnex_rem_intr: ino=0x%llx, cannot get state\n",
767*1ae08745Sheppo 			    iinfo->ino);
768*1ae08745Sheppo 		}
769*1ae08745Sheppo 
770*1ae08745Sheppo 		if (rv || ((gethrtime() - start) > cnex_pending_tmout))
771*1ae08745Sheppo 			break;
772*1ae08745Sheppo 
773*1ae08745Sheppo 	} while (!panicstr && istate == HV_INTR_DELIVERED_STATE);
774*1ae08745Sheppo 
775*1ae08745Sheppo 	/* if interrupts are still pending print warning */
776*1ae08745Sheppo 	if (istate != HV_INTR_IDLE_STATE) {
777*1ae08745Sheppo 		DWARN("cnex_rem_intr: cannot remove intr busy ino=%x\n",
778*1ae08745Sheppo 		    iinfo->ino);
779*1ae08745Sheppo 		/* clear interrupt state */
780*1ae08745Sheppo 		(void) hvldc_intr_setstate(cnex_ssp->cfghdl, iinfo->ino,
781*1ae08745Sheppo 		    HV_INTR_IDLE_STATE);
782*1ae08745Sheppo 	}
783*1ae08745Sheppo 
784*1ae08745Sheppo 	/* remove interrupt */
785*1ae08745Sheppo 	rem_ivintr(iinfo->icookie, NULL);
786*1ae08745Sheppo 
787*1ae08745Sheppo 	/* clear interrupt info */
788*1ae08745Sheppo 	bzero(iinfo, sizeof (*iinfo));
789*1ae08745Sheppo 
790*1ae08745Sheppo 	mutex_exit(&cldcp->lock);
791*1ae08745Sheppo 
792*1ae08745Sheppo 	return (0);
793*1ae08745Sheppo }
794*1ae08745Sheppo 
795*1ae08745Sheppo 
796*1ae08745Sheppo /*
797*1ae08745Sheppo  * Clear pending Tx/Rx interrupt
798*1ae08745Sheppo  */
799*1ae08745Sheppo static int
800*1ae08745Sheppo cnex_clr_intr(dev_info_t *dip, uint64_t id, cnex_intrtype_t itype)
801*1ae08745Sheppo {
802*1ae08745Sheppo 	int			rv;
803*1ae08745Sheppo 	cnex_ldc_t		*cldcp;
804*1ae08745Sheppo 	cnex_intr_t		*iinfo;
805*1ae08745Sheppo 	cnex_soft_state_t	*cnex_ssp;
806*1ae08745Sheppo 	int			instance;
807*1ae08745Sheppo 
808*1ae08745Sheppo 	/* Get device instance and structure */
809*1ae08745Sheppo 	instance = ddi_get_instance(dip);
810*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
811*1ae08745Sheppo 
812*1ae08745Sheppo 	/* get channel info */
813*1ae08745Sheppo 	mutex_enter(&cnex_ssp->clist_lock);
814*1ae08745Sheppo 	cldcp = cnex_ssp->clist;
815*1ae08745Sheppo 	while (cldcp) {
816*1ae08745Sheppo 		if (cldcp->id == id)
817*1ae08745Sheppo 			break;
818*1ae08745Sheppo 		cldcp = cldcp->next;
819*1ae08745Sheppo 	}
820*1ae08745Sheppo 	if (cldcp == NULL) {
821*1ae08745Sheppo 		DWARN("cnex_clr_intr: channel 0x%llx does not exist\n", id);
822*1ae08745Sheppo 		mutex_exit(&cnex_ssp->clist_lock);
823*1ae08745Sheppo 		return (EINVAL);
824*1ae08745Sheppo 	}
825*1ae08745Sheppo 	mutex_exit(&cnex_ssp->clist_lock);
826*1ae08745Sheppo 
827*1ae08745Sheppo 	mutex_enter(&cldcp->lock);
828*1ae08745Sheppo 
829*1ae08745Sheppo 	/* get interrupt type */
830*1ae08745Sheppo 	if (itype == CNEX_TX_INTR) {
831*1ae08745Sheppo 		iinfo = &(cldcp->tx);
832*1ae08745Sheppo 	} else if (itype == CNEX_RX_INTR) {
833*1ae08745Sheppo 		iinfo = &(cldcp->rx);
834*1ae08745Sheppo 	} else {
835*1ae08745Sheppo 		DWARN("cnex_rem_intr: invalid interrupt type\n");
836*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
837*1ae08745Sheppo 		return (EINVAL);
838*1ae08745Sheppo 	}
839*1ae08745Sheppo 
840*1ae08745Sheppo 	D1("cnex_rem_intr: interrupt ino=0x%x\n", iinfo->ino);
841*1ae08745Sheppo 
842*1ae08745Sheppo 	/* check if a handler is already added */
843*1ae08745Sheppo 	if (iinfo->hdlr == 0) {
844*1ae08745Sheppo 		DWARN("cnex_clr_intr: interrupt handler does not exist\n");
845*1ae08745Sheppo 		mutex_exit(&cldcp->lock);
846*1ae08745Sheppo 		return (EINVAL);
847*1ae08745Sheppo 	}
848*1ae08745Sheppo 
849*1ae08745Sheppo 	rv = hvldc_intr_setstate(cnex_ssp->cfghdl, iinfo->ino,
850*1ae08745Sheppo 	    HV_INTR_IDLE_STATE);
851*1ae08745Sheppo 	if (rv) {
852*1ae08745Sheppo 		DWARN("cnex_intr_wrapper: cannot clear interrupt state\n");
853*1ae08745Sheppo 	}
854*1ae08745Sheppo 
855*1ae08745Sheppo 	mutex_exit(&cldcp->lock);
856*1ae08745Sheppo 
857*1ae08745Sheppo 	return (0);
858*1ae08745Sheppo }
859*1ae08745Sheppo 
860*1ae08745Sheppo /*
861*1ae08745Sheppo  * Channel nexus interrupt handler wrapper
862*1ae08745Sheppo  */
863*1ae08745Sheppo static uint_t
864*1ae08745Sheppo cnex_intr_wrapper(caddr_t arg)
865*1ae08745Sheppo {
866*1ae08745Sheppo 	int 			res;
867*1ae08745Sheppo 	uint_t 			(*handler)();
868*1ae08745Sheppo 	caddr_t 		handler_arg1;
869*1ae08745Sheppo 	caddr_t 		handler_arg2;
870*1ae08745Sheppo 	cnex_intr_t 		*iinfo = (cnex_intr_t *)arg;
871*1ae08745Sheppo 
872*1ae08745Sheppo 	ASSERT(iinfo != NULL);
873*1ae08745Sheppo 
874*1ae08745Sheppo 	handler = iinfo->hdlr;
875*1ae08745Sheppo 	handler_arg1 = iinfo->arg1;
876*1ae08745Sheppo 	handler_arg2 = iinfo->arg2;
877*1ae08745Sheppo 
878*1ae08745Sheppo 	D1("cnex_intr_wrapper: ino=0x%llx invoke client handler\n", iinfo->ino);
879*1ae08745Sheppo 	res = (*handler)(handler_arg1, handler_arg2);
880*1ae08745Sheppo 
881*1ae08745Sheppo 	return (res);
882*1ae08745Sheppo }
883*1ae08745Sheppo 
884*1ae08745Sheppo /*ARGSUSED*/
885*1ae08745Sheppo static int
886*1ae08745Sheppo cnex_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
887*1ae08745Sheppo {
888*1ae08745Sheppo 	int 		rv, instance, reglen;
889*1ae08745Sheppo 	cnex_regspec_t	*reg_p;
890*1ae08745Sheppo 	ldc_cnex_t	cinfo;
891*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
892*1ae08745Sheppo 
893*1ae08745Sheppo 	switch (cmd) {
894*1ae08745Sheppo 	case DDI_ATTACH:
895*1ae08745Sheppo 		break;
896*1ae08745Sheppo 	case DDI_RESUME:
897*1ae08745Sheppo 		return (DDI_SUCCESS);
898*1ae08745Sheppo 	default:
899*1ae08745Sheppo 		return (DDI_FAILURE);
900*1ae08745Sheppo 	}
901*1ae08745Sheppo 
902*1ae08745Sheppo 	/*
903*1ae08745Sheppo 	 * Get the instance specific soft state structure.
904*1ae08745Sheppo 	 * Save the devi for this instance in the soft_state data.
905*1ae08745Sheppo 	 */
906*1ae08745Sheppo 	instance = ddi_get_instance(devi);
907*1ae08745Sheppo 	if (ddi_soft_state_zalloc(cnex_state, instance) != DDI_SUCCESS)
908*1ae08745Sheppo 		return (DDI_FAILURE);
909*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
910*1ae08745Sheppo 
911*1ae08745Sheppo 	cnex_ssp->devi = devi;
912*1ae08745Sheppo 	cnex_ssp->clist = NULL;
913*1ae08745Sheppo 
914*1ae08745Sheppo 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
915*1ae08745Sheppo 		"reg", (caddr_t)&reg_p, &reglen) != DDI_SUCCESS) {
916*1ae08745Sheppo 		return (DDI_FAILURE);
917*1ae08745Sheppo 	}
918*1ae08745Sheppo 
919*1ae08745Sheppo 	/* get the sun4v config handle for this device */
920*1ae08745Sheppo 	cnex_ssp->cfghdl = SUN4V_REG_SPEC2CFG_HDL(reg_p->physaddr);
921*1ae08745Sheppo 	kmem_free(reg_p, reglen);
922*1ae08745Sheppo 
923*1ae08745Sheppo 	D1("cnex_attach: cfghdl=0x%llx\n", cnex_ssp->cfghdl);
924*1ae08745Sheppo 
925*1ae08745Sheppo 	/* init channel list mutex */
926*1ae08745Sheppo 	mutex_init(&cnex_ssp->clist_lock, NULL, MUTEX_DRIVER, NULL);
927*1ae08745Sheppo 
928*1ae08745Sheppo 	/* Register with LDC module */
929*1ae08745Sheppo 	cinfo.dip = devi;
930*1ae08745Sheppo 	cinfo.reg_chan = cnex_reg_chan;
931*1ae08745Sheppo 	cinfo.unreg_chan = cnex_unreg_chan;
932*1ae08745Sheppo 	cinfo.add_intr = cnex_add_intr;
933*1ae08745Sheppo 	cinfo.rem_intr = cnex_rem_intr;
934*1ae08745Sheppo 	cinfo.clr_intr = cnex_clr_intr;
935*1ae08745Sheppo 
936*1ae08745Sheppo 	/*
937*1ae08745Sheppo 	 * LDC register will fail if an nexus instance had already
938*1ae08745Sheppo 	 * registered with the LDC framework
939*1ae08745Sheppo 	 */
940*1ae08745Sheppo 	rv = ldc_register(&cinfo);
941*1ae08745Sheppo 	if (rv) {
942*1ae08745Sheppo 		DWARN("cnex_attach: unable to register with LDC\n");
943*1ae08745Sheppo 		ddi_soft_state_free(cnex_state, instance);
944*1ae08745Sheppo 		mutex_destroy(&cnex_ssp->clist_lock);
945*1ae08745Sheppo 		return (DDI_FAILURE);
946*1ae08745Sheppo 	}
947*1ae08745Sheppo 
948*1ae08745Sheppo 	if (ddi_create_minor_node(devi, "devctl", S_IFCHR, instance,
949*1ae08745Sheppo 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
950*1ae08745Sheppo 		ddi_remove_minor_node(devi, NULL);
951*1ae08745Sheppo 		ddi_soft_state_free(cnex_state, instance);
952*1ae08745Sheppo 		mutex_destroy(&cnex_ssp->clist_lock);
953*1ae08745Sheppo 		return (DDI_FAILURE);
954*1ae08745Sheppo 	}
955*1ae08745Sheppo 
956*1ae08745Sheppo 	/* Add interrupt redistribution callback. */
957*1ae08745Sheppo 	intr_dist_add(cnex_intr_redist, cnex_ssp);
958*1ae08745Sheppo 
959*1ae08745Sheppo 	ddi_report_dev(devi);
960*1ae08745Sheppo 	return (DDI_SUCCESS);
961*1ae08745Sheppo }
962*1ae08745Sheppo 
963*1ae08745Sheppo /*ARGSUSED*/
964*1ae08745Sheppo static int
965*1ae08745Sheppo cnex_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
966*1ae08745Sheppo {
967*1ae08745Sheppo 	int 		instance;
968*1ae08745Sheppo 	ldc_cnex_t	cinfo;
969*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
970*1ae08745Sheppo 
971*1ae08745Sheppo 	switch (cmd) {
972*1ae08745Sheppo 	case DDI_DETACH:
973*1ae08745Sheppo 		break;
974*1ae08745Sheppo 	case DDI_SUSPEND:
975*1ae08745Sheppo 		return (DDI_SUCCESS);
976*1ae08745Sheppo 	default:
977*1ae08745Sheppo 		return (DDI_FAILURE);
978*1ae08745Sheppo 	}
979*1ae08745Sheppo 
980*1ae08745Sheppo 	instance = ddi_get_instance(devi);
981*1ae08745Sheppo 	cnex_ssp = ddi_get_soft_state(cnex_state, instance);
982*1ae08745Sheppo 
983*1ae08745Sheppo 	/* check if there are any channels still registered */
984*1ae08745Sheppo 	if (cnex_ssp->clist) {
985*1ae08745Sheppo 		cmn_err(CE_WARN, "?cnex_dettach: channels registered %d\n",
986*1ae08745Sheppo 		    ddi_get_instance(devi));
987*1ae08745Sheppo 		return (DDI_FAILURE);
988*1ae08745Sheppo 	}
989*1ae08745Sheppo 
990*1ae08745Sheppo 	/* Unregister with LDC module */
991*1ae08745Sheppo 	cinfo.dip = devi;
992*1ae08745Sheppo 	(void) ldc_unregister(&cinfo);
993*1ae08745Sheppo 
994*1ae08745Sheppo 	/* Remove interrupt redistribution callback. */
995*1ae08745Sheppo 	intr_dist_rem(cnex_intr_redist, cnex_ssp);
996*1ae08745Sheppo 
997*1ae08745Sheppo 	/* destroy mutex */
998*1ae08745Sheppo 	mutex_destroy(&cnex_ssp->clist_lock);
999*1ae08745Sheppo 
1000*1ae08745Sheppo 	/* free soft state structure */
1001*1ae08745Sheppo 	ddi_soft_state_free(cnex_state, instance);
1002*1ae08745Sheppo 
1003*1ae08745Sheppo 	return (DDI_SUCCESS);
1004*1ae08745Sheppo }
1005*1ae08745Sheppo 
1006*1ae08745Sheppo /*ARGSUSED*/
1007*1ae08745Sheppo static int
1008*1ae08745Sheppo cnex_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1009*1ae08745Sheppo {
1010*1ae08745Sheppo 	int instance;
1011*1ae08745Sheppo 
1012*1ae08745Sheppo 	if (otyp != OTYP_CHR)
1013*1ae08745Sheppo 		return (EINVAL);
1014*1ae08745Sheppo 
1015*1ae08745Sheppo 	instance = getminor(*devp);
1016*1ae08745Sheppo 	if (ddi_get_soft_state(cnex_state, instance) == NULL)
1017*1ae08745Sheppo 		return (ENXIO);
1018*1ae08745Sheppo 
1019*1ae08745Sheppo 	return (0);
1020*1ae08745Sheppo }
1021*1ae08745Sheppo 
1022*1ae08745Sheppo /*ARGSUSED*/
1023*1ae08745Sheppo static int
1024*1ae08745Sheppo cnex_close(dev_t dev, int flags, int otyp, cred_t *credp)
1025*1ae08745Sheppo {
1026*1ae08745Sheppo 	int instance;
1027*1ae08745Sheppo 
1028*1ae08745Sheppo 	if (otyp != OTYP_CHR)
1029*1ae08745Sheppo 		return (EINVAL);
1030*1ae08745Sheppo 
1031*1ae08745Sheppo 	instance = getminor(dev);
1032*1ae08745Sheppo 	if (ddi_get_soft_state(cnex_state, instance) == NULL)
1033*1ae08745Sheppo 		return (ENXIO);
1034*1ae08745Sheppo 
1035*1ae08745Sheppo 	return (0);
1036*1ae08745Sheppo }
1037*1ae08745Sheppo 
1038*1ae08745Sheppo /*ARGSUSED*/
1039*1ae08745Sheppo static int
1040*1ae08745Sheppo cnex_ioctl(dev_t dev,
1041*1ae08745Sheppo     int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
1042*1ae08745Sheppo {
1043*1ae08745Sheppo 	int instance;
1044*1ae08745Sheppo 	cnex_soft_state_t *cnex_ssp;
1045*1ae08745Sheppo 
1046*1ae08745Sheppo 	instance = getminor(dev);
1047*1ae08745Sheppo 	if ((cnex_ssp = ddi_get_soft_state(cnex_state, instance)) == NULL)
1048*1ae08745Sheppo 		return (ENXIO);
1049*1ae08745Sheppo 	ASSERT(cnex_ssp->devi);
1050*1ae08745Sheppo 	return (ndi_devctl_ioctl(cnex_ssp->devi, cmd, arg, mode, 0));
1051*1ae08745Sheppo }
1052*1ae08745Sheppo 
1053*1ae08745Sheppo static int
1054*1ae08745Sheppo cnex_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
1055*1ae08745Sheppo     void *arg, void *result)
1056*1ae08745Sheppo {
1057*1ae08745Sheppo 	char		name[MAXNAMELEN];
1058*1ae08745Sheppo 	uint32_t	reglen;
1059*1ae08745Sheppo 	int		*cnex_regspec;
1060*1ae08745Sheppo 
1061*1ae08745Sheppo 	switch (ctlop) {
1062*1ae08745Sheppo 	case DDI_CTLOPS_REPORTDEV:
1063*1ae08745Sheppo 		if (rdip == NULL)
1064*1ae08745Sheppo 			return (DDI_FAILURE);
1065*1ae08745Sheppo 		cmn_err(CE_CONT, "?channel-device: %s%d\n",
1066*1ae08745Sheppo 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
1067*1ae08745Sheppo 		return (DDI_SUCCESS);
1068*1ae08745Sheppo 
1069*1ae08745Sheppo 	case DDI_CTLOPS_INITCHILD:
1070*1ae08745Sheppo 	{
1071*1ae08745Sheppo 		dev_info_t *child = (dev_info_t *)arg;
1072*1ae08745Sheppo 
1073*1ae08745Sheppo 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
1074*1ae08745Sheppo 			DDI_PROP_DONTPASS, "reg",
1075*1ae08745Sheppo 			&cnex_regspec, &reglen) != DDI_SUCCESS) {
1076*1ae08745Sheppo 			return (DDI_FAILURE);
1077*1ae08745Sheppo 		}
1078*1ae08745Sheppo 
1079*1ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%x", *cnex_regspec);
1080*1ae08745Sheppo 		ddi_set_name_addr(child, name);
1081*1ae08745Sheppo 		ddi_set_parent_data(child, NULL);
1082*1ae08745Sheppo 		ddi_prop_free(cnex_regspec);
1083*1ae08745Sheppo 		return (DDI_SUCCESS);
1084*1ae08745Sheppo 	}
1085*1ae08745Sheppo 
1086*1ae08745Sheppo 	case DDI_CTLOPS_UNINITCHILD:
1087*1ae08745Sheppo 	{
1088*1ae08745Sheppo 		dev_info_t *child = (dev_info_t *)arg;
1089*1ae08745Sheppo 
1090*1ae08745Sheppo 		NDI_CONFIG_DEBUG((CE_NOTE,
1091*1ae08745Sheppo 		    "DDI_CTLOPS_UNINITCHILD(%s, instance=%d)",
1092*1ae08745Sheppo 		    ddi_driver_name(child), DEVI(child)->devi_instance));
1093*1ae08745Sheppo 
1094*1ae08745Sheppo 		ddi_set_name_addr(child, NULL);
1095*1ae08745Sheppo 
1096*1ae08745Sheppo 		return (DDI_SUCCESS);
1097*1ae08745Sheppo 	}
1098*1ae08745Sheppo 
1099*1ae08745Sheppo 	case DDI_CTLOPS_DMAPMAPC:
1100*1ae08745Sheppo 	case DDI_CTLOPS_REPORTINT:
1101*1ae08745Sheppo 	case DDI_CTLOPS_REGSIZE:
1102*1ae08745Sheppo 	case DDI_CTLOPS_NREGS:
1103*1ae08745Sheppo 	case DDI_CTLOPS_SIDDEV:
1104*1ae08745Sheppo 	case DDI_CTLOPS_SLAVEONLY:
1105*1ae08745Sheppo 	case DDI_CTLOPS_AFFINITY:
1106*1ae08745Sheppo 	case DDI_CTLOPS_POKE:
1107*1ae08745Sheppo 	case DDI_CTLOPS_PEEK:
1108*1ae08745Sheppo 		/*
1109*1ae08745Sheppo 		 * These ops correspond to functions that "shouldn't" be called
1110*1ae08745Sheppo 		 * by a channel-device driver.  So we whine when we're called.
1111*1ae08745Sheppo 		 */
1112*1ae08745Sheppo 		cmn_err(CE_WARN, "%s%d: invalid op (%d) from %s%d\n",
1113*1ae08745Sheppo 		    ddi_driver_name(dip), ddi_get_instance(dip), ctlop,
1114*1ae08745Sheppo 		    ddi_driver_name(rdip), ddi_get_instance(rdip));
1115*1ae08745Sheppo 		return (DDI_FAILURE);
1116*1ae08745Sheppo 
1117*1ae08745Sheppo 	case DDI_CTLOPS_ATTACH:
1118*1ae08745Sheppo 	case DDI_CTLOPS_BTOP:
1119*1ae08745Sheppo 	case DDI_CTLOPS_BTOPR:
1120*1ae08745Sheppo 	case DDI_CTLOPS_DETACH:
1121*1ae08745Sheppo 	case DDI_CTLOPS_DVMAPAGESIZE:
1122*1ae08745Sheppo 	case DDI_CTLOPS_IOMIN:
1123*1ae08745Sheppo 	case DDI_CTLOPS_POWER:
1124*1ae08745Sheppo 	case DDI_CTLOPS_PTOB:
1125*1ae08745Sheppo 	default:
1126*1ae08745Sheppo 		/*
1127*1ae08745Sheppo 		 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
1128*1ae08745Sheppo 		 */
1129*1ae08745Sheppo 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
1130*1ae08745Sheppo 	}
1131*1ae08745Sheppo }
1132*1ae08745Sheppo 
1133*1ae08745Sheppo /* -------------------------------------------------------------------------- */
1134