staging: brcm80211: replaced bmac acronym by brcms_b_
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / stf.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
17 #include <net/mac80211.h>
18
19 #include "types.h"
20 #include "d11.h"
21 #include "rate.h"
22 #include "phy/phy_hal.h"
23 #include "channel.h"
24 #include "main.h"
25 #include "bmac.h"
26 #include "stf.h"
27
28 #define MIN_SPATIAL_EXPANSION   0
29 #define MAX_SPATIAL_EXPANSION   1
30
31 #define WLC_STF_SS_STBC_RX(wlc) (WLCISNPHY(wlc->band) && \
32         NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
33
34 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val);
35 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 val);
36 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val);
37 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val);
38
39 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc);
40 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec);
41
42 #define NSTS_1  1
43 #define NSTS_2  2
44 #define NSTS_3  3
45 #define NSTS_4  4
46 const u8 txcore_default[5] = {
47         (0),                    /* bitmap of the core enabled */
48         (0x01),                 /* For Nsts = 1, enable core 1 */
49         (0x03),                 /* For Nsts = 2, enable core 1 & 2 */
50         (0x07),                 /* For Nsts = 3, enable core 1, 2 & 3 */
51         (0x0f)                  /* For Nsts = 4, enable all cores */
52 };
53
54 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val)
55 {
56         /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
57         if (WLC_STF_SS_STBC_RX(wlc)) {
58                 if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
59                         return;
60         }
61
62         wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_RX_STBC;
63         wlc->ht_cap.cap_info |= (val << IEEE80211_HT_CAP_RX_STBC_SHIFT);
64
65         if (wlc->pub->up) {
66                 wlc_update_beacon(wlc);
67                 wlc_update_probe_resp(wlc, true);
68         }
69 }
70
71 /* every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to turn on/off txchain */
72 void wlc_tempsense_upd(struct wlc_info *wlc)
73 {
74         wlc_phy_t *pi = wlc->band->pi;
75         uint active_chains, txchain;
76
77         /* Check if the chip is too hot. Disable one Tx chain, if it is */
78         /* high 4 bits are for Rx chain, low 4 bits are  for Tx chain */
79         active_chains = wlc_phy_stf_chain_active_get(pi);
80         txchain = active_chains & 0xf;
81
82         if (wlc->stf->txchain == wlc->stf->hw_txchain) {
83                 if (txchain && (txchain < wlc->stf->hw_txchain)) {
84                         /* turn off 1 tx chain */
85                         wlc_stf_txchain_set(wlc, txchain, true);
86                 }
87         } else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
88                 if (txchain == wlc->stf->hw_txchain) {
89                         /* turn back on txchain */
90                         wlc_stf_txchain_set(wlc, txchain, true);
91                 }
92         }
93 }
94
95 void
96 wlc_stf_ss_algo_channel_get(struct wlc_info *wlc, u16 *ss_algo_channel,
97                             chanspec_t chanspec)
98 {
99         tx_power_t power;
100         u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
101
102         /* Clear previous settings */
103         *ss_algo_channel = 0;
104
105         if (!wlc->pub->up) {
106                 *ss_algo_channel = (u16) -1;
107                 return;
108         }
109
110         wlc_phy_txpower_get_current(wlc->band->pi, &power,
111                                     CHSPEC_CHANNEL(chanspec));
112
113         siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
114             WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
115         cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
116             WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
117         stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
118             WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
119
120         /* criteria to choose stf mode */
121
122         /* the "+3dbm (12 0.25db units)" is to account for the fact that with CDD, tx occurs
123          * on both chains
124          */
125         if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
126                 setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
127         else
128                 setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
129
130         /* STBC is ORed into to algo channel as STBC requires per-packet SCB capability check
131          * so cannot be default mode of operation. One of SISO, CDD have to be set
132          */
133         if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
134                 setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
135 }
136
137 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val)
138 {
139         if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON)) {
140                 return false;
141         }
142
143         if ((int_val == ON) && (wlc->stf->txstreams == 1))
144                 return false;
145
146         if ((int_val == OFF) || (wlc->stf->txstreams == 1)
147             || !WLC_STBC_CAP_PHY(wlc))
148                 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
149         else
150                 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_TX_STBC;
151
152         wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
153         wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
154
155         return true;
156 }
157
158 bool wlc_stf_stbc_rx_set(struct wlc_info *wlc, s32 int_val)
159 {
160         if ((int_val != HT_CAP_RX_STBC_NO)
161             && (int_val != HT_CAP_RX_STBC_ONE_STREAM)) {
162                 return false;
163         }
164
165         if (WLC_STF_SS_STBC_RX(wlc)) {
166                 if ((int_val != HT_CAP_RX_STBC_NO)
167                     && (wlc->stf->rxstreams == 1))
168                         return false;
169         }
170
171         wlc_stf_stbc_rx_ht_update(wlc, int_val);
172         return true;
173 }
174
175 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 core_mask)
176 {
177         BCMMSG(wlc->wiphy, "wl%d: Nsts %d core_mask %x\n",
178                  wlc->pub->unit, Nsts, core_mask);
179
180         if (WLC_BITSCNT(core_mask) > wlc->stf->txstreams) {
181                 core_mask = 0;
182         }
183
184         if ((WLC_BITSCNT(core_mask) == wlc->stf->txstreams) &&
185             ((core_mask & ~wlc->stf->txchain)
186              || !(core_mask & wlc->stf->txchain))) {
187                 core_mask = wlc->stf->txchain;
188         }
189
190         wlc->stf->txcore[Nsts] = core_mask;
191         /* Nsts = 1..4, txcore index = 1..4 */
192         if (Nsts == 1) {
193                 /* Needs to update beacon and ucode generated response
194                  * frames when 1 stream core map changed
195                  */
196                 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
197                 brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
198                 if (wlc->clk) {
199                         wlc_suspend_mac_and_wait(wlc);
200                         wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
201                         wlc_enable_mac(wlc);
202                 }
203         }
204
205         return 0;
206 }
207
208 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val)
209 {
210         int i;
211         u8 core_mask = 0;
212
213         BCMMSG(wlc->wiphy, "wl%d: val %x\n", wlc->pub->unit, val);
214
215         wlc->stf->spatial_policy = (s8) val;
216         for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
217                 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
218                     wlc->stf->txchain : txcore_default[i];
219                 wlc_stf_txcore_set(wlc, (u8) i, core_mask);
220         }
221         return 0;
222 }
223
224 int wlc_stf_txchain_set(struct wlc_info *wlc, s32 int_val, bool force)
225 {
226         u8 txchain = (u8) int_val;
227         u8 txstreams;
228         uint i;
229
230         if (wlc->stf->txchain == txchain)
231                 return 0;
232
233         if ((txchain & ~wlc->stf->hw_txchain)
234             || !(txchain & wlc->stf->hw_txchain))
235                 return -EINVAL;
236
237         /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
238         txstreams = (u8) WLC_BITSCNT(txchain);
239         if (txstreams > MAX_STREAMS_SUPPORTED)
240                 return -EINVAL;
241
242         if (txstreams == 1) {
243                 for (i = 0; i < NBANDS(wlc); i++)
244                         if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
245                              PHY_TXC1_MODE_SISO)
246                             || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
247                                 PHY_TXC1_MODE_SISO)) {
248                                 if (!force)
249                                         return -EBADE;
250
251                                 /* over-write the override rspec */
252                                 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
253                                     != PHY_TXC1_MODE_SISO) {
254                                         wlc->bandstate[i]->rspec_override = 0;
255                                         wiphy_err(wlc->wiphy, "%s(): temp "
256                                                   "sense override non-SISO "
257                                                   "rspec_override\n",
258                                                   __func__);
259                                 }
260                                 if (RSPEC_STF
261                                     (wlc->bandstate[i]->mrspec_override) !=
262                                     PHY_TXC1_MODE_SISO) {
263                                         wlc->bandstate[i]->mrspec_override = 0;
264                                         wiphy_err(wlc->wiphy, "%s(): temp "
265                                                   "sense override non-SISO "
266                                                   "mrspec_override\n",
267                                                   __func__);
268                                 }
269                         }
270         }
271
272         wlc->stf->txchain = txchain;
273         wlc->stf->txstreams = txstreams;
274         wlc_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
275         wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
276         wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
277         wlc->stf->txant =
278             (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
279         _wlc_stf_phy_txant_upd(wlc);
280
281         wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
282                               wlc->stf->rxchain);
283
284         for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
285                 wlc_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
286
287         return 0;
288 }
289
290 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
291 int wlc_stf_ss_update(struct wlc_info *wlc, struct wlcband *band)
292 {
293         int ret_code = 0;
294         u8 prev_stf_ss;
295         u8 upd_stf_ss;
296
297         prev_stf_ss = wlc->stf->ss_opmode;
298
299         /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
300         if (WLC_STBC_CAP_PHY(wlc) &&
301             wlc->stf->ss_algosel_auto
302             && (wlc->stf->ss_algo_channel != (u16) -1)) {
303                 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
304                               || isset(&wlc->stf->ss_algo_channel,
305                                        PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
306                     : PHY_TXC1_MODE_CDD;
307         } else {
308                 if (wlc->band != band)
309                         return ret_code;
310                 upd_stf_ss = (wlc->stf->no_cddstbc
311                               || (wlc->stf->txstreams ==
312                                   1)) ? PHY_TXC1_MODE_SISO : band->
313                     band_stf_ss_mode;
314         }
315         if (prev_stf_ss != upd_stf_ss) {
316                 wlc->stf->ss_opmode = upd_stf_ss;
317                 brcms_b_band_stf_ss_set(wlc->hw, upd_stf_ss);
318         }
319
320         return ret_code;
321 }
322
323 int wlc_stf_attach(struct wlc_info *wlc)
324 {
325         wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
326         wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
327
328         if (WLCISNPHY(wlc->band) &&
329             (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
330                 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
331                     PHY_TXC1_MODE_CDD;
332         wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
333         wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
334
335         wlc_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
336         wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
337         wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
338
339         if (WLC_STBC_CAP_PHY(wlc)) {
340                 wlc->stf->ss_algosel_auto = true;
341                 wlc->stf->ss_algo_channel = (u16) -1;   /* Init the default value */
342         }
343         return 0;
344 }
345
346 void wlc_stf_detach(struct wlc_info *wlc)
347 {
348 }
349
350 /*
351  * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
352  *  change
353  *
354  * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
355  *   achieve various tx/rx antenna selection schemes
356  *
357  * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
358  * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
359  *    do tx-antenna selection for SISO transmissions
360  * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
361  *    do tx-antenna selection for SISO transmissions
362  * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
363 */
364 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc)
365 {
366         s8 txant;
367
368         txant = (s8) wlc->stf->txant;
369         if (WLC_PHY_11N_CAP(wlc->band)) {
370                 if (txant == ANT_TX_FORCE_0) {
371                         wlc->stf->phytxant = PHY_TXC_ANT_0;
372                 } else if (txant == ANT_TX_FORCE_1) {
373                         wlc->stf->phytxant = PHY_TXC_ANT_1;
374
375                         if (WLCISNPHY(wlc->band) &&
376                             NREV_GE(wlc->band->phyrev, 3)
377                             && NREV_LT(wlc->band->phyrev, 7)) {
378                                 wlc->stf->phytxant = PHY_TXC_ANT_2;
379                         }
380                 } else {
381                         if (WLCISLCNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band))
382                                 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
383                         else {
384                                 /* catch out of sync wlc->stf->txcore */
385                                 WARN_ON(wlc->stf->txchain <= 0);
386                                 wlc->stf->phytxant =
387                                     wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
388                         }
389                 }
390         } else {
391                 if (txant == ANT_TX_FORCE_0)
392                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
393                 else if (txant == ANT_TX_FORCE_1)
394                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
395                 else
396                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
397         }
398
399         brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
400 }
401
402 void wlc_stf_phy_txant_upd(struct wlc_info *wlc)
403 {
404         _wlc_stf_phy_txant_upd(wlc);
405 }
406
407 void wlc_stf_phy_chain_calc(struct wlc_info *wlc)
408 {
409         /* get available rx/tx chains */
410         wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
411         wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
412
413         /* these parameter are intended to be used for all PHY types */
414         if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
415                 if (WLCISNPHY(wlc->band)) {
416                         wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
417                 } else {
418                         wlc->stf->hw_txchain = TXCHAIN_DEF;
419                 }
420         }
421
422         wlc->stf->txchain = wlc->stf->hw_txchain;
423         wlc->stf->txstreams = (u8) WLC_BITSCNT(wlc->stf->hw_txchain);
424
425         if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
426                 if (WLCISNPHY(wlc->band)) {
427                         wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
428                 } else {
429                         wlc->stf->hw_rxchain = RXCHAIN_DEF;
430                 }
431         }
432
433         wlc->stf->rxchain = wlc->stf->hw_rxchain;
434         wlc->stf->rxstreams = (u8) WLC_BITSCNT(wlc->stf->hw_rxchain);
435
436         /* initialize the txcore table */
437         memcpy(wlc->stf->txcore, txcore_default, sizeof(wlc->stf->txcore));
438
439         /* default spatial_policy */
440         wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
441         wlc_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
442 }
443
444 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
445 {
446         u16 phytxant = wlc->stf->phytxant;
447
448         if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
449                 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
450         } else if (wlc->stf->txant == ANT_TX_DEF)
451                 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
452         phytxant &= PHY_TXC_ANT_MASK;
453         return phytxant;
454 }
455
456 u16 wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
457 {
458         return _wlc_stf_phytxchain_sel(wlc, rspec);
459 }
460
461 u16 wlc_stf_d11hdrs_phyctl_txant(struct wlc_info *wlc, ratespec_t rspec)
462 {
463         u16 phytxant = wlc->stf->phytxant;
464         u16 mask = PHY_TXC_ANT_MASK;
465
466         /* for non-siso rates or default setting, use the available chains */
467         if (WLCISNPHY(wlc->band)) {
468                 phytxant = _wlc_stf_phytxchain_sel(wlc, rspec);
469                 mask = PHY_TXC_HTANT_MASK;
470         }
471         phytxant |= phytxant & mask;
472         return phytxant;
473 }