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