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 /*
291 * Report our version to dom0.
292 */
293 if (xenbus_printf(XBT_NULL, "guest/xpvd", "version", "%d",
294 HVMPV_XPVD_VERS))
295 cmn_err(CE_WARN, "xpvd: couldn't write version\n");
296 #endif /* XPV_HVM_DRIVER */
297
298 /* watch both frontend and backend for new devices */
299 if (DOMAIN_IS_INITDOMAIN(xen_info))
300 (void) xs_register_xenbus_callback(xvdi_watch_devices);
301 else
302 xvdi_watch_devices(XENSTORE_UP);
303
304 xpvd_dip = devi;
305 ddi_report_dev(devi);
306
307 return (DDI_SUCCESS);
308 }
309
310 /*ARGSUSED*/
311 static int
312 xpvd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
313 {
314 return (DDI_FAILURE);
315 }
316
317 /*
318 * xpvd_prop_op()
319 *
320 * Query xenstore for the value of properties if DDI_PROP_NOTPROM
321 * is not set. Xenstore property values are represented as ascii strings.
322 */
323 static int
324 xpvd_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
325 ddi_prop_op_t prop_op, int mod_flags, char *name, caddr_t valuep,
326 int *lengthp)
327 {
328 caddr_t buff;
329 struct xendev_ppd *pdp;
330 void *prop_str;
331 size_t prop_len;
332 unsigned int len;
333 int rv;
334
335 pdp = (struct xendev_ppd *)ddi_get_parent_data(ch_dip);
336
337 if ((pdp == NULL) || !(mod_flags & (DDI_PROP_CANSLEEP)) ||
338 (mod_flags & DDI_PROP_NOTPROM) || (pdp->xd_xsdev.nodename == NULL))
339 goto toss_off;
340 /*
341 * First try reading the property off the the frontend. if that
342 * fails, try and read it from the backend node. If that
343 * also fails, pass the request on the DDI framework
344 */
345 prop_str = NULL;
346 if ((xenbus_read(XBT_NULL, pdp->xd_xsdev.nodename, name, &prop_str,
347 &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0))
348 goto got_xs_prop;
349
350 prop_str = NULL;
351 if ((pdp->xd_xsdev.otherend != NULL) &&
352 (xenbus_read(XBT_NULL, pdp->xd_xsdev.otherend, name, &prop_str,
353 &len) == 0) && (prop_str != NULL) && (strlen(prop_str) != 0))
354 goto got_xs_prop;
355
356 toss_off:
357 return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
358 mod_flags | DDI_PROP_NOTPROM, name, valuep, lengthp));
359
360 got_xs_prop:
361 prop_len = strlen(prop_str) + 1;
362 rv = DDI_PROP_SUCCESS;
363
364 switch (prop_op) {
365 case PROP_LEN:
366 *lengthp = prop_len;
367 break;
368
369 case PROP_LEN_AND_VAL_ALLOC:
370 buff = kmem_alloc((size_t)prop_len, KM_SLEEP);
371 *(caddr_t *)valuep = (caddr_t)buff;
372 break;
373 case PROP_LEN_AND_VAL_BUF:
374 buff = (caddr_t)valuep;
375 if (*lengthp < prop_len)
376 rv = DDI_PROP_BUF_TOO_SMALL;
377 break;
378 default:
379 rv = DDI_PROP_INVAL_ARG;
380 break;
381 }
382
383 if ((rv == DDI_PROP_SUCCESS) && (prop_len > 0)) {
384 bcopy(prop_str, buff, prop_len);
385 *lengthp = prop_len;
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;
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 int circ;
935 char *cname = NULL;
936
937 ndi_devi_enter(parent, &circ);
938
939 switch (op) {
940 case BUS_CONFIG_ONE: {
941 xendev_devclass_t devclass;
942 domid_t dom;
943 int vdev;
944
945 if (!i_xpvd_parse_devname(arg, &devclass, &dom, &vdev)) {
946 ndi_devi_exit(parent, circ);
947 return (NDI_FAILURE);
948 }
949
950 *childp = xvdi_find_dev(parent, devclass, dom, vdev);
951 if (*childp == NULL)
952 *childp = xvdi_create_dev(parent, devclass, dom, vdev);
953
954 ndi_devi_exit(parent, circ);
955
956 if (*childp == NULL)
957 return (NDI_FAILURE);
958 else
959 return (ndi_busop_bus_config(parent, flag,
960 op, arg, childp, 0));
961 }
962
963 case BUS_CONFIG_DRIVER: {
964 xendev_devclass_t devclass = XEN_INVAL;
965
966 cname = ddi_major_to_name((major_t)(uintptr_t)arg);
967 if (cname != NULL)
968 devclass = xendev_nodename_to_devclass(cname);
969
970 if (devclass == XEN_INVAL) {
971 ndi_devi_exit(parent, circ);
972 return (NDI_FAILURE);
973 } else {
974 xendev_enum_class(parent, devclass);
975 ndi_devi_exit(parent, circ);
976 return (ndi_busop_bus_config(parent, flag, op,
977 arg, childp, 0));
978 }
979 /* NOTREACHED */
980 }
981
982 case BUS_CONFIG_ALL:
983 xendev_enum_all(parent, B_FALSE);
984 ndi_devi_exit(parent, circ);
985
986 return (ndi_busop_bus_config(parent, flag, op,
987 arg, childp, 0));
988
989 default:
990 ndi_devi_exit(parent, circ);
991 return (NDI_FAILURE);
992 }
993 }
994
995 /*ARGSUSED*/
996 static int
997 xpvd_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
998 char *eventname, ddi_eventcookie_t *cookie)
999 {
1000 return (ndi_event_retrieve_cookie(xpvd_ndi_event_handle,
1001 rdip, eventname, cookie, NDI_EVENT_NOPASS));
1002 }
1003
1004 /*ARGSUSED*/
1005 static int
1006 xpvd_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
1007 ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
1008 ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
1009 void *arg, ddi_callback_id_t *cb_id)
1010 {
1011 return (ndi_event_add_callback(xpvd_ndi_event_handle,
1012 rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
1013 }
1014
1015 /*ARGSUSED*/
1016 static int
1017 xpvd_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
1018 {
1019 return (ndi_event_remove_callback(xpvd_ndi_event_handle,
1020 cb_id));
1021 }
1022
1023 /*ARGSUSED*/
1024 static int
1025 xpvd_post_event(dev_info_t *dip, dev_info_t *rdip,
1026 ddi_eventcookie_t cookie, void *bus_impldata)
1027 {
1028 return (ndi_event_run_callbacks(xpvd_ndi_event_handle, rdip,
1029 cookie, bus_impldata));
1030 }