summaryrefslogtreecommitdiff
path: root/wl_roam.c
blob: a53697d7d3d996662d795627f769d2db34aacf0f (plain)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * Linux roam cache
 *
 * Copyright (C) 2022, Broadcom.
 *
 *      Unless you and Broadcom execute a separate written software license
 * agreement governing use of this software, this software is licensed to you
 * under the terms of the GNU General Public License version 2 (the "GPL"),
 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
 * following added to such license:
 *
 *      As a special exception, the copyright holders of this software give you
 * permission to link this software with independent modules, and to copy and
 * distribute the resulting executable under terms of your choice, provided that
 * you also meet, for each linked independent module, the terms and conditions of
 * the license of that module.  An independent module is a module which is not
 * derived from this software.  The special exception does not apply to any
 * modifications of the software.
 *
 *
 * <<Broadcom-WL-IPTag/Dual:>>
 */

#include <typedefs.h>
#include <osl.h>
#include <bcmwifi_channels.h>
#include <wlioctl.h>
#include <bcmutils.h>
#ifdef WL_CFG80211
#include <wl_cfg80211.h>
#endif
#include <wldev_common.h>
#if defined(__linux__)
#include <bcmstdlib_s.h>
#endif /* defined(__linux__) */

#ifdef ESCAN_CHANNEL_CACHE
#define MAX_ROAM_CACHE		200
#define MAX_SSID_BUFSIZE	36

typedef struct {
	chanspec_t chanspec;
	int ssid_len;
	char ssid[MAX_SSID_BUFSIZE];
} roam_channel_cache;

static int n_roam_cache = 0;
static int roam_band = WLC_BAND_AUTO;
static roam_channel_cache roam_cache[MAX_ROAM_CACHE];
static uint band_bw;

static void add_roamcache_channel(wl_roam_channel_list_t *channels, chanspec_t ch)
{
	int i;

	if (channels->n >= MAX_ROAM_CHANNEL) /* buffer full */
		return;

	for (i = 0; i < channels->n; i++) {
		if (channels->channels[i] == ch) /* already in the list */
			return;
	}

	channels->channels[i] = ch;
	channels->n++;

	WL_DBG((" RCC: %02d 0x%04X\n",
		ch & WL_CHANSPEC_CHAN_MASK, ch));
}

void update_roam_cache(struct bcm_cfg80211 *cfg, int ioctl_ver)
{
	int error, i, prev_channels;
	wl_roam_channel_list_t channel_list;
	char iobuf[WLC_IOCTL_SMLEN];
	struct net_device *dev = bcmcfg_to_prmry_ndev(cfg);
	wlc_ssid_t ssid;
#ifdef WL_DUAL_STA
	struct net_info *iter, *next;
#endif /* WL_DUAL_STA */

	if (!cfg->rcc_enabled) {
		return;
	}

#ifdef WES_SUPPORT
	if (cfg->roamscan_mode == ROAMSCAN_MODE_WES) {
		/* no update when ROAMSCAN_MODE_WES */
		return;
	}
#endif /* WES_SUPPORT */

#ifdef WL_DUAL_STA
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	for_each_ndev(cfg, iter, next) {
		GCC_DIAGNOSTIC_POP();
		if ((iter->wdev) && (iter->iftype == WL_IF_TYPE_STA)) {
			struct net_device *ndev = iter->wdev->netdev;
			if (IS_INET_LINK_NDEV(cfg, ndev)) {
				/* Update the net device with primary interface */
				dev = ndev;
				WL_DBG(("ndev considered for RCC %s\n", dev->name));
				break;
			}
		}
	}
#endif /* WL_DUAL_STA */

	if (!wl_get_drv_status(cfg, CONNECTED, dev)) {
		WL_DBG(("Not associated\n"));
		return;
	}

	/* need to read out the current cache list
	   as the firmware may change dynamically
	*/
	error = wldev_iovar_getbuf(dev, "roamscan_channels", 0, 0,
		(void *)&channel_list, sizeof(channel_list), NULL);
	if (error) {
		WL_ERR(("Failed to get roamscan channels, error = %d\n", error));
		return;
	}

	error = wldev_get_ssid(dev, &ssid);
	if (error) {
		WL_ERR(("Failed to get SSID, err=%d\n", error));
		return;
	}

	prev_channels = channel_list.n;
	for (i = 0; i < n_roam_cache; i++) {
		chanspec_t ch = roam_cache[i].chanspec;
		bool band_match = ((roam_band == WLC_BAND_AUTO) ||
#ifdef WL_6G_BAND
			((roam_band == WLC_BAND_6G) && (CHSPEC_IS6G(ch))) ||
#endif /* WL_6G_BAND */
			((roam_band == WLC_BAND_2G) && (CHSPEC_IS2G(ch))) ||
			((roam_band == WLC_BAND_5G) && (CHSPEC_IS5G(ch))));

		if ((roam_cache[i].ssid_len == ssid.SSID_len) &&
			band_match && (memcmp(roam_cache[i].ssid, ssid.SSID, ssid.SSID_len) == 0)) {
			/* match found, add it */
			ch = wf_chspec_ctlchan(ch) | CHSPEC_BAND(ch) | band_bw;
			add_roamcache_channel(&channel_list, ch);
		}
	}
	if (prev_channels != channel_list.n) {
		/* channel list updated */
		error = wldev_iovar_setbuf(dev, "roamscan_channels", &channel_list,
			sizeof(channel_list), iobuf, sizeof(iobuf), NULL);
		if (error) {
			WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
		}
	}

	WL_DBG(("%d AP, %d cache item(s), err=%d\n", n_roam_cache, channel_list.n, error));
}

