1 /*
2  * Copyright (c) 2016-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 #ifndef _DP_TXRX_WDS_H_
21 #define _DP_TXRX_WDS_H_
22 
23 #ifdef WIFI_MONITOR_SUPPORT
24 #include "dp_htt.h"
25 #include "dp_mon.h"
26 #endif
27 
28 /* host managed flag */
29 #define DP_AST_FLAGS_HM 0x0020
30 
31 /* WDS AST entry aging timer value */
32 #define DP_WDS_AST_AGING_TIMER_DEFAULT_MS	120000
33 #define DP_WDS_AST_AGING_TIMER_CNT \
34 ((DP_WDS_AST_AGING_TIMER_DEFAULT_MS / DP_AST_AGING_TIMER_DEFAULT_MS) - 1)
35 
36 /**
37  * dp_soc_wds_attach() - Setup WDS timer and AST table
38  * @soc: Datapath SOC handle
39  *
40  * Return: None
41  */
42 void dp_soc_wds_attach(struct dp_soc *soc);
43 
44 /**
45  * dp_soc_wds_detach() - Detach WDS data structures and timers
46  * @soc: DP SOC handle
47  *
48  * Return: None
49  */
50 void dp_soc_wds_detach(struct dp_soc *soc);
51 #ifdef QCA_PEER_MULTIQ_SUPPORT
52 /**
53  * dp_peer_find_ast_index_by_flowq_id() - API to get ast idx for a given flowid
54  * @soc: soc handle
55  * @vdev_id: vdev ID
56  * @peer_mac_addr: mac address of the peer
57  * @flow_id: flow id to find ast index
58  * @tid: TID
59  *
60  * Return: ast index for a given flow id, -1 for fail cases
61  */
62 int dp_peer_find_ast_index_by_flowq_id(struct cdp_soc_t *soc,
63 	       uint16_t vdev_id, uint8_t *peer_mac_addr,
64 	       uint8_t flow_id, uint8_t tid);
65 #endif
66 
67 /**
68  * dp_rx_da_learn() - Add AST entry based on DA lookup
69  *			This is a WAR for HK 1.0 and will
70  *			be removed in HK 2.0
71  *
72  * @soc: core txrx main context
73  * @rx_tlv_hdr: start address of rx tlvs
74  * @ta_peer: Transmitter peer entry
75  * @nbuf: nbuf to retrieve destination mac for which AST will be added
76  *
77  */
78 void
79 dp_rx_da_learn(struct dp_soc *soc,
80 	       uint8_t *rx_tlv_hdr,
81 	       struct dp_txrx_peer *ta_peer,
82 	       qdf_nbuf_t nbuf);
83 
84 /**
85  * dp_tx_mec_handler() - Tx  MEC Notify Handler
86  * @vdev: pointer to dp dev handler
87  * @status : Tx completion status from HTT descriptor
88  *
89  * Handles MEC notify event sent from fw to Host
90  *
91  * Return: none
92  */
93 void dp_tx_mec_handler(struct dp_vdev *vdev, uint8_t *status);
94 #ifdef FEATURE_WDS
95 #ifdef FEATURE_MCL_REPEATER
dp_tx_da_search_override(struct dp_vdev * vdev)96 static inline bool dp_tx_da_search_override(struct dp_vdev *vdev)
97 {
98 	if (vdev->mec_enabled)
99 		return true;
100 
101 	return false;
102 }
103 #else
dp_tx_da_search_override(struct dp_vdev * vdev)104 static inline bool dp_tx_da_search_override(struct dp_vdev *vdev)
105 {
106 	struct dp_soc *soc = vdev->pdev->soc;
107 
108 	/*
109 	 * If AST index override support is available (HKv2 etc),
110 	 * DA search flag be enabled always
111 	 *
112 	 * If AST index override support is not available (HKv1),
113 	 * DA search flag should be used for all modes except QWRAP
114 	 */
115 	if (soc->ast_override_support || !vdev->proxysta_vdev)
116 		return true;
117 
118 	return false;
119 }
120 #endif /* FEATURE_MCL_REPEATER */
121 #endif /* FEATURE_WDS */
122 #ifdef WDS_VENDOR_EXTENSION
123 
124 /**
125  * dp_txrx_peer_wds_tx_policy_update() - API to set tx wds policy
126  *
127  * @soc: DP soc handle
128  * @vdev_id: id of vdev handle
129  * @peer_mac: peer mac address
130  * @wds_tx_ucast: policy for unicast transmission
131  * @wds_tx_mcast: policy for multicast transmission
132  *
133  * Return: void
134  */
135 QDF_STATUS
136 dp_txrx_peer_wds_tx_policy_update(struct cdp_soc_t *soc,  uint8_t vdev_id,
137 				  uint8_t *peer_mac, int wds_tx_ucast,
138 				  int wds_tx_mcast);
139 
140 /**
141  * dp_txrx_set_wds_rx_policy() - API to store datapath
142  *                            config parameters
143  * @soc_hdl: datapath soc handle
144  * @vdev_id: id of datapath vdev handle
145  * @val: WDS rx policy value
146  *
147  * Return: status
148  */
149 QDF_STATUS
150 dp_txrx_set_wds_rx_policy(struct cdp_soc_t *soc_hdl, uint8_t vdev_id,
151 			  u_int32_t val);
152 #endif
153 
154 /**
155  * dp_hmwds_ast_add_notify() - schedules hmwds ast add status work
156  * @peer: DP peer
157  * @mac_addr: ast mac addr
158  * @type: ast type
159  * @err: QDF_STATUS error code
160  * @is_peer_map: notify is from peer map
161  *
162  * Return: void
163  */
164 void dp_hmwds_ast_add_notify(struct dp_peer *peer,
165 			     uint8_t *mac_addr,
166 			     enum cdp_txrx_ast_entry_type type,
167 			     QDF_STATUS err,
168 			     bool is_peer_map);
169 
170 #ifdef QCA_SUPPORT_WDS_EXTENDED
171 /**
172  * dp_wds_ext_peer_learn() - function to send event to control
173  * path on receiving 1st 4-address frame from backhaul.
174  * @soc: DP soc
175  * @ta_peer: WDS repeater peer
176  *
177  * Return: void
178  */
dp_wds_ext_peer_learn(struct dp_soc * soc,struct dp_peer * ta_peer)179 static inline void dp_wds_ext_peer_learn(struct dp_soc *soc,
180 					 struct dp_peer *ta_peer)
181 {
182 	uint8_t wds_ext_src_mac[QDF_MAC_ADDR_SIZE];
183 
184 	if (ta_peer->vdev->wds_ext_enabled &&
185 	    !qdf_atomic_test_and_set_bit(WDS_EXT_PEER_INIT_BIT,
186 					 &ta_peer->txrx_peer->wds_ext.init)) {
187 		qdf_mem_copy(wds_ext_src_mac, &ta_peer->mac_addr.raw[0],
188 			     QDF_MAC_ADDR_SIZE);
189 		soc->cdp_soc.ol_ops->rx_wds_ext_peer_learn(
190 						soc->ctrl_psoc,
191 						ta_peer->peer_id,
192 						ta_peer->vdev->vdev_id,
193 						wds_ext_src_mac);
194 	}
195 }
196 #else
dp_wds_ext_peer_learn(struct dp_soc * soc,struct dp_peer * ta_peer)197 static inline void dp_wds_ext_peer_learn(struct dp_soc *soc,
198 					 struct dp_peer *ta_peer)
199 {
200 }
201 #endif
202 
203 /**
204  * dp_rx_wds_add_or_update_ast() - Add or update the ast entry.
205  *
206  * @soc: core txrx main context
207  * @ta_peer: WDS repeater txrx peer
208  * @nbuf: network buffer
209  * @is_ad4_valid: 4-address valid flag
210  * @is_sa_valid: source address valid flag
211  * @is_chfrag_start: frag start flag
212  * @sa_idx: source-address index for peer
213  * @sa_sw_peer_id: software source-address peer-id
214  *
215  * Return: void:
216  */
217 static inline void
dp_rx_wds_add_or_update_ast(struct dp_soc * soc,struct dp_txrx_peer * ta_peer,qdf_nbuf_t nbuf,uint8_t is_ad4_valid,uint8_t is_sa_valid,uint8_t is_chfrag_start,uint16_t sa_idx,uint16_t sa_sw_peer_id)218 dp_rx_wds_add_or_update_ast(struct dp_soc *soc,
219 			    struct dp_txrx_peer *ta_peer,
220 			    qdf_nbuf_t nbuf, uint8_t is_ad4_valid,
221 			    uint8_t is_sa_valid, uint8_t is_chfrag_start,
222 			    uint16_t sa_idx, uint16_t sa_sw_peer_id)
223 {
224 	struct dp_peer *sa_peer;
225 	struct dp_ast_entry *ast;
226 	uint32_t flags = DP_AST_FLAGS_HM;
227 	struct dp_pdev *pdev = ta_peer->vdev->pdev;
228 	uint8_t wds_src_mac[QDF_MAC_ADDR_SIZE];
229 	struct dp_peer *ta_base_peer;
230 
231 
232 	if (!(is_chfrag_start && is_ad4_valid))
233 		return;
234 
235 	if (qdf_unlikely(!is_sa_valid)) {
236 		qdf_mem_copy(wds_src_mac,
237 			     (qdf_nbuf_data(nbuf) + QDF_MAC_ADDR_SIZE),
238 			     QDF_MAC_ADDR_SIZE);
239 
240 		ta_base_peer = dp_peer_get_ref_by_id(soc, ta_peer->peer_id,
241 						     DP_MOD_ID_RX);
242 		if (ta_base_peer) {
243 			if (ta_peer->vdev->opmode == wlan_op_mode_ap)
244 				dp_wds_ext_peer_learn(soc, ta_base_peer);
245 
246 			dp_peer_add_ast(soc, ta_base_peer, wds_src_mac,
247 					CDP_TXRX_AST_TYPE_WDS, flags);
248 
249 			dp_peer_unref_delete(ta_base_peer, DP_MOD_ID_RX);
250 		}
251 		return;
252 	}
253 
254 	qdf_spin_lock_bh(&soc->ast_lock);
255 	ast = soc->ast_table[sa_idx];
256 
257 	if (!ast) {
258 		qdf_spin_unlock_bh(&soc->ast_lock);
259 		/*
260 		 * In HKv1, it is possible that HW retains the AST entry in
261 		 * GSE cache on 1 radio , even after the AST entry is deleted
262 		 * (on another radio).
263 		 *
264 		 * Due to this, host might still get sa_is_valid indications
265 		 * for frames with SA not really present in AST table.
266 		 *
267 		 * So we go ahead and send an add_ast command to FW in such
268 		 * cases where sa is reported still as valid, so that FW will
269 		 * invalidate this GSE cache entry and new AST entry gets
270 		 * cached.
271 		 */
272 		if (!soc->ast_override_support) {
273 			qdf_mem_copy(wds_src_mac,
274 				     (qdf_nbuf_data(nbuf) + QDF_MAC_ADDR_SIZE),
275 				     QDF_MAC_ADDR_SIZE);
276 
277 			ta_base_peer = dp_peer_get_ref_by_id(soc,
278 							     ta_peer->peer_id,
279 							     DP_MOD_ID_RX);
280 
281 			if (ta_base_peer) {
282 				dp_peer_add_ast(soc, ta_base_peer,
283 						wds_src_mac,
284 						CDP_TXRX_AST_TYPE_WDS,
285 						flags);
286 
287 				dp_peer_unref_delete(ta_base_peer,
288 						     DP_MOD_ID_RX);
289 			}
290 			return;
291 		} else {
292 			/* In HKv2 smart monitor case, when NAC client is
293 			 * added first and this client roams within BSS to
294 			 * connect to RE, since we have an AST entry for
295 			 * NAC we get sa_is_valid bit set. So we check if
296 			 * smart monitor is enabled and send add_ast command
297 			 * to FW.
298 			 */
299 			ta_base_peer = dp_peer_get_ref_by_id(soc,
300 							     ta_peer->peer_id,
301 							     DP_MOD_ID_RX);
302 			if (ta_base_peer) {
303 				dp_monitor_neighbour_peer_add_ast(pdev,
304 								  ta_base_peer,
305 								  wds_src_mac,
306 								  nbuf,
307 								  flags);
308 				dp_peer_unref_delete(ta_base_peer,
309 						     DP_MOD_ID_RX);
310 			}
311 			return;
312 		}
313 	}
314 
315 	/*
316 	 * Ensure we are updating the right AST entry by
317 	 * validating ast_idx.
318 	 * There is a possibility we might arrive here without
319 	 * AST MAP event , so this check is mandatory
320 	 */
321 	if (ast->is_mapped && (ast->ast_idx == sa_idx))
322 		ast->is_active = TRUE;
323 
324 	if (ast->peer_id != ta_peer->peer_id) {
325 		if ((ast->type == CDP_TXRX_AST_TYPE_WDS_HM) ||
326 		    (ast->type == CDP_TXRX_AST_TYPE_WDS_HM_SEC)) {
327 			qdf_spin_unlock_bh(&soc->ast_lock);
328 			return;
329 		}
330 
331 		if ((ast->type != CDP_TXRX_AST_TYPE_STATIC) &&
332 		    (ast->type != CDP_TXRX_AST_TYPE_SELF) &&
333 		    (ast->type != CDP_TXRX_AST_TYPE_STA_BSS)) {
334 			if (ast->pdev_id != ta_peer->vdev->pdev->pdev_id) {
335 				/* This case is when a STA roams from one
336 				 * repeater to another repeater, but these
337 				 * repeaters are connected to root AP on
338 				 * different radios.
339 				 * Ex: rptr1 connected to ROOT AP over 5G
340 				 * and rptr2 connected to ROOT AP over 2G
341 				 * radio
342 				 */
343 				dp_peer_del_ast(soc, ast);
344 				qdf_spin_unlock_bh(&soc->ast_lock);
345 				return;
346 			} else {
347 				/* this case is when a STA roams from one
348 				 * reapter to another repeater, but inside
349 				 * same radio.
350 				 */
351 				/* For HKv2 do not update the AST entry if
352 				 * new ta_peer is on STA vap as SRC port
353 				 * learning is disable on STA vap
354 				 */
355 				if (soc->ast_override_support &&
356 				    (ta_peer->vdev->opmode == wlan_op_mode_sta)) {
357 					dp_peer_del_ast(soc, ast);
358 					qdf_spin_unlock_bh(&soc->ast_lock);
359 					return;
360 				} else {
361 					ta_base_peer =
362 					dp_peer_get_ref_by_id(soc,
363 							      ta_peer->peer_id,
364 							      DP_MOD_ID_RX);
365 					if (ta_base_peer) {
366 						dp_wds_ext_peer_learn(soc,
367 								ta_base_peer);
368 						dp_peer_update_ast(soc,
369 								   ta_base_peer,
370 								   ast, flags);
371 
372 						dp_peer_unref_delete(ta_base_peer,
373 								DP_MOD_ID_RX);
374 					}
375 				}
376 				qdf_spin_unlock_bh(&soc->ast_lock);
377 				return;
378 			}
379 		}
380 		/*
381 		 * Do not kickout STA if it belongs to a different radio.
382 		 * For DBDC repeater, it is possible to arrive here
383 		 * for multicast loopback frames originated from connected
384 		 * clients and looped back (intrabss) by Root AP
385 		 */
386 		if (ast->pdev_id != ta_peer->vdev->pdev->pdev_id) {
387 			qdf_spin_unlock_bh(&soc->ast_lock);
388 			return;
389 		}
390 
391 		qdf_spin_unlock_bh(&soc->ast_lock);
392 		/*
393 		 * Kickout, when direct associated peer(SA) roams
394 		 * to another AP and reachable via TA peer
395 		 */
396 		sa_peer = dp_peer_get_ref_by_id(soc, ast->peer_id,
397 						DP_MOD_ID_RX);
398 		if (!sa_peer)
399 			return;
400 
401 		if ((sa_peer->vdev->opmode == wlan_op_mode_ap) &&
402 		    !sa_peer->delete_in_progress) {
403 			qdf_mem_copy(wds_src_mac,
404 				     (qdf_nbuf_data(nbuf) + QDF_MAC_ADDR_SIZE),
405 				     QDF_MAC_ADDR_SIZE);
406 			sa_peer->delete_in_progress = true;
407 			if (soc->cdp_soc.ol_ops->peer_sta_kickout) {
408 				soc->cdp_soc.ol_ops->peer_sta_kickout(
409 					soc->ctrl_psoc,
410 					sa_peer->vdev->pdev->pdev_id,
411 					wds_src_mac);
412 			}
413 		}
414 		dp_peer_unref_delete(sa_peer, DP_MOD_ID_RX);
415 		return;
416 	}
417 
418 	qdf_spin_unlock_bh(&soc->ast_lock);
419 }
420 
421 /**
422  * dp_rx_wds_srcport_learn() - Add or update the STA PEER which
423  *				is behind the WDS repeater.
424  * @soc: core txrx main context
425  * @rx_tlv_hdr: base address of RX TLV header
426  * @ta_peer: WDS repeater peer
427  * @nbuf: rx pkt
428  * @msdu_end_info:
429  *
430  * Return: void:
431  */
432 static inline void
dp_rx_wds_srcport_learn(struct dp_soc * soc,uint8_t * rx_tlv_hdr,struct dp_txrx_peer * ta_peer,qdf_nbuf_t nbuf,struct hal_rx_msdu_metadata msdu_end_info)433 dp_rx_wds_srcport_learn(struct dp_soc *soc,
434 			uint8_t *rx_tlv_hdr,
435 			struct dp_txrx_peer *ta_peer,
436 			qdf_nbuf_t nbuf,
437 			struct hal_rx_msdu_metadata msdu_end_info)
438 {
439 	uint8_t sa_is_valid = qdf_nbuf_is_sa_valid(nbuf);
440 	uint8_t is_chfrag_start = 0;
441 	uint8_t is_ad4_valid = 0;
442 
443 	if (qdf_unlikely(!ta_peer))
444 		return;
445 
446 	is_chfrag_start = qdf_nbuf_is_rx_chfrag_start(nbuf);
447 	if (is_chfrag_start)
448 		is_ad4_valid = hal_rx_get_mpdu_mac_ad4_valid(soc->hal_soc, rx_tlv_hdr);
449 
450 
451 	/*
452 	 * Get the AST entry from HW SA index and mark it as active
453 	 */
454 	dp_rx_wds_add_or_update_ast(soc, ta_peer, nbuf, is_ad4_valid,
455 				    sa_is_valid, is_chfrag_start,
456 				    msdu_end_info.sa_idx, msdu_end_info.sa_sw_peer_id);
457 }
458 
459 #ifdef IPA_WDS_EASYMESH_FEATURE
460 /**
461  * dp_rx_ipa_wds_srcport_learn() - Add or update the STA PEER which
462  *				is behind the WDS repeater.
463  *
464  * @soc: core txrx main context
465  * @ta_peer: WDS repeater peer
466  * @nbuf: rx pkt
467  * @msdu_end_info: msdu end info
468  * @ad4_valid: address4 valid bit
469  * @chfrag_start: Msdu start bit
470  *
471  * Return: void
472  */
473 static inline void
dp_rx_ipa_wds_srcport_learn(struct dp_soc * soc,struct dp_peer * ta_peer,qdf_nbuf_t nbuf,struct hal_rx_msdu_metadata msdu_end_info,bool ad4_valid,bool chfrag_start)474 dp_rx_ipa_wds_srcport_learn(struct dp_soc *soc,
475 			    struct dp_peer *ta_peer, qdf_nbuf_t nbuf,
476 			    struct hal_rx_msdu_metadata msdu_end_info,
477 			    bool ad4_valid, bool chfrag_start)
478 {
479 	uint8_t sa_is_valid = qdf_nbuf_is_sa_valid(nbuf);
480 	uint8_t is_chfrag_start = (uint8_t)chfrag_start;
481 	uint8_t is_ad4_valid = (uint8_t)ad4_valid;
482 	struct dp_txrx_peer *peer = (struct dp_txrx_peer *)ta_peer;
483 
484 	if (qdf_unlikely(!ta_peer))
485 		return;
486 
487 	/*
488 	 * Get the AST entry from HW SA index and mark it as active
489 	 */
490 	dp_rx_wds_add_or_update_ast(soc, peer, nbuf, is_ad4_valid,
491 				    sa_is_valid, is_chfrag_start,
492 				    msdu_end_info.sa_idx,
493 				    msdu_end_info.sa_sw_peer_id);
494 }
495 #endif
496 
497 /**
498  * dp_rx_ast_set_active() - set the active flag of the astentry
499  *				    corresponding to a hw index.
500  * @soc: core txrx main context
501  * @sa_idx: hw idx
502  * @is_active: active flag
503  *
504  */
dp_rx_ast_set_active(struct dp_soc * soc,uint16_t sa_idx,bool is_active)505 static inline QDF_STATUS dp_rx_ast_set_active(struct dp_soc *soc,
506 					      uint16_t sa_idx, bool is_active)
507 {
508 	struct dp_ast_entry *ast;
509 
510 	qdf_spin_lock_bh(&soc->ast_lock);
511 	ast = soc->ast_table[sa_idx];
512 
513 	if (!ast) {
514 		qdf_spin_unlock_bh(&soc->ast_lock);
515 		return QDF_STATUS_E_NULL_VALUE;
516 	}
517 
518 	if (!ast->is_mapped) {
519 		qdf_spin_unlock_bh(&soc->ast_lock);
520 		return QDF_STATUS_E_INVAL;
521 	}
522 
523 	/*
524 	 * Ensure we are updating the right AST entry by
525 	 * validating ast_idx.
526 	 * There is a possibility we might arrive here without
527 	 * AST MAP event , so this check is mandatory
528 	 */
529 	if (ast->ast_idx == sa_idx) {
530 		ast->is_active = is_active;
531 		qdf_spin_unlock_bh(&soc->ast_lock);
532 		return QDF_STATUS_SUCCESS;
533 	}
534 
535 	qdf_spin_unlock_bh(&soc->ast_lock);
536 	return QDF_STATUS_E_FAILURE;
537 }
538 #endif /* DP_TXRX_WDS*/
539