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