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