1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 * Copyright (C) 2007 Solarflare Communications, Inc.
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:
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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
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>
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)
45 #include <xen/xenbus.h>
47 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
48 #include <xen/platform-compat.h>
51 struct netfront_stats {
56 struct u64_stats_sync syncp;
60 * Function pointer table for hooks into a network acceleration
61 * plugin. These are called at appropriate points from the netfront
64 struct netfront_accel_hooks {
66 * new_device: Accelerator hook to ask the plugin to support a
67 * new network interface
69 int (*new_device)(struct net_device *net_dev, struct xenbus_device *dev);
71 * remove: Opposite of new_device
73 int (*remove)(struct xenbus_device *dev);
75 * The net_device is being polled, check the accelerated
76 * hardware for any pending packets
78 int (*netdev_poll)(struct net_device *dev, int *pbudget);
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
85 int (*start_xmit)(struct sk_buff *skb, struct net_device *dev);
87 * start/stop_napi_interrupts Used by netfront to indicate
88 * when napi interrupts should be enabled or disabled
90 int (*start_napi_irq)(struct net_device *dev);
91 void (*stop_napi_irq)(struct net_device *dev);
93 * Called before re-enabling the TX queue to check the fast
96 int (*check_ready)(struct net_device *dev);
98 * Get the fastpath network statistics
100 int (*get_stats)(struct net_device *dev,
101 struct net_device_stats *dev_stats,
102 struct netfront_stats *link_stats);
106 /* Version of API/protocol for communication between netfront and
107 acceleration plugin supported */
108 #define NETFRONT_ACCEL_VERSION 0x00010003
111 * Per-netfront device state for the accelerator. This is used to
112 * allow efficient per-netfront device access to the accelerator
115 struct netfront_accel_vif_state {
116 struct list_head link;
118 struct xenbus_device *dev;
119 struct netfront_info *np;
120 struct netfront_accel_hooks *hooks;
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;
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
135 struct netfront_accelerator {
136 /* Used to make a list */
137 struct list_head link;
138 /* ID of the accelerator */
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
146 /* The hooks into the accelerator plugin module */
147 struct netfront_accel_hooks *hooks;
150 * List of per-netfront device state (struct
151 * netfront_accel_vif_state) for each netfront device that is
152 * using this accelerator
154 struct list_head vif_states;
155 spinlock_t vif_states_lock;
158 struct netfront_info {
159 struct list_head list;
160 struct net_device *netdev;
162 struct netif_tx_front_ring tx;
163 struct netif_rx_front_ring rx;
168 struct napi_struct napi;
171 unsigned int copying_receiver;
172 unsigned int carrier;
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;
181 struct timer_list rx_refill_timer;
184 * {tx,rx}_skbs store outstanding skbuffs. The first entry in tx_skbs
185 * is an index into a chain of free entries.
187 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
188 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
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];
196 struct xenbus_device *xbdev;
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];
206 struct netfront_stats __percpu *stats;
207 unsigned long rx_gso_csum_fixups;
209 /* Private pointer to state internal to accelerator module */
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;
218 /* Exported Functions */
221 * Called by an accelerator plugin module when it has loaded.
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
227 * return: 0 on success, <0 on error, >0 (with version supported) on
230 extern int netfront_accelerator_loaded(int version, const char *frontend,
231 struct netfront_accel_hooks *hooks);
234 * Called by an accelerator plugin module when it is about to unload.
236 * frontend: the string describing the accelerator. Must match the
237 * one passed to netfront_accelerator_loaded()
239 extern void netfront_accelerator_stop(const char *frontend);
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
246 extern int netfront_check_queue_ready(struct net_device *net_dev);
249 /* Internal-to-netfront Functions */
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
257 int netfront_check_accelerator_queue_ready(struct net_device *dev,
258 struct netfront_info *np);
260 int netfront_accelerator_call_remove(struct netfront_info *np,
261 struct xenbus_device *dev);
263 int netfront_accelerator_suspend(struct netfront_info *np,
264 struct xenbus_device *dev);
266 int netfront_accelerator_suspend_cancel(struct netfront_info *np,
267 struct xenbus_device *dev);
269 void netfront_accelerator_resume(struct netfront_info *np,
270 struct xenbus_device *dev);
272 void netfront_accelerator_call_stop_napi_irq(struct netfront_info *np,
273 struct net_device *dev);
275 int netfront_accelerator_call_get_stats(struct netfront_info *np,
276 struct net_device *dev);
278 void netfront_accelerator_add_watch(struct netfront_info *np);
281 void netif_init_accel(void);
283 void netif_exit_accel(void);
286 void init_accelerator_vif(struct netfront_info *np,
287 struct xenbus_device *dev);
288 #endif /* NETFRONT_H */