phy-ctxt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. *
  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. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. *
  43. * * Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * * Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in
  47. * the documentation and/or other materials provided with the
  48. * distribution.
  49. * * Neither the name Intel Corporation nor the names of its
  50. * contributors may be used to endorse or promote products derived
  51. * from this software without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  54. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  55. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  56. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  57. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  58. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  59. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  60. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  63. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. *****************************************************************************/
  66. #include <net/mac80211.h>
  67. #include "fw-api.h"
  68. #include "mvm.h"
  69. /* Maps the driver specific channel width definition to the fw values */
  70. u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
  71. {
  72. switch (chandef->width) {
  73. case NL80211_CHAN_WIDTH_20_NOHT:
  74. case NL80211_CHAN_WIDTH_20:
  75. return PHY_VHT_CHANNEL_MODE20;
  76. case NL80211_CHAN_WIDTH_40:
  77. return PHY_VHT_CHANNEL_MODE40;
  78. case NL80211_CHAN_WIDTH_80:
  79. return PHY_VHT_CHANNEL_MODE80;
  80. case NL80211_CHAN_WIDTH_160:
  81. return PHY_VHT_CHANNEL_MODE160;
  82. default:
  83. WARN(1, "Invalid channel width=%u", chandef->width);
  84. return PHY_VHT_CHANNEL_MODE20;
  85. }
  86. }
  87. /*
  88. * Maps the driver specific control channel position (relative to the center
  89. * freq) definitions to the the fw values
  90. */
  91. u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
  92. {
  93. switch (chandef->chan->center_freq - chandef->center_freq1) {
  94. case -70:
  95. return PHY_VHT_CTRL_POS_4_BELOW;
  96. case -50:
  97. return PHY_VHT_CTRL_POS_3_BELOW;
  98. case -30:
  99. return PHY_VHT_CTRL_POS_2_BELOW;
  100. case -10:
  101. return PHY_VHT_CTRL_POS_1_BELOW;
  102. case 10:
  103. return PHY_VHT_CTRL_POS_1_ABOVE;
  104. case 30:
  105. return PHY_VHT_CTRL_POS_2_ABOVE;
  106. case 50:
  107. return PHY_VHT_CTRL_POS_3_ABOVE;
  108. case 70:
  109. return PHY_VHT_CTRL_POS_4_ABOVE;
  110. default:
  111. WARN(1, "Invalid channel definition");
  112. case 0:
  113. /*
  114. * The FW is expected to check the control channel position only
  115. * when in HT/VHT and the channel width is not 20MHz. Return
  116. * this value as the default one.
  117. */
  118. return PHY_VHT_CTRL_POS_1_BELOW;
  119. }
  120. }
  121. /*
  122. * Construct the generic fields of the PHY context command
  123. */
  124. static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
  125. struct iwl_phy_context_cmd *cmd,
  126. u32 action, u32 apply_time)
  127. {
  128. memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
  129. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
  130. ctxt->color));
  131. cmd->action = cpu_to_le32(action);
  132. cmd->apply_time = cpu_to_le32(apply_time);
  133. }
  134. /*
  135. * Add the phy configuration to the PHY context command
  136. */
  137. static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
  138. struct iwl_phy_context_cmd *cmd,
  139. struct cfg80211_chan_def *chandef,
  140. u8 chains_static, u8 chains_dynamic)
  141. {
  142. u8 active_cnt, idle_cnt;
  143. /* Set the channel info data */
  144. cmd->ci.band = (chandef->chan->band == NL80211_BAND_2GHZ ?
  145. PHY_BAND_24 : PHY_BAND_5);
  146. cmd->ci.channel = chandef->chan->hw_value;
  147. cmd->ci.width = iwl_mvm_get_channel_width(chandef);
  148. cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
  149. /* Set rx the chains */
  150. idle_cnt = chains_static;
  151. active_cnt = chains_dynamic;
  152. /* In scenarios where we only ever use a single-stream rates,
  153. * i.e. legacy 11b/g/a associations, single-stream APs or even
  154. * static SMPS, enable both chains to get diversity, improving
  155. * the case where we're far enough from the AP that attenuation
  156. * between the two antennas is sufficiently different to impact
  157. * performance.
  158. */
  159. if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
  160. idle_cnt = 2;
  161. active_cnt = 2;
  162. }
  163. cmd->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
  164. PHY_RX_CHAIN_VALID_POS);
  165. cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
  166. cmd->rxchain_info |= cpu_to_le32(active_cnt <<
  167. PHY_RX_CHAIN_MIMO_CNT_POS);
  168. #ifdef CONFIG_IWLWIFI_DEBUGFS
  169. if (unlikely(mvm->dbgfs_rx_phyinfo))
  170. cmd->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
  171. #endif
  172. cmd->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  173. }
  174. /*
  175. * Send a command to apply the current phy configuration. The command is send
  176. * only if something in the configuration changed: in case that this is the
  177. * first time that the phy configuration is applied or in case that the phy
  178. * configuration changed from the previous apply.
  179. */
  180. static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
  181. struct iwl_mvm_phy_ctxt *ctxt,
  182. struct cfg80211_chan_def *chandef,
  183. u8 chains_static, u8 chains_dynamic,
  184. u32 action, u32 apply_time)
  185. {
  186. struct iwl_phy_context_cmd cmd;
  187. int ret;
  188. /* Set the command header fields */
  189. iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
  190. /* Set the command data */
  191. iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
  192. chains_static, chains_dynamic);
  193. ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0,
  194. sizeof(struct iwl_phy_context_cmd),
  195. &cmd);
  196. if (ret)
  197. IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
  198. return ret;
  199. }
  200. /*
  201. * Send a command to add a PHY context based on the current HW configuration.
  202. */
  203. int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  204. struct cfg80211_chan_def *chandef,
  205. u8 chains_static, u8 chains_dynamic)
  206. {
  207. WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  208. ctxt->ref);
  209. lockdep_assert_held(&mvm->mutex);
  210. ctxt->channel = chandef->chan;
  211. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  212. chains_static, chains_dynamic,
  213. FW_CTXT_ACTION_ADD, 0);
  214. }
  215. /*
  216. * Update the number of references to the given PHY context. This is valid only
  217. * in case the PHY context was already created, i.e., its reference count > 0.
  218. */
  219. void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  220. {
  221. lockdep_assert_held(&mvm->mutex);
  222. ctxt->ref++;
  223. }
  224. /*
  225. * Send a command to modify the PHY context based on the current HW
  226. * configuration. Note that the function does not check that the configuration
  227. * changed.
  228. */
  229. int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  230. struct cfg80211_chan_def *chandef,
  231. u8 chains_static, u8 chains_dynamic)
  232. {
  233. enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
  234. lockdep_assert_held(&mvm->mutex);
  235. if (fw_has_capa(&mvm->fw->ucode_capa,
  236. IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
  237. ctxt->channel->band != chandef->chan->band) {
  238. int ret;
  239. /* ... remove it here ...*/
  240. ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  241. chains_static, chains_dynamic,
  242. FW_CTXT_ACTION_REMOVE, 0);
  243. if (ret)
  244. return ret;
  245. /* ... and proceed to add it again */
  246. action = FW_CTXT_ACTION_ADD;
  247. }
  248. ctxt->channel = chandef->chan;
  249. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  250. chains_static, chains_dynamic,
  251. action, 0);
  252. }
  253. void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  254. {
  255. lockdep_assert_held(&mvm->mutex);
  256. if (WARN_ON_ONCE(!ctxt))
  257. return;
  258. ctxt->ref--;
  259. }
  260. static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
  261. struct ieee80211_vif *vif)
  262. {
  263. unsigned long *data = _data;
  264. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  265. if (!mvmvif->phy_ctxt)
  266. return;
  267. if (vif->type == NL80211_IFTYPE_STATION ||
  268. vif->type == NL80211_IFTYPE_AP)
  269. __set_bit(mvmvif->phy_ctxt->id, data);
  270. }
  271. int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
  272. {
  273. unsigned long phy_ctxt_counter = 0;
  274. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  275. IEEE80211_IFACE_ITER_NORMAL,
  276. iwl_mvm_binding_iterator,
  277. &phy_ctxt_counter);
  278. return hweight8(phy_ctxt_counter);
  279. }