blob: 509a66d05245bb37ab9deb7391530297181be0c7 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <net/mac80211.h>
67#include "fw-api.h"
68#include "mvm.h"
69
Johannes Berg6ca40d62013-12-18 15:56:28 +010070#define QUOTA_100 IWL_MVM_MAX_QUOTA
71#define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
72
Johannes Berg8ca151b2013-01-24 14:25:36 +010073struct iwl_mvm_quota_iterator_data {
74 int n_interfaces[MAX_BINDINGS];
75 int colors[MAX_BINDINGS];
Johannes Berg6ca40d62013-12-18 15:56:28 +010076 int low_latency[MAX_BINDINGS];
77 int n_low_latency_bindings;
Johannes Berg01662302014-06-06 15:18:45 +020078 struct ieee80211_vif *disabled_vif;
Johannes Berg8ca151b2013-01-24 14:25:36 +010079};
80
81static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
82 struct ieee80211_vif *vif)
83{
84 struct iwl_mvm_quota_iterator_data *data = _data;
85 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
86 u16 id;
87
Johannes Berg01662302014-06-06 15:18:45 +020088 /* skip disabled interfaces here immediately */
89 if (vif == data->disabled_vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010090 return;
91
92 if (!mvmvif->phy_ctxt)
93 return;
94
95 /* currently, PHY ID == binding ID */
96 id = mvmvif->phy_ctxt->id;
97
98 /* need at least one binding per PHY */
99 BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
100
101 if (WARN_ON_ONCE(id >= MAX_BINDINGS))
102 return;
103
Johannes Berg8ca151b2013-01-24 14:25:36 +0100104 switch (vif->type) {
105 case NL80211_IFTYPE_STATION:
106 if (vif->bss_conf.assoc)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100107 break;
108 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109 case NL80211_IFTYPE_AP:
Johannes Berg5023d962013-07-31 14:07:43 +0200110 case NL80211_IFTYPE_ADHOC:
111 if (mvmvif->ap_ibss_active)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100112 break;
113 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100114 case NL80211_IFTYPE_MONITOR:
Ilan Peer1e1391c2013-03-13 14:52:04 +0200115 if (mvmvif->monitor_active)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100116 break;
117 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100118 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg6ca40d62013-12-18 15:56:28 +0100119 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100120 default:
121 WARN_ON_ONCE(1);
Johannes Berg6ca40d62013-12-18 15:56:28 +0100122 return;
123 }
124
Johannes Berg63faceb2014-05-23 16:15:11 +0200125 if (data->colors[id] < 0)
126 data->colors[id] = mvmvif->phy_ctxt->color;
127 else
128 WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
129
Johannes Berg6ca40d62013-12-18 15:56:28 +0100130 data->n_interfaces[id]++;
131
132 if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
133 data->n_low_latency_bindings++;
134 data->low_latency[id] = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100135 }
136}
137
David Spinadel507cadf2013-07-31 18:07:21 +0300138static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
139 struct iwl_time_quota_cmd *cmd)
140{
141#ifdef CONFIG_NL80211_TESTMODE
142 struct iwl_mvm_vif *mvmvif;
143 int i, phy_id = -1, beacon_int = 0;
144
145 if (!mvm->noa_duration || !mvm->noa_vif)
146 return;
147
148 mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
Johannes Berg5023d962013-07-31 14:07:43 +0200149 if (!mvmvif->ap_ibss_active)
David Spinadel507cadf2013-07-31 18:07:21 +0300150 return;
151
152 phy_id = mvmvif->phy_ctxt->id;
153 beacon_int = mvm->noa_vif->bss_conf.beacon_int;
154
155 for (i = 0; i < MAX_BINDINGS; i++) {
156 u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
157 u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
158 u32 quota = le32_to_cpu(cmd->quotas[i].quota);
159
160 if (id != phy_id)
161 continue;
162
David Spinadel246dd992013-10-17 09:49:12 +0300163 quota *= (beacon_int - mvm->noa_duration);
164 quota /= beacon_int;
David Spinadel507cadf2013-07-31 18:07:21 +0300165
Johannes Berga43ad462014-08-04 16:39:54 +0200166 IWL_DEBUG_QUOTA(mvm, "quota: adjust for NoA from %d to %d\n",
167 le32_to_cpu(cmd->quotas[i].quota), quota);
168
David Spinadel507cadf2013-07-31 18:07:21 +0300169 cmd->quotas[i].quota = cpu_to_le32(quota);
170 }
171#endif
172}
173
Johannes Berg01662302014-06-06 15:18:45 +0200174int iwl_mvm_update_quotas(struct iwl_mvm *mvm,
Emmanuel Grumbach7754ae72015-02-26 15:14:35 +0200175 bool force_update,
Johannes Berg01662302014-06-06 15:18:45 +0200176 struct ieee80211_vif *disabled_vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100177{
Johannes Bergf5e45f22013-07-25 22:36:27 +0200178 struct iwl_time_quota_cmd cmd = {};
Johannes Bergb2b78752014-09-08 16:42:54 +0200179 int i, idx, err, num_active_macs, quota, quota_rem, n_non_lowlat;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100180 struct iwl_mvm_quota_iterator_data data = {
181 .n_interfaces = {},
182 .colors = { -1, -1, -1, -1 },
Johannes Berg01662302014-06-06 15:18:45 +0200183 .disabled_vif = disabled_vif,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100184 };
Johannes Bergb2b78752014-09-08 16:42:54 +0200185 struct iwl_time_quota_cmd *last = &mvm->last_quota_cmd;
186 bool send = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100187
Johannes Bergf5e45f22013-07-25 22:36:27 +0200188 lockdep_assert_held(&mvm->mutex);
189
Johannes Berg8ca151b2013-01-24 14:25:36 +0100190 /* update all upon completion */
191 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
192 return 0;
193
Johannes Bergf5e45f22013-07-25 22:36:27 +0200194 /* iterator data above must match */
195 BUILD_BUG_ON(MAX_BINDINGS != 4);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100196
197 ieee80211_iterate_active_interfaces_atomic(
198 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
199 iwl_mvm_quota_iterator, &data);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100200
Ilan Peer35adfd62013-02-04 13:16:24 +0200201 /*
202 * The FW's scheduling session consists of
203 * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
204 * equally between all the bindings that require quota
205 */
Johannes Berg7b8359c2013-07-10 12:59:38 +0200206 num_active_macs = 0;
Ilan Peer35adfd62013-02-04 13:16:24 +0200207 for (i = 0; i < MAX_BINDINGS; i++) {
208 cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
Johannes Berg7b8359c2013-07-10 12:59:38 +0200209 num_active_macs += data.n_interfaces[i];
Ilan Peer35adfd62013-02-04 13:16:24 +0200210 }
211
Johannes Berg6ca40d62013-12-18 15:56:28 +0100212 n_non_lowlat = num_active_macs;
213
214 if (data.n_low_latency_bindings == 1) {
215 for (i = 0; i < MAX_BINDINGS; i++) {
216 if (data.low_latency[i]) {
217 n_non_lowlat -= data.n_interfaces[i];
218 break;
219 }
220 }
Ilan Peer7b4fe062014-01-19 11:38:39 +0200221 }
222
223 if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
224 /*
225 * Reserve quota for the low latency binding in case that
226 * there are several data bindings but only a single
227 * low latency one. Split the rest of the quota equally
228 * between the other data interfaces.
229 */
230 quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
231 quota_rem = QUOTA_100 - n_non_lowlat * quota -
232 QUOTA_LOWLAT_MIN;
Johannes Berga43ad462014-08-04 16:39:54 +0200233 IWL_DEBUG_QUOTA(mvm,
234 "quota: low-latency binding active, remaining quota per other binding: %d\n",
235 quota);
Johannes Berg6ca40d62013-12-18 15:56:28 +0100236 } else if (num_active_macs) {
Ilan Peer7b4fe062014-01-19 11:38:39 +0200237 /*
238 * There are 0 or more than 1 low latency bindings, or all the
239 * data interfaces belong to the single low latency binding.
240 * Split the quota equally between the data interfaces.
241 */
Johannes Berg6ca40d62013-12-18 15:56:28 +0100242 quota = QUOTA_100 / num_active_macs;
243 quota_rem = QUOTA_100 % num_active_macs;
Johannes Berga43ad462014-08-04 16:39:54 +0200244 IWL_DEBUG_QUOTA(mvm,
245 "quota: splitting evenly per binding: %d\n",
246 quota);
Johannes Berg6ca40d62013-12-18 15:56:28 +0100247 } else {
248 /* values don't really matter - won't be used */
249 quota = 0;
250 quota_rem = 0;
Ilan Peer8d683b72013-05-30 07:31:01 +0300251 }
Ilan Peer35adfd62013-02-04 13:16:24 +0200252
Johannes Berg8ca151b2013-01-24 14:25:36 +0100253 for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
Ilan Peer8d683b72013-05-30 07:31:01 +0300254 if (data.colors[i] < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100255 continue;
256
257 cmd.quotas[idx].id_and_color =
258 cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
Ilan Peer8d683b72013-05-30 07:31:01 +0300259
Johannes Berg1fb184b2013-11-12 22:18:45 +0100260 if (data.n_interfaces[i] <= 0)
Ilan Peer8d683b72013-05-30 07:31:01 +0300261 cmd.quotas[idx].quota = cpu_to_le32(0);
Johannes Berg1fb184b2013-11-12 22:18:45 +0100262 else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
263 data.low_latency[i])
Ilan Peer7b4fe062014-01-19 11:38:39 +0200264 /*
265 * There is more than one binding, but only one of the
266 * bindings is in low latency. For this case, allocate
267 * the minimal required quota for the low latency
268 * binding.
269 */
Johannes Berg6ca40d62013-12-18 15:56:28 +0100270 cmd.quotas[idx].quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
Johannes Berg1fb184b2013-11-12 22:18:45 +0100271 else
Johannes Berg7b8359c2013-07-10 12:59:38 +0200272 cmd.quotas[idx].quota =
273 cpu_to_le32(quota * data.n_interfaces[i]);
Johannes Berg1fb184b2013-11-12 22:18:45 +0100274
Ilan Peer7b4fe062014-01-19 11:38:39 +0200275 WARN_ONCE(le32_to_cpu(cmd.quotas[idx].quota) > QUOTA_100,
276 "Binding=%d, quota=%u > max=%u\n",
277 idx, le32_to_cpu(cmd.quotas[idx].quota), QUOTA_100);
278
Johannes Berg65d66622014-03-17 09:34:29 +0100279 cmd.quotas[idx].max_duration = cpu_to_le32(0);
Johannes Berg1fb184b2013-11-12 22:18:45 +0100280
Johannes Berg8ca151b2013-01-24 14:25:36 +0100281 idx++;
282 }
283
Ilan Peer2d675e52014-01-19 11:46:52 +0200284 /* Give the remainder of the session to the first data binding */
285 for (i = 0; i < MAX_BINDINGS; i++) {
286 if (le32_to_cpu(cmd.quotas[i].quota) != 0) {
287 le32_add_cpu(&cmd.quotas[i].quota, quota_rem);
Johannes Berga43ad462014-08-04 16:39:54 +0200288 IWL_DEBUG_QUOTA(mvm,
289 "quota: giving remainder of %d to binding %d\n",
290 quota_rem, i);
Ilan Peer2d675e52014-01-19 11:46:52 +0200291 break;
292 }
293 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100294
David Spinadel507cadf2013-07-31 18:07:21 +0300295 iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
296
Johannes Berg99a12302014-05-23 16:13:57 +0200297 /* check that we have non-zero quota for all valid bindings */
298 for (i = 0; i < MAX_BINDINGS; i++) {
Johannes Bergb2b78752014-09-08 16:42:54 +0200299 if (cmd.quotas[i].id_and_color != last->quotas[i].id_and_color)
300 send = true;
301 if (cmd.quotas[i].max_duration != last->quotas[i].max_duration)
302 send = true;
303 if (abs((int)le32_to_cpu(cmd.quotas[i].quota) -
304 (int)le32_to_cpu(last->quotas[i].quota))
305 > IWL_MVM_QUOTA_THRESHOLD)
306 send = true;
Johannes Berg99a12302014-05-23 16:13:57 +0200307 if (cmd.quotas[i].id_and_color == cpu_to_le32(FW_CTXT_INVALID))
308 continue;
309 WARN_ONCE(cmd.quotas[i].quota == 0,
310 "zero quota on binding %d\n", i);
311 }
312
Emmanuel Grumbach7754ae72015-02-26 15:14:35 +0200313 if (!send && !force_update) {
Johannes Bergb2b78752014-09-08 16:42:54 +0200314 /* don't send a practically unchanged command, the firmware has
315 * to re-initialize a lot of state and that can have an adverse
316 * impact on it
317 */
Johannes Berg3d4060d2014-09-11 14:12:06 +0200318 return 0;
Johannes Bergb2b78752014-09-08 16:42:54 +0200319 }
320
Johannes Berg3d4060d2014-09-11 14:12:06 +0200321 err = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0, sizeof(cmd), &cmd);
322
Johannes Bergb2b78752014-09-08 16:42:54 +0200323 if (err)
324 IWL_ERR(mvm, "Failed to send quota: %d\n", err);
325 else
326 mvm->last_quota_cmd = cmd;
327 return err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100328}