1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2026 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2017 Intel Deutschland GmbH
6 */
7 #include <linux/jiffies.h>
8 #include <net/mac80211.h>
9
10 #include "fw/notif-wait.h"
11 #include "iwl-trans.h"
12 #include "fw-api.h"
13 #include "time-event.h"
14 #include "mvm.h"
15 #include "iwl-io.h"
16 #include "iwl-prph.h"
17
18 /*
19 * For the high priority TE use a time event type that has similar priority to
20 * the FW's action scan priority.
21 */
22 #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
23 #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
24
iwl_mvm_te_clear_data(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data)25 void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
26 struct iwl_mvm_time_event_data *te_data)
27 {
28 lockdep_assert_held(&mvm->time_event_lock);
29
30 if (!te_data || !te_data->vif)
31 return;
32
33 list_del(&te_data->list);
34
35 /*
36 * the list is only used for AUX ROC events so make sure it is always
37 * initialized
38 */
39 INIT_LIST_HEAD(&te_data->list);
40
41 te_data->running = false;
42 te_data->uid = 0;
43 te_data->id = TE_MAX;
44 te_data->vif = NULL;
45 }
46
iwl_mvm_cleanup_roc(struct iwl_mvm * mvm)47 static void iwl_mvm_cleanup_roc(struct iwl_mvm *mvm)
48 {
49 struct ieee80211_vif *vif = mvm->p2p_device_vif;
50
51 lockdep_assert_held(&mvm->mutex);
52
53 /*
54 * Clear the ROC_P2P_RUNNING status bit.
55 * This will cause the TX path to drop offchannel transmissions.
56 * That would also be done by mac80211, but it is racy, in particular
57 * in the case that the time event actually completed in the firmware.
58 *
59 * Also flush the offchannel queue -- this is called when the time
60 * event finishes or is canceled, so that frames queued for it
61 * won't get stuck on the queue and be transmitted in the next
62 * time event.
63 */
64 if (test_and_clear_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status)) {
65 struct iwl_mvm_vif *mvmvif;
66
67 synchronize_net();
68
69 /*
70 * NB: access to this pointer would be racy, but the flush bit
71 * can only be set when we had a P2P-Device VIF, and we have a
72 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
73 * not really racy.
74 */
75
76 if (!WARN_ON(!vif)) {
77 mvmvif = iwl_mvm_vif_from_mac80211(vif);
78 iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,
79 mvmvif->deflink.bcast_sta.tfd_queue_msk);
80
81 if (mvm->mld_api_is_used) {
82 iwl_mvm_mld_rm_bcast_sta(mvm, vif,
83 &vif->bss_conf);
84
85 iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
86 LINK_CONTEXT_MODIFY_ACTIVE,
87 false);
88 } else {
89 iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
90 iwl_mvm_binding_remove_vif(mvm, vif);
91 mvmvif->p2p_in_binding = false;
92 }
93
94 /* Do not remove the PHY context as removing and adding
95 * a PHY context has timing overheads. Leaving it
96 * configured in FW would be useful in case the next ROC
97 * is with the same channel.
98 */
99 }
100 }
101
102 /*
103 * P2P AUX ROC and HS2.0 ROC do not run simultaneously.
104 * Clear the ROC_AUX_RUNNING status bit.
105 * This will cause the TX path to drop offchannel transmissions.
106 * That would also be done by mac80211, but it is racy, in particular
107 * in the case that the time event actually completed in the firmware
108 * (which is handled in iwl_mvm_te_handle_notif).
109 */
110 if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
111 synchronize_net();
112
113 iwl_mvm_flush_sta(mvm, mvm->aux_sta.sta_id,
114 mvm->aux_sta.tfd_queue_msk);
115
116 /* In newer version of this command an aux station is added only
117 * in cases of dedicated tx queue and need to be removed in end
118 * of use. For the even newer mld api, use the appropriate
119 * function.
120 */
121 if (mvm->mld_api_is_used)
122 iwl_mvm_mld_rm_aux_sta(mvm);
123 else if (iwl_mvm_has_new_station_api(mvm->fw))
124 iwl_mvm_rm_aux_sta(mvm);
125 }
126
127 mutex_unlock(&mvm->mutex);
128 }
129
iwl_mvm_roc_done_wk(struct work_struct * wk)130 void iwl_mvm_roc_done_wk(struct work_struct *wk)
131 {
132 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
133
134 mutex_lock(&mvm->mutex);
135 /* Mutex is released inside */
136 iwl_mvm_cleanup_roc(mvm);
137 }
138
iwl_mvm_roc_finished(struct iwl_mvm * mvm)139 static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
140 {
141 /*
142 * Of course, our status bit is just as racy as mac80211, so in
143 * addition, fire off the work struct which will drop all frames
144 * from the hardware queues that made it through the race. First
145 * it will of course synchronize the TX path to make sure that
146 * any *new* TX will be rejected.
147 */
148 schedule_work(&mvm->roc_done_wk);
149 }
150
iwl_mvm_csa_noa_start(struct iwl_mvm * mvm)151 static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
152 {
153 struct ieee80211_vif *csa_vif;
154
155 rcu_read_lock();
156
157 csa_vif = rcu_dereference(mvm->csa_vif);
158 if (!csa_vif || !csa_vif->bss_conf.csa_active)
159 goto out_unlock;
160
161 IWL_DEBUG_TE(mvm, "CSA NOA started\n");
162
163 /*
164 * CSA NoA is started but we still have beacons to
165 * transmit on the current channel.
166 * So we just do nothing here and the switch
167 * will be performed on the last TBTT.
168 */
169 if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {
170 IWL_WARN(mvm, "CSA NOA started too early\n");
171 goto out_unlock;
172 }
173
174 ieee80211_csa_finish(csa_vif, 0);
175
176 rcu_read_unlock();
177
178 RCU_INIT_POINTER(mvm->csa_vif, NULL);
179
180 return;
181
182 out_unlock:
183 rcu_read_unlock();
184 }
185
iwl_mvm_te_check_disconnect(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const char * errmsg)186 static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
187 struct ieee80211_vif *vif,
188 const char *errmsg)
189 {
190 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
191
192 if (vif->type != NL80211_IFTYPE_STATION)
193 return false;
194
195 if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
196 vif->bss_conf.dtim_period)
197 return false;
198 if (errmsg)
199 IWL_ERR(mvm, "%s\n", errmsg);
200
201 if (mvmvif->csa_bcn_pending) {
202 struct iwl_mvm_sta *mvmsta;
203
204 rcu_read_lock();
205 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm,
206 mvmvif->deflink.ap_sta_id);
207 if (!WARN_ON(!mvmsta))
208 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
209 rcu_read_unlock();
210 }
211
212 if (vif->cfg.assoc) {
213 /*
214 * When not associated, this will be called from
215 * iwl_mvm_event_mlme_callback_ini()
216 */
217 iwl_dbg_tlv_time_point(&mvm->fwrt,
218 IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
219 NULL);
220
221 mvmvif->session_prot_connection_loss = true;
222 }
223
224 iwl_mvm_connection_loss(mvm, vif, errmsg);
225 return true;
226 }
227
228 static void
iwl_mvm_te_handle_notify_csa(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)229 iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
230 struct iwl_mvm_time_event_data *te_data,
231 struct iwl_time_event_notif *notif)
232 {
233 struct ieee80211_vif *vif = te_data->vif;
234 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
235
236 if (!notif->status)
237 IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
238
239 switch (te_data->vif->type) {
240 case NL80211_IFTYPE_AP:
241 if (!notif->status)
242 mvmvif->csa_failed = true;
243 iwl_mvm_csa_noa_start(mvm);
244 break;
245 case NL80211_IFTYPE_STATION:
246 if (!notif->status) {
247 iwl_mvm_connection_loss(mvm, vif,
248 "CSA TE failed to start");
249 break;
250 }
251 iwl_mvm_csa_client_absent(mvm, te_data->vif);
252 cancel_delayed_work(&mvmvif->csa_work);
253 ieee80211_chswitch_done(te_data->vif, true, 0);
254 break;
255 default:
256 /* should never happen */
257 WARN_ON_ONCE(1);
258 break;
259 }
260
261 /* we don't need it anymore */
262 iwl_mvm_te_clear_data(mvm, te_data);
263 }
264
iwl_mvm_te_check_trigger(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif,struct iwl_mvm_time_event_data * te_data)265 static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
266 struct iwl_time_event_notif *notif,
267 struct iwl_mvm_time_event_data *te_data)
268 {
269 struct iwl_fw_dbg_trigger_tlv *trig;
270 struct iwl_fw_dbg_trigger_time_event *te_trig;
271 int i;
272
273 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
274 ieee80211_vif_to_wdev(te_data->vif),
275 FW_DBG_TRIGGER_TIME_EVENT);
276 if (!trig)
277 return;
278
279 te_trig = (void *)trig->data;
280
281 for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
282 u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
283 u32 trig_action_bitmap =
284 le32_to_cpu(te_trig->time_events[i].action_bitmap);
285 u32 trig_status_bitmap =
286 le32_to_cpu(te_trig->time_events[i].status_bitmap);
287
288 if (trig_te_id != te_data->id ||
289 !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
290 !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
291 continue;
292
293 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
294 "Time event %d Action 0x%x received status: %d",
295 te_data->id,
296 le32_to_cpu(notif->action),
297 le32_to_cpu(notif->status));
298 break;
299 }
300 }
301
302 /*
303 * Handles a FW notification for an event that is known to the driver.
304 *
305 * @mvm: the mvm component
306 * @te_data: the time event data
307 * @notif: the notification data corresponding the time event data.
308 */
iwl_mvm_te_handle_notif(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)309 static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
310 struct iwl_mvm_time_event_data *te_data,
311 struct iwl_time_event_notif *notif)
312 {
313 lockdep_assert_held(&mvm->time_event_lock);
314
315 IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
316 le32_to_cpu(notif->unique_id),
317 le32_to_cpu(notif->action));
318
319 iwl_mvm_te_check_trigger(mvm, notif, te_data);
320
321 /*
322 * The FW sends the start/end time event notifications even for events
323 * that it fails to schedule. This is indicated in the status field of
324 * the notification. This happens in cases that the scheduler cannot
325 * find a schedule that can handle the event (for example requesting a
326 * P2P Device discoveribility, while there are other higher priority
327 * events in the system).
328 */
329 if (!le32_to_cpu(notif->status)) {
330 const char *msg;
331
332 if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
333 msg = "Time Event start notification failure";
334 else
335 msg = "Time Event end notification failure";
336
337 IWL_DEBUG_TE(mvm, "%s\n", msg);
338
339 if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
340 iwl_mvm_te_clear_data(mvm, te_data);
341 return;
342 }
343 }
344
345 if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
346 IWL_DEBUG_TE(mvm,
347 "TE ended - current time %lu, estimated end %lu\n",
348 jiffies, te_data->end_jiffies);
349
350 switch (te_data->vif->type) {
351 case NL80211_IFTYPE_P2P_DEVICE:
352 ieee80211_remain_on_channel_expired(mvm->hw);
353 iwl_mvm_roc_finished(mvm);
354 break;
355 case NL80211_IFTYPE_STATION:
356 /*
357 * If we are switching channel, don't disconnect
358 * if the time event is already done. Beacons can
359 * be delayed a bit after the switch.
360 */
361 if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
362 IWL_DEBUG_TE(mvm,
363 "No beacon heard and the CS time event is over, don't disconnect\n");
364 break;
365 }
366
367 /*
368 * By now, we should have finished association
369 * and know the dtim period.
370 */
371 iwl_mvm_te_check_disconnect(mvm, te_data->vif,
372 !te_data->vif->cfg.assoc ?
373 "Not associated and the time event is over already..." :
374 "No beacon heard and the time event is over already...");
375 break;
376 default:
377 break;
378 }
379
380 iwl_mvm_te_clear_data(mvm, te_data);
381 } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
382 te_data->running = true;
383 te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
384
385 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
386 set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
387 ieee80211_ready_on_channel(mvm->hw);
388 } else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
389 iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
390 }
391 } else {
392 IWL_WARN(mvm, "Got TE with unknown action\n");
393 }
394 }
395
396 struct iwl_mvm_rx_roc_iterator_data {
397 u32 activity;
398 bool end_activity;
399 bool found;
400 };
401
iwl_mvm_rx_roc_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)402 static void iwl_mvm_rx_roc_iterator(void *_data, u8 *mac,
403 struct ieee80211_vif *vif)
404 {
405 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
406 struct iwl_mvm_rx_roc_iterator_data *data = _data;
407
408 if (mvmvif->roc_activity == data->activity) {
409 data->found = true;
410 if (data->end_activity)
411 mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
412 }
413 }
414
iwl_mvm_rx_roc_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)415 void iwl_mvm_rx_roc_notif(struct iwl_mvm *mvm,
416 struct iwl_rx_cmd_buffer *rxb)
417 {
418 struct iwl_rx_packet *pkt = rxb_addr(rxb);
419 struct iwl_roc_notif *notif = (void *)pkt->data;
420 u32 activity = le32_to_cpu(notif->activity);
421 bool started = le32_to_cpu(notif->success) &&
422 le32_to_cpu(notif->started);
423 struct iwl_mvm_rx_roc_iterator_data data = {
424 .activity = activity,
425 .end_activity = !started,
426 };
427
428 /* Clear vif roc_activity if done (set to ROC_NUM_ACTIVITIES) */
429 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
430 IEEE80211_IFACE_ITER_NORMAL,
431 iwl_mvm_rx_roc_iterator,
432 &data);
433 /*
434 * It is possible that the ROC was canceled
435 * but the notification was already fired.
436 */
437 if (!data.found)
438 return;
439
440 if (started) {
441 set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
442 ieee80211_ready_on_channel(mvm->hw);
443 } else {
444 iwl_mvm_roc_finished(mvm);
445 ieee80211_remain_on_channel_expired(mvm->hw);
446 }
447 }
448
449 /*
450 * Handle A Aux ROC time event
451 */
iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif)452 static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
453 struct iwl_time_event_notif *notif)
454 {
455 struct iwl_mvm_time_event_data *aux_roc_te = NULL, *te_data;
456
457 list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {
458 if (le32_to_cpu(notif->unique_id) == te_data->uid) {
459 aux_roc_te = te_data;
460 break;
461 }
462 }
463 if (!aux_roc_te) /* Not a Aux ROC time event */
464 return -EINVAL;
465
466 iwl_mvm_te_check_trigger(mvm, notif, aux_roc_te);
467
468 IWL_DEBUG_TE(mvm,
469 "Aux ROC time event notification - UID = 0x%x action %d (error = %d)\n",
470 le32_to_cpu(notif->unique_id),
471 le32_to_cpu(notif->action), le32_to_cpu(notif->status));
472
473 if (!le32_to_cpu(notif->status) ||
474 le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
475 /* End TE, notify mac80211 */
476 ieee80211_remain_on_channel_expired(mvm->hw);
477 iwl_mvm_roc_finished(mvm); /* flush aux queue */
478 list_del(&aux_roc_te->list); /* remove from list */
479 aux_roc_te->running = false;
480 aux_roc_te->vif = NULL;
481 aux_roc_te->uid = 0;
482 aux_roc_te->id = TE_MAX;
483 } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
484 set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
485 aux_roc_te->running = true;
486 ieee80211_ready_on_channel(mvm->hw); /* Start TE */
487 } else {
488 IWL_DEBUG_TE(mvm,
489 "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
490 le32_to_cpu(notif->action));
491 return -EINVAL;
492 }
493
494 return 0;
495 }
496
497 /*
498 * The Rx handler for time event notifications
499 */
iwl_mvm_rx_time_event_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)500 void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
501 struct iwl_rx_cmd_buffer *rxb)
502 {
503 struct iwl_rx_packet *pkt = rxb_addr(rxb);
504 struct iwl_time_event_notif *notif = (void *)pkt->data;
505 struct iwl_mvm_time_event_data *te_data, *tmp;
506
507 IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
508 le32_to_cpu(notif->unique_id),
509 le32_to_cpu(notif->action));
510
511 spin_lock_bh(&mvm->time_event_lock);
512 /* This time event is triggered for Aux ROC request */
513 if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
514 goto unlock;
515
516 list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
517 if (le32_to_cpu(notif->unique_id) == te_data->uid)
518 iwl_mvm_te_handle_notif(mvm, te_data, notif);
519 }
520 unlock:
521 spin_unlock_bh(&mvm->time_event_lock);
522 }
523
iwl_mvm_te_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)524 static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
525 struct iwl_rx_packet *pkt, void *data)
526 {
527 struct iwl_mvm *mvm =
528 container_of(notif_wait, struct iwl_mvm, notif_wait);
529 struct iwl_mvm_time_event_data *te_data = data;
530 struct iwl_time_event_notif *resp;
531 int resp_len = iwl_rx_packet_payload_len(pkt);
532
533 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
534 return true;
535
536 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
537 IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
538 return true;
539 }
540
541 resp = (void *)pkt->data;
542
543 /* te_data->uid is already set in the TIME_EVENT_CMD response */
544 if (le32_to_cpu(resp->unique_id) != te_data->uid)
545 return false;
546
547 IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
548 te_data->uid);
549 if (!resp->status)
550 IWL_ERR(mvm,
551 "TIME_EVENT_NOTIFICATION received but not executed\n");
552
553 return true;
554 }
555
iwl_mvm_time_event_response(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)556 static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
557 struct iwl_rx_packet *pkt, void *data)
558 {
559 struct iwl_mvm *mvm =
560 container_of(notif_wait, struct iwl_mvm, notif_wait);
561 struct iwl_mvm_time_event_data *te_data = data;
562 struct iwl_time_event_resp *resp;
563 int resp_len = iwl_rx_packet_payload_len(pkt);
564
565 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
566 return true;
567
568 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
569 IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
570 return true;
571 }
572
573 resp = (void *)pkt->data;
574
575 /* we should never get a response to another TIME_EVENT_CMD here */
576 if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
577 return false;
578
579 te_data->uid = le32_to_cpu(resp->unique_id);
580 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
581 te_data->uid);
582 return true;
583 }
584
iwl_mvm_time_event_send_add(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_cmd * te_cmd)585 static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
586 struct ieee80211_vif *vif,
587 struct iwl_mvm_time_event_data *te_data,
588 struct iwl_time_event_cmd *te_cmd)
589 {
590 static const u16 time_event_response[] = { TIME_EVENT_CMD };
591 struct iwl_notification_wait wait_time_event;
592 int ret;
593
594 lockdep_assert_held(&mvm->mutex);
595
596 IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
597 le32_to_cpu(te_cmd->duration));
598
599 spin_lock_bh(&mvm->time_event_lock);
600 if (WARN_ON(te_data->id != TE_MAX)) {
601 spin_unlock_bh(&mvm->time_event_lock);
602 return -EIO;
603 }
604 te_data->vif = vif;
605 te_data->duration = le32_to_cpu(te_cmd->duration);
606 te_data->id = le32_to_cpu(te_cmd->id);
607 list_add_tail(&te_data->list, &mvm->time_event_list);
608 spin_unlock_bh(&mvm->time_event_lock);
609
610 /*
611 * Use a notification wait, which really just processes the
612 * command response and doesn't wait for anything, in order
613 * to be able to process the response and get the UID inside
614 * the RX path. Using CMD_WANT_SKB doesn't work because it
615 * stores the buffer and then wakes up this thread, by which
616 * time another notification (that the time event started)
617 * might already be processed unsuccessfully.
618 */
619 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
620 time_event_response,
621 ARRAY_SIZE(time_event_response),
622 iwl_mvm_time_event_response, te_data);
623
624 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
625 sizeof(*te_cmd), te_cmd);
626 if (ret) {
627 IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
628 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
629 goto out_clear_te;
630 }
631
632 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
633 ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
634 /* should never fail */
635 WARN_ON_ONCE(ret);
636
637 if (ret) {
638 out_clear_te:
639 spin_lock_bh(&mvm->time_event_lock);
640 iwl_mvm_te_clear_data(mvm, te_data);
641 spin_unlock_bh(&mvm->time_event_lock);
642 }
643 return ret;
644 }
645
iwl_mvm_protect_session(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,u32 max_delay,bool wait_for_notif)646 void iwl_mvm_protect_session(struct iwl_mvm *mvm,
647 struct ieee80211_vif *vif,
648 u32 duration, u32 min_duration,
649 u32 max_delay, bool wait_for_notif)
650 {
651 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
652 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
653 const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
654 struct iwl_notification_wait wait_te_notif;
655 struct iwl_time_event_cmd time_cmd = {};
656
657 lockdep_assert_held(&mvm->mutex);
658
659 if (te_data->running &&
660 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
661 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
662 jiffies_to_msecs(te_data->end_jiffies - jiffies));
663 return;
664 }
665
666 if (te_data->running) {
667 IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
668 te_data->uid,
669 jiffies_to_msecs(te_data->end_jiffies - jiffies));
670 /*
671 * we don't have enough time
672 * cancel the current TE and issue a new one
673 * Of course it would be better to remove the old one only
674 * when the new one is added, but we don't care if we are off
675 * channel for a bit. All we need to do, is not to return
676 * before we actually begin to be on the channel.
677 */
678 iwl_mvm_stop_session_protection(mvm, vif);
679 }
680
681 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
682 time_cmd.id_and_color =
683 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
684 time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
685
686 time_cmd.apply_time = cpu_to_le32(0);
687
688 time_cmd.max_frags = TE_V2_FRAG_NONE;
689 time_cmd.max_delay = cpu_to_le32(max_delay);
690 /* TODO: why do we need to interval = bi if it is not periodic? */
691 time_cmd.interval = cpu_to_le32(1);
692 time_cmd.duration = cpu_to_le32(duration);
693 time_cmd.repeat = 1;
694 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
695 TE_V2_NOTIF_HOST_EVENT_END |
696 TE_V2_START_IMMEDIATELY);
697
698 if (!wait_for_notif) {
699 iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
700 return;
701 }
702
703 /*
704 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
705 * right after we send the time event
706 */
707 iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
708 te_notif_response,
709 ARRAY_SIZE(te_notif_response),
710 iwl_mvm_te_notif, te_data);
711
712 /* If TE was sent OK - wait for the notification that started */
713 if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
714 IWL_ERR(mvm, "Failed to add TE to protect session\n");
715 iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
716 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
717 TU_TO_JIFFIES(max_delay))) {
718 IWL_ERR(mvm, "Failed to protect session until TE\n");
719 }
720 }
721
722 /* Determine whether mac or link id should be used, and validate the link id */
iwl_mvm_get_session_prot_id(struct iwl_mvm * mvm,struct ieee80211_vif * vif)723 static int iwl_mvm_get_session_prot_id(struct iwl_mvm *mvm,
724 struct ieee80211_vif *vif)
725 {
726 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
727 int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
728 WIDE_ID(MAC_CONF_GROUP,
729 SESSION_PROTECTION_CMD), 1);
730
731 if (ver < 2)
732 return mvmvif->id;
733
734 if (WARN(!mvmvif->deflink.active,
735 "Session Protection on an inactive link\n"))
736 return -EINVAL;
737
738 return mvmvif->deflink.fw_link_id;
739 }
740
iwl_mvm_cancel_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 id)741 static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
742 struct ieee80211_vif *vif,
743 u32 id)
744 {
745 int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif);
746 struct iwl_session_prot_cmd cmd = {
747 .id_and_color = cpu_to_le32(mac_link_id),
748 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
749 .conf_id = cpu_to_le32(id),
750 };
751 int ret;
752
753 if (mac_link_id < 0)
754 return;
755
756 ret = iwl_mvm_send_cmd_pdu(mvm,
757 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
758 0, sizeof(cmd), &cmd);
759 if (ret)
760 IWL_ERR(mvm,
761 "Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
762 }
763
iwl_mvm_roc_rm_cmd(struct iwl_mvm * mvm,u32 activity)764 static void iwl_mvm_roc_rm_cmd(struct iwl_mvm *mvm, u32 activity)
765 {
766 struct iwl_roc_req roc_cmd = {
767 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
768 .activity = cpu_to_le32(activity),
769 };
770 u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
771 u16 cmd_len = ver < 6 ? sizeof(struct iwl_roc_req_v5) : sizeof(roc_cmd);
772 int ret;
773
774 lockdep_assert_held(&mvm->mutex);
775 ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0,
776 cmd_len, &roc_cmd);
777 if (ret)
778 IWL_ERR(mvm, "Couldn't send the ROC_CMD: %d\n", ret);
779 }
780
__iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,u32 * uid)781 static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
782 struct iwl_mvm_time_event_data *te_data,
783 u32 *uid)
784 {
785 u32 id;
786 struct ieee80211_vif *vif = te_data->vif;
787 struct iwl_mvm_vif *mvmvif;
788 enum nl80211_iftype iftype;
789 bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
790 u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
791 WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
792
793 if (!vif)
794 return false;
795
796 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
797 iftype = te_data->vif->type;
798
799 /*
800 * It is possible that by the time we got to this point the time
801 * event was already removed.
802 */
803 spin_lock_bh(&mvm->time_event_lock);
804
805 /* Save time event uid before clearing its data */
806 *uid = te_data->uid;
807 id = te_data->id;
808
809 /*
810 * The clear_data function handles time events that were already removed
811 */
812 iwl_mvm_te_clear_data(mvm, te_data);
813 spin_unlock_bh(&mvm->time_event_lock);
814
815 if ((p2p_aux && iftype == NL80211_IFTYPE_P2P_DEVICE) ||
816 (roc_ver >= 3 && mvmvif->roc_activity == ROC_ACTIVITY_HOTSPOT)) {
817 if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
818 iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
819 mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
820 iwl_mvm_roc_finished(mvm);
821 }
822 return false;
823 } else if (fw_has_capa(&mvm->fw->ucode_capa,
824 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
825 id != HOT_SPOT_CMD) {
826 /* When session protection is used, the te_data->id field
827 * is reused to save session protection's configuration.
828 * For AUX ROC, HOT_SPOT_CMD is used and the te_data->id
829 * field is set to HOT_SPOT_CMD.
830 */
831 if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
832 /* Session protection is still ongoing. Cancel it */
833 iwl_mvm_cancel_session_protection(mvm, vif, id);
834 if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
835 iwl_mvm_roc_finished(mvm);
836 }
837 }
838 return false;
839 } else {
840 /* It is possible that by the time we try to remove it, the
841 * time event has already ended and removed. In such a case
842 * there is no need to send a removal command.
843 */
844 if (id == TE_MAX) {
845 IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
846 return false;
847 }
848 }
849
850 return true;
851 }
852
853 /*
854 * Explicit request to remove a aux roc time event. The removal of a time
855 * event needs to be synchronized with the flow of a time event's end
856 * notification, which also removes the time event from the op mode
857 * data structures.
858 */
iwl_mvm_remove_aux_roc_te(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)859 static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
860 struct iwl_mvm_vif *mvmvif,
861 struct iwl_mvm_time_event_data *te_data)
862 {
863 struct iwl_hs20_roc_req aux_cmd = {};
864 u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
865
866 u32 uid;
867 int ret;
868
869 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
870 return;
871
872 aux_cmd.event_unique_id = cpu_to_le32(uid);
873 aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
874 aux_cmd.id_and_color =
875 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
876 IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
877 le32_to_cpu(aux_cmd.event_unique_id));
878 ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
879 len, &aux_cmd);
880
881 if (WARN_ON(ret))
882 return;
883 }
884
885 /*
886 * Explicit request to remove a time event. The removal of a time event needs to
887 * be synchronized with the flow of a time event's end notification, which also
888 * removes the time event from the op mode data structures.
889 */
iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)890 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
891 struct iwl_mvm_vif *mvmvif,
892 struct iwl_mvm_time_event_data *te_data)
893 {
894 struct iwl_time_event_cmd time_cmd = {};
895 u32 uid;
896 int ret;
897
898 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
899 return;
900
901 /* When we remove a TE, the UID is to be set in the id field */
902 time_cmd.id = cpu_to_le32(uid);
903 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
904 time_cmd.id_and_color =
905 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
906
907 IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
908 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
909 sizeof(time_cmd), &time_cmd);
910 if (ret)
911 IWL_ERR(mvm, "Couldn't remove the time event\n");
912 }
913
iwl_mvm_stop_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif)914 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
915 struct ieee80211_vif *vif)
916 {
917 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
918 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
919 u32 id;
920
921 lockdep_assert_held(&mvm->mutex);
922
923 spin_lock_bh(&mvm->time_event_lock);
924 id = te_data->id;
925 spin_unlock_bh(&mvm->time_event_lock);
926
927 if (fw_has_capa(&mvm->fw->ucode_capa,
928 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
929 if (id != SESSION_PROTECT_CONF_ASSOC) {
930 IWL_DEBUG_TE(mvm,
931 "don't remove session protection id=%u\n",
932 id);
933 return;
934 }
935 } else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
936 IWL_DEBUG_TE(mvm,
937 "don't remove TE with id=%u (not session protection)\n",
938 id);
939 return;
940 }
941
942 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
943 }
944
iwl_mvm_rx_session_protect_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)945 void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
946 struct iwl_rx_cmd_buffer *rxb)
947 {
948 struct iwl_rx_packet *pkt = rxb_addr(rxb);
949 struct iwl_session_prot_notif *notif = (void *)pkt->data;
950 int id = le32_to_cpu(notif->mac_link_id);
951 struct ieee80211_vif *vif;
952 struct iwl_mvm_vif *mvmvif;
953
954 if (IWL_FW_CHECK(mvm, id >= ARRAY_SIZE(mvm->vif_id_to_mac),
955 "Invalid mac_link_id (%d) in session protect notif\n",
956 id))
957 return;
958
959 rcu_read_lock();
960
961 /* note we use link ID == MAC ID */
962 vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
963 if (!vif)
964 goto out_unlock;
965
966 mvmvif = iwl_mvm_vif_from_mac80211(vif);
967
968 /* The vif is not a P2P_DEVICE, maintain its time_event_data */
969 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
970 struct iwl_mvm_time_event_data *te_data =
971 &mvmvif->time_event_data;
972
973 if (!le32_to_cpu(notif->status)) {
974 iwl_mvm_te_check_disconnect(mvm, vif,
975 "Session protection failure");
976 spin_lock_bh(&mvm->time_event_lock);
977 iwl_mvm_te_clear_data(mvm, te_data);
978 spin_unlock_bh(&mvm->time_event_lock);
979 }
980
981 if (le32_to_cpu(notif->start)) {
982 spin_lock_bh(&mvm->time_event_lock);
983 te_data->running = le32_to_cpu(notif->start);
984 te_data->end_jiffies =
985 TU_TO_EXP_TIME(te_data->duration);
986 spin_unlock_bh(&mvm->time_event_lock);
987 } else {
988 /*
989 * By now, we should have finished association
990 * and know the dtim period.
991 */
992 iwl_mvm_te_check_disconnect(mvm, vif,
993 !vif->cfg.assoc ?
994 "Not associated and the session protection is over already..." :
995 "No beacon heard and the session protection is over already...");
996 spin_lock_bh(&mvm->time_event_lock);
997 iwl_mvm_te_clear_data(mvm, te_data);
998 spin_unlock_bh(&mvm->time_event_lock);
999 }
1000
1001 goto out_unlock;
1002 }
1003
1004 if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
1005 /* End TE, notify mac80211 */
1006 mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
1007 /* set the bit so the ROC cleanup will actually clean up */
1008 set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1009 iwl_mvm_roc_finished(mvm);
1010 ieee80211_remain_on_channel_expired(mvm->hw);
1011 } else if (le32_to_cpu(notif->start)) {
1012 if (WARN_ON(mvmvif->time_event_data.id !=
1013 le32_to_cpu(notif->conf_id)))
1014 goto out_unlock;
1015 set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1016 ieee80211_ready_on_channel(mvm->hw); /* Start TE */
1017 }
1018
1019 out_unlock:
1020 rcu_read_unlock();
1021 }
1022
1023 #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
1024 #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
1025 #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
1026 #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
1027 #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
1028
iwl_mvm_roc_duration_and_delay(struct ieee80211_vif * vif,u32 duration_ms,u32 * duration_tu,u32 * delay)1029 void iwl_mvm_roc_duration_and_delay(struct ieee80211_vif *vif,
1030 u32 duration_ms,
1031 u32 *duration_tu,
1032 u32 *delay)
1033 {
1034 struct ieee80211_bss_conf *link_conf;
1035 unsigned int link_id;
1036 u32 dtim_interval = 0;
1037
1038 *delay = AUX_ROC_MIN_DELAY;
1039 *duration_tu = MSEC_TO_TU(duration_ms);
1040
1041 rcu_read_lock();
1042 for_each_vif_active_link(vif, link_conf, link_id) {
1043 dtim_interval =
1044 max_t(u32, dtim_interval,
1045 link_conf->dtim_period * link_conf->beacon_int);
1046 }
1047 rcu_read_unlock();
1048
1049 /*
1050 * If we are associated we want the delay time to be at least one
1051 * dtim interval so that the FW can wait until after the DTIM and
1052 * then start the time event, this will potentially allow us to
1053 * remain off-channel for the max duration.
1054 * Since we want to use almost a whole dtim interval we would also
1055 * like the delay to be for 2-3 dtim intervals, in case there are
1056 * other time events with higher priority.
1057 * dtim_interval should never be 0, it can be 1 if we don't know it
1058 * (we haven't heard any beacon yet).
1059 */
1060 if (vif->cfg.assoc && !WARN_ON(!dtim_interval)) {
1061 *delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
1062 /* We cannot remain off-channel longer than the DTIM interval */
1063 if (dtim_interval <= *duration_tu) {
1064 *duration_tu = dtim_interval - AUX_ROC_SAFETY_BUFFER;
1065 if (*duration_tu <= AUX_ROC_MIN_DURATION)
1066 *duration_tu = dtim_interval -
1067 AUX_ROC_MIN_SAFETY_BUFFER;
1068 }
1069 }
1070 }
1071
iwl_mvm_roc_add_cmd(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration,enum iwl_roc_activity activity)1072 int iwl_mvm_roc_add_cmd(struct iwl_mvm *mvm,
1073 struct ieee80211_channel *channel,
1074 struct ieee80211_vif *vif,
1075 int duration, enum iwl_roc_activity activity)
1076 {
1077 int res;
1078 u32 duration_tu, delay;
1079 struct iwl_roc_req roc_req = {
1080 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1081 .activity = cpu_to_le32(activity),
1082 .sta_id = cpu_to_le32(mvm->aux_sta.sta_id),
1083 };
1084 u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
1085 u16 cmd_len = ver < 6 ? sizeof(struct iwl_roc_req_v5) : sizeof(roc_req);
1086 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1087
1088 lockdep_assert_held(&mvm->mutex);
1089
1090 if (WARN_ON(mvmvif->roc_activity != ROC_NUM_ACTIVITIES))
1091 return -EBUSY;
1092
1093 /* Set the channel info data */
1094 iwl_mvm_set_chan_info(mvm, &roc_req.channel_info,
1095 channel->hw_value,
1096 iwl_mvm_phy_band_from_nl80211(channel->band),
1097 IWL_PHY_CHANNEL_MODE20, 0);
1098
1099 iwl_mvm_roc_duration_and_delay(vif, duration, &duration_tu,
1100 &delay);
1101 roc_req.duration = cpu_to_le32(duration_tu);
1102 roc_req.max_delay = cpu_to_le32(delay);
1103
1104 IWL_DEBUG_TE(mvm,
1105 "\t(requested = %ums, max_delay = %ums)\n",
1106 duration, delay);
1107 IWL_DEBUG_TE(mvm,
1108 "Requesting to remain on channel %u for %utu. activity %u\n",
1109 channel->hw_value, duration_tu, activity);
1110
1111 /* Set the node address */
1112 memcpy(roc_req.node_addr, vif->addr, ETH_ALEN);
1113
1114 res = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD),
1115 0, cmd_len, &roc_req);
1116 if (!res)
1117 mvmvif->roc_activity = activity;
1118
1119 return res;
1120 }
1121
1122 static int
iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)1123 iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
1124 struct ieee80211_vif *vif,
1125 int duration,
1126 enum ieee80211_roc_type type)
1127 {
1128 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1129 struct iwl_session_prot_cmd cmd = {
1130 .id_and_color =
1131 cpu_to_le32(iwl_mvm_get_session_prot_id(mvm, vif)),
1132 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1133 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1134 };
1135
1136 lockdep_assert_held(&mvm->mutex);
1137
1138 /* The time_event_data.id field is reused to save session
1139 * protection's configuration.
1140 */
1141
1142 switch (type) {
1143 case IEEE80211_ROC_TYPE_NORMAL:
1144 mvmvif->time_event_data.id =
1145 SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
1146 break;
1147 case IEEE80211_ROC_TYPE_MGMT_TX:
1148 mvmvif->time_event_data.id =
1149 SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
1150 break;
1151 default:
1152 WARN_ONCE(1, "Got an invalid ROC type\n");
1153 return -EINVAL;
1154 }
1155
1156 cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
1157 return iwl_mvm_send_cmd_pdu(mvm,
1158 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1159 0, sizeof(cmd), &cmd);
1160 }
1161
iwl_mvm_start_p2p_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)1162 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1163 int duration, enum ieee80211_roc_type type)
1164 {
1165 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1166 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1167 struct iwl_time_event_cmd time_cmd = {};
1168
1169 lockdep_assert_held(&mvm->mutex);
1170 if (te_data->running) {
1171 IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
1172 return -EBUSY;
1173 }
1174
1175 if (fw_has_capa(&mvm->fw->ucode_capa,
1176 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
1177 return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
1178 duration,
1179 type);
1180
1181 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1182 time_cmd.id_and_color =
1183 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1184
1185 switch (type) {
1186 case IEEE80211_ROC_TYPE_NORMAL:
1187 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
1188 break;
1189 case IEEE80211_ROC_TYPE_MGMT_TX:
1190 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
1191 break;
1192 default:
1193 WARN_ONCE(1, "Got an invalid ROC type\n");
1194 return -EINVAL;
1195 }
1196
1197 time_cmd.apply_time = cpu_to_le32(0);
1198 time_cmd.interval = cpu_to_le32(1);
1199
1200 /*
1201 * The P2P Device TEs can have lower priority than other events
1202 * that are being scheduled by the driver/fw, and thus it might not be
1203 * scheduled. To improve the chances of it being scheduled, allow them
1204 * to be fragmented, and in addition allow them to be delayed.
1205 */
1206 time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
1207 time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
1208 time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
1209 time_cmd.repeat = 1;
1210 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1211 TE_V2_NOTIF_HOST_EVENT_END |
1212 TE_V2_START_IMMEDIATELY);
1213
1214 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1215 }
1216
iwl_mvm_get_roc_te(struct iwl_mvm * mvm)1217 static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
1218 {
1219 struct iwl_mvm_time_event_data *te_data;
1220
1221 lockdep_assert_held(&mvm->mutex);
1222
1223 spin_lock_bh(&mvm->time_event_lock);
1224
1225 /*
1226 * Iterate over the list of time events and find the time event that is
1227 * associated with a P2P_DEVICE interface.
1228 * This assumes that a P2P_DEVICE interface can have only a single time
1229 * event at any given time and this time event coresponds to a ROC
1230 * request
1231 */
1232 list_for_each_entry(te_data, &mvm->time_event_list, list) {
1233 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1234 goto out;
1235 }
1236
1237 /* There can only be at most one AUX ROC time event, we just use the
1238 * list to simplify/unify code. Remove it if it exists.
1239 */
1240 te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1241 struct iwl_mvm_time_event_data,
1242 list);
1243 out:
1244 spin_unlock_bh(&mvm->time_event_lock);
1245 return te_data;
1246 }
1247
iwl_mvm_cleanup_roc_te(struct iwl_mvm * mvm)1248 void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1249 {
1250 struct iwl_mvm_time_event_data *te_data;
1251 u32 uid;
1252
1253 te_data = iwl_mvm_get_roc_te(mvm);
1254 if (te_data)
1255 __iwl_mvm_remove_time_event(mvm, te_data, &uid);
1256 }
1257
iwl_mvm_stop_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1258 void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1259 {
1260 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1261 struct iwl_mvm_time_event_data *te_data;
1262 bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
1263 u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1264 WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
1265 int iftype = vif->type;
1266
1267 mutex_lock(&mvm->mutex);
1268
1269 if (p2p_aux || (roc_ver >= 3 && iftype != NL80211_IFTYPE_P2P_DEVICE)) {
1270 if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
1271 iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
1272 mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
1273 }
1274 goto cleanup_roc;
1275 } else if (fw_has_capa(&mvm->fw->ucode_capa,
1276 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1277 te_data = &mvmvif->time_event_data;
1278
1279 if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
1280 if (te_data->id >= SESSION_PROTECT_CONF_MAX_ID) {
1281 IWL_DEBUG_TE(mvm,
1282 "No remain on channel event\n");
1283 mutex_unlock(&mvm->mutex);
1284 return;
1285 }
1286 iwl_mvm_cancel_session_protection(mvm, vif,
1287 te_data->id);
1288 } else {
1289 iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1290 &mvmvif->hs_time_event_data);
1291 }
1292 goto cleanup_roc;
1293 }
1294
1295 te_data = iwl_mvm_get_roc_te(mvm);
1296 if (!te_data) {
1297 IWL_WARN(mvm, "No remain on channel event\n");
1298 mutex_unlock(&mvm->mutex);
1299 return;
1300 }
1301
1302 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1303 iftype = te_data->vif->type;
1304 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
1305 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1306 else
1307 iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1308
1309 cleanup_roc:
1310 /*
1311 * In case we get here before the ROC event started,
1312 * (so the status bit isn't set) set it here so iwl_mvm_cleanup_roc will
1313 * cleanup things properly
1314 */
1315 if (p2p_aux || iftype != NL80211_IFTYPE_P2P_DEVICE)
1316 set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
1317 else
1318 set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1319
1320 /* Mutex is released inside this function */
1321 iwl_mvm_cleanup_roc(mvm);
1322 }
1323
iwl_mvm_remove_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1324 void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1325 struct ieee80211_vif *vif)
1326 {
1327 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1328 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1329 u32 id;
1330
1331 lockdep_assert_held(&mvm->mutex);
1332
1333 spin_lock_bh(&mvm->time_event_lock);
1334 id = te_data->id;
1335 spin_unlock_bh(&mvm->time_event_lock);
1336
1337 if (id != TE_CHANNEL_SWITCH_PERIOD)
1338 return;
1339
1340 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1341 }
1342
iwl_mvm_schedule_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 apply_time)1343 int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1344 struct ieee80211_vif *vif,
1345 u32 duration, u32 apply_time)
1346 {
1347 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1348 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1349 struct iwl_time_event_cmd time_cmd = {};
1350
1351 lockdep_assert_held(&mvm->mutex);
1352
1353 if (te_data->running) {
1354 u32 id;
1355
1356 spin_lock_bh(&mvm->time_event_lock);
1357 id = te_data->id;
1358 spin_unlock_bh(&mvm->time_event_lock);
1359
1360 if (id == TE_CHANNEL_SWITCH_PERIOD) {
1361 IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1362 return -EBUSY;
1363 }
1364
1365 /*
1366 * Remove the session protection time event to allow the
1367 * channel switch. If we got here, we just heard a beacon so
1368 * the session protection is not needed anymore anyway.
1369 */
1370 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1371 }
1372
1373 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1374 time_cmd.id_and_color =
1375 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1376 time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1377 time_cmd.apply_time = cpu_to_le32(apply_time);
1378 time_cmd.max_frags = TE_V2_FRAG_NONE;
1379 time_cmd.duration = cpu_to_le32(duration);
1380 time_cmd.repeat = 1;
1381 time_cmd.interval = cpu_to_le32(1);
1382 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1383 TE_V2_ABSENCE);
1384 if (!apply_time)
1385 time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1386
1387 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1388 }
1389
iwl_mvm_session_prot_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)1390 static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1391 struct iwl_rx_packet *pkt, void *data)
1392 {
1393 struct iwl_mvm *mvm =
1394 container_of(notif_wait, struct iwl_mvm, notif_wait);
1395 struct iwl_session_prot_notif *resp;
1396 int resp_len = iwl_rx_packet_payload_len(pkt);
1397
1398 if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1399 pkt->hdr.group_id != MAC_CONF_GROUP))
1400 return true;
1401
1402 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1403 IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1404 return true;
1405 }
1406
1407 resp = (void *)pkt->data;
1408
1409 if (!resp->status)
1410 IWL_ERR(mvm,
1411 "TIME_EVENT_NOTIFICATION received but not executed\n");
1412
1413 return true;
1414 }
1415
iwl_mvm_schedule_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,bool wait_for_notif)1416 void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1417 struct ieee80211_vif *vif,
1418 u32 duration, u32 min_duration,
1419 bool wait_for_notif)
1420 {
1421 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1422 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1423 const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1424 struct iwl_notification_wait wait_notif;
1425 int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif);
1426 struct iwl_session_prot_cmd cmd = {
1427 .id_and_color = cpu_to_le32(mac_link_id),
1428 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1429 .conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1430 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1431 };
1432
1433 if (mac_link_id < 0)
1434 return;
1435
1436 lockdep_assert_held(&mvm->mutex);
1437
1438 spin_lock_bh(&mvm->time_event_lock);
1439 if (te_data->running &&
1440 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1441 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1442 jiffies_to_msecs(te_data->end_jiffies - jiffies));
1443 spin_unlock_bh(&mvm->time_event_lock);
1444
1445 return;
1446 }
1447
1448 iwl_mvm_te_clear_data(mvm, te_data);
1449 /*
1450 * The time_event_data.id field is reused to save session
1451 * protection's configuration.
1452 */
1453 te_data->id = le32_to_cpu(cmd.conf_id);
1454 te_data->duration = le32_to_cpu(cmd.duration_tu);
1455 te_data->vif = vif;
1456 spin_unlock_bh(&mvm->time_event_lock);
1457
1458 IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1459 le32_to_cpu(cmd.duration_tu));
1460
1461 if (!wait_for_notif) {
1462 if (iwl_mvm_send_cmd_pdu(mvm,
1463 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1464 0, sizeof(cmd), &cmd)) {
1465 goto send_cmd_err;
1466 }
1467
1468 return;
1469 }
1470
1471 iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1472 notif, ARRAY_SIZE(notif),
1473 iwl_mvm_session_prot_notif, NULL);
1474
1475 if (iwl_mvm_send_cmd_pdu(mvm,
1476 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1477 0, sizeof(cmd), &cmd)) {
1478 iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1479 goto send_cmd_err;
1480 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1481 TU_TO_JIFFIES(100))) {
1482 IWL_ERR(mvm,
1483 "Failed to protect session until session protection\n");
1484 }
1485 return;
1486
1487 send_cmd_err:
1488 IWL_ERR(mvm,
1489 "Couldn't send the SESSION_PROTECTION_CMD\n");
1490 spin_lock_bh(&mvm->time_event_lock);
1491 iwl_mvm_te_clear_data(mvm, te_data);
1492 spin_unlock_bh(&mvm->time_event_lock);
1493 }
1494