d3.c 51 KB

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