quota.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 - 2017 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 - 2017 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. struct iwl_time_quota_data *data =
  149. iwl_mvm_quota_cmd_get_quota(mvm, cmd,
  150. i);
  151. u32 id_n_c = le32_to_cpu(data->id_and_color);
  152. u32 id = (id_n_c & FW_CTXT_ID_MSK) >> FW_CTXT_ID_POS;
  153. u32 quota = le32_to_cpu(data->quota);
  154. if (id != phy_id)
  155. continue;
  156. quota *= (beacon_int - mvm->noa_duration);
  157. quota /= beacon_int;
  158. IWL_DEBUG_QUOTA(mvm, "quota: adjust for NoA from %d to %d\n",
  159. le32_to_cpu(data->quota), quota);
  160. data->quota = cpu_to_le32(quota);
  161. }
  162. #endif
  163. }
  164. int iwl_mvm_update_quotas(struct iwl_mvm *mvm,
  165. bool force_update,
  166. struct ieee80211_vif *disabled_vif)
  167. {
  168. struct iwl_time_quota_cmd cmd = {};
  169. int i, idx, err, num_active_macs, quota, quota_rem, n_non_lowlat;
  170. struct iwl_mvm_quota_iterator_data data = {
  171. .n_interfaces = {},
  172. .colors = { -1, -1, -1, -1 },
  173. .disabled_vif = disabled_vif,
  174. };
  175. struct iwl_time_quota_cmd *last = &mvm->last_quota_cmd;
  176. struct iwl_time_quota_data *qdata, *last_data;
  177. bool send = false;
  178. lockdep_assert_held(&mvm->mutex);
  179. if (fw_has_capa(&mvm->fw->ucode_capa,
  180. IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
  181. return 0;
  182. /* update all upon completion */
  183. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  184. return 0;
  185. /* iterator data above must match */
  186. BUILD_BUG_ON(MAX_BINDINGS != 4);
  187. ieee80211_iterate_active_interfaces_atomic(
  188. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  189. iwl_mvm_quota_iterator, &data);
  190. /*
  191. * The FW's scheduling session consists of
  192. * IWL_MVM_MAX_QUOTA fragments. Divide these fragments
  193. * equally between all the bindings that require quota
  194. */
  195. num_active_macs = 0;
  196. for (i = 0; i < MAX_BINDINGS; i++) {
  197. qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
  198. qdata->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  199. num_active_macs += data.n_interfaces[i];
  200. }
  201. n_non_lowlat = num_active_macs;
  202. if (data.n_low_latency_bindings == 1) {
  203. for (i = 0; i < MAX_BINDINGS; i++) {
  204. if (data.low_latency[i]) {
  205. n_non_lowlat -= data.n_interfaces[i];
  206. break;
  207. }
  208. }
  209. }
  210. if (data.n_low_latency_bindings == 1 && n_non_lowlat) {
  211. /*
  212. * Reserve quota for the low latency binding in case that
  213. * there are several data bindings but only a single
  214. * low latency one. Split the rest of the quota equally
  215. * between the other data interfaces.
  216. */
  217. quota = (QUOTA_100 - QUOTA_LOWLAT_MIN) / n_non_lowlat;
  218. quota_rem = QUOTA_100 - n_non_lowlat * quota -
  219. QUOTA_LOWLAT_MIN;
  220. IWL_DEBUG_QUOTA(mvm,
  221. "quota: low-latency binding active, remaining quota per other binding: %d\n",
  222. quota);
  223. } else if (num_active_macs) {
  224. /*
  225. * There are 0 or more than 1 low latency bindings, or all the
  226. * data interfaces belong to the single low latency binding.
  227. * Split the quota equally between the data interfaces.
  228. */
  229. quota = QUOTA_100 / num_active_macs;
  230. quota_rem = QUOTA_100 % num_active_macs;
  231. IWL_DEBUG_QUOTA(mvm,
  232. "quota: splitting evenly per binding: %d\n",
  233. quota);
  234. } else {
  235. /* values don't really matter - won't be used */
  236. quota = 0;
  237. quota_rem = 0;
  238. }
  239. for (idx = 0, i = 0; i < MAX_BINDINGS; i++) {
  240. if (data.colors[i] < 0)
  241. continue;
  242. qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, idx);
  243. qdata->id_and_color =
  244. cpu_to_le32(FW_CMD_ID_AND_COLOR(i, data.colors[i]));
  245. if (data.n_interfaces[i] <= 0)
  246. qdata->quota = cpu_to_le32(0);
  247. #ifdef CONFIG_IWLWIFI_DEBUGFS
  248. else if (data.dbgfs_min[i])
  249. qdata->quota =
  250. cpu_to_le32(data.dbgfs_min[i] * QUOTA_100 / 100);
  251. #endif
  252. else if (data.n_low_latency_bindings == 1 && n_non_lowlat &&
  253. data.low_latency[i])
  254. /*
  255. * There is more than one binding, but only one of the
  256. * bindings is in low latency. For this case, allocate
  257. * the minimal required quota for the low latency
  258. * binding.
  259. */
  260. qdata->quota = cpu_to_le32(QUOTA_LOWLAT_MIN);
  261. else
  262. qdata->quota =
  263. cpu_to_le32(quota * data.n_interfaces[i]);
  264. WARN_ONCE(le32_to_cpu(qdata->quota) > QUOTA_100,
  265. "Binding=%d, quota=%u > max=%u\n",
  266. idx, le32_to_cpu(qdata->quota), QUOTA_100);
  267. qdata->max_duration = cpu_to_le32(0);
  268. idx++;
  269. }
  270. /* Give the remainder of the session to the first data binding */
  271. for (i = 0; i < MAX_BINDINGS; i++) {
  272. qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
  273. if (le32_to_cpu(qdata->quota) != 0) {
  274. le32_add_cpu(&qdata->quota, quota_rem);
  275. IWL_DEBUG_QUOTA(mvm,
  276. "quota: giving remainder of %d to binding %d\n",
  277. quota_rem, i);
  278. break;
  279. }
  280. }
  281. iwl_mvm_adjust_quota_for_noa(mvm, &cmd);
  282. /* check that we have non-zero quota for all valid bindings */
  283. for (i = 0; i < MAX_BINDINGS; i++) {
  284. qdata = iwl_mvm_quota_cmd_get_quota(mvm, &cmd, i);
  285. last_data = iwl_mvm_quota_cmd_get_quota(mvm, last, i);
  286. if (qdata->id_and_color != last_data->id_and_color)
  287. send = true;
  288. if (qdata->max_duration != last_data->max_duration)
  289. send = true;
  290. if (abs((int)le32_to_cpu(qdata->quota) -
  291. (int)le32_to_cpu(last_data->quota))
  292. > IWL_MVM_QUOTA_THRESHOLD)
  293. send = true;
  294. if (qdata->id_and_color == cpu_to_le32(FW_CTXT_INVALID))
  295. continue;
  296. WARN_ONCE(qdata->quota == 0,
  297. "zero quota on binding %d\n", i);
  298. }
  299. if (!send && !force_update) {
  300. /* don't send a practically unchanged command, the firmware has
  301. * to re-initialize a lot of state and that can have an adverse
  302. * impact on it
  303. */
  304. return 0;
  305. }
  306. err = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  307. iwl_mvm_quota_cmd_size(mvm), &cmd);
  308. if (err)
  309. IWL_ERR(mvm, "Failed to send quota: %d\n", err);
  310. else
  311. mvm->last_quota_cmd = cmd;
  312. return err;
  313. }