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 
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 
22 #include <bcmdefs.h>
23 #include <bcmutils.h>
24 #include <siutils.h>
25 #include <sbhnddma.h>
26 #include <wlioctl.h>
27 
28 #include "wlc_types.h"
29 #include "d11.h"
30 #include "wlc_cfg.h"
31 #include "wlc_scb.h"
32 #include "wlc_pub.h"
33 #include "wlc_key.h"
34 #include "phy/wlc_phy_hal.h"
35 #include "wlc_bmac.h"
36 #include "wlc_rate.h"
37 #include "wlc_channel.h"
38 #include "wlc_main.h"
39 #include "wlc_stf.h"
40 #include "wl_dbg.h"
41 
42 #define	VALID_CHANNEL20_DB(wlc, val) wlc_valid_channel20_db((wlc)->cmi, val)
43 #define	VALID_CHANNEL20_IN_BAND(wlc, bandunit, val) \
44 	wlc_valid_channel20_in_band((wlc)->cmi, bandunit, val)
45 #define	VALID_CHANNEL20(wlc, val) wlc_valid_channel20((wlc)->cmi, val)
46 
47 typedef struct wlc_cm_band {
48 	u8 locale_flags;	/* locale_info_t flags */
49 	chanvec_t valid_channels;	/* List of valid channels in the country */
50 	const chanvec_t *restricted_channels;	/* List of restricted use channels */
51 	const chanvec_t *radar_channels;	/* List of radar sensitive channels */
52 	u8 PAD[8];
53 } wlc_cm_band_t;
54 
55 struct wlc_cm_info {
56 	struct wlc_pub *pub;
57 	struct wlc_info *wlc;
58 	char srom_ccode[WLC_CNTRY_BUF_SZ];	/* Country Code in SROM */
59 	uint srom_regrev;	/* Regulatory Rev for the SROM ccode */
60 	const country_info_t *country;	/* current country def */
61 	char ccode[WLC_CNTRY_BUF_SZ];	/* current internal Country Code */
62 	uint regrev;		/* current Regulatory Revision */
63 	char country_abbrev[WLC_CNTRY_BUF_SZ];	/* current advertised ccode */
64 	wlc_cm_band_t bandstate[MAXBANDS];	/* per-band state (one per phy/radio) */
65 	/* quiet channels currently for radar sensitivity or 11h support */
66 	chanvec_t quiet_channels;	/* channels on which we cannot transmit */
67 };
68 
69 static int wlc_channels_init(wlc_cm_info_t *wlc_cm,
70 			     const country_info_t *country);
71 static void wlc_set_country_common(wlc_cm_info_t *wlc_cm,
72 				   const char *country_abbrev,
73 				   const char *ccode, uint regrev,
74 				   const country_info_t *country);
75 static int wlc_set_countrycode(wlc_cm_info_t *wlc_cm, const char *ccode);
76 static int wlc_set_countrycode_rev(wlc_cm_info_t *wlc_cm,
77 				   const char *country_abbrev,
78 				   const char *ccode, int regrev);
79 static int wlc_country_aggregate_map(wlc_cm_info_t *wlc_cm, const char *ccode,
80 				     char *mapped_ccode, uint *mapped_regrev);
81 static const country_info_t *wlc_country_lookup_direct(const char *ccode,
82 						       uint regrev);
83 static const country_info_t *wlc_countrycode_map(wlc_cm_info_t *wlc_cm,
84 						 const char *ccode,
85 						 char *mapped_ccode,
86 						 uint *mapped_regrev);
87 static void wlc_channels_commit(wlc_cm_info_t *wlc_cm);
88 static void wlc_quiet_channels_reset(wlc_cm_info_t *wlc_cm);
89 static bool wlc_quiet_chanspec(wlc_cm_info_t *wlc_cm, chanspec_t chspec);
90 static bool wlc_valid_channel20_db(wlc_cm_info_t *wlc_cm, uint val);
91 static bool wlc_valid_channel20_in_band(wlc_cm_info_t *wlc_cm, uint bandunit,
92 					uint val);
93 static bool wlc_valid_channel20(wlc_cm_info_t *wlc_cm, uint val);
94 static const country_info_t *wlc_country_lookup(struct wlc_info *wlc,
95 						const char *ccode);
96 static void wlc_locale_get_channels(const locale_info_t *locale,
97 				    chanvec_t *valid_channels);
98 static const locale_info_t *wlc_get_locale_2g(u8 locale_idx);
99 static const locale_info_t *wlc_get_locale_5g(u8 locale_idx);
100 static bool wlc_japan(struct wlc_info *wlc);
101 static bool wlc_japan_ccode(const char *ccode);
102 static void wlc_channel_min_txpower_limits_with_local_constraint(wlc_cm_info_t *
103 								 wlc_cm,
104 								 struct
105 								 txpwr_limits
106 								 *txpwr,
107 								 u8
108 								 local_constraint_qdbm);
109 void wlc_locale_add_channels(chanvec_t *target, const chanvec_t *channels);
110 static const locale_mimo_info_t *wlc_get_mimo_2g(u8 locale_idx);
111 static const locale_mimo_info_t *wlc_get_mimo_5g(u8 locale_idx);
112 
113 /* QDB() macro takes a dB value and converts to a quarter dB value */
114 #ifdef QDB
115 #undef QDB
116 #endif
117 #define QDB(n) ((n) * WLC_TXPWR_DB_FACTOR)
118 
119 /* Regulatory Matrix Spreadsheet (CLM) MIMO v3.7.9 */
120 
121 /*
122  * Some common channel sets
123  */
124 
125 /* No channels */
126 static const chanvec_t chanvec_none = {
127 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 	 0x00, 0x00, 0x00, 0x00}
131 };
132 
133 /* All 2.4 GHz HW channels */
134 const chanvec_t chanvec_all_2G = {
135 	{0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 	 0x00, 0x00, 0x00, 0x00}
139 };
140 
141 /* All 5 GHz HW channels */
142 const chanvec_t chanvec_all_5G = {
143 	{0x00, 0x00, 0x00, 0x00, 0x54, 0x55, 0x11, 0x11,
144 	 0x01, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x11,
145 	 0x11, 0x11, 0x20, 0x22, 0x22, 0x00, 0x00, 0x11,
146 	 0x11, 0x11, 0x11, 0x01}
147 };
148 
149 /*
150  * Radar channel sets
151  */
152 
153 /* No radar */
154 #define radar_set_none chanvec_none
155 
156 static const chanvec_t radar_set1 = {	/* Channels 52 - 64, 100 - 140 */
157 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,	/* 52 - 60 */
158 	 0x01, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x11,	/* 64, 100 - 124 */
159 	 0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 128 - 140 */
160 	 0x00, 0x00, 0x00, 0x00}
161 };
162 
163 /*
164  * Restricted channel sets
165  */
166 
167 #define restricted_set_none chanvec_none
168 
169 /* Channels 34, 38, 42, 46 */
170 static const chanvec_t restricted_set_japan_legacy = {
171 	{0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x00, 0x00,
172 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
174 	 0x00, 0x00, 0x00, 0x00}
175 };
176 
177 /* Channels 12, 13 */
178 static const chanvec_t restricted_set_2g_short = {
179 	{0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182 	 0x00, 0x00, 0x00, 0x00}
183 };
184 
185 /* Channel 165 */
186 static const chanvec_t restricted_chan_165 = {
187 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 	 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
190 	 0x00, 0x00, 0x00, 0x00}
191 };
192 
193 /* Channels 36 - 48 & 149 - 165 */
194 static const chanvec_t restricted_low_hi = {
195 	{0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x01, 0x00,
196 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 	 0x00, 0x00, 0x20, 0x22, 0x22, 0x00, 0x00, 0x00,
198 	 0x00, 0x00, 0x00, 0x00}
199 };
200 
201 /* Channels 12 - 14 */
202 static const chanvec_t restricted_set_12_13_14 = {
203 	{0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 	 0x00, 0x00, 0x00, 0x00}
207 };
208 
209 #define  LOCALE_CHAN_01_11	 (1<<0)
210 #define  LOCALE_CHAN_12_13	 (1<<1)
211 #define  LOCALE_CHAN_14		 (1<<2)
212 #define  LOCALE_SET_5G_LOW_JP1   (1<<3)	/* 34-48, step 2 */
213 #define  LOCALE_SET_5G_LOW_JP2   (1<<4)	/* 34-46, step 4 */
214 #define  LOCALE_SET_5G_LOW1      (1<<5)	/* 36-48, step 4 */
215 #define  LOCALE_SET_5G_LOW2      (1<<6)	/* 52 */
216 #define  LOCALE_SET_5G_LOW3      (1<<7)	/* 56-64, step 4 */
217 #define  LOCALE_SET_5G_MID1      (1<<8)	/* 100-116, step 4 */
218 #define  LOCALE_SET_5G_MID2	 (1<<9)	/* 120-124, step 4 */
219 #define  LOCALE_SET_5G_MID3      (1<<10)	/* 128 */
220 #define  LOCALE_SET_5G_HIGH1     (1<<11)	/* 132-140, step 4 */
221 #define  LOCALE_SET_5G_HIGH2     (1<<12)	/* 149-161, step 4 */
222 #define  LOCALE_SET_5G_HIGH3     (1<<13)	/* 165 */
223 #define  LOCALE_CHAN_52_140_ALL  (1<<14)
224 #define  LOCALE_SET_5G_HIGH4     (1<<15)	/* 184-216 */
225 
226 #define  LOCALE_CHAN_36_64       (LOCALE_SET_5G_LOW1 | LOCALE_SET_5G_LOW2 | LOCALE_SET_5G_LOW3)
227 #define  LOCALE_CHAN_52_64       (LOCALE_SET_5G_LOW2 | LOCALE_SET_5G_LOW3)
228 #define  LOCALE_CHAN_100_124	 (LOCALE_SET_5G_MID1 | LOCALE_SET_5G_MID2)
229 #define  LOCALE_CHAN_100_140     \
230 	(LOCALE_SET_5G_MID1 | LOCALE_SET_5G_MID2 | LOCALE_SET_5G_MID3 | LOCALE_SET_5G_HIGH1)
231 #define  LOCALE_CHAN_149_165     (LOCALE_SET_5G_HIGH2 | LOCALE_SET_5G_HIGH3)
232 #define  LOCALE_CHAN_184_216     LOCALE_SET_5G_HIGH4
233 
234 #define  LOCALE_CHAN_01_14	(LOCALE_CHAN_01_11 | LOCALE_CHAN_12_13 | LOCALE_CHAN_14)
235 
236 #define  LOCALE_RADAR_SET_NONE		  0
237 #define  LOCALE_RADAR_SET_1		  1
238 
239 #define  LOCALE_RESTRICTED_NONE		  0
240 #define  LOCALE_RESTRICTED_SET_2G_SHORT   1
241 #define  LOCALE_RESTRICTED_CHAN_165       2
242 #define  LOCALE_CHAN_ALL_5G		  3
243 #define  LOCALE_RESTRICTED_JAPAN_LEGACY   4
244 #define  LOCALE_RESTRICTED_11D_2G	  5
245 #define  LOCALE_RESTRICTED_11D_5G	  6
246 #define  LOCALE_RESTRICTED_LOW_HI	  7
247 #define  LOCALE_RESTRICTED_12_13_14	  8
248 
249 /* global memory to provide working buffer for expanded locale */
250 
251 static const chanvec_t *g_table_radar_set[] = {
252 	&chanvec_none,
253 	&radar_set1
254 };
255 
256 static const chanvec_t *g_table_restricted_chan[] = {
257 	&chanvec_none,		/* restricted_set_none */
258 	&restricted_set_2g_short,
259 	&restricted_chan_165,
260 	&chanvec_all_5G,
261 	&restricted_set_japan_legacy,
262 	&chanvec_all_2G,	/* restricted_set_11d_2G */
263 	&chanvec_all_5G,	/* restricted_set_11d_5G */
264 	&restricted_low_hi,
265 	&restricted_set_12_13_14
266 };
267 
268 static const chanvec_t locale_2g_01_11 = {
269 	{0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
270 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272 	 0x00, 0x00, 0x00, 0x00}
273 };
274 
275 static const chanvec_t locale_2g_12_13 = {
276 	{0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 	 0x00, 0x00, 0x00, 0x00}
280 };
281 
282 static const chanvec_t locale_2g_14 = {
283 	{0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
285 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 	 0x00, 0x00, 0x00, 0x00}
287 };
288 
289 static const chanvec_t locale_5g_LOW_JP1 = {
290 	{0x00, 0x00, 0x00, 0x00, 0x54, 0x55, 0x01, 0x00,
291 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 	 0x00, 0x00, 0x00, 0x00}
294 };
295 
296 static const chanvec_t locale_5g_LOW_JP2 = {
297 	{0x00, 0x00, 0x00, 0x00, 0x44, 0x44, 0x00, 0x00,
298 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
299 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 	 0x00, 0x00, 0x00, 0x00}
301 };
302 
303 static const chanvec_t locale_5g_LOW1 = {
304 	{0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x01, 0x00,
305 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 	 0x00, 0x00, 0x00, 0x00}
308 };
309 
310 static const chanvec_t locale_5g_LOW2 = {
311 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
312 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314 	 0x00, 0x00, 0x00, 0x00}
315 };
316 
317 static const chanvec_t locale_5g_LOW3 = {
318 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
319 	 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321 	 0x00, 0x00, 0x00, 0x00}
322 };
323 
324 static const chanvec_t locale_5g_MID1 = {
325 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 	 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x11, 0x00,
327 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 	 0x00, 0x00, 0x00, 0x00}
329 };
330 
331 static const chanvec_t locale_5g_MID2 = {
332 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
333 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
334 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335 	 0x00, 0x00, 0x00, 0x00}
336 };
337 
338 static const chanvec_t locale_5g_MID3 = {
339 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341 	 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342 	 0x00, 0x00, 0x00, 0x00}
343 };
344 
345 static const chanvec_t locale_5g_HIGH1 = {
346 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 	 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349 	 0x00, 0x00, 0x00, 0x00}
350 };
351 
352 static const chanvec_t locale_5g_HIGH2 = {
353 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 	 0x00, 0x00, 0x20, 0x22, 0x02, 0x00, 0x00, 0x00,
356 	 0x00, 0x00, 0x00, 0x00}
357 };
358 
359 static const chanvec_t locale_5g_HIGH3 = {
360 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362 	 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
363 	 0x00, 0x00, 0x00, 0x00}
364 };
365 
366 static const chanvec_t locale_5g_52_140_ALL = {
367 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,
368 	 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
369 	 0x11, 0x11, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
370 	 0x00, 0x00, 0x00, 0x00}
371 };
372 
373 static const chanvec_t locale_5g_HIGH4 = {
374 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
375 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
377 	 0x11, 0x11, 0x11, 0x11}
378 };
379 
380 static const chanvec_t *g_table_locale_base[] = {
381 	&locale_2g_01_11,
382 	&locale_2g_12_13,
383 	&locale_2g_14,
384 	&locale_5g_LOW_JP1,
385 	&locale_5g_LOW_JP2,
386 	&locale_5g_LOW1,
387 	&locale_5g_LOW2,
388 	&locale_5g_LOW3,
389 	&locale_5g_MID1,
390 	&locale_5g_MID2,
391 	&locale_5g_MID3,
392 	&locale_5g_HIGH1,
393 	&locale_5g_HIGH2,
394 	&locale_5g_HIGH3,
395 	&locale_5g_52_140_ALL,
396 	&locale_5g_HIGH4
397 };
398 
wlc_locale_add_channels(chanvec_t * target,const chanvec_t * channels)399 void wlc_locale_add_channels(chanvec_t *target, const chanvec_t *channels)
400 {
401 	u8 i;
402 	for (i = 0; i < sizeof(chanvec_t); i++) {
403 		target->vec[i] |= channels->vec[i];
404 	}
405 }
406 
wlc_locale_get_channels(const locale_info_t * locale,chanvec_t * channels)407 static void wlc_locale_get_channels(const locale_info_t *locale,
408 				    chanvec_t *channels)
409 {
410 	u8 i;
411 
412 	memset(channels, 0, sizeof(chanvec_t));
413 
414 	for (i = 0; i < ARRAY_SIZE(g_table_locale_base); i++) {
415 		if (locale->valid_channels & (1 << i)) {
416 			wlc_locale_add_channels(channels,
417 						g_table_locale_base[i]);
418 		}
419 	}
420 }
421 
422 /*
423  * Locale Definitions - 2.4 GHz
424  */
425 static const locale_info_t locale_i = {	/* locale i. channel 1 - 13 */
426 	LOCALE_CHAN_01_11 | LOCALE_CHAN_12_13,
427 	LOCALE_RADAR_SET_NONE,
428 	LOCALE_RESTRICTED_SET_2G_SHORT,
429 	{QDB(19), QDB(19), QDB(19),
430 	 QDB(19), QDB(19), QDB(19)},
431 	{20, 20, 20, 0},
432 	WLC_EIRP
433 };
434 
435 /*
436  * Locale Definitions - 5 GHz
437  */
438 static const locale_info_t locale_11 = {
439 	/* locale 11. channel 36 - 48, 52 - 64, 100 - 140, 149 - 165 */
440 	LOCALE_CHAN_36_64 | LOCALE_CHAN_100_140 | LOCALE_CHAN_149_165,
441 	LOCALE_RADAR_SET_1,
442 	LOCALE_RESTRICTED_NONE,
443 	{QDB(21), QDB(21), QDB(21), QDB(21), QDB(21)},
444 	{23, 23, 23, 30, 30},
445 	WLC_EIRP | WLC_DFS_EU
446 };
447 
448 #define LOCALE_2G_IDX_i			0
449 static const locale_info_t *g_locale_2g_table[] = {
450 	&locale_i
451 };
452 
453 #define LOCALE_5G_IDX_11	0
454 static const locale_info_t *g_locale_5g_table[] = {
455 	&locale_11
456 };
457 
458 /*
459  * MIMO Locale Definitions - 2.4 GHz
460  */
461 static const locale_mimo_info_t locale_bn = {
462 	{QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
463 	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
464 	 QDB(13), QDB(13), QDB(13)},
465 	{0, 0, QDB(13), QDB(13), QDB(13),
466 	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
467 	 QDB(13), 0, 0},
468 	0
469 };
470 
471 /* locale mimo 2g indexes */
472 #define LOCALE_MIMO_IDX_bn			0
473 
474 static const locale_mimo_info_t *g_mimo_2g_table[] = {
475 	&locale_bn
476 };
477 
478 /*
479  * MIMO Locale Definitions - 5 GHz
480  */
481 static const locale_mimo_info_t locale_11n = {
482 	{ /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
483 	{QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
484 	0
485 };
486 
487 #define LOCALE_MIMO_IDX_11n			0
488 static const locale_mimo_info_t *g_mimo_5g_table[] = {
489 	&locale_11n
490 };
491 
492 #ifdef LC
493 #undef LC
494 #endif
495 #define LC(id)	LOCALE_MIMO_IDX_ ## id
496 
497 #ifdef LC_2G
498 #undef LC_2G
499 #endif
500 #define LC_2G(id)	LOCALE_2G_IDX_ ## id
501 
502 #ifdef LC_5G
503 #undef LC_5G
504 #endif
505 #define LC_5G(id)	LOCALE_5G_IDX_ ## id
506 
507 #define LOCALES(band2, band5, mimo2, mimo5)     {LC_2G(band2), LC_5G(band5), LC(mimo2), LC(mimo5)}
508 
509 static const struct {
510 	char abbrev[WLC_CNTRY_BUF_SZ];	/* country abbreviation */
511 	country_info_t country;
512 } cntry_locales[] = {
513 	{
514 	"X2", LOCALES(i, 11, bn, 11n)},	/* Worldwide RoW 2 */
515 };
516 
517 #ifdef SUPPORT_40MHZ
518 /* 20MHz channel info for 40MHz pairing support */
519 struct chan20_info {
520 	u8 sb;
521 	u8 adj_sbs;
522 };
523 
524 /* indicates adjacent channels that are allowed for a 40 Mhz channel and
525  * those that permitted by the HT
526  */
527 struct chan20_info chan20_info[] = {
528 	/* 11b/11g */
529 /* 0 */ {1, (CH_UPPER_SB | CH_EWA_VALID)},
530 /* 1 */ {2, (CH_UPPER_SB | CH_EWA_VALID)},
531 /* 2 */ {3, (CH_UPPER_SB | CH_EWA_VALID)},
532 /* 3 */ {4, (CH_UPPER_SB | CH_EWA_VALID)},
533 /* 4 */ {5, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
534 /* 5 */ {6, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
535 /* 6 */ {7, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
536 /* 7 */ {8, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
537 /* 8 */ {9, (CH_UPPER_SB | CH_LOWER_SB | CH_EWA_VALID)},
538 /* 9 */ {10, (CH_LOWER_SB | CH_EWA_VALID)},
539 /* 10 */ {11, (CH_LOWER_SB | CH_EWA_VALID)},
540 /* 11 */ {12, (CH_LOWER_SB)},
541 /* 12 */ {13, (CH_LOWER_SB)},
542 /* 13 */ {14, (CH_LOWER_SB)},
543 
544 /* 11a japan high */
545 /* 14 */ {34, (CH_UPPER_SB)},
546 /* 15 */ {38, (CH_LOWER_SB)},
547 /* 16 */ {42, (CH_LOWER_SB)},
548 /* 17 */ {46, (CH_LOWER_SB)},
549 
550 /* 11a usa low */
551 /* 18 */ {36, (CH_UPPER_SB | CH_EWA_VALID)},
552 /* 19 */ {40, (CH_LOWER_SB | CH_EWA_VALID)},
553 /* 20 */ {44, (CH_UPPER_SB | CH_EWA_VALID)},
554 /* 21 */ {48, (CH_LOWER_SB | CH_EWA_VALID)},
555 /* 22 */ {52, (CH_UPPER_SB | CH_EWA_VALID)},
556 /* 23 */ {56, (CH_LOWER_SB | CH_EWA_VALID)},
557 /* 24 */ {60, (CH_UPPER_SB | CH_EWA_VALID)},
558 /* 25 */ {64, (CH_LOWER_SB | CH_EWA_VALID)},
559 
560 /* 11a Europe */
561 /* 26 */ {100, (CH_UPPER_SB | CH_EWA_VALID)},
562 /* 27 */ {104, (CH_LOWER_SB | CH_EWA_VALID)},
563 /* 28 */ {108, (CH_UPPER_SB | CH_EWA_VALID)},
564 /* 29 */ {112, (CH_LOWER_SB | CH_EWA_VALID)},
565 /* 30 */ {116, (CH_UPPER_SB | CH_EWA_VALID)},
566 /* 31 */ {120, (CH_LOWER_SB | CH_EWA_VALID)},
567 /* 32 */ {124, (CH_UPPER_SB | CH_EWA_VALID)},
568 /* 33 */ {128, (CH_LOWER_SB | CH_EWA_VALID)},
569 /* 34 */ {132, (CH_UPPER_SB | CH_EWA_VALID)},
570 /* 35 */ {136, (CH_LOWER_SB | CH_EWA_VALID)},
571 /* 36 */ {140, (CH_LOWER_SB)},
572 
573 /* 11a usa high, ref5 only */
574 /* The 0x80 bit in pdiv means these are REF5, other entries are REF20 */
575 /* 37 */ {149, (CH_UPPER_SB | CH_EWA_VALID)},
576 /* 38 */ {153, (CH_LOWER_SB | CH_EWA_VALID)},
577 /* 39 */ {157, (CH_UPPER_SB | CH_EWA_VALID)},
578 /* 40 */ {161, (CH_LOWER_SB | CH_EWA_VALID)},
579 /* 41 */ {165, (CH_LOWER_SB)},
580 
581 /* 11a japan */
582 /* 42 */ {184, (CH_UPPER_SB)},
583 /* 43 */ {188, (CH_LOWER_SB)},
584 /* 44 */ {192, (CH_UPPER_SB)},
585 /* 45 */ {196, (CH_LOWER_SB)},
586 /* 46 */ {200, (CH_UPPER_SB)},
587 /* 47 */ {204, (CH_LOWER_SB)},
588 /* 48 */ {208, (CH_UPPER_SB)},
589 /* 49 */ {212, (CH_LOWER_SB)},
590 /* 50 */ {216, (CH_LOWER_SB)}
591 };
592 #endif				/* SUPPORT_40MHZ */
593 
wlc_get_locale_2g(u8 locale_idx)594 static const locale_info_t *wlc_get_locale_2g(u8 locale_idx)
595 {
596 	if (locale_idx >= ARRAY_SIZE(g_locale_2g_table)) {
597 		WL_ERROR("%s: locale 2g index size out of range %d\n",
598 			 __func__, locale_idx);
599 		ASSERT(locale_idx < ARRAY_SIZE(g_locale_2g_table));
600 		return NULL;
601 	}
602 	return g_locale_2g_table[locale_idx];
603 }
604 
wlc_get_locale_5g(u8 locale_idx)605 static const locale_info_t *wlc_get_locale_5g(u8 locale_idx)
606 {
607 	if (locale_idx >= ARRAY_SIZE(g_locale_5g_table)) {
608 		WL_ERROR("%s: locale 5g index size out of range %d\n",
609 			 __func__, locale_idx);
610 		ASSERT(locale_idx < ARRAY_SIZE(g_locale_5g_table));
611 		return NULL;
612 	}
613 	return g_locale_5g_table[locale_idx];
614 }
615 
wlc_get_mimo_2g(u8 locale_idx)616 const locale_mimo_info_t *wlc_get_mimo_2g(u8 locale_idx)
617 {
618 	if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table)) {
619 		WL_ERROR("%s: mimo 2g index size out of range %d\n",
620 			 __func__, locale_idx);
621 		return NULL;
622 	}
623 	return g_mimo_2g_table[locale_idx];
624 }
625 
wlc_get_mimo_5g(u8 locale_idx)626 const locale_mimo_info_t *wlc_get_mimo_5g(u8 locale_idx)
627 {
628 	if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table)) {
629 		WL_ERROR("%s: mimo 5g index size out of range %d\n",
630 			 __func__, locale_idx);
631 		return NULL;
632 	}
633 	return g_mimo_5g_table[locale_idx];
634 }
635 
wlc_channel_mgr_attach(struct wlc_info * wlc)636 wlc_cm_info_t *wlc_channel_mgr_attach(struct wlc_info *wlc)
637 {
638 	wlc_cm_info_t *wlc_cm;
639 	char country_abbrev[WLC_CNTRY_BUF_SZ];
640 	const country_info_t *country;
641 	struct wlc_pub *pub = wlc->pub;
642 	char *ccode;
643 
644 	WL_TRACE("wl%d: wlc_channel_mgr_attach\n", wlc->pub->unit);
645 
646 	wlc_cm = kzalloc(sizeof(wlc_cm_info_t), GFP_ATOMIC);
647 	if (wlc_cm == NULL) {
648 		WL_ERROR("wl%d: %s: out of memory", pub->unit, __func__);
649 		return NULL;
650 	}
651 	wlc_cm->pub = pub;
652 	wlc_cm->wlc = wlc;
653 	wlc->cmi = wlc_cm;
654 
655 	/* store the country code for passing up as a regulatory hint */
656 	ccode = getvar(wlc->pub->vars, "ccode");
657 	if (ccode) {
658 		strncpy(wlc->pub->srom_ccode, ccode, WLC_CNTRY_BUF_SZ - 1);
659 		WL_NONE("%s: SROM country code is %c%c\n",
660 			__func__,
661 			wlc->pub->srom_ccode[0], wlc->pub->srom_ccode[1]);
662 	}
663 
664 	/* internal country information which must match regulatory constraints in firmware */
665 	memset(country_abbrev, 0, WLC_CNTRY_BUF_SZ);
666 	strncpy(country_abbrev, "X2", sizeof(country_abbrev) - 1);
667 	country = wlc_country_lookup(wlc, country_abbrev);
668 
669 	ASSERT(country != NULL);
670 
671 	/* save default country for exiting 11d regulatory mode */
672 	strncpy(wlc->country_default, country_abbrev, WLC_CNTRY_BUF_SZ - 1);
673 
674 	/* initialize autocountry_default to driver default */
675 	strncpy(wlc->autocountry_default, "X2", WLC_CNTRY_BUF_SZ - 1);
676 
677 	wlc_set_countrycode(wlc_cm, country_abbrev);
678 
679 	return wlc_cm;
680 }
681 
wlc_channel_mgr_detach(wlc_cm_info_t * wlc_cm)682 void wlc_channel_mgr_detach(wlc_cm_info_t *wlc_cm)
683 {
684 	kfree(wlc_cm);
685 }
686 
wlc_channel_locale_flags_in_band(wlc_cm_info_t * wlc_cm,uint bandunit)687 u8 wlc_channel_locale_flags_in_band(wlc_cm_info_t *wlc_cm, uint bandunit)
688 {
689 	return wlc_cm->bandstate[bandunit].locale_flags;
690 }
691 
692 /* set the driver's current country and regulatory information using a country code
693  * as the source. Lookup built in country information found with the country code.
694  */
wlc_set_countrycode(wlc_cm_info_t * wlc_cm,const char * ccode)695 static int wlc_set_countrycode(wlc_cm_info_t *wlc_cm, const char *ccode)
696 {
697 	char country_abbrev[WLC_CNTRY_BUF_SZ];
698 	strncpy(country_abbrev, ccode, WLC_CNTRY_BUF_SZ);
699 	return wlc_set_countrycode_rev(wlc_cm, country_abbrev, ccode, -1);
700 }
701 
702 static int
wlc_set_countrycode_rev(wlc_cm_info_t * wlc_cm,const char * country_abbrev,const char * ccode,int regrev)703 wlc_set_countrycode_rev(wlc_cm_info_t *wlc_cm,
704 			const char *country_abbrev,
705 			const char *ccode, int regrev)
706 {
707 	const country_info_t *country;
708 	char mapped_ccode[WLC_CNTRY_BUF_SZ];
709 	uint mapped_regrev;
710 
711 	WL_NONE("%s: (country_abbrev \"%s\", ccode \"%s\", regrev %d) SPROM \"%s\"/%u\n",
712 		__func__, country_abbrev, ccode, regrev,
713 		wlc_cm->srom_ccode, wlc_cm->srom_regrev);
714 
715 	/* if regrev is -1, lookup the mapped country code,
716 	 * otherwise use the ccode and regrev directly
717 	 */
718 	if (regrev == -1) {
719 		/* map the country code to a built-in country code, regrev, and country_info */
720 		country =
721 		    wlc_countrycode_map(wlc_cm, ccode, mapped_ccode,
722 					&mapped_regrev);
723 	} else {
724 		/* find the matching built-in country definition */
725 		ASSERT(0);
726 		country = wlc_country_lookup_direct(ccode, regrev);
727 		strncpy(mapped_ccode, ccode, WLC_CNTRY_BUF_SZ);
728 		mapped_regrev = regrev;
729 	}
730 
731 	if (country == NULL)
732 		return BCME_BADARG;
733 
734 	/* set the driver state for the country */
735 	wlc_set_country_common(wlc_cm, country_abbrev, mapped_ccode,
736 			       mapped_regrev, country);
737 
738 	return 0;
739 }
740 
741 /* set the driver's current country and regulatory information using a country code
742  * as the source. Look up built in country information found with the country code.
743  */
744 static void
wlc_set_country_common(wlc_cm_info_t * wlc_cm,const char * country_abbrev,const char * ccode,uint regrev,const country_info_t * country)745 wlc_set_country_common(wlc_cm_info_t *wlc_cm,
746 		       const char *country_abbrev,
747 		       const char *ccode, uint regrev,
748 		       const country_info_t *country)
749 {
750 	const locale_mimo_info_t *li_mimo;
751 	const locale_info_t *locale;
752 	struct wlc_info *wlc = wlc_cm->wlc;
753 	char prev_country_abbrev[WLC_CNTRY_BUF_SZ];
754 
755 	ASSERT(country != NULL);
756 
757 	/* save current country state */
758 	wlc_cm->country = country;
759 
760 	memset(&prev_country_abbrev, 0, WLC_CNTRY_BUF_SZ);
761 	strncpy(prev_country_abbrev, wlc_cm->country_abbrev,
762 		WLC_CNTRY_BUF_SZ - 1);
763 
764 	strncpy(wlc_cm->country_abbrev, country_abbrev, WLC_CNTRY_BUF_SZ - 1);
765 	strncpy(wlc_cm->ccode, ccode, WLC_CNTRY_BUF_SZ - 1);
766 	wlc_cm->regrev = regrev;
767 
768 	/* disable/restore nmode based on country regulations */
769 	li_mimo = wlc_get_mimo_2g(country->locale_mimo_2G);
770 	if (li_mimo && (li_mimo->flags & WLC_NO_MIMO)) {
771 		wlc_set_nmode(wlc, OFF);
772 		wlc->stf->no_cddstbc = true;
773 	} else {
774 		wlc->stf->no_cddstbc = false;
775 		if (N_ENAB(wlc->pub) != wlc->protection->nmode_user)
776 			wlc_set_nmode(wlc, wlc->protection->nmode_user);
777 	}
778 
779 	wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
780 	wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
781 	/* set or restore gmode as required by regulatory */
782 	locale = wlc_get_locale_2g(country->locale_2G);
783 	if (locale && (locale->flags & WLC_NO_OFDM)) {
784 		wlc_set_gmode(wlc, GMODE_LEGACY_B, false);
785 	} else {
786 		wlc_set_gmode(wlc, wlc->protection->gmode_user, false);
787 	}
788 
789 	wlc_channels_init(wlc_cm, country);
790 
791 	return;
792 }
793 
794 /* Lookup a country info structure from a null terminated country code
795  * The lookup is case sensitive.
796  */
wlc_country_lookup(struct wlc_info * wlc,const char * ccode)797 static const country_info_t *wlc_country_lookup(struct wlc_info *wlc,
798 					 const char *ccode)
799 {
800 	const country_info_t *country;
801 	char mapped_ccode[WLC_CNTRY_BUF_SZ];
802 	uint mapped_regrev;
803 
804 	/* map the country code to a built-in country code, regrev, and country_info struct */
805 	country =
806 	    wlc_countrycode_map(wlc->cmi, ccode, mapped_ccode, &mapped_regrev);
807 
808 	return country;
809 }
810 
wlc_countrycode_map(wlc_cm_info_t * wlc_cm,const char * ccode,char * mapped_ccode,uint * mapped_regrev)811 static const country_info_t *wlc_countrycode_map(wlc_cm_info_t *wlc_cm,
812 						 const char *ccode,
813 						 char *mapped_ccode,
814 						 uint *mapped_regrev)
815 {
816 	struct wlc_info *wlc = wlc_cm->wlc;
817 	const country_info_t *country;
818 	uint srom_regrev = wlc_cm->srom_regrev;
819 	const char *srom_ccode = wlc_cm->srom_ccode;
820 	int mapped;
821 
822 	/* check for currently supported ccode size */
823 	if (strlen(ccode) > (WLC_CNTRY_BUF_SZ - 1)) {
824 		WL_ERROR("wl%d: %s: ccode \"%s\" too long for match\n",
825 			 wlc->pub->unit, __func__, ccode);
826 		return NULL;
827 	}
828 
829 	/* default mapping is the given ccode and regrev 0 */
830 	strncpy(mapped_ccode, ccode, WLC_CNTRY_BUF_SZ);
831 	*mapped_regrev = 0;
832 
833 	/* If the desired country code matches the srom country code,
834 	 * then the mapped country is the srom regulatory rev.
835 	 * Otherwise look for an aggregate mapping.
836 	 */
837 	if (!strcmp(srom_ccode, ccode)) {
838 		*mapped_regrev = srom_regrev;
839 		mapped = 0;
840 		WL_ERROR("srom_code == ccode %s\n", __func__);
841 		ASSERT(0);
842 	} else {
843 		mapped =
844 		    wlc_country_aggregate_map(wlc_cm, ccode, mapped_ccode,
845 					      mapped_regrev);
846 	}
847 
848 	/* find the matching built-in country definition */
849 	country = wlc_country_lookup_direct(mapped_ccode, *mapped_regrev);
850 
851 	/* if there is not an exact rev match, default to rev zero */
852 	if (country == NULL && *mapped_regrev != 0) {
853 		*mapped_regrev = 0;
854 		ASSERT(0);
855 		country =
856 		    wlc_country_lookup_direct(mapped_ccode, *mapped_regrev);
857 	}
858 
859 	return country;
860 }
861 
862 static int
wlc_country_aggregate_map(wlc_cm_info_t * wlc_cm,const char * ccode,char * mapped_ccode,uint * mapped_regrev)863 wlc_country_aggregate_map(wlc_cm_info_t *wlc_cm, const char *ccode,
864 			  char *mapped_ccode, uint *mapped_regrev)
865 {
866 	return false;
867 }
868 
869 /* Lookup a country info structure from a null terminated country
870  * abbreviation and regrev directly with no translation.
871  */
wlc_country_lookup_direct(const char * ccode,uint regrev)872 static const country_info_t *wlc_country_lookup_direct(const char *ccode,
873 						       uint regrev)
874 {
875 	uint size, i;
876 
877 	/* Should just return 0 for single locale driver. */
878 	/* Keep it this way in case we add more locales. (for now anyway) */
879 
880 	/* all other country def arrays are for regrev == 0, so if regrev is non-zero, fail */
881 	if (regrev > 0)
882 		return NULL;
883 
884 	/* find matched table entry from country code */
885 	size = ARRAY_SIZE(cntry_locales);
886 	for (i = 0; i < size; i++) {
887 		if (strcmp(ccode, cntry_locales[i].abbrev) == 0) {
888 			return &cntry_locales[i].country;
889 		}
890 	}
891 
892 	WL_ERROR("%s: Returning NULL\n", __func__);
893 	ASSERT(0);
894 	return NULL;
895 }
896 
897 static int
wlc_channels_init(wlc_cm_info_t * wlc_cm,const country_info_t * country)898 wlc_channels_init(wlc_cm_info_t *wlc_cm, const country_info_t *country)
899 {
900 	struct wlc_info *wlc = wlc_cm->wlc;
901 	uint i, j;
902 	struct wlcband *band;
903 	const locale_info_t *li;
904 	chanvec_t sup_chan;
905 	const locale_mimo_info_t *li_mimo;
906 
907 	band = wlc->band;
908 	for (i = 0; i < NBANDS(wlc);
909 	     i++, band = wlc->bandstate[OTHERBANDUNIT(wlc)]) {
910 
911 		li = BAND_5G(band->bandtype) ?
912 		    wlc_get_locale_5g(country->locale_5G) :
913 		    wlc_get_locale_2g(country->locale_2G);
914 		ASSERT(li);
915 		wlc_cm->bandstate[band->bandunit].locale_flags = li->flags;
916 		li_mimo = BAND_5G(band->bandtype) ?
917 		    wlc_get_mimo_5g(country->locale_mimo_5G) :
918 		    wlc_get_mimo_2g(country->locale_mimo_2G);
919 		ASSERT(li_mimo);
920 
921 		/* merge the mimo non-mimo locale flags */
922 		wlc_cm->bandstate[band->bandunit].locale_flags |=
923 		    li_mimo->flags;
924 
925 		wlc_cm->bandstate[band->bandunit].restricted_channels =
926 		    g_table_restricted_chan[li->restricted_channels];
927 		wlc_cm->bandstate[band->bandunit].radar_channels =
928 		    g_table_radar_set[li->radar_channels];
929 
930 		/* set the channel availability,
931 		 * masking out the channels that may not be supported on this phy
932 		 */
933 		wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
934 					      &sup_chan);
935 		wlc_locale_get_channels(li,
936 					&wlc_cm->bandstate[band->bandunit].
937 					valid_channels);
938 		for (j = 0; j < sizeof(chanvec_t); j++)
939 			wlc_cm->bandstate[band->bandunit].valid_channels.
940 			    vec[j] &= sup_chan.vec[j];
941 	}
942 
943 	wlc_quiet_channels_reset(wlc_cm);
944 	wlc_channels_commit(wlc_cm);
945 
946 	return 0;
947 }
948 
949 /* Update the radio state (enable/disable) and tx power targets
950  * based on a new set of channel/regulatory information
951  */
wlc_channels_commit(wlc_cm_info_t * wlc_cm)952 static void wlc_channels_commit(wlc_cm_info_t *wlc_cm)
953 {
954 	struct wlc_info *wlc = wlc_cm->wlc;
955 	uint chan;
956 	struct txpwr_limits txpwr;
957 
958 	/* search for the existence of any valid channel */
959 	for (chan = 0; chan < MAXCHANNEL; chan++) {
960 		if (VALID_CHANNEL20_DB(wlc, chan)) {
961 			break;
962 		}
963 	}
964 	if (chan == MAXCHANNEL)
965 		chan = INVCHANNEL;
966 
967 	/* based on the channel search above, set or clear WL_RADIO_COUNTRY_DISABLE */
968 	if (chan == INVCHANNEL) {
969 		/* country/locale with no valid channels, set the radio disable bit */
970 		mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
971 		WL_ERROR("wl%d: %s: no valid channel for \"%s\" nbands %d bandlocked %d\n",
972 			 wlc->pub->unit, __func__,
973 			 wlc_cm->country_abbrev, NBANDS(wlc), wlc->bandlocked);
974 	} else
975 	    if (mboolisset(wlc->pub->radio_disabled,
976 		WL_RADIO_COUNTRY_DISABLE)) {
977 		/* country/locale with valid channel, clear the radio disable bit */
978 		mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
979 	}
980 
981 	/* Now that the country abbreviation is set, if the radio supports 2G, then
982 	 * set channel 14 restrictions based on the new locale.
983 	 */
984 	if (NBANDS(wlc) > 1 || BAND_2G(wlc->band->bandtype)) {
985 		wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
986 						     wlc_japan(wlc) ? true :
987 						     false);
988 	}
989 
990 	if (wlc->pub->up && chan != INVCHANNEL) {
991 		wlc_channel_reg_limits(wlc_cm, wlc->chanspec, &txpwr);
992 		wlc_channel_min_txpower_limits_with_local_constraint(wlc_cm,
993 								     &txpwr,
994 								     WLC_TXPWR_MAX);
995 		wlc_phy_txpower_limit_set(wlc->band->pi, &txpwr, wlc->chanspec);
996 	}
997 }
998 
999 /* reset the quiet channels vector to the union of the restricted and radar channel sets */
wlc_quiet_channels_reset(wlc_cm_info_t * wlc_cm)1000 static void wlc_quiet_channels_reset(wlc_cm_info_t *wlc_cm)
1001 {
1002 	struct wlc_info *wlc = wlc_cm->wlc;
1003 	uint i, j;
1004 	struct wlcband *band;
1005 	const chanvec_t *chanvec;
1006 
1007 	memset(&wlc_cm->quiet_channels, 0, sizeof(chanvec_t));
1008 
1009 	band = wlc->band;
1010 	for (i = 0; i < NBANDS(wlc);
1011 	     i++, band = wlc->bandstate[OTHERBANDUNIT(wlc)]) {
1012 
1013 		/* initialize quiet channels for restricted channels */
1014 		chanvec = wlc_cm->bandstate[band->bandunit].restricted_channels;
1015 		for (j = 0; j < sizeof(chanvec_t); j++)
1016 			wlc_cm->quiet_channels.vec[j] |= chanvec->vec[j];
1017 
1018 	}
1019 }
1020 
wlc_quiet_chanspec(wlc_cm_info_t * wlc_cm,chanspec_t chspec)1021 static bool wlc_quiet_chanspec(wlc_cm_info_t *wlc_cm, chanspec_t chspec)
1022 {
1023 	return N_ENAB(wlc_cm->wlc->pub) && CHSPEC_IS40(chspec) ?
1024 		(isset
1025 		 (wlc_cm->quiet_channels.vec,
1026 		  LOWER_20_SB(CHSPEC_CHANNEL(chspec)))
1027 		 || isset(wlc_cm->quiet_channels.vec,
1028 			  UPPER_20_SB(CHSPEC_CHANNEL(chspec)))) : isset(wlc_cm->
1029 									quiet_channels.
1030 									vec,
1031 									CHSPEC_CHANNEL
1032 									(chspec));
1033 }
1034 
1035 /* Is the channel valid for the current locale? (but don't consider channels not
1036  *   available due to bandlocking)
1037  */
wlc_valid_channel20_db(wlc_cm_info_t * wlc_cm,uint val)1038 static bool wlc_valid_channel20_db(wlc_cm_info_t *wlc_cm, uint val)
1039 {
1040 	struct wlc_info *wlc = wlc_cm->wlc;
1041 
1042 	return VALID_CHANNEL20(wlc, val) ||
1043 		(!wlc->bandlocked
1044 		 && VALID_CHANNEL20_IN_BAND(wlc, OTHERBANDUNIT(wlc), val));
1045 }
1046 
1047 /* Is the channel valid for the current locale and specified band? */
1048 static bool
wlc_valid_channel20_in_band(wlc_cm_info_t * wlc_cm,uint bandunit,uint val)1049 wlc_valid_channel20_in_band(wlc_cm_info_t *wlc_cm, uint bandunit, uint val)
1050 {
1051 	return ((val < MAXCHANNEL)
1052 		&& isset(wlc_cm->bandstate[bandunit].valid_channels.vec, val));
1053 }
1054 
1055 /* Is the channel valid for the current locale and current band? */
wlc_valid_channel20(wlc_cm_info_t * wlc_cm,uint val)1056 static bool wlc_valid_channel20(wlc_cm_info_t *wlc_cm, uint val)
1057 {
1058 	struct wlc_info *wlc = wlc_cm->wlc;
1059 
1060 	return ((val < MAXCHANNEL) &&
1061 		isset(wlc_cm->bandstate[wlc->band->bandunit].valid_channels.vec,
1062 		      val));
1063 }
1064 
1065 static void
wlc_channel_min_txpower_limits_with_local_constraint(wlc_cm_info_t * wlc_cm,struct txpwr_limits * txpwr,u8 local_constraint_qdbm)1066 wlc_channel_min_txpower_limits_with_local_constraint(wlc_cm_info_t *wlc_cm,
1067 						     struct txpwr_limits *txpwr,
1068 						     u8
1069 						     local_constraint_qdbm)
1070 {
1071 	int j;
1072 
1073 	/* CCK Rates */
1074 	for (j = 0; j < WL_TX_POWER_CCK_NUM; j++) {
1075 		txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
1076 	}
1077 
1078 	/* 20 MHz Legacy OFDM SISO */
1079 	for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++) {
1080 		txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
1081 	}
1082 
1083 	/* 20 MHz Legacy OFDM CDD */
1084 	for (j = 0; j < WLC_NUM_RATES_OFDM; j++) {
1085 		txpwr->ofdm_cdd[j] =
1086 		    min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
1087 	}
1088 
1089 	/* 40 MHz Legacy OFDM SISO */
1090 	for (j = 0; j < WLC_NUM_RATES_OFDM; j++) {
1091 		txpwr->ofdm_40_siso[j] =
1092 		    min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
1093 	}
1094 
1095 	/* 40 MHz Legacy OFDM CDD */
1096 	for (j = 0; j < WLC_NUM_RATES_OFDM; j++) {
1097 		txpwr->ofdm_40_cdd[j] =
1098 		    min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
1099 	}
1100 
1101 	/* 20MHz MCS 0-7 SISO */
1102 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1103 		txpwr->mcs_20_siso[j] =
1104 		    min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
1105 	}
1106 
1107 	/* 20MHz MCS 0-7 CDD */
1108 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1109 		txpwr->mcs_20_cdd[j] =
1110 		    min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
1111 	}
1112 
1113 	/* 20MHz MCS 0-7 STBC */
1114 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1115 		txpwr->mcs_20_stbc[j] =
1116 		    min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
1117 	}
1118 
1119 	/* 20MHz MCS 8-15 MIMO */
1120 	for (j = 0; j < WLC_NUM_RATES_MCS_2_STREAM; j++)
1121 		txpwr->mcs_20_mimo[j] =
1122 		    min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
1123 
1124 	/* 40MHz MCS 0-7 SISO */
1125 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1126 		txpwr->mcs_40_siso[j] =
1127 		    min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
1128 	}
1129 
1130 	/* 40MHz MCS 0-7 CDD */
1131 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1132 		txpwr->mcs_40_cdd[j] =
1133 		    min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
1134 	}
1135 
1136 	/* 40MHz MCS 0-7 STBC */
1137 	for (j = 0; j < WLC_NUM_RATES_MCS_1_STREAM; j++) {
1138 		txpwr->mcs_40_stbc[j] =
1139 		    min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
1140 	}
1141 
1142 	/* 40MHz MCS 8-15 MIMO */
1143 	for (j = 0; j < WLC_NUM_RATES_MCS_2_STREAM; j++)
1144 		txpwr->mcs_40_mimo[j] =
1145 		    min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
1146 
1147 	/* 40MHz MCS 32 */
1148 	txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
1149 
1150 }
1151 
1152 void
wlc_channel_set_chanspec(wlc_cm_info_t * wlc_cm,chanspec_t chanspec,u8 local_constraint_qdbm)1153 wlc_channel_set_chanspec(wlc_cm_info_t *wlc_cm, chanspec_t chanspec,
1154 			 u8 local_constraint_qdbm)
1155 {
1156 	struct wlc_info *wlc = wlc_cm->wlc;
1157 	struct txpwr_limits txpwr;
1158 
1159 	wlc_channel_reg_limits(wlc_cm, chanspec, &txpwr);
1160 
1161 	wlc_channel_min_txpower_limits_with_local_constraint(wlc_cm, &txpwr,
1162 							     local_constraint_qdbm);
1163 
1164 	wlc_bmac_set_chanspec(wlc->hw, chanspec,
1165 			      (wlc_quiet_chanspec(wlc_cm, chanspec) != 0),
1166 			      &txpwr);
1167 }
1168 
1169 #ifdef POWER_DBG
wlc_phy_txpower_limits_dump(txpwr_limits_t * txpwr)1170 static void wlc_phy_txpower_limits_dump(txpwr_limits_t *txpwr)
1171 {
1172 	int i;
1173 	char buf[80];
1174 	char fraction[4][4] = { "   ", ".25", ".5 ", ".75" };
1175 
1176 	sprintf(buf, "CCK                ");
1177 	for (i = 0; i < WLC_NUM_RATES_CCK; i++) {
1178 		sprintf(buf[strlen(buf)], " %2d%s",
1179 			txpwr->cck[i] / WLC_TXPWR_DB_FACTOR,
1180 			fraction[txpwr->cck[i] % WLC_TXPWR_DB_FACTOR]);
1181 	}
1182 	printk(KERN_DEBUG "%s\n", buf);
1183 
1184 	sprintf(buf, "20 MHz OFDM SISO   ");
1185 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1186 		sprintf(buf[strlen(buf)], " %2d%s",
1187 			txpwr->ofdm[i] / WLC_TXPWR_DB_FACTOR,
1188 			fraction[txpwr->ofdm[i] % WLC_TXPWR_DB_FACTOR]);
1189 	}
1190 	printk(KERN_DEBUG "%s\n", buf);
1191 
1192 	sprintf(buf, "20 MHz OFDM CDD    ");
1193 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1194 		sprintf(buf[strlen(buf)], " %2d%s",
1195 			txpwr->ofdm_cdd[i] / WLC_TXPWR_DB_FACTOR,
1196 			fraction[txpwr->ofdm_cdd[i] % WLC_TXPWR_DB_FACTOR]);
1197 	}
1198 	printk(KERN_DEBUG "%s\n", buf);
1199 
1200 	sprintf(buf, "40 MHz OFDM SISO   ");
1201 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1202 		sprintf(buf[strlen(buf)], " %2d%s",
1203 			txpwr->ofdm_40_siso[i] / WLC_TXPWR_DB_FACTOR,
1204 			fraction[txpwr->ofdm_40_siso[i] % WLC_TXPWR_DB_FACTOR]);
1205 	}
1206 	printk(KERN_DEBUG "%s\n", buf);
1207 
1208 	sprintf(buf, "40 MHz OFDM CDD    ");
1209 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1210 		sprintf(buf[strlen(buf)], " %2d%s",
1211 			txpwr->ofdm_40_cdd[i] / WLC_TXPWR_DB_FACTOR,
1212 			fraction[txpwr->ofdm_40_cdd[i] % WLC_TXPWR_DB_FACTOR]);
1213 	}
1214 	printk(KERN_DEBUG "%s\n", buf);
1215 
1216 	sprintf(buf, "20 MHz MCS0-7 SISO ");
1217 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1218 		sprintf(buf[strlen(buf)], " %2d%s",
1219 			txpwr->mcs_20_siso[i] / WLC_TXPWR_DB_FACTOR,
1220 			fraction[txpwr->mcs_20_siso[i] % WLC_TXPWR_DB_FACTOR]);
1221 	}
1222 	printk(KERN_DEBUG "%s\n", buf);
1223 
1224 	sprintf(buf, "20 MHz MCS0-7 CDD  ");
1225 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1226 		sprintf(buf[strlen(buf)], " %2d%s",
1227 			txpwr->mcs_20_cdd[i] / WLC_TXPWR_DB_FACTOR,
1228 			fraction[txpwr->mcs_20_cdd[i] % WLC_TXPWR_DB_FACTOR]);
1229 	}
1230 	printk(KERN_DEBUG "%s\n", buf);
1231 
1232 	sprintf(buf, "20 MHz MCS0-7 STBC ");
1233 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1234 		sprintf(buf[strlen(buf)], " %2d%s",
1235 			txpwr->mcs_20_stbc[i] / WLC_TXPWR_DB_FACTOR,
1236 			fraction[txpwr->mcs_20_stbc[i] % WLC_TXPWR_DB_FACTOR]);
1237 	}
1238 	printk(KERN_DEBUG "%s\n", buf);
1239 
1240 	sprintf(buf, "20 MHz MCS8-15 SDM ");
1241 	for (i = 0; i < WLC_NUM_RATES_MCS_2_STREAM; i++) {
1242 		sprintf(buf[strlen(buf)], " %2d%s",
1243 			txpwr->mcs_20_mimo[i] / WLC_TXPWR_DB_FACTOR,
1244 			fraction[txpwr->mcs_20_mimo[i] % WLC_TXPWR_DB_FACTOR]);
1245 	}
1246 	printk(KERN_DEBUG "%s\n", buf);
1247 
1248 	sprintf(buf, "40 MHz MCS0-7 SISO ");
1249 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1250 		sprintf(buf[strlen(buf)], " %2d%s",
1251 			txpwr->mcs_40_siso[i] / WLC_TXPWR_DB_FACTOR,
1252 			fraction[txpwr->mcs_40_siso[i] % WLC_TXPWR_DB_FACTOR]);
1253 	}
1254 	printk(KERN_DEBUG "%s\n", buf);
1255 
1256 	sprintf(buf, "40 MHz MCS0-7 CDD  ");
1257 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1258 		sprintf(buf[strlen(buf)], " %2d%s",
1259 			txpwr->mcs_40_cdd[i] / WLC_TXPWR_DB_FACTOR,
1260 			fraction[txpwr->mcs_40_cdd[i] % WLC_TXPWR_DB_FACTOR]);
1261 	}
1262 	printk(KERN_DEBUG "%s\n", buf);
1263 
1264 	sprintf(buf, "40 MHz MCS0-7 STBC ");
1265 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1266 		sprintf(buf[strlen(buf)], " %2d%s",
1267 			txpwr->mcs_40_stbc[i] / WLC_TXPWR_DB_FACTOR,
1268 			fraction[txpwr->mcs_40_stbc[i] % WLC_TXPWR_DB_FACTOR]);
1269 	}
1270 	printk(KERN_DEBUG "%s\n", buf);
1271 
1272 	sprintf(buf, "40 MHz MCS8-15 SDM ");
1273 	for (i = 0; i < WLC_NUM_RATES_MCS_2_STREAM; i++) {
1274 		sprintf(buf[strlen(buf)], " %2d%s",
1275 			txpwr->mcs_40_mimo[i] / WLC_TXPWR_DB_FACTOR,
1276 			fraction[txpwr->mcs_40_mimo[i] % WLC_TXPWR_DB_FACTOR]);
1277 	}
1278 	printk(KERN_DEBUG "%s\n", buf);
1279 
1280 	printk(KERN_DEBUG "MCS32               %2d%s\n",
1281 	       txpwr->mcs32 / WLC_TXPWR_DB_FACTOR,
1282 	       fraction[txpwr->mcs32 % WLC_TXPWR_DB_FACTOR]);
1283 }
1284 #endif				/* POWER_DBG */
1285 
1286 void
wlc_channel_reg_limits(wlc_cm_info_t * wlc_cm,chanspec_t chanspec,txpwr_limits_t * txpwr)1287 wlc_channel_reg_limits(wlc_cm_info_t *wlc_cm, chanspec_t chanspec,
1288 		       txpwr_limits_t *txpwr)
1289 {
1290 	struct wlc_info *wlc = wlc_cm->wlc;
1291 	uint i;
1292 	uint chan;
1293 	int maxpwr;
1294 	int delta;
1295 	const country_info_t *country;
1296 	struct wlcband *band;
1297 	const locale_info_t *li;
1298 	int conducted_max;
1299 	int conducted_ofdm_max;
1300 	const locale_mimo_info_t *li_mimo;
1301 	int maxpwr20, maxpwr40;
1302 	int maxpwr_idx;
1303 	uint j;
1304 
1305 	memset(txpwr, 0, sizeof(txpwr_limits_t));
1306 
1307 	if (!wlc_valid_chanspec_db(wlc_cm, chanspec)) {
1308 		country = wlc_country_lookup(wlc, wlc->autocountry_default);
1309 		if (country == NULL)
1310 			return;
1311 	} else {
1312 		country = wlc_cm->country;
1313 	}
1314 
1315 	chan = CHSPEC_CHANNEL(chanspec);
1316 	band = wlc->bandstate[CHSPEC_WLCBANDUNIT(chanspec)];
1317 	li = BAND_5G(band->bandtype) ?
1318 	    wlc_get_locale_5g(country->locale_5G) :
1319 	    wlc_get_locale_2g(country->locale_2G);
1320 
1321 	li_mimo = BAND_5G(band->bandtype) ?
1322 	    wlc_get_mimo_5g(country->locale_mimo_5G) :
1323 	    wlc_get_mimo_2g(country->locale_mimo_2G);
1324 
1325 	if (li->flags & WLC_EIRP) {
1326 		delta = band->antgain;
1327 	} else {
1328 		delta = 0;
1329 		if (band->antgain > QDB(6))
1330 			delta = band->antgain - QDB(6);	/* Excess over 6 dB */
1331 	}
1332 
1333 	if (li == &locale_i) {
1334 		conducted_max = QDB(22);
1335 		conducted_ofdm_max = QDB(22);
1336 	}
1337 
1338 	/* CCK txpwr limits for 2.4G band */
1339 	if (BAND_2G(band->bandtype)) {
1340 		maxpwr = li->maxpwr[CHANNEL_POWER_IDX_2G_CCK(chan)];
1341 
1342 		maxpwr = maxpwr - delta;
1343 		maxpwr = max(maxpwr, 0);
1344 		maxpwr = min(maxpwr, conducted_max);
1345 
1346 		for (i = 0; i < WLC_NUM_RATES_CCK; i++)
1347 			txpwr->cck[i] = (u8) maxpwr;
1348 	}
1349 
1350 	/* OFDM txpwr limits for 2.4G or 5G bands */
1351 	if (BAND_2G(band->bandtype)) {
1352 		maxpwr = li->maxpwr[CHANNEL_POWER_IDX_2G_OFDM(chan)];
1353 
1354 	} else {
1355 		maxpwr = li->maxpwr[CHANNEL_POWER_IDX_5G(chan)];
1356 	}
1357 
1358 	maxpwr = maxpwr - delta;
1359 	maxpwr = max(maxpwr, 0);
1360 	maxpwr = min(maxpwr, conducted_ofdm_max);
1361 
1362 	/* Keep OFDM lmit below CCK limit */
1363 	if (BAND_2G(band->bandtype))
1364 		maxpwr = min_t(int, maxpwr, txpwr->cck[0]);
1365 
1366 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1367 		txpwr->ofdm[i] = (u8) maxpwr;
1368 	}
1369 
1370 	for (i = 0; i < WLC_NUM_RATES_OFDM; i++) {
1371 		/* OFDM 40 MHz SISO has the same power as the corresponding MCS0-7 rate unless
1372 		 * overriden by the locale specific code. We set this value to 0 as a
1373 		 * flag (presumably 0 dBm isn't a possibility) and then copy the MCS0-7 value
1374 		 * to the 40 MHz value if it wasn't explicitly set.
1375 		 */
1376 		txpwr->ofdm_40_siso[i] = 0;
1377 
1378 		txpwr->ofdm_cdd[i] = (u8) maxpwr;
1379 
1380 		txpwr->ofdm_40_cdd[i] = 0;
1381 	}
1382 
1383 	/* MIMO/HT specific limits */
1384 	if (li_mimo->flags & WLC_EIRP) {
1385 		delta = band->antgain;
1386 	} else {
1387 		delta = 0;
1388 		if (band->antgain > QDB(6))
1389 			delta = band->antgain - QDB(6);	/* Excess over 6 dB */
1390 	}
1391 
1392 	if (BAND_2G(band->bandtype))
1393 		maxpwr_idx = (chan - 1);
1394 	else
1395 		maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
1396 
1397 	maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
1398 	maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
1399 
1400 	maxpwr20 = maxpwr20 - delta;
1401 	maxpwr20 = max(maxpwr20, 0);
1402 	maxpwr40 = maxpwr40 - delta;
1403 	maxpwr40 = max(maxpwr40, 0);
1404 
1405 	/* Fill in the MCS 0-7 (SISO) rates */
1406 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1407 
1408 		/* 20 MHz has the same power as the corresponding OFDM rate unless
1409 		 * overriden by the locale specific code.
1410 		 */
1411 		txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
1412 		txpwr->mcs_40_siso[i] = 0;
1413 	}
1414 
1415 	/* Fill in the MCS 0-7 CDD rates */
1416 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1417 		txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
1418 		txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
1419 	}
1420 
1421 	/* These locales have SISO expressed in the table and override CDD later */
1422 	if (li_mimo == &locale_bn) {
1423 		if (li_mimo == &locale_bn) {
1424 			maxpwr20 = QDB(16);
1425 			maxpwr40 = 0;
1426 
1427 			if (chan >= 3 && chan <= 11) {
1428 				maxpwr40 = QDB(16);
1429 			}
1430 		}
1431 
1432 		for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1433 			txpwr->mcs_20_siso[i] = (u8) maxpwr20;
1434 			txpwr->mcs_40_siso[i] = (u8) maxpwr40;
1435 		}
1436 	}
1437 
1438 	/* Fill in the MCS 0-7 STBC rates */
1439 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1440 		txpwr->mcs_20_stbc[i] = 0;
1441 		txpwr->mcs_40_stbc[i] = 0;
1442 	}
1443 
1444 	/* Fill in the MCS 8-15 SDM rates */
1445 	for (i = 0; i < WLC_NUM_RATES_MCS_2_STREAM; i++) {
1446 		txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
1447 		txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
1448 	}
1449 
1450 	/* Fill in MCS32 */
1451 	txpwr->mcs32 = (u8) maxpwr40;
1452 
1453 	for (i = 0, j = 0; i < WLC_NUM_RATES_OFDM; i++, j++) {
1454 		if (txpwr->ofdm_40_cdd[i] == 0)
1455 			txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
1456 		if (i == 0) {
1457 			i = i + 1;
1458 			if (txpwr->ofdm_40_cdd[i] == 0)
1459 				txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
1460 		}
1461 	}
1462 
1463 	/* Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO value if it wasn't
1464 	 * provided explicitly.
1465 	 */
1466 
1467 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1468 		if (txpwr->mcs_40_siso[i] == 0)
1469 			txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
1470 	}
1471 
1472 	for (i = 0, j = 0; i < WLC_NUM_RATES_OFDM; i++, j++) {
1473 		if (txpwr->ofdm_40_siso[i] == 0)
1474 			txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
1475 		if (i == 0) {
1476 			i = i + 1;
1477 			if (txpwr->ofdm_40_siso[i] == 0)
1478 				txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
1479 		}
1480 	}
1481 
1482 	/* Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding STBC values if they weren't
1483 	 * provided explicitly.
1484 	 */
1485 	for (i = 0; i < WLC_NUM_RATES_MCS_1_STREAM; i++) {
1486 		if (txpwr->mcs_20_stbc[i] == 0)
1487 			txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
1488 
1489 		if (txpwr->mcs_40_stbc[i] == 0)
1490 			txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
1491 	}
1492 
1493 #ifdef POWER_DBG
1494 	wlc_phy_txpower_limits_dump(txpwr);
1495 #endif
1496 	return;
1497 }
1498 
1499 /* Returns true if currently set country is Japan or variant */
wlc_japan(struct wlc_info * wlc)1500 static bool wlc_japan(struct wlc_info *wlc)
1501 {
1502 	return wlc_japan_ccode(wlc->cmi->country_abbrev);
1503 }
1504 
1505 /* JP, J1 - J10 are Japan ccodes */
wlc_japan_ccode(const char * ccode)1506 static bool wlc_japan_ccode(const char *ccode)
1507 {
1508 	return (ccode[0] == 'J' &&
1509 		(ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
1510 }
1511 
1512 /*
1513  * Validate the chanspec for this locale, for 40MHZ we need to also check that the sidebands
1514  * are valid 20MZH channels in this locale and they are also a legal HT combination
1515  */
1516 static bool
wlc_valid_chanspec_ext(wlc_cm_info_t * wlc_cm,chanspec_t chspec,bool dualband)1517 wlc_valid_chanspec_ext(wlc_cm_info_t *wlc_cm, chanspec_t chspec, bool dualband)
1518 {
1519 	struct wlc_info *wlc = wlc_cm->wlc;
1520 	u8 channel = CHSPEC_CHANNEL(chspec);
1521 
1522 	/* check the chanspec */
1523 	if (wf_chspec_malformed(chspec)) {
1524 		WL_ERROR("wl%d: malformed chanspec 0x%x\n",
1525 			 wlc->pub->unit, chspec);
1526 		ASSERT(0);
1527 		return false;
1528 	}
1529 
1530 	if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
1531 	    CHSPEC_WLCBANDUNIT(chspec))
1532 		return false;
1533 
1534 	/* Check a 20Mhz channel */
1535 	if (CHSPEC_IS20(chspec)) {
1536 		if (dualband)
1537 			return VALID_CHANNEL20_DB(wlc_cm->wlc, channel);
1538 		else
1539 			return VALID_CHANNEL20(wlc_cm->wlc, channel);
1540 	}
1541 #ifdef SUPPORT_40MHZ
1542 	/* We know we are now checking a 40MHZ channel, so we should only be here
1543 	 * for NPHYS
1544 	 */
1545 	if (WLCISNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band)) {
1546 		u8 upper_sideband = 0, idx;
1547 		u8 num_ch20_entries =
1548 		    sizeof(chan20_info) / sizeof(struct chan20_info);
1549 
1550 		if (!VALID_40CHANSPEC_IN_BAND(wlc, CHSPEC_WLCBANDUNIT(chspec)))
1551 			return false;
1552 
1553 		if (dualband) {
1554 			if (!VALID_CHANNEL20_DB(wlc, LOWER_20_SB(channel)) ||
1555 			    !VALID_CHANNEL20_DB(wlc, UPPER_20_SB(channel)))
1556 				return false;
1557 		} else {
1558 			if (!VALID_CHANNEL20(wlc, LOWER_20_SB(channel)) ||
1559 			    !VALID_CHANNEL20(wlc, UPPER_20_SB(channel)))
1560 				return false;
1561 		}
1562 
1563 		/* find the lower sideband info in the sideband array */
1564 		for (idx = 0; idx < num_ch20_entries; idx++) {
1565 			if (chan20_info[idx].sb == LOWER_20_SB(channel))
1566 				upper_sideband = chan20_info[idx].adj_sbs;
1567 		}
1568 		/* check that the lower sideband allows an upper sideband */
1569 		if ((upper_sideband & (CH_UPPER_SB | CH_EWA_VALID)) ==
1570 		    (CH_UPPER_SB | CH_EWA_VALID))
1571 			return true;
1572 		return false;
1573 	}
1574 #endif				/* 40 MHZ */
1575 
1576 	return false;
1577 }
1578 
wlc_valid_chanspec_db(wlc_cm_info_t * wlc_cm,chanspec_t chspec)1579 bool wlc_valid_chanspec_db(wlc_cm_info_t *wlc_cm, chanspec_t chspec)
1580 {
1581 	return wlc_valid_chanspec_ext(wlc_cm, chspec, true);
1582 }
1583