iwl-agn-lib.c 37 KB

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