Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / iwl-agn-eeprom.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) 2008 - 2010 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 - 2010 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/kernel.h>
65 #include <linux/module.h>
66 #include <linux/slab.h>
67 #include <linux/init.h>
68
69 #include <net/mac80211.h>
70
71 #include "iwl-commands.h"
72 #include "iwl-dev.h"
73 #include "iwl-core.h"
74 #include "iwl-debug.h"
75 #include "iwl-agn.h"
76 #include "iwl-io.h"
77
78 /************************** EEPROM BANDS ****************************
79  *
80  * The iwl_eeprom_band definitions below provide the mapping from the
81  * EEPROM contents to the specific channel number supported for each
82  * band.
83  *
84  * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
85  * definition below maps to physical channel 42 in the 5.2GHz spectrum.
86  * The specific geography and calibration information for that channel
87  * is contained in the eeprom map itself.
88  *
89  * During init, we copy the eeprom information and channel map
90  * information into priv->channel_info_24/52 and priv->channel_map_24/52
91  *
92  * channel_map_24/52 provides the index in the channel_info array for a
93  * given channel.  We have to have two separate maps as there is channel
94  * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
95  * band_2
96  *
97  * A value of 0xff stored in the channel_map indicates that the channel
98  * is not supported by the hardware at all.
99  *
100  * A value of 0xfe in the channel_map indicates that the channel is not
101  * valid for Tx with the current hardware.  This means that
102  * while the system can tune and receive on a given channel, it may not
103  * be able to associate or transmit any frames on that
104  * channel.  There is no corresponding channel information for that
105  * entry.
106  *
107  *********************************************************************/
108
109 /**
110  * struct iwl_txpwr_section: eeprom section information
111  * @offset: indirect address into eeprom image
112  * @count: number of "struct iwl_eeprom_enhanced_txpwr" in this section
113  * @band: band type for the section
114  * @is_common - true: common section, false: channel section
115  * @is_cck - true: cck section, false: not cck section
116  * @is_ht_40 - true: all channel in the section are HT40 channel,
117  *             false: legacy or HT 20 MHz
118  *             ignore if it is common section
119  * @iwl_eeprom_section_channel: channel array in the section,
120  *             ignore if common section
121  */
122 struct iwl_txpwr_section {
123         u32 offset;
124         u8 count;
125         enum ieee80211_band band;
126         bool is_common;
127         bool is_cck;
128         bool is_ht40;
129         u8 iwl_eeprom_section_channel[EEPROM_MAX_TXPOWER_SECTION_ELEMENTS];
130 };
131
132 /**
133  * section 1 - 3 are regulatory tx power apply to all channels based on
134  *    modulation: CCK, OFDM
135  *    Band: 2.4GHz, 5.2GHz
136  * section 4 - 10 are regulatory tx power apply to specified channels
137  *    For example:
138  *      1L - Channel 1 Legacy
139  *      1HT - Channel 1 HT
140  *      (1,+1) - Channel 1 HT40 "_above_"
141  *
142  * Section 1: all CCK channels
143  * Section 2: all 2.4 GHz OFDM (Legacy, HT and HT40) channels
144  * Section 3: all 5.2 GHz OFDM (Legacy, HT and HT40) channels
145  * Section 4: 2.4 GHz 20MHz channels: 1L, 1HT, 2L, 2HT, 10L, 10HT, 11L, 11HT
146  * Section 5: 2.4 GHz 40MHz channels: (1,+1) (2,+1) (6,+1) (7,+1) (9,+1)
147  * Section 6: 5.2 GHz 20MHz channels: 36L, 64L, 100L, 36HT, 64HT, 100HT
148  * Section 7: 5.2 GHz 40MHz channels: (36,+1) (60,+1) (100,+1)
149  * Section 8: 2.4 GHz channel: 13L, 13HT
150  * Section 9: 2.4 GHz channel: 140L, 140HT
151  * Section 10: 2.4 GHz 40MHz channels: (132,+1)  (44,+1)
152  *
153  */
154 static const struct iwl_txpwr_section enhinfo[] = {
155         { EEPROM_LB_CCK_20_COMMON, 1, IEEE80211_BAND_2GHZ, true, true, false },
156         { EEPROM_LB_OFDM_COMMON, 3, IEEE80211_BAND_2GHZ, true, false, false },
157         { EEPROM_HB_OFDM_COMMON, 3, IEEE80211_BAND_5GHZ, true, false, false },
158         { EEPROM_LB_OFDM_20_BAND, 8, IEEE80211_BAND_2GHZ,
159                 false, false, false,
160                 {1, 1, 2, 2, 10, 10, 11, 11 } },
161         { EEPROM_LB_OFDM_HT40_BAND, 5, IEEE80211_BAND_2GHZ,
162                 false, false, true,
163                 { 1, 2, 6, 7, 9 } },
164         { EEPROM_HB_OFDM_20_BAND, 6, IEEE80211_BAND_5GHZ,
165                 false, false, false,
166                 { 36, 64, 100, 36, 64, 100 } },
167         { EEPROM_HB_OFDM_HT40_BAND, 3, IEEE80211_BAND_5GHZ,
168                 false, false, true,
169                 { 36, 60, 100 } },
170         { EEPROM_LB_OFDM_20_CHANNEL_13, 2, IEEE80211_BAND_2GHZ,
171                 false, false, false,
172                 { 13, 13 } },
173         { EEPROM_HB_OFDM_20_CHANNEL_140, 2, IEEE80211_BAND_5GHZ,
174                 false, false, false,
175                 { 140, 140 } },
176         { EEPROM_HB_OFDM_HT40_BAND_1, 2, IEEE80211_BAND_5GHZ,
177                 false, false, true,
178                 { 132, 44 } },
179 };
180
181 /******************************************************************************
182  *
183  * EEPROM related functions
184  *
185 ******************************************************************************/
186
187 /*
188  * The device's EEPROM semaphore prevents conflicts between driver and uCode
189  * when accessing the EEPROM; each access is a series of pulses to/from the
190  * EEPROM chip, not a single event, so even reads could conflict if they
191  * weren't arbitrated by the semaphore.
192  */
193 int iwlcore_eeprom_acquire_semaphore(struct iwl_priv *priv)
194 {
195         u16 count;
196         int ret;
197
198         for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
199                 /* Request semaphore */
200                 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
201                             CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
202
203                 /* See if we got it */
204                 ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
205                                 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
206                                 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
207                                 EEPROM_SEM_TIMEOUT);
208                 if (ret >= 0) {
209                         IWL_DEBUG_IO(priv,
210                                 "Acquired semaphore after %d tries.\n",
211                                 count+1);
212                         return ret;
213                 }
214         }
215
216         return ret;
217 }
218
219 void iwlcore_eeprom_release_semaphore(struct iwl_priv *priv)
220 {
221         iwl_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
222                 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
223
224 }
225
226 int iwl_eeprom_check_version(struct iwl_priv *priv)
227 {
228         u16 eeprom_ver;
229         u16 calib_ver;
230
231         eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
232         calib_ver = priv->cfg->ops->lib->eeprom_ops.calib_version(priv);
233
234         if (eeprom_ver < priv->cfg->eeprom_ver ||
235             calib_ver < priv->cfg->eeprom_calib_ver)
236                 goto err;
237
238         IWL_INFO(priv, "device EEPROM VER=0x%x, CALIB=0x%x\n",
239                  eeprom_ver, calib_ver);
240
241         return 0;
242 err:
243         IWL_ERR(priv, "Unsupported (too old) EEPROM VER=0x%x < 0x%x "
244                   "CALIB=0x%x < 0x%x\n",
245                   eeprom_ver, priv->cfg->eeprom_ver,
246                   calib_ver,  priv->cfg->eeprom_calib_ver);
247         return -EINVAL;
248
249 }
250
251 int iwl_eeprom_check_sku(struct iwl_priv *priv)
252 {
253         u16 eeprom_sku;
254         u16 radio_cfg;
255
256         eeprom_sku = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
257
258         priv->cfg->sku = ((eeprom_sku & EEPROM_SKU_CAP_BAND_SELECTION) >>
259                         EEPROM_SKU_CAP_BAND_POS);
260         if (eeprom_sku & EEPROM_SKU_CAP_11N_ENABLE)
261                 priv->cfg->sku |= IWL_SKU_N;
262
263         if (!priv->cfg->sku) {
264                 IWL_ERR(priv, "Invalid device sku\n");
265                 return -EINVAL;
266         }
267
268         IWL_INFO(priv, "Device SKU: 0X%x\n", priv->cfg->sku);
269
270         if (!priv->cfg->valid_tx_ant && !priv->cfg->valid_rx_ant) {
271                 /* not using .cfg overwrite */
272                 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
273                 priv->cfg->valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
274                 priv->cfg->valid_rx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
275                 if (!priv->cfg->valid_tx_ant || !priv->cfg->valid_rx_ant) {
276                         IWL_ERR(priv, "Invalid chain (0X%x, 0X%x)\n",
277                                 priv->cfg->valid_tx_ant,
278                                 priv->cfg->valid_rx_ant);
279                         return -EINVAL;
280                 }
281                 IWL_INFO(priv, "Valid Tx ant: 0X%x, Valid Rx ant: 0X%x\n",
282                          priv->cfg->valid_tx_ant, priv->cfg->valid_rx_ant);
283         }
284         /*
285          * for some special cases,
286          * EEPROM did not reflect the correct antenna setting
287          * so overwrite the valid tx/rx antenna from .cfg
288          */
289         return 0;
290 }
291
292 void iwl_eeprom_get_mac(const struct iwl_priv *priv, u8 *mac)
293 {
294         const u8 *addr = priv->cfg->ops->lib->eeprom_ops.query_addr(priv,
295                                         EEPROM_MAC_ADDRESS);
296         memcpy(mac, addr, ETH_ALEN);
297 }
298
299 /**
300  * iwl_get_max_txpower_avg - get the highest tx power from all chains.
301  *     find the highest tx power from all chains for the channel
302  */
303 static s8 iwl_get_max_txpower_avg(struct iwl_priv *priv,
304                 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
305                 int element, s8 *max_txpower_in_half_dbm)
306 {
307         s8 max_txpower_avg = 0; /* (dBm) */
308
309         IWL_DEBUG_INFO(priv, "%d - "
310                         "chain_a: %d dB chain_b: %d dB "
311                         "chain_c: %d dB mimo2: %d dB mimo3: %d dB\n",
312                         element,
313                         enhanced_txpower[element].chain_a_max >> 1,
314                         enhanced_txpower[element].chain_b_max >> 1,
315                         enhanced_txpower[element].chain_c_max >> 1,
316                         enhanced_txpower[element].mimo2_max >> 1,
317                         enhanced_txpower[element].mimo3_max >> 1);
318         /* Take the highest tx power from any valid chains */
319         if ((priv->cfg->valid_tx_ant & ANT_A) &&
320             (enhanced_txpower[element].chain_a_max > max_txpower_avg))
321                 max_txpower_avg = enhanced_txpower[element].chain_a_max;
322         if ((priv->cfg->valid_tx_ant & ANT_B) &&
323             (enhanced_txpower[element].chain_b_max > max_txpower_avg))
324                 max_txpower_avg = enhanced_txpower[element].chain_b_max;
325         if ((priv->cfg->valid_tx_ant & ANT_C) &&
326             (enhanced_txpower[element].chain_c_max > max_txpower_avg))
327                 max_txpower_avg = enhanced_txpower[element].chain_c_max;
328         if (((priv->cfg->valid_tx_ant == ANT_AB) |
329             (priv->cfg->valid_tx_ant == ANT_BC) |
330             (priv->cfg->valid_tx_ant == ANT_AC)) &&
331             (enhanced_txpower[element].mimo2_max > max_txpower_avg))
332                 max_txpower_avg =  enhanced_txpower[element].mimo2_max;
333         if ((priv->cfg->valid_tx_ant == ANT_ABC) &&
334             (enhanced_txpower[element].mimo3_max > max_txpower_avg))
335                 max_txpower_avg = enhanced_txpower[element].mimo3_max;
336
337         /*
338          * max. tx power in EEPROM is in 1/2 dBm format
339          * convert from 1/2 dBm to dBm (round-up convert)
340          * but we also do not want to loss 1/2 dBm resolution which
341          * will impact performance
342          */
343         *max_txpower_in_half_dbm = max_txpower_avg;
344         return (max_txpower_avg & 0x01) + (max_txpower_avg >> 1);
345 }
346
347 /**
348  * iwl_update_common_txpower: update channel tx power
349  *     update tx power per band based on EEPROM enhanced tx power info.
350  */
351 static s8 iwl_update_common_txpower(struct iwl_priv *priv,
352                 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
353                 int section, int element, s8 *max_txpower_in_half_dbm)
354 {
355         struct iwl_channel_info *ch_info;
356         int ch;
357         bool is_ht40 = false;
358         s8 max_txpower_avg; /* (dBm) */
359
360         /* it is common section, contain all type (Legacy, HT and HT40)
361          * based on the element in the section to determine
362          * is it HT 40 or not
363          */
364         if (element == EEPROM_TXPOWER_COMMON_HT40_INDEX)
365                 is_ht40 = true;
366         max_txpower_avg =
367                 iwl_get_max_txpower_avg(priv, enhanced_txpower,
368                                         element, max_txpower_in_half_dbm);
369
370         ch_info = priv->channel_info;
371
372         for (ch = 0; ch < priv->channel_count; ch++) {
373                 /* find matching band and update tx power if needed */
374                 if ((ch_info->band == enhinfo[section].band) &&
375                     (ch_info->max_power_avg < max_txpower_avg) &&
376                     (!is_ht40)) {
377                         /* Update regulatory-based run-time data */
378                         ch_info->max_power_avg = ch_info->curr_txpow =
379                                 max_txpower_avg;
380                         ch_info->scan_power = max_txpower_avg;
381                 }
382                 if ((ch_info->band == enhinfo[section].band) && is_ht40 &&
383                     (ch_info->ht40_max_power_avg < max_txpower_avg)) {
384                         /* Update regulatory-based run-time data */
385                         ch_info->ht40_max_power_avg = max_txpower_avg;
386                 }
387                 ch_info++;
388         }
389         return max_txpower_avg;
390 }
391
392 /**
393  * iwl_update_channel_txpower: update channel tx power
394  *      update channel tx power based on EEPROM enhanced tx power info.
395  */
396 static s8 iwl_update_channel_txpower(struct iwl_priv *priv,
397                 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
398                 int section, int element, s8 *max_txpower_in_half_dbm)
399 {
400         struct iwl_channel_info *ch_info;
401         int ch;
402         u8 channel;
403         s8 max_txpower_avg; /* (dBm) */
404
405         channel = enhinfo[section].iwl_eeprom_section_channel[element];
406         max_txpower_avg =
407                 iwl_get_max_txpower_avg(priv, enhanced_txpower,
408                                         element, max_txpower_in_half_dbm);
409
410         ch_info = priv->channel_info;
411         for (ch = 0; ch < priv->channel_count; ch++) {
412                 /* find matching channel and update tx power if needed */
413                 if (ch_info->channel == channel) {
414                         if ((ch_info->max_power_avg < max_txpower_avg) &&
415                             (!enhinfo[section].is_ht40)) {
416                                 /* Update regulatory-based run-time data */
417                                 ch_info->max_power_avg = max_txpower_avg;
418                                 ch_info->curr_txpow = max_txpower_avg;
419                                 ch_info->scan_power = max_txpower_avg;
420                         }
421                         if ((enhinfo[section].is_ht40) &&
422                             (ch_info->ht40_max_power_avg < max_txpower_avg)) {
423                                 /* Update regulatory-based run-time data */
424                                 ch_info->ht40_max_power_avg = max_txpower_avg;
425                         }
426                         break;
427                 }
428                 ch_info++;
429         }
430         return max_txpower_avg;
431 }
432
433 /**
434  * iwlcore_eeprom_enhanced_txpower: process enhanced tx power info
435  */
436 static void iwlcore_eeprom_enhanced_txpower_old(struct iwl_priv *priv)
437 {
438         int eeprom_section_count = 0;
439         int section, element;
440         struct iwl_eeprom_enhanced_txpwr *enhanced_txpower;
441         u32 offset;
442         s8 max_txpower_avg; /* (dBm) */
443         s8 max_txpower_in_half_dbm; /* (half-dBm) */
444
445         /* Loop through all the sections
446          * adjust bands and channel's max tx power
447          * Set the tx_power_user_lmt to the highest power
448          * supported by any channels and chains
449          */
450         for (section = 0; section < ARRAY_SIZE(enhinfo); section++) {
451                 eeprom_section_count = enhinfo[section].count;
452                 offset = enhinfo[section].offset;
453                 enhanced_txpower = (struct iwl_eeprom_enhanced_txpwr *)
454                                 iwl_eeprom_query_addr(priv, offset);
455
456                 /*
457                  * check for valid entry -
458                  * different version of EEPROM might contain different set
459                  * of enhanced tx power table
460                  * always check for valid entry before process
461                  * the information
462                  */
463                 if (!(enhanced_txpower->flags || enhanced_txpower->channel) ||
464                     enhanced_txpower->delta_20_in_40)
465                         continue;
466
467                 for (element = 0; element < eeprom_section_count; element++) {
468                         if (enhinfo[section].is_common)
469                                 max_txpower_avg =
470                                         iwl_update_common_txpower(priv,
471                                                 enhanced_txpower, section,
472                                                 element,
473                                                 &max_txpower_in_half_dbm);
474                         else
475                                 max_txpower_avg =
476                                         iwl_update_channel_txpower(priv,
477                                                 enhanced_txpower, section,
478                                                 element,
479                                                 &max_txpower_in_half_dbm);
480
481                         /* Update the tx_power_user_lmt to the highest power
482                          * supported by any channel */
483                         if (max_txpower_avg > priv->tx_power_user_lmt)
484                                 priv->tx_power_user_lmt = max_txpower_avg;
485
486                         /*
487                          * Update the tx_power_lmt_in_half_dbm to
488                          * the highest power supported by any channel
489                          */
490                         if (max_txpower_in_half_dbm >
491                             priv->tx_power_lmt_in_half_dbm)
492                                 priv->tx_power_lmt_in_half_dbm =
493                                         max_txpower_in_half_dbm;
494                 }
495         }
496 }
497
498 static void
499 iwlcore_eeprom_enh_txp_read_element(struct iwl_priv *priv,
500                                     struct iwl_eeprom_enhanced_txpwr *txp,
501                                     s8 max_txpower_avg)
502 {
503         int ch_idx;
504         bool is_ht40 = txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ;
505         enum ieee80211_band band;
506
507         band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
508                 IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
509
510         for (ch_idx = 0; ch_idx < priv->channel_count; ch_idx++) {
511                 struct iwl_channel_info *ch_info = &priv->channel_info[ch_idx];
512
513                 /* update matching channel or from common data only */
514                 if (txp->channel != 0 && ch_info->channel != txp->channel)
515                         continue;
516
517                 /* update matching band only */
518                 if (band != ch_info->band)
519                         continue;
520
521                 if (ch_info->max_power_avg < max_txpower_avg && !is_ht40) {
522                         ch_info->max_power_avg = max_txpower_avg;
523                         ch_info->curr_txpow = max_txpower_avg;
524                         ch_info->scan_power = max_txpower_avg;
525                 }
526
527                 if (is_ht40 && ch_info->ht40_max_power_avg < max_txpower_avg)
528                         ch_info->ht40_max_power_avg = max_txpower_avg;
529         }
530 }
531
532 #define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
533 #define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
534 #define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
535
536 static void iwlcore_eeprom_enhanced_txpower_new(struct iwl_priv *priv)
537 {
538         struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
539         int idx, entries;
540         __le16 *txp_len;
541         s8 max_txp_avg, max_txp_avg_halfdbm;
542
543         BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
544
545         /* the length is in 16-bit words, but we want entries */
546         txp_len = (__le16 *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_SZ_OFFS);
547         entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
548
549         txp_array = (void *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_OFFS);
550         for (idx = 0; idx < entries; idx++) {
551                 txp = &txp_array[idx];
552
553                 /* skip invalid entries */
554                 if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
555                         continue;
556
557                 max_txp_avg = iwl_get_max_txpower_avg(priv, txp_array, idx,
558                                                       &max_txp_avg_halfdbm);
559
560                 /*
561                  * Update the user limit values values to the highest
562                  * power supported by any channel
563                  */
564                 if (max_txp_avg > priv->tx_power_user_lmt)
565                         priv->tx_power_user_lmt = max_txp_avg;
566                 if (max_txp_avg_halfdbm > priv->tx_power_lmt_in_half_dbm)
567                         priv->tx_power_lmt_in_half_dbm = max_txp_avg_halfdbm;
568
569                 iwlcore_eeprom_enh_txp_read_element(priv, txp, max_txp_avg);
570         }
571 }
572
573 void iwlcore_eeprom_enhanced_txpower(struct iwl_priv *priv)
574 {
575         if (priv->cfg->use_new_eeprom_reading)
576                 iwlcore_eeprom_enhanced_txpower_new(priv);
577         else
578                 iwlcore_eeprom_enhanced_txpower_old(priv);
579 }