time-event.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  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/jiffies.h>
  66. #include <net/mac80211.h>
  67. #include "iwl-notif-wait.h"
  68. #include "iwl-trans.h"
  69. #include "fw-api.h"
  70. #include "time-event.h"
  71. #include "mvm.h"
  72. #include "iwl-io.h"
  73. #include "iwl-prph.h"
  74. /*
  75. * For the high priority TE use a time event type that has similar priority to
  76. * the FW's action scan priority.
  77. */
  78. #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
  79. #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
  80. void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
  81. struct iwl_mvm_time_event_data *te_data)
  82. {
  83. lockdep_assert_held(&mvm->time_event_lock);
  84. if (te_data->id == TE_MAX)
  85. return;
  86. list_del(&te_data->list);
  87. te_data->running = false;
  88. te_data->uid = 0;
  89. te_data->id = TE_MAX;
  90. te_data->vif = NULL;
  91. }
  92. void iwl_mvm_roc_done_wk(struct work_struct *wk)
  93. {
  94. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
  95. u32 queues = 0;
  96. /*
  97. * Clear the ROC_RUNNING /ROC_AUX_RUNNING status bit.
  98. * This will cause the TX path to drop offchannel transmissions.
  99. * That would also be done by mac80211, but it is racy, in particular
  100. * in the case that the time event actually completed in the firmware
  101. * (which is handled in iwl_mvm_te_handle_notif).
  102. */
  103. if (test_and_clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
  104. queues |= BIT(IWL_MVM_OFFCHANNEL_QUEUE);
  105. if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
  106. queues |= BIT(mvm->aux_queue);
  107. iwl_mvm_unref(mvm, IWL_MVM_REF_ROC);
  108. synchronize_net();
  109. /*
  110. * Flush the offchannel queue -- this is called when the time
  111. * event finishes or is cancelled, so that frames queued for it
  112. * won't get stuck on the queue and be transmitted in the next
  113. * time event.
  114. * We have to send the command asynchronously since this cannot
  115. * be under the mutex for locking reasons, but that's not an
  116. * issue as it will have to complete before the next command is
  117. * executed, and a new time event means a new command.
  118. */
  119. iwl_mvm_flush_tx_path(mvm, queues, false);
  120. }
  121. static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
  122. {
  123. /*
  124. * Of course, our status bit is just as racy as mac80211, so in
  125. * addition, fire off the work struct which will drop all frames
  126. * from the hardware queues that made it through the race. First
  127. * it will of course synchronize the TX path to make sure that
  128. * any *new* TX will be rejected.
  129. */
  130. schedule_work(&mvm->roc_done_wk);
  131. }
  132. static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
  133. {
  134. struct ieee80211_vif *csa_vif;
  135. rcu_read_lock();
  136. csa_vif = rcu_dereference(mvm->csa_vif);
  137. if (!csa_vif || !csa_vif->csa_active)
  138. goto out_unlock;
  139. IWL_DEBUG_TE(mvm, "CSA NOA started\n");
  140. /*
  141. * CSA NoA is started but we still have beacons to
  142. * transmit on the current channel.
  143. * So we just do nothing here and the switch
  144. * will be performed on the last TBTT.
  145. */
  146. if (!ieee80211_csa_is_complete(csa_vif)) {
  147. IWL_WARN(mvm, "CSA NOA started too early\n");
  148. goto out_unlock;
  149. }
  150. ieee80211_csa_finish(csa_vif);
  151. rcu_read_unlock();
  152. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  153. return;
  154. out_unlock:
  155. rcu_read_unlock();
  156. }
  157. static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
  158. struct ieee80211_vif *vif,
  159. const char *errmsg)
  160. {
  161. if (vif->type != NL80211_IFTYPE_STATION)
  162. return false;
  163. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period)
  164. return false;
  165. if (errmsg)
  166. IWL_ERR(mvm, "%s\n", errmsg);
  167. ieee80211_connection_loss(vif);
  168. return true;
  169. }
  170. static void
  171. iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
  172. struct iwl_mvm_time_event_data *te_data,
  173. struct iwl_time_event_notif *notif)
  174. {
  175. if (!le32_to_cpu(notif->status)) {
  176. IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
  177. iwl_mvm_te_clear_data(mvm, te_data);
  178. return;
  179. }
  180. switch (te_data->vif->type) {
  181. case NL80211_IFTYPE_AP:
  182. iwl_mvm_csa_noa_start(mvm);
  183. break;
  184. case NL80211_IFTYPE_STATION:
  185. iwl_mvm_csa_client_absent(mvm, te_data->vif);
  186. ieee80211_chswitch_done(te_data->vif, true);
  187. break;
  188. default:
  189. /* should never happen */
  190. WARN_ON_ONCE(1);
  191. break;
  192. }
  193. /* we don't need it anymore */
  194. iwl_mvm_te_clear_data(mvm, te_data);
  195. }
  196. /*
  197. * Handles a FW notification for an event that is known to the driver.
  198. *
  199. * @mvm: the mvm component
  200. * @te_data: the time event data
  201. * @notif: the notification data corresponding the time event data.
  202. */
  203. static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
  204. struct iwl_mvm_time_event_data *te_data,
  205. struct iwl_time_event_notif *notif)
  206. {
  207. lockdep_assert_held(&mvm->time_event_lock);
  208. IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
  209. le32_to_cpu(notif->unique_id),
  210. le32_to_cpu(notif->action));
  211. /*
  212. * The FW sends the start/end time event notifications even for events
  213. * that it fails to schedule. This is indicated in the status field of
  214. * the notification. This happens in cases that the scheduler cannot
  215. * find a schedule that can handle the event (for example requesting a
  216. * P2P Device discoveribility, while there are other higher priority
  217. * events in the system).
  218. */
  219. if (!le32_to_cpu(notif->status)) {
  220. bool start = le32_to_cpu(notif->action) &
  221. TE_V2_NOTIF_HOST_EVENT_START;
  222. IWL_WARN(mvm, "Time Event %s notification failure\n",
  223. start ? "start" : "end");
  224. if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, NULL)) {
  225. iwl_mvm_te_clear_data(mvm, te_data);
  226. return;
  227. }
  228. }
  229. if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
  230. IWL_DEBUG_TE(mvm,
  231. "TE ended - current time %lu, estimated end %lu\n",
  232. jiffies, te_data->end_jiffies);
  233. if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  234. ieee80211_remain_on_channel_expired(mvm->hw);
  235. iwl_mvm_roc_finished(mvm);
  236. }
  237. /*
  238. * By now, we should have finished association
  239. * and know the dtim period.
  240. */
  241. iwl_mvm_te_check_disconnect(mvm, te_data->vif,
  242. "No association and the time event is over already...");
  243. iwl_mvm_te_clear_data(mvm, te_data);
  244. } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
  245. te_data->running = true;
  246. te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
  247. if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  248. set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
  249. iwl_mvm_ref(mvm, IWL_MVM_REF_ROC);
  250. ieee80211_ready_on_channel(mvm->hw);
  251. } else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
  252. iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
  253. }
  254. } else {
  255. IWL_WARN(mvm, "Got TE with unknown action\n");
  256. }
  257. }
  258. /*
  259. * Handle A Aux ROC time event
  260. */
  261. static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
  262. struct iwl_time_event_notif *notif)
  263. {
  264. struct iwl_mvm_time_event_data *te_data, *tmp;
  265. bool aux_roc_te = false;
  266. list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
  267. if (le32_to_cpu(notif->unique_id) == te_data->uid) {
  268. aux_roc_te = true;
  269. break;
  270. }
  271. }
  272. if (!aux_roc_te) /* Not a Aux ROC time event */
  273. return -EINVAL;
  274. if (!le32_to_cpu(notif->status)) {
  275. IWL_DEBUG_TE(mvm,
  276. "ERROR: Aux ROC Time Event %s notification failure\n",
  277. (le32_to_cpu(notif->action) &
  278. TE_V2_NOTIF_HOST_EVENT_START) ? "start" : "end");
  279. return -EINVAL;
  280. }
  281. IWL_DEBUG_TE(mvm,
  282. "Aux ROC time event notification - UID = 0x%x action %d\n",
  283. le32_to_cpu(notif->unique_id),
  284. le32_to_cpu(notif->action));
  285. if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
  286. /* End TE, notify mac80211 */
  287. ieee80211_remain_on_channel_expired(mvm->hw);
  288. iwl_mvm_roc_finished(mvm); /* flush aux queue */
  289. list_del(&te_data->list); /* remove from list */
  290. te_data->running = false;
  291. te_data->vif = NULL;
  292. te_data->uid = 0;
  293. te_data->id = TE_MAX;
  294. } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
  295. set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
  296. te_data->running = true;
  297. ieee80211_ready_on_channel(mvm->hw); /* Start TE */
  298. } else {
  299. IWL_DEBUG_TE(mvm,
  300. "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
  301. le32_to_cpu(notif->action));
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * The Rx handler for time event notifications
  308. */
  309. int iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
  310. struct iwl_rx_cmd_buffer *rxb,
  311. struct iwl_device_cmd *cmd)
  312. {
  313. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  314. struct iwl_time_event_notif *notif = (void *)pkt->data;
  315. struct iwl_mvm_time_event_data *te_data, *tmp;
  316. IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
  317. le32_to_cpu(notif->unique_id),
  318. le32_to_cpu(notif->action));
  319. spin_lock_bh(&mvm->time_event_lock);
  320. /* This time event is triggered for Aux ROC request */
  321. if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
  322. goto unlock;
  323. list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
  324. if (le32_to_cpu(notif->unique_id) == te_data->uid)
  325. iwl_mvm_te_handle_notif(mvm, te_data, notif);
  326. }
  327. unlock:
  328. spin_unlock_bh(&mvm->time_event_lock);
  329. return 0;
  330. }
  331. static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
  332. struct iwl_rx_packet *pkt, void *data)
  333. {
  334. struct iwl_mvm *mvm =
  335. container_of(notif_wait, struct iwl_mvm, notif_wait);
  336. struct iwl_mvm_time_event_data *te_data = data;
  337. struct iwl_time_event_notif *resp;
  338. int resp_len = iwl_rx_packet_payload_len(pkt);
  339. if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
  340. return true;
  341. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  342. IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
  343. return true;
  344. }
  345. resp = (void *)pkt->data;
  346. /* te_data->uid is already set in the TIME_EVENT_CMD response */
  347. if (le32_to_cpu(resp->unique_id) != te_data->uid)
  348. return false;
  349. IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
  350. te_data->uid);
  351. if (!resp->status)
  352. IWL_ERR(mvm,
  353. "TIME_EVENT_NOTIFICATION received but not executed\n");
  354. return true;
  355. }
  356. static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
  357. struct iwl_rx_packet *pkt, void *data)
  358. {
  359. struct iwl_mvm *mvm =
  360. container_of(notif_wait, struct iwl_mvm, notif_wait);
  361. struct iwl_mvm_time_event_data *te_data = data;
  362. struct iwl_time_event_resp *resp;
  363. int resp_len = iwl_rx_packet_payload_len(pkt);
  364. if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
  365. return true;
  366. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  367. IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
  368. return true;
  369. }
  370. resp = (void *)pkt->data;
  371. /* we should never get a response to another TIME_EVENT_CMD here */
  372. if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
  373. return false;
  374. te_data->uid = le32_to_cpu(resp->unique_id);
  375. IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
  376. te_data->uid);
  377. return true;
  378. }
  379. static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
  380. struct ieee80211_vif *vif,
  381. struct iwl_mvm_time_event_data *te_data,
  382. struct iwl_time_event_cmd *te_cmd)
  383. {
  384. static const u8 time_event_response[] = { TIME_EVENT_CMD };
  385. struct iwl_notification_wait wait_time_event;
  386. int ret;
  387. lockdep_assert_held(&mvm->mutex);
  388. IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
  389. le32_to_cpu(te_cmd->duration));
  390. spin_lock_bh(&mvm->time_event_lock);
  391. if (WARN_ON(te_data->id != TE_MAX)) {
  392. spin_unlock_bh(&mvm->time_event_lock);
  393. return -EIO;
  394. }
  395. te_data->vif = vif;
  396. te_data->duration = le32_to_cpu(te_cmd->duration);
  397. te_data->id = le32_to_cpu(te_cmd->id);
  398. list_add_tail(&te_data->list, &mvm->time_event_list);
  399. spin_unlock_bh(&mvm->time_event_lock);
  400. /*
  401. * Use a notification wait, which really just processes the
  402. * command response and doesn't wait for anything, in order
  403. * to be able to process the response and get the UID inside
  404. * the RX path. Using CMD_WANT_SKB doesn't work because it
  405. * stores the buffer and then wakes up this thread, by which
  406. * time another notification (that the time event started)
  407. * might already be processed unsuccessfully.
  408. */
  409. iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
  410. time_event_response,
  411. ARRAY_SIZE(time_event_response),
  412. iwl_mvm_time_event_response, te_data);
  413. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
  414. sizeof(*te_cmd), te_cmd);
  415. if (ret) {
  416. IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
  417. iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
  418. goto out_clear_te;
  419. }
  420. /* No need to wait for anything, so just pass 1 (0 isn't valid) */
  421. ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
  422. /* should never fail */
  423. WARN_ON_ONCE(ret);
  424. if (ret) {
  425. out_clear_te:
  426. spin_lock_bh(&mvm->time_event_lock);
  427. iwl_mvm_te_clear_data(mvm, te_data);
  428. spin_unlock_bh(&mvm->time_event_lock);
  429. }
  430. return ret;
  431. }
  432. void iwl_mvm_protect_session(struct iwl_mvm *mvm,
  433. struct ieee80211_vif *vif,
  434. u32 duration, u32 min_duration,
  435. u32 max_delay, bool wait_for_notif)
  436. {
  437. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  438. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  439. const u8 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
  440. struct iwl_notification_wait wait_te_notif;
  441. struct iwl_time_event_cmd time_cmd = {};
  442. lockdep_assert_held(&mvm->mutex);
  443. if (te_data->running &&
  444. time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
  445. IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
  446. jiffies_to_msecs(te_data->end_jiffies - jiffies));
  447. return;
  448. }
  449. if (te_data->running) {
  450. IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
  451. te_data->uid,
  452. jiffies_to_msecs(te_data->end_jiffies - jiffies));
  453. /*
  454. * we don't have enough time
  455. * cancel the current TE and issue a new one
  456. * Of course it would be better to remove the old one only
  457. * when the new one is added, but we don't care if we are off
  458. * channel for a bit. All we need to do, is not to return
  459. * before we actually begin to be on the channel.
  460. */
  461. iwl_mvm_stop_session_protection(mvm, vif);
  462. }
  463. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  464. time_cmd.id_and_color =
  465. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  466. time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
  467. time_cmd.apply_time =
  468. cpu_to_le32(iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG));
  469. time_cmd.max_frags = TE_V2_FRAG_NONE;
  470. time_cmd.max_delay = cpu_to_le32(max_delay);
  471. /* TODO: why do we need to interval = bi if it is not periodic? */
  472. time_cmd.interval = cpu_to_le32(1);
  473. time_cmd.duration = cpu_to_le32(duration);
  474. time_cmd.repeat = 1;
  475. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  476. TE_V2_NOTIF_HOST_EVENT_END |
  477. T2_V2_START_IMMEDIATELY);
  478. if (!wait_for_notif) {
  479. iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  480. return;
  481. }
  482. /*
  483. * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
  484. * right after we send the time event
  485. */
  486. iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
  487. te_notif_response,
  488. ARRAY_SIZE(te_notif_response),
  489. iwl_mvm_te_notif, te_data);
  490. /* If TE was sent OK - wait for the notification that started */
  491. if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
  492. IWL_ERR(mvm, "Failed to add TE to protect session\n");
  493. iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
  494. } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
  495. TU_TO_JIFFIES(max_delay))) {
  496. IWL_ERR(mvm, "Failed to protect session until TE\n");
  497. }
  498. }
  499. static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
  500. struct iwl_mvm_time_event_data *te_data,
  501. u32 *uid)
  502. {
  503. u32 id;
  504. /*
  505. * It is possible that by the time we got to this point the time
  506. * event was already removed.
  507. */
  508. spin_lock_bh(&mvm->time_event_lock);
  509. /* Save time event uid before clearing its data */
  510. *uid = te_data->uid;
  511. id = te_data->id;
  512. /*
  513. * The clear_data function handles time events that were already removed
  514. */
  515. iwl_mvm_te_clear_data(mvm, te_data);
  516. spin_unlock_bh(&mvm->time_event_lock);
  517. /*
  518. * It is possible that by the time we try to remove it, the time event
  519. * has already ended and removed. In such a case there is no need to
  520. * send a removal command.
  521. */
  522. if (id == TE_MAX) {
  523. IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
  524. return false;
  525. }
  526. return true;
  527. }
  528. /*
  529. * Explicit request to remove a aux roc time event. The removal of a time
  530. * event needs to be synchronized with the flow of a time event's end
  531. * notification, which also removes the time event from the op mode
  532. * data structures.
  533. */
  534. static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
  535. struct iwl_mvm_vif *mvmvif,
  536. struct iwl_mvm_time_event_data *te_data)
  537. {
  538. struct iwl_hs20_roc_req aux_cmd = {};
  539. u32 uid;
  540. int ret;
  541. if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
  542. return;
  543. aux_cmd.event_unique_id = cpu_to_le32(uid);
  544. aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  545. aux_cmd.id_and_color =
  546. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  547. IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
  548. le32_to_cpu(aux_cmd.event_unique_id));
  549. ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
  550. sizeof(aux_cmd), &aux_cmd);
  551. if (WARN_ON(ret))
  552. return;
  553. }
  554. /*
  555. * Explicit request to remove a time event. The removal of a time event needs to
  556. * be synchronized with the flow of a time event's end notification, which also
  557. * removes the time event from the op mode data structures.
  558. */
  559. void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
  560. struct iwl_mvm_vif *mvmvif,
  561. struct iwl_mvm_time_event_data *te_data)
  562. {
  563. struct iwl_time_event_cmd time_cmd = {};
  564. u32 uid;
  565. int ret;
  566. if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
  567. return;
  568. /* When we remove a TE, the UID is to be set in the id field */
  569. time_cmd.id = cpu_to_le32(uid);
  570. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  571. time_cmd.id_and_color =
  572. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  573. IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
  574. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
  575. sizeof(time_cmd), &time_cmd);
  576. if (WARN_ON(ret))
  577. return;
  578. }
  579. void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
  580. struct ieee80211_vif *vif)
  581. {
  582. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  583. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  584. lockdep_assert_held(&mvm->mutex);
  585. iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
  586. }
  587. int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  588. int duration, enum ieee80211_roc_type type)
  589. {
  590. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  591. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  592. struct iwl_time_event_cmd time_cmd = {};
  593. lockdep_assert_held(&mvm->mutex);
  594. if (te_data->running) {
  595. IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
  596. return -EBUSY;
  597. }
  598. /*
  599. * Flush the done work, just in case it's still pending, so that
  600. * the work it does can complete and we can accept new frames.
  601. */
  602. flush_work(&mvm->roc_done_wk);
  603. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  604. time_cmd.id_and_color =
  605. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  606. switch (type) {
  607. case IEEE80211_ROC_TYPE_NORMAL:
  608. time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
  609. break;
  610. case IEEE80211_ROC_TYPE_MGMT_TX:
  611. time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
  612. break;
  613. default:
  614. WARN_ONCE(1, "Got an invalid ROC type\n");
  615. return -EINVAL;
  616. }
  617. time_cmd.apply_time = cpu_to_le32(0);
  618. time_cmd.interval = cpu_to_le32(1);
  619. /*
  620. * The P2P Device TEs can have lower priority than other events
  621. * that are being scheduled by the driver/fw, and thus it might not be
  622. * scheduled. To improve the chances of it being scheduled, allow them
  623. * to be fragmented, and in addition allow them to be delayed.
  624. */
  625. time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
  626. time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
  627. time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
  628. time_cmd.repeat = 1;
  629. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  630. TE_V2_NOTIF_HOST_EVENT_END |
  631. T2_V2_START_IMMEDIATELY);
  632. return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  633. }
  634. void iwl_mvm_stop_roc(struct iwl_mvm *mvm)
  635. {
  636. struct iwl_mvm_vif *mvmvif;
  637. struct iwl_mvm_time_event_data *te_data;
  638. bool is_p2p = false;
  639. lockdep_assert_held(&mvm->mutex);
  640. mvmvif = NULL;
  641. spin_lock_bh(&mvm->time_event_lock);
  642. /*
  643. * Iterate over the list of time events and find the time event that is
  644. * associated with a P2P_DEVICE interface.
  645. * This assumes that a P2P_DEVICE interface can have only a single time
  646. * event at any given time and this time event coresponds to a ROC
  647. * request
  648. */
  649. list_for_each_entry(te_data, &mvm->time_event_list, list) {
  650. if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  651. mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
  652. is_p2p = true;
  653. goto remove_te;
  654. }
  655. }
  656. /*
  657. * Iterate over the list of aux roc time events and find the time
  658. * event that is associated with a BSS interface.
  659. * This assumes that a BSS interface can have only a single time
  660. * event at any given time and this time event coresponds to a ROC
  661. * request
  662. */
  663. list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {
  664. mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
  665. goto remove_te;
  666. }
  667. remove_te:
  668. spin_unlock_bh(&mvm->time_event_lock);
  669. if (!mvmvif) {
  670. IWL_WARN(mvm, "No remain on channel event\n");
  671. return;
  672. }
  673. if (is_p2p)
  674. iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
  675. else
  676. iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
  677. iwl_mvm_roc_finished(mvm);
  678. }
  679. int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
  680. struct ieee80211_vif *vif,
  681. u32 duration, u32 apply_time)
  682. {
  683. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  684. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  685. struct iwl_time_event_cmd time_cmd = {};
  686. lockdep_assert_held(&mvm->mutex);
  687. if (te_data->running) {
  688. IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
  689. return -EBUSY;
  690. }
  691. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  692. time_cmd.id_and_color =
  693. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  694. time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
  695. time_cmd.apply_time = cpu_to_le32(apply_time);
  696. time_cmd.max_frags = TE_V2_FRAG_NONE;
  697. time_cmd.duration = cpu_to_le32(duration);
  698. time_cmd.repeat = 1;
  699. time_cmd.interval = cpu_to_le32(1);
  700. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  701. TE_V2_ABSENCE);
  702. return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  703. }