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