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