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.
19 #include <linux/kernel.h>
25 #include <bcmendian.h>
26 #include <proto/802.11.h>
33 #include <wlc_channel.h>
34 #include <wlc_bsscfg.h>
35 #include <wlc_mac80211.h>
37 #include <wl_export.h>
41 #define WLC_STF_SS_STBC_RX(wlc) (WLCISNPHY(wlc->band) && \
42 NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
44 static s8 wlc_stf_stbc_rx_get(wlc_info_t *wlc);
45 static bool wlc_stf_stbc_tx_set(wlc_info_t *wlc, s32 int_val);
46 static int wlc_stf_txcore_set(wlc_info_t *wlc, u8 Nsts, u8 val);
47 static int wlc_stf_spatial_policy_set(wlc_info_t *wlc, int val);
48 static void wlc_stf_stbc_rx_ht_update(wlc_info_t *wlc, int val);
50 static void _wlc_stf_phy_txant_upd(wlc_info_t *wlc);
51 static u16 _wlc_stf_phytxchain_sel(wlc_info_t *wlc, ratespec_t rspec);
57 const u8 txcore_default[5] = {
58 (0), /* bitmap of the core enabled */
59 (0x01), /* For Nsts = 1, enable core 1 */
60 (0x03), /* For Nsts = 2, enable core 1 & 2 */
61 (0x07), /* For Nsts = 3, enable core 1, 2 & 3 */
62 (0x0f) /* For Nsts = 4, enable all cores */
65 static void wlc_stf_stbc_rx_ht_update(wlc_info_t *wlc, int val)
67 ASSERT((val == HT_CAP_RX_STBC_NO)
68 || (val == HT_CAP_RX_STBC_ONE_STREAM));
70 /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
71 if (WLC_STF_SS_STBC_RX(wlc)) {
72 if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
76 wlc->ht_cap.cap &= ~HT_CAP_RX_STBC_MASK;
77 wlc->ht_cap.cap |= (val << HT_CAP_RX_STBC_SHIFT);
80 wlc_update_beacon(wlc);
81 wlc_update_probe_resp(wlc, true);
85 /* every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to turn on/off txchain */
86 void wlc_tempsense_upd(wlc_info_t *wlc)
88 wlc_phy_t *pi = wlc->band->pi;
89 uint active_chains, txchain;
91 /* Check if the chip is too hot. Disable one Tx chain, if it is */
92 /* high 4 bits are for Rx chain, low 4 bits are for Tx chain */
93 active_chains = wlc_phy_stf_chain_active_get(pi);
94 txchain = active_chains & 0xf;
96 if (wlc->stf->txchain == wlc->stf->hw_txchain) {
97 if (txchain && (txchain < wlc->stf->hw_txchain)) {
98 /* turn off 1 tx chain */
99 wlc_stf_txchain_set(wlc, txchain, true);
101 } else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
102 if (txchain == wlc->stf->hw_txchain) {
103 /* turn back on txchain */
104 wlc_stf_txchain_set(wlc, txchain, true);
110 wlc_stf_ss_algo_channel_get(wlc_info_t *wlc, u16 *ss_algo_channel,
114 u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
116 /* Clear previous settings */
117 *ss_algo_channel = 0;
120 *ss_algo_channel = (u16) -1;
124 wlc_phy_txpower_get_current(wlc->band->pi, &power,
125 CHSPEC_CHANNEL(chanspec));
127 siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
128 WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
129 cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
130 WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
131 stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
132 WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
134 /* criteria to choose stf mode */
136 /* the "+3dbm (12 0.25db units)" is to account for the fact that with CDD, tx occurs
139 if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
140 setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
142 setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
144 /* STBC is ORed into to algo channel as STBC requires per-packet SCB capability check
145 * so cannot be default mode of operation. One of SISO, CDD have to be set
147 if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
148 setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
151 static s8 wlc_stf_stbc_rx_get(wlc_info_t *wlc)
153 return (wlc->ht_cap.cap & HT_CAP_RX_STBC_MASK) >> HT_CAP_RX_STBC_SHIFT;
156 static bool wlc_stf_stbc_tx_set(wlc_info_t *wlc, s32 int_val)
158 if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON)) {
162 if ((int_val == ON) && (wlc->stf->txstreams == 1))
165 if ((int_val == OFF) || (wlc->stf->txstreams == 1)
166 || !WLC_STBC_CAP_PHY(wlc))
167 wlc->ht_cap.cap &= ~HT_CAP_TX_STBC;
169 wlc->ht_cap.cap |= HT_CAP_TX_STBC;
171 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
172 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
177 bool wlc_stf_stbc_rx_set(wlc_info_t *wlc, s32 int_val)
179 if ((int_val != HT_CAP_RX_STBC_NO)
180 && (int_val != HT_CAP_RX_STBC_ONE_STREAM)) {
184 if (WLC_STF_SS_STBC_RX(wlc)) {
185 if ((int_val != HT_CAP_RX_STBC_NO)
186 && (wlc->stf->rxstreams == 1))
190 wlc_stf_stbc_rx_ht_update(wlc, int_val);
194 static int wlc_stf_txcore_set(wlc_info_t *wlc, u8 Nsts, u8 core_mask)
196 WL_TRACE(("wl%d: %s: Nsts %d core_mask %x\n",
197 wlc->pub->unit, __func__, Nsts, core_mask));
199 ASSERT((Nsts > 0) && (Nsts <= MAX_STREAMS_SUPPORTED));
201 if (WLC_BITSCNT(core_mask) > wlc->stf->txstreams) {
205 if ((WLC_BITSCNT(core_mask) == wlc->stf->txstreams) &&
206 ((core_mask & ~wlc->stf->txchain)
207 || !(core_mask & wlc->stf->txchain))) {
208 core_mask = wlc->stf->txchain;
211 ASSERT(!core_mask || Nsts <= WLC_BITSCNT(core_mask));
213 wlc->stf->txcore[Nsts] = core_mask;
214 /* Nsts = 1..4, txcore index = 1..4 */
216 /* Needs to update beacon and ucode generated response
217 * frames when 1 stream core map changed
219 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
220 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
222 wlc_suspend_mac_and_wait(wlc);
223 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
231 static int wlc_stf_spatial_policy_set(wlc_info_t *wlc, int val)
236 WL_TRACE(("wl%d: %s: val %x\n", wlc->pub->unit, __func__, val));
238 wlc->stf->spatial_policy = (s8) val;
239 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
240 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
241 wlc->stf->txchain : txcore_default[i];
242 wlc_stf_txcore_set(wlc, (u8) i, core_mask);
247 int wlc_stf_txchain_set(wlc_info_t *wlc, s32 int_val, bool force)
249 u8 txchain = (u8) int_val;
253 if (wlc->stf->txchain == txchain)
256 if ((txchain & ~wlc->stf->hw_txchain)
257 || !(txchain & wlc->stf->hw_txchain))
260 /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
261 txstreams = (u8) WLC_BITSCNT(txchain);
262 if (txstreams > MAX_STREAMS_SUPPORTED)
265 if (txstreams == 1) {
266 for (i = 0; i < NBANDS(wlc); i++)
267 if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
269 || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
270 PHY_TXC1_MODE_SISO)) {
274 /* over-write the override rspec */
275 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
276 != PHY_TXC1_MODE_SISO) {
277 wlc->bandstate[i]->rspec_override = 0;
278 WL_ERROR(("%s(): temp sense override non-SISO" " rspec_override.\n", __func__));
281 (wlc->bandstate[i]->mrspec_override) !=
282 PHY_TXC1_MODE_SISO) {
283 wlc->bandstate[i]->mrspec_override = 0;
284 WL_ERROR(("%s(): temp sense override non-SISO" " mrspec_override.\n", __func__));
289 wlc->stf->txchain = txchain;
290 wlc->stf->txstreams = txstreams;
291 wlc_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
292 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
293 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
295 (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
296 _wlc_stf_phy_txant_upd(wlc);
298 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
301 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
302 wlc_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
307 int wlc_stf_rxchain_set(wlc_info_t *wlc, s32 int_val)
310 u8 rxchain = (u8) int_val;
312 u8 old_rxchain, old_rxchain_cnt;
314 if (wlc->stf->rxchain == rxchain)
317 if ((rxchain & ~wlc->stf->hw_rxchain)
318 || !(rxchain & wlc->stf->hw_rxchain))
321 rxchain_cnt = (u8) WLC_BITSCNT(rxchain);
322 if (WLC_STF_SS_STBC_RX(wlc)) {
323 if ((rxchain_cnt == 1)
324 && (wlc_stf_stbc_rx_get(wlc) != HT_CAP_RX_STBC_NO))
328 if (APSTA_ENAB(wlc->pub) && (wlc->pub->associated))
329 return BCME_ASSOCIATED;
331 old_rxchain = wlc->stf->rxchain;
332 old_rxchain_cnt = wlc->stf->rxstreams;
334 wlc->stf->rxchain = rxchain;
335 wlc->stf->rxstreams = rxchain_cnt;
337 if (rxchain_cnt != old_rxchain_cnt) {
339 (rxchain_cnt == 1) ? HT_CAP_MIMO_PS_ON : HT_CAP_MIMO_PS_OFF;
340 wlc->mimops_PM = mimops_mode;
341 if (AP_ENAB(wlc->pub)) {
342 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
344 wlc_ht_mimops_cap_update(wlc, mimops_mode);
345 if (wlc->pub->associated)
346 wlc_mimops_action_ht_send(wlc, wlc->cfg,
350 if (wlc->pub->associated) {
351 if (mimops_mode == HT_CAP_MIMO_PS_OFF) {
352 /* if mimops is off, turn on the Rx chain first */
353 wlc_phy_stf_chain_set(wlc->band->pi,
356 wlc_ht_mimops_cap_update(wlc, mimops_mode);
359 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
361 wlc_ht_mimops_cap_update(wlc, mimops_mode);
363 } else if (old_rxchain != rxchain)
364 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
370 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
371 int wlc_stf_ss_update(wlc_info_t *wlc, wlcband_t *band)
377 prev_stf_ss = wlc->stf->ss_opmode;
379 /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
380 if (WLC_STBC_CAP_PHY(wlc) &&
381 wlc->stf->ss_algosel_auto
382 && (wlc->stf->ss_algo_channel != (u16) -1)) {
383 ASSERT(isset(&wlc->stf->ss_algo_channel, PHY_TXC1_MODE_CDD)
384 || isset(&wlc->stf->ss_algo_channel,
385 PHY_TXC1_MODE_SISO));
386 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
387 || isset(&wlc->stf->ss_algo_channel,
388 PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
391 if (wlc->band != band)
393 upd_stf_ss = (wlc->stf->no_cddstbc
394 || (wlc->stf->txstreams ==
395 1)) ? PHY_TXC1_MODE_SISO : band->
398 if (prev_stf_ss != upd_stf_ss) {
399 wlc->stf->ss_opmode = upd_stf_ss;
400 wlc_bmac_band_stf_ss_set(wlc->hw, upd_stf_ss);
406 int wlc_stf_attach(wlc_info_t *wlc)
408 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
409 wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
411 if (WLCISNPHY(wlc->band) &&
412 (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
413 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
415 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
416 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
418 wlc_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
419 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
420 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
422 if (WLC_STBC_CAP_PHY(wlc)) {
423 wlc->stf->ss_algosel_auto = true;
424 wlc->stf->ss_algo_channel = (u16) -1; /* Init the default value */
429 void wlc_stf_detach(wlc_info_t *wlc)
433 int wlc_stf_ant_txant_validate(wlc_info_t *wlc, s8 val)
435 int bcmerror = BCME_OK;
437 /* when there is only 1 tx_streams, don't allow to change the txant */
438 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
439 return ((val == wlc->stf->txant) ? bcmerror : BCME_RANGE);
446 val = ANT_TX_FORCE_0;
449 val = ANT_TX_FORCE_1;
452 val = ANT_TX_LAST_RX;
455 bcmerror = BCME_RANGE;
459 if (bcmerror == BCME_OK)
460 wlc->stf->txant = (s8) val;
467 * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
470 * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
471 * achieve various tx/rx antenna selection schemes
473 * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
474 * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
475 * do tx-antenna selection for SISO transmissions
476 * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
477 * do tx-antenna selection for SISO transmissions
478 * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
480 static void _wlc_stf_phy_txant_upd(wlc_info_t *wlc)
484 txant = (s8) wlc->stf->txant;
485 ASSERT(txant == ANT_TX_FORCE_0 || txant == ANT_TX_FORCE_1
486 || txant == ANT_TX_LAST_RX);
488 if (WLC_PHY_11N_CAP(wlc->band)) {
489 if (txant == ANT_TX_FORCE_0) {
490 wlc->stf->phytxant = PHY_TXC_ANT_0;
491 } else if (txant == ANT_TX_FORCE_1) {
492 wlc->stf->phytxant = PHY_TXC_ANT_1;
494 if (WLCISNPHY(wlc->band) &&
495 NREV_GE(wlc->band->phyrev, 3)
496 && NREV_LT(wlc->band->phyrev, 7)) {
497 wlc->stf->phytxant = PHY_TXC_ANT_2;
500 if (WLCISLCNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band))
501 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
503 /* keep this assert to catch out of sync wlc->stf->txcore */
504 ASSERT(wlc->stf->txchain > 0);
506 wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
510 if (txant == ANT_TX_FORCE_0)
511 wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
512 else if (txant == ANT_TX_FORCE_1)
513 wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
515 wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
518 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
521 void wlc_stf_phy_txant_upd(wlc_info_t *wlc)
523 _wlc_stf_phy_txant_upd(wlc);
526 void wlc_stf_phy_chain_calc(wlc_info_t *wlc)
528 /* get available rx/tx chains */
529 wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
530 wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
532 /* these parameter are intended to be used for all PHY types */
533 if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
534 if (WLCISNPHY(wlc->band)) {
535 wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
537 wlc->stf->hw_txchain = TXCHAIN_DEF;
541 wlc->stf->txchain = wlc->stf->hw_txchain;
542 wlc->stf->txstreams = (u8) WLC_BITSCNT(wlc->stf->hw_txchain);
544 if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
545 if (WLCISNPHY(wlc->band)) {
546 wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
548 wlc->stf->hw_rxchain = RXCHAIN_DEF;
552 wlc->stf->rxchain = wlc->stf->hw_rxchain;
553 wlc->stf->rxstreams = (u8) WLC_BITSCNT(wlc->stf->hw_rxchain);
555 /* initialize the txcore table */
556 bcopy(txcore_default, wlc->stf->txcore, sizeof(wlc->stf->txcore));
558 /* default spatial_policy */
559 wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
560 wlc_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
563 static u16 _wlc_stf_phytxchain_sel(wlc_info_t *wlc, ratespec_t rspec)
565 u16 phytxant = wlc->stf->phytxant;
567 if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
568 ASSERT(wlc->stf->txstreams > 1);
569 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
570 } else if (wlc->stf->txant == ANT_TX_DEF)
571 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
572 phytxant &= PHY_TXC_ANT_MASK;
576 u16 wlc_stf_phytxchain_sel(wlc_info_t *wlc, ratespec_t rspec)
578 return _wlc_stf_phytxchain_sel(wlc, rspec);
581 u16 wlc_stf_d11hdrs_phyctl_txant(wlc_info_t *wlc, ratespec_t rspec)
583 u16 phytxant = wlc->stf->phytxant;
584 u16 mask = PHY_TXC_ANT_MASK;
586 /* for non-siso rates or default setting, use the available chains */
587 if (WLCISNPHY(wlc->band)) {
588 ASSERT(wlc->stf->txchain != 0);
589 phytxant = _wlc_stf_phytxchain_sel(wlc, rspec);
590 mask = PHY_TXC_HTANT_MASK;
592 phytxant |= phytxant & mask;