blob: ca82f545ec2ca12678ca4f4ce6669c0b564f8bb2 [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)) {
Zoltan Kiss97a6d1b2014-08-11 18:32:23 +0100631 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
632 slots, skb->len);
633 if (skb_linearize(skb))
634 goto drop;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700635 }
636
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100637 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700638
639 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000640 (slots > 1 && !xennet_can_sg(dev)) ||
Jesse Grossfc741212011-01-09 06:23:32 +0000641 netif_needs_gso(skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100642 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700643 goto drop;
644 }
645
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100646 i = queue->tx.req_prod_pvt;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700647
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100648 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
649 queue->tx_skbs[id].skb = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700650
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100651 tx = RING_GET_REQUEST(&queue->tx, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700652
653 tx->id = id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100654 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700655 BUG_ON((signed short)ref < 0);
656 mfn = virt_to_mfn(data);
657 gnttab_grant_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100658 ref, queue->info->xbdev->otherend_id, mfn, GNTMAP_readonly);
659 queue->grant_tx_page[id] = virt_to_page(data);
660 tx->gref = queue->grant_tx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700661 tx->offset = offset;
662 tx->size = len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700663
664 tx->flags = 0;
665 if (skb->ip_summed == CHECKSUM_PARTIAL)
666 /* local packet? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000667 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700668 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
669 /* remote but checksummed. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000670 tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700671
672 if (skb_shinfo(skb)->gso_size) {
673 struct xen_netif_extra_info *gso;
674
675 gso = (struct xen_netif_extra_info *)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100676 RING_GET_REQUEST(&queue->tx, ++i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700677
Wei Liue2d617c2013-03-25 01:08:17 +0000678 tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700679
680 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000681 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
682 XEN_NETIF_GSO_TYPE_TCPV6 :
683 XEN_NETIF_GSO_TYPE_TCPV4;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700684 gso->u.gso.pad = 0;
685 gso->u.gso.features = 0;
686
687 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
688 gso->flags = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700689 }
690
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100691 queue->tx.req_prod_pvt = i + 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700692
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100693 xennet_make_frags(skb, queue, tx);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700694 tx->size = skb->len;
695
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100696 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700697 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100698 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700699
stephen hemmingere00f85b2011-06-21 05:35:31 +0000700 u64_stats_update_begin(&stats->syncp);
701 stats->tx_bytes += skb->len;
702 stats->tx_packets++;
703 u64_stats_update_end(&stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700704
705 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100706 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700707
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100708 if (!netfront_tx_slot_available(queue))
709 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700710
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100711 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700712
Patrick McHardy6ed10652009-06-23 06:03:08 +0000713 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700714
715 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700716 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700717 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000718 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700719}
720
721static int xennet_close(struct net_device *dev)
722{
723 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100724 unsigned int num_queues = dev->real_num_tx_queues;
725 unsigned int i;
726 struct netfront_queue *queue;
727 netif_tx_stop_all_queues(np->netdev);
728 for (i = 0; i < num_queues; ++i) {
729 queue = &np->queues[i];
730 napi_disable(&queue->napi);
731 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700732 return 0;
733}
734
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100735static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700736 grant_ref_t ref)
737{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100738 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700739
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100740 BUG_ON(queue->rx_skbs[new]);
741 queue->rx_skbs[new] = skb;
742 queue->grant_rx_ref[new] = ref;
743 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
744 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
745 queue->rx.req_prod_pvt++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700746}
747
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100748static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700749 struct xen_netif_extra_info *extras,
750 RING_IDX rp)
751
752{
753 struct xen_netif_extra_info *extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100754 struct device *dev = &queue->info->netdev->dev;
755 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700756 int err = 0;
757
758 do {
759 struct sk_buff *skb;
760 grant_ref_t ref;
761
762 if (unlikely(cons + 1 == rp)) {
763 if (net_ratelimit())
764 dev_warn(dev, "Missing extra info\n");
765 err = -EBADR;
766 break;
767 }
768
769 extra = (struct xen_netif_extra_info *)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100770 RING_GET_RESPONSE(&queue->rx, ++cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700771
772 if (unlikely(!extra->type ||
773 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
774 if (net_ratelimit())
775 dev_warn(dev, "Invalid extra type: %d\n",
776 extra->type);
777 err = -EINVAL;
778 } else {
779 memcpy(&extras[extra->type - 1], extra,
780 sizeof(*extra));
781 }
782
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100783 skb = xennet_get_rx_skb(queue, cons);
784 ref = xennet_get_rx_ref(queue, cons);
785 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700786 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
787
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100788 queue->rx.rsp_cons = cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700789 return err;
790}
791
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100792static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700793 struct netfront_rx_info *rinfo, RING_IDX rp,
794 struct sk_buff_head *list)
795{
796 struct xen_netif_rx_response *rx = &rinfo->rx;
797 struct xen_netif_extra_info *extras = rinfo->extras;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100798 struct device *dev = &queue->info->netdev->dev;
799 RING_IDX cons = queue->rx.rsp_cons;
800 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
801 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700802 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
Wei Liu7158ff62013-03-25 01:08:19 +0000803 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700804 int err = 0;
805 unsigned long ret;
806
Ian Campbellf942dc22011-03-15 00:06:18 +0000807 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100808 err = xennet_get_extras(queue, extras, rp);
809 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700810 }
811
812 for (;;) {
813 if (unlikely(rx->status < 0 ||
814 rx->offset + rx->status > PAGE_SIZE)) {
815 if (net_ratelimit())
816 dev_warn(dev, "rx->offset: %x, size: %u\n",
817 rx->offset, rx->status);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100818 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700819 err = -EINVAL;
820 goto next;
821 }
822
823 /*
824 * This definitely indicates a bug, either in this driver or in
825 * the backend driver. In future this should flag the bad
Wei Liu697089d2013-04-22 02:20:40 +0000826 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700827 */
828 if (ref == GRANT_INVALID_REF) {
829 if (net_ratelimit())
830 dev_warn(dev, "Bad rx response id %d.\n",
831 rx->id);
832 err = -EINVAL;
833 goto next;
834 }
835
836 ret = gnttab_end_foreign_access_ref(ref, 0);
837 BUG_ON(!ret);
838
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100839 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700840
841 __skb_queue_tail(list, skb);
842
843next:
Ian Campbellf942dc22011-03-15 00:06:18 +0000844 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700845 break;
846
Wei Liu7158ff62013-03-25 01:08:19 +0000847 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700848 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +0000849 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700850 err = -ENOENT;
851 break;
852 }
853
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100854 rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
855 skb = xennet_get_rx_skb(queue, cons + slots);
856 ref = xennet_get_rx_ref(queue, cons + slots);
Wei Liu7158ff62013-03-25 01:08:19 +0000857 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700858 }
859
Wei Liu7158ff62013-03-25 01:08:19 +0000860 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700861 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +0000862 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700863 err = -E2BIG;
864 }
865
866 if (unlikely(err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100867 queue->rx.rsp_cons = cons + slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700868
869 return err;
870}
871
872static int xennet_set_skb_gso(struct sk_buff *skb,
873 struct xen_netif_extra_info *gso)
874{
875 if (!gso->u.gso.size) {
876 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700877 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700878 return -EINVAL;
879 }
880
Paul Durrant2c0057d2014-01-15 17:30:33 +0000881 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
882 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700883 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700884 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700885 return -EINVAL;
886 }
887
888 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000889 skb_shinfo(skb)->gso_type =
890 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
891 SKB_GSO_TCPV4 :
892 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700893
894 /* Header must be checked, and gso_segs computed. */
895 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
896 skb_shinfo(skb)->gso_segs = 0;
897
898 return 0;
899}
900
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100901static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700902 struct sk_buff *skb,
903 struct sk_buff_head *list)
904{
905 struct skb_shared_info *shinfo = skb_shinfo(skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100906 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700907 struct sk_buff *nskb;
908
909 while ((nskb = __skb_dequeue(list))) {
910 struct xen_netif_rx_response *rx =
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100911 RING_GET_RESPONSE(&queue->rx, ++cons);
Ian Campbell01c68022011-10-05 00:28:47 +0000912 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700913
Jan Beulich093b9c72013-07-17 08:09:37 +0100914 if (shinfo->nr_frags == MAX_SKB_FRAGS) {
915 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700916
Jan Beulich093b9c72013-07-17 08:09:37 +0100917 BUG_ON(pull_to <= skb_headlen(skb));
918 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
919 }
920 BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
921
922 skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
923 rx->offset, rx->status, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700924
925 skb_shinfo(nskb)->nr_frags = 0;
926 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700927 }
928
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700929 return cons;
930}
931
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000932static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700933{
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000934 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000935
936 /*
937 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
938 * peers can fail to set NETRXF_csum_blank when sending a GSO
939 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
940 * recalculate the partial checksum.
941 */
942 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
943 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100944 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000945 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000946 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000947 }
948
949 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
950 if (skb->ip_summed != CHECKSUM_PARTIAL)
951 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700952
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000953 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700954}
955
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100956static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700957 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700958{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100959 struct netfront_stats *stats = this_cpu_ptr(queue->info->stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700960 int packets_dropped = 0;
961 struct sk_buff *skb;
962
963 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +0000964 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700965
Jan Beulich093b9c72013-07-17 08:09:37 +0100966 if (pull_to > skb_headlen(skb))
967 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700968
969 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100970 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +0000971 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700972
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100973 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000974 kfree_skb(skb);
975 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100976 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000977 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700978 }
979
stephen hemmingere00f85b2011-06-21 05:35:31 +0000980 u64_stats_update_begin(&stats->syncp);
981 stats->rx_packets++;
982 stats->rx_bytes += skb->len;
983 u64_stats_update_end(&stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700984
985 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100986 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700987 }
988
989 return packets_dropped;
990}
991
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700992static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700993{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100994 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
995 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700996 struct sk_buff *skb;
997 struct netfront_rx_info rinfo;
998 struct xen_netif_rx_response *rx = &rinfo.rx;
999 struct xen_netif_extra_info *extras = rinfo.extras;
1000 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001001 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001002 struct sk_buff_head rxq;
1003 struct sk_buff_head errq;
1004 struct sk_buff_head tmpq;
1005 unsigned long flags;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001006 int err;
1007
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001008 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001009
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001010 skb_queue_head_init(&rxq);
1011 skb_queue_head_init(&errq);
1012 skb_queue_head_init(&tmpq);
1013
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001014 rp = queue->rx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001015 rmb(); /* Ensure we see queued responses up to 'rp'. */
1016
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001017 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001018 work_done = 0;
1019 while ((i != rp) && (work_done < budget)) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001020 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001021 memset(extras, 0, sizeof(rinfo.extras));
1022
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001023 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001024
1025 if (unlikely(err)) {
1026err:
1027 while ((skb = __skb_dequeue(&tmpq)))
1028 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001029 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001030 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001031 continue;
1032 }
1033
1034 skb = __skb_dequeue(&tmpq);
1035
1036 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1037 struct xen_netif_extra_info *gso;
1038 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1039
1040 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1041 __skb_queue_head(&tmpq, skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001042 queue->rx.rsp_cons += skb_queue_len(&tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001043 goto err;
1044 }
1045 }
1046
Ian Campbell36832432012-08-22 00:26:47 +00001047 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1048 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1049 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001050
Ian Campbell36832432012-08-22 00:26:47 +00001051 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
1052 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1053 skb->data_len = rx->status;
Jan Beulich093b9c72013-07-17 08:09:37 +01001054 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001055
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001056 i = xennet_fill_frags(queue, skb, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001057
Ian Campbellf942dc22011-03-15 00:06:18 +00001058 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001059 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001060 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001061 skb->ip_summed = CHECKSUM_UNNECESSARY;
1062
1063 __skb_queue_tail(&rxq, skb);
1064
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001065 queue->rx.rsp_cons = ++i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001066 work_done++;
1067 }
1068
Wang Chen56cfe5d2008-05-22 18:09:06 +08001069 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001070
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001071 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001072
1073 /* If we get a callback with very few responses, reduce fill target. */
1074 /* NB. Note exponential increase, linear decrease. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001075 if (((queue->rx.req_prod_pvt - queue->rx.sring->rsp_prod) >
1076 ((3*queue->rx_target) / 4)) &&
1077 (--queue->rx_target < queue->rx_min_target))
1078 queue->rx_target = queue->rx_min_target;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001079
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001080 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001081
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001082 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001083 int more_to_do = 0;
1084
Wei Liu99d3d582013-09-30 13:46:34 +01001085 napi_gro_flush(napi, false);
1086
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001087 local_irq_save(flags);
1088
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001089 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001090 if (!more_to_do)
Ben Hutchings288379f2009-01-19 16:43:59 -08001091 __napi_complete(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001092
1093 local_irq_restore(flags);
1094 }
1095
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001096 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001097
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001098 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001099}
1100
1101static int xennet_change_mtu(struct net_device *dev, int mtu)
1102{
Wei Liu9ecd1a72013-04-22 02:20:41 +00001103 int max = xennet_can_sg(dev) ?
1104 XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001105
1106 if (mtu > max)
1107 return -EINVAL;
1108 dev->mtu = mtu;
1109 return 0;
1110}
1111
stephen hemmingere00f85b2011-06-21 05:35:31 +00001112static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1113 struct rtnl_link_stats64 *tot)
1114{
1115 struct netfront_info *np = netdev_priv(dev);
1116 int cpu;
1117
1118 for_each_possible_cpu(cpu) {
1119 struct netfront_stats *stats = per_cpu_ptr(np->stats, cpu);
1120 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1121 unsigned int start;
1122
1123 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001124 start = u64_stats_fetch_begin_irq(&stats->syncp);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001125
1126 rx_packets = stats->rx_packets;
1127 tx_packets = stats->tx_packets;
1128 rx_bytes = stats->rx_bytes;
1129 tx_bytes = stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001130 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001131
1132 tot->rx_packets += rx_packets;
1133 tot->tx_packets += tx_packets;
1134 tot->rx_bytes += rx_bytes;
1135 tot->tx_bytes += tx_bytes;
1136 }
1137
1138 tot->rx_errors = dev->stats.rx_errors;
1139 tot->tx_dropped = dev->stats.tx_dropped;
1140
1141 return tot;
1142}
1143
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001144static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001145{
1146 struct sk_buff *skb;
1147 int i;
1148
1149 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1150 /* Skip over entries which are actually freelist references */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001151 if (skb_entry_is_link(&queue->tx_skbs[i]))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001152 continue;
1153
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001154 skb = queue->tx_skbs[i].skb;
1155 get_page(queue->grant_tx_page[i]);
1156 gnttab_end_foreign_access(queue->grant_tx_ref[i],
Annie Licefe0072014-01-28 11:35:42 +08001157 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001158 (unsigned long)page_address(queue->grant_tx_page[i]));
1159 queue->grant_tx_page[i] = NULL;
1160 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1161 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001162 dev_kfree_skb_irq(skb);
1163 }
1164}
1165
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001166static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001167{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001168 int id, ref;
1169
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001170 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001171
1172 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001173 struct sk_buff *skb;
1174 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001175
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001176 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001177 if (!skb)
1178 continue;
1179
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001180 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001181 if (ref == GRANT_INVALID_REF)
1182 continue;
1183
1184 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1185
1186 /* gnttab_end_foreign_access() needs a page ref until
1187 * foreign access is ended (which may be deferred).
1188 */
1189 get_page(page);
1190 gnttab_end_foreign_access(ref, 0,
1191 (unsigned long)page_address(page));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001192 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001193
Annie Licefe0072014-01-28 11:35:42 +08001194 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001195 }
1196
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001197 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001198}
1199
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001200static netdev_features_t xennet_fix_features(struct net_device *dev,
1201 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001202{
1203 struct netfront_info *np = netdev_priv(dev);
1204 int val;
1205
1206 if (features & NETIF_F_SG) {
1207 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1208 "%d", &val) < 0)
1209 val = 0;
1210
1211 if (!val)
1212 features &= ~NETIF_F_SG;
1213 }
1214
Paul Durrant2c0057d2014-01-15 17:30:33 +00001215 if (features & NETIF_F_IPV6_CSUM) {
1216 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1217 "feature-ipv6-csum-offload", "%d", &val) < 0)
1218 val = 0;
1219
1220 if (!val)
1221 features &= ~NETIF_F_IPV6_CSUM;
1222 }
1223
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001224 if (features & NETIF_F_TSO) {
1225 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1226 "feature-gso-tcpv4", "%d", &val) < 0)
1227 val = 0;
1228
1229 if (!val)
1230 features &= ~NETIF_F_TSO;
1231 }
1232
Paul Durrant2c0057d2014-01-15 17:30:33 +00001233 if (features & NETIF_F_TSO6) {
1234 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1235 "feature-gso-tcpv6", "%d", &val) < 0)
1236 val = 0;
1237
1238 if (!val)
1239 features &= ~NETIF_F_TSO6;
1240 }
1241
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001242 return features;
1243}
1244
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001245static int xennet_set_features(struct net_device *dev,
1246 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001247{
1248 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1249 netdev_info(dev, "Reducing MTU because no SG offload");
1250 dev->mtu = ETH_DATA_LEN;
1251 }
1252
1253 return 0;
1254}
1255
Wei Liud634bf22013-05-22 06:34:46 +00001256static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001257{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001258 struct netfront_queue *queue = dev_id;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001259 unsigned long flags;
1260
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001261 spin_lock_irqsave(&queue->tx_lock, flags);
1262 xennet_tx_buf_gc(queue);
1263 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001264
1265 return IRQ_HANDLED;
1266}
1267
Wei Liud634bf22013-05-22 06:34:46 +00001268static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1269{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001270 struct netfront_queue *queue = dev_id;
1271 struct net_device *dev = queue->info->netdev;
Wei Liud634bf22013-05-22 06:34:46 +00001272
1273 if (likely(netif_carrier_ok(dev) &&
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001274 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
David Vrabel76541862014-06-18 10:47:27 +01001275 napi_schedule(&queue->napi);
Wei Liud634bf22013-05-22 06:34:46 +00001276
1277 return IRQ_HANDLED;
1278}
1279
1280static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1281{
1282 xennet_tx_interrupt(irq, dev_id);
1283 xennet_rx_interrupt(irq, dev_id);
1284 return IRQ_HANDLED;
1285}
1286
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001287#ifdef CONFIG_NET_POLL_CONTROLLER
1288static void xennet_poll_controller(struct net_device *dev)
1289{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001290 /* Poll each queue */
1291 struct netfront_info *info = netdev_priv(dev);
1292 unsigned int num_queues = dev->real_num_tx_queues;
1293 unsigned int i;
1294 for (i = 0; i < num_queues; ++i)
1295 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001296}
1297#endif
1298
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001299static const struct net_device_ops xennet_netdev_ops = {
1300 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001301 .ndo_stop = xennet_close,
1302 .ndo_start_xmit = xennet_start_xmit,
1303 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001304 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001305 .ndo_set_mac_address = eth_mac_addr,
1306 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001307 .ndo_fix_features = xennet_fix_features,
1308 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001309 .ndo_select_queue = xennet_select_queue,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001310#ifdef CONFIG_NET_POLL_CONTROLLER
1311 .ndo_poll_controller = xennet_poll_controller,
1312#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001313};
1314
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001315static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001316{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001317 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001318 struct net_device *netdev;
1319 struct netfront_info *np;
1320
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001321 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001322 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001323 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001324
1325 np = netdev_priv(netdev);
1326 np->xbdev = dev;
1327
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001328 /* No need to use rtnl_lock() before the call below as it
1329 * happens before register_netdev().
1330 */
1331 netif_set_real_num_tx_queues(netdev, 0);
1332 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001333
stephen hemmingere00f85b2011-06-21 05:35:31 +00001334 err = -ENOMEM;
WANG Cong1c213bd2014-02-13 11:46:28 -08001335 np->stats = netdev_alloc_pcpu_stats(struct netfront_stats);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001336 if (np->stats == NULL)
1337 goto exit;
1338
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001339 netdev->netdev_ops = &xennet_netdev_ops;
1340
Michał Mirosławfb507932011-03-31 01:01:35 +00001341 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1342 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001343 netdev->hw_features = NETIF_F_SG |
1344 NETIF_F_IPV6_CSUM |
1345 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001346
Ian Campbellfc3e5942011-04-04 11:07:57 -07001347 /*
1348 * Assume that all hw features are available for now. This set
1349 * will be adjusted by the call to netdev_update_features() in
1350 * xennet_connect() which is the earliest point where we can
1351 * negotiate with the backend regarding supported features.
1352 */
1353 netdev->features |= netdev->hw_features;
1354
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001355 netdev->ethtool_ops = &xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001356 SET_NETDEV_DEV(netdev, &dev->dev);
1357
Wei Liu9ecd1a72013-04-22 02:20:41 +00001358 netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
1359
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001360 np->netdev = netdev;
1361
1362 netif_carrier_off(netdev);
1363
1364 return netdev;
1365
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001366 exit:
1367 free_netdev(netdev);
1368 return ERR_PTR(err);
1369}
1370
1371/**
1372 * Entry point to this code when a new device is created. Allocate the basic
1373 * structures and the ring buffers for communication with the backend, and
1374 * inform the backend of the appropriate details for those.
1375 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001376static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001377 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001378{
1379 int err;
1380 struct net_device *netdev;
1381 struct netfront_info *info;
1382
1383 netdev = xennet_create_dev(dev);
1384 if (IS_ERR(netdev)) {
1385 err = PTR_ERR(netdev);
1386 xenbus_dev_fatal(dev, err, "creating netdev");
1387 return err;
1388 }
1389
1390 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001391 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001392
1393 err = register_netdev(info->netdev);
1394 if (err) {
Joe Perches383eda32013-06-27 21:57:49 -07001395 pr_warn("%s: register_netdev err=%d\n", __func__, err);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001396 goto fail;
1397 }
1398
1399 err = xennet_sysfs_addif(info->netdev);
1400 if (err) {
1401 unregister_netdev(info->netdev);
Joe Perches383eda32013-06-27 21:57:49 -07001402 pr_warn("%s: add sysfs failed err=%d\n", __func__, err);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001403 goto fail;
1404 }
1405
1406 return 0;
1407
1408 fail:
1409 free_netdev(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001410 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001411 return err;
1412}
1413
1414static void xennet_end_access(int ref, void *page)
1415{
1416 /* This frees the page as a side-effect */
1417 if (ref != GRANT_INVALID_REF)
1418 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1419}
1420
1421static void xennet_disconnect_backend(struct netfront_info *info)
1422{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001423 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001424 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001425
David Vrabelf9feb1e2014-07-02 16:09:15 +01001426 netif_carrier_off(info->netdev);
1427
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001428 for (i = 0; i < num_queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001429 struct netfront_queue *queue = &info->queues[i];
1430
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001431 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1432 unbind_from_irqhandler(queue->tx_irq, queue);
1433 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1434 unbind_from_irqhandler(queue->tx_irq, queue);
1435 unbind_from_irqhandler(queue->rx_irq, queue);
1436 }
1437 queue->tx_evtchn = queue->rx_evtchn = 0;
1438 queue->tx_irq = queue->rx_irq = 0;
1439
David Vrabelf9feb1e2014-07-02 16:09:15 +01001440 napi_synchronize(&queue->napi);
1441
David Vrabela5b5dc32014-07-31 17:38:23 +01001442 xennet_release_tx_bufs(queue);
1443 xennet_release_rx_bufs(queue);
1444 gnttab_free_grant_references(queue->gref_tx_head);
1445 gnttab_free_grant_references(queue->gref_rx_head);
1446
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001447 /* End access and free the pages */
1448 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1449 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1450
1451 queue->tx_ring_ref = GRANT_INVALID_REF;
1452 queue->rx_ring_ref = GRANT_INVALID_REF;
1453 queue->tx.sring = NULL;
1454 queue->rx.sring = NULL;
Wei Liud634bf22013-05-22 06:34:46 +00001455 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001456}
1457
1458/**
1459 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1460 * driver restart. We tear down our netif structure and recreate it, but
1461 * leave the device-layer structures intact so that this is transparent to the
1462 * rest of the kernel.
1463 */
1464static int netfront_resume(struct xenbus_device *dev)
1465{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001466 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001467
1468 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1469
1470 xennet_disconnect_backend(info);
1471 return 0;
1472}
1473
1474static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1475{
1476 char *s, *e, *macstr;
1477 int i;
1478
1479 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1480 if (IS_ERR(macstr))
1481 return PTR_ERR(macstr);
1482
1483 for (i = 0; i < ETH_ALEN; i++) {
1484 mac[i] = simple_strtoul(s, &e, 16);
1485 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1486 kfree(macstr);
1487 return -ENOENT;
1488 }
1489 s = e+1;
1490 }
1491
1492 kfree(macstr);
1493 return 0;
1494}
1495
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001496static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001497{
1498 int err;
1499
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001500 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001501 if (err < 0)
1502 goto fail;
1503
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001504 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001505 xennet_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001506 0, queue->info->netdev->name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001507 if (err < 0)
1508 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001509 queue->rx_evtchn = queue->tx_evtchn;
1510 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001511
1512 return 0;
1513
1514bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001515 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1516 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001517fail:
1518 return err;
1519}
1520
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001521static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001522{
1523 int err;
1524
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001525 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001526 if (err < 0)
1527 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001528 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001529 if (err < 0)
1530 goto alloc_rx_evtchn_fail;
1531
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001532 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1533 "%s-tx", queue->name);
1534 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001535 xennet_tx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001536 0, queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001537 if (err < 0)
1538 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001539 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001540
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001541 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1542 "%s-rx", queue->name);
1543 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001544 xennet_rx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001545 0, queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001546 if (err < 0)
1547 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001548 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001549
1550 return 0;
1551
1552bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001553 unbind_from_irqhandler(queue->tx_irq, queue);
1554 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001555bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001556 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1557 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001558alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001559 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1560 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001561fail:
1562 return err;
1563}
1564
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001565static int setup_netfront(struct xenbus_device *dev,
1566 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001567{
1568 struct xen_netif_tx_sring *txs;
1569 struct xen_netif_rx_sring *rxs;
1570 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001571
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001572 queue->tx_ring_ref = GRANT_INVALID_REF;
1573 queue->rx_ring_ref = GRANT_INVALID_REF;
1574 queue->rx.sring = NULL;
1575 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001576
Ian Campbella144ff02008-06-17 10:47:08 +02001577 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001578 if (!txs) {
1579 err = -ENOMEM;
1580 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1581 goto fail;
1582 }
1583 SHARED_RING_INIT(txs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001584 FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001585
1586 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
Wei Liu1ca29832013-05-20 01:05:12 +00001587 if (err < 0)
1588 goto grant_tx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001589 queue->tx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001590
Ian Campbella144ff02008-06-17 10:47:08 +02001591 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001592 if (!rxs) {
1593 err = -ENOMEM;
1594 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001595 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001596 }
1597 SHARED_RING_INIT(rxs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001598 FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001599
1600 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
Wei Liu1ca29832013-05-20 01:05:12 +00001601 if (err < 0)
1602 goto grant_rx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001603 queue->rx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001604
Wei Liud634bf22013-05-22 06:34:46 +00001605 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001606 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001607 /* setup single event channel if
1608 * a) feature-split-event-channels == 0
1609 * b) feature-split-event-channels == 1 but failed to setup
1610 */
1611 if (!feature_split_evtchn || (feature_split_evtchn && err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001612 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001613
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001614 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001615 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001616
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001617 return 0;
1618
Wei Liu1ca29832013-05-20 01:05:12 +00001619 /* If we fail to setup netfront, it is safe to just revoke access to
1620 * granted pages because backend is not accessing it at this point.
1621 */
Wei Liu1ca29832013-05-20 01:05:12 +00001622alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001623 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001624grant_rx_ring_fail:
1625 free_page((unsigned long)rxs);
1626alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001627 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001628grant_tx_ring_fail:
1629 free_page((unsigned long)txs);
1630fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001631 return err;
1632}
1633
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001634/* Queue-specific initialisation
1635 * This used to be done in xennet_create_dev() but must now
1636 * be run per-queue.
1637 */
1638static int xennet_init_queue(struct netfront_queue *queue)
1639{
1640 unsigned short i;
1641 int err = 0;
1642
1643 spin_lock_init(&queue->tx_lock);
1644 spin_lock_init(&queue->rx_lock);
1645
1646 skb_queue_head_init(&queue->rx_batch);
1647 queue->rx_target = RX_DFL_MIN_TARGET;
1648 queue->rx_min_target = RX_DFL_MIN_TARGET;
1649 queue->rx_max_target = RX_MAX_TARGET;
1650
1651 init_timer(&queue->rx_refill_timer);
1652 queue->rx_refill_timer.data = (unsigned long)queue;
1653 queue->rx_refill_timer.function = rx_refill_timeout;
1654
Wei Liu8b715012014-06-04 10:30:47 +01001655 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
1656 queue->info->netdev->name, queue->id);
1657
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001658 /* Initialise tx_skbs as a free chain containing every entry. */
1659 queue->tx_skb_freelist = 0;
1660 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1661 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1662 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1663 queue->grant_tx_page[i] = NULL;
1664 }
1665
1666 /* Clear out rx_skbs */
1667 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1668 queue->rx_skbs[i] = NULL;
1669 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1670 }
1671
1672 /* A grant for every tx ring slot */
1673 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1674 &queue->gref_tx_head) < 0) {
1675 pr_alert("can't alloc tx grant refs\n");
1676 err = -ENOMEM;
1677 goto exit;
1678 }
1679
1680 /* A grant for every rx ring slot */
1681 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1682 &queue->gref_rx_head) < 0) {
1683 pr_alert("can't alloc rx grant refs\n");
1684 err = -ENOMEM;
1685 goto exit_free_tx;
1686 }
1687
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001688 return 0;
1689
1690 exit_free_tx:
1691 gnttab_free_grant_references(queue->gref_tx_head);
1692 exit:
1693 return err;
1694}
1695
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001696static int write_queue_xenstore_keys(struct netfront_queue *queue,
1697 struct xenbus_transaction *xbt, int write_hierarchical)
1698{
1699 /* Write the queue-specific keys into XenStore in the traditional
1700 * way for a single queue, or in a queue subkeys for multiple
1701 * queues.
1702 */
1703 struct xenbus_device *dev = queue->info->xbdev;
1704 int err;
1705 const char *message;
1706 char *path;
1707 size_t pathsize;
1708
1709 /* Choose the correct place to write the keys */
1710 if (write_hierarchical) {
1711 pathsize = strlen(dev->nodename) + 10;
1712 path = kzalloc(pathsize, GFP_KERNEL);
1713 if (!path) {
1714 err = -ENOMEM;
1715 message = "out of memory while writing ring references";
1716 goto error;
1717 }
1718 snprintf(path, pathsize, "%s/queue-%u",
1719 dev->nodename, queue->id);
1720 } else {
1721 path = (char *)dev->nodename;
1722 }
1723
1724 /* Write ring references */
1725 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1726 queue->tx_ring_ref);
1727 if (err) {
1728 message = "writing tx-ring-ref";
1729 goto error;
1730 }
1731
1732 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1733 queue->rx_ring_ref);
1734 if (err) {
1735 message = "writing rx-ring-ref";
1736 goto error;
1737 }
1738
1739 /* Write event channels; taking into account both shared
1740 * and split event channel scenarios.
1741 */
1742 if (queue->tx_evtchn == queue->rx_evtchn) {
1743 /* Shared event channel */
1744 err = xenbus_printf(*xbt, path,
1745 "event-channel", "%u", queue->tx_evtchn);
1746 if (err) {
1747 message = "writing event-channel";
1748 goto error;
1749 }
1750 } else {
1751 /* Split event channels */
1752 err = xenbus_printf(*xbt, path,
1753 "event-channel-tx", "%u", queue->tx_evtchn);
1754 if (err) {
1755 message = "writing event-channel-tx";
1756 goto error;
1757 }
1758
1759 err = xenbus_printf(*xbt, path,
1760 "event-channel-rx", "%u", queue->rx_evtchn);
1761 if (err) {
1762 message = "writing event-channel-rx";
1763 goto error;
1764 }
1765 }
1766
1767 if (write_hierarchical)
1768 kfree(path);
1769 return 0;
1770
1771error:
1772 if (write_hierarchical)
1773 kfree(path);
1774 xenbus_dev_fatal(dev, err, "%s", message);
1775 return err;
1776}
1777
David Vrabelce587252014-06-18 10:47:28 +01001778static void xennet_destroy_queues(struct netfront_info *info)
1779{
1780 unsigned int i;
1781
1782 rtnl_lock();
1783
1784 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1785 struct netfront_queue *queue = &info->queues[i];
1786
1787 if (netif_running(info->netdev))
1788 napi_disable(&queue->napi);
1789 netif_napi_del(&queue->napi);
1790 }
1791
1792 rtnl_unlock();
1793
1794 kfree(info->queues);
1795 info->queues = NULL;
1796}
1797
1798static int xennet_create_queues(struct netfront_info *info,
1799 unsigned int num_queues)
1800{
1801 unsigned int i;
1802 int ret;
1803
1804 info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
1805 GFP_KERNEL);
1806 if (!info->queues)
1807 return -ENOMEM;
1808
1809 rtnl_lock();
1810
1811 for (i = 0; i < num_queues; i++) {
1812 struct netfront_queue *queue = &info->queues[i];
1813
1814 queue->id = i;
1815 queue->info = info;
1816
1817 ret = xennet_init_queue(queue);
1818 if (ret < 0) {
David Vrabel69cb8522014-07-31 17:38:24 +01001819 dev_warn(&info->netdev->dev,
1820 "only created %d queues\n", i);
David Vrabelce587252014-06-18 10:47:28 +01001821 num_queues = i;
1822 break;
1823 }
1824
1825 netif_napi_add(queue->info->netdev, &queue->napi,
1826 xennet_poll, 64);
1827 if (netif_running(info->netdev))
1828 napi_enable(&queue->napi);
1829 }
1830
1831 netif_set_real_num_tx_queues(info->netdev, num_queues);
1832
1833 rtnl_unlock();
1834
1835 if (num_queues == 0) {
1836 dev_err(&info->netdev->dev, "no queues\n");
1837 return -EINVAL;
1838 }
1839 return 0;
1840}
1841
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001842/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00001843static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001844 struct netfront_info *info)
1845{
1846 const char *message;
1847 struct xenbus_transaction xbt;
1848 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001849 unsigned int feature_split_evtchn;
1850 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001851 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001852 struct netfront_queue *queue = NULL;
1853 unsigned int num_queues = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001854
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001855 info->netdev->irq = 0;
1856
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001857 /* Check if backend supports multiple queues */
1858 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1859 "multi-queue-max-queues", "%u", &max_queues);
1860 if (err < 0)
1861 max_queues = 1;
1862 num_queues = min(max_queues, xennet_max_queues);
1863
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001864 /* Check feature-split-event-channels */
1865 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1866 "feature-split-event-channels", "%u",
1867 &feature_split_evtchn);
1868 if (err < 0)
1869 feature_split_evtchn = 0;
1870
1871 /* Read mac addr. */
1872 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1873 if (err) {
1874 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001875 goto out;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001876 }
1877
David Vrabelce587252014-06-18 10:47:28 +01001878 if (info->queues)
1879 xennet_destroy_queues(info);
1880
1881 err = xennet_create_queues(info, num_queues);
1882 if (err < 0)
1883 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001884
1885 /* Create shared ring, alloc event channel -- for each queue */
1886 for (i = 0; i < num_queues; ++i) {
1887 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001888 err = setup_netfront(dev, queue, feature_split_evtchn);
1889 if (err) {
David Vrabelce587252014-06-18 10:47:28 +01001890 /* setup_netfront() will tidy up the current
1891 * queue on error, but we need to clean up
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001892 * those already allocated.
1893 */
1894 if (i > 0) {
1895 rtnl_lock();
1896 netif_set_real_num_tx_queues(info->netdev, i);
1897 rtnl_unlock();
1898 goto destroy_ring;
1899 } else {
1900 goto out;
1901 }
1902 }
1903 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001904
1905again:
1906 err = xenbus_transaction_start(&xbt);
1907 if (err) {
1908 xenbus_dev_fatal(dev, err, "starting transaction");
1909 goto destroy_ring;
1910 }
1911
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001912 if (num_queues == 1) {
1913 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1914 if (err)
1915 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001916 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001917 /* Write the number of queues */
1918 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
1919 "%u", num_queues);
Wei Liud634bf22013-05-22 06:34:46 +00001920 if (err) {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001921 message = "writing multi-queue-num-queues";
1922 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001923 }
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001924
1925 /* Write the keys for each queue */
1926 for (i = 0; i < num_queues; ++i) {
1927 queue = &info->queues[i];
1928 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1929 if (err)
1930 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001931 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001932 }
1933
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001934 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001935 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1936 1);
1937 if (err) {
1938 message = "writing request-rx-copy";
1939 goto abort_transaction;
1940 }
1941
1942 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1943 if (err) {
1944 message = "writing feature-rx-notify";
1945 goto abort_transaction;
1946 }
1947
1948 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1949 if (err) {
1950 message = "writing feature-sg";
1951 goto abort_transaction;
1952 }
1953
1954 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1955 if (err) {
1956 message = "writing feature-gso-tcpv4";
1957 goto abort_transaction;
1958 }
1959
Paul Durrant2c0057d2014-01-15 17:30:33 +00001960 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1961 if (err) {
1962 message = "writing feature-gso-tcpv6";
1963 goto abort_transaction;
1964 }
1965
1966 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1967 "1");
1968 if (err) {
1969 message = "writing feature-ipv6-csum-offload";
1970 goto abort_transaction;
1971 }
1972
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001973 err = xenbus_transaction_end(xbt, 0);
1974 if (err) {
1975 if (err == -EAGAIN)
1976 goto again;
1977 xenbus_dev_fatal(dev, err, "completing transaction");
1978 goto destroy_ring;
1979 }
1980
1981 return 0;
1982
1983 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001984 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001985abort_transaction_no_dev_fatal:
1986 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001987 destroy_ring:
1988 xennet_disconnect_backend(info);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001989 kfree(info->queues);
1990 info->queues = NULL;
1991 rtnl_lock();
1992 netif_set_real_num_tx_queues(info->netdev, 0);
David Vrabeldb8c8ab2014-07-31 17:38:22 +01001993 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001994 out:
1995 return err;
1996}
1997
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001998static int xennet_connect(struct net_device *dev)
1999{
2000 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002001 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01002002 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002003 unsigned int feature_rx_copy;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002004 unsigned int j = 0;
2005 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002006
2007 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
2008 "feature-rx-copy", "%u", &feature_rx_copy);
2009 if (err != 1)
2010 feature_rx_copy = 0;
2011
2012 if (!feature_rx_copy) {
2013 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07002014 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002015 return -ENODEV;
2016 }
2017
Ian Campbellf502bf22010-08-18 23:27:49 +00002018 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002019 if (err)
2020 return err;
2021
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002022 /* talk_to_netback() sets the correct number of queues */
2023 num_queues = dev->real_num_tx_queues;
2024
Ian Campbell1ba37c52011-05-24 21:56:02 +00002025 rtnl_lock();
Michał Mirosławfb507932011-03-31 01:01:35 +00002026 netdev_update_features(dev);
Ian Campbell1ba37c52011-05-24 21:56:02 +00002027 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002028
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002029 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01002030 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002031 * ready to start sending and receiving packets and give the driver
2032 * domain a kick because we've probably just requeued some
2033 * packets.
2034 */
2035 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002036 for (j = 0; j < num_queues; ++j) {
2037 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01002038
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002039 notify_remote_via_irq(queue->tx_irq);
2040 if (queue->tx_irq != queue->rx_irq)
2041 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002042
David Vrabelf50b4072014-07-02 16:09:14 +01002043 spin_lock_irq(&queue->tx_lock);
2044 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002045 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01002046
2047 spin_lock_bh(&queue->rx_lock);
2048 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002049 spin_unlock_bh(&queue->rx_lock);
2050 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002051
2052 return 0;
2053}
2054
2055/**
2056 * Callback received when the backend's state changes.
2057 */
Ian Campbellf502bf22010-08-18 23:27:49 +00002058static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002059 enum xenbus_state backend_state)
2060{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002061 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002062 struct net_device *netdev = np->netdev;
2063
2064 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2065
2066 switch (backend_state) {
2067 case XenbusStateInitialising:
2068 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002069 case XenbusStateReconfiguring:
2070 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002071 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002072 break;
2073
2074 case XenbusStateInitWait:
2075 if (dev->state != XenbusStateInitialising)
2076 break;
2077 if (xennet_connect(netdev) != 0)
2078 break;
2079 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00002080 break;
2081
2082 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00002083 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002084 break;
2085
David Vrabelbce3ea82014-02-04 18:50:26 +00002086 case XenbusStateClosed:
2087 if (dev->state == XenbusStateClosed)
2088 break;
2089 /* Missed the backend's CLOSING state -- fallthrough */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002090 case XenbusStateClosing:
2091 xenbus_frontend_closed(dev);
2092 break;
2093 }
2094}
2095
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002096static const struct xennet_stat {
2097 char name[ETH_GSTRING_LEN];
2098 u16 offset;
2099} xennet_stats[] = {
2100 {
2101 "rx_gso_checksum_fixup",
2102 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2103 },
2104};
2105
2106static int xennet_get_sset_count(struct net_device *dev, int string_set)
2107{
2108 switch (string_set) {
2109 case ETH_SS_STATS:
2110 return ARRAY_SIZE(xennet_stats);
2111 default:
2112 return -EINVAL;
2113 }
2114}
2115
2116static void xennet_get_ethtool_stats(struct net_device *dev,
2117 struct ethtool_stats *stats, u64 * data)
2118{
2119 void *np = netdev_priv(dev);
2120 int i;
2121
2122 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002123 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002124}
2125
2126static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2127{
2128 int i;
2129
2130 switch (stringset) {
2131 case ETH_SS_STATS:
2132 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2133 memcpy(data + i * ETH_GSTRING_LEN,
2134 xennet_stats[i].name, ETH_GSTRING_LEN);
2135 break;
2136 }
2137}
2138
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002139static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002140{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002141 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002142
2143 .get_sset_count = xennet_get_sset_count,
2144 .get_ethtool_stats = xennet_get_ethtool_stats,
2145 .get_strings = xennet_get_strings,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002146};
2147
2148#ifdef CONFIG_SYSFS
2149static ssize_t show_rxbuf_min(struct device *dev,
2150 struct device_attribute *attr, char *buf)
2151{
2152 struct net_device *netdev = to_net_dev(dev);
2153 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002154 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002155
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002156 if (num_queues)
2157 return sprintf(buf, "%u\n", info->queues[0].rx_min_target);
2158 else
2159 return sprintf(buf, "%u\n", RX_MIN_TARGET);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002160}
2161
2162static ssize_t store_rxbuf_min(struct device *dev,
2163 struct device_attribute *attr,
2164 const char *buf, size_t len)
2165{
2166 struct net_device *netdev = to_net_dev(dev);
2167 struct netfront_info *np = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002168 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002169 char *endp;
2170 unsigned long target;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002171 unsigned int i;
2172 struct netfront_queue *queue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002173
2174 if (!capable(CAP_NET_ADMIN))
2175 return -EPERM;
2176
2177 target = simple_strtoul(buf, &endp, 0);
2178 if (endp == buf)
2179 return -EBADMSG;
2180
2181 if (target < RX_MIN_TARGET)
2182 target = RX_MIN_TARGET;
2183 if (target > RX_MAX_TARGET)
2184 target = RX_MAX_TARGET;
2185
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002186 for (i = 0; i < num_queues; ++i) {
2187 queue = &np->queues[i];
2188 spin_lock_bh(&queue->rx_lock);
2189 if (target > queue->rx_max_target)
2190 queue->rx_max_target = target;
2191 queue->rx_min_target = target;
2192 if (target > queue->rx_target)
2193 queue->rx_target = target;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002194
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002195 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002196
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002197 spin_unlock_bh(&queue->rx_lock);
2198 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002199 return len;
2200}
2201
2202static ssize_t show_rxbuf_max(struct device *dev,
2203 struct device_attribute *attr, char *buf)
2204{
2205 struct net_device *netdev = to_net_dev(dev);
2206 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002207 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002208
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002209 if (num_queues)
2210 return sprintf(buf, "%u\n", info->queues[0].rx_max_target);
2211 else
2212 return sprintf(buf, "%u\n", RX_MAX_TARGET);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002213}
2214
2215static ssize_t store_rxbuf_max(struct device *dev,
2216 struct device_attribute *attr,
2217 const char *buf, size_t len)
2218{
2219 struct net_device *netdev = to_net_dev(dev);
2220 struct netfront_info *np = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002221 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002222 char *endp;
2223 unsigned long target;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002224 unsigned int i = 0;
2225 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002226
2227 if (!capable(CAP_NET_ADMIN))
2228 return -EPERM;
2229
2230 target = simple_strtoul(buf, &endp, 0);
2231 if (endp == buf)
2232 return -EBADMSG;
2233
2234 if (target < RX_MIN_TARGET)
2235 target = RX_MIN_TARGET;
2236 if (target > RX_MAX_TARGET)
2237 target = RX_MAX_TARGET;
2238
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002239 for (i = 0; i < num_queues; ++i) {
2240 queue = &np->queues[i];
2241 spin_lock_bh(&queue->rx_lock);
2242 if (target < queue->rx_min_target)
2243 queue->rx_min_target = target;
2244 queue->rx_max_target = target;
2245 if (target < queue->rx_target)
2246 queue->rx_target = target;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002247
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002248 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002249
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002250 spin_unlock_bh(&queue->rx_lock);
2251 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002252 return len;
2253}
2254
2255static ssize_t show_rxbuf_cur(struct device *dev,
2256 struct device_attribute *attr, char *buf)
2257{
2258 struct net_device *netdev = to_net_dev(dev);
2259 struct netfront_info *info = netdev_priv(netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002260 unsigned int num_queues = netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002261
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002262 if (num_queues)
2263 return sprintf(buf, "%u\n", info->queues[0].rx_target);
2264 else
2265 return sprintf(buf, "0\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002266}
2267
2268static struct device_attribute xennet_attrs[] = {
2269 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
2270 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
2271 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
2272};
2273
2274static int xennet_sysfs_addif(struct net_device *netdev)
2275{
2276 int i;
2277 int err;
2278
2279 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
2280 err = device_create_file(&netdev->dev,
2281 &xennet_attrs[i]);
2282 if (err)
2283 goto fail;
2284 }
2285 return 0;
2286
2287 fail:
2288 while (--i >= 0)
2289 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2290 return err;
2291}
2292
2293static void xennet_sysfs_delif(struct net_device *netdev)
2294{
2295 int i;
2296
2297 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
2298 device_remove_file(&netdev->dev, &xennet_attrs[i]);
2299}
2300
2301#endif /* CONFIG_SYSFS */
2302
Jan Beulich73db1442011-12-22 09:08:13 +00002303static const struct xenbus_device_id netfront_ids[] = {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002304 { "vif" },
2305 { "" }
2306};
2307
2308
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002309static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002310{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002311 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002312 unsigned int num_queues = info->netdev->real_num_tx_queues;
2313 struct netfront_queue *queue = NULL;
2314 unsigned int i = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002315
2316 dev_dbg(&dev->dev, "%s\n", dev->nodename);
2317
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002318 xennet_disconnect_backend(info);
2319
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002320 xennet_sysfs_delif(info->netdev);
2321
Ian Campbell6bc96d02012-06-25 22:48:41 +00002322 unregister_netdev(info->netdev);
2323
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002324 for (i = 0; i < num_queues; ++i) {
2325 queue = &info->queues[i];
2326 del_timer_sync(&queue->rx_refill_timer);
2327 }
2328
2329 if (num_queues) {
2330 kfree(info->queues);
2331 info->queues = NULL;
2332 }
Ian Campbell6bc96d02012-06-25 22:48:41 +00002333
stephen hemmingere00f85b2011-06-21 05:35:31 +00002334 free_percpu(info->stats);
2335
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002336 free_netdev(info->netdev);
2337
2338 return 0;
2339}
2340
Jan Beulich73db1442011-12-22 09:08:13 +00002341static DEFINE_XENBUS_DRIVER(netfront, ,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002342 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002343 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002344 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002345 .otherend_changed = netback_changed,
Jan Beulich73db1442011-12-22 09:08:13 +00002346);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002347
2348static int __init netif_init(void)
2349{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002350 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002351 return -ENODEV;
2352
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002353 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002354 return -ENODEV;
2355
Joe Perches383eda32013-06-27 21:57:49 -07002356 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002357
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002358 /* Allow as many queues as there are CPUs, by default */
2359 xennet_max_queues = num_online_cpus();
2360
Al Viroffb78a22008-11-22 17:38:14 +00002361 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002362}
2363module_init(netif_init);
2364
2365
2366static void __exit netif_exit(void)
2367{
Al Viroffb78a22008-11-22 17:38:14 +00002368 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002369}
2370module_exit(netif_exit);
2371
2372MODULE_DESCRIPTION("Xen virtual network device frontend");
2373MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002374MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002375MODULE_ALIAS("xennet");