dfs.c 5.1 KB

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