1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2025 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 */
iwl_mvm_get_channel_width(const struct cfg80211_chan_def * chandef)12 u8 iwl_mvm_get_channel_width(const 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 IWL_PHY_CHANNEL_MODE20;
18 case NL80211_CHAN_WIDTH_40:
19 return IWL_PHY_CHANNEL_MODE40;
20 case NL80211_CHAN_WIDTH_80:
21 return IWL_PHY_CHANNEL_MODE80;
22 case NL80211_CHAN_WIDTH_160:
23 return IWL_PHY_CHANNEL_MODE160;
24 case NL80211_CHAN_WIDTH_320:
25 return IWL_PHY_CHANNEL_MODE320;
26 default:
27 WARN(1, "Invalid channel width=%u", chandef->width);
28 return IWL_PHY_CHANNEL_MODE20;
29 }
30 }
31
32 /*
33 * Maps the driver specific control channel position (relative to the center
34 * freq) definitions to the fw values
35 */
iwl_mvm_get_ctrl_pos(const struct cfg80211_chan_def * chandef)36 u8 iwl_mvm_get_ctrl_pos(const struct cfg80211_chan_def *chandef)
37 {
38 int offs = chandef->chan->center_freq - chandef->center_freq1;
39 int abs_offs = abs(offs);
40 u8 ret;
41
42 if (offs == 0) {
43 /*
44 * The FW is expected to check the control channel position only
45 * when in HT/VHT and the channel width is not 20MHz. Return
46 * this value as the default one.
47 */
48 return 0;
49 }
50
51 /* this results in a value 0-7, i.e. fitting into 0b0111 */
52 ret = (abs_offs - 10) / 20;
53 /*
54 * But we need the value to be in 0b1011 because 0b0100 is
55 * IWL_PHY_CTRL_POS_ABOVE, so shift bit 2 up to land in
56 * IWL_PHY_CTRL_POS_OFFS_EXT (0b1000)
57 */
58 ret = (ret & IWL_PHY_CTRL_POS_OFFS_MSK) |
59 ((ret & BIT(2)) << 1);
60 /* and add the above bit */
61 ret |= (offs > 0) * IWL_PHY_CTRL_POS_ABOVE;
62
63 return ret;
64 }
65
66 /*
67 * Construct the generic fields of the PHY context command
68 */
iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt * ctxt,struct iwl_phy_context_cmd * cmd,u32 action)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
iwl_mvm_phy_ctxt_set_rxchain(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,__le32 * rxchain_info,u8 chains_static,u8 chains_dynamic)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 *rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
103 PHY_RX_CHAIN_VALID_POS);
104 *rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
105 *rxchain_info |= cpu_to_le32(active_cnt <<
106 PHY_RX_CHAIN_MIMO_CNT_POS);
107 #ifdef CONFIG_IWLWIFI_DEBUGFS
108 if (unlikely(mvm->dbgfs_rx_phyinfo))
109 *rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
110 #endif
111 }
112
113 /*
114 * Add the phy configuration to the PHY context command
115 */
iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,struct iwl_phy_context_cmd_v1 * cmd,const struct cfg80211_chan_def * chandef,u8 chains_static,u8 chains_dynamic)116 static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm,
117 struct iwl_mvm_phy_ctxt *ctxt,
118 struct iwl_phy_context_cmd_v1 *cmd,
119 const struct cfg80211_chan_def *chandef,
120 u8 chains_static, u8 chains_dynamic)
121 {
122 struct iwl_phy_context_cmd_tail *tail =
123 iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);
124
125 /* Set the channel info data */
126 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
127
128 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &tail->rxchain_info,
129 chains_static, chains_dynamic);
130
131 tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
132 }
133
134 /*
135 * Add the phy configuration to the PHY context command
136 */
iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,struct iwl_phy_context_cmd * cmd,const struct cfg80211_chan_def * chandef,u8 chains_static,u8 chains_dynamic)137 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
138 struct iwl_mvm_phy_ctxt *ctxt,
139 struct iwl_phy_context_cmd *cmd,
140 const struct cfg80211_chan_def *chandef,
141 u8 chains_static, u8 chains_dynamic)
142 {
143 cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm,
144 chandef->chan->band));
145
146 /* Set the channel info data */
147 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
148
149 /* we only support RLC command version 2 */
150 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, RLC_CONFIG_CMD), 0) < 2)
151 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd->rxchain_info,
152 chains_static, chains_dynamic);
153 }
154
iwl_mvm_phy_send_rlc(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,u8 chains_static,u8 chains_dynamic)155 int iwl_mvm_phy_send_rlc(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
156 u8 chains_static, u8 chains_dynamic)
157 {
158 struct iwl_rlc_config_cmd cmd = {
159 .phy_id = cpu_to_le32(ctxt->id),
160 };
161
162 /* From version 3, RLC is offloaded to firmware, so the driver no
163 * longer needs to send cmd.rlc, note that we are not using any
164 * other fields in the command - don't send it.
165 */
166 if (iwl_mvm_has_rlc_offload(mvm) || ctxt->rlc_disabled)
167 return 0;
168
169 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,
170 RLC_CONFIG_CMD), 0) < 2)
171 return 0;
172
173 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_DRIVER_FORCE !=
174 PHY_RX_CHAIN_DRIVER_FORCE_MSK);
175 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_VALID !=
176 PHY_RX_CHAIN_VALID_MSK);
177 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE !=
178 PHY_RX_CHAIN_FORCE_SEL_MSK);
179 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE_MIMO !=
180 PHY_RX_CHAIN_FORCE_MIMO_SEL_MSK);
181 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_COUNT != PHY_RX_CHAIN_CNT_MSK);
182 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_MIMO_COUNT !=
183 PHY_RX_CHAIN_MIMO_CNT_MSK);
184
185 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd.rlc.rx_chain_info,
186 chains_static, chains_dynamic);
187
188 IWL_DEBUG_FW(mvm, "Send RLC command: phy=%d, rx_chain_info=0x%x\n",
189 ctxt->id, cmd.rlc.rx_chain_info);
190
191 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(RLC_CONFIG_CMD,
192 DATA_PATH_GROUP, 2),
193 0, sizeof(cmd), &cmd);
194 }
195
196 /*
197 * Send a command to apply the current phy configuration. The command is send
198 * only if something in the configuration changed: in case that this is the
199 * first time that the phy configuration is applied or in case that the phy
200 * configuration changed from the previous apply.
201 */
iwl_mvm_phy_ctxt_apply(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,const struct cfg80211_chan_def * chandef,u8 chains_static,u8 chains_dynamic,u32 action)202 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
203 struct iwl_mvm_phy_ctxt *ctxt,
204 const struct cfg80211_chan_def *chandef,
205 u8 chains_static, u8 chains_dynamic,
206 u32 action)
207 {
208 int ret;
209 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1);
210
211 if (ver >= 3 && ver <= 4) {
212 struct iwl_phy_context_cmd cmd = {};
213
214 /* Set the command header fields */
215 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action);
216
217 /* Set the command data */
218 iwl_mvm_phy_ctxt_cmd_data(mvm, ctxt, &cmd, chandef,
219 chains_static,
220 chains_dynamic);
221
222 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
223 0, sizeof(cmd), &cmd);
224 } else if (ver < 3) {
225 struct iwl_phy_context_cmd_v1 cmd = {};
226 u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
227
228 /* Set the command header fields */
229 iwl_mvm_phy_ctxt_cmd_hdr(ctxt,
230 (struct iwl_phy_context_cmd *)&cmd,
231 action);
232
233 /* Set the command data */
234 iwl_mvm_phy_ctxt_cmd_data_v1(mvm, ctxt, &cmd, chandef,
235 chains_static,
236 chains_dynamic);
237 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
238 0, len, &cmd);
239 } else {
240 IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver);
241 return -EOPNOTSUPP;
242 }
243
244
245 if (ret) {
246 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
247 return ret;
248 }
249
250 if (action != FW_CTXT_ACTION_REMOVE)
251 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
252 chains_dynamic);
253
254 return 0;
255 }
256
257 /*
258 * Send a command to add a PHY context based on the current HW configuration.
259 */
iwl_mvm_phy_ctxt_add(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,const struct cfg80211_chan_def * chandef,const struct cfg80211_chan_def * ap,u8 chains_static,u8 chains_dynamic)260 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
261 const struct cfg80211_chan_def *chandef,
262 const struct cfg80211_chan_def *ap,
263 u8 chains_static, u8 chains_dynamic)
264 {
265 int ret;
266
267 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
268 ctxt->ref);
269 lockdep_assert_held(&mvm->mutex);
270
271 ctxt->channel = chandef->chan;
272 ctxt->width = chandef->width;
273 ctxt->center_freq1 = chandef->center_freq1;
274
275 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
276 chains_static, chains_dynamic,
277 FW_CTXT_ACTION_ADD);
278
279 if (ret)
280 return ret;
281
282 ctxt->ref++;
283
284 return 0;
285 }
286
287 /*
288 * Update the number of references to the given PHY context. This is valid only
289 * in case the PHY context was already created, i.e., its reference count > 0.
290 */
iwl_mvm_phy_ctxt_ref(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt)291 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
292 {
293 lockdep_assert_held(&mvm->mutex);
294
295 /* If we were taking the first ref, we should have
296 * called iwl_mvm_phy_ctxt_add.
297 */
298 WARN_ON(!ctxt->ref);
299 ctxt->ref++;
300 }
301
302 /*
303 * Send a command to modify the PHY context based on the current HW
304 * configuration. Note that the function does not check that the configuration
305 * changed.
306 */
iwl_mvm_phy_ctxt_changed(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt,const struct cfg80211_chan_def * chandef,const struct cfg80211_chan_def * ap,u8 chains_static,u8 chains_dynamic)307 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
308 const struct cfg80211_chan_def *chandef,
309 const struct cfg80211_chan_def *ap,
310 u8 chains_static, u8 chains_dynamic)
311 {
312 enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
313
314 lockdep_assert_held(&mvm->mutex);
315
316 if (WARN_ON_ONCE(!ctxt->ref))
317 return -EINVAL;
318
319 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,
320 RLC_CONFIG_CMD), 0) >= 2 &&
321 ctxt->channel == chandef->chan &&
322 ctxt->width == chandef->width &&
323 ctxt->center_freq1 == chandef->center_freq1)
324 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
325 chains_dynamic);
326
327 if (fw_has_capa(&mvm->fw->ucode_capa,
328 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
329 ctxt->channel->band != chandef->chan->band) {
330 int ret;
331
332 /* ... remove it here ...*/
333 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
334 chains_static, chains_dynamic,
335 FW_CTXT_ACTION_REMOVE);
336 if (ret)
337 return ret;
338
339 /* ... and proceed to add it again */
340 action = FW_CTXT_ACTION_ADD;
341 }
342
343 ctxt->channel = chandef->chan;
344 ctxt->width = chandef->width;
345 ctxt->center_freq1 = chandef->center_freq1;
346
347 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
348 chains_static, chains_dynamic,
349 action);
350 }
351
iwl_mvm_phy_ctxt_unref(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt)352 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
353 {
354 struct cfg80211_chan_def chandef;
355 lockdep_assert_held(&mvm->mutex);
356
357 if (WARN_ON_ONCE(!ctxt))
358 return;
359
360 ctxt->ref--;
361
362 if (ctxt->ref)
363 return;
364
365 cfg80211_chandef_create(&chandef, ctxt->channel, NL80211_CHAN_NO_HT);
366
367 iwl_mvm_phy_ctxt_apply(mvm, ctxt, &chandef, 1, 1,
368 FW_CTXT_ACTION_REMOVE);
369 }
370
iwl_mvm_binding_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)371 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
372 struct ieee80211_vif *vif)
373 {
374 unsigned long *data = _data;
375 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
376
377 if (!mvmvif->deflink.phy_ctxt)
378 return;
379
380 if (vif->type == NL80211_IFTYPE_STATION ||
381 vif->type == NL80211_IFTYPE_AP)
382 __set_bit(mvmvif->deflink.phy_ctxt->id, data);
383 }
384
iwl_mvm_phy_ctx_count(struct iwl_mvm * mvm)385 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
386 {
387 unsigned long phy_ctxt_counter = 0;
388
389 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
390 IEEE80211_IFACE_ITER_NORMAL,
391 iwl_mvm_binding_iterator,
392 &phy_ctxt_counter);
393
394 return hweight8(phy_ctxt_counter);
395 }
396