xref: /freebsd/sys/contrib/dev/iwlwifi/mld/ftm-initiator.c (revision 6b627f88584ce13118e0a24951b503c0b1f2d5a7)
1*6b627f88SBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*6b627f88SBjoern A. Zeeb /*
3*6b627f88SBjoern A. Zeeb  * Copyright (C) 2025 Intel Corporation
4*6b627f88SBjoern A. Zeeb  */
5*6b627f88SBjoern A. Zeeb #include <linux/etherdevice.h>
6*6b627f88SBjoern A. Zeeb #include <linux/math64.h>
7*6b627f88SBjoern A. Zeeb #include <net/cfg80211.h>
8*6b627f88SBjoern A. Zeeb #include "mld.h"
9*6b627f88SBjoern A. Zeeb #include "iface.h"
10*6b627f88SBjoern A. Zeeb #include "phy.h"
11*6b627f88SBjoern A. Zeeb #include "iwl-io.h"
12*6b627f88SBjoern A. Zeeb #include "iwl-prph.h"
13*6b627f88SBjoern A. Zeeb #include "constants.h"
14*6b627f88SBjoern A. Zeeb #include "fw/api/location.h"
15*6b627f88SBjoern A. Zeeb #include "ftm-initiator.h"
16*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_cmd_common(struct iwl_mld * mld,struct ieee80211_vif * vif,struct iwl_tof_range_req_cmd * cmd,struct cfg80211_pmsr_request * req)17*6b627f88SBjoern A. Zeeb static void iwl_mld_ftm_cmd_common(struct iwl_mld *mld,
18*6b627f88SBjoern A. Zeeb 				   struct ieee80211_vif *vif,
19*6b627f88SBjoern A. Zeeb 				   struct iwl_tof_range_req_cmd *cmd,
20*6b627f88SBjoern A. Zeeb 				   struct cfg80211_pmsr_request *req)
21*6b627f88SBjoern A. Zeeb {
22*6b627f88SBjoern A. Zeeb 	int i;
23*6b627f88SBjoern A. Zeeb 
24*6b627f88SBjoern A. Zeeb 	cmd->initiator_flags =
25*6b627f88SBjoern A. Zeeb 		cpu_to_le32(IWL_TOF_INITIATOR_FLAGS_MACADDR_RANDOM |
26*6b627f88SBjoern A. Zeeb 			    IWL_TOF_INITIATOR_FLAGS_NON_ASAP_SUPPORT);
27*6b627f88SBjoern A. Zeeb 	cmd->request_id = req->cookie;
28*6b627f88SBjoern A. Zeeb 	cmd->num_of_ap = req->n_peers;
29*6b627f88SBjoern A. Zeeb 
30*6b627f88SBjoern A. Zeeb 	/* Use a large value for "no timeout". Don't use the maximum value
31*6b627f88SBjoern A. Zeeb 	 * because of fw limitations.
32*6b627f88SBjoern A. Zeeb 	 */
33*6b627f88SBjoern A. Zeeb 	if (req->timeout)
34*6b627f88SBjoern A. Zeeb 		cmd->req_timeout_ms = cpu_to_le32(min(req->timeout, 0xfffff));
35*6b627f88SBjoern A. Zeeb 	else
36*6b627f88SBjoern A. Zeeb 		cmd->req_timeout_ms = cpu_to_le32(0xfffff);
37*6b627f88SBjoern A. Zeeb 
38*6b627f88SBjoern A. Zeeb 	memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN);
39*6b627f88SBjoern A. Zeeb 	for (i = 0; i < ETH_ALEN; i++)
40*6b627f88SBjoern A. Zeeb 		cmd->macaddr_mask[i] = ~req->mac_addr_mask[i];
41*6b627f88SBjoern A. Zeeb 
42*6b627f88SBjoern A. Zeeb 	if (vif->cfg.assoc) {
43*6b627f88SBjoern A. Zeeb 		memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN);
44*6b627f88SBjoern A. Zeeb 
45*6b627f88SBjoern A. Zeeb 		/* AP's TSF is only relevant if associated */
46*6b627f88SBjoern A. Zeeb 		for (i = 0; i < req->n_peers; i++) {
47*6b627f88SBjoern A. Zeeb 			if (req->peers[i].report_ap_tsf) {
48*6b627f88SBjoern A. Zeeb 				struct iwl_mld_vif *mld_vif =
49*6b627f88SBjoern A. Zeeb 					iwl_mld_vif_from_mac80211(vif);
50*6b627f88SBjoern A. Zeeb 
51*6b627f88SBjoern A. Zeeb 				cmd->tsf_mac_id = cpu_to_le32(mld_vif->fw_id);
52*6b627f88SBjoern A. Zeeb 				return;
53*6b627f88SBjoern A. Zeeb 			}
54*6b627f88SBjoern A. Zeeb 		}
55*6b627f88SBjoern A. Zeeb 	} else {
56*6b627f88SBjoern A. Zeeb 		eth_broadcast_addr(cmd->range_req_bssid);
57*6b627f88SBjoern A. Zeeb 	}
58*6b627f88SBjoern A. Zeeb 
59*6b627f88SBjoern A. Zeeb 	/* Don't report AP's TSF */
60*6b627f88SBjoern A. Zeeb 	cmd->tsf_mac_id = cpu_to_le32(0xff);
61*6b627f88SBjoern A. Zeeb }
62*6b627f88SBjoern A. Zeeb 
63*6b627f88SBjoern A. Zeeb static int
iwl_mld_ftm_set_target_chandef(struct iwl_mld * mld,struct cfg80211_pmsr_request_peer * peer,struct iwl_tof_range_req_ap_entry * target)64*6b627f88SBjoern A. Zeeb iwl_mld_ftm_set_target_chandef(struct iwl_mld *mld,
65*6b627f88SBjoern A. Zeeb 			       struct cfg80211_pmsr_request_peer *peer,
66*6b627f88SBjoern A. Zeeb 			       struct iwl_tof_range_req_ap_entry *target)
67*6b627f88SBjoern A. Zeeb {
68*6b627f88SBjoern A. Zeeb 	u32 freq = peer->chandef.chan->center_freq;
69*6b627f88SBjoern A. Zeeb 
70*6b627f88SBjoern A. Zeeb 	target->channel_num = ieee80211_frequency_to_channel(freq);
71*6b627f88SBjoern A. Zeeb 
72*6b627f88SBjoern A. Zeeb 	switch (peer->chandef.width) {
73*6b627f88SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20_NOHT:
74*6b627f88SBjoern A. Zeeb 		target->format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY;
75*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
76*6b627f88SBjoern A. Zeeb 		break;
77*6b627f88SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_20:
78*6b627f88SBjoern A. Zeeb 		target->format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
79*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
80*6b627f88SBjoern A. Zeeb 		break;
81*6b627f88SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_40:
82*6b627f88SBjoern A. Zeeb 		target->format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
83*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS;
84*6b627f88SBjoern A. Zeeb 		break;
85*6b627f88SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_80:
86*6b627f88SBjoern A. Zeeb 		target->format_bw = IWL_LOCATION_FRAME_FORMAT_VHT;
87*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS;
88*6b627f88SBjoern A. Zeeb 		break;
89*6b627f88SBjoern A. Zeeb 	case NL80211_CHAN_WIDTH_160:
90*6b627f88SBjoern A. Zeeb 		target->format_bw = IWL_LOCATION_FRAME_FORMAT_HE;
91*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_BW_160MHZ << LOCATION_BW_POS;
92*6b627f88SBjoern A. Zeeb 		break;
93*6b627f88SBjoern A. Zeeb 	default:
94*6b627f88SBjoern A. Zeeb 		IWL_ERR(mld, "Unsupported BW in FTM request (%d)\n",
95*6b627f88SBjoern A. Zeeb 			peer->chandef.width);
96*6b627f88SBjoern A. Zeeb 		return -EINVAL;
97*6b627f88SBjoern A. Zeeb 	}
98*6b627f88SBjoern A. Zeeb 
99*6b627f88SBjoern A. Zeeb 	/* non EDCA based measurement must use HE preamble */
100*6b627f88SBjoern A. Zeeb 	if (peer->ftm.trigger_based || peer->ftm.non_trigger_based)
101*6b627f88SBjoern A. Zeeb 		target->format_bw |= IWL_LOCATION_FRAME_FORMAT_HE;
102*6b627f88SBjoern A. Zeeb 
103*6b627f88SBjoern A. Zeeb 	target->ctrl_ch_position =
104*6b627f88SBjoern A. Zeeb 		(peer->chandef.width > NL80211_CHAN_WIDTH_20) ?
105*6b627f88SBjoern A. Zeeb 		iwl_mld_get_fw_ctrl_pos(&peer->chandef) : 0;
106*6b627f88SBjoern A. Zeeb 
107*6b627f88SBjoern A. Zeeb 	target->band = iwl_mld_nl80211_band_to_fw(peer->chandef.chan->band);
108*6b627f88SBjoern A. Zeeb 	return 0;
109*6b627f88SBjoern A. Zeeb }
110*6b627f88SBjoern A. Zeeb 
111*6b627f88SBjoern A. Zeeb #define FTM_SET_FLAG(flag) (target->initiator_ap_flags |= \
112*6b627f88SBjoern A. Zeeb 			    cpu_to_le32(IWL_INITIATOR_AP_FLAGS_##flag))
113*6b627f88SBjoern A. Zeeb 
114*6b627f88SBjoern A. Zeeb static void
iwl_mld_ftm_set_target_flags(struct iwl_mld * mld,struct cfg80211_pmsr_request_peer * peer,struct iwl_tof_range_req_ap_entry * target)115*6b627f88SBjoern A. Zeeb iwl_mld_ftm_set_target_flags(struct iwl_mld *mld,
116*6b627f88SBjoern A. Zeeb 			     struct cfg80211_pmsr_request_peer *peer,
117*6b627f88SBjoern A. Zeeb 			     struct iwl_tof_range_req_ap_entry *target)
118*6b627f88SBjoern A. Zeeb {
119*6b627f88SBjoern A. Zeeb 	target->initiator_ap_flags = cpu_to_le32(0);
120*6b627f88SBjoern A. Zeeb 
121*6b627f88SBjoern A. Zeeb 	if (peer->ftm.asap)
122*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(ASAP);
123*6b627f88SBjoern A. Zeeb 
124*6b627f88SBjoern A. Zeeb 	if (peer->ftm.request_lci)
125*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(LCI_REQUEST);
126*6b627f88SBjoern A. Zeeb 
127*6b627f88SBjoern A. Zeeb 	if (peer->ftm.request_civicloc)
128*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(CIVIC_REQUEST);
129*6b627f88SBjoern A. Zeeb 
130*6b627f88SBjoern A. Zeeb 	if (IWL_MLD_FTM_INITIATOR_DYNACK)
131*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(DYN_ACK);
132*6b627f88SBjoern A. Zeeb 
133*6b627f88SBjoern A. Zeeb 	if (IWL_MLD_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_LINEAR_REG)
134*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(ALGO_LR);
135*6b627f88SBjoern A. Zeeb 	else if (IWL_MLD_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_FFT)
136*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(ALGO_FFT);
137*6b627f88SBjoern A. Zeeb 
138*6b627f88SBjoern A. Zeeb 	if (peer->ftm.trigger_based)
139*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(TB);
140*6b627f88SBjoern A. Zeeb 	else if (peer->ftm.non_trigger_based)
141*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(NON_TB);
142*6b627f88SBjoern A. Zeeb 
143*6b627f88SBjoern A. Zeeb 	if ((peer->ftm.trigger_based || peer->ftm.non_trigger_based) &&
144*6b627f88SBjoern A. Zeeb 	    peer->ftm.lmr_feedback)
145*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(LMR_FEEDBACK);
146*6b627f88SBjoern A. Zeeb }
147*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_set_sta(struct iwl_mld * mld,struct ieee80211_vif * vif,struct cfg80211_pmsr_request_peer * peer,struct iwl_tof_range_req_ap_entry * target)148*6b627f88SBjoern A. Zeeb static void iwl_mld_ftm_set_sta(struct iwl_mld *mld, struct ieee80211_vif *vif,
149*6b627f88SBjoern A. Zeeb 				struct cfg80211_pmsr_request_peer *peer,
150*6b627f88SBjoern A. Zeeb 				struct iwl_tof_range_req_ap_entry *target)
151*6b627f88SBjoern A. Zeeb {
152*6b627f88SBjoern A. Zeeb 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
153*6b627f88SBjoern A. Zeeb 	u32 sta_id_mask;
154*6b627f88SBjoern A. Zeeb 
155*6b627f88SBjoern A. Zeeb 	target->sta_id = IWL_INVALID_STA;
156*6b627f88SBjoern A. Zeeb 
157*6b627f88SBjoern A. Zeeb 	/* TODO: add ftm_unprotected debugfs support */
158*6b627f88SBjoern A. Zeeb 
159*6b627f88SBjoern A. Zeeb 	if (!vif->cfg.assoc || !mld_vif->ap_sta)
160*6b627f88SBjoern A. Zeeb 		return;
161*6b627f88SBjoern A. Zeeb 
162*6b627f88SBjoern A. Zeeb 	sta_id_mask = iwl_mld_fw_sta_id_mask(mld, mld_vif->ap_sta);
163*6b627f88SBjoern A. Zeeb 	if (WARN_ON(hweight32(sta_id_mask) != 1))
164*6b627f88SBjoern A. Zeeb 		return;
165*6b627f88SBjoern A. Zeeb 
166*6b627f88SBjoern A. Zeeb 	target->sta_id = __ffs(sta_id_mask);
167*6b627f88SBjoern A. Zeeb 
168*6b627f88SBjoern A. Zeeb 	if (mld_vif->ap_sta->mfp &&
169*6b627f88SBjoern A. Zeeb 	    (peer->ftm.trigger_based || peer->ftm.non_trigger_based))
170*6b627f88SBjoern A. Zeeb 		FTM_SET_FLAG(PMF);
171*6b627f88SBjoern A. Zeeb }
172*6b627f88SBjoern A. Zeeb 
173*6b627f88SBjoern A. Zeeb static int
iwl_mld_ftm_set_target(struct iwl_mld * mld,struct ieee80211_vif * vif,struct cfg80211_pmsr_request_peer * peer,struct iwl_tof_range_req_ap_entry * target)174*6b627f88SBjoern A. Zeeb iwl_mld_ftm_set_target(struct iwl_mld *mld, struct ieee80211_vif *vif,
175*6b627f88SBjoern A. Zeeb 		       struct cfg80211_pmsr_request_peer *peer,
176*6b627f88SBjoern A. Zeeb 		       struct iwl_tof_range_req_ap_entry *target)
177*6b627f88SBjoern A. Zeeb {
178*6b627f88SBjoern A. Zeeb 	u32 i2r_max_sts;
179*6b627f88SBjoern A. Zeeb 	int ret;
180*6b627f88SBjoern A. Zeeb 
181*6b627f88SBjoern A. Zeeb 	ret = iwl_mld_ftm_set_target_chandef(mld, peer, target);
182*6b627f88SBjoern A. Zeeb 	if (ret)
183*6b627f88SBjoern A. Zeeb 		return ret;
184*6b627f88SBjoern A. Zeeb 
185*6b627f88SBjoern A. Zeeb 	memcpy(target->bssid, peer->addr, ETH_ALEN);
186*6b627f88SBjoern A. Zeeb 	target->burst_period = cpu_to_le16(peer->ftm.burst_period);
187*6b627f88SBjoern A. Zeeb 	target->samples_per_burst = peer->ftm.ftms_per_burst;
188*6b627f88SBjoern A. Zeeb 	target->num_of_bursts = peer->ftm.num_bursts_exp;
189*6b627f88SBjoern A. Zeeb 	iwl_mld_ftm_set_target_flags(mld, peer, target);
190*6b627f88SBjoern A. Zeeb 	iwl_mld_ftm_set_sta(mld, vif, peer, target);
191*6b627f88SBjoern A. Zeeb 
192*6b627f88SBjoern A. Zeeb 	/* TODO: add secured ranging support */
193*6b627f88SBjoern A. Zeeb 
194*6b627f88SBjoern A. Zeeb 	i2r_max_sts = IWL_MLD_FTM_I2R_MAX_STS > 1 ? 1 :
195*6b627f88SBjoern A. Zeeb 		IWL_MLD_FTM_I2R_MAX_STS;
196*6b627f88SBjoern A. Zeeb 
197*6b627f88SBjoern A. Zeeb 	target->r2i_ndp_params = IWL_MLD_FTM_R2I_MAX_REP |
198*6b627f88SBjoern A. Zeeb 		(IWL_MLD_FTM_R2I_MAX_STS << IWL_LOCATION_MAX_STS_POS) |
199*6b627f88SBjoern A. Zeeb 		(IWL_MLD_FTM_R2I_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS);
200*6b627f88SBjoern A. Zeeb 	target->i2r_ndp_params = IWL_MLD_FTM_I2R_MAX_REP |
201*6b627f88SBjoern A. Zeeb 		(i2r_max_sts << IWL_LOCATION_MAX_STS_POS) |
202*6b627f88SBjoern A. Zeeb 		(IWL_MLD_FTM_I2R_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS);
203*6b627f88SBjoern A. Zeeb 
204*6b627f88SBjoern A. Zeeb 	if (peer->ftm.non_trigger_based) {
205*6b627f88SBjoern A. Zeeb 		target->min_time_between_msr =
206*6b627f88SBjoern A. Zeeb 			cpu_to_le16(IWL_MLD_FTM_NON_TB_MIN_TIME_BETWEEN_MSR);
207*6b627f88SBjoern A. Zeeb 		target->burst_period =
208*6b627f88SBjoern A. Zeeb 			cpu_to_le16(IWL_MLD_FTM_NON_TB_MAX_TIME_BETWEEN_MSR);
209*6b627f88SBjoern A. Zeeb 	} else {
210*6b627f88SBjoern A. Zeeb 		target->min_time_between_msr = cpu_to_le16(0);
211*6b627f88SBjoern A. Zeeb 	}
212*6b627f88SBjoern A. Zeeb 
213*6b627f88SBjoern A. Zeeb 	/* TODO: Beacon interval is currently unknown, so use the common value
214*6b627f88SBjoern A. Zeeb 	 * of 100 TUs.
215*6b627f88SBjoern A. Zeeb 	 */
216*6b627f88SBjoern A. Zeeb 	target->beacon_interval = cpu_to_le16(100);
217*6b627f88SBjoern A. Zeeb 
218*6b627f88SBjoern A. Zeeb 	return 0;
219*6b627f88SBjoern A. Zeeb }
220*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_start(struct iwl_mld * mld,struct ieee80211_vif * vif,struct cfg80211_pmsr_request * req)221*6b627f88SBjoern A. Zeeb int iwl_mld_ftm_start(struct iwl_mld *mld, struct ieee80211_vif *vif,
222*6b627f88SBjoern A. Zeeb 		      struct cfg80211_pmsr_request *req)
223*6b627f88SBjoern A. Zeeb {
224*6b627f88SBjoern A. Zeeb 	struct iwl_tof_range_req_cmd cmd;
225*6b627f88SBjoern A. Zeeb 	struct iwl_host_cmd hcmd = {
226*6b627f88SBjoern A. Zeeb 		.id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD),
227*6b627f88SBjoern A. Zeeb 		.dataflags[0] = IWL_HCMD_DFL_DUP,
228*6b627f88SBjoern A. Zeeb 		.data[0] = &cmd,
229*6b627f88SBjoern A. Zeeb 		.len[0] = sizeof(cmd),
230*6b627f88SBjoern A. Zeeb 	};
231*6b627f88SBjoern A. Zeeb 	u8 i;
232*6b627f88SBjoern A. Zeeb 	int ret;
233*6b627f88SBjoern A. Zeeb 
234*6b627f88SBjoern A. Zeeb 	lockdep_assert_wiphy(mld->wiphy);
235*6b627f88SBjoern A. Zeeb 
236*6b627f88SBjoern A. Zeeb 	if (mld->ftm_initiator.req)
237*6b627f88SBjoern A. Zeeb 		return -EBUSY;
238*6b627f88SBjoern A. Zeeb 
239*6b627f88SBjoern A. Zeeb 	if (req->n_peers > ARRAY_SIZE(cmd.ap))
240*6b627f88SBjoern A. Zeeb 		return -EINVAL;
241*6b627f88SBjoern A. Zeeb 
242*6b627f88SBjoern A. Zeeb 	memset(&cmd, 0, sizeof(cmd));
243*6b627f88SBjoern A. Zeeb 
244*6b627f88SBjoern A. Zeeb 	iwl_mld_ftm_cmd_common(mld, vif, (void *)&cmd, req);
245*6b627f88SBjoern A. Zeeb 
246*6b627f88SBjoern A. Zeeb 	for (i = 0; i < cmd.num_of_ap; i++) {
247*6b627f88SBjoern A. Zeeb 		struct cfg80211_pmsr_request_peer *peer = &req->peers[i];
248*6b627f88SBjoern A. Zeeb 		struct iwl_tof_range_req_ap_entry *target = &cmd.ap[i];
249*6b627f88SBjoern A. Zeeb 
250*6b627f88SBjoern A. Zeeb 		ret = iwl_mld_ftm_set_target(mld, vif, peer, target);
251*6b627f88SBjoern A. Zeeb 		if (ret)
252*6b627f88SBjoern A. Zeeb 			return ret;
253*6b627f88SBjoern A. Zeeb 	}
254*6b627f88SBjoern A. Zeeb 
255*6b627f88SBjoern A. Zeeb 	/* TODO: get the status from the response*/
256*6b627f88SBjoern A. Zeeb 	ret = iwl_mld_send_cmd(mld, &hcmd);
257*6b627f88SBjoern A. Zeeb 	if (!ret) {
258*6b627f88SBjoern A. Zeeb 		mld->ftm_initiator.req = req;
259*6b627f88SBjoern A. Zeeb 		mld->ftm_initiator.req_wdev = ieee80211_vif_to_wdev(vif);
260*6b627f88SBjoern A. Zeeb 	}
261*6b627f88SBjoern A. Zeeb 
262*6b627f88SBjoern A. Zeeb 	return ret;
263*6b627f88SBjoern A. Zeeb }
264*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_reset(struct iwl_mld * mld)265*6b627f88SBjoern A. Zeeb static void iwl_mld_ftm_reset(struct iwl_mld *mld)
266*6b627f88SBjoern A. Zeeb {
267*6b627f88SBjoern A. Zeeb 	lockdep_assert_wiphy(mld->wiphy);
268*6b627f88SBjoern A. Zeeb 
269*6b627f88SBjoern A. Zeeb 	mld->ftm_initiator.req = NULL;
270*6b627f88SBjoern A. Zeeb 	mld->ftm_initiator.req_wdev = NULL;
271*6b627f88SBjoern A. Zeeb 	memset(mld->ftm_initiator.responses, 0,
272*6b627f88SBjoern A. Zeeb 	       sizeof(mld->ftm_initiator.responses));
273*6b627f88SBjoern A. Zeeb }
274*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_range_resp_valid(struct iwl_mld * mld,u8 request_id,u8 num_of_aps)275*6b627f88SBjoern A. Zeeb static int iwl_mld_ftm_range_resp_valid(struct iwl_mld *mld, u8 request_id,
276*6b627f88SBjoern A. Zeeb 					u8 num_of_aps)
277*6b627f88SBjoern A. Zeeb {
278*6b627f88SBjoern A. Zeeb 	if (IWL_FW_CHECK(mld, request_id != (u8)mld->ftm_initiator.req->cookie,
279*6b627f88SBjoern A. Zeeb 			 "Request ID mismatch, got %u, active %u\n",
280*6b627f88SBjoern A. Zeeb 			 request_id, (u8)mld->ftm_initiator.req->cookie))
281*6b627f88SBjoern A. Zeeb 		return -EINVAL;
282*6b627f88SBjoern A. Zeeb 
283*6b627f88SBjoern A. Zeeb 	if (IWL_FW_CHECK(mld, num_of_aps > mld->ftm_initiator.req->n_peers ||
284*6b627f88SBjoern A. Zeeb 			 num_of_aps > IWL_TOF_MAX_APS,
285*6b627f88SBjoern A. Zeeb 			 "FTM range response: invalid num of APs (%u)\n",
286*6b627f88SBjoern A. Zeeb 			 num_of_aps))
287*6b627f88SBjoern A. Zeeb 		return -EINVAL;
288*6b627f88SBjoern A. Zeeb 
289*6b627f88SBjoern A. Zeeb 	return 0;
290*6b627f88SBjoern A. Zeeb }
291*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_find_peer(struct cfg80211_pmsr_request * req,const u8 * addr)292*6b627f88SBjoern A. Zeeb static int iwl_mld_ftm_find_peer(struct cfg80211_pmsr_request *req,
293*6b627f88SBjoern A. Zeeb 				 const u8 *addr)
294*6b627f88SBjoern A. Zeeb {
295*6b627f88SBjoern A. Zeeb 	for (int i = 0; i < req->n_peers; i++) {
296*6b627f88SBjoern A. Zeeb 		struct cfg80211_pmsr_request_peer *peer = &req->peers[i];
297*6b627f88SBjoern A. Zeeb 
298*6b627f88SBjoern A. Zeeb 		if (ether_addr_equal_unaligned(peer->addr, addr))
299*6b627f88SBjoern A. Zeeb 			return i;
300*6b627f88SBjoern A. Zeeb 	}
301*6b627f88SBjoern A. Zeeb 
302*6b627f88SBjoern A. Zeeb 	return -ENOENT;
303*6b627f88SBjoern A. Zeeb }
304*6b627f88SBjoern A. Zeeb 
iwl_mld_debug_range_resp(struct iwl_mld * mld,u8 index,struct cfg80211_pmsr_result * res)305*6b627f88SBjoern A. Zeeb static void iwl_mld_debug_range_resp(struct iwl_mld *mld, u8 index,
306*6b627f88SBjoern A. Zeeb 				     struct cfg80211_pmsr_result *res)
307*6b627f88SBjoern A. Zeeb {
308*6b627f88SBjoern A. Zeeb 	s64 rtt_avg = div_s64(res->ftm.rtt_avg * 100, 6666);
309*6b627f88SBjoern A. Zeeb 
310*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "entry %d\n", index);
311*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\tstatus: %d\n", res->status);
312*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\tBSSID: %pM\n", res->addr);
313*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\thost time: %llu\n", res->host_time);
314*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\tburst index: %d\n", res->ftm.burst_index);
315*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\tsuccess num: %u\n", res->ftm.num_ftmr_successes);
316*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\trssi: %d\n", res->ftm.rssi_avg);
317*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\trssi spread: %d\n", res->ftm.rssi_spread);
318*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\trtt: %lld\n", res->ftm.rtt_avg);
319*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\trtt var: %llu\n", res->ftm.rtt_variance);
320*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\trtt spread: %llu\n", res->ftm.rtt_spread);
321*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "\tdistance: %lld\n", rtt_avg);
322*6b627f88SBjoern A. Zeeb }
323*6b627f88SBjoern A. Zeeb 
iwl_mld_handle_ftm_resp_notif(struct iwl_mld * mld,struct iwl_rx_packet * pkt)324*6b627f88SBjoern A. Zeeb void iwl_mld_handle_ftm_resp_notif(struct iwl_mld *mld,
325*6b627f88SBjoern A. Zeeb 				   struct iwl_rx_packet *pkt)
326*6b627f88SBjoern A. Zeeb {
327*6b627f88SBjoern A. Zeeb 	struct iwl_tof_range_rsp_ntfy *fw_resp = (void *)pkt->data;
328*6b627f88SBjoern A. Zeeb 	u8 num_of_aps, last_in_batch;
329*6b627f88SBjoern A. Zeeb 
330*6b627f88SBjoern A. Zeeb 	if (IWL_FW_CHECK(mld, !mld->ftm_initiator.req,
331*6b627f88SBjoern A. Zeeb 			 "FTM response without a pending request\n"))
332*6b627f88SBjoern A. Zeeb 		return;
333*6b627f88SBjoern A. Zeeb 
334*6b627f88SBjoern A. Zeeb 	if (iwl_mld_ftm_range_resp_valid(mld, fw_resp->request_id,
335*6b627f88SBjoern A. Zeeb 					 fw_resp->num_of_aps))
336*6b627f88SBjoern A. Zeeb 		return;
337*6b627f88SBjoern A. Zeeb 
338*6b627f88SBjoern A. Zeeb 	num_of_aps = fw_resp->num_of_aps;
339*6b627f88SBjoern A. Zeeb 	last_in_batch = fw_resp->last_report;
340*6b627f88SBjoern A. Zeeb 
341*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "Range response received\n");
342*6b627f88SBjoern A. Zeeb 	IWL_DEBUG_INFO(mld, "request id: %llu, num of entries: %u\n",
343*6b627f88SBjoern A. Zeeb 		       mld->ftm_initiator.req->cookie, num_of_aps);
344*6b627f88SBjoern A. Zeeb 
345*6b627f88SBjoern A. Zeeb 	for (int i = 0; i < num_of_aps; i++) {
346*6b627f88SBjoern A. Zeeb 		struct cfg80211_pmsr_result result = {};
347*6b627f88SBjoern A. Zeeb 		struct iwl_tof_range_rsp_ap_entry_ntfy *fw_ap;
348*6b627f88SBjoern A. Zeeb 		int peer_idx;
349*6b627f88SBjoern A. Zeeb 
350*6b627f88SBjoern A. Zeeb 		fw_ap = &fw_resp->ap[i];
351*6b627f88SBjoern A. Zeeb 		result.final = fw_ap->last_burst;
352*6b627f88SBjoern A. Zeeb 		result.ap_tsf = le32_to_cpu(fw_ap->start_tsf);
353*6b627f88SBjoern A. Zeeb 		result.ap_tsf_valid = 1;
354*6b627f88SBjoern A. Zeeb 
355*6b627f88SBjoern A. Zeeb 		peer_idx = iwl_mld_ftm_find_peer(mld->ftm_initiator.req,
356*6b627f88SBjoern A. Zeeb 						 fw_ap->bssid);
357*6b627f88SBjoern A. Zeeb 		if (peer_idx < 0) {
358*6b627f88SBjoern A. Zeeb 			IWL_WARN(mld,
359*6b627f88SBjoern A. Zeeb 				 "Unknown address (%pM, target #%d) in FTM response\n",
360*6b627f88SBjoern A. Zeeb 				 fw_ap->bssid, i);
361*6b627f88SBjoern A. Zeeb 			continue;
362*6b627f88SBjoern A. Zeeb 		}
363*6b627f88SBjoern A. Zeeb 
364*6b627f88SBjoern A. Zeeb 		switch (fw_ap->measure_status) {
365*6b627f88SBjoern A. Zeeb 		case IWL_TOF_ENTRY_SUCCESS:
366*6b627f88SBjoern A. Zeeb 			result.status = NL80211_PMSR_STATUS_SUCCESS;
367*6b627f88SBjoern A. Zeeb 			break;
368*6b627f88SBjoern A. Zeeb 		case IWL_TOF_ENTRY_TIMING_MEASURE_TIMEOUT:
369*6b627f88SBjoern A. Zeeb 			result.status = NL80211_PMSR_STATUS_TIMEOUT;
370*6b627f88SBjoern A. Zeeb 			break;
371*6b627f88SBjoern A. Zeeb 		case IWL_TOF_ENTRY_NO_RESPONSE:
372*6b627f88SBjoern A. Zeeb 			result.status = NL80211_PMSR_STATUS_FAILURE;
373*6b627f88SBjoern A. Zeeb 			result.ftm.failure_reason =
374*6b627f88SBjoern A. Zeeb 				NL80211_PMSR_FTM_FAILURE_NO_RESPONSE;
375*6b627f88SBjoern A. Zeeb 			break;
376*6b627f88SBjoern A. Zeeb 		case IWL_TOF_ENTRY_REQUEST_REJECTED:
377*6b627f88SBjoern A. Zeeb 			result.status = NL80211_PMSR_STATUS_FAILURE;
378*6b627f88SBjoern A. Zeeb 			result.ftm.failure_reason =
379*6b627f88SBjoern A. Zeeb 				NL80211_PMSR_FTM_FAILURE_PEER_BUSY;
380*6b627f88SBjoern A. Zeeb 			result.ftm.busy_retry_time = fw_ap->refusal_period;
381*6b627f88SBjoern A. Zeeb 			break;
382*6b627f88SBjoern A. Zeeb 		default:
383*6b627f88SBjoern A. Zeeb 			result.status = NL80211_PMSR_STATUS_FAILURE;
384*6b627f88SBjoern A. Zeeb 			result.ftm.failure_reason =
385*6b627f88SBjoern A. Zeeb 				NL80211_PMSR_FTM_FAILURE_UNSPECIFIED;
386*6b627f88SBjoern A. Zeeb 			break;
387*6b627f88SBjoern A. Zeeb 		}
388*6b627f88SBjoern A. Zeeb 		memcpy(result.addr, fw_ap->bssid, ETH_ALEN);
389*6b627f88SBjoern A. Zeeb 
390*6b627f88SBjoern A. Zeeb 		/* TODO: convert the timestamp from the result to systime */
391*6b627f88SBjoern A. Zeeb 		result.host_time = ktime_get_boottime_ns();
392*6b627f88SBjoern A. Zeeb 
393*6b627f88SBjoern A. Zeeb 		result.type = NL80211_PMSR_TYPE_FTM;
394*6b627f88SBjoern A. Zeeb 		result.ftm.burst_index = mld->ftm_initiator.responses[peer_idx];
395*6b627f88SBjoern A. Zeeb 		mld->ftm_initiator.responses[peer_idx]++;
396*6b627f88SBjoern A. Zeeb 		result.ftm.rssi_avg = fw_ap->rssi;
397*6b627f88SBjoern A. Zeeb 		result.ftm.rssi_avg_valid = 1;
398*6b627f88SBjoern A. Zeeb 		result.ftm.rssi_spread = fw_ap->rssi_spread;
399*6b627f88SBjoern A. Zeeb 		result.ftm.rssi_spread_valid = 1;
400*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_avg = (s32)le32_to_cpu(fw_ap->rtt);
401*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_avg_valid = 1;
402*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_variance = le32_to_cpu(fw_ap->rtt_variance);
403*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_variance_valid = 1;
404*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_spread = le32_to_cpu(fw_ap->rtt_spread);
405*6b627f88SBjoern A. Zeeb 		result.ftm.rtt_spread_valid = 1;
406*6b627f88SBjoern A. Zeeb 
407*6b627f88SBjoern A. Zeeb 		cfg80211_pmsr_report(mld->ftm_initiator.req_wdev,
408*6b627f88SBjoern A. Zeeb 				     mld->ftm_initiator.req,
409*6b627f88SBjoern A. Zeeb 				     &result, GFP_KERNEL);
410*6b627f88SBjoern A. Zeeb 
411*6b627f88SBjoern A. Zeeb 		if (fw_has_api(&mld->fw->ucode_capa,
412*6b627f88SBjoern A. Zeeb 			       IWL_UCODE_TLV_API_FTM_RTT_ACCURACY))
413*6b627f88SBjoern A. Zeeb 			IWL_DEBUG_INFO(mld, "RTT confidence: %u\n",
414*6b627f88SBjoern A. Zeeb 				       fw_ap->rttConfidence);
415*6b627f88SBjoern A. Zeeb 
416*6b627f88SBjoern A. Zeeb 		iwl_mld_debug_range_resp(mld, i, &result);
417*6b627f88SBjoern A. Zeeb 	}
418*6b627f88SBjoern A. Zeeb 
419*6b627f88SBjoern A. Zeeb 	if (last_in_batch) {
420*6b627f88SBjoern A. Zeeb 		cfg80211_pmsr_complete(mld->ftm_initiator.req_wdev,
421*6b627f88SBjoern A. Zeeb 				       mld->ftm_initiator.req,
422*6b627f88SBjoern A. Zeeb 				       GFP_KERNEL);
423*6b627f88SBjoern A. Zeeb 		iwl_mld_ftm_reset(mld);
424*6b627f88SBjoern A. Zeeb 	}
425*6b627f88SBjoern A. Zeeb }
426*6b627f88SBjoern A. Zeeb 
iwl_mld_ftm_restart_cleanup(struct iwl_mld * mld)427*6b627f88SBjoern A. Zeeb void iwl_mld_ftm_restart_cleanup(struct iwl_mld *mld)
428*6b627f88SBjoern A. Zeeb {
429*6b627f88SBjoern A. Zeeb 	struct cfg80211_pmsr_result result = {
430*6b627f88SBjoern A. Zeeb 		.status = NL80211_PMSR_STATUS_FAILURE,
431*6b627f88SBjoern A. Zeeb 		.final = 1,
432*6b627f88SBjoern A. Zeeb 		.host_time = ktime_get_boottime_ns(),
433*6b627f88SBjoern A. Zeeb 		.type = NL80211_PMSR_TYPE_FTM,
434*6b627f88SBjoern A. Zeeb 	};
435*6b627f88SBjoern A. Zeeb 
436*6b627f88SBjoern A. Zeeb 	if (!mld->ftm_initiator.req)
437*6b627f88SBjoern A. Zeeb 		return;
438*6b627f88SBjoern A. Zeeb 
439*6b627f88SBjoern A. Zeeb 	for (int i = 0; i < mld->ftm_initiator.req->n_peers; i++) {
440*6b627f88SBjoern A. Zeeb 		memcpy(result.addr, mld->ftm_initiator.req->peers[i].addr,
441*6b627f88SBjoern A. Zeeb 		       ETH_ALEN);
442*6b627f88SBjoern A. Zeeb 
443*6b627f88SBjoern A. Zeeb 		cfg80211_pmsr_report(mld->ftm_initiator.req_wdev,
444*6b627f88SBjoern A. Zeeb 				     mld->ftm_initiator.req,
445*6b627f88SBjoern A. Zeeb 				     &result, GFP_KERNEL);
446*6b627f88SBjoern A. Zeeb 	}
447*6b627f88SBjoern A. Zeeb 
448*6b627f88SBjoern A. Zeeb 	cfg80211_pmsr_complete(mld->ftm_initiator.req_wdev,
449*6b627f88SBjoern A. Zeeb 			       mld->ftm_initiator.req, GFP_KERNEL);
450*6b627f88SBjoern A. Zeeb 	iwl_mld_ftm_reset(mld);
451*6b627f88SBjoern A. Zeeb }
452