1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2021 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  * Copyright (C) 2017 Intel Deutschland GmbH
6  */
7 #include <net/mac80211.h>
8 #include "fw-api.h"
9 #include "mvm.h"
10 
11 /* Maps the driver specific channel width definition to the fw values */
12 u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
13 {
14 	switch (chandef->width) {
15 	case NL80211_CHAN_WIDTH_20_NOHT:
16 	case NL80211_CHAN_WIDTH_20:
17 		return PHY_VHT_CHANNEL_MODE20;
18 	case NL80211_CHAN_WIDTH_40:
19 		return PHY_VHT_CHANNEL_MODE40;
20 	case NL80211_CHAN_WIDTH_80:
21 		return PHY_VHT_CHANNEL_MODE80;
22 	case NL80211_CHAN_WIDTH_160:
23 		return PHY_VHT_CHANNEL_MODE160;
24 	default:
25 		WARN(1, "Invalid channel width=%u", chandef->width);
26 		return PHY_VHT_CHANNEL_MODE20;
27 	}
28 }
29 
30 /*
31  * Maps the driver specific control channel position (relative to the center
32  * freq) definitions to the the fw values
33  */
34 u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
35 {
36 	switch (chandef->chan->center_freq - chandef->center_freq1) {
37 	case -70:
38 		return PHY_VHT_CTRL_POS_4_BELOW;
39 	case -50:
40 		return PHY_VHT_CTRL_POS_3_BELOW;
41 	case -30:
42 		return PHY_VHT_CTRL_POS_2_BELOW;
43 	case -10:
44 		return PHY_VHT_CTRL_POS_1_BELOW;
45 	case  10:
46 		return PHY_VHT_CTRL_POS_1_ABOVE;
47 	case  30:
48 		return PHY_VHT_CTRL_POS_2_ABOVE;
49 	case  50:
50 		return PHY_VHT_CTRL_POS_3_ABOVE;
51 	case  70:
52 		return PHY_VHT_CTRL_POS_4_ABOVE;
53 	default:
54 		WARN(1, "Invalid channel definition");
55 		fallthrough;
56 	case 0:
57 		/*
58 		 * The FW is expected to check the control channel position only
59 		 * when in HT/VHT and the channel width is not 20MHz. Return
60 		 * this value as the default one.
61 		 */
62 		return PHY_VHT_CTRL_POS_1_BELOW;
63 	}
64 }
65 
66 /*
67  * Construct the generic fields of the PHY context command
68  */
69 static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
70 				     struct iwl_phy_context_cmd *cmd,
71 				     u32 action)
72 {
73 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
74 							    ctxt->color));
75 	cmd->action = cpu_to_le32(action);
76 }
77 
78 static void iwl_mvm_phy_ctxt_set_rxchain(struct iwl_mvm *mvm,
79 					 struct iwl_mvm_phy_ctxt *ctxt,
80 					 __le32 *rxchain_info,
81 					 u8 chains_static,
82 					 u8 chains_dynamic)
83 {
84 	u8 active_cnt, idle_cnt;
85 
86 	/* Set rx the chains */
87 	idle_cnt = chains_static;
88 	active_cnt = chains_dynamic;
89 
90 	/* In scenarios where we only ever use a single-stream rates,
91 	 * i.e. legacy 11b/g/a associations, single-stream APs or even
92 	 * static SMPS, enable both chains to get diversity, improving
93 	 * the case where we're far enough from the AP that attenuation
94 	 * between the two antennas is sufficiently different to impact
95 	 * performance.
96 	 */
97 	if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm, ctxt)) {
98 		idle_cnt = 2;
99 		active_cnt = 2;
100 	}
101 
102 	/*
103 	 * If the firmware requested it, then we know that it supports
104 	 * getting zero for the values to indicate "use one, but pick
105 	 * which one yourself", which means it can dynamically pick one
106 	 * that e.g. has better RSSI.
107 	 */
108 	if (mvm->fw_static_smps_request && active_cnt == 1 && idle_cnt == 1) {
109 		idle_cnt = 0;
110 		active_cnt = 0;
111 	}
112 
113 	*rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
114 					PHY_RX_CHAIN_VALID_POS);
115 	*rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
116 	*rxchain_info |= cpu_to_le32(active_cnt <<
117 					 PHY_RX_CHAIN_MIMO_CNT_POS);
118 #ifdef CONFIG_IWLWIFI_DEBUGFS
119 	if (unlikely(mvm->dbgfs_rx_phyinfo))
120 		*rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
121 #endif
122 }
123 
124 /*
125  * Add the phy configuration to the PHY context command
126  */
127 static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm,
128 					 struct iwl_mvm_phy_ctxt *ctxt,
129 					 struct iwl_phy_context_cmd_v1 *cmd,
130 					 struct cfg80211_chan_def *chandef,
131 					 u8 chains_static, u8 chains_dynamic)
132 {
133 	struct iwl_phy_context_cmd_tail *tail =
134 		iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);
135 
136 	/* Set the channel info data */
137 	iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
138 
139 	iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &tail->rxchain_info,
140 				     chains_static, chains_dynamic);
141 
142 	tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
143 }
144 
145 /*
146  * Add the phy configuration to the PHY context command
147  */
148 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
149 				      struct iwl_mvm_phy_ctxt *ctxt,
150 				      struct iwl_phy_context_cmd *cmd,
151 				      struct cfg80211_chan_def *chandef,
152 				      u8 chains_static, u8 chains_dynamic)
153 {
154 	cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm->fw,
155 						       chandef->chan->band));
156 
157 	/* Set the channel info data */
158 	iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
159 
160 	/* we only support RLC command version 2 */
161 	if (iwl_fw_lookup_cmd_ver(mvm->fw, DATA_PATH_GROUP,
162 				  RLC_CONFIG_CMD, 0) < 2)
163 		iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd->rxchain_info,
164 					     chains_static, chains_dynamic);
165 }
166 
167 static int iwl_mvm_phy_send_rlc(struct iwl_mvm *mvm,
168 				struct iwl_mvm_phy_ctxt *ctxt,
169 				u8 chains_static, u8 chains_dynamic)
170 {
171 	struct iwl_rlc_config_cmd cmd = {
172 		.phy_id = cpu_to_le32(ctxt->id),
173 	};
174 
175 	if (iwl_fw_lookup_cmd_ver(mvm->fw, DATA_PATH_GROUP,
176 				  RLC_CONFIG_CMD, 0) < 2)
177 		return 0;
178 
179 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_DRIVER_FORCE !=
180 		     PHY_RX_CHAIN_DRIVER_FORCE_MSK);
181 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_VALID !=
182 		     PHY_RX_CHAIN_VALID_MSK);
183 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE !=
184 		     PHY_RX_CHAIN_FORCE_SEL_MSK);
185 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE_MIMO !=
186 		     PHY_RX_CHAIN_FORCE_MIMO_SEL_MSK);
187 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_COUNT != PHY_RX_CHAIN_CNT_MSK);
188 	BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_MIMO_COUNT !=
189 		     PHY_RX_CHAIN_MIMO_CNT_MSK);
190 
191 	iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd.rlc.rx_chain_info,
192 				     chains_static, chains_dynamic);
193 
194 	return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(RLC_CONFIG_CMD,
195 						    DATA_PATH_GROUP, 2),
196 				    0, sizeof(cmd), &cmd);
197 }
198 
199 /*
200  * Send a command to apply the current phy configuration. The command is send
201  * only if something in the configuration changed: in case that this is the
202  * first time that the phy configuration is applied or in case that the phy
203  * configuration changed from the previous apply.
204  */
205 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
206 				  struct iwl_mvm_phy_ctxt *ctxt,
207 				  struct cfg80211_chan_def *chandef,
208 				  u8 chains_static, u8 chains_dynamic,
209 				  u32 action)
210 {
211 	int ret;
212 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
213 					PHY_CONTEXT_CMD, 1);
214 
215 	if (ver == 3 || ver == 4) {
216 		struct iwl_phy_context_cmd cmd = {};
217 
218 		/* Set the command header fields */
219 		iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action);
220 
221 		/* Set the command data */
222 		iwl_mvm_phy_ctxt_cmd_data(mvm, ctxt, &cmd, chandef,
223 					  chains_static,
224 					  chains_dynamic);
225 
226 		ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
227 					   0, sizeof(cmd), &cmd);
228 	} else if (ver < 3) {
229 		struct iwl_phy_context_cmd_v1 cmd = {};
230 		u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
231 
232 		/* Set the command header fields */
233 		iwl_mvm_phy_ctxt_cmd_hdr(ctxt,
234 					 (struct iwl_phy_context_cmd *)&cmd,
235 					 action);
236 
237 		/* Set the command data */
238 		iwl_mvm_phy_ctxt_cmd_data_v1(mvm, ctxt, &cmd, chandef,
239 					     chains_static,
240 					     chains_dynamic);
241 		ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
242 					   0, len, &cmd);
243 	} else {
244 		IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver);
245 		return -EOPNOTSUPP;
246 	}
247 
248 
249 	if (ret) {
250 		IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
251 		return ret;
252 	}
253 
254 	if (action != FW_CTXT_ACTION_REMOVE)
255 		return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
256 					    chains_dynamic);
257 
258 	return 0;
259 }
260 
261 /*
262  * Send a command to add a PHY context based on the current HW configuration.
263  */
264 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
265 			 struct cfg80211_chan_def *chandef,
266 			 u8 chains_static, u8 chains_dynamic)
267 {
268 	WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
269 		ctxt->ref);
270 	lockdep_assert_held(&mvm->mutex);
271 
272 	ctxt->channel = chandef->chan;
273 	ctxt->width = chandef->width;
274 	ctxt->center_freq1 = chandef->center_freq1;
275 
276 	return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
277 				      chains_static, chains_dynamic,
278 				      FW_CTXT_ACTION_ADD);
279 }
280 
281 /*
282  * Update the number of references to the given PHY context. This is valid only
283  * in case the PHY context was already created, i.e., its reference count > 0.
284  */
285 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
286 {
287 	lockdep_assert_held(&mvm->mutex);
288 	ctxt->ref++;
289 }
290 
291 /*
292  * Send a command to modify the PHY context based on the current HW
293  * configuration. Note that the function does not check that the configuration
294  * changed.
295  */
296 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
297 			     struct cfg80211_chan_def *chandef,
298 			     u8 chains_static, u8 chains_dynamic)
299 {
300 	enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
301 
302 	lockdep_assert_held(&mvm->mutex);
303 
304 	if (iwl_fw_lookup_cmd_ver(mvm->fw, DATA_PATH_GROUP,
305 				  RLC_CONFIG_CMD, 0) >= 2 &&
306 	    ctxt->channel == chandef->chan &&
307 	    ctxt->width == chandef->width &&
308 	    ctxt->center_freq1 == chandef->center_freq1)
309 		return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
310 					    chains_dynamic);
311 
312 	if (fw_has_capa(&mvm->fw->ucode_capa,
313 			IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
314 	    ctxt->channel->band != chandef->chan->band) {
315 		int ret;
316 
317 		/* ... remove it here ...*/
318 		ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
319 					     chains_static, chains_dynamic,
320 					     FW_CTXT_ACTION_REMOVE);
321 		if (ret)
322 			return ret;
323 
324 		/* ... and proceed to add it again */
325 		action = FW_CTXT_ACTION_ADD;
326 	}
327 
328 	ctxt->channel = chandef->chan;
329 	ctxt->width = chandef->width;
330 	ctxt->center_freq1 = chandef->center_freq1;
331 
332 	return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
333 				      chains_static, chains_dynamic,
334 				      action);
335 }
336 
337 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
338 {
339 	lockdep_assert_held(&mvm->mutex);
340 
341 	if (WARN_ON_ONCE(!ctxt))
342 		return;
343 
344 	ctxt->ref--;
345 
346 	/*
347 	 * Move unused phy's to a default channel. When the phy is moved the,
348 	 * fw will cleanup immediate quiet bit if it was previously set,
349 	 * otherwise we might not be able to reuse this phy.
350 	 */
351 	if (ctxt->ref == 0) {
352 		struct ieee80211_channel *chan;
353 		struct cfg80211_chan_def chandef;
354 		struct ieee80211_supported_band *sband = NULL;
355 		enum nl80211_band band = NL80211_BAND_2GHZ;
356 
357 		while (!sband && band < NUM_NL80211_BANDS)
358 			sband = mvm->hw->wiphy->bands[band++];
359 
360 		if (WARN_ON(!sband))
361 			return;
362 
363 		chan = &sband->channels[0];
364 
365 		cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
366 		iwl_mvm_phy_ctxt_changed(mvm, ctxt, &chandef, 1, 1);
367 	}
368 }
369 
370 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
371 				     struct ieee80211_vif *vif)
372 {
373 	unsigned long *data = _data;
374 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
375 
376 	if (!mvmvif->phy_ctxt)
377 		return;
378 
379 	if (vif->type == NL80211_IFTYPE_STATION ||
380 	    vif->type == NL80211_IFTYPE_AP)
381 		__set_bit(mvmvif->phy_ctxt->id, data);
382 }
383 
384 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
385 {
386 	unsigned long phy_ctxt_counter = 0;
387 
388 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
389 						   IEEE80211_IFACE_ITER_NORMAL,
390 						   iwl_mvm_binding_iterator,
391 						   &phy_ctxt_counter);
392 
393 	return hweight8(phy_ctxt_counter);
394 }
395