iwl-agn-lib.c 35 KB

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