d3.c 27 KB

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