d3.c 43 KB

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