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>
24 #include <bcmendian.h>
25 #include <proto/802.11.h>
34 #include <wlc_channel.h>
35 #include <wlc_bsscfg.h>
36 #include <wlc_event.h>
37 #include <wlc_mac80211.h>
39 #include <wl_export.h>
44 #define WLC_STF_SS_STBC_RX(wlc) (WLCISNPHY(wlc->band) && \
45 NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
47 static s8 wlc_stf_stbc_rx_get(struct wlc_info *wlc);
48 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val);
49 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 val);
50 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val);
51 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val);
53 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc);
54 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec);
60 const u8 txcore_default[5] = {
61 (0), /* bitmap of the core enabled */
62 (0x01), /* For Nsts = 1, enable core 1 */
63 (0x03), /* For Nsts = 2, enable core 1 & 2 */
64 (0x07), /* For Nsts = 3, enable core 1, 2 & 3 */
65 (0x0f) /* For Nsts = 4, enable all cores */
68 static void wlc_stf_stbc_rx_ht_update(struct wlc_info *wlc, int val)
70 ASSERT((val == HT_CAP_RX_STBC_NO)
71 || (val == HT_CAP_RX_STBC_ONE_STREAM));
73 /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
74 if (WLC_STF_SS_STBC_RX(wlc)) {
75 if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
79 wlc->ht_cap.cap &= ~HT_CAP_RX_STBC_MASK;
80 wlc->ht_cap.cap |= (val << HT_CAP_RX_STBC_SHIFT);
83 wlc_update_beacon(wlc);
84 wlc_update_probe_resp(wlc, true);
88 /* every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to turn on/off txchain */
89 void wlc_tempsense_upd(struct wlc_info *wlc)
91 wlc_phy_t *pi = wlc->band->pi;
92 uint active_chains, txchain;
94 /* Check if the chip is too hot. Disable one Tx chain, if it is */
95 /* high 4 bits are for Rx chain, low 4 bits are for Tx chain */
96 active_chains = wlc_phy_stf_chain_active_get(pi);
97 txchain = active_chains & 0xf;
99 if (wlc->stf->txchain == wlc->stf->hw_txchain) {
100 if (txchain && (txchain < wlc->stf->hw_txchain)) {
101 /* turn off 1 tx chain */
102 wlc_stf_txchain_set(wlc, txchain, true);
104 } else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
105 if (txchain == wlc->stf->hw_txchain) {
106 /* turn back on txchain */
107 wlc_stf_txchain_set(wlc, txchain, true);
113 wlc_stf_ss_algo_channel_get(struct wlc_info *wlc, u16 *ss_algo_channel,
117 u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
119 /* Clear previous settings */
120 *ss_algo_channel = 0;
123 *ss_algo_channel = (u16) -1;
127 wlc_phy_txpower_get_current(wlc->band->pi, &power,
128 CHSPEC_CHANNEL(chanspec));
130 siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
131 WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
132 cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
133 WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
134 stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
135 WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
137 /* criteria to choose stf mode */
139 /* the "+3dbm (12 0.25db units)" is to account for the fact that with CDD, tx occurs
142 if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
143 setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
145 setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
147 /* STBC is ORed into to algo channel as STBC requires per-packet SCB capability check
148 * so cannot be default mode of operation. One of SISO, CDD have to be set
150 if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
151 setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
154 static s8 wlc_stf_stbc_rx_get(struct wlc_info *wlc)
156 return (wlc->ht_cap.cap & HT_CAP_RX_STBC_MASK) >> HT_CAP_RX_STBC_SHIFT;
159 static bool wlc_stf_stbc_tx_set(struct wlc_info *wlc, s32 int_val)
161 if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON)) {
165 if ((int_val == ON) && (wlc->stf->txstreams == 1))
168 if ((int_val == OFF) || (wlc->stf->txstreams == 1)
169 || !WLC_STBC_CAP_PHY(wlc))
170 wlc->ht_cap.cap &= ~HT_CAP_TX_STBC;
172 wlc->ht_cap.cap |= HT_CAP_TX_STBC;
174 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
175 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
180 bool wlc_stf_stbc_rx_set(struct wlc_info *wlc, s32 int_val)
182 if ((int_val != HT_CAP_RX_STBC_NO)
183 && (int_val != HT_CAP_RX_STBC_ONE_STREAM)) {
187 if (WLC_STF_SS_STBC_RX(wlc)) {
188 if ((int_val != HT_CAP_RX_STBC_NO)
189 && (wlc->stf->rxstreams == 1))
193 wlc_stf_stbc_rx_ht_update(wlc, int_val);
197 static int wlc_stf_txcore_set(struct wlc_info *wlc, u8 Nsts, u8 core_mask)
199 WL_TRACE("wl%d: %s: Nsts %d core_mask %x\n",
200 wlc->pub->unit, __func__, Nsts, core_mask);
202 ASSERT((Nsts > 0) && (Nsts <= MAX_STREAMS_SUPPORTED));
204 if (WLC_BITSCNT(core_mask) > wlc->stf->txstreams) {
208 if ((WLC_BITSCNT(core_mask) == wlc->stf->txstreams) &&
209 ((core_mask & ~wlc->stf->txchain)
210 || !(core_mask & wlc->stf->txchain))) {
211 core_mask = wlc->stf->txchain;
214 ASSERT(!core_mask || Nsts <= WLC_BITSCNT(core_mask));
216 wlc->stf->txcore[Nsts] = core_mask;
217 /* Nsts = 1..4, txcore index = 1..4 */
219 /* Needs to update beacon and ucode generated response
220 * frames when 1 stream core map changed
222 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
223 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
225 wlc_suspend_mac_and_wait(wlc);
226 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
234 static int wlc_stf_spatial_policy_set(struct wlc_info *wlc, int val)
239 WL_TRACE("wl%d: %s: val %x\n", wlc->pub->unit, __func__, val);
241 wlc->stf->spatial_policy = (s8) val;
242 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
243 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
244 wlc->stf->txchain : txcore_default[i];
245 wlc_stf_txcore_set(wlc, (u8) i, core_mask);
250 int wlc_stf_txchain_set(struct wlc_info *wlc, s32 int_val, bool force)
252 u8 txchain = (u8) int_val;
256 if (wlc->stf->txchain == txchain)
259 if ((txchain & ~wlc->stf->hw_txchain)
260 || !(txchain & wlc->stf->hw_txchain))
263 /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
264 txstreams = (u8) WLC_BITSCNT(txchain);
265 if (txstreams > MAX_STREAMS_SUPPORTED)
268 if (txstreams == 1) {
269 for (i = 0; i < NBANDS(wlc); i++)
270 if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
272 || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
273 PHY_TXC1_MODE_SISO)) {
277 /* over-write the override rspec */
278 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
279 != PHY_TXC1_MODE_SISO) {
280 wlc->bandstate[i]->rspec_override = 0;
281 WL_ERROR("%s(): temp sense override non-SISO rspec_override\n",
285 (wlc->bandstate[i]->mrspec_override) !=
286 PHY_TXC1_MODE_SISO) {
287 wlc->bandstate[i]->mrspec_override = 0;
288 WL_ERROR("%s(): temp sense override non-SISO mrspec_override\n",
294 wlc->stf->txchain = txchain;
295 wlc->stf->txstreams = txstreams;
296 wlc_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
297 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
298 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
300 (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
301 _wlc_stf_phy_txant_upd(wlc);
303 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
306 for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
307 wlc_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
312 int wlc_stf_rxchain_set(struct wlc_info *wlc, s32 int_val)
315 u8 rxchain = (u8) int_val;
317 u8 old_rxchain, old_rxchain_cnt;
319 if (wlc->stf->rxchain == rxchain)
322 if ((rxchain & ~wlc->stf->hw_rxchain)
323 || !(rxchain & wlc->stf->hw_rxchain))
326 rxchain_cnt = (u8) WLC_BITSCNT(rxchain);
327 if (WLC_STF_SS_STBC_RX(wlc)) {
328 if ((rxchain_cnt == 1)
329 && (wlc_stf_stbc_rx_get(wlc) != HT_CAP_RX_STBC_NO))
333 if (APSTA_ENAB(wlc->pub) && (wlc->pub->associated))
334 return BCME_ASSOCIATED;
336 old_rxchain = wlc->stf->rxchain;
337 old_rxchain_cnt = wlc->stf->rxstreams;
339 wlc->stf->rxchain = rxchain;
340 wlc->stf->rxstreams = rxchain_cnt;
342 if (rxchain_cnt != old_rxchain_cnt) {
344 (rxchain_cnt == 1) ? HT_CAP_MIMO_PS_ON : HT_CAP_MIMO_PS_OFF;
345 wlc->mimops_PM = mimops_mode;
346 if (AP_ENAB(wlc->pub)) {
347 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
349 wlc_ht_mimops_cap_update(wlc, mimops_mode);
350 if (wlc->pub->associated)
351 wlc_mimops_action_ht_send(wlc, wlc->cfg,
355 if (wlc->pub->associated) {
356 if (mimops_mode == HT_CAP_MIMO_PS_OFF) {
357 /* if mimops is off, turn on the Rx chain first */
358 wlc_phy_stf_chain_set(wlc->band->pi,
361 wlc_ht_mimops_cap_update(wlc, mimops_mode);
364 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
366 wlc_ht_mimops_cap_update(wlc, mimops_mode);
368 } else if (old_rxchain != rxchain)
369 wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
375 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
376 int wlc_stf_ss_update(struct wlc_info *wlc, struct wlcband *band)
382 prev_stf_ss = wlc->stf->ss_opmode;
384 /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
385 if (WLC_STBC_CAP_PHY(wlc) &&
386 wlc->stf->ss_algosel_auto
387 && (wlc->stf->ss_algo_channel != (u16) -1)) {
388 ASSERT(isset(&wlc->stf->ss_algo_channel, PHY_TXC1_MODE_CDD)
389 || isset(&wlc->stf->ss_algo_channel,
390 PHY_TXC1_MODE_SISO));
391 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
392 || isset(&wlc->stf->ss_algo_channel,
393 PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
396 if (wlc->band != band)
398 upd_stf_ss = (wlc->stf->no_cddstbc
399 || (wlc->stf->txstreams ==
400 1)) ? PHY_TXC1_MODE_SISO : band->
403 if (prev_stf_ss != upd_stf_ss) {
404 wlc->stf->ss_opmode = upd_stf_ss;
405 wlc_bmac_band_stf_ss_set(wlc->hw, upd_stf_ss);
411 int wlc_stf_attach(struct wlc_info *wlc)
413 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
414 wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
416 if (WLCISNPHY(wlc->band) &&
417 (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
418 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
420 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
421 wlc_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
423 wlc_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
424 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
425 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
427 if (WLC_STBC_CAP_PHY(wlc)) {
428 wlc->stf->ss_algosel_auto = true;
429 wlc->stf->ss_algo_channel = (u16) -1; /* Init the default value */
434 void wlc_stf_detach(struct wlc_info *wlc)
438 int wlc_stf_ant_txant_validate(struct wlc_info *wlc, s8 val)
440 int bcmerror = BCME_OK;
442 /* when there is only 1 tx_streams, don't allow to change the txant */
443 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
444 return ((val == wlc->stf->txant) ? bcmerror : BCME_RANGE);
451 val = ANT_TX_FORCE_0;
454 val = ANT_TX_FORCE_1;
457 val = ANT_TX_LAST_RX;
460 bcmerror = BCME_RANGE;
464 if (bcmerror == BCME_OK)
465 wlc->stf->txant = (s8) val;
472 * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
475 * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
476 * achieve various tx/rx antenna selection schemes
478 * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
479 * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
480 * do tx-antenna selection for SISO transmissions
481 * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
482 * do tx-antenna selection for SISO transmissions
483 * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
485 static void _wlc_stf_phy_txant_upd(struct wlc_info *wlc)
489 txant = (s8) wlc->stf->txant;
490 ASSERT(txant == ANT_TX_FORCE_0 || txant == ANT_TX_FORCE_1
491 || txant == ANT_TX_LAST_RX);
493 if (WLC_PHY_11N_CAP(wlc->band)) {
494 if (txant == ANT_TX_FORCE_0) {
495 wlc->stf->phytxant = PHY_TXC_ANT_0;
496 } else if (txant == ANT_TX_FORCE_1) {
497 wlc->stf->phytxant = PHY_TXC_ANT_1;
499 if (WLCISNPHY(wlc->band) &&
500 NREV_GE(wlc->band->phyrev, 3)
501 && NREV_LT(wlc->band->phyrev, 7)) {
502 wlc->stf->phytxant = PHY_TXC_ANT_2;
505 if (WLCISLCNPHY(wlc->band) || WLCISSSLPNPHY(wlc->band))
506 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
508 /* keep this assert to catch out of sync wlc->stf->txcore */
509 ASSERT(wlc->stf->txchain > 0);
511 wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
515 if (txant == ANT_TX_FORCE_0)
516 wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
517 else if (txant == ANT_TX_FORCE_1)
518 wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
520 wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
523 wlc_bmac_txant_set(wlc->hw, wlc->stf->phytxant);
526 void wlc_stf_phy_txant_upd(struct wlc_info *wlc)
528 _wlc_stf_phy_txant_upd(wlc);
531 void wlc_stf_phy_chain_calc(struct wlc_info *wlc)
533 /* get available rx/tx chains */
534 wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
535 wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
537 /* these parameter are intended to be used for all PHY types */
538 if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
539 if (WLCISNPHY(wlc->band)) {
540 wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
542 wlc->stf->hw_txchain = TXCHAIN_DEF;
546 wlc->stf->txchain = wlc->stf->hw_txchain;
547 wlc->stf->txstreams = (u8) WLC_BITSCNT(wlc->stf->hw_txchain);
549 if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
550 if (WLCISNPHY(wlc->band)) {
551 wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
553 wlc->stf->hw_rxchain = RXCHAIN_DEF;
557 wlc->stf->rxchain = wlc->stf->hw_rxchain;
558 wlc->stf->rxstreams = (u8) WLC_BITSCNT(wlc->stf->hw_rxchain);
560 /* initialize the txcore table */
561 bcopy(txcore_default, wlc->stf->txcore, sizeof(wlc->stf->txcore));
563 /* default spatial_policy */
564 wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
565 wlc_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
568 static u16 _wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
570 u16 phytxant = wlc->stf->phytxant;
572 if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
573 ASSERT(wlc->stf->txstreams > 1);
574 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
575 } else if (wlc->stf->txant == ANT_TX_DEF)
576 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
577 phytxant &= PHY_TXC_ANT_MASK;
581 u16 wlc_stf_phytxchain_sel(struct wlc_info *wlc, ratespec_t rspec)
583 return _wlc_stf_phytxchain_sel(wlc, rspec);
586 u16 wlc_stf_d11hdrs_phyctl_txant(struct wlc_info *wlc, ratespec_t rspec)
588 u16 phytxant = wlc->stf->phytxant;
589 u16 mask = PHY_TXC_ANT_MASK;
591 /* for non-siso rates or default setting, use the available chains */
592 if (WLCISNPHY(wlc->band)) {
593 ASSERT(wlc->stf->txchain != 0);
594 phytxant = _wlc_stf_phytxchain_sel(wlc, rspec);
595 mask = PHY_TXC_HTANT_MASK;
597 phytxant |= phytxant & mask;