1 /*
2 * Linux roam cache
3 *
4 * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
5 *
6 * Copyright (C) 1999-2017, Broadcom Corporation
7 *
8 * Unless you and Broadcom execute a separate written software license
9 * agreement governing use of this software, this software is licensed to you
10 * under the terms of the GNU General Public License version 2 (the "GPL"),
11 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12 * following added to such license:
13 *
14 * As a special exception, the copyright holders of this software give you
15 * permission to link this software with independent modules, and to copy and
16 * distribute the resulting executable under terms of your choice, provided that
17 * you also meet, for each linked independent module, the terms and conditions of
18 * the license of that module. An independent module is a module which is not
19 * derived from this software. The special exception does not apply to any
20 * modifications of the software.
21 *
22 * Notwithstanding the above, under no circumstances may you combine this
23 * software in any way with any other Broadcom software provided under a license
24 * other than the GPL, without Broadcom's express prior written consent.
25 *
26 *
27 * <<Broadcom-WL-IPTag/Open:>>
28 *
29 * $Id: wl_roam.c 798173 2019-01-07 09:23:21Z $
30 */
31
32 #include <typedefs.h>
33 #include <osl.h>
34 #include <bcmwifi_channels.h>
35 #include <wlioctl.h>
36 #include <bcmutils.h>
37 #ifdef WL_CFG80211
38 #include <wl_cfg80211.h>
39 #endif // endif
40 #include <wldev_common.h>
41 #include <bcmstdlib_s.h>
42
43 #ifdef ESCAN_CHANNEL_CACHE
44 #define MAX_ROAM_CACHE 200
45 #define MAX_SSID_BUFSIZE 36
46
47 #define ROAMSCAN_MODE_NORMAL 0
48 #define ROAMSCAN_MODE_WES 1
49
50 typedef struct {
51 chanspec_t chanspec;
52 int ssid_len;
53 char ssid[MAX_SSID_BUFSIZE];
54 } roam_channel_cache;
55
56 static int n_roam_cache = 0;
57 static int roam_band = WLC_BAND_AUTO;
58 static roam_channel_cache roam_cache[MAX_ROAM_CACHE];
59 static uint band2G, band5G, band_bw;
60
61 #ifdef WES_SUPPORT
62 static int roamscan_mode = ROAMSCAN_MODE_NORMAL;
63 #endif /* WES_SUPPORT */
64
65 #ifdef ROAM_CHANNEL_CACHE
init_roam_cache(struct bcm_cfg80211 * cfg,int ioctl_ver)66 int init_roam_cache(struct bcm_cfg80211 *cfg, int ioctl_ver)
67 {
68 int err;
69 struct net_device *dev = bcmcfg_to_prmry_ndev(cfg);
70 s32 mode;
71
72 /* Check support in firmware */
73 err = wldev_iovar_getint(dev, "roamscan_mode", &mode);
74 if (err && (err == BCME_UNSUPPORTED)) {
75 /* If firmware doesn't support, return error. Else proceed */
76 WL_ERR(("roamscan_mode iovar failed. %d\n", err));
77 return err;
78 }
79
80 #ifdef D11AC_IOTYPES
81 if (ioctl_ver == 1) {
82 /* legacy chanspec */
83 band2G = WL_LCHANSPEC_BAND_2G;
84 band5G = WL_LCHANSPEC_BAND_5G;
85 band_bw = WL_LCHANSPEC_BW_20 | WL_LCHANSPEC_CTL_SB_NONE;
86 } else {
87 band2G = WL_CHANSPEC_BAND_2G;
88 band5G = WL_CHANSPEC_BAND_5G;
89 band_bw = WL_CHANSPEC_BW_20;
90 }
91 #else
92 band2G = WL_CHANSPEC_BAND_2G;
93 band5G = WL_CHANSPEC_BAND_5G;
94 band_bw = WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
95 #endif /* D11AC_IOTYPES */
96
97 n_roam_cache = 0;
98 roam_band = WLC_BAND_AUTO;
99 #ifdef WES_SUPPORT
100 roamscan_mode = ROAMSCAN_MODE_NORMAL;
101 #endif /* WES_SUPPORT */
102
103 return 0;
104 }
105 #endif /* ROAM_CHANNEL_CACHE */
106
107 #ifdef WES_SUPPORT
get_roamscan_mode(struct net_device * dev,int * mode)108 int get_roamscan_mode(struct net_device *dev, int *mode)
109 {
110 *mode = roamscan_mode;
111
112 return 0;
113 }
114
set_roamscan_mode(struct net_device * dev,int mode)115 int set_roamscan_mode(struct net_device *dev, int mode)
116 {
117 int error = 0;
118 roamscan_mode = mode;
119 n_roam_cache = 0;
120
121 error = wldev_iovar_setint(dev, "roamscan_mode", mode);
122 if (error) {
123 WL_ERR(("Failed to set roamscan mode to %d, error = %d\n", mode, error));
124 }
125
126 return error;
127 }
128
get_roamscan_channel_list(struct net_device * dev,unsigned char channels[],int n_channels)129 int get_roamscan_channel_list(struct net_device *dev, unsigned char channels[],
130 int n_channels)
131 {
132 int n = 0;
133 int max_channel_number = MIN(n_channels, n_roam_cache);
134
135 if (roamscan_mode == ROAMSCAN_MODE_WES) {
136 for (n = 0; n < max_channel_number; n++) {
137 channels[n] = roam_cache[n].chanspec & WL_CHANSPEC_CHAN_MASK;
138
139 WL_DBG(("channel[%d] - [%02d] \n", n, channels[n]));
140 }
141 }
142
143 return n;
144 }
145
set_roamscan_channel_list(struct net_device * dev,unsigned char n,unsigned char channels[],int ioctl_ver)146 int set_roamscan_channel_list(struct net_device *dev,
147 unsigned char n, unsigned char channels[], int ioctl_ver)
148 {
149 int i;
150 int error;
151 wl_roam_channel_list_t channel_list;
152 char iobuf[WLC_IOCTL_SMLEN];
153 roamscan_mode = ROAMSCAN_MODE_WES;
154
155 if (n > MAX_ROAM_CHANNEL)
156 n = MAX_ROAM_CHANNEL;
157
158 for (i = 0; i < n; i++) {
159 chanspec_t chanspec;
160
161 if (channels[i] <= CH_MAX_2G_CHANNEL) {
162 chanspec = band2G | band_bw | channels[i];
163 } else {
164 chanspec = band5G | band_bw | channels[i];
165 }
166 roam_cache[i].chanspec = chanspec;
167 channel_list.channels[i] = chanspec;
168
169 WL_DBG(("channel[%d] - [%02d] \n", i, channels[i]));
170 }
171
172 n_roam_cache = n;
173 channel_list.n = n;
174
175 /* need to set ROAMSCAN_MODE_NORMAL to update roamscan_channels,
176 * otherwise, it won't be updated
177 */
178 error = wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_NORMAL);
179 if (error) {
180 WL_ERR(("Failed to set roamscan mode to %d, error = %d\n",
181 ROAMSCAN_MODE_NORMAL, error));
182 return error;
183 }
184 error = wldev_iovar_setbuf(dev, "roamscan_channels", &channel_list,
185 sizeof(channel_list), iobuf, sizeof(iobuf), NULL);
186 if (error) {
187 WL_ERR(("Failed to set roamscan channels, error = %d\n", error));
188 return error;
189 }
190 error = wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_WES);
191 if (error) {
192 WL_ERR(("Failed to set roamscan mode to %d, error = %d\n",
193 ROAMSCAN_MODE_WES, error));
194 }
195
196 return error;
197 }
198 #endif /* WES_SUPPORT */
199
200 #ifdef ESCAN_CHANNEL_CACHE
set_roam_band(int band)201 void set_roam_band(int band)
202 {
203 roam_band = band;
204 }
205
reset_roam_cache(struct bcm_cfg80211 * cfg)206 void reset_roam_cache(struct bcm_cfg80211 *cfg)
207 {
208 if (!cfg->rcc_enabled) {
209 return;
210 }
211
212 #ifdef WES_SUPPORT
213 if (roamscan_mode == ROAMSCAN_MODE_WES)
214 return;
215 #endif /* WES_SUPPORT */
216
217 n_roam_cache = 0;
218 }
219
add_roam_cache(struct bcm_cfg80211 * cfg,wl_bss_info_t * bi)220 void add_roam_cache(struct bcm_cfg80211 *cfg, wl_bss_info_t *bi)
221 {
222 int i;
223 uint8 channel;
224 char chanbuf[CHANSPEC_STR_LEN];
225
226 if (!cfg->rcc_enabled) {
227 return;
228 }
229
230 #ifdef WES_SUPPORT
231 if (roamscan_mode == ROAMSCAN_MODE_WES)
232 return;
233 #endif /* WES_SUPPORT */
234
235 if (n_roam_cache >= MAX_ROAM_CACHE)
236 return;
237
238 for (i = 0; i < n_roam_cache; i++) {
239 if ((roam_cache[i].ssid_len == bi->SSID_len) &&
240 (roam_cache[i].chanspec == bi->chanspec) &&
241 (memcmp(roam_cache[i].ssid, bi->SSID, bi->SSID_len) == 0)) {
242 /* identical one found, just return */
243 return;
244 }
245 }
246
247 roam_cache[n_roam_cache].ssid_len = bi->SSID_len;
248 channel = wf_chspec_ctlchan(bi->chanspec);
249 WL_DBG(("CHSPEC = %s, CTL %d\n", wf_chspec_ntoa_ex(bi->chanspec, chanbuf), channel));
250 roam_cache[n_roam_cache].chanspec =
251 (channel <= CH_MAX_2G_CHANNEL ? band2G : band5G) | band_bw | channel;
252 (void)memcpy_s(roam_cache[n_roam_cache].ssid, bi->SSID_len, bi->SSID, bi->SSID_len);
253
254 n_roam_cache++;
255 }
256
is_duplicated_channel(const chanspec_t * channels,int n_channels,chanspec_t new)257 static bool is_duplicated_channel(const chanspec_t *channels, int n_channels, chanspec_t new)
258 {
259 int i;
260
261 for (i = 0; i < n_channels; i++) {
262 if (channels[i] == new)
263 return TRUE;
264 }
265
266 return FALSE;
267 }
268
get_roam_channel_list(int target_chan,chanspec_t * channels,int n_channels,const wlc_ssid_t * ssid,int ioctl_ver)269 int get_roam_channel_list(int target_chan,
270 chanspec_t *channels, int n_channels, const wlc_ssid_t *ssid, int ioctl_ver)
271 {
272 int i, n = 1;
273 char chanbuf[CHANSPEC_STR_LEN];
274
275 /* first index is filled with the given target channel */
276 if (target_chan) {
277 channels[0] = (target_chan & WL_CHANSPEC_CHAN_MASK) |
278 (target_chan <= CH_MAX_2G_CHANNEL ? band2G : band5G) | band_bw;
279 } else {
280 /* If target channel is not provided, set the index to 0 */
281 n = 0;
282 }
283
284 WL_DBG((" %s: %03d 0x%04X\n", __FUNCTION__, target_chan, channels[0]));
285
286 #ifdef WES_SUPPORT
287 if (roamscan_mode == ROAMSCAN_MODE_WES) {
288 for (i = 0; i < n_roam_cache; i++) {
289 chanspec_t ch = roam_cache[i].chanspec;
290 bool is_2G = ioctl_ver == 1 ? LCHSPEC_IS2G(ch) : CHSPEC_IS2G(ch);
291 bool is_5G = ioctl_ver == 1 ? LCHSPEC_IS5G(ch) : CHSPEC_IS5G(ch);
292 bool band_match = ((roam_band == WLC_BAND_AUTO) ||
293 ((roam_band == WLC_BAND_2G) && is_2G) ||
294 ((roam_band == WLC_BAND_5G) && is_5G));
295
296 ch = CHSPEC_CHANNEL(ch) | (is_2G ? band2G : band5G) | band_bw;
297 if (band_match && !is_duplicated_channel(channels, n, ch)) {
298 WL_DBG(("%s: Chanspec = %s\n", __FUNCTION__,
299 wf_chspec_ntoa_ex(ch, chanbuf)));
300 channels[n++] = ch;
301 if (n >= n_channels) {
302 WL_ERR(("Too many roam scan channels\n"));
303 return n;
304 }
305 }
306 }
307
308 return n;
309 }
310 #endif /* WES_SUPPORT */
311
312 for (i = 0; i < n_roam_cache; i++) {
313 chanspec_t ch = roam_cache[i].chanspec;
314 bool is_2G = ioctl_ver == 1 ? LCHSPEC_IS2G(ch) : CHSPEC_IS2G(ch);
315 bool is_5G = ioctl_ver == 1 ? LCHSPEC_IS5G(ch) : CHSPEC_IS5G(ch);
316 bool band_match = ((roam_band == WLC_BAND_AUTO) ||
317 ((roam_band == WLC_BAND_2G) && is_2G) ||
318 ((roam_band == WLC_BAND_5G) && is_5G));
319
320 ch = CHSPEC_CHANNEL(ch) | (is_2G ? band2G : band5G) | band_bw;
321 if ((roam_cache[i].ssid_len == ssid->SSID_len) &&
322 band_match && !is_duplicated_channel(channels, n, ch) &&
323 (memcmp(roam_cache[i].ssid, ssid->SSID, ssid->SSID_len) == 0)) {
324 /* match found, add it */
325 WL_DBG(("%s: Chanspec = %s\n", __FUNCTION__,
326 wf_chspec_ntoa_ex(ch, chanbuf)));
327 channels[n++] = ch;
328 if (n >= n_channels) {
329 WL_ERR(("Too many roam scan channels\n"));
330 return n;
331 }
332 }
333 }
334
335 return n;
336 }
337 #endif /* ESCAN_CHANNEL_CACHE */
338
339 #ifdef ROAM_CHANNEL_CACHE
print_roam_cache(struct bcm_cfg80211 * cfg)340 void print_roam_cache(struct bcm_cfg80211 *cfg)
341 {
342 int i;
343
344 if (!cfg->rcc_enabled) {
345 return;
346 }
347
348 WL_DBG((" %d cache\n", n_roam_cache));
349
350 for (i = 0; i < n_roam_cache; i++) {
351 roam_cache[i].ssid[roam_cache[i].ssid_len] = 0;
352 WL_DBG(("0x%02X %02d %s\n", roam_cache[i].chanspec,
353 roam_cache[i].ssid_len, roam_cache[i].ssid));
354 }
355 }
356
add_roamcache_channel(wl_roam_channel_list_t * channels,chanspec_t ch)357 static void add_roamcache_channel(wl_roam_channel_list_t *channels, chanspec_t ch)
358 {
359 int i;
360
361 if (channels->n >= MAX_ROAM_CHANNEL) /* buffer full */
362 return;
363
364 for (i = 0; i < channels->n; i++) {
365 if (channels->channels[i] == ch) /* already in the list */
366 return;
367 }
368
369 channels->channels[i] = ch;
370 channels->n++;
371
372 WL_DBG((" RCC: %02d 0x%04X\n",
373 ch & WL_CHANSPEC_CHAN_MASK, ch));
374 }
375
update_roam_cache(struct bcm_cfg80211 * cfg,int ioctl_ver)376 void update_roam_cache(struct bcm_cfg80211 *cfg, int ioctl_ver)
377 {
378 int error, i, prev_channels;
379 wl_roam_channel_list_t channel_list;
380 char iobuf[WLC_IOCTL_SMLEN];
381 struct net_device *dev = bcmcfg_to_prmry_ndev(cfg);
382 wlc_ssid_t ssid;
383
384 if (!cfg->rcc_enabled) {
385 return;
386 }
387
388 #ifdef WES_SUPPORT
389 if (roamscan_mode == ROAMSCAN_MODE_WES) {
390 /* no update when ROAMSCAN_MODE_WES */
391 return;
392 }
393 #endif /* WES_SUPPORT */
394
395 if (!wl_get_drv_status(cfg, CONNECTED, dev)) {
396 WL_DBG(("Not associated\n"));
397 return;
398 }
399
400 /* need to read out the current cache list
401 as the firmware may change dynamically
402 */
403 error = wldev_iovar_getbuf(dev, "roamscan_channels", 0, 0,
404 (void *)&channel_list, sizeof(channel_list), NULL);
405 if (error) {
406 WL_ERR(("Failed to get roamscan channels, error = %d\n", error));
407 return;
408 }
409
410 error = wldev_get_ssid(dev, &ssid);
411 if (error) {
412 WL_ERR(("Failed to get SSID, err=%d\n", error));
413 return;
414 }
415
416 prev_channels = channel_list.n;
417 for (i = 0; i < n_roam_cache; i++) {
418 chanspec_t ch = roam_cache[i].chanspec;
419 bool is_2G = ioctl_ver == 1 ? LCHSPEC_IS2G(ch) : CHSPEC_IS2G(ch);
420 bool is_5G = ioctl_ver == 1 ? LCHSPEC_IS5G(ch) : CHSPEC_IS5G(ch);
421 bool band_match = ((roam_band == WLC_BAND_AUTO) ||
422 ((roam_band == WLC_BAND_2G) && is_2G) ||
423 ((roam_band == WLC_BAND_5G) && is_5G));
424
425 if ((roam_cache[i].ssid_len == ssid.SSID_len) &&
426 band_match && (memcmp(roam_cache[i].ssid, ssid.SSID, ssid.SSID_len) == 0)) {
427 /* match found, add it */
428 ch = CHSPEC_CHANNEL(ch) | (is_2G ? band2G : band5G) | band_bw;
429 add_roamcache_channel(&channel_list, ch);
430 }
431 }
432 if (prev_channels != channel_list.n) {
433 /* channel list updated */
434 error = wldev_iovar_setbuf(dev, "roamscan_channels", &channel_list,
435 sizeof(channel_list), iobuf, sizeof(iobuf), NULL);
436 if (error) {
437 WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
438 }
439 }
440
441 WL_DBG(("%d AP, %d cache item(s), err=%d\n", n_roam_cache, channel_list.n, error));
442 }
443
wl_update_roamscan_cache_by_band(struct net_device * dev,int band)444 void wl_update_roamscan_cache_by_band(struct net_device *dev, int band)
445 {
446 int i, error, ioctl_ver, wes_mode;
447 wl_roam_channel_list_t chanlist_before, chanlist_after;
448 char iobuf[WLC_IOCTL_SMLEN];
449
450 roam_band = band;
451
452 error = wldev_iovar_getint(dev, "roamscan_mode", &wes_mode);
453 if (error) {
454 WL_ERR(("Failed to get roamscan mode, error = %d\n", error));
455 return;
456 }
457
458 ioctl_ver = wl_cfg80211_get_ioctl_version();
459 /* in case of WES mode, update channel list by band based on the cache in DHD */
460 if (wes_mode) {
461 int n = 0;
462 chanlist_before.n = n_roam_cache;
463
464 for (n = 0; n < n_roam_cache; n++) {
465 chanspec_t ch = roam_cache[n].chanspec;
466 bool is_2G = ioctl_ver == 1 ? LCHSPEC_IS2G(ch) : CHSPEC_IS2G(ch);
467 chanlist_before.channels[n] = CHSPEC_CHANNEL(ch) |
468 (is_2G ? band2G : band5G) | band_bw;
469 }
470 } else {
471 if (band == WLC_BAND_AUTO) {
472 return;
473 }
474 error = wldev_iovar_getbuf(dev, "roamscan_channels", 0, 0,
475 (void *)&chanlist_before, sizeof(wl_roam_channel_list_t), NULL);
476 if (error) {
477 WL_ERR(("Failed to get roamscan channels, error = %d\n", error));
478 return;
479 }
480 }
481 chanlist_after.n = 0;
482 /* filtering by the given band */
483 for (i = 0; i < chanlist_before.n; i++) {
484 chanspec_t chspec = chanlist_before.channels[i];
485 bool is_2G = ioctl_ver == 1 ? LCHSPEC_IS2G(chspec) : CHSPEC_IS2G(chspec);
486 bool is_5G = ioctl_ver == 1 ? LCHSPEC_IS5G(chspec) : CHSPEC_IS5G(chspec);
487 bool band_match = ((band == WLC_BAND_AUTO) ||
488 ((band == WLC_BAND_2G) && is_2G) ||
489 ((band == WLC_BAND_5G) && is_5G));
490 if (band_match) {
491 chanlist_after.channels[chanlist_after.n++] = chspec;
492 }
493 }
494
495 if (wes_mode) {
496 /* need to set ROAMSCAN_MODE_NORMAL to update roamscan_channels,
497 * otherwise, it won't be updated
498 */
499 wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_NORMAL);
500
501 error = wldev_iovar_setbuf(dev, "roamscan_channels", &chanlist_after,
502 sizeof(wl_roam_channel_list_t), iobuf, sizeof(iobuf), NULL);
503 if (error) {
504 WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
505 }
506 wldev_iovar_setint(dev, "roamscan_mode", ROAMSCAN_MODE_WES);
507 } else {
508 if (chanlist_before.n == chanlist_after.n) {
509 return;
510 }
511 error = wldev_iovar_setbuf(dev, "roamscan_channels", &chanlist_after,
512 sizeof(wl_roam_channel_list_t), iobuf, sizeof(iobuf), NULL);
513 if (error) {
514 WL_ERR(("Failed to update roamscan channels, error = %d\n", error));
515 }
516 }
517 }
518 #endif /* ROAM_CHANNEL_CACHE */
519 #endif /* ESCAN_CHANNEL_CACHE */
520