d3.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  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. enum iwl_mvm_tcp_packet_type {
  380. MVM_TCP_TX_SYN,
  381. MVM_TCP_RX_SYNACK,
  382. MVM_TCP_TX_DATA,
  383. MVM_TCP_RX_ACK,
  384. MVM_TCP_RX_WAKE,
  385. MVM_TCP_TX_FIN,
  386. };
  387. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  388. {
  389. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  390. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  391. }
  392. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  393. struct cfg80211_wowlan_tcp *tcp,
  394. void *_pkt, u8 *mask,
  395. __le16 *pseudo_hdr_csum,
  396. enum iwl_mvm_tcp_packet_type ptype)
  397. {
  398. struct {
  399. struct ethhdr eth;
  400. struct iphdr ip;
  401. struct tcphdr tcp;
  402. u8 data[];
  403. } __packed *pkt = _pkt;
  404. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  405. int i;
  406. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  407. pkt->ip.version = 4;
  408. pkt->ip.ihl = 5;
  409. pkt->ip.protocol = IPPROTO_TCP;
  410. switch (ptype) {
  411. case MVM_TCP_TX_SYN:
  412. case MVM_TCP_TX_DATA:
  413. case MVM_TCP_TX_FIN:
  414. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  415. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  416. pkt->ip.ttl = 128;
  417. pkt->ip.saddr = tcp->src;
  418. pkt->ip.daddr = tcp->dst;
  419. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  420. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  421. /* overwritten for TX SYN later */
  422. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  423. pkt->tcp.window = cpu_to_be16(65000);
  424. break;
  425. case MVM_TCP_RX_SYNACK:
  426. case MVM_TCP_RX_ACK:
  427. case MVM_TCP_RX_WAKE:
  428. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  429. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  430. pkt->ip.saddr = tcp->dst;
  431. pkt->ip.daddr = tcp->src;
  432. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  433. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  434. break;
  435. default:
  436. WARN_ON(1);
  437. return;
  438. }
  439. switch (ptype) {
  440. case MVM_TCP_TX_SYN:
  441. /* firmware assumes 8 option bytes - 8 NOPs for now */
  442. memset(pkt->data, 0x01, 8);
  443. ip_tot_len += 8;
  444. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  445. pkt->tcp.syn = 1;
  446. break;
  447. case MVM_TCP_TX_DATA:
  448. ip_tot_len += tcp->payload_len;
  449. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  450. pkt->tcp.psh = 1;
  451. pkt->tcp.ack = 1;
  452. break;
  453. case MVM_TCP_TX_FIN:
  454. pkt->tcp.fin = 1;
  455. pkt->tcp.ack = 1;
  456. break;
  457. case MVM_TCP_RX_SYNACK:
  458. pkt->tcp.syn = 1;
  459. pkt->tcp.ack = 1;
  460. break;
  461. case MVM_TCP_RX_ACK:
  462. pkt->tcp.ack = 1;
  463. break;
  464. case MVM_TCP_RX_WAKE:
  465. ip_tot_len += tcp->wake_len;
  466. pkt->tcp.psh = 1;
  467. pkt->tcp.ack = 1;
  468. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  469. break;
  470. }
  471. switch (ptype) {
  472. case MVM_TCP_TX_SYN:
  473. case MVM_TCP_TX_DATA:
  474. case MVM_TCP_TX_FIN:
  475. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  476. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  477. break;
  478. case MVM_TCP_RX_WAKE:
  479. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  480. u8 tmp = tcp->wake_mask[i];
  481. mask[i + 6] |= tmp << 6;
  482. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  483. mask[i + 7] = tmp >> 2;
  484. }
  485. /* fall through for ethernet/IP/TCP headers mask */
  486. case MVM_TCP_RX_SYNACK:
  487. case MVM_TCP_RX_ACK:
  488. mask[0] = 0xff; /* match ethernet */
  489. /*
  490. * match ethernet, ip.version, ip.ihl
  491. * the ip.ihl half byte is really masked out by firmware
  492. */
  493. mask[1] = 0x7f;
  494. mask[2] = 0x80; /* match ip.protocol */
  495. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  496. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  497. mask[5] = 0x80; /* match tcp flags */
  498. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  499. break;
  500. };
  501. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  502. pkt->ip.saddr, pkt->ip.daddr);
  503. }
  504. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  505. struct ieee80211_vif *vif,
  506. struct cfg80211_wowlan_tcp *tcp)
  507. {
  508. struct iwl_wowlan_remote_wake_config *cfg;
  509. struct iwl_host_cmd cmd = {
  510. .id = REMOTE_WAKE_CONFIG_CMD,
  511. .len = { sizeof(*cfg), },
  512. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  513. };
  514. int ret;
  515. if (!tcp)
  516. return 0;
  517. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  518. if (!cfg)
  519. return -ENOMEM;
  520. cmd.data[0] = cfg;
  521. cfg->max_syn_retries = 10;
  522. cfg->max_data_retries = 10;
  523. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  524. cfg->tcp_ack_timeout = 1; /* seconds */
  525. /* SYN (TX) */
  526. iwl_mvm_build_tcp_packet(
  527. vif, tcp, cfg->syn_tx.data, NULL,
  528. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  529. MVM_TCP_TX_SYN);
  530. cfg->syn_tx.info.tcp_payload_length = 0;
  531. /* SYN/ACK (RX) */
  532. iwl_mvm_build_tcp_packet(
  533. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  534. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  535. MVM_TCP_RX_SYNACK);
  536. cfg->synack_rx.info.tcp_payload_length = 0;
  537. /* KEEPALIVE/ACK (TX) */
  538. iwl_mvm_build_tcp_packet(
  539. vif, tcp, cfg->keepalive_tx.data, NULL,
  540. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  541. MVM_TCP_TX_DATA);
  542. cfg->keepalive_tx.info.tcp_payload_length =
  543. cpu_to_le16(tcp->payload_len);
  544. cfg->sequence_number_offset = tcp->payload_seq.offset;
  545. /* length must be 0..4, the field is little endian */
  546. cfg->sequence_number_length = tcp->payload_seq.len;
  547. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  548. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  549. if (tcp->payload_tok.len) {
  550. cfg->token_offset = tcp->payload_tok.offset;
  551. cfg->token_length = tcp->payload_tok.len;
  552. cfg->num_tokens =
  553. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  554. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  555. tcp->tokens_size);
  556. } else {
  557. /* set tokens to max value to almost never run out */
  558. cfg->num_tokens = cpu_to_le16(65535);
  559. }
  560. /* ACK (RX) */
  561. iwl_mvm_build_tcp_packet(
  562. vif, tcp, cfg->keepalive_ack_rx.data,
  563. cfg->keepalive_ack_rx.rx_mask,
  564. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  565. MVM_TCP_RX_ACK);
  566. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  567. /* WAKEUP (RX) */
  568. iwl_mvm_build_tcp_packet(
  569. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  570. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  571. MVM_TCP_RX_WAKE);
  572. cfg->wake_rx.info.tcp_payload_length =
  573. cpu_to_le16(tcp->wake_len);
  574. /* FIN */
  575. iwl_mvm_build_tcp_packet(
  576. vif, tcp, cfg->fin_tx.data, NULL,
  577. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  578. MVM_TCP_TX_FIN);
  579. cfg->fin_tx.info.tcp_payload_length = 0;
  580. ret = iwl_mvm_send_cmd(mvm, &cmd);
  581. kfree(cfg);
  582. return ret;
  583. }
  584. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  585. struct ieee80211_sta *ap_sta)
  586. {
  587. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  588. struct ieee80211_chanctx_conf *ctx;
  589. u8 chains_static, chains_dynamic;
  590. struct cfg80211_chan_def chandef;
  591. int ret, i;
  592. struct iwl_binding_cmd binding_cmd = {};
  593. struct iwl_time_quota_cmd quota_cmd = {};
  594. struct iwl_time_quota_data *quota;
  595. u32 status;
  596. int size;
  597. if (fw_has_capa(&mvm->fw->ucode_capa,
  598. IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
  599. size = sizeof(binding_cmd);
  600. if (mvmvif->phy_ctxt->channel->band == NL80211_BAND_2GHZ ||
  601. !iwl_mvm_is_cdb_supported(mvm))
  602. binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX);
  603. else
  604. binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_5G_INDEX);
  605. } else {
  606. size = IWL_BINDING_CMD_SIZE_V1;
  607. }
  608. /* add back the PHY */
  609. if (WARN_ON(!mvmvif->phy_ctxt))
  610. return -EINVAL;
  611. rcu_read_lock();
  612. ctx = rcu_dereference(vif->chanctx_conf);
  613. if (WARN_ON(!ctx)) {
  614. rcu_read_unlock();
  615. return -EINVAL;
  616. }
  617. chandef = ctx->def;
  618. chains_static = ctx->rx_chains_static;
  619. chains_dynamic = ctx->rx_chains_dynamic;
  620. rcu_read_unlock();
  621. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  622. chains_static, chains_dynamic);
  623. if (ret)
  624. return ret;
  625. /* add back the MAC */
  626. mvmvif->uploaded = false;
  627. if (WARN_ON(!vif->bss_conf.assoc))
  628. return -EINVAL;
  629. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  630. if (ret)
  631. return ret;
  632. /* add back binding - XXX refactor? */
  633. binding_cmd.id_and_color =
  634. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  635. mvmvif->phy_ctxt->color));
  636. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  637. binding_cmd.phy =
  638. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  639. mvmvif->phy_ctxt->color));
  640. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  641. mvmvif->color));
  642. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  643. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  644. status = 0;
  645. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  646. size, &binding_cmd, &status);
  647. if (ret) {
  648. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  649. return ret;
  650. }
  651. if (status) {
  652. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  653. return -EIO;
  654. }
  655. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
  656. if (ret)
  657. return ret;
  658. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  659. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  660. if (ret)
  661. return ret;
  662. /* and some quota */
  663. quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
  664. quota->id_and_color =
  665. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  666. mvmvif->phy_ctxt->color));
  667. quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  668. quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
  669. for (i = 1; i < MAX_BINDINGS; i++) {
  670. quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
  671. quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  672. }
  673. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
  674. iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
  675. if (ret)
  676. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  677. if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
  678. IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
  679. return 0;
  680. }
  681. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  682. struct ieee80211_vif *vif)
  683. {
  684. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  685. struct iwl_nonqos_seq_query_cmd query_cmd = {
  686. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  687. .mac_id_n_color =
  688. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  689. mvmvif->color)),
  690. };
  691. struct iwl_host_cmd cmd = {
  692. .id = NON_QOS_TX_COUNTER_CMD,
  693. .flags = CMD_WANT_SKB,
  694. };
  695. int err;
  696. u32 size;
  697. cmd.data[0] = &query_cmd;
  698. cmd.len[0] = sizeof(query_cmd);
  699. err = iwl_mvm_send_cmd(mvm, &cmd);
  700. if (err)
  701. return err;
  702. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  703. if (size < sizeof(__le16)) {
  704. err = -EINVAL;
  705. } else {
  706. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  707. /* firmware returns next, not last-used seqno */
  708. err = (u16) (err - 0x10);
  709. }
  710. iwl_free_resp(&cmd);
  711. return err;
  712. }
  713. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  714. {
  715. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  716. struct iwl_nonqos_seq_query_cmd query_cmd = {
  717. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  718. .mac_id_n_color =
  719. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  720. mvmvif->color)),
  721. .value = cpu_to_le16(mvmvif->seqno),
  722. };
  723. /* return if called during restart, not resume from D3 */
  724. if (!mvmvif->seqno_valid)
  725. return;
  726. mvmvif->seqno_valid = false;
  727. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
  728. sizeof(query_cmd), &query_cmd))
  729. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  730. }
  731. static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
  732. {
  733. iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
  734. iwl_mvm_stop_device(mvm);
  735. /*
  736. * Set the HW restart bit -- this is mostly true as we're
  737. * going to load new firmware and reprogram that, though
  738. * the reprogramming is going to be manual to avoid adding
  739. * all the MACs that aren't support.
  740. * We don't have to clear up everything though because the
  741. * reprogramming is manual. When we resume, we'll actually
  742. * go through a proper restart sequence again to switch
  743. * back to the runtime firmware image.
  744. */
  745. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  746. /* the fw is reset, so all the keys are cleared */
  747. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  748. mvm->ptk_ivlen = 0;
  749. mvm->ptk_icvlen = 0;
  750. mvm->ptk_ivlen = 0;
  751. mvm->ptk_icvlen = 0;
  752. return iwl_mvm_load_d3_fw(mvm);
  753. }
  754. static int
  755. iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
  756. struct cfg80211_wowlan *wowlan,
  757. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  758. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  759. struct ieee80211_sta *ap_sta)
  760. {
  761. int ret;
  762. struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  763. /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
  764. wowlan_config_cmd->is_11n_connection =
  765. ap_sta->ht_cap.ht_supported;
  766. wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
  767. ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
  768. /* Query the last used seqno and set it */
  769. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  770. if (ret < 0)
  771. return ret;
  772. wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
  773. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
  774. if (wowlan->disconnect)
  775. wowlan_config_cmd->wakeup_filter |=
  776. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  777. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  778. if (wowlan->magic_pkt)
  779. wowlan_config_cmd->wakeup_filter |=
  780. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  781. if (wowlan->gtk_rekey_failure)
  782. wowlan_config_cmd->wakeup_filter |=
  783. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  784. if (wowlan->eap_identity_req)
  785. wowlan_config_cmd->wakeup_filter |=
  786. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  787. if (wowlan->four_way_handshake)
  788. wowlan_config_cmd->wakeup_filter |=
  789. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  790. if (wowlan->n_patterns)
  791. wowlan_config_cmd->wakeup_filter |=
  792. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  793. if (wowlan->rfkill_release)
  794. wowlan_config_cmd->wakeup_filter |=
  795. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  796. if (wowlan->tcp) {
  797. /*
  798. * Set the "link change" (really "link lost") flag as well
  799. * since that implies losing the TCP connection.
  800. */
  801. wowlan_config_cmd->wakeup_filter |=
  802. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  803. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  804. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  805. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  806. }
  807. return 0;
  808. }
  809. static void
  810. iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
  811. struct ieee80211_vif *vif,
  812. void (*iter)(struct ieee80211_hw *hw,
  813. struct ieee80211_vif *vif,
  814. struct ieee80211_sta *sta,
  815. struct ieee80211_key_conf *key,
  816. void *data),
  817. void *data)
  818. {
  819. struct ieee80211_sta *ap_sta;
  820. rcu_read_lock();
  821. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
  822. if (IS_ERR_OR_NULL(ap_sta))
  823. goto out;
  824. ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
  825. out:
  826. rcu_read_unlock();
  827. }
  828. int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
  829. struct ieee80211_vif *vif,
  830. bool d0i3,
  831. u32 cmd_flags)
  832. {
  833. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  834. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  835. struct wowlan_key_data key_data = {
  836. .configure_keys = !d0i3,
  837. .use_rsc_tsc = false,
  838. .tkip = &tkip_cmd,
  839. .use_tkip = false,
  840. };
  841. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  842. int ret;
  843. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  844. if (!key_data.rsc_tsc)
  845. return -ENOMEM;
  846. /*
  847. * if we have to configure keys, call ieee80211_iter_keys(),
  848. * as we need non-atomic context in order to take the
  849. * required locks.
  850. * for the d0i3 we can't use ieee80211_iter_keys(), as
  851. * taking (almost) any mutex might result in deadlock.
  852. */
  853. if (!d0i3) {
  854. /*
  855. * Note that currently we don't propagate cmd_flags
  856. * to the iterator. In case of key_data.configure_keys,
  857. * all the configured commands are SYNC, and
  858. * iwl_mvm_wowlan_program_keys() will take care of
  859. * locking/unlocking mvm->mutex.
  860. */
  861. ieee80211_iter_keys(mvm->hw, vif,
  862. iwl_mvm_wowlan_program_keys,
  863. &key_data);
  864. } else {
  865. iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
  866. iwl_mvm_wowlan_program_keys,
  867. &key_data);
  868. }
  869. if (key_data.error) {
  870. ret = -EIO;
  871. goto out;
  872. }
  873. if (key_data.use_rsc_tsc) {
  874. ret = iwl_mvm_send_cmd_pdu(mvm,
  875. WOWLAN_TSC_RSC_PARAM, cmd_flags,
  876. sizeof(*key_data.rsc_tsc),
  877. key_data.rsc_tsc);
  878. if (ret)
  879. goto out;
  880. }
  881. if (key_data.use_tkip &&
  882. !fw_has_api(&mvm->fw->ucode_capa,
  883. IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
  884. ret = iwl_mvm_send_cmd_pdu(mvm,
  885. WOWLAN_TKIP_PARAM,
  886. cmd_flags, sizeof(tkip_cmd),
  887. &tkip_cmd);
  888. if (ret)
  889. goto out;
  890. }
  891. /* configure rekey data only if offloaded rekey is supported (d3) */
  892. if (mvmvif->rekey_data.valid && !d0i3) {
  893. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  894. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  895. NL80211_KCK_LEN);
  896. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  897. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  898. NL80211_KEK_LEN);
  899. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  900. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  901. ret = iwl_mvm_send_cmd_pdu(mvm,
  902. WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
  903. sizeof(kek_kck_cmd),
  904. &kek_kck_cmd);
  905. if (ret)
  906. goto out;
  907. }
  908. ret = 0;
  909. out:
  910. kfree(key_data.rsc_tsc);
  911. return ret;
  912. }
  913. static int
  914. iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
  915. struct cfg80211_wowlan *wowlan,
  916. struct iwl_wowlan_config_cmd *wowlan_config_cmd,
  917. struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
  918. struct ieee80211_sta *ap_sta)
  919. {
  920. int ret;
  921. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  922. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  923. if (!unified_image) {
  924. ret = iwl_mvm_switch_to_d3(mvm);
  925. if (ret)
  926. return ret;
  927. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  928. if (ret)
  929. return ret;
  930. }
  931. if (!iwlwifi_mod_params.swcrypto) {
  932. /*
  933. * This needs to be unlocked due to lock ordering
  934. * constraints. Since we're in the suspend path
  935. * that isn't really a problem though.
  936. */
  937. mutex_unlock(&mvm->mutex);
  938. ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
  939. CMD_ASYNC);
  940. mutex_lock(&mvm->mutex);
  941. if (ret)
  942. return ret;
  943. }
  944. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  945. sizeof(*wowlan_config_cmd),
  946. wowlan_config_cmd);
  947. if (ret)
  948. return ret;
  949. ret = iwl_mvm_send_patterns(mvm, wowlan);
  950. if (ret)
  951. return ret;
  952. ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
  953. if (ret)
  954. return ret;
  955. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  956. return ret;
  957. }
  958. static int
  959. iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
  960. struct cfg80211_wowlan *wowlan,
  961. struct cfg80211_sched_scan_request *nd_config,
  962. struct ieee80211_vif *vif)
  963. {
  964. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  965. int ret;
  966. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  967. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  968. if (!unified_image) {
  969. ret = iwl_mvm_switch_to_d3(mvm);
  970. if (ret)
  971. return ret;
  972. } else {
  973. /* In theory, we wouldn't have to stop a running sched
  974. * scan in order to start another one (for
  975. * net-detect). But in practice this doesn't seem to
  976. * work properly, so stop any running sched_scan now.
  977. */
  978. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
  979. if (ret)
  980. return ret;
  981. }
  982. /* rfkill release can be either for wowlan or netdetect */
  983. if (wowlan->rfkill_release)
  984. wowlan_config_cmd.wakeup_filter |=
  985. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  986. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
  987. sizeof(wowlan_config_cmd),
  988. &wowlan_config_cmd);
  989. if (ret)
  990. return ret;
  991. ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
  992. IWL_MVM_SCAN_NETDETECT);
  993. if (ret)
  994. return ret;
  995. if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
  996. return -EBUSY;
  997. /* save the sched scan matchsets... */
  998. if (nd_config->n_match_sets) {
  999. mvm->nd_match_sets = kmemdup(nd_config->match_sets,
  1000. sizeof(*nd_config->match_sets) *
  1001. nd_config->n_match_sets,
  1002. GFP_KERNEL);
  1003. if (mvm->nd_match_sets)
  1004. mvm->n_nd_match_sets = nd_config->n_match_sets;
  1005. }
  1006. /* ...and the sched scan channels for later reporting */
  1007. mvm->nd_channels = kmemdup(nd_config->channels,
  1008. sizeof(*nd_config->channels) *
  1009. nd_config->n_channels,
  1010. GFP_KERNEL);
  1011. if (mvm->nd_channels)
  1012. mvm->n_nd_channels = nd_config->n_channels;
  1013. return 0;
  1014. }
  1015. static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
  1016. {
  1017. kfree(mvm->nd_match_sets);
  1018. mvm->nd_match_sets = NULL;
  1019. mvm->n_nd_match_sets = 0;
  1020. kfree(mvm->nd_channels);
  1021. mvm->nd_channels = NULL;
  1022. mvm->n_nd_channels = 0;
  1023. }
  1024. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  1025. struct cfg80211_wowlan *wowlan,
  1026. bool test)
  1027. {
  1028. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1029. struct ieee80211_vif *vif = NULL;
  1030. struct iwl_mvm_vif *mvmvif = NULL;
  1031. struct ieee80211_sta *ap_sta = NULL;
  1032. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  1033. /*
  1034. * Program the minimum sleep time to 10 seconds, as many
  1035. * platforms have issues processing a wakeup signal while
  1036. * still being in the process of suspending.
  1037. */
  1038. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  1039. };
  1040. struct iwl_host_cmd d3_cfg_cmd = {
  1041. .id = D3_CONFIG_CMD,
  1042. .flags = CMD_WANT_SKB,
  1043. .data[0] = &d3_cfg_cmd_data,
  1044. .len[0] = sizeof(d3_cfg_cmd_data),
  1045. };
  1046. int ret;
  1047. int len __maybe_unused;
  1048. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1049. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1050. if (!wowlan) {
  1051. /*
  1052. * mac80211 shouldn't get here, but for D3 test
  1053. * it doesn't warrant a warning
  1054. */
  1055. WARN_ON(!test);
  1056. return -EINVAL;
  1057. }
  1058. mutex_lock(&mvm->mutex);
  1059. vif = iwl_mvm_get_bss_vif(mvm);
  1060. if (IS_ERR_OR_NULL(vif)) {
  1061. ret = 1;
  1062. goto out_noreset;
  1063. }
  1064. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1065. if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
  1066. /* if we're not associated, this must be netdetect */
  1067. if (!wowlan->nd_config) {
  1068. ret = 1;
  1069. goto out_noreset;
  1070. }
  1071. ret = iwl_mvm_netdetect_config(
  1072. mvm, wowlan, wowlan->nd_config, vif);
  1073. if (ret)
  1074. goto out;
  1075. mvm->net_detect = true;
  1076. } else {
  1077. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  1078. ap_sta = rcu_dereference_protected(
  1079. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  1080. lockdep_is_held(&mvm->mutex));
  1081. if (IS_ERR_OR_NULL(ap_sta)) {
  1082. ret = -EINVAL;
  1083. goto out_noreset;
  1084. }
  1085. ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1086. vif, mvmvif, ap_sta);
  1087. if (ret)
  1088. goto out_noreset;
  1089. ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
  1090. vif, mvmvif, ap_sta);
  1091. if (ret)
  1092. goto out;
  1093. mvm->net_detect = false;
  1094. }
  1095. ret = iwl_mvm_power_update_device(mvm);
  1096. if (ret)
  1097. goto out;
  1098. ret = iwl_mvm_power_update_mac(mvm);
  1099. if (ret)
  1100. goto out;
  1101. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1102. if (mvm->d3_wake_sysassert)
  1103. d3_cfg_cmd_data.wakeup_flags |=
  1104. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  1105. #endif
  1106. /* must be last -- this switches firmware state */
  1107. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  1108. if (ret)
  1109. goto out;
  1110. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1111. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  1112. if (len >= sizeof(u32)) {
  1113. mvm->d3_test_pme_ptr =
  1114. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  1115. }
  1116. #endif
  1117. iwl_free_resp(&d3_cfg_cmd);
  1118. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1119. iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
  1120. out:
  1121. if (ret < 0) {
  1122. iwl_mvm_free_nd(mvm);
  1123. if (!unified_image) {
  1124. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1125. if (mvm->fw_restart > 0) {
  1126. mvm->fw_restart--;
  1127. ieee80211_restart_hw(mvm->hw);
  1128. }
  1129. }
  1130. }
  1131. out_noreset:
  1132. mutex_unlock(&mvm->mutex);
  1133. return ret;
  1134. }
  1135. static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
  1136. {
  1137. struct iwl_notification_wait wait_d3;
  1138. static const u16 d3_notif[] = { D3_CONFIG_CMD };
  1139. int ret;
  1140. iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
  1141. d3_notif, ARRAY_SIZE(d3_notif),
  1142. NULL, NULL);
  1143. ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
  1144. if (ret)
  1145. goto remove_notif;
  1146. ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
  1147. WARN_ON_ONCE(ret);
  1148. return ret;
  1149. remove_notif:
  1150. iwl_remove_notification(&mvm->notif_wait, &wait_d3);
  1151. return ret;
  1152. }
  1153. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1154. {
  1155. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1156. struct iwl_trans *trans = mvm->trans;
  1157. int ret;
  1158. /* make sure the d0i3 exit work is not pending */
  1159. flush_work(&mvm->d0i3_exit_work);
  1160. ret = iwl_trans_suspend(trans);
  1161. if (ret)
  1162. return ret;
  1163. if (wowlan->any) {
  1164. trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  1165. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1166. ret = iwl_mvm_enter_d0i3_sync(mvm);
  1167. if (ret)
  1168. return ret;
  1169. }
  1170. mutex_lock(&mvm->d0i3_suspend_mutex);
  1171. __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1172. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1173. iwl_trans_d3_suspend(trans, false, false);
  1174. return 0;
  1175. }
  1176. trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1177. return __iwl_mvm_suspend(hw, wowlan, false);
  1178. }
  1179. /* converted data from the different status responses */
  1180. struct iwl_wowlan_status_data {
  1181. u16 pattern_number;
  1182. u16 qos_seq_ctr[8];
  1183. u32 wakeup_reasons;
  1184. u32 wake_packet_length;
  1185. u32 wake_packet_bufsize;
  1186. const u8 *wake_packet;
  1187. };
  1188. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1189. struct ieee80211_vif *vif,
  1190. struct iwl_wowlan_status_data *status)
  1191. {
  1192. struct sk_buff *pkt = NULL;
  1193. struct cfg80211_wowlan_wakeup wakeup = {
  1194. .pattern_idx = -1,
  1195. };
  1196. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1197. u32 reasons = status->wakeup_reasons;
  1198. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1199. wakeup_report = NULL;
  1200. goto report;
  1201. }
  1202. pm_wakeup_event(mvm->dev, 0);
  1203. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1204. wakeup.magic_pkt = true;
  1205. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1206. wakeup.pattern_idx =
  1207. status->pattern_number;
  1208. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1209. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1210. wakeup.disconnect = true;
  1211. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1212. wakeup.gtk_rekey_failure = true;
  1213. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1214. wakeup.rfkill_release = true;
  1215. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1216. wakeup.eap_identity_req = true;
  1217. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1218. wakeup.four_way_handshake = true;
  1219. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1220. wakeup.tcp_connlost = true;
  1221. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1222. wakeup.tcp_nomoretokens = true;
  1223. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1224. wakeup.tcp_match = true;
  1225. if (status->wake_packet_bufsize) {
  1226. int pktsize = status->wake_packet_bufsize;
  1227. int pktlen = status->wake_packet_length;
  1228. const u8 *pktdata = status->wake_packet;
  1229. struct ieee80211_hdr *hdr = (void *)pktdata;
  1230. int truncated = pktlen - pktsize;
  1231. /* this would be a firmware bug */
  1232. if (WARN_ON_ONCE(truncated < 0))
  1233. truncated = 0;
  1234. if (ieee80211_is_data(hdr->frame_control)) {
  1235. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1236. int ivlen = 0, icvlen = 4; /* also FCS */
  1237. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1238. if (!pkt)
  1239. goto report;
  1240. skb_put_data(pkt, pktdata, hdrlen);
  1241. pktdata += hdrlen;
  1242. pktsize -= hdrlen;
  1243. if (ieee80211_has_protected(hdr->frame_control)) {
  1244. /*
  1245. * This is unlocked and using gtk_i(c)vlen,
  1246. * but since everything is under RTNL still
  1247. * that's not really a problem - changing
  1248. * it would be difficult.
  1249. */
  1250. if (is_multicast_ether_addr(hdr->addr1)) {
  1251. ivlen = mvm->gtk_ivlen;
  1252. icvlen += mvm->gtk_icvlen;
  1253. } else {
  1254. ivlen = mvm->ptk_ivlen;
  1255. icvlen += mvm->ptk_icvlen;
  1256. }
  1257. }
  1258. /* if truncated, FCS/ICV is (partially) gone */
  1259. if (truncated >= icvlen) {
  1260. icvlen = 0;
  1261. truncated -= icvlen;
  1262. } else {
  1263. icvlen -= truncated;
  1264. truncated = 0;
  1265. }
  1266. pktsize -= ivlen + icvlen;
  1267. pktdata += ivlen;
  1268. skb_put_data(pkt, pktdata, pktsize);
  1269. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1270. goto report;
  1271. wakeup.packet = pkt->data;
  1272. wakeup.packet_present_len = pkt->len;
  1273. wakeup.packet_len = pkt->len - truncated;
  1274. wakeup.packet_80211 = false;
  1275. } else {
  1276. int fcslen = 4;
  1277. if (truncated >= 4) {
  1278. truncated -= 4;
  1279. fcslen = 0;
  1280. } else {
  1281. fcslen -= truncated;
  1282. truncated = 0;
  1283. }
  1284. pktsize -= fcslen;
  1285. wakeup.packet = status->wake_packet;
  1286. wakeup.packet_present_len = pktsize;
  1287. wakeup.packet_len = pktlen - truncated;
  1288. wakeup.packet_80211 = true;
  1289. }
  1290. }
  1291. report:
  1292. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1293. kfree_skb(pkt);
  1294. }
  1295. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1296. struct ieee80211_key_seq *seq)
  1297. {
  1298. u64 pn;
  1299. pn = le64_to_cpu(sc->pn);
  1300. seq->ccmp.pn[0] = pn >> 40;
  1301. seq->ccmp.pn[1] = pn >> 32;
  1302. seq->ccmp.pn[2] = pn >> 24;
  1303. seq->ccmp.pn[3] = pn >> 16;
  1304. seq->ccmp.pn[4] = pn >> 8;
  1305. seq->ccmp.pn[5] = pn;
  1306. }
  1307. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1308. struct ieee80211_key_seq *seq)
  1309. {
  1310. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1311. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1312. }
  1313. static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
  1314. struct ieee80211_sta *sta,
  1315. struct ieee80211_key_conf *key)
  1316. {
  1317. int tid;
  1318. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1319. if (sta && iwl_mvm_has_new_rx_api(mvm)) {
  1320. struct iwl_mvm_sta *mvmsta;
  1321. struct iwl_mvm_key_pn *ptk_pn;
  1322. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1323. ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
  1324. lockdep_is_held(&mvm->mutex));
  1325. if (WARN_ON(!ptk_pn))
  1326. return;
  1327. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  1328. struct ieee80211_key_seq seq = {};
  1329. int i;
  1330. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1331. ieee80211_set_key_rx_seq(key, tid, &seq);
  1332. for (i = 1; i < mvm->trans->num_rx_queues; i++)
  1333. memcpy(ptk_pn->q[i].pn[tid],
  1334. seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
  1335. }
  1336. } else {
  1337. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1338. struct ieee80211_key_seq seq = {};
  1339. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1340. ieee80211_set_key_rx_seq(key, tid, &seq);
  1341. }
  1342. }
  1343. }
  1344. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1345. struct ieee80211_key_conf *key)
  1346. {
  1347. int tid;
  1348. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1349. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1350. struct ieee80211_key_seq seq = {};
  1351. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1352. ieee80211_set_key_rx_seq(key, tid, &seq);
  1353. }
  1354. }
  1355. static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
  1356. struct ieee80211_key_conf *key,
  1357. struct iwl_wowlan_status *status)
  1358. {
  1359. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1360. switch (key->cipher) {
  1361. case WLAN_CIPHER_SUITE_CCMP:
  1362. iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
  1363. break;
  1364. case WLAN_CIPHER_SUITE_TKIP:
  1365. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1366. break;
  1367. default:
  1368. WARN_ON(1);
  1369. }
  1370. }
  1371. struct iwl_mvm_d3_gtk_iter_data {
  1372. struct iwl_mvm *mvm;
  1373. struct iwl_wowlan_status *status;
  1374. void *last_gtk;
  1375. u32 cipher;
  1376. bool find_phase, unhandled_cipher;
  1377. int num_keys;
  1378. };
  1379. static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
  1380. struct ieee80211_vif *vif,
  1381. struct ieee80211_sta *sta,
  1382. struct ieee80211_key_conf *key,
  1383. void *_data)
  1384. {
  1385. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1386. if (data->unhandled_cipher)
  1387. return;
  1388. switch (key->cipher) {
  1389. case WLAN_CIPHER_SUITE_WEP40:
  1390. case WLAN_CIPHER_SUITE_WEP104:
  1391. /* ignore WEP completely, nothing to do */
  1392. return;
  1393. case WLAN_CIPHER_SUITE_CCMP:
  1394. case WLAN_CIPHER_SUITE_TKIP:
  1395. /* we support these */
  1396. break;
  1397. default:
  1398. /* everything else (even CMAC for MFP) - disconnect from AP */
  1399. data->unhandled_cipher = true;
  1400. return;
  1401. }
  1402. data->num_keys++;
  1403. /*
  1404. * pairwise key - update sequence counters only;
  1405. * note that this assumes no TDLS sessions are active
  1406. */
  1407. if (sta) {
  1408. struct ieee80211_key_seq seq = {};
  1409. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1410. if (data->find_phase)
  1411. return;
  1412. switch (key->cipher) {
  1413. case WLAN_CIPHER_SUITE_CCMP:
  1414. iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
  1415. sta, key);
  1416. atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
  1417. break;
  1418. case WLAN_CIPHER_SUITE_TKIP:
  1419. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1420. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1421. atomic64_set(&key->tx_pn,
  1422. (u64)seq.tkip.iv16 |
  1423. ((u64)seq.tkip.iv32 << 16));
  1424. break;
  1425. }
  1426. /* that's it for this key */
  1427. return;
  1428. }
  1429. if (data->find_phase) {
  1430. data->last_gtk = key;
  1431. data->cipher = key->cipher;
  1432. return;
  1433. }
  1434. if (data->status->num_of_gtk_rekeys)
  1435. ieee80211_remove_key(key);
  1436. else if (data->last_gtk == key)
  1437. iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
  1438. }
  1439. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1440. struct ieee80211_vif *vif,
  1441. struct iwl_wowlan_status *status)
  1442. {
  1443. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1444. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1445. .mvm = mvm,
  1446. .status = status,
  1447. };
  1448. u32 disconnection_reasons =
  1449. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1450. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1451. if (!status || !vif->bss_conf.bssid)
  1452. return false;
  1453. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1454. return false;
  1455. /* find last GTK that we used initially, if any */
  1456. gtkdata.find_phase = true;
  1457. ieee80211_iter_keys(mvm->hw, vif,
  1458. iwl_mvm_d3_update_keys, &gtkdata);
  1459. /* not trying to keep connections with MFP/unhandled ciphers */
  1460. if (gtkdata.unhandled_cipher)
  1461. return false;
  1462. if (!gtkdata.num_keys)
  1463. goto out;
  1464. if (!gtkdata.last_gtk)
  1465. return false;
  1466. /*
  1467. * invalidate all other GTKs that might still exist and update
  1468. * the one that we used
  1469. */
  1470. gtkdata.find_phase = false;
  1471. ieee80211_iter_keys(mvm->hw, vif,
  1472. iwl_mvm_d3_update_keys, &gtkdata);
  1473. if (status->num_of_gtk_rekeys) {
  1474. struct ieee80211_key_conf *key;
  1475. struct {
  1476. struct ieee80211_key_conf conf;
  1477. u8 key[32];
  1478. } conf = {
  1479. .conf.cipher = gtkdata.cipher,
  1480. .conf.keyidx = status->gtk.key_index,
  1481. };
  1482. switch (gtkdata.cipher) {
  1483. case WLAN_CIPHER_SUITE_CCMP:
  1484. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1485. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1486. WLAN_KEY_LEN_CCMP);
  1487. break;
  1488. case WLAN_CIPHER_SUITE_TKIP:
  1489. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1490. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1491. /* leave TX MIC key zeroed, we don't use it anyway */
  1492. memcpy(conf.conf.key +
  1493. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1494. status->gtk.tkip_mic_key, 8);
  1495. break;
  1496. }
  1497. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1498. if (IS_ERR(key))
  1499. return false;
  1500. iwl_mvm_set_key_rx_seq(mvm, key, status);
  1501. }
  1502. if (status->num_of_gtk_rekeys) {
  1503. __be64 replay_ctr =
  1504. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1505. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1506. (void *)&replay_ctr, GFP_KERNEL);
  1507. }
  1508. out:
  1509. mvmvif->seqno_valid = true;
  1510. /* +0x10 because the set API expects next-to-use, not last-used */
  1511. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1512. return true;
  1513. }
  1514. static struct iwl_wowlan_status *
  1515. iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1516. {
  1517. u32 base = mvm->error_event_table[0];
  1518. struct error_table_start {
  1519. /* cf. struct iwl_error_event_table */
  1520. u32 valid;
  1521. u32 error_id;
  1522. } err_info;
  1523. struct iwl_host_cmd cmd = {
  1524. .id = WOWLAN_GET_STATUSES,
  1525. .flags = CMD_WANT_SKB,
  1526. };
  1527. struct iwl_wowlan_status *status, *fw_status;
  1528. int ret, len, status_size;
  1529. iwl_trans_read_mem_bytes(mvm->trans, base,
  1530. &err_info, sizeof(err_info));
  1531. if (err_info.valid) {
  1532. IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
  1533. err_info.valid, err_info.error_id);
  1534. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1535. struct cfg80211_wowlan_wakeup wakeup = {
  1536. .rfkill_release = true,
  1537. };
  1538. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1539. GFP_KERNEL);
  1540. }
  1541. return ERR_PTR(-EIO);
  1542. }
  1543. /* only for tracing for now */
  1544. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
  1545. if (ret)
  1546. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1547. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1548. if (ret) {
  1549. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1550. return ERR_PTR(ret);
  1551. }
  1552. status_size = sizeof(*fw_status);
  1553. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1554. if (len < status_size) {
  1555. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1556. fw_status = ERR_PTR(-EIO);
  1557. goto out_free_resp;
  1558. }
  1559. status = (void *)cmd.resp_pkt->data;
  1560. if (len != (status_size +
  1561. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
  1562. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1563. fw_status = ERR_PTR(-EIO);
  1564. goto out_free_resp;
  1565. }
  1566. fw_status = kmemdup(status, len, GFP_KERNEL);
  1567. out_free_resp:
  1568. iwl_free_resp(&cmd);
  1569. return fw_status;
  1570. }
  1571. /* releases the MVM mutex */
  1572. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1573. struct ieee80211_vif *vif)
  1574. {
  1575. struct iwl_wowlan_status_data status;
  1576. struct iwl_wowlan_status *fw_status;
  1577. int i;
  1578. bool keep;
  1579. struct iwl_mvm_sta *mvm_ap_sta;
  1580. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1581. if (IS_ERR_OR_NULL(fw_status))
  1582. goto out_unlock;
  1583. status.pattern_number = le16_to_cpu(fw_status->pattern_number);
  1584. for (i = 0; i < 8; i++)
  1585. status.qos_seq_ctr[i] =
  1586. le16_to_cpu(fw_status->qos_seq_ctr[i]);
  1587. status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1588. status.wake_packet_length =
  1589. le32_to_cpu(fw_status->wake_packet_length);
  1590. status.wake_packet_bufsize =
  1591. le32_to_cpu(fw_status->wake_packet_bufsize);
  1592. status.wake_packet = fw_status->wake_packet;
  1593. /* still at hard-coded place 0 for D3 image */
  1594. mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
  1595. if (!mvm_ap_sta)
  1596. goto out_free;
  1597. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1598. u16 seq = status.qos_seq_ctr[i];
  1599. /* firmware stores last-used value, we store next value */
  1600. seq += 0x10;
  1601. mvm_ap_sta->tid_data[i].seq_number = seq;
  1602. }
  1603. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1604. mutex_unlock(&mvm->mutex);
  1605. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1606. keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
  1607. kfree(fw_status);
  1608. return keep;
  1609. out_free:
  1610. kfree(fw_status);
  1611. out_unlock:
  1612. mutex_unlock(&mvm->mutex);
  1613. return false;
  1614. }
  1615. void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
  1616. struct ieee80211_vif *vif,
  1617. struct iwl_wowlan_status *status)
  1618. {
  1619. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1620. .mvm = mvm,
  1621. .status = status,
  1622. };
  1623. /*
  1624. * rekey handling requires taking locks that can't be taken now.
  1625. * however, d0i3 doesn't offload rekey, so we're fine.
  1626. */
  1627. if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
  1628. return;
  1629. /* find last GTK that we used initially, if any */
  1630. gtkdata.find_phase = true;
  1631. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1632. gtkdata.find_phase = false;
  1633. iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
  1634. }
  1635. struct iwl_mvm_nd_query_results {
  1636. u32 matched_profiles;
  1637. struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
  1638. };
  1639. static int
  1640. iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
  1641. struct iwl_mvm_nd_query_results *results)
  1642. {
  1643. struct iwl_scan_offload_profiles_query *query;
  1644. struct iwl_host_cmd cmd = {
  1645. .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
  1646. .flags = CMD_WANT_SKB,
  1647. };
  1648. int ret, len;
  1649. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1650. if (ret) {
  1651. IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
  1652. return ret;
  1653. }
  1654. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1655. if (len < sizeof(*query)) {
  1656. IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
  1657. ret = -EIO;
  1658. goto out_free_resp;
  1659. }
  1660. query = (void *)cmd.resp_pkt->data;
  1661. results->matched_profiles = le32_to_cpu(query->matched_profiles);
  1662. memcpy(results->matches, query->matches, sizeof(results->matches));
  1663. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1664. mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
  1665. #endif
  1666. out_free_resp:
  1667. iwl_free_resp(&cmd);
  1668. return ret;
  1669. }
  1670. static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
  1671. struct ieee80211_vif *vif)
  1672. {
  1673. struct cfg80211_wowlan_nd_info *net_detect = NULL;
  1674. struct cfg80211_wowlan_wakeup wakeup = {
  1675. .pattern_idx = -1,
  1676. };
  1677. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1678. struct iwl_mvm_nd_query_results query;
  1679. struct iwl_wowlan_status *fw_status;
  1680. unsigned long matched_profiles;
  1681. u32 reasons = 0;
  1682. int i, j, n_matches, ret;
  1683. fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
  1684. if (!IS_ERR_OR_NULL(fw_status)) {
  1685. reasons = le32_to_cpu(fw_status->wakeup_reasons);
  1686. kfree(fw_status);
  1687. }
  1688. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1689. wakeup.rfkill_release = true;
  1690. if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
  1691. goto out;
  1692. ret = iwl_mvm_netdetect_query_results(mvm, &query);
  1693. if (ret || !query.matched_profiles) {
  1694. wakeup_report = NULL;
  1695. goto out;
  1696. }
  1697. matched_profiles = query.matched_profiles;
  1698. if (mvm->n_nd_match_sets) {
  1699. n_matches = hweight_long(matched_profiles);
  1700. } else {
  1701. IWL_ERR(mvm, "no net detect match information available\n");
  1702. n_matches = 0;
  1703. }
  1704. net_detect = kzalloc(sizeof(*net_detect) +
  1705. (n_matches * sizeof(net_detect->matches[0])),
  1706. GFP_KERNEL);
  1707. if (!net_detect || !n_matches)
  1708. goto out_report_nd;
  1709. for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
  1710. struct iwl_scan_offload_profile_match *fw_match;
  1711. struct cfg80211_wowlan_nd_match *match;
  1712. int idx, n_channels = 0;
  1713. fw_match = &query.matches[i];
  1714. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
  1715. n_channels += hweight8(fw_match->matching_channels[j]);
  1716. match = kzalloc(sizeof(*match) +
  1717. (n_channels * sizeof(*match->channels)),
  1718. GFP_KERNEL);
  1719. if (!match)
  1720. goto out_report_nd;
  1721. net_detect->matches[net_detect->n_matches++] = match;
  1722. /* We inverted the order of the SSIDs in the scan
  1723. * request, so invert the index here.
  1724. */
  1725. idx = mvm->n_nd_match_sets - i - 1;
  1726. match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
  1727. memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
  1728. match->ssid.ssid_len);
  1729. if (mvm->n_nd_channels < n_channels)
  1730. continue;
  1731. for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
  1732. if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
  1733. match->channels[match->n_channels++] =
  1734. mvm->nd_channels[j]->center_freq;
  1735. }
  1736. out_report_nd:
  1737. wakeup.net_detect = net_detect;
  1738. out:
  1739. iwl_mvm_free_nd(mvm);
  1740. mutex_unlock(&mvm->mutex);
  1741. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1742. if (net_detect) {
  1743. for (i = 0; i < net_detect->n_matches; i++)
  1744. kfree(net_detect->matches[i]);
  1745. kfree(net_detect);
  1746. }
  1747. }
  1748. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1749. {
  1750. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1751. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1752. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1753. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1754. if (!mvm->store_d3_resume_sram)
  1755. return;
  1756. if (!mvm->d3_resume_sram) {
  1757. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1758. if (!mvm->d3_resume_sram)
  1759. return;
  1760. }
  1761. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1762. #endif
  1763. }
  1764. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1765. struct ieee80211_vif *vif)
  1766. {
  1767. /* skip the one we keep connection on */
  1768. if (data == vif)
  1769. return;
  1770. if (vif->type == NL80211_IFTYPE_STATION)
  1771. ieee80211_resume_disconnect(vif);
  1772. }
  1773. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1774. {
  1775. struct ieee80211_vif *vif = NULL;
  1776. int ret = 1;
  1777. enum iwl_d3_status d3_status;
  1778. bool keep = false;
  1779. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1780. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1781. bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
  1782. IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
  1783. mutex_lock(&mvm->mutex);
  1784. /* get the BSS vif pointer again */
  1785. vif = iwl_mvm_get_bss_vif(mvm);
  1786. if (IS_ERR_OR_NULL(vif))
  1787. goto err;
  1788. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
  1789. if (ret)
  1790. goto err;
  1791. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1792. IWL_INFO(mvm, "Device was reset during suspend\n");
  1793. goto err;
  1794. }
  1795. /* query SRAM first in case we want event logging */
  1796. iwl_mvm_read_d3_sram(mvm);
  1797. if (d0i3_first) {
  1798. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
  1799. if (ret < 0) {
  1800. IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
  1801. ret);
  1802. goto err;
  1803. }
  1804. }
  1805. /*
  1806. * Query the current location and source from the D3 firmware so we
  1807. * can play it back when we re-intiailize the D0 firmware
  1808. */
  1809. iwl_mvm_update_changed_regdom(mvm);
  1810. if (!unified_image)
  1811. /* Re-configure default SAR profile */
  1812. iwl_mvm_sar_select_profile(mvm, 1, 1);
  1813. if (mvm->net_detect) {
  1814. /* If this is a non-unified image, we restart the FW,
  1815. * so no need to stop the netdetect scan. If that
  1816. * fails, continue and try to get the wake-up reasons,
  1817. * but trigger a HW restart by keeping a failure code
  1818. * in ret.
  1819. */
  1820. if (unified_image)
  1821. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
  1822. false);
  1823. iwl_mvm_query_netdetect_reasons(mvm, vif);
  1824. /* has unlocked the mutex, so skip that */
  1825. goto out;
  1826. } else {
  1827. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1828. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1829. if (keep)
  1830. mvm->keep_vif = vif;
  1831. #endif
  1832. /* has unlocked the mutex, so skip that */
  1833. goto out_iterate;
  1834. }
  1835. err:
  1836. iwl_mvm_free_nd(mvm);
  1837. mutex_unlock(&mvm->mutex);
  1838. out_iterate:
  1839. if (!test)
  1840. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1841. IEEE80211_IFACE_ITER_NORMAL,
  1842. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1843. out:
  1844. /* no need to reset the device in unified images, if successful */
  1845. if (unified_image && !ret) {
  1846. /* nothing else to do if we already sent D0I3_END_CMD */
  1847. if (d0i3_first)
  1848. return 0;
  1849. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
  1850. if (!ret)
  1851. return 0;
  1852. }
  1853. /*
  1854. * Reconfigure the device in one of the following cases:
  1855. * 1. We are not using a unified image
  1856. * 2. We are using a unified image but had an error while exiting D3
  1857. */
  1858. set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
  1859. set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
  1860. /*
  1861. * When switching images we return 1, which causes mac80211
  1862. * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
  1863. * This type of reconfig calls iwl_mvm_restart_complete(),
  1864. * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
  1865. * to take the reference here.
  1866. */
  1867. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1868. return 1;
  1869. }
  1870. static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
  1871. {
  1872. iwl_trans_resume(mvm->trans);
  1873. return __iwl_mvm_resume(mvm, false);
  1874. }
  1875. static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
  1876. {
  1877. bool exit_now;
  1878. enum iwl_d3_status d3_status;
  1879. struct iwl_trans *trans = mvm->trans;
  1880. iwl_trans_d3_resume(trans, &d3_status, false, false);
  1881. /*
  1882. * make sure to clear D0I3_DEFER_WAKEUP before
  1883. * calling iwl_trans_resume(), which might wait
  1884. * for d0i3 exit completion.
  1885. */
  1886. mutex_lock(&mvm->d0i3_suspend_mutex);
  1887. __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
  1888. exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
  1889. &mvm->d0i3_suspend_flags);
  1890. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1891. if (exit_now) {
  1892. IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
  1893. _iwl_mvm_exit_d0i3(mvm);
  1894. }
  1895. iwl_trans_resume(trans);
  1896. if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
  1897. int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
  1898. if (ret)
  1899. return ret;
  1900. /*
  1901. * d0i3 exit will be deferred until reconfig_complete.
  1902. * make sure there we are out of d0i3.
  1903. */
  1904. }
  1905. return 0;
  1906. }
  1907. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1908. {
  1909. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1910. int ret;
  1911. if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  1912. ret = iwl_mvm_resume_d0i3(mvm);
  1913. else
  1914. ret = iwl_mvm_resume_d3(mvm);
  1915. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1916. return ret;
  1917. }
  1918. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1919. {
  1920. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1921. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1922. }
  1923. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1924. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1925. {
  1926. struct iwl_mvm *mvm = inode->i_private;
  1927. int err;
  1928. if (mvm->d3_test_active)
  1929. return -EBUSY;
  1930. file->private_data = inode->i_private;
  1931. ieee80211_stop_queues(mvm->hw);
  1932. synchronize_net();
  1933. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
  1934. /* start pseudo D3 */
  1935. rtnl_lock();
  1936. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1937. rtnl_unlock();
  1938. if (err > 0)
  1939. err = -EINVAL;
  1940. if (err) {
  1941. ieee80211_wake_queues(mvm->hw);
  1942. return err;
  1943. }
  1944. mvm->d3_test_active = true;
  1945. mvm->keep_vif = NULL;
  1946. return 0;
  1947. }
  1948. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1949. size_t count, loff_t *ppos)
  1950. {
  1951. struct iwl_mvm *mvm = file->private_data;
  1952. u32 pme_asserted;
  1953. while (true) {
  1954. /* read pme_ptr if available */
  1955. if (mvm->d3_test_pme_ptr) {
  1956. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1957. mvm->d3_test_pme_ptr);
  1958. if (pme_asserted)
  1959. break;
  1960. }
  1961. if (msleep_interruptible(100))
  1962. break;
  1963. }
  1964. return 0;
  1965. }
  1966. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1967. struct ieee80211_vif *vif)
  1968. {
  1969. /* skip the one we keep connection on */
  1970. if (_data == vif)
  1971. return;
  1972. if (vif->type == NL80211_IFTYPE_STATION)
  1973. ieee80211_connection_loss(vif);
  1974. }
  1975. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1976. {
  1977. struct iwl_mvm *mvm = inode->i_private;
  1978. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1979. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1980. mvm->d3_test_active = false;
  1981. rtnl_lock();
  1982. __iwl_mvm_resume(mvm, true);
  1983. rtnl_unlock();
  1984. mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  1985. iwl_abort_notification_waits(&mvm->notif_wait);
  1986. if (!unified_image) {
  1987. int remaining_time = 10;
  1988. ieee80211_restart_hw(mvm->hw);
  1989. /* wait for restart and disconnect all interfaces */
  1990. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1991. remaining_time > 0) {
  1992. remaining_time--;
  1993. msleep(1000);
  1994. }
  1995. if (remaining_time == 0)
  1996. IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
  1997. }
  1998. ieee80211_iterate_active_interfaces_atomic(
  1999. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  2000. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  2001. ieee80211_wake_queues(mvm->hw);
  2002. return 0;
  2003. }
  2004. const struct file_operations iwl_dbgfs_d3_test_ops = {
  2005. .llseek = no_llseek,
  2006. .open = iwl_mvm_d3_test_open,
  2007. .read = iwl_mvm_d3_test_read,
  2008. .release = iwl_mvm_d3_test_release,
  2009. };
  2010. #endif