1 /*
2  * Copyright (c) 2020, 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 any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /**
19  * DOC: defines driver functions interfacing with linux kernel
20  */
21 #include <wmi_unified_param.h>
22 #include <wlan_osif_request_manager.h>
23 #include <osif_sync.h>
24 #include <wlan_objmgr_psoc_obj_i.h>
25 #include <wlan_coex_main.h>
26 #include <wlan_coex_ucfg_api.h>
27 #include <wlan_cfg80211_coex.h>
28 
29 const struct nla_policy
30 btc_chain_mode_policy[QCA_VENDOR_ATTR_BTC_CHAIN_MODE_MAX + 1] = {
31 	[QCA_VENDOR_ATTR_BTC_CHAIN_MODE] = {.type = NLA_U32},
32 	[QCA_VENDOR_ATTR_BTC_CHAIN_MODE_RESTART] = {.type = NLA_FLAG},
33 };
34 
35 static enum coex_btc_chain_mode
__wlan_cfg80211_coex_map_btc_chain_mode(enum qca_btc_chain_mode mode)36 __wlan_cfg80211_coex_map_btc_chain_mode(enum qca_btc_chain_mode mode)
37 {
38 	switch (mode) {
39 	case QCA_BTC_CHAIN_SHARED:
40 		return WLAN_COEX_BTC_CHAIN_MODE_SHARED;
41 	case QCA_BTC_CHAIN_SEPARATED_HYBRID:
42 		return WLAN_COEX_BTC_CHAIN_MODE_HYBRID;
43 	case QCA_BTC_CHAIN_SEPARATED_FDD:
44 		return WLAN_COEX_BTC_CHAIN_MODE_FDD;
45 	default:
46 		return WLAN_COEX_BTC_CHAIN_MODE_UNSETTLED;
47 	}
48 }
49 
50 static int
__wlan_cfg80211_coex_set_btc_chain_mode(struct wlan_objmgr_vdev * vdev,enum coex_btc_chain_mode mode,bool do_restart)51 __wlan_cfg80211_coex_set_btc_chain_mode(struct wlan_objmgr_vdev *vdev,
52 					enum coex_btc_chain_mode mode,
53 					bool do_restart)
54 {
55 	QDF_STATUS status;
56 	enum coex_btc_chain_mode cur_mode;
57 	int err;
58 	struct wlan_objmgr_psoc *psoc;
59 	struct wlan_objmgr_vdev *vdev_tmp;
60 	int vdev_id;
61 	struct coex_psoc_obj *coex_obj;
62 
63 	if (!vdev) {
64 		coex_err("Null vdev");
65 		return -EINVAL;
66 	}
67 
68 	psoc = wlan_vdev_get_psoc(vdev);
69 	if (!psoc) {
70 		coex_err("NULL psoc");
71 		return -EINVAL;
72 	}
73 
74 	coex_obj = wlan_psoc_get_coex_obj(psoc);
75 	if (!coex_obj)
76 		return -EINVAL;
77 
78 	status = ucfg_coex_psoc_get_btc_chain_mode(psoc, &cur_mode);
79 	if (QDF_IS_STATUS_ERROR(status)) {
80 		coex_err("failed to get cur BTC chain mode, status %d", status);
81 		return -EFAULT;
82 	}
83 
84 	if (cur_mode == mode)
85 		return -EALREADY;
86 
87 	status = ucfg_coex_psoc_set_btc_chain_mode(psoc, mode);
88 	if (!QDF_IS_STATUS_SUCCESS(status)) {
89 		coex_err("unable to set BTC chain mode to %d", mode);
90 		return -EFAULT;
91 	}
92 
93 	wlan_objmgr_for_each_psoc_vdev(psoc, vdev_id, vdev_tmp) {
94 		status = ucfg_coex_send_btc_chain_mode(vdev_tmp, mode);
95 		err = qdf_status_to_os_return(status);
96 		if (err) {
97 			coex_err("Failed to set btc chain mode to %d for vdev %d",
98 				 mode, vdev_id);
99 			return err;
100 		}
101 		coex_debug("Set btc chain mode to %d for vdev %d",
102 			   mode, vdev_id);
103 
104 		if (!do_restart)
105 			continue;
106 
107 		wlan_coex_config_updated(vdev_tmp, COEX_CONFIG_BTC_CHAIN_MODE);
108 	}
109 
110 	return 0;
111 }
112 
113 /**
114  * wlan_cfg80211_coex_set_btc_chain_mode() - set btc chain mode
115  * @vdev: pointer to vdev structure.
116  * @data: pointer to btc chain mode command parameters.
117  * @data_len: the length in byte of btc chain mode command parameters.
118  *
119  * Return: An error code or 0 on success.
120  */
wlan_cfg80211_coex_set_btc_chain_mode(struct wlan_objmgr_vdev * vdev,const void * data,int data_len)121 int wlan_cfg80211_coex_set_btc_chain_mode(struct wlan_objmgr_vdev *vdev,
122 					  const void *data, int data_len)
123 {
124 	struct nlattr *tb[QCA_VENDOR_ATTR_BTC_CHAIN_MODE_MAX + 1];
125 	uint32_t mode;
126 	enum coex_btc_chain_mode chain_mode;
127 	bool restart;
128 
129 	if (wlan_cfg80211_nla_parse(tb, QCA_VENDOR_ATTR_BTC_CHAIN_MODE_MAX,
130 				    data, data_len, btc_chain_mode_policy)) {
131 		coex_err("Invalid btc chain mode ATTR");
132 		return -EINVAL;
133 	}
134 
135 	if (!tb[QCA_VENDOR_ATTR_BTC_CHAIN_MODE]) {
136 		coex_err("btc chain mode - no attr mode");
137 		return -EINVAL;
138 	}
139 
140 	mode = nla_get_u32(tb[QCA_VENDOR_ATTR_BTC_CHAIN_MODE]);
141 	if (mode > QCA_BTC_CHAIN_SEPARATED_FDD) {
142 		coex_err("Invalid btc chain mode %d", mode);
143 		return -EINVAL;
144 	}
145 
146 	restart = nla_get_flag(tb[QCA_VENDOR_ATTR_BTC_CHAIN_MODE_RESTART]);
147 
148 	/* map to internal mode definitions */
149 	chain_mode = __wlan_cfg80211_coex_map_btc_chain_mode(mode);
150 	if (chain_mode == WLAN_COEX_BTC_CHAIN_MODE_UNSETTLED) {
151 		coex_err("Invalid wlan btc chain mode %d", chain_mode);
152 		return -EINVAL;
153 	}
154 
155 	coex_debug("vdev_id %u mode %u restart %u",
156 		   wlan_vdev_get_id(vdev), chain_mode, restart);
157 
158 	return __wlan_cfg80211_coex_set_btc_chain_mode(vdev,
159 						       chain_mode,
160 						       restart);
161 }
162