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