1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60#include <net/cfg80211.h>
61#include <linux/etherdevice.h>
62#include "mvm.h"
63#include "constants.h"
64
65static int iwl_mvm_ftm_responder_set_bw_v1(struct cfg80211_chan_def *chandef,
66 u8 *bw, u8 *ctrl_ch_position)
67{
68 switch (chandef->width) {
69 case NL80211_CHAN_WIDTH_20_NOHT:
70 *bw = IWL_TOF_BW_20_LEGACY;
71 break;
72 case NL80211_CHAN_WIDTH_20:
73 *bw = IWL_TOF_BW_20_HT;
74 break;
75 case NL80211_CHAN_WIDTH_40:
76 *bw = IWL_TOF_BW_40;
77 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
78 break;
79 case NL80211_CHAN_WIDTH_80:
80 *bw = IWL_TOF_BW_80;
81 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
82 break;
83 default:
84 return -ENOTSUPP;
85 }
86
87 return 0;
88}
89
90static int iwl_mvm_ftm_responder_set_bw_v2(struct cfg80211_chan_def *chandef,
91 u8 *format_bw,
92 u8 *ctrl_ch_position)
93{
94 switch (chandef->width) {
95 case NL80211_CHAN_WIDTH_20_NOHT:
96 *format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY;
97 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
98 break;
99 case NL80211_CHAN_WIDTH_20:
100 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
101 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
102 break;
103 case NL80211_CHAN_WIDTH_40:
104 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
105 *format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS;
106 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
107 break;
108 case NL80211_CHAN_WIDTH_80:
109 *format_bw = IWL_LOCATION_FRAME_FORMAT_VHT;
110 *format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS;
111 *ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
112 break;
113 default:
114 return -ENOTSUPP;
115 }
116
117 return 0;
118}
119
120static int
121iwl_mvm_ftm_responder_cmd(struct iwl_mvm *mvm,
122 struct ieee80211_vif *vif,
123 struct cfg80211_chan_def *chandef)
124{
125 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
126
127
128
129
130
131 struct iwl_tof_responder_config_cmd cmd = {
132 .channel_num = chandef->chan->hw_value,
133 .cmd_valid_fields =
134 cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_CHAN_INFO |
135 IWL_TOF_RESPONDER_CMD_VALID_BSSID |
136 IWL_TOF_RESPONDER_CMD_VALID_STA_ID),
137 .sta_id = mvmvif->bcast_sta.sta_id,
138 };
139 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LOCATION_GROUP,
140 TOF_RESPONDER_CONFIG_CMD);
141 int err;
142
143 lockdep_assert_held(&mvm->mutex);
144
145 if (cmd_ver == 7)
146 err = iwl_mvm_ftm_responder_set_bw_v2(chandef, &cmd.format_bw,
147 &cmd.ctrl_ch_position);
148 else
149 err = iwl_mvm_ftm_responder_set_bw_v1(chandef, &cmd.format_bw,
150 &cmd.ctrl_ch_position);
151
152 if (err) {
153 IWL_ERR(mvm, "Failed to set responder bandwidth\n");
154 return err;
155 }
156
157 memcpy(cmd.bssid, vif->addr, ETH_ALEN);
158
159 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_RESPONDER_CONFIG_CMD,
160 LOCATION_GROUP, 0),
161 0, sizeof(cmd), &cmd);
162}
163
164static int
165iwl_mvm_ftm_responder_dyn_cfg_cmd(struct iwl_mvm *mvm,
166 struct ieee80211_vif *vif,
167 struct ieee80211_ftm_responder_params *params)
168{
169 struct iwl_tof_responder_dyn_config_cmd cmd = {
170 .lci_len = cpu_to_le32(params->lci_len + 2),
171 .civic_len = cpu_to_le32(params->civicloc_len + 2),
172 };
173 u8 data[IWL_LCI_CIVIC_IE_MAX_SIZE] = {0};
174 struct iwl_host_cmd hcmd = {
175 .id = iwl_cmd_id(TOF_RESPONDER_DYN_CONFIG_CMD,
176 LOCATION_GROUP, 0),
177 .data[0] = &cmd,
178 .len[0] = sizeof(cmd),
179 .data[1] = &data,
180
181
182 .dataflags[1] = IWL_HCMD_DFL_DUP,
183 };
184 u32 aligned_lci_len = ALIGN(params->lci_len + 2, 4);
185 u32 aligned_civicloc_len = ALIGN(params->civicloc_len + 2, 4);
186 u8 *pos = data;
187
188 lockdep_assert_held(&mvm->mutex);
189
190 if (aligned_lci_len + aligned_civicloc_len > sizeof(data)) {
191 IWL_ERR(mvm, "LCI/civicloc data too big (%zd + %zd)\n",
192 params->lci_len, params->civicloc_len);
193 return -ENOBUFS;
194 }
195
196 pos[0] = WLAN_EID_MEASURE_REPORT;
197 pos[1] = params->lci_len;
198 memcpy(pos + 2, params->lci, params->lci_len);
199
200 pos += aligned_lci_len;
201 pos[0] = WLAN_EID_MEASURE_REPORT;
202 pos[1] = params->civicloc_len;
203 memcpy(pos + 2, params->civicloc, params->civicloc_len);
204
205 hcmd.len[1] = aligned_lci_len + aligned_civicloc_len;
206
207 return iwl_mvm_send_cmd(mvm, &hcmd);
208}
209
210int iwl_mvm_ftm_start_responder(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
211{
212 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
213 struct ieee80211_ftm_responder_params *params;
214 struct ieee80211_chanctx_conf ctx, *pctx;
215 u16 *phy_ctxt_id;
216 struct iwl_mvm_phy_ctxt *phy_ctxt;
217 int ret;
218
219 params = vif->bss_conf.ftmr_params;
220
221 lockdep_assert_held(&mvm->mutex);
222
223 if (WARN_ON_ONCE(!vif->bss_conf.ftm_responder))
224 return -EINVAL;
225
226 if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
227 !mvmvif->ap_ibss_active) {
228 IWL_ERR(mvm, "Cannot start responder, not in AP mode\n");
229 return -EIO;
230 }
231
232 rcu_read_lock();
233 pctx = rcu_dereference(vif->chanctx_conf);
234
235
236
237 ctx = *pctx;
238 phy_ctxt_id = (u16 *)pctx->drv_priv;
239 rcu_read_unlock();
240
241 phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
242 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx.def,
243 ctx.rx_chains_static,
244 ctx.rx_chains_dynamic);
245 if (ret)
246 return ret;
247
248 ret = iwl_mvm_ftm_responder_cmd(mvm, vif, &ctx.def);
249 if (ret)
250 return ret;
251
252 if (params)
253 ret = iwl_mvm_ftm_responder_dyn_cfg_cmd(mvm, vif, params);
254
255 return ret;
256}
257
258void iwl_mvm_ftm_restart_responder(struct iwl_mvm *mvm,
259 struct ieee80211_vif *vif)
260{
261 if (!vif->bss_conf.ftm_responder)
262 return;
263
264 iwl_mvm_ftm_start_responder(mvm, vif);
265}
266
267void iwl_mvm_ftm_responder_stats(struct iwl_mvm *mvm,
268 struct iwl_rx_cmd_buffer *rxb)
269{
270 struct iwl_rx_packet *pkt = rxb_addr(rxb);
271 struct iwl_ftm_responder_stats *resp = (void *)pkt->data;
272 struct cfg80211_ftm_responder_stats *stats = &mvm->ftm_resp_stats;
273 u32 flags = le32_to_cpu(resp->flags);
274
275 if (resp->success_ftm == resp->ftm_per_burst)
276 stats->success_num++;
277 else if (resp->success_ftm >= 2)
278 stats->partial_num++;
279 else
280 stats->failed_num++;
281
282 if ((flags & FTM_RESP_STAT_ASAP_REQ) &&
283 (flags & FTM_RESP_STAT_ASAP_RESP))
284 stats->asap_num++;
285
286 if (flags & FTM_RESP_STAT_NON_ASAP_RESP)
287 stats->non_asap_num++;
288
289 stats->total_duration_ms += le32_to_cpu(resp->duration) / USEC_PER_MSEC;
290
291 if (flags & FTM_RESP_STAT_TRIGGER_UNKNOWN)
292 stats->unknown_triggers_num++;
293
294 if (flags & FTM_RESP_STAT_DUP)
295 stats->reschedule_requests_num++;
296
297 if (flags & FTM_RESP_STAT_NON_ASAP_OUT_WIN)
298 stats->out_of_window_triggers_num++;
299}
300