xref: /wlan-dirver/qca-wifi-host-cmn/umac/regulatory/core/src/reg_priv_objs.c (revision d0c05845839e5f2ba5a8dcebe0cd3e4cd4e8dfcf)
1 /*
2  * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: reg_priv_objs.c
22  * This file defines the APIs to create regulatory private PSOC and PDEV
23  * objects.
24  */
25 
26 #include <wlan_cmn.h>
27 #include <reg_services_public_struct.h>
28 #include <wlan_objmgr_psoc_obj.h>
29 #include <wlan_objmgr_pdev_obj.h>
30 #include <qdf_lock.h>
31 #include "reg_priv_objs.h"
32 #include "reg_utils.h"
33 #include "reg_services_common.h"
34 #include "reg_build_chan_list.h"
35 #include "reg_host_11d.h"
36 #include "reg_callbacks.h"
37 #ifdef CONFIG_AFC_SUPPORT
38 #include <cfg_ucfg_api.h>
39 #endif
40 
41 struct wlan_regulatory_psoc_priv_obj *reg_get_psoc_obj(
42 		struct wlan_objmgr_psoc *psoc)
43 {
44 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
45 
46 	if (!psoc) {
47 		reg_alert("psoc is NULL");
48 		return NULL;
49 	}
50 	psoc_priv_obj = wlan_objmgr_psoc_get_comp_private_obj(
51 			psoc, WLAN_UMAC_COMP_REGULATORY);
52 
53 	return psoc_priv_obj;
54 }
55 
56 struct wlan_regulatory_pdev_priv_obj *reg_get_pdev_obj(
57 		struct wlan_objmgr_pdev *pdev)
58 {
59 	struct wlan_regulatory_pdev_priv_obj *pdev_reg;
60 
61 	if (!pdev) {
62 		reg_alert("pdev is NULL");
63 		return NULL;
64 	}
65 
66 	pdev_reg = wlan_objmgr_pdev_get_comp_private_obj(
67 			pdev, WLAN_UMAC_COMP_REGULATORY);
68 
69 	return pdev_reg;
70 }
71 
72 /*
73  * reg_set_5dot9_ghz_chan_in_master_mode - Set 5.9GHz channels to operate
74  * in master mode.
75  * @soc_reg_obj - Pointer to soc_reg_obj.
76  *
77  * Return: void
78  *
79  */
80 #ifdef CONFIG_REG_CLIENT
81 static void
82 reg_set_5dot9_ghz_chan_in_master_mode(struct wlan_regulatory_psoc_priv_obj
83 				      *soc_reg_obj)
84 {
85 	soc_reg_obj->enable_5dot9_ghz_chan_in_master_mode = false;
86 }
87 #else
88 static void
89 reg_set_5dot9_ghz_chan_in_master_mode(struct wlan_regulatory_psoc_priv_obj
90 				      *soc_reg_obj)
91 {
92 	soc_reg_obj->enable_5dot9_ghz_chan_in_master_mode = true;
93 }
94 #endif
95 
96 QDF_STATUS wlan_regulatory_psoc_obj_created_notification(
97 		struct wlan_objmgr_psoc *psoc, void *arg_list)
98 {
99 	struct wlan_regulatory_psoc_priv_obj *soc_reg_obj;
100 	struct regulatory_channel *mas_chan_list;
101 	enum channel_enum chan_enum;
102 	QDF_STATUS status;
103 	uint8_t i;
104 	uint8_t phy_cnt;
105 
106 	soc_reg_obj = qdf_mem_malloc(sizeof(*soc_reg_obj));
107 	if (!soc_reg_obj)
108 		return QDF_STATUS_E_NOMEM;
109 
110 	soc_reg_obj->offload_enabled = false;
111 	soc_reg_obj->psoc_ptr = psoc;
112 	soc_reg_obj->dfs_enabled = true;
113 	soc_reg_obj->band_capability = (BIT(REG_BAND_2G) | BIT(REG_BAND_5G) |
114 					BIT(REG_BAND_6G));
115 	soc_reg_obj->enable_11d_supp = false;
116 	soc_reg_obj->indoor_chan_enabled = true;
117 	soc_reg_obj->force_ssc_disable_indoor_channel = false;
118 	soc_reg_obj->master_vdev_cnt = 0;
119 	soc_reg_obj->vdev_cnt_11d = 0;
120 	soc_reg_obj->vdev_id_for_11d_scan = INVALID_VDEV_ID;
121 	soc_reg_obj->restart_beaconing = CH_AVOID_RULE_RESTART;
122 	soc_reg_obj->enable_srd_chan_in_master_mode = 0xFF;
123 	soc_reg_obj->enable_11d_in_world_mode = false;
124 	soc_reg_obj->five_dot_nine_ghz_supported = false;
125 	reg_set_5dot9_ghz_chan_in_master_mode(soc_reg_obj);
126 	soc_reg_obj->retain_nol_across_regdmn_update = false;
127 	soc_reg_obj->is_ext_tpc_supported = false;
128 	soc_reg_obj->sta_sap_scc_on_indoor_channel = true;
129 
130 	for (i = 0; i < MAX_STA_VDEV_CNT; i++)
131 		soc_reg_obj->vdev_ids_11d[i] = INVALID_VDEV_ID;
132 
133 	qdf_spinlock_create(&soc_reg_obj->cbk_list_lock);
134 
135 	for (phy_cnt = 0; phy_cnt < PSOC_MAX_PHY_REG_CAP; phy_cnt++) {
136 		mas_chan_list =
137 			soc_reg_obj->mas_chan_params[phy_cnt].mas_chan_list;
138 		soc_reg_obj->chan_list_recvd[phy_cnt] = false;
139 
140 		for (chan_enum = 0; chan_enum < NUM_CHANNELS; chan_enum++) {
141 			mas_chan_list[chan_enum].chan_flags |=
142 				REGULATORY_CHAN_DISABLED;
143 			mas_chan_list[chan_enum].state = CHANNEL_STATE_DISABLE;
144 			mas_chan_list[chan_enum].nol_chan = false;
145 		}
146 	}
147 
148 	status = wlan_objmgr_psoc_component_obj_attach(
149 			psoc, WLAN_UMAC_COMP_REGULATORY, soc_reg_obj,
150 			QDF_STATUS_SUCCESS);
151 	if (QDF_IS_STATUS_ERROR(status)) {
152 		qdf_spinlock_destroy(&soc_reg_obj->cbk_list_lock);
153 		qdf_mem_free(soc_reg_obj);
154 		reg_err("Obj attach failed");
155 		return status;
156 	}
157 
158 	reg_debug("reg psoc obj created with status %d", status);
159 
160 	return status;
161 }
162 
163 QDF_STATUS wlan_regulatory_psoc_obj_destroyed_notification(
164 	struct wlan_objmgr_psoc *psoc, void *arg_list)
165 {
166 	QDF_STATUS status;
167 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
168 
169 	psoc_priv_obj = reg_get_psoc_obj(psoc);
170 	if (!psoc_priv_obj) {
171 		reg_err_rl("NULL reg psoc priv obj");
172 		return QDF_STATUS_E_FAULT;
173 	}
174 
175 	psoc_priv_obj->psoc_ptr = NULL;
176 	qdf_spinlock_destroy(&psoc_priv_obj->cbk_list_lock);
177 
178 	status = wlan_objmgr_psoc_component_obj_detach(
179 			psoc, WLAN_UMAC_COMP_REGULATORY, psoc_priv_obj);
180 
181 	if (status != QDF_STATUS_SUCCESS)
182 		reg_err_rl("psoc_priv_obj private obj detach failed");
183 
184 	reg_debug("reg psoc obj detached");
185 
186 	qdf_mem_free(psoc_priv_obj);
187 
188 	return status;
189 }
190 
191 #ifdef DISABLE_UNII_SHARED_BANDS
192 /**
193  * reg_reset_unii_5g_bitmap() - Reset the value of unii_5g_bitmap.
194  * @pdev_priv_obj: pointer to wlan_regulatory_pdev_priv_obj.
195  *
196  * Return : void
197  */
198 static void
199 reg_reset_unii_5g_bitmap(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
200 {
201 	pdev_priv_obj->unii_5g_bitmap = 0x0;
202 }
203 #else
204 static void inline
205 reg_reset_unii_5g_bitmap(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
206 {
207 }
208 #endif
209 
210 #if defined(CONFIG_BAND_6GHZ)
211 #if defined(CONFIG_REG_CLIENT)
212 /**
213  * reg_init_def_client_type() - Initialize the regulatory 6G client type.
214  *
215  * @pdev_priv_obj: pointer to wlan_regulatory_pdev_priv_obj.
216  *
217  * Return : void
218  */
219 static void
220 reg_init_def_client_type(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
221 {
222 	pdev_priv_obj->reg_cur_6g_client_mobility_type = REG_DEFAULT_CLIENT;
223 }
224 #else
225 static void
226 reg_init_def_client_type(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
227 {
228 	pdev_priv_obj->reg_cur_6g_client_mobility_type = REG_SUBORDINATE_CLIENT;
229 }
230 #endif
231 
232 /**
233  * reg_init_6g_vars() - Initialize the regulatory 6G variables viz.
234  * AP power type, client mobility type, rnr tpe usable and unspecified ap
235  * usable.
236  * @pdev_priv_obj: pointer to wlan_regulatory_pdev_priv_obj.
237  *
238  * Return : void
239  */
240 static void
241 reg_init_6g_vars(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
242 {
243 	reg_set_ap_pwr_type(pdev_priv_obj);
244 	pdev_priv_obj->reg_rnr_tpe_usable = false;
245 	pdev_priv_obj->reg_unspecified_ap_usable = false;
246 	reg_init_def_client_type(pdev_priv_obj);
247 }
248 #else
249 static void
250 reg_init_6g_vars(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
251 {
252 }
253 #endif
254 
255 #ifdef CONFIG_AFC_SUPPORT
256 static void
257 reg_create_afc_cb_spinlock(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
258 {
259 	qdf_spinlock_create(&pdev_priv_obj->afc_cb_lock);
260 }
261 
262 static void
263 reg_destroy_afc_cb_spinlock(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
264 {
265 	qdf_spinlock_destroy(&pdev_priv_obj->afc_cb_lock);
266 }
267 
268 static void
269 reg_init_afc_vars(struct wlan_objmgr_psoc *psoc,
270 		  struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
271 {
272 	pdev_priv_obj->is_reg_noaction_on_afc_pwr_evt =
273 			cfg_get(psoc, CFG_OL_AFC_REG_NO_ACTION);
274 }
275 
276 static inline void
277 reg_set_pdev_afc_dev_type(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj,
278 			  struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj)
279 {
280 	pdev_priv_obj->reg_afc_dev_deployment_type =
281 		psoc_priv_obj->reg_afc_dev_type;
282 }
283 #else
284 static inline void
285 reg_create_afc_cb_spinlock(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
286 {
287 }
288 
289 static inline void
290 reg_destroy_afc_cb_spinlock(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
291 {
292 }
293 
294 static void
295 reg_init_afc_vars(struct wlan_objmgr_psoc *psoc,
296 		  struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
297 {
298 }
299 
300 static inline void
301 reg_set_pdev_afc_dev_type(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj,
302 			  struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj)
303 {
304 }
305 #endif
306 
307 QDF_STATUS wlan_regulatory_pdev_obj_created_notification(
308 	struct wlan_objmgr_pdev *pdev, void *arg_list)
309 {
310 	struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj;
311 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
312 	struct wlan_psoc_host_hal_reg_capabilities_ext *reg_cap_ptr;
313 	struct wlan_objmgr_psoc *parent_psoc;
314 	uint8_t pdev_id;
315 	uint8_t phy_id;
316 	uint32_t cnt;
317 	uint32_t range_2g_low, range_2g_high;
318 	uint32_t range_5g_low, range_5g_high;
319 	QDF_STATUS status;
320 	struct reg_rule_info *psoc_reg_rules;
321 	struct wlan_lmac_if_reg_tx_ops *tx_ops;
322 
323 	pdev_priv_obj = qdf_mem_malloc(sizeof(*pdev_priv_obj));
324 	if (!pdev_priv_obj)
325 		return QDF_STATUS_E_NOMEM;
326 
327 	parent_psoc = wlan_pdev_get_psoc(pdev);
328 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
329 	tx_ops = reg_get_psoc_tx_ops(parent_psoc);
330 
331 	if (tx_ops->get_phy_id_from_pdev_id)
332 		tx_ops->get_phy_id_from_pdev_id(parent_psoc, pdev_id, &phy_id);
333 	else
334 		phy_id = pdev_id;
335 
336 	psoc_priv_obj = reg_get_psoc_obj(parent_psoc);
337 	if (!psoc_priv_obj) {
338 		reg_err("reg psoc private obj is NULL");
339 		qdf_mem_free(pdev_priv_obj);
340 		return QDF_STATUS_E_FAULT;
341 	}
342 
343 	pdev_priv_obj->pdev_ptr = pdev;
344 	pdev_priv_obj->dfs_enabled = psoc_priv_obj->dfs_enabled;
345 	pdev_priv_obj->set_fcc_channel = false;
346 	pdev_priv_obj->band_capability = psoc_priv_obj->band_capability;
347 	pdev_priv_obj->indoor_chan_enabled =
348 		psoc_priv_obj->indoor_chan_enabled;
349 
350 	reg_set_pdev_afc_dev_type(pdev_priv_obj, psoc_priv_obj);
351 
352 	pdev_priv_obj->en_chan_144 = true;
353 	reg_reset_unii_5g_bitmap(pdev_priv_obj);
354 
355 	qdf_spinlock_create(&pdev_priv_obj->reg_rules_lock);
356 	reg_create_afc_cb_spinlock(pdev_priv_obj);
357 
358 	reg_cap_ptr = psoc_priv_obj->reg_cap;
359 	pdev_priv_obj->force_ssc_disable_indoor_channel =
360 		psoc_priv_obj->force_ssc_disable_indoor_channel;
361 	pdev_priv_obj->sta_sap_scc_on_indoor_channel =
362 		psoc_priv_obj->sta_sap_scc_on_indoor_channel;
363 
364 	for (cnt = 0; cnt < PSOC_MAX_PHY_REG_CAP; cnt++) {
365 		if (!reg_cap_ptr) {
366 			qdf_mem_free(pdev_priv_obj);
367 			reg_err("reg cap ptr is NULL");
368 			return QDF_STATUS_E_FAULT;
369 		}
370 
371 		if (reg_cap_ptr->phy_id == phy_id)
372 			break;
373 		reg_cap_ptr++;
374 	}
375 
376 	if (cnt == PSOC_MAX_PHY_REG_CAP) {
377 		qdf_mem_free(pdev_priv_obj);
378 		reg_err("extended capabilities not found for pdev");
379 		return QDF_STATUS_E_FAULT;
380 	}
381 
382 	range_2g_low = reg_cap_ptr->low_2ghz_chan;
383 	range_2g_high = reg_cap_ptr->high_2ghz_chan;
384 	range_5g_low = reg_cap_ptr->low_5ghz_chan;
385 	range_5g_high = reg_cap_ptr->high_5ghz_chan;
386 
387 	pdev_priv_obj->range_2g_low = range_2g_low;
388 	pdev_priv_obj->range_2g_high = range_2g_high;
389 	pdev_priv_obj->range_5g_low = range_5g_low;
390 	pdev_priv_obj->range_5g_high = range_5g_high;
391 	pdev_priv_obj->wireless_modes = reg_cap_ptr->wireless_modes;
392 	reg_init_6g_vars(pdev_priv_obj);
393 
394 	reg_init_pdev_mas_chan_list(pdev_priv_obj,
395 				    &psoc_priv_obj->mas_chan_params[phy_id]);
396 
397 	psoc_reg_rules = &psoc_priv_obj->mas_chan_params[phy_id].reg_rules;
398 	reg_save_reg_rules_to_pdev(psoc_reg_rules, pdev_priv_obj);
399 	pdev_priv_obj->chan_list_recvd =
400 		psoc_priv_obj->chan_list_recvd[phy_id];
401 
402 	status = wlan_objmgr_pdev_component_obj_attach(
403 			pdev, WLAN_UMAC_COMP_REGULATORY, pdev_priv_obj,
404 			QDF_STATUS_SUCCESS);
405 	if (QDF_IS_STATUS_ERROR(status)) {
406 		reg_err("Obj attach failed");
407 		qdf_mem_free(pdev_priv_obj);
408 		return status;
409 	}
410 
411 	reg_compute_pdev_current_chan_list(pdev_priv_obj);
412 
413 	reg_init_afc_vars(parent_psoc, pdev_priv_obj);
414 
415 	if (!psoc_priv_obj->is_11d_offloaded)
416 		reg_11d_host_scan_init(parent_psoc);
417 
418 	reg_debug("reg pdev obj created with status %d", status);
419 
420 	return status;
421 }
422 
423 #ifdef CONFIG_AFC_SUPPORT
424 /**
425  * reg_free_chan_obj() - Free the AFC chan object and chan eirp object
426  * information
427  * @afc_chan_info: Pointer to afc_chan_info
428  *
429  * Return: void
430  */
431 static void reg_free_chan_obj(struct afc_chan_obj *afc_chan_info)
432 {
433 	if (afc_chan_info->chan_eirp_info)
434 		qdf_mem_free(afc_chan_info->chan_eirp_info);
435 }
436 
437 /**
438  * reg_free_afc_pwr_info() - Free the AFC power information object
439  * @pdev_priv_obj: Pointer to pdev_priv_obj
440  *
441  * Return: void
442  */
443 void
444 reg_free_afc_pwr_info(struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj)
445 {
446 	struct reg_fw_afc_power_event *power_info;
447 	uint8_t i;
448 
449 	power_info = pdev_priv_obj->power_info;
450 	if (!power_info)
451 		return;
452 
453 	if (power_info->afc_freq_info)
454 		qdf_mem_free(power_info->afc_freq_info);
455 
456 	if (!power_info->afc_chan_info)
457 		return;
458 
459 	for (i = 0; i < power_info->num_chan_objs; i++)
460 		reg_free_chan_obj(&power_info->afc_chan_info[i]);
461 
462 	if (power_info->afc_chan_info)
463 		qdf_mem_free(power_info->afc_chan_info);
464 
465 	qdf_mem_free(power_info);
466 }
467 #endif
468 
469 QDF_STATUS wlan_regulatory_pdev_obj_destroyed_notification(
470 		struct wlan_objmgr_pdev *pdev, void *arg_list)
471 {
472 	QDF_STATUS status;
473 	struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj;
474 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
475 	uint32_t pdev_id;
476 
477 	pdev_priv_obj = reg_get_pdev_obj(pdev);
478 
479 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
480 
481 	if (!IS_VALID_PDEV_REG_OBJ(pdev_priv_obj)) {
482 		reg_err("reg pdev private obj is NULL");
483 		return QDF_STATUS_E_FAILURE;
484 	}
485 
486 	psoc_priv_obj = reg_get_psoc_obj(wlan_pdev_get_psoc(pdev));
487 	if (!IS_VALID_PSOC_REG_OBJ(psoc_priv_obj)) {
488 		reg_err("reg psoc private obj is NULL");
489 		return QDF_STATUS_E_FAILURE;
490 	}
491 
492 	if (!psoc_priv_obj->is_11d_offloaded)
493 		reg_11d_host_scan_deinit(wlan_pdev_get_psoc(pdev));
494 
495 	reg_free_afc_pwr_info(pdev_priv_obj);
496 	pdev_priv_obj->pdev_ptr = NULL;
497 
498 	status = wlan_objmgr_pdev_component_obj_detach(
499 			pdev, WLAN_UMAC_COMP_REGULATORY, pdev_priv_obj);
500 
501 	if (status != QDF_STATUS_SUCCESS)
502 		reg_err("reg pdev private obj detach failed");
503 
504 	reg_debug("reg pdev obj deleted");
505 
506 	qdf_spin_lock_bh(&pdev_priv_obj->reg_rules_lock);
507 	reg_reset_reg_rules(&pdev_priv_obj->reg_rules);
508 	qdf_spin_unlock_bh(&pdev_priv_obj->reg_rules_lock);
509 
510 	reg_destroy_afc_cb_spinlock(pdev_priv_obj);
511 	qdf_spinlock_destroy(&pdev_priv_obj->reg_rules_lock);
512 
513 	qdf_mem_free(pdev_priv_obj);
514 
515 	return status;
516 }
517