Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /*
0003  * Copyright (c) 2013-2017 Qualcomm Atheros, Inc.
0004  */
0005 
0006 #include <linux/relay.h>
0007 #include "core.h"
0008 #include "debug.h"
0009 #include "wmi-ops.h"
0010 
0011 static void send_fft_sample(struct ath10k *ar,
0012                 const struct fft_sample_tlv *fft_sample_tlv)
0013 {
0014     int length;
0015 
0016     if (!ar->spectral.rfs_chan_spec_scan)
0017         return;
0018 
0019     length = __be16_to_cpu(fft_sample_tlv->length) +
0020          sizeof(*fft_sample_tlv);
0021     relay_write(ar->spectral.rfs_chan_spec_scan, fft_sample_tlv, length);
0022 }
0023 
0024 static uint8_t get_max_exp(s8 max_index, u16 max_magnitude, size_t bin_len,
0025                u8 *data)
0026 {
0027     int dc_pos;
0028     u8 max_exp;
0029 
0030     dc_pos = bin_len / 2;
0031 
0032     /* peak index outside of bins */
0033     if (dc_pos < max_index || -dc_pos >= max_index)
0034         return 0;
0035 
0036     for (max_exp = 0; max_exp < 8; max_exp++) {
0037         if (data[dc_pos + max_index] == (max_magnitude >> max_exp))
0038             break;
0039     }
0040 
0041     /* max_exp not found */
0042     if (data[dc_pos + max_index] != (max_magnitude >> max_exp))
0043         return 0;
0044 
0045     return max_exp;
0046 }
0047 
0048 static inline size_t ath10k_spectral_fix_bin_size(struct ath10k *ar,
0049                           size_t bin_len)
0050 {
0051     /* some chipsets reports bin size as 2^n bytes + 'm' bytes in
0052      * report mode 2. First 2^n bytes carries inband tones and last
0053      * 'm' bytes carries band edge detection data mainly used in
0054      * radar detection purpose. Strip last 'm' bytes to make bin size
0055      * as a valid one. 'm' can take possible values of 4, 12.
0056      */
0057     if (!is_power_of_2(bin_len))
0058         bin_len -= ar->hw_params.spectral_bin_discard;
0059 
0060     return bin_len;
0061 }
0062 
0063 int ath10k_spectral_process_fft(struct ath10k *ar,
0064                 struct wmi_phyerr_ev_arg *phyerr,
0065                 const struct phyerr_fft_report *fftr,
0066                 size_t bin_len, u64 tsf)
0067 {
0068     struct fft_sample_ath10k *fft_sample;
0069     u8 buf[sizeof(*fft_sample) + SPECTRAL_ATH10K_MAX_NUM_BINS];
0070     u16 freq1, freq2, total_gain_db, base_pwr_db, length, peak_mag;
0071     u32 reg0, reg1;
0072     u8 chain_idx, *bins;
0073     int dc_pos;
0074 
0075     fft_sample = (struct fft_sample_ath10k *)&buf;
0076 
0077     bin_len = ath10k_spectral_fix_bin_size(ar, bin_len);
0078 
0079     if (bin_len < 64 || bin_len > SPECTRAL_ATH10K_MAX_NUM_BINS)
0080         return -EINVAL;
0081 
0082     reg0 = __le32_to_cpu(fftr->reg0);
0083     reg1 = __le32_to_cpu(fftr->reg1);
0084 
0085     length = sizeof(*fft_sample) - sizeof(struct fft_sample_tlv) + bin_len;
0086     fft_sample->tlv.type = ATH_FFT_SAMPLE_ATH10K;
0087     fft_sample->tlv.length = __cpu_to_be16(length);
0088 
0089     /* TODO: there might be a reason why the hardware reports 20/40/80 MHz,
0090      * but the results/plots suggest that its actually 22/44/88 MHz.
0091      */
0092     switch (phyerr->chan_width_mhz) {
0093     case 20:
0094         fft_sample->chan_width_mhz = 22;
0095         break;
0096     case 40:
0097         fft_sample->chan_width_mhz = 44;
0098         break;
0099     case 80:
0100         /* TODO: As experiments with an analogue sender and various
0101          * configurations (fft-sizes of 64/128/256 and 20/40/80 Mhz)
0102          * show, the particular configuration of 80 MHz/64 bins does
0103          * not match with the other samples at all. Until the reason
0104          * for that is found, don't report these samples.
0105          */
0106         if (bin_len == 64)
0107             return -EINVAL;
0108         fft_sample->chan_width_mhz = 88;
0109         break;
0110     default:
0111         fft_sample->chan_width_mhz = phyerr->chan_width_mhz;
0112     }
0113 
0114     fft_sample->relpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_RELPWR_DB);
0115     fft_sample->avgpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_AVGPWR_DB);
0116 
0117     peak_mag = MS(reg1, SEARCH_FFT_REPORT_REG1_PEAK_MAG);
0118     fft_sample->max_magnitude = __cpu_to_be16(peak_mag);
0119     fft_sample->max_index = MS(reg0, SEARCH_FFT_REPORT_REG0_PEAK_SIDX);
0120     fft_sample->rssi = phyerr->rssi_combined;
0121 
0122     total_gain_db = MS(reg0, SEARCH_FFT_REPORT_REG0_TOTAL_GAIN_DB);
0123     base_pwr_db = MS(reg0, SEARCH_FFT_REPORT_REG0_BASE_PWR_DB);
0124     fft_sample->total_gain_db = __cpu_to_be16(total_gain_db);
0125     fft_sample->base_pwr_db = __cpu_to_be16(base_pwr_db);
0126 
0127     freq1 = phyerr->freq1;
0128     freq2 = phyerr->freq2;
0129     fft_sample->freq1 = __cpu_to_be16(freq1);
0130     fft_sample->freq2 = __cpu_to_be16(freq2);
0131 
0132     chain_idx = MS(reg0, SEARCH_FFT_REPORT_REG0_FFT_CHN_IDX);
0133 
0134     fft_sample->noise = __cpu_to_be16(phyerr->nf_chains[chain_idx]);
0135 
0136     bins = (u8 *)fftr;
0137     bins += sizeof(*fftr) + ar->hw_params.spectral_bin_offset;
0138 
0139     fft_sample->tsf = __cpu_to_be64(tsf);
0140 
0141     /* max_exp has been directly reported by previous hardware (ath9k),
0142      * maybe its possible to get it by other means?
0143      */
0144     fft_sample->max_exp = get_max_exp(fft_sample->max_index, peak_mag,
0145                       bin_len, bins);
0146 
0147     memcpy(fft_sample->data, bins, bin_len);
0148 
0149     /* DC value (value in the middle) is the blind spot of the spectral
0150      * sample and invalid, interpolate it.
0151      */
0152     dc_pos = bin_len / 2;
0153     fft_sample->data[dc_pos] = (fft_sample->data[dc_pos + 1] +
0154                     fft_sample->data[dc_pos - 1]) / 2;
0155 
0156     send_fft_sample(ar, &fft_sample->tlv);
0157 
0158     return 0;
0159 }
0160 
0161 static struct ath10k_vif *ath10k_get_spectral_vdev(struct ath10k *ar)
0162 {
0163     struct ath10k_vif *arvif;
0164 
0165     lockdep_assert_held(&ar->conf_mutex);
0166 
0167     if (list_empty(&ar->arvifs))
0168         return NULL;
0169 
0170     /* if there already is a vif doing spectral, return that. */
0171     list_for_each_entry(arvif, &ar->arvifs, list)
0172         if (arvif->spectral_enabled)
0173             return arvif;
0174 
0175     /* otherwise, return the first vif. */
0176     return list_first_entry(&ar->arvifs, typeof(*arvif), list);
0177 }
0178 
0179 static int ath10k_spectral_scan_trigger(struct ath10k *ar)
0180 {
0181     struct ath10k_vif *arvif;
0182     int res;
0183     int vdev_id;
0184 
0185     lockdep_assert_held(&ar->conf_mutex);
0186 
0187     arvif = ath10k_get_spectral_vdev(ar);
0188     if (!arvif)
0189         return -ENODEV;
0190     vdev_id = arvif->vdev_id;
0191 
0192     if (ar->spectral.mode == SPECTRAL_DISABLED)
0193         return 0;
0194 
0195     res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
0196                           WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
0197                           WMI_SPECTRAL_ENABLE_CMD_ENABLE);
0198     if (res < 0)
0199         return res;
0200 
0201     res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
0202                           WMI_SPECTRAL_TRIGGER_CMD_TRIGGER,
0203                           WMI_SPECTRAL_ENABLE_CMD_ENABLE);
0204     if (res < 0)
0205         return res;
0206 
0207     return 0;
0208 }
0209 
0210 static int ath10k_spectral_scan_config(struct ath10k *ar,
0211                        enum ath10k_spectral_mode mode)
0212 {
0213     struct wmi_vdev_spectral_conf_arg arg;
0214     struct ath10k_vif *arvif;
0215     int vdev_id, count, res = 0;
0216 
0217     lockdep_assert_held(&ar->conf_mutex);
0218 
0219     arvif = ath10k_get_spectral_vdev(ar);
0220     if (!arvif)
0221         return -ENODEV;
0222 
0223     vdev_id = arvif->vdev_id;
0224 
0225     arvif->spectral_enabled = (mode != SPECTRAL_DISABLED);
0226     ar->spectral.mode = mode;
0227 
0228     res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
0229                           WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
0230                           WMI_SPECTRAL_ENABLE_CMD_DISABLE);
0231     if (res < 0) {
0232         ath10k_warn(ar, "failed to enable spectral scan: %d\n", res);
0233         return res;
0234     }
0235 
0236     if (mode == SPECTRAL_DISABLED)
0237         return 0;
0238 
0239     if (mode == SPECTRAL_BACKGROUND)
0240         count = WMI_SPECTRAL_COUNT_DEFAULT;
0241     else
0242         count = max_t(u8, 1, ar->spectral.config.count);
0243 
0244     arg.vdev_id = vdev_id;
0245     arg.scan_count = count;
0246     arg.scan_period = WMI_SPECTRAL_PERIOD_DEFAULT;
0247     arg.scan_priority = WMI_SPECTRAL_PRIORITY_DEFAULT;
0248     arg.scan_fft_size = ar->spectral.config.fft_size;
0249     arg.scan_gc_ena = WMI_SPECTRAL_GC_ENA_DEFAULT;
0250     arg.scan_restart_ena = WMI_SPECTRAL_RESTART_ENA_DEFAULT;
0251     arg.scan_noise_floor_ref = WMI_SPECTRAL_NOISE_FLOOR_REF_DEFAULT;
0252     arg.scan_init_delay = WMI_SPECTRAL_INIT_DELAY_DEFAULT;
0253     arg.scan_nb_tone_thr = WMI_SPECTRAL_NB_TONE_THR_DEFAULT;
0254     arg.scan_str_bin_thr = WMI_SPECTRAL_STR_BIN_THR_DEFAULT;
0255     arg.scan_wb_rpt_mode = WMI_SPECTRAL_WB_RPT_MODE_DEFAULT;
0256     arg.scan_rssi_rpt_mode = WMI_SPECTRAL_RSSI_RPT_MODE_DEFAULT;
0257     arg.scan_rssi_thr = WMI_SPECTRAL_RSSI_THR_DEFAULT;
0258     arg.scan_pwr_format = WMI_SPECTRAL_PWR_FORMAT_DEFAULT;
0259     arg.scan_rpt_mode = WMI_SPECTRAL_RPT_MODE_DEFAULT;
0260     arg.scan_bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
0261     arg.scan_dbm_adj = WMI_SPECTRAL_DBM_ADJ_DEFAULT;
0262     arg.scan_chn_mask = WMI_SPECTRAL_CHN_MASK_DEFAULT;
0263 
0264     res = ath10k_wmi_vdev_spectral_conf(ar, &arg);
0265     if (res < 0) {
0266         ath10k_warn(ar, "failed to configure spectral scan: %d\n", res);
0267         return res;
0268     }
0269 
0270     return 0;
0271 }
0272 
0273 static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
0274                        size_t count, loff_t *ppos)
0275 {
0276     struct ath10k *ar = file->private_data;
0277     char *mode = "";
0278     size_t len;
0279     enum ath10k_spectral_mode spectral_mode;
0280 
0281     mutex_lock(&ar->conf_mutex);
0282     spectral_mode = ar->spectral.mode;
0283     mutex_unlock(&ar->conf_mutex);
0284 
0285     switch (spectral_mode) {
0286     case SPECTRAL_DISABLED:
0287         mode = "disable";
0288         break;
0289     case SPECTRAL_BACKGROUND:
0290         mode = "background";
0291         break;
0292     case SPECTRAL_MANUAL:
0293         mode = "manual";
0294         break;
0295     }
0296 
0297     len = strlen(mode);
0298     return simple_read_from_buffer(user_buf, count, ppos, mode, len);
0299 }
0300 
0301 static ssize_t write_file_spec_scan_ctl(struct file *file,
0302                     const char __user *user_buf,
0303                     size_t count, loff_t *ppos)
0304 {
0305     struct ath10k *ar = file->private_data;
0306     char buf[32];
0307     ssize_t len;
0308     int res;
0309 
0310     len = min(count, sizeof(buf) - 1);
0311     if (copy_from_user(buf, user_buf, len))
0312         return -EFAULT;
0313 
0314     buf[len] = '\0';
0315 
0316     mutex_lock(&ar->conf_mutex);
0317 
0318     if (strncmp("trigger", buf, 7) == 0) {
0319         if (ar->spectral.mode == SPECTRAL_MANUAL ||
0320             ar->spectral.mode == SPECTRAL_BACKGROUND) {
0321             /* reset the configuration to adopt possibly changed
0322              * debugfs parameters
0323              */
0324             res = ath10k_spectral_scan_config(ar,
0325                               ar->spectral.mode);
0326             if (res < 0) {
0327                 ath10k_warn(ar, "failed to reconfigure spectral scan: %d\n",
0328                         res);
0329             }
0330             res = ath10k_spectral_scan_trigger(ar);
0331             if (res < 0) {
0332                 ath10k_warn(ar, "failed to trigger spectral scan: %d\n",
0333                         res);
0334             }
0335         } else {
0336             res = -EINVAL;
0337         }
0338     } else if (strncmp("background", buf, 10) == 0) {
0339         res = ath10k_spectral_scan_config(ar, SPECTRAL_BACKGROUND);
0340     } else if (strncmp("manual", buf, 6) == 0) {
0341         res = ath10k_spectral_scan_config(ar, SPECTRAL_MANUAL);
0342     } else if (strncmp("disable", buf, 7) == 0) {
0343         res = ath10k_spectral_scan_config(ar, SPECTRAL_DISABLED);
0344     } else {
0345         res = -EINVAL;
0346     }
0347 
0348     mutex_unlock(&ar->conf_mutex);
0349 
0350     if (res < 0)
0351         return res;
0352 
0353     return count;
0354 }
0355 
0356 static const struct file_operations fops_spec_scan_ctl = {
0357     .read = read_file_spec_scan_ctl,
0358     .write = write_file_spec_scan_ctl,
0359     .open = simple_open,
0360     .owner = THIS_MODULE,
0361     .llseek = default_llseek,
0362 };
0363 
0364 static ssize_t read_file_spectral_count(struct file *file,
0365                     char __user *user_buf,
0366                     size_t count, loff_t *ppos)
0367 {
0368     struct ath10k *ar = file->private_data;
0369     char buf[32];
0370     size_t len;
0371     u8 spectral_count;
0372 
0373     mutex_lock(&ar->conf_mutex);
0374     spectral_count = ar->spectral.config.count;
0375     mutex_unlock(&ar->conf_mutex);
0376 
0377     len = sprintf(buf, "%d\n", spectral_count);
0378     return simple_read_from_buffer(user_buf, count, ppos, buf, len);
0379 }
0380 
0381 static ssize_t write_file_spectral_count(struct file *file,
0382                      const char __user *user_buf,
0383                      size_t count, loff_t *ppos)
0384 {
0385     struct ath10k *ar = file->private_data;
0386     unsigned long val;
0387     char buf[32];
0388     ssize_t len;
0389 
0390     len = min(count, sizeof(buf) - 1);
0391     if (copy_from_user(buf, user_buf, len))
0392         return -EFAULT;
0393 
0394     buf[len] = '\0';
0395     if (kstrtoul(buf, 0, &val))
0396         return -EINVAL;
0397 
0398     if (val > 255)
0399         return -EINVAL;
0400 
0401     mutex_lock(&ar->conf_mutex);
0402     ar->spectral.config.count = val;
0403     mutex_unlock(&ar->conf_mutex);
0404 
0405     return count;
0406 }
0407 
0408 static const struct file_operations fops_spectral_count = {
0409     .read = read_file_spectral_count,
0410     .write = write_file_spectral_count,
0411     .open = simple_open,
0412     .owner = THIS_MODULE,
0413     .llseek = default_llseek,
0414 };
0415 
0416 static ssize_t read_file_spectral_bins(struct file *file,
0417                        char __user *user_buf,
0418                        size_t count, loff_t *ppos)
0419 {
0420     struct ath10k *ar = file->private_data;
0421     char buf[32];
0422     unsigned int bins, fft_size, bin_scale;
0423     size_t len;
0424 
0425     mutex_lock(&ar->conf_mutex);
0426 
0427     fft_size = ar->spectral.config.fft_size;
0428     bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
0429     bins = 1 << (fft_size - bin_scale);
0430 
0431     mutex_unlock(&ar->conf_mutex);
0432 
0433     len = sprintf(buf, "%d\n", bins);
0434     return simple_read_from_buffer(user_buf, count, ppos, buf, len);
0435 }
0436 
0437 static ssize_t write_file_spectral_bins(struct file *file,
0438                     const char __user *user_buf,
0439                     size_t count, loff_t *ppos)
0440 {
0441     struct ath10k *ar = file->private_data;
0442     unsigned long val;
0443     char buf[32];
0444     ssize_t len;
0445 
0446     len = min(count, sizeof(buf) - 1);
0447     if (copy_from_user(buf, user_buf, len))
0448         return -EFAULT;
0449 
0450     buf[len] = '\0';
0451     if (kstrtoul(buf, 0, &val))
0452         return -EINVAL;
0453 
0454     if (val < 64 || val > SPECTRAL_ATH10K_MAX_NUM_BINS)
0455         return -EINVAL;
0456 
0457     if (!is_power_of_2(val))
0458         return -EINVAL;
0459 
0460     mutex_lock(&ar->conf_mutex);
0461     ar->spectral.config.fft_size = ilog2(val);
0462     ar->spectral.config.fft_size += WMI_SPECTRAL_BIN_SCALE_DEFAULT;
0463     mutex_unlock(&ar->conf_mutex);
0464 
0465     return count;
0466 }
0467 
0468 static const struct file_operations fops_spectral_bins = {
0469     .read = read_file_spectral_bins,
0470     .write = write_file_spectral_bins,
0471     .open = simple_open,
0472     .owner = THIS_MODULE,
0473     .llseek = default_llseek,
0474 };
0475 
0476 static struct dentry *create_buf_file_handler(const char *filename,
0477                           struct dentry *parent,
0478                           umode_t mode,
0479                           struct rchan_buf *buf,
0480                           int *is_global)
0481 {
0482     struct dentry *buf_file;
0483 
0484     buf_file = debugfs_create_file(filename, mode, parent, buf,
0485                        &relay_file_operations);
0486     if (IS_ERR(buf_file))
0487         return NULL;
0488 
0489     *is_global = 1;
0490     return buf_file;
0491 }
0492 
0493 static int remove_buf_file_handler(struct dentry *dentry)
0494 {
0495     debugfs_remove(dentry);
0496 
0497     return 0;
0498 }
0499 
0500 static const struct rchan_callbacks rfs_spec_scan_cb = {
0501     .create_buf_file = create_buf_file_handler,
0502     .remove_buf_file = remove_buf_file_handler,
0503 };
0504 
0505 int ath10k_spectral_start(struct ath10k *ar)
0506 {
0507     struct ath10k_vif *arvif;
0508 
0509     lockdep_assert_held(&ar->conf_mutex);
0510 
0511     list_for_each_entry(arvif, &ar->arvifs, list)
0512         arvif->spectral_enabled = 0;
0513 
0514     ar->spectral.mode = SPECTRAL_DISABLED;
0515     ar->spectral.config.count = WMI_SPECTRAL_COUNT_DEFAULT;
0516     ar->spectral.config.fft_size = WMI_SPECTRAL_FFT_SIZE_DEFAULT;
0517 
0518     return 0;
0519 }
0520 
0521 int ath10k_spectral_vif_stop(struct ath10k_vif *arvif)
0522 {
0523     if (!arvif->spectral_enabled)
0524         return 0;
0525 
0526     return ath10k_spectral_scan_config(arvif->ar, SPECTRAL_DISABLED);
0527 }
0528 
0529 int ath10k_spectral_create(struct ath10k *ar)
0530 {
0531     /* The buffer size covers whole channels in dual bands up to 128 bins.
0532      * Scan with bigger than 128 bins needs to be run on single band each.
0533      */
0534     ar->spectral.rfs_chan_spec_scan = relay_open("spectral_scan",
0535                              ar->debug.debugfs_phy,
0536                              1140, 2500,
0537                              &rfs_spec_scan_cb, NULL);
0538     debugfs_create_file("spectral_scan_ctl",
0539                 0600,
0540                 ar->debug.debugfs_phy, ar,
0541                 &fops_spec_scan_ctl);
0542     debugfs_create_file("spectral_count",
0543                 0600,
0544                 ar->debug.debugfs_phy, ar,
0545                 &fops_spectral_count);
0546     debugfs_create_file("spectral_bins",
0547                 0600,
0548                 ar->debug.debugfs_phy, ar,
0549                 &fops_spectral_bins);
0550 
0551     return 0;
0552 }
0553 
0554 void ath10k_spectral_destroy(struct ath10k *ar)
0555 {
0556     if (ar->spectral.rfs_chan_spec_scan) {
0557         relay_close(ar->spectral.rfs_chan_spec_scan);
0558         ar->spectral.rfs_chan_spec_scan = NULL;
0559     }
0560 }