blob: e9b960f0ff32c8af2ff404a138780ff751bf4572 [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)
David Vrabel1f3c2eb2014-10-22 11:17:06 +010080
81/* Minimum number of Rx slots (includes slot for GSO metadata). */
82#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070083
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +010084/* Queue name is interface name with "-qNNN" appended */
85#define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
86
87/* IRQ name is queue name with "-tx" or "-rx" appended */
88#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
89
stephen hemmingere00f85b2011-06-21 05:35:31 +000090struct netfront_stats {
David Vrabel900e1832015-01-13 16:42:42 +000091 u64 packets;
92 u64 bytes;
stephen hemmingere00f85b2011-06-21 05:35:31 +000093 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
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700140 struct timer_list rx_refill_timer;
141
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700142 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
143 grant_ref_t gref_rx_head;
144 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100145};
146
147struct netfront_info {
148 struct list_head list;
149 struct net_device *netdev;
150
151 struct xenbus_device *xbdev;
152
153 /* Multi-queue support */
154 struct netfront_queue *queues;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000155
156 /* Statistics */
David Vrabel900e1832015-01-13 16:42:42 +0000157 struct netfront_stats __percpu *rx_stats;
158 struct netfront_stats __percpu *tx_stats;
stephen hemmingere00f85b2011-06-21 05:35:31 +0000159
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100160 atomic_t rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700161};
162
163struct netfront_rx_info {
164 struct xen_netif_rx_response rx;
165 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
166};
167
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700168static void skb_entry_set_link(union skb_entry *list, unsigned short id)
169{
170 list->link = id;
171}
172
173static int skb_entry_is_link(const union skb_entry *list)
174{
175 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
Eric Dumazet807540b2010-09-23 05:40:09 +0000176 return (unsigned long)list->skb < PAGE_OFFSET;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700177}
178
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700179/*
180 * Access macros for acquiring freeing slots in tx_skbs[].
181 */
182
183static void add_id_to_freelist(unsigned *head, union skb_entry *list,
184 unsigned short id)
185{
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700186 skb_entry_set_link(&list[id], *head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700187 *head = id;
188}
189
190static unsigned short get_id_from_freelist(unsigned *head,
191 union skb_entry *list)
192{
193 unsigned int id = *head;
194 *head = list[id].link;
195 return id;
196}
197
198static int xennet_rxidx(RING_IDX idx)
199{
200 return idx & (NET_RX_RING_SIZE - 1);
201}
202
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100203static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700204 RING_IDX ri)
205{
206 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100207 struct sk_buff *skb = queue->rx_skbs[i];
208 queue->rx_skbs[i] = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700209 return skb;
210}
211
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100212static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700213 RING_IDX ri)
214{
215 int i = xennet_rxidx(ri);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100216 grant_ref_t ref = queue->grant_rx_ref[i];
217 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700218 return ref;
219}
220
221#ifdef CONFIG_SYSFS
Takashi Iwai27b917e2015-02-04 14:38:55 +0100222static const struct attribute_group xennet_dev_group;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700223#endif
224
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000225static bool xennet_can_sg(struct net_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700226{
Michał Mirosław3ad9b352011-11-16 14:05:33 +0000227 return dev->features & NETIF_F_SG;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700228}
229
230
231static void rx_refill_timeout(unsigned long data)
232{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100233 struct netfront_queue *queue = (struct netfront_queue *)data;
234 napi_schedule(&queue->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700235}
236
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100237static int netfront_tx_slot_available(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700238{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100239 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100240 (NET_TX_RING_SIZE - MAX_SKB_FRAGS - 2);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700241}
242
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100243static void xennet_maybe_wake_tx(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700244{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100245 struct net_device *dev = queue->info->netdev;
246 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700247
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100248 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
249 netfront_tx_slot_available(queue) &&
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700250 likely(netif_running(dev)))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100251 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700252}
253
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100254
255static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700256{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700257 struct sk_buff *skb;
258 struct page *page;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100259
260 skb = __netdev_alloc_skb(queue->info->netdev,
261 RX_COPY_THRESHOLD + NET_IP_ALIGN,
262 GFP_ATOMIC | __GFP_NOWARN);
263 if (unlikely(!skb))
264 return NULL;
265
266 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
267 if (!page) {
268 kfree_skb(skb);
269 return NULL;
270 }
271 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
272
273 /* Align ip header to a 16 bytes boundary */
274 skb_reserve(skb, NET_IP_ALIGN);
275 skb->dev = queue->info->netdev;
276
277 return skb;
278}
279
280
281static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
282{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100283 RING_IDX req_prod = queue->rx.req_prod_pvt;
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100284 int notify;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700285
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100286 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700287 return;
288
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100289 for (req_prod = queue->rx.req_prod_pvt;
290 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
291 req_prod++) {
292 struct sk_buff *skb;
293 unsigned short id;
294 grant_ref_t ref;
295 unsigned long pfn;
296 struct xen_netif_rx_request *req;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700297
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100298 skb = xennet_alloc_one_rx_buffer(queue);
299 if (!skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700300 break;
301
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100302 id = xennet_rxidx(req_prod);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700303
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100304 BUG_ON(queue->rx_skbs[id]);
305 queue->rx_skbs[id] = skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700306
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100307 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700308 BUG_ON((signed short)ref < 0);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100309 queue->grant_rx_ref[id] = ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700310
Ian Campbell01c68022011-10-05 00:28:47 +0000311 pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0]));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700312
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100313 req = RING_GET_REQUEST(&queue->rx, req_prod);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700314 gnttab_grant_foreign_access_ref(ref,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100315 queue->info->xbdev->otherend_id,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700316 pfn_to_mfn(pfn),
317 0);
318
319 req->id = id;
320 req->gref = ref;
321 }
322
David Vrabel1f3c2eb2014-10-22 11:17:06 +0100323 queue->rx.req_prod_pvt = req_prod;
324
325 /* Not enough requests? Try again later. */
326 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN) {
327 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
328 return;
329 }
330
Jeremy Fitzhardinge5dcddfa2007-08-07 14:56:42 -0700331 wmb(); /* barrier so backend seens requests */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700332
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100333 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700334 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100335 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700336}
337
338static int xennet_open(struct net_device *dev)
339{
340 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100341 unsigned int num_queues = dev->real_num_tx_queues;
342 unsigned int i = 0;
343 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700344
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100345 for (i = 0; i < num_queues; ++i) {
346 queue = &np->queues[i];
347 napi_enable(&queue->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700348
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100349 spin_lock_bh(&queue->rx_lock);
350 if (netif_carrier_ok(dev)) {
351 xennet_alloc_rx_buffers(queue);
352 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
353 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
354 napi_schedule(&queue->napi);
355 }
356 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700357 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700358
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100359 netif_tx_start_all_queues(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700360
361 return 0;
362}
363
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100364static void xennet_tx_buf_gc(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700365{
366 RING_IDX cons, prod;
367 unsigned short id;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700368 struct sk_buff *skb;
369
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100370 BUG_ON(!netif_carrier_ok(queue->info->netdev));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700371
372 do {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100373 prod = queue->tx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700374 rmb(); /* Ensure we see responses up to 'rp'. */
375
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100376 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700377 struct xen_netif_tx_response *txrsp;
378
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100379 txrsp = RING_GET_RESPONSE(&queue->tx, cons);
Ian Campbellf942dc22011-03-15 00:06:18 +0000380 if (txrsp->status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700381 continue;
382
383 id = txrsp->id;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100384 skb = queue->tx_skbs[id].skb;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700385 if (unlikely(gnttab_query_foreign_access(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100386 queue->grant_tx_ref[id]) != 0)) {
Joe Perches383eda32013-06-27 21:57:49 -0700387 pr_alert("%s: warning -- grant still in use by backend domain\n",
388 __func__);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700389 BUG();
390 }
391 gnttab_end_foreign_access_ref(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100392 queue->grant_tx_ref[id], GNTMAP_readonly);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700393 gnttab_release_grant_reference(
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100394 &queue->gref_tx_head, queue->grant_tx_ref[id]);
395 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
396 queue->grant_tx_page[id] = NULL;
397 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700398 dev_kfree_skb_irq(skb);
399 }
400
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100401 queue->tx.rsp_cons = prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700402
403 /*
404 * Set a new event, then check for race with update of tx_cons.
405 * Note that it is essential to schedule a callback, no matter
406 * how few buffers are pending. Even if there is space in the
407 * transmit ring, higher layers may be blocked because too much
408 * data is outstanding: in such cases notification from Xen is
409 * likely to be the only kick that we'll get.
410 */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100411 queue->tx.sring->rsp_event =
412 prod + ((queue->tx.sring->req_prod - prod) >> 1) + 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700413 mb(); /* update shared area */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100414 } while ((cons == prod) && (prod != queue->tx.sring->rsp_prod));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700415
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100416 xennet_maybe_wake_tx(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700417}
418
David Vrabela55e8bb2015-01-13 17:16:44 +0000419static struct xen_netif_tx_request *xennet_make_one_txreq(
420 struct netfront_queue *queue, struct sk_buff *skb,
421 struct page *page, unsigned int offset, unsigned int len)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700422{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700423 unsigned int id;
David Vrabela55e8bb2015-01-13 17:16:44 +0000424 struct xen_netif_tx_request *tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700425 grant_ref_t ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700426
David Vrabela55e8bb2015-01-13 17:16:44 +0000427 len = min_t(unsigned int, PAGE_SIZE - offset, len);
428
429 id = get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs);
430 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
431 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
432 BUG_ON((signed short)ref < 0);
433
434 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
435 page_to_mfn(page), GNTMAP_readonly);
436
437 queue->tx_skbs[id].skb = skb;
438 queue->grant_tx_page[id] = page;
439 queue->grant_tx_ref[id] = ref;
440
441 tx->id = id;
442 tx->gref = ref;
443 tx->offset = offset;
444 tx->size = len;
445 tx->flags = 0;
446
447 return tx;
448}
449
450static struct xen_netif_tx_request *xennet_make_txreqs(
451 struct netfront_queue *queue, struct xen_netif_tx_request *tx,
452 struct sk_buff *skb, struct page *page,
453 unsigned int offset, unsigned int len)
454{
455 /* Skip unused frames from start of page */
456 page += offset >> PAGE_SHIFT;
457 offset &= ~PAGE_MASK;
458
459 while (len) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000460 tx->flags |= XEN_NETTXF_more_data;
David Vrabela55e8bb2015-01-13 17:16:44 +0000461 tx = xennet_make_one_txreq(queue, skb_get(skb),
462 page, offset, len);
463 page++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700464 offset = 0;
David Vrabela55e8bb2015-01-13 17:16:44 +0000465 len -= tx->size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700466 }
467
David Vrabela55e8bb2015-01-13 17:16:44 +0000468 return tx;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700469}
470
Ian Campbellf36c3742012-11-21 02:02:16 +0000471/*
David Vrabele84448d2015-01-13 17:16:43 +0000472 * Count how many ring slots are required to send this skb. Each frag
473 * might be a compound page.
Ian Campbellf36c3742012-11-21 02:02:16 +0000474 */
David Vrabele84448d2015-01-13 17:16:43 +0000475static int xennet_count_skb_slots(struct sk_buff *skb)
Ian Campbellf36c3742012-11-21 02:02:16 +0000476{
477 int i, frags = skb_shinfo(skb)->nr_frags;
David Vrabele84448d2015-01-13 17:16:43 +0000478 int pages;
479
480 pages = PFN_UP(offset_in_page(skb->data) + skb_headlen(skb));
Ian Campbellf36c3742012-11-21 02:02:16 +0000481
482 for (i = 0; i < frags; i++) {
483 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
484 unsigned long size = skb_frag_size(frag);
485 unsigned long offset = frag->page_offset;
486
487 /* Skip unused frames from start of page */
488 offset &= ~PAGE_MASK;
489
490 pages += PFN_UP(offset + size);
491 }
492
493 return pages;
494}
495
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100496static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
497 void *accel_priv, select_queue_fallback_t fallback)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100498{
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +0100499 unsigned int num_queues = dev->real_num_tx_queues;
500 u32 hash;
501 u16 queue_idx;
502
503 /* First, check if there is only one queue */
504 if (num_queues == 1) {
505 queue_idx = 0;
506 } else {
507 hash = skb_get_hash(skb);
508 queue_idx = hash % num_queues;
509 }
510
511 return queue_idx;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100512}
513
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700514static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
515{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700516 struct netfront_info *np = netdev_priv(dev);
David Vrabel900e1832015-01-13 16:42:42 +0000517 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
David Vrabela55e8bb2015-01-13 17:16:44 +0000518 struct xen_netif_tx_request *tx, *first_tx;
519 unsigned int i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700520 int notify;
Ian Campbellf36c3742012-11-21 02:02:16 +0000521 int slots;
David Vrabela55e8bb2015-01-13 17:16:44 +0000522 struct page *page;
523 unsigned int offset;
524 unsigned int len;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +0000525 unsigned long flags;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100526 struct netfront_queue *queue = NULL;
527 unsigned int num_queues = dev->real_num_tx_queues;
528 u16 queue_index;
529
530 /* Drop the packet if no queues are set up */
531 if (num_queues < 1)
532 goto drop;
533 /* Determine which queue to transmit this SKB on */
534 queue_index = skb_get_queue_mapping(skb);
535 queue = &np->queues[queue_index];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700536
Wei Liu9ecd1a72013-04-22 02:20:41 +0000537 /* If skb->len is too big for wire format, drop skb and alert
538 * user about misconfiguration.
539 */
540 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
541 net_alert_ratelimited(
542 "xennet: skb->len = %u, too big for wire format\n",
543 skb->len);
544 goto drop;
545 }
546
David Vrabele84448d2015-01-13 17:16:43 +0000547 slots = xennet_count_skb_slots(skb);
Ian Campbellf36c3742012-11-21 02:02:16 +0000548 if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
Zoltan Kiss97a6d1b2014-08-11 18:32:23 +0100549 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
550 slots, skb->len);
551 if (skb_linearize(skb))
552 goto drop;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700553 }
554
David Vrabela55e8bb2015-01-13 17:16:44 +0000555 page = virt_to_page(skb->data);
556 offset = offset_in_page(skb->data);
557 len = skb_headlen(skb);
558
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100559 spin_lock_irqsave(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700560
561 if (unlikely(!netif_carrier_ok(dev) ||
Ian Campbellf36c3742012-11-21 02:02:16 +0000562 (slots > 1 && !xennet_can_sg(dev)) ||
Tom Herbert04ffcb22014-10-14 15:19:06 -0700563 netif_needs_gso(dev, skb, netif_skb_features(skb)))) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100564 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700565 goto drop;
566 }
567
David Vrabela55e8bb2015-01-13 17:16:44 +0000568 /* First request for the linear area. */
569 first_tx = tx = xennet_make_one_txreq(queue, skb,
570 page, offset, len);
571 page++;
572 offset = 0;
573 len -= tx->size;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700574
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700575 if (skb->ip_summed == CHECKSUM_PARTIAL)
576 /* local packet? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000577 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700578 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
579 /* remote but checksummed. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000580 tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700581
David Vrabela55e8bb2015-01-13 17:16:44 +0000582 /* Optional extra info after the first request. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700583 if (skb_shinfo(skb)->gso_size) {
584 struct xen_netif_extra_info *gso;
585
586 gso = (struct xen_netif_extra_info *)
David Vrabela55e8bb2015-01-13 17:16:44 +0000587 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700588
Wei Liue2d617c02013-03-25 01:08:17 +0000589 tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700590
591 gso->u.gso.size = skb_shinfo(skb)->gso_size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000592 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
593 XEN_NETIF_GSO_TYPE_TCPV6 :
594 XEN_NETIF_GSO_TYPE_TCPV4;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700595 gso->u.gso.pad = 0;
596 gso->u.gso.features = 0;
597
598 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
599 gso->flags = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700600 }
601
David Vrabela55e8bb2015-01-13 17:16:44 +0000602 /* Requests for the rest of the linear area. */
603 tx = xennet_make_txreqs(queue, tx, skb, page, offset, len);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700604
David Vrabela55e8bb2015-01-13 17:16:44 +0000605 /* Requests for all the frags. */
606 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
607 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
608 tx = xennet_make_txreqs(queue, tx, skb,
609 skb_frag_page(frag), frag->page_offset,
610 skb_frag_size(frag));
611 }
612
613 /* First request has the packet length. */
614 first_tx->size = skb->len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700615
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100616 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700617 if (notify)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100618 notify_remote_via_irq(queue->tx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700619
David Vrabel900e1832015-01-13 16:42:42 +0000620 u64_stats_update_begin(&tx_stats->syncp);
621 tx_stats->bytes += skb->len;
622 tx_stats->packets++;
623 u64_stats_update_end(&tx_stats->syncp);
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700624
625 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100626 xennet_tx_buf_gc(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700627
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100628 if (!netfront_tx_slot_available(queue))
629 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700630
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100631 spin_unlock_irqrestore(&queue->tx_lock, flags);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700632
Patrick McHardy6ed10652009-06-23 06:03:08 +0000633 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700634
635 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700636 dev->stats.tx_dropped++;
Eric W. Biederman979de8a2014-03-15 18:33:04 -0700637 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000638 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700639}
640
641static int xennet_close(struct net_device *dev)
642{
643 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100644 unsigned int num_queues = dev->real_num_tx_queues;
645 unsigned int i;
646 struct netfront_queue *queue;
647 netif_tx_stop_all_queues(np->netdev);
648 for (i = 0; i < num_queues; ++i) {
649 queue = &np->queues[i];
650 napi_disable(&queue->napi);
651 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700652 return 0;
653}
654
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100655static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700656 grant_ref_t ref)
657{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100658 int new = xennet_rxidx(queue->rx.req_prod_pvt);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700659
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100660 BUG_ON(queue->rx_skbs[new]);
661 queue->rx_skbs[new] = skb;
662 queue->grant_rx_ref[new] = ref;
663 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
664 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
665 queue->rx.req_prod_pvt++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700666}
667
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100668static int xennet_get_extras(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700669 struct xen_netif_extra_info *extras,
670 RING_IDX rp)
671
672{
673 struct xen_netif_extra_info *extra;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100674 struct device *dev = &queue->info->netdev->dev;
675 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700676 int err = 0;
677
678 do {
679 struct sk_buff *skb;
680 grant_ref_t ref;
681
682 if (unlikely(cons + 1 == rp)) {
683 if (net_ratelimit())
684 dev_warn(dev, "Missing extra info\n");
685 err = -EBADR;
686 break;
687 }
688
689 extra = (struct xen_netif_extra_info *)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100690 RING_GET_RESPONSE(&queue->rx, ++cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700691
692 if (unlikely(!extra->type ||
693 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
694 if (net_ratelimit())
695 dev_warn(dev, "Invalid extra type: %d\n",
696 extra->type);
697 err = -EINVAL;
698 } else {
699 memcpy(&extras[extra->type - 1], extra,
700 sizeof(*extra));
701 }
702
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100703 skb = xennet_get_rx_skb(queue, cons);
704 ref = xennet_get_rx_ref(queue, cons);
705 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700706 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
707
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100708 queue->rx.rsp_cons = cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700709 return err;
710}
711
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100712static int xennet_get_responses(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700713 struct netfront_rx_info *rinfo, RING_IDX rp,
714 struct sk_buff_head *list)
715{
716 struct xen_netif_rx_response *rx = &rinfo->rx;
717 struct xen_netif_extra_info *extras = rinfo->extras;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100718 struct device *dev = &queue->info->netdev->dev;
719 RING_IDX cons = queue->rx.rsp_cons;
720 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
721 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700722 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
Wei Liu7158ff62013-03-25 01:08:19 +0000723 int slots = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700724 int err = 0;
725 unsigned long ret;
726
Ian Campbellf942dc22011-03-15 00:06:18 +0000727 if (rx->flags & XEN_NETRXF_extra_info) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100728 err = xennet_get_extras(queue, extras, rp);
729 cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700730 }
731
732 for (;;) {
733 if (unlikely(rx->status < 0 ||
734 rx->offset + rx->status > PAGE_SIZE)) {
735 if (net_ratelimit())
736 dev_warn(dev, "rx->offset: %x, size: %u\n",
737 rx->offset, rx->status);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100738 xennet_move_rx_slot(queue, skb, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700739 err = -EINVAL;
740 goto next;
741 }
742
743 /*
744 * This definitely indicates a bug, either in this driver or in
745 * the backend driver. In future this should flag the bad
Wei Liu697089d2013-04-22 02:20:40 +0000746 * situation to the system controller to reboot the backend.
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700747 */
748 if (ref == GRANT_INVALID_REF) {
749 if (net_ratelimit())
750 dev_warn(dev, "Bad rx response id %d.\n",
751 rx->id);
752 err = -EINVAL;
753 goto next;
754 }
755
756 ret = gnttab_end_foreign_access_ref(ref, 0);
757 BUG_ON(!ret);
758
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100759 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700760
761 __skb_queue_tail(list, skb);
762
763next:
Ian Campbellf942dc22011-03-15 00:06:18 +0000764 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700765 break;
766
Wei Liu7158ff62013-03-25 01:08:19 +0000767 if (cons + slots == rp) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700768 if (net_ratelimit())
Wei Liu7158ff62013-03-25 01:08:19 +0000769 dev_warn(dev, "Need more slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700770 err = -ENOENT;
771 break;
772 }
773
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100774 rx = RING_GET_RESPONSE(&queue->rx, cons + slots);
775 skb = xennet_get_rx_skb(queue, cons + slots);
776 ref = xennet_get_rx_ref(queue, cons + slots);
Wei Liu7158ff62013-03-25 01:08:19 +0000777 slots++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700778 }
779
Wei Liu7158ff62013-03-25 01:08:19 +0000780 if (unlikely(slots > max)) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700781 if (net_ratelimit())
Wei Liu697089d2013-04-22 02:20:40 +0000782 dev_warn(dev, "Too many slots\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700783 err = -E2BIG;
784 }
785
786 if (unlikely(err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100787 queue->rx.rsp_cons = cons + slots;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700788
789 return err;
790}
791
792static int xennet_set_skb_gso(struct sk_buff *skb,
793 struct xen_netif_extra_info *gso)
794{
795 if (!gso->u.gso.size) {
796 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700797 pr_warn("GSO size must not be zero\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700798 return -EINVAL;
799 }
800
Paul Durrant2c0057d2014-01-15 17:30:33 +0000801 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
802 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700803 if (net_ratelimit())
Joe Perches383eda32013-06-27 21:57:49 -0700804 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700805 return -EINVAL;
806 }
807
808 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrant2c0057d2014-01-15 17:30:33 +0000809 skb_shinfo(skb)->gso_type =
810 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
811 SKB_GSO_TCPV4 :
812 SKB_GSO_TCPV6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700813
814 /* Header must be checked, and gso_segs computed. */
815 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
816 skb_shinfo(skb)->gso_segs = 0;
817
818 return 0;
819}
820
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100821static RING_IDX xennet_fill_frags(struct netfront_queue *queue,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700822 struct sk_buff *skb,
823 struct sk_buff_head *list)
824{
825 struct skb_shared_info *shinfo = skb_shinfo(skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100826 RING_IDX cons = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700827 struct sk_buff *nskb;
828
829 while ((nskb = __skb_dequeue(list))) {
830 struct xen_netif_rx_response *rx =
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100831 RING_GET_RESPONSE(&queue->rx, ++cons);
Ian Campbell01c68022011-10-05 00:28:47 +0000832 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700833
Jan Beulich093b9c72013-07-17 08:09:37 +0100834 if (shinfo->nr_frags == MAX_SKB_FRAGS) {
835 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700836
Jan Beulich093b9c72013-07-17 08:09:37 +0100837 BUG_ON(pull_to <= skb_headlen(skb));
838 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
839 }
840 BUG_ON(shinfo->nr_frags >= MAX_SKB_FRAGS);
841
842 skb_add_rx_frag(skb, shinfo->nr_frags, skb_frag_page(nfrag),
843 rx->offset, rx->status, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700844
845 skb_shinfo(nskb)->nr_frags = 0;
846 kfree_skb(nskb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700847 }
848
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700849 return cons;
850}
851
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000852static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700853{
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000854 bool recalculate_partial_csum = false;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000855
856 /*
857 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
858 * peers can fail to set NETRXF_csum_blank when sending a GSO
859 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
860 * recalculate the partial checksum.
861 */
862 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
863 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100864 atomic_inc(&np->rx_gso_checksum_fixup);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000865 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000866 recalculate_partial_csum = true;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000867 }
868
869 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
870 if (skb->ip_summed != CHECKSUM_PARTIAL)
871 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700872
Paul Durrantb5cf66c2014-01-09 10:02:48 +0000873 return skb_checksum_setup(skb, recalculate_partial_csum);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700874}
875
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100876static int handle_incoming_queue(struct netfront_queue *queue,
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700877 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700878{
David Vrabel900e1832015-01-13 16:42:42 +0000879 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700880 int packets_dropped = 0;
881 struct sk_buff *skb;
882
883 while ((skb = __skb_dequeue(rxq)) != NULL) {
Ian Campbell36832432012-08-22 00:26:47 +0000884 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700885
Jan Beulich093b9c72013-07-17 08:09:37 +0100886 if (pull_to > skb_headlen(skb))
887 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700888
889 /* Ethernet work: Delayed to here as it peeks the header. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100890 skb->protocol = eth_type_trans(skb, queue->info->netdev);
Wei Liud554f732014-02-19 18:48:34 +0000891 skb_reset_network_header(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700892
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100893 if (checksum_setup(queue->info->netdev, skb)) {
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000894 kfree_skb(skb);
895 packets_dropped++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100896 queue->info->netdev->stats.rx_errors++;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000897 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700898 }
899
David Vrabel900e1832015-01-13 16:42:42 +0000900 u64_stats_update_begin(&rx_stats->syncp);
901 rx_stats->packets++;
902 rx_stats->bytes += skb->len;
903 u64_stats_update_end(&rx_stats->syncp);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700904
905 /* Pass it up. */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100906 napi_gro_receive(&queue->napi, skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700907 }
908
909 return packets_dropped;
910}
911
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700912static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700913{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100914 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
915 struct net_device *dev = queue->info->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700916 struct sk_buff *skb;
917 struct netfront_rx_info rinfo;
918 struct xen_netif_rx_response *rx = &rinfo.rx;
919 struct xen_netif_extra_info *extras = rinfo.extras;
920 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700921 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700922 struct sk_buff_head rxq;
923 struct sk_buff_head errq;
924 struct sk_buff_head tmpq;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700925 int err;
926
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100927 spin_lock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700928
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700929 skb_queue_head_init(&rxq);
930 skb_queue_head_init(&errq);
931 skb_queue_head_init(&tmpq);
932
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100933 rp = queue->rx.sring->rsp_prod;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700934 rmb(); /* Ensure we see queued responses up to 'rp'. */
935
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100936 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700937 work_done = 0;
938 while ((i != rp) && (work_done < budget)) {
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100939 memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx));
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700940 memset(extras, 0, sizeof(rinfo.extras));
941
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100942 err = xennet_get_responses(queue, &rinfo, rp, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700943
944 if (unlikely(err)) {
945err:
946 while ((skb = __skb_dequeue(&tmpq)))
947 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700948 dev->stats.rx_errors++;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100949 i = queue->rx.rsp_cons;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700950 continue;
951 }
952
953 skb = __skb_dequeue(&tmpq);
954
955 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
956 struct xen_netif_extra_info *gso;
957 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
958
959 if (unlikely(xennet_set_skb_gso(skb, gso))) {
960 __skb_queue_head(&tmpq, skb);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100961 queue->rx.rsp_cons += skb_queue_len(&tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700962 goto err;
963 }
964 }
965
Ian Campbell36832432012-08-22 00:26:47 +0000966 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
967 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
968 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700969
Ian Campbell36832432012-08-22 00:26:47 +0000970 skb_shinfo(skb)->frags[0].page_offset = rx->offset;
971 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
972 skb->data_len = rx->status;
Jan Beulich093b9c72013-07-17 08:09:37 +0100973 skb->len += rx->status;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700974
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100975 i = xennet_fill_frags(queue, skb, &tmpq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700976
Ian Campbellf942dc22011-03-15 00:06:18 +0000977 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700978 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000979 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700980 skb->ip_summed = CHECKSUM_UNNECESSARY;
981
982 __skb_queue_tail(&rxq, skb);
983
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100984 queue->rx.rsp_cons = ++i;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700985 work_done++;
986 }
987
Wang Chen56cfe5d2008-05-22 18:09:06 +0800988 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700989
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100990 work_done -= handle_incoming_queue(queue, &rxq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700991
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100992 xennet_alloc_rx_buffers(queue);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700993
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700994 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700995 int more_to_do = 0;
996
David Vrabel6a6dc082014-12-16 18:59:46 +0000997 napi_complete(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700998
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +0100999 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
David Vrabel6a6dc082014-12-16 18:59:46 +00001000 if (more_to_do)
1001 napi_schedule(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001002 }
1003
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001004 spin_unlock(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001005
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001006 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001007}
1008
1009static int xennet_change_mtu(struct net_device *dev, int mtu)
1010{
Wei Liu9ecd1a72013-04-22 02:20:41 +00001011 int max = xennet_can_sg(dev) ?
1012 XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001013
1014 if (mtu > max)
1015 return -EINVAL;
1016 dev->mtu = mtu;
1017 return 0;
1018}
1019
stephen hemmingere00f85b2011-06-21 05:35:31 +00001020static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
1021 struct rtnl_link_stats64 *tot)
1022{
1023 struct netfront_info *np = netdev_priv(dev);
1024 int cpu;
1025
1026 for_each_possible_cpu(cpu) {
David Vrabel900e1832015-01-13 16:42:42 +00001027 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1028 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
stephen hemmingere00f85b2011-06-21 05:35:31 +00001029 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1030 unsigned int start;
1031
1032 do {
David Vrabel900e1832015-01-13 16:42:42 +00001033 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1034 tx_packets = tx_stats->packets;
1035 tx_bytes = tx_stats->bytes;
1036 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001037
David Vrabel900e1832015-01-13 16:42:42 +00001038 do {
1039 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1040 rx_packets = rx_stats->packets;
1041 rx_bytes = rx_stats->bytes;
1042 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
stephen hemmingere00f85b2011-06-21 05:35:31 +00001043
1044 tot->rx_packets += rx_packets;
1045 tot->tx_packets += tx_packets;
1046 tot->rx_bytes += rx_bytes;
1047 tot->tx_bytes += tx_bytes;
1048 }
1049
1050 tot->rx_errors = dev->stats.rx_errors;
1051 tot->tx_dropped = dev->stats.tx_dropped;
1052
1053 return tot;
1054}
1055
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001056static void xennet_release_tx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001057{
1058 struct sk_buff *skb;
1059 int i;
1060
1061 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1062 /* Skip over entries which are actually freelist references */
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001063 if (skb_entry_is_link(&queue->tx_skbs[i]))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001064 continue;
1065
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001066 skb = queue->tx_skbs[i].skb;
1067 get_page(queue->grant_tx_page[i]);
1068 gnttab_end_foreign_access(queue->grant_tx_ref[i],
Annie Licefe0072014-01-28 11:35:42 +08001069 GNTMAP_readonly,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001070 (unsigned long)page_address(queue->grant_tx_page[i]));
1071 queue->grant_tx_page[i] = NULL;
1072 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1073 add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001074 dev_kfree_skb_irq(skb);
1075 }
1076}
1077
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001078static void xennet_release_rx_bufs(struct netfront_queue *queue)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001079{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001080 int id, ref;
1081
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001082 spin_lock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001083
1084 for (id = 0; id < NET_RX_RING_SIZE; id++) {
Annie Licefe0072014-01-28 11:35:42 +08001085 struct sk_buff *skb;
1086 struct page *page;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001087
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001088 skb = queue->rx_skbs[id];
Annie Licefe0072014-01-28 11:35:42 +08001089 if (!skb)
1090 continue;
1091
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001092 ref = queue->grant_rx_ref[id];
Annie Licefe0072014-01-28 11:35:42 +08001093 if (ref == GRANT_INVALID_REF)
1094 continue;
1095
1096 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
1097
1098 /* gnttab_end_foreign_access() needs a page ref until
1099 * foreign access is ended (which may be deferred).
1100 */
1101 get_page(page);
1102 gnttab_end_foreign_access(ref, 0,
1103 (unsigned long)page_address(page));
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001104 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001105
Annie Licefe0072014-01-28 11:35:42 +08001106 kfree_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001107 }
1108
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001109 spin_unlock_bh(&queue->rx_lock);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001110}
1111
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001112static netdev_features_t xennet_fix_features(struct net_device *dev,
1113 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001114{
1115 struct netfront_info *np = netdev_priv(dev);
1116 int val;
1117
1118 if (features & NETIF_F_SG) {
1119 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1120 "%d", &val) < 0)
1121 val = 0;
1122
1123 if (!val)
1124 features &= ~NETIF_F_SG;
1125 }
1126
Paul Durrant2c0057d2014-01-15 17:30:33 +00001127 if (features & NETIF_F_IPV6_CSUM) {
1128 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1129 "feature-ipv6-csum-offload", "%d", &val) < 0)
1130 val = 0;
1131
1132 if (!val)
1133 features &= ~NETIF_F_IPV6_CSUM;
1134 }
1135
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001136 if (features & NETIF_F_TSO) {
1137 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1138 "feature-gso-tcpv4", "%d", &val) < 0)
1139 val = 0;
1140
1141 if (!val)
1142 features &= ~NETIF_F_TSO;
1143 }
1144
Paul Durrant2c0057d2014-01-15 17:30:33 +00001145 if (features & NETIF_F_TSO6) {
1146 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1147 "feature-gso-tcpv6", "%d", &val) < 0)
1148 val = 0;
1149
1150 if (!val)
1151 features &= ~NETIF_F_TSO6;
1152 }
1153
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001154 return features;
1155}
1156
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001157static int xennet_set_features(struct net_device *dev,
1158 netdev_features_t features)
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001159{
1160 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1161 netdev_info(dev, "Reducing MTU because no SG offload");
1162 dev->mtu = ETH_DATA_LEN;
1163 }
1164
1165 return 0;
1166}
1167
Wei Liud634bf22013-05-22 06:34:46 +00001168static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001169{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001170 struct netfront_queue *queue = dev_id;
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001171 unsigned long flags;
1172
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001173 spin_lock_irqsave(&queue->tx_lock, flags);
1174 xennet_tx_buf_gc(queue);
1175 spin_unlock_irqrestore(&queue->tx_lock, flags);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001176
1177 return IRQ_HANDLED;
1178}
1179
Wei Liud634bf22013-05-22 06:34:46 +00001180static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1181{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001182 struct netfront_queue *queue = dev_id;
1183 struct net_device *dev = queue->info->netdev;
Wei Liud634bf22013-05-22 06:34:46 +00001184
1185 if (likely(netif_carrier_ok(dev) &&
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001186 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
David Vrabel76541862014-06-18 10:47:27 +01001187 napi_schedule(&queue->napi);
Wei Liud634bf22013-05-22 06:34:46 +00001188
1189 return IRQ_HANDLED;
1190}
1191
1192static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1193{
1194 xennet_tx_interrupt(irq, dev_id);
1195 xennet_rx_interrupt(irq, dev_id);
1196 return IRQ_HANDLED;
1197}
1198
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001199#ifdef CONFIG_NET_POLL_CONTROLLER
1200static void xennet_poll_controller(struct net_device *dev)
1201{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001202 /* Poll each queue */
1203 struct netfront_info *info = netdev_priv(dev);
1204 unsigned int num_queues = dev->real_num_tx_queues;
1205 unsigned int i;
1206 for (i = 0; i < num_queues; ++i)
1207 xennet_interrupt(0, &info->queues[i]);
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001208}
1209#endif
1210
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001211static const struct net_device_ops xennet_netdev_ops = {
1212 .ndo_open = xennet_open,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001213 .ndo_stop = xennet_close,
1214 .ndo_start_xmit = xennet_start_xmit,
1215 .ndo_change_mtu = xennet_change_mtu,
stephen hemmingere00f85b2011-06-21 05:35:31 +00001216 .ndo_get_stats64 = xennet_get_stats64,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001217 .ndo_set_mac_address = eth_mac_addr,
1218 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001219 .ndo_fix_features = xennet_fix_features,
1220 .ndo_set_features = xennet_set_features,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001221 .ndo_select_queue = xennet_select_queue,
Konrad Rzeszutek Wilkcf66f9d2012-01-23 08:24:43 +00001222#ifdef CONFIG_NET_POLL_CONTROLLER
1223 .ndo_poll_controller = xennet_poll_controller,
1224#endif
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001225};
1226
David Vrabel900e1832015-01-13 16:42:42 +00001227static void xennet_free_netdev(struct net_device *netdev)
1228{
1229 struct netfront_info *np = netdev_priv(netdev);
1230
1231 free_percpu(np->rx_stats);
1232 free_percpu(np->tx_stats);
1233 free_netdev(netdev);
1234}
1235
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001236static struct net_device *xennet_create_dev(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001237{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001238 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001239 struct net_device *netdev;
1240 struct netfront_info *np;
1241
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001242 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
Joe Perches41de8d42012-01-29 13:47:52 +00001243 if (!netdev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001244 return ERR_PTR(-ENOMEM);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001245
1246 np = netdev_priv(netdev);
1247 np->xbdev = dev;
1248
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001249 /* No need to use rtnl_lock() before the call below as it
1250 * happens before register_netdev().
1251 */
1252 netif_set_real_num_tx_queues(netdev, 0);
1253 np->queues = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001254
stephen hemmingere00f85b2011-06-21 05:35:31 +00001255 err = -ENOMEM;
David Vrabel900e1832015-01-13 16:42:42 +00001256 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1257 if (np->rx_stats == NULL)
1258 goto exit;
1259 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1260 if (np->tx_stats == NULL)
stephen hemmingere00f85b2011-06-21 05:35:31 +00001261 goto exit;
1262
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001263 netdev->netdev_ops = &xennet_netdev_ops;
1264
Michał Mirosławfb507932011-03-31 01:01:35 +00001265 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1266 NETIF_F_GSO_ROBUST;
Paul Durrant2c0057d2014-01-15 17:30:33 +00001267 netdev->hw_features = NETIF_F_SG |
1268 NETIF_F_IPV6_CSUM |
1269 NETIF_F_TSO | NETIF_F_TSO6;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001270
Ian Campbellfc3e5942011-04-04 11:07:57 -07001271 /*
1272 * Assume that all hw features are available for now. This set
1273 * will be adjusted by the call to netdev_update_features() in
1274 * xennet_connect() which is the earliest point where we can
1275 * negotiate with the backend regarding supported features.
1276 */
1277 netdev->features |= netdev->hw_features;
1278
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001279 netdev->ethtool_ops = &xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001280 SET_NETDEV_DEV(netdev, &dev->dev);
1281
Wei Liu9ecd1a72013-04-22 02:20:41 +00001282 netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
1283
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001284 np->netdev = netdev;
1285
1286 netif_carrier_off(netdev);
1287
1288 return netdev;
1289
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001290 exit:
David Vrabel900e1832015-01-13 16:42:42 +00001291 xennet_free_netdev(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001292 return ERR_PTR(err);
1293}
1294
1295/**
1296 * Entry point to this code when a new device is created. Allocate the basic
1297 * structures and the ring buffers for communication with the backend, and
1298 * inform the backend of the appropriate details for those.
1299 */
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05001300static int netfront_probe(struct xenbus_device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001301 const struct xenbus_device_id *id)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001302{
1303 int err;
1304 struct net_device *netdev;
1305 struct netfront_info *info;
1306
1307 netdev = xennet_create_dev(dev);
1308 if (IS_ERR(netdev)) {
1309 err = PTR_ERR(netdev);
1310 xenbus_dev_fatal(dev, err, "creating netdev");
1311 return err;
1312 }
1313
1314 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001315 dev_set_drvdata(&dev->dev, info);
Takashi Iwai27b917e2015-02-04 14:38:55 +01001316#ifdef CONFIG_SYSFS
1317 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1318#endif
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001319 err = register_netdev(info->netdev);
1320 if (err) {
Joe Perches383eda32013-06-27 21:57:49 -07001321 pr_warn("%s: register_netdev err=%d\n", __func__, err);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001322 goto fail;
1323 }
1324
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001325 return 0;
1326
1327 fail:
David Vrabel900e1832015-01-13 16:42:42 +00001328 xennet_free_netdev(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001329 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001330 return err;
1331}
1332
1333static void xennet_end_access(int ref, void *page)
1334{
1335 /* This frees the page as a side-effect */
1336 if (ref != GRANT_INVALID_REF)
1337 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1338}
1339
1340static void xennet_disconnect_backend(struct netfront_info *info)
1341{
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001342 unsigned int i = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001343 unsigned int num_queues = info->netdev->real_num_tx_queues;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001344
David Vrabelf9feb1e2014-07-02 16:09:15 +01001345 netif_carrier_off(info->netdev);
1346
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001347 for (i = 0; i < num_queues; ++i) {
David Vrabel76541862014-06-18 10:47:27 +01001348 struct netfront_queue *queue = &info->queues[i];
1349
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001350 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1351 unbind_from_irqhandler(queue->tx_irq, queue);
1352 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1353 unbind_from_irqhandler(queue->tx_irq, queue);
1354 unbind_from_irqhandler(queue->rx_irq, queue);
1355 }
1356 queue->tx_evtchn = queue->rx_evtchn = 0;
1357 queue->tx_irq = queue->rx_irq = 0;
1358
David Vrabelf9feb1e2014-07-02 16:09:15 +01001359 napi_synchronize(&queue->napi);
1360
David Vrabela5b5dc32014-07-31 17:38:23 +01001361 xennet_release_tx_bufs(queue);
1362 xennet_release_rx_bufs(queue);
1363 gnttab_free_grant_references(queue->gref_tx_head);
1364 gnttab_free_grant_references(queue->gref_rx_head);
1365
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001366 /* End access and free the pages */
1367 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1368 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
1369
1370 queue->tx_ring_ref = GRANT_INVALID_REF;
1371 queue->rx_ring_ref = GRANT_INVALID_REF;
1372 queue->tx.sring = NULL;
1373 queue->rx.sring = NULL;
Wei Liud634bf22013-05-22 06:34:46 +00001374 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001375}
1376
1377/**
1378 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1379 * driver restart. We tear down our netif structure and recreate it, but
1380 * leave the device-layer structures intact so that this is transparent to the
1381 * rest of the kernel.
1382 */
1383static int netfront_resume(struct xenbus_device *dev)
1384{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001385 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001386
1387 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1388
1389 xennet_disconnect_backend(info);
1390 return 0;
1391}
1392
1393static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1394{
1395 char *s, *e, *macstr;
1396 int i;
1397
1398 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1399 if (IS_ERR(macstr))
1400 return PTR_ERR(macstr);
1401
1402 for (i = 0; i < ETH_ALEN; i++) {
1403 mac[i] = simple_strtoul(s, &e, 16);
1404 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1405 kfree(macstr);
1406 return -ENOENT;
1407 }
1408 s = e+1;
1409 }
1410
1411 kfree(macstr);
1412 return 0;
1413}
1414
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001415static int setup_netfront_single(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001416{
1417 int err;
1418
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001419 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001420 if (err < 0)
1421 goto fail;
1422
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001423 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001424 xennet_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001425 0, queue->info->netdev->name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001426 if (err < 0)
1427 goto bind_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001428 queue->rx_evtchn = queue->tx_evtchn;
1429 queue->rx_irq = queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001430
1431 return 0;
1432
1433bind_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001434 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1435 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001436fail:
1437 return err;
1438}
1439
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001440static int setup_netfront_split(struct netfront_queue *queue)
Wei Liud634bf22013-05-22 06:34:46 +00001441{
1442 int err;
1443
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001444 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001445 if (err < 0)
1446 goto fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001447 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
Wei Liud634bf22013-05-22 06:34:46 +00001448 if (err < 0)
1449 goto alloc_rx_evtchn_fail;
1450
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001451 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1452 "%s-tx", queue->name);
1453 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001454 xennet_tx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001455 0, queue->tx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001456 if (err < 0)
1457 goto bind_tx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001458 queue->tx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001459
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001460 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1461 "%s-rx", queue->name);
1462 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
Wei Liud634bf22013-05-22 06:34:46 +00001463 xennet_rx_interrupt,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001464 0, queue->rx_irq_name, queue);
Wei Liud634bf22013-05-22 06:34:46 +00001465 if (err < 0)
1466 goto bind_rx_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001467 queue->rx_irq = err;
Wei Liud634bf22013-05-22 06:34:46 +00001468
1469 return 0;
1470
1471bind_rx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001472 unbind_from_irqhandler(queue->tx_irq, queue);
1473 queue->tx_irq = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001474bind_tx_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001475 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1476 queue->rx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001477alloc_rx_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001478 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1479 queue->tx_evtchn = 0;
Wei Liud634bf22013-05-22 06:34:46 +00001480fail:
1481 return err;
1482}
1483
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001484static int setup_netfront(struct xenbus_device *dev,
1485 struct netfront_queue *queue, unsigned int feature_split_evtchn)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001486{
1487 struct xen_netif_tx_sring *txs;
1488 struct xen_netif_rx_sring *rxs;
1489 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001490
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001491 queue->tx_ring_ref = GRANT_INVALID_REF;
1492 queue->rx_ring_ref = GRANT_INVALID_REF;
1493 queue->rx.sring = NULL;
1494 queue->tx.sring = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001495
Ian Campbella144ff02008-06-17 10:47:08 +02001496 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001497 if (!txs) {
1498 err = -ENOMEM;
1499 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1500 goto fail;
1501 }
1502 SHARED_RING_INIT(txs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001503 FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001504
1505 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
Wei Liu1ca29832013-05-20 01:05:12 +00001506 if (err < 0)
1507 goto grant_tx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001508 queue->tx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001509
Ian Campbella144ff02008-06-17 10:47:08 +02001510 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001511 if (!rxs) {
1512 err = -ENOMEM;
1513 xenbus_dev_fatal(dev, err, "allocating rx ring page");
Wei Liu1ca29832013-05-20 01:05:12 +00001514 goto alloc_rx_ring_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001515 }
1516 SHARED_RING_INIT(rxs);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001517 FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001518
1519 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
Wei Liu1ca29832013-05-20 01:05:12 +00001520 if (err < 0)
1521 goto grant_rx_ring_fail;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001522 queue->rx_ring_ref = err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001523
Wei Liud634bf22013-05-22 06:34:46 +00001524 if (feature_split_evtchn)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001525 err = setup_netfront_split(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001526 /* setup single event channel if
1527 * a) feature-split-event-channels == 0
1528 * b) feature-split-event-channels == 1 but failed to setup
1529 */
1530 if (!feature_split_evtchn || (feature_split_evtchn && err))
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001531 err = setup_netfront_single(queue);
Wei Liud634bf22013-05-22 06:34:46 +00001532
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001533 if (err)
Wei Liu1ca29832013-05-20 01:05:12 +00001534 goto alloc_evtchn_fail;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001535
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001536 return 0;
1537
Wei Liu1ca29832013-05-20 01:05:12 +00001538 /* If we fail to setup netfront, it is safe to just revoke access to
1539 * granted pages because backend is not accessing it at this point.
1540 */
Wei Liu1ca29832013-05-20 01:05:12 +00001541alloc_evtchn_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001542 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001543grant_rx_ring_fail:
1544 free_page((unsigned long)rxs);
1545alloc_rx_ring_fail:
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001546 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
Wei Liu1ca29832013-05-20 01:05:12 +00001547grant_tx_ring_fail:
1548 free_page((unsigned long)txs);
1549fail:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001550 return err;
1551}
1552
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001553/* Queue-specific initialisation
1554 * This used to be done in xennet_create_dev() but must now
1555 * be run per-queue.
1556 */
1557static int xennet_init_queue(struct netfront_queue *queue)
1558{
1559 unsigned short i;
1560 int err = 0;
1561
1562 spin_lock_init(&queue->tx_lock);
1563 spin_lock_init(&queue->rx_lock);
1564
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001565 init_timer(&queue->rx_refill_timer);
1566 queue->rx_refill_timer.data = (unsigned long)queue;
1567 queue->rx_refill_timer.function = rx_refill_timeout;
1568
Wei Liu8b715012014-06-04 10:30:47 +01001569 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
1570 queue->info->netdev->name, queue->id);
1571
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001572 /* Initialise tx_skbs as a free chain containing every entry. */
1573 queue->tx_skb_freelist = 0;
1574 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1575 skb_entry_set_link(&queue->tx_skbs[i], i+1);
1576 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1577 queue->grant_tx_page[i] = NULL;
1578 }
1579
1580 /* Clear out rx_skbs */
1581 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1582 queue->rx_skbs[i] = NULL;
1583 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1584 }
1585
1586 /* A grant for every tx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001587 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001588 &queue->gref_tx_head) < 0) {
1589 pr_alert("can't alloc tx grant refs\n");
1590 err = -ENOMEM;
1591 goto exit;
1592 }
1593
1594 /* A grant for every rx ring slot */
David Vrabel1f3c2eb2014-10-22 11:17:06 +01001595 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001596 &queue->gref_rx_head) < 0) {
1597 pr_alert("can't alloc rx grant refs\n");
1598 err = -ENOMEM;
1599 goto exit_free_tx;
1600 }
1601
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001602 return 0;
1603
1604 exit_free_tx:
1605 gnttab_free_grant_references(queue->gref_tx_head);
1606 exit:
1607 return err;
1608}
1609
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001610static int write_queue_xenstore_keys(struct netfront_queue *queue,
1611 struct xenbus_transaction *xbt, int write_hierarchical)
1612{
1613 /* Write the queue-specific keys into XenStore in the traditional
1614 * way for a single queue, or in a queue subkeys for multiple
1615 * queues.
1616 */
1617 struct xenbus_device *dev = queue->info->xbdev;
1618 int err;
1619 const char *message;
1620 char *path;
1621 size_t pathsize;
1622
1623 /* Choose the correct place to write the keys */
1624 if (write_hierarchical) {
1625 pathsize = strlen(dev->nodename) + 10;
1626 path = kzalloc(pathsize, GFP_KERNEL);
1627 if (!path) {
1628 err = -ENOMEM;
1629 message = "out of memory while writing ring references";
1630 goto error;
1631 }
1632 snprintf(path, pathsize, "%s/queue-%u",
1633 dev->nodename, queue->id);
1634 } else {
1635 path = (char *)dev->nodename;
1636 }
1637
1638 /* Write ring references */
1639 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1640 queue->tx_ring_ref);
1641 if (err) {
1642 message = "writing tx-ring-ref";
1643 goto error;
1644 }
1645
1646 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1647 queue->rx_ring_ref);
1648 if (err) {
1649 message = "writing rx-ring-ref";
1650 goto error;
1651 }
1652
1653 /* Write event channels; taking into account both shared
1654 * and split event channel scenarios.
1655 */
1656 if (queue->tx_evtchn == queue->rx_evtchn) {
1657 /* Shared event channel */
1658 err = xenbus_printf(*xbt, path,
1659 "event-channel", "%u", queue->tx_evtchn);
1660 if (err) {
1661 message = "writing event-channel";
1662 goto error;
1663 }
1664 } else {
1665 /* Split event channels */
1666 err = xenbus_printf(*xbt, path,
1667 "event-channel-tx", "%u", queue->tx_evtchn);
1668 if (err) {
1669 message = "writing event-channel-tx";
1670 goto error;
1671 }
1672
1673 err = xenbus_printf(*xbt, path,
1674 "event-channel-rx", "%u", queue->rx_evtchn);
1675 if (err) {
1676 message = "writing event-channel-rx";
1677 goto error;
1678 }
1679 }
1680
1681 if (write_hierarchical)
1682 kfree(path);
1683 return 0;
1684
1685error:
1686 if (write_hierarchical)
1687 kfree(path);
1688 xenbus_dev_fatal(dev, err, "%s", message);
1689 return err;
1690}
1691
David Vrabelce587252014-06-18 10:47:28 +01001692static void xennet_destroy_queues(struct netfront_info *info)
1693{
1694 unsigned int i;
1695
1696 rtnl_lock();
1697
1698 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1699 struct netfront_queue *queue = &info->queues[i];
1700
1701 if (netif_running(info->netdev))
1702 napi_disable(&queue->napi);
1703 netif_napi_del(&queue->napi);
1704 }
1705
1706 rtnl_unlock();
1707
1708 kfree(info->queues);
1709 info->queues = NULL;
1710}
1711
1712static int xennet_create_queues(struct netfront_info *info,
1713 unsigned int num_queues)
1714{
1715 unsigned int i;
1716 int ret;
1717
1718 info->queues = kcalloc(num_queues, sizeof(struct netfront_queue),
1719 GFP_KERNEL);
1720 if (!info->queues)
1721 return -ENOMEM;
1722
1723 rtnl_lock();
1724
1725 for (i = 0; i < num_queues; i++) {
1726 struct netfront_queue *queue = &info->queues[i];
1727
1728 queue->id = i;
1729 queue->info = info;
1730
1731 ret = xennet_init_queue(queue);
1732 if (ret < 0) {
David Vrabel69cb8522014-07-31 17:38:24 +01001733 dev_warn(&info->netdev->dev,
1734 "only created %d queues\n", i);
David Vrabelce587252014-06-18 10:47:28 +01001735 num_queues = i;
1736 break;
1737 }
1738
1739 netif_napi_add(queue->info->netdev, &queue->napi,
1740 xennet_poll, 64);
1741 if (netif_running(info->netdev))
1742 napi_enable(&queue->napi);
1743 }
1744
1745 netif_set_real_num_tx_queues(info->netdev, num_queues);
1746
1747 rtnl_unlock();
1748
1749 if (num_queues == 0) {
1750 dev_err(&info->netdev->dev, "no queues\n");
1751 return -EINVAL;
1752 }
1753 return 0;
1754}
1755
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001756/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00001757static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001758 struct netfront_info *info)
1759{
1760 const char *message;
1761 struct xenbus_transaction xbt;
1762 int err;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001763 unsigned int feature_split_evtchn;
1764 unsigned int i = 0;
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001765 unsigned int max_queues = 0;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001766 struct netfront_queue *queue = NULL;
1767 unsigned int num_queues = 1;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001768
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001769 info->netdev->irq = 0;
1770
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001771 /* Check if backend supports multiple queues */
1772 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1773 "multi-queue-max-queues", "%u", &max_queues);
1774 if (err < 0)
1775 max_queues = 1;
1776 num_queues = min(max_queues, xennet_max_queues);
1777
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001778 /* Check feature-split-event-channels */
1779 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1780 "feature-split-event-channels", "%u",
1781 &feature_split_evtchn);
1782 if (err < 0)
1783 feature_split_evtchn = 0;
1784
1785 /* Read mac addr. */
1786 err = xen_net_read_mac(dev, info->netdev->dev_addr);
1787 if (err) {
1788 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001789 goto out;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001790 }
1791
David Vrabelce587252014-06-18 10:47:28 +01001792 if (info->queues)
1793 xennet_destroy_queues(info);
1794
1795 err = xennet_create_queues(info, num_queues);
1796 if (err < 0)
1797 goto destroy_ring;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001798
1799 /* Create shared ring, alloc event channel -- for each queue */
1800 for (i = 0; i < num_queues; ++i) {
1801 queue = &info->queues[i];
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001802 err = setup_netfront(dev, queue, feature_split_evtchn);
1803 if (err) {
David Vrabelce587252014-06-18 10:47:28 +01001804 /* setup_netfront() will tidy up the current
1805 * queue on error, but we need to clean up
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001806 * those already allocated.
1807 */
1808 if (i > 0) {
1809 rtnl_lock();
1810 netif_set_real_num_tx_queues(info->netdev, i);
1811 rtnl_unlock();
1812 goto destroy_ring;
1813 } else {
1814 goto out;
1815 }
1816 }
1817 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001818
1819again:
1820 err = xenbus_transaction_start(&xbt);
1821 if (err) {
1822 xenbus_dev_fatal(dev, err, "starting transaction");
1823 goto destroy_ring;
1824 }
1825
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001826 if (num_queues == 1) {
1827 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
1828 if (err)
1829 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001830 } else {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001831 /* Write the number of queues */
1832 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues",
1833 "%u", num_queues);
Wei Liud634bf22013-05-22 06:34:46 +00001834 if (err) {
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001835 message = "writing multi-queue-num-queues";
1836 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001837 }
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001838
1839 /* Write the keys for each queue */
1840 for (i = 0; i < num_queues; ++i) {
1841 queue = &info->queues[i];
1842 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
1843 if (err)
1844 goto abort_transaction_no_dev_fatal;
Wei Liud634bf22013-05-22 06:34:46 +00001845 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001846 }
1847
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001848 /* The remaining keys are not queue-specific */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001849 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1850 1);
1851 if (err) {
1852 message = "writing request-rx-copy";
1853 goto abort_transaction;
1854 }
1855
1856 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1857 if (err) {
1858 message = "writing feature-rx-notify";
1859 goto abort_transaction;
1860 }
1861
1862 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1863 if (err) {
1864 message = "writing feature-sg";
1865 goto abort_transaction;
1866 }
1867
1868 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1869 if (err) {
1870 message = "writing feature-gso-tcpv4";
1871 goto abort_transaction;
1872 }
1873
Paul Durrant2c0057d2014-01-15 17:30:33 +00001874 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
1875 if (err) {
1876 message = "writing feature-gso-tcpv6";
1877 goto abort_transaction;
1878 }
1879
1880 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
1881 "1");
1882 if (err) {
1883 message = "writing feature-ipv6-csum-offload";
1884 goto abort_transaction;
1885 }
1886
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001887 err = xenbus_transaction_end(xbt, 0);
1888 if (err) {
1889 if (err == -EAGAIN)
1890 goto again;
1891 xenbus_dev_fatal(dev, err, "completing transaction");
1892 goto destroy_ring;
1893 }
1894
1895 return 0;
1896
1897 abort_transaction:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001898 xenbus_dev_fatal(dev, err, "%s", message);
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01001899abort_transaction_no_dev_fatal:
1900 xenbus_transaction_end(xbt, 1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001901 destroy_ring:
1902 xennet_disconnect_backend(info);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001903 kfree(info->queues);
1904 info->queues = NULL;
1905 rtnl_lock();
1906 netif_set_real_num_tx_queues(info->netdev, 0);
David Vrabeldb8c8ab2014-07-31 17:38:22 +01001907 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001908 out:
1909 return err;
1910}
1911
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001912static int xennet_connect(struct net_device *dev)
1913{
1914 struct netfront_info *np = netdev_priv(dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001915 unsigned int num_queues = 0;
David Vrabela5b5dc32014-07-31 17:38:23 +01001916 int err;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001917 unsigned int feature_rx_copy;
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001918 unsigned int j = 0;
1919 struct netfront_queue *queue = NULL;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001920
1921 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1922 "feature-rx-copy", "%u", &feature_rx_copy);
1923 if (err != 1)
1924 feature_rx_copy = 0;
1925
1926 if (!feature_rx_copy) {
1927 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07001928 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001929 return -ENODEV;
1930 }
1931
Ian Campbellf502bf22010-08-18 23:27:49 +00001932 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001933 if (err)
1934 return err;
1935
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001936 /* talk_to_netback() sets the correct number of queues */
1937 num_queues = dev->real_num_tx_queues;
1938
Ian Campbell1ba37c52011-05-24 21:56:02 +00001939 rtnl_lock();
Michał Mirosławfb507932011-03-31 01:01:35 +00001940 netdev_update_features(dev);
Ian Campbell1ba37c52011-05-24 21:56:02 +00001941 rtnl_unlock();
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001942
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001943 /*
David Vrabela5b5dc32014-07-31 17:38:23 +01001944 * All public and private state should now be sane. Get
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001945 * ready to start sending and receiving packets and give the driver
1946 * domain a kick because we've probably just requeued some
1947 * packets.
1948 */
1949 netif_carrier_on(np->netdev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001950 for (j = 0; j < num_queues; ++j) {
1951 queue = &np->queues[j];
David Vrabelf50b4072014-07-02 16:09:14 +01001952
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001953 notify_remote_via_irq(queue->tx_irq);
1954 if (queue->tx_irq != queue->rx_irq)
1955 notify_remote_via_irq(queue->rx_irq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001956
David Vrabelf50b4072014-07-02 16:09:14 +01001957 spin_lock_irq(&queue->tx_lock);
1958 xennet_tx_buf_gc(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001959 spin_unlock_irq(&queue->tx_lock);
David Vrabelf50b4072014-07-02 16:09:14 +01001960
1961 spin_lock_bh(&queue->rx_lock);
1962 xennet_alloc_rx_buffers(queue);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01001963 spin_unlock_bh(&queue->rx_lock);
1964 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001965
1966 return 0;
1967}
1968
1969/**
1970 * Callback received when the backend's state changes.
1971 */
Ian Campbellf502bf22010-08-18 23:27:49 +00001972static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001973 enum xenbus_state backend_state)
1974{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001975 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001976 struct net_device *netdev = np->netdev;
1977
1978 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1979
1980 switch (backend_state) {
1981 case XenbusStateInitialising:
1982 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001983 case XenbusStateReconfiguring:
1984 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001985 case XenbusStateUnknown:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001986 break;
1987
1988 case XenbusStateInitWait:
1989 if (dev->state != XenbusStateInitialising)
1990 break;
1991 if (xennet_connect(netdev) != 0)
1992 break;
1993 xenbus_switch_state(dev, XenbusStateConnected);
Laszlo Ersek08e34eb2011-12-11 01:48:59 +00001994 break;
1995
1996 case XenbusStateConnected:
Amerigo Wangee89bab2012-08-09 22:14:56 +00001997 netdev_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001998 break;
1999
David Vrabelbce3ea82014-02-04 18:50:26 +00002000 case XenbusStateClosed:
2001 if (dev->state == XenbusStateClosed)
2002 break;
2003 /* Missed the backend's CLOSING state -- fallthrough */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002004 case XenbusStateClosing:
2005 xenbus_frontend_closed(dev);
2006 break;
2007 }
2008}
2009
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002010static const struct xennet_stat {
2011 char name[ETH_GSTRING_LEN];
2012 u16 offset;
2013} xennet_stats[] = {
2014 {
2015 "rx_gso_checksum_fixup",
2016 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2017 },
2018};
2019
2020static int xennet_get_sset_count(struct net_device *dev, int string_set)
2021{
2022 switch (string_set) {
2023 case ETH_SS_STATS:
2024 return ARRAY_SIZE(xennet_stats);
2025 default:
2026 return -EINVAL;
2027 }
2028}
2029
2030static void xennet_get_ethtool_stats(struct net_device *dev,
2031 struct ethtool_stats *stats, u64 * data)
2032{
2033 void *np = netdev_priv(dev);
2034 int i;
2035
2036 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002037 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002038}
2039
2040static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2041{
2042 int i;
2043
2044 switch (stringset) {
2045 case ETH_SS_STATS:
2046 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2047 memcpy(data + i * ETH_GSTRING_LEN,
2048 xennet_stats[i].name, ETH_GSTRING_LEN);
2049 break;
2050 }
2051}
2052
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002053static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002054{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002055 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00002056
2057 .get_sset_count = xennet_get_sset_count,
2058 .get_ethtool_stats = xennet_get_ethtool_stats,
2059 .get_strings = xennet_get_strings,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002060};
2061
2062#ifdef CONFIG_SYSFS
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002063static ssize_t show_rxbuf(struct device *dev,
2064 struct device_attribute *attr, char *buf)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002065{
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002066 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002067}
2068
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002069static ssize_t store_rxbuf(struct device *dev,
2070 struct device_attribute *attr,
2071 const char *buf, size_t len)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002072{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002073 char *endp;
2074 unsigned long target;
2075
2076 if (!capable(CAP_NET_ADMIN))
2077 return -EPERM;
2078
2079 target = simple_strtoul(buf, &endp, 0);
2080 if (endp == buf)
2081 return -EBADMSG;
2082
David Vrabel1f3c2eb2014-10-22 11:17:06 +01002083 /* rxbuf_min and rxbuf_max are no longer configurable. */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002084
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002085 return len;
2086}
2087
Takashi Iwai27b917e2015-02-04 14:38:55 +01002088static DEVICE_ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2089static DEVICE_ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf, store_rxbuf);
2090static DEVICE_ATTR(rxbuf_cur, S_IRUGO, show_rxbuf, NULL);
2091
2092static struct attribute *xennet_dev_attrs[] = {
2093 &dev_attr_rxbuf_min.attr,
2094 &dev_attr_rxbuf_max.attr,
2095 &dev_attr_rxbuf_cur.attr,
2096 NULL
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002097};
2098
Takashi Iwai27b917e2015-02-04 14:38:55 +01002099static const struct attribute_group xennet_dev_group = {
2100 .attrs = xennet_dev_attrs
2101};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002102#endif /* CONFIG_SYSFS */
2103
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002104static int xennet_remove(struct xenbus_device *dev)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002105{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07002106 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002107 unsigned int num_queues = info->netdev->real_num_tx_queues;
2108 struct netfront_queue *queue = NULL;
2109 unsigned int i = 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002110
2111 dev_dbg(&dev->dev, "%s\n", dev->nodename);
2112
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002113 xennet_disconnect_backend(info);
2114
Ian Campbell6bc96d02012-06-25 22:48:41 +00002115 unregister_netdev(info->netdev);
2116
Andrew J. Bennieston2688fcb2014-06-04 10:30:44 +01002117 for (i = 0; i < num_queues; ++i) {
2118 queue = &info->queues[i];
2119 del_timer_sync(&queue->rx_refill_timer);
2120 }
2121
2122 if (num_queues) {
2123 kfree(info->queues);
2124 info->queues = NULL;
2125 }
Ian Campbell6bc96d02012-06-25 22:48:41 +00002126
David Vrabel900e1832015-01-13 16:42:42 +00002127 xennet_free_netdev(info->netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002128
2129 return 0;
2130}
2131
David Vrabel95afae42014-09-08 17:30:41 +01002132static const struct xenbus_device_id netfront_ids[] = {
2133 { "vif" },
2134 { "" }
2135};
2136
2137static struct xenbus_driver netfront_driver = {
2138 .ids = netfront_ids,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002139 .probe = netfront_probe,
Bill Pemberton8e0e46b2012-12-03 09:24:22 -05002140 .remove = xennet_remove,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002141 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00002142 .otherend_changed = netback_changed,
David Vrabel95afae42014-09-08 17:30:41 +01002143};
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002144
2145static int __init netif_init(void)
2146{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002147 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002148 return -ENODEV;
2149
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002150 if (!xen_has_pv_nic_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002151 return -ENODEV;
2152
Joe Perches383eda32013-06-27 21:57:49 -07002153 pr_info("Initialising Xen virtual ethernet driver\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002154
Andrew J. Bennieston50ee6062014-06-04 10:30:45 +01002155 /* Allow as many queues as there are CPUs, by default */
2156 xennet_max_queues = num_online_cpus();
2157
Al Viroffb78a22008-11-22 17:38:14 +00002158 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002159}
2160module_init(netif_init);
2161
2162
2163static void __exit netif_exit(void)
2164{
Al Viroffb78a22008-11-22 17:38:14 +00002165 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07002166}
2167module_exit(netif_exit);
2168
2169MODULE_DESCRIPTION("Xen virtual network device frontend");
2170MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002171MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002172MODULE_ALIAS("xennet");