d3.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/etherdevice.h>
  66. #include <linux/ip.h>
  67. #include <linux/fs.h>
  68. #include <net/cfg80211.h>
  69. #include <net/ipv6.h>
  70. #include <net/tcp.h>
  71. #include <net/addrconf.h>
  72. #include "iwl-modparams.h"
  73. #include "fw-api.h"
  74. #include "mvm.h"
  75. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  76. struct ieee80211_vif *vif,
  77. struct cfg80211_gtk_rekey_data *data)
  78. {
  79. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  80. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  81. if (iwlwifi_mod_params.sw_crypto)
  82. return;
  83. mutex_lock(&mvm->mutex);
  84. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  85. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  86. mvmvif->rekey_data.replay_ctr =
  87. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  88. mvmvif->rekey_data.valid = true;
  89. mutex_unlock(&mvm->mutex);
  90. }
  91. #if IS_ENABLED(CONFIG_IPV6)
  92. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  93. struct ieee80211_vif *vif,
  94. struct inet6_dev *idev)
  95. {
  96. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  97. struct inet6_ifaddr *ifa;
  98. int idx = 0;
  99. read_lock_bh(&idev->lock);
  100. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  101. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  102. idx++;
  103. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  104. break;
  105. }
  106. read_unlock_bh(&idev->lock);
  107. mvmvif->num_target_ipv6_addrs = idx;
  108. }
  109. #endif
  110. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  111. struct ieee80211_vif *vif, int idx)
  112. {
  113. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  114. mvmvif->tx_key_idx = idx;
  115. }
  116. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  117. {
  118. int i;
  119. for (i = 0; i < IWL_P1K_SIZE; i++)
  120. out[i] = cpu_to_le16(p1k[i]);
  121. }
  122. struct wowlan_key_data {
  123. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  124. struct iwl_wowlan_tkip_params_cmd *tkip;
  125. bool error, use_rsc_tsc, use_tkip;
  126. int wep_key_idx;
  127. };
  128. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  129. struct ieee80211_vif *vif,
  130. struct ieee80211_sta *sta,
  131. struct ieee80211_key_conf *key,
  132. void *_data)
  133. {
  134. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  135. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  136. struct wowlan_key_data *data = _data;
  137. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  138. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  139. struct iwl_p1k_cache *rx_p1ks;
  140. u8 *rx_mic_key;
  141. struct ieee80211_key_seq seq;
  142. u32 cur_rx_iv32 = 0;
  143. u16 p1k[IWL_P1K_SIZE];
  144. int ret, i;
  145. mutex_lock(&mvm->mutex);
  146. switch (key->cipher) {
  147. case WLAN_CIPHER_SUITE_WEP40:
  148. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  149. struct {
  150. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  151. struct iwl_mvm_wep_key wep_key;
  152. } __packed wkc = {
  153. .wep_key_cmd.mac_id_n_color =
  154. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  155. mvmvif->color)),
  156. .wep_key_cmd.num_keys = 1,
  157. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  158. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  159. .wep_key.key_index = key->keyidx,
  160. .wep_key.key_size = key->keylen,
  161. };
  162. /*
  163. * This will fail -- the key functions don't set support
  164. * pairwise WEP keys. However, that's better than silently
  165. * failing WoWLAN. Or maybe not?
  166. */
  167. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  168. break;
  169. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  170. if (key->keyidx == mvmvif->tx_key_idx) {
  171. /* TX key must be at offset 0 */
  172. wkc.wep_key.key_offset = 0;
  173. } else {
  174. /* others start at 1 */
  175. data->wep_key_idx++;
  176. wkc.wep_key.key_offset = data->wep_key_idx;
  177. }
  178. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
  179. data->error = ret != 0;
  180. mvm->ptk_ivlen = key->iv_len;
  181. mvm->ptk_icvlen = key->icv_len;
  182. mvm->gtk_ivlen = key->iv_len;
  183. mvm->gtk_icvlen = key->icv_len;
  184. /* don't upload key again */
  185. goto out_unlock;
  186. }
  187. default:
  188. data->error = true;
  189. goto out_unlock;
  190. case WLAN_CIPHER_SUITE_AES_CMAC:
  191. /*
  192. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  193. * but we also shouldn't abort suspend due to that. It does have
  194. * support for the IGTK key renewal, but doesn't really use the
  195. * IGTK for anything. This means we could spuriously wake up or
  196. * be deauthenticated, but that was considered acceptable.
  197. */
  198. goto out_unlock;
  199. case WLAN_CIPHER_SUITE_TKIP:
  200. if (sta) {
  201. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  202. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  203. rx_p1ks = data->tkip->rx_uni;
  204. ieee80211_get_key_tx_seq(key, &seq);
  205. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  206. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  207. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  208. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  209. memcpy(data->tkip->mic_keys.tx,
  210. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  211. IWL_MIC_KEY_SIZE);
  212. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  213. } else {
  214. tkip_sc =
  215. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  216. rx_p1ks = data->tkip->rx_multi;
  217. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  218. }
  219. /*
  220. * For non-QoS this relies on the fact that both the uCode and
  221. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  222. * for checking the IV in the frames.
  223. */
  224. for (i = 0; i < IWL_NUM_RSC; i++) {
  225. ieee80211_get_key_rx_seq(key, i, &seq);
  226. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  227. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  228. /* wrapping isn't allowed, AP must rekey */
  229. if (seq.tkip.iv32 > cur_rx_iv32)
  230. cur_rx_iv32 = seq.tkip.iv32;
  231. }
  232. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  233. cur_rx_iv32, p1k);
  234. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  235. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  236. cur_rx_iv32 + 1, p1k);
  237. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  238. memcpy(rx_mic_key,
  239. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  240. IWL_MIC_KEY_SIZE);
  241. data->use_tkip = true;
  242. data->use_rsc_tsc = true;
  243. break;
  244. case WLAN_CIPHER_SUITE_CCMP:
  245. if (sta) {
  246. u8 *pn = seq.ccmp.pn;
  247. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  248. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  249. ieee80211_get_key_tx_seq(key, &seq);
  250. aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
  251. ((u64)pn[4] << 8) |
  252. ((u64)pn[3] << 16) |
  253. ((u64)pn[2] << 24) |
  254. ((u64)pn[1] << 32) |
  255. ((u64)pn[0] << 40));
  256. } else {
  257. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  258. }
  259. /*
  260. * For non-QoS this relies on the fact that both the uCode and
  261. * mac80211 use TID 0 for checking the IV in the frames.
  262. */
  263. for (i = 0; i < IWL_NUM_RSC; i++) {
  264. u8 *pn = seq.ccmp.pn;
  265. ieee80211_get_key_rx_seq(key, i, &seq);
  266. aes_sc->pn = cpu_to_le64((u64)pn[5] |
  267. ((u64)pn[4] << 8) |
  268. ((u64)pn[3] << 16) |
  269. ((u64)pn[2] << 24) |
  270. ((u64)pn[1] << 32) |
  271. ((u64)pn[0] << 40));
  272. }
  273. data->use_rsc_tsc = true;
  274. break;
  275. }
  276. /*
  277. * The D3 firmware hardcodes the key offset 0 as the key it uses
  278. * to transmit packets to the AP, i.e. the PTK.
  279. */
  280. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  281. key->hw_key_idx = 0;
  282. mvm->ptk_ivlen = key->iv_len;
  283. mvm->ptk_icvlen = key->icv_len;
  284. } else {
  285. /*
  286. * firmware only supports TSC/RSC for a single key,
  287. * so if there are multiple keep overwriting them
  288. * with new ones -- this relies on mac80211 doing
  289. * list_add_tail().
  290. */
  291. key->hw_key_idx = 1;
  292. mvm->gtk_ivlen = key->iv_len;
  293. mvm->gtk_icvlen = key->icv_len;
  294. }
  295. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  296. data->error = ret != 0;
  297. out_unlock:
  298. mutex_unlock(&mvm->mutex);
  299. }
  300. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  301. struct cfg80211_wowlan *wowlan)
  302. {
  303. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  304. struct iwl_host_cmd cmd = {
  305. .id = WOWLAN_PATTERNS,
  306. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  307. };
  308. int i, err;
  309. if (!wowlan->n_patterns)
  310. return 0;
  311. cmd.len[0] = sizeof(*pattern_cmd) +
  312. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  313. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  314. if (!pattern_cmd)
  315. return -ENOMEM;
  316. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  317. for (i = 0; i < wowlan->n_patterns; i++) {
  318. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  319. memcpy(&pattern_cmd->patterns[i].mask,
  320. wowlan->patterns[i].mask, mask_len);
  321. memcpy(&pattern_cmd->patterns[i].pattern,
  322. wowlan->patterns[i].pattern,
  323. wowlan->patterns[i].pattern_len);
  324. pattern_cmd->patterns[i].mask_size = mask_len;
  325. pattern_cmd->patterns[i].pattern_size =
  326. wowlan->patterns[i].pattern_len;
  327. }
  328. cmd.data[0] = pattern_cmd;
  329. err = iwl_mvm_send_cmd(mvm, &cmd);
  330. kfree(pattern_cmd);
  331. return err;
  332. }
  333. enum iwl_mvm_tcp_packet_type {
  334. MVM_TCP_TX_SYN,
  335. MVM_TCP_RX_SYNACK,
  336. MVM_TCP_TX_DATA,
  337. MVM_TCP_RX_ACK,
  338. MVM_TCP_RX_WAKE,
  339. MVM_TCP_TX_FIN,
  340. };
  341. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  342. {
  343. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  344. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  345. }
  346. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  347. struct cfg80211_wowlan_tcp *tcp,
  348. void *_pkt, u8 *mask,
  349. __le16 *pseudo_hdr_csum,
  350. enum iwl_mvm_tcp_packet_type ptype)
  351. {
  352. struct {
  353. struct ethhdr eth;
  354. struct iphdr ip;
  355. struct tcphdr tcp;
  356. u8 data[];
  357. } __packed *pkt = _pkt;
  358. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  359. int i;
  360. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  361. pkt->ip.version = 4;
  362. pkt->ip.ihl = 5;
  363. pkt->ip.protocol = IPPROTO_TCP;
  364. switch (ptype) {
  365. case MVM_TCP_TX_SYN:
  366. case MVM_TCP_TX_DATA:
  367. case MVM_TCP_TX_FIN:
  368. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  369. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  370. pkt->ip.ttl = 128;
  371. pkt->ip.saddr = tcp->src;
  372. pkt->ip.daddr = tcp->dst;
  373. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  374. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  375. /* overwritten for TX SYN later */
  376. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  377. pkt->tcp.window = cpu_to_be16(65000);
  378. break;
  379. case MVM_TCP_RX_SYNACK:
  380. case MVM_TCP_RX_ACK:
  381. case MVM_TCP_RX_WAKE:
  382. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  383. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  384. pkt->ip.saddr = tcp->dst;
  385. pkt->ip.daddr = tcp->src;
  386. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  387. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  388. break;
  389. default:
  390. WARN_ON(1);
  391. return;
  392. }
  393. switch (ptype) {
  394. case MVM_TCP_TX_SYN:
  395. /* firmware assumes 8 option bytes - 8 NOPs for now */
  396. memset(pkt->data, 0x01, 8);
  397. ip_tot_len += 8;
  398. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  399. pkt->tcp.syn = 1;
  400. break;
  401. case MVM_TCP_TX_DATA:
  402. ip_tot_len += tcp->payload_len;
  403. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  404. pkt->tcp.psh = 1;
  405. pkt->tcp.ack = 1;
  406. break;
  407. case MVM_TCP_TX_FIN:
  408. pkt->tcp.fin = 1;
  409. pkt->tcp.ack = 1;
  410. break;
  411. case MVM_TCP_RX_SYNACK:
  412. pkt->tcp.syn = 1;
  413. pkt->tcp.ack = 1;
  414. break;
  415. case MVM_TCP_RX_ACK:
  416. pkt->tcp.ack = 1;
  417. break;
  418. case MVM_TCP_RX_WAKE:
  419. ip_tot_len += tcp->wake_len;
  420. pkt->tcp.psh = 1;
  421. pkt->tcp.ack = 1;
  422. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  423. break;
  424. }
  425. switch (ptype) {
  426. case MVM_TCP_TX_SYN:
  427. case MVM_TCP_TX_DATA:
  428. case MVM_TCP_TX_FIN:
  429. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  430. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  431. break;
  432. case MVM_TCP_RX_WAKE:
  433. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  434. u8 tmp = tcp->wake_mask[i];
  435. mask[i + 6] |= tmp << 6;
  436. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  437. mask[i + 7] = tmp >> 2;
  438. }
  439. /* fall through for ethernet/IP/TCP headers mask */
  440. case MVM_TCP_RX_SYNACK:
  441. case MVM_TCP_RX_ACK:
  442. mask[0] = 0xff; /* match ethernet */
  443. /*
  444. * match ethernet, ip.version, ip.ihl
  445. * the ip.ihl half byte is really masked out by firmware
  446. */
  447. mask[1] = 0x7f;
  448. mask[2] = 0x80; /* match ip.protocol */
  449. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  450. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  451. mask[5] = 0x80; /* match tcp flags */
  452. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  453. break;
  454. };
  455. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  456. pkt->ip.saddr, pkt->ip.daddr);
  457. }
  458. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  459. struct ieee80211_vif *vif,
  460. struct cfg80211_wowlan_tcp *tcp)
  461. {
  462. struct iwl_wowlan_remote_wake_config *cfg;
  463. struct iwl_host_cmd cmd = {
  464. .id = REMOTE_WAKE_CONFIG_CMD,
  465. .len = { sizeof(*cfg), },
  466. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  467. };
  468. int ret;
  469. if (!tcp)
  470. return 0;
  471. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  472. if (!cfg)
  473. return -ENOMEM;
  474. cmd.data[0] = cfg;
  475. cfg->max_syn_retries = 10;
  476. cfg->max_data_retries = 10;
  477. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  478. cfg->tcp_ack_timeout = 1; /* seconds */
  479. /* SYN (TX) */
  480. iwl_mvm_build_tcp_packet(
  481. vif, tcp, cfg->syn_tx.data, NULL,
  482. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  483. MVM_TCP_TX_SYN);
  484. cfg->syn_tx.info.tcp_payload_length = 0;
  485. /* SYN/ACK (RX) */
  486. iwl_mvm_build_tcp_packet(
  487. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  488. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  489. MVM_TCP_RX_SYNACK);
  490. cfg->synack_rx.info.tcp_payload_length = 0;
  491. /* KEEPALIVE/ACK (TX) */
  492. iwl_mvm_build_tcp_packet(
  493. vif, tcp, cfg->keepalive_tx.data, NULL,
  494. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  495. MVM_TCP_TX_DATA);
  496. cfg->keepalive_tx.info.tcp_payload_length =
  497. cpu_to_le16(tcp->payload_len);
  498. cfg->sequence_number_offset = tcp->payload_seq.offset;
  499. /* length must be 0..4, the field is little endian */
  500. cfg->sequence_number_length = tcp->payload_seq.len;
  501. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  502. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  503. if (tcp->payload_tok.len) {
  504. cfg->token_offset = tcp->payload_tok.offset;
  505. cfg->token_length = tcp->payload_tok.len;
  506. cfg->num_tokens =
  507. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  508. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  509. tcp->tokens_size);
  510. } else {
  511. /* set tokens to max value to almost never run out */
  512. cfg->num_tokens = cpu_to_le16(65535);
  513. }
  514. /* ACK (RX) */
  515. iwl_mvm_build_tcp_packet(
  516. vif, tcp, cfg->keepalive_ack_rx.data,
  517. cfg->keepalive_ack_rx.rx_mask,
  518. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  519. MVM_TCP_RX_ACK);
  520. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  521. /* WAKEUP (RX) */
  522. iwl_mvm_build_tcp_packet(
  523. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  524. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  525. MVM_TCP_RX_WAKE);
  526. cfg->wake_rx.info.tcp_payload_length =
  527. cpu_to_le16(tcp->wake_len);
  528. /* FIN */
  529. iwl_mvm_build_tcp_packet(
  530. vif, tcp, cfg->fin_tx.data, NULL,
  531. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  532. MVM_TCP_TX_FIN);
  533. cfg->fin_tx.info.tcp_payload_length = 0;
  534. ret = iwl_mvm_send_cmd(mvm, &cmd);
  535. kfree(cfg);
  536. return ret;
  537. }
  538. struct iwl_d3_iter_data {
  539. struct iwl_mvm *mvm;
  540. struct ieee80211_vif *vif;
  541. bool error;
  542. };
  543. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  544. struct ieee80211_vif *vif)
  545. {
  546. struct iwl_d3_iter_data *data = _data;
  547. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  548. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  549. return;
  550. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  551. return;
  552. if (data->vif) {
  553. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  554. data->error = true;
  555. return;
  556. }
  557. data->vif = vif;
  558. }
  559. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  560. struct ieee80211_sta *ap_sta)
  561. {
  562. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  563. struct ieee80211_chanctx_conf *ctx;
  564. u8 chains_static, chains_dynamic;
  565. struct cfg80211_chan_def chandef;
  566. int ret, i;
  567. struct iwl_binding_cmd binding_cmd = {};
  568. struct iwl_time_quota_cmd quota_cmd = {};
  569. u32 status;
  570. /* add back the PHY */
  571. if (WARN_ON(!mvmvif->phy_ctxt))
  572. return -EINVAL;
  573. rcu_read_lock();
  574. ctx = rcu_dereference(vif->chanctx_conf);
  575. if (WARN_ON(!ctx)) {
  576. rcu_read_unlock();
  577. return -EINVAL;
  578. }
  579. chandef = ctx->def;
  580. chains_static = ctx->rx_chains_static;
  581. chains_dynamic = ctx->rx_chains_dynamic;
  582. rcu_read_unlock();
  583. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  584. chains_static, chains_dynamic);
  585. if (ret)
  586. return ret;
  587. /* add back the MAC */
  588. mvmvif->uploaded = false;
  589. if (WARN_ON(!vif->bss_conf.assoc))
  590. return -EINVAL;
  591. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  592. if (ret)
  593. return ret;
  594. /* add back binding - XXX refactor? */
  595. binding_cmd.id_and_color =
  596. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  597. mvmvif->phy_ctxt->color));
  598. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  599. binding_cmd.phy =
  600. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  601. mvmvif->phy_ctxt->color));
  602. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  603. mvmvif->color));
  604. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  605. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  606. status = 0;
  607. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  608. sizeof(binding_cmd), &binding_cmd,
  609. &status);
  610. if (ret) {
  611. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  612. return ret;
  613. }
  614. if (status) {
  615. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  616. return -EIO;
  617. }
  618. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  619. if (ret)
  620. return ret;
  621. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  622. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  623. if (ret)
  624. return ret;
  625. /* and some quota */
  626. quota_cmd.quotas[0].id_and_color =
  627. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  628. mvmvif->phy_ctxt->color));
  629. quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  630. quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  631. for (i = 1; i < MAX_BINDINGS; i++)
  632. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  633. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  634. sizeof(quota_cmd), &quota_cmd);
  635. if (ret)
  636. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  637. return 0;
  638. }
  639. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  640. struct ieee80211_vif *vif)
  641. {
  642. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  643. struct iwl_nonqos_seq_query_cmd query_cmd = {
  644. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  645. .mac_id_n_color =
  646. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  647. mvmvif->color)),
  648. };
  649. struct iwl_host_cmd cmd = {
  650. .id = NON_QOS_TX_COUNTER_CMD,
  651. .flags = CMD_WANT_SKB,
  652. };
  653. int err;
  654. u32 size;
  655. cmd.data[0] = &query_cmd;
  656. cmd.len[0] = sizeof(query_cmd);
  657. err = iwl_mvm_send_cmd(mvm, &cmd);
  658. if (err)
  659. return err;
  660. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  661. if (size < sizeof(__le16)) {
  662. err = -EINVAL;
  663. } else {
  664. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  665. /* firmware returns next, not last-used seqno */
  666. err = (u16) (err - 0x10);
  667. }
  668. iwl_free_resp(&cmd);
  669. return err;
  670. }
  671. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  672. {
  673. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  674. struct iwl_nonqos_seq_query_cmd query_cmd = {
  675. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  676. .mac_id_n_color =
  677. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  678. mvmvif->color)),
  679. .value = cpu_to_le16(mvmvif->seqno),
  680. };
  681. /* return if called during restart, not resume from D3 */
  682. if (!mvmvif->seqno_valid)
  683. return;
  684. mvmvif->seqno_valid = false;
  685. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
  686. sizeof(query_cmd), &query_cmd))
  687. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  688. }
  689. static int
  690. iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
  691. const struct iwl_wowlan_config_cmd_v3 *cmd)
  692. {
  693. /* start only with the v2 part of the command */
  694. u16 cmd_len = sizeof(cmd->common);
  695. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
  696. cmd_len = sizeof(*cmd);
  697. return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  698. cmd_len, cmd);
  699. }
  700. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  701. struct cfg80211_wowlan *wowlan,
  702. bool test)
  703. {
  704. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  705. struct iwl_d3_iter_data suspend_iter_data = {
  706. .mvm = mvm,
  707. };
  708. struct ieee80211_vif *vif;
  709. struct iwl_mvm_vif *mvmvif;
  710. struct ieee80211_sta *ap_sta;
  711. struct iwl_mvm_sta *mvm_ap_sta;
  712. struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
  713. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  714. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  715. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  716. /*
  717. * Program the minimum sleep time to 10 seconds, as many
  718. * platforms have issues processing a wakeup signal while
  719. * still being in the process of suspending.
  720. */
  721. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  722. };
  723. struct iwl_host_cmd d3_cfg_cmd = {
  724. .id = D3_CONFIG_CMD,
  725. .flags = CMD_WANT_SKB,
  726. .data[0] = &d3_cfg_cmd_data,
  727. .len[0] = sizeof(d3_cfg_cmd_data),
  728. };
  729. struct wowlan_key_data key_data = {
  730. .use_rsc_tsc = false,
  731. .tkip = &tkip_cmd,
  732. .use_tkip = false,
  733. };
  734. int ret;
  735. int len __maybe_unused;
  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. /* see if there's only a single BSS vif and it's associated */
  749. ieee80211_iterate_active_interfaces_atomic(
  750. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  751. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  752. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  753. ret = 1;
  754. goto out_noreset;
  755. }
  756. vif = suspend_iter_data.vif;
  757. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  758. ap_sta = rcu_dereference_protected(
  759. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  760. lockdep_is_held(&mvm->mutex));
  761. if (IS_ERR_OR_NULL(ap_sta)) {
  762. ret = -EINVAL;
  763. goto out_noreset;
  764. }
  765. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  766. /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
  767. wowlan_config_cmd.common.is_11n_connection =
  768. ap_sta->ht_cap.ht_supported;
  769. /* Query the last used seqno and set it */
  770. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  771. if (ret < 0)
  772. goto out_noreset;
  773. wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
  774. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
  775. if (wowlan->disconnect)
  776. wowlan_config_cmd.common.wakeup_filter |=
  777. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  778. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  779. if (wowlan->magic_pkt)
  780. wowlan_config_cmd.common.wakeup_filter |=
  781. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  782. if (wowlan->gtk_rekey_failure)
  783. wowlan_config_cmd.common.wakeup_filter |=
  784. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  785. if (wowlan->eap_identity_req)
  786. wowlan_config_cmd.common.wakeup_filter |=
  787. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  788. if (wowlan->four_way_handshake)
  789. wowlan_config_cmd.common.wakeup_filter |=
  790. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  791. if (wowlan->n_patterns)
  792. wowlan_config_cmd.common.wakeup_filter |=
  793. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  794. if (wowlan->rfkill_release)
  795. wowlan_config_cmd.common.wakeup_filter |=
  796. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  797. if (wowlan->tcp) {
  798. /*
  799. * Set the "link change" (really "link lost") flag as well
  800. * since that implies losing the TCP connection.
  801. */
  802. wowlan_config_cmd.common.wakeup_filter |=
  803. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  804. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  805. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  806. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  807. }
  808. iwl_mvm_cancel_scan(mvm);
  809. iwl_trans_stop_device(mvm->trans);
  810. /*
  811. * Set the HW restart bit -- this is mostly true as we're
  812. * going to load new firmware and reprogram that, though
  813. * the reprogramming is going to be manual to avoid adding
  814. * all the MACs that aren't support.
  815. * We don't have to clear up everything though because the
  816. * reprogramming is manual. When we resume, we'll actually
  817. * go through a proper restart sequence again to switch
  818. * back to the runtime firmware image.
  819. */
  820. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  821. /* We reprogram keys and shouldn't allocate new key indices */
  822. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  823. mvm->ptk_ivlen = 0;
  824. mvm->ptk_icvlen = 0;
  825. mvm->ptk_ivlen = 0;
  826. mvm->ptk_icvlen = 0;
  827. ret = iwl_mvm_load_d3_fw(mvm);
  828. if (ret)
  829. goto out;
  830. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  831. if (ret)
  832. goto out;
  833. if (!iwlwifi_mod_params.sw_crypto) {
  834. /*
  835. * This needs to be unlocked due to lock ordering
  836. * constraints. Since we're in the suspend path
  837. * that isn't really a problem though.
  838. */
  839. mutex_unlock(&mvm->mutex);
  840. ieee80211_iter_keys(mvm->hw, vif,
  841. iwl_mvm_wowlan_program_keys,
  842. &key_data);
  843. mutex_lock(&mvm->mutex);
  844. if (key_data.error) {
  845. ret = -EIO;
  846. goto out;
  847. }
  848. if (key_data.use_rsc_tsc) {
  849. struct iwl_host_cmd rsc_tsc_cmd = {
  850. .id = WOWLAN_TSC_RSC_PARAM,
  851. .data[0] = key_data.rsc_tsc,
  852. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  853. .len[0] = sizeof(*key_data.rsc_tsc),
  854. };
  855. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  856. if (ret)
  857. goto out;
  858. }
  859. if (key_data.use_tkip) {
  860. ret = iwl_mvm_send_cmd_pdu(mvm,
  861. WOWLAN_TKIP_PARAM,
  862. 0, sizeof(tkip_cmd),
  863. &tkip_cmd);
  864. if (ret)
  865. goto out;
  866. }
  867. if (mvmvif->rekey_data.valid) {
  868. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  869. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  870. NL80211_KCK_LEN);
  871. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  872. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  873. NL80211_KEK_LEN);
  874. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  875. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  876. ret = iwl_mvm_send_cmd_pdu(mvm,
  877. WOWLAN_KEK_KCK_MATERIAL, 0,
  878. sizeof(kek_kck_cmd),
  879. &kek_kck_cmd);
  880. if (ret)
  881. goto out;
  882. }
  883. }
  884. ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
  885. if (ret)
  886. goto out;
  887. ret = iwl_mvm_send_patterns(mvm, wowlan);
  888. if (ret)
  889. goto out;
  890. ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
  891. if (ret)
  892. goto out;
  893. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  894. if (ret)
  895. goto out;
  896. ret = iwl_mvm_power_update_device(mvm);
  897. if (ret)
  898. goto out;
  899. ret = iwl_mvm_power_update_mac(mvm);
  900. if (ret)
  901. goto out;
  902. #ifdef CONFIG_IWLWIFI_DEBUGFS
  903. if (mvm->d3_wake_sysassert)
  904. d3_cfg_cmd_data.wakeup_flags |=
  905. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  906. #endif
  907. /* must be last -- this switches firmware state */
  908. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  909. if (ret)
  910. goto out;
  911. #ifdef CONFIG_IWLWIFI_DEBUGFS
  912. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  913. if (len >= sizeof(u32)) {
  914. mvm->d3_test_pme_ptr =
  915. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  916. }
  917. #endif
  918. iwl_free_resp(&d3_cfg_cmd);
  919. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  920. iwl_trans_d3_suspend(mvm->trans, test);
  921. out:
  922. if (ret < 0)
  923. ieee80211_restart_hw(mvm->hw);
  924. out_noreset:
  925. kfree(key_data.rsc_tsc);
  926. mutex_unlock(&mvm->mutex);
  927. return ret;
  928. }
  929. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  930. {
  931. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  932. if (iwl_mvm_is_d0i3_supported(mvm)) {
  933. mutex_lock(&mvm->d0i3_suspend_mutex);
  934. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  935. mutex_unlock(&mvm->d0i3_suspend_mutex);
  936. return 0;
  937. }
  938. return __iwl_mvm_suspend(hw, wowlan, false);
  939. }
  940. /* converted data from the different status responses */
  941. struct iwl_wowlan_status_data {
  942. u16 pattern_number;
  943. u16 qos_seq_ctr[8];
  944. u32 wakeup_reasons;
  945. u32 wake_packet_length;
  946. u32 wake_packet_bufsize;
  947. const u8 *wake_packet;
  948. };
  949. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  950. struct ieee80211_vif *vif,
  951. struct iwl_wowlan_status_data *status)
  952. {
  953. struct sk_buff *pkt = NULL;
  954. struct cfg80211_wowlan_wakeup wakeup = {
  955. .pattern_idx = -1,
  956. };
  957. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  958. u32 reasons = status->wakeup_reasons;
  959. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  960. wakeup_report = NULL;
  961. goto report;
  962. }
  963. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  964. wakeup.magic_pkt = true;
  965. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  966. wakeup.pattern_idx =
  967. status->pattern_number;
  968. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  969. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  970. wakeup.disconnect = true;
  971. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  972. wakeup.gtk_rekey_failure = true;
  973. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  974. wakeup.rfkill_release = true;
  975. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  976. wakeup.eap_identity_req = true;
  977. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  978. wakeup.four_way_handshake = true;
  979. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  980. wakeup.tcp_connlost = true;
  981. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  982. wakeup.tcp_nomoretokens = true;
  983. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  984. wakeup.tcp_match = true;
  985. if (status->wake_packet_bufsize) {
  986. int pktsize = status->wake_packet_bufsize;
  987. int pktlen = status->wake_packet_length;
  988. const u8 *pktdata = status->wake_packet;
  989. struct ieee80211_hdr *hdr = (void *)pktdata;
  990. int truncated = pktlen - pktsize;
  991. /* this would be a firmware bug */
  992. if (WARN_ON_ONCE(truncated < 0))
  993. truncated = 0;
  994. if (ieee80211_is_data(hdr->frame_control)) {
  995. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  996. int ivlen = 0, icvlen = 4; /* also FCS */
  997. pkt = alloc_skb(pktsize, GFP_KERNEL);
  998. if (!pkt)
  999. goto report;
  1000. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1001. pktdata += hdrlen;
  1002. pktsize -= hdrlen;
  1003. if (ieee80211_has_protected(hdr->frame_control)) {
  1004. /*
  1005. * This is unlocked and using gtk_i(c)vlen,
  1006. * but since everything is under RTNL still
  1007. * that's not really a problem - changing
  1008. * it would be difficult.
  1009. */
  1010. if (is_multicast_ether_addr(hdr->addr1)) {
  1011. ivlen = mvm->gtk_ivlen;
  1012. icvlen += mvm->gtk_icvlen;
  1013. } else {
  1014. ivlen = mvm->ptk_ivlen;
  1015. icvlen += mvm->ptk_icvlen;
  1016. }
  1017. }
  1018. /* if truncated, FCS/ICV is (partially) gone */
  1019. if (truncated >= icvlen) {
  1020. icvlen = 0;
  1021. truncated -= icvlen;
  1022. } else {
  1023. icvlen -= truncated;
  1024. truncated = 0;
  1025. }
  1026. pktsize -= ivlen + icvlen;
  1027. pktdata += ivlen;
  1028. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1029. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1030. goto report;
  1031. wakeup.packet = pkt->data;
  1032. wakeup.packet_present_len = pkt->len;
  1033. wakeup.packet_len = pkt->len - truncated;
  1034. wakeup.packet_80211 = false;
  1035. } else {
  1036. int fcslen = 4;
  1037. if (truncated >= 4) {
  1038. truncated -= 4;
  1039. fcslen = 0;
  1040. } else {
  1041. fcslen -= truncated;
  1042. truncated = 0;
  1043. }
  1044. pktsize -= fcslen;
  1045. wakeup.packet = status->wake_packet;
  1046. wakeup.packet_present_len = pktsize;
  1047. wakeup.packet_len = pktlen - truncated;
  1048. wakeup.packet_80211 = true;
  1049. }
  1050. }
  1051. report:
  1052. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1053. kfree_skb(pkt);
  1054. }
  1055. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1056. struct ieee80211_key_seq *seq)
  1057. {
  1058. u64 pn;
  1059. pn = le64_to_cpu(sc->pn);
  1060. seq->ccmp.pn[0] = pn >> 40;
  1061. seq->ccmp.pn[1] = pn >> 32;
  1062. seq->ccmp.pn[2] = pn >> 24;
  1063. seq->ccmp.pn[3] = pn >> 16;
  1064. seq->ccmp.pn[4] = pn >> 8;
  1065. seq->ccmp.pn[5] = pn;
  1066. }
  1067. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1068. struct ieee80211_key_seq *seq)
  1069. {
  1070. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1071. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1072. }
  1073. static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
  1074. struct ieee80211_key_conf *key)
  1075. {
  1076. int tid;
  1077. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1078. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1079. struct ieee80211_key_seq seq = {};
  1080. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1081. ieee80211_set_key_rx_seq(key, tid, &seq);
  1082. }
  1083. }
  1084. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1085. struct ieee80211_key_conf *key)
  1086. {
  1087. int tid;
  1088. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1089. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1090. struct ieee80211_key_seq seq = {};
  1091. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1092. ieee80211_set_key_rx_seq(key, tid, &seq);
  1093. }
  1094. }
  1095. static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
  1096. struct iwl_wowlan_status *status)
  1097. {
  1098. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1099. switch (key->cipher) {
  1100. case WLAN_CIPHER_SUITE_CCMP:
  1101. iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
  1102. break;
  1103. case WLAN_CIPHER_SUITE_TKIP:
  1104. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1105. break;
  1106. default:
  1107. WARN_ON(1);
  1108. }
  1109. }
  1110. struct iwl_mvm_d3_gtk_iter_data {
  1111. struct iwl_wowlan_status *status;
  1112. void *last_gtk;
  1113. u32 cipher;
  1114. bool find_phase, unhandled_cipher;
  1115. int num_keys;
  1116. };
  1117. static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
  1118. struct ieee80211_vif *vif,
  1119. struct ieee80211_sta *sta,
  1120. struct ieee80211_key_conf *key,
  1121. void *_data)
  1122. {
  1123. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1124. if (data->unhandled_cipher)
  1125. return;
  1126. switch (key->cipher) {
  1127. case WLAN_CIPHER_SUITE_WEP40:
  1128. case WLAN_CIPHER_SUITE_WEP104:
  1129. /* ignore WEP completely, nothing to do */
  1130. return;
  1131. case WLAN_CIPHER_SUITE_CCMP:
  1132. case WLAN_CIPHER_SUITE_TKIP:
  1133. /* we support these */
  1134. break;
  1135. default:
  1136. /* everything else (even CMAC for MFP) - disconnect from AP */
  1137. data->unhandled_cipher = true;
  1138. return;
  1139. }
  1140. data->num_keys++;
  1141. /*
  1142. * pairwise key - update sequence counters only;
  1143. * note that this assumes no TDLS sessions are active
  1144. */
  1145. if (sta) {
  1146. struct ieee80211_key_seq seq = {};
  1147. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1148. if (data->find_phase)
  1149. return;
  1150. switch (key->cipher) {
  1151. case WLAN_CIPHER_SUITE_CCMP:
  1152. iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
  1153. iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
  1154. break;
  1155. case WLAN_CIPHER_SUITE_TKIP:
  1156. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1157. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1158. break;
  1159. }
  1160. ieee80211_set_key_tx_seq(key, &seq);
  1161. /* that's it for this key */
  1162. return;
  1163. }
  1164. if (data->find_phase) {
  1165. data->last_gtk = key;
  1166. data->cipher = key->cipher;
  1167. return;
  1168. }
  1169. if (data->status->num_of_gtk_rekeys)
  1170. ieee80211_remove_key(key);
  1171. else if (data->last_gtk == key)
  1172. iwl_mvm_set_key_rx_seq(key, data->status);
  1173. }
  1174. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1175. struct ieee80211_vif *vif,
  1176. struct iwl_wowlan_status *status)
  1177. {
  1178. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1179. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1180. .status = status,
  1181. };
  1182. u32 disconnection_reasons =
  1183. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1184. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1185. if (!status || !vif->bss_conf.bssid)
  1186. return false;
  1187. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1188. return false;
  1189. /* find last GTK that we used initially, if any */
  1190. gtkdata.find_phase = true;
  1191. ieee80211_iter_keys(mvm->hw, vif,
  1192. iwl_mvm_d3_update_gtks, &gtkdata);
  1193. /* not trying to keep connections with MFP/unhandled ciphers */
  1194. if (gtkdata.unhandled_cipher)
  1195. return false;
  1196. if (!gtkdata.num_keys)
  1197. goto out;
  1198. if (!gtkdata.last_gtk)
  1199. return false;
  1200. /*
  1201. * invalidate all other GTKs that might still exist and update
  1202. * the one that we used
  1203. */
  1204. gtkdata.find_phase = false;
  1205. ieee80211_iter_keys(mvm->hw, vif,
  1206. iwl_mvm_d3_update_gtks, &gtkdata);
  1207. if (status->num_of_gtk_rekeys) {
  1208. struct ieee80211_key_conf *key;
  1209. struct {
  1210. struct ieee80211_key_conf conf;
  1211. u8 key[32];
  1212. } conf = {
  1213. .conf.cipher = gtkdata.cipher,
  1214. .conf.keyidx = status->gtk.key_index,
  1215. };
  1216. switch (gtkdata.cipher) {
  1217. case WLAN_CIPHER_SUITE_CCMP:
  1218. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1219. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1220. WLAN_KEY_LEN_CCMP);
  1221. break;
  1222. case WLAN_CIPHER_SUITE_TKIP:
  1223. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1224. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1225. /* leave TX MIC key zeroed, we don't use it anyway */
  1226. memcpy(conf.conf.key +
  1227. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1228. status->gtk.tkip_mic_key, 8);
  1229. break;
  1230. }
  1231. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1232. if (IS_ERR(key))
  1233. return false;
  1234. iwl_mvm_set_key_rx_seq(key, status);
  1235. }
  1236. if (status->num_of_gtk_rekeys) {
  1237. __be64 replay_ctr =
  1238. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1239. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1240. (void *)&replay_ctr, GFP_KERNEL);
  1241. }
  1242. out:
  1243. mvmvif->seqno_valid = true;
  1244. /* +0x10 because the set API expects next-to-use, not last-used */
  1245. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1246. return true;
  1247. }
  1248. /* releases the MVM mutex */
  1249. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1250. struct ieee80211_vif *vif)
  1251. {
  1252. u32 base = mvm->error_event_table;
  1253. struct error_table_start {
  1254. /* cf. struct iwl_error_event_table */
  1255. u32 valid;
  1256. u32 error_id;
  1257. } err_info;
  1258. struct iwl_host_cmd cmd = {
  1259. .id = WOWLAN_GET_STATUSES,
  1260. .flags = CMD_WANT_SKB,
  1261. };
  1262. struct iwl_wowlan_status_data status;
  1263. struct iwl_wowlan_status *fw_status;
  1264. int ret, len, status_size, i;
  1265. bool keep;
  1266. struct ieee80211_sta *ap_sta;
  1267. struct iwl_mvm_sta *mvm_ap_sta;
  1268. iwl_trans_read_mem_bytes(mvm->trans, base,
  1269. &err_info, sizeof(err_info));
  1270. if (err_info.valid) {
  1271. IWL_INFO(mvm, "error table is valid (%d)\n",
  1272. err_info.valid);
  1273. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1274. struct cfg80211_wowlan_wakeup wakeup = {
  1275. .rfkill_release = true,
  1276. };
  1277. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1278. GFP_KERNEL);
  1279. }
  1280. goto out_unlock;
  1281. }
  1282. /* only for tracing for now */
  1283. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1284. if (ret)
  1285. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1286. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1287. if (ret) {
  1288. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1289. goto out_unlock;
  1290. }
  1291. /* RF-kill already asserted again... */
  1292. if (!cmd.resp_pkt)
  1293. goto out_unlock;
  1294. status_size = sizeof(*fw_status);
  1295. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1296. if (len < status_size) {
  1297. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1298. goto out_free_resp;
  1299. }
  1300. fw_status = (void *)cmd.resp_pkt->data;
  1301. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1302. for (i = 0; i < 8; i++)
  1303. status.qos_seq_ctr[i] =
  1304. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1305. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1306. status.wake_packet_length =
  1307. le32_to_cpu(fw_status->wake_packet_length);
  1308. status.wake_packet_bufsize =
  1309. le32_to_cpu(fw_status->wake_packet_bufsize);
  1310. status.wake_packet = fw_status->wake_packet;
  1311. if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
  1312. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1313. goto out_free_resp;
  1314. }
  1315. /* still at hard-coded place 0 for D3 image */
  1316. ap_sta = rcu_dereference_protected(
  1317. mvm->fw_id_to_mac_id[0],
  1318. lockdep_is_held(&mvm->mutex));
  1319. if (IS_ERR_OR_NULL(ap_sta))
  1320. goto out_free_resp;
  1321. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  1322. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1323. u16 seq = status.qos_seq_ctr[i];
  1324. /* firmware stores last-used value, we store next value */
  1325. seq += 0x10;
  1326. mvm_ap_sta->tid_data[i].seq_number = seq;
  1327. }
  1328. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1329. mutex_unlock(&mvm->mutex);
  1330. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1331. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1332. iwl_free_resp(&cmd);
  1333. return keep;
  1334. out_free_resp:
  1335. iwl_free_resp(&cmd);
  1336. out_unlock:
  1337. mutex_unlock(&mvm->mutex);
  1338. return false;
  1339. }
  1340. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1341. {
  1342. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1343. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1344. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1345. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1346. if (!mvm->store_d3_resume_sram)
  1347. return;
  1348. if (!mvm->d3_resume_sram) {
  1349. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1350. if (!mvm->d3_resume_sram)
  1351. return;
  1352. }
  1353. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1354. #endif
  1355. }
  1356. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1357. struct ieee80211_vif *vif)
  1358. {
  1359. /* skip the one we keep connection on */
  1360. if (data == vif)
  1361. return;
  1362. if (vif->type == NL80211_IFTYPE_STATION)
  1363. ieee80211_resume_disconnect(vif);
  1364. }
  1365. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1366. {
  1367. struct iwl_d3_iter_data resume_iter_data = {
  1368. .mvm = mvm,
  1369. };
  1370. struct ieee80211_vif *vif = NULL;
  1371. int ret;
  1372. enum iwl_d3_status d3_status;
  1373. bool keep = false;
  1374. mutex_lock(&mvm->mutex);
  1375. /* get the BSS vif pointer again */
  1376. ieee80211_iterate_active_interfaces_atomic(
  1377. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1378. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  1379. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  1380. goto out_unlock;
  1381. vif = resume_iter_data.vif;
  1382. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
  1383. if (ret)
  1384. goto out_unlock;
  1385. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1386. IWL_INFO(mvm, "Device was reset during suspend\n");
  1387. goto out_unlock;
  1388. }
  1389. /* query SRAM first in case we want event logging */
  1390. iwl_mvm_read_d3_sram(mvm);
  1391. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1392. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1393. if (keep)
  1394. mvm->keep_vif = vif;
  1395. #endif
  1396. /* has unlocked the mutex, so skip that */
  1397. goto out;
  1398. out_unlock:
  1399. mutex_unlock(&mvm->mutex);
  1400. out:
  1401. if (!test)
  1402. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1403. IEEE80211_IFACE_ITER_NORMAL,
  1404. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1405. /* return 1 to reconfigure the device */
  1406. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1407. return 1;
  1408. }
  1409. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1410. {
  1411. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1412. if (iwl_mvm_is_d0i3_supported(mvm)) {
  1413. bool exit_now;
  1414. mutex_lock(&mvm->d0i3_suspend_mutex);
  1415. __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1416. exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
  1417. &mvm->d0i3_suspend_flags);
  1418. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1419. if (exit_now)
  1420. _iwl_mvm_exit_d0i3(mvm);
  1421. return 0;
  1422. }
  1423. return __iwl_mvm_resume(mvm, false);
  1424. }
  1425. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1426. {
  1427. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1428. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1429. }
  1430. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1431. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1432. {
  1433. struct iwl_mvm *mvm = inode->i_private;
  1434. int err;
  1435. if (mvm->d3_test_active)
  1436. return -EBUSY;
  1437. file->private_data = inode->i_private;
  1438. ieee80211_stop_queues(mvm->hw);
  1439. synchronize_net();
  1440. /* start pseudo D3 */
  1441. rtnl_lock();
  1442. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1443. rtnl_unlock();
  1444. if (err > 0)
  1445. err = -EINVAL;
  1446. if (err) {
  1447. ieee80211_wake_queues(mvm->hw);
  1448. return err;
  1449. }
  1450. mvm->d3_test_active = true;
  1451. mvm->keep_vif = NULL;
  1452. return 0;
  1453. }
  1454. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1455. size_t count, loff_t *ppos)
  1456. {
  1457. struct iwl_mvm *mvm = file->private_data;
  1458. u32 pme_asserted;
  1459. while (true) {
  1460. /* read pme_ptr if available */
  1461. if (mvm->d3_test_pme_ptr) {
  1462. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1463. mvm->d3_test_pme_ptr);
  1464. if (pme_asserted)
  1465. break;
  1466. }
  1467. if (msleep_interruptible(100))
  1468. break;
  1469. }
  1470. return 0;
  1471. }
  1472. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1473. struct ieee80211_vif *vif)
  1474. {
  1475. /* skip the one we keep connection on */
  1476. if (_data == vif)
  1477. return;
  1478. if (vif->type == NL80211_IFTYPE_STATION)
  1479. ieee80211_connection_loss(vif);
  1480. }
  1481. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1482. {
  1483. struct iwl_mvm *mvm = inode->i_private;
  1484. int remaining_time = 10;
  1485. mvm->d3_test_active = false;
  1486. __iwl_mvm_resume(mvm, true);
  1487. iwl_abort_notification_waits(&mvm->notif_wait);
  1488. ieee80211_restart_hw(mvm->hw);
  1489. /* wait for restart and disconnect all interfaces */
  1490. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1491. remaining_time > 0) {
  1492. remaining_time--;
  1493. msleep(1000);
  1494. }
  1495. if (remaining_time == 0)
  1496. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1497. ieee80211_iterate_active_interfaces_atomic(
  1498. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1499. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1500. ieee80211_wake_queues(mvm->hw);
  1501. return 0;
  1502. }
  1503. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1504. .llseek = no_llseek,
  1505. .open = iwl_mvm_d3_test_open,
  1506. .read = iwl_mvm_d3_test_read,
  1507. .release = iwl_mvm_d3_test_release,
  1508. };
  1509. #endif