0001
0002
0003
0004
0005
0006
0007 #include <linux/jiffies.h>
0008 #include <net/mac80211.h>
0009
0010 #include "fw/notif-wait.h"
0011 #include "iwl-trans.h"
0012 #include "fw-api.h"
0013 #include "time-event.h"
0014 #include "mvm.h"
0015 #include "iwl-io.h"
0016 #include "iwl-prph.h"
0017
0018
0019
0020
0021
0022 #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
0023 #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
0024
0025 void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
0026 struct iwl_mvm_time_event_data *te_data)
0027 {
0028 lockdep_assert_held(&mvm->time_event_lock);
0029
0030 if (!te_data || !te_data->vif)
0031 return;
0032
0033 list_del(&te_data->list);
0034
0035
0036
0037
0038
0039 INIT_LIST_HEAD(&te_data->list);
0040
0041 te_data->running = false;
0042 te_data->uid = 0;
0043 te_data->id = TE_MAX;
0044 te_data->vif = NULL;
0045 }
0046
0047 void iwl_mvm_roc_done_wk(struct work_struct *wk)
0048 {
0049 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
0050
0051
0052
0053
0054
0055
0056
0057
0058 clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0059
0060 synchronize_net();
0061
0062
0063
0064
0065
0066
0067
0068
0069 mutex_lock(&mvm->mutex);
0070 if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
0071 struct iwl_mvm_vif *mvmvif;
0072
0073
0074
0075
0076
0077
0078
0079
0080 if (!WARN_ON(!mvm->p2p_device_vif)) {
0081 mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
0082 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
0083 }
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093 if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
0094
0095 iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true);
0096
0097
0098
0099
0100 if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12)
0101 iwl_mvm_rm_aux_sta(mvm);
0102 }
0103
0104 mutex_unlock(&mvm->mutex);
0105 }
0106
0107 static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
0108 {
0109
0110
0111
0112
0113
0114
0115
0116 schedule_work(&mvm->roc_done_wk);
0117 }
0118
0119 static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
0120 {
0121 struct ieee80211_vif *csa_vif;
0122
0123 rcu_read_lock();
0124
0125 csa_vif = rcu_dereference(mvm->csa_vif);
0126 if (!csa_vif || !csa_vif->bss_conf.csa_active)
0127 goto out_unlock;
0128
0129 IWL_DEBUG_TE(mvm, "CSA NOA started\n");
0130
0131
0132
0133
0134
0135
0136
0137 if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
0138 IWL_WARN(mvm, "CSA NOA started too early\n");
0139 goto out_unlock;
0140 }
0141
0142 ieee80211_csa_finish(csa_vif);
0143
0144 rcu_read_unlock();
0145
0146 RCU_INIT_POINTER(mvm->csa_vif, NULL);
0147
0148 return;
0149
0150 out_unlock:
0151 rcu_read_unlock();
0152 }
0153
0154 static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
0155 struct ieee80211_vif *vif,
0156 const char *errmsg)
0157 {
0158 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0159
0160 if (vif->type != NL80211_IFTYPE_STATION)
0161 return false;
0162
0163 if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
0164 vif->bss_conf.dtim_period)
0165 return false;
0166 if (errmsg)
0167 IWL_ERR(mvm, "%s\n", errmsg);
0168
0169 if (mvmvif->csa_bcn_pending) {
0170 struct iwl_mvm_sta *mvmsta;
0171
0172 rcu_read_lock();
0173 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
0174 if (!WARN_ON(!mvmsta))
0175 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
0176 rcu_read_unlock();
0177 }
0178
0179 if (vif->cfg.assoc) {
0180
0181
0182
0183
0184 iwl_dbg_tlv_time_point(&mvm->fwrt,
0185 IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
0186 NULL);
0187 }
0188
0189 iwl_mvm_connection_loss(mvm, vif, errmsg);
0190 return true;
0191 }
0192
0193 static void
0194 iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
0195 struct iwl_mvm_time_event_data *te_data,
0196 struct iwl_time_event_notif *notif)
0197 {
0198 struct ieee80211_vif *vif = te_data->vif;
0199 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0200
0201 if (!notif->status)
0202 IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
0203
0204 switch (te_data->vif->type) {
0205 case NL80211_IFTYPE_AP:
0206 if (!notif->status)
0207 mvmvif->csa_failed = true;
0208 iwl_mvm_csa_noa_start(mvm);
0209 break;
0210 case NL80211_IFTYPE_STATION:
0211 if (!notif->status) {
0212 iwl_mvm_connection_loss(mvm, vif,
0213 "CSA TE failed to start");
0214 break;
0215 }
0216 iwl_mvm_csa_client_absent(mvm, te_data->vif);
0217 cancel_delayed_work(&mvmvif->csa_work);
0218 ieee80211_chswitch_done(te_data->vif, true);
0219 break;
0220 default:
0221
0222 WARN_ON_ONCE(1);
0223 break;
0224 }
0225
0226
0227 iwl_mvm_te_clear_data(mvm, te_data);
0228 }
0229
0230 static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
0231 struct iwl_time_event_notif *notif,
0232 struct iwl_mvm_time_event_data *te_data)
0233 {
0234 struct iwl_fw_dbg_trigger_tlv *trig;
0235 struct iwl_fw_dbg_trigger_time_event *te_trig;
0236 int i;
0237
0238 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
0239 ieee80211_vif_to_wdev(te_data->vif),
0240 FW_DBG_TRIGGER_TIME_EVENT);
0241 if (!trig)
0242 return;
0243
0244 te_trig = (void *)trig->data;
0245
0246 for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
0247 u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
0248 u32 trig_action_bitmap =
0249 le32_to_cpu(te_trig->time_events[i].action_bitmap);
0250 u32 trig_status_bitmap =
0251 le32_to_cpu(te_trig->time_events[i].status_bitmap);
0252
0253 if (trig_te_id != te_data->id ||
0254 !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
0255 !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
0256 continue;
0257
0258 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
0259 "Time event %d Action 0x%x received status: %d",
0260 te_data->id,
0261 le32_to_cpu(notif->action),
0262 le32_to_cpu(notif->status));
0263 break;
0264 }
0265 }
0266
0267 static void iwl_mvm_p2p_roc_finished(struct iwl_mvm *mvm)
0268 {
0269
0270
0271
0272
0273
0274
0275 if (!test_and_set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status))
0276 iwl_mvm_roc_finished(mvm);
0277 }
0278
0279
0280
0281
0282
0283
0284
0285
0286 static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
0287 struct iwl_mvm_time_event_data *te_data,
0288 struct iwl_time_event_notif *notif)
0289 {
0290 lockdep_assert_held(&mvm->time_event_lock);
0291
0292 IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
0293 le32_to_cpu(notif->unique_id),
0294 le32_to_cpu(notif->action));
0295
0296 iwl_mvm_te_check_trigger(mvm, notif, te_data);
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 if (!le32_to_cpu(notif->status)) {
0307 const char *msg;
0308
0309 if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
0310 msg = "Time Event start notification failure";
0311 else
0312 msg = "Time Event end notification failure";
0313
0314 IWL_DEBUG_TE(mvm, "%s\n", msg);
0315
0316 if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
0317 iwl_mvm_te_clear_data(mvm, te_data);
0318 return;
0319 }
0320 }
0321
0322 if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
0323 IWL_DEBUG_TE(mvm,
0324 "TE ended - current time %lu, estimated end %lu\n",
0325 jiffies, te_data->end_jiffies);
0326
0327 switch (te_data->vif->type) {
0328 case NL80211_IFTYPE_P2P_DEVICE:
0329 ieee80211_remain_on_channel_expired(mvm->hw);
0330 iwl_mvm_p2p_roc_finished(mvm);
0331 break;
0332 case NL80211_IFTYPE_STATION:
0333
0334
0335
0336
0337
0338 if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
0339 IWL_DEBUG_TE(mvm,
0340 "No beacon heard and the CS time event is over, don't disconnect\n");
0341 break;
0342 }
0343
0344
0345
0346
0347
0348 iwl_mvm_te_check_disconnect(mvm, te_data->vif,
0349 !te_data->vif->cfg.assoc ?
0350 "Not associated and the time event is over already..." :
0351 "No beacon heard and the time event is over already...");
0352 break;
0353 default:
0354 break;
0355 }
0356
0357 iwl_mvm_te_clear_data(mvm, te_data);
0358 } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
0359 te_data->running = true;
0360 te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
0361
0362 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
0363 set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0364 ieee80211_ready_on_channel(mvm->hw);
0365 } else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
0366 iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
0367 }
0368 } else {
0369 IWL_WARN(mvm, "Got TE with unknown action\n");
0370 }
0371 }
0372
0373
0374
0375
0376 static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
0377 struct iwl_time_event_notif *notif)
0378 {
0379 struct iwl_mvm_time_event_data *te_data, *tmp;
0380 bool aux_roc_te = false;
0381
0382 list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
0383 if (le32_to_cpu(notif->unique_id) == te_data->uid) {
0384 aux_roc_te = true;
0385 break;
0386 }
0387 }
0388 if (!aux_roc_te)
0389 return -EINVAL;
0390
0391 iwl_mvm_te_check_trigger(mvm, notif, te_data);
0392
0393 IWL_DEBUG_TE(mvm,
0394 "Aux ROC time event notification - UID = 0x%x action %d (error = %d)\n",
0395 le32_to_cpu(notif->unique_id),
0396 le32_to_cpu(notif->action), le32_to_cpu(notif->status));
0397
0398 if (!le32_to_cpu(notif->status) ||
0399 le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
0400
0401 ieee80211_remain_on_channel_expired(mvm->hw);
0402 iwl_mvm_roc_finished(mvm);
0403 list_del(&te_data->list);
0404 te_data->running = false;
0405 te_data->vif = NULL;
0406 te_data->uid = 0;
0407 te_data->id = TE_MAX;
0408 } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
0409 set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
0410 te_data->running = true;
0411 ieee80211_ready_on_channel(mvm->hw);
0412 } else {
0413 IWL_DEBUG_TE(mvm,
0414 "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
0415 le32_to_cpu(notif->action));
0416 return -EINVAL;
0417 }
0418
0419 return 0;
0420 }
0421
0422
0423
0424
0425 void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
0426 struct iwl_rx_cmd_buffer *rxb)
0427 {
0428 struct iwl_rx_packet *pkt = rxb_addr(rxb);
0429 struct iwl_time_event_notif *notif = (void *)pkt->data;
0430 struct iwl_mvm_time_event_data *te_data, *tmp;
0431
0432 IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
0433 le32_to_cpu(notif->unique_id),
0434 le32_to_cpu(notif->action));
0435
0436 spin_lock_bh(&mvm->time_event_lock);
0437
0438 if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
0439 goto unlock;
0440
0441 list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
0442 if (le32_to_cpu(notif->unique_id) == te_data->uid)
0443 iwl_mvm_te_handle_notif(mvm, te_data, notif);
0444 }
0445 unlock:
0446 spin_unlock_bh(&mvm->time_event_lock);
0447 }
0448
0449 static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
0450 struct iwl_rx_packet *pkt, void *data)
0451 {
0452 struct iwl_mvm *mvm =
0453 container_of(notif_wait, struct iwl_mvm, notif_wait);
0454 struct iwl_mvm_time_event_data *te_data = data;
0455 struct iwl_time_event_notif *resp;
0456 int resp_len = iwl_rx_packet_payload_len(pkt);
0457
0458 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
0459 return true;
0460
0461 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
0462 IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
0463 return true;
0464 }
0465
0466 resp = (void *)pkt->data;
0467
0468
0469 if (le32_to_cpu(resp->unique_id) != te_data->uid)
0470 return false;
0471
0472 IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
0473 te_data->uid);
0474 if (!resp->status)
0475 IWL_ERR(mvm,
0476 "TIME_EVENT_NOTIFICATION received but not executed\n");
0477
0478 return true;
0479 }
0480
0481 static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
0482 struct iwl_rx_packet *pkt, void *data)
0483 {
0484 struct iwl_mvm *mvm =
0485 container_of(notif_wait, struct iwl_mvm, notif_wait);
0486 struct iwl_mvm_time_event_data *te_data = data;
0487 struct iwl_time_event_resp *resp;
0488 int resp_len = iwl_rx_packet_payload_len(pkt);
0489
0490 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
0491 return true;
0492
0493 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
0494 IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
0495 return true;
0496 }
0497
0498 resp = (void *)pkt->data;
0499
0500
0501 if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
0502 return false;
0503
0504 te_data->uid = le32_to_cpu(resp->unique_id);
0505 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
0506 te_data->uid);
0507 return true;
0508 }
0509
0510 static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
0511 struct ieee80211_vif *vif,
0512 struct iwl_mvm_time_event_data *te_data,
0513 struct iwl_time_event_cmd *te_cmd)
0514 {
0515 static const u16 time_event_response[] = { TIME_EVENT_CMD };
0516 struct iwl_notification_wait wait_time_event;
0517 int ret;
0518
0519 lockdep_assert_held(&mvm->mutex);
0520
0521 IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
0522 le32_to_cpu(te_cmd->duration));
0523
0524 spin_lock_bh(&mvm->time_event_lock);
0525 if (WARN_ON(te_data->id != TE_MAX)) {
0526 spin_unlock_bh(&mvm->time_event_lock);
0527 return -EIO;
0528 }
0529 te_data->vif = vif;
0530 te_data->duration = le32_to_cpu(te_cmd->duration);
0531 te_data->id = le32_to_cpu(te_cmd->id);
0532 list_add_tail(&te_data->list, &mvm->time_event_list);
0533 spin_unlock_bh(&mvm->time_event_lock);
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
0545 time_event_response,
0546 ARRAY_SIZE(time_event_response),
0547 iwl_mvm_time_event_response, te_data);
0548
0549 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
0550 sizeof(*te_cmd), te_cmd);
0551 if (ret) {
0552 IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
0553 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
0554 goto out_clear_te;
0555 }
0556
0557
0558 ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
0559
0560 WARN_ON_ONCE(ret);
0561
0562 if (ret) {
0563 out_clear_te:
0564 spin_lock_bh(&mvm->time_event_lock);
0565 iwl_mvm_te_clear_data(mvm, te_data);
0566 spin_unlock_bh(&mvm->time_event_lock);
0567 }
0568 return ret;
0569 }
0570
0571 void iwl_mvm_protect_session(struct iwl_mvm *mvm,
0572 struct ieee80211_vif *vif,
0573 u32 duration, u32 min_duration,
0574 u32 max_delay, bool wait_for_notif)
0575 {
0576 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0577 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0578 const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
0579 struct iwl_notification_wait wait_te_notif;
0580 struct iwl_time_event_cmd time_cmd = {};
0581
0582 lockdep_assert_held(&mvm->mutex);
0583
0584 if (te_data->running &&
0585 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
0586 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
0587 jiffies_to_msecs(te_data->end_jiffies - jiffies));
0588 return;
0589 }
0590
0591 if (te_data->running) {
0592 IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
0593 te_data->uid,
0594 jiffies_to_msecs(te_data->end_jiffies - jiffies));
0595
0596
0597
0598
0599
0600
0601
0602
0603 iwl_mvm_stop_session_protection(mvm, vif);
0604 }
0605
0606 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
0607 time_cmd.id_and_color =
0608 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0609 time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
0610
0611 time_cmd.apply_time = cpu_to_le32(0);
0612
0613 time_cmd.max_frags = TE_V2_FRAG_NONE;
0614 time_cmd.max_delay = cpu_to_le32(max_delay);
0615
0616 time_cmd.interval = cpu_to_le32(1);
0617 time_cmd.duration = cpu_to_le32(duration);
0618 time_cmd.repeat = 1;
0619 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
0620 TE_V2_NOTIF_HOST_EVENT_END |
0621 TE_V2_START_IMMEDIATELY);
0622
0623 if (!wait_for_notif) {
0624 iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
0625 return;
0626 }
0627
0628
0629
0630
0631
0632 iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
0633 te_notif_response,
0634 ARRAY_SIZE(te_notif_response),
0635 iwl_mvm_te_notif, te_data);
0636
0637
0638 if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
0639 IWL_ERR(mvm, "Failed to add TE to protect session\n");
0640 iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
0641 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
0642 TU_TO_JIFFIES(max_delay))) {
0643 IWL_ERR(mvm, "Failed to protect session until TE\n");
0644 }
0645 }
0646
0647 static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
0648 struct iwl_mvm_vif *mvmvif,
0649 u32 id)
0650 {
0651 struct iwl_mvm_session_prot_cmd cmd = {
0652 .id_and_color =
0653 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
0654 mvmvif->color)),
0655 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
0656 .conf_id = cpu_to_le32(id),
0657 };
0658 int ret;
0659
0660 ret = iwl_mvm_send_cmd_pdu(mvm,
0661 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
0662 0, sizeof(cmd), &cmd);
0663 if (ret)
0664 IWL_ERR(mvm,
0665 "Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
0666 }
0667
0668 static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
0669 struct iwl_mvm_time_event_data *te_data,
0670 u32 *uid)
0671 {
0672 u32 id;
0673 struct iwl_mvm_vif *mvmvif;
0674 enum nl80211_iftype iftype;
0675
0676 if (!te_data->vif)
0677 return false;
0678
0679 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
0680 iftype = te_data->vif->type;
0681
0682
0683
0684
0685
0686 spin_lock_bh(&mvm->time_event_lock);
0687
0688
0689 *uid = te_data->uid;
0690 id = te_data->id;
0691
0692
0693
0694
0695 iwl_mvm_te_clear_data(mvm, te_data);
0696 spin_unlock_bh(&mvm->time_event_lock);
0697
0698
0699
0700
0701
0702
0703 if (fw_has_capa(&mvm->fw->ucode_capa,
0704 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
0705 id != HOT_SPOT_CMD) {
0706 if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
0707
0708 iwl_mvm_cancel_session_protection(mvm, mvmvif, id);
0709 if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
0710 iwl_mvm_p2p_roc_finished(mvm);
0711 }
0712 }
0713 return false;
0714 } else {
0715
0716
0717
0718
0719 if (id == TE_MAX) {
0720 IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
0721 return false;
0722 }
0723 }
0724
0725 return true;
0726 }
0727
0728
0729
0730
0731
0732
0733
0734 static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
0735 struct iwl_mvm_vif *mvmvif,
0736 struct iwl_mvm_time_event_data *te_data)
0737 {
0738 struct iwl_hs20_roc_req aux_cmd = {};
0739 u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
0740
0741 u32 uid;
0742 int ret;
0743
0744 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
0745 return;
0746
0747 aux_cmd.event_unique_id = cpu_to_le32(uid);
0748 aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
0749 aux_cmd.id_and_color =
0750 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0751 IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
0752 le32_to_cpu(aux_cmd.event_unique_id));
0753 ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
0754 len, &aux_cmd);
0755
0756 if (WARN_ON(ret))
0757 return;
0758 }
0759
0760
0761
0762
0763
0764
0765 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
0766 struct iwl_mvm_vif *mvmvif,
0767 struct iwl_mvm_time_event_data *te_data)
0768 {
0769 struct iwl_time_event_cmd time_cmd = {};
0770 u32 uid;
0771 int ret;
0772
0773 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
0774 return;
0775
0776
0777 time_cmd.id = cpu_to_le32(uid);
0778 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
0779 time_cmd.id_and_color =
0780 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0781
0782 IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
0783 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
0784 sizeof(time_cmd), &time_cmd);
0785 if (ret)
0786 IWL_ERR(mvm, "Couldn't remove the time event\n");
0787 }
0788
0789 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
0790 struct ieee80211_vif *vif)
0791 {
0792 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0793 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0794 u32 id;
0795
0796 lockdep_assert_held(&mvm->mutex);
0797
0798 spin_lock_bh(&mvm->time_event_lock);
0799 id = te_data->id;
0800 spin_unlock_bh(&mvm->time_event_lock);
0801
0802 if (fw_has_capa(&mvm->fw->ucode_capa,
0803 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
0804 if (id != SESSION_PROTECT_CONF_ASSOC) {
0805 IWL_DEBUG_TE(mvm,
0806 "don't remove session protection id=%u\n",
0807 id);
0808 return;
0809 }
0810 } else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
0811 IWL_DEBUG_TE(mvm,
0812 "don't remove TE with id=%u (not session protection)\n",
0813 id);
0814 return;
0815 }
0816
0817 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
0818 }
0819
0820 void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
0821 struct iwl_rx_cmd_buffer *rxb)
0822 {
0823 struct iwl_rx_packet *pkt = rxb_addr(rxb);
0824 struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
0825 struct ieee80211_vif *vif;
0826 struct iwl_mvm_vif *mvmvif;
0827
0828 rcu_read_lock();
0829 vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
0830 true);
0831
0832 if (!vif)
0833 goto out_unlock;
0834
0835 mvmvif = iwl_mvm_vif_from_mac80211(vif);
0836
0837
0838 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
0839 struct iwl_mvm_time_event_data *te_data =
0840 &mvmvif->time_event_data;
0841
0842 if (!le32_to_cpu(notif->status)) {
0843 iwl_mvm_te_check_disconnect(mvm, vif,
0844 "Session protection failure");
0845 spin_lock_bh(&mvm->time_event_lock);
0846 iwl_mvm_te_clear_data(mvm, te_data);
0847 spin_unlock_bh(&mvm->time_event_lock);
0848 }
0849
0850 if (le32_to_cpu(notif->start)) {
0851 spin_lock_bh(&mvm->time_event_lock);
0852 te_data->running = le32_to_cpu(notif->start);
0853 te_data->end_jiffies =
0854 TU_TO_EXP_TIME(te_data->duration);
0855 spin_unlock_bh(&mvm->time_event_lock);
0856 } else {
0857
0858
0859
0860
0861 iwl_mvm_te_check_disconnect(mvm, vif,
0862 !vif->cfg.assoc ?
0863 "Not associated and the session protection is over already..." :
0864 "No beacon heard and the session protection is over already...");
0865 spin_lock_bh(&mvm->time_event_lock);
0866 iwl_mvm_te_clear_data(mvm, te_data);
0867 spin_unlock_bh(&mvm->time_event_lock);
0868 }
0869
0870 goto out_unlock;
0871 }
0872
0873 if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
0874
0875 mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
0876 ieee80211_remain_on_channel_expired(mvm->hw);
0877 iwl_mvm_p2p_roc_finished(mvm);
0878 } else if (le32_to_cpu(notif->start)) {
0879 if (WARN_ON(mvmvif->time_event_data.id !=
0880 le32_to_cpu(notif->conf_id)))
0881 goto out_unlock;
0882 set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0883 ieee80211_ready_on_channel(mvm->hw);
0884 }
0885
0886 out_unlock:
0887 rcu_read_unlock();
0888 }
0889
0890 static int
0891 iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
0892 struct ieee80211_vif *vif,
0893 int duration,
0894 enum ieee80211_roc_type type)
0895 {
0896 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0897 struct iwl_mvm_session_prot_cmd cmd = {
0898 .id_and_color =
0899 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
0900 mvmvif->color)),
0901 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
0902 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
0903 };
0904
0905 lockdep_assert_held(&mvm->mutex);
0906
0907
0908
0909
0910 switch (type) {
0911 case IEEE80211_ROC_TYPE_NORMAL:
0912 mvmvif->time_event_data.id =
0913 SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
0914 break;
0915 case IEEE80211_ROC_TYPE_MGMT_TX:
0916 mvmvif->time_event_data.id =
0917 SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
0918 break;
0919 default:
0920 WARN_ONCE(1, "Got an invalid ROC type\n");
0921 return -EINVAL;
0922 }
0923
0924 cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
0925 return iwl_mvm_send_cmd_pdu(mvm,
0926 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
0927 0, sizeof(cmd), &cmd);
0928 }
0929
0930 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
0931 int duration, enum ieee80211_roc_type type)
0932 {
0933 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0934 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0935 struct iwl_time_event_cmd time_cmd = {};
0936
0937 lockdep_assert_held(&mvm->mutex);
0938 if (te_data->running) {
0939 IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
0940 return -EBUSY;
0941 }
0942
0943 if (fw_has_capa(&mvm->fw->ucode_capa,
0944 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
0945 return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
0946 duration,
0947 type);
0948
0949 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
0950 time_cmd.id_and_color =
0951 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0952
0953 switch (type) {
0954 case IEEE80211_ROC_TYPE_NORMAL:
0955 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
0956 break;
0957 case IEEE80211_ROC_TYPE_MGMT_TX:
0958 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
0959 break;
0960 default:
0961 WARN_ONCE(1, "Got an invalid ROC type\n");
0962 return -EINVAL;
0963 }
0964
0965 time_cmd.apply_time = cpu_to_le32(0);
0966 time_cmd.interval = cpu_to_le32(1);
0967
0968
0969
0970
0971
0972
0973
0974 time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
0975 time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
0976 time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
0977 time_cmd.repeat = 1;
0978 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
0979 TE_V2_NOTIF_HOST_EVENT_END |
0980 TE_V2_START_IMMEDIATELY);
0981
0982 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
0983 }
0984
0985 static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
0986 {
0987 struct iwl_mvm_time_event_data *te_data;
0988
0989 lockdep_assert_held(&mvm->mutex);
0990
0991 spin_lock_bh(&mvm->time_event_lock);
0992
0993
0994
0995
0996
0997
0998
0999
1000 list_for_each_entry(te_data, &mvm->time_event_list, list) {
1001 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1002 goto out;
1003 }
1004
1005
1006
1007
1008 te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1009 struct iwl_mvm_time_event_data,
1010 list);
1011 out:
1012 spin_unlock_bh(&mvm->time_event_lock);
1013 return te_data;
1014 }
1015
1016 void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1017 {
1018 struct iwl_mvm_time_event_data *te_data;
1019 u32 uid;
1020
1021 te_data = iwl_mvm_get_roc_te(mvm);
1022 if (te_data)
1023 __iwl_mvm_remove_time_event(mvm, te_data, &uid);
1024 }
1025
1026 void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1027 {
1028 struct iwl_mvm_vif *mvmvif;
1029 struct iwl_mvm_time_event_data *te_data;
1030
1031 if (fw_has_capa(&mvm->fw->ucode_capa,
1032 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1033 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1034
1035 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1036 iwl_mvm_cancel_session_protection(mvm, mvmvif,
1037 mvmvif->time_event_data.id);
1038 iwl_mvm_p2p_roc_finished(mvm);
1039 } else {
1040 iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1041 &mvmvif->hs_time_event_data);
1042 iwl_mvm_roc_finished(mvm);
1043 }
1044
1045 return;
1046 }
1047
1048 te_data = iwl_mvm_get_roc_te(mvm);
1049 if (!te_data) {
1050 IWL_WARN(mvm, "No remain on channel event\n");
1051 return;
1052 }
1053
1054 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1055
1056 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1057 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1058 iwl_mvm_p2p_roc_finished(mvm);
1059 } else {
1060 iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1061 iwl_mvm_roc_finished(mvm);
1062 }
1063 }
1064
1065 void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1066 struct ieee80211_vif *vif)
1067 {
1068 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1069 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1070 u32 id;
1071
1072 lockdep_assert_held(&mvm->mutex);
1073
1074 spin_lock_bh(&mvm->time_event_lock);
1075 id = te_data->id;
1076 spin_unlock_bh(&mvm->time_event_lock);
1077
1078 if (id != TE_CHANNEL_SWITCH_PERIOD)
1079 return;
1080
1081 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1082 }
1083
1084 int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1085 struct ieee80211_vif *vif,
1086 u32 duration, u32 apply_time)
1087 {
1088 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1089 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1090 struct iwl_time_event_cmd time_cmd = {};
1091
1092 lockdep_assert_held(&mvm->mutex);
1093
1094 if (te_data->running) {
1095 u32 id;
1096
1097 spin_lock_bh(&mvm->time_event_lock);
1098 id = te_data->id;
1099 spin_unlock_bh(&mvm->time_event_lock);
1100
1101 if (id == TE_CHANNEL_SWITCH_PERIOD) {
1102 IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1103 return -EBUSY;
1104 }
1105
1106
1107
1108
1109
1110
1111 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1112 }
1113
1114 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1115 time_cmd.id_and_color =
1116 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1117 time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1118 time_cmd.apply_time = cpu_to_le32(apply_time);
1119 time_cmd.max_frags = TE_V2_FRAG_NONE;
1120 time_cmd.duration = cpu_to_le32(duration);
1121 time_cmd.repeat = 1;
1122 time_cmd.interval = cpu_to_le32(1);
1123 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1124 TE_V2_ABSENCE);
1125 if (!apply_time)
1126 time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1127
1128 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1129 }
1130
1131 static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1132 struct iwl_rx_packet *pkt, void *data)
1133 {
1134 struct iwl_mvm *mvm =
1135 container_of(notif_wait, struct iwl_mvm, notif_wait);
1136 struct iwl_mvm_session_prot_notif *resp;
1137 int resp_len = iwl_rx_packet_payload_len(pkt);
1138
1139 if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1140 pkt->hdr.group_id != MAC_CONF_GROUP))
1141 return true;
1142
1143 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1144 IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1145 return true;
1146 }
1147
1148 resp = (void *)pkt->data;
1149
1150 if (!resp->status)
1151 IWL_ERR(mvm,
1152 "TIME_EVENT_NOTIFICATION received but not executed\n");
1153
1154 return true;
1155 }
1156
1157 void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1158 struct ieee80211_vif *vif,
1159 u32 duration, u32 min_duration,
1160 bool wait_for_notif)
1161 {
1162 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1163 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1164 const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1165 struct iwl_notification_wait wait_notif;
1166 struct iwl_mvm_session_prot_cmd cmd = {
1167 .id_and_color =
1168 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1169 mvmvif->color)),
1170 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1171 .conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1172 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1173 };
1174
1175 lockdep_assert_held(&mvm->mutex);
1176
1177 spin_lock_bh(&mvm->time_event_lock);
1178 if (te_data->running &&
1179 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1180 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1181 jiffies_to_msecs(te_data->end_jiffies - jiffies));
1182 spin_unlock_bh(&mvm->time_event_lock);
1183
1184 return;
1185 }
1186
1187 iwl_mvm_te_clear_data(mvm, te_data);
1188
1189
1190
1191
1192 te_data->id = le32_to_cpu(cmd.conf_id);
1193 te_data->duration = le32_to_cpu(cmd.duration_tu);
1194 te_data->vif = vif;
1195 spin_unlock_bh(&mvm->time_event_lock);
1196
1197 IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1198 le32_to_cpu(cmd.duration_tu));
1199
1200 if (!wait_for_notif) {
1201 if (iwl_mvm_send_cmd_pdu(mvm,
1202 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1203 0, sizeof(cmd), &cmd)) {
1204 IWL_ERR(mvm,
1205 "Couldn't send the SESSION_PROTECTION_CMD\n");
1206 spin_lock_bh(&mvm->time_event_lock);
1207 iwl_mvm_te_clear_data(mvm, te_data);
1208 spin_unlock_bh(&mvm->time_event_lock);
1209 }
1210
1211 return;
1212 }
1213
1214 iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1215 notif, ARRAY_SIZE(notif),
1216 iwl_mvm_session_prot_notif, NULL);
1217
1218 if (iwl_mvm_send_cmd_pdu(mvm,
1219 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1220 0, sizeof(cmd), &cmd)) {
1221 IWL_ERR(mvm,
1222 "Couldn't send the SESSION_PROTECTION_CMD\n");
1223 iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1224 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1225 TU_TO_JIFFIES(100))) {
1226 IWL_ERR(mvm,
1227 "Failed to protect session until session protection\n");
1228 }
1229 }