fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.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
37 /*
38  * Module parameter 'queue_length':
39  * 
40  * Enables queuing in the network stack when a client has run out of receive
41  * descriptors. Although this feature can improve receive bandwidth by avoiding
42  * packet loss, it can also result in packets sitting in the 'tx_queue' for
43  * unbounded time. This is bad if those packets hold onto foreign resources.
44  * For example, consider a packet that holds onto resources belonging to the
45  * guest for which it is queued (e.g., packet received on vif1.0, destined for
46  * vif1.1 which is not activated in the guest): in this situation the guest
47  * will never be destroyed, unless vif1.1 is taken down. To avoid this, we
48  * run a timer (tx_queue_timeout) to drain the queue when the interface is
49  * blocked.
50  */
51 static unsigned long netbk_queue_length = 32;
52 module_param_named(queue_length, netbk_queue_length, ulong, 0);
53
54 static void __netif_up(netif_t *netif)
55 {
56         enable_irq(netif->irq);
57         netif_schedule_work(netif);
58 }
59
60 static void __netif_down(netif_t *netif)
61 {
62         disable_irq(netif->irq);
63         netif_deschedule_work(netif);
64 }
65
66 static int net_open(struct net_device *dev)
67 {
68         netif_t *netif = netdev_priv(dev);
69         if (netif_carrier_ok(dev))
70                 __netif_up(netif);
71         return 0;
72 }
73
74 static int net_close(struct net_device *dev)
75 {
76         netif_t *netif = netdev_priv(dev);
77         if (netif_carrier_ok(dev))
78                 __netif_down(netif);
79         return 0;
80 }
81
82 static int netbk_change_mtu(struct net_device *dev, int mtu)
83 {
84         int max = netbk_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
85
86         if (mtu > max)
87                 return -EINVAL;
88         dev->mtu = mtu;
89         return 0;
90 }
91
92 static int netbk_set_sg(struct net_device *dev, u32 data)
93 {
94         if (data) {
95                 netif_t *netif = netdev_priv(dev);
96
97                 if (!(netif->features & NETIF_F_SG))
98                         return -ENOSYS;
99         }
100
101         return ethtool_op_set_sg(dev, data);
102 }
103
104 static int netbk_set_tso(struct net_device *dev, u32 data)
105 {
106         if (data) {
107                 netif_t *netif = netdev_priv(dev);
108
109                 if (!(netif->features & NETIF_F_TSO))
110                         return -ENOSYS;
111         }
112
113         return ethtool_op_set_tso(dev, data);
114 }
115
116 static struct ethtool_ops network_ethtool_ops =
117 {
118         .get_tx_csum = ethtool_op_get_tx_csum,
119         .set_tx_csum = ethtool_op_set_tx_csum,
120         .get_sg = ethtool_op_get_sg,
121         .set_sg = netbk_set_sg,
122         .get_tso = ethtool_op_get_tso,
123         .set_tso = netbk_set_tso,
124         .get_link = ethtool_op_get_link,
125 };
126
127 netif_t *netif_alloc(domid_t domid, unsigned int handle)
128 {
129         int err = 0, i;
130         struct net_device *dev;
131         netif_t *netif;
132         char name[IFNAMSIZ] = {};
133
134         snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
135         dev = alloc_netdev(sizeof(netif_t), name, ether_setup);
136         if (dev == NULL) {
137                 DPRINTK("Could not create netif: out of memory\n");
138                 return ERR_PTR(-ENOMEM);
139         }
140
141         netif_carrier_off(dev);
142
143         netif = netdev_priv(dev);
144         memset(netif, 0, sizeof(*netif));
145         netif->domid  = domid;
146         netif->handle = handle;
147         atomic_set(&netif->refcnt, 1);
148         init_waitqueue_head(&netif->waiting_to_free);
149         netif->dev = dev;
150
151         netif->credit_bytes = netif->remaining_credit = ~0UL;
152         netif->credit_usec  = 0UL;
153         init_timer(&netif->credit_timeout);
154         /* Initialize 'expires' now: it's used to track the credit window. */
155         netif->credit_timeout.expires = jiffies;
156
157         init_timer(&netif->tx_queue_timeout);
158
159         dev->hard_start_xmit = netif_be_start_xmit;
160         dev->get_stats       = netif_be_get_stats;
161         dev->open            = net_open;
162         dev->stop            = net_close;
163         dev->change_mtu      = netbk_change_mtu;
164         dev->features        = NETIF_F_IP_CSUM;
165
166         SET_ETHTOOL_OPS(dev, &network_ethtool_ops);
167
168         dev->tx_queue_len = netbk_queue_length;
169
170         /*
171          * Initialise a dummy MAC address. We choose the numerically
172          * largest non-broadcast address to prevent the address getting
173          * stolen by an Ethernet bridge for STP purposes.
174          * (FE:FF:FF:FF:FF:FF)
175          */ 
176         memset(dev->dev_addr, 0xFF, ETH_ALEN);
177         dev->dev_addr[0] &= ~0x01;
178
179         rtnl_lock();
180         err = register_netdevice(dev);
181         rtnl_unlock();
182         if (err) {
183                 DPRINTK("Could not register new net device %s: err=%d\n",
184                         dev->name, err);
185                 free_netdev(dev);
186                 return ERR_PTR(err);
187         }
188
189         DPRINTK("Successfully created netif\n");
190         return netif;
191 }
192
193 static int map_frontend_pages(
194         netif_t *netif, grant_ref_t tx_ring_ref, grant_ref_t rx_ring_ref)
195 {
196         struct gnttab_map_grant_ref op;
197         int ret;
198
199         gnttab_set_map_op(&op, (unsigned long)netif->tx_comms_area->addr,
200                           GNTMAP_host_map, tx_ring_ref, netif->domid);
201     
202         lock_vm_area(netif->tx_comms_area);
203         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1);
204         unlock_vm_area(netif->tx_comms_area);
205         BUG_ON(ret);
206
207         if (op.status) { 
208                 DPRINTK(" Gnttab failure mapping tx_ring_ref!\n");
209                 return op.status;
210         }
211
212         netif->tx_shmem_ref    = tx_ring_ref;
213         netif->tx_shmem_handle = op.handle;
214
215         gnttab_set_map_op(&op, (unsigned long)netif->rx_comms_area->addr,
216                           GNTMAP_host_map, rx_ring_ref, netif->domid);
217
218         lock_vm_area(netif->rx_comms_area);
219         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1);
220         unlock_vm_area(netif->rx_comms_area);
221         BUG_ON(ret);
222
223         if (op.status) {
224                 DPRINTK(" Gnttab failure mapping rx_ring_ref!\n");
225                 return op.status;
226         }
227
228         netif->rx_shmem_ref    = rx_ring_ref;
229         netif->rx_shmem_handle = op.handle;
230
231         return 0;
232 }
233
234 static void unmap_frontend_pages(netif_t *netif)
235 {
236         struct gnttab_unmap_grant_ref op;
237         int ret;
238
239         gnttab_set_unmap_op(&op, (unsigned long)netif->tx_comms_area->addr,
240                             GNTMAP_host_map, netif->tx_shmem_handle);
241
242         lock_vm_area(netif->tx_comms_area);
243         ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1);
244         unlock_vm_area(netif->tx_comms_area);
245         BUG_ON(ret);
246
247         gnttab_set_unmap_op(&op, (unsigned long)netif->rx_comms_area->addr,
248                             GNTMAP_host_map, netif->rx_shmem_handle);
249
250         lock_vm_area(netif->rx_comms_area);
251         ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1);
252         unlock_vm_area(netif->rx_comms_area);
253         BUG_ON(ret);
254 }
255
256 int netif_map(netif_t *netif, unsigned long tx_ring_ref,
257               unsigned long rx_ring_ref, unsigned int evtchn)
258 {
259         int err = -ENOMEM;
260         netif_tx_sring_t *txs;
261         netif_rx_sring_t *rxs;
262         struct evtchn_bind_interdomain bind_interdomain;
263
264         /* Already connected through? */
265         if (netif->irq)
266                 return 0;
267
268         netif->tx_comms_area = alloc_vm_area(PAGE_SIZE);
269         if (netif->tx_comms_area == NULL)
270                 return -ENOMEM;
271         netif->rx_comms_area = alloc_vm_area(PAGE_SIZE);
272         if (netif->rx_comms_area == NULL)
273                 goto err_rx;
274
275         err = map_frontend_pages(netif, tx_ring_ref, rx_ring_ref);
276         if (err)
277                 goto err_map;
278
279         bind_interdomain.remote_dom = netif->domid;
280         bind_interdomain.remote_port = evtchn;
281
282         err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
283                                           &bind_interdomain);
284         if (err)
285                 goto err_hypervisor;
286
287         netif->evtchn = bind_interdomain.local_port;
288
289         netif->irq = bind_evtchn_to_irqhandler(
290                 netif->evtchn, netif_be_int, 0, netif->dev->name, netif);
291         disable_irq(netif->irq);
292
293         txs = (netif_tx_sring_t *)netif->tx_comms_area->addr;
294         BACK_RING_INIT(&netif->tx, txs, PAGE_SIZE);
295
296         rxs = (netif_rx_sring_t *)
297                 ((char *)netif->rx_comms_area->addr);
298         BACK_RING_INIT(&netif->rx, rxs, PAGE_SIZE);
299
300         netif->rx_req_cons_peek = 0;
301
302         netif_get(netif);
303
304         rtnl_lock();
305         netif_carrier_on(netif->dev);
306         if (netif_running(netif->dev))
307                 __netif_up(netif);
308         rtnl_unlock();
309
310         return 0;
311 err_hypervisor:
312         unmap_frontend_pages(netif);
313 err_map:
314         free_vm_area(netif->rx_comms_area);
315 err_rx:
316         free_vm_area(netif->tx_comms_area);
317         return err;
318 }
319
320 void netif_disconnect(netif_t *netif)
321 {
322         if (netif_carrier_ok(netif->dev)) {
323                 rtnl_lock();
324                 netif_carrier_off(netif->dev);
325                 if (netif_running(netif->dev))
326                         __netif_down(netif);
327                 rtnl_unlock();
328                 netif_put(netif);
329         }
330
331         atomic_dec(&netif->refcnt);
332         wait_event(netif->waiting_to_free, atomic_read(&netif->refcnt) == 0);
333
334         del_timer_sync(&netif->credit_timeout);
335         del_timer_sync(&netif->tx_queue_timeout);
336
337         if (netif->irq)
338                 unbind_from_irqhandler(netif->irq, netif);
339         
340         unregister_netdev(netif->dev);
341
342         if (netif->tx.sring) {
343                 unmap_frontend_pages(netif);
344                 free_vm_area(netif->tx_comms_area);
345                 free_vm_area(netif->rx_comms_area);
346         }
347
348         free_netdev(netif->dev);
349 }