xref: /illumos-gate/usr/src/uts/common/xen/io/xpvd.c (revision 584b574a)
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
_init(void)215 _init(void)
216 {
217 	return (mod_install(&modlinkage));
218 }
219 
220 int
_fini(void)221 _fini(void)
222 {
223 	return (mod_remove(&modlinkage));
224 }
225 
226 int
_info(struct modinfo * modinfop)227 _info(struct modinfo *modinfop)
228 {
229 	return (mod_info(&modlinkage, modinfop));
230 }
231 
232 /* ARGSUSED */
233 static int
xpvd_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)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
xpvd_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)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
xpvd_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)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
xpvd_prop_op(dev_t dev,dev_info_t * dip,dev_info_t * ch_dip,ddi_prop_op_t prop_op,int mod_flags,char * name,caddr_t valuep,int * lengthp)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) {
381 		if (prop_op != PROP_LEN) {
382 			bcopy(prop_str, buff, prop_len);
383 			*lengthp = prop_len;
384 		}
385 	}
386 	kmem_free(prop_str, len);
387 	return (rv);
388 }
389 
390 
391 /*
392  * return address of the device's interrupt spec structure.
393  */
394 /*ARGSUSED*/
395 struct intrspec *
xpvd_get_ispec(dev_info_t * rdip,uint_t inumber)396 xpvd_get_ispec(dev_info_t *rdip, uint_t inumber)
397 {
398 	struct xendev_ppd *pdp;
399 
400 	ASSERT(inumber == 0);
401 
402 	if ((pdp = ddi_get_parent_data(rdip)) == NULL)
403 		return (NULL);
404 
405 	return (&pdp->xd_ispec);
406 }
407 
408 /*
409  * return (and determine) the interrupt priority of the device.
410  */
411 /*ARGSUSED*/
412 static int
xpvd_get_priority(dev_info_t * dip,int inum,int * pri)413 xpvd_get_priority(dev_info_t *dip, int inum, int *pri)
414 {
415 	struct xendev_ppd *pdp;
416 	struct intrspec *ispec;
417 	int	*intpriorities;
418 	uint_t	num_intpriorities;
419 
420 	DDI_INTR_NEXDBG((CE_CONT, "xpvd_get_priority: dip = 0x%p\n",
421 	    (void *)dip));
422 
423 	ASSERT(inum == 0);
424 
425 	if ((pdp = ddi_get_parent_data(dip)) == NULL)
426 		return (DDI_FAILURE);
427 
428 	ispec = &pdp->xd_ispec;
429 
430 	/*
431 	 * Set the default priority based on the device class.  The
432 	 * "interrupt-priorities" property can be used to override
433 	 * the default.
434 	 */
435 	if (ispec->intrspec_pri == 0) {
436 		ispec->intrspec_pri = xendev_devclass_ipl(pdp->xd_devclass);
437 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
438 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
439 		    "interrupt-priorities", &intpriorities,
440 		    &num_intpriorities) == DDI_PROP_SUCCESS) {
441 			ispec->intrspec_pri = intpriorities[0];
442 			ddi_prop_free(intpriorities);
443 		}
444 	}
445 	*pri = ispec->intrspec_pri;
446 	return (DDI_SUCCESS);
447 }
448 
449 
450 /*
451  * xpvd_intr_ops: bus_intr_op() function for interrupt support
452  */
453 /* ARGSUSED */
454 static int
xpvd_intr_ops(dev_info_t * pdip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)455 xpvd_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
456     ddi_intr_handle_impl_t *hdlp, void *result)
457 {
458 	int priority = 0;
459 	struct intrspec *ispec;
460 	struct xendev_ppd *pdp;
461 
462 	DDI_INTR_NEXDBG((CE_CONT,
463 	    "xpvd_intr_ops: pdip 0x%p, rdip 0x%p, op %x handle 0x%p\n",
464 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
465 
466 	/* Process the request */
467 	switch (intr_op) {
468 	case DDI_INTROP_SUPPORTED_TYPES:
469 		/* Fixed supported by default */
470 		*(int *)result = DDI_INTR_TYPE_FIXED;
471 		break;
472 
473 	case DDI_INTROP_NINTRS:
474 		*(int *)result = 1;
475 		break;
476 
477 	case DDI_INTROP_ALLOC:
478 		/*
479 		 * FIXED interrupts: just return available interrupts
480 		 */
481 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) {
482 			/*
483 			 * event channels are edge-triggered, maskable,
484 			 * and support int pending.
485 			 */
486 			hdlp->ih_cap |= XENDEV_INTR_CAPABILITIES;
487 			*(int *)result = 1;	/* DDI_INTR_TYPE_FIXED */
488 		} else {
489 			return (DDI_FAILURE);
490 		}
491 		break;
492 
493 	case DDI_INTROP_FREE:
494 		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
495 		if (ispec == NULL)
496 			return (DDI_FAILURE);
497 		ispec->intrspec_pri = 0; /* mark as un-initialized */
498 		break;
499 
500 	case DDI_INTROP_GETPRI:
501 		if (xpvd_get_priority(rdip, hdlp->ih_inum, &priority) !=
502 		    DDI_SUCCESS)
503 			return (DDI_FAILURE);
504 		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: priority = 0x%x\n",
505 		    priority));
506 		*(int *)result = priority;
507 		break;
508 
509 	case DDI_INTROP_SETPRI:
510 		/* Validate the interrupt priority passed */
511 		if (*(int *)result > LOCK_LEVEL)
512 			return (DDI_FAILURE);
513 
514 		/* Ensure that PSM is all initialized */
515 		if (psm_intr_ops == NULL)
516 			return (DDI_FAILURE);
517 
518 		/* Change the priority */
519 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_PRI, result) ==
520 		    PSM_FAILURE)
521 			return (DDI_FAILURE);
522 
523 		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
524 		if (ispec == NULL)
525 			return (DDI_FAILURE);
526 		ispec->intrspec_pri = *(int *)result;
527 		break;
528 
529 	case DDI_INTROP_ADDISR:
530 		/* update ispec */
531 		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
532 		if (ispec == NULL)
533 			return (DDI_FAILURE);
534 		ispec->intrspec_func = hdlp->ih_cb_func;
535 
536 		break;
537 
538 	case DDI_INTROP_REMISR:
539 		ispec = xpvd_get_ispec(rdip, (int)hdlp->ih_inum);
540 		pdp = (struct xendev_ppd *)ddi_get_parent_data(rdip);
541 
542 		ASSERT(pdp != NULL);
543 		ASSERT(pdp->xd_evtchn != INVALID_EVTCHN);
544 
545 		if (ispec) {
546 			ispec->intrspec_vec = 0;
547 			ispec->intrspec_func = (uint_t (*)()) 0;
548 		}
549 		pdp->xd_evtchn = INVALID_EVTCHN;
550 		break;
551 
552 	case DDI_INTROP_GETCAP:
553 		if (hdlp->ih_type ==  DDI_INTR_TYPE_FIXED) {
554 			/*
555 			 * event channels are edge-triggered, maskable,
556 			 * and support int pending.
557 			 */
558 			*(int *)result = XENDEV_INTR_CAPABILITIES;
559 		} else {
560 			*(int *)result = 0;
561 			return (DDI_FAILURE);
562 		}
563 		DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETCAP returned = %x\n",
564 		    *(int *)result));
565 		break;
566 	case DDI_INTROP_SETCAP:
567 		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: SETCAP cap=0x%x\n",
568 		    *(int *)result));
569 		if (psm_intr_ops == NULL)
570 			return (DDI_FAILURE);
571 
572 		if ((*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_SET_CAP, result)) {
573 			DDI_INTR_NEXDBG((CE_CONT, "GETCAP: psm_intr_ops"
574 			    " returned failure\n"));
575 			return (DDI_FAILURE);
576 		}
577 		break;
578 
579 	case DDI_INTROP_ENABLE:
580 		if (psm_intr_ops == NULL)
581 			return (DDI_FAILURE);
582 
583 		if (xpvd_enable_intr(rdip, hdlp, (int)hdlp->ih_inum) !=
584 		    DDI_SUCCESS)
585 			return (DDI_FAILURE);
586 
587 		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: ENABLE vec=0x%x\n",
588 		    hdlp->ih_vector));
589 		break;
590 
591 	case DDI_INTROP_DISABLE:
592 		if (psm_intr_ops == NULL)
593 			return (DDI_FAILURE);
594 		xpvd_disable_intr(rdip, hdlp, hdlp->ih_inum);
595 		DDI_INTR_NEXDBG((CE_CONT, "xpvd_intr_ops: DISABLE vec = %x\n",
596 		    hdlp->ih_vector));
597 		break;
598 
599 	case DDI_INTROP_BLOCKENABLE:
600 	case DDI_INTROP_BLOCKDISABLE:
601 		return (DDI_FAILURE);
602 
603 	case DDI_INTROP_SETMASK:
604 	case DDI_INTROP_CLRMASK:
605 #ifdef XPV_HVM_DRIVER
606 		return (DDI_ENOTSUP);
607 #else
608 		/*
609 		 * Handle this here
610 		 */
611 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
612 			return (DDI_FAILURE);
613 		if (intr_op == DDI_INTROP_SETMASK) {
614 			ec_disable_irq(hdlp->ih_vector);
615 		} else {
616 			ec_enable_irq(hdlp->ih_vector);
617 		}
618 		break;
619 #endif
620 	case DDI_INTROP_GETPENDING:
621 #ifdef XPV_HVM_DRIVER
622 		return (DDI_ENOTSUP);
623 #else
624 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
625 			return (DDI_FAILURE);
626 		*(int *)result = ec_pending_irq(hdlp->ih_vector);
627 		DDI_INTR_NEXDBG((CE_CONT, "xpvd: GETPENDING returned = %x\n",
628 		    *(int *)result));
629 		break;
630 #endif
631 
632 	case DDI_INTROP_NAVAIL:
633 		*(int *)result = 1;
634 		DDI_INTR_NEXDBG((CE_CONT, "xpvd: NAVAIL returned = %x\n",
635 		    *(int *)result));
636 		break;
637 
638 	default:
639 		return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result));
640 	}
641 
642 	return (DDI_SUCCESS);
643 }
644 
645 
646 static int
xpvd_enable_intr(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp,int inum)647 xpvd_enable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum)
648 {
649 	int		vector;
650 	ihdl_plat_t	*ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private;
651 
652 	DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: hdlp %p inum %x\n",
653 	    (void *)hdlp, inum));
654 
655 	ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum);
656 	if (ihdl_plat_datap->ip_ispecp == NULL)
657 		return (DDI_FAILURE);
658 
659 	/* translate the interrupt if needed */
660 	(void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector);
661 	DDI_INTR_NEXDBG((CE_CONT, "xpvd_enable_intr: priority=%x vector=%x\n",
662 	    hdlp->ih_pri, vector));
663 
664 	/* Add the interrupt handler */
665 	if (!add_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func,
666 	    DEVI(rdip)->devi_name, vector, hdlp->ih_cb_arg1,
667 	    hdlp->ih_cb_arg2, NULL, rdip))
668 		return (DDI_FAILURE);
669 
670 	/* Note this really is an irq. */
671 	hdlp->ih_vector = (ushort_t)vector;
672 
673 	return (DDI_SUCCESS);
674 }
675 
676 
677 static void
xpvd_disable_intr(dev_info_t * rdip,ddi_intr_handle_impl_t * hdlp,int inum)678 xpvd_disable_intr(dev_info_t *rdip, ddi_intr_handle_impl_t *hdlp, int inum)
679 {
680 	int		vector;
681 	ihdl_plat_t	*ihdl_plat_datap = (ihdl_plat_t *)hdlp->ih_private;
682 
683 	DDI_INTR_NEXDBG((CE_CONT, "xpvd_disable_intr: \n"));
684 	ihdl_plat_datap->ip_ispecp = xpvd_get_ispec(rdip, inum);
685 	if (ihdl_plat_datap->ip_ispecp == NULL)
686 		return;
687 
688 	/* translate the interrupt if needed */
689 	(void) (*psm_intr_ops)(rdip, hdlp, PSM_INTR_OP_XLATE_VECTOR, &vector);
690 
691 	/* Disable the interrupt handler */
692 	rem_avintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func, vector);
693 	ihdl_plat_datap->ip_ispecp = NULL;
694 }
695 
696 /*ARGSUSED*/
697 static int
xpvd_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)698 xpvd_ctlops(dev_info_t *dip, dev_info_t *rdip,
699     ddi_ctl_enum_t ctlop, void *arg, void *result)
700 {
701 	switch (ctlop) {
702 	case DDI_CTLOPS_REPORTDEV:
703 		if (rdip == (dev_info_t *)0)
704 			return (DDI_FAILURE);
705 		cmn_err(CE_CONT, "?%s@%s, %s%d\n", ddi_node_name(rdip),
706 		    ddi_get_name_addr(rdip), ddi_driver_name(rdip),
707 		    ddi_get_instance(rdip));
708 		return (DDI_SUCCESS);
709 
710 	case DDI_CTLOPS_INITCHILD:
711 		return (xpvd_initchild((dev_info_t *)arg));
712 
713 	case DDI_CTLOPS_UNINITCHILD:
714 		return (xpvd_removechild((dev_info_t *)arg));
715 
716 	case DDI_CTLOPS_SIDDEV:
717 		return (DDI_SUCCESS);
718 
719 	case DDI_CTLOPS_REGSIZE:
720 	case DDI_CTLOPS_NREGS:
721 		return (DDI_FAILURE);
722 
723 	case DDI_CTLOPS_POWER: {
724 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
725 	}
726 
727 	default:
728 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
729 	}
730 
731 	/* NOTREACHED */
732 
733 }
734 
735 /*
736  * Assign the address portion of the node name
737  */
738 static int
xpvd_name_child(dev_info_t * child,char * addr,int addrlen)739 xpvd_name_child(dev_info_t *child, char *addr, int addrlen)
740 {
741 	int *domain, *vdev;
742 	uint_t ndomain, nvdev;
743 	char *prop_str;
744 
745 	/*
746 	 * i_xpvd_parse_devname() knows the formats used by this
747 	 * routine.  If this code changes, so must that.
748 	 */
749 
750 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
751 	    "domain", &domain, &ndomain) != DDI_PROP_SUCCESS)
752 		return (DDI_FAILURE);
753 	ASSERT(ndomain == 1);
754 
755 	/*
756 	 * Use "domain" and "vdev" properties (backend drivers).
757 	 */
758 	if (*domain != DOMID_SELF) {
759 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
760 		    DDI_PROP_DONTPASS, "vdev", &vdev, &nvdev)
761 		    != DDI_PROP_SUCCESS) {
762 			ddi_prop_free(domain);
763 			return (DDI_FAILURE);
764 		}
765 		ASSERT(nvdev == 1);
766 
767 		(void) snprintf(addr, addrlen, "%d,%d", domain[0], vdev[0]);
768 		ddi_prop_free(vdev);
769 		ddi_prop_free(domain);
770 		return (DDI_SUCCESS);
771 	}
772 	ddi_prop_free(domain);
773 
774 	/*
775 	 * Use "vdev" and "unit-address" properties (frontend/softdev drivers).
776 	 * At boot time, only the vdev property is available on xdf disks.
777 	 */
778 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
779 	    "unit-address", &prop_str) != DDI_PROP_SUCCESS) {
780 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
781 		    DDI_PROP_DONTPASS, "vdev", &vdev,
782 		    &nvdev) != DDI_PROP_SUCCESS)
783 			return (DDI_FAILURE);
784 		ASSERT(nvdev == 1);
785 		(void) snprintf(addr, addrlen, "%d", vdev[0]);
786 		ddi_prop_free(vdev);
787 		return (DDI_SUCCESS);
788 	}
789 	(void) strlcpy(addr, prop_str, addrlen);
790 	ddi_prop_free(prop_str);
791 	return (DDI_SUCCESS);
792 }
793 
794 static int
xpvd_initchild(dev_info_t * child)795 xpvd_initchild(dev_info_t *child)
796 {
797 	char addr[80];
798 
799 	/*
800 	 * Pseudo nodes indicate a prototype node with per-instance
801 	 * properties to be merged into the real h/w device node.
802 	 */
803 	if (ndi_dev_is_persistent_node(child) == 0) {
804 		ddi_set_parent_data(child, NULL);
805 		if (xpvd_name_child(child, addr, sizeof (addr)) != DDI_SUCCESS)
806 			return (DDI_FAILURE);
807 		ddi_set_name_addr(child, addr);
808 
809 		/*
810 		 * Try to merge the properties from this prototype
811 		 * node into real h/w nodes.
812 		 */
813 		if (ndi_merge_node(child, xpvd_name_child) == DDI_SUCCESS) {
814 			/*
815 			 * Merged ok - return failure to remove the node.
816 			 */
817 			ddi_set_name_addr(child, NULL);
818 			return (DDI_FAILURE);
819 		}
820 
821 		/*
822 		 * The child was not merged into a h/w node,
823 		 * but there's not much we can do with it other
824 		 * than return failure to cause the node to be removed.
825 		 */
826 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
827 		    ddi_get_name(child), ddi_get_name_addr(child),
828 		    ddi_get_name(child));
829 		ddi_set_name_addr(child, NULL);
830 		return (DDI_NOT_WELL_FORMED);
831 	}
832 
833 	if (xvdi_init_dev(child) != DDI_SUCCESS)
834 		return (DDI_FAILURE);
835 
836 	if (xpvd_name_child(child, addr, sizeof (addr)) != DDI_SUCCESS) {
837 		xvdi_uninit_dev(child);
838 		return (DDI_FAILURE);
839 	}
840 	ddi_set_name_addr(child, addr);
841 
842 	return (DDI_SUCCESS);
843 }
844 
845 static int
xpvd_removechild(dev_info_t * dip)846 xpvd_removechild(dev_info_t *dip)
847 {
848 	xvdi_uninit_dev(dip);
849 
850 	ddi_set_name_addr(dip, NULL);
851 
852 	/*
853 	 * Strip the node to properly convert it back to prototype
854 	 * form.
855 	 */
856 	ddi_remove_minor_node(dip, NULL);
857 
858 	return (DDI_SUCCESS);
859 }
860 
861 static int
xpvd_bus_unconfig(dev_info_t * parent,uint_t flag,ddi_bus_config_op_t op,void * device_name)862 xpvd_bus_unconfig(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
863     void *device_name)
864 {
865 	return (ndi_busop_bus_unconfig(parent, flag, op, device_name));
866 }
867 
868 /*
869  * Given the name of a child of xpvd, determine the device class,
870  * domain and vdevnum to which it refers.
871  */
872 static boolean_t
i_xpvd_parse_devname(char * name,xendev_devclass_t * devclassp,domid_t * domp,int * vdevp)873 i_xpvd_parse_devname(char *name, xendev_devclass_t *devclassp,
874     domid_t *domp, int *vdevp)
875 {
876 	int len = strlen(name) + 1;
877 	char *device_name = i_ddi_strdup(name, KM_SLEEP);
878 	char *cname = NULL, *caddr = NULL;
879 	boolean_t ret = B_FALSE;
880 
881 	i_ddi_parse_name(device_name, &cname, &caddr, NULL);
882 
883 	if ((cname == NULL) || (strlen(cname) == 0) ||
884 	    (caddr == NULL) || (strlen(caddr) == 0)) {
885 		ret = B_FALSE;
886 		goto done;
887 	}
888 
889 	*devclassp = xendev_nodename_to_devclass(cname);
890 	if (*devclassp < 0) {
891 		ret = B_FALSE;
892 		goto done;
893 	}
894 
895 	/*
896 	 * Parsing the address component requires knowledge of how
897 	 * xpvd_name_child() works.  If that code changes, so must
898 	 * this.
899 	 */
900 
901 	/* Backend format is "<domain>,<vdev>". */
902 	if (sscanf(caddr, "%hu,%d", domp, vdevp) == 2) {
903 		ret = B_TRUE;
904 		goto done;
905 	}
906 
907 	/* Frontend format is "<vdev>". */
908 	*domp = DOMID_SELF;
909 	if (sscanf(caddr, "%d", vdevp) == 1)
910 		ret = B_TRUE;
911 done:
912 	kmem_free(device_name, len);
913 	return (ret);
914 }
915 
916 /*
917  * xpvd_bus_config()
918  *
919  * BUS_CONFIG_ONE:
920  *	Enumerate the exact instance of a driver.
921  *
922  * BUS_CONFIG_ALL:
923  *	Enumerate all the instances of all the possible children (seen before
924  *	and never seen before).
925  *
926  * BUS_CONFIG_DRIVER:
927  *	Enumerate all the instances of a particular driver.
928  */
929 static int
xpvd_bus_config(dev_info_t * parent,uint_t flag,ddi_bus_config_op_t op,void * arg,dev_info_t ** childp)930 xpvd_bus_config(dev_info_t *parent, uint_t flag, ddi_bus_config_op_t op,
931     void *arg, dev_info_t **childp)
932 {
933 	int circ;
934 	char *cname = NULL;
935 
936 	ndi_devi_enter(parent, &circ);
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, circ);
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, circ);
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, circ);
971 			return (NDI_FAILURE);
972 		} else {
973 			xendev_enum_class(parent, devclass);
974 			ndi_devi_exit(parent, circ);
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, circ);
984 
985 		return (ndi_busop_bus_config(parent, flag, op,
986 		    arg, childp, 0));
987 
988 	default:
989 		ndi_devi_exit(parent, circ);
990 		return (NDI_FAILURE);
991 	}
992 }
993 
994 /*ARGSUSED*/
995 static int
xpvd_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookie)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
xpvd_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void (* callback)(dev_info_t * dip,ddi_eventcookie_t cookie,void * arg,void * bus_impldata),void * arg,ddi_callback_id_t * cb_id)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
xpvd_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)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
xpvd_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * bus_impldata)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