d3.c 54 KB

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