Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / brcm80211 / sys / wlc_rate.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/kernel.h>
17 #include <bcmdefs.h>
18 #include <wlc_cfg.h>
19 #include <osl.h>
20 #include <linuxver.h>
21 #include <bcmutils.h>
22 #include <siutils.h>
23 #include <bcmendian.h>
24 #include <wlioctl.h>
25
26 #include <proto/802.11.h>
27 #include <d11.h>
28 #include <wlc_rate.h>
29 #include <wl_dbg.h>
30 #include <wlc_pub.h>
31
32 /* Rate info per rate: It tells whether a rate is ofdm or not and its phy_rate value */
33 const u8 rate_info[WLC_MAXRATE + 1] = {
34         /*  0     1     2     3     4     5     6     7     8     9 */
35 /*   0 */ 0x00, 0x00, 0x0a, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
36 /*  10 */ 0x00, 0x37, 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x00,
37 /*  20 */ 0x00, 0x00, 0x6e, 0x00, 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00,
38 /*  30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
39 /*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x00,
40 /*  50 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 /*  60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 /*  70 */ 0x00, 0x00, 0x8d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 /*  80 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 /*  90 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
45 /* 100 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c
46 };
47
48 /* rates are in units of Kbps */
49 const mcs_info_t mcs_table[MCS_TABLE_SIZE] = {
50         /* MCS  0: SS 1, MOD: BPSK,  CR 1/2 */
51         {6500, 13500, CEIL(6500 * 10, 9), CEIL(13500 * 10, 9), 0x00,
52          WLC_RATE_6M},
53         /* MCS  1: SS 1, MOD: QPSK,  CR 1/2 */
54         {13000, 27000, CEIL(13000 * 10, 9), CEIL(27000 * 10, 9), 0x08,
55          WLC_RATE_12M},
56         /* MCS  2: SS 1, MOD: QPSK,  CR 3/4 */
57         {19500, 40500, CEIL(19500 * 10, 9), CEIL(40500 * 10, 9), 0x0A,
58          WLC_RATE_18M},
59         /* MCS  3: SS 1, MOD: 16QAM, CR 1/2 */
60         {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0x10,
61          WLC_RATE_24M},
62         /* MCS  4: SS 1, MOD: 16QAM, CR 3/4 */
63         {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x12,
64          WLC_RATE_36M},
65         /* MCS  5: SS 1, MOD: 64QAM, CR 2/3 */
66         {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0x19,
67          WLC_RATE_48M},
68         /* MCS  6: SS 1, MOD: 64QAM, CR 3/4 */
69         {58500, 121500, CEIL(58500 * 10, 9), CEIL(121500 * 10, 9), 0x1A,
70          WLC_RATE_54M},
71         /* MCS  7: SS 1, MOD: 64QAM, CR 5/6 */
72         {65000, 135000, CEIL(65000 * 10, 9), CEIL(135000 * 10, 9), 0x1C,
73          WLC_RATE_54M},
74         /* MCS  8: SS 2, MOD: BPSK,  CR 1/2 */
75         {13000, 27000, CEIL(13000 * 10, 9), CEIL(27000 * 10, 9), 0x40,
76          WLC_RATE_6M},
77         /* MCS  9: SS 2, MOD: QPSK,  CR 1/2 */
78         {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0x48,
79          WLC_RATE_12M},
80         /* MCS 10: SS 2, MOD: QPSK,  CR 3/4 */
81         {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x4A,
82          WLC_RATE_18M},
83         /* MCS 11: SS 2, MOD: 16QAM, CR 1/2 */
84         {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0x50,
85          WLC_RATE_24M},
86         /* MCS 12: SS 2, MOD: 16QAM, CR 3/4 */
87         {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0x52,
88          WLC_RATE_36M},
89         /* MCS 13: SS 2, MOD: 64QAM, CR 2/3 */
90         {104000, 216000, CEIL(104000 * 10, 9), CEIL(216000 * 10, 9), 0x59,
91          WLC_RATE_48M},
92         /* MCS 14: SS 2, MOD: 64QAM, CR 3/4 */
93         {117000, 243000, CEIL(117000 * 10, 9), CEIL(243000 * 10, 9), 0x5A,
94          WLC_RATE_54M},
95         /* MCS 15: SS 2, MOD: 64QAM, CR 5/6 */
96         {130000, 270000, CEIL(130000 * 10, 9), CEIL(270000 * 10, 9), 0x5C,
97          WLC_RATE_54M},
98         /* MCS 16: SS 3, MOD: BPSK,  CR 1/2 */
99         {19500, 40500, CEIL(19500 * 10, 9), CEIL(40500 * 10, 9), 0x80,
100          WLC_RATE_6M},
101         /* MCS 17: SS 3, MOD: QPSK,  CR 1/2 */
102         {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x88,
103          WLC_RATE_12M},
104         /* MCS 18: SS 3, MOD: QPSK,  CR 3/4 */
105         {58500, 121500, CEIL(58500 * 10, 9), CEIL(121500 * 10, 9), 0x8A,
106          WLC_RATE_18M},
107         /* MCS 19: SS 3, MOD: 16QAM, CR 1/2 */
108         {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0x90,
109          WLC_RATE_24M},
110         /* MCS 20: SS 3, MOD: 16QAM, CR 3/4 */
111         {117000, 243000, CEIL(117000 * 10, 9), CEIL(243000 * 10, 9), 0x92,
112          WLC_RATE_36M},
113         /* MCS 21: SS 3, MOD: 64QAM, CR 2/3 */
114         {156000, 324000, CEIL(156000 * 10, 9), CEIL(324000 * 10, 9), 0x99,
115          WLC_RATE_48M},
116         /* MCS 22: SS 3, MOD: 64QAM, CR 3/4 */
117         {175500, 364500, CEIL(175500 * 10, 9), CEIL(364500 * 10, 9), 0x9A,
118          WLC_RATE_54M},
119         /* MCS 23: SS 3, MOD: 64QAM, CR 5/6 */
120         {195000, 405000, CEIL(195000 * 10, 9), CEIL(405000 * 10, 9), 0x9B,
121          WLC_RATE_54M},
122         /* MCS 24: SS 4, MOD: BPSK,  CR 1/2 */
123         {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0xC0,
124          WLC_RATE_6M},
125         /* MCS 25: SS 4, MOD: QPSK,  CR 1/2 */
126         {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0xC8,
127          WLC_RATE_12M},
128         /* MCS 26: SS 4, MOD: QPSK,  CR 3/4 */
129         {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0xCA,
130          WLC_RATE_18M},
131         /* MCS 27: SS 4, MOD: 16QAM, CR 1/2 */
132         {104000, 216000, CEIL(104000 * 10, 9), CEIL(216000 * 10, 9), 0xD0,
133          WLC_RATE_24M},
134         /* MCS 28: SS 4, MOD: 16QAM, CR 3/4 */
135         {156000, 324000, CEIL(156000 * 10, 9), CEIL(324000 * 10, 9), 0xD2,
136          WLC_RATE_36M},
137         /* MCS 29: SS 4, MOD: 64QAM, CR 2/3 */
138         {208000, 432000, CEIL(208000 * 10, 9), CEIL(432000 * 10, 9), 0xD9,
139          WLC_RATE_48M},
140         /* MCS 30: SS 4, MOD: 64QAM, CR 3/4 */
141         {234000, 486000, CEIL(234000 * 10, 9), CEIL(486000 * 10, 9), 0xDA,
142          WLC_RATE_54M},
143         /* MCS 31: SS 4, MOD: 64QAM, CR 5/6 */
144         {260000, 540000, CEIL(260000 * 10, 9), CEIL(540000 * 10, 9), 0xDB,
145          WLC_RATE_54M},
146         /* MCS 32: SS 1, MOD: BPSK,  CR 1/2 */
147         {0, 6000, 0, CEIL(6000 * 10, 9), 0x00, WLC_RATE_6M},
148 };
149
150 /* phycfg for legacy OFDM frames: code rate, modulation scheme, spatial streams
151  *   Number of spatial streams: always 1
152  *   other fields: refer to table 78 of section 17.3.2.2 of the original .11a standard
153  */
154 typedef struct legacy_phycfg {
155         u32 rate_ofdm;  /* ofdm mac rate */
156         u8 tx_phy_ctl3; /* phy ctl byte 3, code rate, modulation type, # of streams */
157 } legacy_phycfg_t;
158
159 #define LEGACY_PHYCFG_TABLE_SIZE        12      /* Number of legacy_rate_cfg entries in the table */
160
161 /* In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate */
162 /* Eventually MIMOPHY would also be converted to this format */
163 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
164 static const legacy_phycfg_t legacy_phycfg_table[LEGACY_PHYCFG_TABLE_SIZE] = {
165         {WLC_RATE_1M, 0x00},    /* CCK  1Mbps,  data rate  0 */
166         {WLC_RATE_2M, 0x08},    /* CCK  2Mbps,  data rate  1 */
167         {WLC_RATE_5M5, 0x10},   /* CCK  5.5Mbps,  data rate  2 */
168         {WLC_RATE_11M, 0x18},   /* CCK  11Mbps,  data rate   3 */
169         {WLC_RATE_6M, 0x00},    /* OFDM  6Mbps,  code rate 1/2, BPSK,   1 spatial stream */
170         {WLC_RATE_9M, 0x02},    /* OFDM  9Mbps,  code rate 3/4, BPSK,   1 spatial stream */
171         {WLC_RATE_12M, 0x08},   /* OFDM  12Mbps, code rate 1/2, QPSK,   1 spatial stream */
172         {WLC_RATE_18M, 0x0A},   /* OFDM  18Mbps, code rate 3/4, QPSK,   1 spatial stream */
173         {WLC_RATE_24M, 0x10},   /* OFDM  24Mbps, code rate 1/2, 16-QAM, 1 spatial stream */
174         {WLC_RATE_36M, 0x12},   /* OFDM  36Mbps, code rate 3/4, 16-QAM, 1 spatial stream */
175         {WLC_RATE_48M, 0x19},   /* OFDM  48Mbps, code rate 2/3, 64-QAM, 1 spatial stream */
176         {WLC_RATE_54M, 0x1A},   /* OFDM  54Mbps, code rate 3/4, 64-QAM, 1 spatial stream */
177 };
178
179 /* Hardware rates (also encodes default basic rates) */
180
181 const wlc_rateset_t cck_ofdm_mimo_rates = {
182         12,
183         {                       /*    1b,   2b,   5.5b, 6,    9,    11b,  12,   18,   24,   36,   48,   54 Mbps */
184          0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
185          0x6c},
186         0x00,
187         {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188          0x00, 0x00, 0x00, 0x00}
189 };
190
191 const wlc_rateset_t ofdm_mimo_rates = {
192         8,
193         {                       /*    6b,   9,    12b,  18,   24b,  36,   48,   54 Mbps */
194          0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
195         0x00,
196         {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197          0x00, 0x00, 0x00, 0x00}
198 };
199
200 /* Default ratesets that include MCS32 for 40BW channels */
201 const wlc_rateset_t cck_ofdm_40bw_mimo_rates = {
202         12,
203         {                       /*    1b,   2b,   5.5b, 6,    9,    11b,  12,   18,   24,   36,   48,   54 Mbps */
204          0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
205          0x6c},
206         0x00,
207         {0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208          0x00, 0x00, 0x00, 0x00}
209 };
210
211 const wlc_rateset_t ofdm_40bw_mimo_rates = {
212         8,
213         {                       /*    6b,   9,    12b,  18,   24b,  36,   48,   54 Mbps */
214          0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
215         0x00,
216         {0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217          0x00, 0x00, 0x00, 0x00}
218 };
219
220 const wlc_rateset_t cck_ofdm_rates = {
221         12,
222         {                       /*    1b,   2b,   5.5b, 6,    9,    11b,  12,   18,   24,   36,   48,   54 Mbps */
223          0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
224          0x6c},
225         0x00,
226         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227          0x00, 0x00, 0x00, 0x00}
228 };
229
230 const wlc_rateset_t gphy_legacy_rates = {
231         4,
232         {                       /*    1b,   2b,   5.5b,  11b Mbps */
233          0x82, 0x84, 0x8b, 0x96},
234         0x00,
235         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236          0x00, 0x00, 0x00, 0x00}
237 };
238
239 const wlc_rateset_t ofdm_rates = {
240         8,
241         {                       /*    6b,   9,    12b,  18,   24b,  36,   48,   54 Mbps */
242          0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
243         0x00,
244         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245          0x00, 0x00, 0x00, 0x00}
246 };
247
248 const wlc_rateset_t cck_rates = {
249         4,
250         {                       /*    1b,   2b,   5.5,  11 Mbps */
251          0x82, 0x84, 0x0b, 0x16},
252         0x00,
253         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254          0x00, 0x00, 0x00, 0x00}
255 };
256
257 static bool wlc_rateset_valid(wlc_rateset_t *rs, bool check_brate);
258
259 /* check if rateset is valid.
260  * if check_brate is true, rateset without a basic rate is considered NOT valid.
261  */
262 static bool wlc_rateset_valid(wlc_rateset_t *rs, bool check_brate)
263 {
264         uint idx;
265
266         if (!rs->count)
267                 return false;
268
269         if (!check_brate)
270                 return true;
271
272         /* error if no basic rates */
273         for (idx = 0; idx < rs->count; idx++) {
274                 if (rs->rates[idx] & WLC_RATE_FLAG)
275                         return true;
276         }
277         return false;
278 }
279
280 void wlc_rateset_mcs_upd(wlc_rateset_t *rs, u8 txstreams)
281 {
282         int i;
283         for (i = txstreams; i < MAX_STREAMS_SUPPORTED; i++)
284                 rs->mcs[i] = 0;
285 }
286
287 /* filter based on hardware rateset, and sort filtered rateset with basic bit(s) preserved,
288  * and check if resulting rateset is valid.
289 */
290 bool
291 wlc_rate_hwrs_filter_sort_validate(wlc_rateset_t *rs,
292                                    const wlc_rateset_t *hw_rs,
293                                    bool check_brate, u8 txstreams)
294 {
295         u8 rateset[WLC_MAXRATE + 1];
296         u8 r;
297         uint count;
298         uint i;
299
300         bzero(rateset, sizeof(rateset));
301         count = rs->count;
302
303         for (i = 0; i < count; i++) {
304                 /* mask off "basic rate" bit, WLC_RATE_FLAG */
305                 r = (int)rs->rates[i] & RATE_MASK;
306                 if ((r > WLC_MAXRATE) || (rate_info[r] == 0)) {
307                         continue;
308                 }
309                 rateset[r] = rs->rates[i];      /* preserve basic bit! */
310         }
311
312         /* fill out the rates in order, looking at only supported rates */
313         count = 0;
314         for (i = 0; i < hw_rs->count; i++) {
315                 r = hw_rs->rates[i] & RATE_MASK;
316                 ASSERT(r <= WLC_MAXRATE);
317                 if (rateset[r])
318                         rs->rates[count++] = rateset[r];
319         }
320
321         rs->count = count;
322
323         /* only set the mcs rate bit if the equivalent hw mcs bit is set */
324         for (i = 0; i < MCSSET_LEN; i++)
325                 rs->mcs[i] = (rs->mcs[i] & hw_rs->mcs[i]);
326
327         if (wlc_rateset_valid(rs, check_brate))
328                 return true;
329         else
330                 return false;
331 }
332
333 /* caluclate the rate of a rx'd frame and return it as a ratespec */
334 ratespec_t BCMFASTPATH wlc_compute_rspec(d11rxhdr_t *rxh, u8 *plcp)
335 {
336         int phy_type;
337         ratespec_t rspec = PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT;
338
339         phy_type =
340             ((rxh->RxChan & RXS_CHAN_PHYTYPE_MASK) >> RXS_CHAN_PHYTYPE_SHIFT);
341
342         if ((phy_type == PHY_TYPE_N) || (phy_type == PHY_TYPE_SSN) ||
343             (phy_type == PHY_TYPE_LCN) || (phy_type == PHY_TYPE_HT)) {
344                 switch (rxh->PhyRxStatus_0 & PRXS0_FT_MASK) {
345                 case PRXS0_CCK:
346                         rspec =
347                             CCK_PHY2MAC_RATE(((cck_phy_hdr_t *) plcp)->signal);
348                         break;
349                 case PRXS0_OFDM:
350                         rspec =
351                             OFDM_PHY2MAC_RATE(((ofdm_phy_hdr_t *) plcp)->
352                                               rlpt[0]);
353                         break;
354                 case PRXS0_PREN:
355                         rspec = (plcp[0] & MIMO_PLCP_MCS_MASK) | RSPEC_MIMORATE;
356                         if (plcp[0] & MIMO_PLCP_40MHZ) {
357                                 /* indicate rspec is for 40 MHz mode */
358                                 rspec &= ~RSPEC_BW_MASK;
359                                 rspec |= (PHY_TXC1_BW_40MHZ << RSPEC_BW_SHIFT);
360                         }
361                         break;
362                 case PRXS0_STDN:
363                         /* fallthru */
364                 default:
365                         /* not supported */
366                         ASSERT(0);
367                         break;
368                 }
369                 if (PLCP3_ISSGI(plcp[3]))
370                         rspec |= RSPEC_SHORT_GI;
371         } else
372             if ((phy_type == PHY_TYPE_A) || (rxh->PhyRxStatus_0 & PRXS0_OFDM))
373                 rspec = OFDM_PHY2MAC_RATE(((ofdm_phy_hdr_t *) plcp)->rlpt[0]);
374         else
375                 rspec = CCK_PHY2MAC_RATE(((cck_phy_hdr_t *) plcp)->signal);
376
377         return rspec;
378 }
379
380 /* copy rateset src to dst as-is (no masking or sorting) */
381 void wlc_rateset_copy(const wlc_rateset_t *src, wlc_rateset_t *dst)
382 {
383         bcopy(src, dst, sizeof(wlc_rateset_t));
384 }
385
386 /*
387  * Copy and selectively filter one rateset to another.
388  * 'basic_only' means only copy basic rates.
389  * 'rates' indicates cck (11b) and ofdm rates combinations.
390  *    - 0: cck and ofdm
391  *    - 1: cck only
392  *    - 2: ofdm only
393  * 'xmask' is the copy mask (typically 0x7f or 0xff).
394  */
395 void
396 wlc_rateset_filter(wlc_rateset_t *src, wlc_rateset_t *dst, bool basic_only,
397                    u8 rates, uint xmask, bool mcsallow)
398 {
399         uint i;
400         uint r;
401         uint count;
402
403         count = 0;
404         for (i = 0; i < src->count; i++) {
405                 r = src->rates[i];
406                 if (basic_only && !(r & WLC_RATE_FLAG))
407                         continue;
408                 if ((rates == WLC_RATES_CCK) && IS_OFDM((r & RATE_MASK)))
409                         continue;
410                 if ((rates == WLC_RATES_OFDM) && IS_CCK((r & RATE_MASK)))
411                         continue;
412                 dst->rates[count++] = r & xmask;
413         }
414         dst->count = count;
415         dst->htphy_membership = src->htphy_membership;
416
417         if (mcsallow && rates != WLC_RATES_CCK)
418                 bcopy(&src->mcs[0], &dst->mcs[0], MCSSET_LEN);
419         else
420                 wlc_rateset_mcs_clear(dst);
421 }
422
423 /* select rateset for a given phy_type and bandtype and filter it, sort it
424  * and fill rs_tgt with result
425  */
426 void
427 wlc_rateset_default(wlc_rateset_t *rs_tgt, const wlc_rateset_t *rs_hw,
428                     uint phy_type, int bandtype, bool cck_only, uint rate_mask,
429                     bool mcsallow, u8 bw, u8 txstreams)
430 {
431         const wlc_rateset_t *rs_dflt;
432         wlc_rateset_t rs_sel;
433         if ((PHYTYPE_IS(phy_type, PHY_TYPE_HT)) ||
434             (PHYTYPE_IS(phy_type, PHY_TYPE_N)) ||
435             (PHYTYPE_IS(phy_type, PHY_TYPE_LCN)) ||
436             (PHYTYPE_IS(phy_type, PHY_TYPE_SSN))) {
437                 if (BAND_5G(bandtype)) {
438                         rs_dflt = (bw == WLC_20_MHZ ?
439                                    &ofdm_mimo_rates : &ofdm_40bw_mimo_rates);
440                 } else {
441                         rs_dflt = (bw == WLC_20_MHZ ?
442                                    &cck_ofdm_mimo_rates :
443                                    &cck_ofdm_40bw_mimo_rates);
444                 }
445         } else if (PHYTYPE_IS(phy_type, PHY_TYPE_LP)) {
446                 rs_dflt = (BAND_5G(bandtype)) ? &ofdm_rates : &cck_ofdm_rates;
447         } else if (PHYTYPE_IS(phy_type, PHY_TYPE_A)) {
448                 rs_dflt = &ofdm_rates;
449         } else if (PHYTYPE_IS(phy_type, PHY_TYPE_G)) {
450                 rs_dflt = &cck_ofdm_rates;
451         } else {
452                 ASSERT(0);      /* should not happen */
453                 rs_dflt = &cck_rates;   /* force cck */
454         }
455
456         /* if hw rateset is not supplied, assign selected rateset to it */
457         if (!rs_hw)
458                 rs_hw = rs_dflt;
459
460         wlc_rateset_copy(rs_dflt, &rs_sel);
461         wlc_rateset_mcs_upd(&rs_sel, txstreams);
462         wlc_rateset_filter(&rs_sel, rs_tgt, false,
463                            cck_only ? WLC_RATES_CCK : WLC_RATES_CCK_OFDM,
464                            rate_mask, mcsallow);
465         wlc_rate_hwrs_filter_sort_validate(rs_tgt, rs_hw, false,
466                                            mcsallow ? txstreams : 1);
467 }
468
469 s16 BCMFASTPATH wlc_rate_legacy_phyctl(uint rate)
470 {
471         uint i;
472         for (i = 0; i < LEGACY_PHYCFG_TABLE_SIZE; i++)
473                 if (rate == legacy_phycfg_table[i].rate_ofdm)
474                         return legacy_phycfg_table[i].tx_phy_ctl3;
475
476         return -1;
477 }
478
479 void wlc_rateset_mcs_clear(wlc_rateset_t *rateset)
480 {
481         uint i;
482         for (i = 0; i < MCSSET_LEN; i++)
483                 rateset->mcs[i] = 0;
484 }
485
486 void wlc_rateset_mcs_build(wlc_rateset_t *rateset, u8 txstreams)
487 {
488         bcopy(&cck_ofdm_mimo_rates.mcs[0], &rateset->mcs[0], MCSSET_LEN);
489         wlc_rateset_mcs_upd(rateset, txstreams);
490 }
491
492 /* Based on bandwidth passed, allow/disallow MCS 32 in the rateset */
493 void wlc_rateset_bw_mcs_filter(wlc_rateset_t *rateset, u8 bw)
494 {
495         if (bw == WLC_40_MHZ)
496                 setbit(rateset->mcs, 32);
497         else
498                 clrbit(rateset->mcs, 32);
499 }