Path: blob/main/sys/contrib/dev/iwlwifi/iwl-phy-db.c
48253 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2005-2014, 2020-2021 Intel Corporation3* Copyright (C) 2016 Intel Deutschland GmbH4*/5#include <linux/slab.h>6#include <linux/string.h>7#include <linux/export.h>89#include "iwl-drv.h"10#include "iwl-phy-db.h"11#include "iwl-debug.h"12#include "iwl-op-mode.h"13#include "iwl-trans.h"1415struct iwl_phy_db_entry {16u16 size;17u8 *data;18};1920/**21* struct iwl_phy_db - stores phy configuration and calibration data.22*23* @cfg: phy configuration.24* @calib_nch: non channel specific calibration data.25* @n_group_papd: number of entries in papd channel group.26* @calib_ch_group_papd: calibration data related to papd channel group.27* @n_group_txp: number of entries in tx power channel group.28* @calib_ch_group_txp: calibration data related to tx power chanel group.29* @trans: transport layer30*/31struct iwl_phy_db {32struct iwl_phy_db_entry cfg;33struct iwl_phy_db_entry calib_nch;34int n_group_papd;35struct iwl_phy_db_entry *calib_ch_group_papd;36int n_group_txp;37struct iwl_phy_db_entry *calib_ch_group_txp;3839struct iwl_trans *trans;40};4142enum iwl_phy_db_section_type {43IWL_PHY_DB_CFG = 1,44IWL_PHY_DB_CALIB_NCH,45IWL_PHY_DB_UNUSED,46IWL_PHY_DB_CALIB_CHG_PAPD,47IWL_PHY_DB_CALIB_CHG_TXP,48IWL_PHY_DB_MAX49};5051#define PHY_DB_CMD 0x6c5253/* for parsing of tx power channel group data that comes from the firmware*/54struct iwl_phy_db_chg_txp {55__le32 space;56__le16 max_channel_idx;57} __packed;5859struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)60{61struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),62GFP_KERNEL);6364if (!phy_db)65return phy_db;6667phy_db->trans = trans;6869phy_db->n_group_txp = -1;70phy_db->n_group_papd = -1;7172/* TODO: add default values of the phy db. */73return phy_db;74}75IWL_EXPORT_SYMBOL(iwl_phy_db_init);7677/*78* get phy db section: returns a pointer to a phy db section specified by79* type and channel group id.80*/81static struct iwl_phy_db_entry *82iwl_phy_db_get_section(struct iwl_phy_db *phy_db,83enum iwl_phy_db_section_type type,84u16 chg_id)85{86if (!phy_db || type >= IWL_PHY_DB_MAX)87return NULL;8889switch (type) {90case IWL_PHY_DB_CFG:91return &phy_db->cfg;92case IWL_PHY_DB_CALIB_NCH:93return &phy_db->calib_nch;94case IWL_PHY_DB_CALIB_CHG_PAPD:95if (chg_id >= phy_db->n_group_papd)96return NULL;97return &phy_db->calib_ch_group_papd[chg_id];98case IWL_PHY_DB_CALIB_CHG_TXP:99if (chg_id >= phy_db->n_group_txp)100return NULL;101return &phy_db->calib_ch_group_txp[chg_id];102default:103return NULL;104}105return NULL;106}107108static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,109enum iwl_phy_db_section_type type,110u16 chg_id)111{112struct iwl_phy_db_entry *entry =113iwl_phy_db_get_section(phy_db, type, chg_id);114if (!entry)115return;116117kfree(entry->data);118entry->data = NULL;119entry->size = 0;120}121122void iwl_phy_db_free(struct iwl_phy_db *phy_db)123{124int i;125126if (!phy_db)127return;128129iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);130iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);131132for (i = 0; i < phy_db->n_group_papd; i++)133iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);134kfree(phy_db->calib_ch_group_papd);135136for (i = 0; i < phy_db->n_group_txp; i++)137iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);138kfree(phy_db->calib_ch_group_txp);139140kfree(phy_db);141}142IWL_EXPORT_SYMBOL(iwl_phy_db_free);143144int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,145struct iwl_rx_packet *pkt)146{147unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);148struct iwl_calib_res_notif_phy_db *phy_db_notif =149(struct iwl_calib_res_notif_phy_db *)pkt->data;150enum iwl_phy_db_section_type type;151u16 size;152struct iwl_phy_db_entry *entry;153u16 chg_id = 0;154155if (pkt_len < sizeof(*phy_db_notif))156return -EINVAL;157158type = le16_to_cpu(phy_db_notif->type);159size = le16_to_cpu(phy_db_notif->length);160161if (pkt_len < sizeof(*phy_db_notif) + size)162return -EINVAL;163164if (!phy_db)165return -EINVAL;166167if (type == IWL_PHY_DB_CALIB_CHG_PAPD) {168chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);169if (phy_db && !phy_db->calib_ch_group_papd) {170/*171* Firmware sends the largest index first, so we can use172* it to know how much we should allocate.173*/174phy_db->calib_ch_group_papd = kcalloc(chg_id + 1,175sizeof(struct iwl_phy_db_entry),176GFP_ATOMIC);177if (!phy_db->calib_ch_group_papd)178return -ENOMEM;179phy_db->n_group_papd = chg_id + 1;180}181} else if (type == IWL_PHY_DB_CALIB_CHG_TXP) {182chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);183if (phy_db && !phy_db->calib_ch_group_txp) {184/*185* Firmware sends the largest index first, so we can use186* it to know how much we should allocate.187*/188phy_db->calib_ch_group_txp = kcalloc(chg_id + 1,189sizeof(struct iwl_phy_db_entry),190GFP_ATOMIC);191if (!phy_db->calib_ch_group_txp)192return -ENOMEM;193phy_db->n_group_txp = chg_id + 1;194}195}196197entry = iwl_phy_db_get_section(phy_db, type, chg_id);198if (!entry)199return -EINVAL;200201kfree(entry->data);202entry->data = kmemdup(phy_db_notif->data, size, GFP_ATOMIC);203if (!entry->data) {204entry->size = 0;205return -ENOMEM;206}207208entry->size = size;209210IWL_DEBUG_INFO(phy_db->trans,211"%s(%d): [PHYDB]SET: Type %d , Size: %d\n",212__func__, __LINE__, type, size);213214return 0;215}216IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);217218static int is_valid_channel(u16 ch_id)219{220if (ch_id <= 14 ||221(36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||222(100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||223(145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))224return 1;225return 0;226}227228static u8 ch_id_to_ch_index(u16 ch_id)229{230if (WARN_ON(!is_valid_channel(ch_id)))231return 0xff;232233if (ch_id <= 14)234return ch_id - 1;235if (ch_id <= 64)236return (ch_id + 20) / 4;237if (ch_id <= 140)238return (ch_id - 12) / 4;239return (ch_id - 13) / 4;240}241242243static u16 channel_id_to_papd(u16 ch_id)244{245if (WARN_ON(!is_valid_channel(ch_id)))246return 0xff;247248if (1 <= ch_id && ch_id <= 14)249return 0;250if (36 <= ch_id && ch_id <= 64)251return 1;252if (100 <= ch_id && ch_id <= 140)253return 2;254return 3;255}256257static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)258{259struct iwl_phy_db_chg_txp *txp_chg;260int i;261u8 ch_index = ch_id_to_ch_index(ch_id);262if (ch_index == 0xff)263return 0xff;264265for (i = 0; i < phy_db->n_group_txp; i++) {266txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;267if (!txp_chg)268return 0xff;269/*270* Looking for the first channel group that its max channel is271* higher then wanted channel.272*/273if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)274return i;275}276return 0xff;277}278static279int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,280u32 type, u8 **data, u16 *size, u16 ch_id)281{282struct iwl_phy_db_entry *entry;283u16 ch_group_id = 0;284285if (!phy_db)286return -EINVAL;287288/* find wanted channel group */289if (type == IWL_PHY_DB_CALIB_CHG_PAPD)290ch_group_id = channel_id_to_papd(ch_id);291else if (type == IWL_PHY_DB_CALIB_CHG_TXP)292ch_group_id = channel_id_to_txp(phy_db, ch_id);293294entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);295if (!entry)296return -EINVAL;297298*data = entry->data;299*size = entry->size;300301IWL_DEBUG_INFO(phy_db->trans,302"%s(%d): [PHYDB] GET: Type %d , Size: %d\n",303__func__, __LINE__, type, *size);304305return 0;306}307308static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,309u16 length, void *data)310{311struct iwl_phy_db_cmd phy_db_cmd;312struct iwl_host_cmd cmd = {313.id = PHY_DB_CMD,314};315316IWL_DEBUG_INFO(phy_db->trans,317"Sending PHY-DB hcmd of type %d, of length %d\n",318type, length);319320/* Set phy db cmd variables */321phy_db_cmd.type = cpu_to_le16(type);322phy_db_cmd.length = cpu_to_le16(length);323324/* Set hcmd variables */325cmd.data[0] = &phy_db_cmd;326cmd.len[0] = sizeof(struct iwl_phy_db_cmd);327cmd.data[1] = data;328cmd.len[1] = length;329cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;330331return iwl_trans_send_cmd(phy_db->trans, &cmd);332}333334static int iwl_phy_db_send_all_channel_groups(335struct iwl_phy_db *phy_db,336enum iwl_phy_db_section_type type,337u8 max_ch_groups)338{339u16 i;340int err;341struct iwl_phy_db_entry *entry;342343/* Send all the channel specific groups to operational fw */344for (i = 0; i < max_ch_groups; i++) {345entry = iwl_phy_db_get_section(phy_db,346type,347i);348if (!entry)349return -EINVAL;350351if (!entry->size)352continue;353354/* Send the requested PHY DB section */355err = iwl_send_phy_db_cmd(phy_db,356type,357entry->size,358entry->data);359if (err) {360IWL_ERR(phy_db->trans,361"Can't SEND phy_db section %d (%d), err %d\n",362type, i, err);363return err;364}365366IWL_DEBUG_INFO(phy_db->trans,367"Sent PHY_DB HCMD, type = %d num = %d\n",368type, i);369}370371return 0;372}373374int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)375{376u8 *data = NULL;377u16 size = 0;378int err;379380IWL_DEBUG_INFO(phy_db->trans,381"Sending phy db data and configuration to runtime image\n");382383/* Send PHY DB CFG section */384err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,385&data, &size, 0);386if (err) {387IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");388return err;389}390391err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);392if (err) {393IWL_ERR(phy_db->trans,394"Cannot send HCMD of Phy DB cfg section\n");395return err;396}397398err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,399&data, &size, 0);400if (err) {401IWL_ERR(phy_db->trans,402"Cannot get Phy DB non specific channel section\n");403return err;404}405406err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);407if (err) {408IWL_ERR(phy_db->trans,409"Cannot send HCMD of Phy DB non specific channel section\n");410return err;411}412413/* Send all the TXP channel specific data */414err = iwl_phy_db_send_all_channel_groups(phy_db,415IWL_PHY_DB_CALIB_CHG_PAPD,416phy_db->n_group_papd);417if (err) {418IWL_ERR(phy_db->trans,419"Cannot send channel specific PAPD groups\n");420return err;421}422423/* Send all the TXP channel specific data */424err = iwl_phy_db_send_all_channel_groups(phy_db,425IWL_PHY_DB_CALIB_CHG_TXP,426phy_db->n_group_txp);427if (err) {428IWL_ERR(phy_db->trans,429"Cannot send channel specific TX power groups\n");430return err;431}432433IWL_DEBUG_INFO(phy_db->trans,434"Finished sending phy db non channel data\n");435return 0;436}437IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);438439440