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;
28 /* This is the state that will be reflected in xenstore when any
29 * active hotplug script completes.
31 enum xenbus_state state;
33 enum xenbus_state frontend_state;
34 struct xenbus_watch hotplug_status_watch;
35 u8 have_hotplug_status_watch:1;
38 static int connect_rings(struct backend_info *);
39 static void connect(struct backend_info *);
40 static void backend_create_xenvif(struct backend_info *be);
41 static void unregister_hotplug_status_watch(struct backend_info *be);
42 static void set_backend_state(struct backend_info *be,
43 enum xenbus_state state);
45 static int netback_remove(struct xenbus_device *dev)
47 struct backend_info *be = dev_get_drvdata(&dev->dev);
49 set_backend_state(be, XenbusStateClosed);
51 unregister_hotplug_status_watch(be);
53 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
54 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
59 dev_set_drvdata(&dev->dev, NULL);
65 * Entry point to this code when a new device is created. Allocate the basic
66 * structures and switch to InitWait.
68 static int netback_probe(struct xenbus_device *dev,
69 const struct xenbus_device_id *id)
72 struct xenbus_transaction xbt;
75 struct backend_info *be = kzalloc(sizeof(struct backend_info),
78 xenbus_dev_fatal(dev, -ENOMEM,
79 "allocating backend structure");
84 dev_set_drvdata(&dev->dev, be);
89 err = xenbus_transaction_start(&xbt);
91 xenbus_dev_fatal(dev, err, "starting transaction");
95 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
97 message = "writing feature-sg";
98 goto abort_transaction;
101 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
104 message = "writing feature-gso-tcpv4";
105 goto abort_transaction;
108 /* We support rx-copy path. */
109 err = xenbus_printf(xbt, dev->nodename,
110 "feature-rx-copy", "%d", 1);
112 message = "writing feature-rx-copy";
113 goto abort_transaction;
117 * We don't support rx-flip path (except old guests who don't
118 * grok this feature flag).
120 err = xenbus_printf(xbt, dev->nodename,
121 "feature-rx-flip", "%d", 0);
123 message = "writing feature-rx-flip";
124 goto abort_transaction;
127 err = xenbus_transaction_end(xbt, 0);
128 } while (err == -EAGAIN);
131 xenbus_dev_fatal(dev, err, "completing transaction");
135 err = xenbus_switch_state(dev, XenbusStateInitWait);
139 be->state = XenbusStateInitWait;
141 /* This kicks hotplug scripts, so do it immediately. */
142 backend_create_xenvif(be);
147 xenbus_transaction_end(xbt, 1);
148 xenbus_dev_fatal(dev, err, "%s", message);
157 * Handle the creation of the hotplug script environment. We add the script
158 * and vif variables to the environment, for the benefit of the vif-* hotplug
161 static int netback_uevent(struct xenbus_device *xdev,
162 struct kobj_uevent_env *env)
164 struct backend_info *be = dev_get_drvdata(&xdev->dev);
167 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
169 int err = PTR_ERR(val);
170 xenbus_dev_fatal(xdev, err, "reading script");
173 if (add_uevent_var(env, "script=%s", val)) {
183 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
187 static void backend_create_xenvif(struct backend_info *be)
191 struct xenbus_device *dev = be->dev;
196 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
198 xenbus_dev_fatal(dev, err, "reading handle");
202 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
203 if (IS_ERR(be->vif)) {
204 err = PTR_ERR(be->vif);
206 xenbus_dev_fatal(dev, err, "creating interface");
210 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
213 static void backend_disconnect(struct backend_info *be)
216 xenvif_disconnect(be->vif);
219 static void backend_connect(struct backend_info *be)
225 static inline void backend_switch_state(struct backend_info *be,
226 enum xenbus_state state)
228 struct xenbus_device *dev = be->dev;
230 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
233 /* If we are waiting for a hotplug script then defer the
234 * actual xenbus state change.
236 if (!be->have_hotplug_status_watch)
237 xenbus_switch_state(dev, state);
240 /* Handle backend state transitions:
242 * The backend state starts in InitWait and the following transitions are
245 * InitWait -> Connected
257 * The state argument specifies the eventual state of the backend and the
258 * function transitions to that state via the shortest path.
260 static void set_backend_state(struct backend_info *be,
261 enum xenbus_state state)
263 while (be->state != state) {
265 case XenbusStateClosed:
267 case XenbusStateInitWait:
268 case XenbusStateConnected:
269 pr_info("%s: prepare for reconnect\n",
271 backend_switch_state(be, XenbusStateInitWait);
273 case XenbusStateClosing:
274 backend_switch_state(be, XenbusStateClosing);
280 case XenbusStateInitWait:
282 case XenbusStateConnected:
284 backend_switch_state(be, XenbusStateConnected);
286 case XenbusStateClosing:
287 case XenbusStateClosed:
288 backend_switch_state(be, XenbusStateClosing);
294 case XenbusStateConnected:
296 case XenbusStateInitWait:
297 case XenbusStateClosing:
298 case XenbusStateClosed:
299 backend_disconnect(be);
300 backend_switch_state(be, XenbusStateClosing);
306 case XenbusStateClosing:
308 case XenbusStateInitWait:
309 case XenbusStateConnected:
310 case XenbusStateClosed:
311 backend_switch_state(be, XenbusStateClosed);
324 * Callback received when the frontend's state changes.
326 static void frontend_changed(struct xenbus_device *dev,
327 enum xenbus_state frontend_state)
329 struct backend_info *be = dev_get_drvdata(&dev->dev);
331 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
333 be->frontend_state = frontend_state;
335 switch (frontend_state) {
336 case XenbusStateInitialising:
337 set_backend_state(be, XenbusStateInitWait);
340 case XenbusStateInitialised:
343 case XenbusStateConnected:
344 set_backend_state(be, XenbusStateConnected);
347 case XenbusStateClosing:
348 set_backend_state(be, XenbusStateClosing);
351 case XenbusStateClosed:
352 set_backend_state(be, XenbusStateClosed);
353 if (xenbus_dev_is_online(dev))
355 /* fall through if not online */
356 case XenbusStateUnknown:
357 set_backend_state(be, XenbusStateClosed);
358 device_unregister(&dev->dev);
362 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
369 static void xen_net_read_rate(struct xenbus_device *dev,
370 unsigned long *bytes, unsigned long *usec)
376 /* Default to unlimited bandwidth. */
380 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
385 b = simple_strtoul(s, &e, 10);
386 if ((s == e) || (*e != ','))
390 u = simple_strtoul(s, &e, 10);
391 if ((s == e) || (*e != '\0'))
401 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
405 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
407 char *s, *e, *macstr;
410 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
412 return PTR_ERR(macstr);
414 for (i = 0; i < ETH_ALEN; i++) {
415 mac[i] = simple_strtoul(s, &e, 16);
416 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
427 static void unregister_hotplug_status_watch(struct backend_info *be)
429 if (be->have_hotplug_status_watch) {
430 unregister_xenbus_watch(&be->hotplug_status_watch);
431 kfree(be->hotplug_status_watch.node);
433 be->have_hotplug_status_watch = 0;
436 static void hotplug_status_changed(struct xenbus_watch *watch,
438 unsigned int vec_size)
440 struct backend_info *be = container_of(watch,
442 hotplug_status_watch);
446 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
449 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
450 /* Complete any pending state change */
451 xenbus_switch_state(be->dev, be->state);
453 /* Not interested in this watch anymore. */
454 unregister_hotplug_status_watch(be);
459 static void connect(struct backend_info *be)
462 struct xenbus_device *dev = be->dev;
464 err = connect_rings(be);
468 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
470 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
474 xen_net_read_rate(dev, &be->vif->credit_bytes,
475 &be->vif->credit_usec);
476 be->vif->remaining_credit = be->vif->credit_bytes;
478 unregister_hotplug_status_watch(be);
479 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
480 hotplug_status_changed,
481 "%s/%s", dev->nodename, "hotplug-status");
483 be->have_hotplug_status_watch = 1;
485 netif_wake_queue(be->vif->dev);
489 static int connect_rings(struct backend_info *be)
491 struct xenvif *vif = be->vif;
492 struct xenbus_device *dev = be->dev;
493 unsigned long tx_ring_ref, rx_ring_ref;
494 unsigned int evtchn, rx_copy;
498 err = xenbus_gather(XBT_NIL, dev->otherend,
499 "tx-ring-ref", "%lu", &tx_ring_ref,
500 "rx-ring-ref", "%lu", &rx_ring_ref,
501 "event-channel", "%u", &evtchn, NULL);
503 xenbus_dev_fatal(dev, err,
504 "reading %s/ring-ref and event-channel",
509 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
511 if (err == -ENOENT) {
516 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
523 if (vif->dev->tx_queue_len != 0) {
524 if (xenbus_scanf(XBT_NIL, dev->otherend,
525 "feature-rx-notify", "%d", &val) < 0)
530 /* Must be non-zero for pfifo_fast to work. */
531 vif->dev->tx_queue_len = 1;
534 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
539 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
544 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
547 vif->gso_prefix = !!val;
549 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
554 /* Map the shared frame, irq etc. */
555 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref, evtchn);
557 xenbus_dev_fatal(dev, err,
558 "mapping shared-frames %lu/%lu port %u",
559 tx_ring_ref, rx_ring_ref, evtchn);
566 /* ** Driver Registration ** */
569 static const struct xenbus_device_id netback_ids[] = {
575 static DEFINE_XENBUS_DRIVER(netback, ,
576 .probe = netback_probe,
577 .remove = netback_remove,
578 .uevent = netback_uevent,
579 .otherend_changed = frontend_changed,
582 int xenvif_xenbus_init(void)
584 return xenbus_register_backend(&netback_driver);