Repository navigation
Expand file tree
/
Copy pathchannel.c
More file actions
523 lines (419 loc) · 12 KB
/
Copy pathchannel.c
File metadata and controls
523 lines (419 loc) · 12 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// SPDX-License-Identifier: BSD-3-Clause-Clear
/*
* Copyright (C) 2024 Felix Fietkau <nbd@nbd.name>
*/
#include "mt76.h"
static struct mt76_vif_link *
mt76_alloc_mlink(struct mt76_dev *dev, struct mt76_vif_data *mvif)
{
struct mt76_vif_link *mlink;
mlink = kzalloc(dev->drv->link_data_size, GFP_KERNEL);
if (!mlink)
return NULL;
mlink->mvif = mvif;
return mlink;
}
static int
mt76_phy_update_channel(struct mt76_phy *phy,
struct ieee80211_chanctx_conf *conf)
{
phy->radar_enabled = conf->radar_enabled;
phy->main_chandef = conf->def;
phy->chanctx = (struct mt76_chanctx *)conf->drv_priv;
return __mt76_set_channel(phy, &phy->main_chandef, false);
}
static void mt76_phy_mac_work_restart(struct mt76_phy *phy)
{
struct mt76_dev *dev = phy->dev;
if (test_bit(MT76_STATE_RUNNING, &phy->state) &&
!test_bit(MT76_MCU_RESET, &dev->phy.state) &&
!test_bit(MT76_RESTART, &dev->phy.state))
ieee80211_queue_delayed_work(phy->hw, &phy->mac_work, 0);
}
int mt76_add_chanctx(struct ieee80211_hw *hw,
struct ieee80211_chanctx_conf *conf)
{
struct mt76_chanctx *ctx = (struct mt76_chanctx *)conf->drv_priv;
struct mt76_phy *phy = hw->priv;
struct mt76_dev *dev = phy->dev;
int ret = -EINVAL;
phy = ctx->phy = dev->band_phys[conf->def.chan->band];
if (WARN_ON_ONCE(!phy))
return ret;
if (mt76_phy_scanning(phy))
mt76_abort_scan(dev);
mutex_lock(&dev->mutex);
if (!phy->chanctx)
ret = mt76_phy_update_channel(phy, conf);
else
ret = 0;
mutex_unlock(&dev->mutex);
return ret;
}
EXPORT_SYMBOL_GPL(mt76_add_chanctx);
void mt76_remove_chanctx(struct ieee80211_hw *hw,
struct ieee80211_chanctx_conf *conf)
{
struct mt76_chanctx *ctx = (struct mt76_chanctx *)conf->drv_priv;
struct mt76_phy *phy = hw->priv;
struct mt76_dev *dev = phy->dev;
phy = ctx->phy;
if (WARN_ON_ONCE(!phy))
return;
if (mt76_phy_scanning(phy))
mt76_abort_scan(dev);
mutex_lock(&dev->mutex);
if (phy->chanctx == ctx)
phy->chanctx = NULL;
mutex_unlock(&dev->mutex);
}
EXPORT_SYMBOL_GPL(mt76_remove_chanctx);
void mt76_change_chanctx(struct ieee80211_hw *hw,
struct ieee80211_chanctx_conf *conf,
u32 changed)
{
struct mt76_chanctx *ctx = (struct mt76_chanctx *)conf->drv_priv;
struct mt76_phy *phy = ctx->phy;
struct mt76_dev *dev = phy->dev;
if (!(changed & (IEEE80211_CHANCTX_CHANGE_WIDTH |
IEEE80211_CHANCTX_CHANGE_RADAR |
IEEE80211_CHANCTX_CHANGE_PUNCTURING)))
return;
if (mt76_phy_scanning(phy))
mt76_abort_scan(dev);
if (phy->roc_vif)
mt76_abort_roc(phy);
cancel_delayed_work_sync(&phy->mac_work);
mutex_lock(&dev->mutex);
mt76_phy_update_channel(phy, conf);
mutex_unlock(&dev->mutex);
}
EXPORT_SYMBOL_GPL(mt76_change_chanctx);
int mt76_assign_vif_chanctx(struct ieee80211_hw *hw,
struct ieee80211_vif *vif,
struct ieee80211_bss_conf *link_conf,
struct ieee80211_chanctx_conf *conf)
{
struct mt76_chanctx *ctx = (struct mt76_chanctx *)conf->drv_priv;
struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
struct mt76_vif_data *mvif = mlink->mvif;
int link_id = link_conf->link_id;
struct mt76_phy *phy = ctx->phy;
struct mt76_dev *dev = phy->dev;
bool mlink_alloc = false;
int ret = 0;
if (dev->scan.vif == vif && mt76_phy_scanning(phy))
mt76_abort_scan(dev);
mutex_lock(&dev->mutex);
if (vif->type == NL80211_IFTYPE_MONITOR &&
is_zero_ether_addr(vif->addr))
goto out;
mlink = mt76_vif_conf_link(dev, vif, link_conf);
if (!mlink) {
mlink = mt76_alloc_mlink(dev, mvif);
if (!mlink) {
ret = -ENOMEM;
goto out;
}
mlink_alloc = true;
}
mt76_vif_link_ctx_set(mlink, conf);
ret = dev->drv->vif_link_add(phy, vif, link_conf, mlink);
if (ret) {
if (mlink_alloc)
kfree(mlink);
goto out;
}
if (link_conf != &vif->bss_conf)
rcu_assign_pointer(mvif->link[link_id], mlink);
out:
mutex_unlock(&dev->mutex);
return ret;
}
EXPORT_SYMBOL_GPL(mt76_assign_vif_chanctx);
void mt76_unassign_vif_chanctx(struct ieee80211_hw *hw,
struct ieee80211_vif *vif,
struct ieee80211_bss_conf *link_conf,
struct ieee80211_chanctx_conf *conf)
{
struct mt76_chanctx *ctx = (struct mt76_chanctx *)conf->drv_priv;
struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
struct mt76_phy *phy = ctx->phy;
struct mt76_dev *dev = phy->dev;
if (dev->scan.vif == vif && mt76_phy_scanning(phy))
mt76_abort_scan(dev);
mutex_lock(&dev->mutex);
if (vif->type == NL80211_IFTYPE_MONITOR &&
is_zero_ether_addr(vif->addr))
goto out;
mlink = mt76_vif_conf_link(dev, vif, link_conf);
if (!mlink)
goto out;
dev->drv->vif_link_remove(phy, vif, link_conf, mlink);
mt76_vif_link_ctx_set(mlink, NULL);
out:
mutex_unlock(&dev->mutex);
}
EXPORT_SYMBOL_GPL(mt76_unassign_vif_chanctx);
int mt76_switch_vif_chanctx(struct ieee80211_hw *hw,
struct ieee80211_vif_chanctx_switch *vifs,
int n_vifs,
enum ieee80211_chanctx_switch_mode mode)
{
struct ieee80211_vif_chanctx_switch *v;
struct mt76_chanctx *old_ctx, *new_ctx;
struct mt76_phy *old_phy, *phy = hw->priv;
struct mt76_dev *dev = phy->dev;
struct mt76_vif_link *mlink;
bool need_update[__MT_MAX_BAND] = {};
int i, ret = 0;
for (i = 0; i < n_vifs; i++) {
v = &vifs[i];
new_ctx = (struct mt76_chanctx *)v->new_ctx->drv_priv;
if (mode == CHANCTX_SWMODE_SWAP_CONTEXTS)
phy = new_ctx->phy = dev->band_phys[v->new_ctx->def.chan->band];
else
phy = new_ctx->phy;
if (!phy) {
ret = -EINVAL;
goto restart_mac_work;
}
if (need_update[phy->band_idx])
continue;
if (phy->chanctx != new_ctx) {
if (mt76_phy_scanning(phy))
mt76_abort_scan(dev);
cancel_delayed_work_sync(&phy->mac_work);
need_update[phy->band_idx] = true;
}
}
mutex_lock(&dev->mutex);
for (i = 0; i < n_vifs; i++) {
v = &vifs[i];
old_ctx = (struct mt76_chanctx *)v->old_ctx->drv_priv;
old_phy = old_ctx->phy;
new_ctx = (struct mt76_chanctx *)v->new_ctx->drv_priv;
phy = new_ctx->phy;
if (mode == CHANCTX_SWMODE_SWAP_CONTEXTS && old_phy->chanctx &&
old_phy->chanctx == old_ctx && phy != old_phy)
old_phy->chanctx = NULL;
if (need_update[phy->band_idx]) {
ret = mt76_phy_update_channel(phy, v->new_ctx);
if (ret)
goto out;
need_update[phy->band_idx] = false;
}
mlink = mt76_vif_conf_link(dev, v->vif, v->link_conf);
if (!mlink)
continue;
if (old_phy != phy) {
dev->drv->vif_link_remove(old_phy, v->vif, v->link_conf,
mlink);
ret = dev->drv->vif_link_add(phy, v->vif, v->link_conf,
mlink);
if (ret)
goto out;
}
mt76_vif_link_ctx_set(mlink, v->new_ctx);
}
out:
mutex_unlock(&dev->mutex);
restart_mac_work:
for (i = 0; i < ARRAY_SIZE(need_update); i++)
if (need_update[i] && dev->phys[i])
mt76_phy_mac_work_restart(dev->phys[i]);
return ret;
}
EXPORT_SYMBOL_GPL(mt76_switch_vif_chanctx);
struct mt76_vif_link *mt76_get_vif_phy_link(struct mt76_phy *phy,
struct ieee80211_vif *vif)
{
struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
struct mt76_vif_data *mvif = mlink->mvif;
struct mt76_dev *dev = phy->dev;
int i, ret;
for (i = 0; i < ARRAY_SIZE(mvif->link); i++) {
mlink = mt76_dereference(mvif->link[i], dev);
if (!mlink)
continue;
if (mt76_vif_link_phy(mlink) == phy)
return mlink;
}
if (!dev->drv->vif_link_add)
return ERR_PTR(-EINVAL);
mlink = mt76_alloc_mlink(dev, mvif);
if (!mlink)
return ERR_PTR(-ENOMEM);
mlink->offchannel = true;
ret = dev->drv->vif_link_add(phy, vif, &vif->bss_conf, mlink);
if (ret) {
kfree(mlink);
return ERR_PTR(ret);
}
return mlink;
}
void mt76_put_vif_phy_link(struct mt76_phy *phy, struct ieee80211_vif *vif,
struct mt76_vif_link *mlink)
{
struct mt76_dev *dev = phy->dev;
if (IS_ERR_OR_NULL(mlink) || !mlink->offchannel)
return;
dev->drv->vif_link_remove(phy, vif, &vif->bss_conf, mlink);
kfree_rcu(mlink, rcu_head);
}
struct mt76_csa_active_data {
struct mt76_phy *phy;
bool active;
};
static void
mt76_csa_active_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
{
struct mt76_csa_active_data *data = priv;
struct ieee80211_bss_conf *link_conf;
struct mt76_vif_link *mlink;
unsigned int link_id;
switch (vif->type) {
case NL80211_IFTYPE_AP:
case NL80211_IFTYPE_MESH_POINT:
case NL80211_IFTYPE_ADHOC:
break;
default:
return;
}
for_each_vif_active_link(vif, link_conf, link_id) {
if (!link_conf->csa_active)
continue;
mlink = mt76_vif_link(data->phy->dev, vif, link_id);
if (mlink && mt76_vif_link_phy(mlink) == data->phy)
data->active = true;
}
}
bool mt76_phy_csa_active(struct mt76_phy *phy)
{
struct mt76_csa_active_data data = {
.phy = phy,
};
lockdep_assert_held(&phy->dev->mutex);
ieee80211_iterate_active_interfaces_mtx(phy->hw,
IEEE80211_IFACE_ITER_NORMAL,
mt76_csa_active_iter, &data);
return data.active;
}
static struct mt76_phy *
mt76_vif_roc_phy(struct mt76_dev *dev, struct ieee80211_vif *vif)
{
int i;
for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
struct mt76_phy *phy = dev->phys[i];
if (phy && phy->roc_vif == vif)
return phy;
}
return NULL;
}
void mt76_roc_complete(struct mt76_phy *phy)
{
struct mt76_vif_link *mlink = phy->roc_link;
struct mt76_dev *dev = phy->dev;
if (!phy->roc_vif)
return;
if (mlink && mlink->mvif->roc_phy == phy)
mlink->mvif->roc_phy = NULL;
if (phy->chanctx && phy->main_chandef.chan && phy->offchannel &&
!test_bit(MT76_MCU_RESET, &dev->phy.state)) {
__mt76_set_channel(phy, &phy->main_chandef, false);
mt76_offchannel_notify(phy, false);
} else {
phy->offchannel = false;
}
mt76_put_vif_phy_link(phy, phy->roc_vif, phy->roc_link);
phy->roc_vif = NULL;
phy->roc_link = NULL;
if (!test_bit(MT76_RESTART, &dev->phy.state))
ieee80211_remain_on_channel_expired(phy->hw);
}
void mt76_roc_complete_work(struct work_struct *work)
{
struct mt76_phy *phy = container_of(work, struct mt76_phy, roc_work.work);
struct mt76_dev *dev = phy->dev;
mutex_lock(&dev->mutex);
mt76_roc_complete(phy);
mutex_unlock(&dev->mutex);
}
void mt76_abort_roc(struct mt76_phy *phy)
{
struct mt76_dev *dev = phy->dev;
cancel_delayed_work_sync(&phy->roc_work);
mutex_lock(&dev->mutex);
mt76_roc_complete(phy);
mutex_unlock(&dev->mutex);
}
EXPORT_SYMBOL_GPL(mt76_abort_roc);
int mt76_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
struct ieee80211_channel *chan, int duration,
enum ieee80211_roc_type type)
{
struct cfg80211_chan_def chandef = {};
struct mt76_phy *phy = hw->priv;
struct mt76_dev *dev = phy->dev;
struct mt76_vif_link *mlink;
bool offchannel;
int ret = 0;
phy = dev->band_phys[chan->band];
if (!phy)
return -EINVAL;
cancel_delayed_work_sync(&phy->mac_work);
mutex_lock(&dev->mutex);
if (test_bit(MT76_MCU_RESET, &dev->phy.state) ||
test_bit(MT76_RESTART, &dev->phy.state)) {
ret = -EBUSY;
goto out;
}
if (phy->roc_vif || mt76_vif_roc_phy(dev, vif) ||
mt76_phy_scanning(phy) || mt76_phy_csa_active(phy)) {
ret = -EBUSY;
goto restart_mac_work;
}
mlink = mt76_get_vif_phy_link(phy, vif);
if (IS_ERR(mlink)) {
ret = PTR_ERR(mlink);
goto restart_mac_work;
}
mlink->mvif->roc_phy = phy;
phy->roc_vif = vif;
phy->roc_link = mlink;
offchannel = mt76_offchannel_chandef(phy, chan, &chandef);
if (offchannel)
mt76_offchannel_notify(phy, true);
ret = __mt76_set_channel(phy, &chandef, offchannel);
if (ret) {
mlink->mvif->roc_phy = NULL;
phy->roc_vif = NULL;
phy->roc_link = NULL;
mt76_put_vif_phy_link(phy, vif, mlink);
goto out;
}
ieee80211_ready_on_channel(hw);
ieee80211_queue_delayed_work(phy->hw, &phy->roc_work,
msecs_to_jiffies(duration));
goto out;
restart_mac_work:
mt76_phy_mac_work_restart(phy);
out:
mutex_unlock(&dev->mutex);
return ret;
}
EXPORT_SYMBOL_GPL(mt76_remain_on_channel);
int mt76_cancel_remain_on_channel(struct ieee80211_hw *hw,
struct ieee80211_vif *vif)
{
struct mt76_phy *phy = hw->priv;
struct mt76_dev *dev = phy->dev;
/* a completion holds the mutex until it reported the expiry */
mutex_lock(&dev->mutex);
phy = mt76_vif_roc_phy(dev, vif);
mutex_unlock(&dev->mutex);
if (phy)
mt76_abort_roc(phy);
return 0;
}
EXPORT_SYMBOL_GPL(mt76_cancel_remain_on_channel);