blob: 0cfe4ccf92d21b12eb78e59ff3329fdf6d69abed [file] [log] [blame]
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001/*
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#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/ethtool.h>
38#include <linux/if_ether.h>
39#include <linux/tcp.h>
40#include <linux/udp.h>
41#include <linux/moduleparam.h>
42#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070044#include <net/ip.h>
45
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070046#include <xen/xen.h>
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070047#include <xen/xenbus.h>
48#include <xen/events.h>
49#include <xen/page.h>
50#include <xen/grant_table.h>
51
52#include <xen/interface/io/netif.h>
53#include <xen/interface/memory.h>
54#include <xen/interface/grant_table.h>
55
Stephen Hemminger0fc0b732009-09-02 01:03:33 -070056static const struct ethtool_ops xennet_ethtool_ops;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070057
58struct netfront_cb {
59 struct page *page;
60 unsigned offset;
61};
62
63#define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
64
65#define RX_COPY_THRESHOLD 256
66
67#define GRANT_INVALID_REF 0
68
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080069#define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
70#define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070071#define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
72
73struct netfront_info {
74 struct list_head list;
75 struct net_device *netdev;
76
Stephen Hemmingerbea33482007-10-03 16:41:36 -070077 struct napi_struct napi;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070078
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -070079 unsigned int evtchn;
80 struct xenbus_device *xbdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070081
82 spinlock_t tx_lock;
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -070083 struct xen_netif_tx_front_ring tx;
84 int tx_ring_ref;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070085
86 /*
87 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
88 * are linked from tx_skb_freelist through skb_entry.link.
89 *
90 * NB. Freelist index entries are always going to be less than
91 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
92 * greater than PAGE_OFFSET: we use this property to distinguish
93 * them.
94 */
95 union skb_entry {
96 struct sk_buff *skb;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -070097 unsigned long link;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -070098 } tx_skbs[NET_TX_RING_SIZE];
99 grant_ref_t gref_tx_head;
100 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
101 unsigned tx_skb_freelist;
102
Jeremy Fitzhardinge84284d32007-10-15 12:59:53 -0700103 spinlock_t rx_lock ____cacheline_aligned_in_smp;
104 struct xen_netif_rx_front_ring rx;
105 int rx_ring_ref;
106
107 /* Receive-ring batched refills. */
108#define RX_MIN_TARGET 8
109#define RX_DFL_MIN_TARGET 64
110#define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
111 unsigned rx_min_target, rx_max_target, rx_target;
112 struct sk_buff_head rx_batch;
113
114 struct timer_list rx_refill_timer;
115
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700116 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
117 grant_ref_t gref_rx_head;
118 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
119
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700120 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
121 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
122 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000123
124 /* Statistics */
Ian Campbelle9a799e2011-03-10 07:04:18 +0000125 unsigned long rx_gso_checksum_fixup;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700126};
127
128struct netfront_rx_info {
129 struct xen_netif_rx_response rx;
130 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
131};
132
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700133static void skb_entry_set_link(union skb_entry *list, unsigned short id)
134{
135 list->link = id;
136}
137
138static int skb_entry_is_link(const union skb_entry *list)
139{
140 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
Eric Dumazet807540b2010-09-23 05:40:09 +0000141 return (unsigned long)list->skb < PAGE_OFFSET;
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700142}
143
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700144/*
145 * Access macros for acquiring freeing slots in tx_skbs[].
146 */
147
148static void add_id_to_freelist(unsigned *head, union skb_entry *list,
149 unsigned short id)
150{
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -0700151 skb_entry_set_link(&list[id], *head);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700152 *head = id;
153}
154
155static unsigned short get_id_from_freelist(unsigned *head,
156 union skb_entry *list)
157{
158 unsigned int id = *head;
159 *head = list[id].link;
160 return id;
161}
162
163static int xennet_rxidx(RING_IDX idx)
164{
165 return idx & (NET_RX_RING_SIZE - 1);
166}
167
168static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
169 RING_IDX ri)
170{
171 int i = xennet_rxidx(ri);
172 struct sk_buff *skb = np->rx_skbs[i];
173 np->rx_skbs[i] = NULL;
174 return skb;
175}
176
177static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
178 RING_IDX ri)
179{
180 int i = xennet_rxidx(ri);
181 grant_ref_t ref = np->grant_rx_ref[i];
182 np->grant_rx_ref[i] = GRANT_INVALID_REF;
183 return ref;
184}
185
186#ifdef CONFIG_SYSFS
187static int xennet_sysfs_addif(struct net_device *netdev);
188static void xennet_sysfs_delif(struct net_device *netdev);
189#else /* !CONFIG_SYSFS */
190#define xennet_sysfs_addif(dev) (0)
191#define xennet_sysfs_delif(dev) do { } while (0)
192#endif
193
194static int xennet_can_sg(struct net_device *dev)
195{
196 return dev->features & NETIF_F_SG;
197}
198
199
200static void rx_refill_timeout(unsigned long data)
201{
202 struct net_device *dev = (struct net_device *)data;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700203 struct netfront_info *np = netdev_priv(dev);
Ben Hutchings288379f2009-01-19 16:43:59 -0800204 napi_schedule(&np->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700205}
206
207static int netfront_tx_slot_available(struct netfront_info *np)
208{
Eric Dumazet807540b2010-09-23 05:40:09 +0000209 return (np->tx.req_prod_pvt - np->tx.rsp_cons) <
210 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700211}
212
213static void xennet_maybe_wake_tx(struct net_device *dev)
214{
215 struct netfront_info *np = netdev_priv(dev);
216
217 if (unlikely(netif_queue_stopped(dev)) &&
218 netfront_tx_slot_available(np) &&
219 likely(netif_running(dev)))
220 netif_wake_queue(dev);
221}
222
223static void xennet_alloc_rx_buffers(struct net_device *dev)
224{
225 unsigned short id;
226 struct netfront_info *np = netdev_priv(dev);
227 struct sk_buff *skb;
228 struct page *page;
229 int i, batch_target, notify;
230 RING_IDX req_prod = np->rx.req_prod_pvt;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700231 grant_ref_t ref;
232 unsigned long pfn;
233 void *vaddr;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700234 struct xen_netif_rx_request *req;
235
236 if (unlikely(!netif_carrier_ok(dev)))
237 return;
238
239 /*
240 * Allocate skbuffs greedily, even though we batch updates to the
241 * receive ring. This creates a less bursty demand on the memory
242 * allocator, so should reduce the chance of failed allocation requests
243 * both for ourself and for other kernel subsystems.
244 */
245 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
246 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
Isaku Yamahata617a20b2008-10-14 17:50:42 -0700247 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD + NET_IP_ALIGN,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700248 GFP_ATOMIC | __GFP_NOWARN);
249 if (unlikely(!skb))
250 goto no_skb;
251
Isaku Yamahata617a20b2008-10-14 17:50:42 -0700252 /* Align ip header to a 16 bytes boundary */
253 skb_reserve(skb, NET_IP_ALIGN);
254
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700255 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
256 if (!page) {
257 kfree_skb(skb);
258no_skb:
259 /* Any skbuffs queued for refill? Force them out. */
260 if (i != 0)
261 goto refill;
262 /* Could not allocate any skbuffs. Try again later. */
263 mod_timer(&np->rx_refill_timer,
264 jiffies + (HZ/10));
265 break;
266 }
267
268 skb_shinfo(skb)->frags[0].page = page;
269 skb_shinfo(skb)->nr_frags = 1;
270 __skb_queue_tail(&np->rx_batch, skb);
271 }
272
273 /* Is the batch large enough to be worthwhile? */
274 if (i < (np->rx_target/2)) {
275 if (req_prod > np->rx.sring->req_prod)
276 goto push;
277 return;
278 }
279
280 /* Adjust our fill target if we risked running out of buffers. */
281 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
282 ((np->rx_target *= 2) > np->rx_max_target))
283 np->rx_target = np->rx_max_target;
284
285 refill:
Jeremy Fitzhardinge5dcddfa2007-08-07 14:56:42 -0700286 for (i = 0; ; i++) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700287 skb = __skb_dequeue(&np->rx_batch);
288 if (skb == NULL)
289 break;
290
291 skb->dev = dev;
292
293 id = xennet_rxidx(req_prod + i);
294
295 BUG_ON(np->rx_skbs[id]);
296 np->rx_skbs[id] = skb;
297
298 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
299 BUG_ON((signed short)ref < 0);
300 np->grant_rx_ref[id] = ref;
301
302 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
303 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
304
305 req = RING_GET_REQUEST(&np->rx, req_prod + i);
306 gnttab_grant_foreign_access_ref(ref,
307 np->xbdev->otherend_id,
308 pfn_to_mfn(pfn),
309 0);
310
311 req->id = id;
312 req->gref = ref;
313 }
314
Jeremy Fitzhardinge5dcddfa2007-08-07 14:56:42 -0700315 wmb(); /* barrier so backend seens requests */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700316
317 /* Above is a suitable barrier to ensure backend will see requests. */
318 np->rx.req_prod_pvt = req_prod + i;
319 push:
320 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
321 if (notify)
322 notify_remote_via_irq(np->netdev->irq);
323}
324
325static int xennet_open(struct net_device *dev)
326{
327 struct netfront_info *np = netdev_priv(dev);
328
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700329 napi_enable(&np->napi);
330
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700331 spin_lock_bh(&np->rx_lock);
332 if (netif_carrier_ok(dev)) {
333 xennet_alloc_rx_buffers(dev);
334 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
335 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
Ben Hutchings288379f2009-01-19 16:43:59 -0800336 napi_schedule(&np->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700337 }
338 spin_unlock_bh(&np->rx_lock);
339
Eduardo Habkost0b1ab1b2008-07-31 17:36:55 -0300340 netif_start_queue(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700341
342 return 0;
343}
344
345static void xennet_tx_buf_gc(struct net_device *dev)
346{
347 RING_IDX cons, prod;
348 unsigned short id;
349 struct netfront_info *np = netdev_priv(dev);
350 struct sk_buff *skb;
351
352 BUG_ON(!netif_carrier_ok(dev));
353
354 do {
355 prod = np->tx.sring->rsp_prod;
356 rmb(); /* Ensure we see responses up to 'rp'. */
357
358 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
359 struct xen_netif_tx_response *txrsp;
360
361 txrsp = RING_GET_RESPONSE(&np->tx, cons);
Ian Campbellf942dc22011-03-15 00:06:18 +0000362 if (txrsp->status == XEN_NETIF_RSP_NULL)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700363 continue;
364
365 id = txrsp->id;
366 skb = np->tx_skbs[id].skb;
367 if (unlikely(gnttab_query_foreign_access(
368 np->grant_tx_ref[id]) != 0)) {
369 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
370 "-- grant still in use by backend "
371 "domain.\n");
372 BUG();
373 }
374 gnttab_end_foreign_access_ref(
375 np->grant_tx_ref[id], GNTMAP_readonly);
376 gnttab_release_grant_reference(
377 &np->gref_tx_head, np->grant_tx_ref[id]);
378 np->grant_tx_ref[id] = GRANT_INVALID_REF;
379 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
380 dev_kfree_skb_irq(skb);
381 }
382
383 np->tx.rsp_cons = prod;
384
385 /*
386 * Set a new event, then check for race with update of tx_cons.
387 * Note that it is essential to schedule a callback, no matter
388 * how few buffers are pending. Even if there is space in the
389 * transmit ring, higher layers may be blocked because too much
390 * data is outstanding: in such cases notification from Xen is
391 * likely to be the only kick that we'll get.
392 */
393 np->tx.sring->rsp_event =
394 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
395 mb(); /* update shared area */
396 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
397
398 xennet_maybe_wake_tx(dev);
399}
400
401static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
402 struct xen_netif_tx_request *tx)
403{
404 struct netfront_info *np = netdev_priv(dev);
405 char *data = skb->data;
406 unsigned long mfn;
407 RING_IDX prod = np->tx.req_prod_pvt;
408 int frags = skb_shinfo(skb)->nr_frags;
409 unsigned int offset = offset_in_page(data);
410 unsigned int len = skb_headlen(skb);
411 unsigned int id;
412 grant_ref_t ref;
413 int i;
414
415 /* While the header overlaps a page boundary (including being
416 larger than a page), split it it into page-sized chunks. */
417 while (len > PAGE_SIZE - offset) {
418 tx->size = PAGE_SIZE - offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000419 tx->flags |= XEN_NETTXF_more_data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700420 len -= tx->size;
421 data += tx->size;
422 offset = 0;
423
424 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
425 np->tx_skbs[id].skb = skb_get(skb);
426 tx = RING_GET_REQUEST(&np->tx, prod++);
427 tx->id = id;
428 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
429 BUG_ON((signed short)ref < 0);
430
431 mfn = virt_to_mfn(data);
432 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
433 mfn, GNTMAP_readonly);
434
435 tx->gref = np->grant_tx_ref[id] = ref;
436 tx->offset = offset;
437 tx->size = len;
438 tx->flags = 0;
439 }
440
441 /* Grant backend access to each skb fragment page. */
442 for (i = 0; i < frags; i++) {
443 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
444
Ian Campbellf942dc22011-03-15 00:06:18 +0000445 tx->flags |= XEN_NETTXF_more_data;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700446
447 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
448 np->tx_skbs[id].skb = skb_get(skb);
449 tx = RING_GET_REQUEST(&np->tx, prod++);
450 tx->id = id;
451 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
452 BUG_ON((signed short)ref < 0);
453
454 mfn = pfn_to_mfn(page_to_pfn(frag->page));
455 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
456 mfn, GNTMAP_readonly);
457
458 tx->gref = np->grant_tx_ref[id] = ref;
459 tx->offset = frag->page_offset;
460 tx->size = frag->size;
461 tx->flags = 0;
462 }
463
464 np->tx.req_prod_pvt = prod;
465}
466
467static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
468{
469 unsigned short id;
470 struct netfront_info *np = netdev_priv(dev);
471 struct xen_netif_tx_request *tx;
472 struct xen_netif_extra_info *extra;
473 char *data = skb->data;
474 RING_IDX i;
475 grant_ref_t ref;
476 unsigned long mfn;
477 int notify;
478 int frags = skb_shinfo(skb)->nr_frags;
479 unsigned int offset = offset_in_page(data);
480 unsigned int len = skb_headlen(skb);
481
Julia Lawall43526372008-10-15 22:05:23 -0700482 frags += DIV_ROUND_UP(offset + len, PAGE_SIZE);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700483 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
484 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
485 frags);
486 dump_stack();
487 goto drop;
488 }
489
490 spin_lock_irq(&np->tx_lock);
491
492 if (unlikely(!netif_carrier_ok(dev) ||
493 (frags > 1 && !xennet_can_sg(dev)) ||
Jesse Grossfc741212011-01-09 06:23:32 +0000494 netif_needs_gso(skb, netif_skb_features(skb)))) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700495 spin_unlock_irq(&np->tx_lock);
496 goto drop;
497 }
498
499 i = np->tx.req_prod_pvt;
500
501 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
502 np->tx_skbs[id].skb = skb;
503
504 tx = RING_GET_REQUEST(&np->tx, i);
505
506 tx->id = id;
507 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
508 BUG_ON((signed short)ref < 0);
509 mfn = virt_to_mfn(data);
510 gnttab_grant_foreign_access_ref(
511 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
512 tx->gref = np->grant_tx_ref[id] = ref;
513 tx->offset = offset;
514 tx->size = len;
515 extra = NULL;
516
517 tx->flags = 0;
518 if (skb->ip_summed == CHECKSUM_PARTIAL)
519 /* local packet? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000520 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700521 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
522 /* remote but checksummed. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000523 tx->flags |= XEN_NETTXF_data_validated;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700524
525 if (skb_shinfo(skb)->gso_size) {
526 struct xen_netif_extra_info *gso;
527
528 gso = (struct xen_netif_extra_info *)
529 RING_GET_REQUEST(&np->tx, ++i);
530
531 if (extra)
532 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
533 else
Ian Campbellf942dc22011-03-15 00:06:18 +0000534 tx->flags |= XEN_NETTXF_extra_info;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700535
536 gso->u.gso.size = skb_shinfo(skb)->gso_size;
537 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
538 gso->u.gso.pad = 0;
539 gso->u.gso.features = 0;
540
541 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
542 gso->flags = 0;
543 extra = gso;
544 }
545
546 np->tx.req_prod_pvt = i + 1;
547
548 xennet_make_frags(skb, dev, tx);
549 tx->size = skb->len;
550
551 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
552 if (notify)
553 notify_remote_via_irq(np->netdev->irq);
554
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700555 dev->stats.tx_bytes += skb->len;
556 dev->stats.tx_packets++;
Jeremy Fitzhardinge10a273a2007-08-13 12:54:37 -0700557
558 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700559 xennet_tx_buf_gc(dev);
560
561 if (!netfront_tx_slot_available(np))
562 netif_stop_queue(dev);
563
564 spin_unlock_irq(&np->tx_lock);
565
Patrick McHardy6ed10652009-06-23 06:03:08 +0000566 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700567
568 drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700569 dev->stats.tx_dropped++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700570 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000571 return NETDEV_TX_OK;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700572}
573
574static int xennet_close(struct net_device *dev)
575{
576 struct netfront_info *np = netdev_priv(dev);
577 netif_stop_queue(np->netdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700578 napi_disable(&np->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700579 return 0;
580}
581
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700582static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
583 grant_ref_t ref)
584{
585 int new = xennet_rxidx(np->rx.req_prod_pvt);
586
587 BUG_ON(np->rx_skbs[new]);
588 np->rx_skbs[new] = skb;
589 np->grant_rx_ref[new] = ref;
590 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
591 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
592 np->rx.req_prod_pvt++;
593}
594
595static int xennet_get_extras(struct netfront_info *np,
596 struct xen_netif_extra_info *extras,
597 RING_IDX rp)
598
599{
600 struct xen_netif_extra_info *extra;
601 struct device *dev = &np->netdev->dev;
602 RING_IDX cons = np->rx.rsp_cons;
603 int err = 0;
604
605 do {
606 struct sk_buff *skb;
607 grant_ref_t ref;
608
609 if (unlikely(cons + 1 == rp)) {
610 if (net_ratelimit())
611 dev_warn(dev, "Missing extra info\n");
612 err = -EBADR;
613 break;
614 }
615
616 extra = (struct xen_netif_extra_info *)
617 RING_GET_RESPONSE(&np->rx, ++cons);
618
619 if (unlikely(!extra->type ||
620 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
621 if (net_ratelimit())
622 dev_warn(dev, "Invalid extra type: %d\n",
623 extra->type);
624 err = -EINVAL;
625 } else {
626 memcpy(&extras[extra->type - 1], extra,
627 sizeof(*extra));
628 }
629
630 skb = xennet_get_rx_skb(np, cons);
631 ref = xennet_get_rx_ref(np, cons);
632 xennet_move_rx_slot(np, skb, ref);
633 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
634
635 np->rx.rsp_cons = cons;
636 return err;
637}
638
639static int xennet_get_responses(struct netfront_info *np,
640 struct netfront_rx_info *rinfo, RING_IDX rp,
641 struct sk_buff_head *list)
642{
643 struct xen_netif_rx_response *rx = &rinfo->rx;
644 struct xen_netif_extra_info *extras = rinfo->extras;
645 struct device *dev = &np->netdev->dev;
646 RING_IDX cons = np->rx.rsp_cons;
647 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
648 grant_ref_t ref = xennet_get_rx_ref(np, cons);
649 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
650 int frags = 1;
651 int err = 0;
652 unsigned long ret;
653
Ian Campbellf942dc22011-03-15 00:06:18 +0000654 if (rx->flags & XEN_NETRXF_extra_info) {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700655 err = xennet_get_extras(np, extras, rp);
656 cons = np->rx.rsp_cons;
657 }
658
659 for (;;) {
660 if (unlikely(rx->status < 0 ||
661 rx->offset + rx->status > PAGE_SIZE)) {
662 if (net_ratelimit())
663 dev_warn(dev, "rx->offset: %x, size: %u\n",
664 rx->offset, rx->status);
665 xennet_move_rx_slot(np, skb, ref);
666 err = -EINVAL;
667 goto next;
668 }
669
670 /*
671 * This definitely indicates a bug, either in this driver or in
672 * the backend driver. In future this should flag the bad
673 * situation to the system controller to reboot the backed.
674 */
675 if (ref == GRANT_INVALID_REF) {
676 if (net_ratelimit())
677 dev_warn(dev, "Bad rx response id %d.\n",
678 rx->id);
679 err = -EINVAL;
680 goto next;
681 }
682
683 ret = gnttab_end_foreign_access_ref(ref, 0);
684 BUG_ON(!ret);
685
686 gnttab_release_grant_reference(&np->gref_rx_head, ref);
687
688 __skb_queue_tail(list, skb);
689
690next:
Ian Campbellf942dc22011-03-15 00:06:18 +0000691 if (!(rx->flags & XEN_NETRXF_more_data))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700692 break;
693
694 if (cons + frags == rp) {
695 if (net_ratelimit())
696 dev_warn(dev, "Need more frags\n");
697 err = -ENOENT;
698 break;
699 }
700
701 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
702 skb = xennet_get_rx_skb(np, cons + frags);
703 ref = xennet_get_rx_ref(np, cons + frags);
704 frags++;
705 }
706
707 if (unlikely(frags > max)) {
708 if (net_ratelimit())
709 dev_warn(dev, "Too many frags\n");
710 err = -E2BIG;
711 }
712
713 if (unlikely(err))
714 np->rx.rsp_cons = cons + frags;
715
716 return err;
717}
718
719static int xennet_set_skb_gso(struct sk_buff *skb,
720 struct xen_netif_extra_info *gso)
721{
722 if (!gso->u.gso.size) {
723 if (net_ratelimit())
724 printk(KERN_WARNING "GSO size must not be zero.\n");
725 return -EINVAL;
726 }
727
728 /* Currently only TCPv4 S.O. is supported. */
729 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
730 if (net_ratelimit())
731 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
732 return -EINVAL;
733 }
734
735 skb_shinfo(skb)->gso_size = gso->u.gso.size;
736 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
737
738 /* Header must be checked, and gso_segs computed. */
739 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
740 skb_shinfo(skb)->gso_segs = 0;
741
742 return 0;
743}
744
745static RING_IDX xennet_fill_frags(struct netfront_info *np,
746 struct sk_buff *skb,
747 struct sk_buff_head *list)
748{
749 struct skb_shared_info *shinfo = skb_shinfo(skb);
750 int nr_frags = shinfo->nr_frags;
751 RING_IDX cons = np->rx.rsp_cons;
752 skb_frag_t *frag = shinfo->frags + nr_frags;
753 struct sk_buff *nskb;
754
755 while ((nskb = __skb_dequeue(list))) {
756 struct xen_netif_rx_response *rx =
757 RING_GET_RESPONSE(&np->rx, ++cons);
758
759 frag->page = skb_shinfo(nskb)->frags[0].page;
760 frag->page_offset = rx->offset;
761 frag->size = rx->status;
762
763 skb->data_len += rx->status;
764
765 skb_shinfo(nskb)->nr_frags = 0;
766 kfree_skb(nskb);
767
768 frag++;
769 nr_frags++;
770 }
771
772 shinfo->nr_frags = nr_frags;
773 return cons;
774}
775
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000776static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700777{
778 struct iphdr *iph;
779 unsigned char *th;
780 int err = -EPROTO;
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000781 int recalculate_partial_csum = 0;
782
783 /*
784 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
785 * peers can fail to set NETRXF_csum_blank when sending a GSO
786 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
787 * recalculate the partial checksum.
788 */
789 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
790 struct netfront_info *np = netdev_priv(dev);
791 np->rx_gso_checksum_fixup++;
792 skb->ip_summed = CHECKSUM_PARTIAL;
793 recalculate_partial_csum = 1;
794 }
795
796 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
797 if (skb->ip_summed != CHECKSUM_PARTIAL)
798 return 0;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700799
800 if (skb->protocol != htons(ETH_P_IP))
801 goto out;
802
803 iph = (void *)skb->data;
804 th = skb->data + 4 * iph->ihl;
805 if (th >= skb_tail_pointer(skb))
806 goto out;
807
808 skb->csum_start = th - skb->head;
809 switch (iph->protocol) {
810 case IPPROTO_TCP:
811 skb->csum_offset = offsetof(struct tcphdr, check);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000812
813 if (recalculate_partial_csum) {
814 struct tcphdr *tcph = (struct tcphdr *)th;
815 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
816 skb->len - iph->ihl*4,
817 IPPROTO_TCP, 0);
818 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700819 break;
820 case IPPROTO_UDP:
821 skb->csum_offset = offsetof(struct udphdr, check);
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000822
823 if (recalculate_partial_csum) {
824 struct udphdr *udph = (struct udphdr *)th;
825 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
826 skb->len - iph->ihl*4,
827 IPPROTO_UDP, 0);
828 }
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700829 break;
830 default:
831 if (net_ratelimit())
832 printk(KERN_ERR "Attempting to checksum a non-"
833 "TCP/UDP packet, dropping a protocol"
834 " %d packet", iph->protocol);
835 goto out;
836 }
837
838 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
839 goto out;
840
841 err = 0;
842
843out:
844 return err;
845}
846
847static int handle_incoming_queue(struct net_device *dev,
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700848 struct sk_buff_head *rxq)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700849{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700850 int packets_dropped = 0;
851 struct sk_buff *skb;
852
853 while ((skb = __skb_dequeue(rxq)) != NULL) {
854 struct page *page = NETFRONT_SKB_CB(skb)->page;
855 void *vaddr = page_address(page);
856 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
857
858 memcpy(skb->data, vaddr + offset,
859 skb_headlen(skb));
860
861 if (page != skb_shinfo(skb)->frags[0].page)
862 __free_page(page);
863
864 /* Ethernet work: Delayed to here as it peeks the header. */
865 skb->protocol = eth_type_trans(skb, dev);
866
Ian Campbelle0ce4af2011-01-27 04:14:03 +0000867 if (checksum_setup(dev, skb)) {
868 kfree_skb(skb);
869 packets_dropped++;
870 dev->stats.rx_errors++;
871 continue;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700872 }
873
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700874 dev->stats.rx_packets++;
875 dev->stats.rx_bytes += skb->len;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700876
877 /* Pass it up. */
878 netif_receive_skb(skb);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700879 }
880
881 return packets_dropped;
882}
883
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700884static int xennet_poll(struct napi_struct *napi, int budget)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700885{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700886 struct netfront_info *np = container_of(napi, struct netfront_info, napi);
887 struct net_device *dev = np->netdev;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700888 struct sk_buff *skb;
889 struct netfront_rx_info rinfo;
890 struct xen_netif_rx_response *rx = &rinfo.rx;
891 struct xen_netif_extra_info *extras = rinfo.extras;
892 RING_IDX i, rp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700893 int work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700894 struct sk_buff_head rxq;
895 struct sk_buff_head errq;
896 struct sk_buff_head tmpq;
897 unsigned long flags;
898 unsigned int len;
899 int err;
900
901 spin_lock(&np->rx_lock);
902
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700903 skb_queue_head_init(&rxq);
904 skb_queue_head_init(&errq);
905 skb_queue_head_init(&tmpq);
906
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700907 rp = np->rx.sring->rsp_prod;
908 rmb(); /* Ensure we see queued responses up to 'rp'. */
909
910 i = np->rx.rsp_cons;
911 work_done = 0;
912 while ((i != rp) && (work_done < budget)) {
913 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
914 memset(extras, 0, sizeof(rinfo.extras));
915
916 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
917
918 if (unlikely(err)) {
919err:
920 while ((skb = __skb_dequeue(&tmpq)))
921 __skb_queue_tail(&errq, skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700922 dev->stats.rx_errors++;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700923 i = np->rx.rsp_cons;
924 continue;
925 }
926
927 skb = __skb_dequeue(&tmpq);
928
929 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
930 struct xen_netif_extra_info *gso;
931 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
932
933 if (unlikely(xennet_set_skb_gso(skb, gso))) {
934 __skb_queue_head(&tmpq, skb);
935 np->rx.rsp_cons += skb_queue_len(&tmpq);
936 goto err;
937 }
938 }
939
940 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
941 NETFRONT_SKB_CB(skb)->offset = rx->offset;
942
943 len = rx->status;
944 if (len > RX_COPY_THRESHOLD)
945 len = RX_COPY_THRESHOLD;
946 skb_put(skb, len);
947
948 if (rx->status > len) {
949 skb_shinfo(skb)->frags[0].page_offset =
950 rx->offset + len;
951 skb_shinfo(skb)->frags[0].size = rx->status - len;
952 skb->data_len = rx->status - len;
953 } else {
954 skb_shinfo(skb)->frags[0].page = NULL;
955 skb_shinfo(skb)->nr_frags = 0;
956 }
957
958 i = xennet_fill_frags(np, skb, &tmpq);
959
960 /*
961 * Truesize approximates the size of true data plus
962 * any supervisor overheads. Adding hypervisor
963 * overheads has been shown to significantly reduce
964 * achievable bandwidth with the default receive
965 * buffer size. It is therefore not wise to account
966 * for it here.
967 *
968 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
969 * to RX_COPY_THRESHOLD + the supervisor
970 * overheads. Here, we add the size of the data pulled
971 * in xennet_fill_frags().
972 *
973 * We also adjust for any unused space in the main
974 * data area by subtracting (RX_COPY_THRESHOLD -
975 * len). This is especially important with drivers
976 * which split incoming packets into header and data,
977 * using only 66 bytes of the main data area (see the
978 * e1000 driver for example.) On such systems,
979 * without this last adjustement, our achievable
980 * receive throughout using the standard receive
981 * buffer size was cut by 25%(!!!).
982 */
983 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
984 skb->len += skb->data_len;
985
Ian Campbellf942dc22011-03-15 00:06:18 +0000986 if (rx->flags & XEN_NETRXF_csum_blank)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700987 skb->ip_summed = CHECKSUM_PARTIAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000988 else if (rx->flags & XEN_NETRXF_data_validated)
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700989 skb->ip_summed = CHECKSUM_UNNECESSARY;
990
991 __skb_queue_tail(&rxq, skb);
992
993 np->rx.rsp_cons = ++i;
994 work_done++;
995 }
996
Wang Chen56cfe5d2008-05-22 18:09:06 +0800997 __skb_queue_purge(&errq);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -0700998
999 work_done -= handle_incoming_queue(dev, &rxq);
1000
1001 /* If we get a callback with very few responses, reduce fill target. */
1002 /* NB. Note exponential increase, linear decrease. */
1003 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1004 ((3*np->rx_target) / 4)) &&
1005 (--np->rx_target < np->rx_min_target))
1006 np->rx_target = np->rx_min_target;
1007
1008 xennet_alloc_rx_buffers(dev);
1009
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001010 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001011 int more_to_do = 0;
1012
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001013 local_irq_save(flags);
1014
1015 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1016 if (!more_to_do)
Ben Hutchings288379f2009-01-19 16:43:59 -08001017 __napi_complete(napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001018
1019 local_irq_restore(flags);
1020 }
1021
1022 spin_unlock(&np->rx_lock);
1023
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001024 return work_done;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001025}
1026
1027static int xennet_change_mtu(struct net_device *dev, int mtu)
1028{
1029 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1030
1031 if (mtu > max)
1032 return -EINVAL;
1033 dev->mtu = mtu;
1034 return 0;
1035}
1036
1037static void xennet_release_tx_bufs(struct netfront_info *np)
1038{
1039 struct sk_buff *skb;
1040 int i;
1041
1042 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1043 /* Skip over entries which are actually freelist references */
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -07001044 if (skb_entry_is_link(&np->tx_skbs[i]))
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001045 continue;
1046
1047 skb = np->tx_skbs[i].skb;
1048 gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
1049 GNTMAP_readonly);
1050 gnttab_release_grant_reference(&np->gref_tx_head,
1051 np->grant_tx_ref[i]);
1052 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1053 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
1054 dev_kfree_skb_irq(skb);
1055 }
1056}
1057
1058static void xennet_release_rx_bufs(struct netfront_info *np)
1059{
1060 struct mmu_update *mmu = np->rx_mmu;
1061 struct multicall_entry *mcl = np->rx_mcl;
1062 struct sk_buff_head free_list;
1063 struct sk_buff *skb;
1064 unsigned long mfn;
1065 int xfer = 0, noxfer = 0, unused = 0;
1066 int id, ref;
1067
1068 dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
1069 __func__);
1070 return;
1071
1072 skb_queue_head_init(&free_list);
1073
1074 spin_lock_bh(&np->rx_lock);
1075
1076 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1077 ref = np->grant_rx_ref[id];
1078 if (ref == GRANT_INVALID_REF) {
1079 unused++;
1080 continue;
1081 }
1082
1083 skb = np->rx_skbs[id];
1084 mfn = gnttab_end_foreign_transfer_ref(ref);
1085 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1086 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1087
1088 if (0 == mfn) {
1089 skb_shinfo(skb)->nr_frags = 0;
1090 dev_kfree_skb(skb);
1091 noxfer++;
1092 continue;
1093 }
1094
1095 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1096 /* Remap the page. */
1097 struct page *page = skb_shinfo(skb)->frags[0].page;
1098 unsigned long pfn = page_to_pfn(page);
1099 void *vaddr = page_address(page);
1100
1101 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1102 mfn_pte(mfn, PAGE_KERNEL),
1103 0);
1104 mcl++;
1105 mmu->ptr = ((u64)mfn << PAGE_SHIFT)
1106 | MMU_MACHPHYS_UPDATE;
1107 mmu->val = pfn;
1108 mmu++;
1109
1110 set_phys_to_machine(pfn, mfn);
1111 }
1112 __skb_queue_tail(&free_list, skb);
1113 xfer++;
1114 }
1115
1116 dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
1117 __func__, xfer, noxfer, unused);
1118
1119 if (xfer) {
1120 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1121 /* Do all the remapping work and M2P updates. */
1122 MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
Al Viro79ea13c2008-01-24 02:06:46 -08001123 NULL, DOMID_SELF);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001124 mcl++;
1125 HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
1126 }
1127 }
1128
Wang Chen56cfe5d2008-05-22 18:09:06 +08001129 __skb_queue_purge(&free_list);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001130
1131 spin_unlock_bh(&np->rx_lock);
1132}
1133
1134static void xennet_uninit(struct net_device *dev)
1135{
1136 struct netfront_info *np = netdev_priv(dev);
1137 xennet_release_tx_bufs(np);
1138 xennet_release_rx_bufs(np);
1139 gnttab_free_grant_references(np->gref_tx_head);
1140 gnttab_free_grant_references(np->gref_rx_head);
1141}
1142
Eric Dumazet8f7b01a2011-04-03 17:21:00 -07001143static u32 xennet_fix_features(struct net_device *dev, u32 features)
1144{
1145 struct netfront_info *np = netdev_priv(dev);
1146 int val;
1147
1148 if (features & NETIF_F_SG) {
1149 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1150 "%d", &val) < 0)
1151 val = 0;
1152
1153 if (!val)
1154 features &= ~NETIF_F_SG;
1155 }
1156
1157 if (features & NETIF_F_TSO) {
1158 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1159 "feature-gso-tcpv4", "%d", &val) < 0)
1160 val = 0;
1161
1162 if (!val)
1163 features &= ~NETIF_F_TSO;
1164 }
1165
1166 return features;
1167}
1168
1169static int xennet_set_features(struct net_device *dev, u32 features)
1170{
1171 if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
1172 netdev_info(dev, "Reducing MTU because no SG offload");
1173 dev->mtu = ETH_DATA_LEN;
1174 }
1175
1176 return 0;
1177}
1178
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001179static const struct net_device_ops xennet_netdev_ops = {
1180 .ndo_open = xennet_open,
1181 .ndo_uninit = xennet_uninit,
1182 .ndo_stop = xennet_close,
1183 .ndo_start_xmit = xennet_start_xmit,
1184 .ndo_change_mtu = xennet_change_mtu,
1185 .ndo_set_mac_address = eth_mac_addr,
1186 .ndo_validate_addr = eth_validate_addr,
Michał Mirosławfb507932011-03-31 01:01:35 +00001187 .ndo_fix_features = xennet_fix_features,
1188 .ndo_set_features = xennet_set_features,
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001189};
1190
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001191static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1192{
1193 int i, err;
1194 struct net_device *netdev;
1195 struct netfront_info *np;
1196
1197 netdev = alloc_etherdev(sizeof(struct netfront_info));
1198 if (!netdev) {
1199 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1200 __func__);
1201 return ERR_PTR(-ENOMEM);
1202 }
1203
1204 np = netdev_priv(netdev);
1205 np->xbdev = dev;
1206
1207 spin_lock_init(&np->tx_lock);
1208 spin_lock_init(&np->rx_lock);
1209
1210 skb_queue_head_init(&np->rx_batch);
1211 np->rx_target = RX_DFL_MIN_TARGET;
1212 np->rx_min_target = RX_DFL_MIN_TARGET;
1213 np->rx_max_target = RX_MAX_TARGET;
1214
1215 init_timer(&np->rx_refill_timer);
1216 np->rx_refill_timer.data = (unsigned long)netdev;
1217 np->rx_refill_timer.function = rx_refill_timeout;
1218
1219 /* Initialise tx_skbs as a free chain containing every entry. */
1220 np->tx_skb_freelist = 0;
1221 for (i = 0; i < NET_TX_RING_SIZE; i++) {
Isaku Yamahata1ffb40b2008-07-08 15:06:31 -07001222 skb_entry_set_link(&np->tx_skbs[i], i+1);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001223 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1224 }
1225
1226 /* Clear out rx_skbs */
1227 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1228 np->rx_skbs[i] = NULL;
1229 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1230 }
1231
1232 /* A grant for every tx ring slot */
1233 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1234 &np->gref_tx_head) < 0) {
1235 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1236 err = -ENOMEM;
1237 goto exit;
1238 }
1239 /* A grant for every rx ring slot */
1240 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1241 &np->gref_rx_head) < 0) {
1242 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1243 err = -ENOMEM;
1244 goto exit_free_tx;
1245 }
1246
Stephen Hemminger0a0b9d22009-01-06 10:44:55 -08001247 netdev->netdev_ops = &xennet_netdev_ops;
1248
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001249 netif_napi_add(netdev, &np->napi, xennet_poll, 64);
Michał Mirosławfb507932011-03-31 01:01:35 +00001250 netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
1251 NETIF_F_GSO_ROBUST;
1252 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO;
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001253
1254 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001255 SET_NETDEV_DEV(netdev, &dev->dev);
1256
1257 np->netdev = netdev;
1258
1259 netif_carrier_off(netdev);
1260
1261 return netdev;
1262
1263 exit_free_tx:
1264 gnttab_free_grant_references(np->gref_tx_head);
1265 exit:
1266 free_netdev(netdev);
1267 return ERR_PTR(err);
1268}
1269
1270/**
1271 * Entry point to this code when a new device is created. Allocate the basic
1272 * structures and the ring buffers for communication with the backend, and
1273 * inform the backend of the appropriate details for those.
1274 */
1275static int __devinit netfront_probe(struct xenbus_device *dev,
1276 const struct xenbus_device_id *id)
1277{
1278 int err;
1279 struct net_device *netdev;
1280 struct netfront_info *info;
1281
1282 netdev = xennet_create_dev(dev);
1283 if (IS_ERR(netdev)) {
1284 err = PTR_ERR(netdev);
1285 xenbus_dev_fatal(dev, err, "creating netdev");
1286 return err;
1287 }
1288
1289 info = netdev_priv(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001290 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001291
1292 err = register_netdev(info->netdev);
1293 if (err) {
1294 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1295 __func__, err);
1296 goto fail;
1297 }
1298
1299 err = xennet_sysfs_addif(info->netdev);
1300 if (err) {
1301 unregister_netdev(info->netdev);
1302 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1303 __func__, err);
1304 goto fail;
1305 }
1306
1307 return 0;
1308
1309 fail:
1310 free_netdev(netdev);
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001311 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001312 return err;
1313}
1314
1315static void xennet_end_access(int ref, void *page)
1316{
1317 /* This frees the page as a side-effect */
1318 if (ref != GRANT_INVALID_REF)
1319 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1320}
1321
1322static void xennet_disconnect_backend(struct netfront_info *info)
1323{
1324 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1325 spin_lock_bh(&info->rx_lock);
1326 spin_lock_irq(&info->tx_lock);
1327 netif_carrier_off(info->netdev);
1328 spin_unlock_irq(&info->tx_lock);
1329 spin_unlock_bh(&info->rx_lock);
1330
1331 if (info->netdev->irq)
1332 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1333 info->evtchn = info->netdev->irq = 0;
1334
1335 /* End access and free the pages */
1336 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1337 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1338
1339 info->tx_ring_ref = GRANT_INVALID_REF;
1340 info->rx_ring_ref = GRANT_INVALID_REF;
1341 info->tx.sring = NULL;
1342 info->rx.sring = NULL;
1343}
1344
1345/**
1346 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1347 * driver restart. We tear down our netif structure and recreate it, but
1348 * leave the device-layer structures intact so that this is transparent to the
1349 * rest of the kernel.
1350 */
1351static int netfront_resume(struct xenbus_device *dev)
1352{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001353 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001354
1355 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1356
1357 xennet_disconnect_backend(info);
1358 return 0;
1359}
1360
1361static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1362{
1363 char *s, *e, *macstr;
1364 int i;
1365
1366 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1367 if (IS_ERR(macstr))
1368 return PTR_ERR(macstr);
1369
1370 for (i = 0; i < ETH_ALEN; i++) {
1371 mac[i] = simple_strtoul(s, &e, 16);
1372 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1373 kfree(macstr);
1374 return -ENOENT;
1375 }
1376 s = e+1;
1377 }
1378
1379 kfree(macstr);
1380 return 0;
1381}
1382
1383static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1384{
1385 struct net_device *dev = dev_id;
1386 struct netfront_info *np = netdev_priv(dev);
1387 unsigned long flags;
1388
1389 spin_lock_irqsave(&np->tx_lock, flags);
1390
1391 if (likely(netif_carrier_ok(dev))) {
1392 xennet_tx_buf_gc(dev);
1393 /* Under tx_lock: protects access to rx shared-ring indexes. */
1394 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
Ben Hutchings288379f2009-01-19 16:43:59 -08001395 napi_schedule(&np->napi);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001396 }
1397
1398 spin_unlock_irqrestore(&np->tx_lock, flags);
1399
1400 return IRQ_HANDLED;
1401}
1402
1403static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1404{
1405 struct xen_netif_tx_sring *txs;
1406 struct xen_netif_rx_sring *rxs;
1407 int err;
1408 struct net_device *netdev = info->netdev;
1409
1410 info->tx_ring_ref = GRANT_INVALID_REF;
1411 info->rx_ring_ref = GRANT_INVALID_REF;
1412 info->rx.sring = NULL;
1413 info->tx.sring = NULL;
1414 netdev->irq = 0;
1415
1416 err = xen_net_read_mac(dev, netdev->dev_addr);
1417 if (err) {
1418 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1419 goto fail;
1420 }
1421
Ian Campbella144ff02008-06-17 10:47:08 +02001422 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001423 if (!txs) {
1424 err = -ENOMEM;
1425 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1426 goto fail;
1427 }
1428 SHARED_RING_INIT(txs);
1429 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1430
1431 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1432 if (err < 0) {
1433 free_page((unsigned long)txs);
1434 goto fail;
1435 }
1436
1437 info->tx_ring_ref = err;
Ian Campbella144ff02008-06-17 10:47:08 +02001438 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001439 if (!rxs) {
1440 err = -ENOMEM;
1441 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1442 goto fail;
1443 }
1444 SHARED_RING_INIT(rxs);
1445 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1446
1447 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1448 if (err < 0) {
1449 free_page((unsigned long)rxs);
1450 goto fail;
1451 }
1452 info->rx_ring_ref = err;
1453
1454 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1455 if (err)
1456 goto fail;
1457
1458 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001459 0, netdev->name, netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001460 if (err < 0)
1461 goto fail;
1462 netdev->irq = err;
1463 return 0;
1464
1465 fail:
1466 return err;
1467}
1468
1469/* Common code used when first setting up, and when resuming. */
Ian Campbellf502bf22010-08-18 23:27:49 +00001470static int talk_to_netback(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001471 struct netfront_info *info)
1472{
1473 const char *message;
1474 struct xenbus_transaction xbt;
1475 int err;
1476
1477 /* Create shared ring, alloc event channel. */
1478 err = setup_netfront(dev, info);
1479 if (err)
1480 goto out;
1481
1482again:
1483 err = xenbus_transaction_start(&xbt);
1484 if (err) {
1485 xenbus_dev_fatal(dev, err, "starting transaction");
1486 goto destroy_ring;
1487 }
1488
1489 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1490 info->tx_ring_ref);
1491 if (err) {
1492 message = "writing tx ring-ref";
1493 goto abort_transaction;
1494 }
1495 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1496 info->rx_ring_ref);
1497 if (err) {
1498 message = "writing rx ring-ref";
1499 goto abort_transaction;
1500 }
1501 err = xenbus_printf(xbt, dev->nodename,
1502 "event-channel", "%u", info->evtchn);
1503 if (err) {
1504 message = "writing event-channel";
1505 goto abort_transaction;
1506 }
1507
1508 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1509 1);
1510 if (err) {
1511 message = "writing request-rx-copy";
1512 goto abort_transaction;
1513 }
1514
1515 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1516 if (err) {
1517 message = "writing feature-rx-notify";
1518 goto abort_transaction;
1519 }
1520
1521 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1522 if (err) {
1523 message = "writing feature-sg";
1524 goto abort_transaction;
1525 }
1526
1527 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1528 if (err) {
1529 message = "writing feature-gso-tcpv4";
1530 goto abort_transaction;
1531 }
1532
1533 err = xenbus_transaction_end(xbt, 0);
1534 if (err) {
1535 if (err == -EAGAIN)
1536 goto again;
1537 xenbus_dev_fatal(dev, err, "completing transaction");
1538 goto destroy_ring;
1539 }
1540
1541 return 0;
1542
1543 abort_transaction:
1544 xenbus_transaction_end(xbt, 1);
1545 xenbus_dev_fatal(dev, err, "%s", message);
1546 destroy_ring:
1547 xennet_disconnect_backend(info);
1548 out:
1549 return err;
1550}
1551
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001552static int xennet_connect(struct net_device *dev)
1553{
1554 struct netfront_info *np = netdev_priv(dev);
1555 int i, requeue_idx, err;
1556 struct sk_buff *skb;
1557 grant_ref_t ref;
1558 struct xen_netif_rx_request *req;
1559 unsigned int feature_rx_copy;
1560
1561 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1562 "feature-rx-copy", "%u", &feature_rx_copy);
1563 if (err != 1)
1564 feature_rx_copy = 0;
1565
1566 if (!feature_rx_copy) {
1567 dev_info(&dev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07001568 "backend does not support copying receive path\n");
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001569 return -ENODEV;
1570 }
1571
Ian Campbellf502bf22010-08-18 23:27:49 +00001572 err = talk_to_netback(np->xbdev, np);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001573 if (err)
1574 return err;
1575
Michał Mirosławfb507932011-03-31 01:01:35 +00001576 netdev_update_features(dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001577
1578 spin_lock_bh(&np->rx_lock);
1579 spin_lock_irq(&np->tx_lock);
1580
1581 /* Step 1: Discard all pending TX packet fragments. */
1582 xennet_release_tx_bufs(np);
1583
1584 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1585 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1586 if (!np->rx_skbs[i])
1587 continue;
1588
1589 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1590 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1591 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1592
1593 gnttab_grant_foreign_access_ref(
1594 ref, np->xbdev->otherend_id,
1595 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1596 frags->page)),
1597 0);
1598 req->gref = ref;
1599 req->id = requeue_idx;
1600
1601 requeue_idx++;
1602 }
1603
1604 np->rx.req_prod_pvt = requeue_idx;
1605
1606 /*
1607 * Step 3: All public and private state should now be sane. Get
1608 * ready to start sending and receiving packets and give the driver
1609 * domain a kick because we've probably just requeued some
1610 * packets.
1611 */
1612 netif_carrier_on(np->netdev);
1613 notify_remote_via_irq(np->netdev->irq);
1614 xennet_tx_buf_gc(dev);
1615 xennet_alloc_rx_buffers(dev);
1616
1617 spin_unlock_irq(&np->tx_lock);
1618 spin_unlock_bh(&np->rx_lock);
1619
1620 return 0;
1621}
1622
1623/**
1624 * Callback received when the backend's state changes.
1625 */
Ian Campbellf502bf22010-08-18 23:27:49 +00001626static void netback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001627 enum xenbus_state backend_state)
1628{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001629 struct netfront_info *np = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001630 struct net_device *netdev = np->netdev;
1631
1632 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1633
1634 switch (backend_state) {
1635 case XenbusStateInitialising:
1636 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001637 case XenbusStateReconfiguring:
1638 case XenbusStateReconfigured:
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001639 case XenbusStateConnected:
1640 case XenbusStateUnknown:
1641 case XenbusStateClosed:
1642 break;
1643
1644 case XenbusStateInitWait:
1645 if (dev->state != XenbusStateInitialising)
1646 break;
1647 if (xennet_connect(netdev) != 0)
1648 break;
1649 xenbus_switch_state(dev, XenbusStateConnected);
Ian Campbell59297062010-05-26 00:09:43 +00001650 netif_notify_peers(netdev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001651 break;
1652
1653 case XenbusStateClosing:
1654 xenbus_frontend_closed(dev);
1655 break;
1656 }
1657}
1658
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001659static const struct xennet_stat {
1660 char name[ETH_GSTRING_LEN];
1661 u16 offset;
1662} xennet_stats[] = {
1663 {
1664 "rx_gso_checksum_fixup",
1665 offsetof(struct netfront_info, rx_gso_checksum_fixup)
1666 },
1667};
1668
1669static int xennet_get_sset_count(struct net_device *dev, int string_set)
1670{
1671 switch (string_set) {
1672 case ETH_SS_STATS:
1673 return ARRAY_SIZE(xennet_stats);
1674 default:
1675 return -EINVAL;
1676 }
1677}
1678
1679static void xennet_get_ethtool_stats(struct net_device *dev,
1680 struct ethtool_stats *stats, u64 * data)
1681{
1682 void *np = netdev_priv(dev);
1683 int i;
1684
1685 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
Eric Dumazet48f26d52011-03-14 21:05:40 -07001686 data[i] = *(unsigned long *)(np + xennet_stats[i].offset);
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001687}
1688
1689static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
1690{
1691 int i;
1692
1693 switch (stringset) {
1694 case ETH_SS_STATS:
1695 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
1696 memcpy(data + i * ETH_GSTRING_LEN,
1697 xennet_stats[i].name, ETH_GSTRING_LEN);
1698 break;
1699 }
1700}
1701
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001702static const struct ethtool_ops xennet_ethtool_ops =
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001703{
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001704 .get_link = ethtool_op_get_link,
Ian Campbelle0ce4af2011-01-27 04:14:03 +00001705
1706 .get_sset_count = xennet_get_sset_count,
1707 .get_ethtool_stats = xennet_get_ethtool_stats,
1708 .get_strings = xennet_get_strings,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001709};
1710
1711#ifdef CONFIG_SYSFS
1712static ssize_t show_rxbuf_min(struct device *dev,
1713 struct device_attribute *attr, char *buf)
1714{
1715 struct net_device *netdev = to_net_dev(dev);
1716 struct netfront_info *info = netdev_priv(netdev);
1717
1718 return sprintf(buf, "%u\n", info->rx_min_target);
1719}
1720
1721static ssize_t store_rxbuf_min(struct device *dev,
1722 struct device_attribute *attr,
1723 const char *buf, size_t len)
1724{
1725 struct net_device *netdev = to_net_dev(dev);
1726 struct netfront_info *np = netdev_priv(netdev);
1727 char *endp;
1728 unsigned long target;
1729
1730 if (!capable(CAP_NET_ADMIN))
1731 return -EPERM;
1732
1733 target = simple_strtoul(buf, &endp, 0);
1734 if (endp == buf)
1735 return -EBADMSG;
1736
1737 if (target < RX_MIN_TARGET)
1738 target = RX_MIN_TARGET;
1739 if (target > RX_MAX_TARGET)
1740 target = RX_MAX_TARGET;
1741
1742 spin_lock_bh(&np->rx_lock);
1743 if (target > np->rx_max_target)
1744 np->rx_max_target = target;
1745 np->rx_min_target = target;
1746 if (target > np->rx_target)
1747 np->rx_target = target;
1748
1749 xennet_alloc_rx_buffers(netdev);
1750
1751 spin_unlock_bh(&np->rx_lock);
1752 return len;
1753}
1754
1755static ssize_t show_rxbuf_max(struct device *dev,
1756 struct device_attribute *attr, char *buf)
1757{
1758 struct net_device *netdev = to_net_dev(dev);
1759 struct netfront_info *info = netdev_priv(netdev);
1760
1761 return sprintf(buf, "%u\n", info->rx_max_target);
1762}
1763
1764static ssize_t store_rxbuf_max(struct device *dev,
1765 struct device_attribute *attr,
1766 const char *buf, size_t len)
1767{
1768 struct net_device *netdev = to_net_dev(dev);
1769 struct netfront_info *np = netdev_priv(netdev);
1770 char *endp;
1771 unsigned long target;
1772
1773 if (!capable(CAP_NET_ADMIN))
1774 return -EPERM;
1775
1776 target = simple_strtoul(buf, &endp, 0);
1777 if (endp == buf)
1778 return -EBADMSG;
1779
1780 if (target < RX_MIN_TARGET)
1781 target = RX_MIN_TARGET;
1782 if (target > RX_MAX_TARGET)
1783 target = RX_MAX_TARGET;
1784
1785 spin_lock_bh(&np->rx_lock);
1786 if (target < np->rx_min_target)
1787 np->rx_min_target = target;
1788 np->rx_max_target = target;
1789 if (target < np->rx_target)
1790 np->rx_target = target;
1791
1792 xennet_alloc_rx_buffers(netdev);
1793
1794 spin_unlock_bh(&np->rx_lock);
1795 return len;
1796}
1797
1798static ssize_t show_rxbuf_cur(struct device *dev,
1799 struct device_attribute *attr, char *buf)
1800{
1801 struct net_device *netdev = to_net_dev(dev);
1802 struct netfront_info *info = netdev_priv(netdev);
1803
1804 return sprintf(buf, "%u\n", info->rx_target);
1805}
1806
1807static struct device_attribute xennet_attrs[] = {
1808 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1809 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1810 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1811};
1812
1813static int xennet_sysfs_addif(struct net_device *netdev)
1814{
1815 int i;
1816 int err;
1817
1818 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1819 err = device_create_file(&netdev->dev,
1820 &xennet_attrs[i]);
1821 if (err)
1822 goto fail;
1823 }
1824 return 0;
1825
1826 fail:
1827 while (--i >= 0)
1828 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1829 return err;
1830}
1831
1832static void xennet_sysfs_delif(struct net_device *netdev)
1833{
1834 int i;
1835
1836 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1837 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1838}
1839
1840#endif /* CONFIG_SYSFS */
1841
1842static struct xenbus_device_id netfront_ids[] = {
1843 { "vif" },
1844 { "" }
1845};
1846
1847
1848static int __devexit xennet_remove(struct xenbus_device *dev)
1849{
Greg Kroah-Hartman1b713e02009-05-04 12:40:54 -07001850 struct netfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001851
1852 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1853
1854 unregister_netdev(info->netdev);
1855
1856 xennet_disconnect_backend(info);
1857
1858 del_timer_sync(&info->rx_refill_timer);
1859
1860 xennet_sysfs_delif(info->netdev);
1861
1862 free_netdev(info->netdev);
1863
1864 return 0;
1865}
1866
Al Viroffb78a22008-11-22 17:38:14 +00001867static struct xenbus_driver netfront_driver = {
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001868 .name = "vif",
1869 .owner = THIS_MODULE,
1870 .ids = netfront_ids,
1871 .probe = netfront_probe,
1872 .remove = __devexit_p(xennet_remove),
1873 .resume = netfront_resume,
Ian Campbellf502bf22010-08-18 23:27:49 +00001874 .otherend_changed = netback_changed,
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001875};
1876
1877static int __init netif_init(void)
1878{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001879 if (!xen_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001880 return -ENODEV;
1881
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001882 if (xen_initial_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001883 return 0;
1884
1885 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1886
Al Viroffb78a22008-11-22 17:38:14 +00001887 return xenbus_register_frontend(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001888}
1889module_init(netif_init);
1890
1891
1892static void __exit netif_exit(void)
1893{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001894 if (xen_initial_domain())
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001895 return;
1896
Al Viroffb78a22008-11-22 17:38:14 +00001897 xenbus_unregister_driver(&netfront_driver);
Jeremy Fitzhardinge0d160212007-07-17 18:37:06 -07001898}
1899module_exit(netif_exit);
1900
1901MODULE_DESCRIPTION("Xen virtual network device frontend");
1902MODULE_LICENSE("GPL");
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001903MODULE_ALIAS("xen:vif");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001904MODULE_ALIAS("xennet");