blob: 6de0c6352943839b60985d5ec9543fc2be6f1699 [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 Berg8ca151b2013-01-24 14:25:36 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
64#include <net/mac80211.h>
65#include "fw-api.h"
66#include "mvm.h"
67
Johannes Berg6ca40d62013-12-18 15:56:28 +010068#define QUOTA_100 IWL_MVM_MAX_QUOTA
69#define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
70
Johannes Berg8ca151b2013-01-24 14:25:36 +010071struct iwl_mvm_quota_iterator_data {
72 int n_interfaces[MAX_BINDINGS];
73 int colors[MAX_BINDINGS];
Johannes Berg6ca40d62013-12-18 15:56:28 +010074 int low_latency[MAX_BINDINGS];
75 int n_low_latency_bindings;
Johannes Berg494983e2014-05-23 16:17:13 +020076 struct ieee80211_vif *skip_vif;
Johannes Berg8ca151b2013-01-24 14:25:36 +010077};
78
79static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
80 struct ieee80211_vif *vif)
81{
82 struct iwl_mvm_quota_iterator_data *data = _data;
83 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
84 u16 id;
85
86 /*
87 * We'll account for the new interface (if any) below,
88 * skip it here in case we're not called from within
89 * the add_interface callback (otherwise it won't show
90 * up in iteration)
Johannes Berg494983e2014-05-23 16:17:13 +020091 * Also skip disabled interfaces here immediately.
Johannes Berg8ca151b2013-01-24 14:25:36 +010092 */
Johannes Berg494983e2014-05-23 16:17:13 +020093 if (vif == data->skip_vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010094 return;
95
96 if (!mvmvif->phy_ctxt)
97 return;
98
99 /* currently, PHY ID == binding ID */
100 id = mvmvif->phy_ctxt->id;
101
102 /* need at least one binding per PHY */
103 BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
104
105 if (WARN_ON_ONCE(id >= MAX_BINDINGS))
106 return;
107
Johannes Berg8ca151b2013-01-24 14:25:36 +0100108 switch (vif->type) {
109 case NL80211_IFTYPE_STATION:
110 if (vif->bss_conf.assoc)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100111 break;
112 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100113 case NL80211_IFTYPE_AP:
Johannes Berg5023d962013-07-31 14:07:43 +0200114 case NL80211_IFTYPE_ADHOC:
115 if (mvmvif->ap_ibss_active)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100116 break;
117 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100118 case NL80211_IFTYPE_MONITOR:
Ilan Peer1e1391c2013-03-13 14:52:04 +0200119 if (mvmvif->monitor_active)
Johannes Berg6ca40d62013-12-18 15:56:28 +0100120 break;
121 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100122 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg6ca40d62013-12-18 15:56:28 +0100123 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100124 default:
125 WARN_ON_ONCE(1);
Johannes Berg6ca40d62013-12-18 15:56:28 +0100126 return;
127 }
128
Johannes Berg63faceb2014-05-23 16:15:11 +0200129 if (data->colors[id] < 0)
130 data->colors[id] = mvmvif->phy_ctxt->color;
131 else
132 WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
133
Johannes Berg6ca40d62013-12-18 15:56:28 +0100134 data->n_interfaces[id]++;
135
136 if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
137 data->n_low_latency_bindings++;
138 data->low_latency[id] = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100139 }
140}
141
David Spinadel507cadf2013-07-31 18:07:21 +0300142static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
143 struct iwl_time_quota_cmd *cmd)
144{
145#ifdef CONFIG_NL80211_TESTMODE
146 struct iwl_mvm_vif *mvmvif;
147 int i, phy_id = -1, beacon_int = 0;
148
149 if (!mvm->noa_duration || !mvm->noa_vif)
150 return;
151
152 mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
Johannes Berg5023d962013-07-31 14:07:43 +0200153 if (!mvmvif->ap_ibss_active)
David Spinadel507cadf2013-07-31 18:07:21 +0300154 return;
155
156 phy_id = mvmvif->phy_ctxt->id;
157 beacon_int = mvm->noa_vif->bss_conf.beacon_int;
158
159 for (i = 0; i < MAX_BINDINGS; i++) {
160 u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
161 u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
162 u32 quota = le32_to_cpu(cmd->quotas[i].quota);
163
164 if (id != phy_id)
165 continue;
166
David Spinadel246dd992013-10-17 09:49:12 +0300167 quota *= (beacon_int - mvm->noa_duration);
168 quota /= beacon_int;
David Spinadel507cadf2013-07-31 18:07:21 +0300169
170 cmd->quotas[i].quota = cpu_to_le32(quota);
171 }
172#endif
173}
174
Luciano Coelho63cbe182014-05-13 22:28:35 +0300175int iwl_mvm_update_quotas(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
176 enum iwl_mvm_quota_update_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100177{
Johannes Bergf5e45f22013-07-25 22:36:27 +0200178 struct iwl_time_quota_cmd cmd = {};
Johannes Berg6ca40d62013-12-18 15:56:28 +0100179 int i, idx, ret, 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 Berg494983e2014-05-23 16:17:13 +0200183 .skip_vif = vif,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100184 };
185
Johannes Bergf5e45f22013-07-25 22:36:27 +0200186 lockdep_assert_held(&mvm->mutex);
187
Johannes Berg8ca151b2013-01-24 14:25:36 +0100188 /* update all upon completion */
189 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
190 return 0;
191
Johannes Bergf5e45f22013-07-25 22:36:27 +0200192 /* iterator data above must match */
193 BUILD_BUG_ON(MAX_BINDINGS != 4);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100194
Luciano Coelho63cbe182014-05-13 22:28:35 +0300195 if (WARN_ON_ONCE((type != IWL_MVM_QUOTA_UPDATE_TYPE_REGULAR && !vif) ||
196 (type == IWL_MVM_QUOTA_UPDATE_TYPE_REGULAR && vif)))
197 return -EINVAL;
198
Johannes Berg8ca151b2013-01-24 14:25:36 +0100199 ieee80211_iterate_active_interfaces_atomic(
200 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
201 iwl_mvm_quota_iterator, &data);
Luciano Coelho63cbe182014-05-13 22:28:35 +0300202 if (type == IWL_MVM_QUOTA_UPDATE_TYPE_NEW) {
Johannes Berg494983e2014-05-23 16:17:13 +0200203 data.skip_vif = NULL;
Luciano Coelho63cbe182014-05-13 22:28:35 +0300204 iwl_mvm_quota_iterator(&data, vif->addr, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100205 }
206
Ilan Peer35adfd62013-02-04 13:16:24 +0200207 /*
208 * The FW's scheduling session consists of
209 * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
210 * equally between all the bindings that require quota
211 */
Johannes Berg7b8359c2013-07-10 12:59:38 +0200212 num_active_macs = 0;
Ilan Peer35adfd62013-02-04 13:16:24 +0200213 for (i = 0; i < MAX_BINDINGS; i++) {
214 cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
Johannes Berg7b8359c2013-07-10 12:59:38 +0200215 num_active_macs += data.n_interfaces[i];
Ilan Peer35adfd62013-02-04 13:16:24 +0200216 }
217
Johannes Berg6ca40d62013-12-18 15:56:28 +0100218 n_non_lowlat = num_active_macs;
219
220 if (data.n_low_latency_bindings == 1) {
221 for (i = 0; i < MAX_BINDINGS; i++) {
222 if (data.low_latency[i]) {
223 n_non_lowlat -= data.n_interfaces[i];
224 break;
225 }
226 }
Ilan Peer7b4fe062014-01-19 11:38:39 +0200227 }
228
229 if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
230 /*
231 * Reserve quota for the low latency binding in case that
232 * there are several data bindings but only a single
233 * low latency one. Split the rest of the quota equally
234 * between the other data interfaces.
235 */
236 quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
237 quota_rem = QUOTA_100 - n_non_lowlat * quota -
238 QUOTA_LOWLAT_MIN;
Johannes Berg6ca40d62013-12-18 15:56:28 +0100239 } else if (num_active_macs) {
Ilan Peer7b4fe062014-01-19 11:38:39 +0200240 /*
241 * There are 0 or more than 1 low latency bindings, or all the
242 * data interfaces belong to the single low latency binding.
243 * Split the quota equally between the data interfaces.
244 */
Johannes Berg6ca40d62013-12-18 15:56:28 +0100245 quota = QUOTA_100 / num_active_macs;
246 quota_rem = QUOTA_100 % num_active_macs;
247 } 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);
288 break;
289 }
290 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100291
David Spinadel507cadf2013-07-31 18:07:21 +0300292 iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
293
Johannes Berg99a12302014-05-23 16:13:57 +0200294 /* check that we have non-zero quota for all valid bindings */
295 for (i = 0; i < MAX_BINDINGS; i++) {
296 if (cmd.quotas[i].id_and_color == cpu_to_le32(FW_CTXT_INVALID))
297 continue;
298 WARN_ONCE(cmd.quotas[i].quota == 0,
299 "zero quota on binding %d\n", i);
300 }
301
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300302 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100303 sizeof(cmd), &cmd);
304 if (ret)
305 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
306 return ret;
307}