1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
9 * Copyright (C) 2018 Intel Corporation
10 * Copyright (C) 2019 Intel Corporation
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
24 * Contact Information:
25 * Intel Linux Wireless <linuxwifi@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
31 * Copyright (C) 2018 Intel Corporation
32 * Copyright (C) 2019 Intel Corporation
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * * Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * * Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
45 * * Neither the name Intel Corporation nor the names of its
46 * contributors may be used to endorse or promote products derived
47 * from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62 #include <linux/etherdevice.h>
63 #include <linux/math64.h>
64 #include <net/cfg80211.h>
68 #include "constants.h"
70 struct iwl_mvm_loc_entry {
71 struct list_head list;
73 u8 lci_len, civic_len;
77 static void iwl_mvm_ftm_reset(struct iwl_mvm *mvm)
79 struct iwl_mvm_loc_entry *e, *t;
81 mvm->ftm_initiator.req = NULL;
82 mvm->ftm_initiator.req_wdev = NULL;
83 memset(mvm->ftm_initiator.responses, 0,
84 sizeof(mvm->ftm_initiator.responses));
85 list_for_each_entry_safe(e, t, &mvm->ftm_initiator.loc_list, list) {
91 void iwl_mvm_ftm_restart(struct iwl_mvm *mvm)
93 struct cfg80211_pmsr_result result = {
94 .status = NL80211_PMSR_STATUS_FAILURE,
96 .host_time = ktime_get_boot_ns(),
97 .type = NL80211_PMSR_TYPE_FTM,
101 lockdep_assert_held(&mvm->mutex);
103 if (!mvm->ftm_initiator.req)
106 for (i = 0; i < mvm->ftm_initiator.req->n_peers; i++) {
107 memcpy(result.addr, mvm->ftm_initiator.req->peers[i].addr,
109 result.ftm.burst_index = mvm->ftm_initiator.responses[i];
111 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev,
112 mvm->ftm_initiator.req,
113 &result, GFP_KERNEL);
116 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev,
117 mvm->ftm_initiator.req, GFP_KERNEL);
118 iwl_mvm_ftm_reset(mvm);
122 iwl_ftm_range_request_status_to_err(enum iwl_tof_range_request_status s)
125 case IWL_TOF_RANGE_REQUEST_STATUS_SUCCESS:
127 case IWL_TOF_RANGE_REQUEST_STATUS_BUSY:
135 static void iwl_mvm_ftm_cmd_v5(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
136 struct iwl_tof_range_req_cmd_v5 *cmd,
137 struct cfg80211_pmsr_request *req)
141 cmd->request_id = req->cookie;
142 cmd->num_of_ap = req->n_peers;
144 /* use maximum for "no timeout" or bigger than what we can do */
145 if (!req->timeout || req->timeout > 255 * 100)
146 cmd->req_timeout = 255;
148 cmd->req_timeout = DIV_ROUND_UP(req->timeout, 100);
151 * We treat it always as random, since if not we'll
152 * have filled our local address there instead.
154 cmd->macaddr_random = 1;
155 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN);
156 for (i = 0; i < ETH_ALEN; i++)
157 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i];
159 if (vif->bss_conf.assoc)
160 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN);
162 eth_broadcast_addr(cmd->range_req_bssid);
165 static void iwl_mvm_ftm_cmd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
166 struct iwl_tof_range_req_cmd *cmd,
167 struct cfg80211_pmsr_request *req)
171 cmd->initiator_flags =
172 cpu_to_le32(IWL_TOF_INITIATOR_FLAGS_MACADDR_RANDOM |
173 IWL_TOF_INITIATOR_FLAGS_NON_ASAP_SUPPORT);
174 cmd->request_id = req->cookie;
175 cmd->num_of_ap = req->n_peers;
178 * Use a large value for "no timeout". Don't use the maximum value
179 * because of fw limitations.
182 cmd->req_timeout_ms = cpu_to_le32(req->timeout);
184 cmd->req_timeout_ms = cpu_to_le32(0xfffff);
186 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN);
187 for (i = 0; i < ETH_ALEN; i++)
188 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i];
190 if (vif->bss_conf.assoc)
191 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN);
193 eth_broadcast_addr(cmd->range_req_bssid);
195 /* TODO: fill in tsf_mac_id if needed */
196 cmd->tsf_mac_id = cpu_to_le32(0xff);
199 static int iwl_mvm_ftm_target_chandef(struct iwl_mvm *mvm,
200 struct cfg80211_pmsr_request_peer *peer,
201 u8 *channel, u8 *bandwidth,
202 u8 *ctrl_ch_position)
204 u32 freq = peer->chandef.chan->center_freq;
206 *channel = ieee80211_frequency_to_channel(freq);
208 switch (peer->chandef.width) {
209 case NL80211_CHAN_WIDTH_20_NOHT:
210 *bandwidth = IWL_TOF_BW_20_LEGACY;
212 case NL80211_CHAN_WIDTH_20:
213 *bandwidth = IWL_TOF_BW_20_HT;
215 case NL80211_CHAN_WIDTH_40:
216 *bandwidth = IWL_TOF_BW_40;
218 case NL80211_CHAN_WIDTH_80:
219 *bandwidth = IWL_TOF_BW_80;
222 IWL_ERR(mvm, "Unsupported BW in FTM request (%d)\n",
223 peer->chandef.width);
227 *ctrl_ch_position = (peer->chandef.width > NL80211_CHAN_WIDTH_20) ?
228 iwl_mvm_get_ctrl_pos(&peer->chandef) : 0;
234 iwl_mvm_ftm_put_target_v2(struct iwl_mvm *mvm,
235 struct cfg80211_pmsr_request_peer *peer,
236 struct iwl_tof_range_req_ap_entry_v2 *target)
240 ret = iwl_mvm_ftm_target_chandef(mvm, peer, &target->channel_num,
242 &target->ctrl_ch_position);
246 memcpy(target->bssid, peer->addr, ETH_ALEN);
247 target->burst_period =
248 cpu_to_le16(peer->ftm.burst_period);
249 target->samples_per_burst = peer->ftm.ftms_per_burst;
250 target->num_of_bursts = peer->ftm.num_bursts_exp;
251 target->measure_type = 0; /* regular two-sided FTM */
252 target->retries_per_sample = peer->ftm.ftmr_retries;
253 target->asap_mode = peer->ftm.asap;
254 target->enable_dyn_ack = IWL_MVM_FTM_INITIATOR_DYNACK;
256 if (peer->ftm.request_lci)
257 target->location_req |= IWL_TOF_LOC_LCI;
258 if (peer->ftm.request_civicloc)
259 target->location_req |= IWL_TOF_LOC_CIVIC;
261 target->algo_type = IWL_MVM_FTM_INITIATOR_ALGO;
266 #define FTM_PUT_FLAG(flag) (target->initiator_ap_flags |= \
267 cpu_to_le32(IWL_INITIATOR_AP_FLAGS_##flag))
269 static int iwl_mvm_ftm_put_target(struct iwl_mvm *mvm,
270 struct cfg80211_pmsr_request_peer *peer,
271 struct iwl_tof_range_req_ap_entry *target)
275 ret = iwl_mvm_ftm_target_chandef(mvm, peer, &target->channel_num,
277 &target->ctrl_ch_position);
281 memcpy(target->bssid, peer->addr, ETH_ALEN);
282 target->burst_period =
283 cpu_to_le16(peer->ftm.burst_period);
284 target->samples_per_burst = peer->ftm.ftms_per_burst;
285 target->num_of_bursts = peer->ftm.num_bursts_exp;
286 target->ftmr_max_retries = peer->ftm.ftmr_retries;
287 target->initiator_ap_flags = cpu_to_le32(0);
292 if (peer->ftm.request_lci)
293 FTM_PUT_FLAG(LCI_REQUEST);
295 if (peer->ftm.request_civicloc)
296 FTM_PUT_FLAG(CIVIC_REQUEST);
298 if (IWL_MVM_FTM_INITIATOR_DYNACK)
299 FTM_PUT_FLAG(DYN_ACK);
301 if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_LINEAR_REG)
302 FTM_PUT_FLAG(ALGO_LR);
303 else if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_FFT)
304 FTM_PUT_FLAG(ALGO_FFT);
309 int iwl_mvm_ftm_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
310 struct cfg80211_pmsr_request *req)
312 struct iwl_tof_range_req_cmd_v5 cmd_v5;
313 struct iwl_tof_range_req_cmd cmd;
314 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
315 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ);
317 struct iwl_host_cmd hcmd = {
318 .id = iwl_cmd_id(TOF_RANGE_REQ_CMD, LOCATION_GROUP, 0),
319 .dataflags[0] = IWL_HCMD_DFL_DUP,
324 lockdep_assert_held(&mvm->mutex);
326 if (mvm->ftm_initiator.req)
330 iwl_mvm_ftm_cmd(mvm, vif, &cmd, req);
332 hcmd.len[0] = sizeof(cmd);
333 num_of_ap = cmd.num_of_ap;
335 iwl_mvm_ftm_cmd_v5(mvm, vif, &cmd_v5, req);
336 hcmd.data[0] = &cmd_v5;
337 hcmd.len[0] = sizeof(cmd_v5);
338 num_of_ap = cmd_v5.num_of_ap;
341 for (i = 0; i < num_of_ap; i++) {
342 struct cfg80211_pmsr_request_peer *peer = &req->peers[i];
345 err = iwl_mvm_ftm_put_target(mvm, peer, &cmd.ap[i]);
347 err = iwl_mvm_ftm_put_target_v2(mvm, peer,
354 err = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
355 if (!err && status) {
356 IWL_ERR(mvm, "FTM range request command failure, status: %u\n",
358 err = iwl_ftm_range_request_status_to_err(status);
362 mvm->ftm_initiator.req = req;
363 mvm->ftm_initiator.req_wdev = ieee80211_vif_to_wdev(vif);
369 void iwl_mvm_ftm_abort(struct iwl_mvm *mvm, struct cfg80211_pmsr_request *req)
371 struct iwl_tof_range_abort_cmd cmd = {
372 .request_id = req->cookie,
375 lockdep_assert_held(&mvm->mutex);
377 if (req != mvm->ftm_initiator.req)
380 if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_RANGE_ABORT_CMD,
382 0, sizeof(cmd), &cmd))
383 IWL_ERR(mvm, "failed to abort FTM process\n");
386 static int iwl_mvm_ftm_find_peer(struct cfg80211_pmsr_request *req,
391 for (i = 0; i < req->n_peers; i++) {
392 struct cfg80211_pmsr_request_peer *peer = &req->peers[i];
394 if (ether_addr_equal_unaligned(peer->addr, addr))
401 static u64 iwl_mvm_ftm_get_host_time(struct iwl_mvm *mvm, __le32 fw_gp2_ts)
403 u32 gp2_ts = le32_to_cpu(fw_gp2_ts);
405 u64 now_from_boot_ns;
407 iwl_mvm_get_sync_time(mvm, &curr_gp2, &now_from_boot_ns);
409 if (curr_gp2 >= gp2_ts)
410 diff = curr_gp2 - gp2_ts;
412 diff = curr_gp2 + (U32_MAX - gp2_ts + 1);
414 return now_from_boot_ns - (u64)diff * 1000;
417 static void iwl_mvm_ftm_get_lci_civic(struct iwl_mvm *mvm,
418 struct cfg80211_pmsr_result *res)
420 struct iwl_mvm_loc_entry *entry;
422 list_for_each_entry(entry, &mvm->ftm_initiator.loc_list, list) {
423 if (!ether_addr_equal_unaligned(res->addr, entry->addr))
426 if (entry->lci_len) {
427 res->ftm.lci_len = entry->lci_len;
428 res->ftm.lci = entry->buf;
431 if (entry->civic_len) {
432 res->ftm.civicloc_len = entry->civic_len;
433 res->ftm.civicloc = entry->buf + entry->lci_len;
436 /* we found the entry we needed */
441 static int iwl_mvm_ftm_range_resp_valid(struct iwl_mvm *mvm, u8 request_id,
444 lockdep_assert_held(&mvm->mutex);
446 if (request_id != (u8)mvm->ftm_initiator.req->cookie) {
447 IWL_ERR(mvm, "Request ID mismatch, got %u, active %u\n",
448 request_id, (u8)mvm->ftm_initiator.req->cookie);
452 if (num_of_aps > mvm->ftm_initiator.req->n_peers) {
453 IWL_ERR(mvm, "FTM range response invalid\n");
460 static void iwl_mvm_debug_range_resp(struct iwl_mvm *mvm, u8 index,
461 struct cfg80211_pmsr_result *res)
463 s64 rtt_avg = res->ftm.rtt_avg * 100;
465 do_div(rtt_avg, 6666);
467 IWL_DEBUG_INFO(mvm, "entry %d\n", index);
468 IWL_DEBUG_INFO(mvm, "\tstatus: %d\n", res->status);
469 IWL_DEBUG_INFO(mvm, "\tBSSID: %pM\n", res->addr);
470 IWL_DEBUG_INFO(mvm, "\thost time: %llu\n", res->host_time);
471 IWL_DEBUG_INFO(mvm, "\tburst index: %hhu\n", res->ftm.burst_index);
472 IWL_DEBUG_INFO(mvm, "\tsuccess num: %u\n", res->ftm.num_ftmr_successes);
473 IWL_DEBUG_INFO(mvm, "\trssi: %d\n", res->ftm.rssi_avg);
474 IWL_DEBUG_INFO(mvm, "\trssi spread: %hhu\n", res->ftm.rssi_spread);
475 IWL_DEBUG_INFO(mvm, "\trtt: %lld\n", res->ftm.rtt_avg);
476 IWL_DEBUG_INFO(mvm, "\trtt var: %llu\n", res->ftm.rtt_variance);
477 IWL_DEBUG_INFO(mvm, "\trtt spread: %llu\n", res->ftm.rtt_spread);
478 IWL_DEBUG_INFO(mvm, "\tdistance: %lld\n", rtt_avg);
481 void iwl_mvm_ftm_range_resp(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
483 struct iwl_rx_packet *pkt = rxb_addr(rxb);
484 struct iwl_tof_range_rsp_ntfy_v5 *fw_resp_v5 = (void *)pkt->data;
485 struct iwl_tof_range_rsp_ntfy *fw_resp = (void *)pkt->data;
487 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
488 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ);
489 u8 num_of_aps, last_in_batch;
491 lockdep_assert_held(&mvm->mutex);
493 if (!mvm->ftm_initiator.req) {
494 IWL_ERR(mvm, "Got FTM response but have no request?\n");
499 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp->request_id,
500 fw_resp->num_of_aps))
503 num_of_aps = fw_resp->num_of_aps;
504 last_in_batch = fw_resp->last_report;
506 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp_v5->request_id,
507 fw_resp_v5->num_of_aps))
510 num_of_aps = fw_resp_v5->num_of_aps;
511 last_in_batch = fw_resp_v5->last_in_batch;
514 IWL_DEBUG_INFO(mvm, "Range response received\n");
515 IWL_DEBUG_INFO(mvm, "request id: %lld, num of entries: %hhu\n",
516 mvm->ftm_initiator.req->cookie, num_of_aps);
518 for (i = 0; i < num_of_aps && i < IWL_MVM_TOF_MAX_APS; i++) {
519 struct cfg80211_pmsr_result result = {};
520 struct iwl_tof_range_rsp_ap_entry_ntfy *fw_ap;
524 fw_ap = &fw_resp->ap[i];
525 result.final = fw_resp->ap[i].last_burst;
527 /* the first part is the same for old and new APIs */
528 fw_ap = (void *)&fw_resp_v5->ap[i];
530 * FIXME: the firmware needs to report this, we don't
531 * even know the number of bursts the responder picked
532 * (if we asked it to)
537 peer_idx = iwl_mvm_ftm_find_peer(mvm->ftm_initiator.req,
541 "Unknown address (%pM, target #%d) in FTM response\n",
546 switch (fw_ap->measure_status) {
547 case IWL_TOF_ENTRY_SUCCESS:
548 result.status = NL80211_PMSR_STATUS_SUCCESS;
550 case IWL_TOF_ENTRY_TIMING_MEASURE_TIMEOUT:
551 result.status = NL80211_PMSR_STATUS_TIMEOUT;
553 case IWL_TOF_ENTRY_NO_RESPONSE:
554 result.status = NL80211_PMSR_STATUS_FAILURE;
555 result.ftm.failure_reason =
556 NL80211_PMSR_FTM_FAILURE_NO_RESPONSE;
558 case IWL_TOF_ENTRY_REQUEST_REJECTED:
559 result.status = NL80211_PMSR_STATUS_FAILURE;
560 result.ftm.failure_reason =
561 NL80211_PMSR_FTM_FAILURE_PEER_BUSY;
562 result.ftm.busy_retry_time = fw_ap->refusal_period;
565 result.status = NL80211_PMSR_STATUS_FAILURE;
566 result.ftm.failure_reason =
567 NL80211_PMSR_FTM_FAILURE_UNSPECIFIED;
570 memcpy(result.addr, fw_ap->bssid, ETH_ALEN);
571 result.host_time = iwl_mvm_ftm_get_host_time(mvm,
573 result.type = NL80211_PMSR_TYPE_FTM;
574 result.ftm.burst_index = mvm->ftm_initiator.responses[peer_idx];
575 mvm->ftm_initiator.responses[peer_idx]++;
576 result.ftm.rssi_avg = fw_ap->rssi;
577 result.ftm.rssi_avg_valid = 1;
578 result.ftm.rssi_spread = fw_ap->rssi_spread;
579 result.ftm.rssi_spread_valid = 1;
580 result.ftm.rtt_avg = (s32)le32_to_cpu(fw_ap->rtt);
581 result.ftm.rtt_avg_valid = 1;
582 result.ftm.rtt_variance = le32_to_cpu(fw_ap->rtt_variance);
583 result.ftm.rtt_variance_valid = 1;
584 result.ftm.rtt_spread = le32_to_cpu(fw_ap->rtt_spread);
585 result.ftm.rtt_spread_valid = 1;
587 iwl_mvm_ftm_get_lci_civic(mvm, &result);
589 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev,
590 mvm->ftm_initiator.req,
591 &result, GFP_KERNEL);
593 iwl_mvm_debug_range_resp(mvm, i, &result);
597 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev,
598 mvm->ftm_initiator.req,
600 iwl_mvm_ftm_reset(mvm);
604 void iwl_mvm_ftm_lc_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
606 struct iwl_rx_packet *pkt = rxb_addr(rxb);
607 const struct ieee80211_mgmt *mgmt = (void *)pkt->data;
608 size_t len = iwl_rx_packet_payload_len(pkt);
609 struct iwl_mvm_loc_entry *entry;
610 const u8 *ies, *lci, *civic, *msr_ie;
611 size_t ies_len, lci_len = 0, civic_len = 0;
612 size_t baselen = IEEE80211_MIN_ACTION_SIZE +
613 sizeof(mgmt->u.action.u.ftm);
614 static const u8 rprt_type_lci = IEEE80211_SPCT_MSR_RPRT_TYPE_LCI;
615 static const u8 rprt_type_civic = IEEE80211_SPCT_MSR_RPRT_TYPE_CIVIC;
620 lockdep_assert_held(&mvm->mutex);
622 ies = mgmt->u.action.u.ftm.variable;
623 ies_len = len - baselen;
625 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len,
626 &rprt_type_lci, 1, 4);
632 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len,
633 &rprt_type_civic, 1, 4);
636 civic_len = msr_ie[1];
639 entry = kmalloc(sizeof(*entry) + lci_len + civic_len, GFP_KERNEL);
643 memcpy(entry->addr, mgmt->bssid, ETH_ALEN);
645 entry->lci_len = lci_len;
647 memcpy(entry->buf, lci, lci_len);
649 entry->civic_len = civic_len;
651 memcpy(entry->buf + lci_len, civic, civic_len);
653 list_add_tail(&entry->list, &mvm->ftm_initiator.loc_list);