d3.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  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. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  539. struct ieee80211_sta *ap_sta)
  540. {
  541. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  542. struct ieee80211_chanctx_conf *ctx;
  543. u8 chains_static, chains_dynamic;
  544. struct cfg80211_chan_def chandef;
  545. int ret, i;
  546. struct iwl_binding_cmd binding_cmd = {};
  547. struct iwl_time_quota_cmd quota_cmd = {};
  548. u32 status;
  549. /* add back the PHY */
  550. if (WARN_ON(!mvmvif->phy_ctxt))
  551. return -EINVAL;
  552. rcu_read_lock();
  553. ctx = rcu_dereference(vif->chanctx_conf);
  554. if (WARN_ON(!ctx)) {
  555. rcu_read_unlock();
  556. return -EINVAL;
  557. }
  558. chandef = ctx->def;
  559. chains_static = ctx->rx_chains_static;
  560. chains_dynamic = ctx->rx_chains_dynamic;
  561. rcu_read_unlock();
  562. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  563. chains_static, chains_dynamic);
  564. if (ret)
  565. return ret;
  566. /* add back the MAC */
  567. mvmvif->uploaded = false;
  568. if (WARN_ON(!vif->bss_conf.assoc))
  569. return -EINVAL;
  570. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  571. if (ret)
  572. return ret;
  573. /* add back binding - XXX refactor? */
  574. binding_cmd.id_and_color =
  575. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  576. mvmvif->phy_ctxt->color));
  577. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  578. binding_cmd.phy =
  579. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  580. mvmvif->phy_ctxt->color));
  581. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  582. mvmvif->color));
  583. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  584. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  585. status = 0;
  586. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  587. sizeof(binding_cmd), &binding_cmd,
  588. &status);
  589. if (ret) {
  590. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  591. return ret;
  592. }
  593. if (status) {
  594. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  595. return -EIO;
  596. }
  597. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  598. if (ret)
  599. return ret;
  600. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  601. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  602. if (ret)
  603. return ret;
  604. /* and some quota */
  605. quota_cmd.quotas[0].id_and_color =
  606. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  607. mvmvif->phy_ctxt->color));
  608. quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  609. quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  610. for (i = 1; i < MAX_BINDINGS; i++)
  611. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  612. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  613. sizeof(quota_cmd), &quota_cmd);
  614. if (ret)
  615. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  616. return 0;
  617. }
  618. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  619. struct ieee80211_vif *vif)
  620. {
  621. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  622. struct iwl_nonqos_seq_query_cmd query_cmd = {
  623. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  624. .mac_id_n_color =
  625. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  626. mvmvif->color)),
  627. };
  628. struct iwl_host_cmd cmd = {
  629. .id = NON_QOS_TX_COUNTER_CMD,
  630. .flags = CMD_WANT_SKB,
  631. };
  632. int err;
  633. u32 size;
  634. cmd.data[0] = &query_cmd;
  635. cmd.len[0] = sizeof(query_cmd);
  636. err = iwl_mvm_send_cmd(mvm, &cmd);
  637. if (err)
  638. return err;
  639. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  640. if (size < sizeof(__le16)) {
  641. err = -EINVAL;
  642. } else {
  643. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  644. /* firmware returns next, not last-used seqno */
  645. err = (u16) (err - 0x10);
  646. }
  647. iwl_free_resp(&cmd);
  648. return err;
  649. }
  650. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  651. {
  652. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  653. struct iwl_nonqos_seq_query_cmd query_cmd = {
  654. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  655. .mac_id_n_color =
  656. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  657. mvmvif->color)),
  658. .value = cpu_to_le16(mvmvif->seqno),
  659. };
  660. /* return if called during restart, not resume from D3 */
  661. if (!mvmvif->seqno_valid)
  662. return;
  663. mvmvif->seqno_valid = false;
  664. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
  665. sizeof(query_cmd), &query_cmd))
  666. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  667. }
  668. static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
  669. {
  670. iwl_mvm_cancel_scan(mvm);
  671. iwl_trans_stop_device(mvm->trans);
  672. /*
  673. * Set the HW restart bit -- this is mostly true as we're
  674. * going to load new firmware and reprogram that, though
  675. * the reprogramming is going to be manual to avoid adding
  676. * all the MACs that aren't support.
  677. * We don't have to clear up everything though because the
  678. * reprogramming is manual. When we resume, we'll actually
  679. * go through a proper restart sequence again to switch
  680. * back to the runtime firmware image.
  681. */
  682. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  683. /* We reprogram keys and shouldn't allocate new key indices */
  684. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  685. mvm->ptk_ivlen = 0;
  686. mvm->ptk_icvlen = 0;
  687. mvm->ptk_ivlen = 0;
  688. mvm->ptk_icvlen = 0;
  689. return iwl_mvm_load_d3_fw(mvm);
  690. }
  691. static int
  692. iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
  693. struct cfg80211_wowlan *wowlan,
  694. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  695. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  696. struct ieee80211_sta *ap_sta)
  697. {
  698. int ret;
  699. struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  700. /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
  701. wowlan_config_cmd->is_11n_connection =
  702. ap_sta->ht_cap.ht_supported;
  703. /* Query the last used seqno and set it */
  704. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  705. if (ret < 0)
  706. return ret;
  707. wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
  708. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
  709. if (wowlan->disconnect)
  710. wowlan_config_cmd->wakeup_filter |=
  711. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  712. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  713. if (wowlan->magic_pkt)
  714. wowlan_config_cmd->wakeup_filter |=
  715. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  716. if (wowlan->gtk_rekey_failure)
  717. wowlan_config_cmd->wakeup_filter |=
  718. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  719. if (wowlan->eap_identity_req)
  720. wowlan_config_cmd->wakeup_filter |=
  721. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  722. if (wowlan->four_way_handshake)
  723. wowlan_config_cmd->wakeup_filter |=
  724. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  725. if (wowlan->n_patterns)
  726. wowlan_config_cmd->wakeup_filter |=
  727. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  728. if (wowlan->rfkill_release)
  729. wowlan_config_cmd->wakeup_filter |=
  730. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  731. if (wowlan->tcp) {
  732. /*
  733. * Set the "link change" (really "link lost") flag as well
  734. * since that implies losing the TCP connection.
  735. */
  736. wowlan_config_cmd->wakeup_filter |=
  737. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  738. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  739. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  740. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  741. }
  742. return 0;
  743. }
  744. static int
  745. iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
  746. struct cfg80211_wowlan *wowlan,
  747. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  748. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  749. struct ieee80211_sta *ap_sta)
  750. {
  751. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  752. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  753. struct wowlan_key_data key_data = {
  754. .use_rsc_tsc = false,
  755. .tkip = &tkip_cmd,
  756. .use_tkip = false,
  757. };
  758. int ret;
  759. ret = iwl_mvm_switch_to_d3(mvm);
  760. if (ret)
  761. return ret;
  762. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  763. if (ret)
  764. return ret;
  765. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  766. if (!key_data.rsc_tsc)
  767. return -ENOMEM;
  768. if (!iwlwifi_mod_params.sw_crypto) {
  769. /*
  770. * This needs to be unlocked due to lock ordering
  771. * constraints. Since we're in the suspend path
  772. * that isn't really a problem though.
  773. */
  774. mutex_unlock(&mvm->mutex);
  775. ieee80211_iter_keys(mvm->hw, vif,
  776. iwl_mvm_wowlan_program_keys,
  777. &key_data);
  778. mutex_lock(&mvm->mutex);
  779. if (key_data.error) {
  780. ret = -EIO;
  781. goto out;
  782. }
  783. if (key_data.use_rsc_tsc) {
  784. struct iwl_host_cmd rsc_tsc_cmd = {
  785. .id = WOWLAN_TSC_RSC_PARAM,
  786. .data[0] = key_data.rsc_tsc,
  787. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  788. .len[0] = sizeof(*key_data.rsc_tsc),
  789. };
  790. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  791. if (ret)
  792. goto out;
  793. }
  794. if (key_data.use_tkip) {
  795. ret = iwl_mvm_send_cmd_pdu(mvm,
  796. WOWLAN_TKIP_PARAM,
  797. 0, sizeof(tkip_cmd),
  798. &tkip_cmd);
  799. if (ret)
  800. goto out;
  801. }
  802. if (mvmvif->rekey_data.valid) {
  803. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  804. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  805. NL80211_KCK_LEN);
  806. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  807. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  808. NL80211_KEK_LEN);
  809. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  810. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  811. ret = iwl_mvm_send_cmd_pdu(mvm,
  812. WOWLAN_KEK_KCK_MATERIAL, 0,
  813. sizeof(kek_kck_cmd),
  814. &kek_kck_cmd);
  815. if (ret)
  816. goto out;
  817. }
  818. }
  819. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  820. sizeof(*wowlan_config_cmd),
  821. wowlan_config_cmd);
  822. if (ret)
  823. goto out;
  824. ret = iwl_mvm_send_patterns(mvm, wowlan);
  825. if (ret)
  826. goto out;
  827. ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
  828. if (ret)
  829. goto out;
  830. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  831. out:
  832. kfree(key_data.rsc_tsc);
  833. return ret;
  834. }
  835. static int
  836. iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
  837. struct cfg80211_wowlan *wowlan,
  838. struct cfg80211_sched_scan_request *nd_config,
  839. struct ieee80211_vif *vif)
  840. {
  841. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  842. int ret;
  843. ret = iwl_mvm_switch_to_d3(mvm);
  844. if (ret)
  845. return ret;
  846. /* rfkill release can be either for wowlan or netdetect */
  847. if (wowlan->rfkill_release)
  848. wowlan_config_cmd.wakeup_filter |=
  849. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  850. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  851. sizeof(wowlan_config_cmd),
  852. &wowlan_config_cmd);
  853. if (ret)
  854. return ret;
  855. ret = iwl_mvm_scan_offload_start(mvm, vif, nd_config, &mvm->nd_ies);
  856. if (ret)
  857. return ret;
  858. if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
  859. return -EBUSY;
  860. /* save the sched scan matchsets... */
  861. if (nd_config->n_match_sets) {
  862. mvm->nd_match_sets = kmemdup(nd_config->match_sets,
  863. sizeof(*nd_config->match_sets) *
  864. nd_config->n_match_sets,
  865. GFP_KERNEL);
  866. if (mvm->nd_match_sets)
  867. mvm->n_nd_match_sets = nd_config->n_match_sets;
  868. }
  869. /* ...and the sched scan channels for later reporting */
  870. mvm->nd_channels = kmemdup(nd_config->channels,
  871. sizeof(*nd_config->channels) *
  872. nd_config->n_channels,
  873. GFP_KERNEL);
  874. if (mvm->nd_channels)
  875. mvm->n_nd_channels = nd_config->n_channels;
  876. return 0;
  877. }
  878. static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
  879. {
  880. kfree(mvm->nd_match_sets);
  881. mvm->nd_match_sets = NULL;
  882. mvm->n_nd_match_sets = 0;
  883. kfree(mvm->nd_channels);
  884. mvm->nd_channels = NULL;
  885. mvm->n_nd_channels = 0;
  886. }
  887. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  888. struct cfg80211_wowlan *wowlan,
  889. bool test)
  890. {
  891. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  892. struct ieee80211_vif *vif = NULL;
  893. struct iwl_mvm_vif *mvmvif = NULL;
  894. struct ieee80211_sta *ap_sta = NULL;
  895. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  896. /*
  897. * Program the minimum sleep time to 10 seconds, as many
  898. * platforms have issues processing a wakeup signal while
  899. * still being in the process of suspending.
  900. */
  901. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  902. };
  903. struct iwl_host_cmd d3_cfg_cmd = {
  904. .id = D3_CONFIG_CMD,
  905. .flags = CMD_WANT_SKB,
  906. .data[0] = &d3_cfg_cmd_data,
  907. .len[0] = sizeof(d3_cfg_cmd_data),
  908. };
  909. int ret;
  910. int len __maybe_unused;
  911. if (!wowlan) {
  912. /*
  913. * mac80211 shouldn't get here, but for D3 test
  914. * it doesn't warrant a warning
  915. */
  916. WARN_ON(!test);
  917. return -EINVAL;
  918. }
  919. mutex_lock(&mvm->mutex);
  920. vif = iwl_mvm_get_bss_vif(mvm);
  921. if (IS_ERR_OR_NULL(vif)) {
  922. ret = 1;
  923. goto out_noreset;
  924. }
  925. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  926. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
  927. /* if we're not associated, this must be netdetect */
  928. if (!wowlan->nd_config && !mvm->nd_config) {
  929. ret = 1;
  930. goto out_noreset;
  931. }
  932. ret = iwl_mvm_netdetect_config(
  933. mvm, wowlan, wowlan->nd_config ?: mvm->nd_config, vif);
  934. if (ret)
  935. goto out;
  936. mvm->net_detect = true;
  937. } else {
  938. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  939. ap_sta = rcu_dereference_protected(
  940. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  941. lockdep_is_held(&mvm->mutex));
  942. if (IS_ERR_OR_NULL(ap_sta)) {
  943. ret = -EINVAL;
  944. goto out_noreset;
  945. }
  946. ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  947. vif, mvmvif, ap_sta);
  948. if (ret)
  949. goto out_noreset;
  950. ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  951. vif, mvmvif, ap_sta);
  952. if (ret)
  953. goto out;
  954. mvm->net_detect = false;
  955. }
  956. ret = iwl_mvm_power_update_device(mvm);
  957. if (ret)
  958. goto out;
  959. ret = iwl_mvm_power_update_mac(mvm);
  960. if (ret)
  961. goto out;
  962. #ifdef CONFIG_IWLWIFI_DEBUGFS
  963. if (mvm->d3_wake_sysassert)
  964. d3_cfg_cmd_data.wakeup_flags |=
  965. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  966. #endif
  967. /* must be last -- this switches firmware state */
  968. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  969. if (ret)
  970. goto out;
  971. #ifdef CONFIG_IWLWIFI_DEBUGFS
  972. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  973. if (len >= sizeof(u32)) {
  974. mvm->d3_test_pme_ptr =
  975. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  976. }
  977. #endif
  978. iwl_free_resp(&d3_cfg_cmd);
  979. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  980. iwl_trans_d3_suspend(mvm->trans, test);
  981. out:
  982. if (ret < 0) {
  983. ieee80211_restart_hw(mvm->hw);
  984. iwl_mvm_free_nd(mvm);
  985. }
  986. out_noreset:
  987. mutex_unlock(&mvm->mutex);
  988. return ret;
  989. }
  990. static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
  991. {
  992. struct iwl_notification_wait wait_d3;
  993. static const u8 d3_notif[] = { D3_CONFIG_CMD };
  994. int ret;
  995. iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
  996. d3_notif, ARRAY_SIZE(d3_notif),
  997. NULL, NULL);
  998. ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
  999. if (ret)
  1000. goto remove_notif;
  1001. ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
  1002. WARN_ON_ONCE(ret);
  1003. return ret;
  1004. remove_notif:
  1005. iwl_remove_notification(&mvm->notif_wait, &wait_d3);
  1006. return ret;
  1007. }
  1008. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1009. {
  1010. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1011. iwl_trans_suspend(mvm->trans);
  1012. if (wowlan->any) {
  1013. /* 'any' trigger means d0i3 usage */
  1014. if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
  1015. int ret = iwl_mvm_enter_d0i3_sync(mvm);
  1016. if (ret)
  1017. return ret;
  1018. }
  1019. mutex_lock(&mvm->d0i3_suspend_mutex);
  1020. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1021. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1022. return 0;
  1023. }
  1024. return __iwl_mvm_suspend(hw, wowlan, false);
  1025. }
  1026. /* converted data from the different status responses */
  1027. struct iwl_wowlan_status_data {
  1028. u16 pattern_number;
  1029. u16 qos_seq_ctr[8];
  1030. u32 wakeup_reasons;
  1031. u32 wake_packet_length;
  1032. u32 wake_packet_bufsize;
  1033. const u8 *wake_packet;
  1034. };
  1035. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1036. struct ieee80211_vif *vif,
  1037. struct iwl_wowlan_status_data *status)
  1038. {
  1039. struct sk_buff *pkt = NULL;
  1040. struct cfg80211_wowlan_wakeup wakeup = {
  1041. .pattern_idx = -1,
  1042. };
  1043. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1044. u32 reasons = status->wakeup_reasons;
  1045. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1046. wakeup_report = NULL;
  1047. goto report;
  1048. }
  1049. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1050. wakeup.magic_pkt = true;
  1051. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1052. wakeup.pattern_idx =
  1053. status->pattern_number;
  1054. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1055. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1056. wakeup.disconnect = true;
  1057. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1058. wakeup.gtk_rekey_failure = true;
  1059. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1060. wakeup.rfkill_release = true;
  1061. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1062. wakeup.eap_identity_req = true;
  1063. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1064. wakeup.four_way_handshake = true;
  1065. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1066. wakeup.tcp_connlost = true;
  1067. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1068. wakeup.tcp_nomoretokens = true;
  1069. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1070. wakeup.tcp_match = true;
  1071. if (status->wake_packet_bufsize) {
  1072. int pktsize = status->wake_packet_bufsize;
  1073. int pktlen = status->wake_packet_length;
  1074. const u8 *pktdata = status->wake_packet;
  1075. struct ieee80211_hdr *hdr = (void *)pktdata;
  1076. int truncated = pktlen - pktsize;
  1077. /* this would be a firmware bug */
  1078. if (WARN_ON_ONCE(truncated < 0))
  1079. truncated = 0;
  1080. if (ieee80211_is_data(hdr->frame_control)) {
  1081. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1082. int ivlen = 0, icvlen = 4; /* also FCS */
  1083. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1084. if (!pkt)
  1085. goto report;
  1086. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1087. pktdata += hdrlen;
  1088. pktsize -= hdrlen;
  1089. if (ieee80211_has_protected(hdr->frame_control)) {
  1090. /*
  1091. * This is unlocked and using gtk_i(c)vlen,
  1092. * but since everything is under RTNL still
  1093. * that's not really a problem - changing
  1094. * it would be difficult.
  1095. */
  1096. if (is_multicast_ether_addr(hdr->addr1)) {
  1097. ivlen = mvm->gtk_ivlen;
  1098. icvlen += mvm->gtk_icvlen;
  1099. } else {
  1100. ivlen = mvm->ptk_ivlen;
  1101. icvlen += mvm->ptk_icvlen;
  1102. }
  1103. }
  1104. /* if truncated, FCS/ICV is (partially) gone */
  1105. if (truncated >= icvlen) {
  1106. icvlen = 0;
  1107. truncated -= icvlen;
  1108. } else {
  1109. icvlen -= truncated;
  1110. truncated = 0;
  1111. }
  1112. pktsize -= ivlen + icvlen;
  1113. pktdata += ivlen;
  1114. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1115. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1116. goto report;
  1117. wakeup.packet = pkt->data;
  1118. wakeup.packet_present_len = pkt->len;
  1119. wakeup.packet_len = pkt->len - truncated;
  1120. wakeup.packet_80211 = false;
  1121. } else {
  1122. int fcslen = 4;
  1123. if (truncated >= 4) {
  1124. truncated -= 4;
  1125. fcslen = 0;
  1126. } else {
  1127. fcslen -= truncated;
  1128. truncated = 0;
  1129. }
  1130. pktsize -= fcslen;
  1131. wakeup.packet = status->wake_packet;
  1132. wakeup.packet_present_len = pktsize;
  1133. wakeup.packet_len = pktlen - truncated;
  1134. wakeup.packet_80211 = true;
  1135. }
  1136. }
  1137. report:
  1138. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1139. kfree_skb(pkt);
  1140. }
  1141. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1142. struct ieee80211_key_seq *seq)
  1143. {
  1144. u64 pn;
  1145. pn = le64_to_cpu(sc->pn);
  1146. seq->ccmp.pn[0] = pn >> 40;
  1147. seq->ccmp.pn[1] = pn >> 32;
  1148. seq->ccmp.pn[2] = pn >> 24;
  1149. seq->ccmp.pn[3] = pn >> 16;
  1150. seq->ccmp.pn[4] = pn >> 8;
  1151. seq->ccmp.pn[5] = pn;
  1152. }
  1153. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1154. struct ieee80211_key_seq *seq)
  1155. {
  1156. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1157. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1158. }
  1159. static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
  1160. struct ieee80211_key_conf *key)
  1161. {
  1162. int tid;
  1163. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1164. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1165. struct ieee80211_key_seq seq = {};
  1166. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1167. ieee80211_set_key_rx_seq(key, tid, &seq);
  1168. }
  1169. }
  1170. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1171. struct ieee80211_key_conf *key)
  1172. {
  1173. int tid;
  1174. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1175. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1176. struct ieee80211_key_seq seq = {};
  1177. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1178. ieee80211_set_key_rx_seq(key, tid, &seq);
  1179. }
  1180. }
  1181. static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
  1182. struct iwl_wowlan_status *status)
  1183. {
  1184. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1185. switch (key->cipher) {
  1186. case WLAN_CIPHER_SUITE_CCMP:
  1187. iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
  1188. break;
  1189. case WLAN_CIPHER_SUITE_TKIP:
  1190. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1191. break;
  1192. default:
  1193. WARN_ON(1);
  1194. }
  1195. }
  1196. struct iwl_mvm_d3_gtk_iter_data {
  1197. struct iwl_wowlan_status *status;
  1198. void *last_gtk;
  1199. u32 cipher;
  1200. bool find_phase, unhandled_cipher;
  1201. int num_keys;
  1202. };
  1203. static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
  1204. struct ieee80211_vif *vif,
  1205. struct ieee80211_sta *sta,
  1206. struct ieee80211_key_conf *key,
  1207. void *_data)
  1208. {
  1209. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1210. if (data->unhandled_cipher)
  1211. return;
  1212. switch (key->cipher) {
  1213. case WLAN_CIPHER_SUITE_WEP40:
  1214. case WLAN_CIPHER_SUITE_WEP104:
  1215. /* ignore WEP completely, nothing to do */
  1216. return;
  1217. case WLAN_CIPHER_SUITE_CCMP:
  1218. case WLAN_CIPHER_SUITE_TKIP:
  1219. /* we support these */
  1220. break;
  1221. default:
  1222. /* everything else (even CMAC for MFP) - disconnect from AP */
  1223. data->unhandled_cipher = true;
  1224. return;
  1225. }
  1226. data->num_keys++;
  1227. /*
  1228. * pairwise key - update sequence counters only;
  1229. * note that this assumes no TDLS sessions are active
  1230. */
  1231. if (sta) {
  1232. struct ieee80211_key_seq seq = {};
  1233. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1234. if (data->find_phase)
  1235. return;
  1236. switch (key->cipher) {
  1237. case WLAN_CIPHER_SUITE_CCMP:
  1238. iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
  1239. iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
  1240. break;
  1241. case WLAN_CIPHER_SUITE_TKIP:
  1242. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1243. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1244. break;
  1245. }
  1246. ieee80211_set_key_tx_seq(key, &seq);
  1247. /* that's it for this key */
  1248. return;
  1249. }
  1250. if (data->find_phase) {
  1251. data->last_gtk = key;
  1252. data->cipher = key->cipher;
  1253. return;
  1254. }
  1255. if (data->status->num_of_gtk_rekeys)
  1256. ieee80211_remove_key(key);
  1257. else if (data->last_gtk == key)
  1258. iwl_mvm_set_key_rx_seq(key, data->status);
  1259. }
  1260. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1261. struct ieee80211_vif *vif,
  1262. struct iwl_wowlan_status *status)
  1263. {
  1264. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1265. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1266. .status = status,
  1267. };
  1268. u32 disconnection_reasons =
  1269. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1270. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1271. if (!status || !vif->bss_conf.bssid)
  1272. return false;
  1273. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1274. return false;
  1275. /* find last GTK that we used initially, if any */
  1276. gtkdata.find_phase = true;
  1277. ieee80211_iter_keys(mvm->hw, vif,
  1278. iwl_mvm_d3_update_gtks, &gtkdata);
  1279. /* not trying to keep connections with MFP/unhandled ciphers */
  1280. if (gtkdata.unhandled_cipher)
  1281. return false;
  1282. if (!gtkdata.num_keys)
  1283. goto out;
  1284. if (!gtkdata.last_gtk)
  1285. return false;
  1286. /*
  1287. * invalidate all other GTKs that might still exist and update
  1288. * the one that we used
  1289. */
  1290. gtkdata.find_phase = false;
  1291. ieee80211_iter_keys(mvm->hw, vif,
  1292. iwl_mvm_d3_update_gtks, &gtkdata);
  1293. if (status->num_of_gtk_rekeys) {
  1294. struct ieee80211_key_conf *key;
  1295. struct {
  1296. struct ieee80211_key_conf conf;
  1297. u8 key[32];
  1298. } conf = {
  1299. .conf.cipher = gtkdata.cipher,
  1300. .conf.keyidx = status->gtk.key_index,
  1301. };
  1302. switch (gtkdata.cipher) {
  1303. case WLAN_CIPHER_SUITE_CCMP:
  1304. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1305. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1306. WLAN_KEY_LEN_CCMP);
  1307. break;
  1308. case WLAN_CIPHER_SUITE_TKIP:
  1309. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1310. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1311. /* leave TX MIC key zeroed, we don't use it anyway */
  1312. memcpy(conf.conf.key +
  1313. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1314. status->gtk.tkip_mic_key, 8);
  1315. break;
  1316. }
  1317. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1318. if (IS_ERR(key))
  1319. return false;
  1320. iwl_mvm_set_key_rx_seq(key, status);
  1321. }
  1322. if (status->num_of_gtk_rekeys) {
  1323. __be64 replay_ctr =
  1324. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1325. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1326. (void *)&replay_ctr, GFP_KERNEL);
  1327. }
  1328. out:
  1329. mvmvif->seqno_valid = true;
  1330. /* +0x10 because the set API expects next-to-use, not last-used */
  1331. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1332. return true;
  1333. }
  1334. static struct iwl_wowlan_status *
  1335. iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1336. {
  1337. u32 base = mvm->error_event_table;
  1338. struct error_table_start {
  1339. /* cf. struct iwl_error_event_table */
  1340. u32 valid;
  1341. u32 error_id;
  1342. } err_info;
  1343. struct iwl_host_cmd cmd = {
  1344. .id = WOWLAN_GET_STATUSES,
  1345. .flags = CMD_WANT_SKB,
  1346. };
  1347. struct iwl_wowlan_status *status, *fw_status;
  1348. int ret, len, status_size;
  1349. iwl_trans_read_mem_bytes(mvm->trans, base,
  1350. &err_info, sizeof(err_info));
  1351. if (err_info.valid) {
  1352. IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
  1353. err_info.valid, err_info.error_id);
  1354. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1355. struct cfg80211_wowlan_wakeup wakeup = {
  1356. .rfkill_release = true,
  1357. };
  1358. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1359. GFP_KERNEL);
  1360. }
  1361. return ERR_PTR(-EIO);
  1362. }
  1363. /* only for tracing for now */
  1364. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1365. if (ret)
  1366. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1367. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1368. if (ret) {
  1369. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1370. return ERR_PTR(ret);
  1371. }
  1372. /* RF-kill already asserted again... */
  1373. if (!cmd.resp_pkt) {
  1374. ret = -ERFKILL;
  1375. goto out_free_resp;
  1376. }
  1377. status_size = sizeof(*fw_status);
  1378. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1379. if (len < status_size) {
  1380. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1381. ret = -EIO;
  1382. goto out_free_resp;
  1383. }
  1384. status = (void *)cmd.resp_pkt->data;
  1385. if (len != (status_size +
  1386. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
  1387. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1388. ret = -EIO;
  1389. goto out_free_resp;
  1390. }
  1391. fw_status = kmemdup(status, len, GFP_KERNEL);
  1392. out_free_resp:
  1393. iwl_free_resp(&cmd);
  1394. return ret ? ERR_PTR(ret) : fw_status;
  1395. }
  1396. /* releases the MVM mutex */
  1397. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1398. struct ieee80211_vif *vif)
  1399. {
  1400. struct iwl_wowlan_status_data status;
  1401. struct iwl_wowlan_status *fw_status;
  1402. int i;
  1403. bool keep;
  1404. struct ieee80211_sta *ap_sta;
  1405. struct iwl_mvm_sta *mvm_ap_sta;
  1406. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1407. if (IS_ERR_OR_NULL(fw_status))
  1408. goto out_unlock;
  1409. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1410. for (i = 0; i < 8; i++)
  1411. status.qos_seq_ctr[i] =
  1412. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1413. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1414. status.wake_packet_length =
  1415. le32_to_cpu(fw_status->wake_packet_length);
  1416. status.wake_packet_bufsize =
  1417. le32_to_cpu(fw_status->wake_packet_bufsize);
  1418. status.wake_packet = fw_status->wake_packet;
  1419. /* still at hard-coded place 0 for D3 image */
  1420. ap_sta = rcu_dereference_protected(
  1421. mvm->fw_id_to_mac_id[0],
  1422. lockdep_is_held(&mvm->mutex));
  1423. if (IS_ERR_OR_NULL(ap_sta))
  1424. goto out_free;
  1425. mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  1426. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1427. u16 seq = status.qos_seq_ctr[i];
  1428. /* firmware stores last-used value, we store next value */
  1429. seq += 0x10;
  1430. mvm_ap_sta->tid_data[i].seq_number = seq;
  1431. }
  1432. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1433. mutex_unlock(&mvm->mutex);
  1434. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1435. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1436. kfree(fw_status);
  1437. return keep;
  1438. out_free:
  1439. kfree(fw_status);
  1440. out_unlock:
  1441. mutex_unlock(&mvm->mutex);
  1442. return false;
  1443. }
  1444. struct iwl_mvm_nd_query_results {
  1445. u32 matched_profiles;
  1446. struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
  1447. };
  1448. static int
  1449. iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
  1450. struct iwl_mvm_nd_query_results *results)
  1451. {
  1452. struct iwl_scan_offload_profiles_query *query;
  1453. struct iwl_host_cmd cmd = {
  1454. .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
  1455. .flags = CMD_WANT_SKB,
  1456. };
  1457. int ret, len;
  1458. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1459. if (ret) {
  1460. IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
  1461. return ret;
  1462. }
  1463. /* RF-kill already asserted again... */
  1464. if (!cmd.resp_pkt) {
  1465. ret = -ERFKILL;
  1466. goto out_free_resp;
  1467. }
  1468. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1469. if (len < sizeof(*query)) {
  1470. IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
  1471. ret = -EIO;
  1472. goto out_free_resp;
  1473. }
  1474. query = (void *)cmd.resp_pkt->data;
  1475. results->matched_profiles = le32_to_cpu(query->matched_profiles);
  1476. memcpy(results->matches, query->matches, sizeof(results->matches));
  1477. out_free_resp:
  1478. iwl_free_resp(&cmd);
  1479. return ret;
  1480. }
  1481. static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
  1482. struct ieee80211_vif *vif)
  1483. {
  1484. struct cfg80211_wowlan_nd_info *net_detect = NULL;
  1485. struct cfg80211_wowlan_wakeup wakeup = {
  1486. .pattern_idx = -1,
  1487. };
  1488. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1489. struct iwl_mvm_nd_query_results query;
  1490. struct iwl_wowlan_status *fw_status;
  1491. unsigned long matched_profiles;
  1492. u32 reasons = 0;
  1493. int i, j, n_matches, ret;
  1494. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1495. if (!IS_ERR_OR_NULL(fw_status))
  1496. reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1497. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1498. wakeup.rfkill_release = true;
  1499. if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
  1500. goto out;
  1501. ret = iwl_mvm_netdetect_query_results(mvm, &query);
  1502. if (ret || !query.matched_profiles) {
  1503. wakeup_report = NULL;
  1504. goto out;
  1505. }
  1506. matched_profiles = query.matched_profiles;
  1507. if (mvm->n_nd_match_sets) {
  1508. n_matches = hweight_long(matched_profiles);
  1509. } else {
  1510. IWL_ERR(mvm, "no net detect match information available\n");
  1511. n_matches = 0;
  1512. }
  1513. net_detect = kzalloc(sizeof(*net_detect) +
  1514. (n_matches * sizeof(net_detect->matches[0])),
  1515. GFP_KERNEL);
  1516. if (!net_detect || !n_matches)
  1517. goto out_report_nd;
  1518. for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
  1519. struct iwl_scan_offload_profile_match *fw_match;
  1520. struct cfg80211_wowlan_nd_match *match;
  1521. int n_channels = 0;
  1522. fw_match = &query.matches[i];
  1523. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
  1524. n_channels += hweight8(fw_match->matching_channels[j]);
  1525. match = kzalloc(sizeof(*match) +
  1526. (n_channels * sizeof(*match->channels)),
  1527. GFP_KERNEL);
  1528. if (!match)
  1529. goto out_report_nd;
  1530. net_detect->matches[net_detect->n_matches++] = match;
  1531. match->ssid.ssid_len = mvm->nd_match_sets[i].ssid.ssid_len;
  1532. memcpy(match->ssid.ssid, mvm->nd_match_sets[i].ssid.ssid,
  1533. match->ssid.ssid_len);
  1534. if (mvm->n_nd_channels < n_channels)
  1535. continue;
  1536. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
  1537. if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
  1538. match->channels[match->n_channels++] =
  1539. mvm->nd_channels[j]->center_freq;
  1540. }
  1541. out_report_nd:
  1542. wakeup.net_detect = net_detect;
  1543. out:
  1544. iwl_mvm_free_nd(mvm);
  1545. mutex_unlock(&mvm->mutex);
  1546. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1547. if (net_detect) {
  1548. for (i = 0; i < net_detect->n_matches; i++)
  1549. kfree(net_detect->matches[i]);
  1550. kfree(net_detect);
  1551. }
  1552. }
  1553. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1554. {
  1555. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1556. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1557. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1558. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1559. if (!mvm->store_d3_resume_sram)
  1560. return;
  1561. if (!mvm->d3_resume_sram) {
  1562. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1563. if (!mvm->d3_resume_sram)
  1564. return;
  1565. }
  1566. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1567. #endif
  1568. }
  1569. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1570. struct ieee80211_vif *vif)
  1571. {
  1572. /* skip the one we keep connection on */
  1573. if (data == vif)
  1574. return;
  1575. if (vif->type == NL80211_IFTYPE_STATION)
  1576. ieee80211_resume_disconnect(vif);
  1577. }
  1578. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1579. {
  1580. struct ieee80211_vif *vif = NULL;
  1581. int ret;
  1582. enum iwl_d3_status d3_status;
  1583. bool keep = false;
  1584. mutex_lock(&mvm->mutex);
  1585. /* get the BSS vif pointer again */
  1586. vif = iwl_mvm_get_bss_vif(mvm);
  1587. if (IS_ERR_OR_NULL(vif))
  1588. goto out_unlock;
  1589. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
  1590. if (ret)
  1591. goto out_unlock;
  1592. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1593. IWL_INFO(mvm, "Device was reset during suspend\n");
  1594. goto out_unlock;
  1595. }
  1596. /* query SRAM first in case we want event logging */
  1597. iwl_mvm_read_d3_sram(mvm);
  1598. if (mvm->net_detect) {
  1599. iwl_mvm_query_netdetect_reasons(mvm, vif);
  1600. /* has unlocked the mutex, so skip that */
  1601. goto out;
  1602. } else {
  1603. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1604. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1605. if (keep)
  1606. mvm->keep_vif = vif;
  1607. /* has unlocked the mutex, so skip that */
  1608. goto out_iterate;
  1609. #endif
  1610. }
  1611. out_unlock:
  1612. mutex_unlock(&mvm->mutex);
  1613. out_iterate:
  1614. if (!test)
  1615. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1616. IEEE80211_IFACE_ITER_NORMAL,
  1617. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1618. out:
  1619. /* return 1 to reconfigure the device */
  1620. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1621. set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
  1622. return 1;
  1623. }
  1624. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1625. {
  1626. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1627. iwl_trans_resume(mvm->trans);
  1628. if (mvm->hw->wiphy->wowlan_config->any) {
  1629. /* 'any' trigger means d0i3 usage */
  1630. if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
  1631. int ret = iwl_mvm_exit_d0i3(hw->priv);
  1632. if (ret)
  1633. return ret;
  1634. /*
  1635. * d0i3 exit will be deferred until reconfig_complete.
  1636. * make sure there we are out of d0i3.
  1637. */
  1638. }
  1639. return 0;
  1640. }
  1641. return __iwl_mvm_resume(mvm, false);
  1642. }
  1643. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1644. {
  1645. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1646. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1647. }
  1648. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1649. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1650. {
  1651. struct iwl_mvm *mvm = inode->i_private;
  1652. int err;
  1653. if (mvm->d3_test_active)
  1654. return -EBUSY;
  1655. file->private_data = inode->i_private;
  1656. ieee80211_stop_queues(mvm->hw);
  1657. synchronize_net();
  1658. /* start pseudo D3 */
  1659. rtnl_lock();
  1660. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1661. rtnl_unlock();
  1662. if (err > 0)
  1663. err = -EINVAL;
  1664. if (err) {
  1665. ieee80211_wake_queues(mvm->hw);
  1666. return err;
  1667. }
  1668. mvm->d3_test_active = true;
  1669. mvm->keep_vif = NULL;
  1670. return 0;
  1671. }
  1672. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1673. size_t count, loff_t *ppos)
  1674. {
  1675. struct iwl_mvm *mvm = file->private_data;
  1676. u32 pme_asserted;
  1677. while (true) {
  1678. /* read pme_ptr if available */
  1679. if (mvm->d3_test_pme_ptr) {
  1680. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1681. mvm->d3_test_pme_ptr);
  1682. if (pme_asserted)
  1683. break;
  1684. }
  1685. if (msleep_interruptible(100))
  1686. break;
  1687. }
  1688. return 0;
  1689. }
  1690. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1691. struct ieee80211_vif *vif)
  1692. {
  1693. /* skip the one we keep connection on */
  1694. if (_data == vif)
  1695. return;
  1696. if (vif->type == NL80211_IFTYPE_STATION)
  1697. ieee80211_connection_loss(vif);
  1698. }
  1699. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1700. {
  1701. struct iwl_mvm *mvm = inode->i_private;
  1702. int remaining_time = 10;
  1703. mvm->d3_test_active = false;
  1704. rtnl_lock();
  1705. __iwl_mvm_resume(mvm, true);
  1706. rtnl_unlock();
  1707. iwl_abort_notification_waits(&mvm->notif_wait);
  1708. ieee80211_restart_hw(mvm->hw);
  1709. /* wait for restart and disconnect all interfaces */
  1710. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1711. remaining_time > 0) {
  1712. remaining_time--;
  1713. msleep(1000);
  1714. }
  1715. if (remaining_time == 0)
  1716. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1717. ieee80211_iterate_active_interfaces_atomic(
  1718. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1719. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1720. ieee80211_wake_queues(mvm->hw);
  1721. return 0;
  1722. }
  1723. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1724. .llseek = no_llseek,
  1725. .open = iwl_mvm_d3_test_open,
  1726. .read = iwl_mvm_d3_test_read,
  1727. .release = iwl_mvm_d3_test_release,
  1728. };
  1729. #endif