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.c159
1 files changed, 97 insertions, 62 deletions
diff --git a/drivers/net/xen-netback/interface.c b/drivers/net/xen-netback/interface.c
index 087d2db0389d..01bb854c7f62 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{
@@ -57,28 +48,62 @@ int xenvif_schedulable(struct xenvif *vif)
57 48
58static int xenvif_rx_schedulable(struct xenvif *vif) 49static int xenvif_rx_schedulable(struct xenvif *vif)
59{ 50{
60 return xenvif_schedulable(vif) && !xen_netbk_rx_ring_full(vif); 51 return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif);
61} 52}
62 53
63static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) 54static 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 = xenvif_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. */
@@ -107,13 +133,12 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
107 goto drop; 133 goto drop;
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 += xenvif_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 && xenvif_must_stop_queue(vif))
114 netif_stop_queue(dev); 139 netif_stop_queue(dev);
115 140
116 xen_netbk_queue_tx_skb(vif, skb); 141 xenvif_queue_tx_skb(vif, skb);
117 142
118 return NETDEV_TX_OK; 143 return NETDEV_TX_OK;
119 144
@@ -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,21 +162,20 @@ 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);
149 xen_netbk_check_rx_xenvif(vif); 169 xenvif_check_rx_xenvif(vif);
150} 170}
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);
@@ -326,6 +353,9 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
326 } 353 }
327 354
328 netdev_dbg(dev, "Successfully created xenvif\n"); 355 netdev_dbg(dev, "Successfully created xenvif\n");
356
357 __module_get(THIS_MODULE);
358
329 return vif; 359 return vif;
330} 360}
331 361
@@ -339,9 +369,7 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
339 if (vif->tx_irq) 369 if (vif->tx_irq)
340 return 0; 370 return 0;
341 371
342 __module_get(THIS_MODULE); 372 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
343
344 err = xen_netbk_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
345 if (err < 0) 373 if (err < 0)
346 goto err; 374 goto err;
347 375
@@ -377,7 +405,14 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
377 disable_irq(vif->rx_irq); 405 disable_irq(vif->rx_irq);
378 } 406 }
379 407
380 xenvif_get(vif); 408 init_waitqueue_head(&vif->wq);
409 vif->task = kthread_create(xenvif_kthread,
410 (void *)vif, "%s", vif->dev->name);
411 if (IS_ERR(vif->task)) {
412 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
413 err = PTR_ERR(vif->task);
414 goto err_rx_unbind;
415 }
381 416
382 rtnl_lock(); 417 rtnl_lock();
383 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) 418 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
@@ -388,12 +423,18 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
388 xenvif_up(vif); 423 xenvif_up(vif);
389 rtnl_unlock(); 424 rtnl_unlock();
390 425
426 wake_up_process(vif->task);
427
391 return 0; 428 return 0;
429
430err_rx_unbind:
431 unbind_from_irqhandler(vif->rx_irq, vif);
432 vif->rx_irq = 0;
392err_tx_unbind: 433err_tx_unbind:
393 unbind_from_irqhandler(vif->tx_irq, vif); 434 unbind_from_irqhandler(vif->tx_irq, vif);
394 vif->tx_irq = 0; 435 vif->tx_irq = 0;
395err_unmap: 436err_unmap:
396 xen_netbk_unmap_frontend_rings(vif); 437 xenvif_unmap_frontend_rings(vif);
397err: 438err:
398 module_put(THIS_MODULE); 439 module_put(THIS_MODULE);
399 return err; 440 return err;
@@ -408,23 +449,13 @@ void xenvif_carrier_off(struct xenvif *vif)
408 if (netif_running(dev)) 449 if (netif_running(dev))
409 xenvif_down(vif); 450 xenvif_down(vif);
410 rtnl_unlock(); 451 rtnl_unlock();
411 xenvif_put(vif);
412} 452}
413 453
414void xenvif_disconnect(struct xenvif *vif) 454void xenvif_disconnect(struct xenvif *vif)
415{ 455{
416 /* Disconnect funtion might get called by generic framework
417 * even before vif connects, so we need to check if we really
418 * need to do a module_put.
419 */
420 int need_module_put = 0;
421
422 if (netif_carrier_ok(vif->dev)) 456 if (netif_carrier_ok(vif->dev))
423 xenvif_carrier_off(vif); 457 xenvif_carrier_off(vif);
424 458
425 atomic_dec(&vif->refcnt);
426 wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
427
428 if (vif->tx_irq) { 459 if (vif->tx_irq) {
429 if (vif->tx_irq == vif->rx_irq) 460 if (vif->tx_irq == vif->rx_irq)
430 unbind_from_irqhandler(vif->tx_irq, vif); 461 unbind_from_irqhandler(vif->tx_irq, vif);
@@ -432,18 +463,22 @@ void xenvif_disconnect(struct xenvif *vif)
432 unbind_from_irqhandler(vif->tx_irq, vif); 463 unbind_from_irqhandler(vif->tx_irq, vif);
433 unbind_from_irqhandler(vif->rx_irq, vif); 464 unbind_from_irqhandler(vif->rx_irq, vif);
434 } 465 }
435 /* vif->irq is valid, we had a module_get in 466 vif->tx_irq = 0;
436 * xenvif_connect.
437 */
438 need_module_put = 1;
439 } 467 }
440 468
441 unregister_netdev(vif->dev); 469 if (vif->task)
470 kthread_stop(vif->task);
471
472 xenvif_unmap_frontend_rings(vif);
473}
442 474
443 xen_netbk_unmap_frontend_rings(vif); 475void xenvif_free(struct xenvif *vif)
476{
477 netif_napi_del(&vif->napi);
478
479 unregister_netdev(vif->dev);
444 480
445 free_netdev(vif->dev); 481 free_netdev(vif->dev);
446 482
447 if (need_module_put) 483 module_put(THIS_MODULE);
448 module_put(THIS_MODULE);
449} 484}