1 //------------------------------------------------------------------------------
2 // Copyright (c) 2004-2010 Atheros Communications Inc.
3 // All rights reserved.
4 //
5 //
6 //
7 // Permission to use, copy, modify, and/or distribute this software for any
8 // purpose with or without fee is hereby granted, provided that the above
9 // copyright notice and this permission notice appear in all copies.
10 //
11 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 //
19 //
20 //
21 // Author(s): ="Atheros"
22 //------------------------------------------------------------------------------
23 #include <a_config.h>
24 #include "athdefs.h"
25 #include "a_types.h"
26 #include "a_osapi.h"
27 #include "htc_packet.h"
28 
29 #define AR6000_DATA_OFFSET    64
30 
a_netbuf_enqueue(A_NETBUF_QUEUE_T * q,void * pkt)31 void a_netbuf_enqueue(A_NETBUF_QUEUE_T *q, void *pkt)
32 {
33     skb_queue_tail((struct sk_buff_head *) q, (struct sk_buff *) pkt);
34 }
35 
a_netbuf_prequeue(A_NETBUF_QUEUE_T * q,void * pkt)36 void a_netbuf_prequeue(A_NETBUF_QUEUE_T *q, void *pkt)
37 {
38     skb_queue_head((struct sk_buff_head *) q, (struct sk_buff *) pkt);
39 }
40 
a_netbuf_dequeue(A_NETBUF_QUEUE_T * q)41 void *a_netbuf_dequeue(A_NETBUF_QUEUE_T *q)
42 {
43     return((void *) skb_dequeue((struct sk_buff_head *) q));
44 }
45 
a_netbuf_queue_size(A_NETBUF_QUEUE_T * q)46 int a_netbuf_queue_size(A_NETBUF_QUEUE_T *q)
47 {
48     return(skb_queue_len((struct sk_buff_head *) q));
49 }
50 
a_netbuf_queue_empty(A_NETBUF_QUEUE_T * q)51 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q)
52 {
53     return(skb_queue_empty((struct sk_buff_head *) q));
54 }
55 
a_netbuf_queue_init(A_NETBUF_QUEUE_T * q)56 void a_netbuf_queue_init(A_NETBUF_QUEUE_T *q)
57 {
58     skb_queue_head_init((struct sk_buff_head *) q);
59 }
60 
61 void *
a_netbuf_alloc(int size)62 a_netbuf_alloc(int size)
63 {
64     struct sk_buff *skb;
65     size += 2 * (A_GET_CACHE_LINE_BYTES()); /* add some cacheline space at front and back of buffer */
66     skb = dev_alloc_skb(AR6000_DATA_OFFSET + sizeof(struct htc_packet) + size);
67     skb_reserve(skb, AR6000_DATA_OFFSET + sizeof(struct htc_packet) + A_GET_CACHE_LINE_BYTES());
68     return ((void *)skb);
69 }
70 
71 /*
72  * Allocate an SKB w.o. any encapsulation requirement.
73  */
74 void *
a_netbuf_alloc_raw(int size)75 a_netbuf_alloc_raw(int size)
76 {
77     struct sk_buff *skb;
78 
79     skb = dev_alloc_skb(size);
80 
81     return ((void *)skb);
82 }
83 
84 void
a_netbuf_free(void * bufPtr)85 a_netbuf_free(void *bufPtr)
86 {
87     struct sk_buff *skb = (struct sk_buff *)bufPtr;
88 
89     dev_kfree_skb(skb);
90 }
91 
a_netbuf_to_len(void * bufPtr)92 u32 a_netbuf_to_len(void *bufPtr)
93 {
94     return (((struct sk_buff *)bufPtr)->len);
95 }
96 
97 void *
a_netbuf_to_data(void * bufPtr)98 a_netbuf_to_data(void *bufPtr)
99 {
100     return (((struct sk_buff *)bufPtr)->data);
101 }
102 
103 /*
104  * Add len # of bytes to the beginning of the network buffer
105  * pointed to by bufPtr
106  */
107 int
a_netbuf_push(void * bufPtr,s32 len)108 a_netbuf_push(void *bufPtr, s32 len)
109 {
110     skb_push((struct sk_buff *)bufPtr, len);
111 
112     return 0;
113 }
114 
115 /*
116  * Add len # of bytes to the beginning of the network buffer
117  * pointed to by bufPtr and also fill with data
118  */
119 int
a_netbuf_push_data(void * bufPtr,char * srcPtr,s32 len)120 a_netbuf_push_data(void *bufPtr, char *srcPtr, s32 len)
121 {
122     skb_push((struct sk_buff *) bufPtr, len);
123     memcpy(((struct sk_buff *)bufPtr)->data, srcPtr, len);
124 
125     return 0;
126 }
127 
128 /*
129  * Add len # of bytes to the end of the network buffer
130  * pointed to by bufPtr
131  */
132 int
a_netbuf_put(void * bufPtr,s32 len)133 a_netbuf_put(void *bufPtr, s32 len)
134 {
135     skb_put((struct sk_buff *)bufPtr, len);
136 
137     return 0;
138 }
139 
140 /*
141  * Add len # of bytes to the end of the network buffer
142  * pointed to by bufPtr and also fill with data
143  */
144 int
a_netbuf_put_data(void * bufPtr,char * srcPtr,s32 len)145 a_netbuf_put_data(void *bufPtr, char *srcPtr, s32 len)
146 {
147     char *start = (char*)(((struct sk_buff *)bufPtr)->data +
148         ((struct sk_buff *)bufPtr)->len);
149     skb_put((struct sk_buff *)bufPtr, len);
150     memcpy(start, srcPtr, len);
151 
152     return 0;
153 }
154 
155 
156 /*
157  * Trim the network buffer pointed to by bufPtr to len # of bytes
158  */
159 int
a_netbuf_setlen(void * bufPtr,s32 len)160 a_netbuf_setlen(void *bufPtr, s32 len)
161 {
162     skb_trim((struct sk_buff *)bufPtr, len);
163 
164     return 0;
165 }
166 
167 /*
168  * Chop of len # of bytes from the end of the buffer.
169  */
170 int
a_netbuf_trim(void * bufPtr,s32 len)171 a_netbuf_trim(void *bufPtr, s32 len)
172 {
173     skb_trim((struct sk_buff *)bufPtr, ((struct sk_buff *)bufPtr)->len - len);
174 
175     return 0;
176 }
177 
178 /*
179  * Chop of len # of bytes from the end of the buffer and return the data.
180  */
181 int
a_netbuf_trim_data(void * bufPtr,char * dstPtr,s32 len)182 a_netbuf_trim_data(void *bufPtr, char *dstPtr, s32 len)
183 {
184     char *start = (char*)(((struct sk_buff *)bufPtr)->data +
185         (((struct sk_buff *)bufPtr)->len - len));
186 
187     memcpy(dstPtr, start, len);
188     skb_trim((struct sk_buff *)bufPtr, ((struct sk_buff *)bufPtr)->len - len);
189 
190     return 0;
191 }
192 
193 
194 /*
195  * Returns the number of bytes available to a a_netbuf_push()
196  */
a_netbuf_headroom(void * bufPtr)197 s32 a_netbuf_headroom(void *bufPtr)
198 {
199     return (skb_headroom((struct sk_buff *)bufPtr));
200 }
201 
202 /*
203  * Removes specified number of bytes from the beginning of the buffer
204  */
205 int
a_netbuf_pull(void * bufPtr,s32 len)206 a_netbuf_pull(void *bufPtr, s32 len)
207 {
208     skb_pull((struct sk_buff *)bufPtr, len);
209 
210     return 0;
211 }
212 
213 /*
214  * Removes specified number of bytes from the beginning of the buffer
215  * and return the data
216  */
217 int
a_netbuf_pull_data(void * bufPtr,char * dstPtr,s32 len)218 a_netbuf_pull_data(void *bufPtr, char *dstPtr, s32 len)
219 {
220     memcpy(dstPtr, ((struct sk_buff *)bufPtr)->data, len);
221     skb_pull((struct sk_buff *)bufPtr, len);
222 
223     return 0;
224 }
225 
226 #ifdef EXPORT_HCI_BRIDGE_INTERFACE
227 EXPORT_SYMBOL(a_netbuf_to_data);
228 EXPORT_SYMBOL(a_netbuf_put);
229 EXPORT_SYMBOL(a_netbuf_pull);
230 EXPORT_SYMBOL(a_netbuf_alloc);
231 EXPORT_SYMBOL(a_netbuf_free);
232 #endif
233