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