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