blob: 28204bc4f369550bc816a81fcf6b933d71e52af4 [file] [log] [blame]
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001/*
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 Perches383eda32013-06-27 21:57:49 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070034#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 Liu9ecd1a72013-04-22 02:20:41 +000041#include <net/tcp.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070042#include <linux/udp.h>
43#include <linux/moduleparam.h>
44#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070046#include <net/ip.h>
47
Stefano Stabellinica981632012-08-08 17:21:23 +000048#include <asm/xen/page.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070049#include <xen/xen.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070050#include <xen/xenbus.h>
51#include <xen/events.h>
52#include <xen/page.h>
Igor Mammedovb9136d22012-03-21 15:08:38 +010053#include <xen/platform_pci.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070054#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. Bennieston50ee6062014-06-04 10:30:45 +010060/* Module parameters */
61static unsigned int xennet_max_queues;
62module_param_named(max_queues, xennet_max_queues, uint, 0644);
63MODULE_PARM_DESC(max_queues,
64 "Maximum number of queues per virtual interface");
65
Stephen Hemminger0fc0b732009-09-02 01:03:33 -070066static const struct ethtool_ops xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070067
68struct netfront_cb {
Ian Campbell36832432012-08-22 00:26:47 +000069 int pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070070};
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 Fitzhardinge667c78af2010-12-08 12:39:12 -080078#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 Liu40206dd2012-01-26 07:23:23 +000080#define TX_MAX_TARGET min_t(int, NET_TX_RING_SIZE, 256)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070081
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +010082/* 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 hemmingere00f85b2011-06-21 05:35:31 +000088struct 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. Bennieston2688fcb2014-06-04 10:30:44 +010096struct netfront_info;
97
98struct netfront_queue {
99 unsigned int id; /* Queue ID, 0-based */
100 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
101 struct netfront_info *info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700102
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700103 struct napi_struct napi;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700104
Wei Liud634bf22013-05-22 06:34:46 +0000105 /* 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. Bennieston2688fcb2014-06-04 10:30:44 +0100111 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
112 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700113
114 spinlock_t tx_lock;
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700115 struct xen_netif_tx_front_ring tx;
116 int tx_ring_ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700117
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 Yamahata1ffb40b2008-07-08 15:06:31 -0700129 unsigned long link;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700130 } 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 Licefe0072014-01-28 11:35:42 +0800133 struct page *grant_tx_page[NET_TX_RING_SIZE];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700134 unsigned tx_skb_freelist;
135
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700136 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700149 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700153 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. Bennieston2688fcb2014-06-04 10:30:44 +0100156};
157
158struct 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 Campbelle0ce4af2011-01-27 04:14:03 +0000166
167 /* Statistics */
stephen hemmingere00f85b2011-06-21 05:35:31 +0000168 struct netfront_stats __percpu *stats;
169
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100170 atomic_t rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700171};
172
173struct 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 Yamahata1ffb40b2008-07-08 15:06:31 -0700178static void skb_entry_set_link(union skb_entry *list, unsigned short id)
179{
180 list->link = id;
181}
182
183static int skb_entry_is_link(const union skb_entry *list)
184{
185 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
Eric Dumazet807540b2010-09-23 05:40:09 +0000186 return (unsigned long)list->skb < PAGE_OFFSET;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700187}
188
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700189/*
190 * Access macros for acquiring freeing slots in tx_skbs[].
191 */
192
193static void add_id_to_freelist(unsigned *head, union skb_entry *list,
194 unsigned short id)
195{
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700196 skb_entry_set_link(&list[id], *head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700197 *head = id;
198}
199
200static 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
208static int xennet_rxidx(RING_IDX idx)
209{
210 return idx & (NET_RX_RING_SIZE - 1);
211}
212
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100213static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700214 RING_IDX ri)
215{
216 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100217 struct sk_buff *skb = queue->rx_skbs[i];
218 queue->rx_skbs[i] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700219 return skb;
220}
221
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100222static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700223 RING_IDX ri)
224{
225 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100226 grant_ref_t ref = queue->grant_rx_ref[i];
227 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700228 return ref;
229}
230
231#ifdef CONFIG_SYSFS
232static int xennet_sysfs_addif(struct net_device *netdev);
233static 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ław3ad9b352011-11-16 14:05:33 +0000239static bool xennet_can_sg(struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700240{
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000241 return dev->features & NETIF_F_SG;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700242}
243
244
245static void rx_refill_timeout(unsigned long data)
246{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100247 struct netfront_queue *queue = (struct netfront_queue *)data;
248 napi_schedule(&queue->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700249}
250
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100251static int netfront_tx_slot_available(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700252{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100253 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
Eric Dumazet807540b2010-09-23 05:40:09 +0000254 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700255}
256
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100257static void xennet_maybe_wake_tx(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700258{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100259 struct net_device *dev = queue->info->netdev;
260 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700261
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100262 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
263 netfront_tx_slot_available(queue) &&
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700264 likely(netif_running(dev)))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100265 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700266}
267
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100268static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700269{
270 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700271 struct sk_buff *skb;
272 struct page *page;
273 int i, batch_target, notify;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100274 RING_IDX req_prod = queue->rx.req_prod_pvt;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700275 grant_ref_t ref;
276 unsigned long pfn;
277 void *vaddr;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700278 struct xen_netif_rx_request *req;
279
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100280 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700281 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. Bennieston2688fcb2014-06-04 10:30:44 +0100289 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700293 GFP_ATOMIC | __GFP_NOWARN);
294 if (unlikely(!skb))
295 goto no_skb;
296
Isaku Yamahata617a20b2008-10-14 17:50:42 -0700297 /* Align ip header to a 16 bytes boundary */
298 skb_reserve(skb, NET_IP_ALIGN);
299
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700300 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
301 if (!page) {
302 kfree_skb(skb);
303no_skb:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700304 /* Could not allocate any skbuffs. Try again later. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100305 mod_timer(&queue->rx_refill_timer,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700306 jiffies + (HZ/10));
Ma JieYuefdcf7762013-11-15 12:26:13 +0800307
308 /* Any skbuffs queued for refill? Force them out. */
309 if (i != 0)
310 goto refill;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700311 break;
312 }
313
Jan Beulich093b9c72013-07-17 08:09:37 +0100314 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100315 __skb_queue_tail(&queue->rx_batch, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700316 }
317
318 /* Is the batch large enough to be worthwhile? */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100319 if (i < (queue->rx_target/2)) {
320 if (req_prod > queue->rx.sring->req_prod)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700321 goto push;
322 return;
323 }
324
325 /* Adjust our fill target if we risked running out of buffers. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100326 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700329
330 refill:
Jeremy Fitzhardinge5dcddfa2007-08-07 14:56:42 -0700331 for (i = 0; ; i++) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100332 skb = __skb_dequeue(&queue->rx_batch);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700333 if (skb == NULL)
334 break;
335
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100336 skb->dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700337
338 id = xennet_rxidx(req_prod + i);
339
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100340 BUG_ON(queue->rx_skbs[id]);
341 queue->rx_skbs[id] = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700342
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100343 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700344 BUG_ON((signed short)ref < 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100345 queue->grant_rx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700346
Ian Campbell01c68022011-10-05 00:28:47 +0000347 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700349
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100350 req = RING_GET_REQUEST(&queue->rx, req_prod + i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700351 gnttab_grant_foreign_access_ref(ref,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100352 queue->info->xbdev->otherend_id,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700353 pfn_to_mfn(pfn),
354 0);
355
356 req->id = id;
357 req->gref = ref;
358 }
359
Jeremy Fitzhardinge5dcddfa2007-08-07 14:56:42 -0700360 wmb(); /* barrier so backend seens requests */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700361
362 /* Above is a suitable barrier to ensure backend will see requests. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100363 queue->rx.req_prod_pvt = req_prod + i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700364 push:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100365 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700366 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100367 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700368}
369
370static int xennet_open(struct net_device *dev)
371{
372 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100373 unsigned int num_queues = dev->real_num_tx_queues;
374 unsigned int i = 0;
375 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700376
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100377 for (i = 0; i < num_queues; ++i) {
378 queue = &np->queues[i];
379 napi_enable(&queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700380
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100381 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700389 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700390
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100391 netif_tx_start_all_queues(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700392
393 return 0;
394}
395
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100396static void xennet_tx_buf_gc(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700397{
398 RING_IDX cons, prod;
399 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700400 struct sk_buff *skb;
401
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100402 BUG_ON(!netif_carrier_ok(queue->info->netdev));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700403
404 do {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100405 prod = queue->tx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700406 rmb(); /* Ensure we see responses up to 'rp'. */
407
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100408 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700409 struct xen_netif_tx_response *txrsp;
410
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100411 txrsp = RING_GET_RESPONSE(&queue->tx, cons);
Ian Campbellf942dc22011-03-15 00:06:18 +0000412 if (txrsp->status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700413 continue;
414
415 id = txrsp->id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100416 skb = queue->tx_skbs[id].skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700417 if (unlikely(gnttab_query_foreign_access(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100418 queue->grant_tx_ref[id]) != 0)) {
Joe Perches383eda32013-06-27 21:57:49 -0700419 pr_alert("%s: warning -- grant still in use by backend domain\n",
420 __func__);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700421 BUG();
422 }
423 gnttab_end_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100424 queue->grant_tx_ref[id], GNTMAP_readonly);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700425 gnttab_release_grant_reference(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100426 &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 Fitzhardinge0d160212007-07-17 18:37:06 -0700430 dev_kfree_skb_irq(skb);
431 }
432
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100433 queue->tx.rsp_cons = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700434
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. Bennieston2688fcb2014-06-04 10:30:44 +0100443 queue->tx.sring->rsp_event =
444 prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700445 mb(); /* update shared area */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100446 } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700447
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100448 xennet_maybe_wake_tx(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700449}
450
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100451static void xennet_make_frags(struct sk_buff *skb, struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700452 struct xen_netif_tx_request *tx)
453{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700454 char *data = skb->data;
455 unsigned long mfn;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100456 RING_IDX prod = queue->tx.req_prod_pvt;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700457 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 Campbellf942dc22011-03-15 00:06:18 +0000468 tx->flags |= XEN_NETTXF_more_data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700469 len -= tx->size;
470 data += tx->size;
471 offset = 0;
472
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100473 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700476 tx->id = id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100477 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700478 BUG_ON((signed short)ref < 0);
479
480 mfn = virt_to_mfn(data);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100481 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700482 mfn, GNTMAP_readonly);
483
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100484 queue->grant_tx_page[id] = virt_to_page(data);
485 tx->gref = queue->grant_tx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700486 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 Campbellf36c3742012-11-21 02:02:16 +0000494 struct page *page = skb_frag_page(frag);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700495
Ian Campbellf36c3742012-11-21 02:02:16 +0000496 len = skb_frag_size(frag);
497 offset = frag->page_offset;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700498
Ian Campbellf36c3742012-11-21 02:02:16 +0000499 /* Data must not cross a page boundary. */
500 BUG_ON(len + offset > PAGE_SIZE<<compound_order(page));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700501
Ian Campbellf36c3742012-11-21 02:02:16 +0000502 /* Skip unused frames from start of page */
503 page += offset >> PAGE_SHIFT;
504 offset &= ~PAGE_MASK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700505
Ian Campbellf36c3742012-11-21 02:02:16 +0000506 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. Bennieston2688fcb2014-06-04 10:30:44 +0100517 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 Campbellf36c3742012-11-21 02:02:16 +0000521 tx->id = id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100522 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Ian Campbellf36c3742012-11-21 02:02:16 +0000523 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. Bennieston2688fcb2014-06-04 10:30:44 +0100527 queue->info->xbdev->otherend_id,
Ian Campbellf36c3742012-11-21 02:02:16 +0000528 mfn, GNTMAP_readonly);
529
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100530 queue->grant_tx_page[id] = page;
531 tx->gref = queue->grant_tx_ref[id] = ref;
Ian Campbellf36c3742012-11-21 02:02:16 +0000532 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700546 }
547
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100548 queue->tx.req_prod_pvt = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700549}
550
Ian Campbellf36c3742012-11-21 02:02:16 +0000551/*
552 * Count how many ring slots are required to send the frags of this
553 * skb. Each frag might be a compound page.
554 */
555static 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. Bennieston50ee6062014-06-04 10:30:45 +0100574static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
575 void *accel_priv, select_queue_fallback_t fallback)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100576{
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100577 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. Bennieston2688fcb2014-06-04 10:30:44 +0100590}
591
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700592static 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 hemmingere00f85b2011-06-21 05:35:31 +0000596 struct netfront_stats *stats = this_cpu_ptr(np->stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700597 struct xen_netif_tx_request *tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700598 char *data = skb->data;
599 RING_IDX i;
600 grant_ref_t ref;
601 unsigned long mfn;
602 int notify;
Ian Campbellf36c3742012-11-21 02:02:16 +0000603 int slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700604 unsigned int offset = offset_in_page(data);
605 unsigned int len = skb_headlen(skb);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +0000606 unsigned long flags;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100607 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700617
Wei Liu9ecd1a72013-04-22 02:20:41 +0000618 /* 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 Campbellf36c3742012-11-21 02:02:16 +0000628 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700633 goto drop;
634 }
635
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100636 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700637
638 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000639 (slots > 1 && !xennet_can_sg(dev)) ||
Jesse Grossfc741212011-01-09 06:23:32 +0000640 netif_needs_gso(skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100641 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700642 goto drop;
643 }
644
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100645 i = queue->tx.req_prod_pvt;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700646
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100647 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
648 queue->tx_skbs[id].skb = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700649
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100650 tx = RING_GET_REQUEST(&queue->tx, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700651
652 tx->id = id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100653 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700654 BUG_ON((signed short)ref < 0);
655 mfn = virt_to_mfn(data);
656 gnttab_grant_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100657 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700660 tx->offset = offset;
661 tx->size = len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700662
663 tx->flags = 0;
664 if (skb->ip_summed == CHECKSUM_PARTIAL)
665 /* local packet? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000666 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700667 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
668 /* remote but checksummed. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000669 tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700670
671 if (skb_shinfo(skb)->gso_size) {
672 struct xen_netif_extra_info *gso;
673
674 gso = (struct xen_netif_extra_info *)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100675 RING_GET_REQUEST(&queue->tx, ++i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700676
Wei Liue2d617c2013-03-25 01:08:17 +0000677 tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700678
679 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000680 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700683 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700688 }
689
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100690 queue->tx.req_prod_pvt = i + 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700691
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100692 xennet_make_frags(skb, queue, tx);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700693 tx->size = skb->len;
694
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100695 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700696 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100697 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700698
stephen hemmingere00f85b2011-06-21 05:35:31 +0000699 u64_stats_update_begin(&stats->syncp);
700 stats->tx_bytes += skb->len;
701 stats->tx_packets++;
702 u64_stats_update_end(&stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700703
704 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100705 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700706
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100707 if (!netfront_tx_slot_available(queue))
708 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700709
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100710 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700711
Patrick McHardy6ed10652009-06-23 06:03:08 +0000712 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700713
714 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700715 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700716 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000717 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700718}
719
720static int xennet_close(struct net_device *dev)
721{
722 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100723 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700731 return 0;
732}
733
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100734static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700735 grant_ref_t ref)
736{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100737 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700738
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100739 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700745}
746
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100747static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700748 struct xen_netif_extra_info *extras,
749 RING_IDX rp)
750
751{
752 struct xen_netif_extra_info *extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100753 struct device *dev = &queue->info->netdev->dev;
754 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700755 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. Bennieston2688fcb2014-06-04 10:30:44 +0100769 RING_GET_RESPONSE(&queue->rx, ++cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700770
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. Bennieston2688fcb2014-06-04 10:30:44 +0100782 skb = xennet_get_rx_skb(queue, cons);
783 ref = xennet_get_rx_ref(queue, cons);
784 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700785 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
786
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100787 queue->rx.rsp_cons = cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700788 return err;
789}
790
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100791static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700792 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. Bennieston2688fcb2014-06-04 10:30:44 +0100797 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700801 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
Wei Liu7158ff62013-03-25 01:08:19 +0000802 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700803 int err = 0;
804 unsigned long ret;
805
Ian Campbellf942dc22011-03-15 00:06:18 +0000806 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100807 err = xennet_get_extras(queue, extras, rp);
808 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700809 }
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. Bennieston2688fcb2014-06-04 10:30:44 +0100817 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700818 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 Liu697089d2013-04-22 02:20:40 +0000825 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700826 */
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. Bennieston2688fcb2014-06-04 10:30:44 +0100838 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700839
840 __skb_queue_tail(list, skb);
841
842next:
Ian Campbellf942dc22011-03-15 00:06:18 +0000843 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700844 break;
845
Wei Liu7158ff62013-03-25 01:08:19 +0000846 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700847 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +0000848 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700849 err = -ENOENT;
850 break;
851 }
852
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100853 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 Liu7158ff62013-03-25 01:08:19 +0000856 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700857 }
858
Wei Liu7158ff62013-03-25 01:08:19 +0000859 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700860 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +0000861 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700862 err = -E2BIG;
863 }
864
865 if (unlikely(err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100866 queue->rx.rsp_cons = cons + slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700867
868 return err;
869}
870
871static 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 Perches383eda32013-06-27 21:57:49 -0700876 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700877 return -EINVAL;
878 }
879
Paul Durrant2c0057d2014-01-15 17:30:33 +0000880 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
881 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700882 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700883 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700884 return -EINVAL;
885 }
886
887 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000888 skb_shinfo(skb)->gso_type =
889 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
890 SKB_GSO_TCPV4 :
891 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700892
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. Bennieston2688fcb2014-06-04 10:30:44 +0100900static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700901 struct sk_buff *skb,
902 struct sk_buff_head *list)
903{
904 struct skb_shared_info *shinfo = skb_shinfo(skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100905 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700906 struct sk_buff *nskb;
907
908 while ((nskb = __skb_dequeue(list))) {
909 struct xen_netif_rx_response *rx =
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100910 RING_GET_RESPONSE(&queue->rx, ++cons);
Ian Campbell01c68022011-10-05 00:28:47 +0000911 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700912
Jan Beulich093b9c72013-07-17 08:09:37 +0100913 if (shinfo->nr_frags == MAX_SKB_FRAGS) {
914 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700915
Jan Beulich093b9c72013-07-17 08:09:37 +0100916 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 Fitzhardinge0d160212007-07-17 18:37:06 -0700923
924 skb_shinfo(nskb)->nr_frags = 0;
925 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700926 }
927
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700928 return cons;
929}
930
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000931static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700932{
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000933 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000934
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. Bennieston2688fcb2014-06-04 10:30:44 +0100943 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000944 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000945 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000946 }
947
948 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
949 if (skb->ip_summed != CHECKSUM_PARTIAL)
950 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700951
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000952 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700953}
954
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100955static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700956 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700957{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100958 struct netfront_stats *stats = this_cpu_ptr(queue->info->stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700959 int packets_dropped = 0;
960 struct sk_buff *skb;
961
962 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +0000963 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700964
Jan Beulich093b9c72013-07-17 08:09:37 +0100965 if (pull_to > skb_headlen(skb))
966 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700967
968 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100969 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +0000970 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700971
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100972 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000973 kfree_skb(skb);
974 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100975 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000976 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700977 }
978
stephen hemmingere00f85b2011-06-21 05:35:31 +0000979 u64_stats_update_begin(&stats->syncp);
980 stats->rx_packets++;
981 stats->rx_bytes += skb->len;
982 u64_stats_update_end(&stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700983
984 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100985 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700986 }
987
988 return packets_dropped;
989}
990
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700991static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700992{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100993 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
994 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700995 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 Hemmingerbea33482007-10-03 16:41:36 -07001000 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001001 struct sk_buff_head rxq;
1002 struct sk_buff_head errq;
1003 struct sk_buff_head tmpq;
1004 unsigned long flags;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001005 int err;
1006
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001007 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001008
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001009 skb_queue_head_init(&rxq);
1010 skb_queue_head_init(&errq);
1011 skb_queue_head_init(&tmpq);
1012
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001013 rp = queue->rx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001014 rmb(); /* Ensure we see queued responses up to 'rp'. */
1015
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001016 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001017 work_done = 0;
1018 while ((i != rp) && (work_done < budget)) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001019 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001020 memset(extras, 0, sizeof(rinfo.extras));
1021
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001022 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001023
1024 if (unlikely(err)) {
1025err:
1026 while ((skb = __skb_dequeue(&tmpq)))
1027 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001028 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001029 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001030 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. Bennieston2688fcb2014-06-04 10:30:44 +01001041 queue->rx.rsp_cons += skb_queue_len(&tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001042 goto err;
1043 }
1044 }
1045
Ian Campbell36832432012-08-22 00:26:47 +00001046 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 Fitzhardinge0d160212007-07-17 18:37:06 -07001049
Ian Campbell36832432012-08-22 00:26:47 +00001050 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 Beulich093b9c72013-07-17 08:09:37 +01001053 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001054
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001055 i = xennet_fill_frags(queue, skb, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001056
Ian Campbellf942dc22011-03-15 00:06:18 +00001057 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001058 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001059 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001060 skb->ip_summed = CHECKSUM_UNNECESSARY;
1061
1062 __skb_queue_tail(&rxq, skb);
1063
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001064 queue->rx.rsp_cons = ++i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001065 work_done++;
1066 }
1067
Wang Chen56cfe5d2008-05-22 18:09:06 +08001068 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001069
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001070 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001071
1072 /* If we get a callback with very few responses, reduce fill target. */
1073 /* NB. Note exponential increase, linear decrease. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001074 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 Fitzhardinge0d160212007-07-17 18:37:06 -07001078
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001079 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001080
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001081 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001082 int more_to_do = 0;
1083
Wei Liu99d3d582013-09-30 13:46:34 +01001084 napi_gro_flush(napi, false);
1085
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001086 local_irq_save(flags);
1087
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001088 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001089 if (!more_to_do)
Ben Hutchings288379f2009-01-19 16:43:59 -08001090 __napi_complete(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001091
1092 local_irq_restore(flags);
1093 }
1094
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001095 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001096
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001097 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001098}
1099
1100static int xennet_change_mtu(struct net_device *dev, int mtu)
1101{
Wei Liu9ecd1a72013-04-22 02:20:41 +00001102 int max = xennet_can_sg(dev) ?
1103 XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001104
1105 if (mtu > max)
1106 return -EINVAL;
1107 dev->mtu = mtu;
1108 return 0;
1109}
1110
stephen hemmingere00f85b2011-06-21 05:35:31 +00001111static 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. Biederman57a77442014-03-13 21:26:42 -07001123 start = u64_stats_fetch_begin_irq(&stats->syncp);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001124
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. Biederman57a77442014-03-13 21:26:42 -07001129 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001130
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. Bennieston2688fcb2014-06-04 10:30:44 +01001143static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001144{
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. Bennieston2688fcb2014-06-04 10:30:44 +01001150 if (skb_entry_is_link(&queue->tx_skbs[i]))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001151 continue;
1152
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001153 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 Licefe0072014-01-28 11:35:42 +08001156 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001157 (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 Fitzhardinge0d160212007-07-17 18:37:06 -07001161 dev_kfree_skb_irq(skb);
1162 }
1163}
1164
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001165static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001166{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001167 int id, ref;
1168
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001169 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001170
1171 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001172 struct sk_buff *skb;
1173 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001174
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001175 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001176 if (!skb)
1177 continue;
1178
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001179 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001180 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. Bennieston2688fcb2014-06-04 10:30:44 +01001191 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001192
Annie Licefe0072014-01-28 11:35:42 +08001193 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001194 }
1195
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001196 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001197}
1198
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001199static netdev_features_t xennet_fix_features(struct net_device *dev,
1200 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001201{
1202 struct netfront_info *np = netdev_priv(dev);
1203 int val;
1204
1205 if (features & NETIF_F_SG) {
1206 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1207 "%d", &val) < 0)
1208 val = 0;
1209
1210 if (!val)
1211 features &= ~NETIF_F_SG;
1212 }
1213
Paul Durrant2c0057d2014-01-15 17:30:33 +00001214 if (features & NETIF_F_IPV6_CSUM) {
1215 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1216 "feature-ipv6-csum-offload", "%d", &val) < 0)
1217 val = 0;
1218
1219 if (!val)
1220 features &= ~NETIF_F_IPV6_CSUM;
1221 }
1222
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001223 if (features & NETIF_F_TSO) {
1224 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1225 "feature-gso-tcpv4", "%d", &val) < 0)
1226 val = 0;
1227
1228 if (!val)
1229 features &= ~NETIF_F_TSO;
1230 }
1231
Paul Durrant2c0057d2014-01-15 17:30:33 +00001232 if (features & NETIF_F_TSO6) {
1233 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1234 "feature-gso-tcpv6", "%d", &val) < 0)
1235 val = 0;
1236
1237 if (!val)
1238 features &= ~NETIF_F_TSO6;
1239 }
1240
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001241 return features;
1242}
1243
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001244static int xennet_set_features(struct net_device *dev,
1245 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001246{
1247 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1248 netdev_info(dev, "Reducing MTU because no SG offload");
1249 dev->mtu = ETH_DATA_LEN;
1250 }
1251
1252 return 0;
1253}
1254
Wei Liud634bf22013-05-22 06:34:46 +00001255static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001256{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001257 struct netfront_queue *queue = dev_id;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001258 unsigned long flags;
1259
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001260 spin_lock_irqsave(&queue->tx_lock, flags);
1261 xennet_tx_buf_gc(queue);
1262 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001263
1264 return IRQ_HANDLED;
1265}
1266
Wei Liud634bf22013-05-22 06:34:46 +00001267static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1268{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001269 struct netfront_queue *queue = dev_id;
1270 struct net_device *dev = queue->info->netdev;
Wei Liud634bf22013-05-22 06:34:46 +00001271
1272 if (likely(netif_carrier_ok(dev) &&
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001273 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
David Vrabel76541862014-06-18 10:47:27 +01001274 napi_schedule(&queue->napi);
Wei Liud634bf22013-05-22 06:34:46 +00001275
1276 return IRQ_HANDLED;
1277}
1278
1279static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1280{
1281 xennet_tx_interrupt(irq, dev_id);
1282 xennet_rx_interrupt(irq, dev_id);
1283 return IRQ_HANDLED;
1284}
1285
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001286#ifdef CONFIG_NET_POLL_CONTROLLER
1287static void xennet_poll_controller(struct net_device *dev)
1288{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001289 /* Poll each queue */
1290 struct netfront_info *info = netdev_priv(dev);
1291 unsigned int num_queues = dev->real_num_tx_queues;
1292 unsigned int i;
1293 for (i = 0; i < num_queues; ++i)
1294 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001295}
1296#endif
1297
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001298static const struct net_device_ops xennet_netdev_ops = {
1299 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001300 .ndo_stop = xennet_close,
1301 .ndo_start_xmit = xennet_start_xmit,
1302 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001303 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001304 .ndo_set_mac_address = eth_mac_addr,
1305 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001306 .ndo_fix_features = xennet_fix_features,
1307 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001308 .ndo_select_queue = xennet_select_queue,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001309#ifdef CONFIG_NET_POLL_CONTROLLER
1310 .ndo_poll_controller = xennet_poll_controller,
1311#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001312};
1313
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001314static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001315{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001316 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001317 struct net_device *netdev;
1318 struct netfront_info *np;
1319
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001320 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001321 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001322 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001323
1324 np = netdev_priv(netdev);
1325 np->xbdev = dev;
1326
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001327 /* No need to use rtnl_lock() before the call below as it
1328 * happens before register_netdev().
1329 */
1330 netif_set_real_num_tx_queues(netdev, 0);
1331 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001332
stephen hemmingere00f85b2011-06-21 05:35:31 +00001333 err = -ENOMEM;
WANG Cong1c213bd2014-02-13 11:46:28 -08001334 np->stats = netdev_alloc_pcpu_stats(struct netfront_stats);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001335 if (np->stats == NULL)
1336 goto exit;
1337
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001338 netdev->netdev_ops = &xennet_netdev_ops;
1339
Michał Mirosławfb507932011-03-31 01:01:35 +00001340 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1341 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001342 netdev->hw_features = NETIF_F_SG |
1343 NETIF_F_IPV6_CSUM |
1344 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001345
Ian Campbellfc3e5942011-04-04 11:07:57 -07001346 /*
1347 * Assume that all hw features are available for now. This set
1348 * will be adjusted by the call to netdev_update_features() in
1349 * xennet_connect() which is the earliest point where we can
1350 * negotiate with the backend regarding supported features.
1351 */
1352 netdev->features |= netdev->hw_features;
1353
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001354 netdev->ethtool_ops = &xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001355 SET_NETDEV_DEV(netdev, &dev->dev);
1356
Wei Liu9ecd1a72013-04-22 02:20:41 +00001357 netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
1358
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001359 np->netdev = netdev;
1360
1361 netif_carrier_off(netdev);
1362
1363 return netdev;
1364
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001365 exit:
1366 free_netdev(netdev);
1367 return ERR_PTR(err);
1368}
1369
1370/**
1371 * Entry point to this code when a new device is created. Allocate the basic
1372 * structures and the ring buffers for communication with the backend, and
1373 * inform the backend of the appropriate details for those.
1374 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001375static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001376 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001377{
1378 int err;
1379 struct net_device *netdev;
1380 struct netfront_info *info;
1381
1382 netdev = xennet_create_dev(dev);
1383 if (IS_ERR(netdev)) {
1384 err = PTR_ERR(netdev);
1385 xenbus_dev_fatal(dev, err, "creating netdev");
1386 return err;
1387 }
1388
1389 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001390 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001391
1392 err = register_netdev(info->netdev);
1393 if (err) {
Joe Perches383eda32013-06-27 21:57:49 -07001394 pr_warn("%s: register_netdev err=%d\n", __func__, err);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001395 goto fail;
1396 }
1397
1398 err = xennet_sysfs_addif(info->netdev);
1399 if (err) {
1400 unregister_netdev(info->netdev);
Joe Perches383eda32013-06-27 21:57:49 -07001401 pr_warn("%s: add sysfs failed err=%d\n", __func__, err);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001402 goto fail;
1403 }
1404
1405 return 0;
1406
1407 fail:
1408 free_netdev(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001409 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001410 return err;
1411}
1412
1413static void xennet_end_access(int ref, void *page)
1414{
1415 /* This frees the page as a side-effect */
1416 if (ref != GRANT_INVALID_REF)
1417 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1418}
1419
1420static void xennet_disconnect_backend(struct netfront_info *info)
1421{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001422 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001423 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001424
David Vrabelf9feb1e2014-07-02 16:09:15 +01001425 netif_carrier_off(info->netdev);
1426
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001427 for (i = 0; i < num_queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001428 struct netfront_queue *queue = &info->queues[i];
1429
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001430 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1431 unbind_from_irqhandler(queue->tx_irq, queue);
1432 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1433 unbind_from_irqhandler(queue->tx_irq, queue);
1434 unbind_from_irqhandler(queue->rx_irq, queue);
1435 }
1436 queue->tx_evtchn = queue->rx_evtchn = 0;
1437 queue->tx_irq = queue->rx_irq = 0;
1438
David Vrabelf9feb1e2014-07-02 16:09:15 +01001439 napi_synchronize(&queue->napi);
1440
David Vrabela5b5dc32014-07-31 17:38:23 +01001441 xennet_release_tx_bufs(queue);
1442 xennet_release_rx_bufs(queue);
1443 gnttab_free_grant_references(queue->gref_tx_head);
1444 gnttab_free_grant_references(queue->gref_rx_head);
1445
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001446 /* End access and free the pages */
1447 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1448 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1449
1450 queue->tx_ring_ref = GRANT_INVALID_REF;
1451 queue->rx_ring_ref = GRANT_INVALID_REF;
1452 queue->tx.sring = NULL;
1453 queue->rx.sring = NULL;
Wei Liud634bf22013-05-22 06:34:46 +00001454 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001455}
1456
1457/**
1458 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1459 * driver restart. We tear down our netif structure and recreate it, but
1460 * leave the device-layer structures intact so that this is transparent to the
1461 * rest of the kernel.
1462 */
1463static int netfront_resume(struct xenbus_device *dev)
1464{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001465 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001466
1467 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1468
1469 xennet_disconnect_backend(info);
1470 return 0;
1471}
1472
1473static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1474{
1475 char *s, *e, *macstr;
1476 int i;
1477
1478 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1479 if (IS_ERR(macstr))
1480 return PTR_ERR(macstr);
1481
1482 for (i = 0; i < ETH_ALEN; i++) {
1483 mac[i] = simple_strtoul(s, &e, 16);
1484 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1485 kfree(macstr);
1486 return -ENOENT;
1487 }
1488 s = e+1;
1489 }
1490
1491 kfree(macstr);
1492 return 0;
1493}
1494
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001495static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001496{
1497 int err;
1498
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001499 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001500 if (err < 0)
1501 goto fail;
1502
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001503 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001504 xennet_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001505 0, queue->info->netdev->name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001506 if (err < 0)
1507 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001508 queue->rx_evtchn = queue->tx_evtchn;
1509 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001510
1511 return 0;
1512
1513bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001514 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1515 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001516fail:
1517 return err;
1518}
1519
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001520static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001521{
1522 int err;
1523
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001524 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001525 if (err < 0)
1526 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001527 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001528 if (err < 0)
1529 goto alloc_rx_evtchn_fail;
1530
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001531 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1532 "%s-tx", queue->name);
1533 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001534 xennet_tx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001535 0, queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001536 if (err < 0)
1537 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001538 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001539
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001540 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1541 "%s-rx", queue->name);
1542 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001543 xennet_rx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001544 0, queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001545 if (err < 0)
1546 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001547 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001548
1549 return 0;
1550
1551bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001552 unbind_from_irqhandler(queue->tx_irq, queue);
1553 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001554bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001555 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1556 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001557alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001558 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1559 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001560fail:
1561 return err;
1562}
1563
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001564static int setup_netfront(struct xenbus_device *dev,
1565 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001566{
1567 struct xen_netif_tx_sring *txs;
1568 struct xen_netif_rx_sring *rxs;
1569 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001570
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001571 queue->tx_ring_ref = GRANT_INVALID_REF;
1572 queue->rx_ring_ref = GRANT_INVALID_REF;
1573 queue->rx.sring = NULL;
1574 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001575
Ian Campbella144ff02008-06-17 10:47:08 +02001576 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001577 if (!txs) {
1578 err = -ENOMEM;
1579 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1580 goto fail;
1581 }
1582 SHARED_RING_INIT(txs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001583 FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001584
1585 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
Wei Liu1ca29832013-05-20 01:05:12 +00001586 if (err < 0)
1587 goto grant_tx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001588 queue->tx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001589
Ian Campbella144ff02008-06-17 10:47:08 +02001590 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001591 if (!rxs) {
1592 err = -ENOMEM;
1593 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001594 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001595 }
1596 SHARED_RING_INIT(rxs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001597 FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001598
1599 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
Wei Liu1ca29832013-05-20 01:05:12 +00001600 if (err < 0)
1601 goto grant_rx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001602 queue->rx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001603
Wei Liud634bf22013-05-22 06:34:46 +00001604 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001605 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001606 /* setup single event channel if
1607 * a) feature-split-event-channels == 0
1608 * b) feature-split-event-channels == 1 but failed to setup
1609 */
1610 if (!feature_split_evtchn || (feature_split_evtchn && err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001611 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001612
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001613 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001614 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001615
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001616 return 0;
1617
Wei Liu1ca29832013-05-20 01:05:12 +00001618 /* If we fail to setup netfront, it is safe to just revoke access to
1619 * granted pages because backend is not accessing it at this point.
1620 */
Wei Liu1ca29832013-05-20 01:05:12 +00001621alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001622 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001623grant_rx_ring_fail:
1624 free_page((unsigned long)rxs);
1625alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001626 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001627grant_tx_ring_fail:
1628 free_page((unsigned long)txs);
1629fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001630 return err;
1631}
1632
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001633/* Queue-specific initialisation
1634 * This used to be done in xennet_create_dev() but must now
1635 * be run per-queue.
1636 */
1637static int xennet_init_queue(struct netfront_queue *queue)
1638{
1639 unsigned short i;
1640 int err = 0;
1641
1642 spin_lock_init(&queue->tx_lock);
1643 spin_lock_init(&queue->rx_lock);
1644
1645 skb_queue_head_init(&queue->rx_batch);
1646 queue->rx_target = RX_DFL_MIN_TARGET;
1647 queue->rx_min_target = RX_DFL_MIN_TARGET;
1648 queue->rx_max_target = RX_MAX_TARGET;
1649
1650 init_timer(&queue->rx_refill_timer);
1651 queue->rx_refill_timer.data = (unsigned long)queue;
1652 queue->rx_refill_timer.function = rx_refill_timeout;
1653
Wei Liu8b715012014-06-04 10:30:47 +01001654 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
1655 queue->info->netdev->name, queue->id);
1656
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001657 /* Initialise tx_skbs as a free chain containing every entry. */
1658 queue->tx_skb_freelist = 0;
1659 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1660 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1661 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1662 queue->grant_tx_page[i] = NULL;
1663 }
1664
1665 /* Clear out rx_skbs */
1666 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1667 queue->rx_skbs[i] = NULL;
1668 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1669 }
1670
1671 /* A grant for every tx ring slot */
1672 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1673 &queue->gref_tx_head) < 0) {
1674 pr_alert("can't alloc tx grant refs\n");
1675 err = -ENOMEM;
1676 goto exit;
1677 }
1678
1679 /* A grant for every rx ring slot */
1680 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1681 &queue->gref_rx_head) < 0) {
1682 pr_alert("can't alloc rx grant refs\n");
1683 err = -ENOMEM;
1684 goto exit_free_tx;
1685 }
1686
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001687 return 0;
1688
1689 exit_free_tx:
1690 gnttab_free_grant_references(queue->gref_tx_head);
1691 exit:
1692 return err;
1693}
1694
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001695static int write_queue_xenstore_keys(struct netfront_queue *queue,
1696 struct xenbus_transaction *xbt, int write_hierarchical)
1697{
1698 /* Write the queue-specific keys into XenStore in the traditional
1699 * way for a single queue, or in a queue subkeys for multiple
1700 * queues.
1701 */
1702 struct xenbus_device *dev = queue->info->xbdev;
1703 int err;
1704 const char *message;
1705 char *path;
1706 size_t pathsize;
1707
1708 /* Choose the correct place to write the keys */
1709 if (write_hierarchical) {
1710 pathsize = strlen(dev->nodename) + 10;
1711 path = kzalloc(pathsize, GFP_KERNEL);
1712 if (!path) {
1713 err = -ENOMEM;
1714 message = "out of memory while writing ring references";
1715 goto error;
1716 }
1717 snprintf(path, pathsize, "%s/queue-%u",
1718 dev->nodename, queue->id);
1719 } else {
1720 path = (char *)dev->nodename;
1721 }
1722
1723 /* Write ring references */
1724 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1725 queue->tx_ring_ref);
1726 if (err) {
1727 message = "writing tx-ring-ref";
1728 goto error;
1729 }
1730
1731 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1732 queue->rx_ring_ref);
1733 if (err) {
1734 message = "writing rx-ring-ref";
1735 goto error;
1736 }
1737
1738 /* Write event channels; taking into account both shared
1739 * and split event channel scenarios.
1740 */
1741 if (queue->tx_evtchn == queue->rx_evtchn) {
1742 /* Shared event channel */
1743 err = xenbus_printf(*xbt, path,
1744 "event-channel", "%u", queue->tx_evtchn);
1745 if (err) {
1746 message = "writing event-channel";
1747 goto error;
1748 }
1749 } else {
1750 /* Split event channels */
1751 err = xenbus_printf(*xbt, path,
1752 "event-channel-tx", "%u", queue->tx_evtchn);
1753 if (err) {
1754 message = "writing event-channel-tx";
1755 goto error;
1756 }
1757
1758 err = xenbus_printf(*xbt, path,
1759 "event-channel-rx", "%u", queue->rx_evtchn);
1760 if (err) {
1761 message = "writing event-channel-rx";
1762 goto error;
1763 }
1764 }
1765
1766 if (write_hierarchical)
1767 kfree(path);
1768 return 0;
1769
1770error:
1771 if (write_hierarchical)
1772 kfree(path);
1773 xenbus_dev_fatal(dev, err, "%s", message);
1774 return err;
1775}
1776
David Vrabelce587252014-06-18 10:47:28 +01001777static void xennet_destroy_queues(struct netfront_info *info)
1778{
1779 unsigned int i;
1780
1781 rtnl_lock();
1782
1783 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1784 struct netfront_queue *queue = &info->queues[i];
1785
1786 if (netif_running(info->netdev))
1787 napi_disable(&queue->napi);
1788 netif_napi_del(&queue->napi);
1789 }
1790
1791 rtnl_unlock();
1792
1793 kfree(info->queues);
1794 info->queues = NULL;
1795}
1796
1797static int xennet_create_queues(struct netfront_info *info,
1798 unsigned int num_queues)
1799{
1800 unsigned int i;
1801 int ret;
1802
1803 info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
1804 GFP_KERNEL);
1805 if (!info->queues)
1806 return -ENOMEM;
1807
1808 rtnl_lock();
1809
1810 for (i = 0; i < num_queues; i++) {
1811 struct netfront_queue *queue = &info->queues[i];
1812
1813 queue->id = i;
1814 queue->info = info;
1815
1816 ret = xennet_init_queue(queue);
1817 if (ret < 0) {
David Vrabel69cb8522014-07-31 17:38:24 +01001818 dev_warn(&info->netdev->dev,
1819 "only created %d queues\n", i);
David Vrabelce587252014-06-18 10:47:28 +01001820 num_queues = i;
1821 break;
1822 }
1823
1824 netif_napi_add(queue->info->netdev, &queue->napi,
1825 xennet_poll, 64);
1826 if (netif_running(info->netdev))
1827 napi_enable(&queue->napi);
1828 }
1829
1830 netif_set_real_num_tx_queues(info->netdev, num_queues);
1831
1832 rtnl_unlock();
1833
1834 if (num_queues == 0) {
1835 dev_err(&info->netdev->dev, "no queues\n");
1836 return -EINVAL;
1837 }
1838 return 0;
1839}
1840
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001841/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00001842static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001843 struct netfront_info *info)
1844{
1845 const char *message;
1846 struct xenbus_transaction xbt;
1847 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001848 unsigned int feature_split_evtchn;
1849 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001850 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001851 struct netfront_queue *queue = NULL;
1852 unsigned int num_queues = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001853
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001854 info->netdev->irq = 0;
1855
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001856 /* Check if backend supports multiple queues */
1857 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1858 "multi-queue-max-queues", "%u", &max_queues);
1859 if (err < 0)
1860 max_queues = 1;
1861 num_queues = min(max_queues, xennet_max_queues);
1862
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001863 /* Check feature-split-event-channels */
1864 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1865 "feature-split-event-channels", "%u",
1866 &feature_split_evtchn);
1867 if (err < 0)
1868 feature_split_evtchn = 0;
1869
1870 /* Read mac addr. */
1871 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1872 if (err) {
1873 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001874 goto out;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001875 }
1876
David Vrabelce587252014-06-18 10:47:28 +01001877 if (info->queues)
1878 xennet_destroy_queues(info);
1879
1880 err = xennet_create_queues(info, num_queues);
1881 if (err < 0)
1882 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001883
1884 /* Create shared ring, alloc event channel -- for each queue */
1885 for (i = 0; i < num_queues; ++i) {
1886 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001887 err = setup_netfront(dev, queue, feature_split_evtchn);
1888 if (err) {
David Vrabelce587252014-06-18 10:47:28 +01001889 /* setup_netfront() will tidy up the current
1890 * queue on error, but we need to clean up
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001891 * those already allocated.
1892 */
1893 if (i > 0) {
1894 rtnl_lock();
1895 netif_set_real_num_tx_queues(info->netdev, i);
1896 rtnl_unlock();
1897 goto destroy_ring;
1898 } else {
1899 goto out;
1900 }
1901 }
1902 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001903
1904again:
1905 err = xenbus_transaction_start(&xbt);
1906 if (err) {
1907 xenbus_dev_fatal(dev, err, "starting transaction");
1908 goto destroy_ring;
1909 }
1910
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001911 if (num_queues == 1) {
1912 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1913 if (err)
1914 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001915 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001916 /* Write the number of queues */
1917 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
1918 "%u", num_queues);
Wei Liud634bf22013-05-22 06:34:46 +00001919 if (err) {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001920 message = "writing multi-queue-num-queues";
1921 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001922 }
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001923
1924 /* Write the keys for each queue */
1925 for (i = 0; i < num_queues; ++i) {
1926 queue = &info->queues[i];
1927 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1928 if (err)
1929 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001930 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001931 }
1932
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001933 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001934 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1935 1);
1936 if (err) {
1937 message = "writing request-rx-copy";
1938 goto abort_transaction;
1939 }
1940
1941 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1942 if (err) {
1943 message = "writing feature-rx-notify";
1944 goto abort_transaction;
1945 }
1946
1947 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1948 if (err) {
1949 message = "writing feature-sg";
1950 goto abort_transaction;
1951 }
1952
1953 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1954 if (err) {
1955 message = "writing feature-gso-tcpv4";
1956 goto abort_transaction;
1957 }
1958
Paul Durrant2c0057d2014-01-15 17:30:33 +00001959 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1960 if (err) {
1961 message = "writing feature-gso-tcpv6";
1962 goto abort_transaction;
1963 }
1964
1965 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1966 "1");
1967 if (err) {
1968 message = "writing feature-ipv6-csum-offload";
1969 goto abort_transaction;
1970 }
1971
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001972 err = xenbus_transaction_end(xbt, 0);
1973 if (err) {
1974 if (err == -EAGAIN)
1975 goto again;
1976 xenbus_dev_fatal(dev, err, "completing transaction");
1977 goto destroy_ring;
1978 }
1979
1980 return 0;
1981
1982 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001983 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001984abort_transaction_no_dev_fatal:
1985 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001986 destroy_ring:
1987 xennet_disconnect_backend(info);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001988 kfree(info->queues);
1989 info->queues = NULL;
1990 rtnl_lock();
1991 netif_set_real_num_tx_queues(info->netdev, 0);
David Vrabeldb8c8ab2014-07-31 17:38:22 +01001992 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001993 out:
1994 return err;
1995}
1996
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001997static int xennet_connect(struct net_device *dev)
1998{
1999 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002000 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01002001 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002002 unsigned int feature_rx_copy;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002003 unsigned int j = 0;
2004 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002005
2006 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
2007 "feature-rx-copy", "%u", &feature_rx_copy);
2008 if (err != 1)
2009 feature_rx_copy = 0;
2010
2011 if (!feature_rx_copy) {
2012 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07002013 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002014 return -ENODEV;
2015 }
2016
Ian Campbellf502bf22010-08-18 23:27:49 +00002017 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002018 if (err)
2019 return err;
2020
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002021 /* talk_to_netback() sets the correct number of queues */
2022 num_queues = dev->real_num_tx_queues;
2023
Ian Campbell1ba37c52011-05-24 21:56:02 +00002024 rtnl_lock();
Michał Mirosławfb507932011-03-31 01:01:35 +00002025 netdev_update_features(dev);
Ian Campbell1ba37c52011-05-24 21:56:02 +00002026 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002027
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002028 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01002029 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002030 * ready to start sending and receiving packets and give the driver
2031 * domain a kick because we've probably just requeued some
2032 * packets.
2033 */
2034 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002035 for (j = 0; j < num_queues; ++j) {
2036 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01002037
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002038 notify_remote_via_irq(queue->tx_irq);
2039 if (queue->tx_irq != queue->rx_irq)
2040 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002041
David Vrabelf50b4072014-07-02 16:09:14 +01002042 spin_lock_irq(&queue->tx_lock);
2043 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002044 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01002045
2046 spin_lock_bh(&queue->rx_lock);
2047 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002048 spin_unlock_bh(&queue->rx_lock);
2049 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002050
2051 return 0;
2052}
2053
2054/**
2055 * Callback received when the backend's state changes.
2056 */
Ian Campbellf502bf22010-08-18 23:27:49 +00002057static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002058 enum xenbus_state backend_state)
2059{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002060 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002061 struct net_device *netdev = np->netdev;
2062
2063 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2064
2065 switch (backend_state) {
2066 case XenbusStateInitialising:
2067 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002068 case XenbusStateReconfiguring:
2069 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002070 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002071 break;
2072
2073 case XenbusStateInitWait:
2074 if (dev->state != XenbusStateInitialising)
2075 break;
2076 if (xennet_connect(netdev) != 0)
2077 break;
2078 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00002079 break;
2080
2081 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00002082 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002083 break;
2084
David Vrabelbce3ea82014-02-04 18:50:26 +00002085 case XenbusStateClosed:
2086 if (dev->state == XenbusStateClosed)
2087 break;
2088 /* Missed the backend's CLOSING state -- fallthrough */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002089 case XenbusStateClosing:
2090 xenbus_frontend_closed(dev);
2091 break;
2092 }
2093}
2094
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002095static const struct xennet_stat {
2096 char name[ETH_GSTRING_LEN];
2097 u16 offset;
2098} xennet_stats[] = {
2099 {
2100 "rx_gso_checksum_fixup",
2101 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2102 },
2103};
2104
2105static int xennet_get_sset_count(struct net_device *dev, int string_set)
2106{
2107 switch (string_set) {
2108 case ETH_SS_STATS:
2109 return ARRAY_SIZE(xennet_stats);
2110 default:
2111 return -EINVAL;
2112 }
2113}
2114
2115static void xennet_get_ethtool_stats(struct net_device *dev,
2116 struct ethtool_stats *stats, u64 * data)
2117{
2118 void *np = netdev_priv(dev);
2119 int i;
2120
2121 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002122 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002123}
2124
2125static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2126{
2127 int i;
2128
2129 switch (stringset) {
2130 case ETH_SS_STATS:
2131 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2132 memcpy(data + i * ETH_GSTRING_LEN,
2133 xennet_stats[i].name, ETH_GSTRING_LEN);
2134 break;
2135 }
2136}
2137
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002138static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002139{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002140 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002141
2142 .get_sset_count = xennet_get_sset_count,
2143 .get_ethtool_stats = xennet_get_ethtool_stats,
2144 .get_strings = xennet_get_strings,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002145};
2146
2147#ifdef CONFIG_SYSFS
2148static ssize_t show_rxbuf_min(struct device *dev,
2149 struct device_attribute *attr, char *buf)
2150{
2151 struct net_device *netdev = to_net_dev(dev);
2152 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002153 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002154
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002155 if (num_queues)
2156 return sprintf(buf, "%u\n", info->queues[0].rx_min_target);
2157 else
2158 return sprintf(buf, "%u\n", RX_MIN_TARGET);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002159}
2160
2161static ssize_t store_rxbuf_min(struct device *dev,
2162 struct device_attribute *attr,
2163 const char *buf, size_t len)
2164{
2165 struct net_device *netdev = to_net_dev(dev);
2166 struct netfront_info *np = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002167 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002168 char *endp;
2169 unsigned long target;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002170 unsigned int i;
2171 struct netfront_queue *queue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002172
2173 if (!capable(CAP_NET_ADMIN))
2174 return -EPERM;
2175
2176 target = simple_strtoul(buf, &endp, 0);
2177 if (endp == buf)
2178 return -EBADMSG;
2179
2180 if (target < RX_MIN_TARGET)
2181 target = RX_MIN_TARGET;
2182 if (target > RX_MAX_TARGET)
2183 target = RX_MAX_TARGET;
2184
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002185 for (i = 0; i < num_queues; ++i) {
2186 queue = &np->queues[i];
2187 spin_lock_bh(&queue->rx_lock);
2188 if (target > queue->rx_max_target)
2189 queue->rx_max_target = target;
2190 queue->rx_min_target = target;
2191 if (target > queue->rx_target)
2192 queue->rx_target = target;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002193
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002194 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002195
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002196 spin_unlock_bh(&queue->rx_lock);
2197 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002198 return len;
2199}
2200
2201static ssize_t show_rxbuf_max(struct device *dev,
2202 struct device_attribute *attr, char *buf)
2203{
2204 struct net_device *netdev = to_net_dev(dev);
2205 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002206 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002207
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002208 if (num_queues)
2209 return sprintf(buf, "%u\n", info->queues[0].rx_max_target);
2210 else
2211 return sprintf(buf, "%u\n", RX_MAX_TARGET);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002212}
2213
2214static ssize_t store_rxbuf_max(struct device *dev,
2215 struct device_attribute *attr,
2216 const char *buf, size_t len)
2217{
2218 struct net_device *netdev = to_net_dev(dev);
2219 struct netfront_info *np = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002220 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002221 char *endp;
2222 unsigned long target;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002223 unsigned int i = 0;
2224 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002225
2226 if (!capable(CAP_NET_ADMIN))
2227 return -EPERM;
2228
2229 target = simple_strtoul(buf, &endp, 0);
2230 if (endp == buf)
2231 return -EBADMSG;
2232
2233 if (target < RX_MIN_TARGET)
2234 target = RX_MIN_TARGET;
2235 if (target > RX_MAX_TARGET)
2236 target = RX_MAX_TARGET;
2237
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002238 for (i = 0; i < num_queues; ++i) {
2239 queue = &np->queues[i];
2240 spin_lock_bh(&queue->rx_lock);
2241 if (target < queue->rx_min_target)
2242 queue->rx_min_target = target;
2243 queue->rx_max_target = target;
2244 if (target < queue->rx_target)
2245 queue->rx_target = target;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002246
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002247 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002248
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002249 spin_unlock_bh(&queue->rx_lock);
2250 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002251 return len;
2252}
2253
2254static ssize_t show_rxbuf_cur(struct device *dev,
2255 struct device_attribute *attr, char *buf)
2256{
2257 struct net_device *netdev = to_net_dev(dev);
2258 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002259 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002260
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002261 if (num_queues)
2262 return sprintf(buf, "%u\n", info->queues[0].rx_target);
2263 else
2264 return sprintf(buf, "0\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002265}
2266
2267static struct device_attribute xennet_attrs[] = {
2268 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
2269 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
2270 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
2271};
2272
2273static int xennet_sysfs_addif(struct net_device *netdev)
2274{
2275 int i;
2276 int err;
2277
2278 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
2279 err = device_create_file(&netdev->dev,
2280 &xennet_attrs[i]);
2281 if (err)
2282 goto fail;
2283 }
2284 return 0;
2285
2286 fail:
2287 while (--i >= 0)
2288 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2289 return err;
2290}
2291
2292static void xennet_sysfs_delif(struct net_device *netdev)
2293{
2294 int i;
2295
2296 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
2297 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2298}
2299
2300#endif /* CONFIG_SYSFS */
2301
Jan Beulich73db1442011-12-22 09:08:13 +00002302static const struct xenbus_device_id netfront_ids[] = {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002303 { "vif" },
2304 { "" }
2305};
2306
2307
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002308static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002309{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002310 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002311 unsigned int num_queues = info->netdev->real_num_tx_queues;
2312 struct netfront_queue *queue = NULL;
2313 unsigned int i = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002314
2315 dev_dbg(&dev->dev, "%s\n", dev->nodename);
2316
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002317 xennet_disconnect_backend(info);
2318
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002319 xennet_sysfs_delif(info->netdev);
2320
Ian Campbell6bc96d02012-06-25 22:48:41 +00002321 unregister_netdev(info->netdev);
2322
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002323 for (i = 0; i < num_queues; ++i) {
2324 queue = &info->queues[i];
2325 del_timer_sync(&queue->rx_refill_timer);
2326 }
2327
2328 if (num_queues) {
2329 kfree(info->queues);
2330 info->queues = NULL;
2331 }
Ian Campbell6bc96d02012-06-25 22:48:41 +00002332
stephen hemmingere00f85b2011-06-21 05:35:31 +00002333 free_percpu(info->stats);
2334
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002335 free_netdev(info->netdev);
2336
2337 return 0;
2338}
2339
Jan Beulich73db1442011-12-22 09:08:13 +00002340static DEFINE_XENBUS_DRIVER(netfront, ,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002341 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002342 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002343 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002344 .otherend_changed = netback_changed,
Jan Beulich73db1442011-12-22 09:08:13 +00002345);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002346
2347static int __init netif_init(void)
2348{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002349 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002350 return -ENODEV;
2351
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002352 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002353 return -ENODEV;
2354
Joe Perches383eda32013-06-27 21:57:49 -07002355 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002356
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002357 /* Allow as many queues as there are CPUs, by default */
2358 xennet_max_queues = num_online_cpus();
2359
Al Viroffb78a22008-11-22 17:38:14 +00002360 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002361}
2362module_init(netif_init);
2363
2364
2365static void __exit netif_exit(void)
2366{
Al Viroffb78a22008-11-22 17:38:14 +00002367 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002368}
2369module_exit(netif_exit);
2370
2371MODULE_DESCRIPTION("Xen virtual network device frontend");
2372MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002373MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002374MODULE_ALIAS("xennet");