ia64/linux-2.6.18-xen.hg

view drivers/xen/netback/xenbus.c @ 820:8b86d11a6eb3

netback: parent sysfs device should be set before registering.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 17 14:21:51 2009 +0000 (2009-03-17)
parents 3aa9b8a7876b
children
line source
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
18 */
20 #include <stdarg.h>
21 #include <linux/module.h>
22 #include <xen/xenbus.h>
23 #include "common.h"
25 #if 0
26 #undef DPRINTK
27 #define DPRINTK(fmt, args...) \
28 printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
29 #endif
32 static int connect_rings(struct backend_info *);
33 static void connect(struct backend_info *);
34 static void backend_create_netif(struct backend_info *be);
36 static int netback_remove(struct xenbus_device *dev)
37 {
38 struct backend_info *be = dev->dev.driver_data;
40 netback_remove_accelerators(be, dev);
42 if (be->netif) {
43 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
44 netif_disconnect(be->netif);
45 be->netif = NULL;
46 }
47 kfree(be);
48 dev->dev.driver_data = NULL;
49 return 0;
50 }
53 /**
54 * Entry point to this code when a new device is created. Allocate the basic
55 * structures and switch to InitWait.
56 */
57 static int netback_probe(struct xenbus_device *dev,
58 const struct xenbus_device_id *id)
59 {
60 const char *message;
61 struct xenbus_transaction xbt;
62 int err;
63 int sg;
64 struct backend_info *be = kzalloc(sizeof(struct backend_info),
65 GFP_KERNEL);
66 if (!be) {
67 xenbus_dev_fatal(dev, -ENOMEM,
68 "allocating backend structure");
69 return -ENOMEM;
70 }
72 be->dev = dev;
73 dev->dev.driver_data = be;
75 sg = 1;
76 if (netbk_copy_skb_mode == NETBK_ALWAYS_COPY_SKB)
77 sg = 0;
79 do {
80 err = xenbus_transaction_start(&xbt);
81 if (err) {
82 xenbus_dev_fatal(dev, err, "starting transaction");
83 goto fail;
84 }
86 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
87 if (err) {
88 message = "writing feature-sg";
89 goto abort_transaction;
90 }
92 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
93 "%d", sg);
94 if (err) {
95 message = "writing feature-gso-tcpv4";
96 goto abort_transaction;
97 }
99 /* We support rx-copy path. */
100 err = xenbus_printf(xbt, dev->nodename,
101 "feature-rx-copy", "%d", 1);
102 if (err) {
103 message = "writing feature-rx-copy";
104 goto abort_transaction;
105 }
107 /*
108 * We don't support rx-flip path (except old guests who don't
109 * grok this feature flag).
110 */
111 err = xenbus_printf(xbt, dev->nodename,
112 "feature-rx-flip", "%d", 0);
113 if (err) {
114 message = "writing feature-rx-flip";
115 goto abort_transaction;
116 }
118 err = xenbus_transaction_end(xbt, 0);
119 } while (err == -EAGAIN);
121 if (err) {
122 xenbus_dev_fatal(dev, err, "completing transaction");
123 goto fail;
124 }
126 netback_probe_accelerators(be, dev);
128 err = xenbus_switch_state(dev, XenbusStateInitWait);
129 if (err)
130 goto fail;
132 /* This kicks hotplug scripts, so do it immediately. */
133 backend_create_netif(be);
135 return 0;
137 abort_transaction:
138 xenbus_transaction_end(xbt, 1);
139 xenbus_dev_fatal(dev, err, "%s", message);
140 fail:
141 DPRINTK("failed");
142 netback_remove(dev);
143 return err;
144 }
147 /**
148 * Handle the creation of the hotplug script environment. We add the script
149 * and vif variables to the environment, for the benefit of the vif-* hotplug
150 * scripts.
151 */
152 static int netback_uevent(struct xenbus_device *xdev, char **envp,
153 int num_envp, char *buffer, int buffer_size)
154 {
155 struct backend_info *be = xdev->dev.driver_data;
156 netif_t *netif = be->netif;
157 int i = 0, length = 0;
158 char *val;
160 DPRINTK("netback_uevent");
162 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
163 if (IS_ERR(val)) {
164 int err = PTR_ERR(val);
165 xenbus_dev_fatal(xdev, err, "reading script");
166 return err;
167 }
168 else {
169 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
170 &length, "script=%s", val);
171 kfree(val);
172 }
174 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
175 "vif=%s", netif->dev->name);
177 envp[i] = NULL;
179 return 0;
180 }
183 static void backend_create_netif(struct backend_info *be)
184 {
185 int err;
186 long handle;
187 struct xenbus_device *dev = be->dev;
189 if (be->netif != NULL)
190 return;
192 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
193 if (err != 1) {
194 xenbus_dev_fatal(dev, err, "reading handle");
195 return;
196 }
198 be->netif = netif_alloc(&dev->dev, dev->otherend_id, handle);
199 if (IS_ERR(be->netif)) {
200 err = PTR_ERR(be->netif);
201 be->netif = NULL;
202 xenbus_dev_fatal(dev, err, "creating interface");
203 return;
204 }
206 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
207 }
210 /**
211 * Callback received when the frontend's state changes.
212 */
213 static void frontend_changed(struct xenbus_device *dev,
214 enum xenbus_state frontend_state)
215 {
216 struct backend_info *be = dev->dev.driver_data;
218 DPRINTK("%s", xenbus_strstate(frontend_state));
220 be->frontend_state = frontend_state;
222 switch (frontend_state) {
223 case XenbusStateInitialising:
224 if (dev->state == XenbusStateClosed) {
225 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
226 __FUNCTION__, dev->nodename);
227 xenbus_switch_state(dev, XenbusStateInitWait);
228 }
229 break;
231 case XenbusStateInitialised:
232 break;
234 case XenbusStateConnected:
235 if (dev->state == XenbusStateConnected)
236 break;
237 backend_create_netif(be);
238 if (be->netif)
239 connect(be);
240 break;
242 case XenbusStateClosing:
243 if (be->netif) {
244 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
245 netif_disconnect(be->netif);
246 be->netif = NULL;
247 }
248 xenbus_switch_state(dev, XenbusStateClosing);
249 break;
251 case XenbusStateClosed:
252 xenbus_switch_state(dev, XenbusStateClosed);
253 if (xenbus_dev_is_online(dev))
254 break;
255 /* fall through if not online */
256 case XenbusStateUnknown:
257 device_unregister(&dev->dev);
258 break;
260 default:
261 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
262 frontend_state);
263 break;
264 }
265 }
268 static void xen_net_read_rate(struct xenbus_device *dev,
269 unsigned long *bytes, unsigned long *usec)
270 {
271 char *s, *e;
272 unsigned long b, u;
273 char *ratestr;
275 /* Default to unlimited bandwidth. */
276 *bytes = ~0UL;
277 *usec = 0;
279 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
280 if (IS_ERR(ratestr))
281 return;
283 s = ratestr;
284 b = simple_strtoul(s, &e, 10);
285 if ((s == e) || (*e != ','))
286 goto fail;
288 s = e + 1;
289 u = simple_strtoul(s, &e, 10);
290 if ((s == e) || (*e != '\0'))
291 goto fail;
293 *bytes = b;
294 *usec = u;
296 kfree(ratestr);
297 return;
299 fail:
300 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
301 kfree(ratestr);
302 }
304 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
305 {
306 char *s, *e, *macstr;
307 int i;
309 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
310 if (IS_ERR(macstr))
311 return PTR_ERR(macstr);
313 for (i = 0; i < ETH_ALEN; i++) {
314 mac[i] = simple_strtoul(s, &e, 16);
315 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
316 kfree(macstr);
317 return -ENOENT;
318 }
319 s = e+1;
320 }
322 kfree(macstr);
323 return 0;
324 }
326 static void connect(struct backend_info *be)
327 {
328 int err;
329 struct xenbus_device *dev = be->dev;
331 err = connect_rings(be);
332 if (err)
333 return;
335 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
336 if (err) {
337 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
338 return;
339 }
341 xen_net_read_rate(dev, &be->netif->credit_bytes,
342 &be->netif->credit_usec);
343 be->netif->remaining_credit = be->netif->credit_bytes;
345 xenbus_switch_state(dev, XenbusStateConnected);
347 netif_wake_queue(be->netif->dev);
348 }
351 static int connect_rings(struct backend_info *be)
352 {
353 struct xenbus_device *dev = be->dev;
354 unsigned long tx_ring_ref, rx_ring_ref;
355 unsigned int evtchn, rx_copy;
356 int err;
357 int val;
359 DPRINTK("");
361 err = xenbus_gather(XBT_NIL, dev->otherend,
362 "tx-ring-ref", "%lu", &tx_ring_ref,
363 "rx-ring-ref", "%lu", &rx_ring_ref,
364 "event-channel", "%u", &evtchn, NULL);
365 if (err) {
366 xenbus_dev_fatal(dev, err,
367 "reading %s/ring-ref and event-channel",
368 dev->otherend);
369 return err;
370 }
372 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
373 &rx_copy);
374 if (err == -ENOENT) {
375 err = 0;
376 rx_copy = 0;
377 }
378 if (err < 0) {
379 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
380 dev->otherend);
381 return err;
382 }
383 be->netif->copying_receiver = !!rx_copy;
385 if (be->netif->dev->tx_queue_len != 0) {
386 if (xenbus_scanf(XBT_NIL, dev->otherend,
387 "feature-rx-notify", "%d", &val) < 0)
388 val = 0;
389 if (val)
390 be->netif->can_queue = 1;
391 else
392 /* Must be non-zero for pfifo_fast to work. */
393 be->netif->dev->tx_queue_len = 1;
394 }
396 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
397 val = 0;
398 if (val) {
399 be->netif->features |= NETIF_F_SG;
400 be->netif->dev->features |= NETIF_F_SG;
401 }
403 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
404 &val) < 0)
405 val = 0;
406 if (val) {
407 be->netif->features |= NETIF_F_TSO;
408 be->netif->dev->features |= NETIF_F_TSO;
409 }
411 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
412 "%d", &val) < 0)
413 val = 0;
414 if (val) {
415 be->netif->features &= ~NETIF_F_IP_CSUM;
416 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
417 }
419 /* Map the shared frame, irq etc. */
420 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
421 if (err) {
422 xenbus_dev_fatal(dev, err,
423 "mapping shared-frames %lu/%lu port %u",
424 tx_ring_ref, rx_ring_ref, evtchn);
425 return err;
426 }
427 return 0;
428 }
431 /* ** Driver Registration ** */
434 static const struct xenbus_device_id netback_ids[] = {
435 { "vif" },
436 { "" }
437 };
440 static struct xenbus_driver netback = {
441 .name = "vif",
442 .owner = THIS_MODULE,
443 .ids = netback_ids,
444 .probe = netback_probe,
445 .remove = netback_remove,
446 .uevent = netback_uevent,
447 .otherend_changed = frontend_changed,
448 };
451 void netif_xenbus_init(void)
452 {
453 xenbus_register_backend(&netback);
454 }