xen-netback: Using a new state bit instead of carrier
[cascardo/linux.git] / drivers / net / xen-netback / interface.c
1 /*
2  * Network-device interface management.
3  *
4  * Copyright (c) 2004-2005, Keir Fraser
5  *
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:
11  *
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:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
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.
29  */
30
31 #include "common.h"
32
33 #include <linux/kthread.h>
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/if_vlan.h>
37 #include <linux/vmalloc.h>
38
39 #include <xen/events.h>
40 #include <asm/xen/hypercall.h>
41 #include <xen/balloon.h>
42
43 #define XENVIF_QUEUE_LENGTH 32
44 #define XENVIF_NAPI_WEIGHT  64
45
46 static inline void xenvif_stop_queue(struct xenvif_queue *queue)
47 {
48         struct net_device *dev = queue->vif->dev;
49
50         if (!queue->vif->can_queue)
51                 return;
52
53         netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
54 }
55
56 int xenvif_schedulable(struct xenvif *vif)
57 {
58         return netif_running(vif->dev) &&
59                 test_bit(VIF_STATUS_CONNECTED, &vif->status);
60 }
61
62 static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
63 {
64         struct xenvif_queue *queue = dev_id;
65
66         if (RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))
67                 napi_schedule(&queue->napi);
68
69         return IRQ_HANDLED;
70 }
71
72 int xenvif_poll(struct napi_struct *napi, int budget)
73 {
74         struct xenvif_queue *queue =
75                 container_of(napi, struct xenvif_queue, napi);
76         int work_done;
77
78         /* This vif is rogue, we pretend we've there is nothing to do
79          * for this vif to deschedule it from NAPI. But this interface
80          * will be turned off in thread context later.
81          */
82         if (unlikely(queue->vif->disabled)) {
83                 napi_complete(napi);
84                 return 0;
85         }
86
87         work_done = xenvif_tx_action(queue, budget);
88
89         if (work_done < budget) {
90                 napi_complete(napi);
91                 xenvif_napi_schedule_or_enable_events(queue);
92         }
93
94         return work_done;
95 }
96
97 static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
98 {
99         struct xenvif_queue *queue = dev_id;
100
101         xenvif_kick_thread(queue);
102
103         return IRQ_HANDLED;
104 }
105
106 irqreturn_t xenvif_interrupt(int irq, void *dev_id)
107 {
108         xenvif_tx_interrupt(irq, dev_id);
109         xenvif_rx_interrupt(irq, dev_id);
110
111         return IRQ_HANDLED;
112 }
113
114 int xenvif_queue_stopped(struct xenvif_queue *queue)
115 {
116         struct net_device *dev = queue->vif->dev;
117         unsigned int id = queue->id;
118         return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id));
119 }
120
121 void xenvif_wake_queue(struct xenvif_queue *queue)
122 {
123         struct net_device *dev = queue->vif->dev;
124         unsigned int id = queue->id;
125         netif_tx_wake_queue(netdev_get_tx_queue(dev, id));
126 }
127
128 /* Callback to wake the queue and drain it on timeout */
129 static void xenvif_wake_queue_callback(unsigned long data)
130 {
131         struct xenvif_queue *queue = (struct xenvif_queue *)data;
132
133         if (xenvif_queue_stopped(queue)) {
134                 netdev_err(queue->vif->dev, "draining TX queue\n");
135                 queue->rx_queue_purge = true;
136                 xenvif_kick_thread(queue);
137                 xenvif_wake_queue(queue);
138         }
139 }
140
141 static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
142 {
143         struct xenvif *vif = netdev_priv(dev);
144         struct xenvif_queue *queue = NULL;
145         unsigned int num_queues = vif->num_queues;
146         u16 index;
147         int min_slots_needed;
148
149         BUG_ON(skb->dev != dev);
150
151         /* Drop the packet if queues are not set up */
152         if (num_queues < 1)
153                 goto drop;
154
155         /* Obtain the queue to be used to transmit this packet */
156         index = skb_get_queue_mapping(skb);
157         if (index >= num_queues) {
158                 pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.",
159                                     index, vif->dev->name);
160                 index %= num_queues;
161         }
162         queue = &vif->queues[index];
163
164         /* Drop the packet if queue is not ready */
165         if (queue->task == NULL ||
166             queue->dealloc_task == NULL ||
167             !xenvif_schedulable(vif))
168                 goto drop;
169
170         /* At best we'll need one slot for the header and one for each
171          * frag.
172          */
173         min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
174
175         /* If the skb is GSO then we'll also need an extra slot for the
176          * metadata.
177          */
178         if (skb_is_gso(skb))
179                 min_slots_needed++;
180
181         /* If the skb can't possibly fit in the remaining slots
182          * then turn off the queue to give the ring a chance to
183          * drain.
184          */
185         if (!xenvif_rx_ring_slots_available(queue, min_slots_needed)) {
186                 queue->wake_queue.function = xenvif_wake_queue_callback;
187                 queue->wake_queue.data = (unsigned long)queue;
188                 xenvif_stop_queue(queue);
189                 mod_timer(&queue->wake_queue,
190                         jiffies + rx_drain_timeout_jiffies);
191         }
192
193         skb_queue_tail(&queue->rx_queue, skb);
194         xenvif_kick_thread(queue);
195
196         return NETDEV_TX_OK;
197
198  drop:
199         vif->dev->stats.tx_dropped++;
200         dev_kfree_skb(skb);
201         return NETDEV_TX_OK;
202 }
203
204 static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
205 {
206         struct xenvif *vif = netdev_priv(dev);
207         struct xenvif_queue *queue = NULL;
208         unsigned int num_queues = vif->num_queues;
209         unsigned long rx_bytes = 0;
210         unsigned long rx_packets = 0;
211         unsigned long tx_bytes = 0;
212         unsigned long tx_packets = 0;
213         unsigned int index;
214
215         if (vif->queues == NULL)
216                 goto out;
217
218         /* Aggregate tx and rx stats from each queue */
219         for (index = 0; index < num_queues; ++index) {
220                 queue = &vif->queues[index];
221                 rx_bytes += queue->stats.rx_bytes;
222                 rx_packets += queue->stats.rx_packets;
223                 tx_bytes += queue->stats.tx_bytes;
224                 tx_packets += queue->stats.tx_packets;
225         }
226
227 out:
228         vif->dev->stats.rx_bytes = rx_bytes;
229         vif->dev->stats.rx_packets = rx_packets;
230         vif->dev->stats.tx_bytes = tx_bytes;
231         vif->dev->stats.tx_packets = tx_packets;
232
233         return &vif->dev->stats;
234 }
235
236 static void xenvif_up(struct xenvif *vif)
237 {
238         struct xenvif_queue *queue = NULL;
239         unsigned int num_queues = vif->num_queues;
240         unsigned int queue_index;
241
242         for (queue_index = 0; queue_index < num_queues; ++queue_index) {
243                 queue = &vif->queues[queue_index];
244                 napi_enable(&queue->napi);
245                 enable_irq(queue->tx_irq);
246                 if (queue->tx_irq != queue->rx_irq)
247                         enable_irq(queue->rx_irq);
248                 xenvif_napi_schedule_or_enable_events(queue);
249         }
250 }
251
252 static void xenvif_down(struct xenvif *vif)
253 {
254         struct xenvif_queue *queue = NULL;
255         unsigned int num_queues = vif->num_queues;
256         unsigned int queue_index;
257
258         for (queue_index = 0; queue_index < num_queues; ++queue_index) {
259                 queue = &vif->queues[queue_index];
260                 napi_disable(&queue->napi);
261                 disable_irq(queue->tx_irq);
262                 if (queue->tx_irq != queue->rx_irq)
263                         disable_irq(queue->rx_irq);
264                 del_timer_sync(&queue->credit_timeout);
265         }
266 }
267
268 static int xenvif_open(struct net_device *dev)
269 {
270         struct xenvif *vif = netdev_priv(dev);
271         if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
272                 xenvif_up(vif);
273         netif_tx_start_all_queues(dev);
274         return 0;
275 }
276
277 static int xenvif_close(struct net_device *dev)
278 {
279         struct xenvif *vif = netdev_priv(dev);
280         if (test_bit(VIF_STATUS_CONNECTED, &vif->status))
281                 xenvif_down(vif);
282         netif_tx_stop_all_queues(dev);
283         return 0;
284 }
285
286 static int xenvif_change_mtu(struct net_device *dev, int mtu)
287 {
288         struct xenvif *vif = netdev_priv(dev);
289         int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
290
291         if (mtu > max)
292                 return -EINVAL;
293         dev->mtu = mtu;
294         return 0;
295 }
296
297 static netdev_features_t xenvif_fix_features(struct net_device *dev,
298         netdev_features_t features)
299 {
300         struct xenvif *vif = netdev_priv(dev);
301
302         if (!vif->can_sg)
303                 features &= ~NETIF_F_SG;
304         if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
305                 features &= ~NETIF_F_TSO;
306         if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
307                 features &= ~NETIF_F_TSO6;
308         if (!vif->ip_csum)
309                 features &= ~NETIF_F_IP_CSUM;
310         if (!vif->ipv6_csum)
311                 features &= ~NETIF_F_IPV6_CSUM;
312
313         return features;
314 }
315
316 static const struct xenvif_stat {
317         char name[ETH_GSTRING_LEN];
318         u16 offset;
319 } xenvif_stats[] = {
320         {
321                 "rx_gso_checksum_fixup",
322                 offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
323         },
324         /* If (sent != success + fail), there are probably packets never
325          * freed up properly!
326          */
327         {
328                 "tx_zerocopy_sent",
329                 offsetof(struct xenvif_stats, tx_zerocopy_sent),
330         },
331         {
332                 "tx_zerocopy_success",
333                 offsetof(struct xenvif_stats, tx_zerocopy_success),
334         },
335         {
336                 "tx_zerocopy_fail",
337                 offsetof(struct xenvif_stats, tx_zerocopy_fail)
338         },
339         /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
340          * a guest with the same MAX_SKB_FRAG
341          */
342         {
343                 "tx_frag_overflow",
344                 offsetof(struct xenvif_stats, tx_frag_overflow)
345         },
346 };
347
348 static int xenvif_get_sset_count(struct net_device *dev, int string_set)
349 {
350         switch (string_set) {
351         case ETH_SS_STATS:
352                 return ARRAY_SIZE(xenvif_stats);
353         default:
354                 return -EINVAL;
355         }
356 }
357
358 static void xenvif_get_ethtool_stats(struct net_device *dev,
359                                      struct ethtool_stats *stats, u64 * data)
360 {
361         struct xenvif *vif = netdev_priv(dev);
362         unsigned int num_queues = vif->num_queues;
363         int i;
364         unsigned int queue_index;
365         struct xenvif_stats *vif_stats;
366
367         for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
368                 unsigned long accum = 0;
369                 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
370                         vif_stats = &vif->queues[queue_index].stats;
371                         accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
372                 }
373                 data[i] = accum;
374         }
375 }
376
377 static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
378 {
379         int i;
380
381         switch (stringset) {
382         case ETH_SS_STATS:
383                 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
384                         memcpy(data + i * ETH_GSTRING_LEN,
385                                xenvif_stats[i].name, ETH_GSTRING_LEN);
386                 break;
387         }
388 }
389
390 static const struct ethtool_ops xenvif_ethtool_ops = {
391         .get_link       = ethtool_op_get_link,
392
393         .get_sset_count = xenvif_get_sset_count,
394         .get_ethtool_stats = xenvif_get_ethtool_stats,
395         .get_strings = xenvif_get_strings,
396 };
397
398 static const struct net_device_ops xenvif_netdev_ops = {
399         .ndo_start_xmit = xenvif_start_xmit,
400         .ndo_get_stats  = xenvif_get_stats,
401         .ndo_open       = xenvif_open,
402         .ndo_stop       = xenvif_close,
403         .ndo_change_mtu = xenvif_change_mtu,
404         .ndo_fix_features = xenvif_fix_features,
405         .ndo_set_mac_address = eth_mac_addr,
406         .ndo_validate_addr   = eth_validate_addr,
407 };
408
409 struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
410                             unsigned int handle)
411 {
412         int err;
413         struct net_device *dev;
414         struct xenvif *vif;
415         char name[IFNAMSIZ] = {};
416
417         snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
418         /* Allocate a netdev with the max. supported number of queues.
419          * When the guest selects the desired number, it will be updated
420          * via netif_set_real_num_*_queues().
421          */
422         dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN,
423                               ether_setup, xenvif_max_queues);
424         if (dev == NULL) {
425                 pr_warn("Could not allocate netdev for %s\n", name);
426                 return ERR_PTR(-ENOMEM);
427         }
428
429         SET_NETDEV_DEV(dev, parent);
430
431         vif = netdev_priv(dev);
432
433         vif->domid  = domid;
434         vif->handle = handle;
435         vif->can_sg = 1;
436         vif->ip_csum = 1;
437         vif->dev = dev;
438         vif->disabled = false;
439
440         /* Start out with no queues. */
441         vif->queues = NULL;
442         vif->num_queues = 0;
443
444         dev->netdev_ops = &xenvif_netdev_ops;
445         dev->hw_features = NETIF_F_SG |
446                 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
447                 NETIF_F_TSO | NETIF_F_TSO6;
448         dev->features = dev->hw_features | NETIF_F_RXCSUM;
449         dev->ethtool_ops = &xenvif_ethtool_ops;
450
451         dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
452
453         /*
454          * Initialise a dummy MAC address. We choose the numerically
455          * largest non-broadcast address to prevent the address getting
456          * stolen by an Ethernet bridge for STP purposes.
457          * (FE:FF:FF:FF:FF:FF)
458          */
459         memset(dev->dev_addr, 0xFF, ETH_ALEN);
460         dev->dev_addr[0] &= ~0x01;
461
462         netif_carrier_off(dev);
463
464         err = register_netdev(dev);
465         if (err) {
466                 netdev_warn(dev, "Could not register device: err=%d\n", err);
467                 free_netdev(dev);
468                 return ERR_PTR(err);
469         }
470
471         netdev_dbg(dev, "Successfully created xenvif\n");
472
473         __module_get(THIS_MODULE);
474
475         return vif;
476 }
477
478 int xenvif_init_queue(struct xenvif_queue *queue)
479 {
480         int err, i;
481
482         queue->credit_bytes = queue->remaining_credit = ~0UL;
483         queue->credit_usec  = 0UL;
484         init_timer(&queue->credit_timeout);
485         queue->credit_window_start = get_jiffies_64();
486
487         skb_queue_head_init(&queue->rx_queue);
488         skb_queue_head_init(&queue->tx_queue);
489
490         queue->pending_cons = 0;
491         queue->pending_prod = MAX_PENDING_REQS;
492         for (i = 0; i < MAX_PENDING_REQS; ++i)
493                 queue->pending_ring[i] = i;
494
495         spin_lock_init(&queue->callback_lock);
496         spin_lock_init(&queue->response_lock);
497
498         /* If ballooning is disabled, this will consume real memory, so you
499          * better enable it. The long term solution would be to use just a
500          * bunch of valid page descriptors, without dependency on ballooning
501          */
502         err = alloc_xenballooned_pages(MAX_PENDING_REQS,
503                                        queue->mmap_pages,
504                                        false);
505         if (err) {
506                 netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
507                 return -ENOMEM;
508         }
509
510         for (i = 0; i < MAX_PENDING_REQS; i++) {
511                 queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
512                         { .callback = xenvif_zerocopy_callback,
513                           .ctx = NULL,
514                           .desc = i };
515                 queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
516         }
517
518         init_timer(&queue->wake_queue);
519
520         netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
521                         XENVIF_NAPI_WEIGHT);
522
523         return 0;
524 }
525
526 void xenvif_carrier_on(struct xenvif *vif)
527 {
528         rtnl_lock();
529         if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
530                 dev_set_mtu(vif->dev, ETH_DATA_LEN);
531         netdev_update_features(vif->dev);
532         set_bit(VIF_STATUS_CONNECTED, &vif->status);
533         netif_carrier_on(vif->dev);
534         if (netif_running(vif->dev))
535                 xenvif_up(vif);
536         rtnl_unlock();
537 }
538
539 int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref,
540                    unsigned long rx_ring_ref, unsigned int tx_evtchn,
541                    unsigned int rx_evtchn)
542 {
543         struct task_struct *task;
544         int err = -ENOMEM;
545
546         BUG_ON(queue->tx_irq);
547         BUG_ON(queue->task);
548         BUG_ON(queue->dealloc_task);
549
550         err = xenvif_map_frontend_rings(queue, tx_ring_ref, rx_ring_ref);
551         if (err < 0)
552                 goto err;
553
554         init_waitqueue_head(&queue->wq);
555         init_waitqueue_head(&queue->dealloc_wq);
556
557         if (tx_evtchn == rx_evtchn) {
558                 /* feature-split-event-channels == 0 */
559                 err = bind_interdomain_evtchn_to_irqhandler(
560                         queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
561                         queue->name, queue);
562                 if (err < 0)
563                         goto err_unmap;
564                 queue->tx_irq = queue->rx_irq = err;
565                 disable_irq(queue->tx_irq);
566         } else {
567                 /* feature-split-event-channels == 1 */
568                 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
569                          "%s-tx", queue->name);
570                 err = bind_interdomain_evtchn_to_irqhandler(
571                         queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
572                         queue->tx_irq_name, queue);
573                 if (err < 0)
574                         goto err_unmap;
575                 queue->tx_irq = err;
576                 disable_irq(queue->tx_irq);
577
578                 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
579                          "%s-rx", queue->name);
580                 err = bind_interdomain_evtchn_to_irqhandler(
581                         queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
582                         queue->rx_irq_name, queue);
583                 if (err < 0)
584                         goto err_tx_unbind;
585                 queue->rx_irq = err;
586                 disable_irq(queue->rx_irq);
587         }
588
589         task = kthread_create(xenvif_kthread_guest_rx,
590                               (void *)queue, "%s-guest-rx", queue->name);
591         if (IS_ERR(task)) {
592                 pr_warn("Could not allocate kthread for %s\n", queue->name);
593                 err = PTR_ERR(task);
594                 goto err_rx_unbind;
595         }
596         queue->task = task;
597
598         task = kthread_create(xenvif_dealloc_kthread,
599                               (void *)queue, "%s-dealloc", queue->name);
600         if (IS_ERR(task)) {
601                 pr_warn("Could not allocate kthread for %s\n", queue->name);
602                 err = PTR_ERR(task);
603                 goto err_rx_unbind;
604         }
605         queue->dealloc_task = task;
606
607         wake_up_process(queue->task);
608         wake_up_process(queue->dealloc_task);
609
610         return 0;
611
612 err_rx_unbind:
613         unbind_from_irqhandler(queue->rx_irq, queue);
614         queue->rx_irq = 0;
615 err_tx_unbind:
616         unbind_from_irqhandler(queue->tx_irq, queue);
617         queue->tx_irq = 0;
618 err_unmap:
619         xenvif_unmap_frontend_rings(queue);
620 err:
621         module_put(THIS_MODULE);
622         return err;
623 }
624
625 void xenvif_carrier_off(struct xenvif *vif)
626 {
627         struct net_device *dev = vif->dev;
628
629         rtnl_lock();
630         if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) {
631                 netif_carrier_off(dev); /* discard queued packets */
632                 if (netif_running(dev))
633                         xenvif_down(vif);
634         }
635         rtnl_unlock();
636 }
637
638 static void xenvif_wait_unmap_timeout(struct xenvif_queue *queue,
639                                       unsigned int worst_case_skb_lifetime)
640 {
641         int i, unmap_timeout = 0;
642
643         for (i = 0; i < MAX_PENDING_REQS; ++i) {
644                 if (queue->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
645                         unmap_timeout++;
646                         schedule_timeout(msecs_to_jiffies(1000));
647                         if (unmap_timeout > worst_case_skb_lifetime &&
648                             net_ratelimit())
649                                 netdev_err(queue->vif->dev,
650                                            "Page still granted! Index: %x\n",
651                                            i);
652                         i = -1;
653                 }
654         }
655 }
656
657 void xenvif_disconnect(struct xenvif *vif)
658 {
659         struct xenvif_queue *queue = NULL;
660         unsigned int num_queues = vif->num_queues;
661         unsigned int queue_index;
662
663         xenvif_carrier_off(vif);
664
665         for (queue_index = 0; queue_index < num_queues; ++queue_index) {
666                 queue = &vif->queues[queue_index];
667
668                 if (queue->task) {
669                         del_timer_sync(&queue->wake_queue);
670                         kthread_stop(queue->task);
671                         queue->task = NULL;
672                 }
673
674                 if (queue->dealloc_task) {
675                         kthread_stop(queue->dealloc_task);
676                         queue->dealloc_task = NULL;
677                 }
678
679                 if (queue->tx_irq) {
680                         if (queue->tx_irq == queue->rx_irq)
681                                 unbind_from_irqhandler(queue->tx_irq, queue);
682                         else {
683                                 unbind_from_irqhandler(queue->tx_irq, queue);
684                                 unbind_from_irqhandler(queue->rx_irq, queue);
685                         }
686                         queue->tx_irq = 0;
687                 }
688
689                 xenvif_unmap_frontend_rings(queue);
690         }
691 }
692
693 /* Reverse the relevant parts of xenvif_init_queue().
694  * Used for queue teardown from xenvif_free(), and on the
695  * error handling paths in xenbus.c:connect().
696  */
697 void xenvif_deinit_queue(struct xenvif_queue *queue)
698 {
699         free_xenballooned_pages(MAX_PENDING_REQS, queue->mmap_pages);
700         netif_napi_del(&queue->napi);
701 }
702
703 void xenvif_free(struct xenvif *vif)
704 {
705         struct xenvif_queue *queue = NULL;
706         unsigned int num_queues = vif->num_queues;
707         unsigned int queue_index;
708         /* Here we want to avoid timeout messages if an skb can be legitimately
709          * stuck somewhere else. Realistically this could be an another vif's
710          * internal or QDisc queue. That another vif also has this
711          * rx_drain_timeout_msecs timeout, but the timer only ditches the
712          * internal queue. After that, the QDisc queue can put in worst case
713          * XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS skbs into that another vif's
714          * internal queue, so we need several rounds of such timeouts until we
715          * can be sure that no another vif should have skb's from us. We are
716          * not sending more skb's, so newly stuck packets are not interesting
717          * for us here.
718          */
719         unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) *
720                 DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS));
721
722         unregister_netdev(vif->dev);
723
724         for (queue_index = 0; queue_index < num_queues; ++queue_index) {
725                 queue = &vif->queues[queue_index];
726                 xenvif_wait_unmap_timeout(queue, worst_case_skb_lifetime);
727                 xenvif_deinit_queue(queue);
728         }
729
730         vfree(vif->queues);
731         vif->queues = NULL;
732         vif->num_queues = 0;
733
734         free_netdev(vif->dev);
735
736         module_put(THIS_MODULE);
737 }