1 /******************************************************************************
2 * Copyright (C) 2010 Spectra Logic Corporation
3 * Copyright (C) 2008 Doug Rabson
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005 XenSource Ltd
7 *
8 * This file may be distributed separately from the Linux kernel, or
9 * incorporated into other software packages, subject to the following license:
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this source file (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use, copy, modify,
14 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * IN THE SOFTWARE.
28 */
29
30 /**
31 * \file xenbusb.c
32 *
33 * \brief Shared support functions for managing the NewBus buses that contain
34 * Xen front and back end device instances.
35 *
36 * The NewBus implementation of XenBus attaches a xenbusb_front and xenbusb_back
37 * child bus to the xenstore device. This strategy allows the small differences
38 * in the handling of XenBus operations for front and back devices to be handled
39 * as overrides in xenbusb_front/back.c. Front and back specific device
40 * classes are also provided so device drivers can register for the devices they
41 * can handle without the need to filter within their probe routines. The
42 * net result is a device hierarchy that might look like this:
43 *
44 * xenstore0/
45 * xenbusb_front0/
46 * xn0
47 * xbd0
48 * xbd1
49 * xenbusb_back0/
50 * xbbd0
51 * xnb0
52 * xnb1
53 */
54
55 #include <sys/param.h>
56 #include <sys/bus.h>
57 #include <sys/kernel.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/sbuf.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/systm.h>
65 #include <sys/sx.h>
66 #include <sys/taskqueue.h>
67
68 #include <machine/stdarg.h>
69
70 #include <xen/xen-os.h>
71 #include <xen/gnttab.h>
72 #include <xen/xenstore/xenstorevar.h>
73 #include <xen/xenbus/xenbusb.h>
74 #include <xen/xenbus/xenbusvar.h>
75
76 /*------------------------- Private Functions --------------------------------*/
77 /**
78 * \brief Deallocate XenBus device instance variables.
79 *
80 * \param ivars The instance variable block to free.
81 */
82 static void
xenbusb_free_child_ivars(struct xenbus_device_ivars * ivars)83 xenbusb_free_child_ivars(struct xenbus_device_ivars *ivars)
84 {
85 if (ivars->xd_otherend_watch.node != NULL) {
86 xs_unregister_watch(&ivars->xd_otherend_watch);
87 free(ivars->xd_otherend_watch.node, M_XENBUS);
88 ivars->xd_otherend_watch.node = NULL;
89 }
90
91 if (ivars->xd_local_watch.node != NULL) {
92 xs_unregister_watch(&ivars->xd_local_watch);
93 ivars->xd_local_watch.node = NULL;
94 }
95
96 if (ivars->xd_node != NULL) {
97 free(ivars->xd_node, M_XENBUS);
98 ivars->xd_node = NULL;
99 }
100 ivars->xd_node_len = 0;
101
102 if (ivars->xd_type != NULL) {
103 free(ivars->xd_type, M_XENBUS);
104 ivars->xd_type = NULL;
105 }
106
107 if (ivars->xd_otherend_path != NULL) {
108 free(ivars->xd_otherend_path, M_XENBUS);
109 ivars->xd_otherend_path = NULL;
110 }
111 ivars->xd_otherend_path_len = 0;
112
113 free(ivars, M_XENBUS);
114 }
115
116 /**
117 * XenBus watch callback registered against the "state" XenStore
118 * node of the other-end of a split device connection.
119 *
120 * This callback is invoked whenever the state of a device instance's
121 * peer changes.
122 *
123 * \param watch The xs_watch object used to register this callback
124 * function.
125 * \param vec An array of pointers to NUL terminated strings containing
126 * watch event data. The vector should be indexed via the
127 * xs_watch_type enum in xs_wire.h.
128 * \param vec_size The number of elements in vec.
129 */
130 static void
xenbusb_otherend_watch_cb(struct xs_watch * watch,const char ** vec,unsigned int vec_size __unused)131 xenbusb_otherend_watch_cb(struct xs_watch *watch, const char **vec,
132 unsigned int vec_size __unused)
133 {
134 struct xenbus_device_ivars *ivars;
135 device_t child;
136 device_t bus;
137 const char *path;
138 enum xenbus_state newstate;
139
140 ivars = (struct xenbus_device_ivars *)watch->callback_data;
141 child = ivars->xd_dev;
142 bus = device_get_parent(child);
143
144 path = vec[XS_WATCH_PATH];
145 if (ivars->xd_otherend_path == NULL
146 || strncmp(ivars->xd_otherend_path, path, ivars->xd_otherend_path_len))
147 return;
148
149 newstate = xenbus_read_driver_state(ivars->xd_otherend_path);
150 XENBUSB_OTHEREND_CHANGED(bus, child, newstate);
151 }
152
153 /**
154 * XenBus watch callback registered against the XenStore sub-tree
155 * represnting the local half of a split device connection.
156 *
157 * This callback is invoked whenever any XenStore data in the subtree
158 * is modified, either by us or another privledged domain.
159 *
160 * \param watch The xs_watch object used to register this callback
161 * function.
162 * \param vec An array of pointers to NUL terminated strings containing
163 * watch event data. The vector should be indexed via the
164 * xs_watch_type enum in xs_wire.h.
165 * \param vec_size The number of elements in vec.
166 *
167 */
168 static void
xenbusb_local_watch_cb(struct xs_watch * watch,const char ** vec,unsigned int vec_size __unused)169 xenbusb_local_watch_cb(struct xs_watch *watch, const char **vec,
170 unsigned int vec_size __unused)
171 {
172 struct xenbus_device_ivars *ivars;
173 device_t child;
174 device_t bus;
175 const char *path;
176
177 ivars = (struct xenbus_device_ivars *)watch->callback_data;
178 child = ivars->xd_dev;
179 bus = device_get_parent(child);
180
181 path = vec[XS_WATCH_PATH];
182 if (ivars->xd_node == NULL
183 || strncmp(ivars->xd_node, path, ivars->xd_node_len))
184 return;
185
186 XENBUSB_LOCALEND_CHANGED(bus, child, &path[ivars->xd_node_len]);
187 }
188
189 /**
190 * Search our internal record of configured devices (not the XenStore)
191 * to determine if the XenBus device indicated by \a node is known to
192 * the system.
193 *
194 * \param dev The XenBus bus instance to search for device children.
195 * \param node The XenStore node path for the device to find.
196 *
197 * \return The device_t of the found device if any, or NULL.
198 *
199 * \note device_t is a pointer type, so it can be compared against
200 * NULL for validity.
201 */
202 static device_t
xenbusb_device_exists(device_t dev,const char * node)203 xenbusb_device_exists(device_t dev, const char *node)
204 {
205 device_t *kids;
206 device_t result;
207 struct xenbus_device_ivars *ivars;
208 int i, count;
209
210 if (device_get_children(dev, &kids, &count))
211 return (FALSE);
212
213 result = NULL;
214 for (i = 0; i < count; i++) {
215 ivars = device_get_ivars(kids[i]);
216 if (!strcmp(ivars->xd_node, node)) {
217 result = kids[i];
218 break;
219 }
220 }
221 free(kids, M_TEMP);
222
223 return (result);
224 }
225
226 static void
xenbusb_delete_child(device_t dev,device_t child)227 xenbusb_delete_child(device_t dev, device_t child)
228 {
229 struct xenbus_device_ivars *ivars;
230
231 ivars = device_get_ivars(child);
232
233 /*
234 * We no longer care about the otherend of the
235 * connection. Cancel the watches now so that we
236 * don't try to handle an event for a partially
237 * detached child.
238 */
239 if (ivars->xd_otherend_watch.node != NULL)
240 xs_unregister_watch(&ivars->xd_otherend_watch);
241 if (ivars->xd_local_watch.node != NULL)
242 xs_unregister_watch(&ivars->xd_local_watch);
243
244 device_delete_child(dev, child);
245 xenbusb_free_child_ivars(ivars);
246 }
247
248 /**
249 * \param dev The NewBus device representing this XenBus bus.
250 * \param child The NewBus device representing a child of dev%'s XenBus bus.
251 */
252 static void
xenbusb_verify_device(device_t dev,device_t child)253 xenbusb_verify_device(device_t dev, device_t child)
254 {
255 if (xs_exists(XST_NIL, xenbus_get_node(child), "state") == 0) {
256 /*
257 * Device tree has been removed from Xenbus.
258 * Tear down the device.
259 */
260 xenbusb_delete_child(dev, child);
261 }
262 }
263
264 /**
265 * \brief Enumerate the devices on a XenBus bus and register them with
266 * the NewBus device tree.
267 *
268 * xenbusb_enumerate_bus() will create entries (in state DS_NOTPRESENT)
269 * for nodes that appear in the XenStore, but will not invoke probe/attach
270 * operations on drivers. Probe/Attach processing must be separately
271 * performed via an invocation of xenbusb_probe_children(). This is usually
272 * done via the xbs_probe_children task.
273 *
274 * \param xbs XenBus Bus device softc of the owner of the bus to enumerate.
275 *
276 * \return On success, 0. Otherwise an errno value indicating the
277 * type of failure.
278 */
279 static int
xenbusb_enumerate_bus(struct xenbusb_softc * xbs)280 xenbusb_enumerate_bus(struct xenbusb_softc *xbs)
281 {
282 const char **types;
283 u_int type_idx;
284 u_int type_count;
285 int error;
286
287 error = xs_directory(XST_NIL, xbs->xbs_node, "", &type_count, &types);
288 if (error)
289 return (error);
290
291 for (type_idx = 0; type_idx < type_count; type_idx++)
292 XENBUSB_ENUMERATE_TYPE(xbs->xbs_dev, types[type_idx]);
293
294 free(types, M_XENSTORE);
295
296 return (0);
297 }
298
299 /**
300 * Handler for all generic XenBus device systcl nodes.
301 */
302 static int
xenbusb_device_sysctl_handler(SYSCTL_HANDLER_ARGS)303 xenbusb_device_sysctl_handler(SYSCTL_HANDLER_ARGS)
304 {
305 device_t dev;
306 const char *value;
307
308 dev = (device_t)arg1;
309 switch (arg2) {
310 case XENBUS_IVAR_NODE:
311 value = xenbus_get_node(dev);
312 break;
313 case XENBUS_IVAR_TYPE:
314 value = xenbus_get_type(dev);
315 break;
316 case XENBUS_IVAR_STATE:
317 value = xenbus_strstate(xenbus_get_state(dev));
318 break;
319 case XENBUS_IVAR_OTHEREND_ID:
320 return (sysctl_handle_int(oidp, NULL,
321 xenbus_get_otherend_id(dev),
322 req));
323 /* NOTREACHED */
324 case XENBUS_IVAR_OTHEREND_PATH:
325 value = xenbus_get_otherend_path(dev);
326 break;
327 default:
328 return (EINVAL);
329 }
330 return (SYSCTL_OUT_STR(req, value));
331 }
332
333 /**
334 * Create read-only systcl nodes for xenbusb device ivar data.
335 *
336 * \param dev The XenBus device instance to register with sysctl.
337 */
338 static void
xenbusb_device_sysctl_init(device_t dev)339 xenbusb_device_sysctl_init(device_t dev)
340 {
341 struct sysctl_ctx_list *ctx;
342 struct sysctl_oid *tree;
343
344 ctx = device_get_sysctl_ctx(dev);
345 tree = device_get_sysctl_tree(dev);
346
347 SYSCTL_ADD_PROC(ctx,
348 SYSCTL_CHILDREN(tree),
349 OID_AUTO,
350 "xenstore_path",
351 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
352 dev,
353 XENBUS_IVAR_NODE,
354 xenbusb_device_sysctl_handler,
355 "A",
356 "XenStore path to device");
357
358 SYSCTL_ADD_PROC(ctx,
359 SYSCTL_CHILDREN(tree),
360 OID_AUTO,
361 "xenbus_dev_type",
362 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
363 dev,
364 XENBUS_IVAR_TYPE,
365 xenbusb_device_sysctl_handler,
366 "A",
367 "XenBus device type");
368
369 SYSCTL_ADD_PROC(ctx,
370 SYSCTL_CHILDREN(tree),
371 OID_AUTO,
372 "xenbus_connection_state",
373 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
374 dev,
375 XENBUS_IVAR_STATE,
376 xenbusb_device_sysctl_handler,
377 "A",
378 "XenBus state of peer connection");
379
380 SYSCTL_ADD_PROC(ctx,
381 SYSCTL_CHILDREN(tree),
382 OID_AUTO,
383 "xenbus_peer_domid",
384 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
385 dev,
386 XENBUS_IVAR_OTHEREND_ID,
387 xenbusb_device_sysctl_handler,
388 "I",
389 "Xen domain ID of peer");
390
391 SYSCTL_ADD_PROC(ctx,
392 SYSCTL_CHILDREN(tree),
393 OID_AUTO,
394 "xenstore_peer_path",
395 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
396 dev,
397 XENBUS_IVAR_OTHEREND_PATH,
398 xenbusb_device_sysctl_handler,
399 "A",
400 "XenStore path to peer device");
401 }
402
403 /**
404 * \brief Decrement the number of XenBus child devices in the
405 * connecting state by one and release the xbs_attch_ch
406 * interrupt configuration hook if the connecting count
407 * drops to zero.
408 *
409 * \param xbs XenBus Bus device softc of the owner of the bus to enumerate.
410 */
411 static void
xenbusb_release_confighook(struct xenbusb_softc * xbs)412 xenbusb_release_confighook(struct xenbusb_softc *xbs)
413 {
414 mtx_lock(&xbs->xbs_lock);
415 KASSERT(xbs->xbs_connecting_children > 0,
416 ("Connecting device count error\n"));
417 xbs->xbs_connecting_children--;
418 if (xbs->xbs_connecting_children == 0
419 && (xbs->xbs_flags & XBS_ATTACH_CH_ACTIVE) != 0) {
420 xbs->xbs_flags &= ~XBS_ATTACH_CH_ACTIVE;
421 mtx_unlock(&xbs->xbs_lock);
422 config_intrhook_disestablish(&xbs->xbs_attach_ch);
423 } else {
424 mtx_unlock(&xbs->xbs_lock);
425 }
426 }
427
428 /**
429 * \brief Verify the existence of attached device instances and perform
430 * probe/attach processing for newly arrived devices.
431 *
432 * \param dev The NewBus device representing this XenBus bus.
433 *
434 * \return On success, 0. Otherwise an errno value indicating the
435 * type of failure.
436 */
437 static int
xenbusb_probe_children(device_t dev)438 xenbusb_probe_children(device_t dev)
439 {
440 device_t *kids;
441 struct xenbus_device_ivars *ivars;
442 int i, count, error;
443
444 if (device_get_children(dev, &kids, &count) == 0) {
445 for (i = 0; i < count; i++) {
446 if (device_get_state(kids[i]) != DS_NOTPRESENT) {
447 /*
448 * We already know about this one.
449 * Make sure it's still here.
450 */
451 xenbusb_verify_device(dev, kids[i]);
452 continue;
453 }
454
455 error = device_probe_and_attach(kids[i]);
456 if (error == ENXIO) {
457 struct xenbusb_softc *xbs;
458
459 /*
460 * We don't have a PV driver for this device.
461 * However, an emulated device we do support
462 * may share this backend. Hide the node from
463 * XenBus until the next rescan, but leave it's
464 * state unchanged so we don't inadvertently
465 * prevent attachment of any emulated device.
466 */
467 xenbusb_delete_child(dev, kids[i]);
468
469 /*
470 * Since the XenStore state of this device
471 * still indicates a pending attach, manually
472 * release it's hold on the boot process.
473 */
474 xbs = device_get_softc(dev);
475 xenbusb_release_confighook(xbs);
476
477 continue;
478 } else if (error) {
479 /*
480 * Transition device to the closed state
481 * so the world knows that attachment will
482 * not occur.
483 */
484 xenbus_set_state(kids[i], XenbusStateClosed);
485
486 /*
487 * Remove our record of this device.
488 * So long as it remains in the closed
489 * state in the XenStore, we will not find
490 * it again. The state will only change
491 * if the control domain actively reconfigures
492 * this device.
493 */
494 xenbusb_delete_child(dev, kids[i]);
495
496 continue;
497 }
498 /*
499 * Augment default newbus provided dynamic sysctl
500 * variables with the standard ivar contents of
501 * XenBus devices.
502 */
503 xenbusb_device_sysctl_init(kids[i]);
504
505 /*
506 * Now that we have a driver managing this device
507 * that can receive otherend state change events,
508 * hook up a watch for them.
509 */
510 ivars = device_get_ivars(kids[i]);
511 xs_register_watch(&ivars->xd_otherend_watch);
512 xs_register_watch(&ivars->xd_local_watch);
513 }
514 free(kids, M_TEMP);
515 }
516
517 return (0);
518 }
519
520 /**
521 * \brief Task callback function to perform XenBus probe operations
522 * from a known safe context.
523 *
524 * \param arg The NewBus device_t representing the bus instance to
525 * on which to perform probe processing.
526 * \param pending The number of times this task was queued before it could
527 * be run.
528 */
529 static void
xenbusb_probe_children_cb(void * arg,int pending __unused)530 xenbusb_probe_children_cb(void *arg, int pending __unused)
531 {
532 device_t dev = (device_t)arg;
533
534 bus_topo_lock();
535 xenbusb_probe_children(dev);
536 bus_topo_unlock();
537 }
538
539 /**
540 * \brief XenStore watch callback for the root node of the XenStore
541 * subtree representing a XenBus.
542 *
543 * This callback performs, or delegates to the xbs_probe_children task,
544 * all processing necessary to handle dynmaic device arrival and departure
545 * events from a XenBus.
546 *
547 * \param watch The XenStore watch object associated with this callback.
548 * \param vec The XenStore watch event data.
549 * \param len The number of fields in the event data stream.
550 */
551 static void
xenbusb_devices_changed(struct xs_watch * watch,const char ** vec,unsigned int len)552 xenbusb_devices_changed(struct xs_watch *watch, const char **vec,
553 unsigned int len)
554 {
555 struct xenbusb_softc *xbs;
556 device_t dev;
557 char *node;
558 char *type;
559 char *id;
560 char *p;
561 u_int component;
562
563 xbs = (struct xenbusb_softc *)watch->callback_data;
564 dev = xbs->xbs_dev;
565
566 if (len <= XS_WATCH_PATH) {
567 device_printf(dev, "xenbusb_devices_changed: "
568 "Short Event Data.\n");
569 return;
570 }
571
572 node = strdup(vec[XS_WATCH_PATH], M_XENBUS);
573 p = strchr(node, '/');
574 if (p == NULL)
575 goto out;
576 *p = 0;
577 type = p + 1;
578
579 p = strchr(type, '/');
580 if (p == NULL)
581 goto out;
582 *p++ = 0;
583
584 /*
585 * Extract the device ID. A device ID has one or more path
586 * components separated by the '/' character.
587 *
588 * e.g. "<frontend vm id>/<frontend dev id>" for backend devices.
589 */
590 id = p;
591 for (component = 0; component < xbs->xbs_id_components; component++) {
592 p = strchr(p, '/');
593 if (p == NULL)
594 break;
595 p++;
596 }
597 if (p != NULL)
598 *p = 0;
599
600 if (*id != 0 && component >= xbs->xbs_id_components - 1) {
601 xenbusb_add_device(xbs->xbs_dev, type, id);
602 taskqueue_enqueue(taskqueue_thread, &xbs->xbs_probe_children);
603 }
604 out:
605 free(node, M_XENBUS);
606 }
607
608 /**
609 * \brief Interrupt configuration hook callback associated with xbs_attch_ch.
610 *
611 * Since interrupts are always functional at the time of XenBus configuration,
612 * there is nothing to be done when the callback occurs. This hook is only
613 * registered to hold up boot processing while XenBus devices come online.
614 *
615 * \param arg Unused configuration hook callback argument.
616 */
617 static void
xenbusb_nop_confighook_cb(void * arg __unused)618 xenbusb_nop_confighook_cb(void *arg __unused)
619 {
620 }
621
622 /*--------------------------- Public Functions -------------------------------*/
623 /*--------- API comments for these methods can be found in xenbusb.h ---------*/
624 void
xenbusb_identify(driver_t * driver,device_t parent)625 xenbusb_identify(driver_t *driver, device_t parent)
626 {
627 /*
628 * A single instance of each bus type for which we have a driver
629 * is always present in a system operating under Xen.
630 */
631 BUS_ADD_CHILD(parent, 0, driver->name, 0);
632 }
633
634 int
xenbusb_add_device(device_t dev,const char * type,const char * id)635 xenbusb_add_device(device_t dev, const char *type, const char *id)
636 {
637 struct xenbusb_softc *xbs;
638 struct sbuf *devpath_sbuf;
639 char *devpath;
640 struct xenbus_device_ivars *ivars;
641 int error;
642
643 xbs = device_get_softc(dev);
644 devpath_sbuf = sbuf_new_auto();
645 sbuf_printf(devpath_sbuf, "%s/%s/%s", xbs->xbs_node, type, id);
646 sbuf_finish(devpath_sbuf);
647 devpath = sbuf_data(devpath_sbuf);
648
649 ivars = malloc(sizeof(*ivars), M_XENBUS, M_ZERO|M_WAITOK);
650 error = ENXIO;
651
652 if (xs_exists(XST_NIL, devpath, "") != 0) {
653 device_t child;
654 enum xenbus_state state;
655 char *statepath;
656
657 child = xenbusb_device_exists(dev, devpath);
658 if (child != NULL) {
659 /*
660 * We are already tracking this node
661 */
662 error = 0;
663 goto out;
664 }
665
666 state = xenbus_read_driver_state(devpath);
667 if (state != XenbusStateInitialising) {
668 /*
669 * Device is not new, so ignore it. This can
670 * happen if a device is going away after
671 * switching to Closed.
672 */
673 printf("xenbusb_add_device: Device %s ignored. "
674 "State %d\n", devpath, state);
675 error = 0;
676 goto out;
677 }
678
679 sx_init(&ivars->xd_lock, "xdlock");
680 ivars->xd_flags = XDF_CONNECTING;
681 ivars->xd_node = strdup(devpath, M_XENBUS);
682 ivars->xd_node_len = strlen(devpath);
683 ivars->xd_type = strdup(type, M_XENBUS);
684 ivars->xd_state = XenbusStateInitialising;
685
686 error = XENBUSB_GET_OTHEREND_NODE(dev, ivars);
687 if (error) {
688 printf("xenbus_update_device: %s no otherend id\n",
689 devpath);
690 goto out;
691 }
692
693 statepath = malloc(ivars->xd_otherend_path_len
694 + strlen("/state") + 1, M_XENBUS, M_WAITOK);
695 sprintf(statepath, "%s/state", ivars->xd_otherend_path);
696 ivars->xd_otherend_watch.node = statepath;
697 ivars->xd_otherend_watch.callback = xenbusb_otherend_watch_cb;
698 ivars->xd_otherend_watch.callback_data = (uintptr_t)ivars;
699 /*
700 * Other end state node watch, limit to one pending event
701 * to prevent frontends from queuing too many events that
702 * could cause resource starvation.
703 */
704 ivars->xd_otherend_watch.max_pending = 1;
705
706 ivars->xd_local_watch.node = ivars->xd_node;
707 ivars->xd_local_watch.callback = xenbusb_local_watch_cb;
708 ivars->xd_local_watch.callback_data = (uintptr_t)ivars;
709 /*
710 * Watch our local path, only writable by us or a privileged
711 * domain, no need to limit.
712 */
713 ivars->xd_local_watch.max_pending = 0;
714
715 mtx_lock(&xbs->xbs_lock);
716 xbs->xbs_connecting_children++;
717 mtx_unlock(&xbs->xbs_lock);
718
719 child = device_add_child(dev, NULL, -1);
720 ivars->xd_dev = child;
721 device_set_ivars(child, ivars);
722 }
723
724 out:
725 sbuf_delete(devpath_sbuf);
726 if (error != 0)
727 xenbusb_free_child_ivars(ivars);
728
729 return (error);
730 }
731
732 int
xenbusb_attach(device_t dev,char * bus_node,u_int id_components)733 xenbusb_attach(device_t dev, char *bus_node, u_int id_components)
734 {
735 struct xenbusb_softc *xbs;
736
737 xbs = device_get_softc(dev);
738 mtx_init(&xbs->xbs_lock, "xenbusb softc lock", NULL, MTX_DEF);
739 xbs->xbs_node = bus_node;
740 xbs->xbs_id_components = id_components;
741 xbs->xbs_dev = dev;
742
743 /*
744 * Since XenBus buses are attached to the XenStore, and
745 * the XenStore does not probe children until after interrupt
746 * services are available, this config hook is used solely
747 * to ensure that the remainder of the boot process (e.g.
748 * mount root) is deferred until child devices are adequately
749 * probed. We unblock the boot process as soon as the
750 * connecting child count in our softc goes to 0.
751 */
752 xbs->xbs_attach_ch.ich_func = xenbusb_nop_confighook_cb;
753 xbs->xbs_attach_ch.ich_arg = dev;
754 config_intrhook_establish(&xbs->xbs_attach_ch);
755 xbs->xbs_flags |= XBS_ATTACH_CH_ACTIVE;
756 xbs->xbs_connecting_children = 1;
757
758 /*
759 * The subtree for this bus type may not yet exist
760 * causing initial enumeration to fail. We still
761 * want to return success from our attach though
762 * so that we are ready to handle devices for this
763 * bus when they are dynamically attached to us
764 * by a Xen management action.
765 */
766 (void)xenbusb_enumerate_bus(xbs);
767 xenbusb_probe_children(dev);
768
769 xbs->xbs_device_watch.node = bus_node;
770 xbs->xbs_device_watch.callback = xenbusb_devices_changed;
771 xbs->xbs_device_watch.callback_data = (uintptr_t)xbs;
772 /*
773 * Allow for unlimited pending watches, as those are local paths
774 * either controlled by the guest or only writable by privileged
775 * domains.
776 */
777 xbs->xbs_device_watch.max_pending = 0;
778
779 TASK_INIT(&xbs->xbs_probe_children, 0, xenbusb_probe_children_cb, dev);
780
781 xs_register_watch(&xbs->xbs_device_watch);
782
783 xenbusb_release_confighook(xbs);
784
785 return (0);
786 }
787
788 int
xenbusb_resume(device_t dev)789 xenbusb_resume(device_t dev)
790 {
791 device_t *kids;
792 struct xenbus_device_ivars *ivars;
793 int i, count, error;
794 char *statepath;
795
796 /*
797 * We must re-examine each device and find the new path for
798 * its backend.
799 */
800 if (device_get_children(dev, &kids, &count) == 0) {
801 for (i = 0; i < count; i++) {
802 if (device_get_state(kids[i]) == DS_NOTPRESENT)
803 continue;
804
805 if (xen_suspend_cancelled) {
806 DEVICE_RESUME(kids[i]);
807 continue;
808 }
809
810 ivars = device_get_ivars(kids[i]);
811
812 xs_unregister_watch(&ivars->xd_otherend_watch);
813 xenbus_set_state(kids[i], XenbusStateInitialising);
814
815 /*
816 * Find the new backend details and
817 * re-register our watch.
818 */
819 error = XENBUSB_GET_OTHEREND_NODE(dev, ivars);
820 if (error)
821 return (error);
822
823 statepath = malloc(ivars->xd_otherend_path_len
824 + strlen("/state") + 1, M_XENBUS, M_WAITOK);
825 sprintf(statepath, "%s/state", ivars->xd_otherend_path);
826
827 free(ivars->xd_otherend_watch.node, M_XENBUS);
828 ivars->xd_otherend_watch.node = statepath;
829
830 DEVICE_RESUME(kids[i]);
831
832 xs_register_watch(&ivars->xd_otherend_watch);
833 #if 0
834 /*
835 * Can't do this yet since we are running in
836 * the xenwatch thread and if we sleep here,
837 * we will stop delivering watch notifications
838 * and the device will never come back online.
839 */
840 sx_xlock(&ivars->xd_lock);
841 while (ivars->xd_state != XenbusStateClosed
842 && ivars->xd_state != XenbusStateConnected)
843 sx_sleep(&ivars->xd_state, &ivars->xd_lock,
844 0, "xdresume", 0);
845 sx_xunlock(&ivars->xd_lock);
846 #endif
847 }
848 free(kids, M_TEMP);
849 }
850
851 return (0);
852 }
853
854 int
xenbusb_print_child(device_t dev,device_t child)855 xenbusb_print_child(device_t dev, device_t child)
856 {
857 struct xenbus_device_ivars *ivars = device_get_ivars(child);
858 int retval = 0;
859
860 retval += bus_print_child_header(dev, child);
861 retval += printf(" at %s", ivars->xd_node);
862 retval += bus_print_child_footer(dev, child);
863
864 return (retval);
865 }
866
867 int
xenbusb_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)868 xenbusb_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
869 {
870 struct xenbus_device_ivars *ivars = device_get_ivars(child);
871
872 switch (index) {
873 case XENBUS_IVAR_NODE:
874 *result = (uintptr_t) ivars->xd_node;
875 return (0);
876
877 case XENBUS_IVAR_TYPE:
878 *result = (uintptr_t) ivars->xd_type;
879 return (0);
880
881 case XENBUS_IVAR_STATE:
882 *result = (uintptr_t) ivars->xd_state;
883 return (0);
884
885 case XENBUS_IVAR_OTHEREND_ID:
886 *result = (uintptr_t) ivars->xd_otherend_id;
887 return (0);
888
889 case XENBUS_IVAR_OTHEREND_PATH:
890 *result = (uintptr_t) ivars->xd_otherend_path;
891 return (0);
892 }
893
894 return (ENOENT);
895 }
896
897 int
xenbusb_write_ivar(device_t dev,device_t child,int index,uintptr_t value)898 xenbusb_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
899 {
900 struct xenbus_device_ivars *ivars = device_get_ivars(child);
901 enum xenbus_state newstate;
902 int currstate;
903
904 switch (index) {
905 case XENBUS_IVAR_STATE:
906 {
907 int error;
908 struct xs_transaction xst;
909
910 newstate = (enum xenbus_state)value;
911 sx_xlock(&ivars->xd_lock);
912 if (ivars->xd_state == newstate) {
913 error = 0;
914 goto out;
915 }
916
917 do {
918 error = xs_transaction_start(&xst);
919 if (error != 0)
920 goto out;
921
922 do {
923 error = xs_scanf(xst, ivars->xd_node, "state",
924 NULL, "%d", &currstate);
925 } while (error == EAGAIN);
926 if (error)
927 goto out;
928
929 do {
930 error = xs_printf(xst, ivars->xd_node, "state",
931 "%d", newstate);
932 } while (error == EAGAIN);
933 if (error) {
934 /*
935 * Avoid looping through xenbus_dev_fatal()
936 * which calls xenbus_write_ivar to set the
937 * state to closing.
938 */
939 if (newstate != XenbusStateClosing)
940 xenbus_dev_fatal(dev, error,
941 "writing new state");
942 goto out;
943 }
944 } while (xs_transaction_end(xst, 0));
945 ivars->xd_state = newstate;
946
947 if ((ivars->xd_flags & XDF_CONNECTING) != 0 &&
948 (newstate == XenbusStateClosed ||
949 newstate == XenbusStateConnected)) {
950 struct xenbusb_softc *xbs;
951
952 ivars->xd_flags &= ~XDF_CONNECTING;
953 xbs = device_get_softc(dev);
954 xenbusb_release_confighook(xbs);
955 }
956
957 wakeup(&ivars->xd_state);
958 out:
959 if (error != 0)
960 xs_transaction_end(xst, 1);
961 sx_xunlock(&ivars->xd_lock);
962 /*
963 * Shallow ENOENT errors, as returning an error here will
964 * trigger a panic. ENOENT is fine to ignore, because it means
965 * the toolstack has removed the state node as part of
966 * destroying the device, and so we have to shut down the
967 * device without recreating it or else the node would be
968 * leaked.
969 */
970 return (error == ENOENT ? 0 : error);
971 }
972
973 case XENBUS_IVAR_NODE:
974 case XENBUS_IVAR_TYPE:
975 case XENBUS_IVAR_OTHEREND_ID:
976 case XENBUS_IVAR_OTHEREND_PATH:
977 /*
978 * These variables are read-only.
979 */
980 return (EINVAL);
981 }
982
983 return (ENOENT);
984 }
985
986 void
xenbusb_otherend_changed(device_t bus,device_t child,enum xenbus_state state)987 xenbusb_otherend_changed(device_t bus, device_t child, enum xenbus_state state)
988 {
989 XENBUS_OTHEREND_CHANGED(child, state);
990 }
991
992 void
xenbusb_localend_changed(device_t bus,device_t child,const char * path)993 xenbusb_localend_changed(device_t bus, device_t child, const char *path)
994 {
995
996 if (strcmp(path, "/state") != 0) {
997 struct xenbus_device_ivars *ivars;
998
999 ivars = device_get_ivars(child);
1000 sx_xlock(&ivars->xd_lock);
1001 ivars->xd_state = xenbus_read_driver_state(ivars->xd_node);
1002 sx_xunlock(&ivars->xd_lock);
1003 }
1004 XENBUS_LOCALEND_CHANGED(child, path);
1005 }
1006