blob: c00ac564833f819d8d1f7568a377afc35f36c221 [file] [log] [blame]
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08001/*
2 * Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
3 *
4 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5 *
6 *
7 * Permission to use, copy, modify, and/or distribute this software for
8 * any purpose with or without fee is hereby granted, provided that the
9 * above copyright notice and this permission notice appear in all
10 * copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 * PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * This file was originally distributed by Qualcomm Atheros, Inc.
24 * under proprietary terms before Copyright ownership was assigned
25 * to the Linux Foundation.
26 */
27
28/**
29 * DOC: cdf_util.h
30 *
31 * This file defines utility functions.
32 */
33
34#ifndef _CDF_UTIL_H
35#define _CDF_UTIL_H
36
37#include <i_cdf_util.h>
38
39/**
40 * cdf_unlikely - Compiler-dependent macro denoting code likely to execute
41 * @_expr: expression to be checked
42 */
43#define cdf_unlikely(_expr) __cdf_unlikely(_expr)
44
45/**
46 * cdf_likely - Compiler-dependent macro denoting code unlikely to execute
47 * @_expr: expression to be checked
48 */
49#define cdf_likely(_expr) __cdf_likely(_expr)
50
51CDF_INLINE_FN int cdf_status_to_os_return(CDF_STATUS status)
52{
53 return __cdf_status_to_os_return(status);
54}
55
56/**
57 * cdf_assert - assert "expr" evaluates to false
58 * @expr: assert expression
59 */
60#ifdef CDF_OS_DEBUG
61#define cdf_assert(expr) __cdf_assert(expr)
62#else
63#define cdf_assert(expr)
64#endif /* CDF_OS_DEBUG */
65
66/**
67 * @cdf_assert_always- alway assert "expr" evaluates to false
68 * @expr: assert expression
69 */
70#define cdf_assert_always(expr) __cdf_assert(expr)
71
72/**
73 * cdf_os_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
74 * little-endian byte order
75 * @x: value to be converted
76 */
77#define cdf_os_cpu_to_le64(x) __cdf_os_cpu_to_le64(x)
78
79/**
80 * cdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
81 * to CPU byte order
82 * @x: value to be converted
83 */
84#define cdf_le16_to_cpu(x) __cdf_le16_to_cpu(x)
85
86/**
87 * cdf_le32_to_cpu - Convert a 32-bit value from little-endian byte order to
88 * CPU byte order
89 * @x: value to be converted
90 */
91#define cdf_le32_to_cpu(x) __cdf_le32_to_cpu(x)
92
93/**
94 * cdf_in_interrupt - returns true if in interrupt context
95 */
96#define cdf_in_interrupt in_interrupt
97
98/**
99 * cdf_container_of - cast a member of a structure out to the containing
100 * structure
101 * @ptr: the pointer to the member.
102 * @type: the type of the container struct this is embedded in.
103 * @member: the name of the member within the struct.
104 *
105 */
106#define cdf_container_of(ptr, type, member) \
107 __cdf_container_of(ptr, type, member)
108
109/**
110 * cdf_is_macaddr_equal() - compare two CDF MacAddress
111 * @pMacAddr1: Pointer to one cdf MacAddress to compare
112 * @pMacAddr2: Pointer to the other cdf MacAddress to compare
113 *
114 * This function returns a bool that tells if a two CDF MacAddress'
115 * are equivalent.
116 *
117 * Return: true if the MacAddress's are equal
118 * not true if the MacAddress's are not equal
119 */
120CDF_INLINE_FN bool cdf_is_macaddr_equal(struct cdf_mac_addr *pMacAddr1,
121 struct cdf_mac_addr *pMacAddr2)
122{
123 return 0 == memcmp(pMacAddr1, pMacAddr2, CDF_MAC_ADDR_SIZE);
124}
125
126/**
127 * cdf_is_macaddr_zero() - check for a MacAddress of all zeros.
128 * @pMacAddr - pointer to the struct cdf_mac_addr to check.
129 *
130 * This function returns a bool that tells if a MacAddress is made up of
131 * all zeros.
132 *
133 *
134 * Return: true if the MacAddress is all Zeros
135 * flase if the MacAddress is not all Zeros.
136 *
137 */
138CDF_INLINE_FN bool cdf_is_macaddr_zero(struct cdf_mac_addr *pMacAddr)
139{
140 struct cdf_mac_addr zeroMacAddr = CDF_MAC_ADDR_ZERO_INITIALIZER;
141
142 return cdf_is_macaddr_equal(pMacAddr, &zeroMacAddr);
143}
144
145/**
146 * cdf_zero_macaddr() - zero out a MacAddress
147 * @pMacAddr: pointer to the struct cdf_mac_addr to zero.
148 *
149 * This function zeros out a CDF MacAddress type.
150 *
151 * Return: nothing
152 */
153CDF_INLINE_FN void cdf_zero_macaddr(struct cdf_mac_addr *pMacAddr)
154{
155 memset(pMacAddr, 0, CDF_MAC_ADDR_SIZE);
156}
157
158/**
159 * cdf_is_macaddr_group() - check for a MacAddress is a 'group' address
160 * @pMacAddr1: pointer to the cdf MacAddress to check
161 *
162 * This function returns a bool that tells if a the input CDF MacAddress
163 * is a "group" address. Group addresses have the 'group address bit' turned
164 * on in the MacAddress. Group addresses are made up of Broadcast and
165 * Multicast addresses.
166 *
167 * Return: true if the input MacAddress is a Group address
168 * false if the input MacAddress is not a Group address
169 */
170CDF_INLINE_FN bool cdf_is_macaddr_group(struct cdf_mac_addr *pMacAddr)
171{
172 return pMacAddr->bytes[0] & 0x01;
173}
174
175/**
176 * cdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
177 *
178 * This function returns a bool that tells if a the input CDF MacAddress
179 * is a "broadcast" address.
180 *
181 * @pMacAddr: Pointer to the cdf MacAddress to check
182 *
183 * Return: true if the input MacAddress is a broadcast address
184 * flase if the input MacAddress is not a broadcast address
185 */
186CDF_INLINE_FN bool cdf_is_macaddr_broadcast(struct cdf_mac_addr *pMacAddr)
187{
188 struct cdf_mac_addr broadcastMacAddr =
189 CDF_MAC_ADDR_BROADCAST_INITIALIZER;
190
191 return cdf_is_macaddr_equal(pMacAddr, &broadcastMacAddr);
192}
193
194/**
195 * cdf_copy_macaddr() - copy a CDF MacAddress
196 * @pDst - pointer to the cdf MacAddress to copy TO (the destination)
197 * @pSrc - pointer to the cdf MacAddress to copy FROM (the source)
198 *
199 * This function copies a CDF MacAddress into another CDF MacAddress.
200 *
201 *
202 * Return: nothing
203 */
204CDF_INLINE_FN void cdf_copy_macaddr(struct cdf_mac_addr *pDst,
205 struct cdf_mac_addr *pSrc)
206{
207 *pDst = *pSrc;
208}
209
210/**
211 * cdf_set_macaddr_broadcast() - set a CDF MacAddress to the 'broadcast'
212 * @pMacAddr: pointer to the cdf MacAddress to set to broadcast
213 *
214 * This function sets a CDF MacAddress to the 'broadcast' MacAddress. Broadcast
215 * MacAddress contains all 0xFF bytes.
216 *
217 * Return: nothing
218 */
219CDF_INLINE_FN void cdf_set_macaddr_broadcast(struct cdf_mac_addr *pMacAddr)
220{
221 memset(pMacAddr, 0xff, CDF_MAC_ADDR_SIZE);
222}
223
224#if defined(ANI_LITTLE_BYTE_ENDIAN)
225
226/**
227 * i_cdf_htonl() - convert from host byte order to network byte order
228 * @ul: input to be converted
229 *
230 * Return: converted network byte order
231 */
232CDF_INLINE_FN unsigned long i_cdf_htonl(unsigned long ul)
233{
234 return ((ul & 0x000000ff) << 24) |
235 ((ul & 0x0000ff00) << 8) |
236 ((ul & 0x00ff0000) >> 8) | ((ul & 0xff000000) >> 24);
237}
238
239/**
240 * i_cdf_ntohl() - convert network byte order to host byte order
241 * @ul: input to be converted
242 *
243 * Return: converted host byte order
244 */
245CDF_INLINE_FN unsigned long i_cdf_ntohl(unsigned long ul)
246{
247 return i_cdf_htonl(ul);
248}
249
250#endif
251
252/**
253 * cdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
254 * endianness.
255 * @ptr: Starting address of a byte array
256 * @value: The value to assign to the byte array
257 *
258 * Caller must validate the byte array has enough space to hold the vlaue
259 *
260 * Return: The address to the byte after the assignment. This may or may not
261 * be valid. Caller to verify.
262 */
263CDF_INLINE_FN uint8_t *cdf_set_u16(uint8_t *ptr, uint16_t value)
264{
265#if defined(ANI_BIG_BYTE_ENDIAN)
266 *(ptr) = (uint8_t) (value >> 8);
267 *(ptr + 1) = (uint8_t) (value);
268#else
269 *(ptr + 1) = (uint8_t) (value >> 8);
270 *(ptr) = (uint8_t) (value);
271#endif
272
273 return ptr + 2;
274}
275
276/**
277 * cdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
278 * CPU's endianness.
279 * @ptr: Starting address of a byte array
280 * @pValue: Pointer to a caller allocated buffer for 16 bit value. Value is to
281 * assign to this location.
282 *
283 * Caller must validate the byte array has enough space to hold the vlaue
284 *
285 * Return: The address to the byte after the assignment. This may or may not
286 * be valid. Caller to verify.
287 */
288CDF_INLINE_FN uint8_t *cdf_get_u16(uint8_t *ptr, uint16_t *pValue)
289{
290#if defined(ANI_BIG_BYTE_ENDIAN)
291 *pValue = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
292#else
293 *pValue = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
294#endif
295
296 return ptr + 2;
297}
298
299/**
300 * cdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
301 * CPU's endianness.
302 * @ptr: Starting address of a byte array
303 * @pValue: Pointer to a caller allocated buffer for 32 bit value. Value is to
304 * assign to this location.
305 *
306 * Caller must validate the byte array has enough space to hold the vlaue
307 *
308 * Return: The address to the byte after the assignment. This may or may not
309 * be valid. Caller to verify.
310 */
311CDF_INLINE_FN uint8_t *cdf_get_u32(uint8_t *ptr, uint32_t *pValue)
312{
313#if defined(ANI_BIG_BYTE_ENDIAN)
314 *pValue = ((uint32_t) (*(ptr) << 24) |
315 (uint32_t) (*(ptr + 1) << 16) |
316 (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
317#else
318 *pValue = ((uint32_t) (*(ptr + 3) << 24) |
319 (uint32_t) (*(ptr + 2) << 16) |
320 (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
321#endif
322 return ptr + 4;
323}
324
325#endif /*_CDF_UTIL_H*/