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