iwl-agn-hcmd.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-agn.h"
  37. int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
  38. {
  39. struct iwl_tx_ant_config_cmd tx_ant_cmd = {
  40. .valid = cpu_to_le32(valid_tx_ant),
  41. };
  42. if (IWL_UCODE_API(priv->ucode_ver) > 1) {
  43. IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
  44. return priv->trans.ops->send_cmd_pdu(priv,
  45. TX_ANT_CONFIGURATION_CMD,
  46. CMD_SYNC,
  47. sizeof(struct iwl_tx_ant_config_cmd),
  48. &tx_ant_cmd);
  49. } else {
  50. IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
  51. return -EOPNOTSUPP;
  52. }
  53. }
  54. static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
  55. {
  56. u16 size = (u16)sizeof(struct iwl_addsta_cmd);
  57. struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
  58. memcpy(addsta, cmd, size);
  59. /* resrved in 5000 */
  60. addsta->rate_n_flags = cpu_to_le16(0);
  61. return size;
  62. }
  63. static void iwlagn_gain_computation(struct iwl_priv *priv,
  64. u32 average_noise[NUM_RX_CHAINS],
  65. u16 min_average_noise_antenna_i,
  66. u32 min_average_noise,
  67. u8 default_chain)
  68. {
  69. int i;
  70. s32 delta_g;
  71. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  72. /*
  73. * Find Gain Code for the chains based on "default chain"
  74. */
  75. for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
  76. if ((data->disconn_array[i])) {
  77. data->delta_gain_code[i] = 0;
  78. continue;
  79. }
  80. delta_g = (priv->cfg->base_params->chain_noise_scale *
  81. ((s32)average_noise[default_chain] -
  82. (s32)average_noise[i])) / 1500;
  83. /* bound gain by 2 bits value max, 3rd bit is sign */
  84. data->delta_gain_code[i] =
  85. min(abs(delta_g), (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
  86. if (delta_g < 0)
  87. /*
  88. * set negative sign ...
  89. * note to Intel developers: This is uCode API format,
  90. * not the format of any internal device registers.
  91. * Do not change this format for e.g. 6050 or similar
  92. * devices. Change format only if more resolution
  93. * (i.e. more than 2 bits magnitude) is needed.
  94. */
  95. data->delta_gain_code[i] |= (1 << 2);
  96. }
  97. IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
  98. data->delta_gain_code[1], data->delta_gain_code[2]);
  99. if (!data->radio_write) {
  100. struct iwl_calib_chain_noise_gain_cmd cmd;
  101. memset(&cmd, 0, sizeof(cmd));
  102. iwl_set_calib_hdr(&cmd.hdr,
  103. priv->_agn.phy_calib_chain_noise_gain_cmd);
  104. cmd.delta_gain_1 = data->delta_gain_code[1];
  105. cmd.delta_gain_2 = data->delta_gain_code[2];
  106. priv->trans.ops->send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  107. CMD_ASYNC, sizeof(cmd), &cmd);
  108. data->radio_write = 1;
  109. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  110. }
  111. }
  112. static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
  113. {
  114. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  115. int ret;
  116. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  117. iwl_is_any_associated(priv)) {
  118. struct iwl_calib_chain_noise_reset_cmd cmd;
  119. /* clear data for chain noise calibration algorithm */
  120. data->chain_noise_a = 0;
  121. data->chain_noise_b = 0;
  122. data->chain_noise_c = 0;
  123. data->chain_signal_a = 0;
  124. data->chain_signal_b = 0;
  125. data->chain_signal_c = 0;
  126. data->beacon_count = 0;
  127. memset(&cmd, 0, sizeof(cmd));
  128. iwl_set_calib_hdr(&cmd.hdr,
  129. priv->_agn.phy_calib_chain_noise_reset_cmd);
  130. ret = priv->trans.ops->send_cmd_pdu(priv,
  131. REPLY_PHY_CALIBRATION_CMD,
  132. CMD_SYNC, sizeof(cmd), &cmd);
  133. if (ret)
  134. IWL_ERR(priv,
  135. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  136. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  137. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  138. }
  139. }
  140. static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
  141. struct ieee80211_tx_info *info,
  142. __le16 fc, __le32 *tx_flags)
  143. {
  144. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
  145. info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
  146. info->flags & IEEE80211_TX_CTL_AMPDU)
  147. *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
  148. }
  149. /* Calc max signal level (dBm) among 3 possible receivers */
  150. static int iwlagn_calc_rssi(struct iwl_priv *priv,
  151. struct iwl_rx_phy_res *rx_resp)
  152. {
  153. /* data from PHY/DSP regarding signal strength, etc.,
  154. * contents are always there, not configurable by host
  155. */
  156. struct iwlagn_non_cfg_phy *ncphy =
  157. (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  158. u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
  159. u8 agc;
  160. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
  161. agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
  162. /* Find max rssi among 3 possible receivers.
  163. * These values are measured by the digital signal processor (DSP).
  164. * They should stay fairly constant even as the signal strength varies,
  165. * if the radio's automatic gain control (AGC) is working right.
  166. * AGC value (see below) will provide the "interesting" info.
  167. */
  168. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
  169. rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
  170. IWLAGN_OFDM_RSSI_A_BIT_POS;
  171. rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
  172. IWLAGN_OFDM_RSSI_B_BIT_POS;
  173. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
  174. rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
  175. IWLAGN_OFDM_RSSI_C_BIT_POS;
  176. max_rssi = max_t(u32, rssi_a, rssi_b);
  177. max_rssi = max_t(u32, max_rssi, rssi_c);
  178. IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  179. rssi_a, rssi_b, rssi_c, max_rssi, agc);
  180. /* dBm = max_rssi dB - agc dB - constant.
  181. * Higher AGC (higher radio gain) means lower signal. */
  182. return max_rssi - agc - IWLAGN_RSSI_OFFSET;
  183. }
  184. int iwlagn_set_pan_params(struct iwl_priv *priv)
  185. {
  186. struct iwl_wipan_params_cmd cmd;
  187. struct iwl_rxon_context *ctx_bss, *ctx_pan;
  188. int slot0 = 300, slot1 = 0;
  189. int ret;
  190. if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
  191. return 0;
  192. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  193. lockdep_assert_held(&priv->mutex);
  194. ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
  195. ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
  196. /*
  197. * If the PAN context is inactive, then we don't need
  198. * to update the PAN parameters, the last thing we'll
  199. * have done before it goes inactive is making the PAN
  200. * parameters be WLAN-only.
  201. */
  202. if (!ctx_pan->is_active)
  203. return 0;
  204. memset(&cmd, 0, sizeof(cmd));
  205. /* only 2 slots are currently allowed */
  206. cmd.num_slots = 2;
  207. cmd.slots[0].type = 0; /* BSS */
  208. cmd.slots[1].type = 1; /* PAN */
  209. if (priv->_agn.hw_roc_channel) {
  210. /* both contexts must be used for this to happen */
  211. slot1 = priv->_agn.hw_roc_duration;
  212. slot0 = IWL_MIN_SLOT_TIME;
  213. } else if (ctx_bss->vif && ctx_pan->vif) {
  214. int bcnint = ctx_pan->vif->bss_conf.beacon_int;
  215. int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
  216. /* should be set, but seems unused?? */
  217. cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
  218. if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
  219. bcnint &&
  220. bcnint != ctx_bss->vif->bss_conf.beacon_int) {
  221. IWL_ERR(priv,
  222. "beacon intervals don't match (%d, %d)\n",
  223. ctx_bss->vif->bss_conf.beacon_int,
  224. ctx_pan->vif->bss_conf.beacon_int);
  225. } else
  226. bcnint = max_t(int, bcnint,
  227. ctx_bss->vif->bss_conf.beacon_int);
  228. if (!bcnint)
  229. bcnint = DEFAULT_BEACON_INTERVAL;
  230. slot0 = bcnint / 2;
  231. slot1 = bcnint - slot0;
  232. if (test_bit(STATUS_SCAN_HW, &priv->status) ||
  233. (!ctx_bss->vif->bss_conf.idle &&
  234. !ctx_bss->vif->bss_conf.assoc)) {
  235. slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  236. slot1 = IWL_MIN_SLOT_TIME;
  237. } else if (!ctx_pan->vif->bss_conf.idle &&
  238. !ctx_pan->vif->bss_conf.assoc) {
  239. slot1 = bcnint * 3 - IWL_MIN_SLOT_TIME;
  240. slot0 = IWL_MIN_SLOT_TIME;
  241. }
  242. } else if (ctx_pan->vif) {
  243. slot0 = 0;
  244. slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
  245. ctx_pan->vif->bss_conf.beacon_int;
  246. slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
  247. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  248. slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
  249. slot1 = IWL_MIN_SLOT_TIME;
  250. }
  251. }
  252. cmd.slots[0].width = cpu_to_le16(slot0);
  253. cmd.slots[1].width = cpu_to_le16(slot1);
  254. ret = priv->trans.ops->send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
  255. sizeof(cmd), &cmd);
  256. if (ret)
  257. IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
  258. return ret;
  259. }
  260. struct iwl_hcmd_utils_ops iwlagn_hcmd_utils = {
  261. .build_addsta_hcmd = iwlagn_build_addsta_hcmd,
  262. .gain_computation = iwlagn_gain_computation,
  263. .chain_noise_reset = iwlagn_chain_noise_reset,
  264. .tx_cmd_protection = iwlagn_tx_cmd_protection,
  265. .calc_rssi = iwlagn_calc_rssi,
  266. .request_scan = iwlagn_request_scan,
  267. };