2 * Xenbus code for netif backend
4 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
5 * Copyright (C) 2005 XenSource Ltd
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 struct xenbus_device *dev;
27 enum xenbus_state frontend_state;
28 struct xenbus_watch hotplug_status_watch;
29 u8 have_hotplug_status_watch:1;
32 static int connect_rings(struct backend_info *);
33 static void connect(struct backend_info *);
34 static void backend_create_xenvif(struct backend_info *be);
35 static void unregister_hotplug_status_watch(struct backend_info *be);
37 static int netback_remove(struct xenbus_device *dev)
39 struct backend_info *be = dev_get_drvdata(&dev->dev);
41 unregister_hotplug_status_watch(be);
43 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
44 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
49 dev_set_drvdata(&dev->dev, NULL);
55 * Entry point to this code when a new device is created. Allocate the basic
56 * structures and switch to InitWait.
58 static int netback_probe(struct xenbus_device *dev,
59 const struct xenbus_device_id *id)
62 struct xenbus_transaction xbt;
65 struct backend_info *be = kzalloc(sizeof(struct backend_info),
68 xenbus_dev_fatal(dev, -ENOMEM,
69 "allocating backend structure");
74 dev_set_drvdata(&dev->dev, be);
79 err = xenbus_transaction_start(&xbt);
81 xenbus_dev_fatal(dev, err, "starting transaction");
85 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
87 message = "writing feature-sg";
88 goto abort_transaction;
91 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
94 message = "writing feature-gso-tcpv4";
95 goto abort_transaction;
98 /* We support rx-copy path. */
99 err = xenbus_printf(xbt, dev->nodename,
100 "feature-rx-copy", "%d", 1);
102 message = "writing feature-rx-copy";
103 goto abort_transaction;
107 * We don't support rx-flip path (except old guests who don't
108 * grok this feature flag).
110 err = xenbus_printf(xbt, dev->nodename,
111 "feature-rx-flip", "%d", 0);
113 message = "writing feature-rx-flip";
114 goto abort_transaction;
117 err = xenbus_transaction_end(xbt, 0);
118 } while (err == -EAGAIN);
121 xenbus_dev_fatal(dev, err, "completing transaction");
126 * Split event channels support, this is optional so it is not
127 * put inside the above loop.
129 err = xenbus_printf(XBT_NIL, dev->nodename,
130 "feature-split-event-channels",
131 "%u", separate_tx_rx_irq);
133 pr_debug("Error writing feature-split-event-channels\n");
135 err = xenbus_switch_state(dev, XenbusStateInitWait);
139 /* This kicks hotplug scripts, so do it immediately. */
140 backend_create_xenvif(be);
145 xenbus_transaction_end(xbt, 1);
146 xenbus_dev_fatal(dev, err, "%s", message);
148 pr_debug("failed\n");
155 * Handle the creation of the hotplug script environment. We add the script
156 * and vif variables to the environment, for the benefit of the vif-* hotplug
159 static int netback_uevent(struct xenbus_device *xdev,
160 struct kobj_uevent_env *env)
162 struct backend_info *be = dev_get_drvdata(&xdev->dev);
165 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
167 int err = PTR_ERR(val);
168 xenbus_dev_fatal(xdev, err, "reading script");
171 if (add_uevent_var(env, "script=%s", val)) {
181 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
185 static void backend_create_xenvif(struct backend_info *be)
189 struct xenbus_device *dev = be->dev;
194 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
196 xenbus_dev_fatal(dev, err, "reading handle");
200 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
201 if (IS_ERR(be->vif)) {
202 err = PTR_ERR(be->vif);
204 xenbus_dev_fatal(dev, err, "creating interface");
208 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
212 static void disconnect_backend(struct xenbus_device *dev)
214 struct backend_info *be = dev_get_drvdata(&dev->dev);
217 xenvif_disconnect(be->vif);
220 static void destroy_backend(struct xenbus_device *dev)
222 struct backend_info *be = dev_get_drvdata(&dev->dev);
225 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
226 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
227 xenvif_free(be->vif);
233 * Callback received when the frontend's state changes.
235 static void frontend_changed(struct xenbus_device *dev,
236 enum xenbus_state frontend_state)
238 struct backend_info *be = dev_get_drvdata(&dev->dev);
240 pr_debug("frontend state %s\n", xenbus_strstate(frontend_state));
242 be->frontend_state = frontend_state;
244 switch (frontend_state) {
245 case XenbusStateInitialising:
246 if (dev->state == XenbusStateClosed) {
247 pr_info("%s: prepare for reconnect\n", dev->nodename);
248 xenbus_switch_state(dev, XenbusStateInitWait);
252 case XenbusStateInitialised:
255 case XenbusStateConnected:
256 if (dev->state == XenbusStateConnected)
262 case XenbusStateClosing:
263 disconnect_backend(dev);
264 xenbus_switch_state(dev, XenbusStateClosing);
267 case XenbusStateClosed:
268 xenbus_switch_state(dev, XenbusStateClosed);
269 if (xenbus_dev_is_online(dev))
271 destroy_backend(dev);
272 /* fall through if not online */
273 case XenbusStateUnknown:
274 device_unregister(&dev->dev);
278 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
285 static void xen_net_read_rate(struct xenbus_device *dev,
286 unsigned long *bytes, unsigned long *usec)
292 /* Default to unlimited bandwidth. */
296 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
301 b = simple_strtoul(s, &e, 10);
302 if ((s == e) || (*e != ','))
306 u = simple_strtoul(s, &e, 10);
307 if ((s == e) || (*e != '\0'))
317 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
321 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
323 char *s, *e, *macstr;
326 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
328 return PTR_ERR(macstr);
330 for (i = 0; i < ETH_ALEN; i++) {
331 mac[i] = simple_strtoul(s, &e, 16);
332 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
343 static void unregister_hotplug_status_watch(struct backend_info *be)
345 if (be->have_hotplug_status_watch) {
346 unregister_xenbus_watch(&be->hotplug_status_watch);
347 kfree(be->hotplug_status_watch.node);
349 be->have_hotplug_status_watch = 0;
352 static void hotplug_status_changed(struct xenbus_watch *watch,
354 unsigned int vec_size)
356 struct backend_info *be = container_of(watch,
358 hotplug_status_watch);
362 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
365 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
366 xenbus_switch_state(be->dev, XenbusStateConnected);
367 /* Not interested in this watch anymore. */
368 unregister_hotplug_status_watch(be);
373 static void connect(struct backend_info *be)
376 struct xenbus_device *dev = be->dev;
378 err = connect_rings(be);
382 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
384 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
388 xen_net_read_rate(dev, &be->vif->credit_bytes,
389 &be->vif->credit_usec);
390 be->vif->remaining_credit = be->vif->credit_bytes;
392 unregister_hotplug_status_watch(be);
393 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
394 hotplug_status_changed,
395 "%s/%s", dev->nodename, "hotplug-status");
397 /* Switch now, since we can't do a watch. */
398 xenbus_switch_state(dev, XenbusStateConnected);
400 be->have_hotplug_status_watch = 1;
403 netif_wake_queue(be->vif->dev);
407 static int connect_rings(struct backend_info *be)
409 struct xenvif *vif = be->vif;
410 struct xenbus_device *dev = be->dev;
411 unsigned long tx_ring_ref, rx_ring_ref;
412 unsigned int tx_evtchn, rx_evtchn, rx_copy;
416 err = xenbus_gather(XBT_NIL, dev->otherend,
417 "tx-ring-ref", "%lu", &tx_ring_ref,
418 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
420 xenbus_dev_fatal(dev, err,
421 "reading %s/ring-ref",
426 /* Try split event channels first, then single event channel. */
427 err = xenbus_gather(XBT_NIL, dev->otherend,
428 "event-channel-tx", "%u", &tx_evtchn,
429 "event-channel-rx", "%u", &rx_evtchn, NULL);
431 err = xenbus_scanf(XBT_NIL, dev->otherend,
432 "event-channel", "%u", &tx_evtchn);
434 xenbus_dev_fatal(dev, err,
435 "reading %s/event-channel(-tx/rx)",
439 rx_evtchn = tx_evtchn;
442 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
444 if (err == -ENOENT) {
449 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
456 if (vif->dev->tx_queue_len != 0) {
457 if (xenbus_scanf(XBT_NIL, dev->otherend,
458 "feature-rx-notify", "%d", &val) < 0)
463 /* Must be non-zero for pfifo_fast to work. */
464 vif->dev->tx_queue_len = 1;
467 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
472 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
477 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
480 vif->gso_prefix = !!val;
482 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
487 /* Map the shared frame, irq etc. */
488 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
489 tx_evtchn, rx_evtchn);
491 xenbus_dev_fatal(dev, err,
492 "mapping shared-frames %lu/%lu port tx %u rx %u",
493 tx_ring_ref, rx_ring_ref,
494 tx_evtchn, rx_evtchn);
501 /* ** Driver Registration ** */
504 static const struct xenbus_device_id netback_ids[] = {
510 static DEFINE_XENBUS_DRIVER(netback, ,
511 .probe = netback_probe,
512 .remove = netback_remove,
513 .uevent = netback_uevent,
514 .otherend_changed = frontend_changed,
517 int xenvif_xenbus_init(void)
519 return xenbus_register_backend(&netback_driver);
522 void xenvif_xenbus_fini(void)
524 return xenbus_unregister_driver(&netback_driver);