Merge branch 'next' of git://git.infradead.org/users/pcmoore/selinux into next
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / quota.c
1 /******************************************************************************
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  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  *
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
25  * in the file called COPYING.
26  *
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  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * 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
68 #define QUOTA_100       IWL_MVM_MAX_QUOTA
69 #define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
70
71 struct iwl_mvm_quota_iterator_data {
72         int n_interfaces[MAX_BINDINGS];
73         int colors[MAX_BINDINGS];
74         int low_latency[MAX_BINDINGS];
75         int n_low_latency_bindings;
76         struct ieee80211_vif *disabled_vif;
77 };
78
79 static 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         /* skip disabled interfaces here immediately */
87         if (vif == data->disabled_vif)
88                 return;
89
90         if (!mvmvif->phy_ctxt)
91                 return;
92
93         /* currently, PHY ID == binding ID */
94         id = mvmvif->phy_ctxt->id;
95
96         /* need at least one binding per PHY */
97         BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
98
99         if (WARN_ON_ONCE(id >= MAX_BINDINGS))
100                 return;
101
102         switch (vif->type) {
103         case NL80211_IFTYPE_STATION:
104                 if (vif->bss_conf.assoc)
105                         break;
106                 return;
107         case NL80211_IFTYPE_AP:
108         case NL80211_IFTYPE_ADHOC:
109                 if (mvmvif->ap_ibss_active)
110                         break;
111                 return;
112         case NL80211_IFTYPE_MONITOR:
113                 if (mvmvif->monitor_active)
114                         break;
115                 return;
116         case NL80211_IFTYPE_P2P_DEVICE:
117                 return;
118         default:
119                 WARN_ON_ONCE(1);
120                 return;
121         }
122
123         if (data->colors[id] < 0)
124                 data->colors[id] = mvmvif->phy_ctxt->color;
125         else
126                 WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
127
128         data->n_interfaces[id]++;
129
130         if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
131                 data->n_low_latency_bindings++;
132                 data->low_latency[id] = true;
133         }
134 }
135
136 static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
137                                          struct iwl_time_quota_cmd *cmd)
138 {
139 #ifdef CONFIG_NL80211_TESTMODE
140         struct iwl_mvm_vif *mvmvif;
141         int i, phy_id = -1, beacon_int = 0;
142
143         if (!mvm->noa_duration || !mvm->noa_vif)
144                 return;
145
146         mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
147         if (!mvmvif->ap_ibss_active)
148                 return;
149
150         phy_id = mvmvif->phy_ctxt->id;
151         beacon_int = mvm->noa_vif->bss_conf.beacon_int;
152
153         for (i = 0; i < MAX_BINDINGS; i++) {
154                 u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
155                 u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
156                 u32 quota = le32_to_cpu(cmd->quotas[i].quota);
157
158                 if (id != phy_id)
159                         continue;
160
161                 quota *= (beacon_int - mvm->noa_duration);
162                 quota /= beacon_int;
163
164                 cmd->quotas[i].quota = cpu_to_le32(quota);
165         }
166 #endif
167 }
168
169 int iwl_mvm_update_quotas(struct iwl_mvm *mvm,
170                           struct ieee80211_vif *disabled_vif)
171 {
172         struct iwl_time_quota_cmd cmd = {};
173         int i, idx, ret, num_active_macs, quota, quota_rem, n_non_lowlat;
174         struct iwl_mvm_quota_iterator_data data = {
175                 .n_interfaces = {},
176                 .colors = { -1, -1, -1, -1 },
177                 .disabled_vif = disabled_vif,
178         };
179
180         lockdep_assert_held(&mvm->mutex);
181
182         /* update all upon completion */
183         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
184                 return 0;
185
186         /* iterator data above must match */
187         BUILD_BUG_ON(MAX_BINDINGS != 4);
188
189         ieee80211_iterate_active_interfaces_atomic(
190                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
191                 iwl_mvm_quota_iterator, &data);
192
193         /*
194          * The FW's scheduling session consists of
195          * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
196          * equally between all the bindings that require quota
197          */
198         num_active_macs = 0;
199         for (i = 0; i < MAX_BINDINGS; i++) {
200                 cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
201                 num_active_macs += data.n_interfaces[i];
202         }
203
204         n_non_lowlat = num_active_macs;
205
206         if (data.n_low_latency_bindings == 1) {
207                 for (i = 0; i < MAX_BINDINGS; i++) {
208                         if (data.low_latency[i]) {
209                                 n_non_lowlat -= data.n_interfaces[i];
210                                 break;
211                         }
212                 }
213         }
214
215         if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
216                 /*
217                  * Reserve quota for the low latency binding in case that
218                  * there are several data bindings but only a single
219                  * low latency one. Split the rest of the quota equally
220                  * between the other data interfaces.
221                  */
222                 quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
223                 quota_rem = QUOTA_100 - n_non_lowlat * quota -
224                             QUOTA_LOWLAT_MIN;
225         } else if (num_active_macs) {
226                 /*
227                  * There are 0 or more than 1 low latency bindings, or all the
228                  * data interfaces belong to the single low latency binding.
229                  * Split the quota equally between the data interfaces.
230                  */
231                 quota = QUOTA_100 / num_active_macs;
232                 quota_rem = QUOTA_100 % num_active_macs;
233         } else {
234                 /* values don't really matter - won't be used */
235                 quota = 0;
236                 quota_rem = 0;
237         }
238
239         for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
240                 if (data.colors[i] < 0)
241                         continue;
242
243                 cmd.quotas[idx].id_and_color =
244                         cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
245
246                 if (data.n_interfaces[i] <= 0)
247                         cmd.quotas[idx].quota = cpu_to_le32(0);
248                 else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
249                          data.low_latency[i])
250                         /*
251                          * There is more than one binding, but only one of the
252                          * bindings is in low latency. For this case, allocate
253                          * the minimal required quota for the low latency
254                          * binding.
255                          */
256                         cmd.quotas[idx].quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
257                 else
258                         cmd.quotas[idx].quota =
259                                 cpu_to_le32(quota * data.n_interfaces[i]);
260
261                 WARN_ONCE(le32_to_cpu(cmd.quotas[idx].quota) > QUOTA_100,
262                           "Binding=%d, quota=%u > max=%u\n",
263                           idx, le32_to_cpu(cmd.quotas[idx].quota), QUOTA_100);
264
265                 cmd.quotas[idx].max_duration = cpu_to_le32(0);
266
267                 idx++;
268         }
269
270         /* Give the remainder of the session to the first data binding */
271         for (i = 0; i < MAX_BINDINGS; i++) {
272                 if (le32_to_cpu(cmd.quotas[i].quota) != 0) {
273                         le32_add_cpu(&cmd.quotas[i].quota, quota_rem);
274                         break;
275                 }
276         }
277
278         iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
279
280         /* check that we have non-zero quota for all valid bindings */
281         for (i = 0; i < MAX_BINDINGS; i++) {
282                 if (cmd.quotas[i].id_and_color == cpu_to_le32(FW_CTXT_INVALID))
283                         continue;
284                 WARN_ONCE(cmd.quotas[i].quota == 0,
285                           "zero quota on binding %d\n", i);
286         }
287
288         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
289                                    sizeof(cmd), &cmd);
290         if (ret)
291                 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
292         return ret;
293 }