xref: /wlan-dirver/qcacld-3.0/core/mac/src/include/utils_api.h (revision bf14ba81a9dde77532035d124922099fe95cd35d)
1 /*
2  * Copyright (c) 2011-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef __UTILSAPI_H
20 #define __UTILSAPI_H
21 
22 #include <stdarg.h>
23 #include <sir_common.h>
24 #include "ani_global.h"
25 #include "sys_wrapper.h"
26 #include "wlan_vdev_mlme_main.h"
27 #include "wlan_vdev_mlme_api.h"
28 
29 /**
30  * sir_swap_u16()
31  *
32  * FUNCTION:
33  * This function is called to swap two U8s of an uint16_t value
34  *
35  * LOGIC:
36  *
37  * ASSUMPTIONS:
38  * None.
39  *
40  * NOTE:
41  *
42  * @param  val    uint16_t value to be uint8_t swapped
43  * @return        Swapped uint16_t value
44  */
45 
46 static inline uint16_t sir_swap_u16(uint16_t val)
47 {
48 	return ((val & 0x00FF) << 8) | ((val & 0xFF00) >> 8);
49 } /*** end sir_swap_u16() ***/
50 
51 /**
52  * sir_swap_u16if_needed()
53  *
54  * FUNCTION:
55  * This function is called to swap two U8s of an uint16_t value depending
56  * on endiannes of the target processor/compiler the software is
57  * running on
58  *
59  * LOGIC:
60  *
61  * ASSUMPTIONS:
62  * None.
63  *
64  * NOTE:
65  *
66  * @param  val    uint16_t value to be uint8_t swapped
67  * @return        Swapped uint16_t value
68  */
69 
70 static inline uint16_t sir_swap_u16if_needed(uint16_t val)
71 {
72 #ifndef ANI_LITTLE_BYTE_ENDIAN
73 	return sir_swap_u16(val);
74 #else
75 	return val;
76 #endif
77 } /*** end sir_swap_u16if_needed() ***/
78 
79 /**
80  * sir_swap_u32()
81  *
82  * FUNCTION:
83  * This function is called to swap four U8s of an uint32_t value
84  *
85  * LOGIC:
86  *
87  * ASSUMPTIONS:
88  * None.
89  *
90  * NOTE:
91  *
92  * @param  val    uint32_t value to be uint8_t swapped
93  * @return        Swapped uint32_t value
94  */
95 
96 static inline uint32_t sir_swap_u32(uint32_t val)
97 {
98 	return (val << 24) |
99 		(val >> 24) |
100 		((val & 0x0000FF00) << 8) | ((val & 0x00FF0000) >> 8);
101 } /*** end sir_swap_u32() ***/
102 
103 /**
104  * sir_swap_u32if_needed()
105  *
106  * FUNCTION:
107  * This function is called to swap U8s of an uint32_t value depending
108  * on endiannes of the target processor/compiler the software is
109  * running on
110  *
111  * LOGIC:
112  *
113  * ASSUMPTIONS:
114  * None.
115  *
116  * NOTE:
117  *
118  * @param  val    uint32_t value to be uint8_t swapped
119  * @return        Swapped uint32_t value
120  */
121 
122 static inline uint32_t sir_swap_u32if_needed(uint32_t val)
123 {
124 #ifndef ANI_LITTLE_BYTE_ENDIAN
125 	return sir_swap_u32(val);
126 #else
127 	return val;
128 #endif
129 } /*** end sir_swap_u32if_needed() ***/
130 
131 /**
132  * sir_swap_u32_buf
133  *
134  * FUNCTION:
135  * It swaps N dwords into the same buffer
136  *
137  * LOGIC:
138  *
139  * ASSUMPTIONS:
140  * None.
141  *
142  * NOTE:
143  *
144  * @param  ptr address of uint32_t array
145  * @return void
146  *
147  */
148 
149 /**
150  * sir_read_u32_n
151  *
152  * FUNCTION:
153  * It reads a 32 bit number from the byte array in network byte order
154  * i.e. the least significant byte first
155  *
156  * LOGIC:
157  *
158  * ASSUMPTIONS:
159  * None.
160  *
161  * NOTE:
162  *
163  * @param  ptr address of  byte array
164  * @return 32 bit value
165  */
166 
167 static inline uint32_t sir_read_u32_n(uint8_t *ptr)
168 {
169 	return (*(ptr) << 24) |
170 		(*(ptr + 1) << 16) | (*(ptr + 2) << 8) | (*(ptr + 3));
171 }
172 
173 /**
174  * sir_read_u16
175  *
176  * FUNCTION:
177  * It reads a 16 bit number from the byte array in NON-network byte order
178  * i.e. the least significant byte first
179  *
180  * LOGIC:
181  *
182  * ASSUMPTIONS:
183  * None.
184  *
185  * NOTE:
186  *
187  * @param  ptr address of  byte array
188  * @return 16 bit value
189  */
190 
191 static inline uint16_t sir_read_u16(uint8_t *ptr)
192 {
193 	return (*ptr) | (*(ptr + 1) << 8);
194 }
195 
196 /**
197  * sir_read_u32
198  *
199  * FUNCTION:
200  * It reads a 32 bit number from the byte array in NON-network byte order
201  * i.e. the least significant byte first
202  *
203  * LOGIC:
204  *
205  * ASSUMPTIONS:
206  * None.
207  *
208  * NOTE:
209  *
210  * @param  ptr address of  byte array
211  * @return 32 bit value
212  */
213 
214 static inline uint32_t sir_read_u32(uint8_t *ptr)
215 {
216 	return (*(ptr)) |
217 		(*(ptr + 1) << 8) | (*(ptr + 2) << 16) | (*(ptr + 3) << 24);
218 }
219 
220 /* / Copy a MAC address from 'from' to 'to' */
221 static inline void sir_copy_mac_addr(uint8_t to[], uint8_t from[])
222 {
223 #if defined(_X86_)
224 	uint32_t align = (0x3 & ((uint32_t) to | (uint32_t) from));
225 
226 	if (align == 0) {
227 		*((uint16_t *) &(to[4])) = *((uint16_t *) &(from[4]));
228 		*((uint32_t *) to) = *((uint32_t *) from);
229 	} else if (align == 2) {
230 		*((uint16_t *) &to[4]) = *((uint16_t *) &from[4]);
231 		*((uint16_t *) &to[2]) = *((uint16_t *) &from[2]);
232 		*((uint16_t *) &to[0]) = *((uint16_t *) &from[0]);
233 	} else {
234 		to[5] = from[5];
235 		to[4] = from[4];
236 		to[3] = from[3];
237 		to[2] = from[2];
238 		to[1] = from[1];
239 		to[0] = from[0];
240 	}
241 #else
242 	to[0] = from[0];
243 	to[1] = from[1];
244 	to[2] = from[2];
245 	to[3] = from[3];
246 	to[4] = from[4];
247 	to[5] = from[5];
248 #endif
249 }
250 
251 static inline uint8_t sir_compare_mac_addr(uint8_t addr1[], uint8_t addr2[])
252 {
253 #if defined(_X86_)
254 	uint32_t align = (0x3 & ((uint32_t) addr1 | (uint32_t) addr2));
255 
256 	if (align == 0) {
257 		return (*((uint16_t *) &(addr1[4])) ==
258 			 *((uint16_t *) &(addr2[4])))
259 			&& (*((uint32_t *) addr1) == *((uint32_t *) addr2));
260 	} else if (align == 2) {
261 		return (*((uint16_t *) &addr1[4]) ==
262 			 *((uint16_t *) &addr2[4]))
263 			&& (*((uint16_t *) &addr1[2]) ==
264 			    *((uint16_t *) &addr2[2]))
265 			&& (*((uint16_t *) &addr1[0]) ==
266 			    *((uint16_t *) &addr2[0]));
267 	} else {
268 		return (addr1[5] == addr2[5]) &&
269 			(addr1[4] == addr2[4]) &&
270 			(addr1[3] == addr2[3]) &&
271 			(addr1[2] == addr2[2]) &&
272 			(addr1[1] == addr2[1]) && (addr1[0] == addr2[0]);
273 	}
274 #else
275 	return (addr1[0] == addr2[0]) &&
276 		(addr1[1] == addr2[1]) &&
277 		(addr1[2] == addr2[2]) &&
278 		(addr1[3] == addr2[3]) &&
279 		(addr1[4] == addr2[4]) && (addr1[5] == addr2[5]);
280 #endif
281 }
282 
283 /*
284  * converts uint16_t CW value to 4 bit value to be inserted in IE
285  */
286 static inline uint8_t convert_cw(uint16_t cw)
287 {
288 	uint8_t val = 0;
289 
290 	while (cw > 0) {
291 		val++;
292 		cw >>= 1;
293 	}
294 	if (val > 15)
295 		return 0xF;
296 	return val;
297 }
298 
299 /* The user priority to AC mapping is such:
300  *   UP(1, 2) ---> AC_BK(1)
301  *   UP(0, 3) ---> AC_BE(0)
302  *   UP(4, 5) ---> AC_VI(2)
303  *   UP(6, 7) ---> AC_VO(3)
304  */
305 #define WLAN_UP_TO_AC_MAP            0x33220110
306 #define upToAc(up)                ((WLAN_UP_TO_AC_MAP >> ((up) << 2)) & 0x03)
307 
308 #endif /* __UTILSAPI_H */
309