iwl-agn-hcmd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 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_rxon_assoc(struct iwl_priv *priv,
  38. struct iwl_rxon_context *ctx)
  39. {
  40. int ret = 0;
  41. struct iwl5000_rxon_assoc_cmd rxon_assoc;
  42. const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
  43. const struct iwl_rxon_cmd *rxon2 = &ctx->active;
  44. if ((rxon1->flags == rxon2->flags) &&
  45. (rxon1->filter_flags == rxon2->filter_flags) &&
  46. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  47. (rxon1->ofdm_ht_single_stream_basic_rates ==
  48. rxon2->ofdm_ht_single_stream_basic_rates) &&
  49. (rxon1->ofdm_ht_dual_stream_basic_rates ==
  50. rxon2->ofdm_ht_dual_stream_basic_rates) &&
  51. (rxon1->ofdm_ht_triple_stream_basic_rates ==
  52. rxon2->ofdm_ht_triple_stream_basic_rates) &&
  53. (rxon1->acquisition_data == rxon2->acquisition_data) &&
  54. (rxon1->rx_chain == rxon2->rx_chain) &&
  55. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  56. IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
  57. return 0;
  58. }
  59. rxon_assoc.flags = ctx->staging.flags;
  60. rxon_assoc.filter_flags = ctx->staging.filter_flags;
  61. rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
  62. rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
  63. rxon_assoc.reserved1 = 0;
  64. rxon_assoc.reserved2 = 0;
  65. rxon_assoc.reserved3 = 0;
  66. rxon_assoc.ofdm_ht_single_stream_basic_rates =
  67. ctx->staging.ofdm_ht_single_stream_basic_rates;
  68. rxon_assoc.ofdm_ht_dual_stream_basic_rates =
  69. ctx->staging.ofdm_ht_dual_stream_basic_rates;
  70. rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
  71. rxon_assoc.ofdm_ht_triple_stream_basic_rates =
  72. ctx->staging.ofdm_ht_triple_stream_basic_rates;
  73. rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
  74. ret = iwl_send_cmd_pdu_async(priv, ctx->rxon_assoc_cmd,
  75. sizeof(rxon_assoc), &rxon_assoc, NULL);
  76. if (ret)
  77. return ret;
  78. return ret;
  79. }
  80. int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
  81. {
  82. struct iwl_tx_ant_config_cmd tx_ant_cmd = {
  83. .valid = cpu_to_le32(valid_tx_ant),
  84. };
  85. if (IWL_UCODE_API(priv->ucode_ver) > 1) {
  86. IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
  87. return iwl_send_cmd_pdu(priv, TX_ANT_CONFIGURATION_CMD,
  88. sizeof(struct iwl_tx_ant_config_cmd),
  89. &tx_ant_cmd);
  90. } else {
  91. IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
  92. return -EOPNOTSUPP;
  93. }
  94. }
  95. /* Currently this is the superset of everything */
  96. static u16 iwlagn_get_hcmd_size(u8 cmd_id, u16 len)
  97. {
  98. return len;
  99. }
  100. static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
  101. {
  102. u16 size = (u16)sizeof(struct iwl_addsta_cmd);
  103. struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
  104. memcpy(addsta, cmd, size);
  105. /* resrved in 5000 */
  106. addsta->rate_n_flags = cpu_to_le16(0);
  107. return size;
  108. }
  109. static void iwlagn_gain_computation(struct iwl_priv *priv,
  110. u32 average_noise[NUM_RX_CHAINS],
  111. u16 min_average_noise_antenna_i,
  112. u32 min_average_noise,
  113. u8 default_chain)
  114. {
  115. int i;
  116. s32 delta_g;
  117. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  118. /*
  119. * Find Gain Code for the chains based on "default chain"
  120. */
  121. for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
  122. if ((data->disconn_array[i])) {
  123. data->delta_gain_code[i] = 0;
  124. continue;
  125. }
  126. delta_g = (priv->cfg->base_params->chain_noise_scale *
  127. ((s32)average_noise[default_chain] -
  128. (s32)average_noise[i])) / 1500;
  129. /* bound gain by 2 bits value max, 3rd bit is sign */
  130. data->delta_gain_code[i] =
  131. min(abs(delta_g), (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
  132. if (delta_g < 0)
  133. /*
  134. * set negative sign ...
  135. * note to Intel developers: This is uCode API format,
  136. * not the format of any internal device registers.
  137. * Do not change this format for e.g. 6050 or similar
  138. * devices. Change format only if more resolution
  139. * (i.e. more than 2 bits magnitude) is needed.
  140. */
  141. data->delta_gain_code[i] |= (1 << 2);
  142. }
  143. IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
  144. data->delta_gain_code[1], data->delta_gain_code[2]);
  145. if (!data->radio_write) {
  146. struct iwl_calib_chain_noise_gain_cmd cmd;
  147. memset(&cmd, 0, sizeof(cmd));
  148. cmd.hdr.op_code = priv->_agn.phy_calib_chain_noise_gain_cmd;
  149. cmd.hdr.first_group = 0;
  150. cmd.hdr.groups_num = 1;
  151. cmd.hdr.data_valid = 1;
  152. cmd.delta_gain_1 = data->delta_gain_code[1];
  153. cmd.delta_gain_2 = data->delta_gain_code[2];
  154. iwl_send_cmd_pdu_async(priv, REPLY_PHY_CALIBRATION_CMD,
  155. sizeof(cmd), &cmd, NULL);
  156. data->radio_write = 1;
  157. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  158. }
  159. }
  160. static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
  161. {
  162. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  163. int ret;
  164. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  165. iwl_is_any_associated(priv)) {
  166. struct iwl_calib_chain_noise_reset_cmd cmd;
  167. /* clear data for chain noise calibration algorithm */
  168. data->chain_noise_a = 0;
  169. data->chain_noise_b = 0;
  170. data->chain_noise_c = 0;
  171. data->chain_signal_a = 0;
  172. data->chain_signal_b = 0;
  173. data->chain_signal_c = 0;
  174. data->beacon_count = 0;
  175. memset(&cmd, 0, sizeof(cmd));
  176. cmd.hdr.op_code = priv->_agn.phy_calib_chain_noise_reset_cmd;
  177. cmd.hdr.first_group = 0;
  178. cmd.hdr.groups_num = 1;
  179. cmd.hdr.data_valid = 1;
  180. ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  181. sizeof(cmd), &cmd);
  182. if (ret)
  183. IWL_ERR(priv,
  184. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  185. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  186. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  187. }
  188. }
  189. static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
  190. struct ieee80211_tx_info *info,
  191. __le16 fc, __le32 *tx_flags)
  192. {
  193. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
  194. info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  195. *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
  196. return;
  197. }
  198. if (priv->cfg->ht_params &&
  199. priv->cfg->ht_params->use_rts_for_aggregation &&
  200. info->flags & IEEE80211_TX_CTL_AMPDU) {
  201. *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
  202. return;
  203. }
  204. }
  205. /* Calc max signal level (dBm) among 3 possible receivers */
  206. static int iwlagn_calc_rssi(struct iwl_priv *priv,
  207. struct iwl_rx_phy_res *rx_resp)
  208. {
  209. /* data from PHY/DSP regarding signal strength, etc.,
  210. * contents are always there, not configurable by host
  211. */
  212. struct iwlagn_non_cfg_phy *ncphy =
  213. (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  214. u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
  215. u8 agc;
  216. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
  217. agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
  218. /* Find max rssi among 3 possible receivers.
  219. * These values are measured by the digital signal processor (DSP).
  220. * They should stay fairly constant even as the signal strength varies,
  221. * if the radio's automatic gain control (AGC) is working right.
  222. * AGC value (see below) will provide the "interesting" info.
  223. */
  224. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
  225. rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
  226. IWLAGN_OFDM_RSSI_A_BIT_POS;
  227. rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
  228. IWLAGN_OFDM_RSSI_B_BIT_POS;
  229. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
  230. rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
  231. IWLAGN_OFDM_RSSI_C_BIT_POS;
  232. max_rssi = max_t(u32, rssi_a, rssi_b);
  233. max_rssi = max_t(u32, max_rssi, rssi_c);
  234. IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  235. rssi_a, rssi_b, rssi_c, max_rssi, agc);
  236. /* dBm = max_rssi dB - agc dB - constant.
  237. * Higher AGC (higher radio gain) means lower signal. */
  238. return max_rssi - agc - IWLAGN_RSSI_OFFSET;
  239. }
  240. static int iwlagn_set_pan_params(struct iwl_priv *priv)
  241. {
  242. struct iwl_wipan_params_cmd cmd;
  243. struct iwl_rxon_context *ctx_bss, *ctx_pan;
  244. int slot0 = 300, slot1 = 0;
  245. int ret;
  246. if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
  247. return 0;
  248. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  249. lockdep_assert_held(&priv->mutex);
  250. ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
  251. ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
  252. /*
  253. * If the PAN context is inactive, then we don't need
  254. * to update the PAN parameters, the last thing we'll
  255. * have done before it goes inactive is making the PAN
  256. * parameters be WLAN-only.
  257. */
  258. if (!ctx_pan->is_active)
  259. return 0;
  260. memset(&cmd, 0, sizeof(cmd));
  261. /* only 2 slots are currently allowed */
  262. cmd.num_slots = 2;
  263. cmd.slots[0].type = 0; /* BSS */
  264. cmd.slots[1].type = 1; /* PAN */
  265. if (ctx_bss->vif && ctx_pan->vif) {
  266. int bcnint = ctx_pan->vif->bss_conf.beacon_int;
  267. /* should be set, but seems unused?? */
  268. cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
  269. if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
  270. bcnint &&
  271. bcnint != ctx_bss->vif->bss_conf.beacon_int) {
  272. IWL_ERR(priv,
  273. "beacon intervals don't match (%d, %d)\n",
  274. ctx_bss->vif->bss_conf.beacon_int,
  275. ctx_pan->vif->bss_conf.beacon_int);
  276. } else
  277. bcnint = max_t(int, bcnint,
  278. ctx_bss->vif->bss_conf.beacon_int);
  279. if (!bcnint)
  280. bcnint = DEFAULT_BEACON_INTERVAL;
  281. slot0 = bcnint / 2;
  282. slot1 = bcnint - slot0;
  283. if (test_bit(STATUS_SCAN_HW, &priv->status) ||
  284. (!ctx_bss->vif->bss_conf.idle &&
  285. !ctx_bss->vif->bss_conf.assoc)) {
  286. slot0 = bcnint * 3 - 20;
  287. slot1 = 20;
  288. } else if (!ctx_pan->vif->bss_conf.idle &&
  289. !ctx_pan->vif->bss_conf.assoc) {
  290. slot1 = bcnint * 3 - 20;
  291. slot0 = 20;
  292. }
  293. } else if (ctx_pan->vif) {
  294. slot0 = 0;
  295. slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
  296. ctx_pan->vif->bss_conf.beacon_int;
  297. slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
  298. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  299. slot0 = slot1 * 3 - 20;
  300. slot1 = 20;
  301. }
  302. }
  303. cmd.slots[0].width = cpu_to_le16(slot0);
  304. cmd.slots[1].width = cpu_to_le16(slot1);
  305. ret = iwl_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, sizeof(cmd), &cmd);
  306. if (ret)
  307. IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
  308. return ret;
  309. }
  310. struct iwl_hcmd_ops iwlagn_hcmd = {
  311. .rxon_assoc = iwlagn_send_rxon_assoc,
  312. .commit_rxon = iwl_commit_rxon,
  313. .set_rxon_chain = iwl_set_rxon_chain,
  314. .set_tx_ant = iwlagn_send_tx_ant_config,
  315. .send_bt_config = iwl_send_bt_config,
  316. .set_pan_params = iwlagn_set_pan_params,
  317. };
  318. struct iwl_hcmd_ops iwlagn_bt_hcmd = {
  319. .rxon_assoc = iwlagn_send_rxon_assoc,
  320. .commit_rxon = iwl_commit_rxon,
  321. .set_rxon_chain = iwl_set_rxon_chain,
  322. .set_tx_ant = iwlagn_send_tx_ant_config,
  323. .send_bt_config = iwlagn_send_advance_bt_config,
  324. .set_pan_params = iwlagn_set_pan_params,
  325. };
  326. struct iwl_hcmd_utils_ops iwlagn_hcmd_utils = {
  327. .get_hcmd_size = iwlagn_get_hcmd_size,
  328. .build_addsta_hcmd = iwlagn_build_addsta_hcmd,
  329. .gain_computation = iwlagn_gain_computation,
  330. .chain_noise_reset = iwlagn_chain_noise_reset,
  331. .tx_cmd_protection = iwlagn_tx_cmd_protection,
  332. .calc_rssi = iwlagn_calc_rssi,
  333. .request_scan = iwlagn_request_scan,
  334. };