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