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