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