1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2022 - 2023 Intel Corporation
4  */
5 #include "mvm.h"
6 #include "time-event.h"
7 
8 static u32 iwl_mvm_get_free_fw_link_id(struct iwl_mvm *mvm,
9 				       struct iwl_mvm_vif *mvm_vif)
10 {
11 	u32 link_id;
12 
13 	lockdep_assert_held(&mvm->mutex);
14 
15 	link_id = ffz(mvm->fw_link_ids_map);
16 
17 	/* this case can happen if there're deactivated but not removed links */
18 	if (link_id > IWL_MVM_FW_MAX_LINK_ID)
19 		return IWL_MVM_FW_LINK_ID_INVALID;
20 
21 	mvm->fw_link_ids_map |= BIT(link_id);
22 	return link_id;
23 }
24 
25 static void iwl_mvm_release_fw_link_id(struct iwl_mvm *mvm, u32 link_id)
26 {
27 	lockdep_assert_held(&mvm->mutex);
28 
29 	if (!WARN_ON(link_id > IWL_MVM_FW_MAX_LINK_ID))
30 		mvm->fw_link_ids_map &= ~BIT(link_id);
31 }
32 
33 static int iwl_mvm_link_cmd_send(struct iwl_mvm *mvm,
34 				 struct iwl_link_config_cmd *cmd,
35 				 enum iwl_ctxt_action action)
36 {
37 	int ret;
38 
39 	cmd->action = cpu_to_le32(action);
40 	ret = iwl_mvm_send_cmd_pdu(mvm,
41 				   WIDE_ID(MAC_CONF_GROUP, LINK_CONFIG_CMD), 0,
42 				   sizeof(*cmd), cmd);
43 	if (ret)
44 		IWL_ERR(mvm, "Failed to send LINK_CONFIG_CMD (action:%d): %d\n",
45 			action, ret);
46 	return ret;
47 }
48 
49 int iwl_mvm_add_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
50 		     struct ieee80211_bss_conf *link_conf)
51 {
52 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
53 	unsigned int link_id = link_conf->link_id;
54 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
55 	struct iwl_link_config_cmd cmd = {};
56 	struct iwl_mvm_phy_ctxt *phyctxt;
57 
58 	if (WARN_ON_ONCE(!link_info))
59 		return -EINVAL;
60 
61 	if (link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID) {
62 		link_info->fw_link_id = iwl_mvm_get_free_fw_link_id(mvm,
63 								    mvmvif);
64 		if (link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID)
65 			return -EINVAL;
66 
67 		rcu_assign_pointer(mvm->link_id_to_link_conf[link_info->fw_link_id],
68 				   link_conf);
69 	}
70 
71 	/* Update SF - Disable if needed. if this fails, SF might still be on
72 	 * while many macs are bound, which is forbidden - so fail the binding.
73 	 */
74 	if (iwl_mvm_sf_update(mvm, vif, false))
75 		return -EINVAL;
76 
77 	cmd.link_id = cpu_to_le32(link_info->fw_link_id);
78 	cmd.mac_id = cpu_to_le32(mvmvif->id);
79 	cmd.spec_link_id = link_conf->link_id;
80 	/* P2P-Device already has a valid PHY context during add */
81 	phyctxt = link_info->phy_ctxt;
82 	if (phyctxt)
83 		cmd.phy_id = cpu_to_le32(phyctxt->id);
84 	else
85 		cmd.phy_id = cpu_to_le32(FW_CTXT_INVALID);
86 
87 	memcpy(cmd.local_link_addr, link_conf->addr, ETH_ALEN);
88 
89 	if (vif->type == NL80211_IFTYPE_ADHOC && link_conf->bssid)
90 		memcpy(cmd.ibss_bssid_addr, link_conf->bssid, ETH_ALEN);
91 
92 	return iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_ADD);
93 }
94 
95 int iwl_mvm_link_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
96 			 struct ieee80211_bss_conf *link_conf,
97 			 u32 changes, bool active)
98 {
99 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
100 	unsigned int link_id = link_conf->link_id;
101 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
102 	struct iwl_mvm_phy_ctxt *phyctxt;
103 	struct iwl_link_config_cmd cmd = {};
104 	u32 ht_flag, flags = 0, flags_mask = 0;
105 	int ret;
106 
107 	if (WARN_ON_ONCE(!link_info ||
108 			 link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID))
109 		return -EINVAL;
110 
111 	if (changes & LINK_CONTEXT_MODIFY_ACTIVE) {
112 		/* When activating a link, phy context should be valid;
113 		 * when deactivating a link, it also should be valid since
114 		 * the link was active before. So, do nothing in this case.
115 		 * Since a link is added first with FW_CTXT_INVALID, then we
116 		 * can get here in case it's removed before it was activated.
117 		 */
118 		if (!link_info->phy_ctxt)
119 			return 0;
120 
121 		/* check there aren't too many active links */
122 		if (!link_info->active && active) {
123 			int i, count = 0;
124 
125 			/* link with phy_ctxt is active in FW */
126 			for_each_mvm_vif_valid_link(mvmvif, i)
127 				if (mvmvif->link[i]->phy_ctxt)
128 					count++;
129 
130 			if (vif->type == NL80211_IFTYPE_AP) {
131 				if (count > mvm->fw->ucode_capa.num_beacons)
132 					return -EOPNOTSUPP;
133 			/* this should be per HW or such */
134 			} else if (count >= IWL_MVM_FW_MAX_ACTIVE_LINKS_NUM) {
135 				return -EOPNOTSUPP;
136 			}
137 		}
138 
139 		/* Catch early if driver tries to activate or deactivate a link
140 		 * twice.
141 		 */
142 		WARN_ON_ONCE(active == link_info->active);
143 
144 		/* When deactivating a link session protection should
145 		 * be stopped
146 		 */
147 		if (!active && vif->type == NL80211_IFTYPE_STATION)
148 			iwl_mvm_stop_session_protection(mvm, vif);
149 	}
150 
151 	cmd.link_id = cpu_to_le32(link_info->fw_link_id);
152 
153 	/* The phy_id, link address and listen_lmac can be modified only until
154 	 * the link becomes active, otherwise they will be ignored.
155 	 */
156 	phyctxt = link_info->phy_ctxt;
157 	if (phyctxt)
158 		cmd.phy_id = cpu_to_le32(phyctxt->id);
159 	else
160 		cmd.phy_id = cpu_to_le32(FW_CTXT_INVALID);
161 	cmd.mac_id = cpu_to_le32(mvmvif->id);
162 
163 	memcpy(cmd.local_link_addr, link_conf->addr, ETH_ALEN);
164 
165 	cmd.active = cpu_to_le32(active);
166 
167 	if (vif->type == NL80211_IFTYPE_ADHOC && link_conf->bssid)
168 		memcpy(cmd.ibss_bssid_addr, link_conf->bssid, ETH_ALEN);
169 
170 	/* TODO: set a value to cmd.listen_lmac when system requiremens
171 	 * will define it
172 	 */
173 
174 	iwl_mvm_set_fw_basic_rates(mvm, vif, link_conf,
175 				   &cmd.cck_rates, &cmd.ofdm_rates);
176 
177 	cmd.cck_short_preamble = cpu_to_le32(link_conf->use_short_preamble);
178 	cmd.short_slot = cpu_to_le32(link_conf->use_short_slot);
179 
180 	/* The fw does not distinguish between ht and fat */
181 	ht_flag = LINK_PROT_FLG_HT_PROT | LINK_PROT_FLG_FAT_PROT;
182 	iwl_mvm_set_fw_protection_flags(mvm, vif, link_conf,
183 					&cmd.protection_flags,
184 					ht_flag, LINK_PROT_FLG_TGG_PROTECT);
185 
186 	iwl_mvm_set_fw_qos_params(mvm, vif, link_conf, &cmd.ac[0],
187 				  &cmd.qos_flags);
188 
189 
190 	cmd.bi = cpu_to_le32(link_conf->beacon_int);
191 	cmd.dtim_interval = cpu_to_le32(link_conf->beacon_int *
192 					link_conf->dtim_period);
193 
194 	if (!link_conf->he_support || iwlwifi_mod_params.disable_11ax ||
195 	    (vif->type == NL80211_IFTYPE_STATION && !vif->cfg.assoc)) {
196 		changes &= ~LINK_CONTEXT_MODIFY_HE_PARAMS;
197 		goto send_cmd;
198 	}
199 
200 	cmd.htc_trig_based_pkt_ext = link_conf->htc_trig_based_pkt_ext;
201 
202 	if (link_conf->uora_exists) {
203 		cmd.rand_alloc_ecwmin =
204 			link_conf->uora_ocw_range & 0x7;
205 		cmd.rand_alloc_ecwmax =
206 			(link_conf->uora_ocw_range >> 3) & 0x7;
207 	}
208 
209 	/* TODO  how to set ndp_fdbk_buff_th_exp? */
210 
211 	if (iwl_mvm_set_fw_mu_edca_params(mvm, mvmvif,
212 					  &cmd.trig_based_txf[0])) {
213 		flags |= LINK_FLG_MU_EDCA_CW;
214 		flags_mask |= LINK_FLG_MU_EDCA_CW;
215 	}
216 
217 	if (link_conf->eht_puncturing && !iwlwifi_mod_params.disable_11be)
218 		cmd.puncture_mask = cpu_to_le16(link_conf->eht_puncturing);
219 	else
220 		/* This flag can be set only if the MAC has eht support */
221 		changes &= ~LINK_CONTEXT_MODIFY_EHT_PARAMS;
222 
223 	cmd.bss_color = link_conf->he_bss_color.color;
224 
225 	if (!link_conf->he_bss_color.enabled) {
226 		flags |= LINK_FLG_BSS_COLOR_DIS;
227 		flags_mask |= LINK_FLG_BSS_COLOR_DIS;
228 	}
229 
230 	cmd.frame_time_rts_th = cpu_to_le16(link_conf->frame_time_rts_th);
231 
232 	/* Block 26-tone RU OFDMA transmissions */
233 	if (link_info->he_ru_2mhz_block) {
234 		flags |= LINK_FLG_RU_2MHZ_BLOCK;
235 		flags_mask |= LINK_FLG_RU_2MHZ_BLOCK;
236 	}
237 
238 	if (link_conf->nontransmitted) {
239 		ether_addr_copy(cmd.ref_bssid_addr,
240 				link_conf->transmitter_bssid);
241 		cmd.bssid_index = link_conf->bssid_index;
242 	}
243 
244 send_cmd:
245 	cmd.modify_mask = cpu_to_le32(changes);
246 	cmd.flags = cpu_to_le32(flags);
247 	cmd.flags_mask = cpu_to_le32(flags_mask);
248 
249 	ret = iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_MODIFY);
250 	if (!ret && (changes & LINK_CONTEXT_MODIFY_ACTIVE))
251 		link_info->active = active;
252 
253 	return ret;
254 }
255 
256 int iwl_mvm_remove_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
257 			struct ieee80211_bss_conf *link_conf)
258 {
259 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
260 	unsigned int link_id = link_conf->link_id;
261 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
262 	struct iwl_link_config_cmd cmd = {};
263 	int ret;
264 
265 	if (WARN_ON(!link_info ||
266 		    link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID))
267 		return -EINVAL;
268 
269 	RCU_INIT_POINTER(mvm->link_id_to_link_conf[link_info->fw_link_id],
270 			 NULL);
271 	cmd.link_id = cpu_to_le32(link_info->fw_link_id);
272 	iwl_mvm_release_fw_link_id(mvm, link_info->fw_link_id);
273 	link_info->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
274 
275 	ret = iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_REMOVE);
276 
277 	if (!ret)
278 		if (iwl_mvm_sf_update(mvm, vif, true))
279 			IWL_ERR(mvm, "Failed to update SF state\n");
280 
281 	return ret;
282 }
283 
284 /* link should be deactivated before removal, so in most cases we need to
285  * perform these two operations together
286  */
287 int iwl_mvm_disable_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
288 			 struct ieee80211_bss_conf *link_conf)
289 {
290 	int ret;
291 
292 	ret = iwl_mvm_link_changed(mvm, vif, link_conf,
293 				   LINK_CONTEXT_MODIFY_ACTIVE, false);
294 	if (ret)
295 		return ret;
296 
297 	ret = iwl_mvm_remove_link(mvm, vif, link_conf);
298 	if (ret)
299 		return ret;
300 
301 	return ret;
302 }
303