time-event.c 27 KB

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