time-event.c 27 KB

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