blob: 2e8a2505f4a45051c788c11a76e4e1a6ce60222d [file] [log] [blame]
Chouhan, Anurag57763182016-03-03 18:57:27 +05301/*
2 * Copyright (c) 2014-2016 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: i_qdf_mem.h
30 * Linux-specific definitions for QDF memory API's
31 */
32
33#ifndef __I_QDF_MEM_H
34#define __I_QDF_MEM_H
35
36#ifdef __KERNEL__
37#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)
38#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
39#include <linux/autoconf.h>
40#else
41#include <generated/autoconf.h>
42#endif
43#endif
44#include <linux/slab.h>
45#include <linux/hardirq.h>
46#include <linux/vmalloc.h>
47#include <linux/pci.h> /* pci_alloc_consistent */
48#if CONFIG_MCL
49#include <cds_queue.h>
50#else
51#include <sys/queue.h>
52#endif
53#else
54/*
55 * Provide dummy defs for kernel data types, functions, and enums
56 * used in this header file.
57 */
58#define GFP_KERNEL 0
59#define GFP_ATOMIC 0
60#define kzalloc(size, flags) NULL
61#define vmalloc(size) NULL
62#define kfree(buf)
63#define vfree(buf)
64#define pci_alloc_consistent(dev, size, paddr) NULL
Anurag Chouhanced23452016-06-15 11:33:01 +053065#define __qdf_mempool_t void*
Chouhan, Anurag57763182016-03-03 18:57:27 +053066#endif /* __KERNEL__ */
67#include <qdf_status.h>
68
69#ifdef __KERNEL__
70typedef struct mempool_elem {
71 STAILQ_ENTRY(mempool_elem) mempool_entry;
72} mempool_elem_t;
73
74/**
75 * typedef __qdf_mempool_ctxt_t - Memory pool context
76 * @pool_id: pool identifier
77 * @flags: flags
78 * @elem_size: size of each pool element in bytes
79 * @pool_mem: pool_addr address of the pool created
80 * @mem_size: Total size of the pool in bytes
81 * @free_list: free pool list
82 * @lock: spinlock object
83 * @max_elem: Maximum number of elements in tha pool
84 * @free_cnt: Number of free elements available
85 */
86typedef struct __qdf_mempool_ctxt {
87 int pool_id;
88 u_int32_t flags;
89 size_t elem_size;
90 void *pool_mem;
91 u_int32_t mem_size;
92 STAILQ_HEAD(, mempool_elem) free_list;
93 spinlock_t lock;
94 u_int32_t max_elem;
95 u_int32_t free_cnt;
96} __qdf_mempool_ctxt_t;
Anurag Chouhan85bc91a2016-03-21 19:18:37 +053097
98#endif /* __KERNEL__ */
Chouhan, Anurag57763182016-03-03 18:57:27 +053099
100/* typedef for dma_data_direction */
101typedef enum dma_data_direction __dma_data_direction;
102
103/**
104 * __qdf_str_cmp() - Compare two strings
105 * @str1: First string
106 * @str2: Second string
107 *
108 * Return: =0 equal
109 * >0 not equal, if str1 sorts lexicographically after str2
110 * <0 not equal, if str1 sorts lexicographically before str2
111 */
112static inline int32_t __qdf_str_cmp(const char *str1, const char *str2)
113{
114 return strcmp(str1, str2);
115}
116
117/**
118 * __qdf_str_lcopy() - Copy from one string to another
119 * @dest: destination string
120 * @src: source string
121 * @bytes: limit of num bytes to copy
122 *
123 * @return: 0 returns the initial value of dest
124 */
125static inline uint32_t __qdf_str_lcopy(char *dest, const char *src,
126 uint32_t bytes)
127{
128 return strlcpy(dest, src, bytes);
129}
130
131/**
132 * __qdf_mem_map_nbytes_single - Map memory for DMA
133 * @osdev: pomter OS device context
134 * @buf: pointer to memory to be dma mapped
135 * @dir: DMA map direction
136 * @nbytes: number of bytes to be mapped.
137 * @phy_addr: ponter to recive physical address.
138 *
139 * Return: success/failure
140 */
141static inline uint32_t __qdf_mem_map_nbytes_single(qdf_device_t osdev,
142 void *buf, qdf_dma_dir_t dir,
143 int nbytes,
144 uint32_t *phy_addr)
145{
146 /* assume that the OS only provides a single fragment */
147 *phy_addr = dma_map_single(osdev->dev, buf, nbytes, dir);
148 return dma_mapping_error(osdev->dev, *phy_addr) ?
149 QDF_STATUS_E_FAILURE : QDF_STATUS_SUCCESS;
150}
151
152/**
153 * __qdf_mem_unmap_nbytes_single() - un_map memory for DMA
154 *
155 * @osdev: pomter OS device context
156 * @phy_addr: physical address of memory to be dma unmapped
157 * @dir: DMA unmap direction
158 * @nbytes: number of bytes to be unmapped.
159 *
160 * @return - none
161 */
162static inline void __qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
163 uint32_t phy_addr,
164 qdf_dma_dir_t dir, int nbytes)
165{
166 dma_unmap_single(osdev->dev, phy_addr, nbytes, dir);
167}
168#ifdef __KERNEL__
169
170typedef __qdf_mempool_ctxt_t *__qdf_mempool_t;
171
172int __qdf_mempool_init(qdf_device_t osdev, __qdf_mempool_t *pool, int pool_cnt,
173 size_t pool_entry_size, u_int32_t flags);
174void __qdf_mempool_destroy(qdf_device_t osdev, __qdf_mempool_t pool);
175void *__qdf_mempool_alloc(qdf_device_t osdev, __qdf_mempool_t pool);
176void __qdf_mempool_free(qdf_device_t osdev, __qdf_mempool_t pool, void *buf);
177
178#define __qdf_mempool_elem_size(_pool) ((_pool)->elem_size);
179#endif
180
181/**
182 * __qdf_str_len() - returns the length of a string
183 * @str: input string
184 * Return:
185 * length of string
186 */
187static inline int32_t __qdf_str_len(const char *str)
188{
189 return strlen(str);
190}
191
192/**
193 * __qdf_mem_cmp() - memory compare
194 * @memory1: pointer to one location in memory to compare.
195 * @memory2: pointer to second location in memory to compare.
196 * @num_bytes: the number of bytes to compare.
197 *
198 * Function to compare two pieces of memory, similar to memcmp function
199 * in standard C.
200 * Return:
201 * int32_t - returns a bool value that tells if the memory
202 * locations are equal or not equal.
203 * 0 -- equal
204 * < 0 -- *memory1 is less than *memory2
205 * > 0 -- *memory1 is bigger than *memory2
206 */
207static inline int32_t __qdf_mem_cmp(const void *memory1, const void *memory2,
208 uint32_t num_bytes)
209{
210 return (int32_t) memcmp(memory1, memory2, num_bytes);
211}
212
213#endif /* __I_QDF_MEM_H */