phy-ctxt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /* Maps the driver specific channel width definition to the fw values */
  69. u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
  70. {
  71. switch (chandef->width) {
  72. case NL80211_CHAN_WIDTH_20_NOHT:
  73. case NL80211_CHAN_WIDTH_20:
  74. return PHY_VHT_CHANNEL_MODE20;
  75. case NL80211_CHAN_WIDTH_40:
  76. return PHY_VHT_CHANNEL_MODE40;
  77. case NL80211_CHAN_WIDTH_80:
  78. return PHY_VHT_CHANNEL_MODE80;
  79. case NL80211_CHAN_WIDTH_160:
  80. return PHY_VHT_CHANNEL_MODE160;
  81. default:
  82. WARN(1, "Invalid channel width=%u", chandef->width);
  83. return PHY_VHT_CHANNEL_MODE20;
  84. }
  85. }
  86. /*
  87. * Maps the driver specific control channel position (relative to the center
  88. * freq) definitions to the the fw values
  89. */
  90. u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
  91. {
  92. switch (chandef->chan->center_freq - chandef->center_freq1) {
  93. case -70:
  94. return PHY_VHT_CTRL_POS_4_BELOW;
  95. case -50:
  96. return PHY_VHT_CTRL_POS_3_BELOW;
  97. case -30:
  98. return PHY_VHT_CTRL_POS_2_BELOW;
  99. case -10:
  100. return PHY_VHT_CTRL_POS_1_BELOW;
  101. case 10:
  102. return PHY_VHT_CTRL_POS_1_ABOVE;
  103. case 30:
  104. return PHY_VHT_CTRL_POS_2_ABOVE;
  105. case 50:
  106. return PHY_VHT_CTRL_POS_3_ABOVE;
  107. case 70:
  108. return PHY_VHT_CTRL_POS_4_ABOVE;
  109. default:
  110. WARN(1, "Invalid channel definition");
  111. case 0:
  112. /*
  113. * The FW is expected to check the control channel position only
  114. * when in HT/VHT and the channel width is not 20MHz. Return
  115. * this value as the default one.
  116. */
  117. return PHY_VHT_CTRL_POS_1_BELOW;
  118. }
  119. }
  120. /*
  121. * Construct the generic fields of the PHY context command
  122. */
  123. static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
  124. struct iwl_phy_context_cmd *cmd,
  125. u32 action, u32 apply_time)
  126. {
  127. memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
  128. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
  129. ctxt->color));
  130. cmd->action = cpu_to_le32(action);
  131. cmd->apply_time = cpu_to_le32(apply_time);
  132. }
  133. /*
  134. * Add the phy configuration to the PHY context command
  135. */
  136. static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
  137. struct iwl_phy_context_cmd *cmd,
  138. struct cfg80211_chan_def *chandef,
  139. u8 chains_static, u8 chains_dynamic)
  140. {
  141. u8 active_cnt, idle_cnt;
  142. /* Set the channel info data */
  143. cmd->ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ?
  144. PHY_BAND_24 : PHY_BAND_5);
  145. cmd->ci.channel = chandef->chan->hw_value;
  146. cmd->ci.width = iwl_mvm_get_channel_width(chandef);
  147. cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
  148. /* Set rx the chains */
  149. idle_cnt = chains_static;
  150. active_cnt = chains_dynamic;
  151. /* In scenarios where we only ever use a single-stream rates,
  152. * i.e. legacy 11b/g/a associations, single-stream APs or even
  153. * static SMPS, enable both chains to get diversity, improving
  154. * the case where we're far enough from the AP that attenuation
  155. * between the two antennas is sufficiently different to impact
  156. * performance.
  157. */
  158. if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
  159. idle_cnt = 2;
  160. active_cnt = 2;
  161. }
  162. cmd->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
  163. PHY_RX_CHAIN_VALID_POS);
  164. cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
  165. cmd->rxchain_info |= cpu_to_le32(active_cnt <<
  166. PHY_RX_CHAIN_MIMO_CNT_POS);
  167. #ifdef CONFIG_IWLWIFI_DEBUGFS
  168. if (unlikely(mvm->dbgfs_rx_phyinfo))
  169. cmd->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
  170. #endif
  171. cmd->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  172. }
  173. /*
  174. * Send a command to apply the current phy configuration. The command is send
  175. * only if something in the configuration changed: in case that this is the
  176. * first time that the phy configuration is applied or in case that the phy
  177. * configuration changed from the previous apply.
  178. */
  179. static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
  180. struct iwl_mvm_phy_ctxt *ctxt,
  181. struct cfg80211_chan_def *chandef,
  182. u8 chains_static, u8 chains_dynamic,
  183. u32 action, u32 apply_time)
  184. {
  185. struct iwl_phy_context_cmd cmd;
  186. int ret;
  187. /* Set the command header fields */
  188. iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
  189. /* Set the command data */
  190. iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
  191. chains_static, chains_dynamic);
  192. ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0,
  193. sizeof(struct iwl_phy_context_cmd),
  194. &cmd);
  195. if (ret)
  196. IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
  197. return ret;
  198. }
  199. /*
  200. * Send a command to add a PHY context based on the current HW configuration.
  201. */
  202. int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  203. struct cfg80211_chan_def *chandef,
  204. u8 chains_static, u8 chains_dynamic)
  205. {
  206. WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  207. ctxt->ref);
  208. lockdep_assert_held(&mvm->mutex);
  209. ctxt->channel = chandef->chan;
  210. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  211. chains_static, chains_dynamic,
  212. FW_CTXT_ACTION_ADD, 0);
  213. }
  214. /*
  215. * Update the number of references to the given PHY context. This is valid only
  216. * in case the PHY context was already created, i.e., its reference count > 0.
  217. */
  218. void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  219. {
  220. lockdep_assert_held(&mvm->mutex);
  221. ctxt->ref++;
  222. }
  223. /*
  224. * Send a command to modify the PHY context based on the current HW
  225. * configuration. Note that the function does not check that the configuration
  226. * changed.
  227. */
  228. int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
  229. struct cfg80211_chan_def *chandef,
  230. u8 chains_static, u8 chains_dynamic)
  231. {
  232. lockdep_assert_held(&mvm->mutex);
  233. ctxt->channel = chandef->chan;
  234. return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
  235. chains_static, chains_dynamic,
  236. FW_CTXT_ACTION_MODIFY, 0);
  237. }
  238. void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
  239. {
  240. lockdep_assert_held(&mvm->mutex);
  241. if (WARN_ON_ONCE(!ctxt))
  242. return;
  243. ctxt->ref--;
  244. }
  245. static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
  246. struct ieee80211_vif *vif)
  247. {
  248. unsigned long *data = _data;
  249. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  250. if (!mvmvif->phy_ctxt)
  251. return;
  252. if (vif->type == NL80211_IFTYPE_STATION ||
  253. vif->type == NL80211_IFTYPE_AP)
  254. __set_bit(mvmvif->phy_ctxt->id, data);
  255. }
  256. int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
  257. {
  258. unsigned long phy_ctxt_counter = 0;
  259. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  260. IEEE80211_IFACE_ITER_NORMAL,
  261. iwl_mvm_binding_iterator,
  262. &phy_ctxt_counter);
  263. return hweight8(phy_ctxt_counter);
  264. }