blob: 25745daa0d5da4142abef88f506b8bee614d8905 [file] [log] [blame]
Johannes Bergb1e1adf2013-01-24 14:14:22 +01001/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Bergb1e1adf2013-01-24 14:14:22 +010026 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
64#include <linux/slab.h>
65#include <linux/string.h>
66#include <linux/export.h>
67
Johannes Berg48e29342013-03-01 00:13:33 +010068#include "iwl-drv.h"
Johannes Bergb1e1adf2013-01-24 14:14:22 +010069#include "iwl-phy-db.h"
70#include "iwl-debug.h"
71#include "iwl-op-mode.h"
72#include "iwl-trans.h"
73
74#define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
75#define IWL_NUM_PAPD_CH_GROUPS 4
76#define IWL_NUM_TXP_CH_GROUPS 9
77
78struct iwl_phy_db_entry {
79 u16 size;
80 u8 *data;
81};
82
83/**
84 * struct iwl_phy_db - stores phy configuration and calibration data.
85 *
86 * @cfg: phy configuration.
87 * @calib_nch: non channel specific calibration data.
88 * @calib_ch: channel specific calibration data.
89 * @calib_ch_group_papd: calibration data related to papd channel group.
90 * @calib_ch_group_txp: calibration data related to tx power chanel group.
91 */
92struct iwl_phy_db {
93 struct iwl_phy_db_entry cfg;
94 struct iwl_phy_db_entry calib_nch;
95 struct iwl_phy_db_entry calib_ch;
96 struct iwl_phy_db_entry calib_ch_group_papd[IWL_NUM_PAPD_CH_GROUPS];
97 struct iwl_phy_db_entry calib_ch_group_txp[IWL_NUM_TXP_CH_GROUPS];
98
99 u32 channel_num;
100 u32 channel_size;
101
102 struct iwl_trans *trans;
103};
104
105enum iwl_phy_db_section_type {
106 IWL_PHY_DB_CFG = 1,
107 IWL_PHY_DB_CALIB_NCH,
108 IWL_PHY_DB_CALIB_CH,
109 IWL_PHY_DB_CALIB_CHG_PAPD,
110 IWL_PHY_DB_CALIB_CHG_TXP,
111 IWL_PHY_DB_MAX
112};
113
114#define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */
115
116/*
117 * phy db - configure operational ucode
118 */
119struct iwl_phy_db_cmd {
120 __le16 type;
121 __le16 length;
122 u8 data[];
123} __packed;
124
125/* for parsing of tx power channel group data that comes from the firmware*/
126struct iwl_phy_db_chg_txp {
127 __le32 space;
128 __le16 max_channel_idx;
129} __packed;
130
131/*
132 * phy db - Receieve phy db chunk after calibrations
133 */
134struct iwl_calib_res_notif_phy_db {
135 __le16 type;
136 __le16 length;
137 u8 data[];
138} __packed;
139
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100140struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
141{
142 struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
143 GFP_KERNEL);
144
145 if (!phy_db)
146 return phy_db;
147
148 phy_db->trans = trans;
149
150 /* TODO: add default values of the phy db. */
151 return phy_db;
152}
Johannes Berg48e29342013-03-01 00:13:33 +0100153IWL_EXPORT_SYMBOL(iwl_phy_db_init);
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100154
155/*
156 * get phy db section: returns a pointer to a phy db section specified by
157 * type and channel group id.
158 */
159static struct iwl_phy_db_entry *
160iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
161 enum iwl_phy_db_section_type type,
162 u16 chg_id)
163{
164 if (!phy_db || type >= IWL_PHY_DB_MAX)
165 return NULL;
166
167 switch (type) {
168 case IWL_PHY_DB_CFG:
169 return &phy_db->cfg;
170 case IWL_PHY_DB_CALIB_NCH:
171 return &phy_db->calib_nch;
172 case IWL_PHY_DB_CALIB_CH:
173 return &phy_db->calib_ch;
174 case IWL_PHY_DB_CALIB_CHG_PAPD:
175 if (chg_id >= IWL_NUM_PAPD_CH_GROUPS)
176 return NULL;
177 return &phy_db->calib_ch_group_papd[chg_id];
178 case IWL_PHY_DB_CALIB_CHG_TXP:
179 if (chg_id >= IWL_NUM_TXP_CH_GROUPS)
180 return NULL;
181 return &phy_db->calib_ch_group_txp[chg_id];
182 default:
183 return NULL;
184 }
185 return NULL;
186}
187
188static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
189 enum iwl_phy_db_section_type type,
190 u16 chg_id)
191{
192 struct iwl_phy_db_entry *entry =
193 iwl_phy_db_get_section(phy_db, type, chg_id);
194 if (!entry)
195 return;
196
197 kfree(entry->data);
198 entry->data = NULL;
199 entry->size = 0;
200}
201
202void iwl_phy_db_free(struct iwl_phy_db *phy_db)
203{
204 int i;
205
206 if (!phy_db)
207 return;
208
209 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
210 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
211 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CH, 0);
212 for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
213 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
214 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
215 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
216
217 kfree(phy_db);
218}
Johannes Berg48e29342013-03-01 00:13:33 +0100219IWL_EXPORT_SYMBOL(iwl_phy_db_free);
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100220
221int iwl_phy_db_set_section(struct iwl_phy_db *phy_db, struct iwl_rx_packet *pkt,
222 gfp_t alloc_ctx)
223{
224 struct iwl_calib_res_notif_phy_db *phy_db_notif =
225 (struct iwl_calib_res_notif_phy_db *)pkt->data;
226 enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
227 u16 size = le16_to_cpu(phy_db_notif->length);
228 struct iwl_phy_db_entry *entry;
229 u16 chg_id = 0;
230
231 if (!phy_db)
232 return -EINVAL;
233
234 if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
235 type == IWL_PHY_DB_CALIB_CHG_TXP)
236 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
237
238 entry = iwl_phy_db_get_section(phy_db, type, chg_id);
239 if (!entry)
240 return -EINVAL;
241
242 kfree(entry->data);
243 entry->data = kmemdup(phy_db_notif->data, size, alloc_ctx);
244 if (!entry->data) {
245 entry->size = 0;
246 return -ENOMEM;
247 }
248
249 entry->size = size;
250
251 if (type == IWL_PHY_DB_CALIB_CH) {
252 phy_db->channel_num =
253 le32_to_cpup((__le32 *)phy_db_notif->data);
254 phy_db->channel_size =
255 (size - CHANNEL_NUM_SIZE) / phy_db->channel_num;
256 }
257
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100258 IWL_DEBUG_INFO(phy_db->trans,
259 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
260 __func__, __LINE__, type, size);
261
262 return 0;
263}
Johannes Berg48e29342013-03-01 00:13:33 +0100264IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100265
266static int is_valid_channel(u16 ch_id)
267{
268 if (ch_id <= 14 ||
269 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
270 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
271 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
272 return 1;
273 return 0;
274}
275
276static u8 ch_id_to_ch_index(u16 ch_id)
277{
278 if (WARN_ON(!is_valid_channel(ch_id)))
279 return 0xff;
280
281 if (ch_id <= 14)
282 return ch_id - 1;
283 if (ch_id <= 64)
284 return (ch_id + 20) / 4;
285 if (ch_id <= 140)
286 return (ch_id - 12) / 4;
287 return (ch_id - 13) / 4;
288}
289
290
291static u16 channel_id_to_papd(u16 ch_id)
292{
293 if (WARN_ON(!is_valid_channel(ch_id)))
294 return 0xff;
295
296 if (1 <= ch_id && ch_id <= 14)
297 return 0;
298 if (36 <= ch_id && ch_id <= 64)
299 return 1;
300 if (100 <= ch_id && ch_id <= 140)
301 return 2;
302 return 3;
303}
304
305static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
306{
307 struct iwl_phy_db_chg_txp *txp_chg;
308 int i;
309 u8 ch_index = ch_id_to_ch_index(ch_id);
310 if (ch_index == 0xff)
311 return 0xff;
312
313 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
314 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
315 if (!txp_chg)
316 return 0xff;
317 /*
318 * Looking for the first channel group that its max channel is
319 * higher then wanted channel.
320 */
321 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
322 return i;
323 }
324 return 0xff;
325}
326static
327int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
328 u32 type, u8 **data, u16 *size, u16 ch_id)
329{
330 struct iwl_phy_db_entry *entry;
331 u32 channel_num;
332 u32 channel_size;
333 u16 ch_group_id = 0;
334 u16 index;
335
336 if (!phy_db)
337 return -EINVAL;
338
339 /* find wanted channel group */
340 if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
341 ch_group_id = channel_id_to_papd(ch_id);
342 else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
343 ch_group_id = channel_id_to_txp(phy_db, ch_id);
344
345 entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
346 if (!entry)
347 return -EINVAL;
348
349 if (type == IWL_PHY_DB_CALIB_CH) {
350 index = ch_id_to_ch_index(ch_id);
351 channel_num = phy_db->channel_num;
352 channel_size = phy_db->channel_size;
353 if (index >= channel_num) {
354 IWL_ERR(phy_db->trans, "Wrong channel number %d\n",
355 ch_id);
356 return -EINVAL;
357 }
358 *data = entry->data + CHANNEL_NUM_SIZE + index * channel_size;
359 *size = channel_size;
360 } else {
361 *data = entry->data;
362 *size = entry->size;
363 }
364
Johannes Bergb1e1adf2013-01-24 14:14:22 +0100365 IWL_DEBUG_INFO(phy_db->trans,
366 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
367 __func__, __LINE__, type, *size);
368
369 return 0;
370}
371
372static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
373 u16 length, void *data)
374{
375 struct iwl_phy_db_cmd phy_db_cmd;
376 struct iwl_host_cmd cmd = {
377 .id = PHY_DB_CMD,
378 .flags = CMD_SYNC,
379 };
380
381 IWL_DEBUG_INFO(phy_db->trans,
382 "Sending PHY-DB hcmd of type %d, of length %d\n",
383 type, length);
384
385 /* Set phy db cmd variables */
386 phy_db_cmd.type = cpu_to_le16(type);
387 phy_db_cmd.length = cpu_to_le16(length);
388
389 /* Set hcmd variables */
390 cmd.data[0] = &phy_db_cmd;
391 cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
392 cmd.data[1] = data;
393 cmd.len[1] = length;
394 cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
395
396 return iwl_trans_send_cmd(phy_db->trans, &cmd);
397}
398
399static int iwl_phy_db_send_all_channel_groups(
400 struct iwl_phy_db *phy_db,
401 enum iwl_phy_db_section_type type,
402 u8 max_ch_groups)
403{
404 u16 i;
405 int err;
406 struct iwl_phy_db_entry *entry;
407
408 /* Send all the channel specific groups to operational fw */
409 for (i = 0; i < max_ch_groups; i++) {
410 entry = iwl_phy_db_get_section(phy_db,
411 type,
412 i);
413 if (!entry)
414 return -EINVAL;
415
416 /* Send the requested PHY DB section */
417 err = iwl_send_phy_db_cmd(phy_db,
418 type,
419 entry->size,
420 entry->data);
421 if (err) {
422 IWL_ERR(phy_db->trans,
423 "Can't SEND phy_db section %d (%d), err %d",
424 type, i, err);
425 return err;
426 }
427
428 IWL_DEBUG_INFO(phy_db->trans,
429 "Sent PHY_DB HCMD, type = %d num = %d",
430 type, i);
431 }
432
433 return 0;
434}
435
436int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
437{
438 u8 *data = NULL;
439 u16 size = 0;
440 int err;
441
442 IWL_DEBUG_INFO(phy_db->trans,
443 "Sending phy db data and configuration to runtime image\n");
444
445 /* Send PHY DB CFG section */
446 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
447 &data, &size, 0);
448 if (err) {
449 IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
450 return err;
451 }
452
453 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
454 if (err) {
455 IWL_ERR(phy_db->trans,
456 "Cannot send HCMD of Phy DB cfg section\n");
457 return err;
458 }
459
460 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
461 &data, &size, 0);
462 if (err) {
463 IWL_ERR(phy_db->trans,
464 "Cannot get Phy DB non specific channel section\n");
465 return err;
466 }
467
468 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
469 if (err) {
470 IWL_ERR(phy_db->trans,
471 "Cannot send HCMD of Phy DB non specific channel section\n");
472 return err;
473 }
474
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_PAPD,
478 IWL_NUM_PAPD_CH_GROUPS);
479 if (err) {
480 IWL_ERR(phy_db->trans,
481 "Cannot send channel specific PAPD groups");
482 return err;
483 }
484
485 /* Send all the TXP channel specific data */
486 err = iwl_phy_db_send_all_channel_groups(phy_db,
487 IWL_PHY_DB_CALIB_CHG_TXP,
488 IWL_NUM_TXP_CH_GROUPS);
489 if (err) {
490 IWL_ERR(phy_db->trans,
491 "Cannot send channel specific TX power groups");
492 return err;
493 }
494
495 IWL_DEBUG_INFO(phy_db->trans,
496 "Finished sending phy db non channel data\n");
497 return 0;
498}
Johannes Berg48e29342013-03-01 00:13:33 +0100499IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);