lib.c 37 KB

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