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