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