Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / netfront / netfront.h
1 /******************************************************************************
2  * Virtual network driver for conversing with remote driver backends.
3  *
4  * Copyright (c) 2002-2005, K A Fraser
5  * Copyright (c) 2005, XenSource Ltd
6  * Copyright (C) 2007 Solarflare Communications, Inc.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #ifndef NETFRONT_H
34 #define NETFRONT_H
35
36 #include <xen/interface/io/netif.h>
37 #include <linux/slab.h>
38 #include <linux/netdevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/list.h>
41
42 #define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE)
43 #define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE)
44
45 #include <xen/xenbus.h>
46
47 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
48 #include <xen/platform-compat.h>
49 #endif
50
51 struct netfront_stats {
52         u64                     rx_packets;
53         u64                     tx_packets;
54         u64                     rx_bytes;
55         u64                     tx_bytes;
56         struct u64_stats_sync   syncp;
57 };
58
59 /* 
60  * Function pointer table for hooks into a network acceleration
61  * plugin.  These are called at appropriate points from the netfront
62  * driver 
63  */
64 struct netfront_accel_hooks {
65         /* 
66          * new_device: Accelerator hook to ask the plugin to support a
67          * new network interface
68          */
69         int (*new_device)(struct net_device *net_dev, struct xenbus_device *dev);
70         /*
71          * remove: Opposite of new_device
72          */
73         int (*remove)(struct xenbus_device *dev);
74         /*
75          * The net_device is being polled, check the accelerated
76          * hardware for any pending packets
77          */
78         int (*netdev_poll)(struct net_device *dev, int *pbudget);
79         /*
80          * start_xmit: Used to give the accelerated plugin the option
81          * of sending a packet.  Returns non-zero if has done so, or
82          * zero to decline and force the packet onto normal send
83          * path
84          */
85         int (*start_xmit)(struct sk_buff *skb, struct net_device *dev);
86         /* 
87          * start/stop_napi_interrupts Used by netfront to indicate
88          * when napi interrupts should be enabled or disabled 
89          */
90         int (*start_napi_irq)(struct net_device *dev);
91         void (*stop_napi_irq)(struct net_device *dev);
92         /* 
93          * Called before re-enabling the TX queue to check the fast
94          * path has slots too
95          */
96         int (*check_ready)(struct net_device *dev);
97         /*
98          * Get the fastpath network statistics
99          */
100         int (*get_stats)(struct net_device *dev,
101                          struct net_device_stats *dev_stats,
102                          struct netfront_stats *link_stats);
103 };
104
105
106 /* Version of API/protocol for communication between netfront and
107    acceleration plugin supported */
108 #define NETFRONT_ACCEL_VERSION 0x00010003
109
110 /* 
111  * Per-netfront device state for the accelerator.  This is used to
112  * allow efficient per-netfront device access to the accelerator
113  * hooks 
114  */
115 struct netfront_accel_vif_state {
116         struct list_head link;
117
118         struct xenbus_device *dev;
119         struct netfront_info *np;
120         struct netfront_accel_hooks *hooks;
121
122         /* Watch on the accelerator configuration value */
123         struct xenbus_watch accel_watch;
124         /* Work item to process change in accelerator */
125         struct work_struct accel_work;
126         /* The string from xenbus last time accel_watch fired */
127         char *accel_frontend;
128 }; 
129
130 /* 
131  * Per-accelerator state stored in netfront.  These form a list that
132  * is used to track which devices are accelerated by which plugins,
133  * and what plugins are available/have been requested 
134  */
135 struct netfront_accelerator {
136         /* Used to make a list */
137         struct list_head link;
138         /* ID of the accelerator */
139         int id;
140         /*
141          * String describing the accelerator.  Currently this is the
142          * name of the accelerator module.  This is provided by the
143          * backend accelerator through xenstore 
144          */
145         char *frontend;
146         /* The hooks into the accelerator plugin module */
147         struct netfront_accel_hooks *hooks;
148
149         /* 
150          * List of per-netfront device state (struct
151          * netfront_accel_vif_state) for each netfront device that is
152          * using this accelerator
153          */
154         struct list_head vif_states;
155         spinlock_t vif_states_lock;
156 };
157
158 struct netfront_info {
159         struct list_head list;
160         struct net_device *netdev;
161
162         struct netif_tx_front_ring tx;
163         struct netif_rx_front_ring rx;
164
165         spinlock_t   tx_lock;
166         spinlock_t   rx_lock;
167
168         struct napi_struct      napi;
169
170         unsigned int irq;
171         unsigned int copying_receiver;
172         unsigned int carrier;
173
174         /* Receive-ring batched refills. */
175 #define RX_MIN_TARGET 8
176 #define RX_DFL_MIN_TARGET 64
177 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
178         unsigned rx_min_target, rx_max_target, rx_target;
179         struct sk_buff_head rx_batch;
180
181         struct timer_list rx_refill_timer;
182
183         /*
184          * {tx,rx}_skbs store outstanding skbuffs. The first entry in tx_skbs
185          * is an index into a chain of free entries.
186          */
187         struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
188         struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
189
190 #define TX_MAX_TARGET min_t(int, NET_TX_RING_SIZE, 256)
191         grant_ref_t gref_tx_head;
192         grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
193         grant_ref_t gref_rx_head;
194         grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
195
196         struct xenbus_device *xbdev;
197         int tx_ring_ref;
198         int rx_ring_ref;
199         u8 mac[ETH_ALEN];
200
201         unsigned long rx_pfn_array[NET_RX_RING_SIZE];
202         struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
203         struct mmu_update rx_mmu[NET_RX_RING_SIZE];
204
205         /* Statistics */
206         struct netfront_stats __percpu *stats;
207         unsigned long rx_gso_csum_fixups;
208
209         /* Private pointer to state internal to accelerator module */
210         void *accel_priv;
211         /* The accelerator used by this netfront device */
212         struct netfront_accelerator *accelerator;
213         /* The accelerator state for this netfront device */
214         struct netfront_accel_vif_state accel_vif_state;
215 };
216
217
218 /* Exported Functions */
219
220 /*
221  * Called by an accelerator plugin module when it has loaded.
222  *
223  * frontend: the string describing the accelerator, currently the module name 
224  * hooks: the hooks for netfront to use to call into the accelerator
225  * version: the version of API between frontend and plugin requested
226  * 
227  * return: 0 on success, <0 on error, >0 (with version supported) on
228  * version mismatch
229  */
230 extern int netfront_accelerator_loaded(int version, const char *frontend, 
231                                        struct netfront_accel_hooks *hooks);
232
233 /* 
234  * Called by an accelerator plugin module when it is about to unload.
235  *
236  * frontend: the string describing the accelerator.  Must match the
237  * one passed to netfront_accelerator_loaded()
238  */ 
239 extern void netfront_accelerator_stop(const char *frontend);
240
241 /* 
242  * Called by an accelerator before waking the net device's TX queue to
243  * ensure the slow path has available slots.  Returns true if OK to
244  * wake, false if still busy 
245  */
246 extern int netfront_check_queue_ready(struct net_device *net_dev);
247
248
249 /* Internal-to-netfront Functions */
250
251 /* 
252  * Call into accelerator and check to see if it has tx space before we
253  * wake the net device's TX queue.  Returns true if OK to wake, false
254  * if still busy
255  */ 
256 extern 
257 int netfront_check_accelerator_queue_ready(struct net_device *dev,
258                                            struct netfront_info *np);
259 extern
260 int netfront_accelerator_call_remove(struct netfront_info *np,
261                                      struct xenbus_device *dev);
262 extern
263 int netfront_accelerator_suspend(struct netfront_info *np,
264                                  struct xenbus_device *dev);
265 extern
266 int netfront_accelerator_suspend_cancel(struct netfront_info *np,
267                                         struct xenbus_device *dev);
268 extern
269 void netfront_accelerator_resume(struct netfront_info *np,
270                                  struct xenbus_device *dev);
271 extern
272 void netfront_accelerator_call_stop_napi_irq(struct netfront_info *np,
273                                              struct net_device *dev);
274 extern
275 int netfront_accelerator_call_get_stats(struct netfront_info *np,
276                                         struct net_device *dev);
277 extern
278 void netfront_accelerator_add_watch(struct netfront_info *np);
279
280 extern
281 void netif_init_accel(void);
282 extern
283 void netif_exit_accel(void);
284
285 extern
286 void init_accelerator_vif(struct netfront_info *np,
287                           struct xenbus_device *dev);
288 #endif /* NETFRONT_H */