iwl-agn-lib.c 37 KB

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