1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2025 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7 #include <linux/etherdevice.h>
8 #include <linux/ip.h>
9 #include <linux/fs.h>
10 #include <net/cfg80211.h>
11 #include <net/ipv6.h>
12 #include <net/tcp.h>
13 #include <net/addrconf.h>
14 #include "iwl-modparams.h"
15 #include "fw-api.h"
16 #include "mvm.h"
17 #include "fw/img.h"
18
iwl_mvm_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)19 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
20 struct ieee80211_vif *vif,
21 struct cfg80211_gtk_rekey_data *data)
22 {
23 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
24 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
25
26 mutex_lock(&mvm->mutex);
27
28 mvmvif->rekey_data.kek_len = data->kek_len;
29 mvmvif->rekey_data.kck_len = data->kck_len;
30 memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
31 memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
32 mvmvif->rekey_data.akm = data->akm & 0xFF;
33 mvmvif->rekey_data.replay_ctr =
34 cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
35 mvmvif->rekey_data.valid = true;
36
37 mutex_unlock(&mvm->mutex);
38 }
39
40 #if IS_ENABLED(CONFIG_IPV6)
iwl_mvm_ipv6_addr_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)41 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
42 struct ieee80211_vif *vif,
43 struct inet6_dev *idev)
44 {
45 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
46 struct inet6_ifaddr *ifa;
47 int idx = 0;
48
49 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
50
51 read_lock_bh(&idev->lock);
52 list_for_each_entry(ifa, &idev->addr_list, if_list) {
53 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
54 if (ifa->flags & IFA_F_TENTATIVE)
55 __set_bit(idx, mvmvif->tentative_addrs);
56 idx++;
57 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
58 break;
59 }
60 read_unlock_bh(&idev->lock);
61
62 mvmvif->num_target_ipv6_addrs = idx;
63 }
64 #endif
65
iwl_mvm_set_default_unicast_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int idx)66 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
67 struct ieee80211_vif *vif, int idx)
68 {
69 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
70
71 mvmvif->tx_key_idx = idx;
72 }
73
iwl_mvm_convert_p1k(u16 * p1k,__le16 * out)74 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
75 {
76 int i;
77
78 for (i = 0; i < IWL_P1K_SIZE; i++)
79 out[i] = cpu_to_le16(p1k[i]);
80 }
81
iwl_mvm_find_max_pn(struct ieee80211_key_conf * key,struct iwl_mvm_key_pn * ptk_pn,struct ieee80211_key_seq * seq,int tid,int queues)82 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
83 struct iwl_mvm_key_pn *ptk_pn,
84 struct ieee80211_key_seq *seq,
85 int tid, int queues)
86 {
87 const u8 *ret = seq->ccmp.pn;
88 int i;
89
90 /* get the PN from mac80211, used on the default queue */
91 ieee80211_get_key_rx_seq(key, tid, seq);
92
93 /* and use the internal data for the other queues */
94 for (i = 1; i < queues; i++) {
95 const u8 *tmp = ptk_pn->q[i].pn[tid];
96
97 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
98 ret = tmp;
99 }
100
101 return ret;
102 }
103
104 struct wowlan_key_reprogram_data {
105 bool error;
106 int wep_key_idx;
107 };
108
iwl_mvm_wowlan_program_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)109 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
110 struct ieee80211_vif *vif,
111 struct ieee80211_sta *sta,
112 struct ieee80211_key_conf *key,
113 void *_data)
114 {
115 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
117 struct wowlan_key_reprogram_data *data = _data;
118 int ret;
119
120 switch (key->cipher) {
121 case WLAN_CIPHER_SUITE_WEP40:
122 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
123 DEFINE_RAW_FLEX(struct iwl_mvm_wep_key_cmd, wkc, wep_key, 1);
124 struct iwl_mvm_wep_key *wep_key = wkc->wep_key;
125
126 wkc->mac_id_n_color =
127 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
128 mvmvif->color));
129 wkc->num_keys = 1;
130 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
131 wkc->decryption_type = STA_KEY_FLG_WEP;
132 wep_key->key_index = key->keyidx;
133 wep_key->key_size = key->keylen;
134
135 /*
136 * This will fail -- the key functions don't set support
137 * pairwise WEP keys. However, that's better than silently
138 * failing WoWLAN. Or maybe not?
139 */
140 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
141 break;
142
143 memcpy(&wep_key->key[3], key->key, key->keylen);
144 if (key->keyidx == mvmvif->tx_key_idx) {
145 /* TX key must be at offset 0 */
146 wep_key->key_offset = 0;
147 } else {
148 /* others start at 1 */
149 data->wep_key_idx++;
150 wep_key->key_offset = data->wep_key_idx;
151 }
152
153 mutex_lock(&mvm->mutex);
154 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
155 __struct_size(wkc), wkc);
156 data->error = ret != 0;
157
158 mvm->ptk_ivlen = key->iv_len;
159 mvm->ptk_icvlen = key->icv_len;
160 mvm->gtk_ivlen = key->iv_len;
161 mvm->gtk_icvlen = key->icv_len;
162 mutex_unlock(&mvm->mutex);
163
164 /* don't upload key again */
165 return;
166 }
167 default:
168 data->error = true;
169 return;
170 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
171 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
172 return;
173 case WLAN_CIPHER_SUITE_AES_CMAC:
174 /*
175 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
176 * but we also shouldn't abort suspend due to that. It does have
177 * support for the IGTK key renewal, but doesn't really use the
178 * IGTK for anything. This means we could spuriously wake up or
179 * be deauthenticated, but that was considered acceptable.
180 */
181 return;
182 case WLAN_CIPHER_SUITE_TKIP:
183 case WLAN_CIPHER_SUITE_CCMP:
184 case WLAN_CIPHER_SUITE_GCMP:
185 case WLAN_CIPHER_SUITE_GCMP_256:
186 break;
187 }
188
189 mutex_lock(&mvm->mutex);
190 /*
191 * The D3 firmware hardcodes the key offset 0 as the key it
192 * uses to transmit packets to the AP, i.e. the PTK.
193 */
194 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
195 mvm->ptk_ivlen = key->iv_len;
196 mvm->ptk_icvlen = key->icv_len;
197 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
198 } else {
199 /*
200 * firmware only supports TSC/RSC for a single key,
201 * so if there are multiple keep overwriting them
202 * with new ones -- this relies on mac80211 doing
203 * list_add_tail().
204 */
205 mvm->gtk_ivlen = key->iv_len;
206 mvm->gtk_icvlen = key->icv_len;
207 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
208 }
209 mutex_unlock(&mvm->mutex);
210 data->error = ret != 0;
211 }
212
213 struct wowlan_key_rsc_tsc_data {
214 struct iwl_wowlan_rsc_tsc_params_cmd_ver_2 *rsc_tsc;
215 bool have_rsc_tsc;
216 };
217
iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)218 static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
219 struct ieee80211_vif *vif,
220 struct ieee80211_sta *sta,
221 struct ieee80211_key_conf *key,
222 void *_data)
223 {
224 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
225 struct wowlan_key_rsc_tsc_data *data = _data;
226 struct aes_sc *aes_sc;
227 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
228 struct ieee80211_key_seq seq;
229 int i;
230
231 switch (key->cipher) {
232 default:
233 break;
234 case WLAN_CIPHER_SUITE_TKIP:
235 if (sta) {
236 u64 pn64;
237
238 tkip_sc =
239 data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
240 tkip_tx_sc =
241 &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
242
243 pn64 = atomic64_read(&key->tx_pn);
244 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
245 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
246 } else {
247 tkip_sc =
248 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
249 }
250
251 /*
252 * For non-QoS this relies on the fact that both the uCode and
253 * mac80211 use TID 0 (as they need to avoid replay attacks)
254 * for checking the IV in the frames.
255 */
256 for (i = 0; i < IWL_NUM_RSC; i++) {
257 ieee80211_get_key_rx_seq(key, i, &seq);
258 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
259 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
260 }
261
262 data->have_rsc_tsc = true;
263 break;
264 case WLAN_CIPHER_SUITE_CCMP:
265 case WLAN_CIPHER_SUITE_GCMP:
266 case WLAN_CIPHER_SUITE_GCMP_256:
267 if (sta) {
268 struct aes_sc *aes_tx_sc;
269 u64 pn64;
270
271 aes_sc =
272 data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
273 aes_tx_sc =
274 &data->rsc_tsc->all_tsc_rsc.aes.tsc;
275
276 pn64 = atomic64_read(&key->tx_pn);
277 aes_tx_sc->pn = cpu_to_le64(pn64);
278 } else {
279 aes_sc =
280 data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
281 }
282
283 /*
284 * For non-QoS this relies on the fact that both the uCode and
285 * mac80211/our RX code use TID 0 for checking the PN.
286 */
287 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
288 struct iwl_mvm_sta *mvmsta;
289 struct iwl_mvm_key_pn *ptk_pn;
290 const u8 *pn;
291
292 mvmsta = iwl_mvm_sta_from_mac80211(sta);
293 rcu_read_lock();
294 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
295 if (WARN_ON(!ptk_pn)) {
296 rcu_read_unlock();
297 break;
298 }
299
300 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
301 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
302 mvm->trans->info.num_rxqs);
303 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
304 ((u64)pn[4] << 8) |
305 ((u64)pn[3] << 16) |
306 ((u64)pn[2] << 24) |
307 ((u64)pn[1] << 32) |
308 ((u64)pn[0] << 40));
309 }
310
311 rcu_read_unlock();
312 } else {
313 for (i = 0; i < IWL_NUM_RSC; i++) {
314 u8 *pn = seq.ccmp.pn;
315
316 ieee80211_get_key_rx_seq(key, i, &seq);
317 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
318 ((u64)pn[4] << 8) |
319 ((u64)pn[3] << 16) |
320 ((u64)pn[2] << 24) |
321 ((u64)pn[1] << 32) |
322 ((u64)pn[0] << 40));
323 }
324 }
325 data->have_rsc_tsc = true;
326 break;
327 }
328 }
329
330 struct wowlan_key_rsc_v5_data {
331 struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
332 bool have_rsc;
333 int gtks;
334 int gtk_ids[4];
335 };
336
iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)337 static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
338 struct ieee80211_vif *vif,
339 struct ieee80211_sta *sta,
340 struct ieee80211_key_conf *key,
341 void *_data)
342 {
343 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
344 struct wowlan_key_rsc_v5_data *data = _data;
345 struct ieee80211_key_seq seq;
346 __le64 *rsc;
347 int i;
348
349 /* only for ciphers that can be PTK/GTK */
350 switch (key->cipher) {
351 default:
352 return;
353 case WLAN_CIPHER_SUITE_TKIP:
354 case WLAN_CIPHER_SUITE_CCMP:
355 case WLAN_CIPHER_SUITE_GCMP:
356 case WLAN_CIPHER_SUITE_GCMP_256:
357 break;
358 }
359
360 if (sta) {
361 rsc = data->rsc->ucast_rsc;
362 } else {
363 if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
364 return;
365 data->gtk_ids[data->gtks] = key->keyidx;
366 rsc = data->rsc->mcast_rsc[data->gtks % 2];
367 if (WARN_ON(key->keyidx >=
368 ARRAY_SIZE(data->rsc->mcast_key_id_map)))
369 return;
370 data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
371 if (data->gtks >= 2) {
372 int prev = data->gtks - 2;
373 int prev_idx = data->gtk_ids[prev];
374
375 data->rsc->mcast_key_id_map[prev_idx] =
376 IWL_MCAST_KEY_MAP_INVALID;
377 }
378 data->gtks++;
379 }
380
381 switch (key->cipher) {
382 default:
383 WARN_ON(1);
384 break;
385 case WLAN_CIPHER_SUITE_TKIP:
386
387 /*
388 * For non-QoS this relies on the fact that both the uCode and
389 * mac80211 use TID 0 (as they need to avoid replay attacks)
390 * for checking the IV in the frames.
391 */
392 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
393 ieee80211_get_key_rx_seq(key, i, &seq);
394
395 rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
396 seq.tkip.iv16);
397 }
398
399 data->have_rsc = true;
400 break;
401 case WLAN_CIPHER_SUITE_CCMP:
402 case WLAN_CIPHER_SUITE_GCMP:
403 case WLAN_CIPHER_SUITE_GCMP_256:
404 /*
405 * For non-QoS this relies on the fact that both the uCode and
406 * mac80211/our RX code use TID 0 for checking the PN.
407 */
408 if (sta) {
409 struct iwl_mvm_sta *mvmsta;
410 struct iwl_mvm_key_pn *ptk_pn;
411 const u8 *pn;
412
413 mvmsta = iwl_mvm_sta_from_mac80211(sta);
414 rcu_read_lock();
415 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
416 if (WARN_ON(!ptk_pn)) {
417 rcu_read_unlock();
418 break;
419 }
420
421 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
422 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
423 mvm->trans->info.num_rxqs);
424 rsc[i] = cpu_to_le64((u64)pn[5] |
425 ((u64)pn[4] << 8) |
426 ((u64)pn[3] << 16) |
427 ((u64)pn[2] << 24) |
428 ((u64)pn[1] << 32) |
429 ((u64)pn[0] << 40));
430 }
431
432 rcu_read_unlock();
433 } else {
434 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
435 u8 *pn = seq.ccmp.pn;
436
437 ieee80211_get_key_rx_seq(key, i, &seq);
438 rsc[i] = cpu_to_le64((u64)pn[5] |
439 ((u64)pn[4] << 8) |
440 ((u64)pn[3] << 16) |
441 ((u64)pn[2] << 24) |
442 ((u64)pn[1] << 32) |
443 ((u64)pn[0] << 40));
444 }
445 }
446 data->have_rsc = true;
447 break;
448 }
449 }
450
iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)451 static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
452 struct ieee80211_vif *vif,
453 struct iwl_mvm_vif_link_info *mvm_link)
454 {
455 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
456 IWL_FW_CMD_VER_UNKNOWN);
457 int ret;
458
459 if (ver == 5) {
460 struct wowlan_key_rsc_v5_data data = {};
461 int i;
462
463 data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
464 if (!data.rsc)
465 return -ENOMEM;
466
467 for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
468 data.rsc->mcast_key_id_map[i] =
469 IWL_MCAST_KEY_MAP_INVALID;
470 data.rsc->sta_id = cpu_to_le32(mvm_link->ap_sta_id);
471
472 ieee80211_iter_keys(mvm->hw, vif,
473 iwl_mvm_wowlan_get_rsc_v5_data,
474 &data);
475
476 if (data.have_rsc)
477 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
478 CMD_ASYNC, sizeof(*data.rsc),
479 data.rsc);
480 else
481 ret = 0;
482 kfree(data.rsc);
483 } else if (ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
484 struct wowlan_key_rsc_tsc_data data = {};
485
486 data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
487 if (!data.rsc_tsc)
488 return -ENOMEM;
489
490 ieee80211_iter_keys(mvm->hw, vif,
491 iwl_mvm_wowlan_get_rsc_tsc_data,
492 &data);
493
494 if (data.have_rsc_tsc)
495 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
496 CMD_ASYNC,
497 sizeof(*data.rsc_tsc),
498 data.rsc_tsc);
499 else
500 ret = 0;
501 kfree(data.rsc_tsc);
502 } else {
503 ret = 0;
504 WARN_ON_ONCE(1);
505 }
506
507 return ret;
508 }
509
510 struct wowlan_key_tkip_data {
511 struct iwl_wowlan_tkip_params_cmd tkip;
512 bool have_tkip_keys;
513 };
514
iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)515 static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
516 struct ieee80211_vif *vif,
517 struct ieee80211_sta *sta,
518 struct ieee80211_key_conf *key,
519 void *_data)
520 {
521 struct wowlan_key_tkip_data *data = _data;
522 struct iwl_p1k_cache *rx_p1ks;
523 u8 *rx_mic_key;
524 struct ieee80211_key_seq seq;
525 u32 cur_rx_iv32 = 0;
526 u16 p1k[IWL_P1K_SIZE];
527 int i;
528
529 switch (key->cipher) {
530 default:
531 break;
532 case WLAN_CIPHER_SUITE_TKIP:
533 if (sta) {
534 u64 pn64;
535
536 rx_p1ks = data->tkip.rx_uni;
537
538 pn64 = atomic64_read(&key->tx_pn);
539
540 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
541 p1k);
542 iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
543
544 memcpy(data->tkip.mic_keys.tx,
545 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
546 IWL_MIC_KEY_SIZE);
547
548 rx_mic_key = data->tkip.mic_keys.rx_unicast;
549 } else {
550 rx_p1ks = data->tkip.rx_multi;
551 rx_mic_key = data->tkip.mic_keys.rx_mcast;
552 }
553
554 for (i = 0; i < IWL_NUM_RSC; i++) {
555 ieee80211_get_key_rx_seq(key, i, &seq);
556 /* wrapping isn't allowed, AP must rekey */
557 if (seq.tkip.iv32 > cur_rx_iv32)
558 cur_rx_iv32 = seq.tkip.iv32;
559 }
560
561 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
562 cur_rx_iv32, p1k);
563 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
564 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
565 cur_rx_iv32 + 1, p1k);
566 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
567
568 memcpy(rx_mic_key,
569 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
570 IWL_MIC_KEY_SIZE);
571
572 data->have_tkip_keys = true;
573 break;
574 }
575 }
576
577 struct wowlan_key_gtk_type_iter {
578 struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
579 };
580
iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)581 static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
582 struct ieee80211_vif *vif,
583 struct ieee80211_sta *sta,
584 struct ieee80211_key_conf *key,
585 void *_data)
586 {
587 struct wowlan_key_gtk_type_iter *data = _data;
588 __le32 *cipher = NULL;
589
590 if (key->keyidx == 4 || key->keyidx == 5)
591 cipher = &data->kek_kck_cmd->igtk_cipher;
592 if (key->keyidx == 6 || key->keyidx == 7)
593 cipher = &data->kek_kck_cmd->bigtk_cipher;
594
595 switch (key->cipher) {
596 default:
597 return;
598 case WLAN_CIPHER_SUITE_TKIP:
599 if (!sta)
600 data->kek_kck_cmd->gtk_cipher =
601 cpu_to_le32(STA_KEY_FLG_TKIP);
602 return;
603 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
604 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
605 if (cipher)
606 *cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
607 return;
608 case WLAN_CIPHER_SUITE_AES_CMAC:
609 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
610 if (cipher)
611 *cipher = cpu_to_le32(STA_KEY_FLG_CCM);
612 return;
613 case WLAN_CIPHER_SUITE_CCMP:
614 if (!sta)
615 data->kek_kck_cmd->gtk_cipher =
616 cpu_to_le32(STA_KEY_FLG_CCM);
617 return;
618 case WLAN_CIPHER_SUITE_GCMP:
619 case WLAN_CIPHER_SUITE_GCMP_256:
620 if (!sta)
621 data->kek_kck_cmd->gtk_cipher =
622 cpu_to_le32(STA_KEY_FLG_GCMP);
623 return;
624 }
625 }
626
iwl_mvm_send_patterns_v1(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan)627 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
628 struct cfg80211_wowlan *wowlan)
629 {
630 struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
631 struct iwl_host_cmd cmd = {
632 .id = WOWLAN_PATTERNS,
633 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
634 };
635 int i, err;
636
637 if (!wowlan->n_patterns)
638 return 0;
639
640 cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
641
642 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
643 if (!pattern_cmd)
644 return -ENOMEM;
645
646 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
647
648 for (i = 0; i < wowlan->n_patterns; i++) {
649 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
650
651 memcpy(&pattern_cmd->patterns[i].mask,
652 wowlan->patterns[i].mask, mask_len);
653 memcpy(&pattern_cmd->patterns[i].pattern,
654 wowlan->patterns[i].pattern,
655 wowlan->patterns[i].pattern_len);
656 pattern_cmd->patterns[i].mask_size = mask_len;
657 pattern_cmd->patterns[i].pattern_size =
658 wowlan->patterns[i].pattern_len;
659 }
660
661 cmd.data[0] = pattern_cmd;
662 err = iwl_mvm_send_cmd(mvm, &cmd);
663 kfree(pattern_cmd);
664 return err;
665 }
666
iwl_mvm_send_patterns(struct iwl_mvm * mvm,struct iwl_mvm_vif_link_info * mvm_link,struct cfg80211_wowlan * wowlan)667 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
668 struct iwl_mvm_vif_link_info *mvm_link,
669 struct cfg80211_wowlan *wowlan)
670 {
671 struct iwl_wowlan_patterns_cmd *pattern_cmd;
672 struct iwl_host_cmd cmd = {
673 .id = WOWLAN_PATTERNS,
674 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
675 };
676 int i, err;
677 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
678 IWL_FW_CMD_VER_UNKNOWN);
679
680 if (!wowlan->n_patterns)
681 return 0;
682
683 cmd.len[0] = sizeof(*pattern_cmd) +
684 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
685
686 pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
687 if (!pattern_cmd)
688 return -ENOMEM;
689
690 pattern_cmd->n_patterns = wowlan->n_patterns;
691 if (ver >= 3)
692 pattern_cmd->sta_id = mvm_link->ap_sta_id;
693
694 for (i = 0; i < wowlan->n_patterns; i++) {
695 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
696
697 pattern_cmd->patterns[i].pattern_type =
698 WOWLAN_PATTERN_TYPE_BITMASK;
699
700 memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
701 wowlan->patterns[i].mask, mask_len);
702 memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
703 wowlan->patterns[i].pattern,
704 wowlan->patterns[i].pattern_len);
705 pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
706 pattern_cmd->patterns[i].u.bitmask.pattern_size =
707 wowlan->patterns[i].pattern_len;
708 }
709
710 cmd.data[0] = pattern_cmd;
711 err = iwl_mvm_send_cmd(mvm, &cmd);
712 kfree(pattern_cmd);
713 return err;
714 }
715
iwl_mvm_d3_reprogram(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * ap_sta)716 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
717 struct ieee80211_sta *ap_sta)
718 {
719 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
720 struct ieee80211_chanctx_conf *ctx;
721 u8 chains_static, chains_dynamic;
722 struct cfg80211_chan_def chandef, ap_def;
723 int ret, i;
724 struct iwl_binding_cmd_v1 binding_cmd = {};
725 struct iwl_time_quota_cmd quota_cmd = {};
726 struct iwl_time_quota_data *quota;
727 u32 status;
728
729 if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm) ||
730 ieee80211_vif_is_mld(vif)))
731 return -EINVAL;
732
733 /* add back the PHY */
734 if (WARN_ON(!mvmvif->deflink.phy_ctxt))
735 return -EINVAL;
736
737 rcu_read_lock();
738 ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
739 if (WARN_ON(!ctx)) {
740 rcu_read_unlock();
741 return -EINVAL;
742 }
743 chandef = ctx->def;
744 ap_def = ctx->ap;
745 chains_static = ctx->rx_chains_static;
746 chains_dynamic = ctx->rx_chains_dynamic;
747 rcu_read_unlock();
748
749 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
750 &ap_def, chains_static, chains_dynamic);
751 if (ret)
752 return ret;
753
754 /* add back the MAC */
755 mvmvif->uploaded = false;
756
757 if (WARN_ON(!vif->cfg.assoc))
758 return -EINVAL;
759
760 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
761 if (ret)
762 return ret;
763
764 /* add back binding - XXX refactor? */
765 binding_cmd.id_and_color =
766 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
767 mvmvif->deflink.phy_ctxt->color));
768 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
769 binding_cmd.phy =
770 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
771 mvmvif->deflink.phy_ctxt->color));
772 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
773 mvmvif->color));
774 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
775 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
776
777 status = 0;
778 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
779 IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
780 &status);
781 if (ret) {
782 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
783 return ret;
784 }
785
786 if (status) {
787 IWL_ERR(mvm, "Binding command failed: %u\n", status);
788 return -EIO;
789 }
790
791 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
792 if (ret)
793 return ret;
794 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
795 ap_sta);
796
797 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
798 if (ret)
799 return ret;
800
801 /* and some quota */
802 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, 0);
803 quota->id_and_color =
804 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
805 mvmvif->deflink.phy_ctxt->color));
806 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
807 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
808
809 for (i = 1; i < MAX_BINDINGS; i++) {
810 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, i);
811 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
812 }
813
814 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
815 iwl_mvm_quota_cmd_size(mvm), "a_cmd);
816 if (ret)
817 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
818
819 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm, false))
820 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
821
822 return 0;
823 }
824
iwl_mvm_get_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)825 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
826 struct ieee80211_vif *vif)
827 {
828 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
829 struct iwl_nonqos_seq_query_cmd query_cmd = {
830 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
831 .mac_id_n_color =
832 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
833 mvmvif->color)),
834 };
835 struct iwl_host_cmd cmd = {
836 .id = NON_QOS_TX_COUNTER_CMD,
837 .flags = CMD_WANT_SKB,
838 };
839 int err;
840 u32 size;
841
842 cmd.data[0] = &query_cmd;
843 cmd.len[0] = sizeof(query_cmd);
844
845 err = iwl_mvm_send_cmd(mvm, &cmd);
846 if (err)
847 return err;
848
849 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
850 if (size < sizeof(__le16)) {
851 err = -EINVAL;
852 } else {
853 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
854 /* firmware returns next, not last-used seqno */
855 err = (u16) (err - 0x10);
856 }
857
858 iwl_free_resp(&cmd);
859 return err;
860 }
861
iwl_mvm_set_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)862 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
863 {
864 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
865 struct iwl_nonqos_seq_query_cmd query_cmd = {
866 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
867 .mac_id_n_color =
868 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
869 mvmvif->color)),
870 .value = cpu_to_le16(mvmvif->seqno),
871 };
872
873 /* return if called during restart, not resume from D3 */
874 if (!mvmvif->seqno_valid)
875 return;
876
877 mvmvif->seqno_valid = false;
878
879 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
880 sizeof(query_cmd), &query_cmd))
881 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
882 }
883
iwl_mvm_switch_to_d3(struct iwl_mvm * mvm)884 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
885 {
886 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
887
888 iwl_mvm_stop_device(mvm);
889 /*
890 * Set the HW restart bit -- this is mostly true as we're
891 * going to load new firmware and reprogram that, though
892 * the reprogramming is going to be manual to avoid adding
893 * all the MACs that aren't support.
894 * We don't have to clear up everything though because the
895 * reprogramming is manual. When we resume, we'll actually
896 * go through a proper restart sequence again to switch
897 * back to the runtime firmware image.
898 */
899 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
900
901 /* the fw is reset, so all the keys are cleared */
902 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
903
904 mvm->ptk_ivlen = 0;
905 mvm->ptk_icvlen = 0;
906 mvm->ptk_ivlen = 0;
907 mvm->ptk_icvlen = 0;
908
909 return iwl_mvm_load_d3_fw(mvm);
910 }
911
912 static int
iwl_mvm_get_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd_v6 * wowlan_config_cmd,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct ieee80211_sta * ap_sta)913 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
914 struct cfg80211_wowlan *wowlan,
915 struct iwl_wowlan_config_cmd_v6 *wowlan_config_cmd,
916 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
917 struct ieee80211_sta *ap_sta)
918 {
919 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
920
921 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
922
923 wowlan_config_cmd->is_11n_connection =
924 ap_sta->deflink.ht_cap.ht_supported;
925 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
926 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
927
928 if (ap_sta->mfp)
929 wowlan_config_cmd->flags |= IS_11W_ASSOC;
930
931 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
932 /* Query the last used seqno and set it */
933 int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
934
935 if (ret < 0)
936 return ret;
937
938 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
939 }
940
941 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 7)
942 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
943
944 if (wowlan->disconnect)
945 wowlan_config_cmd->wakeup_filter |=
946 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
947 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
948 if (wowlan->magic_pkt)
949 wowlan_config_cmd->wakeup_filter |=
950 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
951 if (wowlan->gtk_rekey_failure)
952 wowlan_config_cmd->wakeup_filter |=
953 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
954 if (wowlan->eap_identity_req)
955 wowlan_config_cmd->wakeup_filter |=
956 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
957 if (wowlan->four_way_handshake)
958 wowlan_config_cmd->wakeup_filter |=
959 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
960 if (wowlan->n_patterns)
961 wowlan_config_cmd->wakeup_filter |=
962 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
963
964 if (wowlan->rfkill_release)
965 wowlan_config_cmd->wakeup_filter |=
966 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
967
968 if (wowlan->tcp) {
969 /*
970 * Set the "link change" (really "link lost") flag as well
971 * since that implies losing the TCP connection.
972 */
973 wowlan_config_cmd->wakeup_filter |=
974 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
975 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
976 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
977 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
978 }
979
980 if (wowlan->any) {
981 wowlan_config_cmd->wakeup_filter |=
982 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
983 IWL_WOWLAN_WAKEUP_LINK_CHANGE |
984 IWL_WOWLAN_WAKEUP_RX_FRAME |
985 IWL_WOWLAN_WAKEUP_BCN_FILTERING);
986 }
987
988 return 0;
989 }
990
iwl_mvm_wowlan_config_key_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)991 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
992 struct ieee80211_vif *vif,
993 struct iwl_mvm_vif_link_info *mvm_link)
994 {
995 bool unified = fw_has_capa(&mvm->fw->ucode_capa,
996 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
997 struct wowlan_key_reprogram_data key_data = {};
998 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
999 int ret;
1000 u8 cmd_ver;
1001 size_t cmd_size;
1002
1003 if (!unified) {
1004 /*
1005 * if we have to configure keys, call ieee80211_iter_keys(),
1006 * as we need non-atomic context in order to take the
1007 * required locks.
1008 */
1009 /*
1010 * Note that currently we don't use CMD_ASYNC in the iterator.
1011 * In case of key_data.configure_keys, all the configured
1012 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1013 * take care of locking/unlocking mvm->mutex.
1014 */
1015 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1016 &key_data);
1017
1018 if (key_data.error)
1019 return -EIO;
1020 }
1021
1022 ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif, mvm_link);
1023 if (ret)
1024 return ret;
1025
1026 if (!fw_has_api(&mvm->fw->ucode_capa,
1027 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1028 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1029 IWL_FW_CMD_VER_UNKNOWN);
1030 struct wowlan_key_tkip_data tkip_data = {};
1031 int size;
1032
1033 if (ver == 2) {
1034 size = sizeof(tkip_data.tkip);
1035 tkip_data.tkip.sta_id =
1036 cpu_to_le32(mvm_link->ap_sta_id);
1037 } else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1038 size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1039 } else {
1040 WARN_ON_ONCE(1);
1041 return -EINVAL;
1042 }
1043
1044 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1045 &tkip_data);
1046
1047 if (tkip_data.have_tkip_keys) {
1048 /* send relevant data according to CMD version */
1049 ret = iwl_mvm_send_cmd_pdu(mvm,
1050 WOWLAN_TKIP_PARAM,
1051 CMD_ASYNC, size,
1052 &tkip_data.tkip);
1053 if (ret)
1054 return ret;
1055 }
1056 }
1057
1058 /* configure rekey data only if offloaded rekey is supported (d3) */
1059 if (mvmvif->rekey_data.valid) {
1060 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1061 struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1062 &kek_kck_cmd;
1063 struct wowlan_key_gtk_type_iter gtk_type_data = {
1064 .kek_kck_cmd = _kek_kck_cmd,
1065 };
1066
1067 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1068 WOWLAN_KEK_KCK_MATERIAL,
1069 IWL_FW_CMD_VER_UNKNOWN);
1070 if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1071 cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1072 return -EINVAL;
1073
1074 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1075 >k_type_data);
1076
1077 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1078 mvmvif->rekey_data.kck_len);
1079 kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1080 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1081 mvmvif->rekey_data.kek_len);
1082 kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1083 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1084 kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1085 kek_kck_cmd.sta_id = cpu_to_le32(mvm_link->ap_sta_id);
1086
1087 if (cmd_ver == 4) {
1088 cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1089 } else {
1090 if (cmd_ver == 3)
1091 cmd_size =
1092 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1093 else
1094 cmd_size =
1095 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1096 /* skip the sta_id at the beginning */
1097 _kek_kck_cmd = (void *)
1098 ((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1099 }
1100
1101 IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1102 mvmvif->rekey_data.akm);
1103
1104 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1105 CMD_ASYNC, cmd_size, _kek_kck_cmd);
1106 if (ret)
1107 return ret;
1108 }
1109
1110 return 0;
1111 }
1112
1113 static int
iwl_mvm_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd_v6 * wowlan_config_cmd_v6,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_vif_link_info * mvm_link,struct ieee80211_sta * ap_sta)1114 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1115 struct cfg80211_wowlan *wowlan,
1116 struct iwl_wowlan_config_cmd_v6 *wowlan_config_cmd_v6,
1117 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1118 struct iwl_mvm_vif_link_info *mvm_link,
1119 struct ieee80211_sta *ap_sta)
1120 {
1121 int ret;
1122 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1123 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1124
1125 mvm->offload_tid = wowlan_config_cmd_v6->offloading_tid;
1126
1127 if (!unified_image) {
1128 ret = iwl_mvm_switch_to_d3(mvm);
1129 if (ret)
1130 return ret;
1131
1132 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1133 if (ret)
1134 return ret;
1135 }
1136
1137 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, mvm_link);
1138 if (ret)
1139 return ret;
1140
1141 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) > 6) {
1142 struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1143 .wakeup_filter = wowlan_config_cmd_v6->wakeup_filter,
1144 .wowlan_ba_teardown_tids =
1145 wowlan_config_cmd_v6->wowlan_ba_teardown_tids,
1146 .is_11n_connection =
1147 wowlan_config_cmd_v6->is_11n_connection,
1148 .offloading_tid = wowlan_config_cmd_v6->offloading_tid,
1149 .flags = wowlan_config_cmd_v6->flags,
1150 .sta_id = wowlan_config_cmd_v6->sta_id,
1151 };
1152
1153 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1154 sizeof(wowlan_config_cmd),
1155 &wowlan_config_cmd);
1156 } else {
1157 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1158 sizeof(*wowlan_config_cmd_v6),
1159 wowlan_config_cmd_v6);
1160 }
1161 if (ret)
1162 return ret;
1163
1164 if (fw_has_api(&mvm->fw->ucode_capa,
1165 IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1166 ret = iwl_mvm_send_patterns(mvm, mvm_link, wowlan);
1167 else
1168 ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1169 if (ret)
1170 return ret;
1171
1172 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0,
1173 mvm_link->ap_sta_id);
1174 }
1175
1176 static int
iwl_mvm_netdetect_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct cfg80211_sched_scan_request * nd_config,struct ieee80211_vif * vif)1177 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1178 struct cfg80211_wowlan *wowlan,
1179 struct cfg80211_sched_scan_request *nd_config,
1180 struct ieee80211_vif *vif)
1181 {
1182 int ret;
1183 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1184 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1185
1186 if (!unified_image) {
1187 ret = iwl_mvm_switch_to_d3(mvm);
1188 if (ret)
1189 return ret;
1190 } else {
1191 /* In theory, we wouldn't have to stop a running sched
1192 * scan in order to start another one (for
1193 * net-detect). But in practice this doesn't seem to
1194 * work properly, so stop any running sched_scan now.
1195 */
1196 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1197 if (ret)
1198 return ret;
1199 }
1200
1201 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1202 IWL_MVM_SCAN_NETDETECT);
1203 if (ret)
1204 return ret;
1205
1206 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1207 return -EBUSY;
1208
1209 /* save the sched scan matchsets... */
1210 if (nd_config->n_match_sets) {
1211 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1212 sizeof(*nd_config->match_sets) *
1213 nd_config->n_match_sets,
1214 GFP_KERNEL);
1215 if (mvm->nd_match_sets)
1216 mvm->n_nd_match_sets = nd_config->n_match_sets;
1217 }
1218
1219 /* ...and the sched scan channels for later reporting */
1220 mvm->nd_channels = kmemdup(nd_config->channels,
1221 sizeof(*nd_config->channels) *
1222 nd_config->n_channels,
1223 GFP_KERNEL);
1224 if (mvm->nd_channels)
1225 mvm->n_nd_channels = nd_config->n_channels;
1226
1227 return 0;
1228 }
1229
iwl_mvm_free_nd(struct iwl_mvm * mvm)1230 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1231 {
1232 kfree(mvm->nd_match_sets);
1233 mvm->nd_match_sets = NULL;
1234 mvm->n_nd_match_sets = 0;
1235 kfree(mvm->nd_channels);
1236 mvm->nd_channels = NULL;
1237 mvm->n_nd_channels = 0;
1238 }
1239
__iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan,bool test)1240 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1241 struct cfg80211_wowlan *wowlan,
1242 bool test)
1243 {
1244 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1245 struct ieee80211_vif *vif = NULL;
1246 struct iwl_mvm_vif *mvmvif = NULL;
1247 struct ieee80211_sta *ap_sta = NULL;
1248 struct iwl_mvm_vif_link_info *mvm_link;
1249 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1250 /*
1251 * Program the minimum sleep time to 10 seconds, as many
1252 * platforms have issues processing a wakeup signal while
1253 * still being in the process of suspending.
1254 */
1255 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1256 };
1257 struct iwl_host_cmd d3_cfg_cmd = {
1258 .id = D3_CONFIG_CMD,
1259 .flags = CMD_WANT_SKB,
1260 .data[0] = &d3_cfg_cmd_data,
1261 .len[0] = sizeof(d3_cfg_cmd_data),
1262 };
1263 int ret;
1264 int len __maybe_unused;
1265 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1266 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1267
1268 if (!wowlan) {
1269 /*
1270 * mac80211 shouldn't get here, but for D3 test
1271 * it doesn't warrant a warning
1272 */
1273 WARN_ON(!test);
1274 return -EINVAL;
1275 }
1276
1277 vif = iwl_mvm_get_bss_vif(mvm);
1278 if (IS_ERR_OR_NULL(vif))
1279 return 1;
1280
1281 ret = iwl_mvm_block_esr_sync(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
1282 if (ret)
1283 return ret;
1284
1285 mutex_lock(&mvm->mutex);
1286
1287 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1288
1289 synchronize_net();
1290
1291 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1292
1293 mvm_link = mvmvif->link[iwl_mvm_get_primary_link(vif)];
1294 if (WARN_ON_ONCE(!mvm_link)) {
1295 ret = -EINVAL;
1296 goto out_noreset;
1297 }
1298
1299 if (mvm_link->ap_sta_id == IWL_INVALID_STA) {
1300 /* if we're not associated, this must be netdetect */
1301 if (!wowlan->nd_config) {
1302 ret = 1;
1303 goto out_noreset;
1304 }
1305
1306 ret = iwl_mvm_netdetect_config(
1307 mvm, wowlan, wowlan->nd_config, vif);
1308 if (ret)
1309 goto out;
1310
1311 mvm->net_detect = true;
1312 } else {
1313 struct iwl_wowlan_config_cmd_v6 wowlan_config_cmd = {
1314 .offloading_tid = 0,
1315 };
1316
1317 wowlan_config_cmd.sta_id = mvm_link->ap_sta_id;
1318
1319 ap_sta = rcu_dereference_protected(
1320 mvm->fw_id_to_mac_id[mvm_link->ap_sta_id],
1321 lockdep_is_held(&mvm->mutex));
1322 if (IS_ERR_OR_NULL(ap_sta)) {
1323 ret = -EINVAL;
1324 goto out_noreset;
1325 }
1326
1327 ret = iwl_mvm_sta_ensure_queue(
1328 mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1329 if (ret)
1330 goto out_noreset;
1331
1332 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1333 vif, mvmvif, ap_sta);
1334 if (ret)
1335 goto out_noreset;
1336 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1337 vif, mvmvif, mvm_link, ap_sta);
1338 if (ret)
1339 goto out;
1340
1341 mvm->net_detect = false;
1342 }
1343
1344 ret = iwl_mvm_power_update_device(mvm);
1345 if (ret)
1346 goto out;
1347
1348 ret = iwl_mvm_power_update_mac(mvm);
1349 if (ret)
1350 goto out;
1351
1352 #ifdef CONFIG_IWLWIFI_DEBUGFS
1353 if (mvm->d3_wake_sysassert)
1354 d3_cfg_cmd_data.wakeup_flags |=
1355 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1356 #endif
1357
1358 /*
1359 * Prior to 9000 device family the driver needs to stop the dbg
1360 * recording before entering D3. In later devices the FW stops the
1361 * recording automatically.
1362 */
1363 if (mvm->trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1364 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1365
1366 /* must be last -- this switches firmware state */
1367 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1368 if (ret)
1369 goto out;
1370 #ifdef CONFIG_IWLWIFI_DEBUGFS
1371 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1372 if (len >= sizeof(u32)) {
1373 mvm->d3_test_pme_ptr =
1374 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1375 }
1376 #endif
1377 iwl_free_resp(&d3_cfg_cmd);
1378
1379 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1380
1381 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1382 out:
1383 if (ret < 0) {
1384 iwl_mvm_free_nd(mvm);
1385
1386 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1387 }
1388 out_noreset:
1389 mutex_unlock(&mvm->mutex);
1390
1391 return ret;
1392 }
1393
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1394 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1395 {
1396 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1397
1398 iwl_mvm_pause_tcm(mvm, true);
1399
1400 mutex_lock(&mvm->mutex);
1401 iwl_fw_runtime_suspend(&mvm->fwrt);
1402 mutex_unlock(&mvm->mutex);
1403
1404 return __iwl_mvm_suspend(hw, wowlan, false);
1405 }
1406
1407 struct iwl_multicast_key_data {
1408 u8 key[WOWLAN_KEY_MAX_SIZE];
1409 u8 len;
1410 u8 flags;
1411 u8 id;
1412 u8 ipn[6];
1413 };
1414
1415 /* converted data from the different status responses */
1416 struct iwl_wowlan_status_data {
1417 u64 replay_ctr;
1418 u32 num_of_gtk_rekeys;
1419 u32 received_beacons;
1420 u32 wakeup_reasons;
1421 u32 wake_packet_length;
1422 u32 wake_packet_bufsize;
1423 u16 pattern_number;
1424 u16 non_qos_seq_ctr;
1425 u16 qos_seq_ctr[8];
1426 u8 tid_tear_down;
1427 u8 tid_offloaded_tx;
1428
1429 struct {
1430 /* including RX MIC key for TKIP */
1431 u8 key[WOWLAN_KEY_MAX_SIZE];
1432 u8 len;
1433 u8 flags;
1434 u8 id;
1435 } gtk[WOWLAN_GTK_KEYS_NUM];
1436
1437 struct {
1438 /*
1439 * We store both the TKIP and AES representations
1440 * coming from the firmware because we decode the
1441 * data from there before we iterate the keys and
1442 * know which one we need.
1443 */
1444 struct {
1445 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1446 } tkip, aes;
1447
1448 /*
1449 * We use -1 for when we have valid data but don't know
1450 * the key ID from firmware, and thus it needs to be
1451 * installed with the last key (depending on rekeying).
1452 */
1453 s8 key_id;
1454 bool valid;
1455 } gtk_seq[2];
1456
1457 struct {
1458 /* Same as above */
1459 struct {
1460 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1461 u64 tx_pn;
1462 } tkip, aes;
1463 } ptk;
1464
1465 struct iwl_multicast_key_data igtk;
1466 struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
1467
1468 u8 *wake_packet;
1469 };
1470
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1471 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1472 struct ieee80211_vif *vif,
1473 struct iwl_wowlan_status_data *status)
1474 {
1475 struct sk_buff *pkt = NULL;
1476 struct cfg80211_wowlan_wakeup wakeup = {
1477 .pattern_idx = -1,
1478 };
1479 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1480 u32 reasons = status->wakeup_reasons;
1481
1482 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1483 wakeup_report = NULL;
1484 goto report;
1485 }
1486
1487 pm_wakeup_event(mvm->dev, 0);
1488
1489 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1490 wakeup.magic_pkt = true;
1491
1492 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1493 wakeup.pattern_idx =
1494 status->pattern_number;
1495
1496 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1497 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1498 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1499 wakeup.disconnect = true;
1500
1501 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1502 wakeup.gtk_rekey_failure = true;
1503
1504 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1505 wakeup.rfkill_release = true;
1506
1507 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1508 wakeup.eap_identity_req = true;
1509
1510 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1511 wakeup.four_way_handshake = true;
1512
1513 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1514 wakeup.tcp_connlost = true;
1515
1516 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1517 wakeup.tcp_nomoretokens = true;
1518
1519 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1520 wakeup.tcp_match = true;
1521
1522 if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
1523 wakeup.unprot_deauth_disassoc = true;
1524
1525 if (status->wake_packet) {
1526 int pktsize = status->wake_packet_bufsize;
1527 int pktlen = status->wake_packet_length;
1528 const u8 *pktdata = status->wake_packet;
1529 const struct ieee80211_hdr *hdr = (const void *)pktdata;
1530 int truncated = pktlen - pktsize;
1531
1532 /* this would be a firmware bug */
1533 if (WARN_ON_ONCE(truncated < 0))
1534 truncated = 0;
1535
1536 if (ieee80211_is_data(hdr->frame_control)) {
1537 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1538 int ivlen = 0, icvlen = 4; /* also FCS */
1539
1540 pkt = alloc_skb(pktsize, GFP_KERNEL);
1541 if (!pkt)
1542 goto report;
1543
1544 skb_put_data(pkt, pktdata, hdrlen);
1545 pktdata += hdrlen;
1546 pktsize -= hdrlen;
1547
1548 if (ieee80211_has_protected(hdr->frame_control)) {
1549 /*
1550 * This is unlocked and using gtk_i(c)vlen,
1551 * but since everything is under RTNL still
1552 * that's not really a problem - changing
1553 * it would be difficult.
1554 */
1555 if (is_multicast_ether_addr(hdr->addr1)) {
1556 ivlen = mvm->gtk_ivlen;
1557 icvlen += mvm->gtk_icvlen;
1558 } else {
1559 ivlen = mvm->ptk_ivlen;
1560 icvlen += mvm->ptk_icvlen;
1561 }
1562 }
1563
1564 /* if truncated, FCS/ICV is (partially) gone */
1565 if (truncated >= icvlen) {
1566 icvlen = 0;
1567 truncated -= icvlen;
1568 } else {
1569 icvlen -= truncated;
1570 truncated = 0;
1571 }
1572
1573 pktsize -= ivlen + icvlen;
1574 pktdata += ivlen;
1575
1576 skb_put_data(pkt, pktdata, pktsize);
1577
1578 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1579 goto report;
1580 wakeup.packet = pkt->data;
1581 wakeup.packet_present_len = pkt->len;
1582 wakeup.packet_len = pkt->len - truncated;
1583 wakeup.packet_80211 = false;
1584 } else {
1585 int fcslen = 4;
1586
1587 if (truncated >= 4) {
1588 truncated -= 4;
1589 fcslen = 0;
1590 } else {
1591 fcslen -= truncated;
1592 truncated = 0;
1593 }
1594 pktsize -= fcslen;
1595 wakeup.packet = status->wake_packet;
1596 wakeup.packet_present_len = pktsize;
1597 wakeup.packet_len = pktlen - truncated;
1598 wakeup.packet_80211 = true;
1599 }
1600 }
1601
1602 report:
1603 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1604 kfree_skb(pkt);
1605 }
1606
iwl_mvm_le64_to_aes_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1607 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1608 {
1609 u64 pn = le64_to_cpu(le_pn);
1610
1611 seq->ccmp.pn[0] = pn >> 40;
1612 seq->ccmp.pn[1] = pn >> 32;
1613 seq->ccmp.pn[2] = pn >> 24;
1614 seq->ccmp.pn[3] = pn >> 16;
1615 seq->ccmp.pn[4] = pn >> 8;
1616 seq->ccmp.pn[5] = pn;
1617 }
1618
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1619 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1620 struct ieee80211_key_seq *seq)
1621 {
1622 iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1623 }
1624
iwl_mvm_le64_to_tkip_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1625 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1626 {
1627 u64 pn = le64_to_cpu(le_pn);
1628
1629 seq->tkip.iv16 = (u16)pn;
1630 seq->tkip.iv32 = (u32)(pn >> 16);
1631 }
1632
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1633 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1634 struct ieee80211_key_seq *seq)
1635 {
1636 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1637 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1638 }
1639
iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf * key,struct ieee80211_key_seq * seq)1640 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1641 struct ieee80211_key_seq *seq)
1642 {
1643 int tid;
1644
1645 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1646 ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1647 }
1648
iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm * mvm,struct iwl_wowlan_status_data * status,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1649 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1650 struct iwl_wowlan_status_data *status,
1651 struct ieee80211_sta *sta,
1652 struct ieee80211_key_conf *key)
1653 {
1654 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1655 struct iwl_mvm_key_pn *ptk_pn;
1656 int tid;
1657
1658 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1659
1660 if (!iwl_mvm_has_new_rx_api(mvm))
1661 return;
1662
1663
1664 rcu_read_lock();
1665 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1666 if (WARN_ON(!ptk_pn)) {
1667 rcu_read_unlock();
1668 return;
1669 }
1670
1671 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1672 int i;
1673
1674 for (i = 1; i < mvm->trans->info.num_rxqs; i++)
1675 memcpy(ptk_pn->q[i].pn[tid],
1676 status->ptk.aes.seq[tid].ccmp.pn,
1677 IEEE80211_CCMP_PN_LEN);
1678 }
1679 rcu_read_unlock();
1680 }
1681
iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data * status,union iwl_all_tsc_rsc * sc,u8 key_idx)1682 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1683 union iwl_all_tsc_rsc *sc, u8 key_idx)
1684 {
1685 int i;
1686
1687 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1688 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1689
1690 /* GTK RX counters */
1691 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1692 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1693 &status->gtk_seq[0].tkip.seq[i]);
1694 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1695 &status->gtk_seq[0].aes.seq[i]);
1696 }
1697 status->gtk_seq[0].valid = true;
1698 status->gtk_seq[0].key_id = key_idx;
1699
1700 /* PTK TX counter */
1701 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1702 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1703 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1704
1705 /* PTK RX counters */
1706 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1707 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1708 &status->ptk.tkip.seq[i]);
1709 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1710 &status->ptk.aes.seq[i]);
1711 }
1712 }
1713
1714 static void
iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc,unsigned int idx,unsigned int key_id)1715 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1716 struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1717 unsigned int idx, unsigned int key_id)
1718 {
1719 int tid;
1720
1721 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1722 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1723 &status->gtk_seq[idx].tkip.seq[tid]);
1724 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1725 &status->gtk_seq[idx].aes.seq[tid]);
1726 }
1727
1728 status->gtk_seq[idx].valid = true;
1729 status->gtk_seq[idx].key_id = key_id;
1730 }
1731
1732 static void
iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc)1733 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1734 struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1735 {
1736 int i, tid;
1737
1738 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1739 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1740 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1741
1742 /* GTK RX counters */
1743 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1744 u8 entry = sc->mcast_key_id_map[i];
1745
1746 if (entry < ARRAY_SIZE(sc->mcast_rsc))
1747 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1748 entry, i);
1749 }
1750
1751 /* PTK TX counters not needed, assigned in device */
1752
1753 /* PTK RX counters */
1754 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1755 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1756 &status->ptk.tkip.seq[tid]);
1757 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1758 &status->ptk.aes.seq[tid]);
1759 }
1760 }
1761
iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,int idx)1762 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1763 struct iwl_wowlan_status_data *status,
1764 int idx)
1765 {
1766 switch (key->cipher) {
1767 case WLAN_CIPHER_SUITE_CCMP:
1768 case WLAN_CIPHER_SUITE_GCMP:
1769 case WLAN_CIPHER_SUITE_GCMP_256:
1770 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1771 break;
1772 case WLAN_CIPHER_SUITE_TKIP:
1773 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1774 break;
1775 default:
1776 WARN_ON(1);
1777 }
1778 }
1779
iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status)1780 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1781 struct iwl_wowlan_status_data *status)
1782 {
1783 int i;
1784
1785 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1786 if (!status->gtk_seq[i].valid)
1787 continue;
1788
1789 if (status->gtk_seq[i].key_id == key->keyidx)
1790 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1791 }
1792 }
1793
1794 struct iwl_mvm_d3_gtk_iter_data {
1795 struct iwl_mvm *mvm;
1796 struct iwl_wowlan_status_data *status;
1797 u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1798 bool unhandled_cipher, igtk_support, bigtk_support;
1799 int num_keys;
1800 };
1801
iwl_mvm_d3_find_last_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1802 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1803 struct ieee80211_vif *vif,
1804 struct ieee80211_sta *sta,
1805 struct ieee80211_key_conf *key,
1806 void *_data)
1807 {
1808 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1809 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1810
1811 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1812 return;
1813
1814 if (data->unhandled_cipher)
1815 return;
1816
1817 switch (key->cipher) {
1818 case WLAN_CIPHER_SUITE_WEP40:
1819 case WLAN_CIPHER_SUITE_WEP104:
1820 /* ignore WEP completely, nothing to do */
1821 return;
1822 case WLAN_CIPHER_SUITE_CCMP:
1823 case WLAN_CIPHER_SUITE_GCMP:
1824 case WLAN_CIPHER_SUITE_GCMP_256:
1825 case WLAN_CIPHER_SUITE_TKIP:
1826 /* we support these */
1827 data->gtk_cipher = key->cipher;
1828 break;
1829 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1830 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1831 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1832 case WLAN_CIPHER_SUITE_AES_CMAC:
1833 /* we support these */
1834 if (data->igtk_support &&
1835 (key->keyidx == 4 || key->keyidx == 5)) {
1836 data->igtk_cipher = key->cipher;
1837 } else if (data->bigtk_support &&
1838 (key->keyidx == 6 || key->keyidx == 7)) {
1839 data->bigtk_cipher = key->cipher;
1840 } else {
1841 data->unhandled_cipher = true;
1842 return;
1843 }
1844 break;
1845 default:
1846 /* everything else - disconnect from AP */
1847 data->unhandled_cipher = true;
1848 return;
1849 }
1850
1851 data->num_keys++;
1852 }
1853
1854 static void
iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data * key,struct ieee80211_key_seq * seq,u32 cipher)1855 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1856 struct ieee80211_key_seq *seq, u32 cipher)
1857 {
1858 switch (cipher) {
1859 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1860 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1861 BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1862 memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1863 break;
1864 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1865 case WLAN_CIPHER_SUITE_AES_CMAC:
1866 BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1867 memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1868 break;
1869 default:
1870 WARN_ON(1);
1871 }
1872 }
1873
1874 static void
iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data * status,struct ieee80211_key_conf * key,struct iwl_multicast_key_data * key_data)1875 iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1876 struct ieee80211_key_conf *key,
1877 struct iwl_multicast_key_data *key_data)
1878 {
1879 struct ieee80211_key_seq seq;
1880
1881 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, key->cipher);
1882 ieee80211_set_key_rx_seq(key, 0, &seq);
1883 }
1884
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1885 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1886 struct ieee80211_vif *vif,
1887 struct ieee80211_sta *sta,
1888 struct ieee80211_key_conf *key,
1889 void *_data)
1890 {
1891 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1892 struct iwl_wowlan_status_data *status = data->status;
1893 s8 keyidx;
1894 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1895
1896 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1897 return;
1898
1899 if (data->unhandled_cipher)
1900 return;
1901
1902 switch (key->cipher) {
1903 case WLAN_CIPHER_SUITE_WEP40:
1904 case WLAN_CIPHER_SUITE_WEP104:
1905 /* ignore WEP completely, nothing to do */
1906 return;
1907 case WLAN_CIPHER_SUITE_CCMP:
1908 case WLAN_CIPHER_SUITE_GCMP:
1909 case WLAN_CIPHER_SUITE_GCMP_256:
1910 if (sta) {
1911 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1912 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1913 return;
1914 }
1915 fallthrough;
1916 case WLAN_CIPHER_SUITE_TKIP:
1917 if (sta) {
1918 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1919 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1920 return;
1921 }
1922 keyidx = key->keyidx;
1923 /*
1924 * Update the seq even if there was a rekey. If there was a
1925 * rekey, we will update again after replacing the key
1926 */
1927 if ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1928 (status->gtk[1].len && keyidx == status->gtk[1].id))
1929 iwl_mvm_set_key_rx_seq(key, status);
1930 break;
1931 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1932 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1933 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1934 case WLAN_CIPHER_SUITE_AES_CMAC:
1935 if (key->keyidx == 4 || key->keyidx == 5) {
1936 iwl_mvm_d3_update_igtk_bigtk(status, key,
1937 &status->igtk);
1938 }
1939 if (key->keyidx == 6 || key->keyidx == 7) {
1940 u8 idx = key->keyidx == status->bigtk[1].id;
1941
1942 iwl_mvm_d3_update_igtk_bigtk(status, key,
1943 &status->bigtk[idx]);
1944 }
1945 }
1946 }
1947
iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm,u32 gtk_cipher)1948 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
1949 struct ieee80211_vif *vif,
1950 struct iwl_mvm *mvm, u32 gtk_cipher)
1951 {
1952 int i, j;
1953 struct ieee80211_key_conf *key;
1954 DEFINE_RAW_FLEX(struct ieee80211_key_conf, conf, key,
1955 WOWLAN_KEY_MAX_SIZE);
1956 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1957 u8 key_data[WOWLAN_KEY_MAX_SIZE];
1958
1959 conf->cipher = gtk_cipher;
1960
1961 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1962 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_CCMP);
1963 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_GCMP_256);
1964 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_TKIP);
1965 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < sizeof(status->gtk[0].key));
1966
1967 switch (gtk_cipher) {
1968 case WLAN_CIPHER_SUITE_CCMP:
1969 case WLAN_CIPHER_SUITE_GCMP:
1970 conf->keylen = WLAN_KEY_LEN_CCMP;
1971 break;
1972 case WLAN_CIPHER_SUITE_GCMP_256:
1973 conf->keylen = WLAN_KEY_LEN_GCMP_256;
1974 break;
1975 case WLAN_CIPHER_SUITE_TKIP:
1976 conf->keylen = WLAN_KEY_LEN_TKIP;
1977 break;
1978 default:
1979 WARN_ON(1);
1980 }
1981
1982 for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
1983 if (!status->gtk[i].len)
1984 continue;
1985
1986 conf->keyidx = status->gtk[i].id;
1987 IWL_DEBUG_WOWLAN(mvm,
1988 "Received from FW GTK cipher %d, key index %d\n",
1989 conf->cipher, conf->keyidx);
1990 memcpy(conf->key, status->gtk[i].key,
1991 sizeof(status->gtk[i].key));
1992 memcpy(key_data, status->gtk[i].key, sizeof(status->gtk[i].key));
1993
1994 key = ieee80211_gtk_rekey_add(vif, status->gtk[i].id, key_data,
1995 sizeof(key_data), link_id);
1996 if (IS_ERR(key)) {
1997 /* FW may send also the old keys */
1998 if (PTR_ERR(key) == -EALREADY)
1999 continue;
2000 return false;
2001 }
2002
2003 for (j = 0; j < ARRAY_SIZE(status->gtk_seq); j++) {
2004 if (!status->gtk_seq[j].valid ||
2005 status->gtk_seq[j].key_id != key->keyidx)
2006 continue;
2007 iwl_mvm_set_key_rx_seq_idx(key, status, j);
2008 break;
2009 }
2010 WARN_ON(j == ARRAY_SIZE(status->gtk_seq));
2011 }
2012
2013 return true;
2014 }
2015
2016 static bool
iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,u32 cipher,struct iwl_multicast_key_data * key_data)2017 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
2018 struct ieee80211_vif *vif, u32 cipher,
2019 struct iwl_multicast_key_data *key_data)
2020 {
2021 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2022 DEFINE_RAW_FLEX(struct ieee80211_key_conf, conf, key,
2023 WOWLAN_KEY_MAX_SIZE);
2024 struct ieee80211_key_conf *key_config;
2025 struct ieee80211_key_seq seq;
2026 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2027 u8 key[WOWLAN_KEY_MAX_SIZE];
2028 s8 keyidx = key_data->id;
2029
2030 conf->cipher = cipher;
2031 conf->keyidx = keyidx;
2032
2033 if (!key_data->len)
2034 return true;
2035
2036 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf->cipher);
2037
2038 switch (cipher) {
2039 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2040 conf->keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2041 break;
2042 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2043 conf->keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2044 break;
2045 case WLAN_CIPHER_SUITE_AES_CMAC:
2046 conf->keylen = WLAN_KEY_LEN_AES_CMAC;
2047 break;
2048 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2049 conf->keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2050 break;
2051 default:
2052 WARN_ON(1);
2053 }
2054 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < sizeof(key_data->key));
2055 memcpy(conf->key, key_data->key, conf->keylen);
2056
2057 memcpy(key, key_data->key, sizeof(key_data->key));
2058
2059 key_config = ieee80211_gtk_rekey_add(vif, keyidx, key, sizeof(key),
2060 link_id);
2061 if (IS_ERR(key_config)) {
2062 /* FW may send also the old keys */
2063 return PTR_ERR(key_config) == -EALREADY;
2064 }
2065 ieee80211_set_key_rx_seq(key_config, 0, &seq);
2066
2067 if (keyidx == 4 || keyidx == 5) {
2068 struct iwl_mvm_vif_link_info *mvm_link;
2069
2070 link_id = link_id < 0 ? 0 : link_id;
2071 mvm_link = mvmvif->link[link_id];
2072 if (mvm_link->igtk)
2073 mvm_link->igtk->hw_key_idx = STA_KEY_IDX_INVALID;
2074 mvm_link->igtk = key_config;
2075 }
2076
2077 if (vif->type == NL80211_IFTYPE_STATION && (keyidx == 6 || keyidx == 7))
2078 rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
2079 key_config);
2080
2081 return true;
2082 }
2083
iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm * mvm)2084 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2085 {
2086 u8 notif_ver;
2087
2088 if (!fw_has_api(&mvm->fw->ucode_capa,
2089 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2090 return 6;
2091
2092 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2093 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2094 WOWLAN_GET_STATUSES, 0);
2095 if (!notif_ver)
2096 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2097 WOWLAN_GET_STATUSES, 7);
2098
2099 return notif_ver;
2100 }
2101
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2102 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2103 struct ieee80211_vif *vif,
2104 struct iwl_wowlan_status_data *status)
2105 {
2106 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2107 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2108 .mvm = mvm,
2109 .status = status,
2110 };
2111 int i;
2112 u32 disconnection_reasons =
2113 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2114 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2115
2116 if (!status || !vif->bss_conf.bssid)
2117 return false;
2118
2119 if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2120 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2121 WOWLAN_INFO_NOTIFICATION,
2122 0))
2123 gtkdata.igtk_support = true;
2124
2125 if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2126 WOWLAN_INFO_NOTIFICATION,
2127 0) >= 3)
2128 gtkdata.bigtk_support = true;
2129
2130 /* find last GTK that we used initially, if any */
2131 ieee80211_iter_keys(mvm->hw, vif,
2132 iwl_mvm_d3_find_last_keys, >kdata);
2133 /* not trying to keep connections with MFP/unhandled ciphers */
2134 if (gtkdata.unhandled_cipher)
2135 return false;
2136 if (!gtkdata.num_keys)
2137 goto out;
2138
2139 /*
2140 * invalidate all other GTKs that might still exist and update
2141 * the one that we used
2142 */
2143 ieee80211_iter_keys(mvm->hw, vif,
2144 iwl_mvm_d3_update_keys, >kdata);
2145
2146 if (status->num_of_gtk_rekeys) {
2147 __be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2148
2149 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2150 status->num_of_gtk_rekeys);
2151
2152 if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2153 return false;
2154
2155 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2156 gtkdata.igtk_cipher,
2157 &status->igtk))
2158 return false;
2159
2160 for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2161 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2162 gtkdata.bigtk_cipher,
2163 &status->bigtk[i]))
2164 return false;
2165 }
2166
2167 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2168 (void *)&replay_ctr, GFP_KERNEL);
2169 }
2170
2171 out:
2172 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2173 WOWLAN_GET_STATUSES,
2174 IWL_FW_CMD_VER_UNKNOWN) < 10) {
2175 mvmvif->seqno_valid = true;
2176 /* +0x10 because the set API expects next-to-use, not last-used */
2177 mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2178 }
2179
2180 if (status->wakeup_reasons & disconnection_reasons)
2181 return false;
2182
2183 return true;
2184 }
2185
iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v2 * data)2186 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2187 struct iwl_wowlan_gtk_status_v2 *data)
2188 {
2189 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2190 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2191 sizeof(data->tkip_mic_key) >
2192 sizeof(status->gtk[0].key));
2193
2194 status->gtk[0].len = data->key_len;
2195 status->gtk[0].flags = data->key_flags;
2196 status->gtk[0].id = status->gtk[0].flags & IWL_WOWLAN_GTK_IDX_MASK;
2197
2198 memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2199
2200 /* if it's as long as the TKIP encryption key, copy MIC key */
2201 if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2202 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2203 data->tkip_mic_key, sizeof(data->tkip_mic_key));
2204 }
2205
iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v3 * data)2206 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2207 struct iwl_wowlan_gtk_status_v3 *data)
2208 {
2209 int data_idx, status_idx = 0;
2210
2211 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2212 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2213 sizeof(data[0].tkip_mic_key) >
2214 sizeof(status->gtk[0].key));
2215 BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2216 for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2217 if (!(data[data_idx].key_len))
2218 continue;
2219 status->gtk[status_idx].len = data[data_idx].key_len;
2220 status->gtk[status_idx].flags = data[data_idx].key_flags;
2221 status->gtk[status_idx].id = status->gtk[status_idx].flags &
2222 IWL_WOWLAN_GTK_IDX_MASK;
2223
2224 memcpy(status->gtk[status_idx].key, data[data_idx].key,
2225 sizeof(data[data_idx].key));
2226
2227 /* if it's as long as the TKIP encryption key, copy MIC key */
2228 if (status->gtk[status_idx].len ==
2229 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2230 memcpy(status->gtk[status_idx].key +
2231 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2232 data[data_idx].tkip_mic_key,
2233 sizeof(data[data_idx].tkip_mic_key));
2234 status_idx++;
2235 }
2236 }
2237
iwl_mvm_convert_igtk(struct iwl_wowlan_status_data * status,struct iwl_wowlan_igtk_status * data)2238 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2239 struct iwl_wowlan_igtk_status *data)
2240 {
2241 int i;
2242
2243 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2244 BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2245
2246 if (!data->key_len)
2247 return;
2248
2249 status->igtk.len = data->key_len;
2250 status->igtk.flags = data->key_flags;
2251 status->igtk.id = u32_get_bits(data->key_flags,
2252 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2253 + WOWLAN_IGTK_MIN_INDEX;
2254
2255 memcpy(status->igtk.key, data->key, sizeof(data->key));
2256
2257 /* mac80211 expects big endian for memcmp() to work, convert */
2258 for (i = 0; i < sizeof(data->ipn); i++)
2259 status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2260 }
2261
iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data * status,const struct iwl_wowlan_igtk_status * data)2262 static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2263 const struct iwl_wowlan_igtk_status *data)
2264 {
2265 int data_idx, status_idx = 0;
2266
2267 BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2268
2269 for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2270 if (!data[data_idx].key_len)
2271 continue;
2272
2273 status->bigtk[status_idx].len = data[data_idx].key_len;
2274 status->bigtk[status_idx].flags = data[data_idx].key_flags;
2275 status->bigtk[status_idx].id =
2276 u32_get_bits(data[data_idx].key_flags,
2277 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2278 + WOWLAN_BIGTK_MIN_INDEX;
2279
2280 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2281 sizeof(data[data_idx].key));
2282 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2283 sizeof(data[data_idx].ipn));
2284
2285 memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2286 sizeof(data[data_idx].key));
2287 memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2288 sizeof(data[data_idx].ipn));
2289 status_idx++;
2290 }
2291 }
2292
iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif * data,struct iwl_wowlan_status_data * status,u32 len)2293 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2294 struct iwl_wowlan_info_notif *data,
2295 struct iwl_wowlan_status_data *status,
2296 u32 len)
2297 {
2298 if (IWL_FW_CHECK(mvm, data->num_mlo_link_keys,
2299 "MLO is not supported, shouldn't receive MLO keys\n"))
2300 return;
2301
2302 if (len < sizeof(*data)) {
2303 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2304 status = NULL;
2305 return;
2306 }
2307
2308 if (mvm->fast_resume)
2309 return;
2310
2311 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2312 iwl_mvm_convert_gtk_v3(status, data->gtk);
2313 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2314 iwl_mvm_convert_bigtk(status, data->bigtk);
2315 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2316 status->pattern_number = le16_to_cpu(data->pattern_number);
2317 status->tid_offloaded_tx = data->tid_offloaded_tx;
2318 if (IWL_FW_CHECK(mvm,
2319 data->tid_offloaded_tx >=
2320 ARRAY_SIZE(status->qos_seq_ctr),
2321 "tid_offloaded_tx is out of bound %d\n",
2322 data->tid_offloaded_tx))
2323 data->tid_offloaded_tx = 0;
2324 status->qos_seq_ctr[data->tid_offloaded_tx] =
2325 le16_to_cpu(data->qos_seq_ctr);
2326 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2327 status->num_of_gtk_rekeys =
2328 le32_to_cpu(data->num_of_gtk_rekeys);
2329 status->received_beacons = le32_to_cpu(data->received_beacons);
2330 status->tid_tear_down = data->tid_tear_down;
2331 }
2332
2333 static void
iwl_mvm_parse_wowlan_info_notif_v3(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif_v3 * data,struct iwl_wowlan_status_data * status,u32 len)2334 iwl_mvm_parse_wowlan_info_notif_v3(struct iwl_mvm *mvm,
2335 struct iwl_wowlan_info_notif_v3 *data,
2336 struct iwl_wowlan_status_data *status,
2337 u32 len)
2338 {
2339 u32 i;
2340
2341 if (len < sizeof(*data)) {
2342 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2343 status = NULL;
2344 return;
2345 }
2346
2347 if (mvm->fast_resume)
2348 return;
2349
2350 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2351 iwl_mvm_convert_gtk_v3(status, data->gtk);
2352 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2353 iwl_mvm_convert_bigtk(status, data->bigtk);
2354 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2355 status->pattern_number = le16_to_cpu(data->pattern_number);
2356 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2357 status->qos_seq_ctr[i] =
2358 le16_to_cpu(data->qos_seq_ctr[i]);
2359 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2360 status->num_of_gtk_rekeys =
2361 le32_to_cpu(data->num_of_gtk_rekeys);
2362 status->received_beacons = le32_to_cpu(data->received_beacons);
2363 status->tid_tear_down = data->tid_tear_down;
2364 }
2365
2366 static void
iwl_mvm_parse_wowlan_info_notif_v1(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif_v1 * data,struct iwl_wowlan_status_data * status,u32 len)2367 iwl_mvm_parse_wowlan_info_notif_v1(struct iwl_mvm *mvm,
2368 struct iwl_wowlan_info_notif_v1 *data,
2369 struct iwl_wowlan_status_data *status,
2370 u32 len)
2371 {
2372 u32 i;
2373
2374 if (len < sizeof(*data)) {
2375 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2376 status = NULL;
2377 return;
2378 }
2379
2380 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2381 iwl_mvm_convert_gtk_v3(status, data->gtk);
2382 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2383 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2384 status->pattern_number = le16_to_cpu(data->pattern_number);
2385 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2386 status->qos_seq_ctr[i] =
2387 le16_to_cpu(data->qos_seq_ctr[i]);
2388 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2389 status->num_of_gtk_rekeys =
2390 le32_to_cpu(data->num_of_gtk_rekeys);
2391 status->received_beacons = le32_to_cpu(data->received_beacons);
2392 status->tid_tear_down = data->tid_tear_down;
2393 }
2394
2395 /* Occasionally, templates would be nice. This is one of those times ... */
2396 #define iwl_mvm_parse_wowlan_status_common(_ver) \
2397 static struct iwl_wowlan_status_data * \
2398 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \
2399 struct iwl_wowlan_status_ ##_ver *data,\
2400 int len) \
2401 { \
2402 struct iwl_wowlan_status_data *status; \
2403 int data_size, i; \
2404 \
2405 if (len < sizeof(*data)) { \
2406 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2407 return NULL; \
2408 } \
2409 \
2410 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \
2411 if (len != sizeof(*data) + data_size) { \
2412 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2413 return NULL; \
2414 } \
2415 \
2416 status = kzalloc(sizeof(*status), GFP_KERNEL); \
2417 if (!status) \
2418 return NULL; \
2419 \
2420 /* copy all the common fields */ \
2421 status->replay_ctr = le64_to_cpu(data->replay_ctr); \
2422 status->pattern_number = le16_to_cpu(data->pattern_number); \
2423 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \
2424 for (i = 0; i < 8; i++) \
2425 status->qos_seq_ctr[i] = \
2426 le16_to_cpu(data->qos_seq_ctr[i]); \
2427 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \
2428 status->num_of_gtk_rekeys = \
2429 le32_to_cpu(data->num_of_gtk_rekeys); \
2430 status->received_beacons = le32_to_cpu(data->received_beacons); \
2431 status->wake_packet_length = \
2432 le32_to_cpu(data->wake_packet_length); \
2433 status->wake_packet_bufsize = \
2434 le32_to_cpu(data->wake_packet_bufsize); \
2435 if (status->wake_packet_bufsize) { \
2436 status->wake_packet = \
2437 kmemdup(data->wake_packet, \
2438 status->wake_packet_bufsize, \
2439 GFP_KERNEL); \
2440 if (!status->wake_packet) { \
2441 kfree(status); \
2442 return NULL; \
2443 } \
2444 } else { \
2445 status->wake_packet = NULL; \
2446 } \
2447 \
2448 return status; \
2449 }
2450
2451 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)2452 iwl_mvm_parse_wowlan_status_common(v7)
2453
2454 static struct iwl_wowlan_status_data *
2455 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2456 {
2457 struct iwl_wowlan_status_data *status;
2458 struct iwl_wowlan_get_status_cmd get_status_cmd = {
2459 .sta_id = cpu_to_le32(sta_id),
2460 };
2461 struct iwl_host_cmd cmd = {
2462 .id = WOWLAN_GET_STATUSES,
2463 .flags = CMD_WANT_SKB,
2464 .data = { &get_status_cmd, },
2465 .len = { sizeof(get_status_cmd), },
2466 };
2467 int ret, len;
2468 u8 notif_ver;
2469 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2470 IWL_FW_CMD_VER_UNKNOWN);
2471
2472 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2473 cmd.len[0] = 0;
2474
2475 lockdep_assert_held(&mvm->mutex);
2476
2477 ret = iwl_mvm_send_cmd(mvm, &cmd);
2478 if (ret) {
2479 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2480 return ERR_PTR(ret);
2481 }
2482
2483 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2484
2485 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2486 notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2487
2488 if (notif_ver < 7) {
2489 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2490
2491 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2492 if (!status)
2493 goto out_free_resp;
2494
2495 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2496 sizeof(status->gtk[0].key));
2497 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2498 sizeof(v6->gtk.tkip_mic_key) >
2499 sizeof(status->gtk[0].key));
2500
2501 /* copy GTK info to the right place */
2502 memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2503 sizeof(v6->gtk.decrypt_key));
2504 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2505 v6->gtk.tkip_mic_key,
2506 sizeof(v6->gtk.tkip_mic_key));
2507
2508 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc,
2509 v6->gtk.key_index);
2510
2511 /* hardcode the key length to 16 since v6 only supports 16 */
2512 status->gtk[0].len = 16;
2513
2514 /*
2515 * The key index only uses 2 bits (values 0 to 3) and
2516 * we always set bit 7 which means this is the
2517 * currently used key.
2518 */
2519 status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2520 status->gtk[0].id = v6->gtk.key_index;
2521 } else if (notif_ver == 7) {
2522 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2523
2524 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2525 if (!status)
2526 goto out_free_resp;
2527
2528 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc,
2529 v7->gtk[0].key_flags & IWL_WOWLAN_GTK_IDX_MASK);
2530 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2531 iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2532 } else {
2533 IWL_ERR(mvm,
2534 "Firmware advertises unknown WoWLAN status response %d!\n",
2535 notif_ver);
2536 status = NULL;
2537 }
2538
2539 out_free_resp:
2540 iwl_free_resp(&cmd);
2541 return status;
2542 }
2543
2544 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2545 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2546 struct ieee80211_vif *vif,
2547 struct iwl_wowlan_status_data *status)
2548 {
2549 int i;
2550 bool keep = false;
2551 struct iwl_mvm_sta *mvm_ap_sta;
2552 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2553 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2554 struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
2555 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2556 PROT_OFFLOAD_GROUP,
2557 WOWLAN_INFO_NOTIFICATION,
2558 IWL_FW_CMD_VER_UNKNOWN);
2559
2560 if (WARN_ON(!mvm_link))
2561 goto out_unlock;
2562
2563 if (!status)
2564 goto out_unlock;
2565
2566 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2567 status->wakeup_reasons);
2568
2569 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, mvm_link->ap_sta_id);
2570 if (!mvm_ap_sta)
2571 goto out_unlock;
2572
2573 /* firmware stores last-used value, we store next value */
2574 if (wowlan_info_ver >= 5) {
2575 mvm_ap_sta->tid_data[status->tid_offloaded_tx].seq_number =
2576 status->qos_seq_ctr[status->tid_offloaded_tx] + 0x10;
2577 } else {
2578 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2579 mvm_ap_sta->tid_data[i].seq_number =
2580 status->qos_seq_ctr[i] + 0x10;
2581 }
2582
2583 if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2584 i = mvm->offload_tid;
2585 iwl_trans_set_q_ptrs(mvm->trans,
2586 mvm_ap_sta->tid_data[i].txq_id,
2587 mvm_ap_sta->tid_data[i].seq_number >> 4);
2588 }
2589
2590 iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2591
2592 keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2593 out_unlock:
2594 mutex_unlock(&mvm->mutex);
2595 return keep;
2596 }
2597
2598 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2599 IWL_SCAN_MAX_PROFILES)
2600
2601 struct iwl_mvm_nd_results {
2602 u32 matched_profiles;
2603 u8 matches[ND_QUERY_BUF_LEN];
2604 };
2605
2606 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results)2607 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2608 struct iwl_mvm_nd_results *results)
2609 {
2610 struct iwl_scan_offload_match_info *query;
2611 struct iwl_host_cmd cmd = {
2612 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2613 .flags = CMD_WANT_SKB,
2614 };
2615 int ret, len;
2616 size_t query_len, matches_len;
2617 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2618
2619 ret = iwl_mvm_send_cmd(mvm, &cmd);
2620 if (ret) {
2621 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2622 return ret;
2623 }
2624
2625 if (fw_has_api(&mvm->fw->ucode_capa,
2626 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2627 query_len = sizeof(struct iwl_scan_offload_match_info);
2628 matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2629 max_profiles;
2630 } else {
2631 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2632 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2633 max_profiles;
2634 }
2635
2636 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2637 if (len < query_len) {
2638 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2639 ret = -EIO;
2640 goto out_free_resp;
2641 }
2642
2643 query = (void *)cmd.resp_pkt->data;
2644
2645 results->matched_profiles = le32_to_cpu(query->matched_profiles);
2646 memcpy(results->matches, query->matches, matches_len);
2647
2648 #ifdef CONFIG_IWLWIFI_DEBUGFS
2649 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2650 #endif
2651
2652 out_free_resp:
2653 iwl_free_resp(&cmd);
2654 return ret;
2655 }
2656
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,int idx)2657 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2658 struct iwl_mvm_nd_results *results,
2659 int idx)
2660 {
2661 int n_chans = 0, i;
2662
2663 if (fw_has_api(&mvm->fw->ucode_capa,
2664 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2665 struct iwl_scan_offload_profile_match *matches =
2666 (void *)results->matches;
2667
2668 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2669 n_chans += hweight8(matches[idx].matching_channels[i]);
2670 } else {
2671 struct iwl_scan_offload_profile_match_v1 *matches =
2672 (void *)results->matches;
2673
2674 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2675 n_chans += hweight8(matches[idx].matching_channels[i]);
2676 }
2677
2678 return n_chans;
2679 }
2680
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,struct cfg80211_wowlan_nd_match * match,int idx)2681 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2682 struct iwl_mvm_nd_results *results,
2683 struct cfg80211_wowlan_nd_match *match,
2684 int idx)
2685 {
2686 int i;
2687 int n_channels = 0;
2688
2689 if (fw_has_api(&mvm->fw->ucode_capa,
2690 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2691 struct iwl_scan_offload_profile_match *matches =
2692 (void *)results->matches;
2693
2694 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2695 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2696 match->channels[n_channels++] =
2697 mvm->nd_channels[i]->center_freq;
2698 } else {
2699 struct iwl_scan_offload_profile_match_v1 *matches =
2700 (void *)results->matches;
2701
2702 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2703 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2704 match->channels[n_channels++] =
2705 mvm->nd_channels[i]->center_freq;
2706 }
2707 /* We may have ended up with fewer channels than we allocated. */
2708 match->n_channels = n_channels;
2709 }
2710
2711 /**
2712 * enum iwl_d3_notif - d3 notifications
2713 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2714 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2715 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2716 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2717 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2718 */
2719 enum iwl_d3_notif {
2720 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0),
2721 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1),
2722 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2),
2723 IWL_D3_ND_MATCH_INFO = BIT(3),
2724 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4)
2725 };
2726
2727 /* manage d3 resume data */
2728 struct iwl_d3_data {
2729 struct iwl_wowlan_status_data *status;
2730 bool test;
2731 u32 d3_end_flags;
2732 u32 notif_expected; /* bitmap - see &enum iwl_d3_notif */
2733 u32 notif_received; /* bitmap - see &enum iwl_d3_notif */
2734 struct iwl_mvm_nd_results *nd_results;
2735 bool nd_results_valid;
2736 };
2737
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2738 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2739 struct ieee80211_vif *vif,
2740 struct iwl_d3_data *d3_data)
2741 {
2742 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2743 struct cfg80211_wowlan_wakeup wakeup = {
2744 .pattern_idx = -1,
2745 };
2746 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2747 unsigned long matched_profiles;
2748 u32 reasons = 0;
2749 int i, n_matches, ret;
2750
2751 if (WARN_ON(!d3_data || !d3_data->status))
2752 goto out;
2753
2754 reasons = d3_data->status->wakeup_reasons;
2755
2756 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2757 wakeup.rfkill_release = true;
2758
2759 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2760 goto out;
2761
2762 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2763 WOWLAN_INFO_NOTIFICATION, 0)) {
2764 IWL_INFO(mvm, "Query FW for ND results\n");
2765 ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2766
2767 } else {
2768 IWL_INFO(mvm, "Notification based ND results\n");
2769 ret = d3_data->nd_results_valid ? 0 : -1;
2770 }
2771
2772 if (ret || !d3_data->nd_results->matched_profiles) {
2773 wakeup_report = NULL;
2774 goto out;
2775 }
2776
2777 matched_profiles = d3_data->nd_results->matched_profiles;
2778 if (mvm->n_nd_match_sets) {
2779 n_matches = hweight_long(matched_profiles);
2780 } else {
2781 IWL_ERR(mvm, "no net detect match information available\n");
2782 n_matches = 0;
2783 }
2784
2785 net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2786 GFP_KERNEL);
2787 if (!net_detect || !n_matches)
2788 goto out_report_nd;
2789 net_detect->n_matches = n_matches;
2790 n_matches = 0;
2791
2792 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2793 struct cfg80211_wowlan_nd_match *match;
2794 int idx, n_channels = 0;
2795
2796 n_channels = iwl_mvm_query_num_match_chans(mvm,
2797 d3_data->nd_results,
2798 i);
2799
2800 match = kzalloc(struct_size(match, channels, n_channels),
2801 GFP_KERNEL);
2802 if (!match)
2803 goto out_report_nd;
2804 match->n_channels = n_channels;
2805
2806 net_detect->matches[n_matches++] = match;
2807
2808 /* We inverted the order of the SSIDs in the scan
2809 * request, so invert the index here.
2810 */
2811 idx = mvm->n_nd_match_sets - i - 1;
2812 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2813 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2814 match->ssid.ssid_len);
2815
2816 if (mvm->n_nd_channels < n_channels)
2817 continue;
2818
2819 iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2820 }
2821 /* We may have fewer matches than we allocated. */
2822 net_detect->n_matches = n_matches;
2823
2824 out_report_nd:
2825 wakeup.net_detect = net_detect;
2826 out:
2827 iwl_mvm_free_nd(mvm);
2828
2829 mutex_unlock(&mvm->mutex);
2830 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2831
2832 if (net_detect) {
2833 for (i = 0; i < net_detect->n_matches; i++)
2834 kfree(net_detect->matches[i]);
2835 kfree(net_detect);
2836 }
2837 }
2838
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2839 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2840 struct ieee80211_vif *vif)
2841 {
2842 /* skip the one we keep connection on */
2843 if (data == vif)
2844 return;
2845
2846 if (vif->type == NL80211_IFTYPE_STATION)
2847 ieee80211_resume_disconnect(vif);
2848 }
2849
2850 enum rt_status {
2851 FW_ALIVE,
2852 FW_NEEDS_RESET,
2853 FW_ERROR,
2854 };
2855
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2856 static enum rt_status iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2857 struct ieee80211_vif *vif)
2858 {
2859 u32 err_id;
2860
2861 /* check for lmac1 error */
2862 if (iwl_fwrt_read_err_table(mvm->trans,
2863 mvm->trans->dbg.lmac_error_event_table[0],
2864 &err_id)) {
2865 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2866 IWL_WARN(mvm, "Rfkill was toggled during suspend\n");
2867 if (vif) {
2868 struct cfg80211_wowlan_wakeup wakeup = {
2869 .rfkill_release = true,
2870 };
2871
2872 ieee80211_report_wowlan_wakeup(vif, &wakeup,
2873 GFP_KERNEL);
2874 }
2875
2876 return FW_NEEDS_RESET;
2877 }
2878 return FW_ERROR;
2879 }
2880
2881 /* check if we have lmac2 set and check for error */
2882 if (iwl_fwrt_read_err_table(mvm->trans,
2883 mvm->trans->dbg.lmac_error_event_table[1],
2884 NULL))
2885 return FW_ERROR;
2886
2887 /* check for umac error */
2888 if (iwl_fwrt_read_err_table(mvm->trans,
2889 mvm->trans->dbg.umac_error_event_table,
2890 NULL))
2891 return FW_ERROR;
2892
2893 return FW_ALIVE;
2894 }
2895
2896 /*
2897 * This function assumes:
2898 * 1. The mutex is already held.
2899 * 2. The callee functions unlock the mutex.
2900 */
2901 static bool
iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2902 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2903 struct ieee80211_vif *vif,
2904 struct iwl_d3_data *d3_data)
2905 {
2906 lockdep_assert_held(&mvm->mutex);
2907
2908 /* if FW uses status notification, status shouldn't be NULL here */
2909 if (!d3_data->status) {
2910 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2911 u8 sta_id = mvm->net_detect ? IWL_INVALID_STA :
2912 mvmvif->deflink.ap_sta_id;
2913
2914 /* bug - FW with MLO has status notification */
2915 WARN_ON(ieee80211_vif_is_mld(vif));
2916
2917 d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2918 }
2919
2920 if (mvm->net_detect) {
2921 iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2922 } else {
2923 bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2924 d3_data->status);
2925
2926 #ifdef CONFIG_IWLWIFI_DEBUGFS
2927 if (keep)
2928 mvm->keep_vif = vif;
2929 #endif
2930
2931 return keep;
2932 }
2933 return false;
2934 }
2935
2936 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2937 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2938 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2939 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2940 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2941 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2942
iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm * mvm,struct iwl_wowlan_wake_pkt_notif * notif,struct iwl_wowlan_status_data * status,u32 len)2943 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2944 struct iwl_wowlan_wake_pkt_notif *notif,
2945 struct iwl_wowlan_status_data *status,
2946 u32 len)
2947 {
2948 u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2949
2950 if (len < sizeof(*notif)) {
2951 IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2952 return -EIO;
2953 }
2954
2955 if (WARN_ON(!status)) {
2956 IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2957 return -EIO;
2958 }
2959
2960 if (WARN_ON(!(status->wakeup_reasons &
2961 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2962 IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2963 status->wakeup_reasons);
2964 return -EIO;
2965 }
2966
2967 data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2968
2969 /* data_size got the padding from the notification, remove it. */
2970 if (packet_len < data_size)
2971 data_size = packet_len;
2972
2973 status->wake_packet = kmemdup(notif->wake_packet, data_size,
2974 GFP_ATOMIC);
2975
2976 if (!status->wake_packet)
2977 return -ENOMEM;
2978
2979 status->wake_packet_length = packet_len;
2980 status->wake_packet_bufsize = data_size;
2981
2982 return 0;
2983 }
2984
iwl_mvm_nd_match_info_handler(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data,struct iwl_scan_offload_match_info * notif,u32 len)2985 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2986 struct iwl_d3_data *d3_data,
2987 struct iwl_scan_offload_match_info *notif,
2988 u32 len)
2989 {
2990 struct iwl_wowlan_status_data *status = d3_data->status;
2991 struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2992 struct iwl_mvm_nd_results *results = d3_data->nd_results;
2993 size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2994 iwl_umac_scan_get_max_profiles(mvm->fw);
2995
2996 if (IS_ERR_OR_NULL(vif))
2997 return;
2998
2999 if (len < sizeof(struct iwl_scan_offload_match_info)) {
3000 IWL_ERR(mvm, "Invalid scan match info notification\n");
3001 return;
3002 }
3003
3004 if (!mvm->net_detect) {
3005 IWL_ERR(mvm, "Unexpected scan match info notification\n");
3006 return;
3007 }
3008
3009 if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
3010 IWL_ERR(mvm,
3011 "Ignore scan match info notification: no reason\n");
3012 return;
3013 }
3014
3015 #ifdef CONFIG_IWLWIFI_DEBUGFS
3016 mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
3017 #endif
3018
3019 results->matched_profiles = le32_to_cpu(notif->matched_profiles);
3020 IWL_INFO(mvm, "number of matched profiles=%u\n",
3021 results->matched_profiles);
3022
3023 if (results->matched_profiles) {
3024 memcpy(results->matches, notif->matches, matches_len);
3025 d3_data->nd_results_valid = true;
3026 }
3027
3028 /* no scan should be active at this point */
3029 mvm->scan_status = 0;
3030 for (i = 0; i < mvm->max_scans; i++)
3031 mvm->scan_uid_status[i] = 0;
3032 }
3033
iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)3034 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
3035 struct iwl_rx_packet *pkt, void *data)
3036 {
3037 struct iwl_mvm *mvm =
3038 container_of(notif_wait, struct iwl_mvm, notif_wait);
3039 struct iwl_d3_data *d3_data = data;
3040 u32 len = iwl_rx_packet_payload_len(pkt);
3041 int ret;
3042 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
3043 PROT_OFFLOAD_GROUP,
3044 WOWLAN_INFO_NOTIFICATION,
3045 IWL_FW_CMD_VER_UNKNOWN);
3046
3047
3048 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
3049 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
3050
3051 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
3052 /* We might get two notifications due to dual bss */
3053 IWL_DEBUG_WOWLAN(mvm,
3054 "Got additional wowlan info notification\n");
3055 break;
3056 }
3057
3058 if (wowlan_info_ver == 1) {
3059 struct iwl_wowlan_info_notif_v1 *notif_v1 =
3060 (void *)pkt->data;
3061
3062 iwl_mvm_parse_wowlan_info_notif_v1(mvm, notif_v1,
3063 d3_data->status,
3064 len);
3065 } else if (wowlan_info_ver == 3) {
3066 struct iwl_wowlan_info_notif_v3 *notif =
3067 (void *)pkt->data;
3068
3069 iwl_mvm_parse_wowlan_info_notif_v3(mvm, notif,
3070 d3_data->status, len);
3071 } else if (wowlan_info_ver == 5) {
3072 struct iwl_wowlan_info_notif *notif =
3073 (void *)pkt->data;
3074
3075 iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3076 d3_data->status, len);
3077 } else {
3078 IWL_FW_CHECK(mvm, 1,
3079 "Firmware advertises unknown WoWLAN info notification %d!\n",
3080 wowlan_info_ver);
3081 return false;
3082 }
3083
3084 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3085
3086 if (d3_data->status &&
3087 d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3088 /* We are supposed to get also wake packet notif */
3089 d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3090
3091 break;
3092 }
3093 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3094 struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3095
3096 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3097 /* We shouldn't get two wake packet notifications */
3098 IWL_ERR(mvm,
3099 "Got additional wowlan wake packet notification\n");
3100 } else {
3101 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3102 len = iwl_rx_packet_payload_len(pkt);
3103 ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3104 d3_data->status,
3105 len);
3106 if (ret)
3107 IWL_ERR(mvm,
3108 "Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3109 }
3110
3111 break;
3112 }
3113 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3114 struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3115
3116 if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3117 IWL_ERR(mvm,
3118 "Got additional netdetect match info\n");
3119 break;
3120 }
3121
3122 d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3123
3124 /* explicitly set this in the 'expected' as well */
3125 d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3126
3127 len = iwl_rx_packet_payload_len(pkt);
3128 iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3129 break;
3130 }
3131 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3132 struct iwl_d3_end_notif *notif = (void *)pkt->data;
3133
3134 d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3135 d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3136
3137 break;
3138 }
3139 default:
3140 WARN_ON(1);
3141 }
3142
3143 return d3_data->notif_received == d3_data->notif_expected;
3144 }
3145
iwl_mvm_resume_firmware(struct iwl_mvm * mvm,bool test)3146 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3147 {
3148 int ret;
3149 enum iwl_d3_status d3_status;
3150 struct iwl_host_cmd cmd = {
3151 .id = D0I3_END_CMD,
3152 .flags = CMD_WANT_SKB,
3153 };
3154 bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3155 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3156
3157 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3158 if (ret)
3159 return ret;
3160
3161 if (d3_status != IWL_D3_STATUS_ALIVE) {
3162 IWL_INFO(mvm, "Device was reset during suspend\n");
3163 return -ENOENT;
3164 }
3165
3166 /*
3167 * We should trigger resume flow using command only for 22000 family
3168 * AX210 and above don't need the command since they have
3169 * the doorbell interrupt.
3170 */
3171 if (mvm->trans->mac_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3172 fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3173 ret = iwl_mvm_send_cmd(mvm, &cmd);
3174 if (ret < 0)
3175 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3176 ret);
3177 }
3178
3179 return ret;
3180 }
3181
3182 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 3)
3183
iwl_mvm_d3_notif_wait(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data)3184 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3185 struct iwl_d3_data *d3_data)
3186 {
3187 static const u16 d3_resume_notif[] = {
3188 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3189 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3190 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3191 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3192 };
3193 static const u16 d3_fast_resume_notif[] = {
3194 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3195 };
3196 struct iwl_notification_wait wait_d3_notif;
3197 int ret;
3198
3199 if (mvm->fast_resume)
3200 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3201 d3_fast_resume_notif,
3202 ARRAY_SIZE(d3_fast_resume_notif),
3203 iwl_mvm_wait_d3_notif, d3_data);
3204 else
3205 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3206 d3_resume_notif,
3207 ARRAY_SIZE(d3_resume_notif),
3208 iwl_mvm_wait_d3_notif, d3_data);
3209
3210 ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3211 if (ret) {
3212 iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3213 return ret;
3214 }
3215
3216 return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3217 IWL_MVM_D3_NOTIF_TIMEOUT);
3218 }
3219
iwl_mvm_d3_resume_notif_based(struct iwl_mvm * mvm)3220 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3221 {
3222 return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3223 WOWLAN_INFO_NOTIFICATION, 0) &&
3224 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3225 WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3226 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3227 D3_END_NOTIFICATION, 0);
3228 }
3229
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)3230 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3231 {
3232 struct ieee80211_vif *vif = NULL;
3233 int ret = 1;
3234 struct iwl_mvm_nd_results results = {};
3235 struct iwl_d3_data d3_data = {
3236 .test = test,
3237 .notif_expected =
3238 IWL_D3_NOTIF_WOWLAN_INFO |
3239 IWL_D3_NOTIF_D3_END_NOTIF,
3240 .nd_results_valid = false,
3241 .nd_results = &results,
3242 };
3243 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3244 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3245 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3246 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3247 bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3248 enum rt_status rt_status;
3249 bool keep = false;
3250
3251 mutex_lock(&mvm->mutex);
3252
3253 /* Apparently, the device went away and device_powered_off() was called,
3254 * don't even try to read the rt_status, the device is currently
3255 * inaccessible.
3256 */
3257 if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
3258 IWL_INFO(mvm,
3259 "Can't resume, device_powered_off() was called during wowlan\n");
3260 goto err;
3261 }
3262
3263 mvm->last_reset_or_resume_time_jiffies = jiffies;
3264
3265 /* get the BSS vif pointer again */
3266 vif = iwl_mvm_get_bss_vif(mvm);
3267 if (IS_ERR_OR_NULL(vif))
3268 goto err;
3269
3270 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3271
3272 rt_status = iwl_mvm_check_rt_status(mvm, vif);
3273 if (rt_status != FW_ALIVE) {
3274 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3275 if (rt_status == FW_ERROR) {
3276 IWL_ERR(mvm, "FW Error occurred during suspend. Restarting.\n");
3277 iwl_mvm_dump_nic_error_log(mvm);
3278 iwl_dbg_tlv_time_point(&mvm->fwrt,
3279 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3280 NULL);
3281 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3282 &iwl_dump_desc_assert,
3283 false, 0);
3284 }
3285 ret = 1;
3286 goto err;
3287 }
3288
3289 if (resume_notif_based) {
3290 d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3291 if (!d3_data.status) {
3292 IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3293 ret = -ENOMEM;
3294 goto err;
3295 }
3296
3297 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3298 if (ret)
3299 goto err;
3300 } else {
3301 ret = iwl_mvm_resume_firmware(mvm, test);
3302 if (ret < 0)
3303 goto err;
3304 }
3305
3306 iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
3307
3308 /* when reset is required we can't send these following commands */
3309 if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3310 goto query_wakeup_reasons;
3311
3312 /*
3313 * Query the current location and source from the D3 firmware so we
3314 * can play it back when we re-intiailize the D0 firmware
3315 */
3316 iwl_mvm_update_changed_regdom(mvm);
3317
3318 /* Re-configure PPAG settings */
3319 iwl_mvm_ppag_send_cmd(mvm);
3320
3321 if (!unified_image)
3322 /* Re-configure default SAR profile */
3323 iwl_mvm_sar_select_profile(mvm, 1, 1);
3324
3325 if (mvm->net_detect && unified_image) {
3326 /* If this is a non-unified image, we restart the FW,
3327 * so no need to stop the netdetect scan. If that
3328 * fails, continue and try to get the wake-up reasons,
3329 * but trigger a HW restart by keeping a failure code
3330 * in ret.
3331 */
3332 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3333 false);
3334 }
3335
3336 query_wakeup_reasons:
3337 keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3338 /* has unlocked the mutex, so skip that */
3339 goto out;
3340
3341 err:
3342 mutex_unlock(&mvm->mutex);
3343 out:
3344 if (d3_data.status)
3345 kfree(d3_data.status->wake_packet);
3346 kfree(d3_data.status);
3347 iwl_mvm_free_nd(mvm);
3348
3349 if (!d3_data.test && !mvm->net_detect)
3350 ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3351 IEEE80211_IFACE_ITER_NORMAL,
3352 iwl_mvm_d3_disconnect_iter,
3353 keep ? vif : NULL);
3354
3355 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3356
3357 /* no need to reset the device in unified images, if successful */
3358 if (unified_image && !ret) {
3359 /* nothing else to do if we already sent D0I3_END_CMD */
3360 if (d0i3_first)
3361 return 0;
3362
3363 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3364 D3_END_NOTIFICATION, 0)) {
3365 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3366 if (!ret)
3367 return 0;
3368 } else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3369 return 0;
3370 }
3371 }
3372
3373 /*
3374 * Reconfigure the device in one of the following cases:
3375 * 1. We are not using a unified image
3376 * 2. We are using a unified image but had an error while exiting D3
3377 */
3378 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3379
3380 return 1;
3381 }
3382
iwl_mvm_resume(struct ieee80211_hw * hw)3383 int iwl_mvm_resume(struct ieee80211_hw *hw)
3384 {
3385 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3386 int ret;
3387
3388 ret = __iwl_mvm_resume(mvm, false);
3389
3390 iwl_mvm_resume_tcm(mvm);
3391
3392 iwl_fw_runtime_resume(&mvm->fwrt);
3393
3394 return ret;
3395 }
3396
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)3397 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3398 {
3399 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3400
3401 device_set_wakeup_enable(mvm->trans->dev, enabled);
3402 }
3403
iwl_mvm_fast_suspend(struct iwl_mvm * mvm)3404 void iwl_mvm_fast_suspend(struct iwl_mvm *mvm)
3405 {
3406 struct iwl_d3_manager_config d3_cfg_cmd_data = {};
3407 int ret;
3408
3409 lockdep_assert_held(&mvm->mutex);
3410
3411 IWL_DEBUG_WOWLAN(mvm, "Starting fast suspend flow\n");
3412
3413 mvm->fast_resume = true;
3414 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3415
3416 WARN_ON(iwl_mvm_power_update_device(mvm));
3417 ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, 0,
3418 sizeof(d3_cfg_cmd_data), &d3_cfg_cmd_data);
3419 if (ret)
3420 IWL_ERR(mvm,
3421 "fast suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
3422
3423 ret = iwl_trans_d3_suspend(mvm->trans, false, false);
3424 if (ret)
3425 IWL_ERR(mvm, "fast suspend: trans_d3_suspend failed %d\n", ret);
3426 }
3427
iwl_mvm_fast_resume(struct iwl_mvm * mvm)3428 int iwl_mvm_fast_resume(struct iwl_mvm *mvm)
3429 {
3430 struct iwl_d3_data d3_data = {
3431 .notif_expected =
3432 IWL_D3_NOTIF_D3_END_NOTIF,
3433 };
3434 enum rt_status rt_status;
3435 int ret;
3436
3437 lockdep_assert_held(&mvm->mutex);
3438
3439 IWL_DEBUG_WOWLAN(mvm, "Starting the fast resume flow\n");
3440
3441 mvm->last_reset_or_resume_time_jiffies = jiffies;
3442 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3443
3444 rt_status = iwl_mvm_check_rt_status(mvm, NULL);
3445 if (rt_status != FW_ALIVE) {
3446 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3447 if (rt_status == FW_ERROR) {
3448 IWL_ERR(mvm,
3449 "iwl_mvm_check_rt_status failed, device is gone during suspend\n");
3450 iwl_mvm_dump_nic_error_log(mvm);
3451 iwl_dbg_tlv_time_point(&mvm->fwrt,
3452 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3453 NULL);
3454 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3455 &iwl_dump_desc_assert,
3456 false, 0);
3457 }
3458 mvm->trans->state = IWL_TRANS_NO_FW;
3459 ret = -ENODEV;
3460
3461 goto out;
3462 }
3463 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3464
3465 if (ret) {
3466 IWL_ERR(mvm, "Couldn't get the d3 notif %d\n", ret);
3467 mvm->trans->state = IWL_TRANS_NO_FW;
3468 }
3469
3470 out:
3471 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3472 mvm->fast_resume = false;
3473
3474 return ret;
3475 }
3476
3477 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)3478 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3479 {
3480 struct iwl_mvm *mvm = inode->i_private;
3481 int err;
3482
3483 if (mvm->d3_test_active)
3484 return -EBUSY;
3485
3486 file->private_data = inode->i_private;
3487
3488 iwl_mvm_pause_tcm(mvm, true);
3489
3490 iwl_fw_runtime_suspend(&mvm->fwrt);
3491
3492 /* start pseudo D3 */
3493 rtnl_lock();
3494 wiphy_lock(mvm->hw->wiphy);
3495 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3496 wiphy_unlock(mvm->hw->wiphy);
3497 rtnl_unlock();
3498 if (err > 0)
3499 err = -EINVAL;
3500 if (err)
3501 return err;
3502
3503 mvm->d3_test_active = true;
3504 mvm->keep_vif = NULL;
3505 return 0;
3506 }
3507
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3508 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3509 size_t count, loff_t *ppos)
3510 {
3511 struct iwl_mvm *mvm = file->private_data;
3512 unsigned long end = jiffies + 60 * HZ;
3513 u32 pme_asserted;
3514
3515 while (true) {
3516 /* read pme_ptr if available */
3517 if (mvm->d3_test_pme_ptr) {
3518 pme_asserted = iwl_trans_read_mem32(mvm->trans,
3519 mvm->d3_test_pme_ptr);
3520 if (pme_asserted)
3521 break;
3522 }
3523
3524 if (msleep_interruptible(100))
3525 break;
3526
3527 if (time_is_before_jiffies(end)) {
3528 IWL_ERR(mvm,
3529 "ending pseudo-D3 with timeout after ~60 seconds\n");
3530 return -ETIMEDOUT;
3531 }
3532 }
3533
3534 return 0;
3535 }
3536
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)3537 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3538 struct ieee80211_vif *vif)
3539 {
3540 /* skip the one we keep connection on */
3541 if (_data == vif)
3542 return;
3543
3544 if (vif->type == NL80211_IFTYPE_STATION)
3545 ieee80211_connection_loss(vif);
3546 }
3547
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)3548 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3549 {
3550 struct iwl_mvm *mvm = inode->i_private;
3551 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3552 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3553
3554 mvm->d3_test_active = false;
3555
3556 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3557
3558 rtnl_lock();
3559 wiphy_lock(mvm->hw->wiphy);
3560 __iwl_mvm_resume(mvm, true);
3561 wiphy_unlock(mvm->hw->wiphy);
3562 rtnl_unlock();
3563
3564 iwl_mvm_resume_tcm(mvm);
3565
3566 iwl_fw_runtime_resume(&mvm->fwrt);
3567
3568 iwl_abort_notification_waits(&mvm->notif_wait);
3569 if (!unified_image) {
3570 int remaining_time = 10;
3571
3572 ieee80211_restart_hw(mvm->hw);
3573
3574 /* wait for restart and disconnect all interfaces */
3575 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3576 remaining_time > 0) {
3577 remaining_time--;
3578 msleep(1000);
3579 }
3580
3581 if (remaining_time == 0)
3582 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3583 }
3584
3585 ieee80211_iterate_active_interfaces_atomic(
3586 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3587 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3588
3589 return 0;
3590 }
3591
3592 const struct file_operations iwl_dbgfs_d3_test_ops = {
3593 .open = iwl_mvm_d3_test_open,
3594 .read = iwl_mvm_d3_test_read,
3595 .release = iwl_mvm_d3_test_release,
3596 };
3597 #endif
3598