xpvd.c revision 7656:2621e50fdf4a
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/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 *	Host to hypervisor virtual devices nexus driver
29 *
30 * TODO:
31 * - Add watchpoints on vbd/vif and enumerate/offline on watch callback
32 * - Add DR IOCTLs
33 * - Filter/restrict property lookups into xenstore
34 */
35
36#include <sys/conf.h>
37#include <sys/kmem.h>
38#include <sys/debug.h>
39#include <sys/modctl.h>
40#include <sys/autoconf.h>
41#include <sys/ddi_impldefs.h>
42#include <sys/ddi_subrdefs.h>
43#include <sys/ddi.h>
44#include <sys/sunddi.h>
45#include <sys/sunndi.h>
46#include <sys/avintr.h>
47#include <sys/psm.h>
48#include <sys/spl.h>
49#include <sys/promif.h>
50#include <sys/list.h>
51#include <sys/bootconf.h>
52#include <sys/bootsvcs.h>
53#include <util/sscanf.h>
54#include <sys/mach_intr.h>
55#include <sys/bootinfo.h>
56#ifdef XPV_HVM_DRIVER
57#include <sys/xpv_support.h>
58#include <sys/hypervisor.h>
59#include <sys/archsystm.h>
60#include <sys/cpu.h>
61#include <public/xen.h>
62#include <public/event_channel.h>
63#include <public/io/xenbus.h>
64#else
65#include <sys/hypervisor.h>
66#include <sys/evtchn_impl.h>
67#include <sys/xen_mmu.h>
68#endif
69#include <xen/sys/xenbus_impl.h>
70#include <xen/sys/xendev.h>
71
72/*
73 * DDI dev_ops entrypoints
74 */
75static int xpvd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
76static int xpvd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
77static int xpvd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
78
79
80/*
81 * NDI bus_ops entrypoints
82 */
83static int xpvd_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
84	void *);
85static int xpvd_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
86	ddi_intr_handle_impl_t *, void *);
87static int xpvd_prop_op(dev_t, dev_info_t *, dev_info_t *, ddi_prop_op_t,
88	int, char *, caddr_t, int *);
89static int xpvd_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t,
90	void *, dev_info_t **);
91static int xpvd_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t,
92    void *);
93static int xpvd_get_eventcookie(dev_info_t *, dev_info_t *,
94    char *, ddi_eventcookie_t *);
95static int xpvd_add_eventcall(dev_info_t *, dev_info_t *,
96    ddi_eventcookie_t, void (*)(dev_info_t *,
97    ddi_eventcookie_t, void *, void *),
98    void *, ddi_callback_id_t *);
99static int xpvd_remove_eventcall(dev_info_t *, ddi_callback_id_t);
100static int xpvd_post_event(dev_info_t *, dev_info_t *,
101    ddi_eventcookie_t, void *);
102
103/*
104 * misc functions
105 */
106static int xpvd_enable_intr(dev_info_t *, ddi_intr_handle_impl_t *, int);
107static void xpvd_disable_intr(dev_info_t *, ddi_intr_handle_impl_t *, int);
108static int xpvd_removechild(dev_info_t *);
109static int xpvd_initchild(dev_info_t *);
110static int xpvd_name_child(dev_info_t *, char *, int);
111static boolean_t i_xpvd_parse_devname(char *, xendev_devclass_t *,
112    domid_t *, int *);
113
114
115/* Extern declarations */
116extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
117    psm_intr_op_t, int *);
118
119struct bus_ops xpvd_bus_ops = {
120	BUSO_REV,
121	i_ddi_bus_map,
122	NULL,
123	NULL,
124	NULL,
125	i_ddi_map_fault,
126	ddi_dma_map,
127	ddi_dma_allochdl,
128	ddi_dma_freehdl,
129	ddi_dma_bindhdl,
130	ddi_dma_unbindhdl,
131	ddi_dma_flush,
132	ddi_dma_win,
133	ddi_dma_mctl,
134	xpvd_ctlops,
135	xpvd_prop_op,
136	xpvd_get_eventcookie,
137	xpvd_add_eventcall,
138	xpvd_remove_eventcall,
139	xpvd_post_event,
140	0,		/* (*bus_intr_ctl)(); */
141	xpvd_bus_config,
142	xpvd_bus_unconfig,
143	NULL,		/* (*bus_fm_init)(); */
144	NULL,		/* (*bus_fm_fini)(); */
145	NULL,		/* (*bus_fm_access_enter)(); */
146	NULL,		/* (*bus_fm_access_exit)(); */
147	NULL,		/* (*bus_power)(); */
148	xpvd_intr_ops	/* (*bus_intr_op)(); */
149};
150
151struct dev_ops xpvd_ops = {
152	DEVO_REV,		/* devo_rev */
153	0,			/* refcnt  */
154	xpvd_info,		/* info */
155	nulldev,		/* identify */
156	nulldev,		/* probe */
157	xpvd_attach,		/* attach */
158	xpvd_detach,		/* detach */
159	nulldev,		/* reset */
160	(struct cb_ops *)0,	/* driver operations */
161	&xpvd_bus_ops,		/* bus operations */
162	NULL,			/* power */
163	ddi_quiesce_not_needed,		/* quiesce */
164};
165
166
167dev_info_t *xpvd_dip;
168
169#define	CF_DBG		0x1
170#define	ALL_DBG		0xff
171
172static ndi_event_definition_t xpvd_ndi_event_defs[] = {
173	{ 0, XS_OE_STATE, EPL_KERNEL, NDI_EVENT_POST_TO_TGT },
174	{ 1, XS_HP_STATE, EPL_KERNEL, NDI_EVENT_POST_TO_TGT },
175};
176
177#define	XENDEV_N_NDI_EVENTS \
178	(sizeof (xpvd_ndi_event_defs) / sizeof (xpvd_ndi_event_defs[0]))
179
180static ndi_event_set_t xpvd_ndi_events = {
181	NDI_EVENTS_REV1, XENDEV_N_NDI_EVENTS, xpvd_ndi_event_defs
182};
183
184static ndi_event_hdl_t xpvd_ndi_event_handle;
185
186/*
187 * Hypervisor interrupt capabilities
188 */
189#define	XENDEV_INTR_CAPABILITIES \
190	(DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_MASKABLE | DDI_INTR_FLAG_PENDING)
191
192/*
193 * Module linkage information for the kernel.
194 */
195
196static struct modldrv modldrv = {
197	&mod_driverops, /* Type of module */
198	"virtual device nexus driver",
199	&xpvd_ops,	/* driver ops */
200};
201
202static struct modlinkage modlinkage = {
203	MODREV_1,
204	(void *)&modldrv,
205	NULL
206};
207
208int
209_init(void)
210{
211	return (mod_install(&modlinkage));
212}
213
214int
215_fini(void)
216{
217	return (mod_remove(&modlinkage));
218}
219
220int
221_info(struct modinfo *modinfop)
222{
223	return (mod_info(&modlinkage, modinfop));
224}
225
226/* ARGSUSED */
227static int
228xpvd_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
229{
230	switch (cmd) {
231	default:
232		return (DDI_FAILURE);
233
234	case DDI_INFO_DEVT2INSTANCE:
235		*result = (void *)0;
236		return (DDI_SUCCESS);
237
238	case DDI_INFO_DEVT2DEVINFO:
239		*result = (void *)xpvd_dip;
240		return (DDI_SUCCESS);
241	}
242}
243
244/*ARGSUSED*/
245static int
246xpvd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
247{
248	extern void xvdi_watch_devices(int);
249
250#ifdef XPV_HVM_DRIVER
251	if (xen_info == NULL) {
252		if (ddi_hold_installed_driver(ddi_name_to_major("xpv")) ==
253		    NULL) {
254			cmn_err(CE_WARN, "Couldn't initialize xpv framework");
255			return (DDI_FAILURE);
256		}
257	}
258#endif /* XPV_HVM_DRIVER */
259
260	if (ndi_event_alloc_hdl(devi, 0, &xpvd_ndi_event_handle,
261	    NDI_SLEEP) != NDI_SUCCESS) {
262		xpvd_dip = NULL;
263		return (DDI_FAILURE);
264	}
265	if (ndi_event_bind_set(xpvd_ndi_event_handle, &xpvd_ndi_events,
266	    NDI_SLEEP) != NDI_SUCCESS) {
267		(void) ndi_event_free_hdl(xpvd_ndi_event_handle);
268		xpvd_dip = NULL;
269		return (DDI_FAILURE);
270	}
271
272#ifdef XPV_HVM_DRIVER
273	(void) ddi_prop_update_int(DDI_DEV_T_NONE, devi, DDI_NO_AUTODETACH, 1);
274
275	/*
276	 * Report our version to dom0.
277	 */
278	if (xenbus_printf(XBT_NULL, "hvmpv/xpvd", "version", "%d",
279	    HVMPV_XPVD_VERS))
280		cmn_err(CE_WARN, "xpvd: couldn't write version\n");
281#endif /* XPV_HVM_DRIVER */
282
283	/* watch both frontend and backend for new devices */
284	if (DOMAIN_IS_INITDOMAIN(xen_info))
285		(void) xs_register_xenbus_callback(xvdi_watch_devices);
286	else
287		xvdi_watch_devices(XENSTORE_UP);
288
289	xpvd_dip = devi;
290	ddi_report_dev(devi);
291
292	return (DDI_SUCCESS);
293}
294
295/*ARGSUSED*/
296static int
297xpvd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
298{
299	return (DDI_FAILURE);
300}
301
302/*
303 * xpvd_prop_op()
304 *
305 * Query xenstore for the value of properties if DDI_PROP_NOTPROM
306 * is not set.  Xenstore property values are represented as ascii strings.
307 */
308static int
309xpvd_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
310    ddi_prop_op_t prop_op, int mod_flags, char *name, caddr_t valuep,
311    int *lengthp)
312{
313	caddr_t buff;
314	struct xendev_ppd *pdp;
315	void *prop_str;
316	size_t prop_len;
317	unsigned int len;
318	int rv;
319
320	pdp = (struct xendev_ppd *)ddi_get_parent_data(ch_dip);
321
322	if ((pdp == NULL) || !(mod_flags & (DDI_PROP_CANSLEEP)) ||
323	    (mod_flags & DDI_PROP_NOTPROM) || (pdp->xd_xsdev.nodename == NULL))
324		goto toss_off;
325	/*
326	 * First try reading the property off the the frontend. if that
327	 * fails, try and read it from the backend node.  If that
328	 * also fails, pass the request on the DDI framework
329	 */
330	prop_str = NULL;
331	if ((xenbus_read(XBT_NULL, pdp->xd_xsdev.nodename, name, &prop_str,
332	    &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0))
333		goto got_xs_prop;
334
335	prop_str = NULL;
336	if ((pdp->xd_xsdev.otherend != NULL) &&
337	    (xenbus_read(XBT_NULL, pdp->xd_xsdev.otherend, name, &prop_str,
338	    &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0))
339		goto got_xs_prop;
340
341toss_off:
342	return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
343	    mod_flags | DDI_PROP_NOTPROM, name, valuep, lengthp));
344
345got_xs_prop:
346	prop_len = strlen(prop_str) + 1;
347	rv = DDI_PROP_SUCCESS;
348
349	switch (prop_op) {
350	case PROP_LEN:
351		*lengthp = prop_len;
352		break;
353
354	case PROP_LEN_AND_VAL_ALLOC:
355		buff = kmem_alloc((size_t)prop_len, KM_SLEEP);
356		*(caddr_t *)valuep = (caddr_t)buff;
357		break;
358	case PROP_LEN_AND_VAL_BUF:
359		buff = (caddr_t)valuep;
360		if (*lengthp < prop_len)
361			rv = DDI_PROP_BUF_TOO_SMALL;
362		break;
363	default:
364		rv = DDI_PROP_INVAL_ARG;
365		break;
366	}
367
368	if ((rv == DDI_PROP_SUCCESS) && (prop_len > 0)) {
369		bcopy(prop_str, buff, prop_len);
370		*lengthp = prop_len;
371	}
372	kmem_free(prop_str, len);
373	return (rv);
374}
375
376
377/*
378 * return address of the device's interrupt spec structure.
379 */
380/*ARGSUSED*/
381struct intrspec *
382xpvd_get_ispec(dev_info_t *rdip, uint_t inumber)
383{
384	struct xendev_ppd *pdp;
385
386	ASSERT(inumber == 0);
387
388	if ((pdp = ddi_get_parent_data(rdip)) == NULL)
389		return (NULL);
390
391	return (&pdp->xd_ispec);
392}
393
394/*
395 * return (and determine) the interrupt priority of the device.
396 */
397/*ARGSUSED*/
398static int
399xpvd_get_priority(dev_info_t *dip, int inum, int *pri)
400{
401	struct xendev_ppd *pdp;
402	struct intrspec *ispec;
403	int	*intpriorities;
404	uint_t	num_intpriorities;
405
406	DDI_INTR_NEXDBG((CE_CONT, "xpvd_get_priority: dip = 0x%p\n",
407	    (void *)dip));
408
409	ASSERT(inum == 0);
410
411	if ((pdp = ddi_get_parent_data(dip)) == NULL)
412		return (DDI_FAILURE);
413
414	ispec = &pdp->xd_ispec;
415
416	/*
417	 * Set the default priority based on the device class.  The
418	 * "interrupt-priorities" property can be used to override
419	 * the default.
420	 */
421	if (ispec->intrspec_pri == 0) {
422		ispec->intrspec_pri = xendev_devclass_ipl(pdp->xd_devclass);
423		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
424		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
425		    "interrupt-priorities", &intpriorities,
426		    &num_intpriorities) == DDI_PROP_SUCCESS) {
427			ispec->intrspec_pri = intpriorities[0];
428			ddi_prop_free(intpriorities);
429		}
430	}
431	*pri = ispec->intrspec_pri;
432	return (DDI_SUCCESS);
433}
434
435
436/*
437 * xpvd_intr_ops: bus_intr_op() function for interrupt support
438 */
439/* ARGSUSED */
440static int
441xpvd_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
442    ddi_intr_handle_impl_t *hdlp, void *result)
443{
444	int priority = 0;
445	struct intrspec *ispec;
446	struct xendev_ppd *pdp;
447
448	DDI_INTR_NEXDBG((CE_CONT,
449	    "xpvd_intr_ops: pdip 0x%p, rdip 0x%p, op %x handle 0x%p\n",
450	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
451
452	/* Process the request */
453	switch (intr_op) {
454	case DDI_INTROP_SUPPORTED_TYPES:
455		/* Fixed supported by default */
456		*(int *)result = DDI_INTR_TYPE_FIXED;
457		break;
458
459	case DDI_INTROP_NINTRS:
460		*(int *)result = 1;
461		break;
462
463	case DDI_INTROP_ALLOC:
464		/*
465		 * FIXED interrupts: just return available interrupts
466		 */
467		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) {
468			/*
469			 * event channels are edge-triggered, maskable,
470			 * and support int pending.
471			 */
472			hdlp->ih_cap |= XENDEV_INTR_CAPABILITIES;
473			*(int *)result = 1;	/* DDI_INTR_TYPE_FIXED */
474		} else {
475			return (DDI_FAILURE);
476		}
477		break;
478
479	case DDI_INTROP_FREE:
480		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
481		if (ispec == NULL)
482			return (DDI_FAILURE);
483		ispec->intrspec_pri = 0; /* mark as un-initialized */
484		break;
485
486	case DDI_INTROP_GETPRI:
487		if (xpvd_get_priority(rdip, hdlp->ih_inum, &priority) !=
488		    DDI_SUCCESS)
489			return (DDI_FAILURE);
490		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: priority = 0x%x\n",
491		    priority));
492		*(int *)result = priority;
493		break;
494
495	case DDI_INTROP_SETPRI:
496		/* Validate the interrupt priority passed */
497		if (*(int *)result > LOCK_LEVEL)
498			return (DDI_FAILURE);
499
500		/* Ensure that PSM is all initialized */
501		if (psm_intr_ops == NULL)
502			return (DDI_FAILURE);
503
504		/* Change the priority */
505		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) ==
506		    PSM_FAILURE)
507			return (DDI_FAILURE);
508
509		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
510		if (ispec == NULL)
511			return (DDI_FAILURE);
512		ispec->intrspec_pri = *(int *)result;
513		break;
514
515	case DDI_INTROP_ADDISR:
516		/* update ispec */
517		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
518		if (ispec == NULL)
519			return (DDI_FAILURE);
520		ispec->intrspec_func = hdlp->ih_cb_func;
521
522		break;
523
524	case DDI_INTROP_REMISR:
525		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
526		pdp = (struct xendev_ppd *)ddi_get_parent_data(rdip);
527
528		ASSERT(pdp != NULL);
529		ASSERT(pdp->xd_evtchn != INVALID_EVTCHN);
530
531		if (ispec) {
532			ispec->intrspec_vec = 0;
533			ispec->intrspec_func = (uint_t (*)()) 0;
534		}
535		pdp->xd_evtchn = INVALID_EVTCHN;
536		break;
537
538	case DDI_INTROP_GETCAP:
539		if (hdlp->ih_type ==  DDI_INTR_TYPE_FIXED) {
540			/*
541			 * event channels are edge-triggered, maskable,
542			 * and support int pending.
543			 */
544			*(int *)result = XENDEV_INTR_CAPABILITIES;
545		} else {
546			*(int *)result = 0;
547			return (DDI_FAILURE);
548		}
549		DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETCAP returned = %x\n",
550		    *(int *)result));
551		break;
552	case DDI_INTROP_SETCAP:
553		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: SETCAP cap=0x%x\n",
554		    *(int *)result));
555		if (psm_intr_ops == NULL)
556			return (DDI_FAILURE);
557
558		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result)) {
559			DDI_INTR_NEXDBG((CE_CONT, "GETCAP: psm_intr_ops"
560			    " returned failure\n"));
561			return (DDI_FAILURE);
562		}
563		break;
564
565	case DDI_INTROP_ENABLE:
566		if (psm_intr_ops == NULL)
567			return (DDI_FAILURE);
568
569		if (xpvd_enable_intr(rdip, hdlp, (int)hdlp->ih_inum) !=
570		    DDI_SUCCESS)
571			return (DDI_FAILURE);
572
573		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: ENABLE vec=0x%x\n",
574		    hdlp->ih_vector));
575		break;
576
577	case DDI_INTROP_DISABLE:
578		if (psm_intr_ops == NULL)
579			return (DDI_FAILURE);
580		xpvd_disable_intr(rdip, hdlp, hdlp->ih_inum);
581		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: DISABLE vec = %x\n",
582		    hdlp->ih_vector));
583		break;
584
585	case DDI_INTROP_BLOCKENABLE:
586	case DDI_INTROP_BLOCKDISABLE:
587		return (DDI_FAILURE);
588
589	case DDI_INTROP_SETMASK:
590	case DDI_INTROP_CLRMASK:
591#ifdef XPV_HVM_DRIVER
592		return (DDI_ENOTSUP);
593#else
594		/*
595		 * Handle this here
596		 */
597		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
598			return (DDI_FAILURE);
599		if (intr_op == DDI_INTROP_SETMASK) {
600			ec_disable_irq(hdlp->ih_vector);
601		} else {
602			ec_enable_irq(hdlp->ih_vector);
603		}
604		break;
605#endif
606	case DDI_INTROP_GETPENDING:
607#ifdef XPV_HVM_DRIVER
608		return (DDI_ENOTSUP);
609#else
610		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
611			return (DDI_FAILURE);
612		*(int *)result = ec_pending_irq(hdlp->ih_vector);
613		DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETPENDING returned = %x\n",
614		    *(int *)result));
615		break;
616#endif
617
618	case DDI_INTROP_NAVAIL:
619		*(int *)result = 1;
620		DDI_INTR_NEXDBG((CE_CONT, "xpvd: NAVAIL returned = %x\n",
621		    *(int *)result));
622		break;
623
624	default:
625		return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result));
626	}
627
628	return (DDI_SUCCESS);
629}
630
631
632static int
633xpvd_enable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum)
634{
635	int		vector;
636	ihdl_plat_t	*ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private;
637
638	DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: hdlp %p inum %x\n",
639	    (void *)hdlp, inum));
640
641	ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum);
642	if (ihdl_plat_datap->ip_ispecp == NULL)
643		return (DDI_FAILURE);
644
645	/* translate the interrupt if needed */
646	(void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector);
647	DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: priority=%x vector=%x\n",
648	    hdlp->ih_pri, vector));
649
650	/* Add the interrupt handler */
651	if (!add_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func,
652	    DEVI(rdip)->devi_name, vector, hdlp->ih_cb_arg1,
653	    hdlp->ih_cb_arg2, NULL, rdip))
654		return (DDI_FAILURE);
655
656	/* Note this really is an irq. */
657	hdlp->ih_vector = (ushort_t)vector;
658
659	return (DDI_SUCCESS);
660}
661
662
663static void
664xpvd_disable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum)
665{
666	int		vector;
667	ihdl_plat_t	*ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private;
668
669	DDI_INTR_NEXDBG((CE_CONT, "xpvd_disable_intr: \n"));
670	ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum);
671	if (ihdl_plat_datap->ip_ispecp == NULL)
672		return;
673
674	/* translate the interrupt if needed */
675	(void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector);
676
677	/* Disable the interrupt handler */
678	rem_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func, vector);
679	ihdl_plat_datap->ip_ispecp = NULL;
680}
681
682/*ARGSUSED*/
683static int
684xpvd_ctlops(dev_info_t *dip, dev_info_t *rdip,
685	ddi_ctl_enum_t ctlop, void *arg, void *result)
686{
687	switch (ctlop) {
688	case DDI_CTLOPS_REPORTDEV:
689		if (rdip == (dev_info_t *)0)
690			return (DDI_FAILURE);
691		cmn_err(CE_CONT, "?%s@%s, %s%d\n", ddi_node_name(rdip),
692		    ddi_get_name_addr(rdip), ddi_driver_name(rdip),
693		    ddi_get_instance(rdip));
694		return (DDI_SUCCESS);
695
696	case DDI_CTLOPS_INITCHILD:
697		return (xpvd_initchild((dev_info_t *)arg));
698
699	case DDI_CTLOPS_UNINITCHILD:
700		return (xpvd_removechild((dev_info_t *)arg));
701
702	case DDI_CTLOPS_SIDDEV:
703		return (DDI_SUCCESS);
704
705	case DDI_CTLOPS_REGSIZE:
706	case DDI_CTLOPS_NREGS:
707		return (DDI_FAILURE);
708
709	case DDI_CTLOPS_POWER: {
710		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
711	}
712
713	default:
714		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
715	}
716
717	/* NOTREACHED */
718
719}
720
721/*
722 * Assign the address portion of the node name
723 */
724static int
725xpvd_name_child(dev_info_t *child, char *addr, int addrlen)
726{
727	int *domain, *vdev;
728	uint_t ndomain, nvdev;
729	char *prop_str;
730
731	/*
732	 * i_xpvd_parse_devname() knows the formats used by this
733	 * routine.  If this code changes, so must that.
734	 */
735
736	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
737	    "domain", &domain, &ndomain) != DDI_PROP_SUCCESS)
738		return (DDI_FAILURE);
739	ASSERT(ndomain == 1);
740
741	/*
742	 * Use "domain" and "vdev" properties (backend drivers).
743	 */
744	if (*domain != DOMID_SELF) {
745		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
746		    DDI_PROP_DONTPASS, "vdev", &vdev, &nvdev)
747		    != DDI_PROP_SUCCESS) {
748			ddi_prop_free(domain);
749			return (DDI_FAILURE);
750		}
751		ASSERT(nvdev == 1);
752
753		(void) snprintf(addr, addrlen, "%d,%d", domain[0], vdev[0]);
754		ddi_prop_free(vdev);
755		ddi_prop_free(domain);
756		return (DDI_SUCCESS);
757	}
758	ddi_prop_free(domain);
759
760	/*
761	 * Use "unit-address" property (frontend/softdev drivers).
762	 */
763	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
764	    "unit-address", &prop_str) != DDI_PROP_SUCCESS)
765		return (DDI_FAILURE);
766	(void) strlcpy(addr, prop_str, addrlen);
767	ddi_prop_free(prop_str);
768	return (DDI_SUCCESS);
769}
770
771static int
772xpvd_initchild(dev_info_t *child)
773{
774	char addr[80];
775
776	/*
777	 * Pseudo nodes indicate a prototype node with per-instance
778	 * properties to be merged into the real h/w device node.
779	 */
780	if (ndi_dev_is_persistent_node(child) == 0) {
781		ddi_set_parent_data(child, NULL);
782
783		/*
784		 * Try to merge the properties from this prototype
785		 * node into real h/w nodes.
786		 */
787		if (ndi_merge_node(child, xpvd_name_child) == DDI_SUCCESS) {
788			/*
789			 * Merged ok - return failure to remove the node.
790			 */
791			ddi_set_name_addr(child, NULL);
792			return (DDI_FAILURE);
793		}
794
795		/*
796		 * The child was not merged into a h/w node,
797		 * but there's not much we can do with it other
798		 * than return failure to cause the node to be removed.
799		 */
800		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
801		    ddi_get_name(child), ddi_get_name_addr(child),
802		    ddi_get_name(child));
803		ddi_set_name_addr(child, NULL);
804		return (DDI_NOT_WELL_FORMED);
805	}
806
807	if (xvdi_init_dev(child) != DDI_SUCCESS)
808		return (DDI_FAILURE);
809
810	if (xpvd_name_child(child, addr, sizeof (addr)) != DDI_SUCCESS) {
811		xvdi_uninit_dev(child);
812		return (DDI_FAILURE);
813	}
814	ddi_set_name_addr(child, addr);
815
816	return (DDI_SUCCESS);
817}
818
819static int
820xpvd_removechild(dev_info_t *dip)
821{
822	xvdi_uninit_dev(dip);
823
824	ddi_set_name_addr(dip, NULL);
825
826	/*
827	 * Strip the node to properly convert it back to prototype
828	 * form.
829	 */
830	ddi_remove_minor_node(dip, NULL);
831
832	return (DDI_SUCCESS);
833}
834
835static int
836xpvd_bus_unconfig(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
837    void *device_name)
838{
839	return (ndi_busop_bus_unconfig(parent, flag, op, device_name));
840}
841
842/*
843 * Given the name of a child of xpvd, determine the device class,
844 * domain and vdevnum to which it refers.
845 */
846static boolean_t
847i_xpvd_parse_devname(char *name, xendev_devclass_t *devclassp,
848    domid_t *domp, int *vdevp)
849{
850	int len = strlen(name) + 1;
851	char *device_name = i_ddi_strdup(name, KM_SLEEP);
852	char *cname = NULL, *caddr = NULL;
853	boolean_t ret;
854
855	i_ddi_parse_name(device_name, &cname, &caddr, NULL);
856
857	if ((cname == NULL) || (strlen(cname) == 0) ||
858	    (caddr == NULL) || (strlen(caddr) == 0)) {
859		ret = B_FALSE;
860		goto done;
861	}
862
863	*devclassp = xendev_nodename_to_devclass(cname);
864	if (*devclassp < 0) {
865		ret = B_FALSE;
866		goto done;
867	}
868
869	/*
870	 * Parsing the address component requires knowledge of how
871	 * xpvd_name_child() works.  If that code changes, so must
872	 * this.
873	 */
874
875	/* Backend format is "<domain>,<vdev>". */
876	if (sscanf(caddr, "%hu,%d", domp, vdevp) == 2) {
877		ret = B_TRUE;
878		goto done;
879	}
880
881	/* Frontend format is "<vdev>". */
882	*domp = DOMID_SELF;
883	if (sscanf(caddr, "%d", vdevp) == 1)
884		ret = B_TRUE;
885done:
886	kmem_free(device_name, len);
887	return (ret);
888}
889
890/*
891 * xpvd_bus_config()
892 *
893 * BUS_CONFIG_ONE:
894 *	Enumerate the exact instance of a driver.
895 *
896 * BUS_CONFIG_ALL:
897 *	Enumerate all the instances of all the possible children (seen before
898 *	and never seen before).
899 *
900 * BUS_CONFIG_DRIVER:
901 *	Enumerate all the instances of a particular driver.
902 */
903static int
904xpvd_bus_config(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
905	void *arg, dev_info_t **childp)
906{
907	int circ;
908	char *cname = NULL;
909
910	ndi_devi_enter(parent, &circ);
911
912	switch (op) {
913	case BUS_CONFIG_ONE: {
914		xendev_devclass_t devclass;
915		domid_t dom;
916		int vdev;
917
918		if (!i_xpvd_parse_devname(arg, &devclass, &dom, &vdev)) {
919			ndi_devi_exit(parent, circ);
920			return (NDI_FAILURE);
921		}
922
923		*childp = xvdi_find_dev(parent, devclass, dom, vdev);
924		if (*childp == NULL)
925			*childp = xvdi_create_dev(parent, devclass, dom, vdev);
926
927		ndi_devi_exit(parent, circ);
928
929		if (*childp == NULL)
930			return (NDI_FAILURE);
931		else
932			return (ndi_busop_bus_config(parent, flag,
933			    op, arg, childp, 0));
934	}
935
936	case BUS_CONFIG_DRIVER: {
937		xendev_devclass_t devclass = XEN_INVAL;
938
939		cname = ddi_major_to_name((major_t)(uintptr_t)arg);
940		if (cname != NULL)
941			devclass = xendev_nodename_to_devclass(cname);
942
943		if (devclass == XEN_INVAL) {
944			ndi_devi_exit(parent, circ);
945			return (NDI_FAILURE);
946		} else {
947			xendev_enum_class(parent, devclass);
948			ndi_devi_exit(parent, circ);
949			return (ndi_busop_bus_config(parent, flag, op,
950			    arg, childp, 0));
951		}
952		/* NOTREACHED */
953	}
954
955	case BUS_CONFIG_ALL:
956		xendev_enum_all(parent, B_FALSE);
957		ndi_devi_exit(parent, circ);
958
959		return (ndi_busop_bus_config(parent, flag, op,
960		    arg, childp, 0));
961
962	default:
963		ndi_devi_exit(parent, circ);
964		return (NDI_FAILURE);
965	}
966}
967
968/*ARGSUSED*/
969static int
970xpvd_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
971    char *eventname, ddi_eventcookie_t *cookie)
972{
973	return (ndi_event_retrieve_cookie(xpvd_ndi_event_handle,
974	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
975}
976
977/*ARGSUSED*/
978static int
979xpvd_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
980    ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
981    ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
982    void *arg, ddi_callback_id_t *cb_id)
983{
984	return (ndi_event_add_callback(xpvd_ndi_event_handle,
985	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
986}
987
988/*ARGSUSED*/
989static int
990xpvd_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
991{
992	return (ndi_event_remove_callback(xpvd_ndi_event_handle,
993	    cb_id));
994}
995
996/*ARGSUSED*/
997static int
998xpvd_post_event(dev_info_t *dip, dev_info_t *rdip,
999    ddi_eventcookie_t cookie, void *bus_impldata)
1000{
1001	return (ndi_event_run_callbacks(xpvd_ndi_event_handle, rdip,
1002	    cookie, bus_impldata));
1003}
1004