quota.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <net/mac80211.h>
  68. #include "fw-api.h"
  69. #include "mvm.h"
  70. #define QUOTA_100 IWL_MVM_MAX_QUOTA
  71. #define QUOTA_LOWLAT_MIN ((QUOTA_100 * IWL_MVM_LOWLAT_QUOTA_MIN_PERCENT) / 100)
  72. struct iwl_mvm_quota_iterator_data {
  73. int n_interfaces[MAX_BINDINGS];
  74. int colors[MAX_BINDINGS];
  75. int low_latency[MAX_BINDINGS];
  76. #ifdef CONFIG_IWLWIFI_DEBUGFS
  77. int dbgfs_min[MAX_BINDINGS];
  78. #endif
  79. int n_low_latency_bindings;
  80. struct ieee80211_vif *disabled_vif;
  81. };
  82. static void iwl_mvm_quota_iterator(void *_data, u8 *mac,
  83. struct ieee80211_vif *vif)
  84. {
  85. struct iwl_mvm_quota_iterator_data *data = _data;
  86. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  87. u16 id;
  88. /* skip disabled interfaces here immediately */
  89. if (vif == data->disabled_vif)
  90. return;
  91. if (!mvmvif->phy_ctxt)
  92. return;
  93. /* currently, PHY ID == binding ID */
  94. id = mvmvif->phy_ctxt->id;
  95. /* need at least one binding per PHY */
  96. BUILD_BUG_ON(NUM_PHY_CTX > MAX_BINDINGS);
  97. if (WARN_ON_ONCE(id >= MAX_BINDINGS))
  98. return;
  99. switch (vif->type) {
  100. case NL80211_IFTYPE_STATION:
  101. if (vif->bss_conf.assoc)
  102. break;
  103. return;
  104. case NL80211_IFTYPE_AP:
  105. case NL80211_IFTYPE_ADHOC:
  106. if (mvmvif->ap_ibss_active)
  107. break;
  108. return;
  109. case NL80211_IFTYPE_MONITOR:
  110. if (mvmvif->monitor_active)
  111. break;
  112. return;
  113. case NL80211_IFTYPE_P2P_DEVICE:
  114. return;
  115. default:
  116. WARN_ON_ONCE(1);
  117. return;
  118. }
  119. if (data->colors[id] < 0)
  120. data->colors[id] = mvmvif->phy_ctxt->color;
  121. else
  122. WARN_ON_ONCE(data->colors[id] != mvmvif->phy_ctxt->color);
  123. data->n_interfaces[id]++;
  124. #ifdef CONFIG_IWLWIFI_DEBUGFS
  125. if (mvmvif->dbgfs_quota_min)
  126. data->dbgfs_min[id] = max(data->dbgfs_min[id],
  127. mvmvif->dbgfs_quota_min);
  128. #endif
  129. if (iwl_mvm_vif_low_latency(mvmvif) && !data->low_latency[id]) {
  130. data->n_low_latency_bindings++;
  131. data->low_latency[id] = true;
  132. }
  133. }
  134. static void iwl_mvm_adjust_quota_for_noa(struct iwl_mvm *mvm,
  135. struct iwl_time_quota_cmd *cmd)
  136. {
  137. #ifdef CONFIG_NL80211_TESTMODE
  138. struct iwl_mvm_vif *mvmvif;
  139. int i, phy_id = -1, beacon_int = 0;
  140. if (!mvm->noa_duration || !mvm->noa_vif)
  141. return;
  142. mvmvif = iwl_mvm_vif_from_mac80211(mvm->noa_vif);
  143. if (!mvmvif->ap_ibss_active)
  144. return;
  145. phy_id = mvmvif->phy_ctxt->id;
  146. beacon_int = mvm->noa_vif->bss_conf.beacon_int;
  147. for (i = 0; i < MAX_BINDINGS; i++) {
  148. u32 id_n_c = le32_to_cpu(cmd->quotas[i].id_and_color);
  149. u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
  150. u32 quota = le32_to_cpu(cmd->quotas[i].quota);
  151. if (id != phy_id)
  152. continue;
  153. quota *= (beacon_int - mvm->noa_duration);
  154. quota /= beacon_int;
  155. IWL_DEBUG_QUOTA(mvm, "quota: adjust for NoA from %d to %d\n",
  156. le32_to_cpu(cmd->quotas[i].quota), quota);
  157. cmd->quotas[i].quota = cpu_to_le32(quota);
  158. }
  159. #endif
  160. }
  161. int iwl_mvm_update_quotas(struct iwl_mvm *mvm,
  162. bool force_update,
  163. struct ieee80211_vif *disabled_vif)
  164. {
  165. struct iwl_time_quota_cmd cmd = {};
  166. int i, idx, err, num_active_macs, quota, quota_rem, n_non_lowlat;
  167. struct iwl_mvm_quota_iterator_data data = {
  168. .n_interfaces = {},
  169. .colors = { -1, -1, -1, -1 },
  170. .disabled_vif = disabled_vif,
  171. };
  172. struct iwl_time_quota_cmd *last = &mvm->last_quota_cmd;
  173. bool send = false;
  174. lockdep_assert_held(&mvm->mutex);
  175. /* update all upon completion */
  176. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  177. return 0;
  178. /* iterator data above must match */
  179. BUILD_BUG_ON(MAX_BINDINGS != 4);
  180. ieee80211_iterate_active_interfaces_atomic(
  181. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  182. iwl_mvm_quota_iterator, &data);
  183. /*
  184. * The FW's scheduling session consists of
  185. * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
  186. * equally between all the bindings that require quota
  187. */
  188. num_active_macs = 0;
  189. for (i = 0; i < MAX_BINDINGS; i++) {
  190. cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  191. num_active_macs += data.n_interfaces[i];
  192. }
  193. n_non_lowlat = num_active_macs;
  194. if (data.n_low_latency_bindings == 1) {
  195. for (i = 0; i < MAX_BINDINGS; i++) {
  196. if (data.low_latency[i]) {
  197. n_non_lowlat -= data.n_interfaces[i];
  198. break;
  199. }
  200. }
  201. }
  202. if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
  203. /*
  204. * Reserve quota for the low latency binding in case that
  205. * there are several data bindings but only a single
  206. * low latency one. Split the rest of the quota equally
  207. * between the other data interfaces.
  208. */
  209. quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
  210. quota_rem = QUOTA_100 - n_non_lowlat * quota -
  211. QUOTA_LOWLAT_MIN;
  212. IWL_DEBUG_QUOTA(mvm,
  213. "quota: low-latency binding active, remaining quota per other binding: %d\n",
  214. quota);
  215. } else if (num_active_macs) {
  216. /*
  217. * There are 0 or more than 1 low latency bindings, or all the
  218. * data interfaces belong to the single low latency binding.
  219. * Split the quota equally between the data interfaces.
  220. */
  221. quota = QUOTA_100 / num_active_macs;
  222. quota_rem = QUOTA_100 % num_active_macs;
  223. IWL_DEBUG_QUOTA(mvm,
  224. "quota: splitting evenly per binding: %d\n",
  225. quota);
  226. } else {
  227. /* values don't really matter - won't be used */
  228. quota = 0;
  229. quota_rem = 0;
  230. }
  231. for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
  232. if (data.colors[i] < 0)
  233. continue;
  234. cmd.quotas[idx].id_and_color =
  235. cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
  236. if (data.n_interfaces[i] <= 0)
  237. cmd.quotas[idx].quota = cpu_to_le32(0);
  238. #ifdef CONFIG_IWLWIFI_DEBUGFS
  239. else if (data.dbgfs_min[i])
  240. cmd.quotas[idx].quota =
  241. cpu_to_le32(data.dbgfs_min[i] * QUOTA_100 / 100);
  242. #endif
  243. else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
  244. data.low_latency[i])
  245. /*
  246. * There is more than one binding, but only one of the
  247. * bindings is in low latency. For this case, allocate
  248. * the minimal required quota for the low latency
  249. * binding.
  250. */
  251. cmd.quotas[idx].quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
  252. else
  253. cmd.quotas[idx].quota =
  254. cpu_to_le32(quota * data.n_interfaces[i]);
  255. WARN_ONCE(le32_to_cpu(cmd.quotas[idx].quota) > QUOTA_100,
  256. "Binding=%d, quota=%u > max=%u\n",
  257. idx, le32_to_cpu(cmd.quotas[idx].quota), QUOTA_100);
  258. cmd.quotas[idx].max_duration = cpu_to_le32(0);
  259. idx++;
  260. }
  261. /* Give the remainder of the session to the first data binding */
  262. for (i = 0; i < MAX_BINDINGS; i++) {
  263. if (le32_to_cpu(cmd.quotas[i].quota) != 0) {
  264. le32_add_cpu(&cmd.quotas[i].quota, quota_rem);
  265. IWL_DEBUG_QUOTA(mvm,
  266. "quota: giving remainder of %d to binding %d\n",
  267. quota_rem, i);
  268. break;
  269. }
  270. }
  271. iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
  272. /* check that we have non-zero quota for all valid bindings */
  273. for (i = 0; i < MAX_BINDINGS; i++) {
  274. if (cmd.quotas[i].id_and_color != last->quotas[i].id_and_color)
  275. send = true;
  276. if (cmd.quotas[i].max_duration != last->quotas[i].max_duration)
  277. send = true;
  278. if (abs((int)le32_to_cpu(cmd.quotas[i].quota) -
  279. (int)le32_to_cpu(last->quotas[i].quota))
  280. > IWL_MVM_QUOTA_THRESHOLD)
  281. send = true;
  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. if (!send && !force_update) {
  288. /* don't send a practically unchanged command, the firmware has
  289. * to re-initialize a lot of state and that can have an adverse
  290. * impact on it
  291. */
  292. return 0;
  293. }
  294. err = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0, sizeof(cmd), &cmd);
  295. if (err)
  296. IWL_ERR(mvm, "Failed to send quota: %d\n", err);
  297. else
  298. mvm->last_quota_cmd = cmd;
  299. return err;
  300. }