Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / netback / common.h
1 /******************************************************************************
2  * arch/xen/drivers/netif/backend/common.h
3  * 
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation; or, when distributed
7  * separately from the Linux kernel or incorporated into other
8  * software packages, subject to the following license:
9  * 
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this source file (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use, copy, modify,
13  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
14  * and to permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26  * IN THE SOFTWARE.
27  */
28
29 #ifndef __NETIF__BACKEND__COMMON_H__
30 #define __NETIF__BACKEND__COMMON_H__
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/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/wait.h>
40 #include <xen/interface/io/netif.h>
41 #include <xen/xenbus.h>
42 #include <xen/interface/event_channel.h>
43
44 #define DPRINTK(_f, _a...)                      \
45         pr_debug("(file=%s, line=%d) " _f,      \
46                  __FILE__ , __LINE__ , ## _a )
47 #define IPRINTK(fmt, args...) pr_info("xen_net: " fmt, ##args)
48 #define WPRINTK(fmt, args...) pr_warning("xen_net: " fmt, ##args)
49
50 typedef struct netif_st {
51         /* Unique identifier for this interface. */
52         domid_t          domid;
53         unsigned int     group;
54         unsigned int     handle;
55
56         u8               fe_dev_addr[6];
57
58         unsigned int     irq;
59
60         /* The shared rings and indexes. */
61         netif_tx_back_ring_t tx;
62         netif_rx_back_ring_t rx;
63         struct vm_struct *tx_comms_area;
64         struct vm_struct *rx_comms_area;
65
66         /* Flags that must not be set in dev->features */
67         int features_disabled;
68
69         /* Frontend feature information. */
70         u8 can_sg:1;
71         u8 gso:1;
72         u8 csum:1;
73
74         /* Internal feature information. */
75         u8 can_queue:1; /* can queue packets for receiver? */
76         u8 copying_receiver:1;  /* copy packets to receiver?       */
77
78         /* Allow netif_be_start_xmit() to peek ahead in the rx request ring. */
79         RING_IDX rx_req_cons_peek;
80
81         /* Transmit shaping: allow 'credit_bytes' every 'credit_usec'. */
82         unsigned long   credit_bytes;
83         unsigned long   credit_usec;
84         unsigned long   remaining_credit;
85         struct timer_list credit_timeout;
86
87         /* Enforce draining of the transmit queue. */
88         struct timer_list tx_queue_timeout;
89
90         /* Statistics */
91         unsigned long nr_copied_skbs;
92         unsigned long rx_gso_csum_fixups;
93
94         /* Miscellaneous private stuff. */
95         struct list_head list;  /* scheduling list */
96         atomic_t         refcnt;
97         struct net_device *dev;
98
99         unsigned int carrier;
100
101         wait_queue_head_t waiting_to_free;
102 } netif_t;
103
104 /*
105  * Implement our own carrier flag: the network stack's version causes delays
106  * when the carrier is re-enabled (in particular, dev_activate() may not
107  * immediately be called, which can cause packet loss; also the etherbridge
108  * can be rather lazy in activating its port).
109  */
110 #define netback_carrier_on(netif)       ((netif)->carrier = 1)
111 #define netback_carrier_off(netif)      ((netif)->carrier = 0)
112 #define netback_carrier_ok(netif)       ((netif)->carrier)
113
114 enum {
115         NETBK_DONT_COPY_SKB,
116         NETBK_DELAYED_COPY_SKB,
117         NETBK_ALWAYS_COPY_SKB,
118 };
119
120 extern int netbk_copy_skb_mode;
121
122 /* Function pointers into netback accelerator plugin modules */
123 struct netback_accel_hooks {
124         struct module *owner;
125         int  (*probe)(struct xenbus_device *dev);
126         int (*remove)(struct xenbus_device *dev);
127 };
128
129 /* Structure to track the state of a netback accelerator plugin */
130 struct netback_accelerator {
131         struct list_head link;
132         int id;
133         char *eth_name;
134         atomic_t use_count;
135         struct netback_accel_hooks *hooks;
136 };
137
138 struct backend_info {
139         struct xenbus_device *dev;
140         netif_t *netif;
141         enum xenbus_state frontend_state;
142         struct xenbus_watch hotplug_status_watch;
143         int have_hotplug_status_watch:1;
144
145         /* State relating to the netback accelerator */
146         void *netback_accel_priv;
147         /* The accelerator that this backend is currently using */
148         struct netback_accelerator *accelerator;
149 };
150
151 #define NETBACK_ACCEL_VERSION 0x00010001
152
153 /* 
154  * Connect an accelerator plugin module to netback.  Returns zero on
155  * success, < 0 on error, > 0 (with highest version number supported)
156  * if version mismatch.
157  */
158 extern int netback_connect_accelerator(unsigned version,
159                                        int id, const char *eth_name, 
160                                        struct netback_accel_hooks *hooks);
161 /* Disconnect a previously connected accelerator plugin module */
162 extern void netback_disconnect_accelerator(int id, const char *eth_name);
163
164
165 extern
166 void netback_probe_accelerators(struct backend_info *be,
167                                 struct xenbus_device *dev);
168 extern
169 void netback_remove_accelerators(struct backend_info *be,
170                                  struct xenbus_device *dev);
171 extern
172 void netif_accel_init(void);
173
174
175 #define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE)
176 #define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE)
177
178 void netif_disconnect(struct backend_info *be);
179
180 netif_t *netif_alloc(struct device *parent, domid_t domid, unsigned int handle);
181 int netif_map(struct backend_info *be, grant_ref_t tx_ring_ref,
182               grant_ref_t rx_ring_ref, evtchn_port_t evtchn);
183
184 #define netif_get(_b) (atomic_inc(&(_b)->refcnt))
185 #define netif_put(_b)                                           \
186         do {                                                    \
187                 if ( atomic_dec_and_test(&(_b)->refcnt) )       \
188                         wake_up(&(_b)->waiting_to_free);        \
189         } while (0)
190
191 void netif_xenbus_init(void);
192
193 #define netif_schedulable(netif)                                \
194         (netif_running((netif)->dev) && netback_carrier_ok(netif))
195
196 void netif_schedule_work(netif_t *netif);
197 void netif_deschedule_work(netif_t *netif);
198
199 int netif_be_start_xmit(struct sk_buff *skb, struct net_device *dev);
200 irqreturn_t netif_be_int(int irq, void *dev_id);
201
202 static inline int netbk_can_queue(struct net_device *dev)
203 {
204         netif_t *netif = netdev_priv(dev);
205         return netif->can_queue;
206 }
207
208 static inline int netbk_can_sg(struct net_device *dev)
209 {
210         netif_t *netif = netdev_priv(dev);
211         return netif->can_sg;
212 }
213
214 struct pending_tx_info {
215         netif_tx_request_t req;
216         netif_t *netif;
217 };
218 typedef unsigned int pending_ring_idx_t;
219
220 struct netbk_rx_meta {
221         skb_frag_t frag;
222         int id;
223         u8 copy:1;
224 };
225
226 struct netbk_tx_pending_inuse {
227         struct list_head list;
228         unsigned long alloc_time;
229 };
230
231 #define MAX_PENDING_REQS (1U << CONFIG_XEN_NETDEV_TX_SHIFT)
232 #define MAX_MFN_ALLOC 64
233
234 struct xen_netbk {
235         union {
236                 struct {
237                         struct tasklet_struct net_tx_tasklet;
238                         struct tasklet_struct net_rx_tasklet;
239                 };
240                 struct {
241                         wait_queue_head_t netbk_action_wq;
242                         struct task_struct *task;
243                 };
244         };
245
246         struct sk_buff_head rx_queue;
247         struct sk_buff_head tx_queue;
248
249         struct timer_list net_timer;
250         struct timer_list tx_pending_timer;
251
252         pending_ring_idx_t pending_prod;
253         pending_ring_idx_t pending_cons;
254         pending_ring_idx_t dealloc_prod;
255         pending_ring_idx_t dealloc_cons;
256
257         struct list_head pending_inuse_head;
258         struct list_head schedule_list;
259
260         spinlock_t schedule_list_lock;
261         spinlock_t release_lock;
262
263         struct page **mmap_pages;
264
265         atomic_t nr_groups;
266         unsigned int alloc_index;
267
268         struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
269         struct netbk_tx_pending_inuse pending_inuse[MAX_PENDING_REQS];
270         struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
271         struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
272
273         grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
274         u16 pending_ring[MAX_PENDING_REQS];
275         u16 dealloc_ring[MAX_PENDING_REQS];
276
277         struct multicall_entry rx_mcl[NET_RX_RING_SIZE+3];
278         struct mmu_update rx_mmu[NET_RX_RING_SIZE];
279         struct gnttab_transfer grant_trans_op[NET_RX_RING_SIZE];
280         struct gnttab_copy grant_copy_op[NET_RX_RING_SIZE];
281         DECLARE_BITMAP(rx_notify, NR_DYNIRQS);
282 #if !defined(NR_DYNIRQS)
283 # error
284 #elif NR_DYNIRQS <= 0x10000
285         u16 notify_list[NET_RX_RING_SIZE];
286 #else
287         int notify_list[NET_RX_RING_SIZE];
288 #endif
289         struct netbk_rx_meta meta[NET_RX_RING_SIZE];
290
291         unsigned long mfn_list[MAX_MFN_ALLOC];
292 };
293
294 extern struct xen_netbk *xen_netbk;
295 extern unsigned int netbk_nr_groups;
296
297 #endif /* __NETIF__BACKEND__COMMON_H__ */