Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.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 #include "d11.h"
21 #include "rate.h"
22 #include "phy/phy_hal.h"
23 #include "channel.h"
24 #include "main.h"
25 #include "bmac.h"
26 #include "stf.h"
27
28 #define MIN_SPATIAL_EXPANSION   0
29 #define MAX_SPATIAL_EXPANSION   1
30
31 #define BRCMS_STF_SS_STBC_RX(wlc) (BRCMS_ISNPHY(wlc->band) && \
32         NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
33
34 static bool brcms_c_stf_stbc_tx_set(struct brcms_c_info *wlc, s32 int_val);
35 static int brcms_c_stf_txcore_set(struct brcms_c_info *wlc, u8 Nsts, u8 val);
36 static int brcms_c_stf_spatial_policy_set(struct brcms_c_info *wlc, int val);
37 static void brcms_c_stf_stbc_rx_ht_update(struct brcms_c_info *wlc, int val);
38
39 static void _brcms_c_stf_phy_txant_upd(struct brcms_c_info *wlc);
40 static u16 _brcms_c_stf_phytxchain_sel(struct brcms_c_info *wlc,
41                                        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 brcms_c_stf_stbc_rx_ht_update(struct brcms_c_info *wlc, int val)
56 {
57         /* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
58         if (BRCMS_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                 brcms_c_update_beacon(wlc);
68                 brcms_c_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 brcms_c_tempsense_upd(struct brcms_c_info *wlc)
74 {
75         struct brcms_phy_pub *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                         brcms_c_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                         brcms_c_stf_txchain_set(wlc, txchain, true);
92                 }
93         }
94 }
95
96 void
97 brcms_c_stf_ss_algo_channel_get(struct brcms_c_info *wlc, u16 *ss_algo_channel,
98                             chanspec_t chanspec)
99 {
100         struct tx_power 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 brcms_c_stf_stbc_tx_set(struct brcms_c_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             || !BRCMS_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 brcms_c_stf_stbc_rx_set(struct brcms_c_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 (BRCMS_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         brcms_c_stf_stbc_rx_ht_update(wlc, int_val);
173         return true;
174 }
175
176 static int brcms_c_stf_txcore_set(struct brcms_c_info *wlc, u8 Nsts,
177                                   u8 core_mask)
178 {
179         BCMMSG(wlc->wiphy, "wl%d: Nsts %d core_mask %x\n",
180                  wlc->pub->unit, Nsts, core_mask);
181
182         if (BRCMS_BITSCNT(core_mask) > wlc->stf->txstreams) {
183                 core_mask = 0;
184         }
185
186         if ((BRCMS_BITSCNT(core_mask) == wlc->stf->txstreams) &&
187             ((core_mask & ~wlc->stf->txchain)
188              || !(core_mask & wlc->stf->txchain))) {
189                 core_mask = wlc->stf->txchain;
190         }
191
192         wlc->stf->txcore[Nsts] = core_mask;
193         /* Nsts = 1..4, txcore index = 1..4 */
194         if (Nsts == 1) {
195                 /* Needs to update beacon and ucode generated response
196                  * frames when 1 stream core map changed
197                  */
198                 wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
199                 brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
200                 if (wlc->clk) {
201                         brcms_c_suspend_mac_and_wait(wlc);
202                         brcms_c_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
203                         brcms_c_enable_mac(wlc);
204                 }
205         }
206
207         return 0;
208 }
209
210 static int brcms_c_stf_spatial_policy_set(struct brcms_c_info *wlc, int val)
211 {
212         int i;
213         u8 core_mask = 0;
214
215         BCMMSG(wlc->wiphy, "wl%d: val %x\n", wlc->pub->unit, val);
216
217         wlc->stf->spatial_policy = (s8) val;
218         for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
219                 core_mask = (val == MAX_SPATIAL_EXPANSION) ?
220                     wlc->stf->txchain : txcore_default[i];
221                 brcms_c_stf_txcore_set(wlc, (u8) i, core_mask);
222         }
223         return 0;
224 }
225
226 int brcms_c_stf_txchain_set(struct brcms_c_info *wlc, s32 int_val, bool force)
227 {
228         u8 txchain = (u8) int_val;
229         u8 txstreams;
230         uint i;
231
232         if (wlc->stf->txchain == txchain)
233                 return 0;
234
235         if ((txchain & ~wlc->stf->hw_txchain)
236             || !(txchain & wlc->stf->hw_txchain))
237                 return -EINVAL;
238
239         /* if nrate override is configured to be non-SISO STF mode, reject reducing txchain to 1 */
240         txstreams = (u8) BRCMS_BITSCNT(txchain);
241         if (txstreams > MAX_STREAMS_SUPPORTED)
242                 return -EINVAL;
243
244         if (txstreams == 1) {
245                 for (i = 0; i < NBANDS(wlc); i++)
246                         if ((RSPEC_STF(wlc->bandstate[i]->rspec_override) !=
247                              PHY_TXC1_MODE_SISO)
248                             || (RSPEC_STF(wlc->bandstate[i]->mrspec_override) !=
249                                 PHY_TXC1_MODE_SISO)) {
250                                 if (!force)
251                                         return -EBADE;
252
253                                 /* over-write the override rspec */
254                                 if (RSPEC_STF(wlc->bandstate[i]->rspec_override)
255                                     != PHY_TXC1_MODE_SISO) {
256                                         wlc->bandstate[i]->rspec_override = 0;
257                                         wiphy_err(wlc->wiphy, "%s(): temp "
258                                                   "sense override non-SISO "
259                                                   "rspec_override\n",
260                                                   __func__);
261                                 }
262                                 if (RSPEC_STF
263                                     (wlc->bandstate[i]->mrspec_override) !=
264                                     PHY_TXC1_MODE_SISO) {
265                                         wlc->bandstate[i]->mrspec_override = 0;
266                                         wiphy_err(wlc->wiphy, "%s(): temp "
267                                                   "sense override non-SISO "
268                                                   "mrspec_override\n",
269                                                   __func__);
270                                 }
271                         }
272         }
273
274         wlc->stf->txchain = txchain;
275         wlc->stf->txstreams = txstreams;
276         brcms_c_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
277         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
278         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
279         wlc->stf->txant =
280             (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
281         _brcms_c_stf_phy_txant_upd(wlc);
282
283         wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
284                               wlc->stf->rxchain);
285
286         for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
287                 brcms_c_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
288
289         return 0;
290 }
291
292 /* update wlc->stf->ss_opmode which represents the operational stf_ss mode we're using */
293 int brcms_c_stf_ss_update(struct brcms_c_info *wlc, struct brcms_band *band)
294 {
295         int ret_code = 0;
296         u8 prev_stf_ss;
297         u8 upd_stf_ss;
298
299         prev_stf_ss = wlc->stf->ss_opmode;
300
301         /* NOTE: opmode can only be SISO or CDD as STBC is decided on a per-packet basis */
302         if (BRCMS_STBC_CAP_PHY(wlc) &&
303             wlc->stf->ss_algosel_auto
304             && (wlc->stf->ss_algo_channel != (u16) -1)) {
305                 upd_stf_ss = (wlc->stf->no_cddstbc || (wlc->stf->txstreams == 1)
306                               || isset(&wlc->stf->ss_algo_channel,
307                                        PHY_TXC1_MODE_SISO)) ? PHY_TXC1_MODE_SISO
308                     : PHY_TXC1_MODE_CDD;
309         } else {
310                 if (wlc->band != band)
311                         return ret_code;
312                 upd_stf_ss = (wlc->stf->no_cddstbc
313                               || (wlc->stf->txstreams ==
314                                   1)) ? PHY_TXC1_MODE_SISO : band->
315                     band_stf_ss_mode;
316         }
317         if (prev_stf_ss != upd_stf_ss) {
318                 wlc->stf->ss_opmode = upd_stf_ss;
319                 brcms_b_band_stf_ss_set(wlc->hw, upd_stf_ss);
320         }
321
322         return ret_code;
323 }
324
325 int brcms_c_stf_attach(struct brcms_c_info *wlc)
326 {
327         wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
328         wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
329
330         if (BRCMS_ISNPHY(wlc->band) &&
331             (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
332                 wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
333                     PHY_TXC1_MODE_CDD;
334         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
335         brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
336
337         brcms_c_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
338         wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
339         wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
340
341         if (BRCMS_STBC_CAP_PHY(wlc)) {
342                 wlc->stf->ss_algosel_auto = true;
343                 wlc->stf->ss_algo_channel = (u16) -1;   /* Init the default value */
344         }
345         return 0;
346 }
347
348 void brcms_c_stf_detach(struct brcms_c_info *wlc)
349 {
350 }
351
352 /*
353  * Centralized txant update function. call it whenever wlc->stf->txant and/or wlc->stf->txchain
354  *  change
355  *
356  * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
357  *   achieve various tx/rx antenna selection schemes
358  *
359  * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means auto(last rx)
360  * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7 means last rx and
361  *    do tx-antenna selection for SISO transmissions
362  * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7 means last rx and
363  *    do tx-antenna selection for SISO transmissions
364  * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7 means both cores active
365 */
366 static void _brcms_c_stf_phy_txant_upd(struct brcms_c_info *wlc)
367 {
368         s8 txant;
369
370         txant = (s8) wlc->stf->txant;
371         if (BRCMS_PHY_11N_CAP(wlc->band)) {
372                 if (txant == ANT_TX_FORCE_0) {
373                         wlc->stf->phytxant = PHY_TXC_ANT_0;
374                 } else if (txant == ANT_TX_FORCE_1) {
375                         wlc->stf->phytxant = PHY_TXC_ANT_1;
376
377                         if (BRCMS_ISNPHY(wlc->band) &&
378                             NREV_GE(wlc->band->phyrev, 3)
379                             && NREV_LT(wlc->band->phyrev, 7)) {
380                                 wlc->stf->phytxant = PHY_TXC_ANT_2;
381                         }
382                 } else {
383                         if (BRCMS_ISLCNPHY(wlc->band) ||
384                             BRCMS_ISSSLPNPHY(wlc->band))
385                                 wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
386                         else {
387                                 /* catch out of sync wlc->stf->txcore */
388                                 WARN_ON(wlc->stf->txchain <= 0);
389                                 wlc->stf->phytxant =
390                                     wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
391                         }
392                 }
393         } else {
394                 if (txant == ANT_TX_FORCE_0)
395                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
396                 else if (txant == ANT_TX_FORCE_1)
397                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
398                 else
399                         wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
400         }
401
402         brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
403 }
404
405 void brcms_c_stf_phy_txant_upd(struct brcms_c_info *wlc)
406 {
407         _brcms_c_stf_phy_txant_upd(wlc);
408 }
409
410 void brcms_c_stf_phy_chain_calc(struct brcms_c_info *wlc)
411 {
412         /* get available rx/tx chains */
413         wlc->stf->hw_txchain = (u8) getintvar(wlc->pub->vars, "txchain");
414         wlc->stf->hw_rxchain = (u8) getintvar(wlc->pub->vars, "rxchain");
415
416         /* these parameter are intended to be used for all PHY types */
417         if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
418                 if (BRCMS_ISNPHY(wlc->band)) {
419                         wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
420                 } else {
421                         wlc->stf->hw_txchain = TXCHAIN_DEF;
422                 }
423         }
424
425         wlc->stf->txchain = wlc->stf->hw_txchain;
426         wlc->stf->txstreams = (u8) BRCMS_BITSCNT(wlc->stf->hw_txchain);
427
428         if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
429                 if (BRCMS_ISNPHY(wlc->band)) {
430                         wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
431                 } else {
432                         wlc->stf->hw_rxchain = RXCHAIN_DEF;
433                 }
434         }
435
436         wlc->stf->rxchain = wlc->stf->hw_rxchain;
437         wlc->stf->rxstreams = (u8) BRCMS_BITSCNT(wlc->stf->hw_rxchain);
438
439         /* initialize the txcore table */
440         memcpy(wlc->stf->txcore, txcore_default, sizeof(wlc->stf->txcore));
441
442         /* default spatial_policy */
443         wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
444         brcms_c_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
445 }
446
447 static u16 _brcms_c_stf_phytxchain_sel(struct brcms_c_info *wlc,
448                                        ratespec_t rspec)
449 {
450         u16 phytxant = wlc->stf->phytxant;
451
452         if (RSPEC_STF(rspec) != PHY_TXC1_MODE_SISO) {
453                 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
454         } else if (wlc->stf->txant == ANT_TX_DEF)
455                 phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
456         phytxant &= PHY_TXC_ANT_MASK;
457         return phytxant;
458 }
459
460 u16 brcms_c_stf_phytxchain_sel(struct brcms_c_info *wlc, ratespec_t rspec)
461 {
462         return _brcms_c_stf_phytxchain_sel(wlc, rspec);
463 }
464
465 u16 brcms_c_stf_d11hdrs_phyctl_txant(struct brcms_c_info *wlc, ratespec_t rspec)
466 {
467         u16 phytxant = wlc->stf->phytxant;
468         u16 mask = PHY_TXC_ANT_MASK;
469
470         /* for non-siso rates or default setting, use the available chains */
471         if (BRCMS_ISNPHY(wlc->band)) {
472                 phytxant = _brcms_c_stf_phytxchain_sel(wlc, rspec);
473                 mask = PHY_TXC_HTANT_MASK;
474         }
475         phytxant |= phytxant & mask;
476         return phytxant;
477 }