Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2008-2011 Atheros Communications Inc.
0003  * Copyright (c) 2011 Neratec Solutions AG
0004  *
0005  * Permission to use, copy, modify, and/or distribute this software for any
0006  * purpose with or without fee is hereby granted, provided that the above
0007  * copyright notice and this permission notice appear in all copies.
0008  *
0009  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0010  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0011  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0012  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0013  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0014  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0015  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0016  */
0017 
0018 #include "hw.h"
0019 #include "hw-ops.h"
0020 #include "ath9k.h"
0021 #include "dfs.h"
0022 #include "dfs_debug.h"
0023 
0024 /* internal struct to pass radar data */
0025 struct ath_radar_data {
0026     u8 pulse_bw_info;
0027     u8 rssi;
0028     u8 ext_rssi;
0029     u8 pulse_length_ext;
0030     u8 pulse_length_pri;
0031 };
0032 
0033 /**** begin: CHIRP ************************************************************/
0034 
0035 /* min and max gradients for defined FCC chirping pulses, given by
0036  * - 20MHz chirp width over a pulse width of  50us
0037  * -  5MHz chirp width over a pulse width of 100us
0038  */
0039 static const int BIN_DELTA_MIN      = 1;
0040 static const int BIN_DELTA_MAX      = 10;
0041 
0042 /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
0043 #define NUM_DIFFS 3
0044 #define FFT_NUM_SAMPLES     (NUM_DIFFS + 1)
0045 
0046 /* Threshold for difference of delta peaks */
0047 static const int MAX_DIFF       = 2;
0048 
0049 /* width range to be checked for chirping */
0050 static const int MIN_CHIRP_PULSE_WIDTH  = 20;
0051 static const int MAX_CHIRP_PULSE_WIDTH  = 110;
0052 
0053 struct ath9k_dfs_fft_20 {
0054     u8 bin[28];
0055     u8 lower_bins[3];
0056 } __packed;
0057 struct ath9k_dfs_fft_40 {
0058     u8 bin[64];
0059     u8 lower_bins[3];
0060     u8 upper_bins[3];
0061 } __packed;
0062 
0063 static inline int fft_max_index(u8 *bins)
0064 {
0065     return (bins[2] & 0xfc) >> 2;
0066 }
0067 static inline int fft_max_magnitude(u8 *bins)
0068 {
0069     return (bins[0] & 0xc0) >> 6 | bins[1] << 2 | (bins[2] & 0x03) << 10;
0070 }
0071 static inline u8 fft_bitmap_weight(u8 *bins)
0072 {
0073     return bins[0] & 0x3f;
0074 }
0075 
0076 static int ath9k_get_max_index_ht40(struct ath9k_dfs_fft_40 *fft,
0077                     bool is_ctl, bool is_ext)
0078 {
0079     const int DFS_UPPER_BIN_OFFSET = 64;
0080     /* if detected radar on both channels, select the significant one */
0081     if (is_ctl && is_ext) {
0082         /* first check wether channels have 'strong' bins */
0083         is_ctl = fft_bitmap_weight(fft->lower_bins) != 0;
0084         is_ext = fft_bitmap_weight(fft->upper_bins) != 0;
0085 
0086         /* if still unclear, take higher magnitude */
0087         if (is_ctl && is_ext) {
0088             int mag_lower = fft_max_magnitude(fft->lower_bins);
0089             int mag_upper = fft_max_magnitude(fft->upper_bins);
0090             if (mag_upper > mag_lower)
0091                 is_ctl = false;
0092             else
0093                 is_ext = false;
0094         }
0095     }
0096     if (is_ctl)
0097         return fft_max_index(fft->lower_bins);
0098     return fft_max_index(fft->upper_bins) + DFS_UPPER_BIN_OFFSET;
0099 }
0100 static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
0101                  int datalen, bool is_ctl, bool is_ext)
0102 {
0103     int i;
0104     int max_bin[FFT_NUM_SAMPLES];
0105     struct ath_hw *ah = sc->sc_ah;
0106     struct ath_common *common = ath9k_hw_common(ah);
0107     int prev_delta;
0108 
0109     if (IS_CHAN_HT40(ah->curchan)) {
0110         struct ath9k_dfs_fft_40 *fft = (struct ath9k_dfs_fft_40 *) data;
0111         int num_fft_packets = datalen / sizeof(*fft);
0112         if (num_fft_packets == 0)
0113             return false;
0114 
0115         ath_dbg(common, DFS, "HT40: datalen=%d, num_fft_packets=%d\n",
0116             datalen, num_fft_packets);
0117         if (num_fft_packets < FFT_NUM_SAMPLES) {
0118             ath_dbg(common, DFS, "not enough packets for chirp\n");
0119             return false;
0120         }
0121         /* HW sometimes adds 2 garbage bytes in front of FFT samples */
0122         if ((datalen % sizeof(*fft)) == 2) {
0123             fft = (struct ath9k_dfs_fft_40 *) (data + 2);
0124             ath_dbg(common, DFS, "fixing datalen by 2\n");
0125         }
0126         if (IS_CHAN_HT40MINUS(ah->curchan))
0127             swap(is_ctl, is_ext);
0128 
0129         for (i = 0; i < FFT_NUM_SAMPLES; i++)
0130             max_bin[i] = ath9k_get_max_index_ht40(fft + i, is_ctl,
0131                                   is_ext);
0132     } else {
0133         struct ath9k_dfs_fft_20 *fft = (struct ath9k_dfs_fft_20 *) data;
0134         int num_fft_packets = datalen / sizeof(*fft);
0135         if (num_fft_packets == 0)
0136             return false;
0137         ath_dbg(common, DFS, "HT20: datalen=%d, num_fft_packets=%d\n",
0138             datalen, num_fft_packets);
0139         if (num_fft_packets < FFT_NUM_SAMPLES) {
0140             ath_dbg(common, DFS, "not enough packets for chirp\n");
0141             return false;
0142         }
0143         /* in ht20, this is a 6-bit signed number => shift it to 0 */
0144         for (i = 0; i < FFT_NUM_SAMPLES; i++)
0145             max_bin[i] = fft_max_index(fft[i].lower_bins) ^ 0x20;
0146     }
0147     ath_dbg(common, DFS, "bin_max = [%d, %d, %d, %d]\n",
0148         max_bin[0], max_bin[1], max_bin[2], max_bin[3]);
0149 
0150     /* Check for chirp attributes within specs
0151      * a) delta of adjacent max_bins is within range
0152      * b) delta of adjacent deltas are within tolerance
0153      */
0154     prev_delta = 0;
0155     for (i = 0; i < NUM_DIFFS; i++) {
0156         int ddelta = -1;
0157         int delta = max_bin[i + 1] - max_bin[i];
0158 
0159         /* ensure gradient is within valid range */
0160         if (abs(delta) < BIN_DELTA_MIN || abs(delta) > BIN_DELTA_MAX) {
0161             ath_dbg(common, DFS, "CHIRP: invalid delta %d "
0162                 "in sample %d\n", delta, i);
0163             return false;
0164         }
0165         if (i == 0)
0166             goto done;
0167         ddelta = delta - prev_delta;
0168         if (abs(ddelta) > MAX_DIFF) {
0169             ath_dbg(common, DFS, "CHIRP: ddelta %d too high\n",
0170                 ddelta);
0171             return false;
0172         }
0173 done:
0174         ath_dbg(common, DFS, "CHIRP - %d: delta=%d, ddelta=%d\n",
0175             i, delta, ddelta);
0176         prev_delta = delta;
0177     }
0178     return true;
0179 }
0180 /**** end: CHIRP **************************************************************/
0181 
0182 /* convert pulse duration to usecs, considering clock mode */
0183 static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
0184 {
0185     const u32 AR93X_NSECS_PER_DUR = 800;
0186     const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
0187     u32 nsecs;
0188 
0189     if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
0190         nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
0191     else
0192         nsecs = dur * AR93X_NSECS_PER_DUR;
0193 
0194     return (nsecs + 500) / 1000;
0195 }
0196 
0197 #define PRI_CH_RADAR_FOUND 0x01
0198 #define EXT_CH_RADAR_FOUND 0x02
0199 static bool
0200 ath9k_postprocess_radar_event(struct ath_softc *sc,
0201                   struct ath_radar_data *ard,
0202                   struct pulse_event *pe)
0203 {
0204     u8 rssi;
0205     u16 dur;
0206 
0207     /*
0208      * Only the last 2 bits of the BW info are relevant, they indicate
0209      * which channel the radar was detected in.
0210      */
0211     ard->pulse_bw_info &= 0x03;
0212 
0213     switch (ard->pulse_bw_info) {
0214     case PRI_CH_RADAR_FOUND:
0215         /* radar in ctrl channel */
0216         dur = ard->pulse_length_pri;
0217         DFS_STAT_INC(sc, pri_phy_errors);
0218         /*
0219          * cannot use ctrl channel RSSI
0220          * if extension channel is stronger
0221          */
0222         rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
0223         break;
0224     case EXT_CH_RADAR_FOUND:
0225         /* radar in extension channel */
0226         dur = ard->pulse_length_ext;
0227         DFS_STAT_INC(sc, ext_phy_errors);
0228         /*
0229          * cannot use extension channel RSSI
0230          * if control channel is stronger
0231          */
0232         rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
0233         break;
0234     case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
0235         /*
0236          * Conducted testing, when pulse is on DC, both pri and ext
0237          * durations are reported to be same
0238          *
0239          * Radiated testing, when pulse is on DC, different pri and
0240          * ext durations are reported, so take the larger of the two
0241          */
0242         if (ard->pulse_length_ext >= ard->pulse_length_pri)
0243             dur = ard->pulse_length_ext;
0244         else
0245             dur = ard->pulse_length_pri;
0246         DFS_STAT_INC(sc, dc_phy_errors);
0247 
0248         /* when both are present use stronger one */
0249         rssi = max(ard->rssi, ard->ext_rssi);
0250         break;
0251     default:
0252         /*
0253          * Bogus bandwidth info was received in descriptor,
0254          * so ignore this PHY error
0255          */
0256         DFS_STAT_INC(sc, bwinfo_discards);
0257         return false;
0258     }
0259 
0260     if (rssi == 0) {
0261         DFS_STAT_INC(sc, rssi_discards);
0262         return false;
0263     }
0264 
0265     /* convert duration to usecs */
0266     pe->width = dur_to_usecs(sc->sc_ah, dur);
0267     pe->rssi = rssi;
0268 
0269     DFS_STAT_INC(sc, pulses_detected);
0270     return true;
0271 }
0272 
0273 static void
0274 ath9k_dfs_process_radar_pulse(struct ath_softc *sc, struct pulse_event *pe)
0275 {
0276     struct dfs_pattern_detector *pd = sc->dfs_detector;
0277     DFS_STAT_INC(sc, pulses_processed);
0278     if (pd == NULL)
0279         return;
0280     if (!pd->add_pulse(pd, pe, NULL))
0281         return;
0282     DFS_STAT_INC(sc, radar_detected);
0283     ieee80211_radar_detected(sc->hw);
0284 }
0285 
0286 /*
0287  * DFS: check PHY-error for radar pulse and feed the detector
0288  */
0289 void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
0290                   struct ath_rx_status *rs, u64 mactime)
0291 {
0292     struct ath_radar_data ard;
0293     u16 datalen;
0294     char *vdata_end;
0295     struct pulse_event pe;
0296     struct ath_hw *ah = sc->sc_ah;
0297     struct ath_common *common = ath9k_hw_common(ah);
0298 
0299     DFS_STAT_INC(sc, pulses_total);
0300     if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
0301         (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
0302         ath_dbg(common, DFS,
0303             "Error: rs_phyer=0x%x not a radar error\n",
0304             rs->rs_phyerr);
0305         DFS_STAT_INC(sc, pulses_no_dfs);
0306         return;
0307     }
0308 
0309     datalen = rs->rs_datalen;
0310     if (datalen == 0) {
0311         DFS_STAT_INC(sc, datalen_discards);
0312         return;
0313     }
0314 
0315     ard.rssi = rs->rs_rssi_ctl[0];
0316     ard.ext_rssi = rs->rs_rssi_ext[0];
0317 
0318     /*
0319      * hardware stores this as 8 bit signed value.
0320      * we will cap it at 0 if it is a negative number
0321      */
0322     if (ard.rssi & 0x80)
0323         ard.rssi = 0;
0324     if (ard.ext_rssi & 0x80)
0325         ard.ext_rssi = 0;
0326 
0327     vdata_end = data + datalen;
0328     ard.pulse_bw_info = vdata_end[-1];
0329     ard.pulse_length_ext = vdata_end[-2];
0330     ard.pulse_length_pri = vdata_end[-3];
0331     pe.freq = ah->curchan->channel;
0332     pe.ts = mactime;
0333     if (!ath9k_postprocess_radar_event(sc, &ard, &pe))
0334         return;
0335 
0336     if (pe.width > MIN_CHIRP_PULSE_WIDTH &&
0337         pe.width < MAX_CHIRP_PULSE_WIDTH) {
0338         bool is_ctl = !!(ard.pulse_bw_info & PRI_CH_RADAR_FOUND);
0339         bool is_ext = !!(ard.pulse_bw_info & EXT_CH_RADAR_FOUND);
0340         int clen = datalen - 3;
0341         pe.chirp = ath9k_check_chirping(sc, data, clen, is_ctl, is_ext);
0342     } else {
0343         pe.chirp = false;
0344     }
0345 
0346     ath_dbg(common, DFS,
0347         "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
0348         "width=%d, rssi=%d, delta_ts=%llu\n",
0349         ard.pulse_bw_info, pe.freq, pe.ts, pe.width, pe.rssi,
0350         pe.ts - sc->dfs_prev_pulse_ts);
0351     sc->dfs_prev_pulse_ts = pe.ts;
0352     if (ard.pulse_bw_info & PRI_CH_RADAR_FOUND)
0353         ath9k_dfs_process_radar_pulse(sc, &pe);
0354     if (IS_CHAN_HT40(ah->curchan) &&
0355         ard.pulse_bw_info & EXT_CH_RADAR_FOUND) {
0356         pe.freq += IS_CHAN_HT40PLUS(ah->curchan) ? 20 : -20;
0357         ath9k_dfs_process_radar_pulse(sc, &pe);
0358     }
0359 }
0360 #undef PRI_CH_RADAR_FOUND
0361 #undef EXT_CH_RADAR_FOUND