1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2020 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 					 __le32 *rxchain_info,
80 					 u8 chains_static,
81 					 u8 chains_dynamic)
82 {
83 	u8 active_cnt, idle_cnt;
84 
85 	/* Set rx the chains */
86 	idle_cnt = chains_static;
87 	active_cnt = chains_dynamic;
88 
89 	/* In scenarios where we only ever use a single-stream rates,
90 	 * i.e. legacy 11b/g/a associations, single-stream APs or even
91 	 * static SMPS, enable both chains to get diversity, improving
92 	 * the case where we're far enough from the AP that attenuation
93 	 * between the two antennas is sufficiently different to impact
94 	 * performance.
95 	 */
96 	if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
97 		idle_cnt = 2;
98 		active_cnt = 2;
99 	}
100 
101 	*rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
102 					PHY_RX_CHAIN_VALID_POS);
103 	*rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
104 	*rxchain_info |= cpu_to_le32(active_cnt <<
105 					 PHY_RX_CHAIN_MIMO_CNT_POS);
106 #ifdef CONFIG_IWLWIFI_DEBUGFS
107 	if (unlikely(mvm->dbgfs_rx_phyinfo))
108 		*rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
109 #endif
110 }
111 
112 /*
113  * Add the phy configuration to the PHY context command
114  */
115 static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm,
116 					 struct iwl_phy_context_cmd_v1 *cmd,
117 					 struct cfg80211_chan_def *chandef,
118 					 u8 chains_static, u8 chains_dynamic)
119 {
120 	struct iwl_phy_context_cmd_tail *tail =
121 		iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);
122 
123 	/* Set the channel info data */
124 	iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
125 
126 	iwl_mvm_phy_ctxt_set_rxchain(mvm, &tail->rxchain_info,
127 				     chains_static, chains_dynamic);
128 
129 	tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
130 }
131 
132 /*
133  * Add the phy configuration to the PHY context command
134  */
135 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
136 				      struct iwl_phy_context_cmd *cmd,
137 				      struct cfg80211_chan_def *chandef,
138 				      u8 chains_static, u8 chains_dynamic)
139 {
140 	cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm->fw,
141 						       chandef->chan->band));
142 
143 	/* Set the channel info data */
144 	iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
145 
146 	iwl_mvm_phy_ctxt_set_rxchain(mvm, &cmd->rxchain_info,
147 				     chains_static, chains_dynamic);
148 }
149 
150 /*
151  * Send a command to apply the current phy configuration. The command is send
152  * only if something in the configuration changed: in case that this is the
153  * first time that the phy configuration is applied or in case that the phy
154  * configuration changed from the previous apply.
155  */
156 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
157 				  struct iwl_mvm_phy_ctxt *ctxt,
158 				  struct cfg80211_chan_def *chandef,
159 				  u8 chains_static, u8 chains_dynamic,
160 				  u32 action)
161 {
162 	int ret;
163 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
164 					PHY_CONTEXT_CMD, 1);
165 
166 	if (ver == 3) {
167 		struct iwl_phy_context_cmd cmd = {};
168 
169 		/* Set the command header fields */
170 		iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action);
171 
172 		/* Set the command data */
173 		iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
174 					  chains_static,
175 					  chains_dynamic);
176 
177 		ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
178 					   0, sizeof(cmd), &cmd);
179 	} else if (ver < 3) {
180 		struct iwl_phy_context_cmd_v1 cmd = {};
181 		u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
182 
183 		/* Set the command header fields */
184 		iwl_mvm_phy_ctxt_cmd_hdr(ctxt,
185 					 (struct iwl_phy_context_cmd *)&cmd,
186 					 action);
187 
188 		/* Set the command data */
189 		iwl_mvm_phy_ctxt_cmd_data_v1(mvm, &cmd, chandef,
190 					     chains_static,
191 					     chains_dynamic);
192 		ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
193 					   0, len, &cmd);
194 	} else {
195 		IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver);
196 		return -EOPNOTSUPP;
197 	}
198 
199 
200 	if (ret)
201 		IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
202 	return ret;
203 }
204 
205 /*
206  * Send a command to add a PHY context based on the current HW configuration.
207  */
208 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
209 			 struct cfg80211_chan_def *chandef,
210 			 u8 chains_static, u8 chains_dynamic)
211 {
212 	WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
213 		ctxt->ref);
214 	lockdep_assert_held(&mvm->mutex);
215 
216 	ctxt->channel = chandef->chan;
217 
218 	return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
219 				      chains_static, chains_dynamic,
220 				      FW_CTXT_ACTION_ADD);
221 }
222 
223 /*
224  * Update the number of references to the given PHY context. This is valid only
225  * in case the PHY context was already created, i.e., its reference count > 0.
226  */
227 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
228 {
229 	lockdep_assert_held(&mvm->mutex);
230 	ctxt->ref++;
231 }
232 
233 /*
234  * Send a command to modify the PHY context based on the current HW
235  * configuration. Note that the function does not check that the configuration
236  * changed.
237  */
238 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
239 			     struct cfg80211_chan_def *chandef,
240 			     u8 chains_static, u8 chains_dynamic)
241 {
242 	enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
243 
244 	lockdep_assert_held(&mvm->mutex);
245 
246 	if (fw_has_capa(&mvm->fw->ucode_capa,
247 			IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
248 	    ctxt->channel->band != chandef->chan->band) {
249 		int ret;
250 
251 		/* ... remove it here ...*/
252 		ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
253 					     chains_static, chains_dynamic,
254 					     FW_CTXT_ACTION_REMOVE);
255 		if (ret)
256 			return ret;
257 
258 		/* ... and proceed to add it again */
259 		action = FW_CTXT_ACTION_ADD;
260 	}
261 
262 	ctxt->channel = chandef->chan;
263 	ctxt->width = chandef->width;
264 	return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
265 				      chains_static, chains_dynamic,
266 				      action);
267 }
268 
269 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
270 {
271 	lockdep_assert_held(&mvm->mutex);
272 
273 	if (WARN_ON_ONCE(!ctxt))
274 		return;
275 
276 	ctxt->ref--;
277 
278 	/*
279 	 * Move unused phy's to a default channel. When the phy is moved the,
280 	 * fw will cleanup immediate quiet bit if it was previously set,
281 	 * otherwise we might not be able to reuse this phy.
282 	 */
283 	if (ctxt->ref == 0) {
284 		struct ieee80211_channel *chan;
285 		struct cfg80211_chan_def chandef;
286 		struct ieee80211_supported_band *sband = NULL;
287 		enum nl80211_band band = NL80211_BAND_2GHZ;
288 
289 		while (!sband && band < NUM_NL80211_BANDS)
290 			sband = mvm->hw->wiphy->bands[band++];
291 
292 		if (WARN_ON(!sband))
293 			return;
294 
295 		chan = &sband->channels[0];
296 
297 		cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
298 		iwl_mvm_phy_ctxt_changed(mvm, ctxt, &chandef, 1, 1);
299 	}
300 }
301 
302 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
303 				     struct ieee80211_vif *vif)
304 {
305 	unsigned long *data = _data;
306 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
307 
308 	if (!mvmvif->phy_ctxt)
309 		return;
310 
311 	if (vif->type == NL80211_IFTYPE_STATION ||
312 	    vif->type == NL80211_IFTYPE_AP)
313 		__set_bit(mvmvif->phy_ctxt->id, data);
314 }
315 
316 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
317 {
318 	unsigned long phy_ctxt_counter = 0;
319 
320 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
321 						   IEEE80211_IFACE_ITER_NORMAL,
322 						   iwl_mvm_binding_iterator,
323 						   &phy_ctxt_counter);
324 
325 	return hweight8(phy_ctxt_counter);
326 }
327