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