xref: /wlan-dirver/qca-wifi-host-cmn/ipa/core/src/wlan_ipa_core.c (revision 45c28558a520fd0e975b20c0ad534a0aa7f08021)
1 /*
2  * Copyright (c) 2013-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Include Files */
21 #include "wlan_ipa_core.h"
22 #include "wlan_ipa_main.h"
23 #include "cdp_txrx_ipa.h"
24 #include "cdp_txrx_ctrl.h"
25 #include "wal_rx_desc.h"
26 #include "qdf_str.h"
27 #include "host_diag_core_event.h"
28 #include "wlan_objmgr_vdev_obj.h"
29 #include "qdf_platform.h"
30 #include <wmi_unified_param.h>
31 #include <wlan_osif_priv.h>
32 #include <net/cfg80211.h>
33 #ifdef QCA_LL_TX_FLOW_CONTROL_V2
34 #include <cdp_txrx_flow_ctrl_v2.h>
35 #include <cdp_txrx_peer_ops.h>
36 #endif
37 #include <qal_vbus_dev.h>
38 
39 #define IPA_SPS_DESC_SIZE 8
40 #define IPA_DEFAULT_HDL 0
41 #ifdef IPA_WDS_EASYMESH_FEATURE
42 #define IPA_TA_PEER_ID_ATTRI 2
43 #endif
44 
45 static struct wlan_ipa_priv *gp_ipa;
46 static void wlan_ipa_set_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx);
47 static void wlan_ipa_reset_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx);
48 
49 static inline
50 bool wlan_ipa_is_driver_unloading(struct wlan_ipa_priv *ipa_ctx)
51 {
52 	if (ipa_ctx->driver_is_unloading)
53 		return ipa_ctx->driver_is_unloading();
54 	return false;
55 }
56 
57 static struct wlan_ipa_iface_2_client {
58 	qdf_ipa_client_type_t cons_client;
59 	qdf_ipa_client_type_t prod_client;
60 } wlan_ipa_iface_2_client[WLAN_IPA_CLIENT_MAX_IFACE] = {
61 	{
62 		QDF_IPA_CLIENT_WLAN2_CONS, QDF_IPA_CLIENT_WLAN1_PROD
63 	},
64 	{
65 		QDF_IPA_CLIENT_MCC2_CONS,  QDF_IPA_CLIENT_WLAN1_PROD
66 	},
67 #if WLAN_IPA_CLIENT_MAX_IFACE >= 3
68 	{
69 		QDF_IPA_CLIENT_WLAN4_CONS, QDF_IPA_CLIENT_WLAN1_PROD
70 	},
71 #if WLAN_IPA_CLIENT_MAX_IFACE == 4
72 	{
73 		QDF_IPA_CLIENT_WLAN4_CONS, QDF_IPA_CLIENT_WLAN1_PROD
74 	},
75 #endif
76 #endif
77 };
78 
79 /* Local Function Prototypes */
80 static void wlan_ipa_i2w_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
81 			    unsigned long data);
82 static void wlan_ipa_w2i_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
83 			    unsigned long data);
84 static void wlan_ipa_update_wds_params(struct wlan_ipa_priv *ipa_ctx,
85 				       qdf_ipa_wdi_init_in_params_t *in);
86 static void wlan_ipa_msg_wds_update(bool ipa_wds, qdf_ipa_wlan_msg_t *msg);
87 
88 /**
89  * wlan_ipa_uc_sta_is_enabled() - Is STA mode IPA uC offload enabled?
90  * @ipa_cfg: IPA config
91  *
92  * Return: true if STA mode IPA uC offload is enabled, false otherwise
93  */
94 static inline bool wlan_ipa_uc_sta_is_enabled(struct wlan_ipa_config *ipa_cfg)
95 {
96 	return WLAN_IPA_IS_CONFIG_ENABLED(ipa_cfg, WLAN_IPA_UC_STA_ENABLE_MASK);
97 }
98 
99 /**
100  * wlan_ipa_is_pre_filter_enabled() - Is IPA pre-filter enabled?
101  * @ipa_cfg: IPA config
102  *
103  * Return: true if pre-filter is enabled, otherwise false
104  */
105 static inline
106 bool wlan_ipa_is_pre_filter_enabled(struct wlan_ipa_config *ipa_cfg)
107 {
108 	return WLAN_IPA_IS_CONFIG_ENABLED(ipa_cfg,
109 					 WLAN_IPA_PRE_FILTER_ENABLE_MASK);
110 }
111 
112 /**
113  * wlan_ipa_is_ipv6_enabled() - Is IPA IPv6 enabled?
114  * @ipa_cfg: IPA config
115  *
116  * Return: true if IPv6 is enabled, otherwise false
117  */
118 static inline bool wlan_ipa_is_ipv6_enabled(struct wlan_ipa_config *ipa_cfg)
119 {
120 	return WLAN_IPA_IS_CONFIG_ENABLED(ipa_cfg, WLAN_IPA_IPV6_ENABLE_MASK);
121 }
122 
123 /**
124  * wlan_ipa_is_sta_only_offload_enabled() - Is IPA STA only offload enabled
125  *
126  * STA only IPA offload is needed on MDM platforms to support
127  * tethering scenarios in STA-SAP configurations when SAP is idle.
128  *
129  * Currently in STA-SAP configurations, IPA pipes are enabled only
130  * when a wifi client is connected to SAP.
131  *
132  * Impact of this API is only limited to when IPA pipes are enabled
133  * and disabled. To take effect, WLAN_IPA_UC_STA_ENABLE_MASK needs to
134  * set to 1.
135  *
136  * Return: true if MDM_PLATFORM is defined, false otherwise
137  */
138 #ifdef MDM_PLATFORM
139 static inline bool wlan_ipa_is_sta_only_offload_enabled(void)
140 {
141 	return true;
142 }
143 #else
144 static inline bool wlan_ipa_is_sta_only_offload_enabled(void)
145 {
146 	return false;
147 }
148 #endif
149 
150 /**
151  * wlan_ipa_msg_free_fn() - Free an IPA message
152  * @buff: pointer to the IPA message
153  * @len: length of the IPA message
154  * @type: type of IPA message
155  *
156  * Return: None
157  */
158 static void wlan_ipa_msg_free_fn(void *buff, uint32_t len, uint32_t type)
159 {
160 	ipa_debug("msg type:%d, len:%d", type, len);
161 	qdf_mem_free(buff);
162 }
163 
164 /**
165  * wlan_ipa_uc_loaded_uc_cb() - IPA UC loaded event callback
166  * @priv_ctxt: IPA context
167  *
168  * Will be called by IPA context.
169  * It's atomic context, then should be scheduled to kworker thread
170  *
171  * Return: None
172  */
173 static void wlan_ipa_uc_loaded_uc_cb(void *priv_ctxt)
174 {
175 	struct wlan_ipa_priv *ipa_ctx;
176 	struct op_msg_type *msg;
177 	struct uc_op_work_struct *uc_op_work;
178 
179 	if (!ipa_cb_is_ready()) {
180 		ipa_info("IPA is not READY");
181 		return;
182 	}
183 
184 	if (!priv_ctxt) {
185 		ipa_err("Invalid IPA context");
186 		return;
187 	}
188 
189 	ipa_ctx = priv_ctxt;
190 
191 	uc_op_work = &ipa_ctx->uc_op_work[WLAN_IPA_UC_OPCODE_UC_READY];
192 	if (!list_empty(&uc_op_work->work.work.entry)) {
193 		/* uc_op_work is not initialized yet */
194 		ipa_ctx->uc_loaded = true;
195 		return;
196 	}
197 
198 	msg = qdf_mem_malloc(sizeof(*msg));
199 	if (!msg)
200 		return;
201 
202 	msg->op_code = WLAN_IPA_UC_OPCODE_UC_READY;
203 
204 	/* When the same uC OPCODE is already pended, just return */
205 	if (uc_op_work->msg)
206 		goto done;
207 
208 	uc_op_work->msg = msg;
209 
210 	if (!qdf_atomic_read(&ipa_ctx->deinit_in_prog)) {
211 		qdf_sched_work(0, &uc_op_work->work);
212 	} else {
213 		uc_op_work->msg = NULL;
214 		goto done;
215 	}
216 
217 	/* work handler will free the msg buffer */
218 	return;
219 
220 done:
221 	qdf_mem_free(msg);
222 }
223 
224 struct wlan_ipa_priv *wlan_ipa_get_obj_context(void)
225 {
226 	return gp_ipa;
227 }
228 
229 /**
230  * wlan_ipa_send_pkt_to_tl() - Send an IPA packet to TL
231  * @iface_context: interface-specific IPA context
232  * @ipa_tx_desc: packet data descriptor
233  *
234  * Return: None
235  */
236 static void wlan_ipa_send_pkt_to_tl(
237 		struct wlan_ipa_iface_context *iface_context,
238 		qdf_ipa_rx_data_t *ipa_tx_desc)
239 {
240 	struct wlan_ipa_priv *ipa_ctx = iface_context->ipa_ctx;
241 	struct wlan_objmgr_pdev *pdev;
242 	struct wlan_objmgr_psoc *psoc;
243 	qdf_device_t osdev;
244 	qdf_nbuf_t skb;
245 	struct wlan_ipa_tx_desc *tx_desc;
246 	qdf_dma_addr_t paddr;
247 	QDF_STATUS status;
248 
249 	if (!ipa_ctx)
250 		return;
251 	pdev = ipa_ctx->pdev;
252 	psoc = wlan_pdev_get_psoc(pdev);
253 	osdev = wlan_psoc_get_qdf_dev(psoc);
254 
255 	qdf_spin_lock_bh(&iface_context->interface_lock);
256 	/*
257 	 * During CAC period, data packets shouldn't be sent over the air so
258 	 * drop all the packets here
259 	 */
260 	if (iface_context->device_mode == QDF_SAP_MODE ||
261 	    iface_context->device_mode == QDF_P2P_GO_MODE) {
262 		if (ipa_ctx->dfs_cac_block_tx) {
263 			ipa_free_skb(ipa_tx_desc);
264 			qdf_spin_unlock_bh(&iface_context->interface_lock);
265 			iface_context->stats.num_tx_cac_drop++;
266 			wlan_ipa_wdi_rm_try_release(ipa_ctx);
267 			return;
268 		}
269 	}
270 
271 	if (!osdev) {
272 		ipa_free_skb(ipa_tx_desc);
273 		iface_context->stats.num_tx_drop++;
274 		qdf_spin_unlock_bh(&iface_context->interface_lock);
275 		wlan_ipa_wdi_rm_try_release(ipa_ctx);
276 		return;
277 	}
278 	qdf_spin_unlock_bh(&iface_context->interface_lock);
279 
280 	skb = QDF_IPA_RX_DATA_SKB(ipa_tx_desc);
281 
282 	qdf_mem_zero(skb->cb, sizeof(skb->cb));
283 
284 	/* Store IPA Tx buffer ownership into SKB CB */
285 	qdf_nbuf_ipa_owned_set(skb);
286 
287 	if (qdf_mem_smmu_s1_enabled(osdev)) {
288 		status = qdf_nbuf_map(osdev, skb, QDF_DMA_TO_DEVICE);
289 		if (QDF_IS_STATUS_SUCCESS(status)) {
290 			paddr = qdf_nbuf_get_frag_paddr(skb, 0);
291 		} else {
292 			ipa_free_skb(ipa_tx_desc);
293 			qdf_spin_lock_bh(&iface_context->interface_lock);
294 			iface_context->stats.num_tx_drop++;
295 			qdf_spin_unlock_bh(&iface_context->interface_lock);
296 			wlan_ipa_wdi_rm_try_release(ipa_ctx);
297 			return;
298 		}
299 	} else {
300 		paddr = QDF_IPA_RX_DATA_DMA_ADDR(ipa_tx_desc);
301 	}
302 
303 	if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
304 		qdf_nbuf_mapped_paddr_set(skb,
305 					  paddr +
306 					  WLAN_IPA_WLAN_FRAG_HEADER +
307 					  WLAN_IPA_WLAN_IPA_HEADER);
308 		QDF_IPA_RX_DATA_SKB_LEN(ipa_tx_desc) -=
309 			WLAN_IPA_WLAN_FRAG_HEADER + WLAN_IPA_WLAN_IPA_HEADER;
310 	} else {
311 		qdf_nbuf_mapped_paddr_set(skb, paddr);
312 	}
313 
314 	qdf_spin_lock_bh(&ipa_ctx->q_lock);
315 	/* get free Tx desc and assign ipa_tx_desc pointer */
316 	if (ipa_ctx->tx_desc_free_list.count &&
317 	    qdf_list_remove_front(&ipa_ctx->tx_desc_free_list,
318 				  (qdf_list_node_t **)&tx_desc) ==
319 							QDF_STATUS_SUCCESS) {
320 		tx_desc->ipa_tx_desc_ptr = ipa_tx_desc;
321 		ipa_ctx->stats.num_tx_desc_q_cnt++;
322 		qdf_spin_unlock_bh(&ipa_ctx->q_lock);
323 		/* Store Tx Desc index into SKB CB */
324 		qdf_nbuf_ipa_priv_set(skb, tx_desc->id);
325 	} else {
326 		ipa_ctx->stats.num_tx_desc_error++;
327 		qdf_spin_unlock_bh(&ipa_ctx->q_lock);
328 
329 		if (qdf_mem_smmu_s1_enabled(osdev)) {
330 			if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config))
331 				qdf_nbuf_mapped_paddr_set(skb, paddr);
332 
333 			qdf_nbuf_unmap(osdev, skb, QDF_DMA_TO_DEVICE);
334 		}
335 
336 		qdf_ipa_free_skb(ipa_tx_desc);
337 		wlan_ipa_wdi_rm_try_release(ipa_ctx);
338 		return;
339 	}
340 
341 	skb = cdp_ipa_tx_send_data_frame(ipa_ctx->dp_soc,
342 					 iface_context->session_id,
343 					 QDF_IPA_RX_DATA_SKB(ipa_tx_desc));
344 	if (skb) {
345 		qdf_nbuf_free(skb);
346 		iface_context->stats.num_tx_err++;
347 		return;
348 	}
349 
350 	atomic_inc(&ipa_ctx->tx_ref_cnt);
351 
352 	iface_context->stats.num_tx++;
353 }
354 
355 /**
356  * wlan_ipa_forward() - handle packet forwarding to wlan tx
357  * @ipa_ctx: pointer to ipa ipa context
358  * @iface_ctx: interface context
359  * @skb: data pointer
360  *
361  * if exception packet has set forward bit, copied new packet should be
362  * forwarded to wlan tx. if wlan subsystem is in suspend state, packet should
363  * put into pm queue and tx procedure will be differed
364  *
365  * Return: None
366  */
367 static void wlan_ipa_forward(struct wlan_ipa_priv *ipa_ctx,
368 			     struct wlan_ipa_iface_context *iface_ctx,
369 			     qdf_nbuf_t skb)
370 {
371 	struct wlan_ipa_pm_tx_cb *pm_tx_cb;
372 
373 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
374 
375 	/* Set IPA ownership for intra-BSS Tx packets to avoid skb_orphan */
376 	qdf_nbuf_ipa_owned_set(skb);
377 
378 	/* WLAN subsystem is in suspend, put in queue */
379 	if (ipa_ctx->suspended) {
380 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
381 		ipa_info_rl("Tx in suspend, put in queue");
382 		qdf_mem_zero(skb->cb, sizeof(skb->cb));
383 		pm_tx_cb = (struct wlan_ipa_pm_tx_cb *)skb->cb;
384 		pm_tx_cb->exception = true;
385 		pm_tx_cb->iface_context = iface_ctx;
386 		qdf_spin_lock_bh(&ipa_ctx->pm_lock);
387 		qdf_nbuf_queue_add(&ipa_ctx->pm_queue_head, skb);
388 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
389 		ipa_ctx->stats.num_tx_queued++;
390 	} else {
391 		/* Resume, put packet into WLAN TX */
392 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
393 
394 		if (ipa_ctx->softap_xmit) {
395 			if (ipa_ctx->softap_xmit(skb, iface_ctx->dev)) {
396 				ipa_err_rl("packet Tx fail");
397 				ipa_ctx->stats.num_tx_fwd_err++;
398 			} else {
399 				ipa_ctx->stats.num_tx_fwd_ok++;
400 			}
401 		} else {
402 			dev_kfree_skb_any(skb);
403 		}
404 	}
405 }
406 
407 /**
408  * wlan_ipa_intrabss_forward() - Forward intra bss packets.
409  * @ipa_ctx: pointer to IPA IPA struct
410  * @iface_ctx: ipa interface context
411  * @desc: Firmware descriptor
412  * @skb: Data buffer
413  *
414  * Return:
415  *      WLAN_IPA_FORWARD_PKT_NONE
416  *      WLAN_IPA_FORWARD_PKT_DISCARD
417  *      WLAN_IPA_FORWARD_PKT_LOCAL_STACK
418  *
419  */
420 
421 #ifndef QCA_IPA_LL_TX_FLOW_CONTROL
422 static inline
423 bool wlan_ipa_tx_desc_thresh_reached(struct cdp_soc_t *soc, uint8_t vdev_id)
424 {
425 	return cdp_tx_desc_thresh_reached(soc, vdev_id);
426 }
427 
428 static inline
429 bool wlan_ipa_get_peer_state(struct cdp_soc_t *soc, uint8_t vdev_id,
430 			     uint8_t *peer_mac)
431 {
432 	if (cdp_peer_state_get(soc, vdev_id, peer_mac) ==
433 	    OL_TXRX_PEER_STATE_AUTH)
434 		return true;
435 
436 	return false;
437 }
438 #else
439 static inline
440 bool wlan_ipa_tx_desc_thresh_reached(struct cdp_soc_t *soc, uint8_t vdev_id)
441 {
442 	return false;
443 }
444 
445 static inline
446 bool wlan_ipa_get_peer_state(struct cdp_soc_t *soc, uint8_t vdev_id,
447 			     uint8_t *peer_mac)
448 {
449 	return cdp_peer_get_authorize(soc, vdev_id, peer_mac);
450 }
451 #endif
452 
453 static enum wlan_ipa_forward_type wlan_ipa_intrabss_forward(
454 		struct wlan_ipa_priv *ipa_ctx,
455 		struct wlan_ipa_iface_context *iface_ctx,
456 		uint8_t desc,
457 		qdf_nbuf_t skb)
458 {
459 	int ret = WLAN_IPA_FORWARD_PKT_NONE;
460 	void *soc = ipa_ctx->dp_soc;
461 
462 	if ((desc & FW_RX_DESC_FORWARD_M)) {
463 		if (wlan_ipa_tx_desc_thresh_reached(soc,
464 						    iface_ctx->session_id)) {
465 			/* Drop the packet*/
466 			ipa_ctx->stats.num_tx_fwd_err++;
467 			goto drop_pkt;
468 		}
469 
470 		ipa_debug_rl("Forward packet to Tx (fw_desc=%d)", desc);
471 		ipa_ctx->ipa_tx_forward++;
472 
473 		if ((desc & FW_RX_DESC_DISCARD_M)) {
474 			wlan_ipa_forward(ipa_ctx, iface_ctx, skb);
475 			ipa_ctx->ipa_rx_internal_drop_count++;
476 			ipa_ctx->ipa_rx_discard++;
477 			ret = WLAN_IPA_FORWARD_PKT_DISCARD;
478 		} else {
479 			struct sk_buff *cloned_skb = skb_clone(skb, GFP_ATOMIC);
480 
481 			if (cloned_skb)
482 				wlan_ipa_forward(ipa_ctx, iface_ctx,
483 						 cloned_skb);
484 			else
485 				ipa_err_rl("tx skb alloc failed");
486 			ret = WLAN_IPA_FORWARD_PKT_LOCAL_STACK;
487 		}
488 	}
489 	return ret;
490 
491 drop_pkt:
492 	dev_kfree_skb_any(skb);
493 	ret = WLAN_IPA_FORWARD_PKT_DISCARD;
494 	return ret;
495 }
496 
497 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)) || \
498 	defined(CONFIG_IPA_WDI_UNIFIED_API)
499 /*
500  * TODO: Get WDI version through FW capabilities
501  */
502 #if defined(QCA_WIFI_QCA6290) || defined(QCA_WIFI_QCA6390) || \
503     defined(QCA_WIFI_QCA6490) || defined(QCA_WIFI_QCA6750) || \
504     defined(QCA_WIFI_WCN7850) || defined(QCA_WIFI_QCN9000)
505 static inline void wlan_ipa_wdi_get_wdi_version(struct wlan_ipa_priv *ipa_ctx)
506 {
507 	ipa_ctx->wdi_version = IPA_WDI_3;
508 }
509 #elif defined(QCA_WIFI_KIWI) || defined(QCA_WIFI_KIWI_V2)
510 static inline void wlan_ipa_wdi_get_wdi_version(struct wlan_ipa_priv *ipa_ctx)
511 {
512 	ipa_ctx->wdi_version = IPA_WDI_3_V2;
513 }
514 #elif defined(QCA_WIFI_3_0)
515 static inline void wlan_ipa_wdi_get_wdi_version(struct wlan_ipa_priv *ipa_ctx)
516 {
517 	ipa_ctx->wdi_version = IPA_WDI_2;
518 }
519 #else
520 static inline void wlan_ipa_wdi_get_wdi_version(struct wlan_ipa_priv *ipa_ctx)
521 {
522 	ipa_ctx->wdi_version = IPA_WDI_1;
523 }
524 #endif
525 
526 static inline bool wlan_ipa_wdi_is_smmu_enabled(struct wlan_ipa_priv *ipa_ctx,
527 					       qdf_device_t osdev)
528 {
529 	return ipa_ctx->is_smmu_enabled && qdf_mem_smmu_s1_enabled(osdev);
530 }
531 
532 #ifdef IPA_WDS_EASYMESH_FEATURE
533 /**
534  * wlan_ipa_ast_notify_cb() - IPA AST create/update CB
535  * @priv: IPA context
536  * @data: Structure used for updating the AST table
537  *
538  * Will be called by IPA context.
539  *
540  * Return: None
541  */
542 static void wlan_ipa_ast_notify_cb(void *priv, void *data)
543 {
544 	qdf_ipa_ast_info_type_t *ast_info;
545 	struct wlan_ipa_priv *ipa_ctx;
546 
547 	if (!data) {
548 		dp_err("Invalid IPA AST data context");
549 		return;
550 	}
551 
552 	if (!priv) {
553 		dp_err("Invalid IPA context");
554 		return;
555 	}
556 
557 	ast_info = (qdf_ipa_ast_info_type_t *)data;
558 	ipa_ctx = (struct wlan_ipa_priv *)priv;
559 
560 	cdp_ipa_ast_create(ipa_ctx->dp_soc, ast_info);
561 }
562 #else
563 static inline void wlan_ipa_ast_notify_cb(void *priv, void *data)
564 {
565 }
566 #endif
567 
568 static inline QDF_STATUS
569 wlan_ipa_wdi_setup(struct wlan_ipa_priv *ipa_ctx,
570 		   qdf_device_t osdev)
571 {
572 	qdf_ipa_sys_connect_params_t *sys_in = NULL;
573 	int i;
574 	QDF_STATUS qdf_status = QDF_STATUS_SUCCESS;
575 
576 	sys_in = qdf_mem_malloc(sizeof(*sys_in) * WLAN_IPA_MAX_IFACE);
577 	if (!sys_in)
578 		return QDF_STATUS_E_NOMEM;
579 
580 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++)
581 		qdf_mem_copy(sys_in + i,
582 			     &ipa_ctx->sys_pipe[i].ipa_sys_params,
583 			     sizeof(qdf_ipa_sys_connect_params_t));
584 
585 	qdf_status = cdp_ipa_setup(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id,
586 				   wlan_ipa_i2w_cb, wlan_ipa_w2i_cb,
587 				   wlan_ipa_wdi_meter_notifier_cb,
588 				   ipa_ctx->config->desc_size,
589 				   ipa_ctx,
590 				   wlan_ipa_is_rm_enabled(ipa_ctx->config),
591 				   &ipa_ctx->tx_pipe_handle,
592 				   &ipa_ctx->rx_pipe_handle,
593 				   wlan_ipa_wdi_is_smmu_enabled(ipa_ctx, osdev),
594 				   sys_in, ipa_ctx->over_gsi, ipa_ctx->hdl,
595 				   (qdf_ipa_wdi_hdl_t)ipa_ctx->instance_id,
596 				   wlan_ipa_ast_notify_cb);
597 
598 	qdf_mem_free(sys_in);
599 
600 	return qdf_status;
601 }
602 
603 #ifdef FEATURE_METERING
604 /**
605  * wlan_ipa_wdi_init_metering() - IPA WDI metering init
606  * @ipa_ctx: IPA context
607  * @in: IPA WDI in param
608  *
609  * Return: QDF_STATUS
610  */
611 static inline void wlan_ipa_wdi_init_metering(struct wlan_ipa_priv *ipa_ctxt,
612 					      qdf_ipa_wdi_init_in_params_t *in)
613 {
614 	QDF_IPA_WDI_INIT_IN_PARAMS_WDI_NOTIFY(in) =
615 				wlan_ipa_wdi_meter_notifier_cb;
616 }
617 #else
618 static inline void wlan_ipa_wdi_init_metering(struct wlan_ipa_priv *ipa_ctxt,
619 					      qdf_ipa_wdi_init_in_params_t *in)
620 {
621 }
622 #endif
623 
624 #ifdef IPA_WDS_EASYMESH_FEATURE
625 /**
626  * wlan_ipa_update_wds_params();
627  * @ipa_ctx: IPA context
628  * @in: IPA wdi init in params
629  *
630  * This function is to update wds status to IPA in wdi init params
631  *
632  * Return: None
633  */
634 static void wlan_ipa_update_wds_params(struct wlan_ipa_priv *ipa_ctx,
635 				       qdf_ipa_wdi_init_in_params_t *in)
636 {
637 	QDF_IPA_WDI_INIT_IN_PARAMS_WDS_UPDATE(in) = ipa_ctx->config->ipa_wds;
638 }
639 
640 /**
641  * wlan_ipa_msg_wds_update();
642  * @ipa_wds: IPA WDS status
643  * @msg: Meta data message for IPA
644  *
645  * This function is to update wds status to IPA in meta message
646  *
647  * Return: None
648  */
649 static void wlan_ipa_msg_wds_update(bool ipa_wds,
650 				    qdf_ipa_wlan_msg_t *msg)
651 {
652 	QDF_IPA_WLAN_MSG_WDS_UPDATE(msg) = ipa_wds;
653 }
654 #else
655 static void wlan_ipa_update_wds_params(struct wlan_ipa_priv *ipa_ctx,
656 				       qdf_ipa_wdi_init_in_params_t *in)
657 {
658 }
659 
660 static void wlan_ipa_msg_wds_update(bool ipa_wds,
661 				    qdf_ipa_wlan_msg_t *msg)
662 {
663 }
664 #endif
665 
666 /**
667  * wlan_ipa_wdi_init() - IPA WDI init
668  * @ipa_ctx: IPA context
669  *
670  * Return: QDF_STATUS
671  */
672 static inline QDF_STATUS wlan_ipa_wdi_init(struct wlan_ipa_priv *ipa_ctx)
673 {
674 	qdf_ipa_wdi_init_in_params_t in;
675 	qdf_ipa_wdi_init_out_params_t out;
676 	int ret;
677 
678 	ipa_ctx->uc_loaded = false;
679 
680 	qdf_mem_zero(&in, sizeof(in));
681 	qdf_mem_zero(&out, sizeof(out));
682 
683 	QDF_IPA_WDI_INIT_IN_PARAMS_WDI_VERSION(&in) = ipa_ctx->wdi_version;
684 	QDF_IPA_WDI_INIT_IN_PARAMS_NOTIFY(&in) = wlan_ipa_uc_loaded_uc_cb;
685 	QDF_IPA_WDI_INIT_IN_PARAMS_PRIV(&in) = ipa_ctx;
686 	QDF_IPA_WDI_INIT_IN_PARAMS_INSTANCE_ID(&in) = ipa_ctx->instance_id;
687 	wlan_ipa_update_wds_params(ipa_ctx, &in);
688 	wlan_ipa_wdi_init_metering(ipa_ctx, &in);
689 	ret = qdf_ipa_wdi_init(&in, &out);
690 	if (ret) {
691 		ipa_err("ipa_wdi_init failed with ret=%d", ret);
692 		return QDF_STATUS_E_FAILURE;
693 	}
694 
695 	ipa_ctx->over_gsi =
696 		QDF_IPA_WDI_INIT_OUT_PARAMS_IS_OVER_GSI(&out);
697 	ipa_ctx->is_smmu_enabled =
698 		QDF_IPA_WDI_INIT_OUT_PARAMS_IS_SMMU_ENABLED(&out);
699 	ipa_ctx->hdl = QDF_IPA_WDI_INIT_OUT_PARAMS_HANDLE(&out);
700 
701 	ipa_info("ipa_over_gsi: %d, is_smmu_enabled: %d, handle: %d",
702 		 ipa_ctx->over_gsi, ipa_ctx->is_smmu_enabled, ipa_ctx->hdl);
703 
704 	if (QDF_IPA_WDI_INIT_OUT_PARAMS_IS_UC_READY(&out)) {
705 		ipa_debug("IPA uC READY");
706 		ipa_ctx->uc_loaded = true;
707 	} else {
708 		ipa_info("IPA uc not ready");
709 		return QDF_STATUS_E_BUSY;
710 	}
711 
712 	return QDF_STATUS_SUCCESS;
713 }
714 
715 static inline int wlan_ipa_wdi_cleanup(qdf_ipa_wdi_hdl_t hdl)
716 {
717 	int ret;
718 
719 	ret = qdf_ipa_wdi_cleanup(hdl);
720 	if (ret)
721 		ipa_info("ipa_wdi_cleanup failed ret=%d", ret);
722 	return ret;
723 }
724 
725 static inline int wlan_ipa_wdi_setup_sys_pipe(struct wlan_ipa_priv *ipa_ctx,
726 					     struct ipa_sys_connect_params *sys,
727 					     uint32_t *handle)
728 {
729 	return 0;
730 }
731 
732 static inline int wlan_ipa_wdi_teardown_sys_pipe(struct wlan_ipa_priv *ipa_ctx,
733 						uint32_t handle)
734 {
735 	return 0;
736 }
737 
738 /**
739  * wlan_ipa_pm_flush() - flush queued packets
740  * @work: pointer to the scheduled work
741  *
742  * Called during PM resume to send packets to TL which were queued
743  * while host was in the process of suspending.
744  *
745  * Return: None
746  */
747 static void wlan_ipa_pm_flush(void *data)
748 {
749 	struct wlan_ipa_priv *ipa_ctx = (struct wlan_ipa_priv *)data;
750 	struct wlan_ipa_pm_tx_cb *pm_tx_cb = NULL;
751 	qdf_nbuf_t skb;
752 	uint32_t dequeued = 0;
753 
754 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
755 	while (((skb = qdf_nbuf_queue_remove(&ipa_ctx->pm_queue_head)) !=
756 	       NULL)) {
757 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
758 
759 		pm_tx_cb = (struct wlan_ipa_pm_tx_cb *)skb->cb;
760 		dequeued++;
761 
762 		if (pm_tx_cb->exception) {
763 			if (ipa_ctx->softap_xmit &&
764 			    pm_tx_cb->iface_context->dev) {
765 				ipa_ctx->softap_xmit(skb,
766 						pm_tx_cb->iface_context->dev);
767 				ipa_ctx->stats.num_tx_fwd_ok++;
768 			} else {
769 				dev_kfree_skb_any(skb);
770 			}
771 		} else {
772 			wlan_ipa_send_pkt_to_tl(pm_tx_cb->iface_context,
773 						pm_tx_cb->ipa_tx_desc);
774 		}
775 
776 		qdf_spin_lock_bh(&ipa_ctx->pm_lock);
777 	}
778 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
779 
780 	ipa_ctx->stats.num_tx_dequeued += dequeued;
781 	if (dequeued > ipa_ctx->stats.num_max_pm_queue)
782 		ipa_ctx->stats.num_max_pm_queue = dequeued;
783 }
784 
785 int wlan_ipa_uc_smmu_map(bool map, uint32_t num_buf, qdf_mem_info_t *buf_arr)
786 {
787 	if (!ipa_cb_is_ready()) {
788 		ipa_info("IPA is not READY");
789 		return 0;
790 	}
791 
792 	if (!num_buf) {
793 		ipa_info("No buffers to map/unmap");
794 		return 0;
795 	}
796 	/**
797 	 * This API will compile for prelithium chipset
798 	 * where we have only one soc so passing default
799 	 * handle to IPA which is 0.
800 	 */
801 	if (map)
802 		return qdf_ipa_wdi_create_smmu_mapping(IPA_DEFAULT_HDL,
803 						       num_buf, buf_arr);
804 	else
805 		return qdf_ipa_wdi_release_smmu_mapping(IPA_DEFAULT_HDL,
806 							num_buf, buf_arr);
807 	return 0;
808 }
809 
810 #ifdef MDM_PLATFORM
811 /**
812  * is_rx_dest_bridge_dev() - is RX skb bridge device terminated
813  * @iface_ctx: pointer to WLAN IPA interface context
814  * @nbuf: skb buffer
815  *
816  * Check if skb is destined for bridge device, where SAP is a bridge
817  * port of it.
818  *
819  * FIXME: If there's a BH lockless API to check if destination MAC
820  * address is a valid peer, this check can be deleted. Currently
821  * dp_find_peer_by_addr() is used to check if destination MAC
822  * is a valid peer. Since WLAN IPA RX is in process context,
823  * qdf_spin_lock_bh in dp_find_peer_by_addr() turns to spin_lock_bh
824  * and this BH lock hurts netif_rx.
825  *
826  * Return: true/false
827  */
828 static bool is_rx_dest_bridge_dev(struct wlan_ipa_iface_context *iface_ctx,
829 				  qdf_nbuf_t nbuf)
830 {
831 	qdf_netdev_t master_ndev;
832 	qdf_netdev_t ndev;
833 	struct ethhdr *eh;
834 	uint8_t da_is_bcmc;
835 	bool ret;
836 
837 	/*
838 	 * WDI 3.0 skb->cb[] info from IPA driver
839 	 * skb->cb[0] = vdev_id
840 	 * skb->cb[1].bit#1 = da_is_bcmc
841 	 */
842 	da_is_bcmc = ((uint8_t)nbuf->cb[1]) & 0x2;
843 	if (da_is_bcmc)
844 		return false;
845 
846 	ndev = iface_ctx->dev;
847 	if (!ndev)
848 		return false;
849 
850 	if (!netif_is_bridge_port(ndev))
851 		return false;
852 
853 	qal_vbus_rcu_read_lock();
854 
855 	master_ndev = netdev_master_upper_dev_get_rcu(ndev);
856 	if (!master_ndev) {
857 		ret = false;
858 		goto out;
859 	}
860 
861 	eh = (struct ethhdr *)qdf_nbuf_data(nbuf);
862 	if (qdf_mem_cmp(eh->h_dest, master_ndev->dev_addr, QDF_MAC_ADDR_SIZE)) {
863 		ret = false;
864 		goto out;
865 	}
866 
867 	ret = true;
868 
869 out:
870 	qal_vbus_rcu_read_unlock();
871 	return ret;
872 }
873 #else /* !MDM_PLATFORM */
874 static bool is_rx_dest_bridge_dev(struct wlan_ipa_iface_context *iface_ctx,
875 				  qdf_nbuf_t nbuf)
876 {
877 	return false;
878 }
879 #endif /* MDM_PLATFORM */
880 
881 static enum wlan_ipa_forward_type
882 wlan_ipa_rx_intrabss_fwd(struct wlan_ipa_priv *ipa_ctx,
883 			 struct wlan_ipa_iface_context *iface_ctx,
884 			 qdf_nbuf_t nbuf)
885 {
886 	uint8_t fw_desc = 0;
887 	bool fwd_success;
888 	int ret;
889 
890 	/* legacy intra-bss forwarding for WDI 1.0 and 2.0 */
891 	if (ipa_ctx->wdi_version < IPA_WDI_3) {
892 		fw_desc = (uint8_t)nbuf->cb[1];
893 		return wlan_ipa_intrabss_forward(ipa_ctx, iface_ctx, fw_desc,
894 						 nbuf);
895 	}
896 
897 	if (is_rx_dest_bridge_dev(iface_ctx, nbuf)) {
898 		fwd_success = 0;
899 		ret = WLAN_IPA_FORWARD_PKT_LOCAL_STACK;
900 		goto exit;
901 	}
902 
903 	if (cdp_ipa_rx_intrabss_fwd(ipa_ctx->dp_soc, iface_ctx->session_id,
904 				    nbuf, &fwd_success)) {
905 		ipa_ctx->ipa_rx_internal_drop_count++;
906 		ipa_ctx->ipa_rx_discard++;
907 
908 		ret = WLAN_IPA_FORWARD_PKT_DISCARD;
909 	} else {
910 		ret = WLAN_IPA_FORWARD_PKT_LOCAL_STACK;
911 	}
912 
913 exit:
914 	if (fwd_success)
915 		ipa_ctx->stats.num_tx_fwd_ok++;
916 	else
917 		ipa_ctx->stats.num_tx_fwd_err++;
918 
919 	return ret;
920 }
921 
922 #else /* CONFIG_IPA_WDI_UNIFIED_API */
923 
924 static inline void wlan_ipa_wdi_get_wdi_version(struct wlan_ipa_priv *ipa_ctx)
925 {
926 }
927 
928 static inline int wlan_ipa_wdi_is_smmu_enabled(struct wlan_ipa_priv *ipa_ctx,
929 					       qdf_device_t osdev)
930 {
931 	return qdf_mem_smmu_s1_enabled(osdev);
932 }
933 
934 static inline QDF_STATUS wlan_ipa_wdi_setup(struct wlan_ipa_priv *ipa_ctx,
935 					    qdf_device_t osdev)
936 {
937 	return cdp_ipa_setup(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id,
938 			     wlan_ipa_i2w_cb, wlan_ipa_w2i_cb,
939 			     wlan_ipa_wdi_meter_notifier_cb,
940 			     ipa_ctx->config->desc_size,
941 			     ipa_ctx, wlan_ipa_is_rm_enabled(ipa_ctx->config),
942 			     &ipa_ctx->tx_pipe_handle,
943 			     &ipa_ctx->rx_pipe_handle);
944 }
945 
946 static inline QDF_STATUS wlan_ipa_wdi_init(struct wlan_ipa_priv *ipa_ctx)
947 {
948 	struct ipa_wdi_uc_ready_params uc_ready_param;
949 
950 	ipa_ctx->uc_loaded = false;
951 	uc_ready_param.priv = (void *)ipa_ctx;
952 	uc_ready_param.notify = wlan_ipa_uc_loaded_uc_cb;
953 	if (qdf_ipa_uc_reg_rdyCB(&uc_ready_param)) {
954 		ipa_info("UC Ready CB register fail");
955 		return QDF_STATUS_E_FAILURE;
956 	}
957 
958 	if (true == uc_ready_param.is_uC_ready) {
959 		ipa_info("UC Ready");
960 		ipa_ctx->uc_loaded = true;
961 	} else {
962 		return QDF_STATUS_E_BUSY;
963 	}
964 
965 	return QDF_STATUS_SUCCESS;
966 }
967 
968 static inline int wlan_ipa_wdi_cleanup(void)
969 {
970 	int ret;
971 
972 	ret = qdf_ipa_uc_dereg_rdyCB();
973 	if (ret)
974 		ipa_info("UC Ready CB deregister fail");
975 	return ret;
976 }
977 
978 static inline int wlan_ipa_wdi_setup_sys_pipe(
979 		struct wlan_ipa_priv *ipa_ctx,
980 		struct ipa_sys_connect_params *sys, uint32_t *handle)
981 {
982 	return qdf_ipa_setup_sys_pipe(sys, handle);
983 }
984 
985 static inline int wlan_ipa_wdi_teardown_sys_pipe(
986 		struct wlan_ipa_priv *ipa_ctx,
987 		uint32_t handle)
988 {
989 	return qdf_ipa_teardown_sys_pipe(handle);
990 }
991 
992 /**
993  * wlan_ipa_pm_flush() - flush queued packets
994  * @work: pointer to the scheduled work
995  *
996  * Called during PM resume to send packets to TL which were queued
997  * while host was in the process of suspending.
998  *
999  * Return: None
1000  */
1001 static void wlan_ipa_pm_flush(void *data)
1002 {
1003 	struct wlan_ipa_priv *ipa_ctx = (struct wlan_ipa_priv *)data;
1004 	struct wlan_ipa_pm_tx_cb *pm_tx_cb = NULL;
1005 	qdf_nbuf_t skb;
1006 	uint32_t dequeued = 0;
1007 
1008 	qdf_wake_lock_acquire(&ipa_ctx->wake_lock,
1009 			      WIFI_POWER_EVENT_WAKELOCK_IPA);
1010 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
1011 	while (((skb = qdf_nbuf_queue_remove(&ipa_ctx->pm_queue_head)) !=
1012 	       NULL)) {
1013 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1014 
1015 		pm_tx_cb = (struct wlan_ipa_pm_tx_cb *)skb->cb;
1016 		dequeued++;
1017 
1018 		if (pm_tx_cb->exception) {
1019 			if (ipa_ctx->softap_xmit &&
1020 			    pm_tx_cb->iface_context->dev) {
1021 				ipa_ctx->softap_xmit(skb,
1022 						pm_tx_cb->iface_context->dev);
1023 				ipa_ctx->stats.num_tx_fwd_ok++;
1024 			} else {
1025 				dev_kfree_skb_any(skb);
1026 			}
1027 		} else {
1028 			wlan_ipa_send_pkt_to_tl(pm_tx_cb->iface_context,
1029 						pm_tx_cb->ipa_tx_desc);
1030 		}
1031 
1032 		qdf_spin_lock_bh(&ipa_ctx->pm_lock);
1033 	}
1034 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1035 	qdf_wake_lock_release(&ipa_ctx->wake_lock,
1036 			      WIFI_POWER_EVENT_WAKELOCK_IPA);
1037 
1038 	ipa_ctx->stats.num_tx_dequeued += dequeued;
1039 	if (dequeued > ipa_ctx->stats.num_max_pm_queue)
1040 		ipa_ctx->stats.num_max_pm_queue = dequeued;
1041 }
1042 
1043 int wlan_ipa_uc_smmu_map(bool map, uint32_t num_buf, qdf_mem_info_t *buf_arr)
1044 {
1045 	if (!num_buf) {
1046 		ipa_info("No buffers to map/unmap");
1047 		return 0;
1048 	}
1049 
1050 	if (map)
1051 		return qdf_ipa_wdi_create_smmu_mapping(IPA_DEFAULT_HDL,
1052 						       num_buf, buf_arr);
1053 	else
1054 		return qdf_ipa_wdi_release_smmu_mapping(IPA_DEFAULT_HDL,
1055 							num_buf, buf_arr);
1056 	return 0;
1057 }
1058 
1059 static enum wlan_ipa_forward_type
1060 wlan_ipa_rx_intrabss_fwd(struct wlan_ipa_priv *ipa_ctx,
1061 			 struct wlan_ipa_iface_context *iface_ctx,
1062 			 qdf_nbuf_t nbuf)
1063 {
1064 	uint8_t fw_desc;
1065 
1066 	fw_desc = (uint8_t)nbuf->cb[1];
1067 
1068 	return wlan_ipa_intrabss_forward(ipa_ctx, iface_ctx, fw_desc, nbuf);
1069 }
1070 
1071 #endif /* CONFIG_IPA_WDI_UNIFIED_API */
1072 
1073 /**
1074  * wlan_ipa_send_sta_eapol_to_nw() - Send Rx EAPOL pkt for STA to Kernel
1075  * @skb: network buffer
1076  *
1077  * Called when a EAPOL packet is received via IPA Exception path
1078  * before wlan_ipa_setup_iface is done for STA.
1079  *
1080  * Return: 0 on success, err_code for failure.
1081  */
1082 static int wlan_ipa_send_sta_eapol_to_nw(qdf_nbuf_t skb,
1083 					 struct wlan_objmgr_pdev *pdev)
1084 {
1085 	struct wlan_ipa_priv *ipa_ctx;
1086 	struct ethhdr *eh;
1087 	struct wlan_objmgr_vdev *vdev = NULL;
1088 
1089 	ipa_ctx = ipa_pdev_get_priv_obj(pdev);
1090 	if (!ipa_ctx)
1091 		return -EINVAL;
1092 
1093 	eh = (struct ethhdr *)qdf_nbuf_data(skb);
1094 	vdev = wlan_objmgr_get_vdev_by_macaddr_from_pdev(
1095 				pdev, eh->h_dest, WLAN_IPA_ID);
1096 	if (!vdev) {
1097 		ipa_err_rl("Invalid vdev");
1098 		return -EINVAL;
1099 	}
1100 
1101 	if (wlan_vdev_mlme_get_opmode(vdev) != QDF_STA_MODE) {
1102 		ipa_err_rl("device_mode is not STA");
1103 		wlan_objmgr_vdev_release_ref(vdev, WLAN_IPA_ID);
1104 		return -EINVAL;
1105 	}
1106 
1107 	skb->destructor = wlan_ipa_uc_rt_debug_destructor;
1108 
1109 	if (ipa_ctx->send_to_nw)
1110 		ipa_ctx->send_to_nw(skb, vdev->vdev_nif.osdev->wdev->netdev);
1111 
1112 	ipa_ctx->ipa_rx_net_send_count++;
1113 	ipa_ctx->stats.num_rx_no_iface_eapol++;
1114 	wlan_objmgr_vdev_release_ref(vdev, WLAN_IPA_ID);
1115 	return 0;
1116 }
1117 
1118 /**
1119  * wlan_ipa_send_skb_to_network() - Send skb to kernel
1120  * @skb: network buffer
1121  * @iface_ctx: IPA interface context
1122  *
1123  * Called when a network buffer is received which should not be routed
1124  * to the IPA module.
1125  *
1126  * Return: None
1127  */
1128 static void
1129 wlan_ipa_send_skb_to_network(qdf_nbuf_t skb,
1130 			     struct wlan_ipa_iface_context *iface_ctx)
1131 {
1132 	struct wlan_ipa_priv *ipa_ctx;
1133 
1134 	ipa_ctx = iface_ctx->ipa_ctx;
1135 
1136 	if (!iface_ctx->dev) {
1137 		ipa_debug_rl("Invalid interface");
1138 		ipa_ctx->ipa_rx_internal_drop_count++;
1139 		dev_kfree_skb_any(skb);
1140 		return;
1141 	}
1142 
1143 	skb->destructor = wlan_ipa_uc_rt_debug_destructor;
1144 
1145 	if (ipa_ctx->send_to_nw)
1146 		ipa_ctx->send_to_nw(skb, iface_ctx->dev);
1147 
1148 	ipa_ctx->ipa_rx_net_send_count++;
1149 }
1150 
1151 /**
1152  * wlan_ipa_eapol_intrabss_fwd_check() - Check if eapol pkt intrabss fwd is
1153  *  allowed or not
1154  * @ipa_ctx: IPA global context
1155  * @vdev_id: vdev id
1156  * @nbuf: network buffer
1157  *
1158  * Return: true if intrabss fwd is allowed for eapol else false
1159  */
1160 static bool
1161 wlan_ipa_eapol_intrabss_fwd_check(struct wlan_ipa_priv *ipa_ctx,
1162 				  uint8_t vdev_id, qdf_nbuf_t nbuf)
1163 {
1164 	uint8_t *vdev_mac_addr;
1165 
1166 	vdev_mac_addr = cdp_get_vdev_mac_addr(ipa_ctx->dp_soc, vdev_id);
1167 
1168 	if (!vdev_mac_addr)
1169 		return false;
1170 
1171 	if (qdf_mem_cmp(qdf_nbuf_data(nbuf) + QDF_NBUF_DEST_MAC_OFFSET,
1172 			vdev_mac_addr, QDF_MAC_ADDR_SIZE))
1173 		return false;
1174 
1175 	return true;
1176 }
1177 
1178 #ifdef MDM_PLATFORM
1179 static inline void
1180 wlan_ipa_set_sap_client_auth(struct wlan_ipa_priv *ipa_ctx,
1181 			     const uint8_t *peer_mac,
1182 			     uint8_t is_authenticated)
1183 {
1184 	uint8_t idx;
1185 	struct ipa_uc_stas_map *sta_map;
1186 
1187 	for (idx = 0; idx < WLAN_IPA_MAX_STA_COUNT; idx++) {
1188 		sta_map = &ipa_ctx->assoc_stas_map[idx];
1189 		if (sta_map->is_reserved &&
1190 		    qdf_is_macaddr_equal(&sta_map->mac_addr,
1191 					 (struct qdf_mac_addr *)peer_mac)) {
1192 			sta_map->is_authenticated = is_authenticated;
1193 			break;
1194 		}
1195 	}
1196 }
1197 
1198 static inline uint8_t
1199 wlan_ipa_get_sap_client_auth(struct wlan_ipa_priv *ipa_ctx, uint8_t *peer_mac)
1200 {
1201 	uint8_t idx;
1202 	struct ipa_uc_stas_map *sta_map;
1203 
1204 	for (idx = 0; idx < WLAN_IPA_MAX_STA_COUNT; idx++) {
1205 		sta_map = &ipa_ctx->assoc_stas_map[idx];
1206 		if (sta_map->is_reserved &&
1207 		    qdf_is_macaddr_equal(&sta_map->mac_addr,
1208 					 (struct qdf_mac_addr *)peer_mac)) {
1209 			return sta_map->is_authenticated;
1210 		}
1211 	}
1212 
1213 	return false;
1214 }
1215 
1216 #ifdef IPA_WDS_EASYMESH_FEATURE
1217 static inline uint8_t
1218 wlan_ipa_get_peer_auth_state(ol_txrx_soc_handle dp_soc, uint8_t *peer_mac,
1219 			     struct wlan_ipa_iface_context *iface)
1220 {
1221 	uint8_t is_authenticated = false;
1222 	struct cdp_ast_entry_info ast_info = {0};
1223 
1224 	if (ipa_is_wds_enabled()) {
1225 		cdp_peer_get_ast_info_by_soc(dp_soc, peer_mac,
1226 					     &ast_info);
1227 		peer_mac = &ast_info.peer_mac_addr[0];
1228 		is_authenticated = wlan_ipa_get_peer_state(dp_soc,
1229 							   iface->session_id,
1230 							   peer_mac);
1231 	} else {
1232 		is_authenticated = wlan_ipa_get_peer_state(dp_soc,
1233 							   iface->session_id,
1234 							   peer_mac);
1235 	}
1236 
1237 	return is_authenticated;
1238 }
1239 #else
1240 static inline uint8_t
1241 wlan_ipa_get_peer_auth_state(ol_txrx_soc_handle dp_soc, uint8_t *peer_mac,
1242 			     struct wlan_ipa_iface_context *iface)
1243 {
1244 	uint8_t is_authenticated = false;
1245 
1246 	is_authenticated = wlan_ipa_get_peer_state(dp_soc, iface->session_id,
1247 						   peer_mac);
1248 
1249 	return is_authenticated;
1250 }
1251 #endif
1252 
1253 static inline bool
1254 wlan_ipa_is_peer_authenticated(ol_txrx_soc_handle dp_soc,
1255 			       struct wlan_ipa_iface_context *iface,
1256 			       uint8_t *peer_mac)
1257 {
1258 	uint8_t is_authenticated = false;
1259 
1260 	if (iface->device_mode == QDF_SAP_MODE) {
1261 		is_authenticated = wlan_ipa_get_sap_client_auth(iface->ipa_ctx,
1262 								peer_mac);
1263 		if (is_authenticated)
1264 			return is_authenticated;
1265 
1266 		is_authenticated = wlan_ipa_get_peer_auth_state(dp_soc,
1267 								peer_mac,
1268 								iface);
1269 
1270 		if (is_authenticated)
1271 			wlan_ipa_set_sap_client_auth(iface->ipa_ctx,
1272 						     peer_mac,
1273 						     true);
1274 
1275 	} else if (iface->device_mode == QDF_STA_MODE) {
1276 		is_authenticated = iface->is_authenticated;
1277 		if (is_authenticated)
1278 			return is_authenticated;
1279 		is_authenticated = wlan_ipa_get_peer_state(dp_soc,
1280 							   iface->session_id,
1281 							   peer_mac);
1282 		if (is_authenticated)
1283 			iface->is_authenticated = true;
1284 	}
1285 
1286 	return is_authenticated;
1287 }
1288 #else /* !MDM_PLATFORM */
1289 static inline void
1290 wlan_ipa_set_sap_client_auth(struct wlan_ipa_priv *ipa_ctx,
1291 			     const uint8_t *peer_mac,
1292 			     uint8_t is_authenticated)
1293 {}
1294 
1295 static inline bool
1296 wlan_ipa_is_peer_authenticated(ol_txrx_soc_handle dp_soc,
1297 			       struct wlan_ipa_iface_context *iface,
1298 			       uint8_t *peer_mac)
1299 {
1300 	uint8_t is_authenticated = 0;
1301 
1302 	is_authenticated = wlan_ipa_get_peer_state(dp_soc,
1303 						   iface->session_id,
1304 						   peer_mac);
1305 
1306 	return is_authenticated;
1307 }
1308 #endif /* MDM_PLATFORM */
1309 
1310 /**
1311  * __wlan_ipa_w2i_cb() - WLAN to IPA callback handler
1312  * @priv: pointer to private data registered with IPA (we register a
1313  *	pointer to the global IPA context)
1314  * @evt: the IPA event which triggered the callback
1315  * @data: data associated with the event
1316  *
1317  * Return: None
1318  */
1319 static void __wlan_ipa_w2i_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1320 			      unsigned long data)
1321 {
1322 	struct wlan_ipa_priv *ipa_ctx = NULL;
1323 	qdf_nbuf_t skb;
1324 	uint8_t iface_id;
1325 	uint8_t session_id = 0xff;
1326 	struct wlan_ipa_iface_context *iface_context;
1327 	bool is_eapol_wapi = false;
1328 	struct qdf_mac_addr peer_mac_addr = QDF_MAC_ADDR_ZERO_INIT;
1329 
1330 	ipa_ctx = (struct wlan_ipa_priv *)priv;
1331 	if (!ipa_ctx) {
1332 		if (evt == IPA_RECEIVE) {
1333 			skb = (qdf_nbuf_t)data;
1334 			dev_kfree_skb_any(skb);
1335 		}
1336 		return;
1337 	}
1338 
1339 	switch (evt) {
1340 	case IPA_RECEIVE:
1341 		skb = (qdf_nbuf_t) data;
1342 		if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
1343 			session_id = (uint8_t)skb->cb[0];
1344 			iface_id = ipa_ctx->vdev_to_iface[session_id];
1345 			ipa_ctx->stats.num_rx_excep++;
1346 			qdf_nbuf_pull_head(skb, WLAN_IPA_UC_WLAN_CLD_HDR_LEN);
1347 		} else {
1348 			iface_id = WLAN_IPA_GET_IFACE_ID(skb->data);
1349 			qdf_nbuf_pull_head(skb, WLAN_IPA_WLAN_CLD_HDR_LEN);
1350 		}
1351 
1352 		if (iface_id >= WLAN_IPA_MAX_IFACE) {
1353 			ipa_err_rl("Invalid iface_id %u,session_id %x %x %x %x",
1354 				   iface_id, session_id, (uint8_t)skb->cb[1],
1355 				   (uint8_t)skb->cb[2], (uint8_t)skb->cb[3]);
1356 
1357 			if (qdf_nbuf_is_ipv4_eapol_pkt(skb)) {
1358 				ipa_err_rl("EAPOL pkt. Sending to NW!");
1359 				if (!wlan_ipa_send_sta_eapol_to_nw(
1360 						skb, ipa_ctx->pdev))
1361 					break;
1362 			}
1363 			ipa_err_rl("Pkt Dropped!");
1364 			ipa_ctx->ipa_rx_internal_drop_count++;
1365 			dev_kfree_skb_any(skb);
1366 			return;
1367 		}
1368 
1369 		iface_context = &ipa_ctx->iface_context[iface_id];
1370 		if (iface_context->session_id >= WLAN_IPA_MAX_SESSION) {
1371 			ipa_err_rl("session_id of iface_id %u is invalid:%d",
1372 				   iface_id, iface_context->session_id);
1373 			ipa_ctx->ipa_rx_internal_drop_count++;
1374 			dev_kfree_skb_any(skb);
1375 			return;
1376 		}
1377 		iface_context->stats.num_rx_ipa_excep++;
1378 
1379 		if (iface_context->device_mode == QDF_STA_MODE)
1380 			qdf_copy_macaddr(&peer_mac_addr, &iface_context->bssid);
1381 		else if (iface_context->device_mode == QDF_SAP_MODE)
1382 			qdf_mem_copy(&peer_mac_addr.bytes[0],
1383 				     qdf_nbuf_data(skb) +
1384 				     QDF_NBUF_SRC_MAC_OFFSET,
1385 				     QDF_MAC_ADDR_SIZE);
1386 
1387 		if (qdf_nbuf_is_ipv4_eapol_pkt(skb)) {
1388 			is_eapol_wapi = true;
1389 			if (iface_context->device_mode == QDF_SAP_MODE &&
1390 			    !wlan_ipa_eapol_intrabss_fwd_check(ipa_ctx,
1391 					      iface_context->session_id, skb)) {
1392 				ipa_err_rl("EAPOL intrabss fwd drop DA:" QDF_MAC_ADDR_FMT,
1393 					   QDF_MAC_ADDR_REF(qdf_nbuf_data(skb) +
1394 					   QDF_NBUF_DEST_MAC_OFFSET));
1395 				ipa_ctx->ipa_rx_internal_drop_count++;
1396 				dev_kfree_skb_any(skb);
1397 				return;
1398 			}
1399 		} else if (qdf_nbuf_is_ipv4_wapi_pkt(skb)) {
1400 			is_eapol_wapi = true;
1401 		}
1402 
1403 		/*
1404 		 * Check for peer authorized state before allowing
1405 		 * non-EAPOL/WAPI frames to be intrabss forwarded
1406 		 * or submitted to stack.
1407 		 */
1408 		if (!wlan_ipa_is_peer_authenticated(ipa_ctx->dp_soc,
1409 						    iface_context,
1410 						    &peer_mac_addr.bytes[0]) &&
1411 		    !is_eapol_wapi) {
1412 			ipa_err_rl("Non EAPOL/WAPI packet received when peer " QDF_MAC_ADDR_FMT " is unauthorized",
1413 				   QDF_MAC_ADDR_REF(peer_mac_addr.bytes));
1414 			ipa_ctx->ipa_rx_internal_drop_count++;
1415 			dev_kfree_skb_any(skb);
1416 			return;
1417 		}
1418 
1419 		/* Disable to forward Intra-BSS Rx packets when
1420 		 * ap_isolate=1 in hostapd.conf
1421 		 */
1422 		if (!ipa_ctx->disable_intrabss_fwd[iface_context->session_id] &&
1423 		    iface_context->device_mode == QDF_SAP_MODE) {
1424 			/*
1425 			 * When INTRA_BSS_FWD_OFFLOAD is enabled, FW will send
1426 			 * all Rx packets to IPA uC, which need to be forwarded
1427 			 * to other interface.
1428 			 * And, IPA driver will send back to WLAN host driver
1429 			 * through exception pipe with fw_desc field set by FW.
1430 			 * Here we are checking fw_desc field for FORWARD bit
1431 			 * set, and forward to Tx. Then copy to kernel stack
1432 			 * only when DISCARD bit is not set.
1433 			 */
1434 			if (WLAN_IPA_FORWARD_PKT_DISCARD ==
1435 			    wlan_ipa_rx_intrabss_fwd(ipa_ctx, iface_context,
1436 						     skb))
1437 				break;
1438 		} else {
1439 			ipa_debug_rl("Intra-BSS fwd disabled for session_id %u",
1440 				     iface_context->session_id);
1441 		}
1442 
1443 		wlan_ipa_send_skb_to_network(skb, iface_context);
1444 		break;
1445 
1446 	default:
1447 		ipa_err_rl("w2i cb wrong event: 0x%x", evt);
1448 		return;
1449 	}
1450 }
1451 
1452 #ifndef MDM_PLATFORM
1453 /**
1454  * wlan_ipa_w2i_cb() - SSR wrapper for __wlan_ipa_w2i_cb
1455  * @priv: pointer to private data registered with IPA (we register a
1456  *	pointer to the global IPA context)
1457  * @evt: the IPA event which triggered the callback
1458  * @data: data associated with the event
1459  *
1460  * Return: None
1461  */
1462 static void wlan_ipa_w2i_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1463 			    unsigned long data)
1464 {
1465 	struct qdf_op_sync *op_sync;
1466 
1467 	if (qdf_op_protect(&op_sync)) {
1468 		if (evt == IPA_RECEIVE) {
1469 			struct wlan_ipa_priv *ipa_ctx = priv;
1470 			qdf_nbuf_t skb = (qdf_nbuf_t)data;
1471 
1472 			ipa_ctx->ipa_rx_internal_drop_count++;
1473 			dev_kfree_skb_any(skb);
1474 		}
1475 
1476 		return;
1477 	}
1478 
1479 	__wlan_ipa_w2i_cb(priv, evt, data);
1480 
1481 	qdf_op_unprotect(op_sync);
1482 }
1483 #else /* MDM_PLATFORM */
1484 static void wlan_ipa_w2i_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1485 			    unsigned long data)
1486 {
1487 	__wlan_ipa_w2i_cb(priv, evt, data);
1488 }
1489 #endif /* MDM_PLATFORM */
1490 
1491 #if !defined(QCA_LL_TX_FLOW_CONTROL_V2) && !defined(QCA_IPA_LL_TX_FLOW_CONTROL)
1492 
1493 /**
1494  * __wlan_ipa_i2w_cb() - IPA to WLAN callback
1495  * @priv: pointer to private data registered with IPA (we register a
1496  *	pointer to the interface-specific IPA context)
1497  * @evt: the IPA event which triggered the callback
1498  * @data: data associated with the event
1499  *
1500  * Return: None
1501  */
1502 static void __wlan_ipa_i2w_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1503 			      unsigned long data)
1504 {
1505 	struct wlan_ipa_priv *ipa_ctx = NULL;
1506 	qdf_ipa_rx_data_t *ipa_tx_desc;
1507 	struct wlan_ipa_iface_context *iface_context;
1508 	qdf_nbuf_t skb;
1509 	struct wlan_ipa_pm_tx_cb *pm_tx_cb = NULL;
1510 
1511 	iface_context = (struct wlan_ipa_iface_context *)priv;
1512 	ipa_tx_desc = (qdf_ipa_rx_data_t *)data;
1513 	ipa_ctx = iface_context->ipa_ctx;
1514 
1515 	if (evt != IPA_RECEIVE) {
1516 		ipa_err_rl("Event is not IPA_RECEIVE");
1517 		ipa_free_skb(ipa_tx_desc);
1518 		iface_context->stats.num_tx_drop++;
1519 		return;
1520 	}
1521 
1522 	skb = QDF_IPA_RX_DATA_SKB(ipa_tx_desc);
1523 
1524 	/*
1525 	 * If PROD resource is not requested here then there may be cases where
1526 	 * IPA hardware may be clocked down because of not having proper
1527 	 * dependency graph between WLAN CONS and modem PROD pipes. Adding the
1528 	 * workaround to request PROD resource while data is going over CONS
1529 	 * pipe to prevent the IPA hardware clockdown.
1530 	 */
1531 	wlan_ipa_wdi_rm_request(ipa_ctx);
1532 
1533 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
1534 	/*
1535 	 * If host is still suspended then queue the packets and these will be
1536 	 * drained later when resume completes. When packet is arrived here and
1537 	 * host is suspended, this means that there is already resume is in
1538 	 * progress.
1539 	 */
1540 	if (ipa_ctx->suspended) {
1541 		qdf_mem_zero(skb->cb, sizeof(skb->cb));
1542 		pm_tx_cb = (struct wlan_ipa_pm_tx_cb *)skb->cb;
1543 		pm_tx_cb->iface_context = iface_context;
1544 		pm_tx_cb->ipa_tx_desc = ipa_tx_desc;
1545 		qdf_nbuf_queue_add(&ipa_ctx->pm_queue_head, skb);
1546 		ipa_ctx->stats.num_tx_queued++;
1547 
1548 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1549 		return;
1550 	}
1551 
1552 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1553 
1554 	/*
1555 	 * If we are here means, host is not suspended, wait for the work queue
1556 	 * to finish.
1557 	 */
1558 	qdf_flush_work(&ipa_ctx->pm_work);
1559 
1560 	return wlan_ipa_send_pkt_to_tl(iface_context, ipa_tx_desc);
1561 }
1562 
1563 /**
1564  * wlan_ipa_i2w_cb() - IPA to WLAN callback
1565  * @priv: pointer to private data registered with IPA (we register a
1566  *	pointer to the interface-specific IPA context)
1567  * @evt: the IPA event which triggered the callback
1568  * @data: data associated with the event
1569  *
1570  * Return: None
1571  */
1572 static void wlan_ipa_i2w_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1573 			    unsigned long data)
1574 {
1575 	struct qdf_op_sync *op_sync;
1576 
1577 	if (qdf_op_protect(&op_sync)) {
1578 		qdf_ipa_rx_data_t *ipa_tx_desc = (qdf_ipa_rx_data_t *)data;
1579 		struct wlan_ipa_iface_context *iface_context = priv;
1580 
1581 		ipa_free_skb(ipa_tx_desc);
1582 		iface_context->stats.num_tx_drop++;
1583 
1584 		return;
1585 	}
1586 
1587 	__wlan_ipa_i2w_cb(priv, evt, data);
1588 
1589 	qdf_op_unprotect(op_sync);
1590 }
1591 
1592 #else /* QCA_LL_TX_FLOW_CONTROL_V2 */
1593 
1594 /**
1595  * wlan_ipa_i2w_cb() - IPA to WLAN callback
1596  * @priv: pointer to private data registered with IPA (we register a
1597  *	pointer to the interface-specific IPA context)
1598  * @evt: the IPA event which triggered the callback
1599  * @data: data associated with the event
1600  *
1601  * Return: None
1602  */
1603 static void wlan_ipa_i2w_cb(void *priv, qdf_ipa_dp_evt_type_t evt,
1604 			    unsigned long data)
1605 {
1606 }
1607 
1608 #endif /* QCA_LL_TX_FLOW_CONTROL_V2 */
1609 
1610 QDF_STATUS wlan_ipa_suspend(struct wlan_ipa_priv *ipa_ctx)
1611 {
1612 	/*
1613 	 * Check if IPA is ready for suspend, If we are here means, there is
1614 	 * high chance that suspend would go through but just to avoid any race
1615 	 * condition after suspend started, these checks are conducted before
1616 	 * allowing to suspend.
1617 	 */
1618 	if (atomic_read(&ipa_ctx->tx_ref_cnt))
1619 		return QDF_STATUS_E_AGAIN;
1620 
1621 	if (!wlan_ipa_is_rm_released(ipa_ctx))
1622 		return QDF_STATUS_E_AGAIN;
1623 
1624 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
1625 	ipa_ctx->suspended = true;
1626 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1627 
1628 	if (ipa_ctx->config->ipa_force_voting &&
1629 	    !ipa_ctx->ipa_pipes_down)
1630 		wlan_ipa_set_perf_level(ipa_ctx,
1631 					ipa_ctx->config->bus_bw_high,
1632 					ipa_ctx->config->bus_bw_high);
1633 
1634 	return QDF_STATUS_SUCCESS;
1635 }
1636 
1637 QDF_STATUS wlan_ipa_resume(struct wlan_ipa_priv *ipa_ctx)
1638 {
1639 	qdf_sched_work(0, &ipa_ctx->pm_work);
1640 
1641 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
1642 	ipa_ctx->suspended = false;
1643 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
1644 
1645 	return QDF_STATUS_SUCCESS;
1646 }
1647 
1648 QDF_STATUS wlan_ipa_uc_enable_pipes(struct wlan_ipa_priv *ipa_ctx)
1649 {
1650 	int result;
1651 	QDF_STATUS qdf_status = QDF_STATUS_SUCCESS;
1652 
1653 	ipa_debug("enter");
1654 
1655 	qdf_spin_lock_bh(&ipa_ctx->enable_disable_lock);
1656 	if (ipa_ctx->pipes_enable_in_progress) {
1657 		ipa_warn("IPA Pipes Enable in progress");
1658 		qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1659 		return QDF_STATUS_E_ALREADY;
1660 	}
1661 	ipa_ctx->pipes_enable_in_progress = true;
1662 	qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1663 
1664 	if (qdf_atomic_read(&ipa_ctx->waiting_on_pending_tx))
1665 		wlan_ipa_reset_pending_tx_timer(ipa_ctx);
1666 
1667 	if (qdf_atomic_read(&ipa_ctx->pipes_disabled)) {
1668 		result = cdp_ipa_enable_pipes(ipa_ctx->dp_soc,
1669 					      ipa_ctx->dp_pdev_id,
1670 					      ipa_ctx->hdl);
1671 		if (result) {
1672 			ipa_err("Enable IPA WDI PIPE failed: ret=%d", result);
1673 			qdf_status = QDF_STATUS_E_FAILURE;
1674 			goto end;
1675 		}
1676 		qdf_atomic_set(&ipa_ctx->pipes_disabled, 0);
1677 	}
1678 
1679 	qdf_event_reset(&ipa_ctx->ipa_resource_comp);
1680 
1681 	if (qdf_atomic_read(&ipa_ctx->autonomy_disabled)) {
1682 		cdp_ipa_enable_autonomy(ipa_ctx->dp_soc,
1683 					ipa_ctx->dp_pdev_id);
1684 		qdf_atomic_set(&ipa_ctx->autonomy_disabled, 0);
1685 	}
1686 end:
1687 	qdf_spin_lock_bh(&ipa_ctx->enable_disable_lock);
1688 	if (!qdf_atomic_read(&ipa_ctx->autonomy_disabled) &&
1689 	    !qdf_atomic_read(&ipa_ctx->pipes_disabled))
1690 		ipa_ctx->ipa_pipes_down = false;
1691 
1692 	ipa_ctx->pipes_enable_in_progress = false;
1693 	qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1694 
1695 	ipa_debug("exit: ipa_pipes_down=%d", ipa_ctx->ipa_pipes_down);
1696 	return qdf_status;
1697 }
1698 
1699 QDF_STATUS
1700 wlan_ipa_uc_disable_pipes(struct wlan_ipa_priv *ipa_ctx, bool force_disable)
1701 {
1702 	QDF_STATUS qdf_status = QDF_STATUS_SUCCESS;
1703 
1704 	ipa_debug("enter: force_disable %u autonomy_disabled %u pipes_disabled %u",
1705 		  force_disable,
1706 		  qdf_atomic_read(&ipa_ctx->autonomy_disabled),
1707 		  qdf_atomic_read(&ipa_ctx->pipes_disabled));
1708 
1709 	qdf_spin_lock_bh(&ipa_ctx->enable_disable_lock);
1710 	if (ipa_ctx->ipa_pipes_down || ipa_ctx->pipes_down_in_progress) {
1711 		qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1712 		ipa_info("IPA WDI Pipes are already deactivated");
1713 		return QDF_STATUS_E_ALREADY;
1714 	}
1715 	ipa_ctx->pipes_down_in_progress = true;
1716 	qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1717 
1718 
1719 	if (!qdf_atomic_read(&ipa_ctx->autonomy_disabled)) {
1720 		cdp_ipa_disable_autonomy(ipa_ctx->dp_soc,
1721 					 ipa_ctx->dp_pdev_id);
1722 		qdf_atomic_set(&ipa_ctx->autonomy_disabled, 1);
1723 	}
1724 
1725 	if (!qdf_atomic_read(&ipa_ctx->pipes_disabled)) {
1726 		if (!force_disable) {
1727 			wlan_ipa_set_pending_tx_timer(ipa_ctx);
1728 		} else {
1729 			qdf_status = cdp_ipa_disable_pipes(ipa_ctx->dp_soc,
1730 							   ipa_ctx->dp_pdev_id,
1731 							   ipa_ctx->hdl);
1732 			if (QDF_IS_STATUS_ERROR(qdf_status)) {
1733 				ipa_err("Disable IPA WDI PIPE failed: ret=%u",
1734 					qdf_status);
1735 				qdf_status = QDF_STATUS_E_FAILURE;
1736 				goto end;
1737 			}
1738 			qdf_atomic_set(&ipa_ctx->pipes_disabled, 1);
1739 			wlan_ipa_reset_pending_tx_timer(ipa_ctx);
1740 		}
1741 	}
1742 
1743 end:
1744 	qdf_spin_lock_bh(&ipa_ctx->enable_disable_lock);
1745 	if (qdf_atomic_read(&ipa_ctx->pipes_disabled) &&
1746 	    qdf_atomic_read(&ipa_ctx->autonomy_disabled)) {
1747 		ipa_ctx->ipa_pipes_down = true;
1748 	}
1749 	ipa_ctx->pipes_down_in_progress = false;
1750 	qdf_spin_unlock_bh(&ipa_ctx->enable_disable_lock);
1751 
1752 	ipa_debug("exit: ipa_pipes_down %u autonomy_disabled %u pipes_disabled %u",
1753 		  ipa_ctx->ipa_pipes_down,
1754 		  qdf_atomic_read(&ipa_ctx->autonomy_disabled),
1755 		  qdf_atomic_read(&ipa_ctx->pipes_disabled));
1756 	return qdf_status;
1757 }
1758 
1759 /**
1760  * wlan_ipa_uc_find_add_assoc_sta() - Find associated station
1761  * @ipa_ctx: Global IPA IPA context
1762  * @sta_add: Should station be added
1763  * @mac_addr: mac address of station being queried
1764  *
1765  * Return: true if the station was found
1766  */
1767 static bool wlan_ipa_uc_find_add_assoc_sta(struct wlan_ipa_priv *ipa_ctx,
1768 					   bool sta_add,
1769 					   const uint8_t *mac_addr)
1770 {
1771 	bool sta_found = false;
1772 	uint8_t idx;
1773 
1774 	for (idx = 0; idx < WLAN_IPA_MAX_STA_COUNT; idx++) {
1775 		if ((ipa_ctx->assoc_stas_map[idx].is_reserved) &&
1776 		    (qdf_is_macaddr_equal(
1777 			&ipa_ctx->assoc_stas_map[idx].mac_addr,
1778 			(struct qdf_mac_addr *)mac_addr))) {
1779 			sta_found = true;
1780 			break;
1781 		}
1782 	}
1783 	if (sta_add && sta_found) {
1784 		ipa_err("STA already exist, cannot add: " QDF_MAC_ADDR_FMT,
1785 			QDF_MAC_ADDR_REF(mac_addr));
1786 		return sta_found;
1787 	}
1788 	if (sta_add) {
1789 		for (idx = 0; idx < WLAN_IPA_MAX_STA_COUNT; idx++) {
1790 			if (!ipa_ctx->assoc_stas_map[idx].is_reserved) {
1791 				ipa_ctx->assoc_stas_map[idx].is_reserved = true;
1792 				qdf_mem_copy(&ipa_ctx->assoc_stas_map[idx].
1793 					     mac_addr, mac_addr,
1794 					     QDF_NET_ETH_LEN);
1795 				return sta_found;
1796 			}
1797 		}
1798 	}
1799 	if (!sta_add && !sta_found) {
1800 		ipa_info("STA does not exist, cannot delete: "
1801 			 QDF_MAC_ADDR_FMT, QDF_MAC_ADDR_REF(mac_addr));
1802 		return sta_found;
1803 	}
1804 	if (!sta_add) {
1805 		for (idx = 0; idx < WLAN_IPA_MAX_STA_COUNT; idx++) {
1806 			if ((ipa_ctx->assoc_stas_map[idx].is_reserved) &&
1807 			    (qdf_is_macaddr_equal(
1808 				&ipa_ctx->assoc_stas_map[idx].mac_addr,
1809 				(struct qdf_mac_addr *)mac_addr))) {
1810 				ipa_ctx->assoc_stas_map[idx].is_reserved =
1811 					false;
1812 				qdf_mem_zero(
1813 					&ipa_ctx->assoc_stas_map[idx].mac_addr,
1814 					QDF_NET_ETH_LEN);
1815 				return sta_found;
1816 			}
1817 		}
1818 	}
1819 
1820 	return sta_found;
1821 }
1822 
1823 /**
1824  * wlan_ipa_get_ifaceid() - Get IPA context interface ID
1825  * @ipa_ctx: IPA context
1826  * @session_id: Session ID
1827  *
1828  * Return: None
1829  */
1830 static int wlan_ipa_get_ifaceid(struct wlan_ipa_priv *ipa_ctx,
1831 				uint8_t session_id)
1832 {
1833 	struct wlan_ipa_iface_context *iface_ctx;
1834 	int i;
1835 
1836 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
1837 		iface_ctx = &ipa_ctx->iface_context[i];
1838 		if (iface_ctx->session_id == session_id)
1839 			break;
1840 	}
1841 
1842 	return i;
1843 }
1844 
1845 /**
1846  * wlan_ipa_cleanup_iface() - Cleanup IPA on a given interface
1847  * @iface_context: interface-specific IPA context
1848  *
1849  * Return: None
1850  */
1851 static void wlan_ipa_cleanup_iface(struct wlan_ipa_iface_context *iface_context,
1852 				   const uint8_t *mac_addr)
1853 {
1854 	struct wlan_ipa_priv *ipa_ctx = iface_context->ipa_ctx;
1855 
1856 	ipa_debug("enter");
1857 	ipa_err("net:%pK mode:%d MAC:"QDF_MAC_ADDR_FMT" id:%d",
1858 		iface_context->dev, iface_context->device_mode,
1859 		QDF_MAC_ADDR_REF(mac_addr), iface_context->session_id);
1860 
1861 	if (iface_context->session_id == WLAN_IPA_MAX_SESSION)
1862 		return;
1863 
1864 	if (mac_addr && qdf_mem_cmp(iface_context->mac_addr,
1865 				    mac_addr, QDF_MAC_ADDR_SIZE)) {
1866 		ipa_err("MAC mismatch "QDF_MAC_ADDR_FMT":"QDF_MAC_ADDR_FMT"",
1867 			QDF_MAC_ADDR_REF(mac_addr),
1868 			QDF_MAC_ADDR_REF(iface_context->mac_addr));
1869 	}
1870 
1871 	if (cdp_ipa_cleanup_iface(ipa_ctx->dp_soc,
1872 				  iface_context->dev->name,
1873 				  wlan_ipa_is_ipv6_enabled(ipa_ctx->config),
1874 				  ipa_ctx->hdl)) {
1875 		ipa_err("ipa_cleanup_iface failed");
1876 	}
1877 
1878 	if (iface_context->device_mode == QDF_SAP_MODE)
1879 		ipa_ctx->num_sap_connected--;
1880 
1881 	qdf_spin_lock_bh(&iface_context->interface_lock);
1882 	if (qdf_atomic_read(&iface_context->disconn_count) ==
1883 			qdf_atomic_read(&iface_context->conn_count) - 1) {
1884 		qdf_atomic_inc(&iface_context->disconn_count);
1885 	} else {
1886 		ipa_err("connect/disconnect out of sync");
1887 		QDF_BUG(0);
1888 	}
1889 
1890 	iface_context->is_authenticated = false;
1891 	iface_context->dev = NULL;
1892 	iface_context->device_mode = QDF_MAX_NO_OF_MODE;
1893 	iface_context->session_id = WLAN_IPA_MAX_SESSION;
1894 	qdf_mem_set(iface_context->mac_addr, QDF_MAC_ADDR_SIZE, 0);
1895 	qdf_spin_unlock_bh(&iface_context->interface_lock);
1896 	iface_context->ifa_address = 0;
1897 	qdf_zero_macaddr(&iface_context->bssid);
1898 	if (!iface_context->ipa_ctx->num_iface) {
1899 		ipa_err("NUM INTF 0, Invalid");
1900 		QDF_ASSERT(0);
1901 	}
1902 	iface_context->ipa_ctx->num_iface--;
1903 	ipa_debug("exit: num_iface=%d", iface_context->ipa_ctx->num_iface);
1904 }
1905 
1906 #if !defined(QCA_LL_TX_FLOW_CONTROL_V2) && !defined(QCA_IPA_LL_TX_FLOW_CONTROL)
1907 
1908 /**
1909  * wlan_ipa_nbuf_cb() - IPA TX complete callback
1910  * @skb: packet buffer which was transmitted
1911  *
1912  * Return: None
1913  */
1914 static void wlan_ipa_nbuf_cb(qdf_nbuf_t skb)
1915 {
1916 	struct wlan_ipa_priv *ipa_ctx = gp_ipa;
1917 	qdf_ipa_rx_data_t *ipa_tx_desc;
1918 	struct wlan_ipa_tx_desc *tx_desc;
1919 	uint16_t id;
1920 	struct wlan_objmgr_pdev *pdev;
1921 	struct wlan_objmgr_psoc *psoc;
1922 	qdf_device_t osdev;
1923 
1924 	if (!qdf_nbuf_ipa_owned_get(skb)) {
1925 		dev_kfree_skb_any(skb);
1926 		return;
1927 	}
1928 
1929 	if (!ipa_ctx)
1930 		return;
1931 	pdev = ipa_ctx->pdev;
1932 	psoc = wlan_pdev_get_psoc(pdev);
1933 	osdev = wlan_psoc_get_qdf_dev(psoc);
1934 
1935 	if (osdev && qdf_mem_smmu_s1_enabled(osdev)) {
1936 		if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
1937 			qdf_dma_addr_t paddr = QDF_NBUF_CB_PADDR(skb);
1938 
1939 			qdf_nbuf_mapped_paddr_set(skb,
1940 						  paddr -
1941 						  WLAN_IPA_WLAN_FRAG_HEADER -
1942 						  WLAN_IPA_WLAN_IPA_HEADER);
1943 		}
1944 
1945 		qdf_nbuf_unmap(osdev, skb, QDF_DMA_TO_DEVICE);
1946 	}
1947 
1948 	/* Get Tx desc pointer from SKB CB */
1949 	id = qdf_nbuf_ipa_priv_get(skb);
1950 	tx_desc = &ipa_ctx->tx_desc_pool[id];
1951 	ipa_tx_desc = tx_desc->ipa_tx_desc_ptr;
1952 
1953 	/* Return Tx Desc to IPA */
1954 	qdf_ipa_free_skb(ipa_tx_desc);
1955 
1956 	/* Return to free tx desc list */
1957 	qdf_spin_lock_bh(&ipa_ctx->q_lock);
1958 	tx_desc->ipa_tx_desc_ptr = NULL;
1959 	qdf_list_insert_back(&ipa_ctx->tx_desc_free_list, &tx_desc->node);
1960 	ipa_ctx->stats.num_tx_desc_q_cnt--;
1961 	qdf_spin_unlock_bh(&ipa_ctx->q_lock);
1962 
1963 	ipa_ctx->stats.num_tx_comp_cnt++;
1964 
1965 	qdf_atomic_dec(&ipa_ctx->tx_ref_cnt);
1966 
1967 	wlan_ipa_wdi_rm_try_release(ipa_ctx);
1968 }
1969 
1970 #else /* QCA_LL_TX_FLOW_CONTROL_V2 */
1971 
1972 /**
1973  * wlan_ipa_nbuf_cb() - IPA TX complete callback
1974  * @skb: packet buffer which was transmitted
1975  *
1976  * Return: None
1977  */
1978 static void wlan_ipa_nbuf_cb(qdf_nbuf_t skb)
1979 {
1980 	dev_kfree_skb_any(skb);
1981 }
1982 
1983 #endif /* QCA_LL_TX_FLOW_CONTROL_V2 */
1984 
1985 #ifdef IPA_WDI3_TX_TWO_PIPES
1986 #define WLAN_IPA_SESSION_ID_SHIFT 1
1987 static uint8_t wlan_ipa_set_session_id(uint8_t session_id, bool is_2g_iface)
1988 {
1989 	return (session_id << WLAN_IPA_SESSION_ID_SHIFT) | is_2g_iface;
1990 }
1991 #else
1992 static uint8_t wlan_ipa_set_session_id(uint8_t session_id, bool is_2g_iface)
1993 {
1994 	return session_id;
1995 }
1996 #endif
1997 
1998 /**
1999  * wlan_ipa_setup_iface() - Setup IPA on a given interface
2000  * @ipa_ctx: IPA IPA global context
2001  * @net_dev: Interface net device
2002  * @device_mode: Net interface device mode
2003  * @adapter: Interface upon which IPA is being setup
2004  * @session_id: Station ID of the API instance
2005  * @mac_addr: MAC addr of the API instance
2006  * @is_2g_iface: true if Net interface is operating on 2G band, otherwise false
2007  *
2008  * Return: QDF STATUS
2009  */
2010 static QDF_STATUS wlan_ipa_setup_iface(struct wlan_ipa_priv *ipa_ctx,
2011 				       qdf_netdev_t net_dev,
2012 				       uint8_t device_mode,
2013 				       uint8_t session_id,
2014 				       const uint8_t *mac_addr,
2015 				       bool is_2g_iface)
2016 {
2017 	struct wlan_ipa_iface_context *iface_context = NULL;
2018 	int i;
2019 	QDF_STATUS status;
2020 
2021 	ipa_err("net:%pK mode:%d MAC:"QDF_MAC_ADDR_FMT" id:%d",
2022 		net_dev, device_mode, QDF_MAC_ADDR_REF(mac_addr), session_id);
2023 
2024 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2025 		iface_context = &(ipa_ctx->iface_context[i]);
2026 		if (iface_context->dev == net_dev) {
2027 			if (iface_context->device_mode == device_mode) {
2028 				/**
2029 				 * Lower layer may send multiple START_BSS_EVENT
2030 				 * in DFS mode or during channel change.
2031 				 * Since these indications are sent by lower
2032 				 * layer as SAP updates and IPA doesn't have to
2033 				 * do anything for these updates so ignoring!
2034 				 */
2035 				if (device_mode == QDF_SAP_MODE) {
2036 					ipa_debug("found iface %u device_mode %u",
2037 						  i, device_mode);
2038 					return QDF_STATUS_SUCCESS;
2039 				} else if (device_mode == QDF_STA_MODE &&
2040 					   qdf_mem_cmp(
2041 						   iface_context->mac_addr,
2042 						   mac_addr,
2043 						   QDF_MAC_ADDR_SIZE) == 0) {
2044 					ipa_err("same STA iface already connected");
2045 				}
2046 
2047 			}
2048 
2049 			ipa_err("Obsolete iface %u found, device_mode %u, will remove it.",
2050 				i, iface_context->device_mode);
2051 			wlan_ipa_cleanup_iface(iface_context, NULL);
2052 		} else if (iface_context->session_id == session_id) {
2053 			ipa_err("Obsolete iface %u found, net_dev %pK, will remove it.",
2054 				i, iface_context->dev);
2055 			wlan_ipa_cleanup_iface(iface_context, NULL);
2056 		}
2057 	}
2058 
2059 	if (WLAN_IPA_MAX_IFACE == ipa_ctx->num_iface) {
2060 		ipa_err("Max interface reached %d", WLAN_IPA_MAX_IFACE);
2061 		status = QDF_STATUS_E_NOMEM;
2062 		iface_context = NULL;
2063 		QDF_ASSERT(0);
2064 		goto end;
2065 	}
2066 
2067 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2068 		if (ipa_ctx->iface_context[i].session_id ==
2069 						WLAN_IPA_MAX_SESSION) {
2070 			iface_context = &(ipa_ctx->iface_context[i]);
2071 			break;
2072 		}
2073 	}
2074 
2075 	if (!iface_context) {
2076 		ipa_err("All the IPA interfaces are in use");
2077 		status = QDF_STATUS_E_NOMEM;
2078 		QDF_ASSERT(0);
2079 		goto end;
2080 	}
2081 
2082 	qdf_spin_lock_bh(&iface_context->interface_lock);
2083 	if (qdf_atomic_read(&iface_context->conn_count) ==
2084 			qdf_atomic_read(&iface_context->disconn_count)) {
2085 		qdf_atomic_inc(&iface_context->conn_count);
2086 	} else {
2087 		ipa_err("connect/disconnect out of sync");
2088 		QDF_BUG(0);
2089 	}
2090 
2091 	iface_context->dev = net_dev;
2092 	iface_context->device_mode = device_mode;
2093 	iface_context->session_id = session_id;
2094 	qdf_mem_copy(iface_context->mac_addr, mac_addr, QDF_MAC_ADDR_SIZE);
2095 	qdf_spin_unlock_bh(&iface_context->interface_lock);
2096 
2097 	status = cdp_ipa_setup_iface(ipa_ctx->dp_soc, net_dev->name,
2098 				     net_dev->dev_addr,
2099 				     iface_context->prod_client,
2100 				     iface_context->cons_client,
2101 				     wlan_ipa_set_session_id(session_id,
2102 							     is_2g_iface),
2103 				     wlan_ipa_is_ipv6_enabled(ipa_ctx->config),
2104 				     ipa_ctx->hdl);
2105 	if (status != QDF_STATUS_SUCCESS)
2106 		goto end;
2107 
2108 	/* Register IPA Tx desc free callback */
2109 	qdf_nbuf_reg_free_cb(wlan_ipa_nbuf_cb);
2110 
2111 	ipa_ctx->num_iface++;
2112 
2113 	if (device_mode == QDF_SAP_MODE)
2114 		ipa_ctx->num_sap_connected++;
2115 
2116 	ipa_debug("exit: num_iface=%d", ipa_ctx->num_iface);
2117 
2118 	return status;
2119 
2120 end:
2121 	if (iface_context)
2122 		wlan_ipa_cleanup_iface(iface_context, mac_addr);
2123 
2124 	return status;
2125 }
2126 
2127 #if defined(QCA_WIFI_QCA6290) || defined(QCA_WIFI_QCA6390) || \
2128     defined(QCA_WIFI_QCA6490) || defined(QCA_WIFI_QCA6750) || \
2129     defined(QCA_WIFI_WCN7850) || defined(QCA_WIFI_QCN9000) || \
2130     defined(QCA_WIFI_KIWI) || defined(QCA_WIFI_KIWI_V2)
2131 
2132 #ifdef QCA_CONFIG_RPS
2133 void ipa_set_rps(struct wlan_ipa_priv *ipa_ctx, enum QDF_OPMODE mode,
2134 		 bool enable)
2135 {
2136 	struct wlan_ipa_iface_context *iface_ctx;
2137 	wlan_ipa_rps_enable cb = ipa_ctx->rps_enable;
2138 	int i;
2139 
2140 	if (!cb)
2141 		return;
2142 
2143 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2144 		iface_ctx = &ipa_ctx->iface_context[i];
2145 		if (iface_ctx->device_mode == mode)
2146 			cb(iface_ctx->session_id, enable);
2147 	}
2148 }
2149 #endif
2150 
2151 #ifdef QCA_CONFIG_RPS
2152 /**
2153  * wlan_ipa_uc_handle_first_con() - Handle first uC IPA connection
2154  * @ipa_ctx: IPA context
2155  *
2156  * Return: QDF STATUS
2157  */
2158 static QDF_STATUS wlan_ipa_uc_handle_first_con(struct wlan_ipa_priv *ipa_ctx)
2159 {
2160 	ipa_debug("enter");
2161 
2162 	if (qdf_ipa_get_lan_rx_napi() && (ipa_ctx->num_sap_connected > 1)) {
2163 		ipa_debug("Multiple SAP connected. Not enabling pipes. Exit");
2164 		return QDF_STATUS_E_PERM;
2165 	}
2166 
2167 	if (qdf_ipa_get_lan_rx_napi() && ipa_ctx->sta_connected)
2168 		ipa_set_rps(ipa_ctx, QDF_STA_MODE, true);
2169 
2170 	if (wlan_ipa_uc_enable_pipes(ipa_ctx) != QDF_STATUS_SUCCESS) {
2171 		ipa_err("IPA WDI Pipe activation failed");
2172 		return QDF_STATUS_E_BUSY;
2173 	}
2174 
2175 	ipa_debug("exit");
2176 
2177 	return QDF_STATUS_SUCCESS;
2178 }
2179 #else
2180 static QDF_STATUS wlan_ipa_uc_handle_first_con(struct wlan_ipa_priv *ipa_ctx)
2181 {
2182 	ipa_debug("enter");
2183 
2184 	if (wlan_ipa_uc_enable_pipes(ipa_ctx) != QDF_STATUS_SUCCESS) {
2185 		ipa_err("IPA WDI Pipe activation failed");
2186 		return QDF_STATUS_E_BUSY;
2187 	}
2188 
2189 	ipa_debug("exit");
2190 
2191 	return QDF_STATUS_SUCCESS;
2192 }
2193 #endif
2194 
2195 static
2196 void wlan_ipa_uc_handle_last_discon(struct wlan_ipa_priv *ipa_ctx,
2197 				    bool force_disable)
2198 {
2199 	ipa_debug("enter");
2200 
2201 	wlan_ipa_uc_disable_pipes(ipa_ctx, force_disable);
2202 
2203 	if (qdf_ipa_get_lan_rx_napi() && ipa_ctx->sta_connected)
2204 		ipa_set_rps(ipa_ctx, QDF_STA_MODE, false);
2205 
2206 	ipa_debug("exit: IPA WDI Pipes deactivated");
2207 }
2208 
2209 bool wlan_ipa_is_fw_wdi_activated(struct wlan_ipa_priv *ipa_ctx)
2210 {
2211 	return !ipa_ctx->ipa_pipes_down;
2212 }
2213 
2214 /* Time(ms) to wait for pending TX comps after last SAP client disconnects */
2215 #define WLAN_IPA_TX_PENDING_TIMEOUT_MS 15000
2216 
2217 static void wlan_ipa_set_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx)
2218 {
2219 	ipa_ctx->pending_tx_start_ticks = qdf_system_ticks();
2220 	qdf_atomic_set(&ipa_ctx->waiting_on_pending_tx, 1);
2221 	ipa_info("done. pending_tx_start_ticks %llu wait_on_pending %u",
2222 		 ipa_ctx->pending_tx_start_ticks,
2223 		 qdf_atomic_read(&ipa_ctx->waiting_on_pending_tx));
2224 }
2225 
2226 bool wlan_ipa_is_tx_pending(struct wlan_ipa_priv *ipa_ctx)
2227 {
2228 	bool ret = false;
2229 	uint64_t diff_ms = 0;
2230 	uint64_t current_ticks = 0;
2231 
2232 	if (!ipa_ctx) {
2233 		ipa_err("IPA private context is NULL");
2234 		return false;
2235 	}
2236 
2237 	if (!qdf_atomic_read(&ipa_ctx->waiting_on_pending_tx)) {
2238 		ipa_debug("nothing pending");
2239 		return false;
2240 	}
2241 
2242 	current_ticks = qdf_system_ticks();
2243 
2244 	diff_ms = qdf_system_ticks_to_msecs(current_ticks -
2245 					    ipa_ctx->pending_tx_start_ticks);
2246 
2247 	if (diff_ms < WLAN_IPA_TX_PENDING_TIMEOUT_MS) {
2248 		ret = true;
2249 	} else {
2250 		ipa_debug("disabling pipes");
2251 		wlan_ipa_uc_disable_pipes(ipa_ctx, true);
2252 	}
2253 
2254 	ipa_debug("diff_ms %llu pending_tx_start_ticks %llu current_ticks %llu wait_on_pending %u",
2255 		  diff_ms, ipa_ctx->pending_tx_start_ticks, current_ticks,
2256 		  qdf_atomic_read(&ipa_ctx->waiting_on_pending_tx));
2257 
2258 	return ret;
2259 }
2260 
2261 static void wlan_ipa_reset_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx)
2262 {
2263 	ipa_ctx->pending_tx_start_ticks = 0;
2264 	qdf_atomic_set(&ipa_ctx->waiting_on_pending_tx, 0);
2265 	ipa_info("done");
2266 }
2267 
2268 #else
2269 
2270 /**
2271  * wlan_ipa_uc_handle_first_con() - Handle first uC IPA connection
2272  * @ipa_ctx: IPA context
2273  *
2274  * Return: QDF STATUS
2275  */
2276 static QDF_STATUS wlan_ipa_uc_handle_first_con(struct wlan_ipa_priv *ipa_ctx)
2277 {
2278 	ipa_debug("enter");
2279 
2280 	ipa_ctx->activated_fw_pipe = 0;
2281 	ipa_ctx->resource_loading = true;
2282 
2283 	/* If RM feature enabled
2284 	 * Request PROD Resource first
2285 	 * PROD resource may return sync or async manners
2286 	 */
2287 	if (wlan_ipa_is_rm_enabled(ipa_ctx->config)) {
2288 		if (!wlan_ipa_wdi_rm_request_resource(ipa_ctx,
2289 						IPA_RM_RESOURCE_WLAN_PROD)) {
2290 			/* RM PROD request sync return
2291 			 * enable pipe immediately
2292 			 */
2293 			if (wlan_ipa_uc_enable_pipes(ipa_ctx)) {
2294 				ipa_err("IPA WDI Pipe activation failed");
2295 				ipa_ctx->resource_loading = false;
2296 				return QDF_STATUS_E_BUSY;
2297 			}
2298 		} else {
2299 			ipa_err("IPA WDI Pipe activation deferred");
2300 		}
2301 	} else {
2302 		/* RM Disabled
2303 		 * Just enabled all the PIPEs
2304 		 */
2305 		if (wlan_ipa_uc_enable_pipes(ipa_ctx)) {
2306 			ipa_err("IPA WDI Pipe activation failed");
2307 			ipa_ctx->resource_loading = false;
2308 			return QDF_STATUS_E_BUSY;
2309 		}
2310 		ipa_ctx->resource_loading = false;
2311 	}
2312 
2313 	ipa_debug("exit");
2314 
2315 	return QDF_STATUS_SUCCESS;
2316 }
2317 
2318 /**
2319  * wlan_ipa_uc_handle_last_discon() - Handle last uC IPA disconnection
2320  * @ipa_ctx: IPA context
2321  * @force_disable: force IPA pipes disablement
2322  *
2323  * Return: None
2324  */
2325 static
2326 void wlan_ipa_uc_handle_last_discon(struct wlan_ipa_priv *ipa_ctx,
2327 				    bool force_disable)
2328 {
2329 	ipa_debug("enter");
2330 
2331 	ipa_ctx->resource_unloading = true;
2332 	qdf_event_reset(&ipa_ctx->ipa_resource_comp);
2333 	ipa_info("Disable FW RX PIPE");
2334 	cdp_ipa_set_active(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id, false, false);
2335 
2336 	ipa_debug("exit: IPA WDI Pipes deactivated");
2337 }
2338 
2339 bool wlan_ipa_is_fw_wdi_activated(struct wlan_ipa_priv *ipa_ctx)
2340 {
2341 	return (WLAN_IPA_UC_NUM_WDI_PIPE == ipa_ctx->activated_fw_pipe);
2342 }
2343 
2344 static inline
2345 void wlan_ipa_set_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx)
2346 {
2347 }
2348 
2349 bool wlan_ipa_is_tx_pending(struct wlan_ipa_priv *ipa_ctx)
2350 {
2351 	return false;
2352 }
2353 
2354 static inline
2355 void wlan_ipa_reset_pending_tx_timer(struct wlan_ipa_priv *ipa_ctx)
2356 {
2357 }
2358 
2359 #endif
2360 
2361 static inline
2362 bool wlan_sap_no_client_connected(struct wlan_ipa_priv *ipa_ctx)
2363 {
2364 	return !(ipa_ctx->sap_num_connected_sta);
2365 }
2366 
2367 static inline
2368 bool wlan_sta_is_connected(struct wlan_ipa_priv *ipa_ctx)
2369 {
2370 	return ipa_ctx->sta_connected;
2371 }
2372 
2373 static inline
2374 bool wlan_ipa_uc_is_loaded(struct wlan_ipa_priv *ipa_ctx)
2375 {
2376 	return ipa_ctx->uc_loaded;
2377 }
2378 
2379 #ifdef INTRA_BSS_FWD_OFFLOAD
2380 /**
2381  * wlan_ipa_intrabss_enable_disable() - wdi intrabss enable/disable notify to fw
2382  * @ipa_ctx: global IPA context
2383  * @offload_type: MCC or SCC
2384  * @session_id: Session Id
2385  * @enable: intrabss enable or disable
2386  *
2387  * Return: none
2388  */
2389 static void wlan_ipa_intrabss_enable_disable(struct wlan_ipa_priv *ipa_ctx,
2390 					     uint8_t session_id,
2391 					     bool enable)
2392 {
2393 	struct ipa_intrabss_control_params intrabss_req = {0};
2394 	uint32_t intra_bss_fwd = 0;
2395 
2396 	if (!enable || ipa_ctx->disable_intrabss_fwd[session_id]) {
2397 		ipa_debug("%s: ipa_offload->enable=%d, rx_fwd_disabled=%d",
2398 			  __func__, enable,
2399 			  ipa_ctx->disable_intrabss_fwd[session_id]);
2400 		intra_bss_fwd = 1;
2401 	}
2402 
2403 	intrabss_req.vdev_id = session_id;
2404 	intrabss_req.enable = intra_bss_fwd;
2405 
2406 	if (QDF_STATUS_SUCCESS !=
2407 	    ipa_send_intrabss_enable_disable(ipa_ctx->pdev, &intrabss_req)) {
2408 		ipa_err("intrabss offload vdev_id=%d, enable=%d failure",
2409 			session_id, intra_bss_fwd);
2410 	}
2411 }
2412 #else
2413 static inline
2414 void wlan_ipa_intrabss_enable_disable(struct wlan_ipa_priv *ipa_ctx,
2415 				      uint8_t session_id,
2416 				      bool enable)
2417 {}
2418 #endif
2419 
2420 /**
2421  * wlan_ipa_uc_offload_enable_disable() - wdi enable/disable notify to fw
2422  * @ipa_ctx: global IPA context
2423  * @offload_type: MCC or SCC
2424  * @session_id: Session Id
2425  * @enable: TX offload enable or disable
2426  *
2427  * Return: none
2428  */
2429 static void wlan_ipa_uc_offload_enable_disable(struct wlan_ipa_priv *ipa_ctx,
2430 					       uint32_t offload_type,
2431 					       uint8_t session_id,
2432 					       bool enable)
2433 {
2434 
2435 	struct ipa_uc_offload_control_params req = {0};
2436 
2437 	if (session_id >= WLAN_IPA_MAX_SESSION) {
2438 		ipa_err("invalid session id: %d", session_id);
2439 		return;
2440 	}
2441 
2442 	if (enable == ipa_ctx->vdev_offload_enabled[session_id]) {
2443 		ipa_info("IPA offload status is already set");
2444 		ipa_info("offload_type=%d, vdev_id=%d, enable=%d",
2445 			 offload_type, session_id, enable);
2446 		return;
2447 	}
2448 
2449 	ipa_info("offload_type=%d, session_id=%d, enable=%d",
2450 		 offload_type, session_id, enable);
2451 
2452 	req.offload_type = offload_type;
2453 	req.vdev_id = session_id;
2454 	req.enable = enable;
2455 
2456 	if (QDF_STATUS_SUCCESS !=
2457 	    ipa_send_uc_offload_enable_disable(ipa_ctx->pdev, &req)) {
2458 		ipa_err("Fail to enable IPA offload");
2459 		ipa_err("offload type=%d, vdev_id=%d, enable=%d",
2460 			offload_type, session_id, enable);
2461 	} else {
2462 		ipa_ctx->vdev_offload_enabled[session_id] = enable;
2463 	}
2464 
2465 	wlan_ipa_intrabss_enable_disable(ipa_ctx, session_id, enable);
2466 }
2467 
2468 #ifdef WDI3_STATS_BW_MONITOR
2469 static void wlan_ipa_uc_bw_monitor(struct wlan_ipa_priv *ipa_ctx, bool stop)
2470 {
2471 	qdf_ipa_wdi_bw_info_t bw_info;
2472 	uint32_t bw_low = ipa_ctx->config->ipa_bw_low;
2473 	uint32_t bw_medium = ipa_ctx->config->ipa_bw_medium;
2474 	uint32_t bw_high = ipa_ctx->config->ipa_bw_high;
2475 	int ret;
2476 
2477 	bw_info.num = WLAN_IPA_UC_BW_MONITOR_LEVEL;
2478 	/* IPA uc will mobitor three bw levels for wlan client */
2479 	QDF_IPA_WDI_BW_INFO_THRESHOLD_LEVEL_1(&bw_info) = bw_low;
2480 	QDF_IPA_WDI_BW_INFO_THRESHOLD_LEVEL_2(&bw_info) = bw_medium;
2481 	QDF_IPA_WDI_BW_INFO_THRESHOLD_LEVEL_3(&bw_info) = bw_high;
2482 	QDF_IPA_WDI_BW_INFO_START_STOP(&bw_info) = stop;
2483 
2484 	ret = qdf_ipa_uc_bw_monitor(&bw_info);
2485 	if (ret)
2486 		ipa_err("ipa uc bw monitor fails");
2487 
2488 	if (!stop) {
2489 		cdp_ipa_set_perf_level(ipa_ctx->dp_soc,
2490 				       QDF_IPA_CLIENT_WLAN2_CONS,
2491 				       ipa_ctx->config->ipa_bw_low);
2492 		ipa_ctx->curr_bw_level = WLAN_IPA_BW_LEVEL_LOW;
2493 	}
2494 
2495 	ipa_debug("ipa uc bw monitor %s", stop ? "stop" : "start");
2496 }
2497 #else
2498 static inline
2499 void wlan_ipa_uc_bw_monitor(struct wlan_ipa_priv *ipa_ctx, bool stop)
2500 {
2501 }
2502 #endif
2503 
2504 /**
2505  * wlan_ipa_send_msg() - Allocate and send message to IPA
2506  * @net_dev: Interface net device
2507  * @type: event enum of type ipa_wlan_event
2508  * @mac_address: MAC address associated with the event
2509  *
2510  * Return: QDF STATUS
2511  */
2512 static QDF_STATUS wlan_ipa_send_msg(qdf_netdev_t net_dev,
2513 				    qdf_ipa_wlan_event type,
2514 				    const uint8_t *mac_addr)
2515 {
2516 	qdf_ipa_msg_meta_t meta;
2517 	qdf_ipa_wlan_msg_t *msg;
2518 
2519 	QDF_IPA_MSG_META_MSG_LEN(&meta) = sizeof(qdf_ipa_wlan_msg_t);
2520 
2521 	msg = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(&meta));
2522 	if (!msg)
2523 		return QDF_STATUS_E_NOMEM;
2524 
2525 	QDF_IPA_SET_META_MSG_TYPE(&meta, type);
2526 	strlcpy(QDF_IPA_WLAN_MSG_NAME(msg), net_dev->name, IPA_RESOURCE_NAME_MAX);
2527 	qdf_mem_copy(QDF_IPA_WLAN_MSG_MAC_ADDR(msg), mac_addr, QDF_NET_ETH_LEN);
2528 	QDF_IPA_WLAN_MSG_NETDEV_IF_ID(msg) = net_dev->ifindex;
2529 
2530 	ipa_debug("%s: Evt: %d", QDF_IPA_WLAN_MSG_NAME(msg), QDF_IPA_MSG_META_MSG_TYPE(&meta));
2531 
2532 	if (qdf_ipa_send_msg(&meta, msg, wlan_ipa_msg_free_fn)) {
2533 		ipa_err("%s: Evt: %d fail",
2534 			QDF_IPA_WLAN_MSG_NAME(msg),
2535 			QDF_IPA_MSG_META_MSG_TYPE(&meta));
2536 		qdf_mem_free(msg);
2537 		return QDF_STATUS_E_FAILURE;
2538 	}
2539 
2540 	return QDF_STATUS_SUCCESS;
2541 }
2542 
2543 #ifdef QCA_CONFIG_RPS
2544 void wlan_ipa_handle_multiple_sap_evt(struct wlan_ipa_priv *ipa_ctx,
2545 				      qdf_ipa_wlan_event type)
2546 {
2547 	struct wlan_ipa_iface_context *iface_ctx;
2548 	int i;
2549 
2550 	if (type ==  QDF_IPA_AP_DISCONNECT) {
2551 		ipa_debug("Multiple SAP disconnecting. Enabling IPA");
2552 
2553 		if (ipa_ctx->sap_num_connected_sta > 0)
2554 			wlan_ipa_uc_handle_first_con(ipa_ctx);
2555 
2556 		for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2557 			iface_ctx = &ipa_ctx->iface_context[i];
2558 
2559 			if (iface_ctx->device_mode == QDF_SAP_MODE) {
2560 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
2561 							WMI_AP_RX_DATA_OFFLOAD,
2562 							iface_ctx->session_id,
2563 							true);
2564 				break;
2565 			}
2566 		}
2567 	} else if (type ==  QDF_IPA_AP_CONNECT) {
2568 		ipa_debug("Multiple SAP connected. Disabling IPA");
2569 
2570 		for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2571 			iface_ctx = &ipa_ctx->iface_context[i];
2572 
2573 			if (iface_ctx->device_mode == QDF_SAP_MODE) {
2574 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
2575 							WMI_AP_RX_DATA_OFFLOAD,
2576 							iface_ctx->session_id,
2577 							false);
2578 			}
2579 		}
2580 
2581 		if (!ipa_ctx->ipa_pipes_down)
2582 			wlan_ipa_uc_handle_last_discon(ipa_ctx, true);
2583 	}
2584 }
2585 #endif
2586 
2587 static inline void
2588 wlan_ipa_save_bssid_iface_ctx(struct wlan_ipa_priv *ipa_ctx, uint8_t iface_id,
2589 			      const uint8_t *mac_addr)
2590 {
2591 	qdf_mem_copy(ipa_ctx->iface_context[iface_id].bssid.bytes,
2592 		     mac_addr, QDF_MAC_ADDR_SIZE);
2593 }
2594 
2595 #ifdef IPA_WDS_EASYMESH_FEATURE
2596 /** wlan_ipa_set_peer_id() - Set ta_peer_id in IPA
2597  * @ipa_ctx: ipa context
2598  * @meta: Meta data for IPA
2599  * @net_dev: Interface net device
2600  * @type: WLAN IPA event
2601  * @mac_addr: mac_addr of peer
2602  *
2603  * Return: QDF STATUS
2604  */
2605 static QDF_STATUS
2606 wlan_ipa_set_peer_id(struct wlan_ipa_priv *ipa_ctx,
2607 		     qdf_ipa_msg_meta_t *meta,
2608 		     qdf_netdev_t net_dev,
2609 		     qdf_ipa_wlan_event type,
2610 		     const uint8_t *mac_addr)
2611 {
2612 	uint8_t ta_peer_id;
2613 	struct cdp_ast_entry_info peer_ast_info = {0};
2614 	struct cdp_soc_t *cdp_soc;
2615 	qdf_ipa_wlan_msg_ex_t *msg_ex;
2616 	bool status;
2617 
2618 	QDF_IPA_MSG_META_MSG_LEN(meta) =
2619 		(sizeof(qdf_ipa_wlan_msg_ex_t) +
2620 		 sizeof(qdf_ipa_wlan_hdr_attrib_val_t) *
2621 		 IPA_TA_PEER_ID_ATTRI);
2622 
2623 	msg_ex = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(meta));
2624 	if (!msg_ex)
2625 		return QDF_STATUS_E_NOMEM;
2626 
2627 	strlcpy(msg_ex->name, net_dev->name, IPA_RESOURCE_NAME_MAX);
2628 	msg_ex->num_of_attribs = IPA_TA_PEER_ID_ATTRI;
2629 	ipa_info("Num of attribute set to: %d", IPA_TA_PEER_ID_ATTRI);
2630 
2631 	msg_ex->attribs[0].attrib_type = WLAN_HDR_ATTRIB_MAC_ADDR;
2632 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
2633 		msg_ex->attribs[0].offset =
2634 			WLAN_IPA_UC_WLAN_HDR_DES_MAC_OFFSET;
2635 	} else {
2636 		msg_ex->attribs[0].offset =
2637 			WLAN_IPA_WLAN_HDR_DES_MAC_OFFSET;
2638 	}
2639 	memcpy(msg_ex->attribs[0].u.mac_addr, mac_addr, IPA_MAC_ADDR_SIZE);
2640 
2641 	msg_ex->attribs[1].attrib_type = WLAN_HDR_ATTRIB_TA_PEER_ID;
2642 	cdp_soc = (struct cdp_soc_t *)ipa_ctx->dp_soc;
2643 	status = cdp_peer_get_ast_info_by_soc(cdp_soc,
2644 					      msg_ex->attribs[0].u.mac_addr,
2645 					      &peer_ast_info);
2646 
2647 	if (!status) {
2648 		qdf_mem_free(msg_ex);
2649 		return QDF_STATUS_E_FAILURE;
2650 	}
2651 
2652 	ta_peer_id = peer_ast_info.peer_id;
2653 	ipa_info("ta_peer_id set to: %d", ta_peer_id);
2654 	msg_ex->attribs[1].u.ta_peer_id = ta_peer_id;
2655 
2656 	if (qdf_ipa_send_msg(meta, msg_ex, wlan_ipa_msg_free_fn)) {
2657 		ipa_info("%s: Evt: %d send ipa msg fail",
2658 			 net_dev->name, type);
2659 		qdf_mem_free(msg_ex);
2660 		return QDF_STATUS_E_FAILURE;
2661 	}
2662 	ipa_ctx->stats.num_send_msg++;
2663 
2664 	return QDF_STATUS_SUCCESS;
2665 }
2666 #else
2667 static QDF_STATUS
2668 wlan_ipa_set_peer_id(struct wlan_ipa_priv *ipa_ctx,
2669 		     qdf_ipa_msg_meta_t *meta,
2670 		     qdf_netdev_t net_dev,
2671 		     qdf_ipa_wlan_event type,
2672 		     const uint8_t *mac_addr)
2673 {
2674 	qdf_ipa_wlan_msg_ex_t *msg_ex;
2675 
2676 	QDF_IPA_MSG_META_MSG_LEN(meta) =
2677 		(sizeof(qdf_ipa_wlan_msg_ex_t) +
2678 		 sizeof(qdf_ipa_wlan_hdr_attrib_val_t));
2679 
2680 	msg_ex = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(meta));
2681 	if (!msg_ex)
2682 		return QDF_STATUS_E_NOMEM;
2683 
2684 	strlcpy(msg_ex->name, net_dev->name, IPA_RESOURCE_NAME_MAX);
2685 	msg_ex->num_of_attribs = 1;
2686 	msg_ex->attribs[0].attrib_type = WLAN_HDR_ATTRIB_MAC_ADDR;
2687 
2688 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
2689 		msg_ex->attribs[0].offset =
2690 			WLAN_IPA_UC_WLAN_HDR_DES_MAC_OFFSET;
2691 	} else {
2692 		msg_ex->attribs[0].offset = WLAN_IPA_WLAN_HDR_DES_MAC_OFFSET;
2693 	}
2694 	memcpy(msg_ex->attribs[0].u.mac_addr, mac_addr, IPA_MAC_ADDR_SIZE);
2695 
2696 	if (qdf_ipa_send_msg(meta, msg_ex, wlan_ipa_msg_free_fn)) {
2697 		ipa_info("%s: Evt: %d send ipa msg fail",
2698 			 net_dev->name, type);
2699 		qdf_mem_free(msg_ex);
2700 		return QDF_STATUS_E_FAILURE;
2701 	}
2702 	ipa_ctx->stats.num_send_msg++;
2703 
2704 	return QDF_STATUS_SUCCESS;
2705 }
2706 #endif
2707 
2708 /**
2709  * __wlan_ipa_wlan_evt() - IPA event handler
2710  * @net_dev: Interface net device
2711  * @device_mode: Net interface device mode
2712  * @session_id: session id for the event
2713  * @type: event enum of type ipa_wlan_event
2714  * @mac_address: MAC address associated with the event
2715  * @is_2g_iface: @net_dev is 2G or not for QDF_IPA_STA_CONNECT and
2716  *		 QDF_IPA_AP_CONNECT
2717  *
2718  * This function is meant to be called from within wlan_ipa_ctx.c
2719  *
2720  * Return: QDF STATUS
2721  */
2722 static QDF_STATUS __wlan_ipa_wlan_evt(qdf_netdev_t net_dev, uint8_t device_mode,
2723 				      uint8_t session_id,
2724 				      qdf_ipa_wlan_event type,
2725 				      const uint8_t *mac_addr, bool is_2g_iface,
2726 				      struct wlan_ipa_priv *ipa_obj)
2727 {
2728 	struct wlan_ipa_priv *ipa_ctx;
2729 	struct wlan_ipa_iface_context *iface_ctx = NULL;
2730 	qdf_ipa_msg_meta_t meta;
2731 	qdf_ipa_wlan_msg_t *msg;
2732 	qdf_ipa_wlan_msg_ex_t *msg_ex = NULL;
2733 	int i;
2734 	QDF_STATUS status;
2735 	uint8_t sta_session_id = WLAN_IPA_MAX_SESSION;
2736 	struct wlan_objmgr_pdev *pdev;
2737 	struct wlan_objmgr_psoc *psoc;
2738 	struct wlan_objmgr_vdev *vdev;
2739 	bool ipa_wds = false;
2740 
2741 	ipa_debug("%s: EVT: %d, MAC: "QDF_MAC_ADDR_FMT", session_id: %u",
2742 		  net_dev->name, type, QDF_MAC_ADDR_REF(mac_addr), session_id);
2743 
2744 	if (type >= QDF_IPA_WLAN_EVENT_MAX)
2745 		return QDF_STATUS_E_INVAL;
2746 
2747 	ipa_ctx = ipa_obj;
2748 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config) &&
2749 	    !wlan_ipa_uc_sta_is_enabled(ipa_ctx->config) &&
2750 	    (device_mode != QDF_SAP_MODE)) {
2751 		return QDF_STATUS_SUCCESS;
2752 	}
2753 
2754 	pdev = ipa_ctx->pdev;
2755 	psoc = wlan_pdev_get_psoc(pdev);
2756 	vdev = wlan_objmgr_get_vdev_by_id_from_psoc(psoc, session_id,
2757 						    WLAN_IPA_ID);
2758 	QDF_BUG(session_id < WLAN_IPA_MAX_SESSION);
2759 
2760 	if (vdev)
2761 		wlan_objmgr_vdev_release_ref(vdev, WLAN_IPA_ID);
2762 	else
2763 		ipa_err("vdev is NULL, session_id: %u", session_id);
2764 
2765 	if (ipa_ctx->sta_connected) {
2766 		iface_ctx = wlan_ipa_get_iface(ipa_ctx, QDF_STA_MODE);
2767 		if (iface_ctx)
2768 			sta_session_id = iface_ctx->session_id;
2769 		else
2770 			ipa_err("sta iface_ctx is NULL");
2771 	}
2772 
2773 	/*
2774 	 * During IPA UC resource loading/unloading new events can be issued.
2775 	 */
2776 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config) &&
2777 	    (ipa_ctx->resource_loading || ipa_ctx->resource_unloading)) {
2778 		unsigned int pending_event_count;
2779 		struct wlan_ipa_uc_pending_event *pending_event = NULL;
2780 
2781 		ipa_info("Event:%d IPA resource %s inprogress", type,
2782 			 ipa_ctx->resource_loading ?
2783 			 "load" : "unload");
2784 
2785 		/* Wait until completion of the long/unloading */
2786 		status = qdf_wait_for_event_completion(
2787 				&ipa_ctx->ipa_resource_comp,
2788 				IPA_RESOURCE_COMP_WAIT_TIME);
2789 		if (status != QDF_STATUS_SUCCESS) {
2790 			/*
2791 			 * If timed out, store the events separately and
2792 			 * handle them later.
2793 			 */
2794 			ipa_info("IPA resource %s timed out",
2795 				  ipa_ctx->resource_loading ?
2796 				  "load" : "unload");
2797 
2798 			if (type == QDF_IPA_AP_DISCONNECT) {
2799 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
2800 						WMI_AP_RX_DATA_OFFLOAD,
2801 						session_id, false);
2802 			} else if (type == QDF_IPA_CLIENT_CONNECT_EX &&
2803 				   wlan_sap_no_client_connected(ipa_ctx)) {
2804 				if (wlan_sta_is_connected(ipa_ctx) &&
2805 				    wlan_ipa_uc_is_loaded(ipa_ctx) &&
2806 				    wlan_ipa_uc_sta_is_enabled(ipa_ctx->
2807 							       config) &&
2808 				    !wlan_ipa_is_sta_only_offload_enabled()) {
2809 					wlan_ipa_uc_offload_enable_disable(
2810 							ipa_ctx,
2811 							WMI_STA_RX_DATA_OFFLOAD,
2812 							sta_session_id, true);
2813 				}
2814 			}
2815 
2816 			qdf_mutex_acquire(&ipa_ctx->ipa_lock);
2817 
2818 			pending_event_count =
2819 				qdf_list_size(&ipa_ctx->pending_event);
2820 			if (pending_event_count >=
2821 			    WLAN_IPA_MAX_PENDING_EVENT_COUNT) {
2822 				ipa_info("Reached max pending evt count");
2823 				qdf_list_remove_front(
2824 					&ipa_ctx->pending_event,
2825 					(qdf_list_node_t **)&pending_event);
2826 			} else {
2827 				pending_event =
2828 					(struct wlan_ipa_uc_pending_event *)
2829 					qdf_mem_malloc(sizeof(
2830 					struct wlan_ipa_uc_pending_event));
2831 			}
2832 
2833 			if (!pending_event) {
2834 				qdf_mutex_release(&ipa_ctx->ipa_lock);
2835 				return QDF_STATUS_E_NOMEM;
2836 			}
2837 
2838 			pending_event->net_dev = net_dev;
2839 			pending_event->device_mode = device_mode;
2840 			pending_event->session_id = session_id;
2841 			pending_event->type = type;
2842 			pending_event->is_loading = ipa_ctx->resource_loading;
2843 			qdf_mem_copy(pending_event->mac_addr,
2844 				     mac_addr, QDF_MAC_ADDR_SIZE);
2845 			pending_event->is_2g_iface = is_2g_iface;
2846 			qdf_list_insert_back(&ipa_ctx->pending_event,
2847 					     &pending_event->node);
2848 
2849 			qdf_mutex_release(&ipa_ctx->ipa_lock);
2850 
2851 			/* Cleanup interface */
2852 			if (type == QDF_IPA_STA_DISCONNECT ||
2853 			    type == QDF_IPA_AP_DISCONNECT) {
2854 				for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
2855 					iface_ctx = &ipa_ctx->iface_context[i];
2856 					if (iface_ctx->dev == net_dev) {
2857 						wlan_ipa_cleanup_iface(
2858 								iface_ctx,
2859 								mac_addr);
2860 						break;
2861 					}
2862 				}
2863 
2864 				if (qdf_ipa_get_lan_rx_napi() &&
2865 				    ipa_ctx->num_sap_connected == 1) {
2866 					wlan_ipa_handle_multiple_sap_evt(ipa_ctx,
2867 									 type);
2868 				}
2869 			}
2870 
2871 			return QDF_STATUS_SUCCESS;
2872 		}
2873 		ipa_info("IPA resource %s completed",
2874 			 ipa_ctx->resource_loading ?
2875 			 "load" : "unload");
2876 	}
2877 
2878 	ipa_ctx->stats.event[type]++;
2879 
2880 	QDF_IPA_SET_META_MSG_TYPE(&meta, type);
2881 	switch (type) {
2882 	case QDF_IPA_STA_CONNECT:
2883 		qdf_mutex_acquire(&ipa_ctx->event_lock);
2884 
2885 		/* STA already connected and without disconnect, connect again
2886 		 * This is Roaming scenario, clean up ipa iface first, then add
2887 		 * ipa iface later, sta_connected-- first, sta_connected++
2888 		 * later to reflect real sta number on DUT.
2889 		 */
2890 		if (ipa_ctx->sta_connected) {
2891 			iface_ctx = wlan_ipa_get_iface_by_mode_netdev(
2892 					ipa_ctx, net_dev, QDF_STA_MODE);
2893 			if (iface_ctx) {
2894 				ipa_ctx->sta_connected--;
2895 				wlan_ipa_cleanup_iface(iface_ctx, NULL);
2896 			}
2897 			status = wlan_ipa_send_msg(net_dev,
2898 						   QDF_IPA_STA_DISCONNECT,
2899 						   mac_addr);
2900 			if (status != QDF_STATUS_SUCCESS) {
2901 				ipa_err("QDF_IPA_STA_DISCONNECT send failed %u",
2902 					status);
2903 				qdf_mutex_release(&ipa_ctx->event_lock);
2904 				goto end;
2905 			}
2906 		}
2907 
2908 		status = wlan_ipa_setup_iface(ipa_ctx, net_dev, device_mode,
2909 					      session_id, mac_addr,
2910 					      is_2g_iface);
2911 		if (status != QDF_STATUS_SUCCESS) {
2912 			ipa_err("wlan_ipa_setup_iface failed %u", status);
2913 			qdf_mutex_release(&ipa_ctx->event_lock);
2914 			goto end;
2915 		}
2916 
2917 		ipa_ctx->vdev_to_iface[session_id] =
2918 				wlan_ipa_get_ifaceid(ipa_ctx, session_id);
2919 
2920 		wlan_ipa_save_bssid_iface_ctx(ipa_ctx,
2921 					     ipa_ctx->vdev_to_iface[session_id],
2922 					     mac_addr);
2923 
2924 		if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config) &&
2925 		    (ipa_ctx->sap_num_connected_sta > 0 ||
2926 		     wlan_ipa_is_sta_only_offload_enabled()) &&
2927 		    !ipa_ctx->sta_connected) {
2928 			qdf_mutex_release(&ipa_ctx->event_lock);
2929 			wlan_ipa_uc_offload_enable_disable(ipa_ctx,
2930 				WMI_STA_RX_DATA_OFFLOAD, session_id,
2931 				true);
2932 			qdf_mutex_acquire(&ipa_ctx->event_lock);
2933 			qdf_atomic_set(&ipa_ctx->stats_quota, 1);
2934 		}
2935 
2936 		if (!wlan_ipa_is_sta_only_offload_enabled()) {
2937 			ipa_debug("IPA STA only offload not enabled");
2938 		} else if (ipa_ctx->uc_loaded &&
2939 			   !ipa_ctx->sap_num_connected_sta &&
2940 			   !ipa_ctx->sta_connected) {
2941 			status = wlan_ipa_uc_handle_first_con(ipa_ctx);
2942 			if (status) {
2943 				qdf_mutex_release(&ipa_ctx->event_lock);
2944 				ipa_info("handle 1st conn failed %d", status);
2945 				wlan_ipa_uc_offload_enable_disable(
2946 						ipa_ctx,
2947 						WMI_STA_RX_DATA_OFFLOAD,
2948 						session_id,
2949 						false);
2950 				ipa_ctx->vdev_to_iface[session_id] =
2951 				    WLAN_IPA_MAX_SESSION;
2952 				goto end;
2953 			}
2954 		}
2955 
2956 		ipa_ctx->sta_connected++;
2957 
2958 		if (qdf_ipa_get_lan_rx_napi() && ipa_ctx->sap_num_connected_sta)
2959 			ipa_set_rps_per_vdev(ipa_ctx, session_id, true);
2960 
2961 		qdf_mutex_release(&ipa_ctx->event_lock);
2962 
2963 		ipa_debug("sta_connected=%d vdev_to_iface[%u] %u",
2964 			 ipa_ctx->sta_connected,
2965 			 session_id,
2966 			 ipa_ctx->vdev_to_iface[session_id]);
2967 		break;
2968 
2969 	case QDF_IPA_AP_CONNECT:
2970 		qdf_mutex_acquire(&ipa_ctx->event_lock);
2971 
2972 		/* For DFS channel we get two start_bss event (before and after
2973 		 * CAC). Also when ACS range includes both DFS and non DFS
2974 		 * channels, we could possibly change channel many times due to
2975 		 * RADAR detection and chosen channel may not be a DFS channels.
2976 		 * So dont return error here. Just discard the event.
2977 		 */
2978 		if (ipa_ctx->vdev_to_iface[session_id] !=
2979 				WLAN_IPA_MAX_SESSION) {
2980 			qdf_mutex_release(&ipa_ctx->event_lock);
2981 			return 0;
2982 		}
2983 
2984 		status = wlan_ipa_setup_iface(ipa_ctx, net_dev, device_mode,
2985 					      session_id, mac_addr,
2986 					      is_2g_iface);
2987 		if (status != QDF_STATUS_SUCCESS) {
2988 			qdf_mutex_release(&ipa_ctx->event_lock);
2989 			ipa_err("%s: Evt: %d, Interface setup failed",
2990 				msg_ex->name, QDF_IPA_MSG_META_MSG_TYPE(&meta));
2991 			goto end;
2992 		}
2993 
2994 		if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
2995 			qdf_mutex_release(&ipa_ctx->event_lock);
2996 			if (qdf_ipa_get_lan_rx_napi() &&
2997 			    (ipa_ctx->num_sap_connected > 1)) {
2998 				wlan_ipa_handle_multiple_sap_evt(ipa_ctx, type);
2999 			} else {
3000 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
3001 							WMI_AP_RX_DATA_OFFLOAD,
3002 							session_id, true);
3003 			}
3004 			qdf_mutex_acquire(&ipa_ctx->event_lock);
3005 		}
3006 
3007 		ipa_ctx->vdev_to_iface[session_id] =
3008 				wlan_ipa_get_ifaceid(ipa_ctx, session_id);
3009 		ipa_debug("vdev_to_iface[%u]=%u",
3010 			 session_id,
3011 			 ipa_ctx->vdev_to_iface[session_id]);
3012 		ipa_wds = ipa_ctx->config->ipa_wds;
3013 		qdf_mutex_release(&ipa_ctx->event_lock);
3014 		break;
3015 
3016 	case QDF_IPA_STA_DISCONNECT:
3017 		qdf_mutex_acquire(&ipa_ctx->event_lock);
3018 
3019 		if (!ipa_ctx->sta_connected) {
3020 			struct wlan_ipa_iface_context *iface;
3021 
3022 			qdf_mutex_release(&ipa_ctx->event_lock);
3023 			ipa_info("%s: Evt: %d, STA already disconnected",
3024 				 msg_ex->name,
3025 				 QDF_IPA_MSG_META_MSG_TYPE(&meta));
3026 
3027 			iface = wlan_ipa_get_iface_by_mode_netdev(ipa_ctx,
3028 								  net_dev,
3029 								  QDF_STA_MODE);
3030 			if (iface)
3031 				wlan_ipa_cleanup_iface(iface, mac_addr);
3032 
3033 			return QDF_STATUS_E_INVAL;
3034 		}
3035 
3036 		ipa_ctx->sta_connected--;
3037 
3038 		if (!wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
3039 			ipa_debug("%s: IPA UC OFFLOAD NOT ENABLED",
3040 				  msg_ex->name);
3041 		} else {
3042 			/*
3043 			 * Disable IPA pipes when
3044 			 * 1. STA is the last interface or
3045 			 * 2. STA only offload enabled and no clients connected
3046 			 * to SAP
3047 			 */
3048 			if ((ipa_ctx->num_iface == 1 ||
3049 			     (wlan_ipa_is_sta_only_offload_enabled() &&
3050 			      !ipa_ctx->sap_num_connected_sta)) &&
3051 			    wlan_ipa_is_fw_wdi_activated(ipa_ctx) &&
3052 			    !ipa_ctx->ipa_pipes_down &&
3053 			    (ipa_ctx->resource_unloading == false)) {
3054 				if (wlan_ipa_is_driver_unloading(ipa_ctx)) {
3055 					/*
3056 					 * We disable WDI pipes directly here
3057 					 * since IPA_OPCODE_TX/RX_SUSPEND
3058 					 * message will not be processed when
3059 					 * unloading WLAN driver is in progress
3060 					 */
3061 					wlan_ipa_uc_disable_pipes(ipa_ctx,
3062 								  true);
3063 				} else {
3064 					wlan_ipa_uc_handle_last_discon(ipa_ctx,
3065 								       true);
3066 				}
3067 			}
3068 		}
3069 
3070 		if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config) &&
3071 		    (ipa_ctx->sap_num_connected_sta > 0 ||
3072 		     wlan_ipa_is_sta_only_offload_enabled())) {
3073 			qdf_atomic_set(&ipa_ctx->stats_quota, 0);
3074 			qdf_mutex_release(&ipa_ctx->event_lock);
3075 			wlan_ipa_uc_offload_enable_disable(ipa_ctx,
3076 				WMI_STA_RX_DATA_OFFLOAD, session_id, false);
3077 			qdf_mutex_acquire(&ipa_ctx->event_lock);
3078 		}
3079 
3080 		ipa_ctx->vdev_to_iface[session_id] = WLAN_IPA_MAX_SESSION;
3081 		ipa_debug("vdev_to_iface[%u]=%u", session_id,
3082 			  ipa_ctx->vdev_to_iface[session_id]);
3083 
3084 		iface_ctx = wlan_ipa_get_iface_by_mode_netdev(ipa_ctx,
3085 							      net_dev,
3086 							      QDF_STA_MODE);
3087 		if (iface_ctx)
3088 			wlan_ipa_cleanup_iface(iface_ctx, mac_addr);
3089 
3090 		if (qdf_ipa_get_lan_rx_napi() && ipa_ctx->sap_num_connected_sta)
3091 			ipa_set_rps_per_vdev(ipa_ctx, session_id, false);
3092 
3093 		qdf_mutex_release(&ipa_ctx->event_lock);
3094 
3095 		ipa_debug("sta_connected=%d", ipa_ctx->sta_connected);
3096 		break;
3097 
3098 	case QDF_IPA_AP_DISCONNECT:
3099 		qdf_mutex_acquire(&ipa_ctx->event_lock);
3100 
3101 		if ((ipa_ctx->num_iface == 1) &&
3102 		    wlan_ipa_is_fw_wdi_activated(ipa_ctx) &&
3103 		    !ipa_ctx->ipa_pipes_down &&
3104 		    (ipa_ctx->resource_unloading == false)) {
3105 			if (wlan_ipa_is_driver_unloading(ipa_ctx)) {
3106 				/*
3107 				 * We disable WDI pipes directly here since
3108 				 * IPA_OPCODE_TX/RX_SUSPEND message will not be
3109 				 * processed when unloading WLAN driver is in
3110 				 * progress
3111 				 */
3112 				wlan_ipa_uc_disable_pipes(ipa_ctx, true);
3113 			} else {
3114 				/*
3115 				 * This shouldn't happen :
3116 				 * No interface left but WDI pipes are still
3117 				 * active - force close WDI pipes
3118 				 */
3119 				ipa_err("No interface left but WDI pipes are still active");
3120 				wlan_ipa_uc_handle_last_discon(ipa_ctx, true);
3121 			}
3122 		}
3123 
3124 		if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
3125 			qdf_mutex_release(&ipa_ctx->event_lock);
3126 			wlan_ipa_uc_offload_enable_disable(ipa_ctx,
3127 				WMI_AP_RX_DATA_OFFLOAD, session_id, false);
3128 			qdf_mutex_acquire(&ipa_ctx->event_lock);
3129 			ipa_ctx->vdev_to_iface[session_id] =
3130 				WLAN_IPA_MAX_SESSION;
3131 			ipa_debug("vdev_to_iface[%u]=%u",
3132 				 session_id,
3133 				 ipa_ctx->vdev_to_iface[session_id]);
3134 		}
3135 
3136 		for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
3137 			iface_ctx = &ipa_ctx->iface_context[i];
3138 			if (iface_ctx->dev == net_dev) {
3139 				wlan_ipa_cleanup_iface(iface_ctx, mac_addr);
3140 				break;
3141 			}
3142 		}
3143 
3144 		if (qdf_ipa_get_lan_rx_napi() &&
3145 		    (ipa_ctx->num_sap_connected == 1))
3146 			wlan_ipa_handle_multiple_sap_evt(ipa_ctx, type);
3147 
3148 		qdf_mutex_release(&ipa_ctx->event_lock);
3149 		break;
3150 
3151 	case QDF_IPA_CLIENT_CONNECT_EX:
3152 		if (!wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
3153 			ipa_debug("%s: Evt: %d, IPA UC OFFLOAD NOT ENABLED",
3154 				  net_dev->name, type);
3155 			return QDF_STATUS_SUCCESS;
3156 		}
3157 
3158 		qdf_mutex_acquire(&ipa_ctx->event_lock);
3159 		if (wlan_ipa_uc_find_add_assoc_sta(ipa_ctx, true,
3160 						   mac_addr)) {
3161 			qdf_mutex_release(&ipa_ctx->event_lock);
3162 			ipa_err("%s: STA found, addr: " QDF_MAC_ADDR_FMT,
3163 				net_dev->name,
3164 				QDF_MAC_ADDR_REF(mac_addr));
3165 			return QDF_STATUS_SUCCESS;
3166 		}
3167 
3168 		/* Enable IPA UC Data PIPEs when first STA connected */
3169 		if (ipa_ctx->sap_num_connected_sta == 0 &&
3170 				ipa_ctx->uc_loaded == true) {
3171 
3172 			if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config) &&
3173 			    ipa_ctx->sta_connected &&
3174 			    !wlan_ipa_is_sta_only_offload_enabled()) {
3175 				qdf_mutex_release(&ipa_ctx->event_lock);
3176 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
3177 							WMI_STA_RX_DATA_OFFLOAD,
3178 							sta_session_id, true);
3179 				qdf_mutex_acquire(&ipa_ctx->event_lock);
3180 				qdf_atomic_set(&ipa_ctx->stats_quota, 1);
3181 			}
3182 
3183 			/*
3184 			 * IPA pipes already enabled if STA only offload
3185 			 * is enabled and STA is connected to remote AP.
3186 			 */
3187 			if (wlan_ipa_is_sta_only_offload_enabled() &&
3188 			    ipa_ctx->sta_connected) {
3189 				ipa_debug("IPA pipes already enabled");
3190 			} else if (wlan_ipa_uc_handle_first_con(ipa_ctx)) {
3191 				ipa_info("%s: handle 1st con fail",
3192 					 net_dev->name);
3193 
3194 				if (wlan_ipa_uc_sta_is_enabled(
3195 					ipa_ctx->config) &&
3196 				    ipa_ctx->sta_connected &&
3197 				    !wlan_ipa_is_sta_only_offload_enabled()) {
3198 					qdf_atomic_set(&ipa_ctx->stats_quota,
3199 						       0);
3200 					qdf_mutex_release(&ipa_ctx->event_lock);
3201 					wlan_ipa_uc_offload_enable_disable(
3202 							ipa_ctx,
3203 							WMI_STA_RX_DATA_OFFLOAD,
3204 							sta_session_id, false);
3205 				} else {
3206 					qdf_mutex_release(&ipa_ctx->event_lock);
3207 				}
3208 
3209 				return QDF_STATUS_E_BUSY;
3210 			}
3211 			wlan_ipa_uc_bw_monitor(ipa_ctx, false);
3212 			ipa_info("first sap client connected");
3213 		}
3214 
3215 		ipa_ctx->sap_num_connected_sta++;
3216 
3217 		qdf_mutex_release(&ipa_ctx->event_lock);
3218 
3219 		QDF_IPA_SET_META_MSG_TYPE(&meta, type);
3220 
3221 		status = wlan_ipa_set_peer_id(ipa_ctx, &meta, net_dev,
3222 					      type, mac_addr);
3223 		if (QDF_IS_STATUS_ERROR(status))
3224 			return QDF_STATUS_E_FAILURE;
3225 
3226 		ipa_debug("sap_num_connected_sta=%d",
3227 			   ipa_ctx->sap_num_connected_sta);
3228 
3229 		return QDF_STATUS_SUCCESS;
3230 
3231 	case WLAN_CLIENT_DISCONNECT:
3232 		if (!wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
3233 			ipa_debug("%s: IPA UC OFFLOAD NOT ENABLED",
3234 				  msg_ex->name);
3235 			return QDF_STATUS_SUCCESS;
3236 		}
3237 
3238 		qdf_mutex_acquire(&ipa_ctx->event_lock);
3239 		wlan_ipa_set_sap_client_auth(ipa_ctx, mac_addr, false);
3240 		if (!ipa_ctx->sap_num_connected_sta) {
3241 			qdf_mutex_release(&ipa_ctx->event_lock);
3242 			ipa_debug("%s: Evt: %d, Client already disconnected",
3243 				  msg_ex->name,
3244 				  QDF_IPA_MSG_META_MSG_TYPE(&meta));
3245 
3246 			return QDF_STATUS_SUCCESS;
3247 		}
3248 		if (!wlan_ipa_uc_find_add_assoc_sta(ipa_ctx, false,
3249 						    mac_addr)) {
3250 			qdf_mutex_release(&ipa_ctx->event_lock);
3251 			ipa_debug("%s: STA NOT found, not valid: "
3252 				QDF_MAC_ADDR_FMT,
3253 				msg_ex->name, QDF_MAC_ADDR_REF(mac_addr));
3254 
3255 			return QDF_STATUS_SUCCESS;
3256 		}
3257 		ipa_ctx->sap_num_connected_sta--;
3258 
3259 		/*
3260 		 * Disable IPA pipes when
3261 		 * 1. last client disconnected and
3262 		 * 2. STA is not connected if STA only offload is enabled
3263 		 */
3264 		if (!ipa_ctx->sap_num_connected_sta &&
3265 		    ipa_ctx->uc_loaded &&
3266 		    !(wlan_ipa_is_sta_only_offload_enabled() &&
3267 		      ipa_ctx->sta_connected)) {
3268 			if ((false == ipa_ctx->resource_unloading) &&
3269 			    wlan_ipa_is_fw_wdi_activated(ipa_ctx) &&
3270 			    !ipa_ctx->ipa_pipes_down) {
3271 				if (wlan_ipa_is_driver_unloading(ipa_ctx)) {
3272 					/*
3273 					 * We disable WDI pipes directly here
3274 					 * since IPA_OPCODE_TX/RX_SUSPEND
3275 					 * message will not be processed when
3276 					 * unloading WLAN driver is in progress
3277 					 */
3278 
3279 					wlan_ipa_uc_bw_monitor(ipa_ctx, true);
3280 					wlan_ipa_uc_disable_pipes(ipa_ctx,
3281 								  true);
3282 				} else {
3283 					/*
3284 					 * If STA is connected, wait for IPA TX
3285 					 * completions before disabling
3286 					 * IPA pipes
3287 					 */
3288 					wlan_ipa_uc_handle_last_discon(ipa_ctx,
3289 								       !ipa_ctx->sta_connected);
3290 					wlan_ipa_uc_bw_monitor(ipa_ctx, true);
3291 				}
3292 				ipa_info("last sap client disconnected");
3293 			}
3294 
3295 			if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config) &&
3296 			    ipa_ctx->sta_connected &&
3297 			    !wlan_ipa_is_sta_only_offload_enabled()) {
3298 				qdf_atomic_set(&ipa_ctx->stats_quota, 0);
3299 				qdf_mutex_release(&ipa_ctx->event_lock);
3300 				wlan_ipa_uc_offload_enable_disable(ipa_ctx,
3301 							WMI_STA_RX_DATA_OFFLOAD,
3302 							sta_session_id, false);
3303 			} else {
3304 				qdf_mutex_release(&ipa_ctx->event_lock);
3305 			}
3306 		} else {
3307 			qdf_mutex_release(&ipa_ctx->event_lock);
3308 		}
3309 
3310 		ipa_debug("sap_num_connected_sta=%d",
3311 			  ipa_ctx->sap_num_connected_sta);
3312 		break;
3313 
3314 	default:
3315 		return QDF_STATUS_SUCCESS;
3316 	}
3317 
3318 	QDF_IPA_MSG_META_MSG_LEN(&meta) = sizeof(qdf_ipa_wlan_msg_t);
3319 	msg = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(&meta));
3320 	if (!msg)
3321 		return QDF_STATUS_E_NOMEM;
3322 
3323 	QDF_IPA_SET_META_MSG_TYPE(&meta, type);
3324 	strlcpy(QDF_IPA_WLAN_MSG_NAME(msg), net_dev->name,
3325 		IPA_RESOURCE_NAME_MAX);
3326 	qdf_mem_copy(QDF_IPA_WLAN_MSG_MAC_ADDR(msg), mac_addr, QDF_NET_ETH_LEN);
3327 	QDF_IPA_WLAN_MSG_NETDEV_IF_ID(msg) = net_dev->ifindex;
3328 
3329 	wlan_ipa_msg_wds_update(ipa_wds, msg);
3330 
3331 	ipa_debug("%s: Evt: %d", QDF_IPA_WLAN_MSG_NAME(msg),
3332 		  QDF_IPA_MSG_META_MSG_TYPE(&meta));
3333 
3334 	if (qdf_ipa_send_msg(&meta, msg, wlan_ipa_msg_free_fn)) {
3335 
3336 		ipa_err("%s: Evt: %d fail",
3337 			QDF_IPA_WLAN_MSG_NAME(msg),
3338 			QDF_IPA_MSG_META_MSG_TYPE(&meta));
3339 		qdf_mem_free(msg);
3340 
3341 		return QDF_STATUS_E_FAILURE;
3342 	}
3343 
3344 	ipa_ctx->stats.num_send_msg++;
3345 
3346 end:
3347 	return QDF_STATUS_SUCCESS;
3348 }
3349 
3350 /**
3351  * wlan_host_to_ipa_wlan_event() - convert wlan_ipa_wlan_event to ipa_wlan_event
3352  * @wlan_ipa_event_type: event to be converted to an ipa_wlan_event
3353  *
3354  * Return: qdf_ipa_wlan_event representing the wlan_ipa_wlan_event
3355  */
3356 static qdf_ipa_wlan_event
3357 wlan_host_to_ipa_wlan_event(enum wlan_ipa_wlan_event wlan_ipa_event_type)
3358 {
3359 	qdf_ipa_wlan_event ipa_event;
3360 
3361 	switch (wlan_ipa_event_type) {
3362 	case WLAN_IPA_CLIENT_CONNECT:
3363 		ipa_event = QDF_IPA_CLIENT_CONNECT;
3364 		break;
3365 	case WLAN_IPA_CLIENT_DISCONNECT:
3366 		ipa_event = QDF_IPA_CLIENT_DISCONNECT;
3367 		break;
3368 	case WLAN_IPA_AP_CONNECT:
3369 		ipa_event = QDF_IPA_AP_CONNECT;
3370 		break;
3371 	case WLAN_IPA_AP_DISCONNECT:
3372 		ipa_event = QDF_IPA_AP_DISCONNECT;
3373 		break;
3374 	case WLAN_IPA_STA_CONNECT:
3375 		ipa_event = QDF_IPA_STA_CONNECT;
3376 		break;
3377 	case WLAN_IPA_STA_DISCONNECT:
3378 		ipa_event = QDF_IPA_STA_DISCONNECT;
3379 		break;
3380 	case WLAN_IPA_CLIENT_CONNECT_EX:
3381 		ipa_event = QDF_IPA_CLIENT_CONNECT_EX;
3382 		break;
3383 	case WLAN_IPA_WLAN_EVENT_MAX:
3384 	default:
3385 		ipa_event =  QDF_IPA_WLAN_EVENT_MAX;
3386 		break;
3387 	}
3388 
3389 	return ipa_event;
3390 }
3391 
3392 #ifdef IPA_P2P_SUPPORT
3393 /**
3394  * wlan_ipa_device_mode_switch() - Switch P2p GO/CLI to SAP/STA mode
3395  * @device_mode: device mode
3396  *
3397  * Return: New device mode after switching
3398  */
3399 static uint8_t wlan_ipa_device_mode_switch(uint8_t device_mode)
3400 {
3401 	switch (device_mode) {
3402 	case QDF_P2P_CLIENT_MODE:
3403 		return QDF_STA_MODE;
3404 	case QDF_P2P_GO_MODE:
3405 		return QDF_SAP_MODE;
3406 	default:
3407 		break;
3408 	}
3409 
3410 	return device_mode;
3411 }
3412 #else
3413 static uint8_t wlan_ipa_device_mode_switch(uint8_t device_mode)
3414 {
3415 	return device_mode;
3416 }
3417 #endif
3418 
3419 /**
3420  * wlan_ipa_wlan_evt() - SSR wrapper for __wlan_ipa_wlan_evt
3421  * @net_dev: Interface net device
3422  * @device_mode: Net interface device mode
3423  * @session_id: session id for the event
3424  * @ipa_event_type: event enum of type wlan_ipa_wlan_event
3425  * @mac_address: MAC address associated with the event
3426  * @is_2g_iface: @net_dev is 2g interface or not
3427  *
3428  * Return: QDF_STATUS
3429  */
3430 QDF_STATUS wlan_ipa_wlan_evt(qdf_netdev_t net_dev, uint8_t device_mode,
3431 		      uint8_t session_id,
3432 		      enum wlan_ipa_wlan_event ipa_event_type,
3433 		      const uint8_t *mac_addr, bool is_2g_iface,
3434 		      struct wlan_ipa_priv *ipa_obj)
3435 {
3436 	qdf_ipa_wlan_event type = wlan_host_to_ipa_wlan_event(ipa_event_type);
3437 	QDF_STATUS status = QDF_STATUS_SUCCESS;
3438 
3439 	device_mode = wlan_ipa_device_mode_switch(device_mode);
3440 
3441 	/* Data path offload only support for STA and SAP mode */
3442 	if ((device_mode == QDF_STA_MODE) ||
3443 	    (device_mode == QDF_SAP_MODE))
3444 		status  = __wlan_ipa_wlan_evt(net_dev, device_mode,
3445 					      session_id, type, mac_addr,
3446 					      is_2g_iface, ipa_obj);
3447 
3448 	return status;
3449 }
3450 
3451 /**
3452  * wlan_ipa_uc_proc_pending_event() - Process IPA uC pending events
3453  * @ipa_ctx: Global IPA IPA context
3454  * @is_loading: Indicate if invoked during loading
3455  *
3456  * Return: None
3457  */
3458 static void
3459 wlan_ipa_uc_proc_pending_event(struct wlan_ipa_priv *ipa_ctx, bool is_loading)
3460 {
3461 	unsigned int pending_event_count;
3462 	struct wlan_ipa_uc_pending_event *pending_event = NULL;
3463 
3464 	pending_event_count = qdf_list_size(&ipa_ctx->pending_event);
3465 	ipa_debug("Pending Event Count %d",  pending_event_count);
3466 	if (!pending_event_count) {
3467 		ipa_debug("No Pending Event");
3468 		return;
3469 	}
3470 
3471 	qdf_list_remove_front(&ipa_ctx->pending_event,
3472 			(qdf_list_node_t **)&pending_event);
3473 	while (pending_event) {
3474 		struct wlan_objmgr_pdev *pdev = ipa_ctx->pdev;
3475 		struct wlan_objmgr_psoc *psoc = wlan_pdev_get_psoc(pdev);
3476 		struct wlan_objmgr_vdev *vdev =
3477 				wlan_objmgr_get_vdev_by_id_from_psoc(psoc,
3478 					pending_event->session_id,
3479 					WLAN_IPA_ID);
3480 		if (pending_event->is_loading == is_loading && vdev) {
3481 			__wlan_ipa_wlan_evt(pending_event->net_dev,
3482 					   pending_event->device_mode,
3483 					   pending_event->session_id,
3484 					   pending_event->type,
3485 					   pending_event->mac_addr,
3486 					   pending_event->is_2g_iface, ipa_ctx);
3487 		}
3488 
3489 		if (vdev)
3490 			wlan_objmgr_vdev_release_ref(vdev, WLAN_IPA_ID);
3491 		qdf_mem_free(pending_event);
3492 		pending_event = NULL;
3493 		qdf_list_remove_front(&ipa_ctx->pending_event,
3494 				      (qdf_list_node_t **)&pending_event);
3495 	}
3496 }
3497 
3498 #if !defined(QCA_LL_TX_FLOW_CONTROL_V2) && !defined(QCA_IPA_LL_TX_FLOW_CONTROL)
3499 
3500 /**
3501  * wlan_ipa_free_tx_desc_list() - Free IPA Tx desc list
3502  * @ipa_ctx: IPA context
3503  *
3504  * Return: None
3505  */
3506 static inline void wlan_ipa_free_tx_desc_list(struct wlan_ipa_priv *ipa_ctx)
3507 {
3508 	int i;
3509 	qdf_ipa_rx_data_t *ipa_tx_desc;
3510 	uint32_t pool_size;
3511 
3512 	if (!ipa_ctx->tx_desc_pool)
3513 		return;
3514 
3515 	qdf_spin_lock_bh(&ipa_ctx->q_lock);
3516 	pool_size = ipa_ctx->tx_desc_free_list.max_size;
3517 	for (i = 0; i < pool_size; i++) {
3518 		ipa_tx_desc = ipa_ctx->tx_desc_pool[i].ipa_tx_desc_ptr;
3519 		if (ipa_tx_desc)
3520 			qdf_ipa_free_skb(ipa_tx_desc);
3521 
3522 		if (ipa_ctx->tx_desc_free_list.count &&
3523 		    qdf_list_remove_node(&ipa_ctx->tx_desc_free_list,
3524 					 &ipa_ctx->tx_desc_pool[i].node) !=
3525 							QDF_STATUS_SUCCESS)
3526 			ipa_err("Failed to remove node from tx desc freelist");
3527 	}
3528 	qdf_spin_unlock_bh(&ipa_ctx->q_lock);
3529 
3530 	qdf_list_destroy(&ipa_ctx->tx_desc_free_list);
3531 	qdf_mem_free(ipa_ctx->tx_desc_pool);
3532 	ipa_ctx->tx_desc_pool = NULL;
3533 
3534 	ipa_ctx->stats.num_tx_desc_q_cnt = 0;
3535 	ipa_ctx->stats.num_tx_desc_error = 0;
3536 }
3537 
3538 /**
3539  * wlan_ipa_alloc_tx_desc_free_list() - Allocate IPA Tx desc list
3540  * @ipa_ctx: IPA context
3541  *
3542  * Return: QDF_STATUS
3543  */
3544 static QDF_STATUS
3545 wlan_ipa_alloc_tx_desc_free_list(struct wlan_ipa_priv *ipa_ctx)
3546 {
3547 	int i;
3548 	uint32_t max_desc_cnt;
3549 
3550 	max_desc_cnt = ipa_ctx->config->txbuf_count;
3551 
3552 	ipa_ctx->tx_desc_pool = qdf_mem_malloc(sizeof(struct wlan_ipa_tx_desc) *
3553 					       max_desc_cnt);
3554 	if (!ipa_ctx->tx_desc_pool)
3555 		return QDF_STATUS_E_NOMEM;
3556 
3557 	qdf_list_create(&ipa_ctx->tx_desc_free_list, max_desc_cnt);
3558 
3559 	qdf_spin_lock_bh(&ipa_ctx->q_lock);
3560 	for (i = 0; i < max_desc_cnt; i++) {
3561 		ipa_ctx->tx_desc_pool[i].id = i;
3562 		ipa_ctx->tx_desc_pool[i].ipa_tx_desc_ptr = NULL;
3563 		qdf_list_insert_back(&ipa_ctx->tx_desc_free_list,
3564 				     &ipa_ctx->tx_desc_pool[i].node);
3565 	}
3566 
3567 	ipa_ctx->stats.num_tx_desc_q_cnt = 0;
3568 	ipa_ctx->stats.num_tx_desc_error = 0;
3569 
3570 	qdf_spin_unlock_bh(&ipa_ctx->q_lock);
3571 
3572 	return QDF_STATUS_SUCCESS;
3573 }
3574 
3575 /**
3576  * wlan_ipa_setup_tx_sys_pipe() - Setup IPA Tx system pipes
3577  * @ipa_ctx: Global IPA IPA context
3578  * @desc_fifo_sz: Number of descriptors
3579  *
3580  * Return: 0 on success, negative errno on error
3581  */
3582 static int wlan_ipa_setup_tx_sys_pipe(struct wlan_ipa_priv *ipa_ctx,
3583 				     int32_t desc_fifo_sz)
3584 {
3585 	int i, ret = 0;
3586 	qdf_ipa_sys_connect_params_t *ipa;
3587 
3588 	/*setup TX pipes */
3589 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
3590 		ipa = &ipa_ctx->sys_pipe[i].ipa_sys_params;
3591 
3592 		ipa->client = wlan_ipa_iface_2_client[i].cons_client;
3593 		ipa->desc_fifo_sz = desc_fifo_sz;
3594 		ipa->priv = &ipa_ctx->iface_context[i];
3595 		ipa->notify = wlan_ipa_i2w_cb;
3596 
3597 		if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
3598 			ipa->ipa_ep_cfg.hdr.hdr_len =
3599 				WLAN_IPA_UC_WLAN_TX_HDR_LEN;
3600 			ipa->ipa_ep_cfg.nat.nat_en = IPA_BYPASS_NAT;
3601 			ipa->ipa_ep_cfg.hdr.hdr_ofst_pkt_size_valid = 1;
3602 			ipa->ipa_ep_cfg.hdr.hdr_ofst_pkt_size = 0;
3603 			ipa->ipa_ep_cfg.hdr.hdr_additional_const_len =
3604 				WLAN_IPA_UC_WLAN_8023_HDR_SIZE;
3605 			ipa->ipa_ep_cfg.hdr_ext.hdr_little_endian = true;
3606 		} else {
3607 			ipa->ipa_ep_cfg.hdr.hdr_len = WLAN_IPA_WLAN_TX_HDR_LEN;
3608 		}
3609 		ipa->ipa_ep_cfg.mode.mode = IPA_BASIC;
3610 
3611 		ret = wlan_ipa_wdi_setup_sys_pipe(ipa_ctx, ipa,
3612 				&ipa_ctx->sys_pipe[i].conn_hdl);
3613 		if (ret) {
3614 			ipa_err("Failed for pipe %d ret: %d", i, ret);
3615 			return ret;
3616 		}
3617 		ipa_ctx->sys_pipe[i].conn_hdl_valid = 1;
3618 	}
3619 
3620 	return ret;
3621 }
3622 #else /* QCA_LL_TX_FLOW_CONTROL_V2 */
3623 
3624 /**
3625  * wlan_ipa_free_tx_desc_list() - Free IPA Tx desc list
3626  * @ipa_ctx: IPA context
3627  *
3628  * Return: None
3629  */
3630 static inline void wlan_ipa_free_tx_desc_list(struct wlan_ipa_priv *ipa_ctx)
3631 {
3632 }
3633 
3634 /**
3635  * wlan_ipa_alloc_tx_desc_free_list() - Allocate IPA Tx desc list
3636  * @ipa_ctx: IPA context
3637  *
3638  * Return: QDF_STATUS
3639  */
3640 static QDF_STATUS
3641 wlan_ipa_alloc_tx_desc_free_list(struct wlan_ipa_priv *ipa_ctx)
3642 {
3643 	return QDF_STATUS_SUCCESS;
3644 }
3645 
3646 /**
3647  * wlan_ipa_setup_tx_sys_pipe() - Setup IPA Tx system pipes
3648  * @ipa_ctx: IPA context
3649  * @desc_fifo_sz: Number of descriptors
3650  *
3651  * Return: 0 on success, negative errno on error
3652  */
3653 static int wlan_ipa_setup_tx_sys_pipe(struct wlan_ipa_priv *ipa_ctx,
3654 				     int32_t desc_fifo_sz)
3655 {
3656 	/*
3657 	 * The Tx system pipes are not needed for MCC when TX_FLOW_CONTROL_V2
3658 	 * is enabled, where per vdev descriptors are supported in firmware.
3659 	 */
3660 	return 0;
3661 }
3662 #endif /* QCA_LL_TX_FLOW_CONTROL_V2 */
3663 
3664 #if defined(CONFIG_IPA_WDI_UNIFIED_API) && defined(IPA_WDI3_GSI)
3665 /**
3666  * wlan_ipa_get_rx_ipa_client() - Get IPA RX ipa client
3667  * @ipa_ctx: IPA context
3668  *
3669  * Return: rx ipa sys client
3670  */
3671 static inline uint8_t wlan_ipa_get_rx_ipa_client(struct wlan_ipa_priv *ipa_ctx)
3672 {
3673 	if (ipa_ctx->over_gsi)
3674 		return IPA_CLIENT_WLAN2_PROD;
3675 	else
3676 		return IPA_CLIENT_WLAN1_PROD;
3677 }
3678 
3679 /**
3680  * wlan_ipa_uc_send_wdi_control_msg() - Set WDI control message
3681  * @ctrl: WDI control value
3682  *
3683  * Send WLAN_WDI_ENABLE for ctrl = true and WLAN_WDI_DISABLE otherwise.
3684  *
3685  * Return: QDF_STATUS
3686  */
3687 static QDF_STATUS wlan_ipa_uc_send_wdi_control_msg(struct wlan_ipa_priv *ipa_ctx,
3688 						   bool ctrl)
3689 {
3690 	return QDF_STATUS_SUCCESS;
3691 }
3692 
3693 #else
3694 static inline uint8_t wlan_ipa_get_rx_ipa_client(struct wlan_ipa_priv *ipa_ctx)
3695 {
3696 	return IPA_CLIENT_WLAN1_PROD;
3697 }
3698 
3699 static QDF_STATUS wlan_ipa_uc_send_wdi_control_msg(struct wlan_ipa_priv *ipa_ctx,
3700 						   bool ctrl)
3701 {
3702 	struct wlan_ipa_priv *ipa_obj = ipa_ctx;
3703 	qdf_ipa_msg_meta_t meta;
3704 	qdf_ipa_wlan_msg_t *ipa_msg;
3705 	int ret = 0;
3706 
3707 	/* WDI enable message to IPA */
3708 	QDF_IPA_MSG_META_MSG_LEN(&meta) = sizeof(*ipa_msg);
3709 	ipa_msg = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(&meta));
3710 	if (!ipa_msg)
3711 		return QDF_STATUS_E_NOMEM;
3712 
3713 	if (ctrl) {
3714 		QDF_IPA_SET_META_MSG_TYPE(&meta, QDF_WDI_ENABLE);
3715 		ipa_obj->stats.event[QDF_WDI_ENABLE]++;
3716 	} else {
3717 		QDF_IPA_SET_META_MSG_TYPE(&meta, QDF_WDI_DISABLE);
3718 		ipa_obj->stats.event[QDF_WDI_DISABLE]++;
3719 	}
3720 
3721 	ipa_debug("ipa_send_msg(Evt:%d)", QDF_IPA_MSG_META_MSG_TYPE(&meta));
3722 	ret = qdf_ipa_send_msg(&meta, ipa_msg, wlan_ipa_msg_free_fn);
3723 	if (ret) {
3724 		ipa_err("ipa_send_msg(Evt:%d)-fail=%d",
3725 			QDF_IPA_MSG_META_MSG_TYPE(&meta), ret);
3726 		qdf_mem_free(ipa_msg);
3727 		return QDF_STATUS_E_FAILURE;
3728 	}
3729 
3730 	return QDF_STATUS_SUCCESS;
3731 }
3732 #endif
3733 
3734 /**
3735  * wlan_ipa_setup_rx_sys_pipe() - Setup IPA Rx system pipes
3736  * @ipa_ctx: Global IPA IPA context
3737  * @desc_fifo_sz: Number of descriptors
3738  *
3739  * Return: 0 on success, negative errno on error
3740  */
3741 static int wlan_ipa_setup_rx_sys_pipe(struct wlan_ipa_priv *ipa_ctx,
3742 				     int32_t desc_fifo_sz)
3743 {
3744 	int ret = 0;
3745 	qdf_ipa_sys_connect_params_t *ipa;
3746 
3747 	/*
3748 	 * Hard code it here, this can be extended if in case
3749 	 * PROD pipe is also per interface.
3750 	 * Right now there is no advantage of doing this.
3751 	 */
3752 	ipa = &ipa_ctx->sys_pipe[WLAN_IPA_RX_PIPE].ipa_sys_params;
3753 
3754 	ipa->client = wlan_ipa_get_rx_ipa_client(ipa_ctx);
3755 	ipa->desc_fifo_sz = desc_fifo_sz;
3756 	ipa->priv = ipa_ctx;
3757 	ipa->notify = wlan_ipa_w2i_cb;
3758 
3759 	ipa->ipa_ep_cfg.nat.nat_en = IPA_BYPASS_NAT;
3760 	ipa->ipa_ep_cfg.hdr.hdr_len = WLAN_IPA_WLAN_RX_HDR_LEN;
3761 	ipa->ipa_ep_cfg.hdr.hdr_ofst_metadata_valid = 1;
3762 	ipa->ipa_ep_cfg.mode.mode = IPA_BASIC;
3763 
3764 	ret = qdf_ipa_setup_sys_pipe(ipa,
3765 			&ipa_ctx->sys_pipe[WLAN_IPA_RX_PIPE].conn_hdl);
3766 	if (ret) {
3767 		ipa_err("Failed for RX pipe: %d", ret);
3768 		return ret;
3769 	}
3770 	ipa_ctx->sys_pipe[WLAN_IPA_RX_PIPE].conn_hdl_valid = 1;
3771 
3772 	return ret;
3773 }
3774 
3775 /**
3776  * wlan_ipa_teardown_sys_pipe() - Tear down all IPA Sys pipes
3777  * @ipa_ctx: Global IPA IPA context
3778  *
3779  * Return: None
3780  */
3781 static void wlan_ipa_teardown_sys_pipe(struct wlan_ipa_priv *ipa_ctx)
3782 {
3783 	int ret, i;
3784 
3785 	if (!ipa_ctx)
3786 		return;
3787 
3788 	for (i = 0; i < WLAN_IPA_MAX_SYSBAM_PIPE; i++) {
3789 		if (ipa_ctx->sys_pipe[i].conn_hdl_valid) {
3790 			ret = wlan_ipa_wdi_teardown_sys_pipe(ipa_ctx,
3791 							     ipa_ctx->sys_pipe[i].conn_hdl);
3792 			if (ret)
3793 				ipa_err("Failed:%d", ret);
3794 
3795 			ipa_ctx->sys_pipe[i].conn_hdl_valid = 0;
3796 		}
3797 	}
3798 
3799 	wlan_ipa_free_tx_desc_list(ipa_ctx);
3800 }
3801 
3802 /**
3803  * wlan_ipa_setup_sys_pipe() - Setup all IPA system pipes
3804  * @ipa_ctx: Global IPA IPA context
3805  *
3806  * Return: 0 on success, negative errno on error
3807  */
3808 static int wlan_ipa_setup_sys_pipe(struct wlan_ipa_priv *ipa_ctx)
3809 {
3810 	int ret = 0;
3811 	uint32_t desc_fifo_sz;
3812 
3813 	/* The maximum number of descriptors that can be provided to a BAM at
3814 	 * once is one less than the total number of descriptors that the buffer
3815 	 * can contain.
3816 	 * If max_num_of_descriptors = (BAM_PIPE_DESCRIPTOR_FIFO_SIZE / sizeof
3817 	 * (SPS_DESCRIPTOR)), then (max_num_of_descriptors - 1) descriptors can
3818 	 * be provided at once.
3819 	 * Because of above requirement, one extra descriptor will be added to
3820 	 * make sure hardware always has one descriptor.
3821 	 */
3822 	desc_fifo_sz = ipa_ctx->config->desc_size + IPA_SPS_DESC_SIZE;
3823 
3824 	ret = wlan_ipa_setup_tx_sys_pipe(ipa_ctx, desc_fifo_sz);
3825 	if (ret) {
3826 		ipa_err("Failed for TX pipe: %d", ret);
3827 		goto setup_sys_pipe_fail;
3828 	}
3829 
3830 	if (!wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
3831 		ret = wlan_ipa_setup_rx_sys_pipe(ipa_ctx, desc_fifo_sz);
3832 		if (ret) {
3833 			ipa_err("Failed for RX pipe: %d", ret);
3834 			goto setup_sys_pipe_fail;
3835 		}
3836 	}
3837 
3838        /* Allocate free Tx desc list */
3839 	ret = wlan_ipa_alloc_tx_desc_free_list(ipa_ctx);
3840 	if (ret)
3841 		goto setup_sys_pipe_fail;
3842 
3843 	return ret;
3844 
3845 setup_sys_pipe_fail:
3846 	wlan_ipa_teardown_sys_pipe(ipa_ctx);
3847 
3848 	return ret;
3849 }
3850 
3851 #if !defined(QCA_LL_TX_FLOW_CONTROL_V2) && !defined(QCA_IPA_LL_TX_FLOW_CONTROL)
3852 QDF_STATUS wlan_ipa_send_mcc_scc_msg(struct wlan_ipa_priv *ipa_ctx,
3853 				     bool mcc_mode)
3854 {
3855 	qdf_ipa_msg_meta_t meta;
3856 	qdf_ipa_wlan_msg_t *msg;
3857 	int ret;
3858 
3859 	if (!wlan_ipa_uc_sta_is_enabled(ipa_ctx->config))
3860 		return QDF_STATUS_SUCCESS;
3861 
3862 	/* Send SCC/MCC Switching event to IPA */
3863 	QDF_IPA_MSG_META_MSG_LEN(&meta) = sizeof(*msg);
3864 	msg = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(&meta));
3865 	if (!msg)
3866 		return QDF_STATUS_E_NOMEM;
3867 
3868 	if (mcc_mode) {
3869 		QDF_IPA_SET_META_MSG_TYPE(&meta, QDF_SWITCH_TO_MCC);
3870 		ipa_ctx->stats.event[QDF_SWITCH_TO_MCC]++;
3871 	} else {
3872 		QDF_IPA_SET_META_MSG_TYPE(&meta, QDF_SWITCH_TO_SCC);
3873 		ipa_ctx->stats.event[QDF_SWITCH_TO_SCC]++;
3874 	}
3875 
3876 	WLAN_IPA_LOG(QDF_TRACE_LEVEL_DEBUG,
3877 		    "ipa_send_msg(Evt:%d)",
3878 		    QDF_IPA_MSG_META_MSG_TYPE(&meta));
3879 
3880 	ret = qdf_ipa_send_msg(&meta, msg, wlan_ipa_msg_free_fn);
3881 
3882 	if (ret) {
3883 		ipa_err("ipa_send_msg(Evt:%d) - fail=%d",
3884 			QDF_IPA_MSG_META_MSG_TYPE(&meta), ret);
3885 		qdf_mem_free(msg);
3886 		return QDF_STATUS_E_FAILURE;
3887 	}
3888 
3889 	return QDF_STATUS_SUCCESS;
3890 }
3891 
3892 static void wlan_ipa_mcc_work_handler(void *data)
3893 {
3894 	struct wlan_ipa_priv *ipa_ctx = (struct wlan_ipa_priv *)data;
3895 
3896 	wlan_ipa_send_mcc_scc_msg(ipa_ctx, ipa_ctx->mcc_mode);
3897 }
3898 #endif
3899 
3900 /**
3901  * wlan_ipa_setup() - IPA initialization function
3902  * @ipa_ctx: IPA context
3903  * @ipa_cfg: IPA config
3904  *
3905  * Allocate ipa_ctx resources, ipa pipe resource and register
3906  * wlan interface with IPA module.
3907  *
3908  * Return: QDF_STATUS enumeration
3909  */
3910 QDF_STATUS wlan_ipa_setup(struct wlan_ipa_priv *ipa_ctx,
3911 			  struct wlan_ipa_config *ipa_cfg)
3912 {
3913 	int ret, i;
3914 	struct wlan_ipa_iface_context *iface_context = NULL;
3915 	QDF_STATUS status;
3916 
3917 	ipa_debug("enter");
3918 
3919 	gp_ipa = ipa_ctx;
3920 	ipa_ctx->num_iface = 0;
3921 	ipa_ctx->config = ipa_cfg;
3922 
3923 	wlan_ipa_wdi_get_wdi_version(ipa_ctx);
3924 
3925 	/* Create the interface context */
3926 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
3927 		iface_context = &ipa_ctx->iface_context[i];
3928 		iface_context->ipa_ctx = ipa_ctx;
3929 		iface_context->cons_client =
3930 			wlan_ipa_iface_2_client[i].cons_client;
3931 		iface_context->prod_client =
3932 			wlan_ipa_iface_2_client[i].prod_client;
3933 		iface_context->iface_id = i;
3934 		iface_context->dev = NULL;
3935 		iface_context->device_mode = QDF_MAX_NO_OF_MODE;
3936 		iface_context->session_id = WLAN_IPA_MAX_SESSION;
3937 		qdf_atomic_init(&iface_context->conn_count);
3938 		qdf_atomic_init(&iface_context->disconn_count);
3939 		qdf_spinlock_create(&iface_context->interface_lock);
3940 	}
3941 
3942 	qdf_create_work(0, &ipa_ctx->pm_work, wlan_ipa_pm_flush, ipa_ctx);
3943 	qdf_spinlock_create(&ipa_ctx->pm_lock);
3944 	qdf_spinlock_create(&ipa_ctx->q_lock);
3945 	qdf_spinlock_create(&ipa_ctx->enable_disable_lock);
3946 	ipa_ctx->pipes_down_in_progress = false;
3947 	ipa_ctx->pipes_enable_in_progress = false;
3948 	qdf_nbuf_queue_init(&ipa_ctx->pm_queue_head);
3949 	qdf_list_create(&ipa_ctx->pending_event, 1000);
3950 	qdf_mutex_create(&ipa_ctx->event_lock);
3951 	qdf_mutex_create(&ipa_ctx->ipa_lock);
3952 	qdf_atomic_init(&ipa_ctx->deinit_in_prog);
3953 
3954 	status = wlan_ipa_wdi_setup_rm(ipa_ctx);
3955 	if (status != QDF_STATUS_SUCCESS)
3956 		goto fail_setup_rm;
3957 
3958 	for (i = 0; i < WLAN_IPA_MAX_SYSBAM_PIPE; i++)
3959 		qdf_mem_zero(&ipa_ctx->sys_pipe[i],
3960 			     sizeof(struct wlan_ipa_sys_pipe));
3961 
3962 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
3963 		qdf_mem_zero(&ipa_ctx->stats, sizeof(ipa_ctx->stats));
3964 		ipa_ctx->sap_num_connected_sta = 0;
3965 		ipa_ctx->ipa_tx_packets_diff = 0;
3966 		ipa_ctx->ipa_rx_packets_diff = 0;
3967 		ipa_ctx->ipa_p_tx_packets = 0;
3968 		ipa_ctx->ipa_p_rx_packets = 0;
3969 		ipa_ctx->resource_loading = false;
3970 		ipa_ctx->resource_unloading = false;
3971 		ipa_ctx->num_sap_connected = 0;
3972 		ipa_ctx->sta_connected = 0;
3973 		ipa_ctx->ipa_pipes_down = true;
3974 		qdf_atomic_set(&ipa_ctx->pipes_disabled, 1);
3975 		qdf_atomic_set(&ipa_ctx->autonomy_disabled, 1);
3976 		ipa_ctx->wdi_enabled = false;
3977 
3978 		status = wlan_ipa_wdi_init(ipa_ctx);
3979 		if (status == QDF_STATUS_SUCCESS) {
3980 			/* Setup IPA system pipes */
3981 			if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
3982 				ret = wlan_ipa_setup_sys_pipe(ipa_ctx);
3983 				if (ret)
3984 					goto ipa_wdi_destroy;
3985 
3986 				qdf_create_work(0, &ipa_ctx->mcc_work,
3987 						wlan_ipa_mcc_work_handler,
3988 						ipa_ctx);
3989 			}
3990 		} else if (status == QDF_STATUS_E_BUSY) {
3991 			ret = wlan_ipa_uc_send_wdi_control_msg(ipa_ctx, false);
3992 			if (ret) {
3993 				ipa_err("IPA WDI msg send failed: ret=%d", ret);
3994 				goto ipa_wdi_destroy;
3995 			}
3996 		} else {
3997 			ipa_err("IPA WDI init failed: ret=%d", status);
3998 			goto ipa_wdi_destroy;
3999 		}
4000 	} else {
4001 		ret = wlan_ipa_setup_sys_pipe(ipa_ctx);
4002 		if (ret)
4003 			goto ipa_wdi_destroy;
4004 	}
4005 
4006 	qdf_event_create(&ipa_ctx->ipa_resource_comp);
4007 
4008 	ipa_debug("exit: success");
4009 
4010 	return QDF_STATUS_SUCCESS;
4011 
4012 ipa_wdi_destroy:
4013 	wlan_ipa_wdi_destroy_rm(ipa_ctx);
4014 
4015 fail_setup_rm:
4016 	qdf_spinlock_destroy(&ipa_ctx->pm_lock);
4017 	qdf_spinlock_destroy(&ipa_ctx->q_lock);
4018 	qdf_spinlock_destroy(&ipa_ctx->enable_disable_lock);
4019 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4020 		iface_context = &ipa_ctx->iface_context[i];
4021 		qdf_spinlock_destroy(&iface_context->interface_lock);
4022 	}
4023 	qdf_mutex_destroy(&ipa_ctx->event_lock);
4024 	qdf_mutex_destroy(&ipa_ctx->ipa_lock);
4025 	qdf_list_destroy(&ipa_ctx->pending_event);
4026 	gp_ipa = NULL;
4027 	ipa_debug("exit: fail");
4028 
4029 	return QDF_STATUS_E_FAILURE;
4030 }
4031 
4032 void wlan_ipa_flush(struct wlan_ipa_priv *ipa_ctx)
4033 {
4034 	qdf_nbuf_t skb;
4035 	struct wlan_ipa_pm_tx_cb *pm_tx_cb;
4036 
4037 	if (!wlan_ipa_is_enabled(ipa_ctx->config))
4038 		return;
4039 
4040 	qdf_cancel_work(&ipa_ctx->pm_work);
4041 
4042 	qdf_spin_lock_bh(&ipa_ctx->pm_lock);
4043 
4044 	while (((skb = qdf_nbuf_queue_remove(&ipa_ctx->pm_queue_head))
4045 	       != NULL)) {
4046 		qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
4047 
4048 		pm_tx_cb = (struct wlan_ipa_pm_tx_cb *)skb->cb;
4049 
4050 		if (pm_tx_cb->exception) {
4051 			dev_kfree_skb_any(skb);
4052 		} else {
4053 			if (pm_tx_cb->ipa_tx_desc)
4054 				ipa_free_skb(pm_tx_cb->ipa_tx_desc);
4055 		}
4056 
4057 		qdf_spin_lock_bh(&ipa_ctx->pm_lock);
4058 	}
4059 	qdf_spin_unlock_bh(&ipa_ctx->pm_lock);
4060 }
4061 
4062 QDF_STATUS wlan_ipa_cleanup(struct wlan_ipa_priv *ipa_ctx)
4063 {
4064 	struct wlan_ipa_iface_context *iface_context;
4065 	int i;
4066 
4067 	if (!ipa_cb_is_ready())
4068 		return QDF_STATUS_SUCCESS;
4069 	qdf_event_destroy(&ipa_ctx->ipa_resource_comp);
4070 	if (!wlan_ipa_uc_is_enabled(ipa_ctx->config))
4071 		wlan_ipa_teardown_sys_pipe(ipa_ctx);
4072 
4073 	/* Teardown IPA sys_pipe for MCC */
4074 	if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
4075 		wlan_ipa_teardown_sys_pipe(ipa_ctx);
4076 		if (ipa_ctx->uc_loaded)
4077 			qdf_cancel_work(&ipa_ctx->mcc_work);
4078 	}
4079 
4080 	wlan_ipa_wdi_destroy_rm(ipa_ctx);
4081 
4082 	wlan_ipa_flush(ipa_ctx);
4083 
4084 	qdf_spinlock_destroy(&ipa_ctx->pm_lock);
4085 	qdf_spinlock_destroy(&ipa_ctx->q_lock);
4086 	qdf_spinlock_destroy(&ipa_ctx->enable_disable_lock);
4087 	qdf_destroy_work(0, &ipa_ctx->pm_work);
4088 
4089 	/* destroy the interface lock */
4090 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4091 		iface_context = &ipa_ctx->iface_context[i];
4092 		qdf_spinlock_destroy(&iface_context->interface_lock);
4093 	}
4094 
4095 	if (wlan_ipa_uc_is_enabled(ipa_ctx->config)) {
4096 		wlan_ipa_wdi_cleanup(ipa_ctx->hdl);
4097 		qdf_mutex_destroy(&ipa_ctx->event_lock);
4098 		qdf_mutex_destroy(&ipa_ctx->ipa_lock);
4099 		qdf_list_destroy(&ipa_ctx->pending_event);
4100 
4101 	}
4102 
4103 	gp_ipa = NULL;
4104 
4105 	/* Acquire lock */
4106 	ipa_init_deinit_lock();
4107 	if (g_instances_added)
4108 		g_instances_added--;
4109 	/* Unlock */
4110 	ipa_init_deinit_unlock();
4111 
4112 	ipa_ctx->handle_initialized = false;
4113 
4114 	return QDF_STATUS_SUCCESS;
4115 }
4116 
4117 struct wlan_ipa_iface_context
4118 *wlan_ipa_get_iface(struct wlan_ipa_priv *ipa_ctx, uint8_t mode)
4119 {
4120 	struct wlan_ipa_iface_context *iface_ctx = NULL;
4121 	int i;
4122 
4123 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4124 		iface_ctx = &ipa_ctx->iface_context[i];
4125 
4126 		if (iface_ctx->device_mode == mode)
4127 			return iface_ctx;
4128 	}
4129 
4130 	return NULL;
4131 }
4132 
4133 struct wlan_ipa_iface_context *
4134 wlan_ipa_get_iface_by_mode_netdev(struct wlan_ipa_priv *ipa_ctx,
4135 				  qdf_netdev_t ndev, uint8_t mode)
4136 {
4137 	struct wlan_ipa_iface_context *iface_ctx = NULL;
4138 	int i;
4139 
4140 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4141 		iface_ctx = &ipa_ctx->iface_context[i];
4142 
4143 		if (iface_ctx->device_mode == mode && iface_ctx->dev == ndev)
4144 			return iface_ctx;
4145 	}
4146 
4147 	return NULL;
4148 }
4149 
4150 void wlan_ipa_set_mcc_mode(struct wlan_ipa_priv *ipa_ctx, bool mcc_mode)
4151 {
4152 	if (!wlan_ipa_uc_sta_is_enabled(ipa_ctx->config))
4153 		return;
4154 
4155 	if (ipa_ctx->mcc_mode == mcc_mode)
4156 		return;
4157 
4158 	ipa_ctx->mcc_mode = mcc_mode;
4159 	qdf_sched_work(0, &ipa_ctx->mcc_work);
4160 }
4161 
4162 /**
4163  * wlan_ipa_uc_loaded_handler() - Process IPA uC loaded indication
4164  * @ipa_ctx: ipa ipa local context
4165  *
4166  * Will handle IPA UC image loaded indication comes from IPA kernel
4167  *
4168  * Return: None
4169  */
4170 static void wlan_ipa_uc_loaded_handler(struct wlan_ipa_priv *ipa_ctx)
4171 {
4172 	struct wlan_objmgr_pdev *pdev = ipa_ctx->pdev;
4173 	struct wlan_objmgr_psoc *psoc = wlan_pdev_get_psoc(pdev);
4174 	qdf_device_t qdf_dev = wlan_psoc_get_qdf_dev(psoc);
4175 	QDF_STATUS status;
4176 
4177 	ipa_info("UC READY");
4178 
4179 	if (true == ipa_ctx->uc_loaded) {
4180 		ipa_info("UC already loaded");
4181 		return;
4182 	}
4183 
4184 	if (!qdf_dev) {
4185 		ipa_err("qdf_dev is null");
4186 		return;
4187 	}
4188 
4189 	if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
4190 		/* Setup IPA system pipes */
4191 		status = wlan_ipa_setup_sys_pipe(ipa_ctx);
4192 		if (status) {
4193 			ipa_err("Fail to setup sys pipes (status=%d)", status);
4194 			return;
4195 		}
4196 		qdf_create_work(0, &ipa_ctx->mcc_work,
4197 				wlan_ipa_mcc_work_handler, ipa_ctx);
4198 	}
4199 
4200 	/* Connect pipe */
4201 	status = wlan_ipa_wdi_setup(ipa_ctx, qdf_dev);
4202 	if (status) {
4203 		ipa_err("Failure to setup IPA pipes (status=%d)",
4204 			status);
4205 		goto connect_pipe_fail;
4206 	}
4207 	/* Setup the Tx buffer SMMU mappings */
4208 	status = cdp_ipa_tx_buf_smmu_mapping(ipa_ctx->dp_soc,
4209 					     ipa_ctx->dp_pdev_id,
4210 					     __func__, __LINE__);
4211 	if (status) {
4212 		ipa_err("Failure to map Tx buffers for IPA(status=%d)",
4213 			status);
4214 		goto smmu_map_fail;
4215 	}
4216 	ipa_info("TX buffers mapped to IPA");
4217 	cdp_ipa_set_doorbell_paddr(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id);
4218 	wlan_ipa_init_metering(ipa_ctx);
4219 
4220 	if (QDF_IS_STATUS_ERROR(wlan_ipa_init_perf_level(ipa_ctx)))
4221 		ipa_err("Failed to init perf level");
4222 
4223 	/*
4224 	 * Enable IPA/FW PIPEs if
4225 	 * 1. any clients connected to SAP or
4226 	 * 2. STA connected to remote AP if STA only offload is enabled
4227 	 */
4228 	if (ipa_ctx->sap_num_connected_sta ||
4229 	    (wlan_ipa_is_sta_only_offload_enabled() &&
4230 	     ipa_ctx->sta_connected)) {
4231 		ipa_debug("Client already connected, enable IPA/FW PIPEs");
4232 		wlan_ipa_uc_handle_first_con(ipa_ctx);
4233 	}
4234 
4235 	ipa_ctx->uc_loaded = true;
4236 
4237 	return;
4238 
4239 smmu_map_fail:
4240 	qdf_ipa_wdi_disconn_pipes(ipa_ctx->hdl);
4241 
4242 connect_pipe_fail:
4243 	if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
4244 		qdf_cancel_work(&ipa_ctx->mcc_work);
4245 		wlan_ipa_teardown_sys_pipe(ipa_ctx);
4246 	}
4247 }
4248 
4249 /**
4250  * wlan_ipa_uc_op_cb() - IPA uC operation callback
4251  * @op_msg: operation message received from firmware
4252  * @usr_ctxt: user context registered with TL (we register the IPA Global
4253  *	context)
4254  *
4255  * Return: None
4256  */
4257 static void wlan_ipa_uc_op_cb(struct op_msg_type *op_msg,
4258 			      struct wlan_ipa_priv *ipa_ctx)
4259 {
4260 	struct op_msg_type *msg = op_msg;
4261 	struct ipa_uc_fw_stats *uc_fw_stat;
4262 
4263 	if (!ipa_ctx || !op_msg) {
4264 		ipa_err("INVALID ARG");
4265 		return;
4266 	}
4267 
4268 	if (msg->op_code >= WLAN_IPA_UC_OPCODE_MAX) {
4269 		ipa_err("INVALID OPCODE %d",  msg->op_code);
4270 		qdf_mem_free(op_msg);
4271 		return;
4272 	}
4273 
4274 	ipa_debug("OPCODE=%d", msg->op_code);
4275 
4276 	if ((msg->op_code == WLAN_IPA_UC_OPCODE_TX_RESUME) ||
4277 	    (msg->op_code == WLAN_IPA_UC_OPCODE_RX_RESUME)) {
4278 		qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4279 		ipa_ctx->activated_fw_pipe++;
4280 		if (wlan_ipa_is_fw_wdi_activated(ipa_ctx)) {
4281 			ipa_ctx->resource_loading = false;
4282 			qdf_event_set(&ipa_ctx->ipa_resource_comp);
4283 			if (ipa_ctx->wdi_enabled == false) {
4284 				ipa_ctx->wdi_enabled = true;
4285 				if (wlan_ipa_uc_send_wdi_control_msg(ipa_ctx, true) == 0)
4286 					wlan_ipa_send_mcc_scc_msg(ipa_ctx,
4287 							ipa_ctx->mcc_mode);
4288 			}
4289 			wlan_ipa_uc_proc_pending_event(ipa_ctx, true);
4290 			if (ipa_ctx->pending_cons_req)
4291 				wlan_ipa_wdi_rm_notify_completion(
4292 						QDF_IPA_RM_RESOURCE_GRANTED,
4293 						QDF_IPA_RM_RESOURCE_WLAN_CONS);
4294 			ipa_ctx->pending_cons_req = false;
4295 		}
4296 		qdf_mutex_release(&ipa_ctx->ipa_lock);
4297 	} else if ((msg->op_code == WLAN_IPA_UC_OPCODE_TX_SUSPEND) ||
4298 	    (msg->op_code == WLAN_IPA_UC_OPCODE_RX_SUSPEND)) {
4299 		qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4300 
4301 		if (msg->op_code == WLAN_IPA_UC_OPCODE_RX_SUSPEND) {
4302 			wlan_ipa_uc_disable_pipes(ipa_ctx, true);
4303 			ipa_info("Disable FW TX PIPE");
4304 			cdp_ipa_set_active(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id,
4305 					   false, true);
4306 		}
4307 
4308 		ipa_ctx->activated_fw_pipe--;
4309 		if (!ipa_ctx->activated_fw_pipe) {
4310 			/*
4311 			 * Async return success from FW
4312 			 * Disable/suspend all the PIPEs
4313 			 */
4314 			ipa_ctx->resource_unloading = false;
4315 			qdf_event_set(&ipa_ctx->ipa_resource_comp);
4316 			if (wlan_ipa_is_rm_enabled(ipa_ctx->config))
4317 				wlan_ipa_wdi_rm_release_resource(ipa_ctx,
4318 						QDF_IPA_RM_RESOURCE_WLAN_PROD);
4319 			wlan_ipa_uc_proc_pending_event(ipa_ctx, false);
4320 			ipa_ctx->pending_cons_req = false;
4321 		}
4322 		qdf_mutex_release(&ipa_ctx->ipa_lock);
4323 	} else if ((msg->op_code == WLAN_IPA_UC_OPCODE_STATS) &&
4324 		(ipa_ctx->stat_req_reason == WLAN_IPA_UC_STAT_REASON_DEBUG)) {
4325 		uc_fw_stat = (struct ipa_uc_fw_stats *)
4326 			((uint8_t *)op_msg + sizeof(struct op_msg_type));
4327 
4328 		/* WLAN FW WDI stats */
4329 		wlan_ipa_print_fw_wdi_stats(ipa_ctx, uc_fw_stat);
4330 	} else if ((msg->op_code == WLAN_IPA_UC_OPCODE_STATS) &&
4331 		(ipa_ctx->stat_req_reason == WLAN_IPA_UC_STAT_REASON_BW_CAL)) {
4332 		/* STATs from FW */
4333 		uc_fw_stat = (struct ipa_uc_fw_stats *)
4334 			((uint8_t *)op_msg + sizeof(struct op_msg_type));
4335 		qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4336 		ipa_ctx->ipa_tx_packets_diff = BW_GET_DIFF(
4337 			uc_fw_stat->tx_pkts_completed,
4338 			ipa_ctx->ipa_p_tx_packets);
4339 		ipa_ctx->ipa_rx_packets_diff = BW_GET_DIFF(
4340 			(uc_fw_stat->rx_num_ind_drop_no_space +
4341 			uc_fw_stat->rx_num_ind_drop_no_buf +
4342 			uc_fw_stat->rx_num_pkts_indicated),
4343 			ipa_ctx->ipa_p_rx_packets);
4344 
4345 		ipa_ctx->ipa_p_tx_packets = uc_fw_stat->tx_pkts_completed;
4346 		ipa_ctx->ipa_p_rx_packets =
4347 			(uc_fw_stat->rx_num_ind_drop_no_space +
4348 			uc_fw_stat->rx_num_ind_drop_no_buf +
4349 			uc_fw_stat->rx_num_pkts_indicated);
4350 		qdf_mutex_release(&ipa_ctx->ipa_lock);
4351 	} else if (msg->op_code == WLAN_IPA_UC_OPCODE_UC_READY) {
4352 		qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4353 		wlan_ipa_uc_loaded_handler(ipa_ctx);
4354 		qdf_mutex_release(&ipa_ctx->ipa_lock);
4355 	} else if (wlan_ipa_uc_op_metering(ipa_ctx, op_msg)) {
4356 		ipa_err("Invalid message: op_code=%d, reason=%d",
4357 			msg->op_code, ipa_ctx->stat_req_reason);
4358 	}
4359 
4360 	qdf_mem_free(op_msg);
4361 }
4362 
4363 /**
4364  * __wlan_ipa_uc_fw_op_event_handler - IPA uC FW OPvent handler
4365  * @data: uC OP work
4366  *
4367  * Return: None
4368  */
4369 static void __wlan_ipa_uc_fw_op_event_handler(void *data)
4370 {
4371 	struct op_msg_type *msg;
4372 	struct uc_op_work_struct *uc_op_work =
4373 				(struct uc_op_work_struct *)data;
4374 	struct wlan_ipa_priv *ipa_ctx = uc_op_work->ipa_priv_bp;
4375 
4376 	msg = uc_op_work->msg;
4377 	uc_op_work->msg = NULL;
4378 	ipa_debug("posted msg %d", msg->op_code);
4379 
4380 	wlan_ipa_uc_op_cb(msg, ipa_ctx);
4381 }
4382 
4383 /**
4384  * wlan_ipa_uc_fw_op_event_handler - SSR wrapper for
4385  * __wlan_ipa_uc_fw_op_event_handler
4386  * @data: uC OP work
4387  *
4388  * Return: None
4389  */
4390 static void wlan_ipa_uc_fw_op_event_handler(void *data)
4391 {
4392 	if (qdf_is_recovering()) {
4393 		ipa_err("in recovering");
4394 		return;
4395 	}
4396 
4397 	__wlan_ipa_uc_fw_op_event_handler(data);
4398 }
4399 
4400 /**
4401  * wlan_ipa_uc_op_event_handler() - IPA UC OP event handler
4402  * @op_msg: operation message received from firmware
4403  * @ipa_ctx: Global IPA context
4404  *
4405  * Return: None
4406  */
4407 static void wlan_ipa_uc_op_event_handler(uint8_t *op_msg, void *ctx)
4408 {
4409 	struct wlan_ipa_priv *ipa_ctx = (struct wlan_ipa_priv *)ctx;
4410 	struct op_msg_type *msg;
4411 	struct uc_op_work_struct *uc_op_work;
4412 
4413 	if (!ipa_ctx)
4414 		goto end;
4415 
4416 	msg = (struct op_msg_type *)op_msg;
4417 
4418 	if (msg->op_code >= WLAN_IPA_UC_OPCODE_MAX) {
4419 		ipa_err("Invalid OP Code (%d)", msg->op_code);
4420 		goto end;
4421 	}
4422 
4423 	uc_op_work = &ipa_ctx->uc_op_work[msg->op_code];
4424 	if (uc_op_work->msg) {
4425 		/* When the same uC OPCODE is already pended, just return */
4426 		goto end;
4427 	}
4428 
4429 	uc_op_work->msg = msg;
4430 	qdf_sched_work(0, &uc_op_work->work);
4431 	return;
4432 
4433 end:
4434 	qdf_mem_free(op_msg);
4435 }
4436 
4437 QDF_STATUS wlan_ipa_uc_ol_init(struct wlan_ipa_priv *ipa_ctx,
4438 			       qdf_device_t osdev)
4439 {
4440 	uint8_t i;
4441 	QDF_STATUS status = QDF_STATUS_SUCCESS;
4442 
4443 	if (!wlan_ipa_uc_is_enabled(ipa_ctx->config))
4444 		return QDF_STATUS_SUCCESS;
4445 
4446 	ipa_debug("enter");
4447 
4448 	if (!osdev) {
4449 		ipa_err("osdev null");
4450 		status = QDF_STATUS_E_FAILURE;
4451 		goto fail_return;
4452 	}
4453 
4454 	for (i = 0; i < WLAN_IPA_MAX_SESSION; i++) {
4455 		ipa_ctx->vdev_to_iface[i] = WLAN_IPA_MAX_SESSION;
4456 		ipa_ctx->vdev_offload_enabled[i] = false;
4457 		ipa_ctx->disable_intrabss_fwd[i] = false;
4458 	}
4459 
4460 	if (cdp_ipa_get_resource(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id)) {
4461 		ipa_err("IPA UC resource alloc fail");
4462 		status = QDF_STATUS_E_FAILURE;
4463 		goto fail_return;
4464 	}
4465 
4466 	for (i = 0; i < WLAN_IPA_UC_OPCODE_MAX; i++) {
4467 		ipa_ctx->uc_op_work[i].osdev = osdev;
4468 		ipa_ctx->uc_op_work[i].msg = NULL;
4469 		ipa_ctx->uc_op_work[i].ipa_priv_bp = ipa_ctx;
4470 		qdf_create_work(0, &ipa_ctx->uc_op_work[i].work,
4471 				wlan_ipa_uc_fw_op_event_handler,
4472 				&ipa_ctx->uc_op_work[i]);
4473 	}
4474 
4475 	if (true == ipa_ctx->uc_loaded) {
4476 		status = wlan_ipa_wdi_setup(ipa_ctx, osdev);
4477 		if (status) {
4478 			ipa_err("Failure to setup IPA pipes (status=%d)",
4479 				status);
4480 			status = QDF_STATUS_E_FAILURE;
4481 
4482 			if (wlan_ipa_uc_sta_is_enabled(ipa_ctx->config)) {
4483 				qdf_cancel_work(&ipa_ctx->mcc_work);
4484 				wlan_ipa_teardown_sys_pipe(ipa_ctx);
4485 			}
4486 			ipa_ctx->uc_loaded = false;
4487 
4488 			goto fail_return;
4489 		}
4490 
4491 		/* Setup the Tx buffer SMMU mappings */
4492 		status = cdp_ipa_tx_buf_smmu_mapping(ipa_ctx->dp_soc,
4493 						     ipa_ctx->dp_pdev_id,
4494 						     __func__, __LINE__);
4495 		if (status) {
4496 			ipa_err("Failure to map Tx buffers for IPA(status=%d)",
4497 				status);
4498 			return status;
4499 		}
4500 		ipa_info("TX buffers mapped to IPA");
4501 		cdp_ipa_set_doorbell_paddr(ipa_ctx->dp_soc,
4502 					   ipa_ctx->dp_pdev_id);
4503 		wlan_ipa_init_metering(ipa_ctx);
4504 
4505 		if (wlan_ipa_init_perf_level(ipa_ctx) != QDF_STATUS_SUCCESS)
4506 			ipa_err("Failed to init perf level");
4507 	}
4508 
4509 	cdp_ipa_register_op_cb(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id,
4510 			       wlan_ipa_uc_op_event_handler, (void *)ipa_ctx);
4511 fail_return:
4512 	ipa_debug("exit: status=%d", status);
4513 	return status;
4514 }
4515 
4516 /**
4517  * wlan_ipa_cleanup_pending_event() - Cleanup IPA pending event list
4518  * @ipa_ctx: pointer to IPA IPA struct
4519  *
4520  * Return: none
4521  */
4522 static void wlan_ipa_cleanup_pending_event(struct wlan_ipa_priv *ipa_ctx)
4523 {
4524 	struct wlan_ipa_uc_pending_event *pending_event = NULL;
4525 
4526 	while (qdf_list_remove_front(&ipa_ctx->pending_event,
4527 		(qdf_list_node_t **)&pending_event) == QDF_STATUS_SUCCESS)
4528 		qdf_mem_free(pending_event);
4529 }
4530 
4531 QDF_STATUS wlan_ipa_uc_ol_deinit(struct wlan_ipa_priv *ipa_ctx)
4532 {
4533 	QDF_STATUS status = QDF_STATUS_SUCCESS;
4534 	int i;
4535 
4536 	ipa_debug("enter");
4537 
4538 	if (!wlan_ipa_uc_is_enabled(ipa_ctx->config))
4539 		return status;
4540 
4541 	wlan_ipa_uc_disable_pipes(ipa_ctx, true);
4542 
4543 	cdp_ipa_deregister_op_cb(ipa_ctx->dp_soc, ipa_ctx->dp_pdev_id);
4544 	qdf_atomic_set(&ipa_ctx->deinit_in_prog, 1);
4545 
4546 	for (i = 0; i < WLAN_IPA_UC_OPCODE_MAX; i++) {
4547 		qdf_cancel_work(&ipa_ctx->uc_op_work[i].work);
4548 		qdf_mem_free(ipa_ctx->uc_op_work[i].msg);
4549 		ipa_ctx->uc_op_work[i].msg = NULL;
4550 	}
4551 
4552 	cdp_ipa_iounmap_doorbell_vaddr(ipa_ctx->dp_soc,
4553 				       ipa_ctx->dp_pdev_id);
4554 
4555 	if (true == ipa_ctx->uc_loaded) {
4556 		cdp_ipa_tx_buf_smmu_unmapping(ipa_ctx->dp_soc,
4557 					      ipa_ctx->dp_pdev_id,
4558 					      __func__, __LINE__);
4559 		status = cdp_ipa_cleanup(ipa_ctx->dp_soc,
4560 					 ipa_ctx->dp_pdev_id,
4561 					 ipa_ctx->tx_pipe_handle,
4562 					 ipa_ctx->rx_pipe_handle, ipa_ctx->hdl);
4563 		if (status)
4564 			ipa_err("Failure to cleanup IPA pipes (status=%d)",
4565 				status);
4566 	}
4567 
4568 	qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4569 	wlan_ipa_cleanup_pending_event(ipa_ctx);
4570 	qdf_mutex_release(&ipa_ctx->ipa_lock);
4571 
4572 	ipa_debug("exit: ret=%d", status);
4573 	return status;
4574 }
4575 
4576 /**
4577  * wlan_ipa_uc_send_evt() - send event to ipa
4578  * @net_dev: Interface net device
4579  * @type: event type
4580  * @mac_addr: pointer to mac address
4581  *
4582  * Send event to IPA driver
4583  *
4584  * Return: QDF_STATUS
4585  */
4586 static QDF_STATUS wlan_ipa_uc_send_evt(qdf_netdev_t net_dev,
4587 				       qdf_ipa_wlan_event type,
4588 				       uint8_t *mac_addr,
4589 				       struct wlan_ipa_priv *ipa_priv)
4590 {
4591 	struct wlan_ipa_priv *ipa_ctx;
4592 	qdf_ipa_msg_meta_t meta;
4593 	qdf_ipa_wlan_msg_t *msg;
4594 
4595 	if (!ipa_priv)
4596 		return QDF_STATUS_E_INVAL;
4597 
4598 	ipa_ctx = ipa_priv;
4599 
4600 	QDF_IPA_MSG_META_MSG_LEN(&meta) = sizeof(qdf_ipa_wlan_msg_t);
4601 	msg = qdf_mem_malloc(QDF_IPA_MSG_META_MSG_LEN(&meta));
4602 	if (!msg)
4603 		return QDF_STATUS_E_NOMEM;
4604 
4605 	QDF_IPA_SET_META_MSG_TYPE(&meta, type);
4606 	qdf_str_lcopy(QDF_IPA_WLAN_MSG_NAME(msg), net_dev->name,
4607 		      IPA_RESOURCE_NAME_MAX);
4608 	qdf_mem_copy(QDF_IPA_WLAN_MSG_MAC_ADDR(msg), mac_addr, QDF_NET_ETH_LEN);
4609 	QDF_IPA_WLAN_MSG_NETDEV_IF_ID(msg) = net_dev->ifindex;
4610 
4611 	if (qdf_ipa_send_msg(&meta, msg, wlan_ipa_msg_free_fn)) {
4612 		ipa_err("%s: Evt: %d fail",
4613 			QDF_IPA_WLAN_MSG_NAME(msg),
4614 			QDF_IPA_MSG_META_MSG_TYPE(&meta));
4615 		qdf_mem_free(msg);
4616 
4617 		return QDF_STATUS_E_FAILURE;
4618 	}
4619 
4620 	ipa_ctx->stats.num_send_msg++;
4621 
4622 	return QDF_STATUS_SUCCESS;
4623 }
4624 
4625 void wlan_ipa_uc_cleanup_sta(struct wlan_ipa_priv *ipa_ctx,
4626 			     qdf_netdev_t net_dev)
4627 {
4628 	struct wlan_ipa_iface_context *iface_ctx;
4629 	int i;
4630 
4631 	ipa_debug("enter");
4632 
4633 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4634 		iface_ctx = &ipa_ctx->iface_context[i];
4635 		if (iface_ctx && iface_ctx->device_mode == QDF_STA_MODE &&
4636 		    iface_ctx->dev && iface_ctx->dev == net_dev) {
4637 			wlan_ipa_uc_send_evt(net_dev, QDF_IPA_STA_DISCONNECT,
4638 					     net_dev->dev_addr, ipa_ctx);
4639 			wlan_ipa_cleanup_iface(iface_ctx, NULL);
4640 		}
4641 	}
4642 
4643 	ipa_debug("exit");
4644 }
4645 
4646 QDF_STATUS wlan_ipa_uc_disconnect_ap(struct wlan_ipa_priv *ipa_ctx,
4647 				     qdf_netdev_t net_dev)
4648 {
4649 	struct wlan_ipa_iface_context *iface_ctx;
4650 	QDF_STATUS status;
4651 
4652 	ipa_debug("enter");
4653 
4654 	iface_ctx = wlan_ipa_get_iface(ipa_ctx, QDF_SAP_MODE);
4655 	if (iface_ctx)
4656 		status = wlan_ipa_uc_send_evt(net_dev, QDF_IPA_AP_DISCONNECT,
4657 					      net_dev->dev_addr, ipa_ctx);
4658 	else
4659 		return QDF_STATUS_E_INVAL;
4660 
4661 	ipa_debug("exit :%d", status);
4662 
4663 	return status;
4664 }
4665 
4666 void wlan_ipa_cleanup_dev_iface(struct wlan_ipa_priv *ipa_ctx,
4667 				qdf_netdev_t net_dev)
4668 {
4669 	struct wlan_ipa_iface_context *iface_ctx;
4670 	int i;
4671 
4672 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4673 		iface_ctx = &ipa_ctx->iface_context[i];
4674 		if (iface_ctx->dev == net_dev) {
4675 			wlan_ipa_cleanup_iface(iface_ctx, NULL);
4676 			break;
4677 		}
4678 	}
4679 }
4680 
4681 void wlan_ipa_uc_ssr_cleanup(struct wlan_ipa_priv *ipa_ctx)
4682 {
4683 	struct wlan_ipa_iface_context *iface;
4684 	int i;
4685 
4686 	ipa_info("enter");
4687 
4688 	for (i = 0; i < WLAN_IPA_MAX_IFACE; i++) {
4689 		iface = &ipa_ctx->iface_context[i];
4690 		if (iface->dev) {
4691 			if (iface->device_mode == QDF_SAP_MODE)
4692 				wlan_ipa_uc_send_evt(iface->dev,
4693 						     QDF_IPA_AP_DISCONNECT,
4694 						     iface->dev->dev_addr,
4695 						     ipa_ctx);
4696 			else if (iface->device_mode == QDF_STA_MODE)
4697 				wlan_ipa_uc_send_evt(iface->dev,
4698 						     QDF_IPA_STA_DISCONNECT,
4699 						     iface->dev->dev_addr,
4700 						     ipa_ctx);
4701 			wlan_ipa_cleanup_iface(iface, NULL);
4702 		}
4703 	}
4704 }
4705 
4706 void wlan_ipa_fw_rejuvenate_send_msg(struct wlan_ipa_priv *ipa_ctx)
4707 {
4708 	qdf_ipa_msg_meta_t meta;
4709 	qdf_ipa_wlan_msg_t *msg;
4710 	int ret;
4711 
4712 	meta.msg_len = sizeof(*msg);
4713 	msg = qdf_mem_malloc(meta.msg_len);
4714 	if (!msg)
4715 		return;
4716 
4717 	QDF_IPA_SET_META_MSG_TYPE(&meta, QDF_FWR_SSR_BEFORE_SHUTDOWN);
4718 	ipa_debug("ipa_send_msg(Evt:%d)",
4719 		  meta.msg_type);
4720 	ret = qdf_ipa_send_msg(&meta, msg, wlan_ipa_msg_free_fn);
4721 
4722 	if (ret) {
4723 		ipa_err("ipa_send_msg(Evt:%d)-fail=%d",
4724 			meta.msg_type, ret);
4725 		qdf_mem_free(msg);
4726 	}
4727 	ipa_ctx->stats.num_send_msg++;
4728 }
4729 
4730 void wlan_ipa_flush_pending_vdev_events(struct wlan_ipa_priv *ipa_ctx,
4731 					uint8_t vdev_id)
4732 {
4733 	struct wlan_ipa_uc_pending_event *event;
4734 	struct wlan_ipa_uc_pending_event *next_event;
4735 
4736 	qdf_mutex_acquire(&ipa_ctx->ipa_lock);
4737 
4738 	qdf_list_for_each_del(&ipa_ctx->pending_event, event, next_event,
4739 			      node) {
4740 		if (event->session_id == vdev_id) {
4741 			qdf_list_remove_node(&ipa_ctx->pending_event,
4742 					     &event->node);
4743 			qdf_mem_free(event);
4744 		}
4745 	}
4746 
4747 	qdf_mutex_release(&ipa_ctx->ipa_lock);
4748 }
4749