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