Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / netback / xenbus.c
1 /*  Xenbus code for netif backend
2     Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3     Copyright (C) 2005 XenSource Ltd
4
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.
9
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.
14
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 */
19
20 #include <stdarg.h>
21 #include <linux/module.h>
22 #include <xen/xenbus.h>
23 #include "common.h"
24
25 #if 0
26 #undef DPRINTK
27 #define DPRINTK(fmt, args...) \
28     printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
29 #endif
30
31 struct backend_info
32 {
33         struct xenbus_device *dev;
34         netif_t *netif;
35         struct xenbus_watch backend_watch;
36         enum xenbus_state frontend_state;
37 };
38
39 static int connect_rings(struct backend_info *);
40 static void connect(struct backend_info *);
41 static void maybe_connect(struct backend_info *);
42 static void backend_changed(struct xenbus_watch *, const char **,
43                             unsigned int);
44
45 static int netback_remove(struct xenbus_device *dev)
46 {
47         struct backend_info *be = dev->dev.driver_data;
48
49         if (be->backend_watch.node) {
50                 unregister_xenbus_watch(&be->backend_watch);
51                 kfree(be->backend_watch.node);
52                 be->backend_watch.node = NULL;
53         }
54         if (be->netif) {
55                 netif_disconnect(be->netif);
56                 be->netif = NULL;
57         }
58         kfree(be);
59         dev->dev.driver_data = NULL;
60         return 0;
61 }
62
63
64 /**
65  * Entry point to this code when a new device is created.  Allocate the basic
66  * structures, and watch the store waiting for the hotplug scripts to tell us
67  * the device's handle.  Switch to InitWait.
68  */
69 static int netback_probe(struct xenbus_device *dev,
70                          const struct xenbus_device_id *id)
71 {
72         const char *message;
73         struct xenbus_transaction xbt;
74         int err;
75         struct backend_info *be = kzalloc(sizeof(struct backend_info),
76                                           GFP_KERNEL);
77         if (!be) {
78                 xenbus_dev_fatal(dev, -ENOMEM,
79                                  "allocating backend structure");
80                 return -ENOMEM;
81         }
82
83         be->dev = dev;
84         dev->dev.driver_data = be;
85
86         err = xenbus_watch_path2(dev, dev->nodename, "handle",
87                                  &be->backend_watch, backend_changed);
88         if (err)
89                 goto fail;
90
91         do {
92                 err = xenbus_transaction_start(&xbt);
93                 if (err) {
94                         xenbus_dev_fatal(dev, err, "starting transaction");
95                         goto fail;
96                 }
97
98                 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
99                 if (err) {
100                         message = "writing feature-sg";
101                         goto abort_transaction;
102                 }
103
104                 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
105                                     "%d", 1);
106                 if (err) {
107                         message = "writing feature-gso-tcpv4";
108                         goto abort_transaction;
109                 }
110
111                 err = xenbus_printf(xbt, dev->nodename, "feature-rx-copy", "%d", 1);
112                 if (err) {
113                         message = "writing feature-copying";
114                         goto abort_transaction;
115                 }
116
117                 err = xenbus_transaction_end(xbt, 0);
118         } while (err == -EAGAIN);
119
120         if (err) {
121                 xenbus_dev_fatal(dev, err, "completing transaction");
122                 goto fail;
123         }
124
125         err = xenbus_switch_state(dev, XenbusStateInitWait);
126         if (err) {
127                 goto fail;
128         }
129
130         return 0;
131
132 abort_transaction:
133         xenbus_transaction_end(xbt, 1);
134         xenbus_dev_fatal(dev, err, "%s", message);
135 fail:
136         DPRINTK("failed");
137         netback_remove(dev);
138         return err;
139 }
140
141
142 /**
143  * Handle the creation of the hotplug script environment.  We add the script
144  * and vif variables to the environment, for the benefit of the vif-* hotplug
145  * scripts.
146  */
147 static int netback_uevent(struct xenbus_device *xdev, char **envp,
148                           int num_envp, char *buffer, int buffer_size)
149 {
150         struct backend_info *be = xdev->dev.driver_data;
151         netif_t *netif = be->netif;
152         int i = 0, length = 0;
153         char *val;
154
155         DPRINTK("netback_uevent");
156
157         val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
158         if (IS_ERR(val)) {
159                 int err = PTR_ERR(val);
160                 xenbus_dev_fatal(xdev, err, "reading script");
161                 return err;
162         }
163         else {
164                 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
165                                &length, "script=%s", val);
166                 kfree(val);
167         }
168
169         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
170                        "vif=%s", netif->dev->name);
171
172         envp[i] = NULL;
173
174         return 0;
175 }
176
177
178 /**
179  * Callback received when the hotplug scripts have placed the handle node.
180  * Read it, and create a netif structure.  If the frontend is ready, connect.
181  */
182 static void backend_changed(struct xenbus_watch *watch,
183                             const char **vec, unsigned int len)
184 {
185         int err;
186         long handle;
187         struct backend_info *be
188                 = container_of(watch, struct backend_info, backend_watch);
189         struct xenbus_device *dev = be->dev;
190
191         DPRINTK("");
192
193         err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
194         if (XENBUS_EXIST_ERR(err)) {
195                 /* Since this watch will fire once immediately after it is
196                    registered, we expect this.  Ignore it, and wait for the
197                    hotplug scripts. */
198                 return;
199         }
200         if (err != 1) {
201                 xenbus_dev_fatal(dev, err, "reading handle");
202                 return;
203         }
204
205         if (be->netif == NULL) {
206                 u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
207
208                 be->netif = netif_alloc(dev->otherend_id, handle, be_mac);
209                 if (IS_ERR(be->netif)) {
210                         err = PTR_ERR(be->netif);
211                         be->netif = NULL;
212                         xenbus_dev_fatal(dev, err, "creating interface");
213                         return;
214                 }
215
216                 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
217
218                 maybe_connect(be);
219         }
220 }
221
222
223 /**
224  * Callback received when the frontend's state changes.
225  */
226 static void frontend_changed(struct xenbus_device *dev,
227                              enum xenbus_state frontend_state)
228 {
229         struct backend_info *be = dev->dev.driver_data;
230
231         DPRINTK("%s", xenbus_strstate(frontend_state));
232
233         be->frontend_state = frontend_state;
234
235         switch (frontend_state) {
236         case XenbusStateInitialising:
237                 if (dev->state == XenbusStateClosed) {
238                         printk("%s: %s: prepare for reconnect\n",
239                                __FUNCTION__, dev->nodename);
240                         if (be->netif) {
241                                 netif_disconnect(be->netif);
242                                 be->netif = NULL;
243                         }
244                         xenbus_switch_state(dev, XenbusStateInitWait);
245                 }
246                 break;
247
248         case XenbusStateInitialised:
249                 break;
250
251         case XenbusStateConnected:
252                 if (!be->netif) {
253                         /* reconnect: setup be->netif */
254                         backend_changed(&be->backend_watch, NULL, 0);
255                 }
256                 maybe_connect(be);
257                 break;
258
259         case XenbusStateClosing:
260                 xenbus_switch_state(dev, XenbusStateClosing);
261                 break;
262
263         case XenbusStateClosed:
264                 xenbus_switch_state(dev, XenbusStateClosed);
265                 if (xenbus_dev_is_online(dev))
266                         break;
267                 /* fall through if not online */
268         case XenbusStateUnknown:
269                 if (be->netif != NULL)
270                         kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
271                 device_unregister(&dev->dev);
272                 break;
273
274         default:
275                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
276                                  frontend_state);
277                 break;
278         }
279 }
280
281
282 /* ** Connection ** */
283
284
285 static void maybe_connect(struct backend_info *be)
286 {
287         if (be->netif && (be->frontend_state == XenbusStateConnected))
288                 connect(be);
289 }
290
291 static void xen_net_read_rate(struct xenbus_device *dev,
292                               unsigned long *bytes, unsigned long *usec)
293 {
294         char *s, *e;
295         unsigned long b, u;
296         char *ratestr;
297
298         /* Default to unlimited bandwidth. */
299         *bytes = ~0UL;
300         *usec = 0;
301
302         ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
303         if (IS_ERR(ratestr))
304                 return;
305
306         s = ratestr;
307         b = simple_strtoul(s, &e, 10);
308         if ((s == e) || (*e != ','))
309                 goto fail;
310
311         s = e + 1;
312         u = simple_strtoul(s, &e, 10);
313         if ((s == e) || (*e != '\0'))
314                 goto fail;
315
316         *bytes = b;
317         *usec = u;
318
319         kfree(ratestr);
320         return;
321
322  fail:
323         WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
324         kfree(ratestr);
325 }
326
327 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
328 {
329         char *s, *e, *macstr;
330         int i;
331
332         macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
333         if (IS_ERR(macstr))
334                 return PTR_ERR(macstr);
335
336         for (i = 0; i < ETH_ALEN; i++) {
337                 mac[i] = simple_strtoul(s, &e, 16);
338                 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
339                         kfree(macstr);
340                         return -ENOENT;
341                 }
342                 s = e+1;
343         }
344
345         kfree(macstr);
346         return 0;
347 }
348
349 static void connect(struct backend_info *be)
350 {
351         int err;
352         struct xenbus_device *dev = be->dev;
353
354         err = connect_rings(be);
355         if (err)
356                 return;
357
358         err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
359         if (err) {
360                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
361                 return;
362         }
363
364         xen_net_read_rate(dev, &be->netif->credit_bytes,
365                           &be->netif->credit_usec);
366         be->netif->remaining_credit = be->netif->credit_bytes;
367
368         xenbus_switch_state(dev, XenbusStateConnected);
369
370         /* May not get a kick from the frontend, so start the tx_queue now. */
371         if (!netbk_can_queue(be->netif->dev))
372                 netif_start_queue(be->netif->dev);
373 }
374
375
376 static int connect_rings(struct backend_info *be)
377 {
378         struct xenbus_device *dev = be->dev;
379         unsigned long tx_ring_ref, rx_ring_ref;
380         unsigned int evtchn, rx_copy;
381         int err;
382         int val;
383
384         DPRINTK("");
385
386         err = xenbus_gather(XBT_NIL, dev->otherend,
387                             "tx-ring-ref", "%lu", &tx_ring_ref,
388                             "rx-ring-ref", "%lu", &rx_ring_ref,
389                             "event-channel", "%u", &evtchn, NULL);
390         if (err) {
391                 xenbus_dev_fatal(dev, err,
392                                  "reading %s/ring-ref and event-channel",
393                                  dev->otherend);
394                 return err;
395         }
396
397         err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
398                            &rx_copy);
399         if (err == -ENOENT) {
400                 err = 0;
401                 rx_copy = 0;
402         }
403         if (err < 0) {
404                 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
405                                  dev->otherend);
406                 return err;
407         }
408         be->netif->copying_receiver = !!rx_copy;
409
410         if (be->netif->dev->tx_queue_len != 0) {
411                 if (xenbus_scanf(XBT_NIL, dev->otherend,
412                                  "feature-rx-notify", "%d", &val) < 0)
413                         val = 0;
414                 if (val)
415                         be->netif->can_queue = 1;
416                 else
417                         /* Must be non-zero for pfifo_fast to work. */
418                         be->netif->dev->tx_queue_len = 1;
419         }
420
421         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
422                 val = 0;
423         if (val) {
424                 be->netif->features |= NETIF_F_SG;
425                 be->netif->dev->features |= NETIF_F_SG;
426         }
427
428         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
429                          &val) < 0)
430                 val = 0;
431         if (val) {
432                 be->netif->features |= NETIF_F_TSO;
433                 be->netif->dev->features |= NETIF_F_TSO;
434         }
435
436         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
437                          "%d", &val) < 0)
438                 val = 0;
439         if (val) {
440                 be->netif->features &= ~NETIF_F_IP_CSUM;
441                 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
442         }
443
444         /* Map the shared frame, irq etc. */
445         err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
446         if (err) {
447                 xenbus_dev_fatal(dev, err,
448                                  "mapping shared-frames %lu/%lu port %u",
449                                  tx_ring_ref, rx_ring_ref, evtchn);
450                 return err;
451         }
452         return 0;
453 }
454
455
456 /* ** Driver Registration ** */
457
458
459 static struct xenbus_device_id netback_ids[] = {
460         { "vif" },
461         { "" }
462 };
463
464
465 static struct xenbus_driver netback = {
466         .name = "vif",
467         .owner = THIS_MODULE,
468         .ids = netback_ids,
469         .probe = netback_probe,
470         .remove = netback_remove,
471         .uevent = netback_uevent,
472         .otherend_changed = frontend_changed,
473 };
474
475
476 void netif_xenbus_init(void)
477 {
478         xenbus_register_backend(&netback);
479 }