dfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011 Neratec Solutions AG
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "hw.h"
  18. #include "hw-ops.h"
  19. #include "ath9k.h"
  20. #include "dfs.h"
  21. #include "dfs_debug.h"
  22. /* internal struct to pass radar data */
  23. struct ath_radar_data {
  24. u8 pulse_bw_info;
  25. u8 rssi;
  26. u8 ext_rssi;
  27. u8 pulse_length_ext;
  28. u8 pulse_length_pri;
  29. };
  30. /**** begin: CHIRP ************************************************************/
  31. /* min and max gradients for defined FCC chirping pulses, given by
  32. * - 20MHz chirp width over a pulse width of 50us
  33. * - 5MHz chirp width over a pulse width of 100us
  34. */
  35. static const int BIN_DELTA_MIN = 1;
  36. static const int BIN_DELTA_MAX = 10;
  37. /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
  38. #define NUM_DIFFS 3
  39. static const int FFT_NUM_SAMPLES = (NUM_DIFFS + 1);
  40. /* Threshold for difference of delta peaks */
  41. static const int MAX_DIFF = 2;
  42. /* width range to be checked for chirping */
  43. static const int MIN_CHIRP_PULSE_WIDTH = 20;
  44. static const int MAX_CHIRP_PULSE_WIDTH = 110;
  45. struct ath9k_dfs_fft_20 {
  46. u8 bin[28];
  47. u8 lower_bins[3];
  48. } __packed;
  49. struct ath9k_dfs_fft_40 {
  50. u8 bin[64];
  51. u8 lower_bins[3];
  52. u8 upper_bins[3];
  53. } __packed;
  54. static inline int fft_max_index(u8 *bins)
  55. {
  56. return (bins[2] & 0xfc) >> 2;
  57. }
  58. static inline int fft_max_magnitude(u8 *bins)
  59. {
  60. return (bins[0] & 0xc0) >> 6 | bins[1] << 2 | (bins[2] & 0x03) << 10;
  61. }
  62. static inline u8 fft_bitmap_weight(u8 *bins)
  63. {
  64. return bins[0] & 0x3f;
  65. }
  66. static int ath9k_get_max_index_ht40(struct ath9k_dfs_fft_40 *fft,
  67. bool is_ctl, bool is_ext)
  68. {
  69. const int DFS_UPPER_BIN_OFFSET = 64;
  70. /* if detected radar on both channels, select the significant one */
  71. if (is_ctl && is_ext) {
  72. /* first check wether channels have 'strong' bins */
  73. is_ctl = fft_bitmap_weight(fft->lower_bins) != 0;
  74. is_ext = fft_bitmap_weight(fft->upper_bins) != 0;
  75. /* if still unclear, take higher magnitude */
  76. if (is_ctl && is_ext) {
  77. int mag_lower = fft_max_magnitude(fft->lower_bins);
  78. int mag_upper = fft_max_magnitude(fft->upper_bins);
  79. if (mag_upper > mag_lower)
  80. is_ctl = false;
  81. else
  82. is_ext = false;
  83. }
  84. }
  85. if (is_ctl)
  86. return fft_max_index(fft->lower_bins);
  87. return fft_max_index(fft->upper_bins) + DFS_UPPER_BIN_OFFSET;
  88. }
  89. static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
  90. int datalen, bool is_ctl, bool is_ext)
  91. {
  92. int i;
  93. int max_bin[FFT_NUM_SAMPLES];
  94. struct ath_hw *ah = sc->sc_ah;
  95. struct ath_common *common = ath9k_hw_common(ah);
  96. int prev_delta;
  97. if (IS_CHAN_HT40(ah->curchan)) {
  98. struct ath9k_dfs_fft_40 *fft = (struct ath9k_dfs_fft_40 *) data;
  99. int num_fft_packets = datalen / sizeof(*fft);
  100. if (num_fft_packets == 0)
  101. return false;
  102. ath_dbg(common, DFS, "HT40: datalen=%d, num_fft_packets=%d\n",
  103. datalen, num_fft_packets);
  104. if (num_fft_packets < (FFT_NUM_SAMPLES)) {
  105. ath_dbg(common, DFS, "not enough packets for chirp\n");
  106. return false;
  107. }
  108. /* HW sometimes adds 2 garbage bytes in front of FFT samples */
  109. if ((datalen % sizeof(*fft)) == 2) {
  110. fft = (struct ath9k_dfs_fft_40 *) (data + 2);
  111. ath_dbg(common, DFS, "fixing datalen by 2\n");
  112. }
  113. if (IS_CHAN_HT40MINUS(ah->curchan)) {
  114. int temp = is_ctl;
  115. is_ctl = is_ext;
  116. is_ext = temp;
  117. }
  118. for (i = 0; i < FFT_NUM_SAMPLES; i++)
  119. max_bin[i] = ath9k_get_max_index_ht40(fft + i, is_ctl,
  120. is_ext);
  121. } else {
  122. struct ath9k_dfs_fft_20 *fft = (struct ath9k_dfs_fft_20 *) data;
  123. int num_fft_packets = datalen / sizeof(*fft);
  124. if (num_fft_packets == 0)
  125. return false;
  126. ath_dbg(common, DFS, "HT20: datalen=%d, num_fft_packets=%d\n",
  127. datalen, num_fft_packets);
  128. if (num_fft_packets < (FFT_NUM_SAMPLES)) {
  129. ath_dbg(common, DFS, "not enough packets for chirp\n");
  130. return false;
  131. }
  132. /* in ht20, this is a 6-bit signed number => shift it to 0 */
  133. for (i = 0; i < FFT_NUM_SAMPLES; i++)
  134. max_bin[i] = fft_max_index(fft[i].lower_bins) ^ 0x20;
  135. }
  136. ath_dbg(common, DFS, "bin_max = [%d, %d, %d, %d]\n",
  137. max_bin[0], max_bin[1], max_bin[2], max_bin[3]);
  138. /* Check for chirp attributes within specs
  139. * a) delta of adjacent max_bins is within range
  140. * b) delta of adjacent deltas are within tolerance
  141. */
  142. prev_delta = 0;
  143. for (i = 0; i < NUM_DIFFS; i++) {
  144. int ddelta = -1;
  145. int delta = max_bin[i + 1] - max_bin[i];
  146. /* ensure gradient is within valid range */
  147. if (abs(delta) < BIN_DELTA_MIN || abs(delta) > BIN_DELTA_MAX) {
  148. ath_dbg(common, DFS, "CHIRP: invalid delta %d "
  149. "in sample %d\n", delta, i);
  150. return false;
  151. }
  152. if (i == 0)
  153. goto done;
  154. ddelta = delta - prev_delta;
  155. if (abs(ddelta) > MAX_DIFF) {
  156. ath_dbg(common, DFS, "CHIRP: ddelta %d too high\n",
  157. ddelta);
  158. return false;
  159. }
  160. done:
  161. ath_dbg(common, DFS, "CHIRP - %d: delta=%d, ddelta=%d\n",
  162. i, delta, ddelta);
  163. prev_delta = delta;
  164. }
  165. return true;
  166. }
  167. /**** end: CHIRP **************************************************************/
  168. /* convert pulse duration to usecs, considering clock mode */
  169. static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
  170. {
  171. const u32 AR93X_NSECS_PER_DUR = 800;
  172. const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
  173. u32 nsecs;
  174. if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
  175. nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
  176. else
  177. nsecs = dur * AR93X_NSECS_PER_DUR;
  178. return (nsecs + 500) / 1000;
  179. }
  180. #define PRI_CH_RADAR_FOUND 0x01
  181. #define EXT_CH_RADAR_FOUND 0x02
  182. static bool
  183. ath9k_postprocess_radar_event(struct ath_softc *sc,
  184. struct ath_radar_data *ard,
  185. struct pulse_event *pe)
  186. {
  187. u8 rssi;
  188. u16 dur;
  189. /*
  190. * Only the last 2 bits of the BW info are relevant, they indicate
  191. * which channel the radar was detected in.
  192. */
  193. ard->pulse_bw_info &= 0x03;
  194. switch (ard->pulse_bw_info) {
  195. case PRI_CH_RADAR_FOUND:
  196. /* radar in ctrl channel */
  197. dur = ard->pulse_length_pri;
  198. DFS_STAT_INC(sc, pri_phy_errors);
  199. /*
  200. * cannot use ctrl channel RSSI
  201. * if extension channel is stronger
  202. */
  203. rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
  204. break;
  205. case EXT_CH_RADAR_FOUND:
  206. /* radar in extension channel */
  207. dur = ard->pulse_length_ext;
  208. DFS_STAT_INC(sc, ext_phy_errors);
  209. /*
  210. * cannot use extension channel RSSI
  211. * if control channel is stronger
  212. */
  213. rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
  214. break;
  215. case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
  216. /*
  217. * Conducted testing, when pulse is on DC, both pri and ext
  218. * durations are reported to be same
  219. *
  220. * Radiated testing, when pulse is on DC, different pri and
  221. * ext durations are reported, so take the larger of the two
  222. */
  223. if (ard->pulse_length_ext >= ard->pulse_length_pri)
  224. dur = ard->pulse_length_ext;
  225. else
  226. dur = ard->pulse_length_pri;
  227. DFS_STAT_INC(sc, dc_phy_errors);
  228. /* when both are present use stronger one */
  229. rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
  230. break;
  231. default:
  232. /*
  233. * Bogus bandwidth info was received in descriptor,
  234. * so ignore this PHY error
  235. */
  236. DFS_STAT_INC(sc, bwinfo_discards);
  237. return false;
  238. }
  239. if (rssi == 0) {
  240. DFS_STAT_INC(sc, rssi_discards);
  241. return false;
  242. }
  243. /* convert duration to usecs */
  244. pe->width = dur_to_usecs(sc->sc_ah, dur);
  245. pe->rssi = rssi;
  246. DFS_STAT_INC(sc, pulses_detected);
  247. return true;
  248. }
  249. static void
  250. ath9k_dfs_process_radar_pulse(struct ath_softc *sc, struct pulse_event *pe)
  251. {
  252. struct dfs_pattern_detector *pd = sc->dfs_detector;
  253. DFS_STAT_INC(sc, pulses_processed);
  254. if (pd == NULL)
  255. return;
  256. if (!pd->add_pulse(pd, pe))
  257. return;
  258. DFS_STAT_INC(sc, radar_detected);
  259. ieee80211_radar_detected(sc->hw);
  260. }
  261. /*
  262. * DFS: check PHY-error for radar pulse and feed the detector
  263. */
  264. void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
  265. struct ath_rx_status *rs, u64 mactime)
  266. {
  267. struct ath_radar_data ard;
  268. u16 datalen;
  269. char *vdata_end;
  270. struct pulse_event pe;
  271. struct ath_hw *ah = sc->sc_ah;
  272. struct ath_common *common = ath9k_hw_common(ah);
  273. DFS_STAT_INC(sc, pulses_total);
  274. if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
  275. (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
  276. ath_dbg(common, DFS,
  277. "Error: rs_phyer=0x%x not a radar error\n",
  278. rs->rs_phyerr);
  279. DFS_STAT_INC(sc, pulses_no_dfs);
  280. return;
  281. }
  282. datalen = rs->rs_datalen;
  283. if (datalen == 0) {
  284. DFS_STAT_INC(sc, datalen_discards);
  285. return;
  286. }
  287. ard.rssi = rs->rs_rssi_ctl[0];
  288. ard.ext_rssi = rs->rs_rssi_ext[0];
  289. /*
  290. * hardware stores this as 8 bit signed value.
  291. * we will cap it at 0 if it is a negative number
  292. */
  293. if (ard.rssi & 0x80)
  294. ard.rssi = 0;
  295. if (ard.ext_rssi & 0x80)
  296. ard.ext_rssi = 0;
  297. vdata_end = (char *)data + datalen;
  298. ard.pulse_bw_info = vdata_end[-1];
  299. ard.pulse_length_ext = vdata_end[-2];
  300. ard.pulse_length_pri = vdata_end[-3];
  301. pe.freq = ah->curchan->channel;
  302. pe.ts = mactime;
  303. if (!ath9k_postprocess_radar_event(sc, &ard, &pe))
  304. return;
  305. if (pe.width > MIN_CHIRP_PULSE_WIDTH &&
  306. pe.width < MAX_CHIRP_PULSE_WIDTH) {
  307. bool is_ctl = !!(ard.pulse_bw_info & PRI_CH_RADAR_FOUND);
  308. bool is_ext = !!(ard.pulse_bw_info & EXT_CH_RADAR_FOUND);
  309. int clen = datalen - 3;
  310. pe.chirp = ath9k_check_chirping(sc, data, clen, is_ctl, is_ext);
  311. } else {
  312. pe.chirp = false;
  313. }
  314. ath_dbg(common, DFS,
  315. "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
  316. "width=%d, rssi=%d, delta_ts=%llu\n",
  317. ard.pulse_bw_info, pe.freq, pe.ts, pe.width, pe.rssi,
  318. pe.ts - sc->dfs_prev_pulse_ts);
  319. sc->dfs_prev_pulse_ts = pe.ts;
  320. if (ard.pulse_bw_info & PRI_CH_RADAR_FOUND)
  321. ath9k_dfs_process_radar_pulse(sc, &pe);
  322. if (IS_CHAN_HT40(ah->curchan) &&
  323. ard.pulse_bw_info & EXT_CH_RADAR_FOUND) {
  324. pe.freq += IS_CHAN_HT40PLUS(ah->curchan) ? 20 : -20;
  325. ath9k_dfs_process_radar_pulse(sc, &pe);
  326. }
  327. }
  328. #undef PRI_CH_RADAR_FOUND
  329. #undef EXT_CH_RADAR_FOUND