time-event.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. if (!le32_to_cpu(notif->status)) {
  326. IWL_DEBUG_TE(mvm,
  327. "ERROR: Aux ROC Time Event %s notification failure\n",
  328. (le32_to_cpu(notif->action) &
  329. TE_V2_NOTIF_HOST_EVENT_START) ? "start" : "end");
  330. return -EINVAL;
  331. }
  332. IWL_DEBUG_TE(mvm,
  333. "Aux ROC time event notification - UID = 0x%x action %d\n",
  334. le32_to_cpu(notif->unique_id),
  335. le32_to_cpu(notif->action));
  336. if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
  337. /* End TE, notify mac80211 */
  338. ieee80211_remain_on_channel_expired(mvm->hw);
  339. iwl_mvm_roc_finished(mvm); /* flush aux queue */
  340. list_del(&te_data->list); /* remove from list */
  341. te_data->running = false;
  342. te_data->vif = NULL;
  343. te_data->uid = 0;
  344. te_data->id = TE_MAX;
  345. } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
  346. set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
  347. te_data->running = true;
  348. iwl_mvm_ref(mvm, IWL_MVM_REF_ROC_AUX);
  349. ieee80211_ready_on_channel(mvm->hw); /* Start TE */
  350. } else {
  351. IWL_DEBUG_TE(mvm,
  352. "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
  353. le32_to_cpu(notif->action));
  354. return -EINVAL;
  355. }
  356. return 0;
  357. }
  358. /*
  359. * The Rx handler for time event notifications
  360. */
  361. void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
  362. struct iwl_rx_cmd_buffer *rxb)
  363. {
  364. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  365. struct iwl_time_event_notif *notif = (void *)pkt->data;
  366. struct iwl_mvm_time_event_data *te_data, *tmp;
  367. IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
  368. le32_to_cpu(notif->unique_id),
  369. le32_to_cpu(notif->action));
  370. spin_lock_bh(&mvm->time_event_lock);
  371. /* This time event is triggered for Aux ROC request */
  372. if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
  373. goto unlock;
  374. list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
  375. if (le32_to_cpu(notif->unique_id) == te_data->uid)
  376. iwl_mvm_te_handle_notif(mvm, te_data, notif);
  377. }
  378. unlock:
  379. spin_unlock_bh(&mvm->time_event_lock);
  380. }
  381. static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
  382. struct iwl_rx_packet *pkt, void *data)
  383. {
  384. struct iwl_mvm *mvm =
  385. container_of(notif_wait, struct iwl_mvm, notif_wait);
  386. struct iwl_mvm_time_event_data *te_data = data;
  387. struct iwl_time_event_notif *resp;
  388. int resp_len = iwl_rx_packet_payload_len(pkt);
  389. if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
  390. return true;
  391. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  392. IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
  393. return true;
  394. }
  395. resp = (void *)pkt->data;
  396. /* te_data->uid is already set in the TIME_EVENT_CMD response */
  397. if (le32_to_cpu(resp->unique_id) != te_data->uid)
  398. return false;
  399. IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
  400. te_data->uid);
  401. if (!resp->status)
  402. IWL_ERR(mvm,
  403. "TIME_EVENT_NOTIFICATION received but not executed\n");
  404. return true;
  405. }
  406. static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
  407. struct iwl_rx_packet *pkt, void *data)
  408. {
  409. struct iwl_mvm *mvm =
  410. container_of(notif_wait, struct iwl_mvm, notif_wait);
  411. struct iwl_mvm_time_event_data *te_data = data;
  412. struct iwl_time_event_resp *resp;
  413. int resp_len = iwl_rx_packet_payload_len(pkt);
  414. if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
  415. return true;
  416. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  417. IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
  418. return true;
  419. }
  420. resp = (void *)pkt->data;
  421. /* we should never get a response to another TIME_EVENT_CMD here */
  422. if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
  423. return false;
  424. te_data->uid = le32_to_cpu(resp->unique_id);
  425. IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
  426. te_data->uid);
  427. return true;
  428. }
  429. static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
  430. struct ieee80211_vif *vif,
  431. struct iwl_mvm_time_event_data *te_data,
  432. struct iwl_time_event_cmd *te_cmd)
  433. {
  434. static const u16 time_event_response[] = { TIME_EVENT_CMD };
  435. struct iwl_notification_wait wait_time_event;
  436. int ret;
  437. lockdep_assert_held(&mvm->mutex);
  438. IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
  439. le32_to_cpu(te_cmd->duration));
  440. spin_lock_bh(&mvm->time_event_lock);
  441. if (WARN_ON(te_data->id != TE_MAX)) {
  442. spin_unlock_bh(&mvm->time_event_lock);
  443. return -EIO;
  444. }
  445. te_data->vif = vif;
  446. te_data->duration = le32_to_cpu(te_cmd->duration);
  447. te_data->id = le32_to_cpu(te_cmd->id);
  448. list_add_tail(&te_data->list, &mvm->time_event_list);
  449. spin_unlock_bh(&mvm->time_event_lock);
  450. /*
  451. * Use a notification wait, which really just processes the
  452. * command response and doesn't wait for anything, in order
  453. * to be able to process the response and get the UID inside
  454. * the RX path. Using CMD_WANT_SKB doesn't work because it
  455. * stores the buffer and then wakes up this thread, by which
  456. * time another notification (that the time event started)
  457. * might already be processed unsuccessfully.
  458. */
  459. iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
  460. time_event_response,
  461. ARRAY_SIZE(time_event_response),
  462. iwl_mvm_time_event_response, te_data);
  463. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
  464. sizeof(*te_cmd), te_cmd);
  465. if (ret) {
  466. IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
  467. iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
  468. goto out_clear_te;
  469. }
  470. /* No need to wait for anything, so just pass 1 (0 isn't valid) */
  471. ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
  472. /* should never fail */
  473. WARN_ON_ONCE(ret);
  474. if (ret) {
  475. out_clear_te:
  476. spin_lock_bh(&mvm->time_event_lock);
  477. iwl_mvm_te_clear_data(mvm, te_data);
  478. spin_unlock_bh(&mvm->time_event_lock);
  479. }
  480. return ret;
  481. }
  482. void iwl_mvm_protect_session(struct iwl_mvm *mvm,
  483. struct ieee80211_vif *vif,
  484. u32 duration, u32 min_duration,
  485. u32 max_delay, bool wait_for_notif)
  486. {
  487. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  488. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  489. const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
  490. struct iwl_notification_wait wait_te_notif;
  491. struct iwl_time_event_cmd time_cmd = {};
  492. lockdep_assert_held(&mvm->mutex);
  493. if (te_data->running &&
  494. time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
  495. IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
  496. jiffies_to_msecs(te_data->end_jiffies - jiffies));
  497. return;
  498. }
  499. if (te_data->running) {
  500. IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
  501. te_data->uid,
  502. jiffies_to_msecs(te_data->end_jiffies - jiffies));
  503. /*
  504. * we don't have enough time
  505. * cancel the current TE and issue a new one
  506. * Of course it would be better to remove the old one only
  507. * when the new one is added, but we don't care if we are off
  508. * channel for a bit. All we need to do, is not to return
  509. * before we actually begin to be on the channel.
  510. */
  511. iwl_mvm_stop_session_protection(mvm, vif);
  512. }
  513. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  514. time_cmd.id_and_color =
  515. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  516. time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
  517. time_cmd.apply_time = cpu_to_le32(0);
  518. time_cmd.max_frags = TE_V2_FRAG_NONE;
  519. time_cmd.max_delay = cpu_to_le32(max_delay);
  520. /* TODO: why do we need to interval = bi if it is not periodic? */
  521. time_cmd.interval = cpu_to_le32(1);
  522. time_cmd.duration = cpu_to_le32(duration);
  523. time_cmd.repeat = 1;
  524. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  525. TE_V2_NOTIF_HOST_EVENT_END |
  526. T2_V2_START_IMMEDIATELY);
  527. if (!wait_for_notif) {
  528. iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  529. return;
  530. }
  531. /*
  532. * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
  533. * right after we send the time event
  534. */
  535. iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
  536. te_notif_response,
  537. ARRAY_SIZE(te_notif_response),
  538. iwl_mvm_te_notif, te_data);
  539. /* If TE was sent OK - wait for the notification that started */
  540. if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
  541. IWL_ERR(mvm, "Failed to add TE to protect session\n");
  542. iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
  543. } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
  544. TU_TO_JIFFIES(max_delay))) {
  545. IWL_ERR(mvm, "Failed to protect session until TE\n");
  546. }
  547. }
  548. static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
  549. struct iwl_mvm_time_event_data *te_data,
  550. u32 *uid)
  551. {
  552. u32 id;
  553. /*
  554. * It is possible that by the time we got to this point the time
  555. * event was already removed.
  556. */
  557. spin_lock_bh(&mvm->time_event_lock);
  558. /* Save time event uid before clearing its data */
  559. *uid = te_data->uid;
  560. id = te_data->id;
  561. /*
  562. * The clear_data function handles time events that were already removed
  563. */
  564. iwl_mvm_te_clear_data(mvm, te_data);
  565. spin_unlock_bh(&mvm->time_event_lock);
  566. /*
  567. * It is possible that by the time we try to remove it, the time event
  568. * has already ended and removed. In such a case there is no need to
  569. * send a removal command.
  570. */
  571. if (id == TE_MAX) {
  572. IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
  573. return false;
  574. }
  575. return true;
  576. }
  577. /*
  578. * Explicit request to remove a aux roc time event. The removal of a time
  579. * event needs to be synchronized with the flow of a time event's end
  580. * notification, which also removes the time event from the op mode
  581. * data structures.
  582. */
  583. static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
  584. struct iwl_mvm_vif *mvmvif,
  585. struct iwl_mvm_time_event_data *te_data)
  586. {
  587. struct iwl_hs20_roc_req aux_cmd = {};
  588. u32 uid;
  589. int ret;
  590. if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
  591. return;
  592. aux_cmd.event_unique_id = cpu_to_le32(uid);
  593. aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  594. aux_cmd.id_and_color =
  595. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  596. IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
  597. le32_to_cpu(aux_cmd.event_unique_id));
  598. ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
  599. sizeof(aux_cmd), &aux_cmd);
  600. if (WARN_ON(ret))
  601. return;
  602. }
  603. /*
  604. * Explicit request to remove a time event. The removal of a time event needs to
  605. * be synchronized with the flow of a time event's end notification, which also
  606. * removes the time event from the op mode data structures.
  607. */
  608. void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
  609. struct iwl_mvm_vif *mvmvif,
  610. struct iwl_mvm_time_event_data *te_data)
  611. {
  612. struct iwl_time_event_cmd time_cmd = {};
  613. u32 uid;
  614. int ret;
  615. if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
  616. return;
  617. /* When we remove a TE, the UID is to be set in the id field */
  618. time_cmd.id = cpu_to_le32(uid);
  619. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  620. time_cmd.id_and_color =
  621. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  622. IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
  623. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
  624. sizeof(time_cmd), &time_cmd);
  625. if (WARN_ON(ret))
  626. return;
  627. }
  628. void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
  629. struct ieee80211_vif *vif)
  630. {
  631. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  632. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  633. lockdep_assert_held(&mvm->mutex);
  634. iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
  635. }
  636. int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  637. int duration, enum ieee80211_roc_type type)
  638. {
  639. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  640. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  641. struct iwl_time_event_cmd time_cmd = {};
  642. lockdep_assert_held(&mvm->mutex);
  643. if (te_data->running) {
  644. IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
  645. return -EBUSY;
  646. }
  647. /*
  648. * Flush the done work, just in case it's still pending, so that
  649. * the work it does can complete and we can accept new frames.
  650. */
  651. flush_work(&mvm->roc_done_wk);
  652. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  653. time_cmd.id_and_color =
  654. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  655. switch (type) {
  656. case IEEE80211_ROC_TYPE_NORMAL:
  657. time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
  658. break;
  659. case IEEE80211_ROC_TYPE_MGMT_TX:
  660. time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
  661. break;
  662. default:
  663. WARN_ONCE(1, "Got an invalid ROC type\n");
  664. return -EINVAL;
  665. }
  666. time_cmd.apply_time = cpu_to_le32(0);
  667. time_cmd.interval = cpu_to_le32(1);
  668. /*
  669. * The P2P Device TEs can have lower priority than other events
  670. * that are being scheduled by the driver/fw, and thus it might not be
  671. * scheduled. To improve the chances of it being scheduled, allow them
  672. * to be fragmented, and in addition allow them to be delayed.
  673. */
  674. time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
  675. time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
  676. time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
  677. time_cmd.repeat = 1;
  678. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  679. TE_V2_NOTIF_HOST_EVENT_END |
  680. T2_V2_START_IMMEDIATELY);
  681. return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  682. }
  683. static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
  684. {
  685. struct iwl_mvm_time_event_data *te_data;
  686. lockdep_assert_held(&mvm->mutex);
  687. spin_lock_bh(&mvm->time_event_lock);
  688. /*
  689. * Iterate over the list of time events and find the time event that is
  690. * associated with a P2P_DEVICE interface.
  691. * This assumes that a P2P_DEVICE interface can have only a single time
  692. * event at any given time and this time event coresponds to a ROC
  693. * request
  694. */
  695. list_for_each_entry(te_data, &mvm->time_event_list, list) {
  696. if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
  697. goto out;
  698. }
  699. /* There can only be at most one AUX ROC time event, we just use the
  700. * list to simplify/unify code. Remove it if it exists.
  701. */
  702. te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
  703. struct iwl_mvm_time_event_data,
  704. list);
  705. out:
  706. spin_unlock_bh(&mvm->time_event_lock);
  707. return te_data;
  708. }
  709. void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
  710. {
  711. struct iwl_mvm_time_event_data *te_data;
  712. u32 uid;
  713. te_data = iwl_mvm_get_roc_te(mvm);
  714. if (te_data)
  715. __iwl_mvm_remove_time_event(mvm, te_data, &uid);
  716. }
  717. void iwl_mvm_stop_roc(struct iwl_mvm *mvm)
  718. {
  719. struct iwl_mvm_vif *mvmvif;
  720. struct iwl_mvm_time_event_data *te_data;
  721. te_data = iwl_mvm_get_roc_te(mvm);
  722. if (!te_data) {
  723. IWL_WARN(mvm, "No remain on channel event\n");
  724. return;
  725. }
  726. mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
  727. if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
  728. iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
  729. else
  730. iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
  731. iwl_mvm_roc_finished(mvm);
  732. }
  733. int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
  734. struct ieee80211_vif *vif,
  735. u32 duration, u32 apply_time)
  736. {
  737. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  738. struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
  739. struct iwl_time_event_cmd time_cmd = {};
  740. lockdep_assert_held(&mvm->mutex);
  741. if (te_data->running) {
  742. IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
  743. return -EBUSY;
  744. }
  745. time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  746. time_cmd.id_and_color =
  747. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
  748. time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
  749. time_cmd.apply_time = cpu_to_le32(apply_time);
  750. time_cmd.max_frags = TE_V2_FRAG_NONE;
  751. time_cmd.duration = cpu_to_le32(duration);
  752. time_cmd.repeat = 1;
  753. time_cmd.interval = cpu_to_le32(1);
  754. time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
  755. TE_V2_ABSENCE);
  756. return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
  757. }