1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2005-2014, 2020 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 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
18 struct iwl_phy_db_entry
{
24 * struct iwl_phy_db - stores phy configuration and calibration data.
26 * @cfg: phy configuration.
27 * @calib_nch: non channel specific calibration data.
28 * @n_group_papd: number of entries in papd channel group.
29 * @calib_ch_group_papd: calibration data related to papd channel group.
30 * @n_group_txp: number of entries in tx power channel group.
31 * @calib_ch_group_txp: calibration data related to tx power chanel group.
32 * @trans: transport layer
35 struct iwl_phy_db_entry cfg
;
36 struct iwl_phy_db_entry calib_nch
;
38 struct iwl_phy_db_entry
*calib_ch_group_papd
;
40 struct iwl_phy_db_entry
*calib_ch_group_txp
;
42 struct iwl_trans
*trans
;
45 enum iwl_phy_db_section_type
{
49 IWL_PHY_DB_CALIB_CHG_PAPD
,
50 IWL_PHY_DB_CALIB_CHG_TXP
,
54 #define PHY_DB_CMD 0x6c
56 /* for parsing of tx power channel group data that comes from the firmware*/
57 struct iwl_phy_db_chg_txp
{
59 __le16 max_channel_idx
;
62 struct iwl_phy_db
*iwl_phy_db_init(struct iwl_trans
*trans
)
64 struct iwl_phy_db
*phy_db
= kzalloc(sizeof(struct iwl_phy_db
),
70 phy_db
->trans
= trans
;
72 phy_db
->n_group_txp
= -1;
73 phy_db
->n_group_papd
= -1;
75 /* TODO: add default values of the phy db. */
78 IWL_EXPORT_SYMBOL(iwl_phy_db_init
);
81 * get phy db section: returns a pointer to a phy db section specified by
82 * type and channel group id.
84 static struct iwl_phy_db_entry
*
85 iwl_phy_db_get_section(struct iwl_phy_db
*phy_db
,
86 enum iwl_phy_db_section_type type
,
89 if (!phy_db
|| type
>= IWL_PHY_DB_MAX
)
95 case IWL_PHY_DB_CALIB_NCH
:
96 return &phy_db
->calib_nch
;
97 case IWL_PHY_DB_CALIB_CHG_PAPD
:
98 if (chg_id
>= phy_db
->n_group_papd
)
100 return &phy_db
->calib_ch_group_papd
[chg_id
];
101 case IWL_PHY_DB_CALIB_CHG_TXP
:
102 if (chg_id
>= phy_db
->n_group_txp
)
104 return &phy_db
->calib_ch_group_txp
[chg_id
];
111 static void iwl_phy_db_free_section(struct iwl_phy_db
*phy_db
,
112 enum iwl_phy_db_section_type type
,
115 struct iwl_phy_db_entry
*entry
=
116 iwl_phy_db_get_section(phy_db
, type
, chg_id
);
125 void iwl_phy_db_free(struct iwl_phy_db
*phy_db
)
132 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CFG
, 0);
133 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_NCH
, 0);
135 for (i
= 0; i
< phy_db
->n_group_papd
; i
++)
136 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_CHG_PAPD
, i
);
137 kfree(phy_db
->calib_ch_group_papd
);
139 for (i
= 0; i
< phy_db
->n_group_txp
; i
++)
140 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_CHG_TXP
, i
);
141 kfree(phy_db
->calib_ch_group_txp
);
145 IWL_EXPORT_SYMBOL(iwl_phy_db_free
);
147 int iwl_phy_db_set_section(struct iwl_phy_db
*phy_db
,
148 struct iwl_rx_packet
*pkt
)
150 unsigned int pkt_len
= iwl_rx_packet_payload_len(pkt
);
151 struct iwl_calib_res_notif_phy_db
*phy_db_notif
=
152 (struct iwl_calib_res_notif_phy_db
*)pkt
->data
;
153 enum iwl_phy_db_section_type type
;
155 struct iwl_phy_db_entry
*entry
;
158 if (pkt_len
< sizeof(*phy_db_notif
))
161 type
= le16_to_cpu(phy_db_notif
->type
);
162 size
= le16_to_cpu(phy_db_notif
->length
);
164 if (pkt_len
< sizeof(*phy_db_notif
) + size
)
170 if (type
== IWL_PHY_DB_CALIB_CHG_PAPD
) {
171 chg_id
= le16_to_cpup((__le16
*)phy_db_notif
->data
);
172 if (phy_db
&& !phy_db
->calib_ch_group_papd
) {
174 * Firmware sends the largest index first, so we can use
175 * it to know how much we should allocate.
177 phy_db
->calib_ch_group_papd
= kcalloc(chg_id
+ 1,
178 sizeof(struct iwl_phy_db_entry
),
180 if (!phy_db
->calib_ch_group_papd
)
182 phy_db
->n_group_papd
= chg_id
+ 1;
184 } else if (type
== IWL_PHY_DB_CALIB_CHG_TXP
) {
185 chg_id
= le16_to_cpup((__le16
*)phy_db_notif
->data
);
186 if (phy_db
&& !phy_db
->calib_ch_group_txp
) {
188 * Firmware sends the largest index first, so we can use
189 * it to know how much we should allocate.
191 phy_db
->calib_ch_group_txp
= kcalloc(chg_id
+ 1,
192 sizeof(struct iwl_phy_db_entry
),
194 if (!phy_db
->calib_ch_group_txp
)
196 phy_db
->n_group_txp
= chg_id
+ 1;
200 entry
= iwl_phy_db_get_section(phy_db
, type
, chg_id
);
205 entry
->data
= kmemdup(phy_db_notif
->data
, size
, GFP_ATOMIC
);
213 IWL_DEBUG_INFO(phy_db
->trans
,
214 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
215 __func__
, __LINE__
, type
, size
);
219 IWL_EXPORT_SYMBOL(iwl_phy_db_set_section
);
221 static int is_valid_channel(u16 ch_id
)
224 (36 <= ch_id
&& ch_id
<= 64 && ch_id
% 4 == 0) ||
225 (100 <= ch_id
&& ch_id
<= 140 && ch_id
% 4 == 0) ||
226 (145 <= ch_id
&& ch_id
<= 165 && ch_id
% 4 == 1))
231 static u8
ch_id_to_ch_index(u16 ch_id
)
233 if (WARN_ON(!is_valid_channel(ch_id
)))
239 return (ch_id
+ 20) / 4;
241 return (ch_id
- 12) / 4;
242 return (ch_id
- 13) / 4;
246 static u16
channel_id_to_papd(u16 ch_id
)
248 if (WARN_ON(!is_valid_channel(ch_id
)))
251 if (1 <= ch_id
&& ch_id
<= 14)
253 if (36 <= ch_id
&& ch_id
<= 64)
255 if (100 <= ch_id
&& ch_id
<= 140)
260 static u16
channel_id_to_txp(struct iwl_phy_db
*phy_db
, u16 ch_id
)
262 struct iwl_phy_db_chg_txp
*txp_chg
;
264 u8 ch_index
= ch_id_to_ch_index(ch_id
);
265 if (ch_index
== 0xff)
268 for (i
= 0; i
< phy_db
->n_group_txp
; i
++) {
269 txp_chg
= (void *)phy_db
->calib_ch_group_txp
[i
].data
;
273 * Looking for the first channel group that its max channel is
274 * higher then wanted channel.
276 if (le16_to_cpu(txp_chg
->max_channel_idx
) >= ch_index
)
282 int iwl_phy_db_get_section_data(struct iwl_phy_db
*phy_db
,
283 u32 type
, u8
**data
, u16
*size
, u16 ch_id
)
285 struct iwl_phy_db_entry
*entry
;
291 /* find wanted channel group */
292 if (type
== IWL_PHY_DB_CALIB_CHG_PAPD
)
293 ch_group_id
= channel_id_to_papd(ch_id
);
294 else if (type
== IWL_PHY_DB_CALIB_CHG_TXP
)
295 ch_group_id
= channel_id_to_txp(phy_db
, ch_id
);
297 entry
= iwl_phy_db_get_section(phy_db
, type
, ch_group_id
);
304 IWL_DEBUG_INFO(phy_db
->trans
,
305 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
306 __func__
, __LINE__
, type
, *size
);
311 static int iwl_send_phy_db_cmd(struct iwl_phy_db
*phy_db
, u16 type
,
312 u16 length
, void *data
)
314 struct iwl_phy_db_cmd phy_db_cmd
;
315 struct iwl_host_cmd cmd
= {
319 IWL_DEBUG_INFO(phy_db
->trans
,
320 "Sending PHY-DB hcmd of type %d, of length %d\n",
323 /* Set phy db cmd variables */
324 phy_db_cmd
.type
= cpu_to_le16(type
);
325 phy_db_cmd
.length
= cpu_to_le16(length
);
327 /* Set hcmd variables */
328 cmd
.data
[0] = &phy_db_cmd
;
329 cmd
.len
[0] = sizeof(struct iwl_phy_db_cmd
);
332 cmd
.dataflags
[1] = IWL_HCMD_DFL_NOCOPY
;
334 return iwl_trans_send_cmd(phy_db
->trans
, &cmd
);
337 static int iwl_phy_db_send_all_channel_groups(
338 struct iwl_phy_db
*phy_db
,
339 enum iwl_phy_db_section_type type
,
344 struct iwl_phy_db_entry
*entry
;
346 /* Send all the channel specific groups to operational fw */
347 for (i
= 0; i
< max_ch_groups
; i
++) {
348 entry
= iwl_phy_db_get_section(phy_db
,
357 /* Send the requested PHY DB section */
358 err
= iwl_send_phy_db_cmd(phy_db
,
363 IWL_ERR(phy_db
->trans
,
364 "Can't SEND phy_db section %d (%d), err %d\n",
369 IWL_DEBUG_INFO(phy_db
->trans
,
370 "Sent PHY_DB HCMD, type = %d num = %d\n",
377 int iwl_send_phy_db_data(struct iwl_phy_db
*phy_db
)
383 IWL_DEBUG_INFO(phy_db
->trans
,
384 "Sending phy db data and configuration to runtime image\n");
386 /* Send PHY DB CFG section */
387 err
= iwl_phy_db_get_section_data(phy_db
, IWL_PHY_DB_CFG
,
390 IWL_ERR(phy_db
->trans
, "Cannot get Phy DB cfg section\n");
394 err
= iwl_send_phy_db_cmd(phy_db
, IWL_PHY_DB_CFG
, size
, data
);
396 IWL_ERR(phy_db
->trans
,
397 "Cannot send HCMD of Phy DB cfg section\n");
401 err
= iwl_phy_db_get_section_data(phy_db
, IWL_PHY_DB_CALIB_NCH
,
404 IWL_ERR(phy_db
->trans
,
405 "Cannot get Phy DB non specific channel section\n");
409 err
= iwl_send_phy_db_cmd(phy_db
, IWL_PHY_DB_CALIB_NCH
, size
, data
);
411 IWL_ERR(phy_db
->trans
,
412 "Cannot send HCMD of Phy DB non specific channel section\n");
416 /* Send all the TXP channel specific data */
417 err
= iwl_phy_db_send_all_channel_groups(phy_db
,
418 IWL_PHY_DB_CALIB_CHG_PAPD
,
419 phy_db
->n_group_papd
);
421 IWL_ERR(phy_db
->trans
,
422 "Cannot send channel specific PAPD groups\n");
426 /* Send all the TXP channel specific data */
427 err
= iwl_phy_db_send_all_channel_groups(phy_db
,
428 IWL_PHY_DB_CALIB_CHG_TXP
,
429 phy_db
->n_group_txp
);
431 IWL_ERR(phy_db
->trans
,
432 "Cannot send channel specific TX power groups\n");
436 IWL_DEBUG_INFO(phy_db
->trans
,
437 "Finished sending phy db non channel data\n");
440 IWL_EXPORT_SYMBOL(iwl_send_phy_db_data
);