Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Virtual network driver for conversing with remote driver backends. |
| 3 | * |
| 4 | * Copyright (c) 2002-2005, K A Fraser |
| 5 | * Copyright (c) 2005, XenSource Ltd |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version 2 |
| 9 | * as published by the Free Software Foundation; or, when distributed |
| 10 | * separately from the Linux kernel or incorporated into other |
| 11 | * software packages, subject to the following license: |
| 12 | * |
| 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | * of this source file (the "Software"), to deal in the Software without |
| 15 | * restriction, including without limitation the rights to use, copy, modify, |
| 16 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 18 | * the following conditions: |
| 19 | * |
| 20 | * The above copyright notice and this permission notice shall be included in |
| 21 | * all copies or substantial portions of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 29 | * IN THE SOFTWARE. |
| 30 | */ |
| 31 | |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 33 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/netdevice.h> |
| 37 | #include <linux/etherdevice.h> |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/ethtool.h> |
| 40 | #include <linux/if_ether.h> |
Wei Liu | 9ecd1a7 | 2013-04-22 02:20:41 +0000 | [diff] [blame] | 41 | #include <net/tcp.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 42 | #include <linux/udp.h> |
| 43 | #include <linux/moduleparam.h> |
| 44 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 45 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | #include <net/ip.h> |
| 47 | |
Stefano Stabellini | ca98163 | 2012-08-08 17:21:23 +0000 | [diff] [blame] | 48 | #include <asm/xen/page.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 49 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 50 | #include <xen/xenbus.h> |
| 51 | #include <xen/events.h> |
| 52 | #include <xen/page.h> |
Igor Mammedov | b9136d2 | 2012-03-21 15:08:38 +0100 | [diff] [blame] | 53 | #include <xen/platform_pci.h> |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 54 | #include <xen/grant_table.h> |
| 55 | |
| 56 | #include <xen/interface/io/netif.h> |
| 57 | #include <xen/interface/memory.h> |
| 58 | #include <xen/interface/grant_table.h> |
| 59 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 60 | /* Module parameters */ |
| 61 | static unsigned int xennet_max_queues; |
| 62 | module_param_named(max_queues, xennet_max_queues, uint, 0644); |
| 63 | MODULE_PARM_DESC(max_queues, |
| 64 | "Maximum number of queues per virtual interface"); |
| 65 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 66 | static const struct ethtool_ops xennet_ethtool_ops; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 67 | |
| 68 | struct netfront_cb { |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 69 | int pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb)) |
| 73 | |
| 74 | #define RX_COPY_THRESHOLD 256 |
| 75 | |
| 76 | #define GRANT_INVALID_REF 0 |
| 77 | |
Jeremy Fitzhardinge | 667c78af | 2010-12-08 12:39:12 -0800 | [diff] [blame] | 78 | #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE) |
| 79 | #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE) |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 80 | |
| 81 | /* Minimum number of Rx slots (includes slot for GSO metadata). */ |
| 82 | #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 83 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 84 | /* Queue name is interface name with "-qNNN" appended */ |
| 85 | #define QUEUE_NAME_SIZE (IFNAMSIZ + 6) |
| 86 | |
| 87 | /* IRQ name is queue name with "-tx" or "-rx" appended */ |
| 88 | #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3) |
| 89 | |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 90 | struct netfront_stats { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 91 | u64 packets; |
| 92 | u64 bytes; |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 93 | struct u64_stats_sync syncp; |
| 94 | }; |
| 95 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 96 | struct netfront_info; |
| 97 | |
| 98 | struct netfront_queue { |
| 99 | unsigned int id; /* Queue ID, 0-based */ |
| 100 | char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */ |
| 101 | struct netfront_info *info; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 102 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 103 | struct napi_struct napi; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 104 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 105 | /* Split event channels support, tx_* == rx_* when using |
| 106 | * single event channel. |
| 107 | */ |
| 108 | unsigned int tx_evtchn, rx_evtchn; |
| 109 | unsigned int tx_irq, rx_irq; |
| 110 | /* Only used when split event channels support is enabled */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 111 | char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */ |
| 112 | char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 113 | |
| 114 | spinlock_t tx_lock; |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 115 | struct xen_netif_tx_front_ring tx; |
| 116 | int tx_ring_ref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries |
| 120 | * are linked from tx_skb_freelist through skb_entry.link. |
| 121 | * |
| 122 | * NB. Freelist index entries are always going to be less than |
| 123 | * PAGE_OFFSET, whereas pointers to skbs will always be equal or |
| 124 | * greater than PAGE_OFFSET: we use this property to distinguish |
| 125 | * them. |
| 126 | */ |
| 127 | union skb_entry { |
| 128 | struct sk_buff *skb; |
Isaku Yamahata | 1ffb40b | 2008-07-08 15:06:31 -0700 | [diff] [blame] | 129 | unsigned long link; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 130 | } tx_skbs[NET_TX_RING_SIZE]; |
| 131 | grant_ref_t gref_tx_head; |
| 132 | grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 133 | struct page *grant_tx_page[NET_TX_RING_SIZE]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 134 | unsigned tx_skb_freelist; |
| 135 | |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 136 | spinlock_t rx_lock ____cacheline_aligned_in_smp; |
| 137 | struct xen_netif_rx_front_ring rx; |
| 138 | int rx_ring_ref; |
| 139 | |
Jeremy Fitzhardinge | 84284d3 | 2007-10-15 12:59:53 -0700 | [diff] [blame] | 140 | struct timer_list rx_refill_timer; |
| 141 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 142 | struct sk_buff *rx_skbs[NET_RX_RING_SIZE]; |
| 143 | grant_ref_t gref_rx_head; |
| 144 | grant_ref_t grant_rx_ref[NET_RX_RING_SIZE]; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | struct netfront_info { |
| 148 | struct list_head list; |
| 149 | struct net_device *netdev; |
| 150 | |
| 151 | struct xenbus_device *xbdev; |
| 152 | |
| 153 | /* Multi-queue support */ |
| 154 | struct netfront_queue *queues; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 155 | |
| 156 | /* Statistics */ |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 157 | struct netfront_stats __percpu *rx_stats; |
| 158 | struct netfront_stats __percpu *tx_stats; |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 159 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 160 | atomic_t rx_gso_checksum_fixup; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | struct netfront_rx_info { |
| 164 | struct xen_netif_rx_response rx; |
| 165 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; |
| 166 | }; |
| 167 | |
Isaku Yamahata | 1ffb40b | 2008-07-08 15:06:31 -0700 | [diff] [blame] | 168 | static void skb_entry_set_link(union skb_entry *list, unsigned short id) |
| 169 | { |
| 170 | list->link = id; |
| 171 | } |
| 172 | |
| 173 | static int skb_entry_is_link(const union skb_entry *list) |
| 174 | { |
| 175 | BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link)); |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 176 | return (unsigned long)list->skb < PAGE_OFFSET; |
Isaku Yamahata | 1ffb40b | 2008-07-08 15:06:31 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 179 | /* |
| 180 | * Access macros for acquiring freeing slots in tx_skbs[]. |
| 181 | */ |
| 182 | |
| 183 | static void add_id_to_freelist(unsigned *head, union skb_entry *list, |
| 184 | unsigned short id) |
| 185 | { |
Isaku Yamahata | 1ffb40b | 2008-07-08 15:06:31 -0700 | [diff] [blame] | 186 | skb_entry_set_link(&list[id], *head); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 187 | *head = id; |
| 188 | } |
| 189 | |
| 190 | static unsigned short get_id_from_freelist(unsigned *head, |
| 191 | union skb_entry *list) |
| 192 | { |
| 193 | unsigned int id = *head; |
| 194 | *head = list[id].link; |
| 195 | return id; |
| 196 | } |
| 197 | |
| 198 | static int xennet_rxidx(RING_IDX idx) |
| 199 | { |
| 200 | return idx & (NET_RX_RING_SIZE - 1); |
| 201 | } |
| 202 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 203 | static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 204 | RING_IDX ri) |
| 205 | { |
| 206 | int i = xennet_rxidx(ri); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 207 | struct sk_buff *skb = queue->rx_skbs[i]; |
| 208 | queue->rx_skbs[i] = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 209 | return skb; |
| 210 | } |
| 211 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 212 | static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 213 | RING_IDX ri) |
| 214 | { |
| 215 | int i = xennet_rxidx(ri); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 216 | grant_ref_t ref = queue->grant_rx_ref[i]; |
| 217 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 218 | return ref; |
| 219 | } |
| 220 | |
| 221 | #ifdef CONFIG_SYSFS |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 222 | static const struct attribute_group xennet_dev_group; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 223 | #endif |
| 224 | |
Michał Mirosław | 3ad9b35 | 2011-11-16 14:05:33 +0000 | [diff] [blame] | 225 | static bool xennet_can_sg(struct net_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 226 | { |
Michał Mirosław | 3ad9b35 | 2011-11-16 14:05:33 +0000 | [diff] [blame] | 227 | return dev->features & NETIF_F_SG; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | static void rx_refill_timeout(unsigned long data) |
| 232 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 233 | struct netfront_queue *queue = (struct netfront_queue *)data; |
| 234 | napi_schedule(&queue->napi); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 237 | static int netfront_tx_slot_available(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 238 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 239 | return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) < |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 240 | (NET_TX_RING_SIZE - MAX_SKB_FRAGS - 2); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 243 | static void xennet_maybe_wake_tx(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 244 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 245 | struct net_device *dev = queue->info->netdev; |
| 246 | struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 247 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 248 | if (unlikely(netif_tx_queue_stopped(dev_queue)) && |
| 249 | netfront_tx_slot_available(queue) && |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 250 | likely(netif_running(dev))) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 251 | netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 252 | } |
| 253 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 254 | |
| 255 | static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 256 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 257 | struct sk_buff *skb; |
| 258 | struct page *page; |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 259 | |
| 260 | skb = __netdev_alloc_skb(queue->info->netdev, |
| 261 | RX_COPY_THRESHOLD + NET_IP_ALIGN, |
| 262 | GFP_ATOMIC | __GFP_NOWARN); |
| 263 | if (unlikely(!skb)) |
| 264 | return NULL; |
| 265 | |
| 266 | page = alloc_page(GFP_ATOMIC | __GFP_NOWARN); |
| 267 | if (!page) { |
| 268 | kfree_skb(skb); |
| 269 | return NULL; |
| 270 | } |
| 271 | skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE); |
| 272 | |
| 273 | /* Align ip header to a 16 bytes boundary */ |
| 274 | skb_reserve(skb, NET_IP_ALIGN); |
| 275 | skb->dev = queue->info->netdev; |
| 276 | |
| 277 | return skb; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | static void xennet_alloc_rx_buffers(struct netfront_queue *queue) |
| 282 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 283 | RING_IDX req_prod = queue->rx.req_prod_pvt; |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 284 | int notify; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 285 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 286 | if (unlikely(!netif_carrier_ok(queue->info->netdev))) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 287 | return; |
| 288 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 289 | for (req_prod = queue->rx.req_prod_pvt; |
| 290 | req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE; |
| 291 | req_prod++) { |
| 292 | struct sk_buff *skb; |
| 293 | unsigned short id; |
| 294 | grant_ref_t ref; |
| 295 | unsigned long pfn; |
| 296 | struct xen_netif_rx_request *req; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 297 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 298 | skb = xennet_alloc_one_rx_buffer(queue); |
| 299 | if (!skb) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 300 | break; |
| 301 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 302 | id = xennet_rxidx(req_prod); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 303 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 304 | BUG_ON(queue->rx_skbs[id]); |
| 305 | queue->rx_skbs[id] = skb; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 306 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 307 | ref = gnttab_claim_grant_reference(&queue->gref_rx_head); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 308 | BUG_ON((signed short)ref < 0); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 309 | queue->grant_rx_ref[id] = ref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 310 | |
Ian Campbell | 01c6802 | 2011-10-05 00:28:47 +0000 | [diff] [blame] | 311 | pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0])); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 312 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 313 | req = RING_GET_REQUEST(&queue->rx, req_prod); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 314 | gnttab_grant_foreign_access_ref(ref, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 315 | queue->info->xbdev->otherend_id, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 316 | pfn_to_mfn(pfn), |
| 317 | 0); |
| 318 | |
| 319 | req->id = id; |
| 320 | req->gref = ref; |
| 321 | } |
| 322 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 323 | queue->rx.req_prod_pvt = req_prod; |
| 324 | |
| 325 | /* Not enough requests? Try again later. */ |
| 326 | if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN) { |
| 327 | mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10)); |
| 328 | return; |
| 329 | } |
| 330 | |
Jeremy Fitzhardinge | 5dcddfa | 2007-08-07 14:56:42 -0700 | [diff] [blame] | 331 | wmb(); /* barrier so backend seens requests */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 332 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 333 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 334 | if (notify) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 335 | notify_remote_via_irq(queue->rx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static int xennet_open(struct net_device *dev) |
| 339 | { |
| 340 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 341 | unsigned int num_queues = dev->real_num_tx_queues; |
| 342 | unsigned int i = 0; |
| 343 | struct netfront_queue *queue = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 344 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 345 | for (i = 0; i < num_queues; ++i) { |
| 346 | queue = &np->queues[i]; |
| 347 | napi_enable(&queue->napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 348 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 349 | spin_lock_bh(&queue->rx_lock); |
| 350 | if (netif_carrier_ok(dev)) { |
| 351 | xennet_alloc_rx_buffers(queue); |
| 352 | queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1; |
| 353 | if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)) |
| 354 | napi_schedule(&queue->napi); |
| 355 | } |
| 356 | spin_unlock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 357 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 358 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 359 | netif_tx_start_all_queues(dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 364 | static void xennet_tx_buf_gc(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 365 | { |
| 366 | RING_IDX cons, prod; |
| 367 | unsigned short id; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 368 | struct sk_buff *skb; |
| 369 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 370 | BUG_ON(!netif_carrier_ok(queue->info->netdev)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 371 | |
| 372 | do { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 373 | prod = queue->tx.sring->rsp_prod; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 374 | rmb(); /* Ensure we see responses up to 'rp'. */ |
| 375 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 376 | for (cons = queue->tx.rsp_cons; cons != prod; cons++) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 377 | struct xen_netif_tx_response *txrsp; |
| 378 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 379 | txrsp = RING_GET_RESPONSE(&queue->tx, cons); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 380 | if (txrsp->status == XEN_NETIF_RSP_NULL) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 381 | continue; |
| 382 | |
| 383 | id = txrsp->id; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 384 | skb = queue->tx_skbs[id].skb; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 385 | if (unlikely(gnttab_query_foreign_access( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 386 | queue->grant_tx_ref[id]) != 0)) { |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 387 | pr_alert("%s: warning -- grant still in use by backend domain\n", |
| 388 | __func__); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 389 | BUG(); |
| 390 | } |
| 391 | gnttab_end_foreign_access_ref( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 392 | queue->grant_tx_ref[id], GNTMAP_readonly); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 393 | gnttab_release_grant_reference( |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 394 | &queue->gref_tx_head, queue->grant_tx_ref[id]); |
| 395 | queue->grant_tx_ref[id] = GRANT_INVALID_REF; |
| 396 | queue->grant_tx_page[id] = NULL; |
| 397 | add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 398 | dev_kfree_skb_irq(skb); |
| 399 | } |
| 400 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 401 | queue->tx.rsp_cons = prod; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Set a new event, then check for race with update of tx_cons. |
| 405 | * Note that it is essential to schedule a callback, no matter |
| 406 | * how few buffers are pending. Even if there is space in the |
| 407 | * transmit ring, higher layers may be blocked because too much |
| 408 | * data is outstanding: in such cases notification from Xen is |
| 409 | * likely to be the only kick that we'll get. |
| 410 | */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 411 | queue->tx.sring->rsp_event = |
| 412 | prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 413 | mb(); /* update shared area */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 414 | } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 415 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 416 | xennet_maybe_wake_tx(queue); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 417 | } |
| 418 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 419 | static struct xen_netif_tx_request *xennet_make_one_txreq( |
| 420 | struct netfront_queue *queue, struct sk_buff *skb, |
| 421 | struct page *page, unsigned int offset, unsigned int len) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 422 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 423 | unsigned int id; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 424 | struct xen_netif_tx_request *tx; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 425 | grant_ref_t ref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 426 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 427 | len = min_t(unsigned int, PAGE_SIZE - offset, len); |
| 428 | |
| 429 | id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs); |
| 430 | tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
| 431 | ref = gnttab_claim_grant_reference(&queue->gref_tx_head); |
| 432 | BUG_ON((signed short)ref < 0); |
| 433 | |
| 434 | gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id, |
| 435 | page_to_mfn(page), GNTMAP_readonly); |
| 436 | |
| 437 | queue->tx_skbs[id].skb = skb; |
| 438 | queue->grant_tx_page[id] = page; |
| 439 | queue->grant_tx_ref[id] = ref; |
| 440 | |
| 441 | tx->id = id; |
| 442 | tx->gref = ref; |
| 443 | tx->offset = offset; |
| 444 | tx->size = len; |
| 445 | tx->flags = 0; |
| 446 | |
| 447 | return tx; |
| 448 | } |
| 449 | |
| 450 | static struct xen_netif_tx_request *xennet_make_txreqs( |
| 451 | struct netfront_queue *queue, struct xen_netif_tx_request *tx, |
| 452 | struct sk_buff *skb, struct page *page, |
| 453 | unsigned int offset, unsigned int len) |
| 454 | { |
| 455 | /* Skip unused frames from start of page */ |
| 456 | page += offset >> PAGE_SHIFT; |
| 457 | offset &= ~PAGE_MASK; |
| 458 | |
| 459 | while (len) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 460 | tx->flags |= XEN_NETTXF_more_data; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 461 | tx = xennet_make_one_txreq(queue, skb_get(skb), |
| 462 | page, offset, len); |
| 463 | page++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 464 | offset = 0; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 465 | len -= tx->size; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 466 | } |
| 467 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 468 | return tx; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 471 | /* |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 472 | * Count how many ring slots are required to send this skb. Each frag |
| 473 | * might be a compound page. |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 474 | */ |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 475 | static int xennet_count_skb_slots(struct sk_buff *skb) |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 476 | { |
| 477 | int i, frags = skb_shinfo(skb)->nr_frags; |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 478 | int pages; |
| 479 | |
| 480 | pages = PFN_UP(offset_in_page(skb->data) + skb_headlen(skb)); |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 481 | |
| 482 | for (i = 0; i < frags; i++) { |
| 483 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 484 | unsigned long size = skb_frag_size(frag); |
| 485 | unsigned long offset = frag->page_offset; |
| 486 | |
| 487 | /* Skip unused frames from start of page */ |
| 488 | offset &= ~PAGE_MASK; |
| 489 | |
| 490 | pages += PFN_UP(offset + size); |
| 491 | } |
| 492 | |
| 493 | return pages; |
| 494 | } |
| 495 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 496 | static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb, |
| 497 | void *accel_priv, select_queue_fallback_t fallback) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 498 | { |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 499 | unsigned int num_queues = dev->real_num_tx_queues; |
| 500 | u32 hash; |
| 501 | u16 queue_idx; |
| 502 | |
| 503 | /* First, check if there is only one queue */ |
| 504 | if (num_queues == 1) { |
| 505 | queue_idx = 0; |
| 506 | } else { |
| 507 | hash = skb_get_hash(skb); |
| 508 | queue_idx = hash % num_queues; |
| 509 | } |
| 510 | |
| 511 | return queue_idx; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 514 | static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 515 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 516 | struct netfront_info *np = netdev_priv(dev); |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 517 | struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats); |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 518 | struct xen_netif_tx_request *tx, *first_tx; |
| 519 | unsigned int i; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 520 | int notify; |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 521 | int slots; |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 522 | struct page *page; |
| 523 | unsigned int offset; |
| 524 | unsigned int len; |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 525 | unsigned long flags; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 526 | struct netfront_queue *queue = NULL; |
| 527 | unsigned int num_queues = dev->real_num_tx_queues; |
| 528 | u16 queue_index; |
| 529 | |
| 530 | /* Drop the packet if no queues are set up */ |
| 531 | if (num_queues < 1) |
| 532 | goto drop; |
| 533 | /* Determine which queue to transmit this SKB on */ |
| 534 | queue_index = skb_get_queue_mapping(skb); |
| 535 | queue = &np->queues[queue_index]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 536 | |
Wei Liu | 9ecd1a7 | 2013-04-22 02:20:41 +0000 | [diff] [blame] | 537 | /* If skb->len is too big for wire format, drop skb and alert |
| 538 | * user about misconfiguration. |
| 539 | */ |
| 540 | if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) { |
| 541 | net_alert_ratelimited( |
| 542 | "xennet: skb->len = %u, too big for wire format\n", |
| 543 | skb->len); |
| 544 | goto drop; |
| 545 | } |
| 546 | |
David Vrabel | e84448d | 2015-01-13 17:16:43 +0000 | [diff] [blame] | 547 | slots = xennet_count_skb_slots(skb); |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 548 | if (unlikely(slots > MAX_SKB_FRAGS + 1)) { |
Zoltan Kiss | 97a6d1b | 2014-08-11 18:32:23 +0100 | [diff] [blame] | 549 | net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n", |
| 550 | slots, skb->len); |
| 551 | if (skb_linearize(skb)) |
| 552 | goto drop; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 553 | } |
| 554 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 555 | page = virt_to_page(skb->data); |
| 556 | offset = offset_in_page(skb->data); |
| 557 | len = skb_headlen(skb); |
| 558 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 559 | spin_lock_irqsave(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 560 | |
| 561 | if (unlikely(!netif_carrier_ok(dev) || |
Ian Campbell | f36c374 | 2012-11-21 02:02:16 +0000 | [diff] [blame] | 562 | (slots > 1 && !xennet_can_sg(dev)) || |
Johannes Berg | 8b86a61 | 2015-04-17 15:45:04 +0200 | [diff] [blame] | 563 | netif_needs_gso(skb, netif_skb_features(skb)))) { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 564 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 565 | goto drop; |
| 566 | } |
| 567 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 568 | /* First request for the linear area. */ |
| 569 | first_tx = tx = xennet_make_one_txreq(queue, skb, |
| 570 | page, offset, len); |
| 571 | page++; |
| 572 | offset = 0; |
| 573 | len -= tx->size; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 574 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 575 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 576 | /* local packet? */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 577 | tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 578 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
| 579 | /* remote but checksummed. */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 580 | tx->flags |= XEN_NETTXF_data_validated; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 581 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 582 | /* Optional extra info after the first request. */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 583 | if (skb_shinfo(skb)->gso_size) { |
| 584 | struct xen_netif_extra_info *gso; |
| 585 | |
| 586 | gso = (struct xen_netif_extra_info *) |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 587 | RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 588 | |
Wei Liu | e2d617c0 | 2013-03-25 01:08:17 +0000 | [diff] [blame] | 589 | tx->flags |= XEN_NETTXF_extra_info; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 590 | |
| 591 | gso->u.gso.size = skb_shinfo(skb)->gso_size; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 592 | gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ? |
| 593 | XEN_NETIF_GSO_TYPE_TCPV6 : |
| 594 | XEN_NETIF_GSO_TYPE_TCPV4; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 595 | gso->u.gso.pad = 0; |
| 596 | gso->u.gso.features = 0; |
| 597 | |
| 598 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; |
| 599 | gso->flags = 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 600 | } |
| 601 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 602 | /* Requests for the rest of the linear area. */ |
| 603 | tx = xennet_make_txreqs(queue, tx, skb, page, offset, len); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 604 | |
David Vrabel | a55e8bb | 2015-01-13 17:16:44 +0000 | [diff] [blame] | 605 | /* Requests for all the frags. */ |
| 606 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 607 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 608 | tx = xennet_make_txreqs(queue, tx, skb, |
| 609 | skb_frag_page(frag), frag->page_offset, |
| 610 | skb_frag_size(frag)); |
| 611 | } |
| 612 | |
| 613 | /* First request has the packet length. */ |
| 614 | first_tx->size = skb->len; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 615 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 616 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 617 | if (notify) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 618 | notify_remote_via_irq(queue->tx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 619 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 620 | u64_stats_update_begin(&tx_stats->syncp); |
| 621 | tx_stats->bytes += skb->len; |
| 622 | tx_stats->packets++; |
| 623 | u64_stats_update_end(&tx_stats->syncp); |
Jeremy Fitzhardinge | 10a273a | 2007-08-13 12:54:37 -0700 | [diff] [blame] | 624 | |
| 625 | /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 626 | xennet_tx_buf_gc(queue); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 627 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 628 | if (!netfront_tx_slot_available(queue)) |
| 629 | netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 630 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 631 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 632 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 633 | return NETDEV_TX_OK; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 634 | |
| 635 | drop: |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 636 | dev->stats.tx_dropped++; |
Eric W. Biederman | 979de8a | 2014-03-15 18:33:04 -0700 | [diff] [blame] | 637 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 638 | return NETDEV_TX_OK; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static int xennet_close(struct net_device *dev) |
| 642 | { |
| 643 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 644 | unsigned int num_queues = dev->real_num_tx_queues; |
| 645 | unsigned int i; |
| 646 | struct netfront_queue *queue; |
| 647 | netif_tx_stop_all_queues(np->netdev); |
| 648 | for (i = 0; i < num_queues; ++i) { |
| 649 | queue = &np->queues[i]; |
| 650 | napi_disable(&queue->napi); |
| 651 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 655 | static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 656 | grant_ref_t ref) |
| 657 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 658 | int new = xennet_rxidx(queue->rx.req_prod_pvt); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 659 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 660 | BUG_ON(queue->rx_skbs[new]); |
| 661 | queue->rx_skbs[new] = skb; |
| 662 | queue->grant_rx_ref[new] = ref; |
| 663 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new; |
| 664 | RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref; |
| 665 | queue->rx.req_prod_pvt++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 668 | static int xennet_get_extras(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 669 | struct xen_netif_extra_info *extras, |
| 670 | RING_IDX rp) |
| 671 | |
| 672 | { |
| 673 | struct xen_netif_extra_info *extra; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 674 | struct device *dev = &queue->info->netdev->dev; |
| 675 | RING_IDX cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 676 | int err = 0; |
| 677 | |
| 678 | do { |
| 679 | struct sk_buff *skb; |
| 680 | grant_ref_t ref; |
| 681 | |
| 682 | if (unlikely(cons + 1 == rp)) { |
| 683 | if (net_ratelimit()) |
| 684 | dev_warn(dev, "Missing extra info\n"); |
| 685 | err = -EBADR; |
| 686 | break; |
| 687 | } |
| 688 | |
| 689 | extra = (struct xen_netif_extra_info *) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 690 | RING_GET_RESPONSE(&queue->rx, ++cons); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 691 | |
| 692 | if (unlikely(!extra->type || |
| 693 | extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) { |
| 694 | if (net_ratelimit()) |
| 695 | dev_warn(dev, "Invalid extra type: %d\n", |
| 696 | extra->type); |
| 697 | err = -EINVAL; |
| 698 | } else { |
| 699 | memcpy(&extras[extra->type - 1], extra, |
| 700 | sizeof(*extra)); |
| 701 | } |
| 702 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 703 | skb = xennet_get_rx_skb(queue, cons); |
| 704 | ref = xennet_get_rx_ref(queue, cons); |
| 705 | xennet_move_rx_slot(queue, skb, ref); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 706 | } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); |
| 707 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 708 | queue->rx.rsp_cons = cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 709 | return err; |
| 710 | } |
| 711 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 712 | static int xennet_get_responses(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 713 | struct netfront_rx_info *rinfo, RING_IDX rp, |
| 714 | struct sk_buff_head *list) |
| 715 | { |
| 716 | struct xen_netif_rx_response *rx = &rinfo->rx; |
| 717 | struct xen_netif_extra_info *extras = rinfo->extras; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 718 | struct device *dev = &queue->info->netdev->dev; |
| 719 | RING_IDX cons = queue->rx.rsp_cons; |
| 720 | struct sk_buff *skb = xennet_get_rx_skb(queue, cons); |
| 721 | grant_ref_t ref = xennet_get_rx_ref(queue, cons); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 722 | int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD); |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 723 | int slots = 1; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 724 | int err = 0; |
| 725 | unsigned long ret; |
| 726 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 727 | if (rx->flags & XEN_NETRXF_extra_info) { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 728 | err = xennet_get_extras(queue, extras, rp); |
| 729 | cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | for (;;) { |
| 733 | if (unlikely(rx->status < 0 || |
| 734 | rx->offset + rx->status > PAGE_SIZE)) { |
| 735 | if (net_ratelimit()) |
| 736 | dev_warn(dev, "rx->offset: %x, size: %u\n", |
| 737 | rx->offset, rx->status); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 738 | xennet_move_rx_slot(queue, skb, ref); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 739 | err = -EINVAL; |
| 740 | goto next; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * This definitely indicates a bug, either in this driver or in |
| 745 | * the backend driver. In future this should flag the bad |
Wei Liu | 697089d | 2013-04-22 02:20:40 +0000 | [diff] [blame] | 746 | * situation to the system controller to reboot the backend. |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 747 | */ |
| 748 | if (ref == GRANT_INVALID_REF) { |
| 749 | if (net_ratelimit()) |
| 750 | dev_warn(dev, "Bad rx response id %d.\n", |
| 751 | rx->id); |
| 752 | err = -EINVAL; |
| 753 | goto next; |
| 754 | } |
| 755 | |
| 756 | ret = gnttab_end_foreign_access_ref(ref, 0); |
| 757 | BUG_ON(!ret); |
| 758 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 759 | gnttab_release_grant_reference(&queue->gref_rx_head, ref); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 760 | |
| 761 | __skb_queue_tail(list, skb); |
| 762 | |
| 763 | next: |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 764 | if (!(rx->flags & XEN_NETRXF_more_data)) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 765 | break; |
| 766 | |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 767 | if (cons + slots == rp) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 768 | if (net_ratelimit()) |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 769 | dev_warn(dev, "Need more slots\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 770 | err = -ENOENT; |
| 771 | break; |
| 772 | } |
| 773 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 774 | rx = RING_GET_RESPONSE(&queue->rx, cons + slots); |
| 775 | skb = xennet_get_rx_skb(queue, cons + slots); |
| 776 | ref = xennet_get_rx_ref(queue, cons + slots); |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 777 | slots++; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Wei Liu | 7158ff6 | 2013-03-25 01:08:19 +0000 | [diff] [blame] | 780 | if (unlikely(slots > max)) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 781 | if (net_ratelimit()) |
Wei Liu | 697089d | 2013-04-22 02:20:40 +0000 | [diff] [blame] | 782 | dev_warn(dev, "Too many slots\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 783 | err = -E2BIG; |
| 784 | } |
| 785 | |
| 786 | if (unlikely(err)) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 787 | queue->rx.rsp_cons = cons + slots; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 788 | |
| 789 | return err; |
| 790 | } |
| 791 | |
| 792 | static int xennet_set_skb_gso(struct sk_buff *skb, |
| 793 | struct xen_netif_extra_info *gso) |
| 794 | { |
| 795 | if (!gso->u.gso.size) { |
| 796 | if (net_ratelimit()) |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 797 | pr_warn("GSO size must not be zero\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | } |
| 800 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 801 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 && |
| 802 | gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 803 | if (net_ratelimit()) |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 804 | pr_warn("Bad GSO type %d\n", gso->u.gso.type); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | } |
| 807 | |
| 808 | skb_shinfo(skb)->gso_size = gso->u.gso.size; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 809 | skb_shinfo(skb)->gso_type = |
| 810 | (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ? |
| 811 | SKB_GSO_TCPV4 : |
| 812 | SKB_GSO_TCPV6; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 813 | |
| 814 | /* Header must be checked, and gso_segs computed. */ |
| 815 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 816 | skb_shinfo(skb)->gso_segs = 0; |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 821 | static RING_IDX xennet_fill_frags(struct netfront_queue *queue, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 822 | struct sk_buff *skb, |
| 823 | struct sk_buff_head *list) |
| 824 | { |
| 825 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 826 | RING_IDX cons = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 827 | struct sk_buff *nskb; |
| 828 | |
| 829 | while ((nskb = __skb_dequeue(list))) { |
| 830 | struct xen_netif_rx_response *rx = |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 831 | RING_GET_RESPONSE(&queue->rx, ++cons); |
Ian Campbell | 01c6802 | 2011-10-05 00:28:47 +0000 | [diff] [blame] | 832 | skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0]; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 833 | |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 834 | if (shinfo->nr_frags == MAX_SKB_FRAGS) { |
| 835 | unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 836 | |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 837 | BUG_ON(pull_to <= skb_headlen(skb)); |
| 838 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
| 839 | } |
| 840 | BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS); |
| 841 | |
| 842 | skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag), |
| 843 | rx->offset, rx->status, PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 844 | |
| 845 | skb_shinfo(nskb)->nr_frags = 0; |
| 846 | kfree_skb(nskb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 849 | return cons; |
| 850 | } |
| 851 | |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 852 | static int checksum_setup(struct net_device *dev, struct sk_buff *skb) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 853 | { |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 854 | bool recalculate_partial_csum = false; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 855 | |
| 856 | /* |
| 857 | * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy |
| 858 | * peers can fail to set NETRXF_csum_blank when sending a GSO |
| 859 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and |
| 860 | * recalculate the partial checksum. |
| 861 | */ |
| 862 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { |
| 863 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 864 | atomic_inc(&np->rx_gso_checksum_fixup); |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 865 | skb->ip_summed = CHECKSUM_PARTIAL; |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 866 | recalculate_partial_csum = true; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ |
| 870 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 871 | return 0; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 872 | |
Paul Durrant | b5cf66c | 2014-01-09 10:02:48 +0000 | [diff] [blame] | 873 | return skb_checksum_setup(skb, recalculate_partial_csum); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 876 | static int handle_incoming_queue(struct netfront_queue *queue, |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 877 | struct sk_buff_head *rxq) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 878 | { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 879 | struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 880 | int packets_dropped = 0; |
| 881 | struct sk_buff *skb; |
| 882 | |
| 883 | while ((skb = __skb_dequeue(rxq)) != NULL) { |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 884 | int pull_to = NETFRONT_SKB_CB(skb)->pull_to; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 885 | |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 886 | if (pull_to > skb_headlen(skb)) |
| 887 | __pskb_pull_tail(skb, pull_to - skb_headlen(skb)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 888 | |
| 889 | /* Ethernet work: Delayed to here as it peeks the header. */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 890 | skb->protocol = eth_type_trans(skb, queue->info->netdev); |
Wei Liu | d554f73 | 2014-02-19 18:48:34 +0000 | [diff] [blame] | 891 | skb_reset_network_header(skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 892 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 893 | if (checksum_setup(queue->info->netdev, skb)) { |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 894 | kfree_skb(skb); |
| 895 | packets_dropped++; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 896 | queue->info->netdev->stats.rx_errors++; |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 897 | continue; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 898 | } |
| 899 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 900 | u64_stats_update_begin(&rx_stats->syncp); |
| 901 | rx_stats->packets++; |
| 902 | rx_stats->bytes += skb->len; |
| 903 | u64_stats_update_end(&rx_stats->syncp); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 904 | |
| 905 | /* Pass it up. */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 906 | napi_gro_receive(&queue->napi, skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return packets_dropped; |
| 910 | } |
| 911 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 912 | static int xennet_poll(struct napi_struct *napi, int budget) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 913 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 914 | struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi); |
| 915 | struct net_device *dev = queue->info->netdev; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 916 | struct sk_buff *skb; |
| 917 | struct netfront_rx_info rinfo; |
| 918 | struct xen_netif_rx_response *rx = &rinfo.rx; |
| 919 | struct xen_netif_extra_info *extras = rinfo.extras; |
| 920 | RING_IDX i, rp; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 921 | int work_done; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 922 | struct sk_buff_head rxq; |
| 923 | struct sk_buff_head errq; |
| 924 | struct sk_buff_head tmpq; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 925 | int err; |
| 926 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 927 | spin_lock(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 928 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 929 | skb_queue_head_init(&rxq); |
| 930 | skb_queue_head_init(&errq); |
| 931 | skb_queue_head_init(&tmpq); |
| 932 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 933 | rp = queue->rx.sring->rsp_prod; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 934 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
| 935 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 936 | i = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 937 | work_done = 0; |
| 938 | while ((i != rp) && (work_done < budget)) { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 939 | memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx)); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 940 | memset(extras, 0, sizeof(rinfo.extras)); |
| 941 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 942 | err = xennet_get_responses(queue, &rinfo, rp, &tmpq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 943 | |
| 944 | if (unlikely(err)) { |
| 945 | err: |
| 946 | while ((skb = __skb_dequeue(&tmpq))) |
| 947 | __skb_queue_tail(&errq, skb); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 948 | dev->stats.rx_errors++; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 949 | i = queue->rx.rsp_cons; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 950 | continue; |
| 951 | } |
| 952 | |
| 953 | skb = __skb_dequeue(&tmpq); |
| 954 | |
| 955 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { |
| 956 | struct xen_netif_extra_info *gso; |
| 957 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; |
| 958 | |
| 959 | if (unlikely(xennet_set_skb_gso(skb, gso))) { |
| 960 | __skb_queue_head(&tmpq, skb); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 961 | queue->rx.rsp_cons += skb_queue_len(&tmpq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 962 | goto err; |
| 963 | } |
| 964 | } |
| 965 | |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 966 | NETFRONT_SKB_CB(skb)->pull_to = rx->status; |
| 967 | if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD) |
| 968 | NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 969 | |
Ian Campbell | 3683243 | 2012-08-22 00:26:47 +0000 | [diff] [blame] | 970 | skb_shinfo(skb)->frags[0].page_offset = rx->offset; |
| 971 | skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status); |
| 972 | skb->data_len = rx->status; |
Jan Beulich | 093b9c7 | 2013-07-17 08:09:37 +0100 | [diff] [blame] | 973 | skb->len += rx->status; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 974 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 975 | i = xennet_fill_frags(queue, skb, &tmpq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 976 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 977 | if (rx->flags & XEN_NETRXF_csum_blank) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 978 | skb->ip_summed = CHECKSUM_PARTIAL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 979 | else if (rx->flags & XEN_NETRXF_data_validated) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 980 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 981 | |
| 982 | __skb_queue_tail(&rxq, skb); |
| 983 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 984 | queue->rx.rsp_cons = ++i; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 985 | work_done++; |
| 986 | } |
| 987 | |
Wang Chen | 56cfe5d | 2008-05-22 18:09:06 +0800 | [diff] [blame] | 988 | __skb_queue_purge(&errq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 989 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 990 | work_done -= handle_incoming_queue(queue, &rxq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 991 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 992 | xennet_alloc_rx_buffers(queue); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 993 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 994 | if (work_done < budget) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 995 | int more_to_do = 0; |
| 996 | |
David Vrabel | 6a6dc08 | 2014-12-16 18:59:46 +0000 | [diff] [blame] | 997 | napi_complete(napi); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 998 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 999 | RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do); |
David Vrabel | 6a6dc08 | 2014-12-16 18:59:46 +0000 | [diff] [blame] | 1000 | if (more_to_do) |
| 1001 | napi_schedule(napi); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1004 | spin_unlock(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1005 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1006 | return work_done; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int xennet_change_mtu(struct net_device *dev, int mtu) |
| 1010 | { |
Jonathan Davies | 0c36820 | 2015-03-31 11:05:15 +0100 | [diff] [blame] | 1011 | int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1012 | |
| 1013 | if (mtu > max) |
| 1014 | return -EINVAL; |
| 1015 | dev->mtu = mtu; |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1019 | static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev, |
| 1020 | struct rtnl_link_stats64 *tot) |
| 1021 | { |
| 1022 | struct netfront_info *np = netdev_priv(dev); |
| 1023 | int cpu; |
| 1024 | |
| 1025 | for_each_possible_cpu(cpu) { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1026 | struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu); |
| 1027 | struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1028 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 1029 | unsigned int start; |
| 1030 | |
| 1031 | do { |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1032 | start = u64_stats_fetch_begin_irq(&tx_stats->syncp); |
| 1033 | tx_packets = tx_stats->packets; |
| 1034 | tx_bytes = tx_stats->bytes; |
| 1035 | } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start)); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1036 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1037 | do { |
| 1038 | start = u64_stats_fetch_begin_irq(&rx_stats->syncp); |
| 1039 | rx_packets = rx_stats->packets; |
| 1040 | rx_bytes = rx_stats->bytes; |
| 1041 | } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start)); |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1042 | |
| 1043 | tot->rx_packets += rx_packets; |
| 1044 | tot->tx_packets += tx_packets; |
| 1045 | tot->rx_bytes += rx_bytes; |
| 1046 | tot->tx_bytes += tx_bytes; |
| 1047 | } |
| 1048 | |
| 1049 | tot->rx_errors = dev->stats.rx_errors; |
| 1050 | tot->tx_dropped = dev->stats.tx_dropped; |
| 1051 | |
| 1052 | return tot; |
| 1053 | } |
| 1054 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1055 | static void xennet_release_tx_bufs(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1056 | { |
| 1057 | struct sk_buff *skb; |
| 1058 | int i; |
| 1059 | |
| 1060 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
| 1061 | /* Skip over entries which are actually freelist references */ |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1062 | if (skb_entry_is_link(&queue->tx_skbs[i])) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1063 | continue; |
| 1064 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1065 | skb = queue->tx_skbs[i].skb; |
| 1066 | get_page(queue->grant_tx_page[i]); |
| 1067 | gnttab_end_foreign_access(queue->grant_tx_ref[i], |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1068 | GNTMAP_readonly, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1069 | (unsigned long)page_address(queue->grant_tx_page[i])); |
| 1070 | queue->grant_tx_page[i] = NULL; |
| 1071 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; |
| 1072 | add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1073 | dev_kfree_skb_irq(skb); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1077 | static void xennet_release_rx_bufs(struct netfront_queue *queue) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1078 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1079 | int id, ref; |
| 1080 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1081 | spin_lock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1082 | |
| 1083 | for (id = 0; id < NET_RX_RING_SIZE; id++) { |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1084 | struct sk_buff *skb; |
| 1085 | struct page *page; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1086 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1087 | skb = queue->rx_skbs[id]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1088 | if (!skb) |
| 1089 | continue; |
| 1090 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1091 | ref = queue->grant_rx_ref[id]; |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1092 | if (ref == GRANT_INVALID_REF) |
| 1093 | continue; |
| 1094 | |
| 1095 | page = skb_frag_page(&skb_shinfo(skb)->frags[0]); |
| 1096 | |
| 1097 | /* gnttab_end_foreign_access() needs a page ref until |
| 1098 | * foreign access is ended (which may be deferred). |
| 1099 | */ |
| 1100 | get_page(page); |
| 1101 | gnttab_end_foreign_access(ref, 0, |
| 1102 | (unsigned long)page_address(page)); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1103 | queue->grant_rx_ref[id] = GRANT_INVALID_REF; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1104 | |
Annie Li | cefe007 | 2014-01-28 11:35:42 +0800 | [diff] [blame] | 1105 | kfree_skb(skb); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1108 | spin_unlock_bh(&queue->rx_lock); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1111 | static netdev_features_t xennet_fix_features(struct net_device *dev, |
| 1112 | netdev_features_t features) |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1113 | { |
| 1114 | struct netfront_info *np = netdev_priv(dev); |
| 1115 | int val; |
| 1116 | |
| 1117 | if (features & NETIF_F_SG) { |
| 1118 | if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg", |
| 1119 | "%d", &val) < 0) |
| 1120 | val = 0; |
| 1121 | |
| 1122 | if (!val) |
| 1123 | features &= ~NETIF_F_SG; |
| 1124 | } |
| 1125 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1126 | if (features & NETIF_F_IPV6_CSUM) { |
| 1127 | if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, |
| 1128 | "feature-ipv6-csum-offload", "%d", &val) < 0) |
| 1129 | val = 0; |
| 1130 | |
| 1131 | if (!val) |
| 1132 | features &= ~NETIF_F_IPV6_CSUM; |
| 1133 | } |
| 1134 | |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1135 | if (features & NETIF_F_TSO) { |
| 1136 | if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, |
| 1137 | "feature-gso-tcpv4", "%d", &val) < 0) |
| 1138 | val = 0; |
| 1139 | |
| 1140 | if (!val) |
| 1141 | features &= ~NETIF_F_TSO; |
| 1142 | } |
| 1143 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1144 | if (features & NETIF_F_TSO6) { |
| 1145 | if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, |
| 1146 | "feature-gso-tcpv6", "%d", &val) < 0) |
| 1147 | val = 0; |
| 1148 | |
| 1149 | if (!val) |
| 1150 | features &= ~NETIF_F_TSO6; |
| 1151 | } |
| 1152 | |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1153 | return features; |
| 1154 | } |
| 1155 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1156 | static int xennet_set_features(struct net_device *dev, |
| 1157 | netdev_features_t features) |
Eric Dumazet | 8f7b01a | 2011-04-03 17:21:00 -0700 | [diff] [blame] | 1158 | { |
| 1159 | if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) { |
| 1160 | netdev_info(dev, "Reducing MTU because no SG offload"); |
| 1161 | dev->mtu = ETH_DATA_LEN; |
| 1162 | } |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1167 | static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id) |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1168 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1169 | struct netfront_queue *queue = dev_id; |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1170 | unsigned long flags; |
| 1171 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1172 | spin_lock_irqsave(&queue->tx_lock, flags); |
| 1173 | xennet_tx_buf_gc(queue); |
| 1174 | spin_unlock_irqrestore(&queue->tx_lock, flags); |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1175 | |
| 1176 | return IRQ_HANDLED; |
| 1177 | } |
| 1178 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1179 | static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id) |
| 1180 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1181 | struct netfront_queue *queue = dev_id; |
| 1182 | struct net_device *dev = queue->info->netdev; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (likely(netif_carrier_ok(dev) && |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1185 | RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))) |
David Vrabel | 7654186 | 2014-06-18 10:47:27 +0100 | [diff] [blame] | 1186 | napi_schedule(&queue->napi); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1187 | |
| 1188 | return IRQ_HANDLED; |
| 1189 | } |
| 1190 | |
| 1191 | static irqreturn_t xennet_interrupt(int irq, void *dev_id) |
| 1192 | { |
| 1193 | xennet_tx_interrupt(irq, dev_id); |
| 1194 | xennet_rx_interrupt(irq, dev_id); |
| 1195 | return IRQ_HANDLED; |
| 1196 | } |
| 1197 | |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1198 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1199 | static void xennet_poll_controller(struct net_device *dev) |
| 1200 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1201 | /* Poll each queue */ |
| 1202 | struct netfront_info *info = netdev_priv(dev); |
| 1203 | unsigned int num_queues = dev->real_num_tx_queues; |
| 1204 | unsigned int i; |
| 1205 | for (i = 0; i < num_queues; ++i) |
| 1206 | xennet_interrupt(0, &info->queues[i]); |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1207 | } |
| 1208 | #endif |
| 1209 | |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1210 | static const struct net_device_ops xennet_netdev_ops = { |
| 1211 | .ndo_open = xennet_open, |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1212 | .ndo_stop = xennet_close, |
| 1213 | .ndo_start_xmit = xennet_start_xmit, |
| 1214 | .ndo_change_mtu = xennet_change_mtu, |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1215 | .ndo_get_stats64 = xennet_get_stats64, |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1216 | .ndo_set_mac_address = eth_mac_addr, |
| 1217 | .ndo_validate_addr = eth_validate_addr, |
Michał Mirosław | fb50793 | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 1218 | .ndo_fix_features = xennet_fix_features, |
| 1219 | .ndo_set_features = xennet_set_features, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1220 | .ndo_select_queue = xennet_select_queue, |
Konrad Rzeszutek Wilk | cf66f9d | 2012-01-23 08:24:43 +0000 | [diff] [blame] | 1221 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1222 | .ndo_poll_controller = xennet_poll_controller, |
| 1223 | #endif |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1224 | }; |
| 1225 | |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1226 | static void xennet_free_netdev(struct net_device *netdev) |
| 1227 | { |
| 1228 | struct netfront_info *np = netdev_priv(netdev); |
| 1229 | |
| 1230 | free_percpu(np->rx_stats); |
| 1231 | free_percpu(np->tx_stats); |
| 1232 | free_netdev(netdev); |
| 1233 | } |
| 1234 | |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 1235 | static struct net_device *xennet_create_dev(struct xenbus_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1236 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1237 | int err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1238 | struct net_device *netdev; |
| 1239 | struct netfront_info *np; |
| 1240 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1241 | netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1242 | if (!netdev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1243 | return ERR_PTR(-ENOMEM); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1244 | |
| 1245 | np = netdev_priv(netdev); |
| 1246 | np->xbdev = dev; |
| 1247 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1248 | /* No need to use rtnl_lock() before the call below as it |
| 1249 | * happens before register_netdev(). |
| 1250 | */ |
| 1251 | netif_set_real_num_tx_queues(netdev, 0); |
| 1252 | np->queues = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1253 | |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1254 | err = -ENOMEM; |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1255 | np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); |
| 1256 | if (np->rx_stats == NULL) |
| 1257 | goto exit; |
| 1258 | np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats); |
| 1259 | if (np->tx_stats == NULL) |
stephen hemminger | e00f85b | 2011-06-21 05:35:31 +0000 | [diff] [blame] | 1260 | goto exit; |
| 1261 | |
Stephen Hemminger | 0a0b9d2 | 2009-01-06 10:44:55 -0800 | [diff] [blame] | 1262 | netdev->netdev_ops = &xennet_netdev_ops; |
| 1263 | |
Michał Mirosław | fb50793 | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 1264 | netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | |
| 1265 | NETIF_F_GSO_ROBUST; |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1266 | netdev->hw_features = NETIF_F_SG | |
| 1267 | NETIF_F_IPV6_CSUM | |
| 1268 | NETIF_F_TSO | NETIF_F_TSO6; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1269 | |
Ian Campbell | fc3e594 | 2011-04-04 11:07:57 -0700 | [diff] [blame] | 1270 | /* |
| 1271 | * Assume that all hw features are available for now. This set |
| 1272 | * will be adjusted by the call to netdev_update_features() in |
| 1273 | * xennet_connect() which is the earliest point where we can |
| 1274 | * negotiate with the backend regarding supported features. |
| 1275 | */ |
| 1276 | netdev->features |= netdev->hw_features; |
| 1277 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1278 | netdev->ethtool_ops = &xennet_ethtool_ops; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1279 | SET_NETDEV_DEV(netdev, &dev->dev); |
| 1280 | |
| 1281 | np->netdev = netdev; |
| 1282 | |
| 1283 | netif_carrier_off(netdev); |
| 1284 | |
| 1285 | return netdev; |
| 1286 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1287 | exit: |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1288 | xennet_free_netdev(netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1289 | return ERR_PTR(err); |
| 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * Entry point to this code when a new device is created. Allocate the basic |
| 1294 | * structures and the ring buffers for communication with the backend, and |
| 1295 | * inform the backend of the appropriate details for those. |
| 1296 | */ |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 1297 | static int netfront_probe(struct xenbus_device *dev, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 1298 | const struct xenbus_device_id *id) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1299 | { |
| 1300 | int err; |
| 1301 | struct net_device *netdev; |
| 1302 | struct netfront_info *info; |
| 1303 | |
| 1304 | netdev = xennet_create_dev(dev); |
| 1305 | if (IS_ERR(netdev)) { |
| 1306 | err = PTR_ERR(netdev); |
| 1307 | xenbus_dev_fatal(dev, err, "creating netdev"); |
| 1308 | return err; |
| 1309 | } |
| 1310 | |
| 1311 | info = netdev_priv(netdev); |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1312 | dev_set_drvdata(&dev->dev, info); |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 1313 | #ifdef CONFIG_SYSFS |
| 1314 | info->netdev->sysfs_groups[0] = &xennet_dev_group; |
| 1315 | #endif |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1316 | err = register_netdev(info->netdev); |
| 1317 | if (err) { |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 1318 | pr_warn("%s: register_netdev err=%d\n", __func__, err); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1319 | goto fail; |
| 1320 | } |
| 1321 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1322 | return 0; |
| 1323 | |
| 1324 | fail: |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 1325 | xennet_free_netdev(netdev); |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1326 | dev_set_drvdata(&dev->dev, NULL); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1327 | return err; |
| 1328 | } |
| 1329 | |
| 1330 | static void xennet_end_access(int ref, void *page) |
| 1331 | { |
| 1332 | /* This frees the page as a side-effect */ |
| 1333 | if (ref != GRANT_INVALID_REF) |
| 1334 | gnttab_end_foreign_access(ref, 0, (unsigned long)page); |
| 1335 | } |
| 1336 | |
| 1337 | static void xennet_disconnect_backend(struct netfront_info *info) |
| 1338 | { |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1339 | unsigned int i = 0; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1340 | unsigned int num_queues = info->netdev->real_num_tx_queues; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1341 | |
David Vrabel | f9feb1e | 2014-07-02 16:09:15 +0100 | [diff] [blame] | 1342 | netif_carrier_off(info->netdev); |
| 1343 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1344 | for (i = 0; i < num_queues; ++i) { |
David Vrabel | 7654186 | 2014-06-18 10:47:27 +0100 | [diff] [blame] | 1345 | struct netfront_queue *queue = &info->queues[i]; |
| 1346 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1347 | if (queue->tx_irq && (queue->tx_irq == queue->rx_irq)) |
| 1348 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1349 | if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) { |
| 1350 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1351 | unbind_from_irqhandler(queue->rx_irq, queue); |
| 1352 | } |
| 1353 | queue->tx_evtchn = queue->rx_evtchn = 0; |
| 1354 | queue->tx_irq = queue->rx_irq = 0; |
| 1355 | |
David Vrabel | f9feb1e | 2014-07-02 16:09:15 +0100 | [diff] [blame] | 1356 | napi_synchronize(&queue->napi); |
| 1357 | |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 1358 | xennet_release_tx_bufs(queue); |
| 1359 | xennet_release_rx_bufs(queue); |
| 1360 | gnttab_free_grant_references(queue->gref_tx_head); |
| 1361 | gnttab_free_grant_references(queue->gref_rx_head); |
| 1362 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1363 | /* End access and free the pages */ |
| 1364 | xennet_end_access(queue->tx_ring_ref, queue->tx.sring); |
| 1365 | xennet_end_access(queue->rx_ring_ref, queue->rx.sring); |
| 1366 | |
| 1367 | queue->tx_ring_ref = GRANT_INVALID_REF; |
| 1368 | queue->rx_ring_ref = GRANT_INVALID_REF; |
| 1369 | queue->tx.sring = NULL; |
| 1370 | queue->rx.sring = NULL; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1371 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
| 1376 | * driver restart. We tear down our netif structure and recreate it, but |
| 1377 | * leave the device-layer structures intact so that this is transparent to the |
| 1378 | * rest of the kernel. |
| 1379 | */ |
| 1380 | static int netfront_resume(struct xenbus_device *dev) |
| 1381 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1382 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1383 | |
| 1384 | dev_dbg(&dev->dev, "%s\n", dev->nodename); |
| 1385 | |
| 1386 | xennet_disconnect_backend(info); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) |
| 1391 | { |
| 1392 | char *s, *e, *macstr; |
| 1393 | int i; |
| 1394 | |
| 1395 | macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); |
| 1396 | if (IS_ERR(macstr)) |
| 1397 | return PTR_ERR(macstr); |
| 1398 | |
| 1399 | for (i = 0; i < ETH_ALEN; i++) { |
| 1400 | mac[i] = simple_strtoul(s, &e, 16); |
| 1401 | if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { |
| 1402 | kfree(macstr); |
| 1403 | return -ENOENT; |
| 1404 | } |
| 1405 | s = e+1; |
| 1406 | } |
| 1407 | |
| 1408 | kfree(macstr); |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1412 | static int setup_netfront_single(struct netfront_queue *queue) |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1413 | { |
| 1414 | int err; |
| 1415 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1416 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1417 | if (err < 0) |
| 1418 | goto fail; |
| 1419 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1420 | err = bind_evtchn_to_irqhandler(queue->tx_evtchn, |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1421 | xennet_interrupt, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1422 | 0, queue->info->netdev->name, queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1423 | if (err < 0) |
| 1424 | goto bind_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1425 | queue->rx_evtchn = queue->tx_evtchn; |
| 1426 | queue->rx_irq = queue->tx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1427 | |
| 1428 | return 0; |
| 1429 | |
| 1430 | bind_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1431 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
| 1432 | queue->tx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1433 | fail: |
| 1434 | return err; |
| 1435 | } |
| 1436 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1437 | static int setup_netfront_split(struct netfront_queue *queue) |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1438 | { |
| 1439 | int err; |
| 1440 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1441 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1442 | if (err < 0) |
| 1443 | goto fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1444 | err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1445 | if (err < 0) |
| 1446 | goto alloc_rx_evtchn_fail; |
| 1447 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1448 | snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), |
| 1449 | "%s-tx", queue->name); |
| 1450 | err = bind_evtchn_to_irqhandler(queue->tx_evtchn, |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1451 | xennet_tx_interrupt, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1452 | 0, queue->tx_irq_name, queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1453 | if (err < 0) |
| 1454 | goto bind_tx_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1455 | queue->tx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1456 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1457 | snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), |
| 1458 | "%s-rx", queue->name); |
| 1459 | err = bind_evtchn_to_irqhandler(queue->rx_evtchn, |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1460 | xennet_rx_interrupt, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1461 | 0, queue->rx_irq_name, queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1462 | if (err < 0) |
| 1463 | goto bind_rx_fail; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1464 | queue->rx_irq = err; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1465 | |
| 1466 | return 0; |
| 1467 | |
| 1468 | bind_rx_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1469 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 1470 | queue->tx_irq = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1471 | bind_tx_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1472 | xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn); |
| 1473 | queue->rx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1474 | alloc_rx_evtchn_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1475 | xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn); |
| 1476 | queue->tx_evtchn = 0; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1477 | fail: |
| 1478 | return err; |
| 1479 | } |
| 1480 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1481 | static int setup_netfront(struct xenbus_device *dev, |
| 1482 | struct netfront_queue *queue, unsigned int feature_split_evtchn) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1483 | { |
| 1484 | struct xen_netif_tx_sring *txs; |
| 1485 | struct xen_netif_rx_sring *rxs; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1486 | grant_ref_t gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1487 | int err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1488 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1489 | queue->tx_ring_ref = GRANT_INVALID_REF; |
| 1490 | queue->rx_ring_ref = GRANT_INVALID_REF; |
| 1491 | queue->rx.sring = NULL; |
| 1492 | queue->tx.sring = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1493 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1494 | txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1495 | if (!txs) { |
| 1496 | err = -ENOMEM; |
| 1497 | xenbus_dev_fatal(dev, err, "allocating tx ring page"); |
| 1498 | goto fail; |
| 1499 | } |
| 1500 | SHARED_RING_INIT(txs); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1501 | FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1502 | |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1503 | err = xenbus_grant_ring(dev, txs, 1, &gref); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1504 | if (err < 0) |
| 1505 | goto grant_tx_ring_fail; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1506 | queue->tx_ring_ref = gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1507 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1508 | rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1509 | if (!rxs) { |
| 1510 | err = -ENOMEM; |
| 1511 | xenbus_dev_fatal(dev, err, "allocating rx ring page"); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1512 | goto alloc_rx_ring_fail; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1513 | } |
| 1514 | SHARED_RING_INIT(rxs); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1515 | FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1516 | |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1517 | err = xenbus_grant_ring(dev, rxs, 1, &gref); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1518 | if (err < 0) |
| 1519 | goto grant_rx_ring_fail; |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 1520 | queue->rx_ring_ref = gref; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1521 | |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1522 | if (feature_split_evtchn) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1523 | err = setup_netfront_split(queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1524 | /* setup single event channel if |
| 1525 | * a) feature-split-event-channels == 0 |
| 1526 | * b) feature-split-event-channels == 1 but failed to setup |
| 1527 | */ |
| 1528 | if (!feature_split_evtchn || (feature_split_evtchn && err)) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1529 | err = setup_netfront_single(queue); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1530 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1531 | if (err) |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1532 | goto alloc_evtchn_fail; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1533 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1534 | return 0; |
| 1535 | |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1536 | /* If we fail to setup netfront, it is safe to just revoke access to |
| 1537 | * granted pages because backend is not accessing it at this point. |
| 1538 | */ |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1539 | alloc_evtchn_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1540 | gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1541 | grant_rx_ring_fail: |
| 1542 | free_page((unsigned long)rxs); |
| 1543 | alloc_rx_ring_fail: |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1544 | gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0); |
Wei Liu | 1ca2983 | 2013-05-20 01:05:12 +0000 | [diff] [blame] | 1545 | grant_tx_ring_fail: |
| 1546 | free_page((unsigned long)txs); |
| 1547 | fail: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1548 | return err; |
| 1549 | } |
| 1550 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1551 | /* Queue-specific initialisation |
| 1552 | * This used to be done in xennet_create_dev() but must now |
| 1553 | * be run per-queue. |
| 1554 | */ |
| 1555 | static int xennet_init_queue(struct netfront_queue *queue) |
| 1556 | { |
| 1557 | unsigned short i; |
| 1558 | int err = 0; |
| 1559 | |
| 1560 | spin_lock_init(&queue->tx_lock); |
| 1561 | spin_lock_init(&queue->rx_lock); |
| 1562 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1563 | init_timer(&queue->rx_refill_timer); |
| 1564 | queue->rx_refill_timer.data = (unsigned long)queue; |
| 1565 | queue->rx_refill_timer.function = rx_refill_timeout; |
| 1566 | |
Wei Liu | 8b71501 | 2014-06-04 10:30:47 +0100 | [diff] [blame] | 1567 | snprintf(queue->name, sizeof(queue->name), "%s-q%u", |
| 1568 | queue->info->netdev->name, queue->id); |
| 1569 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1570 | /* Initialise tx_skbs as a free chain containing every entry. */ |
| 1571 | queue->tx_skb_freelist = 0; |
| 1572 | for (i = 0; i < NET_TX_RING_SIZE; i++) { |
| 1573 | skb_entry_set_link(&queue->tx_skbs[i], i+1); |
| 1574 | queue->grant_tx_ref[i] = GRANT_INVALID_REF; |
| 1575 | queue->grant_tx_page[i] = NULL; |
| 1576 | } |
| 1577 | |
| 1578 | /* Clear out rx_skbs */ |
| 1579 | for (i = 0; i < NET_RX_RING_SIZE; i++) { |
| 1580 | queue->rx_skbs[i] = NULL; |
| 1581 | queue->grant_rx_ref[i] = GRANT_INVALID_REF; |
| 1582 | } |
| 1583 | |
| 1584 | /* A grant for every tx ring slot */ |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 1585 | if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1586 | &queue->gref_tx_head) < 0) { |
| 1587 | pr_alert("can't alloc tx grant refs\n"); |
| 1588 | err = -ENOMEM; |
| 1589 | goto exit; |
| 1590 | } |
| 1591 | |
| 1592 | /* A grant for every rx ring slot */ |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 1593 | if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1594 | &queue->gref_rx_head) < 0) { |
| 1595 | pr_alert("can't alloc rx grant refs\n"); |
| 1596 | err = -ENOMEM; |
| 1597 | goto exit_free_tx; |
| 1598 | } |
| 1599 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1600 | return 0; |
| 1601 | |
| 1602 | exit_free_tx: |
| 1603 | gnttab_free_grant_references(queue->gref_tx_head); |
| 1604 | exit: |
| 1605 | return err; |
| 1606 | } |
| 1607 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1608 | static int write_queue_xenstore_keys(struct netfront_queue *queue, |
| 1609 | struct xenbus_transaction *xbt, int write_hierarchical) |
| 1610 | { |
| 1611 | /* Write the queue-specific keys into XenStore in the traditional |
| 1612 | * way for a single queue, or in a queue subkeys for multiple |
| 1613 | * queues. |
| 1614 | */ |
| 1615 | struct xenbus_device *dev = queue->info->xbdev; |
| 1616 | int err; |
| 1617 | const char *message; |
| 1618 | char *path; |
| 1619 | size_t pathsize; |
| 1620 | |
| 1621 | /* Choose the correct place to write the keys */ |
| 1622 | if (write_hierarchical) { |
| 1623 | pathsize = strlen(dev->nodename) + 10; |
| 1624 | path = kzalloc(pathsize, GFP_KERNEL); |
| 1625 | if (!path) { |
| 1626 | err = -ENOMEM; |
| 1627 | message = "out of memory while writing ring references"; |
| 1628 | goto error; |
| 1629 | } |
| 1630 | snprintf(path, pathsize, "%s/queue-%u", |
| 1631 | dev->nodename, queue->id); |
| 1632 | } else { |
| 1633 | path = (char *)dev->nodename; |
| 1634 | } |
| 1635 | |
| 1636 | /* Write ring references */ |
| 1637 | err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u", |
| 1638 | queue->tx_ring_ref); |
| 1639 | if (err) { |
| 1640 | message = "writing tx-ring-ref"; |
| 1641 | goto error; |
| 1642 | } |
| 1643 | |
| 1644 | err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u", |
| 1645 | queue->rx_ring_ref); |
| 1646 | if (err) { |
| 1647 | message = "writing rx-ring-ref"; |
| 1648 | goto error; |
| 1649 | } |
| 1650 | |
| 1651 | /* Write event channels; taking into account both shared |
| 1652 | * and split event channel scenarios. |
| 1653 | */ |
| 1654 | if (queue->tx_evtchn == queue->rx_evtchn) { |
| 1655 | /* Shared event channel */ |
| 1656 | err = xenbus_printf(*xbt, path, |
| 1657 | "event-channel", "%u", queue->tx_evtchn); |
| 1658 | if (err) { |
| 1659 | message = "writing event-channel"; |
| 1660 | goto error; |
| 1661 | } |
| 1662 | } else { |
| 1663 | /* Split event channels */ |
| 1664 | err = xenbus_printf(*xbt, path, |
| 1665 | "event-channel-tx", "%u", queue->tx_evtchn); |
| 1666 | if (err) { |
| 1667 | message = "writing event-channel-tx"; |
| 1668 | goto error; |
| 1669 | } |
| 1670 | |
| 1671 | err = xenbus_printf(*xbt, path, |
| 1672 | "event-channel-rx", "%u", queue->rx_evtchn); |
| 1673 | if (err) { |
| 1674 | message = "writing event-channel-rx"; |
| 1675 | goto error; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | if (write_hierarchical) |
| 1680 | kfree(path); |
| 1681 | return 0; |
| 1682 | |
| 1683 | error: |
| 1684 | if (write_hierarchical) |
| 1685 | kfree(path); |
| 1686 | xenbus_dev_fatal(dev, err, "%s", message); |
| 1687 | return err; |
| 1688 | } |
| 1689 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 1690 | static void xennet_destroy_queues(struct netfront_info *info) |
| 1691 | { |
| 1692 | unsigned int i; |
| 1693 | |
| 1694 | rtnl_lock(); |
| 1695 | |
| 1696 | for (i = 0; i < info->netdev->real_num_tx_queues; i++) { |
| 1697 | struct netfront_queue *queue = &info->queues[i]; |
| 1698 | |
| 1699 | if (netif_running(info->netdev)) |
| 1700 | napi_disable(&queue->napi); |
David Vrabel | ad06811 | 2015-05-27 15:46:10 +0100 | [diff] [blame] | 1701 | del_timer_sync(&queue->rx_refill_timer); |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 1702 | netif_napi_del(&queue->napi); |
| 1703 | } |
| 1704 | |
| 1705 | rtnl_unlock(); |
| 1706 | |
| 1707 | kfree(info->queues); |
| 1708 | info->queues = NULL; |
| 1709 | } |
| 1710 | |
| 1711 | static int xennet_create_queues(struct netfront_info *info, |
| 1712 | unsigned int num_queues) |
| 1713 | { |
| 1714 | unsigned int i; |
| 1715 | int ret; |
| 1716 | |
| 1717 | info->queues = kcalloc(num_queues, sizeof(struct netfront_queue), |
| 1718 | GFP_KERNEL); |
| 1719 | if (!info->queues) |
| 1720 | return -ENOMEM; |
| 1721 | |
| 1722 | rtnl_lock(); |
| 1723 | |
| 1724 | for (i = 0; i < num_queues; i++) { |
| 1725 | struct netfront_queue *queue = &info->queues[i]; |
| 1726 | |
| 1727 | queue->id = i; |
| 1728 | queue->info = info; |
| 1729 | |
| 1730 | ret = xennet_init_queue(queue); |
| 1731 | if (ret < 0) { |
David Vrabel | 69cb852 | 2014-07-31 17:38:24 +0100 | [diff] [blame] | 1732 | dev_warn(&info->netdev->dev, |
| 1733 | "only created %d queues\n", i); |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 1734 | num_queues = i; |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | netif_napi_add(queue->info->netdev, &queue->napi, |
| 1739 | xennet_poll, 64); |
| 1740 | if (netif_running(info->netdev)) |
| 1741 | napi_enable(&queue->napi); |
| 1742 | } |
| 1743 | |
| 1744 | netif_set_real_num_tx_queues(info->netdev, num_queues); |
| 1745 | |
| 1746 | rtnl_unlock(); |
| 1747 | |
| 1748 | if (num_queues == 0) { |
| 1749 | dev_err(&info->netdev->dev, "no queues\n"); |
| 1750 | return -EINVAL; |
| 1751 | } |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1755 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 1756 | static int talk_to_netback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1757 | struct netfront_info *info) |
| 1758 | { |
| 1759 | const char *message; |
| 1760 | struct xenbus_transaction xbt; |
| 1761 | int err; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1762 | unsigned int feature_split_evtchn; |
| 1763 | unsigned int i = 0; |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1764 | unsigned int max_queues = 0; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1765 | struct netfront_queue *queue = NULL; |
| 1766 | unsigned int num_queues = 1; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1767 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1768 | info->netdev->irq = 0; |
| 1769 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1770 | /* Check if backend supports multiple queues */ |
| 1771 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 1772 | "multi-queue-max-queues", "%u", &max_queues); |
| 1773 | if (err < 0) |
| 1774 | max_queues = 1; |
| 1775 | num_queues = min(max_queues, xennet_max_queues); |
| 1776 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1777 | /* Check feature-split-event-channels */ |
| 1778 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 1779 | "feature-split-event-channels", "%u", |
| 1780 | &feature_split_evtchn); |
| 1781 | if (err < 0) |
| 1782 | feature_split_evtchn = 0; |
| 1783 | |
| 1784 | /* Read mac addr. */ |
| 1785 | err = xen_net_read_mac(dev, info->netdev->dev_addr); |
| 1786 | if (err) { |
| 1787 | xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1788 | goto out; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 1791 | if (info->queues) |
| 1792 | xennet_destroy_queues(info); |
| 1793 | |
| 1794 | err = xennet_create_queues(info, num_queues); |
| 1795 | if (err < 0) |
| 1796 | goto destroy_ring; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1797 | |
| 1798 | /* Create shared ring, alloc event channel -- for each queue */ |
| 1799 | for (i = 0; i < num_queues; ++i) { |
| 1800 | queue = &info->queues[i]; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1801 | err = setup_netfront(dev, queue, feature_split_evtchn); |
| 1802 | if (err) { |
David Vrabel | ce58725 | 2014-06-18 10:47:28 +0100 | [diff] [blame] | 1803 | /* setup_netfront() will tidy up the current |
| 1804 | * queue on error, but we need to clean up |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1805 | * those already allocated. |
| 1806 | */ |
| 1807 | if (i > 0) { |
| 1808 | rtnl_lock(); |
| 1809 | netif_set_real_num_tx_queues(info->netdev, i); |
| 1810 | rtnl_unlock(); |
| 1811 | goto destroy_ring; |
| 1812 | } else { |
| 1813 | goto out; |
| 1814 | } |
| 1815 | } |
| 1816 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1817 | |
| 1818 | again: |
| 1819 | err = xenbus_transaction_start(&xbt); |
| 1820 | if (err) { |
| 1821 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 1822 | goto destroy_ring; |
| 1823 | } |
| 1824 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1825 | if (num_queues == 1) { |
| 1826 | err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */ |
| 1827 | if (err) |
| 1828 | goto abort_transaction_no_dev_fatal; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1829 | } else { |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1830 | /* Write the number of queues */ |
| 1831 | err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", |
| 1832 | "%u", num_queues); |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1833 | if (err) { |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1834 | message = "writing multi-queue-num-queues"; |
| 1835 | goto abort_transaction_no_dev_fatal; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1836 | } |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1837 | |
| 1838 | /* Write the keys for each queue */ |
| 1839 | for (i = 0; i < num_queues; ++i) { |
| 1840 | queue = &info->queues[i]; |
| 1841 | err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */ |
| 1842 | if (err) |
| 1843 | goto abort_transaction_no_dev_fatal; |
Wei Liu | d634bf2 | 2013-05-22 06:34:46 +0000 | [diff] [blame] | 1844 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1847 | /* The remaining keys are not queue-specific */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1848 | err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u", |
| 1849 | 1); |
| 1850 | if (err) { |
| 1851 | message = "writing request-rx-copy"; |
| 1852 | goto abort_transaction; |
| 1853 | } |
| 1854 | |
| 1855 | err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1); |
| 1856 | if (err) { |
| 1857 | message = "writing feature-rx-notify"; |
| 1858 | goto abort_transaction; |
| 1859 | } |
| 1860 | |
| 1861 | err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1); |
| 1862 | if (err) { |
| 1863 | message = "writing feature-sg"; |
| 1864 | goto abort_transaction; |
| 1865 | } |
| 1866 | |
| 1867 | err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1); |
| 1868 | if (err) { |
| 1869 | message = "writing feature-gso-tcpv4"; |
| 1870 | goto abort_transaction; |
| 1871 | } |
| 1872 | |
Paul Durrant | 2c0057d | 2014-01-15 17:30:33 +0000 | [diff] [blame] | 1873 | err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1"); |
| 1874 | if (err) { |
| 1875 | message = "writing feature-gso-tcpv6"; |
| 1876 | goto abort_transaction; |
| 1877 | } |
| 1878 | |
| 1879 | err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload", |
| 1880 | "1"); |
| 1881 | if (err) { |
| 1882 | message = "writing feature-ipv6-csum-offload"; |
| 1883 | goto abort_transaction; |
| 1884 | } |
| 1885 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1886 | err = xenbus_transaction_end(xbt, 0); |
| 1887 | if (err) { |
| 1888 | if (err == -EAGAIN) |
| 1889 | goto again; |
| 1890 | xenbus_dev_fatal(dev, err, "completing transaction"); |
| 1891 | goto destroy_ring; |
| 1892 | } |
| 1893 | |
| 1894 | return 0; |
| 1895 | |
| 1896 | abort_transaction: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1897 | xenbus_dev_fatal(dev, err, "%s", message); |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 1898 | abort_transaction_no_dev_fatal: |
| 1899 | xenbus_transaction_end(xbt, 1); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1900 | destroy_ring: |
| 1901 | xennet_disconnect_backend(info); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1902 | kfree(info->queues); |
| 1903 | info->queues = NULL; |
| 1904 | rtnl_lock(); |
| 1905 | netif_set_real_num_tx_queues(info->netdev, 0); |
David Vrabel | db8c8ab | 2014-07-31 17:38:22 +0100 | [diff] [blame] | 1906 | rtnl_unlock(); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1907 | out: |
| 1908 | return err; |
| 1909 | } |
| 1910 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1911 | static int xennet_connect(struct net_device *dev) |
| 1912 | { |
| 1913 | struct netfront_info *np = netdev_priv(dev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1914 | unsigned int num_queues = 0; |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 1915 | int err; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1916 | unsigned int feature_rx_copy; |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1917 | unsigned int j = 0; |
| 1918 | struct netfront_queue *queue = NULL; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1919 | |
| 1920 | err = xenbus_scanf(XBT_NIL, np->xbdev->otherend, |
| 1921 | "feature-rx-copy", "%u", &feature_rx_copy); |
| 1922 | if (err != 1) |
| 1923 | feature_rx_copy = 0; |
| 1924 | |
| 1925 | if (!feature_rx_copy) { |
| 1926 | dev_info(&dev->dev, |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1927 | "backend does not support copying receive path\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1928 | return -ENODEV; |
| 1929 | } |
| 1930 | |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 1931 | err = talk_to_netback(np->xbdev, np); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1932 | if (err) |
| 1933 | return err; |
| 1934 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1935 | /* talk_to_netback() sets the correct number of queues */ |
| 1936 | num_queues = dev->real_num_tx_queues; |
| 1937 | |
Ian Campbell | 1ba37c5 | 2011-05-24 21:56:02 +0000 | [diff] [blame] | 1938 | rtnl_lock(); |
Michał Mirosław | fb50793 | 2011-03-31 01:01:35 +0000 | [diff] [blame] | 1939 | netdev_update_features(dev); |
Ian Campbell | 1ba37c5 | 2011-05-24 21:56:02 +0000 | [diff] [blame] | 1940 | rtnl_unlock(); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1941 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1942 | /* |
David Vrabel | a5b5dc3 | 2014-07-31 17:38:23 +0100 | [diff] [blame] | 1943 | * All public and private state should now be sane. Get |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1944 | * ready to start sending and receiving packets and give the driver |
| 1945 | * domain a kick because we've probably just requeued some |
| 1946 | * packets. |
| 1947 | */ |
| 1948 | netif_carrier_on(np->netdev); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1949 | for (j = 0; j < num_queues; ++j) { |
| 1950 | queue = &np->queues[j]; |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 1951 | |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1952 | notify_remote_via_irq(queue->tx_irq); |
| 1953 | if (queue->tx_irq != queue->rx_irq) |
| 1954 | notify_remote_via_irq(queue->rx_irq); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1955 | |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 1956 | spin_lock_irq(&queue->tx_lock); |
| 1957 | xennet_tx_buf_gc(queue); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1958 | spin_unlock_irq(&queue->tx_lock); |
David Vrabel | f50b407 | 2014-07-02 16:09:14 +0100 | [diff] [blame] | 1959 | |
| 1960 | spin_lock_bh(&queue->rx_lock); |
| 1961 | xennet_alloc_rx_buffers(queue); |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 1962 | spin_unlock_bh(&queue->rx_lock); |
| 1963 | } |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * Callback received when the backend's state changes. |
| 1970 | */ |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 1971 | static void netback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1972 | enum xenbus_state backend_state) |
| 1973 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1974 | struct netfront_info *np = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1975 | struct net_device *netdev = np->netdev; |
| 1976 | |
| 1977 | dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state)); |
| 1978 | |
| 1979 | switch (backend_state) { |
| 1980 | case XenbusStateInitialising: |
| 1981 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 1982 | case XenbusStateReconfiguring: |
| 1983 | case XenbusStateReconfigured: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1984 | case XenbusStateUnknown: |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1985 | break; |
| 1986 | |
| 1987 | case XenbusStateInitWait: |
| 1988 | if (dev->state != XenbusStateInitialising) |
| 1989 | break; |
| 1990 | if (xennet_connect(netdev) != 0) |
| 1991 | break; |
| 1992 | xenbus_switch_state(dev, XenbusStateConnected); |
Laszlo Ersek | 08e34eb | 2011-12-11 01:48:59 +0000 | [diff] [blame] | 1993 | break; |
| 1994 | |
| 1995 | case XenbusStateConnected: |
Amerigo Wang | ee89bab | 2012-08-09 22:14:56 +0000 | [diff] [blame] | 1996 | netdev_notify_peers(netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1997 | break; |
| 1998 | |
David Vrabel | bce3ea8 | 2014-02-04 18:50:26 +0000 | [diff] [blame] | 1999 | case XenbusStateClosed: |
| 2000 | if (dev->state == XenbusStateClosed) |
| 2001 | break; |
| 2002 | /* Missed the backend's CLOSING state -- fallthrough */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2003 | case XenbusStateClosing: |
| 2004 | xenbus_frontend_closed(dev); |
| 2005 | break; |
| 2006 | } |
| 2007 | } |
| 2008 | |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2009 | static const struct xennet_stat { |
| 2010 | char name[ETH_GSTRING_LEN]; |
| 2011 | u16 offset; |
| 2012 | } xennet_stats[] = { |
| 2013 | { |
| 2014 | "rx_gso_checksum_fixup", |
| 2015 | offsetof(struct netfront_info, rx_gso_checksum_fixup) |
| 2016 | }, |
| 2017 | }; |
| 2018 | |
| 2019 | static int xennet_get_sset_count(struct net_device *dev, int string_set) |
| 2020 | { |
| 2021 | switch (string_set) { |
| 2022 | case ETH_SS_STATS: |
| 2023 | return ARRAY_SIZE(xennet_stats); |
| 2024 | default: |
| 2025 | return -EINVAL; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | static void xennet_get_ethtool_stats(struct net_device *dev, |
| 2030 | struct ethtool_stats *stats, u64 * data) |
| 2031 | { |
| 2032 | void *np = netdev_priv(dev); |
| 2033 | int i; |
| 2034 | |
| 2035 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) |
Andrew J. Bennieston | 2688fcb | 2014-06-04 10:30:44 +0100 | [diff] [blame] | 2036 | data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset)); |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data) |
| 2040 | { |
| 2041 | int i; |
| 2042 | |
| 2043 | switch (stringset) { |
| 2044 | case ETH_SS_STATS: |
| 2045 | for (i = 0; i < ARRAY_SIZE(xennet_stats); i++) |
| 2046 | memcpy(data + i * ETH_GSTRING_LEN, |
| 2047 | xennet_stats[i].name, ETH_GSTRING_LEN); |
| 2048 | break; |
| 2049 | } |
| 2050 | } |
| 2051 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 2052 | static const struct ethtool_ops xennet_ethtool_ops = |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2053 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2054 | .get_link = ethtool_op_get_link, |
Ian Campbell | e0ce4af | 2011-01-27 04:14:03 +0000 | [diff] [blame] | 2055 | |
| 2056 | .get_sset_count = xennet_get_sset_count, |
| 2057 | .get_ethtool_stats = xennet_get_ethtool_stats, |
| 2058 | .get_strings = xennet_get_strings, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2059 | }; |
| 2060 | |
| 2061 | #ifdef CONFIG_SYSFS |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2062 | static ssize_t show_rxbuf(struct device *dev, |
| 2063 | struct device_attribute *attr, char *buf) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2064 | { |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2065 | return sprintf(buf, "%lu\n", NET_RX_RING_SIZE); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2068 | static ssize_t store_rxbuf(struct device *dev, |
| 2069 | struct device_attribute *attr, |
| 2070 | const char *buf, size_t len) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2071 | { |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2072 | char *endp; |
| 2073 | unsigned long target; |
| 2074 | |
| 2075 | if (!capable(CAP_NET_ADMIN)) |
| 2076 | return -EPERM; |
| 2077 | |
| 2078 | target = simple_strtoul(buf, &endp, 0); |
| 2079 | if (endp == buf) |
| 2080 | return -EBADMSG; |
| 2081 | |
David Vrabel | 1f3c2eb | 2014-10-22 11:17:06 +0100 | [diff] [blame] | 2082 | /* rxbuf_min and rxbuf_max are no longer configurable. */ |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2083 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2084 | return len; |
| 2085 | } |
| 2086 | |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 2087 | static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf); |
| 2088 | static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf); |
| 2089 | static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL); |
| 2090 | |
| 2091 | static struct attribute *xennet_dev_attrs[] = { |
| 2092 | &dev_attr_rxbuf_min.attr, |
| 2093 | &dev_attr_rxbuf_max.attr, |
| 2094 | &dev_attr_rxbuf_cur.attr, |
| 2095 | NULL |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2096 | }; |
| 2097 | |
Takashi Iwai | 27b917e | 2015-02-04 14:38:55 +0100 | [diff] [blame] | 2098 | static const struct attribute_group xennet_dev_group = { |
| 2099 | .attrs = xennet_dev_attrs |
| 2100 | }; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2101 | #endif /* CONFIG_SYSFS */ |
| 2102 | |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 2103 | static int xennet_remove(struct xenbus_device *dev) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2104 | { |
Greg Kroah-Hartman | 1b713e0 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 2105 | struct netfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2106 | |
| 2107 | dev_dbg(&dev->dev, "%s\n", dev->nodename); |
| 2108 | |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2109 | xennet_disconnect_backend(info); |
| 2110 | |
Ian Campbell | 6bc96d0 | 2012-06-25 22:48:41 +0000 | [diff] [blame] | 2111 | unregister_netdev(info->netdev); |
| 2112 | |
David Vrabel | ad06811 | 2015-05-27 15:46:10 +0100 | [diff] [blame] | 2113 | xennet_destroy_queues(info); |
David Vrabel | 900e183 | 2015-01-13 16:42:42 +0000 | [diff] [blame] | 2114 | xennet_free_netdev(info->netdev); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2115 | |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2119 | static const struct xenbus_device_id netfront_ids[] = { |
| 2120 | { "vif" }, |
| 2121 | { "" } |
| 2122 | }; |
| 2123 | |
| 2124 | static struct xenbus_driver netfront_driver = { |
| 2125 | .ids = netfront_ids, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2126 | .probe = netfront_probe, |
Bill Pemberton | 8e0e46b | 2012-12-03 09:24:22 -0500 | [diff] [blame] | 2127 | .remove = xennet_remove, |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2128 | .resume = netfront_resume, |
Ian Campbell | f502bf2 | 2010-08-18 23:27:49 +0000 | [diff] [blame] | 2129 | .otherend_changed = netback_changed, |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2130 | }; |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2131 | |
| 2132 | static int __init netif_init(void) |
| 2133 | { |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 2134 | if (!xen_domain()) |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2135 | return -ENODEV; |
| 2136 | |
Konrad Rzeszutek Wilk | 51c71a3 | 2013-11-26 15:05:40 -0500 | [diff] [blame] | 2137 | if (!xen_has_pv_nic_devices()) |
Igor Mammedov | b9136d2 | 2012-03-21 15:08:38 +0100 | [diff] [blame] | 2138 | return -ENODEV; |
| 2139 | |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 2140 | pr_info("Initialising Xen virtual ethernet driver\n"); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2141 | |
Andrew J. Bennieston | 50ee606 | 2014-06-04 10:30:45 +0100 | [diff] [blame] | 2142 | /* Allow as many queues as there are CPUs, by default */ |
| 2143 | xennet_max_queues = num_online_cpus(); |
| 2144 | |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 2145 | return xenbus_register_frontend(&netfront_driver); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2146 | } |
| 2147 | module_init(netif_init); |
| 2148 | |
| 2149 | |
| 2150 | static void __exit netif_exit(void) |
| 2151 | { |
Al Viro | ffb78a2 | 2008-11-22 17:38:14 +0000 | [diff] [blame] | 2152 | xenbus_unregister_driver(&netfront_driver); |
Jeremy Fitzhardinge | 0d16021 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2153 | } |
| 2154 | module_exit(netif_exit); |
| 2155 | |
| 2156 | MODULE_DESCRIPTION("Xen virtual network device frontend"); |
| 2157 | MODULE_LICENSE("GPL"); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 2158 | MODULE_ALIAS("xen:vif"); |
Mark McLoughlin | 4f93f09b | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 2159 | MODULE_ALIAS("xennet"); |