1 /* $NetBSD: xenbus_probe.c,v 1.62 2025/02/06 15:51:58 bouyer Exp $ */
2 /******************************************************************************
3  * Talks to Xen Store to figure out what devices we have.
4  *
5  * Copyright (C) 2005 Rusty Russell, IBM Corporation
6  * Copyright (C) 2005 Mike Wray, Hewlett-Packard
7  * Copyright (C) 2005 XenSource Ltd
8  *
9  * This file may be distributed separately from the Linux kernel, or
10  * incorporated into other software packages, subject to the following license:
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this source file (the "Software"), to deal in the Software without
14  * restriction, including without limitation the rights to use, copy, modify,
15  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16  * and to permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28  * IN THE SOFTWARE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.62 2025/02/06 15:51:58 bouyer Exp $");
33 
34 #if 0
35 #define DPRINTK(fmt, args...) \
36     printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
37 #else
38 #define DPRINTK(fmt, args...) ((void)0)
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/null.h>
43 #include <sys/errno.h>
44 #include <sys/kmem.h>
45 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <uvm/uvm.h>
50 
51 #include <xen/xen.h>          /* for xendomain_is_dom0() */
52 #include <xen/hypervisor.h>
53 #include <xen/xenbus.h>
54 #include <xen/evtchn.h>
55 #include <xen/shutdown_xenbus.h>
56 
57 #include "xenbus_comms.h"
58 
59 #include "kernfs.h"
60 
61 static int  xenbus_match(device_t, cfdata_t, void *);
62 static void xenbus_attach(device_t, device_t, void *);
63 static int  xenbus_print(void *, const char *);
64 
65 /* power management, for save/restore */
66 static bool xenbus_suspend(device_t, const pmf_qual_t *);
67 static bool xenbus_resume(device_t, const pmf_qual_t *);
68 
69 /* routines gathering device information from XenStore */
70 static int  read_otherend_details(struct xenbus_device *,
71                     const char *, const char *);
72 static int  read_backend_details (struct xenbus_device *);
73 static int  read_frontend_details(struct xenbus_device *);
74 static void free_otherend_details(struct xenbus_device *);
75 
76 static int  watch_otherend     (struct xenbus_device *);
77 static void free_otherend_watch(struct xenbus_device *);
78 
79 static void xenbus_probe_init(void *);
80 
81 static struct xenbus_device *xenbus_lookup_device_path(const char *);
82 
83 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
84     NULL, NULL);
85 
86 device_t xenbus_dev;
87 bus_dma_tag_t xenbus_dmat;
88 
89 SLIST_HEAD(, xenbus_device) xenbus_device_list;
90 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
91           SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
92 
93 int
xenbus_match(device_t parent,cfdata_t match,void * aux)94 xenbus_match(device_t parent, cfdata_t match, void *aux)
95 {
96           struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
97 
98           if (strcmp(xa->xa_device, "xenbus") == 0)
99                     return 1;
100           return 0;
101 }
102 
103 static void
xenbus_attach(device_t parent,device_t self,void * aux)104 xenbus_attach(device_t parent, device_t self, void *aux)
105 {
106           struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
107           int err;
108 
109           aprint_normal(": Xen Virtual Bus Interface\n");
110           xenbus_dev = self;
111           xenbus_dmat = xa->xa_dmat;
112           config_pending_incr(self);
113 
114           err = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
115               xenbus_probe_init, NULL, NULL, "xenbus_probe");
116           if (err)
117                     aprint_error_dev(xenbus_dev,
118                                         "kthread_create(xenbus_probe): %d\n", err);
119 
120           if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
121                     aprint_error_dev(self, "couldn't establish power handler\n");
122 }
123 
124 static bool
xenbus_suspend(device_t dev,const pmf_qual_t * qual)125 xenbus_suspend(device_t dev, const pmf_qual_t *qual)
126 {
127           xs_suspend();
128           xb_suspend_comms(dev);
129 
130           return true;
131 }
132 
133 static bool
xenbus_resume(device_t dev,const pmf_qual_t * qual)134 xenbus_resume(device_t dev, const pmf_qual_t *qual)
135 {
136           xb_resume_comms(dev);
137           xs_resume();
138 
139           return true;
140 }
141 
142 /*
143  * Suspend a xenbus device
144  */
145 bool
xenbus_device_suspend(struct xenbus_device * dev)146 xenbus_device_suspend(struct xenbus_device *dev) {
147 
148           free_otherend_details(dev);
149           return true;
150 }
151 
152 /*
153  * Resume a xenbus device
154  */
155 bool
xenbus_device_resume(struct xenbus_device * dev)156 xenbus_device_resume(struct xenbus_device *dev) {
157 
158           if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
159                     read_backend_details(dev);
160           }
161 
162           return true;
163 }
164 
165 void
xenbus_backend_register(struct xenbus_backend_driver * xbakd)166 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
167 {
168           SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
169 }
170 
171 static int
read_otherend_details(struct xenbus_device * xendev,const char * id_node,const char * path_node)172 read_otherend_details(struct xenbus_device *xendev,
173                                          const char *id_node, const char *path_node)
174 {
175           int err;
176           unsigned long id;
177 
178           err = xenbus_read_ul(NULL, xendev->xbusd_path, id_node, &id, 10);
179           if (err) {
180                     printf("reading other end details %s from %s\n",
181                         id_node, xendev->xbusd_path);
182                     xenbus_dev_fatal(xendev, err,
183                                          "reading other end details %s from %s",
184                                          id_node, xendev->xbusd_path);
185                     return err;
186           }
187           xendev->xbusd_otherend_id = (int)id;
188 
189           err = xenbus_read(NULL, xendev->xbusd_path, path_node,
190               xendev->xbusd_otherend, sizeof(xendev->xbusd_otherend));
191           if (err) {
192                     printf("reading other end details %s from %s (%d)\n",
193                         path_node, xendev->xbusd_path, err);
194                     xenbus_dev_fatal(xendev, err,
195                                          "reading other end details %s from %s",
196                                          path_node, xendev->xbusd_path);
197                     return err;
198           }
199           DPRINTK("read_otherend_details: read %s/%s returned %s\n",
200               xendev->xbusd_path, path_node, xendev->xbusd_otherend);
201 
202           if (strlen(xendev->xbusd_otherend) == 0 ||
203               !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
204                     printf("missing other end from %s\n", xendev->xbusd_path);
205                     xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
206                                          xendev->xbusd_path);
207                     free_otherend_details(xendev);
208                     return ENOENT;
209           }
210 
211           return 0;
212 }
213 
214 static int
read_backend_details(struct xenbus_device * xendev)215 read_backend_details(struct xenbus_device *xendev)
216 {
217           return read_otherend_details(xendev, "backend-id", "backend");
218 }
219 
220 
221 static int
read_frontend_details(struct xenbus_device * xendev)222 read_frontend_details(struct xenbus_device *xendev)
223 {
224           return read_otherend_details(xendev, "frontend-id", "frontend");
225 }
226 
227 static void
free_otherend_details(struct xenbus_device * dev)228 free_otherend_details(struct xenbus_device *dev)
229 {
230           /* Nothing to free */
231           dev->xbusd_otherend[0] = '\0';
232 }
233 
234 static void
free_otherend_watch(struct xenbus_device * dev)235 free_otherend_watch(struct xenbus_device *dev)
236 {
237           if (dev->xbusd_otherend_watch.node)
238                     xenbus_unwatch_path(&dev->xbusd_otherend_watch);
239 }
240 
241 static void
otherend_changed(struct xenbus_watch * watch,const char ** vec,unsigned int len)242 otherend_changed(struct xenbus_watch *watch,
243                                    const char **vec, unsigned int len)
244 {
245           struct xenbus_device *xdev = watch->xbw_dev;
246           XenbusState state;
247 
248           /* Protect us against watches firing on old details when the otherend
249              details change, say immediately after a resume. */
250           if (strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
251                         strlen(xdev->xbusd_otherend))) {
252                     DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
253                     return;
254           }
255 
256           state = xenbus_read_driver_state(xdev->xbusd_otherend);
257 
258           DPRINTK("state is %d, %s, %s",
259                     state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
260           if (state == XenbusStateClosed) {
261                     int error;
262                     if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
263                               error = xdev->xbusd_u.b.b_detach(
264                                   xdev->xbusd_u.b.b_cookie);
265                               if (error) {
266                                         printf("could not detach %s: %d\n",
267                                             xdev->xbusd_path, error);
268                                         return;
269                               }
270                     } else {
271                               error = config_detach(xdev->xbusd_u.f.f_dev,
272                                   DETACH_FORCE);
273                               if (error) {
274                                         printf("could not detach %s: %d\n",
275                                             device_xname(xdev->xbusd_u.f.f_dev), error);
276                                         return;
277                               }
278                     }
279                     xenbus_free_device(xdev);
280                     return;
281           }
282           if (xdev->xbusd_otherend_changed)
283                     xdev->xbusd_otherend_changed(
284                         (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
285                         xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
286 }
287 
288 static int
watch_otherend(struct xenbus_device * dev)289 watch_otherend(struct xenbus_device *dev)
290 {
291           free_otherend_watch(dev);
292 
293           return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
294                                           &dev->xbusd_otherend_watch,
295                                           otherend_changed);
296 }
297 
298 static struct xenbus_device *
xenbus_lookup_device_path(const char * path)299 xenbus_lookup_device_path(const char *path)
300 {
301           struct xenbus_device *xbusd;
302 
303           SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
304                     if (strcmp(xbusd->xbusd_path, path) == 0)
305                               return xbusd;
306           }
307           return NULL;
308 }
309 
310 static int
xenbus_probe_device_type(const char * path,const char * type,int (* create)(struct xenbus_device *))311 xenbus_probe_device_type(const char *path, const char *type,
312     int (*create)(struct xenbus_device *))
313 {
314           int err, i, pos, msize;
315           int *lookup = NULL;
316           size_t lookup_sz = 0;
317           unsigned long state;
318           char **dir;
319           unsigned int orig_dir_n = 0, dir_n;
320           struct xenbus_device *xbusd;
321           struct xenbusdev_attach_args xa;
322           char *ep;
323 
324           DPRINTK("probe %s type %s", path, type);
325           err = xenbus_directory(NULL, path, "", &orig_dir_n, &dir);
326           DPRINTK("directory err %d dir_n %d", err, orig_dir_n);
327           if (err)
328                     return err;
329           dir_n = orig_dir_n;
330 
331           /* Only sort frontend devices i.e. create == NULL*/
332           if (dir_n > 1 && create == NULL) {
333                     int minp;
334                     unsigned long minv;
335                     unsigned long *id;
336                     size_t id_sz;
337 
338                     lookup_sz = sizeof(int) * dir_n;
339                     lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
340 
341                     id_sz = sizeof(unsigned long) * dir_n;
342                     id = kmem_zalloc(id_sz, KM_SLEEP);
343 
344                     /* Convert string values to numeric; skip invalid */
345                     for (i = 0; i < dir_n; i++) {
346                               /*
347                                * Add one to differentiate numerical zero from invalid
348                                * string. Has no effect on sort order.
349                                */
350                               id[i] = strtoul(dir[i], &ep, 10) + 1;
351                               if (dir[i][0] == '\0' || *ep != '\0')
352                                         id[i] = 0;
353                     }
354 
355                     /* Build lookup table in ascending order */
356                     for (pos = 0; pos < dir_n; ) {
357                               minv = UINT32_MAX;
358                               minp = -1;
359                               for (i = 0; i < dir_n; i++) {
360                                         if (id[i] < minv && id[i] > 0) {
361                                                   minv = id[i];
362                                                   minp = i;
363                                         }
364                               }
365                               if (minp >= 0) {
366                                         lookup[pos++] = minp;
367                                         id[minp] = 0;
368                               }
369                               else
370                                         break;
371                     }
372 
373                     kmem_free(id, id_sz);
374 
375                     /* Adjust in case we had to skip non-numeric entries */
376                     dir_n = pos;
377           }
378 
379           for (pos = 0; pos < dir_n; pos++) {
380                     err = 0;
381                     if (lookup)
382                               i = lookup[pos];
383                     else
384                               i = pos;
385                     /*
386                      * add size of path to size of xenbus_device. xenbus_device
387                      * already has room for one char in xbusd_path.
388                      */
389                     msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
390                     xbusd = kmem_zalloc(msize, KM_SLEEP);
391                     xbusd->xbusd_sz = msize;
392                     xbusd->xbusd_dmat = xenbus_dmat;
393 
394                     snprintf(__UNCONST(xbusd->xbusd_path),
395                         msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
396                     if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
397                               /* device already registered */
398                               kmem_free(xbusd, xbusd->xbusd_sz);
399                               continue;
400                     }
401                     err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
402                         &state, 10);
403                     if (err) {
404                               aprint_error_dev(xenbus_dev, "can't get state "
405                                   "for %s (%d)\n", xbusd->xbusd_path, err);
406                               kmem_free(xbusd, xbusd->xbusd_sz);
407                               err = 0;
408                               continue;
409                     }
410                     if (state != XenbusStateInitialising) {
411                               /* device is not new */
412                               kmem_free(xbusd, xbusd->xbusd_sz);
413                               continue;
414                     }
415 
416                     xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
417                     DPRINTK("xenbus_probe_device_type probe %s\n",
418                         xbusd->xbusd_path);
419                     if (create != NULL) {
420                               xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
421                               err = read_frontend_details(xbusd);
422                               if (err != 0) {
423                                         aprint_error_dev(xenbus_dev,
424                                             "can't get frontend details for %s (%d)\n",
425                                             xbusd->xbusd_path, err);
426                                         break;
427                               }
428                               if (create(xbusd)) {
429                                         kmem_free(xbusd, xbusd->xbusd_sz);
430                                         continue;
431                               }
432                     } else {
433                               xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
434                               xa.xa_xbusd = xbusd;
435                               xa.xa_type = type;
436                               xa.xa_id = strtoul(dir[i], &ep, 0);
437                               if (dir[i][0] == '\0' || *ep != '\0') {
438                                         aprint_error_dev(xenbus_dev,
439                                             "device type %s: id %s is not a number\n",
440                                             type, dir[i]);
441                                         err = EFTYPE;
442                                         kmem_free(xbusd, xbusd->xbusd_sz);
443                                         break;
444                               }
445                               if (strcmp(xa.xa_type, "vbd") == 0) {
446                                         char dtype[10];
447                                         if (xenbus_read(NULL, xbusd->xbusd_path,
448                                             "device-type", dtype, sizeof(dtype)) !=0) {
449                                                   aprint_error_dev(xenbus_dev,
450                                                       "%s: can't read device-type\n",
451                                                       xbusd->xbusd_path);
452                                                   kmem_free(xbusd, xbusd->xbusd_sz);
453                                                   break;
454                                         }
455                                         if (vm_guest == VM_GUEST_XENPVHVM &&
456                                             strcmp(dtype, "cdrom") == 0) {
457                                                   aprint_verbose_dev(xenbus_dev,
458                                                       "ignoring %s type cdrom\n",
459                                                       xbusd->xbusd_path);
460                                                   kmem_free(xbusd, xbusd->xbusd_sz);
461                                                   continue;
462                                         }
463                               }
464                               err = read_backend_details(xbusd);
465                               if (err != 0) {
466                                         aprint_error_dev(xenbus_dev,
467                                             "can't get backend details for %s (%d)\n",
468                                             xbusd->xbusd_path, err);
469                                         kmem_free(xbusd, xbusd->xbusd_sz);
470                                         break;
471                               }
472 
473                               KERNEL_LOCK(1, curlwp);
474                               xbusd->xbusd_u.f.f_dev = config_found(xenbus_dev,
475                                   &xa, xenbus_print, CFARGS_NONE);
476                               KERNEL_UNLOCK_ONE(curlwp);
477                               if (xbusd->xbusd_u.f.f_dev == NULL) {
478                                         kmem_free(xbusd, xbusd->xbusd_sz);
479                                         continue;
480                               }
481                     }
482                     SLIST_INSERT_HEAD(&xenbus_device_list,
483                         xbusd, xbusd_entries);
484                     watch_otherend(xbusd);
485           }
486           xenbus_directory_free(orig_dir_n, dir);
487           if (lookup)
488                     kmem_free(lookup, lookup_sz);
489 
490           return err;
491 }
492 
493 static int
xenbus_print(void * aux,const char * pnp)494 xenbus_print(void *aux, const char *pnp)
495 {
496           struct xenbusdev_attach_args *xa = aux;
497 
498           if (pnp) {
499                     if (strcmp(xa->xa_type, "vbd") == 0)
500                               aprint_normal("xbd");
501                     else if (strcmp(xa->xa_type, "vif") == 0)
502                               aprint_normal("xennet");
503                     else if (strcmp(xa->xa_type, "balloon") == 0)
504                               aprint_normal("balloon");
505                     else
506                               aprint_normal("unknown type %s", xa->xa_type);
507                     aprint_normal(" at %s", pnp);
508           }
509           aprint_normal(" id %d", xa->xa_id);
510           return(UNCONF);
511 }
512 
513 static int
xenbus_probe_frontends(void)514 xenbus_probe_frontends(void)
515 {
516           int err;
517           char **dir;
518           unsigned int i, dir_n;
519           char path[30];
520 
521           DPRINTK("probe device");
522           err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
523           DPRINTK("directory err %d dir_n %d", err, dir_n);
524           if (err)
525                     return err;
526 
527           for (i = 0; i < dir_n; i++) {
528                     /*
529                      * console is configured through xen_start_info when
530                      * xencons is attaching to hypervisor, so avoid console
531                      * probing when configuring xenbus devices
532                      */
533                     if (strcmp(dir[i], "console") == 0)
534                               continue;
535 
536                     snprintf(path, sizeof(path), "device/%s", dir[i]);
537                     err = xenbus_probe_device_type(path, dir[i], NULL);
538                     if (err)
539                               break;
540           }
541           xenbus_directory_free(dir_n, dir);
542           return err;
543 }
544 
545 static int
xenbus_probe_backends(void)546 xenbus_probe_backends(void)
547 {
548           int err;
549           char **dirt, **dirid;
550           unsigned int type, id, dirt_n, dirid_n;
551           char path[30];
552           struct xenbus_backend_driver *xbakd;
553 
554           DPRINTK("probe backend");
555           err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
556           DPRINTK("directory err %d dirt_n %d", err, dirt_n);
557           if (err)
558                     return err;
559 
560           for (type = 0; type < dirt_n; type++) {
561                     SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
562                         xbakd_entries) {
563                               if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
564                                         break;
565                     }
566                     if (xbakd == NULL)
567                               continue;
568                     err = xenbus_directory(NULL, "backend", dirt[type],
569                         &dirid_n, &dirid);
570                     DPRINTK("directory backend/%s err %d dirid_n %d",
571                         dirt[type], err, dirid_n);
572                     if (err)
573                               goto out;
574 
575                     for (id = 0; id < dirid_n; id++) {
576                               snprintf(path, sizeof(path), "backend/%s/%s",
577                                   dirt[type], dirid[id]);
578                               err = xenbus_probe_device_type(path, dirt[type],
579                                   xbakd->xbakd_create);
580                               if (err)
581                                         break;
582                     }
583                     xenbus_directory_free(dirid_n, dirid);
584           }
585 
586 out:
587           xenbus_directory_free(dirt_n, dirt);
588           return err;
589 }
590 
591 int
xenbus_free_device(struct xenbus_device * xbusd)592 xenbus_free_device(struct xenbus_device *xbusd)
593 {
594           KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
595           SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
596           free_otherend_watch(xbusd);
597           free_otherend_details(xbusd);
598           xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
599           kmem_free(xbusd, xbusd->xbusd_sz);
600           return 0;
601 }
602 
603 static void
frontend_changed(struct xenbus_watch * watch,const char ** vec,unsigned int len)604 frontend_changed(struct xenbus_watch *watch,
605                                    const char **vec, unsigned int len)
606 {
607           DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
608           xenbus_probe_frontends();
609 }
610 
611 static void
backend_changed(struct xenbus_watch * watch,const char ** vec,unsigned int len)612 backend_changed(struct xenbus_watch *watch,
613                                   const char **vec, unsigned int len)
614 {
615           DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
616           xenbus_probe_backends();
617 }
618 
619 
620 /* We watch for devices appearing and vanishing. */
621 static struct xenbus_watch fe_watch;
622 
623 static struct xenbus_watch be_watch;
624 
625 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
626 int xenstored_ready = 0;
627 static kmutex_t xenstored_lock;
628 static kcondvar_t xenstored_cv;
629 
630 void
xenbus_probe(void * unused)631 xenbus_probe(void *unused)
632 {
633           struct xenbusdev_attach_args balloon_xa = {
634                     .xa_id = 0,
635                     .xa_type = "balloon"
636           };
637 
638           KASSERT((xenstored_ready > 0));
639 
640           /* Enumerate devices in xenstore. */
641           xenbus_probe_frontends();
642           xenbus_probe_backends();
643 
644           /* Watch for changes. */
645           fe_watch.node_sz = strlen("device") + 1;
646           fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
647           strcpy(fe_watch.node, "device");
648           fe_watch.xbw_callback = frontend_changed;
649           register_xenbus_watch(&fe_watch);
650 
651           be_watch.node_sz = strlen("backend") + 1;
652           be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
653           strcpy(be_watch.node, "backend");
654           be_watch.xbw_callback = backend_changed;
655           register_xenbus_watch(&be_watch);
656 
657           /* attach balloon. */
658           KERNEL_LOCK(1, curlwp);
659           config_found(xenbus_dev, &balloon_xa, xenbus_print,
660               CFARGS_NONE);
661           KERNEL_UNLOCK_ONE(curlwp);
662 
663           shutdown_xenbus_setup();
664 
665           /* Notify others that xenstore is up */
666           //notifier_call_chain(&xenstore_chain, 0, NULL);
667 }
668 
669 void
xb_xenstored_make_ready(void)670 xb_xenstored_make_ready(void)
671 {
672           mutex_enter(&xenstored_lock);
673           xenstored_ready = 1;
674           cv_broadcast(&xenstored_cv);
675           mutex_exit(&xenstored_lock);
676 }
677 
678 static void
xenbus_probe_init(void * unused)679 xenbus_probe_init(void *unused)
680 {
681           int err = 0;
682           bool dom0;
683           vaddr_t page = 0;
684 
685           DPRINTK("");
686 
687           SLIST_INIT(&xenbus_device_list);
688           mutex_init(&xenstored_lock, MUTEX_DEFAULT, IPL_TTY);
689           cv_init(&xenstored_cv, "xsready");
690 
691           /*
692           ** Domain0 doesn't have a store_evtchn or store_mfn yet.
693           */
694           dom0 = xendomain_is_dom0();
695           if (dom0) {
696 #if defined(DOM0OPS)
697                     paddr_t ma;
698                     evtchn_op_t op = { .cmd = 0 };
699 
700                     /* Allocate page. */
701                     page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
702                         UVM_KMF_ZERO | UVM_KMF_WIRED);
703                     if (!page)
704                               panic("can't get xenstore page");
705 
706                     (void)pmap_extract_ma(pmap_kernel(), page, &ma);
707                     xen_start_info.store_mfn = ma >> PAGE_SHIFT;
708                     xenstore_interface = (void *)page;
709 
710                     /* Next allocate a local port which xenstored can bind to */
711                     op.cmd = EVTCHNOP_alloc_unbound;
712                     op.u.alloc_unbound.dom        = DOMID_SELF;
713                     op.u.alloc_unbound.remote_dom = 0;
714 
715                     err = HYPERVISOR_event_channel_op(&op);
716                     if (err) {
717                               aprint_error_dev(xenbus_dev,
718                                         "can't register xenstore event\n");
719                               goto err0;
720                     }
721 
722                     xen_start_info.store_evtchn = op.u.alloc_unbound.port;
723 
724                     DELAY(1000);
725 #else /* DOM0OPS */
726                     panic("dom0 support not compiled in");
727 #endif /* DOM0OPS */
728           }
729 
730 #if NKERNFS > 0
731           /* Publish xenbus and Xenstore info in /kern/xen */
732           xenbus_kernfs_init();
733 #endif
734 
735           /* register event handler */
736           xb_init_comms(xenbus_dev);
737 
738           /* Initialize the interface to xenstore. */
739           err = xs_init(xenbus_dev);
740           if (err) {
741                     aprint_error_dev(xenbus_dev,
742                         "Error initializing xenstore comms: %i\n", err);
743                     goto err0;
744           }
745 
746           if (!dom0) {
747                     xenstored_ready = 1;
748                     xenbus_probe(NULL);
749           }
750 
751           DPRINTK("done");
752           config_pending_decr(xenbus_dev);
753 #ifdef DOM0OPS
754           if (dom0) {
755                     mutex_enter(&xenstored_lock);
756                     while (xenstored_ready == 0) {
757                               cv_wait(&xenstored_cv, &xenstored_lock);
758                               mutex_exit(&xenstored_lock);
759                               xenbus_probe(NULL);
760                               mutex_enter(&xenstored_lock);
761                     }
762                     mutex_exit(&xenstored_lock);
763           }
764 #endif
765           kthread_exit(0);
766 
767 err0:
768           if (page)
769                     uvm_km_free(kernel_map, page, PAGE_SIZE,
770                                         UVM_KMF_ZERO | UVM_KMF_WIRED);
771           kthread_exit(err);
772 }
773 
774 /*
775  * Local variables:
776  *  c-file-style: "linux"
777  *  indent-tabs-mode: t
778  *  c-indent-level: 8
779  *  c-basic-offset: 8
780  *  tab-width: 8
781  * End:
782  */
783