iwl-agn-lib.c 37 KB

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