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