dfs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /* convert pulse duration to usecs, considering clock mode */
  31. static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
  32. {
  33. const u32 AR93X_NSECS_PER_DUR = 800;
  34. const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
  35. u32 nsecs;
  36. if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
  37. nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
  38. else
  39. nsecs = dur * AR93X_NSECS_PER_DUR;
  40. return (nsecs + 500) / 1000;
  41. }
  42. #define PRI_CH_RADAR_FOUND 0x01
  43. #define EXT_CH_RADAR_FOUND 0x02
  44. static bool
  45. ath9k_postprocess_radar_event(struct ath_softc *sc,
  46. struct ath_radar_data *ard,
  47. struct pulse_event *pe)
  48. {
  49. u8 rssi;
  50. u16 dur;
  51. ath_dbg(ath9k_hw_common(sc->sc_ah), DFS,
  52. "pulse_bw_info=0x%x, pri,ext len/rssi=(%u/%u, %u/%u)\n",
  53. ard->pulse_bw_info,
  54. ard->pulse_length_pri, ard->rssi,
  55. ard->pulse_length_ext, ard->ext_rssi);
  56. /*
  57. * Only the last 2 bits of the BW info are relevant, they indicate
  58. * which channel the radar was detected in.
  59. */
  60. ard->pulse_bw_info &= 0x03;
  61. switch (ard->pulse_bw_info) {
  62. case PRI_CH_RADAR_FOUND:
  63. /* radar in ctrl channel */
  64. dur = ard->pulse_length_pri;
  65. DFS_STAT_INC(sc, pri_phy_errors);
  66. /*
  67. * cannot use ctrl channel RSSI
  68. * if extension channel is stronger
  69. */
  70. rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
  71. break;
  72. case EXT_CH_RADAR_FOUND:
  73. /* radar in extension channel */
  74. dur = ard->pulse_length_ext;
  75. DFS_STAT_INC(sc, ext_phy_errors);
  76. /*
  77. * cannot use extension channel RSSI
  78. * if control channel is stronger
  79. */
  80. rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
  81. break;
  82. case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
  83. /*
  84. * Conducted testing, when pulse is on DC, both pri and ext
  85. * durations are reported to be same
  86. *
  87. * Radiated testing, when pulse is on DC, different pri and
  88. * ext durations are reported, so take the larger of the two
  89. */
  90. if (ard->pulse_length_ext >= ard->pulse_length_pri)
  91. dur = ard->pulse_length_ext;
  92. else
  93. dur = ard->pulse_length_pri;
  94. DFS_STAT_INC(sc, dc_phy_errors);
  95. /* when both are present use stronger one */
  96. rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
  97. break;
  98. default:
  99. /*
  100. * Bogus bandwidth info was received in descriptor,
  101. * so ignore this PHY error
  102. */
  103. DFS_STAT_INC(sc, bwinfo_discards);
  104. return false;
  105. }
  106. if (rssi == 0) {
  107. DFS_STAT_INC(sc, rssi_discards);
  108. return false;
  109. }
  110. /*
  111. * TODO: check chirping pulses
  112. * checks for chirping are dependent on the DFS regulatory domain
  113. * used, which is yet TBD
  114. */
  115. /* convert duration to usecs */
  116. pe->width = dur_to_usecs(sc->sc_ah, dur);
  117. pe->rssi = rssi;
  118. DFS_STAT_INC(sc, pulses_detected);
  119. return true;
  120. }
  121. #undef PRI_CH_RADAR_FOUND
  122. #undef EXT_CH_RADAR_FOUND
  123. /*
  124. * DFS: check PHY-error for radar pulse and feed the detector
  125. */
  126. void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
  127. struct ath_rx_status *rs, u64 mactime)
  128. {
  129. struct ath_radar_data ard;
  130. u16 datalen;
  131. char *vdata_end;
  132. struct pulse_event pe;
  133. struct ath_hw *ah = sc->sc_ah;
  134. struct ath_common *common = ath9k_hw_common(ah);
  135. DFS_STAT_INC(sc, pulses_total);
  136. if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
  137. (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
  138. ath_dbg(common, DFS,
  139. "Error: rs_phyer=0x%x not a radar error\n",
  140. rs->rs_phyerr);
  141. DFS_STAT_INC(sc, pulses_no_dfs);
  142. return;
  143. }
  144. datalen = rs->rs_datalen;
  145. if (datalen == 0) {
  146. DFS_STAT_INC(sc, datalen_discards);
  147. return;
  148. }
  149. ard.rssi = rs->rs_rssi_ctl0;
  150. ard.ext_rssi = rs->rs_rssi_ext0;
  151. /*
  152. * hardware stores this as 8 bit signed value.
  153. * we will cap it at 0 if it is a negative number
  154. */
  155. if (ard.rssi & 0x80)
  156. ard.rssi = 0;
  157. if (ard.ext_rssi & 0x80)
  158. ard.ext_rssi = 0;
  159. vdata_end = (char *)data + datalen;
  160. ard.pulse_bw_info = vdata_end[-1];
  161. ard.pulse_length_ext = vdata_end[-2];
  162. ard.pulse_length_pri = vdata_end[-3];
  163. pe.freq = ah->curchan->channel;
  164. pe.ts = mactime;
  165. if (ath9k_postprocess_radar_event(sc, &ard, &pe)) {
  166. struct dfs_pattern_detector *pd = sc->dfs_detector;
  167. static u64 last_ts;
  168. ath_dbg(common, DFS,
  169. "ath9k_dfs_process_phyerr: channel=%d, ts=%llu, "
  170. "width=%d, rssi=%d, delta_ts=%llu\n",
  171. pe.freq, pe.ts, pe.width, pe.rssi, pe.ts-last_ts);
  172. last_ts = pe.ts;
  173. DFS_STAT_INC(sc, pulses_processed);
  174. if (pd != NULL && pd->add_pulse(pd, &pe)) {
  175. DFS_STAT_INC(sc, radar_detected);
  176. /*
  177. * TODO: forward radar event to DFS management layer
  178. */
  179. }
  180. }
  181. }