d3.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  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 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 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.sw_crypto)
  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. /* add back the PHY */
  596. if (WARN_ON(!mvmvif->phy_ctxt))
  597. return -EINVAL;
  598. rcu_read_lock();
  599. ctx = rcu_dereference(vif->chanctx_conf);
  600. if (WARN_ON(!ctx)) {
  601. rcu_read_unlock();
  602. return -EINVAL;
  603. }
  604. chandef = ctx->def;
  605. chains_static = ctx->rx_chains_static;
  606. chains_dynamic = ctx->rx_chains_dynamic;
  607. rcu_read_unlock();
  608. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  609. chains_static, chains_dynamic);
  610. if (ret)
  611. return ret;
  612. /* add back the MAC */
  613. mvmvif->uploaded = false;
  614. if (WARN_ON(!vif->bss_conf.assoc))
  615. return -EINVAL;
  616. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  617. if (ret)
  618. return ret;
  619. /* add back binding - XXX refactor? */
  620. binding_cmd.id_and_color =
  621. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  622. mvmvif->phy_ctxt->color));
  623. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  624. binding_cmd.phy =
  625. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  626. mvmvif->phy_ctxt->color));
  627. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  628. mvmvif->color));
  629. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  630. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  631. status = 0;
  632. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  633. sizeof(binding_cmd), &binding_cmd,
  634. &status);
  635. if (ret) {
  636. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  637. return ret;
  638. }
  639. if (status) {
  640. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  641. return -EIO;
  642. }
  643. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
  644. if (ret)
  645. return ret;
  646. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  647. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  648. if (ret)
  649. return ret;
  650. /* and some quota */
  651. quota_cmd.quotas[0].id_and_color =
  652. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  653. mvmvif->phy_ctxt->color));
  654. quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  655. quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  656. for (i = 1; i < MAX_BINDINGS; i++)
  657. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  658. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  659. sizeof(quota_cmd), &quota_cmd);
  660. if (ret)
  661. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  662. if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
  663. IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
  664. return 0;
  665. }
  666. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  667. struct ieee80211_vif *vif)
  668. {
  669. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  670. struct iwl_nonqos_seq_query_cmd query_cmd = {
  671. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  672. .mac_id_n_color =
  673. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  674. mvmvif->color)),
  675. };
  676. struct iwl_host_cmd cmd = {
  677. .id = NON_QOS_TX_COUNTER_CMD,
  678. .flags = CMD_WANT_SKB,
  679. };
  680. int err;
  681. u32 size;
  682. cmd.data[0] = &query_cmd;
  683. cmd.len[0] = sizeof(query_cmd);
  684. err = iwl_mvm_send_cmd(mvm, &cmd);
  685. if (err)
  686. return err;
  687. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  688. if (size < sizeof(__le16)) {
  689. err = -EINVAL;
  690. } else {
  691. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  692. /* firmware returns next, not last-used seqno */
  693. err = (u16) (err - 0x10);
  694. }
  695. iwl_free_resp(&cmd);
  696. return err;
  697. }
  698. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  699. {
  700. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  701. struct iwl_nonqos_seq_query_cmd query_cmd = {
  702. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  703. .mac_id_n_color =
  704. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  705. mvmvif->color)),
  706. .value = cpu_to_le16(mvmvif->seqno),
  707. };
  708. /* return if called during restart, not resume from D3 */
  709. if (!mvmvif->seqno_valid)
  710. return;
  711. mvmvif->seqno_valid = false;
  712. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
  713. sizeof(query_cmd), &query_cmd))
  714. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  715. }
  716. static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
  717. {
  718. iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
  719. iwl_mvm_stop_device(mvm);
  720. /*
  721. * Set the HW restart bit -- this is mostly true as we're
  722. * going to load new firmware and reprogram that, though
  723. * the reprogramming is going to be manual to avoid adding
  724. * all the MACs that aren't support.
  725. * We don't have to clear up everything though because the
  726. * reprogramming is manual. When we resume, we'll actually
  727. * go through a proper restart sequence again to switch
  728. * back to the runtime firmware image.
  729. */
  730. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  731. /* the fw is reset, so all the keys are cleared */
  732. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  733. mvm->ptk_ivlen = 0;
  734. mvm->ptk_icvlen = 0;
  735. mvm->ptk_ivlen = 0;
  736. mvm->ptk_icvlen = 0;
  737. return iwl_mvm_load_d3_fw(mvm);
  738. }
  739. static int
  740. iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
  741. struct cfg80211_wowlan *wowlan,
  742. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  743. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  744. struct ieee80211_sta *ap_sta)
  745. {
  746. int ret;
  747. struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  748. /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
  749. wowlan_config_cmd->is_11n_connection =
  750. ap_sta->ht_cap.ht_supported;
  751. wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
  752. ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
  753. /* Query the last used seqno and set it */
  754. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  755. if (ret < 0)
  756. return ret;
  757. wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
  758. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
  759. if (wowlan->disconnect)
  760. wowlan_config_cmd->wakeup_filter |=
  761. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  762. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  763. if (wowlan->magic_pkt)
  764. wowlan_config_cmd->wakeup_filter |=
  765. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  766. if (wowlan->gtk_rekey_failure)
  767. wowlan_config_cmd->wakeup_filter |=
  768. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  769. if (wowlan->eap_identity_req)
  770. wowlan_config_cmd->wakeup_filter |=
  771. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  772. if (wowlan->four_way_handshake)
  773. wowlan_config_cmd->wakeup_filter |=
  774. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  775. if (wowlan->n_patterns)
  776. wowlan_config_cmd->wakeup_filter |=
  777. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  778. if (wowlan->rfkill_release)
  779. wowlan_config_cmd->wakeup_filter |=
  780. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  781. if (wowlan->tcp) {
  782. /*
  783. * Set the "link change" (really "link lost") flag as well
  784. * since that implies losing the TCP connection.
  785. */
  786. wowlan_config_cmd->wakeup_filter |=
  787. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  788. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  789. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  790. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  791. }
  792. return 0;
  793. }
  794. static void
  795. iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
  796. struct ieee80211_vif *vif,
  797. void (*iter)(struct ieee80211_hw *hw,
  798. struct ieee80211_vif *vif,
  799. struct ieee80211_sta *sta,
  800. struct ieee80211_key_conf *key,
  801. void *data),
  802. void *data)
  803. {
  804. struct ieee80211_sta *ap_sta;
  805. rcu_read_lock();
  806. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
  807. if (IS_ERR_OR_NULL(ap_sta))
  808. goto out;
  809. ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
  810. out:
  811. rcu_read_unlock();
  812. }
  813. int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
  814. struct ieee80211_vif *vif,
  815. bool d0i3,
  816. u32 cmd_flags)
  817. {
  818. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  819. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  820. struct wowlan_key_data key_data = {
  821. .configure_keys = !d0i3,
  822. .use_rsc_tsc = false,
  823. .tkip = &tkip_cmd,
  824. .use_tkip = false,
  825. };
  826. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  827. int ret;
  828. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  829. if (!key_data.rsc_tsc)
  830. return -ENOMEM;
  831. /*
  832. * if we have to configure keys, call ieee80211_iter_keys(),
  833. * as we need non-atomic context in order to take the
  834. * required locks.
  835. * for the d0i3 we can't use ieee80211_iter_keys(), as
  836. * taking (almost) any mutex might result in deadlock.
  837. */
  838. if (!d0i3) {
  839. /*
  840. * Note that currently we don't propagate cmd_flags
  841. * to the iterator. In case of key_data.configure_keys,
  842. * all the configured commands are SYNC, and
  843. * iwl_mvm_wowlan_program_keys() will take care of
  844. * locking/unlocking mvm->mutex.
  845. */
  846. ieee80211_iter_keys(mvm->hw, vif,
  847. iwl_mvm_wowlan_program_keys,
  848. &key_data);
  849. } else {
  850. iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
  851. iwl_mvm_wowlan_program_keys,
  852. &key_data);
  853. }
  854. if (key_data.error) {
  855. ret = -EIO;
  856. goto out;
  857. }
  858. if (key_data.use_rsc_tsc) {
  859. ret = iwl_mvm_send_cmd_pdu(mvm,
  860. WOWLAN_TSC_RSC_PARAM, cmd_flags,
  861. sizeof(*key_data.rsc_tsc),
  862. key_data.rsc_tsc);
  863. if (ret)
  864. goto out;
  865. }
  866. if (key_data.use_tkip) {
  867. ret = iwl_mvm_send_cmd_pdu(mvm,
  868. WOWLAN_TKIP_PARAM,
  869. cmd_flags, sizeof(tkip_cmd),
  870. &tkip_cmd);
  871. if (ret)
  872. goto out;
  873. }
  874. /* configure rekey data only if offloaded rekey is supported (d3) */
  875. if (mvmvif->rekey_data.valid && !d0i3) {
  876. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  877. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  878. NL80211_KCK_LEN);
  879. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  880. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  881. NL80211_KEK_LEN);
  882. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  883. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  884. ret = iwl_mvm_send_cmd_pdu(mvm,
  885. WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
  886. sizeof(kek_kck_cmd),
  887. &kek_kck_cmd);
  888. if (ret)
  889. goto out;
  890. }
  891. ret = 0;
  892. out:
  893. kfree(key_data.rsc_tsc);
  894. return ret;
  895. }
  896. static int
  897. iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
  898. struct cfg80211_wowlan *wowlan,
  899. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  900. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  901. struct ieee80211_sta *ap_sta)
  902. {
  903. int ret;
  904. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  905. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  906. if (!unified_image) {
  907. ret = iwl_mvm_switch_to_d3(mvm);
  908. if (ret)
  909. return ret;
  910. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  911. if (ret)
  912. return ret;
  913. }
  914. if (!iwlwifi_mod_params.sw_crypto) {
  915. /*
  916. * This needs to be unlocked due to lock ordering
  917. * constraints. Since we're in the suspend path
  918. * that isn't really a problem though.
  919. */
  920. mutex_unlock(&mvm->mutex);
  921. ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
  922. CMD_ASYNC);
  923. mutex_lock(&mvm->mutex);
  924. if (ret)
  925. return ret;
  926. }
  927. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  928. sizeof(*wowlan_config_cmd),
  929. wowlan_config_cmd);
  930. if (ret)
  931. return ret;
  932. ret = iwl_mvm_send_patterns(mvm, wowlan);
  933. if (ret)
  934. return ret;
  935. ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
  936. if (ret)
  937. return ret;
  938. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  939. return ret;
  940. }
  941. static int
  942. iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
  943. struct cfg80211_wowlan *wowlan,
  944. struct cfg80211_sched_scan_request *nd_config,
  945. struct ieee80211_vif *vif)
  946. {
  947. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  948. int ret;
  949. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  950. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  951. if (!unified_image) {
  952. ret = iwl_mvm_switch_to_d3(mvm);
  953. if (ret)
  954. return ret;
  955. } else {
  956. /* In theory, we wouldn't have to stop a running sched
  957. * scan in order to start another one (for
  958. * net-detect). But in practice this doesn't seem to
  959. * work properly, so stop any running sched_scan now.
  960. */
  961. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
  962. if (ret)
  963. return ret;
  964. }
  965. /* rfkill release can be either for wowlan or netdetect */
  966. if (wowlan->rfkill_release)
  967. wowlan_config_cmd.wakeup_filter |=
  968. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  969. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  970. sizeof(wowlan_config_cmd),
  971. &wowlan_config_cmd);
  972. if (ret)
  973. return ret;
  974. ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
  975. IWL_MVM_SCAN_NETDETECT);
  976. if (ret)
  977. return ret;
  978. if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
  979. return -EBUSY;
  980. /* save the sched scan matchsets... */
  981. if (nd_config->n_match_sets) {
  982. mvm->nd_match_sets = kmemdup(nd_config->match_sets,
  983. sizeof(*nd_config->match_sets) *
  984. nd_config->n_match_sets,
  985. GFP_KERNEL);
  986. if (mvm->nd_match_sets)
  987. mvm->n_nd_match_sets = nd_config->n_match_sets;
  988. }
  989. /* ...and the sched scan channels for later reporting */
  990. mvm->nd_channels = kmemdup(nd_config->channels,
  991. sizeof(*nd_config->channels) *
  992. nd_config->n_channels,
  993. GFP_KERNEL);
  994. if (mvm->nd_channels)
  995. mvm->n_nd_channels = nd_config->n_channels;
  996. return 0;
  997. }
  998. static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
  999. {
  1000. kfree(mvm->nd_match_sets);
  1001. mvm->nd_match_sets = NULL;
  1002. mvm->n_nd_match_sets = 0;
  1003. kfree(mvm->nd_channels);
  1004. mvm->nd_channels = NULL;
  1005. mvm->n_nd_channels = 0;
  1006. }
  1007. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  1008. struct cfg80211_wowlan *wowlan,
  1009. bool test)
  1010. {
  1011. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1012. struct ieee80211_vif *vif = NULL;
  1013. struct iwl_mvm_vif *mvmvif = NULL;
  1014. struct ieee80211_sta *ap_sta = NULL;
  1015. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  1016. /*
  1017. * Program the minimum sleep time to 10 seconds, as many
  1018. * platforms have issues processing a wakeup signal while
  1019. * still being in the process of suspending.
  1020. */
  1021. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  1022. };
  1023. struct iwl_host_cmd d3_cfg_cmd = {
  1024. .id = D3_CONFIG_CMD,
  1025. .flags = CMD_WANT_SKB,
  1026. .data[0] = &d3_cfg_cmd_data,
  1027. .len[0] = sizeof(d3_cfg_cmd_data),
  1028. };
  1029. int ret;
  1030. int len __maybe_unused;
  1031. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1032. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1033. if (!wowlan) {
  1034. /*
  1035. * mac80211 shouldn't get here, but for D3 test
  1036. * it doesn't warrant a warning
  1037. */
  1038. WARN_ON(!test);
  1039. return -EINVAL;
  1040. }
  1041. mutex_lock(&mvm->mutex);
  1042. vif = iwl_mvm_get_bss_vif(mvm);
  1043. if (IS_ERR_OR_NULL(vif)) {
  1044. ret = 1;
  1045. goto out_noreset;
  1046. }
  1047. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1048. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
  1049. /* if we're not associated, this must be netdetect */
  1050. if (!wowlan->nd_config) {
  1051. ret = 1;
  1052. goto out_noreset;
  1053. }
  1054. ret = iwl_mvm_netdetect_config(
  1055. mvm, wowlan, wowlan->nd_config, vif);
  1056. if (ret)
  1057. goto out;
  1058. mvm->net_detect = true;
  1059. } else {
  1060. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  1061. ap_sta = rcu_dereference_protected(
  1062. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  1063. lockdep_is_held(&mvm->mutex));
  1064. if (IS_ERR_OR_NULL(ap_sta)) {
  1065. ret = -EINVAL;
  1066. goto out_noreset;
  1067. }
  1068. ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1069. vif, mvmvif, ap_sta);
  1070. if (ret)
  1071. goto out_noreset;
  1072. ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1073. vif, mvmvif, ap_sta);
  1074. if (ret)
  1075. goto out;
  1076. mvm->net_detect = false;
  1077. }
  1078. ret = iwl_mvm_power_update_device(mvm);
  1079. if (ret)
  1080. goto out;
  1081. ret = iwl_mvm_power_update_mac(mvm);
  1082. if (ret)
  1083. goto out;
  1084. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1085. if (mvm->d3_wake_sysassert)
  1086. d3_cfg_cmd_data.wakeup_flags |=
  1087. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  1088. #endif
  1089. /* must be last -- this switches firmware state */
  1090. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  1091. if (ret)
  1092. goto out;
  1093. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1094. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  1095. if (len >= sizeof(u32)) {
  1096. mvm->d3_test_pme_ptr =
  1097. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  1098. }
  1099. #endif
  1100. iwl_free_resp(&d3_cfg_cmd);
  1101. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1102. iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
  1103. out:
  1104. if (ret < 0) {
  1105. iwl_mvm_free_nd(mvm);
  1106. if (!unified_image) {
  1107. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1108. if (mvm->restart_fw > 0) {
  1109. mvm->restart_fw--;
  1110. ieee80211_restart_hw(mvm->hw);
  1111. }
  1112. }
  1113. }
  1114. out_noreset:
  1115. mutex_unlock(&mvm->mutex);
  1116. return ret;
  1117. }
  1118. static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
  1119. {
  1120. struct iwl_notification_wait wait_d3;
  1121. static const u16 d3_notif[] = { D3_CONFIG_CMD };
  1122. int ret;
  1123. iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
  1124. d3_notif, ARRAY_SIZE(d3_notif),
  1125. NULL, NULL);
  1126. ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
  1127. if (ret)
  1128. goto remove_notif;
  1129. ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
  1130. WARN_ON_ONCE(ret);
  1131. return ret;
  1132. remove_notif:
  1133. iwl_remove_notification(&mvm->notif_wait, &wait_d3);
  1134. return ret;
  1135. }
  1136. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1137. {
  1138. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1139. struct iwl_trans *trans = mvm->trans;
  1140. int ret;
  1141. /* make sure the d0i3 exit work is not pending */
  1142. flush_work(&mvm->d0i3_exit_work);
  1143. ret = iwl_trans_suspend(trans);
  1144. if (ret)
  1145. return ret;
  1146. if (wowlan->any) {
  1147. trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  1148. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1149. ret = iwl_mvm_enter_d0i3_sync(mvm);
  1150. if (ret)
  1151. return ret;
  1152. }
  1153. mutex_lock(&mvm->d0i3_suspend_mutex);
  1154. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1155. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1156. iwl_trans_d3_suspend(trans, false, false);
  1157. return 0;
  1158. }
  1159. trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1160. return __iwl_mvm_suspend(hw, wowlan, false);
  1161. }
  1162. /* converted data from the different status responses */
  1163. struct iwl_wowlan_status_data {
  1164. u16 pattern_number;
  1165. u16 qos_seq_ctr[8];
  1166. u32 wakeup_reasons;
  1167. u32 wake_packet_length;
  1168. u32 wake_packet_bufsize;
  1169. const u8 *wake_packet;
  1170. };
  1171. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1172. struct ieee80211_vif *vif,
  1173. struct iwl_wowlan_status_data *status)
  1174. {
  1175. struct sk_buff *pkt = NULL;
  1176. struct cfg80211_wowlan_wakeup wakeup = {
  1177. .pattern_idx = -1,
  1178. };
  1179. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1180. u32 reasons = status->wakeup_reasons;
  1181. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1182. wakeup_report = NULL;
  1183. goto report;
  1184. }
  1185. pm_wakeup_event(mvm->dev, 0);
  1186. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1187. wakeup.magic_pkt = true;
  1188. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1189. wakeup.pattern_idx =
  1190. status->pattern_number;
  1191. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1192. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1193. wakeup.disconnect = true;
  1194. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1195. wakeup.gtk_rekey_failure = true;
  1196. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1197. wakeup.rfkill_release = true;
  1198. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1199. wakeup.eap_identity_req = true;
  1200. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1201. wakeup.four_way_handshake = true;
  1202. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1203. wakeup.tcp_connlost = true;
  1204. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1205. wakeup.tcp_nomoretokens = true;
  1206. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1207. wakeup.tcp_match = true;
  1208. if (status->wake_packet_bufsize) {
  1209. int pktsize = status->wake_packet_bufsize;
  1210. int pktlen = status->wake_packet_length;
  1211. const u8 *pktdata = status->wake_packet;
  1212. struct ieee80211_hdr *hdr = (void *)pktdata;
  1213. int truncated = pktlen - pktsize;
  1214. /* this would be a firmware bug */
  1215. if (WARN_ON_ONCE(truncated < 0))
  1216. truncated = 0;
  1217. if (ieee80211_is_data(hdr->frame_control)) {
  1218. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1219. int ivlen = 0, icvlen = 4; /* also FCS */
  1220. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1221. if (!pkt)
  1222. goto report;
  1223. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1224. pktdata += hdrlen;
  1225. pktsize -= hdrlen;
  1226. if (ieee80211_has_protected(hdr->frame_control)) {
  1227. /*
  1228. * This is unlocked and using gtk_i(c)vlen,
  1229. * but since everything is under RTNL still
  1230. * that's not really a problem - changing
  1231. * it would be difficult.
  1232. */
  1233. if (is_multicast_ether_addr(hdr->addr1)) {
  1234. ivlen = mvm->gtk_ivlen;
  1235. icvlen += mvm->gtk_icvlen;
  1236. } else {
  1237. ivlen = mvm->ptk_ivlen;
  1238. icvlen += mvm->ptk_icvlen;
  1239. }
  1240. }
  1241. /* if truncated, FCS/ICV is (partially) gone */
  1242. if (truncated >= icvlen) {
  1243. icvlen = 0;
  1244. truncated -= icvlen;
  1245. } else {
  1246. icvlen -= truncated;
  1247. truncated = 0;
  1248. }
  1249. pktsize -= ivlen + icvlen;
  1250. pktdata += ivlen;
  1251. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1252. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1253. goto report;
  1254. wakeup.packet = pkt->data;
  1255. wakeup.packet_present_len = pkt->len;
  1256. wakeup.packet_len = pkt->len - truncated;
  1257. wakeup.packet_80211 = false;
  1258. } else {
  1259. int fcslen = 4;
  1260. if (truncated >= 4) {
  1261. truncated -= 4;
  1262. fcslen = 0;
  1263. } else {
  1264. fcslen -= truncated;
  1265. truncated = 0;
  1266. }
  1267. pktsize -= fcslen;
  1268. wakeup.packet = status->wake_packet;
  1269. wakeup.packet_present_len = pktsize;
  1270. wakeup.packet_len = pktlen - truncated;
  1271. wakeup.packet_80211 = true;
  1272. }
  1273. }
  1274. report:
  1275. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1276. kfree_skb(pkt);
  1277. }
  1278. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1279. struct ieee80211_key_seq *seq)
  1280. {
  1281. u64 pn;
  1282. pn = le64_to_cpu(sc->pn);
  1283. seq->ccmp.pn[0] = pn >> 40;
  1284. seq->ccmp.pn[1] = pn >> 32;
  1285. seq->ccmp.pn[2] = pn >> 24;
  1286. seq->ccmp.pn[3] = pn >> 16;
  1287. seq->ccmp.pn[4] = pn >> 8;
  1288. seq->ccmp.pn[5] = pn;
  1289. }
  1290. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1291. struct ieee80211_key_seq *seq)
  1292. {
  1293. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1294. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1295. }
  1296. static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
  1297. struct ieee80211_sta *sta,
  1298. struct ieee80211_key_conf *key)
  1299. {
  1300. int tid;
  1301. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1302. if (sta && iwl_mvm_has_new_rx_api(mvm)) {
  1303. struct iwl_mvm_sta *mvmsta;
  1304. struct iwl_mvm_key_pn *ptk_pn;
  1305. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1306. ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
  1307. lockdep_is_held(&mvm->mutex));
  1308. if (WARN_ON(!ptk_pn))
  1309. return;
  1310. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  1311. struct ieee80211_key_seq seq = {};
  1312. int i;
  1313. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1314. ieee80211_set_key_rx_seq(key, tid, &seq);
  1315. for (i = 1; i < mvm->trans->num_rx_queues; i++)
  1316. memcpy(ptk_pn->q[i].pn[tid],
  1317. seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1318. }
  1319. } else {
  1320. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1321. struct ieee80211_key_seq seq = {};
  1322. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1323. ieee80211_set_key_rx_seq(key, tid, &seq);
  1324. }
  1325. }
  1326. }
  1327. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1328. struct ieee80211_key_conf *key)
  1329. {
  1330. int tid;
  1331. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1332. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1333. struct ieee80211_key_seq seq = {};
  1334. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1335. ieee80211_set_key_rx_seq(key, tid, &seq);
  1336. }
  1337. }
  1338. static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
  1339. struct ieee80211_key_conf *key,
  1340. struct iwl_wowlan_status *status)
  1341. {
  1342. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1343. switch (key->cipher) {
  1344. case WLAN_CIPHER_SUITE_CCMP:
  1345. iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
  1346. break;
  1347. case WLAN_CIPHER_SUITE_TKIP:
  1348. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1349. break;
  1350. default:
  1351. WARN_ON(1);
  1352. }
  1353. }
  1354. struct iwl_mvm_d3_gtk_iter_data {
  1355. struct iwl_mvm *mvm;
  1356. struct iwl_wowlan_status *status;
  1357. void *last_gtk;
  1358. u32 cipher;
  1359. bool find_phase, unhandled_cipher;
  1360. int num_keys;
  1361. };
  1362. static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
  1363. struct ieee80211_vif *vif,
  1364. struct ieee80211_sta *sta,
  1365. struct ieee80211_key_conf *key,
  1366. void *_data)
  1367. {
  1368. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1369. if (data->unhandled_cipher)
  1370. return;
  1371. switch (key->cipher) {
  1372. case WLAN_CIPHER_SUITE_WEP40:
  1373. case WLAN_CIPHER_SUITE_WEP104:
  1374. /* ignore WEP completely, nothing to do */
  1375. return;
  1376. case WLAN_CIPHER_SUITE_CCMP:
  1377. case WLAN_CIPHER_SUITE_TKIP:
  1378. /* we support these */
  1379. break;
  1380. default:
  1381. /* everything else (even CMAC for MFP) - disconnect from AP */
  1382. data->unhandled_cipher = true;
  1383. return;
  1384. }
  1385. data->num_keys++;
  1386. /*
  1387. * pairwise key - update sequence counters only;
  1388. * note that this assumes no TDLS sessions are active
  1389. */
  1390. if (sta) {
  1391. struct ieee80211_key_seq seq = {};
  1392. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1393. if (data->find_phase)
  1394. return;
  1395. switch (key->cipher) {
  1396. case WLAN_CIPHER_SUITE_CCMP:
  1397. iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
  1398. sta, key);
  1399. atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
  1400. break;
  1401. case WLAN_CIPHER_SUITE_TKIP:
  1402. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1403. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1404. atomic64_set(&key->tx_pn,
  1405. (u64)seq.tkip.iv16 |
  1406. ((u64)seq.tkip.iv32 << 16));
  1407. break;
  1408. }
  1409. /* that's it for this key */
  1410. return;
  1411. }
  1412. if (data->find_phase) {
  1413. data->last_gtk = key;
  1414. data->cipher = key->cipher;
  1415. return;
  1416. }
  1417. if (data->status->num_of_gtk_rekeys)
  1418. ieee80211_remove_key(key);
  1419. else if (data->last_gtk == key)
  1420. iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
  1421. }
  1422. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1423. struct ieee80211_vif *vif,
  1424. struct iwl_wowlan_status *status)
  1425. {
  1426. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1427. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1428. .mvm = mvm,
  1429. .status = status,
  1430. };
  1431. u32 disconnection_reasons =
  1432. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1433. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1434. if (!status || !vif->bss_conf.bssid)
  1435. return false;
  1436. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1437. return false;
  1438. /* find last GTK that we used initially, if any */
  1439. gtkdata.find_phase = true;
  1440. ieee80211_iter_keys(mvm->hw, vif,
  1441. iwl_mvm_d3_update_keys, &gtkdata);
  1442. /* not trying to keep connections with MFP/unhandled ciphers */
  1443. if (gtkdata.unhandled_cipher)
  1444. return false;
  1445. if (!gtkdata.num_keys)
  1446. goto out;
  1447. if (!gtkdata.last_gtk)
  1448. return false;
  1449. /*
  1450. * invalidate all other GTKs that might still exist and update
  1451. * the one that we used
  1452. */
  1453. gtkdata.find_phase = false;
  1454. ieee80211_iter_keys(mvm->hw, vif,
  1455. iwl_mvm_d3_update_keys, &gtkdata);
  1456. if (status->num_of_gtk_rekeys) {
  1457. struct ieee80211_key_conf *key;
  1458. struct {
  1459. struct ieee80211_key_conf conf;
  1460. u8 key[32];
  1461. } conf = {
  1462. .conf.cipher = gtkdata.cipher,
  1463. .conf.keyidx = status->gtk.key_index,
  1464. };
  1465. switch (gtkdata.cipher) {
  1466. case WLAN_CIPHER_SUITE_CCMP:
  1467. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1468. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1469. WLAN_KEY_LEN_CCMP);
  1470. break;
  1471. case WLAN_CIPHER_SUITE_TKIP:
  1472. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1473. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1474. /* leave TX MIC key zeroed, we don't use it anyway */
  1475. memcpy(conf.conf.key +
  1476. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1477. status->gtk.tkip_mic_key, 8);
  1478. break;
  1479. }
  1480. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1481. if (IS_ERR(key))
  1482. return false;
  1483. iwl_mvm_set_key_rx_seq(mvm, key, status);
  1484. }
  1485. if (status->num_of_gtk_rekeys) {
  1486. __be64 replay_ctr =
  1487. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1488. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1489. (void *)&replay_ctr, GFP_KERNEL);
  1490. }
  1491. out:
  1492. mvmvif->seqno_valid = true;
  1493. /* +0x10 because the set API expects next-to-use, not last-used */
  1494. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1495. return true;
  1496. }
  1497. static struct iwl_wowlan_status *
  1498. iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1499. {
  1500. u32 base = mvm->error_event_table;
  1501. struct error_table_start {
  1502. /* cf. struct iwl_error_event_table */
  1503. u32 valid;
  1504. u32 error_id;
  1505. } err_info;
  1506. struct iwl_host_cmd cmd = {
  1507. .id = WOWLAN_GET_STATUSES,
  1508. .flags = CMD_WANT_SKB,
  1509. };
  1510. struct iwl_wowlan_status *status, *fw_status;
  1511. int ret, len, status_size;
  1512. iwl_trans_read_mem_bytes(mvm->trans, base,
  1513. &err_info, sizeof(err_info));
  1514. if (err_info.valid) {
  1515. IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
  1516. err_info.valid, err_info.error_id);
  1517. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1518. struct cfg80211_wowlan_wakeup wakeup = {
  1519. .rfkill_release = true,
  1520. };
  1521. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1522. GFP_KERNEL);
  1523. }
  1524. return ERR_PTR(-EIO);
  1525. }
  1526. /* only for tracing for now */
  1527. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1528. if (ret)
  1529. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1530. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1531. if (ret) {
  1532. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1533. return ERR_PTR(ret);
  1534. }
  1535. /* RF-kill already asserted again... */
  1536. if (!cmd.resp_pkt) {
  1537. fw_status = ERR_PTR(-ERFKILL);
  1538. goto out_free_resp;
  1539. }
  1540. status_size = sizeof(*fw_status);
  1541. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1542. if (len < status_size) {
  1543. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1544. fw_status = ERR_PTR(-EIO);
  1545. goto out_free_resp;
  1546. }
  1547. status = (void *)cmd.resp_pkt->data;
  1548. if (len != (status_size +
  1549. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
  1550. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1551. fw_status = ERR_PTR(-EIO);
  1552. goto out_free_resp;
  1553. }
  1554. fw_status = kmemdup(status, len, GFP_KERNEL);
  1555. out_free_resp:
  1556. iwl_free_resp(&cmd);
  1557. return fw_status;
  1558. }
  1559. /* releases the MVM mutex */
  1560. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1561. struct ieee80211_vif *vif)
  1562. {
  1563. struct iwl_wowlan_status_data status;
  1564. struct iwl_wowlan_status *fw_status;
  1565. int i;
  1566. bool keep;
  1567. struct iwl_mvm_sta *mvm_ap_sta;
  1568. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1569. if (IS_ERR_OR_NULL(fw_status))
  1570. goto out_unlock;
  1571. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1572. for (i = 0; i < 8; i++)
  1573. status.qos_seq_ctr[i] =
  1574. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1575. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1576. status.wake_packet_length =
  1577. le32_to_cpu(fw_status->wake_packet_length);
  1578. status.wake_packet_bufsize =
  1579. le32_to_cpu(fw_status->wake_packet_bufsize);
  1580. status.wake_packet = fw_status->wake_packet;
  1581. /* still at hard-coded place 0 for D3 image */
  1582. mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
  1583. if (!mvm_ap_sta)
  1584. goto out_free;
  1585. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1586. u16 seq = status.qos_seq_ctr[i];
  1587. /* firmware stores last-used value, we store next value */
  1588. seq += 0x10;
  1589. mvm_ap_sta->tid_data[i].seq_number = seq;
  1590. }
  1591. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1592. mutex_unlock(&mvm->mutex);
  1593. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1594. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1595. kfree(fw_status);
  1596. return keep;
  1597. out_free:
  1598. kfree(fw_status);
  1599. out_unlock:
  1600. mutex_unlock(&mvm->mutex);
  1601. return false;
  1602. }
  1603. void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
  1604. struct ieee80211_vif *vif,
  1605. struct iwl_wowlan_status *status)
  1606. {
  1607. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1608. .mvm = mvm,
  1609. .status = status,
  1610. };
  1611. /*
  1612. * rekey handling requires taking locks that can't be taken now.
  1613. * however, d0i3 doesn't offload rekey, so we're fine.
  1614. */
  1615. if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
  1616. return;
  1617. /* find last GTK that we used initially, if any */
  1618. gtkdata.find_phase = true;
  1619. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1620. gtkdata.find_phase = false;
  1621. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1622. }
  1623. struct iwl_mvm_nd_query_results {
  1624. u32 matched_profiles;
  1625. struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
  1626. };
  1627. static int
  1628. iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
  1629. struct iwl_mvm_nd_query_results *results)
  1630. {
  1631. struct iwl_scan_offload_profiles_query *query;
  1632. struct iwl_host_cmd cmd = {
  1633. .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
  1634. .flags = CMD_WANT_SKB,
  1635. };
  1636. int ret, len;
  1637. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1638. if (ret) {
  1639. IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
  1640. return ret;
  1641. }
  1642. /* RF-kill already asserted again... */
  1643. if (!cmd.resp_pkt) {
  1644. ret = -ERFKILL;
  1645. goto out_free_resp;
  1646. }
  1647. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1648. if (len < sizeof(*query)) {
  1649. IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
  1650. ret = -EIO;
  1651. goto out_free_resp;
  1652. }
  1653. query = (void *)cmd.resp_pkt->data;
  1654. results->matched_profiles = le32_to_cpu(query->matched_profiles);
  1655. memcpy(results->matches, query->matches, sizeof(results->matches));
  1656. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1657. mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
  1658. #endif
  1659. out_free_resp:
  1660. iwl_free_resp(&cmd);
  1661. return ret;
  1662. }
  1663. static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
  1664. struct ieee80211_vif *vif)
  1665. {
  1666. struct cfg80211_wowlan_nd_info *net_detect = NULL;
  1667. struct cfg80211_wowlan_wakeup wakeup = {
  1668. .pattern_idx = -1,
  1669. };
  1670. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1671. struct iwl_mvm_nd_query_results query;
  1672. struct iwl_wowlan_status *fw_status;
  1673. unsigned long matched_profiles;
  1674. u32 reasons = 0;
  1675. int i, j, n_matches, ret;
  1676. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1677. if (!IS_ERR_OR_NULL(fw_status)) {
  1678. reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1679. kfree(fw_status);
  1680. }
  1681. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1682. wakeup.rfkill_release = true;
  1683. if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
  1684. goto out;
  1685. ret = iwl_mvm_netdetect_query_results(mvm, &query);
  1686. if (ret || !query.matched_profiles) {
  1687. wakeup_report = NULL;
  1688. goto out;
  1689. }
  1690. matched_profiles = query.matched_profiles;
  1691. if (mvm->n_nd_match_sets) {
  1692. n_matches = hweight_long(matched_profiles);
  1693. } else {
  1694. IWL_ERR(mvm, "no net detect match information available\n");
  1695. n_matches = 0;
  1696. }
  1697. net_detect = kzalloc(sizeof(*net_detect) +
  1698. (n_matches * sizeof(net_detect->matches[0])),
  1699. GFP_KERNEL);
  1700. if (!net_detect || !n_matches)
  1701. goto out_report_nd;
  1702. for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
  1703. struct iwl_scan_offload_profile_match *fw_match;
  1704. struct cfg80211_wowlan_nd_match *match;
  1705. int idx, n_channels = 0;
  1706. fw_match = &query.matches[i];
  1707. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
  1708. n_channels += hweight8(fw_match->matching_channels[j]);
  1709. match = kzalloc(sizeof(*match) +
  1710. (n_channels * sizeof(*match->channels)),
  1711. GFP_KERNEL);
  1712. if (!match)
  1713. goto out_report_nd;
  1714. net_detect->matches[net_detect->n_matches++] = match;
  1715. /* We inverted the order of the SSIDs in the scan
  1716. * request, so invert the index here.
  1717. */
  1718. idx = mvm->n_nd_match_sets - i - 1;
  1719. match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
  1720. memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
  1721. match->ssid.ssid_len);
  1722. if (mvm->n_nd_channels < n_channels)
  1723. continue;
  1724. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
  1725. if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
  1726. match->channels[match->n_channels++] =
  1727. mvm->nd_channels[j]->center_freq;
  1728. }
  1729. out_report_nd:
  1730. wakeup.net_detect = net_detect;
  1731. out:
  1732. iwl_mvm_free_nd(mvm);
  1733. mutex_unlock(&mvm->mutex);
  1734. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1735. if (net_detect) {
  1736. for (i = 0; i < net_detect->n_matches; i++)
  1737. kfree(net_detect->matches[i]);
  1738. kfree(net_detect);
  1739. }
  1740. }
  1741. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1742. {
  1743. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1744. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1745. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1746. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1747. if (!mvm->store_d3_resume_sram)
  1748. return;
  1749. if (!mvm->d3_resume_sram) {
  1750. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1751. if (!mvm->d3_resume_sram)
  1752. return;
  1753. }
  1754. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1755. #endif
  1756. }
  1757. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1758. struct ieee80211_vif *vif)
  1759. {
  1760. /* skip the one we keep connection on */
  1761. if (data == vif)
  1762. return;
  1763. if (vif->type == NL80211_IFTYPE_STATION)
  1764. ieee80211_resume_disconnect(vif);
  1765. }
  1766. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1767. {
  1768. struct ieee80211_vif *vif = NULL;
  1769. int ret = 1;
  1770. enum iwl_d3_status d3_status;
  1771. bool keep = false;
  1772. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1773. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1774. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
  1775. CMD_WAKE_UP_TRANS;
  1776. mutex_lock(&mvm->mutex);
  1777. /* get the BSS vif pointer again */
  1778. vif = iwl_mvm_get_bss_vif(mvm);
  1779. if (IS_ERR_OR_NULL(vif))
  1780. goto err;
  1781. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
  1782. if (ret)
  1783. goto err;
  1784. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1785. IWL_INFO(mvm, "Device was reset during suspend\n");
  1786. goto err;
  1787. }
  1788. /* query SRAM first in case we want event logging */
  1789. iwl_mvm_read_d3_sram(mvm);
  1790. /*
  1791. * Query the current location and source from the D3 firmware so we
  1792. * can play it back when we re-intiailize the D0 firmware
  1793. */
  1794. iwl_mvm_update_changed_regdom(mvm);
  1795. if (mvm->net_detect) {
  1796. /* If this is a non-unified image, we restart the FW,
  1797. * so no need to stop the netdetect scan. If that
  1798. * fails, continue and try to get the wake-up reasons,
  1799. * but trigger a HW restart by keeping a failure code
  1800. * in ret.
  1801. */
  1802. if (unified_image)
  1803. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
  1804. false);
  1805. iwl_mvm_query_netdetect_reasons(mvm, vif);
  1806. /* has unlocked the mutex, so skip that */
  1807. goto out;
  1808. } else {
  1809. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1810. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1811. if (keep)
  1812. mvm->keep_vif = vif;
  1813. #endif
  1814. /* has unlocked the mutex, so skip that */
  1815. goto out_iterate;
  1816. }
  1817. err:
  1818. iwl_mvm_free_nd(mvm);
  1819. mutex_unlock(&mvm->mutex);
  1820. out_iterate:
  1821. if (!test)
  1822. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1823. IEEE80211_IFACE_ITER_NORMAL,
  1824. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1825. out:
  1826. if (unified_image && !ret) {
  1827. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
  1828. if (!ret) /* D3 ended successfully - no need to reset device */
  1829. return 0;
  1830. }
  1831. /*
  1832. * Reconfigure the device in one of the following cases:
  1833. * 1. We are not using a unified image
  1834. * 2. We are using a unified image but had an error while exiting D3
  1835. */
  1836. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1837. set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
  1838. /*
  1839. * When switching images we return 1, which causes mac80211
  1840. * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
  1841. * This type of reconfig calls iwl_mvm_restart_complete(),
  1842. * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
  1843. * to take the reference here.
  1844. */
  1845. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1846. return 1;
  1847. }
  1848. static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
  1849. {
  1850. iwl_trans_resume(mvm->trans);
  1851. return __iwl_mvm_resume(mvm, false);
  1852. }
  1853. static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
  1854. {
  1855. bool exit_now;
  1856. enum iwl_d3_status d3_status;
  1857. struct iwl_trans *trans = mvm->trans;
  1858. iwl_trans_d3_resume(trans, &d3_status, false, false);
  1859. /*
  1860. * make sure to clear D0I3_DEFER_WAKEUP before
  1861. * calling iwl_trans_resume(), which might wait
  1862. * for d0i3 exit completion.
  1863. */
  1864. mutex_lock(&mvm->d0i3_suspend_mutex);
  1865. __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1866. exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
  1867. &mvm->d0i3_suspend_flags);
  1868. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1869. if (exit_now) {
  1870. IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
  1871. _iwl_mvm_exit_d0i3(mvm);
  1872. }
  1873. iwl_trans_resume(trans);
  1874. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1875. int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
  1876. if (ret)
  1877. return ret;
  1878. /*
  1879. * d0i3 exit will be deferred until reconfig_complete.
  1880. * make sure there we are out of d0i3.
  1881. */
  1882. }
  1883. return 0;
  1884. }
  1885. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1886. {
  1887. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1888. int ret;
  1889. if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  1890. ret = iwl_mvm_resume_d0i3(mvm);
  1891. else
  1892. ret = iwl_mvm_resume_d3(mvm);
  1893. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1894. return ret;
  1895. }
  1896. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1897. {
  1898. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1899. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1900. }
  1901. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1902. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1903. {
  1904. struct iwl_mvm *mvm = inode->i_private;
  1905. int err;
  1906. if (mvm->d3_test_active)
  1907. return -EBUSY;
  1908. file->private_data = inode->i_private;
  1909. ieee80211_stop_queues(mvm->hw);
  1910. synchronize_net();
  1911. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1912. /* start pseudo D3 */
  1913. rtnl_lock();
  1914. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1915. rtnl_unlock();
  1916. if (err > 0)
  1917. err = -EINVAL;
  1918. if (err) {
  1919. ieee80211_wake_queues(mvm->hw);
  1920. return err;
  1921. }
  1922. mvm->d3_test_active = true;
  1923. mvm->keep_vif = NULL;
  1924. return 0;
  1925. }
  1926. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1927. size_t count, loff_t *ppos)
  1928. {
  1929. struct iwl_mvm *mvm = file->private_data;
  1930. u32 pme_asserted;
  1931. while (true) {
  1932. /* read pme_ptr if available */
  1933. if (mvm->d3_test_pme_ptr) {
  1934. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1935. mvm->d3_test_pme_ptr);
  1936. if (pme_asserted)
  1937. break;
  1938. }
  1939. if (msleep_interruptible(100))
  1940. break;
  1941. }
  1942. return 0;
  1943. }
  1944. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1945. struct ieee80211_vif *vif)
  1946. {
  1947. /* skip the one we keep connection on */
  1948. if (_data == vif)
  1949. return;
  1950. if (vif->type == NL80211_IFTYPE_STATION)
  1951. ieee80211_connection_loss(vif);
  1952. }
  1953. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1954. {
  1955. struct iwl_mvm *mvm = inode->i_private;
  1956. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1957. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1958. mvm->d3_test_active = false;
  1959. rtnl_lock();
  1960. __iwl_mvm_resume(mvm, true);
  1961. rtnl_unlock();
  1962. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1963. iwl_abort_notification_waits(&mvm->notif_wait);
  1964. if (!unified_image) {
  1965. int remaining_time = 10;
  1966. ieee80211_restart_hw(mvm->hw);
  1967. /* wait for restart and disconnect all interfaces */
  1968. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1969. remaining_time > 0) {
  1970. remaining_time--;
  1971. msleep(1000);
  1972. }
  1973. if (remaining_time == 0)
  1974. IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
  1975. }
  1976. ieee80211_iterate_active_interfaces_atomic(
  1977. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1978. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1979. ieee80211_wake_queues(mvm->hw);
  1980. return 0;
  1981. }
  1982. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1983. .llseek = no_llseek,
  1984. .open = iwl_mvm_d3_test_open,
  1985. .read = iwl_mvm_d3_test_read,
  1986. .release = iwl_mvm_d3_test_release,
  1987. };
  1988. #endif