1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66#include <net/mac80211.h>
67#include "fw-api.h"
68#include "mvm.h"
69
70
71u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
72{
73 switch (chandef->width) {
74 case NL80211_CHAN_WIDTH_20_NOHT:
75 case NL80211_CHAN_WIDTH_20:
76 return PHY_VHT_CHANNEL_MODE20;
77 case NL80211_CHAN_WIDTH_40:
78 return PHY_VHT_CHANNEL_MODE40;
79 case NL80211_CHAN_WIDTH_80:
80 return PHY_VHT_CHANNEL_MODE80;
81 case NL80211_CHAN_WIDTH_160:
82 return PHY_VHT_CHANNEL_MODE160;
83 default:
84 WARN(1, "Invalid channel width=%u", chandef->width);
85 return PHY_VHT_CHANNEL_MODE20;
86 }
87}
88
89
90
91
92
93u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
94{
95 switch (chandef->chan->center_freq - chandef->center_freq1) {
96 case -70:
97 return PHY_VHT_CTRL_POS_4_BELOW;
98 case -50:
99 return PHY_VHT_CTRL_POS_3_BELOW;
100 case -30:
101 return PHY_VHT_CTRL_POS_2_BELOW;
102 case -10:
103 return PHY_VHT_CTRL_POS_1_BELOW;
104 case 10:
105 return PHY_VHT_CTRL_POS_1_ABOVE;
106 case 30:
107 return PHY_VHT_CTRL_POS_2_ABOVE;
108 case 50:
109 return PHY_VHT_CTRL_POS_3_ABOVE;
110 case 70:
111 return PHY_VHT_CTRL_POS_4_ABOVE;
112 default:
113 WARN(1, "Invalid channel definition");
114 case 0:
115
116
117
118
119
120 return PHY_VHT_CTRL_POS_1_BELOW;
121 }
122}
123
124
125
126
127static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
128 struct iwl_phy_context_cmd *cmd,
129 u32 action, u32 apply_time)
130{
131 memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
132
133 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
134 ctxt->color));
135 cmd->action = cpu_to_le32(action);
136 cmd->apply_time = cpu_to_le32(apply_time);
137}
138
139
140
141
142static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
143 struct iwl_phy_context_cmd *cmd,
144 struct cfg80211_chan_def *chandef,
145 u8 chains_static, u8 chains_dynamic)
146{
147 u8 active_cnt, idle_cnt;
148
149
150 cmd->ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ?
151 PHY_BAND_24 : PHY_BAND_5);
152
153 cmd->ci.channel = chandef->chan->hw_value;
154 cmd->ci.width = iwl_mvm_get_channel_width(chandef);
155 cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
156
157
158 idle_cnt = chains_static;
159 active_cnt = chains_dynamic;
160
161
162
163
164
165
166
167
168 if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
169 idle_cnt = 2;
170 active_cnt = 2;
171 }
172
173 cmd->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
174 PHY_RX_CHAIN_VALID_POS);
175 cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
176 cmd->rxchain_info |= cpu_to_le32(active_cnt <<
177 PHY_RX_CHAIN_MIMO_CNT_POS);
178#ifdef CONFIG_IWLWIFI_DEBUGFS
179 if (unlikely(mvm->dbgfs_rx_phyinfo))
180 cmd->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
181#endif
182
183 cmd->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
184}
185
186
187
188
189
190
191
192static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
193 struct iwl_mvm_phy_ctxt *ctxt,
194 struct cfg80211_chan_def *chandef,
195 u8 chains_static, u8 chains_dynamic,
196 u32 action, u32 apply_time)
197{
198 struct iwl_phy_context_cmd cmd;
199 int ret;
200
201
202 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
203
204
205 iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
206 chains_static, chains_dynamic);
207
208 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0,
209 sizeof(struct iwl_phy_context_cmd),
210 &cmd);
211 if (ret)
212 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
213 return ret;
214}
215
216
217
218
219int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
220 struct cfg80211_chan_def *chandef,
221 u8 chains_static, u8 chains_dynamic)
222{
223 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
224 ctxt->ref);
225 lockdep_assert_held(&mvm->mutex);
226
227 ctxt->channel = chandef->chan;
228
229 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
230 chains_static, chains_dynamic,
231 FW_CTXT_ACTION_ADD, 0);
232}
233
234
235
236
237
238void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
239{
240 lockdep_assert_held(&mvm->mutex);
241 ctxt->ref++;
242}
243
244
245
246
247
248
249int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
250 struct cfg80211_chan_def *chandef,
251 u8 chains_static, u8 chains_dynamic)
252{
253 lockdep_assert_held(&mvm->mutex);
254
255 ctxt->channel = chandef->chan;
256 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
257 chains_static, chains_dynamic,
258 FW_CTXT_ACTION_MODIFY, 0);
259}
260
261void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
262{
263 lockdep_assert_held(&mvm->mutex);
264
265 if (WARN_ON_ONCE(!ctxt))
266 return;
267
268 ctxt->ref--;
269}
270
271static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
272 struct ieee80211_vif *vif)
273{
274 unsigned long *data = _data;
275 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
276
277 if (!mvmvif->phy_ctxt)
278 return;
279
280 if (vif->type == NL80211_IFTYPE_STATION ||
281 vif->type == NL80211_IFTYPE_AP)
282 __set_bit(mvmvif->phy_ctxt->id, data);
283}
284
285int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
286{
287 unsigned long phy_ctxt_counter = 0;
288
289 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
290 IEEE80211_IFACE_ITER_NORMAL,
291 iwl_mvm_binding_iterator,
292 &phy_ctxt_counter);
293
294 return hweight8(phy_ctxt_counter);
295}
296