Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 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 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 33 | #include <linux/kthread.h> |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 34 | #include <linux/ethtool.h> |
| 35 | #include <linux/rtnetlink.h> |
| 36 | #include <linux/if_vlan.h> |
| 37 | |
| 38 | #include <xen/events.h> |
| 39 | #include <asm/xen/hypercall.h> |
| 40 | |
| 41 | #define XENVIF_QUEUE_LENGTH 32 |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 42 | #define XENVIF_NAPI_WEIGHT 64 |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 43 | |
| 44 | int xenvif_schedulable(struct xenvif *vif) |
| 45 | { |
| 46 | return netif_running(vif->dev) && netif_carrier_ok(vif->dev); |
| 47 | } |
| 48 | |
| 49 | static int xenvif_rx_schedulable(struct xenvif *vif) |
| 50 | { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 51 | return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 54 | static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 55 | { |
| 56 | struct xenvif *vif = dev_id; |
| 57 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 58 | if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) |
| 59 | napi_schedule(&vif->napi); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 60 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 61 | return IRQ_HANDLED; |
| 62 | } |
| 63 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 64 | static 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 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 69 | work_done = xenvif_tx_action(vif, budget); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 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 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 103 | static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) |
| 104 | { |
| 105 | struct xenvif *vif = dev_id; |
| 106 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 107 | if (xenvif_rx_schedulable(vif)) |
| 108 | netif_wake_queue(vif->dev); |
| 109 | |
| 110 | return IRQ_HANDLED; |
| 111 | } |
| 112 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 113 | static irqreturn_t xenvif_interrupt(int irq, void *dev_id) |
| 114 | { |
| 115 | xenvif_tx_interrupt(irq, dev_id); |
| 116 | xenvif_rx_interrupt(irq, dev_id); |
| 117 | |
| 118 | return IRQ_HANDLED; |
| 119 | } |
| 120 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 121 | static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 122 | { |
| 123 | struct xenvif *vif = netdev_priv(dev); |
| 124 | |
| 125 | BUG_ON(skb->dev != dev); |
| 126 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 127 | /* Drop the packet if vif is not ready */ |
| 128 | if (vif->task == NULL) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 129 | goto drop; |
| 130 | |
| 131 | /* Drop the packet if the target domain has no receive buffers. */ |
| 132 | if (!xenvif_rx_schedulable(vif)) |
| 133 | goto drop; |
| 134 | |
| 135 | /* Reserve ring slots for the worst-case number of fragments. */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 136 | vif->rx_req_cons_peek += xenvif_count_skb_slots(vif, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 137 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 138 | if (vif->can_queue && xenvif_must_stop_queue(vif)) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 139 | netif_stop_queue(dev); |
| 140 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 141 | xenvif_queue_tx_skb(vif, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 142 | |
| 143 | return NETDEV_TX_OK; |
| 144 | |
| 145 | drop: |
| 146 | vif->dev->stats.tx_dropped++; |
| 147 | dev_kfree_skb(skb); |
| 148 | return NETDEV_TX_OK; |
| 149 | } |
| 150 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 151 | void xenvif_notify_tx_completion(struct xenvif *vif) |
| 152 | { |
| 153 | if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif)) |
| 154 | netif_wake_queue(vif->dev); |
| 155 | } |
| 156 | |
| 157 | static struct net_device_stats *xenvif_get_stats(struct net_device *dev) |
| 158 | { |
| 159 | struct xenvif *vif = netdev_priv(dev); |
| 160 | return &vif->dev->stats; |
| 161 | } |
| 162 | |
| 163 | static void xenvif_up(struct xenvif *vif) |
| 164 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 165 | napi_enable(&vif->napi); |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 166 | enable_irq(vif->tx_irq); |
| 167 | if (vif->tx_irq != vif->rx_irq) |
| 168 | enable_irq(vif->rx_irq); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 169 | xenvif_check_rx_xenvif(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void xenvif_down(struct xenvif *vif) |
| 173 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 174 | napi_disable(&vif->napi); |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 175 | disable_irq(vif->tx_irq); |
| 176 | if (vif->tx_irq != vif->rx_irq) |
| 177 | disable_irq(vif->rx_irq); |
David Vrabel | 3e55f8b | 2013-02-14 03:18:58 +0000 | [diff] [blame] | 178 | del_timer_sync(&vif->credit_timeout); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int xenvif_open(struct net_device *dev) |
| 182 | { |
| 183 | struct xenvif *vif = netdev_priv(dev); |
| 184 | if (netif_carrier_ok(dev)) |
| 185 | xenvif_up(vif); |
| 186 | netif_start_queue(dev); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int xenvif_close(struct net_device *dev) |
| 191 | { |
| 192 | struct xenvif *vif = netdev_priv(dev); |
| 193 | if (netif_carrier_ok(dev)) |
| 194 | xenvif_down(vif); |
| 195 | netif_stop_queue(dev); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int xenvif_change_mtu(struct net_device *dev, int mtu) |
| 200 | { |
| 201 | struct xenvif *vif = netdev_priv(dev); |
| 202 | int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN; |
| 203 | |
| 204 | if (mtu > max) |
| 205 | return -EINVAL; |
| 206 | dev->mtu = mtu; |
| 207 | return 0; |
| 208 | } |
| 209 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 210 | static netdev_features_t xenvif_fix_features(struct net_device *dev, |
| 211 | netdev_features_t features) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 212 | { |
| 213 | struct xenvif *vif = netdev_priv(dev); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 214 | |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 215 | if (!vif->can_sg) |
| 216 | features &= ~NETIF_F_SG; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 217 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4)) |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 218 | features &= ~NETIF_F_TSO; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 219 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6)) |
| 220 | features &= ~NETIF_F_TSO6; |
Paul Durrant | 146c8a7 | 2013-10-16 17:50:28 +0100 | [diff] [blame] | 221 | if (!vif->ip_csum) |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 222 | features &= ~NETIF_F_IP_CSUM; |
Paul Durrant | 146c8a7 | 2013-10-16 17:50:28 +0100 | [diff] [blame] | 223 | if (!vif->ipv6_csum) |
| 224 | features &= ~NETIF_F_IPV6_CSUM; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 225 | |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 226 | return features; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static const struct xenvif_stat { |
| 230 | char name[ETH_GSTRING_LEN]; |
| 231 | u16 offset; |
| 232 | } xenvif_stats[] = { |
| 233 | { |
| 234 | "rx_gso_checksum_fixup", |
| 235 | offsetof(struct xenvif, rx_gso_checksum_fixup) |
| 236 | }, |
| 237 | }; |
| 238 | |
| 239 | static int xenvif_get_sset_count(struct net_device *dev, int string_set) |
| 240 | { |
| 241 | switch (string_set) { |
| 242 | case ETH_SS_STATS: |
| 243 | return ARRAY_SIZE(xenvif_stats); |
| 244 | default: |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void xenvif_get_ethtool_stats(struct net_device *dev, |
| 250 | struct ethtool_stats *stats, u64 * data) |
| 251 | { |
| 252 | void *vif = netdev_priv(dev); |
| 253 | int i; |
| 254 | |
| 255 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) |
| 256 | data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset); |
| 257 | } |
| 258 | |
| 259 | static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | switch (stringset) { |
| 264 | case ETH_SS_STATS: |
| 265 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) |
| 266 | memcpy(data + i * ETH_GSTRING_LEN, |
| 267 | xenvif_stats[i].name, ETH_GSTRING_LEN); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
stephen hemminger | 813abbb | 2012-01-04 11:56:58 +0000 | [diff] [blame] | 272 | static const struct ethtool_ops xenvif_ethtool_ops = { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 273 | .get_link = ethtool_op_get_link, |
| 274 | |
| 275 | .get_sset_count = xenvif_get_sset_count, |
| 276 | .get_ethtool_stats = xenvif_get_ethtool_stats, |
| 277 | .get_strings = xenvif_get_strings, |
| 278 | }; |
| 279 | |
stephen hemminger | 813abbb | 2012-01-04 11:56:58 +0000 | [diff] [blame] | 280 | static const struct net_device_ops xenvif_netdev_ops = { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 281 | .ndo_start_xmit = xenvif_start_xmit, |
| 282 | .ndo_get_stats = xenvif_get_stats, |
| 283 | .ndo_open = xenvif_open, |
| 284 | .ndo_stop = xenvif_close, |
| 285 | .ndo_change_mtu = xenvif_change_mtu, |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 286 | .ndo_fix_features = xenvif_fix_features, |
Matt Wilson | 4a633a6 | 2013-01-22 08:08:25 +0000 | [diff] [blame] | 287 | .ndo_set_mac_address = eth_mac_addr, |
| 288 | .ndo_validate_addr = eth_validate_addr, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | struct xenvif *xenvif_alloc(struct device *parent, domid_t domid, |
| 292 | unsigned int handle) |
| 293 | { |
| 294 | int err; |
| 295 | struct net_device *dev; |
| 296 | struct xenvif *vif; |
| 297 | char name[IFNAMSIZ] = {}; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 298 | int i; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 299 | |
| 300 | snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); |
| 301 | dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); |
| 302 | if (dev == NULL) { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 303 | pr_warn("Could not allocate netdev for %s\n", name); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 304 | return ERR_PTR(-ENOMEM); |
| 305 | } |
| 306 | |
| 307 | SET_NETDEV_DEV(dev, parent); |
| 308 | |
| 309 | vif = netdev_priv(dev); |
| 310 | vif->domid = domid; |
| 311 | vif->handle = handle; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 312 | vif->can_sg = 1; |
Paul Durrant | 146c8a7 | 2013-10-16 17:50:28 +0100 | [diff] [blame] | 313 | vif->ip_csum = 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 314 | vif->dev = dev; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 315 | |
| 316 | vif->credit_bytes = vif->remaining_credit = ~0UL; |
| 317 | vif->credit_usec = 0UL; |
| 318 | init_timer(&vif->credit_timeout); |
Wei Liu | 059dfa6 | 2013-10-28 12:07:57 +0000 | [diff] [blame] | 319 | vif->credit_window_start = get_jiffies_64(); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 320 | |
| 321 | dev->netdev_ops = &xenvif_netdev_ops; |
Paul Durrant | 146c8a7 | 2013-10-16 17:50:28 +0100 | [diff] [blame] | 322 | dev->hw_features = NETIF_F_SG | |
| 323 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 324 | NETIF_F_TSO | NETIF_F_TSO6; |
Paul Durrant | 7365bcf | 2013-10-16 17:50:30 +0100 | [diff] [blame] | 325 | dev->features = dev->hw_features | NETIF_F_RXCSUM; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 326 | SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops); |
| 327 | |
| 328 | dev->tx_queue_len = XENVIF_QUEUE_LENGTH; |
| 329 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 330 | skb_queue_head_init(&vif->rx_queue); |
| 331 | skb_queue_head_init(&vif->tx_queue); |
| 332 | |
| 333 | vif->pending_cons = 0; |
| 334 | vif->pending_prod = MAX_PENDING_REQS; |
| 335 | for (i = 0; i < MAX_PENDING_REQS; i++) |
| 336 | vif->pending_ring[i] = i; |
| 337 | for (i = 0; i < MAX_PENDING_REQS; i++) |
| 338 | vif->mmap_pages[i] = NULL; |
| 339 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 340 | /* |
| 341 | * Initialise a dummy MAC address. We choose the numerically |
| 342 | * largest non-broadcast address to prevent the address getting |
| 343 | * stolen by an Ethernet bridge for STP purposes. |
| 344 | * (FE:FF:FF:FF:FF:FF) |
| 345 | */ |
| 346 | memset(dev->dev_addr, 0xFF, ETH_ALEN); |
| 347 | dev->dev_addr[0] &= ~0x01; |
| 348 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 349 | netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT); |
| 350 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 351 | netif_carrier_off(dev); |
| 352 | |
| 353 | err = register_netdev(dev); |
| 354 | if (err) { |
| 355 | netdev_warn(dev, "Could not register device: err=%d\n", err); |
| 356 | free_netdev(dev); |
| 357 | return ERR_PTR(err); |
| 358 | } |
| 359 | |
| 360 | netdev_dbg(dev, "Successfully created xenvif\n"); |
Paul Durrant | 279f438 | 2013-09-17 17:46:08 +0100 | [diff] [blame] | 361 | |
| 362 | __module_get(THIS_MODULE); |
| 363 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 364 | return vif; |
| 365 | } |
| 366 | |
| 367 | int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref, |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 368 | unsigned long rx_ring_ref, unsigned int tx_evtchn, |
| 369 | unsigned int rx_evtchn) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 370 | { |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 371 | struct task_struct *task; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 372 | int err = -ENOMEM; |
| 373 | |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 374 | BUG_ON(vif->tx_irq); |
| 375 | BUG_ON(vif->task); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 376 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 377 | err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 378 | if (err < 0) |
| 379 | goto err; |
| 380 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 381 | if (tx_evtchn == rx_evtchn) { |
| 382 | /* feature-split-event-channels == 0 */ |
| 383 | err = bind_interdomain_evtchn_to_irqhandler( |
| 384 | vif->domid, tx_evtchn, xenvif_interrupt, 0, |
| 385 | vif->dev->name, vif); |
| 386 | if (err < 0) |
| 387 | goto err_unmap; |
| 388 | vif->tx_irq = vif->rx_irq = err; |
| 389 | disable_irq(vif->tx_irq); |
| 390 | } else { |
| 391 | /* feature-split-event-channels == 1 */ |
| 392 | snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name), |
| 393 | "%s-tx", vif->dev->name); |
| 394 | err = bind_interdomain_evtchn_to_irqhandler( |
| 395 | vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, |
| 396 | vif->tx_irq_name, vif); |
| 397 | if (err < 0) |
| 398 | goto err_unmap; |
| 399 | vif->tx_irq = err; |
| 400 | disable_irq(vif->tx_irq); |
| 401 | |
| 402 | snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name), |
| 403 | "%s-rx", vif->dev->name); |
| 404 | err = bind_interdomain_evtchn_to_irqhandler( |
| 405 | vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, |
| 406 | vif->rx_irq_name, vif); |
| 407 | if (err < 0) |
| 408 | goto err_tx_unbind; |
| 409 | vif->rx_irq = err; |
| 410 | disable_irq(vif->rx_irq); |
| 411 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 412 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 413 | init_waitqueue_head(&vif->wq); |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 414 | task = kthread_create(xenvif_kthread, |
| 415 | (void *)vif, "%s", vif->dev->name); |
| 416 | if (IS_ERR(task)) { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 417 | pr_warn("Could not allocate kthread for %s\n", vif->dev->name); |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 418 | err = PTR_ERR(task); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 419 | goto err_rx_unbind; |
| 420 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 421 | |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 422 | vif->task = task; |
| 423 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 424 | rtnl_lock(); |
Michał Mirosław | 4710304 | 2011-04-19 03:35:06 +0000 | [diff] [blame] | 425 | if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) |
| 426 | dev_set_mtu(vif->dev, ETH_DATA_LEN); |
| 427 | netdev_update_features(vif->dev); |
| 428 | netif_carrier_on(vif->dev); |
David Vrabel | d0e5d83 | 2011-09-30 06:37:51 +0000 | [diff] [blame] | 429 | if (netif_running(vif->dev)) |
| 430 | xenvif_up(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 431 | rtnl_unlock(); |
| 432 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 433 | wake_up_process(vif->task); |
| 434 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 435 | return 0; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 436 | |
| 437 | err_rx_unbind: |
| 438 | unbind_from_irqhandler(vif->rx_irq, vif); |
| 439 | vif->rx_irq = 0; |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 440 | err_tx_unbind: |
| 441 | unbind_from_irqhandler(vif->tx_irq, vif); |
| 442 | vif->tx_irq = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 443 | err_unmap: |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 444 | xenvif_unmap_frontend_rings(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 445 | err: |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 446 | module_put(THIS_MODULE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 447 | return err; |
| 448 | } |
| 449 | |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 450 | void xenvif_carrier_off(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 451 | { |
| 452 | struct net_device *dev = vif->dev; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 453 | |
| 454 | rtnl_lock(); |
| 455 | netif_carrier_off(dev); /* discard queued packets */ |
| 456 | if (netif_running(dev)) |
| 457 | xenvif_down(vif); |
| 458 | rtnl_unlock(); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void xenvif_disconnect(struct xenvif *vif) |
| 462 | { |
| 463 | if (netif_carrier_ok(vif->dev)) |
| 464 | xenvif_carrier_off(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 465 | |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 466 | if (vif->task) { |
David Vrabel | db739ef | 2013-11-21 15:26:09 +0000 | [diff] [blame] | 467 | kthread_stop(vif->task); |
Paul Durrant | 67fa366 | 2013-12-03 14:06:25 +0000 | [diff] [blame] | 468 | vif->task = NULL; |
| 469 | } |
David Vrabel | db739ef | 2013-11-21 15:26:09 +0000 | [diff] [blame] | 470 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 471 | if (vif->tx_irq) { |
| 472 | if (vif->tx_irq == vif->rx_irq) |
| 473 | unbind_from_irqhandler(vif->tx_irq, vif); |
| 474 | else { |
| 475 | unbind_from_irqhandler(vif->tx_irq, vif); |
| 476 | unbind_from_irqhandler(vif->rx_irq, vif); |
| 477 | } |
Paul Durrant | 279f438 | 2013-09-17 17:46:08 +0100 | [diff] [blame] | 478 | vif->tx_irq = 0; |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 479 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 480 | |
Paul Durrant | 279f438 | 2013-09-17 17:46:08 +0100 | [diff] [blame] | 481 | xenvif_unmap_frontend_rings(vif); |
| 482 | } |
| 483 | |
| 484 | void xenvif_free(struct xenvif *vif) |
| 485 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 486 | netif_napi_del(&vif->napi); |
| 487 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 488 | unregister_netdev(vif->dev); |
| 489 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 490 | free_netdev(vif->dev); |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 491 | |
Paul Durrant | 279f438 | 2013-09-17 17:46:08 +0100 | [diff] [blame] | 492 | module_put(THIS_MODULE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 493 | } |