d3.c 56 KB

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