xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/include/bcmwifi_channels.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Misc utility routines for WL and Apps
3  * This header file housing the define and function prototype use by
4  * both the wl driver, tools & Apps.
5  *
6  * Copyright (C) 2020, Broadcom.
7  *
8  *      Unless you and Broadcom execute a separate written software license
9  * agreement governing use of this software, this software is licensed to you
10  * under the terms of the GNU General Public License version 2 (the "GPL"),
11  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12  * following added to such license:
13  *
14  *      As a special exception, the copyright holders of this software give you
15  * permission to link this software with independent modules, and to copy and
16  * distribute the resulting executable under terms of your choice, provided that
17  * you also meet, for each linked independent module, the terms and conditions of
18  * the license of that module.  An independent module is a module which is not
19  * derived from this software.  The special exception does not apply to any
20  * modifications of the software.
21  *
22  *
23  * <<Broadcom-WL-IPTag/Dual:>>
24  */
25 
26 #ifndef	_bcmwifi_channels_h_
27 #define	_bcmwifi_channels_h_
28 
29 /* A chanspec holds the channel number, band, bandwidth and primary 20MHz sub-band */
30 typedef uint16 chanspec_t;
31 typedef uint16 chanspec_band_t;
32 typedef uint16 chanspec_bw_t;
33 typedef uint16 chanspec_subband_t;
34 
35 /* channel defines */
36 #define CH_80MHZ_APART                   16u
37 #define CH_40MHZ_APART                    8u
38 #define CH_20MHZ_APART                    4u
39 #define CH_10MHZ_APART                    2u
40 #define CH_5MHZ_APART                     1u    /* 2G band channels are 5 Mhz apart */
41 #define CH_160MHZ_APART                  (32u * CH_5MHZ_APART)	/* 32 5Mhz-spaces */
42 
43 #define CH_MIN_2G_CHANNEL                 1u    /* Min channel in 2G band */
44 #define CH_MAX_2G_CHANNEL                14u    /* Max channel in 2G band */
45 #define CH_MIN_2G_40M_CHANNEL             3u    /* Min 40MHz center channel in 2G band */
46 #define CH_MAX_2G_40M_CHANNEL            11u    /* Max 40MHz center channel in 2G band */
47 
48 #define CH_MIN_6G_CHANNEL                 1u    /* Min 20MHz channel in 6G band */
49 #define CH_MAX_6G_CHANNEL               253u    /* Max 20MHz channel in 6G band */
50 #define CH_MIN_6G_40M_CHANNEL             3u    /* Min 40MHz center channel in 6G band */
51 #define CH_MAX_6G_40M_CHANNEL           227u    /* Max 40MHz center channel in 6G band */
52 #define CH_MIN_6G_80M_CHANNEL             7u    /* Min 80MHz center channel in 6G band */
53 #define CH_MAX_6G_80M_CHANNEL           215u    /* Max 80MHz center channel in 6G band */
54 #define CH_MIN_6G_160M_CHANNEL           15u    /* Min 160MHz center channel in 6G band */
55 #define CH_MAX_6G_160M_CHANNEL          207u    /* Max 160MHz center channel in 6G band */
56 #define CH_MIN_6G_240M_CHANNEL		 23u	/* Min 240MHz center channel in 6G band */
57 #define CH_MAX_6G_240M_CHANNEL		167u	/* Max 240MHz center channel in 6G band */
58 #define CH_MIN_6G_320M_CHANNEL           31u    /* Min 320MHz center channel in 6G band */
59 #define CH_MAX_6G_320M_CHANNEL          199u    /* Max 320MHz center channel in 6G band */
60 
61 /* maximum # channels the s/w supports */
62 #define MAXCHANNEL                      254u    /* max # supported channels.
63 						 * DO NOT MAKE > 255: channels are uint8's all over
64 						 */
65 #define MAXCHANNEL_NUM	(MAXCHANNEL - 1)	/* max channel number */
66 
67 #define INVCHANNEL                      255u    /* error value for a bad channel */
68 
69 /* length of channel vector bitmap is the MAXCHANNEL we want to handle rounded up to a byte */
70 /* The actual CHANVEC_LEN fix is leading to high static memory impact
71 * in all projects wherein the previous CHANVEC_LEN definition is used.
72 *
73 * Retaining the previous definition under MAXCHNL_ROM_COMPAT flag.
74 * All those chip porgrams where memory impact is observed need to define the same.
75 */
76 #ifdef MAXCHNL_ROM_COMPAT
77 #define CHANVEC_LEN (MAXCHANNEL + (8 - 1) / 8)
78 #else
79 #define CHANVEC_LEN ((MAXCHANNEL + (8 - 1)) / 8)
80 #endif
81 
82 /* channel bitvec */
83 typedef struct {
84 	uint8   vec[CHANVEC_LEN];   /* bitvec of channels */
85 } chanvec_t;
86 
87 /* make sure channel num is within valid range */
88 #define CH_NUM_VALID_RANGE(ch_num) ((ch_num) > 0 && (ch_num) <= MAXCHANNEL_NUM)
89 
90 #define CHSPEC_CTLOVLP(sp1, sp2, sep)	\
91 	((uint)ABS(wf_chspec_ctlchan(sp1) - wf_chspec_ctlchan(sp2)) < (uint)(sep))
92 
93 /* All builds use the new 11ac ratespec/chanspec */
94 #undef  D11AC_IOTYPES
95 #define D11AC_IOTYPES
96 
97 /* For contiguous channel bandwidth other than 240MHz/320Mhz */
98 #define WL_CHANSPEC_CHAN_MASK		0x00ffu
99 #define WL_CHANSPEC_CHAN_SHIFT		0u
100 
101 /* For contiguous channel bandwidth >= 240MHz */
102 #define WL_CHANSPEC_GE240_CHAN_MASK	0x0003u
103 #define WL_CHANSPEC_GE240_CHAN_SHIFT	0u
104 
105 /* For discontiguous channel bandwidth */
106 #define WL_CHANSPEC_CHAN0_MASK		0x000fu
107 #define WL_CHANSPEC_CHAN0_SHIFT		0u
108 #define WL_CHANSPEC_CHAN1_MASK		0x00f0u
109 #define WL_CHANSPEC_CHAN1_SHIFT		4u
110 
111 /* Non-320/Non-240 Mhz channel sideband indication */
112 #define WL_CHANSPEC_CTL_SB_MASK		0x0700u
113 #define WL_CHANSPEC_CTL_SB_SHIFT	8u
114 #define WL_CHANSPEC_CTL_SB_LLL		0x0000u
115 #define WL_CHANSPEC_CTL_SB_LLU		0x0100u
116 #define WL_CHANSPEC_CTL_SB_LUL		0x0200u
117 #define WL_CHANSPEC_CTL_SB_LUU		0x0300u
118 #define WL_CHANSPEC_CTL_SB_ULL		0x0400u
119 #define WL_CHANSPEC_CTL_SB_ULU		0x0500u
120 #define WL_CHANSPEC_CTL_SB_UUL		0x0600u
121 #define WL_CHANSPEC_CTL_SB_UUU		0x0700u
122 #define WL_CHANSPEC_CTL_SB_LL		WL_CHANSPEC_CTL_SB_LLL
123 #define WL_CHANSPEC_CTL_SB_LU		WL_CHANSPEC_CTL_SB_LLU
124 #define WL_CHANSPEC_CTL_SB_UL		WL_CHANSPEC_CTL_SB_LUL
125 #define WL_CHANSPEC_CTL_SB_UU		WL_CHANSPEC_CTL_SB_LUU
126 #define WL_CHANSPEC_CTL_SB_L		WL_CHANSPEC_CTL_SB_LLL
127 #define WL_CHANSPEC_CTL_SB_U		WL_CHANSPEC_CTL_SB_LLU
128 #define WL_CHANSPEC_CTL_SB_LOWER	WL_CHANSPEC_CTL_SB_LLL
129 #define WL_CHANSPEC_CTL_SB_UPPER	WL_CHANSPEC_CTL_SB_LLU
130 #define WL_CHANSPEC_CTL_SB_NONE		WL_CHANSPEC_CTL_SB_LLL
131 
132 /* channel sideband indication for frequency >= 240MHz */
133 #define WL_CHANSPEC_GE240_SB_MASK	0x0780u
134 #define WL_CHANSPEC_GE240_SB_SHIFT	7u
135 
136 /* Bandwidth field */
137 #define WL_CHANSPEC_BW_MASK		0x3800u
138 #define WL_CHANSPEC_BW_SHIFT		11u
139 #define WL_CHANSPEC_BW_320		0x0000u
140 #define WL_CHANSPEC_BW_160160		0x0800u
141 #define WL_CHANSPEC_BW_20		0x1000u
142 #define WL_CHANSPEC_BW_40		0x1800u
143 #define WL_CHANSPEC_BW_80		0x2000u
144 #define WL_CHANSPEC_BW_160		0x2800u
145 #define WL_CHANSPEC_BW_8080		0x3000u
146 #define WL_CHANSPEC_BW_240		0x3800u
147 
148 /* Band field */
149 #define WL_CHANSPEC_BAND_MASK		0xc000u
150 #define WL_CHANSPEC_BAND_SHIFT		14u
151 #define WL_CHANSPEC_BAND_2G		0x0000u
152 #define WL_CHANSPEC_BAND_6G		0x4000u
153 #define WL_CHANSPEC_BAND_4G		0x8000u
154 #define WL_CHANSPEC_BAND_5G		0xc000u
155 
156 #define INVCHANSPEC			255u
157 #define MAX_CHANSPEC			0xFFFFu
158 
159 #define WL_CHSPEC_BW(chspec)	((chspec & WL_CHANSPEC_BW_MASK) >> WL_CHANSPEC_BW_SHIFT)
160 #define MAX_BW_NUM		(uint8)(((WL_CHANSPEC_BW_MASK) >> WL_CHANSPEC_BW_SHIFT))
161 
162 #define WL_CHANNEL_BAND(ch)	(((uint)(ch) <= CH_MAX_2G_CHANNEL) ? \
163 				 WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G)
164 
165 /* channel defines */
166 #define LOWER_20_SB(channel)		(((channel) > CH_10MHZ_APART) ? \
167 					((channel) - CH_10MHZ_APART) : 0)
168 #define UPPER_20_SB(channel)		(((channel) < (MAXCHANNEL - CH_10MHZ_APART)) ? \
169 					((channel) + CH_10MHZ_APART) : 0)
170 
171 /* pass a 80MHz channel number (uint8) to get respective LL, UU, LU, UL */
172 #define LL_20_SB(channel) (((channel) > 3 * CH_10MHZ_APART) ? ((channel) - 3 * CH_10MHZ_APART) : 0)
173 #define UU_20_SB(channel) (((channel) < (MAXCHANNEL - 3 * CH_10MHZ_APART)) ? \
174 			   ((channel) + 3 * CH_10MHZ_APART) : 0)
175 #define LU_20_SB(channel) LOWER_20_SB(channel)
176 #define UL_20_SB(channel) UPPER_20_SB(channel)
177 
178 #define LOWER_40_SB(channel)		((channel) - CH_20MHZ_APART)
179 #define UPPER_40_SB(channel)		((channel) + CH_20MHZ_APART)
180 
181 #ifndef CHSPEC_WLCBANDUNIT
182 #define CHSPEC_WLCBANDUNIT(chspec) \
183 	((CHSPEC_IS5G(chspec) || CHSPEC_IS6G(chspec)) ? BAND_5G_INDEX : BAND_2G_INDEX)
184 #endif
185 #define CH20MHZ_CHSPEC(channel)		(chanspec_t)((chanspec_t)(channel) | WL_CHANSPEC_BW_20 | \
186 						     WL_CHANNEL_BAND(channel))
187 #define NEXT_20MHZ_CHAN(channel)	(((channel) < (MAXCHANNEL - CH_20MHZ_APART)) ? \
188 					((channel) + CH_20MHZ_APART) : 0)
189 #define CH40MHZ_CHSPEC(channel, ctlsb)	(chanspec_t) \
190 					((channel) | (ctlsb) | WL_CHANSPEC_BW_40 | \
191 					 WL_CHANNEL_BAND(channel))
192 #define CH80MHZ_CHSPEC(channel, ctlsb)	(chanspec_t) \
193 					((channel) | (ctlsb) | \
194 					 WL_CHANSPEC_BW_80 | WL_CHANSPEC_BAND_5G)
195 #define CH160MHZ_CHSPEC(channel, ctlsb)	(chanspec_t) \
196 					((channel) | (ctlsb) | \
197 					 WL_CHANSPEC_BW_160 | WL_CHANSPEC_BAND_5G)
198 
199 /* simple MACROs to get different fields of chanspec */
200 #define CHSPEC_CHANNEL(chspec)	((uint8)((chspec) & WL_CHANSPEC_CHAN_MASK))
201 #define CHSPEC_CHAN0(chspec)	(((chspec) & WL_CHANSPEC_CHAN0_MASK) >> WL_CHANSPEC_CHAN0_SHIFT)
202 #define CHSPEC_CHAN1(chspec)	(((chspec) & WL_CHANSPEC_CHAN1_MASK) >> WL_CHANSPEC_CHAN1_SHIFT)
203 #define CHSPEC_BAND(chspec)	((chspec) & WL_CHANSPEC_BAND_MASK)
204 #define CHSPEC_CTL_SB(chspec)	((chspec) & WL_CHANSPEC_CTL_SB_MASK)
205 #define CHSPEC_BW(chspec)	((chspec) & WL_CHANSPEC_BW_MASK)
206 #define CHSPEC_GE240_CHAN(chspec)	(((chspec) & WL_CHANSPEC_GE240_CHAN_MASK) >> \
207 					WL_CHANSPEC_GE240_CHAN_SHIFT)
208 #define CHSPEC_GE240_SB(chspec)	((chspec) & WL_CHANSPEC_GE240_SB_MASK)
209 
210 #define CHSPEC_IS20(chspec)	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_20)
211 #define CHSPEC_IS20_5G(chspec)	((((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_20) && \
212 				 CHSPEC_IS5G(chspec))
213 #ifndef CHSPEC_IS40
214 #define CHSPEC_IS40(chspec)	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_40)
215 #endif
216 #ifndef CHSPEC_IS80
217 #define CHSPEC_IS80(chspec)	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_80)
218 #endif
219 #ifndef CHSPEC_IS160
220 #define CHSPEC_IS160(chspec)	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_160)
221 #endif
222 #define CHSPEC_IS8080(chspec)	(FALSE)
223 #ifndef CHSPEC_IS320
224 #ifdef WL11BE
225 #define CHSPEC_IS320(chspec)	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_320)
226 #else
227 #define CHSPEC_IS320(chspec)	(FALSE)
228 #endif
229 #endif /* CHSPEC_IS320 */
230 #ifndef CHSPEC_IS240
231 #ifdef WL11BE
232 #define CHSPEC_IS240(chspec)    (((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_240)
233 #else
234 #define CHSPEC_IS240(chspec)	(FALSE)
235 #endif
236 #endif /* CHSPEC_IS240 */
237 
238 /* pass a center channel and get channel offset from it by 10MHz */
239 #define CH_OFF_10MHZ_MULTIPLES(channel, offset)				\
240 ((uint8) (((offset) < 0) ?						\
241 	  (((channel) > (WL_CHANSPEC_CHAN_MASK & ((uint16)((-(offset)) * CH_10MHZ_APART)))) ? \
242 	   ((channel) + (offset) * CH_10MHZ_APART) : 0) :		\
243 	  ((((uint16)(channel) + (uint16)(offset) * CH_10MHZ_APART) < (uint16)MAXCHANNEL) ? \
244 	   ((channel) + (offset) * CH_10MHZ_APART) : 0)))
245 
246 uint wf_chspec_first_20_sb(chanspec_t chspec);
247 
248 #if defined(WL_BW160MHZ)
249 /* pass a 160MHz center channel to get 20MHz subband channel numbers */
250 #define LLL_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel, -7)
251 #define LLU_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel, -5)
252 #define LUL_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel, -3)
253 #define LUU_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel, -1)
254 #define ULL_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel,  1)
255 #define ULU_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel,  3)
256 #define UUL_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel,  5)
257 #define UUU_20_SB_160(channel)  CH_OFF_10MHZ_MULTIPLES(channel,  7)
258 
259 /* get lowest 20MHz sideband of a given chspec
260  * (works with 20, 40, 80, 160)
261  */
262 #define CH_FIRST_20_SB(chspec)  ((uint8) (\
263 		CHSPEC_IS160(chspec) ? LLL_20_SB_160(CHSPEC_CHANNEL(chspec)) : (\
264 			CHSPEC_IS80(chspec) ? LL_20_SB(CHSPEC_CHANNEL(chspec)) : (\
265 				CHSPEC_IS40(chspec) ? LOWER_20_SB(CHSPEC_CHANNEL(chspec)) : \
266 					CHSPEC_CHANNEL(chspec)))))
267 
268 /* get upper most 20MHz sideband of a given chspec
269  * (works with 20, 40, 80, 160)
270  */
271 #define CH_LAST_20_SB(chspec)  ((uint8) (\
272 		CHSPEC_IS160(chspec) ? UUU_20_SB_160(CHSPEC_CHANNEL(chspec)) : (\
273 			CHSPEC_IS80(chspec) ? UU_20_SB(CHSPEC_CHANNEL(chspec)) : (\
274 				CHSPEC_IS40(chspec) ? UPPER_20_SB(CHSPEC_CHANNEL(chspec)) : \
275 					CHSPEC_CHANNEL(chspec)))))
276 
277 /* call this with chspec and a valid 20MHz sideband of this channel to get the next 20MHz sideband
278  * (works with 20, 40, 80, 160)
279  * resolves to 0 if called with upper most channel
280  */
281 #define CH_NEXT_20_SB(chspec, channel)  ((uint8) (\
282 			((uint8) ((channel) + CH_20MHZ_APART) > CH_LAST_20_SB(chspec) ? 0 : \
283 				((channel) + CH_20MHZ_APART))))
284 
285 #else /* WL_BW160MHZ */
286 
287 #define LLL_20_SB_160(channel)  0
288 #define LLU_20_SB_160(channel)  0
289 #define LUL_20_SB_160(channel)  0
290 #define LUU_20_SB_160(channel)  0
291 #define ULL_20_SB_160(channel)  0
292 #define ULU_20_SB_160(channel)  0
293 #define UUL_20_SB_160(channel)  0
294 #define UUU_20_SB_160(channel)  0
295 
296 /* get lowest 20MHz sideband of a given chspec
297  * (works with 20, 40, 80)
298  */
299 #define CH_FIRST_20_SB(chspec)  ((uint8) (\
300 			CHSPEC_IS80(chspec) ? LL_20_SB(CHSPEC_CHANNEL(chspec)) : (\
301 				CHSPEC_IS40(chspec) ? LOWER_20_SB(CHSPEC_CHANNEL(chspec)) : \
302 					CHSPEC_CHANNEL(chspec))))
303 /* get upper most 20MHz sideband of a given chspec
304  * (works with 20, 40, 80, 160)
305  */
306 #define CH_LAST_20_SB(chspec)  ((uint8) (\
307 			CHSPEC_IS80(chspec) ? UU_20_SB(CHSPEC_CHANNEL(chspec)) : (\
308 				CHSPEC_IS40(chspec) ? UPPER_20_SB(CHSPEC_CHANNEL(chspec)) : \
309 					CHSPEC_CHANNEL(chspec))))
310 
311 /* call this with chspec and a valid 20MHz sideband of this channel to get the next 20MHz sideband
312  * (works with 20, 40, 80, 160)
313  * resolves to 0 if called with upper most channel
314  */
315 #define CH_NEXT_20_SB(chspec, channel)  ((uint8) (\
316 			((uint8) ((channel) + CH_20MHZ_APART) > CH_LAST_20_SB(chspec) ? 0 : \
317 				((channel) + CH_20MHZ_APART))))
318 
319 #endif /* WL_BW160MHZ */
320 
321 /* Iterator for 20MHz side bands of a chanspec: (chanspec_t chspec, uint8 channel)
322  * 'chspec' chanspec_t of interest (used in loop, better to pass a resolved value than a macro)
323  * 'channel' must be a variable (not an expression).
324  */
325 #define FOREACH_20_SB(chspec, channel) \
326 	for (channel = (uint8)wf_chspec_first_20_sb(chspec); channel;	\
327 			channel = CH_NEXT_20_SB((chspec), channel))
328 
329 /* Uses iterator to populate array with all side bands involved (sorted lower to upper).
330  *     'chspec' chanspec_t of interest
331  *     'psb' pointer to uint8 array of enough size to hold all side bands for the given chspec
332  */
333 #define GET_ALL_SB(chspec, psb) do { \
334 		uint8 channel, idx = 0; \
335 		chanspec_t chspec_local = chspec; \
336 		FOREACH_20_SB(chspec_local, channel) \
337 			(psb)[idx++] = channel; \
338 } while (0)
339 
340 /* given a chanspec of any bw, tests if primary20 SB is in lower 20, 40, 80 respectively */
341 #define IS_CTL_IN_L20(chspec) !((chspec) & WL_CHANSPEC_CTL_SB_U) /* CTL SB is in low 20 of any 40 */
342 #define IS_CTL_IN_L40(chspec) !((chspec) & WL_CHANSPEC_CTL_SB_UL)	/* in low 40 of any 80 */
343 #define IS_CTL_IN_L80(chspec) !((chspec) & WL_CHANSPEC_CTL_SB_ULL)	/* in low 80 of 160 */
344 
345 #define BW_LE40(bw)		((bw) == WL_CHANSPEC_BW_20 || ((bw) == WL_CHANSPEC_BW_40))
346 #define BW_LE80(bw)		(BW_LE40(bw) || ((bw) == WL_CHANSPEC_BW_80))
347 #define BW_LE160(bw)		(BW_LE80(bw) || ((bw) == WL_CHANSPEC_BW_160))
348 
349 #define CHSPEC_IS6G(chspec)	(((chspec) & WL_CHANSPEC_BAND_MASK) == WL_CHANSPEC_BAND_6G)
350 #define CHSPEC_IS5G(chspec)	(((chspec) & WL_CHANSPEC_BAND_MASK) == WL_CHANSPEC_BAND_5G)
351 #define CHSPEC_IS2G(chspec)	(((chspec) & WL_CHANSPEC_BAND_MASK) == WL_CHANSPEC_BAND_2G)
352 #define CHSPEC_SB_UPPER(chspec)	\
353 	((((chspec) & WL_CHANSPEC_CTL_SB_MASK) == WL_CHANSPEC_CTL_SB_UPPER) && \
354 	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_40))
355 #define CHSPEC_SB_LOWER(chspec)	\
356 	((((chspec) & WL_CHANSPEC_CTL_SB_MASK) == WL_CHANSPEC_CTL_SB_LOWER) && \
357 	(((chspec) & WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_40))
358 
359 #ifdef WL_6G_BAND
360 #define CHSPEC2WLC_BAND(chspec) (CHSPEC_IS2G(chspec) ? WLC_BAND_2G : CHSPEC_IS5G(chspec) ? \
361 	WLC_BAND_5G : WLC_BAND_6G)
362 #else
363 #define CHSPEC2WLC_BAND(chspec) (CHSPEC_IS2G(chspec) ? WLC_BAND_2G : WLC_BAND_5G)
364 #endif
365 
366 #define CHSPEC_BW_CHANGED(prev_chspec, curr_chspec) \
367 	(((prev_chspec) & WL_CHANSPEC_BW_MASK) != ((curr_chspec) & WL_CHANSPEC_BW_MASK))
368 
369 #if (defined(WL_BAND6G) && !defined(WL_BAND6G_DISABLED))
370 #define CHSPEC_IS_5G_6G(chspec)		(CHSPEC_IS5G(chspec) || CHSPEC_IS6G(chspec))
371 #define CHSPEC_IS20_5G_6G(chspec)	((((chspec) & \
372 					WL_CHANSPEC_BW_MASK) == WL_CHANSPEC_BW_20) && \
373 					(CHSPEC_IS5G(chspec) || CHSPEC_IS6G(chspec)))
374 #else
375 #define CHSPEC_IS_5G_6G(chspec)		(CHSPEC_IS5G(chspec))
376 #define CHSPEC_IS20_5G_6G(chspec)	(CHSPEC_IS20_5G(chspec))
377 #endif
378 
379 /**
380  * Number of chars needed for wf_chspec_ntoa() destination character buffer.
381  */
382 #ifdef WL11BE
383 #define CHANSPEC_STR_LEN    22
384 #else
385 #define CHANSPEC_STR_LEN    20
386 #endif
387 
388 /*
389  * This function returns TRUE if both the chanspec can co-exist in PHY.
390  * Addition to primary20 channel, the function checks for side band for 2g 40 channels
391  */
392 extern bool wf_chspec_coexist(chanspec_t chspec1, chanspec_t chspec2);
393 
394 #define CHSPEC_IS_BW_160_WIDE(chspec) (CHSPEC_BW(chspec) == WL_CHANSPEC_BW_160 ||\
395 	CHSPEC_BW(chspec) == WL_CHANSPEC_BW_8080)
396 
397 /* BW inequality comparisons, GE (>=), GT (>) */
398 
399 #define CHSPEC_BW_GE(chspec, bw) (CHSPEC_BW(chspec) >= (bw))
400 
401 #define CHSPEC_BW_GT(chspec, bw) (CHSPEC_BW(chspec) > (bw))
402 
403 /* Legacy Chanspec defines
404  * These are the defines for the previous format of the chanspec_t
405  */
406 #define WL_LCHANSPEC_CHAN_MASK		0x00ff
407 #define WL_LCHANSPEC_CHAN_SHIFT		     0
408 
409 #define WL_LCHANSPEC_CTL_SB_MASK	0x0300
410 #define WL_LCHANSPEC_CTL_SB_SHIFT	     8
411 #define WL_LCHANSPEC_CTL_SB_LOWER	0x0100
412 #define WL_LCHANSPEC_CTL_SB_UPPER	0x0200
413 #define WL_LCHANSPEC_CTL_SB_NONE	0x0300
414 
415 #define WL_LCHANSPEC_BW_MASK		0x0C00
416 #define WL_LCHANSPEC_BW_SHIFT		    10
417 #define WL_LCHANSPEC_BW_10		0x0400
418 #define WL_LCHANSPEC_BW_20		0x0800
419 #define WL_LCHANSPEC_BW_40		0x0C00
420 
421 #define WL_LCHANSPEC_BAND_MASK		0xf000
422 #define WL_LCHANSPEC_BAND_SHIFT		    12
423 #define WL_LCHANSPEC_BAND_5G		0x1000
424 #define WL_LCHANSPEC_BAND_2G		0x2000
425 
426 #define LCHSPEC_CHANNEL(chspec)	((uint8)((chspec) & WL_LCHANSPEC_CHAN_MASK))
427 #define LCHSPEC_BAND(chspec)	((chspec) & WL_LCHANSPEC_BAND_MASK)
428 #define LCHSPEC_CTL_SB(chspec)	((chspec) & WL_LCHANSPEC_CTL_SB_MASK)
429 #define LCHSPEC_BW(chspec)	((chspec) & WL_LCHANSPEC_BW_MASK)
430 #define LCHSPEC_IS20(chspec)	(((chspec) & WL_LCHANSPEC_BW_MASK) == WL_LCHANSPEC_BW_20)
431 #define LCHSPEC_IS40(chspec)	(((chspec) & WL_LCHANSPEC_BW_MASK) == WL_LCHANSPEC_BW_40)
432 #define LCHSPEC_IS5G(chspec)	(((chspec) & WL_LCHANSPEC_BAND_MASK) == WL_LCHANSPEC_BAND_5G)
433 #define LCHSPEC_IS2G(chspec)	(((chspec) & WL_LCHANSPEC_BAND_MASK) == WL_LCHANSPEC_BAND_2G)
434 
435 #define LCHSPEC_SB_UPPER(chspec)	\
436 	((((chspec) & WL_LCHANSPEC_CTL_SB_MASK) == WL_LCHANSPEC_CTL_SB_UPPER) && \
437 	(((chspec) & WL_LCHANSPEC_BW_MASK) == WL_LCHANSPEC_BW_40))
438 #define LCHSPEC_SB_LOWER(chspec)	\
439 	((((chspec) & WL_LCHANSPEC_CTL_SB_MASK) == WL_LCHANSPEC_CTL_SB_LOWER) && \
440 	(((chspec) & WL_LCHANSPEC_BW_MASK) == WL_LCHANSPEC_BW_40))
441 
442 #define LCHSPEC_CREATE(chan, band, bw, sb)  ((uint16)((chan) | (sb) | (bw) | (band)))
443 
444 #define CH20MHZ_LCHSPEC(channel) \
445 	(chanspec_t)((chanspec_t)(channel) | WL_LCHANSPEC_BW_20 | \
446 	WL_LCHANSPEC_CTL_SB_NONE | (((channel) <= CH_MAX_2G_CHANNEL) ? \
447 	WL_LCHANSPEC_BAND_2G : WL_LCHANSPEC_BAND_5G))
448 
449 #define GET_ALL_EXT wf_get_all_ext
450 
451 /*
452  * WF_CHAN_FACTOR_* constants are used to calculate channel frequency
453  * given a channel number.
454  * chan_freq = chan_factor * 500Mhz + chan_number * 5
455  */
456 
457 /**
458  * Channel Factor for the starting frequence of 2.4 GHz channels.
459  * The value corresponds to 2407 MHz.
460  */
461 #define WF_CHAN_FACTOR_2_4_G		4814u	/* 2.4 GHz band, 2407 MHz */
462 
463 /**
464  * Channel Factor for the starting frequence of 4.9 GHz channels.
465  * The value corresponds to 4000 MHz.
466  */
467 #define WF_CHAN_FACTOR_4_G		8000u	/* 4.9 GHz band for Japan */
468 
469 /**
470  * Channel Factor for the starting frequence of 5 GHz channels.
471  * The value corresponds to 5000 MHz.
472  */
473 #define WF_CHAN_FACTOR_5_G		10000u	/* 5   GHz band, 5000 MHz */
474 
475 /**
476  * Channel Factor for the starting frequence of 6 GHz channels.
477  * The value corresponds to 5940 MHz.
478  */
479 #define WF_CHAN_FACTOR_6_G		11900u	/* 6   GHz band, 5950 MHz */
480 
481 #define WLC_2G_25MHZ_OFFSET		5	/* 2.4GHz band channel offset */
482 
483 /**
484  *  No of sub-band value of the specified Mhz chanspec
485  */
486 #define WF_NUM_SIDEBANDS_40MHZ   2u
487 #define WF_NUM_SIDEBANDS_80MHZ   4u
488 #define WF_NUM_SIDEBANDS_160MHZ  8u
489 
490 /**
491  * Return the chanspec bandwidth in MHz
492  */
493 uint wf_bw_chspec_to_mhz(chanspec_t chspec);
494 
495 /**
496  * Return the bandwidth string for a given chanspec
497  */
498 const char *wf_chspec_to_bw_str(chanspec_t chspec);
499 
500 /**
501  * Convert chanspec to ascii string, or formats hex of an invalid chanspec.
502  */
503 char * wf_chspec_ntoa_ex(chanspec_t chspec, char *buf);
504 
505 /**
506  * Convert chanspec to ascii string, or returns NULL on error.
507  */
508 char * wf_chspec_ntoa(chanspec_t chspec, char *buf);
509 
510 /**
511  * Convert ascii string to chanspec
512  */
513 chanspec_t wf_chspec_aton(const char *a);
514 
515 /**
516  * Verify the chanspec fields are valid for a chanspec_t
517  */
518 bool wf_chspec_malformed(chanspec_t chanspec);
519 
520 /**
521  * Verify the chanspec specifies a valid channel according to 802.11.
522  */
523 bool wf_chspec_valid(chanspec_t chanspec);
524 
525 /**
526  * Verify that the channel is a valid 20MHz channel according to 802.11.
527  */
528 bool wf_valid_20MHz_chan(uint channel, chanspec_band_t band);
529 
530 /**
531  * Verify that the center channel is a valid 40MHz center channel according to 802.11.
532  */
533 bool wf_valid_40MHz_center_chan(uint center_channel, chanspec_band_t band);
534 
535 /**
536  * Verify that the center channel is a valid 80MHz center channel according to 802.11.
537  */
538 bool wf_valid_80MHz_center_chan(uint center_channel, chanspec_band_t band);
539 
540 /**
541  * Verify that the center channel is a valid 160MHz center channel according to 802.11.
542  */
543 bool wf_valid_160MHz_center_chan(uint center_channel, chanspec_band_t band);
544 
545 /**
546  * Verify that the center channel is a valid 240MHz center channel according to 802.11.
547  */
548 bool wf_valid_240MHz_center_chan(uint center_channel, chanspec_band_t band);
549 
550 /**
551  * Verify that the center channel is a valid 320MHz center channel according to 802.11.
552  */
553 bool wf_valid_320MHz_center_chan(uint center_channel, chanspec_band_t band);
554 
555 /**
556  * Create a 20MHz chanspec for the given band.
557  */
558 chanspec_t wf_create_20MHz_chspec(uint channel, chanspec_band_t band);
559 
560 /**
561  * Returns the chanspec for a 40MHz channel given the primary 20MHz channel number,
562  * the center channel number, and the band.
563  */
564 chanspec_t wf_create_40MHz_chspec(uint primary_channel, uint center_channel,
565                                   chanspec_band_t band);
566 
567 /**
568  * Returns the chanspec for a 40MHz channel given the primary 20MHz channel number,
569  * the sub-band for the primary 20MHz channel, and the band.
570  */
571 chanspec_t wf_create_40MHz_chspec_primary_sb(uint primary_channel,
572                                              chanspec_subband_t primary_subband,
573                                              chanspec_band_t band);
574 /**
575  * Returns the chanspec for an 80MHz channel given the primary 20MHz channel number,
576  * the center channel number, and the band.
577  */
578 chanspec_t wf_create_80MHz_chspec(uint primary_channel, uint center_channel,
579                                   chanspec_band_t band);
580 
581 /**
582  * Returns the chanspec for an 160MHz channel given the primary 20MHz channel number,
583  * the center channel number, and the band.
584  */
585 chanspec_t wf_create_160MHz_chspec(uint primary_channel, uint center_channel,
586                                    chanspec_band_t band);
587 
588 /**
589  * Returns the chanspec for an 240MHz channel given the primary 20MHz channel number,
590  * the center channel number, and the band.
591  */
592 chanspec_t wf_create_240MHz_chspec(uint primary_channel, uint center_channel,
593                                    chanspec_band_t band);
594 
595 /**
596  * Returns the chanspec for an 320MHz channel given the primary 20MHz channel number,
597  * the center channel number, and the band.
598  */
599 chanspec_t wf_create_320MHz_chspec(uint primary_channel, uint center_channel,
600                                    chanspec_band_t band);
601 
602 /**
603  * Returns the chanspec for an 80+80MHz channel given the primary 20MHz channel number,
604  * the center channel numbers for each frequency segment, and the band.
605  */
606 chanspec_t wf_create_8080MHz_chspec(uint primary_channel, uint chan0, uint chan1,
607                                     chanspec_band_t band);
608 
609 /**
610  * Returns the chanspec for an 160+160MHz channel given the primary 20MHz channel number,
611  * the center channel numbers for each frequency segment, and the band.
612  */
613 chanspec_t wf_create_160160MHz_chspec(uint primary_channel, uint chan0, uint chan1,
614                                       chanspec_band_t band);
615 /**
616  * Returns the chanspec given the primary 20MHz channel number,
617  * the center channel number, channel width, and the band.
618  *
619  * The channel width must be 20, 40, 80, or 160 MHz.
620  */
621 chanspec_t wf_create_chspec(uint primary_channel, uint center_channel,
622                             chanspec_bw_t bw, chanspec_band_t band);
623 
624 /**
625  * Returns the chanspec given the primary 20MHz channel number,
626  * channel width, and the band.
627  */
628 chanspec_t wf_create_chspec_from_primary(uint primary_channel, chanspec_bw_t bw,
629                                          chanspec_band_t band);
630 
631 /**
632  * Returns the chanspec given the index of primary 20MHz channel within whole
633  * channel, the center channel number, channel width, and the band.
634  *
635  * The channel width must be 20, 40, 80, or 160 MHz.
636  */
637 chanspec_t wf_create_chspec_sb(uint sb, uint center_channel, chanspec_bw_t bw,
638                                chanspec_band_t band);
639 
640 /**
641  * Returns the chanspec for an 160+160MHz channel given the index of primary 20MHz
642  * channel within whole channel pair (0-3 if within chan0, 4-7 if within chan1),
643  * the center channel numbers for each frequency segment, and the band.
644  */
645 chanspec_t wf_create_160160MHz_chspec_sb(uint sb, uint chan0, uint chan1,
646                                          chanspec_band_t band);
647 
648 /**
649  * Return the primary 20MHz channel.
650  */
651 uint8 wf_chspec_primary20_chan(chanspec_t chspec);
652 
653 /* alias for old function name */
654 #define wf_chspec_ctlchan(c) wf_chspec_primary20_chan(c)
655 
656 /**
657  * Return the primary 20MHz chanspec of a given chanspec
658  */
659 chanspec_t wf_chspec_primary20_chspec(chanspec_t chspec);
660 
661 /* alias for old function name */
662 #define wf_chspec_ctlchspec(c) wf_chspec_primary20_chspec(c)
663 
664 /**
665  * Return the primary 40MHz chanspec for a 40MHz or wider channel
666  */
667 chanspec_t wf_chspec_primary40_chspec(chanspec_t chspec);
668 
669 /**
670  * Return the channel number for a given frequency and base frequency
671  */
672 int wf_mhz2channel(uint freq, uint start_factor);
673 
674 /**
675  * Return the center frequency in MHz of the given channel and base frequency.
676  */
677 int wf_channel2mhz(uint channel, uint start_factor);
678 
679 /**
680  * Returns the chanspec 80Mhz channel corresponding to the following input
681  * parameters
682  *
683  *	primary_channel - primary 20Mhz channel
684  *	center_channel   - center frequecny of the 80Mhz channel
685  *
686  * The center_channel can be one of {42, 58, 106, 122, 138, 155}
687  *
688  * returns INVCHANSPEC in case of error
689  */
690 extern chanspec_t wf_chspec_80(uint8 center_channel, uint8 primary_channel);
691 
692 /**
693  * Convert ctl chan and bw to chanspec
694  *
695  * @param	ctl_ch		channel
696  * @param	bw	        bandwidth
697  *
698  * @return	> 0 if successful or 0 otherwise
699  *
700  */
701 extern uint16 wf_channel2chspec(uint ctl_ch, uint bw);
702 
703 /*
704  * Returns the 80+80 MHz chanspec corresponding to the following input parameters
705  *
706  *    primary_20mhz - Primary 20 MHz channel
707  *    chan0_80MHz - center channel number of one frequency segment
708  *    chan1_80MHz - center channel number of the other frequency segment
709  *
710  * Parameters chan0_80MHz and chan1_80MHz are channel numbers in {42, 58, 106, 122, 138, 155}.
711  * The primary channel must be contained in one of the 80MHz channels. This routine
712  * will determine which frequency segment is the primary 80 MHz segment.
713  *
714  * Returns INVCHANSPEC in case of error.
715  *
716  * Refer to 802.11-2016 section 22.3.14 "Channelization".
717  */
718 extern chanspec_t wf_chspec_get8080_chspec(uint8 primary_20mhz,
719 	uint8 chan0_80Mhz, uint8 chan1_80Mhz);
720 
721 /**
722  * Returns the center channel of the primary 80 MHz sub-band of the provided chanspec
723  *
724  * @param	chspec    input chanspec
725  *
726  * @return	center channel number of the primary 80MHz sub-band of the input.
727  *		Will return the center channel of an input 80MHz chspec.
728  *		Will return INVCHANNEL if the chspec is malformed or less than 80MHz bw.
729  */
730 extern uint8 wf_chspec_primary80_channel(chanspec_t chanspec);
731 
732 /**
733  * Returns the center channel of the secondary 80 MHz sub-band of the provided chanspec
734  *
735  * @param	chspec    input chanspec
736  *
737  * @return	center channel number of the secondary 80MHz sub-band of the input.
738  *		Will return INVCHANNEL if the chspec is malformed or bw is not greater than 80MHz.
739  */
740 extern uint8 wf_chspec_secondary80_channel(chanspec_t chanspec);
741 
742 /**
743  * Returns the chanspec for the primary 80MHz sub-band of an 160MHz or 80+80 channel
744  *
745  * @param	chspec    input chanspec
746  *
747  * @return	An 80MHz chanspec describing the primary 80MHz sub-band of the input.
748  *		Will return an input 80MHz chspec as is.
749  *		Will return INVCHANSPEC if the chspec is malformed or less than 80MHz bw.
750  */
751 extern chanspec_t wf_chspec_primary80_chspec(chanspec_t chspec);
752 
753 /**
754  * Returns the chanspec for the secondary 80MHz sub-band of an 160MHz or 80+80 channel
755  * The sideband in the chanspec is always set to WL_CHANSPEC_CTL_SB_LL since this sub-band
756  * does not contain the primary 20MHz channel.
757  *
758  * @param	chspec    input chanspec
759  *
760  * @return	An 80MHz chanspec describing the secondary 80MHz sub-band of the input.
761  *		Will return INVCHANSPEC if the chspec is malformed or bw is not greater than 80MHz.
762  */
763 extern chanspec_t wf_chspec_secondary80_chspec(chanspec_t chspec);
764 
765 /**
766  * Returns the center channel of the primary 160MHz sub-band of the provided chanspec
767  *
768  * @param	chspec    input chanspec
769  *
770  * @return	center channel number of the primary 160MHz sub-band of the input.
771  *		Will return the center channel of an input 160MHz chspec.
772  *		Will return INVCHANNEL if the chspec is malformed or less than 160MHz bw.
773  */
774 extern uint8 wf_chspec_primary160_channel(chanspec_t chanspec);
775 
776 /**
777  * Returns the chanspec for the primary 160MHz sub-band of an 320MHz channel
778  *
779  * @param	chspec    input chanspec
780  *
781  * @return	An 160MHz chanspec describing the primary 160MHz sub-band of the input.
782  *		Will return an input 160MHz chspec as is.
783  *		Will return INVCHANSPEC if the chspec is malformed or less than 160MHz bw.
784  */
785 extern chanspec_t wf_chspec_primary160_chspec(chanspec_t chspec);
786 
787 /*
788  * For 160MHz or 80P80 chanspec, set ch[0]/ch[1] to be the low/high 80 Mhz channels
789  *
790  * For 20/40/80MHz chanspec, set ch[0] to be the center freq, and chan[1]=-1
791  */
792 extern void wf_chspec_get_80p80_channels(chanspec_t chspec, uint8 *ch);
793 
794 /* wf_chanspec_iter_... iterator API is deprecated. Use wlc_clm_chanspec_iter_... API instead */
795 
796 struct wf_iter_range {
797 	uint8 start;
798 	uint8 end;
799 };
800 
801 /* Internal structure for wf_chanspec_iter_* functions.
802  * Do not directly access the members. Only use the related
803  * functions to query and manipulate the structure.
804  */
805 typedef struct chanspec_iter {
806 	uint8 state;
807 	chanspec_t chanspec;
808 	chanspec_band_t band;
809 	chanspec_bw_t bw;
810 	struct wf_iter_range range;
811 	union {
812 		uint8 range_id;
813 		struct {
814 			uint8 ch0;
815 			uint8 ch1;
816 		};
817 	};
818 } wf_chanspec_iter_t;
819 
820 /**
821  * Initialize a chanspec iteration structure.
822  * The parameters define the set of chanspecs to generate in the iteration.
823  * After initialization wf_chanspec_iter_current() will return the first chanspec
824  * in the set. A call to wf_chanspec_iter_next() will advance the interation
825  * to the next chanspec in the set.
826  *
827  * Example use:
828  *      wf_chanspec_iter_t iter;
829  *      chanspec_t chanspec;
830  *
831  *      wf_chanspec_iter_init(&iter, band, bw);
832  *
833  *      while (wf_chanspec_iter_next(&iter, &chanspec)) {
834  *              ... do some work ...
835  *      }
836  *
837  * @param iter  pointer to a wf_chanspec_iter_t structure to initialize
838  * @param band  chanspec_band_t value specifying the band of interest
839  * @param bw    chanspec_bw_t value specifying the bandwidth of interest,
840  *              or INVCHANSPEC to specify all bandwidths
841  *
842  * @return a success value, FALSE on error, or TRUE if OK
843  */
844 bool wf_chanspec_iter_init(wf_chanspec_iter_t *iter, chanspec_band_t band, chanspec_bw_t bw);
845 
846 /**
847  * Advance the iteration to the next chanspec in the set.
848  *
849  * @param iter    pointer to a wf_chanspec_iter_t structure
850  * @param chspec  pointer to storage for the next chanspec. Return value will be INVCHANSPEC
851  *                if the iteration ended. Pass in NULL if return value is not desired.
852  *
853  * @return a success value, TRUE if there was another chanspec in the iteration, FALSE if not
854  */
855 bool wf_chanspec_iter_next(wf_chanspec_iter_t *iter, chanspec_t *chspec);
856 
857 /**
858  * Return the current chanspec of the iteration.
859  *
860  * @param iter  pointer to a wf_chanspec_iter_t structure
861  *
862  * @return the current chanspec_t
863  */
864 chanspec_t wf_chanspec_iter_current(wf_chanspec_iter_t *iter);
865 
866 /* Populates array with all 20MHz side bands of a given chanspec_t in the following order:
867  *		primary20, ext20, two ext40s, four ext80s.
868  *     'chspec' is the chanspec of interest
869  *     'pext' must point to an uint8 array of long enough to hold all side bands of the given chspec
870  *
871  * Works with 20, 40, 80 and 160MHz chspec
872  */
873 
874 extern void wf_get_all_ext(chanspec_t chspec, uint8 *chan_ptr);
875 
876 /*
877  * Given two chanspecs, returns true if they overlap.
878  * (Overlap: At least one 20MHz subband is common between the two chanspecs provided)
879  */
880 extern bool wf_chspec_overlap(chanspec_t chspec0, chanspec_t chspec1);
881 
882 extern uint8 channel_bw_to_width(chanspec_t chspec);
883 
884 uint8 wf_chspec_320_id2cch(chanspec_t chanspec);
885 
886 uint8 wf_chspec_240_id2cch(chanspec_t chanspec);
887 
888 #endif	/* _bcmwifi_channels_h_ */
889