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