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