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