void set_roam_band(int band)
{
	roam_band = band;
}

void reset_roam_cache(struct bcm_cfg80211 *cfg)
{
	if (!cfg->rcc_enabled) {
		return;
	}

#ifdef WES_SUPPORT
	if (cfg->roamscan_mode == ROAMSCAN_MODE_WES)
		return;
#endif /* WES_SUPPORT */

	n_roam_cache = 0;
}

static void
add_roam_cache_list(uint8 *SSID, uint32 SSID_len, chanspec_t chanspec)
{
	int i;
	uint8 channel;
	char chanbuf[CHANSPEC_STR_LEN];

	if (n_roam_cache >= MAX_ROAM_CACHE) {
		return;
	}

	for (i = 0; i < n_roam_cache; i++) {
		if ((roam_cache[i].ssid_len == SSID_len) &&
			(roam_cache[i].chanspec == chanspec) &&
			(memcmp(roam_cache[i].ssid, SSID, SSID_len) == 0)) {
			/* identical one found, just return */
			return;
		}
	}

	roam_cache[n_roam_cache].ssid_len = SSID_len;
	channel = wf_chspec_ctlchan(chanspec);
	WL_DBG(("CHSPEC  = %s, CTL %d SSID %s\n",
		wf_chspec_ntoa_ex(chanspec, chanbuf), channel, SSID));
	roam_cache[n_roam_cache].chanspec = CHSPEC_BAND(chanspec) | band_bw | channel;
	(void)memcpy_s(roam_cache[n_roam_cache].ssid, SSID_len, SSID, SSID_len);

	n_roam_cache++;
}

void
add_roam_cache(struct bcm_cfg80211 *cfg, wl_bss_info_v109_t *bi)
{
	if (!cfg->rcc_enabled) {
		return;
	}

#ifdef WES_SUPPORT
	if (cfg->roamscan_mode == ROAMSCAN_MODE_WES) {
		return;
	}
#endif /* WES_SUPPORT */

	add_roam_cache_list(bi->SSID, bi->SSID_len, bi->chanspec);
}

static bool is_duplicated_channel(const chanspec_t *channels, int n_channels, chanspec_t new)
{
	int i;

	for (i = 0; i < n_channels; i++) {
		if (channels[i] == new)
			return TRUE;
	}

	return FALSE;
}

