dfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #define 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. swap(is_ctl, is_ext);
  115. for (i = 0; i < FFT_NUM_SAMPLES; i++)
  116. max_bin[i] = ath9k_get_max_index_ht40(fft + i, is_ctl,
  117. is_ext);
  118. } else {
  119. struct ath9k_dfs_fft_20 *fft = (struct ath9k_dfs_fft_20 *) data;
  120. int num_fft_packets = datalen / sizeof(*fft);
  121. if (num_fft_packets == 0)
  122. return false;
  123. ath_dbg(common, DFS, "HT20: datalen=%d, num_fft_packets=%d\n",
  124. datalen, num_fft_packets);
  125. if (num_fft_packets < FFT_NUM_SAMPLES) {
  126. ath_dbg(common, DFS, "not enough packets for chirp\n");
  127. return false;
  128. }
  129. /* in ht20, this is a 6-bit signed number => shift it to 0 */
  130. for (i = 0; i < FFT_NUM_SAMPLES; i++)
  131. max_bin[i] = fft_max_index(fft[i].lower_bins) ^ 0x20;
  132. }
  133. ath_dbg(common, DFS, "bin_max = [%d, %d, %d, %d]\n",
  134. max_bin[0], max_bin[1], max_bin[2], max_bin[3]);
  135. /* Check for chirp attributes within specs
  136. * a) delta of adjacent max_bins is within range
  137. * b) delta of adjacent deltas are within tolerance
  138. */
  139. prev_delta = 0;
  140. for (i = 0; i < NUM_DIFFS; i++) {
  141. int ddelta = -1;
  142. int delta = max_bin[i + 1] - max_bin[i];
  143. /* ensure gradient is within valid range */
  144. if (abs(delta) < BIN_DELTA_MIN || abs(delta) > BIN_DELTA_MAX) {
  145. ath_dbg(common, DFS, "CHIRP: invalid delta %d "
  146. "in sample %d\n", delta, i);
  147. return false;
  148. }
  149. if (i == 0)
  150. goto done;
  151. ddelta = delta - prev_delta;
  152. if (abs(ddelta) > MAX_DIFF) {
  153. ath_dbg(common, DFS, "CHIRP: ddelta %d too high\n",
  154. ddelta);
  155. return false;
  156. }
  157. done:
  158. ath_dbg(common, DFS, "CHIRP - %d: delta=%d, ddelta=%d\n",
  159. i, delta, ddelta);
  160. prev_delta = delta;
  161. }
  162. return true;
  163. }
  164. /**** end: CHIRP **************************************************************/
  165. /* convert pulse duration to usecs, considering clock mode */
  166. static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
  167. {
  168. const u32 AR93X_NSECS_PER_DUR = 800;
  169. const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
  170. u32 nsecs;
  171. if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
  172. nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
  173. else
  174. nsecs = dur * AR93X_NSECS_PER_DUR;
  175. return (nsecs + 500) / 1000;
  176. }
  177. #define PRI_CH_RADAR_FOUND 0x01
  178. #define EXT_CH_RADAR_FOUND 0x02
  179. static bool
  180. ath9k_postprocess_radar_event(struct ath_softc *sc,
  181. struct ath_radar_data *ard,
  182. struct pulse_event *pe)
  183. {
  184. u8 rssi;
  185. u16 dur;
  186. /*
  187. * Only the last 2 bits of the BW info are relevant, they indicate
  188. * which channel the radar was detected in.
  189. */
  190. ard->pulse_bw_info &= 0x03;
  191. switch (ard->pulse_bw_info) {
  192. case PRI_CH_RADAR_FOUND:
  193. /* radar in ctrl channel */
  194. dur = ard->pulse_length_pri;
  195. DFS_STAT_INC(sc, pri_phy_errors);
  196. /*
  197. * cannot use ctrl channel RSSI
  198. * if extension channel is stronger
  199. */
  200. rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
  201. break;
  202. case EXT_CH_RADAR_FOUND:
  203. /* radar in extension channel */
  204. dur = ard->pulse_length_ext;
  205. DFS_STAT_INC(sc, ext_phy_errors);
  206. /*
  207. * cannot use extension channel RSSI
  208. * if control channel is stronger
  209. */
  210. rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
  211. break;
  212. case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
  213. /*
  214. * Conducted testing, when pulse is on DC, both pri and ext
  215. * durations are reported to be same
  216. *
  217. * Radiated testing, when pulse is on DC, different pri and
  218. * ext durations are reported, so take the larger of the two
  219. */
  220. if (ard->pulse_length_ext >= ard->pulse_length_pri)
  221. dur = ard->pulse_length_ext;
  222. else
  223. dur = ard->pulse_length_pri;
  224. DFS_STAT_INC(sc, dc_phy_errors);
  225. /* when both are present use stronger one */
  226. rssi = max(ard->rssi, ard->ext_rssi);
  227. break;
  228. default:
  229. /*
  230. * Bogus bandwidth info was received in descriptor,
  231. * so ignore this PHY error
  232. */
  233. DFS_STAT_INC(sc, bwinfo_discards);
  234. return false;
  235. }
  236. if (rssi == 0) {
  237. DFS_STAT_INC(sc, rssi_discards);
  238. return false;
  239. }
  240. /* convert duration to usecs */
  241. pe->width = dur_to_usecs(sc->sc_ah, dur);
  242. pe->rssi = rssi;
  243. DFS_STAT_INC(sc, pulses_detected);
  244. return true;
  245. }
  246. static void
  247. ath9k_dfs_process_radar_pulse(struct ath_softc *sc, struct pulse_event *pe)
  248. {
  249. struct dfs_pattern_detector *pd = sc->dfs_detector;
  250. DFS_STAT_INC(sc, pulses_processed);
  251. if (pd == NULL)
  252. return;
  253. if (!pd->add_pulse(pd, pe, NULL))
  254. return;
  255. DFS_STAT_INC(sc, radar_detected);
  256. ieee80211_radar_detected(sc->hw);
  257. }
  258. /*
  259. * DFS: check PHY-error for radar pulse and feed the detector
  260. */
  261. void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
  262. struct ath_rx_status *rs, u64 mactime)
  263. {
  264. struct ath_radar_data ard;
  265. u16 datalen;
  266. char *vdata_end;
  267. struct pulse_event pe;
  268. struct ath_hw *ah = sc->sc_ah;
  269. struct ath_common *common = ath9k_hw_common(ah);
  270. DFS_STAT_INC(sc, pulses_total);
  271. if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
  272. (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
  273. ath_dbg(common, DFS,
  274. "Error: rs_phyer=0x%x not a radar error\n",
  275. rs->rs_phyerr);
  276. DFS_STAT_INC(sc, pulses_no_dfs);
  277. return;
  278. }
  279. datalen = rs->rs_datalen;
  280. if (datalen == 0) {
  281. DFS_STAT_INC(sc, datalen_discards);
  282. return;
  283. }
  284. ard.rssi = rs->rs_rssi_ctl[0];
  285. ard.ext_rssi = rs->rs_rssi_ext[0];
  286. /*
  287. * hardware stores this as 8 bit signed value.
  288. * we will cap it at 0 if it is a negative number
  289. */
  290. if (ard.rssi & 0x80)
  291. ard.rssi = 0;
  292. if (ard.ext_rssi & 0x80)
  293. ard.ext_rssi = 0;
  294. vdata_end = data + datalen;
  295. ard.pulse_bw_info = vdata_end[-1];
  296. ard.pulse_length_ext = vdata_end[-2];
  297. ard.pulse_length_pri = vdata_end[-3];
  298. pe.freq = ah->curchan->channel;
  299. pe.ts = mactime;
  300. if (!ath9k_postprocess_radar_event(sc, &ard, &pe))
  301. return;
  302. if (pe.width > MIN_CHIRP_PULSE_WIDTH &&
  303. pe.width < MAX_CHIRP_PULSE_WIDTH) {
  304. bool is_ctl = !!(ard.pulse_bw_info & PRI_CH_RADAR_FOUND);
  305. bool is_ext = !!(ard.pulse_bw_info & EXT_CH_RADAR_FOUND);
  306. int clen = datalen - 3;
  307. pe.chirp = ath9k_check_chirping(sc, data, clen, is_ctl, is_ext);
  308. } else {
  309. pe.chirp = false;
  310. }
  311. ath_dbg(common, DFS,
  312. "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
  313. "width=%d, rssi=%d, delta_ts=%llu\n",
  314. ard.pulse_bw_info, pe.freq, pe.ts, pe.width, pe.rssi,
  315. pe.ts - sc->dfs_prev_pulse_ts);
  316. sc->dfs_prev_pulse_ts = pe.ts;
  317. if (ard.pulse_bw_info & PRI_CH_RADAR_FOUND)
  318. ath9k_dfs_process_radar_pulse(sc, &pe);
  319. if (IS_CHAN_HT40(ah->curchan) &&
  320. ard.pulse_bw_info & EXT_CH_RADAR_FOUND) {
  321. pe.freq += IS_CHAN_HT40PLUS(ah->curchan) ? 20 : -20;
  322. ath9k_dfs_process_radar_pulse(sc, &pe);
  323. }
  324. }
  325. #undef PRI_CH_RADAR_FOUND
  326. #undef EXT_CH_RADAR_FOUND