xref: /illumos-gate/usr/src/uts/common/xen/io/xpvd.c (revision 5bb86dd8f405a48942aaaab3ca1f410ed7e6db4d)
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 2007 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 #ifdef XPV_HVM_DRIVER
187 static int hvm_vdev_num[26];
188 #endif
189 
190 /*
191  * Hypervisor interrupt capabilities
192  */
193 #define	XENDEV_INTR_CAPABILITIES \
194 	(DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_MASKABLE | DDI_INTR_FLAG_PENDING)
195 
196 /*
197  * Module linkage information for the kernel.
198  */
199 
200 static struct modldrv modldrv = {
201 	&mod_driverops, /* Type of module */
202 	"virtual device nexus driver %I%",
203 	&xpvd_ops,	/* driver ops */
204 };
205 
206 static struct modlinkage modlinkage = {
207 	MODREV_1,
208 	(void *)&modldrv,
209 	NULL
210 };
211 
212 int
213 _init(void)
214 {
215 	return (mod_install(&modlinkage));
216 }
217 
218 int
219 _fini(void)
220 {
221 	return (mod_remove(&modlinkage));
222 }
223 
224 int
225 _info(struct modinfo *modinfop)
226 {
227 	return (mod_info(&modlinkage, modinfop));
228 }
229 
230 /* ARGSUSED */
231 static int
232 xpvd_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
233 {
234 	switch (cmd) {
235 	default:
236 		return (DDI_FAILURE);
237 
238 	case DDI_INFO_DEVT2INSTANCE:
239 		*result = (void *)0;
240 		return (DDI_SUCCESS);
241 
242 	case DDI_INFO_DEVT2DEVINFO:
243 		*result = (void *)xpvd_dip;
244 		return (DDI_SUCCESS);
245 	}
246 }
247 
248 /*ARGSUSED*/
249 static int
250 xpvd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
251 {
252 	extern void xvdi_watch_devices(int);
253 
254 #ifdef XPV_HVM_DRIVER
255 	if (xen_info == NULL) {
256 		if (ddi_hold_installed_driver(ddi_name_to_major("xpv")) ==
257 		    NULL) {
258 			cmn_err(CE_WARN, "Couldn't initialize xpv framework");
259 			return (DDI_FAILURE);
260 		}
261 	}
262 #endif
263 
264 	if (ndi_event_alloc_hdl(devi, 0, &xpvd_ndi_event_handle,
265 	    NDI_SLEEP) != NDI_SUCCESS) {
266 		xpvd_dip = NULL;
267 		return (DDI_FAILURE);
268 	}
269 	if (ndi_event_bind_set(xpvd_ndi_event_handle, &xpvd_ndi_events,
270 	    NDI_SLEEP) != NDI_SUCCESS) {
271 		(void) ndi_event_free_hdl(xpvd_ndi_event_handle);
272 		xpvd_dip = NULL;
273 		return (DDI_FAILURE);
274 	}
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 *name, int namelen)
719 {
720 	int *domain, *vdev;
721 	uint_t ndomain, nvdev;
722 	char *unit_address;
723 	int devno;
724 #ifdef XPV_HVM_DRIVER
725 	char *xip;
726 	int xenstore_id;
727 #endif
728 
729 	/*
730 	 * i_xpvd_parse_devname() knows the formats used by this
731 	 * routine.  If this code changes, so must that.
732 	 */
733 
734 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
735 	    "domain", &domain, &ndomain) != DDI_PROP_SUCCESS)
736 		return (DDI_FAILURE);
737 	ASSERT(ndomain == 1);
738 
739 	/*
740 	 * Use "domain" and "vdev" properties (backend drivers).
741 	 */
742 	if (*domain != DOMID_SELF) {
743 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
744 		    DDI_PROP_DONTPASS, "vdev", &vdev, &nvdev)
745 		    != DDI_PROP_SUCCESS) {
746 			ddi_prop_free(domain);
747 			return (DDI_FAILURE);
748 		}
749 		ASSERT(nvdev == 1);
750 
751 		(void) snprintf(name, namelen, "%d,%d", domain[0], vdev[0]);
752 		ddi_prop_free(vdev);
753 		ddi_prop_free(domain);
754 		return (DDI_SUCCESS);
755 	}
756 	ddi_prop_free(domain);
757 
758 	/*
759 	 * Use "unit-address" property (frontend/softdev drivers).
760 	 *
761 	 * For PV domains, the disk name should be a simple number.  In an
762 	 * HVM domain, it will be a string of the form hdX.  In the latter
763 	 * case we convert hda to 0, hdb to 1, and so on.
764 	 */
765 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
766 	    DDI_PROP_DONTPASS, "unit-address", &unit_address)
767 	    == DDI_PROP_SUCCESS) {
768 		devno = -1;
769 		if (unit_address[0] >= '0' && unit_address[0] <= '9')
770 			(void) sscanf(unit_address, "%d", &devno);
771 #ifdef XPV_HVM_DRIVER
772 		/*
773 		 * XXX: we should really check the device class here.  We
774 		 * always want to set hvm_vdev_num[] - even if we somehow
775 		 * end up with a non-hdX device name.
776 		 */
777 		else if (strlen(unit_address) == 3 &&
778 		    unit_address[0] == 'h' && unit_address[1] == 'd') {
779 			devno = unit_address[2] - 'a';
780 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
781 			    DDI_PROP_DONTPASS, "xenstore-id", &xip)
782 			    == DDI_PROP_SUCCESS) {
783 				(void) sscanf(xip, "%d", &xenstore_id);
784 				ddi_prop_free(xip);
785 				hvm_vdev_num[devno] = xenstore_id;
786 			} else {
787 				devno = -1;
788 			}
789 		}
790 #endif
791 
792 		if (devno < 0) {
793 			cmn_err(CE_WARN, "Unrecognized device: %s",
794 			    unit_address);
795 			ddi_prop_free(unit_address);
796 			return (DDI_FAILURE);
797 		}
798 		(void) snprintf(name, namelen, "%x", devno);
799 		ddi_prop_free(unit_address);
800 		return (DDI_SUCCESS);
801 	}
802 
803 	return (DDI_FAILURE);
804 }
805 
806 static int
807 xpvd_initchild(dev_info_t *child)
808 {
809 	char name[80];
810 
811 	/*
812 	 * Pseudo nodes indicate a prototype node with per-instance
813 	 * properties to be merged into the real h/w device node.
814 	 */
815 	if (ndi_dev_is_persistent_node(child) == 0) {
816 		ddi_set_parent_data(child, NULL);
817 
818 		/*
819 		 * Try to merge the properties from this prototype
820 		 * node into real h/w nodes.
821 		 */
822 		if (ndi_merge_node(child, xpvd_name_child) == DDI_SUCCESS) {
823 			/*
824 			 * Merged ok - return failure to remove the node.
825 			 */
826 			ddi_set_name_addr(child, NULL);
827 			return (DDI_FAILURE);
828 		}
829 
830 		/*
831 		 * The child was not merged into a h/w node,
832 		 * but there's not much we can do with it other
833 		 * than return failure to cause the node to be removed.
834 		 */
835 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
836 		    ddi_get_name(child), ddi_get_name_addr(child),
837 		    ddi_get_name(child));
838 		ddi_set_name_addr(child, NULL);
839 		return (DDI_NOT_WELL_FORMED);
840 	}
841 
842 	if (xvdi_init_dev(child) != DDI_SUCCESS)
843 		return (DDI_FAILURE);
844 
845 	if (xpvd_name_child(child, name, 80) != DDI_SUCCESS) {
846 		xvdi_uninit_dev(child);
847 		return (DDI_FAILURE);
848 	}
849 	ddi_set_name_addr(child, name);
850 
851 	return (DDI_SUCCESS);
852 }
853 
854 static int
855 xpvd_removechild(dev_info_t *dip)
856 {
857 	xvdi_uninit_dev(dip);
858 
859 	ddi_set_name_addr(dip, NULL);
860 
861 	/*
862 	 * Strip the node to properly convert it back to prototype
863 	 * form.
864 	 */
865 	ddi_remove_minor_node(dip, NULL);
866 
867 	return (DDI_SUCCESS);
868 }
869 
870 static int
871 xpvd_bus_unconfig(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
872     void *device_name)
873 {
874 	return (ndi_busop_bus_unconfig(parent, flag, op, device_name));
875 }
876 
877 /*
878  * Given the name of a child of xpvd, determine the device class,
879  * domain and vdevnum to which it refers.
880  */
881 static boolean_t
882 i_xpvd_parse_devname(char *name, xendev_devclass_t *devclassp,
883     domid_t *domp, int *vdevp)
884 {
885 	int len = strlen(name) + 1;
886 	char *device_name = i_ddi_strdup(name, KM_SLEEP);
887 	char *cname = NULL, *caddr = NULL;
888 	boolean_t ret;
889 
890 	i_ddi_parse_name(device_name, &cname, &caddr, NULL);
891 
892 	if ((cname == NULL) || (strlen(cname) == 0) ||
893 	    (caddr == NULL) || (strlen(caddr) == 0)) {
894 		ret = B_FALSE;
895 		goto done;
896 	}
897 
898 	*devclassp = xendev_nodename_to_devclass(cname);
899 	if (*devclassp < 0) {
900 		ret = B_FALSE;
901 		goto done;
902 	}
903 
904 	/*
905 	 * Parsing the address component requires knowledge of how
906 	 * xpvd_name_child() works.  If that code changes, so must
907 	 * this.
908 	 */
909 
910 	/* Backend format is "<domain>,<vdev>". */
911 	if (sscanf(caddr, "%d,%d", domp, vdevp) == 2) {
912 		ret = B_TRUE;
913 		goto done;
914 	}
915 
916 	/* Frontend format is "<vdev>". */
917 	*domp = DOMID_SELF;
918 	if (sscanf(caddr, "%x", vdevp) == 1) {
919 #ifdef XPV_HVM_DRIVER
920 		if (*devclassp == XEN_VBLK) {
921 			if (*vdevp < 0 || *vdevp > 26) {
922 				*vdevp = -1;
923 				goto done;
924 			}
925 			*vdevp = hvm_vdev_num[*vdevp];
926 		}
927 #endif
928 		ret = B_TRUE;
929 		goto done;
930 	}
931 
932 
933 done:
934 	kmem_free(device_name, len);
935 	return (ret);
936 }
937 
938 /*
939  * xpvd_bus_config()
940  *
941  * BUS_CONFIG_ONE:
942  *	Enumerate the exact instance of a driver.
943  *
944  * BUS_CONFIG_ALL:
945  *	Enumerate all the instances of all the possible children (seen before
946  *	and never seen before).
947  *
948  * BUS_CONFIG_DRIVER:
949  *	Enumerate all the instances of a particular driver.
950  */
951 static int
952 xpvd_bus_config(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
953 	void *arg, dev_info_t **childp)
954 {
955 	int circ;
956 	char *cname = NULL;
957 
958 	ndi_devi_enter(parent, &circ);
959 
960 	switch (op) {
961 	case BUS_CONFIG_ONE: {
962 		xendev_devclass_t devclass;
963 		domid_t dom;
964 		int vdev;
965 
966 		if (!i_xpvd_parse_devname(arg, &devclass, &dom, &vdev)) {
967 			ndi_devi_exit(parent, circ);
968 			return (NDI_FAILURE);
969 		}
970 
971 		*childp = xvdi_find_dev(parent, devclass, dom, vdev);
972 		if (*childp == NULL)
973 			*childp = xvdi_create_dev(parent, devclass, dom, vdev);
974 
975 		ndi_devi_exit(parent, circ);
976 
977 		if (*childp == NULL)
978 			return (NDI_FAILURE);
979 		else
980 			return (ndi_busop_bus_config(parent, flag,
981 			    op, arg, childp, 0));
982 	}
983 
984 	case BUS_CONFIG_DRIVER: {
985 		xendev_devclass_t devclass = XEN_INVAL;
986 
987 		cname = ddi_major_to_name((major_t)(uintptr_t)arg);
988 		if (cname != NULL)
989 			devclass = xendev_nodename_to_devclass(cname);
990 
991 		if (devclass == XEN_INVAL) {
992 			ndi_devi_exit(parent, circ);
993 			return (NDI_FAILURE);
994 		} else {
995 			xendev_enum_class(parent, devclass);
996 			ndi_devi_exit(parent, circ);
997 			return (ndi_busop_bus_config(parent, flag, op,
998 			    arg, childp, 0));
999 		}
1000 		/* NOTREACHED */
1001 	}
1002 
1003 	case BUS_CONFIG_ALL:
1004 		xendev_enum_all(parent, B_FALSE);
1005 		ndi_devi_exit(parent, circ);
1006 
1007 		return (ndi_busop_bus_config(parent, flag, op,
1008 		    arg, childp, 0));
1009 
1010 	default:
1011 		ndi_devi_exit(parent, circ);
1012 		return (NDI_FAILURE);
1013 	}
1014 }
1015 
1016 /*ARGSUSED*/
1017 static int
1018 xpvd_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
1019     char *eventname, ddi_eventcookie_t *cookie)
1020 {
1021 	return (ndi_event_retrieve_cookie(xpvd_ndi_event_handle,
1022 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
1023 }
1024 
1025 /*ARGSUSED*/
1026 static int
1027 xpvd_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
1028     ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
1029     ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
1030     void *arg, ddi_callback_id_t *cb_id)
1031 {
1032 	return (ndi_event_add_callback(xpvd_ndi_event_handle,
1033 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
1034 }
1035 
1036 /*ARGSUSED*/
1037 static int
1038 xpvd_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
1039 {
1040 	return (ndi_event_remove_callback(xpvd_ndi_event_handle,
1041 	    cb_id));
1042 }
1043 
1044 /*ARGSUSED*/
1045 static int
1046 xpvd_post_event(dev_info_t *dip, dev_info_t *rdip,
1047     ddi_eventcookie_t cookie, void *bus_impldata)
1048 {
1049 	return (ndi_event_run_callbacks(xpvd_ndi_event_handle, rdip,
1050 	    cookie, bus_impldata));
1051 }
1052