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;
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
);
55 kobject_uevent(&dev
->dev
.kobj
, KOBJ_OFFLINE
);
56 xenbus_rm(XBT_NIL
, dev
->nodename
, "hotplug-status");
60 kfree(be
->hotplug_script
);
62 dev_set_drvdata(&dev
->dev
, NULL
);
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
)
75 struct xenbus_transaction xbt
;
79 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
82 xenbus_dev_fatal(dev
, -ENOMEM
,
83 "allocating backend structure");
88 dev_set_drvdata(&dev
->dev
, be
);
93 err
= xenbus_transaction_start(&xbt
);
95 xenbus_dev_fatal(dev
, err
, "starting transaction");
99 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", sg
);
101 message
= "writing feature-sg";
102 goto abort_transaction
;
105 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4",
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);
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);
127 message
= "writing feature-rx-flip";
128 goto abort_transaction
;
131 err
= xenbus_transaction_end(xbt
, 0);
132 } while (err
== -EAGAIN
);
135 xenbus_dev_fatal(dev
, err
, "completing transaction");
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
);
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");
156 be
->hotplug_script
= script
;
158 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
162 be
->state
= XenbusStateInitWait
;
164 /* This kicks hotplug scripts, so do it immediately. */
165 backend_create_xenvif(be
);
170 xenbus_transaction_end(xbt
, 1);
171 xenbus_dev_fatal(dev
, err
, "%s", message
);
173 pr_debug("failed\n");
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
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
);
192 if (add_uevent_var(env
, "script=%s", be
->hotplug_script
))
198 return add_uevent_var(env
, "vif=%s", be
->vif
->dev
->name
);
202 static void backend_create_xenvif(struct backend_info
*be
)
206 struct xenbus_device
*dev
= be
->dev
;
211 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "handle", "%li", &handle
);
213 xenbus_dev_fatal(dev
, err
, "reading handle");
217 be
->vif
= xenvif_alloc(&dev
->dev
, dev
->otherend_id
, handle
);
218 if (IS_ERR(be
->vif
)) {
219 err
= PTR_ERR(be
->vif
);
221 xenbus_dev_fatal(dev
, err
, "creating interface");
225 kobject_uevent(&dev
->dev
.kobj
, KOBJ_ONLINE
);
228 static void backend_disconnect(struct backend_info
*be
)
231 xenvif_disconnect(be
->vif
);
234 static void backend_connect(struct backend_info
*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
));
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
260 * InitWait -> Connected
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
) {
280 case XenbusStateClosed
:
282 case XenbusStateInitWait
:
283 case XenbusStateConnected
:
284 pr_info("%s: prepare for reconnect\n",
286 backend_switch_state(be
, XenbusStateInitWait
);
288 case XenbusStateClosing
:
289 backend_switch_state(be
, XenbusStateClosing
);
295 case XenbusStateInitWait
:
297 case XenbusStateConnected
:
299 backend_switch_state(be
, XenbusStateConnected
);
301 case XenbusStateClosing
:
302 case XenbusStateClosed
:
303 backend_switch_state(be
, XenbusStateClosing
);
309 case XenbusStateConnected
:
311 case XenbusStateInitWait
:
312 case XenbusStateClosing
:
313 case XenbusStateClosed
:
314 backend_disconnect(be
);
315 backend_switch_state(be
, XenbusStateClosing
);
321 case XenbusStateClosing
:
323 case XenbusStateInitWait
:
324 case XenbusStateConnected
:
325 case XenbusStateClosed
:
326 backend_switch_state(be
, XenbusStateClosed
);
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
);
355 case XenbusStateInitialised
:
358 case XenbusStateConnected
:
359 set_backend_state(be
, XenbusStateConnected
);
362 case XenbusStateClosing
:
363 set_backend_state(be
, XenbusStateClosing
);
366 case XenbusStateClosed
:
367 set_backend_state(be
, XenbusStateClosed
);
368 if (xenbus_dev_is_online(dev
))
370 /* fall through if not online */
371 case XenbusStateUnknown
:
372 set_backend_state(be
, XenbusStateClosed
);
373 device_unregister(&dev
->dev
);
377 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
384 static void xen_net_read_rate(struct xenbus_device
*dev
,
385 unsigned long *bytes
, unsigned long *usec
)
391 /* Default to unlimited bandwidth. */
395 ratestr
= xenbus_read(XBT_NIL
, dev
->nodename
, "rate", NULL
);
400 b
= simple_strtoul(s
, &e
, 10);
401 if ((s
== e
) || (*e
!= ','))
405 u
= simple_strtoul(s
, &e
, 10);
406 if ((s
== e
) || (*e
!= '\0'))
416 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
420 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
422 char *s
, *e
, *macstr
;
425 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
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' : ':'))) {
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
,
453 unsigned int vec_size
)
455 struct backend_info
*be
= container_of(watch
,
457 hotplug_status_watch
);
461 str
= xenbus_read(XBT_NIL
, be
->dev
->nodename
, "hotplug-status", &len
);
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
);
474 static void connect(struct backend_info
*be
)
477 struct xenbus_device
*dev
= be
->dev
;
479 err
= connect_rings(be
);
483 err
= xen_net_read_mac(dev
, be
->vif
->fe_dev_addr
);
485 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
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");
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
;
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
);
517 xenbus_dev_fatal(dev
, err
,
518 "reading %s/ring-ref",
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
);
528 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
,
529 "event-channel", "%u", &tx_evtchn
);
531 xenbus_dev_fatal(dev
, err
,
532 "reading %s/event-channel(-tx/rx)",
536 rx_evtchn
= tx_evtchn
;
539 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "request-rx-copy", "%u",
541 if (err
== -ENOENT
) {
546 xenbus_dev_fatal(dev
, err
, "reading %s/request-rx-copy",
553 if (vif
->dev
->tx_queue_len
!= 0) {
554 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
555 "feature-rx-notify", "%d", &val
) < 0)
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",
569 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv4",
574 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv4-prefix",
577 vif
->gso_prefix
= !!val
;
579 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-no-csum-offload",
584 /* Map the shared frame, irq etc. */
585 err
= xenvif_connect(vif
, tx_ring_ref
, rx_ring_ref
,
586 tx_evtchn
, rx_evtchn
);
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
);
598 /* ** Driver Registration ** */
601 static const struct xenbus_device_id netback_ids
[] = {
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
);