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