d3.c 54 KB

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