1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2015-2017 Intel Deutschland GmbH 4 * Copyright (C) 2018-2025 Intel Corporation 5 */ 6 #include <linux/etherdevice.h> 7 #include <linux/math64.h> 8 #include <net/cfg80211.h> 9 #include "mvm.h" 10 #include "iwl-io.h" 11 #include "iwl-prph.h" 12 #include "constants.h" 13 14 struct iwl_mvm_loc_entry { 15 struct list_head list; 16 u8 addr[ETH_ALEN]; 17 u8 lci_len, civic_len; 18 u8 buf[]; 19 }; 20 21 struct iwl_mvm_smooth_entry { 22 struct list_head list; 23 u8 addr[ETH_ALEN]; 24 s64 rtt_avg; 25 u64 host_time; 26 }; 27 28 enum iwl_mvm_pasn_flags { 29 IWL_MVM_PASN_FLAG_HAS_HLTK = BIT(0), 30 }; 31 32 struct iwl_mvm_ftm_pasn_entry { 33 struct list_head list; 34 u8 addr[ETH_ALEN]; 35 u8 hltk[HLTK_11AZ_LEN]; 36 u8 tk[TK_11AZ_LEN]; 37 u8 cipher; 38 u8 tx_pn[IEEE80211_CCMP_PN_LEN]; 39 u8 rx_pn[IEEE80211_CCMP_PN_LEN]; 40 u32 flags; 41 }; 42 43 struct iwl_mvm_ftm_iter_data { 44 u8 *cipher; 45 u8 *bssid; 46 u8 *tk; 47 }; 48 49 static void iwl_mvm_ftm_reset(struct iwl_mvm *mvm) 50 { 51 struct iwl_mvm_loc_entry *e, *t; 52 53 mvm->ftm_initiator.req = NULL; 54 mvm->ftm_initiator.req_wdev = NULL; 55 memset(mvm->ftm_initiator.responses, 0, 56 sizeof(mvm->ftm_initiator.responses)); 57 58 list_for_each_entry_safe(e, t, &mvm->ftm_initiator.loc_list, list) { 59 list_del(&e->list); 60 kfree(e); 61 } 62 } 63 64 void iwl_mvm_ftm_restart(struct iwl_mvm *mvm) 65 { 66 struct cfg80211_pmsr_result result = { 67 .status = NL80211_PMSR_STATUS_FAILURE, 68 .final = 1, 69 .host_time = ktime_get_boottime_ns(), 70 .type = NL80211_PMSR_TYPE_FTM, 71 }; 72 int i; 73 74 lockdep_assert_held(&mvm->mutex); 75 76 if (!mvm->ftm_initiator.req) 77 return; 78 79 for (i = 0; i < mvm->ftm_initiator.req->n_peers; i++) { 80 memcpy(result.addr, mvm->ftm_initiator.req->peers[i].addr, 81 ETH_ALEN); 82 result.ftm.burst_index = mvm->ftm_initiator.responses[i]; 83 84 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev, 85 mvm->ftm_initiator.req, 86 &result, GFP_KERNEL); 87 } 88 89 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev, 90 mvm->ftm_initiator.req, GFP_KERNEL); 91 iwl_mvm_ftm_reset(mvm); 92 } 93 94 void iwl_mvm_ftm_initiator_smooth_config(struct iwl_mvm *mvm) 95 { 96 INIT_LIST_HEAD(&mvm->ftm_initiator.smooth.resp); 97 98 IWL_DEBUG_INFO(mvm, 99 "enable=%u, alpha=%u, age_jiffies=%u, thresh=(%u:%u)\n", 100 IWL_MVM_FTM_INITIATOR_ENABLE_SMOOTH, 101 IWL_MVM_FTM_INITIATOR_SMOOTH_ALPHA, 102 IWL_MVM_FTM_INITIATOR_SMOOTH_AGE_SEC * HZ, 103 IWL_MVM_FTM_INITIATOR_SMOOTH_OVERSHOOT, 104 IWL_MVM_FTM_INITIATOR_SMOOTH_UNDERSHOOT); 105 } 106 107 void iwl_mvm_ftm_initiator_smooth_stop(struct iwl_mvm *mvm) 108 { 109 struct iwl_mvm_smooth_entry *se, *st; 110 111 list_for_each_entry_safe(se, st, &mvm->ftm_initiator.smooth.resp, 112 list) { 113 list_del(&se->list); 114 kfree(se); 115 } 116 } 117 118 static int 119 iwl_ftm_range_request_status_to_err(enum iwl_tof_range_request_status s) 120 { 121 switch (s) { 122 case IWL_TOF_RANGE_REQUEST_STATUS_SUCCESS: 123 return 0; 124 case IWL_TOF_RANGE_REQUEST_STATUS_BUSY: 125 return -EBUSY; 126 default: 127 WARN_ON_ONCE(1); 128 return -EIO; 129 } 130 } 131 132 static void iwl_mvm_ftm_cmd_v5(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 133 struct iwl_tof_range_req_cmd_v5 *cmd, 134 struct cfg80211_pmsr_request *req) 135 { 136 int i; 137 138 cmd->request_id = req->cookie; 139 cmd->num_of_ap = req->n_peers; 140 141 /* use maximum for "no timeout" or bigger than what we can do */ 142 if (!req->timeout || req->timeout > 255 * 100) 143 cmd->req_timeout = 255; 144 else 145 cmd->req_timeout = DIV_ROUND_UP(req->timeout, 100); 146 147 /* 148 * We treat it always as random, since if not we'll 149 * have filled our local address there instead. 150 */ 151 cmd->macaddr_random = 1; 152 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN); 153 for (i = 0; i < ETH_ALEN; i++) 154 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i]; 155 156 if (vif->cfg.assoc) 157 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN); 158 else 159 eth_broadcast_addr(cmd->range_req_bssid); 160 } 161 162 static void iwl_mvm_ftm_cmd_common(struct iwl_mvm *mvm, 163 struct ieee80211_vif *vif, 164 struct iwl_tof_range_req_cmd_v9 *cmd, 165 struct cfg80211_pmsr_request *req) 166 { 167 int i; 168 169 cmd->initiator_flags = 170 cpu_to_le32(IWL_TOF_INITIATOR_FLAGS_MACADDR_RANDOM | 171 IWL_TOF_INITIATOR_FLAGS_NON_ASAP_SUPPORT); 172 cmd->request_id = req->cookie; 173 cmd->num_of_ap = req->n_peers; 174 175 /* 176 * Use a large value for "no timeout". Don't use the maximum value 177 * because of fw limitations. 178 */ 179 if (req->timeout) 180 cmd->req_timeout_ms = cpu_to_le32(req->timeout); 181 else 182 cmd->req_timeout_ms = cpu_to_le32(0xfffff); 183 184 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN); 185 for (i = 0; i < ETH_ALEN; i++) 186 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i]; 187 188 if (vif->cfg.assoc) { 189 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN); 190 191 /* AP's TSF is only relevant if associated */ 192 for (i = 0; i < req->n_peers; i++) { 193 if (req->peers[i].report_ap_tsf) { 194 struct iwl_mvm_vif *mvmvif = 195 iwl_mvm_vif_from_mac80211(vif); 196 197 cmd->tsf_mac_id = cpu_to_le32(mvmvif->id); 198 return; 199 } 200 } 201 } else { 202 eth_broadcast_addr(cmd->range_req_bssid); 203 } 204 205 /* Don't report AP's TSF */ 206 cmd->tsf_mac_id = cpu_to_le32(0xff); 207 } 208 209 static void iwl_mvm_ftm_cmd_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 210 struct iwl_tof_range_req_cmd_v8 *cmd, 211 struct cfg80211_pmsr_request *req) 212 { 213 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)cmd, req); 214 } 215 216 static int 217 iwl_mvm_ftm_target_chandef_v1(struct iwl_mvm *mvm, 218 struct cfg80211_pmsr_request_peer *peer, 219 u8 *channel, u8 *bandwidth, 220 u8 *ctrl_ch_position) 221 { 222 u32 freq = peer->chandef.chan->center_freq; 223 224 *channel = ieee80211_frequency_to_channel(freq); 225 226 switch (peer->chandef.width) { 227 case NL80211_CHAN_WIDTH_20_NOHT: 228 *bandwidth = IWL_TOF_BW_20_LEGACY; 229 break; 230 case NL80211_CHAN_WIDTH_20: 231 *bandwidth = IWL_TOF_BW_20_HT; 232 break; 233 case NL80211_CHAN_WIDTH_40: 234 *bandwidth = IWL_TOF_BW_40; 235 break; 236 case NL80211_CHAN_WIDTH_80: 237 *bandwidth = IWL_TOF_BW_80; 238 break; 239 default: 240 IWL_ERR(mvm, "Unsupported BW in FTM request (%d)\n", 241 peer->chandef.width); 242 return -EINVAL; 243 } 244 245 *ctrl_ch_position = (peer->chandef.width > NL80211_CHAN_WIDTH_20) ? 246 iwl_mvm_get_ctrl_pos(&peer->chandef) : 0; 247 248 return 0; 249 } 250 251 static int 252 iwl_mvm_ftm_target_chandef_v2(struct iwl_mvm *mvm, 253 struct cfg80211_pmsr_request_peer *peer, 254 u8 *channel, u8 *format_bw, 255 u8 *ctrl_ch_position) 256 { 257 u32 freq = peer->chandef.chan->center_freq; 258 u8 cmd_ver; 259 260 *channel = ieee80211_frequency_to_channel(freq); 261 262 switch (peer->chandef.width) { 263 case NL80211_CHAN_WIDTH_20_NOHT: 264 *format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY; 265 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS; 266 break; 267 case NL80211_CHAN_WIDTH_20: 268 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT; 269 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS; 270 break; 271 case NL80211_CHAN_WIDTH_40: 272 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT; 273 *format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS; 274 break; 275 case NL80211_CHAN_WIDTH_80: 276 *format_bw = IWL_LOCATION_FRAME_FORMAT_VHT; 277 *format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS; 278 break; 279 case NL80211_CHAN_WIDTH_160: 280 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 281 WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 282 IWL_FW_CMD_VER_UNKNOWN); 283 284 if (cmd_ver >= 13) { 285 *format_bw = IWL_LOCATION_FRAME_FORMAT_HE; 286 *format_bw |= IWL_LOCATION_BW_160MHZ << LOCATION_BW_POS; 287 break; 288 } 289 fallthrough; 290 default: 291 IWL_ERR(mvm, "Unsupported BW in FTM request (%d)\n", 292 peer->chandef.width); 293 return -EINVAL; 294 } 295 296 /* non EDCA based measurement must use HE preamble */ 297 if (peer->ftm.trigger_based || peer->ftm.non_trigger_based) 298 *format_bw |= IWL_LOCATION_FRAME_FORMAT_HE; 299 300 *ctrl_ch_position = (peer->chandef.width > NL80211_CHAN_WIDTH_20) ? 301 iwl_mvm_get_ctrl_pos(&peer->chandef) : 0; 302 303 return 0; 304 } 305 306 static int 307 iwl_mvm_ftm_put_target_v2(struct iwl_mvm *mvm, 308 struct cfg80211_pmsr_request_peer *peer, 309 struct iwl_tof_range_req_ap_entry_v2 *target) 310 { 311 int ret; 312 313 ret = iwl_mvm_ftm_target_chandef_v1(mvm, peer, &target->channel_num, 314 &target->bandwidth, 315 &target->ctrl_ch_position); 316 if (ret) 317 return ret; 318 319 memcpy(target->bssid, peer->addr, ETH_ALEN); 320 target->burst_period = 321 cpu_to_le16(peer->ftm.burst_period); 322 target->samples_per_burst = peer->ftm.ftms_per_burst; 323 target->num_of_bursts = peer->ftm.num_bursts_exp; 324 target->measure_type = 0; /* regular two-sided FTM */ 325 target->retries_per_sample = peer->ftm.ftmr_retries; 326 target->asap_mode = peer->ftm.asap; 327 target->enable_dyn_ack = IWL_MVM_FTM_INITIATOR_DYNACK; 328 329 if (peer->ftm.request_lci) 330 target->location_req |= IWL_TOF_LOC_LCI; 331 if (peer->ftm.request_civicloc) 332 target->location_req |= IWL_TOF_LOC_CIVIC; 333 334 target->algo_type = IWL_MVM_FTM_INITIATOR_ALGO; 335 336 return 0; 337 } 338 339 #define FTM_SET_FLAG(flag) (*flags |= \ 340 cpu_to_le32(IWL_INITIATOR_AP_FLAGS_##flag)) 341 342 static void 343 iwl_mvm_ftm_set_target_flags(struct iwl_mvm *mvm, 344 struct cfg80211_pmsr_request_peer *peer, 345 __le32 *flags) 346 { 347 *flags = cpu_to_le32(0); 348 349 if (peer->ftm.asap) 350 FTM_SET_FLAG(ASAP); 351 352 if (peer->ftm.request_lci) 353 FTM_SET_FLAG(LCI_REQUEST); 354 355 if (peer->ftm.request_civicloc) 356 FTM_SET_FLAG(CIVIC_REQUEST); 357 358 if (IWL_MVM_FTM_INITIATOR_DYNACK) 359 FTM_SET_FLAG(DYN_ACK); 360 361 if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_LINEAR_REG) 362 FTM_SET_FLAG(ALGO_LR); 363 else if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_FFT) 364 FTM_SET_FLAG(ALGO_FFT); 365 366 if (peer->ftm.trigger_based) 367 FTM_SET_FLAG(TB); 368 else if (peer->ftm.non_trigger_based) 369 FTM_SET_FLAG(NON_TB); 370 371 if ((peer->ftm.trigger_based || peer->ftm.non_trigger_based) && 372 peer->ftm.lmr_feedback) 373 FTM_SET_FLAG(LMR_FEEDBACK); 374 } 375 376 static void 377 iwl_mvm_ftm_put_target_common(struct iwl_mvm *mvm, 378 struct cfg80211_pmsr_request_peer *peer, 379 struct iwl_tof_range_req_ap_entry_v6 *target) 380 { 381 memcpy(target->bssid, peer->addr, ETH_ALEN); 382 target->burst_period = 383 cpu_to_le16(peer->ftm.burst_period); 384 target->samples_per_burst = peer->ftm.ftms_per_burst; 385 target->num_of_bursts = peer->ftm.num_bursts_exp; 386 target->ftmr_max_retries = peer->ftm.ftmr_retries; 387 iwl_mvm_ftm_set_target_flags(mvm, peer, &target->initiator_ap_flags); 388 } 389 390 static int 391 iwl_mvm_ftm_put_target_v3(struct iwl_mvm *mvm, 392 struct cfg80211_pmsr_request_peer *peer, 393 struct iwl_tof_range_req_ap_entry_v3 *target) 394 { 395 int ret; 396 397 ret = iwl_mvm_ftm_target_chandef_v1(mvm, peer, &target->channel_num, 398 &target->bandwidth, 399 &target->ctrl_ch_position); 400 if (ret) 401 return ret; 402 403 /* 404 * Versions 3 and 4 has some common fields, so 405 * iwl_mvm_ftm_put_target_common() can be used for version 7 too. 406 */ 407 iwl_mvm_ftm_put_target_common(mvm, peer, (void *)target); 408 409 return 0; 410 } 411 412 static int 413 iwl_mvm_ftm_put_target_v4(struct iwl_mvm *mvm, 414 struct cfg80211_pmsr_request_peer *peer, 415 struct iwl_tof_range_req_ap_entry_v4 *target) 416 { 417 int ret; 418 419 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 420 &target->format_bw, 421 &target->ctrl_ch_position); 422 if (ret) 423 return ret; 424 425 iwl_mvm_ftm_put_target_common(mvm, peer, (void *)target); 426 427 return 0; 428 } 429 430 static int iwl_mvm_ftm_set_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 431 struct cfg80211_pmsr_request_peer *peer, 432 u8 *sta_id, __le32 *flags) 433 { 434 if (vif->cfg.assoc) { 435 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 436 struct ieee80211_sta *sta; 437 struct ieee80211_bss_conf *link_conf; 438 unsigned int link_id; 439 440 rcu_read_lock(); 441 for_each_vif_active_link(vif, link_conf, link_id) { 442 if (memcmp(peer->addr, link_conf->bssid, ETH_ALEN)) 443 continue; 444 445 *sta_id = mvmvif->link[link_id]->ap_sta_id; 446 sta = rcu_dereference(mvm->fw_id_to_mac_id[*sta_id]); 447 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) { 448 rcu_read_unlock(); 449 return PTR_ERR_OR_ZERO(sta); 450 } 451 452 if (sta->mfp && (peer->ftm.trigger_based || 453 peer->ftm.non_trigger_based)) 454 FTM_SET_FLAG(PMF); 455 break; 456 } 457 rcu_read_unlock(); 458 459 #ifdef CONFIG_IWLWIFI_DEBUGFS 460 if (mvmvif->ftm_unprotected) { 461 *sta_id = IWL_INVALID_STA; 462 *flags &= ~cpu_to_le32(IWL_INITIATOR_AP_FLAGS_PMF); 463 } 464 #endif 465 } else { 466 *sta_id = IWL_INVALID_STA; 467 } 468 469 return 0; 470 } 471 472 static int 473 iwl_mvm_ftm_put_target(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 474 struct cfg80211_pmsr_request_peer *peer, 475 struct iwl_tof_range_req_ap_entry_v6 *target) 476 { 477 int ret; 478 479 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 480 &target->format_bw, 481 &target->ctrl_ch_position); 482 if (ret) 483 return ret; 484 485 iwl_mvm_ftm_put_target_common(mvm, peer, target); 486 487 iwl_mvm_ftm_set_sta(mvm, vif, peer, &target->sta_id, 488 &target->initiator_ap_flags); 489 490 /* 491 * TODO: Beacon interval is currently unknown, so use the common value 492 * of 100 TUs. 493 */ 494 target->beacon_interval = cpu_to_le16(100); 495 return 0; 496 } 497 498 static int iwl_mvm_ftm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *hcmd) 499 { 500 u32 status; 501 int err = iwl_mvm_send_cmd_status(mvm, hcmd, &status); 502 503 if (!err && status) { 504 IWL_ERR(mvm, "FTM range request command failure, status: %u\n", 505 status); 506 err = iwl_ftm_range_request_status_to_err(status); 507 } 508 509 return err; 510 } 511 512 static int iwl_mvm_ftm_start_v5(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 513 struct cfg80211_pmsr_request *req) 514 { 515 struct iwl_tof_range_req_cmd_v5 cmd_v5; 516 struct iwl_host_cmd hcmd = { 517 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 518 .dataflags[0] = IWL_HCMD_DFL_DUP, 519 .data[0] = &cmd_v5, 520 .len[0] = sizeof(cmd_v5), 521 }; 522 u8 i; 523 int err; 524 525 iwl_mvm_ftm_cmd_v5(mvm, vif, &cmd_v5, req); 526 527 for (i = 0; i < cmd_v5.num_of_ap; i++) { 528 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 529 530 err = iwl_mvm_ftm_put_target_v2(mvm, peer, &cmd_v5.ap[i]); 531 if (err) 532 return err; 533 } 534 535 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 536 } 537 538 static int iwl_mvm_ftm_start_v7(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 539 struct cfg80211_pmsr_request *req) 540 { 541 struct iwl_tof_range_req_cmd_v7 cmd_v7; 542 struct iwl_host_cmd hcmd = { 543 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 544 .dataflags[0] = IWL_HCMD_DFL_DUP, 545 .data[0] = &cmd_v7, 546 .len[0] = sizeof(cmd_v7), 547 }; 548 u8 i; 549 int err; 550 551 /* 552 * Versions 7 and 8 has the same structure except from the responders 553 * list, so iwl_mvm_ftm_cmd() can be used for version 7 too. 554 */ 555 iwl_mvm_ftm_cmd_v8(mvm, vif, (void *)&cmd_v7, req); 556 557 for (i = 0; i < cmd_v7.num_of_ap; i++) { 558 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 559 560 err = iwl_mvm_ftm_put_target_v3(mvm, peer, &cmd_v7.ap[i]); 561 if (err) 562 return err; 563 } 564 565 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 566 } 567 568 static int iwl_mvm_ftm_start_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 569 struct cfg80211_pmsr_request *req) 570 { 571 struct iwl_tof_range_req_cmd_v8 cmd; 572 struct iwl_host_cmd hcmd = { 573 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 574 .dataflags[0] = IWL_HCMD_DFL_DUP, 575 .data[0] = &cmd, 576 .len[0] = sizeof(cmd), 577 }; 578 u8 i; 579 int err; 580 581 iwl_mvm_ftm_cmd_v8(mvm, vif, (void *)&cmd, req); 582 583 for (i = 0; i < cmd.num_of_ap; i++) { 584 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 585 586 err = iwl_mvm_ftm_put_target_v4(mvm, peer, &cmd.ap[i]); 587 if (err) 588 return err; 589 } 590 591 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 592 } 593 594 static int iwl_mvm_ftm_start_v9(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 595 struct cfg80211_pmsr_request *req) 596 { 597 struct iwl_tof_range_req_cmd_v9 cmd; 598 struct iwl_host_cmd hcmd = { 599 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 600 .dataflags[0] = IWL_HCMD_DFL_DUP, 601 .data[0] = &cmd, 602 .len[0] = sizeof(cmd), 603 }; 604 u8 i; 605 int err; 606 607 iwl_mvm_ftm_cmd_common(mvm, vif, &cmd, req); 608 609 for (i = 0; i < cmd.num_of_ap; i++) { 610 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 611 struct iwl_tof_range_req_ap_entry_v6 *target = &cmd.ap[i]; 612 613 err = iwl_mvm_ftm_put_target(mvm, vif, peer, target); 614 if (err) 615 return err; 616 } 617 618 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 619 } 620 621 static void iter(struct ieee80211_hw *hw, 622 struct ieee80211_vif *vif, 623 struct ieee80211_sta *sta, 624 struct ieee80211_key_conf *key, 625 void *data) 626 { 627 struct iwl_mvm_ftm_iter_data *target = data; 628 629 if (!sta || memcmp(sta->addr, target->bssid, ETH_ALEN)) 630 return; 631 632 WARN_ON(!sta->mfp); 633 634 target->tk = key->key; 635 *target->cipher = iwl_mvm_cipher_to_location_cipher(key->cipher); 636 WARN_ON(*target->cipher == IWL_LOCATION_CIPHER_INVALID); 637 } 638 639 static void 640 iwl_mvm_ftm_set_secured_ranging(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 641 u8 *bssid, u8 *cipher, u8 *hltk, u8 *tk, 642 u8 *rx_pn, u8 *tx_pn, __le32 *flags) 643 { 644 struct iwl_mvm_ftm_pasn_entry *entry; 645 #ifdef CONFIG_IWLWIFI_DEBUGFS 646 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 647 648 if (mvmvif->ftm_unprotected) 649 return; 650 #endif 651 652 if (!(le32_to_cpu(*flags) & (IWL_INITIATOR_AP_FLAGS_NON_TB | 653 IWL_INITIATOR_AP_FLAGS_TB))) 654 return; 655 656 lockdep_assert_held(&mvm->mutex); 657 658 list_for_each_entry(entry, &mvm->ftm_initiator.pasn_list, list) { 659 if (memcmp(entry->addr, bssid, sizeof(entry->addr))) 660 continue; 661 662 *cipher = entry->cipher; 663 664 if (entry->flags & IWL_MVM_PASN_FLAG_HAS_HLTK) 665 memcpy(hltk, entry->hltk, sizeof(entry->hltk)); 666 else 667 memset(hltk, 0, sizeof(entry->hltk)); 668 669 if (vif->cfg.assoc && 670 !memcmp(vif->bss_conf.bssid, bssid, ETH_ALEN)) { 671 struct iwl_mvm_ftm_iter_data target; 672 673 target.bssid = bssid; 674 target.cipher = cipher; 675 ieee80211_iter_keys(mvm->hw, vif, iter, &target); 676 } else { 677 memcpy(tk, entry->tk, sizeof(entry->tk)); 678 } 679 680 memcpy(rx_pn, entry->rx_pn, sizeof(entry->rx_pn)); 681 memcpy(tx_pn, entry->tx_pn, sizeof(entry->tx_pn)); 682 683 FTM_SET_FLAG(SECURED); 684 return; 685 } 686 } 687 688 static int 689 iwl_mvm_ftm_put_target_v7(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 690 struct cfg80211_pmsr_request_peer *peer, 691 struct iwl_tof_range_req_ap_entry_v7 *target) 692 { 693 int err = iwl_mvm_ftm_put_target(mvm, vif, peer, (void *)target); 694 if (err) 695 return err; 696 697 iwl_mvm_ftm_set_secured_ranging(mvm, vif, target->bssid, 698 &target->cipher, target->hltk, 699 target->tk, target->rx_pn, 700 target->tx_pn, 701 &target->initiator_ap_flags); 702 return err; 703 } 704 705 static int iwl_mvm_ftm_start_v11(struct iwl_mvm *mvm, 706 struct ieee80211_vif *vif, 707 struct cfg80211_pmsr_request *req) 708 { 709 struct iwl_tof_range_req_cmd_v11 cmd; 710 struct iwl_host_cmd hcmd = { 711 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 712 .dataflags[0] = IWL_HCMD_DFL_DUP, 713 .data[0] = &cmd, 714 .len[0] = sizeof(cmd), 715 }; 716 u8 i; 717 int err; 718 719 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 720 721 for (i = 0; i < cmd.num_of_ap; i++) { 722 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 723 struct iwl_tof_range_req_ap_entry_v7 *target = &cmd.ap[i]; 724 725 err = iwl_mvm_ftm_put_target_v7(mvm, vif, peer, target); 726 if (err) 727 return err; 728 } 729 730 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 731 } 732 733 static void 734 iwl_mvm_ftm_set_ndp_params(struct iwl_mvm *mvm, 735 struct iwl_tof_range_req_ap_entry_v8 *target) 736 { 737 /* Only 2 STS are supported on Tx */ 738 u32 i2r_max_sts = IWL_MVM_FTM_I2R_MAX_STS > 1 ? 1 : 739 IWL_MVM_FTM_I2R_MAX_STS; 740 741 target->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP | 742 (IWL_MVM_FTM_R2I_MAX_STS << IWL_LOCATION_MAX_STS_POS); 743 target->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP | 744 (i2r_max_sts << IWL_LOCATION_MAX_STS_POS); 745 target->r2i_max_total_ltf = IWL_MVM_FTM_R2I_MAX_TOTAL_LTF; 746 target->i2r_max_total_ltf = IWL_MVM_FTM_I2R_MAX_TOTAL_LTF; 747 } 748 749 static int 750 iwl_mvm_ftm_put_target_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 751 struct cfg80211_pmsr_request_peer *peer, 752 struct iwl_tof_range_req_ap_entry_v8 *target) 753 { 754 u32 flags; 755 int ret = iwl_mvm_ftm_put_target_v7(mvm, vif, peer, (void *)target); 756 757 if (ret) 758 return ret; 759 760 iwl_mvm_ftm_set_ndp_params(mvm, target); 761 762 /* 763 * If secure LTF is turned off, replace the flag with PMF only 764 */ 765 flags = le32_to_cpu(target->initiator_ap_flags); 766 if (flags & IWL_INITIATOR_AP_FLAGS_SECURED) { 767 if (!IWL_MVM_FTM_INITIATOR_SECURE_LTF) 768 flags &= ~IWL_INITIATOR_AP_FLAGS_SECURED; 769 770 flags |= IWL_INITIATOR_AP_FLAGS_PMF; 771 target->initiator_ap_flags = cpu_to_le32(flags); 772 } 773 774 return 0; 775 } 776 777 static int iwl_mvm_ftm_start_v12(struct iwl_mvm *mvm, 778 struct ieee80211_vif *vif, 779 struct cfg80211_pmsr_request *req) 780 { 781 struct iwl_tof_range_req_cmd_v12 cmd; 782 struct iwl_host_cmd hcmd = { 783 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 784 .dataflags[0] = IWL_HCMD_DFL_DUP, 785 .data[0] = &cmd, 786 .len[0] = sizeof(cmd), 787 }; 788 u8 i; 789 int err; 790 791 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 792 793 for (i = 0; i < cmd.num_of_ap; i++) { 794 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 795 struct iwl_tof_range_req_ap_entry_v8 *target = &cmd.ap[i]; 796 797 err = iwl_mvm_ftm_put_target_v8(mvm, vif, peer, target); 798 if (err) 799 return err; 800 } 801 802 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 803 } 804 805 static int iwl_mvm_ftm_start_v13(struct iwl_mvm *mvm, 806 struct ieee80211_vif *vif, 807 struct cfg80211_pmsr_request *req) 808 { 809 struct iwl_tof_range_req_cmd_v13 cmd; 810 struct iwl_host_cmd hcmd = { 811 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 812 .dataflags[0] = IWL_HCMD_DFL_DUP, 813 .data[0] = &cmd, 814 .len[0] = sizeof(cmd), 815 }; 816 u8 i; 817 int err; 818 819 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 820 821 for (i = 0; i < cmd.num_of_ap; i++) { 822 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 823 struct iwl_tof_range_req_ap_entry_v9 *target = &cmd.ap[i]; 824 825 err = iwl_mvm_ftm_put_target_v8(mvm, vif, peer, (void *)target); 826 if (err) 827 return err; 828 829 if (peer->ftm.trigger_based || peer->ftm.non_trigger_based) 830 target->bss_color = peer->ftm.bss_color; 831 832 if (peer->ftm.non_trigger_based) { 833 target->min_time_between_msr = 834 cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR); 835 target->burst_period = 836 cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR); 837 } else { 838 target->min_time_between_msr = cpu_to_le16(0); 839 } 840 841 target->band = 842 iwl_mvm_phy_band_from_nl80211(peer->chandef.chan->band); 843 } 844 845 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 846 } 847 848 static int 849 iwl_mvm_ftm_put_target_v10(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 850 struct cfg80211_pmsr_request_peer *peer, 851 struct iwl_tof_range_req_ap_entry *target) 852 { 853 u32 i2r_max_sts, flags; 854 int ret; 855 856 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 857 &target->format_bw, 858 &target->ctrl_ch_position); 859 if (ret) 860 return ret; 861 862 memcpy(target->bssid, peer->addr, ETH_ALEN); 863 target->burst_period = 864 cpu_to_le16(peer->ftm.burst_period); 865 target->samples_per_burst = peer->ftm.ftms_per_burst; 866 target->num_of_bursts = peer->ftm.num_bursts_exp; 867 iwl_mvm_ftm_set_target_flags(mvm, peer, &target->initiator_ap_flags); 868 iwl_mvm_ftm_set_sta(mvm, vif, peer, &target->sta_id, 869 &target->initiator_ap_flags); 870 iwl_mvm_ftm_set_secured_ranging(mvm, vif, target->bssid, 871 &target->cipher, target->hltk, 872 target->tk, target->rx_pn, 873 target->tx_pn, 874 &target->initiator_ap_flags); 875 876 i2r_max_sts = IWL_MVM_FTM_I2R_MAX_STS > 1 ? 1 : 877 IWL_MVM_FTM_I2R_MAX_STS; 878 879 target->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP | 880 (IWL_MVM_FTM_R2I_MAX_STS << IWL_LOCATION_MAX_STS_POS) | 881 (IWL_MVM_FTM_R2I_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS); 882 target->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP | 883 (i2r_max_sts << IWL_LOCATION_MAX_STS_POS) | 884 (IWL_MVM_FTM_I2R_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS); 885 886 if (peer->ftm.non_trigger_based) { 887 target->min_time_between_msr = 888 cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR); 889 target->burst_period = 890 cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR); 891 } else { 892 target->min_time_between_msr = cpu_to_le16(0); 893 } 894 895 target->band = 896 iwl_mvm_phy_band_from_nl80211(peer->chandef.chan->band); 897 898 /* 899 * TODO: Beacon interval is currently unknown, so use the common value 900 * of 100 TUs. 901 */ 902 target->beacon_interval = cpu_to_le16(100); 903 904 /* 905 * If secure LTF is turned off, replace the flag with PMF only 906 */ 907 flags = le32_to_cpu(target->initiator_ap_flags); 908 if (flags & IWL_INITIATOR_AP_FLAGS_SECURED) { 909 if (!IWL_MVM_FTM_INITIATOR_SECURE_LTF) 910 flags &= ~IWL_INITIATOR_AP_FLAGS_SECURED; 911 912 flags |= IWL_INITIATOR_AP_FLAGS_PMF; 913 target->initiator_ap_flags = cpu_to_le32(flags); 914 } 915 916 return 0; 917 } 918 919 static int iwl_mvm_ftm_start_v14(struct iwl_mvm *mvm, 920 struct ieee80211_vif *vif, 921 struct cfg80211_pmsr_request *req) 922 { 923 struct iwl_tof_range_req_cmd cmd; 924 struct iwl_host_cmd hcmd = { 925 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 926 .dataflags[0] = IWL_HCMD_DFL_DUP, 927 .data[0] = &cmd, 928 .len[0] = sizeof(cmd), 929 }; 930 u8 i; 931 int err; 932 933 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 934 935 for (i = 0; i < cmd.num_of_ap; i++) { 936 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 937 struct iwl_tof_range_req_ap_entry *target = &cmd.ap[i]; 938 939 err = iwl_mvm_ftm_put_target_v10(mvm, vif, peer, target); 940 if (err) 941 return err; 942 } 943 944 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 945 } 946 947 int iwl_mvm_ftm_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 948 struct cfg80211_pmsr_request *req) 949 { 950 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 951 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ); 952 int err; 953 954 lockdep_assert_held(&mvm->mutex); 955 956 if (mvm->ftm_initiator.req) 957 return -EBUSY; 958 959 if (new_api) { 960 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 961 WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 962 IWL_FW_CMD_VER_UNKNOWN); 963 964 switch (cmd_ver) { 965 case 15: 966 /* Version 15 has the same struct as 14 */ 967 case 14: 968 err = iwl_mvm_ftm_start_v14(mvm, vif, req); 969 break; 970 case 13: 971 err = iwl_mvm_ftm_start_v13(mvm, vif, req); 972 break; 973 case 12: 974 err = iwl_mvm_ftm_start_v12(mvm, vif, req); 975 break; 976 case 11: 977 err = iwl_mvm_ftm_start_v11(mvm, vif, req); 978 break; 979 case 9: 980 case 10: 981 err = iwl_mvm_ftm_start_v9(mvm, vif, req); 982 break; 983 case 8: 984 err = iwl_mvm_ftm_start_v8(mvm, vif, req); 985 break; 986 default: 987 err = iwl_mvm_ftm_start_v7(mvm, vif, req); 988 break; 989 } 990 } else { 991 err = iwl_mvm_ftm_start_v5(mvm, vif, req); 992 } 993 994 if (!err) { 995 mvm->ftm_initiator.req = req; 996 mvm->ftm_initiator.req_wdev = ieee80211_vif_to_wdev(vif); 997 } 998 999 return err; 1000 } 1001 1002 void iwl_mvm_ftm_abort(struct iwl_mvm *mvm, struct cfg80211_pmsr_request *req) 1003 { 1004 struct iwl_tof_range_abort_cmd cmd = { 1005 .request_id = req->cookie, 1006 }; 1007 1008 lockdep_assert_held(&mvm->mutex); 1009 1010 if (req != mvm->ftm_initiator.req) 1011 return; 1012 1013 iwl_mvm_ftm_reset(mvm); 1014 1015 if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(LOCATION_GROUP, TOF_RANGE_ABORT_CMD), 1016 0, sizeof(cmd), &cmd)) 1017 IWL_ERR(mvm, "failed to abort FTM process\n"); 1018 } 1019 1020 static int iwl_mvm_ftm_find_peer(struct cfg80211_pmsr_request *req, 1021 const u8 *addr) 1022 { 1023 int i; 1024 1025 for (i = 0; i < req->n_peers; i++) { 1026 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 1027 1028 if (ether_addr_equal_unaligned(peer->addr, addr)) 1029 return i; 1030 } 1031 1032 return -ENOENT; 1033 } 1034 1035 static u64 iwl_mvm_ftm_get_host_time(struct iwl_mvm *mvm, __le32 fw_gp2_ts) 1036 { 1037 u32 gp2_ts = le32_to_cpu(fw_gp2_ts); 1038 u32 curr_gp2, diff; 1039 u64 now_from_boot_ns; 1040 1041 iwl_mvm_get_sync_time(mvm, CLOCK_BOOTTIME, &curr_gp2, 1042 &now_from_boot_ns, NULL); 1043 1044 if (curr_gp2 >= gp2_ts) 1045 diff = curr_gp2 - gp2_ts; 1046 else 1047 diff = curr_gp2 + (U32_MAX - gp2_ts + 1); 1048 1049 return now_from_boot_ns - (u64)diff * 1000; 1050 } 1051 1052 static void iwl_mvm_ftm_get_lci_civic(struct iwl_mvm *mvm, 1053 struct cfg80211_pmsr_result *res) 1054 { 1055 struct iwl_mvm_loc_entry *entry; 1056 1057 list_for_each_entry(entry, &mvm->ftm_initiator.loc_list, list) { 1058 if (!ether_addr_equal_unaligned(res->addr, entry->addr)) 1059 continue; 1060 1061 if (entry->lci_len) { 1062 res->ftm.lci_len = entry->lci_len; 1063 res->ftm.lci = entry->buf; 1064 } 1065 1066 if (entry->civic_len) { 1067 res->ftm.civicloc_len = entry->civic_len; 1068 res->ftm.civicloc = entry->buf + entry->lci_len; 1069 } 1070 1071 /* we found the entry we needed */ 1072 break; 1073 } 1074 } 1075 1076 static int iwl_mvm_ftm_range_resp_valid(struct iwl_mvm *mvm, u8 request_id, 1077 u8 num_of_aps) 1078 { 1079 lockdep_assert_held(&mvm->mutex); 1080 1081 if (request_id != (u8)mvm->ftm_initiator.req->cookie) { 1082 IWL_ERR(mvm, "Request ID mismatch, got %u, active %u\n", 1083 request_id, (u8)mvm->ftm_initiator.req->cookie); 1084 return -EINVAL; 1085 } 1086 1087 if (num_of_aps > mvm->ftm_initiator.req->n_peers) { 1088 IWL_ERR(mvm, "FTM range response invalid\n"); 1089 return -EINVAL; 1090 } 1091 1092 return 0; 1093 } 1094 1095 static void iwl_mvm_ftm_rtt_smoothing(struct iwl_mvm *mvm, 1096 struct cfg80211_pmsr_result *res) 1097 { 1098 struct iwl_mvm_smooth_entry *resp = NULL, *iter; 1099 s64 rtt_avg, rtt = res->ftm.rtt_avg; 1100 u32 undershoot, overshoot; 1101 u8 alpha; 1102 1103 if (!IWL_MVM_FTM_INITIATOR_ENABLE_SMOOTH) 1104 return; 1105 1106 WARN_ON(rtt < 0); 1107 1108 if (res->status != NL80211_PMSR_STATUS_SUCCESS) { 1109 IWL_DEBUG_INFO(mvm, 1110 ": %pM: ignore failed measurement. Status=%u\n", 1111 res->addr, res->status); 1112 return; 1113 } 1114 1115 list_for_each_entry(iter, &mvm->ftm_initiator.smooth.resp, list) { 1116 if (!memcmp(res->addr, iter->addr, ETH_ALEN)) { 1117 resp = iter; 1118 break; 1119 } 1120 } 1121 1122 if (!resp) { 1123 resp = kzalloc(sizeof(*resp), GFP_KERNEL); 1124 if (!resp) 1125 return; 1126 1127 memcpy(resp->addr, res->addr, ETH_ALEN); 1128 list_add_tail(&resp->list, &mvm->ftm_initiator.smooth.resp); 1129 1130 resp->rtt_avg = rtt; 1131 1132 IWL_DEBUG_INFO(mvm, "new: %pM: rtt_avg=%lld\n", 1133 resp->addr, resp->rtt_avg); 1134 goto update_time; 1135 } 1136 1137 if (res->host_time - resp->host_time > 1138 IWL_MVM_FTM_INITIATOR_SMOOTH_AGE_SEC * 1000000000) { 1139 resp->rtt_avg = rtt; 1140 1141 IWL_DEBUG_INFO(mvm, "expired: %pM: rtt_avg=%lld\n", 1142 resp->addr, resp->rtt_avg); 1143 goto update_time; 1144 } 1145 1146 /* Smooth the results based on the tracked RTT average */ 1147 undershoot = IWL_MVM_FTM_INITIATOR_SMOOTH_UNDERSHOOT; 1148 overshoot = IWL_MVM_FTM_INITIATOR_SMOOTH_OVERSHOOT; 1149 alpha = IWL_MVM_FTM_INITIATOR_SMOOTH_ALPHA; 1150 1151 rtt_avg = div_s64(alpha * rtt + (100 - alpha) * resp->rtt_avg, 100); 1152 1153 IWL_DEBUG_INFO(mvm, 1154 "%pM: prev rtt_avg=%lld, new rtt_avg=%lld, rtt=%lld\n", 1155 resp->addr, resp->rtt_avg, rtt_avg, rtt); 1156 1157 /* 1158 * update the responder's average RTT results regardless of 1159 * the under/over shoot logic below 1160 */ 1161 resp->rtt_avg = rtt_avg; 1162 1163 /* smooth the results */ 1164 if (rtt_avg > rtt && (rtt_avg - rtt) > undershoot) { 1165 res->ftm.rtt_avg = rtt_avg; 1166 1167 IWL_DEBUG_INFO(mvm, 1168 "undershoot: val=%lld\n", 1169 (rtt_avg - rtt)); 1170 } else if (rtt_avg < rtt && (rtt - rtt_avg) > 1171 overshoot) { 1172 res->ftm.rtt_avg = rtt_avg; 1173 IWL_DEBUG_INFO(mvm, 1174 "overshoot: val=%lld\n", 1175 (rtt - rtt_avg)); 1176 } 1177 1178 update_time: 1179 resp->host_time = res->host_time; 1180 } 1181 1182 static void iwl_mvm_debug_range_resp(struct iwl_mvm *mvm, u8 index, 1183 struct cfg80211_pmsr_result *res) 1184 { 1185 s64 rtt_avg = div_s64(res->ftm.rtt_avg * 100, 6666); 1186 1187 IWL_DEBUG_INFO(mvm, "entry %d\n", index); 1188 IWL_DEBUG_INFO(mvm, "\tstatus: %d\n", res->status); 1189 IWL_DEBUG_INFO(mvm, "\tBSSID: %pM\n", res->addr); 1190 IWL_DEBUG_INFO(mvm, "\thost time: %llu\n", res->host_time); 1191 IWL_DEBUG_INFO(mvm, "\tburst index: %d\n", res->ftm.burst_index); 1192 IWL_DEBUG_INFO(mvm, "\tsuccess num: %u\n", res->ftm.num_ftmr_successes); 1193 IWL_DEBUG_INFO(mvm, "\trssi: %d\n", res->ftm.rssi_avg); 1194 IWL_DEBUG_INFO(mvm, "\trssi spread: %d\n", res->ftm.rssi_spread); 1195 IWL_DEBUG_INFO(mvm, "\trtt: %lld\n", res->ftm.rtt_avg); 1196 IWL_DEBUG_INFO(mvm, "\trtt var: %llu\n", res->ftm.rtt_variance); 1197 IWL_DEBUG_INFO(mvm, "\trtt spread: %llu\n", res->ftm.rtt_spread); 1198 IWL_DEBUG_INFO(mvm, "\tdistance: %lld\n", rtt_avg); 1199 } 1200 1201 static void 1202 iwl_mvm_ftm_pasn_update_pn(struct iwl_mvm *mvm, 1203 struct iwl_tof_range_rsp_ap_entry_ntfy *fw_ap) 1204 { 1205 struct iwl_mvm_ftm_pasn_entry *entry; 1206 1207 lockdep_assert_held(&mvm->mutex); 1208 1209 list_for_each_entry(entry, &mvm->ftm_initiator.pasn_list, list) { 1210 if (memcmp(fw_ap->bssid, entry->addr, sizeof(entry->addr))) 1211 continue; 1212 1213 memcpy(entry->rx_pn, fw_ap->rx_pn, sizeof(entry->rx_pn)); 1214 memcpy(entry->tx_pn, fw_ap->tx_pn, sizeof(entry->tx_pn)); 1215 return; 1216 } 1217 } 1218 1219 static u8 iwl_mvm_ftm_get_range_resp_ver(struct iwl_mvm *mvm) 1220 { 1221 if (!fw_has_api(&mvm->fw->ucode_capa, 1222 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ)) 1223 return 5; 1224 1225 /* Starting from version 8, the FW advertises the version */ 1226 if (mvm->cmd_ver.range_resp >= 8) 1227 return mvm->cmd_ver.range_resp; 1228 else if (fw_has_api(&mvm->fw->ucode_capa, 1229 IWL_UCODE_TLV_API_FTM_RTT_ACCURACY)) 1230 return 7; 1231 1232 /* The first version of the new range request API */ 1233 return 6; 1234 } 1235 1236 static bool iwl_mvm_ftm_resp_size_validation(u8 ver, unsigned int pkt_len) 1237 { 1238 switch (ver) { 1239 case 9: 1240 case 8: 1241 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy); 1242 case 7: 1243 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v7); 1244 case 6: 1245 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v6); 1246 case 5: 1247 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v5); 1248 default: 1249 WARN_ONCE(1, "FTM: unsupported range response version %u", ver); 1250 return false; 1251 } 1252 } 1253 1254 void iwl_mvm_ftm_range_resp(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 1255 { 1256 struct iwl_rx_packet *pkt = rxb_addr(rxb); 1257 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt); 1258 struct iwl_tof_range_rsp_ntfy_v5 *fw_resp_v5 = (void *)pkt->data; 1259 struct iwl_tof_range_rsp_ntfy_v6 *fw_resp_v6 = (void *)pkt->data; 1260 struct iwl_tof_range_rsp_ntfy_v7 *fw_resp_v7 = (void *)pkt->data; 1261 struct iwl_tof_range_rsp_ntfy *fw_resp_v8 = (void *)pkt->data; 1262 int i; 1263 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 1264 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ); 1265 u8 num_of_aps, last_in_batch; 1266 u8 notif_ver = iwl_mvm_ftm_get_range_resp_ver(mvm); 1267 1268 lockdep_assert_held(&mvm->mutex); 1269 1270 if (!mvm->ftm_initiator.req) { 1271 return; 1272 } 1273 1274 if (unlikely(!iwl_mvm_ftm_resp_size_validation(notif_ver, pkt_len))) 1275 return; 1276 1277 if (new_api) { 1278 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp_v8->request_id, 1279 fw_resp_v8->num_of_aps)) 1280 return; 1281 1282 num_of_aps = fw_resp_v8->num_of_aps; 1283 last_in_batch = fw_resp_v8->last_report; 1284 } else { 1285 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp_v5->request_id, 1286 fw_resp_v5->num_of_aps)) 1287 return; 1288 1289 num_of_aps = fw_resp_v5->num_of_aps; 1290 last_in_batch = fw_resp_v5->last_in_batch; 1291 } 1292 1293 IWL_DEBUG_INFO(mvm, "Range response received\n"); 1294 IWL_DEBUG_INFO(mvm, "request id: %lld, num of entries: %u\n", 1295 mvm->ftm_initiator.req->cookie, num_of_aps); 1296 1297 for (i = 0; i < num_of_aps && i < IWL_TOF_MAX_APS; i++) { 1298 struct cfg80211_pmsr_result result = {}; 1299 struct iwl_tof_range_rsp_ap_entry_ntfy *fw_ap; 1300 int peer_idx; 1301 1302 if (new_api) { 1303 if (notif_ver >= 8) { 1304 fw_ap = &fw_resp_v8->ap[i]; 1305 iwl_mvm_ftm_pasn_update_pn(mvm, fw_ap); 1306 } else if (notif_ver == 7) { 1307 fw_ap = (void *)&fw_resp_v7->ap[i]; 1308 } else { 1309 fw_ap = (void *)&fw_resp_v6->ap[i]; 1310 } 1311 1312 result.final = fw_ap->last_burst; 1313 result.ap_tsf = le32_to_cpu(fw_ap->start_tsf); 1314 result.ap_tsf_valid = 1; 1315 } else { 1316 /* the first part is the same for old and new APIs */ 1317 fw_ap = (void *)&fw_resp_v5->ap[i]; 1318 /* 1319 * FIXME: the firmware needs to report this, we don't 1320 * even know the number of bursts the responder picked 1321 * (if we asked it to) 1322 */ 1323 result.final = 0; 1324 } 1325 1326 peer_idx = iwl_mvm_ftm_find_peer(mvm->ftm_initiator.req, 1327 fw_ap->bssid); 1328 if (peer_idx < 0) { 1329 IWL_WARN(mvm, 1330 "Unknown address (%pM, target #%d) in FTM response\n", 1331 fw_ap->bssid, i); 1332 continue; 1333 } 1334 1335 switch (fw_ap->measure_status) { 1336 case IWL_TOF_ENTRY_SUCCESS: 1337 result.status = NL80211_PMSR_STATUS_SUCCESS; 1338 break; 1339 case IWL_TOF_ENTRY_TIMING_MEASURE_TIMEOUT: 1340 result.status = NL80211_PMSR_STATUS_TIMEOUT; 1341 break; 1342 case IWL_TOF_ENTRY_NO_RESPONSE: 1343 result.status = NL80211_PMSR_STATUS_FAILURE; 1344 result.ftm.failure_reason = 1345 NL80211_PMSR_FTM_FAILURE_NO_RESPONSE; 1346 break; 1347 case IWL_TOF_ENTRY_REQUEST_REJECTED: 1348 result.status = NL80211_PMSR_STATUS_FAILURE; 1349 result.ftm.failure_reason = 1350 NL80211_PMSR_FTM_FAILURE_PEER_BUSY; 1351 result.ftm.busy_retry_time = fw_ap->refusal_period; 1352 break; 1353 default: 1354 result.status = NL80211_PMSR_STATUS_FAILURE; 1355 result.ftm.failure_reason = 1356 NL80211_PMSR_FTM_FAILURE_UNSPECIFIED; 1357 break; 1358 } 1359 memcpy(result.addr, fw_ap->bssid, ETH_ALEN); 1360 result.host_time = iwl_mvm_ftm_get_host_time(mvm, 1361 fw_ap->timestamp); 1362 result.type = NL80211_PMSR_TYPE_FTM; 1363 result.ftm.burst_index = mvm->ftm_initiator.responses[peer_idx]; 1364 mvm->ftm_initiator.responses[peer_idx]++; 1365 result.ftm.rssi_avg = fw_ap->rssi; 1366 result.ftm.rssi_avg_valid = 1; 1367 result.ftm.rssi_spread = fw_ap->rssi_spread; 1368 result.ftm.rssi_spread_valid = 1; 1369 result.ftm.rtt_avg = (s32)le32_to_cpu(fw_ap->rtt); 1370 result.ftm.rtt_avg_valid = 1; 1371 result.ftm.rtt_variance = le32_to_cpu(fw_ap->rtt_variance); 1372 result.ftm.rtt_variance_valid = 1; 1373 result.ftm.rtt_spread = le32_to_cpu(fw_ap->rtt_spread); 1374 result.ftm.rtt_spread_valid = 1; 1375 1376 iwl_mvm_ftm_get_lci_civic(mvm, &result); 1377 1378 iwl_mvm_ftm_rtt_smoothing(mvm, &result); 1379 1380 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev, 1381 mvm->ftm_initiator.req, 1382 &result, GFP_KERNEL); 1383 1384 if (fw_has_api(&mvm->fw->ucode_capa, 1385 IWL_UCODE_TLV_API_FTM_RTT_ACCURACY)) 1386 IWL_DEBUG_INFO(mvm, "RTT confidence: %u\n", 1387 fw_ap->rttConfidence); 1388 1389 iwl_mvm_debug_range_resp(mvm, i, &result); 1390 } 1391 1392 if (last_in_batch) { 1393 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev, 1394 mvm->ftm_initiator.req, 1395 GFP_KERNEL); 1396 iwl_mvm_ftm_reset(mvm); 1397 } 1398 } 1399 1400 void iwl_mvm_ftm_lc_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 1401 { 1402 struct iwl_rx_packet *pkt = rxb_addr(rxb); 1403 const struct ieee80211_mgmt *mgmt = (void *)pkt->data; 1404 size_t len = iwl_rx_packet_payload_len(pkt); 1405 struct iwl_mvm_loc_entry *entry; 1406 const u8 *ies, *lci, *civic, *msr_ie; 1407 size_t ies_len, lci_len = 0, civic_len = 0; 1408 size_t baselen = IEEE80211_MIN_ACTION_SIZE + 1409 sizeof(mgmt->u.action.u.ftm); 1410 static const u8 rprt_type_lci = IEEE80211_SPCT_MSR_RPRT_TYPE_LCI; 1411 static const u8 rprt_type_civic = IEEE80211_SPCT_MSR_RPRT_TYPE_CIVIC; 1412 1413 if (len <= baselen) 1414 return; 1415 1416 lockdep_assert_held(&mvm->mutex); 1417 1418 ies = mgmt->u.action.u.ftm.variable; 1419 ies_len = len - baselen; 1420 1421 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len, 1422 &rprt_type_lci, 1, 4); 1423 if (msr_ie) { 1424 lci = msr_ie + 2; 1425 lci_len = msr_ie[1]; 1426 } 1427 1428 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len, 1429 &rprt_type_civic, 1, 4); 1430 if (msr_ie) { 1431 civic = msr_ie + 2; 1432 civic_len = msr_ie[1]; 1433 } 1434 1435 entry = kmalloc(sizeof(*entry) + lci_len + civic_len, GFP_KERNEL); 1436 if (!entry) 1437 return; 1438 1439 memcpy(entry->addr, mgmt->bssid, ETH_ALEN); 1440 1441 entry->lci_len = lci_len; 1442 if (lci_len) 1443 memcpy(entry->buf, lci, lci_len); 1444 1445 entry->civic_len = civic_len; 1446 if (civic_len) 1447 memcpy(entry->buf + lci_len, civic, civic_len); 1448 1449 list_add_tail(&entry->list, &mvm->ftm_initiator.loc_list); 1450 } 1451