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