iwl-nvm-parse.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. * Copyright(c) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  25. * USA
  26. *
  27. * The full GNU General Public License is included in this distribution
  28. * in the file called COPYING.
  29. *
  30. * Contact Information:
  31. * Intel Linux Wireless <linuxwifi@intel.com>
  32. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  33. *
  34. * BSD LICENSE
  35. *
  36. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  37. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  38. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  39. * Copyright(c) 2018 Intel Corporation
  40. * All rights reserved.
  41. *
  42. * Redistribution and use in source and binary forms, with or without
  43. * modification, are permitted provided that the following conditions
  44. * are met:
  45. *
  46. * * Redistributions of source code must retain the above copyright
  47. * notice, this list of conditions and the following disclaimer.
  48. * * Redistributions in binary form must reproduce the above copyright
  49. * notice, this list of conditions and the following disclaimer in
  50. * the documentation and/or other materials provided with the
  51. * distribution.
  52. * * Neither the name Intel Corporation nor the names of its
  53. * contributors may be used to endorse or promote products derived
  54. * from this software without specific prior written permission.
  55. *
  56. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  57. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  58. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  59. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  60. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  61. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  62. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  63. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  64. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  65. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  66. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. *****************************************************************************/
  68. #include <linux/types.h>
  69. #include <linux/slab.h>
  70. #include <linux/export.h>
  71. #include <linux/etherdevice.h>
  72. #include <linux/pci.h>
  73. #include <linux/firmware.h>
  74. #include "iwl-drv.h"
  75. #include "iwl-modparams.h"
  76. #include "iwl-nvm-parse.h"
  77. #include "iwl-prph.h"
  78. #include "iwl-io.h"
  79. #include "iwl-csr.h"
  80. #include "fw/acpi.h"
  81. #include "fw/api/nvm-reg.h"
  82. #include "fw/api/commands.h"
  83. #include "fw/api/cmdhdr.h"
  84. #include "fw/img.h"
  85. /* NVM offsets (in words) definitions */
  86. enum nvm_offsets {
  87. /* NVM HW-Section offset (in words) definitions */
  88. SUBSYSTEM_ID = 0x0A,
  89. HW_ADDR = 0x15,
  90. /* NVM SW-Section offset (in words) definitions */
  91. NVM_SW_SECTION = 0x1C0,
  92. NVM_VERSION = 0,
  93. RADIO_CFG = 1,
  94. SKU = 2,
  95. N_HW_ADDRS = 3,
  96. NVM_CHANNELS = 0x1E0 - NVM_SW_SECTION,
  97. /* NVM calibration section offset (in words) definitions */
  98. NVM_CALIB_SECTION = 0x2B8,
  99. XTAL_CALIB = 0x316 - NVM_CALIB_SECTION,
  100. /* NVM REGULATORY -Section offset (in words) definitions */
  101. NVM_CHANNELS_SDP = 0,
  102. };
  103. enum ext_nvm_offsets {
  104. /* NVM HW-Section offset (in words) definitions */
  105. MAC_ADDRESS_OVERRIDE_EXT_NVM = 1,
  106. /* NVM SW-Section offset (in words) definitions */
  107. NVM_VERSION_EXT_NVM = 0,
  108. RADIO_CFG_FAMILY_EXT_NVM = 0,
  109. SKU_FAMILY_8000 = 2,
  110. N_HW_ADDRS_FAMILY_8000 = 3,
  111. /* NVM REGULATORY -Section offset (in words) definitions */
  112. NVM_CHANNELS_EXTENDED = 0,
  113. NVM_LAR_OFFSET_OLD = 0x4C7,
  114. NVM_LAR_OFFSET = 0x507,
  115. NVM_LAR_ENABLED = 0x7,
  116. };
  117. /* SKU Capabilities (actual values from NVM definition) */
  118. enum nvm_sku_bits {
  119. NVM_SKU_CAP_BAND_24GHZ = BIT(0),
  120. NVM_SKU_CAP_BAND_52GHZ = BIT(1),
  121. NVM_SKU_CAP_11N_ENABLE = BIT(2),
  122. NVM_SKU_CAP_11AC_ENABLE = BIT(3),
  123. NVM_SKU_CAP_MIMO_DISABLE = BIT(5),
  124. };
  125. /*
  126. * These are the channel numbers in the order that they are stored in the NVM
  127. */
  128. static const u8 iwl_nvm_channels[] = {
  129. /* 2.4 GHz */
  130. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  131. /* 5 GHz */
  132. 36, 40, 44 , 48, 52, 56, 60, 64,
  133. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
  134. 149, 153, 157, 161, 165
  135. };
  136. static const u8 iwl_ext_nvm_channels[] = {
  137. /* 2.4 GHz */
  138. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  139. /* 5 GHz */
  140. 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92,
  141. 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
  142. 149, 153, 157, 161, 165, 169, 173, 177, 181
  143. };
  144. #define IWL_NVM_NUM_CHANNELS ARRAY_SIZE(iwl_nvm_channels)
  145. #define IWL_NVM_NUM_CHANNELS_EXT ARRAY_SIZE(iwl_ext_nvm_channels)
  146. #define NUM_2GHZ_CHANNELS 14
  147. #define NUM_2GHZ_CHANNELS_EXT 14
  148. #define FIRST_2GHZ_HT_MINUS 5
  149. #define LAST_2GHZ_HT_PLUS 9
  150. #define LAST_5GHZ_HT 165
  151. #define LAST_5GHZ_HT_FAMILY_8000 181
  152. #define N_HW_ADDR_MASK 0xF
  153. /* rate data (static) */
  154. static struct ieee80211_rate iwl_cfg80211_rates[] = {
  155. { .bitrate = 1 * 10, .hw_value = 0, .hw_value_short = 0, },
  156. { .bitrate = 2 * 10, .hw_value = 1, .hw_value_short = 1,
  157. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  158. { .bitrate = 5.5 * 10, .hw_value = 2, .hw_value_short = 2,
  159. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  160. { .bitrate = 11 * 10, .hw_value = 3, .hw_value_short = 3,
  161. .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
  162. { .bitrate = 6 * 10, .hw_value = 4, .hw_value_short = 4, },
  163. { .bitrate = 9 * 10, .hw_value = 5, .hw_value_short = 5, },
  164. { .bitrate = 12 * 10, .hw_value = 6, .hw_value_short = 6, },
  165. { .bitrate = 18 * 10, .hw_value = 7, .hw_value_short = 7, },
  166. { .bitrate = 24 * 10, .hw_value = 8, .hw_value_short = 8, },
  167. { .bitrate = 36 * 10, .hw_value = 9, .hw_value_short = 9, },
  168. { .bitrate = 48 * 10, .hw_value = 10, .hw_value_short = 10, },
  169. { .bitrate = 54 * 10, .hw_value = 11, .hw_value_short = 11, },
  170. };
  171. #define RATES_24_OFFS 0
  172. #define N_RATES_24 ARRAY_SIZE(iwl_cfg80211_rates)
  173. #define RATES_52_OFFS 4
  174. #define N_RATES_52 (N_RATES_24 - RATES_52_OFFS)
  175. /**
  176. * enum iwl_nvm_channel_flags - channel flags in NVM
  177. * @NVM_CHANNEL_VALID: channel is usable for this SKU/geo
  178. * @NVM_CHANNEL_IBSS: usable as an IBSS channel
  179. * @NVM_CHANNEL_ACTIVE: active scanning allowed
  180. * @NVM_CHANNEL_RADAR: radar detection required
  181. * @NVM_CHANNEL_INDOOR_ONLY: only indoor use is allowed
  182. * @NVM_CHANNEL_GO_CONCURRENT: GO operation is allowed when connected to BSS
  183. * on same channel on 2.4 or same UNII band on 5.2
  184. * @NVM_CHANNEL_UNIFORM: uniform spreading required
  185. * @NVM_CHANNEL_20MHZ: 20 MHz channel okay
  186. * @NVM_CHANNEL_40MHZ: 40 MHz channel okay
  187. * @NVM_CHANNEL_80MHZ: 80 MHz channel okay
  188. * @NVM_CHANNEL_160MHZ: 160 MHz channel okay
  189. * @NVM_CHANNEL_DC_HIGH: DC HIGH required/allowed (?)
  190. */
  191. enum iwl_nvm_channel_flags {
  192. NVM_CHANNEL_VALID = BIT(0),
  193. NVM_CHANNEL_IBSS = BIT(1),
  194. NVM_CHANNEL_ACTIVE = BIT(3),
  195. NVM_CHANNEL_RADAR = BIT(4),
  196. NVM_CHANNEL_INDOOR_ONLY = BIT(5),
  197. NVM_CHANNEL_GO_CONCURRENT = BIT(6),
  198. NVM_CHANNEL_UNIFORM = BIT(7),
  199. NVM_CHANNEL_20MHZ = BIT(8),
  200. NVM_CHANNEL_40MHZ = BIT(9),
  201. NVM_CHANNEL_80MHZ = BIT(10),
  202. NVM_CHANNEL_160MHZ = BIT(11),
  203. NVM_CHANNEL_DC_HIGH = BIT(12),
  204. };
  205. static inline void iwl_nvm_print_channel_flags(struct device *dev, u32 level,
  206. int chan, u16 flags)
  207. {
  208. #define CHECK_AND_PRINT_I(x) \
  209. ((flags & NVM_CHANNEL_##x) ? " " #x : "")
  210. if (!(flags & NVM_CHANNEL_VALID)) {
  211. IWL_DEBUG_DEV(dev, level, "Ch. %d: 0x%x: No traffic\n",
  212. chan, flags);
  213. return;
  214. }
  215. /* Note: already can print up to 101 characters, 110 is the limit! */
  216. IWL_DEBUG_DEV(dev, level,
  217. "Ch. %d: 0x%x:%s%s%s%s%s%s%s%s%s%s%s%s\n",
  218. chan, flags,
  219. CHECK_AND_PRINT_I(VALID),
  220. CHECK_AND_PRINT_I(IBSS),
  221. CHECK_AND_PRINT_I(ACTIVE),
  222. CHECK_AND_PRINT_I(RADAR),
  223. CHECK_AND_PRINT_I(INDOOR_ONLY),
  224. CHECK_AND_PRINT_I(GO_CONCURRENT),
  225. CHECK_AND_PRINT_I(UNIFORM),
  226. CHECK_AND_PRINT_I(20MHZ),
  227. CHECK_AND_PRINT_I(40MHZ),
  228. CHECK_AND_PRINT_I(80MHZ),
  229. CHECK_AND_PRINT_I(160MHZ),
  230. CHECK_AND_PRINT_I(DC_HIGH));
  231. #undef CHECK_AND_PRINT_I
  232. }
  233. static u32 iwl_get_channel_flags(u8 ch_num, int ch_idx, bool is_5ghz,
  234. u16 nvm_flags, const struct iwl_cfg *cfg)
  235. {
  236. u32 flags = IEEE80211_CHAN_NO_HT40;
  237. u32 last_5ghz_ht = LAST_5GHZ_HT;
  238. if (cfg->nvm_type == IWL_NVM_EXT)
  239. last_5ghz_ht = LAST_5GHZ_HT_FAMILY_8000;
  240. if (!is_5ghz && (nvm_flags & NVM_CHANNEL_40MHZ)) {
  241. if (ch_num <= LAST_2GHZ_HT_PLUS)
  242. flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  243. if (ch_num >= FIRST_2GHZ_HT_MINUS)
  244. flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  245. } else if (ch_num <= last_5ghz_ht && (nvm_flags & NVM_CHANNEL_40MHZ)) {
  246. if ((ch_idx - NUM_2GHZ_CHANNELS) % 2 == 0)
  247. flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  248. else
  249. flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  250. }
  251. if (!(nvm_flags & NVM_CHANNEL_80MHZ))
  252. flags |= IEEE80211_CHAN_NO_80MHZ;
  253. if (!(nvm_flags & NVM_CHANNEL_160MHZ))
  254. flags |= IEEE80211_CHAN_NO_160MHZ;
  255. if (!(nvm_flags & NVM_CHANNEL_IBSS))
  256. flags |= IEEE80211_CHAN_NO_IR;
  257. if (!(nvm_flags & NVM_CHANNEL_ACTIVE))
  258. flags |= IEEE80211_CHAN_NO_IR;
  259. if (nvm_flags & NVM_CHANNEL_RADAR)
  260. flags |= IEEE80211_CHAN_RADAR;
  261. if (nvm_flags & NVM_CHANNEL_INDOOR_ONLY)
  262. flags |= IEEE80211_CHAN_INDOOR_ONLY;
  263. /* Set the GO concurrent flag only in case that NO_IR is set.
  264. * Otherwise it is meaningless
  265. */
  266. if ((nvm_flags & NVM_CHANNEL_GO_CONCURRENT) &&
  267. (flags & IEEE80211_CHAN_NO_IR))
  268. flags |= IEEE80211_CHAN_IR_CONCURRENT;
  269. return flags;
  270. }
  271. static int iwl_init_channel_map(struct device *dev, const struct iwl_cfg *cfg,
  272. struct iwl_nvm_data *data,
  273. const __le16 * const nvm_ch_flags,
  274. u32 sbands_flags)
  275. {
  276. int ch_idx;
  277. int n_channels = 0;
  278. struct ieee80211_channel *channel;
  279. u16 ch_flags;
  280. int num_of_ch, num_2ghz_channels;
  281. const u8 *nvm_chan;
  282. if (cfg->nvm_type != IWL_NVM_EXT) {
  283. num_of_ch = IWL_NVM_NUM_CHANNELS;
  284. nvm_chan = &iwl_nvm_channels[0];
  285. num_2ghz_channels = NUM_2GHZ_CHANNELS;
  286. } else {
  287. num_of_ch = IWL_NVM_NUM_CHANNELS_EXT;
  288. nvm_chan = &iwl_ext_nvm_channels[0];
  289. num_2ghz_channels = NUM_2GHZ_CHANNELS_EXT;
  290. }
  291. for (ch_idx = 0; ch_idx < num_of_ch; ch_idx++) {
  292. bool is_5ghz = (ch_idx >= num_2ghz_channels);
  293. ch_flags = __le16_to_cpup(nvm_ch_flags + ch_idx);
  294. if (is_5ghz && !data->sku_cap_band_52ghz_enable)
  295. continue;
  296. /* workaround to disable wide channels in 5GHz */
  297. if ((sbands_flags & IWL_NVM_SBANDS_FLAGS_NO_WIDE_IN_5GHZ) &&
  298. is_5ghz) {
  299. ch_flags &= ~(NVM_CHANNEL_40MHZ |
  300. NVM_CHANNEL_80MHZ |
  301. NVM_CHANNEL_160MHZ);
  302. }
  303. if (ch_flags & NVM_CHANNEL_160MHZ)
  304. data->vht160_supported = true;
  305. if (!(sbands_flags & IWL_NVM_SBANDS_FLAGS_LAR) &&
  306. !(ch_flags & NVM_CHANNEL_VALID)) {
  307. /*
  308. * Channels might become valid later if lar is
  309. * supported, hence we still want to add them to
  310. * the list of supported channels to cfg80211.
  311. */
  312. iwl_nvm_print_channel_flags(dev, IWL_DL_EEPROM,
  313. nvm_chan[ch_idx], ch_flags);
  314. continue;
  315. }
  316. channel = &data->channels[n_channels];
  317. n_channels++;
  318. channel->hw_value = nvm_chan[ch_idx];
  319. channel->band = is_5ghz ?
  320. NL80211_BAND_5GHZ : NL80211_BAND_2GHZ;
  321. channel->center_freq =
  322. ieee80211_channel_to_frequency(
  323. channel->hw_value, channel->band);
  324. /* Initialize regulatory-based run-time data */
  325. /*
  326. * Default value - highest tx power value. max_power
  327. * is not used in mvm, and is used for backwards compatibility
  328. */
  329. channel->max_power = IWL_DEFAULT_MAX_TX_POWER;
  330. /* don't put limitations in case we're using LAR */
  331. if (!(sbands_flags & IWL_NVM_SBANDS_FLAGS_LAR))
  332. channel->flags = iwl_get_channel_flags(nvm_chan[ch_idx],
  333. ch_idx, is_5ghz,
  334. ch_flags, cfg);
  335. else
  336. channel->flags = 0;
  337. iwl_nvm_print_channel_flags(dev, IWL_DL_EEPROM,
  338. channel->hw_value, ch_flags);
  339. IWL_DEBUG_EEPROM(dev, "Ch. %d: %ddBm\n",
  340. channel->hw_value, channel->max_power);
  341. }
  342. return n_channels;
  343. }
  344. static void iwl_init_vht_hw_capab(const struct iwl_cfg *cfg,
  345. struct iwl_nvm_data *data,
  346. struct ieee80211_sta_vht_cap *vht_cap,
  347. u8 tx_chains, u8 rx_chains)
  348. {
  349. int num_rx_ants = num_of_ant(rx_chains);
  350. int num_tx_ants = num_of_ant(tx_chains);
  351. unsigned int max_ampdu_exponent = (cfg->max_vht_ampdu_exponent ?:
  352. IEEE80211_VHT_MAX_AMPDU_1024K);
  353. vht_cap->vht_supported = true;
  354. vht_cap->cap = IEEE80211_VHT_CAP_SHORT_GI_80 |
  355. IEEE80211_VHT_CAP_RXSTBC_1 |
  356. IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  357. 3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT |
  358. max_ampdu_exponent <<
  359. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  360. if (data->vht160_supported)
  361. vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
  362. IEEE80211_VHT_CAP_SHORT_GI_160;
  363. if (cfg->vht_mu_mimo_supported)
  364. vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
  365. if (cfg->ht_params->ldpc)
  366. vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
  367. if (data->sku_cap_mimo_disabled) {
  368. num_rx_ants = 1;
  369. num_tx_ants = 1;
  370. }
  371. if (num_tx_ants > 1)
  372. vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
  373. else
  374. vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
  375. switch (iwlwifi_mod_params.amsdu_size) {
  376. case IWL_AMSDU_DEF:
  377. if (cfg->mq_rx_supported)
  378. vht_cap->cap |=
  379. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
  380. else
  381. vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
  382. break;
  383. case IWL_AMSDU_4K:
  384. vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
  385. break;
  386. case IWL_AMSDU_8K:
  387. vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991;
  388. break;
  389. case IWL_AMSDU_12K:
  390. vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
  391. break;
  392. default:
  393. break;
  394. }
  395. vht_cap->vht_mcs.rx_mcs_map =
  396. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
  397. IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
  398. IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
  399. IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
  400. IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
  401. IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
  402. IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
  403. IEEE80211_VHT_MCS_NOT_SUPPORTED << 14);
  404. if (num_rx_ants == 1 || cfg->rx_with_siso_diversity) {
  405. vht_cap->cap |= IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN;
  406. /* this works because NOT_SUPPORTED == 3 */
  407. vht_cap->vht_mcs.rx_mcs_map |=
  408. cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << 2);
  409. }
  410. vht_cap->vht_mcs.tx_mcs_map = vht_cap->vht_mcs.rx_mcs_map;
  411. }
  412. static void iwl_init_sbands(struct device *dev, const struct iwl_cfg *cfg,
  413. struct iwl_nvm_data *data,
  414. const __le16 *nvm_ch_flags, u8 tx_chains,
  415. u8 rx_chains, u32 sbands_flags)
  416. {
  417. int n_channels;
  418. int n_used = 0;
  419. struct ieee80211_supported_band *sband;
  420. n_channels = iwl_init_channel_map(dev, cfg, data, nvm_ch_flags,
  421. sbands_flags);
  422. sband = &data->bands[NL80211_BAND_2GHZ];
  423. sband->band = NL80211_BAND_2GHZ;
  424. sband->bitrates = &iwl_cfg80211_rates[RATES_24_OFFS];
  425. sband->n_bitrates = N_RATES_24;
  426. n_used += iwl_init_sband_channels(data, sband, n_channels,
  427. NL80211_BAND_2GHZ);
  428. iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, NL80211_BAND_2GHZ,
  429. tx_chains, rx_chains);
  430. sband = &data->bands[NL80211_BAND_5GHZ];
  431. sband->band = NL80211_BAND_5GHZ;
  432. sband->bitrates = &iwl_cfg80211_rates[RATES_52_OFFS];
  433. sband->n_bitrates = N_RATES_52;
  434. n_used += iwl_init_sband_channels(data, sband, n_channels,
  435. NL80211_BAND_5GHZ);
  436. iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, NL80211_BAND_5GHZ,
  437. tx_chains, rx_chains);
  438. if (data->sku_cap_11ac_enable && !iwlwifi_mod_params.disable_11ac)
  439. iwl_init_vht_hw_capab(cfg, data, &sband->vht_cap,
  440. tx_chains, rx_chains);
  441. if (n_channels != n_used)
  442. IWL_ERR_DEV(dev, "NVM: used only %d of %d channels\n",
  443. n_used, n_channels);
  444. }
  445. static int iwl_get_sku(const struct iwl_cfg *cfg, const __le16 *nvm_sw,
  446. const __le16 *phy_sku)
  447. {
  448. if (cfg->nvm_type != IWL_NVM_EXT)
  449. return le16_to_cpup(nvm_sw + SKU);
  450. return le32_to_cpup((__le32 *)(phy_sku + SKU_FAMILY_8000));
  451. }
  452. static int iwl_get_nvm_version(const struct iwl_cfg *cfg, const __le16 *nvm_sw)
  453. {
  454. if (cfg->nvm_type != IWL_NVM_EXT)
  455. return le16_to_cpup(nvm_sw + NVM_VERSION);
  456. else
  457. return le32_to_cpup((__le32 *)(nvm_sw +
  458. NVM_VERSION_EXT_NVM));
  459. }
  460. static int iwl_get_radio_cfg(const struct iwl_cfg *cfg, const __le16 *nvm_sw,
  461. const __le16 *phy_sku)
  462. {
  463. if (cfg->nvm_type != IWL_NVM_EXT)
  464. return le16_to_cpup(nvm_sw + RADIO_CFG);
  465. return le32_to_cpup((__le32 *)(phy_sku + RADIO_CFG_FAMILY_EXT_NVM));
  466. }
  467. static int iwl_get_n_hw_addrs(const struct iwl_cfg *cfg, const __le16 *nvm_sw)
  468. {
  469. int n_hw_addr;
  470. if (cfg->nvm_type != IWL_NVM_EXT)
  471. return le16_to_cpup(nvm_sw + N_HW_ADDRS);
  472. n_hw_addr = le32_to_cpup((__le32 *)(nvm_sw + N_HW_ADDRS_FAMILY_8000));
  473. return n_hw_addr & N_HW_ADDR_MASK;
  474. }
  475. static void iwl_set_radio_cfg(const struct iwl_cfg *cfg,
  476. struct iwl_nvm_data *data,
  477. u32 radio_cfg)
  478. {
  479. if (cfg->nvm_type != IWL_NVM_EXT) {
  480. data->radio_cfg_type = NVM_RF_CFG_TYPE_MSK(radio_cfg);
  481. data->radio_cfg_step = NVM_RF_CFG_STEP_MSK(radio_cfg);
  482. data->radio_cfg_dash = NVM_RF_CFG_DASH_MSK(radio_cfg);
  483. data->radio_cfg_pnum = NVM_RF_CFG_PNUM_MSK(radio_cfg);
  484. return;
  485. }
  486. /* set the radio configuration for family 8000 */
  487. data->radio_cfg_type = EXT_NVM_RF_CFG_TYPE_MSK(radio_cfg);
  488. data->radio_cfg_step = EXT_NVM_RF_CFG_STEP_MSK(radio_cfg);
  489. data->radio_cfg_dash = EXT_NVM_RF_CFG_DASH_MSK(radio_cfg);
  490. data->radio_cfg_pnum = EXT_NVM_RF_CFG_FLAVOR_MSK(radio_cfg);
  491. data->valid_tx_ant = EXT_NVM_RF_CFG_TX_ANT_MSK(radio_cfg);
  492. data->valid_rx_ant = EXT_NVM_RF_CFG_RX_ANT_MSK(radio_cfg);
  493. }
  494. static void iwl_flip_hw_address(__le32 mac_addr0, __le32 mac_addr1, u8 *dest)
  495. {
  496. const u8 *hw_addr;
  497. hw_addr = (const u8 *)&mac_addr0;
  498. dest[0] = hw_addr[3];
  499. dest[1] = hw_addr[2];
  500. dest[2] = hw_addr[1];
  501. dest[3] = hw_addr[0];
  502. hw_addr = (const u8 *)&mac_addr1;
  503. dest[4] = hw_addr[1];
  504. dest[5] = hw_addr[0];
  505. }
  506. static void iwl_set_hw_address_from_csr(struct iwl_trans *trans,
  507. struct iwl_nvm_data *data)
  508. {
  509. __le32 mac_addr0 =
  510. cpu_to_le32(iwl_read32(trans,
  511. trans->cfg->csr->mac_addr0_strap));
  512. __le32 mac_addr1 =
  513. cpu_to_le32(iwl_read32(trans,
  514. trans->cfg->csr->mac_addr1_strap));
  515. iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
  516. /*
  517. * If the OEM fused a valid address, use it instead of the one in the
  518. * OTP
  519. */
  520. if (is_valid_ether_addr(data->hw_addr))
  521. return;
  522. mac_addr0 = cpu_to_le32(iwl_read32(trans,
  523. trans->cfg->csr->mac_addr0_otp));
  524. mac_addr1 = cpu_to_le32(iwl_read32(trans,
  525. trans->cfg->csr->mac_addr1_otp));
  526. iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
  527. }
  528. static void iwl_set_hw_address_family_8000(struct iwl_trans *trans,
  529. const struct iwl_cfg *cfg,
  530. struct iwl_nvm_data *data,
  531. const __le16 *mac_override,
  532. const __be16 *nvm_hw)
  533. {
  534. const u8 *hw_addr;
  535. if (mac_override) {
  536. static const u8 reserved_mac[] = {
  537. 0x02, 0xcc, 0xaa, 0xff, 0xee, 0x00
  538. };
  539. hw_addr = (const u8 *)(mac_override +
  540. MAC_ADDRESS_OVERRIDE_EXT_NVM);
  541. /*
  542. * Store the MAC address from MAO section.
  543. * No byte swapping is required in MAO section
  544. */
  545. memcpy(data->hw_addr, hw_addr, ETH_ALEN);
  546. /*
  547. * Force the use of the OTP MAC address in case of reserved MAC
  548. * address in the NVM, or if address is given but invalid.
  549. */
  550. if (is_valid_ether_addr(data->hw_addr) &&
  551. memcmp(reserved_mac, hw_addr, ETH_ALEN) != 0)
  552. return;
  553. IWL_ERR(trans,
  554. "mac address from nvm override section is not valid\n");
  555. }
  556. if (nvm_hw) {
  557. /* read the mac address from WFMP registers */
  558. __le32 mac_addr0 = cpu_to_le32(iwl_trans_read_prph(trans,
  559. WFMP_MAC_ADDR_0));
  560. __le32 mac_addr1 = cpu_to_le32(iwl_trans_read_prph(trans,
  561. WFMP_MAC_ADDR_1));
  562. iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
  563. return;
  564. }
  565. IWL_ERR(trans, "mac address is not found\n");
  566. }
  567. static int iwl_set_hw_address(struct iwl_trans *trans,
  568. const struct iwl_cfg *cfg,
  569. struct iwl_nvm_data *data, const __be16 *nvm_hw,
  570. const __le16 *mac_override)
  571. {
  572. if (cfg->mac_addr_from_csr) {
  573. iwl_set_hw_address_from_csr(trans, data);
  574. } else if (cfg->nvm_type != IWL_NVM_EXT) {
  575. const u8 *hw_addr = (const u8 *)(nvm_hw + HW_ADDR);
  576. /* The byte order is little endian 16 bit, meaning 214365 */
  577. data->hw_addr[0] = hw_addr[1];
  578. data->hw_addr[1] = hw_addr[0];
  579. data->hw_addr[2] = hw_addr[3];
  580. data->hw_addr[3] = hw_addr[2];
  581. data->hw_addr[4] = hw_addr[5];
  582. data->hw_addr[5] = hw_addr[4];
  583. } else {
  584. iwl_set_hw_address_family_8000(trans, cfg, data,
  585. mac_override, nvm_hw);
  586. }
  587. if (!is_valid_ether_addr(data->hw_addr)) {
  588. IWL_ERR(trans, "no valid mac address was found\n");
  589. return -EINVAL;
  590. }
  591. IWL_INFO(trans, "base HW address: %pM\n", data->hw_addr);
  592. return 0;
  593. }
  594. static bool
  595. iwl_nvm_no_wide_in_5ghz(struct device *dev, const struct iwl_cfg *cfg,
  596. const __be16 *nvm_hw)
  597. {
  598. /*
  599. * Workaround a bug in Indonesia SKUs where the regulatory in
  600. * some 7000-family OTPs erroneously allow wide channels in
  601. * 5GHz. To check for Indonesia, we take the SKU value from
  602. * bits 1-4 in the subsystem ID and check if it is either 5 or
  603. * 9. In those cases, we need to force-disable wide channels
  604. * in 5GHz otherwise the FW will throw a sysassert when we try
  605. * to use them.
  606. */
  607. if (cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  608. /*
  609. * Unlike the other sections in the NVM, the hw
  610. * section uses big-endian.
  611. */
  612. u16 subsystem_id = be16_to_cpup(nvm_hw + SUBSYSTEM_ID);
  613. u8 sku = (subsystem_id & 0x1e) >> 1;
  614. if (sku == 5 || sku == 9) {
  615. IWL_DEBUG_EEPROM(dev,
  616. "disabling wide channels in 5GHz (0x%0x %d)\n",
  617. subsystem_id, sku);
  618. return true;
  619. }
  620. }
  621. return false;
  622. }
  623. struct iwl_nvm_data *
  624. iwl_parse_nvm_data(struct iwl_trans *trans, const struct iwl_cfg *cfg,
  625. const __be16 *nvm_hw, const __le16 *nvm_sw,
  626. const __le16 *nvm_calib, const __le16 *regulatory,
  627. const __le16 *mac_override, const __le16 *phy_sku,
  628. u8 tx_chains, u8 rx_chains, bool lar_fw_supported)
  629. {
  630. struct device *dev = trans->dev;
  631. struct iwl_nvm_data *data;
  632. bool lar_enabled;
  633. u32 sku, radio_cfg;
  634. u32 sbands_flags = 0;
  635. u16 lar_config;
  636. const __le16 *ch_section;
  637. if (cfg->nvm_type != IWL_NVM_EXT)
  638. data = kzalloc(sizeof(*data) +
  639. sizeof(struct ieee80211_channel) *
  640. IWL_NVM_NUM_CHANNELS,
  641. GFP_KERNEL);
  642. else
  643. data = kzalloc(sizeof(*data) +
  644. sizeof(struct ieee80211_channel) *
  645. IWL_NVM_NUM_CHANNELS_EXT,
  646. GFP_KERNEL);
  647. if (!data)
  648. return NULL;
  649. data->nvm_version = iwl_get_nvm_version(cfg, nvm_sw);
  650. radio_cfg = iwl_get_radio_cfg(cfg, nvm_sw, phy_sku);
  651. iwl_set_radio_cfg(cfg, data, radio_cfg);
  652. if (data->valid_tx_ant)
  653. tx_chains &= data->valid_tx_ant;
  654. if (data->valid_rx_ant)
  655. rx_chains &= data->valid_rx_ant;
  656. sku = iwl_get_sku(cfg, nvm_sw, phy_sku);
  657. data->sku_cap_band_24ghz_enable = sku & NVM_SKU_CAP_BAND_24GHZ;
  658. data->sku_cap_band_52ghz_enable = sku & NVM_SKU_CAP_BAND_52GHZ;
  659. data->sku_cap_11n_enable = sku & NVM_SKU_CAP_11N_ENABLE;
  660. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_ALL)
  661. data->sku_cap_11n_enable = false;
  662. data->sku_cap_11ac_enable = data->sku_cap_11n_enable &&
  663. (sku & NVM_SKU_CAP_11AC_ENABLE);
  664. data->sku_cap_mimo_disabled = sku & NVM_SKU_CAP_MIMO_DISABLE;
  665. data->n_hw_addrs = iwl_get_n_hw_addrs(cfg, nvm_sw);
  666. if (cfg->nvm_type != IWL_NVM_EXT) {
  667. /* Checking for required sections */
  668. if (!nvm_calib) {
  669. IWL_ERR(trans,
  670. "Can't parse empty Calib NVM sections\n");
  671. kfree(data);
  672. return NULL;
  673. }
  674. ch_section = cfg->nvm_type == IWL_NVM_SDP ?
  675. &regulatory[NVM_CHANNELS_SDP] :
  676. &nvm_sw[NVM_CHANNELS];
  677. /* in family 8000 Xtal calibration values moved to OTP */
  678. data->xtal_calib[0] = *(nvm_calib + XTAL_CALIB);
  679. data->xtal_calib[1] = *(nvm_calib + XTAL_CALIB + 1);
  680. lar_enabled = true;
  681. } else {
  682. u16 lar_offset = data->nvm_version < 0xE39 ?
  683. NVM_LAR_OFFSET_OLD :
  684. NVM_LAR_OFFSET;
  685. lar_config = le16_to_cpup(regulatory + lar_offset);
  686. data->lar_enabled = !!(lar_config &
  687. NVM_LAR_ENABLED);
  688. lar_enabled = data->lar_enabled;
  689. ch_section = &regulatory[NVM_CHANNELS_EXTENDED];
  690. }
  691. /* If no valid mac address was found - bail out */
  692. if (iwl_set_hw_address(trans, cfg, data, nvm_hw, mac_override)) {
  693. kfree(data);
  694. return NULL;
  695. }
  696. if (lar_fw_supported && lar_enabled)
  697. sbands_flags |= IWL_NVM_SBANDS_FLAGS_LAR;
  698. if (iwl_nvm_no_wide_in_5ghz(dev, cfg, nvm_hw))
  699. sbands_flags |= IWL_NVM_SBANDS_FLAGS_NO_WIDE_IN_5GHZ;
  700. iwl_init_sbands(dev, cfg, data, ch_section, tx_chains, rx_chains,
  701. sbands_flags);
  702. data->calib_version = 255;
  703. return data;
  704. }
  705. IWL_EXPORT_SYMBOL(iwl_parse_nvm_data);
  706. static u32 iwl_nvm_get_regdom_bw_flags(const u8 *nvm_chan,
  707. int ch_idx, u16 nvm_flags,
  708. const struct iwl_cfg *cfg)
  709. {
  710. u32 flags = NL80211_RRF_NO_HT40;
  711. u32 last_5ghz_ht = LAST_5GHZ_HT;
  712. if (cfg->nvm_type == IWL_NVM_EXT)
  713. last_5ghz_ht = LAST_5GHZ_HT_FAMILY_8000;
  714. if (ch_idx < NUM_2GHZ_CHANNELS &&
  715. (nvm_flags & NVM_CHANNEL_40MHZ)) {
  716. if (nvm_chan[ch_idx] <= LAST_2GHZ_HT_PLUS)
  717. flags &= ~NL80211_RRF_NO_HT40PLUS;
  718. if (nvm_chan[ch_idx] >= FIRST_2GHZ_HT_MINUS)
  719. flags &= ~NL80211_RRF_NO_HT40MINUS;
  720. } else if (nvm_chan[ch_idx] <= last_5ghz_ht &&
  721. (nvm_flags & NVM_CHANNEL_40MHZ)) {
  722. if ((ch_idx - NUM_2GHZ_CHANNELS) % 2 == 0)
  723. flags &= ~NL80211_RRF_NO_HT40PLUS;
  724. else
  725. flags &= ~NL80211_RRF_NO_HT40MINUS;
  726. }
  727. if (!(nvm_flags & NVM_CHANNEL_80MHZ))
  728. flags |= NL80211_RRF_NO_80MHZ;
  729. if (!(nvm_flags & NVM_CHANNEL_160MHZ))
  730. flags |= NL80211_RRF_NO_160MHZ;
  731. if (!(nvm_flags & NVM_CHANNEL_ACTIVE))
  732. flags |= NL80211_RRF_NO_IR;
  733. if (nvm_flags & NVM_CHANNEL_RADAR)
  734. flags |= NL80211_RRF_DFS;
  735. if (nvm_flags & NVM_CHANNEL_INDOOR_ONLY)
  736. flags |= NL80211_RRF_NO_OUTDOOR;
  737. /* Set the GO concurrent flag only in case that NO_IR is set.
  738. * Otherwise it is meaningless
  739. */
  740. if ((nvm_flags & NVM_CHANNEL_GO_CONCURRENT) &&
  741. (flags & NL80211_RRF_NO_IR))
  742. flags |= NL80211_RRF_GO_CONCURRENT;
  743. return flags;
  744. }
  745. struct regdb_ptrs {
  746. struct ieee80211_wmm_rule *rule;
  747. u32 token;
  748. };
  749. struct ieee80211_regdomain *
  750. iwl_parse_nvm_mcc_info(struct device *dev, const struct iwl_cfg *cfg,
  751. int num_of_ch, __le32 *channels, u16 fw_mcc,
  752. u16 geo_info)
  753. {
  754. int ch_idx;
  755. u16 ch_flags;
  756. u32 reg_rule_flags, prev_reg_rule_flags = 0;
  757. const u8 *nvm_chan = cfg->nvm_type == IWL_NVM_EXT ?
  758. iwl_ext_nvm_channels : iwl_nvm_channels;
  759. struct ieee80211_regdomain *regd, *copy_rd;
  760. int size_of_regd, regd_to_copy, wmms_to_copy;
  761. int size_of_wmms = 0;
  762. struct ieee80211_reg_rule *rule;
  763. struct ieee80211_wmm_rule *wmm_rule, *d_wmm, *s_wmm;
  764. struct regdb_ptrs *regdb_ptrs;
  765. enum nl80211_band band;
  766. int center_freq, prev_center_freq = 0;
  767. int valid_rules = 0, n_wmms = 0;
  768. int i;
  769. bool new_rule;
  770. int max_num_ch = cfg->nvm_type == IWL_NVM_EXT ?
  771. IWL_NVM_NUM_CHANNELS_EXT : IWL_NVM_NUM_CHANNELS;
  772. if (WARN_ON_ONCE(num_of_ch > NL80211_MAX_SUPP_REG_RULES))
  773. return ERR_PTR(-EINVAL);
  774. if (WARN_ON(num_of_ch > max_num_ch))
  775. num_of_ch = max_num_ch;
  776. IWL_DEBUG_DEV(dev, IWL_DL_LAR, "building regdom for %d channels\n",
  777. num_of_ch);
  778. /* build a regdomain rule for every valid channel */
  779. size_of_regd =
  780. sizeof(struct ieee80211_regdomain) +
  781. num_of_ch * sizeof(struct ieee80211_reg_rule);
  782. if (geo_info & GEO_WMM_ETSI_5GHZ_INFO)
  783. size_of_wmms =
  784. num_of_ch * sizeof(struct ieee80211_wmm_rule);
  785. regd = kzalloc(size_of_regd + size_of_wmms, GFP_KERNEL);
  786. if (!regd)
  787. return ERR_PTR(-ENOMEM);
  788. regdb_ptrs = kcalloc(num_of_ch, sizeof(*regdb_ptrs), GFP_KERNEL);
  789. if (!regdb_ptrs) {
  790. copy_rd = ERR_PTR(-ENOMEM);
  791. goto out;
  792. }
  793. /* set alpha2 from FW. */
  794. regd->alpha2[0] = fw_mcc >> 8;
  795. regd->alpha2[1] = fw_mcc & 0xff;
  796. wmm_rule = (struct ieee80211_wmm_rule *)((u8 *)regd + size_of_regd);
  797. for (ch_idx = 0; ch_idx < num_of_ch; ch_idx++) {
  798. ch_flags = (u16)__le32_to_cpup(channels + ch_idx);
  799. band = (ch_idx < NUM_2GHZ_CHANNELS) ?
  800. NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
  801. center_freq = ieee80211_channel_to_frequency(nvm_chan[ch_idx],
  802. band);
  803. new_rule = false;
  804. if (!(ch_flags & NVM_CHANNEL_VALID)) {
  805. iwl_nvm_print_channel_flags(dev, IWL_DL_LAR,
  806. nvm_chan[ch_idx], ch_flags);
  807. continue;
  808. }
  809. reg_rule_flags = iwl_nvm_get_regdom_bw_flags(nvm_chan, ch_idx,
  810. ch_flags, cfg);
  811. /* we can't continue the same rule */
  812. if (ch_idx == 0 || prev_reg_rule_flags != reg_rule_flags ||
  813. center_freq - prev_center_freq > 20) {
  814. valid_rules++;
  815. new_rule = true;
  816. }
  817. rule = &regd->reg_rules[valid_rules - 1];
  818. if (new_rule)
  819. rule->freq_range.start_freq_khz =
  820. MHZ_TO_KHZ(center_freq - 10);
  821. rule->freq_range.end_freq_khz = MHZ_TO_KHZ(center_freq + 10);
  822. /* this doesn't matter - not used by FW */
  823. rule->power_rule.max_antenna_gain = DBI_TO_MBI(6);
  824. rule->power_rule.max_eirp =
  825. DBM_TO_MBM(IWL_DEFAULT_MAX_TX_POWER);
  826. rule->flags = reg_rule_flags;
  827. /* rely on auto-calculation to merge BW of contiguous chans */
  828. rule->flags |= NL80211_RRF_AUTO_BW;
  829. rule->freq_range.max_bandwidth_khz = 0;
  830. prev_center_freq = center_freq;
  831. prev_reg_rule_flags = reg_rule_flags;
  832. iwl_nvm_print_channel_flags(dev, IWL_DL_LAR,
  833. nvm_chan[ch_idx], ch_flags);
  834. if (!(geo_info & GEO_WMM_ETSI_5GHZ_INFO) ||
  835. band == NL80211_BAND_2GHZ)
  836. continue;
  837. if (!reg_query_regdb_wmm(regd->alpha2, center_freq,
  838. &regdb_ptrs[n_wmms].token, wmm_rule)) {
  839. /* Add only new rules */
  840. for (i = 0; i < n_wmms; i++) {
  841. if (regdb_ptrs[i].token ==
  842. regdb_ptrs[n_wmms].token) {
  843. rule->wmm_rule = regdb_ptrs[i].rule;
  844. break;
  845. }
  846. }
  847. if (i == n_wmms) {
  848. rule->wmm_rule = wmm_rule;
  849. regdb_ptrs[n_wmms++].rule = wmm_rule;
  850. wmm_rule++;
  851. }
  852. }
  853. }
  854. regd->n_reg_rules = valid_rules;
  855. regd->n_wmm_rules = n_wmms;
  856. /*
  857. * Narrow down regdom for unused regulatory rules to prevent hole
  858. * between reg rules to wmm rules.
  859. */
  860. regd_to_copy = sizeof(struct ieee80211_regdomain) +
  861. valid_rules * sizeof(struct ieee80211_reg_rule);
  862. wmms_to_copy = sizeof(struct ieee80211_wmm_rule) * n_wmms;
  863. copy_rd = kzalloc(regd_to_copy + wmms_to_copy, GFP_KERNEL);
  864. if (!copy_rd) {
  865. copy_rd = ERR_PTR(-ENOMEM);
  866. goto out;
  867. }
  868. memcpy(copy_rd, regd, regd_to_copy);
  869. memcpy((u8 *)copy_rd + regd_to_copy, (u8 *)regd + size_of_regd,
  870. wmms_to_copy);
  871. d_wmm = (struct ieee80211_wmm_rule *)((u8 *)copy_rd + regd_to_copy);
  872. s_wmm = (struct ieee80211_wmm_rule *)((u8 *)regd + size_of_regd);
  873. for (i = 0; i < regd->n_reg_rules; i++) {
  874. if (!regd->reg_rules[i].wmm_rule)
  875. continue;
  876. copy_rd->reg_rules[i].wmm_rule = d_wmm +
  877. (regd->reg_rules[i].wmm_rule - s_wmm);
  878. }
  879. out:
  880. kfree(regdb_ptrs);
  881. kfree(regd);
  882. return copy_rd;
  883. }
  884. IWL_EXPORT_SYMBOL(iwl_parse_nvm_mcc_info);
  885. #define IWL_MAX_NVM_SECTION_SIZE 0x1b58
  886. #define IWL_MAX_EXT_NVM_SECTION_SIZE 0x1ffc
  887. #define MAX_NVM_FILE_LEN 16384
  888. void iwl_nvm_fixups(u32 hw_id, unsigned int section, u8 *data,
  889. unsigned int len)
  890. {
  891. #define IWL_4165_DEVICE_ID 0x5501
  892. #define NVM_SKU_CAP_MIMO_DISABLE BIT(5)
  893. if (section == NVM_SECTION_TYPE_PHY_SKU &&
  894. hw_id == IWL_4165_DEVICE_ID && data && len >= 5 &&
  895. (data[4] & NVM_SKU_CAP_MIMO_DISABLE))
  896. /* OTP 0x52 bug work around: it's a 1x1 device */
  897. data[3] = ANT_B | (ANT_B << 4);
  898. }
  899. IWL_EXPORT_SYMBOL(iwl_nvm_fixups);
  900. /*
  901. * Reads external NVM from a file into mvm->nvm_sections
  902. *
  903. * HOW TO CREATE THE NVM FILE FORMAT:
  904. * ------------------------------
  905. * 1. create hex file, format:
  906. * 3800 -> header
  907. * 0000 -> header
  908. * 5a40 -> data
  909. *
  910. * rev - 6 bit (word1)
  911. * len - 10 bit (word1)
  912. * id - 4 bit (word2)
  913. * rsv - 12 bit (word2)
  914. *
  915. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  916. *
  917. * 3. create binary file from the hex file
  918. *
  919. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  920. */
  921. int iwl_read_external_nvm(struct iwl_trans *trans,
  922. const char *nvm_file_name,
  923. struct iwl_nvm_section *nvm_sections)
  924. {
  925. int ret, section_size;
  926. u16 section_id;
  927. const struct firmware *fw_entry;
  928. const struct {
  929. __le16 word1;
  930. __le16 word2;
  931. u8 data[];
  932. } *file_sec;
  933. const u8 *eof;
  934. u8 *temp;
  935. int max_section_size;
  936. const __le32 *dword_buff;
  937. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  938. #define NVM_WORD2_ID(x) (x >> 12)
  939. #define EXT_NVM_WORD2_LEN(x) (2 * (((x) & 0xFF) << 8 | (x) >> 8))
  940. #define EXT_NVM_WORD1_ID(x) ((x) >> 4)
  941. #define NVM_HEADER_0 (0x2A504C54)
  942. #define NVM_HEADER_1 (0x4E564D2A)
  943. #define NVM_HEADER_SIZE (4 * sizeof(u32))
  944. IWL_DEBUG_EEPROM(trans->dev, "Read from external NVM\n");
  945. /* Maximal size depends on NVM version */
  946. if (trans->cfg->nvm_type != IWL_NVM_EXT)
  947. max_section_size = IWL_MAX_NVM_SECTION_SIZE;
  948. else
  949. max_section_size = IWL_MAX_EXT_NVM_SECTION_SIZE;
  950. /*
  951. * Obtain NVM image via request_firmware. Since we already used
  952. * request_firmware_nowait() for the firmware binary load and only
  953. * get here after that we assume the NVM request can be satisfied
  954. * synchronously.
  955. */
  956. ret = request_firmware(&fw_entry, nvm_file_name, trans->dev);
  957. if (ret) {
  958. IWL_ERR(trans, "ERROR: %s isn't available %d\n",
  959. nvm_file_name, ret);
  960. return ret;
  961. }
  962. IWL_INFO(trans, "Loaded NVM file %s (%zu bytes)\n",
  963. nvm_file_name, fw_entry->size);
  964. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  965. IWL_ERR(trans, "NVM file too large\n");
  966. ret = -EINVAL;
  967. goto out;
  968. }
  969. eof = fw_entry->data + fw_entry->size;
  970. dword_buff = (__le32 *)fw_entry->data;
  971. /* some NVM file will contain a header.
  972. * The header is identified by 2 dwords header as follow:
  973. * dword[0] = 0x2A504C54
  974. * dword[1] = 0x4E564D2A
  975. *
  976. * This header must be skipped when providing the NVM data to the FW.
  977. */
  978. if (fw_entry->size > NVM_HEADER_SIZE &&
  979. dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
  980. dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
  981. file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
  982. IWL_INFO(trans, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
  983. IWL_INFO(trans, "NVM Manufacturing date %08X\n",
  984. le32_to_cpu(dword_buff[3]));
  985. /* nvm file validation, dword_buff[2] holds the file version */
  986. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 &&
  987. CSR_HW_REV_STEP(trans->hw_rev) == SILICON_C_STEP &&
  988. le32_to_cpu(dword_buff[2]) < 0xE4A) {
  989. ret = -EFAULT;
  990. goto out;
  991. }
  992. } else {
  993. file_sec = (void *)fw_entry->data;
  994. }
  995. while (true) {
  996. if (file_sec->data > eof) {
  997. IWL_ERR(trans,
  998. "ERROR - NVM file too short for section header\n");
  999. ret = -EINVAL;
  1000. break;
  1001. }
  1002. /* check for EOF marker */
  1003. if (!file_sec->word1 && !file_sec->word2) {
  1004. ret = 0;
  1005. break;
  1006. }
  1007. if (trans->cfg->nvm_type != IWL_NVM_EXT) {
  1008. section_size =
  1009. 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  1010. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  1011. } else {
  1012. section_size = 2 * EXT_NVM_WORD2_LEN(
  1013. le16_to_cpu(file_sec->word2));
  1014. section_id = EXT_NVM_WORD1_ID(
  1015. le16_to_cpu(file_sec->word1));
  1016. }
  1017. if (section_size > max_section_size) {
  1018. IWL_ERR(trans, "ERROR - section too large (%d)\n",
  1019. section_size);
  1020. ret = -EINVAL;
  1021. break;
  1022. }
  1023. if (!section_size) {
  1024. IWL_ERR(trans, "ERROR - section empty\n");
  1025. ret = -EINVAL;
  1026. break;
  1027. }
  1028. if (file_sec->data + section_size > eof) {
  1029. IWL_ERR(trans,
  1030. "ERROR - NVM file too short for section (%d bytes)\n",
  1031. section_size);
  1032. ret = -EINVAL;
  1033. break;
  1034. }
  1035. if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
  1036. "Invalid NVM section ID %d\n", section_id)) {
  1037. ret = -EINVAL;
  1038. break;
  1039. }
  1040. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  1041. if (!temp) {
  1042. ret = -ENOMEM;
  1043. break;
  1044. }
  1045. iwl_nvm_fixups(trans->hw_id, section_id, temp, section_size);
  1046. kfree(nvm_sections[section_id].data);
  1047. nvm_sections[section_id].data = temp;
  1048. nvm_sections[section_id].length = section_size;
  1049. /* advance to the next section */
  1050. file_sec = (void *)(file_sec->data + section_size);
  1051. }
  1052. out:
  1053. release_firmware(fw_entry);
  1054. return ret;
  1055. }
  1056. IWL_EXPORT_SYMBOL(iwl_read_external_nvm);
  1057. struct iwl_nvm_data *iwl_get_nvm(struct iwl_trans *trans,
  1058. const struct iwl_fw *fw)
  1059. {
  1060. struct iwl_nvm_get_info cmd = {};
  1061. struct iwl_nvm_get_info_rsp *rsp;
  1062. struct iwl_nvm_data *nvm;
  1063. struct iwl_host_cmd hcmd = {
  1064. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  1065. .data = { &cmd, },
  1066. .len = { sizeof(cmd) },
  1067. .id = WIDE_ID(REGULATORY_AND_NVM_GROUP, NVM_GET_INFO)
  1068. };
  1069. int ret;
  1070. bool lar_fw_supported = !iwlwifi_mod_params.lar_disable &&
  1071. fw_has_capa(&fw->ucode_capa,
  1072. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  1073. u32 mac_flags;
  1074. u32 sbands_flags = 0;
  1075. ret = iwl_trans_send_cmd(trans, &hcmd);
  1076. if (ret)
  1077. return ERR_PTR(ret);
  1078. if (WARN(iwl_rx_packet_payload_len(hcmd.resp_pkt) != sizeof(*rsp),
  1079. "Invalid payload len in NVM response from FW %d",
  1080. iwl_rx_packet_payload_len(hcmd.resp_pkt))) {
  1081. ret = -EINVAL;
  1082. goto out;
  1083. }
  1084. rsp = (void *)hcmd.resp_pkt->data;
  1085. if (le32_to_cpu(rsp->general.flags) & NVM_GENERAL_FLAGS_EMPTY_OTP)
  1086. IWL_INFO(trans, "OTP is empty\n");
  1087. nvm = kzalloc(sizeof(*nvm) +
  1088. sizeof(struct ieee80211_channel) * IWL_NUM_CHANNELS,
  1089. GFP_KERNEL);
  1090. if (!nvm) {
  1091. ret = -ENOMEM;
  1092. goto out;
  1093. }
  1094. iwl_set_hw_address_from_csr(trans, nvm);
  1095. /* TODO: if platform NVM has MAC address - override it here */
  1096. if (!is_valid_ether_addr(nvm->hw_addr)) {
  1097. IWL_ERR(trans, "no valid mac address was found\n");
  1098. ret = -EINVAL;
  1099. goto err_free;
  1100. }
  1101. IWL_INFO(trans, "base HW address: %pM\n", nvm->hw_addr);
  1102. /* Initialize general data */
  1103. nvm->nvm_version = le16_to_cpu(rsp->general.nvm_version);
  1104. /* Initialize MAC sku data */
  1105. mac_flags = le32_to_cpu(rsp->mac_sku.mac_sku_flags);
  1106. nvm->sku_cap_11ac_enable =
  1107. !!(mac_flags & NVM_MAC_SKU_FLAGS_802_11AC_ENABLED);
  1108. nvm->sku_cap_11n_enable =
  1109. !!(mac_flags & NVM_MAC_SKU_FLAGS_802_11N_ENABLED);
  1110. nvm->sku_cap_band_24ghz_enable =
  1111. !!(mac_flags & NVM_MAC_SKU_FLAGS_BAND_2_4_ENABLED);
  1112. nvm->sku_cap_band_52ghz_enable =
  1113. !!(mac_flags & NVM_MAC_SKU_FLAGS_BAND_5_2_ENABLED);
  1114. nvm->sku_cap_mimo_disabled =
  1115. !!(mac_flags & NVM_MAC_SKU_FLAGS_MIMO_DISABLED);
  1116. /* Initialize PHY sku data */
  1117. nvm->valid_tx_ant = (u8)le32_to_cpu(rsp->phy_sku.tx_chains);
  1118. nvm->valid_rx_ant = (u8)le32_to_cpu(rsp->phy_sku.rx_chains);
  1119. if (le32_to_cpu(rsp->regulatory.lar_enabled) && lar_fw_supported) {
  1120. nvm->lar_enabled = true;
  1121. sbands_flags |= IWL_NVM_SBANDS_FLAGS_LAR;
  1122. }
  1123. iwl_init_sbands(trans->dev, trans->cfg, nvm,
  1124. rsp->regulatory.channel_profile,
  1125. nvm->valid_tx_ant & fw->valid_tx_ant,
  1126. nvm->valid_rx_ant & fw->valid_rx_ant,
  1127. sbands_flags);
  1128. iwl_free_resp(&hcmd);
  1129. return nvm;
  1130. err_free:
  1131. kfree(nvm);
  1132. out:
  1133. iwl_free_resp(&hcmd);
  1134. return ERR_PTR(ret);
  1135. }
  1136. IWL_EXPORT_SYMBOL(iwl_get_nvm);