Merge tag 'for-linus-20181207' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / net / wireless / intel / iwlwifi / mvm / tof.c
1 /******************************************************************************
2  *
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.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Deutschland GmbH
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * The full GNU General Public License is included in this distribution
20  * in the file called COPYING.
21  *
22  * Contact Information:
23  * Intel Linux Wireless <linuxwifi@intel.com>
24  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2015 Intel Deutschland GmbH
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *  * Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  *  * Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in
39  *    the documentation and/or other materials provided with the
40  *    distribution.
41  *  * Neither the name Intel Corporation nor the names of its
42  *    contributors may be used to endorse or promote products derived
43  *    from this software without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  *
57  *****************************************************************************/
58 #include "mvm.h"
59 #include "fw/api/tof.h"
60
61 #define IWL_MVM_TOF_RANGE_REQ_MAX_ID 256
62
63 void iwl_mvm_tof_init(struct iwl_mvm *mvm)
64 {
65         struct iwl_mvm_tof_data *tof_data = &mvm->tof_data;
66
67         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
68                 return;
69
70         memset(tof_data, 0, sizeof(*tof_data));
71
72         tof_data->tof_cfg.sub_grp_cmd_id = cpu_to_le32(TOF_CONFIG_CMD);
73
74 #ifdef CONFIG_IWLWIFI_DEBUGFS
75         if (IWL_MVM_TOF_IS_RESPONDER) {
76                 tof_data->responder_cfg.sub_grp_cmd_id =
77                         cpu_to_le32(TOF_RESPONDER_CONFIG_CMD);
78                 tof_data->responder_cfg.sta_id = IWL_MVM_INVALID_STA;
79         }
80 #endif
81
82         tof_data->range_req.sub_grp_cmd_id = cpu_to_le32(TOF_RANGE_REQ_CMD);
83         tof_data->range_req.req_timeout = 1;
84         tof_data->range_req.initiator = 1;
85         tof_data->range_req.report_policy = 3;
86
87         tof_data->range_req_ext.sub_grp_cmd_id =
88                 cpu_to_le32(TOF_RANGE_REQ_EXT_CMD);
89
90         mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
91         mvm->init_status |= IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE;
92 }
93
94 void iwl_mvm_tof_clean(struct iwl_mvm *mvm)
95 {
96         struct iwl_mvm_tof_data *tof_data = &mvm->tof_data;
97
98         if (!fw_has_capa(&mvm->fw->ucode_capa,
99                          IWL_UCODE_TLV_CAPA_TOF_SUPPORT) ||
100             !(mvm->init_status & IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE))
101                 return;
102
103         memset(tof_data, 0, sizeof(*tof_data));
104         mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
105         mvm->init_status &= ~IWL_MVM_INIT_STATUS_TOF_INIT_COMPLETE;
106 }
107
108 static void iwl_tof_iterator(void *_data, u8 *mac,
109                              struct ieee80211_vif *vif)
110 {
111         bool *enabled = _data;
112
113         /* non bss vif exists */
114         if (ieee80211_vif_type_p2p(vif) !=  NL80211_IFTYPE_STATION)
115                 *enabled = false;
116 }
117
118 int iwl_mvm_tof_config_cmd(struct iwl_mvm *mvm)
119 {
120         struct iwl_tof_config_cmd *cmd = &mvm->tof_data.tof_cfg;
121         bool enabled;
122
123         lockdep_assert_held(&mvm->mutex);
124
125         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
126                 return -EINVAL;
127
128         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
129                                                    IEEE80211_IFACE_ITER_NORMAL,
130                                                    iwl_tof_iterator, &enabled);
131         if (!enabled) {
132                 IWL_DEBUG_INFO(mvm, "ToF is not supported (non bss vif)\n");
133                 return -EINVAL;
134         }
135
136         mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
137         return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
138                                                     IWL_ALWAYS_LONG_GROUP, 0),
139                                     0, sizeof(*cmd), cmd);
140 }
141
142 int iwl_mvm_tof_range_abort_cmd(struct iwl_mvm *mvm, u8 id)
143 {
144         struct iwl_tof_range_abort_cmd cmd = {
145                 .sub_grp_cmd_id = cpu_to_le32(TOF_RANGE_ABORT_CMD),
146                 .request_id = id,
147         };
148
149         lockdep_assert_held(&mvm->mutex);
150
151         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
152                 return -EINVAL;
153
154         if (id != mvm->tof_data.active_range_request) {
155                 IWL_ERR(mvm, "Invalid range request id %d (active %d)\n",
156                         id, mvm->tof_data.active_range_request);
157                 return -EINVAL;
158         }
159
160         /* after abort is sent there's no active request anymore */
161         mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
162
163         return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
164                                                     IWL_ALWAYS_LONG_GROUP, 0),
165                                     0, sizeof(cmd), &cmd);
166 }
167
168 #ifdef CONFIG_IWLWIFI_DEBUGFS
169 int iwl_mvm_tof_responder_cmd(struct iwl_mvm *mvm,
170                               struct ieee80211_vif *vif)
171 {
172         struct iwl_tof_responder_config_cmd *cmd = &mvm->tof_data.responder_cfg;
173         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
174
175         lockdep_assert_held(&mvm->mutex);
176
177         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
178                 return -EINVAL;
179
180         if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
181             !mvmvif->ap_ibss_active) {
182                 IWL_ERR(mvm, "Cannot start responder, not in AP mode\n");
183                 return -EIO;
184         }
185
186         cmd->sta_id = mvmvif->bcast_sta.sta_id;
187         memcpy(cmd->bssid, vif->addr, ETH_ALEN);
188         return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
189                                                     IWL_ALWAYS_LONG_GROUP, 0),
190                                     0, sizeof(*cmd), cmd);
191 }
192 #endif
193
194 int iwl_mvm_tof_range_request_cmd(struct iwl_mvm *mvm,
195                                   struct ieee80211_vif *vif)
196 {
197         struct iwl_host_cmd cmd = {
198                 .id = iwl_cmd_id(TOF_CMD, IWL_ALWAYS_LONG_GROUP, 0),
199                 .len = { sizeof(mvm->tof_data.range_req), },
200                 /* no copy because of the command size */
201                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
202         };
203
204         lockdep_assert_held(&mvm->mutex);
205
206         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
207                 return -EINVAL;
208
209         if (ieee80211_vif_type_p2p(vif) !=  NL80211_IFTYPE_STATION) {
210                 IWL_ERR(mvm, "Cannot send range request, not STA mode\n");
211                 return -EIO;
212         }
213
214         /* nesting of range requests is not supported in FW */
215         if (mvm->tof_data.active_range_request !=
216                 IWL_MVM_TOF_RANGE_REQ_MAX_ID) {
217                 IWL_ERR(mvm, "Cannot send range req, already active req %d\n",
218                         mvm->tof_data.active_range_request);
219                 return -EIO;
220         }
221
222         mvm->tof_data.active_range_request = mvm->tof_data.range_req.request_id;
223
224         cmd.data[0] = &mvm->tof_data.range_req;
225         return iwl_mvm_send_cmd(mvm, &cmd);
226 }
227
228 int iwl_mvm_tof_range_request_ext_cmd(struct iwl_mvm *mvm,
229                                       struct ieee80211_vif *vif)
230 {
231         lockdep_assert_held(&mvm->mutex);
232
233         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TOF_SUPPORT))
234                 return -EINVAL;
235
236         if (ieee80211_vif_type_p2p(vif) !=  NL80211_IFTYPE_STATION) {
237                 IWL_ERR(mvm, "Cannot send ext range req, not in STA mode\n");
238                 return -EIO;
239         }
240
241         return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_CMD,
242                                                     IWL_ALWAYS_LONG_GROUP, 0),
243                                     0, sizeof(mvm->tof_data.range_req_ext),
244                                     &mvm->tof_data.range_req_ext);
245 }
246
247 static int iwl_mvm_tof_range_resp(struct iwl_mvm *mvm, void *data)
248 {
249         struct iwl_tof_range_rsp_ntfy *resp = (void *)data;
250
251         if (resp->request_id != mvm->tof_data.active_range_request) {
252                 IWL_ERR(mvm, "Request id mismatch, got %d, active %d\n",
253                         resp->request_id, mvm->tof_data.active_range_request);
254                 return -EIO;
255         }
256
257         memcpy(&mvm->tof_data.range_resp, resp,
258                sizeof(struct iwl_tof_range_rsp_ntfy));
259         mvm->tof_data.active_range_request = IWL_MVM_TOF_RANGE_REQ_MAX_ID;
260
261         return 0;
262 }
263
264 static int iwl_mvm_tof_mcsi_notif(struct iwl_mvm *mvm, void *data)
265 {
266         struct iwl_tof_mcsi_notif *resp = (struct iwl_tof_mcsi_notif *)data;
267
268         IWL_DEBUG_INFO(mvm, "MCSI notification, token %d\n", resp->token);
269         return 0;
270 }
271
272 static int iwl_mvm_tof_nb_report_notif(struct iwl_mvm *mvm, void *data)
273 {
274         struct iwl_tof_neighbor_report *report =
275                 (struct iwl_tof_neighbor_report *)data;
276
277         IWL_DEBUG_INFO(mvm, "NB report, bssid %pM, token %d, status 0x%x\n",
278                        report->bssid, report->request_token, report->status);
279         return 0;
280 }
281
282 void iwl_mvm_tof_resp_handler(struct iwl_mvm *mvm,
283                               struct iwl_rx_cmd_buffer *rxb)
284 {
285         struct iwl_rx_packet *pkt = rxb_addr(rxb);
286         struct iwl_tof_gen_resp_cmd *resp = (void *)pkt->data;
287
288         lockdep_assert_held(&mvm->mutex);
289
290         switch (le32_to_cpu(resp->sub_grp_cmd_id)) {
291         case TOF_RANGE_RESPONSE_NOTIF:
292                 iwl_mvm_tof_range_resp(mvm, resp->data);
293                 break;
294         case TOF_MCSI_DEBUG_NOTIF:
295                 iwl_mvm_tof_mcsi_notif(mvm, resp->data);
296                 break;
297         case TOF_NEIGHBOR_REPORT_RSP_NOTIF:
298                 iwl_mvm_tof_nb_report_notif(mvm, resp->data);
299                 break;
300         default:
301                IWL_ERR(mvm, "Unknown sub-group command 0x%x\n",
302                        resp->sub_grp_cmd_id);
303                break;
304         }
305 }