tof.c 9.7 KB

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