x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / net / xen-netback / interface.c
blobadfe46068581e46cee2c1cf5bdf1a6c160ceb2df
1 /*
2 * Network-device interface management.
4 * Copyright (c) 2004-2005, Keir Fraser
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
31 #include "common.h"
33 #include <linux/kthread.h>
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/if_vlan.h>
38 #include <xen/events.h>
39 #include <asm/xen/hypercall.h>
41 #define XENVIF_QUEUE_LENGTH 32
42 #define XENVIF_NAPI_WEIGHT 64
44 int xenvif_schedulable(struct xenvif *vif)
46 return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
49 static int xenvif_rx_schedulable(struct xenvif *vif)
51 return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif);
54 static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
56 struct xenvif *vif = dev_id;
58 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
59 napi_schedule(&vif->napi);
61 return IRQ_HANDLED;
64 static int xenvif_poll(struct napi_struct *napi, int budget)
66 struct xenvif *vif = container_of(napi, struct xenvif, napi);
67 int work_done;
69 /* This vif is rogue, we pretend we've there is nothing to do
70 * for this vif to deschedule it from NAPI. But this interface
71 * will be turned off in thread context later.
73 if (unlikely(vif->disabled)) {
74 napi_complete(napi);
75 return 0;
78 work_done = xenvif_tx_action(vif, budget);
80 if (work_done < budget) {
81 int more_to_do = 0;
82 unsigned long flags;
84 /* It is necessary to disable IRQ before calling
85 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
86 * lose event from the frontend.
88 * Consider:
89 * RING_HAS_UNCONSUMED_REQUESTS
90 * <frontend generates event to trigger napi_schedule>
91 * __napi_complete
93 * This handler is still in scheduled state so the
94 * event has no effect at all. After __napi_complete
95 * this handler is descheduled and cannot get
96 * scheduled again. We lose event in this case and the ring
97 * will be completely stalled.
100 local_irq_save(flags);
102 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
103 if (!more_to_do)
104 __napi_complete(napi);
106 local_irq_restore(flags);
109 return work_done;
112 static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
114 struct xenvif *vif = dev_id;
116 if (xenvif_rx_schedulable(vif))
117 netif_wake_queue(vif->dev);
119 return IRQ_HANDLED;
122 static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
124 xenvif_tx_interrupt(irq, dev_id);
125 xenvif_rx_interrupt(irq, dev_id);
127 return IRQ_HANDLED;
130 static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
132 struct xenvif *vif = netdev_priv(dev);
134 BUG_ON(skb->dev != dev);
136 /* Drop the packet if vif is not ready */
137 if (vif->task == NULL)
138 goto drop;
140 /* Drop the packet if the target domain has no receive buffers. */
141 if (!xenvif_rx_schedulable(vif))
142 goto drop;
144 /* Reserve ring slots for the worst-case number of fragments. */
145 vif->rx_req_cons_peek += xenvif_count_skb_slots(vif, skb);
147 if (vif->can_queue && xenvif_must_stop_queue(vif))
148 netif_stop_queue(dev);
150 xenvif_queue_tx_skb(vif, skb);
152 return NETDEV_TX_OK;
154 drop:
155 vif->dev->stats.tx_dropped++;
156 dev_kfree_skb(skb);
157 return NETDEV_TX_OK;
160 void xenvif_notify_tx_completion(struct xenvif *vif)
162 if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif))
163 netif_wake_queue(vif->dev);
166 static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
168 struct xenvif *vif = netdev_priv(dev);
169 return &vif->dev->stats;
172 static void xenvif_up(struct xenvif *vif)
174 napi_enable(&vif->napi);
175 enable_irq(vif->tx_irq);
176 if (vif->tx_irq != vif->rx_irq)
177 enable_irq(vif->rx_irq);
178 xenvif_check_rx_xenvif(vif);
181 static void xenvif_down(struct xenvif *vif)
183 napi_disable(&vif->napi);
184 disable_irq(vif->tx_irq);
185 if (vif->tx_irq != vif->rx_irq)
186 disable_irq(vif->rx_irq);
187 del_timer_sync(&vif->credit_timeout);
190 static int xenvif_open(struct net_device *dev)
192 struct xenvif *vif = netdev_priv(dev);
193 if (netif_carrier_ok(dev))
194 xenvif_up(vif);
195 netif_start_queue(dev);
196 return 0;
199 static int xenvif_close(struct net_device *dev)
201 struct xenvif *vif = netdev_priv(dev);
202 if (netif_carrier_ok(dev))
203 xenvif_down(vif);
204 netif_stop_queue(dev);
205 return 0;
208 static int xenvif_change_mtu(struct net_device *dev, int mtu)
210 struct xenvif *vif = netdev_priv(dev);
211 int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
213 if (mtu > max)
214 return -EINVAL;
215 dev->mtu = mtu;
216 return 0;
219 static netdev_features_t xenvif_fix_features(struct net_device *dev,
220 netdev_features_t features)
222 struct xenvif *vif = netdev_priv(dev);
224 if (!vif->can_sg)
225 features &= ~NETIF_F_SG;
226 if (!vif->gso && !vif->gso_prefix)
227 features &= ~NETIF_F_TSO;
228 if (!vif->csum)
229 features &= ~NETIF_F_IP_CSUM;
231 return features;
234 static const struct xenvif_stat {
235 char name[ETH_GSTRING_LEN];
236 u16 offset;
237 } xenvif_stats[] = {
239 "rx_gso_checksum_fixup",
240 offsetof(struct xenvif, rx_gso_checksum_fixup)
244 static int xenvif_get_sset_count(struct net_device *dev, int string_set)
246 switch (string_set) {
247 case ETH_SS_STATS:
248 return ARRAY_SIZE(xenvif_stats);
249 default:
250 return -EINVAL;
254 static void xenvif_get_ethtool_stats(struct net_device *dev,
255 struct ethtool_stats *stats, u64 * data)
257 void *vif = netdev_priv(dev);
258 int i;
260 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
261 data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
264 static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
266 int i;
268 switch (stringset) {
269 case ETH_SS_STATS:
270 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
271 memcpy(data + i * ETH_GSTRING_LEN,
272 xenvif_stats[i].name, ETH_GSTRING_LEN);
273 break;
277 static const struct ethtool_ops xenvif_ethtool_ops = {
278 .get_link = ethtool_op_get_link,
280 .get_sset_count = xenvif_get_sset_count,
281 .get_ethtool_stats = xenvif_get_ethtool_stats,
282 .get_strings = xenvif_get_strings,
285 static const struct net_device_ops xenvif_netdev_ops = {
286 .ndo_start_xmit = xenvif_start_xmit,
287 .ndo_get_stats = xenvif_get_stats,
288 .ndo_open = xenvif_open,
289 .ndo_stop = xenvif_close,
290 .ndo_change_mtu = xenvif_change_mtu,
291 .ndo_fix_features = xenvif_fix_features,
292 .ndo_set_mac_address = eth_mac_addr,
293 .ndo_validate_addr = eth_validate_addr,
296 struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
297 unsigned int handle)
299 int err;
300 struct net_device *dev;
301 struct xenvif *vif;
302 char name[IFNAMSIZ] = {};
303 int i;
305 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
306 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
307 if (dev == NULL) {
308 pr_warn("Could not allocate netdev for %s\n", name);
309 return ERR_PTR(-ENOMEM);
312 SET_NETDEV_DEV(dev, parent);
314 vif = netdev_priv(dev);
315 vif->domid = domid;
316 vif->handle = handle;
317 vif->can_sg = 1;
318 vif->csum = 1;
319 vif->dev = dev;
321 vif->disabled = false;
323 vif->credit_bytes = vif->remaining_credit = ~0UL;
324 vif->credit_usec = 0UL;
325 init_timer(&vif->credit_timeout);
326 vif->credit_window_start = get_jiffies_64();
328 dev->netdev_ops = &xenvif_netdev_ops;
329 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
330 dev->features = dev->hw_features;
331 SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
333 dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
335 skb_queue_head_init(&vif->rx_queue);
336 skb_queue_head_init(&vif->tx_queue);
338 vif->pending_cons = 0;
339 vif->pending_prod = MAX_PENDING_REQS;
340 for (i = 0; i < MAX_PENDING_REQS; i++)
341 vif->pending_ring[i] = i;
342 for (i = 0; i < MAX_PENDING_REQS; i++)
343 vif->mmap_pages[i] = NULL;
346 * Initialise a dummy MAC address. We choose the numerically
347 * largest non-broadcast address to prevent the address getting
348 * stolen by an Ethernet bridge for STP purposes.
349 * (FE:FF:FF:FF:FF:FF)
351 memset(dev->dev_addr, 0xFF, ETH_ALEN);
352 dev->dev_addr[0] &= ~0x01;
354 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
356 netif_carrier_off(dev);
358 err = register_netdev(dev);
359 if (err) {
360 netdev_warn(dev, "Could not register device: err=%d\n", err);
361 free_netdev(dev);
362 return ERR_PTR(err);
365 netdev_dbg(dev, "Successfully created xenvif\n");
367 __module_get(THIS_MODULE);
369 return vif;
372 int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
373 unsigned long rx_ring_ref, unsigned int tx_evtchn,
374 unsigned int rx_evtchn)
376 int err = -ENOMEM;
378 /* Already connected through? */
379 if (vif->tx_irq)
380 return 0;
382 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
383 if (err < 0)
384 goto err;
386 if (tx_evtchn == rx_evtchn) {
387 /* feature-split-event-channels == 0 */
388 err = bind_interdomain_evtchn_to_irqhandler(
389 vif->domid, tx_evtchn, xenvif_interrupt, 0,
390 vif->dev->name, vif);
391 if (err < 0)
392 goto err_unmap;
393 vif->tx_irq = vif->rx_irq = err;
394 disable_irq(vif->tx_irq);
395 } else {
396 /* feature-split-event-channels == 1 */
397 snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
398 "%s-tx", vif->dev->name);
399 err = bind_interdomain_evtchn_to_irqhandler(
400 vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
401 vif->tx_irq_name, vif);
402 if (err < 0)
403 goto err_unmap;
404 vif->tx_irq = err;
405 disable_irq(vif->tx_irq);
407 snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
408 "%s-rx", vif->dev->name);
409 err = bind_interdomain_evtchn_to_irqhandler(
410 vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
411 vif->rx_irq_name, vif);
412 if (err < 0)
413 goto err_tx_unbind;
414 vif->rx_irq = err;
415 disable_irq(vif->rx_irq);
418 init_waitqueue_head(&vif->wq);
419 vif->task = kthread_create(xenvif_kthread,
420 (void *)vif, "%s", vif->dev->name);
421 if (IS_ERR(vif->task)) {
422 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
423 err = PTR_ERR(vif->task);
424 goto err_rx_unbind;
427 rtnl_lock();
428 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
429 dev_set_mtu(vif->dev, ETH_DATA_LEN);
430 netdev_update_features(vif->dev);
431 netif_carrier_on(vif->dev);
432 if (netif_running(vif->dev))
433 xenvif_up(vif);
434 rtnl_unlock();
436 wake_up_process(vif->task);
438 return 0;
440 err_rx_unbind:
441 unbind_from_irqhandler(vif->rx_irq, vif);
442 vif->rx_irq = 0;
443 err_tx_unbind:
444 unbind_from_irqhandler(vif->tx_irq, vif);
445 vif->tx_irq = 0;
446 err_unmap:
447 xenvif_unmap_frontend_rings(vif);
448 err:
449 module_put(THIS_MODULE);
450 return err;
453 void xenvif_carrier_off(struct xenvif *vif)
455 struct net_device *dev = vif->dev;
457 rtnl_lock();
458 netif_carrier_off(dev); /* discard queued packets */
459 if (netif_running(dev))
460 xenvif_down(vif);
461 rtnl_unlock();
464 void xenvif_disconnect(struct xenvif *vif)
466 if (netif_carrier_ok(vif->dev))
467 xenvif_carrier_off(vif);
469 if (vif->tx_irq) {
470 if (vif->tx_irq == vif->rx_irq)
471 unbind_from_irqhandler(vif->tx_irq, vif);
472 else {
473 unbind_from_irqhandler(vif->tx_irq, vif);
474 unbind_from_irqhandler(vif->rx_irq, vif);
476 vif->tx_irq = 0;
479 if (vif->task)
480 kthread_stop(vif->task);
482 xenvif_unmap_frontend_rings(vif);
485 void xenvif_free(struct xenvif *vif)
487 netif_napi_del(&vif->napi);
489 unregister_netdev(vif->dev);
491 free_netdev(vif->dev);
493 module_put(THIS_MODULE);