]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/xen-netfront.c
xen/netfront: disentangle tx_skb_freelist
[mirror_ubuntu-jammy-kernel.git] / drivers / net / xen-netfront.c
CommitLineData
0d160211
JF
1/*
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
31
383eda32
JP
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
0d160211
JF
34#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/netdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/skbuff.h>
39#include <linux/ethtool.h>
40#include <linux/if_ether.h>
9ecd1a75 41#include <net/tcp.h>
0d160211
JF
42#include <linux/udp.h>
43#include <linux/moduleparam.h>
44#include <linux/mm.h>
5a0e3ad6 45#include <linux/slab.h>
0d160211 46#include <net/ip.h>
6c5aa6fc
DK
47#include <linux/bpf.h>
48#include <net/page_pool.h>
49#include <linux/bpf_trace.h>
0d160211 50
1ccbf534 51#include <xen/xen.h>
0d160211
JF
52#include <xen/xenbus.h>
53#include <xen/events.h>
54#include <xen/page.h>
b9136d20 55#include <xen/platform_pci.h>
0d160211
JF
56#include <xen/grant_table.h>
57
58#include <xen/interface/io/netif.h>
59#include <xen/interface/memory.h>
60#include <xen/interface/grant_table.h>
61
50ee6061 62/* Module parameters */
034702a6 63#define MAX_QUEUES_DEFAULT 8
50ee6061
AB
64static unsigned int xennet_max_queues;
65module_param_named(max_queues, xennet_max_queues, uint, 0644);
66MODULE_PARM_DESC(max_queues,
67 "Maximum number of queues per virtual interface");
68
c2c63310
AR
69#define XENNET_TIMEOUT (5 * HZ)
70
0fc0b732 71static const struct ethtool_ops xennet_ethtool_ops;
0d160211
JF
72
73struct netfront_cb {
3683243b 74 int pull_to;
0d160211
JF
75};
76
77#define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
78
79#define RX_COPY_THRESHOLD 256
80
81#define GRANT_INVALID_REF 0
82
30c5d7f0
JG
83#define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
84#define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
1f3c2eba
DV
85
86/* Minimum number of Rx slots (includes slot for GSO metadata). */
87#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
0d160211 88
2688fcb7
AB
89/* Queue name is interface name with "-qNNN" appended */
90#define QUEUE_NAME_SIZE (IFNAMSIZ + 6)
91
92/* IRQ name is queue name with "-tx" or "-rx" appended */
93#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
94
8edfe2e9 95static DECLARE_WAIT_QUEUE_HEAD(module_wq);
5b5971df 96
e00f85be 97struct netfront_stats {
900e1833
DV
98 u64 packets;
99 u64 bytes;
e00f85be 100 struct u64_stats_sync syncp;
101};
102
2688fcb7
AB
103struct netfront_info;
104
105struct netfront_queue {
106 unsigned int id; /* Queue ID, 0-based */
107 char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
108 struct netfront_info *info;
0d160211 109
6c5aa6fc
DK
110 struct bpf_prog __rcu *xdp_prog;
111
bea3348e 112 struct napi_struct napi;
0d160211 113
d634bf2c
WL
114 /* Split event channels support, tx_* == rx_* when using
115 * single event channel.
116 */
117 unsigned int tx_evtchn, rx_evtchn;
118 unsigned int tx_irq, rx_irq;
119 /* Only used when split event channels support is enabled */
2688fcb7
AB
120 char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
121 char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
0d160211 122
84284d3c
JF
123 spinlock_t tx_lock;
124 struct xen_netif_tx_front_ring tx;
125 int tx_ring_ref;
0d160211
JF
126
127 /*
128 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
21631d2d 129 * are linked from tx_skb_freelist through tx_link.
0d160211 130 */
21631d2d
JG
131 struct sk_buff *tx_skbs[NET_TX_RING_SIZE];
132 unsigned short tx_link[NET_TX_RING_SIZE];
133#define TX_LINK_NONE 0xffff
0d160211
JF
134 grant_ref_t gref_tx_head;
135 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
cefe0078 136 struct page *grant_tx_page[NET_TX_RING_SIZE];
0d160211
JF
137 unsigned tx_skb_freelist;
138
84284d3c
JF
139 spinlock_t rx_lock ____cacheline_aligned_in_smp;
140 struct xen_netif_rx_front_ring rx;
141 int rx_ring_ref;
142
84284d3c
JF
143 struct timer_list rx_refill_timer;
144
0d160211
JF
145 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
146 grant_ref_t gref_rx_head;
147 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
6c5aa6fc
DK
148
149 struct page_pool *page_pool;
150 struct xdp_rxq_info xdp_rxq;
2688fcb7
AB
151};
152
153struct netfront_info {
154 struct list_head list;
155 struct net_device *netdev;
156
157 struct xenbus_device *xbdev;
158
159 /* Multi-queue support */
160 struct netfront_queue *queues;
e0ce4af9
IC
161
162 /* Statistics */
900e1833
DV
163 struct netfront_stats __percpu *rx_stats;
164 struct netfront_stats __percpu *tx_stats;
e00f85be 165
6c5aa6fc
DK
166 /* XDP state */
167 bool netback_has_xdp_headroom;
168 bool netfront_xdp_enabled;
169
2688fcb7 170 atomic_t rx_gso_checksum_fixup;
0d160211
JF
171};
172
173struct netfront_rx_info {
174 struct xen_netif_rx_response rx;
175 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
176};
177
178/*
179 * Access macros for acquiring freeing slots in tx_skbs[].
180 */
181
21631d2d
JG
182static void add_id_to_list(unsigned *head, unsigned short *list,
183 unsigned short id)
0d160211 184{
21631d2d 185 list[id] = *head;
0d160211
JF
186 *head = id;
187}
188
21631d2d 189static unsigned short get_id_from_list(unsigned *head, unsigned short *list)
0d160211
JF
190{
191 unsigned int id = *head;
21631d2d
JG
192
193 if (id != TX_LINK_NONE) {
194 *head = list[id];
195 list[id] = TX_LINK_NONE;
196 }
0d160211
JF
197 return id;
198}
199
200static int xennet_rxidx(RING_IDX idx)
201{
202 return idx & (NET_RX_RING_SIZE - 1);
203}
204
2688fcb7 205static struct sk_buff *xennet_get_rx_skb(struct netfront_queue *queue,
0d160211
JF
206 RING_IDX ri)
207{
208 int i = xennet_rxidx(ri);
2688fcb7
AB
209 struct sk_buff *skb = queue->rx_skbs[i];
210 queue->rx_skbs[i] = NULL;
0d160211
JF
211 return skb;
212}
213
2688fcb7 214static grant_ref_t xennet_get_rx_ref(struct netfront_queue *queue,
0d160211
JF
215 RING_IDX ri)
216{
217 int i = xennet_rxidx(ri);
2688fcb7
AB
218 grant_ref_t ref = queue->grant_rx_ref[i];
219 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
0d160211
JF
220 return ref;
221}
222
223#ifdef CONFIG_SYSFS
27b917e5 224static const struct attribute_group xennet_dev_group;
0d160211
JF
225#endif
226
3ad9b358 227static bool xennet_can_sg(struct net_device *dev)
0d160211 228{
3ad9b358 229 return dev->features & NETIF_F_SG;
0d160211
JF
230}
231
232
e99e88a9 233static void rx_refill_timeout(struct timer_list *t)
0d160211 234{
e99e88a9 235 struct netfront_queue *queue = from_timer(queue, t, rx_refill_timer);
2688fcb7 236 napi_schedule(&queue->napi);
0d160211
JF
237}
238
2688fcb7 239static int netfront_tx_slot_available(struct netfront_queue *queue)
0d160211 240{
2688fcb7 241 return (queue->tx.req_prod_pvt - queue->tx.rsp_cons) <
57f230ab 242 (NET_TX_RING_SIZE - XEN_NETIF_NR_SLOTS_MIN - 1);
0d160211
JF
243}
244
2688fcb7 245static void xennet_maybe_wake_tx(struct netfront_queue *queue)
0d160211 246{
2688fcb7
AB
247 struct net_device *dev = queue->info->netdev;
248 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, queue->id);
0d160211 249
2688fcb7
AB
250 if (unlikely(netif_tx_queue_stopped(dev_queue)) &&
251 netfront_tx_slot_available(queue) &&
0d160211 252 likely(netif_running(dev)))
2688fcb7 253 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
0d160211
JF
254}
255
1f3c2eba
DV
256
257static struct sk_buff *xennet_alloc_one_rx_buffer(struct netfront_queue *queue)
0d160211 258{
0d160211
JF
259 struct sk_buff *skb;
260 struct page *page;
0d160211 261
1f3c2eba
DV
262 skb = __netdev_alloc_skb(queue->info->netdev,
263 RX_COPY_THRESHOLD + NET_IP_ALIGN,
264 GFP_ATOMIC | __GFP_NOWARN);
265 if (unlikely(!skb))
266 return NULL;
0d160211 267
6c5aa6fc
DK
268 page = page_pool_dev_alloc_pages(queue->page_pool);
269 if (unlikely(!page)) {
1f3c2eba
DV
270 kfree_skb(skb);
271 return NULL;
0d160211 272 }
1f3c2eba
DV
273 skb_add_rx_frag(skb, 0, page, 0, 0, PAGE_SIZE);
274
275 /* Align ip header to a 16 bytes boundary */
276 skb_reserve(skb, NET_IP_ALIGN);
277 skb->dev = queue->info->netdev;
278
279 return skb;
280}
0d160211 281
1f3c2eba
DV
282
283static void xennet_alloc_rx_buffers(struct netfront_queue *queue)
284{
285 RING_IDX req_prod = queue->rx.req_prod_pvt;
286 int notify;
538d9291 287 int err = 0;
1f3c2eba
DV
288
289 if (unlikely(!netif_carrier_ok(queue->info->netdev)))
0d160211 290 return;
0d160211 291
1f3c2eba
DV
292 for (req_prod = queue->rx.req_prod_pvt;
293 req_prod - queue->rx.rsp_cons < NET_RX_RING_SIZE;
294 req_prod++) {
295 struct sk_buff *skb;
296 unsigned short id;
297 grant_ref_t ref;
30c5d7f0 298 struct page *page;
1f3c2eba 299 struct xen_netif_rx_request *req;
0d160211 300
1f3c2eba 301 skb = xennet_alloc_one_rx_buffer(queue);
538d9291
VRP
302 if (!skb) {
303 err = -ENOMEM;
0d160211 304 break;
538d9291 305 }
0d160211 306
1f3c2eba 307 id = xennet_rxidx(req_prod);
0d160211 308
2688fcb7
AB
309 BUG_ON(queue->rx_skbs[id]);
310 queue->rx_skbs[id] = skb;
0d160211 311
2688fcb7 312 ref = gnttab_claim_grant_reference(&queue->gref_rx_head);
269ebce4 313 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
2688fcb7 314 queue->grant_rx_ref[id] = ref;
0d160211 315
30c5d7f0 316 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
0d160211 317
1f3c2eba 318 req = RING_GET_REQUEST(&queue->rx, req_prod);
30c5d7f0
JG
319 gnttab_page_grant_foreign_access_ref_one(ref,
320 queue->info->xbdev->otherend_id,
321 page,
322 0);
0d160211
JF
323 req->id = id;
324 req->gref = ref;
325 }
326
1f3c2eba
DV
327 queue->rx.req_prod_pvt = req_prod;
328
538d9291
VRP
329 /* Try again later if there are not enough requests or skb allocation
330 * failed.
331 * Enough requests is quantified as the sum of newly created slots and
332 * the unconsumed slots at the backend.
333 */
334 if (req_prod - queue->rx.rsp_cons < NET_RX_SLOTS_MIN ||
335 unlikely(err)) {
1f3c2eba
DV
336 mod_timer(&queue->rx_refill_timer, jiffies + (HZ/10));
337 return;
338 }
339
2688fcb7 340 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->rx, notify);
0d160211 341 if (notify)
2688fcb7 342 notify_remote_via_irq(queue->rx_irq);
0d160211
JF
343}
344
345static int xennet_open(struct net_device *dev)
346{
347 struct netfront_info *np = netdev_priv(dev);
2688fcb7
AB
348 unsigned int num_queues = dev->real_num_tx_queues;
349 unsigned int i = 0;
350 struct netfront_queue *queue = NULL;
351
f599c64f
RL
352 if (!np->queues)
353 return -ENODEV;
354
2688fcb7
AB
355 for (i = 0; i < num_queues; ++i) {
356 queue = &np->queues[i];
357 napi_enable(&queue->napi);
358
359 spin_lock_bh(&queue->rx_lock);
360 if (netif_carrier_ok(dev)) {
361 xennet_alloc_rx_buffers(queue);
362 queue->rx.sring->rsp_event = queue->rx.rsp_cons + 1;
363 if (RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))
364 napi_schedule(&queue->napi);
365 }
366 spin_unlock_bh(&queue->rx_lock);
0d160211 367 }
0d160211 368
2688fcb7 369 netif_tx_start_all_queues(dev);
0d160211
JF
370
371 return 0;
372}
373
2688fcb7 374static void xennet_tx_buf_gc(struct netfront_queue *queue)
0d160211
JF
375{
376 RING_IDX cons, prod;
377 unsigned short id;
0d160211 378 struct sk_buff *skb;
7d0105b5 379 bool more_to_do;
0d160211 380
2688fcb7 381 BUG_ON(!netif_carrier_ok(queue->info->netdev));
0d160211
JF
382
383 do {
2688fcb7 384 prod = queue->tx.sring->rsp_prod;
0d160211
JF
385 rmb(); /* Ensure we see responses up to 'rp'. */
386
2688fcb7 387 for (cons = queue->tx.rsp_cons; cons != prod; cons++) {
8446066b 388 struct xen_netif_tx_response txrsp;
0d160211 389
8446066b
JG
390 RING_COPY_RESPONSE(&queue->tx, cons, &txrsp);
391 if (txrsp.status == XEN_NETIF_RSP_NULL)
0d160211
JF
392 continue;
393
8446066b 394 id = txrsp.id;
21631d2d
JG
395 skb = queue->tx_skbs[id];
396 queue->tx_skbs[id] = NULL;
0d160211 397 if (unlikely(gnttab_query_foreign_access(
2688fcb7 398 queue->grant_tx_ref[id]) != 0)) {
383eda32
JP
399 pr_alert("%s: warning -- grant still in use by backend domain\n",
400 __func__);
0d160211
JF
401 BUG();
402 }
403 gnttab_end_foreign_access_ref(
2688fcb7 404 queue->grant_tx_ref[id], GNTMAP_readonly);
0d160211 405 gnttab_release_grant_reference(
2688fcb7
AB
406 &queue->gref_tx_head, queue->grant_tx_ref[id]);
407 queue->grant_tx_ref[id] = GRANT_INVALID_REF;
408 queue->grant_tx_page[id] = NULL;
21631d2d 409 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id);
0d160211
JF
410 dev_kfree_skb_irq(skb);
411 }
412
2688fcb7 413 queue->tx.rsp_cons = prod;
0d160211 414
7d0105b5
MC
415 RING_FINAL_CHECK_FOR_RESPONSES(&queue->tx, more_to_do);
416 } while (more_to_do);
0d160211 417
2688fcb7 418 xennet_maybe_wake_tx(queue);
0d160211
JF
419}
420
30c5d7f0
JG
421struct xennet_gnttab_make_txreq {
422 struct netfront_queue *queue;
423 struct sk_buff *skb;
424 struct page *page;
162081ec
JG
425 struct xen_netif_tx_request *tx; /* Last request on ring page */
426 struct xen_netif_tx_request tx_local; /* Last request local copy*/
30c5d7f0
JG
427 unsigned int size;
428};
429
430static void xennet_tx_setup_grant(unsigned long gfn, unsigned int offset,
431 unsigned int len, void *data)
0d160211 432{
30c5d7f0 433 struct xennet_gnttab_make_txreq *info = data;
0d160211 434 unsigned int id;
a55e8bb8 435 struct xen_netif_tx_request *tx;
0d160211 436 grant_ref_t ref;
30c5d7f0
JG
437 /* convenient aliases */
438 struct page *page = info->page;
439 struct netfront_queue *queue = info->queue;
440 struct sk_buff *skb = info->skb;
0d160211 441
21631d2d 442 id = get_id_from_list(&queue->tx_skb_freelist, queue->tx_link);
a55e8bb8
DV
443 tx = RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
444 ref = gnttab_claim_grant_reference(&queue->gref_tx_head);
269ebce4 445 WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref));
0d160211 446
30c5d7f0
JG
447 gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id,
448 gfn, GNTMAP_readonly);
0d160211 449
21631d2d 450 queue->tx_skbs[id] = skb;
a55e8bb8
DV
451 queue->grant_tx_page[id] = page;
452 queue->grant_tx_ref[id] = ref;
0d160211 453
162081ec
JG
454 info->tx_local.id = id;
455 info->tx_local.gref = ref;
456 info->tx_local.offset = offset;
457 info->tx_local.size = len;
458 info->tx_local.flags = 0;
459
460 *tx = info->tx_local;
0d160211 461
30c5d7f0 462 info->tx = tx;
162081ec 463 info->size += info->tx_local.size;
30c5d7f0
JG
464}
465
466static struct xen_netif_tx_request *xennet_make_first_txreq(
162081ec
JG
467 struct xennet_gnttab_make_txreq *info,
468 unsigned int offset, unsigned int len)
30c5d7f0 469{
162081ec 470 info->size = 0;
30c5d7f0 471
162081ec 472 gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info);
30c5d7f0 473
162081ec 474 return info->tx;
30c5d7f0
JG
475}
476
477static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset,
478 unsigned int len, void *data)
479{
480 struct xennet_gnttab_make_txreq *info = data;
481
482 info->tx->flags |= XEN_NETTXF_more_data;
483 skb_get(info->skb);
484 xennet_tx_setup_grant(gfn, offset, len, data);
a55e8bb8 485}
0d160211 486
162081ec
JG
487static void xennet_make_txreqs(
488 struct xennet_gnttab_make_txreq *info,
489 struct page *page,
a55e8bb8
DV
490 unsigned int offset, unsigned int len)
491{
492 /* Skip unused frames from start of page */
493 page += offset >> PAGE_SHIFT;
494 offset &= ~PAGE_MASK;
0d160211 495
a55e8bb8 496 while (len) {
162081ec
JG
497 info->page = page;
498 info->size = 0;
30c5d7f0
JG
499
500 gnttab_foreach_grant_in_range(page, offset, len,
501 xennet_make_one_txreq,
162081ec 502 info);
30c5d7f0 503
a55e8bb8
DV
504 page++;
505 offset = 0;
162081ec 506 len -= info->size;
0d160211 507 }
0d160211
JF
508}
509
f36c3747 510/*
e84448d5
DV
511 * Count how many ring slots are required to send this skb. Each frag
512 * might be a compound page.
f36c3747 513 */
e84448d5 514static int xennet_count_skb_slots(struct sk_buff *skb)
f36c3747
IC
515{
516 int i, frags = skb_shinfo(skb)->nr_frags;
30c5d7f0 517 int slots;
e84448d5 518
30c5d7f0
JG
519 slots = gnttab_count_grant(offset_in_page(skb->data),
520 skb_headlen(skb));
f36c3747
IC
521
522 for (i = 0; i < frags; i++) {
523 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
524 unsigned long size = skb_frag_size(frag);
b54c9d5b 525 unsigned long offset = skb_frag_off(frag);
f36c3747
IC
526
527 /* Skip unused frames from start of page */
528 offset &= ~PAGE_MASK;
529
30c5d7f0 530 slots += gnttab_count_grant(offset, size);
f36c3747
IC
531 }
532
30c5d7f0 533 return slots;
f36c3747
IC
534}
535
50ee6061 536static u16 xennet_select_queue(struct net_device *dev, struct sk_buff *skb,
a350ecce 537 struct net_device *sb_dev)
2688fcb7 538{
50ee6061
AB
539 unsigned int num_queues = dev->real_num_tx_queues;
540 u32 hash;
541 u16 queue_idx;
542
543 /* First, check if there is only one queue */
544 if (num_queues == 1) {
545 queue_idx = 0;
546 } else {
547 hash = skb_get_hash(skb);
548 queue_idx = hash % num_queues;
549 }
550
551 return queue_idx;
2688fcb7
AB
552}
553
6c5aa6fc
DK
554static int xennet_xdp_xmit_one(struct net_device *dev,
555 struct netfront_queue *queue,
556 struct xdp_frame *xdpf)
557{
558 struct netfront_info *np = netdev_priv(dev);
559 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
162081ec
JG
560 struct xennet_gnttab_make_txreq info = {
561 .queue = queue,
562 .skb = NULL,
563 .page = virt_to_page(xdpf->data),
564 };
6c5aa6fc
DK
565 int notify;
566
162081ec 567 xennet_make_first_txreq(&info,
6c5aa6fc
DK
568 offset_in_page(xdpf->data),
569 xdpf->len);
570
571 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
572 if (notify)
573 notify_remote_via_irq(queue->tx_irq);
574
575 u64_stats_update_begin(&tx_stats->syncp);
576 tx_stats->bytes += xdpf->len;
577 tx_stats->packets++;
578 u64_stats_update_end(&tx_stats->syncp);
579
580 xennet_tx_buf_gc(queue);
581
582 return 0;
583}
584
585static int xennet_xdp_xmit(struct net_device *dev, int n,
586 struct xdp_frame **frames, u32 flags)
587{
588 unsigned int num_queues = dev->real_num_tx_queues;
589 struct netfront_info *np = netdev_priv(dev);
590 struct netfront_queue *queue = NULL;
591 unsigned long irq_flags;
fdc13979
LB
592 int nxmit = 0;
593 int i;
6c5aa6fc
DK
594
595 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
596 return -EINVAL;
597
598 queue = &np->queues[smp_processor_id() % num_queues];
599
600 spin_lock_irqsave(&queue->tx_lock, irq_flags);
601 for (i = 0; i < n; i++) {
602 struct xdp_frame *xdpf = frames[i];
603
604 if (!xdpf)
605 continue;
fdc13979
LB
606 if (xennet_xdp_xmit_one(dev, queue, xdpf))
607 break;
608 nxmit++;
6c5aa6fc
DK
609 }
610 spin_unlock_irqrestore(&queue->tx_lock, irq_flags);
611
fdc13979 612 return nxmit;
6c5aa6fc
DK
613}
614
615
30c5d7f0
JG
616#define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
617
24a94b3c 618static netdev_tx_t xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
0d160211 619{
0d160211 620 struct netfront_info *np = netdev_priv(dev);
900e1833 621 struct netfront_stats *tx_stats = this_cpu_ptr(np->tx_stats);
162081ec 622 struct xen_netif_tx_request *first_tx;
a55e8bb8 623 unsigned int i;
0d160211 624 int notify;
f36c3747 625 int slots;
a55e8bb8
DV
626 struct page *page;
627 unsigned int offset;
628 unsigned int len;
cf66f9d4 629 unsigned long flags;
2688fcb7 630 struct netfront_queue *queue = NULL;
162081ec 631 struct xennet_gnttab_make_txreq info = { };
2688fcb7
AB
632 unsigned int num_queues = dev->real_num_tx_queues;
633 u16 queue_index;
fd07160b 634 struct sk_buff *nskb;
2688fcb7
AB
635
636 /* Drop the packet if no queues are set up */
637 if (num_queues < 1)
638 goto drop;
639 /* Determine which queue to transmit this SKB on */
640 queue_index = skb_get_queue_mapping(skb);
641 queue = &np->queues[queue_index];
0d160211 642
9ecd1a75
WL
643 /* If skb->len is too big for wire format, drop skb and alert
644 * user about misconfiguration.
645 */
646 if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
647 net_alert_ratelimited(
648 "xennet: skb->len = %u, too big for wire format\n",
649 skb->len);
650 goto drop;
651 }
652
e84448d5 653 slots = xennet_count_skb_slots(skb);
30c5d7f0 654 if (unlikely(slots > MAX_XEN_SKB_FRAGS + 1)) {
97a6d1bb
ZK
655 net_dbg_ratelimited("xennet: skb rides the rocket: %d slots, %d bytes\n",
656 slots, skb->len);
657 if (skb_linearize(skb))
658 goto drop;
0d160211
JF
659 }
660
a55e8bb8
DV
661 page = virt_to_page(skb->data);
662 offset = offset_in_page(skb->data);
fd07160b
VK
663
664 /* The first req should be at least ETH_HLEN size or the packet will be
665 * dropped by netback.
666 */
667 if (unlikely(PAGE_SIZE - offset < ETH_HLEN)) {
668 nskb = skb_copy(skb, GFP_ATOMIC);
669 if (!nskb)
670 goto drop;
62f3250f 671 dev_consume_skb_any(skb);
fd07160b
VK
672 skb = nskb;
673 page = virt_to_page(skb->data);
674 offset = offset_in_page(skb->data);
675 }
676
a55e8bb8
DV
677 len = skb_headlen(skb);
678
2688fcb7 679 spin_lock_irqsave(&queue->tx_lock, flags);
0d160211
JF
680
681 if (unlikely(!netif_carrier_ok(dev) ||
f36c3747 682 (slots > 1 && !xennet_can_sg(dev)) ||
8b86a61d 683 netif_needs_gso(skb, netif_skb_features(skb)))) {
2688fcb7 684 spin_unlock_irqrestore(&queue->tx_lock, flags);
0d160211
JF
685 goto drop;
686 }
687
a55e8bb8 688 /* First request for the linear area. */
162081ec
JG
689 info.queue = queue;
690 info.skb = skb;
691 info.page = page;
692 first_tx = xennet_make_first_txreq(&info, offset, len);
693 offset += info.tx_local.size;
30c5d7f0
JG
694 if (offset == PAGE_SIZE) {
695 page++;
696 offset = 0;
697 }
162081ec 698 len -= info.tx_local.size;
0d160211 699
0d160211
JF
700 if (skb->ip_summed == CHECKSUM_PARTIAL)
701 /* local packet? */
162081ec
JG
702 first_tx->flags |= XEN_NETTXF_csum_blank |
703 XEN_NETTXF_data_validated;
0d160211
JF
704 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
705 /* remote but checksummed. */
162081ec 706 first_tx->flags |= XEN_NETTXF_data_validated;
0d160211 707
a55e8bb8 708 /* Optional extra info after the first request. */
0d160211
JF
709 if (skb_shinfo(skb)->gso_size) {
710 struct xen_netif_extra_info *gso;
711
712 gso = (struct xen_netif_extra_info *)
a55e8bb8 713 RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++);
0d160211 714
162081ec 715 first_tx->flags |= XEN_NETTXF_extra_info;
0d160211
JF
716
717 gso->u.gso.size = skb_shinfo(skb)->gso_size;
2c0057de
PD
718 gso->u.gso.type = (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ?
719 XEN_NETIF_GSO_TYPE_TCPV6 :
720 XEN_NETIF_GSO_TYPE_TCPV4;
0d160211
JF
721 gso->u.gso.pad = 0;
722 gso->u.gso.features = 0;
723
724 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
725 gso->flags = 0;
0d160211
JF
726 }
727
a55e8bb8 728 /* Requests for the rest of the linear area. */
162081ec 729 xennet_make_txreqs(&info, page, offset, len);
a55e8bb8
DV
730
731 /* Requests for all the frags. */
732 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
733 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
162081ec 734 xennet_make_txreqs(&info, skb_frag_page(frag),
b54c9d5b 735 skb_frag_off(frag),
a55e8bb8
DV
736 skb_frag_size(frag));
737 }
0d160211 738
a55e8bb8
DV
739 /* First request has the packet length. */
740 first_tx->size = skb->len;
0d160211 741
91ffb9d3
DD
742 /* timestamp packet in software */
743 skb_tx_timestamp(skb);
744
2688fcb7 745 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify);
0d160211 746 if (notify)
2688fcb7 747 notify_remote_via_irq(queue->tx_irq);
0d160211 748
900e1833
DV
749 u64_stats_update_begin(&tx_stats->syncp);
750 tx_stats->bytes += skb->len;
751 tx_stats->packets++;
752 u64_stats_update_end(&tx_stats->syncp);
10a273a6
JF
753
754 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
2688fcb7 755 xennet_tx_buf_gc(queue);
0d160211 756
2688fcb7
AB
757 if (!netfront_tx_slot_available(queue))
758 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
0d160211 759
2688fcb7 760 spin_unlock_irqrestore(&queue->tx_lock, flags);
0d160211 761
6ed10654 762 return NETDEV_TX_OK;
0d160211
JF
763
764 drop:
09f75cd7 765 dev->stats.tx_dropped++;
979de8a0 766 dev_kfree_skb_any(skb);
6ed10654 767 return NETDEV_TX_OK;
0d160211
JF
768}
769
770static int xennet_close(struct net_device *dev)
771{
772 struct netfront_info *np = netdev_priv(dev);
2688fcb7
AB
773 unsigned int num_queues = dev->real_num_tx_queues;
774 unsigned int i;
775 struct netfront_queue *queue;
776 netif_tx_stop_all_queues(np->netdev);
777 for (i = 0; i < num_queues; ++i) {
778 queue = &np->queues[i];
779 napi_disable(&queue->napi);
780 }
0d160211
JF
781 return 0;
782}
783
2688fcb7 784static void xennet_move_rx_slot(struct netfront_queue *queue, struct sk_buff *skb,
0d160211
JF
785 grant_ref_t ref)
786{
2688fcb7
AB
787 int new = xennet_rxidx(queue->rx.req_prod_pvt);
788
789 BUG_ON(queue->rx_skbs[new]);
790 queue->rx_skbs[new] = skb;
791 queue->grant_rx_ref[new] = ref;
792 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->id = new;
793 RING_GET_REQUEST(&queue->rx, queue->rx.req_prod_pvt)->gref = ref;
794 queue->rx.req_prod_pvt++;
0d160211
JF
795}
796
2688fcb7 797static int xennet_get_extras(struct netfront_queue *queue,
0d160211
JF
798 struct xen_netif_extra_info *extras,
799 RING_IDX rp)
800
801{
8446066b 802 struct xen_netif_extra_info extra;
2688fcb7
AB
803 struct device *dev = &queue->info->netdev->dev;
804 RING_IDX cons = queue->rx.rsp_cons;
0d160211
JF
805 int err = 0;
806
807 do {
808 struct sk_buff *skb;
809 grant_ref_t ref;
810
811 if (unlikely(cons + 1 == rp)) {
812 if (net_ratelimit())
813 dev_warn(dev, "Missing extra info\n");
814 err = -EBADR;
815 break;
816 }
817
8446066b 818 RING_COPY_RESPONSE(&queue->rx, ++cons, &extra);
0d160211 819
8446066b
JG
820 if (unlikely(!extra.type ||
821 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
0d160211
JF
822 if (net_ratelimit())
823 dev_warn(dev, "Invalid extra type: %d\n",
8446066b 824 extra.type);
0d160211
JF
825 err = -EINVAL;
826 } else {
8446066b 827 extras[extra.type - 1] = extra;
0d160211
JF
828 }
829
2688fcb7
AB
830 skb = xennet_get_rx_skb(queue, cons);
831 ref = xennet_get_rx_ref(queue, cons);
832 xennet_move_rx_slot(queue, skb, ref);
8446066b 833 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
0d160211 834
2688fcb7 835 queue->rx.rsp_cons = cons;
0d160211
JF
836 return err;
837}
838
6c5aa6fc
DK
839static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
840 struct xen_netif_rx_response *rx, struct bpf_prog *prog,
841 struct xdp_buff *xdp, bool *need_xdp_flush)
842{
843 struct xdp_frame *xdpf;
844 u32 len = rx->status;
e44f65fd 845 u32 act;
6c5aa6fc
DK
846 int err;
847
43b5169d
LB
848 xdp_init_buff(xdp, XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
849 &queue->xdp_rxq);
be9df4af
LB
850 xdp_prepare_buff(xdp, page_address(pdata), XDP_PACKET_HEADROOM,
851 len, false);
6c5aa6fc
DK
852
853 act = bpf_prog_run_xdp(prog, xdp);
854 switch (act) {
855 case XDP_TX:
856 get_page(pdata);
857 xdpf = xdp_convert_buff_to_frame(xdp);
858 err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
fdc13979
LB
859 if (unlikely(!err))
860 xdp_return_frame_rx_napi(xdpf);
861 else if (unlikely(err < 0))
6c5aa6fc
DK
862 trace_xdp_exception(queue->info->netdev, prog, act);
863 break;
864 case XDP_REDIRECT:
865 get_page(pdata);
866 err = xdp_do_redirect(queue->info->netdev, xdp, prog);
867 *need_xdp_flush = true;
868 if (unlikely(err))
869 trace_xdp_exception(queue->info->netdev, prog, act);
870 break;
871 case XDP_PASS:
872 case XDP_DROP:
873 break;
874
875 case XDP_ABORTED:
876 trace_xdp_exception(queue->info->netdev, prog, act);
877 break;
878
879 default:
880 bpf_warn_invalid_xdp_action(act);
881 }
882
883 return act;
884}
885
2688fcb7 886static int xennet_get_responses(struct netfront_queue *queue,
0d160211 887 struct netfront_rx_info *rinfo, RING_IDX rp,
6c5aa6fc
DK
888 struct sk_buff_head *list,
889 bool *need_xdp_flush)
0d160211 890{
8446066b 891 struct xen_netif_rx_response *rx = &rinfo->rx, rx_local;
6c5aa6fc 892 int max = XEN_NETIF_NR_SLOTS_MIN + (rx->status <= RX_COPY_THRESHOLD);
2688fcb7
AB
893 RING_IDX cons = queue->rx.rsp_cons;
894 struct sk_buff *skb = xennet_get_rx_skb(queue, cons);
6c5aa6fc 895 struct xen_netif_extra_info *extras = rinfo->extras;
2688fcb7 896 grant_ref_t ref = xennet_get_rx_ref(queue, cons);
6c5aa6fc
DK
897 struct device *dev = &queue->info->netdev->dev;
898 struct bpf_prog *xdp_prog;
899 struct xdp_buff xdp;
900 unsigned long ret;
7158ff6d 901 int slots = 1;
0d160211 902 int err = 0;
6c5aa6fc 903 u32 verdict;
0d160211 904
f942dc25 905 if (rx->flags & XEN_NETRXF_extra_info) {
2688fcb7 906 err = xennet_get_extras(queue, extras, rp);
6c5aa6fc
DK
907 if (!err) {
908 if (extras[XEN_NETIF_EXTRA_TYPE_XDP - 1].type) {
909 struct xen_netif_extra_info *xdp;
910
911 xdp = &extras[XEN_NETIF_EXTRA_TYPE_XDP - 1];
912 rx->offset = xdp->u.xdp.headroom;
913 }
914 }
2688fcb7 915 cons = queue->rx.rsp_cons;
0d160211
JF
916 }
917
918 for (;;) {
919 if (unlikely(rx->status < 0 ||
30c5d7f0 920 rx->offset + rx->status > XEN_PAGE_SIZE)) {
0d160211 921 if (net_ratelimit())
6c10127d 922 dev_warn(dev, "rx->offset: %u, size: %d\n",
0d160211 923 rx->offset, rx->status);
2688fcb7 924 xennet_move_rx_slot(queue, skb, ref);
0d160211
JF
925 err = -EINVAL;
926 goto next;
927 }
928
929 /*
930 * This definitely indicates a bug, either in this driver or in
931 * the backend driver. In future this should flag the bad
697089dc 932 * situation to the system controller to reboot the backend.
0d160211
JF
933 */
934 if (ref == GRANT_INVALID_REF) {
935 if (net_ratelimit())
936 dev_warn(dev, "Bad rx response id %d.\n",
937 rx->id);
938 err = -EINVAL;
939 goto next;
940 }
941
942 ret = gnttab_end_foreign_access_ref(ref, 0);
943 BUG_ON(!ret);
944
2688fcb7 945 gnttab_release_grant_reference(&queue->gref_rx_head, ref);
0d160211 946
6c5aa6fc
DK
947 rcu_read_lock();
948 xdp_prog = rcu_dereference(queue->xdp_prog);
949 if (xdp_prog) {
950 if (!(rx->flags & XEN_NETRXF_more_data)) {
951 /* currently only a single page contains data */
952 verdict = xennet_run_xdp(queue,
953 skb_frag_page(&skb_shinfo(skb)->frags[0]),
954 rx, xdp_prog, &xdp, need_xdp_flush);
955 if (verdict != XDP_PASS)
956 err = -EINVAL;
957 } else {
958 /* drop the frame */
959 err = -EINVAL;
960 }
961 }
962 rcu_read_unlock();
0d160211 963next:
6c5aa6fc 964 __skb_queue_tail(list, skb);
f942dc25 965 if (!(rx->flags & XEN_NETRXF_more_data))
0d160211
JF
966 break;
967
7158ff6d 968 if (cons + slots == rp) {
0d160211 969 if (net_ratelimit())
7158ff6d 970 dev_warn(dev, "Need more slots\n");
0d160211
JF
971 err = -ENOENT;
972 break;
973 }
974
8446066b
JG
975 RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local);
976 rx = &rx_local;
2688fcb7
AB
977 skb = xennet_get_rx_skb(queue, cons + slots);
978 ref = xennet_get_rx_ref(queue, cons + slots);
7158ff6d 979 slots++;
0d160211
JF
980 }
981
7158ff6d 982 if (unlikely(slots > max)) {
0d160211 983 if (net_ratelimit())
697089dc 984 dev_warn(dev, "Too many slots\n");
0d160211
JF
985 err = -E2BIG;
986 }
987
988 if (unlikely(err))
2688fcb7 989 queue->rx.rsp_cons = cons + slots;
0d160211
JF
990
991 return err;
992}
993
994static int xennet_set_skb_gso(struct sk_buff *skb,
995 struct xen_netif_extra_info *gso)
996{
997 if (!gso->u.gso.size) {
998 if (net_ratelimit())
383eda32 999 pr_warn("GSO size must not be zero\n");
0d160211
JF
1000 return -EINVAL;
1001 }
1002
2c0057de
PD
1003 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4 &&
1004 gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV6) {
0d160211 1005 if (net_ratelimit())
383eda32 1006 pr_warn("Bad GSO type %d\n", gso->u.gso.type);
0d160211
JF
1007 return -EINVAL;
1008 }
1009
1010 skb_shinfo(skb)->gso_size = gso->u.gso.size;
2c0057de
PD
1011 skb_shinfo(skb)->gso_type =
1012 (gso->u.gso.type == XEN_NETIF_GSO_TYPE_TCPV4) ?
1013 SKB_GSO_TCPV4 :
1014 SKB_GSO_TCPV6;
0d160211
JF
1015
1016 /* Header must be checked, and gso_segs computed. */
1017 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1018 skb_shinfo(skb)->gso_segs = 0;
1019
1020 return 0;
1021}
1022
a761129e
DZ
1023static int xennet_fill_frags(struct netfront_queue *queue,
1024 struct sk_buff *skb,
1025 struct sk_buff_head *list)
0d160211 1026{
2688fcb7 1027 RING_IDX cons = queue->rx.rsp_cons;
0d160211
JF
1028 struct sk_buff *nskb;
1029
1030 while ((nskb = __skb_dequeue(list))) {
8446066b 1031 struct xen_netif_rx_response rx;
01c68026 1032 skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
0d160211 1033
8446066b
JG
1034 RING_COPY_RESPONSE(&queue->rx, ++cons, &rx);
1035
d472b3a6 1036 if (skb_shinfo(skb)->nr_frags == MAX_SKB_FRAGS) {
093b9c71 1037 unsigned int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
0d160211 1038
d81c5054 1039 BUG_ON(pull_to < skb_headlen(skb));
093b9c71
JB
1040 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
1041 }
ad4f15dc 1042 if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
00b36850 1043 queue->rx.rsp_cons = ++cons + skb_queue_len(list);
ad4f15dc 1044 kfree_skb(nskb);
a761129e 1045 return -ENOENT;
ad4f15dc 1046 }
093b9c71 1047
d472b3a6
JG
1048 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1049 skb_frag_page(nfrag),
8446066b 1050 rx.offset, rx.status, PAGE_SIZE);
0d160211
JF
1051
1052 skb_shinfo(nskb)->nr_frags = 0;
1053 kfree_skb(nskb);
0d160211
JF
1054 }
1055
a761129e
DZ
1056 queue->rx.rsp_cons = cons;
1057
1058 return 0;
0d160211
JF
1059}
1060
e0ce4af9 1061static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
0d160211 1062{
b5cf66cd 1063 bool recalculate_partial_csum = false;
e0ce4af9
IC
1064
1065 /*
1066 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1067 * peers can fail to set NETRXF_csum_blank when sending a GSO
1068 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1069 * recalculate the partial checksum.
1070 */
1071 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1072 struct netfront_info *np = netdev_priv(dev);
2688fcb7 1073 atomic_inc(&np->rx_gso_checksum_fixup);
e0ce4af9 1074 skb->ip_summed = CHECKSUM_PARTIAL;
b5cf66cd 1075 recalculate_partial_csum = true;
e0ce4af9
IC
1076 }
1077
1078 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1079 if (skb->ip_summed != CHECKSUM_PARTIAL)
1080 return 0;
0d160211 1081
b5cf66cd 1082 return skb_checksum_setup(skb, recalculate_partial_csum);
0d160211
JF
1083}
1084
2688fcb7 1085static int handle_incoming_queue(struct netfront_queue *queue,
09f75cd7 1086 struct sk_buff_head *rxq)
0d160211 1087{
900e1833 1088 struct netfront_stats *rx_stats = this_cpu_ptr(queue->info->rx_stats);
0d160211
JF
1089 int packets_dropped = 0;
1090 struct sk_buff *skb;
1091
1092 while ((skb = __skb_dequeue(rxq)) != NULL) {
3683243b 1093 int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
0d160211 1094
093b9c71
JB
1095 if (pull_to > skb_headlen(skb))
1096 __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
0d160211
JF
1097
1098 /* Ethernet work: Delayed to here as it peeks the header. */
2688fcb7 1099 skb->protocol = eth_type_trans(skb, queue->info->netdev);
d554f73d 1100 skb_reset_network_header(skb);
0d160211 1101
2688fcb7 1102 if (checksum_setup(queue->info->netdev, skb)) {
e0ce4af9
IC
1103 kfree_skb(skb);
1104 packets_dropped++;
2688fcb7 1105 queue->info->netdev->stats.rx_errors++;
e0ce4af9 1106 continue;
0d160211
JF
1107 }
1108
900e1833
DV
1109 u64_stats_update_begin(&rx_stats->syncp);
1110 rx_stats->packets++;
1111 rx_stats->bytes += skb->len;
1112 u64_stats_update_end(&rx_stats->syncp);
0d160211
JF
1113
1114 /* Pass it up. */
2688fcb7 1115 napi_gro_receive(&queue->napi, skb);
0d160211
JF
1116 }
1117
1118 return packets_dropped;
1119}
1120
bea3348e 1121static int xennet_poll(struct napi_struct *napi, int budget)
0d160211 1122{
2688fcb7
AB
1123 struct netfront_queue *queue = container_of(napi, struct netfront_queue, napi);
1124 struct net_device *dev = queue->info->netdev;
0d160211
JF
1125 struct sk_buff *skb;
1126 struct netfront_rx_info rinfo;
1127 struct xen_netif_rx_response *rx = &rinfo.rx;
1128 struct xen_netif_extra_info *extras = rinfo.extras;
1129 RING_IDX i, rp;
bea3348e 1130 int work_done;
0d160211
JF
1131 struct sk_buff_head rxq;
1132 struct sk_buff_head errq;
1133 struct sk_buff_head tmpq;
0d160211 1134 int err;
6c5aa6fc 1135 bool need_xdp_flush = false;
0d160211 1136
2688fcb7 1137 spin_lock(&queue->rx_lock);
0d160211 1138
0d160211
JF
1139 skb_queue_head_init(&rxq);
1140 skb_queue_head_init(&errq);
1141 skb_queue_head_init(&tmpq);
1142
2688fcb7 1143 rp = queue->rx.sring->rsp_prod;
0d160211
JF
1144 rmb(); /* Ensure we see queued responses up to 'rp'. */
1145
2688fcb7 1146 i = queue->rx.rsp_cons;
0d160211
JF
1147 work_done = 0;
1148 while ((i != rp) && (work_done < budget)) {
8446066b 1149 RING_COPY_RESPONSE(&queue->rx, i, rx);
0d160211
JF
1150 memset(extras, 0, sizeof(rinfo.extras));
1151
6c5aa6fc
DK
1152 err = xennet_get_responses(queue, &rinfo, rp, &tmpq,
1153 &need_xdp_flush);
0d160211
JF
1154
1155 if (unlikely(err)) {
1156err:
1157 while ((skb = __skb_dequeue(&tmpq)))
1158 __skb_queue_tail(&errq, skb);
09f75cd7 1159 dev->stats.rx_errors++;
2688fcb7 1160 i = queue->rx.rsp_cons;
0d160211
JF
1161 continue;
1162 }
1163
1164 skb = __skb_dequeue(&tmpq);
1165
1166 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1167 struct xen_netif_extra_info *gso;
1168 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1169
1170 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1171 __skb_queue_head(&tmpq, skb);
2688fcb7 1172 queue->rx.rsp_cons += skb_queue_len(&tmpq);
0d160211
JF
1173 goto err;
1174 }
1175 }
1176
3683243b
IC
1177 NETFRONT_SKB_CB(skb)->pull_to = rx->status;
1178 if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
1179 NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
0d160211 1180
b54c9d5b 1181 skb_frag_off_set(&skb_shinfo(skb)->frags[0], rx->offset);
3683243b
IC
1182 skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
1183 skb->data_len = rx->status;
093b9c71 1184 skb->len += rx->status;
0d160211 1185
a761129e 1186 if (unlikely(xennet_fill_frags(queue, skb, &tmpq)))
ad4f15dc 1187 goto err;
0d160211 1188
f942dc25 1189 if (rx->flags & XEN_NETRXF_csum_blank)
0d160211 1190 skb->ip_summed = CHECKSUM_PARTIAL;
f942dc25 1191 else if (rx->flags & XEN_NETRXF_data_validated)
0d160211
JF
1192 skb->ip_summed = CHECKSUM_UNNECESSARY;
1193
1194 __skb_queue_tail(&rxq, skb);
1195
a761129e 1196 i = ++queue->rx.rsp_cons;
0d160211
JF
1197 work_done++;
1198 }
6c5aa6fc
DK
1199 if (need_xdp_flush)
1200 xdp_do_flush();
0d160211 1201
56cfe5d0 1202 __skb_queue_purge(&errq);
0d160211 1203
2688fcb7 1204 work_done -= handle_incoming_queue(queue, &rxq);
0d160211 1205
2688fcb7 1206 xennet_alloc_rx_buffers(queue);
0d160211 1207
0d160211 1208 if (work_done < budget) {
bea3348e
SH
1209 int more_to_do = 0;
1210
6ad20165 1211 napi_complete_done(napi, work_done);
0d160211 1212
2688fcb7 1213 RING_FINAL_CHECK_FOR_RESPONSES(&queue->rx, more_to_do);
6a6dc08f
DV
1214 if (more_to_do)
1215 napi_schedule(napi);
0d160211
JF
1216 }
1217
2688fcb7 1218 spin_unlock(&queue->rx_lock);
0d160211 1219
bea3348e 1220 return work_done;
0d160211
JF
1221}
1222
1223static int xennet_change_mtu(struct net_device *dev, int mtu)
1224{
0c36820e 1225 int max = xennet_can_sg(dev) ? XEN_NETIF_MAX_TX_SIZE : ETH_DATA_LEN;
0d160211
JF
1226
1227 if (mtu > max)
1228 return -EINVAL;
1229 dev->mtu = mtu;
1230 return 0;
1231}
1232
bc1f4470 1233static void xennet_get_stats64(struct net_device *dev,
1234 struct rtnl_link_stats64 *tot)
e00f85be 1235{
1236 struct netfront_info *np = netdev_priv(dev);
1237 int cpu;
1238
1239 for_each_possible_cpu(cpu) {
900e1833
DV
1240 struct netfront_stats *rx_stats = per_cpu_ptr(np->rx_stats, cpu);
1241 struct netfront_stats *tx_stats = per_cpu_ptr(np->tx_stats, cpu);
e00f85be 1242 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1243 unsigned int start;
1244
1245 do {
900e1833
DV
1246 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
1247 tx_packets = tx_stats->packets;
1248 tx_bytes = tx_stats->bytes;
1249 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
e00f85be 1250
900e1833
DV
1251 do {
1252 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
1253 rx_packets = rx_stats->packets;
1254 rx_bytes = rx_stats->bytes;
1255 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
e00f85be 1256
1257 tot->rx_packets += rx_packets;
1258 tot->tx_packets += tx_packets;
1259 tot->rx_bytes += rx_bytes;
1260 tot->tx_bytes += tx_bytes;
1261 }
1262
1263 tot->rx_errors = dev->stats.rx_errors;
1264 tot->tx_dropped = dev->stats.tx_dropped;
e00f85be 1265}
1266
2688fcb7 1267static void xennet_release_tx_bufs(struct netfront_queue *queue)
0d160211
JF
1268{
1269 struct sk_buff *skb;
1270 int i;
1271
1272 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1273 /* Skip over entries which are actually freelist references */
21631d2d 1274 if (!queue->tx_skbs[i])
0d160211
JF
1275 continue;
1276
21631d2d
JG
1277 skb = queue->tx_skbs[i];
1278 queue->tx_skbs[i] = NULL;
2688fcb7
AB
1279 get_page(queue->grant_tx_page[i]);
1280 gnttab_end_foreign_access(queue->grant_tx_ref[i],
cefe0078 1281 GNTMAP_readonly,
2688fcb7
AB
1282 (unsigned long)page_address(queue->grant_tx_page[i]));
1283 queue->grant_tx_page[i] = NULL;
1284 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
21631d2d 1285 add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i);
0d160211
JF
1286 dev_kfree_skb_irq(skb);
1287 }
1288}
1289
2688fcb7 1290static void xennet_release_rx_bufs(struct netfront_queue *queue)
0d160211 1291{
0d160211
JF
1292 int id, ref;
1293
2688fcb7 1294 spin_lock_bh(&queue->rx_lock);
0d160211
JF
1295
1296 for (id = 0; id < NET_RX_RING_SIZE; id++) {
cefe0078
AL
1297 struct sk_buff *skb;
1298 struct page *page;
0d160211 1299
2688fcb7 1300 skb = queue->rx_skbs[id];
cefe0078 1301 if (!skb)
0d160211 1302 continue;
0d160211 1303
2688fcb7 1304 ref = queue->grant_rx_ref[id];
cefe0078
AL
1305 if (ref == GRANT_INVALID_REF)
1306 continue;
0d160211 1307
cefe0078 1308 page = skb_frag_page(&skb_shinfo(skb)->frags[0]);
0d160211 1309
cefe0078
AL
1310 /* gnttab_end_foreign_access() needs a page ref until
1311 * foreign access is ended (which may be deferred).
1312 */
1313 get_page(page);
1314 gnttab_end_foreign_access(ref, 0,
1315 (unsigned long)page_address(page));
2688fcb7 1316 queue->grant_rx_ref[id] = GRANT_INVALID_REF;
0d160211 1317
cefe0078 1318 kfree_skb(skb);
0d160211
JF
1319 }
1320
2688fcb7 1321 spin_unlock_bh(&queue->rx_lock);
0d160211
JF
1322}
1323
c8f44aff
MM
1324static netdev_features_t xennet_fix_features(struct net_device *dev,
1325 netdev_features_t features)
8f7b01a1
ED
1326{
1327 struct netfront_info *np = netdev_priv(dev);
8f7b01a1 1328
2890ea5c
JG
1329 if (features & NETIF_F_SG &&
1330 !xenbus_read_unsigned(np->xbdev->otherend, "feature-sg", 0))
1331 features &= ~NETIF_F_SG;
8f7b01a1 1332
2890ea5c
JG
1333 if (features & NETIF_F_IPV6_CSUM &&
1334 !xenbus_read_unsigned(np->xbdev->otherend,
1335 "feature-ipv6-csum-offload", 0))
1336 features &= ~NETIF_F_IPV6_CSUM;
8f7b01a1 1337
2890ea5c
JG
1338 if (features & NETIF_F_TSO &&
1339 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv4", 0))
1340 features &= ~NETIF_F_TSO;
8f7b01a1 1341
2890ea5c
JG
1342 if (features & NETIF_F_TSO6 &&
1343 !xenbus_read_unsigned(np->xbdev->otherend, "feature-gso-tcpv6", 0))
1344 features &= ~NETIF_F_TSO6;
2c0057de 1345
8f7b01a1
ED
1346 return features;
1347}
1348
c8f44aff
MM
1349static int xennet_set_features(struct net_device *dev,
1350 netdev_features_t features)
8f7b01a1
ED
1351{
1352 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1353 netdev_info(dev, "Reducing MTU because no SG offload");
1354 dev->mtu = ETH_DATA_LEN;
1355 }
1356
1357 return 0;
1358}
1359
d634bf2c 1360static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
cf66f9d4 1361{
2688fcb7 1362 struct netfront_queue *queue = dev_id;
cf66f9d4
KRW
1363 unsigned long flags;
1364
2688fcb7
AB
1365 spin_lock_irqsave(&queue->tx_lock, flags);
1366 xennet_tx_buf_gc(queue);
1367 spin_unlock_irqrestore(&queue->tx_lock, flags);
cf66f9d4 1368
d634bf2c
WL
1369 return IRQ_HANDLED;
1370}
1371
1372static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
1373{
2688fcb7
AB
1374 struct netfront_queue *queue = dev_id;
1375 struct net_device *dev = queue->info->netdev;
d634bf2c
WL
1376
1377 if (likely(netif_carrier_ok(dev) &&
2688fcb7 1378 RING_HAS_UNCONSUMED_RESPONSES(&queue->rx)))
76541869 1379 napi_schedule(&queue->napi);
cf66f9d4 1380
d634bf2c
WL
1381 return IRQ_HANDLED;
1382}
cf66f9d4 1383
d634bf2c
WL
1384static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1385{
1386 xennet_tx_interrupt(irq, dev_id);
1387 xennet_rx_interrupt(irq, dev_id);
cf66f9d4
KRW
1388 return IRQ_HANDLED;
1389}
1390
1391#ifdef CONFIG_NET_POLL_CONTROLLER
1392static void xennet_poll_controller(struct net_device *dev)
1393{
2688fcb7
AB
1394 /* Poll each queue */
1395 struct netfront_info *info = netdev_priv(dev);
1396 unsigned int num_queues = dev->real_num_tx_queues;
1397 unsigned int i;
1398 for (i = 0; i < num_queues; ++i)
1399 xennet_interrupt(0, &info->queues[i]);
cf66f9d4
KRW
1400}
1401#endif
1402
6c5aa6fc
DK
1403#define NETBACK_XDP_HEADROOM_DISABLE 0
1404#define NETBACK_XDP_HEADROOM_ENABLE 1
1405
1406static int talk_to_netback_xdp(struct netfront_info *np, int xdp)
1407{
1408 int err;
1409 unsigned short headroom;
1410
1411 headroom = xdp ? XDP_PACKET_HEADROOM : 0;
1412 err = xenbus_printf(XBT_NIL, np->xbdev->nodename,
1413 "xdp-headroom", "%hu",
1414 headroom);
1415 if (err)
1416 pr_warn("Error writing xdp-headroom\n");
1417
1418 return err;
1419}
1420
1421static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1422 struct netlink_ext_ack *extack)
1423{
1424 unsigned long max_mtu = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM;
1425 struct netfront_info *np = netdev_priv(dev);
1426 struct bpf_prog *old_prog;
1427 unsigned int i, err;
1428
1429 if (dev->mtu > max_mtu) {
1430 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_mtu);
1431 return -EINVAL;
1432 }
1433
1434 if (!np->netback_has_xdp_headroom)
1435 return 0;
1436
1437 xenbus_switch_state(np->xbdev, XenbusStateReconfiguring);
1438
1439 err = talk_to_netback_xdp(np, prog ? NETBACK_XDP_HEADROOM_ENABLE :
1440 NETBACK_XDP_HEADROOM_DISABLE);
1441 if (err)
1442 return err;
1443
1444 /* avoid the race with XDP headroom adjustment */
1445 wait_event(module_wq,
1446 xenbus_read_driver_state(np->xbdev->otherend) ==
1447 XenbusStateReconfigured);
1448 np->netfront_xdp_enabled = true;
1449
1450 old_prog = rtnl_dereference(np->queues[0].xdp_prog);
1451
1452 if (prog)
1453 bpf_prog_add(prog, dev->real_num_tx_queues);
1454
1455 for (i = 0; i < dev->real_num_tx_queues; ++i)
1456 rcu_assign_pointer(np->queues[i].xdp_prog, prog);
1457
1458 if (old_prog)
1459 for (i = 0; i < dev->real_num_tx_queues; ++i)
1460 bpf_prog_put(old_prog);
1461
1462 xenbus_switch_state(np->xbdev, XenbusStateConnected);
1463
1464 return 0;
1465}
1466
6c5aa6fc
DK
1467static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1468{
1469 switch (xdp->command) {
1470 case XDP_SETUP_PROG:
1471 return xennet_xdp_set(dev, xdp->prog, xdp->extack);
6c5aa6fc
DK
1472 default:
1473 return -EINVAL;
1474 }
1475}
1476
0a0b9d2e
SH
1477static const struct net_device_ops xennet_netdev_ops = {
1478 .ndo_open = xennet_open,
0a0b9d2e
SH
1479 .ndo_stop = xennet_close,
1480 .ndo_start_xmit = xennet_start_xmit,
1481 .ndo_change_mtu = xennet_change_mtu,
e00f85be 1482 .ndo_get_stats64 = xennet_get_stats64,
0a0b9d2e
SH
1483 .ndo_set_mac_address = eth_mac_addr,
1484 .ndo_validate_addr = eth_validate_addr,
fb507934
MM
1485 .ndo_fix_features = xennet_fix_features,
1486 .ndo_set_features = xennet_set_features,
2688fcb7 1487 .ndo_select_queue = xennet_select_queue,
6c5aa6fc
DK
1488 .ndo_bpf = xennet_xdp,
1489 .ndo_xdp_xmit = xennet_xdp_xmit,
cf66f9d4
KRW
1490#ifdef CONFIG_NET_POLL_CONTROLLER
1491 .ndo_poll_controller = xennet_poll_controller,
1492#endif
0a0b9d2e
SH
1493};
1494
900e1833
DV
1495static void xennet_free_netdev(struct net_device *netdev)
1496{
1497 struct netfront_info *np = netdev_priv(netdev);
1498
1499 free_percpu(np->rx_stats);
1500 free_percpu(np->tx_stats);
1501 free_netdev(netdev);
1502}
1503
8e0e46bb 1504static struct net_device *xennet_create_dev(struct xenbus_device *dev)
0d160211 1505{
2688fcb7 1506 int err;
0d160211
JF
1507 struct net_device *netdev;
1508 struct netfront_info *np;
1509
50ee6061 1510 netdev = alloc_etherdev_mq(sizeof(struct netfront_info), xennet_max_queues);
41de8d4c 1511 if (!netdev)
0d160211 1512 return ERR_PTR(-ENOMEM);
0d160211
JF
1513
1514 np = netdev_priv(netdev);
1515 np->xbdev = dev;
1516
2688fcb7 1517 np->queues = NULL;
0d160211 1518
e00f85be 1519 err = -ENOMEM;
900e1833
DV
1520 np->rx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1521 if (np->rx_stats == NULL)
1522 goto exit;
1523 np->tx_stats = netdev_alloc_pcpu_stats(struct netfront_stats);
1524 if (np->tx_stats == NULL)
e00f85be 1525 goto exit;
1526
0a0b9d2e
SH
1527 netdev->netdev_ops = &xennet_netdev_ops;
1528
fb507934
MM
1529 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1530 NETIF_F_GSO_ROBUST;
2c0057de
PD
1531 netdev->hw_features = NETIF_F_SG |
1532 NETIF_F_IPV6_CSUM |
1533 NETIF_F_TSO | NETIF_F_TSO6;
0d160211 1534
fc3e5941
IC
1535 /*
1536 * Assume that all hw features are available for now. This set
1537 * will be adjusted by the call to netdev_update_features() in
1538 * xennet_connect() which is the earliest point where we can
1539 * negotiate with the backend regarding supported features.
1540 */
1541 netdev->features |= netdev->hw_features;
1542
7ad24ea4 1543 netdev->ethtool_ops = &xennet_ethtool_ops;
e1043a4b 1544 netdev->min_mtu = ETH_MIN_MTU;
d0c2c997 1545 netdev->max_mtu = XEN_NETIF_MAX_TX_SIZE;
0d160211
JF
1546 SET_NETDEV_DEV(netdev, &dev->dev);
1547
1548 np->netdev = netdev;
6c5aa6fc 1549 np->netfront_xdp_enabled = false;
0d160211
JF
1550
1551 netif_carrier_off(netdev);
1552
c2c63310
AR
1553 do {
1554 xenbus_switch_state(dev, XenbusStateInitialising);
1555 err = wait_event_timeout(module_wq,
1556 xenbus_read_driver_state(dev->otherend) !=
1557 XenbusStateClosed &&
1558 xenbus_read_driver_state(dev->otherend) !=
1559 XenbusStateUnknown, XENNET_TIMEOUT);
1560 } while (!err);
1561
0d160211
JF
1562 return netdev;
1563
0d160211 1564 exit:
900e1833 1565 xennet_free_netdev(netdev);
0d160211
JF
1566 return ERR_PTR(err);
1567}
1568
80708602 1569/*
0d160211
JF
1570 * Entry point to this code when a new device is created. Allocate the basic
1571 * structures and the ring buffers for communication with the backend, and
1572 * inform the backend of the appropriate details for those.
1573 */
8e0e46bb 1574static int netfront_probe(struct xenbus_device *dev,
1dd06ae8 1575 const struct xenbus_device_id *id)
0d160211
JF
1576{
1577 int err;
1578 struct net_device *netdev;
1579 struct netfront_info *info;
1580
1581 netdev = xennet_create_dev(dev);
1582 if (IS_ERR(netdev)) {
1583 err = PTR_ERR(netdev);
1584 xenbus_dev_fatal(dev, err, "creating netdev");
1585 return err;
1586 }
1587
1588 info = netdev_priv(netdev);
1b713e00 1589 dev_set_drvdata(&dev->dev, info);
27b917e5
TI
1590#ifdef CONFIG_SYSFS
1591 info->netdev->sysfs_groups[0] = &xennet_dev_group;
1592#endif
0d160211 1593
0d160211 1594 return 0;
0d160211
JF
1595}
1596
1597static void xennet_end_access(int ref, void *page)
1598{
1599 /* This frees the page as a side-effect */
1600 if (ref != GRANT_INVALID_REF)
1601 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1602}
1603
1604static void xennet_disconnect_backend(struct netfront_info *info)
1605{
2688fcb7 1606 unsigned int i = 0;
2688fcb7
AB
1607 unsigned int num_queues = info->netdev->real_num_tx_queues;
1608
f9feb1e6
DV
1609 netif_carrier_off(info->netdev);
1610
9a873c71 1611 for (i = 0; i < num_queues && info->queues; ++i) {
76541869
DV
1612 struct netfront_queue *queue = &info->queues[i];
1613
74470954
BO
1614 del_timer_sync(&queue->rx_refill_timer);
1615
2688fcb7
AB
1616 if (queue->tx_irq && (queue->tx_irq == queue->rx_irq))
1617 unbind_from_irqhandler(queue->tx_irq, queue);
1618 if (queue->tx_irq && (queue->tx_irq != queue->rx_irq)) {
1619 unbind_from_irqhandler(queue->tx_irq, queue);
1620 unbind_from_irqhandler(queue->rx_irq, queue);
1621 }
1622 queue->tx_evtchn = queue->rx_evtchn = 0;
1623 queue->tx_irq = queue->rx_irq = 0;
0d160211 1624
274b0455
CW
1625 if (netif_running(info->netdev))
1626 napi_synchronize(&queue->napi);
f9feb1e6 1627
a5b5dc3c
DV
1628 xennet_release_tx_bufs(queue);
1629 xennet_release_rx_bufs(queue);
1630 gnttab_free_grant_references(queue->gref_tx_head);
1631 gnttab_free_grant_references(queue->gref_rx_head);
1632
2688fcb7
AB
1633 /* End access and free the pages */
1634 xennet_end_access(queue->tx_ring_ref, queue->tx.sring);
1635 xennet_end_access(queue->rx_ring_ref, queue->rx.sring);
0d160211 1636
2688fcb7
AB
1637 queue->tx_ring_ref = GRANT_INVALID_REF;
1638 queue->rx_ring_ref = GRANT_INVALID_REF;
1639 queue->tx.sring = NULL;
1640 queue->rx.sring = NULL;
6c5aa6fc
DK
1641
1642 page_pool_destroy(queue->page_pool);
2688fcb7 1643 }
0d160211
JF
1644}
1645
80708602 1646/*
0d160211
JF
1647 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1648 * driver restart. We tear down our netif structure and recreate it, but
1649 * leave the device-layer structures intact so that this is transparent to the
1650 * rest of the kernel.
1651 */
1652static int netfront_resume(struct xenbus_device *dev)
1653{
1b713e00 1654 struct netfront_info *info = dev_get_drvdata(&dev->dev);
0d160211
JF
1655
1656 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1657
1658 xennet_disconnect_backend(info);
1659 return 0;
1660}
1661
1662static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1663{
1664 char *s, *e, *macstr;
1665 int i;
1666
1667 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1668 if (IS_ERR(macstr))
1669 return PTR_ERR(macstr);
1670
1671 for (i = 0; i < ETH_ALEN; i++) {
1672 mac[i] = simple_strtoul(s, &e, 16);
1673 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1674 kfree(macstr);
1675 return -ENOENT;
1676 }
1677 s = e+1;
1678 }
1679
1680 kfree(macstr);
1681 return 0;
1682}
1683
2688fcb7 1684static int setup_netfront_single(struct netfront_queue *queue)
d634bf2c
WL
1685{
1686 int err;
1687
2688fcb7 1688 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
d634bf2c
WL
1689 if (err < 0)
1690 goto fail;
1691
2688fcb7 1692 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
d634bf2c 1693 xennet_interrupt,
2688fcb7 1694 0, queue->info->netdev->name, queue);
d634bf2c
WL
1695 if (err < 0)
1696 goto bind_fail;
2688fcb7
AB
1697 queue->rx_evtchn = queue->tx_evtchn;
1698 queue->rx_irq = queue->tx_irq = err;
d634bf2c
WL
1699
1700 return 0;
1701
1702bind_fail:
2688fcb7
AB
1703 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1704 queue->tx_evtchn = 0;
d634bf2c
WL
1705fail:
1706 return err;
1707}
1708
2688fcb7 1709static int setup_netfront_split(struct netfront_queue *queue)
d634bf2c
WL
1710{
1711 int err;
1712
2688fcb7 1713 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->tx_evtchn);
d634bf2c
WL
1714 if (err < 0)
1715 goto fail;
2688fcb7 1716 err = xenbus_alloc_evtchn(queue->info->xbdev, &queue->rx_evtchn);
d634bf2c
WL
1717 if (err < 0)
1718 goto alloc_rx_evtchn_fail;
1719
2688fcb7
AB
1720 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
1721 "%s-tx", queue->name);
1722 err = bind_evtchn_to_irqhandler(queue->tx_evtchn,
d634bf2c 1723 xennet_tx_interrupt,
2688fcb7 1724 0, queue->tx_irq_name, queue);
d634bf2c
WL
1725 if (err < 0)
1726 goto bind_tx_fail;
2688fcb7 1727 queue->tx_irq = err;
d634bf2c 1728
2688fcb7
AB
1729 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
1730 "%s-rx", queue->name);
1731 err = bind_evtchn_to_irqhandler(queue->rx_evtchn,
d634bf2c 1732 xennet_rx_interrupt,
2688fcb7 1733 0, queue->rx_irq_name, queue);
d634bf2c
WL
1734 if (err < 0)
1735 goto bind_rx_fail;
2688fcb7 1736 queue->rx_irq = err;
d634bf2c
WL
1737
1738 return 0;
1739
1740bind_rx_fail:
2688fcb7
AB
1741 unbind_from_irqhandler(queue->tx_irq, queue);
1742 queue->tx_irq = 0;
d634bf2c 1743bind_tx_fail:
2688fcb7
AB
1744 xenbus_free_evtchn(queue->info->xbdev, queue->rx_evtchn);
1745 queue->rx_evtchn = 0;
d634bf2c 1746alloc_rx_evtchn_fail:
2688fcb7
AB
1747 xenbus_free_evtchn(queue->info->xbdev, queue->tx_evtchn);
1748 queue->tx_evtchn = 0;
d634bf2c
WL
1749fail:
1750 return err;
1751}
1752
2688fcb7
AB
1753static int setup_netfront(struct xenbus_device *dev,
1754 struct netfront_queue *queue, unsigned int feature_split_evtchn)
0d160211
JF
1755{
1756 struct xen_netif_tx_sring *txs;
1757 struct xen_netif_rx_sring *rxs;
ccc9d90a 1758 grant_ref_t gref;
0d160211 1759 int err;
0d160211 1760
2688fcb7
AB
1761 queue->tx_ring_ref = GRANT_INVALID_REF;
1762 queue->rx_ring_ref = GRANT_INVALID_REF;
1763 queue->rx.sring = NULL;
1764 queue->tx.sring = NULL;
0d160211 1765
a144ff09 1766 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
0d160211
JF
1767 if (!txs) {
1768 err = -ENOMEM;
1769 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1770 goto fail;
1771 }
1772 SHARED_RING_INIT(txs);
30c5d7f0 1773 FRONT_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
0d160211 1774
ccc9d90a 1775 err = xenbus_grant_ring(dev, txs, 1, &gref);
1ca2983a
WL
1776 if (err < 0)
1777 goto grant_tx_ring_fail;
ccc9d90a 1778 queue->tx_ring_ref = gref;
0d160211 1779
a144ff09 1780 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
0d160211
JF
1781 if (!rxs) {
1782 err = -ENOMEM;
1783 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1ca2983a 1784 goto alloc_rx_ring_fail;
0d160211
JF
1785 }
1786 SHARED_RING_INIT(rxs);
30c5d7f0 1787 FRONT_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
0d160211 1788
ccc9d90a 1789 err = xenbus_grant_ring(dev, rxs, 1, &gref);
1ca2983a
WL
1790 if (err < 0)
1791 goto grant_rx_ring_fail;
ccc9d90a 1792 queue->rx_ring_ref = gref;
0d160211 1793
d634bf2c 1794 if (feature_split_evtchn)
2688fcb7 1795 err = setup_netfront_split(queue);
d634bf2c
WL
1796 /* setup single event channel if
1797 * a) feature-split-event-channels == 0
1798 * b) feature-split-event-channels == 1 but failed to setup
1799 */
e93fac3b 1800 if (!feature_split_evtchn || err)
2688fcb7 1801 err = setup_netfront_single(queue);
d634bf2c 1802
0d160211 1803 if (err)
1ca2983a 1804 goto alloc_evtchn_fail;
0d160211 1805
0d160211
JF
1806 return 0;
1807
1ca2983a
WL
1808 /* If we fail to setup netfront, it is safe to just revoke access to
1809 * granted pages because backend is not accessing it at this point.
1810 */
1ca2983a 1811alloc_evtchn_fail:
2688fcb7 1812 gnttab_end_foreign_access_ref(queue->rx_ring_ref, 0);
1ca2983a
WL
1813grant_rx_ring_fail:
1814 free_page((unsigned long)rxs);
1815alloc_rx_ring_fail:
2688fcb7 1816 gnttab_end_foreign_access_ref(queue->tx_ring_ref, 0);
1ca2983a
WL
1817grant_tx_ring_fail:
1818 free_page((unsigned long)txs);
1819fail:
0d160211
JF
1820 return err;
1821}
1822
2688fcb7
AB
1823/* Queue-specific initialisation
1824 * This used to be done in xennet_create_dev() but must now
1825 * be run per-queue.
1826 */
1827static int xennet_init_queue(struct netfront_queue *queue)
1828{
1829 unsigned short i;
1830 int err = 0;
21f2706b 1831 char *devid;
2688fcb7
AB
1832
1833 spin_lock_init(&queue->tx_lock);
1834 spin_lock_init(&queue->rx_lock);
1835
e99e88a9 1836 timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
2688fcb7 1837
21f2706b
XL
1838 devid = strrchr(queue->info->xbdev->nodename, '/') + 1;
1839 snprintf(queue->name, sizeof(queue->name), "vif%s-q%u",
1840 devid, queue->id);
8b715010 1841
21631d2d 1842 /* Initialise tx_skb_freelist as a free chain containing every entry. */
2688fcb7
AB
1843 queue->tx_skb_freelist = 0;
1844 for (i = 0; i < NET_TX_RING_SIZE; i++) {
21631d2d 1845 queue->tx_link[i] = i + 1;
2688fcb7
AB
1846 queue->grant_tx_ref[i] = GRANT_INVALID_REF;
1847 queue->grant_tx_page[i] = NULL;
1848 }
21631d2d 1849 queue->tx_link[NET_TX_RING_SIZE - 1] = TX_LINK_NONE;
2688fcb7
AB
1850
1851 /* Clear out rx_skbs */
1852 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1853 queue->rx_skbs[i] = NULL;
1854 queue->grant_rx_ref[i] = GRANT_INVALID_REF;
1855 }
1856
1857 /* A grant for every tx ring slot */
1f3c2eba 1858 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2688fcb7
AB
1859 &queue->gref_tx_head) < 0) {
1860 pr_alert("can't alloc tx grant refs\n");
1861 err = -ENOMEM;
1862 goto exit;
1863 }
1864
1865 /* A grant for every rx ring slot */
1f3c2eba 1866 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
2688fcb7
AB
1867 &queue->gref_rx_head) < 0) {
1868 pr_alert("can't alloc rx grant refs\n");
1869 err = -ENOMEM;
1870 goto exit_free_tx;
1871 }
1872
2688fcb7
AB
1873 return 0;
1874
1875 exit_free_tx:
1876 gnttab_free_grant_references(queue->gref_tx_head);
1877 exit:
1878 return err;
1879}
1880
50ee6061
AB
1881static int write_queue_xenstore_keys(struct netfront_queue *queue,
1882 struct xenbus_transaction *xbt, int write_hierarchical)
1883{
1884 /* Write the queue-specific keys into XenStore in the traditional
1885 * way for a single queue, or in a queue subkeys for multiple
1886 * queues.
1887 */
1888 struct xenbus_device *dev = queue->info->xbdev;
1889 int err;
1890 const char *message;
1891 char *path;
1892 size_t pathsize;
1893
1894 /* Choose the correct place to write the keys */
1895 if (write_hierarchical) {
1896 pathsize = strlen(dev->nodename) + 10;
1897 path = kzalloc(pathsize, GFP_KERNEL);
1898 if (!path) {
1899 err = -ENOMEM;
1900 message = "out of memory while writing ring references";
1901 goto error;
1902 }
1903 snprintf(path, pathsize, "%s/queue-%u",
1904 dev->nodename, queue->id);
1905 } else {
1906 path = (char *)dev->nodename;
1907 }
1908
1909 /* Write ring references */
1910 err = xenbus_printf(*xbt, path, "tx-ring-ref", "%u",
1911 queue->tx_ring_ref);
1912 if (err) {
1913 message = "writing tx-ring-ref";
1914 goto error;
1915 }
1916
1917 err = xenbus_printf(*xbt, path, "rx-ring-ref", "%u",
1918 queue->rx_ring_ref);
1919 if (err) {
1920 message = "writing rx-ring-ref";
1921 goto error;
1922 }
1923
1924 /* Write event channels; taking into account both shared
1925 * and split event channel scenarios.
1926 */
1927 if (queue->tx_evtchn == queue->rx_evtchn) {
1928 /* Shared event channel */
1929 err = xenbus_printf(*xbt, path,
1930 "event-channel", "%u", queue->tx_evtchn);
1931 if (err) {
1932 message = "writing event-channel";
1933 goto error;
1934 }
1935 } else {
1936 /* Split event channels */
1937 err = xenbus_printf(*xbt, path,
1938 "event-channel-tx", "%u", queue->tx_evtchn);
1939 if (err) {
1940 message = "writing event-channel-tx";
1941 goto error;
1942 }
1943
1944 err = xenbus_printf(*xbt, path,
1945 "event-channel-rx", "%u", queue->rx_evtchn);
1946 if (err) {
1947 message = "writing event-channel-rx";
1948 goto error;
1949 }
1950 }
1951
1952 if (write_hierarchical)
1953 kfree(path);
1954 return 0;
1955
1956error:
1957 if (write_hierarchical)
1958 kfree(path);
1959 xenbus_dev_fatal(dev, err, "%s", message);
1960 return err;
1961}
1962
ce58725f
DV
1963static void xennet_destroy_queues(struct netfront_info *info)
1964{
1965 unsigned int i;
1966
ce58725f
DV
1967 for (i = 0; i < info->netdev->real_num_tx_queues; i++) {
1968 struct netfront_queue *queue = &info->queues[i];
1969
1970 if (netif_running(info->netdev))
1971 napi_disable(&queue->napi);
1972 netif_napi_del(&queue->napi);
1973 }
1974
ce58725f
DV
1975 kfree(info->queues);
1976 info->queues = NULL;
1977}
1978
6c5aa6fc
DK
1979
1980
1981static int xennet_create_page_pool(struct netfront_queue *queue)
1982{
1983 int err;
1984 struct page_pool_params pp_params = {
1985 .order = 0,
1986 .flags = 0,
1987 .pool_size = NET_RX_RING_SIZE,
1988 .nid = NUMA_NO_NODE,
1989 .dev = &queue->info->netdev->dev,
1990 .offset = XDP_PACKET_HEADROOM,
1991 .max_len = XEN_PAGE_SIZE - XDP_PACKET_HEADROOM,
1992 };
1993
1994 queue->page_pool = page_pool_create(&pp_params);
1995 if (IS_ERR(queue->page_pool)) {
1996 err = PTR_ERR(queue->page_pool);
1997 queue->page_pool = NULL;
1998 return err;
1999 }
2000
2001 err = xdp_rxq_info_reg(&queue->xdp_rxq, queue->info->netdev,
b02e5a0e 2002 queue->id, 0);
6c5aa6fc
DK
2003 if (err) {
2004 netdev_err(queue->info->netdev, "xdp_rxq_info_reg failed\n");
2005 goto err_free_pp;
2006 }
2007
2008 err = xdp_rxq_info_reg_mem_model(&queue->xdp_rxq,
2009 MEM_TYPE_PAGE_POOL, queue->page_pool);
2010 if (err) {
2011 netdev_err(queue->info->netdev, "xdp_rxq_info_reg_mem_model failed\n");
2012 goto err_unregister_rxq;
2013 }
2014 return 0;
2015
2016err_unregister_rxq:
2017 xdp_rxq_info_unreg(&queue->xdp_rxq);
2018err_free_pp:
2019 page_pool_destroy(queue->page_pool);
2020 queue->page_pool = NULL;
2021 return err;
2022}
2023
ce58725f 2024static int xennet_create_queues(struct netfront_info *info,
ca88ea12 2025 unsigned int *num_queues)
ce58725f
DV
2026{
2027 unsigned int i;
2028 int ret;
2029
ca88ea12 2030 info->queues = kcalloc(*num_queues, sizeof(struct netfront_queue),
ce58725f
DV
2031 GFP_KERNEL);
2032 if (!info->queues)
2033 return -ENOMEM;
2034
ca88ea12 2035 for (i = 0; i < *num_queues; i++) {
ce58725f
DV
2036 struct netfront_queue *queue = &info->queues[i];
2037
2038 queue->id = i;
2039 queue->info = info;
2040
2041 ret = xennet_init_queue(queue);
2042 if (ret < 0) {
f599c64f 2043 dev_warn(&info->xbdev->dev,
69cb8524 2044 "only created %d queues\n", i);
ca88ea12 2045 *num_queues = i;
ce58725f
DV
2046 break;
2047 }
2048
6c5aa6fc
DK
2049 /* use page pool recycling instead of buddy allocator */
2050 ret = xennet_create_page_pool(queue);
2051 if (ret < 0) {
2052 dev_err(&info->xbdev->dev, "can't allocate page pool\n");
2053 *num_queues = i;
2054 return ret;
2055 }
2056
ce58725f
DV
2057 netif_napi_add(queue->info->netdev, &queue->napi,
2058 xennet_poll, 64);
2059 if (netif_running(info->netdev))
2060 napi_enable(&queue->napi);
2061 }
2062
ca88ea12 2063 netif_set_real_num_tx_queues(info->netdev, *num_queues);
ce58725f 2064
ca88ea12 2065 if (*num_queues == 0) {
f599c64f 2066 dev_err(&info->xbdev->dev, "no queues\n");
ce58725f
DV
2067 return -EINVAL;
2068 }
2069 return 0;
2070}
2071
0d160211 2072/* Common code used when first setting up, and when resuming. */
f502bf2b 2073static int talk_to_netback(struct xenbus_device *dev,
0d160211
JF
2074 struct netfront_info *info)
2075{
2076 const char *message;
2077 struct xenbus_transaction xbt;
2078 int err;
2688fcb7
AB
2079 unsigned int feature_split_evtchn;
2080 unsigned int i = 0;
50ee6061 2081 unsigned int max_queues = 0;
2688fcb7
AB
2082 struct netfront_queue *queue = NULL;
2083 unsigned int num_queues = 1;
0d160211 2084
2688fcb7
AB
2085 info->netdev->irq = 0;
2086
50ee6061 2087 /* Check if backend supports multiple queues */
2890ea5c
JG
2088 max_queues = xenbus_read_unsigned(info->xbdev->otherend,
2089 "multi-queue-max-queues", 1);
50ee6061
AB
2090 num_queues = min(max_queues, xennet_max_queues);
2091
2688fcb7 2092 /* Check feature-split-event-channels */
2890ea5c
JG
2093 feature_split_evtchn = xenbus_read_unsigned(info->xbdev->otherend,
2094 "feature-split-event-channels", 0);
2688fcb7
AB
2095
2096 /* Read mac addr. */
2097 err = xen_net_read_mac(dev, info->netdev->dev_addr);
2098 if (err) {
2099 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
cb257783 2100 goto out_unlocked;
2688fcb7
AB
2101 }
2102
6c5aa6fc
DK
2103 info->netback_has_xdp_headroom = xenbus_read_unsigned(info->xbdev->otherend,
2104 "feature-xdp-headroom", 0);
2105 if (info->netback_has_xdp_headroom) {
2106 /* set the current xen-netfront xdp state */
2107 err = talk_to_netback_xdp(info, info->netfront_xdp_enabled ?
2108 NETBACK_XDP_HEADROOM_ENABLE :
2109 NETBACK_XDP_HEADROOM_DISABLE);
2110 if (err)
2111 goto out_unlocked;
2112 }
2113
f599c64f 2114 rtnl_lock();
ce58725f
DV
2115 if (info->queues)
2116 xennet_destroy_queues(info);
2117
ca88ea12 2118 err = xennet_create_queues(info, &num_queues);
e2e004ac
RL
2119 if (err < 0) {
2120 xenbus_dev_fatal(dev, err, "creating queues");
2121 kfree(info->queues);
2122 info->queues = NULL;
2123 goto out;
2124 }
f599c64f 2125 rtnl_unlock();
2688fcb7
AB
2126
2127 /* Create shared ring, alloc event channel -- for each queue */
2128 for (i = 0; i < num_queues; ++i) {
2129 queue = &info->queues[i];
2688fcb7 2130 err = setup_netfront(dev, queue, feature_split_evtchn);
e2e004ac
RL
2131 if (err)
2132 goto destroy_ring;
2688fcb7 2133 }
0d160211
JF
2134
2135again:
2136 err = xenbus_transaction_start(&xbt);
2137 if (err) {
2138 xenbus_dev_fatal(dev, err, "starting transaction");
2139 goto destroy_ring;
2140 }
2141
812494d9 2142 if (xenbus_exists(XBT_NIL,
2143 info->xbdev->otherend, "multi-queue-max-queues")) {
50ee6061 2144 /* Write the number of queues */
812494d9 2145 err = xenbus_printf(xbt, dev->nodename,
2146 "multi-queue-num-queues", "%u", num_queues);
d634bf2c 2147 if (err) {
50ee6061
AB
2148 message = "writing multi-queue-num-queues";
2149 goto abort_transaction_no_dev_fatal;
d634bf2c 2150 }
812494d9 2151 }
50ee6061 2152
812494d9 2153 if (num_queues == 1) {
2154 err = write_queue_xenstore_keys(&info->queues[0], &xbt, 0); /* flat */
2155 if (err)
2156 goto abort_transaction_no_dev_fatal;
2157 } else {
50ee6061
AB
2158 /* Write the keys for each queue */
2159 for (i = 0; i < num_queues; ++i) {
2160 queue = &info->queues[i];
2161 err = write_queue_xenstore_keys(queue, &xbt, 1); /* hierarchical */
2162 if (err)
2163 goto abort_transaction_no_dev_fatal;
d634bf2c 2164 }
0d160211
JF
2165 }
2166
50ee6061 2167 /* The remaining keys are not queue-specific */
0d160211
JF
2168 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
2169 1);
2170 if (err) {
2171 message = "writing request-rx-copy";
2172 goto abort_transaction;
2173 }
2174
2175 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
2176 if (err) {
2177 message = "writing feature-rx-notify";
2178 goto abort_transaction;
2179 }
2180
2181 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
2182 if (err) {
2183 message = "writing feature-sg";
2184 goto abort_transaction;
2185 }
2186
2187 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
2188 if (err) {
2189 message = "writing feature-gso-tcpv4";
2190 goto abort_transaction;
2191 }
2192
2c0057de
PD
2193 err = xenbus_write(xbt, dev->nodename, "feature-gso-tcpv6", "1");
2194 if (err) {
2195 message = "writing feature-gso-tcpv6";
2196 goto abort_transaction;
2197 }
2198
2199 err = xenbus_write(xbt, dev->nodename, "feature-ipv6-csum-offload",
2200 "1");
2201 if (err) {
2202 message = "writing feature-ipv6-csum-offload";
2203 goto abort_transaction;
2204 }
2205
0d160211
JF
2206 err = xenbus_transaction_end(xbt, 0);
2207 if (err) {
2208 if (err == -EAGAIN)
2209 goto again;
2210 xenbus_dev_fatal(dev, err, "completing transaction");
2211 goto destroy_ring;
2212 }
2213
2214 return 0;
2215
2216 abort_transaction:
0d160211 2217 xenbus_dev_fatal(dev, err, "%s", message);
50ee6061
AB
2218abort_transaction_no_dev_fatal:
2219 xenbus_transaction_end(xbt, 1);
0d160211
JF
2220 destroy_ring:
2221 xennet_disconnect_backend(info);
f599c64f 2222 rtnl_lock();
e2e004ac 2223 xennet_destroy_queues(info);
0d160211 2224 out:
f599c64f 2225 rtnl_unlock();
cb257783 2226out_unlocked:
d86b5672 2227 device_unregister(&dev->dev);
0d160211
JF
2228 return err;
2229}
2230
0d160211
JF
2231static int xennet_connect(struct net_device *dev)
2232{
2233 struct netfront_info *np = netdev_priv(dev);
2688fcb7 2234 unsigned int num_queues = 0;
a5b5dc3c 2235 int err;
2688fcb7
AB
2236 unsigned int j = 0;
2237 struct netfront_queue *queue = NULL;
0d160211 2238
2890ea5c 2239 if (!xenbus_read_unsigned(np->xbdev->otherend, "feature-rx-copy", 0)) {
0d160211 2240 dev_info(&dev->dev,
898eb71c 2241 "backend does not support copying receive path\n");
0d160211
JF
2242 return -ENODEV;
2243 }
2244
f502bf2b 2245 err = talk_to_netback(np->xbdev, np);
0d160211
JF
2246 if (err)
2247 return err;
6c5aa6fc
DK
2248 if (np->netback_has_xdp_headroom)
2249 pr_info("backend supports XDP headroom\n");
0d160211 2250
2688fcb7
AB
2251 /* talk_to_netback() sets the correct number of queues */
2252 num_queues = dev->real_num_tx_queues;
2253
f599c64f
RL
2254 if (dev->reg_state == NETREG_UNINITIALIZED) {
2255 err = register_netdev(dev);
2256 if (err) {
2257 pr_warn("%s: register_netdev err=%d\n", __func__, err);
2258 device_unregister(&np->xbdev->dev);
2259 return err;
2260 }
2261 }
2262
45c8184c
RL
2263 rtnl_lock();
2264 netdev_update_features(dev);
2265 rtnl_unlock();
2266
0d160211 2267 /*
a5b5dc3c 2268 * All public and private state should now be sane. Get
0d160211
JF
2269 * ready to start sending and receiving packets and give the driver
2270 * domain a kick because we've probably just requeued some
2271 * packets.
2272 */
2273 netif_carrier_on(np->netdev);
2688fcb7
AB
2274 for (j = 0; j < num_queues; ++j) {
2275 queue = &np->queues[j];
f50b4076 2276
2688fcb7
AB
2277 notify_remote_via_irq(queue->tx_irq);
2278 if (queue->tx_irq != queue->rx_irq)
2279 notify_remote_via_irq(queue->rx_irq);
2688fcb7 2280
f50b4076
DV
2281 spin_lock_irq(&queue->tx_lock);
2282 xennet_tx_buf_gc(queue);
2688fcb7 2283 spin_unlock_irq(&queue->tx_lock);
f50b4076
DV
2284
2285 spin_lock_bh(&queue->rx_lock);
2286 xennet_alloc_rx_buffers(queue);
2688fcb7
AB
2287 spin_unlock_bh(&queue->rx_lock);
2288 }
0d160211
JF
2289
2290 return 0;
2291}
2292
80708602 2293/*
0d160211
JF
2294 * Callback received when the backend's state changes.
2295 */
f502bf2b 2296static void netback_changed(struct xenbus_device *dev,
0d160211
JF
2297 enum xenbus_state backend_state)
2298{
1b713e00 2299 struct netfront_info *np = dev_get_drvdata(&dev->dev);
0d160211
JF
2300 struct net_device *netdev = np->netdev;
2301
2302 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
2303
8edfe2e9
JG
2304 wake_up_all(&module_wq);
2305
0d160211
JF
2306 switch (backend_state) {
2307 case XenbusStateInitialising:
2308 case XenbusStateInitialised:
b78c9512
NI
2309 case XenbusStateReconfiguring:
2310 case XenbusStateReconfigured:
0d160211 2311 case XenbusStateUnknown:
0d160211
JF
2312 break;
2313
2314 case XenbusStateInitWait:
2315 if (dev->state != XenbusStateInitialising)
2316 break;
2317 if (xennet_connect(netdev) != 0)
2318 break;
2319 xenbus_switch_state(dev, XenbusStateConnected);
08e34eb1
LE
2320 break;
2321
2322 case XenbusStateConnected:
ee89bab1 2323 netdev_notify_peers(netdev);
0d160211
JF
2324 break;
2325
bce3ea81
DV
2326 case XenbusStateClosed:
2327 if (dev->state == XenbusStateClosed)
2328 break;
df561f66 2329 fallthrough; /* Missed the backend's CLOSING state */
0d160211
JF
2330 case XenbusStateClosing:
2331 xenbus_frontend_closed(dev);
2332 break;
2333 }
2334}
2335
e0ce4af9
IC
2336static const struct xennet_stat {
2337 char name[ETH_GSTRING_LEN];
2338 u16 offset;
2339} xennet_stats[] = {
2340 {
2341 "rx_gso_checksum_fixup",
2342 offsetof(struct netfront_info, rx_gso_checksum_fixup)
2343 },
2344};
2345
2346static int xennet_get_sset_count(struct net_device *dev, int string_set)
2347{
2348 switch (string_set) {
2349 case ETH_SS_STATS:
2350 return ARRAY_SIZE(xennet_stats);
2351 default:
2352 return -EINVAL;
2353 }
2354}
2355
2356static void xennet_get_ethtool_stats(struct net_device *dev,
2357 struct ethtool_stats *stats, u64 * data)
2358{
2359 void *np = netdev_priv(dev);
2360 int i;
2361
2362 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2688fcb7 2363 data[i] = atomic_read((atomic_t *)(np + xennet_stats[i].offset));
e0ce4af9
IC
2364}
2365
2366static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
2367{
2368 int i;
2369
2370 switch (stringset) {
2371 case ETH_SS_STATS:
2372 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
2373 memcpy(data + i * ETH_GSTRING_LEN,
2374 xennet_stats[i].name, ETH_GSTRING_LEN);
2375 break;
2376 }
2377}
2378
0fc0b732 2379static const struct ethtool_ops xennet_ethtool_ops =
0d160211 2380{
0d160211 2381 .get_link = ethtool_op_get_link,
e0ce4af9
IC
2382
2383 .get_sset_count = xennet_get_sset_count,
2384 .get_ethtool_stats = xennet_get_ethtool_stats,
2385 .get_strings = xennet_get_strings,
91ffb9d3 2386 .get_ts_info = ethtool_op_get_ts_info,
0d160211
JF
2387};
2388
2389#ifdef CONFIG_SYSFS
1f3c2eba
DV
2390static ssize_t show_rxbuf(struct device *dev,
2391 struct device_attribute *attr, char *buf)
0d160211 2392{
1f3c2eba 2393 return sprintf(buf, "%lu\n", NET_RX_RING_SIZE);
0d160211
JF
2394}
2395
1f3c2eba
DV
2396static ssize_t store_rxbuf(struct device *dev,
2397 struct device_attribute *attr,
2398 const char *buf, size_t len)
0d160211 2399{
0d160211 2400 char *endp;
0d160211
JF
2401
2402 if (!capable(CAP_NET_ADMIN))
2403 return -EPERM;
2404
8ed7ec13 2405 simple_strtoul(buf, &endp, 0);
0d160211
JF
2406 if (endp == buf)
2407 return -EBADMSG;
2408
1f3c2eba 2409 /* rxbuf_min and rxbuf_max are no longer configurable. */
0d160211 2410
0d160211
JF
2411 return len;
2412}
2413
d61e4038
JP
2414static DEVICE_ATTR(rxbuf_min, 0644, show_rxbuf, store_rxbuf);
2415static DEVICE_ATTR(rxbuf_max, 0644, show_rxbuf, store_rxbuf);
2416static DEVICE_ATTR(rxbuf_cur, 0444, show_rxbuf, NULL);
0d160211 2417
27b917e5
TI
2418static struct attribute *xennet_dev_attrs[] = {
2419 &dev_attr_rxbuf_min.attr,
2420 &dev_attr_rxbuf_max.attr,
2421 &dev_attr_rxbuf_cur.attr,
2422 NULL
2423};
0d160211 2424
27b917e5
TI
2425static const struct attribute_group xennet_dev_group = {
2426 .attrs = xennet_dev_attrs
2427};
0d160211
JF
2428#endif /* CONFIG_SYSFS */
2429
c2c63310 2430static void xennet_bus_close(struct xenbus_device *dev)
0d160211 2431{
c2c63310 2432 int ret;
0d160211 2433
c2c63310
AR
2434 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2435 return;
2436 do {
5b5971df 2437 xenbus_switch_state(dev, XenbusStateClosing);
c2c63310
AR
2438 ret = wait_event_timeout(module_wq,
2439 xenbus_read_driver_state(dev->otherend) ==
2440 XenbusStateClosing ||
2441 xenbus_read_driver_state(dev->otherend) ==
2442 XenbusStateClosed ||
2443 xenbus_read_driver_state(dev->otherend) ==
2444 XenbusStateUnknown,
2445 XENNET_TIMEOUT);
2446 } while (!ret);
2447
2448 if (xenbus_read_driver_state(dev->otherend) == XenbusStateClosed)
2449 return;
5b5971df 2450
c2c63310 2451 do {
5b5971df 2452 xenbus_switch_state(dev, XenbusStateClosed);
c2c63310
AR
2453 ret = wait_event_timeout(module_wq,
2454 xenbus_read_driver_state(dev->otherend) ==
2455 XenbusStateClosed ||
2456 xenbus_read_driver_state(dev->otherend) ==
2457 XenbusStateUnknown,
2458 XENNET_TIMEOUT);
2459 } while (!ret);
2460}
2461
2462static int xennet_remove(struct xenbus_device *dev)
2463{
2464 struct netfront_info *info = dev_get_drvdata(&dev->dev);
5b5971df 2465
c2c63310 2466 xennet_bus_close(dev);
0d160211
JF
2467 xennet_disconnect_backend(info);
2468
f599c64f
RL
2469 if (info->netdev->reg_state == NETREG_REGISTERED)
2470 unregister_netdev(info->netdev);
6bc96d04 2471
f599c64f
RL
2472 if (info->queues) {
2473 rtnl_lock();
9a873c71 2474 xennet_destroy_queues(info);
f599c64f
RL
2475 rtnl_unlock();
2476 }
900e1833 2477 xennet_free_netdev(info->netdev);
0d160211
JF
2478
2479 return 0;
2480}
2481
95afae48
DV
2482static const struct xenbus_device_id netfront_ids[] = {
2483 { "vif" },
2484 { "" }
2485};
2486
2487static struct xenbus_driver netfront_driver = {
2488 .ids = netfront_ids,
0d160211 2489 .probe = netfront_probe,
8e0e46bb 2490 .remove = xennet_remove,
0d160211 2491 .resume = netfront_resume,
f502bf2b 2492 .otherend_changed = netback_changed,
95afae48 2493};
0d160211
JF
2494
2495static int __init netif_init(void)
2496{
6e833587 2497 if (!xen_domain())
0d160211
JF
2498 return -ENODEV;
2499
51c71a3b 2500 if (!xen_has_pv_nic_devices())
b9136d20
IM
2501 return -ENODEV;
2502
383eda32 2503 pr_info("Initialising Xen virtual ethernet driver\n");
0d160211 2504
034702a6 2505 /* Allow as many queues as there are CPUs inut max. 8 if user has not
32a84405
WL
2506 * specified a value.
2507 */
2508 if (xennet_max_queues == 0)
034702a6
JG
2509 xennet_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
2510 num_online_cpus());
50ee6061 2511
ffb78a26 2512 return xenbus_register_frontend(&netfront_driver);
0d160211
JF
2513}
2514module_init(netif_init);
2515
2516
2517static void __exit netif_exit(void)
2518{
ffb78a26 2519 xenbus_unregister_driver(&netfront_driver);
0d160211
JF
2520}
2521module_exit(netif_exit);
2522
2523MODULE_DESCRIPTION("Xen virtual network device frontend");
2524MODULE_LICENSE("GPL");
d2f0c52b 2525MODULE_ALIAS("xen:vif");
4f93f09b 2526MODULE_ALIAS("xennet");