1 /* 2 * Copyright (c) 2016-2021 The Linux Foundation. All rights reserved. 3 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /** 29 * DOC: This file has the functions related to DFS CAC. 30 */ 31 32 #include "../dfs_channel.h" 33 #include "../dfs_zero_cac.h" 34 #include <wlan_objmgr_vdev_obj.h> 35 #include "wlan_dfs_utils_api.h" 36 #include "wlan_dfs_mlme_api.h" 37 #include "../dfs_internal.h" 38 #include "../dfs_process_radar_found_ind.h" 39 40 #define IS_CHANNEL_WEATHER_RADAR(freq) ((freq >= 5600) && (freq <= 5650)) 41 #define ADJACENT_WEATHER_RADAR_CHANNEL 5580 42 #define CH100_START_FREQ 5490 43 #define CH100 100 44 45 /** 46 * dfs_cac_valid_timeout() - Timeout function for dfs_cac_valid_timer 47 * cac_valid bit will be reset in this function. 48 */ 49 static os_timer_func(dfs_cac_valid_timeout) 50 { 51 struct wlan_dfs *dfs = NULL; 52 53 OS_GET_TIMER_ARG(dfs, struct wlan_dfs *); 54 dfs->dfs_cac_valid = 0; 55 dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, ": Timed out!!"); 56 } 57 58 /** 59 * dfs_clear_cac_started_chan() - Clear dfs cac started channel. 60 * @dfs: Pointer to wlan_dfs structure. 61 */ 62 void dfs_clear_cac_started_chan(struct wlan_dfs *dfs) 63 { 64 qdf_mem_zero(&dfs->dfs_cac_started_chan, 65 sizeof(dfs->dfs_cac_started_chan)); 66 } 67 68 static void dfs_clear_nol_history_for_curchan(struct wlan_dfs *dfs) 69 { 70 struct dfs_channel *chan = dfs->dfs_curchan; 71 uint16_t sub_channels[MAX_20MHZ_SUBCHANS]; 72 uint8_t num_subchs; 73 74 num_subchs = dfs_get_bonding_channel_without_seg_info_for_freq( 75 chan, sub_channels); 76 77 if (dfs->dfs_is_stadfs_enabled) 78 if (dfs_mlme_is_opmode_sta(dfs->dfs_pdev_obj)) 79 utils_dfs_reg_update_nol_history_chan_for_freq( 80 dfs->dfs_pdev_obj, sub_channels, 81 num_subchs, DFS_NOL_HISTORY_RESET); 82 } 83 84 void dfs_process_cac_completion(void *context) 85 { 86 struct wlan_dfs *dfs = (struct wlan_dfs *)context; 87 enum phy_ch_width ch_width = CH_WIDTH_INVALID; 88 uint16_t primary_chan_freq = 0, sec_chan_freq = 0; 89 struct dfs_channel *dfs_curchan; 90 91 dfs->dfs_cac_timer_running = 0; 92 dfs_curchan = dfs->dfs_curchan; 93 94 dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "cac expired, chan %d cur time %d", 95 dfs->dfs_curchan->dfs_ch_freq, 96 (qdf_system_ticks_to_msecs(qdf_system_ticks()) / 1000)); 97 98 /* 99 * When radar is detected during a CAC we are woken up prematurely to 100 * switch to a new channel. Check the channel to decide how to act. 101 */ 102 if (WLAN_IS_CHAN_RADAR(dfs, dfs->dfs_curchan)) { 103 dfs_mlme_mark_dfs(dfs->dfs_pdev_obj, 104 dfs_curchan->dfs_ch_ieee, 105 dfs_curchan->dfs_ch_freq, 106 dfs_curchan->dfs_ch_mhz_freq_seg2, 107 dfs_curchan->dfs_ch_flags); 108 dfs_debug(dfs, WLAN_DEBUG_DFS, 109 "CAC timer on chan %u (%u MHz) stopped due to radar", 110 dfs_curchan->dfs_ch_ieee, 111 dfs_curchan->dfs_ch_freq); 112 } else { 113 dfs_debug(dfs, WLAN_DEBUG_DFS, 114 "CAC timer on channel %u (%u MHz) expired;" 115 "no radar detected", 116 dfs_curchan->dfs_ch_ieee, 117 dfs_curchan->dfs_ch_freq); 118 119 /* On CAC completion, set the bit 'cac_valid'. 120 * CAC will not be re-done if this bit is reset. 121 * The flag will be reset when dfs_cac_valid_timer 122 * timesout. 123 */ 124 if (dfs->dfs_cac_valid_time) { 125 dfs->dfs_cac_valid = 1; 126 qdf_timer_mod(&dfs->dfs_cac_valid_timer, 127 dfs->dfs_cac_valid_time * 1000); 128 } 129 130 dfs_find_curchwidth_and_center_chan_for_freq(dfs, 131 &ch_width, 132 &primary_chan_freq, 133 &sec_chan_freq); 134 135 /* ETSI allows the driver to cache the CAC ( Once CAC done, 136 * it can be used in future). 137 * Therefore mark the current channel CAC done. 138 */ 139 if (utils_get_dfsdomain(dfs->dfs_pdev_obj) == DFS_ETSI_DOMAIN) 140 dfs_mark_precac_done_for_freq(dfs, 141 primary_chan_freq, 142 sec_chan_freq, 143 ch_width); 144 } 145 146 dfs_clear_cac_started_chan(dfs); 147 148 /* Clear NOL history for current channel on successful CAC completion */ 149 dfs_clear_nol_history_for_curchan(dfs); 150 /* Iterate over the nodes, processing the CAC completion event. */ 151 dfs_mlme_proc_cac(dfs->dfs_pdev_obj, 0); 152 153 /* Send a CAC timeout, VAP up event to user space */ 154 dfs_mlme_deliver_event_up_after_cac(dfs->dfs_pdev_obj); 155 156 if (dfs->dfs_defer_precac_channel_change == 1) { 157 dfs_mlme_channel_change_by_precac(dfs->dfs_pdev_obj); 158 dfs->dfs_defer_precac_channel_change = 0; 159 } 160 } 161 162 /** 163 * dfs_cac_timeout() - DFS cactimeout function. 164 * 165 * Sets dfs_cac_timer_running to 0 and dfs_cac_valid_timer. 166 */ 167 #ifdef CONFIG_CHAN_FREQ_API 168 static enum qdf_hrtimer_restart_status 169 dfs_cac_timeout(qdf_hrtimer_data_t *arg) 170 { 171 struct wlan_dfs *dfs; 172 173 dfs = container_of(arg, struct wlan_dfs, dfs_cac_timer); 174 175 if (dfs_is_hw_mode_switch_in_progress(dfs)) 176 dfs->dfs_defer_params.is_cac_completed = true; 177 else 178 qdf_sched_work(NULL, &dfs->dfs_cac_completion_work); 179 180 return QDF_HRTIMER_NORESTART; 181 } 182 #endif 183 184 #ifdef QCA_SUPPORT_DFS_CAC 185 void dfs_cac_timer_attach(struct wlan_dfs *dfs) 186 { 187 dfs->dfs_cac_timeout_override = -1; 188 dfs->wlan_dfs_cac_time = WLAN_DFS_WAIT_MS; 189 qdf_hrtimer_init(&dfs->dfs_cac_timer, 190 dfs_cac_timeout, 191 QDF_CLOCK_MONOTONIC, 192 QDF_HRTIMER_MODE_REL, 193 QDF_CONTEXT_HARDWARE); 194 qdf_create_work(NULL, 195 &dfs->dfs_cac_completion_work, 196 dfs_process_cac_completion, 197 dfs); 198 qdf_timer_init(NULL, 199 &(dfs->dfs_cac_valid_timer), 200 dfs_cac_valid_timeout, 201 (void *)(dfs), 202 QDF_TIMER_TYPE_WAKE_APPS); 203 } 204 205 void dfs_cac_timer_reset(struct wlan_dfs *dfs) 206 { 207 qdf_hrtimer_cancel(&dfs->dfs_cac_timer); 208 qdf_flush_work(&dfs->dfs_cac_completion_work); 209 dfs_get_override_cac_timeout(dfs, 210 &(dfs->dfs_cac_timeout_override)); 211 dfs_clear_cac_started_chan(dfs); 212 } 213 214 void dfs_cac_timer_detach(struct wlan_dfs *dfs) 215 { 216 qdf_hrtimer_kill(&dfs->dfs_cac_timer); 217 qdf_flush_work(&dfs->dfs_cac_completion_work); 218 qdf_destroy_work(NULL, &dfs->dfs_cac_completion_work); 219 qdf_timer_free(&dfs->dfs_cac_valid_timer); 220 dfs->dfs_cac_valid = 0; 221 } 222 223 int dfs_is_ap_cac_timer_running(struct wlan_dfs *dfs) 224 { 225 return dfs->dfs_cac_timer_running; 226 } 227 228 #ifdef CONFIG_CHAN_FREQ_API 229 void dfs_start_cac_timer(struct wlan_dfs *dfs) 230 { 231 int cac_timeout = 0; 232 struct dfs_channel *chan = dfs->dfs_curchan; 233 234 cac_timeout = 235 dfs_mlme_get_cac_timeout_for_freq(dfs->dfs_pdev_obj, 236 chan->dfs_ch_freq, 237 chan->dfs_ch_mhz_freq_seg2, 238 chan->dfs_ch_flags); 239 240 dfs->dfs_cac_started_chan = *chan; 241 242 dfs_deliver_cac_state_events(dfs); 243 dfs_debug(dfs, WLAN_DEBUG_DFS, 244 "chan = %d cfreq2 = %d timeout = %d sec, curr_time = %d sec", 245 chan->dfs_ch_ieee, chan->dfs_ch_vhtop_ch_freq_seg2, 246 cac_timeout, 247 qdf_system_ticks_to_msecs(qdf_system_ticks()) / 1000); 248 249 qdf_hrtimer_start(&dfs->dfs_cac_timer, 250 qdf_time_ms_to_ktime(cac_timeout * 1000), 251 QDF_HRTIMER_MODE_REL); 252 dfs->dfs_cac_aborted = 0; 253 } 254 #endif 255 256 void dfs_cancel_cac_timer(struct wlan_dfs *dfs) 257 { 258 qdf_hrtimer_cancel(&dfs->dfs_cac_timer); 259 dfs_clear_cac_started_chan(dfs); 260 } 261 262 void dfs_send_dfs_events_for_chan(struct wlan_dfs *dfs, 263 struct dfs_channel *chan, 264 enum WLAN_DFS_EVENTS event) 265 { 266 uint8_t nchannels, i; 267 qdf_freq_t freq_list[MAX_20MHZ_SUBCHANS]; 268 269 nchannels = 270 dfs_get_bonding_channel_without_seg_info_for_freq(chan, 271 freq_list); 272 for (i = 0; i < nchannels; i++) 273 utils_dfs_deliver_event(dfs->dfs_pdev_obj, 274 freq_list[i], 275 event); 276 } 277 278 void dfs_cac_stop(struct wlan_dfs *dfs) 279 { 280 uint32_t phyerr; 281 struct dfs_channel *chan; 282 283 chan = &dfs->dfs_cac_started_chan; 284 dfs_get_debug_info(dfs, (void *)&phyerr); 285 dfs_debug(dfs, WLAN_DEBUG_DFS, 286 "Stopping CAC Timer %d procphyerr 0x%08x", 287 dfs->dfs_curchan->dfs_ch_freq, phyerr); 288 qdf_hrtimer_cancel(&dfs->dfs_cac_timer); 289 290 dfs_send_dfs_events_for_chan(dfs, chan, WLAN_EV_CAC_RESET); 291 292 if (dfs->dfs_cac_timer_running) 293 dfs->dfs_cac_aborted = 1; 294 dfs_clear_cac_started_chan(dfs); 295 dfs->dfs_cac_timer_running = 0; 296 } 297 298 void dfs_stacac_stop(struct wlan_dfs *dfs) 299 { 300 uint32_t phyerr; 301 302 dfs_get_debug_info(dfs, (void *)&phyerr); 303 dfs_debug(dfs, WLAN_DEBUG_DFS, 304 "Stopping STA CAC Timer %d procphyerr 0x%08x", 305 dfs->dfs_curchan->dfs_ch_freq, phyerr); 306 dfs_clear_cac_started_chan(dfs); 307 } 308 309 int dfs_override_cac_timeout(struct wlan_dfs *dfs, int cac_timeout) 310 { 311 if (!dfs) 312 return -EIO; 313 314 dfs->dfs_cac_timeout_override = cac_timeout; 315 dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "CAC timeout is now %s %d", 316 (cac_timeout == -1) ? "default" : "overridden", 317 cac_timeout); 318 319 return 0; 320 } 321 322 int dfs_get_override_cac_timeout(struct wlan_dfs *dfs, int *cac_timeout) 323 { 324 if (!dfs) 325 return -EIO; 326 327 (*cac_timeout) = dfs->dfs_cac_timeout_override; 328 329 return 0; 330 } 331 332 #ifdef CONFIG_CHAN_FREQ_API 333 void dfs_cac_valid_reset_for_freq(struct wlan_dfs *dfs, 334 uint16_t prevchan_freq, 335 uint32_t prevchan_flags) 336 { 337 if (dfs->dfs_cac_valid_time) { 338 if ((prevchan_freq != dfs->dfs_curchan->dfs_ch_freq) || 339 (prevchan_flags != dfs->dfs_curchan->dfs_ch_flags)) { 340 dfs_err(dfs, WLAN_DEBUG_DFS_ALWAYS, 341 "Cancelling timer & clearing cac_valid"); 342 qdf_timer_stop(&dfs->dfs_cac_valid_timer); 343 dfs->dfs_cac_valid = 0; 344 } 345 } 346 } 347 #endif 348 #endif 349 350 #ifdef CONFIG_CHAN_FREQ_API 351 bool 352 dfs_is_subset_channel_for_freq(uint16_t *old_subchans_freq, 353 uint8_t old_n_chans, 354 uint16_t *new_subchans_freq, 355 uint8_t new_n_chans) 356 { 357 bool is_found; 358 int i, j; 359 360 if (!new_n_chans) 361 return true; 362 363 if (new_n_chans > old_n_chans) 364 return false; 365 366 for (i = 0; i < new_n_chans; i++) { 367 is_found = false; 368 for (j = 0; j < old_n_chans; j++) { 369 if (new_subchans_freq[i] == old_subchans_freq[j]) { 370 is_found = true; 371 break; 372 } 373 } 374 375 /* If new_subchans[i] is not found in old_subchans, then, 376 * new_chan is not subset of old_chan. 377 */ 378 if (!is_found) 379 break; 380 } 381 382 return is_found; 383 } 384 #endif 385 386 #ifdef CONFIG_CHAN_FREQ_API 387 uint8_t 388 dfs_find_dfs_sub_channels_for_freq(struct wlan_dfs *dfs, 389 struct dfs_channel *chan, 390 uint16_t *subchan_arr) 391 { 392 if (WLAN_IS_CHAN_MODE_160(chan) || WLAN_IS_CHAN_MODE_80_80(chan)) { 393 if (WLAN_IS_CHAN_DFS(chan) && WLAN_IS_CHAN_DFS_CFREQ2(chan)) 394 return dfs_get_bonding_channel_without_seg_info_for_freq 395 (chan, subchan_arr); 396 if (WLAN_IS_CHAN_DFS(chan)) 397 return dfs_get_bonding_channels_for_freq(dfs, 398 chan, 399 SEG_ID_PRIMARY, 400 DETECTOR_ID_0, 401 subchan_arr); 402 if (WLAN_IS_CHAN_DFS_CFREQ2(chan)) 403 return dfs_get_bonding_channels_for_freq 404 (dfs, chan, SEG_ID_SECONDARY, 405 DETECTOR_ID_0, subchan_arr); 406 /* All channels in 160/80_80 BW are non DFS, return 0 407 * as number of subchannels 408 */ 409 return 0; 410 } else if (WLAN_IS_CHAN_DFS(chan)) { 411 return dfs_get_bonding_channel_without_seg_info_for_freq 412 (chan, subchan_arr); 413 } 414 /* All channels are non DFS, return 0 as number of subchannels*/ 415 return 0; 416 } 417 #endif 418 419 #ifdef CONFIG_CHAN_FREQ_API 420 bool 421 dfs_is_new_chan_subset_of_old_chan(struct wlan_dfs *dfs, 422 struct dfs_channel *new_chan, 423 struct dfs_channel *old_chan) 424 { 425 uint16_t new_subchans[MAX_20MHZ_SUBCHANS]; 426 uint16_t old_subchans[MAX_20MHZ_SUBCHANS]; 427 uint8_t n_new_subchans = 0; 428 uint8_t n_old_subchans = 0; 429 430 /* Given channel is the old channel. i.e. The channel which 431 * should have the new channel as subset. 432 */ 433 n_old_subchans = dfs_find_dfs_sub_channels_for_freq(dfs, old_chan, 434 old_subchans); 435 /* cur_chan is the new channel to be check if subset of old channel */ 436 n_new_subchans = dfs_find_dfs_sub_channels_for_freq(dfs, new_chan, 437 new_subchans); 438 439 return dfs_is_subset_channel_for_freq(old_subchans, 440 n_old_subchans, 441 new_subchans, 442 n_new_subchans); 443 } 444 #endif 445 446 #ifdef QCA_SUPPORT_DFS_CAC 447 bool dfs_is_cac_required(struct wlan_dfs *dfs, 448 struct dfs_channel *cur_chan, 449 struct dfs_channel *prev_chan, 450 bool *continue_current_cac, 451 bool is_vap_restart) 452 { 453 struct dfs_channel *cac_started_chan = &dfs->dfs_cac_started_chan; 454 455 if (!WLAN_IS_PRIMARY_OR_SECONDARY_CHAN_DFS(cur_chan)) { 456 dfs_debug(dfs, WLAN_DEBUG_DFS, "Skip CAC on non-DFS channel"); 457 return false; 458 } 459 460 if (dfs->dfs_ignore_dfs || dfs->dfs_cac_valid || dfs->dfs_ignore_cac) { 461 dfs_debug(dfs, WLAN_DEBUG_DFS, 462 "Skip CAC, ignore_dfs = %d cac_valid = %d ignore_cac = %d", 463 dfs->dfs_ignore_dfs, dfs->dfs_cac_valid, 464 dfs->dfs_ignore_cac); 465 return false; 466 } 467 468 /* In case of RCAC, check if CAC is completed only on the RCAC channel 469 * and do not check the CAC info on current operating channel. 470 */ 471 if (dfs_is_agile_rcac_enabled(dfs) && 472 dfs_is_rcac_cac_done(dfs, cur_chan, prev_chan)) 473 return false; 474 475 /* If the channel has completed PRE-CAC then CAC can be skipped here. */ 476 if (dfs_is_precac_done(dfs, cur_chan)) { 477 dfs_debug(dfs, WLAN_DEBUG_DFS, 478 "PRE-CAC alreay done on this channel %d", 479 cur_chan->dfs_ch_ieee); 480 return false; 481 } 482 483 if (dfs_is_ap_cac_timer_running(dfs)) { 484 /* Check if we should continue the existing CAC or 485 * cancel the existing CAC. 486 * For example: - if an existing VAP(0) is already in 487 * DFS wait state (which means the radio(wifi) is 488 * running the CAC) and it is in channel A and another 489 * VAP(1) comes up in the same channel then instead of 490 * cancelling the CAC we can let the CAC continue. 491 */ 492 if (dfs_is_new_chan_subset_of_old_chan(dfs, 493 cur_chan, 494 cac_started_chan)) { 495 if (continue_current_cac) 496 *continue_current_cac = true; 497 } else { 498 /* New CAC is needed, cancel the running CAC 499 * timer. 500 * 1) When AP is in DFS_WAIT state and it is in 501 * channel A and user restarts the AP vap in 502 * channel B, then cancel the running CAC in 503 * channel A and start new CAC in channel B. 504 * 505 * 2) When AP detects the RADAR during CAC in 506 * channel A, it cancels the running CAC and 507 * tries to find channel B with the reduced 508 * bandwidth with of channel A. 509 * In this case, since the CAC is aborted by 510 * the RADAR, AP should start the CAC again. 511 */ 512 dfs_cancel_cac_timer(dfs); 513 } 514 } else { /* CAC timer is not running. */ 515 /* If channel change happens via VAP DOWN/UP on subset channels, 516 * (eg: from 52 HT80 to 64 HT80) CAC done information 517 * (of 52 HT80) based on subset logic 518 * (as 52 and 64 HT80 are subsets of each other) 519 * is not expected to be preserved as VAP has come up 520 * from DOWN state. Hence do not skip CAC on 64 HT80. 521 * is_vap_restart flag is used as an identifer to indicate if 522 * vap has come up from a DOWN state or UP state (vap restart). 523 */ 524 if (!is_vap_restart) { 525 dfs_debug(dfs, WLAN_DEBUG_DFS, "CAC is needed"); 526 return true; 527 } 528 if (dfs_is_new_chan_subset_of_old_chan(dfs, 529 cur_chan, 530 prev_chan)) { 531 /* AP bandwidth reduce case: 532 * When AP detects the RADAR in in-service monitoring 533 * mode in channel A, it cancels the running CAC and 534 * tries to find the channel B with the reduced 535 * bandwidth of channel A. 536 * If the new channel B is subset of the channel A 537 * then AP skips the CAC. 538 */ 539 if (!dfs->dfs_cac_aborted) { 540 dfs_debug(dfs, WLAN_DEBUG_DFS, "Skip CAC"); 541 return false; 542 } 543 } 544 } 545 546 return true; 547 } 548 #endif 549