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