d3.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 COPYING.
  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 <linux/ip.h>
  65. #include <linux/fs.h>
  66. #include <net/cfg80211.h>
  67. #include <net/ipv6.h>
  68. #include <net/tcp.h>
  69. #include "iwl-modparams.h"
  70. #include "fw-api.h"
  71. #include "mvm.h"
  72. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  73. struct ieee80211_vif *vif,
  74. struct cfg80211_gtk_rekey_data *data)
  75. {
  76. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  77. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  78. if (iwlwifi_mod_params.sw_crypto)
  79. return;
  80. mutex_lock(&mvm->mutex);
  81. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  82. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  83. mvmvif->rekey_data.replay_ctr =
  84. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  85. mvmvif->rekey_data.valid = true;
  86. mutex_unlock(&mvm->mutex);
  87. }
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  90. struct ieee80211_vif *vif,
  91. struct inet6_dev *idev)
  92. {
  93. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  94. struct inet6_ifaddr *ifa;
  95. int idx = 0;
  96. read_lock_bh(&idev->lock);
  97. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  98. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  99. idx++;
  100. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  101. break;
  102. }
  103. read_unlock_bh(&idev->lock);
  104. mvmvif->num_target_ipv6_addrs = idx;
  105. }
  106. #endif
  107. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  108. struct ieee80211_vif *vif, int idx)
  109. {
  110. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  111. mvmvif->tx_key_idx = idx;
  112. }
  113. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  114. {
  115. int i;
  116. for (i = 0; i < IWL_P1K_SIZE; i++)
  117. out[i] = cpu_to_le16(p1k[i]);
  118. }
  119. struct wowlan_key_data {
  120. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  121. struct iwl_wowlan_tkip_params_cmd *tkip;
  122. bool error, use_rsc_tsc, use_tkip;
  123. int wep_key_idx;
  124. };
  125. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  126. struct ieee80211_vif *vif,
  127. struct ieee80211_sta *sta,
  128. struct ieee80211_key_conf *key,
  129. void *_data)
  130. {
  131. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  132. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  133. struct wowlan_key_data *data = _data;
  134. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  135. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  136. struct iwl_p1k_cache *rx_p1ks;
  137. u8 *rx_mic_key;
  138. struct ieee80211_key_seq seq;
  139. u32 cur_rx_iv32 = 0;
  140. u16 p1k[IWL_P1K_SIZE];
  141. int ret, i;
  142. mutex_lock(&mvm->mutex);
  143. switch (key->cipher) {
  144. case WLAN_CIPHER_SUITE_WEP40:
  145. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  146. struct {
  147. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  148. struct iwl_mvm_wep_key wep_key;
  149. } __packed wkc = {
  150. .wep_key_cmd.mac_id_n_color =
  151. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  152. mvmvif->color)),
  153. .wep_key_cmd.num_keys = 1,
  154. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  155. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  156. .wep_key.key_index = key->keyidx,
  157. .wep_key.key_size = key->keylen,
  158. };
  159. /*
  160. * This will fail -- the key functions don't set support
  161. * pairwise WEP keys. However, that's better than silently
  162. * failing WoWLAN. Or maybe not?
  163. */
  164. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  165. break;
  166. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  167. if (key->keyidx == mvmvif->tx_key_idx) {
  168. /* TX key must be at offset 0 */
  169. wkc.wep_key.key_offset = 0;
  170. } else {
  171. /* others start at 1 */
  172. data->wep_key_idx++;
  173. wkc.wep_key.key_offset = data->wep_key_idx;
  174. }
  175. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
  176. sizeof(wkc), &wkc);
  177. data->error = ret != 0;
  178. mvm->ptk_ivlen = key->iv_len;
  179. mvm->ptk_icvlen = key->icv_len;
  180. mvm->gtk_ivlen = key->iv_len;
  181. mvm->gtk_icvlen = key->icv_len;
  182. /* don't upload key again */
  183. goto out_unlock;
  184. }
  185. default:
  186. data->error = true;
  187. goto out_unlock;
  188. case WLAN_CIPHER_SUITE_AES_CMAC:
  189. /*
  190. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  191. * but we also shouldn't abort suspend due to that. It does have
  192. * support for the IGTK key renewal, but doesn't really use the
  193. * IGTK for anything. This means we could spuriously wake up or
  194. * be deauthenticated, but that was considered acceptable.
  195. */
  196. goto out_unlock;
  197. case WLAN_CIPHER_SUITE_TKIP:
  198. if (sta) {
  199. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  200. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  201. rx_p1ks = data->tkip->rx_uni;
  202. ieee80211_get_key_tx_seq(key, &seq);
  203. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  204. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  205. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  206. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  207. memcpy(data->tkip->mic_keys.tx,
  208. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  209. IWL_MIC_KEY_SIZE);
  210. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  211. } else {
  212. tkip_sc =
  213. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  214. rx_p1ks = data->tkip->rx_multi;
  215. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  216. }
  217. /*
  218. * For non-QoS this relies on the fact that both the uCode and
  219. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  220. * for checking the IV in the frames.
  221. */
  222. for (i = 0; i < IWL_NUM_RSC; i++) {
  223. ieee80211_get_key_rx_seq(key, i, &seq);
  224. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  225. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  226. /* wrapping isn't allowed, AP must rekey */
  227. if (seq.tkip.iv32 > cur_rx_iv32)
  228. cur_rx_iv32 = seq.tkip.iv32;
  229. }
  230. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  231. cur_rx_iv32, p1k);
  232. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  233. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  234. cur_rx_iv32 + 1, p1k);
  235. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  236. memcpy(rx_mic_key,
  237. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  238. IWL_MIC_KEY_SIZE);
  239. data->use_tkip = true;
  240. data->use_rsc_tsc = true;
  241. break;
  242. case WLAN_CIPHER_SUITE_CCMP:
  243. if (sta) {
  244. u8 *pn = seq.ccmp.pn;
  245. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  246. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  247. ieee80211_get_key_tx_seq(key, &seq);
  248. aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
  249. ((u64)pn[4] << 8) |
  250. ((u64)pn[3] << 16) |
  251. ((u64)pn[2] << 24) |
  252. ((u64)pn[1] << 32) |
  253. ((u64)pn[0] << 40));
  254. } else {
  255. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  256. }
  257. /*
  258. * For non-QoS this relies on the fact that both the uCode and
  259. * mac80211 use TID 0 for checking the IV in the frames.
  260. */
  261. for (i = 0; i < IWL_NUM_RSC; i++) {
  262. u8 *pn = seq.ccmp.pn;
  263. ieee80211_get_key_rx_seq(key, i, &seq);
  264. aes_sc->pn = cpu_to_le64((u64)pn[5] |
  265. ((u64)pn[4] << 8) |
  266. ((u64)pn[3] << 16) |
  267. ((u64)pn[2] << 24) |
  268. ((u64)pn[1] << 32) |
  269. ((u64)pn[0] << 40));
  270. }
  271. data->use_rsc_tsc = true;
  272. break;
  273. }
  274. /*
  275. * The D3 firmware hardcodes the key offset 0 as the key it uses
  276. * to transmit packets to the AP, i.e. the PTK.
  277. */
  278. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  279. key->hw_key_idx = 0;
  280. mvm->ptk_ivlen = key->iv_len;
  281. mvm->ptk_icvlen = key->icv_len;
  282. } else {
  283. /*
  284. * firmware only supports TSC/RSC for a single key,
  285. * so if there are multiple keep overwriting them
  286. * with new ones -- this relies on mac80211 doing
  287. * list_add_tail().
  288. */
  289. key->hw_key_idx = 1;
  290. mvm->gtk_ivlen = key->iv_len;
  291. mvm->gtk_icvlen = key->icv_len;
  292. }
  293. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  294. data->error = ret != 0;
  295. out_unlock:
  296. mutex_unlock(&mvm->mutex);
  297. }
  298. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  299. struct cfg80211_wowlan *wowlan)
  300. {
  301. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  302. struct iwl_host_cmd cmd = {
  303. .id = WOWLAN_PATTERNS,
  304. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  305. .flags = CMD_SYNC,
  306. };
  307. int i, err;
  308. if (!wowlan->n_patterns)
  309. return 0;
  310. cmd.len[0] = sizeof(*pattern_cmd) +
  311. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  312. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  313. if (!pattern_cmd)
  314. return -ENOMEM;
  315. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  316. for (i = 0; i < wowlan->n_patterns; i++) {
  317. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  318. memcpy(&pattern_cmd->patterns[i].mask,
  319. wowlan->patterns[i].mask, mask_len);
  320. memcpy(&pattern_cmd->patterns[i].pattern,
  321. wowlan->patterns[i].pattern,
  322. wowlan->patterns[i].pattern_len);
  323. pattern_cmd->patterns[i].mask_size = mask_len;
  324. pattern_cmd->patterns[i].pattern_size =
  325. wowlan->patterns[i].pattern_len;
  326. }
  327. cmd.data[0] = pattern_cmd;
  328. err = iwl_mvm_send_cmd(mvm, &cmd);
  329. kfree(pattern_cmd);
  330. return err;
  331. }
  332. static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
  333. struct ieee80211_vif *vif)
  334. {
  335. union {
  336. struct iwl_proto_offload_cmd_v1 v1;
  337. struct iwl_proto_offload_cmd_v2 v2;
  338. } cmd = {};
  339. struct iwl_proto_offload_cmd_common *common;
  340. u32 enabled = 0, size;
  341. #if IS_ENABLED(CONFIG_IPV6)
  342. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  343. int i;
  344. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  345. if (mvmvif->num_target_ipv6_addrs) {
  346. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  347. memcpy(cmd.v2.ndp_mac_addr, vif->addr, ETH_ALEN);
  348. }
  349. BUILD_BUG_ON(sizeof(cmd.v2.target_ipv6_addr[0]) !=
  350. sizeof(mvmvif->target_ipv6_addrs[0]));
  351. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  352. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V2); i++)
  353. memcpy(cmd.v2.target_ipv6_addr[i],
  354. &mvmvif->target_ipv6_addrs[i],
  355. sizeof(cmd.v2.target_ipv6_addr[i]));
  356. } else {
  357. if (mvmvif->num_target_ipv6_addrs) {
  358. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  359. memcpy(cmd.v1.ndp_mac_addr, vif->addr, ETH_ALEN);
  360. }
  361. BUILD_BUG_ON(sizeof(cmd.v1.target_ipv6_addr[0]) !=
  362. sizeof(mvmvif->target_ipv6_addrs[0]));
  363. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  364. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V1); i++)
  365. memcpy(cmd.v1.target_ipv6_addr[i],
  366. &mvmvif->target_ipv6_addrs[i],
  367. sizeof(cmd.v1.target_ipv6_addr[i]));
  368. }
  369. #endif
  370. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  371. common = &cmd.v2.common;
  372. size = sizeof(cmd.v2);
  373. } else {
  374. common = &cmd.v1.common;
  375. size = sizeof(cmd.v1);
  376. }
  377. if (vif->bss_conf.arp_addr_cnt) {
  378. enabled |= IWL_D3_PROTO_OFFLOAD_ARP;
  379. common->host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
  380. memcpy(common->arp_mac_addr, vif->addr, ETH_ALEN);
  381. }
  382. if (!enabled)
  383. return 0;
  384. common->enabled = cpu_to_le32(enabled);
  385. return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
  386. size, &cmd);
  387. }
  388. enum iwl_mvm_tcp_packet_type {
  389. MVM_TCP_TX_SYN,
  390. MVM_TCP_RX_SYNACK,
  391. MVM_TCP_TX_DATA,
  392. MVM_TCP_RX_ACK,
  393. MVM_TCP_RX_WAKE,
  394. MVM_TCP_TX_FIN,
  395. };
  396. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  397. {
  398. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  399. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  400. }
  401. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  402. struct cfg80211_wowlan_tcp *tcp,
  403. void *_pkt, u8 *mask,
  404. __le16 *pseudo_hdr_csum,
  405. enum iwl_mvm_tcp_packet_type ptype)
  406. {
  407. struct {
  408. struct ethhdr eth;
  409. struct iphdr ip;
  410. struct tcphdr tcp;
  411. u8 data[];
  412. } __packed *pkt = _pkt;
  413. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  414. int i;
  415. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  416. pkt->ip.version = 4;
  417. pkt->ip.ihl = 5;
  418. pkt->ip.protocol = IPPROTO_TCP;
  419. switch (ptype) {
  420. case MVM_TCP_TX_SYN:
  421. case MVM_TCP_TX_DATA:
  422. case MVM_TCP_TX_FIN:
  423. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  424. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  425. pkt->ip.ttl = 128;
  426. pkt->ip.saddr = tcp->src;
  427. pkt->ip.daddr = tcp->dst;
  428. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  429. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  430. /* overwritten for TX SYN later */
  431. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  432. pkt->tcp.window = cpu_to_be16(65000);
  433. break;
  434. case MVM_TCP_RX_SYNACK:
  435. case MVM_TCP_RX_ACK:
  436. case MVM_TCP_RX_WAKE:
  437. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  438. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  439. pkt->ip.saddr = tcp->dst;
  440. pkt->ip.daddr = tcp->src;
  441. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  442. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  443. break;
  444. default:
  445. WARN_ON(1);
  446. return;
  447. }
  448. switch (ptype) {
  449. case MVM_TCP_TX_SYN:
  450. /* firmware assumes 8 option bytes - 8 NOPs for now */
  451. memset(pkt->data, 0x01, 8);
  452. ip_tot_len += 8;
  453. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  454. pkt->tcp.syn = 1;
  455. break;
  456. case MVM_TCP_TX_DATA:
  457. ip_tot_len += tcp->payload_len;
  458. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  459. pkt->tcp.psh = 1;
  460. pkt->tcp.ack = 1;
  461. break;
  462. case MVM_TCP_TX_FIN:
  463. pkt->tcp.fin = 1;
  464. pkt->tcp.ack = 1;
  465. break;
  466. case MVM_TCP_RX_SYNACK:
  467. pkt->tcp.syn = 1;
  468. pkt->tcp.ack = 1;
  469. break;
  470. case MVM_TCP_RX_ACK:
  471. pkt->tcp.ack = 1;
  472. break;
  473. case MVM_TCP_RX_WAKE:
  474. ip_tot_len += tcp->wake_len;
  475. pkt->tcp.psh = 1;
  476. pkt->tcp.ack = 1;
  477. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  478. break;
  479. }
  480. switch (ptype) {
  481. case MVM_TCP_TX_SYN:
  482. case MVM_TCP_TX_DATA:
  483. case MVM_TCP_TX_FIN:
  484. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  485. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  486. break;
  487. case MVM_TCP_RX_WAKE:
  488. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  489. u8 tmp = tcp->wake_mask[i];
  490. mask[i + 6] |= tmp << 6;
  491. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  492. mask[i + 7] = tmp >> 2;
  493. }
  494. /* fall through for ethernet/IP/TCP headers mask */
  495. case MVM_TCP_RX_SYNACK:
  496. case MVM_TCP_RX_ACK:
  497. mask[0] = 0xff; /* match ethernet */
  498. /*
  499. * match ethernet, ip.version, ip.ihl
  500. * the ip.ihl half byte is really masked out by firmware
  501. */
  502. mask[1] = 0x7f;
  503. mask[2] = 0x80; /* match ip.protocol */
  504. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  505. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  506. mask[5] = 0x80; /* match tcp flags */
  507. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  508. break;
  509. };
  510. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  511. pkt->ip.saddr, pkt->ip.daddr);
  512. }
  513. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  514. struct ieee80211_vif *vif,
  515. struct cfg80211_wowlan_tcp *tcp)
  516. {
  517. struct iwl_wowlan_remote_wake_config *cfg;
  518. struct iwl_host_cmd cmd = {
  519. .id = REMOTE_WAKE_CONFIG_CMD,
  520. .len = { sizeof(*cfg), },
  521. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  522. .flags = CMD_SYNC,
  523. };
  524. int ret;
  525. if (!tcp)
  526. return 0;
  527. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  528. if (!cfg)
  529. return -ENOMEM;
  530. cmd.data[0] = cfg;
  531. cfg->max_syn_retries = 10;
  532. cfg->max_data_retries = 10;
  533. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  534. cfg->tcp_ack_timeout = 1; /* seconds */
  535. /* SYN (TX) */
  536. iwl_mvm_build_tcp_packet(
  537. vif, tcp, cfg->syn_tx.data, NULL,
  538. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  539. MVM_TCP_TX_SYN);
  540. cfg->syn_tx.info.tcp_payload_length = 0;
  541. /* SYN/ACK (RX) */
  542. iwl_mvm_build_tcp_packet(
  543. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  544. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  545. MVM_TCP_RX_SYNACK);
  546. cfg->synack_rx.info.tcp_payload_length = 0;
  547. /* KEEPALIVE/ACK (TX) */
  548. iwl_mvm_build_tcp_packet(
  549. vif, tcp, cfg->keepalive_tx.data, NULL,
  550. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  551. MVM_TCP_TX_DATA);
  552. cfg->keepalive_tx.info.tcp_payload_length =
  553. cpu_to_le16(tcp->payload_len);
  554. cfg->sequence_number_offset = tcp->payload_seq.offset;
  555. /* length must be 0..4, the field is little endian */
  556. cfg->sequence_number_length = tcp->payload_seq.len;
  557. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  558. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  559. if (tcp->payload_tok.len) {
  560. cfg->token_offset = tcp->payload_tok.offset;
  561. cfg->token_length = tcp->payload_tok.len;
  562. cfg->num_tokens =
  563. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  564. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  565. tcp->tokens_size);
  566. } else {
  567. /* set tokens to max value to almost never run out */
  568. cfg->num_tokens = cpu_to_le16(65535);
  569. }
  570. /* ACK (RX) */
  571. iwl_mvm_build_tcp_packet(
  572. vif, tcp, cfg->keepalive_ack_rx.data,
  573. cfg->keepalive_ack_rx.rx_mask,
  574. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  575. MVM_TCP_RX_ACK);
  576. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  577. /* WAKEUP (RX) */
  578. iwl_mvm_build_tcp_packet(
  579. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  580. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  581. MVM_TCP_RX_WAKE);
  582. cfg->wake_rx.info.tcp_payload_length =
  583. cpu_to_le16(tcp->wake_len);
  584. /* FIN */
  585. iwl_mvm_build_tcp_packet(
  586. vif, tcp, cfg->fin_tx.data, NULL,
  587. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  588. MVM_TCP_TX_FIN);
  589. cfg->fin_tx.info.tcp_payload_length = 0;
  590. ret = iwl_mvm_send_cmd(mvm, &cmd);
  591. kfree(cfg);
  592. return ret;
  593. }
  594. struct iwl_d3_iter_data {
  595. struct iwl_mvm *mvm;
  596. struct ieee80211_vif *vif;
  597. bool error;
  598. };
  599. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  600. struct ieee80211_vif *vif)
  601. {
  602. struct iwl_d3_iter_data *data = _data;
  603. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  604. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  605. return;
  606. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  607. return;
  608. if (data->vif) {
  609. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  610. data->error = true;
  611. return;
  612. }
  613. data->vif = vif;
  614. }
  615. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  616. struct ieee80211_sta *ap_sta)
  617. {
  618. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  619. struct ieee80211_chanctx_conf *ctx;
  620. u8 chains_static, chains_dynamic;
  621. struct cfg80211_chan_def chandef;
  622. int ret, i;
  623. struct iwl_binding_cmd binding_cmd = {};
  624. struct iwl_time_quota_cmd quota_cmd = {};
  625. u32 status;
  626. /* add back the PHY */
  627. if (WARN_ON(!mvmvif->phy_ctxt))
  628. return -EINVAL;
  629. rcu_read_lock();
  630. ctx = rcu_dereference(vif->chanctx_conf);
  631. if (WARN_ON(!ctx)) {
  632. rcu_read_unlock();
  633. return -EINVAL;
  634. }
  635. chandef = ctx->def;
  636. chains_static = ctx->rx_chains_static;
  637. chains_dynamic = ctx->rx_chains_dynamic;
  638. rcu_read_unlock();
  639. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  640. chains_static, chains_dynamic);
  641. if (ret)
  642. return ret;
  643. /* add back the MAC */
  644. mvmvif->uploaded = false;
  645. if (WARN_ON(!vif->bss_conf.assoc))
  646. return -EINVAL;
  647. /* hack */
  648. vif->bss_conf.assoc = false;
  649. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  650. vif->bss_conf.assoc = true;
  651. if (ret)
  652. return ret;
  653. /* add back binding - XXX refactor? */
  654. binding_cmd.id_and_color =
  655. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  656. mvmvif->phy_ctxt->color));
  657. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  658. binding_cmd.phy =
  659. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  660. mvmvif->phy_ctxt->color));
  661. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  662. mvmvif->color));
  663. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  664. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  665. status = 0;
  666. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  667. sizeof(binding_cmd), &binding_cmd,
  668. &status);
  669. if (ret) {
  670. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  671. return ret;
  672. }
  673. if (status) {
  674. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  675. return -EIO;
  676. }
  677. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  678. if (ret)
  679. return ret;
  680. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  681. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  682. if (ret)
  683. return ret;
  684. /* and some quota */
  685. quota_cmd.quotas[0].id_and_color =
  686. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  687. mvmvif->phy_ctxt->color));
  688. quota_cmd.quotas[0].quota = cpu_to_le32(100);
  689. quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
  690. for (i = 1; i < MAX_BINDINGS; i++)
  691. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  692. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  693. sizeof(quota_cmd), &quota_cmd);
  694. if (ret)
  695. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  696. return 0;
  697. }
  698. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  699. struct cfg80211_wowlan *wowlan,
  700. bool test)
  701. {
  702. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  703. struct iwl_d3_iter_data suspend_iter_data = {
  704. .mvm = mvm,
  705. };
  706. struct ieee80211_vif *vif;
  707. struct iwl_mvm_vif *mvmvif;
  708. struct ieee80211_sta *ap_sta;
  709. struct iwl_mvm_sta *mvm_ap_sta;
  710. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  711. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  712. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  713. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  714. /*
  715. * Program the minimum sleep time to 10 seconds, as many
  716. * platforms have issues processing a wakeup signal while
  717. * still being in the process of suspending.
  718. */
  719. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  720. };
  721. struct iwl_host_cmd d3_cfg_cmd = {
  722. .id = D3_CONFIG_CMD,
  723. .flags = CMD_SYNC | CMD_WANT_SKB,
  724. .data[0] = &d3_cfg_cmd_data,
  725. .len[0] = sizeof(d3_cfg_cmd_data),
  726. };
  727. struct wowlan_key_data key_data = {
  728. .use_rsc_tsc = false,
  729. .tkip = &tkip_cmd,
  730. .use_tkip = false,
  731. };
  732. int ret, i;
  733. int len __maybe_unused;
  734. u16 seq;
  735. u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
  736. if (!wowlan) {
  737. /*
  738. * mac80211 shouldn't get here, but for D3 test
  739. * it doesn't warrant a warning
  740. */
  741. WARN_ON(!test);
  742. return -EINVAL;
  743. }
  744. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  745. if (!key_data.rsc_tsc)
  746. return -ENOMEM;
  747. mutex_lock(&mvm->mutex);
  748. old_aux_sta_id = mvm->aux_sta.sta_id;
  749. /* see if there's only a single BSS vif and it's associated */
  750. ieee80211_iterate_active_interfaces_atomic(
  751. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  752. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  753. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  754. ret = 1;
  755. goto out_noreset;
  756. }
  757. vif = suspend_iter_data.vif;
  758. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  759. ap_sta = rcu_dereference_protected(
  760. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  761. lockdep_is_held(&mvm->mutex));
  762. if (IS_ERR_OR_NULL(ap_sta)) {
  763. ret = -EINVAL;
  764. goto out_noreset;
  765. }
  766. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  767. /*
  768. * The D3 firmware still hardcodes the AP station ID for the
  769. * BSS we're associated with as 0. Store the real STA ID here
  770. * and assign 0. When we leave this function, we'll restore
  771. * the original value for the resume code.
  772. */
  773. old_ap_sta_id = mvm_ap_sta->sta_id;
  774. mvm_ap_sta->sta_id = 0;
  775. mvmvif->ap_sta_id = 0;
  776. /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
  777. wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
  778. /*
  779. * We know the last used seqno, and the uCode expects to know that
  780. * one, it will increment before TX.
  781. */
  782. seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
  783. wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
  784. /*
  785. * For QoS counters, we store the one to use next, so subtract 0x10
  786. * since the uCode will add 0x10 *before* using the value while we
  787. * increment after using the value (i.e. store the next value to use).
  788. */
  789. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  790. seq = mvm_ap_sta->tid_data[i].seq_number;
  791. seq -= 0x10;
  792. wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
  793. }
  794. if (wowlan->disconnect)
  795. wowlan_config_cmd.wakeup_filter |=
  796. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  797. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  798. if (wowlan->magic_pkt)
  799. wowlan_config_cmd.wakeup_filter |=
  800. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  801. if (wowlan->gtk_rekey_failure)
  802. wowlan_config_cmd.wakeup_filter |=
  803. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  804. if (wowlan->eap_identity_req)
  805. wowlan_config_cmd.wakeup_filter |=
  806. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  807. if (wowlan->four_way_handshake)
  808. wowlan_config_cmd.wakeup_filter |=
  809. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  810. if (wowlan->n_patterns)
  811. wowlan_config_cmd.wakeup_filter |=
  812. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  813. if (wowlan->rfkill_release)
  814. wowlan_config_cmd.wakeup_filter |=
  815. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  816. if (wowlan->tcp) {
  817. /*
  818. * Set the "link change" (really "link lost") flag as well
  819. * since that implies losing the TCP connection.
  820. */
  821. wowlan_config_cmd.wakeup_filter |=
  822. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  823. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  824. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  825. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  826. }
  827. iwl_mvm_cancel_scan(mvm);
  828. iwl_trans_stop_device(mvm->trans);
  829. /*
  830. * Set the HW restart bit -- this is mostly true as we're
  831. * going to load new firmware and reprogram that, though
  832. * the reprogramming is going to be manual to avoid adding
  833. * all the MACs that aren't support.
  834. * We don't have to clear up everything though because the
  835. * reprogramming is manual. When we resume, we'll actually
  836. * go through a proper restart sequence again to switch
  837. * back to the runtime firmware image.
  838. */
  839. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  840. /* We reprogram keys and shouldn't allocate new key indices */
  841. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  842. mvm->ptk_ivlen = 0;
  843. mvm->ptk_icvlen = 0;
  844. mvm->ptk_ivlen = 0;
  845. mvm->ptk_icvlen = 0;
  846. /*
  847. * The D3 firmware still hardcodes the AP station ID for the
  848. * BSS we're associated with as 0. As a result, we have to move
  849. * the auxiliary station to ID 1 so the ID 0 remains free for
  850. * the AP station for later.
  851. * We set the sta_id to 1 here, and reset it to its previous
  852. * value (that we stored above) later.
  853. */
  854. mvm->aux_sta.sta_id = 1;
  855. ret = iwl_mvm_load_d3_fw(mvm);
  856. if (ret)
  857. goto out;
  858. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  859. if (ret)
  860. goto out;
  861. if (!iwlwifi_mod_params.sw_crypto) {
  862. /*
  863. * This needs to be unlocked due to lock ordering
  864. * constraints. Since we're in the suspend path
  865. * that isn't really a problem though.
  866. */
  867. mutex_unlock(&mvm->mutex);
  868. ieee80211_iter_keys(mvm->hw, vif,
  869. iwl_mvm_wowlan_program_keys,
  870. &key_data);
  871. mutex_lock(&mvm->mutex);
  872. if (key_data.error) {
  873. ret = -EIO;
  874. goto out;
  875. }
  876. if (key_data.use_rsc_tsc) {
  877. struct iwl_host_cmd rsc_tsc_cmd = {
  878. .id = WOWLAN_TSC_RSC_PARAM,
  879. .flags = CMD_SYNC,
  880. .data[0] = key_data.rsc_tsc,
  881. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  882. .len[0] = sizeof(*key_data.rsc_tsc),
  883. };
  884. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  885. if (ret)
  886. goto out;
  887. }
  888. if (key_data.use_tkip) {
  889. ret = iwl_mvm_send_cmd_pdu(mvm,
  890. WOWLAN_TKIP_PARAM,
  891. CMD_SYNC, sizeof(tkip_cmd),
  892. &tkip_cmd);
  893. if (ret)
  894. goto out;
  895. }
  896. if (mvmvif->rekey_data.valid) {
  897. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  898. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  899. NL80211_KCK_LEN);
  900. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  901. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  902. NL80211_KEK_LEN);
  903. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  904. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  905. ret = iwl_mvm_send_cmd_pdu(mvm,
  906. WOWLAN_KEK_KCK_MATERIAL,
  907. CMD_SYNC,
  908. sizeof(kek_kck_cmd),
  909. &kek_kck_cmd);
  910. if (ret)
  911. goto out;
  912. }
  913. }
  914. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
  915. CMD_SYNC, sizeof(wowlan_config_cmd),
  916. &wowlan_config_cmd);
  917. if (ret)
  918. goto out;
  919. ret = iwl_mvm_send_patterns(mvm, wowlan);
  920. if (ret)
  921. goto out;
  922. ret = iwl_mvm_send_proto_offload(mvm, vif);
  923. if (ret)
  924. goto out;
  925. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  926. if (ret)
  927. goto out;
  928. ret = iwl_mvm_power_update_mode(mvm, vif);
  929. if (ret)
  930. goto out;
  931. #ifdef CONFIG_IWLWIFI_DEBUGFS
  932. if (mvm->d3_wake_sysassert)
  933. d3_cfg_cmd_data.wakeup_flags |=
  934. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  935. #endif
  936. /* must be last -- this switches firmware state */
  937. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  938. if (ret)
  939. goto out;
  940. #ifdef CONFIG_IWLWIFI_DEBUGFS
  941. len = le32_to_cpu(d3_cfg_cmd.resp_pkt->len_n_flags) &
  942. FH_RSCSR_FRAME_SIZE_MSK;
  943. if (len >= sizeof(u32) * 2) {
  944. mvm->d3_test_pme_ptr =
  945. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  946. } else if (test) {
  947. /* in test mode we require the pointer */
  948. ret = -EIO;
  949. goto out;
  950. }
  951. #endif
  952. iwl_free_resp(&d3_cfg_cmd);
  953. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  954. iwl_trans_d3_suspend(mvm->trans, test);
  955. out:
  956. mvm->aux_sta.sta_id = old_aux_sta_id;
  957. mvm_ap_sta->sta_id = old_ap_sta_id;
  958. mvmvif->ap_sta_id = old_ap_sta_id;
  959. out_noreset:
  960. kfree(key_data.rsc_tsc);
  961. if (ret < 0)
  962. ieee80211_restart_hw(mvm->hw);
  963. mutex_unlock(&mvm->mutex);
  964. return ret;
  965. }
  966. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  967. {
  968. return __iwl_mvm_suspend(hw, wowlan, false);
  969. }
  970. static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  971. struct ieee80211_vif *vif)
  972. {
  973. u32 base = mvm->error_event_table;
  974. struct error_table_start {
  975. /* cf. struct iwl_error_event_table */
  976. u32 valid;
  977. u32 error_id;
  978. } err_info;
  979. struct cfg80211_wowlan_wakeup wakeup = {
  980. .pattern_idx = -1,
  981. };
  982. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  983. struct iwl_host_cmd cmd = {
  984. .id = WOWLAN_GET_STATUSES,
  985. .flags = CMD_SYNC | CMD_WANT_SKB,
  986. };
  987. struct iwl_wowlan_status *status;
  988. u32 reasons;
  989. int ret, len;
  990. struct sk_buff *pkt = NULL;
  991. iwl_trans_read_mem_bytes(mvm->trans, base,
  992. &err_info, sizeof(err_info));
  993. if (err_info.valid) {
  994. IWL_INFO(mvm, "error table is valid (%d)\n",
  995. err_info.valid);
  996. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  997. wakeup.rfkill_release = true;
  998. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  999. GFP_KERNEL);
  1000. }
  1001. return;
  1002. }
  1003. /* only for tracing for now */
  1004. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
  1005. if (ret)
  1006. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1007. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1008. if (ret) {
  1009. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1010. return;
  1011. }
  1012. /* RF-kill already asserted again... */
  1013. if (!cmd.resp_pkt)
  1014. return;
  1015. len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  1016. if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
  1017. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1018. goto out;
  1019. }
  1020. status = (void *)cmd.resp_pkt->data;
  1021. if (len - sizeof(struct iwl_cmd_header) !=
  1022. sizeof(*status) +
  1023. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4)) {
  1024. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1025. goto out;
  1026. }
  1027. reasons = le32_to_cpu(status->wakeup_reasons);
  1028. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1029. wakeup_report = NULL;
  1030. goto report;
  1031. }
  1032. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1033. wakeup.magic_pkt = true;
  1034. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1035. wakeup.pattern_idx =
  1036. le16_to_cpu(status->pattern_number);
  1037. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1038. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1039. wakeup.disconnect = true;
  1040. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1041. wakeup.gtk_rekey_failure = true;
  1042. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1043. wakeup.rfkill_release = true;
  1044. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1045. wakeup.eap_identity_req = true;
  1046. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1047. wakeup.four_way_handshake = true;
  1048. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1049. wakeup.tcp_connlost = true;
  1050. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1051. wakeup.tcp_nomoretokens = true;
  1052. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1053. wakeup.tcp_match = true;
  1054. if (status->wake_packet_bufsize) {
  1055. int pktsize = le32_to_cpu(status->wake_packet_bufsize);
  1056. int pktlen = le32_to_cpu(status->wake_packet_length);
  1057. const u8 *pktdata = status->wake_packet;
  1058. struct ieee80211_hdr *hdr = (void *)pktdata;
  1059. int truncated = pktlen - pktsize;
  1060. /* this would be a firmware bug */
  1061. if (WARN_ON_ONCE(truncated < 0))
  1062. truncated = 0;
  1063. if (ieee80211_is_data(hdr->frame_control)) {
  1064. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1065. int ivlen = 0, icvlen = 4; /* also FCS */
  1066. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1067. if (!pkt)
  1068. goto report;
  1069. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1070. pktdata += hdrlen;
  1071. pktsize -= hdrlen;
  1072. if (ieee80211_has_protected(hdr->frame_control)) {
  1073. if (is_multicast_ether_addr(hdr->addr1)) {
  1074. ivlen = mvm->gtk_ivlen;
  1075. icvlen += mvm->gtk_icvlen;
  1076. } else {
  1077. ivlen = mvm->ptk_ivlen;
  1078. icvlen += mvm->ptk_icvlen;
  1079. }
  1080. }
  1081. /* if truncated, FCS/ICV is (partially) gone */
  1082. if (truncated >= icvlen) {
  1083. icvlen = 0;
  1084. truncated -= icvlen;
  1085. } else {
  1086. icvlen -= truncated;
  1087. truncated = 0;
  1088. }
  1089. pktsize -= ivlen + icvlen;
  1090. pktdata += ivlen;
  1091. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1092. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1093. goto report;
  1094. wakeup.packet = pkt->data;
  1095. wakeup.packet_present_len = pkt->len;
  1096. wakeup.packet_len = pkt->len - truncated;
  1097. wakeup.packet_80211 = false;
  1098. } else {
  1099. int fcslen = 4;
  1100. if (truncated >= 4) {
  1101. truncated -= 4;
  1102. fcslen = 0;
  1103. } else {
  1104. fcslen -= truncated;
  1105. truncated = 0;
  1106. }
  1107. pktsize -= fcslen;
  1108. wakeup.packet = status->wake_packet;
  1109. wakeup.packet_present_len = pktsize;
  1110. wakeup.packet_len = pktlen - truncated;
  1111. wakeup.packet_80211 = true;
  1112. }
  1113. }
  1114. report:
  1115. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1116. kfree_skb(pkt);
  1117. out:
  1118. iwl_free_resp(&cmd);
  1119. }
  1120. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1121. {
  1122. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1123. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1124. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1125. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1126. if (!mvm->store_d3_resume_sram)
  1127. return;
  1128. if (!mvm->d3_resume_sram) {
  1129. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1130. if (!mvm->d3_resume_sram)
  1131. return;
  1132. }
  1133. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1134. #endif
  1135. }
  1136. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1137. {
  1138. struct iwl_d3_iter_data resume_iter_data = {
  1139. .mvm = mvm,
  1140. };
  1141. struct ieee80211_vif *vif = NULL;
  1142. int ret;
  1143. enum iwl_d3_status d3_status;
  1144. mutex_lock(&mvm->mutex);
  1145. /* get the BSS vif pointer again */
  1146. ieee80211_iterate_active_interfaces_atomic(
  1147. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1148. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  1149. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  1150. goto out_unlock;
  1151. vif = resume_iter_data.vif;
  1152. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
  1153. if (ret)
  1154. goto out_unlock;
  1155. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1156. IWL_INFO(mvm, "Device was reset during suspend\n");
  1157. goto out_unlock;
  1158. }
  1159. /* query SRAM first in case we want event logging */
  1160. iwl_mvm_read_d3_sram(mvm);
  1161. iwl_mvm_query_wakeup_reasons(mvm, vif);
  1162. out_unlock:
  1163. mutex_unlock(&mvm->mutex);
  1164. if (!test && vif)
  1165. ieee80211_resume_disconnect(vif);
  1166. /* return 1 to reconfigure the device */
  1167. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1168. return 1;
  1169. }
  1170. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1171. {
  1172. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1173. return __iwl_mvm_resume(mvm, false);
  1174. }
  1175. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1176. {
  1177. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1178. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1179. }
  1180. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1181. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1182. {
  1183. struct iwl_mvm *mvm = inode->i_private;
  1184. int err;
  1185. if (mvm->d3_test_active)
  1186. return -EBUSY;
  1187. file->private_data = inode->i_private;
  1188. ieee80211_stop_queues(mvm->hw);
  1189. synchronize_net();
  1190. /* start pseudo D3 */
  1191. rtnl_lock();
  1192. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1193. rtnl_unlock();
  1194. if (err > 0)
  1195. err = -EINVAL;
  1196. if (err) {
  1197. ieee80211_wake_queues(mvm->hw);
  1198. return err;
  1199. }
  1200. mvm->d3_test_active = true;
  1201. return 0;
  1202. }
  1203. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1204. size_t count, loff_t *ppos)
  1205. {
  1206. struct iwl_mvm *mvm = file->private_data;
  1207. u32 pme_asserted;
  1208. while (true) {
  1209. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1210. mvm->d3_test_pme_ptr);
  1211. if (pme_asserted)
  1212. break;
  1213. if (msleep_interruptible(100))
  1214. break;
  1215. }
  1216. return 0;
  1217. }
  1218. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1219. struct ieee80211_vif *vif)
  1220. {
  1221. if (vif->type == NL80211_IFTYPE_STATION)
  1222. ieee80211_connection_loss(vif);
  1223. }
  1224. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1225. {
  1226. struct iwl_mvm *mvm = inode->i_private;
  1227. int remaining_time = 10;
  1228. mvm->d3_test_active = false;
  1229. __iwl_mvm_resume(mvm, true);
  1230. iwl_abort_notification_waits(&mvm->notif_wait);
  1231. ieee80211_restart_hw(mvm->hw);
  1232. /* wait for restart and disconnect all interfaces */
  1233. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1234. remaining_time > 0) {
  1235. remaining_time--;
  1236. msleep(1000);
  1237. }
  1238. if (remaining_time == 0)
  1239. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1240. ieee80211_iterate_active_interfaces_atomic(
  1241. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1242. iwl_mvm_d3_test_disconn_work_iter, NULL);
  1243. ieee80211_wake_queues(mvm->hw);
  1244. return 0;
  1245. }
  1246. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1247. .llseek = no_llseek,
  1248. .open = iwl_mvm_d3_test_open,
  1249. .read = iwl_mvm_d3_test_read,
  1250. .release = iwl_mvm_d3_test_release,
  1251. };
  1252. #endif