int get_roam_channel_list(struct bcm_cfg80211 *cfg, chanspec_t target_chan,
	chanspec_t *channels, int n_channels, const wlc_ssid_t *ssid, int ioctl_ver)
{
	int i, n = 0;
	char chanbuf[CHANSPEC_STR_LEN];

	/* first index is filled with the given target channel */
	if ((target_chan != INVCHANSPEC) && (target_chan != 0)) {
		channels[0] = target_chan;
		n++;
	}

	WL_DBG((" %s: 0x%04X\n", __FUNCTION__, channels[0]));

#ifdef WES_SUPPORT
	if (cfg->roamscan_mode == ROAMSCAN_MODE_WES) {
		for (i = 0; i < n_roam_cache; i++) {
			chanspec_t ch = roam_cache[i].chanspec;
			bool band_match = ((roam_band == WLC_BAND_AUTO) ||
#ifdef WL_6G_BAND
				((roam_band == WLC_BAND_6G) && (CHSPEC_IS6G(ch))) ||
#endif /* WL_6G_BAND */
				((roam_band == WLC_BAND_2G) && (CHSPEC_IS2G(ch))) ||
				((roam_band == WLC_BAND_5G) && (CHSPEC_IS5G(ch))));

			ch = wf_chspec_ctlchan(ch) | CHSPEC_BAND(ch) | band_bw;

			if (band_match && !is_duplicated_channel(channels, n, ch)) {
				WL_DBG(("%s: Chanspec = %s\n", __FUNCTION__,
					wf_chspec_ntoa_ex(ch, chanbuf)));
				channels[n++] = ch;
				if (n >= n_channels) {
					WL_ERR(("Too many roam scan channels\n"));
					return n;
				}
			}
		}

		return n;
	}
#endif /* WES_SUPPORT */

	for (i = 0; i < n_roam_cache; i++) {
		chanspec_t ch = roam_cache[i].chanspec;
		bool band_match = ((roam_band == WLC_BAND_AUTO) ||
#ifdef WL_6G_BAND
			((roam_band == WLC_BAND_6G) && (CHSPEC_IS6G(ch))) ||
#endif /* WL_6G_BAND */
			((roam_band == WLC_BAND_2G) && (CHSPEC_IS2G(ch))) ||
			((roam_band == WLC_BAND_5G) && (CHSPEC_IS5G(ch))));

		ch = wf_chspec_ctlchan(ch) | CHSPEC_BAND(ch) | band_bw;
		if ((roam_cache[i].ssid_len == ssid->SSID_len) &&
			band_match && !is_duplicated_channel(channels, n, ch) &&
			(memcmp(roam_cache[i].ssid, ssid->SSID, ssid->SSID_len) == 0)) {
			/* match found, add it */
			WL_DBG(("%s: Chanspec = %s\n", __FUNCTION__,
				wf_chspec_ntoa_ex(ch, chanbuf)));
			channels[n++] = ch;
			if (n >= n_channels) {
				WL_ERR(("Too many roam scan channels\n"));
				return n;
			}
		}
	}

	return n;
}

#ifdef WES_SUPPORT
int get_roamscan_mode(struct net_device *dev, int *mode)
{
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	*mode = cfg->roamscan_mode;

	return 0;
}

int set_roamscan_mode(struct net_device *dev, int mode)
{
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	int error = 0;
	cfg->roamscan_mode = mode;
	n_roam_cache = 0;

	error = wldev_iovar_setint(dev, "roamscan_mode", mode);
	if (error) {
		WL_ERR(("Failed to set roamscan mode to %d, error = %d\n", mode, error));
	}

	return error;
}

int
get_roamscan_chanspec_list(struct net_device *dev, chanspec_t *chanspecs)
{
	int i = 0;
	int error = BCME_OK;
	wl_roam_channel_list_t channel_list;

	/* Get Current RCC List */
	error = wldev_iovar_getbuf(dev, "roamscan_channels", 0, 0,
		(void *)&channel_list, sizeof(channel_list), NULL);
	if (error) {
		WL_ERR(("Failed to get roamscan channels, err = %d\n", error));
		return error;
	}
	if (channel_list.n > MAX_ROAM_CHANNEL) {
		WL_ERR(("Invalid roamscan channels count(%d)\n", channel_list.n));
		return BCME_ERROR;
	}

	for (i = 0; i < channel_list.n; i++) {
		chanspecs[i] = channel_list.channels[i];
		WL_DBG(("%02d: chanspec %04x\n", i, chanspecs[i]));
	}

	return i;
}

