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