quota.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 - 2013 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 - 2013 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. struct iwl_mvm_quota_iterator_data {
  67. int n_interfaces[MAX_BINDINGS];
  68. int colors[MAX_BINDINGS];
  69. struct ieee80211_vif *new_vif;
  70. };
  71. static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
  72. struct ieee80211_vif *vif)
  73. {
  74. struct iwl_mvm_quota_iterator_data *data = _data;
  75. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  76. u16 id;
  77. /*
  78. * We'll account for the new interface (if any) below,
  79. * skip it here in case we're not called from within
  80. * the add_interface callback (otherwise it won't show
  81. * up in iteration)
  82. */
  83. if (vif == data->new_vif)
  84. return;
  85. if (!mvmvif->phy_ctxt)
  86. return;
  87. /* currently, PHY ID == binding ID */
  88. id = mvmvif->phy_ctxt->id;
  89. /* need at least one binding per PHY */
  90. BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
  91. if (WARN_ON_ONCE(id >= MAX_BINDINGS))
  92. return;
  93. if (data->colors[id] < 0)
  94. data->colors[id] = mvmvif->phy_ctxt->color;
  95. else
  96. WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
  97. switch (vif->type) {
  98. case NL80211_IFTYPE_STATION:
  99. if (vif->bss_conf.assoc)
  100. data->n_interfaces[id]++;
  101. break;
  102. case NL80211_IFTYPE_AP:
  103. case NL80211_IFTYPE_ADHOC:
  104. if (mvmvif->ap_ibss_active)
  105. data->n_interfaces[id]++;
  106. break;
  107. case NL80211_IFTYPE_MONITOR:
  108. if (mvmvif->monitor_active)
  109. data->n_interfaces[id]++;
  110. break;
  111. case NL80211_IFTYPE_P2P_DEVICE:
  112. break;
  113. default:
  114. WARN_ON_ONCE(1);
  115. break;
  116. }
  117. }
  118. static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
  119. struct iwl_time_quota_cmd *cmd)
  120. {
  121. #ifdef CONFIG_NL80211_TESTMODE
  122. struct iwl_mvm_vif *mvmvif;
  123. int i, phy_id = -1, beacon_int = 0;
  124. if (!mvm->noa_duration || !mvm->noa_vif)
  125. return;
  126. mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
  127. if (!mvmvif->ap_ibss_active)
  128. return;
  129. phy_id = mvmvif->phy_ctxt->id;
  130. beacon_int = mvm->noa_vif->bss_conf.beacon_int;
  131. for (i = 0; i < MAX_BINDINGS; i++) {
  132. u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
  133. u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
  134. u32 quota = le32_to_cpu(cmd->quotas[i].quota);
  135. if (id != phy_id)
  136. continue;
  137. quota *= (beacon_int - mvm->noa_duration);
  138. quota /= beacon_int;
  139. cmd->quotas[i].quota = cpu_to_le32(quota);
  140. }
  141. #endif
  142. }
  143. int iwl_mvm_update_quotas(struct iwl_mvm *mvm, struct ieee80211_vif *newvif)
  144. {
  145. struct iwl_time_quota_cmd cmd = {};
  146. int i, idx, ret, num_active_macs, quota, quota_rem;
  147. struct iwl_mvm_quota_iterator_data data = {
  148. .n_interfaces = {},
  149. .colors = { -1, -1, -1, -1 },
  150. .new_vif = newvif,
  151. };
  152. lockdep_assert_held(&mvm->mutex);
  153. /* update all upon completion */
  154. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  155. return 0;
  156. /* iterator data above must match */
  157. BUILD_BUG_ON(MAX_BINDINGS != 4);
  158. ieee80211_iterate_active_interfaces_atomic(
  159. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  160. iwl_mvm_quota_iterator, &data);
  161. if (newvif) {
  162. data.new_vif = NULL;
  163. iwl_mvm_quota_iterator(&data, newvif->addr, newvif);
  164. }
  165. /*
  166. * The FW's scheduling session consists of
  167. * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
  168. * equally between all the bindings that require quota
  169. */
  170. num_active_macs = 0;
  171. for (i = 0; i < MAX_BINDINGS; i++) {
  172. cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  173. num_active_macs += data.n_interfaces[i];
  174. }
  175. quota = 0;
  176. quota_rem = 0;
  177. if (num_active_macs) {
  178. quota = IWL_MVM_MAX_QUOTA / num_active_macs;
  179. quota_rem = IWL_MVM_MAX_QUOTA % num_active_macs;
  180. }
  181. for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
  182. if (data.colors[i] < 0)
  183. continue;
  184. cmd.quotas[idx].id_and_color =
  185. cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
  186. if (data.n_interfaces[i] <= 0) {
  187. cmd.quotas[idx].quota = cpu_to_le32(0);
  188. cmd.quotas[idx].max_duration = cpu_to_le32(0);
  189. } else {
  190. cmd.quotas[idx].quota =
  191. cpu_to_le32(quota * data.n_interfaces[i]);
  192. cmd.quotas[idx].max_duration =
  193. cpu_to_le32(IWL_MVM_MAX_QUOTA);
  194. }
  195. idx++;
  196. }
  197. /* Give the remainder of the session to the first binding */
  198. le32_add_cpu(&cmd.quotas[0].quota, quota_rem);
  199. iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
  200. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  201. sizeof(cmd), &cmd);
  202. if (ret)
  203. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  204. return ret;
  205. }