1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2005-2014, 2020-2021 Intel Corporation
4 * Copyright (C) 2016 Intel Deutschland GmbH
6 #include <linux/slab.h>
7 #include <linux/string.h>
8 #include <linux/export.h>
11 #include "iwl-phy-db.h"
12 #include "iwl-debug.h"
13 #include "iwl-op-mode.h"
14 #include "iwl-trans.h"
16 struct iwl_phy_db_entry {
22 * struct iwl_phy_db - stores phy configuration and calibration data.
24 * @cfg: phy configuration.
25 * @calib_nch: non channel specific calibration data.
26 * @n_group_papd: number of entries in papd channel group.
27 * @calib_ch_group_papd: calibration data related to papd channel group.
28 * @n_group_txp: number of entries in tx power channel group.
29 * @calib_ch_group_txp: calibration data related to tx power chanel group.
30 * @trans: transport layer
33 struct iwl_phy_db_entry cfg;
34 struct iwl_phy_db_entry calib_nch;
36 struct iwl_phy_db_entry *calib_ch_group_papd;
38 struct iwl_phy_db_entry *calib_ch_group_txp;
40 struct iwl_trans *trans;
43 enum iwl_phy_db_section_type {
47 IWL_PHY_DB_CALIB_CHG_PAPD,
48 IWL_PHY_DB_CALIB_CHG_TXP,
52 #define PHY_DB_CMD 0x6c
54 /* for parsing of tx power channel group data that comes from the firmware*/
55 struct iwl_phy_db_chg_txp {
57 __le16 max_channel_idx;
60 struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
62 struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
68 phy_db->trans = trans;
70 phy_db->n_group_txp = -1;
71 phy_db->n_group_papd = -1;
73 /* TODO: add default values of the phy db. */
76 IWL_EXPORT_SYMBOL(iwl_phy_db_init);
79 * get phy db section: returns a pointer to a phy db section specified by
80 * type and channel group id.
82 static struct iwl_phy_db_entry *
83 iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
84 enum iwl_phy_db_section_type type,
87 if (!phy_db || type >= IWL_PHY_DB_MAX)
93 case IWL_PHY_DB_CALIB_NCH:
94 return &phy_db->calib_nch;
95 case IWL_PHY_DB_CALIB_CHG_PAPD:
96 if (chg_id >= phy_db->n_group_papd)
98 return &phy_db->calib_ch_group_papd[chg_id];
99 case IWL_PHY_DB_CALIB_CHG_TXP:
100 if (chg_id >= phy_db->n_group_txp)
102 return &phy_db->calib_ch_group_txp[chg_id];
109 static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
110 enum iwl_phy_db_section_type type,
113 struct iwl_phy_db_entry *entry =
114 iwl_phy_db_get_section(phy_db, type, chg_id);
123 void iwl_phy_db_free(struct iwl_phy_db *phy_db)
130 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
131 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
133 for (i = 0; i < phy_db->n_group_papd; i++)
134 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
135 kfree(phy_db->calib_ch_group_papd);
137 for (i = 0; i < phy_db->n_group_txp; i++)
138 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
139 kfree(phy_db->calib_ch_group_txp);
143 IWL_EXPORT_SYMBOL(iwl_phy_db_free);
145 int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
146 struct iwl_rx_packet *pkt)
148 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
149 struct iwl_calib_res_notif_phy_db *phy_db_notif =
150 (struct iwl_calib_res_notif_phy_db *)pkt->data;
151 enum iwl_phy_db_section_type type;
153 struct iwl_phy_db_entry *entry;
156 if (pkt_len < sizeof(*phy_db_notif))
159 type = le16_to_cpu(phy_db_notif->type);
160 size = le16_to_cpu(phy_db_notif->length);
162 if (pkt_len < sizeof(*phy_db_notif) + size)
168 if (type == IWL_PHY_DB_CALIB_CHG_PAPD) {
169 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
170 if (phy_db && !phy_db->calib_ch_group_papd) {
172 * Firmware sends the largest index first, so we can use
173 * it to know how much we should allocate.
175 phy_db->calib_ch_group_papd = kcalloc(chg_id + 1,
176 sizeof(struct iwl_phy_db_entry),
178 if (!phy_db->calib_ch_group_papd)
180 phy_db->n_group_papd = chg_id + 1;
182 } else if (type == IWL_PHY_DB_CALIB_CHG_TXP) {
183 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
184 if (phy_db && !phy_db->calib_ch_group_txp) {
186 * Firmware sends the largest index first, so we can use
187 * it to know how much we should allocate.
189 phy_db->calib_ch_group_txp = kcalloc(chg_id + 1,
190 sizeof(struct iwl_phy_db_entry),
192 if (!phy_db->calib_ch_group_txp)
194 phy_db->n_group_txp = chg_id + 1;
198 entry = iwl_phy_db_get_section(phy_db, type, chg_id);
203 entry->data = kmemdup(phy_db_notif->data, size, GFP_ATOMIC);
211 IWL_DEBUG_INFO(phy_db->trans,
212 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
213 __func__, __LINE__, type, size);
217 IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
219 static int is_valid_channel(u16 ch_id)
222 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
223 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
224 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
229 static u8 ch_id_to_ch_index(u16 ch_id)
231 if (WARN_ON(!is_valid_channel(ch_id)))
237 return (ch_id + 20) / 4;
239 return (ch_id - 12) / 4;
240 return (ch_id - 13) / 4;
244 static u16 channel_id_to_papd(u16 ch_id)
246 if (WARN_ON(!is_valid_channel(ch_id)))
249 if (1 <= ch_id && ch_id <= 14)
251 if (36 <= ch_id && ch_id <= 64)
253 if (100 <= ch_id && ch_id <= 140)
258 static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
260 struct iwl_phy_db_chg_txp *txp_chg;
262 u8 ch_index = ch_id_to_ch_index(ch_id);
263 if (ch_index == 0xff)
266 for (i = 0; i < phy_db->n_group_txp; i++) {
267 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
271 * Looking for the first channel group that its max channel is
272 * higher then wanted channel.
274 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
280 int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
281 u32 type, u8 **data, u16 *size, u16 ch_id)
283 struct iwl_phy_db_entry *entry;
289 /* find wanted channel group */
290 if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
291 ch_group_id = channel_id_to_papd(ch_id);
292 else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
293 ch_group_id = channel_id_to_txp(phy_db, ch_id);
295 entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
302 IWL_DEBUG_INFO(phy_db->trans,
303 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
304 __func__, __LINE__, type, *size);
309 static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
310 u16 length, void *data)
312 struct iwl_phy_db_cmd phy_db_cmd;
313 struct iwl_host_cmd cmd = {
317 IWL_DEBUG_INFO(phy_db->trans,
318 "Sending PHY-DB hcmd of type %d, of length %d\n",
321 /* Set phy db cmd variables */
322 phy_db_cmd.type = cpu_to_le16(type);
323 phy_db_cmd.length = cpu_to_le16(length);
325 /* Set hcmd variables */
326 cmd.data[0] = &phy_db_cmd;
327 cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
330 cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
332 return iwl_trans_send_cmd(phy_db->trans, &cmd);
335 static int iwl_phy_db_send_all_channel_groups(
336 struct iwl_phy_db *phy_db,
337 enum iwl_phy_db_section_type type,
342 struct iwl_phy_db_entry *entry;
344 /* Send all the channel specific groups to operational fw */
345 for (i = 0; i < max_ch_groups; i++) {
346 entry = iwl_phy_db_get_section(phy_db,
355 /* Send the requested PHY DB section */
356 err = iwl_send_phy_db_cmd(phy_db,
361 IWL_ERR(phy_db->trans,
362 "Can't SEND phy_db section %d (%d), err %d\n",
367 IWL_DEBUG_INFO(phy_db->trans,
368 "Sent PHY_DB HCMD, type = %d num = %d\n",
375 int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
381 IWL_DEBUG_INFO(phy_db->trans,
382 "Sending phy db data and configuration to runtime image\n");
384 /* Send PHY DB CFG section */
385 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
388 IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
392 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
394 IWL_ERR(phy_db->trans,
395 "Cannot send HCMD of Phy DB cfg section\n");
399 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
402 IWL_ERR(phy_db->trans,
403 "Cannot get Phy DB non specific channel section\n");
407 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
409 IWL_ERR(phy_db->trans,
410 "Cannot send HCMD of Phy DB non specific channel section\n");
414 /* Send all the TXP channel specific data */
415 err = iwl_phy_db_send_all_channel_groups(phy_db,
416 IWL_PHY_DB_CALIB_CHG_PAPD,
417 phy_db->n_group_papd);
419 IWL_ERR(phy_db->trans,
420 "Cannot send channel specific PAPD groups\n");
424 /* Send all the TXP channel specific data */
425 err = iwl_phy_db_send_all_channel_groups(phy_db,
426 IWL_PHY_DB_CALIB_CHG_TXP,
427 phy_db->n_group_txp);
429 IWL_ERR(phy_db->trans,
430 "Cannot send channel specific TX power groups\n");
434 IWL_DEBUG_INFO(phy_db->trans,
435 "Finished sending phy db non channel data\n");
438 IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);