1df660251SIan Campbell /******************************************************************************
2df660251SIan Campbell * Talks to Xen Store to figure out what devices we have (backend half).
3df660251SIan Campbell *
4df660251SIan Campbell * Copyright (C) 2005 Rusty Russell, IBM Corporation
5df660251SIan Campbell * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6df660251SIan Campbell * Copyright (C) 2005, 2006 XenSource Ltd
7df660251SIan Campbell * Copyright (C) 2007 Solarflare Communications, Inc.
8df660251SIan Campbell *
9df660251SIan Campbell * This program is free software; you can redistribute it and/or
10df660251SIan Campbell * modify it under the terms of the GNU General Public License version 2
11df660251SIan Campbell * as published by the Free Software Foundation; or, when distributed
12df660251SIan Campbell * separately from the Linux kernel or incorporated into other
13df660251SIan Campbell * software packages, subject to the following license:
14df660251SIan Campbell *
15df660251SIan Campbell * Permission is hereby granted, free of charge, to any person obtaining a copy
16df660251SIan Campbell * of this source file (the "Software"), to deal in the Software without
17df660251SIan Campbell * restriction, including without limitation the rights to use, copy, modify,
18df660251SIan Campbell * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19df660251SIan Campbell * and to permit persons to whom the Software is furnished to do so, subject to
20df660251SIan Campbell * the following conditions:
21df660251SIan Campbell *
22df660251SIan Campbell * The above copyright notice and this permission notice shall be included in
23df660251SIan Campbell * all copies or substantial portions of the Software.
24df660251SIan Campbell *
25df660251SIan Campbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26df660251SIan Campbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27df660251SIan Campbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28df660251SIan Campbell * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29df660251SIan Campbell * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30df660251SIan Campbell * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31df660251SIan Campbell * IN THE SOFTWARE.
32df660251SIan Campbell */
33df660251SIan Campbell
34283c0972SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35283c0972SJoe Perches
36283c0972SJoe Perches #define DPRINTK(fmt, ...) \
37283c0972SJoe Perches pr_debug("(%s:%d) " fmt "\n", \
38283c0972SJoe Perches __func__, __LINE__, ##__VA_ARGS__)
39df660251SIan Campbell
40df660251SIan Campbell #include <linux/kernel.h>
41df660251SIan Campbell #include <linux/err.h>
42df660251SIan Campbell #include <linux/string.h>
43df660251SIan Campbell #include <linux/ctype.h>
44df660251SIan Campbell #include <linux/fcntl.h>
45df660251SIan Campbell #include <linux/mm.h>
46df660251SIan Campbell #include <linux/notifier.h>
4763c9744bSPaul Gortmaker #include <linux/export.h>
482f69a110SJuergen Gross #include <linux/semaphore.h>
49df660251SIan Campbell
50df660251SIan Campbell #include <asm/page.h>
51df660251SIan Campbell #include <asm/xen/hypervisor.h>
52df660251SIan Campbell #include <asm/hypervisor.h>
53df660251SIan Campbell #include <xen/xenbus.h>
54df660251SIan Campbell #include <xen/features.h>
55df660251SIan Campbell
56332f791dSJuergen Gross #include "xenbus.h"
57df660251SIan Campbell
58df660251SIan Campbell /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
backend_bus_id(char bus_id[XEN_BUS_ID_SIZE],const char * nodename)59df660251SIan Campbell static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
60df660251SIan Campbell {
61df660251SIan Campbell int domid, err;
62df660251SIan Campbell const char *devid, *type, *frontend;
63df660251SIan Campbell unsigned int typelen;
64df660251SIan Campbell
65df660251SIan Campbell type = strchr(nodename, '/');
66df660251SIan Campbell if (!type)
67df660251SIan Campbell return -EINVAL;
68df660251SIan Campbell type++;
69df660251SIan Campbell typelen = strcspn(type, "/");
70df660251SIan Campbell if (!typelen || type[typelen] != '/')
71df660251SIan Campbell return -EINVAL;
72df660251SIan Campbell
73df660251SIan Campbell devid = strrchr(nodename, '/') + 1;
74df660251SIan Campbell
75df660251SIan Campbell err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
76df660251SIan Campbell "frontend", NULL, &frontend,
77df660251SIan Campbell NULL);
78df660251SIan Campbell if (err)
79df660251SIan Campbell return err;
80df660251SIan Campbell if (strlen(frontend) == 0)
81df660251SIan Campbell err = -ERANGE;
82df660251SIan Campbell if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
83df660251SIan Campbell err = -ENOENT;
84df660251SIan Campbell kfree(frontend);
85df660251SIan Campbell
86df660251SIan Campbell if (err)
87df660251SIan Campbell return err;
88df660251SIan Campbell
896bac7f9fSIan Campbell if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
906bac7f9fSIan Campbell typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
91df660251SIan Campbell return -ENOSPC;
92df660251SIan Campbell return 0;
93df660251SIan Campbell }
94df660251SIan Campbell
xenbus_uevent_backend(const struct device * dev,struct kobj_uevent_env * env)952a81ada3SGreg Kroah-Hartman static int xenbus_uevent_backend(const struct device *dev,
96df660251SIan Campbell struct kobj_uevent_env *env)
97df660251SIan Campbell {
982a81ada3SGreg Kroah-Hartman const struct xenbus_device *xdev;
992a81ada3SGreg Kroah-Hartman const struct xenbus_driver *drv;
1002a81ada3SGreg Kroah-Hartman const struct xen_bus_type *bus;
101df660251SIan Campbell
102df660251SIan Campbell DPRINTK("");
103df660251SIan Campbell
104df660251SIan Campbell if (dev == NULL)
105df660251SIan Campbell return -ENODEV;
106df660251SIan Campbell
107df660251SIan Campbell xdev = to_xenbus_device(dev);
108df660251SIan Campbell bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
109df660251SIan Campbell
110149bb2faSBastian Blank if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
111149bb2faSBastian Blank return -ENOMEM;
112149bb2faSBastian Blank
113df660251SIan Campbell /* stuff we want to pass to /sbin/hotplug */
114df660251SIan Campbell if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
115df660251SIan Campbell return -ENOMEM;
116df660251SIan Campbell
117df660251SIan Campbell if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
118df660251SIan Campbell return -ENOMEM;
119df660251SIan Campbell
120df660251SIan Campbell if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
121df660251SIan Campbell return -ENOMEM;
122df660251SIan Campbell
123df660251SIan Campbell if (dev->driver) {
124df660251SIan Campbell drv = to_xenbus_driver(dev->driver);
125df660251SIan Campbell if (drv && drv->uevent)
126df660251SIan Campbell return drv->uevent(xdev, env);
127df660251SIan Campbell }
128df660251SIan Campbell
129df660251SIan Campbell return 0;
130df660251SIan Campbell }
131df660251SIan Campbell
132df660251SIan Campbell /* backend/<typename>/<frontend-uuid>/<name> */
xenbus_probe_backend_unit(struct xen_bus_type * bus,const char * dir,const char * type,const char * name)133df660251SIan Campbell static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
134df660251SIan Campbell const char *dir,
135df660251SIan Campbell const char *type,
136df660251SIan Campbell const char *name)
137df660251SIan Campbell {
138df660251SIan Campbell char *nodename;
139df660251SIan Campbell int err;
140df660251SIan Campbell
141df660251SIan Campbell nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
142df660251SIan Campbell if (!nodename)
143df660251SIan Campbell return -ENOMEM;
144df660251SIan Campbell
145df660251SIan Campbell DPRINTK("%s\n", nodename);
146df660251SIan Campbell
147df660251SIan Campbell err = xenbus_probe_node(bus, type, nodename);
148df660251SIan Campbell kfree(nodename);
149df660251SIan Campbell return err;
150df660251SIan Campbell }
151df660251SIan Campbell
152df660251SIan Campbell /* backend/<typename>/<frontend-domid> */
xenbus_probe_backend(struct xen_bus_type * bus,const char * type,const char * domid)1536bac7f9fSIan Campbell static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
1546bac7f9fSIan Campbell const char *domid)
155df660251SIan Campbell {
156df660251SIan Campbell char *nodename;
157df660251SIan Campbell int err = 0;
158df660251SIan Campbell char **dir;
159df660251SIan Campbell unsigned int i, dir_n = 0;
160df660251SIan Campbell
161df660251SIan Campbell DPRINTK("");
162df660251SIan Campbell
163df660251SIan Campbell nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
164df660251SIan Campbell if (!nodename)
165df660251SIan Campbell return -ENOMEM;
166df660251SIan Campbell
167df660251SIan Campbell dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
168df660251SIan Campbell if (IS_ERR(dir)) {
169df660251SIan Campbell kfree(nodename);
170df660251SIan Campbell return PTR_ERR(dir);
171df660251SIan Campbell }
172df660251SIan Campbell
173df660251SIan Campbell for (i = 0; i < dir_n; i++) {
174df660251SIan Campbell err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
175df660251SIan Campbell if (err)
176df660251SIan Campbell break;
177df660251SIan Campbell }
178df660251SIan Campbell kfree(dir);
179df660251SIan Campbell kfree(nodename);
180df660251SIan Campbell return err;
181df660251SIan Campbell }
182df660251SIan Campbell
frontend_will_handle(struct xenbus_watch * watch,const char * path,const char * token)1839996bd49SSeongJae Park static bool frontend_will_handle(struct xenbus_watch *watch,
1849996bd49SSeongJae Park const char *path, const char *token)
1859996bd49SSeongJae Park {
1869996bd49SSeongJae Park return watch->nr_pending == 0;
1879996bd49SSeongJae Park }
1889996bd49SSeongJae Park
frontend_changed(struct xenbus_watch * watch,const char * path,const char * token)189df660251SIan Campbell static void frontend_changed(struct xenbus_watch *watch,
1905584ea25SJuergen Gross const char *path, const char *token)
191df660251SIan Campbell {
1925584ea25SJuergen Gross xenbus_otherend_changed(watch, path, token, 0);
193df660251SIan Campbell }
194df660251SIan Campbell
195df660251SIan Campbell static struct xen_bus_type xenbus_backend = {
196df660251SIan Campbell .root = "backend",
197df660251SIan Campbell .levels = 3, /* backend/type/<frontend>/<id> */
198df660251SIan Campbell .get_bus_id = backend_bus_id,
199df660251SIan Campbell .probe = xenbus_probe_backend,
2009996bd49SSeongJae Park .otherend_will_handle = frontend_will_handle,
201df660251SIan Campbell .otherend_changed = frontend_changed,
202df660251SIan Campbell .bus = {
203df660251SIan Campbell .name = "xen-backend",
204df660251SIan Campbell .match = xenbus_match,
205df660251SIan Campbell .uevent = xenbus_uevent_backend,
206df660251SIan Campbell .probe = xenbus_dev_probe,
207df660251SIan Campbell .remove = xenbus_dev_remove,
20885dd9268SGreg Kroah-Hartman .dev_groups = xenbus_dev_groups,
209df660251SIan Campbell },
210df660251SIan Campbell };
211df660251SIan Campbell
backend_changed(struct xenbus_watch * watch,const char * path,const char * token)212df660251SIan Campbell static void backend_changed(struct xenbus_watch *watch,
2135584ea25SJuergen Gross const char *path, const char *token)
214df660251SIan Campbell {
215df660251SIan Campbell DPRINTK("");
216df660251SIan Campbell
2175584ea25SJuergen Gross xenbus_dev_changed(path, &xenbus_backend);
218df660251SIan Campbell }
219df660251SIan Campbell
220df660251SIan Campbell static struct xenbus_watch be_watch = {
221df660251SIan Campbell .node = "backend",
222df660251SIan Campbell .callback = backend_changed,
223df660251SIan Campbell };
224df660251SIan Campbell
read_frontend_details(struct xenbus_device * xendev)225df660251SIan Campbell static int read_frontend_details(struct xenbus_device *xendev)
226df660251SIan Campbell {
227df660251SIan Campbell return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
228df660251SIan Campbell }
229df660251SIan Campbell
xenbus_dev_is_online(struct xenbus_device * dev)230df660251SIan Campbell int xenbus_dev_is_online(struct xenbus_device *dev)
231df660251SIan Campbell {
232999c9af9SJuergen Gross return !!xenbus_read_unsigned(dev->nodename, "online", 0);
233df660251SIan Campbell }
234df660251SIan Campbell EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
235df660251SIan Campbell
__xenbus_register_backend(struct xenbus_driver * drv,struct module * owner,const char * mod_name)23695afae48SDavid Vrabel int __xenbus_register_backend(struct xenbus_driver *drv, struct module *owner,
23795afae48SDavid Vrabel const char *mod_name)
238df660251SIan Campbell {
239df660251SIan Campbell drv->read_otherend_details = read_frontend_details;
240df660251SIan Campbell
24195afae48SDavid Vrabel return xenbus_register_driver_common(drv, &xenbus_backend,
24295afae48SDavid Vrabel owner, mod_name);
243df660251SIan Campbell }
24495afae48SDavid Vrabel EXPORT_SYMBOL_GPL(__xenbus_register_backend);
245df660251SIan Campbell
backend_probe_and_watch(struct notifier_block * notifier,unsigned long event,void * data)246df660251SIan Campbell static int backend_probe_and_watch(struct notifier_block *notifier,
247df660251SIan Campbell unsigned long event,
248df660251SIan Campbell void *data)
249df660251SIan Campbell {
250df660251SIan Campbell /* Enumerate devices in xenstore and watch for changes. */
251df660251SIan Campbell xenbus_probe_devices(&xenbus_backend);
252df660251SIan Campbell register_xenbus_watch(&be_watch);
253b5d33d08SJeremy Fitzhardinge
254df660251SIan Campbell return NOTIFY_DONE;
255df660251SIan Campbell }
256df660251SIan Campbell
backend_reclaim_memory(struct device * dev,void * data)2578a105678SSeongJae Park static int backend_reclaim_memory(struct device *dev, void *data)
2588a105678SSeongJae Park {
2598a105678SSeongJae Park const struct xenbus_driver *drv;
260060eabe8SSeongJae Park struct xenbus_device *xdev;
2618a105678SSeongJae Park
2628a105678SSeongJae Park if (!dev->driver)
2638a105678SSeongJae Park return 0;
2648a105678SSeongJae Park drv = to_xenbus_driver(dev->driver);
265060eabe8SSeongJae Park if (drv && drv->reclaim_memory) {
266060eabe8SSeongJae Park xdev = to_xenbus_device(dev);
2672f69a110SJuergen Gross if (down_trylock(&xdev->reclaim_sem))
268060eabe8SSeongJae Park return 0;
269060eabe8SSeongJae Park drv->reclaim_memory(xdev);
2702f69a110SJuergen Gross up(&xdev->reclaim_sem);
271060eabe8SSeongJae Park }
2728a105678SSeongJae Park return 0;
2738a105678SSeongJae Park }
2748a105678SSeongJae Park
2758a105678SSeongJae Park /*
2768a105678SSeongJae Park * Returns 0 always because we are using shrinker to only detect memory
2778a105678SSeongJae Park * pressure.
2788a105678SSeongJae Park */
backend_shrink_memory_count(struct shrinker * shrinker,struct shrink_control * sc)2798a105678SSeongJae Park static unsigned long backend_shrink_memory_count(struct shrinker *shrinker,
2808a105678SSeongJae Park struct shrink_control *sc)
2818a105678SSeongJae Park {
2828a105678SSeongJae Park bus_for_each_dev(&xenbus_backend.bus, NULL, NULL,
2838a105678SSeongJae Park backend_reclaim_memory);
2848a105678SSeongJae Park return 0;
2858a105678SSeongJae Park }
2868a105678SSeongJae Park
xenbus_probe_backend_init(void)287df660251SIan Campbell static int __init xenbus_probe_backend_init(void)
288df660251SIan Campbell {
289*1ec016bfSQi Zheng struct shrinker *backend_memory_shrinker;
290df660251SIan Campbell static struct notifier_block xenstore_notifier = {
291df660251SIan Campbell .notifier_call = backend_probe_and_watch
292df660251SIan Campbell };
293df660251SIan Campbell int err;
294df660251SIan Campbell
295df660251SIan Campbell DPRINTK("");
296df660251SIan Campbell
297df660251SIan Campbell /* Register ourselves with the kernel bus subsystem */
298df660251SIan Campbell err = bus_register(&xenbus_backend.bus);
299b5d33d08SJeremy Fitzhardinge if (err)
300df660251SIan Campbell return err;
301df660251SIan Campbell
302df660251SIan Campbell register_xenstore_notifier(&xenstore_notifier);
303df660251SIan Campbell
304*1ec016bfSQi Zheng backend_memory_shrinker = shrinker_alloc(0, "xen-backend");
305*1ec016bfSQi Zheng if (!backend_memory_shrinker) {
306*1ec016bfSQi Zheng pr_warn("shrinker allocation failed\n");
307*1ec016bfSQi Zheng return 0;
308*1ec016bfSQi Zheng }
309*1ec016bfSQi Zheng
310*1ec016bfSQi Zheng backend_memory_shrinker->count_objects = backend_shrink_memory_count;
311*1ec016bfSQi Zheng
312*1ec016bfSQi Zheng shrinker_register(backend_memory_shrinker);
3138a105678SSeongJae Park
314df660251SIan Campbell return 0;
315df660251SIan Campbell }
316c3676e84SJeremy Fitzhardinge subsys_initcall(xenbus_probe_backend_init);
317