1 /* Xenbus code for netif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/rwsem.h>
22 #include <xen/xenbus.h>
27 #define DPRINTK(fmt, args...) \
28 printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
31 static DECLARE_RWSEM(teardown_sem);
33 static int connect_rings(struct backend_info *);
34 static void connect(struct backend_info *);
35 static void backend_create_netif(struct backend_info *be);
36 static void unregister_hotplug_status_watch(struct backend_info *be);
37 static void netback_disconnect(struct device *, bool);
39 static int netback_remove(struct xenbus_device *dev)
41 struct backend_info *be = dev_get_drvdata(&dev->dev);
43 netback_remove_accelerators(be, dev);
45 netback_disconnect(&dev->dev, true);
50 static void netback_disconnect(struct device *xbdev_dev, bool clear)
52 struct backend_info *be = dev_get_drvdata(xbdev_dev);
54 unregister_hotplug_status_watch(be);
56 kobject_uevent(&xbdev_dev->kobj, KOBJ_OFFLINE);
58 xenbus_rm(XBT_NIL, be->dev->nodename, "hotplug-status");
60 down_write(&teardown_sem);
66 dev_set_drvdata(xbdev_dev, NULL);
67 up_write(&teardown_sem);
71 * Entry point to this code when a new device is created. Allocate the basic
72 * structures and switch to InitWait.
74 static int netback_probe(struct xenbus_device *dev,
75 const struct xenbus_device_id *id)
78 struct xenbus_transaction xbt;
81 struct backend_info *be = kzalloc(sizeof(struct backend_info),
84 xenbus_dev_fatal(dev, -ENOMEM,
85 "allocating backend structure");
90 dev_set_drvdata(&dev->dev, be);
93 if (netbk_copy_skb_mode == NETBK_ALWAYS_COPY_SKB)
97 err = xenbus_transaction_start(&xbt);
99 xenbus_dev_fatal(dev, err, "starting transaction");
103 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
105 message = "writing feature-sg";
106 goto abort_transaction;
109 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
112 message = "writing feature-gso-tcpv4";
113 goto abort_transaction;
116 /* We support rx-copy path. */
117 err = xenbus_printf(xbt, dev->nodename,
118 "feature-rx-copy", "%d", 1);
120 message = "writing feature-rx-copy";
121 goto abort_transaction;
125 * We don't support rx-flip path (except old guests who don't
126 * grok this feature flag).
128 err = xenbus_printf(xbt, dev->nodename,
129 "feature-rx-flip", "%d", 0);
131 message = "writing feature-rx-flip";
132 goto abort_transaction;
135 err = xenbus_transaction_end(xbt, 0);
136 } while (err == -EAGAIN);
139 xenbus_dev_fatal(dev, err, "completing transaction");
143 netback_probe_accelerators(be, dev);
145 err = xenbus_switch_state(dev, XenbusStateInitWait);
149 /* This kicks hotplug scripts, so do it immediately. */
150 backend_create_netif(be);
155 xenbus_transaction_end(xbt, 1);
156 xenbus_dev_fatal(dev, err, "%s", message);
165 * Handle the creation of the hotplug script environment. We add the script
166 * and vif variables to the environment, for the benefit of the vif-* hotplug
169 static int netback_uevent(struct xenbus_device *xdev, struct kobj_uevent_env *env)
171 struct backend_info *be;
174 DPRINTK("netback_uevent");
176 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
178 int err = PTR_ERR(val);
179 xenbus_dev_fatal(xdev, err, "reading script");
183 add_uevent_var(env, "script=%s", val);
186 down_read(&teardown_sem);
187 be = dev_get_drvdata(&xdev->dev);
189 add_uevent_var(env, "vif=%s", be->netif->dev->name);
190 up_read(&teardown_sem);
196 static void backend_create_netif(struct backend_info *be)
200 struct xenbus_device *dev = be->dev;
203 if (be->netif != NULL)
206 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
208 xenbus_dev_fatal(dev, err, "reading handle");
212 netif = netif_alloc(&dev->dev, dev->otherend_id, handle);
214 err = PTR_ERR(netif);
215 xenbus_dev_fatal(dev, err, "creating interface");
220 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
225 * Callback received when the frontend's state changes.
227 static void frontend_changed(struct xenbus_device *dev,
228 enum xenbus_state frontend_state)
230 struct backend_info *be = dev_get_drvdata(&dev->dev);
232 DPRINTK("%s", xenbus_strstate(frontend_state));
234 be->frontend_state = frontend_state;
236 switch (frontend_state) {
237 case XenbusStateInitialising:
238 if (dev->state == XenbusStateClosed) {
239 pr_info("%s: %s: prepare for reconnect\n",
240 __FUNCTION__, dev->nodename);
241 xenbus_switch_state(dev, XenbusStateInitWait);
245 case XenbusStateInitialised:
248 case XenbusStateConnected:
249 if (dev->state == XenbusStateConnected)
252 /* backend_create_netif() is idempotent */
253 backend_create_netif(be);
258 case XenbusStateClosing:
259 netback_disconnect(&dev->dev, false);
260 xenbus_switch_state(dev, XenbusStateClosing);
263 case XenbusStateClosed:
264 xenbus_switch_state(dev, XenbusStateClosed);
265 if (xenbus_dev_is_online(dev))
267 /* fall through if not online */
268 case XenbusStateUnknown:
269 /* implies netback_disconnect() via netback_remove() */
270 device_unregister(&dev->dev);
274 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
281 static void xen_net_read_rate(struct xenbus_device *dev,
282 unsigned long *bytes, unsigned long *usec)
288 /* Default to unlimited bandwidth. */
292 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
297 b = simple_strtoul(s, &e, 10);
298 if ((s == e) || (*e != ','))
302 u = simple_strtoul(s, &e, 10);
303 if ((s == e) || (*e != '\0'))
313 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
317 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
319 char *s, *e, *macstr;
322 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
324 return PTR_ERR(macstr);
326 for (i = 0; i < ETH_ALEN; i++) {
327 mac[i] = simple_strtoul(s, &e, 16);
328 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
339 static void unregister_hotplug_status_watch(struct backend_info *be)
341 if (be->have_hotplug_status_watch) {
342 unregister_xenbus_watch(&be->hotplug_status_watch);
343 kfree(be->hotplug_status_watch.node);
345 be->have_hotplug_status_watch = 0;
348 static void hotplug_status_changed(struct xenbus_watch *watch,
350 unsigned int vec_size)
352 struct backend_info *be = container_of(watch,
354 hotplug_status_watch);
358 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
361 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
362 xenbus_switch_state(be->dev, XenbusStateConnected);
363 /* Not interested in this watch anymore. */
364 unregister_hotplug_status_watch(be);
369 static void connect(struct backend_info *be)
372 struct xenbus_device *dev = be->dev;
374 err = connect_rings(be);
378 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
380 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
384 xen_net_read_rate(dev, &be->netif->credit_bytes,
385 &be->netif->credit_usec);
386 be->netif->remaining_credit = be->netif->credit_bytes;
388 unregister_hotplug_status_watch(be);
389 err = xenbus_watch_path2(dev, dev->nodename, "hotplug-status",
390 &be->hotplug_status_watch,
391 hotplug_status_changed);
393 /* Switch now, since we can't do a watch. */
394 xenbus_switch_state(dev, XenbusStateConnected);
396 be->have_hotplug_status_watch = 1;
399 netif_wake_queue(be->netif->dev);
403 static int connect_rings(struct backend_info *be)
405 netif_t *netif = be->netif;
406 struct xenbus_device *dev = be->dev;
407 unsigned int tx_ring_ref, rx_ring_ref;
408 unsigned int evtchn, rx_copy;
414 err = xenbus_gather(XBT_NIL, dev->otherend,
415 "tx-ring-ref", "%u", &tx_ring_ref,
416 "rx-ring-ref", "%u", &rx_ring_ref,
417 "event-channel", "%u", &evtchn, NULL);
419 xenbus_dev_fatal(dev, err,
420 "reading %s/ring-ref and event-channel",
425 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
427 if (err == -ENOENT) {
432 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
436 netif->copying_receiver = !!rx_copy;
438 if (netif->dev->tx_queue_len != 0) {
439 if (xenbus_scanf(XBT_NIL, dev->otherend,
440 "feature-rx-notify", "%d", &val) < 0)
443 netif->can_queue = 1;
445 /* Must be non-zero for pfifo_fast to work. */
446 netif->dev->tx_queue_len = 1;
449 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
451 netif->can_sg = !!val;
453 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
458 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
463 /* Map the shared frame, irq etc. */
464 err = netif_map(be, tx_ring_ref, rx_ring_ref, evtchn);
466 xenbus_dev_fatal(dev, err,
467 "mapping shared-frames %u/%u port %u",
468 tx_ring_ref, rx_ring_ref, evtchn);
475 /* ** Driver Registration ** */
478 static const struct xenbus_device_id netback_ids[] = {
483 static DEFINE_XENBUS_DRIVER(netback, ,
484 .probe = netback_probe,
485 .remove = netback_remove,
486 .uevent = netback_uevent,
487 .otherend_changed = frontend_changed,
491 void netif_xenbus_init(void)
493 WARN_ON(xenbus_register_backend(&netback_driver));