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
61
62
63#include "mvm.h"
64#include "fw/api/tof.h"
65
66#define IWL_MVM_TOF_RANGE_REQ_MAX_ID 256
67
68void iwl_mvm_tof_init(struct iwl_mvm *mvm)
69{
70 struct iwl_mvm_tof_data *tof_data = &mvm->tof_data;
71
72 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
73 return;
74
75 memset(tof_data, 0, sizeof(*tof_data));
76
77 tof_data->tof_cfg.sub_grp_cmd_id = cpu_to_le32(TOF_CONFIG_CMD);
78
79#ifdef CONFIG_IWLWIFI_DEBUGFS
80 if (IWL_MVM_TOF_IS_RESPONDER) {
81 tof_data->responder_cfg.sub_grp_cmd_id =
82 cpu_to_le32(TOF_RESPONDER_CONFIG_CMD);
83 tof_data->responder_cfg.sta_id = IWL_MVM_INVALID_STA;
84 }
85#endif
86
87 tof_data->range_req.sub_grp_cmd_id = cpu_to_le32(TOF_RANGE_REQ_CMD);
88 tof_data->range_req.req_timeout = 1;
89 tof_data->range_req.initiator = 1;
90 tof_data->range_req.report_policy = 3;
91
92 tof_data->range_req_ext.sub_grp_cmd_id =
93 cpu_to_le32(TOF_RANGE_REQ_EXT_CMD);
94
95 mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
96 mvm->init_status |= IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE;
97}
98
99void iwl_mvm_tof_clean(struct iwl_mvm *mvm)
100{
101 struct iwl_mvm_tof_data *tof_data = &mvm->tof_data;
102
103 if (!fw_has_capa(&mvm->fw->ucode_capa,
104 IWL_UCODE_TLV_CAPA_TOF_SUPPORT) ||
105 !(mvm->init_status & IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE))
106 return;
107
108 memset(tof_data, 0, sizeof(*tof_data));
109 mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
110 mvm->init_status &= ~IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE;
111}
112
113static void iwl_tof_iterator(void *_data, u8 *mac,
114 struct ieee80211_vif *vif)
115{
116 bool *enabled = _data;
117
118
119 if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION)
120 *enabled = false;
121}
122
123int iwl_mvm_tof_config_cmd(struct iwl_mvm *mvm)
124{
125 struct iwl_tof_config_cmd *cmd = &mvm->tof_data.tof_cfg;
126 bool enabled;
127
128 lockdep_assert_held(&mvm->mutex);
129
130 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
131 return -EINVAL;
132
133 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
134 IEEE80211_IFACE_ITER_NORMAL,
135 iwl_tof_iterator, &enabled);
136 if (!enabled) {
137 IWL_DEBUG_INFO(mvm, "ToF is not supported (non bss vif)\n");
138 return -EINVAL;
139 }
140
141 mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
142 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
143 IWL_ALWAYS_LONG_GROUP, 0),
144 0, sizeof(*cmd), cmd);
145}
146
147int iwl_mvm_tof_range_abort_cmd(struct iwl_mvm *mvm, u8 id)
148{
149 struct iwl_tof_range_abort_cmd cmd = {
150 .sub_grp_cmd_id = cpu_to_le32(TOF_RANGE_ABORT_CMD),
151 .request_id = id,
152 };
153
154 lockdep_assert_held(&mvm->mutex);
155
156 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
157 return -EINVAL;
158
159 if (id != mvm->tof_data.active_range_request) {
160 IWL_ERR(mvm, "Invalid range request id %d (active %d)\n",
161 id, mvm->tof_data.active_range_request);
162 return -EINVAL;
163 }
164
165
166 mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
167
168 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
169 IWL_ALWAYS_LONG_GROUP, 0),
170 0, sizeof(cmd), &cmd);
171}
172
173#ifdef CONFIG_IWLWIFI_DEBUGFS
174int iwl_mvm_tof_responder_cmd(struct iwl_mvm *mvm,
175 struct ieee80211_vif *vif)
176{
177 struct iwl_tof_responder_config_cmd *cmd = &mvm->tof_data.responder_cfg;
178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
179
180 lockdep_assert_held(&mvm->mutex);
181
182 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
183 return -EINVAL;
184
185 if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
186 !mvmvif->ap_ibss_active) {
187 IWL_ERR(mvm, "Cannot start responder, not in AP mode\n");
188 return -EIO;
189 }
190
191 cmd->sta_id = mvmvif->bcast_sta.sta_id;
192 memcpy(cmd->bssid, vif->addr, ETH_ALEN);
193 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
194 IWL_ALWAYS_LONG_GROUP, 0),
195 0, sizeof(*cmd), cmd);
196}
197#endif
198
199int iwl_mvm_tof_range_request_cmd(struct iwl_mvm *mvm,
200 struct ieee80211_vif *vif)
201{
202 struct iwl_host_cmd cmd = {
203 .id = iwl_cmd_id(TOF_CMD, IWL_ALWAYS_LONG_GROUP, 0),
204 .len = { sizeof(mvm->tof_data.range_req), },
205
206 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
207 };
208
209 lockdep_assert_held(&mvm->mutex);
210
211 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
212 return -EINVAL;
213
214 if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION) {
215 IWL_ERR(mvm, "Cannot send range request, not STA mode\n");
216 return -EIO;
217 }
218
219
220 if (mvm->tof_data.active_range_request !=
221 IWL_MVM_TOF_RANGE_REQ_MAX_ID) {
222 IWL_ERR(mvm, "Cannot send range req, already active req %d\n",
223 mvm->tof_data.active_range_request);
224 return -EIO;
225 }
226
227 mvm->tof_data.active_range_request = mvm->tof_data.range_req.request_id;
228
229 cmd.data[0] = &mvm->tof_data.range_req;
230 return iwl_mvm_send_cmd(mvm, &cmd);
231}
232
233int iwl_mvm_tof_range_request_ext_cmd(struct iwl_mvm *mvm,
234 struct ieee80211_vif *vif)
235{
236 lockdep_assert_held(&mvm->mutex);
237
238 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
239 return -EINVAL;
240
241 if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION) {
242 IWL_ERR(mvm, "Cannot send ext range req, not in STA mode\n");
243 return -EIO;
244 }
245
246 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
247 IWL_ALWAYS_LONG_GROUP, 0),
248 0, sizeof(mvm->tof_data.range_req_ext),
249 &mvm->tof_data.range_req_ext);
250}
251
252static int iwl_mvm_tof_range_resp(struct iwl_mvm *mvm, void *data)
253{
254 struct iwl_tof_range_rsp_ntfy *resp = (void *)data;
255
256 if (resp->request_id != mvm->tof_data.active_range_request) {
257 IWL_ERR(mvm, "Request id mismatch, got %d, active %d\n",
258 resp->request_id, mvm->tof_data.active_range_request);
259 return -EIO;
260 }
261
262 memcpy(&mvm->tof_data.range_resp, resp,
263 sizeof(struct iwl_tof_range_rsp_ntfy));
264 mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
265
266 return 0;
267}
268
269static int iwl_mvm_tof_mcsi_notif(struct iwl_mvm *mvm, void *data)
270{
271 struct iwl_tof_mcsi_notif *resp = (struct iwl_tof_mcsi_notif *)data;
272
273 IWL_DEBUG_INFO(mvm, "MCSI notification, token %d\n", resp->token);
274 return 0;
275}
276
277static int iwl_mvm_tof_nb_report_notif(struct iwl_mvm *mvm, void *data)
278{
279 struct iwl_tof_neighbor_report *report =
280 (struct iwl_tof_neighbor_report *)data;
281
282 IWL_DEBUG_INFO(mvm, "NB report, bssid %pM, token %d, status 0x%x\n",
283 report->bssid, report->request_token, report->status);
284 return 0;
285}
286
287void iwl_mvm_tof_resp_handler(struct iwl_mvm *mvm,
288 struct iwl_rx_cmd_buffer *rxb)
289{
290 struct iwl_rx_packet *pkt = rxb_addr(rxb);
291 struct iwl_tof_gen_resp_cmd *resp = (void *)pkt->data;
292
293 lockdep_assert_held(&mvm->mutex);
294
295 switch (le32_to_cpu(resp->sub_grp_cmd_id)) {
296 case TOF_RANGE_RESPONSE_NOTIF:
297 iwl_mvm_tof_range_resp(mvm, resp->data);
298 break;
299 case TOF_MCSI_DEBUG_NOTIF:
300 iwl_mvm_tof_mcsi_notif(mvm, resp->data);
301 break;
302 case TOF_NEIGHBOR_REPORT_RSP_NOTIF:
303 iwl_mvm_tof_nb_report_notif(mvm, resp->data);
304 break;
305 default:
306 IWL_ERR(mvm, "Unknown sub-group command 0x%x\n",
307 resp->sub_grp_cmd_id);
308 break;
309 }
310}
311