bool
check_prune_roam_band(uint8 allowed_band, chanspec_t chanspec)
{
	int ret = FALSE;

	if ((allowed_band == WLC_ROAM_ALLOW_BAND_AUTO) ||
		(allowed_band == WLC_ROAM_ALLOW_BAND_MAX)) {
		return ret;
	}

	/* Pruned BSS via ROAM Band mode */
	if ((CHSPEC_IS2G(chanspec) && !(allowed_band & WLC_ROAM_ALLOW_BAND_2G)))  {
		ret = TRUE;
	} else if (CHSPEC_IS5G(chanspec) && !(allowed_band & WLC_ROAM_ALLOW_BAND_5G)) {
		ret = TRUE;
#ifdef WL_6G_BAND
	} else if (CHSPEC_IS6G(chanspec) && !(allowed_band & WLC_ROAM_ALLOW_BAND_6G)) {
		ret = TRUE;
#endif /* WL_6G_BAND */
	}

	return ret;
}

int
set_roamscan_chanspec_list(struct net_device *dev, uint nchan, chanspec_t *chanspecs)
{
	int i, j;
	int error;
	wl_roam_channel_list_t channel_list;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	char iobuf[WLC_IOCTL_SMLEN];

	if (nchan > MAX_ROAM_CHANNEL) {
		nchan = MAX_ROAM_CHANNEL;
	}

	for (i = 0, j = 0; i < nchan; i++) {
		if (!wf_chspec_valid(chanspecs[i])) {
			WL_ERR(("%02d/%d: invalid chan: 0x%04x\n", i, nchan, chanspecs[i]));
			continue;
		}
		if (check_prune_roam_band(cfg->roam_allowed_band, chanspecs[i])) {
			WL_ERR(("%02d/%d: Pruned ROAM band(%d) 0x%04x\n", i, nchan,
				cfg->roam_allowed_band, chanspecs[i]));
			continue;
		}
		channel_list.channels[j] = roam_cache[j].chanspec = chanspecs[i];
		WL_DBG(("%02d/%d: chan: 0x%04x\n", i, nchan, chanspecs[i]));
		j++;
	}

	channel_list.n = n_roam_cache = j;

	/* need to set ROAMSCAN_MODE_NORMAL to update roamscan_channels,
	 * otherwise, it won't be updated
	 */
	error = wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_NORMAL);
	if (error) {
		WL_ERR(("Failed to set roamscan mode to %d, error = %d\n",
			ROAMSCAN_MODE_NORMAL, error));
		return error;
	}
	error = wldev_iovar_setbuf(dev, "roamscan_channels", &channel_list,
		sizeof(channel_list), iobuf, sizeof(iobuf), NULL);
	if (error) {
		WL_ERR(("Failed to set roamscan channels, error = %d\n", error));
		return error;
	}

	return error;
}

int
add_roamscan_chanspec_list(struct net_device *dev, uint nchan, chanspec_t *chanspecs)
{
	int i, error = BCME_OK;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	wlc_ssid_t ssid;

	if (!cfg->rcc_enabled) {
		return BCME_ERROR;
	}

	if (cfg->roamscan_mode == ROAMSCAN_MODE_WES) {
		WL_ERR(("Failed to add roamscan channels, WES mode %d\n",
			cfg->roamscan_mode));
		return BCME_ERROR;
	}

	if (nchan > MAX_ROAM_CHANNEL) {
		WL_ERR(("Failed Over MAX channel list(%d)\n", nchan));
		return BCME_BADARG;
	}

	error = wldev_get_ssid(dev, &ssid);
	if (error) {
		WL_ERR(("Failed to get SSID, err=%d\n", error));
		return error;
	}

	WL_DBG(("Add Roam scan channel count %d\n", nchan));

	for (i = 0; i < nchan; i++) {
		if (!wf_chspec_valid(chanspecs[i])) {
			continue;
		}
		if (check_prune_roam_band(cfg->roam_allowed_band, chanspecs[i])) {
			WL_ERR(("%02d/%d: Pruned ROAM band(%d) 0x%04x\n", i, nchan,
				cfg->roam_allowed_band, chanspecs[i]));
			continue;
		}
		add_roam_cache_list(ssid.SSID, ssid.SSID_len, chanspecs[i]);
		WL_DBG(("channel[%d] - 0x%04x SSID %s\n", i, chanspecs[i], ssid.SSID));
	}

	update_roam_cache(cfg, ioctl_version);

	return error;
}
#endif /* WES_SUPPORT */

