- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / sfc_netfront / accel_bufs.h
1 /****************************************************************************
2  * Solarflare driver for Xen network acceleration
3  *
4  * Copyright 2006-2008: Solarflare Communications Inc,
5  *                      9501 Jeronimo Road, Suite 250,
6  *                      Irvine, CA 92618, USA
7  *
8  * Maintained by Solarflare Communications <linux-xen-drivers@solarflare.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published
12  * by the Free Software Foundation, incorporated herein by reference.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  ****************************************************************************
23  */
24
25 #ifndef NETFRONT_ACCEL_BUFS_H
26 #define NETFRONT_ACCEL_BUFS_H
27
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <xen/xenbus.h>
31
32 #include "accel_msg_iface.h"
33
34
35 /*! Buffer descriptor structure */
36 typedef struct netfront_accel_pkt_desc {
37         int buf_id;
38         u32 pkt_buff_addr;
39         void *pkt_kva;
40         /* This is the socket buffer currently married to this buffer */
41         struct sk_buff *skb;
42         int next_free;
43 } netfront_accel_pkt_desc;
44
45
46 #define NETFRONT_ACCEL_DEFAULT_BUF_PAGES (384)
47 #define NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK_SHIFT (4)
48 #define NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK              \
49         (1 << (NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK_SHIFT))
50 #define NETFRONT_ACCEL_BUFS_PER_PAGE_SHIFT (1)
51 #define NETFRONT_ACCEL_BUFS_PER_PAGE                    \
52         (1 << (NETFRONT_ACCEL_BUFS_PER_PAGE_SHIFT))
53 #define NETFRONT_ACCEL_BUFS_PER_BLOCK_SHIFT             \
54         (NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK_SHIFT +     \
55          NETFRONT_ACCEL_BUFS_PER_PAGE_SHIFT)
56 #define NETFRONT_ACCEL_BUFS_PER_BLOCK                   \
57         (1 << NETFRONT_ACCEL_BUFS_PER_BLOCK_SHIFT)
58 #define NETFRONT_ACCEL_BUF_NUM_BLOCKS(max_pages)                        \
59         (((max_pages)+NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK-1) /           \
60          NETFRONT_ACCEL_BUF_PAGES_PER_BLOCK)
61
62 /*! Buffer management structure. */
63 struct netfront_accel_bufinfo {
64         /* number added to this manager */
65         unsigned npages;
66         /* number currently used from this manager */
67         unsigned nused;
68
69         int first_free;
70
71         int internally_locked;
72         spinlock_t *lock;
73
74         /*
75          * array of pointers (length NETFRONT_ACCEL_BUF_NUM_BLOCKS) to
76          * pkt descs
77          */
78         struct netfront_accel_pkt_desc **desc_blocks; 
79 };
80
81
82 struct netfront_accel_bufpages {
83         /* length of lists of pages/grants */
84         int max_pages;
85         /* list of pages allocated for network buffers */
86         void **page_list;
87         /* list of grants for the above pages */
88         grant_ref_t *grant_list;
89         
90         /* number of page requests that have been made */
91         unsigned page_reqs;
92 };
93
94
95 /*! Allocate memory for the buffer manager, set up locks etc.
96  * Optionally takes a lock to use, if not supplied it makes its own.
97  *
98  * \return pointer to netfront_accel_bufinfo structure that represents the
99  * buffer manager
100  */
101 extern struct netfront_accel_bufinfo *
102 netfront_accel_init_bufs(spinlock_t *lock);
103
104 /*! Allocate memory for the buffers
105  */
106 extern int
107 netfront_accel_alloc_buffer_mem(struct netfront_accel_bufpages *bufpages,
108                                 struct netfront_accel_bufinfo *rx_res,
109                                 struct netfront_accel_bufinfo *tx_res,
110                                 int pages);
111 extern void
112 netfront_accel_free_buffer_mem(struct netfront_accel_bufpages *bufpages,
113                                struct netfront_accel_bufinfo *rx_res,
114                                struct netfront_accel_bufinfo *tx_res);
115
116 /*! Release memory for the buffer manager, buffers, etc.
117  *
118  * \param manager pointer to netfront_accel_bufinfo structure that
119  * represents the buffer manager
120  */
121 extern void netfront_accel_fini_bufs(struct netfront_accel_bufinfo *manager);
122
123 /*! Release a buffer.
124  *
125  * \param manager  The buffer manager which owns the buffer.
126  * \param id   The buffer identifier.
127  */
128 extern int netfront_accel_buf_put(struct netfront_accel_bufinfo *manager, 
129                                   u16 id);
130
131 /*! Get the packet descriptor associated with a buffer id.
132  *
133  * \param manager  The buffer manager which owns the buffer.
134  * \param id       The buffer identifier.
135  *
136  * The returned value is the packet descriptor for this buffer.
137  */
138 extern netfront_accel_pkt_desc *
139 netfront_accel_buf_find(struct netfront_accel_bufinfo *manager, u16 id);
140
141
142 /*! Fill out a message request for some buffers to be mapped by the
143  * back end driver
144  * 
145  * \param manager The buffer manager 
146  * \param msg Pointer to an ef_msg to complete.
147  * \return 0 on success
148  */
149 extern int 
150 netfront_accel_buf_map_request(struct xenbus_device *dev,
151                                struct netfront_accel_bufpages *bufpages,
152                                struct net_accel_msg *msg, 
153                                int pages, int offset);
154
155 /*! Process a response to a buffer request. 
156  * 
157  * Deal with a received message from the back end in response to our
158  * request for buffers
159  * 
160  * \param manager The buffer manager
161  * \param msg The received message from the back end describing new
162  * buffers
163  * \return 0 on success
164  */
165 extern int 
166 netfront_accel_add_bufs(struct netfront_accel_bufpages *bufpages,
167                         struct netfront_accel_bufinfo *manager,
168                         struct net_accel_msg *msg);
169
170
171 /*! Allocate a buffer from the buffer manager 
172  *
173  * \param manager The buffer manager data structure
174  * \param id On exit, the id of the buffer allocated
175  * \return Pointer to buffer descriptor.
176  */
177 struct netfront_accel_pkt_desc *
178 netfront_accel_buf_get(struct netfront_accel_bufinfo *manager);
179
180 #endif /* NETFRONT_ACCEL_BUFS_H */
181