d3.c 56 KB

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