iwl-agn-lib.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 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/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-io.h"
  36. #include "iwl-agn-hw.h"
  37. #include "iwl-agn.h"
  38. #include "iwl-trans.h"
  39. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  40. {
  41. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  42. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  43. }
  44. int iwlagn_send_tx_power(struct iwl_priv *priv)
  45. {
  46. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  47. u8 tx_ant_cfg_cmd;
  48. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
  49. "TX Power requested while scanning!\n"))
  50. return -EAGAIN;
  51. /* half dBm need to multiply */
  52. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  53. if (priv->tx_power_lmt_in_half_dbm &&
  54. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  55. /*
  56. * For the newer devices which using enhanced/extend tx power
  57. * table in EEPROM, the format is in half dBm. driver need to
  58. * convert to dBm format before report to mac80211.
  59. * By doing so, there is a possibility of 1/2 dBm resolution
  60. * lost. driver will perform "round-up" operation before
  61. * reporting, but it will cause 1/2 dBm tx power over the
  62. * regulatory limit. Perform the checking here, if the
  63. * "tx_power_user_lmt" is higher than EEPROM value (in
  64. * half-dBm format), lower the tx power based on EEPROM
  65. */
  66. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  67. }
  68. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  69. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  70. if (IWL_UCODE_API(priv->fw->ucode_ver) == 1)
  71. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  72. else
  73. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  74. return iwl_dvm_send_cmd_pdu(priv, tx_ant_cfg_cmd, CMD_SYNC,
  75. sizeof(tx_power_cmd), &tx_power_cmd);
  76. }
  77. void iwlagn_temperature(struct iwl_priv *priv)
  78. {
  79. lockdep_assert_held(&priv->statistics.lock);
  80. /* store temperature from correct statistics (in Celsius) */
  81. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  82. iwl_tt_handler(priv);
  83. }
  84. struct iwl_mod_params iwlagn_mod_params = {
  85. .amsdu_size_8K = 1,
  86. .restart_fw = 1,
  87. .plcp_check = true,
  88. .bt_coex_active = true,
  89. .power_level = IWL_POWER_INDEX_1,
  90. .bt_ch_announce = true,
  91. .auto_agg = true,
  92. /* the rest are 0 by default */
  93. };
  94. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  95. {
  96. int idx = 0;
  97. int band_offset = 0;
  98. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  99. if (rate_n_flags & RATE_MCS_HT_MSK) {
  100. idx = (rate_n_flags & 0xff);
  101. return idx;
  102. /* Legacy rate format, search for match in table */
  103. } else {
  104. if (band == IEEE80211_BAND_5GHZ)
  105. band_offset = IWL_FIRST_OFDM_RATE;
  106. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  107. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  108. return idx - band_offset;
  109. }
  110. return -1;
  111. }
  112. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  113. struct ieee80211_vif *vif, bool add)
  114. {
  115. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  116. if (add)
  117. return iwlagn_add_bssid_station(priv, vif_priv->ctx,
  118. vif->bss_conf.bssid,
  119. &vif_priv->ibss_bssid_sta_id);
  120. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  121. vif->bss_conf.bssid);
  122. }
  123. /**
  124. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  125. *
  126. * pre-requirements:
  127. * 1. acquire mutex before calling
  128. * 2. make sure rf is on and not in exit state
  129. */
  130. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  131. {
  132. struct iwl_txfifo_flush_cmd flush_cmd;
  133. struct iwl_host_cmd cmd = {
  134. .id = REPLY_TXFIFO_FLUSH,
  135. .len = { sizeof(struct iwl_txfifo_flush_cmd), },
  136. .flags = CMD_SYNC,
  137. .data = { &flush_cmd, },
  138. };
  139. might_sleep();
  140. memset(&flush_cmd, 0, sizeof(flush_cmd));
  141. if (flush_control & BIT(IWL_RXON_CTX_BSS))
  142. flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
  143. IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
  144. IWL_SCD_MGMT_MSK;
  145. if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
  146. (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
  147. flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
  148. IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
  149. IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
  150. IWL_PAN_SCD_MULTICAST_MSK;
  151. if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
  152. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  153. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  154. flush_cmd.fifo_control);
  155. flush_cmd.flush_control = cpu_to_le16(flush_control);
  156. return iwl_dvm_send_cmd(priv, &cmd);
  157. }
  158. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  159. {
  160. mutex_lock(&priv->mutex);
  161. ieee80211_stop_queues(priv->hw);
  162. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  163. IWL_ERR(priv, "flush request fail\n");
  164. goto done;
  165. }
  166. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  167. iwl_trans_wait_tx_queue_empty(priv->trans);
  168. done:
  169. ieee80211_wake_queues(priv->hw);
  170. mutex_unlock(&priv->mutex);
  171. }
  172. /*
  173. * BT coex
  174. */
  175. /*
  176. * Macros to access the lookup table.
  177. *
  178. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  179. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  180. *
  181. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  182. *
  183. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  184. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  185. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  186. *
  187. * These macros encode that format.
  188. */
  189. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  190. wifi_txrx, wifi_sh_ant_req) \
  191. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  192. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  193. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  194. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  195. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  196. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  197. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  198. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  199. wifi_sh_ant_req))))
  200. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  201. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  202. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  203. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  204. wifi_sh_ant_req))
  205. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  206. wifi_req, wifi_prio, wifi_txrx, \
  207. wifi_sh_ant_req) \
  208. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  209. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  210. wifi_sh_ant_req))
  211. #define LUT_WLAN_KILL_OP(lut, op, val) \
  212. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  213. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  214. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  215. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  216. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  217. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  218. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  219. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  220. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  221. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  222. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  223. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  224. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  225. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  226. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  227. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  228. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  229. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  230. wifi_req, wifi_prio, wifi_txrx, \
  231. wifi_sh_ant_req))))
  232. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  233. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  234. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  235. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  236. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  237. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  238. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  239. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  240. static const __le32 iwlagn_def_3w_lookup[12] = {
  241. cpu_to_le32(0xaaaaaaaa),
  242. cpu_to_le32(0xaaaaaaaa),
  243. cpu_to_le32(0xaeaaaaaa),
  244. cpu_to_le32(0xaaaaaaaa),
  245. cpu_to_le32(0xcc00ff28),
  246. cpu_to_le32(0x0000aaaa),
  247. cpu_to_le32(0xcc00aaaa),
  248. cpu_to_le32(0x0000aaaa),
  249. cpu_to_le32(0xc0004000),
  250. cpu_to_le32(0x00004000),
  251. cpu_to_le32(0xf0005000),
  252. cpu_to_le32(0xf0005000),
  253. };
  254. static const __le32 iwlagn_concurrent_lookup[12] = {
  255. cpu_to_le32(0xaaaaaaaa),
  256. cpu_to_le32(0xaaaaaaaa),
  257. cpu_to_le32(0xaaaaaaaa),
  258. cpu_to_le32(0xaaaaaaaa),
  259. cpu_to_le32(0xaaaaaaaa),
  260. cpu_to_le32(0xaaaaaaaa),
  261. cpu_to_le32(0xaaaaaaaa),
  262. cpu_to_le32(0xaaaaaaaa),
  263. cpu_to_le32(0x00000000),
  264. cpu_to_le32(0x00000000),
  265. cpu_to_le32(0x00000000),
  266. cpu_to_le32(0x00000000),
  267. };
  268. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  269. {
  270. struct iwl_basic_bt_cmd basic = {
  271. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  272. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  273. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  274. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  275. };
  276. struct iwl_bt_cmd_v1 bt_cmd_v1;
  277. struct iwl_bt_cmd_v2 bt_cmd_v2;
  278. int ret;
  279. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  280. sizeof(basic.bt3_lookup_table));
  281. if (priv->cfg->bt_params) {
  282. /*
  283. * newer generation of devices (2000 series and newer)
  284. * use the version 2 of the bt command
  285. * we need to make sure sending the host command
  286. * with correct data structure to avoid uCode assert
  287. */
  288. if (priv->cfg->bt_params->bt_session_2) {
  289. bt_cmd_v2.prio_boost = cpu_to_le32(
  290. priv->cfg->bt_params->bt_prio_boost);
  291. bt_cmd_v2.tx_prio_boost = 0;
  292. bt_cmd_v2.rx_prio_boost = 0;
  293. } else {
  294. bt_cmd_v1.prio_boost =
  295. priv->cfg->bt_params->bt_prio_boost;
  296. bt_cmd_v1.tx_prio_boost = 0;
  297. bt_cmd_v1.rx_prio_boost = 0;
  298. }
  299. } else {
  300. IWL_ERR(priv, "failed to construct BT Coex Config\n");
  301. return;
  302. }
  303. basic.kill_ack_mask = priv->kill_ack_mask;
  304. basic.kill_cts_mask = priv->kill_cts_mask;
  305. basic.valid = priv->bt_valid;
  306. /*
  307. * Configure BT coex mode to "no coexistence" when the
  308. * user disabled BT coexistence, we have no interface
  309. * (might be in monitor mode), or the interface is in
  310. * IBSS mode (no proper uCode support for coex then).
  311. */
  312. if (!iwlagn_mod_params.bt_coex_active ||
  313. priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  314. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
  315. } else {
  316. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  317. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  318. if (!priv->bt_enable_pspoll)
  319. basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  320. else
  321. basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  322. if (priv->bt_ch_announce)
  323. basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  324. IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
  325. }
  326. priv->bt_enable_flag = basic.flags;
  327. if (priv->bt_full_concurrent)
  328. memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
  329. sizeof(iwlagn_concurrent_lookup));
  330. else
  331. memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
  332. sizeof(iwlagn_def_3w_lookup));
  333. IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
  334. basic.flags ? "active" : "disabled",
  335. priv->bt_full_concurrent ?
  336. "full concurrency" : "3-wire");
  337. if (priv->cfg->bt_params->bt_session_2) {
  338. memcpy(&bt_cmd_v2.basic, &basic,
  339. sizeof(basic));
  340. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  341. CMD_SYNC, sizeof(bt_cmd_v2), &bt_cmd_v2);
  342. } else {
  343. memcpy(&bt_cmd_v1.basic, &basic,
  344. sizeof(basic));
  345. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  346. CMD_SYNC, sizeof(bt_cmd_v1), &bt_cmd_v1);
  347. }
  348. if (ret)
  349. IWL_ERR(priv, "failed to send BT Coex Config\n");
  350. }
  351. void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
  352. {
  353. struct iwl_rxon_context *ctx, *found_ctx = NULL;
  354. bool found_ap = false;
  355. lockdep_assert_held(&priv->mutex);
  356. /* Check whether AP or GO mode is active. */
  357. if (rssi_ena) {
  358. for_each_context(priv, ctx) {
  359. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
  360. iwl_is_associated_ctx(ctx)) {
  361. found_ap = true;
  362. break;
  363. }
  364. }
  365. }
  366. /*
  367. * If disable was received or If GO/AP mode, disable RSSI
  368. * measurements.
  369. */
  370. if (!rssi_ena || found_ap) {
  371. if (priv->cur_rssi_ctx) {
  372. ctx = priv->cur_rssi_ctx;
  373. ieee80211_disable_rssi_reports(ctx->vif);
  374. priv->cur_rssi_ctx = NULL;
  375. }
  376. return;
  377. }
  378. /*
  379. * If rssi measurements need to be enabled, consider all cases now.
  380. * Figure out how many contexts are active.
  381. */
  382. for_each_context(priv, ctx) {
  383. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
  384. iwl_is_associated_ctx(ctx)) {
  385. found_ctx = ctx;
  386. break;
  387. }
  388. }
  389. /*
  390. * rssi monitor already enabled for the correct interface...nothing
  391. * to do.
  392. */
  393. if (found_ctx == priv->cur_rssi_ctx)
  394. return;
  395. /*
  396. * Figure out if rssi monitor is currently enabled, and needs
  397. * to be changed. If rssi monitor is already enabled, disable
  398. * it first else just enable rssi measurements on the
  399. * interface found above.
  400. */
  401. if (priv->cur_rssi_ctx) {
  402. ctx = priv->cur_rssi_ctx;
  403. if (ctx->vif)
  404. ieee80211_disable_rssi_reports(ctx->vif);
  405. }
  406. priv->cur_rssi_ctx = found_ctx;
  407. if (!found_ctx)
  408. return;
  409. ieee80211_enable_rssi_reports(found_ctx->vif,
  410. IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
  411. IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
  412. }
  413. static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
  414. {
  415. return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
  416. BT_UART_MSG_FRAME3SCOESCO_POS;
  417. }
  418. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  419. {
  420. struct iwl_priv *priv =
  421. container_of(work, struct iwl_priv, bt_traffic_change_work);
  422. struct iwl_rxon_context *ctx;
  423. int smps_request = -1;
  424. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  425. /* bt coex disabled */
  426. return;
  427. }
  428. /*
  429. * Note: bt_traffic_load can be overridden by scan complete and
  430. * coex profile notifications. Ignore that since only bad consequence
  431. * can be not matching debug print with actual state.
  432. */
  433. IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
  434. priv->bt_traffic_load);
  435. switch (priv->bt_traffic_load) {
  436. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  437. if (priv->bt_status)
  438. smps_request = IEEE80211_SMPS_DYNAMIC;
  439. else
  440. smps_request = IEEE80211_SMPS_AUTOMATIC;
  441. break;
  442. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  443. smps_request = IEEE80211_SMPS_DYNAMIC;
  444. break;
  445. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  446. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  447. smps_request = IEEE80211_SMPS_STATIC;
  448. break;
  449. default:
  450. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  451. priv->bt_traffic_load);
  452. break;
  453. }
  454. mutex_lock(&priv->mutex);
  455. /*
  456. * We can not send command to firmware while scanning. When the scan
  457. * complete we will schedule this work again. We do check with mutex
  458. * locked to prevent new scan request to arrive. We do not check
  459. * STATUS_SCANNING to avoid race when queue_work two times from
  460. * different notifications, but quit and not perform any work at all.
  461. */
  462. if (test_bit(STATUS_SCAN_HW, &priv->status))
  463. goto out;
  464. iwl_update_chain_flags(priv);
  465. if (smps_request != -1) {
  466. priv->current_ht_config.smps = smps_request;
  467. for_each_context(priv, ctx) {
  468. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  469. ieee80211_request_smps(ctx->vif, smps_request);
  470. }
  471. }
  472. /*
  473. * Dynamic PS poll related functionality. Adjust RSSI measurements if
  474. * necessary.
  475. */
  476. iwlagn_bt_coex_rssi_monitor(priv);
  477. out:
  478. mutex_unlock(&priv->mutex);
  479. }
  480. /*
  481. * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
  482. * correct interface or disable it if this is the last interface to be
  483. * removed.
  484. */
  485. void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
  486. {
  487. if (priv->bt_is_sco &&
  488. priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
  489. iwlagn_bt_adjust_rssi_monitor(priv, true);
  490. else
  491. iwlagn_bt_adjust_rssi_monitor(priv, false);
  492. }
  493. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  494. struct iwl_bt_uart_msg *uart_msg)
  495. {
  496. IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
  497. "Update Req = 0x%X\n",
  498. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  499. BT_UART_MSG_FRAME1MSGTYPE_POS,
  500. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  501. BT_UART_MSG_FRAME1SSN_POS,
  502. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  503. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  504. IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  505. "Chl_SeqN = 0x%X, In band = 0x%X\n",
  506. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  507. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  508. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  509. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  510. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  511. BT_UART_MSG_FRAME2CHLSEQN_POS,
  512. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  513. BT_UART_MSG_FRAME2INBAND_POS);
  514. IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  515. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X\n",
  516. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  517. BT_UART_MSG_FRAME3SCOESCO_POS,
  518. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  519. BT_UART_MSG_FRAME3SNIFF_POS,
  520. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  521. BT_UART_MSG_FRAME3A2DP_POS,
  522. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  523. BT_UART_MSG_FRAME3ACL_POS,
  524. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  525. BT_UART_MSG_FRAME3MASTER_POS,
  526. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  527. BT_UART_MSG_FRAME3OBEX_POS);
  528. IWL_DEBUG_COEX(priv, "Idle duration = 0x%X\n",
  529. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  530. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  531. IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  532. "eSCO Retransmissions = 0x%X\n",
  533. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  534. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  535. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  536. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  537. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  538. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  539. IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X\n",
  540. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  541. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  542. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  543. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  544. IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
  545. "0x%X, Inquiry = 0x%X, Connectable = 0x%X\n",
  546. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  547. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  548. (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
  549. BT_UART_MSG_FRAME7PAGE_POS,
  550. (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
  551. BT_UART_MSG_FRAME7INQUIRY_POS,
  552. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  553. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  554. }
  555. static void iwlagn_set_kill_msk(struct iwl_priv *priv,
  556. struct iwl_bt_uart_msg *uart_msg)
  557. {
  558. u8 kill_msk;
  559. static const __le32 bt_kill_ack_msg[2] = {
  560. IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
  561. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  562. static const __le32 bt_kill_cts_msg[2] = {
  563. IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
  564. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  565. kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
  566. ? 1 : 0;
  567. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
  568. priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
  569. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  570. priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
  571. priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
  572. priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
  573. /* schedule to send runtime bt_config */
  574. queue_work(priv->workqueue, &priv->bt_runtime_config);
  575. }
  576. }
  577. int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  578. struct iwl_rx_cmd_buffer *rxb,
  579. struct iwl_device_cmd *cmd)
  580. {
  581. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  582. struct iwl_bt_coex_profile_notif *coex = (void *)pkt->data;
  583. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  584. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  585. /* bt coex disabled */
  586. return 0;
  587. }
  588. IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
  589. IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
  590. IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
  591. IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
  592. coex->bt_ci_compliance);
  593. iwlagn_print_uartmsg(priv, uart_msg);
  594. priv->last_bt_traffic_load = priv->bt_traffic_load;
  595. priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
  596. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  597. if (priv->bt_status != coex->bt_status ||
  598. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  599. if (coex->bt_status) {
  600. /* BT on */
  601. if (!priv->bt_ch_announce)
  602. priv->bt_traffic_load =
  603. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  604. else
  605. priv->bt_traffic_load =
  606. coex->bt_traffic_load;
  607. } else {
  608. /* BT off */
  609. priv->bt_traffic_load =
  610. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  611. }
  612. priv->bt_status = coex->bt_status;
  613. queue_work(priv->workqueue,
  614. &priv->bt_traffic_change_work);
  615. }
  616. }
  617. iwlagn_set_kill_msk(priv, uart_msg);
  618. /* FIXME: based on notification, adjust the prio_boost */
  619. priv->bt_ci_compliance = coex->bt_ci_compliance;
  620. return 0;
  621. }
  622. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  623. {
  624. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  625. iwlagn_bt_coex_profile_notif;
  626. }
  627. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  628. {
  629. INIT_WORK(&priv->bt_traffic_change_work,
  630. iwlagn_bt_traffic_change_work);
  631. }
  632. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  633. {
  634. cancel_work_sync(&priv->bt_traffic_change_work);
  635. }
  636. static bool is_single_rx_stream(struct iwl_priv *priv)
  637. {
  638. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  639. priv->current_ht_config.single_chain_sufficient;
  640. }
  641. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  642. #define IWL_NUM_RX_CHAINS_SINGLE 2
  643. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  644. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  645. /*
  646. * Determine how many receiver/antenna chains to use.
  647. *
  648. * More provides better reception via diversity. Fewer saves power
  649. * at the expense of throughput, but only when not in powersave to
  650. * start with.
  651. *
  652. * MIMO (dual stream) requires at least 2, but works better with 3.
  653. * This does not determine *which* chains to use, just how many.
  654. */
  655. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  656. {
  657. if (priv->cfg->bt_params &&
  658. priv->cfg->bt_params->advanced_bt_coexist &&
  659. (priv->bt_full_concurrent ||
  660. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  661. /*
  662. * only use chain 'A' in bt high traffic load or
  663. * full concurrency mode
  664. */
  665. return IWL_NUM_RX_CHAINS_SINGLE;
  666. }
  667. /* # of Rx chains to use when expecting MIMO. */
  668. if (is_single_rx_stream(priv))
  669. return IWL_NUM_RX_CHAINS_SINGLE;
  670. else
  671. return IWL_NUM_RX_CHAINS_MULTIPLE;
  672. }
  673. /*
  674. * When we are in power saving mode, unless device support spatial
  675. * multiplexing power save, use the active count for rx chain count.
  676. */
  677. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  678. {
  679. /* # Rx chains when idling, depending on SMPS mode */
  680. switch (priv->current_ht_config.smps) {
  681. case IEEE80211_SMPS_STATIC:
  682. case IEEE80211_SMPS_DYNAMIC:
  683. return IWL_NUM_IDLE_CHAINS_SINGLE;
  684. case IEEE80211_SMPS_AUTOMATIC:
  685. case IEEE80211_SMPS_OFF:
  686. return active_cnt;
  687. default:
  688. WARN(1, "invalid SMPS mode %d",
  689. priv->current_ht_config.smps);
  690. return active_cnt;
  691. }
  692. }
  693. /* up to 4 chains */
  694. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  695. {
  696. u8 res;
  697. res = (chain_bitmap & BIT(0)) >> 0;
  698. res += (chain_bitmap & BIT(1)) >> 1;
  699. res += (chain_bitmap & BIT(2)) >> 2;
  700. res += (chain_bitmap & BIT(3)) >> 3;
  701. return res;
  702. }
  703. /**
  704. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  705. *
  706. * Selects how many and which Rx receivers/antennas/chains to use.
  707. * This should not be used for scan command ... it puts data in wrong place.
  708. */
  709. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  710. {
  711. bool is_single = is_single_rx_stream(priv);
  712. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  713. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  714. u32 active_chains;
  715. u16 rx_chain;
  716. /* Tell uCode which antennas are actually connected.
  717. * Before first association, we assume all antennas are connected.
  718. * Just after first association, iwl_chain_noise_calibration()
  719. * checks which antennas actually *are* connected. */
  720. if (priv->chain_noise_data.active_chains)
  721. active_chains = priv->chain_noise_data.active_chains;
  722. else
  723. active_chains = priv->hw_params.valid_rx_ant;
  724. if (priv->cfg->bt_params &&
  725. priv->cfg->bt_params->advanced_bt_coexist &&
  726. (priv->bt_full_concurrent ||
  727. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  728. /*
  729. * only use chain 'A' in bt high traffic load or
  730. * full concurrency mode
  731. */
  732. active_chains = first_antenna(active_chains);
  733. }
  734. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  735. /* How many receivers should we use? */
  736. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  737. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  738. /* correct rx chain count according hw settings
  739. * and chain noise calibration
  740. */
  741. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  742. if (valid_rx_cnt < active_rx_cnt)
  743. active_rx_cnt = valid_rx_cnt;
  744. if (valid_rx_cnt < idle_rx_cnt)
  745. idle_rx_cnt = valid_rx_cnt;
  746. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  747. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  748. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  749. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  750. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  751. else
  752. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  753. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  754. ctx->staging.rx_chain,
  755. active_rx_cnt, idle_rx_cnt);
  756. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  757. active_rx_cnt < idle_rx_cnt);
  758. }
  759. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  760. {
  761. int i;
  762. u8 ind = ant;
  763. if (priv->band == IEEE80211_BAND_2GHZ &&
  764. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  765. return 0;
  766. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  767. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  768. if (valid & BIT(ind))
  769. return ind;
  770. }
  771. return ant;
  772. }
  773. #ifdef CONFIG_PM_SLEEP
  774. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  775. {
  776. int i;
  777. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  778. out[i] = cpu_to_le16(p1k[i]);
  779. }
  780. struct wowlan_key_data {
  781. struct iwl_rxon_context *ctx;
  782. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  783. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  784. const u8 *bssid;
  785. bool error, use_rsc_tsc, use_tkip;
  786. };
  787. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  788. struct ieee80211_vif *vif,
  789. struct ieee80211_sta *sta,
  790. struct ieee80211_key_conf *key,
  791. void *_data)
  792. {
  793. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  794. struct wowlan_key_data *data = _data;
  795. struct iwl_rxon_context *ctx = data->ctx;
  796. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  797. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  798. struct iwlagn_p1k_cache *rx_p1ks;
  799. u8 *rx_mic_key;
  800. struct ieee80211_key_seq seq;
  801. u32 cur_rx_iv32 = 0;
  802. u16 p1k[IWLAGN_P1K_SIZE];
  803. int ret, i;
  804. mutex_lock(&priv->mutex);
  805. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  806. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  807. !sta && !ctx->key_mapping_keys)
  808. ret = iwl_set_default_wep_key(priv, ctx, key);
  809. else
  810. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  811. if (ret) {
  812. IWL_ERR(priv, "Error setting key during suspend!\n");
  813. data->error = true;
  814. }
  815. switch (key->cipher) {
  816. case WLAN_CIPHER_SUITE_TKIP:
  817. if (sta) {
  818. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  819. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  820. rx_p1ks = data->tkip->rx_uni;
  821. ieee80211_get_key_tx_seq(key, &seq);
  822. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  823. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  824. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  825. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  826. memcpy(data->tkip->mic_keys.tx,
  827. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  828. IWLAGN_MIC_KEY_SIZE);
  829. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  830. } else {
  831. tkip_sc =
  832. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  833. rx_p1ks = data->tkip->rx_multi;
  834. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  835. }
  836. /*
  837. * For non-QoS this relies on the fact that both the uCode and
  838. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  839. * for checking the IV in the frames.
  840. */
  841. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  842. ieee80211_get_key_rx_seq(key, i, &seq);
  843. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  844. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  845. /* wrapping isn't allowed, AP must rekey */
  846. if (seq.tkip.iv32 > cur_rx_iv32)
  847. cur_rx_iv32 = seq.tkip.iv32;
  848. }
  849. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  850. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  851. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  852. cur_rx_iv32 + 1, p1k);
  853. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  854. memcpy(rx_mic_key,
  855. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  856. IWLAGN_MIC_KEY_SIZE);
  857. data->use_tkip = true;
  858. data->use_rsc_tsc = true;
  859. break;
  860. case WLAN_CIPHER_SUITE_CCMP:
  861. if (sta) {
  862. u8 *pn = seq.ccmp.pn;
  863. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  864. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  865. ieee80211_get_key_tx_seq(key, &seq);
  866. aes_tx_sc->pn = cpu_to_le64(
  867. (u64)pn[5] |
  868. ((u64)pn[4] << 8) |
  869. ((u64)pn[3] << 16) |
  870. ((u64)pn[2] << 24) |
  871. ((u64)pn[1] << 32) |
  872. ((u64)pn[0] << 40));
  873. } else
  874. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  875. /*
  876. * For non-QoS this relies on the fact that both the uCode and
  877. * mac80211 use TID 0 for checking the IV in the frames.
  878. */
  879. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  880. u8 *pn = seq.ccmp.pn;
  881. ieee80211_get_key_rx_seq(key, i, &seq);
  882. aes_sc->pn = cpu_to_le64(
  883. (u64)pn[5] |
  884. ((u64)pn[4] << 8) |
  885. ((u64)pn[3] << 16) |
  886. ((u64)pn[2] << 24) |
  887. ((u64)pn[1] << 32) |
  888. ((u64)pn[0] << 40));
  889. }
  890. data->use_rsc_tsc = true;
  891. break;
  892. }
  893. mutex_unlock(&priv->mutex);
  894. }
  895. int iwlagn_send_patterns(struct iwl_priv *priv,
  896. struct cfg80211_wowlan *wowlan)
  897. {
  898. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  899. struct iwl_host_cmd cmd = {
  900. .id = REPLY_WOWLAN_PATTERNS,
  901. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  902. .flags = CMD_SYNC,
  903. };
  904. int i, err;
  905. if (!wowlan->n_patterns)
  906. return 0;
  907. cmd.len[0] = sizeof(*pattern_cmd) +
  908. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  909. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  910. if (!pattern_cmd)
  911. return -ENOMEM;
  912. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  913. for (i = 0; i < wowlan->n_patterns; i++) {
  914. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  915. memcpy(&pattern_cmd->patterns[i].mask,
  916. wowlan->patterns[i].mask, mask_len);
  917. memcpy(&pattern_cmd->patterns[i].pattern,
  918. wowlan->patterns[i].pattern,
  919. wowlan->patterns[i].pattern_len);
  920. pattern_cmd->patterns[i].mask_size = mask_len;
  921. pattern_cmd->patterns[i].pattern_size =
  922. wowlan->patterns[i].pattern_len;
  923. }
  924. cmd.data[0] = pattern_cmd;
  925. err = iwl_dvm_send_cmd(priv, &cmd);
  926. kfree(pattern_cmd);
  927. return err;
  928. }
  929. int iwlagn_suspend(struct iwl_priv *priv, struct cfg80211_wowlan *wowlan)
  930. {
  931. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  932. struct iwl_rxon_cmd rxon;
  933. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  934. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  935. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  936. struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
  937. struct wowlan_key_data key_data = {
  938. .ctx = ctx,
  939. .bssid = ctx->active.bssid_addr,
  940. .use_rsc_tsc = false,
  941. .tkip = &tkip_cmd,
  942. .use_tkip = false,
  943. };
  944. int ret, i;
  945. u16 seq;
  946. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  947. if (!key_data.rsc_tsc)
  948. return -ENOMEM;
  949. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  950. /*
  951. * We know the last used seqno, and the uCode expects to know that
  952. * one, it will increment before TX.
  953. */
  954. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  955. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  956. /*
  957. * For QoS counters, we store the one to use next, so subtract 0x10
  958. * since the uCode will add 0x10 before using the value.
  959. */
  960. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  961. seq = priv->tid_data[IWL_AP_ID][i].seq_number;
  962. seq -= 0x10;
  963. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  964. }
  965. if (wowlan->disconnect)
  966. wakeup_filter_cmd.enabled |=
  967. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  968. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  969. if (wowlan->magic_pkt)
  970. wakeup_filter_cmd.enabled |=
  971. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  972. if (wowlan->gtk_rekey_failure)
  973. wakeup_filter_cmd.enabled |=
  974. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  975. if (wowlan->eap_identity_req)
  976. wakeup_filter_cmd.enabled |=
  977. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  978. if (wowlan->four_way_handshake)
  979. wakeup_filter_cmd.enabled |=
  980. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  981. if (wowlan->n_patterns)
  982. wakeup_filter_cmd.enabled |=
  983. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  984. if (wowlan->rfkill_release)
  985. d3_cfg_cmd.wakeup_flags |=
  986. cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
  987. iwl_scan_cancel_timeout(priv, 200);
  988. memcpy(&rxon, &ctx->active, sizeof(rxon));
  989. priv->ucode_loaded = false;
  990. iwl_trans_stop_device(priv->trans);
  991. priv->wowlan = true;
  992. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
  993. if (ret)
  994. goto out;
  995. /* now configure WoWLAN ucode */
  996. ret = iwl_alive_start(priv);
  997. if (ret)
  998. goto out;
  999. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  1000. ret = iwlagn_commit_rxon(priv, ctx);
  1001. if (ret)
  1002. goto out;
  1003. ret = iwl_power_update_mode(priv, true);
  1004. if (ret)
  1005. goto out;
  1006. if (!iwlagn_mod_params.sw_crypto) {
  1007. /* mark all keys clear */
  1008. priv->ucode_key_table = 0;
  1009. ctx->key_mapping_keys = 0;
  1010. /*
  1011. * This needs to be unlocked due to lock ordering
  1012. * constraints. Since we're in the suspend path
  1013. * that isn't really a problem though.
  1014. */
  1015. mutex_unlock(&priv->mutex);
  1016. ieee80211_iter_keys(priv->hw, ctx->vif,
  1017. iwlagn_wowlan_program_keys,
  1018. &key_data);
  1019. mutex_lock(&priv->mutex);
  1020. if (key_data.error) {
  1021. ret = -EIO;
  1022. goto out;
  1023. }
  1024. if (key_data.use_rsc_tsc) {
  1025. struct iwl_host_cmd rsc_tsc_cmd = {
  1026. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  1027. .flags = CMD_SYNC,
  1028. .data[0] = key_data.rsc_tsc,
  1029. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1030. .len[0] = sizeof(*key_data.rsc_tsc),
  1031. };
  1032. ret = iwl_dvm_send_cmd(priv, &rsc_tsc_cmd);
  1033. if (ret)
  1034. goto out;
  1035. }
  1036. if (key_data.use_tkip) {
  1037. ret = iwl_dvm_send_cmd_pdu(priv,
  1038. REPLY_WOWLAN_TKIP_PARAMS,
  1039. CMD_SYNC, sizeof(tkip_cmd),
  1040. &tkip_cmd);
  1041. if (ret)
  1042. goto out;
  1043. }
  1044. if (priv->have_rekey_data) {
  1045. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1046. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  1047. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1048. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  1049. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1050. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  1051. ret = iwl_dvm_send_cmd_pdu(priv,
  1052. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  1053. CMD_SYNC, sizeof(kek_kck_cmd),
  1054. &kek_kck_cmd);
  1055. if (ret)
  1056. goto out;
  1057. }
  1058. }
  1059. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_D3_CONFIG, CMD_SYNC,
  1060. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  1061. if (ret)
  1062. goto out;
  1063. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_WAKEUP_FILTER,
  1064. CMD_SYNC, sizeof(wakeup_filter_cmd),
  1065. &wakeup_filter_cmd);
  1066. if (ret)
  1067. goto out;
  1068. ret = iwlagn_send_patterns(priv, wowlan);
  1069. out:
  1070. kfree(key_data.rsc_tsc);
  1071. return ret;
  1072. }
  1073. #endif
  1074. int iwl_dvm_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  1075. {
  1076. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  1077. IWL_WARN(priv, "Not sending command - %s KILL\n",
  1078. iwl_is_rfkill(priv) ? "RF" : "CT");
  1079. return -EIO;
  1080. }
  1081. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  1082. IWL_ERR(priv, "Command %s failed: FW Error\n",
  1083. iwl_dvm_get_cmd_string(cmd->id));
  1084. return -EIO;
  1085. }
  1086. /*
  1087. * Synchronous commands from this op-mode must hold
  1088. * the mutex, this ensures we don't try to send two
  1089. * (or more) synchronous commands at a time.
  1090. */
  1091. if (cmd->flags & CMD_SYNC)
  1092. lockdep_assert_held(&priv->mutex);
  1093. if (priv->ucode_owner == IWL_OWNERSHIP_TM &&
  1094. !(cmd->flags & CMD_ON_DEMAND)) {
  1095. IWL_DEBUG_HC(priv, "tm own the uCode, no regular hcmd send\n");
  1096. return -EIO;
  1097. }
  1098. return iwl_trans_send_cmd(priv->trans, cmd);
  1099. }
  1100. int iwl_dvm_send_cmd_pdu(struct iwl_priv *priv, u8 id,
  1101. u32 flags, u16 len, const void *data)
  1102. {
  1103. struct iwl_host_cmd cmd = {
  1104. .id = id,
  1105. .len = { len, },
  1106. .data = { data, },
  1107. .flags = flags,
  1108. };
  1109. return iwl_dvm_send_cmd(priv, &cmd);
  1110. }