1 /*
2  * Copyright (c) 2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: qdf_nbuf_frag.h
22  * This file defines the nbuf frag abstraction.
23  */
24 
25 #ifndef _QDF_NBUF_FRAG_H
26 #define _QDF_NBUF_FRAG_H
27 
28 #include <qdf_util.h>
29 #include <i_qdf_trace.h>
30 #include <i_qdf_nbuf_frag.h>
31 
32 /*
33  * typedef qdf_frag_cache_t - Platform independent
34  * frag cache abstraction
35  */
36 typedef __qdf_frag_cache_t qdf_frag_cache_t;
37 
38 /*
39  * typedef qdf_frag_t - Platform independent frag address abstraction
40  */
41 typedef __qdf_frag_t qdf_frag_t;
42 
43 /*
44  * Maximum number of frags an SKB can hold
45  */
46 #define QDF_NBUF_MAX_FRAGS __QDF_NBUF_MAX_FRAGS
47 
48 #ifdef NBUF_FRAG_MEMORY_DEBUG
49 /**
50  * qdf_frag_debug_init() - Initialize frag debug tracker
51  *
52  * Return: none
53  */
54 void qdf_frag_debug_init(void);
55 
56 /**
57  * qdf_frag_debug_exit() - Exit network frag debug functionality
58  *
59  * Exit network frag tracking debug functionality and log frag memory leaks
60  *
61  * Return: none
62  */
63 void qdf_frag_debug_exit(void);
64 
65 /**
66  * qdf_frag_debug_add_node() - Add frag node in the debug hash table
67  * @fragp: Pointer to frag
68  * @func_name: Caller function name
69  * @line_num: Caller function line no.
70  *
71  * Return: none
72  */
73 void qdf_frag_debug_add_node(qdf_frag_t fragp, const char *func_name,
74 			     uint32_t line_num);
75 
76 /**
77  * qdf_frag_debug_refcount_inc() - Increment refcount for frag node
78  * @fragp: Pointer to frag
79  * @func_name: Caller function name
80  * @line_num: Caller function line no.
81  *
82  * Return: none
83  */
84 void qdf_frag_debug_refcount_inc(qdf_frag_t fragp, const char *func_name,
85 				 uint32_t line_num);
86 
87 /**
88  * qdf_frag_debug_refcount_dec() - Decrement refcount for frag node
89  * @fragp: Pointer to frag
90  * @func_name: Caller function name
91  * @line_num: Caller function line no.
92  *
93  * Return: none
94  */
95 void qdf_frag_debug_refcount_dec(qdf_frag_t fragp, const char *func_name,
96 				 uint32_t line_num);
97 
98 /**
99  * qdf_frag_debug_delete_node() - Remove frag node from debug hash table
100  * @fragp: Pointer to frag
101  * @func_name: Caller function name
102  * @line_num: Caller function line no.
103  *
104  * Return: none
105  */
106 void qdf_frag_debug_delete_node(qdf_frag_t fragp, const char *func_name,
107 				uint32_t line_num);
108 
109 /**
110  * qdf_frag_debug_update_addr() - Update frag address in debug tracker
111  * @p_fragp: Previous frag address
112  * @n_fragp: New frag address
113  * @func_name: Caller function name
114  * @line_num: Caller function line no.
115  *
116  * Return: none
117  */
118 void qdf_frag_debug_update_addr(qdf_frag_t p_fragp, qdf_frag_t n_fragp,
119 				const char *func_name, uint32_t line_num);
120 
121 #define qdf_frag_alloc(p, s) \
122 	qdf_frag_alloc_debug(p, s, __func__, __LINE__)
123 
124 /**
125  * qdf_frag_alloc_debug() - Allocate frag memory
126  * @pf_cache: page frag cache
127  * @fragsz: Size of frag memory to be allocated
128  * @func_name: Caller function name
129  * @line_num: Caller function line no.
130  *
131  * Return: Allocated frag address
132  */
133 qdf_frag_t qdf_frag_alloc_debug(qdf_frag_cache_t *pf_cache,
134 				unsigned int fragsz,
135 				const char *func_name,
136 				uint32_t line_num);
137 
138 #define qdf_frag_free(p) \
139 	qdf_frag_free_debug(p, __func__, __LINE__)
140 
141 /**
142  * qdf_frag_free_debug() - Free allocated frag memory
143  * @vaddr: Frag address to be freed
144  * @func_name: Caller function name
145  * @line_num: Caller function line no.
146  *
147  * Return: none
148  */
149 void qdf_frag_free_debug(qdf_frag_t vaddr, const char *func_name,
150 			 uint32_t line_num);
151 
152 #else /* NBUF_FRAG_MEMORY_DEBUG */
153 
qdf_frag_debug_init(void)154 static inline void qdf_frag_debug_init(void)
155 {
156 }
157 
qdf_frag_debug_exit(void)158 static inline void qdf_frag_debug_exit(void)
159 {
160 }
161 
qdf_frag_debug_add_node(qdf_frag_t fragp,const char * func_name,uint32_t line_num)162 static inline void qdf_frag_debug_add_node(qdf_frag_t fragp,
163 					   const char *func_name,
164 					   uint32_t line_num)
165 {
166 }
167 
qdf_frag_debug_refcount_inc(qdf_frag_t fragp,const char * func_name,uint32_t line_num)168 static inline void qdf_frag_debug_refcount_inc(qdf_frag_t fragp,
169 					       const char *func_name,
170 					       uint32_t line_num)
171 {
172 }
173 
qdf_frag_debug_refcount_dec(qdf_frag_t fragp,const char * func_name,uint32_t line_num)174 static inline void qdf_frag_debug_refcount_dec(qdf_frag_t fragp,
175 					       const char *func_name,
176 					       uint32_t line_num)
177 {
178 }
179 
qdf_frag_debug_delete_node(qdf_frag_t fragp,const char * func_name,uint32_t line_num)180 static inline void qdf_frag_debug_delete_node(qdf_frag_t fragp,
181 					      const char *func_name,
182 					      uint32_t line_num)
183 {
184 }
185 
qdf_frag_debug_update_addr(qdf_frag_t p_fragp,qdf_frag_t n_fragp,const char * func_name,uint32_t line_num)186 static inline void qdf_frag_debug_update_addr(qdf_frag_t p_fragp,
187 					      qdf_frag_t n_fragp,
188 					      const char *func_name,
189 					      uint32_t line_num)
190 {
191 }
192 
193 /**
194  * qdf_frag_alloc() - Allocate frag memory
195  * @pf_cache: page frag cache
196  * @fragsz: Size of frag memory to be allocated
197  *
198  * Return: Allocated frag address
199  */
qdf_frag_alloc(qdf_frag_cache_t * pf_cache,unsigned int fragsz)200 static inline qdf_frag_t qdf_frag_alloc(qdf_frag_cache_t *pf_cache,
201 					unsigned int fragsz)
202 {
203 	return __qdf_frag_alloc(pf_cache, fragsz);
204 }
205 
206 /**
207  * qdf_frag_free() - Free allocated frag memory
208  * @vaddr: Frag address to be freed
209  *
210  * Return: none
211  */
qdf_frag_free(qdf_frag_t vaddr)212 static inline void qdf_frag_free(qdf_frag_t vaddr)
213 {
214 	__qdf_frag_free(vaddr);
215 }
216 
217 #endif /* NBUF_FRAG_MEMORY_DEBUG */
218 
219 /**
220  * qdf_frag_count_get() - Get global frag gauge
221  *
222  * Return: Global frag gauge
223  */
qdf_frag_count_get(void)224 static inline uint32_t qdf_frag_count_get(void)
225 {
226 	return __qdf_frag_count_get();
227 }
228 
229 /**
230  * qdf_frag_count_inc() - Increment global frag count
231  * @value: Increment value
232  *
233  * Return: none
234  */
qdf_frag_count_inc(uint32_t value)235 static inline void qdf_frag_count_inc(uint32_t value)
236 {
237 	return __qdf_frag_count_inc(value);
238 }
239 
240 /**
241  * qdf_frag_count_dec() - Decrement global frag count
242  * @value: Decrement value
243  *
244  * Return: none
245  */
qdf_frag_count_dec(uint32_t value)246 static inline void qdf_frag_count_dec(uint32_t value)
247 {
248 	return __qdf_frag_count_dec(value);
249 }
250 
251 /**
252  * qdf_frag_mod_init() - Initialization routine for qdf_frag
253  *
254  * Return: none
255  */
qdf_frag_mod_init(void)256 static inline void qdf_frag_mod_init(void)
257 {
258 	return __qdf_frag_mod_init();
259 }
260 
261 /**
262  * qdf_frag_mod_exit() - Unintialization routine for qdf_frag
263  *
264  * Return: none
265  */
qdf_frag_mod_exit(void)266 static inline void qdf_frag_mod_exit(void)
267 {
268 	return __qdf_frag_mod_exit();
269 }
270 
271 /**
272  * qdf_mem_map_page() - Map Page
273  * @osdev: qdf_device_t
274  * @buf: Virtual page address to be mapped
275  * @dir: qdf_dma_dir_t
276  * @nbytes: Size of memory to be mapped
277  * @phy_addr: Corresponding mapped physical address
278  *
279  * Return: QDF_STATUS
280  */
qdf_mem_map_page(qdf_device_t osdev,qdf_frag_t buf,qdf_dma_dir_t dir,size_t nbytes,qdf_dma_addr_t * phy_addr)281 static inline QDF_STATUS qdf_mem_map_page(qdf_device_t osdev, qdf_frag_t buf,
282 					  qdf_dma_dir_t dir, size_t nbytes,
283 					  qdf_dma_addr_t *phy_addr)
284 {
285 	return __qdf_mem_map_page(osdev, buf, dir, nbytes, phy_addr);
286 }
287 
288 /**
289  * qdf_mem_unmap_page() - Unmap Page
290  * @osdev: qdf_device_t
291  * @paddr: Physical memory to be unmapped
292  * @nbytes: Size of memory to be unmapped
293  * @dir: qdf_dma_dir_t
294  */
qdf_mem_unmap_page(qdf_device_t osdev,qdf_dma_addr_t paddr,size_t nbytes,qdf_dma_dir_t dir)295 static inline void qdf_mem_unmap_page(qdf_device_t osdev, qdf_dma_addr_t paddr,
296 				      size_t nbytes, qdf_dma_dir_t dir)
297 {
298 	__qdf_mem_unmap_page(osdev, paddr, nbytes, dir);
299 }
300 
301 /*
302  * qdf_frag_cache_drain() - Drain page frag cache
303  *
304  * @pf_cache: page frag cache
305  *
306  * Return: void
307  */
qdf_frag_cache_drain(qdf_frag_cache_t * pf_cache)308 static inline void qdf_frag_cache_drain(qdf_frag_cache_t *pf_cache)
309 {
310 	__qdf_frag_cache_drain(pf_cache);
311 }
312 #endif /* _QDF_NBUF_FRAG_H */
313