xref: /OK3568_Linux_fs/kernel/drivers/net/xen-netback/netback.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Back-end of the driver for virtual network devices. This portion of the
3*4882a593Smuzhiyun  * driver exports a 'unified' network-device interface that can be accessed
4*4882a593Smuzhiyun  * by any operating system that implements a compatible front end. A
5*4882a593Smuzhiyun  * reference front-end implementation can be found in:
6*4882a593Smuzhiyun  *  drivers/net/xen-netfront.c
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (c) 2002-2005, K A Fraser
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
11*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License version 2
12*4882a593Smuzhiyun  * as published by the Free Software Foundation; or, when distributed
13*4882a593Smuzhiyun  * separately from the Linux kernel or incorporated into other
14*4882a593Smuzhiyun  * software packages, subject to the following license:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a copy
17*4882a593Smuzhiyun  * of this source file (the "Software"), to deal in the Software without
18*4882a593Smuzhiyun  * restriction, including without limitation the rights to use, copy, modify,
19*4882a593Smuzhiyun  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so, subject to
21*4882a593Smuzhiyun  * the following conditions:
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
24*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29*4882a593Smuzhiyun  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32*4882a593Smuzhiyun  * IN THE SOFTWARE.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include "common.h"
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <linux/kthread.h>
38*4882a593Smuzhiyun #include <linux/if_vlan.h>
39*4882a593Smuzhiyun #include <linux/udp.h>
40*4882a593Smuzhiyun #include <linux/highmem.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include <net/tcp.h>
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <xen/xen.h>
45*4882a593Smuzhiyun #include <xen/events.h>
46*4882a593Smuzhiyun #include <xen/interface/memory.h>
47*4882a593Smuzhiyun #include <xen/page.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include <asm/xen/hypercall.h>
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* Provide an option to disable split event channels at load time as
52*4882a593Smuzhiyun  * event channels are limited resource. Split event channels are
53*4882a593Smuzhiyun  * enabled by default.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun bool separate_tx_rx_irq = true;
56*4882a593Smuzhiyun module_param(separate_tx_rx_irq, bool, 0644);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* The time that packets can stay on the guest Rx internal queue
59*4882a593Smuzhiyun  * before they are dropped.
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun unsigned int rx_drain_timeout_msecs = 10000;
62*4882a593Smuzhiyun module_param(rx_drain_timeout_msecs, uint, 0444);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* The length of time before the frontend is considered unresponsive
65*4882a593Smuzhiyun  * because it isn't providing Rx slots.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun unsigned int rx_stall_timeout_msecs = 60000;
68*4882a593Smuzhiyun module_param(rx_stall_timeout_msecs, uint, 0444);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define MAX_QUEUES_DEFAULT 8
71*4882a593Smuzhiyun unsigned int xenvif_max_queues;
72*4882a593Smuzhiyun module_param_named(max_queues, xenvif_max_queues, uint, 0644);
73*4882a593Smuzhiyun MODULE_PARM_DESC(max_queues,
74*4882a593Smuzhiyun 		 "Maximum number of queues per virtual interface");
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * This is the maximum slots a skb can have. If a guest sends a skb
78*4882a593Smuzhiyun  * which exceeds this limit it is considered malicious.
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun #define FATAL_SKB_SLOTS_DEFAULT 20
81*4882a593Smuzhiyun static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
82*4882a593Smuzhiyun module_param(fatal_skb_slots, uint, 0444);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* The amount to copy out of the first guest Tx slot into the skb's
85*4882a593Smuzhiyun  * linear area.  If the first slot has more data, it will be mapped
86*4882a593Smuzhiyun  * and put into the first frag.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * This is sized to avoid pulling headers from the frags for most
89*4882a593Smuzhiyun  * TCP/IP packets.
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun #define XEN_NETBACK_TX_COPY_LEN 128
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* This is the maximum number of flows in the hash cache. */
94*4882a593Smuzhiyun #define XENVIF_HASH_CACHE_SIZE_DEFAULT 64
95*4882a593Smuzhiyun unsigned int xenvif_hash_cache_size = XENVIF_HASH_CACHE_SIZE_DEFAULT;
96*4882a593Smuzhiyun module_param_named(hash_cache_size, xenvif_hash_cache_size, uint, 0644);
97*4882a593Smuzhiyun MODULE_PARM_DESC(hash_cache_size, "Number of flows in the hash cache");
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* The module parameter tells that we have to put data
100*4882a593Smuzhiyun  * for xen-netfront with the XDP_PACKET_HEADROOM offset
101*4882a593Smuzhiyun  * needed for XDP processing
102*4882a593Smuzhiyun  */
103*4882a593Smuzhiyun bool provides_xdp_headroom = true;
104*4882a593Smuzhiyun module_param(provides_xdp_headroom, bool, 0644);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
107*4882a593Smuzhiyun 			       u8 status);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun static void make_tx_response(struct xenvif_queue *queue,
110*4882a593Smuzhiyun 			     struct xen_netif_tx_request *txp,
111*4882a593Smuzhiyun 			     unsigned int extra_count,
112*4882a593Smuzhiyun 			     s8       st);
113*4882a593Smuzhiyun static void push_tx_responses(struct xenvif_queue *queue);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static inline int tx_work_todo(struct xenvif_queue *queue);
118*4882a593Smuzhiyun 
idx_to_pfn(struct xenvif_queue * queue,u16 idx)119*4882a593Smuzhiyun static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
120*4882a593Smuzhiyun 				       u16 idx)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	return page_to_pfn(queue->mmap_pages[idx]);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
idx_to_kaddr(struct xenvif_queue * queue,u16 idx)125*4882a593Smuzhiyun static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
126*4882a593Smuzhiyun 					 u16 idx)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #define callback_param(vif, pending_idx) \
132*4882a593Smuzhiyun 	(vif->pending_tx_info[pending_idx].callback_struct)
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /* Find the containing VIF's structure from a pointer in pending_tx_info array
135*4882a593Smuzhiyun  */
ubuf_to_queue(const struct ubuf_info * ubuf)136*4882a593Smuzhiyun static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	u16 pending_idx = ubuf->desc;
139*4882a593Smuzhiyun 	struct pending_tx_info *temp =
140*4882a593Smuzhiyun 		container_of(ubuf, struct pending_tx_info, callback_struct);
141*4882a593Smuzhiyun 	return container_of(temp - pending_idx,
142*4882a593Smuzhiyun 			    struct xenvif_queue,
143*4882a593Smuzhiyun 			    pending_tx_info[0]);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
frag_get_pending_idx(skb_frag_t * frag)146*4882a593Smuzhiyun static u16 frag_get_pending_idx(skb_frag_t *frag)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	return (u16)skb_frag_off(frag);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
frag_set_pending_idx(skb_frag_t * frag,u16 pending_idx)151*4882a593Smuzhiyun static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	skb_frag_off_set(frag, pending_idx);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
pending_index(unsigned i)156*4882a593Smuzhiyun static inline pending_ring_idx_t pending_index(unsigned i)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	return i & (MAX_PENDING_REQS-1);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
xenvif_kick_thread(struct xenvif_queue * queue)161*4882a593Smuzhiyun void xenvif_kick_thread(struct xenvif_queue *queue)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	wake_up(&queue->wq);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
xenvif_napi_schedule_or_enable_events(struct xenvif_queue * queue)166*4882a593Smuzhiyun void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	int more_to_do;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (more_to_do)
173*4882a593Smuzhiyun 		napi_schedule(&queue->napi);
174*4882a593Smuzhiyun 	else if (atomic_fetch_andnot(NETBK_TX_EOI | NETBK_COMMON_EOI,
175*4882a593Smuzhiyun 				     &queue->eoi_pending) &
176*4882a593Smuzhiyun 		 (NETBK_TX_EOI | NETBK_COMMON_EOI))
177*4882a593Smuzhiyun 		xen_irq_lateeoi(queue->tx_irq, 0);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
tx_add_credit(struct xenvif_queue * queue)180*4882a593Smuzhiyun static void tx_add_credit(struct xenvif_queue *queue)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	unsigned long max_burst, max_credit;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/*
185*4882a593Smuzhiyun 	 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
186*4882a593Smuzhiyun 	 * Otherwise the interface can seize up due to insufficient credit.
187*4882a593Smuzhiyun 	 */
188*4882a593Smuzhiyun 	max_burst = max(131072UL, queue->credit_bytes);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	/* Take care that adding a new chunk of credit doesn't wrap to zero. */
191*4882a593Smuzhiyun 	max_credit = queue->remaining_credit + queue->credit_bytes;
192*4882a593Smuzhiyun 	if (max_credit < queue->remaining_credit)
193*4882a593Smuzhiyun 		max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	queue->remaining_credit = min(max_credit, max_burst);
196*4882a593Smuzhiyun 	queue->rate_limited = false;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
xenvif_tx_credit_callback(struct timer_list * t)199*4882a593Smuzhiyun void xenvif_tx_credit_callback(struct timer_list *t)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	struct xenvif_queue *queue = from_timer(queue, t, credit_timeout);
202*4882a593Smuzhiyun 	tx_add_credit(queue);
203*4882a593Smuzhiyun 	xenvif_napi_schedule_or_enable_events(queue);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
xenvif_tx_err(struct xenvif_queue * queue,struct xen_netif_tx_request * txp,unsigned int extra_count,RING_IDX end)206*4882a593Smuzhiyun static void xenvif_tx_err(struct xenvif_queue *queue,
207*4882a593Smuzhiyun 			  struct xen_netif_tx_request *txp,
208*4882a593Smuzhiyun 			  unsigned int extra_count, RING_IDX end)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	RING_IDX cons = queue->tx.req_cons;
211*4882a593Smuzhiyun 	unsigned long flags;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	do {
214*4882a593Smuzhiyun 		spin_lock_irqsave(&queue->response_lock, flags);
215*4882a593Smuzhiyun 		make_tx_response(queue, txp, extra_count, XEN_NETIF_RSP_ERROR);
216*4882a593Smuzhiyun 		push_tx_responses(queue);
217*4882a593Smuzhiyun 		spin_unlock_irqrestore(&queue->response_lock, flags);
218*4882a593Smuzhiyun 		if (cons == end)
219*4882a593Smuzhiyun 			break;
220*4882a593Smuzhiyun 		RING_COPY_REQUEST(&queue->tx, cons++, txp);
221*4882a593Smuzhiyun 		extra_count = 0; /* only the first frag can have extras */
222*4882a593Smuzhiyun 	} while (1);
223*4882a593Smuzhiyun 	queue->tx.req_cons = cons;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
xenvif_fatal_tx_err(struct xenvif * vif)226*4882a593Smuzhiyun static void xenvif_fatal_tx_err(struct xenvif *vif)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	netdev_err(vif->dev, "fatal error; disabling device\n");
229*4882a593Smuzhiyun 	vif->disabled = true;
230*4882a593Smuzhiyun 	/* Disable the vif from queue 0's kthread */
231*4882a593Smuzhiyun 	if (vif->num_queues)
232*4882a593Smuzhiyun 		xenvif_kick_thread(&vif->queues[0]);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
xenvif_count_requests(struct xenvif_queue * queue,struct xen_netif_tx_request * first,unsigned int extra_count,struct xen_netif_tx_request * txp,int work_to_do)235*4882a593Smuzhiyun static int xenvif_count_requests(struct xenvif_queue *queue,
236*4882a593Smuzhiyun 				 struct xen_netif_tx_request *first,
237*4882a593Smuzhiyun 				 unsigned int extra_count,
238*4882a593Smuzhiyun 				 struct xen_netif_tx_request *txp,
239*4882a593Smuzhiyun 				 int work_to_do)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	RING_IDX cons = queue->tx.req_cons;
242*4882a593Smuzhiyun 	int slots = 0;
243*4882a593Smuzhiyun 	int drop_err = 0;
244*4882a593Smuzhiyun 	int more_data;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (!(first->flags & XEN_NETTXF_more_data))
247*4882a593Smuzhiyun 		return 0;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	do {
250*4882a593Smuzhiyun 		struct xen_netif_tx_request dropped_tx = { 0 };
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		if (slots >= work_to_do) {
253*4882a593Smuzhiyun 			netdev_err(queue->vif->dev,
254*4882a593Smuzhiyun 				   "Asked for %d slots but exceeds this limit\n",
255*4882a593Smuzhiyun 				   work_to_do);
256*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
257*4882a593Smuzhiyun 			return -ENODATA;
258*4882a593Smuzhiyun 		}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		/* This guest is really using too many slots and
261*4882a593Smuzhiyun 		 * considered malicious.
262*4882a593Smuzhiyun 		 */
263*4882a593Smuzhiyun 		if (unlikely(slots >= fatal_skb_slots)) {
264*4882a593Smuzhiyun 			netdev_err(queue->vif->dev,
265*4882a593Smuzhiyun 				   "Malicious frontend using %d slots, threshold %u\n",
266*4882a593Smuzhiyun 				   slots, fatal_skb_slots);
267*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
268*4882a593Smuzhiyun 			return -E2BIG;
269*4882a593Smuzhiyun 		}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		/* Xen network protocol had implicit dependency on
272*4882a593Smuzhiyun 		 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
273*4882a593Smuzhiyun 		 * the historical MAX_SKB_FRAGS value 18 to honor the
274*4882a593Smuzhiyun 		 * same behavior as before. Any packet using more than
275*4882a593Smuzhiyun 		 * 18 slots but less than fatal_skb_slots slots is
276*4882a593Smuzhiyun 		 * dropped
277*4882a593Smuzhiyun 		 */
278*4882a593Smuzhiyun 		if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
279*4882a593Smuzhiyun 			if (net_ratelimit())
280*4882a593Smuzhiyun 				netdev_dbg(queue->vif->dev,
281*4882a593Smuzhiyun 					   "Too many slots (%d) exceeding limit (%d), dropping packet\n",
282*4882a593Smuzhiyun 					   slots, XEN_NETBK_LEGACY_SLOTS_MAX);
283*4882a593Smuzhiyun 			drop_err = -E2BIG;
284*4882a593Smuzhiyun 		}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		if (drop_err)
287*4882a593Smuzhiyun 			txp = &dropped_tx;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 		/* If the guest submitted a frame >= 64 KiB then
292*4882a593Smuzhiyun 		 * first->size overflowed and following slots will
293*4882a593Smuzhiyun 		 * appear to be larger than the frame.
294*4882a593Smuzhiyun 		 *
295*4882a593Smuzhiyun 		 * This cannot be fatal error as there are buggy
296*4882a593Smuzhiyun 		 * frontends that do this.
297*4882a593Smuzhiyun 		 *
298*4882a593Smuzhiyun 		 * Consume all slots and drop the packet.
299*4882a593Smuzhiyun 		 */
300*4882a593Smuzhiyun 		if (!drop_err && txp->size > first->size) {
301*4882a593Smuzhiyun 			if (net_ratelimit())
302*4882a593Smuzhiyun 				netdev_dbg(queue->vif->dev,
303*4882a593Smuzhiyun 					   "Invalid tx request, slot size %u > remaining size %u\n",
304*4882a593Smuzhiyun 					   txp->size, first->size);
305*4882a593Smuzhiyun 			drop_err = -EIO;
306*4882a593Smuzhiyun 		}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		first->size -= txp->size;
309*4882a593Smuzhiyun 		slots++;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 		if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
312*4882a593Smuzhiyun 			netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
313*4882a593Smuzhiyun 				 txp->offset, txp->size);
314*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
315*4882a593Smuzhiyun 			return -EINVAL;
316*4882a593Smuzhiyun 		}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 		more_data = txp->flags & XEN_NETTXF_more_data;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		if (!drop_err)
321*4882a593Smuzhiyun 			txp++;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	} while (more_data);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (drop_err) {
326*4882a593Smuzhiyun 		xenvif_tx_err(queue, first, extra_count, cons + slots);
327*4882a593Smuzhiyun 		return drop_err;
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return slots;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun struct xenvif_tx_cb {
335*4882a593Smuzhiyun 	u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
336*4882a593Smuzhiyun 	u8 copy_count;
337*4882a593Smuzhiyun };
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
340*4882a593Smuzhiyun #define copy_pending_idx(skb, i) (XENVIF_TX_CB(skb)->copy_pending_idx[i])
341*4882a593Smuzhiyun #define copy_count(skb) (XENVIF_TX_CB(skb)->copy_count)
342*4882a593Smuzhiyun 
xenvif_tx_create_map_op(struct xenvif_queue * queue,u16 pending_idx,struct xen_netif_tx_request * txp,unsigned int extra_count,struct gnttab_map_grant_ref * mop)343*4882a593Smuzhiyun static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
344*4882a593Smuzhiyun 					   u16 pending_idx,
345*4882a593Smuzhiyun 					   struct xen_netif_tx_request *txp,
346*4882a593Smuzhiyun 					   unsigned int extra_count,
347*4882a593Smuzhiyun 					   struct gnttab_map_grant_ref *mop)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
350*4882a593Smuzhiyun 	gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
351*4882a593Smuzhiyun 			  GNTMAP_host_map | GNTMAP_readonly,
352*4882a593Smuzhiyun 			  txp->gref, queue->vif->domid);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	memcpy(&queue->pending_tx_info[pending_idx].req, txp,
355*4882a593Smuzhiyun 	       sizeof(*txp));
356*4882a593Smuzhiyun 	queue->pending_tx_info[pending_idx].extra_count = extra_count;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
xenvif_alloc_skb(unsigned int size)359*4882a593Smuzhiyun static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	struct sk_buff *skb =
362*4882a593Smuzhiyun 		alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
363*4882a593Smuzhiyun 			  GFP_ATOMIC | __GFP_NOWARN);
364*4882a593Smuzhiyun 	if (unlikely(skb == NULL))
365*4882a593Smuzhiyun 		return NULL;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* Packets passed to netif_rx() must have some headroom. */
368*4882a593Smuzhiyun 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/* Initialize it here to avoid later surprises */
371*4882a593Smuzhiyun 	skb_shinfo(skb)->destructor_arg = NULL;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	return skb;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun 
xenvif_get_requests(struct xenvif_queue * queue,struct sk_buff * skb,struct xen_netif_tx_request * first,struct xen_netif_tx_request * txfrags,unsigned * copy_ops,unsigned * map_ops,unsigned int frag_overflow,struct sk_buff * nskb,unsigned int extra_count,unsigned int data_len)376*4882a593Smuzhiyun static void xenvif_get_requests(struct xenvif_queue *queue,
377*4882a593Smuzhiyun 				struct sk_buff *skb,
378*4882a593Smuzhiyun 				struct xen_netif_tx_request *first,
379*4882a593Smuzhiyun 				struct xen_netif_tx_request *txfrags,
380*4882a593Smuzhiyun 			        unsigned *copy_ops,
381*4882a593Smuzhiyun 			        unsigned *map_ops,
382*4882a593Smuzhiyun 				unsigned int frag_overflow,
383*4882a593Smuzhiyun 				struct sk_buff *nskb,
384*4882a593Smuzhiyun 				unsigned int extra_count,
385*4882a593Smuzhiyun 				unsigned int data_len)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	struct skb_shared_info *shinfo = skb_shinfo(skb);
388*4882a593Smuzhiyun 	skb_frag_t *frags = shinfo->frags;
389*4882a593Smuzhiyun 	u16 pending_idx;
390*4882a593Smuzhiyun 	pending_ring_idx_t index;
391*4882a593Smuzhiyun 	unsigned int nr_slots;
392*4882a593Smuzhiyun 	struct gnttab_copy *cop = queue->tx_copy_ops + *copy_ops;
393*4882a593Smuzhiyun 	struct gnttab_map_grant_ref *gop = queue->tx_map_ops + *map_ops;
394*4882a593Smuzhiyun 	struct xen_netif_tx_request *txp = first;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	nr_slots = shinfo->nr_frags + 1;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	copy_count(skb) = 0;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* Create copy ops for exactly data_len bytes into the skb head. */
401*4882a593Smuzhiyun 	__skb_put(skb, data_len);
402*4882a593Smuzhiyun 	while (data_len > 0) {
403*4882a593Smuzhiyun 		int amount = data_len > txp->size ? txp->size : data_len;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 		cop->source.u.ref = txp->gref;
406*4882a593Smuzhiyun 		cop->source.domid = queue->vif->domid;
407*4882a593Smuzhiyun 		cop->source.offset = txp->offset;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 		cop->dest.domid = DOMID_SELF;
410*4882a593Smuzhiyun 		cop->dest.offset = (offset_in_page(skb->data +
411*4882a593Smuzhiyun 						   skb_headlen(skb) -
412*4882a593Smuzhiyun 						   data_len)) & ~XEN_PAGE_MASK;
413*4882a593Smuzhiyun 		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
414*4882a593Smuzhiyun 				               - data_len);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 		cop->len = amount;
417*4882a593Smuzhiyun 		cop->flags = GNTCOPY_source_gref;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 		index = pending_index(queue->pending_cons);
420*4882a593Smuzhiyun 		pending_idx = queue->pending_ring[index];
421*4882a593Smuzhiyun 		callback_param(queue, pending_idx).ctx = NULL;
422*4882a593Smuzhiyun 		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
423*4882a593Smuzhiyun 		copy_count(skb)++;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 		cop++;
426*4882a593Smuzhiyun 		data_len -= amount;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 		if (amount == txp->size) {
429*4882a593Smuzhiyun 			/* The copy op covered the full tx_request */
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 			memcpy(&queue->pending_tx_info[pending_idx].req,
432*4882a593Smuzhiyun 			       txp, sizeof(*txp));
433*4882a593Smuzhiyun 			queue->pending_tx_info[pending_idx].extra_count =
434*4882a593Smuzhiyun 				(txp == first) ? extra_count : 0;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 			if (txp == first)
437*4882a593Smuzhiyun 				txp = txfrags;
438*4882a593Smuzhiyun 			else
439*4882a593Smuzhiyun 				txp++;
440*4882a593Smuzhiyun 			queue->pending_cons++;
441*4882a593Smuzhiyun 			nr_slots--;
442*4882a593Smuzhiyun 		} else {
443*4882a593Smuzhiyun 			/* The copy op partially covered the tx_request.
444*4882a593Smuzhiyun 			 * The remainder will be mapped.
445*4882a593Smuzhiyun 			 */
446*4882a593Smuzhiyun 			txp->offset += amount;
447*4882a593Smuzhiyun 			txp->size -= amount;
448*4882a593Smuzhiyun 		}
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	for (shinfo->nr_frags = 0; shinfo->nr_frags < nr_slots;
452*4882a593Smuzhiyun 	     shinfo->nr_frags++, gop++) {
453*4882a593Smuzhiyun 		index = pending_index(queue->pending_cons++);
454*4882a593Smuzhiyun 		pending_idx = queue->pending_ring[index];
455*4882a593Smuzhiyun 		xenvif_tx_create_map_op(queue, pending_idx, txp,
456*4882a593Smuzhiyun 				        txp == first ? extra_count : 0, gop);
457*4882a593Smuzhiyun 		frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 		if (txp == first)
460*4882a593Smuzhiyun 			txp = txfrags;
461*4882a593Smuzhiyun 		else
462*4882a593Smuzhiyun 			txp++;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (frag_overflow) {
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 		shinfo = skb_shinfo(nskb);
468*4882a593Smuzhiyun 		frags = shinfo->frags;
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 		for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
471*4882a593Smuzhiyun 		     shinfo->nr_frags++, txp++, gop++) {
472*4882a593Smuzhiyun 			index = pending_index(queue->pending_cons++);
473*4882a593Smuzhiyun 			pending_idx = queue->pending_ring[index];
474*4882a593Smuzhiyun 			xenvif_tx_create_map_op(queue, pending_idx, txp, 0,
475*4882a593Smuzhiyun 						gop);
476*4882a593Smuzhiyun 			frag_set_pending_idx(&frags[shinfo->nr_frags],
477*4882a593Smuzhiyun 					     pending_idx);
478*4882a593Smuzhiyun 		}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		skb_shinfo(skb)->frag_list = nskb;
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	(*copy_ops) = cop - queue->tx_copy_ops;
484*4882a593Smuzhiyun 	(*map_ops) = gop - queue->tx_map_ops;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
xenvif_grant_handle_set(struct xenvif_queue * queue,u16 pending_idx,grant_handle_t handle)487*4882a593Smuzhiyun static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
488*4882a593Smuzhiyun 					   u16 pending_idx,
489*4882a593Smuzhiyun 					   grant_handle_t handle)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun 	if (unlikely(queue->grant_tx_handle[pending_idx] !=
492*4882a593Smuzhiyun 		     NETBACK_INVALID_HANDLE)) {
493*4882a593Smuzhiyun 		netdev_err(queue->vif->dev,
494*4882a593Smuzhiyun 			   "Trying to overwrite active handle! pending_idx: 0x%x\n",
495*4882a593Smuzhiyun 			   pending_idx);
496*4882a593Smuzhiyun 		BUG();
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 	queue->grant_tx_handle[pending_idx] = handle;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
xenvif_grant_handle_reset(struct xenvif_queue * queue,u16 pending_idx)501*4882a593Smuzhiyun static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
502*4882a593Smuzhiyun 					     u16 pending_idx)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	if (unlikely(queue->grant_tx_handle[pending_idx] ==
505*4882a593Smuzhiyun 		     NETBACK_INVALID_HANDLE)) {
506*4882a593Smuzhiyun 		netdev_err(queue->vif->dev,
507*4882a593Smuzhiyun 			   "Trying to unmap invalid handle! pending_idx: 0x%x\n",
508*4882a593Smuzhiyun 			   pending_idx);
509*4882a593Smuzhiyun 		BUG();
510*4882a593Smuzhiyun 	}
511*4882a593Smuzhiyun 	queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
xenvif_tx_check_gop(struct xenvif_queue * queue,struct sk_buff * skb,struct gnttab_map_grant_ref ** gopp_map,struct gnttab_copy ** gopp_copy)514*4882a593Smuzhiyun static int xenvif_tx_check_gop(struct xenvif_queue *queue,
515*4882a593Smuzhiyun 			       struct sk_buff *skb,
516*4882a593Smuzhiyun 			       struct gnttab_map_grant_ref **gopp_map,
517*4882a593Smuzhiyun 			       struct gnttab_copy **gopp_copy)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	struct gnttab_map_grant_ref *gop_map = *gopp_map;
520*4882a593Smuzhiyun 	u16 pending_idx;
521*4882a593Smuzhiyun 	/* This always points to the shinfo of the skb being checked, which
522*4882a593Smuzhiyun 	 * could be either the first or the one on the frag_list
523*4882a593Smuzhiyun 	 */
524*4882a593Smuzhiyun 	struct skb_shared_info *shinfo = skb_shinfo(skb);
525*4882a593Smuzhiyun 	/* If this is non-NULL, we are currently checking the frag_list skb, and
526*4882a593Smuzhiyun 	 * this points to the shinfo of the first one
527*4882a593Smuzhiyun 	 */
528*4882a593Smuzhiyun 	struct skb_shared_info *first_shinfo = NULL;
529*4882a593Smuzhiyun 	int nr_frags = shinfo->nr_frags;
530*4882a593Smuzhiyun 	const bool sharedslot = nr_frags &&
531*4882a593Smuzhiyun 				frag_get_pending_idx(&shinfo->frags[0]) ==
532*4882a593Smuzhiyun 				    copy_pending_idx(skb, copy_count(skb) - 1);
533*4882a593Smuzhiyun 	int i, err = 0;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	for (i = 0; i < copy_count(skb); i++) {
536*4882a593Smuzhiyun 		int newerr;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 		/* Check status of header. */
539*4882a593Smuzhiyun 		pending_idx = copy_pending_idx(skb, i);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		newerr = (*gopp_copy)->status;
542*4882a593Smuzhiyun 		if (likely(!newerr)) {
543*4882a593Smuzhiyun 			/* The first frag might still have this slot mapped */
544*4882a593Smuzhiyun 			if (i < copy_count(skb) - 1 || !sharedslot)
545*4882a593Smuzhiyun 				xenvif_idx_release(queue, pending_idx,
546*4882a593Smuzhiyun 						   XEN_NETIF_RSP_OKAY);
547*4882a593Smuzhiyun 		} else {
548*4882a593Smuzhiyun 			err = newerr;
549*4882a593Smuzhiyun 			if (net_ratelimit())
550*4882a593Smuzhiyun 				netdev_dbg(queue->vif->dev,
551*4882a593Smuzhiyun 					   "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
552*4882a593Smuzhiyun 					   (*gopp_copy)->status,
553*4882a593Smuzhiyun 					   pending_idx,
554*4882a593Smuzhiyun 					   (*gopp_copy)->source.u.ref);
555*4882a593Smuzhiyun 			/* The first frag might still have this slot mapped */
556*4882a593Smuzhiyun 			if (i < copy_count(skb) - 1 || !sharedslot)
557*4882a593Smuzhiyun 				xenvif_idx_release(queue, pending_idx,
558*4882a593Smuzhiyun 						   XEN_NETIF_RSP_ERROR);
559*4882a593Smuzhiyun 		}
560*4882a593Smuzhiyun 		(*gopp_copy)++;
561*4882a593Smuzhiyun 	}
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun check_frags:
564*4882a593Smuzhiyun 	for (i = 0; i < nr_frags; i++, gop_map++) {
565*4882a593Smuzhiyun 		int j, newerr;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 		/* Check error status: if okay then remember grant handle. */
570*4882a593Smuzhiyun 		newerr = gop_map->status;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 		if (likely(!newerr)) {
573*4882a593Smuzhiyun 			xenvif_grant_handle_set(queue,
574*4882a593Smuzhiyun 						pending_idx,
575*4882a593Smuzhiyun 						gop_map->handle);
576*4882a593Smuzhiyun 			/* Had a previous error? Invalidate this fragment. */
577*4882a593Smuzhiyun 			if (unlikely(err)) {
578*4882a593Smuzhiyun 				xenvif_idx_unmap(queue, pending_idx);
579*4882a593Smuzhiyun 				/* If the mapping of the first frag was OK, but
580*4882a593Smuzhiyun 				 * the header's copy failed, and they are
581*4882a593Smuzhiyun 				 * sharing a slot, send an error
582*4882a593Smuzhiyun 				 */
583*4882a593Smuzhiyun 				if (i == 0 && !first_shinfo && sharedslot)
584*4882a593Smuzhiyun 					xenvif_idx_release(queue, pending_idx,
585*4882a593Smuzhiyun 							   XEN_NETIF_RSP_ERROR);
586*4882a593Smuzhiyun 				else
587*4882a593Smuzhiyun 					xenvif_idx_release(queue, pending_idx,
588*4882a593Smuzhiyun 							   XEN_NETIF_RSP_OKAY);
589*4882a593Smuzhiyun 			}
590*4882a593Smuzhiyun 			continue;
591*4882a593Smuzhiyun 		}
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 		/* Error on this fragment: respond to client with an error. */
594*4882a593Smuzhiyun 		if (net_ratelimit())
595*4882a593Smuzhiyun 			netdev_dbg(queue->vif->dev,
596*4882a593Smuzhiyun 				   "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
597*4882a593Smuzhiyun 				   i,
598*4882a593Smuzhiyun 				   gop_map->status,
599*4882a593Smuzhiyun 				   pending_idx,
600*4882a593Smuzhiyun 				   gop_map->ref);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 		xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 		/* Not the first error? Preceding frags already invalidated. */
605*4882a593Smuzhiyun 		if (err)
606*4882a593Smuzhiyun 			continue;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 		/* Invalidate preceding fragments of this skb. */
609*4882a593Smuzhiyun 		for (j = 0; j < i; j++) {
610*4882a593Smuzhiyun 			pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
611*4882a593Smuzhiyun 			xenvif_idx_unmap(queue, pending_idx);
612*4882a593Smuzhiyun 			xenvif_idx_release(queue, pending_idx,
613*4882a593Smuzhiyun 					   XEN_NETIF_RSP_OKAY);
614*4882a593Smuzhiyun 		}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 		/* And if we found the error while checking the frag_list, unmap
617*4882a593Smuzhiyun 		 * the first skb's frags
618*4882a593Smuzhiyun 		 */
619*4882a593Smuzhiyun 		if (first_shinfo) {
620*4882a593Smuzhiyun 			for (j = 0; j < first_shinfo->nr_frags; j++) {
621*4882a593Smuzhiyun 				pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
622*4882a593Smuzhiyun 				xenvif_idx_unmap(queue, pending_idx);
623*4882a593Smuzhiyun 				xenvif_idx_release(queue, pending_idx,
624*4882a593Smuzhiyun 						   XEN_NETIF_RSP_OKAY);
625*4882a593Smuzhiyun 			}
626*4882a593Smuzhiyun 		}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 		/* Remember the error: invalidate all subsequent fragments. */
629*4882a593Smuzhiyun 		err = newerr;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	if (skb_has_frag_list(skb) && !first_shinfo) {
633*4882a593Smuzhiyun 		first_shinfo = skb_shinfo(skb);
634*4882a593Smuzhiyun 		shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
635*4882a593Smuzhiyun 		nr_frags = shinfo->nr_frags;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		goto check_frags;
638*4882a593Smuzhiyun 	}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	*gopp_map = gop_map;
641*4882a593Smuzhiyun 	return err;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun 
xenvif_fill_frags(struct xenvif_queue * queue,struct sk_buff * skb)644*4882a593Smuzhiyun static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	struct skb_shared_info *shinfo = skb_shinfo(skb);
647*4882a593Smuzhiyun 	int nr_frags = shinfo->nr_frags;
648*4882a593Smuzhiyun 	int i;
649*4882a593Smuzhiyun 	u16 prev_pending_idx = INVALID_PENDING_IDX;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	for (i = 0; i < nr_frags; i++) {
652*4882a593Smuzhiyun 		skb_frag_t *frag = shinfo->frags + i;
653*4882a593Smuzhiyun 		struct xen_netif_tx_request *txp;
654*4882a593Smuzhiyun 		struct page *page;
655*4882a593Smuzhiyun 		u16 pending_idx;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		pending_idx = frag_get_pending_idx(frag);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 		/* If this is not the first frag, chain it to the previous*/
660*4882a593Smuzhiyun 		if (prev_pending_idx == INVALID_PENDING_IDX)
661*4882a593Smuzhiyun 			skb_shinfo(skb)->destructor_arg =
662*4882a593Smuzhiyun 				&callback_param(queue, pending_idx);
663*4882a593Smuzhiyun 		else
664*4882a593Smuzhiyun 			callback_param(queue, prev_pending_idx).ctx =
665*4882a593Smuzhiyun 				&callback_param(queue, pending_idx);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		callback_param(queue, pending_idx).ctx = NULL;
668*4882a593Smuzhiyun 		prev_pending_idx = pending_idx;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 		txp = &queue->pending_tx_info[pending_idx].req;
671*4882a593Smuzhiyun 		page = virt_to_page(idx_to_kaddr(queue, pending_idx));
672*4882a593Smuzhiyun 		__skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
673*4882a593Smuzhiyun 		skb->len += txp->size;
674*4882a593Smuzhiyun 		skb->data_len += txp->size;
675*4882a593Smuzhiyun 		skb->truesize += txp->size;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 		/* Take an extra reference to offset network stack's put_page */
678*4882a593Smuzhiyun 		get_page(queue->mmap_pages[pending_idx]);
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
xenvif_get_extras(struct xenvif_queue * queue,struct xen_netif_extra_info * extras,unsigned int * extra_count,int work_to_do)682*4882a593Smuzhiyun static int xenvif_get_extras(struct xenvif_queue *queue,
683*4882a593Smuzhiyun 			     struct xen_netif_extra_info *extras,
684*4882a593Smuzhiyun 			     unsigned int *extra_count,
685*4882a593Smuzhiyun 			     int work_to_do)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	struct xen_netif_extra_info extra;
688*4882a593Smuzhiyun 	RING_IDX cons = queue->tx.req_cons;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	do {
691*4882a593Smuzhiyun 		if (unlikely(work_to_do-- <= 0)) {
692*4882a593Smuzhiyun 			netdev_err(queue->vif->dev, "Missing extra info\n");
693*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
694*4882a593Smuzhiyun 			return -EBADR;
695*4882a593Smuzhiyun 		}
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 		RING_COPY_REQUEST(&queue->tx, cons, &extra);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 		queue->tx.req_cons = ++cons;
700*4882a593Smuzhiyun 		(*extra_count)++;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 		if (unlikely(!extra.type ||
703*4882a593Smuzhiyun 			     extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
704*4882a593Smuzhiyun 			netdev_err(queue->vif->dev,
705*4882a593Smuzhiyun 				   "Invalid extra type: %d\n", extra.type);
706*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
707*4882a593Smuzhiyun 			return -EINVAL;
708*4882a593Smuzhiyun 		}
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 		memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
711*4882a593Smuzhiyun 	} while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	return work_to_do;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun 
xenvif_set_skb_gso(struct xenvif * vif,struct sk_buff * skb,struct xen_netif_extra_info * gso)716*4882a593Smuzhiyun static int xenvif_set_skb_gso(struct xenvif *vif,
717*4882a593Smuzhiyun 			      struct sk_buff *skb,
718*4882a593Smuzhiyun 			      struct xen_netif_extra_info *gso)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	if (!gso->u.gso.size) {
721*4882a593Smuzhiyun 		netdev_err(vif->dev, "GSO size must not be zero.\n");
722*4882a593Smuzhiyun 		xenvif_fatal_tx_err(vif);
723*4882a593Smuzhiyun 		return -EINVAL;
724*4882a593Smuzhiyun 	}
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	switch (gso->u.gso.type) {
727*4882a593Smuzhiyun 	case XEN_NETIF_GSO_TYPE_TCPV4:
728*4882a593Smuzhiyun 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
729*4882a593Smuzhiyun 		break;
730*4882a593Smuzhiyun 	case XEN_NETIF_GSO_TYPE_TCPV6:
731*4882a593Smuzhiyun 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
732*4882a593Smuzhiyun 		break;
733*4882a593Smuzhiyun 	default:
734*4882a593Smuzhiyun 		netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
735*4882a593Smuzhiyun 		xenvif_fatal_tx_err(vif);
736*4882a593Smuzhiyun 		return -EINVAL;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	skb_shinfo(skb)->gso_size = gso->u.gso.size;
740*4882a593Smuzhiyun 	/* gso_segs will be calculated later */
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	return 0;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun 
checksum_setup(struct xenvif_queue * queue,struct sk_buff * skb)745*4882a593Smuzhiyun static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun 	bool recalculate_partial_csum = false;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
750*4882a593Smuzhiyun 	 * peers can fail to set NETRXF_csum_blank when sending a GSO
751*4882a593Smuzhiyun 	 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
752*4882a593Smuzhiyun 	 * recalculate the partial checksum.
753*4882a593Smuzhiyun 	 */
754*4882a593Smuzhiyun 	if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
755*4882a593Smuzhiyun 		queue->stats.rx_gso_checksum_fixup++;
756*4882a593Smuzhiyun 		skb->ip_summed = CHECKSUM_PARTIAL;
757*4882a593Smuzhiyun 		recalculate_partial_csum = true;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* A non-CHECKSUM_PARTIAL SKB does not require setup. */
761*4882a593Smuzhiyun 	if (skb->ip_summed != CHECKSUM_PARTIAL)
762*4882a593Smuzhiyun 		return 0;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	return skb_checksum_setup(skb, recalculate_partial_csum);
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
tx_credit_exceeded(struct xenvif_queue * queue,unsigned size)767*4882a593Smuzhiyun static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	u64 now = get_jiffies_64();
770*4882a593Smuzhiyun 	u64 next_credit = queue->credit_window_start +
771*4882a593Smuzhiyun 		msecs_to_jiffies(queue->credit_usec / 1000);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	/* Timer could already be pending in rare cases. */
774*4882a593Smuzhiyun 	if (timer_pending(&queue->credit_timeout)) {
775*4882a593Smuzhiyun 		queue->rate_limited = true;
776*4882a593Smuzhiyun 		return true;
777*4882a593Smuzhiyun 	}
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	/* Passed the point where we can replenish credit? */
780*4882a593Smuzhiyun 	if (time_after_eq64(now, next_credit)) {
781*4882a593Smuzhiyun 		queue->credit_window_start = now;
782*4882a593Smuzhiyun 		tx_add_credit(queue);
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	/* Still too big to send right now? Set a callback. */
786*4882a593Smuzhiyun 	if (size > queue->remaining_credit) {
787*4882a593Smuzhiyun 		mod_timer(&queue->credit_timeout,
788*4882a593Smuzhiyun 			  next_credit);
789*4882a593Smuzhiyun 		queue->credit_window_start = next_credit;
790*4882a593Smuzhiyun 		queue->rate_limited = true;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 		return true;
793*4882a593Smuzhiyun 	}
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	return false;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /* No locking is required in xenvif_mcast_add/del() as they are
799*4882a593Smuzhiyun  * only ever invoked from NAPI poll. An RCU list is used because
800*4882a593Smuzhiyun  * xenvif_mcast_match() is called asynchronously, during start_xmit.
801*4882a593Smuzhiyun  */
802*4882a593Smuzhiyun 
xenvif_mcast_add(struct xenvif * vif,const u8 * addr)803*4882a593Smuzhiyun static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	struct xenvif_mcast_addr *mcast;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
808*4882a593Smuzhiyun 		if (net_ratelimit())
809*4882a593Smuzhiyun 			netdev_err(vif->dev,
810*4882a593Smuzhiyun 				   "Too many multicast addresses\n");
811*4882a593Smuzhiyun 		return -ENOSPC;
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
815*4882a593Smuzhiyun 	if (!mcast)
816*4882a593Smuzhiyun 		return -ENOMEM;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	ether_addr_copy(mcast->addr, addr);
819*4882a593Smuzhiyun 	list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
820*4882a593Smuzhiyun 	vif->fe_mcast_count++;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
xenvif_mcast_del(struct xenvif * vif,const u8 * addr)825*4882a593Smuzhiyun static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	struct xenvif_mcast_addr *mcast;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
830*4882a593Smuzhiyun 		if (ether_addr_equal(addr, mcast->addr)) {
831*4882a593Smuzhiyun 			--vif->fe_mcast_count;
832*4882a593Smuzhiyun 			list_del_rcu(&mcast->entry);
833*4882a593Smuzhiyun 			kfree_rcu(mcast, rcu);
834*4882a593Smuzhiyun 			break;
835*4882a593Smuzhiyun 		}
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun 
xenvif_mcast_match(struct xenvif * vif,const u8 * addr)839*4882a593Smuzhiyun bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun 	struct xenvif_mcast_addr *mcast;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	rcu_read_lock();
844*4882a593Smuzhiyun 	list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
845*4882a593Smuzhiyun 		if (ether_addr_equal(addr, mcast->addr)) {
846*4882a593Smuzhiyun 			rcu_read_unlock();
847*4882a593Smuzhiyun 			return true;
848*4882a593Smuzhiyun 		}
849*4882a593Smuzhiyun 	}
850*4882a593Smuzhiyun 	rcu_read_unlock();
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	return false;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun 
xenvif_mcast_addr_list_free(struct xenvif * vif)855*4882a593Smuzhiyun void xenvif_mcast_addr_list_free(struct xenvif *vif)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun 	/* No need for locking or RCU here. NAPI poll and TX queue
858*4882a593Smuzhiyun 	 * are stopped.
859*4882a593Smuzhiyun 	 */
860*4882a593Smuzhiyun 	while (!list_empty(&vif->fe_mcast_addr)) {
861*4882a593Smuzhiyun 		struct xenvif_mcast_addr *mcast;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 		mcast = list_first_entry(&vif->fe_mcast_addr,
864*4882a593Smuzhiyun 					 struct xenvif_mcast_addr,
865*4882a593Smuzhiyun 					 entry);
866*4882a593Smuzhiyun 		--vif->fe_mcast_count;
867*4882a593Smuzhiyun 		list_del(&mcast->entry);
868*4882a593Smuzhiyun 		kfree(mcast);
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
xenvif_tx_build_gops(struct xenvif_queue * queue,int budget,unsigned * copy_ops,unsigned * map_ops)872*4882a593Smuzhiyun static void xenvif_tx_build_gops(struct xenvif_queue *queue,
873*4882a593Smuzhiyun 				     int budget,
874*4882a593Smuzhiyun 				     unsigned *copy_ops,
875*4882a593Smuzhiyun 				     unsigned *map_ops)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct sk_buff *skb, *nskb;
878*4882a593Smuzhiyun 	int ret;
879*4882a593Smuzhiyun 	unsigned int frag_overflow;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	while (skb_queue_len(&queue->tx_queue) < budget) {
882*4882a593Smuzhiyun 		struct xen_netif_tx_request txreq;
883*4882a593Smuzhiyun 		struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
884*4882a593Smuzhiyun 		struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
885*4882a593Smuzhiyun 		unsigned int extra_count;
886*4882a593Smuzhiyun 		u16 pending_idx;
887*4882a593Smuzhiyun 		RING_IDX idx;
888*4882a593Smuzhiyun 		int work_to_do;
889*4882a593Smuzhiyun 		unsigned int data_len;
890*4882a593Smuzhiyun 		pending_ring_idx_t index;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 		if (queue->tx.sring->req_prod - queue->tx.req_cons >
893*4882a593Smuzhiyun 		    XEN_NETIF_TX_RING_SIZE) {
894*4882a593Smuzhiyun 			netdev_err(queue->vif->dev,
895*4882a593Smuzhiyun 				   "Impossible number of requests. "
896*4882a593Smuzhiyun 				   "req_prod %d, req_cons %d, size %ld\n",
897*4882a593Smuzhiyun 				   queue->tx.sring->req_prod, queue->tx.req_cons,
898*4882a593Smuzhiyun 				   XEN_NETIF_TX_RING_SIZE);
899*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
900*4882a593Smuzhiyun 			break;
901*4882a593Smuzhiyun 		}
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 		work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
904*4882a593Smuzhiyun 		if (!work_to_do)
905*4882a593Smuzhiyun 			break;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 		idx = queue->tx.req_cons;
908*4882a593Smuzhiyun 		rmb(); /* Ensure that we see the request before we copy it. */
909*4882a593Smuzhiyun 		RING_COPY_REQUEST(&queue->tx, idx, &txreq);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		/* Credit-based scheduling. */
912*4882a593Smuzhiyun 		if (txreq.size > queue->remaining_credit &&
913*4882a593Smuzhiyun 		    tx_credit_exceeded(queue, txreq.size))
914*4882a593Smuzhiyun 			break;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 		queue->remaining_credit -= txreq.size;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 		work_to_do--;
919*4882a593Smuzhiyun 		queue->tx.req_cons = ++idx;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 		memset(extras, 0, sizeof(extras));
922*4882a593Smuzhiyun 		extra_count = 0;
923*4882a593Smuzhiyun 		if (txreq.flags & XEN_NETTXF_extra_info) {
924*4882a593Smuzhiyun 			work_to_do = xenvif_get_extras(queue, extras,
925*4882a593Smuzhiyun 						       &extra_count,
926*4882a593Smuzhiyun 						       work_to_do);
927*4882a593Smuzhiyun 			idx = queue->tx.req_cons;
928*4882a593Smuzhiyun 			if (unlikely(work_to_do < 0))
929*4882a593Smuzhiyun 				break;
930*4882a593Smuzhiyun 		}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 		if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
933*4882a593Smuzhiyun 			struct xen_netif_extra_info *extra;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 			extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
936*4882a593Smuzhiyun 			ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 			make_tx_response(queue, &txreq, extra_count,
939*4882a593Smuzhiyun 					 (ret == 0) ?
940*4882a593Smuzhiyun 					 XEN_NETIF_RSP_OKAY :
941*4882a593Smuzhiyun 					 XEN_NETIF_RSP_ERROR);
942*4882a593Smuzhiyun 			push_tx_responses(queue);
943*4882a593Smuzhiyun 			continue;
944*4882a593Smuzhiyun 		}
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 		if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
947*4882a593Smuzhiyun 			struct xen_netif_extra_info *extra;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 			extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
950*4882a593Smuzhiyun 			xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 			make_tx_response(queue, &txreq, extra_count,
953*4882a593Smuzhiyun 					 XEN_NETIF_RSP_OKAY);
954*4882a593Smuzhiyun 			push_tx_responses(queue);
955*4882a593Smuzhiyun 			continue;
956*4882a593Smuzhiyun 		}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN) ?
959*4882a593Smuzhiyun 			XEN_NETBACK_TX_COPY_LEN : txreq.size;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 		ret = xenvif_count_requests(queue, &txreq, extra_count,
962*4882a593Smuzhiyun 					    txfrags, work_to_do);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 		if (unlikely(ret < 0))
965*4882a593Smuzhiyun 			break;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 		idx += ret;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 		if (unlikely(txreq.size < ETH_HLEN)) {
970*4882a593Smuzhiyun 			netdev_dbg(queue->vif->dev,
971*4882a593Smuzhiyun 				   "Bad packet size: %d\n", txreq.size);
972*4882a593Smuzhiyun 			xenvif_tx_err(queue, &txreq, extra_count, idx);
973*4882a593Smuzhiyun 			break;
974*4882a593Smuzhiyun 		}
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 		/* No crossing a page as the payload mustn't fragment. */
977*4882a593Smuzhiyun 		if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
978*4882a593Smuzhiyun 			netdev_err(queue->vif->dev,
979*4882a593Smuzhiyun 				   "txreq.offset: %u, size: %u, end: %lu\n",
980*4882a593Smuzhiyun 				   txreq.offset, txreq.size,
981*4882a593Smuzhiyun 				   (unsigned long)(txreq.offset&~XEN_PAGE_MASK) + txreq.size);
982*4882a593Smuzhiyun 			xenvif_fatal_tx_err(queue->vif);
983*4882a593Smuzhiyun 			break;
984*4882a593Smuzhiyun 		}
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 		index = pending_index(queue->pending_cons);
987*4882a593Smuzhiyun 		pending_idx = queue->pending_ring[index];
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
990*4882a593Smuzhiyun 			data_len = txreq.size;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 		skb = xenvif_alloc_skb(data_len);
993*4882a593Smuzhiyun 		if (unlikely(skb == NULL)) {
994*4882a593Smuzhiyun 			netdev_dbg(queue->vif->dev,
995*4882a593Smuzhiyun 				   "Can't allocate a skb in start_xmit.\n");
996*4882a593Smuzhiyun 			xenvif_tx_err(queue, &txreq, extra_count, idx);
997*4882a593Smuzhiyun 			break;
998*4882a593Smuzhiyun 		}
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 		skb_shinfo(skb)->nr_frags = ret;
1001*4882a593Smuzhiyun 		/* At this point shinfo->nr_frags is in fact the number of
1002*4882a593Smuzhiyun 		 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1003*4882a593Smuzhiyun 		 */
1004*4882a593Smuzhiyun 		frag_overflow = 0;
1005*4882a593Smuzhiyun 		nskb = NULL;
1006*4882a593Smuzhiyun 		if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
1007*4882a593Smuzhiyun 			frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
1008*4882a593Smuzhiyun 			BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1009*4882a593Smuzhiyun 			skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
1010*4882a593Smuzhiyun 			nskb = xenvif_alloc_skb(0);
1011*4882a593Smuzhiyun 			if (unlikely(nskb == NULL)) {
1012*4882a593Smuzhiyun 				skb_shinfo(skb)->nr_frags = 0;
1013*4882a593Smuzhiyun 				kfree_skb(skb);
1014*4882a593Smuzhiyun 				xenvif_tx_err(queue, &txreq, extra_count, idx);
1015*4882a593Smuzhiyun 				if (net_ratelimit())
1016*4882a593Smuzhiyun 					netdev_err(queue->vif->dev,
1017*4882a593Smuzhiyun 						   "Can't allocate the frag_list skb.\n");
1018*4882a593Smuzhiyun 				break;
1019*4882a593Smuzhiyun 			}
1020*4882a593Smuzhiyun 		}
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 		if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1023*4882a593Smuzhiyun 			struct xen_netif_extra_info *gso;
1024*4882a593Smuzhiyun 			gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 			if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1027*4882a593Smuzhiyun 				/* Failure in xenvif_set_skb_gso is fatal. */
1028*4882a593Smuzhiyun 				skb_shinfo(skb)->nr_frags = 0;
1029*4882a593Smuzhiyun 				kfree_skb(skb);
1030*4882a593Smuzhiyun 				kfree_skb(nskb);
1031*4882a593Smuzhiyun 				break;
1032*4882a593Smuzhiyun 			}
1033*4882a593Smuzhiyun 		}
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 		if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
1036*4882a593Smuzhiyun 			struct xen_netif_extra_info *extra;
1037*4882a593Smuzhiyun 			enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 			extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 			switch (extra->u.hash.type) {
1042*4882a593Smuzhiyun 			case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
1043*4882a593Smuzhiyun 			case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
1044*4882a593Smuzhiyun 				type = PKT_HASH_TYPE_L3;
1045*4882a593Smuzhiyun 				break;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 			case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
1048*4882a593Smuzhiyun 			case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
1049*4882a593Smuzhiyun 				type = PKT_HASH_TYPE_L4;
1050*4882a593Smuzhiyun 				break;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 			default:
1053*4882a593Smuzhiyun 				break;
1054*4882a593Smuzhiyun 			}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 			if (type != PKT_HASH_TYPE_NONE)
1057*4882a593Smuzhiyun 				skb_set_hash(skb,
1058*4882a593Smuzhiyun 					     *(u32 *)extra->u.hash.value,
1059*4882a593Smuzhiyun 					     type);
1060*4882a593Smuzhiyun 		}
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 		xenvif_get_requests(queue, skb, &txreq, txfrags, copy_ops,
1063*4882a593Smuzhiyun 				    map_ops, frag_overflow, nskb, extra_count,
1064*4882a593Smuzhiyun 				    data_len);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 		__skb_queue_tail(&queue->tx_queue, skb);
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 		queue->tx.req_cons = idx;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 		if ((*map_ops >= ARRAY_SIZE(queue->tx_map_ops)) ||
1071*4882a593Smuzhiyun 		    (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1072*4882a593Smuzhiyun 			break;
1073*4882a593Smuzhiyun 	}
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	return;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun /* Consolidate skb with a frag_list into a brand new one with local pages on
1079*4882a593Smuzhiyun  * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1080*4882a593Smuzhiyun  */
xenvif_handle_frag_list(struct xenvif_queue * queue,struct sk_buff * skb)1081*4882a593Smuzhiyun static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1082*4882a593Smuzhiyun {
1083*4882a593Smuzhiyun 	unsigned int offset = skb_headlen(skb);
1084*4882a593Smuzhiyun 	skb_frag_t frags[MAX_SKB_FRAGS];
1085*4882a593Smuzhiyun 	int i, f;
1086*4882a593Smuzhiyun 	struct ubuf_info *uarg;
1087*4882a593Smuzhiyun 	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	queue->stats.tx_zerocopy_sent += 2;
1090*4882a593Smuzhiyun 	queue->stats.tx_frag_overflow++;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	xenvif_fill_frags(queue, nskb);
1093*4882a593Smuzhiyun 	/* Subtract frags size, we will correct it later */
1094*4882a593Smuzhiyun 	skb->truesize -= skb->data_len;
1095*4882a593Smuzhiyun 	skb->len += nskb->len;
1096*4882a593Smuzhiyun 	skb->data_len += nskb->len;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	/* create a brand new frags array and coalesce there */
1099*4882a593Smuzhiyun 	for (i = 0; offset < skb->len; i++) {
1100*4882a593Smuzhiyun 		struct page *page;
1101*4882a593Smuzhiyun 		unsigned int len;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 		BUG_ON(i >= MAX_SKB_FRAGS);
1104*4882a593Smuzhiyun 		page = alloc_page(GFP_ATOMIC);
1105*4882a593Smuzhiyun 		if (!page) {
1106*4882a593Smuzhiyun 			int j;
1107*4882a593Smuzhiyun 			skb->truesize += skb->data_len;
1108*4882a593Smuzhiyun 			for (j = 0; j < i; j++)
1109*4882a593Smuzhiyun 				put_page(skb_frag_page(&frags[j]));
1110*4882a593Smuzhiyun 			return -ENOMEM;
1111*4882a593Smuzhiyun 		}
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 		if (offset + PAGE_SIZE < skb->len)
1114*4882a593Smuzhiyun 			len = PAGE_SIZE;
1115*4882a593Smuzhiyun 		else
1116*4882a593Smuzhiyun 			len = skb->len - offset;
1117*4882a593Smuzhiyun 		if (skb_copy_bits(skb, offset, page_address(page), len))
1118*4882a593Smuzhiyun 			BUG();
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 		offset += len;
1121*4882a593Smuzhiyun 		__skb_frag_set_page(&frags[i], page);
1122*4882a593Smuzhiyun 		skb_frag_off_set(&frags[i], 0);
1123*4882a593Smuzhiyun 		skb_frag_size_set(&frags[i], len);
1124*4882a593Smuzhiyun 	}
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	/* Release all the original (foreign) frags. */
1127*4882a593Smuzhiyun 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1128*4882a593Smuzhiyun 		skb_frag_unref(skb, f);
1129*4882a593Smuzhiyun 	uarg = skb_shinfo(skb)->destructor_arg;
1130*4882a593Smuzhiyun 	/* increase inflight counter to offset decrement in callback */
1131*4882a593Smuzhiyun 	atomic_inc(&queue->inflight_packets);
1132*4882a593Smuzhiyun 	uarg->callback(uarg, true);
1133*4882a593Smuzhiyun 	skb_shinfo(skb)->destructor_arg = NULL;
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	/* Fill the skb with the new (local) frags. */
1136*4882a593Smuzhiyun 	memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1137*4882a593Smuzhiyun 	skb_shinfo(skb)->nr_frags = i;
1138*4882a593Smuzhiyun 	skb->truesize += i * PAGE_SIZE;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	return 0;
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun 
xenvif_tx_submit(struct xenvif_queue * queue)1143*4882a593Smuzhiyun static int xenvif_tx_submit(struct xenvif_queue *queue)
1144*4882a593Smuzhiyun {
1145*4882a593Smuzhiyun 	struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1146*4882a593Smuzhiyun 	struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1147*4882a593Smuzhiyun 	struct sk_buff *skb;
1148*4882a593Smuzhiyun 	int work_done = 0;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1151*4882a593Smuzhiyun 		struct xen_netif_tx_request *txp;
1152*4882a593Smuzhiyun 		u16 pending_idx;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 		pending_idx = copy_pending_idx(skb, 0);
1155*4882a593Smuzhiyun 		txp = &queue->pending_tx_info[pending_idx].req;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 		/* Check the remap error code. */
1158*4882a593Smuzhiyun 		if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1159*4882a593Smuzhiyun 			/* If there was an error, xenvif_tx_check_gop is
1160*4882a593Smuzhiyun 			 * expected to release all the frags which were mapped,
1161*4882a593Smuzhiyun 			 * so kfree_skb shouldn't do it again
1162*4882a593Smuzhiyun 			 */
1163*4882a593Smuzhiyun 			skb_shinfo(skb)->nr_frags = 0;
1164*4882a593Smuzhiyun 			if (skb_has_frag_list(skb)) {
1165*4882a593Smuzhiyun 				struct sk_buff *nskb =
1166*4882a593Smuzhiyun 						skb_shinfo(skb)->frag_list;
1167*4882a593Smuzhiyun 				skb_shinfo(nskb)->nr_frags = 0;
1168*4882a593Smuzhiyun 			}
1169*4882a593Smuzhiyun 			kfree_skb(skb);
1170*4882a593Smuzhiyun 			continue;
1171*4882a593Smuzhiyun 		}
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 		if (txp->flags & XEN_NETTXF_csum_blank)
1174*4882a593Smuzhiyun 			skb->ip_summed = CHECKSUM_PARTIAL;
1175*4882a593Smuzhiyun 		else if (txp->flags & XEN_NETTXF_data_validated)
1176*4882a593Smuzhiyun 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 		xenvif_fill_frags(queue, skb);
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 		if (unlikely(skb_has_frag_list(skb))) {
1181*4882a593Smuzhiyun 			struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1182*4882a593Smuzhiyun 			xenvif_skb_zerocopy_prepare(queue, nskb);
1183*4882a593Smuzhiyun 			if (xenvif_handle_frag_list(queue, skb)) {
1184*4882a593Smuzhiyun 				if (net_ratelimit())
1185*4882a593Smuzhiyun 					netdev_err(queue->vif->dev,
1186*4882a593Smuzhiyun 						   "Not enough memory to consolidate frag_list!\n");
1187*4882a593Smuzhiyun 				xenvif_skb_zerocopy_prepare(queue, skb);
1188*4882a593Smuzhiyun 				kfree_skb(skb);
1189*4882a593Smuzhiyun 				continue;
1190*4882a593Smuzhiyun 			}
1191*4882a593Smuzhiyun 			/* Copied all the bits from the frag list -- free it. */
1192*4882a593Smuzhiyun 			skb_frag_list_init(skb);
1193*4882a593Smuzhiyun 			kfree_skb(nskb);
1194*4882a593Smuzhiyun 		}
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 		skb->dev      = queue->vif->dev;
1197*4882a593Smuzhiyun 		skb->protocol = eth_type_trans(skb, skb->dev);
1198*4882a593Smuzhiyun 		skb_reset_network_header(skb);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 		if (checksum_setup(queue, skb)) {
1201*4882a593Smuzhiyun 			netdev_dbg(queue->vif->dev,
1202*4882a593Smuzhiyun 				   "Can't setup checksum in net_tx_action\n");
1203*4882a593Smuzhiyun 			/* We have to set this flag to trigger the callback */
1204*4882a593Smuzhiyun 			if (skb_shinfo(skb)->destructor_arg)
1205*4882a593Smuzhiyun 				xenvif_skb_zerocopy_prepare(queue, skb);
1206*4882a593Smuzhiyun 			kfree_skb(skb);
1207*4882a593Smuzhiyun 			continue;
1208*4882a593Smuzhiyun 		}
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 		skb_probe_transport_header(skb);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 		/* If the packet is GSO then we will have just set up the
1213*4882a593Smuzhiyun 		 * transport header offset in checksum_setup so it's now
1214*4882a593Smuzhiyun 		 * straightforward to calculate gso_segs.
1215*4882a593Smuzhiyun 		 */
1216*4882a593Smuzhiyun 		if (skb_is_gso(skb)) {
1217*4882a593Smuzhiyun 			int mss, hdrlen;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 			/* GSO implies having the L4 header. */
1220*4882a593Smuzhiyun 			WARN_ON_ONCE(!skb_transport_header_was_set(skb));
1221*4882a593Smuzhiyun 			if (unlikely(!skb_transport_header_was_set(skb))) {
1222*4882a593Smuzhiyun 				kfree_skb(skb);
1223*4882a593Smuzhiyun 				continue;
1224*4882a593Smuzhiyun 			}
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 			mss = skb_shinfo(skb)->gso_size;
1227*4882a593Smuzhiyun 			hdrlen = skb_transport_header(skb) -
1228*4882a593Smuzhiyun 				skb_mac_header(skb) +
1229*4882a593Smuzhiyun 				tcp_hdrlen(skb);
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 			skb_shinfo(skb)->gso_segs =
1232*4882a593Smuzhiyun 				DIV_ROUND_UP(skb->len - hdrlen, mss);
1233*4882a593Smuzhiyun 		}
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 		queue->stats.rx_bytes += skb->len;
1236*4882a593Smuzhiyun 		queue->stats.rx_packets++;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 		work_done++;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		/* Set this flag right before netif_receive_skb, otherwise
1241*4882a593Smuzhiyun 		 * someone might think this packet already left netback, and
1242*4882a593Smuzhiyun 		 * do a skb_copy_ubufs while we are still in control of the
1243*4882a593Smuzhiyun 		 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1244*4882a593Smuzhiyun 		 */
1245*4882a593Smuzhiyun 		if (skb_shinfo(skb)->destructor_arg) {
1246*4882a593Smuzhiyun 			xenvif_skb_zerocopy_prepare(queue, skb);
1247*4882a593Smuzhiyun 			queue->stats.tx_zerocopy_sent++;
1248*4882a593Smuzhiyun 		}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 		netif_receive_skb(skb);
1251*4882a593Smuzhiyun 	}
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	return work_done;
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun 
xenvif_zerocopy_callback(struct ubuf_info * ubuf,bool zerocopy_success)1256*4882a593Smuzhiyun void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun 	unsigned long flags;
1259*4882a593Smuzhiyun 	pending_ring_idx_t index;
1260*4882a593Smuzhiyun 	struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	/* This is the only place where we grab this lock, to protect callbacks
1263*4882a593Smuzhiyun 	 * from each other.
1264*4882a593Smuzhiyun 	 */
1265*4882a593Smuzhiyun 	spin_lock_irqsave(&queue->callback_lock, flags);
1266*4882a593Smuzhiyun 	do {
1267*4882a593Smuzhiyun 		u16 pending_idx = ubuf->desc;
1268*4882a593Smuzhiyun 		ubuf = (struct ubuf_info *) ubuf->ctx;
1269*4882a593Smuzhiyun 		BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1270*4882a593Smuzhiyun 			MAX_PENDING_REQS);
1271*4882a593Smuzhiyun 		index = pending_index(queue->dealloc_prod);
1272*4882a593Smuzhiyun 		queue->dealloc_ring[index] = pending_idx;
1273*4882a593Smuzhiyun 		/* Sync with xenvif_tx_dealloc_action:
1274*4882a593Smuzhiyun 		 * insert idx then incr producer.
1275*4882a593Smuzhiyun 		 */
1276*4882a593Smuzhiyun 		smp_wmb();
1277*4882a593Smuzhiyun 		queue->dealloc_prod++;
1278*4882a593Smuzhiyun 	} while (ubuf);
1279*4882a593Smuzhiyun 	spin_unlock_irqrestore(&queue->callback_lock, flags);
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	if (likely(zerocopy_success))
1282*4882a593Smuzhiyun 		queue->stats.tx_zerocopy_success++;
1283*4882a593Smuzhiyun 	else
1284*4882a593Smuzhiyun 		queue->stats.tx_zerocopy_fail++;
1285*4882a593Smuzhiyun 	xenvif_skb_zerocopy_complete(queue);
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun 
xenvif_tx_dealloc_action(struct xenvif_queue * queue)1288*4882a593Smuzhiyun static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun 	struct gnttab_unmap_grant_ref *gop;
1291*4882a593Smuzhiyun 	pending_ring_idx_t dc, dp;
1292*4882a593Smuzhiyun 	u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1293*4882a593Smuzhiyun 	unsigned int i = 0;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	dc = queue->dealloc_cons;
1296*4882a593Smuzhiyun 	gop = queue->tx_unmap_ops;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	/* Free up any grants we have finished using */
1299*4882a593Smuzhiyun 	do {
1300*4882a593Smuzhiyun 		dp = queue->dealloc_prod;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 		/* Ensure we see all indices enqueued by all
1303*4882a593Smuzhiyun 		 * xenvif_zerocopy_callback().
1304*4882a593Smuzhiyun 		 */
1305*4882a593Smuzhiyun 		smp_rmb();
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 		while (dc != dp) {
1308*4882a593Smuzhiyun 			BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
1309*4882a593Smuzhiyun 			pending_idx =
1310*4882a593Smuzhiyun 				queue->dealloc_ring[pending_index(dc++)];
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 			pending_idx_release[gop - queue->tx_unmap_ops] =
1313*4882a593Smuzhiyun 				pending_idx;
1314*4882a593Smuzhiyun 			queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
1315*4882a593Smuzhiyun 				queue->mmap_pages[pending_idx];
1316*4882a593Smuzhiyun 			gnttab_set_unmap_op(gop,
1317*4882a593Smuzhiyun 					    idx_to_kaddr(queue, pending_idx),
1318*4882a593Smuzhiyun 					    GNTMAP_host_map,
1319*4882a593Smuzhiyun 					    queue->grant_tx_handle[pending_idx]);
1320*4882a593Smuzhiyun 			xenvif_grant_handle_reset(queue, pending_idx);
1321*4882a593Smuzhiyun 			++gop;
1322*4882a593Smuzhiyun 		}
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	} while (dp != queue->dealloc_prod);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	queue->dealloc_cons = dc;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	if (gop - queue->tx_unmap_ops > 0) {
1329*4882a593Smuzhiyun 		int ret;
1330*4882a593Smuzhiyun 		ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1331*4882a593Smuzhiyun 					NULL,
1332*4882a593Smuzhiyun 					queue->pages_to_unmap,
1333*4882a593Smuzhiyun 					gop - queue->tx_unmap_ops);
1334*4882a593Smuzhiyun 		if (ret) {
1335*4882a593Smuzhiyun 			netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
1336*4882a593Smuzhiyun 				   gop - queue->tx_unmap_ops, ret);
1337*4882a593Smuzhiyun 			for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1338*4882a593Smuzhiyun 				if (gop[i].status != GNTST_okay)
1339*4882a593Smuzhiyun 					netdev_err(queue->vif->dev,
1340*4882a593Smuzhiyun 						   " host_addr: 0x%llx handle: 0x%x status: %d\n",
1341*4882a593Smuzhiyun 						   gop[i].host_addr,
1342*4882a593Smuzhiyun 						   gop[i].handle,
1343*4882a593Smuzhiyun 						   gop[i].status);
1344*4882a593Smuzhiyun 			}
1345*4882a593Smuzhiyun 			BUG();
1346*4882a593Smuzhiyun 		}
1347*4882a593Smuzhiyun 	}
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1350*4882a593Smuzhiyun 		xenvif_idx_release(queue, pending_idx_release[i],
1351*4882a593Smuzhiyun 				   XEN_NETIF_RSP_OKAY);
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun /* Called after netfront has transmitted */
xenvif_tx_action(struct xenvif_queue * queue,int budget)1356*4882a593Smuzhiyun int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1357*4882a593Smuzhiyun {
1358*4882a593Smuzhiyun 	unsigned nr_mops = 0, nr_cops = 0;
1359*4882a593Smuzhiyun 	int work_done, ret;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	if (unlikely(!tx_work_todo(queue)))
1362*4882a593Smuzhiyun 		return 0;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	if (nr_cops == 0)
1367*4882a593Smuzhiyun 		return 0;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1370*4882a593Smuzhiyun 	if (nr_mops != 0) {
1371*4882a593Smuzhiyun 		ret = gnttab_map_refs(queue->tx_map_ops,
1372*4882a593Smuzhiyun 				      NULL,
1373*4882a593Smuzhiyun 				      queue->pages_to_map,
1374*4882a593Smuzhiyun 				      nr_mops);
1375*4882a593Smuzhiyun 		if (ret) {
1376*4882a593Smuzhiyun 			unsigned int i;
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 			netdev_err(queue->vif->dev, "Map fail: nr %u ret %d\n",
1379*4882a593Smuzhiyun 				   nr_mops, ret);
1380*4882a593Smuzhiyun 			for (i = 0; i < nr_mops; ++i)
1381*4882a593Smuzhiyun 				WARN_ON_ONCE(queue->tx_map_ops[i].status ==
1382*4882a593Smuzhiyun 				             GNTST_okay);
1383*4882a593Smuzhiyun 		}
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	work_done = xenvif_tx_submit(queue);
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	return work_done;
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun 
xenvif_idx_release(struct xenvif_queue * queue,u16 pending_idx,u8 status)1391*4882a593Smuzhiyun static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1392*4882a593Smuzhiyun 			       u8 status)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun 	struct pending_tx_info *pending_tx_info;
1395*4882a593Smuzhiyun 	pending_ring_idx_t index;
1396*4882a593Smuzhiyun 	unsigned long flags;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	pending_tx_info = &queue->pending_tx_info[pending_idx];
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	spin_lock_irqsave(&queue->response_lock, flags);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	make_tx_response(queue, &pending_tx_info->req,
1403*4882a593Smuzhiyun 			 pending_tx_info->extra_count, status);
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	/* Release the pending index before pusing the Tx response so
1406*4882a593Smuzhiyun 	 * its available before a new Tx request is pushed by the
1407*4882a593Smuzhiyun 	 * frontend.
1408*4882a593Smuzhiyun 	 */
1409*4882a593Smuzhiyun 	index = pending_index(queue->pending_prod++);
1410*4882a593Smuzhiyun 	queue->pending_ring[index] = pending_idx;
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	push_tx_responses(queue);
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	spin_unlock_irqrestore(&queue->response_lock, flags);
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 
make_tx_response(struct xenvif_queue * queue,struct xen_netif_tx_request * txp,unsigned int extra_count,s8 st)1418*4882a593Smuzhiyun static void make_tx_response(struct xenvif_queue *queue,
1419*4882a593Smuzhiyun 			     struct xen_netif_tx_request *txp,
1420*4882a593Smuzhiyun 			     unsigned int extra_count,
1421*4882a593Smuzhiyun 			     s8       st)
1422*4882a593Smuzhiyun {
1423*4882a593Smuzhiyun 	RING_IDX i = queue->tx.rsp_prod_pvt;
1424*4882a593Smuzhiyun 	struct xen_netif_tx_response *resp;
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	resp = RING_GET_RESPONSE(&queue->tx, i);
1427*4882a593Smuzhiyun 	resp->id     = txp->id;
1428*4882a593Smuzhiyun 	resp->status = st;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	while (extra_count-- != 0)
1431*4882a593Smuzhiyun 		RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	queue->tx.rsp_prod_pvt = ++i;
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun 
push_tx_responses(struct xenvif_queue * queue)1436*4882a593Smuzhiyun static void push_tx_responses(struct xenvif_queue *queue)
1437*4882a593Smuzhiyun {
1438*4882a593Smuzhiyun 	int notify;
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1441*4882a593Smuzhiyun 	if (notify)
1442*4882a593Smuzhiyun 		notify_remote_via_irq(queue->tx_irq);
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun 
xenvif_idx_unmap(struct xenvif_queue * queue,u16 pending_idx)1445*4882a593Smuzhiyun static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun 	int ret;
1448*4882a593Smuzhiyun 	struct gnttab_unmap_grant_ref tx_unmap_op;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	gnttab_set_unmap_op(&tx_unmap_op,
1451*4882a593Smuzhiyun 			    idx_to_kaddr(queue, pending_idx),
1452*4882a593Smuzhiyun 			    GNTMAP_host_map,
1453*4882a593Smuzhiyun 			    queue->grant_tx_handle[pending_idx]);
1454*4882a593Smuzhiyun 	xenvif_grant_handle_reset(queue, pending_idx);
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1457*4882a593Smuzhiyun 				&queue->mmap_pages[pending_idx], 1);
1458*4882a593Smuzhiyun 	if (ret) {
1459*4882a593Smuzhiyun 		netdev_err(queue->vif->dev,
1460*4882a593Smuzhiyun 			   "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
1461*4882a593Smuzhiyun 			   ret,
1462*4882a593Smuzhiyun 			   pending_idx,
1463*4882a593Smuzhiyun 			   tx_unmap_op.host_addr,
1464*4882a593Smuzhiyun 			   tx_unmap_op.handle,
1465*4882a593Smuzhiyun 			   tx_unmap_op.status);
1466*4882a593Smuzhiyun 		BUG();
1467*4882a593Smuzhiyun 	}
1468*4882a593Smuzhiyun }
1469*4882a593Smuzhiyun 
tx_work_todo(struct xenvif_queue * queue)1470*4882a593Smuzhiyun static inline int tx_work_todo(struct xenvif_queue *queue)
1471*4882a593Smuzhiyun {
1472*4882a593Smuzhiyun 	if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1473*4882a593Smuzhiyun 		return 1;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	return 0;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun 
tx_dealloc_work_todo(struct xenvif_queue * queue)1478*4882a593Smuzhiyun static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun 	return queue->dealloc_cons != queue->dealloc_prod;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun 
xenvif_unmap_frontend_data_rings(struct xenvif_queue * queue)1483*4882a593Smuzhiyun void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
1484*4882a593Smuzhiyun {
1485*4882a593Smuzhiyun 	if (queue->tx.sring)
1486*4882a593Smuzhiyun 		xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1487*4882a593Smuzhiyun 					queue->tx.sring);
1488*4882a593Smuzhiyun 	if (queue->rx.sring)
1489*4882a593Smuzhiyun 		xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1490*4882a593Smuzhiyun 					queue->rx.sring);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun 
xenvif_map_frontend_data_rings(struct xenvif_queue * queue,grant_ref_t tx_ring_ref,grant_ref_t rx_ring_ref)1493*4882a593Smuzhiyun int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
1494*4882a593Smuzhiyun 				   grant_ref_t tx_ring_ref,
1495*4882a593Smuzhiyun 				   grant_ref_t rx_ring_ref)
1496*4882a593Smuzhiyun {
1497*4882a593Smuzhiyun 	void *addr;
1498*4882a593Smuzhiyun 	struct xen_netif_tx_sring *txs;
1499*4882a593Smuzhiyun 	struct xen_netif_rx_sring *rxs;
1500*4882a593Smuzhiyun 	RING_IDX rsp_prod, req_prod;
1501*4882a593Smuzhiyun 	int err = -ENOMEM;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1504*4882a593Smuzhiyun 				     &tx_ring_ref, 1, &addr);
1505*4882a593Smuzhiyun 	if (err)
1506*4882a593Smuzhiyun 		goto err;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	txs = (struct xen_netif_tx_sring *)addr;
1509*4882a593Smuzhiyun 	rsp_prod = READ_ONCE(txs->rsp_prod);
1510*4882a593Smuzhiyun 	req_prod = READ_ONCE(txs->req_prod);
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun 	BACK_RING_ATTACH(&queue->tx, txs, rsp_prod, XEN_PAGE_SIZE);
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	err = -EIO;
1515*4882a593Smuzhiyun 	if (req_prod - rsp_prod > RING_SIZE(&queue->tx))
1516*4882a593Smuzhiyun 		goto err;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1519*4882a593Smuzhiyun 				     &rx_ring_ref, 1, &addr);
1520*4882a593Smuzhiyun 	if (err)
1521*4882a593Smuzhiyun 		goto err;
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 	rxs = (struct xen_netif_rx_sring *)addr;
1524*4882a593Smuzhiyun 	rsp_prod = READ_ONCE(rxs->rsp_prod);
1525*4882a593Smuzhiyun 	req_prod = READ_ONCE(rxs->req_prod);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	BACK_RING_ATTACH(&queue->rx, rxs, rsp_prod, XEN_PAGE_SIZE);
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun 	err = -EIO;
1530*4882a593Smuzhiyun 	if (req_prod - rsp_prod > RING_SIZE(&queue->rx))
1531*4882a593Smuzhiyun 		goto err;
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	return 0;
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun err:
1536*4882a593Smuzhiyun 	xenvif_unmap_frontend_data_rings(queue);
1537*4882a593Smuzhiyun 	return err;
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun 
xenvif_dealloc_kthread_should_stop(struct xenvif_queue * queue)1540*4882a593Smuzhiyun static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1541*4882a593Smuzhiyun {
1542*4882a593Smuzhiyun 	/* Dealloc thread must remain running until all inflight
1543*4882a593Smuzhiyun 	 * packets complete.
1544*4882a593Smuzhiyun 	 */
1545*4882a593Smuzhiyun 	return kthread_should_stop() &&
1546*4882a593Smuzhiyun 		!atomic_read(&queue->inflight_packets);
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun 
xenvif_dealloc_kthread(void * data)1549*4882a593Smuzhiyun int xenvif_dealloc_kthread(void *data)
1550*4882a593Smuzhiyun {
1551*4882a593Smuzhiyun 	struct xenvif_queue *queue = data;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	for (;;) {
1554*4882a593Smuzhiyun 		wait_event_interruptible(queue->dealloc_wq,
1555*4882a593Smuzhiyun 					 tx_dealloc_work_todo(queue) ||
1556*4882a593Smuzhiyun 					 xenvif_dealloc_kthread_should_stop(queue));
1557*4882a593Smuzhiyun 		if (xenvif_dealloc_kthread_should_stop(queue))
1558*4882a593Smuzhiyun 			break;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 		xenvif_tx_dealloc_action(queue);
1561*4882a593Smuzhiyun 		cond_resched();
1562*4882a593Smuzhiyun 	}
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	/* Unmap anything remaining*/
1565*4882a593Smuzhiyun 	if (tx_dealloc_work_todo(queue))
1566*4882a593Smuzhiyun 		xenvif_tx_dealloc_action(queue);
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	return 0;
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun 
make_ctrl_response(struct xenvif * vif,const struct xen_netif_ctrl_request * req,u32 status,u32 data)1571*4882a593Smuzhiyun static void make_ctrl_response(struct xenvif *vif,
1572*4882a593Smuzhiyun 			       const struct xen_netif_ctrl_request *req,
1573*4882a593Smuzhiyun 			       u32 status, u32 data)
1574*4882a593Smuzhiyun {
1575*4882a593Smuzhiyun 	RING_IDX idx = vif->ctrl.rsp_prod_pvt;
1576*4882a593Smuzhiyun 	struct xen_netif_ctrl_response rsp = {
1577*4882a593Smuzhiyun 		.id = req->id,
1578*4882a593Smuzhiyun 		.type = req->type,
1579*4882a593Smuzhiyun 		.status = status,
1580*4882a593Smuzhiyun 		.data = data,
1581*4882a593Smuzhiyun 	};
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 	*RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
1584*4882a593Smuzhiyun 	vif->ctrl.rsp_prod_pvt = ++idx;
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun 
push_ctrl_response(struct xenvif * vif)1587*4882a593Smuzhiyun static void push_ctrl_response(struct xenvif *vif)
1588*4882a593Smuzhiyun {
1589*4882a593Smuzhiyun 	int notify;
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
1592*4882a593Smuzhiyun 	if (notify)
1593*4882a593Smuzhiyun 		notify_remote_via_irq(vif->ctrl_irq);
1594*4882a593Smuzhiyun }
1595*4882a593Smuzhiyun 
process_ctrl_request(struct xenvif * vif,const struct xen_netif_ctrl_request * req)1596*4882a593Smuzhiyun static void process_ctrl_request(struct xenvif *vif,
1597*4882a593Smuzhiyun 				 const struct xen_netif_ctrl_request *req)
1598*4882a593Smuzhiyun {
1599*4882a593Smuzhiyun 	u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
1600*4882a593Smuzhiyun 	u32 data = 0;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	switch (req->type) {
1603*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
1604*4882a593Smuzhiyun 		status = xenvif_set_hash_alg(vif, req->data[0]);
1605*4882a593Smuzhiyun 		break;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
1608*4882a593Smuzhiyun 		status = xenvif_get_hash_flags(vif, &data);
1609*4882a593Smuzhiyun 		break;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
1612*4882a593Smuzhiyun 		status = xenvif_set_hash_flags(vif, req->data[0]);
1613*4882a593Smuzhiyun 		break;
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
1616*4882a593Smuzhiyun 		status = xenvif_set_hash_key(vif, req->data[0],
1617*4882a593Smuzhiyun 					     req->data[1]);
1618*4882a593Smuzhiyun 		break;
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
1621*4882a593Smuzhiyun 		status = XEN_NETIF_CTRL_STATUS_SUCCESS;
1622*4882a593Smuzhiyun 		data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
1623*4882a593Smuzhiyun 		break;
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
1626*4882a593Smuzhiyun 		status = xenvif_set_hash_mapping_size(vif,
1627*4882a593Smuzhiyun 						      req->data[0]);
1628*4882a593Smuzhiyun 		break;
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
1631*4882a593Smuzhiyun 		status = xenvif_set_hash_mapping(vif, req->data[0],
1632*4882a593Smuzhiyun 						 req->data[1],
1633*4882a593Smuzhiyun 						 req->data[2]);
1634*4882a593Smuzhiyun 		break;
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	default:
1637*4882a593Smuzhiyun 		break;
1638*4882a593Smuzhiyun 	}
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	make_ctrl_response(vif, req, status, data);
1641*4882a593Smuzhiyun 	push_ctrl_response(vif);
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
xenvif_ctrl_action(struct xenvif * vif)1644*4882a593Smuzhiyun static void xenvif_ctrl_action(struct xenvif *vif)
1645*4882a593Smuzhiyun {
1646*4882a593Smuzhiyun 	for (;;) {
1647*4882a593Smuzhiyun 		RING_IDX req_prod, req_cons;
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun 		req_prod = vif->ctrl.sring->req_prod;
1650*4882a593Smuzhiyun 		req_cons = vif->ctrl.req_cons;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 		/* Make sure we can see requests before we process them. */
1653*4882a593Smuzhiyun 		rmb();
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 		if (req_cons == req_prod)
1656*4882a593Smuzhiyun 			break;
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 		while (req_cons != req_prod) {
1659*4882a593Smuzhiyun 			struct xen_netif_ctrl_request req;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 			RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
1662*4882a593Smuzhiyun 			req_cons++;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 			process_ctrl_request(vif, &req);
1665*4882a593Smuzhiyun 		}
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 		vif->ctrl.req_cons = req_cons;
1668*4882a593Smuzhiyun 		vif->ctrl.sring->req_event = req_cons + 1;
1669*4882a593Smuzhiyun 	}
1670*4882a593Smuzhiyun }
1671*4882a593Smuzhiyun 
xenvif_ctrl_work_todo(struct xenvif * vif)1672*4882a593Smuzhiyun static bool xenvif_ctrl_work_todo(struct xenvif *vif)
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun 	if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
1675*4882a593Smuzhiyun 		return true;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	return false;
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun 
xenvif_ctrl_irq_fn(int irq,void * data)1680*4882a593Smuzhiyun irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
1681*4882a593Smuzhiyun {
1682*4882a593Smuzhiyun 	struct xenvif *vif = data;
1683*4882a593Smuzhiyun 	unsigned int eoi_flag = XEN_EOI_FLAG_SPURIOUS;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	while (xenvif_ctrl_work_todo(vif)) {
1686*4882a593Smuzhiyun 		xenvif_ctrl_action(vif);
1687*4882a593Smuzhiyun 		eoi_flag = 0;
1688*4882a593Smuzhiyun 	}
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	xen_irq_lateeoi(irq, eoi_flag);
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	return IRQ_HANDLED;
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun 
netback_init(void)1695*4882a593Smuzhiyun static int __init netback_init(void)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun 	int rc = 0;
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	if (!xen_domain())
1700*4882a593Smuzhiyun 		return -ENODEV;
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	/* Allow as many queues as there are CPUs but max. 8 if user has not
1703*4882a593Smuzhiyun 	 * specified a value.
1704*4882a593Smuzhiyun 	 */
1705*4882a593Smuzhiyun 	if (xenvif_max_queues == 0)
1706*4882a593Smuzhiyun 		xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
1707*4882a593Smuzhiyun 					  num_online_cpus());
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1710*4882a593Smuzhiyun 		pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1711*4882a593Smuzhiyun 			fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1712*4882a593Smuzhiyun 		fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1713*4882a593Smuzhiyun 	}
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	rc = xenvif_xenbus_init();
1716*4882a593Smuzhiyun 	if (rc)
1717*4882a593Smuzhiyun 		goto failed_init;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
1720*4882a593Smuzhiyun 	xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
1721*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun 	return 0;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun failed_init:
1726*4882a593Smuzhiyun 	return rc;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun module_init(netback_init);
1730*4882a593Smuzhiyun 
netback_fini(void)1731*4882a593Smuzhiyun static void __exit netback_fini(void)
1732*4882a593Smuzhiyun {
1733*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
1734*4882a593Smuzhiyun 	debugfs_remove_recursive(xen_netback_dbg_root);
1735*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1736*4882a593Smuzhiyun 	xenvif_xenbus_fini();
1737*4882a593Smuzhiyun }
1738*4882a593Smuzhiyun module_exit(netback_fini);
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
1741*4882a593Smuzhiyun MODULE_ALIAS("xen-backend:vif");
1742