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