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