1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2014, 2018-2021 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((__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, LONG_GROUP, 457 WOWLAN_TSC_RSC_PARAM, 458 IWL_FW_CMD_VER_UNKNOWN); 459 int ret; 460 461 if (ver == 5) { 462 struct wowlan_key_rsc_v5_data data = {}; 463 int i; 464 465 data.rsc = kmalloc(sizeof(*data.rsc), GFP_KERNEL); 466 if (!data.rsc) 467 return -ENOMEM; 468 469 memset(data.rsc, 0xff, sizeof(*data.rsc)); 470 471 for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++) 472 data.rsc->mcast_key_id_map[i] = 473 IWL_MCAST_KEY_MAP_INVALID; 474 data.rsc->sta_id = cpu_to_le32(mvmvif->ap_sta_id); 475 476 ieee80211_iter_keys(mvm->hw, vif, 477 iwl_mvm_wowlan_get_rsc_v5_data, 478 &data); 479 480 if (data.have_rsc) 481 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM, 482 CMD_ASYNC, sizeof(*data.rsc), 483 data.rsc); 484 else 485 ret = 0; 486 kfree(data.rsc); 487 } else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) { 488 struct wowlan_key_rsc_tsc_data data = {}; 489 int size; 490 491 data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL); 492 if (!data.rsc_tsc) 493 return -ENOMEM; 494 495 if (ver == 4) { 496 size = sizeof(*data.rsc_tsc); 497 data.rsc_tsc->sta_id = cpu_to_le32(mvmvif->ap_sta_id); 498 } else { 499 /* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */ 500 size = sizeof(data.rsc_tsc->params); 501 } 502 503 ieee80211_iter_keys(mvm->hw, vif, 504 iwl_mvm_wowlan_get_rsc_tsc_data, 505 &data); 506 507 if (data.have_rsc_tsc) 508 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM, 509 CMD_ASYNC, size, 510 data.rsc_tsc); 511 else 512 ret = 0; 513 kfree(data.rsc_tsc); 514 } else { 515 ret = 0; 516 WARN_ON_ONCE(1); 517 } 518 519 return ret; 520 } 521 522 struct wowlan_key_tkip_data { 523 struct iwl_wowlan_tkip_params_cmd tkip; 524 bool have_tkip_keys; 525 }; 526 527 static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw, 528 struct ieee80211_vif *vif, 529 struct ieee80211_sta *sta, 530 struct ieee80211_key_conf *key, 531 void *_data) 532 { 533 struct wowlan_key_tkip_data *data = _data; 534 struct iwl_p1k_cache *rx_p1ks; 535 u8 *rx_mic_key; 536 struct ieee80211_key_seq seq; 537 u32 cur_rx_iv32 = 0; 538 u16 p1k[IWL_P1K_SIZE]; 539 int i; 540 541 switch (key->cipher) { 542 default: 543 break; 544 case WLAN_CIPHER_SUITE_TKIP: 545 if (sta) { 546 u64 pn64; 547 548 rx_p1ks = data->tkip.rx_uni; 549 550 pn64 = atomic64_read(&key->tx_pn); 551 552 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64), 553 p1k); 554 iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k); 555 556 memcpy(data->tkip.mic_keys.tx, 557 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY], 558 IWL_MIC_KEY_SIZE); 559 560 rx_mic_key = data->tkip.mic_keys.rx_unicast; 561 } else { 562 rx_p1ks = data->tkip.rx_multi; 563 rx_mic_key = data->tkip.mic_keys.rx_mcast; 564 } 565 566 for (i = 0; i < IWL_NUM_RSC; i++) { 567 /* wrapping isn't allowed, AP must rekey */ 568 if (seq.tkip.iv32 > cur_rx_iv32) 569 cur_rx_iv32 = seq.tkip.iv32; 570 } 571 572 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid, 573 cur_rx_iv32, p1k); 574 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k); 575 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid, 576 cur_rx_iv32 + 1, p1k); 577 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k); 578 579 memcpy(rx_mic_key, 580 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY], 581 IWL_MIC_KEY_SIZE); 582 583 data->have_tkip_keys = true; 584 break; 585 } 586 } 587 588 struct wowlan_key_gtk_type_iter { 589 struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd; 590 }; 591 592 static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw, 593 struct ieee80211_vif *vif, 594 struct ieee80211_sta *sta, 595 struct ieee80211_key_conf *key, 596 void *_data) 597 { 598 struct wowlan_key_gtk_type_iter *data = _data; 599 600 switch (key->cipher) { 601 default: 602 return; 603 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 604 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 605 data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_GCMP); 606 return; 607 case WLAN_CIPHER_SUITE_AES_CMAC: 608 data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_CCM); 609 return; 610 case WLAN_CIPHER_SUITE_CCMP: 611 if (!sta) 612 data->kek_kck_cmd->gtk_cipher = 613 cpu_to_le32(STA_KEY_FLG_CCM); 614 break; 615 case WLAN_CIPHER_SUITE_GCMP: 616 case WLAN_CIPHER_SUITE_GCMP_256: 617 if (!sta) 618 data->kek_kck_cmd->gtk_cipher = 619 cpu_to_le32(STA_KEY_FLG_GCMP); 620 break; 621 } 622 } 623 624 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm, 625 struct cfg80211_wowlan *wowlan) 626 { 627 struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd; 628 struct iwl_host_cmd cmd = { 629 .id = WOWLAN_PATTERNS, 630 .dataflags[0] = IWL_HCMD_DFL_NOCOPY, 631 }; 632 int i, err; 633 634 if (!wowlan->n_patterns) 635 return 0; 636 637 cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns); 638 639 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL); 640 if (!pattern_cmd) 641 return -ENOMEM; 642 643 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns); 644 645 for (i = 0; i < wowlan->n_patterns; i++) { 646 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8); 647 648 memcpy(&pattern_cmd->patterns[i].mask, 649 wowlan->patterns[i].mask, mask_len); 650 memcpy(&pattern_cmd->patterns[i].pattern, 651 wowlan->patterns[i].pattern, 652 wowlan->patterns[i].pattern_len); 653 pattern_cmd->patterns[i].mask_size = mask_len; 654 pattern_cmd->patterns[i].pattern_size = 655 wowlan->patterns[i].pattern_len; 656 } 657 658 cmd.data[0] = pattern_cmd; 659 err = iwl_mvm_send_cmd(mvm, &cmd); 660 kfree(pattern_cmd); 661 return err; 662 } 663 664 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm, 665 struct ieee80211_vif *vif, 666 struct cfg80211_wowlan *wowlan) 667 { 668 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 669 struct iwl_wowlan_patterns_cmd *pattern_cmd; 670 struct iwl_host_cmd cmd = { 671 .id = WOWLAN_PATTERNS, 672 .dataflags[0] = IWL_HCMD_DFL_NOCOPY, 673 }; 674 int i, err; 675 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 676 WOWLAN_PATTERNS, 677 IWL_FW_CMD_VER_UNKNOWN); 678 679 if (!wowlan->n_patterns) 680 return 0; 681 682 cmd.len[0] = sizeof(*pattern_cmd) + 683 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2); 684 685 pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL); 686 if (!pattern_cmd) 687 return -ENOMEM; 688 689 pattern_cmd->n_patterns = wowlan->n_patterns; 690 if (ver >= 3) 691 pattern_cmd->sta_id = mvmvif->ap_sta_id; 692 693 for (i = 0; i < wowlan->n_patterns; i++) { 694 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8); 695 696 pattern_cmd->patterns[i].pattern_type = 697 WOWLAN_PATTERN_TYPE_BITMASK; 698 699 memcpy(&pattern_cmd->patterns[i].u.bitmask.mask, 700 wowlan->patterns[i].mask, mask_len); 701 memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern, 702 wowlan->patterns[i].pattern, 703 wowlan->patterns[i].pattern_len); 704 pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len; 705 pattern_cmd->patterns[i].u.bitmask.pattern_size = 706 wowlan->patterns[i].pattern_len; 707 } 708 709 cmd.data[0] = pattern_cmd; 710 err = iwl_mvm_send_cmd(mvm, &cmd); 711 kfree(pattern_cmd); 712 return err; 713 } 714 715 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 716 struct ieee80211_sta *ap_sta) 717 { 718 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 719 struct ieee80211_chanctx_conf *ctx; 720 u8 chains_static, chains_dynamic; 721 struct cfg80211_chan_def chandef; 722 int ret, i; 723 struct iwl_binding_cmd_v1 binding_cmd = {}; 724 struct iwl_time_quota_cmd quota_cmd = {}; 725 struct iwl_time_quota_data *quota; 726 u32 status; 727 728 if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm))) 729 return -EINVAL; 730 731 /* add back the PHY */ 732 if (WARN_ON(!mvmvif->phy_ctxt)) 733 return -EINVAL; 734 735 rcu_read_lock(); 736 ctx = rcu_dereference(vif->chanctx_conf); 737 if (WARN_ON(!ctx)) { 738 rcu_read_unlock(); 739 return -EINVAL; 740 } 741 chandef = ctx->def; 742 chains_static = ctx->rx_chains_static; 743 chains_dynamic = ctx->rx_chains_dynamic; 744 rcu_read_unlock(); 745 746 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef, 747 chains_static, chains_dynamic); 748 if (ret) 749 return ret; 750 751 /* add back the MAC */ 752 mvmvif->uploaded = false; 753 754 if (WARN_ON(!vif->bss_conf.assoc)) 755 return -EINVAL; 756 757 ret = iwl_mvm_mac_ctxt_add(mvm, vif); 758 if (ret) 759 return ret; 760 761 /* add back binding - XXX refactor? */ 762 binding_cmd.id_and_color = 763 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id, 764 mvmvif->phy_ctxt->color)); 765 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD); 766 binding_cmd.phy = 767 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id, 768 mvmvif->phy_ctxt->color)); 769 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, 770 mvmvif->color)); 771 for (i = 1; i < MAX_MACS_IN_BINDING; i++) 772 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID); 773 774 status = 0; 775 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD, 776 IWL_BINDING_CMD_SIZE_V1, &binding_cmd, 777 &status); 778 if (ret) { 779 IWL_ERR(mvm, "Failed to add binding: %d\n", ret); 780 return ret; 781 } 782 783 if (status) { 784 IWL_ERR(mvm, "Binding command failed: %u\n", status); 785 return -EIO; 786 } 787 788 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0); 789 if (ret) 790 return ret; 791 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta); 792 793 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL); 794 if (ret) 795 return ret; 796 797 /* and some quota */ 798 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, 0); 799 quota->id_and_color = 800 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id, 801 mvmvif->phy_ctxt->color)); 802 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA); 803 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA); 804 805 for (i = 1; i < MAX_BINDINGS; i++) { 806 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, i); 807 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID); 808 } 809 810 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0, 811 iwl_mvm_quota_cmd_size(mvm), "a_cmd); 812 if (ret) 813 IWL_ERR(mvm, "Failed to send quota: %d\n", ret); 814 815 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm)) 816 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n"); 817 818 return 0; 819 } 820 821 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm, 822 struct ieee80211_vif *vif) 823 { 824 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 825 struct iwl_nonqos_seq_query_cmd query_cmd = { 826 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET), 827 .mac_id_n_color = 828 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, 829 mvmvif->color)), 830 }; 831 struct iwl_host_cmd cmd = { 832 .id = NON_QOS_TX_COUNTER_CMD, 833 .flags = CMD_WANT_SKB, 834 }; 835 int err; 836 u32 size; 837 838 cmd.data[0] = &query_cmd; 839 cmd.len[0] = sizeof(query_cmd); 840 841 err = iwl_mvm_send_cmd(mvm, &cmd); 842 if (err) 843 return err; 844 845 size = iwl_rx_packet_payload_len(cmd.resp_pkt); 846 if (size < sizeof(__le16)) { 847 err = -EINVAL; 848 } else { 849 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data); 850 /* firmware returns next, not last-used seqno */ 851 err = (u16) (err - 0x10); 852 } 853 854 iwl_free_resp(&cmd); 855 return err; 856 } 857 858 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 859 { 860 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 861 struct iwl_nonqos_seq_query_cmd query_cmd = { 862 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET), 863 .mac_id_n_color = 864 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, 865 mvmvif->color)), 866 .value = cpu_to_le16(mvmvif->seqno), 867 }; 868 869 /* return if called during restart, not resume from D3 */ 870 if (!mvmvif->seqno_valid) 871 return; 872 873 mvmvif->seqno_valid = false; 874 875 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0, 876 sizeof(query_cmd), &query_cmd)) 877 IWL_ERR(mvm, "failed to set non-QoS seqno\n"); 878 } 879 880 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm) 881 { 882 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true); 883 884 iwl_mvm_stop_device(mvm); 885 /* 886 * Set the HW restart bit -- this is mostly true as we're 887 * going to load new firmware and reprogram that, though 888 * the reprogramming is going to be manual to avoid adding 889 * all the MACs that aren't support. 890 * We don't have to clear up everything though because the 891 * reprogramming is manual. When we resume, we'll actually 892 * go through a proper restart sequence again to switch 893 * back to the runtime firmware image. 894 */ 895 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status); 896 897 /* the fw is reset, so all the keys are cleared */ 898 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table)); 899 900 mvm->ptk_ivlen = 0; 901 mvm->ptk_icvlen = 0; 902 mvm->ptk_ivlen = 0; 903 mvm->ptk_icvlen = 0; 904 905 return iwl_mvm_load_d3_fw(mvm); 906 } 907 908 static int 909 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm, 910 struct cfg80211_wowlan *wowlan, 911 struct iwl_wowlan_config_cmd *wowlan_config_cmd, 912 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif, 913 struct ieee80211_sta *ap_sta) 914 { 915 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta); 916 917 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */ 918 919 wowlan_config_cmd->is_11n_connection = 920 ap_sta->ht_cap.ht_supported; 921 wowlan_config_cmd->flags = ENABLE_L3_FILTERING | 922 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING; 923 924 if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 925 WOWLAN_CONFIGURATION, 0) < 6) { 926 /* Query the last used seqno and set it */ 927 int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif); 928 929 if (ret < 0) 930 return ret; 931 932 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret); 933 } 934 935 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd); 936 937 if (wowlan->disconnect) 938 wowlan_config_cmd->wakeup_filter |= 939 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS | 940 IWL_WOWLAN_WAKEUP_LINK_CHANGE); 941 if (wowlan->magic_pkt) 942 wowlan_config_cmd->wakeup_filter |= 943 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET); 944 if (wowlan->gtk_rekey_failure) 945 wowlan_config_cmd->wakeup_filter |= 946 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL); 947 if (wowlan->eap_identity_req) 948 wowlan_config_cmd->wakeup_filter |= 949 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ); 950 if (wowlan->four_way_handshake) 951 wowlan_config_cmd->wakeup_filter |= 952 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE); 953 if (wowlan->n_patterns) 954 wowlan_config_cmd->wakeup_filter |= 955 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH); 956 957 if (wowlan->rfkill_release) 958 wowlan_config_cmd->wakeup_filter |= 959 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT); 960 961 if (wowlan->tcp) { 962 /* 963 * Set the "link change" (really "link lost") flag as well 964 * since that implies losing the TCP connection. 965 */ 966 wowlan_config_cmd->wakeup_filter |= 967 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS | 968 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE | 969 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET | 970 IWL_WOWLAN_WAKEUP_LINK_CHANGE); 971 } 972 973 if (wowlan->any) { 974 wowlan_config_cmd->wakeup_filter |= 975 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS | 976 IWL_WOWLAN_WAKEUP_LINK_CHANGE | 977 IWL_WOWLAN_WAKEUP_RX_FRAME | 978 IWL_WOWLAN_WAKEUP_BCN_FILTERING); 979 } 980 981 return 0; 982 } 983 984 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm, 985 struct ieee80211_vif *vif) 986 { 987 bool unified = fw_has_capa(&mvm->fw->ucode_capa, 988 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 989 struct wowlan_key_reprogram_data key_data = {}; 990 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 991 int ret; 992 u8 cmd_ver; 993 size_t cmd_size; 994 995 if (!unified) { 996 /* 997 * if we have to configure keys, call ieee80211_iter_keys(), 998 * as we need non-atomic context in order to take the 999 * required locks. 1000 */ 1001 /* 1002 * Note that currently we don't use CMD_ASYNC in the iterator. 1003 * In case of key_data.configure_keys, all the configured 1004 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will 1005 * take care of locking/unlocking mvm->mutex. 1006 */ 1007 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys, 1008 &key_data); 1009 1010 if (key_data.error) 1011 return -EIO; 1012 } 1013 1014 ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif); 1015 if (ret) 1016 return ret; 1017 1018 if (!fw_has_api(&mvm->fw->ucode_capa, 1019 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) { 1020 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 1021 WOWLAN_TKIP_PARAM, 1022 IWL_FW_CMD_VER_UNKNOWN); 1023 struct wowlan_key_tkip_data tkip_data = {}; 1024 int size; 1025 1026 if (ver == 2) { 1027 size = sizeof(tkip_data.tkip); 1028 tkip_data.tkip.sta_id = 1029 cpu_to_le32(mvmvif->ap_sta_id); 1030 } else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) { 1031 size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1); 1032 } else { 1033 WARN_ON_ONCE(1); 1034 return -EINVAL; 1035 } 1036 1037 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data, 1038 &tkip_data); 1039 1040 if (tkip_data.have_tkip_keys) { 1041 /* send relevant data according to CMD version */ 1042 ret = iwl_mvm_send_cmd_pdu(mvm, 1043 WOWLAN_TKIP_PARAM, 1044 CMD_ASYNC, size, 1045 &tkip_data.tkip); 1046 if (ret) 1047 return ret; 1048 } 1049 } 1050 1051 /* configure rekey data only if offloaded rekey is supported (d3) */ 1052 if (mvmvif->rekey_data.valid) { 1053 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {}; 1054 struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd = 1055 &kek_kck_cmd; 1056 struct wowlan_key_gtk_type_iter gtk_type_data = { 1057 .kek_kck_cmd = _kek_kck_cmd, 1058 }; 1059 1060 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 1061 IWL_ALWAYS_LONG_GROUP, 1062 WOWLAN_KEK_KCK_MATERIAL, 1063 IWL_FW_CMD_VER_UNKNOWN); 1064 if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 && 1065 cmd_ver != IWL_FW_CMD_VER_UNKNOWN)) 1066 return -EINVAL; 1067 1068 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter, 1069 >k_type_data); 1070 1071 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck, 1072 mvmvif->rekey_data.kck_len); 1073 kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len); 1074 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek, 1075 mvmvif->rekey_data.kek_len); 1076 kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len); 1077 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr; 1078 kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm); 1079 kek_kck_cmd.sta_id = cpu_to_le32(mvmvif->ap_sta_id); 1080 1081 if (cmd_ver == 4) { 1082 cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4); 1083 } else { 1084 if (cmd_ver == 3) 1085 cmd_size = 1086 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3); 1087 else 1088 cmd_size = 1089 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2); 1090 /* skip the sta_id at the beginning */ 1091 _kek_kck_cmd = (void *) 1092 ((u8 *)_kek_kck_cmd) + sizeof(kek_kck_cmd.sta_id); 1093 } 1094 1095 IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n", 1096 mvmvif->rekey_data.akm); 1097 1098 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL, 1099 CMD_ASYNC, cmd_size, _kek_kck_cmd); 1100 if (ret) 1101 return ret; 1102 } 1103 1104 return 0; 1105 } 1106 1107 static int 1108 iwl_mvm_wowlan_config(struct iwl_mvm *mvm, 1109 struct cfg80211_wowlan *wowlan, 1110 struct iwl_wowlan_config_cmd *wowlan_config_cmd, 1111 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif, 1112 struct ieee80211_sta *ap_sta) 1113 { 1114 int ret; 1115 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 1116 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 1117 1118 mvm->offload_tid = wowlan_config_cmd->offloading_tid; 1119 1120 if (!unified_image) { 1121 ret = iwl_mvm_switch_to_d3(mvm); 1122 if (ret) 1123 return ret; 1124 1125 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta); 1126 if (ret) 1127 return ret; 1128 } 1129 1130 /* 1131 * This needs to be unlocked due to lock ordering 1132 * constraints. Since we're in the suspend path 1133 * that isn't really a problem though. 1134 */ 1135 mutex_unlock(&mvm->mutex); 1136 ret = iwl_mvm_wowlan_config_key_params(mvm, vif); 1137 mutex_lock(&mvm->mutex); 1138 if (ret) 1139 return ret; 1140 1141 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0, 1142 sizeof(*wowlan_config_cmd), 1143 wowlan_config_cmd); 1144 if (ret) 1145 return ret; 1146 1147 if (fw_has_api(&mvm->fw->ucode_capa, 1148 IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE)) 1149 ret = iwl_mvm_send_patterns(mvm, vif, wowlan); 1150 else 1151 ret = iwl_mvm_send_patterns_v1(mvm, wowlan); 1152 if (ret) 1153 return ret; 1154 1155 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0); 1156 } 1157 1158 static int 1159 iwl_mvm_netdetect_config(struct iwl_mvm *mvm, 1160 struct cfg80211_wowlan *wowlan, 1161 struct cfg80211_sched_scan_request *nd_config, 1162 struct ieee80211_vif *vif) 1163 { 1164 int ret; 1165 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 1166 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 1167 1168 if (!unified_image) { 1169 ret = iwl_mvm_switch_to_d3(mvm); 1170 if (ret) 1171 return ret; 1172 } else { 1173 /* In theory, we wouldn't have to stop a running sched 1174 * scan in order to start another one (for 1175 * net-detect). But in practice this doesn't seem to 1176 * work properly, so stop any running sched_scan now. 1177 */ 1178 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true); 1179 if (ret) 1180 return ret; 1181 } 1182 1183 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies, 1184 IWL_MVM_SCAN_NETDETECT); 1185 if (ret) 1186 return ret; 1187 1188 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels)) 1189 return -EBUSY; 1190 1191 /* save the sched scan matchsets... */ 1192 if (nd_config->n_match_sets) { 1193 mvm->nd_match_sets = kmemdup(nd_config->match_sets, 1194 sizeof(*nd_config->match_sets) * 1195 nd_config->n_match_sets, 1196 GFP_KERNEL); 1197 if (mvm->nd_match_sets) 1198 mvm->n_nd_match_sets = nd_config->n_match_sets; 1199 } 1200 1201 /* ...and the sched scan channels for later reporting */ 1202 mvm->nd_channels = kmemdup(nd_config->channels, 1203 sizeof(*nd_config->channels) * 1204 nd_config->n_channels, 1205 GFP_KERNEL); 1206 if (mvm->nd_channels) 1207 mvm->n_nd_channels = nd_config->n_channels; 1208 1209 return 0; 1210 } 1211 1212 static void iwl_mvm_free_nd(struct iwl_mvm *mvm) 1213 { 1214 kfree(mvm->nd_match_sets); 1215 mvm->nd_match_sets = NULL; 1216 mvm->n_nd_match_sets = 0; 1217 kfree(mvm->nd_channels); 1218 mvm->nd_channels = NULL; 1219 mvm->n_nd_channels = 0; 1220 } 1221 1222 static int __iwl_mvm_suspend(struct ieee80211_hw *hw, 1223 struct cfg80211_wowlan *wowlan, 1224 bool test) 1225 { 1226 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 1227 struct ieee80211_vif *vif = NULL; 1228 struct iwl_mvm_vif *mvmvif = NULL; 1229 struct ieee80211_sta *ap_sta = NULL; 1230 struct iwl_d3_manager_config d3_cfg_cmd_data = { 1231 /* 1232 * Program the minimum sleep time to 10 seconds, as many 1233 * platforms have issues processing a wakeup signal while 1234 * still being in the process of suspending. 1235 */ 1236 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000), 1237 }; 1238 struct iwl_host_cmd d3_cfg_cmd = { 1239 .id = D3_CONFIG_CMD, 1240 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3, 1241 .data[0] = &d3_cfg_cmd_data, 1242 .len[0] = sizeof(d3_cfg_cmd_data), 1243 }; 1244 int ret; 1245 int len __maybe_unused; 1246 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 1247 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 1248 1249 if (!wowlan) { 1250 /* 1251 * mac80211 shouldn't get here, but for D3 test 1252 * it doesn't warrant a warning 1253 */ 1254 WARN_ON(!test); 1255 return -EINVAL; 1256 } 1257 1258 mutex_lock(&mvm->mutex); 1259 1260 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status); 1261 1262 synchronize_net(); 1263 1264 vif = iwl_mvm_get_bss_vif(mvm); 1265 if (IS_ERR_OR_NULL(vif)) { 1266 ret = 1; 1267 goto out_noreset; 1268 } 1269 1270 mvmvif = iwl_mvm_vif_from_mac80211(vif); 1271 1272 if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) { 1273 /* if we're not associated, this must be netdetect */ 1274 if (!wowlan->nd_config) { 1275 ret = 1; 1276 goto out_noreset; 1277 } 1278 1279 ret = iwl_mvm_netdetect_config( 1280 mvm, wowlan, wowlan->nd_config, vif); 1281 if (ret) 1282 goto out; 1283 1284 mvm->net_detect = true; 1285 } else { 1286 struct iwl_wowlan_config_cmd wowlan_config_cmd = {}; 1287 1288 wowlan_config_cmd.sta_id = mvmvif->ap_sta_id; 1289 1290 ap_sta = rcu_dereference_protected( 1291 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], 1292 lockdep_is_held(&mvm->mutex)); 1293 if (IS_ERR_OR_NULL(ap_sta)) { 1294 ret = -EINVAL; 1295 goto out_noreset; 1296 } 1297 1298 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd, 1299 vif, mvmvif, ap_sta); 1300 if (ret) 1301 goto out_noreset; 1302 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd, 1303 vif, mvmvif, ap_sta); 1304 if (ret) 1305 goto out; 1306 1307 mvm->net_detect = false; 1308 } 1309 1310 ret = iwl_mvm_power_update_device(mvm); 1311 if (ret) 1312 goto out; 1313 1314 ret = iwl_mvm_power_update_mac(mvm); 1315 if (ret) 1316 goto out; 1317 1318 #ifdef CONFIG_IWLWIFI_DEBUGFS 1319 if (mvm->d3_wake_sysassert) 1320 d3_cfg_cmd_data.wakeup_flags |= 1321 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR); 1322 #endif 1323 1324 /* 1325 * Prior to 9000 device family the driver needs to stop the dbg 1326 * recording before entering D3. In later devices the FW stops the 1327 * recording automatically. 1328 */ 1329 if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000) 1330 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true); 1331 1332 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3; 1333 1334 /* must be last -- this switches firmware state */ 1335 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd); 1336 if (ret) 1337 goto out; 1338 #ifdef CONFIG_IWLWIFI_DEBUGFS 1339 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt); 1340 if (len >= sizeof(u32)) { 1341 mvm->d3_test_pme_ptr = 1342 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data); 1343 } 1344 #endif 1345 iwl_free_resp(&d3_cfg_cmd); 1346 1347 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status); 1348 1349 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image); 1350 out: 1351 if (ret < 0) { 1352 iwl_mvm_free_nd(mvm); 1353 1354 if (!unified_image) { 1355 if (mvm->fw_restart > 0) { 1356 mvm->fw_restart--; 1357 ieee80211_restart_hw(mvm->hw); 1358 } 1359 } 1360 1361 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status); 1362 } 1363 out_noreset: 1364 mutex_unlock(&mvm->mutex); 1365 1366 return ret; 1367 } 1368 1369 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan) 1370 { 1371 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 1372 1373 iwl_mvm_pause_tcm(mvm, true); 1374 1375 iwl_fw_runtime_suspend(&mvm->fwrt); 1376 1377 return __iwl_mvm_suspend(hw, wowlan, false); 1378 } 1379 1380 /* converted data from the different status responses */ 1381 struct iwl_wowlan_status_data { 1382 u64 replay_ctr; 1383 u32 num_of_gtk_rekeys; 1384 u32 received_beacons; 1385 u32 wakeup_reasons; 1386 u32 wake_packet_length; 1387 u32 wake_packet_bufsize; 1388 u16 pattern_number; 1389 u16 non_qos_seq_ctr; 1390 u16 qos_seq_ctr[8]; 1391 u8 tid_tear_down; 1392 1393 struct { 1394 /* 1395 * We store both the TKIP and AES representations 1396 * coming from the firmware because we decode the 1397 * data from there before we iterate the keys and 1398 * know which one we need. 1399 */ 1400 struct { 1401 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT]; 1402 } tkip, aes; 1403 /* including RX MIC key for TKIP */ 1404 u8 key[WOWLAN_KEY_MAX_SIZE]; 1405 u8 len; 1406 u8 flags; 1407 } gtk; 1408 1409 struct { 1410 /* Same as above */ 1411 struct { 1412 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT]; 1413 u64 tx_pn; 1414 } tkip, aes; 1415 } ptk; 1416 1417 struct { 1418 u64 ipn; 1419 u8 key[WOWLAN_KEY_MAX_SIZE]; 1420 u8 len; 1421 u8 flags; 1422 } igtk; 1423 1424 u8 wake_packet[]; 1425 }; 1426 1427 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm, 1428 struct ieee80211_vif *vif, 1429 struct iwl_wowlan_status_data *status) 1430 { 1431 struct sk_buff *pkt = NULL; 1432 struct cfg80211_wowlan_wakeup wakeup = { 1433 .pattern_idx = -1, 1434 }; 1435 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup; 1436 u32 reasons = status->wakeup_reasons; 1437 1438 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) { 1439 wakeup_report = NULL; 1440 goto report; 1441 } 1442 1443 pm_wakeup_event(mvm->dev, 0); 1444 1445 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET) 1446 wakeup.magic_pkt = true; 1447 1448 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN) 1449 wakeup.pattern_idx = 1450 status->pattern_number; 1451 1452 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 1453 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH)) 1454 wakeup.disconnect = true; 1455 1456 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE) 1457 wakeup.gtk_rekey_failure = true; 1458 1459 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 1460 wakeup.rfkill_release = true; 1461 1462 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST) 1463 wakeup.eap_identity_req = true; 1464 1465 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE) 1466 wakeup.four_way_handshake = true; 1467 1468 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS) 1469 wakeup.tcp_connlost = true; 1470 1471 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE) 1472 wakeup.tcp_nomoretokens = true; 1473 1474 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET) 1475 wakeup.tcp_match = true; 1476 1477 if (status->wake_packet_bufsize) { 1478 int pktsize = status->wake_packet_bufsize; 1479 int pktlen = status->wake_packet_length; 1480 const u8 *pktdata = status->wake_packet; 1481 struct ieee80211_hdr *hdr = (void *)pktdata; 1482 int truncated = pktlen - pktsize; 1483 1484 /* this would be a firmware bug */ 1485 if (WARN_ON_ONCE(truncated < 0)) 1486 truncated = 0; 1487 1488 if (ieee80211_is_data(hdr->frame_control)) { 1489 int hdrlen = ieee80211_hdrlen(hdr->frame_control); 1490 int ivlen = 0, icvlen = 4; /* also FCS */ 1491 1492 pkt = alloc_skb(pktsize, GFP_KERNEL); 1493 if (!pkt) 1494 goto report; 1495 1496 skb_put_data(pkt, pktdata, hdrlen); 1497 pktdata += hdrlen; 1498 pktsize -= hdrlen; 1499 1500 if (ieee80211_has_protected(hdr->frame_control)) { 1501 /* 1502 * This is unlocked and using gtk_i(c)vlen, 1503 * but since everything is under RTNL still 1504 * that's not really a problem - changing 1505 * it would be difficult. 1506 */ 1507 if (is_multicast_ether_addr(hdr->addr1)) { 1508 ivlen = mvm->gtk_ivlen; 1509 icvlen += mvm->gtk_icvlen; 1510 } else { 1511 ivlen = mvm->ptk_ivlen; 1512 icvlen += mvm->ptk_icvlen; 1513 } 1514 } 1515 1516 /* if truncated, FCS/ICV is (partially) gone */ 1517 if (truncated >= icvlen) { 1518 icvlen = 0; 1519 truncated -= icvlen; 1520 } else { 1521 icvlen -= truncated; 1522 truncated = 0; 1523 } 1524 1525 pktsize -= ivlen + icvlen; 1526 pktdata += ivlen; 1527 1528 skb_put_data(pkt, pktdata, pktsize); 1529 1530 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type)) 1531 goto report; 1532 wakeup.packet = pkt->data; 1533 wakeup.packet_present_len = pkt->len; 1534 wakeup.packet_len = pkt->len - truncated; 1535 wakeup.packet_80211 = false; 1536 } else { 1537 int fcslen = 4; 1538 1539 if (truncated >= 4) { 1540 truncated -= 4; 1541 fcslen = 0; 1542 } else { 1543 fcslen -= truncated; 1544 truncated = 0; 1545 } 1546 pktsize -= fcslen; 1547 wakeup.packet = status->wake_packet; 1548 wakeup.packet_present_len = pktsize; 1549 wakeup.packet_len = pktlen - truncated; 1550 wakeup.packet_80211 = true; 1551 } 1552 } 1553 1554 report: 1555 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL); 1556 kfree_skb(pkt); 1557 } 1558 1559 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc, 1560 struct ieee80211_key_seq *seq) 1561 { 1562 u64 pn; 1563 1564 pn = le64_to_cpu(sc->pn); 1565 seq->ccmp.pn[0] = pn >> 40; 1566 seq->ccmp.pn[1] = pn >> 32; 1567 seq->ccmp.pn[2] = pn >> 24; 1568 seq->ccmp.pn[3] = pn >> 16; 1569 seq->ccmp.pn[4] = pn >> 8; 1570 seq->ccmp.pn[5] = pn; 1571 } 1572 1573 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc, 1574 struct ieee80211_key_seq *seq) 1575 { 1576 seq->tkip.iv32 = le32_to_cpu(sc->iv32); 1577 seq->tkip.iv16 = le16_to_cpu(sc->iv16); 1578 } 1579 1580 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key, 1581 struct ieee80211_key_seq *seq) 1582 { 1583 int tid; 1584 1585 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) 1586 ieee80211_set_key_rx_seq(key, tid, &seq[tid]); 1587 } 1588 1589 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm, 1590 struct iwl_wowlan_status_data *status, 1591 struct ieee80211_sta *sta, 1592 struct ieee80211_key_conf *key) 1593 { 1594 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1595 struct iwl_mvm_key_pn *ptk_pn; 1596 int tid; 1597 1598 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq); 1599 1600 if (!iwl_mvm_has_new_rx_api(mvm)) 1601 return; 1602 1603 1604 rcu_read_lock(); 1605 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]); 1606 if (WARN_ON(!ptk_pn)) { 1607 rcu_read_unlock(); 1608 return; 1609 } 1610 1611 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 1612 int i; 1613 1614 for (i = 1; i < mvm->trans->num_rx_queues; i++) 1615 memcpy(ptk_pn->q[i].pn[tid], 1616 status->ptk.aes.seq[tid].ccmp.pn, 1617 IEEE80211_CCMP_PN_LEN); 1618 } 1619 rcu_read_unlock(); 1620 } 1621 1622 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status, 1623 union iwl_all_tsc_rsc *sc) 1624 { 1625 int i; 1626 1627 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT); 1628 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC); 1629 1630 /* GTK RX counters */ 1631 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 1632 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i], 1633 &status->gtk.tkip.seq[i]); 1634 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i], 1635 &status->gtk.aes.seq[i]); 1636 } 1637 1638 /* PTK TX counter */ 1639 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) | 1640 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16); 1641 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn); 1642 1643 /* PTK RX counters */ 1644 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 1645 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i], 1646 &status->ptk.tkip.seq[i]); 1647 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i], 1648 &status->ptk.aes.seq[i]); 1649 } 1650 } 1651 1652 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm, 1653 struct ieee80211_key_conf *key, 1654 struct iwl_wowlan_status_data *status) 1655 { 1656 switch (key->cipher) { 1657 case WLAN_CIPHER_SUITE_CCMP: 1658 case WLAN_CIPHER_SUITE_GCMP: 1659 case WLAN_CIPHER_SUITE_GCMP_256: 1660 iwl_mvm_set_key_rx_seq_tids(key, status->gtk.aes.seq); 1661 break; 1662 case WLAN_CIPHER_SUITE_TKIP: 1663 iwl_mvm_set_key_rx_seq_tids(key, status->gtk.tkip.seq); 1664 break; 1665 default: 1666 WARN_ON(1); 1667 } 1668 } 1669 1670 struct iwl_mvm_d3_gtk_iter_data { 1671 struct iwl_mvm *mvm; 1672 struct iwl_wowlan_status_data *status; 1673 void *last_gtk; 1674 u32 cipher; 1675 bool find_phase, unhandled_cipher; 1676 int num_keys; 1677 }; 1678 1679 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw, 1680 struct ieee80211_vif *vif, 1681 struct ieee80211_sta *sta, 1682 struct ieee80211_key_conf *key, 1683 void *_data) 1684 { 1685 struct iwl_mvm_d3_gtk_iter_data *data = _data; 1686 struct iwl_wowlan_status_data *status = data->status; 1687 1688 if (data->unhandled_cipher) 1689 return; 1690 1691 switch (key->cipher) { 1692 case WLAN_CIPHER_SUITE_WEP40: 1693 case WLAN_CIPHER_SUITE_WEP104: 1694 /* ignore WEP completely, nothing to do */ 1695 return; 1696 case WLAN_CIPHER_SUITE_CCMP: 1697 case WLAN_CIPHER_SUITE_GCMP: 1698 case WLAN_CIPHER_SUITE_GCMP_256: 1699 case WLAN_CIPHER_SUITE_TKIP: 1700 /* we support these */ 1701 break; 1702 default: 1703 /* everything else (even CMAC for MFP) - disconnect from AP */ 1704 data->unhandled_cipher = true; 1705 return; 1706 } 1707 1708 data->num_keys++; 1709 1710 /* 1711 * pairwise key - update sequence counters only; 1712 * note that this assumes no TDLS sessions are active 1713 */ 1714 if (sta) { 1715 if (data->find_phase) 1716 return; 1717 1718 switch (key->cipher) { 1719 case WLAN_CIPHER_SUITE_CCMP: 1720 case WLAN_CIPHER_SUITE_GCMP: 1721 case WLAN_CIPHER_SUITE_GCMP_256: 1722 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn); 1723 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key); 1724 break; 1725 case WLAN_CIPHER_SUITE_TKIP: 1726 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn); 1727 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq); 1728 break; 1729 } 1730 1731 /* that's it for this key */ 1732 return; 1733 } 1734 1735 if (data->find_phase) { 1736 data->last_gtk = key; 1737 data->cipher = key->cipher; 1738 return; 1739 } 1740 1741 if (data->status->num_of_gtk_rekeys) 1742 ieee80211_remove_key(key); 1743 else if (data->last_gtk == key) 1744 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status); 1745 } 1746 1747 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm, 1748 struct ieee80211_vif *vif, 1749 struct iwl_wowlan_status_data *status) 1750 { 1751 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1752 struct iwl_mvm_d3_gtk_iter_data gtkdata = { 1753 .mvm = mvm, 1754 .status = status, 1755 }; 1756 u32 disconnection_reasons = 1757 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 1758 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH; 1759 1760 if (!status || !vif->bss_conf.bssid) 1761 return false; 1762 1763 if (status->wakeup_reasons & disconnection_reasons) 1764 return false; 1765 1766 /* find last GTK that we used initially, if any */ 1767 gtkdata.find_phase = true; 1768 ieee80211_iter_keys(mvm->hw, vif, 1769 iwl_mvm_d3_update_keys, >kdata); 1770 /* not trying to keep connections with MFP/unhandled ciphers */ 1771 if (gtkdata.unhandled_cipher) 1772 return false; 1773 if (!gtkdata.num_keys) 1774 goto out; 1775 if (!gtkdata.last_gtk) 1776 return false; 1777 1778 /* 1779 * invalidate all other GTKs that might still exist and update 1780 * the one that we used 1781 */ 1782 gtkdata.find_phase = false; 1783 ieee80211_iter_keys(mvm->hw, vif, 1784 iwl_mvm_d3_update_keys, >kdata); 1785 1786 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n", 1787 status->num_of_gtk_rekeys); 1788 if (status->num_of_gtk_rekeys) { 1789 struct ieee80211_key_conf *key; 1790 struct { 1791 struct ieee80211_key_conf conf; 1792 u8 key[32]; 1793 } conf = { 1794 .conf.cipher = gtkdata.cipher, 1795 .conf.keyidx = 1796 status->gtk.flags & IWL_WOWLAN_GTK_IDX_MASK, 1797 }; 1798 __be64 replay_ctr; 1799 1800 IWL_DEBUG_WOWLAN(mvm, 1801 "Received from FW GTK cipher %d, key index %d\n", 1802 conf.conf.cipher, conf.conf.keyidx); 1803 1804 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP); 1805 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP); 1806 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256); 1807 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP); 1808 BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk.key)); 1809 1810 memcpy(conf.conf.key, status->gtk.key, sizeof(status->gtk.key)); 1811 1812 switch (gtkdata.cipher) { 1813 case WLAN_CIPHER_SUITE_CCMP: 1814 case WLAN_CIPHER_SUITE_GCMP: 1815 conf.conf.keylen = WLAN_KEY_LEN_CCMP; 1816 break; 1817 case WLAN_CIPHER_SUITE_GCMP_256: 1818 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256; 1819 break; 1820 case WLAN_CIPHER_SUITE_TKIP: 1821 conf.conf.keylen = WLAN_KEY_LEN_TKIP; 1822 break; 1823 } 1824 1825 key = ieee80211_gtk_rekey_add(vif, &conf.conf); 1826 if (IS_ERR(key)) 1827 return false; 1828 iwl_mvm_set_key_rx_seq(mvm, key, status); 1829 1830 replay_ctr = cpu_to_be64(status->replay_ctr); 1831 1832 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid, 1833 (void *)&replay_ctr, GFP_KERNEL); 1834 } 1835 1836 out: 1837 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, 1838 WOWLAN_GET_STATUSES, 0) < 10) { 1839 mvmvif->seqno_valid = true; 1840 /* +0x10 because the set API expects next-to-use, not last-used */ 1841 mvmvif->seqno = status->non_qos_seq_ctr + 0x10; 1842 } 1843 1844 return true; 1845 } 1846 1847 /* Occasionally, templates would be nice. This is one of those times ... */ 1848 #define iwl_mvm_parse_wowlan_status_common(_ver) \ 1849 static struct iwl_wowlan_status_data * \ 1850 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \ 1851 struct iwl_wowlan_status_ ##_ver *data,\ 1852 int len) \ 1853 { \ 1854 struct iwl_wowlan_status_data *status; \ 1855 int data_size, i; \ 1856 \ 1857 if (len < sizeof(*data)) { \ 1858 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \ 1859 return ERR_PTR(-EIO); \ 1860 } \ 1861 \ 1862 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \ 1863 if (len != sizeof(*data) + data_size) { \ 1864 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \ 1865 return ERR_PTR(-EIO); \ 1866 } \ 1867 \ 1868 status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL); \ 1869 if (!status) \ 1870 return ERR_PTR(-ENOMEM); \ 1871 \ 1872 /* copy all the common fields */ \ 1873 status->replay_ctr = le64_to_cpu(data->replay_ctr); \ 1874 status->pattern_number = le16_to_cpu(data->pattern_number); \ 1875 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \ 1876 for (i = 0; i < 8; i++) \ 1877 status->qos_seq_ctr[i] = \ 1878 le16_to_cpu(data->qos_seq_ctr[i]); \ 1879 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \ 1880 status->num_of_gtk_rekeys = \ 1881 le32_to_cpu(data->num_of_gtk_rekeys); \ 1882 status->received_beacons = le32_to_cpu(data->received_beacons); \ 1883 status->wake_packet_length = \ 1884 le32_to_cpu(data->wake_packet_length); \ 1885 status->wake_packet_bufsize = \ 1886 le32_to_cpu(data->wake_packet_bufsize); \ 1887 memcpy(status->wake_packet, data->wake_packet, \ 1888 status->wake_packet_bufsize); \ 1889 \ 1890 return status; \ 1891 } 1892 1893 iwl_mvm_parse_wowlan_status_common(v6) 1894 iwl_mvm_parse_wowlan_status_common(v7) 1895 iwl_mvm_parse_wowlan_status_common(v9) 1896 1897 static void iwl_mvm_convert_gtk(struct iwl_wowlan_status_data *status, 1898 struct iwl_wowlan_gtk_status *data) 1899 { 1900 BUILD_BUG_ON(sizeof(status->gtk.key) < sizeof(data->key)); 1901 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY + 1902 sizeof(data->tkip_mic_key) > 1903 sizeof(status->gtk.key)); 1904 1905 status->gtk.len = data->key_len; 1906 status->gtk.flags = data->key_flags; 1907 1908 memcpy(status->gtk.key, data->key, sizeof(data->key)); 1909 1910 /* if it's as long as the TKIP encryption key, copy MIC key */ 1911 if (status->gtk.len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY) 1912 memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, 1913 data->tkip_mic_key, sizeof(data->tkip_mic_key)); 1914 } 1915 1916 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status, 1917 struct iwl_wowlan_igtk_status *data) 1918 { 1919 const u8 *ipn = data->ipn; 1920 1921 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key)); 1922 1923 status->igtk.len = data->key_len; 1924 status->igtk.flags = data->key_flags; 1925 1926 memcpy(status->igtk.key, data->key, sizeof(data->key)); 1927 1928 status->igtk.ipn = ((u64)ipn[5] << 0) | 1929 ((u64)ipn[4] << 8) | 1930 ((u64)ipn[3] << 16) | 1931 ((u64)ipn[2] << 24) | 1932 ((u64)ipn[1] << 32) | 1933 ((u64)ipn[0] << 40); 1934 } 1935 1936 static struct iwl_wowlan_status_data * 1937 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id) 1938 { 1939 struct iwl_wowlan_status_data *status; 1940 struct iwl_wowlan_get_status_cmd get_status_cmd = { 1941 .sta_id = cpu_to_le32(sta_id), 1942 }; 1943 struct iwl_host_cmd cmd = { 1944 .id = WOWLAN_GET_STATUSES, 1945 .flags = CMD_WANT_SKB, 1946 .data = { &get_status_cmd, }, 1947 .len = { sizeof(get_status_cmd), }, 1948 }; 1949 int ret, len; 1950 u8 notif_ver; 1951 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 1952 WOWLAN_GET_STATUSES, 1953 IWL_FW_CMD_VER_UNKNOWN); 1954 1955 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN) 1956 cmd.len[0] = 0; 1957 1958 lockdep_assert_held(&mvm->mutex); 1959 1960 ret = iwl_mvm_send_cmd(mvm, &cmd); 1961 if (ret) { 1962 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret); 1963 return ERR_PTR(ret); 1964 } 1965 1966 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 1967 1968 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */ 1969 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, 1970 WOWLAN_GET_STATUSES, 0); 1971 if (!notif_ver) 1972 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, 1973 WOWLAN_GET_STATUSES, 7); 1974 1975 if (!fw_has_api(&mvm->fw->ucode_capa, 1976 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) { 1977 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data; 1978 1979 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len); 1980 if (IS_ERR(status)) 1981 goto out_free_resp; 1982 1983 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) > 1984 sizeof(status->gtk.key)); 1985 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY + 1986 sizeof(v6->gtk.tkip_mic_key) > 1987 sizeof(status->gtk.key)); 1988 1989 /* copy GTK info to the right place */ 1990 memcpy(status->gtk.key, v6->gtk.decrypt_key, 1991 sizeof(v6->gtk.decrypt_key)); 1992 memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, 1993 v6->gtk.tkip_mic_key, 1994 sizeof(v6->gtk.tkip_mic_key)); 1995 1996 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc); 1997 1998 /* hardcode the key length to 16 since v6 only supports 16 */ 1999 status->gtk.len = 16; 2000 2001 /* 2002 * The key index only uses 2 bits (values 0 to 3) and 2003 * we always set bit 7 which means this is the 2004 * currently used key. 2005 */ 2006 status->gtk.flags = v6->gtk.key_index | BIT(7); 2007 } else if (notif_ver == 7) { 2008 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data; 2009 2010 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len); 2011 if (IS_ERR(status)) 2012 goto out_free_resp; 2013 2014 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc); 2015 iwl_mvm_convert_gtk(status, &v7->gtk[0]); 2016 iwl_mvm_convert_igtk(status, &v7->igtk[0]); 2017 } else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) { 2018 struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data; 2019 2020 /* these three command versions have same layout and size, the 2021 * difference is only in a few not used (reserved) fields. 2022 */ 2023 status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len); 2024 if (IS_ERR(status)) 2025 goto out_free_resp; 2026 2027 iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc); 2028 iwl_mvm_convert_gtk(status, &v9->gtk[0]); 2029 iwl_mvm_convert_igtk(status, &v9->igtk[0]); 2030 2031 status->tid_tear_down = v9->tid_tear_down; 2032 } else { 2033 IWL_ERR(mvm, 2034 "Firmware advertises unknown WoWLAN status response %d!\n", 2035 notif_ver); 2036 status = ERR_PTR(-EIO); 2037 } 2038 2039 out_free_resp: 2040 iwl_free_resp(&cmd); 2041 return status; 2042 } 2043 2044 static struct iwl_wowlan_status_data * 2045 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, u8 sta_id) 2046 { 2047 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 2048 OFFLOADS_QUERY_CMD, 2049 IWL_FW_CMD_VER_UNKNOWN); 2050 __le32 station_id = cpu_to_le32(sta_id); 2051 u32 cmd_size = cmd_ver != IWL_FW_CMD_VER_UNKNOWN ? sizeof(station_id) : 0; 2052 2053 if (!mvm->net_detect) { 2054 /* only for tracing for now */ 2055 int ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 2056 cmd_size, &station_id); 2057 if (ret) 2058 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret); 2059 } 2060 2061 return iwl_mvm_send_wowlan_get_status(mvm, sta_id); 2062 } 2063 2064 /* releases the MVM mutex */ 2065 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm, 2066 struct ieee80211_vif *vif) 2067 { 2068 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2069 struct iwl_wowlan_status_data *status; 2070 int i; 2071 bool keep; 2072 struct iwl_mvm_sta *mvm_ap_sta; 2073 2074 status = iwl_mvm_get_wakeup_status(mvm, mvmvif->ap_sta_id); 2075 if (IS_ERR(status)) 2076 goto out_unlock; 2077 2078 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n", 2079 status->wakeup_reasons); 2080 2081 /* still at hard-coded place 0 for D3 image */ 2082 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0); 2083 if (!mvm_ap_sta) 2084 goto out_free; 2085 2086 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 2087 u16 seq = status->qos_seq_ctr[i]; 2088 /* firmware stores last-used value, we store next value */ 2089 seq += 0x10; 2090 mvm_ap_sta->tid_data[i].seq_number = seq; 2091 } 2092 2093 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) { 2094 i = mvm->offload_tid; 2095 iwl_trans_set_q_ptrs(mvm->trans, 2096 mvm_ap_sta->tid_data[i].txq_id, 2097 mvm_ap_sta->tid_data[i].seq_number >> 4); 2098 } 2099 2100 /* now we have all the data we need, unlock to avoid mac80211 issues */ 2101 mutex_unlock(&mvm->mutex); 2102 2103 iwl_mvm_report_wakeup_reasons(mvm, vif, status); 2104 2105 keep = iwl_mvm_setup_connection_keep(mvm, vif, status); 2106 2107 kfree(status); 2108 return keep; 2109 2110 out_free: 2111 kfree(status); 2112 out_unlock: 2113 mutex_unlock(&mvm->mutex); 2114 return false; 2115 } 2116 2117 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \ 2118 IWL_SCAN_MAX_PROFILES) 2119 2120 struct iwl_mvm_nd_query_results { 2121 u32 matched_profiles; 2122 u8 matches[ND_QUERY_BUF_LEN]; 2123 }; 2124 2125 static int 2126 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm, 2127 struct iwl_mvm_nd_query_results *results) 2128 { 2129 struct iwl_scan_offload_profiles_query *query; 2130 struct iwl_host_cmd cmd = { 2131 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD, 2132 .flags = CMD_WANT_SKB, 2133 }; 2134 int ret, len; 2135 size_t query_len, matches_len; 2136 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw); 2137 2138 ret = iwl_mvm_send_cmd(mvm, &cmd); 2139 if (ret) { 2140 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret); 2141 return ret; 2142 } 2143 2144 if (fw_has_api(&mvm->fw->ucode_capa, 2145 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2146 query_len = sizeof(struct iwl_scan_offload_profiles_query); 2147 matches_len = sizeof(struct iwl_scan_offload_profile_match) * 2148 max_profiles; 2149 } else { 2150 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1); 2151 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) * 2152 max_profiles; 2153 } 2154 2155 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 2156 if (len < query_len) { 2157 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n"); 2158 ret = -EIO; 2159 goto out_free_resp; 2160 } 2161 2162 query = (void *)cmd.resp_pkt->data; 2163 2164 results->matched_profiles = le32_to_cpu(query->matched_profiles); 2165 memcpy(results->matches, query->matches, matches_len); 2166 2167 #ifdef CONFIG_IWLWIFI_DEBUGFS 2168 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done); 2169 #endif 2170 2171 out_free_resp: 2172 iwl_free_resp(&cmd); 2173 return ret; 2174 } 2175 2176 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm, 2177 struct iwl_mvm_nd_query_results *query, 2178 int idx) 2179 { 2180 int n_chans = 0, i; 2181 2182 if (fw_has_api(&mvm->fw->ucode_capa, 2183 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2184 struct iwl_scan_offload_profile_match *matches = 2185 (struct iwl_scan_offload_profile_match *)query->matches; 2186 2187 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++) 2188 n_chans += hweight8(matches[idx].matching_channels[i]); 2189 } else { 2190 struct iwl_scan_offload_profile_match_v1 *matches = 2191 (struct iwl_scan_offload_profile_match_v1 *)query->matches; 2192 2193 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++) 2194 n_chans += hweight8(matches[idx].matching_channels[i]); 2195 } 2196 2197 return n_chans; 2198 } 2199 2200 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm, 2201 struct iwl_mvm_nd_query_results *query, 2202 struct cfg80211_wowlan_nd_match *match, 2203 int idx) 2204 { 2205 int i; 2206 2207 if (fw_has_api(&mvm->fw->ucode_capa, 2208 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2209 struct iwl_scan_offload_profile_match *matches = 2210 (struct iwl_scan_offload_profile_match *)query->matches; 2211 2212 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++) 2213 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8))) 2214 match->channels[match->n_channels++] = 2215 mvm->nd_channels[i]->center_freq; 2216 } else { 2217 struct iwl_scan_offload_profile_match_v1 *matches = 2218 (struct iwl_scan_offload_profile_match_v1 *)query->matches; 2219 2220 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++) 2221 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8))) 2222 match->channels[match->n_channels++] = 2223 mvm->nd_channels[i]->center_freq; 2224 } 2225 } 2226 2227 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm, 2228 struct ieee80211_vif *vif) 2229 { 2230 struct cfg80211_wowlan_nd_info *net_detect = NULL; 2231 struct cfg80211_wowlan_wakeup wakeup = { 2232 .pattern_idx = -1, 2233 }; 2234 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup; 2235 struct iwl_wowlan_status_data *status; 2236 struct iwl_mvm_nd_query_results query; 2237 unsigned long matched_profiles; 2238 u32 reasons = 0; 2239 int i, n_matches, ret; 2240 2241 status = iwl_mvm_get_wakeup_status(mvm, IWL_MVM_INVALID_STA); 2242 if (!IS_ERR(status)) { 2243 reasons = status->wakeup_reasons; 2244 kfree(status); 2245 } 2246 2247 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 2248 wakeup.rfkill_release = true; 2249 2250 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) 2251 goto out; 2252 2253 ret = iwl_mvm_netdetect_query_results(mvm, &query); 2254 if (ret || !query.matched_profiles) { 2255 wakeup_report = NULL; 2256 goto out; 2257 } 2258 2259 matched_profiles = query.matched_profiles; 2260 if (mvm->n_nd_match_sets) { 2261 n_matches = hweight_long(matched_profiles); 2262 } else { 2263 IWL_ERR(mvm, "no net detect match information available\n"); 2264 n_matches = 0; 2265 } 2266 2267 net_detect = kzalloc(struct_size(net_detect, matches, n_matches), 2268 GFP_KERNEL); 2269 if (!net_detect || !n_matches) 2270 goto out_report_nd; 2271 2272 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) { 2273 struct cfg80211_wowlan_nd_match *match; 2274 int idx, n_channels = 0; 2275 2276 n_channels = iwl_mvm_query_num_match_chans(mvm, &query, i); 2277 2278 match = kzalloc(struct_size(match, channels, n_channels), 2279 GFP_KERNEL); 2280 if (!match) 2281 goto out_report_nd; 2282 2283 net_detect->matches[net_detect->n_matches++] = match; 2284 2285 /* We inverted the order of the SSIDs in the scan 2286 * request, so invert the index here. 2287 */ 2288 idx = mvm->n_nd_match_sets - i - 1; 2289 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len; 2290 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid, 2291 match->ssid.ssid_len); 2292 2293 if (mvm->n_nd_channels < n_channels) 2294 continue; 2295 2296 iwl_mvm_query_set_freqs(mvm, &query, match, i); 2297 } 2298 2299 out_report_nd: 2300 wakeup.net_detect = net_detect; 2301 out: 2302 iwl_mvm_free_nd(mvm); 2303 2304 mutex_unlock(&mvm->mutex); 2305 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL); 2306 2307 if (net_detect) { 2308 for (i = 0; i < net_detect->n_matches; i++) 2309 kfree(net_detect->matches[i]); 2310 kfree(net_detect); 2311 } 2312 } 2313 2314 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac, 2315 struct ieee80211_vif *vif) 2316 { 2317 /* skip the one we keep connection on */ 2318 if (data == vif) 2319 return; 2320 2321 if (vif->type == NL80211_IFTYPE_STATION) 2322 ieee80211_resume_disconnect(vif); 2323 } 2324 2325 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id) 2326 { 2327 struct error_table_start { 2328 /* cf. struct iwl_error_event_table */ 2329 u32 valid; 2330 __le32 err_id; 2331 } err_info; 2332 2333 if (!base) 2334 return false; 2335 2336 iwl_trans_read_mem_bytes(trans, base, 2337 &err_info, sizeof(err_info)); 2338 if (err_info.valid && err_id) 2339 *err_id = le32_to_cpu(err_info.err_id); 2340 2341 return !!err_info.valid; 2342 } 2343 2344 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm, 2345 struct ieee80211_vif *vif) 2346 { 2347 u32 err_id; 2348 2349 /* check for lmac1 error */ 2350 if (iwl_mvm_rt_status(mvm->trans, 2351 mvm->trans->dbg.lmac_error_event_table[0], 2352 &err_id)) { 2353 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) { 2354 struct cfg80211_wowlan_wakeup wakeup = { 2355 .rfkill_release = true, 2356 }; 2357 ieee80211_report_wowlan_wakeup(vif, &wakeup, 2358 GFP_KERNEL); 2359 } 2360 return true; 2361 } 2362 2363 /* check if we have lmac2 set and check for error */ 2364 if (iwl_mvm_rt_status(mvm->trans, 2365 mvm->trans->dbg.lmac_error_event_table[1], NULL)) 2366 return true; 2367 2368 /* check for umac error */ 2369 if (iwl_mvm_rt_status(mvm->trans, 2370 mvm->trans->dbg.umac_error_event_table, NULL)) 2371 return true; 2372 2373 return false; 2374 } 2375 2376 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test) 2377 { 2378 struct ieee80211_vif *vif = NULL; 2379 int ret = 1; 2380 enum iwl_d3_status d3_status; 2381 bool keep = false; 2382 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 2383 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 2384 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa, 2385 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST); 2386 2387 mutex_lock(&mvm->mutex); 2388 2389 mvm->last_reset_or_resume_time_jiffies = jiffies; 2390 2391 /* get the BSS vif pointer again */ 2392 vif = iwl_mvm_get_bss_vif(mvm); 2393 if (IS_ERR_OR_NULL(vif)) 2394 goto err; 2395 2396 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt); 2397 2398 if (iwl_mvm_check_rt_status(mvm, vif)) { 2399 set_bit(STATUS_FW_ERROR, &mvm->trans->status); 2400 iwl_mvm_dump_nic_error_log(mvm); 2401 iwl_dbg_tlv_time_point(&mvm->fwrt, 2402 IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL); 2403 iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert, 2404 false, 0); 2405 ret = 1; 2406 goto err; 2407 } 2408 2409 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image); 2410 if (ret) 2411 goto err; 2412 2413 if (d3_status != IWL_D3_STATUS_ALIVE) { 2414 IWL_INFO(mvm, "Device was reset during suspend\n"); 2415 goto err; 2416 } 2417 2418 if (d0i3_first) { 2419 struct iwl_host_cmd cmd = { 2420 .id = D0I3_END_CMD, 2421 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3, 2422 }; 2423 int len; 2424 2425 ret = iwl_mvm_send_cmd(mvm, &cmd); 2426 if (ret < 0) { 2427 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n", 2428 ret); 2429 goto err; 2430 } 2431 switch (mvm->cmd_ver.d0i3_resp) { 2432 case 0: 2433 break; 2434 case 1: 2435 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 2436 if (len != sizeof(u32)) { 2437 IWL_ERR(mvm, 2438 "Error with D0I3_END_CMD response size (%d)\n", 2439 len); 2440 goto err; 2441 } 2442 if (IWL_D0I3_RESET_REQUIRE & 2443 le32_to_cpu(*(__le32 *)cmd.resp_pkt->data)) { 2444 iwl_write32(mvm->trans, CSR_RESET, 2445 CSR_RESET_REG_FLAG_FORCE_NMI); 2446 iwl_free_resp(&cmd); 2447 } 2448 break; 2449 default: 2450 WARN_ON(1); 2451 } 2452 } 2453 2454 /* after the successful handshake, we're out of D3 */ 2455 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED; 2456 2457 /* 2458 * Query the current location and source from the D3 firmware so we 2459 * can play it back when we re-intiailize the D0 firmware 2460 */ 2461 iwl_mvm_update_changed_regdom(mvm); 2462 2463 /* Re-configure PPAG settings */ 2464 iwl_mvm_ppag_send_cmd(mvm); 2465 2466 if (!unified_image) 2467 /* Re-configure default SAR profile */ 2468 iwl_mvm_sar_select_profile(mvm, 1, 1); 2469 2470 if (mvm->net_detect) { 2471 /* If this is a non-unified image, we restart the FW, 2472 * so no need to stop the netdetect scan. If that 2473 * fails, continue and try to get the wake-up reasons, 2474 * but trigger a HW restart by keeping a failure code 2475 * in ret. 2476 */ 2477 if (unified_image) 2478 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT, 2479 false); 2480 2481 iwl_mvm_query_netdetect_reasons(mvm, vif); 2482 /* has unlocked the mutex, so skip that */ 2483 goto out; 2484 } else { 2485 keep = iwl_mvm_query_wakeup_reasons(mvm, vif); 2486 #ifdef CONFIG_IWLWIFI_DEBUGFS 2487 if (keep) 2488 mvm->keep_vif = vif; 2489 #endif 2490 /* has unlocked the mutex, so skip that */ 2491 goto out_iterate; 2492 } 2493 2494 err: 2495 iwl_mvm_free_nd(mvm); 2496 mutex_unlock(&mvm->mutex); 2497 2498 out_iterate: 2499 if (!test) 2500 ieee80211_iterate_active_interfaces_mtx(mvm->hw, 2501 IEEE80211_IFACE_ITER_NORMAL, 2502 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL); 2503 2504 out: 2505 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status); 2506 2507 /* no need to reset the device in unified images, if successful */ 2508 if (unified_image && !ret) { 2509 /* nothing else to do if we already sent D0I3_END_CMD */ 2510 if (d0i3_first) 2511 return 0; 2512 2513 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL); 2514 if (!ret) 2515 return 0; 2516 } 2517 2518 /* 2519 * Reconfigure the device in one of the following cases: 2520 * 1. We are not using a unified image 2521 * 2. We are using a unified image but had an error while exiting D3 2522 */ 2523 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status); 2524 2525 /* regardless of what happened, we're now out of D3 */ 2526 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED; 2527 2528 return 1; 2529 } 2530 2531 int iwl_mvm_resume(struct ieee80211_hw *hw) 2532 { 2533 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 2534 int ret; 2535 2536 ret = __iwl_mvm_resume(mvm, false); 2537 2538 iwl_mvm_resume_tcm(mvm); 2539 2540 iwl_fw_runtime_resume(&mvm->fwrt); 2541 2542 return ret; 2543 } 2544 2545 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled) 2546 { 2547 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 2548 2549 device_set_wakeup_enable(mvm->trans->dev, enabled); 2550 } 2551 2552 #ifdef CONFIG_IWLWIFI_DEBUGFS 2553 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file) 2554 { 2555 struct iwl_mvm *mvm = inode->i_private; 2556 int err; 2557 2558 if (mvm->d3_test_active) 2559 return -EBUSY; 2560 2561 file->private_data = inode->i_private; 2562 2563 iwl_mvm_pause_tcm(mvm, true); 2564 2565 iwl_fw_runtime_suspend(&mvm->fwrt); 2566 2567 /* start pseudo D3 */ 2568 rtnl_lock(); 2569 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true); 2570 rtnl_unlock(); 2571 if (err > 0) 2572 err = -EINVAL; 2573 if (err) 2574 return err; 2575 2576 mvm->d3_test_active = true; 2577 mvm->keep_vif = NULL; 2578 return 0; 2579 } 2580 2581 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf, 2582 size_t count, loff_t *ppos) 2583 { 2584 struct iwl_mvm *mvm = file->private_data; 2585 u32 pme_asserted; 2586 2587 while (true) { 2588 /* read pme_ptr if available */ 2589 if (mvm->d3_test_pme_ptr) { 2590 pme_asserted = iwl_trans_read_mem32(mvm->trans, 2591 mvm->d3_test_pme_ptr); 2592 if (pme_asserted) 2593 break; 2594 } 2595 2596 if (msleep_interruptible(100)) 2597 break; 2598 } 2599 2600 return 0; 2601 } 2602 2603 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac, 2604 struct ieee80211_vif *vif) 2605 { 2606 /* skip the one we keep connection on */ 2607 if (_data == vif) 2608 return; 2609 2610 if (vif->type == NL80211_IFTYPE_STATION) 2611 ieee80211_connection_loss(vif); 2612 } 2613 2614 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file) 2615 { 2616 struct iwl_mvm *mvm = inode->i_private; 2617 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 2618 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 2619 2620 mvm->d3_test_active = false; 2621 2622 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt); 2623 2624 rtnl_lock(); 2625 __iwl_mvm_resume(mvm, true); 2626 rtnl_unlock(); 2627 2628 iwl_mvm_resume_tcm(mvm); 2629 2630 iwl_fw_runtime_resume(&mvm->fwrt); 2631 2632 iwl_abort_notification_waits(&mvm->notif_wait); 2633 if (!unified_image) { 2634 int remaining_time = 10; 2635 2636 ieee80211_restart_hw(mvm->hw); 2637 2638 /* wait for restart and disconnect all interfaces */ 2639 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) && 2640 remaining_time > 0) { 2641 remaining_time--; 2642 msleep(1000); 2643 } 2644 2645 if (remaining_time == 0) 2646 IWL_ERR(mvm, "Timed out waiting for HW restart!\n"); 2647 } 2648 2649 ieee80211_iterate_active_interfaces_atomic( 2650 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 2651 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif); 2652 2653 return 0; 2654 } 2655 2656 const struct file_operations iwl_dbgfs_d3_test_ops = { 2657 .llseek = no_llseek, 2658 .open = iwl_mvm_d3_test_open, 2659 .read = iwl_mvm_d3_test_read, 2660 .release = iwl_mvm_d3_test_release, 2661 }; 2662 #endif 2663