Path: blob/main/sys/contrib/dev/athk/ath10k/spectral.c
48375 views
// SPDX-License-Identifier: ISC1/*2* Copyright (c) 2013-2017 Qualcomm Atheros, Inc.3*/45#include <linux/relay.h>6#include "core.h"7#include "debug.h"8#include "wmi-ops.h"910static void send_fft_sample(struct ath10k *ar,11const struct fft_sample_tlv *fft_sample_tlv)12{13int length;1415if (!ar->spectral.rfs_chan_spec_scan)16return;1718length = __be16_to_cpu(fft_sample_tlv->length) +19sizeof(*fft_sample_tlv);20relay_write(ar->spectral.rfs_chan_spec_scan, fft_sample_tlv, length);21}2223static uint8_t get_max_exp(s8 max_index, u16 max_magnitude, size_t bin_len,24u8 *data)25{26int dc_pos;27u8 max_exp;2829dc_pos = bin_len / 2;3031/* peak index outside of bins */32if (dc_pos < max_index || -dc_pos >= max_index)33return 0;3435for (max_exp = 0; max_exp < 8; max_exp++) {36if (data[dc_pos + max_index] == (max_magnitude >> max_exp))37break;38}3940/* max_exp not found */41if (data[dc_pos + max_index] != (max_magnitude >> max_exp))42return 0;4344return max_exp;45}4647static inline size_t ath10k_spectral_fix_bin_size(struct ath10k *ar,48size_t bin_len)49{50/* some chipsets reports bin size as 2^n bytes + 'm' bytes in51* report mode 2. First 2^n bytes carries inband tones and last52* 'm' bytes carries band edge detection data mainly used in53* radar detection purpose. Strip last 'm' bytes to make bin size54* as a valid one. 'm' can take possible values of 4, 12.55*/56if (!is_power_of_2(bin_len))57bin_len -= ar->hw_params.spectral_bin_discard;5859return bin_len;60}6162int ath10k_spectral_process_fft(struct ath10k *ar,63struct wmi_phyerr_ev_arg *phyerr,64const struct phyerr_fft_report *fftr,65size_t bin_len, u64 tsf)66{67struct fft_sample_ath10k *fft_sample;68u8 buf[sizeof(*fft_sample) + SPECTRAL_ATH10K_MAX_NUM_BINS];69u16 freq1, freq2, total_gain_db, base_pwr_db, length, peak_mag;70u32 reg0, reg1;71u8 chain_idx, *bins;72int dc_pos;7374fft_sample = (struct fft_sample_ath10k *)&buf;7576bin_len = ath10k_spectral_fix_bin_size(ar, bin_len);7778if (bin_len < 64 || bin_len > SPECTRAL_ATH10K_MAX_NUM_BINS)79return -EINVAL;8081reg0 = __le32_to_cpu(fftr->reg0);82reg1 = __le32_to_cpu(fftr->reg1);8384length = sizeof(*fft_sample) - sizeof(struct fft_sample_tlv) + bin_len;85fft_sample->tlv.type = ATH_FFT_SAMPLE_ATH10K;86fft_sample->tlv.length = __cpu_to_be16(length);8788/* TODO: there might be a reason why the hardware reports 20/40/80 MHz,89* but the results/plots suggest that its actually 22/44/88 MHz.90*/91switch (phyerr->chan_width_mhz) {92case 20:93fft_sample->chan_width_mhz = 22;94break;95case 40:96fft_sample->chan_width_mhz = 44;97break;98case 80:99/* TODO: As experiments with an analogue sender and various100* configurations (fft-sizes of 64/128/256 and 20/40/80 Mhz)101* show, the particular configuration of 80 MHz/64 bins does102* not match with the other samples at all. Until the reason103* for that is found, don't report these samples.104*/105if (bin_len == 64)106return -EINVAL;107fft_sample->chan_width_mhz = 88;108break;109default:110fft_sample->chan_width_mhz = phyerr->chan_width_mhz;111}112113fft_sample->relpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_RELPWR_DB);114fft_sample->avgpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_AVGPWR_DB);115116peak_mag = MS(reg1, SEARCH_FFT_REPORT_REG1_PEAK_MAG);117fft_sample->max_magnitude = __cpu_to_be16(peak_mag);118fft_sample->max_index = MS(reg0, SEARCH_FFT_REPORT_REG0_PEAK_SIDX);119fft_sample->rssi = phyerr->rssi_combined;120121total_gain_db = MS(reg0, SEARCH_FFT_REPORT_REG0_TOTAL_GAIN_DB);122base_pwr_db = MS(reg0, SEARCH_FFT_REPORT_REG0_BASE_PWR_DB);123fft_sample->total_gain_db = __cpu_to_be16(total_gain_db);124fft_sample->base_pwr_db = __cpu_to_be16(base_pwr_db);125126freq1 = phyerr->freq1;127freq2 = phyerr->freq2;128fft_sample->freq1 = __cpu_to_be16(freq1);129fft_sample->freq2 = __cpu_to_be16(freq2);130131chain_idx = MS(reg0, SEARCH_FFT_REPORT_REG0_FFT_CHN_IDX);132133fft_sample->noise = __cpu_to_be16(phyerr->nf_chains[chain_idx]);134135bins = (u8 *)fftr;136bins += sizeof(*fftr) + ar->hw_params.spectral_bin_offset;137138fft_sample->tsf = __cpu_to_be64(tsf);139140/* max_exp has been directly reported by previous hardware (ath9k),141* maybe its possible to get it by other means?142*/143fft_sample->max_exp = get_max_exp(fft_sample->max_index, peak_mag,144bin_len, bins);145146memcpy(fft_sample->data, bins, bin_len);147148/* DC value (value in the middle) is the blind spot of the spectral149* sample and invalid, interpolate it.150*/151dc_pos = bin_len / 2;152fft_sample->data[dc_pos] = (fft_sample->data[dc_pos + 1] +153fft_sample->data[dc_pos - 1]) / 2;154155send_fft_sample(ar, &fft_sample->tlv);156157return 0;158}159160static struct ath10k_vif *ath10k_get_spectral_vdev(struct ath10k *ar)161{162struct ath10k_vif *arvif;163164lockdep_assert_held(&ar->conf_mutex);165166if (list_empty(&ar->arvifs))167return NULL;168169/* if there already is a vif doing spectral, return that. */170list_for_each_entry(arvif, &ar->arvifs, list)171if (arvif->spectral_enabled)172return arvif;173174/* otherwise, return the first vif. */175return list_first_entry(&ar->arvifs, typeof(*arvif), list);176}177178static int ath10k_spectral_scan_trigger(struct ath10k *ar)179{180struct ath10k_vif *arvif;181int res;182int vdev_id;183184lockdep_assert_held(&ar->conf_mutex);185186arvif = ath10k_get_spectral_vdev(ar);187if (!arvif)188return -ENODEV;189vdev_id = arvif->vdev_id;190191if (ar->spectral.mode == SPECTRAL_DISABLED)192return 0;193194res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,195WMI_SPECTRAL_TRIGGER_CMD_CLEAR,196WMI_SPECTRAL_ENABLE_CMD_ENABLE);197if (res < 0)198return res;199200res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,201WMI_SPECTRAL_TRIGGER_CMD_TRIGGER,202WMI_SPECTRAL_ENABLE_CMD_ENABLE);203if (res < 0)204return res;205206return 0;207}208209static int ath10k_spectral_scan_config(struct ath10k *ar,210enum ath10k_spectral_mode mode)211{212struct wmi_vdev_spectral_conf_arg arg;213struct ath10k_vif *arvif;214int vdev_id, count, res = 0;215216lockdep_assert_held(&ar->conf_mutex);217218arvif = ath10k_get_spectral_vdev(ar);219if (!arvif)220return -ENODEV;221222vdev_id = arvif->vdev_id;223224arvif->spectral_enabled = (mode != SPECTRAL_DISABLED);225ar->spectral.mode = mode;226227res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,228WMI_SPECTRAL_TRIGGER_CMD_CLEAR,229WMI_SPECTRAL_ENABLE_CMD_DISABLE);230if (res < 0) {231ath10k_warn(ar, "failed to enable spectral scan: %d\n", res);232return res;233}234235if (mode == SPECTRAL_DISABLED)236return 0;237238if (mode == SPECTRAL_BACKGROUND)239count = WMI_SPECTRAL_COUNT_DEFAULT;240else241count = max_t(u8, 1, ar->spectral.config.count);242243arg.vdev_id = vdev_id;244arg.scan_count = count;245arg.scan_period = WMI_SPECTRAL_PERIOD_DEFAULT;246arg.scan_priority = WMI_SPECTRAL_PRIORITY_DEFAULT;247arg.scan_fft_size = ar->spectral.config.fft_size;248arg.scan_gc_ena = WMI_SPECTRAL_GC_ENA_DEFAULT;249arg.scan_restart_ena = WMI_SPECTRAL_RESTART_ENA_DEFAULT;250arg.scan_noise_floor_ref = WMI_SPECTRAL_NOISE_FLOOR_REF_DEFAULT;251arg.scan_init_delay = WMI_SPECTRAL_INIT_DELAY_DEFAULT;252arg.scan_nb_tone_thr = WMI_SPECTRAL_NB_TONE_THR_DEFAULT;253arg.scan_str_bin_thr = WMI_SPECTRAL_STR_BIN_THR_DEFAULT;254arg.scan_wb_rpt_mode = WMI_SPECTRAL_WB_RPT_MODE_DEFAULT;255arg.scan_rssi_rpt_mode = WMI_SPECTRAL_RSSI_RPT_MODE_DEFAULT;256arg.scan_rssi_thr = WMI_SPECTRAL_RSSI_THR_DEFAULT;257arg.scan_pwr_format = WMI_SPECTRAL_PWR_FORMAT_DEFAULT;258arg.scan_rpt_mode = WMI_SPECTRAL_RPT_MODE_DEFAULT;259arg.scan_bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;260arg.scan_dbm_adj = WMI_SPECTRAL_DBM_ADJ_DEFAULT;261arg.scan_chn_mask = WMI_SPECTRAL_CHN_MASK_DEFAULT;262263res = ath10k_wmi_vdev_spectral_conf(ar, &arg);264if (res < 0) {265ath10k_warn(ar, "failed to configure spectral scan: %d\n", res);266return res;267}268269return 0;270}271272static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,273size_t count, loff_t *ppos)274{275struct ath10k *ar = file->private_data;276char *mode = "";277size_t len;278enum ath10k_spectral_mode spectral_mode;279280mutex_lock(&ar->conf_mutex);281spectral_mode = ar->spectral.mode;282mutex_unlock(&ar->conf_mutex);283284switch (spectral_mode) {285case SPECTRAL_DISABLED:286mode = "disable";287break;288case SPECTRAL_BACKGROUND:289mode = "background";290break;291case SPECTRAL_MANUAL:292mode = "manual";293break;294}295296len = strlen(mode);297return simple_read_from_buffer(user_buf, count, ppos, mode, len);298}299300static ssize_t write_file_spec_scan_ctl(struct file *file,301const char __user *user_buf,302size_t count, loff_t *ppos)303{304struct ath10k *ar = file->private_data;305char buf[32];306ssize_t len;307int res;308309len = min(count, sizeof(buf) - 1);310if (copy_from_user(buf, user_buf, len))311return -EFAULT;312313buf[len] = '\0';314315mutex_lock(&ar->conf_mutex);316317if (strncmp("trigger", buf, 7) == 0) {318if (ar->spectral.mode == SPECTRAL_MANUAL ||319ar->spectral.mode == SPECTRAL_BACKGROUND) {320/* reset the configuration to adopt possibly changed321* debugfs parameters322*/323res = ath10k_spectral_scan_config(ar,324ar->spectral.mode);325if (res < 0) {326ath10k_warn(ar, "failed to reconfigure spectral scan: %d\n",327res);328}329res = ath10k_spectral_scan_trigger(ar);330if (res < 0) {331ath10k_warn(ar, "failed to trigger spectral scan: %d\n",332res);333}334} else {335res = -EINVAL;336}337} else if (strncmp("background", buf, 10) == 0) {338res = ath10k_spectral_scan_config(ar, SPECTRAL_BACKGROUND);339} else if (strncmp("manual", buf, 6) == 0) {340res = ath10k_spectral_scan_config(ar, SPECTRAL_MANUAL);341} else if (strncmp("disable", buf, 7) == 0) {342res = ath10k_spectral_scan_config(ar, SPECTRAL_DISABLED);343} else {344res = -EINVAL;345}346347mutex_unlock(&ar->conf_mutex);348349if (res < 0)350return res;351352return count;353}354355static const struct file_operations fops_spec_scan_ctl = {356.read = read_file_spec_scan_ctl,357.write = write_file_spec_scan_ctl,358.open = simple_open,359.owner = THIS_MODULE,360.llseek = default_llseek,361};362363static ssize_t read_file_spectral_count(struct file *file,364char __user *user_buf,365size_t count, loff_t *ppos)366{367struct ath10k *ar = file->private_data;368char buf[32];369size_t len;370u8 spectral_count;371372mutex_lock(&ar->conf_mutex);373spectral_count = ar->spectral.config.count;374mutex_unlock(&ar->conf_mutex);375376len = sprintf(buf, "%d\n", spectral_count);377return simple_read_from_buffer(user_buf, count, ppos, buf, len);378}379380static ssize_t write_file_spectral_count(struct file *file,381const char __user *user_buf,382size_t count, loff_t *ppos)383{384struct ath10k *ar = file->private_data;385unsigned long val;386char buf[32];387ssize_t len;388389len = min(count, sizeof(buf) - 1);390if (copy_from_user(buf, user_buf, len))391return -EFAULT;392393buf[len] = '\0';394if (kstrtoul(buf, 0, &val))395return -EINVAL;396397if (val > 255)398return -EINVAL;399400mutex_lock(&ar->conf_mutex);401ar->spectral.config.count = val;402mutex_unlock(&ar->conf_mutex);403404return count;405}406407static const struct file_operations fops_spectral_count = {408.read = read_file_spectral_count,409.write = write_file_spectral_count,410.open = simple_open,411.owner = THIS_MODULE,412.llseek = default_llseek,413};414415static ssize_t read_file_spectral_bins(struct file *file,416char __user *user_buf,417size_t count, loff_t *ppos)418{419struct ath10k *ar = file->private_data;420char buf[32];421unsigned int bins, fft_size, bin_scale;422size_t len;423424mutex_lock(&ar->conf_mutex);425426fft_size = ar->spectral.config.fft_size;427bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;428bins = 1 << (fft_size - bin_scale);429430mutex_unlock(&ar->conf_mutex);431432len = sprintf(buf, "%d\n", bins);433return simple_read_from_buffer(user_buf, count, ppos, buf, len);434}435436static ssize_t write_file_spectral_bins(struct file *file,437const char __user *user_buf,438size_t count, loff_t *ppos)439{440struct ath10k *ar = file->private_data;441unsigned long val;442char buf[32];443ssize_t len;444445len = min(count, sizeof(buf) - 1);446if (copy_from_user(buf, user_buf, len))447return -EFAULT;448449buf[len] = '\0';450if (kstrtoul(buf, 0, &val))451return -EINVAL;452453if (val < 64 || val > SPECTRAL_ATH10K_MAX_NUM_BINS)454return -EINVAL;455456if (!is_power_of_2(val))457return -EINVAL;458459mutex_lock(&ar->conf_mutex);460ar->spectral.config.fft_size = ilog2(val);461ar->spectral.config.fft_size += WMI_SPECTRAL_BIN_SCALE_DEFAULT;462mutex_unlock(&ar->conf_mutex);463464return count;465}466467static const struct file_operations fops_spectral_bins = {468.read = read_file_spectral_bins,469.write = write_file_spectral_bins,470.open = simple_open,471.owner = THIS_MODULE,472.llseek = default_llseek,473};474475static struct dentry *create_buf_file_handler(const char *filename,476struct dentry *parent,477umode_t mode,478struct rchan_buf *buf,479int *is_global)480{481struct dentry *buf_file;482483buf_file = debugfs_create_file(filename, mode, parent, buf,484&relay_file_operations);485if (IS_ERR(buf_file))486return NULL;487488*is_global = 1;489return buf_file;490}491492static int remove_buf_file_handler(struct dentry *dentry)493{494debugfs_remove(dentry);495496return 0;497}498499static const struct rchan_callbacks rfs_spec_scan_cb = {500.create_buf_file = create_buf_file_handler,501.remove_buf_file = remove_buf_file_handler,502};503504int ath10k_spectral_start(struct ath10k *ar)505{506struct ath10k_vif *arvif;507508lockdep_assert_held(&ar->conf_mutex);509510list_for_each_entry(arvif, &ar->arvifs, list)511arvif->spectral_enabled = 0;512513ar->spectral.mode = SPECTRAL_DISABLED;514ar->spectral.config.count = WMI_SPECTRAL_COUNT_DEFAULT;515ar->spectral.config.fft_size = WMI_SPECTRAL_FFT_SIZE_DEFAULT;516517return 0;518}519520int ath10k_spectral_vif_stop(struct ath10k_vif *arvif)521{522if (!arvif->spectral_enabled)523return 0;524525return ath10k_spectral_scan_config(arvif->ar, SPECTRAL_DISABLED);526}527528int ath10k_spectral_create(struct ath10k *ar)529{530/* The buffer size covers whole channels in dual bands up to 128 bins.531* Scan with bigger than 128 bins needs to be run on single band each.532*/533ar->spectral.rfs_chan_spec_scan = relay_open("spectral_scan",534ar->debug.debugfs_phy,5351140, 2500,536&rfs_spec_scan_cb, NULL);537debugfs_create_file("spectral_scan_ctl",5380600,539ar->debug.debugfs_phy, ar,540&fops_spec_scan_ctl);541debugfs_create_file("spectral_count",5420600,543ar->debug.debugfs_phy, ar,544&fops_spectral_count);545debugfs_create_file("spectral_bins",5460600,547ar->debug.debugfs_phy, ar,548&fops_spectral_bins);549550return 0;551}552553void ath10k_spectral_destroy(struct ath10k *ar)554{555if (ar->spectral.rfs_chan_spec_scan) {556relay_close(ar->spectral.rfs_chan_spec_scan);557ar->spectral.rfs_chan_spec_scan = NULL;558}559}560561562