Linux 3.12.49
[linux/fpc-iii.git] / drivers / net / xen-netback / xenbus.c
blob659a6f2abb67b4dc5f442c26c0fd79a1d3c6ea3a
1 /*
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
22 #include "common.h"
24 struct backend_info {
25 struct xenbus_device *dev;
26 struct xenvif *vif;
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;
37 const char *hotplug_script;
40 static int connect_rings(struct backend_info *);
41 static void connect(struct backend_info *);
42 static void backend_create_xenvif(struct backend_info *be);
43 static void unregister_hotplug_status_watch(struct backend_info *be);
44 static void set_backend_state(struct backend_info *be,
45 enum xenbus_state state);
47 static int netback_remove(struct xenbus_device *dev)
49 struct backend_info *be = dev_get_drvdata(&dev->dev);
51 set_backend_state(be, XenbusStateClosed);
53 unregister_hotplug_status_watch(be);
54 if (be->vif) {
55 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
56 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
57 xenvif_free(be->vif);
58 be->vif = NULL;
60 kfree(be->hotplug_script);
61 kfree(be);
62 dev_set_drvdata(&dev->dev, NULL);
63 return 0;
67 /**
68 * Entry point to this code when a new device is created. Allocate the basic
69 * structures and switch to InitWait.
71 static int netback_probe(struct xenbus_device *dev,
72 const struct xenbus_device_id *id)
74 const char *message;
75 struct xenbus_transaction xbt;
76 int err;
77 int sg;
78 const char *script;
79 struct backend_info *be = kzalloc(sizeof(struct backend_info),
80 GFP_KERNEL);
81 if (!be) {
82 xenbus_dev_fatal(dev, -ENOMEM,
83 "allocating backend structure");
84 return -ENOMEM;
87 be->dev = dev;
88 dev_set_drvdata(&dev->dev, be);
90 sg = 1;
92 do {
93 err = xenbus_transaction_start(&xbt);
94 if (err) {
95 xenbus_dev_fatal(dev, err, "starting transaction");
96 goto fail;
99 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
100 if (err) {
101 message = "writing feature-sg";
102 goto abort_transaction;
105 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
106 "%d", sg);
107 if (err) {
108 message = "writing feature-gso-tcpv4";
109 goto abort_transaction;
112 /* We support rx-copy path. */
113 err = xenbus_printf(xbt, dev->nodename,
114 "feature-rx-copy", "%d", 1);
115 if (err) {
116 message = "writing feature-rx-copy";
117 goto abort_transaction;
121 * We don't support rx-flip path (except old guests who don't
122 * grok this feature flag).
124 err = xenbus_printf(xbt, dev->nodename,
125 "feature-rx-flip", "%d", 0);
126 if (err) {
127 message = "writing feature-rx-flip";
128 goto abort_transaction;
131 err = xenbus_transaction_end(xbt, 0);
132 } while (err == -EAGAIN);
134 if (err) {
135 xenbus_dev_fatal(dev, err, "completing transaction");
136 goto fail;
140 * Split event channels support, this is optional so it is not
141 * put inside the above loop.
143 err = xenbus_printf(XBT_NIL, dev->nodename,
144 "feature-split-event-channels",
145 "%u", separate_tx_rx_irq);
146 if (err)
147 pr_debug("Error writing feature-split-event-channels\n");
149 script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
150 if (IS_ERR(script)) {
151 err = PTR_ERR(script);
152 xenbus_dev_fatal(dev, err, "reading script");
153 goto fail;
156 be->hotplug_script = script;
158 err = xenbus_switch_state(dev, XenbusStateInitWait);
159 if (err)
160 goto fail;
162 be->state = XenbusStateInitWait;
164 /* This kicks hotplug scripts, so do it immediately. */
165 backend_create_xenvif(be);
167 return 0;
169 abort_transaction:
170 xenbus_transaction_end(xbt, 1);
171 xenbus_dev_fatal(dev, err, "%s", message);
172 fail:
173 pr_debug("failed\n");
174 netback_remove(dev);
175 return err;
180 * Handle the creation of the hotplug script environment. We add the script
181 * and vif variables to the environment, for the benefit of the vif-* hotplug
182 * scripts.
184 static int netback_uevent(struct xenbus_device *xdev,
185 struct kobj_uevent_env *env)
187 struct backend_info *be = dev_get_drvdata(&xdev->dev);
189 if (!be)
190 return 0;
192 if (add_uevent_var(env, "script=%s", be->hotplug_script))
193 return -ENOMEM;
195 if (!be->vif)
196 return 0;
198 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
202 static void backend_create_xenvif(struct backend_info *be)
204 int err;
205 long handle;
206 struct xenbus_device *dev = be->dev;
208 if (be->vif != NULL)
209 return;
211 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
212 if (err != 1) {
213 xenbus_dev_fatal(dev, err, "reading handle");
214 return;
217 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
218 if (IS_ERR(be->vif)) {
219 err = PTR_ERR(be->vif);
220 be->vif = NULL;
221 xenbus_dev_fatal(dev, err, "creating interface");
222 return;
225 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
228 static void backend_disconnect(struct backend_info *be)
230 if (be->vif)
231 xenvif_disconnect(be->vif);
234 static void backend_connect(struct backend_info *be)
236 if (be->vif)
237 connect(be);
240 static inline void backend_switch_state(struct backend_info *be,
241 enum xenbus_state state)
243 struct xenbus_device *dev = be->dev;
245 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
246 be->state = state;
248 /* If we are waiting for a hotplug script then defer the
249 * actual xenbus state change.
251 if (!be->have_hotplug_status_watch)
252 xenbus_switch_state(dev, state);
255 /* Handle backend state transitions:
257 * The backend state starts in InitWait and the following transitions are
258 * allowed.
260 * InitWait -> Connected
262 * ^ \ |
263 * | \ |
264 * | \ |
265 * | \ |
266 * | \ |
267 * | \ |
268 * | V V
270 * Closed <-> Closing
272 * The state argument specifies the eventual state of the backend and the
273 * function transitions to that state via the shortest path.
275 static void set_backend_state(struct backend_info *be,
276 enum xenbus_state state)
278 while (be->state != state) {
279 switch (be->state) {
280 case XenbusStateClosed:
281 switch (state) {
282 case XenbusStateInitWait:
283 case XenbusStateConnected:
284 pr_info("%s: prepare for reconnect\n",
285 be->dev->nodename);
286 backend_switch_state(be, XenbusStateInitWait);
287 break;
288 case XenbusStateClosing:
289 backend_switch_state(be, XenbusStateClosing);
290 break;
291 default:
292 BUG();
294 break;
295 case XenbusStateInitWait:
296 switch (state) {
297 case XenbusStateConnected:
298 backend_connect(be);
299 backend_switch_state(be, XenbusStateConnected);
300 break;
301 case XenbusStateClosing:
302 case XenbusStateClosed:
303 backend_switch_state(be, XenbusStateClosing);
304 break;
305 default:
306 BUG();
308 break;
309 case XenbusStateConnected:
310 switch (state) {
311 case XenbusStateInitWait:
312 case XenbusStateClosing:
313 case XenbusStateClosed:
314 backend_disconnect(be);
315 backend_switch_state(be, XenbusStateClosing);
316 break;
317 default:
318 BUG();
320 break;
321 case XenbusStateClosing:
322 switch (state) {
323 case XenbusStateInitWait:
324 case XenbusStateConnected:
325 case XenbusStateClosed:
326 backend_switch_state(be, XenbusStateClosed);
327 break;
328 default:
329 BUG();
331 break;
332 default:
333 BUG();
339 * Callback received when the frontend's state changes.
341 static void frontend_changed(struct xenbus_device *dev,
342 enum xenbus_state frontend_state)
344 struct backend_info *be = dev_get_drvdata(&dev->dev);
346 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
348 be->frontend_state = frontend_state;
350 switch (frontend_state) {
351 case XenbusStateInitialising:
352 set_backend_state(be, XenbusStateInitWait);
353 break;
355 case XenbusStateInitialised:
356 break;
358 case XenbusStateConnected:
359 set_backend_state(be, XenbusStateConnected);
360 break;
362 case XenbusStateClosing:
363 set_backend_state(be, XenbusStateClosing);
364 break;
366 case XenbusStateClosed:
367 set_backend_state(be, XenbusStateClosed);
368 if (xenbus_dev_is_online(dev))
369 break;
370 /* fall through if not online */
371 case XenbusStateUnknown:
372 set_backend_state(be, XenbusStateClosed);
373 device_unregister(&dev->dev);
374 break;
376 default:
377 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
378 frontend_state);
379 break;
384 static void xen_net_read_rate(struct xenbus_device *dev,
385 unsigned long *bytes, unsigned long *usec)
387 char *s, *e;
388 unsigned long b, u;
389 char *ratestr;
391 /* Default to unlimited bandwidth. */
392 *bytes = ~0UL;
393 *usec = 0;
395 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
396 if (IS_ERR(ratestr))
397 return;
399 s = ratestr;
400 b = simple_strtoul(s, &e, 10);
401 if ((s == e) || (*e != ','))
402 goto fail;
404 s = e + 1;
405 u = simple_strtoul(s, &e, 10);
406 if ((s == e) || (*e != '\0'))
407 goto fail;
409 *bytes = b;
410 *usec = u;
412 kfree(ratestr);
413 return;
415 fail:
416 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
417 kfree(ratestr);
420 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
422 char *s, *e, *macstr;
423 int i;
425 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
426 if (IS_ERR(macstr))
427 return PTR_ERR(macstr);
429 for (i = 0; i < ETH_ALEN; i++) {
430 mac[i] = simple_strtoul(s, &e, 16);
431 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
432 kfree(macstr);
433 return -ENOENT;
435 s = e+1;
438 kfree(macstr);
439 return 0;
442 static void unregister_hotplug_status_watch(struct backend_info *be)
444 if (be->have_hotplug_status_watch) {
445 unregister_xenbus_watch(&be->hotplug_status_watch);
446 kfree(be->hotplug_status_watch.node);
448 be->have_hotplug_status_watch = 0;
451 static void hotplug_status_changed(struct xenbus_watch *watch,
452 const char **vec,
453 unsigned int vec_size)
455 struct backend_info *be = container_of(watch,
456 struct backend_info,
457 hotplug_status_watch);
458 char *str;
459 unsigned int len;
461 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
462 if (IS_ERR(str))
463 return;
464 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
465 /* Complete any pending state change */
466 xenbus_switch_state(be->dev, be->state);
468 /* Not interested in this watch anymore. */
469 unregister_hotplug_status_watch(be);
471 kfree(str);
474 static void connect(struct backend_info *be)
476 int err;
477 struct xenbus_device *dev = be->dev;
479 err = connect_rings(be);
480 if (err)
481 return;
483 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
484 if (err) {
485 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
486 return;
489 xen_net_read_rate(dev, &be->vif->credit_bytes,
490 &be->vif->credit_usec);
491 be->vif->remaining_credit = be->vif->credit_bytes;
493 unregister_hotplug_status_watch(be);
494 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
495 hotplug_status_changed,
496 "%s/%s", dev->nodename, "hotplug-status");
497 if (!err)
498 be->have_hotplug_status_watch = 1;
500 netif_wake_queue(be->vif->dev);
504 static int connect_rings(struct backend_info *be)
506 struct xenvif *vif = be->vif;
507 struct xenbus_device *dev = be->dev;
508 unsigned long tx_ring_ref, rx_ring_ref;
509 unsigned int tx_evtchn, rx_evtchn, rx_copy;
510 int err;
511 int val;
513 err = xenbus_gather(XBT_NIL, dev->otherend,
514 "tx-ring-ref", "%lu", &tx_ring_ref,
515 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
516 if (err) {
517 xenbus_dev_fatal(dev, err,
518 "reading %s/ring-ref",
519 dev->otherend);
520 return err;
523 /* Try split event channels first, then single event channel. */
524 err = xenbus_gather(XBT_NIL, dev->otherend,
525 "event-channel-tx", "%u", &tx_evtchn,
526 "event-channel-rx", "%u", &rx_evtchn, NULL);
527 if (err < 0) {
528 err = xenbus_scanf(XBT_NIL, dev->otherend,
529 "event-channel", "%u", &tx_evtchn);
530 if (err < 0) {
531 xenbus_dev_fatal(dev, err,
532 "reading %s/event-channel(-tx/rx)",
533 dev->otherend);
534 return err;
536 rx_evtchn = tx_evtchn;
539 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
540 &rx_copy);
541 if (err == -ENOENT) {
542 err = 0;
543 rx_copy = 0;
545 if (err < 0) {
546 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
547 dev->otherend);
548 return err;
550 if (!rx_copy)
551 return -EOPNOTSUPP;
553 if (vif->dev->tx_queue_len != 0) {
554 if (xenbus_scanf(XBT_NIL, dev->otherend,
555 "feature-rx-notify", "%d", &val) < 0)
556 val = 0;
557 if (val)
558 vif->can_queue = 1;
559 else
560 /* Must be non-zero for pfifo_fast to work. */
561 vif->dev->tx_queue_len = 1;
564 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
565 "%d", &val) < 0)
566 val = 0;
567 vif->can_sg = !!val;
569 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
570 "%d", &val) < 0)
571 val = 0;
572 vif->gso = !!val;
574 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
575 "%d", &val) < 0)
576 val = 0;
577 vif->gso_prefix = !!val;
579 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
580 "%d", &val) < 0)
581 val = 0;
582 vif->csum = !val;
584 /* Map the shared frame, irq etc. */
585 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
586 tx_evtchn, rx_evtchn);
587 if (err) {
588 xenbus_dev_fatal(dev, err,
589 "mapping shared-frames %lu/%lu port tx %u rx %u",
590 tx_ring_ref, rx_ring_ref,
591 tx_evtchn, rx_evtchn);
592 return err;
594 return 0;
598 /* ** Driver Registration ** */
601 static const struct xenbus_device_id netback_ids[] = {
602 { "vif" },
603 { "" }
607 static DEFINE_XENBUS_DRIVER(netback, ,
608 .probe = netback_probe,
609 .remove = netback_remove,
610 .uevent = netback_uevent,
611 .otherend_changed = frontend_changed,
614 int xenvif_xenbus_init(void)
616 return xenbus_register_backend(&netback_driver);
619 void xenvif_xenbus_fini(void)
621 return xenbus_unregister_driver(&netback_driver);