d3.c 54 KB

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