xref: /wlan-dirver/qca-wifi-host-cmn/target_if/init_deinit/src/service_ready_util.c (revision 4865edfd190c086bbe2c69aae12a8226f877b91e)
1 /*
2  * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 /**
19  * DOC: service_ready_util.c
20  *
21  * Public APIs implementation source file for accessing (ext)service ready
22  * data from psoc object
23  */
24 #include "service_ready_util.h"
25 #include <wlan_reg_ucfg_api.h>
26 #include <target_type.h>
27 #include <qdf_module.h>
28 
29 QDF_STATUS init_deinit_chainmask_table_alloc(
30 		struct wlan_psoc_host_service_ext_param *ser_ext_par)
31 {
32 	int i;
33 	uint32_t alloc_size;
34 	QDF_STATUS status;
35 
36 	if (ser_ext_par->num_chainmask_tables > 0) {
37 		status = QDF_STATUS_SUCCESS;
38 		for (i = 0; i < ser_ext_par->num_chainmask_tables; i++) {
39 			alloc_size =
40 			(sizeof(struct wlan_psoc_host_chainmask_capabilities) *
41 			ser_ext_par->chainmask_table[i].num_valid_chainmasks);
42 
43 			ser_ext_par->chainmask_table[i].cap_list =
44 				qdf_mem_alloc_outline(NULL, alloc_size);
45 			if (!ser_ext_par->chainmask_table[i].cap_list) {
46 				init_deinit_chainmask_table_free(ser_ext_par);
47 				status = QDF_STATUS_E_NOMEM;
48 				break;
49 			}
50 		}
51 	} else {
52 		status = QDF_STATUS_E_NOSUPPORT;
53 	}
54 
55 	return status;
56 }
57 
58 qdf_export_symbol(init_deinit_chainmask_table_alloc);
59 
60 QDF_STATUS init_deinit_chainmask_table_free(
61 		struct wlan_psoc_host_service_ext_param *ser_ext_par)
62 {
63 	struct wlan_psoc_host_chainmask_table *table;
64 	int i;
65 
66 	for (i = 0; i < ser_ext_par->num_chainmask_tables; i++) {
67 		table =  &(ser_ext_par->chainmask_table[i]);
68 		if (table->cap_list) {
69 			qdf_mem_free(table->cap_list);
70 			table->cap_list = NULL;
71 		}
72 	}
73 
74 	return QDF_STATUS_SUCCESS;
75 }
76 
77 qdf_export_symbol(init_deinit_chainmask_table_free);
78 
79 int init_deinit_populate_service_bitmap(void *wmi_handle, uint8_t *event,
80 				      uint32_t *service_bitmap)
81 {
82 	QDF_STATUS status;
83 
84 	status = wmi_save_service_bitmap(wmi_handle, event, service_bitmap);
85 	if (QDF_IS_STATUS_ERROR(status)) {
86 		target_if_err("failed to parse service bitmap");
87 		return qdf_status_to_os_return(status);
88 	}
89 
90 	return 0;
91 }
92 
93 int init_deinit_populate_fw_version_cmd(void *wmi_handle, uint8_t *event)
94 {
95 	QDF_STATUS status;
96 
97 	status = wmi_unified_save_fw_version_cmd(wmi_handle, event);
98 	if (QDF_IS_STATUS_ERROR(status))
99 		target_if_err("failed to save fw version");
100 
101 	return 0;
102 }
103 
104 int init_deinit_populate_target_cap(void *wmi_handle, uint8_t *event,
105 			       struct wlan_psoc_target_capability_info *cap)
106 {
107 	QDF_STATUS status;
108 
109 	status = wmi_get_target_cap_from_service_ready(wmi_handle, event, cap);
110 	if (QDF_IS_STATUS_ERROR(status)) {
111 		target_if_err("failed to parse target cap");
112 		return qdf_status_to_os_return(status);
113 	}
114 
115 	return 0;
116 }
117 
118 int init_deinit_populate_service_ready_ext_param(void *handle, uint8_t *evt,
119 			struct wlan_psoc_host_service_ext_param *param)
120 {
121 	QDF_STATUS status;
122 
123 	status = wmi_extract_service_ready_ext(handle, evt, param);
124 	if (QDF_IS_STATUS_ERROR(status)) {
125 		target_if_err("failed to parse wmi service ready ext param");
126 		return qdf_status_to_os_return(status);
127 	}
128 
129 	return 0;
130 }
131 
132 int init_deinit_populate_chainmask_tables(void *handle, uint8_t *evt,
133 		struct wlan_psoc_host_chainmask_table *param)
134 {
135 	QDF_STATUS status;
136 
137 	status = wmi_extract_chainmask_tables(handle, evt, param);
138 	if (QDF_IS_STATUS_ERROR(status)) {
139 		target_if_err("failed to parse wmi service ready ext param");
140 		return qdf_status_to_os_return(status);
141 	}
142 
143 	return 0;
144 }
145 
146 int init_deinit_populate_mac_phy_capability(void *handle, uint8_t *evt,
147 	struct wlan_psoc_host_hw_mode_caps *hw_cap, struct tgt_info *info)
148 {
149 	QDF_STATUS status;
150 	uint32_t hw_mode_id;
151 	uint32_t phy_bit_map;
152 	uint8_t mac_phy_id;
153 
154 	hw_mode_id = hw_cap->hw_mode_id;
155 	phy_bit_map = hw_cap->phy_id_map;
156 	target_if_info("hw_mode_id %d phy_bit_map 0x%x",
157 		       hw_mode_id, phy_bit_map);
158 
159 	mac_phy_id = 0;
160 	while (phy_bit_map) {
161 		if (info->total_mac_phy_cnt >= PSOC_MAX_MAC_PHY_CAP) {
162 			target_if_err("total mac phy exceeds max limit %d",
163 				      info->total_mac_phy_cnt);
164 			return -EINVAL;
165 		}
166 
167 		status = wmi_extract_mac_phy_cap_service_ready_ext(handle,
168 				evt, hw_mode_id, mac_phy_id,
169 				&(info->mac_phy_cap[info->total_mac_phy_cnt]));
170 		if (QDF_IS_STATUS_ERROR(status)) {
171 			target_if_err("failed to parse mac phy capability");
172 			return qdf_status_to_os_return(status);
173 		}
174 		info->mac_phy_cap[info->total_mac_phy_cnt].hw_mode_config_type
175 					= hw_cap->hw_mode_config_type;
176 		info->total_mac_phy_cnt++;
177 		phy_bit_map &= (phy_bit_map - 1);
178 		mac_phy_id++;
179 	}
180 	target_if_info("total_mac_phy_cnt %d", info->total_mac_phy_cnt);
181 
182 	return 0;
183 }
184 
185 static int get_hw_mode(void *handle, uint8_t *evt, uint8_t hw_idx,
186 			struct wlan_psoc_host_hw_mode_caps *cap)
187 {
188 	QDF_STATUS status;
189 
190 	status = wmi_extract_hw_mode_cap_service_ready_ext(handle, evt,
191 					hw_idx, cap);
192 	if (QDF_IS_STATUS_ERROR(status)) {
193 		target_if_err("failed to parse hw mode capability");
194 		return qdf_status_to_os_return(status);
195 	}
196 
197 	return 0;
198 }
199 
200 int init_deinit_populate_hw_mode_capability(void *wmi_handle,
201 		uint8_t *event, struct target_psoc_info *tgt_hdl)
202 {
203 	QDF_STATUS status = QDF_STATUS_SUCCESS;
204 	uint8_t hw_idx;
205 	uint32_t num_hw_modes;
206 	struct wlan_psoc_host_hw_mode_caps hw_mode_caps[PSOC_MAX_HW_MODE];
207 	uint32_t preferred_mode;
208 	struct tgt_info *info;
209 
210 	info = &tgt_hdl->info;
211 	num_hw_modes = info->service_ext_param.num_hw_modes;
212 	if (num_hw_modes > PSOC_MAX_HW_MODE) {
213 		target_if_err("invalid num_hw_modes %d", num_hw_modes);
214 		return -EINVAL;
215 	}
216 	target_if_info("num_hw_modes %d", num_hw_modes);
217 
218 	qdf_mem_zero(&hw_mode_caps, sizeof(hw_mode_caps));
219 
220 	preferred_mode = target_psoc_get_preferred_hw_mode(tgt_hdl);
221 	for (hw_idx = 0; hw_idx < num_hw_modes; hw_idx++) {
222 		status = get_hw_mode(wmi_handle, event, hw_idx,
223 						&hw_mode_caps[hw_idx]);
224 		if (status)
225 			goto return_exit;
226 
227 		if ((preferred_mode != WMI_HOST_HW_MODE_MAX) &&
228 		    (hw_mode_caps[hw_idx].hw_mode_id != preferred_mode))
229 			continue;
230 
231 		status = init_deinit_populate_mac_phy_capability(wmi_handle,
232 				event, &hw_mode_caps[hw_idx], info);
233 		if (status)
234 			goto return_exit;
235 
236 		if ((preferred_mode != WMI_HOST_HW_MODE_MAX) &&
237 		    (hw_mode_caps[hw_idx].hw_mode_id == preferred_mode)) {
238 			info->num_radios = info->total_mac_phy_cnt;
239 			target_if_info("num radios is %d\n", info->num_radios);
240 		}
241 	}
242 
243 return_exit:
244 	return qdf_status_to_os_return(status);
245 }
246 
247 int init_deinit_populate_dbr_ring_cap(struct wlan_objmgr_psoc *psoc,
248 			void *handle, uint8_t *event, struct tgt_info *info)
249 
250 {
251 	uint8_t cap_idx;
252 	uint32_t num_dbr_ring_caps;
253 	QDF_STATUS status = QDF_STATUS_SUCCESS;
254 
255 	num_dbr_ring_caps = info->service_ext_param.num_dbr_ring_caps;
256 
257 	target_if_info("Num DMA Capabilities = %d", num_dbr_ring_caps);
258 
259 	if (!num_dbr_ring_caps)
260 		return 0;
261 
262 	info->dbr_ring_cap = qdf_mem_malloc(
263 				sizeof(struct wlan_psoc_host_dbr_ring_caps) *
264 				num_dbr_ring_caps);
265 
266 	if (!info->dbr_ring_cap) {
267 		target_if_err("Mem alloc for DMA cap failed");
268 		return -EINVAL;
269 	}
270 
271 	for (cap_idx = 0; cap_idx < num_dbr_ring_caps; cap_idx++) {
272 		status = wmi_extract_dbr_ring_cap_service_ready_ext(handle,
273 				event, cap_idx,
274 				&(info->dbr_ring_cap[cap_idx]));
275 		if (QDF_IS_STATUS_ERROR(status)) {
276 			target_if_err("Extraction of DMA cap failed");
277 			goto free_and_return;
278 		}
279 	}
280 
281 	return 0;
282 
283 free_and_return:
284 	qdf_mem_free(info->dbr_ring_cap);
285 	info->dbr_ring_cap = NULL;
286 
287 	return qdf_status_to_os_return(status);
288 }
289 
290 QDF_STATUS init_deinit_dbr_ring_cap_free(
291 		struct target_psoc_info *tgt_psoc_info)
292 {
293 	QDF_STATUS status = QDF_STATUS_SUCCESS;
294 
295 	if (tgt_psoc_info->info.dbr_ring_cap) {
296 		qdf_mem_free(tgt_psoc_info->info.dbr_ring_cap);
297 		tgt_psoc_info->info.dbr_ring_cap = NULL;
298 	}
299 
300 	return status;
301 }
302 qdf_export_symbol(init_deinit_dbr_ring_cap_free);
303 
304 int init_deinit_populate_phy_reg_cap(struct wlan_objmgr_psoc *psoc,
305 				void *handle, uint8_t *event,
306 				struct tgt_info *info, bool service_ready)
307 {
308 	uint8_t reg_idx;
309 	uint32_t num_phy_reg_cap;
310 	QDF_STATUS status = QDF_STATUS_SUCCESS;
311 	struct wlan_psoc_hal_reg_capability cap;
312 	struct wlan_psoc_host_hal_reg_capabilities_ext
313 				reg_cap[PSOC_MAX_PHY_REG_CAP];
314 
315 	if (service_ready) {
316 		status = wmi_extract_hal_reg_cap(handle, event, &cap);
317 		if (QDF_IS_STATUS_ERROR(status)) {
318 			target_if_err("failed to parse hal reg cap");
319 			return qdf_status_to_os_return(status);
320 		}
321 		info->service_ext_param.num_phy = 1;
322 		num_phy_reg_cap = 1;
323 		reg_cap[0].phy_id = 0;
324 		qdf_mem_copy(&(reg_cap[0].eeprom_reg_domain), &cap,
325 			     sizeof(struct wlan_psoc_hal_reg_capability));
326 		target_if_info("FW wireless modes 0x%x",
327 			       reg_cap[0].wireless_modes);
328 	} else {
329 		num_phy_reg_cap = info->service_ext_param.num_phy;
330 		if (num_phy_reg_cap > PSOC_MAX_PHY_REG_CAP) {
331 			target_if_err("Invalid num_phy_reg_cap %d",
332 				      num_phy_reg_cap);
333 			return -EINVAL;
334 		}
335 		target_if_info("num_phy_reg_cap %d", num_phy_reg_cap);
336 
337 		for (reg_idx = 0; reg_idx < num_phy_reg_cap; reg_idx++) {
338 			status = wmi_extract_reg_cap_service_ready_ext(handle,
339 					event, reg_idx, &(reg_cap[reg_idx]));
340 			if (QDF_IS_STATUS_ERROR(status)) {
341 				target_if_err("failed to parse reg cap");
342 				return qdf_status_to_os_return(status);
343 			}
344 		}
345 	}
346 
347 	status = ucfg_reg_set_hal_reg_cap(psoc, reg_cap, num_phy_reg_cap);
348 
349 	return qdf_status_to_os_return(status);
350 }
351 
352 static bool init_deinit_regdmn_160mhz_support(
353 		struct wlan_psoc_host_hal_reg_capabilities_ext *hal_cap)
354 {
355 	return ((hal_cap->wireless_modes &
356 		WMI_HOST_REGDMN_MODE_11AC_VHT160) != 0);
357 }
358 
359 static bool init_deinit_regdmn_80p80mhz_support(
360 		struct wlan_psoc_host_hal_reg_capabilities_ext *hal_cap)
361 {
362 	return ((hal_cap->wireless_modes &
363 			WMI_HOST_REGDMN_MODE_11AC_VHT80_80) != 0);
364 }
365 
366 static bool init_deinit_vht_160mhz_is_supported(uint32_t vhtcap)
367 {
368 	return ((vhtcap & WLAN_VHTCAP_SUP_CHAN_WIDTH_160) != 0);
369 }
370 
371 static bool init_deinit_vht_80p80mhz_is_supported(uint32_t vhtcap)
372 {
373 	return ((vhtcap & WLAN_VHTCAP_SUP_CHAN_WIDTH_80_160) != 0);
374 }
375 
376 static bool init_deinit_vht_160mhz_shortgi_is_supported(uint32_t vhtcap)
377 {
378 	return ((vhtcap & WLAN_VHTCAP_SHORTGI_160) != 0);
379 }
380 
381 QDF_STATUS init_deinit_validate_160_80p80_fw_caps(
382 		 struct wlan_objmgr_psoc *psoc,
383 		 struct target_psoc_info *tgt_hdl)
384 {
385 	bool wireless_mode_160mhz = false;
386 	bool wireless_mode_80p80mhz = false;
387 	bool vhtcap_160mhz = false;
388 	bool vhtcap_80p80_160mhz = false;
389 	bool vhtcap_160mhz_sgi = false;
390 	bool valid = false;
391 	struct wlan_psoc_host_hal_reg_capabilities_ext *reg_cap;
392 	struct common_wmi_handle *wmi_handle;
393 
394 	if (!tgt_hdl) {
395 		target_if_err(
396 		"target_psoc_info is null in validate 160n80p80 cap check");
397 		return QDF_STATUS_E_INVAL;
398 	}
399 
400 	wmi_handle = target_psoc_get_wmi_hdl(tgt_hdl);
401 
402 	if ((tgt_hdl->info.target_type == TARGET_TYPE_QCA8074) ||
403 	    (tgt_hdl->info.target_type == TARGET_TYPE_QCA6290)) {
404 		/**
405 		 * Return true for now. This is not available in
406 		 * qca8074 fw yet
407 		 */
408 		return QDF_STATUS_SUCCESS;
409 	}
410 
411 	reg_cap = ucfg_reg_get_hal_reg_cap(psoc);
412 	if (reg_cap == NULL) {
413 		target_if_err("reg cap is NULL");
414 		return QDF_STATUS_E_FAILURE;
415 	}
416 
417 	/* NOTE: Host driver gets vht capability and supported channel
418 	 * width / channel frequency range from FW/HALPHY and obeys it.
419 	 * Host driver is unaware of any physical filters or any other
420 	 * hardware factors that can impact these capabilities.
421 	 * These need to be correctly determined by firmware.
422 	 */
423 
424 	/*This table lists all valid and invalid combinations
425 	 * WMODE160 WMODE80_80  VHTCAP_160 VHTCAP_80+80_160  IsCombinationvalid?
426 	 *      0         0           0              0                 YES
427 	 *      0         0           0              1                 NO
428 	 *      0         0           1              0                 NO
429 	 *      0         0           1              1                 NO
430 	 *      0         1           0              0                 NO
431 	 *      0         1           0              1                 NO
432 	 *      0         1           1              0                 NO
433 	 *      0         1           1              1                 NO
434 	 *      1         0           0              0                 NO
435 	 *      1         0           0              1                 NO
436 	 *      1         0           1              0                 YES
437 	 *      1         0           1              1                 NO
438 	 *      1         1           0              0                 NO
439 	 *      1         1           0              1                 YES
440 	 *      1         1           1              0                 NO
441 	 *      1         1           1              1                 NO
442 	 */
443 
444 	/* NOTE: Last row in above table is invalid because value corresponding
445 	 * to both VHTCAP_160 and VHTCAP_80+80_160 being set is reserved as per
446 	 * 802.11ac. Only one of them can be set at a time.
447 	 */
448 
449 	wireless_mode_160mhz = init_deinit_regdmn_160mhz_support(reg_cap);
450 	wireless_mode_80p80mhz = init_deinit_regdmn_80p80mhz_support(reg_cap);
451 	vhtcap_160mhz = init_deinit_vht_160mhz_is_supported(
452 				tgt_hdl->info.target_caps.vht_cap_info);
453 	vhtcap_80p80_160mhz = init_deinit_vht_80p80mhz_is_supported(
454 				tgt_hdl->info.target_caps.vht_cap_info);
455 	vhtcap_160mhz_sgi = init_deinit_vht_160mhz_shortgi_is_supported(
456 				tgt_hdl->info.target_caps.vht_cap_info);
457 
458 	if (!(wireless_mode_160mhz || wireless_mode_80p80mhz ||
459 	      vhtcap_160mhz || vhtcap_80p80_160mhz)) {
460 		valid = QDF_STATUS_SUCCESS;
461 	} else if (wireless_mode_160mhz && !wireless_mode_80p80mhz &&
462 		   vhtcap_160mhz && !vhtcap_80p80_160mhz) {
463 		valid = QDF_STATUS_SUCCESS;
464 	} else if (wireless_mode_160mhz && wireless_mode_80p80mhz &&
465 		   !vhtcap_160mhz && vhtcap_160mhz_sgi) {
466 		valid = QDF_STATUS_SUCCESS;
467 	}
468 
469 	if (valid == QDF_STATUS_SUCCESS) {
470 		/*
471 		 * Ensure short GI for 160 MHz is enabled
472 		 * only if 160/80+80 is supported.
473 		 */
474 		if (vhtcap_160mhz_sgi &&
475 		    !(vhtcap_160mhz || vhtcap_80p80_160mhz)) {
476 			valid = QDF_STATUS_E_FAILURE;
477 		}
478 	}
479 
480 	/* Invalid config specified by FW */
481 	if (valid != QDF_STATUS_SUCCESS) {
482 		target_if_err("Invalid 160/80+80 MHz config specified by FW. Take care of it first");
483 		target_if_err("wireless_mode_160mhz: %d, wireless_mode_80p80mhz: %d",
484 			      wireless_mode_160mhz, wireless_mode_80p80mhz);
485 		target_if_err("vhtcap_160mhz: %d, vhtcap_80p80_160mhz: %d,vhtcap_160mhz_sgi: %d",
486 			      vhtcap_160mhz, vhtcap_80p80_160mhz,
487 			      vhtcap_160mhz_sgi);
488 	}
489 	return valid;
490 }
491 
492 void init_deinit_chainmask_config(
493 		 struct wlan_objmgr_psoc *psoc,
494 		 struct target_psoc_info *tgt_hdl)
495 {
496 	tgt_hdl->info.wlan_res_cfg.tx_chain_mask =
497 		((1 << tgt_hdl->info.target_caps.num_rf_chains) - 1);
498 	tgt_hdl->info.wlan_res_cfg.rx_chain_mask =
499 		((1 << tgt_hdl->info.target_caps.num_rf_chains) - 1);
500 }
501 
502 QDF_STATUS init_deinit_is_service_ext_msg(
503 		 struct wlan_objmgr_psoc *psoc,
504 		 struct target_psoc_info *tgt_hdl)
505 {
506 	struct common_wmi_handle *wmi_handle;
507 
508 	if (!tgt_hdl) {
509 		target_if_err(
510 			"psoc target_psoc_info is null in service ext msg");
511 		return QDF_STATUS_E_INVAL;
512 	}
513 
514 	wmi_handle = target_psoc_get_wmi_hdl(tgt_hdl);
515 
516 	if (wmi_service_enabled(wmi_handle, wmi_service_ext_msg))
517 		return QDF_STATUS_SUCCESS;
518 	else
519 		return QDF_STATUS_E_FAILURE;
520 }
521 
522 bool init_deinit_is_preferred_hw_mode_supported(
523 		 struct wlan_objmgr_psoc *psoc,
524 		 struct target_psoc_info *tgt_hdl)
525 {
526 	uint16_t i;
527 	struct tgt_info *info;
528 
529 	if (!tgt_hdl) {
530 		target_if_err(
531 			"psoc target_psoc_info is null in service ext msg");
532 		return FALSE;
533 	}
534 
535 	info = &tgt_hdl->info;
536 
537 	if (info->preferred_hw_mode == WMI_HOST_HW_MODE_MAX)
538 		return TRUE;
539 
540 	for (i = 0; i < target_psoc_get_total_mac_phy_cnt(tgt_hdl); i++) {
541 		if (info->mac_phy_cap[i].hw_mode_id == info->preferred_hw_mode)
542 			return TRUE;
543 	}
544 
545 	return FALSE;
546 }
547 
548 void init_deinit_wakeup_host_wait(
549 		 struct wlan_objmgr_psoc *psoc,
550 		 struct target_psoc_info *tgt_hdl)
551 {
552 	if (!tgt_hdl) {
553 		target_if_err("psoc target_psoc_info is null in target ready");
554 		return;
555 	}
556 	qdf_wake_up(&tgt_hdl->info.event_queue);
557 }
558