Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / rate.h
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 #ifndef _BRCM_RATE_H_
18 #define _BRCM_RATE_H_
19
20 #include "types.h"
21
22 extern const u8 rate_info[];
23 extern const struct brcms_rateset cck_ofdm_mimo_rates;
24 extern const struct brcms_rateset ofdm_mimo_rates;
25 extern const struct brcms_rateset cck_ofdm_rates;
26 extern const struct brcms_rateset ofdm_rates;
27 extern const struct brcms_rateset cck_rates;
28 extern const struct brcms_rateset gphy_legacy_rates;
29 extern const struct brcms_rateset wlc_lrs_rates;
30 extern const struct brcms_rateset rate_limit_1_2;
31
32 struct brcms_mcs_info {
33         u32 phy_rate_20;        /* phy rate in kbps [20Mhz] */
34         u32 phy_rate_40;        /* phy rate in kbps [40Mhz] */
35         u32 phy_rate_20_sgi;    /* phy rate in kbps [20Mhz] with SGI */
36         u32 phy_rate_40_sgi;    /* phy rate in kbps [40Mhz] with SGI */
37         u8 tx_phy_ctl3; /* phy ctl byte 3, code rate, modulation type, # of streams */
38         u8 leg_ofdm;            /* matching legacy ofdm rate in 500bkps */
39 };
40
41 #define BRCMS_MAXMCS    32      /* max valid mcs index */
42 #define MCS_TABLE_SIZE  33      /* Number of mcs entries in the table */
43 extern const struct brcms_mcs_info mcs_table[];
44
45 #define MCS_INVALID     0xFF
46 #define MCS_CR_MASK     0x07    /* Code Rate bit mask */
47 #define MCS_MOD_MASK    0x38    /* Modulation bit shift */
48 #define MCS_MOD_SHIFT   3       /* MOdulation bit shift */
49 #define MCS_TXS_MASK    0xc0    /* num tx streams - 1 bit mask */
50 #define MCS_TXS_SHIFT   6       /* num tx streams - 1 bit shift */
51 #define MCS_CR(_mcs)    (mcs_table[_mcs].tx_phy_ctl3 & MCS_CR_MASK)
52 #define MCS_MOD(_mcs)   ((mcs_table[_mcs].tx_phy_ctl3 & MCS_MOD_MASK) >> MCS_MOD_SHIFT)
53 #define MCS_TXS(_mcs)   ((mcs_table[_mcs].tx_phy_ctl3 & MCS_TXS_MASK) >> MCS_TXS_SHIFT)
54 #define MCS_RATE(_mcs, _is40, _sgi)     (_sgi ? \
55         (_is40 ? mcs_table[_mcs].phy_rate_40_sgi : mcs_table[_mcs].phy_rate_20_sgi) : \
56         (_is40 ? mcs_table[_mcs].phy_rate_40 : mcs_table[_mcs].phy_rate_20))
57 #define VALID_MCS(_mcs) ((_mcs < MCS_TABLE_SIZE))
58
59 /* Macro to use the rate_info table */
60 #define BRCMS_RATE_MASK_FULL 0xff /* Rate value mask with basic rate flag */
61
62 /* convert 500kbps to bps */
63 #define BRCMS_RATE_500K_TO_BPS(rate)    ((rate) * 500000)
64
65 /* rate spec : holds rate and mode specific information required to generate a tx frame. */
66 /* Legacy CCK and OFDM information is held in the same manner as was done in the past    */
67 /* (in the lower byte) the upper 3 bytes primarily hold MIMO specific information        */
68
69 /* rate spec bit fields */
70 #define RSPEC_RATE_MASK         0x0000007F      /* Either 500Kbps units or MIMO MCS idx */
71 #define RSPEC_MIMORATE          0x08000000      /* mimo MCS is stored in RSPEC_RATE_MASK */
72 #define RSPEC_BW_MASK           0x00000700      /* mimo bw mask */
73 #define RSPEC_BW_SHIFT          8       /* mimo bw shift */
74 #define RSPEC_STF_MASK          0x00003800      /* mimo Space/Time/Frequency mode mask */
75 #define RSPEC_STF_SHIFT         11      /* mimo Space/Time/Frequency mode shift */
76 #define RSPEC_CT_MASK           0x0000C000      /* mimo coding type mask */
77 #define RSPEC_CT_SHIFT          14      /* mimo coding type shift */
78 #define RSPEC_STC_MASK          0x00300000      /* mimo num STC streams per PLCP defn. */
79 #define RSPEC_STC_SHIFT         20      /* mimo num STC streams per PLCP defn. */
80 #define RSPEC_LDPC_CODING       0x00400000      /* mimo bit indicates adv coding in use */
81 #define RSPEC_SHORT_GI          0x00800000      /* mimo bit indicates short GI in use */
82 #define RSPEC_OVERRIDE          0x80000000      /* bit indicates override both rate & mode */
83 #define RSPEC_OVERRIDE_MCS_ONLY 0x40000000      /* bit indicates override rate only */
84
85 #define BRCMS_HTPHY             127     /* HT PHY Membership */
86
87 #define RSPEC_ACTIVE(rspec)     (rspec & (RSPEC_RATE_MASK | RSPEC_MIMORATE))
88 #define RSPEC2RATE(rspec)       ((rspec & RSPEC_MIMORATE) ? \
89         MCS_RATE((rspec & RSPEC_RATE_MASK), RSPEC_IS40MHZ(rspec), RSPEC_ISSGI(rspec)) : \
90         (rspec & RSPEC_RATE_MASK))
91 /* return rate in unit of 500Kbps -- for internal use in wlc_rate_sel.c */
92 #define RSPEC2RATE500K(rspec)   ((rspec & RSPEC_MIMORATE) ? \
93                 MCS_RATE((rspec & RSPEC_RATE_MASK), state->is40bw, RSPEC_ISSGI(rspec))/500 : \
94                 (rspec & RSPEC_RATE_MASK))
95 #define CRSPEC2RATE500K(rspec)  ((rspec & RSPEC_MIMORATE) ? \
96                 MCS_RATE((rspec & RSPEC_RATE_MASK), RSPEC_IS40MHZ(rspec), RSPEC_ISSGI(rspec))/500 :\
97                 (rspec & RSPEC_RATE_MASK))
98
99 #define RSPEC2KBPS(rspec)       (IS_MCS(rspec) ? RSPEC2RATE(rspec) : RSPEC2RATE(rspec)*500)
100 #define RSPEC_PHYTXBYTE2(rspec) ((rspec & 0xff00) >> 8)
101 #define RSPEC_GET_BW(rspec)     ((rspec & RSPEC_BW_MASK) >> RSPEC_BW_SHIFT)
102 #define RSPEC_IS40MHZ(rspec)    ((((rspec & RSPEC_BW_MASK) >> RSPEC_BW_SHIFT) == \
103                                 PHY_TXC1_BW_40MHZ) || (((rspec & RSPEC_BW_MASK) >> \
104                                 RSPEC_BW_SHIFT) == PHY_TXC1_BW_40MHZ_DUP))
105 #define RSPEC_ISSGI(rspec)      ((rspec & RSPEC_SHORT_GI) == RSPEC_SHORT_GI)
106 #define RSPEC_MIMOPLCP3(rspec)  ((rspec & 0xf00000) >> 16)
107 #define PLCP3_ISSGI(plcp)       (plcp & (RSPEC_SHORT_GI >> 16))
108 #define RSPEC_STC(rspec)        ((rspec & RSPEC_STC_MASK) >> RSPEC_STC_SHIFT)
109 #define RSPEC_STF(rspec)        ((rspec & RSPEC_STF_MASK) >> RSPEC_STF_SHIFT)
110 #define PLCP3_ISSTBC(plcp)      ((plcp & (RSPEC_STC_MASK) >> 16) == 0x10)
111 #define PLCP3_STC_MASK          0x30
112 #define PLCP3_STC_SHIFT         4
113
114 /* Rate info table; takes a legacy rate or ratespec_t */
115 #define IS_MCS(r)       (r & RSPEC_MIMORATE)
116 #define IS_OFDM(r)      (!IS_MCS(r) && (rate_info[(r) & RSPEC_RATE_MASK] & \
117                                         BRCMS_RATE_FLAG))
118 #define IS_CCK(r)       (!IS_MCS(r) && ( \
119                          ((r) & BRCMS_RATE_MASK) == BRCM_RATE_1M || \
120                          ((r) & BRCMS_RATE_MASK) == BRCM_RATE_2M || \
121                          ((r) & BRCMS_RATE_MASK) == BRCM_RATE_5M5 || \
122                          ((r) & BRCMS_RATE_MASK) == BRCM_RATE_11M))
123 #define IS_SINGLE_STREAM(mcs)   (((mcs) <= HIGHEST_SINGLE_STREAM_MCS) || ((mcs) == 32))
124 #define CCK_RSPEC(cck)          ((cck) & RSPEC_RATE_MASK)
125 #define OFDM_RSPEC(ofdm)        (((ofdm) & RSPEC_RATE_MASK) |\
126         (PHY_TXC1_MODE_CDD << RSPEC_STF_SHIFT))
127 #define LEGACY_RSPEC(rate)      (IS_CCK(rate) ? CCK_RSPEC(rate) : OFDM_RSPEC(rate))
128
129 #define MCS_RSPEC(mcs)          (((mcs) & RSPEC_RATE_MASK) | RSPEC_MIMORATE | \
130         (IS_SINGLE_STREAM(mcs) ? (PHY_TXC1_MODE_CDD << RSPEC_STF_SHIFT) : \
131         (PHY_TXC1_MODE_SDM << RSPEC_STF_SHIFT)))
132
133 /* Convert encoded rate value in plcp header to numerical rates in 500 KHz increments */
134 extern const u8 ofdm_rate_lookup[];
135 #define OFDM_PHY2MAC_RATE(rlpt)         (ofdm_rate_lookup[rlpt & 0x7])
136 #define CCK_PHY2MAC_RATE(signal)        (signal/5)
137
138 /* Rates specified in brcms_c_rateset_filter() */
139 #define BRCMS_RATES_CCK_OFDM    0
140 #define BRCMS_RATES_CCK         1
141 #define BRCMS_RATES_OFDM                2
142
143 /* sanitize, and sort a rateset with the basic bit(s) preserved, validate rateset */
144 extern bool
145 brcms_c_rate_hwrs_filter_sort_validate(struct brcms_rateset *rs,
146                                        const struct brcms_rateset *hw_rs,
147                                        bool check_brate, u8 txstreams);
148 /* copy rateset src to dst as-is (no masking or sorting) */
149 extern void brcms_c_rateset_copy(const struct brcms_rateset *src,
150                              struct brcms_rateset *dst);
151
152 /* would be nice to have these documented ... */
153 extern ratespec_t brcms_c_compute_rspec(struct d11rxhdr *rxh, u8 *plcp);
154
155 extern void brcms_c_rateset_filter(struct brcms_rateset *src,
156         struct brcms_rateset *dst, bool basic_only, u8 rates, uint xmask,
157         bool mcsallow);
158
159 extern void
160 brcms_c_rateset_default(struct brcms_rateset *rs_tgt,
161                         const struct brcms_rateset *rs_hw, uint phy_type,
162                         int bandtype, bool cck_only, uint rate_mask,
163                         bool mcsallow, u8 bw, u8 txstreams);
164
165 extern s16 brcms_c_rate_legacy_phyctl(uint rate);
166
167 extern void brcms_c_rateset_mcs_upd(struct brcms_rateset *rs, u8 txstreams);
168 extern void brcms_c_rateset_mcs_clear(struct brcms_rateset *rateset);
169 extern void brcms_c_rateset_mcs_build(struct brcms_rateset *rateset,
170                                       u8 txstreams);
171 extern void brcms_c_rateset_bw_mcs_filter(struct brcms_rateset *rateset, u8 bw);
172
173 #endif                          /* _BRCM_RATE_H_ */