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 /* including RX MIC key for TKIP */ 1395 u8 key[WOWLAN_KEY_MAX_SIZE]; 1396 u8 len; 1397 u8 flags; 1398 } gtk; 1399 1400 struct { 1401 /* 1402 * We store both the TKIP and AES representations 1403 * coming from the firmware because we decode the 1404 * data from there before we iterate the keys and 1405 * know which one we need. 1406 */ 1407 struct { 1408 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT]; 1409 } tkip, aes; 1410 1411 /* 1412 * We use -1 for when we have valid data but don't know 1413 * the key ID from firmware, and thus it needs to be 1414 * installed with the last key (depending on rekeying). 1415 */ 1416 s8 key_id; 1417 bool valid; 1418 } gtk_seq[2]; 1419 1420 struct { 1421 /* Same as above */ 1422 struct { 1423 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT]; 1424 u64 tx_pn; 1425 } tkip, aes; 1426 } ptk; 1427 1428 struct { 1429 u64 ipn; 1430 u8 key[WOWLAN_KEY_MAX_SIZE]; 1431 u8 len; 1432 u8 flags; 1433 } igtk; 1434 1435 u8 wake_packet[]; 1436 }; 1437 1438 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm, 1439 struct ieee80211_vif *vif, 1440 struct iwl_wowlan_status_data *status) 1441 { 1442 struct sk_buff *pkt = NULL; 1443 struct cfg80211_wowlan_wakeup wakeup = { 1444 .pattern_idx = -1, 1445 }; 1446 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup; 1447 u32 reasons = status->wakeup_reasons; 1448 1449 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) { 1450 wakeup_report = NULL; 1451 goto report; 1452 } 1453 1454 pm_wakeup_event(mvm->dev, 0); 1455 1456 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET) 1457 wakeup.magic_pkt = true; 1458 1459 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN) 1460 wakeup.pattern_idx = 1461 status->pattern_number; 1462 1463 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 1464 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH)) 1465 wakeup.disconnect = true; 1466 1467 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE) 1468 wakeup.gtk_rekey_failure = true; 1469 1470 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 1471 wakeup.rfkill_release = true; 1472 1473 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST) 1474 wakeup.eap_identity_req = true; 1475 1476 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE) 1477 wakeup.four_way_handshake = true; 1478 1479 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS) 1480 wakeup.tcp_connlost = true; 1481 1482 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE) 1483 wakeup.tcp_nomoretokens = true; 1484 1485 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET) 1486 wakeup.tcp_match = true; 1487 1488 if (status->wake_packet_bufsize) { 1489 int pktsize = status->wake_packet_bufsize; 1490 int pktlen = status->wake_packet_length; 1491 const u8 *pktdata = status->wake_packet; 1492 struct ieee80211_hdr *hdr = (void *)pktdata; 1493 int truncated = pktlen - pktsize; 1494 1495 /* this would be a firmware bug */ 1496 if (WARN_ON_ONCE(truncated < 0)) 1497 truncated = 0; 1498 1499 if (ieee80211_is_data(hdr->frame_control)) { 1500 int hdrlen = ieee80211_hdrlen(hdr->frame_control); 1501 int ivlen = 0, icvlen = 4; /* also FCS */ 1502 1503 pkt = alloc_skb(pktsize, GFP_KERNEL); 1504 if (!pkt) 1505 goto report; 1506 1507 skb_put_data(pkt, pktdata, hdrlen); 1508 pktdata += hdrlen; 1509 pktsize -= hdrlen; 1510 1511 if (ieee80211_has_protected(hdr->frame_control)) { 1512 /* 1513 * This is unlocked and using gtk_i(c)vlen, 1514 * but since everything is under RTNL still 1515 * that's not really a problem - changing 1516 * it would be difficult. 1517 */ 1518 if (is_multicast_ether_addr(hdr->addr1)) { 1519 ivlen = mvm->gtk_ivlen; 1520 icvlen += mvm->gtk_icvlen; 1521 } else { 1522 ivlen = mvm->ptk_ivlen; 1523 icvlen += mvm->ptk_icvlen; 1524 } 1525 } 1526 1527 /* if truncated, FCS/ICV is (partially) gone */ 1528 if (truncated >= icvlen) { 1529 icvlen = 0; 1530 truncated -= icvlen; 1531 } else { 1532 icvlen -= truncated; 1533 truncated = 0; 1534 } 1535 1536 pktsize -= ivlen + icvlen; 1537 pktdata += ivlen; 1538 1539 skb_put_data(pkt, pktdata, pktsize); 1540 1541 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type)) 1542 goto report; 1543 wakeup.packet = pkt->data; 1544 wakeup.packet_present_len = pkt->len; 1545 wakeup.packet_len = pkt->len - truncated; 1546 wakeup.packet_80211 = false; 1547 } else { 1548 int fcslen = 4; 1549 1550 if (truncated >= 4) { 1551 truncated -= 4; 1552 fcslen = 0; 1553 } else { 1554 fcslen -= truncated; 1555 truncated = 0; 1556 } 1557 pktsize -= fcslen; 1558 wakeup.packet = status->wake_packet; 1559 wakeup.packet_present_len = pktsize; 1560 wakeup.packet_len = pktlen - truncated; 1561 wakeup.packet_80211 = true; 1562 } 1563 } 1564 1565 report: 1566 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL); 1567 kfree_skb(pkt); 1568 } 1569 1570 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq) 1571 { 1572 u64 pn = le64_to_cpu(le_pn); 1573 1574 seq->ccmp.pn[0] = pn >> 40; 1575 seq->ccmp.pn[1] = pn >> 32; 1576 seq->ccmp.pn[2] = pn >> 24; 1577 seq->ccmp.pn[3] = pn >> 16; 1578 seq->ccmp.pn[4] = pn >> 8; 1579 seq->ccmp.pn[5] = pn; 1580 } 1581 1582 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc, 1583 struct ieee80211_key_seq *seq) 1584 { 1585 iwl_mvm_le64_to_aes_seq(sc->pn, seq); 1586 } 1587 1588 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq) 1589 { 1590 u64 pn = le64_to_cpu(le_pn); 1591 1592 seq->tkip.iv16 = (u16)pn; 1593 seq->tkip.iv32 = (u32)(pn >> 16); 1594 } 1595 1596 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc, 1597 struct ieee80211_key_seq *seq) 1598 { 1599 seq->tkip.iv32 = le32_to_cpu(sc->iv32); 1600 seq->tkip.iv16 = le16_to_cpu(sc->iv16); 1601 } 1602 1603 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key, 1604 struct ieee80211_key_seq *seq) 1605 { 1606 int tid; 1607 1608 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) 1609 ieee80211_set_key_rx_seq(key, tid, &seq[tid]); 1610 } 1611 1612 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm, 1613 struct iwl_wowlan_status_data *status, 1614 struct ieee80211_sta *sta, 1615 struct ieee80211_key_conf *key) 1616 { 1617 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1618 struct iwl_mvm_key_pn *ptk_pn; 1619 int tid; 1620 1621 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq); 1622 1623 if (!iwl_mvm_has_new_rx_api(mvm)) 1624 return; 1625 1626 1627 rcu_read_lock(); 1628 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]); 1629 if (WARN_ON(!ptk_pn)) { 1630 rcu_read_unlock(); 1631 return; 1632 } 1633 1634 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 1635 int i; 1636 1637 for (i = 1; i < mvm->trans->num_rx_queues; i++) 1638 memcpy(ptk_pn->q[i].pn[tid], 1639 status->ptk.aes.seq[tid].ccmp.pn, 1640 IEEE80211_CCMP_PN_LEN); 1641 } 1642 rcu_read_unlock(); 1643 } 1644 1645 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status, 1646 union iwl_all_tsc_rsc *sc) 1647 { 1648 int i; 1649 1650 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT); 1651 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC); 1652 1653 /* GTK RX counters */ 1654 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 1655 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i], 1656 &status->gtk_seq[0].tkip.seq[i]); 1657 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i], 1658 &status->gtk_seq[0].aes.seq[i]); 1659 } 1660 status->gtk_seq[0].valid = true; 1661 status->gtk_seq[0].key_id = -1; 1662 1663 /* PTK TX counter */ 1664 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) | 1665 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16); 1666 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn); 1667 1668 /* PTK RX counters */ 1669 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 1670 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i], 1671 &status->ptk.tkip.seq[i]); 1672 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i], 1673 &status->ptk.aes.seq[i]); 1674 } 1675 } 1676 1677 static void 1678 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status, 1679 struct iwl_wowlan_all_rsc_tsc_v5 *sc, 1680 unsigned int idx, unsigned int key_id) 1681 { 1682 int tid; 1683 1684 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 1685 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid], 1686 &status->gtk_seq[idx].tkip.seq[tid]); 1687 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid], 1688 &status->gtk_seq[idx].aes.seq[tid]); 1689 } 1690 1691 status->gtk_seq[idx].valid = true; 1692 status->gtk_seq[idx].key_id = key_id; 1693 } 1694 1695 static void 1696 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status, 1697 struct iwl_wowlan_all_rsc_tsc_v5 *sc) 1698 { 1699 int i, tid; 1700 1701 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT); 1702 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC); 1703 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq)); 1704 1705 /* GTK RX counters */ 1706 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) { 1707 u8 entry = sc->mcast_key_id_map[i]; 1708 1709 if (entry < ARRAY_SIZE(sc->mcast_rsc)) 1710 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc, 1711 entry, i); 1712 } 1713 1714 /* PTK TX counters not needed, assigned in device */ 1715 1716 /* PTK RX counters */ 1717 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 1718 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid], 1719 &status->ptk.tkip.seq[tid]); 1720 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid], 1721 &status->ptk.aes.seq[tid]); 1722 } 1723 } 1724 1725 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key, 1726 struct iwl_wowlan_status_data *status, 1727 int idx) 1728 { 1729 switch (key->cipher) { 1730 case WLAN_CIPHER_SUITE_CCMP: 1731 case WLAN_CIPHER_SUITE_GCMP: 1732 case WLAN_CIPHER_SUITE_GCMP_256: 1733 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq); 1734 break; 1735 case WLAN_CIPHER_SUITE_TKIP: 1736 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq); 1737 break; 1738 default: 1739 WARN_ON(1); 1740 } 1741 } 1742 1743 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key, 1744 struct iwl_wowlan_status_data *status, 1745 bool installed) 1746 { 1747 int i; 1748 1749 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) { 1750 if (!status->gtk_seq[i].valid) 1751 continue; 1752 1753 /* Handle the case where we know the key ID */ 1754 if (status->gtk_seq[i].key_id == key->keyidx) { 1755 s8 new_key_id = -1; 1756 1757 if (status->num_of_gtk_rekeys) 1758 new_key_id = status->gtk.flags & 1759 IWL_WOWLAN_GTK_IDX_MASK; 1760 1761 /* Don't install a new key's value to an old key */ 1762 if (new_key_id != key->keyidx) 1763 iwl_mvm_set_key_rx_seq_idx(key, status, i); 1764 continue; 1765 } 1766 1767 /* handle the case where we didn't, last key only */ 1768 if (status->gtk_seq[i].key_id == -1 && 1769 (!status->num_of_gtk_rekeys || installed)) 1770 iwl_mvm_set_key_rx_seq_idx(key, status, i); 1771 } 1772 } 1773 1774 struct iwl_mvm_d3_gtk_iter_data { 1775 struct iwl_mvm *mvm; 1776 struct iwl_wowlan_status_data *status; 1777 void *last_gtk; 1778 u32 cipher; 1779 bool find_phase, unhandled_cipher; 1780 int num_keys; 1781 }; 1782 1783 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw, 1784 struct ieee80211_vif *vif, 1785 struct ieee80211_sta *sta, 1786 struct ieee80211_key_conf *key, 1787 void *_data) 1788 { 1789 struct iwl_mvm_d3_gtk_iter_data *data = _data; 1790 struct iwl_wowlan_status_data *status = data->status; 1791 1792 if (data->unhandled_cipher) 1793 return; 1794 1795 switch (key->cipher) { 1796 case WLAN_CIPHER_SUITE_WEP40: 1797 case WLAN_CIPHER_SUITE_WEP104: 1798 /* ignore WEP completely, nothing to do */ 1799 return; 1800 case WLAN_CIPHER_SUITE_CCMP: 1801 case WLAN_CIPHER_SUITE_GCMP: 1802 case WLAN_CIPHER_SUITE_GCMP_256: 1803 case WLAN_CIPHER_SUITE_TKIP: 1804 /* we support these */ 1805 break; 1806 default: 1807 /* everything else (even CMAC for MFP) - disconnect from AP */ 1808 data->unhandled_cipher = true; 1809 return; 1810 } 1811 1812 data->num_keys++; 1813 1814 /* 1815 * pairwise key - update sequence counters only; 1816 * note that this assumes no TDLS sessions are active 1817 */ 1818 if (sta) { 1819 if (data->find_phase) 1820 return; 1821 1822 switch (key->cipher) { 1823 case WLAN_CIPHER_SUITE_CCMP: 1824 case WLAN_CIPHER_SUITE_GCMP: 1825 case WLAN_CIPHER_SUITE_GCMP_256: 1826 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn); 1827 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key); 1828 break; 1829 case WLAN_CIPHER_SUITE_TKIP: 1830 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn); 1831 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq); 1832 break; 1833 } 1834 1835 /* that's it for this key */ 1836 return; 1837 } 1838 1839 if (data->find_phase) { 1840 data->last_gtk = key; 1841 data->cipher = key->cipher; 1842 return; 1843 } 1844 1845 if (data->status->num_of_gtk_rekeys) 1846 ieee80211_remove_key(key); 1847 1848 if (data->last_gtk == key) 1849 iwl_mvm_set_key_rx_seq(key, data->status, false); 1850 } 1851 1852 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm, 1853 struct ieee80211_vif *vif, 1854 struct iwl_wowlan_status_data *status) 1855 { 1856 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1857 struct iwl_mvm_d3_gtk_iter_data gtkdata = { 1858 .mvm = mvm, 1859 .status = status, 1860 }; 1861 u32 disconnection_reasons = 1862 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 1863 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH; 1864 1865 if (!status || !vif->bss_conf.bssid) 1866 return false; 1867 1868 if (status->wakeup_reasons & disconnection_reasons) 1869 return false; 1870 1871 /* find last GTK that we used initially, if any */ 1872 gtkdata.find_phase = true; 1873 ieee80211_iter_keys(mvm->hw, vif, 1874 iwl_mvm_d3_update_keys, >kdata); 1875 /* not trying to keep connections with MFP/unhandled ciphers */ 1876 if (gtkdata.unhandled_cipher) 1877 return false; 1878 if (!gtkdata.num_keys) 1879 goto out; 1880 if (!gtkdata.last_gtk) 1881 return false; 1882 1883 /* 1884 * invalidate all other GTKs that might still exist and update 1885 * the one that we used 1886 */ 1887 gtkdata.find_phase = false; 1888 ieee80211_iter_keys(mvm->hw, vif, 1889 iwl_mvm_d3_update_keys, >kdata); 1890 1891 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n", 1892 status->num_of_gtk_rekeys); 1893 if (status->num_of_gtk_rekeys) { 1894 struct ieee80211_key_conf *key; 1895 struct { 1896 struct ieee80211_key_conf conf; 1897 u8 key[32]; 1898 } conf = { 1899 .conf.cipher = gtkdata.cipher, 1900 .conf.keyidx = 1901 status->gtk.flags & IWL_WOWLAN_GTK_IDX_MASK, 1902 }; 1903 __be64 replay_ctr; 1904 1905 IWL_DEBUG_WOWLAN(mvm, 1906 "Received from FW GTK cipher %d, key index %d\n", 1907 conf.conf.cipher, conf.conf.keyidx); 1908 1909 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP); 1910 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP); 1911 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256); 1912 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP); 1913 BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk.key)); 1914 1915 memcpy(conf.conf.key, status->gtk.key, sizeof(status->gtk.key)); 1916 1917 switch (gtkdata.cipher) { 1918 case WLAN_CIPHER_SUITE_CCMP: 1919 case WLAN_CIPHER_SUITE_GCMP: 1920 conf.conf.keylen = WLAN_KEY_LEN_CCMP; 1921 break; 1922 case WLAN_CIPHER_SUITE_GCMP_256: 1923 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256; 1924 break; 1925 case WLAN_CIPHER_SUITE_TKIP: 1926 conf.conf.keylen = WLAN_KEY_LEN_TKIP; 1927 break; 1928 } 1929 1930 key = ieee80211_gtk_rekey_add(vif, &conf.conf); 1931 if (IS_ERR(key)) 1932 return false; 1933 iwl_mvm_set_key_rx_seq(key, status, true); 1934 1935 replay_ctr = cpu_to_be64(status->replay_ctr); 1936 1937 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid, 1938 (void *)&replay_ctr, GFP_KERNEL); 1939 } 1940 1941 out: 1942 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, 1943 WOWLAN_GET_STATUSES, 0) < 10) { 1944 mvmvif->seqno_valid = true; 1945 /* +0x10 because the set API expects next-to-use, not last-used */ 1946 mvmvif->seqno = status->non_qos_seq_ctr + 0x10; 1947 } 1948 1949 return true; 1950 } 1951 1952 /* Occasionally, templates would be nice. This is one of those times ... */ 1953 #define iwl_mvm_parse_wowlan_status_common(_ver) \ 1954 static struct iwl_wowlan_status_data * \ 1955 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \ 1956 struct iwl_wowlan_status_ ##_ver *data,\ 1957 int len) \ 1958 { \ 1959 struct iwl_wowlan_status_data *status; \ 1960 int data_size, i; \ 1961 \ 1962 if (len < sizeof(*data)) { \ 1963 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \ 1964 return ERR_PTR(-EIO); \ 1965 } \ 1966 \ 1967 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \ 1968 if (len != sizeof(*data) + data_size) { \ 1969 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \ 1970 return ERR_PTR(-EIO); \ 1971 } \ 1972 \ 1973 status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL); \ 1974 if (!status) \ 1975 return ERR_PTR(-ENOMEM); \ 1976 \ 1977 /* copy all the common fields */ \ 1978 status->replay_ctr = le64_to_cpu(data->replay_ctr); \ 1979 status->pattern_number = le16_to_cpu(data->pattern_number); \ 1980 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \ 1981 for (i = 0; i < 8; i++) \ 1982 status->qos_seq_ctr[i] = \ 1983 le16_to_cpu(data->qos_seq_ctr[i]); \ 1984 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \ 1985 status->num_of_gtk_rekeys = \ 1986 le32_to_cpu(data->num_of_gtk_rekeys); \ 1987 status->received_beacons = le32_to_cpu(data->received_beacons); \ 1988 status->wake_packet_length = \ 1989 le32_to_cpu(data->wake_packet_length); \ 1990 status->wake_packet_bufsize = \ 1991 le32_to_cpu(data->wake_packet_bufsize); \ 1992 memcpy(status->wake_packet, data->wake_packet, \ 1993 status->wake_packet_bufsize); \ 1994 \ 1995 return status; \ 1996 } 1997 1998 iwl_mvm_parse_wowlan_status_common(v6) 1999 iwl_mvm_parse_wowlan_status_common(v7) 2000 iwl_mvm_parse_wowlan_status_common(v9) 2001 iwl_mvm_parse_wowlan_status_common(v12) 2002 2003 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status, 2004 struct iwl_wowlan_gtk_status_v2 *data) 2005 { 2006 BUILD_BUG_ON(sizeof(status->gtk.key) < sizeof(data->key)); 2007 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY + 2008 sizeof(data->tkip_mic_key) > 2009 sizeof(status->gtk.key)); 2010 2011 status->gtk.len = data->key_len; 2012 status->gtk.flags = data->key_flags; 2013 2014 memcpy(status->gtk.key, data->key, sizeof(data->key)); 2015 2016 /* if it's as long as the TKIP encryption key, copy MIC key */ 2017 if (status->gtk.len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY) 2018 memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, 2019 data->tkip_mic_key, sizeof(data->tkip_mic_key)); 2020 } 2021 2022 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status, 2023 struct iwl_wowlan_gtk_status_v3 *data) 2024 { 2025 /* The parts we need are identical in v2 and v3 */ 2026 #define CHECK(_f) do { \ 2027 BUILD_BUG_ON(offsetof(struct iwl_wowlan_gtk_status_v2, _f) != \ 2028 offsetof(struct iwl_wowlan_gtk_status_v3, _f)); \ 2029 BUILD_BUG_ON(offsetofend(struct iwl_wowlan_gtk_status_v2, _f) !=\ 2030 offsetofend(struct iwl_wowlan_gtk_status_v3, _f)); \ 2031 } while (0) 2032 2033 CHECK(key); 2034 CHECK(key_len); 2035 CHECK(key_flags); 2036 CHECK(tkip_mic_key); 2037 #undef CHECK 2038 2039 iwl_mvm_convert_gtk_v2(status, (void *)data); 2040 } 2041 2042 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status, 2043 struct iwl_wowlan_igtk_status *data) 2044 { 2045 const u8 *ipn = data->ipn; 2046 2047 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key)); 2048 2049 status->igtk.len = data->key_len; 2050 status->igtk.flags = data->key_flags; 2051 2052 memcpy(status->igtk.key, data->key, sizeof(data->key)); 2053 2054 status->igtk.ipn = ((u64)ipn[5] << 0) | 2055 ((u64)ipn[4] << 8) | 2056 ((u64)ipn[3] << 16) | 2057 ((u64)ipn[2] << 24) | 2058 ((u64)ipn[1] << 32) | 2059 ((u64)ipn[0] << 40); 2060 } 2061 2062 static struct iwl_wowlan_status_data * 2063 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id) 2064 { 2065 struct iwl_wowlan_status_data *status; 2066 struct iwl_wowlan_get_status_cmd get_status_cmd = { 2067 .sta_id = cpu_to_le32(sta_id), 2068 }; 2069 struct iwl_host_cmd cmd = { 2070 .id = WOWLAN_GET_STATUSES, 2071 .flags = CMD_WANT_SKB, 2072 .data = { &get_status_cmd, }, 2073 .len = { sizeof(get_status_cmd), }, 2074 }; 2075 int ret, len; 2076 u8 notif_ver; 2077 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 2078 WOWLAN_GET_STATUSES, 2079 IWL_FW_CMD_VER_UNKNOWN); 2080 2081 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN) 2082 cmd.len[0] = 0; 2083 2084 lockdep_assert_held(&mvm->mutex); 2085 2086 ret = iwl_mvm_send_cmd(mvm, &cmd); 2087 if (ret) { 2088 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret); 2089 return ERR_PTR(ret); 2090 } 2091 2092 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 2093 2094 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */ 2095 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, 2096 WOWLAN_GET_STATUSES, 0); 2097 if (!notif_ver) 2098 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, 2099 WOWLAN_GET_STATUSES, 7); 2100 2101 if (!fw_has_api(&mvm->fw->ucode_capa, 2102 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) { 2103 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data; 2104 2105 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len); 2106 if (IS_ERR(status)) 2107 goto out_free_resp; 2108 2109 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) > 2110 sizeof(status->gtk.key)); 2111 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY + 2112 sizeof(v6->gtk.tkip_mic_key) > 2113 sizeof(status->gtk.key)); 2114 2115 /* copy GTK info to the right place */ 2116 memcpy(status->gtk.key, v6->gtk.decrypt_key, 2117 sizeof(v6->gtk.decrypt_key)); 2118 memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, 2119 v6->gtk.tkip_mic_key, 2120 sizeof(v6->gtk.tkip_mic_key)); 2121 2122 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc); 2123 2124 /* hardcode the key length to 16 since v6 only supports 16 */ 2125 status->gtk.len = 16; 2126 2127 /* 2128 * The key index only uses 2 bits (values 0 to 3) and 2129 * we always set bit 7 which means this is the 2130 * currently used key. 2131 */ 2132 status->gtk.flags = v6->gtk.key_index | BIT(7); 2133 } else if (notif_ver == 7) { 2134 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data; 2135 2136 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len); 2137 if (IS_ERR(status)) 2138 goto out_free_resp; 2139 2140 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc); 2141 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]); 2142 iwl_mvm_convert_igtk(status, &v7->igtk[0]); 2143 } else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) { 2144 struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data; 2145 2146 /* these three command versions have same layout and size, the 2147 * difference is only in a few not used (reserved) fields. 2148 */ 2149 status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len); 2150 if (IS_ERR(status)) 2151 goto out_free_resp; 2152 2153 iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc); 2154 iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]); 2155 iwl_mvm_convert_igtk(status, &v9->igtk[0]); 2156 2157 status->tid_tear_down = v9->tid_tear_down; 2158 } else if (notif_ver == 12) { 2159 struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data; 2160 2161 status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len); 2162 if (IS_ERR(status)) 2163 goto out_free_resp; 2164 2165 iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc); 2166 iwl_mvm_convert_gtk_v3(status, &v12->gtk[0]); 2167 iwl_mvm_convert_igtk(status, &v12->igtk[0]); 2168 2169 status->tid_tear_down = v12->tid_tear_down; 2170 } else { 2171 IWL_ERR(mvm, 2172 "Firmware advertises unknown WoWLAN status response %d!\n", 2173 notif_ver); 2174 status = ERR_PTR(-EIO); 2175 } 2176 2177 out_free_resp: 2178 iwl_free_resp(&cmd); 2179 return status; 2180 } 2181 2182 static struct iwl_wowlan_status_data * 2183 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, u8 sta_id) 2184 { 2185 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, 2186 OFFLOADS_QUERY_CMD, 2187 IWL_FW_CMD_VER_UNKNOWN); 2188 __le32 station_id = cpu_to_le32(sta_id); 2189 u32 cmd_size = cmd_ver != IWL_FW_CMD_VER_UNKNOWN ? sizeof(station_id) : 0; 2190 2191 if (!mvm->net_detect) { 2192 /* only for tracing for now */ 2193 int ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 2194 cmd_size, &station_id); 2195 if (ret) 2196 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret); 2197 } 2198 2199 return iwl_mvm_send_wowlan_get_status(mvm, sta_id); 2200 } 2201 2202 /* releases the MVM mutex */ 2203 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm, 2204 struct ieee80211_vif *vif) 2205 { 2206 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2207 struct iwl_wowlan_status_data *status; 2208 int i; 2209 bool keep; 2210 struct iwl_mvm_sta *mvm_ap_sta; 2211 2212 status = iwl_mvm_get_wakeup_status(mvm, mvmvif->ap_sta_id); 2213 if (IS_ERR(status)) 2214 goto out_unlock; 2215 2216 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n", 2217 status->wakeup_reasons); 2218 2219 /* still at hard-coded place 0 for D3 image */ 2220 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0); 2221 if (!mvm_ap_sta) 2222 goto out_free; 2223 2224 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 2225 u16 seq = status->qos_seq_ctr[i]; 2226 /* firmware stores last-used value, we store next value */ 2227 seq += 0x10; 2228 mvm_ap_sta->tid_data[i].seq_number = seq; 2229 } 2230 2231 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) { 2232 i = mvm->offload_tid; 2233 iwl_trans_set_q_ptrs(mvm->trans, 2234 mvm_ap_sta->tid_data[i].txq_id, 2235 mvm_ap_sta->tid_data[i].seq_number >> 4); 2236 } 2237 2238 /* now we have all the data we need, unlock to avoid mac80211 issues */ 2239 mutex_unlock(&mvm->mutex); 2240 2241 iwl_mvm_report_wakeup_reasons(mvm, vif, status); 2242 2243 keep = iwl_mvm_setup_connection_keep(mvm, vif, status); 2244 2245 kfree(status); 2246 return keep; 2247 2248 out_free: 2249 kfree(status); 2250 out_unlock: 2251 mutex_unlock(&mvm->mutex); 2252 return false; 2253 } 2254 2255 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \ 2256 IWL_SCAN_MAX_PROFILES) 2257 2258 struct iwl_mvm_nd_query_results { 2259 u32 matched_profiles; 2260 u8 matches[ND_QUERY_BUF_LEN]; 2261 }; 2262 2263 static int 2264 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm, 2265 struct iwl_mvm_nd_query_results *results) 2266 { 2267 struct iwl_scan_offload_profiles_query *query; 2268 struct iwl_host_cmd cmd = { 2269 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD, 2270 .flags = CMD_WANT_SKB, 2271 }; 2272 int ret, len; 2273 size_t query_len, matches_len; 2274 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw); 2275 2276 ret = iwl_mvm_send_cmd(mvm, &cmd); 2277 if (ret) { 2278 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret); 2279 return ret; 2280 } 2281 2282 if (fw_has_api(&mvm->fw->ucode_capa, 2283 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2284 query_len = sizeof(struct iwl_scan_offload_profiles_query); 2285 matches_len = sizeof(struct iwl_scan_offload_profile_match) * 2286 max_profiles; 2287 } else { 2288 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1); 2289 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) * 2290 max_profiles; 2291 } 2292 2293 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 2294 if (len < query_len) { 2295 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n"); 2296 ret = -EIO; 2297 goto out_free_resp; 2298 } 2299 2300 query = (void *)cmd.resp_pkt->data; 2301 2302 results->matched_profiles = le32_to_cpu(query->matched_profiles); 2303 memcpy(results->matches, query->matches, matches_len); 2304 2305 #ifdef CONFIG_IWLWIFI_DEBUGFS 2306 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done); 2307 #endif 2308 2309 out_free_resp: 2310 iwl_free_resp(&cmd); 2311 return ret; 2312 } 2313 2314 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm, 2315 struct iwl_mvm_nd_query_results *query, 2316 int idx) 2317 { 2318 int n_chans = 0, i; 2319 2320 if (fw_has_api(&mvm->fw->ucode_capa, 2321 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2322 struct iwl_scan_offload_profile_match *matches = 2323 (struct iwl_scan_offload_profile_match *)query->matches; 2324 2325 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++) 2326 n_chans += hweight8(matches[idx].matching_channels[i]); 2327 } else { 2328 struct iwl_scan_offload_profile_match_v1 *matches = 2329 (struct iwl_scan_offload_profile_match_v1 *)query->matches; 2330 2331 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++) 2332 n_chans += hweight8(matches[idx].matching_channels[i]); 2333 } 2334 2335 return n_chans; 2336 } 2337 2338 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm, 2339 struct iwl_mvm_nd_query_results *query, 2340 struct cfg80211_wowlan_nd_match *match, 2341 int idx) 2342 { 2343 int i; 2344 2345 if (fw_has_api(&mvm->fw->ucode_capa, 2346 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) { 2347 struct iwl_scan_offload_profile_match *matches = 2348 (struct iwl_scan_offload_profile_match *)query->matches; 2349 2350 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++) 2351 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8))) 2352 match->channels[match->n_channels++] = 2353 mvm->nd_channels[i]->center_freq; 2354 } else { 2355 struct iwl_scan_offload_profile_match_v1 *matches = 2356 (struct iwl_scan_offload_profile_match_v1 *)query->matches; 2357 2358 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++) 2359 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8))) 2360 match->channels[match->n_channels++] = 2361 mvm->nd_channels[i]->center_freq; 2362 } 2363 } 2364 2365 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm, 2366 struct ieee80211_vif *vif) 2367 { 2368 struct cfg80211_wowlan_nd_info *net_detect = NULL; 2369 struct cfg80211_wowlan_wakeup wakeup = { 2370 .pattern_idx = -1, 2371 }; 2372 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup; 2373 struct iwl_wowlan_status_data *status; 2374 struct iwl_mvm_nd_query_results query; 2375 unsigned long matched_profiles; 2376 u32 reasons = 0; 2377 int i, n_matches, ret; 2378 2379 status = iwl_mvm_get_wakeup_status(mvm, IWL_MVM_INVALID_STA); 2380 if (!IS_ERR(status)) { 2381 reasons = status->wakeup_reasons; 2382 kfree(status); 2383 } 2384 2385 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 2386 wakeup.rfkill_release = true; 2387 2388 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) 2389 goto out; 2390 2391 ret = iwl_mvm_netdetect_query_results(mvm, &query); 2392 if (ret || !query.matched_profiles) { 2393 wakeup_report = NULL; 2394 goto out; 2395 } 2396 2397 matched_profiles = query.matched_profiles; 2398 if (mvm->n_nd_match_sets) { 2399 n_matches = hweight_long(matched_profiles); 2400 } else { 2401 IWL_ERR(mvm, "no net detect match information available\n"); 2402 n_matches = 0; 2403 } 2404 2405 net_detect = kzalloc(struct_size(net_detect, matches, n_matches), 2406 GFP_KERNEL); 2407 if (!net_detect || !n_matches) 2408 goto out_report_nd; 2409 2410 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) { 2411 struct cfg80211_wowlan_nd_match *match; 2412 int idx, n_channels = 0; 2413 2414 n_channels = iwl_mvm_query_num_match_chans(mvm, &query, i); 2415 2416 match = kzalloc(struct_size(match, channels, n_channels), 2417 GFP_KERNEL); 2418 if (!match) 2419 goto out_report_nd; 2420 2421 net_detect->matches[net_detect->n_matches++] = match; 2422 2423 /* We inverted the order of the SSIDs in the scan 2424 * request, so invert the index here. 2425 */ 2426 idx = mvm->n_nd_match_sets - i - 1; 2427 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len; 2428 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid, 2429 match->ssid.ssid_len); 2430 2431 if (mvm->n_nd_channels < n_channels) 2432 continue; 2433 2434 iwl_mvm_query_set_freqs(mvm, &query, match, i); 2435 } 2436 2437 out_report_nd: 2438 wakeup.net_detect = net_detect; 2439 out: 2440 iwl_mvm_free_nd(mvm); 2441 2442 mutex_unlock(&mvm->mutex); 2443 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL); 2444 2445 if (net_detect) { 2446 for (i = 0; i < net_detect->n_matches; i++) 2447 kfree(net_detect->matches[i]); 2448 kfree(net_detect); 2449 } 2450 } 2451 2452 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac, 2453 struct ieee80211_vif *vif) 2454 { 2455 /* skip the one we keep connection on */ 2456 if (data == vif) 2457 return; 2458 2459 if (vif->type == NL80211_IFTYPE_STATION) 2460 ieee80211_resume_disconnect(vif); 2461 } 2462 2463 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id) 2464 { 2465 struct error_table_start { 2466 /* cf. struct iwl_error_event_table */ 2467 u32 valid; 2468 __le32 err_id; 2469 } err_info; 2470 2471 if (!base) 2472 return false; 2473 2474 iwl_trans_read_mem_bytes(trans, base, 2475 &err_info, sizeof(err_info)); 2476 if (err_info.valid && err_id) 2477 *err_id = le32_to_cpu(err_info.err_id); 2478 2479 return !!err_info.valid; 2480 } 2481 2482 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm, 2483 struct ieee80211_vif *vif) 2484 { 2485 u32 err_id; 2486 2487 /* check for lmac1 error */ 2488 if (iwl_mvm_rt_status(mvm->trans, 2489 mvm->trans->dbg.lmac_error_event_table[0], 2490 &err_id)) { 2491 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) { 2492 struct cfg80211_wowlan_wakeup wakeup = { 2493 .rfkill_release = true, 2494 }; 2495 ieee80211_report_wowlan_wakeup(vif, &wakeup, 2496 GFP_KERNEL); 2497 } 2498 return true; 2499 } 2500 2501 /* check if we have lmac2 set and check for error */ 2502 if (iwl_mvm_rt_status(mvm->trans, 2503 mvm->trans->dbg.lmac_error_event_table[1], NULL)) 2504 return true; 2505 2506 /* check for umac error */ 2507 if (iwl_mvm_rt_status(mvm->trans, 2508 mvm->trans->dbg.umac_error_event_table, NULL)) 2509 return true; 2510 2511 return false; 2512 } 2513 2514 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test) 2515 { 2516 struct ieee80211_vif *vif = NULL; 2517 int ret = 1; 2518 enum iwl_d3_status d3_status; 2519 bool keep = false; 2520 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 2521 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 2522 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa, 2523 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST); 2524 2525 mutex_lock(&mvm->mutex); 2526 2527 mvm->last_reset_or_resume_time_jiffies = jiffies; 2528 2529 /* get the BSS vif pointer again */ 2530 vif = iwl_mvm_get_bss_vif(mvm); 2531 if (IS_ERR_OR_NULL(vif)) 2532 goto err; 2533 2534 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt); 2535 2536 if (iwl_mvm_check_rt_status(mvm, vif)) { 2537 set_bit(STATUS_FW_ERROR, &mvm->trans->status); 2538 iwl_mvm_dump_nic_error_log(mvm); 2539 iwl_dbg_tlv_time_point(&mvm->fwrt, 2540 IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL); 2541 iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert, 2542 false, 0); 2543 ret = 1; 2544 goto err; 2545 } 2546 2547 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image); 2548 if (ret) 2549 goto err; 2550 2551 if (d3_status != IWL_D3_STATUS_ALIVE) { 2552 IWL_INFO(mvm, "Device was reset during suspend\n"); 2553 goto err; 2554 } 2555 2556 if (d0i3_first) { 2557 struct iwl_host_cmd cmd = { 2558 .id = D0I3_END_CMD, 2559 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3, 2560 }; 2561 int len; 2562 2563 ret = iwl_mvm_send_cmd(mvm, &cmd); 2564 if (ret < 0) { 2565 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n", 2566 ret); 2567 goto err; 2568 } 2569 switch (mvm->cmd_ver.d0i3_resp) { 2570 case 0: 2571 break; 2572 case 1: 2573 len = iwl_rx_packet_payload_len(cmd.resp_pkt); 2574 if (len != sizeof(u32)) { 2575 IWL_ERR(mvm, 2576 "Error with D0I3_END_CMD response size (%d)\n", 2577 len); 2578 goto err; 2579 } 2580 if (IWL_D0I3_RESET_REQUIRE & 2581 le32_to_cpu(*(__le32 *)cmd.resp_pkt->data)) { 2582 iwl_write32(mvm->trans, CSR_RESET, 2583 CSR_RESET_REG_FLAG_FORCE_NMI); 2584 iwl_free_resp(&cmd); 2585 } 2586 break; 2587 default: 2588 WARN_ON(1); 2589 } 2590 } 2591 2592 /* after the successful handshake, we're out of D3 */ 2593 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED; 2594 2595 /* 2596 * Query the current location and source from the D3 firmware so we 2597 * can play it back when we re-intiailize the D0 firmware 2598 */ 2599 iwl_mvm_update_changed_regdom(mvm); 2600 2601 /* Re-configure PPAG settings */ 2602 iwl_mvm_ppag_send_cmd(mvm); 2603 2604 if (!unified_image) 2605 /* Re-configure default SAR profile */ 2606 iwl_mvm_sar_select_profile(mvm, 1, 1); 2607 2608 if (mvm->net_detect) { 2609 /* If this is a non-unified image, we restart the FW, 2610 * so no need to stop the netdetect scan. If that 2611 * fails, continue and try to get the wake-up reasons, 2612 * but trigger a HW restart by keeping a failure code 2613 * in ret. 2614 */ 2615 if (unified_image) 2616 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT, 2617 false); 2618 2619 iwl_mvm_query_netdetect_reasons(mvm, vif); 2620 /* has unlocked the mutex, so skip that */ 2621 goto out; 2622 } else { 2623 keep = iwl_mvm_query_wakeup_reasons(mvm, vif); 2624 #ifdef CONFIG_IWLWIFI_DEBUGFS 2625 if (keep) 2626 mvm->keep_vif = vif; 2627 #endif 2628 /* has unlocked the mutex, so skip that */ 2629 goto out_iterate; 2630 } 2631 2632 err: 2633 iwl_mvm_free_nd(mvm); 2634 mutex_unlock(&mvm->mutex); 2635 2636 out_iterate: 2637 if (!test) 2638 ieee80211_iterate_active_interfaces_mtx(mvm->hw, 2639 IEEE80211_IFACE_ITER_NORMAL, 2640 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL); 2641 2642 out: 2643 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status); 2644 2645 /* no need to reset the device in unified images, if successful */ 2646 if (unified_image && !ret) { 2647 /* nothing else to do if we already sent D0I3_END_CMD */ 2648 if (d0i3_first) 2649 return 0; 2650 2651 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL); 2652 if (!ret) 2653 return 0; 2654 } 2655 2656 /* 2657 * Reconfigure the device in one of the following cases: 2658 * 1. We are not using a unified image 2659 * 2. We are using a unified image but had an error while exiting D3 2660 */ 2661 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status); 2662 2663 /* regardless of what happened, we're now out of D3 */ 2664 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED; 2665 2666 return 1; 2667 } 2668 2669 int iwl_mvm_resume(struct ieee80211_hw *hw) 2670 { 2671 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 2672 int ret; 2673 2674 ret = __iwl_mvm_resume(mvm, false); 2675 2676 iwl_mvm_resume_tcm(mvm); 2677 2678 iwl_fw_runtime_resume(&mvm->fwrt); 2679 2680 return ret; 2681 } 2682 2683 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled) 2684 { 2685 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw); 2686 2687 device_set_wakeup_enable(mvm->trans->dev, enabled); 2688 } 2689 2690 #ifdef CONFIG_IWLWIFI_DEBUGFS 2691 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file) 2692 { 2693 struct iwl_mvm *mvm = inode->i_private; 2694 int err; 2695 2696 if (mvm->d3_test_active) 2697 return -EBUSY; 2698 2699 file->private_data = inode->i_private; 2700 2701 iwl_mvm_pause_tcm(mvm, true); 2702 2703 iwl_fw_runtime_suspend(&mvm->fwrt); 2704 2705 /* start pseudo D3 */ 2706 rtnl_lock(); 2707 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true); 2708 rtnl_unlock(); 2709 if (err > 0) 2710 err = -EINVAL; 2711 if (err) 2712 return err; 2713 2714 mvm->d3_test_active = true; 2715 mvm->keep_vif = NULL; 2716 return 0; 2717 } 2718 2719 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf, 2720 size_t count, loff_t *ppos) 2721 { 2722 struct iwl_mvm *mvm = file->private_data; 2723 u32 pme_asserted; 2724 2725 while (true) { 2726 /* read pme_ptr if available */ 2727 if (mvm->d3_test_pme_ptr) { 2728 pme_asserted = iwl_trans_read_mem32(mvm->trans, 2729 mvm->d3_test_pme_ptr); 2730 if (pme_asserted) 2731 break; 2732 } 2733 2734 if (msleep_interruptible(100)) 2735 break; 2736 } 2737 2738 return 0; 2739 } 2740 2741 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac, 2742 struct ieee80211_vif *vif) 2743 { 2744 /* skip the one we keep connection on */ 2745 if (_data == vif) 2746 return; 2747 2748 if (vif->type == NL80211_IFTYPE_STATION) 2749 ieee80211_connection_loss(vif); 2750 } 2751 2752 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file) 2753 { 2754 struct iwl_mvm *mvm = inode->i_private; 2755 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa, 2756 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG); 2757 2758 mvm->d3_test_active = false; 2759 2760 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt); 2761 2762 rtnl_lock(); 2763 __iwl_mvm_resume(mvm, true); 2764 rtnl_unlock(); 2765 2766 iwl_mvm_resume_tcm(mvm); 2767 2768 iwl_fw_runtime_resume(&mvm->fwrt); 2769 2770 iwl_abort_notification_waits(&mvm->notif_wait); 2771 if (!unified_image) { 2772 int remaining_time = 10; 2773 2774 ieee80211_restart_hw(mvm->hw); 2775 2776 /* wait for restart and disconnect all interfaces */ 2777 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) && 2778 remaining_time > 0) { 2779 remaining_time--; 2780 msleep(1000); 2781 } 2782 2783 if (remaining_time == 0) 2784 IWL_ERR(mvm, "Timed out waiting for HW restart!\n"); 2785 } 2786 2787 ieee80211_iterate_active_interfaces_atomic( 2788 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 2789 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif); 2790 2791 return 0; 2792 } 2793 2794 const struct file_operations iwl_dbgfs_d3_test_ops = { 2795 .llseek = no_llseek, 2796 .open = iwl_mvm_d3_test_open, 2797 .read = iwl_mvm_d3_test_read, 2798 .release = iwl_mvm_d3_test_release, 2799 }; 2800 #endif 2801