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