This commit was manufactured by cvs2svn to create branch 'vserver'.
[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_transaction_end(xbt, 0);
112         } while (err == -EAGAIN);
113
114         if (err) {
115                 xenbus_dev_fatal(dev, err, "completing transaction");
116                 goto fail;
117         }
118
119         err = xenbus_switch_state(dev, XenbusStateInitWait);
120         if (err) {
121                 goto fail;
122         }
123
124         return 0;
125
126 abort_transaction:
127         xenbus_transaction_end(xbt, 1);
128         xenbus_dev_fatal(dev, err, "%s", message);
129 fail:
130         DPRINTK("failed");
131         netback_remove(dev);
132         return err;
133 }
134
135
136 /**
137  * Handle the creation of the hotplug script environment.  We add the script
138  * and vif variables to the environment, for the benefit of the vif-* hotplug
139  * scripts.
140  */
141 static int netback_uevent(struct xenbus_device *xdev, char **envp,
142                           int num_envp, char *buffer, int buffer_size)
143 {
144         struct backend_info *be = xdev->dev.driver_data;
145         netif_t *netif = be->netif;
146         int i = 0, length = 0;
147         char *val;
148
149         DPRINTK("netback_uevent");
150
151         val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
152         if (IS_ERR(val)) {
153                 int err = PTR_ERR(val);
154                 xenbus_dev_fatal(xdev, err, "reading script");
155                 return err;
156         }
157         else {
158                 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
159                                &length, "script=%s", val);
160                 kfree(val);
161         }
162
163         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
164                        "vif=%s", netif->dev->name);
165
166         envp[i] = NULL;
167
168         return 0;
169 }
170
171
172 /**
173  * Callback received when the hotplug scripts have placed the handle node.
174  * Read it, and create a netif structure.  If the frontend is ready, connect.
175  */
176 static void backend_changed(struct xenbus_watch *watch,
177                             const char **vec, unsigned int len)
178 {
179         int err;
180         long handle;
181         struct backend_info *be
182                 = container_of(watch, struct backend_info, backend_watch);
183         struct xenbus_device *dev = be->dev;
184
185         DPRINTK("");
186
187         err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
188         if (XENBUS_EXIST_ERR(err)) {
189                 /* Since this watch will fire once immediately after it is
190                    registered, we expect this.  Ignore it, and wait for the
191                    hotplug scripts. */
192                 return;
193         }
194         if (err != 1) {
195                 xenbus_dev_fatal(dev, err, "reading handle");
196                 return;
197         }
198
199         if (be->netif == NULL) {
200                 u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
201
202                 be->netif = netif_alloc(dev->otherend_id, handle, be_mac);
203                 if (IS_ERR(be->netif)) {
204                         err = PTR_ERR(be->netif);
205                         be->netif = NULL;
206                         xenbus_dev_fatal(dev, err, "creating interface");
207                         return;
208                 }
209
210                 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
211
212                 maybe_connect(be);
213         }
214 }
215
216
217 /**
218  * Callback received when the frontend's state changes.
219  */
220 static void frontend_changed(struct xenbus_device *dev,
221                              enum xenbus_state frontend_state)
222 {
223         struct backend_info *be = dev->dev.driver_data;
224
225         DPRINTK("");
226
227         be->frontend_state = frontend_state;
228
229         switch (frontend_state) {
230         case XenbusStateInitialising:
231         case XenbusStateInitialised:
232                 break;
233
234         case XenbusStateConnected:
235                 maybe_connect(be);
236                 break;
237
238         case XenbusStateClosing:
239                 xenbus_switch_state(dev, XenbusStateClosing);
240                 break;
241
242         case XenbusStateClosed:
243                 if (be->netif != NULL)
244                         kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
245                 device_unregister(&dev->dev);
246                 break;
247
248         case XenbusStateUnknown:
249         case XenbusStateInitWait:
250         default:
251                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
252                                  frontend_state);
253                 break;
254         }
255 }
256
257
258 /* ** Connection ** */
259
260
261 static void maybe_connect(struct backend_info *be)
262 {
263         if (be->netif && (be->frontend_state == XenbusStateConnected))
264                 connect(be);
265 }
266
267 static void xen_net_read_rate(struct xenbus_device *dev,
268                               unsigned long *bytes, unsigned long *usec)
269 {
270         char *s, *e;
271         unsigned long b, u;
272         char *ratestr;
273
274         /* Default to unlimited bandwidth. */
275         *bytes = ~0UL;
276         *usec = 0;
277
278         ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
279         if (IS_ERR(ratestr))
280                 return;
281
282         s = ratestr;
283         b = simple_strtoul(s, &e, 10);
284         if ((s == e) || (*e != ','))
285                 goto fail;
286
287         s = e + 1;
288         u = simple_strtoul(s, &e, 10);
289         if ((s == e) || (*e != '\0'))
290                 goto fail;
291
292         *bytes = b;
293         *usec = u;
294
295         kfree(ratestr);
296         return;
297
298  fail:
299         WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
300         kfree(ratestr);
301 }
302
303 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
304 {
305         char *s, *e, *macstr;
306         int i;
307
308         macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
309         if (IS_ERR(macstr))
310                 return PTR_ERR(macstr);
311
312         for (i = 0; i < ETH_ALEN; i++) {
313                 mac[i] = simple_strtoul(s, &e, 16);
314                 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
315                         kfree(macstr);
316                         return -ENOENT;
317                 }
318                 s = e+1;
319         }
320
321         kfree(macstr);
322         return 0;
323 }
324
325 static void connect(struct backend_info *be)
326 {
327         int err;
328         struct xenbus_device *dev = be->dev;
329
330         err = connect_rings(be);
331         if (err)
332                 return;
333
334         err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
335         if (err) {
336                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
337                 return;
338         }
339
340         xen_net_read_rate(dev, &be->netif->credit_bytes,
341                           &be->netif->credit_usec);
342         be->netif->remaining_credit = be->netif->credit_bytes;
343
344         xenbus_switch_state(dev, XenbusStateConnected);
345 }
346
347
348 static int connect_rings(struct backend_info *be)
349 {
350         struct xenbus_device *dev = be->dev;
351         unsigned long tx_ring_ref, rx_ring_ref;
352         unsigned int evtchn;
353         int err;
354         int val;
355
356         DPRINTK("");
357
358         err = xenbus_gather(XBT_NIL, dev->otherend,
359                             "tx-ring-ref", "%lu", &tx_ring_ref,
360                             "rx-ring-ref", "%lu", &rx_ring_ref,
361                             "event-channel", "%u", &evtchn, NULL);
362         if (err) {
363                 xenbus_dev_fatal(dev, err,
364                                  "reading %s/ring-ref and event-channel",
365                                  dev->otherend);
366                 return err;
367         }
368
369         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-rx-notify", "%d",
370                          &val) < 0)
371                 val = 0;
372         if (val)
373                 be->netif->can_queue = 1;
374         else
375                 /* Must be non-zero for pfifo_fast to work. */
376                 be->netif->dev->tx_queue_len = 1;
377
378         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
379                 val = 0;
380         if (val) {
381                 be->netif->features |= NETIF_F_SG;
382                 be->netif->dev->features |= NETIF_F_SG;
383         }
384
385         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
386                          &val) < 0)
387                 val = 0;
388         if (val) {
389                 be->netif->features |= NETIF_F_TSO;
390                 be->netif->dev->features |= NETIF_F_TSO;
391         }
392
393         /* Map the shared frame, irq etc. */
394         err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
395         if (err) {
396                 xenbus_dev_fatal(dev, err,
397                                  "mapping shared-frames %lu/%lu port %u",
398                                  tx_ring_ref, rx_ring_ref, evtchn);
399                 return err;
400         }
401         return 0;
402 }
403
404
405 /* ** Driver Registration ** */
406
407
408 static struct xenbus_device_id netback_ids[] = {
409         { "vif" },
410         { "" }
411 };
412
413
414 static struct xenbus_driver netback = {
415         .name = "vif",
416         .owner = THIS_MODULE,
417         .ids = netback_ids,
418         .probe = netback_probe,
419         .remove = netback_remove,
420         .uevent = netback_uevent,
421         .otherend_changed = frontend_changed,
422 };
423
424
425 void netif_xenbus_init(void)
426 {
427         xenbus_register_backend(&netback);
428 }