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 .offloading_tid = 0,
1298 };
1299
1300 wowlan_config_cmd.sta_id = mvmvif->deflink.ap_sta_id;
1301
1302 ap_sta = rcu_dereference_protected(
1303 mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
1304 lockdep_is_held(&mvm->mutex));
1305 if (IS_ERR_OR_NULL(ap_sta)) {
1306 ret = -EINVAL;
1307 goto out_noreset;
1308 }
1309
1310 ret = iwl_mvm_sta_ensure_queue(
1311 mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1312 if (ret)
1313 goto out_noreset;
1314
1315 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1316 vif, mvmvif, ap_sta);
1317 if (ret)
1318 goto out_noreset;
1319 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1320 vif, mvmvif, ap_sta);
1321 if (ret)
1322 goto out;
1323
1324 mvm->net_detect = false;
1325 }
1326
1327 ret = iwl_mvm_power_update_device(mvm);
1328 if (ret)
1329 goto out;
1330
1331 ret = iwl_mvm_power_update_mac(mvm);
1332 if (ret)
1333 goto out;
1334
1335 #ifdef CONFIG_IWLWIFI_DEBUGFS
1336 if (mvm->d3_wake_sysassert)
1337 d3_cfg_cmd_data.wakeup_flags |=
1338 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1339 #endif
1340
1341 /*
1342 * Prior to 9000 device family the driver needs to stop the dbg
1343 * recording before entering D3. In later devices the FW stops the
1344 * recording automatically.
1345 */
1346 if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1347 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1348
1349 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1350
1351 /* must be last -- this switches firmware state */
1352 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1353 if (ret)
1354 goto out;
1355 #ifdef CONFIG_IWLWIFI_DEBUGFS
1356 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1357 if (len >= sizeof(u32)) {
1358 mvm->d3_test_pme_ptr =
1359 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1360 }
1361 #endif
1362 iwl_free_resp(&d3_cfg_cmd);
1363
1364 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1365
1366 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1367 out:
1368 if (ret < 0) {
1369 iwl_mvm_free_nd(mvm);
1370
1371 if (!unified_image) {
1372 if (mvm->fw_restart > 0) {
1373 mvm->fw_restart--;
1374 ieee80211_restart_hw(mvm->hw);
1375 }
1376 }
1377
1378 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1379 }
1380 out_noreset:
1381 mutex_unlock(&mvm->mutex);
1382
1383 return ret;
1384 }
1385
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1386 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1387 {
1388 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1389
1390 iwl_mvm_pause_tcm(mvm, true);
1391
1392 mutex_lock(&mvm->mutex);
1393 iwl_fw_runtime_suspend(&mvm->fwrt);
1394 mutex_unlock(&mvm->mutex);
1395
1396 return __iwl_mvm_suspend(hw, wowlan, false);
1397 }
1398
1399 struct iwl_multicast_key_data {
1400 u8 key[WOWLAN_KEY_MAX_SIZE];
1401 u8 len;
1402 u8 flags;
1403 u8 id;
1404 u8 ipn[6];
1405 };
1406
1407 /* converted data from the different status responses */
1408 struct iwl_wowlan_status_data {
1409 u64 replay_ctr;
1410 u32 num_of_gtk_rekeys;
1411 u32 received_beacons;
1412 u32 wakeup_reasons;
1413 u32 wake_packet_length;
1414 u32 wake_packet_bufsize;
1415 u16 pattern_number;
1416 u16 non_qos_seq_ctr;
1417 u16 qos_seq_ctr[8];
1418 u8 tid_tear_down;
1419
1420 struct {
1421 /* including RX MIC key for TKIP */
1422 u8 key[WOWLAN_KEY_MAX_SIZE];
1423 u8 len;
1424 u8 flags;
1425 u8 id;
1426 } gtk[WOWLAN_GTK_KEYS_NUM];
1427
1428 struct {
1429 /*
1430 * We store both the TKIP and AES representations
1431 * coming from the firmware because we decode the
1432 * data from there before we iterate the keys and
1433 * know which one we need.
1434 */
1435 struct {
1436 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1437 } tkip, aes;
1438
1439 /*
1440 * We use -1 for when we have valid data but don't know
1441 * the key ID from firmware, and thus it needs to be
1442 * installed with the last key (depending on rekeying).
1443 */
1444 s8 key_id;
1445 bool valid;
1446 } gtk_seq[2];
1447
1448 struct {
1449 /* Same as above */
1450 struct {
1451 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1452 u64 tx_pn;
1453 } tkip, aes;
1454 } ptk;
1455
1456 struct iwl_multicast_key_data igtk;
1457
1458 u8 *wake_packet;
1459 };
1460
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1461 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1462 struct ieee80211_vif *vif,
1463 struct iwl_wowlan_status_data *status)
1464 {
1465 struct sk_buff *pkt = NULL;
1466 struct cfg80211_wowlan_wakeup wakeup = {
1467 .pattern_idx = -1,
1468 };
1469 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1470 u32 reasons = status->wakeup_reasons;
1471
1472 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1473 wakeup_report = NULL;
1474 goto report;
1475 }
1476
1477 pm_wakeup_event(mvm->dev, 0);
1478
1479 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1480 wakeup.magic_pkt = true;
1481
1482 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1483 wakeup.pattern_idx =
1484 status->pattern_number;
1485
1486 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1487 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1488 wakeup.disconnect = true;
1489
1490 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1491 wakeup.gtk_rekey_failure = true;
1492
1493 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1494 wakeup.rfkill_release = true;
1495
1496 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1497 wakeup.eap_identity_req = true;
1498
1499 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1500 wakeup.four_way_handshake = true;
1501
1502 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1503 wakeup.tcp_connlost = true;
1504
1505 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1506 wakeup.tcp_nomoretokens = true;
1507
1508 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1509 wakeup.tcp_match = true;
1510
1511 if (status->wake_packet) {
1512 int pktsize = status->wake_packet_bufsize;
1513 int pktlen = status->wake_packet_length;
1514 const u8 *pktdata = status->wake_packet;
1515 const struct ieee80211_hdr *hdr = (const void *)pktdata;
1516 int truncated = pktlen - pktsize;
1517
1518 /* this would be a firmware bug */
1519 if (WARN_ON_ONCE(truncated < 0))
1520 truncated = 0;
1521
1522 if (ieee80211_is_data(hdr->frame_control)) {
1523 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1524 int ivlen = 0, icvlen = 4; /* also FCS */
1525
1526 pkt = alloc_skb(pktsize, GFP_KERNEL);
1527 if (!pkt)
1528 goto report;
1529
1530 skb_put_data(pkt, pktdata, hdrlen);
1531 pktdata += hdrlen;
1532 pktsize -= hdrlen;
1533
1534 if (ieee80211_has_protected(hdr->frame_control)) {
1535 /*
1536 * This is unlocked and using gtk_i(c)vlen,
1537 * but since everything is under RTNL still
1538 * that's not really a problem - changing
1539 * it would be difficult.
1540 */
1541 if (is_multicast_ether_addr(hdr->addr1)) {
1542 ivlen = mvm->gtk_ivlen;
1543 icvlen += mvm->gtk_icvlen;
1544 } else {
1545 ivlen = mvm->ptk_ivlen;
1546 icvlen += mvm->ptk_icvlen;
1547 }
1548 }
1549
1550 /* if truncated, FCS/ICV is (partially) gone */
1551 if (truncated >= icvlen) {
1552 icvlen = 0;
1553 truncated -= icvlen;
1554 } else {
1555 icvlen -= truncated;
1556 truncated = 0;
1557 }
1558
1559 pktsize -= ivlen + icvlen;
1560 pktdata += ivlen;
1561
1562 skb_put_data(pkt, pktdata, pktsize);
1563
1564 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1565 goto report;
1566 wakeup.packet = pkt->data;
1567 wakeup.packet_present_len = pkt->len;
1568 wakeup.packet_len = pkt->len - truncated;
1569 wakeup.packet_80211 = false;
1570 } else {
1571 int fcslen = 4;
1572
1573 if (truncated >= 4) {
1574 truncated -= 4;
1575 fcslen = 0;
1576 } else {
1577 fcslen -= truncated;
1578 truncated = 0;
1579 }
1580 pktsize -= fcslen;
1581 wakeup.packet = status->wake_packet;
1582 wakeup.packet_present_len = pktsize;
1583 wakeup.packet_len = pktlen - truncated;
1584 wakeup.packet_80211 = true;
1585 }
1586 }
1587
1588 report:
1589 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1590 kfree_skb(pkt);
1591 }
1592
iwl_mvm_le64_to_aes_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1593 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1594 {
1595 u64 pn = le64_to_cpu(le_pn);
1596
1597 seq->ccmp.pn[0] = pn >> 40;
1598 seq->ccmp.pn[1] = pn >> 32;
1599 seq->ccmp.pn[2] = pn >> 24;
1600 seq->ccmp.pn[3] = pn >> 16;
1601 seq->ccmp.pn[4] = pn >> 8;
1602 seq->ccmp.pn[5] = pn;
1603 }
1604
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1605 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1606 struct ieee80211_key_seq *seq)
1607 {
1608 iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1609 }
1610
iwl_mvm_le64_to_tkip_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1611 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1612 {
1613 u64 pn = le64_to_cpu(le_pn);
1614
1615 seq->tkip.iv16 = (u16)pn;
1616 seq->tkip.iv32 = (u32)(pn >> 16);
1617 }
1618
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1619 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1620 struct ieee80211_key_seq *seq)
1621 {
1622 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1623 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1624 }
1625
iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf * key,struct ieee80211_key_seq * seq)1626 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1627 struct ieee80211_key_seq *seq)
1628 {
1629 int tid;
1630
1631 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1632 ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1633 }
1634
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)1635 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1636 struct iwl_wowlan_status_data *status,
1637 struct ieee80211_sta *sta,
1638 struct ieee80211_key_conf *key)
1639 {
1640 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1641 struct iwl_mvm_key_pn *ptk_pn;
1642 int tid;
1643
1644 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1645
1646 if (!iwl_mvm_has_new_rx_api(mvm))
1647 return;
1648
1649
1650 rcu_read_lock();
1651 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1652 if (WARN_ON(!ptk_pn)) {
1653 rcu_read_unlock();
1654 return;
1655 }
1656
1657 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1658 int i;
1659
1660 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1661 memcpy(ptk_pn->q[i].pn[tid],
1662 status->ptk.aes.seq[tid].ccmp.pn,
1663 IEEE80211_CCMP_PN_LEN);
1664 }
1665 rcu_read_unlock();
1666 }
1667
iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data * status,union iwl_all_tsc_rsc * sc)1668 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1669 union iwl_all_tsc_rsc *sc)
1670 {
1671 int i;
1672
1673 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1674 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1675
1676 /* GTK RX counters */
1677 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1678 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1679 &status->gtk_seq[0].tkip.seq[i]);
1680 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1681 &status->gtk_seq[0].aes.seq[i]);
1682 }
1683 status->gtk_seq[0].valid = true;
1684 status->gtk_seq[0].key_id = -1;
1685
1686 /* PTK TX counter */
1687 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1688 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1689 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1690
1691 /* PTK RX counters */
1692 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1693 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1694 &status->ptk.tkip.seq[i]);
1695 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1696 &status->ptk.aes.seq[i]);
1697 }
1698 }
1699
1700 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)1701 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1702 struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1703 unsigned int idx, unsigned int key_id)
1704 {
1705 int tid;
1706
1707 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1708 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1709 &status->gtk_seq[idx].tkip.seq[tid]);
1710 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1711 &status->gtk_seq[idx].aes.seq[tid]);
1712 }
1713
1714 status->gtk_seq[idx].valid = true;
1715 status->gtk_seq[idx].key_id = key_id;
1716 }
1717
1718 static void
iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc)1719 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1720 struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1721 {
1722 int i, tid;
1723
1724 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1725 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1726 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1727
1728 /* GTK RX counters */
1729 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1730 u8 entry = sc->mcast_key_id_map[i];
1731
1732 if (entry < ARRAY_SIZE(sc->mcast_rsc))
1733 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1734 entry, i);
1735 }
1736
1737 /* PTK TX counters not needed, assigned in device */
1738
1739 /* PTK RX counters */
1740 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1741 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1742 &status->ptk.tkip.seq[tid]);
1743 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1744 &status->ptk.aes.seq[tid]);
1745 }
1746 }
1747
iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,int idx)1748 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1749 struct iwl_wowlan_status_data *status,
1750 int idx)
1751 {
1752 switch (key->cipher) {
1753 case WLAN_CIPHER_SUITE_CCMP:
1754 case WLAN_CIPHER_SUITE_GCMP:
1755 case WLAN_CIPHER_SUITE_GCMP_256:
1756 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1757 break;
1758 case WLAN_CIPHER_SUITE_TKIP:
1759 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1760 break;
1761 default:
1762 WARN_ON(1);
1763 }
1764 }
1765
iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,bool installed)1766 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1767 struct iwl_wowlan_status_data *status,
1768 bool installed)
1769 {
1770 int i;
1771
1772 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1773 if (!status->gtk_seq[i].valid)
1774 continue;
1775
1776 /* Handle the case where we know the key ID */
1777 if (status->gtk_seq[i].key_id == key->keyidx) {
1778 s8 new_key_id = -1;
1779
1780 if (status->num_of_gtk_rekeys)
1781 new_key_id = status->gtk[0].flags &
1782 IWL_WOWLAN_GTK_IDX_MASK;
1783
1784 /* Don't install a new key's value to an old key */
1785 if (new_key_id != key->keyidx)
1786 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1787 continue;
1788 }
1789
1790 /* handle the case where we didn't, last key only */
1791 if (status->gtk_seq[i].key_id == -1 &&
1792 (!status->num_of_gtk_rekeys || installed))
1793 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1794 }
1795 }
1796
1797 struct iwl_mvm_d3_gtk_iter_data {
1798 struct iwl_mvm *mvm;
1799 struct iwl_wowlan_status_data *status;
1800 u32 gtk_cipher, igtk_cipher;
1801 bool unhandled_cipher, igtk_support;
1802 int num_keys;
1803 };
1804
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)1805 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1806 struct ieee80211_vif *vif,
1807 struct ieee80211_sta *sta,
1808 struct ieee80211_key_conf *key,
1809 void *_data)
1810 {
1811 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1812
1813 if (data->unhandled_cipher)
1814 return;
1815
1816 switch (key->cipher) {
1817 case WLAN_CIPHER_SUITE_WEP40:
1818 case WLAN_CIPHER_SUITE_WEP104:
1819 /* ignore WEP completely, nothing to do */
1820 return;
1821 case WLAN_CIPHER_SUITE_CCMP:
1822 case WLAN_CIPHER_SUITE_GCMP:
1823 case WLAN_CIPHER_SUITE_GCMP_256:
1824 case WLAN_CIPHER_SUITE_TKIP:
1825 /* we support these */
1826 data->gtk_cipher = key->cipher;
1827 break;
1828 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1829 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1830 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1831 case WLAN_CIPHER_SUITE_AES_CMAC:
1832 /* we support these */
1833 if (data->igtk_support &&
1834 (key->keyidx == 4 || key->keyidx == 5)) {
1835 data->igtk_cipher = key->cipher;
1836 } else {
1837 data->unhandled_cipher = true;
1838 return;
1839 }
1840 break;
1841 default:
1842 /* everything else - disconnect from AP */
1843 data->unhandled_cipher = true;
1844 return;
1845 }
1846
1847 data->num_keys++;
1848 }
1849
1850 static void
iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data * key,struct ieee80211_key_seq * seq,u32 cipher)1851 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1852 struct ieee80211_key_seq *seq, u32 cipher)
1853 {
1854 switch (cipher) {
1855 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1856 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1857 BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1858 memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1859 break;
1860 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1861 case WLAN_CIPHER_SUITE_AES_CMAC:
1862 BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1863 memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1864 break;
1865 default:
1866 WARN_ON(1);
1867 }
1868 }
1869
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1870 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1871 struct ieee80211_vif *vif,
1872 struct ieee80211_sta *sta,
1873 struct ieee80211_key_conf *key,
1874 void *_data)
1875 {
1876 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1877 struct iwl_wowlan_status_data *status = data->status;
1878 s8 keyidx;
1879
1880 if (data->unhandled_cipher)
1881 return;
1882
1883 switch (key->cipher) {
1884 case WLAN_CIPHER_SUITE_WEP40:
1885 case WLAN_CIPHER_SUITE_WEP104:
1886 /* ignore WEP completely, nothing to do */
1887 return;
1888 case WLAN_CIPHER_SUITE_CCMP:
1889 case WLAN_CIPHER_SUITE_GCMP:
1890 case WLAN_CIPHER_SUITE_GCMP_256:
1891 if (sta) {
1892 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1893 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1894 return;
1895 }
1896 fallthrough;
1897 case WLAN_CIPHER_SUITE_TKIP:
1898 if (sta) {
1899 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1900 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1901 return;
1902 }
1903 keyidx = key->keyidx;
1904 /* The current key is always sent by the FW, even if it wasn't
1905 * rekeyed during D3.
1906 * We remove an existing key if it has the same index as
1907 * a new key
1908 */
1909 if (status->num_of_gtk_rekeys &&
1910 ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1911 (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1912 ieee80211_remove_key(key);
1913 } else {
1914 iwl_mvm_set_key_rx_seq(key, data->status, false);
1915 }
1916 break;
1917 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1918 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1919 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1920 case WLAN_CIPHER_SUITE_AES_CMAC:
1921 if (key->keyidx == 4 || key->keyidx == 5) {
1922 /* remove rekeyed key */
1923 if (status->num_of_gtk_rekeys) {
1924 ieee80211_remove_key(key);
1925 } else {
1926 struct ieee80211_key_seq seq;
1927
1928 iwl_mvm_d3_set_igtk_bigtk_ipn(&status->igtk,
1929 &seq,
1930 key->cipher);
1931 ieee80211_set_key_rx_seq(key, 0, &seq);
1932 }
1933 }
1934 }
1935 }
1936
iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm,u32 gtk_cipher)1937 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
1938 struct ieee80211_vif *vif,
1939 struct iwl_mvm *mvm, u32 gtk_cipher)
1940 {
1941 int i;
1942 struct ieee80211_key_conf *key;
1943 struct {
1944 struct ieee80211_key_conf conf;
1945 u8 key[32];
1946 } conf = {
1947 .conf.cipher = gtk_cipher,
1948 };
1949
1950 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1951 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
1952 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
1953 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
1954 BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
1955
1956 switch (gtk_cipher) {
1957 case WLAN_CIPHER_SUITE_CCMP:
1958 case WLAN_CIPHER_SUITE_GCMP:
1959 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1960 break;
1961 case WLAN_CIPHER_SUITE_GCMP_256:
1962 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
1963 break;
1964 case WLAN_CIPHER_SUITE_TKIP:
1965 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1966 break;
1967 default:
1968 WARN_ON(1);
1969 }
1970
1971 for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
1972 if (!status->gtk[i].len)
1973 continue;
1974
1975 conf.conf.keyidx = status->gtk[i].id;
1976 IWL_DEBUG_WOWLAN(mvm,
1977 "Received from FW GTK cipher %d, key index %d\n",
1978 conf.conf.cipher, conf.conf.keyidx);
1979 memcpy(conf.conf.key, status->gtk[i].key,
1980 sizeof(status->gtk[i].key));
1981
1982 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1983 if (IS_ERR(key))
1984 return false;
1985 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1986 }
1987
1988 return true;
1989 }
1990
1991 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)1992 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
1993 struct ieee80211_vif *vif, u32 cipher,
1994 struct iwl_multicast_key_data *key_data)
1995 {
1996 struct ieee80211_key_conf *key_config;
1997 struct {
1998 struct ieee80211_key_conf conf;
1999 u8 key[WOWLAN_KEY_MAX_SIZE];
2000 } conf = {
2001 .conf.cipher = cipher,
2002 .conf.keyidx = key_data->id,
2003 };
2004 struct ieee80211_key_seq seq;
2005
2006 if (!key_data->len)
2007 return true;
2008
2009 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
2010
2011 switch (cipher) {
2012 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2013 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2014 break;
2015 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2016 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2017 break;
2018 case WLAN_CIPHER_SUITE_AES_CMAC:
2019 conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2020 break;
2021 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2022 conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2023 break;
2024 default:
2025 WARN_ON(1);
2026 }
2027 BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2028 memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2029
2030 key_config = ieee80211_gtk_rekey_add(vif, &conf.conf);
2031 if (IS_ERR(key_config))
2032 return false;
2033 ieee80211_set_key_rx_seq(key_config, 0, &seq);
2034
2035 if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2036 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2037 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2038 struct iwl_mvm_vif_link_info *mvm_link =
2039 mvmvif->link[link_id];
2040
2041 mvm_link->igtk = key_config;
2042 }
2043
2044 return true;
2045 }
2046
iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm * mvm)2047 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2048 {
2049 u8 notif_ver;
2050
2051 if (!fw_has_api(&mvm->fw->ucode_capa,
2052 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2053 return 6;
2054
2055 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2056 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2057 WOWLAN_GET_STATUSES, 0);
2058 if (!notif_ver)
2059 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2060 WOWLAN_GET_STATUSES, 7);
2061
2062 return notif_ver;
2063 }
2064
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2065 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2066 struct ieee80211_vif *vif,
2067 struct iwl_wowlan_status_data *status)
2068 {
2069 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2070 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2071 .mvm = mvm,
2072 .status = status,
2073 };
2074 u32 disconnection_reasons =
2075 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2076 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2077
2078 if (!status || !vif->bss_conf.bssid)
2079 return false;
2080
2081 if (status->wakeup_reasons & disconnection_reasons)
2082 return false;
2083
2084 if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2085 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2086 WOWLAN_INFO_NOTIFICATION,
2087 0))
2088 gtkdata.igtk_support = true;
2089
2090 /* find last GTK that we used initially, if any */
2091 ieee80211_iter_keys(mvm->hw, vif,
2092 iwl_mvm_d3_find_last_keys, >kdata);
2093 /* not trying to keep connections with MFP/unhandled ciphers */
2094 if (gtkdata.unhandled_cipher)
2095 return false;
2096 if (!gtkdata.num_keys)
2097 goto out;
2098
2099 /*
2100 * invalidate all other GTKs that might still exist and update
2101 * the one that we used
2102 */
2103 ieee80211_iter_keys(mvm->hw, vif,
2104 iwl_mvm_d3_update_keys, >kdata);
2105
2106 if (status->num_of_gtk_rekeys) {
2107 __be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2108
2109 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2110 status->num_of_gtk_rekeys);
2111
2112 if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2113 return false;
2114
2115 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2116 gtkdata.igtk_cipher,
2117 &status->igtk))
2118 return false;
2119
2120 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2121 (void *)&replay_ctr, GFP_KERNEL);
2122 }
2123
2124 out:
2125 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2126 WOWLAN_GET_STATUSES,
2127 IWL_FW_CMD_VER_UNKNOWN) < 10) {
2128 mvmvif->seqno_valid = true;
2129 /* +0x10 because the set API expects next-to-use, not last-used */
2130 mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2131 }
2132
2133 return true;
2134 }
2135
iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v2 * data)2136 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2137 struct iwl_wowlan_gtk_status_v2 *data)
2138 {
2139 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2140 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2141 sizeof(data->tkip_mic_key) >
2142 sizeof(status->gtk[0].key));
2143
2144 status->gtk[0].len = data->key_len;
2145 status->gtk[0].flags = data->key_flags;
2146
2147 memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2148
2149 /* if it's as long as the TKIP encryption key, copy MIC key */
2150 if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2151 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2152 data->tkip_mic_key, sizeof(data->tkip_mic_key));
2153 }
2154
iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v3 * data)2155 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2156 struct iwl_wowlan_gtk_status_v3 *data)
2157 {
2158 int data_idx, status_idx = 0;
2159
2160 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2161 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2162 sizeof(data[0].tkip_mic_key) >
2163 sizeof(status->gtk[0].key));
2164 BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2165 for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2166 if (!(data[data_idx].key_len))
2167 continue;
2168 status->gtk[status_idx].len = data[data_idx].key_len;
2169 status->gtk[status_idx].flags = data[data_idx].key_flags;
2170 status->gtk[status_idx].id = status->gtk[status_idx].flags &
2171 IWL_WOWLAN_GTK_IDX_MASK;
2172
2173 memcpy(status->gtk[status_idx].key, data[data_idx].key,
2174 sizeof(data[data_idx].key));
2175
2176 /* if it's as long as the TKIP encryption key, copy MIC key */
2177 if (status->gtk[status_idx].len ==
2178 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2179 memcpy(status->gtk[status_idx].key +
2180 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2181 data[data_idx].tkip_mic_key,
2182 sizeof(data[data_idx].tkip_mic_key));
2183 status_idx++;
2184 }
2185 }
2186
iwl_mvm_convert_igtk(struct iwl_wowlan_status_data * status,struct iwl_wowlan_igtk_status * data)2187 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2188 struct iwl_wowlan_igtk_status *data)
2189 {
2190 int i;
2191
2192 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2193 BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2194
2195 if (!data->key_len)
2196 return;
2197
2198 status->igtk.len = data->key_len;
2199 status->igtk.flags = data->key_flags;
2200 status->igtk.id = u32_get_bits(data->key_flags,
2201 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2202 + WOWLAN_IGTK_MIN_INDEX;
2203
2204 memcpy(status->igtk.key, data->key, sizeof(data->key));
2205
2206 /* mac80211 expects big endian for memcmp() to work, convert */
2207 for (i = 0; i < sizeof(data->ipn); i++)
2208 status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2209 }
2210
iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif * data,struct iwl_wowlan_status_data * status,u32 len)2211 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2212 struct iwl_wowlan_info_notif *data,
2213 struct iwl_wowlan_status_data *status,
2214 u32 len)
2215 {
2216 u32 i;
2217
2218 if (!data) {
2219 IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2220 status = NULL;
2221 return;
2222 }
2223
2224 if (len < sizeof(*data)) {
2225 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2226 status = NULL;
2227 return;
2228 }
2229
2230 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2231 iwl_mvm_convert_gtk_v3(status, data->gtk);
2232 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2233
2234 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2235 status->pattern_number = le16_to_cpu(data->pattern_number);
2236 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2237 status->qos_seq_ctr[i] =
2238 le16_to_cpu(data->qos_seq_ctr[i]);
2239 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2240 status->num_of_gtk_rekeys =
2241 le32_to_cpu(data->num_of_gtk_rekeys);
2242 status->received_beacons = le32_to_cpu(data->received_beacons);
2243 status->tid_tear_down = data->tid_tear_down;
2244 }
2245
2246 /* Occasionally, templates would be nice. This is one of those times ... */
2247 #define iwl_mvm_parse_wowlan_status_common(_ver) \
2248 static struct iwl_wowlan_status_data * \
2249 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \
2250 struct iwl_wowlan_status_ ##_ver *data,\
2251 int len) \
2252 { \
2253 struct iwl_wowlan_status_data *status; \
2254 int data_size, i; \
2255 \
2256 if (len < sizeof(*data)) { \
2257 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2258 return NULL; \
2259 } \
2260 \
2261 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \
2262 if (len != sizeof(*data) + data_size) { \
2263 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2264 return NULL; \
2265 } \
2266 \
2267 status = kzalloc(sizeof(*status), GFP_KERNEL); \
2268 if (!status) \
2269 return NULL; \
2270 \
2271 /* copy all the common fields */ \
2272 status->replay_ctr = le64_to_cpu(data->replay_ctr); \
2273 status->pattern_number = le16_to_cpu(data->pattern_number); \
2274 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \
2275 for (i = 0; i < 8; i++) \
2276 status->qos_seq_ctr[i] = \
2277 le16_to_cpu(data->qos_seq_ctr[i]); \
2278 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \
2279 status->num_of_gtk_rekeys = \
2280 le32_to_cpu(data->num_of_gtk_rekeys); \
2281 status->received_beacons = le32_to_cpu(data->received_beacons); \
2282 status->wake_packet_length = \
2283 le32_to_cpu(data->wake_packet_length); \
2284 status->wake_packet_bufsize = \
2285 le32_to_cpu(data->wake_packet_bufsize); \
2286 if (status->wake_packet_bufsize) { \
2287 status->wake_packet = \
2288 kmemdup(data->wake_packet, \
2289 status->wake_packet_bufsize, \
2290 GFP_KERNEL); \
2291 if (!status->wake_packet) { \
2292 kfree(status); \
2293 return NULL; \
2294 } \
2295 } else { \
2296 status->wake_packet = NULL; \
2297 } \
2298 \
2299 return status; \
2300 }
2301
2302 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)2303 iwl_mvm_parse_wowlan_status_common(v7)
2304 iwl_mvm_parse_wowlan_status_common(v9)
2305 iwl_mvm_parse_wowlan_status_common(v12)
2306
2307 static struct iwl_wowlan_status_data *
2308 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2309 {
2310 struct iwl_wowlan_status_data *status;
2311 struct iwl_wowlan_get_status_cmd get_status_cmd = {
2312 .sta_id = cpu_to_le32(sta_id),
2313 };
2314 struct iwl_host_cmd cmd = {
2315 .id = WOWLAN_GET_STATUSES,
2316 .flags = CMD_WANT_SKB,
2317 .data = { &get_status_cmd, },
2318 .len = { sizeof(get_status_cmd), },
2319 };
2320 int ret, len;
2321 u8 notif_ver;
2322 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2323 IWL_FW_CMD_VER_UNKNOWN);
2324
2325 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2326 cmd.len[0] = 0;
2327
2328 lockdep_assert_held(&mvm->mutex);
2329
2330 ret = iwl_mvm_send_cmd(mvm, &cmd);
2331 if (ret) {
2332 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2333 return ERR_PTR(ret);
2334 }
2335
2336 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2337
2338 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2339 notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2340
2341 if (notif_ver < 7) {
2342 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2343
2344 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2345 if (!status)
2346 goto out_free_resp;
2347
2348 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2349 sizeof(status->gtk[0].key));
2350 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2351 sizeof(v6->gtk.tkip_mic_key) >
2352 sizeof(status->gtk[0].key));
2353
2354 /* copy GTK info to the right place */
2355 memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2356 sizeof(v6->gtk.decrypt_key));
2357 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2358 v6->gtk.tkip_mic_key,
2359 sizeof(v6->gtk.tkip_mic_key));
2360
2361 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2362
2363 /* hardcode the key length to 16 since v6 only supports 16 */
2364 status->gtk[0].len = 16;
2365
2366 /*
2367 * The key index only uses 2 bits (values 0 to 3) and
2368 * we always set bit 7 which means this is the
2369 * currently used key.
2370 */
2371 status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2372 } else if (notif_ver == 7) {
2373 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2374
2375 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2376 if (!status)
2377 goto out_free_resp;
2378
2379 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2380 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2381 iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2382 } else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2383 struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2384
2385 /* these three command versions have same layout and size, the
2386 * difference is only in a few not used (reserved) fields.
2387 */
2388 status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2389 if (!status)
2390 goto out_free_resp;
2391
2392 iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2393 iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2394 iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2395
2396 status->tid_tear_down = v9->tid_tear_down;
2397 } else if (notif_ver == 12) {
2398 struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2399
2400 status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2401 if (!status)
2402 goto out_free_resp;
2403
2404 iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2405 iwl_mvm_convert_gtk_v3(status, v12->gtk);
2406 iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2407
2408 status->tid_tear_down = v12->tid_tear_down;
2409 } else {
2410 IWL_ERR(mvm,
2411 "Firmware advertises unknown WoWLAN status response %d!\n",
2412 notif_ver);
2413 status = NULL;
2414 }
2415
2416 out_free_resp:
2417 iwl_free_resp(&cmd);
2418 return status;
2419 }
2420
2421 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2422 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2423 struct ieee80211_vif *vif,
2424 struct iwl_wowlan_status_data *status)
2425 {
2426 int i;
2427 bool keep;
2428 struct iwl_mvm_sta *mvm_ap_sta;
2429
2430 if (!status)
2431 goto out_unlock;
2432
2433 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2434 status->wakeup_reasons);
2435
2436 /* still at hard-coded place 0 for D3 image */
2437 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
2438 if (!mvm_ap_sta)
2439 goto out_unlock;
2440
2441 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2442 u16 seq = status->qos_seq_ctr[i];
2443 /* firmware stores last-used value, we store next value */
2444 seq += 0x10;
2445 mvm_ap_sta->tid_data[i].seq_number = seq;
2446 }
2447
2448 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2449 i = mvm->offload_tid;
2450 iwl_trans_set_q_ptrs(mvm->trans,
2451 mvm_ap_sta->tid_data[i].txq_id,
2452 mvm_ap_sta->tid_data[i].seq_number >> 4);
2453 }
2454
2455 /* now we have all the data we need, unlock to avoid mac80211 issues */
2456 mutex_unlock(&mvm->mutex);
2457
2458 iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2459
2460 keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2461
2462 return keep;
2463
2464 out_unlock:
2465 mutex_unlock(&mvm->mutex);
2466 return false;
2467 }
2468
2469 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2470 IWL_SCAN_MAX_PROFILES)
2471
2472 struct iwl_mvm_nd_results {
2473 u32 matched_profiles;
2474 u8 matches[ND_QUERY_BUF_LEN];
2475 };
2476
2477 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results)2478 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2479 struct iwl_mvm_nd_results *results)
2480 {
2481 struct iwl_scan_offload_match_info *query;
2482 struct iwl_host_cmd cmd = {
2483 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2484 .flags = CMD_WANT_SKB,
2485 };
2486 int ret, len;
2487 size_t query_len, matches_len;
2488 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2489
2490 ret = iwl_mvm_send_cmd(mvm, &cmd);
2491 if (ret) {
2492 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2493 return ret;
2494 }
2495
2496 if (fw_has_api(&mvm->fw->ucode_capa,
2497 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2498 query_len = sizeof(struct iwl_scan_offload_match_info);
2499 matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2500 max_profiles;
2501 } else {
2502 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2503 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2504 max_profiles;
2505 }
2506
2507 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2508 if (len < query_len) {
2509 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2510 ret = -EIO;
2511 goto out_free_resp;
2512 }
2513
2514 query = (void *)cmd.resp_pkt->data;
2515
2516 results->matched_profiles = le32_to_cpu(query->matched_profiles);
2517 memcpy(results->matches, query->matches, matches_len);
2518
2519 #ifdef CONFIG_IWLWIFI_DEBUGFS
2520 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2521 #endif
2522
2523 out_free_resp:
2524 iwl_free_resp(&cmd);
2525 return ret;
2526 }
2527
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,int idx)2528 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2529 struct iwl_mvm_nd_results *results,
2530 int idx)
2531 {
2532 int n_chans = 0, i;
2533
2534 if (fw_has_api(&mvm->fw->ucode_capa,
2535 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2536 struct iwl_scan_offload_profile_match *matches =
2537 (void *)results->matches;
2538
2539 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2540 n_chans += hweight8(matches[idx].matching_channels[i]);
2541 } else {
2542 struct iwl_scan_offload_profile_match_v1 *matches =
2543 (void *)results->matches;
2544
2545 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2546 n_chans += hweight8(matches[idx].matching_channels[i]);
2547 }
2548
2549 return n_chans;
2550 }
2551
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,struct cfg80211_wowlan_nd_match * match,int idx)2552 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2553 struct iwl_mvm_nd_results *results,
2554 struct cfg80211_wowlan_nd_match *match,
2555 int idx)
2556 {
2557 int i;
2558
2559 if (fw_has_api(&mvm->fw->ucode_capa,
2560 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2561 struct iwl_scan_offload_profile_match *matches =
2562 (void *)results->matches;
2563
2564 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2565 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2566 match->channels[match->n_channels++] =
2567 mvm->nd_channels[i]->center_freq;
2568 } else {
2569 struct iwl_scan_offload_profile_match_v1 *matches =
2570 (void *)results->matches;
2571
2572 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2573 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2574 match->channels[match->n_channels++] =
2575 mvm->nd_channels[i]->center_freq;
2576 }
2577 }
2578
2579 /**
2580 * enum iwl_d3_notif - d3 notifications
2581 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2582 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2583 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2584 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2585 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2586 */
2587 enum iwl_d3_notif {
2588 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0),
2589 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1),
2590 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2),
2591 IWL_D3_ND_MATCH_INFO = BIT(3),
2592 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4)
2593 };
2594
2595 /* manage d3 resume data */
2596 struct iwl_d3_data {
2597 struct iwl_wowlan_status_data *status;
2598 bool test;
2599 u32 d3_end_flags;
2600 u32 notif_expected; /* bitmap - see &enum iwl_d3_notif */
2601 u32 notif_received; /* bitmap - see &enum iwl_d3_notif */
2602 struct iwl_mvm_nd_results *nd_results;
2603 bool nd_results_valid;
2604 };
2605
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2606 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2607 struct ieee80211_vif *vif,
2608 struct iwl_d3_data *d3_data)
2609 {
2610 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2611 struct cfg80211_wowlan_wakeup wakeup = {
2612 .pattern_idx = -1,
2613 };
2614 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2615 unsigned long matched_profiles;
2616 u32 reasons = 0;
2617 int i, n_matches, ret;
2618
2619 if (WARN_ON(!d3_data || !d3_data->status))
2620 goto out;
2621
2622 reasons = d3_data->status->wakeup_reasons;
2623
2624 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2625 wakeup.rfkill_release = true;
2626
2627 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2628 goto out;
2629
2630 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2631 WOWLAN_INFO_NOTIFICATION, 0)) {
2632 IWL_INFO(mvm, "Query FW for ND results\n");
2633 ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2634
2635 } else {
2636 IWL_INFO(mvm, "Notification based ND results\n");
2637 ret = d3_data->nd_results_valid ? 0 : -1;
2638 }
2639
2640 if (ret || !d3_data->nd_results->matched_profiles) {
2641 wakeup_report = NULL;
2642 goto out;
2643 }
2644
2645 matched_profiles = d3_data->nd_results->matched_profiles;
2646 if (mvm->n_nd_match_sets) {
2647 n_matches = hweight_long(matched_profiles);
2648 } else {
2649 IWL_ERR(mvm, "no net detect match information available\n");
2650 n_matches = 0;
2651 }
2652
2653 net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2654 GFP_KERNEL);
2655 if (!net_detect || !n_matches)
2656 goto out_report_nd;
2657
2658 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2659 struct cfg80211_wowlan_nd_match *match;
2660 int idx, n_channels = 0;
2661
2662 n_channels = iwl_mvm_query_num_match_chans(mvm,
2663 d3_data->nd_results,
2664 i);
2665
2666 match = kzalloc(struct_size(match, channels, n_channels),
2667 GFP_KERNEL);
2668 if (!match)
2669 goto out_report_nd;
2670
2671 net_detect->matches[net_detect->n_matches++] = match;
2672
2673 /* We inverted the order of the SSIDs in the scan
2674 * request, so invert the index here.
2675 */
2676 idx = mvm->n_nd_match_sets - i - 1;
2677 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2678 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2679 match->ssid.ssid_len);
2680
2681 if (mvm->n_nd_channels < n_channels)
2682 continue;
2683
2684 iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2685 }
2686
2687 out_report_nd:
2688 wakeup.net_detect = net_detect;
2689 out:
2690 iwl_mvm_free_nd(mvm);
2691
2692 mutex_unlock(&mvm->mutex);
2693 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2694
2695 if (net_detect) {
2696 for (i = 0; i < net_detect->n_matches; i++)
2697 kfree(net_detect->matches[i]);
2698 kfree(net_detect);
2699 }
2700 }
2701
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2702 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2703 struct ieee80211_vif *vif)
2704 {
2705 /* skip the one we keep connection on */
2706 if (data == vif)
2707 return;
2708
2709 if (vif->type == NL80211_IFTYPE_STATION)
2710 ieee80211_resume_disconnect(vif);
2711 }
2712
iwl_mvm_rt_status(struct iwl_trans * trans,u32 base,u32 * err_id)2713 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
2714 {
2715 struct error_table_start {
2716 /* cf. struct iwl_error_event_table */
2717 u32 valid;
2718 __le32 err_id;
2719 } err_info;
2720
2721 if (!base)
2722 return false;
2723
2724 iwl_trans_read_mem_bytes(trans, base,
2725 &err_info, sizeof(err_info));
2726 if (err_info.valid && err_id)
2727 *err_id = le32_to_cpu(err_info.err_id);
2728
2729 return !!err_info.valid;
2730 }
2731
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2732 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2733 struct ieee80211_vif *vif)
2734 {
2735 u32 err_id;
2736
2737 /* check for lmac1 error */
2738 if (iwl_mvm_rt_status(mvm->trans,
2739 mvm->trans->dbg.lmac_error_event_table[0],
2740 &err_id)) {
2741 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2742 struct cfg80211_wowlan_wakeup wakeup = {
2743 .rfkill_release = true,
2744 };
2745 ieee80211_report_wowlan_wakeup(vif, &wakeup,
2746 GFP_KERNEL);
2747 }
2748 return true;
2749 }
2750
2751 /* check if we have lmac2 set and check for error */
2752 if (iwl_mvm_rt_status(mvm->trans,
2753 mvm->trans->dbg.lmac_error_event_table[1], NULL))
2754 return true;
2755
2756 /* check for umac error */
2757 if (iwl_mvm_rt_status(mvm->trans,
2758 mvm->trans->dbg.umac_error_event_table, NULL))
2759 return true;
2760
2761 return false;
2762 }
2763
2764 /*
2765 * This function assumes:
2766 * 1. The mutex is already held.
2767 * 2. The callee functions unlock the mutex.
2768 */
2769 static bool
iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2770 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2771 struct ieee80211_vif *vif,
2772 struct iwl_d3_data *d3_data)
2773 {
2774 lockdep_assert_held(&mvm->mutex);
2775
2776 /* if FW uses status notification, status shouldn't be NULL here */
2777 if (!d3_data->status) {
2778 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2779 u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA :
2780 mvmvif->deflink.ap_sta_id;
2781
2782 d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2783 }
2784
2785 if (mvm->net_detect) {
2786 iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2787 } else {
2788 bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2789 d3_data->status);
2790
2791 #ifdef CONFIG_IWLWIFI_DEBUGFS
2792 if (keep)
2793 mvm->keep_vif = vif;
2794 #endif
2795
2796 return keep;
2797 }
2798 return false;
2799 }
2800
2801 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2802 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2803 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2804 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2805 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2806 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2807
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)2808 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2809 struct iwl_wowlan_wake_pkt_notif *notif,
2810 struct iwl_wowlan_status_data *status,
2811 u32 len)
2812 {
2813 u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2814
2815 if (len < sizeof(*notif)) {
2816 IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2817 return -EIO;
2818 }
2819
2820 if (WARN_ON(!status)) {
2821 IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2822 return -EIO;
2823 }
2824
2825 if (WARN_ON(!(status->wakeup_reasons &
2826 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2827 IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2828 status->wakeup_reasons);
2829 return -EIO;
2830 }
2831
2832 data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2833
2834 /* data_size got the padding from the notification, remove it. */
2835 if (packet_len < data_size)
2836 data_size = packet_len;
2837
2838 status->wake_packet = kmemdup(notif->wake_packet, data_size,
2839 GFP_ATOMIC);
2840
2841 if (!status->wake_packet)
2842 return -ENOMEM;
2843
2844 status->wake_packet_length = packet_len;
2845 status->wake_packet_bufsize = data_size;
2846
2847 return 0;
2848 }
2849
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)2850 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2851 struct iwl_d3_data *d3_data,
2852 struct iwl_scan_offload_match_info *notif,
2853 u32 len)
2854 {
2855 struct iwl_wowlan_status_data *status = d3_data->status;
2856 struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2857 struct iwl_mvm_nd_results *results = d3_data->nd_results;
2858 size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2859 iwl_umac_scan_get_max_profiles(mvm->fw);
2860
2861 if (IS_ERR_OR_NULL(vif))
2862 return;
2863
2864 if (len < sizeof(struct iwl_scan_offload_match_info)) {
2865 IWL_ERR(mvm, "Invalid scan match info notification\n");
2866 return;
2867 }
2868
2869 if (!mvm->net_detect) {
2870 IWL_ERR(mvm, "Unexpected scan match info notification\n");
2871 return;
2872 }
2873
2874 if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
2875 IWL_ERR(mvm,
2876 "Ignore scan match info notification: no reason\n");
2877 return;
2878 }
2879
2880 #ifdef CONFIG_IWLWIFI_DEBUGFS
2881 mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
2882 #endif
2883
2884 results->matched_profiles = le32_to_cpu(notif->matched_profiles);
2885 IWL_INFO(mvm, "number of matched profiles=%u\n",
2886 results->matched_profiles);
2887
2888 if (results->matched_profiles) {
2889 memcpy(results->matches, notif->matches, matches_len);
2890 d3_data->nd_results_valid = TRUE;
2891 }
2892
2893 /* no scan should be active at this point */
2894 mvm->scan_status = 0;
2895 for (i = 0; i < mvm->max_scans; i++)
2896 mvm->scan_uid_status[i] = 0;
2897 }
2898
iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)2899 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
2900 struct iwl_rx_packet *pkt, void *data)
2901 {
2902 struct iwl_mvm *mvm =
2903 container_of(notif_wait, struct iwl_mvm, notif_wait);
2904 struct iwl_d3_data *d3_data = data;
2905 u32 len;
2906 int ret;
2907 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2908 PROT_OFFLOAD_GROUP,
2909 WOWLAN_INFO_NOTIFICATION,
2910 IWL_FW_CMD_VER_UNKNOWN);
2911
2912
2913 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
2914 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
2915 struct iwl_wowlan_info_notif *notif;
2916
2917 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
2918 /* We might get two notifications due to dual bss */
2919 IWL_DEBUG_WOWLAN(mvm,
2920 "Got additional wowlan info notification\n");
2921 break;
2922 }
2923
2924 if (wowlan_info_ver < 2) {
2925 struct iwl_wowlan_info_notif_v1 *notif_v1 = (void *)pkt->data;
2926
2927 notif = kmemdup(notif_v1, sizeof(*notif), GFP_ATOMIC);
2928 if (!notif)
2929 return false;
2930
2931 notif->tid_tear_down = notif_v1->tid_tear_down;
2932 notif->station_id = notif_v1->station_id;
2933 memset_after(notif, 0, station_id);
2934 } else {
2935 notif = (void *)pkt->data;
2936 }
2937
2938 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
2939 len = iwl_rx_packet_payload_len(pkt);
2940 iwl_mvm_parse_wowlan_info_notif(mvm, notif, d3_data->status,
2941 len);
2942
2943 if (wowlan_info_ver < 2)
2944 kfree(notif);
2945
2946 if (d3_data->status &&
2947 d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
2948 /* We are supposed to get also wake packet notif */
2949 d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2950
2951 break;
2952 }
2953 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
2954 struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
2955
2956 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
2957 /* We shouldn't get two wake packet notifications */
2958 IWL_ERR(mvm,
2959 "Got additional wowlan wake packet notification\n");
2960 } else {
2961 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2962 len = iwl_rx_packet_payload_len(pkt);
2963 ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
2964 d3_data->status,
2965 len);
2966 if (ret)
2967 IWL_ERR(mvm,
2968 "Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
2969 }
2970
2971 break;
2972 }
2973 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
2974 struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
2975
2976 if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
2977 IWL_ERR(mvm,
2978 "Got additional netdetect match info\n");
2979 break;
2980 }
2981
2982 d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
2983
2984 /* explicitly set this in the 'expected' as well */
2985 d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
2986
2987 len = iwl_rx_packet_payload_len(pkt);
2988 iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
2989 break;
2990 }
2991 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
2992 struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
2993
2994 d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
2995 d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
2996
2997 break;
2998 }
2999 default:
3000 WARN_ON(1);
3001 }
3002
3003 return d3_data->notif_received == d3_data->notif_expected;
3004 }
3005
iwl_mvm_resume_firmware(struct iwl_mvm * mvm,bool test)3006 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3007 {
3008 int ret;
3009 enum iwl_d3_status d3_status;
3010 struct iwl_host_cmd cmd = {
3011 .id = D0I3_END_CMD,
3012 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3013 };
3014 bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3015 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3016
3017 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3018 if (ret)
3019 return ret;
3020
3021 if (d3_status != IWL_D3_STATUS_ALIVE) {
3022 IWL_INFO(mvm, "Device was reset during suspend\n");
3023 return -ENOENT;
3024 }
3025
3026 /*
3027 * We should trigger resume flow using command only for 22000 family
3028 * AX210 and above don't need the command since they have
3029 * the doorbell interrupt.
3030 */
3031 if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3032 fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3033 ret = iwl_mvm_send_cmd(mvm, &cmd);
3034 if (ret < 0)
3035 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3036 ret);
3037 }
3038
3039 return ret;
3040 }
3041
3042 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 5)
3043
iwl_mvm_d3_notif_wait(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data)3044 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3045 struct iwl_d3_data *d3_data)
3046 {
3047 static const u16 d3_resume_notif[] = {
3048 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3049 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3050 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3051 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3052 };
3053 struct iwl_notification_wait wait_d3_notif;
3054 int ret;
3055
3056 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3057 d3_resume_notif, ARRAY_SIZE(d3_resume_notif),
3058 iwl_mvm_wait_d3_notif, d3_data);
3059
3060 ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3061 if (ret) {
3062 iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3063 return ret;
3064 }
3065
3066 return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3067 IWL_MVM_D3_NOTIF_TIMEOUT);
3068 }
3069
iwl_mvm_d3_resume_notif_based(struct iwl_mvm * mvm)3070 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3071 {
3072 return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3073 WOWLAN_INFO_NOTIFICATION, 0) &&
3074 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3075 WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3076 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3077 D3_END_NOTIFICATION, 0);
3078 }
3079
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)3080 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3081 {
3082 struct ieee80211_vif *vif = NULL;
3083 int ret = 1;
3084 struct iwl_mvm_nd_results results = {};
3085 struct iwl_d3_data d3_data = {
3086 .test = test,
3087 .notif_expected =
3088 IWL_D3_NOTIF_WOWLAN_INFO |
3089 IWL_D3_NOTIF_D3_END_NOTIF,
3090 .nd_results_valid = false,
3091 .nd_results = &results,
3092 };
3093 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3094 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3095 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3096 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3097 bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3098 bool keep = false;
3099
3100 mutex_lock(&mvm->mutex);
3101
3102 mvm->last_reset_or_resume_time_jiffies = jiffies;
3103
3104 /* get the BSS vif pointer again */
3105 vif = iwl_mvm_get_bss_vif(mvm);
3106 if (IS_ERR_OR_NULL(vif))
3107 goto err;
3108
3109 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3110
3111 if (iwl_mvm_check_rt_status(mvm, vif)) {
3112 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3113 iwl_mvm_dump_nic_error_log(mvm);
3114 iwl_dbg_tlv_time_point(&mvm->fwrt,
3115 IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3116 iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3117 false, 0);
3118 ret = 1;
3119 goto err;
3120 }
3121
3122 if (resume_notif_based) {
3123 d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3124 if (!d3_data.status) {
3125 IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3126 ret = -ENOMEM;
3127 goto err;
3128 }
3129
3130 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3131 if (ret)
3132 goto err;
3133 } else {
3134 ret = iwl_mvm_resume_firmware(mvm, test);
3135 if (ret < 0)
3136 goto err;
3137 }
3138
3139 /* after the successful handshake, we're out of D3 */
3140 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3141
3142 /* when reset is required we can't send these following commands */
3143 if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3144 goto query_wakeup_reasons;
3145
3146 /*
3147 * Query the current location and source from the D3 firmware so we
3148 * can play it back when we re-intiailize the D0 firmware
3149 */
3150 iwl_mvm_update_changed_regdom(mvm);
3151
3152 /* Re-configure PPAG settings */
3153 iwl_mvm_ppag_send_cmd(mvm);
3154
3155 if (!unified_image)
3156 /* Re-configure default SAR profile */
3157 iwl_mvm_sar_select_profile(mvm, 1, 1);
3158
3159 if (mvm->net_detect && unified_image) {
3160 /* If this is a non-unified image, we restart the FW,
3161 * so no need to stop the netdetect scan. If that
3162 * fails, continue and try to get the wake-up reasons,
3163 * but trigger a HW restart by keeping a failure code
3164 * in ret.
3165 */
3166 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3167 false);
3168 }
3169
3170 query_wakeup_reasons:
3171 keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3172 /* has unlocked the mutex, so skip that */
3173 goto out;
3174
3175 err:
3176 mutex_unlock(&mvm->mutex);
3177 out:
3178 if (d3_data.status)
3179 kfree(d3_data.status->wake_packet);
3180 kfree(d3_data.status);
3181 iwl_mvm_free_nd(mvm);
3182
3183 if (!d3_data.test && !mvm->net_detect)
3184 ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3185 IEEE80211_IFACE_ITER_NORMAL,
3186 iwl_mvm_d3_disconnect_iter,
3187 keep ? vif : NULL);
3188
3189 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3190
3191 /* no need to reset the device in unified images, if successful */
3192 if (unified_image && !ret) {
3193 /* nothing else to do if we already sent D0I3_END_CMD */
3194 if (d0i3_first)
3195 return 0;
3196
3197 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3198 D3_END_NOTIFICATION, 0)) {
3199 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3200 if (!ret)
3201 return 0;
3202 } else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3203 return 0;
3204 }
3205 }
3206
3207 /*
3208 * Reconfigure the device in one of the following cases:
3209 * 1. We are not using a unified image
3210 * 2. We are using a unified image but had an error while exiting D3
3211 */
3212 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3213
3214 /* regardless of what happened, we're now out of D3 */
3215 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3216
3217 return 1;
3218 }
3219
iwl_mvm_resume(struct ieee80211_hw * hw)3220 int iwl_mvm_resume(struct ieee80211_hw *hw)
3221 {
3222 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3223 int ret;
3224
3225 ret = __iwl_mvm_resume(mvm, false);
3226
3227 iwl_mvm_resume_tcm(mvm);
3228
3229 iwl_fw_runtime_resume(&mvm->fwrt);
3230
3231 return ret;
3232 }
3233
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)3234 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3235 {
3236 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3237
3238 device_set_wakeup_enable(mvm->trans->dev, enabled);
3239 }
3240
3241 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)3242 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3243 {
3244 struct iwl_mvm *mvm = inode->i_private;
3245 int err;
3246
3247 if (mvm->d3_test_active)
3248 return -EBUSY;
3249
3250 file->private_data = inode->i_private;
3251
3252 iwl_mvm_pause_tcm(mvm, true);
3253
3254 iwl_fw_runtime_suspend(&mvm->fwrt);
3255
3256 /* start pseudo D3 */
3257 rtnl_lock();
3258 wiphy_lock(mvm->hw->wiphy);
3259 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3260 wiphy_unlock(mvm->hw->wiphy);
3261 rtnl_unlock();
3262 if (err > 0)
3263 err = -EINVAL;
3264 if (err)
3265 return err;
3266
3267 mvm->d3_test_active = true;
3268 mvm->keep_vif = NULL;
3269 return 0;
3270 }
3271
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3272 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3273 size_t count, loff_t *ppos)
3274 {
3275 struct iwl_mvm *mvm = file->private_data;
3276 u32 pme_asserted;
3277
3278 while (true) {
3279 /* read pme_ptr if available */
3280 if (mvm->d3_test_pme_ptr) {
3281 pme_asserted = iwl_trans_read_mem32(mvm->trans,
3282 mvm->d3_test_pme_ptr);
3283 if (pme_asserted)
3284 break;
3285 }
3286
3287 if (msleep_interruptible(100))
3288 break;
3289 }
3290
3291 return 0;
3292 }
3293
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)3294 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3295 struct ieee80211_vif *vif)
3296 {
3297 /* skip the one we keep connection on */
3298 if (_data == vif)
3299 return;
3300
3301 if (vif->type == NL80211_IFTYPE_STATION)
3302 ieee80211_connection_loss(vif);
3303 }
3304
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)3305 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3306 {
3307 struct iwl_mvm *mvm = inode->i_private;
3308 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3309 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3310
3311 mvm->d3_test_active = false;
3312
3313 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3314
3315 rtnl_lock();
3316 wiphy_lock(mvm->hw->wiphy);
3317 __iwl_mvm_resume(mvm, true);
3318 wiphy_unlock(mvm->hw->wiphy);
3319 rtnl_unlock();
3320
3321 iwl_mvm_resume_tcm(mvm);
3322
3323 iwl_fw_runtime_resume(&mvm->fwrt);
3324
3325 iwl_abort_notification_waits(&mvm->notif_wait);
3326 if (!unified_image) {
3327 int remaining_time = 10;
3328
3329 ieee80211_restart_hw(mvm->hw);
3330
3331 /* wait for restart and disconnect all interfaces */
3332 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3333 remaining_time > 0) {
3334 remaining_time--;
3335 msleep(1000);
3336 }
3337
3338 if (remaining_time == 0)
3339 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3340 }
3341
3342 ieee80211_iterate_active_interfaces_atomic(
3343 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3344 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3345
3346 return 0;
3347 }
3348
3349 const struct file_operations iwl_dbgfs_d3_test_ops = {
3350 .llseek = no_llseek,
3351 .open = iwl_mvm_d3_test_open,
3352 .read = iwl_mvm_d3_test_read,
3353 .release = iwl_mvm_d3_test_release,
3354 };
3355 #endif
3356