d3.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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);
  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. }
  956. /* rfkill release can be either for wowlan or netdetect */
  957. if (wowlan->rfkill_release)
  958. wowlan_config_cmd.wakeup_filter |=
  959. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  960. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  961. sizeof(wowlan_config_cmd),
  962. &wowlan_config_cmd);
  963. if (ret)
  964. return ret;
  965. ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
  966. IWL_MVM_SCAN_NETDETECT);
  967. if (ret)
  968. return ret;
  969. if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
  970. return -EBUSY;
  971. /* save the sched scan matchsets... */
  972. if (nd_config->n_match_sets) {
  973. mvm->nd_match_sets = kmemdup(nd_config->match_sets,
  974. sizeof(*nd_config->match_sets) *
  975. nd_config->n_match_sets,
  976. GFP_KERNEL);
  977. if (mvm->nd_match_sets)
  978. mvm->n_nd_match_sets = nd_config->n_match_sets;
  979. }
  980. /* ...and the sched scan channels for later reporting */
  981. mvm->nd_channels = kmemdup(nd_config->channels,
  982. sizeof(*nd_config->channels) *
  983. nd_config->n_channels,
  984. GFP_KERNEL);
  985. if (mvm->nd_channels)
  986. mvm->n_nd_channels = nd_config->n_channels;
  987. return 0;
  988. }
  989. static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
  990. {
  991. kfree(mvm->nd_match_sets);
  992. mvm->nd_match_sets = NULL;
  993. mvm->n_nd_match_sets = 0;
  994. kfree(mvm->nd_channels);
  995. mvm->nd_channels = NULL;
  996. mvm->n_nd_channels = 0;
  997. }
  998. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  999. struct cfg80211_wowlan *wowlan,
  1000. bool test)
  1001. {
  1002. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1003. struct ieee80211_vif *vif = NULL;
  1004. struct iwl_mvm_vif *mvmvif = NULL;
  1005. struct ieee80211_sta *ap_sta = NULL;
  1006. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  1007. /*
  1008. * Program the minimum sleep time to 10 seconds, as many
  1009. * platforms have issues processing a wakeup signal while
  1010. * still being in the process of suspending.
  1011. */
  1012. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  1013. };
  1014. struct iwl_host_cmd d3_cfg_cmd = {
  1015. .id = D3_CONFIG_CMD,
  1016. .flags = CMD_WANT_SKB,
  1017. .data[0] = &d3_cfg_cmd_data,
  1018. .len[0] = sizeof(d3_cfg_cmd_data),
  1019. };
  1020. int ret;
  1021. int len __maybe_unused;
  1022. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1023. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1024. if (!wowlan) {
  1025. /*
  1026. * mac80211 shouldn't get here, but for D3 test
  1027. * it doesn't warrant a warning
  1028. */
  1029. WARN_ON(!test);
  1030. return -EINVAL;
  1031. }
  1032. mutex_lock(&mvm->mutex);
  1033. vif = iwl_mvm_get_bss_vif(mvm);
  1034. if (IS_ERR_OR_NULL(vif)) {
  1035. ret = 1;
  1036. goto out_noreset;
  1037. }
  1038. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1039. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
  1040. /* if we're not associated, this must be netdetect */
  1041. if (!wowlan->nd_config) {
  1042. ret = 1;
  1043. goto out_noreset;
  1044. }
  1045. ret = iwl_mvm_netdetect_config(
  1046. mvm, wowlan, wowlan->nd_config, vif);
  1047. if (ret)
  1048. goto out;
  1049. mvm->net_detect = true;
  1050. } else {
  1051. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  1052. ap_sta = rcu_dereference_protected(
  1053. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  1054. lockdep_is_held(&mvm->mutex));
  1055. if (IS_ERR_OR_NULL(ap_sta)) {
  1056. ret = -EINVAL;
  1057. goto out_noreset;
  1058. }
  1059. ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1060. vif, mvmvif, ap_sta);
  1061. if (ret)
  1062. goto out_noreset;
  1063. ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1064. vif, mvmvif, ap_sta);
  1065. if (ret)
  1066. goto out;
  1067. mvm->net_detect = false;
  1068. }
  1069. ret = iwl_mvm_power_update_device(mvm);
  1070. if (ret)
  1071. goto out;
  1072. ret = iwl_mvm_power_update_mac(mvm);
  1073. if (ret)
  1074. goto out;
  1075. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1076. if (mvm->d3_wake_sysassert)
  1077. d3_cfg_cmd_data.wakeup_flags |=
  1078. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  1079. #endif
  1080. /* must be last -- this switches firmware state */
  1081. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  1082. if (ret)
  1083. goto out;
  1084. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1085. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  1086. if (len >= sizeof(u32)) {
  1087. mvm->d3_test_pme_ptr =
  1088. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  1089. }
  1090. #endif
  1091. iwl_free_resp(&d3_cfg_cmd);
  1092. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1093. iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
  1094. out:
  1095. if (ret < 0) {
  1096. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1097. ieee80211_restart_hw(mvm->hw);
  1098. iwl_mvm_free_nd(mvm);
  1099. }
  1100. out_noreset:
  1101. mutex_unlock(&mvm->mutex);
  1102. return ret;
  1103. }
  1104. static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
  1105. {
  1106. struct iwl_notification_wait wait_d3;
  1107. static const u16 d3_notif[] = { D3_CONFIG_CMD };
  1108. int ret;
  1109. iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
  1110. d3_notif, ARRAY_SIZE(d3_notif),
  1111. NULL, NULL);
  1112. ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
  1113. if (ret)
  1114. goto remove_notif;
  1115. ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
  1116. WARN_ON_ONCE(ret);
  1117. return ret;
  1118. remove_notif:
  1119. iwl_remove_notification(&mvm->notif_wait, &wait_d3);
  1120. return ret;
  1121. }
  1122. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1123. {
  1124. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1125. struct iwl_trans *trans = mvm->trans;
  1126. int ret;
  1127. /* make sure the d0i3 exit work is not pending */
  1128. flush_work(&mvm->d0i3_exit_work);
  1129. ret = iwl_trans_suspend(trans);
  1130. if (ret)
  1131. return ret;
  1132. if (wowlan->any) {
  1133. trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  1134. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1135. ret = iwl_mvm_enter_d0i3_sync(mvm);
  1136. if (ret)
  1137. return ret;
  1138. }
  1139. mutex_lock(&mvm->d0i3_suspend_mutex);
  1140. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1141. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1142. iwl_trans_d3_suspend(trans, false, false);
  1143. return 0;
  1144. }
  1145. trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1146. return __iwl_mvm_suspend(hw, wowlan, false);
  1147. }
  1148. /* converted data from the different status responses */
  1149. struct iwl_wowlan_status_data {
  1150. u16 pattern_number;
  1151. u16 qos_seq_ctr[8];
  1152. u32 wakeup_reasons;
  1153. u32 wake_packet_length;
  1154. u32 wake_packet_bufsize;
  1155. const u8 *wake_packet;
  1156. };
  1157. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1158. struct ieee80211_vif *vif,
  1159. struct iwl_wowlan_status_data *status)
  1160. {
  1161. struct sk_buff *pkt = NULL;
  1162. struct cfg80211_wowlan_wakeup wakeup = {
  1163. .pattern_idx = -1,
  1164. };
  1165. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1166. u32 reasons = status->wakeup_reasons;
  1167. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1168. wakeup_report = NULL;
  1169. goto report;
  1170. }
  1171. pm_wakeup_event(mvm->dev, 0);
  1172. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1173. wakeup.magic_pkt = true;
  1174. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1175. wakeup.pattern_idx =
  1176. status->pattern_number;
  1177. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1178. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1179. wakeup.disconnect = true;
  1180. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1181. wakeup.gtk_rekey_failure = true;
  1182. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1183. wakeup.rfkill_release = true;
  1184. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1185. wakeup.eap_identity_req = true;
  1186. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1187. wakeup.four_way_handshake = true;
  1188. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1189. wakeup.tcp_connlost = true;
  1190. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1191. wakeup.tcp_nomoretokens = true;
  1192. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1193. wakeup.tcp_match = true;
  1194. if (status->wake_packet_bufsize) {
  1195. int pktsize = status->wake_packet_bufsize;
  1196. int pktlen = status->wake_packet_length;
  1197. const u8 *pktdata = status->wake_packet;
  1198. struct ieee80211_hdr *hdr = (void *)pktdata;
  1199. int truncated = pktlen - pktsize;
  1200. /* this would be a firmware bug */
  1201. if (WARN_ON_ONCE(truncated < 0))
  1202. truncated = 0;
  1203. if (ieee80211_is_data(hdr->frame_control)) {
  1204. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1205. int ivlen = 0, icvlen = 4; /* also FCS */
  1206. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1207. if (!pkt)
  1208. goto report;
  1209. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1210. pktdata += hdrlen;
  1211. pktsize -= hdrlen;
  1212. if (ieee80211_has_protected(hdr->frame_control)) {
  1213. /*
  1214. * This is unlocked and using gtk_i(c)vlen,
  1215. * but since everything is under RTNL still
  1216. * that's not really a problem - changing
  1217. * it would be difficult.
  1218. */
  1219. if (is_multicast_ether_addr(hdr->addr1)) {
  1220. ivlen = mvm->gtk_ivlen;
  1221. icvlen += mvm->gtk_icvlen;
  1222. } else {
  1223. ivlen = mvm->ptk_ivlen;
  1224. icvlen += mvm->ptk_icvlen;
  1225. }
  1226. }
  1227. /* if truncated, FCS/ICV is (partially) gone */
  1228. if (truncated >= icvlen) {
  1229. icvlen = 0;
  1230. truncated -= icvlen;
  1231. } else {
  1232. icvlen -= truncated;
  1233. truncated = 0;
  1234. }
  1235. pktsize -= ivlen + icvlen;
  1236. pktdata += ivlen;
  1237. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1238. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1239. goto report;
  1240. wakeup.packet = pkt->data;
  1241. wakeup.packet_present_len = pkt->len;
  1242. wakeup.packet_len = pkt->len - truncated;
  1243. wakeup.packet_80211 = false;
  1244. } else {
  1245. int fcslen = 4;
  1246. if (truncated >= 4) {
  1247. truncated -= 4;
  1248. fcslen = 0;
  1249. } else {
  1250. fcslen -= truncated;
  1251. truncated = 0;
  1252. }
  1253. pktsize -= fcslen;
  1254. wakeup.packet = status->wake_packet;
  1255. wakeup.packet_present_len = pktsize;
  1256. wakeup.packet_len = pktlen - truncated;
  1257. wakeup.packet_80211 = true;
  1258. }
  1259. }
  1260. report:
  1261. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1262. kfree_skb(pkt);
  1263. }
  1264. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1265. struct ieee80211_key_seq *seq)
  1266. {
  1267. u64 pn;
  1268. pn = le64_to_cpu(sc->pn);
  1269. seq->ccmp.pn[0] = pn >> 40;
  1270. seq->ccmp.pn[1] = pn >> 32;
  1271. seq->ccmp.pn[2] = pn >> 24;
  1272. seq->ccmp.pn[3] = pn >> 16;
  1273. seq->ccmp.pn[4] = pn >> 8;
  1274. seq->ccmp.pn[5] = pn;
  1275. }
  1276. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1277. struct ieee80211_key_seq *seq)
  1278. {
  1279. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1280. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1281. }
  1282. static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
  1283. struct ieee80211_sta *sta,
  1284. struct ieee80211_key_conf *key)
  1285. {
  1286. int tid;
  1287. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1288. if (sta && iwl_mvm_has_new_rx_api(mvm)) {
  1289. struct iwl_mvm_sta *mvmsta;
  1290. struct iwl_mvm_key_pn *ptk_pn;
  1291. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1292. ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
  1293. lockdep_is_held(&mvm->mutex));
  1294. if (WARN_ON(!ptk_pn))
  1295. return;
  1296. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  1297. struct ieee80211_key_seq seq = {};
  1298. int i;
  1299. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1300. ieee80211_set_key_rx_seq(key, tid, &seq);
  1301. for (i = 1; i < mvm->trans->num_rx_queues; i++)
  1302. memcpy(ptk_pn->q[i].pn[tid],
  1303. seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1304. }
  1305. } else {
  1306. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1307. struct ieee80211_key_seq seq = {};
  1308. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1309. ieee80211_set_key_rx_seq(key, tid, &seq);
  1310. }
  1311. }
  1312. }
  1313. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1314. struct ieee80211_key_conf *key)
  1315. {
  1316. int tid;
  1317. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1318. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1319. struct ieee80211_key_seq seq = {};
  1320. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1321. ieee80211_set_key_rx_seq(key, tid, &seq);
  1322. }
  1323. }
  1324. static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
  1325. struct ieee80211_key_conf *key,
  1326. struct iwl_wowlan_status *status)
  1327. {
  1328. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1329. switch (key->cipher) {
  1330. case WLAN_CIPHER_SUITE_CCMP:
  1331. iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
  1332. break;
  1333. case WLAN_CIPHER_SUITE_TKIP:
  1334. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1335. break;
  1336. default:
  1337. WARN_ON(1);
  1338. }
  1339. }
  1340. struct iwl_mvm_d3_gtk_iter_data {
  1341. struct iwl_mvm *mvm;
  1342. struct iwl_wowlan_status *status;
  1343. void *last_gtk;
  1344. u32 cipher;
  1345. bool find_phase, unhandled_cipher;
  1346. int num_keys;
  1347. };
  1348. static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
  1349. struct ieee80211_vif *vif,
  1350. struct ieee80211_sta *sta,
  1351. struct ieee80211_key_conf *key,
  1352. void *_data)
  1353. {
  1354. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1355. if (data->unhandled_cipher)
  1356. return;
  1357. switch (key->cipher) {
  1358. case WLAN_CIPHER_SUITE_WEP40:
  1359. case WLAN_CIPHER_SUITE_WEP104:
  1360. /* ignore WEP completely, nothing to do */
  1361. return;
  1362. case WLAN_CIPHER_SUITE_CCMP:
  1363. case WLAN_CIPHER_SUITE_TKIP:
  1364. /* we support these */
  1365. break;
  1366. default:
  1367. /* everything else (even CMAC for MFP) - disconnect from AP */
  1368. data->unhandled_cipher = true;
  1369. return;
  1370. }
  1371. data->num_keys++;
  1372. /*
  1373. * pairwise key - update sequence counters only;
  1374. * note that this assumes no TDLS sessions are active
  1375. */
  1376. if (sta) {
  1377. struct ieee80211_key_seq seq = {};
  1378. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1379. if (data->find_phase)
  1380. return;
  1381. switch (key->cipher) {
  1382. case WLAN_CIPHER_SUITE_CCMP:
  1383. iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
  1384. sta, key);
  1385. atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
  1386. break;
  1387. case WLAN_CIPHER_SUITE_TKIP:
  1388. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1389. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1390. atomic64_set(&key->tx_pn,
  1391. (u64)seq.tkip.iv16 |
  1392. ((u64)seq.tkip.iv32 << 16));
  1393. break;
  1394. }
  1395. /* that's it for this key */
  1396. return;
  1397. }
  1398. if (data->find_phase) {
  1399. data->last_gtk = key;
  1400. data->cipher = key->cipher;
  1401. return;
  1402. }
  1403. if (data->status->num_of_gtk_rekeys)
  1404. ieee80211_remove_key(key);
  1405. else if (data->last_gtk == key)
  1406. iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
  1407. }
  1408. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1409. struct ieee80211_vif *vif,
  1410. struct iwl_wowlan_status *status)
  1411. {
  1412. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1413. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1414. .mvm = mvm,
  1415. .status = status,
  1416. };
  1417. u32 disconnection_reasons =
  1418. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1419. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1420. if (!status || !vif->bss_conf.bssid)
  1421. return false;
  1422. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1423. return false;
  1424. /* find last GTK that we used initially, if any */
  1425. gtkdata.find_phase = true;
  1426. ieee80211_iter_keys(mvm->hw, vif,
  1427. iwl_mvm_d3_update_keys, &gtkdata);
  1428. /* not trying to keep connections with MFP/unhandled ciphers */
  1429. if (gtkdata.unhandled_cipher)
  1430. return false;
  1431. if (!gtkdata.num_keys)
  1432. goto out;
  1433. if (!gtkdata.last_gtk)
  1434. return false;
  1435. /*
  1436. * invalidate all other GTKs that might still exist and update
  1437. * the one that we used
  1438. */
  1439. gtkdata.find_phase = false;
  1440. ieee80211_iter_keys(mvm->hw, vif,
  1441. iwl_mvm_d3_update_keys, &gtkdata);
  1442. if (status->num_of_gtk_rekeys) {
  1443. struct ieee80211_key_conf *key;
  1444. struct {
  1445. struct ieee80211_key_conf conf;
  1446. u8 key[32];
  1447. } conf = {
  1448. .conf.cipher = gtkdata.cipher,
  1449. .conf.keyidx = status->gtk.key_index,
  1450. };
  1451. switch (gtkdata.cipher) {
  1452. case WLAN_CIPHER_SUITE_CCMP:
  1453. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1454. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1455. WLAN_KEY_LEN_CCMP);
  1456. break;
  1457. case WLAN_CIPHER_SUITE_TKIP:
  1458. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1459. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1460. /* leave TX MIC key zeroed, we don't use it anyway */
  1461. memcpy(conf.conf.key +
  1462. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1463. status->gtk.tkip_mic_key, 8);
  1464. break;
  1465. }
  1466. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1467. if (IS_ERR(key))
  1468. return false;
  1469. iwl_mvm_set_key_rx_seq(mvm, key, status);
  1470. }
  1471. if (status->num_of_gtk_rekeys) {
  1472. __be64 replay_ctr =
  1473. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1474. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1475. (void *)&replay_ctr, GFP_KERNEL);
  1476. }
  1477. out:
  1478. mvmvif->seqno_valid = true;
  1479. /* +0x10 because the set API expects next-to-use, not last-used */
  1480. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1481. return true;
  1482. }
  1483. static struct iwl_wowlan_status *
  1484. iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1485. {
  1486. u32 base = mvm->error_event_table;
  1487. struct error_table_start {
  1488. /* cf. struct iwl_error_event_table */
  1489. u32 valid;
  1490. u32 error_id;
  1491. } err_info;
  1492. struct iwl_host_cmd cmd = {
  1493. .id = WOWLAN_GET_STATUSES,
  1494. .flags = CMD_WANT_SKB,
  1495. };
  1496. struct iwl_wowlan_status *status, *fw_status;
  1497. int ret, len, status_size;
  1498. iwl_trans_read_mem_bytes(mvm->trans, base,
  1499. &err_info, sizeof(err_info));
  1500. if (err_info.valid) {
  1501. IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
  1502. err_info.valid, err_info.error_id);
  1503. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1504. struct cfg80211_wowlan_wakeup wakeup = {
  1505. .rfkill_release = true,
  1506. };
  1507. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1508. GFP_KERNEL);
  1509. }
  1510. return ERR_PTR(-EIO);
  1511. }
  1512. /* only for tracing for now */
  1513. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1514. if (ret)
  1515. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1516. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1517. if (ret) {
  1518. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1519. return ERR_PTR(ret);
  1520. }
  1521. /* RF-kill already asserted again... */
  1522. if (!cmd.resp_pkt) {
  1523. fw_status = ERR_PTR(-ERFKILL);
  1524. goto out_free_resp;
  1525. }
  1526. status_size = sizeof(*fw_status);
  1527. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1528. if (len < status_size) {
  1529. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1530. fw_status = ERR_PTR(-EIO);
  1531. goto out_free_resp;
  1532. }
  1533. status = (void *)cmd.resp_pkt->data;
  1534. if (len != (status_size +
  1535. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
  1536. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1537. fw_status = ERR_PTR(-EIO);
  1538. goto out_free_resp;
  1539. }
  1540. fw_status = kmemdup(status, len, GFP_KERNEL);
  1541. out_free_resp:
  1542. iwl_free_resp(&cmd);
  1543. return fw_status;
  1544. }
  1545. /* releases the MVM mutex */
  1546. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1547. struct ieee80211_vif *vif)
  1548. {
  1549. struct iwl_wowlan_status_data status;
  1550. struct iwl_wowlan_status *fw_status;
  1551. int i;
  1552. bool keep;
  1553. struct ieee80211_sta *ap_sta;
  1554. struct iwl_mvm_sta *mvm_ap_sta;
  1555. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1556. if (IS_ERR_OR_NULL(fw_status))
  1557. goto out_unlock;
  1558. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1559. for (i = 0; i < 8; i++)
  1560. status.qos_seq_ctr[i] =
  1561. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1562. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1563. status.wake_packet_length =
  1564. le32_to_cpu(fw_status->wake_packet_length);
  1565. status.wake_packet_bufsize =
  1566. le32_to_cpu(fw_status->wake_packet_bufsize);
  1567. status.wake_packet = fw_status->wake_packet;
  1568. /* still at hard-coded place 0 for D3 image */
  1569. ap_sta = rcu_dereference_protected(
  1570. mvm->fw_id_to_mac_id[0],
  1571. lockdep_is_held(&mvm->mutex));
  1572. if (IS_ERR_OR_NULL(ap_sta))
  1573. goto out_free;
  1574. mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  1575. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1576. u16 seq = status.qos_seq_ctr[i];
  1577. /* firmware stores last-used value, we store next value */
  1578. seq += 0x10;
  1579. mvm_ap_sta->tid_data[i].seq_number = seq;
  1580. }
  1581. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1582. mutex_unlock(&mvm->mutex);
  1583. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1584. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1585. kfree(fw_status);
  1586. return keep;
  1587. out_free:
  1588. kfree(fw_status);
  1589. out_unlock:
  1590. mutex_unlock(&mvm->mutex);
  1591. return false;
  1592. }
  1593. void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
  1594. struct ieee80211_vif *vif,
  1595. struct iwl_wowlan_status *status)
  1596. {
  1597. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1598. .mvm = mvm,
  1599. .status = status,
  1600. };
  1601. /*
  1602. * rekey handling requires taking locks that can't be taken now.
  1603. * however, d0i3 doesn't offload rekey, so we're fine.
  1604. */
  1605. if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
  1606. return;
  1607. /* find last GTK that we used initially, if any */
  1608. gtkdata.find_phase = true;
  1609. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1610. gtkdata.find_phase = false;
  1611. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1612. }
  1613. struct iwl_mvm_nd_query_results {
  1614. u32 matched_profiles;
  1615. struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
  1616. };
  1617. static int
  1618. iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
  1619. struct iwl_mvm_nd_query_results *results)
  1620. {
  1621. struct iwl_scan_offload_profiles_query *query;
  1622. struct iwl_host_cmd cmd = {
  1623. .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
  1624. .flags = CMD_WANT_SKB,
  1625. };
  1626. int ret, len;
  1627. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1628. if (ret) {
  1629. IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
  1630. return ret;
  1631. }
  1632. /* RF-kill already asserted again... */
  1633. if (!cmd.resp_pkt) {
  1634. ret = -ERFKILL;
  1635. goto out_free_resp;
  1636. }
  1637. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1638. if (len < sizeof(*query)) {
  1639. IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
  1640. ret = -EIO;
  1641. goto out_free_resp;
  1642. }
  1643. query = (void *)cmd.resp_pkt->data;
  1644. results->matched_profiles = le32_to_cpu(query->matched_profiles);
  1645. memcpy(results->matches, query->matches, sizeof(results->matches));
  1646. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1647. mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
  1648. #endif
  1649. out_free_resp:
  1650. iwl_free_resp(&cmd);
  1651. return ret;
  1652. }
  1653. static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
  1654. struct ieee80211_vif *vif)
  1655. {
  1656. struct cfg80211_wowlan_nd_info *net_detect = NULL;
  1657. struct cfg80211_wowlan_wakeup wakeup = {
  1658. .pattern_idx = -1,
  1659. };
  1660. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1661. struct iwl_mvm_nd_query_results query;
  1662. struct iwl_wowlan_status *fw_status;
  1663. unsigned long matched_profiles;
  1664. u32 reasons = 0;
  1665. int i, j, n_matches, ret;
  1666. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1667. if (!IS_ERR_OR_NULL(fw_status)) {
  1668. reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1669. kfree(fw_status);
  1670. }
  1671. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1672. wakeup.rfkill_release = true;
  1673. if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
  1674. goto out;
  1675. ret = iwl_mvm_netdetect_query_results(mvm, &query);
  1676. if (ret || !query.matched_profiles) {
  1677. wakeup_report = NULL;
  1678. goto out;
  1679. }
  1680. matched_profiles = query.matched_profiles;
  1681. if (mvm->n_nd_match_sets) {
  1682. n_matches = hweight_long(matched_profiles);
  1683. } else {
  1684. IWL_ERR(mvm, "no net detect match information available\n");
  1685. n_matches = 0;
  1686. }
  1687. net_detect = kzalloc(sizeof(*net_detect) +
  1688. (n_matches * sizeof(net_detect->matches[0])),
  1689. GFP_KERNEL);
  1690. if (!net_detect || !n_matches)
  1691. goto out_report_nd;
  1692. for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
  1693. struct iwl_scan_offload_profile_match *fw_match;
  1694. struct cfg80211_wowlan_nd_match *match;
  1695. int idx, n_channels = 0;
  1696. fw_match = &query.matches[i];
  1697. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
  1698. n_channels += hweight8(fw_match->matching_channels[j]);
  1699. match = kzalloc(sizeof(*match) +
  1700. (n_channels * sizeof(*match->channels)),
  1701. GFP_KERNEL);
  1702. if (!match)
  1703. goto out_report_nd;
  1704. net_detect->matches[net_detect->n_matches++] = match;
  1705. /* We inverted the order of the SSIDs in the scan
  1706. * request, so invert the index here.
  1707. */
  1708. idx = mvm->n_nd_match_sets - i - 1;
  1709. match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
  1710. memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
  1711. match->ssid.ssid_len);
  1712. if (mvm->n_nd_channels < n_channels)
  1713. continue;
  1714. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
  1715. if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
  1716. match->channels[match->n_channels++] =
  1717. mvm->nd_channels[j]->center_freq;
  1718. }
  1719. out_report_nd:
  1720. wakeup.net_detect = net_detect;
  1721. out:
  1722. iwl_mvm_free_nd(mvm);
  1723. mutex_unlock(&mvm->mutex);
  1724. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1725. if (net_detect) {
  1726. for (i = 0; i < net_detect->n_matches; i++)
  1727. kfree(net_detect->matches[i]);
  1728. kfree(net_detect);
  1729. }
  1730. }
  1731. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1732. {
  1733. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1734. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1735. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1736. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1737. if (!mvm->store_d3_resume_sram)
  1738. return;
  1739. if (!mvm->d3_resume_sram) {
  1740. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1741. if (!mvm->d3_resume_sram)
  1742. return;
  1743. }
  1744. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1745. #endif
  1746. }
  1747. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1748. struct ieee80211_vif *vif)
  1749. {
  1750. /* skip the one we keep connection on */
  1751. if (data == vif)
  1752. return;
  1753. if (vif->type == NL80211_IFTYPE_STATION)
  1754. ieee80211_resume_disconnect(vif);
  1755. }
  1756. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1757. {
  1758. struct ieee80211_vif *vif = NULL;
  1759. int ret = 1;
  1760. enum iwl_d3_status d3_status;
  1761. bool keep = false;
  1762. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1763. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1764. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
  1765. CMD_WAKE_UP_TRANS;
  1766. mutex_lock(&mvm->mutex);
  1767. /* get the BSS vif pointer again */
  1768. vif = iwl_mvm_get_bss_vif(mvm);
  1769. if (IS_ERR_OR_NULL(vif))
  1770. goto err;
  1771. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
  1772. if (ret)
  1773. goto err;
  1774. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1775. IWL_INFO(mvm, "Device was reset during suspend\n");
  1776. goto err;
  1777. }
  1778. /* query SRAM first in case we want event logging */
  1779. iwl_mvm_read_d3_sram(mvm);
  1780. /*
  1781. * Query the current location and source from the D3 firmware so we
  1782. * can play it back when we re-intiailize the D0 firmware
  1783. */
  1784. iwl_mvm_update_changed_regdom(mvm);
  1785. if (mvm->net_detect) {
  1786. iwl_mvm_query_netdetect_reasons(mvm, vif);
  1787. /* has unlocked the mutex, so skip that */
  1788. goto out;
  1789. } else {
  1790. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1791. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1792. if (keep)
  1793. mvm->keep_vif = vif;
  1794. #endif
  1795. /* has unlocked the mutex, so skip that */
  1796. goto out_iterate;
  1797. }
  1798. err:
  1799. iwl_mvm_free_nd(mvm);
  1800. mutex_unlock(&mvm->mutex);
  1801. out_iterate:
  1802. if (!test)
  1803. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1804. IEEE80211_IFACE_ITER_NORMAL,
  1805. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1806. out:
  1807. if (unified_image && !ret) {
  1808. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
  1809. if (!ret) /* D3 ended successfully - no need to reset device */
  1810. return 0;
  1811. }
  1812. /*
  1813. * Reconfigure the device in one of the following cases:
  1814. * 1. We are not using a unified image
  1815. * 2. We are using a unified image but had an error while exiting D3
  1816. */
  1817. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1818. set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
  1819. /*
  1820. * When switching images we return 1, which causes mac80211
  1821. * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
  1822. * This type of reconfig calls iwl_mvm_restart_complete(),
  1823. * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
  1824. * to take the reference here.
  1825. */
  1826. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1827. return 1;
  1828. }
  1829. static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
  1830. {
  1831. iwl_trans_resume(mvm->trans);
  1832. return __iwl_mvm_resume(mvm, false);
  1833. }
  1834. static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
  1835. {
  1836. bool exit_now;
  1837. enum iwl_d3_status d3_status;
  1838. struct iwl_trans *trans = mvm->trans;
  1839. iwl_trans_d3_resume(trans, &d3_status, false, false);
  1840. /*
  1841. * make sure to clear D0I3_DEFER_WAKEUP before
  1842. * calling iwl_trans_resume(), which might wait
  1843. * for d0i3 exit completion.
  1844. */
  1845. mutex_lock(&mvm->d0i3_suspend_mutex);
  1846. __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1847. exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
  1848. &mvm->d0i3_suspend_flags);
  1849. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1850. if (exit_now) {
  1851. IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
  1852. _iwl_mvm_exit_d0i3(mvm);
  1853. }
  1854. iwl_trans_resume(trans);
  1855. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1856. int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
  1857. if (ret)
  1858. return ret;
  1859. /*
  1860. * d0i3 exit will be deferred until reconfig_complete.
  1861. * make sure there we are out of d0i3.
  1862. */
  1863. }
  1864. return 0;
  1865. }
  1866. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1867. {
  1868. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1869. int ret;
  1870. if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  1871. ret = iwl_mvm_resume_d0i3(mvm);
  1872. else
  1873. ret = iwl_mvm_resume_d3(mvm);
  1874. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1875. return ret;
  1876. }
  1877. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1878. {
  1879. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1880. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1881. }
  1882. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1883. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1884. {
  1885. struct iwl_mvm *mvm = inode->i_private;
  1886. int err;
  1887. if (mvm->d3_test_active)
  1888. return -EBUSY;
  1889. file->private_data = inode->i_private;
  1890. ieee80211_stop_queues(mvm->hw);
  1891. synchronize_net();
  1892. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1893. /* start pseudo D3 */
  1894. rtnl_lock();
  1895. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1896. rtnl_unlock();
  1897. if (err > 0)
  1898. err = -EINVAL;
  1899. if (err) {
  1900. ieee80211_wake_queues(mvm->hw);
  1901. return err;
  1902. }
  1903. mvm->d3_test_active = true;
  1904. mvm->keep_vif = NULL;
  1905. return 0;
  1906. }
  1907. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1908. size_t count, loff_t *ppos)
  1909. {
  1910. struct iwl_mvm *mvm = file->private_data;
  1911. u32 pme_asserted;
  1912. while (true) {
  1913. /* read pme_ptr if available */
  1914. if (mvm->d3_test_pme_ptr) {
  1915. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1916. mvm->d3_test_pme_ptr);
  1917. if (pme_asserted)
  1918. break;
  1919. }
  1920. if (msleep_interruptible(100))
  1921. break;
  1922. }
  1923. return 0;
  1924. }
  1925. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1926. struct ieee80211_vif *vif)
  1927. {
  1928. /* skip the one we keep connection on */
  1929. if (_data == vif)
  1930. return;
  1931. if (vif->type == NL80211_IFTYPE_STATION)
  1932. ieee80211_connection_loss(vif);
  1933. }
  1934. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1935. {
  1936. struct iwl_mvm *mvm = inode->i_private;
  1937. int remaining_time = 10;
  1938. mvm->d3_test_active = false;
  1939. rtnl_lock();
  1940. __iwl_mvm_resume(mvm, true);
  1941. rtnl_unlock();
  1942. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1943. iwl_abort_notification_waits(&mvm->notif_wait);
  1944. ieee80211_restart_hw(mvm->hw);
  1945. /* wait for restart and disconnect all interfaces */
  1946. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1947. remaining_time > 0) {
  1948. remaining_time--;
  1949. msleep(1000);
  1950. }
  1951. if (remaining_time == 0)
  1952. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1953. ieee80211_iterate_active_interfaces_atomic(
  1954. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1955. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1956. ieee80211_wake_queues(mvm->hw);
  1957. return 0;
  1958. }
  1959. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1960. .llseek = no_llseek,
  1961. .open = iwl_mvm_d3_test_open,
  1962. .read = iwl_mvm_d3_test_read,
  1963. .release = iwl_mvm_d3_test_release,
  1964. };
  1965. #endif