18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include "mt76x02.h" 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define RADAR_SPEC(m, len, el, eh, wl, wh, \ 98c2ecf20Sopenharmony_ci w_tolerance, tl, th, t_tolerance, \ 108c2ecf20Sopenharmony_ci bl, bh, event_exp, power_jmp) \ 118c2ecf20Sopenharmony_ci{ \ 128c2ecf20Sopenharmony_ci .mode = m, \ 138c2ecf20Sopenharmony_ci .avg_len = len, \ 148c2ecf20Sopenharmony_ci .e_low = el, \ 158c2ecf20Sopenharmony_ci .e_high = eh, \ 168c2ecf20Sopenharmony_ci .w_low = wl, \ 178c2ecf20Sopenharmony_ci .w_high = wh, \ 188c2ecf20Sopenharmony_ci .w_margin = w_tolerance, \ 198c2ecf20Sopenharmony_ci .t_low = tl, \ 208c2ecf20Sopenharmony_ci .t_high = th, \ 218c2ecf20Sopenharmony_ci .t_margin = t_tolerance, \ 228c2ecf20Sopenharmony_ci .b_low = bl, \ 238c2ecf20Sopenharmony_ci .b_high = bh, \ 248c2ecf20Sopenharmony_ci .event_expiration = event_exp, \ 258c2ecf20Sopenharmony_ci .pwr_jmp = power_jmp \ 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic const struct mt76x02_radar_specs etsi_radar_specs[] = { 298c2ecf20Sopenharmony_ci /* 20MHz */ 308c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 15, 106, 150, 10, 4900, 100096, 10, 0, 318c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 328c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 59, 96, 380, 150, 4900, 100096, 40, 0, 338c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 348c2ecf20Sopenharmony_ci RADAR_SPEC(3, 60, 20, 46, 300, 640, 80, 4900, 10100, 80, 0, 358c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19dd), 368c2ecf20Sopenharmony_ci RADAR_SPEC(8, 8, 2, 9, 106, 150, 32, 4900, 296704, 32, 0, 378c2ecf20Sopenharmony_ci 0x7fffffff, 0x2191c0, 0x15cc), 388c2ecf20Sopenharmony_ci /* 40MHz */ 398c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 15, 106, 150, 10, 4900, 100096, 10, 0, 408c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 418c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 59, 96, 380, 150, 4900, 100096, 40, 0, 428c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 438c2ecf20Sopenharmony_ci RADAR_SPEC(3, 60, 20, 46, 300, 640, 80, 4900, 10100, 80, 0, 448c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19dd), 458c2ecf20Sopenharmony_ci RADAR_SPEC(8, 8, 2, 9, 106, 150, 32, 4900, 296704, 32, 0, 468c2ecf20Sopenharmony_ci 0x7fffffff, 0x2191c0, 0x15cc), 478c2ecf20Sopenharmony_ci /* 80MHz */ 488c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 15, 106, 150, 10, 4900, 100096, 10, 0, 498c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 508c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 59, 96, 380, 150, 4900, 100096, 40, 0, 518c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19cc), 528c2ecf20Sopenharmony_ci RADAR_SPEC(3, 60, 20, 46, 300, 640, 80, 4900, 10100, 80, 0, 538c2ecf20Sopenharmony_ci 0x7fffffff, 0x155cc0, 0x19dd), 548c2ecf20Sopenharmony_ci RADAR_SPEC(8, 8, 2, 9, 106, 150, 32, 4900, 296704, 32, 0, 558c2ecf20Sopenharmony_ci 0x7fffffff, 0x2191c0, 0x15cc) 568c2ecf20Sopenharmony_ci}; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic const struct mt76x02_radar_specs fcc_radar_specs[] = { 598c2ecf20Sopenharmony_ci /* 20MHz */ 608c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 12, 106, 150, 5, 2900, 80100, 5, 0, 618c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x13dc), 628c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 638c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x19dd), 648c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 54, 96, 480, 150, 2900, 80100, 40, 0, 658c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x12cc), 668c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 63, 640, 2080, 32, 19600, 40200, 32, 0, 678c2ecf20Sopenharmony_ci 0x3938700, 0x57bcf00, 0x1289), 688c2ecf20Sopenharmony_ci /* 40MHz */ 698c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 12, 106, 150, 5, 2900, 80100, 5, 0, 708c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x13dc), 718c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 728c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x19dd), 738c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 54, 96, 480, 150, 2900, 80100, 40, 0, 748c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x12cc), 758c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 63, 640, 2080, 32, 19600, 40200, 32, 0, 768c2ecf20Sopenharmony_ci 0x3938700, 0x57bcf00, 0x1289), 778c2ecf20Sopenharmony_ci /* 80MHz */ 788c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 14, 106, 150, 15, 2900, 80100, 15, 0, 798c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x16cc), 808c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 818c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x19dd), 828c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 54, 96, 480, 150, 2900, 80100, 40, 0, 838c2ecf20Sopenharmony_ci 0x7fffffff, 0xfe808, 0x12cc), 848c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 63, 640, 2080, 32, 19600, 40200, 32, 0, 858c2ecf20Sopenharmony_ci 0x3938700, 0x57bcf00, 0x1289) 868c2ecf20Sopenharmony_ci}; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic const struct mt76x02_radar_specs jp_w56_radar_specs[] = { 898c2ecf20Sopenharmony_ci /* 20MHz */ 908c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 150, 5, 2900, 80100, 5, 0, 918c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x13dc), 928c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 938c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x19dd), 948c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 480, 150, 2900, 80100, 40, 0, 958c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x12cc), 968c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 48, 940, 2080, 32, 19600, 40200, 32, 0, 978c2ecf20Sopenharmony_ci 0x3938700, 0X57bcf00, 0x1289), 988c2ecf20Sopenharmony_ci /* 40MHz */ 998c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 150, 5, 2900, 80100, 5, 0, 1008c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x13dc), 1018c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 1028c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x19dd), 1038c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 480, 150, 2900, 80100, 40, 0, 1048c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x12cc), 1058c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 48, 940, 2080, 32, 19600, 40200, 32, 0, 1068c2ecf20Sopenharmony_ci 0x3938700, 0X57bcf00, 0x1289), 1078c2ecf20Sopenharmony_ci /* 80MHz */ 1088c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 9, 106, 150, 15, 2900, 80100, 15, 0, 1098c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1108c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 7, 106, 140, 5, 27600, 27900, 5, 0, 1118c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x19dd), 1128c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 480, 150, 2900, 80100, 40, 0, 1138c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x12cc), 1148c2ecf20Sopenharmony_ci RADAR_SPEC(2, 60, 15, 48, 940, 2080, 32, 19600, 40200, 32, 0, 1158c2ecf20Sopenharmony_ci 0x3938700, 0X57bcf00, 0x1289) 1168c2ecf20Sopenharmony_ci}; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const struct mt76x02_radar_specs jp_w53_radar_specs[] = { 1198c2ecf20Sopenharmony_ci /* 20MHz */ 1208c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 9, 106, 150, 20, 28400, 77000, 20, 0, 1218c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1228c2ecf20Sopenharmony_ci { 0 }, 1238c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 200, 150, 28400, 77000, 60, 0, 1248c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1258c2ecf20Sopenharmony_ci { 0 }, 1268c2ecf20Sopenharmony_ci /* 40MHz */ 1278c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 9, 106, 150, 20, 28400, 77000, 20, 0, 1288c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1298c2ecf20Sopenharmony_ci { 0 }, 1308c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 200, 150, 28400, 77000, 60, 0, 1318c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1328c2ecf20Sopenharmony_ci { 0 }, 1338c2ecf20Sopenharmony_ci /* 80MHz */ 1348c2ecf20Sopenharmony_ci RADAR_SPEC(0, 8, 2, 9, 106, 150, 20, 28400, 77000, 20, 0, 1358c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1368c2ecf20Sopenharmony_ci { 0 }, 1378c2ecf20Sopenharmony_ci RADAR_SPEC(0, 40, 4, 44, 96, 200, 150, 28400, 77000, 60, 0, 1388c2ecf20Sopenharmony_ci 0x7fffffff, 0x14c080, 0x16cc), 1398c2ecf20Sopenharmony_ci { 0 } 1408c2ecf20Sopenharmony_ci}; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic void 1438c2ecf20Sopenharmony_cimt76x02_dfs_set_capture_mode_ctrl(struct mt76x02_dev *dev, u8 enable) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci u32 data; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci data = (1 << 1) | enable; 1488c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 36), data); 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic void mt76x02_dfs_seq_pool_put(struct mt76x02_dev *dev, 1528c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence *seq) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci list_add(&seq->head, &dfs_pd->seq_pool); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci dfs_pd->seq_stats.seq_pool_len++; 1598c2ecf20Sopenharmony_ci dfs_pd->seq_stats.seq_len--; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic struct mt76x02_dfs_sequence * 1638c2ecf20Sopenharmony_cimt76x02_dfs_seq_pool_get(struct mt76x02_dev *dev) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 1668c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence *seq; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (list_empty(&dfs_pd->seq_pool)) { 1698c2ecf20Sopenharmony_ci seq = devm_kzalloc(dev->mt76.dev, sizeof(*seq), GFP_ATOMIC); 1708c2ecf20Sopenharmony_ci } else { 1718c2ecf20Sopenharmony_ci seq = list_first_entry(&dfs_pd->seq_pool, 1728c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence, 1738c2ecf20Sopenharmony_ci head); 1748c2ecf20Sopenharmony_ci list_del(&seq->head); 1758c2ecf20Sopenharmony_ci dfs_pd->seq_stats.seq_pool_len--; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci if (seq) 1788c2ecf20Sopenharmony_ci dfs_pd->seq_stats.seq_len++; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci return seq; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic int mt76x02_dfs_get_multiple(int val, int frac, int margin) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci int remainder, factor; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci if (!frac) 1888c2ecf20Sopenharmony_ci return 0; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci if (abs(val - frac) <= margin) 1918c2ecf20Sopenharmony_ci return 1; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci factor = val / frac; 1948c2ecf20Sopenharmony_ci remainder = val % frac; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci if (remainder > margin) { 1978c2ecf20Sopenharmony_ci if ((frac - remainder) <= margin) 1988c2ecf20Sopenharmony_ci factor++; 1998c2ecf20Sopenharmony_ci else 2008c2ecf20Sopenharmony_ci factor = 0; 2018c2ecf20Sopenharmony_ci } 2028c2ecf20Sopenharmony_ci return factor; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic void mt76x02_dfs_detector_reset(struct mt76x02_dev *dev) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 2088c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence *seq, *tmp_seq; 2098c2ecf20Sopenharmony_ci int i; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* reset hw detector */ 2128c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 1), 0xf); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* reset sw detector */ 2158c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(dfs_pd->event_rb); i++) { 2168c2ecf20Sopenharmony_ci dfs_pd->event_rb[i].h_rb = 0; 2178c2ecf20Sopenharmony_ci dfs_pd->event_rb[i].t_rb = 0; 2188c2ecf20Sopenharmony_ci } 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci list_for_each_entry_safe(seq, tmp_seq, &dfs_pd->sequences, head) { 2218c2ecf20Sopenharmony_ci list_del_init(&seq->head); 2228c2ecf20Sopenharmony_ci mt76x02_dfs_seq_pool_put(dev, seq); 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic bool mt76x02_dfs_check_chirp(struct mt76x02_dev *dev) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci bool ret = false; 2298c2ecf20Sopenharmony_ci u32 current_ts, delta_ts; 2308c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci current_ts = mt76_rr(dev, MT_PBF_LIFE_TIMER); 2338c2ecf20Sopenharmony_ci delta_ts = current_ts - dfs_pd->chirp_pulse_ts; 2348c2ecf20Sopenharmony_ci dfs_pd->chirp_pulse_ts = current_ts; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* 12 sec */ 2378c2ecf20Sopenharmony_ci if (delta_ts <= (12 * (1 << 20))) { 2388c2ecf20Sopenharmony_ci if (++dfs_pd->chirp_pulse_cnt > 8) 2398c2ecf20Sopenharmony_ci ret = true; 2408c2ecf20Sopenharmony_ci } else { 2418c2ecf20Sopenharmony_ci dfs_pd->chirp_pulse_cnt = 1; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci return ret; 2458c2ecf20Sopenharmony_ci} 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_cistatic void mt76x02_dfs_get_hw_pulse(struct mt76x02_dev *dev, 2488c2ecf20Sopenharmony_ci struct mt76x02_dfs_hw_pulse *pulse) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci u32 data; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci /* select channel */ 2538c2ecf20Sopenharmony_ci data = (MT_DFS_CH_EN << 16) | pulse->engine; 2548c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), data); 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* reported period */ 2578c2ecf20Sopenharmony_ci pulse->period = mt76_rr(dev, MT_BBP(DFS, 19)); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* reported width */ 2608c2ecf20Sopenharmony_ci pulse->w1 = mt76_rr(dev, MT_BBP(DFS, 20)); 2618c2ecf20Sopenharmony_ci pulse->w2 = mt76_rr(dev, MT_BBP(DFS, 23)); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* reported burst number */ 2648c2ecf20Sopenharmony_ci pulse->burst = mt76_rr(dev, MT_BBP(DFS, 22)); 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic bool mt76x02_dfs_check_hw_pulse(struct mt76x02_dev *dev, 2688c2ecf20Sopenharmony_ci struct mt76x02_dfs_hw_pulse *pulse) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci bool ret = false; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci if (!pulse->period || !pulse->w1) 2738c2ecf20Sopenharmony_ci return false; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci switch (dev->mt76.region) { 2768c2ecf20Sopenharmony_ci case NL80211_DFS_FCC: 2778c2ecf20Sopenharmony_ci if (pulse->engine > 3) 2788c2ecf20Sopenharmony_ci break; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (pulse->engine == 3) { 2818c2ecf20Sopenharmony_ci ret = mt76x02_dfs_check_chirp(dev); 2828c2ecf20Sopenharmony_ci break; 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci /* check short pulse*/ 2868c2ecf20Sopenharmony_ci if (pulse->w1 < 120) 2878c2ecf20Sopenharmony_ci ret = (pulse->period >= 2900 && 2888c2ecf20Sopenharmony_ci (pulse->period <= 4700 || 2898c2ecf20Sopenharmony_ci pulse->period >= 6400) && 2908c2ecf20Sopenharmony_ci (pulse->period <= 6800 || 2918c2ecf20Sopenharmony_ci pulse->period >= 10200) && 2928c2ecf20Sopenharmony_ci pulse->period <= 61600); 2938c2ecf20Sopenharmony_ci else if (pulse->w1 < 130) /* 120 - 130 */ 2948c2ecf20Sopenharmony_ci ret = (pulse->period >= 2900 && 2958c2ecf20Sopenharmony_ci pulse->period <= 61600); 2968c2ecf20Sopenharmony_ci else 2978c2ecf20Sopenharmony_ci ret = (pulse->period >= 3500 && 2988c2ecf20Sopenharmony_ci pulse->period <= 10100); 2998c2ecf20Sopenharmony_ci break; 3008c2ecf20Sopenharmony_ci case NL80211_DFS_ETSI: 3018c2ecf20Sopenharmony_ci if (pulse->engine >= 3) 3028c2ecf20Sopenharmony_ci break; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci ret = (pulse->period >= 4900 && 3058c2ecf20Sopenharmony_ci (pulse->period <= 10200 || 3068c2ecf20Sopenharmony_ci pulse->period >= 12400) && 3078c2ecf20Sopenharmony_ci pulse->period <= 100100); 3088c2ecf20Sopenharmony_ci break; 3098c2ecf20Sopenharmony_ci case NL80211_DFS_JP: 3108c2ecf20Sopenharmony_ci if (dev->mphy.chandef.chan->center_freq >= 5250 && 3118c2ecf20Sopenharmony_ci dev->mphy.chandef.chan->center_freq <= 5350) { 3128c2ecf20Sopenharmony_ci /* JPW53 */ 3138c2ecf20Sopenharmony_ci if (pulse->w1 <= 130) 3148c2ecf20Sopenharmony_ci ret = (pulse->period >= 28360 && 3158c2ecf20Sopenharmony_ci (pulse->period <= 28700 || 3168c2ecf20Sopenharmony_ci pulse->period >= 76900) && 3178c2ecf20Sopenharmony_ci pulse->period <= 76940); 3188c2ecf20Sopenharmony_ci break; 3198c2ecf20Sopenharmony_ci } 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci if (pulse->engine > 3) 3228c2ecf20Sopenharmony_ci break; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci if (pulse->engine == 3) { 3258c2ecf20Sopenharmony_ci ret = mt76x02_dfs_check_chirp(dev); 3268c2ecf20Sopenharmony_ci break; 3278c2ecf20Sopenharmony_ci } 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci /* check short pulse*/ 3308c2ecf20Sopenharmony_ci if (pulse->w1 < 120) 3318c2ecf20Sopenharmony_ci ret = (pulse->period >= 2900 && 3328c2ecf20Sopenharmony_ci (pulse->period <= 4700 || 3338c2ecf20Sopenharmony_ci pulse->period >= 6400) && 3348c2ecf20Sopenharmony_ci (pulse->period <= 6800 || 3358c2ecf20Sopenharmony_ci pulse->period >= 27560) && 3368c2ecf20Sopenharmony_ci (pulse->period <= 27960 || 3378c2ecf20Sopenharmony_ci pulse->period >= 28360) && 3388c2ecf20Sopenharmony_ci (pulse->period <= 28700 || 3398c2ecf20Sopenharmony_ci pulse->period >= 79900) && 3408c2ecf20Sopenharmony_ci pulse->period <= 80100); 3418c2ecf20Sopenharmony_ci else if (pulse->w1 < 130) /* 120 - 130 */ 3428c2ecf20Sopenharmony_ci ret = (pulse->period >= 2900 && 3438c2ecf20Sopenharmony_ci (pulse->period <= 10100 || 3448c2ecf20Sopenharmony_ci pulse->period >= 27560) && 3458c2ecf20Sopenharmony_ci (pulse->period <= 27960 || 3468c2ecf20Sopenharmony_ci pulse->period >= 28360) && 3478c2ecf20Sopenharmony_ci (pulse->period <= 28700 || 3488c2ecf20Sopenharmony_ci pulse->period >= 79900) && 3498c2ecf20Sopenharmony_ci pulse->period <= 80100); 3508c2ecf20Sopenharmony_ci else 3518c2ecf20Sopenharmony_ci ret = (pulse->period >= 3900 && 3528c2ecf20Sopenharmony_ci pulse->period <= 10100); 3538c2ecf20Sopenharmony_ci break; 3548c2ecf20Sopenharmony_ci case NL80211_DFS_UNSET: 3558c2ecf20Sopenharmony_ci default: 3568c2ecf20Sopenharmony_ci return false; 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci return ret; 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_cistatic bool mt76x02_dfs_fetch_event(struct mt76x02_dev *dev, 3638c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci u32 data; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci /* 1st: DFS_R37[31]: 0 (engine 0) - 1 (engine 2) 3688c2ecf20Sopenharmony_ci * 2nd: DFS_R37[21:0]: pulse time 3698c2ecf20Sopenharmony_ci * 3rd: DFS_R37[11:0]: pulse width 3708c2ecf20Sopenharmony_ci * 3rd: DFS_R37[25:16]: phase 3718c2ecf20Sopenharmony_ci * 4th: DFS_R37[12:0]: current pwr 3728c2ecf20Sopenharmony_ci * 4th: DFS_R37[21:16]: pwr stable counter 3738c2ecf20Sopenharmony_ci * 3748c2ecf20Sopenharmony_ci * 1st: DFS_R37[31:0] set to 0xffffffff means no event detected 3758c2ecf20Sopenharmony_ci */ 3768c2ecf20Sopenharmony_ci data = mt76_rr(dev, MT_BBP(DFS, 37)); 3778c2ecf20Sopenharmony_ci if (!MT_DFS_CHECK_EVENT(data)) 3788c2ecf20Sopenharmony_ci return false; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci event->engine = MT_DFS_EVENT_ENGINE(data); 3818c2ecf20Sopenharmony_ci data = mt76_rr(dev, MT_BBP(DFS, 37)); 3828c2ecf20Sopenharmony_ci event->ts = MT_DFS_EVENT_TIMESTAMP(data); 3838c2ecf20Sopenharmony_ci data = mt76_rr(dev, MT_BBP(DFS, 37)); 3848c2ecf20Sopenharmony_ci event->width = MT_DFS_EVENT_WIDTH(data); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci return true; 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_cistatic bool mt76x02_dfs_check_event(struct mt76x02_dev *dev, 3908c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event) 3918c2ecf20Sopenharmony_ci{ 3928c2ecf20Sopenharmony_ci if (event->engine == 2) { 3938c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 3948c2ecf20Sopenharmony_ci struct mt76x02_dfs_event_rb *event_buff = &dfs_pd->event_rb[1]; 3958c2ecf20Sopenharmony_ci u16 last_event_idx; 3968c2ecf20Sopenharmony_ci u32 delta_ts; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci last_event_idx = mt76_decr(event_buff->t_rb, 3998c2ecf20Sopenharmony_ci MT_DFS_EVENT_BUFLEN); 4008c2ecf20Sopenharmony_ci delta_ts = event->ts - event_buff->data[last_event_idx].ts; 4018c2ecf20Sopenharmony_ci if (delta_ts < MT_DFS_EVENT_TIME_MARGIN && 4028c2ecf20Sopenharmony_ci event_buff->data[last_event_idx].width >= 200) 4038c2ecf20Sopenharmony_ci return false; 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci return true; 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_cistatic void mt76x02_dfs_queue_event(struct mt76x02_dev *dev, 4098c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 4128c2ecf20Sopenharmony_ci struct mt76x02_dfs_event_rb *event_buff; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* add radar event to ring buffer */ 4158c2ecf20Sopenharmony_ci event_buff = event->engine == 2 ? &dfs_pd->event_rb[1] 4168c2ecf20Sopenharmony_ci : &dfs_pd->event_rb[0]; 4178c2ecf20Sopenharmony_ci event_buff->data[event_buff->t_rb] = *event; 4188c2ecf20Sopenharmony_ci event_buff->data[event_buff->t_rb].fetch_ts = jiffies; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci event_buff->t_rb = mt76_incr(event_buff->t_rb, MT_DFS_EVENT_BUFLEN); 4218c2ecf20Sopenharmony_ci if (event_buff->t_rb == event_buff->h_rb) 4228c2ecf20Sopenharmony_ci event_buff->h_rb = mt76_incr(event_buff->h_rb, 4238c2ecf20Sopenharmony_ci MT_DFS_EVENT_BUFLEN); 4248c2ecf20Sopenharmony_ci} 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_cistatic int mt76x02_dfs_create_sequence(struct mt76x02_dev *dev, 4278c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event, 4288c2ecf20Sopenharmony_ci u16 cur_len) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 4318c2ecf20Sopenharmony_ci struct mt76x02_dfs_sw_detector_params *sw_params; 4328c2ecf20Sopenharmony_ci u32 width_delta, with_sum; 4338c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence seq, *seq_p; 4348c2ecf20Sopenharmony_ci struct mt76x02_dfs_event_rb *event_rb; 4358c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *cur_event; 4368c2ecf20Sopenharmony_ci int i, j, end, pri, factor, cur_pri; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci event_rb = event->engine == 2 ? &dfs_pd->event_rb[1] 4398c2ecf20Sopenharmony_ci : &dfs_pd->event_rb[0]; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci i = mt76_decr(event_rb->t_rb, MT_DFS_EVENT_BUFLEN); 4428c2ecf20Sopenharmony_ci end = mt76_decr(event_rb->h_rb, MT_DFS_EVENT_BUFLEN); 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci while (i != end) { 4458c2ecf20Sopenharmony_ci cur_event = &event_rb->data[i]; 4468c2ecf20Sopenharmony_ci with_sum = event->width + cur_event->width; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci sw_params = &dfs_pd->sw_dpd_params; 4498c2ecf20Sopenharmony_ci switch (dev->mt76.region) { 4508c2ecf20Sopenharmony_ci case NL80211_DFS_FCC: 4518c2ecf20Sopenharmony_ci case NL80211_DFS_JP: 4528c2ecf20Sopenharmony_ci if (with_sum < 600) 4538c2ecf20Sopenharmony_ci width_delta = 8; 4548c2ecf20Sopenharmony_ci else 4558c2ecf20Sopenharmony_ci width_delta = with_sum >> 3; 4568c2ecf20Sopenharmony_ci break; 4578c2ecf20Sopenharmony_ci case NL80211_DFS_ETSI: 4588c2ecf20Sopenharmony_ci if (event->engine == 2) 4598c2ecf20Sopenharmony_ci width_delta = with_sum >> 6; 4608c2ecf20Sopenharmony_ci else if (with_sum < 620) 4618c2ecf20Sopenharmony_ci width_delta = 24; 4628c2ecf20Sopenharmony_ci else 4638c2ecf20Sopenharmony_ci width_delta = 8; 4648c2ecf20Sopenharmony_ci break; 4658c2ecf20Sopenharmony_ci case NL80211_DFS_UNSET: 4668c2ecf20Sopenharmony_ci default: 4678c2ecf20Sopenharmony_ci return -EINVAL; 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci pri = event->ts - cur_event->ts; 4718c2ecf20Sopenharmony_ci if (abs(event->width - cur_event->width) > width_delta || 4728c2ecf20Sopenharmony_ci pri < sw_params->min_pri) 4738c2ecf20Sopenharmony_ci goto next; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci if (pri > sw_params->max_pri) 4768c2ecf20Sopenharmony_ci break; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci seq.pri = event->ts - cur_event->ts; 4798c2ecf20Sopenharmony_ci seq.first_ts = cur_event->ts; 4808c2ecf20Sopenharmony_ci seq.last_ts = event->ts; 4818c2ecf20Sopenharmony_ci seq.engine = event->engine; 4828c2ecf20Sopenharmony_ci seq.count = 2; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci j = mt76_decr(i, MT_DFS_EVENT_BUFLEN); 4858c2ecf20Sopenharmony_ci while (j != end) { 4868c2ecf20Sopenharmony_ci cur_event = &event_rb->data[j]; 4878c2ecf20Sopenharmony_ci cur_pri = event->ts - cur_event->ts; 4888c2ecf20Sopenharmony_ci factor = mt76x02_dfs_get_multiple(cur_pri, seq.pri, 4898c2ecf20Sopenharmony_ci sw_params->pri_margin); 4908c2ecf20Sopenharmony_ci if (factor > 0) { 4918c2ecf20Sopenharmony_ci seq.first_ts = cur_event->ts; 4928c2ecf20Sopenharmony_ci seq.count++; 4938c2ecf20Sopenharmony_ci } 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci j = mt76_decr(j, MT_DFS_EVENT_BUFLEN); 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci if (seq.count <= cur_len) 4988c2ecf20Sopenharmony_ci goto next; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci seq_p = mt76x02_dfs_seq_pool_get(dev); 5018c2ecf20Sopenharmony_ci if (!seq_p) 5028c2ecf20Sopenharmony_ci return -ENOMEM; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci *seq_p = seq; 5058c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&seq_p->head); 5068c2ecf20Sopenharmony_ci list_add(&seq_p->head, &dfs_pd->sequences); 5078c2ecf20Sopenharmony_cinext: 5088c2ecf20Sopenharmony_ci i = mt76_decr(i, MT_DFS_EVENT_BUFLEN); 5098c2ecf20Sopenharmony_ci } 5108c2ecf20Sopenharmony_ci return 0; 5118c2ecf20Sopenharmony_ci} 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_cistatic u16 mt76x02_dfs_add_event_to_sequence(struct mt76x02_dev *dev, 5148c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 5178c2ecf20Sopenharmony_ci struct mt76x02_dfs_sw_detector_params *sw_params; 5188c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence *seq, *tmp_seq; 5198c2ecf20Sopenharmony_ci u16 max_seq_len = 0; 5208c2ecf20Sopenharmony_ci int factor, pri; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci sw_params = &dfs_pd->sw_dpd_params; 5238c2ecf20Sopenharmony_ci list_for_each_entry_safe(seq, tmp_seq, &dfs_pd->sequences, head) { 5248c2ecf20Sopenharmony_ci if (event->ts > seq->first_ts + MT_DFS_SEQUENCE_WINDOW) { 5258c2ecf20Sopenharmony_ci list_del_init(&seq->head); 5268c2ecf20Sopenharmony_ci mt76x02_dfs_seq_pool_put(dev, seq); 5278c2ecf20Sopenharmony_ci continue; 5288c2ecf20Sopenharmony_ci } 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci if (event->engine != seq->engine) 5318c2ecf20Sopenharmony_ci continue; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci pri = event->ts - seq->last_ts; 5348c2ecf20Sopenharmony_ci factor = mt76x02_dfs_get_multiple(pri, seq->pri, 5358c2ecf20Sopenharmony_ci sw_params->pri_margin); 5368c2ecf20Sopenharmony_ci if (factor > 0) { 5378c2ecf20Sopenharmony_ci seq->last_ts = event->ts; 5388c2ecf20Sopenharmony_ci seq->count++; 5398c2ecf20Sopenharmony_ci max_seq_len = max_t(u16, max_seq_len, seq->count); 5408c2ecf20Sopenharmony_ci } 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci return max_seq_len; 5438c2ecf20Sopenharmony_ci} 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_cistatic bool mt76x02_dfs_check_detection(struct mt76x02_dev *dev) 5468c2ecf20Sopenharmony_ci{ 5478c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 5488c2ecf20Sopenharmony_ci struct mt76x02_dfs_sequence *seq; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci if (list_empty(&dfs_pd->sequences)) 5518c2ecf20Sopenharmony_ci return false; 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci list_for_each_entry(seq, &dfs_pd->sequences, head) { 5548c2ecf20Sopenharmony_ci if (seq->count > MT_DFS_SEQUENCE_TH) { 5558c2ecf20Sopenharmony_ci dfs_pd->stats[seq->engine].sw_pattern++; 5568c2ecf20Sopenharmony_ci return true; 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci } 5598c2ecf20Sopenharmony_ci return false; 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_cistatic void mt76x02_dfs_add_events(struct mt76x02_dev *dev) 5638c2ecf20Sopenharmony_ci{ 5648c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 5658c2ecf20Sopenharmony_ci struct mt76x02_dfs_event event; 5668c2ecf20Sopenharmony_ci int i, seq_len; 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci /* disable debug mode */ 5698c2ecf20Sopenharmony_ci mt76x02_dfs_set_capture_mode_ctrl(dev, false); 5708c2ecf20Sopenharmony_ci for (i = 0; i < MT_DFS_EVENT_LOOP; i++) { 5718c2ecf20Sopenharmony_ci if (!mt76x02_dfs_fetch_event(dev, &event)) 5728c2ecf20Sopenharmony_ci break; 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci if (dfs_pd->last_event_ts > event.ts) 5758c2ecf20Sopenharmony_ci mt76x02_dfs_detector_reset(dev); 5768c2ecf20Sopenharmony_ci dfs_pd->last_event_ts = event.ts; 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci if (!mt76x02_dfs_check_event(dev, &event)) 5798c2ecf20Sopenharmony_ci continue; 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci seq_len = mt76x02_dfs_add_event_to_sequence(dev, &event); 5828c2ecf20Sopenharmony_ci mt76x02_dfs_create_sequence(dev, &event, seq_len); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci mt76x02_dfs_queue_event(dev, &event); 5858c2ecf20Sopenharmony_ci } 5868c2ecf20Sopenharmony_ci mt76x02_dfs_set_capture_mode_ctrl(dev, true); 5878c2ecf20Sopenharmony_ci} 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_cistatic void mt76x02_dfs_check_event_window(struct mt76x02_dev *dev) 5908c2ecf20Sopenharmony_ci{ 5918c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 5928c2ecf20Sopenharmony_ci struct mt76x02_dfs_event_rb *event_buff; 5938c2ecf20Sopenharmony_ci struct mt76x02_dfs_event *event; 5948c2ecf20Sopenharmony_ci int i; 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(dfs_pd->event_rb); i++) { 5978c2ecf20Sopenharmony_ci event_buff = &dfs_pd->event_rb[i]; 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci while (event_buff->h_rb != event_buff->t_rb) { 6008c2ecf20Sopenharmony_ci event = &event_buff->data[event_buff->h_rb]; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci /* sorted list */ 6038c2ecf20Sopenharmony_ci if (time_is_after_jiffies(event->fetch_ts + 6048c2ecf20Sopenharmony_ci MT_DFS_EVENT_WINDOW)) 6058c2ecf20Sopenharmony_ci break; 6068c2ecf20Sopenharmony_ci event_buff->h_rb = mt76_incr(event_buff->h_rb, 6078c2ecf20Sopenharmony_ci MT_DFS_EVENT_BUFLEN); 6088c2ecf20Sopenharmony_ci } 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci} 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_cistatic void mt76x02_dfs_tasklet(unsigned long arg) 6138c2ecf20Sopenharmony_ci{ 6148c2ecf20Sopenharmony_ci struct mt76x02_dev *dev = (struct mt76x02_dev *)arg; 6158c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 6168c2ecf20Sopenharmony_ci u32 engine_mask; 6178c2ecf20Sopenharmony_ci int i; 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci if (test_bit(MT76_SCANNING, &dev->mphy.state)) 6208c2ecf20Sopenharmony_ci goto out; 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci if (time_is_before_jiffies(dfs_pd->last_sw_check + 6238c2ecf20Sopenharmony_ci MT_DFS_SW_TIMEOUT)) { 6248c2ecf20Sopenharmony_ci bool radar_detected; 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_ci dfs_pd->last_sw_check = jiffies; 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci mt76x02_dfs_add_events(dev); 6298c2ecf20Sopenharmony_ci radar_detected = mt76x02_dfs_check_detection(dev); 6308c2ecf20Sopenharmony_ci if (radar_detected) { 6318c2ecf20Sopenharmony_ci /* sw detector rx radar pattern */ 6328c2ecf20Sopenharmony_ci ieee80211_radar_detected(dev->mt76.hw); 6338c2ecf20Sopenharmony_ci mt76x02_dfs_detector_reset(dev); 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci return; 6368c2ecf20Sopenharmony_ci } 6378c2ecf20Sopenharmony_ci mt76x02_dfs_check_event_window(dev); 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci engine_mask = mt76_rr(dev, MT_BBP(DFS, 1)); 6418c2ecf20Sopenharmony_ci if (!(engine_mask & 0xf)) 6428c2ecf20Sopenharmony_ci goto out; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci for (i = 0; i < MT_DFS_NUM_ENGINES; i++) { 6458c2ecf20Sopenharmony_ci struct mt76x02_dfs_hw_pulse pulse; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci if (!(engine_mask & (1 << i))) 6488c2ecf20Sopenharmony_ci continue; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci pulse.engine = i; 6518c2ecf20Sopenharmony_ci mt76x02_dfs_get_hw_pulse(dev, &pulse); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci if (!mt76x02_dfs_check_hw_pulse(dev, &pulse)) { 6548c2ecf20Sopenharmony_ci dfs_pd->stats[i].hw_pulse_discarded++; 6558c2ecf20Sopenharmony_ci continue; 6568c2ecf20Sopenharmony_ci } 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci /* hw detector rx radar pattern */ 6598c2ecf20Sopenharmony_ci dfs_pd->stats[i].hw_pattern++; 6608c2ecf20Sopenharmony_ci ieee80211_radar_detected(dev->mt76.hw); 6618c2ecf20Sopenharmony_ci mt76x02_dfs_detector_reset(dev); 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci return; 6648c2ecf20Sopenharmony_ci } 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci /* reset hw detector */ 6678c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 1), 0xf); 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ciout: 6708c2ecf20Sopenharmony_ci mt76x02_irq_enable(dev, MT_INT_GPTIMER); 6718c2ecf20Sopenharmony_ci} 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_cistatic void mt76x02_dfs_init_sw_detector(struct mt76x02_dev *dev) 6748c2ecf20Sopenharmony_ci{ 6758c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci switch (dev->mt76.region) { 6788c2ecf20Sopenharmony_ci case NL80211_DFS_FCC: 6798c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.max_pri = MT_DFS_FCC_MAX_PRI; 6808c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.min_pri = MT_DFS_FCC_MIN_PRI; 6818c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.pri_margin = MT_DFS_PRI_MARGIN; 6828c2ecf20Sopenharmony_ci break; 6838c2ecf20Sopenharmony_ci case NL80211_DFS_ETSI: 6848c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.max_pri = MT_DFS_ETSI_MAX_PRI; 6858c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.min_pri = MT_DFS_ETSI_MIN_PRI; 6868c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.pri_margin = MT_DFS_PRI_MARGIN << 2; 6878c2ecf20Sopenharmony_ci break; 6888c2ecf20Sopenharmony_ci case NL80211_DFS_JP: 6898c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.max_pri = MT_DFS_JP_MAX_PRI; 6908c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.min_pri = MT_DFS_JP_MIN_PRI; 6918c2ecf20Sopenharmony_ci dfs_pd->sw_dpd_params.pri_margin = MT_DFS_PRI_MARGIN; 6928c2ecf20Sopenharmony_ci break; 6938c2ecf20Sopenharmony_ci case NL80211_DFS_UNSET: 6948c2ecf20Sopenharmony_ci default: 6958c2ecf20Sopenharmony_ci break; 6968c2ecf20Sopenharmony_ci } 6978c2ecf20Sopenharmony_ci} 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_cistatic void mt76x02_dfs_set_bbp_params(struct mt76x02_dev *dev) 7008c2ecf20Sopenharmony_ci{ 7018c2ecf20Sopenharmony_ci const struct mt76x02_radar_specs *radar_specs; 7028c2ecf20Sopenharmony_ci u8 i, shift; 7038c2ecf20Sopenharmony_ci u32 data; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci switch (dev->mphy.chandef.width) { 7068c2ecf20Sopenharmony_ci case NL80211_CHAN_WIDTH_40: 7078c2ecf20Sopenharmony_ci shift = MT_DFS_NUM_ENGINES; 7088c2ecf20Sopenharmony_ci break; 7098c2ecf20Sopenharmony_ci case NL80211_CHAN_WIDTH_80: 7108c2ecf20Sopenharmony_ci shift = 2 * MT_DFS_NUM_ENGINES; 7118c2ecf20Sopenharmony_ci break; 7128c2ecf20Sopenharmony_ci default: 7138c2ecf20Sopenharmony_ci shift = 0; 7148c2ecf20Sopenharmony_ci break; 7158c2ecf20Sopenharmony_ci } 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci switch (dev->mt76.region) { 7188c2ecf20Sopenharmony_ci case NL80211_DFS_FCC: 7198c2ecf20Sopenharmony_ci radar_specs = &fcc_radar_specs[shift]; 7208c2ecf20Sopenharmony_ci break; 7218c2ecf20Sopenharmony_ci case NL80211_DFS_ETSI: 7228c2ecf20Sopenharmony_ci radar_specs = &etsi_radar_specs[shift]; 7238c2ecf20Sopenharmony_ci break; 7248c2ecf20Sopenharmony_ci case NL80211_DFS_JP: 7258c2ecf20Sopenharmony_ci if (dev->mphy.chandef.chan->center_freq >= 5250 && 7268c2ecf20Sopenharmony_ci dev->mphy.chandef.chan->center_freq <= 5350) 7278c2ecf20Sopenharmony_ci radar_specs = &jp_w53_radar_specs[shift]; 7288c2ecf20Sopenharmony_ci else 7298c2ecf20Sopenharmony_ci radar_specs = &jp_w56_radar_specs[shift]; 7308c2ecf20Sopenharmony_ci break; 7318c2ecf20Sopenharmony_ci case NL80211_DFS_UNSET: 7328c2ecf20Sopenharmony_ci default: 7338c2ecf20Sopenharmony_ci return; 7348c2ecf20Sopenharmony_ci } 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci data = (MT_DFS_VGA_MASK << 16) | 7378c2ecf20Sopenharmony_ci (MT_DFS_PWR_GAIN_OFFSET << 12) | 7388c2ecf20Sopenharmony_ci (MT_DFS_PWR_DOWN_TIME << 8) | 7398c2ecf20Sopenharmony_ci (MT_DFS_SYM_ROUND << 4) | 7408c2ecf20Sopenharmony_ci (MT_DFS_DELTA_DELAY & 0xf); 7418c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 2), data); 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci data = (MT_DFS_RX_PE_MASK << 16) | MT_DFS_PKT_END_MASK; 7448c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 3), data); 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci for (i = 0; i < MT_DFS_NUM_ENGINES; i++) { 7478c2ecf20Sopenharmony_ci /* configure engine */ 7488c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), i); 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci /* detection mode + avg_len */ 7518c2ecf20Sopenharmony_ci data = ((radar_specs[i].avg_len & 0x1ff) << 16) | 7528c2ecf20Sopenharmony_ci (radar_specs[i].mode & 0xf); 7538c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 4), data); 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci /* dfs energy */ 7568c2ecf20Sopenharmony_ci data = ((radar_specs[i].e_high & 0x0fff) << 16) | 7578c2ecf20Sopenharmony_ci (radar_specs[i].e_low & 0x0fff); 7588c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 5), data); 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci /* dfs period */ 7618c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 7), radar_specs[i].t_low); 7628c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 9), radar_specs[i].t_high); 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci /* dfs burst */ 7658c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 11), radar_specs[i].b_low); 7668c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 13), radar_specs[i].b_high); 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci /* dfs width */ 7698c2ecf20Sopenharmony_ci data = ((radar_specs[i].w_high & 0x0fff) << 16) | 7708c2ecf20Sopenharmony_ci (radar_specs[i].w_low & 0x0fff); 7718c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 14), data); 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci /* dfs margins */ 7748c2ecf20Sopenharmony_ci data = (radar_specs[i].w_margin << 16) | 7758c2ecf20Sopenharmony_ci radar_specs[i].t_margin; 7768c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 15), data); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci /* dfs event expiration */ 7798c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 17), radar_specs[i].event_expiration); 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci /* dfs pwr adj */ 7828c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 30), radar_specs[i].pwr_jmp); 7838c2ecf20Sopenharmony_ci } 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci /* reset status */ 7868c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 1), 0xf); 7878c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 36), 0x3); 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci /* enable detection*/ 7908c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), MT_DFS_CH_EN << 16); 7918c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(IBI, 11), 0x0c350001); 7928c2ecf20Sopenharmony_ci} 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_civoid mt76x02_phy_dfs_adjust_agc(struct mt76x02_dev *dev) 7958c2ecf20Sopenharmony_ci{ 7968c2ecf20Sopenharmony_ci u32 agc_r8, agc_r4, val_r8, val_r4, dfs_r31; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci agc_r8 = mt76_rr(dev, MT_BBP(AGC, 8)); 7998c2ecf20Sopenharmony_ci agc_r4 = mt76_rr(dev, MT_BBP(AGC, 4)); 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci val_r8 = (agc_r8 & 0x00007e00) >> 9; 8028c2ecf20Sopenharmony_ci val_r4 = agc_r4 & ~0x1f000000; 8038c2ecf20Sopenharmony_ci val_r4 += (((val_r8 + 1) >> 1) << 24); 8048c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(AGC, 4), val_r4); 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci dfs_r31 = FIELD_GET(MT_BBP_AGC_LNA_HIGH_GAIN, val_r4); 8078c2ecf20Sopenharmony_ci dfs_r31 += val_r8; 8088c2ecf20Sopenharmony_ci dfs_r31 -= (agc_r8 & 0x00000038) >> 3; 8098c2ecf20Sopenharmony_ci dfs_r31 = (dfs_r31 << 16) | 0x00000307; 8108c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 31), dfs_r31); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci if (is_mt76x2(dev)) { 8138c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 32), 0x00040071); 8148c2ecf20Sopenharmony_ci } else { 8158c2ecf20Sopenharmony_ci /* disable hw detector */ 8168c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), 0); 8178c2ecf20Sopenharmony_ci /* enable hw detector */ 8188c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), MT_DFS_CH_EN << 16); 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci} 8218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_phy_dfs_adjust_agc); 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_civoid mt76x02_dfs_init_params(struct mt76x02_dev *dev) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci struct cfg80211_chan_def *chandef = &dev->mphy.chandef; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) && 8288c2ecf20Sopenharmony_ci dev->mt76.region != NL80211_DFS_UNSET) { 8298c2ecf20Sopenharmony_ci mt76x02_dfs_init_sw_detector(dev); 8308c2ecf20Sopenharmony_ci mt76x02_dfs_set_bbp_params(dev); 8318c2ecf20Sopenharmony_ci /* enable debug mode */ 8328c2ecf20Sopenharmony_ci mt76x02_dfs_set_capture_mode_ctrl(dev, true); 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci mt76x02_irq_enable(dev, MT_INT_GPTIMER); 8358c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_INT_TIMER_EN, 8368c2ecf20Sopenharmony_ci MT_INT_TIMER_EN_GP_TIMER_EN, 1); 8378c2ecf20Sopenharmony_ci } else { 8388c2ecf20Sopenharmony_ci /* disable hw detector */ 8398c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 0), 0); 8408c2ecf20Sopenharmony_ci /* clear detector status */ 8418c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(DFS, 1), 0xf); 8428c2ecf20Sopenharmony_ci if (mt76_chip(&dev->mt76) == 0x7610 || 8438c2ecf20Sopenharmony_ci mt76_chip(&dev->mt76) == 0x7630) 8448c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(IBI, 11), 0xfde8081); 8458c2ecf20Sopenharmony_ci else 8468c2ecf20Sopenharmony_ci mt76_wr(dev, MT_BBP(IBI, 11), 0); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci mt76x02_irq_disable(dev, MT_INT_GPTIMER); 8498c2ecf20Sopenharmony_ci mt76_rmw_field(dev, MT_INT_TIMER_EN, 8508c2ecf20Sopenharmony_ci MT_INT_TIMER_EN_GP_TIMER_EN, 0); 8518c2ecf20Sopenharmony_ci } 8528c2ecf20Sopenharmony_ci} 8538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x02_dfs_init_params); 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_civoid mt76x02_dfs_init_detector(struct mt76x02_dev *dev) 8568c2ecf20Sopenharmony_ci{ 8578c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&dfs_pd->sequences); 8608c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&dfs_pd->seq_pool); 8618c2ecf20Sopenharmony_ci dev->mt76.region = NL80211_DFS_UNSET; 8628c2ecf20Sopenharmony_ci dfs_pd->last_sw_check = jiffies; 8638c2ecf20Sopenharmony_ci tasklet_init(&dfs_pd->dfs_tasklet, mt76x02_dfs_tasklet, 8648c2ecf20Sopenharmony_ci (unsigned long)dev); 8658c2ecf20Sopenharmony_ci} 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_cistatic void 8688c2ecf20Sopenharmony_cimt76x02_dfs_set_domain(struct mt76x02_dev *dev, 8698c2ecf20Sopenharmony_ci enum nl80211_dfs_regions region) 8708c2ecf20Sopenharmony_ci{ 8718c2ecf20Sopenharmony_ci struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd; 8728c2ecf20Sopenharmony_ci 8738c2ecf20Sopenharmony_ci mutex_lock(&dev->mt76.mutex); 8748c2ecf20Sopenharmony_ci if (dev->mt76.region != region) { 8758c2ecf20Sopenharmony_ci tasklet_disable(&dfs_pd->dfs_tasklet); 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci dev->ed_monitor = dev->ed_monitor_enabled && 8788c2ecf20Sopenharmony_ci region == NL80211_DFS_ETSI; 8798c2ecf20Sopenharmony_ci mt76x02_edcca_init(dev); 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci dev->mt76.region = region; 8828c2ecf20Sopenharmony_ci mt76x02_dfs_init_params(dev); 8838c2ecf20Sopenharmony_ci tasklet_enable(&dfs_pd->dfs_tasklet); 8848c2ecf20Sopenharmony_ci } 8858c2ecf20Sopenharmony_ci mutex_unlock(&dev->mt76.mutex); 8868c2ecf20Sopenharmony_ci} 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_civoid mt76x02_regd_notifier(struct wiphy *wiphy, 8898c2ecf20Sopenharmony_ci struct regulatory_request *request) 8908c2ecf20Sopenharmony_ci{ 8918c2ecf20Sopenharmony_ci struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 8928c2ecf20Sopenharmony_ci struct mt76x02_dev *dev = hw->priv; 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci mt76x02_dfs_set_domain(dev, request->dfs_region); 8958c2ecf20Sopenharmony_ci} 896