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