#ifdef ROAM_CHANNEL_CACHE
int init_roam_cache(struct bcm_cfg80211 *cfg, int ioctl_ver)
{
	int err;
	struct net_device *dev = bcmcfg_to_prmry_ndev(cfg);
	s32 mode;

	/* Check support in firmware */
	err = wldev_iovar_getint(dev, "roamscan_mode", &mode);
	if (err && (err == BCME_UNSUPPORTED)) {
		/* If firmware doesn't support, return error. Else proceed */
		WL_ERR(("roamscan_mode iovar failed. %d\n", err));
		return err;
	}

#ifdef D11AC_IOTYPES
	band_bw = WL_CHANSPEC_BW_20;
#else
	band_bw = WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
#endif /* D11AC_IOTYPES */

	n_roam_cache = 0;
	roam_band = WLC_BAND_AUTO;
	cfg->roamscan_mode = ROAMSCAN_MODE_NORMAL;

	return 0;
}

void print_roam_cache(struct bcm_cfg80211 *cfg)
{
	int i;

	if (!cfg->rcc_enabled) {
		return;
	}

	WL_DBG((" %d cache\n", n_roam_cache));

	for (i = 0; i < n_roam_cache; i++) {
		roam_cache[i].ssid[roam_cache[i].ssid_len] = 0;
		WL_DBG(("0x%02X %02d %s\n", roam_cache[i].chanspec,
			roam_cache[i].ssid_len, roam_cache[i].ssid));
	}
}

void wl_update_roamscan_cache_by_band(struct net_device *dev, int band)
{
	int i, error, roamscan_mode;
	wl_roam_channel_list_t chanlist_before, chanlist_after;
	char iobuf[WLC_IOCTL_SMLEN];

	roam_band = band;

	error = wldev_iovar_getint(dev, "roamscan_mode", &roamscan_mode);
	if (error) {
		WL_ERR(("Failed to get roamscan mode, error = %d\n", error));
		return;
	}

	/* in case of WES mode, update channel list by band based on the cache in DHD */
	if (roamscan_mode) {
		int n = 0;
		chanlist_before.n = n_roam_cache;

		for (n = 0; n < n_roam_cache; n++) {
			chanspec_t ch = roam_cache[n].chanspec;
			chanlist_before.channels[n] = wf_chspec_ctlchan(ch) |
				CHSPEC_BAND(ch) | band_bw;
		}
	} else {
		if (band == WLC_BAND_AUTO) {
			return;
		}
		error = wldev_iovar_getbuf(dev, "roamscan_channels", 0, 0,
				(void *)&chanlist_before, sizeof(wl_roam_channel_list_t), NULL);
		if (error) {
			WL_ERR(("Failed to get roamscan channels, error = %d\n", error));
			return;
		}
	}
	chanlist_after.n = 0;
	/* filtering by the given band */
	for (i = 0; i < chanlist_before.n; i++) {
		chanspec_t chspec = chanlist_before.channels[i];
		bool band_match = ((band == WLC_BAND_AUTO) ||
#ifdef WL_6G_BAND
				((band == WLC_BAND_6G) && (CHSPEC_IS6G(chspec))) ||
#endif /* WL_6G_BAND */
				((band == WLC_BAND_2G) && (CHSPEC_IS2G(chspec))) ||
				((band == WLC_BAND_5G) && (CHSPEC_IS5G(chspec))));
		if (band_match) {
			chanlist_after.channels[chanlist_after.n++] = chspec;
		}
	}

	if (roamscan_mode) {
		/* need to set ROAMSCAN_MODE_NORMAL to update roamscan_channels,
		 * otherwise, it won't be updated
		 */
		wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_NORMAL);

		error = wldev_iovar_setbuf(dev, "roamscan_channels", &chanlist_after,
				sizeof(wl_roam_channel_list_t), iobuf, sizeof(iobuf), NULL);
		if (error) {
			WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
		}
		wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_WES);
	} else {
		if (chanlist_before.n == chanlist_after.n) {
			return;
		}
		error = wldev_iovar_setbuf(dev, "roamscan_channels", &chanlist_after,
				sizeof(wl_roam_channel_list_t), iobuf, sizeof(iobuf), NULL);
		if (error) {
			WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
		}
	}
}
#endif /* ROAM_CHANNEL_CACHE */
#endif /* ESCAN_CHANNEL_CACHE */