d3.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <net/cfg80211.h>
  64. #include <net/ipv6.h>
  65. #include "iwl-modparams.h"
  66. #include "fw-api.h"
  67. #include "mvm.h"
  68. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  69. struct ieee80211_vif *vif,
  70. struct cfg80211_gtk_rekey_data *data)
  71. {
  72. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  73. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  74. if (iwlwifi_mod_params.sw_crypto)
  75. return;
  76. mutex_lock(&mvm->mutex);
  77. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  78. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  79. mvmvif->rekey_data.replay_ctr =
  80. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  81. mvmvif->rekey_data.valid = true;
  82. mutex_unlock(&mvm->mutex);
  83. }
  84. #if IS_ENABLED(CONFIG_IPV6)
  85. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  86. struct ieee80211_vif *vif,
  87. struct inet6_dev *idev)
  88. {
  89. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  90. struct inet6_ifaddr *ifa;
  91. int idx = 0;
  92. read_lock_bh(&idev->lock);
  93. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  94. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  95. idx++;
  96. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS)
  97. break;
  98. }
  99. read_unlock_bh(&idev->lock);
  100. mvmvif->num_target_ipv6_addrs = idx;
  101. }
  102. #endif
  103. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  104. struct ieee80211_vif *vif, int idx)
  105. {
  106. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  107. mvmvif->tx_key_idx = idx;
  108. }
  109. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  110. {
  111. int i;
  112. for (i = 0; i < IWL_P1K_SIZE; i++)
  113. out[i] = cpu_to_le16(p1k[i]);
  114. }
  115. struct wowlan_key_data {
  116. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  117. struct iwl_wowlan_tkip_params_cmd *tkip;
  118. bool error, use_rsc_tsc, use_tkip;
  119. int gtk_key_idx;
  120. };
  121. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  122. struct ieee80211_vif *vif,
  123. struct ieee80211_sta *sta,
  124. struct ieee80211_key_conf *key,
  125. void *_data)
  126. {
  127. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  128. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  129. struct wowlan_key_data *data = _data;
  130. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  131. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  132. struct iwl_p1k_cache *rx_p1ks;
  133. u8 *rx_mic_key;
  134. struct ieee80211_key_seq seq;
  135. u32 cur_rx_iv32 = 0;
  136. u16 p1k[IWL_P1K_SIZE];
  137. int ret, i;
  138. mutex_lock(&mvm->mutex);
  139. switch (key->cipher) {
  140. case WLAN_CIPHER_SUITE_WEP40:
  141. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  142. struct {
  143. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  144. struct iwl_mvm_wep_key wep_key;
  145. } __packed wkc = {
  146. .wep_key_cmd.mac_id_n_color =
  147. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  148. mvmvif->color)),
  149. .wep_key_cmd.num_keys = 1,
  150. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  151. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  152. .wep_key.key_index = key->keyidx,
  153. .wep_key.key_size = key->keylen,
  154. };
  155. /*
  156. * This will fail -- the key functions don't set support
  157. * pairwise WEP keys. However, that's better than silently
  158. * failing WoWLAN. Or maybe not?
  159. */
  160. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  161. break;
  162. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  163. if (key->keyidx == mvmvif->tx_key_idx) {
  164. /* TX key must be at offset 0 */
  165. wkc.wep_key.key_offset = 0;
  166. } else {
  167. /* others start at 1 */
  168. data->gtk_key_idx++;
  169. wkc.wep_key.key_offset = data->gtk_key_idx;
  170. }
  171. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
  172. sizeof(wkc), &wkc);
  173. data->error = ret != 0;
  174. /* don't upload key again */
  175. goto out_unlock;
  176. }
  177. default:
  178. data->error = true;
  179. goto out_unlock;
  180. case WLAN_CIPHER_SUITE_AES_CMAC:
  181. /*
  182. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  183. * but we also shouldn't abort suspend due to that. It does have
  184. * support for the IGTK key renewal, but doesn't really use the
  185. * IGTK for anything. This means we could spuriously wake up or
  186. * be deauthenticated, but that was considered acceptable.
  187. */
  188. goto out_unlock;
  189. case WLAN_CIPHER_SUITE_TKIP:
  190. if (sta) {
  191. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  192. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  193. rx_p1ks = data->tkip->rx_uni;
  194. ieee80211_get_key_tx_seq(key, &seq);
  195. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  196. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  197. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  198. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  199. memcpy(data->tkip->mic_keys.tx,
  200. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  201. IWL_MIC_KEY_SIZE);
  202. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  203. } else {
  204. tkip_sc =
  205. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  206. rx_p1ks = data->tkip->rx_multi;
  207. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  208. }
  209. /*
  210. * For non-QoS this relies on the fact that both the uCode and
  211. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  212. * for checking the IV in the frames.
  213. */
  214. for (i = 0; i < IWL_NUM_RSC; i++) {
  215. ieee80211_get_key_rx_seq(key, i, &seq);
  216. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  217. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  218. /* wrapping isn't allowed, AP must rekey */
  219. if (seq.tkip.iv32 > cur_rx_iv32)
  220. cur_rx_iv32 = seq.tkip.iv32;
  221. }
  222. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  223. cur_rx_iv32, p1k);
  224. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  225. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  226. cur_rx_iv32 + 1, p1k);
  227. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  228. memcpy(rx_mic_key,
  229. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  230. IWL_MIC_KEY_SIZE);
  231. data->use_tkip = true;
  232. data->use_rsc_tsc = true;
  233. break;
  234. case WLAN_CIPHER_SUITE_CCMP:
  235. if (sta) {
  236. u8 *pn = seq.ccmp.pn;
  237. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  238. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  239. ieee80211_get_key_tx_seq(key, &seq);
  240. aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
  241. ((u64)pn[4] << 8) |
  242. ((u64)pn[3] << 16) |
  243. ((u64)pn[2] << 24) |
  244. ((u64)pn[1] << 32) |
  245. ((u64)pn[0] << 40));
  246. } else {
  247. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  248. }
  249. /*
  250. * For non-QoS this relies on the fact that both the uCode and
  251. * mac80211 use TID 0 for checking the IV in the frames.
  252. */
  253. for (i = 0; i < IWL_NUM_RSC; i++) {
  254. u8 *pn = seq.ccmp.pn;
  255. ieee80211_get_key_rx_seq(key, i, &seq);
  256. aes_sc->pn = cpu_to_le64((u64)pn[5] |
  257. ((u64)pn[4] << 8) |
  258. ((u64)pn[3] << 16) |
  259. ((u64)pn[2] << 24) |
  260. ((u64)pn[1] << 32) |
  261. ((u64)pn[0] << 40));
  262. }
  263. data->use_rsc_tsc = true;
  264. break;
  265. }
  266. /*
  267. * The D3 firmware hardcodes the key offset 0 as the key it uses
  268. * to transmit packets to the AP, i.e. the PTK.
  269. */
  270. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  271. key->hw_key_idx = 0;
  272. } else {
  273. data->gtk_key_idx++;
  274. key->hw_key_idx = data->gtk_key_idx;
  275. }
  276. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  277. data->error = ret != 0;
  278. out_unlock:
  279. mutex_unlock(&mvm->mutex);
  280. }
  281. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  282. struct cfg80211_wowlan *wowlan)
  283. {
  284. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  285. struct iwl_host_cmd cmd = {
  286. .id = WOWLAN_PATTERNS,
  287. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  288. .flags = CMD_SYNC,
  289. };
  290. int i, err;
  291. if (!wowlan->n_patterns)
  292. return 0;
  293. cmd.len[0] = sizeof(*pattern_cmd) +
  294. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  295. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  296. if (!pattern_cmd)
  297. return -ENOMEM;
  298. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  299. for (i = 0; i < wowlan->n_patterns; i++) {
  300. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  301. memcpy(&pattern_cmd->patterns[i].mask,
  302. wowlan->patterns[i].mask, mask_len);
  303. memcpy(&pattern_cmd->patterns[i].pattern,
  304. wowlan->patterns[i].pattern,
  305. wowlan->patterns[i].pattern_len);
  306. pattern_cmd->patterns[i].mask_size = mask_len;
  307. pattern_cmd->patterns[i].pattern_size =
  308. wowlan->patterns[i].pattern_len;
  309. }
  310. cmd.data[0] = pattern_cmd;
  311. err = iwl_mvm_send_cmd(mvm, &cmd);
  312. kfree(pattern_cmd);
  313. return err;
  314. }
  315. static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
  316. struct ieee80211_vif *vif)
  317. {
  318. struct iwl_proto_offload_cmd cmd = {};
  319. #if IS_ENABLED(CONFIG_IPV6)
  320. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  321. int i;
  322. if (mvmvif->num_target_ipv6_addrs) {
  323. cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_NS);
  324. memcpy(cmd.ndp_mac_addr, vif->addr, ETH_ALEN);
  325. }
  326. BUILD_BUG_ON(sizeof(cmd.target_ipv6_addr[i]) !=
  327. sizeof(mvmvif->target_ipv6_addrs[i]));
  328. for (i = 0; i < mvmvif->num_target_ipv6_addrs; i++)
  329. memcpy(cmd.target_ipv6_addr[i],
  330. &mvmvif->target_ipv6_addrs[i],
  331. sizeof(cmd.target_ipv6_addr[i]));
  332. #endif
  333. if (vif->bss_conf.arp_addr_cnt) {
  334. cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_ARP);
  335. cmd.host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
  336. memcpy(cmd.arp_mac_addr, vif->addr, ETH_ALEN);
  337. }
  338. if (!cmd.enabled)
  339. return 0;
  340. return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
  341. sizeof(cmd), &cmd);
  342. }
  343. struct iwl_d3_iter_data {
  344. struct iwl_mvm *mvm;
  345. struct ieee80211_vif *vif;
  346. bool error;
  347. };
  348. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  349. struct ieee80211_vif *vif)
  350. {
  351. struct iwl_d3_iter_data *data = _data;
  352. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  353. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  354. return;
  355. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  356. return;
  357. if (data->vif) {
  358. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  359. data->error = true;
  360. return;
  361. }
  362. data->vif = vif;
  363. }
  364. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  365. struct ieee80211_sta *ap_sta)
  366. {
  367. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  368. struct ieee80211_chanctx_conf *ctx;
  369. u8 chains_static, chains_dynamic;
  370. struct cfg80211_chan_def chandef;
  371. int ret, i;
  372. struct iwl_binding_cmd binding_cmd = {};
  373. struct iwl_time_quota_cmd quota_cmd = {};
  374. u32 status;
  375. /* add back the PHY */
  376. if (WARN_ON(!mvmvif->phy_ctxt))
  377. return -EINVAL;
  378. rcu_read_lock();
  379. ctx = rcu_dereference(vif->chanctx_conf);
  380. if (WARN_ON(!ctx)) {
  381. rcu_read_unlock();
  382. return -EINVAL;
  383. }
  384. chandef = ctx->def;
  385. chains_static = ctx->rx_chains_static;
  386. chains_dynamic = ctx->rx_chains_dynamic;
  387. rcu_read_unlock();
  388. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  389. chains_static, chains_dynamic);
  390. if (ret)
  391. return ret;
  392. /* add back the MAC */
  393. mvmvif->uploaded = false;
  394. if (WARN_ON(!vif->bss_conf.assoc))
  395. return -EINVAL;
  396. /* hack */
  397. vif->bss_conf.assoc = false;
  398. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  399. vif->bss_conf.assoc = true;
  400. if (ret)
  401. return ret;
  402. /* add back binding - XXX refactor? */
  403. binding_cmd.id_and_color =
  404. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  405. mvmvif->phy_ctxt->color));
  406. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  407. binding_cmd.phy =
  408. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  409. mvmvif->phy_ctxt->color));
  410. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  411. mvmvif->color));
  412. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  413. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  414. status = 0;
  415. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  416. sizeof(binding_cmd), &binding_cmd,
  417. &status);
  418. if (ret) {
  419. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  420. return ret;
  421. }
  422. if (status) {
  423. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  424. return -EIO;
  425. }
  426. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  427. if (ret)
  428. return ret;
  429. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  430. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  431. if (ret)
  432. return ret;
  433. /* and some quota */
  434. quota_cmd.quotas[0].id_and_color =
  435. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  436. mvmvif->phy_ctxt->color));
  437. quota_cmd.quotas[0].quota = cpu_to_le32(100);
  438. quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
  439. for (i = 1; i < MAX_BINDINGS; i++)
  440. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  441. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  442. sizeof(quota_cmd), &quota_cmd);
  443. if (ret)
  444. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  445. return 0;
  446. }
  447. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  448. {
  449. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  450. struct iwl_d3_iter_data suspend_iter_data = {
  451. .mvm = mvm,
  452. };
  453. struct ieee80211_vif *vif;
  454. struct iwl_mvm_vif *mvmvif;
  455. struct ieee80211_sta *ap_sta;
  456. struct iwl_mvm_sta *mvm_ap_sta;
  457. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  458. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  459. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  460. struct iwl_d3_manager_config d3_cfg_cmd = {};
  461. struct wowlan_key_data key_data = {
  462. .use_rsc_tsc = false,
  463. .tkip = &tkip_cmd,
  464. .use_tkip = false,
  465. };
  466. int ret, i;
  467. u16 seq;
  468. u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
  469. if (WARN_ON(!wowlan))
  470. return -EINVAL;
  471. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  472. if (!key_data.rsc_tsc)
  473. return -ENOMEM;
  474. mutex_lock(&mvm->mutex);
  475. old_aux_sta_id = mvm->aux_sta.sta_id;
  476. /* see if there's only a single BSS vif and it's associated */
  477. ieee80211_iterate_active_interfaces_atomic(
  478. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  479. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  480. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  481. ret = 1;
  482. goto out_noreset;
  483. }
  484. vif = suspend_iter_data.vif;
  485. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  486. ap_sta = rcu_dereference_protected(
  487. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  488. lockdep_is_held(&mvm->mutex));
  489. if (IS_ERR_OR_NULL(ap_sta)) {
  490. ret = -EINVAL;
  491. goto out_noreset;
  492. }
  493. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  494. /*
  495. * The D3 firmware still hardcodes the AP station ID for the
  496. * BSS we're associated with as 0. Store the real STA ID here
  497. * and assign 0. When we leave this function, we'll restore
  498. * the original value for the resume code.
  499. */
  500. old_ap_sta_id = mvm_ap_sta->sta_id;
  501. mvm_ap_sta->sta_id = 0;
  502. mvmvif->ap_sta_id = 0;
  503. /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
  504. wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
  505. /*
  506. * We know the last used seqno, and the uCode expects to know that
  507. * one, it will increment before TX.
  508. */
  509. seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
  510. wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
  511. /*
  512. * For QoS counters, we store the one to use next, so subtract 0x10
  513. * since the uCode will add 0x10 *before* using the value while we
  514. * increment after using the value (i.e. store the next value to use).
  515. */
  516. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  517. seq = mvm_ap_sta->tid_data[i].seq_number;
  518. seq -= 0x10;
  519. wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
  520. }
  521. if (wowlan->disconnect)
  522. wowlan_config_cmd.wakeup_filter |=
  523. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  524. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  525. if (wowlan->magic_pkt)
  526. wowlan_config_cmd.wakeup_filter |=
  527. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  528. if (wowlan->gtk_rekey_failure)
  529. wowlan_config_cmd.wakeup_filter |=
  530. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  531. if (wowlan->eap_identity_req)
  532. wowlan_config_cmd.wakeup_filter |=
  533. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  534. if (wowlan->four_way_handshake)
  535. wowlan_config_cmd.wakeup_filter |=
  536. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  537. if (wowlan->n_patterns)
  538. wowlan_config_cmd.wakeup_filter |=
  539. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  540. if (wowlan->rfkill_release)
  541. d3_cfg_cmd.wakeup_flags |=
  542. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  543. iwl_mvm_cancel_scan(mvm);
  544. iwl_trans_stop_device(mvm->trans);
  545. /*
  546. * Set the HW restart bit -- this is mostly true as we're
  547. * going to load new firmware and reprogram that, though
  548. * the reprogramming is going to be manual to avoid adding
  549. * all the MACs that aren't support.
  550. * We don't have to clear up everything though because the
  551. * reprogramming is manual. When we resume, we'll actually
  552. * go through a proper restart sequence again to switch
  553. * back to the runtime firmware image.
  554. */
  555. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  556. /* We reprogram keys and shouldn't allocate new key indices */
  557. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  558. /*
  559. * The D3 firmware still hardcodes the AP station ID for the
  560. * BSS we're associated with as 0. As a result, we have to move
  561. * the auxiliary station to ID 1 so the ID 0 remains free for
  562. * the AP station for later.
  563. * We set the sta_id to 1 here, and reset it to its previous
  564. * value (that we stored above) later.
  565. */
  566. mvm->aux_sta.sta_id = 1;
  567. ret = iwl_mvm_load_d3_fw(mvm);
  568. if (ret)
  569. goto out;
  570. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  571. if (ret)
  572. goto out;
  573. if (!iwlwifi_mod_params.sw_crypto) {
  574. /*
  575. * This needs to be unlocked due to lock ordering
  576. * constraints. Since we're in the suspend path
  577. * that isn't really a problem though.
  578. */
  579. mutex_unlock(&mvm->mutex);
  580. ieee80211_iter_keys(mvm->hw, vif,
  581. iwl_mvm_wowlan_program_keys,
  582. &key_data);
  583. mutex_lock(&mvm->mutex);
  584. if (key_data.error) {
  585. ret = -EIO;
  586. goto out;
  587. }
  588. if (key_data.use_rsc_tsc) {
  589. struct iwl_host_cmd rsc_tsc_cmd = {
  590. .id = WOWLAN_TSC_RSC_PARAM,
  591. .flags = CMD_SYNC,
  592. .data[0] = key_data.rsc_tsc,
  593. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  594. .len[0] = sizeof(*key_data.rsc_tsc),
  595. };
  596. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  597. if (ret)
  598. goto out;
  599. }
  600. if (key_data.use_tkip) {
  601. ret = iwl_mvm_send_cmd_pdu(mvm,
  602. WOWLAN_TKIP_PARAM,
  603. CMD_SYNC, sizeof(tkip_cmd),
  604. &tkip_cmd);
  605. if (ret)
  606. goto out;
  607. }
  608. if (mvmvif->rekey_data.valid) {
  609. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  610. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  611. NL80211_KCK_LEN);
  612. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  613. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  614. NL80211_KEK_LEN);
  615. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  616. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  617. ret = iwl_mvm_send_cmd_pdu(mvm,
  618. WOWLAN_KEK_KCK_MATERIAL,
  619. CMD_SYNC,
  620. sizeof(kek_kck_cmd),
  621. &kek_kck_cmd);
  622. if (ret)
  623. goto out;
  624. }
  625. }
  626. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
  627. CMD_SYNC, sizeof(wowlan_config_cmd),
  628. &wowlan_config_cmd);
  629. if (ret)
  630. goto out;
  631. ret = iwl_mvm_send_patterns(mvm, wowlan);
  632. if (ret)
  633. goto out;
  634. ret = iwl_mvm_send_proto_offload(mvm, vif);
  635. if (ret)
  636. goto out;
  637. /* must be last -- this switches firmware state */
  638. ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, CMD_SYNC,
  639. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  640. if (ret)
  641. goto out;
  642. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  643. iwl_trans_d3_suspend(mvm->trans);
  644. out:
  645. mvm->aux_sta.sta_id = old_aux_sta_id;
  646. mvm_ap_sta->sta_id = old_ap_sta_id;
  647. mvmvif->ap_sta_id = old_ap_sta_id;
  648. out_noreset:
  649. kfree(key_data.rsc_tsc);
  650. if (ret < 0)
  651. ieee80211_restart_hw(mvm->hw);
  652. mutex_unlock(&mvm->mutex);
  653. return ret;
  654. }
  655. static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  656. struct ieee80211_vif *vif)
  657. {
  658. u32 base = mvm->error_event_table;
  659. struct error_table_start {
  660. /* cf. struct iwl_error_event_table */
  661. u32 valid;
  662. u32 error_id;
  663. } err_info;
  664. struct cfg80211_wowlan_wakeup wakeup = {
  665. .pattern_idx = -1,
  666. };
  667. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  668. struct iwl_host_cmd cmd = {
  669. .id = WOWLAN_GET_STATUSES,
  670. .flags = CMD_SYNC | CMD_WANT_SKB,
  671. };
  672. struct iwl_wowlan_status *status;
  673. u32 reasons;
  674. int ret, len;
  675. bool pkt8023 = false;
  676. struct sk_buff *pkt = NULL;
  677. iwl_trans_read_mem_bytes(mvm->trans, base,
  678. &err_info, sizeof(err_info));
  679. if (err_info.valid) {
  680. IWL_INFO(mvm, "error table is valid (%d)\n",
  681. err_info.valid);
  682. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  683. wakeup.rfkill_release = true;
  684. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  685. GFP_KERNEL);
  686. }
  687. return;
  688. }
  689. /* only for tracing for now */
  690. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
  691. if (ret)
  692. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  693. ret = iwl_mvm_send_cmd(mvm, &cmd);
  694. if (ret) {
  695. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  696. return;
  697. }
  698. /* RF-kill already asserted again... */
  699. if (!cmd.resp_pkt)
  700. return;
  701. len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  702. if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
  703. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  704. goto out;
  705. }
  706. status = (void *)cmd.resp_pkt->data;
  707. if (len - sizeof(struct iwl_cmd_header) !=
  708. sizeof(*status) + le32_to_cpu(status->wake_packet_bufsize)) {
  709. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  710. goto out;
  711. }
  712. reasons = le32_to_cpu(status->wakeup_reasons);
  713. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  714. wakeup_report = NULL;
  715. goto report;
  716. }
  717. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET) {
  718. wakeup.magic_pkt = true;
  719. pkt8023 = true;
  720. }
  721. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN) {
  722. wakeup.pattern_idx =
  723. le16_to_cpu(status->pattern_number);
  724. pkt8023 = true;
  725. }
  726. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  727. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  728. wakeup.disconnect = true;
  729. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE) {
  730. wakeup.gtk_rekey_failure = true;
  731. pkt8023 = true;
  732. }
  733. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) {
  734. wakeup.rfkill_release = true;
  735. pkt8023 = true;
  736. }
  737. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST) {
  738. wakeup.eap_identity_req = true;
  739. pkt8023 = true;
  740. }
  741. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE) {
  742. wakeup.four_way_handshake = true;
  743. pkt8023 = true;
  744. }
  745. if (status->wake_packet_bufsize) {
  746. u32 pktsize = le32_to_cpu(status->wake_packet_bufsize);
  747. u32 pktlen = le32_to_cpu(status->wake_packet_length);
  748. if (pkt8023) {
  749. pkt = alloc_skb(pktsize, GFP_KERNEL);
  750. if (!pkt)
  751. goto report;
  752. memcpy(skb_put(pkt, pktsize), status->wake_packet,
  753. pktsize);
  754. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  755. goto report;
  756. wakeup.packet = pkt->data;
  757. wakeup.packet_present_len = pkt->len;
  758. wakeup.packet_len = pkt->len - (pktlen - pktsize);
  759. wakeup.packet_80211 = false;
  760. } else {
  761. wakeup.packet = status->wake_packet;
  762. wakeup.packet_present_len = pktsize;
  763. wakeup.packet_len = pktlen;
  764. wakeup.packet_80211 = true;
  765. }
  766. }
  767. report:
  768. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  769. kfree_skb(pkt);
  770. out:
  771. iwl_free_resp(&cmd);
  772. }
  773. int iwl_mvm_resume(struct ieee80211_hw *hw)
  774. {
  775. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  776. struct iwl_d3_iter_data resume_iter_data = {
  777. .mvm = mvm,
  778. };
  779. struct ieee80211_vif *vif = NULL;
  780. int ret;
  781. enum iwl_d3_status d3_status;
  782. mutex_lock(&mvm->mutex);
  783. /* get the BSS vif pointer again */
  784. ieee80211_iterate_active_interfaces_atomic(
  785. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  786. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  787. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  788. goto out_unlock;
  789. vif = resume_iter_data.vif;
  790. ret = iwl_trans_d3_resume(mvm->trans, &d3_status);
  791. if (ret)
  792. goto out_unlock;
  793. if (d3_status != IWL_D3_STATUS_ALIVE) {
  794. IWL_INFO(mvm, "Device was reset during suspend\n");
  795. goto out_unlock;
  796. }
  797. iwl_mvm_query_wakeup_reasons(mvm, vif);
  798. out_unlock:
  799. mutex_unlock(&mvm->mutex);
  800. if (vif)
  801. ieee80211_resume_disconnect(vif);
  802. /* return 1 to reconfigure the device */
  803. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  804. return 1;
  805. }
  806. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  807. {
  808. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  809. device_set_wakeup_enable(mvm->trans->dev, enabled);
  810. }