phy-ctxt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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) 2017 Intel Deutschland GmbH
  11. * Copyright(c) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <linuxwifi@intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. * BSD LICENSE
  30. *
  31. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  32. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  33. * Copyright(c) 2018 Intel Corporation
  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. /* Maps the driver specific channel width definition to the fw values */
  67. u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
  68. {
  69. switch (chandef->width) {
  70. case NL80211_CHAN_WIDTH_20_NOHT:
  71. case NL80211_CHAN_WIDTH_20:
  72. return PHY_VHT_CHANNEL_MODE20;
  73. case NL80211_CHAN_WIDTH_40:
  74. return PHY_VHT_CHANNEL_MODE40;
  75. case NL80211_CHAN_WIDTH_80:
  76. return PHY_VHT_CHANNEL_MODE80;
  77. case NL80211_CHAN_WIDTH_160:
  78. return PHY_VHT_CHANNEL_MODE160;
  79. default:
  80. WARN(1, "Invalid channel width=%u", chandef->width);
  81. return PHY_VHT_CHANNEL_MODE20;
  82. }
  83. }
  84. /*
  85. * Maps the driver specific control channel position (relative to the center
  86. * freq) definitions to the the fw values
  87. */
  88. u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
  89. {
  90. switch (chandef->chan->center_freq - chandef->center_freq1) {
  91. case -70:
  92. return PHY_VHT_CTRL_POS_4_BELOW;
  93. case -50:
  94. return PHY_VHT_CTRL_POS_3_BELOW;
  95. case -30:
  96. return PHY_VHT_CTRL_POS_2_BELOW;
  97. case -10:
  98. return PHY_VHT_CTRL_POS_1_BELOW;
  99. case 10:
  100. return PHY_VHT_CTRL_POS_1_ABOVE;
  101. case 30:
  102. return PHY_VHT_CTRL_POS_2_ABOVE;
  103. case 50:
  104. return PHY_VHT_CTRL_POS_3_ABOVE;
  105. case 70:
  106. return PHY_VHT_CTRL_POS_4_ABOVE;
  107. default:
  108. WARN(1, "Invalid channel definition");
  109. case 0:
  110. /*
  111. * The FW is expected to check the control channel position only
  112. * when in HT/VHT and the channel width is not 20MHz. Return
  113. * this value as the default one.
  114. */
  115. return PHY_VHT_CTRL_POS_1_BELOW;
  116. }
  117. }
  118. /*
  119. * Construct the generic fields of the PHY context command
  120. */
  121. static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
  122. struct iwl_phy_context_cmd *cmd,
  123. u32 action, u32 apply_time)
  124. {
  125. memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
  126. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
  127. ctxt->color));
  128. cmd->action = cpu_to_le32(action);
  129. cmd->apply_time = cpu_to_le32(apply_time);
  130. }
  131. /*
  132. * Add the phy configuration to the PHY context command
  133. */
  134. static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
  135. struct iwl_phy_context_cmd *cmd,
  136. struct cfg80211_chan_def *chandef,
  137. u8 chains_static, u8 chains_dynamic)
  138. {
  139. u8 active_cnt, idle_cnt;
  140. /* Set the channel info data */
  141. cmd->ci.band = (chandef->chan->band == NL80211_BAND_2GHZ ?
  142. PHY_BAND_24 : PHY_BAND_5);
  143. cmd->ci.channel = chandef->chan->hw_value;
  144. cmd->ci.width = iwl_mvm_get_channel_width(chandef);
  145. cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
  146. /* Set rx the chains */
  147. idle_cnt = chains_static;
  148. active_cnt = chains_dynamic;
  149. /* In scenarios where we only ever use a single-stream rates,
  150. * i.e. legacy 11b/g/a associations, single-stream APs or even
  151. * static SMPS, enable both chains to get diversity, improving
  152. * the case where we're far enough from the AP that attenuation
  153. * between the two antennas is sufficiently different to impact
  154. * performance.
  155. */
  156. if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
  157. idle_cnt = 2;
  158. active_cnt = 2;
  159. }
  160. cmd->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
  161. PHY_RX_CHAIN_VALID_POS);
  162. cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
  163. cmd->rxchain_info |= cpu_to_le32(active_cnt <<
  164. PHY_RX_CHAIN_MIMO_CNT_POS);
  165. #ifdef CONFIG_IWLWIFI_DEBUGFS
  166. if (unlikely(mvm->dbgfs_rx_phyinfo))
  167. cmd->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
  168. #endif
  169. cmd->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  170. }
  171. /*
  172. * Send a command to apply the current phy configuration. The command is send
  173. * only if something in the configuration changed: in case that this is the
  174. * first time that the phy configuration is applied or in case that the phy
  175. * configuration changed from the previous apply.
  176. */
  177. static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
  178. struct iwl_mvm_phy_ctxt *ctxt,
  179. struct cfg80211_chan_def *chandef,
  180. u8 chains_static, u8 chains_dynamic,
  181. u32 action, u32 apply_time)
  182. {
  183. struct iwl_phy_context_cmd cmd;
  184. int ret;
  185. /* Set the command header fields */
  186. iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
  187. /* Set the command data */
  188. iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
  189. chains_static, chains_dynamic);
  190. ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0,
  191. sizeof(struct iwl_phy_context_cmd),
  192. &cmd);
  193. if (ret)
  194. IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
  195. return ret;
  196. }
  197. /*
  198. * Send a command to add a PHY context based on the current HW configuration.
  199. */
  200. int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  201. struct cfg80211_chan_def *chandef,
  202. u8 chains_static, u8 chains_dynamic)
  203. {
  204. WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  205. ctxt->ref);
  206. lockdep_assert_held(&mvm->mutex);
  207. ctxt->channel = chandef->chan;
  208. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  209. chains_static, chains_dynamic,
  210. FW_CTXT_ACTION_ADD, 0);
  211. }
  212. /*
  213. * Update the number of references to the given PHY context. This is valid only
  214. * in case the PHY context was already created, i.e., its reference count > 0.
  215. */
  216. void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  217. {
  218. lockdep_assert_held(&mvm->mutex);
  219. ctxt->ref++;
  220. }
  221. /*
  222. * Send a command to modify the PHY context based on the current HW
  223. * configuration. Note that the function does not check that the configuration
  224. * changed.
  225. */
  226. int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  227. struct cfg80211_chan_def *chandef,
  228. u8 chains_static, u8 chains_dynamic)
  229. {
  230. enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
  231. lockdep_assert_held(&mvm->mutex);
  232. if (fw_has_capa(&mvm->fw->ucode_capa,
  233. IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
  234. ctxt->channel->band != chandef->chan->band) {
  235. int ret;
  236. /* ... remove it here ...*/
  237. ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  238. chains_static, chains_dynamic,
  239. FW_CTXT_ACTION_REMOVE, 0);
  240. if (ret)
  241. return ret;
  242. /* ... and proceed to add it again */
  243. action = FW_CTXT_ACTION_ADD;
  244. }
  245. ctxt->channel = chandef->chan;
  246. ctxt->width = chandef->width;
  247. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  248. chains_static, chains_dynamic,
  249. action, 0);
  250. }
  251. void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  252. {
  253. lockdep_assert_held(&mvm->mutex);
  254. if (WARN_ON_ONCE(!ctxt))
  255. return;
  256. ctxt->ref--;
  257. /*
  258. * Move unused phy's to a default channel. When the phy is moved the,
  259. * fw will cleanup immediate quiet bit if it was previously set,
  260. * otherwise we might not be able to reuse this phy.
  261. */
  262. if (ctxt->ref == 0) {
  263. struct ieee80211_channel *chan;
  264. struct cfg80211_chan_def chandef;
  265. chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
  266. cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
  267. iwl_mvm_phy_ctxt_changed(mvm, ctxt, &chandef, 1, 1);
  268. }
  269. }
  270. static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
  271. struct ieee80211_vif *vif)
  272. {
  273. unsigned long *data = _data;
  274. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  275. if (!mvmvif->phy_ctxt)
  276. return;
  277. if (vif->type == NL80211_IFTYPE_STATION ||
  278. vif->type == NL80211_IFTYPE_AP)
  279. __set_bit(mvmvif->phy_ctxt->id, data);
  280. }
  281. int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
  282. {
  283. unsigned long phy_ctxt_counter = 0;
  284. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  285. IEEE80211_IFACE_ITER_NORMAL,
  286. iwl_mvm_binding_iterator,
  287. &phy_ctxt_counter);
  288. return hweight8(phy_ctxt_counter);
  289. }