- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / netback / interface.c
1 /******************************************************************************
2  * arch/xen/drivers/netif/backend/interface.c
3  * 
4  * Network-device interface management.
5  * 
6  * Copyright (c) 2004-2005, Keir Fraser
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  * 
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #include "common.h"
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/delay.h>
37 #include <linux/vmalloc.h>
38 #include <xen/evtchn.h>
39
40 /*
41  * Module parameter 'queue_length':
42  * 
43  * Enables queuing in the network stack when a client has run out of receive
44  * descriptors. Although this feature can improve receive bandwidth by avoiding
45  * packet loss, it can also result in packets sitting in the 'tx_queue' for
46  * unbounded time. This is bad if those packets hold onto foreign resources.
47  * For example, consider a packet that holds onto resources belonging to the
48  * guest for which it is queued (e.g., packet received on vif1.0, destined for
49  * vif1.1 which is not activated in the guest): in this situation the guest
50  * will never be destroyed, unless vif1.1 is taken down. To avoid this, we
51  * run a timer (tx_queue_timeout) to drain the queue when the interface is
52  * blocked.
53  */
54 static unsigned long netbk_queue_length = 32;
55 module_param_named(queue_length, netbk_queue_length, ulong, 0644);
56
57 static void __netif_up(netif_t *netif)
58 {
59         unsigned int group = 0;
60         unsigned int min_groups = atomic_read(&xen_netbk[0].nr_groups);
61         unsigned int i;
62
63         /* Find the list which contains least number of domains. */
64         for (i = 1; i < netbk_nr_groups; i++) {
65                 unsigned int nr_groups = atomic_read(&xen_netbk[i].nr_groups);
66
67                 if (nr_groups < min_groups) {
68                         group = i;
69                         min_groups = nr_groups;
70                 }
71         }
72
73         atomic_inc(&xen_netbk[group].nr_groups);
74         netif->group = group;
75
76         enable_irq(netif->irq);
77         netif_schedule_work(netif);
78 }
79
80 static void __netif_down(netif_t *netif)
81 {
82         struct xen_netbk *netbk = xen_netbk + netif->group;
83
84         disable_irq(netif->irq);
85         netif_deschedule_work(netif);
86
87         netif->group = UINT_MAX;
88         atomic_dec(&netbk->nr_groups);
89 }
90
91 static int net_open(struct net_device *dev)
92 {
93         netif_t *netif = netdev_priv(dev);
94         if (netback_carrier_ok(netif)) {
95                 __netif_up(netif);
96                 netif_start_queue(dev);
97         }
98         return 0;
99 }
100
101 static int net_close(struct net_device *dev)
102 {
103         netif_t *netif = netdev_priv(dev);
104         if (netback_carrier_ok(netif))
105                 __netif_down(netif);
106         netif_stop_queue(dev);
107         return 0;
108 }
109
110 static int netbk_change_mtu(struct net_device *dev, int mtu)
111 {
112         int max = netbk_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
113
114         if (mtu > max)
115                 return -EINVAL;
116         dev->mtu = mtu;
117         return 0;
118 }
119
120 static netdev_features_t netbk_fix_features(struct net_device *dev,
121                                             netdev_features_t features)
122 {
123         netif_t *netif = netdev_priv(dev);
124
125         if (!netif->can_sg)
126                 features &= ~NETIF_F_SG;
127         if (!netif->gso)
128                 features &= ~NETIF_F_TSO;
129         if (!netif->csum)
130                 features &= ~NETIF_F_IP_CSUM;
131
132         return features;
133 }
134
135 static void netbk_get_drvinfo(struct net_device *dev,
136                               struct ethtool_drvinfo *info)
137 {
138         strcpy(info->driver, "netbk");
139         strlcpy(info->bus_info, dev_name(dev->dev.parent),
140                 ARRAY_SIZE(info->bus_info));
141 }
142
143 static const struct netif_stat {
144         char name[ETH_GSTRING_LEN];
145         u16 offset;
146 } netbk_stats[] = {
147         { "copied_skbs", offsetof(netif_t, nr_copied_skbs) / sizeof(long) },
148         { "rx_gso_csum_fixups", offsetof(netif_t, rx_gso_csum_fixups) / sizeof(long) },
149 };
150
151 static int netbk_get_sset_count(struct net_device *dev, int sset)
152 {
153         switch (sset) {
154         case ETH_SS_STATS:
155                 return ARRAY_SIZE(netbk_stats);
156         }
157         return -EOPNOTSUPP;
158 }
159
160 static void netbk_get_ethtool_stats(struct net_device *dev,
161                                    struct ethtool_stats *stats, u64 * data)
162 {
163         unsigned long *np = netdev_priv(dev);
164         int i;
165
166         for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
167                 data[i] = np[netbk_stats[i].offset];
168 }
169
170 static void netbk_get_strings(struct net_device *dev, u32 stringset, u8 * data)
171 {
172         int i;
173
174         switch (stringset) {
175         case ETH_SS_STATS:
176                 for (i = 0; i < ARRAY_SIZE(netbk_stats); i++)
177                         memcpy(data + i * ETH_GSTRING_LEN,
178                                netbk_stats[i].name, ETH_GSTRING_LEN);
179                 break;
180         }
181 }
182
183 static const struct ethtool_ops network_ethtool_ops =
184 {
185         .get_drvinfo = netbk_get_drvinfo,
186         .get_link = ethtool_op_get_link,
187
188         .get_sset_count = netbk_get_sset_count,
189         .get_ethtool_stats = netbk_get_ethtool_stats,
190         .get_strings = netbk_get_strings,
191 };
192
193 static const struct net_device_ops netif_be_netdev_ops = {
194         .ndo_open               = net_open,
195         .ndo_stop               = net_close,
196         .ndo_start_xmit         = netif_be_start_xmit,
197         .ndo_change_mtu         = netbk_change_mtu,
198         .ndo_fix_features       = netbk_fix_features,
199         .ndo_set_mac_address    = eth_mac_addr,
200         .ndo_validate_addr      = eth_validate_addr,
201 };
202
203 netif_t *netif_alloc(struct device *parent, domid_t domid, unsigned int handle)
204 {
205         int err = 0;
206         struct net_device *dev;
207         netif_t *netif;
208         char name[IFNAMSIZ] = {};
209
210         snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
211         dev = alloc_netdev(sizeof(netif_t), name, ether_setup);
212         if (dev == NULL) {
213                 DPRINTK("Could not create netif: out of memory\n");
214                 return ERR_PTR(-ENOMEM);
215         }
216
217         SET_NETDEV_DEV(dev, parent);
218
219         netif = netdev_priv(dev);
220         netif->domid  = domid;
221         netif->group = UINT_MAX;
222         netif->handle = handle;
223         netif->can_sg = 1;
224         netif->csum = 1;
225         atomic_set(&netif->refcnt, 1);
226         init_waitqueue_head(&netif->waiting_to_free);
227         netif->dev = dev;
228
229         netback_carrier_off(netif);
230
231         netif->credit_bytes = netif->remaining_credit = ~0UL;
232         netif->credit_usec  = 0UL;
233         init_timer(&netif->credit_timeout);
234         /* Initialize 'expires' now: it's used to track the credit window. */
235         netif->credit_timeout.expires = jiffies;
236
237         init_timer(&netif->tx_queue_timeout);
238
239         dev->netdev_ops = &netif_be_netdev_ops;
240
241         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
242         dev->features = dev->hw_features;
243
244         SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
245
246         dev->tx_queue_len = netbk_queue_length;
247
248         /*
249          * Initialise a dummy MAC address. We choose the numerically
250          * largest non-broadcast address to prevent the address getting
251          * stolen by an Ethernet bridge for STP purposes.
252          * (FE:FF:FF:FF:FF:FF)
253          */ 
254         memset(dev->dev_addr, 0xFF, ETH_ALEN);
255         dev->dev_addr[0] &= ~0x01;
256
257         rtnl_lock();
258         err = register_netdevice(dev);
259         rtnl_unlock();
260         if (err) {
261                 DPRINTK("Could not register new net device %s: err=%d\n",
262                         dev->name, err);
263                 free_netdev(dev);
264                 return ERR_PTR(err);
265         }
266
267         DPRINTK("Successfully created netif\n");
268         return netif;
269 }
270
271 int netif_map(struct backend_info *be, grant_ref_t tx_ring_ref,
272               grant_ref_t rx_ring_ref, evtchn_port_t evtchn)
273 {
274         netif_t *netif = be->netif;
275         struct vm_struct *area;
276         int err = -ENOMEM;
277         netif_tx_sring_t *txs;
278         netif_rx_sring_t *rxs;
279
280         /* Already connected through? */
281         if (netif->irq)
282                 return 0;
283
284         area = xenbus_map_ring_valloc(be->dev, tx_ring_ref);
285         if (IS_ERR(area))
286                 return PTR_ERR(area);
287         netif->tx_comms_area = area;
288         area = xenbus_map_ring_valloc(be->dev, rx_ring_ref);
289         if (IS_ERR(area)) {
290                 err = PTR_ERR(area);
291                 goto err_rx;
292         }
293         netif->rx_comms_area = area;
294
295         err = bind_interdomain_evtchn_to_irqhandler(
296                 netif->domid, evtchn, netif_be_int, 0,
297                 netif->dev->name, netif);
298         if (err < 0)
299                 goto err_hypervisor;
300         BUG_ON(err < DYNIRQ_BASE || err >= DYNIRQ_BASE + NR_DYNIRQS);
301         netif->irq = err;
302         disable_irq(netif->irq);
303
304         txs = (netif_tx_sring_t *)netif->tx_comms_area->addr;
305         BACK_RING_INIT(&netif->tx, txs, PAGE_SIZE);
306
307         rxs = (netif_rx_sring_t *)
308                 ((char *)netif->rx_comms_area->addr);
309         BACK_RING_INIT(&netif->rx, rxs, PAGE_SIZE);
310
311         netif->rx_req_cons_peek = 0;
312
313         netif_get(netif);
314
315         rtnl_lock();
316         if (!netif->can_sg && netif->dev->mtu > ETH_DATA_LEN)
317                 dev_set_mtu(netif->dev, ETH_DATA_LEN);
318         netdev_update_features(netif->dev);
319         netback_carrier_on(netif);
320         if (netif_running(netif->dev))
321                 __netif_up(netif);
322         rtnl_unlock();
323
324         return 0;
325 err_hypervisor:
326         xenbus_unmap_ring_vfree(be->dev, netif->rx_comms_area);
327 err_rx:
328         xenbus_unmap_ring_vfree(be->dev, netif->tx_comms_area);
329         return err;
330 }
331
332 void netif_disconnect(struct backend_info *be)
333 {
334         netif_t *netif = be->netif;
335
336         if (netback_carrier_ok(netif)) {
337                 rtnl_lock();
338                 netback_carrier_off(netif);
339                 netif_carrier_off(netif->dev); /* discard queued packets */
340                 if (netif_running(netif->dev))
341                         __netif_down(netif);
342                 rtnl_unlock();
343                 netif_put(netif);
344         }
345
346         atomic_dec(&netif->refcnt);
347         wait_event(netif->waiting_to_free, atomic_read(&netif->refcnt) == 0);
348
349         del_timer_sync(&netif->credit_timeout);
350         del_timer_sync(&netif->tx_queue_timeout);
351
352         if (netif->irq)
353                 unbind_from_irqhandler(netif->irq, netif);
354         
355         unregister_netdev(netif->dev);
356
357         if (netif->tx.sring) {
358                 xenbus_unmap_ring_vfree(be->dev, netif->tx_comms_area);
359                 xenbus_unmap_ring_vfree(be->dev, netif->rx_comms_area);
360         }
361
362         free_netdev(netif->dev);
363 }