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
67
68#include <net/mac80211.h>
69#include "fw-api.h"
70#include "mvm.h"
71
72#define QUOTA_100 IWL_MVM_MAX_QUOTA
73#define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
74
75struct iwl_mvm_quota_iterator_data {
76 int n_interfaces[MAX_BINDINGS];
77 int colors[MAX_BINDINGS];
78 int low_latency[MAX_BINDINGS];
79#ifdef CONFIG_IWLWIFI_DEBUGFS
80 int dbgfs_min[MAX_BINDINGS];
81#endif
82 int n_low_latency_bindings;
83 struct ieee80211_vif *disabled_vif;
84};
85
86static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
87 struct ieee80211_vif *vif)
88{
89 struct iwl_mvm_quota_iterator_data *data = _data;
90 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
91 u16 id;
92
93
94 if (vif == data->disabled_vif)
95 return;
96
97 if (!mvmvif->phy_ctxt)
98 return;
99
100
101 id = mvmvif->phy_ctxt->id;
102
103
104 BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
105
106 if (WARN_ON_ONCE(id >= MAX_BINDINGS))
107 return;
108
109 switch (vif->type) {
110 case NL80211_IFTYPE_STATION:
111 if (vif->bss_conf.assoc)
112 break;
113 return;
114 case NL80211_IFTYPE_AP:
115 case NL80211_IFTYPE_ADHOC:
116 if (mvmvif->ap_ibss_active)
117 break;
118 return;
119 case NL80211_IFTYPE_MONITOR:
120 if (mvmvif->monitor_active)
121 break;
122 return;
123 case NL80211_IFTYPE_P2P_DEVICE:
124 return;
125 default:
126 WARN_ON_ONCE(1);
127 return;
128 }
129
130 if (data->colors[id] < 0)
131 data->colors[id] = mvmvif->phy_ctxt->color;
132 else
133 WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
134
135 data->n_interfaces[id]++;
136
137#ifdef CONFIG_IWLWIFI_DEBUGFS
138 if (mvmvif->dbgfs_quota_min)
139 data->dbgfs_min[id] = max(data->dbgfs_min[id],
140 mvmvif->dbgfs_quota_min);
141#endif
142
143 if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
144 data->n_low_latency_bindings++;
145 data->low_latency[id] = true;
146 }
147}
148
149static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
150 struct iwl_time_quota_cmd *cmd)
151{
152#ifdef CONFIG_NL80211_TESTMODE
153 struct iwl_mvm_vif *mvmvif;
154 int i, phy_id = -1, beacon_int = 0;
155
156 if (!mvm->noa_duration || !mvm->noa_vif)
157 return;
158
159 mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
160 if (!mvmvif->ap_ibss_active)
161 return;
162
163 phy_id = mvmvif->phy_ctxt->id;
164 beacon_int = mvm->noa_vif->bss_conf.beacon_int;
165
166 for (i = 0; i < MAX_BINDINGS; i++) {
167 struct iwl_time_quota_data *data =
168 iwl_mvm_quota_cmd_get_quota(mvm, cmd,
169 i);
170 u32 id_n_c = le32_to_cpu(data->id_and_color);
171 u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
172 u32 quota = le32_to_cpu(data->quota);
173
174 if (id != phy_id)
175 continue;
176
177 quota *= (beacon_int - mvm->noa_duration);
178 quota /= beacon_int;
179
180 IWL_DEBUG_QUOTA(mvm, "quota: adjust for NoA from %d to %d\n",
181 le32_to_cpu(data->quota), quota);
182
183 data->quota = cpu_to_le32(quota);
184 }
185#endif
186}
187
188int iwl_mvm_update_quotas(struct iwl_mvm *mvm,
189 bool force_update,
190 struct ieee80211_vif *disabled_vif)
191{
192 struct iwl_time_quota_cmd cmd = {};
193 int i, idx, err, num_active_macs, quota, quota_rem, n_non_lowlat;
194 struct iwl_mvm_quota_iterator_data data = {
195 .n_interfaces = {},
196 .colors = { -1, -1, -1, -1 },
197 .disabled_vif = disabled_vif,
198 };
199 struct iwl_time_quota_cmd *last = &mvm->last_quota_cmd;
200 struct iwl_time_quota_data *qdata, *last_data;
201 bool send = false;
202
203 lockdep_assert_held(&mvm->mutex);
204
205
206 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
207 return 0;
208
209
210 BUILD_BUG_ON(MAX_BINDINGS != 4);
211
212 ieee80211_iterate_active_interfaces_atomic(
213 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
214 iwl_mvm_quota_iterator, &data);
215
216
217
218
219
220
221 num_active_macs = 0;
222 for (i = 0; i < MAX_BINDINGS; i++) {
223 qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
224 qdata->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
225 num_active_macs += data.n_interfaces[i];
226 }
227
228 n_non_lowlat = num_active_macs;
229
230 if (data.n_low_latency_bindings == 1) {
231 for (i = 0; i < MAX_BINDINGS; i++) {
232 if (data.low_latency[i]) {
233 n_non_lowlat -= data.n_interfaces[i];
234 break;
235 }
236 }
237 }
238
239 if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
240
241
242
243
244
245
246 quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
247 quota_rem = QUOTA_100 - n_non_lowlat * quota -
248 QUOTA_LOWLAT_MIN;
249 IWL_DEBUG_QUOTA(mvm,
250 "quota: low-latency binding active, remaining quota per other binding: %d\n",
251 quota);
252 } else if (num_active_macs) {
253
254
255
256
257
258 quota = QUOTA_100 / num_active_macs;
259 quota_rem = QUOTA_100 % num_active_macs;
260 IWL_DEBUG_QUOTA(mvm,
261 "quota: splitting evenly per binding: %d\n",
262 quota);
263 } else {
264
265 quota = 0;
266 quota_rem = 0;
267 }
268
269 for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
270 if (data.colors[i] < 0)
271 continue;
272
273 qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, idx);
274
275 qdata->id_and_color =
276 cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
277
278 if (data.n_interfaces[i] <= 0)
279 qdata->quota = cpu_to_le32(0);
280#ifdef CONFIG_IWLWIFI_DEBUGFS
281 else if (data.dbgfs_min[i])
282 qdata->quota =
283 cpu_to_le32(data.dbgfs_min[i] * QUOTA_100 / 100);
284#endif
285 else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
286 data.low_latency[i])
287
288
289
290
291
292
293 qdata->quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
294 else
295 qdata->quota =
296 cpu_to_le32(quota * data.n_interfaces[i]);
297
298 WARN_ONCE(le32_to_cpu(qdata->quota) > QUOTA_100,
299 "Binding=%d, quota=%u > max=%u\n",
300 idx, le32_to_cpu(qdata->quota), QUOTA_100);
301
302 qdata->max_duration = cpu_to_le32(0);
303
304 idx++;
305 }
306
307
308 for (i = 0; i < MAX_BINDINGS; i++) {
309 qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
310 if (le32_to_cpu(qdata->quota) != 0) {
311 le32_add_cpu(&qdata->quota, quota_rem);
312 IWL_DEBUG_QUOTA(mvm,
313 "quota: giving remainder of %d to binding %d\n",
314 quota_rem, i);
315 break;
316 }
317 }
318
319 iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
320
321
322 for (i = 0; i < MAX_BINDINGS; i++) {
323 qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
324 last_data = iwl_mvm_quota_cmd_get_quota(mvm, last, i);
325 if (qdata->id_and_color != last_data->id_and_color)
326 send = true;
327 if (qdata->max_duration != last_data->max_duration)
328 send = true;
329 if (abs((int)le32_to_cpu(qdata->quota) -
330 (int)le32_to_cpu(last_data->quota))
331 > IWL_MVM_QUOTA_THRESHOLD)
332 send = true;
333 if (qdata->id_and_color == cpu_to_le32(FW_CTXT_INVALID))
334 continue;
335 WARN_ONCE(qdata->quota == 0,
336 "zero quota on binding %d\n", i);
337 }
338
339 if (!send && !force_update) {
340
341
342
343
344 return 0;
345 }
346
347 err = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
348 iwl_mvm_quota_cmd_size(mvm), &cmd);
349
350 if (err)
351 IWL_ERR(mvm, "Failed to send quota: %d\n", err);
352 else
353 mvm->last_quota_cmd = cmd;
354 return err;
355}
356