lib.c 36 KB

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