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