Path: blob/main/sys/contrib/dev/athk/ath10k/spectral.c
107935 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;386ssize_t ret;387388ret = kstrtoul_from_user(user_buf, count, 0, &val);389if (ret)390return ret;391392if (val > 255)393return -EINVAL;394395mutex_lock(&ar->conf_mutex);396ar->spectral.config.count = val;397mutex_unlock(&ar->conf_mutex);398399return count;400}401402static const struct file_operations fops_spectral_count = {403.read = read_file_spectral_count,404.write = write_file_spectral_count,405.open = simple_open,406.owner = THIS_MODULE,407.llseek = default_llseek,408};409410static ssize_t read_file_spectral_bins(struct file *file,411char __user *user_buf,412size_t count, loff_t *ppos)413{414struct ath10k *ar = file->private_data;415char buf[32];416unsigned int bins, fft_size, bin_scale;417size_t len;418419mutex_lock(&ar->conf_mutex);420421fft_size = ar->spectral.config.fft_size;422bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;423bins = 1 << (fft_size - bin_scale);424425mutex_unlock(&ar->conf_mutex);426427len = sprintf(buf, "%d\n", bins);428return simple_read_from_buffer(user_buf, count, ppos, buf, len);429}430431static ssize_t write_file_spectral_bins(struct file *file,432const char __user *user_buf,433size_t count, loff_t *ppos)434{435struct ath10k *ar = file->private_data;436unsigned long val;437ssize_t ret;438439ret = kstrtoul_from_user(user_buf, count, 0, &val);440if (ret)441return ret;442443if (val < 64 || val > SPECTRAL_ATH10K_MAX_NUM_BINS)444return -EINVAL;445446if (!is_power_of_2(val))447return -EINVAL;448449mutex_lock(&ar->conf_mutex);450ar->spectral.config.fft_size = ilog2(val);451ar->spectral.config.fft_size += WMI_SPECTRAL_BIN_SCALE_DEFAULT;452mutex_unlock(&ar->conf_mutex);453454return count;455}456457static const struct file_operations fops_spectral_bins = {458.read = read_file_spectral_bins,459.write = write_file_spectral_bins,460.open = simple_open,461.owner = THIS_MODULE,462.llseek = default_llseek,463};464465static struct dentry *create_buf_file_handler(const char *filename,466struct dentry *parent,467umode_t mode,468struct rchan_buf *buf,469int *is_global)470{471struct dentry *buf_file;472473buf_file = debugfs_create_file(filename, mode, parent, buf,474&relay_file_operations);475if (IS_ERR(buf_file))476return NULL;477478*is_global = 1;479return buf_file;480}481482static int remove_buf_file_handler(struct dentry *dentry)483{484debugfs_remove(dentry);485486return 0;487}488489static const struct rchan_callbacks rfs_spec_scan_cb = {490.create_buf_file = create_buf_file_handler,491.remove_buf_file = remove_buf_file_handler,492};493494int ath10k_spectral_start(struct ath10k *ar)495{496struct ath10k_vif *arvif;497498lockdep_assert_held(&ar->conf_mutex);499500list_for_each_entry(arvif, &ar->arvifs, list)501arvif->spectral_enabled = 0;502503ar->spectral.mode = SPECTRAL_DISABLED;504ar->spectral.config.count = WMI_SPECTRAL_COUNT_DEFAULT;505ar->spectral.config.fft_size = WMI_SPECTRAL_FFT_SIZE_DEFAULT;506507return 0;508}509510int ath10k_spectral_vif_stop(struct ath10k_vif *arvif)511{512if (!arvif->spectral_enabled)513return 0;514515return ath10k_spectral_scan_config(arvif->ar, SPECTRAL_DISABLED);516}517518int ath10k_spectral_create(struct ath10k *ar)519{520/* The buffer size covers whole channels in dual bands up to 128 bins.521* Scan with bigger than 128 bins needs to be run on single band each.522*/523ar->spectral.rfs_chan_spec_scan = relay_open("spectral_scan",524ar->debug.debugfs_phy,5251140, 2500,526&rfs_spec_scan_cb, NULL);527debugfs_create_file("spectral_scan_ctl",5280600,529ar->debug.debugfs_phy, ar,530&fops_spec_scan_ctl);531debugfs_create_file("spectral_count",5320600,533ar->debug.debugfs_phy, ar,534&fops_spectral_count);535debugfs_create_file("spectral_bins",5360600,537ar->debug.debugfs_phy, ar,538&fops_spectral_bins);539540return 0;541}542543void ath10k_spectral_destroy(struct ath10k *ar)544{545if (ar->spectral.rfs_chan_spec_scan) {546relay_close(ar->spectral.rfs_chan_spec_scan);547ar->spectral.rfs_chan_spec_scan = NULL;548}549}550551552