d3.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 - 2013 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 - 2013 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 "iwl-modparams.h"
  70. #include "fw-api.h"
  71. #include "mvm.h"
  72. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  73. struct ieee80211_vif *vif,
  74. struct cfg80211_gtk_rekey_data *data)
  75. {
  76. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  77. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  78. if (iwlwifi_mod_params.sw_crypto)
  79. return;
  80. mutex_lock(&mvm->mutex);
  81. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  82. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  83. mvmvif->rekey_data.replay_ctr =
  84. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  85. mvmvif->rekey_data.valid = true;
  86. mutex_unlock(&mvm->mutex);
  87. }
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  90. struct ieee80211_vif *vif,
  91. struct inet6_dev *idev)
  92. {
  93. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  94. struct inet6_ifaddr *ifa;
  95. int idx = 0;
  96. read_lock_bh(&idev->lock);
  97. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  98. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  99. idx++;
  100. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  101. break;
  102. }
  103. read_unlock_bh(&idev->lock);
  104. mvmvif->num_target_ipv6_addrs = idx;
  105. }
  106. #endif
  107. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  108. struct ieee80211_vif *vif, int idx)
  109. {
  110. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  111. mvmvif->tx_key_idx = idx;
  112. }
  113. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  114. {
  115. int i;
  116. for (i = 0; i < IWL_P1K_SIZE; i++)
  117. out[i] = cpu_to_le16(p1k[i]);
  118. }
  119. struct wowlan_key_data {
  120. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  121. struct iwl_wowlan_tkip_params_cmd *tkip;
  122. bool error, use_rsc_tsc, use_tkip;
  123. int gtk_key_idx;
  124. };
  125. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  126. struct ieee80211_vif *vif,
  127. struct ieee80211_sta *sta,
  128. struct ieee80211_key_conf *key,
  129. void *_data)
  130. {
  131. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  132. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  133. struct wowlan_key_data *data = _data;
  134. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  135. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  136. struct iwl_p1k_cache *rx_p1ks;
  137. u8 *rx_mic_key;
  138. struct ieee80211_key_seq seq;
  139. u32 cur_rx_iv32 = 0;
  140. u16 p1k[IWL_P1K_SIZE];
  141. int ret, i;
  142. mutex_lock(&mvm->mutex);
  143. switch (key->cipher) {
  144. case WLAN_CIPHER_SUITE_WEP40:
  145. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  146. struct {
  147. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  148. struct iwl_mvm_wep_key wep_key;
  149. } __packed wkc = {
  150. .wep_key_cmd.mac_id_n_color =
  151. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  152. mvmvif->color)),
  153. .wep_key_cmd.num_keys = 1,
  154. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  155. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  156. .wep_key.key_index = key->keyidx,
  157. .wep_key.key_size = key->keylen,
  158. };
  159. /*
  160. * This will fail -- the key functions don't set support
  161. * pairwise WEP keys. However, that's better than silently
  162. * failing WoWLAN. Or maybe not?
  163. */
  164. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  165. break;
  166. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  167. if (key->keyidx == mvmvif->tx_key_idx) {
  168. /* TX key must be at offset 0 */
  169. wkc.wep_key.key_offset = 0;
  170. } else {
  171. /* others start at 1 */
  172. data->gtk_key_idx++;
  173. wkc.wep_key.key_offset = data->gtk_key_idx;
  174. }
  175. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
  176. 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. data->gtk_key_idx++;
  284. key->hw_key_idx = data->gtk_key_idx;
  285. mvm->gtk_ivlen = key->iv_len;
  286. mvm->gtk_icvlen = key->icv_len;
  287. }
  288. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  289. data->error = ret != 0;
  290. out_unlock:
  291. mutex_unlock(&mvm->mutex);
  292. }
  293. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  294. struct cfg80211_wowlan *wowlan)
  295. {
  296. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  297. struct iwl_host_cmd cmd = {
  298. .id = WOWLAN_PATTERNS,
  299. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  300. .flags = CMD_SYNC,
  301. };
  302. int i, err;
  303. if (!wowlan->n_patterns)
  304. return 0;
  305. cmd.len[0] = sizeof(*pattern_cmd) +
  306. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  307. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  308. if (!pattern_cmd)
  309. return -ENOMEM;
  310. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  311. for (i = 0; i < wowlan->n_patterns; i++) {
  312. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  313. memcpy(&pattern_cmd->patterns[i].mask,
  314. wowlan->patterns[i].mask, mask_len);
  315. memcpy(&pattern_cmd->patterns[i].pattern,
  316. wowlan->patterns[i].pattern,
  317. wowlan->patterns[i].pattern_len);
  318. pattern_cmd->patterns[i].mask_size = mask_len;
  319. pattern_cmd->patterns[i].pattern_size =
  320. wowlan->patterns[i].pattern_len;
  321. }
  322. cmd.data[0] = pattern_cmd;
  323. err = iwl_mvm_send_cmd(mvm, &cmd);
  324. kfree(pattern_cmd);
  325. return err;
  326. }
  327. static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
  328. struct ieee80211_vif *vif)
  329. {
  330. union {
  331. struct iwl_proto_offload_cmd_v1 v1;
  332. struct iwl_proto_offload_cmd_v2 v2;
  333. } cmd = {};
  334. struct iwl_proto_offload_cmd_common *common;
  335. u32 enabled = 0, size;
  336. #if IS_ENABLED(CONFIG_IPV6)
  337. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  338. int i;
  339. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  340. if (mvmvif->num_target_ipv6_addrs) {
  341. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  342. memcpy(cmd.v2.ndp_mac_addr, vif->addr, ETH_ALEN);
  343. }
  344. BUILD_BUG_ON(sizeof(cmd.v2.target_ipv6_addr[0]) !=
  345. sizeof(mvmvif->target_ipv6_addrs[0]));
  346. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  347. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V2); i++)
  348. memcpy(cmd.v2.target_ipv6_addr[i],
  349. &mvmvif->target_ipv6_addrs[i],
  350. sizeof(cmd.v2.target_ipv6_addr[i]));
  351. } else {
  352. if (mvmvif->num_target_ipv6_addrs) {
  353. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  354. memcpy(cmd.v1.ndp_mac_addr, vif->addr, ETH_ALEN);
  355. }
  356. BUILD_BUG_ON(sizeof(cmd.v1.target_ipv6_addr[0]) !=
  357. sizeof(mvmvif->target_ipv6_addrs[0]));
  358. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  359. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V1); i++)
  360. memcpy(cmd.v1.target_ipv6_addr[i],
  361. &mvmvif->target_ipv6_addrs[i],
  362. sizeof(cmd.v1.target_ipv6_addr[i]));
  363. }
  364. #endif
  365. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  366. common = &cmd.v2.common;
  367. size = sizeof(cmd.v2);
  368. } else {
  369. common = &cmd.v1.common;
  370. size = sizeof(cmd.v1);
  371. }
  372. if (vif->bss_conf.arp_addr_cnt) {
  373. enabled |= IWL_D3_PROTO_OFFLOAD_ARP;
  374. common->host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
  375. memcpy(common->arp_mac_addr, vif->addr, ETH_ALEN);
  376. }
  377. if (!enabled)
  378. return 0;
  379. common->enabled = cpu_to_le32(enabled);
  380. return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
  381. size, &cmd);
  382. }
  383. enum iwl_mvm_tcp_packet_type {
  384. MVM_TCP_TX_SYN,
  385. MVM_TCP_RX_SYNACK,
  386. MVM_TCP_TX_DATA,
  387. MVM_TCP_RX_ACK,
  388. MVM_TCP_RX_WAKE,
  389. MVM_TCP_TX_FIN,
  390. };
  391. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  392. {
  393. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  394. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  395. }
  396. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  397. struct cfg80211_wowlan_tcp *tcp,
  398. void *_pkt, u8 *mask,
  399. __le16 *pseudo_hdr_csum,
  400. enum iwl_mvm_tcp_packet_type ptype)
  401. {
  402. struct {
  403. struct ethhdr eth;
  404. struct iphdr ip;
  405. struct tcphdr tcp;
  406. u8 data[];
  407. } __packed *pkt = _pkt;
  408. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  409. int i;
  410. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  411. pkt->ip.version = 4;
  412. pkt->ip.ihl = 5;
  413. pkt->ip.protocol = IPPROTO_TCP;
  414. switch (ptype) {
  415. case MVM_TCP_TX_SYN:
  416. case MVM_TCP_TX_DATA:
  417. case MVM_TCP_TX_FIN:
  418. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  419. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  420. pkt->ip.ttl = 128;
  421. pkt->ip.saddr = tcp->src;
  422. pkt->ip.daddr = tcp->dst;
  423. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  424. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  425. /* overwritten for TX SYN later */
  426. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  427. pkt->tcp.window = cpu_to_be16(65000);
  428. break;
  429. case MVM_TCP_RX_SYNACK:
  430. case MVM_TCP_RX_ACK:
  431. case MVM_TCP_RX_WAKE:
  432. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  433. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  434. pkt->ip.saddr = tcp->dst;
  435. pkt->ip.daddr = tcp->src;
  436. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  437. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  438. break;
  439. default:
  440. WARN_ON(1);
  441. return;
  442. }
  443. switch (ptype) {
  444. case MVM_TCP_TX_SYN:
  445. /* firmware assumes 8 option bytes - 8 NOPs for now */
  446. memset(pkt->data, 0x01, 8);
  447. ip_tot_len += 8;
  448. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  449. pkt->tcp.syn = 1;
  450. break;
  451. case MVM_TCP_TX_DATA:
  452. ip_tot_len += tcp->payload_len;
  453. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  454. pkt->tcp.psh = 1;
  455. pkt->tcp.ack = 1;
  456. break;
  457. case MVM_TCP_TX_FIN:
  458. pkt->tcp.fin = 1;
  459. pkt->tcp.ack = 1;
  460. break;
  461. case MVM_TCP_RX_SYNACK:
  462. pkt->tcp.syn = 1;
  463. pkt->tcp.ack = 1;
  464. break;
  465. case MVM_TCP_RX_ACK:
  466. pkt->tcp.ack = 1;
  467. break;
  468. case MVM_TCP_RX_WAKE:
  469. ip_tot_len += tcp->wake_len;
  470. pkt->tcp.psh = 1;
  471. pkt->tcp.ack = 1;
  472. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  473. break;
  474. }
  475. switch (ptype) {
  476. case MVM_TCP_TX_SYN:
  477. case MVM_TCP_TX_DATA:
  478. case MVM_TCP_TX_FIN:
  479. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  480. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  481. break;
  482. case MVM_TCP_RX_WAKE:
  483. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  484. u8 tmp = tcp->wake_mask[i];
  485. mask[i + 6] |= tmp << 6;
  486. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  487. mask[i + 7] = tmp >> 2;
  488. }
  489. /* fall through for ethernet/IP/TCP headers mask */
  490. case MVM_TCP_RX_SYNACK:
  491. case MVM_TCP_RX_ACK:
  492. mask[0] = 0xff; /* match ethernet */
  493. /*
  494. * match ethernet, ip.version, ip.ihl
  495. * the ip.ihl half byte is really masked out by firmware
  496. */
  497. mask[1] = 0x7f;
  498. mask[2] = 0x80; /* match ip.protocol */
  499. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  500. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  501. mask[5] = 0x80; /* match tcp flags */
  502. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  503. break;
  504. };
  505. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  506. pkt->ip.saddr, pkt->ip.daddr);
  507. }
  508. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  509. struct ieee80211_vif *vif,
  510. struct cfg80211_wowlan_tcp *tcp)
  511. {
  512. struct iwl_wowlan_remote_wake_config *cfg;
  513. struct iwl_host_cmd cmd = {
  514. .id = REMOTE_WAKE_CONFIG_CMD,
  515. .len = { sizeof(*cfg), },
  516. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  517. .flags = CMD_SYNC,
  518. };
  519. int ret;
  520. if (!tcp)
  521. return 0;
  522. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  523. if (!cfg)
  524. return -ENOMEM;
  525. cmd.data[0] = cfg;
  526. cfg->max_syn_retries = 10;
  527. cfg->max_data_retries = 10;
  528. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  529. cfg->tcp_ack_timeout = 1; /* seconds */
  530. /* SYN (TX) */
  531. iwl_mvm_build_tcp_packet(
  532. vif, tcp, cfg->syn_tx.data, NULL,
  533. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  534. MVM_TCP_TX_SYN);
  535. cfg->syn_tx.info.tcp_payload_length = 0;
  536. /* SYN/ACK (RX) */
  537. iwl_mvm_build_tcp_packet(
  538. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  539. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  540. MVM_TCP_RX_SYNACK);
  541. cfg->synack_rx.info.tcp_payload_length = 0;
  542. /* KEEPALIVE/ACK (TX) */
  543. iwl_mvm_build_tcp_packet(
  544. vif, tcp, cfg->keepalive_tx.data, NULL,
  545. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  546. MVM_TCP_TX_DATA);
  547. cfg->keepalive_tx.info.tcp_payload_length =
  548. cpu_to_le16(tcp->payload_len);
  549. cfg->sequence_number_offset = tcp->payload_seq.offset;
  550. /* length must be 0..4, the field is little endian */
  551. cfg->sequence_number_length = tcp->payload_seq.len;
  552. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  553. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  554. if (tcp->payload_tok.len) {
  555. cfg->token_offset = tcp->payload_tok.offset;
  556. cfg->token_length = tcp->payload_tok.len;
  557. cfg->num_tokens =
  558. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  559. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  560. tcp->tokens_size);
  561. } else {
  562. /* set tokens to max value to almost never run out */
  563. cfg->num_tokens = cpu_to_le16(65535);
  564. }
  565. /* ACK (RX) */
  566. iwl_mvm_build_tcp_packet(
  567. vif, tcp, cfg->keepalive_ack_rx.data,
  568. cfg->keepalive_ack_rx.rx_mask,
  569. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  570. MVM_TCP_RX_ACK);
  571. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  572. /* WAKEUP (RX) */
  573. iwl_mvm_build_tcp_packet(
  574. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  575. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  576. MVM_TCP_RX_WAKE);
  577. cfg->wake_rx.info.tcp_payload_length =
  578. cpu_to_le16(tcp->wake_len);
  579. /* FIN */
  580. iwl_mvm_build_tcp_packet(
  581. vif, tcp, cfg->fin_tx.data, NULL,
  582. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  583. MVM_TCP_TX_FIN);
  584. cfg->fin_tx.info.tcp_payload_length = 0;
  585. ret = iwl_mvm_send_cmd(mvm, &cmd);
  586. kfree(cfg);
  587. return ret;
  588. }
  589. struct iwl_d3_iter_data {
  590. struct iwl_mvm *mvm;
  591. struct ieee80211_vif *vif;
  592. bool error;
  593. };
  594. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  595. struct ieee80211_vif *vif)
  596. {
  597. struct iwl_d3_iter_data *data = _data;
  598. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  599. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  600. return;
  601. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  602. return;
  603. if (data->vif) {
  604. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  605. data->error = true;
  606. return;
  607. }
  608. data->vif = vif;
  609. }
  610. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  611. struct ieee80211_sta *ap_sta)
  612. {
  613. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  614. struct ieee80211_chanctx_conf *ctx;
  615. u8 chains_static, chains_dynamic;
  616. struct cfg80211_chan_def chandef;
  617. int ret, i;
  618. struct iwl_binding_cmd binding_cmd = {};
  619. struct iwl_time_quota_cmd quota_cmd = {};
  620. u32 status;
  621. /* add back the PHY */
  622. if (WARN_ON(!mvmvif->phy_ctxt))
  623. return -EINVAL;
  624. rcu_read_lock();
  625. ctx = rcu_dereference(vif->chanctx_conf);
  626. if (WARN_ON(!ctx)) {
  627. rcu_read_unlock();
  628. return -EINVAL;
  629. }
  630. chandef = ctx->def;
  631. chains_static = ctx->rx_chains_static;
  632. chains_dynamic = ctx->rx_chains_dynamic;
  633. rcu_read_unlock();
  634. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  635. chains_static, chains_dynamic);
  636. if (ret)
  637. return ret;
  638. /* add back the MAC */
  639. mvmvif->uploaded = false;
  640. if (WARN_ON(!vif->bss_conf.assoc))
  641. return -EINVAL;
  642. /* hack */
  643. vif->bss_conf.assoc = false;
  644. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  645. vif->bss_conf.assoc = true;
  646. if (ret)
  647. return ret;
  648. /* add back binding - XXX refactor? */
  649. binding_cmd.id_and_color =
  650. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  651. mvmvif->phy_ctxt->color));
  652. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  653. binding_cmd.phy =
  654. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  655. mvmvif->phy_ctxt->color));
  656. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  657. mvmvif->color));
  658. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  659. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  660. status = 0;
  661. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  662. sizeof(binding_cmd), &binding_cmd,
  663. &status);
  664. if (ret) {
  665. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  666. return ret;
  667. }
  668. if (status) {
  669. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  670. return -EIO;
  671. }
  672. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  673. if (ret)
  674. return ret;
  675. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  676. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  677. if (ret)
  678. return ret;
  679. /* and some quota */
  680. quota_cmd.quotas[0].id_and_color =
  681. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  682. mvmvif->phy_ctxt->color));
  683. quota_cmd.quotas[0].quota = cpu_to_le32(100);
  684. quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
  685. for (i = 1; i < MAX_BINDINGS; i++)
  686. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  687. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  688. sizeof(quota_cmd), &quota_cmd);
  689. if (ret)
  690. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  691. return 0;
  692. }
  693. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  694. struct cfg80211_wowlan *wowlan,
  695. bool test)
  696. {
  697. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  698. struct iwl_d3_iter_data suspend_iter_data = {
  699. .mvm = mvm,
  700. };
  701. struct ieee80211_vif *vif;
  702. struct iwl_mvm_vif *mvmvif;
  703. struct ieee80211_sta *ap_sta;
  704. struct iwl_mvm_sta *mvm_ap_sta;
  705. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  706. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  707. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  708. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  709. /*
  710. * Program the minimum sleep time to 10 seconds, as many
  711. * platforms have issues processing a wakeup signal while
  712. * still being in the process of suspending.
  713. */
  714. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  715. };
  716. struct iwl_host_cmd d3_cfg_cmd = {
  717. .id = D3_CONFIG_CMD,
  718. .flags = CMD_SYNC | CMD_WANT_SKB,
  719. .data[0] = &d3_cfg_cmd_data,
  720. .len[0] = sizeof(d3_cfg_cmd_data),
  721. };
  722. struct wowlan_key_data key_data = {
  723. .use_rsc_tsc = false,
  724. .tkip = &tkip_cmd,
  725. .use_tkip = false,
  726. };
  727. int ret, i;
  728. int len __maybe_unused;
  729. u16 seq;
  730. u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
  731. if (!wowlan) {
  732. /*
  733. * mac80211 shouldn't get here, but for D3 test
  734. * it doesn't warrant a warning
  735. */
  736. WARN_ON(!test);
  737. return -EINVAL;
  738. }
  739. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  740. if (!key_data.rsc_tsc)
  741. return -ENOMEM;
  742. mutex_lock(&mvm->mutex);
  743. old_aux_sta_id = mvm->aux_sta.sta_id;
  744. /* see if there's only a single BSS vif and it's associated */
  745. ieee80211_iterate_active_interfaces_atomic(
  746. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  747. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  748. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  749. ret = 1;
  750. goto out_noreset;
  751. }
  752. vif = suspend_iter_data.vif;
  753. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  754. ap_sta = rcu_dereference_protected(
  755. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  756. lockdep_is_held(&mvm->mutex));
  757. if (IS_ERR_OR_NULL(ap_sta)) {
  758. ret = -EINVAL;
  759. goto out_noreset;
  760. }
  761. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  762. /*
  763. * The D3 firmware still hardcodes the AP station ID for the
  764. * BSS we're associated with as 0. Store the real STA ID here
  765. * and assign 0. When we leave this function, we'll restore
  766. * the original value for the resume code.
  767. */
  768. old_ap_sta_id = mvm_ap_sta->sta_id;
  769. mvm_ap_sta->sta_id = 0;
  770. mvmvif->ap_sta_id = 0;
  771. /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
  772. wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
  773. /*
  774. * We know the last used seqno, and the uCode expects to know that
  775. * one, it will increment before TX.
  776. */
  777. seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
  778. wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
  779. /*
  780. * For QoS counters, we store the one to use next, so subtract 0x10
  781. * since the uCode will add 0x10 *before* using the value while we
  782. * increment after using the value (i.e. store the next value to use).
  783. */
  784. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  785. seq = mvm_ap_sta->tid_data[i].seq_number;
  786. seq -= 0x10;
  787. wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
  788. }
  789. if (wowlan->disconnect)
  790. wowlan_config_cmd.wakeup_filter |=
  791. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  792. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  793. if (wowlan->magic_pkt)
  794. wowlan_config_cmd.wakeup_filter |=
  795. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  796. if (wowlan->gtk_rekey_failure)
  797. wowlan_config_cmd.wakeup_filter |=
  798. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  799. if (wowlan->eap_identity_req)
  800. wowlan_config_cmd.wakeup_filter |=
  801. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  802. if (wowlan->four_way_handshake)
  803. wowlan_config_cmd.wakeup_filter |=
  804. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  805. if (wowlan->n_patterns)
  806. wowlan_config_cmd.wakeup_filter |=
  807. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  808. if (wowlan->rfkill_release)
  809. wowlan_config_cmd.wakeup_filter |=
  810. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  811. if (wowlan->tcp) {
  812. /*
  813. * Set the "link change" (really "link lost") flag as well
  814. * since that implies losing the TCP connection.
  815. */
  816. wowlan_config_cmd.wakeup_filter |=
  817. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  818. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  819. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  820. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  821. }
  822. iwl_mvm_cancel_scan(mvm);
  823. iwl_trans_stop_device(mvm->trans);
  824. /*
  825. * Set the HW restart bit -- this is mostly true as we're
  826. * going to load new firmware and reprogram that, though
  827. * the reprogramming is going to be manual to avoid adding
  828. * all the MACs that aren't support.
  829. * We don't have to clear up everything though because the
  830. * reprogramming is manual. When we resume, we'll actually
  831. * go through a proper restart sequence again to switch
  832. * back to the runtime firmware image.
  833. */
  834. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  835. /* We reprogram keys and shouldn't allocate new key indices */
  836. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  837. mvm->ptk_ivlen = 0;
  838. mvm->ptk_icvlen = 0;
  839. mvm->ptk_ivlen = 0;
  840. mvm->ptk_icvlen = 0;
  841. /*
  842. * The D3 firmware still hardcodes the AP station ID for the
  843. * BSS we're associated with as 0. As a result, we have to move
  844. * the auxiliary station to ID 1 so the ID 0 remains free for
  845. * the AP station for later.
  846. * We set the sta_id to 1 here, and reset it to its previous
  847. * value (that we stored above) later.
  848. */
  849. mvm->aux_sta.sta_id = 1;
  850. ret = iwl_mvm_load_d3_fw(mvm);
  851. if (ret)
  852. goto out;
  853. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  854. if (ret)
  855. goto out;
  856. if (!iwlwifi_mod_params.sw_crypto) {
  857. /*
  858. * This needs to be unlocked due to lock ordering
  859. * constraints. Since we're in the suspend path
  860. * that isn't really a problem though.
  861. */
  862. mutex_unlock(&mvm->mutex);
  863. ieee80211_iter_keys(mvm->hw, vif,
  864. iwl_mvm_wowlan_program_keys,
  865. &key_data);
  866. mutex_lock(&mvm->mutex);
  867. if (key_data.error) {
  868. ret = -EIO;
  869. goto out;
  870. }
  871. if (key_data.use_rsc_tsc) {
  872. struct iwl_host_cmd rsc_tsc_cmd = {
  873. .id = WOWLAN_TSC_RSC_PARAM,
  874. .flags = CMD_SYNC,
  875. .data[0] = key_data.rsc_tsc,
  876. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  877. .len[0] = sizeof(*key_data.rsc_tsc),
  878. };
  879. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  880. if (ret)
  881. goto out;
  882. }
  883. if (key_data.use_tkip) {
  884. ret = iwl_mvm_send_cmd_pdu(mvm,
  885. WOWLAN_TKIP_PARAM,
  886. CMD_SYNC, sizeof(tkip_cmd),
  887. &tkip_cmd);
  888. if (ret)
  889. goto out;
  890. }
  891. if (mvmvif->rekey_data.valid) {
  892. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  893. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  894. NL80211_KCK_LEN);
  895. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  896. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  897. NL80211_KEK_LEN);
  898. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  899. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  900. ret = iwl_mvm_send_cmd_pdu(mvm,
  901. WOWLAN_KEK_KCK_MATERIAL,
  902. CMD_SYNC,
  903. sizeof(kek_kck_cmd),
  904. &kek_kck_cmd);
  905. if (ret)
  906. goto out;
  907. }
  908. }
  909. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
  910. CMD_SYNC, sizeof(wowlan_config_cmd),
  911. &wowlan_config_cmd);
  912. if (ret)
  913. goto out;
  914. ret = iwl_mvm_send_patterns(mvm, wowlan);
  915. if (ret)
  916. goto out;
  917. ret = iwl_mvm_send_proto_offload(mvm, vif);
  918. if (ret)
  919. goto out;
  920. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  921. if (ret)
  922. goto out;
  923. ret = iwl_mvm_power_update_mode(mvm, vif);
  924. if (ret)
  925. goto out;
  926. #ifdef CONFIG_IWLWIFI_DEBUGFS
  927. if (mvm->d3_wake_sysassert)
  928. d3_cfg_cmd_data.wakeup_flags |=
  929. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  930. #endif
  931. /* must be last -- this switches firmware state */
  932. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  933. if (ret)
  934. goto out;
  935. #ifdef CONFIG_IWLWIFI_DEBUGFS
  936. len = le32_to_cpu(d3_cfg_cmd.resp_pkt->len_n_flags) &
  937. FH_RSCSR_FRAME_SIZE_MSK;
  938. if (len >= sizeof(u32) * 2) {
  939. mvm->d3_test_pme_ptr =
  940. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  941. } else if (test) {
  942. /* in test mode we require the pointer */
  943. ret = -EIO;
  944. goto out;
  945. }
  946. #endif
  947. iwl_free_resp(&d3_cfg_cmd);
  948. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  949. iwl_trans_d3_suspend(mvm->trans, test);
  950. out:
  951. mvm->aux_sta.sta_id = old_aux_sta_id;
  952. mvm_ap_sta->sta_id = old_ap_sta_id;
  953. mvmvif->ap_sta_id = old_ap_sta_id;
  954. out_noreset:
  955. kfree(key_data.rsc_tsc);
  956. if (ret < 0)
  957. ieee80211_restart_hw(mvm->hw);
  958. mutex_unlock(&mvm->mutex);
  959. return ret;
  960. }
  961. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  962. {
  963. return __iwl_mvm_suspend(hw, wowlan, false);
  964. }
  965. static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  966. struct ieee80211_vif *vif)
  967. {
  968. u32 base = mvm->error_event_table;
  969. struct error_table_start {
  970. /* cf. struct iwl_error_event_table */
  971. u32 valid;
  972. u32 error_id;
  973. } err_info;
  974. struct cfg80211_wowlan_wakeup wakeup = {
  975. .pattern_idx = -1,
  976. };
  977. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  978. struct iwl_host_cmd cmd = {
  979. .id = WOWLAN_GET_STATUSES,
  980. .flags = CMD_SYNC | CMD_WANT_SKB,
  981. };
  982. struct iwl_wowlan_status *status;
  983. u32 reasons;
  984. int ret, len;
  985. struct sk_buff *pkt = NULL;
  986. iwl_trans_read_mem_bytes(mvm->trans, base,
  987. &err_info, sizeof(err_info));
  988. if (err_info.valid) {
  989. IWL_INFO(mvm, "error table is valid (%d)\n",
  990. err_info.valid);
  991. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  992. wakeup.rfkill_release = true;
  993. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  994. GFP_KERNEL);
  995. }
  996. return;
  997. }
  998. /* only for tracing for now */
  999. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
  1000. if (ret)
  1001. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1002. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1003. if (ret) {
  1004. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1005. return;
  1006. }
  1007. /* RF-kill already asserted again... */
  1008. if (!cmd.resp_pkt)
  1009. return;
  1010. len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  1011. if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
  1012. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1013. goto out;
  1014. }
  1015. status = (void *)cmd.resp_pkt->data;
  1016. if (len - sizeof(struct iwl_cmd_header) !=
  1017. sizeof(*status) +
  1018. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4)) {
  1019. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1020. goto out;
  1021. }
  1022. reasons = le32_to_cpu(status->wakeup_reasons);
  1023. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1024. wakeup_report = NULL;
  1025. goto report;
  1026. }
  1027. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1028. wakeup.magic_pkt = true;
  1029. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1030. wakeup.pattern_idx =
  1031. le16_to_cpu(status->pattern_number);
  1032. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1033. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1034. wakeup.disconnect = true;
  1035. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1036. wakeup.gtk_rekey_failure = true;
  1037. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1038. wakeup.rfkill_release = true;
  1039. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1040. wakeup.eap_identity_req = true;
  1041. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1042. wakeup.four_way_handshake = true;
  1043. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1044. wakeup.tcp_connlost = true;
  1045. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1046. wakeup.tcp_nomoretokens = true;
  1047. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1048. wakeup.tcp_match = true;
  1049. if (status->wake_packet_bufsize) {
  1050. int pktsize = le32_to_cpu(status->wake_packet_bufsize);
  1051. int pktlen = le32_to_cpu(status->wake_packet_length);
  1052. const u8 *pktdata = status->wake_packet;
  1053. struct ieee80211_hdr *hdr = (void *)pktdata;
  1054. int truncated = pktlen - pktsize;
  1055. /* this would be a firmware bug */
  1056. if (WARN_ON_ONCE(truncated < 0))
  1057. truncated = 0;
  1058. if (ieee80211_is_data(hdr->frame_control)) {
  1059. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1060. int ivlen = 0, icvlen = 4; /* also FCS */
  1061. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1062. if (!pkt)
  1063. goto report;
  1064. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1065. pktdata += hdrlen;
  1066. pktsize -= hdrlen;
  1067. if (ieee80211_has_protected(hdr->frame_control)) {
  1068. if (is_multicast_ether_addr(hdr->addr1)) {
  1069. ivlen = mvm->gtk_ivlen;
  1070. icvlen += mvm->gtk_icvlen;
  1071. } else {
  1072. ivlen = mvm->ptk_ivlen;
  1073. icvlen += mvm->ptk_icvlen;
  1074. }
  1075. }
  1076. /* if truncated, FCS/ICV is (partially) gone */
  1077. if (truncated >= icvlen) {
  1078. icvlen = 0;
  1079. truncated -= icvlen;
  1080. } else {
  1081. icvlen -= truncated;
  1082. truncated = 0;
  1083. }
  1084. pktsize -= ivlen + icvlen;
  1085. pktdata += ivlen;
  1086. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1087. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1088. goto report;
  1089. wakeup.packet = pkt->data;
  1090. wakeup.packet_present_len = pkt->len;
  1091. wakeup.packet_len = pkt->len - truncated;
  1092. wakeup.packet_80211 = false;
  1093. } else {
  1094. int fcslen = 4;
  1095. if (truncated >= 4) {
  1096. truncated -= 4;
  1097. fcslen = 0;
  1098. } else {
  1099. fcslen -= truncated;
  1100. truncated = 0;
  1101. }
  1102. pktsize -= fcslen;
  1103. wakeup.packet = status->wake_packet;
  1104. wakeup.packet_present_len = pktsize;
  1105. wakeup.packet_len = pktlen - truncated;
  1106. wakeup.packet_80211 = true;
  1107. }
  1108. }
  1109. report:
  1110. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1111. kfree_skb(pkt);
  1112. out:
  1113. iwl_free_resp(&cmd);
  1114. }
  1115. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1116. {
  1117. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1118. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1119. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1120. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1121. if (!mvm->store_d3_resume_sram)
  1122. return;
  1123. if (!mvm->d3_resume_sram) {
  1124. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1125. if (!mvm->d3_resume_sram)
  1126. return;
  1127. }
  1128. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1129. #endif
  1130. }
  1131. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1132. {
  1133. struct iwl_d3_iter_data resume_iter_data = {
  1134. .mvm = mvm,
  1135. };
  1136. struct ieee80211_vif *vif = NULL;
  1137. int ret;
  1138. enum iwl_d3_status d3_status;
  1139. mutex_lock(&mvm->mutex);
  1140. /* get the BSS vif pointer again */
  1141. ieee80211_iterate_active_interfaces_atomic(
  1142. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1143. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  1144. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  1145. goto out_unlock;
  1146. vif = resume_iter_data.vif;
  1147. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
  1148. if (ret)
  1149. goto out_unlock;
  1150. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1151. IWL_INFO(mvm, "Device was reset during suspend\n");
  1152. goto out_unlock;
  1153. }
  1154. /* query SRAM first in case we want event logging */
  1155. iwl_mvm_read_d3_sram(mvm);
  1156. iwl_mvm_query_wakeup_reasons(mvm, vif);
  1157. out_unlock:
  1158. mutex_unlock(&mvm->mutex);
  1159. if (!test && vif)
  1160. ieee80211_resume_disconnect(vif);
  1161. /* return 1 to reconfigure the device */
  1162. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1163. return 1;
  1164. }
  1165. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1166. {
  1167. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1168. return __iwl_mvm_resume(mvm, false);
  1169. }
  1170. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1171. {
  1172. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1173. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1174. }
  1175. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1176. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1177. {
  1178. struct iwl_mvm *mvm = inode->i_private;
  1179. int err;
  1180. if (mvm->d3_test_active)
  1181. return -EBUSY;
  1182. file->private_data = inode->i_private;
  1183. ieee80211_stop_queues(mvm->hw);
  1184. synchronize_net();
  1185. /* start pseudo D3 */
  1186. rtnl_lock();
  1187. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1188. rtnl_unlock();
  1189. if (err > 0)
  1190. err = -EINVAL;
  1191. if (err) {
  1192. ieee80211_wake_queues(mvm->hw);
  1193. return err;
  1194. }
  1195. mvm->d3_test_active = true;
  1196. return 0;
  1197. }
  1198. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1199. size_t count, loff_t *ppos)
  1200. {
  1201. struct iwl_mvm *mvm = file->private_data;
  1202. u32 pme_asserted;
  1203. while (true) {
  1204. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1205. mvm->d3_test_pme_ptr);
  1206. if (pme_asserted)
  1207. break;
  1208. if (msleep_interruptible(100))
  1209. break;
  1210. }
  1211. return 0;
  1212. }
  1213. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1214. struct ieee80211_vif *vif)
  1215. {
  1216. if (vif->type == NL80211_IFTYPE_STATION)
  1217. ieee80211_connection_loss(vif);
  1218. }
  1219. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1220. {
  1221. struct iwl_mvm *mvm = inode->i_private;
  1222. int remaining_time = 10;
  1223. mvm->d3_test_active = false;
  1224. __iwl_mvm_resume(mvm, true);
  1225. iwl_abort_notification_waits(&mvm->notif_wait);
  1226. ieee80211_restart_hw(mvm->hw);
  1227. /* wait for restart and disconnect all interfaces */
  1228. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1229. remaining_time > 0) {
  1230. remaining_time--;
  1231. msleep(1000);
  1232. }
  1233. if (remaining_time == 0)
  1234. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1235. ieee80211_iterate_active_interfaces_atomic(
  1236. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1237. iwl_mvm_d3_test_disconn_work_iter, NULL);
  1238. ieee80211_wake_queues(mvm->hw);
  1239. return 0;
  1240. }
  1241. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1242. .llseek = no_llseek,
  1243. .open = iwl_mvm_d3_test_open,
  1244. .read = iwl_mvm_d3_test_read,
  1245. .release = iwl_mvm_d3_test_release,
  1246. };
  1247. #endif