iwl-agn-lib.c 37 KB

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