staging: brcm80211: remove nested include statements
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / brcmsmac / wlc_stf.c
blob6574fb4d693742b84fff424d3b3c1b3ccaf97e63
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <proto/802.11.h>
21 #include <proto/802.1d.h>
23 #include <bcmdefs.h>
24 #include <osl.h>
25 #include <bcmutils.h>
26 #include <siutils.h>
27 #include <wlioctl.h>
28 #include <bcmwifi.h>
29 #include <sbhnddma.h>
31 #include "wlc_types.h"
32 #include "sbhndpio.h"
33 #include "d11.h"
34 #include "wl_dbg.h"
35 #include "wlc_cfg.h"
36 #include "wlc_rate.h"
37 #include "wlc_scb.h"
38 #include "wlc_pub.h"
39 #include "wlc_key.h"
40 #include "phy/wlc_phy_hal.h"
41 #include "wlc_channel.h"
42 #include "wlc_bsscfg.h"
43 #include "wlc_mac80211.h"
44 #include "wl_export.h"
45 #include "wlc_bmac.h"
46 #include "wlc_stf.h"
48 #define MIN_SPATIAL_EXPANSION 0
49 #define MAX_SPATIAL_EXPANSION 1
51 #define WLC_STF_SS_STBC_RX(wlc) (WLCISNPHY(wlc->band) && \
52 NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
54 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val);
55 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 val);
56 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val);
57 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val);
59 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc);
60 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec);
62 #define NSTS_1 1
63 #define NSTS_2 2
64 #define NSTS_3 3
65 #define NSTS_4 4
66 const u8 txcore_default[5] = {
67 (0), /* bitmap of the core enabled */
68 (0x01), /* For Nsts = 1, enable core 1 */
69 (0x03), /* For Nsts = 2, enable core 1 & 2 */
70 (0x07), /* For Nsts = 3, enable core 1, 2 & 3 */
71 (0x0f) /* For Nsts = 4, enable all cores */
74 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val)
76 ASSERT((val == HT_CAP_RX_STBC_NO)
77 || (val == HT_CAP_RX_STBC_ONE_STREAM));
79 /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
80 if (WLC_STF_SS_STBC_RX(wlc)) {
81 if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
82 return;
85 wlc->ht_cap.cap_info &= ~HT_CAP_RX_STBC_MASK;
86 wlc->ht_cap.cap_info |= (val << HT_CAP_RX_STBC_SHIFT);
88 if (wlc->pub->up) {
89 wlc_update_beacon(wlc);
90 wlc_update_probe_resp(wlc, true);
94 /* every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to turn on/off txchain */
95 void wlc_tempsense_upd(struct wlc_info *wlc)
97 wlc_phy_t *pi = wlc->band->pi;
98 uint active_chains, txchain;
100 /* Check if the chip is too hot. Disable one Tx chain, if it is */
101 /* high 4 bits are for Rx chain, low 4 bits are for Tx chain */
102 active_chains = wlc_phy_stf_chain_active_get(pi);
103 txchain = active_chains & 0xf;
105 if (wlc->stf->txchain == wlc->stf->hw_txchain) {
106 if (txchain && (txchain < wlc->stf->hw_txchain)) {
107 /* turn off 1 tx chain */
108 wlc_stf_txchain_set(wlc, txchain, true);
110 } else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
111 if (txchain == wlc->stf->hw_txchain) {
112 /* turn back on txchain */
113 wlc_stf_txchain_set(wlc, txchain, true);
118 void
119 wlc_stf_ss_algo_channel_get(struct wlc_info *wlc, u16 *ss_algo_channel,
120 chanspec_t chanspec)
122 tx_power_t power;
123 u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
125 /* Clear previous settings */
126 *ss_algo_channel = 0;
128 if (!wlc->pub->up) {
129 *ss_algo_channel = (u16) -1;
130 return;
133 wlc_phy_txpower_get_current(wlc->band->pi, &power,
134 CHSPEC_CHANNEL(chanspec));
136 siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
137 WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
138 cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
139 WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
140 stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
141 WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
143 /* criteria to choose stf mode */
145 /* the "+3dbm (12 0.25db units)" is to account for the fact that with CDD, tx occurs
146 * on both chains
148 if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
149 setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
150 else
151 setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
153 /* STBC is ORed into to algo channel as STBC requires per-packet SCB capability check
154 * so cannot be default mode of operation. One of SISO, CDD have to be set
156 if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
157 setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
160 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val)
162 if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON)) {
163 return false;
166 if ((int_val == ON) && (wlc->stf->txstreams == 1))
167 return false;
169 if ((int_val == OFF) || (wlc->stf->txstreams == 1)
170 || !WLC_STBC_CAP_PHY(wlc))
171 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
172 else
173 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_TX_STBC;
175 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
176 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
178 return true;
181 bool wlc_stf_stbc_rx_set(struct wlc_info *wlc, s32 int_val)
183 if ((int_val != HT_CAP_RX_STBC_NO)
184 && (int_val != HT_CAP_RX_STBC_ONE_STREAM)) {
185 return false;
188 if (WLC_STF_SS_STBC_RX(wlc)) {
189 if ((int_val != HT_CAP_RX_STBC_NO)
190 && (wlc->stf->rxstreams == 1))
191 return false;
194 wlc_stf_stbc_rx_ht_update(wlc, int_val);
195 return true;
198 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 core_mask)
200 WL_TRACE("wl%d: %s: Nsts %d core_mask %x\n",
201 wlc->pub->unit, __func__, Nsts, core_mask);
203 ASSERT((Nsts > 0) && (Nsts <= MAX_STREAMS_SUPPORTED));
205 if (WLC_BITSCNT(core_mask) > wlc->stf->txstreams) {
206 core_mask = 0;
209 if ((WLC_BITSCNT(core_mask) == wlc->stf->txstreams) &&
210 ((core_mask & ~wlc->stf->txchain)
211 || !(core_mask & wlc->stf->txchain))) {
212 core_mask = wlc->stf->txchain;
215 ASSERT(!core_mask || Nsts <= WLC_BITSCNT(core_mask));
217 wlc->stf->txcore[Nsts] = core_mask;
218 /* Nsts = 1..4, txcore index = 1..4 */
219 if (Nsts == 1) {
220 /* Needs to update beacon and ucode generated response
221 * frames when 1 stream core map changed
223 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
224 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
225 if (wlc->clk) {
226 wlc_suspend_mac_and_wait(wlc);
227 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
228 wlc_enable_mac(wlc);
232 return BCME_OK;
235 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val)
237 int i;
238 u8 core_mask = 0;
240 WL_TRACE("wl%d: %s: val %x\n", wlc->pub->unit, __func__, val);
242 wlc->stf->spatial_policy = (s8) val;
243 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
244 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
245 wlc->stf->txchain : txcore_default[i];
246 wlc_stf_txcore_set(wlc, (u8) i, core_mask);
248 return BCME_OK;
251 int wlc_stf_txchain_set(struct wlc_info *wlc, s32 int_val, bool force)
253 u8 txchain = (u8) int_val;
254 u8 txstreams;
255 uint i;
257 if (wlc->stf->txchain == txchain)
258 return BCME_OK;
260 if ((txchain & ~wlc->stf->hw_txchain)
261 || !(txchain & wlc->stf->hw_txchain))
262 return BCME_RANGE;
264 /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
265 txstreams = (u8) WLC_BITSCNT(txchain);
266 if (txstreams > MAX_STREAMS_SUPPORTED)
267 return BCME_RANGE;
269 if (txstreams == 1) {
270 for (i = 0; i < NBANDS(wlc); i++)
271 if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
272 PHY_TXC1_MODE_SISO)
273 || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
274 PHY_TXC1_MODE_SISO)) {
275 if (!force)
276 return BCME_ERROR;
278 /* over-write the override rspec */
279 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
280 != PHY_TXC1_MODE_SISO) {
281 wlc->bandstate[i]->rspec_override = 0;
282 WL_ERROR("%s(): temp sense override non-SISO rspec_override\n",
283 __func__);
285 if (RSPEC_STF
286 (wlc->bandstate[i]->mrspec_override) !=
287 PHY_TXC1_MODE_SISO) {
288 wlc->bandstate[i]->mrspec_override = 0;
289 WL_ERROR("%s(): temp sense override non-SISO mrspec_override\n",
290 __func__);
295 wlc->stf->txchain = txchain;
296 wlc->stf->txstreams = txstreams;
297 wlc_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
298 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
299 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
300 wlc->stf->txant =
301 (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
302 _wlc_stf_phy_txant_upd(wlc);
304 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
305 wlc->stf->rxchain);
307 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
308 wlc_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
310 return BCME_OK;
313 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
314 int wlc_stf_ss_update(struct wlc_info *wlc, struct wlcband *band)
316 int ret_code = 0;
317 u8 prev_stf_ss;
318 u8 upd_stf_ss;
320 prev_stf_ss = wlc->stf->ss_opmode;
322 /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
323 if (WLC_STBC_CAP_PHY(wlc) &&
324 wlc->stf->ss_algosel_auto
325 && (wlc->stf->ss_algo_channel != (u16) -1)) {
326 ASSERT(isset(&wlc->stf->ss_algo_channel, PHY_TXC1_MODE_CDD)
327 || isset(&wlc->stf->ss_algo_channel,
328 PHY_TXC1_MODE_SISO));
329 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
330 || isset(&wlc->stf->ss_algo_channel,
331 PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
332 : PHY_TXC1_MODE_CDD;
333 } else {
334 if (wlc->band != band)
335 return ret_code;
336 upd_stf_ss = (wlc->stf->no_cddstbc
337 || (wlc->stf->txstreams ==
338 1)) ? PHY_TXC1_MODE_SISO : band->
339 band_stf_ss_mode;
341 if (prev_stf_ss != upd_stf_ss) {
342 wlc->stf->ss_opmode = upd_stf_ss;
343 wlc_bmac_band_stf_ss_set(wlc->hw, upd_stf_ss);
346 return ret_code;
349 int wlc_stf_attach(struct wlc_info *wlc)
351 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
352 wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
354 if (WLCISNPHY(wlc->band) &&
355 (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
356 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
357 PHY_TXC1_MODE_CDD;
358 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
359 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
361 wlc_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
362 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
363 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
365 if (WLC_STBC_CAP_PHY(wlc)) {
366 wlc->stf->ss_algosel_auto = true;
367 wlc->stf->ss_algo_channel = (u16) -1; /* Init the default value */
369 return 0;
372 void wlc_stf_detach(struct wlc_info *wlc)
376 int wlc_stf_ant_txant_validate(struct wlc_info *wlc, s8 val)
378 int bcmerror = BCME_OK;
380 /* when there is only 1 tx_streams, don't allow to change the txant */
381 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
382 return ((val == wlc->stf->txant) ? bcmerror : BCME_RANGE);
384 switch (val) {
385 case -1:
386 val = ANT_TX_DEF;
387 break;
388 case 0:
389 val = ANT_TX_FORCE_0;
390 break;
391 case 1:
392 val = ANT_TX_FORCE_1;
393 break;
394 case 3:
395 val = ANT_TX_LAST_RX;
396 break;
397 default:
398 bcmerror = BCME_RANGE;
399 break;
402 if (bcmerror == BCME_OK)
403 wlc->stf->txant = (s8) val;
405 return bcmerror;
410 * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
411 * change
413 * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
414 * achieve various tx/rx antenna selection schemes
416 * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
417 * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
418 * do tx-antenna selection for SISO transmissions
419 * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
420 * do tx-antenna selection for SISO transmissions
421 * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
423 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc)
425 s8 txant;
427 txant = (s8) wlc->stf->txant;
428 ASSERT(txant == ANT_TX_FORCE_0 || txant == ANT_TX_FORCE_1
429 || txant == ANT_TX_LAST_RX);
431 if (WLC_PHY_11N_CAP(wlc->band)) {
432 if (txant == ANT_TX_FORCE_0) {
433 wlc->stf->phytxant = PHY_TXC_ANT_0;
434 } else if (txant == ANT_TX_FORCE_1) {
435 wlc->stf->phytxant = PHY_TXC_ANT_1;
437 if (WLCISNPHY(wlc->band) &&
438 NREV_GE(wlc->band->phyrev, 3)
439 && NREV_LT(wlc->band->phyrev, 7)) {
440 wlc->stf->phytxant = PHY_TXC_ANT_2;
442 } else {
443 if (WLCISLCNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band))
444 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
445 else {
446 /* keep this assert to catch out of sync wlc->stf->txcore */
447 ASSERT(wlc->stf->txchain > 0);
448 wlc->stf->phytxant =
449 wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
452 } else {
453 if (txant == ANT_TX_FORCE_0)
454 wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
455 else if (txant == ANT_TX_FORCE_1)
456 wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
457 else
458 wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
461 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
464 void wlc_stf_phy_txant_upd(struct wlc_info *wlc)
466 _wlc_stf_phy_txant_upd(wlc);
469 void wlc_stf_phy_chain_calc(struct wlc_info *wlc)
471 /* get available rx/tx chains */
472 wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
473 wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
475 /* these parameter are intended to be used for all PHY types */
476 if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
477 if (WLCISNPHY(wlc->band)) {
478 wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
479 } else {
480 wlc->stf->hw_txchain = TXCHAIN_DEF;
484 wlc->stf->txchain = wlc->stf->hw_txchain;
485 wlc->stf->txstreams = (u8) WLC_BITSCNT(wlc->stf->hw_txchain);
487 if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
488 if (WLCISNPHY(wlc->band)) {
489 wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
490 } else {
491 wlc->stf->hw_rxchain = RXCHAIN_DEF;
495 wlc->stf->rxchain = wlc->stf->hw_rxchain;
496 wlc->stf->rxstreams = (u8) WLC_BITSCNT(wlc->stf->hw_rxchain);
498 /* initialize the txcore table */
499 memcpy(wlc->stf->txcore, txcore_default, sizeof(wlc->stf->txcore));
501 /* default spatial_policy */
502 wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
503 wlc_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
506 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
508 u16 phytxant = wlc->stf->phytxant;
510 if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
511 ASSERT(wlc->stf->txstreams > 1);
512 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
513 } else if (wlc->stf->txant == ANT_TX_DEF)
514 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
515 phytxant &= PHY_TXC_ANT_MASK;
516 return phytxant;
519 u16 wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
521 return _wlc_stf_phytxchain_sel(wlc, rspec);
524 u16 wlc_stf_d11hdrs_phyctl_txant(struct wlc_info *wlc, ratespec_t rspec)
526 u16 phytxant = wlc->stf->phytxant;
527 u16 mask = PHY_TXC_ANT_MASK;
529 /* for non-siso rates or default setting, use the available chains */
530 if (WLCISNPHY(wlc->band)) {
531 ASSERT(wlc->stf->txchain != 0);
532 phytxant = _wlc_stf_phytxchain_sel(wlc, rspec);
533 mask = PHY_TXC_HTANT_MASK;
535 phytxant |= phytxant & mask;
536 return phytxant;