quota.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include <net/mac80211.h>
  64. #include "fw-api.h"
  65. #include "mvm.h"
  66. #define QUOTA_100 IWL_MVM_MAX_QUOTA
  67. #define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
  68. struct iwl_mvm_quota_iterator_data {
  69. int n_interfaces[MAX_BINDINGS];
  70. int colors[MAX_BINDINGS];
  71. int low_latency[MAX_BINDINGS];
  72. int n_low_latency_bindings;
  73. struct ieee80211_vif *new_vif;
  74. };
  75. static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
  76. struct ieee80211_vif *vif)
  77. {
  78. struct iwl_mvm_quota_iterator_data *data = _data;
  79. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  80. u16 id;
  81. /*
  82. * We'll account for the new interface (if any) below,
  83. * skip it here in case we're not called from within
  84. * the add_interface callback (otherwise it won't show
  85. * up in iteration)
  86. */
  87. if (vif == data->new_vif)
  88. return;
  89. if (!mvmvif->phy_ctxt)
  90. return;
  91. /* currently, PHY ID == binding ID */
  92. id = mvmvif->phy_ctxt->id;
  93. /* need at least one binding per PHY */
  94. BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
  95. if (WARN_ON_ONCE(id >= MAX_BINDINGS))
  96. return;
  97. if (data->colors[id] < 0)
  98. data->colors[id] = mvmvif->phy_ctxt->color;
  99. else
  100. WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
  101. switch (vif->type) {
  102. case NL80211_IFTYPE_STATION:
  103. if (vif->bss_conf.assoc)
  104. break;
  105. return;
  106. case NL80211_IFTYPE_AP:
  107. case NL80211_IFTYPE_ADHOC:
  108. if (mvmvif->ap_ibss_active)
  109. break;
  110. return;
  111. case NL80211_IFTYPE_MONITOR:
  112. if (mvmvif->monitor_active)
  113. break;
  114. return;
  115. case NL80211_IFTYPE_P2P_DEVICE:
  116. return;
  117. default:
  118. WARN_ON_ONCE(1);
  119. return;
  120. }
  121. data->n_interfaces[id]++;
  122. if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
  123. data->n_low_latency_bindings++;
  124. data->low_latency[id] = true;
  125. }
  126. }
  127. static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
  128. struct iwl_time_quota_cmd *cmd)
  129. {
  130. #ifdef CONFIG_NL80211_TESTMODE
  131. struct iwl_mvm_vif *mvmvif;
  132. int i, phy_id = -1, beacon_int = 0;
  133. if (!mvm->noa_duration || !mvm->noa_vif)
  134. return;
  135. mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
  136. if (!mvmvif->ap_ibss_active)
  137. return;
  138. phy_id = mvmvif->phy_ctxt->id;
  139. beacon_int = mvm->noa_vif->bss_conf.beacon_int;
  140. for (i = 0; i < MAX_BINDINGS; i++) {
  141. u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
  142. u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
  143. u32 quota = le32_to_cpu(cmd->quotas[i].quota);
  144. if (id != phy_id)
  145. continue;
  146. quota *= (beacon_int - mvm->noa_duration);
  147. quota /= beacon_int;
  148. cmd->quotas[i].quota = cpu_to_le32(quota);
  149. }
  150. #endif
  151. }
  152. int iwl_mvm_update_quotas(struct iwl_mvm *mvm, struct ieee80211_vif *newvif)
  153. {
  154. struct iwl_time_quota_cmd cmd = {};
  155. int i, idx, ret, num_active_macs, quota, quota_rem, n_non_lowlat;
  156. struct iwl_mvm_quota_iterator_data data = {
  157. .n_interfaces = {},
  158. .colors = { -1, -1, -1, -1 },
  159. .new_vif = newvif,
  160. };
  161. lockdep_assert_held(&mvm->mutex);
  162. /* update all upon completion */
  163. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  164. return 0;
  165. /* iterator data above must match */
  166. BUILD_BUG_ON(MAX_BINDINGS != 4);
  167. ieee80211_iterate_active_interfaces_atomic(
  168. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  169. iwl_mvm_quota_iterator, &data);
  170. if (newvif) {
  171. data.new_vif = NULL;
  172. iwl_mvm_quota_iterator(&data, newvif->addr, newvif);
  173. }
  174. /*
  175. * The FW's scheduling session consists of
  176. * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
  177. * equally between all the bindings that require quota
  178. */
  179. num_active_macs = 0;
  180. for (i = 0; i < MAX_BINDINGS; i++) {
  181. cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  182. num_active_macs += data.n_interfaces[i];
  183. }
  184. n_non_lowlat = num_active_macs;
  185. if (data.n_low_latency_bindings == 1) {
  186. for (i = 0; i < MAX_BINDINGS; i++) {
  187. if (data.low_latency[i]) {
  188. n_non_lowlat -= data.n_interfaces[i];
  189. break;
  190. }
  191. }
  192. }
  193. if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
  194. /*
  195. * Reserve quota for the low latency binding in case that
  196. * there are several data bindings but only a single
  197. * low latency one. Split the rest of the quota equally
  198. * between the other data interfaces.
  199. */
  200. quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
  201. quota_rem = QUOTA_100 - n_non_lowlat * quota -
  202. QUOTA_LOWLAT_MIN;
  203. } else if (num_active_macs) {
  204. /*
  205. * There are 0 or more than 1 low latency bindings, or all the
  206. * data interfaces belong to the single low latency binding.
  207. * Split the quota equally between the data interfaces.
  208. */
  209. quota = QUOTA_100 / num_active_macs;
  210. quota_rem = QUOTA_100 % num_active_macs;
  211. } else {
  212. /* values don't really matter - won't be used */
  213. quota = 0;
  214. quota_rem = 0;
  215. }
  216. for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
  217. if (data.colors[i] < 0)
  218. continue;
  219. cmd.quotas[idx].id_and_color =
  220. cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
  221. if (data.n_interfaces[i] <= 0)
  222. cmd.quotas[idx].quota = cpu_to_le32(0);
  223. else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
  224. data.low_latency[i])
  225. /*
  226. * There is more than one binding, but only one of the
  227. * bindings is in low latency. For this case, allocate
  228. * the minimal required quota for the low latency
  229. * binding.
  230. */
  231. cmd.quotas[idx].quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
  232. else
  233. cmd.quotas[idx].quota =
  234. cpu_to_le32(quota * data.n_interfaces[i]);
  235. WARN_ONCE(le32_to_cpu(cmd.quotas[idx].quota) > QUOTA_100,
  236. "Binding=%d, quota=%u > max=%u\n",
  237. idx, le32_to_cpu(cmd.quotas[idx].quota), QUOTA_100);
  238. cmd.quotas[idx].max_duration = cpu_to_le32(0);
  239. idx++;
  240. }
  241. /* Give the remainder of the session to the first data binding */
  242. for (i = 0; i < MAX_BINDINGS; i++) {
  243. if (le32_to_cpu(cmd.quotas[i].quota) != 0) {
  244. le32_add_cpu(&cmd.quotas[i].quota, quota_rem);
  245. break;
  246. }
  247. }
  248. iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
  249. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  250. sizeof(cmd), &cmd);
  251. if (ret)
  252. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  253. return ret;
  254. }