iwl-agn-lib.c 38 KB

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