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