d3.c 46 KB

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