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