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