1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64#include <linux/jiffies.h>
65#include <net/mac80211.h>
66
67#include "iwl-notif-wait.h"
68#include "iwl-trans.h"
69#include "fw-api.h"
70#include "time-event.h"
71#include "mvm.h"
72#include "iwl-io.h"
73#include "iwl-prph.h"
74
75
76#define TU_TO_JIFFIES(_tu) (usecs_to_jiffies((_tu) * 1024))
77#define MSEC_TO_TU(_msec) (_msec*1000/1024)
78
79
80
81
82
83#define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
84#define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
85
86void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
87 struct iwl_mvm_time_event_data *te_data)
88{
89 lockdep_assert_held(&mvm->time_event_lock);
90
91 if (te_data->id == TE_MAX)
92 return;
93
94 list_del(&te_data->list);
95 te_data->running = false;
96 te_data->uid = 0;
97 te_data->id = TE_MAX;
98 te_data->vif = NULL;
99}
100
101void iwl_mvm_roc_done_wk(struct work_struct *wk)
102{
103 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
104
105 synchronize_net();
106
107
108
109
110
111
112
113
114
115
116
117 iwl_mvm_flush_tx_path(mvm, BIT(IWL_MVM_OFFCHANNEL_QUEUE), false);
118}
119
120static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
121{
122
123
124
125
126
127
128
129 clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
130
131
132
133
134
135
136
137
138 schedule_work(&mvm->roc_done_wk);
139}
140
141
142
143
144
145
146
147
148static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
149 struct iwl_mvm_time_event_data *te_data,
150 struct iwl_time_event_notif *notif)
151{
152 lockdep_assert_held(&mvm->time_event_lock);
153
154 IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
155 le32_to_cpu(notif->unique_id),
156 le32_to_cpu(notif->action));
157
158
159
160
161
162
163
164
165
166 WARN_ONCE(!le32_to_cpu(notif->status),
167 "Failed to schedule time event\n");
168
169 if (le32_to_cpu(notif->action) & TE_NOTIF_HOST_EVENT_END) {
170 IWL_DEBUG_TE(mvm,
171 "TE ended - current time %lu, estimated end %lu\n",
172 jiffies, te_data->end_jiffies);
173
174 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
175 ieee80211_remain_on_channel_expired(mvm->hw);
176 iwl_mvm_roc_finished(mvm);
177 }
178
179
180
181
182
183 if (te_data->vif->type == NL80211_IFTYPE_STATION &&
184 (!te_data->vif->bss_conf.assoc ||
185 !te_data->vif->bss_conf.dtim_period)) {
186 IWL_ERR(mvm,
187 "No assocation and the time event is over already...\n");
188 ieee80211_connection_loss(te_data->vif);
189 }
190
191 iwl_mvm_te_clear_data(mvm, te_data);
192 } else if (le32_to_cpu(notif->action) & TE_NOTIF_HOST_EVENT_START) {
193 te_data->running = true;
194 te_data->end_jiffies = jiffies +
195 TU_TO_JIFFIES(te_data->duration);
196
197 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
198 set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
199 ieee80211_ready_on_channel(mvm->hw);
200 }
201 } else {
202 IWL_WARN(mvm, "Got TE with unknown action\n");
203 }
204}
205
206
207
208
209int iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
210 struct iwl_rx_cmd_buffer *rxb,
211 struct iwl_device_cmd *cmd)
212{
213 struct iwl_rx_packet *pkt = rxb_addr(rxb);
214 struct iwl_time_event_notif *notif = (void *)pkt->data;
215 struct iwl_mvm_time_event_data *te_data, *tmp;
216
217 IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
218 le32_to_cpu(notif->unique_id),
219 le32_to_cpu(notif->action));
220
221 spin_lock_bh(&mvm->time_event_lock);
222 list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
223 if (le32_to_cpu(notif->unique_id) == te_data->uid)
224 iwl_mvm_te_handle_notif(mvm, te_data, notif);
225 }
226 spin_unlock_bh(&mvm->time_event_lock);
227
228 return 0;
229}
230
231static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
232 struct iwl_rx_packet *pkt, void *data)
233{
234 struct iwl_mvm *mvm =
235 container_of(notif_wait, struct iwl_mvm, notif_wait);
236 struct iwl_mvm_time_event_data *te_data = data;
237 struct iwl_time_event_resp *resp;
238 int resp_len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
239
240 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
241 return true;
242
243 if (WARN_ON_ONCE(resp_len != sizeof(pkt->hdr) + sizeof(*resp))) {
244 IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
245 return true;
246 }
247
248 resp = (void *)pkt->data;
249
250
251 if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
252 return false;
253
254 te_data->uid = le32_to_cpu(resp->unique_id);
255 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
256 te_data->uid);
257 return true;
258}
259
260static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
261 struct ieee80211_vif *vif,
262 struct iwl_mvm_time_event_data *te_data,
263 struct iwl_time_event_cmd *te_cmd)
264{
265 static const u8 time_event_response[] = { TIME_EVENT_CMD };
266 struct iwl_notification_wait wait_time_event;
267 int ret;
268
269 lockdep_assert_held(&mvm->mutex);
270
271 IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
272 le32_to_cpu(te_cmd->duration));
273
274 spin_lock_bh(&mvm->time_event_lock);
275 if (WARN_ON(te_data->id != TE_MAX)) {
276 spin_unlock_bh(&mvm->time_event_lock);
277 return -EIO;
278 }
279 te_data->vif = vif;
280 te_data->duration = le32_to_cpu(te_cmd->duration);
281 te_data->id = le32_to_cpu(te_cmd->id);
282 list_add_tail(&te_data->list, &mvm->time_event_list);
283 spin_unlock_bh(&mvm->time_event_lock);
284
285
286
287
288
289
290
291
292
293
294 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
295 time_event_response,
296 ARRAY_SIZE(time_event_response),
297 iwl_mvm_time_event_response, te_data);
298
299 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, CMD_SYNC,
300 sizeof(*te_cmd), te_cmd);
301 if (ret) {
302 IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
303 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
304 goto out_clear_te;
305 }
306
307
308 ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
309
310 WARN_ON_ONCE(ret);
311
312 if (ret) {
313 out_clear_te:
314 spin_lock_bh(&mvm->time_event_lock);
315 iwl_mvm_te_clear_data(mvm, te_data);
316 spin_unlock_bh(&mvm->time_event_lock);
317 }
318 return ret;
319}
320
321void iwl_mvm_protect_session(struct iwl_mvm *mvm,
322 struct ieee80211_vif *vif,
323 u32 duration, u32 min_duration)
324{
325 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
326 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
327 struct iwl_time_event_cmd time_cmd = {};
328
329 lockdep_assert_held(&mvm->mutex);
330
331 if (te_data->running &&
332 time_after(te_data->end_jiffies,
333 jiffies + TU_TO_JIFFIES(min_duration))) {
334 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
335 jiffies_to_msecs(te_data->end_jiffies - jiffies));
336 return;
337 }
338
339 if (te_data->running) {
340 IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
341 te_data->uid,
342 jiffies_to_msecs(te_data->end_jiffies - jiffies));
343
344
345
346
347
348
349
350
351 iwl_mvm_stop_session_protection(mvm, vif);
352 }
353
354 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
355 time_cmd.id_and_color =
356 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
357 time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
358
359 time_cmd.apply_time =
360 cpu_to_le32(iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG));
361
362 time_cmd.dep_policy = TE_INDEPENDENT;
363 time_cmd.is_present = cpu_to_le32(1);
364 time_cmd.max_frags = cpu_to_le32(TE_FRAG_NONE);
365 time_cmd.max_delay = cpu_to_le32(500);
366
367 time_cmd.interval = cpu_to_le32(1);
368 time_cmd.interval_reciprocal = cpu_to_le32(iwl_mvm_reciprocal(1));
369 time_cmd.duration = cpu_to_le32(duration);
370 time_cmd.repeat = cpu_to_le32(1);
371 time_cmd.notify = cpu_to_le32(TE_NOTIF_HOST_EVENT_START |
372 TE_NOTIF_HOST_EVENT_END);
373
374 iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
375}
376
377
378
379
380
381
382void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
383 struct iwl_mvm_vif *mvmvif,
384 struct iwl_mvm_time_event_data *te_data)
385{
386 struct iwl_time_event_cmd time_cmd = {};
387 u32 id, uid;
388 int ret;
389
390
391
392
393
394 spin_lock_bh(&mvm->time_event_lock);
395
396
397 uid = te_data->uid;
398 id = te_data->id;
399
400
401
402
403 iwl_mvm_te_clear_data(mvm, te_data);
404 spin_unlock_bh(&mvm->time_event_lock);
405
406
407
408
409
410
411 if (id == TE_MAX) {
412 IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", uid);
413 return;
414 }
415
416
417 time_cmd.id = cpu_to_le32(uid);
418 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
419 time_cmd.id_and_color =
420 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
421
422 IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
423 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, CMD_SYNC,
424 sizeof(time_cmd), &time_cmd);
425 if (WARN_ON(ret))
426 return;
427}
428
429void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
430 struct ieee80211_vif *vif)
431{
432 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
433 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
434
435 lockdep_assert_held(&mvm->mutex);
436 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
437}
438
439int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
440 int duration, enum ieee80211_roc_type type)
441{
442 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
443 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
444 struct iwl_time_event_cmd time_cmd = {};
445
446 lockdep_assert_held(&mvm->mutex);
447 if (te_data->running) {
448 IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
449 return -EBUSY;
450 }
451
452
453
454
455
456 flush_work(&mvm->roc_done_wk);
457
458 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
459 time_cmd.id_and_color =
460 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
461
462 switch (type) {
463 case IEEE80211_ROC_TYPE_NORMAL:
464 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
465 break;
466 case IEEE80211_ROC_TYPE_MGMT_TX:
467 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
468 break;
469 default:
470 WARN_ONCE(1, "Got an invalid ROC type\n");
471 return -EINVAL;
472 }
473
474 time_cmd.apply_time = cpu_to_le32(0);
475 time_cmd.dep_policy = cpu_to_le32(TE_INDEPENDENT);
476 time_cmd.is_present = cpu_to_le32(1);
477 time_cmd.interval = cpu_to_le32(1);
478
479
480
481
482
483
484
485 time_cmd.max_frags = cpu_to_le32(MSEC_TO_TU(duration)/20);
486 time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
487 time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
488 time_cmd.repeat = cpu_to_le32(1);
489 time_cmd.notify = cpu_to_le32(TE_NOTIF_HOST_EVENT_START |
490 TE_NOTIF_HOST_EVENT_END);
491
492 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
493}
494
495void iwl_mvm_stop_p2p_roc(struct iwl_mvm *mvm)
496{
497 struct iwl_mvm_vif *mvmvif;
498 struct iwl_mvm_time_event_data *te_data;
499
500 lockdep_assert_held(&mvm->mutex);
501
502
503
504
505
506
507
508
509 mvmvif = NULL;
510 spin_lock_bh(&mvm->time_event_lock);
511 list_for_each_entry(te_data, &mvm->time_event_list, list) {
512 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
513 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
514 break;
515 }
516 }
517 spin_unlock_bh(&mvm->time_event_lock);
518
519 if (!mvmvif) {
520 IWL_WARN(mvm, "P2P_DEVICE no remain on channel event\n");
521 return;
522 }
523
524 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
525
526 iwl_mvm_roc_finished(mvm);
527}
528