d3.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/etherdevice.h>
  68. #include <linux/ip.h>
  69. #include <linux/fs.h>
  70. #include <net/cfg80211.h>
  71. #include <net/ipv6.h>
  72. #include <net/tcp.h>
  73. #include <net/addrconf.h>
  74. #include "iwl-modparams.h"
  75. #include "fw-api.h"
  76. #include "mvm.h"
  77. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  78. struct ieee80211_vif *vif,
  79. struct cfg80211_gtk_rekey_data *data)
  80. {
  81. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  82. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  83. if (iwlwifi_mod_params.swcrypto)
  84. return;
  85. mutex_lock(&mvm->mutex);
  86. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  87. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  88. mvmvif->rekey_data.replay_ctr =
  89. cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
  90. mvmvif->rekey_data.valid = true;
  91. mutex_unlock(&mvm->mutex);
  92. }
  93. #if IS_ENABLED(CONFIG_IPV6)
  94. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  95. struct ieee80211_vif *vif,
  96. struct inet6_dev *idev)
  97. {
  98. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  99. struct inet6_ifaddr *ifa;
  100. int idx = 0;
  101. memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
  102. read_lock_bh(&idev->lock);
  103. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  104. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  105. if (ifa->flags & IFA_F_TENTATIVE)
  106. __set_bit(idx, mvmvif->tentative_addrs);
  107. idx++;
  108. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  109. break;
  110. }
  111. read_unlock_bh(&idev->lock);
  112. mvmvif->num_target_ipv6_addrs = idx;
  113. }
  114. #endif
  115. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  116. struct ieee80211_vif *vif, int idx)
  117. {
  118. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  119. mvmvif->tx_key_idx = idx;
  120. }
  121. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  122. {
  123. int i;
  124. for (i = 0; i < IWL_P1K_SIZE; i++)
  125. out[i] = cpu_to_le16(p1k[i]);
  126. }
  127. static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
  128. struct iwl_mvm_key_pn *ptk_pn,
  129. struct ieee80211_key_seq *seq,
  130. int tid, int queues)
  131. {
  132. const u8 *ret = seq->ccmp.pn;
  133. int i;
  134. /* get the PN from mac80211, used on the default queue */
  135. ieee80211_get_key_rx_seq(key, tid, seq);
  136. /* and use the internal data for the other queues */
  137. for (i = 1; i < queues; i++) {
  138. const u8 *tmp = ptk_pn->q[i].pn[tid];
  139. if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
  140. ret = tmp;
  141. }
  142. return ret;
  143. }
  144. struct wowlan_key_data {
  145. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  146. struct iwl_wowlan_tkip_params_cmd *tkip;
  147. bool error, use_rsc_tsc, use_tkip, configure_keys;
  148. int wep_key_idx;
  149. };
  150. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  151. struct ieee80211_vif *vif,
  152. struct ieee80211_sta *sta,
  153. struct ieee80211_key_conf *key,
  154. void *_data)
  155. {
  156. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  157. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  158. struct wowlan_key_data *data = _data;
  159. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  160. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  161. struct iwl_p1k_cache *rx_p1ks;
  162. u8 *rx_mic_key;
  163. struct ieee80211_key_seq seq;
  164. u32 cur_rx_iv32 = 0;
  165. u16 p1k[IWL_P1K_SIZE];
  166. int ret, i;
  167. switch (key->cipher) {
  168. case WLAN_CIPHER_SUITE_WEP40:
  169. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  170. struct {
  171. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  172. struct iwl_mvm_wep_key wep_key;
  173. } __packed wkc = {
  174. .wep_key_cmd.mac_id_n_color =
  175. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  176. mvmvif->color)),
  177. .wep_key_cmd.num_keys = 1,
  178. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  179. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  180. .wep_key.key_index = key->keyidx,
  181. .wep_key.key_size = key->keylen,
  182. };
  183. /*
  184. * This will fail -- the key functions don't set support
  185. * pairwise WEP keys. However, that's better than silently
  186. * failing WoWLAN. Or maybe not?
  187. */
  188. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  189. break;
  190. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  191. if (key->keyidx == mvmvif->tx_key_idx) {
  192. /* TX key must be at offset 0 */
  193. wkc.wep_key.key_offset = 0;
  194. } else {
  195. /* others start at 1 */
  196. data->wep_key_idx++;
  197. wkc.wep_key.key_offset = data->wep_key_idx;
  198. }
  199. if (data->configure_keys) {
  200. mutex_lock(&mvm->mutex);
  201. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
  202. sizeof(wkc), &wkc);
  203. data->error = ret != 0;
  204. mvm->ptk_ivlen = key->iv_len;
  205. mvm->ptk_icvlen = key->icv_len;
  206. mvm->gtk_ivlen = key->iv_len;
  207. mvm->gtk_icvlen = key->icv_len;
  208. mutex_unlock(&mvm->mutex);
  209. }
  210. /* don't upload key again */
  211. return;
  212. }
  213. default:
  214. data->error = true;
  215. return;
  216. case WLAN_CIPHER_SUITE_AES_CMAC:
  217. /*
  218. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  219. * but we also shouldn't abort suspend due to that. It does have
  220. * support for the IGTK key renewal, but doesn't really use the
  221. * IGTK for anything. This means we could spuriously wake up or
  222. * be deauthenticated, but that was considered acceptable.
  223. */
  224. return;
  225. case WLAN_CIPHER_SUITE_TKIP:
  226. if (sta) {
  227. u64 pn64;
  228. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  229. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  230. rx_p1ks = data->tkip->rx_uni;
  231. pn64 = atomic64_read(&key->tx_pn);
  232. tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
  233. tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
  234. ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
  235. p1k);
  236. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  237. memcpy(data->tkip->mic_keys.tx,
  238. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  239. IWL_MIC_KEY_SIZE);
  240. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  241. } else {
  242. tkip_sc =
  243. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  244. rx_p1ks = data->tkip->rx_multi;
  245. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  246. }
  247. /*
  248. * For non-QoS this relies on the fact that both the uCode and
  249. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  250. * for checking the IV in the frames.
  251. */
  252. for (i = 0; i < IWL_NUM_RSC; i++) {
  253. ieee80211_get_key_rx_seq(key, i, &seq);
  254. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  255. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  256. /* wrapping isn't allowed, AP must rekey */
  257. if (seq.tkip.iv32 > cur_rx_iv32)
  258. cur_rx_iv32 = seq.tkip.iv32;
  259. }
  260. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  261. cur_rx_iv32, p1k);
  262. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  263. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  264. cur_rx_iv32 + 1, p1k);
  265. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  266. memcpy(rx_mic_key,
  267. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  268. IWL_MIC_KEY_SIZE);
  269. data->use_tkip = true;
  270. data->use_rsc_tsc = true;
  271. break;
  272. case WLAN_CIPHER_SUITE_CCMP:
  273. if (sta) {
  274. u64 pn64;
  275. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  276. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  277. pn64 = atomic64_read(&key->tx_pn);
  278. aes_tx_sc->pn = cpu_to_le64(pn64);
  279. } else {
  280. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  281. }
  282. /*
  283. * For non-QoS this relies on the fact that both the uCode and
  284. * mac80211/our RX code use TID 0 for checking the PN.
  285. */
  286. if (sta && iwl_mvm_has_new_rx_api(mvm)) {
  287. struct iwl_mvm_sta *mvmsta;
  288. struct iwl_mvm_key_pn *ptk_pn;
  289. const u8 *pn;
  290. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  291. ptk_pn = rcu_dereference_protected(
  292. mvmsta->ptk_pn[key->keyidx],
  293. lockdep_is_held(&mvm->mutex));
  294. if (WARN_ON(!ptk_pn))
  295. break;
  296. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  297. pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
  298. mvm->trans->num_rx_queues);
  299. aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
  300. ((u64)pn[4] << 8) |
  301. ((u64)pn[3] << 16) |
  302. ((u64)pn[2] << 24) |
  303. ((u64)pn[1] << 32) |
  304. ((u64)pn[0] << 40));
  305. }
  306. } else {
  307. for (i = 0; i < IWL_NUM_RSC; i++) {
  308. u8 *pn = seq.ccmp.pn;
  309. ieee80211_get_key_rx_seq(key, i, &seq);
  310. aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
  311. ((u64)pn[4] << 8) |
  312. ((u64)pn[3] << 16) |
  313. ((u64)pn[2] << 24) |
  314. ((u64)pn[1] << 32) |
  315. ((u64)pn[0] << 40));
  316. }
  317. }
  318. data->use_rsc_tsc = true;
  319. break;
  320. }
  321. if (data->configure_keys) {
  322. mutex_lock(&mvm->mutex);
  323. /*
  324. * The D3 firmware hardcodes the key offset 0 as the key it
  325. * uses to transmit packets to the AP, i.e. the PTK.
  326. */
  327. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  328. mvm->ptk_ivlen = key->iv_len;
  329. mvm->ptk_icvlen = key->icv_len;
  330. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
  331. } else {
  332. /*
  333. * firmware only supports TSC/RSC for a single key,
  334. * so if there are multiple keep overwriting them
  335. * with new ones -- this relies on mac80211 doing
  336. * list_add_tail().
  337. */
  338. mvm->gtk_ivlen = key->iv_len;
  339. mvm->gtk_icvlen = key->icv_len;
  340. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
  341. }
  342. mutex_unlock(&mvm->mutex);
  343. data->error = ret != 0;
  344. }
  345. }
  346. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  347. struct cfg80211_wowlan *wowlan)
  348. {
  349. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  350. struct iwl_host_cmd cmd = {
  351. .id = WOWLAN_PATTERNS,
  352. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  353. };
  354. int i, err;
  355. if (!wowlan->n_patterns)
  356. return 0;
  357. cmd.len[0] = sizeof(*pattern_cmd) +
  358. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  359. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  360. if (!pattern_cmd)
  361. return -ENOMEM;
  362. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  363. for (i = 0; i < wowlan->n_patterns; i++) {
  364. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  365. memcpy(&pattern_cmd->patterns[i].mask,
  366. wowlan->patterns[i].mask, mask_len);
  367. memcpy(&pattern_cmd->patterns[i].pattern,
  368. wowlan->patterns[i].pattern,
  369. wowlan->patterns[i].pattern_len);
  370. pattern_cmd->patterns[i].mask_size = mask_len;
  371. pattern_cmd->patterns[i].pattern_size =
  372. wowlan->patterns[i].pattern_len;
  373. }
  374. cmd.data[0] = pattern_cmd;
  375. err = iwl_mvm_send_cmd(mvm, &cmd);
  376. kfree(pattern_cmd);
  377. return err;
  378. }
  379. enum iwl_mvm_tcp_packet_type {
  380. MVM_TCP_TX_SYN,
  381. MVM_TCP_RX_SYNACK,
  382. MVM_TCP_TX_DATA,
  383. MVM_TCP_RX_ACK,
  384. MVM_TCP_RX_WAKE,
  385. MVM_TCP_TX_FIN,
  386. };
  387. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  388. {
  389. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  390. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  391. }
  392. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  393. struct cfg80211_wowlan_tcp *tcp,
  394. void *_pkt, u8 *mask,
  395. __le16 *pseudo_hdr_csum,
  396. enum iwl_mvm_tcp_packet_type ptype)
  397. {
  398. struct {
  399. struct ethhdr eth;
  400. struct iphdr ip;
  401. struct tcphdr tcp;
  402. u8 data[];
  403. } __packed *pkt = _pkt;
  404. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  405. int i;
  406. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  407. pkt->ip.version = 4;
  408. pkt->ip.ihl = 5;
  409. pkt->ip.protocol = IPPROTO_TCP;
  410. switch (ptype) {
  411. case MVM_TCP_TX_SYN:
  412. case MVM_TCP_TX_DATA:
  413. case MVM_TCP_TX_FIN:
  414. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  415. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  416. pkt->ip.ttl = 128;
  417. pkt->ip.saddr = tcp->src;
  418. pkt->ip.daddr = tcp->dst;
  419. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  420. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  421. /* overwritten for TX SYN later */
  422. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  423. pkt->tcp.window = cpu_to_be16(65000);
  424. break;
  425. case MVM_TCP_RX_SYNACK:
  426. case MVM_TCP_RX_ACK:
  427. case MVM_TCP_RX_WAKE:
  428. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  429. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  430. pkt->ip.saddr = tcp->dst;
  431. pkt->ip.daddr = tcp->src;
  432. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  433. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  434. break;
  435. default:
  436. WARN_ON(1);
  437. return;
  438. }
  439. switch (ptype) {
  440. case MVM_TCP_TX_SYN:
  441. /* firmware assumes 8 option bytes - 8 NOPs for now */
  442. memset(pkt->data, 0x01, 8);
  443. ip_tot_len += 8;
  444. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  445. pkt->tcp.syn = 1;
  446. break;
  447. case MVM_TCP_TX_DATA:
  448. ip_tot_len += tcp->payload_len;
  449. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  450. pkt->tcp.psh = 1;
  451. pkt->tcp.ack = 1;
  452. break;
  453. case MVM_TCP_TX_FIN:
  454. pkt->tcp.fin = 1;
  455. pkt->tcp.ack = 1;
  456. break;
  457. case MVM_TCP_RX_SYNACK:
  458. pkt->tcp.syn = 1;
  459. pkt->tcp.ack = 1;
  460. break;
  461. case MVM_TCP_RX_ACK:
  462. pkt->tcp.ack = 1;
  463. break;
  464. case MVM_TCP_RX_WAKE:
  465. ip_tot_len += tcp->wake_len;
  466. pkt->tcp.psh = 1;
  467. pkt->tcp.ack = 1;
  468. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  469. break;
  470. }
  471. switch (ptype) {
  472. case MVM_TCP_TX_SYN:
  473. case MVM_TCP_TX_DATA:
  474. case MVM_TCP_TX_FIN:
  475. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  476. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  477. break;
  478. case MVM_TCP_RX_WAKE:
  479. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  480. u8 tmp = tcp->wake_mask[i];
  481. mask[i + 6] |= tmp << 6;
  482. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  483. mask[i + 7] = tmp >> 2;
  484. }
  485. /* fall through for ethernet/IP/TCP headers mask */
  486. case MVM_TCP_RX_SYNACK:
  487. case MVM_TCP_RX_ACK:
  488. mask[0] = 0xff; /* match ethernet */
  489. /*
  490. * match ethernet, ip.version, ip.ihl
  491. * the ip.ihl half byte is really masked out by firmware
  492. */
  493. mask[1] = 0x7f;
  494. mask[2] = 0x80; /* match ip.protocol */
  495. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  496. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  497. mask[5] = 0x80; /* match tcp flags */
  498. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  499. break;
  500. };
  501. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  502. pkt->ip.saddr, pkt->ip.daddr);
  503. }
  504. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  505. struct ieee80211_vif *vif,
  506. struct cfg80211_wowlan_tcp *tcp)
  507. {
  508. struct iwl_wowlan_remote_wake_config *cfg;
  509. struct iwl_host_cmd cmd = {
  510. .id = REMOTE_WAKE_CONFIG_CMD,
  511. .len = { sizeof(*cfg), },
  512. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  513. };
  514. int ret;
  515. if (!tcp)
  516. return 0;
  517. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  518. if (!cfg)
  519. return -ENOMEM;
  520. cmd.data[0] = cfg;
  521. cfg->max_syn_retries = 10;
  522. cfg->max_data_retries = 10;
  523. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  524. cfg->tcp_ack_timeout = 1; /* seconds */
  525. /* SYN (TX) */
  526. iwl_mvm_build_tcp_packet(
  527. vif, tcp, cfg->syn_tx.data, NULL,
  528. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  529. MVM_TCP_TX_SYN);
  530. cfg->syn_tx.info.tcp_payload_length = 0;
  531. /* SYN/ACK (RX) */
  532. iwl_mvm_build_tcp_packet(
  533. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  534. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  535. MVM_TCP_RX_SYNACK);
  536. cfg->synack_rx.info.tcp_payload_length = 0;
  537. /* KEEPALIVE/ACK (TX) */
  538. iwl_mvm_build_tcp_packet(
  539. vif, tcp, cfg->keepalive_tx.data, NULL,
  540. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  541. MVM_TCP_TX_DATA);
  542. cfg->keepalive_tx.info.tcp_payload_length =
  543. cpu_to_le16(tcp->payload_len);
  544. cfg->sequence_number_offset = tcp->payload_seq.offset;
  545. /* length must be 0..4, the field is little endian */
  546. cfg->sequence_number_length = tcp->payload_seq.len;
  547. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  548. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  549. if (tcp->payload_tok.len) {
  550. cfg->token_offset = tcp->payload_tok.offset;
  551. cfg->token_length = tcp->payload_tok.len;
  552. cfg->num_tokens =
  553. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  554. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  555. tcp->tokens_size);
  556. } else {
  557. /* set tokens to max value to almost never run out */
  558. cfg->num_tokens = cpu_to_le16(65535);
  559. }
  560. /* ACK (RX) */
  561. iwl_mvm_build_tcp_packet(
  562. vif, tcp, cfg->keepalive_ack_rx.data,
  563. cfg->keepalive_ack_rx.rx_mask,
  564. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  565. MVM_TCP_RX_ACK);
  566. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  567. /* WAKEUP (RX) */
  568. iwl_mvm_build_tcp_packet(
  569. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  570. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  571. MVM_TCP_RX_WAKE);
  572. cfg->wake_rx.info.tcp_payload_length =
  573. cpu_to_le16(tcp->wake_len);
  574. /* FIN */
  575. iwl_mvm_build_tcp_packet(
  576. vif, tcp, cfg->fin_tx.data, NULL,
  577. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  578. MVM_TCP_TX_FIN);
  579. cfg->fin_tx.info.tcp_payload_length = 0;
  580. ret = iwl_mvm_send_cmd(mvm, &cmd);
  581. kfree(cfg);
  582. return ret;
  583. }
  584. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  585. struct ieee80211_sta *ap_sta)
  586. {
  587. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  588. struct ieee80211_chanctx_conf *ctx;
  589. u8 chains_static, chains_dynamic;
  590. struct cfg80211_chan_def chandef;
  591. int ret, i;
  592. struct iwl_binding_cmd binding_cmd = {};
  593. struct iwl_time_quota_cmd quota_cmd = {};
  594. u32 status;
  595. int size;
  596. if (fw_has_capa(&mvm->fw->ucode_capa,
  597. IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
  598. size = sizeof(binding_cmd);
  599. if (mvmvif->phy_ctxt->channel->band == NL80211_BAND_2GHZ ||
  600. !iwl_mvm_is_cdb_supported(mvm))
  601. binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX);
  602. else
  603. binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_5G_INDEX);
  604. } else {
  605. size = IWL_BINDING_CMD_SIZE_V1;
  606. }
  607. /* add back the PHY */
  608. if (WARN_ON(!mvmvif->phy_ctxt))
  609. return -EINVAL;
  610. rcu_read_lock();
  611. ctx = rcu_dereference(vif->chanctx_conf);
  612. if (WARN_ON(!ctx)) {
  613. rcu_read_unlock();
  614. return -EINVAL;
  615. }
  616. chandef = ctx->def;
  617. chains_static = ctx->rx_chains_static;
  618. chains_dynamic = ctx->rx_chains_dynamic;
  619. rcu_read_unlock();
  620. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  621. chains_static, chains_dynamic);
  622. if (ret)
  623. return ret;
  624. /* add back the MAC */
  625. mvmvif->uploaded = false;
  626. if (WARN_ON(!vif->bss_conf.assoc))
  627. return -EINVAL;
  628. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  629. if (ret)
  630. return ret;
  631. /* add back binding - XXX refactor? */
  632. binding_cmd.id_and_color =
  633. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  634. mvmvif->phy_ctxt->color));
  635. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  636. binding_cmd.phy =
  637. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  638. mvmvif->phy_ctxt->color));
  639. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  640. mvmvif->color));
  641. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  642. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  643. status = 0;
  644. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  645. size, &binding_cmd, &status);
  646. if (ret) {
  647. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  648. return ret;
  649. }
  650. if (status) {
  651. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  652. return -EIO;
  653. }
  654. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
  655. if (ret)
  656. return ret;
  657. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  658. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  659. if (ret)
  660. return ret;
  661. /* and some quota */
  662. quota_cmd.quotas[0].id_and_color =
  663. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  664. mvmvif->phy_ctxt->color));
  665. quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  666. quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  667. for (i = 1; i < MAX_BINDINGS; i++)
  668. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  669. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  670. sizeof(quota_cmd), &quota_cmd);
  671. if (ret)
  672. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  673. if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
  674. IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
  675. return 0;
  676. }
  677. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  678. struct ieee80211_vif *vif)
  679. {
  680. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  681. struct iwl_nonqos_seq_query_cmd query_cmd = {
  682. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  683. .mac_id_n_color =
  684. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  685. mvmvif->color)),
  686. };
  687. struct iwl_host_cmd cmd = {
  688. .id = NON_QOS_TX_COUNTER_CMD,
  689. .flags = CMD_WANT_SKB,
  690. };
  691. int err;
  692. u32 size;
  693. cmd.data[0] = &query_cmd;
  694. cmd.len[0] = sizeof(query_cmd);
  695. err = iwl_mvm_send_cmd(mvm, &cmd);
  696. if (err)
  697. return err;
  698. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  699. if (size < sizeof(__le16)) {
  700. err = -EINVAL;
  701. } else {
  702. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  703. /* firmware returns next, not last-used seqno */
  704. err = (u16) (err - 0x10);
  705. }
  706. iwl_free_resp(&cmd);
  707. return err;
  708. }
  709. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  710. {
  711. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  712. struct iwl_nonqos_seq_query_cmd query_cmd = {
  713. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  714. .mac_id_n_color =
  715. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  716. mvmvif->color)),
  717. .value = cpu_to_le16(mvmvif->seqno),
  718. };
  719. /* return if called during restart, not resume from D3 */
  720. if (!mvmvif->seqno_valid)
  721. return;
  722. mvmvif->seqno_valid = false;
  723. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
  724. sizeof(query_cmd), &query_cmd))
  725. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  726. }
  727. static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
  728. {
  729. iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
  730. iwl_mvm_stop_device(mvm);
  731. /*
  732. * Set the HW restart bit -- this is mostly true as we're
  733. * going to load new firmware and reprogram that, though
  734. * the reprogramming is going to be manual to avoid adding
  735. * all the MACs that aren't support.
  736. * We don't have to clear up everything though because the
  737. * reprogramming is manual. When we resume, we'll actually
  738. * go through a proper restart sequence again to switch
  739. * back to the runtime firmware image.
  740. */
  741. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  742. /* the fw is reset, so all the keys are cleared */
  743. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  744. mvm->ptk_ivlen = 0;
  745. mvm->ptk_icvlen = 0;
  746. mvm->ptk_ivlen = 0;
  747. mvm->ptk_icvlen = 0;
  748. return iwl_mvm_load_d3_fw(mvm);
  749. }
  750. static int
  751. iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
  752. struct cfg80211_wowlan *wowlan,
  753. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  754. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  755. struct ieee80211_sta *ap_sta)
  756. {
  757. int ret;
  758. struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  759. /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
  760. wowlan_config_cmd->is_11n_connection =
  761. ap_sta->ht_cap.ht_supported;
  762. wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
  763. ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
  764. /* Query the last used seqno and set it */
  765. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  766. if (ret < 0)
  767. return ret;
  768. wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
  769. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
  770. if (wowlan->disconnect)
  771. wowlan_config_cmd->wakeup_filter |=
  772. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  773. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  774. if (wowlan->magic_pkt)
  775. wowlan_config_cmd->wakeup_filter |=
  776. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  777. if (wowlan->gtk_rekey_failure)
  778. wowlan_config_cmd->wakeup_filter |=
  779. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  780. if (wowlan->eap_identity_req)
  781. wowlan_config_cmd->wakeup_filter |=
  782. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  783. if (wowlan->four_way_handshake)
  784. wowlan_config_cmd->wakeup_filter |=
  785. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  786. if (wowlan->n_patterns)
  787. wowlan_config_cmd->wakeup_filter |=
  788. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  789. if (wowlan->rfkill_release)
  790. wowlan_config_cmd->wakeup_filter |=
  791. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  792. if (wowlan->tcp) {
  793. /*
  794. * Set the "link change" (really "link lost") flag as well
  795. * since that implies losing the TCP connection.
  796. */
  797. wowlan_config_cmd->wakeup_filter |=
  798. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  799. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  800. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  801. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  802. }
  803. return 0;
  804. }
  805. static void
  806. iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
  807. struct ieee80211_vif *vif,
  808. void (*iter)(struct ieee80211_hw *hw,
  809. struct ieee80211_vif *vif,
  810. struct ieee80211_sta *sta,
  811. struct ieee80211_key_conf *key,
  812. void *data),
  813. void *data)
  814. {
  815. struct ieee80211_sta *ap_sta;
  816. rcu_read_lock();
  817. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
  818. if (IS_ERR_OR_NULL(ap_sta))
  819. goto out;
  820. ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
  821. out:
  822. rcu_read_unlock();
  823. }
  824. int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
  825. struct ieee80211_vif *vif,
  826. bool d0i3,
  827. u32 cmd_flags)
  828. {
  829. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  830. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  831. struct wowlan_key_data key_data = {
  832. .configure_keys = !d0i3,
  833. .use_rsc_tsc = false,
  834. .tkip = &tkip_cmd,
  835. .use_tkip = false,
  836. };
  837. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  838. int ret;
  839. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  840. if (!key_data.rsc_tsc)
  841. return -ENOMEM;
  842. /*
  843. * if we have to configure keys, call ieee80211_iter_keys(),
  844. * as we need non-atomic context in order to take the
  845. * required locks.
  846. * for the d0i3 we can't use ieee80211_iter_keys(), as
  847. * taking (almost) any mutex might result in deadlock.
  848. */
  849. if (!d0i3) {
  850. /*
  851. * Note that currently we don't propagate cmd_flags
  852. * to the iterator. In case of key_data.configure_keys,
  853. * all the configured commands are SYNC, and
  854. * iwl_mvm_wowlan_program_keys() will take care of
  855. * locking/unlocking mvm->mutex.
  856. */
  857. ieee80211_iter_keys(mvm->hw, vif,
  858. iwl_mvm_wowlan_program_keys,
  859. &key_data);
  860. } else {
  861. iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
  862. iwl_mvm_wowlan_program_keys,
  863. &key_data);
  864. }
  865. if (key_data.error) {
  866. ret = -EIO;
  867. goto out;
  868. }
  869. if (key_data.use_rsc_tsc) {
  870. ret = iwl_mvm_send_cmd_pdu(mvm,
  871. WOWLAN_TSC_RSC_PARAM, cmd_flags,
  872. sizeof(*key_data.rsc_tsc),
  873. key_data.rsc_tsc);
  874. if (ret)
  875. goto out;
  876. }
  877. if (key_data.use_tkip &&
  878. !fw_has_api(&mvm->fw->ucode_capa,
  879. IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
  880. ret = iwl_mvm_send_cmd_pdu(mvm,
  881. WOWLAN_TKIP_PARAM,
  882. cmd_flags, sizeof(tkip_cmd),
  883. &tkip_cmd);
  884. if (ret)
  885. goto out;
  886. }
  887. /* configure rekey data only if offloaded rekey is supported (d3) */
  888. if (mvmvif->rekey_data.valid && !d0i3) {
  889. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  890. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  891. NL80211_KCK_LEN);
  892. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  893. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  894. NL80211_KEK_LEN);
  895. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  896. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  897. ret = iwl_mvm_send_cmd_pdu(mvm,
  898. WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
  899. sizeof(kek_kck_cmd),
  900. &kek_kck_cmd);
  901. if (ret)
  902. goto out;
  903. }
  904. ret = 0;
  905. out:
  906. kfree(key_data.rsc_tsc);
  907. return ret;
  908. }
  909. static int
  910. iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
  911. struct cfg80211_wowlan *wowlan,
  912. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  913. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  914. struct ieee80211_sta *ap_sta)
  915. {
  916. int ret;
  917. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  918. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  919. if (!unified_image) {
  920. ret = iwl_mvm_switch_to_d3(mvm);
  921. if (ret)
  922. return ret;
  923. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  924. if (ret)
  925. return ret;
  926. }
  927. if (!iwlwifi_mod_params.swcrypto) {
  928. /*
  929. * This needs to be unlocked due to lock ordering
  930. * constraints. Since we're in the suspend path
  931. * that isn't really a problem though.
  932. */
  933. mutex_unlock(&mvm->mutex);
  934. ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
  935. CMD_ASYNC);
  936. mutex_lock(&mvm->mutex);
  937. if (ret)
  938. return ret;
  939. }
  940. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  941. sizeof(*wowlan_config_cmd),
  942. wowlan_config_cmd);
  943. if (ret)
  944. return ret;
  945. ret = iwl_mvm_send_patterns(mvm, wowlan);
  946. if (ret)
  947. return ret;
  948. ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
  949. if (ret)
  950. return ret;
  951. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  952. return ret;
  953. }
  954. static int
  955. iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
  956. struct cfg80211_wowlan *wowlan,
  957. struct cfg80211_sched_scan_request *nd_config,
  958. struct ieee80211_vif *vif)
  959. {
  960. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  961. int ret;
  962. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  963. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  964. if (!unified_image) {
  965. ret = iwl_mvm_switch_to_d3(mvm);
  966. if (ret)
  967. return ret;
  968. } else {
  969. /* In theory, we wouldn't have to stop a running sched
  970. * scan in order to start another one (for
  971. * net-detect). But in practice this doesn't seem to
  972. * work properly, so stop any running sched_scan now.
  973. */
  974. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
  975. if (ret)
  976. return ret;
  977. }
  978. /* rfkill release can be either for wowlan or netdetect */
  979. if (wowlan->rfkill_release)
  980. wowlan_config_cmd.wakeup_filter |=
  981. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  982. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  983. sizeof(wowlan_config_cmd),
  984. &wowlan_config_cmd);
  985. if (ret)
  986. return ret;
  987. ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
  988. IWL_MVM_SCAN_NETDETECT);
  989. if (ret)
  990. return ret;
  991. if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
  992. return -EBUSY;
  993. /* save the sched scan matchsets... */
  994. if (nd_config->n_match_sets) {
  995. mvm->nd_match_sets = kmemdup(nd_config->match_sets,
  996. sizeof(*nd_config->match_sets) *
  997. nd_config->n_match_sets,
  998. GFP_KERNEL);
  999. if (mvm->nd_match_sets)
  1000. mvm->n_nd_match_sets = nd_config->n_match_sets;
  1001. }
  1002. /* ...and the sched scan channels for later reporting */
  1003. mvm->nd_channels = kmemdup(nd_config->channels,
  1004. sizeof(*nd_config->channels) *
  1005. nd_config->n_channels,
  1006. GFP_KERNEL);
  1007. if (mvm->nd_channels)
  1008. mvm->n_nd_channels = nd_config->n_channels;
  1009. return 0;
  1010. }
  1011. static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
  1012. {
  1013. kfree(mvm->nd_match_sets);
  1014. mvm->nd_match_sets = NULL;
  1015. mvm->n_nd_match_sets = 0;
  1016. kfree(mvm->nd_channels);
  1017. mvm->nd_channels = NULL;
  1018. mvm->n_nd_channels = 0;
  1019. }
  1020. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  1021. struct cfg80211_wowlan *wowlan,
  1022. bool test)
  1023. {
  1024. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1025. struct ieee80211_vif *vif = NULL;
  1026. struct iwl_mvm_vif *mvmvif = NULL;
  1027. struct ieee80211_sta *ap_sta = NULL;
  1028. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  1029. /*
  1030. * Program the minimum sleep time to 10 seconds, as many
  1031. * platforms have issues processing a wakeup signal while
  1032. * still being in the process of suspending.
  1033. */
  1034. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  1035. };
  1036. struct iwl_host_cmd d3_cfg_cmd = {
  1037. .id = D3_CONFIG_CMD,
  1038. .flags = CMD_WANT_SKB,
  1039. .data[0] = &d3_cfg_cmd_data,
  1040. .len[0] = sizeof(d3_cfg_cmd_data),
  1041. };
  1042. int ret;
  1043. int len __maybe_unused;
  1044. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1045. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1046. if (!wowlan) {
  1047. /*
  1048. * mac80211 shouldn't get here, but for D3 test
  1049. * it doesn't warrant a warning
  1050. */
  1051. WARN_ON(!test);
  1052. return -EINVAL;
  1053. }
  1054. mutex_lock(&mvm->mutex);
  1055. vif = iwl_mvm_get_bss_vif(mvm);
  1056. if (IS_ERR_OR_NULL(vif)) {
  1057. ret = 1;
  1058. goto out_noreset;
  1059. }
  1060. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1061. if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
  1062. /* if we're not associated, this must be netdetect */
  1063. if (!wowlan->nd_config) {
  1064. ret = 1;
  1065. goto out_noreset;
  1066. }
  1067. ret = iwl_mvm_netdetect_config(
  1068. mvm, wowlan, wowlan->nd_config, vif);
  1069. if (ret)
  1070. goto out;
  1071. mvm->net_detect = true;
  1072. } else {
  1073. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  1074. ap_sta = rcu_dereference_protected(
  1075. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  1076. lockdep_is_held(&mvm->mutex));
  1077. if (IS_ERR_OR_NULL(ap_sta)) {
  1078. ret = -EINVAL;
  1079. goto out_noreset;
  1080. }
  1081. ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1082. vif, mvmvif, ap_sta);
  1083. if (ret)
  1084. goto out_noreset;
  1085. ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1086. vif, mvmvif, ap_sta);
  1087. if (ret)
  1088. goto out;
  1089. mvm->net_detect = false;
  1090. }
  1091. ret = iwl_mvm_power_update_device(mvm);
  1092. if (ret)
  1093. goto out;
  1094. ret = iwl_mvm_power_update_mac(mvm);
  1095. if (ret)
  1096. goto out;
  1097. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1098. if (mvm->d3_wake_sysassert)
  1099. d3_cfg_cmd_data.wakeup_flags |=
  1100. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  1101. #endif
  1102. /* must be last -- this switches firmware state */
  1103. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  1104. if (ret)
  1105. goto out;
  1106. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1107. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  1108. if (len >= sizeof(u32)) {
  1109. mvm->d3_test_pme_ptr =
  1110. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  1111. }
  1112. #endif
  1113. iwl_free_resp(&d3_cfg_cmd);
  1114. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1115. iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
  1116. out:
  1117. if (ret < 0) {
  1118. iwl_mvm_free_nd(mvm);
  1119. if (!unified_image) {
  1120. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1121. if (mvm->fw_restart > 0) {
  1122. mvm->fw_restart--;
  1123. ieee80211_restart_hw(mvm->hw);
  1124. }
  1125. }
  1126. }
  1127. out_noreset:
  1128. mutex_unlock(&mvm->mutex);
  1129. return ret;
  1130. }
  1131. static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
  1132. {
  1133. struct iwl_notification_wait wait_d3;
  1134. static const u16 d3_notif[] = { D3_CONFIG_CMD };
  1135. int ret;
  1136. iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
  1137. d3_notif, ARRAY_SIZE(d3_notif),
  1138. NULL, NULL);
  1139. ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
  1140. if (ret)
  1141. goto remove_notif;
  1142. ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
  1143. WARN_ON_ONCE(ret);
  1144. return ret;
  1145. remove_notif:
  1146. iwl_remove_notification(&mvm->notif_wait, &wait_d3);
  1147. return ret;
  1148. }
  1149. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1150. {
  1151. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1152. struct iwl_trans *trans = mvm->trans;
  1153. int ret;
  1154. /* make sure the d0i3 exit work is not pending */
  1155. flush_work(&mvm->d0i3_exit_work);
  1156. ret = iwl_trans_suspend(trans);
  1157. if (ret)
  1158. return ret;
  1159. if (wowlan->any) {
  1160. trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  1161. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1162. ret = iwl_mvm_enter_d0i3_sync(mvm);
  1163. if (ret)
  1164. return ret;
  1165. }
  1166. mutex_lock(&mvm->d0i3_suspend_mutex);
  1167. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1168. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1169. iwl_trans_d3_suspend(trans, false, false);
  1170. return 0;
  1171. }
  1172. trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1173. return __iwl_mvm_suspend(hw, wowlan, false);
  1174. }
  1175. /* converted data from the different status responses */
  1176. struct iwl_wowlan_status_data {
  1177. u16 pattern_number;
  1178. u16 qos_seq_ctr[8];
  1179. u32 wakeup_reasons;
  1180. u32 wake_packet_length;
  1181. u32 wake_packet_bufsize;
  1182. const u8 *wake_packet;
  1183. };
  1184. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1185. struct ieee80211_vif *vif,
  1186. struct iwl_wowlan_status_data *status)
  1187. {
  1188. struct sk_buff *pkt = NULL;
  1189. struct cfg80211_wowlan_wakeup wakeup = {
  1190. .pattern_idx = -1,
  1191. };
  1192. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1193. u32 reasons = status->wakeup_reasons;
  1194. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1195. wakeup_report = NULL;
  1196. goto report;
  1197. }
  1198. pm_wakeup_event(mvm->dev, 0);
  1199. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1200. wakeup.magic_pkt = true;
  1201. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1202. wakeup.pattern_idx =
  1203. status->pattern_number;
  1204. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1205. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1206. wakeup.disconnect = true;
  1207. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1208. wakeup.gtk_rekey_failure = true;
  1209. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1210. wakeup.rfkill_release = true;
  1211. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1212. wakeup.eap_identity_req = true;
  1213. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1214. wakeup.four_way_handshake = true;
  1215. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1216. wakeup.tcp_connlost = true;
  1217. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1218. wakeup.tcp_nomoretokens = true;
  1219. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1220. wakeup.tcp_match = true;
  1221. if (status->wake_packet_bufsize) {
  1222. int pktsize = status->wake_packet_bufsize;
  1223. int pktlen = status->wake_packet_length;
  1224. const u8 *pktdata = status->wake_packet;
  1225. struct ieee80211_hdr *hdr = (void *)pktdata;
  1226. int truncated = pktlen - pktsize;
  1227. /* this would be a firmware bug */
  1228. if (WARN_ON_ONCE(truncated < 0))
  1229. truncated = 0;
  1230. if (ieee80211_is_data(hdr->frame_control)) {
  1231. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1232. int ivlen = 0, icvlen = 4; /* also FCS */
  1233. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1234. if (!pkt)
  1235. goto report;
  1236. skb_put_data(pkt, pktdata, hdrlen);
  1237. pktdata += hdrlen;
  1238. pktsize -= hdrlen;
  1239. if (ieee80211_has_protected(hdr->frame_control)) {
  1240. /*
  1241. * This is unlocked and using gtk_i(c)vlen,
  1242. * but since everything is under RTNL still
  1243. * that's not really a problem - changing
  1244. * it would be difficult.
  1245. */
  1246. if (is_multicast_ether_addr(hdr->addr1)) {
  1247. ivlen = mvm->gtk_ivlen;
  1248. icvlen += mvm->gtk_icvlen;
  1249. } else {
  1250. ivlen = mvm->ptk_ivlen;
  1251. icvlen += mvm->ptk_icvlen;
  1252. }
  1253. }
  1254. /* if truncated, FCS/ICV is (partially) gone */
  1255. if (truncated >= icvlen) {
  1256. icvlen = 0;
  1257. truncated -= icvlen;
  1258. } else {
  1259. icvlen -= truncated;
  1260. truncated = 0;
  1261. }
  1262. pktsize -= ivlen + icvlen;
  1263. pktdata += ivlen;
  1264. skb_put_data(pkt, pktdata, pktsize);
  1265. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1266. goto report;
  1267. wakeup.packet = pkt->data;
  1268. wakeup.packet_present_len = pkt->len;
  1269. wakeup.packet_len = pkt->len - truncated;
  1270. wakeup.packet_80211 = false;
  1271. } else {
  1272. int fcslen = 4;
  1273. if (truncated >= 4) {
  1274. truncated -= 4;
  1275. fcslen = 0;
  1276. } else {
  1277. fcslen -= truncated;
  1278. truncated = 0;
  1279. }
  1280. pktsize -= fcslen;
  1281. wakeup.packet = status->wake_packet;
  1282. wakeup.packet_present_len = pktsize;
  1283. wakeup.packet_len = pktlen - truncated;
  1284. wakeup.packet_80211 = true;
  1285. }
  1286. }
  1287. report:
  1288. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1289. kfree_skb(pkt);
  1290. }
  1291. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1292. struct ieee80211_key_seq *seq)
  1293. {
  1294. u64 pn;
  1295. pn = le64_to_cpu(sc->pn);
  1296. seq->ccmp.pn[0] = pn >> 40;
  1297. seq->ccmp.pn[1] = pn >> 32;
  1298. seq->ccmp.pn[2] = pn >> 24;
  1299. seq->ccmp.pn[3] = pn >> 16;
  1300. seq->ccmp.pn[4] = pn >> 8;
  1301. seq->ccmp.pn[5] = pn;
  1302. }
  1303. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1304. struct ieee80211_key_seq *seq)
  1305. {
  1306. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1307. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1308. }
  1309. static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
  1310. struct ieee80211_sta *sta,
  1311. struct ieee80211_key_conf *key)
  1312. {
  1313. int tid;
  1314. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1315. if (sta && iwl_mvm_has_new_rx_api(mvm)) {
  1316. struct iwl_mvm_sta *mvmsta;
  1317. struct iwl_mvm_key_pn *ptk_pn;
  1318. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1319. ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
  1320. lockdep_is_held(&mvm->mutex));
  1321. if (WARN_ON(!ptk_pn))
  1322. return;
  1323. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  1324. struct ieee80211_key_seq seq = {};
  1325. int i;
  1326. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1327. ieee80211_set_key_rx_seq(key, tid, &seq);
  1328. for (i = 1; i < mvm->trans->num_rx_queues; i++)
  1329. memcpy(ptk_pn->q[i].pn[tid],
  1330. seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1331. }
  1332. } else {
  1333. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1334. struct ieee80211_key_seq seq = {};
  1335. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1336. ieee80211_set_key_rx_seq(key, tid, &seq);
  1337. }
  1338. }
  1339. }
  1340. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1341. struct ieee80211_key_conf *key)
  1342. {
  1343. int tid;
  1344. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1345. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1346. struct ieee80211_key_seq seq = {};
  1347. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1348. ieee80211_set_key_rx_seq(key, tid, &seq);
  1349. }
  1350. }
  1351. static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
  1352. struct ieee80211_key_conf *key,
  1353. struct iwl_wowlan_status *status)
  1354. {
  1355. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1356. switch (key->cipher) {
  1357. case WLAN_CIPHER_SUITE_CCMP:
  1358. iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
  1359. break;
  1360. case WLAN_CIPHER_SUITE_TKIP:
  1361. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1362. break;
  1363. default:
  1364. WARN_ON(1);
  1365. }
  1366. }
  1367. struct iwl_mvm_d3_gtk_iter_data {
  1368. struct iwl_mvm *mvm;
  1369. struct iwl_wowlan_status *status;
  1370. void *last_gtk;
  1371. u32 cipher;
  1372. bool find_phase, unhandled_cipher;
  1373. int num_keys;
  1374. };
  1375. static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
  1376. struct ieee80211_vif *vif,
  1377. struct ieee80211_sta *sta,
  1378. struct ieee80211_key_conf *key,
  1379. void *_data)
  1380. {
  1381. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1382. if (data->unhandled_cipher)
  1383. return;
  1384. switch (key->cipher) {
  1385. case WLAN_CIPHER_SUITE_WEP40:
  1386. case WLAN_CIPHER_SUITE_WEP104:
  1387. /* ignore WEP completely, nothing to do */
  1388. return;
  1389. case WLAN_CIPHER_SUITE_CCMP:
  1390. case WLAN_CIPHER_SUITE_TKIP:
  1391. /* we support these */
  1392. break;
  1393. default:
  1394. /* everything else (even CMAC for MFP) - disconnect from AP */
  1395. data->unhandled_cipher = true;
  1396. return;
  1397. }
  1398. data->num_keys++;
  1399. /*
  1400. * pairwise key - update sequence counters only;
  1401. * note that this assumes no TDLS sessions are active
  1402. */
  1403. if (sta) {
  1404. struct ieee80211_key_seq seq = {};
  1405. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1406. if (data->find_phase)
  1407. return;
  1408. switch (key->cipher) {
  1409. case WLAN_CIPHER_SUITE_CCMP:
  1410. iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
  1411. sta, key);
  1412. atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
  1413. break;
  1414. case WLAN_CIPHER_SUITE_TKIP:
  1415. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1416. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1417. atomic64_set(&key->tx_pn,
  1418. (u64)seq.tkip.iv16 |
  1419. ((u64)seq.tkip.iv32 << 16));
  1420. break;
  1421. }
  1422. /* that's it for this key */
  1423. return;
  1424. }
  1425. if (data->find_phase) {
  1426. data->last_gtk = key;
  1427. data->cipher = key->cipher;
  1428. return;
  1429. }
  1430. if (data->status->num_of_gtk_rekeys)
  1431. ieee80211_remove_key(key);
  1432. else if (data->last_gtk == key)
  1433. iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
  1434. }
  1435. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1436. struct ieee80211_vif *vif,
  1437. struct iwl_wowlan_status *status)
  1438. {
  1439. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1440. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1441. .mvm = mvm,
  1442. .status = status,
  1443. };
  1444. u32 disconnection_reasons =
  1445. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1446. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1447. if (!status || !vif->bss_conf.bssid)
  1448. return false;
  1449. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1450. return false;
  1451. /* find last GTK that we used initially, if any */
  1452. gtkdata.find_phase = true;
  1453. ieee80211_iter_keys(mvm->hw, vif,
  1454. iwl_mvm_d3_update_keys, &gtkdata);
  1455. /* not trying to keep connections with MFP/unhandled ciphers */
  1456. if (gtkdata.unhandled_cipher)
  1457. return false;
  1458. if (!gtkdata.num_keys)
  1459. goto out;
  1460. if (!gtkdata.last_gtk)
  1461. return false;
  1462. /*
  1463. * invalidate all other GTKs that might still exist and update
  1464. * the one that we used
  1465. */
  1466. gtkdata.find_phase = false;
  1467. ieee80211_iter_keys(mvm->hw, vif,
  1468. iwl_mvm_d3_update_keys, &gtkdata);
  1469. if (status->num_of_gtk_rekeys) {
  1470. struct ieee80211_key_conf *key;
  1471. struct {
  1472. struct ieee80211_key_conf conf;
  1473. u8 key[32];
  1474. } conf = {
  1475. .conf.cipher = gtkdata.cipher,
  1476. .conf.keyidx = status->gtk.key_index,
  1477. };
  1478. switch (gtkdata.cipher) {
  1479. case WLAN_CIPHER_SUITE_CCMP:
  1480. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1481. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1482. WLAN_KEY_LEN_CCMP);
  1483. break;
  1484. case WLAN_CIPHER_SUITE_TKIP:
  1485. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1486. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1487. /* leave TX MIC key zeroed, we don't use it anyway */
  1488. memcpy(conf.conf.key +
  1489. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1490. status->gtk.tkip_mic_key, 8);
  1491. break;
  1492. }
  1493. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1494. if (IS_ERR(key))
  1495. return false;
  1496. iwl_mvm_set_key_rx_seq(mvm, key, status);
  1497. }
  1498. if (status->num_of_gtk_rekeys) {
  1499. __be64 replay_ctr =
  1500. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1501. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1502. (void *)&replay_ctr, GFP_KERNEL);
  1503. }
  1504. out:
  1505. mvmvif->seqno_valid = true;
  1506. /* +0x10 because the set API expects next-to-use, not last-used */
  1507. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1508. return true;
  1509. }
  1510. static struct iwl_wowlan_status *
  1511. iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1512. {
  1513. u32 base = mvm->error_event_table[0];
  1514. struct error_table_start {
  1515. /* cf. struct iwl_error_event_table */
  1516. u32 valid;
  1517. u32 error_id;
  1518. } err_info;
  1519. struct iwl_host_cmd cmd = {
  1520. .id = WOWLAN_GET_STATUSES,
  1521. .flags = CMD_WANT_SKB,
  1522. };
  1523. struct iwl_wowlan_status *status, *fw_status;
  1524. int ret, len, status_size;
  1525. iwl_trans_read_mem_bytes(mvm->trans, base,
  1526. &err_info, sizeof(err_info));
  1527. if (err_info.valid) {
  1528. IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
  1529. err_info.valid, err_info.error_id);
  1530. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1531. struct cfg80211_wowlan_wakeup wakeup = {
  1532. .rfkill_release = true,
  1533. };
  1534. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1535. GFP_KERNEL);
  1536. }
  1537. return ERR_PTR(-EIO);
  1538. }
  1539. /* only for tracing for now */
  1540. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1541. if (ret)
  1542. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1543. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1544. if (ret) {
  1545. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1546. return ERR_PTR(ret);
  1547. }
  1548. status_size = sizeof(*fw_status);
  1549. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1550. if (len < status_size) {
  1551. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1552. fw_status = ERR_PTR(-EIO);
  1553. goto out_free_resp;
  1554. }
  1555. status = (void *)cmd.resp_pkt->data;
  1556. if (len != (status_size +
  1557. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
  1558. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1559. fw_status = ERR_PTR(-EIO);
  1560. goto out_free_resp;
  1561. }
  1562. fw_status = kmemdup(status, len, GFP_KERNEL);
  1563. out_free_resp:
  1564. iwl_free_resp(&cmd);
  1565. return fw_status;
  1566. }
  1567. /* releases the MVM mutex */
  1568. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1569. struct ieee80211_vif *vif)
  1570. {
  1571. struct iwl_wowlan_status_data status;
  1572. struct iwl_wowlan_status *fw_status;
  1573. int i;
  1574. bool keep;
  1575. struct iwl_mvm_sta *mvm_ap_sta;
  1576. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1577. if (IS_ERR_OR_NULL(fw_status))
  1578. goto out_unlock;
  1579. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1580. for (i = 0; i < 8; i++)
  1581. status.qos_seq_ctr[i] =
  1582. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1583. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1584. status.wake_packet_length =
  1585. le32_to_cpu(fw_status->wake_packet_length);
  1586. status.wake_packet_bufsize =
  1587. le32_to_cpu(fw_status->wake_packet_bufsize);
  1588. status.wake_packet = fw_status->wake_packet;
  1589. /* still at hard-coded place 0 for D3 image */
  1590. mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
  1591. if (!mvm_ap_sta)
  1592. goto out_free;
  1593. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1594. u16 seq = status.qos_seq_ctr[i];
  1595. /* firmware stores last-used value, we store next value */
  1596. seq += 0x10;
  1597. mvm_ap_sta->tid_data[i].seq_number = seq;
  1598. }
  1599. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1600. mutex_unlock(&mvm->mutex);
  1601. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1602. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1603. kfree(fw_status);
  1604. return keep;
  1605. out_free:
  1606. kfree(fw_status);
  1607. out_unlock:
  1608. mutex_unlock(&mvm->mutex);
  1609. return false;
  1610. }
  1611. void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
  1612. struct ieee80211_vif *vif,
  1613. struct iwl_wowlan_status *status)
  1614. {
  1615. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1616. .mvm = mvm,
  1617. .status = status,
  1618. };
  1619. /*
  1620. * rekey handling requires taking locks that can't be taken now.
  1621. * however, d0i3 doesn't offload rekey, so we're fine.
  1622. */
  1623. if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
  1624. return;
  1625. /* find last GTK that we used initially, if any */
  1626. gtkdata.find_phase = true;
  1627. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1628. gtkdata.find_phase = false;
  1629. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1630. }
  1631. struct iwl_mvm_nd_query_results {
  1632. u32 matched_profiles;
  1633. struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
  1634. };
  1635. static int
  1636. iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
  1637. struct iwl_mvm_nd_query_results *results)
  1638. {
  1639. struct iwl_scan_offload_profiles_query *query;
  1640. struct iwl_host_cmd cmd = {
  1641. .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
  1642. .flags = CMD_WANT_SKB,
  1643. };
  1644. int ret, len;
  1645. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1646. if (ret) {
  1647. IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
  1648. return ret;
  1649. }
  1650. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1651. if (len < sizeof(*query)) {
  1652. IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
  1653. ret = -EIO;
  1654. goto out_free_resp;
  1655. }
  1656. query = (void *)cmd.resp_pkt->data;
  1657. results->matched_profiles = le32_to_cpu(query->matched_profiles);
  1658. memcpy(results->matches, query->matches, sizeof(results->matches));
  1659. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1660. mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
  1661. #endif
  1662. out_free_resp:
  1663. iwl_free_resp(&cmd);
  1664. return ret;
  1665. }
  1666. static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
  1667. struct ieee80211_vif *vif)
  1668. {
  1669. struct cfg80211_wowlan_nd_info *net_detect = NULL;
  1670. struct cfg80211_wowlan_wakeup wakeup = {
  1671. .pattern_idx = -1,
  1672. };
  1673. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1674. struct iwl_mvm_nd_query_results query;
  1675. struct iwl_wowlan_status *fw_status;
  1676. unsigned long matched_profiles;
  1677. u32 reasons = 0;
  1678. int i, j, n_matches, ret;
  1679. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1680. if (!IS_ERR_OR_NULL(fw_status)) {
  1681. reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1682. kfree(fw_status);
  1683. }
  1684. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1685. wakeup.rfkill_release = true;
  1686. if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
  1687. goto out;
  1688. ret = iwl_mvm_netdetect_query_results(mvm, &query);
  1689. if (ret || !query.matched_profiles) {
  1690. wakeup_report = NULL;
  1691. goto out;
  1692. }
  1693. matched_profiles = query.matched_profiles;
  1694. if (mvm->n_nd_match_sets) {
  1695. n_matches = hweight_long(matched_profiles);
  1696. } else {
  1697. IWL_ERR(mvm, "no net detect match information available\n");
  1698. n_matches = 0;
  1699. }
  1700. net_detect = kzalloc(sizeof(*net_detect) +
  1701. (n_matches * sizeof(net_detect->matches[0])),
  1702. GFP_KERNEL);
  1703. if (!net_detect || !n_matches)
  1704. goto out_report_nd;
  1705. for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
  1706. struct iwl_scan_offload_profile_match *fw_match;
  1707. struct cfg80211_wowlan_nd_match *match;
  1708. int idx, n_channels = 0;
  1709. fw_match = &query.matches[i];
  1710. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
  1711. n_channels += hweight8(fw_match->matching_channels[j]);
  1712. match = kzalloc(sizeof(*match) +
  1713. (n_channels * sizeof(*match->channels)),
  1714. GFP_KERNEL);
  1715. if (!match)
  1716. goto out_report_nd;
  1717. net_detect->matches[net_detect->n_matches++] = match;
  1718. /* We inverted the order of the SSIDs in the scan
  1719. * request, so invert the index here.
  1720. */
  1721. idx = mvm->n_nd_match_sets - i - 1;
  1722. match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
  1723. memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
  1724. match->ssid.ssid_len);
  1725. if (mvm->n_nd_channels < n_channels)
  1726. continue;
  1727. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
  1728. if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
  1729. match->channels[match->n_channels++] =
  1730. mvm->nd_channels[j]->center_freq;
  1731. }
  1732. out_report_nd:
  1733. wakeup.net_detect = net_detect;
  1734. out:
  1735. iwl_mvm_free_nd(mvm);
  1736. mutex_unlock(&mvm->mutex);
  1737. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1738. if (net_detect) {
  1739. for (i = 0; i < net_detect->n_matches; i++)
  1740. kfree(net_detect->matches[i]);
  1741. kfree(net_detect);
  1742. }
  1743. }
  1744. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1745. {
  1746. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1747. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1748. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1749. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1750. if (!mvm->store_d3_resume_sram)
  1751. return;
  1752. if (!mvm->d3_resume_sram) {
  1753. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1754. if (!mvm->d3_resume_sram)
  1755. return;
  1756. }
  1757. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1758. #endif
  1759. }
  1760. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1761. struct ieee80211_vif *vif)
  1762. {
  1763. /* skip the one we keep connection on */
  1764. if (data == vif)
  1765. return;
  1766. if (vif->type == NL80211_IFTYPE_STATION)
  1767. ieee80211_resume_disconnect(vif);
  1768. }
  1769. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1770. {
  1771. struct ieee80211_vif *vif = NULL;
  1772. int ret = 1;
  1773. enum iwl_d3_status d3_status;
  1774. bool keep = false;
  1775. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1776. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1777. bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
  1778. IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
  1779. mutex_lock(&mvm->mutex);
  1780. /* get the BSS vif pointer again */
  1781. vif = iwl_mvm_get_bss_vif(mvm);
  1782. if (IS_ERR_OR_NULL(vif))
  1783. goto err;
  1784. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
  1785. if (ret)
  1786. goto err;
  1787. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1788. IWL_INFO(mvm, "Device was reset during suspend\n");
  1789. goto err;
  1790. }
  1791. /* query SRAM first in case we want event logging */
  1792. iwl_mvm_read_d3_sram(mvm);
  1793. if (d0i3_first) {
  1794. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
  1795. if (ret < 0) {
  1796. IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
  1797. ret);
  1798. goto err;
  1799. }
  1800. }
  1801. /*
  1802. * Query the current location and source from the D3 firmware so we
  1803. * can play it back when we re-intiailize the D0 firmware
  1804. */
  1805. iwl_mvm_update_changed_regdom(mvm);
  1806. if (!unified_image)
  1807. /* Re-configure default SAR profile */
  1808. iwl_mvm_sar_select_profile(mvm, 1, 1);
  1809. if (mvm->net_detect) {
  1810. /* If this is a non-unified image, we restart the FW,
  1811. * so no need to stop the netdetect scan. If that
  1812. * fails, continue and try to get the wake-up reasons,
  1813. * but trigger a HW restart by keeping a failure code
  1814. * in ret.
  1815. */
  1816. if (unified_image)
  1817. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
  1818. false);
  1819. iwl_mvm_query_netdetect_reasons(mvm, vif);
  1820. /* has unlocked the mutex, so skip that */
  1821. goto out;
  1822. } else {
  1823. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1824. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1825. if (keep)
  1826. mvm->keep_vif = vif;
  1827. #endif
  1828. /* has unlocked the mutex, so skip that */
  1829. goto out_iterate;
  1830. }
  1831. err:
  1832. iwl_mvm_free_nd(mvm);
  1833. mutex_unlock(&mvm->mutex);
  1834. out_iterate:
  1835. if (!test)
  1836. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1837. IEEE80211_IFACE_ITER_NORMAL,
  1838. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1839. out:
  1840. /* no need to reset the device in unified images, if successful */
  1841. if (unified_image && !ret) {
  1842. /* nothing else to do if we already sent D0I3_END_CMD */
  1843. if (d0i3_first)
  1844. return 0;
  1845. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
  1846. if (!ret)
  1847. return 0;
  1848. }
  1849. /*
  1850. * Reconfigure the device in one of the following cases:
  1851. * 1. We are not using a unified image
  1852. * 2. We are using a unified image but had an error while exiting D3
  1853. */
  1854. set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
  1855. set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
  1856. /*
  1857. * When switching images we return 1, which causes mac80211
  1858. * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
  1859. * This type of reconfig calls iwl_mvm_restart_complete(),
  1860. * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
  1861. * to take the reference here.
  1862. */
  1863. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1864. return 1;
  1865. }
  1866. static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
  1867. {
  1868. iwl_trans_resume(mvm->trans);
  1869. return __iwl_mvm_resume(mvm, false);
  1870. }
  1871. static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
  1872. {
  1873. bool exit_now;
  1874. enum iwl_d3_status d3_status;
  1875. struct iwl_trans *trans = mvm->trans;
  1876. iwl_trans_d3_resume(trans, &d3_status, false, false);
  1877. /*
  1878. * make sure to clear D0I3_DEFER_WAKEUP before
  1879. * calling iwl_trans_resume(), which might wait
  1880. * for d0i3 exit completion.
  1881. */
  1882. mutex_lock(&mvm->d0i3_suspend_mutex);
  1883. __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1884. exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
  1885. &mvm->d0i3_suspend_flags);
  1886. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1887. if (exit_now) {
  1888. IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
  1889. _iwl_mvm_exit_d0i3(mvm);
  1890. }
  1891. iwl_trans_resume(trans);
  1892. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1893. int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
  1894. if (ret)
  1895. return ret;
  1896. /*
  1897. * d0i3 exit will be deferred until reconfig_complete.
  1898. * make sure there we are out of d0i3.
  1899. */
  1900. }
  1901. return 0;
  1902. }
  1903. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1904. {
  1905. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1906. int ret;
  1907. if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  1908. ret = iwl_mvm_resume_d0i3(mvm);
  1909. else
  1910. ret = iwl_mvm_resume_d3(mvm);
  1911. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1912. return ret;
  1913. }
  1914. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1915. {
  1916. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1917. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1918. }
  1919. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1920. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1921. {
  1922. struct iwl_mvm *mvm = inode->i_private;
  1923. int err;
  1924. if (mvm->d3_test_active)
  1925. return -EBUSY;
  1926. file->private_data = inode->i_private;
  1927. ieee80211_stop_queues(mvm->hw);
  1928. synchronize_net();
  1929. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1930. /* start pseudo D3 */
  1931. rtnl_lock();
  1932. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1933. rtnl_unlock();
  1934. if (err > 0)
  1935. err = -EINVAL;
  1936. if (err) {
  1937. ieee80211_wake_queues(mvm->hw);
  1938. return err;
  1939. }
  1940. mvm->d3_test_active = true;
  1941. mvm->keep_vif = NULL;
  1942. return 0;
  1943. }
  1944. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1945. size_t count, loff_t *ppos)
  1946. {
  1947. struct iwl_mvm *mvm = file->private_data;
  1948. u32 pme_asserted;
  1949. while (true) {
  1950. /* read pme_ptr if available */
  1951. if (mvm->d3_test_pme_ptr) {
  1952. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1953. mvm->d3_test_pme_ptr);
  1954. if (pme_asserted)
  1955. break;
  1956. }
  1957. if (msleep_interruptible(100))
  1958. break;
  1959. }
  1960. return 0;
  1961. }
  1962. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1963. struct ieee80211_vif *vif)
  1964. {
  1965. /* skip the one we keep connection on */
  1966. if (_data == vif)
  1967. return;
  1968. if (vif->type == NL80211_IFTYPE_STATION)
  1969. ieee80211_connection_loss(vif);
  1970. }
  1971. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1972. {
  1973. struct iwl_mvm *mvm = inode->i_private;
  1974. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1975. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1976. mvm->d3_test_active = false;
  1977. rtnl_lock();
  1978. __iwl_mvm_resume(mvm, true);
  1979. rtnl_unlock();
  1980. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1981. iwl_abort_notification_waits(&mvm->notif_wait);
  1982. if (!unified_image) {
  1983. int remaining_time = 10;
  1984. ieee80211_restart_hw(mvm->hw);
  1985. /* wait for restart and disconnect all interfaces */
  1986. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1987. remaining_time > 0) {
  1988. remaining_time--;
  1989. msleep(1000);
  1990. }
  1991. if (remaining_time == 0)
  1992. IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
  1993. }
  1994. ieee80211_iterate_active_interfaces_atomic(
  1995. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1996. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1997. ieee80211_wake_queues(mvm->hw);
  1998. return 0;
  1999. }
  2000. const struct file_operations iwl_dbgfs_d3_test_ops = {
  2001. .llseek = no_llseek,
  2002. .open = iwl_mvm_d3_test_open,
  2003. .read = iwl_mvm_d3_test_read,
  2004. .release = iwl_mvm_d3_test_release,
  2005. };
  2006. #endif