1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2016 Intel Deutschland GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * The full GNU General Public License is included in this distribution
21 * in the file called COPYING.
23 * Contact Information:
24 * Intel Linux Wireless <linuxwifi@intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
29 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * * Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * * Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in
40 * the documentation and/or other materials provided with the
42 * * Neither the name Intel Corporation nor the names of its
43 * contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
49 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
50 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *****************************************************************************/
60 #include <linux/slab.h>
61 #include <linux/string.h>
62 #include <linux/export.h>
65 #include "iwl-phy-db.h"
66 #include "iwl-debug.h"
67 #include "iwl-op-mode.h"
68 #include "iwl-trans.h"
70 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
72 struct iwl_phy_db_entry
{
78 * struct iwl_phy_db - stores phy configuration and calibration data.
80 * @cfg: phy configuration.
81 * @calib_nch: non channel specific calibration data.
82 * @calib_ch: channel specific calibration data.
83 * @n_group_papd: number of entries in papd channel group.
84 * @calib_ch_group_papd: calibration data related to papd channel group.
85 * @n_group_txp: number of entries in tx power channel group.
86 * @calib_ch_group_txp: calibration data related to tx power chanel group.
89 struct iwl_phy_db_entry cfg
;
90 struct iwl_phy_db_entry calib_nch
;
92 struct iwl_phy_db_entry
*calib_ch_group_papd
;
94 struct iwl_phy_db_entry
*calib_ch_group_txp
;
96 struct iwl_trans
*trans
;
99 enum iwl_phy_db_section_type
{
101 IWL_PHY_DB_CALIB_NCH
,
103 IWL_PHY_DB_CALIB_CHG_PAPD
,
104 IWL_PHY_DB_CALIB_CHG_TXP
,
108 #define PHY_DB_CMD 0x6c
110 /* for parsing of tx power channel group data that comes from the firmware*/
111 struct iwl_phy_db_chg_txp
{
113 __le16 max_channel_idx
;
116 struct iwl_phy_db
*iwl_phy_db_init(struct iwl_trans
*trans
)
118 struct iwl_phy_db
*phy_db
= kzalloc(sizeof(struct iwl_phy_db
),
124 phy_db
->trans
= trans
;
126 phy_db
->n_group_txp
= -1;
127 phy_db
->n_group_papd
= -1;
129 /* TODO: add default values of the phy db. */
132 IWL_EXPORT_SYMBOL(iwl_phy_db_init
);
135 * get phy db section: returns a pointer to a phy db section specified by
136 * type and channel group id.
138 static struct iwl_phy_db_entry
*
139 iwl_phy_db_get_section(struct iwl_phy_db
*phy_db
,
140 enum iwl_phy_db_section_type type
,
143 if (!phy_db
|| type
>= IWL_PHY_DB_MAX
)
149 case IWL_PHY_DB_CALIB_NCH
:
150 return &phy_db
->calib_nch
;
151 case IWL_PHY_DB_CALIB_CHG_PAPD
:
152 if (chg_id
>= phy_db
->n_group_papd
)
154 return &phy_db
->calib_ch_group_papd
[chg_id
];
155 case IWL_PHY_DB_CALIB_CHG_TXP
:
156 if (chg_id
>= phy_db
->n_group_txp
)
158 return &phy_db
->calib_ch_group_txp
[chg_id
];
165 static void iwl_phy_db_free_section(struct iwl_phy_db
*phy_db
,
166 enum iwl_phy_db_section_type type
,
169 struct iwl_phy_db_entry
*entry
=
170 iwl_phy_db_get_section(phy_db
, type
, chg_id
);
179 void iwl_phy_db_free(struct iwl_phy_db
*phy_db
)
186 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CFG
, 0);
187 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_NCH
, 0);
189 for (i
= 0; i
< phy_db
->n_group_papd
; i
++)
190 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_CHG_PAPD
, i
);
191 kfree(phy_db
->calib_ch_group_papd
);
193 for (i
= 0; i
< phy_db
->n_group_txp
; i
++)
194 iwl_phy_db_free_section(phy_db
, IWL_PHY_DB_CALIB_CHG_TXP
, i
);
195 kfree(phy_db
->calib_ch_group_txp
);
199 IWL_EXPORT_SYMBOL(iwl_phy_db_free
);
201 int iwl_phy_db_set_section(struct iwl_phy_db
*phy_db
,
202 struct iwl_rx_packet
*pkt
)
204 struct iwl_calib_res_notif_phy_db
*phy_db_notif
=
205 (struct iwl_calib_res_notif_phy_db
*)pkt
->data
;
206 enum iwl_phy_db_section_type type
= le16_to_cpu(phy_db_notif
->type
);
207 u16 size
= le16_to_cpu(phy_db_notif
->length
);
208 struct iwl_phy_db_entry
*entry
;
214 if (type
== IWL_PHY_DB_CALIB_CHG_PAPD
) {
215 chg_id
= le16_to_cpup((__le16
*)phy_db_notif
->data
);
216 if (phy_db
&& !phy_db
->calib_ch_group_papd
) {
218 * Firmware sends the largest index first, so we can use
219 * it to know how much we should allocate.
221 phy_db
->calib_ch_group_papd
= kcalloc(chg_id
+ 1,
222 sizeof(struct iwl_phy_db_entry
),
224 if (!phy_db
->calib_ch_group_papd
)
226 phy_db
->n_group_papd
= chg_id
+ 1;
228 } else if (type
== IWL_PHY_DB_CALIB_CHG_TXP
) {
229 chg_id
= le16_to_cpup((__le16
*)phy_db_notif
->data
);
230 if (phy_db
&& !phy_db
->calib_ch_group_txp
) {
232 * Firmware sends the largest index first, so we can use
233 * it to know how much we should allocate.
235 phy_db
->calib_ch_group_txp
= kcalloc(chg_id
+ 1,
236 sizeof(struct iwl_phy_db_entry
),
238 if (!phy_db
->calib_ch_group_txp
)
240 phy_db
->n_group_txp
= chg_id
+ 1;
244 entry
= iwl_phy_db_get_section(phy_db
, type
, chg_id
);
249 entry
->data
= kmemdup(phy_db_notif
->data
, size
, GFP_ATOMIC
);
257 IWL_DEBUG_INFO(phy_db
->trans
,
258 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
259 __func__
, __LINE__
, type
, size
);
263 IWL_EXPORT_SYMBOL(iwl_phy_db_set_section
);
265 static int is_valid_channel(u16 ch_id
)
268 (36 <= ch_id
&& ch_id
<= 64 && ch_id
% 4 == 0) ||
269 (100 <= ch_id
&& ch_id
<= 140 && ch_id
% 4 == 0) ||
270 (145 <= ch_id
&& ch_id
<= 165 && ch_id
% 4 == 1))
275 static u8
ch_id_to_ch_index(u16 ch_id
)
277 if (WARN_ON(!is_valid_channel(ch_id
)))
283 return (ch_id
+ 20) / 4;
285 return (ch_id
- 12) / 4;
286 return (ch_id
- 13) / 4;
290 static u16
channel_id_to_papd(u16 ch_id
)
292 if (WARN_ON(!is_valid_channel(ch_id
)))
295 if (1 <= ch_id
&& ch_id
<= 14)
297 if (36 <= ch_id
&& ch_id
<= 64)
299 if (100 <= ch_id
&& ch_id
<= 140)
304 static u16
channel_id_to_txp(struct iwl_phy_db
*phy_db
, u16 ch_id
)
306 struct iwl_phy_db_chg_txp
*txp_chg
;
308 u8 ch_index
= ch_id_to_ch_index(ch_id
);
309 if (ch_index
== 0xff)
312 for (i
= 0; i
< phy_db
->n_group_txp
; i
++) {
313 txp_chg
= (void *)phy_db
->calib_ch_group_txp
[i
].data
;
317 * Looking for the first channel group that its max channel is
318 * higher then wanted channel.
320 if (le16_to_cpu(txp_chg
->max_channel_idx
) >= ch_index
)
326 int iwl_phy_db_get_section_data(struct iwl_phy_db
*phy_db
,
327 u32 type
, u8
**data
, u16
*size
, u16 ch_id
)
329 struct iwl_phy_db_entry
*entry
;
335 /* find wanted channel group */
336 if (type
== IWL_PHY_DB_CALIB_CHG_PAPD
)
337 ch_group_id
= channel_id_to_papd(ch_id
);
338 else if (type
== IWL_PHY_DB_CALIB_CHG_TXP
)
339 ch_group_id
= channel_id_to_txp(phy_db
, ch_id
);
341 entry
= iwl_phy_db_get_section(phy_db
, type
, ch_group_id
);
348 IWL_DEBUG_INFO(phy_db
->trans
,
349 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
350 __func__
, __LINE__
, type
, *size
);
355 static int iwl_send_phy_db_cmd(struct iwl_phy_db
*phy_db
, u16 type
,
356 u16 length
, void *data
)
358 struct iwl_phy_db_cmd phy_db_cmd
;
359 struct iwl_host_cmd cmd
= {
363 IWL_DEBUG_INFO(phy_db
->trans
,
364 "Sending PHY-DB hcmd of type %d, of length %d\n",
367 /* Set phy db cmd variables */
368 phy_db_cmd
.type
= cpu_to_le16(type
);
369 phy_db_cmd
.length
= cpu_to_le16(length
);
371 /* Set hcmd variables */
372 cmd
.data
[0] = &phy_db_cmd
;
373 cmd
.len
[0] = sizeof(struct iwl_phy_db_cmd
);
376 cmd
.dataflags
[1] = IWL_HCMD_DFL_NOCOPY
;
378 return iwl_trans_send_cmd(phy_db
->trans
, &cmd
);
381 static int iwl_phy_db_send_all_channel_groups(
382 struct iwl_phy_db
*phy_db
,
383 enum iwl_phy_db_section_type type
,
388 struct iwl_phy_db_entry
*entry
;
390 /* Send all the channel specific groups to operational fw */
391 for (i
= 0; i
< max_ch_groups
; i
++) {
392 entry
= iwl_phy_db_get_section(phy_db
,
401 /* Send the requested PHY DB section */
402 err
= iwl_send_phy_db_cmd(phy_db
,
407 IWL_ERR(phy_db
->trans
,
408 "Can't SEND phy_db section %d (%d), err %d\n",
413 IWL_DEBUG_INFO(phy_db
->trans
,
414 "Sent PHY_DB HCMD, type = %d num = %d\n",
421 int iwl_send_phy_db_data(struct iwl_phy_db
*phy_db
)
427 IWL_DEBUG_INFO(phy_db
->trans
,
428 "Sending phy db data and configuration to runtime image\n");
430 /* Send PHY DB CFG section */
431 err
= iwl_phy_db_get_section_data(phy_db
, IWL_PHY_DB_CFG
,
434 IWL_ERR(phy_db
->trans
, "Cannot get Phy DB cfg section\n");
438 err
= iwl_send_phy_db_cmd(phy_db
, IWL_PHY_DB_CFG
, size
, data
);
440 IWL_ERR(phy_db
->trans
,
441 "Cannot send HCMD of Phy DB cfg section\n");
445 err
= iwl_phy_db_get_section_data(phy_db
, IWL_PHY_DB_CALIB_NCH
,
448 IWL_ERR(phy_db
->trans
,
449 "Cannot get Phy DB non specific channel section\n");
453 err
= iwl_send_phy_db_cmd(phy_db
, IWL_PHY_DB_CALIB_NCH
, size
, data
);
455 IWL_ERR(phy_db
->trans
,
456 "Cannot send HCMD of Phy DB non specific channel section\n");
460 /* Send all the TXP channel specific data */
461 err
= iwl_phy_db_send_all_channel_groups(phy_db
,
462 IWL_PHY_DB_CALIB_CHG_PAPD
,
463 phy_db
->n_group_papd
);
465 IWL_ERR(phy_db
->trans
,
466 "Cannot send channel specific PAPD groups\n");
470 /* Send all the TXP channel specific data */
471 err
= iwl_phy_db_send_all_channel_groups(phy_db
,
472 IWL_PHY_DB_CALIB_CHG_TXP
,
473 phy_db
->n_group_txp
);
475 IWL_ERR(phy_db
->trans
,
476 "Cannot send channel specific TX power groups\n");
480 IWL_DEBUG_INFO(phy_db
->trans
,
481 "Finished sending phy db non channel data\n");
484 IWL_EXPORT_SYMBOL(iwl_send_phy_db_data
);