xref: /wlan-dirver/qca-wifi-host-cmn/target_if/regulatory/src/target_if_reg.c (revision 11f5a63a6cbdda84849a730de22f0a71e635d58c)
1 /*
2  * Copyright (c) 2017-2019 The Linux Foundation. All rights reserved.
3  *
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: target_if_reg.c
22  * This file contains regulatory target interfaces.
23  */
24 
25 #include <wmi_unified_api.h>
26 #include <reg_services_public_struct.h>
27 #include <wlan_reg_tgt_api.h>
28 #include <target_if.h>
29 #include <target_if_reg.h>
30 #include <wmi_unified_reg_api.h>
31 #include <qdf_platform.h>
32 #include <target_if_reg_11d.h>
33 #include <target_if_reg_lte.h>
34 #include <wlan_reg_ucfg_api.h>
35 
36 /**
37  * get_chan_list_cc_event_id() - Get chan_list_cc event i
38  *
39  * Return: Event id
40  */
41 static inline uint32_t get_chan_list_cc_event_id(void)
42 {
43 	return wmi_reg_chan_list_cc_event_id;
44 }
45 
46 /**
47  * tgt_if_regulatory_is_regdb_offloaded() - Check if regdb is offloaded
48  * @psoc: Pointer to psoc
49  *
50  * Return: true if regdb if offloaded, else false
51  */
52 static bool tgt_if_regulatory_is_regdb_offloaded(struct wlan_objmgr_psoc *psoc)
53 {
54 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
55 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
56 
57 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
58 
59 	if (!wmi_handle)
60 		return false;
61 
62 	if (reg_rx_ops->reg_ignore_fw_reg_offload_ind &&
63 	    reg_rx_ops->reg_ignore_fw_reg_offload_ind(psoc)) {
64 		target_if_debug("User disabled regulatory offload from ini");
65 		return 0;
66 	}
67 
68 	return wmi_service_enabled(wmi_handle, wmi_service_regulatory_db);
69 }
70 
71 /**
72  * tgt_if_regulatory_is_there_serv_ready_extn() - Check for service ready
73  * extension
74  * @psoc: Pointer to psoc object
75  *
76  * Return: true if service ready extension is present, else false.
77  */
78 static bool tgt_if_regulatory_is_there_serv_ready_extn(
79 		struct wlan_objmgr_psoc *psoc)
80 {
81 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
82 
83 	if (!wmi_handle)
84 		return false;
85 
86 	return wmi_service_enabled(wmi_handle, wmi_service_ext_msg);
87 }
88 
89 /**
90  * target_if_regulatory_get_rx_ops() - Get regdb rx ops
91  * @psoc: Pointer to psoc object
92  *
93  * Return: Reg rx_ops
94  */
95 struct wlan_lmac_if_reg_rx_ops *
96 target_if_regulatory_get_rx_ops(struct wlan_objmgr_psoc *psoc)
97 {
98 	return &psoc->soc_cb.rx_ops.reg_rx_ops;
99 }
100 
101 QDF_STATUS target_if_reg_set_offloaded_info(struct wlan_objmgr_psoc *psoc)
102 {
103 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
104 
105 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
106 	if (!reg_rx_ops) {
107 		target_if_err("reg_rx_ops is NULL");
108 		return QDF_STATUS_E_FAILURE;
109 	}
110 
111 	if (reg_rx_ops->reg_set_regdb_offloaded)
112 		reg_rx_ops->reg_set_regdb_offloaded(
113 				psoc,
114 				tgt_if_regulatory_is_regdb_offloaded(psoc));
115 
116 	if (reg_rx_ops->reg_set_11d_offloaded)
117 		reg_rx_ops->reg_set_11d_offloaded(
118 				psoc, tgt_if_regulatory_is_11d_offloaded(psoc));
119 
120 	return QDF_STATUS_SUCCESS;
121 }
122 
123 /**
124  * tgt_reg_chan_list_update_handler() - Channel list update handler
125  * @handle: scn handle
126  * @event_buf: pointer to event buffer
127  * @len: buffer length
128  *
129  * Return: 0 on success
130  */
131 static int tgt_reg_chan_list_update_handler(ol_scn_t handle, uint8_t *event_buf,
132 					    uint32_t len)
133 {
134 	struct wlan_objmgr_psoc *psoc;
135 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
136 	struct cur_regulatory_info *reg_info;
137 	QDF_STATUS status;
138 	struct wmi_unified *wmi_handle;
139 	int ret_val = 0;
140 
141 	TARGET_IF_ENTER();
142 
143 	psoc = target_if_get_psoc_from_scn_hdl(handle);
144 	if (!psoc) {
145 		target_if_err("psoc ptr is NULL");
146 		return -EINVAL;
147 	}
148 
149 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
150 	if (!reg_rx_ops->master_list_handler) {
151 		target_if_err("master_list_handler is NULL");
152 		return -EINVAL;
153 	}
154 
155 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
156 	if (!wmi_handle) {
157 		target_if_err("invalid wmi handle");
158 		return -EINVAL;
159 	}
160 
161 	reg_info = qdf_mem_malloc(sizeof(*reg_info));
162 	if (!reg_info)
163 		return -ENOMEM;
164 
165 	if (wmi_extract_reg_chan_list_update_event(wmi_handle,
166 						   event_buf, reg_info, len)
167 	    != QDF_STATUS_SUCCESS) {
168 		target_if_err("Extraction of channel list event failed");
169 		ret_val = -EFAULT;
170 		goto clean;
171 	}
172 
173 	if (reg_info->phy_id >= PSOC_MAX_PHY_REG_CAP) {
174 		target_if_err_rl("phy_id %d is out of bounds",
175 				 reg_info->phy_id);
176 		ret_val = -EFAULT;
177 		goto clean;
178 	}
179 
180 	reg_info->psoc = psoc;
181 
182 	status = reg_rx_ops->master_list_handler(reg_info);
183 	if (status != QDF_STATUS_SUCCESS) {
184 		target_if_err("Failed to process master channel list handler");
185 		ret_val = -EFAULT;
186 	}
187 
188 clean:
189 	qdf_mem_free(reg_info->reg_rules_2g_ptr);
190 	qdf_mem_free(reg_info->reg_rules_5g_ptr);
191 	qdf_mem_free(reg_info);
192 
193 	target_if_debug("processed reg channel list ret_val %d", ret_val);
194 
195 	return ret_val;
196 }
197 
198 /**
199  * tgt_if_regulatory_register_master_list_handler() - Register master channel
200  * list
201  * @psoc: Pointer to psoc
202  * @arg: Pointer to argument list
203  *
204  * Return: QDF_STATUS
205  */
206 static QDF_STATUS tgt_if_regulatory_register_master_list_handler(
207 	struct wlan_objmgr_psoc *psoc, void *arg)
208 {
209 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
210 
211 	if (!wmi_handle)
212 		return QDF_STATUS_E_FAILURE;
213 
214 	return wmi_unified_register_event_handler(
215 			wmi_handle, wmi_reg_chan_list_cc_event_id,
216 			tgt_reg_chan_list_update_handler, WMI_RX_UMAC_CTX);
217 }
218 
219 /**
220  * tgt_if_regulatory_unregister_master_list_handler() - Unregister master
221  * channel list
222  * @psoc: Pointer to psoc
223  * @arg: Pointer to argument list
224  *
225  * Return: QDF_STATUS
226  */
227 static QDF_STATUS tgt_if_regulatory_unregister_master_list_handler(
228 	struct wlan_objmgr_psoc *psoc, void *arg)
229 {
230 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
231 
232 	if (!wmi_handle)
233 		return QDF_STATUS_E_FAILURE;
234 
235 	return wmi_unified_unregister_event_handler(
236 			wmi_handle, wmi_reg_chan_list_cc_event_id);
237 }
238 
239 /**
240  * tgt_if_regulatory_set_country_code() - Set country code
241  * @psoc: Pointer to psoc
242  * @arg: Pointer to argument list
243  *
244  * Return: QDF_STATUS
245  */
246 static QDF_STATUS tgt_if_regulatory_set_country_code(
247 	struct wlan_objmgr_psoc *psoc, void *arg)
248 {
249 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
250 
251 	if (!wmi_handle)
252 		return QDF_STATUS_E_FAILURE;
253 
254 	return wmi_unified_set_country_cmd_send(wmi_handle, arg);
255 }
256 
257 /**
258  * tgt_if_regulatory_set_user_country_code() - Set user country code
259  * @psoc: Pointer to psoc
260  * @pdev_id: Pdev id
261  * @rd: Pointer to regdomain structure
262  *
263  * Return: QDF_STATUS
264  */
265 static QDF_STATUS tgt_if_regulatory_set_user_country_code(
266 	struct wlan_objmgr_psoc *psoc, uint8_t pdev_id, struct cc_regdmn_s *rd)
267 {
268 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
269 
270 	if (!wmi_handle)
271 		return QDF_STATUS_E_FAILURE;
272 
273 	if (wmi_unified_set_user_country_code_cmd_send(
274 				wmi_handle, pdev_id, rd) != QDF_STATUS_SUCCESS
275 			) {
276 		target_if_err("Set user country code failed");
277 		return QDF_STATUS_E_FAILURE;
278 	}
279 
280 	return QDF_STATUS_SUCCESS;
281 }
282 
283 QDF_STATUS tgt_if_regulatory_modify_freq_range(struct wlan_objmgr_psoc *psoc)
284 {
285 	struct wlan_psoc_host_hal_reg_capabilities_ext *reg_cap;
286 
287 	reg_cap = ucfg_reg_get_hal_reg_cap(psoc);
288 	if (!reg_cap) {
289 		target_if_err("reg cap is NULL");
290 		return QDF_STATUS_E_FAILURE;
291 	}
292 
293 	if (!(reg_cap->wireless_modes & WMI_HOST_REGDMN_MODE_11A)) {
294 		reg_cap->low_5ghz_chan = 0;
295 		reg_cap->high_5ghz_chan = 0;
296 	}
297 
298 	if (!(reg_cap->wireless_modes &
299 	     (WMI_HOST_REGDMN_MODE_11B | WMI_HOST_REGDMN_MODE_PUREG))) {
300 		reg_cap->low_2ghz_chan = 0;
301 		reg_cap->high_2ghz_chan = 0;
302 	}
303 
304 	target_if_debug("phy_id = %d - low_2ghz_chan = %d high_2ghz_chan = %d low_5ghz_chan = %d high_5ghz_chan = %d",
305 			reg_cap->phy_id,
306 			reg_cap->low_2ghz_chan,
307 			reg_cap->high_2ghz_chan,
308 			reg_cap->low_5ghz_chan,
309 			reg_cap->high_5ghz_chan);
310 
311 	return QDF_STATUS_SUCCESS;
312 }
313 
314 QDF_STATUS target_if_register_regulatory_tx_ops(
315 		struct wlan_lmac_if_tx_ops *tx_ops)
316 {
317 	struct wlan_lmac_if_reg_tx_ops *reg_ops = &tx_ops->reg_ops;
318 
319 	reg_ops->register_master_handler =
320 		tgt_if_regulatory_register_master_list_handler;
321 
322 	reg_ops->unregister_master_handler =
323 		tgt_if_regulatory_unregister_master_list_handler;
324 
325 	reg_ops->set_country_code = tgt_if_regulatory_set_country_code;
326 
327 	reg_ops->fill_umac_legacy_chanlist = NULL;
328 
329 	reg_ops->set_country_failed = NULL;
330 
331 	reg_ops->register_11d_new_cc_handler =
332 		tgt_if_regulatory_register_11d_new_cc_handler;
333 
334 	reg_ops->unregister_11d_new_cc_handler =
335 		tgt_if_regulatory_unregister_11d_new_cc_handler;
336 
337 	reg_ops->start_11d_scan = tgt_if_regulatory_start_11d_scan;
338 
339 	reg_ops->stop_11d_scan = tgt_if_regulatory_stop_11d_scan;
340 
341 	reg_ops->is_there_serv_ready_extn =
342 		tgt_if_regulatory_is_there_serv_ready_extn;
343 
344 	reg_ops->set_user_country_code =
345 		tgt_if_regulatory_set_user_country_code;
346 
347 	reg_ops->register_ch_avoid_event_handler =
348 		tgt_if_regulatory_register_ch_avoid_event_handler;
349 
350 	reg_ops->unregister_ch_avoid_event_handler =
351 		tgt_if_regulatory_unregister_ch_avoid_event_handler;
352 
353 	return QDF_STATUS_SUCCESS;
354 }
355