1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2015-2017 Intel Deutschland GmbH
4 * Copyright (C) 2018-2023 Intel Corporation
6 #include <net/cfg80211.h>
7 #include <linux/etherdevice.h>
11 struct iwl_mvm_pasn_sta {
12 struct list_head list;
13 struct iwl_mvm_int_sta int_sta;
16 /* must be last as it followed by buffer holding the key */
17 struct ieee80211_key_conf keyconf;
20 struct iwl_mvm_pasn_hltk_data {
26 static int iwl_mvm_ftm_responder_set_bw_v1(struct cfg80211_chan_def *chandef,
27 u8 *bw, u8 *ctrl_ch_position)
29 switch (chandef->width) {
30 case NL80211_CHAN_WIDTH_20_NOHT:
31 *bw = IWL_TOF_BW_20_LEGACY;
33 case NL80211_CHAN_WIDTH_20:
34 *bw = IWL_TOF_BW_20_HT;
36 case NL80211_CHAN_WIDTH_40:
38 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
40 case NL80211_CHAN_WIDTH_80:
42 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
51 static int iwl_mvm_ftm_responder_set_bw_v2(struct cfg80211_chan_def *chandef,
52 u8 *format_bw, u8 *ctrl_ch_position,
55 switch (chandef->width) {
56 case NL80211_CHAN_WIDTH_20_NOHT:
57 *format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY;
58 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
60 case NL80211_CHAN_WIDTH_20:
61 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
62 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
64 case NL80211_CHAN_WIDTH_40:
65 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
66 *format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS;
67 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
69 case NL80211_CHAN_WIDTH_80:
70 *format_bw = IWL_LOCATION_FRAME_FORMAT_VHT;
71 *format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS;
72 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
74 case NL80211_CHAN_WIDTH_160:
76 *format_bw = IWL_LOCATION_FRAME_FORMAT_HE;
77 *format_bw |= IWL_LOCATION_BW_160MHZ << LOCATION_BW_POS;
78 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
90 iwl_mvm_ftm_responder_set_ndp(struct iwl_mvm *mvm,
91 struct iwl_tof_responder_config_cmd_v9 *cmd)
93 /* Up to 2 R2I STS are allowed on the responder */
94 u32 r2i_max_sts = IWL_MVM_FTM_R2I_MAX_STS < 2 ?
95 IWL_MVM_FTM_R2I_MAX_STS : 1;
97 cmd->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP |
98 (r2i_max_sts << IWL_RESPONDER_STS_POS) |
99 (IWL_MVM_FTM_R2I_MAX_TOTAL_LTF << IWL_RESPONDER_TOTAL_LTF_POS);
100 cmd->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP |
101 (IWL_MVM_FTM_I2R_MAX_STS << IWL_RESPONDER_STS_POS) |
102 (IWL_MVM_FTM_I2R_MAX_TOTAL_LTF << IWL_RESPONDER_TOTAL_LTF_POS);
103 cmd->cmd_valid_fields |=
104 cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_NDP_PARAMS);
108 iwl_mvm_ftm_responder_cmd(struct iwl_mvm *mvm,
109 struct ieee80211_vif *vif,
110 struct cfg80211_chan_def *chandef,
111 struct ieee80211_bss_conf *link_conf)
113 u32 cmd_id = WIDE_ID(LOCATION_GROUP, TOF_RESPONDER_CONFIG_CMD);
114 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
116 * The command structure is the same for versions 6, 7 and 8 (only the
117 * field interpretation is different), so the same struct can be use
120 struct iwl_tof_responder_config_cmd_v9 cmd = {
121 .channel_num = chandef->chan->hw_value,
123 cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_CHAN_INFO |
124 IWL_TOF_RESPONDER_CMD_VALID_BSSID |
125 IWL_TOF_RESPONDER_CMD_VALID_STA_ID),
126 .sta_id = mvmvif->link[link_conf->link_id]->bcast_sta.sta_id,
128 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 6);
132 lockdep_assert_held(&mvm->mutex);
134 /* Use a default of bss_color=1 for now */
136 cmd.cmd_valid_fields |=
137 cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_BSS_COLOR |
138 IWL_TOF_RESPONDER_CMD_VALID_MIN_MAX_TIME_BETWEEN_MSR);
140 cmd.min_time_between_msr =
141 cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR);
142 cmd.max_time_between_msr =
143 cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR);
144 cmd_size = sizeof(struct iwl_tof_responder_config_cmd_v9);
146 /* All versions up to version 8 have the same size */
147 cmd_size = sizeof(struct iwl_tof_responder_config_cmd_v8);
151 iwl_mvm_ftm_responder_set_ndp(mvm, &cmd);
154 err = iwl_mvm_ftm_responder_set_bw_v2(chandef, &cmd.format_bw,
155 &cmd.ctrl_ch_position,
158 err = iwl_mvm_ftm_responder_set_bw_v1(chandef, &cmd.format_bw,
159 &cmd.ctrl_ch_position);
162 IWL_ERR(mvm, "Failed to set responder bandwidth\n");
166 memcpy(cmd.bssid, vif->addr, ETH_ALEN);
168 return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, cmd_size, &cmd);
172 iwl_mvm_ftm_responder_dyn_cfg_v2(struct iwl_mvm *mvm,
173 struct ieee80211_vif *vif,
174 struct ieee80211_ftm_responder_params *params)
176 struct iwl_tof_responder_dyn_config_cmd_v2 cmd = {
177 .lci_len = cpu_to_le32(params->lci_len + 2),
178 .civic_len = cpu_to_le32(params->civicloc_len + 2),
180 u8 data[IWL_LCI_CIVIC_IE_MAX_SIZE] = {0};
181 struct iwl_host_cmd hcmd = {
182 .id = WIDE_ID(LOCATION_GROUP, TOF_RESPONDER_DYN_CONFIG_CMD),
184 .len[0] = sizeof(cmd),
186 /* .len[1] set later */
187 /* may not be able to DMA from stack */
188 .dataflags[1] = IWL_HCMD_DFL_DUP,
190 u32 aligned_lci_len = ALIGN(params->lci_len + 2, 4);
191 u32 aligned_civicloc_len = ALIGN(params->civicloc_len + 2, 4);
194 lockdep_assert_held(&mvm->mutex);
196 if (aligned_lci_len + aligned_civicloc_len > sizeof(data)) {
197 IWL_ERR(mvm, "LCI/civicloc data too big (%zd + %zd)\n",
198 params->lci_len, params->civicloc_len);
202 pos[0] = WLAN_EID_MEASURE_REPORT;
203 pos[1] = params->lci_len;
204 memcpy(pos + 2, params->lci, params->lci_len);
206 pos += aligned_lci_len;
207 pos[0] = WLAN_EID_MEASURE_REPORT;
208 pos[1] = params->civicloc_len;
209 memcpy(pos + 2, params->civicloc, params->civicloc_len);
211 hcmd.len[1] = aligned_lci_len + aligned_civicloc_len;
213 return iwl_mvm_send_cmd(mvm, &hcmd);
217 iwl_mvm_ftm_responder_dyn_cfg_v3(struct iwl_mvm *mvm,
218 struct ieee80211_vif *vif,
219 struct ieee80211_ftm_responder_params *params,
220 struct iwl_mvm_pasn_hltk_data *hltk_data)
222 struct iwl_tof_responder_dyn_config_cmd cmd;
223 struct iwl_host_cmd hcmd = {
224 .id = WIDE_ID(LOCATION_GROUP, TOF_RESPONDER_DYN_CONFIG_CMD),
226 .len[0] = sizeof(cmd),
227 /* may not be able to DMA from stack */
228 .dataflags[0] = IWL_HCMD_DFL_DUP,
231 lockdep_assert_held(&mvm->mutex);
236 if (params->lci_len + 2 > sizeof(cmd.lci_buf) ||
237 params->civicloc_len + 2 > sizeof(cmd.civic_buf)) {
239 "LCI/civic data too big (lci=%zd, civic=%zd)\n",
240 params->lci_len, params->civicloc_len);
244 cmd.lci_buf[0] = WLAN_EID_MEASURE_REPORT;
245 cmd.lci_buf[1] = params->lci_len;
246 memcpy(cmd.lci_buf + 2, params->lci, params->lci_len);
247 cmd.lci_len = params->lci_len + 2;
249 cmd.civic_buf[0] = WLAN_EID_MEASURE_REPORT;
250 cmd.civic_buf[1] = params->civicloc_len;
251 memcpy(cmd.civic_buf + 2, params->civicloc,
252 params->civicloc_len);
253 cmd.civic_len = params->civicloc_len + 2;
255 cmd.valid_flags |= IWL_RESPONDER_DYN_CFG_VALID_LCI |
256 IWL_RESPONDER_DYN_CFG_VALID_CIVIC;
260 if (hltk_data->cipher > IWL_LOCATION_CIPHER_GCMP_256) {
261 IWL_ERR(mvm, "invalid cipher: %u\n",
266 cmd.cipher = hltk_data->cipher;
267 memcpy(cmd.addr, hltk_data->addr, sizeof(cmd.addr));
268 memcpy(cmd.hltk_buf, hltk_data->hltk, sizeof(cmd.hltk_buf));
269 cmd.valid_flags |= IWL_RESPONDER_DYN_CFG_VALID_PASN_STA;
272 return iwl_mvm_send_cmd(mvm, &hcmd);
276 iwl_mvm_ftm_responder_dyn_cfg_cmd(struct iwl_mvm *mvm,
277 struct ieee80211_vif *vif,
278 struct ieee80211_ftm_responder_params *params)
281 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
282 WIDE_ID(LOCATION_GROUP, TOF_RESPONDER_DYN_CONFIG_CMD),
287 ret = iwl_mvm_ftm_responder_dyn_cfg_v2(mvm, vif,
291 ret = iwl_mvm_ftm_responder_dyn_cfg_v3(mvm, vif,
295 IWL_ERR(mvm, "Unsupported DYN_CONFIG_CMD version %u\n",
303 static void iwl_mvm_resp_del_pasn_sta(struct iwl_mvm *mvm,
304 struct ieee80211_vif *vif,
305 struct iwl_mvm_pasn_sta *sta)
307 list_del(&sta->list);
309 if (sta->keyconf.keylen)
310 iwl_mvm_sec_key_del_pasn(mvm, vif, BIT(sta->int_sta.sta_id),
313 if (iwl_mvm_has_mld_api(mvm->fw))
314 iwl_mvm_mld_rm_sta_id(mvm, sta->int_sta.sta_id);
316 iwl_mvm_rm_sta_id(mvm, vif, sta->int_sta.sta_id);
318 iwl_mvm_dealloc_int_sta(mvm, &sta->int_sta);
322 int iwl_mvm_ftm_respoder_add_pasn_sta(struct iwl_mvm *mvm,
323 struct ieee80211_vif *vif,
324 u8 *addr, u32 cipher, u8 *tk, u32 tk_len,
325 u8 *hltk, u32 hltk_len)
328 struct iwl_mvm_pasn_sta *sta = NULL;
329 struct iwl_mvm_pasn_hltk_data hltk_data = {
333 struct iwl_mvm_pasn_hltk_data *hltk_data_ptr = NULL;
335 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
336 WIDE_ID(LOCATION_GROUP, TOF_RESPONDER_DYN_CONFIG_CMD),
339 lockdep_assert_held(&mvm->mutex);
342 IWL_ERR(mvm, "Adding PASN station not supported by FW\n");
346 if ((!hltk || !hltk_len) && (!tk || !tk_len)) {
347 IWL_ERR(mvm, "TK and HLTK not set\n");
351 if (hltk && hltk_len) {
352 if (!fw_has_capa(&mvm->fw->ucode_capa,
353 IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT)) {
354 IWL_ERR(mvm, "No support for secure LTF measurement\n");
358 hltk_data.cipher = iwl_mvm_cipher_to_location_cipher(cipher);
359 if (hltk_data.cipher == IWL_LOCATION_CIPHER_INVALID) {
360 IWL_ERR(mvm, "invalid cipher: %u\n", cipher);
364 hltk_data_ptr = &hltk_data;
368 sta = kzalloc(sizeof(*sta) + tk_len, GFP_KERNEL);
372 ret = iwl_mvm_add_pasn_sta(mvm, vif, &sta->int_sta, addr,
373 cipher, tk, tk_len, &sta->keyconf);
379 memcpy(sta->addr, addr, ETH_ALEN);
380 list_add_tail(&sta->list, &mvm->resp_pasn_list);
383 ret = iwl_mvm_ftm_responder_dyn_cfg_v3(mvm, vif, NULL, hltk_data_ptr);
385 iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
390 int iwl_mvm_ftm_resp_remove_pasn_sta(struct iwl_mvm *mvm,
391 struct ieee80211_vif *vif, u8 *addr)
393 struct iwl_mvm_pasn_sta *sta, *prev;
395 lockdep_assert_held(&mvm->mutex);
397 list_for_each_entry_safe(sta, prev, &mvm->resp_pasn_list, list) {
398 if (!memcmp(sta->addr, addr, ETH_ALEN)) {
399 iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
404 IWL_ERR(mvm, "FTM: PASN station %pM not found\n", addr);
408 int iwl_mvm_ftm_start_responder(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
409 struct ieee80211_bss_conf *bss_conf)
411 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
412 struct ieee80211_ftm_responder_params *params;
413 struct ieee80211_chanctx_conf ctx, *pctx;
415 struct iwl_mvm_phy_ctxt *phy_ctxt;
418 params = bss_conf->ftmr_params;
420 lockdep_assert_held(&mvm->mutex);
422 if (WARN_ON_ONCE(!bss_conf->ftm_responder))
425 if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
426 !mvmvif->ap_ibss_active) {
427 IWL_ERR(mvm, "Cannot start responder, not in AP mode\n");
432 pctx = rcu_dereference(bss_conf->chanctx_conf);
433 /* Copy the ctx to unlock the rcu and send the phy ctxt. We don't care
434 * about changes in the ctx after releasing the lock because the driver
435 * is still protected by the mutex. */
437 phy_ctxt_id = (u16 *)pctx->drv_priv;
440 phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
441 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx.def, &ctx.ap,
442 ctx.rx_chains_static,
443 ctx.rx_chains_dynamic);
447 ret = iwl_mvm_ftm_responder_cmd(mvm, vif, &ctx.def, bss_conf);
452 ret = iwl_mvm_ftm_responder_dyn_cfg_cmd(mvm, vif, params);
457 void iwl_mvm_ftm_responder_clear(struct iwl_mvm *mvm,
458 struct ieee80211_vif *vif)
460 struct iwl_mvm_pasn_sta *sta, *prev;
462 lockdep_assert_held(&mvm->mutex);
464 list_for_each_entry_safe(sta, prev, &mvm->resp_pasn_list, list)
465 iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
468 void iwl_mvm_ftm_restart_responder(struct iwl_mvm *mvm,
469 struct ieee80211_vif *vif,
470 struct ieee80211_bss_conf *bss_conf)
472 if (!bss_conf->ftm_responder)
475 iwl_mvm_ftm_responder_clear(mvm, vif);
476 iwl_mvm_ftm_start_responder(mvm, vif, bss_conf);
479 void iwl_mvm_ftm_responder_stats(struct iwl_mvm *mvm,
480 struct iwl_rx_cmd_buffer *rxb)
482 struct iwl_rx_packet *pkt = rxb_addr(rxb);
483 struct iwl_ftm_responder_stats *resp = (void *)pkt->data;
484 struct cfg80211_ftm_responder_stats *stats = &mvm->ftm_resp_stats;
485 u32 flags = le32_to_cpu(resp->flags);
487 if (resp->success_ftm == resp->ftm_per_burst)
488 stats->success_num++;
489 else if (resp->success_ftm >= 2)
490 stats->partial_num++;
494 if ((flags & FTM_RESP_STAT_ASAP_REQ) &&
495 (flags & FTM_RESP_STAT_ASAP_RESP))
498 if (flags & FTM_RESP_STAT_NON_ASAP_RESP)
499 stats->non_asap_num++;
501 stats->total_duration_ms += le32_to_cpu(resp->duration) / USEC_PER_MSEC;
503 if (flags & FTM_RESP_STAT_TRIGGER_UNKNOWN)
504 stats->unknown_triggers_num++;
506 if (flags & FTM_RESP_STAT_DUP)
507 stats->reschedule_requests_num++;
509 if (flags & FTM_RESP_STAT_NON_ASAP_OUT_WIN)
510 stats->out_of_window_triggers_num++;