aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/xen-netback/interface.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/xen-netback/interface.c')
-rw-r--r--drivers/net/xen-netback/interface.c119
1 files changed, 80 insertions, 39 deletions
diff --git a/drivers/net/xen-netback/interface.c b/drivers/net/xen-netback/interface.c
index 087d2db0389d..44d6b707c77e 100644
--- a/drivers/net/xen-netback/interface.c
+++ b/drivers/net/xen-netback/interface.c
@@ -30,6 +30,7 @@
30 30
31#include "common.h" 31#include "common.h"
32 32
33#include <linux/kthread.h>
33#include <linux/ethtool.h> 34#include <linux/ethtool.h>
34#include <linux/rtnetlink.h> 35#include <linux/rtnetlink.h>
35#include <linux/if_vlan.h> 36#include <linux/if_vlan.h>
@@ -38,17 +39,7 @@
38#include <asm/xen/hypercall.h> 39#include <asm/xen/hypercall.h>
39 40
40#define XENVIF_QUEUE_LENGTH 32 41#define XENVIF_QUEUE_LENGTH 32
41 42#define XENVIF_NAPI_WEIGHT 64
42void xenvif_get(struct xenvif *vif)
43{
44 atomic_inc(&vif->refcnt);
45}
46
47void xenvif_put(struct xenvif *vif)
48{
49 if (atomic_dec_and_test(&vif->refcnt))
50 wake_up(&vif->waiting_to_free);
51}
52 43
53int xenvif_schedulable(struct xenvif *vif) 44int xenvif_schedulable(struct xenvif *vif)
54{ 45{
@@ -64,21 +55,55 @@ static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
64{ 55{
65 struct xenvif *vif = dev_id; 56 struct xenvif *vif = dev_id;
66 57
67 if (vif->netbk == NULL) 58 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
68 return IRQ_HANDLED; 59 napi_schedule(&vif->napi);
69
70 xen_netbk_schedule_xenvif(vif);
71 60
72 return IRQ_HANDLED; 61 return IRQ_HANDLED;
73} 62}
74 63
64static int xenvif_poll(struct napi_struct *napi, int budget)
65{
66 struct xenvif *vif = container_of(napi, struct xenvif, napi);
67 int work_done;
68
69 work_done = xen_netbk_tx_action(vif, budget);
70
71 if (work_done < budget) {
72 int more_to_do = 0;
73 unsigned long flags;
74
75 /* It is necessary to disable IRQ before calling
76 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
77 * lose event from the frontend.
78 *
79 * Consider:
80 * RING_HAS_UNCONSUMED_REQUESTS
81 * <frontend generates event to trigger napi_schedule>
82 * __napi_complete
83 *
84 * This handler is still in scheduled state so the
85 * event has no effect at all. After __napi_complete
86 * this handler is descheduled and cannot get
87 * scheduled again. We lose event in this case and the ring
88 * will be completely stalled.
89 */
90
91 local_irq_save(flags);
92
93 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
94 if (!more_to_do)
95 __napi_complete(napi);
96
97 local_irq_restore(flags);
98 }
99
100 return work_done;
101}
102
75static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) 103static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
76{ 104{
77 struct xenvif *vif = dev_id; 105 struct xenvif *vif = dev_id;
78 106
79 if (vif->netbk == NULL)
80 return IRQ_HANDLED;
81
82 if (xenvif_rx_schedulable(vif)) 107 if (xenvif_rx_schedulable(vif))
83 netif_wake_queue(vif->dev); 108 netif_wake_queue(vif->dev);
84 109
@@ -99,7 +124,8 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
99 124
100 BUG_ON(skb->dev != dev); 125 BUG_ON(skb->dev != dev);
101 126
102 if (vif->netbk == NULL) 127 /* Drop the packet if vif is not ready */
128 if (vif->task == NULL)
103 goto drop; 129 goto drop;
104 130
105 /* Drop the packet if the target domain has no receive buffers. */ 131 /* Drop the packet if the target domain has no receive buffers. */
@@ -108,7 +134,6 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
108 134
109 /* Reserve ring slots for the worst-case number of fragments. */ 135 /* Reserve ring slots for the worst-case number of fragments. */
110 vif->rx_req_cons_peek += xen_netbk_count_skb_slots(vif, skb); 136 vif->rx_req_cons_peek += xen_netbk_count_skb_slots(vif, skb);
111 xenvif_get(vif);
112 137
113 if (vif->can_queue && xen_netbk_must_stop_queue(vif)) 138 if (vif->can_queue && xen_netbk_must_stop_queue(vif))
114 netif_stop_queue(dev); 139 netif_stop_queue(dev);
@@ -123,11 +148,6 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
123 return NETDEV_TX_OK; 148 return NETDEV_TX_OK;
124} 149}
125 150
126void xenvif_receive_skb(struct xenvif *vif, struct sk_buff *skb)
127{
128 netif_rx_ni(skb);
129}
130
131void xenvif_notify_tx_completion(struct xenvif *vif) 151void xenvif_notify_tx_completion(struct xenvif *vif)
132{ 152{
133 if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif)) 153 if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif))
@@ -142,7 +162,7 @@ static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
142 162
143static void xenvif_up(struct xenvif *vif) 163static void xenvif_up(struct xenvif *vif)
144{ 164{
145 xen_netbk_add_xenvif(vif); 165 napi_enable(&vif->napi);
146 enable_irq(vif->tx_irq); 166 enable_irq(vif->tx_irq);
147 if (vif->tx_irq != vif->rx_irq) 167 if (vif->tx_irq != vif->rx_irq)
148 enable_irq(vif->rx_irq); 168 enable_irq(vif->rx_irq);
@@ -151,12 +171,11 @@ static void xenvif_up(struct xenvif *vif)
151 171
152static void xenvif_down(struct xenvif *vif) 172static void xenvif_down(struct xenvif *vif)
153{ 173{
174 napi_disable(&vif->napi);
154 disable_irq(vif->tx_irq); 175 disable_irq(vif->tx_irq);
155 if (vif->tx_irq != vif->rx_irq) 176 if (vif->tx_irq != vif->rx_irq)
156 disable_irq(vif->rx_irq); 177 disable_irq(vif->rx_irq);
157 del_timer_sync(&vif->credit_timeout); 178 del_timer_sync(&vif->credit_timeout);
158 xen_netbk_deschedule_xenvif(vif);
159 xen_netbk_remove_xenvif(vif);
160} 179}
161 180
162static int xenvif_open(struct net_device *dev) 181static int xenvif_open(struct net_device *dev)
@@ -272,11 +291,12 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
272 struct net_device *dev; 291 struct net_device *dev;
273 struct xenvif *vif; 292 struct xenvif *vif;
274 char name[IFNAMSIZ] = {}; 293 char name[IFNAMSIZ] = {};
294 int i;
275 295
276 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); 296 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
277 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); 297 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
278 if (dev == NULL) { 298 if (dev == NULL) {
279 pr_warn("Could not allocate netdev\n"); 299 pr_warn("Could not allocate netdev for %s\n", name);
280 return ERR_PTR(-ENOMEM); 300 return ERR_PTR(-ENOMEM);
281 } 301 }
282 302
@@ -285,14 +305,9 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
285 vif = netdev_priv(dev); 305 vif = netdev_priv(dev);
286 vif->domid = domid; 306 vif->domid = domid;
287 vif->handle = handle; 307 vif->handle = handle;
288 vif->netbk = NULL;
289 vif->can_sg = 1; 308 vif->can_sg = 1;
290 vif->csum = 1; 309 vif->csum = 1;
291 atomic_set(&vif->refcnt, 1);
292 init_waitqueue_head(&vif->waiting_to_free);
293 vif->dev = dev; 310 vif->dev = dev;
294 INIT_LIST_HEAD(&vif->schedule_list);
295 INIT_LIST_HEAD(&vif->notify_list);
296 311
297 vif->credit_bytes = vif->remaining_credit = ~0UL; 312 vif->credit_bytes = vif->remaining_credit = ~0UL;
298 vif->credit_usec = 0UL; 313 vif->credit_usec = 0UL;
@@ -307,6 +322,16 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
307 322
308 dev->tx_queue_len = XENVIF_QUEUE_LENGTH; 323 dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
309 324
325 skb_queue_head_init(&vif->rx_queue);
326 skb_queue_head_init(&vif->tx_queue);
327
328 vif->pending_cons = 0;
329 vif->pending_prod = MAX_PENDING_REQS;
330 for (i = 0; i < MAX_PENDING_REQS; i++)
331 vif->pending_ring[i] = i;
332 for (i = 0; i < MAX_PENDING_REQS; i++)
333 vif->mmap_pages[i] = NULL;
334
310 /* 335 /*
311 * Initialise a dummy MAC address. We choose the numerically 336 * Initialise a dummy MAC address. We choose the numerically
312 * largest non-broadcast address to prevent the address getting 337 * largest non-broadcast address to prevent the address getting
@@ -316,6 +341,8 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
316 memset(dev->dev_addr, 0xFF, ETH_ALEN); 341 memset(dev->dev_addr, 0xFF, ETH_ALEN);
317 dev->dev_addr[0] &= ~0x01; 342 dev->dev_addr[0] &= ~0x01;
318 343
344 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
345
319 netif_carrier_off(dev); 346 netif_carrier_off(dev);
320 347
321 err = register_netdev(dev); 348 err = register_netdev(dev);
@@ -377,7 +404,14 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
377 disable_irq(vif->rx_irq); 404 disable_irq(vif->rx_irq);
378 } 405 }
379 406
380 xenvif_get(vif); 407 init_waitqueue_head(&vif->wq);
408 vif->task = kthread_create(xen_netbk_kthread,
409 (void *)vif, vif->dev->name);
410 if (IS_ERR(vif->task)) {
411 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
412 err = PTR_ERR(vif->task);
413 goto err_rx_unbind;
414 }
381 415
382 rtnl_lock(); 416 rtnl_lock();
383 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) 417 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
@@ -388,7 +422,13 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
388 xenvif_up(vif); 422 xenvif_up(vif);
389 rtnl_unlock(); 423 rtnl_unlock();
390 424
425 wake_up_process(vif->task);
426
391 return 0; 427 return 0;
428
429err_rx_unbind:
430 unbind_from_irqhandler(vif->rx_irq, vif);
431 vif->rx_irq = 0;
392err_tx_unbind: 432err_tx_unbind:
393 unbind_from_irqhandler(vif->tx_irq, vif); 433 unbind_from_irqhandler(vif->tx_irq, vif);
394 vif->tx_irq = 0; 434 vif->tx_irq = 0;
@@ -408,7 +448,6 @@ void xenvif_carrier_off(struct xenvif *vif)
408 if (netif_running(dev)) 448 if (netif_running(dev))
409 xenvif_down(vif); 449 xenvif_down(vif);
410 rtnl_unlock(); 450 rtnl_unlock();
411 xenvif_put(vif);
412} 451}
413 452
414void xenvif_disconnect(struct xenvif *vif) 453void xenvif_disconnect(struct xenvif *vif)
@@ -422,9 +461,6 @@ void xenvif_disconnect(struct xenvif *vif)
422 if (netif_carrier_ok(vif->dev)) 461 if (netif_carrier_ok(vif->dev))
423 xenvif_carrier_off(vif); 462 xenvif_carrier_off(vif);
424 463
425 atomic_dec(&vif->refcnt);
426 wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
427
428 if (vif->tx_irq) { 464 if (vif->tx_irq) {
429 if (vif->tx_irq == vif->rx_irq) 465 if (vif->tx_irq == vif->rx_irq)
430 unbind_from_irqhandler(vif->tx_irq, vif); 466 unbind_from_irqhandler(vif->tx_irq, vif);
@@ -438,6 +474,11 @@ void xenvif_disconnect(struct xenvif *vif)
438 need_module_put = 1; 474 need_module_put = 1;
439 } 475 }
440 476
477 if (vif->task)
478 kthread_stop(vif->task);
479
480 netif_napi_del(&vif->napi);
481
441 unregister_netdev(vif->dev); 482 unregister_netdev(vif->dev);
442 483
443 xen_netbk_unmap_frontend_rings(vif); 484 xen_netbk_unmap_frontend_rings(vif);