iwl-agn-lib.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-agn-hw.h"
  38. #include "iwl-agn.h"
  39. #include "iwl-trans.h"
  40. #include "iwl-shared.h"
  41. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  42. {
  43. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  44. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  45. }
  46. int iwlagn_send_tx_power(struct iwl_priv *priv)
  47. {
  48. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  49. u8 tx_ant_cfg_cmd;
  50. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
  51. "TX Power requested while scanning!\n"))
  52. return -EAGAIN;
  53. /* half dBm need to multiply */
  54. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  55. if (priv->tx_power_lmt_in_half_dbm &&
  56. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  57. /*
  58. * For the newer devices which using enhanced/extend tx power
  59. * table in EEPROM, the format is in half dBm. driver need to
  60. * convert to dBm format before report to mac80211.
  61. * By doing so, there is a possibility of 1/2 dBm resolution
  62. * lost. driver will perform "round-up" operation before
  63. * reporting, but it will cause 1/2 dBm tx power over the
  64. * regulatory limit. Perform the checking here, if the
  65. * "tx_power_user_lmt" is higher than EEPROM value (in
  66. * half-dBm format), lower the tx power based on EEPROM
  67. */
  68. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  69. }
  70. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  71. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  72. if (IWL_UCODE_API(priv->fw->ucode_ver) == 1)
  73. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  74. else
  75. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  76. return iwl_dvm_send_cmd_pdu(priv, tx_ant_cfg_cmd, CMD_SYNC,
  77. sizeof(tx_power_cmd), &tx_power_cmd);
  78. }
  79. void iwlagn_temperature(struct iwl_priv *priv)
  80. {
  81. lockdep_assert_held(&priv->statistics.lock);
  82. /* store temperature from correct statistics (in Celsius) */
  83. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  84. iwl_tt_handler(priv);
  85. }
  86. 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 (hw_params(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_dvm_send_cmd(priv, &cmd);
  215. }
  216. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  217. {
  218. mutex_lock(&priv->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->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_dvm_send_cmd_pdu(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_dvm_send_cmd_pdu(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->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->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->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->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_cmd_buffer *rxb,
  631. struct iwl_device_cmd *cmd)
  632. {
  633. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  634. struct iwl_bt_coex_profile_notif *coex = (void *)pkt->data;
  635. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  636. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  637. /* bt coex disabled */
  638. return 0;
  639. }
  640. IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
  641. IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
  642. IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
  643. IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
  644. coex->bt_ci_compliance);
  645. iwlagn_print_uartmsg(priv, uart_msg);
  646. priv->last_bt_traffic_load = priv->bt_traffic_load;
  647. priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
  648. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  649. if (priv->bt_status != coex->bt_status ||
  650. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  651. if (coex->bt_status) {
  652. /* BT on */
  653. if (!priv->bt_ch_announce)
  654. priv->bt_traffic_load =
  655. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  656. else
  657. priv->bt_traffic_load =
  658. coex->bt_traffic_load;
  659. } else {
  660. /* BT off */
  661. priv->bt_traffic_load =
  662. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  663. }
  664. priv->bt_status = coex->bt_status;
  665. queue_work(priv->workqueue,
  666. &priv->bt_traffic_change_work);
  667. }
  668. }
  669. iwlagn_set_kill_msk(priv, uart_msg);
  670. /* FIXME: based on notification, adjust the prio_boost */
  671. priv->bt_ci_compliance = coex->bt_ci_compliance;
  672. return 0;
  673. }
  674. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  675. {
  676. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  677. iwlagn_bt_coex_profile_notif;
  678. }
  679. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  680. {
  681. INIT_WORK(&priv->bt_traffic_change_work,
  682. iwlagn_bt_traffic_change_work);
  683. }
  684. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  685. {
  686. cancel_work_sync(&priv->bt_traffic_change_work);
  687. }
  688. static bool is_single_rx_stream(struct iwl_priv *priv)
  689. {
  690. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  691. priv->current_ht_config.single_chain_sufficient;
  692. }
  693. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  694. #define IWL_NUM_RX_CHAINS_SINGLE 2
  695. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  696. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  697. /*
  698. * Determine how many receiver/antenna chains to use.
  699. *
  700. * More provides better reception via diversity. Fewer saves power
  701. * at the expense of throughput, but only when not in powersave to
  702. * start with.
  703. *
  704. * MIMO (dual stream) requires at least 2, but works better with 3.
  705. * This does not determine *which* chains to use, just how many.
  706. */
  707. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  708. {
  709. if (cfg(priv)->bt_params &&
  710. cfg(priv)->bt_params->advanced_bt_coexist &&
  711. (priv->bt_full_concurrent ||
  712. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  713. /*
  714. * only use chain 'A' in bt high traffic load or
  715. * full concurrency mode
  716. */
  717. return IWL_NUM_RX_CHAINS_SINGLE;
  718. }
  719. /* # of Rx chains to use when expecting MIMO. */
  720. if (is_single_rx_stream(priv))
  721. return IWL_NUM_RX_CHAINS_SINGLE;
  722. else
  723. return IWL_NUM_RX_CHAINS_MULTIPLE;
  724. }
  725. /*
  726. * When we are in power saving mode, unless device support spatial
  727. * multiplexing power save, use the active count for rx chain count.
  728. */
  729. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  730. {
  731. /* # Rx chains when idling, depending on SMPS mode */
  732. switch (priv->current_ht_config.smps) {
  733. case IEEE80211_SMPS_STATIC:
  734. case IEEE80211_SMPS_DYNAMIC:
  735. return IWL_NUM_IDLE_CHAINS_SINGLE;
  736. case IEEE80211_SMPS_AUTOMATIC:
  737. case IEEE80211_SMPS_OFF:
  738. return active_cnt;
  739. default:
  740. WARN(1, "invalid SMPS mode %d",
  741. priv->current_ht_config.smps);
  742. return active_cnt;
  743. }
  744. }
  745. /* up to 4 chains */
  746. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  747. {
  748. u8 res;
  749. res = (chain_bitmap & BIT(0)) >> 0;
  750. res += (chain_bitmap & BIT(1)) >> 1;
  751. res += (chain_bitmap & BIT(2)) >> 2;
  752. res += (chain_bitmap & BIT(3)) >> 3;
  753. return res;
  754. }
  755. /**
  756. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  757. *
  758. * Selects how many and which Rx receivers/antennas/chains to use.
  759. * This should not be used for scan command ... it puts data in wrong place.
  760. */
  761. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  762. {
  763. bool is_single = is_single_rx_stream(priv);
  764. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
  765. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  766. u32 active_chains;
  767. u16 rx_chain;
  768. /* Tell uCode which antennas are actually connected.
  769. * Before first association, we assume all antennas are connected.
  770. * Just after first association, iwl_chain_noise_calibration()
  771. * checks which antennas actually *are* connected. */
  772. if (priv->chain_noise_data.active_chains)
  773. active_chains = priv->chain_noise_data.active_chains;
  774. else
  775. active_chains = hw_params(priv).valid_rx_ant;
  776. if (cfg(priv)->bt_params &&
  777. cfg(priv)->bt_params->advanced_bt_coexist &&
  778. (priv->bt_full_concurrent ||
  779. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  780. /*
  781. * only use chain 'A' in bt high traffic load or
  782. * full concurrency mode
  783. */
  784. active_chains = first_antenna(active_chains);
  785. }
  786. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  787. /* How many receivers should we use? */
  788. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  789. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  790. /* correct rx chain count according hw settings
  791. * and chain noise calibration
  792. */
  793. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  794. if (valid_rx_cnt < active_rx_cnt)
  795. active_rx_cnt = valid_rx_cnt;
  796. if (valid_rx_cnt < idle_rx_cnt)
  797. idle_rx_cnt = valid_rx_cnt;
  798. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  799. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  800. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  801. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  802. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  803. else
  804. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  805. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  806. ctx->staging.rx_chain,
  807. active_rx_cnt, idle_rx_cnt);
  808. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  809. active_rx_cnt < idle_rx_cnt);
  810. }
  811. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  812. {
  813. int i;
  814. u8 ind = ant;
  815. if (priv->band == IEEE80211_BAND_2GHZ &&
  816. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  817. return 0;
  818. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  819. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  820. if (valid & BIT(ind))
  821. return ind;
  822. }
  823. return ant;
  824. }
  825. #ifdef CONFIG_PM_SLEEP
  826. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  827. {
  828. int i;
  829. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  830. out[i] = cpu_to_le16(p1k[i]);
  831. }
  832. struct wowlan_key_data {
  833. struct iwl_rxon_context *ctx;
  834. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  835. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  836. const u8 *bssid;
  837. bool error, use_rsc_tsc, use_tkip;
  838. };
  839. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  840. struct ieee80211_vif *vif,
  841. struct ieee80211_sta *sta,
  842. struct ieee80211_key_conf *key,
  843. void *_data)
  844. {
  845. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  846. struct wowlan_key_data *data = _data;
  847. struct iwl_rxon_context *ctx = data->ctx;
  848. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  849. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  850. struct iwlagn_p1k_cache *rx_p1ks;
  851. u8 *rx_mic_key;
  852. struct ieee80211_key_seq seq;
  853. u32 cur_rx_iv32 = 0;
  854. u16 p1k[IWLAGN_P1K_SIZE];
  855. int ret, i;
  856. mutex_lock(&priv->mutex);
  857. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  858. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  859. !sta && !ctx->key_mapping_keys)
  860. ret = iwl_set_default_wep_key(priv, ctx, key);
  861. else
  862. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  863. if (ret) {
  864. IWL_ERR(priv, "Error setting key during suspend!\n");
  865. data->error = true;
  866. }
  867. switch (key->cipher) {
  868. case WLAN_CIPHER_SUITE_TKIP:
  869. if (sta) {
  870. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  871. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  872. rx_p1ks = data->tkip->rx_uni;
  873. ieee80211_get_key_tx_seq(key, &seq);
  874. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  875. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  876. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  877. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  878. memcpy(data->tkip->mic_keys.tx,
  879. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  880. IWLAGN_MIC_KEY_SIZE);
  881. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  882. } else {
  883. tkip_sc =
  884. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  885. rx_p1ks = data->tkip->rx_multi;
  886. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  887. }
  888. /*
  889. * For non-QoS this relies on the fact that both the uCode and
  890. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  891. * for checking the IV in the frames.
  892. */
  893. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  894. ieee80211_get_key_rx_seq(key, i, &seq);
  895. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  896. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  897. /* wrapping isn't allowed, AP must rekey */
  898. if (seq.tkip.iv32 > cur_rx_iv32)
  899. cur_rx_iv32 = seq.tkip.iv32;
  900. }
  901. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  902. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  903. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  904. cur_rx_iv32 + 1, p1k);
  905. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  906. memcpy(rx_mic_key,
  907. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  908. IWLAGN_MIC_KEY_SIZE);
  909. data->use_tkip = true;
  910. data->use_rsc_tsc = true;
  911. break;
  912. case WLAN_CIPHER_SUITE_CCMP:
  913. if (sta) {
  914. u8 *pn = seq.ccmp.pn;
  915. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  916. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  917. ieee80211_get_key_tx_seq(key, &seq);
  918. aes_tx_sc->pn = cpu_to_le64(
  919. (u64)pn[5] |
  920. ((u64)pn[4] << 8) |
  921. ((u64)pn[3] << 16) |
  922. ((u64)pn[2] << 24) |
  923. ((u64)pn[1] << 32) |
  924. ((u64)pn[0] << 40));
  925. } else
  926. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  927. /*
  928. * For non-QoS this relies on the fact that both the uCode and
  929. * mac80211 use TID 0 for checking the IV in the frames.
  930. */
  931. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  932. u8 *pn = seq.ccmp.pn;
  933. ieee80211_get_key_rx_seq(key, i, &seq);
  934. aes_sc[i].pn = cpu_to_le64(
  935. (u64)pn[5] |
  936. ((u64)pn[4] << 8) |
  937. ((u64)pn[3] << 16) |
  938. ((u64)pn[2] << 24) |
  939. ((u64)pn[1] << 32) |
  940. ((u64)pn[0] << 40));
  941. }
  942. data->use_rsc_tsc = true;
  943. break;
  944. }
  945. mutex_unlock(&priv->mutex);
  946. }
  947. int iwlagn_send_patterns(struct iwl_priv *priv,
  948. struct cfg80211_wowlan *wowlan)
  949. {
  950. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  951. struct iwl_host_cmd cmd = {
  952. .id = REPLY_WOWLAN_PATTERNS,
  953. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  954. .flags = CMD_SYNC,
  955. };
  956. int i, err;
  957. if (!wowlan->n_patterns)
  958. return 0;
  959. cmd.len[0] = sizeof(*pattern_cmd) +
  960. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  961. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  962. if (!pattern_cmd)
  963. return -ENOMEM;
  964. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  965. for (i = 0; i < wowlan->n_patterns; i++) {
  966. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  967. memcpy(&pattern_cmd->patterns[i].mask,
  968. wowlan->patterns[i].mask, mask_len);
  969. memcpy(&pattern_cmd->patterns[i].pattern,
  970. wowlan->patterns[i].pattern,
  971. wowlan->patterns[i].pattern_len);
  972. pattern_cmd->patterns[i].mask_size = mask_len;
  973. pattern_cmd->patterns[i].pattern_size =
  974. wowlan->patterns[i].pattern_len;
  975. }
  976. cmd.data[0] = pattern_cmd;
  977. err = iwl_dvm_send_cmd(priv, &cmd);
  978. kfree(pattern_cmd);
  979. return err;
  980. }
  981. int iwlagn_suspend(struct iwl_priv *priv, struct cfg80211_wowlan *wowlan)
  982. {
  983. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  984. struct iwl_rxon_cmd rxon;
  985. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  986. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  987. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  988. struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
  989. struct wowlan_key_data key_data = {
  990. .ctx = ctx,
  991. .bssid = ctx->active.bssid_addr,
  992. .use_rsc_tsc = false,
  993. .tkip = &tkip_cmd,
  994. .use_tkip = false,
  995. };
  996. int ret, i;
  997. u16 seq;
  998. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  999. if (!key_data.rsc_tsc)
  1000. return -ENOMEM;
  1001. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  1002. /*
  1003. * We know the last used seqno, and the uCode expects to know that
  1004. * one, it will increment before TX.
  1005. */
  1006. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  1007. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  1008. /*
  1009. * For QoS counters, we store the one to use next, so subtract 0x10
  1010. * since the uCode will add 0x10 before using the value.
  1011. */
  1012. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1013. seq = priv->tid_data[IWL_AP_ID][i].seq_number;
  1014. seq -= 0x10;
  1015. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  1016. }
  1017. if (wowlan->disconnect)
  1018. wakeup_filter_cmd.enabled |=
  1019. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  1020. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  1021. if (wowlan->magic_pkt)
  1022. wakeup_filter_cmd.enabled |=
  1023. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  1024. if (wowlan->gtk_rekey_failure)
  1025. wakeup_filter_cmd.enabled |=
  1026. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  1027. if (wowlan->eap_identity_req)
  1028. wakeup_filter_cmd.enabled |=
  1029. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  1030. if (wowlan->four_way_handshake)
  1031. wakeup_filter_cmd.enabled |=
  1032. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  1033. if (wowlan->n_patterns)
  1034. wakeup_filter_cmd.enabled |=
  1035. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  1036. if (wowlan->rfkill_release)
  1037. d3_cfg_cmd.wakeup_flags |=
  1038. cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
  1039. iwl_scan_cancel_timeout(priv, 200);
  1040. memcpy(&rxon, &ctx->active, sizeof(rxon));
  1041. priv->ucode_loaded = false;
  1042. iwl_trans_stop_device(trans(priv));
  1043. priv->wowlan = true;
  1044. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
  1045. if (ret)
  1046. goto out;
  1047. /* now configure WoWLAN ucode */
  1048. ret = iwl_alive_start(priv);
  1049. if (ret)
  1050. goto out;
  1051. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  1052. ret = iwlagn_commit_rxon(priv, ctx);
  1053. if (ret)
  1054. goto out;
  1055. ret = iwl_power_update_mode(priv, true);
  1056. if (ret)
  1057. goto out;
  1058. if (!iwlagn_mod_params.sw_crypto) {
  1059. /* mark all keys clear */
  1060. priv->ucode_key_table = 0;
  1061. ctx->key_mapping_keys = 0;
  1062. /*
  1063. * This needs to be unlocked due to lock ordering
  1064. * constraints. Since we're in the suspend path
  1065. * that isn't really a problem though.
  1066. */
  1067. mutex_unlock(&priv->mutex);
  1068. ieee80211_iter_keys(priv->hw, ctx->vif,
  1069. iwlagn_wowlan_program_keys,
  1070. &key_data);
  1071. mutex_lock(&priv->mutex);
  1072. if (key_data.error) {
  1073. ret = -EIO;
  1074. goto out;
  1075. }
  1076. if (key_data.use_rsc_tsc) {
  1077. struct iwl_host_cmd rsc_tsc_cmd = {
  1078. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  1079. .flags = CMD_SYNC,
  1080. .data[0] = key_data.rsc_tsc,
  1081. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1082. .len[0] = sizeof(*key_data.rsc_tsc),
  1083. };
  1084. ret = iwl_dvm_send_cmd(priv, &rsc_tsc_cmd);
  1085. if (ret)
  1086. goto out;
  1087. }
  1088. if (key_data.use_tkip) {
  1089. ret = iwl_dvm_send_cmd_pdu(priv,
  1090. REPLY_WOWLAN_TKIP_PARAMS,
  1091. CMD_SYNC, sizeof(tkip_cmd),
  1092. &tkip_cmd);
  1093. if (ret)
  1094. goto out;
  1095. }
  1096. if (priv->have_rekey_data) {
  1097. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1098. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  1099. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1100. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  1101. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1102. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  1103. ret = iwl_dvm_send_cmd_pdu(priv,
  1104. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  1105. CMD_SYNC, sizeof(kek_kck_cmd),
  1106. &kek_kck_cmd);
  1107. if (ret)
  1108. goto out;
  1109. }
  1110. }
  1111. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_D3_CONFIG, CMD_SYNC,
  1112. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  1113. if (ret)
  1114. goto out;
  1115. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_WAKEUP_FILTER,
  1116. CMD_SYNC, sizeof(wakeup_filter_cmd),
  1117. &wakeup_filter_cmd);
  1118. if (ret)
  1119. goto out;
  1120. ret = iwlagn_send_patterns(priv, wowlan);
  1121. out:
  1122. kfree(key_data.rsc_tsc);
  1123. return ret;
  1124. }
  1125. #endif
  1126. int iwl_dvm_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  1127. {
  1128. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  1129. IWL_WARN(priv, "Not sending command - %s KILL\n",
  1130. iwl_is_rfkill(priv) ? "RF" : "CT");
  1131. return -EIO;
  1132. }
  1133. /*
  1134. * Synchronous commands from this op-mode must hold
  1135. * the mutex, this ensures we don't try to send two
  1136. * (or more) synchronous commands at a time.
  1137. */
  1138. if (cmd->flags & CMD_SYNC)
  1139. lockdep_assert_held(&priv->mutex);
  1140. if (priv->ucode_owner == IWL_OWNERSHIP_TM &&
  1141. !(cmd->flags & CMD_ON_DEMAND)) {
  1142. IWL_DEBUG_HC(priv, "tm own the uCode, no regular hcmd send\n");
  1143. return -EIO;
  1144. }
  1145. return iwl_trans_send_cmd(trans(priv), cmd);
  1146. }
  1147. int iwl_dvm_send_cmd_pdu(struct iwl_priv *priv, u8 id,
  1148. u32 flags, u16 len, const void *data)
  1149. {
  1150. struct iwl_host_cmd cmd = {
  1151. .id = id,
  1152. .len = { len, },
  1153. .data = { data, },
  1154. .flags = flags,
  1155. };
  1156. return iwl_dvm_send_cmd(priv, &cmd);
  1157. }