- Updated to 2.6.22-rc2-git7:
[linux-flexiantxendom0-3.2.10.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         struct xenbus_device *dev;
33         netif_t *netif;
34         enum xenbus_state frontend_state;
35 };
36
37 static int connect_rings(struct backend_info *);
38 static void connect(struct backend_info *);
39 static void backend_create_netif(struct backend_info *be);
40
41 static int netback_remove(struct xenbus_device *dev)
42 {
43         struct backend_info *be = dev->dev.driver_data;
44
45         if (be->netif) {
46                 netif_disconnect(be->netif);
47                 be->netif = NULL;
48         }
49         kfree(be);
50         dev->dev.driver_data = NULL;
51         return 0;
52 }
53
54
55 /**
56  * Entry point to this code when a new device is created.  Allocate the basic
57  * structures and switch to InitWait.
58  */
59 static int netback_probe(struct xenbus_device *dev,
60                          const struct xenbus_device_id *id)
61 {
62         const char *message;
63         struct xenbus_transaction xbt;
64         int err;
65         struct backend_info *be = kzalloc(sizeof(struct backend_info),
66                                           GFP_KERNEL);
67         if (!be) {
68                 xenbus_dev_fatal(dev, -ENOMEM,
69                                  "allocating backend structure");
70                 return -ENOMEM;
71         }
72
73         be->dev = dev;
74         dev->dev.driver_data = be;
75
76         do {
77                 err = xenbus_transaction_start(&xbt);
78                 if (err) {
79                         xenbus_dev_fatal(dev, err, "starting transaction");
80                         goto fail;
81                 }
82
83                 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
84                 if (err) {
85                         message = "writing feature-sg";
86                         goto abort_transaction;
87                 }
88
89                 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
90                                     "%d", 1);
91                 if (err) {
92                         message = "writing feature-gso-tcpv4";
93                         goto abort_transaction;
94                 }
95
96                 /* We support rx-copy path. */
97                 err = xenbus_printf(xbt, dev->nodename,
98                                     "feature-rx-copy", "%d", 1);
99                 if (err) {
100                         message = "writing feature-rx-copy";
101                         goto abort_transaction;
102                 }
103
104                 /*
105                  * We don't support rx-flip path (except old guests who don't
106                  * grok this feature flag).
107                  */
108                 err = xenbus_printf(xbt, dev->nodename,
109                                     "feature-rx-flip", "%d", 0);
110                 if (err) {
111                         message = "writing feature-rx-flip";
112                         goto abort_transaction;
113                 }
114
115                 err = xenbus_transaction_end(xbt, 0);
116         } while (err == -EAGAIN);
117
118         if (err) {
119                 xenbus_dev_fatal(dev, err, "completing transaction");
120                 goto fail;
121         }
122
123         err = xenbus_switch_state(dev, XenbusStateInitWait);
124         if (err)
125                 goto fail;
126
127         /* This kicks hotplug scripts, so do it immediately. */
128         backend_create_netif(be);
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 static void backend_create_netif(struct backend_info *be)
179 {
180         int err;
181         long handle;
182         struct xenbus_device *dev = be->dev;
183
184         if (be->netif != NULL)
185                 return;
186
187         err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
188         if (err != 1) {
189                 xenbus_dev_fatal(dev, err, "reading handle");
190                 return;
191         }
192
193         be->netif = netif_alloc(dev->otherend_id, handle);
194         if (IS_ERR(be->netif)) {
195                 err = PTR_ERR(be->netif);
196                 be->netif = NULL;
197                 xenbus_dev_fatal(dev, err, "creating interface");
198                 return;
199         }
200
201         kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
202 }
203
204
205 /**
206  * Callback received when the frontend's state changes.
207  */
208 static void frontend_changed(struct xenbus_device *dev,
209                              enum xenbus_state frontend_state)
210 {
211         struct backend_info *be = dev->dev.driver_data;
212
213         DPRINTK("%s", xenbus_strstate(frontend_state));
214
215         be->frontend_state = frontend_state;
216
217         switch (frontend_state) {
218         case XenbusStateInitialising:
219                 if (dev->state == XenbusStateClosed) {
220                         printk(KERN_INFO "%s: %s: prepare for reconnect\n",
221                                __FUNCTION__, dev->nodename);
222                         if (be->netif) {
223                                 netif_disconnect(be->netif);
224                                 be->netif = NULL;
225                         }
226                         xenbus_switch_state(dev, XenbusStateInitWait);
227                 }
228                 break;
229
230         case XenbusStateInitialised:
231                 break;
232
233         case XenbusStateConnected:
234                 backend_create_netif(be);
235                 if (be->netif)
236                         connect(be);
237                 break;
238
239         case XenbusStateClosing:
240                 xenbus_switch_state(dev, XenbusStateClosing);
241                 break;
242
243         case XenbusStateClosed:
244                 xenbus_switch_state(dev, XenbusStateClosed);
245                 if (xenbus_dev_is_online(dev))
246                         break;
247                 /* fall through if not online */
248         case XenbusStateUnknown:
249                 if (be->netif != NULL)
250                         kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
251                 device_unregister(&dev->dev);
252                 break;
253
254         default:
255                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
256                                  frontend_state);
257                 break;
258         }
259 }
260
261
262 static void xen_net_read_rate(struct xenbus_device *dev,
263                               unsigned long *bytes, unsigned long *usec)
264 {
265         char *s, *e;
266         unsigned long b, u;
267         char *ratestr;
268
269         /* Default to unlimited bandwidth. */
270         *bytes = ~0UL;
271         *usec = 0;
272
273         ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
274         if (IS_ERR(ratestr))
275                 return;
276
277         s = ratestr;
278         b = simple_strtoul(s, &e, 10);
279         if ((s == e) || (*e != ','))
280                 goto fail;
281
282         s = e + 1;
283         u = simple_strtoul(s, &e, 10);
284         if ((s == e) || (*e != '\0'))
285                 goto fail;
286
287         *bytes = b;
288         *usec = u;
289
290         kfree(ratestr);
291         return;
292
293  fail:
294         WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
295         kfree(ratestr);
296 }
297
298 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
299 {
300         char *s, *e, *macstr;
301         int i;
302
303         macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
304         if (IS_ERR(macstr))
305                 return PTR_ERR(macstr);
306
307         for (i = 0; i < ETH_ALEN; i++) {
308                 mac[i] = simple_strtoul(s, &e, 16);
309                 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
310                         kfree(macstr);
311                         return -ENOENT;
312                 }
313                 s = e+1;
314         }
315
316         kfree(macstr);
317         return 0;
318 }
319
320 static void connect(struct backend_info *be)
321 {
322         int err;
323         struct xenbus_device *dev = be->dev;
324
325         err = connect_rings(be);
326         if (err)
327                 return;
328
329         err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
330         if (err) {
331                 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
332                 return;
333         }
334
335         xen_net_read_rate(dev, &be->netif->credit_bytes,
336                           &be->netif->credit_usec);
337         be->netif->remaining_credit = be->netif->credit_bytes;
338
339         xenbus_switch_state(dev, XenbusStateConnected);
340
341         netif_wake_queue(be->netif->dev);
342 }
343
344
345 static int connect_rings(struct backend_info *be)
346 {
347         struct xenbus_device *dev = be->dev;
348         unsigned long tx_ring_ref, rx_ring_ref;
349         unsigned int evtchn, rx_copy;
350         int err;
351         int val;
352
353         DPRINTK("");
354
355         err = xenbus_gather(XBT_NIL, dev->otherend,
356                             "tx-ring-ref", "%lu", &tx_ring_ref,
357                             "rx-ring-ref", "%lu", &rx_ring_ref,
358                             "event-channel", "%u", &evtchn, NULL);
359         if (err) {
360                 xenbus_dev_fatal(dev, err,
361                                  "reading %s/ring-ref and event-channel",
362                                  dev->otherend);
363                 return err;
364         }
365
366         err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
367                            &rx_copy);
368         if (err == -ENOENT) {
369                 err = 0;
370                 rx_copy = 0;
371         }
372         if (err < 0) {
373                 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
374                                  dev->otherend);
375                 return err;
376         }
377         be->netif->copying_receiver = !!rx_copy;
378
379         if (be->netif->dev->tx_queue_len != 0) {
380                 if (xenbus_scanf(XBT_NIL, dev->otherend,
381                                  "feature-rx-notify", "%d", &val) < 0)
382                         val = 0;
383                 if (val)
384                         be->netif->can_queue = 1;
385                 else
386                         /* Must be non-zero for pfifo_fast to work. */
387                         be->netif->dev->tx_queue_len = 1;
388         }
389
390         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
391                 val = 0;
392         if (val) {
393                 be->netif->features |= NETIF_F_SG;
394                 be->netif->dev->features |= NETIF_F_SG;
395         }
396
397         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
398                          &val) < 0)
399                 val = 0;
400         if (val) {
401                 be->netif->features |= NETIF_F_TSO;
402                 be->netif->dev->features |= NETIF_F_TSO;
403         }
404
405         if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
406                          "%d", &val) < 0)
407                 val = 0;
408         if (val) {
409                 be->netif->features &= ~NETIF_F_IP_CSUM;
410                 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
411         }
412
413         /* Map the shared frame, irq etc. */
414         err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
415         if (err) {
416                 xenbus_dev_fatal(dev, err,
417                                  "mapping shared-frames %lu/%lu port %u",
418                                  tx_ring_ref, rx_ring_ref, evtchn);
419                 return err;
420         }
421         return 0;
422 }
423
424
425 /* ** Driver Registration ** */
426
427
428 static struct xenbus_device_id netback_ids[] = {
429         { "vif" },
430         { "" }
431 };
432
433
434 static struct xenbus_driver netback = {
435         .name = "vif",
436         .owner = THIS_MODULE,
437         .ids = netback_ids,
438         .probe = netback_probe,
439         .remove = netback_remove,
440         .uevent = netback_uevent,
441         .otherend_changed = frontend_changed,
442 };
443
444
445 void netif_xenbus_init(void)
446 {
447         xenbus_register_backend(&netback);
448 }