xref: /wlan-dirver/qca-wifi-host-cmn/wmi/src/wmi_unified.c (revision 3149adf58a329e17232a4c0e58d460d025edd55a)
1 /*
2  * Copyright (c) 2015-2018 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  * Host WMI unified implementation
30  */
31 #include "htc_api.h"
32 #include "htc_api.h"
33 #include "wmi_unified_priv.h"
34 #include "qdf_module.h"
35 
36 #ifndef WMI_NON_TLV_SUPPORT
37 #include "wmi_tlv_helper.h"
38 #endif
39 
40 #include <linux/debugfs.h>
41 
42 /* This check for CONFIG_WIN temporary added due to redeclaration compilation
43 error in MCL. Error is caused due to inclusion of wmi.h in wmi_unified_api.h
44 which gets included here through ol_if_athvar.h. Eventually it is expected that
45 wmi.h will be removed from wmi_unified_api.h after cleanup, which will need
46 WMI_CMD_HDR to be defined here. */
47 #ifdef CONFIG_WIN
48 /* Copied from wmi.h */
49 #undef MS
50 #define MS(_v, _f) (((_v) & _f##_MASK) >> _f##_LSB)
51 #undef SM
52 #define SM(_v, _f) (((_v) << _f##_LSB) & _f##_MASK)
53 #undef WO
54 #define WO(_f)      ((_f##_OFFSET) >> 2)
55 
56 #undef GET_FIELD
57 #define GET_FIELD(_addr, _f) MS(*((A_UINT32 *)(_addr) + WO(_f)), _f)
58 #undef SET_FIELD
59 #define SET_FIELD(_addr, _f, _val)  \
60 	    (*((A_UINT32 *)(_addr) + WO(_f)) = \
61 		(*((A_UINT32 *)(_addr) + WO(_f)) & ~_f##_MASK) | SM(_val, _f))
62 
63 #define WMI_GET_FIELD(_msg_buf, _msg_type, _f) \
64 	    GET_FIELD(_msg_buf, _msg_type ## _ ## _f)
65 
66 #define WMI_SET_FIELD(_msg_buf, _msg_type, _f, _val) \
67 	    SET_FIELD(_msg_buf, _msg_type ## _ ## _f, _val)
68 
69 #define WMI_EP_APASS           0x0
70 #define WMI_EP_LPASS           0x1
71 #define WMI_EP_SENSOR          0x2
72 
73 /*
74  *  * Control Path
75  *   */
76 typedef PREPACK struct {
77 	A_UINT32	commandId:24,
78 			reserved:2, /* used for WMI endpoint ID */
79 			plt_priv:6; /* platform private */
80 } POSTPACK WMI_CMD_HDR;        /* used for commands and events */
81 
82 #define WMI_CMD_HDR_COMMANDID_LSB           0
83 #define WMI_CMD_HDR_COMMANDID_MASK          0x00ffffff
84 #define WMI_CMD_HDR_COMMANDID_OFFSET        0x00000000
85 #define WMI_CMD_HDR_WMI_ENDPOINTID_MASK        0x03000000
86 #define WMI_CMD_HDR_WMI_ENDPOINTID_OFFSET      24
87 #define WMI_CMD_HDR_PLT_PRIV_LSB               24
88 #define WMI_CMD_HDR_PLT_PRIV_MASK              0xff000000
89 #define WMI_CMD_HDR_PLT_PRIV_OFFSET            0x00000000
90 /* end of copy wmi.h */
91 #endif /* CONFIG_WIN */
92 
93 #define WMI_MIN_HEAD_ROOM 64
94 
95 #ifdef WMI_INTERFACE_EVENT_LOGGING
96 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0))
97 /* TODO Cleanup this backported function */
98 static int wmi_bp_seq_printf(struct seq_file *m, const char *f, ...)
99 {
100 	va_list args;
101 
102 	va_start(args, f);
103 	seq_vprintf(m, f, args);
104 	va_end(args);
105 
106 	return 0;
107 }
108 #else
109 #define wmi_bp_seq_printf(m, fmt, ...) seq_printf((m), fmt, ##__VA_ARGS__)
110 #endif
111 
112 #ifndef MAX_WMI_INSTANCES
113 #ifdef CONFIG_MCL
114 #define MAX_WMI_INSTANCES 1
115 #else
116 #define MAX_WMI_INSTANCES 3
117 #endif
118 #define CUSTOM_MGMT_CMD_DATA_SIZE 4
119 #endif
120 
121 #ifdef CONFIG_MCL
122 /* WMI commands */
123 uint32_t g_wmi_command_buf_idx = 0;
124 struct wmi_command_debug wmi_command_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
125 
126 /* WMI commands TX completed */
127 uint32_t g_wmi_command_tx_cmp_buf_idx = 0;
128 struct wmi_command_debug
129 	wmi_command_tx_cmp_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
130 
131 /* WMI events when processed */
132 uint32_t g_wmi_event_buf_idx = 0;
133 struct wmi_event_debug wmi_event_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
134 
135 /* WMI events when queued */
136 uint32_t g_wmi_rx_event_buf_idx = 0;
137 struct wmi_event_debug wmi_rx_event_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
138 #endif
139 
140 #define WMI_COMMAND_RECORD(h, a, b) {					\
141 	if (wmi_log_max_entry <=					\
142 		*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx))	\
143 		*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx) = 0;\
144 	((struct wmi_command_debug *)h->log_info.wmi_command_log_buf_info.buf)\
145 		[*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)]\
146 						.command = a;		\
147 	qdf_mem_copy(((struct wmi_command_debug *)h->log_info.		\
148 				wmi_command_log_buf_info.buf)		\
149 		[*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)].data,\
150 			b, wmi_record_max_length);			\
151 	((struct wmi_command_debug *)h->log_info.wmi_command_log_buf_info.buf)\
152 		[*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)].\
153 		time = qdf_get_log_timestamp();			\
154 	(*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx))++;	\
155 	h->log_info.wmi_command_log_buf_info.length++;			\
156 }
157 
158 #define WMI_COMMAND_TX_CMP_RECORD(h, a, b) {				\
159 	if (wmi_log_max_entry <=					\
160 		*(h->log_info.wmi_command_tx_cmp_log_buf_info.p_buf_tail_idx))\
161 		*(h->log_info.wmi_command_tx_cmp_log_buf_info.		\
162 				p_buf_tail_idx) = 0;			\
163 	((struct wmi_command_debug *)h->log_info.			\
164 		wmi_command_tx_cmp_log_buf_info.buf)			\
165 		[*(h->log_info.wmi_command_tx_cmp_log_buf_info.		\
166 				p_buf_tail_idx)].			\
167 							command	= a;	\
168 	qdf_mem_copy(((struct wmi_command_debug *)h->log_info.		\
169 				wmi_command_tx_cmp_log_buf_info.buf)	\
170 		[*(h->log_info.wmi_command_tx_cmp_log_buf_info.		\
171 			p_buf_tail_idx)].				\
172 		data, b, wmi_record_max_length);			\
173 	((struct wmi_command_debug *)h->log_info.			\
174 		wmi_command_tx_cmp_log_buf_info.buf)			\
175 		[*(h->log_info.wmi_command_tx_cmp_log_buf_info.		\
176 				p_buf_tail_idx)].			\
177 		time = qdf_get_log_timestamp();				\
178 	(*(h->log_info.wmi_command_tx_cmp_log_buf_info.p_buf_tail_idx))++;\
179 	h->log_info.wmi_command_tx_cmp_log_buf_info.length++;		\
180 }
181 
182 #define WMI_EVENT_RECORD(h, a, b) {					\
183 	if (wmi_log_max_entry <=					\
184 		*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx))	\
185 		*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx) = 0;\
186 	((struct wmi_event_debug *)h->log_info.wmi_event_log_buf_info.buf)\
187 		[*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)].	\
188 		event = a;						\
189 	qdf_mem_copy(((struct wmi_event_debug *)h->log_info.		\
190 				wmi_event_log_buf_info.buf)		\
191 		[*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)].data, b,\
192 		wmi_record_max_length);					\
193 	((struct wmi_event_debug *)h->log_info.wmi_event_log_buf_info.buf)\
194 		[*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)].time =\
195 		qdf_get_log_timestamp();				\
196 	(*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx))++;	\
197 	h->log_info.wmi_event_log_buf_info.length++;			\
198 }
199 
200 #define WMI_RX_EVENT_RECORD(h, a, b) {					\
201 	if (wmi_log_max_entry <=					\
202 		*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx))\
203 		*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx) = 0;\
204 	((struct wmi_event_debug *)h->log_info.wmi_rx_event_log_buf_info.buf)\
205 		[*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
206 		event = a;						\
207 	qdf_mem_copy(((struct wmi_event_debug *)h->log_info.		\
208 				wmi_rx_event_log_buf_info.buf)		\
209 		[*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
210 			data, b, wmi_record_max_length);		\
211 	((struct wmi_event_debug *)h->log_info.wmi_rx_event_log_buf_info.buf)\
212 		[*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
213 		time =	qdf_get_log_timestamp();			\
214 	(*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx))++;	\
215 	h->log_info.wmi_rx_event_log_buf_info.length++;			\
216 }
217 
218 #ifdef CONFIG_MCL
219 uint32_t g_wmi_mgmt_command_buf_idx = 0;
220 struct
221 wmi_command_debug wmi_mgmt_command_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
222 
223 /* wmi_mgmt commands TX completed */
224 uint32_t g_wmi_mgmt_command_tx_cmp_buf_idx = 0;
225 struct wmi_command_debug
226 wmi_mgmt_command_tx_cmp_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
227 
228 /* wmi_mgmt events when processed */
229 uint32_t g_wmi_mgmt_event_buf_idx = 0;
230 struct wmi_event_debug
231 wmi_mgmt_event_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
232 #endif
233 
234 #define WMI_MGMT_COMMAND_RECORD(h, a, b) {                              \
235 	if (wmi_mgmt_log_max_entry <=                                   \
236 		*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)) \
237 		*(h->log_info.wmi_mgmt_command_log_buf_info.		\
238 				p_buf_tail_idx) = 0;			\
239 	((struct wmi_command_debug *)h->log_info.                       \
240 		 wmi_mgmt_command_log_buf_info.buf)                     \
241 		[*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
242 			command = a;                                    \
243 	qdf_mem_copy(((struct wmi_command_debug *)h->log_info.          \
244 				wmi_mgmt_command_log_buf_info.buf)      \
245 		[*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
246 		data, b,                                                \
247 		wmi_record_max_length);                                	\
248 	((struct wmi_command_debug *)h->log_info.                       \
249 		 wmi_mgmt_command_log_buf_info.buf)                     \
250 		[*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
251 			time =        qdf_get_log_timestamp();          \
252 	(*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx))++;\
253 	h->log_info.wmi_mgmt_command_log_buf_info.length++;             \
254 }
255 
256 #define WMI_MGMT_COMMAND_TX_CMP_RECORD(h, a, b) {			\
257 	if (wmi_mgmt_log_max_entry <=					\
258 		*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.	\
259 			p_buf_tail_idx))				\
260 		*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.	\
261 			p_buf_tail_idx) = 0;				\
262 	((struct wmi_command_debug *)h->log_info.			\
263 			wmi_mgmt_command_tx_cmp_log_buf_info.buf)	\
264 		[*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.	\
265 				p_buf_tail_idx)].command = a;		\
266 	qdf_mem_copy(((struct wmi_command_debug *)h->log_info.		\
267 				wmi_mgmt_command_tx_cmp_log_buf_info.buf)\
268 		[*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.	\
269 			p_buf_tail_idx)].data, b,			\
270 			wmi_record_max_length);				\
271 	((struct wmi_command_debug *)h->log_info.			\
272 			wmi_mgmt_command_tx_cmp_log_buf_info.buf)	\
273 		[*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.	\
274 				p_buf_tail_idx)].time =			\
275 		qdf_get_log_timestamp();				\
276 	(*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.		\
277 			p_buf_tail_idx))++;				\
278 	h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.length++;	\
279 }
280 
281 #define WMI_MGMT_EVENT_RECORD(h, a, b) {				\
282 	if (wmi_mgmt_log_max_entry <=					\
283 		*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx))\
284 		*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx) = 0;\
285 	((struct wmi_event_debug *)h->log_info.wmi_mgmt_event_log_buf_info.buf)\
286 		[*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)]\
287 					.event = a;			\
288 	qdf_mem_copy(((struct wmi_event_debug *)h->log_info.		\
289 				wmi_mgmt_event_log_buf_info.buf)	\
290 		[*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)].\
291 			data, b, wmi_record_max_length);		\
292 	((struct wmi_event_debug *)h->log_info.wmi_mgmt_event_log_buf_info.buf)\
293 		[*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)].\
294 			time = qdf_get_log_timestamp();			\
295 	(*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx))++;	\
296 	h->log_info.wmi_mgmt_event_log_buf_info.length++;		\
297 }
298 
299 /* These are defined to made it as module param, which can be configured */
300 uint32_t wmi_log_max_entry = WMI_EVENT_DEBUG_MAX_ENTRY;
301 uint32_t wmi_mgmt_log_max_entry = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
302 uint32_t wmi_record_max_length = WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH;
303 uint32_t wmi_display_size = 100;
304 
305 /**
306  * wmi_log_init() - Initialize WMI event logging
307  * @wmi_handle: WMI handle.
308  *
309  * Return: Initialization status
310  */
311 #ifdef CONFIG_MCL
312 static QDF_STATUS wmi_log_init(struct wmi_unified *wmi_handle)
313 {
314 	struct wmi_log_buf_t *cmd_log_buf =
315 			&wmi_handle->log_info.wmi_command_log_buf_info;
316 	struct wmi_log_buf_t *cmd_tx_cmpl_log_buf =
317 			&wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info;
318 
319 	struct wmi_log_buf_t *event_log_buf =
320 			&wmi_handle->log_info.wmi_event_log_buf_info;
321 	struct wmi_log_buf_t *rx_event_log_buf =
322 			&wmi_handle->log_info.wmi_rx_event_log_buf_info;
323 
324 	struct wmi_log_buf_t *mgmt_cmd_log_buf =
325 			&wmi_handle->log_info.wmi_mgmt_command_log_buf_info;
326 	struct wmi_log_buf_t *mgmt_cmd_tx_cmp_log_buf =
327 		&wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info;
328 	struct wmi_log_buf_t *mgmt_event_log_buf =
329 			&wmi_handle->log_info.wmi_mgmt_event_log_buf_info;
330 
331 	/* WMI commands */
332 	cmd_log_buf->length = 0;
333 	cmd_log_buf->buf_tail_idx = 0;
334 	cmd_log_buf->buf = wmi_command_log_buffer;
335 	cmd_log_buf->p_buf_tail_idx = &g_wmi_command_buf_idx;
336 	cmd_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
337 
338 	/* WMI commands TX completed */
339 	cmd_tx_cmpl_log_buf->length = 0;
340 	cmd_tx_cmpl_log_buf->buf_tail_idx = 0;
341 	cmd_tx_cmpl_log_buf->buf = wmi_command_tx_cmp_log_buffer;
342 	cmd_tx_cmpl_log_buf->p_buf_tail_idx = &g_wmi_command_tx_cmp_buf_idx;
343 	cmd_tx_cmpl_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
344 
345 	/* WMI events when processed */
346 	event_log_buf->length = 0;
347 	event_log_buf->buf_tail_idx = 0;
348 	event_log_buf->buf = wmi_event_log_buffer;
349 	event_log_buf->p_buf_tail_idx = &g_wmi_event_buf_idx;
350 	event_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
351 
352 	/* WMI events when queued */
353 	rx_event_log_buf->length = 0;
354 	rx_event_log_buf->buf_tail_idx = 0;
355 	rx_event_log_buf->buf = wmi_rx_event_log_buffer;
356 	rx_event_log_buf->p_buf_tail_idx = &g_wmi_rx_event_buf_idx;
357 	rx_event_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
358 
359 	/* WMI Management commands */
360 	mgmt_cmd_log_buf->length = 0;
361 	mgmt_cmd_log_buf->buf_tail_idx = 0;
362 	mgmt_cmd_log_buf->buf = wmi_mgmt_command_log_buffer;
363 	mgmt_cmd_log_buf->p_buf_tail_idx = &g_wmi_mgmt_command_buf_idx;
364 	mgmt_cmd_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
365 
366 	/* WMI Management commands Tx completed*/
367 	mgmt_cmd_tx_cmp_log_buf->length = 0;
368 	mgmt_cmd_tx_cmp_log_buf->buf_tail_idx = 0;
369 	mgmt_cmd_tx_cmp_log_buf->buf = wmi_mgmt_command_tx_cmp_log_buffer;
370 	mgmt_cmd_tx_cmp_log_buf->p_buf_tail_idx =
371 		&g_wmi_mgmt_command_tx_cmp_buf_idx;
372 	mgmt_cmd_tx_cmp_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
373 
374 	/* WMI Management events when processed*/
375 	mgmt_event_log_buf->length = 0;
376 	mgmt_event_log_buf->buf_tail_idx = 0;
377 	mgmt_event_log_buf->buf = wmi_mgmt_event_log_buffer;
378 	mgmt_event_log_buf->p_buf_tail_idx = &g_wmi_mgmt_event_buf_idx;
379 	mgmt_event_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
380 
381 	qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
382 	wmi_handle->log_info.wmi_logging_enable = 1;
383 
384 	return QDF_STATUS_SUCCESS;
385 }
386 #else
387 static QDF_STATUS wmi_log_init(struct wmi_unified *wmi_handle)
388 {
389 	struct wmi_log_buf_t *cmd_log_buf =
390 			&wmi_handle->log_info.wmi_command_log_buf_info;
391 	struct wmi_log_buf_t *cmd_tx_cmpl_log_buf =
392 			&wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info;
393 
394 	struct wmi_log_buf_t *event_log_buf =
395 			&wmi_handle->log_info.wmi_event_log_buf_info;
396 	struct wmi_log_buf_t *rx_event_log_buf =
397 			&wmi_handle->log_info.wmi_rx_event_log_buf_info;
398 
399 	struct wmi_log_buf_t *mgmt_cmd_log_buf =
400 			&wmi_handle->log_info.wmi_mgmt_command_log_buf_info;
401 	struct wmi_log_buf_t *mgmt_cmd_tx_cmp_log_buf =
402 		&wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info;
403 	struct wmi_log_buf_t *mgmt_event_log_buf =
404 			&wmi_handle->log_info.wmi_mgmt_event_log_buf_info;
405 
406 	wmi_handle->log_info.wmi_logging_enable = 0;
407 
408 	/* WMI commands */
409 	cmd_log_buf->length = 0;
410 	cmd_log_buf->buf_tail_idx = 0;
411 	cmd_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
412 		wmi_log_max_entry * sizeof(struct wmi_command_debug));
413 	cmd_log_buf->size = wmi_log_max_entry;
414 
415 	if (!cmd_log_buf->buf) {
416 		qdf_print("no memory for WMI command log buffer..\n");
417 		return QDF_STATUS_E_NOMEM;
418 	}
419 	cmd_log_buf->p_buf_tail_idx = &cmd_log_buf->buf_tail_idx;
420 
421 	/* WMI commands TX completed */
422 	cmd_tx_cmpl_log_buf->length = 0;
423 	cmd_tx_cmpl_log_buf->buf_tail_idx = 0;
424 	cmd_tx_cmpl_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
425 		wmi_log_max_entry * sizeof(struct wmi_command_debug));
426 	cmd_tx_cmpl_log_buf->size = wmi_log_max_entry;
427 
428 	if (!cmd_tx_cmpl_log_buf->buf) {
429 		qdf_print("no memory for WMI Command Tx Complete log buffer..\n");
430 		return QDF_STATUS_E_NOMEM;
431 	}
432 	cmd_tx_cmpl_log_buf->p_buf_tail_idx =
433 		&cmd_tx_cmpl_log_buf->buf_tail_idx;
434 
435 	/* WMI events when processed */
436 	event_log_buf->length = 0;
437 	event_log_buf->buf_tail_idx = 0;
438 	event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
439 		wmi_log_max_entry * sizeof(struct wmi_event_debug));
440 	event_log_buf->size = wmi_log_max_entry;
441 
442 	if (!event_log_buf->buf) {
443 		qdf_print("no memory for WMI Event log buffer..\n");
444 		return QDF_STATUS_E_NOMEM;
445 	}
446 	event_log_buf->p_buf_tail_idx = &event_log_buf->buf_tail_idx;
447 
448 	/* WMI events when queued */
449 	rx_event_log_buf->length = 0;
450 	rx_event_log_buf->buf_tail_idx = 0;
451 	rx_event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
452 		wmi_log_max_entry * sizeof(struct wmi_event_debug));
453 	rx_event_log_buf->size = wmi_log_max_entry;
454 
455 	if (!rx_event_log_buf->buf) {
456 		qdf_print("no memory for WMI Event Rx log buffer..\n");
457 		return QDF_STATUS_E_NOMEM;
458 	}
459 	rx_event_log_buf->p_buf_tail_idx = &rx_event_log_buf->buf_tail_idx;
460 
461 	/* WMI Management commands */
462 	mgmt_cmd_log_buf->length = 0;
463 	mgmt_cmd_log_buf->buf_tail_idx = 0;
464 	mgmt_cmd_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
465 		wmi_mgmt_log_max_entry * sizeof(struct wmi_command_debug));
466 	mgmt_cmd_log_buf->size = wmi_mgmt_log_max_entry;
467 
468 	if (!mgmt_cmd_log_buf->buf) {
469 		qdf_print("no memory for WMI Management Command log buffer..\n");
470 		return QDF_STATUS_E_NOMEM;
471 	}
472 	mgmt_cmd_log_buf->p_buf_tail_idx = &mgmt_cmd_log_buf->buf_tail_idx;
473 
474 	/* WMI Management commands Tx completed*/
475 	mgmt_cmd_tx_cmp_log_buf->length = 0;
476 	mgmt_cmd_tx_cmp_log_buf->buf_tail_idx = 0;
477 	mgmt_cmd_tx_cmp_log_buf->buf = (struct wmi_command_debug *)
478 		qdf_mem_malloc(
479 		wmi_mgmt_log_max_entry *
480 		sizeof(struct wmi_command_debug));
481 	mgmt_cmd_tx_cmp_log_buf->size = wmi_mgmt_log_max_entry;
482 
483 	if (!mgmt_cmd_tx_cmp_log_buf->buf) {
484 		qdf_print("no memory for WMI Management Command Tx complete log buffer..\n");
485 		return QDF_STATUS_E_NOMEM;
486 	}
487 	mgmt_cmd_tx_cmp_log_buf->p_buf_tail_idx =
488 		&mgmt_cmd_tx_cmp_log_buf->buf_tail_idx;
489 
490 	/* WMI Management events when processed*/
491 	mgmt_event_log_buf->length = 0;
492 	mgmt_event_log_buf->buf_tail_idx = 0;
493 
494 	mgmt_event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
495 		wmi_mgmt_log_max_entry *
496 		sizeof(struct wmi_event_debug));
497 	mgmt_event_log_buf->size = wmi_mgmt_log_max_entry;
498 
499 	if (!mgmt_event_log_buf->buf) {
500 		qdf_print("no memory for WMI Management Event log buffer..\n");
501 		return QDF_STATUS_E_NOMEM;
502 	}
503 	mgmt_event_log_buf->p_buf_tail_idx = &mgmt_event_log_buf->buf_tail_idx;
504 
505 	qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
506 	wmi_handle->log_info.wmi_logging_enable = 1;
507 
508 	return QDF_STATUS_SUCCESS;
509 }
510 #endif
511 
512 /**
513  * wmi_log_buffer_free() - Free all dynamic allocated buffer memory for
514  * event logging
515  * @wmi_handle: WMI handle.
516  *
517  * Return: None
518  */
519 #ifndef CONFIG_MCL
520 static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle)
521 {
522 	if (wmi_handle->log_info.wmi_command_log_buf_info.buf)
523 		qdf_mem_free(wmi_handle->log_info.wmi_command_log_buf_info.buf);
524 	if (wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info.buf)
525 		qdf_mem_free(
526 		wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info.buf);
527 	if (wmi_handle->log_info.wmi_event_log_buf_info.buf)
528 		qdf_mem_free(wmi_handle->log_info.wmi_event_log_buf_info.buf);
529 	if (wmi_handle->log_info.wmi_rx_event_log_buf_info.buf)
530 		qdf_mem_free(
531 			wmi_handle->log_info.wmi_rx_event_log_buf_info.buf);
532 	if (wmi_handle->log_info.wmi_mgmt_command_log_buf_info.buf)
533 		qdf_mem_free(
534 			wmi_handle->log_info.wmi_mgmt_command_log_buf_info.buf);
535 	if (wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.buf)
536 		qdf_mem_free(
537 		wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.buf);
538 	if (wmi_handle->log_info.wmi_mgmt_event_log_buf_info.buf)
539 		qdf_mem_free(
540 			wmi_handle->log_info.wmi_mgmt_event_log_buf_info.buf);
541 	wmi_handle->log_info.wmi_logging_enable = 0;
542 	qdf_spinlock_destroy(&wmi_handle->log_info.wmi_record_lock);
543 }
544 #else
545 static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle)
546 {
547 	/* Do Nothing */
548 }
549 #endif
550 
551 /**
552  * wmi_print_cmd_log_buffer() - an output agnostic wmi command log printer
553  * @log_buffer: the command log buffer metadata of the buffer to print
554  * @count: the maximum number of entries to print
555  * @print: an abstract print method, e.g. a qdf_print() or seq_printf() wrapper
556  * @print_priv: any data required by the print method, e.g. a file handle
557  *
558  * Return: None
559  */
560 static void
561 wmi_print_cmd_log_buffer(struct wmi_log_buf_t *log_buffer, uint32_t count,
562 			 qdf_abstract_print *print, void *print_priv)
563 {
564 	static const int data_len =
565 		WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH / sizeof(uint32_t);
566 	char str[128];
567 	uint32_t idx;
568 
569 	if (count > log_buffer->size)
570 		count = log_buffer->size;
571 	if (count > log_buffer->length)
572 		count = log_buffer->length;
573 
574 	/* subtract count from index, and wrap if necessary */
575 	idx = log_buffer->size + *log_buffer->p_buf_tail_idx - count;
576 	idx %= log_buffer->size;
577 
578 	print(print_priv, "Time (seconds)      Cmd Id              Payload");
579 	while (count) {
580 		struct wmi_command_debug *cmd_log = (struct wmi_command_debug *)
581 			&((struct wmi_command_debug *)log_buffer->buf)[idx];
582 		uint64_t secs, usecs;
583 		int len = 0;
584 		int i;
585 
586 		qdf_log_timestamp_to_secs(cmd_log->time, &secs, &usecs);
587 		len += scnprintf(str + len, sizeof(str) - len,
588 				 "% 8lld.%06lld    %6u (0x%06x)    ",
589 				 secs, usecs,
590 				 cmd_log->command, cmd_log->command);
591 		for (i = 0; i < data_len; ++i) {
592 			len += scnprintf(str + len, sizeof(str) - len,
593 					 "0x%08x ", cmd_log->data[i]);
594 		}
595 
596 		print(print_priv, str);
597 
598 		--count;
599 		++idx;
600 		if (idx >= log_buffer->size)
601 			idx = 0;
602 	}
603 }
604 
605 /**
606  * wmi_print_event_log_buffer() - an output agnostic wmi event log printer
607  * @log_buffer: the event log buffer metadata of the buffer to print
608  * @count: the maximum number of entries to print
609  * @print: an abstract print method, e.g. a qdf_print() or seq_printf() wrapper
610  * @print_priv: any data required by the print method, e.g. a file handle
611  *
612  * Return: None
613  */
614 static void
615 wmi_print_event_log_buffer(struct wmi_log_buf_t *log_buffer, uint32_t count,
616 			   qdf_abstract_print *print, void *print_priv)
617 {
618 	static const int data_len =
619 		WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH / sizeof(uint32_t);
620 	char str[128];
621 	uint32_t idx;
622 
623 	if (count > log_buffer->size)
624 		count = log_buffer->size;
625 	if (count > log_buffer->length)
626 		count = log_buffer->length;
627 
628 	/* subtract count from index, and wrap if necessary */
629 	idx = log_buffer->size + *log_buffer->p_buf_tail_idx - count;
630 	idx %= log_buffer->size;
631 
632 	print(print_priv, "Time (seconds)      Event Id             Payload");
633 	while (count) {
634 		struct wmi_event_debug *event_log = (struct wmi_event_debug *)
635 			&((struct wmi_event_debug *)log_buffer->buf)[idx];
636 		uint64_t secs, usecs;
637 		int len = 0;
638 		int i;
639 
640 		qdf_log_timestamp_to_secs(event_log->time, &secs, &usecs);
641 		len += scnprintf(str + len, sizeof(str) - len,
642 				 "% 8lld.%06lld    %6u (0x%06x)    ",
643 				 secs, usecs,
644 				 event_log->event, event_log->event);
645 		for (i = 0; i < data_len; ++i) {
646 			len += scnprintf(str + len, sizeof(str) - len,
647 					 "0x%08x ", event_log->data[i]);
648 		}
649 
650 		print(print_priv, str);
651 
652 		--count;
653 		++idx;
654 		if (idx >= log_buffer->size)
655 			idx = 0;
656 	}
657 }
658 
659 inline void
660 wmi_print_cmd_log(wmi_unified_t wmi, uint32_t count,
661 		  qdf_abstract_print *print, void *print_priv)
662 {
663 	wmi_print_cmd_log_buffer(
664 		&wmi->log_info.wmi_command_log_buf_info,
665 		count, print, print_priv);
666 }
667 
668 inline void
669 wmi_print_cmd_tx_cmp_log(wmi_unified_t wmi, uint32_t count,
670 			 qdf_abstract_print *print, void *print_priv)
671 {
672 	wmi_print_cmd_log_buffer(
673 		&wmi->log_info.wmi_command_tx_cmp_log_buf_info,
674 		count, print, print_priv);
675 }
676 
677 inline void
678 wmi_print_mgmt_cmd_log(wmi_unified_t wmi, uint32_t count,
679 		       qdf_abstract_print *print, void *print_priv)
680 {
681 	wmi_print_cmd_log_buffer(
682 		&wmi->log_info.wmi_mgmt_command_log_buf_info,
683 		count, print, print_priv);
684 }
685 
686 inline void
687 wmi_print_mgmt_cmd_tx_cmp_log(wmi_unified_t wmi, uint32_t count,
688 			      qdf_abstract_print *print, void *print_priv)
689 {
690 	wmi_print_cmd_log_buffer(
691 		&wmi->log_info.wmi_mgmt_command_tx_cmp_log_buf_info,
692 		count, print, print_priv);
693 }
694 
695 inline void
696 wmi_print_event_log(wmi_unified_t wmi, uint32_t count,
697 		    qdf_abstract_print *print, void *print_priv)
698 {
699 	wmi_print_event_log_buffer(
700 		&wmi->log_info.wmi_event_log_buf_info,
701 		count, print, print_priv);
702 }
703 
704 inline void
705 wmi_print_rx_event_log(wmi_unified_t wmi, uint32_t count,
706 		       qdf_abstract_print *print, void *print_priv)
707 {
708 	wmi_print_event_log_buffer(
709 		&wmi->log_info.wmi_rx_event_log_buf_info,
710 		count, print, print_priv);
711 }
712 
713 inline void
714 wmi_print_mgmt_event_log(wmi_unified_t wmi, uint32_t count,
715 			 qdf_abstract_print *print, void *print_priv)
716 {
717 	wmi_print_event_log_buffer(
718 		&wmi->log_info.wmi_mgmt_event_log_buf_info,
719 		count, print, print_priv);
720 }
721 
722 #ifdef CONFIG_MCL
723 const int8_t * const debugfs_dir[MAX_WMI_INSTANCES] = {"WMI0"};
724 #else
725 const int8_t * const debugfs_dir[MAX_WMI_INSTANCES] = {"WMI0", "WMI1", "WMI2"};
726 #endif
727 
728 /* debugfs routines*/
729 
730 /**
731  * debug_wmi_##func_base##_show() - debugfs functions to display content of
732  * command and event buffers. Macro uses max buffer length to display
733  * buffer when it is wraparound.
734  *
735  * @m: debugfs handler to access wmi_handle
736  * @v: Variable arguments (not used)
737  *
738  * Return: Length of characters printed
739  */
740 #define GENERATE_COMMAND_DEBUG_SHOW_FUNCS(func_base, wmi_ring_size)	\
741 	static int debug_wmi_##func_base##_show(struct seq_file *m,	\
742 						void *v)		\
743 	{								\
744 		wmi_unified_t wmi_handle = (wmi_unified_t) m->private;	\
745 		struct wmi_log_buf_t *wmi_log =				\
746 			&wmi_handle->log_info.wmi_##func_base##_buf_info;\
747 		int pos, nread, outlen;					\
748 		int i;							\
749 		uint64_t secs, usecs;					\
750 									\
751 		qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock);	\
752 		if (!wmi_log->length) {					\
753 			qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);\
754 			return wmi_bp_seq_printf(m,			\
755 			"no elements to read from ring buffer!\n");	\
756 		}							\
757 									\
758 		if (wmi_log->length <= wmi_ring_size)			\
759 			nread = wmi_log->length;			\
760 		else							\
761 			nread = wmi_ring_size;				\
762 									\
763 		if (*(wmi_log->p_buf_tail_idx) == 0)			\
764 			/* tail can be 0 after wrap-around */		\
765 			pos = wmi_ring_size - 1;			\
766 		else							\
767 			pos = *(wmi_log->p_buf_tail_idx) - 1;		\
768 									\
769 		outlen = wmi_bp_seq_printf(m, "Length = %d\n", wmi_log->length);\
770 		qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);	\
771 		while (nread--) {					\
772 			struct wmi_command_debug *wmi_record;		\
773 									\
774 			wmi_record = (struct wmi_command_debug *)	\
775 			&(((struct wmi_command_debug *)wmi_log->buf)[pos]);\
776 			outlen += wmi_bp_seq_printf(m, "CMD ID = %x\n",	\
777 				(wmi_record->command));			\
778 			qdf_log_timestamp_to_secs(wmi_record->time, &secs,\
779 				&usecs);				\
780 			outlen +=					\
781 			wmi_bp_seq_printf(m, "CMD TIME = [%llu.%06llu]\n",\
782 				secs, usecs);				\
783 			outlen += wmi_bp_seq_printf(m, "CMD = ");	\
784 			for (i = 0; i < (wmi_record_max_length/		\
785 					sizeof(uint32_t)); i++)		\
786 				outlen += wmi_bp_seq_printf(m, "%x ",	\
787 					wmi_record->data[i]);		\
788 			outlen += wmi_bp_seq_printf(m, "\n");		\
789 									\
790 			if (pos == 0)					\
791 				pos = wmi_ring_size - 1;		\
792 			else						\
793 				pos--;					\
794 		}							\
795 		return outlen;						\
796 	}								\
797 
798 #define GENERATE_EVENT_DEBUG_SHOW_FUNCS(func_base, wmi_ring_size)	\
799 	static int debug_wmi_##func_base##_show(struct seq_file *m,	\
800 						void *v)		\
801 	{								\
802 		wmi_unified_t wmi_handle = (wmi_unified_t) m->private;	\
803 		struct wmi_log_buf_t *wmi_log =				\
804 			&wmi_handle->log_info.wmi_##func_base##_buf_info;\
805 		int pos, nread, outlen;					\
806 		int i;							\
807 		uint64_t secs, usecs;					\
808 									\
809 		qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock);	\
810 		if (!wmi_log->length) {					\
811 			qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);\
812 			return wmi_bp_seq_printf(m,			\
813 			"no elements to read from ring buffer!\n");	\
814 		}							\
815 									\
816 		if (wmi_log->length <= wmi_ring_size)			\
817 			nread = wmi_log->length;			\
818 		else							\
819 			nread = wmi_ring_size;				\
820 									\
821 		if (*(wmi_log->p_buf_tail_idx) == 0)			\
822 			/* tail can be 0 after wrap-around */		\
823 			pos = wmi_ring_size - 1;			\
824 		else							\
825 			pos = *(wmi_log->p_buf_tail_idx) - 1;		\
826 									\
827 		outlen = wmi_bp_seq_printf(m, "Length = %d\n", wmi_log->length);\
828 		qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);	\
829 		while (nread--) {					\
830 			struct wmi_event_debug *wmi_record;		\
831 									\
832 			wmi_record = (struct wmi_event_debug *)		\
833 			&(((struct wmi_event_debug *)wmi_log->buf)[pos]);\
834 			qdf_log_timestamp_to_secs(wmi_record->time, &secs,\
835 				&usecs);				\
836 			outlen += wmi_bp_seq_printf(m, "Event ID = %x\n",\
837 				(wmi_record->event));			\
838 			outlen +=					\
839 			wmi_bp_seq_printf(m, "Event TIME = [%llu.%06llu]\n",\
840 				secs, usecs);				\
841 			outlen += wmi_bp_seq_printf(m, "CMD = ");	\
842 			for (i = 0; i < (wmi_record_max_length/		\
843 					sizeof(uint32_t)); i++)		\
844 				outlen += wmi_bp_seq_printf(m, "%x ",	\
845 					wmi_record->data[i]);		\
846 			outlen += wmi_bp_seq_printf(m, "\n");		\
847 									\
848 			if (pos == 0)					\
849 				pos = wmi_ring_size - 1;		\
850 			else						\
851 				pos--;					\
852 		}							\
853 		return outlen;						\
854 	}
855 
856 GENERATE_COMMAND_DEBUG_SHOW_FUNCS(command_log, wmi_display_size);
857 GENERATE_COMMAND_DEBUG_SHOW_FUNCS(command_tx_cmp_log, wmi_display_size);
858 GENERATE_EVENT_DEBUG_SHOW_FUNCS(event_log, wmi_display_size);
859 GENERATE_EVENT_DEBUG_SHOW_FUNCS(rx_event_log, wmi_display_size);
860 GENERATE_COMMAND_DEBUG_SHOW_FUNCS(mgmt_command_log, wmi_display_size);
861 GENERATE_COMMAND_DEBUG_SHOW_FUNCS(mgmt_command_tx_cmp_log,
862 					wmi_display_size);
863 GENERATE_EVENT_DEBUG_SHOW_FUNCS(mgmt_event_log, wmi_display_size);
864 
865 /**
866  * debug_wmi_enable_show() - debugfs functions to display enable state of
867  * wmi logging feature.
868  *
869  * @m: debugfs handler to access wmi_handle
870  * @v: Variable arguments (not used)
871  *
872  * Return: always 1
873  */
874 static int debug_wmi_enable_show(struct seq_file *m, void *v)
875 {
876 	wmi_unified_t wmi_handle = (wmi_unified_t) m->private;
877 
878 	return wmi_bp_seq_printf(m, "%d\n",
879 			wmi_handle->log_info.wmi_logging_enable);
880 }
881 
882 /**
883  * debug_wmi_log_size_show() - debugfs functions to display configured size of
884  * wmi logging command/event buffer and management command/event buffer.
885  *
886  * @m: debugfs handler to access wmi_handle
887  * @v: Variable arguments (not used)
888  *
889  * Return: Length of characters printed
890  */
891 static int debug_wmi_log_size_show(struct seq_file *m, void *v)
892 {
893 
894 	wmi_bp_seq_printf(m, "WMI command/event log max size:%d\n",
895 				wmi_log_max_entry);
896 	return wmi_bp_seq_printf(m,
897 			"WMI management command/events log max size:%d\n",
898 			wmi_mgmt_log_max_entry);
899 }
900 
901 /**
902  * debug_wmi_##func_base##_write() - debugfs functions to clear
903  * wmi logging command/event buffer and management command/event buffer.
904  *
905  * @file: file handler to access wmi_handle
906  * @buf: received data buffer
907  * @count: length of received buffer
908  * @ppos: Not used
909  *
910  * Return: count
911  */
912 #define GENERATE_DEBUG_WRITE_FUNCS(func_base, wmi_ring_size, wmi_record_type)\
913 	static ssize_t debug_wmi_##func_base##_write(struct file *file,	\
914 				const char __user *buf,			\
915 				size_t count, loff_t *ppos)		\
916 	{								\
917 		int k, ret;						\
918 		wmi_unified_t wmi_handle =				\
919 			((struct seq_file *)file->private_data)->private;\
920 		struct wmi_log_buf_t *wmi_log = &wmi_handle->log_info.	\
921 				wmi_##func_base##_buf_info;		\
922 		char locbuf[50];					\
923 									\
924 		if ((!buf) || (count > 50))				\
925 			return -EFAULT;					\
926 									\
927 		if (copy_from_user(locbuf, buf, count))			\
928 			return -EFAULT;					\
929 									\
930 		ret = sscanf(locbuf, "%d", &k);				\
931 		if ((ret != 1) || (k != 0)) {                           \
932 			qdf_print("Wrong input, echo 0 to clear the wmi	buffer\n");\
933 			return -EINVAL;					\
934 		}							\
935 									\
936 		qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock);	\
937 		qdf_mem_zero(wmi_log->buf, wmi_ring_size *		\
938 				sizeof(struct wmi_record_type));	\
939 		wmi_log->length = 0;					\
940 		*(wmi_log->p_buf_tail_idx) = 0;				\
941 		qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);	\
942 									\
943 		return count;						\
944 	}
945 
946 GENERATE_DEBUG_WRITE_FUNCS(command_log, wmi_log_max_entry,
947 					wmi_command_debug);
948 GENERATE_DEBUG_WRITE_FUNCS(command_tx_cmp_log, wmi_log_max_entry,
949 					wmi_command_debug);
950 GENERATE_DEBUG_WRITE_FUNCS(event_log, wmi_log_max_entry,
951 					wmi_event_debug);
952 GENERATE_DEBUG_WRITE_FUNCS(rx_event_log, wmi_log_max_entry,
953 					wmi_event_debug);
954 GENERATE_DEBUG_WRITE_FUNCS(mgmt_command_log, wmi_mgmt_log_max_entry,
955 					wmi_command_debug);
956 GENERATE_DEBUG_WRITE_FUNCS(mgmt_command_tx_cmp_log,
957 		wmi_mgmt_log_max_entry, wmi_command_debug);
958 GENERATE_DEBUG_WRITE_FUNCS(mgmt_event_log, wmi_mgmt_log_max_entry,
959 					wmi_event_debug);
960 
961 /**
962  * debug_wmi_enable_write() - debugfs functions to enable/disable
963  * wmi logging feature.
964  *
965  * @file: file handler to access wmi_handle
966  * @buf: received data buffer
967  * @count: length of received buffer
968  * @ppos: Not used
969  *
970  * Return: count
971  */
972 static ssize_t debug_wmi_enable_write(struct file *file, const char __user *buf,
973 					size_t count, loff_t *ppos)
974 {
975 	wmi_unified_t wmi_handle =
976 		((struct seq_file *)file->private_data)->private;
977 	int k, ret;
978 	char locbuf[50];
979 
980 	if ((!buf) || (count > 50))
981 		return -EFAULT;
982 
983 	if (copy_from_user(locbuf, buf, count))
984 		return -EFAULT;
985 
986 	ret = sscanf(locbuf, "%d", &k);
987 	if ((ret != 1) || ((k != 0) && (k != 1)))
988 		return -EINVAL;
989 
990 	wmi_handle->log_info.wmi_logging_enable = k;
991 	return count;
992 }
993 
994 /**
995  * debug_wmi_log_size_write() - reserved.
996  *
997  * @file: file handler to access wmi_handle
998  * @buf: received data buffer
999  * @count: length of received buffer
1000  * @ppos: Not used
1001  *
1002  * Return: count
1003  */
1004 static ssize_t debug_wmi_log_size_write(struct file *file,
1005 		const char __user *buf, size_t count, loff_t *ppos)
1006 {
1007 	return -EINVAL;
1008 }
1009 
1010 /* Structure to maintain debug information */
1011 struct wmi_debugfs_info {
1012 	const char *name;
1013 	struct dentry *de[MAX_WMI_INSTANCES];
1014 	const struct file_operations *ops;
1015 };
1016 
1017 #define DEBUG_FOO(func_base) { .name = #func_base,			\
1018 	.ops = &debug_##func_base##_ops }
1019 
1020 /**
1021  * debug_##func_base##_open() - Open debugfs entry for respective command
1022  * and event buffer.
1023  *
1024  * @inode: node for debug dir entry
1025  * @file: file handler
1026  *
1027  * Return: open status
1028  */
1029 #define GENERATE_DEBUG_STRUCTS(func_base)				\
1030 	static int debug_##func_base##_open(struct inode *inode,	\
1031 						struct file *file)	\
1032 	{								\
1033 		return single_open(file, debug_##func_base##_show,	\
1034 				inode->i_private);			\
1035 	}								\
1036 									\
1037 									\
1038 	static struct file_operations debug_##func_base##_ops = {	\
1039 		.open		= debug_##func_base##_open,		\
1040 		.read		= seq_read,				\
1041 		.llseek		= seq_lseek,				\
1042 		.write		= debug_##func_base##_write,		\
1043 		.release	= single_release,			\
1044 	};
1045 
1046 GENERATE_DEBUG_STRUCTS(wmi_command_log);
1047 GENERATE_DEBUG_STRUCTS(wmi_command_tx_cmp_log);
1048 GENERATE_DEBUG_STRUCTS(wmi_event_log);
1049 GENERATE_DEBUG_STRUCTS(wmi_rx_event_log);
1050 GENERATE_DEBUG_STRUCTS(wmi_mgmt_command_log);
1051 GENERATE_DEBUG_STRUCTS(wmi_mgmt_command_tx_cmp_log);
1052 GENERATE_DEBUG_STRUCTS(wmi_mgmt_event_log);
1053 GENERATE_DEBUG_STRUCTS(wmi_enable);
1054 GENERATE_DEBUG_STRUCTS(wmi_log_size);
1055 
1056 struct wmi_debugfs_info wmi_debugfs_infos[] = {
1057 	DEBUG_FOO(wmi_command_log),
1058 	DEBUG_FOO(wmi_command_tx_cmp_log),
1059 	DEBUG_FOO(wmi_event_log),
1060 	DEBUG_FOO(wmi_rx_event_log),
1061 	DEBUG_FOO(wmi_mgmt_command_log),
1062 	DEBUG_FOO(wmi_mgmt_command_tx_cmp_log),
1063 	DEBUG_FOO(wmi_mgmt_event_log),
1064 	DEBUG_FOO(wmi_enable),
1065 	DEBUG_FOO(wmi_log_size),
1066 };
1067 
1068 #define NUM_DEBUG_INFOS (sizeof(wmi_debugfs_infos) /			\
1069 		sizeof(wmi_debugfs_infos[0]))
1070 
1071 /**
1072  * wmi_debugfs_create() - Create debug_fs entry for wmi logging.
1073  *
1074  * @wmi_handle: wmi handle
1075  * @par_entry: debug directory entry
1076  * @id: Index to debug info data array
1077  *
1078  * Return: none
1079  */
1080 static void wmi_debugfs_create(wmi_unified_t wmi_handle,
1081 		struct dentry *par_entry, int id)
1082 {
1083 	int i;
1084 
1085 	if (par_entry == NULL || (id < 0) || (id >= MAX_WMI_INSTANCES))
1086 		goto out;
1087 
1088 	for (i = 0; i < NUM_DEBUG_INFOS; ++i) {
1089 
1090 		wmi_debugfs_infos[i].de[id] = debugfs_create_file(
1091 				wmi_debugfs_infos[i].name, 0644, par_entry,
1092 				wmi_handle, wmi_debugfs_infos[i].ops);
1093 
1094 		if (wmi_debugfs_infos[i].de[id] == NULL) {
1095 			qdf_print("%s: debug Entry creation failed!\n",
1096 					__func__);
1097 			goto out;
1098 		}
1099 	}
1100 
1101 	return;
1102 
1103 out:
1104 	qdf_print("%s: debug Entry creation failed!\n", __func__);
1105 	wmi_log_buffer_free(wmi_handle);
1106 	return;
1107 }
1108 
1109 /**
1110  * wmi_debugfs_remove() - Remove debugfs entry for wmi logging.
1111  * @wmi_handle: wmi handle
1112  * @dentry: debugfs directory entry
1113  * @id: Index to debug info data array
1114  *
1115  * Return: none
1116  */
1117 static void wmi_debugfs_remove(wmi_unified_t wmi_handle)
1118 {
1119 	int i;
1120 	struct dentry *dentry = wmi_handle->log_info.wmi_log_debugfs_dir;
1121 	int id;
1122 
1123 	if (!wmi_handle->log_info.wmi_instance_id)
1124 		return;
1125 
1126 	id = wmi_handle->log_info.wmi_instance_id - 1;
1127 	if (dentry && (!(id < 0) || (id >= MAX_WMI_INSTANCES))) {
1128 		for (i = 0; i < NUM_DEBUG_INFOS; ++i) {
1129 			if (wmi_debugfs_infos[i].de[id])
1130 				wmi_debugfs_infos[i].de[id] = NULL;
1131 		}
1132 	}
1133 
1134 	if (dentry)
1135 		debugfs_remove_recursive(dentry);
1136 
1137 	if (wmi_handle->log_info.wmi_instance_id)
1138 		wmi_handle->log_info.wmi_instance_id--;
1139 }
1140 
1141 /**
1142  * wmi_debugfs_init() - debugfs functions to create debugfs directory and to
1143  * create debugfs enteries.
1144  *
1145  * @h: wmi handler
1146  *
1147  * Return: init status
1148  */
1149 static QDF_STATUS wmi_debugfs_init(wmi_unified_t wmi_handle)
1150 {
1151 	int wmi_index = wmi_handle->log_info.wmi_instance_id;
1152 
1153 	if (wmi_index < MAX_WMI_INSTANCES) {
1154 		wmi_handle->log_info.wmi_log_debugfs_dir =
1155 			debugfs_create_dir(debugfs_dir[wmi_index], NULL);
1156 
1157 		if (!wmi_handle->log_info.wmi_log_debugfs_dir) {
1158 			qdf_print("error while creating debugfs dir for %s\n",
1159 				  debugfs_dir[wmi_index]);
1160 			return QDF_STATUS_E_FAILURE;
1161 		}
1162 
1163 		wmi_debugfs_create(wmi_handle,
1164 				   wmi_handle->log_info.wmi_log_debugfs_dir,
1165 				   wmi_index);
1166 		wmi_handle->log_info.wmi_instance_id++;
1167 	}
1168 
1169 	return QDF_STATUS_SUCCESS;
1170 }
1171 
1172 /**
1173  * wmi_mgmt_cmd_record() - Wrapper function for mgmt command logging macro
1174  *
1175  * @wmi_handle: wmi handle
1176  * @cmd: mgmt command
1177  * @header: pointer to 802.11 header
1178  * @vdev_id: vdev id
1179  * @chanfreq: channel frequency
1180  *
1181  * Return: none
1182  */
1183 void wmi_mgmt_cmd_record(wmi_unified_t wmi_handle, uint32_t cmd,
1184 			void *header, uint32_t vdev_id, uint32_t chanfreq)
1185 {
1186 
1187 	uint32_t data[CUSTOM_MGMT_CMD_DATA_SIZE];
1188 
1189 	data[0] = ((struct wmi_command_header *)header)->type;
1190 	data[1] = ((struct wmi_command_header *)header)->sub_type;
1191 	data[2] = vdev_id;
1192 	data[3] = chanfreq;
1193 
1194 	qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
1195 
1196 	WMI_MGMT_COMMAND_RECORD(wmi_handle, cmd, (uint8_t *)data);
1197 
1198 	qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
1199 }
1200 #else
1201 /**
1202  * wmi_debugfs_remove() - Remove debugfs entry for wmi logging.
1203  * @wmi_handle: wmi handle
1204  * @dentry: debugfs directory entry
1205  * @id: Index to debug info data array
1206  *
1207  * Return: none
1208  */
1209 static void wmi_debugfs_remove(wmi_unified_t wmi_handle) { }
1210 void wmi_mgmt_cmd_record(wmi_unified_t wmi_handle, uint32_t cmd,
1211 			void *header, uint32_t vdev_id, uint32_t chanfreq) { }
1212 static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle) { }
1213 #endif /*WMI_INTERFACE_EVENT_LOGGING */
1214 qdf_export_symbol(wmi_mgmt_cmd_record);
1215 
1216 int wmi_get_host_credits(wmi_unified_t wmi_handle);
1217 /* WMI buffer APIs */
1218 
1219 #ifdef MEMORY_DEBUG
1220 wmi_buf_t
1221 wmi_buf_alloc_debug(wmi_unified_t wmi_handle, uint16_t len, uint8_t *file_name,
1222 			uint32_t line_num)
1223 {
1224 	wmi_buf_t wmi_buf;
1225 
1226 	if (roundup(len + WMI_MIN_HEAD_ROOM, 4) > wmi_handle->max_msg_len) {
1227 		QDF_ASSERT(0);
1228 		return NULL;
1229 	}
1230 
1231 	wmi_buf = qdf_nbuf_alloc_debug(NULL,
1232 					roundup(len + WMI_MIN_HEAD_ROOM, 4),
1233 					WMI_MIN_HEAD_ROOM, 4, false, file_name,
1234 					line_num);
1235 
1236 	if (!wmi_buf)
1237 		return NULL;
1238 
1239 	/* Clear the wmi buffer */
1240 	OS_MEMZERO(qdf_nbuf_data(wmi_buf), len);
1241 
1242 	/*
1243 	 * Set the length of the buffer to match the allocation size.
1244 	 */
1245 	qdf_nbuf_set_pktlen(wmi_buf, len);
1246 
1247 	return wmi_buf;
1248 }
1249 qdf_export_symbol(wmi_buf_alloc_debug);
1250 
1251 void wmi_buf_free(wmi_buf_t net_buf)
1252 {
1253 	qdf_nbuf_free(net_buf);
1254 }
1255 qdf_export_symbol(wmi_buf_free);
1256 #else
1257 wmi_buf_t wmi_buf_alloc(wmi_unified_t wmi_handle, uint16_t len)
1258 {
1259 	wmi_buf_t wmi_buf;
1260 
1261 	if (roundup(len + WMI_MIN_HEAD_ROOM, 4) > wmi_handle->max_msg_len) {
1262 		QDF_ASSERT(0);
1263 		return NULL;
1264 	}
1265 
1266 	wmi_buf = qdf_nbuf_alloc(NULL, roundup(len + WMI_MIN_HEAD_ROOM, 4),
1267 				WMI_MIN_HEAD_ROOM, 4, false);
1268 	if (!wmi_buf)
1269 		return NULL;
1270 
1271 	/* Clear the wmi buffer */
1272 	OS_MEMZERO(qdf_nbuf_data(wmi_buf), len);
1273 
1274 	/*
1275 	 * Set the length of the buffer to match the allocation size.
1276 	 */
1277 	qdf_nbuf_set_pktlen(wmi_buf, len);
1278 	return wmi_buf;
1279 }
1280 qdf_export_symbol(wmi_buf_alloc);
1281 
1282 void wmi_buf_free(wmi_buf_t net_buf)
1283 {
1284 	qdf_nbuf_free(net_buf);
1285 }
1286 qdf_export_symbol(wmi_buf_free);
1287 #endif
1288 
1289 /**
1290  * wmi_get_max_msg_len() - get maximum WMI message length
1291  * @wmi_handle: WMI handle.
1292  *
1293  * This function returns the maximum WMI message length
1294  *
1295  * Return: maximum WMI message length
1296  */
1297 uint16_t wmi_get_max_msg_len(wmi_unified_t wmi_handle)
1298 {
1299 	return wmi_handle->max_msg_len - WMI_MIN_HEAD_ROOM;
1300 }
1301 qdf_export_symbol(wmi_get_max_msg_len);
1302 
1303 #ifndef WMI_CMD_STRINGS
1304 static uint8_t *wmi_id_to_name(uint32_t wmi_command)
1305 {
1306 	return "Invalid WMI cmd";
1307 }
1308 
1309 #endif
1310 
1311 #ifdef CONFIG_MCL
1312 static inline void wmi_log_cmd_id(uint32_t cmd_id, uint32_t tag)
1313 {
1314 	WMI_LOGD("Send WMI command:%s command_id:%d htc_tag:%d\n",
1315 		 wmi_id_to_name(cmd_id), cmd_id, tag);
1316 }
1317 
1318 /**
1319  * wmi_is_pm_resume_cmd() - check if a cmd is part of the resume sequence
1320  * @cmd_id: command to check
1321  *
1322  * Return: true if the command is part of the resume sequence.
1323  */
1324 static bool wmi_is_pm_resume_cmd(uint32_t cmd_id)
1325 {
1326 	switch (cmd_id) {
1327 	case WMI_WOW_HOSTWAKEUP_FROM_SLEEP_CMDID:
1328 	case WMI_PDEV_RESUME_CMDID:
1329 		return true;
1330 
1331 	default:
1332 		return false;
1333 	}
1334 }
1335 #else
1336 static bool wmi_is_pm_resume_cmd(uint32_t cmd_id)
1337 {
1338 	return false;
1339 }
1340 #endif
1341 
1342 /**
1343  * wmi_unified_cmd_send() - WMI command API
1344  * @wmi_handle: handle to wmi
1345  * @buf: wmi buf
1346  * @len: wmi buffer length
1347  * @cmd_id: wmi command id
1348  *
1349  * Note, it is NOT safe to access buf after calling this function!
1350  *
1351  * Return: 0 on success
1352  */
1353 QDF_STATUS wmi_unified_cmd_send(wmi_unified_t wmi_handle, wmi_buf_t buf,
1354 				uint32_t len, uint32_t cmd_id)
1355 {
1356 	HTC_PACKET *pkt;
1357 	QDF_STATUS status;
1358 	uint16_t htc_tag = 0;
1359 
1360 	if (wmi_get_runtime_pm_inprogress(wmi_handle)) {
1361 		htc_tag =
1362 			(A_UINT16)wmi_handle->ops->wmi_set_htc_tx_tag(
1363 						wmi_handle, buf, cmd_id);
1364 	} else if (qdf_atomic_read(&wmi_handle->is_target_suspended) &&
1365 		(!wmi_is_pm_resume_cmd(cmd_id))) {
1366 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1367 				  "%s: Target is suspended", __func__);
1368 		QDF_ASSERT(0);
1369 		return QDF_STATUS_E_BUSY;
1370 	}
1371 	if (wmi_handle->wmi_stopinprogress) {
1372 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1373 			"WMI  stop in progress\n");
1374 		return QDF_STATUS_E_INVAL;
1375 	}
1376 
1377 #ifndef WMI_NON_TLV_SUPPORT
1378 	/* Do sanity check on the TLV parameter structure */
1379 	if (wmi_handle->target_type == WMI_TLV_TARGET) {
1380 		void *buf_ptr = (void *)qdf_nbuf_data(buf);
1381 
1382 		if (wmi_handle->ops->wmi_check_command_params(NULL, buf_ptr, len, cmd_id)
1383 			!= 0) {
1384 			QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1385 			"\nERROR: %s: Invalid WMI Param Buffer for Cmd:%d",
1386 				__func__, cmd_id);
1387 			return QDF_STATUS_E_INVAL;
1388 		}
1389 	}
1390 #endif
1391 
1392 	if (qdf_nbuf_push_head(buf, sizeof(WMI_CMD_HDR)) == NULL) {
1393 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1394 			 "%s, Failed to send cmd %x, no memory",
1395 			 __func__, cmd_id);
1396 		return QDF_STATUS_E_NOMEM;
1397 	}
1398 
1399 	WMI_SET_FIELD(qdf_nbuf_data(buf), WMI_CMD_HDR, COMMANDID, cmd_id);
1400 
1401 	qdf_atomic_inc(&wmi_handle->pending_cmds);
1402 	if (qdf_atomic_read(&wmi_handle->pending_cmds) >= WMI_MAX_CMDS) {
1403 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1404 		    "\n%s: hostcredits = %d", __func__,
1405 		wmi_get_host_credits(wmi_handle));
1406 		htc_dump_counter_info(wmi_handle->htc_handle);
1407 		qdf_atomic_dec(&wmi_handle->pending_cmds);
1408 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1409 			"%s: MAX %d WMI Pending cmds reached.", __func__,
1410 			WMI_MAX_CMDS);
1411 		QDF_BUG(0);
1412 		return QDF_STATUS_E_BUSY;
1413 	}
1414 
1415 	pkt = qdf_mem_malloc(sizeof(*pkt));
1416 	if (!pkt) {
1417 		qdf_atomic_dec(&wmi_handle->pending_cmds);
1418 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1419 			 "%s, Failed to alloc htc packet %x, no memory",
1420 			 __func__, cmd_id);
1421 		return QDF_STATUS_E_NOMEM;
1422 	}
1423 
1424 	SET_HTC_PACKET_INFO_TX(pkt,
1425 			       NULL,
1426 			       qdf_nbuf_data(buf), len + sizeof(WMI_CMD_HDR),
1427 			       wmi_handle->wmi_endpoint_id, htc_tag);
1428 
1429 	SET_HTC_PACKET_NET_BUF_CONTEXT(pkt, buf);
1430 #ifdef CONFIG_MCL
1431 	wmi_log_cmd_id(cmd_id, htc_tag);
1432 #endif
1433 
1434 #ifdef WMI_INTERFACE_EVENT_LOGGING
1435 	if (wmi_handle->log_info.wmi_logging_enable) {
1436 		qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
1437 		/*
1438 		 * Record 16 bytes of WMI cmd data -
1439 		 * exclude TLV and WMI headers
1440 		 *
1441 		 * WMI mgmt command already recorded in wmi_mgmt_cmd_record
1442 		 */
1443 		if (wmi_handle->ops->is_management_record(cmd_id) == false) {
1444 			WMI_COMMAND_RECORD(wmi_handle, cmd_id,
1445 					qdf_nbuf_data(buf) +
1446 			 wmi_handle->log_info.buf_offset_command);
1447 		}
1448 		qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
1449 	}
1450 #endif
1451 
1452 	status = htc_send_pkt(wmi_handle->htc_handle, pkt);
1453 
1454 	if (QDF_STATUS_SUCCESS != status) {
1455 		qdf_atomic_dec(&wmi_handle->pending_cmds);
1456 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1457 		   "%s %d, htc_send_pkt failed", __func__, __LINE__);
1458 		qdf_mem_free(pkt);
1459 		return status;
1460 	}
1461 
1462 	return QDF_STATUS_SUCCESS;
1463 }
1464 qdf_export_symbol(wmi_unified_cmd_send);
1465 
1466 /**
1467  * wmi_unified_get_event_handler_ix() - gives event handler's index
1468  * @wmi_handle: handle to wmi
1469  * @event_id: wmi  event id
1470  *
1471  * Return: event handler's index
1472  */
1473 static int wmi_unified_get_event_handler_ix(wmi_unified_t wmi_handle,
1474 					    uint32_t event_id)
1475 {
1476 	uint32_t idx = 0;
1477 	int32_t invalid_idx = -1;
1478 	struct wmi_soc *soc = wmi_handle->soc;
1479 
1480 	for (idx = 0; (idx < soc->max_event_idx &&
1481 		       idx < WMI_UNIFIED_MAX_EVENT); ++idx) {
1482 		if (wmi_handle->event_id[idx] == event_id &&
1483 		    wmi_handle->event_handler[idx] != NULL) {
1484 			return idx;
1485 		}
1486 	}
1487 
1488 	return invalid_idx;
1489 }
1490 
1491 /**
1492  * wmi_unified_register_event() - register wmi event handler
1493  * @wmi_handle: handle to wmi
1494  * @event_id: wmi event id
1495  * @handler_func: wmi event handler function
1496  *
1497  * Return: 0 on success
1498  */
1499 int wmi_unified_register_event(wmi_unified_t wmi_handle,
1500 				       uint32_t event_id,
1501 				       wmi_unified_event_handler handler_func)
1502 {
1503 	uint32_t idx = 0;
1504 	uint32_t evt_id;
1505 	struct wmi_soc *soc = wmi_handle->soc;
1506 
1507 	if (event_id >= wmi_events_max ||
1508 		wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
1509 		qdf_print("%s: Event id %d is unavailable\n",
1510 				 __func__, event_id);
1511 		return QDF_STATUS_E_FAILURE;
1512 	}
1513 	evt_id = wmi_handle->wmi_events[event_id];
1514 	if (wmi_unified_get_event_handler_ix(wmi_handle, evt_id) != -1) {
1515 		qdf_print("%s : event handler already registered 0x%x\n",
1516 		       __func__, evt_id);
1517 		return QDF_STATUS_E_FAILURE;
1518 	}
1519 	if (soc->max_event_idx == WMI_UNIFIED_MAX_EVENT) {
1520 		qdf_print("%s : no more event handlers 0x%x\n",
1521 		       __func__, evt_id);
1522 		return QDF_STATUS_E_FAILURE;
1523 	}
1524 	idx = soc->max_event_idx;
1525 	wmi_handle->event_handler[idx] = handler_func;
1526 	wmi_handle->event_id[idx] = evt_id;
1527 	qdf_spin_lock_bh(&soc->ctx_lock);
1528 	wmi_handle->ctx[idx] = WMI_RX_UMAC_CTX;
1529 	qdf_spin_unlock_bh(&soc->ctx_lock);
1530 	soc->max_event_idx++;
1531 
1532 	return 0;
1533 }
1534 
1535 /**
1536  * wmi_unified_register_event_handler() - register wmi event handler
1537  * @wmi_handle: handle to wmi
1538  * @event_id: wmi event id
1539  * @handler_func: wmi event handler function
1540  * @rx_ctx: rx execution context for wmi rx events
1541  *
1542  * This API is to support legacy requirements. Will be deprecated in future.
1543  * Return: 0 on success
1544  */
1545 int wmi_unified_register_event_handler(wmi_unified_t wmi_handle,
1546 				       wmi_conv_event_id event_id,
1547 				       wmi_unified_event_handler handler_func,
1548 				       uint8_t rx_ctx)
1549 {
1550 	uint32_t idx = 0;
1551 	uint32_t evt_id;
1552 	struct wmi_soc *soc = wmi_handle->soc;
1553 
1554 	if (event_id >= wmi_events_max ||
1555 		wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
1556 		qdf_print("%s: Event id %d is unavailable\n",
1557 				 __func__, event_id);
1558 		return QDF_STATUS_E_FAILURE;
1559 	}
1560 	evt_id = wmi_handle->wmi_events[event_id];
1561 
1562 	if (wmi_unified_get_event_handler_ix(wmi_handle, evt_id) != -1) {
1563 		qdf_print("%s : event handler already registered 0x%x\n",
1564 		       __func__, evt_id);
1565 		return QDF_STATUS_E_FAILURE;
1566 	}
1567 	if (soc->max_event_idx == WMI_UNIFIED_MAX_EVENT) {
1568 		qdf_print("%s : no more event handlers 0x%x\n",
1569 		       __func__, evt_id);
1570 		return QDF_STATUS_E_FAILURE;
1571 	}
1572 	QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_DEBUG,
1573 			"Registered event handler for event 0x%8x\n", evt_id);
1574 	idx = soc->max_event_idx;
1575 	wmi_handle->event_handler[idx] = handler_func;
1576 	wmi_handle->event_id[idx] = evt_id;
1577 	qdf_spin_lock_bh(&soc->ctx_lock);
1578 	wmi_handle->ctx[idx] = rx_ctx;
1579 	qdf_spin_unlock_bh(&soc->ctx_lock);
1580 	soc->max_event_idx++;
1581 
1582 	return 0;
1583 }
1584 qdf_export_symbol(wmi_unified_register_event_handler);
1585 
1586 /**
1587  * wmi_unified_unregister_event() - unregister wmi event handler
1588  * @wmi_handle: handle to wmi
1589  * @event_id: wmi event id
1590  *
1591  * Return: 0 on success
1592  */
1593 int wmi_unified_unregister_event(wmi_unified_t wmi_handle,
1594 					 uint32_t event_id)
1595 {
1596 	uint32_t idx = 0;
1597 	uint32_t evt_id;
1598 	struct wmi_soc *soc = wmi_handle->soc;
1599 
1600 	if (event_id >= wmi_events_max ||
1601 		wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
1602 		qdf_print("%s: Event id %d is unavailable\n",
1603 				 __func__, event_id);
1604 		return QDF_STATUS_E_FAILURE;
1605 	}
1606 	evt_id = wmi_handle->wmi_events[event_id];
1607 
1608 	idx = wmi_unified_get_event_handler_ix(wmi_handle, evt_id);
1609 	if (idx == -1) {
1610 		qdf_print("%s : event handler is not registered: evt id 0x%x\n",
1611 		       __func__, evt_id);
1612 		return QDF_STATUS_E_FAILURE;
1613 	}
1614 	wmi_handle->event_handler[idx] = NULL;
1615 	wmi_handle->event_id[idx] = 0;
1616 	--soc->max_event_idx;
1617 	wmi_handle->event_handler[idx] =
1618 		wmi_handle->event_handler[soc->max_event_idx];
1619 	wmi_handle->event_id[idx] =
1620 		wmi_handle->event_id[soc->max_event_idx];
1621 
1622 	return 0;
1623 }
1624 
1625 /**
1626  * wmi_unified_unregister_event_handler() - unregister wmi event handler
1627  * @wmi_handle: handle to wmi
1628  * @event_id: wmi event id
1629  *
1630  * Return: 0 on success
1631  */
1632 int wmi_unified_unregister_event_handler(wmi_unified_t wmi_handle,
1633 					 wmi_conv_event_id event_id)
1634 {
1635 	uint32_t idx = 0;
1636 	uint32_t evt_id;
1637 	struct wmi_soc *soc = wmi_handle->soc;
1638 
1639 	if (event_id >= wmi_events_max ||
1640 		wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
1641 		qdf_print("%s: Event id %d is unavailable\n",
1642 				 __func__, event_id);
1643 		return QDF_STATUS_E_FAILURE;
1644 	}
1645 	evt_id = wmi_handle->wmi_events[event_id];
1646 
1647 	idx = wmi_unified_get_event_handler_ix(wmi_handle, evt_id);
1648 	if (idx == -1) {
1649 		qdf_print("%s : event handler is not registered: evt id 0x%x\n",
1650 		       __func__, evt_id);
1651 		return QDF_STATUS_E_FAILURE;
1652 	}
1653 	wmi_handle->event_handler[idx] = NULL;
1654 	wmi_handle->event_id[idx] = 0;
1655 	--soc->max_event_idx;
1656 	wmi_handle->event_handler[idx] =
1657 		wmi_handle->event_handler[soc->max_event_idx];
1658 	wmi_handle->event_id[idx] =
1659 		wmi_handle->event_id[soc->max_event_idx];
1660 
1661 	return 0;
1662 }
1663 qdf_export_symbol(wmi_unified_unregister_event_handler);
1664 
1665 /**
1666  * wmi_process_fw_event_default_ctx() - process in default caller context
1667  * @wmi_handle: handle to wmi
1668  * @htc_packet: pointer to htc packet
1669  * @exec_ctx: execution context for wmi fw event
1670  *
1671  * Event process by below function will be in default caller context.
1672  * wmi internally provides rx work thread processing context.
1673  *
1674  * Return: none
1675  */
1676 static void wmi_process_fw_event_default_ctx(struct wmi_unified *wmi_handle,
1677 		       HTC_PACKET *htc_packet, uint8_t exec_ctx)
1678 {
1679 	wmi_buf_t evt_buf;
1680 	evt_buf = (wmi_buf_t) htc_packet->pPktContext;
1681 
1682 #ifndef CONFIG_MCL
1683 	wmi_handle->rx_ops.wma_process_fw_event_handler_cbk
1684 		(wmi_handle->scn_handle, evt_buf, exec_ctx);
1685 #else
1686 	wmi_handle->rx_ops.wma_process_fw_event_handler_cbk(wmi_handle,
1687 					 evt_buf, exec_ctx);
1688 #endif
1689 
1690 	return;
1691 }
1692 
1693 /**
1694  * wmi_process_fw_event_worker_thread_ctx() - process in worker thread context
1695  * @wmi_handle: handle to wmi
1696  * @htc_packet: pointer to htc packet
1697  *
1698  * Event process by below function will be in worker thread context.
1699  * Use this method for events which are not critical and not
1700  * handled in protocol stack.
1701  *
1702  * Return: none
1703  */
1704 static void wmi_process_fw_event_worker_thread_ctx
1705 		(struct wmi_unified *wmi_handle, HTC_PACKET *htc_packet)
1706 {
1707 	wmi_buf_t evt_buf;
1708 
1709 	evt_buf = (wmi_buf_t) htc_packet->pPktContext;
1710 
1711 	qdf_spin_lock_bh(&wmi_handle->eventq_lock);
1712 	qdf_nbuf_queue_add(&wmi_handle->event_queue, evt_buf);
1713 	qdf_spin_unlock_bh(&wmi_handle->eventq_lock);
1714 	qdf_queue_work(0, wmi_handle->wmi_rx_work_queue,
1715 			&wmi_handle->rx_event_work);
1716 
1717 	return;
1718 }
1719 
1720 /**
1721  * wmi_get_pdev_ep: Get wmi handle based on endpoint
1722  * @soc: handle to wmi soc
1723  * @ep: endpoint id
1724  *
1725  * Return: none
1726  */
1727 static struct wmi_unified *wmi_get_pdev_ep(struct wmi_soc *soc,
1728 						HTC_ENDPOINT_ID ep)
1729 {
1730 	uint32_t i;
1731 
1732 	for (i = 0; i < WMI_MAX_RADIOS; i++)
1733 		if (soc->wmi_endpoint_id[i] == ep)
1734 			break;
1735 
1736 	if (i == WMI_MAX_RADIOS)
1737 		return NULL;
1738 
1739 	return soc->wmi_pdev[i];
1740 }
1741 
1742 /**
1743  * wmi_control_rx() - process fw events callbacks
1744  * @ctx: handle to wmi
1745  * @htc_packet: pointer to htc packet
1746  *
1747  * Return: none
1748  */
1749 static void wmi_control_rx(void *ctx, HTC_PACKET *htc_packet)
1750 {
1751 	struct wmi_soc *soc = (struct wmi_soc *) ctx;
1752 	struct wmi_unified *wmi_handle;
1753 	wmi_buf_t evt_buf;
1754 	uint32_t id;
1755 	uint32_t idx = 0;
1756 	enum wmi_rx_exec_ctx exec_ctx;
1757 
1758 	evt_buf = (wmi_buf_t) htc_packet->pPktContext;
1759 
1760 	wmi_handle = wmi_get_pdev_ep(soc, htc_packet->Endpoint);
1761 	if (wmi_handle == NULL) {
1762 		qdf_print
1763 		("%s :unable to get wmi_handle to Endpoint %d\n",
1764 			__func__, htc_packet->Endpoint);
1765 		qdf_nbuf_free(evt_buf);
1766 		return;
1767 	}
1768 
1769 	id = WMI_GET_FIELD(qdf_nbuf_data(evt_buf), WMI_CMD_HDR, COMMANDID);
1770 	idx = wmi_unified_get_event_handler_ix(wmi_handle, id);
1771 	if (qdf_unlikely(idx == A_ERROR)) {
1772 		WMI_LOGD("%s :event handler is not registered: event id 0x%x\n",
1773 				 __func__, id);
1774 		qdf_nbuf_free(evt_buf);
1775 		return;
1776 	}
1777 	qdf_spin_lock_bh(&soc->ctx_lock);
1778 	exec_ctx = wmi_handle->ctx[idx];
1779 	qdf_spin_unlock_bh(&soc->ctx_lock);
1780 
1781 #ifdef WMI_INTERFACE_EVENT_LOGGING
1782 	if (wmi_handle->log_info.wmi_logging_enable) {
1783 		uint8_t *data;
1784 		data = qdf_nbuf_data(evt_buf);
1785 
1786 		qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
1787 		/* Exclude 4 bytes of TLV header */
1788 		WMI_RX_EVENT_RECORD(wmi_handle, id, data +
1789 				wmi_handle->log_info.buf_offset_event);
1790 		qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
1791 	}
1792 #endif
1793 
1794 	if (exec_ctx == WMI_RX_WORK_CTX) {
1795 		wmi_process_fw_event_worker_thread_ctx
1796 					(wmi_handle, htc_packet);
1797 	} else if (exec_ctx > WMI_RX_WORK_CTX) {
1798 		wmi_process_fw_event_default_ctx
1799 					(wmi_handle, htc_packet, exec_ctx);
1800 	} else {
1801 		qdf_print("%s :Invalid event context %d\n", __func__, exec_ctx);
1802 		qdf_nbuf_free(evt_buf);
1803 	}
1804 
1805 }
1806 
1807 /**
1808  * wmi_process_fw_event() - process any fw event
1809  * @wmi_handle: wmi handle
1810  * @evt_buf: fw event buffer
1811  *
1812  * This function process fw event in caller context
1813  *
1814  * Return: none
1815  */
1816 void wmi_process_fw_event(struct wmi_unified *wmi_handle, wmi_buf_t evt_buf)
1817 {
1818 	__wmi_control_rx(wmi_handle, evt_buf);
1819 }
1820 
1821 /**
1822  * __wmi_control_rx() - process serialize wmi event callback
1823  * @wmi_handle: wmi handle
1824  * @evt_buf: fw event buffer
1825  *
1826  * Return: none
1827  */
1828 void __wmi_control_rx(struct wmi_unified *wmi_handle, wmi_buf_t evt_buf)
1829 {
1830 	uint32_t id;
1831 	uint8_t *data;
1832 	uint32_t len;
1833 	void *wmi_cmd_struct_ptr = NULL;
1834 #ifndef WMI_NON_TLV_SUPPORT
1835 	int tlv_ok_status = 0;
1836 #endif
1837 	uint32_t idx = 0;
1838 
1839 	id = WMI_GET_FIELD(qdf_nbuf_data(evt_buf), WMI_CMD_HDR, COMMANDID);
1840 
1841 	if (qdf_nbuf_pull_head(evt_buf, sizeof(WMI_CMD_HDR)) == NULL)
1842 		goto end;
1843 
1844 	data = qdf_nbuf_data(evt_buf);
1845 	len = qdf_nbuf_len(evt_buf);
1846 
1847 #ifndef WMI_NON_TLV_SUPPORT
1848 	if (wmi_handle->target_type == WMI_TLV_TARGET) {
1849 		/* Validate and pad(if necessary) the TLVs */
1850 		tlv_ok_status =
1851 			wmi_handle->ops->wmi_check_and_pad_event(wmi_handle->scn_handle,
1852 							data, len, id,
1853 							&wmi_cmd_struct_ptr);
1854 		if (tlv_ok_status != 0) {
1855 			QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1856 				"%s: Error: id=0x%d, wmitlv check status=%d\n",
1857 				__func__, id, tlv_ok_status);
1858 			goto end;
1859 		}
1860 	}
1861 #endif
1862 
1863 	idx = wmi_unified_get_event_handler_ix(wmi_handle, id);
1864 	if (idx == A_ERROR) {
1865 		QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
1866 		   "%s : event handler is not registered: event id 0x%x\n",
1867 			__func__, id);
1868 		goto end;
1869 	}
1870 #ifdef WMI_INTERFACE_EVENT_LOGGING
1871 	if (wmi_handle->log_info.wmi_logging_enable) {
1872 		qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
1873 		/* Exclude 4 bytes of TLV header */
1874 		if (wmi_handle->ops->is_management_record(id)) {
1875 			WMI_MGMT_EVENT_RECORD(wmi_handle, id, data
1876 				+ wmi_handle->log_info.buf_offset_event);
1877 		} else {
1878 			WMI_EVENT_RECORD(wmi_handle, id, data +
1879 					wmi_handle->log_info.buf_offset_event);
1880 		}
1881 		qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
1882 	}
1883 #endif
1884 	/* Call the WMI registered event handler */
1885 	if (wmi_handle->target_type == WMI_TLV_TARGET)
1886 		wmi_handle->event_handler[idx] (wmi_handle->scn_handle,
1887 			wmi_cmd_struct_ptr, len);
1888 	else
1889 		wmi_handle->event_handler[idx] (wmi_handle->scn_handle,
1890 			data, len);
1891 
1892 end:
1893 	/* Free event buffer and allocated event tlv */
1894 #ifndef WMI_NON_TLV_SUPPORT
1895 	if (wmi_handle->target_type == WMI_TLV_TARGET)
1896 		wmi_handle->ops->wmi_free_allocated_event(id, &wmi_cmd_struct_ptr);
1897 #endif
1898 
1899 	qdf_nbuf_free(evt_buf);
1900 
1901 }
1902 
1903 #define WMI_WQ_WD_TIMEOUT (30 * 1000) /* 30s */
1904 
1905 static inline void wmi_workqueue_watchdog_warn(uint32_t msg_type_id)
1906 {
1907 	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
1908 		  "%s: Message type %x has exceeded its alloted time of %ds",
1909 		  __func__, msg_type_id, WMI_WQ_WD_TIMEOUT / 1000);
1910 }
1911 
1912 #ifdef CONFIG_SLUB_DEBUG_ON
1913 static void wmi_workqueue_watchdog_bite(void *arg)
1914 {
1915 	struct wmi_wq_dbg_info *info = arg;
1916 
1917 	wmi_workqueue_watchdog_warn(info->wd_msg_type_id);
1918 	qdf_print_thread_trace(info->task);
1919 
1920 	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
1921 		  "%s: Going down for WMI WQ Watchdog Bite!", __func__);
1922 	QDF_BUG(0);
1923 }
1924 #else
1925 static inline void wmi_workqueue_watchdog_bite(void *arg)
1926 {
1927 	struct wmi_wq_dbg_info *info = arg;
1928 
1929 	wmi_workqueue_watchdog_warn(info->wd_msg_type_id);
1930 }
1931 #endif
1932 
1933 /**
1934  * wmi_rx_event_work() - process rx event in rx work queue context
1935  * @arg: opaque pointer to wmi handle
1936  *
1937  * This function process any fw event to serialize it through rx worker thread.
1938  *
1939  * Return: none
1940  */
1941 static void wmi_rx_event_work(void *arg)
1942 {
1943 	wmi_buf_t buf;
1944 	struct wmi_unified *wmi = arg;
1945 	qdf_timer_t wd_timer;
1946 	struct wmi_wq_dbg_info info;
1947 
1948 	/* initialize WMI workqueue watchdog timer */
1949 	qdf_timer_init(NULL, &wd_timer, &wmi_workqueue_watchdog_bite,
1950 			&info, QDF_TIMER_TYPE_SW);
1951 	qdf_spin_lock_bh(&wmi->eventq_lock);
1952 	buf = qdf_nbuf_queue_remove(&wmi->event_queue);
1953 	qdf_spin_unlock_bh(&wmi->eventq_lock);
1954 	while (buf) {
1955 		qdf_timer_start(&wd_timer, WMI_WQ_WD_TIMEOUT);
1956 		info.wd_msg_type_id =
1957 		   WMI_GET_FIELD(qdf_nbuf_data(buf), WMI_CMD_HDR, COMMANDID);
1958 		info.wmi_wq = wmi->wmi_rx_work_queue;
1959 		info.task = qdf_get_current_task();
1960 		__wmi_control_rx(wmi, buf);
1961 		qdf_timer_stop(&wd_timer);
1962 		qdf_spin_lock_bh(&wmi->eventq_lock);
1963 		buf = qdf_nbuf_queue_remove(&wmi->event_queue);
1964 		qdf_spin_unlock_bh(&wmi->eventq_lock);
1965 	}
1966 	qdf_timer_free(&wd_timer);
1967 }
1968 
1969 #ifdef FEATURE_RUNTIME_PM
1970 /**
1971  * wmi_runtime_pm_init() - initialize runtime pm wmi variables
1972  * @wmi_handle: wmi context
1973  */
1974 static void wmi_runtime_pm_init(struct wmi_unified *wmi_handle)
1975 {
1976 	qdf_atomic_init(&wmi_handle->runtime_pm_inprogress);
1977 }
1978 
1979 /**
1980  * wmi_set_runtime_pm_inprogress() - set runtime pm progress flag
1981  * @wmi_handle: wmi context
1982  * @val: runtime pm progress flag
1983  */
1984 void wmi_set_runtime_pm_inprogress(wmi_unified_t wmi_handle, A_BOOL val)
1985 {
1986 	qdf_atomic_set(&wmi_handle->runtime_pm_inprogress, val);
1987 }
1988 
1989 /**
1990  * wmi_get_runtime_pm_inprogress() - get runtime pm progress flag
1991  * @wmi_handle: wmi context
1992  */
1993 inline bool wmi_get_runtime_pm_inprogress(wmi_unified_t wmi_handle)
1994 {
1995 	return qdf_atomic_read(&wmi_handle->runtime_pm_inprogress);
1996 }
1997 #else
1998 static void wmi_runtime_pm_init(struct wmi_unified *wmi_handle)
1999 {
2000 }
2001 #endif
2002 
2003 /**
2004  * wmi_unified_get_soc_handle: Get WMI SoC handle
2005  * @param wmi_handle: WMI context got from wmi_attach
2006  *
2007  * return: Pointer to Soc handle
2008  */
2009 void *wmi_unified_get_soc_handle(struct wmi_unified *wmi_handle)
2010 {
2011 	return wmi_handle->soc;
2012 }
2013 
2014 /**
2015  * wmi_interface_logging_init: Interface looging init
2016  * @param wmi_handle: Pointer to wmi handle object
2017  *
2018  * return: None
2019  */
2020 #ifdef WMI_INTERFACE_EVENT_LOGGING
2021 static inline void wmi_interface_logging_init(struct wmi_unified *wmi_handle)
2022 {
2023 	if (QDF_STATUS_SUCCESS == wmi_log_init(wmi_handle)) {
2024 		qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
2025 		wmi_debugfs_init(wmi_handle);
2026 	}
2027 }
2028 #else
2029 static inline void wmi_interface_logging_init(struct wmi_unified *wmi_handle)
2030 {
2031 }
2032 #endif
2033 
2034 /**
2035  * wmi_target_params_init: Target specific params init
2036  * @param wmi_soc: Pointer to wmi soc object
2037  * @param wmi_handle: Pointer to wmi handle object
2038  *
2039  * return: None
2040  */
2041 #ifndef CONFIG_MCL
2042 static inline void wmi_target_params_init(struct wmi_soc *soc,
2043 				struct wmi_unified *wmi_handle)
2044 {
2045 	wmi_handle->pdev_param = soc->pdev_param;
2046 	wmi_handle->vdev_param = soc->vdev_param;
2047 	wmi_handle->services = soc->services;
2048 }
2049 #else
2050 static inline void wmi_target_params_init(struct wmi_soc *soc,
2051 				struct wmi_unified *wmi_handle)
2052 {
2053 	wmi_handle->services = soc->services;
2054 }
2055 #endif
2056 
2057 /**
2058  * wmi_unified_get_pdev_handle: Get WMI SoC handle
2059  * @param wmi_soc: Pointer to wmi soc object
2060  * @param pdev_idx: pdev index
2061  *
2062  * return: Pointer to wmi handle or NULL on failure
2063  */
2064 void *wmi_unified_get_pdev_handle(struct wmi_soc *soc, uint32_t pdev_idx)
2065 {
2066 	struct wmi_unified *wmi_handle;
2067 
2068 	if (pdev_idx >= WMI_MAX_RADIOS)
2069 		return NULL;
2070 
2071 	if (soc->wmi_pdev[pdev_idx] == NULL) {
2072 		wmi_handle =
2073 			(struct wmi_unified *) qdf_mem_malloc(
2074 					sizeof(struct wmi_unified));
2075 		if (wmi_handle == NULL) {
2076 			qdf_print("allocation of wmi handle failed %zu\n",
2077 					sizeof(struct wmi_unified));
2078 			return NULL;
2079 		}
2080 		wmi_handle->scn_handle = soc->scn_handle;
2081 		wmi_handle->event_id = soc->event_id;
2082 		wmi_handle->event_handler = soc->event_handler;
2083 		wmi_handle->ctx = soc->ctx;
2084 		wmi_handle->ops = soc->ops;
2085 		qdf_spinlock_create(&wmi_handle->eventq_lock);
2086 		qdf_nbuf_queue_init(&wmi_handle->event_queue);
2087 
2088 		qdf_create_work(0, &wmi_handle->rx_event_work,
2089 				wmi_rx_event_work, wmi_handle);
2090 		wmi_handle->wmi_rx_work_queue =
2091 			qdf_create_workqueue("wmi_rx_event_work_queue");
2092 		if (NULL == wmi_handle->wmi_rx_work_queue) {
2093 			WMI_LOGE("failed to create wmi_rx_event_work_queue");
2094 			goto error;
2095 		}
2096 		wmi_handle->wmi_events = soc->wmi_events;
2097 		wmi_target_params_init(soc, wmi_handle);
2098 		wmi_interface_logging_init(wmi_handle);
2099 		qdf_atomic_init(&wmi_handle->pending_cmds);
2100 		qdf_atomic_init(&wmi_handle->is_target_suspended);
2101 		wmi_handle->target_type = soc->target_type;
2102 		wmi_handle->soc = soc;
2103 
2104 		soc->wmi_pdev[pdev_idx] = wmi_handle;
2105 	} else
2106 		wmi_handle = soc->wmi_pdev[pdev_idx];
2107 
2108 	wmi_handle->wmi_stopinprogress = 0;
2109 	wmi_handle->wmi_endpoint_id = soc->wmi_endpoint_id[pdev_idx];
2110 	wmi_handle->htc_handle = soc->htc_handle;
2111 	wmi_handle->max_msg_len = soc->max_msg_len[pdev_idx];
2112 
2113 	return wmi_handle;
2114 
2115 error:
2116 	qdf_mem_free(wmi_handle);
2117 
2118 	return NULL;
2119 }
2120 qdf_export_symbol(wmi_unified_get_pdev_handle);
2121 
2122 static void (*wmi_attach_register[WMI_MAX_TARGET_TYPE])(wmi_unified_t);
2123 
2124 void wmi_unified_register_module(enum wmi_target_type target_type,
2125 			void (*wmi_attach)(wmi_unified_t wmi_handle))
2126 {
2127 	if (target_type < WMI_MAX_TARGET_TYPE)
2128 		wmi_attach_register[target_type] = wmi_attach;
2129 
2130 	return;
2131 }
2132 qdf_export_symbol(wmi_unified_register_module);
2133 
2134 /**
2135  * wmi_unified_attach() -  attach for unified WMI
2136  * @scn_handle: handle to SCN
2137  * @osdev: OS device context
2138  * @target_type: TLV or not-TLV based target
2139  * @use_cookie: cookie based allocation enabled/disabled
2140  * @ops: umac rx callbacks
2141  * @psoc: objmgr psoc
2142  *
2143  * @Return: wmi handle.
2144  */
2145 void *wmi_unified_attach(void *scn_handle,
2146 			 osdev_t osdev, enum wmi_target_type target_type,
2147 			 bool use_cookie, struct wmi_rx_ops *rx_ops,
2148 			 struct wlan_objmgr_psoc *psoc)
2149 {
2150 	struct wmi_unified *wmi_handle;
2151 	struct wmi_soc *soc;
2152 
2153 	soc = (struct wmi_soc *) qdf_mem_malloc(sizeof(struct wmi_soc));
2154 	if (soc == NULL) {
2155 		qdf_print("Allocation of wmi_soc failed %zu\n",
2156 				sizeof(struct wmi_soc));
2157 		return NULL;
2158 	}
2159 
2160 	wmi_handle =
2161 		(struct wmi_unified *) qdf_mem_malloc(
2162 			sizeof(struct wmi_unified));
2163 	if (wmi_handle == NULL) {
2164 		qdf_mem_free(soc);
2165 		qdf_print("allocation of wmi handle failed %zu\n",
2166 			sizeof(struct wmi_unified));
2167 		return NULL;
2168 	}
2169 	wmi_handle->soc = soc;
2170 	wmi_handle->event_id = soc->event_id;
2171 	wmi_handle->event_handler = soc->event_handler;
2172 	wmi_handle->ctx = soc->ctx;
2173 	wmi_handle->wmi_events = soc->wmi_events;
2174 	wmi_target_params_init(soc, wmi_handle);
2175 	wmi_handle->scn_handle = scn_handle;
2176 	soc->scn_handle = scn_handle;
2177 	qdf_atomic_init(&wmi_handle->pending_cmds);
2178 	qdf_atomic_init(&wmi_handle->is_target_suspended);
2179 	wmi_runtime_pm_init(wmi_handle);
2180 	qdf_spinlock_create(&wmi_handle->eventq_lock);
2181 	qdf_nbuf_queue_init(&wmi_handle->event_queue);
2182 	qdf_create_work(0, &wmi_handle->rx_event_work,
2183 			wmi_rx_event_work, wmi_handle);
2184 	wmi_handle->wmi_rx_work_queue =
2185 		qdf_create_workqueue("wmi_rx_event_work_queue");
2186 	if (NULL == wmi_handle->wmi_rx_work_queue) {
2187 		WMI_LOGE("failed to create wmi_rx_event_work_queue");
2188 		goto error;
2189 	}
2190 	wmi_interface_logging_init(wmi_handle);
2191 	/* Attach mc_thread context processing function */
2192 	wmi_handle->rx_ops.wma_process_fw_event_handler_cbk =
2193 				rx_ops->wma_process_fw_event_handler_cbk;
2194 	wmi_handle->target_type = target_type;
2195 	soc->target_type = target_type;
2196 	if (wmi_attach_register[target_type]) {
2197 		wmi_attach_register[target_type](wmi_handle);
2198 	} else {
2199 		WMI_LOGE("wmi attach is not registered");
2200 		goto error;
2201 	}
2202 	/* Assign target cookie capablity */
2203 	wmi_handle->use_cookie = use_cookie;
2204 	wmi_handle->osdev = osdev;
2205 	wmi_handle->wmi_stopinprogress = 0;
2206 	/* Increase the ref count once refcount infra is present */
2207 	soc->wmi_psoc = psoc;
2208 	qdf_spinlock_create(&soc->ctx_lock);
2209 
2210 	soc->ops = wmi_handle->ops;
2211 	soc->wmi_pdev[0] = wmi_handle;
2212 
2213 	return wmi_handle;
2214 
2215 error:
2216 	qdf_mem_free(soc);
2217 	qdf_mem_free(wmi_handle);
2218 
2219 	return NULL;
2220 }
2221 
2222 /**
2223  * wmi_unified_detach() -  detach for unified WMI
2224  *
2225  * @wmi_handle  : handle to wmi.
2226  *
2227  * @Return: none.
2228  */
2229 void wmi_unified_detach(struct wmi_unified *wmi_handle)
2230 {
2231 	wmi_buf_t buf;
2232 	struct wmi_soc *soc;
2233 	uint8_t i;
2234 
2235 	soc = wmi_handle->soc;
2236 	for (i = 0; i < WMI_MAX_RADIOS; i++) {
2237 		if (soc->wmi_pdev[i]) {
2238 			qdf_flush_workqueue(0,
2239 				soc->wmi_pdev[i]->wmi_rx_work_queue);
2240 			qdf_destroy_workqueue(0,
2241 				soc->wmi_pdev[i]->wmi_rx_work_queue);
2242 			wmi_debugfs_remove(soc->wmi_pdev[i]);
2243 			buf = qdf_nbuf_queue_remove(
2244 					&soc->wmi_pdev[i]->event_queue);
2245 			while (buf) {
2246 				qdf_nbuf_free(buf);
2247 				buf = qdf_nbuf_queue_remove(
2248 						&soc->wmi_pdev[i]->event_queue);
2249 			}
2250 
2251 			wmi_log_buffer_free(soc->wmi_pdev[i]);
2252 
2253 			/* Free events logs list */
2254 			if (soc->wmi_pdev[i]->events_logs_list)
2255 				qdf_mem_free(
2256 					soc->wmi_pdev[i]->events_logs_list);
2257 
2258 			qdf_spinlock_destroy(&soc->wmi_pdev[i]->eventq_lock);
2259 			qdf_mem_free(soc->wmi_pdev[i]);
2260 		}
2261 	}
2262 	qdf_spinlock_destroy(&soc->ctx_lock);
2263 
2264 	if (soc->wmi_service_bitmap) {
2265 		qdf_mem_free(soc->wmi_service_bitmap);
2266 		soc->wmi_service_bitmap = NULL;
2267 	}
2268 
2269 	if (soc->wmi_ext_service_bitmap) {
2270 		qdf_mem_free(soc->wmi_ext_service_bitmap);
2271 		soc->wmi_ext_service_bitmap = NULL;
2272 	}
2273 
2274 	/* Decrease the ref count once refcount infra is present */
2275 	soc->wmi_psoc = NULL;
2276 	qdf_mem_free(soc);
2277 }
2278 
2279 /**
2280  * wmi_unified_remove_work() - detach for WMI work
2281  * @wmi_handle: handle to WMI
2282  *
2283  * A function that does not fully detach WMI, but just remove work
2284  * queue items associated with it. This is used to make sure that
2285  * before any other processing code that may destroy related contexts
2286  * (HTC, etc), work queue processing on WMI has already been stopped.
2287  *
2288  * Return: None
2289  */
2290 void
2291 wmi_unified_remove_work(struct wmi_unified *wmi_handle)
2292 {
2293 	wmi_buf_t buf;
2294 
2295 	qdf_flush_workqueue(0, wmi_handle->wmi_rx_work_queue);
2296 	qdf_spin_lock_bh(&wmi_handle->eventq_lock);
2297 	buf = qdf_nbuf_queue_remove(&wmi_handle->event_queue);
2298 	while (buf) {
2299 		qdf_nbuf_free(buf);
2300 		buf = qdf_nbuf_queue_remove(&wmi_handle->event_queue);
2301 	}
2302 	qdf_spin_unlock_bh(&wmi_handle->eventq_lock);
2303 }
2304 
2305 /**
2306  * wmi_htc_tx_complete() - Process htc tx completion
2307  *
2308  * @ctx: handle to wmi
2309  * @htc_packet: pointer to htc packet
2310  *
2311  * @Return: none.
2312  */
2313 static void wmi_htc_tx_complete(void *ctx, HTC_PACKET *htc_pkt)
2314 {
2315 	struct wmi_soc *soc = (struct wmi_soc *) ctx;
2316 	wmi_buf_t wmi_cmd_buf = GET_HTC_PACKET_NET_BUF_CONTEXT(htc_pkt);
2317 	u_int8_t *buf_ptr;
2318 	u_int32_t len;
2319 	struct wmi_unified *wmi_handle;
2320 #ifdef WMI_INTERFACE_EVENT_LOGGING
2321 	uint32_t cmd_id;
2322 #endif
2323 
2324 	ASSERT(wmi_cmd_buf);
2325 	wmi_handle = wmi_get_pdev_ep(soc, htc_pkt->Endpoint);
2326 	if (wmi_handle == NULL) {
2327 		WMI_LOGE("%s: Unable to get wmi handle\n", __func__);
2328 		QDF_ASSERT(0);
2329 		return;
2330 	}
2331 #ifdef WMI_INTERFACE_EVENT_LOGGING
2332 	if (wmi_handle && wmi_handle->log_info.wmi_logging_enable) {
2333 		cmd_id = WMI_GET_FIELD(qdf_nbuf_data(wmi_cmd_buf),
2334 				WMI_CMD_HDR, COMMANDID);
2335 
2336 	qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
2337 	/* Record 16 bytes of WMI cmd tx complete data
2338 	- exclude TLV and WMI headers */
2339 	if (wmi_handle->ops->is_management_record(cmd_id)) {
2340 		WMI_MGMT_COMMAND_TX_CMP_RECORD(wmi_handle, cmd_id,
2341 			qdf_nbuf_data(wmi_cmd_buf) +
2342 			wmi_handle->log_info.buf_offset_command);
2343 	} else {
2344 		WMI_COMMAND_TX_CMP_RECORD(wmi_handle, cmd_id,
2345 			qdf_nbuf_data(wmi_cmd_buf) +
2346 			wmi_handle->log_info.buf_offset_command);
2347 	}
2348 
2349 	qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
2350 	}
2351 #endif
2352 	buf_ptr = (u_int8_t *) wmi_buf_data(wmi_cmd_buf);
2353 	len = qdf_nbuf_len(wmi_cmd_buf);
2354 	qdf_mem_zero(buf_ptr, len);
2355 	qdf_nbuf_free(wmi_cmd_buf);
2356 	qdf_mem_free(htc_pkt);
2357 	qdf_atomic_dec(&wmi_handle->pending_cmds);
2358 }
2359 
2360 /**
2361  * wmi_connect_pdev_htc_service() -  WMI API to get connect to HTC service
2362  *
2363  * @wmi_handle: handle to WMI.
2364  * @pdev_idx: Pdev index
2365  *
2366  * @Return: status.
2367  */
2368 static int wmi_connect_pdev_htc_service(struct wmi_soc *soc,
2369 						uint32_t pdev_idx)
2370 {
2371 	int status;
2372 	struct htc_service_connect_resp response;
2373 	struct htc_service_connect_req connect;
2374 
2375 	OS_MEMZERO(&connect, sizeof(connect));
2376 	OS_MEMZERO(&response, sizeof(response));
2377 
2378 	/* meta data is unused for now */
2379 	connect.pMetaData = NULL;
2380 	connect.MetaDataLength = 0;
2381 	/* these fields are the same for all service endpoints */
2382 	connect.EpCallbacks.pContext = soc;
2383 	connect.EpCallbacks.EpTxCompleteMultiple =
2384 		NULL /* Control path completion ar6000_tx_complete */;
2385 	connect.EpCallbacks.EpRecv = wmi_control_rx /* Control path rx */;
2386 	connect.EpCallbacks.EpRecvRefill = NULL /* ar6000_rx_refill */;
2387 	connect.EpCallbacks.EpSendFull = NULL /* ar6000_tx_queue_full */;
2388 	connect.EpCallbacks.EpTxComplete =
2389 		wmi_htc_tx_complete /* ar6000_tx_queue_full */;
2390 
2391 	/* connect to control service */
2392 	connect.service_id = soc->svc_ids[pdev_idx];
2393 	status = htc_connect_service(soc->htc_handle, &connect,
2394 				&response);
2395 
2396 
2397 	if (status != EOK) {
2398 		qdf_print
2399 			("Failed to connect to WMI CONTROL service status:%d\n",
2400 			status);
2401 		return status;
2402 	}
2403 
2404 	soc->wmi_endpoint_id[pdev_idx] = response.Endpoint;
2405 	soc->max_msg_len[pdev_idx] = response.MaxMsgLength;
2406 
2407 	return 0;
2408 }
2409 
2410 /**
2411  * wmi_unified_connect_htc_service() -  WMI API to get connect to HTC service
2412  *
2413  * @wmi_handle: handle to WMI.
2414  *
2415  * @Return: status.
2416  */
2417 QDF_STATUS
2418 wmi_unified_connect_htc_service(struct wmi_unified *wmi_handle,
2419 				void *htc_handle)
2420 {
2421 	uint32_t i;
2422 	uint8_t wmi_ep_count;
2423 
2424 	wmi_handle->soc->htc_handle = htc_handle;
2425 
2426 	wmi_ep_count = htc_get_wmi_endpoint_count(htc_handle);
2427 	if (wmi_ep_count > WMI_MAX_RADIOS)
2428 		return QDF_STATUS_E_FAULT;
2429 
2430 	for (i = 0; i < wmi_ep_count; i++)
2431 		wmi_connect_pdev_htc_service(wmi_handle->soc, i);
2432 
2433 	wmi_handle->htc_handle = htc_handle;
2434 	wmi_handle->wmi_endpoint_id = wmi_handle->soc->wmi_endpoint_id[0];
2435 	wmi_handle->max_msg_len = wmi_handle->soc->max_msg_len[0];
2436 
2437 	return QDF_STATUS_SUCCESS;
2438 }
2439 
2440 /**
2441  * wmi_get_host_credits() -  WMI API to get updated host_credits
2442  *
2443  * @wmi_handle: handle to WMI.
2444  *
2445  * @Return: updated host_credits.
2446  */
2447 int wmi_get_host_credits(wmi_unified_t wmi_handle)
2448 {
2449 	int host_credits = 0;
2450 
2451 	htc_get_control_endpoint_tx_host_credits(wmi_handle->htc_handle,
2452 						 &host_credits);
2453 	return host_credits;
2454 }
2455 
2456 /**
2457  * wmi_get_pending_cmds() - WMI API to get WMI Pending Commands in the HTC
2458  *                          queue
2459  *
2460  * @wmi_handle: handle to WMI.
2461  *
2462  * @Return: Pending Commands in the HTC queue.
2463  */
2464 int wmi_get_pending_cmds(wmi_unified_t wmi_handle)
2465 {
2466 	return qdf_atomic_read(&wmi_handle->pending_cmds);
2467 }
2468 
2469 /**
2470  * wmi_set_target_suspend() -  WMI API to set target suspend state
2471  *
2472  * @wmi_handle: handle to WMI.
2473  * @val: suspend state boolean.
2474  *
2475  * @Return: none.
2476  */
2477 void wmi_set_target_suspend(wmi_unified_t wmi_handle, A_BOOL val)
2478 {
2479 	qdf_atomic_set(&wmi_handle->is_target_suspended, val);
2480 }
2481 
2482 /**
2483  * WMI API to set crash injection state
2484  * @param wmi_handle:	handle to WMI.
2485  * @param val:		crash injection state boolean.
2486  */
2487 void wmi_tag_crash_inject(wmi_unified_t wmi_handle, A_BOOL flag)
2488 {
2489 	wmi_handle->tag_crash_inject = flag;
2490 }
2491 
2492 /**
2493  * WMI API to set bus suspend state
2494  * @param wmi_handle:	handle to WMI.
2495  * @param val:		suspend state boolean.
2496  */
2497 void wmi_set_is_wow_bus_suspended(wmi_unified_t wmi_handle, A_BOOL val)
2498 {
2499 	qdf_atomic_set(&wmi_handle->is_wow_bus_suspended, val);
2500 }
2501 
2502 void wmi_set_tgt_assert(wmi_unified_t wmi_handle, bool val)
2503 {
2504 	wmi_handle->tgt_force_assert_enable = val;
2505 }
2506 
2507 #ifndef CONFIG_MCL
2508 /**
2509  * API to flush all the previous packets  associated with the wmi endpoint
2510  *
2511  * @param wmi_handle      : handle to WMI.
2512  */
2513 void
2514 wmi_flush_endpoint(wmi_unified_t wmi_handle)
2515 {
2516 	htc_flush_endpoint(wmi_handle->htc_handle,
2517 		wmi_handle->wmi_endpoint_id, 0);
2518 }
2519 qdf_export_symbol(wmi_flush_endpoint);
2520 
2521 /**
2522  * generic function to block unified WMI command
2523  * @param wmi_handle      : handle to WMI.
2524  * @return 0  on success and -ve on failure.
2525  */
2526 int
2527 wmi_stop(wmi_unified_t wmi_handle)
2528 {
2529 	QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_INFO,
2530 			"WMI Stop\n");
2531 	wmi_handle->wmi_stopinprogress = 1;
2532 	return 0;
2533 }
2534 
2535 /**
2536  * wmi_pdev_id_conversion_enable() - API to enable pdev_id conversion in WMI
2537  *                     By default pdev_id conversion is not done in WMI.
2538  *                     This API can be used enable conversion in WMI.
2539  * @param wmi_handle   : handle to WMI
2540  * Return none
2541  */
2542 void wmi_pdev_id_conversion_enable(wmi_unified_t wmi_handle)
2543 {
2544 	if (wmi_handle->target_type == WMI_TLV_TARGET)
2545 		wmi_handle->ops->wmi_pdev_id_conversion_enable(wmi_handle);
2546 }
2547 
2548 #endif
2549