tdls.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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) 2014 Intel Mobile Communications GmbH
  9. * Copyright(c) 2017 Intel Deutschland GmbH
  10. * Copyright(C) 2018 Intel Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program.
  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) 2014 Intel Mobile Communications GmbH
  34. * Copyright(c) 2017 Intel Deutschland GmbH
  35. * Copyright(C) 2018 Intel Corporation
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/etherdevice.h>
  66. #include "mvm.h"
  67. #include "time-event.h"
  68. #include "iwl-io.h"
  69. #include "iwl-prph.h"
  70. #define TU_TO_US(x) (x * 1024)
  71. #define TU_TO_MS(x) (TU_TO_US(x) / 1000)
  72. void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
  73. {
  74. struct ieee80211_sta *sta;
  75. struct iwl_mvm_sta *mvmsta;
  76. int i;
  77. lockdep_assert_held(&mvm->mutex);
  78. for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
  79. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  80. lockdep_is_held(&mvm->mutex));
  81. if (!sta || IS_ERR(sta) || !sta->tdls)
  82. continue;
  83. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  84. ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
  85. NL80211_TDLS_TEARDOWN,
  86. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
  87. GFP_KERNEL);
  88. }
  89. }
  90. int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  91. {
  92. struct ieee80211_sta *sta;
  93. struct iwl_mvm_sta *mvmsta;
  94. int count = 0;
  95. int i;
  96. lockdep_assert_held(&mvm->mutex);
  97. for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
  98. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  99. lockdep_is_held(&mvm->mutex));
  100. if (!sta || IS_ERR(sta) || !sta->tdls)
  101. continue;
  102. if (vif) {
  103. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  104. if (mvmsta->vif != vif)
  105. continue;
  106. }
  107. count++;
  108. }
  109. return count;
  110. }
  111. static void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  112. {
  113. struct iwl_rx_packet *pkt;
  114. struct iwl_tdls_config_res *resp;
  115. struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
  116. struct iwl_host_cmd cmd = {
  117. .id = TDLS_CONFIG_CMD,
  118. .flags = CMD_WANT_SKB,
  119. .data = { &tdls_cfg_cmd, },
  120. .len = { sizeof(struct iwl_tdls_config_cmd), },
  121. };
  122. struct ieee80211_sta *sta;
  123. int ret, i, cnt;
  124. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  125. lockdep_assert_held(&mvm->mutex);
  126. tdls_cfg_cmd.id_and_color =
  127. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  128. tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
  129. tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
  130. /* for now the Tx cmd is empty and unused */
  131. /* populate TDLS peer data */
  132. cnt = 0;
  133. for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
  134. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  135. lockdep_is_held(&mvm->mutex));
  136. if (IS_ERR_OR_NULL(sta) || !sta->tdls)
  137. continue;
  138. tdls_cfg_cmd.sta_info[cnt].sta_id = i;
  139. tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
  140. IWL_MVM_TDLS_FW_TID;
  141. tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
  142. tdls_cfg_cmd.sta_info[cnt].is_initiator =
  143. cpu_to_le32(sta->tdls_initiator ? 1 : 0);
  144. cnt++;
  145. }
  146. tdls_cfg_cmd.tdls_peer_count = cnt;
  147. IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
  148. ret = iwl_mvm_send_cmd(mvm, &cmd);
  149. if (WARN_ON_ONCE(ret))
  150. return;
  151. pkt = cmd.resp_pkt;
  152. WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp));
  153. /* we don't really care about the response at this point */
  154. iwl_free_resp(&cmd);
  155. }
  156. void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  157. bool sta_added)
  158. {
  159. int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
  160. /* when the first peer joins, send a power update first */
  161. if (tdls_sta_cnt == 1 && sta_added)
  162. iwl_mvm_power_update_mac(mvm);
  163. /* Configure the FW with TDLS peer info only if TDLS channel switch
  164. * capability is set.
  165. * TDLS config data is used currently only in TDLS channel switch code.
  166. * Supposed to serve also TDLS buffer station which is not implemneted
  167. * yet in FW*/
  168. if (fw_has_capa(&mvm->fw->ucode_capa,
  169. IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH))
  170. iwl_mvm_tdls_config(mvm, vif);
  171. /* when the last peer leaves, send a power update last */
  172. if (tdls_sta_cnt == 0 && !sta_added)
  173. iwl_mvm_power_update_mac(mvm);
  174. }
  175. void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
  176. struct ieee80211_vif *vif)
  177. {
  178. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  179. u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
  180. /*
  181. * iwl_mvm_protect_session() reads directly from the device
  182. * (the system time), so make sure it is available.
  183. */
  184. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_TDLS))
  185. return;
  186. mutex_lock(&mvm->mutex);
  187. /* Protect the session to hear the TDLS setup response on the channel */
  188. iwl_mvm_protect_session(mvm, vif, duration, duration, 100, true);
  189. mutex_unlock(&mvm->mutex);
  190. iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_TDLS);
  191. }
  192. static const char *
  193. iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
  194. {
  195. switch (state) {
  196. case IWL_MVM_TDLS_SW_IDLE:
  197. return "IDLE";
  198. case IWL_MVM_TDLS_SW_REQ_SENT:
  199. return "REQ SENT";
  200. case IWL_MVM_TDLS_SW_RESP_RCVD:
  201. return "RESP RECEIVED";
  202. case IWL_MVM_TDLS_SW_REQ_RCVD:
  203. return "REQ RECEIVED";
  204. case IWL_MVM_TDLS_SW_ACTIVE:
  205. return "ACTIVE";
  206. }
  207. return NULL;
  208. }
  209. static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
  210. enum iwl_mvm_tdls_cs_state state)
  211. {
  212. if (mvm->tdls_cs.state == state)
  213. return;
  214. IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
  215. iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
  216. iwl_mvm_tdls_cs_state_str(state));
  217. mvm->tdls_cs.state = state;
  218. /* we only send requests to our switching peer - update sent time */
  219. if (state == IWL_MVM_TDLS_SW_REQ_SENT)
  220. mvm->tdls_cs.peer.sent_timestamp =
  221. iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
  222. if (state == IWL_MVM_TDLS_SW_IDLE)
  223. mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
  224. }
  225. void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  226. {
  227. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  228. struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
  229. struct ieee80211_sta *sta;
  230. unsigned int delay;
  231. struct iwl_mvm_sta *mvmsta;
  232. struct ieee80211_vif *vif;
  233. u32 sta_id = le32_to_cpu(notif->sta_id);
  234. lockdep_assert_held(&mvm->mutex);
  235. /* can fail sometimes */
  236. if (!le32_to_cpu(notif->status)) {
  237. iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
  238. return;
  239. }
  240. if (WARN_ON(sta_id >= IWL_MVM_STATION_COUNT))
  241. return;
  242. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  243. lockdep_is_held(&mvm->mutex));
  244. /* the station may not be here, but if it is, it must be a TDLS peer */
  245. if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
  246. return;
  247. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  248. vif = mvmsta->vif;
  249. /*
  250. * Update state and possibly switch again after this is over (DTIM).
  251. * Also convert TU to msec.
  252. */
  253. delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
  254. mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
  255. msecs_to_jiffies(delay));
  256. iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
  257. }
  258. static int
  259. iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
  260. enum iwl_tdls_channel_switch_type type,
  261. const u8 *peer, bool peer_initiator, u32 timestamp)
  262. {
  263. bool same_peer = false;
  264. int ret = 0;
  265. /* get the existing peer if it's there */
  266. if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
  267. mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
  268. struct ieee80211_sta *sta = rcu_dereference_protected(
  269. mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
  270. lockdep_is_held(&mvm->mutex));
  271. if (!IS_ERR_OR_NULL(sta))
  272. same_peer = ether_addr_equal(peer, sta->addr);
  273. }
  274. switch (mvm->tdls_cs.state) {
  275. case IWL_MVM_TDLS_SW_IDLE:
  276. /*
  277. * might be spurious packet from the peer after the switch is
  278. * already done
  279. */
  280. if (type == TDLS_MOVE_CH)
  281. ret = -EINVAL;
  282. break;
  283. case IWL_MVM_TDLS_SW_REQ_SENT:
  284. /* only allow requests from the same peer */
  285. if (!same_peer)
  286. ret = -EBUSY;
  287. else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
  288. !peer_initiator)
  289. /*
  290. * We received a ch-switch request while an outgoing
  291. * one is pending. Allow it if the peer is the link
  292. * initiator.
  293. */
  294. ret = -EBUSY;
  295. else if (type == TDLS_SEND_CHAN_SW_REQ)
  296. /* wait for idle before sending another request */
  297. ret = -EBUSY;
  298. else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
  299. /* we got a stale response - ignore it */
  300. ret = -EINVAL;
  301. break;
  302. case IWL_MVM_TDLS_SW_RESP_RCVD:
  303. /*
  304. * we are waiting for the FW to give an "active" notification,
  305. * so ignore requests in the meantime
  306. */
  307. ret = -EBUSY;
  308. break;
  309. case IWL_MVM_TDLS_SW_REQ_RCVD:
  310. /* as above, allow the link initiator to proceed */
  311. if (type == TDLS_SEND_CHAN_SW_REQ) {
  312. if (!same_peer)
  313. ret = -EBUSY;
  314. else if (peer_initiator) /* they are the initiator */
  315. ret = -EBUSY;
  316. } else if (type == TDLS_MOVE_CH) {
  317. ret = -EINVAL;
  318. }
  319. break;
  320. case IWL_MVM_TDLS_SW_ACTIVE:
  321. /*
  322. * the only valid request when active is a request to return
  323. * to the base channel by the current off-channel peer
  324. */
  325. if (type != TDLS_MOVE_CH || !same_peer)
  326. ret = -EBUSY;
  327. break;
  328. }
  329. if (ret)
  330. IWL_DEBUG_TDLS(mvm,
  331. "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
  332. type, mvm->tdls_cs.state, peer, same_peer,
  333. peer_initiator);
  334. return ret;
  335. }
  336. static int
  337. iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
  338. struct ieee80211_vif *vif,
  339. enum iwl_tdls_channel_switch_type type,
  340. const u8 *peer, bool peer_initiator,
  341. u8 oper_class,
  342. struct cfg80211_chan_def *chandef,
  343. u32 timestamp, u16 switch_time,
  344. u16 switch_timeout, struct sk_buff *skb,
  345. u32 ch_sw_tm_ie)
  346. {
  347. struct ieee80211_sta *sta;
  348. struct iwl_mvm_sta *mvmsta;
  349. struct ieee80211_tx_info *info;
  350. struct ieee80211_hdr *hdr;
  351. struct iwl_tdls_channel_switch_cmd cmd = {0};
  352. int ret;
  353. lockdep_assert_held(&mvm->mutex);
  354. ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
  355. timestamp);
  356. if (ret)
  357. return ret;
  358. if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
  359. ret = -EINVAL;
  360. goto out;
  361. }
  362. cmd.switch_type = type;
  363. cmd.timing.frame_timestamp = cpu_to_le32(timestamp);
  364. cmd.timing.switch_time = cpu_to_le32(switch_time);
  365. cmd.timing.switch_timeout = cpu_to_le32(switch_timeout);
  366. rcu_read_lock();
  367. sta = ieee80211_find_sta(vif, peer);
  368. if (!sta) {
  369. rcu_read_unlock();
  370. ret = -ENOENT;
  371. goto out;
  372. }
  373. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  374. cmd.peer_sta_id = cpu_to_le32(mvmsta->sta_id);
  375. if (!chandef) {
  376. if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
  377. mvm->tdls_cs.peer.chandef.chan) {
  378. /* actually moving to the channel */
  379. chandef = &mvm->tdls_cs.peer.chandef;
  380. } else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
  381. type == TDLS_MOVE_CH) {
  382. /* we need to return to base channel */
  383. struct ieee80211_chanctx_conf *chanctx =
  384. rcu_dereference(vif->chanctx_conf);
  385. if (WARN_ON_ONCE(!chanctx)) {
  386. rcu_read_unlock();
  387. goto out;
  388. }
  389. chandef = &chanctx->def;
  390. }
  391. }
  392. if (chandef) {
  393. cmd.ci.band = (chandef->chan->band == NL80211_BAND_2GHZ ?
  394. PHY_BAND_24 : PHY_BAND_5);
  395. cmd.ci.channel = chandef->chan->hw_value;
  396. cmd.ci.width = iwl_mvm_get_channel_width(chandef);
  397. cmd.ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
  398. }
  399. /* keep quota calculation simple for now - 50% of DTIM for TDLS */
  400. cmd.timing.max_offchan_duration =
  401. cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
  402. vif->bss_conf.beacon_int) / 2);
  403. /* Switch time is the first element in the switch-timing IE. */
  404. cmd.frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
  405. info = IEEE80211_SKB_CB(skb);
  406. hdr = (void *)skb->data;
  407. if (info->control.hw_key) {
  408. if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
  409. rcu_read_unlock();
  410. ret = -EINVAL;
  411. goto out;
  412. }
  413. iwl_mvm_set_tx_cmd_ccmp(info, &cmd.frame.tx_cmd);
  414. }
  415. iwl_mvm_set_tx_cmd(mvm, skb, &cmd.frame.tx_cmd, info,
  416. mvmsta->sta_id);
  417. iwl_mvm_set_tx_cmd_rate(mvm, &cmd.frame.tx_cmd, info, sta,
  418. hdr->frame_control);
  419. rcu_read_unlock();
  420. memcpy(cmd.frame.data, skb->data, skb->len);
  421. ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0,
  422. sizeof(cmd), &cmd);
  423. if (ret) {
  424. IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
  425. ret);
  426. goto out;
  427. }
  428. /* channel switch has started, update state */
  429. if (type != TDLS_MOVE_CH) {
  430. mvm->tdls_cs.cur_sta_id = mvmsta->sta_id;
  431. iwl_mvm_tdls_update_cs_state(mvm,
  432. type == TDLS_SEND_CHAN_SW_REQ ?
  433. IWL_MVM_TDLS_SW_REQ_SENT :
  434. IWL_MVM_TDLS_SW_REQ_RCVD);
  435. } else {
  436. iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
  437. }
  438. out:
  439. /* channel switch failed - we are idle */
  440. if (ret)
  441. iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
  442. return ret;
  443. }
  444. void iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
  445. {
  446. struct iwl_mvm *mvm;
  447. struct ieee80211_sta *sta;
  448. struct iwl_mvm_sta *mvmsta;
  449. struct ieee80211_vif *vif;
  450. unsigned int delay;
  451. int ret;
  452. mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
  453. mutex_lock(&mvm->mutex);
  454. /* called after an active channel switch has finished or timed-out */
  455. iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
  456. /* station might be gone, in that case do nothing */
  457. if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA)
  458. goto out;
  459. sta = rcu_dereference_protected(
  460. mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
  461. lockdep_is_held(&mvm->mutex));
  462. /* the station may not be here, but if it is, it must be a TDLS peer */
  463. if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
  464. goto out;
  465. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  466. vif = mvmsta->vif;
  467. ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
  468. TDLS_SEND_CHAN_SW_REQ,
  469. sta->addr,
  470. mvm->tdls_cs.peer.initiator,
  471. mvm->tdls_cs.peer.op_class,
  472. &mvm->tdls_cs.peer.chandef,
  473. 0, 0, 0,
  474. mvm->tdls_cs.peer.skb,
  475. mvm->tdls_cs.peer.ch_sw_tm_ie);
  476. if (ret)
  477. IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
  478. /* retry after a DTIM if we failed sending now */
  479. delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
  480. schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
  481. out:
  482. mutex_unlock(&mvm->mutex);
  483. }
  484. int
  485. iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
  486. struct ieee80211_vif *vif,
  487. struct ieee80211_sta *sta, u8 oper_class,
  488. struct cfg80211_chan_def *chandef,
  489. struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
  490. {
  491. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  492. struct iwl_mvm_sta *mvmsta;
  493. unsigned int delay;
  494. int ret;
  495. mutex_lock(&mvm->mutex);
  496. IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
  497. sta->addr, chandef->chan->center_freq, chandef->width);
  498. /* we only support a single peer for channel switching */
  499. if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
  500. IWL_DEBUG_TDLS(mvm,
  501. "Existing peer. Can't start switch with %pM\n",
  502. sta->addr);
  503. ret = -EBUSY;
  504. goto out;
  505. }
  506. ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
  507. TDLS_SEND_CHAN_SW_REQ,
  508. sta->addr, sta->tdls_initiator,
  509. oper_class, chandef, 0, 0, 0,
  510. tmpl_skb, ch_sw_tm_ie);
  511. if (ret)
  512. goto out;
  513. /*
  514. * Mark the peer as "in tdls switch" for this vif. We only allow a
  515. * single such peer per vif.
  516. */
  517. mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
  518. if (!mvm->tdls_cs.peer.skb) {
  519. ret = -ENOMEM;
  520. goto out;
  521. }
  522. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  523. mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
  524. mvm->tdls_cs.peer.chandef = *chandef;
  525. mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
  526. mvm->tdls_cs.peer.op_class = oper_class;
  527. mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
  528. /*
  529. * Wait for 2 DTIM periods before attempting the next switch. The next
  530. * switch will be made sooner if the current one completes before that.
  531. */
  532. delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
  533. vif->bss_conf.beacon_int);
  534. mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
  535. msecs_to_jiffies(delay));
  536. out:
  537. mutex_unlock(&mvm->mutex);
  538. return ret;
  539. }
  540. void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
  541. struct ieee80211_vif *vif,
  542. struct ieee80211_sta *sta)
  543. {
  544. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  545. struct ieee80211_sta *cur_sta;
  546. bool wait_for_phy = false;
  547. mutex_lock(&mvm->mutex);
  548. IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
  549. /* we only support a single peer for channel switching */
  550. if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
  551. IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
  552. goto out;
  553. }
  554. cur_sta = rcu_dereference_protected(
  555. mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
  556. lockdep_is_held(&mvm->mutex));
  557. /* make sure it's the same peer */
  558. if (cur_sta != sta)
  559. goto out;
  560. /*
  561. * If we're currently in a switch because of the now canceled peer,
  562. * wait a DTIM here to make sure the phy is back on the base channel.
  563. * We can't otherwise force it.
  564. */
  565. if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
  566. mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
  567. wait_for_phy = true;
  568. mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
  569. dev_kfree_skb(mvm->tdls_cs.peer.skb);
  570. mvm->tdls_cs.peer.skb = NULL;
  571. out:
  572. mutex_unlock(&mvm->mutex);
  573. /* make sure the phy is on the base channel */
  574. if (wait_for_phy)
  575. msleep(TU_TO_MS(vif->bss_conf.dtim_period *
  576. vif->bss_conf.beacon_int));
  577. /* flush the channel switch state */
  578. flush_delayed_work(&mvm->tdls_cs.dwork);
  579. IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
  580. }
  581. void
  582. iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
  583. struct ieee80211_vif *vif,
  584. struct ieee80211_tdls_ch_sw_params *params)
  585. {
  586. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  587. enum iwl_tdls_channel_switch_type type;
  588. unsigned int delay;
  589. const char *action_str =
  590. params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
  591. "REQ" : "RESP";
  592. mutex_lock(&mvm->mutex);
  593. IWL_DEBUG_TDLS(mvm,
  594. "Received TDLS ch switch action %s from %pM status %d\n",
  595. action_str, params->sta->addr, params->status);
  596. /*
  597. * we got a non-zero status from a peer we were switching to - move to
  598. * the idle state and retry again later
  599. */
  600. if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
  601. params->status != 0 &&
  602. mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
  603. mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
  604. struct ieee80211_sta *cur_sta;
  605. /* make sure it's the same peer */
  606. cur_sta = rcu_dereference_protected(
  607. mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
  608. lockdep_is_held(&mvm->mutex));
  609. if (cur_sta == params->sta) {
  610. iwl_mvm_tdls_update_cs_state(mvm,
  611. IWL_MVM_TDLS_SW_IDLE);
  612. goto retry;
  613. }
  614. }
  615. type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
  616. TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
  617. iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
  618. params->sta->tdls_initiator, 0,
  619. params->chandef, params->timestamp,
  620. params->switch_time,
  621. params->switch_timeout,
  622. params->tmpl_skb,
  623. params->ch_sw_tm_ie);
  624. retry:
  625. /* register a timeout in case we don't succeed in switching */
  626. delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
  627. 1024 / 1000;
  628. mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
  629. msecs_to_jiffies(delay));
  630. mutex_unlock(&mvm->mutex);
  631. }