]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/iwl-phy-db.c
Merge tag 'staging-3.9-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / iwl-phy-db.c
1 /******************************************************************************
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
25  * in the file called LICENSE.GPL.
26  *
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
68 #include "iwl-phy-db.h"
69 #include "iwl-debug.h"
70 #include "iwl-op-mode.h"
71 #include "iwl-trans.h"
72
73 #define CHANNEL_NUM_SIZE        4       /* num of channels in calib_ch size */
74 #define IWL_NUM_PAPD_CH_GROUPS  4
75 #define IWL_NUM_TXP_CH_GROUPS   9
76
77 struct iwl_phy_db_entry {
78         u16     size;
79         u8      *data;
80 };
81
82 /**
83  * struct iwl_phy_db - stores phy configuration and calibration data.
84  *
85  * @cfg: phy configuration.
86  * @calib_nch: non channel specific calibration data.
87  * @calib_ch: channel specific calibration data.
88  * @calib_ch_group_papd: calibration data related to papd channel group.
89  * @calib_ch_group_txp: calibration data related to tx power chanel group.
90  */
91 struct iwl_phy_db {
92         struct iwl_phy_db_entry cfg;
93         struct iwl_phy_db_entry calib_nch;
94         struct iwl_phy_db_entry calib_ch;
95         struct iwl_phy_db_entry calib_ch_group_papd[IWL_NUM_PAPD_CH_GROUPS];
96         struct iwl_phy_db_entry calib_ch_group_txp[IWL_NUM_TXP_CH_GROUPS];
97
98         u32 channel_num;
99         u32 channel_size;
100
101         struct iwl_trans *trans;
102 };
103
104 enum iwl_phy_db_section_type {
105         IWL_PHY_DB_CFG = 1,
106         IWL_PHY_DB_CALIB_NCH,
107         IWL_PHY_DB_CALIB_CH,
108         IWL_PHY_DB_CALIB_CHG_PAPD,
109         IWL_PHY_DB_CALIB_CHG_TXP,
110         IWL_PHY_DB_MAX
111 };
112
113 #define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */
114
115 /*
116  * phy db - configure operational ucode
117  */
118 struct iwl_phy_db_cmd {
119         __le16 type;
120         __le16 length;
121         u8 data[];
122 } __packed;
123
124 /* for parsing of tx power channel group data that comes from the firmware*/
125 struct iwl_phy_db_chg_txp {
126         __le32 space;
127         __le16 max_channel_idx;
128 } __packed;
129
130 /*
131  * phy db - Receieve phy db chunk after calibrations
132  */
133 struct iwl_calib_res_notif_phy_db {
134         __le16 type;
135         __le16 length;
136         u8 data[];
137 } __packed;
138
139 struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
140 {
141         struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
142                                             GFP_KERNEL);
143
144         if (!phy_db)
145                 return phy_db;
146
147         phy_db->trans = trans;
148
149         /* TODO: add default values of the phy db. */
150         return phy_db;
151 }
152 EXPORT_SYMBOL(iwl_phy_db_init);
153
154 /*
155  * get phy db section: returns a pointer to a phy db section specified by
156  * type and channel group id.
157  */
158 static struct iwl_phy_db_entry *
159 iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
160                        enum iwl_phy_db_section_type type,
161                        u16 chg_id)
162 {
163         if (!phy_db || type >= IWL_PHY_DB_MAX)
164                 return NULL;
165
166         switch (type) {
167         case IWL_PHY_DB_CFG:
168                 return &phy_db->cfg;
169         case IWL_PHY_DB_CALIB_NCH:
170                 return &phy_db->calib_nch;
171         case IWL_PHY_DB_CALIB_CH:
172                 return &phy_db->calib_ch;
173         case IWL_PHY_DB_CALIB_CHG_PAPD:
174                 if (chg_id >= IWL_NUM_PAPD_CH_GROUPS)
175                         return NULL;
176                 return &phy_db->calib_ch_group_papd[chg_id];
177         case IWL_PHY_DB_CALIB_CHG_TXP:
178                 if (chg_id >= IWL_NUM_TXP_CH_GROUPS)
179                         return NULL;
180                 return &phy_db->calib_ch_group_txp[chg_id];
181         default:
182                 return NULL;
183         }
184         return NULL;
185 }
186
187 static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
188                                     enum iwl_phy_db_section_type type,
189                                     u16 chg_id)
190 {
191         struct iwl_phy_db_entry *entry =
192                                 iwl_phy_db_get_section(phy_db, type, chg_id);
193         if (!entry)
194                 return;
195
196         kfree(entry->data);
197         entry->data = NULL;
198         entry->size = 0;
199 }
200
201 void iwl_phy_db_free(struct iwl_phy_db *phy_db)
202 {
203         int i;
204
205         if (!phy_db)
206                 return;
207
208         iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
209         iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
210         iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CH, 0);
211         for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
212                 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
213         for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
214                 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
215
216         kfree(phy_db);
217 }
218 EXPORT_SYMBOL(iwl_phy_db_free);
219
220 int iwl_phy_db_set_section(struct iwl_phy_db *phy_db, struct iwl_rx_packet *pkt,
221                            gfp_t alloc_ctx)
222 {
223         struct iwl_calib_res_notif_phy_db *phy_db_notif =
224                         (struct iwl_calib_res_notif_phy_db *)pkt->data;
225         enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
226         u16 size  = le16_to_cpu(phy_db_notif->length);
227         struct iwl_phy_db_entry *entry;
228         u16 chg_id = 0;
229
230         if (!phy_db)
231                 return -EINVAL;
232
233         if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
234             type == IWL_PHY_DB_CALIB_CHG_TXP)
235                 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
236
237         entry = iwl_phy_db_get_section(phy_db, type, chg_id);
238         if (!entry)
239                 return -EINVAL;
240
241         kfree(entry->data);
242         entry->data = kmemdup(phy_db_notif->data, size, alloc_ctx);
243         if (!entry->data) {
244                 entry->size = 0;
245                 return -ENOMEM;
246         }
247
248         entry->size = size;
249
250         if (type == IWL_PHY_DB_CALIB_CH) {
251                 phy_db->channel_num =
252                         le32_to_cpup((__le32 *)phy_db_notif->data);
253                 phy_db->channel_size =
254                         (size - CHANNEL_NUM_SIZE) / phy_db->channel_num;
255         }
256
257         IWL_DEBUG_INFO(phy_db->trans,
258                        "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
259                        __func__, __LINE__, type, size);
260
261         return 0;
262 }
263 EXPORT_SYMBOL(iwl_phy_db_set_section);
264
265 static int is_valid_channel(u16 ch_id)
266 {
267         if (ch_id <= 14 ||
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))
271                 return 1;
272         return 0;
273 }
274
275 static u8 ch_id_to_ch_index(u16 ch_id)
276 {
277         if (WARN_ON(!is_valid_channel(ch_id)))
278                 return 0xff;
279
280         if (ch_id <= 14)
281                 return ch_id - 1;
282         if (ch_id <= 64)
283                 return (ch_id + 20) / 4;
284         if (ch_id <= 140)
285                 return (ch_id - 12) / 4;
286         return (ch_id - 13) / 4;
287 }
288
289
290 static u16 channel_id_to_papd(u16 ch_id)
291 {
292         if (WARN_ON(!is_valid_channel(ch_id)))
293                 return 0xff;
294
295         if (1 <= ch_id && ch_id <= 14)
296                 return 0;
297         if (36 <= ch_id && ch_id <= 64)
298                 return 1;
299         if (100 <= ch_id && ch_id <= 140)
300                 return 2;
301         return 3;
302 }
303
304 static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
305 {
306         struct iwl_phy_db_chg_txp *txp_chg;
307         int i;
308         u8 ch_index = ch_id_to_ch_index(ch_id);
309         if (ch_index == 0xff)
310                 return 0xff;
311
312         for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
313                 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
314                 if (!txp_chg)
315                         return 0xff;
316                 /*
317                  * Looking for the first channel group that its max channel is
318                  * higher then wanted channel.
319                  */
320                 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
321                         return i;
322         }
323         return 0xff;
324 }
325 static
326 int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
327                                 u32 type, u8 **data, u16 *size, u16 ch_id)
328 {
329         struct iwl_phy_db_entry *entry;
330         u32 channel_num;
331         u32 channel_size;
332         u16 ch_group_id = 0;
333         u16 index;
334
335         if (!phy_db)
336                 return -EINVAL;
337
338         /* find wanted channel group */
339         if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
340                 ch_group_id = channel_id_to_papd(ch_id);
341         else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
342                 ch_group_id = channel_id_to_txp(phy_db, ch_id);
343
344         entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
345         if (!entry)
346                 return -EINVAL;
347
348         if (type == IWL_PHY_DB_CALIB_CH) {
349                 index = ch_id_to_ch_index(ch_id);
350                 channel_num = phy_db->channel_num;
351                 channel_size = phy_db->channel_size;
352                 if (index >= channel_num) {
353                         IWL_ERR(phy_db->trans, "Wrong channel number %d\n",
354                                 ch_id);
355                         return -EINVAL;
356                 }
357                 *data = entry->data + CHANNEL_NUM_SIZE + index * channel_size;
358                 *size = channel_size;
359         } else {
360                 *data = entry->data;
361                 *size = entry->size;
362         }
363
364         IWL_DEBUG_INFO(phy_db->trans,
365                        "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
366                        __func__, __LINE__, type, *size);
367
368         return 0;
369 }
370
371 static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
372                                u16 length, void *data)
373 {
374         struct iwl_phy_db_cmd phy_db_cmd;
375         struct iwl_host_cmd cmd = {
376                 .id = PHY_DB_CMD,
377                 .flags = CMD_SYNC,
378         };
379
380         IWL_DEBUG_INFO(phy_db->trans,
381                        "Sending PHY-DB hcmd of type %d, of length %d\n",
382                        type, length);
383
384         /* Set phy db cmd variables */
385         phy_db_cmd.type = cpu_to_le16(type);
386         phy_db_cmd.length = cpu_to_le16(length);
387
388         /* Set hcmd variables */
389         cmd.data[0] = &phy_db_cmd;
390         cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
391         cmd.data[1] = data;
392         cmd.len[1] = length;
393         cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
394
395         return iwl_trans_send_cmd(phy_db->trans, &cmd);
396 }
397
398 static int iwl_phy_db_send_all_channel_groups(
399                                         struct iwl_phy_db *phy_db,
400                                         enum iwl_phy_db_section_type type,
401                                         u8 max_ch_groups)
402 {
403         u16 i;
404         int err;
405         struct iwl_phy_db_entry *entry;
406
407         /* Send all the  channel specific groups to operational fw */
408         for (i = 0; i < max_ch_groups; i++) {
409                 entry = iwl_phy_db_get_section(phy_db,
410                                                type,
411                                                i);
412                 if (!entry)
413                         return -EINVAL;
414
415                 /* Send the requested PHY DB section */
416                 err = iwl_send_phy_db_cmd(phy_db,
417                                           type,
418                                           entry->size,
419                                           entry->data);
420                 if (err) {
421                         IWL_ERR(phy_db->trans,
422                                 "Can't SEND phy_db section %d (%d), err %d",
423                                 type, i, err);
424                         return err;
425                 }
426
427                 IWL_DEBUG_INFO(phy_db->trans,
428                                "Sent PHY_DB HCMD, type = %d num = %d",
429                                type, i);
430         }
431
432         return 0;
433 }
434
435 int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
436 {
437         u8 *data = NULL;
438         u16 size = 0;
439         int err;
440
441         IWL_DEBUG_INFO(phy_db->trans,
442                        "Sending phy db data and configuration to runtime image\n");
443
444         /* Send PHY DB CFG section */
445         err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
446                                           &data, &size, 0);
447         if (err) {
448                 IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
449                 return err;
450         }
451
452         err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
453         if (err) {
454                 IWL_ERR(phy_db->trans,
455                         "Cannot send HCMD of  Phy DB cfg section\n");
456                 return err;
457         }
458
459         err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
460                                           &data, &size, 0);
461         if (err) {
462                 IWL_ERR(phy_db->trans,
463                         "Cannot get Phy DB non specific channel section\n");
464                 return err;
465         }
466
467         err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
468         if (err) {
469                 IWL_ERR(phy_db->trans,
470                         "Cannot send HCMD of Phy DB non specific channel section\n");
471                 return err;
472         }
473
474         /* Send all the TXP channel specific data */
475         err = iwl_phy_db_send_all_channel_groups(phy_db,
476                                                  IWL_PHY_DB_CALIB_CHG_PAPD,
477                                                  IWL_NUM_PAPD_CH_GROUPS);
478         if (err) {
479                 IWL_ERR(phy_db->trans,
480                         "Cannot send channel specific PAPD groups");
481                 return err;
482         }
483
484         /* Send all the TXP channel specific data */
485         err = iwl_phy_db_send_all_channel_groups(phy_db,
486                                                  IWL_PHY_DB_CALIB_CHG_TXP,
487                                                  IWL_NUM_TXP_CH_GROUPS);
488         if (err) {
489                 IWL_ERR(phy_db->trans,
490                         "Cannot send channel specific TX power groups");
491                 return err;
492         }
493
494         IWL_DEBUG_INFO(phy_db->trans,
495                        "Finished sending phy db non channel data\n");
496         return 0;
497 }
498 EXPORT_SYMBOL(iwl_send_phy_db_data);