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