time-event.c 27 KB

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