xref: /titanic_44/usr/src/uts/common/xen/io/xpvd.c (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
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 
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*/
296 static int
297 xpvd_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  */
308 static int
309 xpvd_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 
341 toss_off:
342 	return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
343 	    mod_flags | DDI_PROP_NOTPROM, name, valuep, lengthp));
344 
345 got_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*/
381 struct intrspec *
382 xpvd_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*/
398 static int
399 xpvd_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 */
440 static int
441 xpvd_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 
632 static int
633 xpvd_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 
663 static void
664 xpvd_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*/
683 static int
684 xpvd_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  */
724 static int
725 xpvd_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 
771 static int
772 xpvd_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 
819 static int
820 xpvd_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 
835 static int
836 xpvd_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  */
846 static boolean_t
847 i_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, "%d,%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;
885 done:
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  */
903 static int
904 xpvd_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*/
969 static int
970 xpvd_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*/
978 static int
979 xpvd_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*/
989 static int
990 xpvd_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*/
997 static int
998 xpvd_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