cfp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP Wireless LAN device driver: Channel, Frequence and Power
  4. *
  5. * Copyright 2011-2020 NXP
  6. */
  7. #include "decl.h"
  8. #include "ioctl.h"
  9. #include "util.h"
  10. #include "fw.h"
  11. #include "main.h"
  12. #include "cfg80211.h"
  13. /* 100mW */
  14. #define MWIFIEX_TX_PWR_DEFAULT 20
  15. /* 100mW */
  16. #define MWIFIEX_TX_PWR_US_DEFAULT 20
  17. /* 50mW */
  18. #define MWIFIEX_TX_PWR_JP_DEFAULT 16
  19. /* 100mW */
  20. #define MWIFIEX_TX_PWR_FR_100MW 20
  21. /* 10mW */
  22. #define MWIFIEX_TX_PWR_FR_10MW 10
  23. /* 100mW */
  24. #define MWIFIEX_TX_PWR_EMEA_DEFAULT 20
  25. static u8 adhoc_rates_b[B_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96, 0 };
  26. static u8 adhoc_rates_g[G_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
  27. 0xb0, 0x48, 0x60, 0x6c, 0 };
  28. static u8 adhoc_rates_bg[BG_SUPPORTED_RATES] = { 0x82, 0x84, 0x8b, 0x96,
  29. 0x0c, 0x12, 0x18, 0x24,
  30. 0x30, 0x48, 0x60, 0x6c, 0 };
  31. static u8 adhoc_rates_a[A_SUPPORTED_RATES] = { 0x8c, 0x12, 0x98, 0x24,
  32. 0xb0, 0x48, 0x60, 0x6c, 0 };
  33. static u8 supported_rates_a[A_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
  34. 0xb0, 0x48, 0x60, 0x6c, 0 };
  35. static u16 mwifiex_data_rates[MWIFIEX_SUPPORTED_RATES_EXT] = { 0x02, 0x04,
  36. 0x0B, 0x16, 0x00, 0x0C, 0x12, 0x18,
  37. 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90,
  38. 0x0D, 0x1A, 0x27, 0x34, 0x4E, 0x68,
  39. 0x75, 0x82, 0x0C, 0x1B, 0x36, 0x51,
  40. 0x6C, 0xA2, 0xD8, 0xF3, 0x10E, 0x00 };
  41. static u8 supported_rates_b[B_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x16, 0 };
  42. static u8 supported_rates_g[G_SUPPORTED_RATES] = { 0x0c, 0x12, 0x18, 0x24,
  43. 0x30, 0x48, 0x60, 0x6c, 0 };
  44. static u8 supported_rates_bg[BG_SUPPORTED_RATES] = { 0x02, 0x04, 0x0b, 0x0c,
  45. 0x12, 0x16, 0x18, 0x24, 0x30, 0x48,
  46. 0x60, 0x6c, 0 };
  47. u16 region_code_index[MWIFIEX_MAX_REGION_CODE] = { 0x00, 0x10, 0x20, 0x30,
  48. 0x31, 0x32, 0x40, 0x41, 0x50 };
  49. static u8 supported_rates_n[N_SUPPORTED_RATES] = { 0x02, 0x04, 0 };
  50. /* For every mcs_rate line, the first 8 bytes are for stream 1x1,
  51. * and all 16 bytes are for stream 2x2.
  52. */
  53. static const u16 mcs_rate[4][16] = {
  54. /* LGI 40M */
  55. { 0x1b, 0x36, 0x51, 0x6c, 0xa2, 0xd8, 0xf3, 0x10e,
  56. 0x36, 0x6c, 0xa2, 0xd8, 0x144, 0x1b0, 0x1e6, 0x21c },
  57. /* SGI 40M */
  58. { 0x1e, 0x3c, 0x5a, 0x78, 0xb4, 0xf0, 0x10e, 0x12c,
  59. 0x3c, 0x78, 0xb4, 0xf0, 0x168, 0x1e0, 0x21c, 0x258 },
  60. /* LGI 20M */
  61. { 0x0d, 0x1a, 0x27, 0x34, 0x4e, 0x68, 0x75, 0x82,
  62. 0x1a, 0x34, 0x4e, 0x68, 0x9c, 0xd0, 0xea, 0x104 },
  63. /* SGI 20M */
  64. { 0x0e, 0x1c, 0x2b, 0x39, 0x56, 0x73, 0x82, 0x90,
  65. 0x1c, 0x39, 0x56, 0x73, 0xad, 0xe7, 0x104, 0x120 }
  66. };
  67. /* AC rates */
  68. static const u16 ac_mcs_rate_nss1[8][10] = {
  69. /* LG 160M */
  70. { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
  71. 0x492, 0x57C, 0x618 },
  72. /* SG 160M */
  73. { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
  74. 0x514, 0x618, 0x6C6 },
  75. /* LG 80M */
  76. { 0x3B, 0x75, 0xB0, 0xEA, 0x15F, 0x1D4, 0x20F,
  77. 0x249, 0x2BE, 0x30C },
  78. /* SG 80M */
  79. { 0x41, 0x82, 0xC3, 0x104, 0x186, 0x208, 0x249,
  80. 0x28A, 0x30C, 0x363 },
  81. /* LG 40M */
  82. { 0x1B, 0x36, 0x51, 0x6C, 0xA2, 0xD8, 0xF3,
  83. 0x10E, 0x144, 0x168 },
  84. /* SG 40M */
  85. { 0x1E, 0x3C, 0x5A, 0x78, 0xB4, 0xF0, 0x10E,
  86. 0x12C, 0x168, 0x190 },
  87. /* LG 20M */
  88. { 0xD, 0x1A, 0x27, 0x34, 0x4E, 0x68, 0x75, 0x82, 0x9C, 0x00 },
  89. /* SG 20M */
  90. { 0xF, 0x1D, 0x2C, 0x3A, 0x57, 0x74, 0x82, 0x91, 0xAE, 0x00 },
  91. };
  92. /* NSS2 note: the value in the table is 2 multiplier of the actual rate */
  93. static const u16 ac_mcs_rate_nss2[8][10] = {
  94. /* LG 160M */
  95. { 0xEA, 0x1D4, 0x2BE, 0x3A8, 0x57C, 0x750, 0x83A,
  96. 0x924, 0xAF8, 0xC30 },
  97. /* SG 160M */
  98. { 0x104, 0x208, 0x30C, 0x410, 0x618, 0x820, 0x924,
  99. 0xA28, 0xC30, 0xD8B },
  100. /* LG 80M */
  101. { 0x75, 0xEA, 0x15F, 0x1D4, 0x2BE, 0x3A8, 0x41D,
  102. 0x492, 0x57C, 0x618 },
  103. /* SG 80M */
  104. { 0x82, 0x104, 0x186, 0x208, 0x30C, 0x410, 0x492,
  105. 0x514, 0x618, 0x6C6 },
  106. /* LG 40M */
  107. { 0x36, 0x6C, 0xA2, 0xD8, 0x144, 0x1B0, 0x1E6,
  108. 0x21C, 0x288, 0x2D0 },
  109. /* SG 40M */
  110. { 0x3C, 0x78, 0xB4, 0xF0, 0x168, 0x1E0, 0x21C,
  111. 0x258, 0x2D0, 0x320 },
  112. /* LG 20M */
  113. { 0x1A, 0x34, 0x4A, 0x68, 0x9C, 0xD0, 0xEA, 0x104,
  114. 0x138, 0x00 },
  115. /* SG 20M */
  116. { 0x1D, 0x3A, 0x57, 0x74, 0xAE, 0xE6, 0x104, 0x121,
  117. 0x15B, 0x00 },
  118. };
  119. struct region_code_mapping {
  120. u8 code;
  121. u8 region[IEEE80211_COUNTRY_STRING_LEN];
  122. };
  123. static struct region_code_mapping region_code_mapping_t[] = {
  124. { 0x10, "US " }, /* US FCC */
  125. { 0x20, "CA " }, /* IC Canada */
  126. { 0x30, "FR " }, /* France */
  127. { 0x31, "ES " }, /* Spain */
  128. { 0x32, "FR " }, /* France */
  129. { 0x40, "JP " }, /* Japan */
  130. { 0x41, "JP " }, /* Japan */
  131. { 0x50, "CN " }, /* China */
  132. };
  133. /* This function converts integer code to region string */
  134. u8 *mwifiex_11d_code_2_region(u8 code)
  135. {
  136. u8 i;
  137. /* Look for code in mapping table */
  138. for (i = 0; i < ARRAY_SIZE(region_code_mapping_t); i++)
  139. if (region_code_mapping_t[i].code == code)
  140. return region_code_mapping_t[i].region;
  141. return NULL;
  142. }
  143. /*
  144. * This function maps an index in supported rates table into
  145. * the corresponding data rate.
  146. */
  147. u32 mwifiex_index_to_acs_data_rate(struct mwifiex_private *priv,
  148. u8 index, u8 ht_info)
  149. {
  150. u32 rate = 0;
  151. u8 mcs_index = 0;
  152. u8 bw = 0;
  153. u8 gi = 0;
  154. if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_VHT) {
  155. mcs_index = min(index & 0xF, 9);
  156. /* 20M: bw=0, 40M: bw=1, 80M: bw=2, 160M: bw=3 */
  157. bw = (ht_info & 0xC) >> 2;
  158. /* LGI: gi =0, SGI: gi = 1 */
  159. gi = (ht_info & 0x10) >> 4;
  160. if ((index >> 4) == 1) /* NSS = 2 */
  161. rate = ac_mcs_rate_nss2[2 * (3 - bw) + gi][mcs_index];
  162. else /* NSS = 1 */
  163. rate = ac_mcs_rate_nss1[2 * (3 - bw) + gi][mcs_index];
  164. } else if ((ht_info & 0x3) == MWIFIEX_RATE_FORMAT_HT) {
  165. /* 20M: bw=0, 40M: bw=1 */
  166. bw = (ht_info & 0xC) >> 2;
  167. /* LGI: gi =0, SGI: gi = 1 */
  168. gi = (ht_info & 0x10) >> 4;
  169. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  170. if (gi == 1)
  171. rate = 0x0D; /* MCS 32 SGI rate */
  172. else
  173. rate = 0x0C; /* MCS 32 LGI rate */
  174. } else if (index < 16) {
  175. if ((bw == 1) || (bw == 0))
  176. rate = mcs_rate[2 * (1 - bw) + gi][index];
  177. else
  178. rate = mwifiex_data_rates[0];
  179. } else {
  180. rate = mwifiex_data_rates[0];
  181. }
  182. } else {
  183. /* 11n non-HT rates */
  184. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  185. index = 0;
  186. rate = mwifiex_data_rates[index];
  187. }
  188. return rate;
  189. }
  190. /* This function maps an index in supported rates table into
  191. * the corresponding data rate.
  192. */
  193. u32 mwifiex_index_to_data_rate(struct mwifiex_private *priv,
  194. u8 index, u8 ht_info)
  195. {
  196. u32 mcs_num_supp =
  197. (priv->adapter->user_dev_mcs_support == HT_STREAM_2X2) ? 16 : 8;
  198. u32 rate;
  199. if (priv->adapter->is_hw_11ac_capable)
  200. return mwifiex_index_to_acs_data_rate(priv, index, ht_info);
  201. if (ht_info & BIT(0)) {
  202. if (index == MWIFIEX_RATE_BITMAP_MCS0) {
  203. if (ht_info & BIT(2))
  204. rate = 0x0D; /* MCS 32 SGI rate */
  205. else
  206. rate = 0x0C; /* MCS 32 LGI rate */
  207. } else if (index < mcs_num_supp) {
  208. if (ht_info & BIT(1)) {
  209. if (ht_info & BIT(2))
  210. /* SGI, 40M */
  211. rate = mcs_rate[1][index];
  212. else
  213. /* LGI, 40M */
  214. rate = mcs_rate[0][index];
  215. } else {
  216. if (ht_info & BIT(2))
  217. /* SGI, 20M */
  218. rate = mcs_rate[3][index];
  219. else
  220. /* LGI, 20M */
  221. rate = mcs_rate[2][index];
  222. }
  223. } else
  224. rate = mwifiex_data_rates[0];
  225. } else {
  226. if (index >= MWIFIEX_SUPPORTED_RATES_EXT)
  227. index = 0;
  228. rate = mwifiex_data_rates[index];
  229. }
  230. return rate;
  231. }
  232. /*
  233. * This function returns the current active data rates.
  234. *
  235. * The result may vary depending upon connection status.
  236. */
  237. u32 mwifiex_get_active_data_rates(struct mwifiex_private *priv, u8 *rates)
  238. {
  239. if (!priv->media_connected)
  240. return mwifiex_get_supported_rates(priv, rates);
  241. else
  242. return mwifiex_copy_rates(rates, 0,
  243. priv->curr_bss_params.data_rates,
  244. priv->curr_bss_params.num_of_rates);
  245. }
  246. /*
  247. * This function locates the Channel-Frequency-Power triplet based upon
  248. * band and channel/frequency parameters.
  249. */
  250. struct mwifiex_chan_freq_power *
  251. mwifiex_get_cfp(struct mwifiex_private *priv, u8 band, u16 channel, u32 freq)
  252. {
  253. struct mwifiex_chan_freq_power *cfp = NULL;
  254. struct ieee80211_supported_band *sband;
  255. struct ieee80211_channel *ch = NULL;
  256. int i;
  257. if (!channel && !freq)
  258. return cfp;
  259. if (mwifiex_band_to_radio_type(band) == HostCmd_SCAN_RADIO_TYPE_BG)
  260. sband = priv->wdev.wiphy->bands[NL80211_BAND_2GHZ];
  261. else
  262. sband = priv->wdev.wiphy->bands[NL80211_BAND_5GHZ];
  263. if (!sband) {
  264. mwifiex_dbg(priv->adapter, ERROR,
  265. "%s: cannot find cfp by band %d\n",
  266. __func__, band);
  267. return cfp;
  268. }
  269. for (i = 0; i < sband->n_channels; i++) {
  270. ch = &sband->channels[i];
  271. if (ch->flags & IEEE80211_CHAN_DISABLED)
  272. continue;
  273. if (freq) {
  274. if (ch->center_freq == freq)
  275. break;
  276. } else {
  277. /* find by valid channel*/
  278. if (ch->hw_value == channel ||
  279. channel == FIRST_VALID_CHANNEL)
  280. break;
  281. }
  282. }
  283. if (i == sband->n_channels) {
  284. mwifiex_dbg(priv->adapter, WARN,
  285. "%s: cannot find cfp by band %d\t"
  286. "& channel=%d freq=%d\n",
  287. __func__, band, channel, freq);
  288. } else {
  289. if (!ch)
  290. return cfp;
  291. priv->cfp.channel = ch->hw_value;
  292. priv->cfp.freq = ch->center_freq;
  293. priv->cfp.max_tx_power = ch->max_power;
  294. cfp = &priv->cfp;
  295. }
  296. return cfp;
  297. }
  298. /*
  299. * This function checks if the data rate is set to auto.
  300. */
  301. u8
  302. mwifiex_is_rate_auto(struct mwifiex_private *priv)
  303. {
  304. u32 i;
  305. int rate_num = 0;
  306. for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates); i++)
  307. if (priv->bitmap_rates[i])
  308. rate_num++;
  309. if (rate_num > 1)
  310. return true;
  311. else
  312. return false;
  313. }
  314. /* This function gets the supported data rates from bitmask inside
  315. * cfg80211_scan_request.
  316. */
  317. u32 mwifiex_get_rates_from_cfg80211(struct mwifiex_private *priv,
  318. u8 *rates, u8 radio_type)
  319. {
  320. struct wiphy *wiphy = priv->adapter->wiphy;
  321. struct cfg80211_scan_request *request = priv->scan_request;
  322. u32 num_rates, rate_mask;
  323. struct ieee80211_supported_band *sband;
  324. int i;
  325. if (radio_type) {
  326. sband = wiphy->bands[NL80211_BAND_5GHZ];
  327. if (WARN_ON_ONCE(!sband))
  328. return 0;
  329. rate_mask = request->rates[NL80211_BAND_5GHZ];
  330. } else {
  331. sband = wiphy->bands[NL80211_BAND_2GHZ];
  332. if (WARN_ON_ONCE(!sband))
  333. return 0;
  334. rate_mask = request->rates[NL80211_BAND_2GHZ];
  335. }
  336. num_rates = 0;
  337. for (i = 0; i < sband->n_bitrates; i++) {
  338. if ((BIT(i) & rate_mask) == 0)
  339. continue; /* skip rate */
  340. rates[num_rates++] = (u8)(sband->bitrates[i].bitrate / 5);
  341. }
  342. return num_rates;
  343. }
  344. /* This function gets the supported data rates. The function works in
  345. * both Ad-Hoc and infra mode by printing the band and returning the
  346. * data rates.
  347. */
  348. u32 mwifiex_get_supported_rates(struct mwifiex_private *priv, u8 *rates)
  349. {
  350. u32 k = 0;
  351. struct mwifiex_adapter *adapter = priv->adapter;
  352. if (priv->bss_mode == NL80211_IFTYPE_STATION ||
  353. priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT) {
  354. switch (adapter->config_bands) {
  355. case BAND_B:
  356. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  357. "supported_rates_b\n",
  358. adapter->config_bands);
  359. k = mwifiex_copy_rates(rates, k, supported_rates_b,
  360. sizeof(supported_rates_b));
  361. break;
  362. case BAND_G:
  363. case BAND_G | BAND_GN:
  364. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  365. "supported_rates_g\n",
  366. adapter->config_bands);
  367. k = mwifiex_copy_rates(rates, k, supported_rates_g,
  368. sizeof(supported_rates_g));
  369. break;
  370. case BAND_B | BAND_G:
  371. case BAND_A | BAND_B | BAND_G:
  372. case BAND_A | BAND_B:
  373. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN:
  374. case BAND_A | BAND_B | BAND_G | BAND_GN | BAND_AN | BAND_AAC:
  375. case BAND_B | BAND_G | BAND_GN:
  376. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  377. "supported_rates_bg\n",
  378. adapter->config_bands);
  379. k = mwifiex_copy_rates(rates, k, supported_rates_bg,
  380. sizeof(supported_rates_bg));
  381. break;
  382. case BAND_A:
  383. case BAND_A | BAND_G:
  384. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  385. "supported_rates_a\n",
  386. adapter->config_bands);
  387. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  388. sizeof(supported_rates_a));
  389. break;
  390. case BAND_AN:
  391. case BAND_A | BAND_AN:
  392. case BAND_A | BAND_AN | BAND_AAC:
  393. case BAND_A | BAND_G | BAND_AN | BAND_GN:
  394. case BAND_A | BAND_G | BAND_AN | BAND_GN | BAND_AAC:
  395. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  396. "supported_rates_a\n",
  397. adapter->config_bands);
  398. k = mwifiex_copy_rates(rates, k, supported_rates_a,
  399. sizeof(supported_rates_a));
  400. break;
  401. case BAND_GN:
  402. mwifiex_dbg(adapter, INFO, "info: infra band=%d\t"
  403. "supported_rates_n\n",
  404. adapter->config_bands);
  405. k = mwifiex_copy_rates(rates, k, supported_rates_n,
  406. sizeof(supported_rates_n));
  407. break;
  408. }
  409. } else {
  410. /* Ad-hoc mode */
  411. switch (adapter->adhoc_start_band) {
  412. case BAND_B:
  413. mwifiex_dbg(adapter, INFO, "info: adhoc B\n");
  414. k = mwifiex_copy_rates(rates, k, adhoc_rates_b,
  415. sizeof(adhoc_rates_b));
  416. break;
  417. case BAND_G:
  418. case BAND_G | BAND_GN:
  419. mwifiex_dbg(adapter, INFO, "info: adhoc G only\n");
  420. k = mwifiex_copy_rates(rates, k, adhoc_rates_g,
  421. sizeof(adhoc_rates_g));
  422. break;
  423. case BAND_B | BAND_G:
  424. case BAND_B | BAND_G | BAND_GN:
  425. mwifiex_dbg(adapter, INFO, "info: adhoc BG\n");
  426. k = mwifiex_copy_rates(rates, k, adhoc_rates_bg,
  427. sizeof(adhoc_rates_bg));
  428. break;
  429. case BAND_A:
  430. case BAND_A | BAND_AN:
  431. mwifiex_dbg(adapter, INFO, "info: adhoc A\n");
  432. k = mwifiex_copy_rates(rates, k, adhoc_rates_a,
  433. sizeof(adhoc_rates_a));
  434. break;
  435. }
  436. }
  437. return k;
  438. }
  439. u8 mwifiex_adjust_data_rate(struct mwifiex_private *priv,
  440. u8 rx_rate, u8 rate_info)
  441. {
  442. u8 rate_index = 0;
  443. /* HT40 */
  444. if ((rate_info & BIT(0)) && (rate_info & BIT(1)))
  445. rate_index = MWIFIEX_RATE_INDEX_MCS0 +
  446. MWIFIEX_BW20_MCS_NUM + rx_rate;
  447. else if (rate_info & BIT(0)) /* HT20 */
  448. rate_index = MWIFIEX_RATE_INDEX_MCS0 + rx_rate;
  449. else
  450. rate_index = (rx_rate > MWIFIEX_RATE_INDEX_OFDM0) ?
  451. rx_rate - 1 : rx_rate;
  452. if (rate_index >= MWIFIEX_MAX_AC_RX_RATES)
  453. rate_index = MWIFIEX_MAX_AC_RX_RATES - 1;
  454. return rate_index;
  455. }