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