quota.c 9.9 KB

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