tdls.c 21 KB

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