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