1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License version 2
4 * as published by the Free Software Foundation; or, when distributed
5 * separately from the Linux kernel or incorporated into other
6 * software packages, subject to the following license:
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this source file (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy, modify,
11 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 */
26
27#ifndef __XEN_NETBACK__COMMON_H__
28#define __XEN_NETBACK__COMMON_H__
29
30#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
31
32#include <linux/module.h>
33#include <linux/interrupt.h>
34#include <linux/slab.h>
35#include <linux/ip.h>
36#include <linux/in.h>
37#include <linux/io.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/wait.h>
41#include <linux/sched.h>
42
43#include <xen/interface/io/netif.h>
44#include <xen/interface/grant_table.h>
45#include <xen/grant_table.h>
46#include <xen/xenbus.h>
47#include <linux/debugfs.h>
48
49typedef unsigned int pending_ring_idx_t;
50#define INVALID_PENDING_RING_IDX (~0U)
51
52struct pending_tx_info {
53	struct xen_netif_tx_request req; /* tx request */
54	/* Callback data for released SKBs. The callback is always
55	 * xenvif_zerocopy_callback, desc contains the pending_idx, which is
56	 * also an index in pending_tx_info array. It is initialized in
57	 * xenvif_alloc and it never changes.
58	 * skb_shinfo(skb)->destructor_arg points to the first mapped slot's
59	 * callback_struct in this array of struct pending_tx_info's, then ctx
60	 * to the next, or NULL if there is no more slot for this skb.
61	 * ubuf_to_vif is a helper which finds the struct xenvif from a pointer
62	 * to this field.
63	 */
64	struct ubuf_info callback_struct;
65};
66
67#define XEN_NETIF_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
68#define XEN_NETIF_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
69
70struct xenvif_rx_meta {
71	int id;
72	int size;
73	int gso_type;
74	int gso_size;
75};
76
77#define GSO_BIT(type) \
78	(1 << XEN_NETIF_GSO_TYPE_ ## type)
79
80/* Discriminate from any valid pending_idx value. */
81#define INVALID_PENDING_IDX 0xFFFF
82
83#define MAX_BUFFER_OFFSET PAGE_SIZE
84
85#define MAX_PENDING_REQS XEN_NETIF_TX_RING_SIZE
86
87/* It's possible for an skb to have a maximal number of frags
88 * but still be less than MAX_BUFFER_OFFSET in size. Thus the
89 * worst-case number of copy operations is MAX_SKB_FRAGS per
90 * ring slot.
91 */
92#define MAX_GRANT_COPY_OPS (MAX_SKB_FRAGS * XEN_NETIF_RX_RING_SIZE)
93
94#define NETBACK_INVALID_HANDLE -1
95
96/* To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
97 * the maximum slots a valid packet can use. Now this value is defined
98 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
99 * all backend.
100 */
101#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
102
103/* Queue name is interface name with "-qNNN" appended */
104#define QUEUE_NAME_SIZE (IFNAMSIZ + 5)
105
106/* IRQ name is queue name with "-tx" or "-rx" appended */
107#define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
108
109struct xenvif;
110
111struct xenvif_stats {
112	/* Stats fields to be updated per-queue.
113	 * A subset of struct net_device_stats that contains only the
114	 * fields that are updated in netback.c for each queue.
115	 */
116	unsigned int rx_bytes;
117	unsigned int rx_packets;
118	unsigned int tx_bytes;
119	unsigned int tx_packets;
120
121	/* Additional stats used by xenvif */
122	unsigned long rx_gso_checksum_fixup;
123	unsigned long tx_zerocopy_sent;
124	unsigned long tx_zerocopy_success;
125	unsigned long tx_zerocopy_fail;
126	unsigned long tx_frag_overflow;
127};
128
129struct xenvif_queue { /* Per-queue data for xenvif */
130	unsigned int id; /* Queue ID, 0-based */
131	char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
132	struct xenvif *vif; /* Parent VIF */
133
134	/* Use NAPI for guest TX */
135	struct napi_struct napi;
136	/* When feature-split-event-channels = 0, tx_irq = rx_irq. */
137	unsigned int tx_irq;
138	/* Only used when feature-split-event-channels = 1 */
139	char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
140	struct xen_netif_tx_back_ring tx;
141	struct sk_buff_head tx_queue;
142	struct page *mmap_pages[MAX_PENDING_REQS];
143	pending_ring_idx_t pending_prod;
144	pending_ring_idx_t pending_cons;
145	u16 pending_ring[MAX_PENDING_REQS];
146	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
147	grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
148
149	struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
150	struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
151	struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
152	/* passed to gnttab_[un]map_refs with pages under (un)mapping */
153	struct page *pages_to_map[MAX_PENDING_REQS];
154	struct page *pages_to_unmap[MAX_PENDING_REQS];
155
156	/* This prevents zerocopy callbacks  to race over dealloc_ring */
157	spinlock_t callback_lock;
158	/* This prevents dealloc thread and NAPI instance to race over response
159	 * creation and pending_ring in xenvif_idx_release. In xenvif_tx_err
160	 * it only protect response creation
161	 */
162	spinlock_t response_lock;
163	pending_ring_idx_t dealloc_prod;
164	pending_ring_idx_t dealloc_cons;
165	u16 dealloc_ring[MAX_PENDING_REQS];
166	struct task_struct *dealloc_task;
167	wait_queue_head_t dealloc_wq;
168	atomic_t inflight_packets;
169
170	/* Use kthread for guest RX */
171	struct task_struct *task;
172	wait_queue_head_t wq;
173	/* When feature-split-event-channels = 0, tx_irq = rx_irq. */
174	unsigned int rx_irq;
175	/* Only used when feature-split-event-channels = 1 */
176	char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
177	struct xen_netif_rx_back_ring rx;
178	struct sk_buff_head rx_queue;
179
180	unsigned int rx_queue_max;
181	unsigned int rx_queue_len;
182	unsigned long last_rx_time;
183	bool stalled;
184
185	struct gnttab_copy grant_copy_op[MAX_GRANT_COPY_OPS];
186
187	/* We create one meta structure per ring request we consume, so
188	 * the maximum number is the same as the ring size.
189	 */
190	struct xenvif_rx_meta meta[XEN_NETIF_RX_RING_SIZE];
191
192	/* Transmit shaping: allow 'credit_bytes' every 'credit_usec'. */
193	unsigned long   credit_bytes;
194	unsigned long   credit_usec;
195	unsigned long   remaining_credit;
196	struct timer_list credit_timeout;
197	u64 credit_window_start;
198
199	/* Statistics */
200	struct xenvif_stats stats;
201};
202
203/* Maximum number of Rx slots a to-guest packet may use, including the
204 * slot needed for GSO meta-data.
205 */
206#define XEN_NETBK_RX_SLOTS_MAX (MAX_SKB_FRAGS + 1)
207
208enum state_bit_shift {
209	/* This bit marks that the vif is connected */
210	VIF_STATUS_CONNECTED,
211};
212
213struct xenvif {
214	/* Unique identifier for this interface. */
215	domid_t          domid;
216	unsigned int     handle;
217
218	u8               fe_dev_addr[6];
219
220	/* Frontend feature information. */
221	int gso_mask;
222	int gso_prefix_mask;
223
224	u8 can_sg:1;
225	u8 ip_csum:1;
226	u8 ipv6_csum:1;
227
228	/* Is this interface disabled? True when backend discovers
229	 * frontend is rogue.
230	 */
231	bool disabled;
232	unsigned long status;
233	unsigned long drain_timeout;
234	unsigned long stall_timeout;
235
236	/* Queues */
237	struct xenvif_queue *queues;
238	unsigned int num_queues; /* active queues, resource allocated */
239	unsigned int stalled_queues;
240
241	struct xenbus_watch credit_watch;
242
243	spinlock_t lock;
244
245#ifdef CONFIG_DEBUG_FS
246	struct dentry *xenvif_dbg_root;
247#endif
248
249	/* Miscellaneous private stuff. */
250	struct net_device *dev;
251};
252
253struct xenvif_rx_cb {
254	unsigned long expires;
255	int meta_slots_used;
256};
257
258#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
259
260static inline struct xenbus_device *xenvif_to_xenbus_device(struct xenvif *vif)
261{
262	return to_xenbus_device(vif->dev->dev.parent);
263}
264
265void xenvif_tx_credit_callback(unsigned long data);
266
267struct xenvif *xenvif_alloc(struct device *parent,
268			    domid_t domid,
269			    unsigned int handle);
270
271int xenvif_init_queue(struct xenvif_queue *queue);
272void xenvif_deinit_queue(struct xenvif_queue *queue);
273
274int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref,
275		   unsigned long rx_ring_ref, unsigned int tx_evtchn,
276		   unsigned int rx_evtchn);
277void xenvif_disconnect(struct xenvif *vif);
278void xenvif_free(struct xenvif *vif);
279
280int xenvif_xenbus_init(void);
281void xenvif_xenbus_fini(void);
282
283int xenvif_schedulable(struct xenvif *vif);
284
285int xenvif_queue_stopped(struct xenvif_queue *queue);
286void xenvif_wake_queue(struct xenvif_queue *queue);
287
288/* (Un)Map communication rings. */
289void xenvif_unmap_frontend_rings(struct xenvif_queue *queue);
290int xenvif_map_frontend_rings(struct xenvif_queue *queue,
291			      grant_ref_t tx_ring_ref,
292			      grant_ref_t rx_ring_ref);
293
294/* Check for SKBs from frontend and schedule backend processing */
295void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue);
296
297/* Prevent the device from generating any further traffic. */
298void xenvif_carrier_off(struct xenvif *vif);
299
300int xenvif_tx_action(struct xenvif_queue *queue, int budget);
301
302int xenvif_kthread_guest_rx(void *data);
303void xenvif_kick_thread(struct xenvif_queue *queue);
304
305int xenvif_dealloc_kthread(void *data);
306
307void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb);
308
309/* Determine whether the needed number of slots (req) are available,
310 * and set req_event if not.
311 */
312bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed);
313
314void xenvif_carrier_on(struct xenvif *vif);
315
316/* Callback from stack when TX packet can be released */
317void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success);
318
319/* Unmap a pending page and release it back to the guest */
320void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx);
321
322static inline pending_ring_idx_t nr_pending_reqs(struct xenvif_queue *queue)
323{
324	return MAX_PENDING_REQS -
325		queue->pending_prod + queue->pending_cons;
326}
327
328/* Callback from stack when TX packet can be released */
329void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success);
330
331irqreturn_t xenvif_interrupt(int irq, void *dev_id);
332
333extern bool separate_tx_rx_irq;
334
335extern unsigned int rx_drain_timeout_msecs;
336extern unsigned int rx_stall_timeout_msecs;
337extern unsigned int xenvif_max_queues;
338
339#ifdef CONFIG_DEBUG_FS
340extern struct dentry *xen_netback_dbg_root;
341#endif
342
343void xenvif_skb_zerocopy_prepare(struct xenvif_queue *queue,
344				 struct sk_buff *skb);
345void xenvif_skb_zerocopy_complete(struct xenvif_queue *queue);
346
347#endif /* __XEN_NETBACK__COMMON_H__ */
348