dfs.c 5.6 KB

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