iwl-eeprom-parse.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 - 2012 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 - 2012 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. #include <linux/types.h>
  63. #include <linux/slab.h>
  64. #include <linux/export.h>
  65. #include "iwl-modparams.h"
  66. #include "iwl-eeprom-parse.h"
  67. /* EEPROM offset definitions */
  68. /* indirect access definitions */
  69. #define ADDRESS_MSK 0x0000FFFF
  70. #define INDIRECT_TYPE_MSK 0x000F0000
  71. #define INDIRECT_HOST 0x00010000
  72. #define INDIRECT_GENERAL 0x00020000
  73. #define INDIRECT_REGULATORY 0x00030000
  74. #define INDIRECT_CALIBRATION 0x00040000
  75. #define INDIRECT_PROCESS_ADJST 0x00050000
  76. #define INDIRECT_OTHERS 0x00060000
  77. #define INDIRECT_TXP_LIMIT 0x00070000
  78. #define INDIRECT_TXP_LIMIT_SIZE 0x00080000
  79. #define INDIRECT_ADDRESS 0x00100000
  80. /* corresponding link offsets in EEPROM */
  81. #define EEPROM_LINK_HOST (2*0x64)
  82. #define EEPROM_LINK_GENERAL (2*0x65)
  83. #define EEPROM_LINK_REGULATORY (2*0x66)
  84. #define EEPROM_LINK_CALIBRATION (2*0x67)
  85. #define EEPROM_LINK_PROCESS_ADJST (2*0x68)
  86. #define EEPROM_LINK_OTHERS (2*0x69)
  87. #define EEPROM_LINK_TXP_LIMIT (2*0x6a)
  88. #define EEPROM_LINK_TXP_LIMIT_SIZE (2*0x6b)
  89. /* General */
  90. #define EEPROM_DEVICE_ID (2*0x08) /* 2 bytes */
  91. #define EEPROM_SUBSYSTEM_ID (2*0x0A) /* 2 bytes */
  92. #define EEPROM_MAC_ADDRESS (2*0x15) /* 6 bytes */
  93. #define EEPROM_BOARD_REVISION (2*0x35) /* 2 bytes */
  94. #define EEPROM_BOARD_PBA_NUMBER (2*0x3B+1) /* 9 bytes */
  95. #define EEPROM_VERSION (2*0x44) /* 2 bytes */
  96. #define EEPROM_SKU_CAP (2*0x45) /* 2 bytes */
  97. #define EEPROM_OEM_MODE (2*0x46) /* 2 bytes */
  98. #define EEPROM_RADIO_CONFIG (2*0x48) /* 2 bytes */
  99. #define EEPROM_NUM_MAC_ADDRESS (2*0x4C) /* 2 bytes */
  100. /* calibration */
  101. struct iwl_eeprom_calib_hdr {
  102. u8 version;
  103. u8 pa_type;
  104. __le16 voltage;
  105. } __packed;
  106. #define EEPROM_CALIB_ALL (INDIRECT_ADDRESS | INDIRECT_CALIBRATION)
  107. #define EEPROM_XTAL ((2*0x128) | EEPROM_CALIB_ALL)
  108. /* temperature */
  109. #define EEPROM_KELVIN_TEMPERATURE ((2*0x12A) | EEPROM_CALIB_ALL)
  110. #define EEPROM_RAW_TEMPERATURE ((2*0x12B) | EEPROM_CALIB_ALL)
  111. /*
  112. * EEPROM bands
  113. * These are the channel numbers from each band in the order
  114. * that they are stored in the EEPROM band information. Note
  115. * that EEPROM bands aren't the same as mac80211 bands, and
  116. * there are even special "ht40 bands" in the EEPROM.
  117. */
  118. static const u8 iwl_eeprom_band_1[14] = { /* 2.4 GHz */
  119. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  120. };
  121. static const u8 iwl_eeprom_band_2[] = { /* 4915-5080MHz */
  122. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  123. };
  124. static const u8 iwl_eeprom_band_3[] = { /* 5170-5320MHz */
  125. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  126. };
  127. static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
  128. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  129. };
  130. static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
  131. 145, 149, 153, 157, 161, 165
  132. };
  133. static const u8 iwl_eeprom_band_6[] = { /* 2.4 ht40 channel */
  134. 1, 2, 3, 4, 5, 6, 7
  135. };
  136. static const u8 iwl_eeprom_band_7[] = { /* 5.2 ht40 channel */
  137. 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
  138. };
  139. #define IWL_NUM_CHANNELS (ARRAY_SIZE(iwl_eeprom_band_1) + \
  140. ARRAY_SIZE(iwl_eeprom_band_2) + \
  141. ARRAY_SIZE(iwl_eeprom_band_3) + \
  142. ARRAY_SIZE(iwl_eeprom_band_4) + \
  143. ARRAY_SIZE(iwl_eeprom_band_5))
  144. /* rate data (static) */
  145. static struct ieee80211_rate iwl_cfg80211_rates[] = {
  146. { .bitrate = 1 * 10, .hw_value = 0, .hw_value_short = 0, },
  147. { .bitrate = 2 * 10, .hw_value = 1, .hw_value_short = 1,
  148. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  149. { .bitrate = 5.5 * 10, .hw_value = 2, .hw_value_short = 2,
  150. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  151. { .bitrate = 11 * 10, .hw_value = 3, .hw_value_short = 3,
  152. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  153. { .bitrate = 6 * 10, .hw_value = 4, .hw_value_short = 4, },
  154. { .bitrate = 9 * 10, .hw_value = 5, .hw_value_short = 5, },
  155. { .bitrate = 12 * 10, .hw_value = 6, .hw_value_short = 6, },
  156. { .bitrate = 18 * 10, .hw_value = 7, .hw_value_short = 7, },
  157. { .bitrate = 24 * 10, .hw_value = 8, .hw_value_short = 8, },
  158. { .bitrate = 36 * 10, .hw_value = 9, .hw_value_short = 9, },
  159. { .bitrate = 48 * 10, .hw_value = 10, .hw_value_short = 10, },
  160. { .bitrate = 54 * 10, .hw_value = 11, .hw_value_short = 11, },
  161. };
  162. #define RATES_24_OFFS 0
  163. #define N_RATES_24 ARRAY_SIZE(iwl_cfg80211_rates)
  164. #define RATES_52_OFFS 4
  165. #define N_RATES_52 (N_RATES_24 - RATES_52_OFFS)
  166. /* EEPROM reading functions */
  167. static u16 iwl_eeprom_query16(const u8 *eeprom, size_t eeprom_size, int offset)
  168. {
  169. if (WARN_ON(offset + sizeof(u16) > eeprom_size))
  170. return 0;
  171. return le16_to_cpup((__le16 *)(eeprom + offset));
  172. }
  173. static u32 eeprom_indirect_address(const u8 *eeprom, size_t eeprom_size,
  174. u32 address)
  175. {
  176. u16 offset = 0;
  177. if ((address & INDIRECT_ADDRESS) == 0)
  178. return address;
  179. switch (address & INDIRECT_TYPE_MSK) {
  180. case INDIRECT_HOST:
  181. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  182. EEPROM_LINK_HOST);
  183. break;
  184. case INDIRECT_GENERAL:
  185. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  186. EEPROM_LINK_GENERAL);
  187. break;
  188. case INDIRECT_REGULATORY:
  189. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  190. EEPROM_LINK_REGULATORY);
  191. break;
  192. case INDIRECT_TXP_LIMIT:
  193. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  194. EEPROM_LINK_TXP_LIMIT);
  195. break;
  196. case INDIRECT_TXP_LIMIT_SIZE:
  197. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  198. EEPROM_LINK_TXP_LIMIT_SIZE);
  199. break;
  200. case INDIRECT_CALIBRATION:
  201. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  202. EEPROM_LINK_CALIBRATION);
  203. break;
  204. case INDIRECT_PROCESS_ADJST:
  205. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  206. EEPROM_LINK_PROCESS_ADJST);
  207. break;
  208. case INDIRECT_OTHERS:
  209. offset = iwl_eeprom_query16(eeprom, eeprom_size,
  210. EEPROM_LINK_OTHERS);
  211. break;
  212. default:
  213. WARN_ON(1);
  214. break;
  215. }
  216. /* translate the offset from words to byte */
  217. return (address & ADDRESS_MSK) + (offset << 1);
  218. }
  219. static const u8 *iwl_eeprom_query_addr(const u8 *eeprom, size_t eeprom_size,
  220. u32 offset)
  221. {
  222. u32 address = eeprom_indirect_address(eeprom, eeprom_size, offset);
  223. if (WARN_ON(address >= eeprom_size))
  224. return NULL;
  225. return &eeprom[address];
  226. }
  227. static int iwl_eeprom_read_calib(const u8 *eeprom, size_t eeprom_size,
  228. struct iwl_eeprom_data *data)
  229. {
  230. struct iwl_eeprom_calib_hdr *hdr;
  231. hdr = (void *)iwl_eeprom_query_addr(eeprom, eeprom_size,
  232. EEPROM_CALIB_ALL);
  233. if (!hdr)
  234. return -ENODATA;
  235. data->calib_version = hdr->version;
  236. data->calib_voltage = hdr->voltage;
  237. return 0;
  238. }
  239. /**
  240. * enum iwl_eeprom_channel_flags - channel flags in EEPROM
  241. * @EEPROM_CHANNEL_VALID: channel is usable for this SKU/geo
  242. * @EEPROM_CHANNEL_IBSS: usable as an IBSS channel
  243. * @EEPROM_CHANNEL_ACTIVE: active scanning allowed
  244. * @EEPROM_CHANNEL_RADAR: radar detection required
  245. * @EEPROM_CHANNEL_WIDE: 20 MHz channel okay (?)
  246. * @EEPROM_CHANNEL_DFS: dynamic freq selection candidate
  247. */
  248. enum iwl_eeprom_channel_flags {
  249. EEPROM_CHANNEL_VALID = BIT(0),
  250. EEPROM_CHANNEL_IBSS = BIT(1),
  251. EEPROM_CHANNEL_ACTIVE = BIT(3),
  252. EEPROM_CHANNEL_RADAR = BIT(4),
  253. EEPROM_CHANNEL_WIDE = BIT(5),
  254. EEPROM_CHANNEL_DFS = BIT(7),
  255. };
  256. /**
  257. * struct iwl_eeprom_channel - EEPROM channel data
  258. * @flags: %EEPROM_CHANNEL_* flags
  259. * @max_power_avg: max power (in dBm) on this channel, at most 31 dBm
  260. */
  261. struct iwl_eeprom_channel {
  262. u8 flags;
  263. s8 max_power_avg;
  264. } __packed;
  265. enum iwl_eeprom_enhanced_txpwr_flags {
  266. IWL_EEPROM_ENH_TXP_FL_VALID = BIT(0),
  267. IWL_EEPROM_ENH_TXP_FL_BAND_52G = BIT(1),
  268. IWL_EEPROM_ENH_TXP_FL_OFDM = BIT(2),
  269. IWL_EEPROM_ENH_TXP_FL_40MHZ = BIT(3),
  270. IWL_EEPROM_ENH_TXP_FL_HT_AP = BIT(4),
  271. IWL_EEPROM_ENH_TXP_FL_RES1 = BIT(5),
  272. IWL_EEPROM_ENH_TXP_FL_RES2 = BIT(6),
  273. IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE = BIT(7),
  274. };
  275. /**
  276. * iwl_eeprom_enhanced_txpwr structure
  277. * @flags: entry flags
  278. * @channel: channel number
  279. * @chain_a_max_pwr: chain a max power in 1/2 dBm
  280. * @chain_b_max_pwr: chain b max power in 1/2 dBm
  281. * @chain_c_max_pwr: chain c max power in 1/2 dBm
  282. * @delta_20_in_40: 20-in-40 deltas (hi/lo)
  283. * @mimo2_max_pwr: mimo2 max power in 1/2 dBm
  284. * @mimo3_max_pwr: mimo3 max power in 1/2 dBm
  285. *
  286. * This structure presents the enhanced regulatory tx power limit layout
  287. * in an EEPROM image.
  288. */
  289. struct iwl_eeprom_enhanced_txpwr {
  290. u8 flags;
  291. u8 channel;
  292. s8 chain_a_max;
  293. s8 chain_b_max;
  294. s8 chain_c_max;
  295. u8 delta_20_in_40;
  296. s8 mimo2_max;
  297. s8 mimo3_max;
  298. } __packed;
  299. static s8 iwl_get_max_txpwr_half_dbm(const struct iwl_eeprom_data *data,
  300. struct iwl_eeprom_enhanced_txpwr *txp)
  301. {
  302. s8 result = 0; /* (.5 dBm) */
  303. /* Take the highest tx power from any valid chains */
  304. if (data->valid_tx_ant & ANT_A && txp->chain_a_max > result)
  305. result = txp->chain_a_max;
  306. if (data->valid_tx_ant & ANT_B && txp->chain_b_max > result)
  307. result = txp->chain_b_max;
  308. if (data->valid_tx_ant & ANT_C && txp->chain_c_max > result)
  309. result = txp->chain_c_max;
  310. if ((data->valid_tx_ant == ANT_AB ||
  311. data->valid_tx_ant == ANT_BC ||
  312. data->valid_tx_ant == ANT_AC) && txp->mimo2_max > result)
  313. result = txp->mimo2_max;
  314. if (data->valid_tx_ant == ANT_ABC && txp->mimo3_max > result)
  315. result = txp->mimo3_max;
  316. return result;
  317. }
  318. #define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
  319. #define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
  320. #define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
  321. #define TXP_CHECK_AND_PRINT(x) \
  322. ((txp->flags & IWL_EEPROM_ENH_TXP_FL_##x) ? # x " " : "")
  323. static void
  324. iwl_eeprom_enh_txp_read_element(struct iwl_eeprom_data *data,
  325. struct iwl_eeprom_enhanced_txpwr *txp,
  326. int n_channels, s8 max_txpower_avg)
  327. {
  328. int ch_idx;
  329. enum ieee80211_band band;
  330. band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
  331. IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
  332. for (ch_idx = 0; ch_idx < n_channels; ch_idx++) {
  333. struct ieee80211_channel *chan = &data->channels[ch_idx];
  334. /* update matching channel or from common data only */
  335. if (txp->channel != 0 && chan->hw_value != txp->channel)
  336. continue;
  337. /* update matching band only */
  338. if (band != chan->band)
  339. continue;
  340. if (chan->max_power < max_txpower_avg &&
  341. !(txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ))
  342. chan->max_power = max_txpower_avg;
  343. }
  344. }
  345. static void iwl_eeprom_enhanced_txpower(struct device *dev,
  346. struct iwl_eeprom_data *data,
  347. const u8 *eeprom, size_t eeprom_size,
  348. int n_channels)
  349. {
  350. struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
  351. int idx, entries;
  352. __le16 *txp_len;
  353. s8 max_txp_avg_halfdbm;
  354. BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
  355. /* the length is in 16-bit words, but we want entries */
  356. txp_len = (__le16 *)iwl_eeprom_query_addr(eeprom, eeprom_size,
  357. EEPROM_TXP_SZ_OFFS);
  358. entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
  359. txp_array = (void *)iwl_eeprom_query_addr(eeprom, eeprom_size,
  360. EEPROM_TXP_OFFS);
  361. for (idx = 0; idx < entries; idx++) {
  362. txp = &txp_array[idx];
  363. /* skip invalid entries */
  364. if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
  365. continue;
  366. IWL_DEBUG_EEPROM(dev, "%s %d:\t %s%s%s%s%s%s%s%s (0x%02x)\n",
  367. (txp->channel && (txp->flags &
  368. IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE)) ?
  369. "Common " : (txp->channel) ?
  370. "Channel" : "Common",
  371. (txp->channel),
  372. TXP_CHECK_AND_PRINT(VALID),
  373. TXP_CHECK_AND_PRINT(BAND_52G),
  374. TXP_CHECK_AND_PRINT(OFDM),
  375. TXP_CHECK_AND_PRINT(40MHZ),
  376. TXP_CHECK_AND_PRINT(HT_AP),
  377. TXP_CHECK_AND_PRINT(RES1),
  378. TXP_CHECK_AND_PRINT(RES2),
  379. TXP_CHECK_AND_PRINT(COMMON_TYPE),
  380. txp->flags);
  381. IWL_DEBUG_EEPROM(dev,
  382. "\t\t chain_A: 0x%02x chain_B: 0X%02x chain_C: 0X%02x\n",
  383. txp->chain_a_max, txp->chain_b_max,
  384. txp->chain_c_max);
  385. IWL_DEBUG_EEPROM(dev,
  386. "\t\t MIMO2: 0x%02x MIMO3: 0x%02x High 20_on_40: 0x%02x Low 20_on_40: 0x%02x\n",
  387. txp->mimo2_max, txp->mimo3_max,
  388. ((txp->delta_20_in_40 & 0xf0) >> 4),
  389. (txp->delta_20_in_40 & 0x0f));
  390. max_txp_avg_halfdbm = iwl_get_max_txpwr_half_dbm(data, txp);
  391. iwl_eeprom_enh_txp_read_element(data, txp, n_channels,
  392. DIV_ROUND_UP(max_txp_avg_halfdbm, 2));
  393. if (max_txp_avg_halfdbm > data->max_tx_pwr_half_dbm)
  394. data->max_tx_pwr_half_dbm = max_txp_avg_halfdbm;
  395. }
  396. }
  397. static void iwl_init_band_reference(const struct iwl_cfg *cfg,
  398. const u8 *eeprom, size_t eeprom_size,
  399. int eeprom_band, int *eeprom_ch_count,
  400. const struct iwl_eeprom_channel **ch_info,
  401. const u8 **eeprom_ch_array)
  402. {
  403. u32 offset = cfg->eeprom_params->regulatory_bands[eeprom_band - 1];
  404. offset |= INDIRECT_ADDRESS | INDIRECT_REGULATORY;
  405. *ch_info = (void *)iwl_eeprom_query_addr(eeprom, eeprom_size, offset);
  406. switch (eeprom_band) {
  407. case 1: /* 2.4GHz band */
  408. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
  409. *eeprom_ch_array = iwl_eeprom_band_1;
  410. break;
  411. case 2: /* 4.9GHz band */
  412. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
  413. *eeprom_ch_array = iwl_eeprom_band_2;
  414. break;
  415. case 3: /* 5.2GHz band */
  416. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
  417. *eeprom_ch_array = iwl_eeprom_band_3;
  418. break;
  419. case 4: /* 5.5GHz band */
  420. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
  421. *eeprom_ch_array = iwl_eeprom_band_4;
  422. break;
  423. case 5: /* 5.7GHz band */
  424. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
  425. *eeprom_ch_array = iwl_eeprom_band_5;
  426. break;
  427. case 6: /* 2.4GHz ht40 channels */
  428. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
  429. *eeprom_ch_array = iwl_eeprom_band_6;
  430. break;
  431. case 7: /* 5 GHz ht40 channels */
  432. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
  433. *eeprom_ch_array = iwl_eeprom_band_7;
  434. break;
  435. default:
  436. *eeprom_ch_count = 0;
  437. *eeprom_ch_array = NULL;
  438. WARN_ON(1);
  439. }
  440. }
  441. #define CHECK_AND_PRINT(x) \
  442. ((eeprom_ch->flags & EEPROM_CHANNEL_##x) ? # x " " : "")
  443. static void iwl_mod_ht40_chan_info(struct device *dev,
  444. struct iwl_eeprom_data *data, int n_channels,
  445. enum ieee80211_band band, u16 channel,
  446. const struct iwl_eeprom_channel *eeprom_ch,
  447. u8 clear_ht40_extension_channel)
  448. {
  449. struct ieee80211_channel *chan = NULL;
  450. int i;
  451. for (i = 0; i < n_channels; i++) {
  452. if (data->channels[i].band != band)
  453. continue;
  454. if (data->channels[i].hw_value != channel)
  455. continue;
  456. chan = &data->channels[i];
  457. break;
  458. }
  459. if (!chan)
  460. return;
  461. IWL_DEBUG_EEPROM(dev,
  462. "HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm): Ad-Hoc %ssupported\n",
  463. channel,
  464. band == IEEE80211_BAND_5GHZ ? "5.2" : "2.4",
  465. CHECK_AND_PRINT(IBSS),
  466. CHECK_AND_PRINT(ACTIVE),
  467. CHECK_AND_PRINT(RADAR),
  468. CHECK_AND_PRINT(WIDE),
  469. CHECK_AND_PRINT(DFS),
  470. eeprom_ch->flags,
  471. eeprom_ch->max_power_avg,
  472. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS) &&
  473. !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ? ""
  474. : "not ");
  475. if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
  476. chan->flags &= ~clear_ht40_extension_channel;
  477. }
  478. #define CHECK_AND_PRINT_I(x) \
  479. ((eeprom_ch_info[ch_idx].flags & EEPROM_CHANNEL_##x) ? # x " " : "")
  480. static int iwl_init_channel_map(struct device *dev, const struct iwl_cfg *cfg,
  481. struct iwl_eeprom_data *data,
  482. const u8 *eeprom, size_t eeprom_size)
  483. {
  484. int band, ch_idx;
  485. const struct iwl_eeprom_channel *eeprom_ch_info;
  486. const u8 *eeprom_ch_array;
  487. int eeprom_ch_count;
  488. int n_channels = 0;
  489. /*
  490. * Loop through the 5 EEPROM bands and add them to the parse list
  491. */
  492. for (band = 1; band <= 5; band++) {
  493. struct ieee80211_channel *channel;
  494. iwl_init_band_reference(cfg, eeprom, eeprom_size, band,
  495. &eeprom_ch_count, &eeprom_ch_info,
  496. &eeprom_ch_array);
  497. /* Loop through each band adding each of the channels */
  498. for (ch_idx = 0; ch_idx < eeprom_ch_count; ch_idx++) {
  499. const struct iwl_eeprom_channel *eeprom_ch;
  500. eeprom_ch = &eeprom_ch_info[ch_idx];
  501. if (!(eeprom_ch->flags & EEPROM_CHANNEL_VALID)) {
  502. IWL_DEBUG_EEPROM(dev,
  503. "Ch. %d Flags %x [%sGHz] - No traffic\n",
  504. eeprom_ch_array[ch_idx],
  505. eeprom_ch_info[ch_idx].flags,
  506. (band != 1) ? "5.2" : "2.4");
  507. continue;
  508. }
  509. channel = &data->channels[n_channels];
  510. n_channels++;
  511. channel->hw_value = eeprom_ch_array[ch_idx];
  512. channel->band = (band == 1) ? IEEE80211_BAND_2GHZ
  513. : IEEE80211_BAND_5GHZ;
  514. channel->center_freq =
  515. ieee80211_channel_to_frequency(
  516. channel->hw_value, channel->band);
  517. /* set no-HT40, will enable as appropriate later */
  518. channel->flags = IEEE80211_CHAN_NO_HT40;
  519. if (!(eeprom_ch->flags & EEPROM_CHANNEL_IBSS))
  520. channel->flags |= IEEE80211_CHAN_NO_IBSS;
  521. if (!(eeprom_ch->flags & EEPROM_CHANNEL_ACTIVE))
  522. channel->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  523. if (eeprom_ch->flags & EEPROM_CHANNEL_RADAR)
  524. channel->flags |= IEEE80211_CHAN_RADAR;
  525. /* Initialize regulatory-based run-time data */
  526. channel->max_power =
  527. eeprom_ch_info[ch_idx].max_power_avg;
  528. IWL_DEBUG_EEPROM(dev,
  529. "Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x %ddBm): Ad-Hoc %ssupported\n",
  530. channel->hw_value,
  531. (band != 1) ? "5.2" : "2.4",
  532. CHECK_AND_PRINT_I(VALID),
  533. CHECK_AND_PRINT_I(IBSS),
  534. CHECK_AND_PRINT_I(ACTIVE),
  535. CHECK_AND_PRINT_I(RADAR),
  536. CHECK_AND_PRINT_I(WIDE),
  537. CHECK_AND_PRINT_I(DFS),
  538. eeprom_ch_info[ch_idx].flags,
  539. eeprom_ch_info[ch_idx].max_power_avg,
  540. ((eeprom_ch_info[ch_idx].flags &
  541. EEPROM_CHANNEL_IBSS) &&
  542. !(eeprom_ch_info[ch_idx].flags &
  543. EEPROM_CHANNEL_RADAR))
  544. ? "" : "not ");
  545. }
  546. }
  547. if (cfg->eeprom_params->enhanced_txpower) {
  548. /*
  549. * for newer device (6000 series and up)
  550. * EEPROM contain enhanced tx power information
  551. * driver need to process addition information
  552. * to determine the max channel tx power limits
  553. */
  554. iwl_eeprom_enhanced_txpower(dev, data, eeprom, eeprom_size,
  555. n_channels);
  556. } else {
  557. /* All others use data from channel map */
  558. int i;
  559. data->max_tx_pwr_half_dbm = -128;
  560. for (i = 0; i < n_channels; i++)
  561. data->max_tx_pwr_half_dbm =
  562. max_t(s8, data->max_tx_pwr_half_dbm,
  563. data->channels[i].max_power * 2);
  564. }
  565. /* Check if we do have HT40 channels */
  566. if (cfg->eeprom_params->regulatory_bands[5] ==
  567. EEPROM_REGULATORY_BAND_NO_HT40 &&
  568. cfg->eeprom_params->regulatory_bands[6] ==
  569. EEPROM_REGULATORY_BAND_NO_HT40)
  570. return n_channels;
  571. /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
  572. for (band = 6; band <= 7; band++) {
  573. enum ieee80211_band ieeeband;
  574. iwl_init_band_reference(cfg, eeprom, eeprom_size, band,
  575. &eeprom_ch_count, &eeprom_ch_info,
  576. &eeprom_ch_array);
  577. /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
  578. ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ
  579. : IEEE80211_BAND_5GHZ;
  580. /* Loop through each band adding each of the channels */
  581. for (ch_idx = 0; ch_idx < eeprom_ch_count; ch_idx++) {
  582. /* Set up driver's info for lower half */
  583. iwl_mod_ht40_chan_info(dev, data, n_channels, ieeeband,
  584. eeprom_ch_array[ch_idx],
  585. &eeprom_ch_info[ch_idx],
  586. IEEE80211_CHAN_NO_HT40PLUS);
  587. /* Set up driver's info for upper half */
  588. iwl_mod_ht40_chan_info(dev, data, n_channels, ieeeband,
  589. eeprom_ch_array[ch_idx] + 4,
  590. &eeprom_ch_info[ch_idx],
  591. IEEE80211_CHAN_NO_HT40MINUS);
  592. }
  593. }
  594. return n_channels;
  595. }
  596. static int iwl_init_sband_channels(struct iwl_eeprom_data *data,
  597. struct ieee80211_supported_band *sband,
  598. int n_channels, enum ieee80211_band band)
  599. {
  600. struct ieee80211_channel *chan = &data->channels[0];
  601. int n = 0, idx = 0;
  602. while (chan->band != band && idx < n_channels)
  603. chan = &data->channels[++idx];
  604. sband->channels = &data->channels[idx];
  605. while (chan->band == band && idx < n_channels) {
  606. chan = &data->channels[++idx];
  607. n++;
  608. }
  609. sband->n_channels = n;
  610. return n;
  611. }
  612. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  613. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  614. static void iwl_init_ht_hw_capab(const struct iwl_cfg *cfg,
  615. struct iwl_eeprom_data *data,
  616. struct ieee80211_sta_ht_cap *ht_info,
  617. enum ieee80211_band band)
  618. {
  619. int max_bit_rate = 0;
  620. u8 rx_chains;
  621. u8 tx_chains;
  622. tx_chains = hweight8(data->valid_tx_ant);
  623. if (cfg->rx_with_siso_diversity)
  624. rx_chains = 1;
  625. else
  626. rx_chains = hweight8(data->valid_rx_ant);
  627. if (!(data->sku & EEPROM_SKU_CAP_11N_ENABLE) || !cfg->ht_params) {
  628. ht_info->ht_supported = false;
  629. return;
  630. }
  631. ht_info->ht_supported = true;
  632. ht_info->cap = 0;
  633. if (iwlwifi_mod_params.amsdu_size_8K)
  634. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  635. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  636. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_4;
  637. ht_info->mcs.rx_mask[0] = 0xFF;
  638. if (rx_chains >= 2)
  639. ht_info->mcs.rx_mask[1] = 0xFF;
  640. if (rx_chains >= 3)
  641. ht_info->mcs.rx_mask[2] = 0xFF;
  642. if (cfg->ht_params->ht_greenfield_support)
  643. ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
  644. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  645. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  646. if (cfg->ht_params->ht40_bands & BIT(band)) {
  647. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  648. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  649. ht_info->mcs.rx_mask[4] = 0x01;
  650. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  651. }
  652. /* Highest supported Rx data rate */
  653. max_bit_rate *= rx_chains;
  654. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  655. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  656. /* Tx MCS capabilities */
  657. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  658. if (tx_chains != rx_chains) {
  659. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  660. ht_info->mcs.tx_params |= ((tx_chains - 1) <<
  661. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  662. }
  663. }
  664. static void iwl_init_sbands(struct device *dev, const struct iwl_cfg *cfg,
  665. struct iwl_eeprom_data *data,
  666. const u8 *eeprom, size_t eeprom_size)
  667. {
  668. int n_channels = iwl_init_channel_map(dev, cfg, data,
  669. eeprom, eeprom_size);
  670. int n_used = 0;
  671. struct ieee80211_supported_band *sband;
  672. sband = &data->bands[IEEE80211_BAND_2GHZ];
  673. sband->band = IEEE80211_BAND_2GHZ;
  674. sband->bitrates = &iwl_cfg80211_rates[RATES_24_OFFS];
  675. sband->n_bitrates = N_RATES_24;
  676. n_used += iwl_init_sband_channels(data, sband, n_channels,
  677. IEEE80211_BAND_2GHZ);
  678. iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, IEEE80211_BAND_2GHZ);
  679. sband = &data->bands[IEEE80211_BAND_5GHZ];
  680. sband->band = IEEE80211_BAND_5GHZ;
  681. sband->bitrates = &iwl_cfg80211_rates[RATES_52_OFFS];
  682. sband->n_bitrates = N_RATES_52;
  683. n_used += iwl_init_sband_channels(data, sband, n_channels,
  684. IEEE80211_BAND_5GHZ);
  685. iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, IEEE80211_BAND_5GHZ);
  686. if (n_channels != n_used)
  687. IWL_ERR_DEV(dev, "EEPROM: used only %d of %d channels\n",
  688. n_used, n_channels);
  689. }
  690. /* EEPROM data functions */
  691. struct iwl_eeprom_data *
  692. iwl_parse_eeprom_data(struct device *dev, const struct iwl_cfg *cfg,
  693. const u8 *eeprom, size_t eeprom_size)
  694. {
  695. struct iwl_eeprom_data *data;
  696. const void *tmp;
  697. if (WARN_ON(!cfg || !cfg->eeprom_params))
  698. return NULL;
  699. data = kzalloc(sizeof(*data) +
  700. sizeof(struct ieee80211_channel) * IWL_NUM_CHANNELS,
  701. GFP_KERNEL);
  702. if (!data)
  703. return NULL;
  704. /* get MAC address(es) */
  705. tmp = iwl_eeprom_query_addr(eeprom, eeprom_size, EEPROM_MAC_ADDRESS);
  706. if (!tmp)
  707. goto err_free;
  708. memcpy(data->hw_addr, tmp, ETH_ALEN);
  709. data->n_hw_addrs = iwl_eeprom_query16(eeprom, eeprom_size,
  710. EEPROM_NUM_MAC_ADDRESS);
  711. if (iwl_eeprom_read_calib(eeprom, eeprom_size, data))
  712. goto err_free;
  713. tmp = iwl_eeprom_query_addr(eeprom, eeprom_size, EEPROM_XTAL);
  714. if (!tmp)
  715. goto err_free;
  716. memcpy(data->xtal_calib, tmp, sizeof(data->xtal_calib));
  717. tmp = iwl_eeprom_query_addr(eeprom, eeprom_size,
  718. EEPROM_RAW_TEMPERATURE);
  719. if (!tmp)
  720. goto err_free;
  721. data->raw_temperature = *(__le16 *)tmp;
  722. tmp = iwl_eeprom_query_addr(eeprom, eeprom_size,
  723. EEPROM_KELVIN_TEMPERATURE);
  724. if (!tmp)
  725. goto err_free;
  726. data->kelvin_temperature = *(__le16 *)tmp;
  727. data->kelvin_voltage = *((__le16 *)tmp + 1);
  728. data->radio_cfg = iwl_eeprom_query16(eeprom, eeprom_size,
  729. EEPROM_RADIO_CONFIG);
  730. data->sku = iwl_eeprom_query16(eeprom, eeprom_size,
  731. EEPROM_SKU_CAP);
  732. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_ALL)
  733. data->sku &= ~EEPROM_SKU_CAP_11N_ENABLE;
  734. data->eeprom_version = iwl_eeprom_query16(eeprom, eeprom_size,
  735. EEPROM_VERSION);
  736. data->valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(data->radio_cfg);
  737. data->valid_rx_ant = EEPROM_RF_CFG_RX_ANT_MSK(data->radio_cfg);
  738. /* check overrides (some devices have wrong EEPROM) */
  739. if (cfg->valid_tx_ant)
  740. data->valid_tx_ant = cfg->valid_tx_ant;
  741. if (cfg->valid_rx_ant)
  742. data->valid_rx_ant = cfg->valid_rx_ant;
  743. if (!data->valid_tx_ant || !data->valid_rx_ant) {
  744. IWL_ERR_DEV(dev, "invalid antennas (0x%x, 0x%x)\n",
  745. data->valid_tx_ant, data->valid_rx_ant);
  746. goto err_free;
  747. }
  748. iwl_init_sbands(dev, cfg, data, eeprom, eeprom_size);
  749. return data;
  750. err_free:
  751. kfree(data);
  752. return NULL;
  753. }
  754. EXPORT_SYMBOL_GPL(iwl_parse_eeprom_data);
  755. /* helper functions */
  756. int iwl_eeprom_check_version(struct iwl_eeprom_data *data,
  757. struct iwl_trans *trans)
  758. {
  759. if (data->eeprom_version >= trans->cfg->eeprom_ver ||
  760. data->calib_version >= trans->cfg->eeprom_calib_ver) {
  761. IWL_INFO(trans, "device EEPROM VER=0x%x, CALIB=0x%x\n",
  762. data->eeprom_version, data->calib_version);
  763. return 0;
  764. }
  765. IWL_ERR(trans,
  766. "Unsupported (too old) EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
  767. data->eeprom_version, trans->cfg->eeprom_ver,
  768. data->calib_version, trans->cfg->eeprom_calib_ver);
  769. return -EINVAL;
  770. }
  771. EXPORT_SYMBOL_GPL(iwl_eeprom_check_version);