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 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <linuxwifi@intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *****************************************************************************/
65 #include <linux/slab.h>
66 #include <linux/string.h>
67 #include <linux/export.h>
70 #include "iwl-phy-db.h"
71 #include "iwl-debug.h"
72 #include "iwl-op-mode.h"
73 #include "iwl-trans.h"
75 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
77 struct iwl_phy_db_entry {
83 * struct iwl_phy_db - stores phy configuration and calibration data.
85 * @cfg: phy configuration.
86 * @calib_nch: non channel specific calibration data.
87 * @calib_ch: channel specific calibration data.
88 * @n_group_papd: number of entries in papd channel group.
89 * @calib_ch_group_papd: calibration data related to papd channel group.
90 * @n_group_txp: number of entries in tx power channel group.
91 * @calib_ch_group_txp: calibration data related to tx power chanel group.
94 struct iwl_phy_db_entry cfg;
95 struct iwl_phy_db_entry calib_nch;
97 struct iwl_phy_db_entry *calib_ch_group_papd;
99 struct iwl_phy_db_entry *calib_ch_group_txp;
101 struct iwl_trans *trans;
104 enum iwl_phy_db_section_type {
106 IWL_PHY_DB_CALIB_NCH,
108 IWL_PHY_DB_CALIB_CHG_PAPD,
109 IWL_PHY_DB_CALIB_CHG_TXP,
113 #define PHY_DB_CMD 0x6c
115 /* for parsing of tx power channel group data that comes from the firmware*/
116 struct iwl_phy_db_chg_txp {
118 __le16 max_channel_idx;
121 struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
123 struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
129 phy_db->trans = trans;
131 phy_db->n_group_txp = -1;
132 phy_db->n_group_papd = -1;
134 /* TODO: add default values of the phy db. */
137 IWL_EXPORT_SYMBOL(iwl_phy_db_init);
140 * get phy db section: returns a pointer to a phy db section specified by
141 * type and channel group id.
143 static struct iwl_phy_db_entry *
144 iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
145 enum iwl_phy_db_section_type type,
148 if (!phy_db || type >= IWL_PHY_DB_MAX)
154 case IWL_PHY_DB_CALIB_NCH:
155 return &phy_db->calib_nch;
156 case IWL_PHY_DB_CALIB_CHG_PAPD:
157 if (chg_id >= phy_db->n_group_papd)
159 return &phy_db->calib_ch_group_papd[chg_id];
160 case IWL_PHY_DB_CALIB_CHG_TXP:
161 if (chg_id >= phy_db->n_group_txp)
163 return &phy_db->calib_ch_group_txp[chg_id];
170 static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
171 enum iwl_phy_db_section_type type,
174 struct iwl_phy_db_entry *entry =
175 iwl_phy_db_get_section(phy_db, type, chg_id);
184 void iwl_phy_db_free(struct iwl_phy_db *phy_db)
191 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
192 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
194 for (i = 0; i < phy_db->n_group_papd; i++)
195 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
196 kfree(phy_db->calib_ch_group_papd);
198 for (i = 0; i < phy_db->n_group_txp; i++)
199 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
200 kfree(phy_db->calib_ch_group_txp);
204 IWL_EXPORT_SYMBOL(iwl_phy_db_free);
206 int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
207 struct iwl_rx_packet *pkt)
209 struct iwl_calib_res_notif_phy_db *phy_db_notif =
210 (struct iwl_calib_res_notif_phy_db *)pkt->data;
211 enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
212 u16 size = le16_to_cpu(phy_db_notif->length);
213 struct iwl_phy_db_entry *entry;
219 if (type == IWL_PHY_DB_CALIB_CHG_PAPD) {
220 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
221 if (phy_db && !phy_db->calib_ch_group_papd) {
223 * Firmware sends the largest index first, so we can use
224 * it to know how much we should allocate.
226 phy_db->calib_ch_group_papd = kcalloc(chg_id + 1,
227 sizeof(struct iwl_phy_db_entry),
229 if (!phy_db->calib_ch_group_papd)
231 phy_db->n_group_papd = chg_id + 1;
233 } else if (type == IWL_PHY_DB_CALIB_CHG_TXP) {
234 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
235 if (phy_db && !phy_db->calib_ch_group_txp) {
237 * Firmware sends the largest index first, so we can use
238 * it to know how much we should allocate.
240 phy_db->calib_ch_group_txp = kcalloc(chg_id + 1,
241 sizeof(struct iwl_phy_db_entry),
243 if (!phy_db->calib_ch_group_txp)
245 phy_db->n_group_txp = chg_id + 1;
249 entry = iwl_phy_db_get_section(phy_db, type, chg_id);
254 entry->data = kmemdup(phy_db_notif->data, size, GFP_ATOMIC);
262 IWL_DEBUG_INFO(phy_db->trans,
263 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
264 __func__, __LINE__, type, size);
268 IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
270 static int is_valid_channel(u16 ch_id)
273 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
274 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
275 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
280 static u8 ch_id_to_ch_index(u16 ch_id)
282 if (WARN_ON(!is_valid_channel(ch_id)))
288 return (ch_id + 20) / 4;
290 return (ch_id - 12) / 4;
291 return (ch_id - 13) / 4;
295 static u16 channel_id_to_papd(u16 ch_id)
297 if (WARN_ON(!is_valid_channel(ch_id)))
300 if (1 <= ch_id && ch_id <= 14)
302 if (36 <= ch_id && ch_id <= 64)
304 if (100 <= ch_id && ch_id <= 140)
309 static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
311 struct iwl_phy_db_chg_txp *txp_chg;
313 u8 ch_index = ch_id_to_ch_index(ch_id);
314 if (ch_index == 0xff)
317 for (i = 0; i < phy_db->n_group_txp; i++) {
318 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
322 * Looking for the first channel group that its max channel is
323 * higher then wanted channel.
325 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
331 int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
332 u32 type, u8 **data, u16 *size, u16 ch_id)
334 struct iwl_phy_db_entry *entry;
340 /* find wanted channel group */
341 if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
342 ch_group_id = channel_id_to_papd(ch_id);
343 else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
344 ch_group_id = channel_id_to_txp(phy_db, ch_id);
346 entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
353 IWL_DEBUG_INFO(phy_db->trans,
354 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
355 __func__, __LINE__, type, *size);
360 static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
361 u16 length, void *data)
363 struct iwl_phy_db_cmd phy_db_cmd;
364 struct iwl_host_cmd cmd = {
368 IWL_DEBUG_INFO(phy_db->trans,
369 "Sending PHY-DB hcmd of type %d, of length %d\n",
372 /* Set phy db cmd variables */
373 phy_db_cmd.type = cpu_to_le16(type);
374 phy_db_cmd.length = cpu_to_le16(length);
376 /* Set hcmd variables */
377 cmd.data[0] = &phy_db_cmd;
378 cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
381 cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
383 return iwl_trans_send_cmd(phy_db->trans, &cmd);
386 static int iwl_phy_db_send_all_channel_groups(
387 struct iwl_phy_db *phy_db,
388 enum iwl_phy_db_section_type type,
393 struct iwl_phy_db_entry *entry;
395 /* Send all the channel specific groups to operational fw */
396 for (i = 0; i < max_ch_groups; i++) {
397 entry = iwl_phy_db_get_section(phy_db,
406 /* Send the requested PHY DB section */
407 err = iwl_send_phy_db_cmd(phy_db,
412 IWL_ERR(phy_db->trans,
413 "Can't SEND phy_db section %d (%d), err %d\n",
418 IWL_DEBUG_INFO(phy_db->trans,
419 "Sent PHY_DB HCMD, type = %d num = %d\n",
426 int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
432 IWL_DEBUG_INFO(phy_db->trans,
433 "Sending phy db data and configuration to runtime image\n");
435 /* Send PHY DB CFG section */
436 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
439 IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
443 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
445 IWL_ERR(phy_db->trans,
446 "Cannot send HCMD of Phy DB cfg section\n");
450 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
453 IWL_ERR(phy_db->trans,
454 "Cannot get Phy DB non specific channel section\n");
458 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
460 IWL_ERR(phy_db->trans,
461 "Cannot send HCMD of Phy DB non specific channel section\n");
465 /* Send all the TXP channel specific data */
466 err = iwl_phy_db_send_all_channel_groups(phy_db,
467 IWL_PHY_DB_CALIB_CHG_PAPD,
468 phy_db->n_group_papd);
470 IWL_ERR(phy_db->trans,
471 "Cannot send channel specific PAPD groups\n");
475 /* Send all the TXP channel specific data */
476 err = iwl_phy_db_send_all_channel_groups(phy_db,
477 IWL_PHY_DB_CALIB_CHG_TXP,
478 phy_db->n_group_txp);
480 IWL_ERR(phy_db->trans,
481 "Cannot send channel specific TX power groups\n");
485 IWL_DEBUG_INFO(phy_db->trans,
486 "Finished sending phy db non channel data\n");
489 IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);