d3.c 38 KB

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