channel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <net/cfg80211.h>
  18. #include <net/mac80211.h>
  19. #include <net/regulatory.h>
  20. #include <defs.h>
  21. #include "pub.h"
  22. #include "phy/phy_hal.h"
  23. #include "main.h"
  24. #include "stf.h"
  25. #include "channel.h"
  26. #include "mac80211_if.h"
  27. #include "debug.h"
  28. /* QDB() macro takes a dB value and converts to a quarter dB value */
  29. #define QDB(n) ((n) * BRCMS_TXPWR_DB_FACTOR)
  30. #define LOCALE_MIMO_IDX_bn 0
  31. #define LOCALE_MIMO_IDX_11n 0
  32. /* max of BAND_5G_PWR_LVLS and 14 for 2.4 GHz */
  33. #define BRCMS_MAXPWR_MIMO_TBL_SIZE 14
  34. /* maxpwr mapping to 5GHz band channels:
  35. * maxpwr[0] - channels [34-48]
  36. * maxpwr[1] - channels [52-60]
  37. * maxpwr[2] - channels [62-64]
  38. * maxpwr[3] - channels [100-140]
  39. * maxpwr[4] - channels [149-165]
  40. */
  41. #define BAND_5G_PWR_LVLS 5 /* 5 power levels for 5G */
  42. #define LC(id) LOCALE_MIMO_IDX_ ## id
  43. #define LOCALES(mimo2, mimo5) \
  44. {LC(mimo2), LC(mimo5)}
  45. /* macro to get 5 GHz channel group index for tx power */
  46. #define CHANNEL_POWER_IDX_5G(c) (((c) < 52) ? 0 : \
  47. (((c) < 62) ? 1 : \
  48. (((c) < 100) ? 2 : \
  49. (((c) < 149) ? 3 : 4))))
  50. #define BRCM_2GHZ_2412_2462 REG_RULE(2412-10, 2462+10, 40, 0, 19, 0)
  51. #define BRCM_2GHZ_2467_2472 REG_RULE(2467-10, 2472+10, 20, 0, 19, \
  52. NL80211_RRF_PASSIVE_SCAN | \
  53. NL80211_RRF_NO_IBSS)
  54. #define BRCM_5GHZ_5180_5240 REG_RULE(5180-10, 5240+10, 40, 0, 21, \
  55. NL80211_RRF_PASSIVE_SCAN | \
  56. NL80211_RRF_NO_IBSS)
  57. #define BRCM_5GHZ_5260_5320 REG_RULE(5260-10, 5320+10, 40, 0, 21, \
  58. NL80211_RRF_PASSIVE_SCAN | \
  59. NL80211_RRF_DFS | \
  60. NL80211_RRF_NO_IBSS)
  61. #define BRCM_5GHZ_5500_5700 REG_RULE(5500-10, 5700+10, 40, 0, 21, \
  62. NL80211_RRF_PASSIVE_SCAN | \
  63. NL80211_RRF_DFS | \
  64. NL80211_RRF_NO_IBSS)
  65. #define BRCM_5GHZ_5745_5825 REG_RULE(5745-10, 5825+10, 40, 0, 21, \
  66. NL80211_RRF_PASSIVE_SCAN | \
  67. NL80211_RRF_NO_IBSS)
  68. static const struct ieee80211_regdomain brcms_regdom_x2 = {
  69. .n_reg_rules = 6,
  70. .alpha2 = "X2",
  71. .reg_rules = {
  72. BRCM_2GHZ_2412_2462,
  73. BRCM_2GHZ_2467_2472,
  74. BRCM_5GHZ_5180_5240,
  75. BRCM_5GHZ_5260_5320,
  76. BRCM_5GHZ_5500_5700,
  77. BRCM_5GHZ_5745_5825,
  78. }
  79. };
  80. /* locale per-channel tx power limits for MIMO frames
  81. * maxpwr arrays are index by channel for 2.4 GHz limits, and
  82. * by sub-band for 5 GHz limits using CHANNEL_POWER_IDX_5G(channel)
  83. */
  84. struct locale_mimo_info {
  85. /* tx 20 MHz power limits, qdBm units */
  86. s8 maxpwr20[BRCMS_MAXPWR_MIMO_TBL_SIZE];
  87. /* tx 40 MHz power limits, qdBm units */
  88. s8 maxpwr40[BRCMS_MAXPWR_MIMO_TBL_SIZE];
  89. };
  90. /* Country names and abbreviations with locale defined from ISO 3166 */
  91. struct country_info {
  92. const u8 locale_mimo_2G; /* 2.4G mimo info */
  93. const u8 locale_mimo_5G; /* 5G mimo info */
  94. };
  95. struct brcms_regd {
  96. struct country_info country;
  97. const struct ieee80211_regdomain *regdomain;
  98. };
  99. struct brcms_cm_info {
  100. struct brcms_pub *pub;
  101. struct brcms_c_info *wlc;
  102. const struct brcms_regd *world_regd;
  103. };
  104. /*
  105. * MIMO Locale Definitions - 2.4 GHz
  106. */
  107. static const struct locale_mimo_info locale_bn = {
  108. {QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  109. QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  110. QDB(13), QDB(13), QDB(13)},
  111. {0, 0, QDB(13), QDB(13), QDB(13),
  112. QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
  113. QDB(13), 0, 0},
  114. };
  115. static const struct locale_mimo_info *g_mimo_2g_table[] = {
  116. &locale_bn
  117. };
  118. /*
  119. * MIMO Locale Definitions - 5 GHz
  120. */
  121. static const struct locale_mimo_info locale_11n = {
  122. { /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
  123. {QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
  124. };
  125. static const struct locale_mimo_info *g_mimo_5g_table[] = {
  126. &locale_11n
  127. };
  128. static const struct brcms_regd cntry_locales[] = {
  129. /* Worldwide RoW 2, must always be at index 0 */
  130. {
  131. .country = LOCALES(bn, 11n),
  132. .regdomain = &brcms_regdom_x2,
  133. },
  134. };
  135. static const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx)
  136. {
  137. if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table))
  138. return NULL;
  139. return g_mimo_2g_table[locale_idx];
  140. }
  141. static const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx)
  142. {
  143. if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table))
  144. return NULL;
  145. return g_mimo_5g_table[locale_idx];
  146. }
  147. /*
  148. * Indicates whether the country provided is valid to pass
  149. * to cfg80211 or not.
  150. *
  151. * returns true if valid; false if not.
  152. */
  153. static bool brcms_c_country_valid(const char *ccode)
  154. {
  155. /*
  156. * only allow ascii alpha uppercase for the first 2
  157. * chars.
  158. */
  159. if (!((0x80 & ccode[0]) == 0 && ccode[0] >= 0x41 && ccode[0] <= 0x5A &&
  160. (0x80 & ccode[1]) == 0 && ccode[1] >= 0x41 && ccode[1] <= 0x5A &&
  161. ccode[2] == '\0'))
  162. return false;
  163. /*
  164. * do not match ISO 3166-1 user assigned country codes
  165. * that may be in the driver table
  166. */
  167. if (!strcmp("AA", ccode) || /* AA */
  168. !strcmp("ZZ", ccode) || /* ZZ */
  169. ccode[0] == 'X' || /* XA - XZ */
  170. (ccode[0] == 'Q' && /* QM - QZ */
  171. (ccode[1] >= 'M' && ccode[1] <= 'Z')))
  172. return false;
  173. if (!strcmp("NA", ccode))
  174. return false;
  175. return true;
  176. }
  177. static const struct brcms_regd *brcms_world_regd(const char *regdom, int len)
  178. {
  179. const struct brcms_regd *regd = NULL;
  180. int i;
  181. for (i = 0; i < ARRAY_SIZE(cntry_locales); i++) {
  182. if (!strncmp(regdom, cntry_locales[i].regdomain->alpha2, len)) {
  183. regd = &cntry_locales[i];
  184. break;
  185. }
  186. }
  187. return regd;
  188. }
  189. static const struct brcms_regd *brcms_default_world_regd(void)
  190. {
  191. return &cntry_locales[0];
  192. }
  193. /* JP, J1 - J10 are Japan ccodes */
  194. static bool brcms_c_japan_ccode(const char *ccode)
  195. {
  196. return (ccode[0] == 'J' &&
  197. (ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
  198. }
  199. static void
  200. brcms_c_channel_min_txpower_limits_with_local_constraint(
  201. struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
  202. u8 local_constraint_qdbm)
  203. {
  204. int j;
  205. /* CCK Rates */
  206. for (j = 0; j < WL_TX_POWER_CCK_NUM; j++)
  207. txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
  208. /* 20 MHz Legacy OFDM SISO */
  209. for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++)
  210. txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
  211. /* 20 MHz Legacy OFDM CDD */
  212. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  213. txpwr->ofdm_cdd[j] =
  214. min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
  215. /* 40 MHz Legacy OFDM SISO */
  216. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  217. txpwr->ofdm_40_siso[j] =
  218. min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
  219. /* 40 MHz Legacy OFDM CDD */
  220. for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
  221. txpwr->ofdm_40_cdd[j] =
  222. min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
  223. /* 20MHz MCS 0-7 SISO */
  224. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  225. txpwr->mcs_20_siso[j] =
  226. min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
  227. /* 20MHz MCS 0-7 CDD */
  228. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  229. txpwr->mcs_20_cdd[j] =
  230. min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
  231. /* 20MHz MCS 0-7 STBC */
  232. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  233. txpwr->mcs_20_stbc[j] =
  234. min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
  235. /* 20MHz MCS 8-15 MIMO */
  236. for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
  237. txpwr->mcs_20_mimo[j] =
  238. min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
  239. /* 40MHz MCS 0-7 SISO */
  240. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  241. txpwr->mcs_40_siso[j] =
  242. min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
  243. /* 40MHz MCS 0-7 CDD */
  244. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  245. txpwr->mcs_40_cdd[j] =
  246. min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
  247. /* 40MHz MCS 0-7 STBC */
  248. for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
  249. txpwr->mcs_40_stbc[j] =
  250. min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
  251. /* 40MHz MCS 8-15 MIMO */
  252. for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
  253. txpwr->mcs_40_mimo[j] =
  254. min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
  255. /* 40MHz MCS 32 */
  256. txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
  257. }
  258. /*
  259. * set the driver's current country and regulatory information
  260. * using a country code as the source. Look up built in country
  261. * information found with the country code.
  262. */
  263. static void
  264. brcms_c_set_country(struct brcms_cm_info *wlc_cm,
  265. const struct brcms_regd *regd)
  266. {
  267. struct brcms_c_info *wlc = wlc_cm->wlc;
  268. if ((wlc->pub->_n_enab & SUPPORT_11N) !=
  269. wlc->protection->nmode_user)
  270. brcms_c_set_nmode(wlc);
  271. brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
  272. brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
  273. brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
  274. return;
  275. }
  276. struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc)
  277. {
  278. struct brcms_cm_info *wlc_cm;
  279. struct brcms_pub *pub = wlc->pub;
  280. struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
  281. const char *ccode = sprom->alpha2;
  282. int ccode_len = sizeof(sprom->alpha2);
  283. wlc_cm = kzalloc(sizeof(struct brcms_cm_info), GFP_ATOMIC);
  284. if (wlc_cm == NULL)
  285. return NULL;
  286. wlc_cm->pub = pub;
  287. wlc_cm->wlc = wlc;
  288. wlc->cmi = wlc_cm;
  289. /* store the country code for passing up as a regulatory hint */
  290. wlc_cm->world_regd = brcms_world_regd(ccode, ccode_len);
  291. if (brcms_c_country_valid(ccode))
  292. strncpy(wlc->pub->srom_ccode, ccode, ccode_len);
  293. /*
  294. * If no custom world domain is found in the SROM, use the
  295. * default "X2" domain.
  296. */
  297. if (!wlc_cm->world_regd) {
  298. wlc_cm->world_regd = brcms_default_world_regd();
  299. ccode = wlc_cm->world_regd->regdomain->alpha2;
  300. ccode_len = BRCM_CNTRY_BUF_SZ - 1;
  301. }
  302. /* save default country for exiting 11d regulatory mode */
  303. strncpy(wlc->country_default, ccode, ccode_len);
  304. /* initialize autocountry_default to driver default */
  305. strncpy(wlc->autocountry_default, ccode, ccode_len);
  306. brcms_c_set_country(wlc_cm, wlc_cm->world_regd);
  307. return wlc_cm;
  308. }
  309. void brcms_c_channel_mgr_detach(struct brcms_cm_info *wlc_cm)
  310. {
  311. kfree(wlc_cm);
  312. }
  313. void
  314. brcms_c_channel_set_chanspec(struct brcms_cm_info *wlc_cm, u16 chanspec,
  315. u8 local_constraint_qdbm)
  316. {
  317. struct brcms_c_info *wlc = wlc_cm->wlc;
  318. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
  319. struct txpwr_limits txpwr;
  320. brcms_c_channel_reg_limits(wlc_cm, chanspec, &txpwr);
  321. brcms_c_channel_min_txpower_limits_with_local_constraint(
  322. wlc_cm, &txpwr, local_constraint_qdbm
  323. );
  324. /* set or restore gmode as required by regulatory */
  325. if (ch->flags & IEEE80211_CHAN_NO_OFDM)
  326. brcms_c_set_gmode(wlc, GMODE_LEGACY_B, false);
  327. else
  328. brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
  329. brcms_b_set_chanspec(wlc->hw, chanspec,
  330. !!(ch->flags & IEEE80211_CHAN_PASSIVE_SCAN),
  331. &txpwr);
  332. }
  333. void
  334. brcms_c_channel_reg_limits(struct brcms_cm_info *wlc_cm, u16 chanspec,
  335. struct txpwr_limits *txpwr)
  336. {
  337. struct brcms_c_info *wlc = wlc_cm->wlc;
  338. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
  339. uint i;
  340. uint chan;
  341. int maxpwr;
  342. int delta;
  343. const struct country_info *country;
  344. struct brcms_band *band;
  345. int conducted_max = BRCMS_TXPWR_MAX;
  346. const struct locale_mimo_info *li_mimo;
  347. int maxpwr20, maxpwr40;
  348. int maxpwr_idx;
  349. uint j;
  350. memset(txpwr, 0, sizeof(struct txpwr_limits));
  351. if (WARN_ON(!ch))
  352. return;
  353. country = &wlc_cm->world_regd->country;
  354. chan = CHSPEC_CHANNEL(chanspec);
  355. band = wlc->bandstate[chspec_bandunit(chanspec)];
  356. li_mimo = (band->bandtype == BRCM_BAND_5G) ?
  357. brcms_c_get_mimo_5g(country->locale_mimo_5G) :
  358. brcms_c_get_mimo_2g(country->locale_mimo_2G);
  359. delta = band->antgain;
  360. if (band->bandtype == BRCM_BAND_2G)
  361. conducted_max = QDB(22);
  362. maxpwr = QDB(ch->max_power) - delta;
  363. maxpwr = max(maxpwr, 0);
  364. maxpwr = min(maxpwr, conducted_max);
  365. /* CCK txpwr limits for 2.4G band */
  366. if (band->bandtype == BRCM_BAND_2G) {
  367. for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
  368. txpwr->cck[i] = (u8) maxpwr;
  369. }
  370. for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++) {
  371. txpwr->ofdm[i] = (u8) maxpwr;
  372. /*
  373. * OFDM 40 MHz SISO has the same power as the corresponding
  374. * MCS0-7 rate unless overriden by the locale specific code.
  375. * We set this value to 0 as a flag (presumably 0 dBm isn't
  376. * a possibility) and then copy the MCS0-7 value to the 40 MHz
  377. * value if it wasn't explicitly set.
  378. */
  379. txpwr->ofdm_40_siso[i] = 0;
  380. txpwr->ofdm_cdd[i] = (u8) maxpwr;
  381. txpwr->ofdm_40_cdd[i] = 0;
  382. }
  383. delta = 0;
  384. if (band->antgain > QDB(6))
  385. delta = band->antgain - QDB(6); /* Excess over 6 dB */
  386. if (band->bandtype == BRCM_BAND_2G)
  387. maxpwr_idx = (chan - 1);
  388. else
  389. maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
  390. maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
  391. maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
  392. maxpwr20 = maxpwr20 - delta;
  393. maxpwr20 = max(maxpwr20, 0);
  394. maxpwr40 = maxpwr40 - delta;
  395. maxpwr40 = max(maxpwr40, 0);
  396. /* Fill in the MCS 0-7 (SISO) rates */
  397. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  398. /*
  399. * 20 MHz has the same power as the corresponding OFDM rate
  400. * unless overriden by the locale specific code.
  401. */
  402. txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
  403. txpwr->mcs_40_siso[i] = 0;
  404. }
  405. /* Fill in the MCS 0-7 CDD rates */
  406. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  407. txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
  408. txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
  409. }
  410. /*
  411. * These locales have SISO expressed in the
  412. * table and override CDD later
  413. */
  414. if (li_mimo == &locale_bn) {
  415. if (li_mimo == &locale_bn) {
  416. maxpwr20 = QDB(16);
  417. maxpwr40 = 0;
  418. if (chan >= 3 && chan <= 11)
  419. maxpwr40 = QDB(16);
  420. }
  421. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  422. txpwr->mcs_20_siso[i] = (u8) maxpwr20;
  423. txpwr->mcs_40_siso[i] = (u8) maxpwr40;
  424. }
  425. }
  426. /* Fill in the MCS 0-7 STBC rates */
  427. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  428. txpwr->mcs_20_stbc[i] = 0;
  429. txpwr->mcs_40_stbc[i] = 0;
  430. }
  431. /* Fill in the MCS 8-15 SDM rates */
  432. for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++) {
  433. txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
  434. txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
  435. }
  436. /* Fill in MCS32 */
  437. txpwr->mcs32 = (u8) maxpwr40;
  438. for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
  439. if (txpwr->ofdm_40_cdd[i] == 0)
  440. txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
  441. if (i == 0) {
  442. i = i + 1;
  443. if (txpwr->ofdm_40_cdd[i] == 0)
  444. txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
  445. }
  446. }
  447. /*
  448. * Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO
  449. * value if it wasn't provided explicitly.
  450. */
  451. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  452. if (txpwr->mcs_40_siso[i] == 0)
  453. txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
  454. }
  455. for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
  456. if (txpwr->ofdm_40_siso[i] == 0)
  457. txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
  458. if (i == 0) {
  459. i = i + 1;
  460. if (txpwr->ofdm_40_siso[i] == 0)
  461. txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
  462. }
  463. }
  464. /*
  465. * Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding
  466. * STBC values if they weren't provided explicitly.
  467. */
  468. for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
  469. if (txpwr->mcs_20_stbc[i] == 0)
  470. txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
  471. if (txpwr->mcs_40_stbc[i] == 0)
  472. txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
  473. }
  474. return;
  475. }
  476. /*
  477. * Verify the chanspec is using a legal set of parameters, i.e. that the
  478. * chanspec specified a band, bw, ctl_sb and channel and that the
  479. * combination could be legal given any set of circumstances.
  480. * RETURNS: true is the chanspec is malformed, false if it looks good.
  481. */
  482. static bool brcms_c_chspec_malformed(u16 chanspec)
  483. {
  484. /* must be 2G or 5G band */
  485. if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
  486. return true;
  487. /* must be 20 or 40 bandwidth */
  488. if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
  489. return true;
  490. /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
  491. if (CHSPEC_IS20(chanspec)) {
  492. if (!CHSPEC_SB_NONE(chanspec))
  493. return true;
  494. } else if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec)) {
  495. return true;
  496. }
  497. return false;
  498. }
  499. /*
  500. * Validate the chanspec for this locale, for 40MHZ we need to also
  501. * check that the sidebands are valid 20MZH channels in this locale
  502. * and they are also a legal HT combination
  503. */
  504. static bool
  505. brcms_c_valid_chanspec_ext(struct brcms_cm_info *wlc_cm, u16 chspec)
  506. {
  507. struct brcms_c_info *wlc = wlc_cm->wlc;
  508. u8 channel = CHSPEC_CHANNEL(chspec);
  509. /* check the chanspec */
  510. if (brcms_c_chspec_malformed(chspec)) {
  511. brcms_err(wlc->hw->d11core, "wl%d: malformed chanspec 0x%x\n",
  512. wlc->pub->unit, chspec);
  513. return false;
  514. }
  515. if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
  516. chspec_bandunit(chspec))
  517. return false;
  518. return true;
  519. }
  520. bool brcms_c_valid_chanspec_db(struct brcms_cm_info *wlc_cm, u16 chspec)
  521. {
  522. return brcms_c_valid_chanspec_ext(wlc_cm, chspec);
  523. }
  524. static bool brcms_is_radar_freq(u16 center_freq)
  525. {
  526. return center_freq >= 5260 && center_freq <= 5700;
  527. }
  528. static void brcms_reg_apply_radar_flags(struct wiphy *wiphy)
  529. {
  530. struct ieee80211_supported_band *sband;
  531. struct ieee80211_channel *ch;
  532. int i;
  533. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  534. if (!sband)
  535. return;
  536. for (i = 0; i < sband->n_channels; i++) {
  537. ch = &sband->channels[i];
  538. if (!brcms_is_radar_freq(ch->center_freq))
  539. continue;
  540. /*
  541. * All channels in this range should be passive and have
  542. * DFS enabled.
  543. */
  544. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  545. ch->flags |= IEEE80211_CHAN_RADAR |
  546. IEEE80211_CHAN_NO_IBSS |
  547. IEEE80211_CHAN_PASSIVE_SCAN;
  548. }
  549. }
  550. static void
  551. brcms_reg_apply_beaconing_flags(struct wiphy *wiphy,
  552. enum nl80211_reg_initiator initiator)
  553. {
  554. struct ieee80211_supported_band *sband;
  555. struct ieee80211_channel *ch;
  556. const struct ieee80211_reg_rule *rule;
  557. int band, i, ret;
  558. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  559. sband = wiphy->bands[band];
  560. if (!sband)
  561. continue;
  562. for (i = 0; i < sband->n_channels; i++) {
  563. ch = &sband->channels[i];
  564. if (ch->flags &
  565. (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_RADAR))
  566. continue;
  567. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  568. ret = freq_reg_info(wiphy, ch->center_freq,
  569. 0, &rule);
  570. if (ret)
  571. continue;
  572. if (!(rule->flags & NL80211_RRF_NO_IBSS))
  573. ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
  574. if (!(rule->flags & NL80211_RRF_PASSIVE_SCAN))
  575. ch->flags &=
  576. ~IEEE80211_CHAN_PASSIVE_SCAN;
  577. } else if (ch->beacon_found) {
  578. ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
  579. IEEE80211_CHAN_PASSIVE_SCAN);
  580. }
  581. }
  582. }
  583. }
  584. static int brcms_reg_notifier(struct wiphy *wiphy,
  585. struct regulatory_request *request)
  586. {
  587. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  588. struct brcms_info *wl = hw->priv;
  589. struct brcms_c_info *wlc = wl->wlc;
  590. struct ieee80211_supported_band *sband;
  591. struct ieee80211_channel *ch;
  592. int band, i;
  593. bool ch_found = false;
  594. brcms_reg_apply_radar_flags(wiphy);
  595. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  596. brcms_reg_apply_beaconing_flags(wiphy, request->initiator);
  597. /* Disable radio if all channels disallowed by regulatory */
  598. for (band = 0; !ch_found && band < IEEE80211_NUM_BANDS; band++) {
  599. sband = wiphy->bands[band];
  600. if (!sband)
  601. continue;
  602. for (i = 0; !ch_found && i < sband->n_channels; i++) {
  603. ch = &sband->channels[i];
  604. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  605. ch_found = true;
  606. }
  607. }
  608. if (ch_found) {
  609. mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
  610. } else {
  611. mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
  612. brcms_err(wlc->hw->d11core,
  613. "wl%d: %s: no valid channel for \"%s\"\n",
  614. wlc->pub->unit, __func__, request->alpha2);
  615. }
  616. if (wlc->pub->_nbands > 1 || wlc->band->bandtype == BRCM_BAND_2G)
  617. wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
  618. brcms_c_japan_ccode(request->alpha2));
  619. return 0;
  620. }
  621. void brcms_c_regd_init(struct brcms_c_info *wlc)
  622. {
  623. struct wiphy *wiphy = wlc->wiphy;
  624. const struct brcms_regd *regd = wlc->cmi->world_regd;
  625. struct ieee80211_supported_band *sband;
  626. struct ieee80211_channel *ch;
  627. struct brcms_chanvec sup_chan;
  628. struct brcms_band *band;
  629. int band_idx, i;
  630. /* Disable any channels not supported by the phy */
  631. for (band_idx = 0; band_idx < wlc->pub->_nbands; band_idx++) {
  632. band = wlc->bandstate[band_idx];
  633. wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
  634. &sup_chan);
  635. if (band_idx == BAND_2G_INDEX)
  636. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  637. else
  638. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  639. for (i = 0; i < sband->n_channels; i++) {
  640. ch = &sband->channels[i];
  641. if (!isset(sup_chan.vec, ch->hw_value))
  642. ch->flags |= IEEE80211_CHAN_DISABLED;
  643. }
  644. }
  645. wlc->wiphy->reg_notifier = brcms_reg_notifier;
  646. wlc->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  647. WIPHY_FLAG_STRICT_REGULATORY;
  648. wiphy_apply_custom_regulatory(wlc->wiphy, regd->regdomain);
  649. brcms_reg_apply_beaconing_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER);
  650. }