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#include <net/mac80211.h>
65#include "fw-api.h"
66#include "mvm.h"
67
68
69static inline u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
70{
71 switch (chandef->width) {
72 case NL80211_CHAN_WIDTH_20_NOHT:
73 case NL80211_CHAN_WIDTH_20:
74 return PHY_VHT_CHANNEL_MODE20;
75 case NL80211_CHAN_WIDTH_40:
76 return PHY_VHT_CHANNEL_MODE40;
77 case NL80211_CHAN_WIDTH_80:
78 return PHY_VHT_CHANNEL_MODE80;
79 case NL80211_CHAN_WIDTH_160:
80 return PHY_VHT_CHANNEL_MODE160;
81 default:
82 WARN(1, "Invalid channel width=%u", chandef->width);
83 return PHY_VHT_CHANNEL_MODE20;
84 }
85}
86
87
88
89
90
91static inline u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
92{
93 switch (chandef->chan->center_freq - chandef->center_freq1) {
94 case -70:
95 return PHY_VHT_CTRL_POS_4_BELOW;
96 case -50:
97 return PHY_VHT_CTRL_POS_3_BELOW;
98 case -30:
99 return PHY_VHT_CTRL_POS_2_BELOW;
100 case -10:
101 return PHY_VHT_CTRL_POS_1_BELOW;
102 case 10:
103 return PHY_VHT_CTRL_POS_1_ABOVE;
104 case 30:
105 return PHY_VHT_CTRL_POS_2_ABOVE;
106 case 50:
107 return PHY_VHT_CTRL_POS_3_ABOVE;
108 case 70:
109 return PHY_VHT_CTRL_POS_4_ABOVE;
110 default:
111 WARN(1, "Invalid channel definition");
112 case 0:
113
114
115
116
117
118 return PHY_VHT_CTRL_POS_1_BELOW;
119 }
120}
121
122
123
124
125static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
126 struct iwl_phy_context_cmd *cmd,
127 u32 action, u32 apply_time)
128{
129 memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
130
131 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
132 ctxt->color));
133 cmd->action = cpu_to_le32(action);
134 cmd->apply_time = cpu_to_le32(apply_time);
135}
136
137
138
139
140static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
141 struct iwl_phy_context_cmd *cmd,
142 struct cfg80211_chan_def *chandef,
143 u8 chains_static, u8 chains_dynamic)
144{
145 u8 active_cnt, idle_cnt;
146
147
148 cmd->ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ?
149 PHY_BAND_24 : PHY_BAND_5);
150
151 cmd->ci.channel = chandef->chan->hw_value;
152 cmd->ci.width = iwl_mvm_get_channel_width(chandef);
153 cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
154
155
156 idle_cnt = chains_static;
157 active_cnt = chains_dynamic;
158
159 cmd->rxchain_info = cpu_to_le32(iwl_fw_valid_rx_ant(mvm->fw) <<
160 PHY_RX_CHAIN_VALID_POS);
161 cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
162 cmd->rxchain_info |= cpu_to_le32(active_cnt <<
163 PHY_RX_CHAIN_MIMO_CNT_POS);
164
165 cmd->txchain_info = cpu_to_le32(iwl_fw_valid_tx_ant(mvm->fw));
166}
167
168
169
170
171
172
173
174static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
175 struct iwl_mvm_phy_ctxt *ctxt,
176 struct cfg80211_chan_def *chandef,
177 u8 chains_static, u8 chains_dynamic,
178 u32 action, u32 apply_time)
179{
180 struct iwl_phy_context_cmd cmd;
181 int ret;
182
183
184 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
185
186
187 iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
188 chains_static, chains_dynamic);
189
190 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, CMD_SYNC,
191 sizeof(struct iwl_phy_context_cmd),
192 &cmd);
193 if (ret)
194 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
195 return ret;
196}
197
198
199
200
201int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
202 struct cfg80211_chan_def *chandef,
203 u8 chains_static, u8 chains_dynamic)
204{
205 int ret;
206
207 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
208 ctxt->ref);
209 lockdep_assert_held(&mvm->mutex);
210
211 ctxt->channel = chandef->chan;
212 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
213 chains_static, chains_dynamic,
214 FW_CTXT_ACTION_ADD, 0);
215
216 return ret;
217}
218
219
220
221
222
223void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
224{
225 lockdep_assert_held(&mvm->mutex);
226 ctxt->ref++;
227}
228
229
230
231
232
233
234int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
235 struct cfg80211_chan_def *chandef,
236 u8 chains_static, u8 chains_dynamic)
237{
238 lockdep_assert_held(&mvm->mutex);
239
240 ctxt->channel = chandef->chan;
241 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
242 chains_static, chains_dynamic,
243 FW_CTXT_ACTION_MODIFY, 0);
244}
245
246void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
247{
248 lockdep_assert_held(&mvm->mutex);
249
250 if (WARN_ON_ONCE(!ctxt))
251 return;
252
253 ctxt->ref--;
254}
255