]> Pileus Git - ~andy/linux/blob - drivers/staging/brcm80211/brcmsmac/main.c
staging: brcm80211: removed synchronisation of wlc->machwcap field
[~andy/linux] / drivers / staging / brcm80211 / brcmsmac / main.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/pci_ids.h>
18 #include <linux/if_ether.h>
19 #include <net/mac80211.h>
20 #include <brcm_hw_ids.h>
21 #include <aiutils.h>
22 #include <chipcommon.h>
23 #include "rate.h"
24 #include "scb.h"
25 #include "phy/phy_hal.h"
26 #include "channel.h"
27 #include "antsel.h"
28 #include "stf.h"
29 #include "ampdu.h"
30 #include "mac80211_if.h"
31 #include "ucode_loader.h"
32 #include "main.h"
33
34 /*
35  * Indication for txflowcontrol that all priority bits in
36  * TXQ_STOP_FOR_PRIOFC_MASK are to be considered.
37  */
38 #define ALLPRIO         -1
39
40 /*
41  * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
42  */
43 #define SSID_FMT_BUF_LEN        ((4 * IEEE80211_MAX_SSID_LEN) + 1)
44
45 /* watchdog timer, in unit of ms */
46 #define TIMER_INTERVAL_WATCHDOG 1000
47 /* radio monitor timer, in unit of ms */
48 #define TIMER_INTERVAL_RADIOCHK 800
49
50 /* Max MPC timeout, in unit of watchdog */
51 #ifndef BRCMS_MPC_MAX_DELAYCNT
52 #define BRCMS_MPC_MAX_DELAYCNT  10
53 #endif
54
55 /* Min MPC timeout, in unit of watchdog */
56 #define BRCMS_MPC_MIN_DELAYCNT  1
57 #define BRCMS_MPC_THRESHOLD     3       /* MPC count threshold level */
58
59 /* beacon interval, in unit of 1024TU */
60 #define BEACON_INTERVAL_DEFAULT 100
61 /* DTIM interval, in unit of beacon interval */
62 #define DTIM_INTERVAL_DEFAULT   3
63
64 /* Scale down delays to accommodate QT slow speed */
65 /* beacon interval, in unit of 1024TU */
66 #define BEACON_INTERVAL_DEF_QT  20
67 /* DTIM interval, in unit of beacon interval */
68 #define DTIM_INTERVAL_DEF_QT    1
69
70 #define TBTT_ALIGN_LEEWAY_US    100     /* min leeway before first TBTT in us */
71
72 /* n-mode support capability */
73 /* 2x2 includes both 1x1 & 2x2 devices
74  * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
75  * control it independently
76  */
77 #define WL_11N_2x2                      1
78 #define WL_11N_3x3                      3
79 #define WL_11N_4x4                      4
80
81 /* define 11n feature disable flags */
82 #define WLFEATURE_DISABLE_11N           0x00000001
83 #define WLFEATURE_DISABLE_11N_STBC_TX   0x00000002
84 #define WLFEATURE_DISABLE_11N_STBC_RX   0x00000004
85 #define WLFEATURE_DISABLE_11N_SGI_TX    0x00000008
86 #define WLFEATURE_DISABLE_11N_SGI_RX    0x00000010
87 #define WLFEATURE_DISABLE_11N_AMPDU_TX  0x00000020
88 #define WLFEATURE_DISABLE_11N_AMPDU_RX  0x00000040
89 #define WLFEATURE_DISABLE_11N_GF        0x00000080
90
91 #define EDCF_ACI_MASK                0x60
92 #define EDCF_ACI_SHIFT               5
93 #define EDCF_ECWMIN_MASK             0x0f
94 #define EDCF_ECWMAX_SHIFT            4
95 #define EDCF_AIFSN_MASK              0x0f
96 #define EDCF_AIFSN_MAX               15
97 #define EDCF_ECWMAX_MASK             0xf0
98
99 #define EDCF_AC_BE_TXOP_STA          0x0000
100 #define EDCF_AC_BK_TXOP_STA          0x0000
101 #define EDCF_AC_VO_ACI_STA           0x62
102 #define EDCF_AC_VO_ECW_STA           0x32
103 #define EDCF_AC_VI_ACI_STA           0x42
104 #define EDCF_AC_VI_ECW_STA           0x43
105 #define EDCF_AC_BK_ECW_STA           0xA4
106 #define EDCF_AC_VI_TXOP_STA          0x005e
107 #define EDCF_AC_VO_TXOP_STA          0x002f
108 #define EDCF_AC_BE_ACI_STA           0x03
109 #define EDCF_AC_BE_ECW_STA           0xA4
110 #define EDCF_AC_BK_ACI_STA           0x27
111 #define EDCF_AC_VO_TXOP_AP           0x002f
112
113 #define EDCF_TXOP2USEC(txop)         ((txop) << 5)
114 #define EDCF_ECW2CW(exp)             ((1 << (exp)) - 1)
115
116 #define APHY_SYMBOL_TIME        4
117 #define APHY_PREAMBLE_TIME      16
118 #define APHY_SIGNAL_TIME        4
119 #define APHY_SIFS_TIME          16
120 #define APHY_SERVICE_NBITS      16
121 #define APHY_TAIL_NBITS         6
122 #define BPHY_SIFS_TIME          10
123 #define BPHY_PLCP_SHORT_TIME    96
124
125 #define PREN_PREAMBLE           24
126 #define PREN_MM_EXT             12
127 #define PREN_PREAMBLE_EXT       4
128
129 #define DOT11_MAC_HDR_LEN               24
130 #define DOT11_ACK_LEN           10
131 #define DOT11_BA_LEN            4
132 #define DOT11_OFDM_SIGNAL_EXTENSION     6
133 #define DOT11_MIN_FRAG_LEN              256
134 #define DOT11_RTS_LEN           16
135 #define DOT11_CTS_LEN           10
136 #define DOT11_BA_BITMAP_LEN             128
137 #define DOT11_MIN_BEACON_PERIOD         1
138 #define DOT11_MAX_BEACON_PERIOD         0xFFFF
139 #define DOT11_MAXNUMFRAGS       16
140 #define DOT11_MAX_FRAG_LEN              2346
141
142 #define BPHY_PLCP_TIME          192
143 #define RIFS_11N_TIME           2
144
145 #define WME_VER                 1
146 #define WME_SUBTYPE_PARAM_IE    1
147 #define WME_TYPE                2
148 #define WME_OUI                 "\x00\x50\xf2"
149
150 #define AC_BE                   0
151 #define AC_BK                   1
152 #define AC_VI                   2
153 #define AC_VO                   3
154
155 #define BCN_TMPL_LEN            512     /* length of the BCN template area */
156
157 /* brcms_bss_info flag bit values */
158 #define BRCMS_BSS_HT            0x0020  /* BSS is HT (MIMO) capable */
159
160 /* Flags used in brcms_c_txq_info.stopped */
161 /* per prio flow control bits */
162 #define TXQ_STOP_FOR_PRIOFC_MASK        0x000000FF
163 /* stop txq enqueue for packet drain */
164 #define TXQ_STOP_FOR_PKT_DRAIN          0x00000100
165 /* stop txq enqueue for ampdu flow control */
166 #define TXQ_STOP_FOR_AMPDU_FLOW_CNTRL   0x00000200
167
168 #define BRCMS_HWRXOFF           38      /* chip rx buffer offset */
169
170 /* Find basic rate for a given rate */
171 static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
172 {
173         if (is_mcs_rate(rspec))
174                 return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
175                        .leg_ofdm];
176         return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
177 }
178
179 static u16 frametype(u32 rspec, u8 mimoframe)
180 {
181         if (is_mcs_rate(rspec))
182                 return mimoframe;
183         return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
184 }
185
186 /* rfdisable delay timer 500 ms, runs of ALP clock */
187 #define RFDISABLE_DEFAULT       10000000
188
189 #define BRCMS_TEMPSENSE_PERIOD          10      /* 10 second timeout */
190
191 /* precedences numbers for wlc queues. These are twice as may levels as
192  * 802.1D priorities.
193  * Odd numbers are used for HI priority traffic at same precedence levels
194  * These constants are used ONLY by wlc_prio2prec_map.  Do not use them
195  * elsewhere.
196  */
197 #define _BRCMS_PREC_NONE                0       /* None = - */
198 #define _BRCMS_PREC_BK          2       /* BK - Background */
199 #define _BRCMS_PREC_BE          4       /* BE - Best-effort */
200 #define _BRCMS_PREC_EE          6       /* EE - Excellent-effort */
201 #define _BRCMS_PREC_CL          8       /* CL - Controlled Load */
202 #define _BRCMS_PREC_VI          10      /* Vi - Video */
203 #define _BRCMS_PREC_VO          12      /* Vo - Voice */
204 #define _BRCMS_PREC_NC          14      /* NC - Network Control */
205
206 /* The BSS is generating beacons in HW */
207 #define BRCMS_BSSCFG_HW_BCN     0x20
208
209 #define SYNTHPU_DLY_APHY_US     3700    /* a phy synthpu_dly time in us */
210 #define SYNTHPU_DLY_BPHY_US     1050    /* b/g phy synthpu_dly time in us */
211 #define SYNTHPU_DLY_NPHY_US     2048    /* n phy REV3 synthpu_dly time in us */
212 #define SYNTHPU_DLY_LPPHY_US    300     /* lpphy synthpu_dly time in us */
213
214 #define SYNTHPU_DLY_PHY_US_QT   100     /* QT synthpu_dly time in us */
215
216 #define ANTCNT                  10      /* vanilla M_MAX_ANTCNT value */
217
218 /* Per-AC retry limit register definitions; uses defs.h bitfield macros */
219 #define EDCF_SHORT_S            0
220 #define EDCF_SFB_S              4
221 #define EDCF_LONG_S             8
222 #define EDCF_LFB_S              12
223 #define EDCF_SHORT_M            BITFIELD_MASK(4)
224 #define EDCF_SFB_M              BITFIELD_MASK(4)
225 #define EDCF_LONG_M             BITFIELD_MASK(4)
226 #define EDCF_LFB_M              BITFIELD_MASK(4)
227
228 #define RETRY_SHORT_DEF                 7       /* Default Short retry Limit */
229 #define RETRY_SHORT_MAX                 255     /* Maximum Short retry Limit */
230 #define RETRY_LONG_DEF                  4       /* Default Long retry count */
231 #define RETRY_SHORT_FB                  3 /* Short count for fallback rate */
232 #define RETRY_LONG_FB                   2 /* Long count for fallback rate */
233
234 #define APHY_CWMIN              15
235 #define PHY_CWMAX               1023
236
237 #define EDCF_AIFSN_MIN               1
238
239 #define FRAGNUM_MASK            0xF
240
241 #define APHY_SLOT_TIME          9
242 #define BPHY_SLOT_TIME          20
243
244 #define WL_SPURAVOID_OFF        0
245 #define WL_SPURAVOID_ON1        1
246 #define WL_SPURAVOID_ON2        2
247
248 /* invalid core flags, use the saved coreflags */
249 #define BRCMS_USE_COREFLAGS     0xffffffff
250
251 /* values for PLCPHdr_override */
252 #define BRCMS_PLCP_AUTO -1
253 #define BRCMS_PLCP_SHORT        0
254 #define BRCMS_PLCP_LONG 1
255
256 /* values for g_protection_override and n_protection_override */
257 #define BRCMS_PROTECTION_AUTO           -1
258 #define BRCMS_PROTECTION_OFF            0
259 #define BRCMS_PROTECTION_ON             1
260 #define BRCMS_PROTECTION_MMHDR_ONLY     2
261 #define BRCMS_PROTECTION_CTS_ONLY               3
262
263 /* values for g_protection_control and n_protection_control */
264 #define BRCMS_PROTECTION_CTL_OFF                0
265 #define BRCMS_PROTECTION_CTL_LOCAL      1
266 #define BRCMS_PROTECTION_CTL_OVERLAP    2
267
268 /* values for n_protection */
269 #define BRCMS_N_PROTECTION_OFF          0
270 #define BRCMS_N_PROTECTION_OPTIONAL     1
271 #define BRCMS_N_PROTECTION_20IN40               2
272 #define BRCMS_N_PROTECTION_MIXEDMODE    3
273
274 /* values for band specific 40MHz capabilities */
275 #define BRCMS_N_BW_20ALL                        0
276 #define BRCMS_N_BW_40ALL                        1
277 #define BRCMS_N_BW_20IN2G_40IN5G                2
278
279 /* bitflags for SGI support (sgi_rx iovar) */
280 #define BRCMS_N_SGI_20                  0x01
281 #define BRCMS_N_SGI_40                  0x02
282
283 /* defines used by the nrate iovar */
284 /* MSC in use,indicates b0-6 holds an mcs */
285 #define NRATE_MCS_INUSE 0x00000080
286 /* rate/mcs value */
287 #define NRATE_RATE_MASK 0x0000007f
288 /* stf mode mask: siso, cdd, stbc, sdm */
289 #define NRATE_STF_MASK  0x0000ff00
290 /* stf mode shift */
291 #define NRATE_STF_SHIFT 8
292 /* bit indicates override both rate & mode */
293 #define NRATE_OVERRIDE  0x80000000
294 /* bit indicate to override mcs only */
295 #define NRATE_OVERRIDE_MCS_ONLY 0x40000000
296 #define NRATE_SGI_MASK  0x00800000      /* sgi mode */
297 #define NRATE_SGI_SHIFT 23      /* sgi mode */
298 #define NRATE_LDPC_CODING 0x00400000    /* bit indicates adv coding in use */
299 #define NRATE_LDPC_SHIFT 22     /* ldpc shift */
300
301 #define NRATE_STF_SISO  0       /* stf mode SISO */
302 #define NRATE_STF_CDD   1       /* stf mode CDD */
303 #define NRATE_STF_STBC  2       /* stf mode STBC */
304 #define NRATE_STF_SDM   3       /* stf mode SDM */
305
306 #define MAX_DMA_SEGS 4
307
308 /* Max # of entries in Tx FIFO based on 4kb page size */
309 #define NTXD            256
310 /* Max # of entries in Rx FIFO based on 4kb page size */
311 #define NRXD            256
312
313 /* try to keep this # rbufs posted to the chip */
314 #define NRXBUFPOST      32
315
316 /* data msg txq hiwat mark */
317 #define BRCMS_DATAHIWAT         50
318
319 /* bounded rx loops */
320 #define RXBND           8 /* max # frames to process in brcms_c_recv() */
321 #define TXSBND          8 /* max # tx status to process in wlc_txstatus() */
322
323 /*
324  * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
325  */
326 #define SSID_FMT_BUF_LEN        ((4 * IEEE80211_MAX_SSID_LEN) + 1)
327
328 /*
329  * The following table lists the buffer memory allocated to xmt fifos in HW.
330  * the size is in units of 256bytes(one block), total size is HW dependent
331  * ucode has default fifo partition, sw can overwrite if necessary
332  *
333  * This is documented in twiki under the topic UcodeTxFifo. Please ensure
334  * the twiki is updated before making changes.
335  */
336
337 /* Starting corerev for the fifo size table */
338 #define XMTFIFOTBL_STARTREV     20
339
340 struct d11init {
341         __le16 addr;
342         __le16 size;
343         __le32 value;
344 };
345
346 /* currently the best mechanism for determining SIFS is the band in use */
347 static u16 get_sifs(struct brcms_band *band)
348 {
349         return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
350                                  BPHY_SIFS_TIME;
351 }
352
353
354 /*
355  * Detect Card removed.
356  * Even checking an sbconfig register read will not false trigger when the core
357  * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
358  * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
359  * reg with fixed 0/1 pattern (some platforms return all 0).
360  * If clocks are present, call the sb routine which will figure out if the
361  * device is removed.
362  */
363 static bool brcms_deviceremoved(struct brcms_c_info *wlc)
364 {
365         if (!wlc->hw->clk)
366                 return ai_deviceremoved(wlc->hw->sih);
367         return (R_REG(&wlc->hw->regs->maccontrol) &
368                 (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
369 }
370
371 /* sum the individual fifo tx pending packet counts */
372 static s16 brcms_txpktpendtot(struct brcms_c_info *wlc)
373 {
374         return wlc->core->txpktpend[0] + wlc->core->txpktpend[1] +
375                wlc->core->txpktpend[2] + wlc->core->txpktpend[3];
376 }
377
378 static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
379 {
380         return wlc->pub->_nbands > 1 && !wlc->bandlocked;
381 }
382
383 static int brcms_chspec_bw(u16 chanspec)
384 {
385         if (CHSPEC_IS40(chanspec))
386                 return BRCMS_40_MHZ;
387         if (CHSPEC_IS20(chanspec))
388                 return BRCMS_20_MHZ;
389
390         return BRCMS_10_MHZ;
391 }
392
393 struct edcf_acparam {
394         u8 ACI;
395         u8 ECW;
396         u16 TXOP;
397 } __packed;
398
399 const u8 prio2fifo[NUMPRIO] = {
400         TX_AC_BE_FIFO,          /* 0    BE      AC_BE   Best Effort */
401         TX_AC_BK_FIFO,          /* 1    BK      AC_BK   Background */
402         TX_AC_BK_FIFO,          /* 2    --      AC_BK   Background */
403         TX_AC_BE_FIFO,          /* 3    EE      AC_BE   Best Effort */
404         TX_AC_VI_FIFO,          /* 4    CL      AC_VI   Video */
405         TX_AC_VI_FIFO,          /* 5    VI      AC_VI   Video */
406         TX_AC_VO_FIFO,          /* 6    VO      AC_VO   Voice */
407         TX_AC_VO_FIFO           /* 7    NC      AC_VO   Voice */
408 };
409
410 /* debug/trace */
411 uint brcm_msg_level =
412 #if defined(BCMDBG)
413         LOG_ERROR_VAL;
414 #else
415         0;
416 #endif                          /* BCMDBG */
417
418 /* TX FIFO number to WME/802.1E Access Category */
419 static const u8 wme_fifo2ac[] = { AC_BK, AC_BE, AC_VI, AC_VO, AC_BE, AC_BE };
420
421 /* WME/802.1E Access Category to TX FIFO number */
422 static const u8 wme_ac2fifo[] = { 1, 0, 2, 3 };
423
424 /* 802.1D Priority to precedence queue mapping */
425 const u8 wlc_prio2prec_map[] = {
426         _BRCMS_PREC_BE,         /* 0 BE - Best-effort */
427         _BRCMS_PREC_BK,         /* 1 BK - Background */
428         _BRCMS_PREC_NONE,               /* 2 None = - */
429         _BRCMS_PREC_EE,         /* 3 EE - Excellent-effort */
430         _BRCMS_PREC_CL,         /* 4 CL - Controlled Load */
431         _BRCMS_PREC_VI,         /* 5 Vi - Video */
432         _BRCMS_PREC_VO,         /* 6 Vo - Voice */
433         _BRCMS_PREC_NC,         /* 7 NC - Network Control */
434 };
435
436 static const u16 xmtfifo_sz[][NFIFO] = {
437         /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
438         {20, 192, 192, 21, 17, 5},
439         /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
440         {9, 58, 22, 14, 14, 5},
441         /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
442         {20, 192, 192, 21, 17, 5},
443         /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
444         {20, 192, 192, 21, 17, 5},
445         /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
446         {9, 58, 22, 14, 14, 5},
447 };
448
449 static const u8 acbitmap2maxprio[] = {
450         PRIO_8021D_BE, PRIO_8021D_BE, PRIO_8021D_BK, PRIO_8021D_BK,
451         PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI,
452         PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO,
453         PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO
454 };
455
456 #ifdef BCMDBG
457 static const char * const fifo_names[] = {
458         "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
459 #else
460 static const char fifo_names[6][0];
461 #endif
462
463 #ifdef BCMDBG
464 /* pointer to most recently allocated wl/wlc */
465 static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
466 #endif
467
468 static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
469 {
470         if (cfg == NULL)
471                 return;
472
473         kfree(cfg->current_bss);
474         kfree(cfg);
475 }
476
477 static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
478 {
479         if (wlc == NULL)
480                 return;
481
482         brcms_c_bsscfg_mfree(wlc->bsscfg);
483         kfree(wlc->pub);
484         kfree(wlc->modulecb);
485         kfree(wlc->default_bss);
486         kfree(wlc->protection);
487         kfree(wlc->stf);
488         kfree(wlc->bandstate[0]);
489         kfree(wlc->corestate->macstat_snapshot);
490         kfree(wlc->corestate);
491         kfree(wlc->hw->bandstate[0]);
492         kfree(wlc->hw);
493
494         /* free the wlc */
495         kfree(wlc);
496         wlc = NULL;
497 }
498
499 static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
500 {
501         struct brcms_bss_cfg *cfg;
502
503         cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
504         if (cfg == NULL)
505                 goto fail;
506
507         cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
508         if (cfg->current_bss == NULL)
509                 goto fail;
510
511         return cfg;
512
513  fail:
514         brcms_c_bsscfg_mfree(cfg);
515         return NULL;
516 }
517
518 static struct brcms_c_info *
519 brcms_c_attach_malloc(uint unit, uint *err, uint devid)
520 {
521         struct brcms_c_info *wlc;
522
523         wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
524         if (wlc == NULL) {
525                 *err = 1002;
526                 goto fail;
527         }
528
529         /* allocate struct brcms_c_pub state structure */
530         wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
531         if (wlc->pub == NULL) {
532                 *err = 1003;
533                 goto fail;
534         }
535         wlc->pub->wlc = wlc;
536
537         /* allocate struct brcms_hardware state structure */
538
539         wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
540         if (wlc->hw == NULL) {
541                 *err = 1005;
542                 goto fail;
543         }
544         wlc->hw->wlc = wlc;
545
546         wlc->hw->bandstate[0] =
547                 kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
548         if (wlc->hw->bandstate[0] == NULL) {
549                 *err = 1006;
550                 goto fail;
551         } else {
552                 int i;
553
554                 for (i = 1; i < MAXBANDS; i++)
555                         wlc->hw->bandstate[i] = (struct brcms_hw_band *)
556                             ((unsigned long)wlc->hw->bandstate[0] +
557                              (sizeof(struct brcms_hw_band) * i));
558         }
559
560         wlc->modulecb =
561                 kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
562         if (wlc->modulecb == NULL) {
563                 *err = 1009;
564                 goto fail;
565         }
566
567         wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
568         if (wlc->default_bss == NULL) {
569                 *err = 1010;
570                 goto fail;
571         }
572
573         wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
574         if (wlc->bsscfg == NULL) {
575                 *err = 1011;
576                 goto fail;
577         }
578
579         wlc->protection = kzalloc(sizeof(struct brcms_protection),
580                                   GFP_ATOMIC);
581         if (wlc->protection == NULL) {
582                 *err = 1016;
583                 goto fail;
584         }
585
586         wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
587         if (wlc->stf == NULL) {
588                 *err = 1017;
589                 goto fail;
590         }
591
592         wlc->bandstate[0] =
593                 kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
594         if (wlc->bandstate[0] == NULL) {
595                 *err = 1025;
596                 goto fail;
597         } else {
598                 int i;
599
600                 for (i = 1; i < MAXBANDS; i++)
601                         wlc->bandstate[i] = (struct brcms_band *)
602                                 ((unsigned long)wlc->bandstate[0]
603                                 + (sizeof(struct brcms_band)*i));
604         }
605
606         wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
607         if (wlc->corestate == NULL) {
608                 *err = 1026;
609                 goto fail;
610         }
611
612         wlc->corestate->macstat_snapshot =
613                 kzalloc(sizeof(struct macstat), GFP_ATOMIC);
614         if (wlc->corestate->macstat_snapshot == NULL) {
615                 *err = 1027;
616                 goto fail;
617         }
618
619         return wlc;
620
621  fail:
622         brcms_c_detach_mfree(wlc);
623         return NULL;
624 }
625
626 /*
627  * Update the slot timing for standard 11b/g (20us slots)
628  * or shortslot 11g (9us slots)
629  * The PSM needs to be suspended for this call.
630  */
631 static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
632                                         bool shortslot)
633 {
634         struct d11regs __iomem *regs;
635
636         regs = wlc_hw->regs;
637
638         if (shortslot) {
639                 /* 11g short slot: 11a timing */
640                 W_REG(&regs->ifs_slot, 0x0207); /* APHY_SLOT_TIME */
641                 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
642         } else {
643                 /* 11g long slot: 11b timing */
644                 W_REG(&regs->ifs_slot, 0x0212); /* BPHY_SLOT_TIME */
645                 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
646         }
647 }
648
649 static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
650                                 const struct d11init *inits)
651 {
652         int i;
653         u8 __iomem *base;
654         u8 __iomem *addr;
655         u16 size;
656         u32 value;
657
658         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
659
660         base = (u8 __iomem *)wlc_hw->regs;
661
662         for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
663                 size = le16_to_cpu(inits[i].size);
664                 addr = base + le16_to_cpu(inits[i].addr);
665                 value = le32_to_cpu(inits[i].value);
666                 if (size == 2)
667                         W_REG((u16 __iomem *)addr, value);
668                 else if (size == 4)
669                         W_REG((u32 __iomem *)addr, value);
670                 else
671                         break;
672         }
673 }
674
675 static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
676 {
677         u8 idx;
678         u16 addr[] = {
679                 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
680                 M_HOST_FLAGS5
681         };
682
683         for (idx = 0; idx < MHFMAX; idx++)
684                 brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
685 }
686
687 static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
688 {
689         struct wiphy *wiphy = wlc_hw->wlc->wiphy;
690         struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
691
692         /* init microcode host flags */
693         brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
694
695         /* do band-specific ucode IHR, SHM, and SCR inits */
696         if (D11REV_IS(wlc_hw->corerev, 23)) {
697                 if (BRCMS_ISNPHY(wlc_hw->band))
698                         brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
699                 else
700                         wiphy_err(wiphy, "%s: wl%d: unsupported phy in corerev"
701                                   " %d\n", __func__, wlc_hw->unit,
702                                   wlc_hw->corerev);
703         } else {
704                 if (D11REV_IS(wlc_hw->corerev, 24)) {
705                         if (BRCMS_ISLCNPHY(wlc_hw->band))
706                                 brcms_c_write_inits(wlc_hw,
707                                                     ucode->d11lcn0bsinitvals24);
708                         else
709                                 wiphy_err(wiphy, "%s: wl%d: unsupported phy in"
710                                           " core rev %d\n", __func__,
711                                           wlc_hw->unit, wlc_hw->corerev);
712                 } else {
713                         wiphy_err(wiphy, "%s: wl%d: unsupported corerev %d\n",
714                                 __func__, wlc_hw->unit, wlc_hw->corerev);
715                 }
716         }
717 }
718
719 static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
720 {
721         BCMMSG(wlc_hw->wlc->wiphy, "wl%d: clk %d\n", wlc_hw->unit, clk);
722
723         wlc_hw->phyclk = clk;
724
725         if (OFF == clk) {       /* clear gmode bit, put phy into reset */
726
727                 ai_core_cflags(wlc_hw->sih, (SICF_PRST | SICF_FGC | SICF_GMODE),
728                                (SICF_PRST | SICF_FGC));
729                 udelay(1);
730                 ai_core_cflags(wlc_hw->sih, (SICF_PRST | SICF_FGC), SICF_PRST);
731                 udelay(1);
732
733         } else {                /* take phy out of reset */
734
735                 ai_core_cflags(wlc_hw->sih, (SICF_PRST | SICF_FGC), SICF_FGC);
736                 udelay(1);
737                 ai_core_cflags(wlc_hw->sih, (SICF_FGC), 0);
738                 udelay(1);
739
740         }
741 }
742
743 /* switch to new band but leave it inactive */
744 static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
745 {
746         struct brcms_hardware *wlc_hw = wlc->hw;
747         u32 macintmask;
748
749         BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
750
751         WARN_ON((R_REG(&wlc_hw->regs->maccontrol) & MCTL_EN_MAC) != 0);
752
753         /* disable interrupts */
754         macintmask = brcms_intrsoff(wlc->wl);
755
756         /* radio off */
757         wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
758
759         brcms_b_core_phy_clk(wlc_hw, OFF);
760
761         brcms_c_setxband(wlc_hw, bandunit);
762
763         return macintmask;
764 }
765
766 /* Process received frames */
767 /*
768  * Return true if more frames need to be processed. false otherwise.
769  * Param 'bound' indicates max. # frames to process before break out.
770  */
771 static bool
772 brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
773 {
774         struct sk_buff *p;
775         struct sk_buff *head = NULL;
776         struct sk_buff *tail = NULL;
777         uint n = 0;
778         uint bound_limit = bound ? RXBND : -1;
779
780         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
781         /* gather received frames */
782         while ((p = dma_rx(wlc_hw->di[fifo]))) {
783
784                 if (!tail)
785                         head = tail = p;
786                 else {
787                         tail->prev = p;
788                         tail = p;
789                 }
790
791                 /* !give others some time to run! */
792                 if (++n >= bound_limit)
793                         break;
794         }
795
796         /* post more rbufs */
797         dma_rxfill(wlc_hw->di[fifo]);
798
799         /* process each frame */
800         while ((p = head) != NULL) {
801                 struct d11rxhdr_le *rxh_le;
802                 struct d11rxhdr *rxh;
803                 head = head->prev;
804                 p->prev = NULL;
805
806                 rxh_le = (struct d11rxhdr_le *)p->data;
807                 rxh = (struct d11rxhdr *)p->data;
808
809                 /* fixup rx header endianness */
810                 rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
811                 rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
812                 rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
813                 rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
814                 rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
815                 rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
816                 rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
817                 rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
818                 rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
819                 rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
820                 rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
821
822                 brcms_c_recv(wlc_hw->wlc, p);
823         }
824
825         return n >= bound_limit;
826 }
827
828 /* process an individual struct tx_status */
829 static bool
830 brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
831 {
832         struct sk_buff *p;
833         uint queue;
834         struct d11txh *txh;
835         struct scb *scb = NULL;
836         bool free_pdu;
837         int tx_rts, tx_frame_count, tx_rts_count;
838         uint totlen, supr_status;
839         bool lastframe;
840         struct ieee80211_hdr *h;
841         u16 mcl;
842         struct ieee80211_tx_info *tx_info;
843         struct ieee80211_tx_rate *txrate;
844         int i;
845
846         /* discard intermediate indications for ucode with one legitimate case:
847          *   e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
848          *   but the subsequent tx of DATA failed. so it will start rts/cts
849          *   from the beginning (resetting the rts transmission count)
850          */
851         if (!(txs->status & TX_STATUS_AMPDU)
852             && (txs->status & TX_STATUS_INTERMEDIATE)) {
853                 wiphy_err(wlc->wiphy, "%s: INTERMEDIATE but not AMPDU\n",
854                           __func__);
855                 return false;
856         }
857
858         queue = txs->frameid & TXFID_QUEUE_MASK;
859         if (queue >= NFIFO) {
860                 p = NULL;
861                 goto fatal;
862         }
863
864         p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
865         if (p == NULL)
866                 goto fatal;
867
868         txh = (struct d11txh *) (p->data);
869         mcl = le16_to_cpu(txh->MacTxControlLow);
870
871         if (txs->phyerr) {
872                 if (brcm_msg_level & LOG_ERROR_VAL) {
873                         wiphy_err(wlc->wiphy, "phyerr 0x%x, rate 0x%x\n",
874                                   txs->phyerr, txh->MainRates);
875                         brcms_c_print_txdesc(txh);
876                 }
877                 brcms_c_print_txstatus(txs);
878         }
879
880         if (txs->frameid != le16_to_cpu(txh->TxFrameID))
881                 goto fatal;
882         tx_info = IEEE80211_SKB_CB(p);
883         h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
884
885         if (tx_info->control.sta)
886                 scb = &wlc->pri_scb;
887
888         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
889                 brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
890                 return false;
891         }
892
893         supr_status = txs->status & TX_STATUS_SUPR_MASK;
894         if (supr_status == TX_STATUS_SUPR_BADCH)
895                 BCMMSG(wlc->wiphy,
896                        "%s: Pkt tx suppressed, possibly channel %d\n",
897                        __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
898
899         tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
900         tx_frame_count =
901             (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
902         tx_rts_count =
903             (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
904
905         lastframe = !ieee80211_has_morefrags(h->frame_control);
906
907         if (!lastframe) {
908                 wiphy_err(wlc->wiphy, "Not last frame!\n");
909         } else {
910                 /*
911                  * Set information to be consumed by Minstrel ht.
912                  *
913                  * The "fallback limit" is the number of tx attempts a given
914                  * MPDU is sent at the "primary" rate. Tx attempts beyond that
915                  * limit are sent at the "secondary" rate.
916                  * A 'short frame' does not exceed RTS treshold.
917                  */
918                 u16 sfbl,       /* Short Frame Rate Fallback Limit */
919                     lfbl,       /* Long Frame Rate Fallback Limit */
920                     fbl;
921
922                 if (queue < AC_COUNT) {
923                         sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
924                                       EDCF_SFB);
925                         lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
926                                       EDCF_LFB);
927                 } else {
928                         sfbl = wlc->SFBL;
929                         lfbl = wlc->LFBL;
930                 }
931
932                 txrate = tx_info->status.rates;
933                 if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
934                         fbl = lfbl;
935                 else
936                         fbl = sfbl;
937
938                 ieee80211_tx_info_clear_status(tx_info);
939
940                 if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
941                         /*
942                          * rate selection requested a fallback rate
943                          * and we used it
944                          */
945                         txrate[0].count = fbl;
946                         txrate[1].count = tx_frame_count - fbl;
947                 } else {
948                         /*
949                          * rate selection did not request fallback rate, or
950                          * we didn't need it
951                          */
952                         txrate[0].count = tx_frame_count;
953                         /*
954                          * rc80211_minstrel.c:minstrel_tx_status() expects
955                          * unused rates to be marked with idx = -1
956                          */
957                         txrate[1].idx = -1;
958                         txrate[1].count = 0;
959                 }
960
961                 /* clear the rest of the rates */
962                 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
963                         txrate[i].idx = -1;
964                         txrate[i].count = 0;
965                 }
966
967                 if (txs->status & TX_STATUS_ACK_RCV)
968                         tx_info->flags |= IEEE80211_TX_STAT_ACK;
969         }
970
971         totlen = brcmu_pkttotlen(p);
972         free_pdu = true;
973
974         brcms_c_txfifo_complete(wlc, queue, 1);
975
976         if (lastframe) {
977                 p->next = NULL;
978                 p->prev = NULL;
979                 /* remove PLCP & Broadcom tx descriptor header */
980                 skb_pull(p, D11_PHY_HDR_LEN);
981                 skb_pull(p, D11_TXH_LEN);
982                 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
983         } else {
984                 wiphy_err(wlc->wiphy, "%s: Not last frame => not calling "
985                           "tx_status\n", __func__);
986         }
987
988         return false;
989
990  fatal:
991         if (p)
992                 brcmu_pkt_buf_free_skb(p);
993
994         return true;
995
996 }
997
998 static bool
999 brcms_b_dotxstatus(struct brcms_hardware *wlc_hw, struct tx_status *txs)
1000 {
1001         /* discard intermediate indications for ucode with one legitimate case:
1002          *   e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
1003          *   but the subsequent tx of DATA failed. so it will start rts/cts from
1004          *   the beginning (resetting the rts transmission count)
1005          */
1006         if (!(txs->status & TX_STATUS_AMPDU)
1007             && (txs->status & TX_STATUS_INTERMEDIATE))
1008                 return false;
1009
1010         return brcms_c_dotxstatus(wlc_hw->wlc, txs);
1011 }
1012
1013 /* process tx completion events in BMAC
1014  * Return true if more tx status need to be processed. false otherwise.
1015  */
1016 static bool
1017 brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
1018 {
1019         bool morepending = false;
1020         struct brcms_c_info *wlc = wlc_hw->wlc;
1021         struct d11regs __iomem *regs;
1022         struct tx_status txstatus, *txs;
1023         u32 s1, s2;
1024         uint n = 0;
1025         /*
1026          * Param 'max_tx_num' indicates max. # tx status to process before
1027          * break out.
1028          */
1029         uint max_tx_num = bound ? TXSBND : -1;
1030
1031         BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
1032
1033         txs = &txstatus;
1034         regs = wlc_hw->regs;
1035         while (!(*fatal)
1036                && (s1 = R_REG(&regs->frmtxstatus)) & TXS_V) {
1037
1038                 if (s1 == 0xffffffff) {
1039                         wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n",
1040                                 wlc_hw->unit, __func__);
1041                         return morepending;
1042                 }
1043
1044                         s2 = R_REG(&regs->frmtxstatus2);
1045
1046                 txs->status = s1 & TXS_STATUS_MASK;
1047                 txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
1048                 txs->sequence = s2 & TXS_SEQ_MASK;
1049                 txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
1050                 txs->lasttxtime = 0;
1051
1052                 *fatal = brcms_b_dotxstatus(wlc_hw, txs);
1053
1054                 /* !give others some time to run! */
1055                 if (++n >= max_tx_num)
1056                         break;
1057         }
1058
1059         if (*fatal)
1060                 return 0;
1061
1062         if (n >= max_tx_num)
1063                 morepending = true;
1064
1065         if (!pktq_empty(&wlc->pkt_queue->q))
1066                 brcms_c_send_q(wlc);
1067
1068         return morepending;
1069 }
1070
1071 /* second-level interrupt processing
1072  *   Return true if another dpc needs to be re-scheduled. false otherwise.
1073  *   Param 'bounded' indicates if applicable loops should be bounded.
1074  */
1075 bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
1076 {
1077         u32 macintstatus;
1078         struct brcms_hardware *wlc_hw = wlc->hw;
1079         struct d11regs __iomem *regs = wlc_hw->regs;
1080         bool fatal = false;
1081         struct wiphy *wiphy = wlc->wiphy;
1082
1083         if (brcms_deviceremoved(wlc)) {
1084                 wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
1085                           __func__);
1086                 brcms_down(wlc->wl);
1087                 return false;
1088         }
1089
1090         /* grab and clear the saved software intstatus bits */
1091         macintstatus = wlc->macintstatus;
1092         wlc->macintstatus = 0;
1093
1094         BCMMSG(wlc->wiphy, "wl%d: macintstatus 0x%x\n",
1095                wlc_hw->unit, macintstatus);
1096
1097         WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
1098
1099         /* tx status */
1100         if (macintstatus & MI_TFS) {
1101                 if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
1102                         wlc->macintstatus |= MI_TFS;
1103                 if (fatal) {
1104                         wiphy_err(wiphy, "MI_TFS: fatal\n");
1105                         goto fatal;
1106                 }
1107         }
1108
1109         if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
1110                 brcms_c_tbtt(wlc);
1111
1112         /* ATIM window end */
1113         if (macintstatus & MI_ATIMWINEND) {
1114                 BCMMSG(wlc->wiphy, "end of ATIM window\n");
1115                 OR_REG(&regs->maccommand, wlc->qvalid);
1116                 wlc->qvalid = 0;
1117         }
1118
1119         /*
1120          * received data or control frame, MI_DMAINT is
1121          * indication of RX_FIFO interrupt
1122          */
1123         if (macintstatus & MI_DMAINT)
1124                 if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
1125                         wlc->macintstatus |= MI_DMAINT;
1126
1127         /* noise sample collected */
1128         if (macintstatus & MI_BG_NOISE)
1129                 wlc_phy_noise_sample_intr(wlc_hw->band->pi);
1130
1131         if (macintstatus & MI_GP0) {
1132                 wiphy_err(wiphy, "wl%d: PSM microcode watchdog fired at %d "
1133                         "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
1134
1135                 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
1136                                         __func__, wlc_hw->sih->chip,
1137                                         wlc_hw->sih->chiprev);
1138                 /* big hammer */
1139                 brcms_init(wlc->wl);
1140         }
1141
1142         /* gptimer timeout */
1143         if (macintstatus & MI_TO)
1144                 W_REG(&regs->gptimer, 0);
1145
1146         if (macintstatus & MI_RFDISABLE) {
1147                 BCMMSG(wlc->wiphy, "wl%d: BMAC Detected a change on the"
1148                        " RF Disable Input\n", wlc_hw->unit);
1149                 brcms_rfkill_set_hw_state(wlc->wl);
1150         }
1151
1152         /* send any enq'd tx packets. Just makes sure to jump start tx */
1153         if (!pktq_empty(&wlc->pkt_queue->q))
1154                 brcms_c_send_q(wlc);
1155
1156         /* it isn't done and needs to be resched if macintstatus is non-zero */
1157         return wlc->macintstatus != 0;
1158
1159  fatal:
1160         brcms_init(wlc->wl);
1161         return wlc->macintstatus != 0;
1162 }
1163
1164 /* set initial host flags value */
1165 static void
1166 brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
1167 {
1168         struct brcms_hardware *wlc_hw = wlc->hw;
1169
1170         memset(mhfs, 0, MHFMAX * sizeof(u16));
1171
1172         mhfs[MHF2] |= mhf2_init;
1173
1174         /* prohibit use of slowclock on multifunction boards */
1175         if (wlc_hw->boardflags & BFL_NOPLLDOWN)
1176                 mhfs[MHF1] |= MHF1_FORCEFASTCLK;
1177
1178         if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
1179                 mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
1180                 mhfs[MHF1] |= MHF1_IQSWAP_WAR;
1181         }
1182 }
1183
1184 static struct dma64regs __iomem *
1185 dmareg(struct brcms_hardware *hw, uint direction, uint fifonum)
1186 {
1187         if (direction == DMA_TX)
1188                 return &(hw->regs->fifo64regs[fifonum].dmaxmt);
1189         return &(hw->regs->fifo64regs[fifonum].dmarcv);
1190 }
1191
1192 static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
1193 {
1194         uint i;
1195         char name[8];
1196         /*
1197          * ucode host flag 2 needed for pio mode, independent of band and fifo
1198          */
1199         u16 pio_mhf2 = 0;
1200         struct brcms_hardware *wlc_hw = wlc->hw;
1201         uint unit = wlc_hw->unit;
1202         struct wiphy *wiphy = wlc->wiphy;
1203
1204         /* name and offsets for dma_attach */
1205         snprintf(name, sizeof(name), "wl%d", unit);
1206
1207         if (wlc_hw->di[0] == NULL) {    /* Init FIFOs */
1208                 int dma_attach_err = 0;
1209
1210                 /*
1211                  * FIFO 0
1212                  * TX: TX_AC_BK_FIFO (TX AC Background data packets)
1213                  * RX: RX_FIFO (RX data packets)
1214                  */
1215                 wlc_hw->di[0] = dma_attach(name, wlc_hw->sih,
1216                                            (wme ? dmareg(wlc_hw, DMA_TX, 0) :
1217                                             NULL), dmareg(wlc_hw, DMA_RX, 0),
1218                                            (wme ? NTXD : 0), NRXD,
1219                                            RXBUFSZ, -1, NRXBUFPOST,
1220                                            BRCMS_HWRXOFF, &brcm_msg_level);
1221                 dma_attach_err |= (NULL == wlc_hw->di[0]);
1222
1223                 /*
1224                  * FIFO 1
1225                  * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
1226                  *   (legacy) TX_DATA_FIFO (TX data packets)
1227                  * RX: UNUSED
1228                  */
1229                 wlc_hw->di[1] = dma_attach(name, wlc_hw->sih,
1230                                            dmareg(wlc_hw, DMA_TX, 1), NULL,
1231                                            NTXD, 0, 0, -1, 0, 0,
1232                                            &brcm_msg_level);
1233                 dma_attach_err |= (NULL == wlc_hw->di[1]);
1234
1235                 /*
1236                  * FIFO 2
1237                  * TX: TX_AC_VI_FIFO (TX AC Video data packets)
1238                  * RX: UNUSED
1239                  */
1240                 wlc_hw->di[2] = dma_attach(name, wlc_hw->sih,
1241                                            dmareg(wlc_hw, DMA_TX, 2), NULL,
1242                                            NTXD, 0, 0, -1, 0, 0,
1243                                            &brcm_msg_level);
1244                 dma_attach_err |= (NULL == wlc_hw->di[2]);
1245                 /*
1246                  * FIFO 3
1247                  * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
1248                  *   (legacy) TX_CTL_FIFO (TX control & mgmt packets)
1249                  */
1250                 wlc_hw->di[3] = dma_attach(name, wlc_hw->sih,
1251                                            dmareg(wlc_hw, DMA_TX, 3),
1252                                            NULL, NTXD, 0, 0, -1,
1253                                            0, 0, &brcm_msg_level);
1254                 dma_attach_err |= (NULL == wlc_hw->di[3]);
1255 /* Cleaner to leave this as if with AP defined */
1256
1257                 if (dma_attach_err) {
1258                         wiphy_err(wiphy, "wl%d: wlc_attach: dma_attach failed"
1259                                   "\n", unit);
1260                         return false;
1261                 }
1262
1263                 /* get pointer to dma engine tx flow control variable */
1264                 for (i = 0; i < NFIFO; i++)
1265                         if (wlc_hw->di[i])
1266                                 wlc_hw->txavail[i] =
1267                                     (uint *) dma_getvar(wlc_hw->di[i],
1268                                                         "&txavail");
1269         }
1270
1271         /* initial ucode host flags */
1272         brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
1273
1274         return true;
1275 }
1276
1277 static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
1278 {
1279         uint j;
1280
1281         for (j = 0; j < NFIFO; j++) {
1282                 if (wlc_hw->di[j]) {
1283                         dma_detach(wlc_hw->di[j]);
1284                         wlc_hw->di[j] = NULL;
1285                 }
1286         }
1287 }
1288
1289 /*
1290  * Initialize brcms_c_info default values ...
1291  * may get overrides later in this function
1292  *  BMAC_NOTES, move low out and resolve the dangling ones
1293  */
1294 static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
1295 {
1296         struct brcms_c_info *wlc = wlc_hw->wlc;
1297
1298         /* set default sw macintmask value */
1299         wlc->defmacintmask = DEF_MACINTMASK;
1300
1301         /* various 802.11g modes */
1302         wlc_hw->shortslot = false;
1303
1304         wlc_hw->SFBL = RETRY_SHORT_FB;
1305         wlc_hw->LFBL = RETRY_LONG_FB;
1306
1307         /* default mac retry limits */
1308         wlc_hw->SRL = RETRY_SHORT_DEF;
1309         wlc_hw->LRL = RETRY_LONG_DEF;
1310         wlc_hw->chanspec = ch20mhz_chspec(1);
1311 }
1312
1313 static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
1314 {
1315         /* delay before first read of ucode state */
1316         udelay(40);
1317
1318         /* wait until ucode is no longer asleep */
1319         SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
1320                   DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
1321 }
1322
1323 /* control chip clock to save power, enable dynamic clock or force fast clock */
1324 static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, uint mode)
1325 {
1326         if (wlc_hw->sih->cccaps & CC_CAP_PMU) {
1327                 /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
1328                  * on backplane, but mac core will still run on ALP(not HT) when
1329                  * it enters powersave mode, which means the FCA bit may not be
1330                  * set. Should wakeup mac if driver wants it to run on HT.
1331                  */
1332
1333                 if (wlc_hw->clk) {
1334                         if (mode == CLK_FAST) {
1335                                 OR_REG(&wlc_hw->regs->clk_ctl_st,
1336                                        CCS_FORCEHT);
1337
1338                                 udelay(64);
1339
1340                                 SPINWAIT(((R_REG
1341                                            (&wlc_hw->regs->
1342                                             clk_ctl_st) & CCS_HTAVAIL) == 0),
1343                                          PMU_MAX_TRANSITION_DLY);
1344                                 WARN_ON(!(R_REG
1345                                           (&wlc_hw->regs->
1346                                            clk_ctl_st) & CCS_HTAVAIL));
1347                         } else {
1348                                 if ((wlc_hw->sih->pmurev == 0) &&
1349                                     (R_REG
1350                                      (&wlc_hw->regs->
1351                                       clk_ctl_st) & (CCS_FORCEHT | CCS_HTAREQ)))
1352                                         SPINWAIT(((R_REG
1353                                                    (&wlc_hw->regs->
1354                                                     clk_ctl_st) & CCS_HTAVAIL)
1355                                                   == 0),
1356                                                  PMU_MAX_TRANSITION_DLY);
1357                                 AND_REG(&wlc_hw->regs->clk_ctl_st,
1358                                         ~CCS_FORCEHT);
1359                         }
1360                 }
1361                 wlc_hw->forcefastclk = (mode == CLK_FAST);
1362         } else {
1363
1364                 /* old chips w/o PMU, force HT through cc,
1365                  * then use FCA to verify mac is running fast clock
1366                  */
1367
1368                 wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
1369
1370                 /* check fast clock is available (if core is not in reset) */
1371                 if (wlc_hw->forcefastclk && wlc_hw->clk)
1372                         WARN_ON(!(ai_core_sflags(wlc_hw->sih, 0, 0) &
1373                                   SISF_FCLKA));
1374
1375                 /*
1376                  * keep the ucode wake bit on if forcefastclk is on since we
1377                  * do not want ucode to put us back to slow clock when it dozes
1378                  * for PM mode. Code below matches the wake override bit with
1379                  * current forcefastclk state. Only setting bit in wake_override
1380                  * instead of waking ucode immediately since old code had this
1381                  * behavior. Older code set wlc->forcefastclk but only had the
1382                  * wake happen if the wakup_ucode work (protected by an up
1383                  * check) was executed just below.
1384                  */
1385                 if (wlc_hw->forcefastclk)
1386                         mboolset(wlc_hw->wake_override,
1387                                  BRCMS_WAKE_OVERRIDE_FORCEFAST);
1388                 else
1389                         mboolclr(wlc_hw->wake_override,
1390                                  BRCMS_WAKE_OVERRIDE_FORCEFAST);
1391         }
1392 }
1393
1394 /* set or clear ucode host flag bits
1395  * it has an optimization for no-change write
1396  * it only writes through shared memory when the core has clock;
1397  * pre-CLK changes should use wlc_write_mhf to get around the optimization
1398  *
1399  *
1400  * bands values are: BRCM_BAND_AUTO <--- Current band only
1401  *                   BRCM_BAND_5G   <--- 5G band only
1402  *                   BRCM_BAND_2G   <--- 2G band only
1403  *                   BRCM_BAND_ALL  <--- All bands
1404  */
1405 void
1406 brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
1407              int bands)
1408 {
1409         u16 save;
1410         u16 addr[MHFMAX] = {
1411                 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
1412                 M_HOST_FLAGS5
1413         };
1414         struct brcms_hw_band *band;
1415
1416         if ((val & ~mask) || idx >= MHFMAX)
1417                 return; /* error condition */
1418
1419         switch (bands) {
1420                 /* Current band only or all bands,
1421                  * then set the band to current band
1422                  */
1423         case BRCM_BAND_AUTO:
1424         case BRCM_BAND_ALL:
1425                 band = wlc_hw->band;
1426                 break;
1427         case BRCM_BAND_5G:
1428                 band = wlc_hw->bandstate[BAND_5G_INDEX];
1429                 break;
1430         case BRCM_BAND_2G:
1431                 band = wlc_hw->bandstate[BAND_2G_INDEX];
1432                 break;
1433         default:
1434                 band = NULL;    /* error condition */
1435         }
1436
1437         if (band) {
1438                 save = band->mhfs[idx];
1439                 band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
1440
1441                 /* optimization: only write through if changed, and
1442                  * changed band is the current band
1443                  */
1444                 if (wlc_hw->clk && (band->mhfs[idx] != save)
1445                     && (band == wlc_hw->band))
1446                         brcms_b_write_shm(wlc_hw, addr[idx],
1447                                            (u16) band->mhfs[idx]);
1448         }
1449
1450         if (bands == BRCM_BAND_ALL) {
1451                 wlc_hw->bandstate[0]->mhfs[idx] =
1452                     (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
1453                 wlc_hw->bandstate[1]->mhfs[idx] =
1454                     (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
1455         }
1456 }
1457
1458 /* set the maccontrol register to desired reset state and
1459  * initialize the sw cache of the register
1460  */
1461 static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
1462 {
1463         /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
1464         wlc_hw->maccontrol = 0;
1465         wlc_hw->suspended_fifos = 0;
1466         wlc_hw->wake_override = 0;
1467         wlc_hw->mute_override = 0;
1468         brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
1469 }
1470
1471 /*
1472  * write the software state of maccontrol and
1473  * overrides to the maccontrol register
1474  */
1475 static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
1476 {
1477         u32 maccontrol = wlc_hw->maccontrol;
1478
1479         /* OR in the wake bit if overridden */
1480         if (wlc_hw->wake_override)
1481                 maccontrol |= MCTL_WAKE;
1482
1483         /* set AP and INFRA bits for mute if needed */
1484         if (wlc_hw->mute_override) {
1485                 maccontrol &= ~(MCTL_AP);
1486                 maccontrol |= MCTL_INFRA;
1487         }
1488
1489         W_REG(&wlc_hw->regs->maccontrol, maccontrol);
1490 }
1491
1492 /* set or clear maccontrol bits */
1493 void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
1494 {
1495         u32 maccontrol;
1496         u32 new_maccontrol;
1497
1498         if (val & ~mask)
1499                 return; /* error condition */
1500         maccontrol = wlc_hw->maccontrol;
1501         new_maccontrol = (maccontrol & ~mask) | val;
1502
1503         /* if the new maccontrol value is the same as the old, nothing to do */
1504         if (new_maccontrol == maccontrol)
1505                 return;
1506
1507         /* something changed, cache the new value */
1508         wlc_hw->maccontrol = new_maccontrol;
1509
1510         /* write the new values with overrides applied */
1511         brcms_c_mctrl_write(wlc_hw);
1512 }
1513
1514 void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
1515                                  u32 override_bit)
1516 {
1517         if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
1518                 mboolset(wlc_hw->wake_override, override_bit);
1519                 return;
1520         }
1521
1522         mboolset(wlc_hw->wake_override, override_bit);
1523
1524         brcms_c_mctrl_write(wlc_hw);
1525         brcms_b_wait_for_wake(wlc_hw);
1526 }
1527
1528 void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
1529                                    u32 override_bit)
1530 {
1531         mboolclr(wlc_hw->wake_override, override_bit);
1532
1533         if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
1534                 return;
1535
1536         brcms_c_mctrl_write(wlc_hw);
1537 }
1538
1539 /* When driver needs ucode to stop beaconing, it has to make sure that
1540  * MCTL_AP is clear and MCTL_INFRA is set
1541  * Mode           MCTL_AP        MCTL_INFRA
1542  * AP                1              1
1543  * STA               0              1 <--- This will ensure no beacons
1544  * IBSS              0              0
1545  */
1546 static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
1547 {
1548         wlc_hw->mute_override = 1;
1549
1550         /* if maccontrol already has AP == 0 and INFRA == 1 without this
1551          * override, then there is no change to write
1552          */
1553         if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1554                 return;
1555
1556         brcms_c_mctrl_write(wlc_hw);
1557 }
1558
1559 /* Clear the override on AP and INFRA bits */
1560 static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
1561 {
1562         if (wlc_hw->mute_override == 0)
1563                 return;
1564
1565         wlc_hw->mute_override = 0;
1566
1567         /* if maccontrol already has AP == 0 and INFRA == 1 without this
1568          * override, then there is no change to write
1569          */
1570         if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1571                 return;
1572
1573         brcms_c_mctrl_write(wlc_hw);
1574 }
1575
1576 /*
1577  * Write a MAC address to the given match reg offset in the RXE match engine.
1578  */
1579 static void
1580 brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
1581                        const u8 *addr)
1582 {
1583         struct d11regs __iomem *regs;
1584         u16 mac_l;
1585         u16 mac_m;
1586         u16 mac_h;
1587
1588         BCMMSG(wlc_hw->wlc->wiphy, "wl%d: brcms_b_set_addrmatch\n",
1589                  wlc_hw->unit);
1590
1591         regs = wlc_hw->regs;
1592         mac_l = addr[0] | (addr[1] << 8);
1593         mac_m = addr[2] | (addr[3] << 8);
1594         mac_h = addr[4] | (addr[5] << 8);
1595
1596         /* enter the MAC addr into the RXE match registers */
1597         W_REG(&regs->rcm_ctl, RCM_INC_DATA | match_reg_offset);
1598         W_REG(&regs->rcm_mat_data, mac_l);
1599         W_REG(&regs->rcm_mat_data, mac_m);
1600         W_REG(&regs->rcm_mat_data, mac_h);
1601
1602 }
1603
1604 void
1605 brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
1606                             void *buf)
1607 {
1608         struct d11regs __iomem *regs;
1609         u32 word;
1610         __le32 word_le;
1611         __be32 word_be;
1612         bool be_bit;
1613         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
1614
1615         regs = wlc_hw->regs;
1616         W_REG(&regs->tplatewrptr, offset);
1617
1618         /* if MCTL_BIGEND bit set in mac control register,
1619          * the chip swaps data in fifo, as well as data in
1620          * template ram
1621          */
1622         be_bit = (R_REG(&regs->maccontrol) & MCTL_BIGEND) != 0;
1623
1624         while (len > 0) {
1625                 memcpy(&word, buf, sizeof(u32));
1626
1627                 if (be_bit) {
1628                         word_be = cpu_to_be32(word);
1629                         word = *(u32 *)&word_be;
1630                 } else {
1631                         word_le = cpu_to_le32(word);
1632                         word = *(u32 *)&word_le;
1633                 }
1634
1635                 W_REG(&regs->tplatewrdata, word);
1636
1637                 buf = (u8 *) buf + sizeof(u32);
1638                 len -= sizeof(u32);
1639         }
1640 }
1641
1642 static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
1643 {
1644         wlc_hw->band->CWmin = newmin;
1645
1646         W_REG(&wlc_hw->regs->objaddr, OBJADDR_SCR_SEL | S_DOT11_CWMIN);
1647         (void)R_REG(&wlc_hw->regs->objaddr);
1648         W_REG(&wlc_hw->regs->objdata, newmin);
1649 }
1650
1651 static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
1652 {
1653         wlc_hw->band->CWmax = newmax;
1654
1655         W_REG(&wlc_hw->regs->objaddr, OBJADDR_SCR_SEL | S_DOT11_CWMAX);
1656         (void)R_REG(&wlc_hw->regs->objaddr);
1657         W_REG(&wlc_hw->regs->objdata, newmax);
1658 }
1659
1660 void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
1661 {
1662         bool fastclk;
1663
1664         /* request FAST clock if not on */
1665         fastclk = wlc_hw->forcefastclk;
1666         if (!fastclk)
1667                 brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
1668
1669         wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
1670
1671         brcms_b_phy_reset(wlc_hw);
1672         wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
1673
1674         /* restore the clk */
1675         if (!fastclk)
1676                 brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
1677 }
1678
1679 static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
1680 {
1681         u16 v;
1682         struct brcms_c_info *wlc = wlc_hw->wlc;
1683         /* update SYNTHPU_DLY */
1684
1685         if (BRCMS_ISLCNPHY(wlc->band))
1686                 v = SYNTHPU_DLY_LPPHY_US;
1687         else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
1688                 v = SYNTHPU_DLY_NPHY_US;
1689         else
1690                 v = SYNTHPU_DLY_BPHY_US;
1691
1692         brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
1693 }
1694
1695 static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
1696 {
1697         u16 phyctl;
1698         u16 phytxant = wlc_hw->bmac_phytxant;
1699         u16 mask = PHY_TXC_ANT_MASK;
1700
1701         /* set the Probe Response frame phy control word */
1702         phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
1703         phyctl = (phyctl & ~mask) | phytxant;
1704         brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
1705
1706         /* set the Response (ACK/CTS) frame phy control word */
1707         phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
1708         phyctl = (phyctl & ~mask) | phytxant;
1709         brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
1710 }
1711
1712 static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
1713                                          u8 rate)
1714 {
1715         uint i;
1716         u8 plcp_rate = 0;
1717         struct plcp_signal_rate_lookup {
1718                 u8 rate;
1719                 u8 signal_rate;
1720         };
1721         /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
1722         const struct plcp_signal_rate_lookup rate_lookup[] = {
1723                 {BRCM_RATE_6M, 0xB},
1724                 {BRCM_RATE_9M, 0xF},
1725                 {BRCM_RATE_12M, 0xA},
1726                 {BRCM_RATE_18M, 0xE},
1727                 {BRCM_RATE_24M, 0x9},
1728                 {BRCM_RATE_36M, 0xD},
1729                 {BRCM_RATE_48M, 0x8},
1730                 {BRCM_RATE_54M, 0xC}
1731         };
1732
1733         for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
1734                 if (rate == rate_lookup[i].rate) {
1735                         plcp_rate = rate_lookup[i].signal_rate;
1736                         break;
1737                 }
1738         }
1739
1740         /* Find the SHM pointer to the rate table entry by looking in the
1741          * Direct-map Table
1742          */
1743         return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
1744 }
1745
1746 static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
1747 {
1748         u8 rate;
1749         u8 rates[8] = {
1750                 BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
1751                 BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
1752         };
1753         u16 entry_ptr;
1754         u16 pctl1;
1755         uint i;
1756
1757         if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
1758                 return;
1759
1760         /* walk the phy rate table and update the entries */
1761         for (i = 0; i < ARRAY_SIZE(rates); i++) {
1762                 rate = rates[i];
1763
1764                 entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
1765
1766                 /* read the SHM Rate Table entry OFDM PCTL1 values */
1767                 pctl1 =
1768                     brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
1769
1770                 /* modify the value */
1771                 pctl1 &= ~PHY_TXC1_MODE_MASK;
1772                 pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
1773
1774                 /* Update the SHM Rate Table entry OFDM PCTL1 values */
1775                 brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
1776                                    pctl1);
1777         }
1778 }
1779
1780 /* band-specific init */
1781 static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
1782 {
1783         struct brcms_hardware *wlc_hw = wlc->hw;
1784
1785         BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
1786                 wlc_hw->band->bandunit);
1787
1788         brcms_c_ucode_bsinit(wlc_hw);
1789
1790         wlc_phy_init(wlc_hw->band->pi, chanspec);
1791
1792         brcms_c_ucode_txant_set(wlc_hw);
1793
1794         /*
1795          * cwmin is band-specific, update hardware
1796          * with value for current band
1797          */
1798         brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
1799         brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
1800
1801         brcms_b_update_slot_timing(wlc_hw,
1802                                    wlc_hw->band->bandtype == BRCM_BAND_5G ?
1803                                    true : wlc_hw->shortslot);
1804
1805         /* write phytype and phyvers */
1806         brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
1807         brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
1808
1809         /*
1810          * initialize the txphyctl1 rate table since
1811          * shmem is shared between bands
1812          */
1813         brcms_upd_ofdm_pctl1_table(wlc_hw);
1814
1815         brcms_b_upd_synthpu(wlc_hw);
1816 }
1817
1818 /* Perform a soft reset of the PHY PLL */
1819 void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
1820 {
1821         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
1822
1823         ai_corereg(wlc_hw->sih, SI_CC_IDX,
1824                    offsetof(struct chipcregs, chipcontrol_addr), ~0, 0);
1825         udelay(1);
1826         ai_corereg(wlc_hw->sih, SI_CC_IDX,
1827                    offsetof(struct chipcregs, chipcontrol_data), 0x4, 0);
1828         udelay(1);
1829         ai_corereg(wlc_hw->sih, SI_CC_IDX,
1830                    offsetof(struct chipcregs, chipcontrol_data), 0x4, 4);
1831         udelay(1);
1832         ai_corereg(wlc_hw->sih, SI_CC_IDX,
1833                    offsetof(struct chipcregs, chipcontrol_data), 0x4, 0);
1834         udelay(1);
1835 }
1836
1837 /* light way to turn on phy clock without reset for NPHY only
1838  *  refer to brcms_b_core_phy_clk for full version
1839  */
1840 void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
1841 {
1842         /* support(necessary for NPHY and HYPHY) only */
1843         if (!BRCMS_ISNPHY(wlc_hw->band))
1844                 return;
1845
1846         if (ON == clk)
1847                 ai_core_cflags(wlc_hw->sih, SICF_FGC, SICF_FGC);
1848         else
1849                 ai_core_cflags(wlc_hw->sih, SICF_FGC, 0);
1850
1851 }
1852
1853 void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
1854 {
1855         if (ON == clk)
1856                 ai_core_cflags(wlc_hw->sih, SICF_MPCLKE, SICF_MPCLKE);
1857         else
1858                 ai_core_cflags(wlc_hw->sih, SICF_MPCLKE, 0);
1859 }
1860
1861 void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
1862 {
1863         struct brcms_phy_pub *pih = wlc_hw->band->pi;
1864         u32 phy_bw_clkbits;
1865         bool phy_in_reset = false;
1866
1867         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
1868
1869         if (pih == NULL)
1870                 return;
1871
1872         phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
1873
1874         /* Specific reset sequence required for NPHY rev 3 and 4 */
1875         if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
1876             NREV_LE(wlc_hw->band->phyrev, 4)) {
1877                 /* Set the PHY bandwidth */
1878                 ai_core_cflags(wlc_hw->sih, SICF_BWMASK, phy_bw_clkbits);
1879
1880                 udelay(1);
1881
1882                 /* Perform a soft reset of the PHY PLL */
1883                 brcms_b_core_phypll_reset(wlc_hw);
1884
1885                 /* reset the PHY */
1886                 ai_core_cflags(wlc_hw->sih, (SICF_PRST | SICF_PCLKE),
1887                                (SICF_PRST | SICF_PCLKE));
1888                 phy_in_reset = true;
1889         } else {
1890                 ai_core_cflags(wlc_hw->sih,
1891                                (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
1892                                (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
1893         }
1894
1895         udelay(2);
1896         brcms_b_core_phy_clk(wlc_hw, ON);
1897
1898         if (pih)
1899                 wlc_phy_anacore(pih, ON);
1900 }
1901
1902 /* switch to and initialize new band */
1903 static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
1904                             u16 chanspec) {
1905         struct brcms_c_info *wlc = wlc_hw->wlc;
1906         u32 macintmask;
1907
1908         /* Enable the d11 core before accessing it */
1909         if (!ai_iscoreup(wlc_hw->sih)) {
1910                 ai_core_reset(wlc_hw->sih, 0, 0);
1911                 brcms_c_mctrl_reset(wlc_hw);
1912         }
1913
1914         macintmask = brcms_c_setband_inact(wlc, bandunit);
1915
1916         if (!wlc_hw->up)
1917                 return;
1918
1919         brcms_b_core_phy_clk(wlc_hw, ON);
1920
1921         /* band-specific initializations */
1922         brcms_b_bsinit(wlc, chanspec);
1923
1924         /*
1925          * If there are any pending software interrupt bits,
1926          * then replace these with a harmless nonzero value
1927          * so brcms_c_dpc() will re-enable interrupts when done.
1928          */
1929         if (wlc->macintstatus)
1930                 wlc->macintstatus = MI_DMAINT;
1931
1932         /* restore macintmask */
1933         brcms_intrsrestore(wlc->wl, macintmask);
1934
1935         /* ucode should still be suspended.. */
1936         WARN_ON((R_REG(&wlc_hw->regs->maccontrol) & MCTL_EN_MAC) != 0);
1937 }
1938
1939 /* low-level band switch utility routine */
1940 void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
1941 {
1942         BCMMSG(wlc_hw->wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
1943                 bandunit);
1944
1945         wlc_hw->band = wlc_hw->bandstate[bandunit];
1946
1947         /*
1948          * BMAC_NOTE:
1949          *   until we eliminate need for wlc->band refs in low level code
1950          */
1951         wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
1952
1953         /* set gmode core flag */
1954         if (wlc_hw->sbclk && !wlc_hw->noreset)
1955                 ai_core_cflags(wlc_hw->sih, SICF_GMODE,
1956                                ((bandunit == 0) ? SICF_GMODE : 0));
1957 }
1958
1959 static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
1960 {
1961
1962         /* reject unsupported corerev */
1963         if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
1964                 wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
1965                           wlc_hw->corerev);
1966                 return false;
1967         }
1968
1969         return true;
1970 }
1971
1972 /* Validate some board info parameters */
1973 static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
1974 {
1975         uint boardrev = wlc_hw->boardrev;
1976
1977         /* 4 bits each for board type, major, minor, and tiny version */
1978         uint brt = (boardrev & 0xf000) >> 12;
1979         uint b0 = (boardrev & 0xf00) >> 8;
1980         uint b1 = (boardrev & 0xf0) >> 4;
1981         uint b2 = boardrev & 0xf;
1982
1983         /* voards from other vendors are always considered valid */
1984         if (wlc_hw->sih->boardvendor != PCI_VENDOR_ID_BROADCOM)
1985                 return true;
1986
1987         /* do some boardrev sanity checks when boardvendor is Broadcom */
1988         if (boardrev == 0)
1989                 return false;
1990
1991         if (boardrev <= 0xff)
1992                 return true;
1993
1994         if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
1995                 || (b2 > 9))
1996                 return false;
1997
1998         return true;
1999 }
2000
2001 static char *brcms_c_get_macaddr(struct brcms_hardware *wlc_hw)
2002 {
2003         const char *varname = "macaddr";
2004         char *macaddr;
2005
2006         /* If macaddr exists, use it (Sromrev4, CIS, ...). */
2007         macaddr = getvar(wlc_hw->vars, varname);
2008         if (macaddr != NULL)
2009                 return macaddr;
2010
2011         if (wlc_hw->_nbands > 1)
2012                 varname = "et1macaddr";
2013         else
2014                 varname = "il0macaddr";
2015
2016         macaddr = getvar(wlc_hw->vars, varname);
2017         if (macaddr == NULL)
2018                 wiphy_err(wlc_hw->wlc->wiphy, "wl%d: wlc_get_macaddr: macaddr "
2019                           "getvar(%s) not found\n", wlc_hw->unit, varname);
2020
2021         return macaddr;
2022 }
2023
2024 /* power both the pll and external oscillator on/off */
2025 static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
2026 {
2027         BCMMSG(wlc_hw->wlc->wiphy, "wl%d: want %d\n", wlc_hw->unit, want);
2028
2029         /*
2030          * dont power down if plldown is false or
2031          * we must poll hw radio disable
2032          */
2033         if (!want && wlc_hw->pllreq)
2034                 return;
2035
2036         if (wlc_hw->sih)
2037                 ai_clkctl_xtal(wlc_hw->sih, XTAL | PLL, want);
2038
2039         wlc_hw->sbclk = want;
2040         if (!wlc_hw->sbclk) {
2041                 wlc_hw->clk = false;
2042                 if (wlc_hw->band && wlc_hw->band->pi)
2043                         wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
2044         }
2045 }
2046
2047 /*
2048  * Return true if radio is disabled, otherwise false.
2049  * hw radio disable signal is an external pin, users activate it asynchronously
2050  * this function could be called when driver is down and w/o clock
2051  * it operates on different registers depending on corerev and boardflag.
2052  */
2053 static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
2054 {
2055         bool v, clk, xtal;
2056         u32 resetbits = 0, flags = 0;
2057
2058         xtal = wlc_hw->sbclk;
2059         if (!xtal)
2060                 brcms_b_xtal(wlc_hw, ON);
2061
2062         /* may need to take core out of reset first */
2063         clk = wlc_hw->clk;
2064         if (!clk) {
2065                 /*
2066                  * mac no longer enables phyclk automatically when driver
2067                  * accesses phyreg throughput mac. This can be skipped since
2068                  * only mac reg is accessed below
2069                  */
2070                 flags |= SICF_PCLKE;
2071
2072                 /*
2073                  * AI chip doesn't restore bar0win2 on
2074                  * hibernation/resume, need sw fixup
2075                  */
2076                 if ((wlc_hw->sih->chip == BCM43224_CHIP_ID) ||
2077                     (wlc_hw->sih->chip == BCM43225_CHIP_ID))
2078                         wlc_hw->regs = (struct d11regs __iomem *)
2079                                         ai_setcore(wlc_hw->sih, D11_CORE_ID, 0);
2080                 ai_core_reset(wlc_hw->sih, flags, resetbits);
2081                 brcms_c_mctrl_reset(wlc_hw);
2082         }
2083
2084         v = ((R_REG(&wlc_hw->regs->phydebug) & PDBG_RFD) != 0);
2085
2086         /* put core back into reset */
2087         if (!clk)
2088                 ai_core_disable(wlc_hw->sih, 0);
2089
2090         if (!xtal)
2091                 brcms_b_xtal(wlc_hw, OFF);
2092
2093         return v;
2094 }
2095
2096 static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
2097 {
2098         struct dma_pub *di = wlc_hw->di[fifo];
2099         return dma_rxreset(di);
2100 }
2101
2102 /* d11 core reset
2103  *   ensure fask clock during reset
2104  *   reset dma
2105  *   reset d11(out of reset)
2106  *   reset phy(out of reset)
2107  *   clear software macintstatus for fresh new start
2108  * one testing hack wlc_hw->noreset will bypass the d11/phy reset
2109  */
2110 void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
2111 {
2112         struct d11regs __iomem *regs;
2113         uint i;
2114         bool fastclk;
2115         u32 resetbits = 0;
2116
2117         if (flags == BRCMS_USE_COREFLAGS)
2118                 flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
2119
2120         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
2121
2122         regs = wlc_hw->regs;
2123
2124         /* request FAST clock if not on  */
2125         fastclk = wlc_hw->forcefastclk;
2126         if (!fastclk)
2127                 brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
2128
2129         /* reset the dma engines except first time thru */
2130         if (ai_iscoreup(wlc_hw->sih)) {
2131                 for (i = 0; i < NFIFO; i++)
2132                         if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
2133                                 wiphy_err(wlc_hw->wlc->wiphy, "wl%d: %s: "
2134                                           "dma_txreset[%d]: cannot stop dma\n",
2135                                            wlc_hw->unit, __func__, i);
2136
2137                 if ((wlc_hw->di[RX_FIFO])
2138                     && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
2139                         wiphy_err(wlc_hw->wlc->wiphy, "wl%d: %s: dma_rxreset"
2140                                   "[%d]: cannot stop dma\n",
2141                                   wlc_hw->unit, __func__, RX_FIFO);
2142         }
2143         /* if noreset, just stop the psm and return */
2144         if (wlc_hw->noreset) {
2145                 wlc_hw->wlc->macintstatus = 0;  /* skip wl_dpc after down */
2146                 brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
2147                 return;
2148         }
2149
2150         /*
2151          * mac no longer enables phyclk automatically when driver accesses
2152          * phyreg throughput mac, AND phy_reset is skipped at early stage when
2153          * band->pi is invalid. need to enable PHY CLK
2154          */
2155         flags |= SICF_PCLKE;
2156
2157         /*
2158          * reset the core
2159          * In chips with PMU, the fastclk request goes through d11 core
2160          * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
2161          *
2162          * This adds some delay and we can optimize it by also requesting
2163          * fastclk through chipcommon during this period if necessary. But
2164          * that has to work coordinate with other driver like mips/arm since
2165          * they may touch chipcommon as well.
2166          */
2167         wlc_hw->clk = false;
2168         ai_core_reset(wlc_hw->sih, flags, resetbits);
2169         wlc_hw->clk = true;
2170         if (wlc_hw->band && wlc_hw->band->pi)
2171                 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
2172
2173         brcms_c_mctrl_reset(wlc_hw);
2174
2175         if (wlc_hw->sih->cccaps & CC_CAP_PMU)
2176                 brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
2177
2178         brcms_b_phy_reset(wlc_hw);
2179
2180         /* turn on PHY_PLL */
2181         brcms_b_core_phypll_ctl(wlc_hw, true);
2182
2183         /* clear sw intstatus */
2184         wlc_hw->wlc->macintstatus = 0;
2185
2186         /* restore the clk setting */
2187         if (!fastclk)
2188                 brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
2189 }
2190
2191 /* txfifo sizes needs to be modified(increased) since the newer cores
2192  * have more memory.
2193  */
2194 static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
2195 {
2196         struct d11regs __iomem *regs = wlc_hw->regs;
2197         u16 fifo_nu;
2198         u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
2199         u16 txfifo_def, txfifo_def1;
2200         u16 txfifo_cmd;
2201
2202         /* tx fifos start at TXFIFO_START_BLK from the Base address */
2203         txfifo_startblk = TXFIFO_START_BLK;
2204
2205         /* sequence of operations:  reset fifo, set fifo size, reset fifo */
2206         for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
2207
2208                 txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
2209                 txfifo_def = (txfifo_startblk & 0xff) |
2210                     (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
2211                 txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
2212                     ((((txfifo_endblk -
2213                         1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
2214                 txfifo_cmd =
2215                     TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
2216
2217                 W_REG(&regs->xmtfifocmd, txfifo_cmd);
2218                 W_REG(&regs->xmtfifodef, txfifo_def);
2219                 W_REG(&regs->xmtfifodef1, txfifo_def1);
2220
2221                 W_REG(&regs->xmtfifocmd, txfifo_cmd);
2222
2223                 txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
2224         }
2225         /*
2226          * need to propagate to shm location to be in sync since ucode/hw won't
2227          * do this
2228          */
2229         brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
2230                            wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
2231         brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
2232                            wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
2233         brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
2234                            ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
2235                             xmtfifo_sz[TX_AC_BK_FIFO]));
2236         brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
2237                            ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
2238                             xmtfifo_sz[TX_BCMC_FIFO]));
2239 }
2240
2241 /* This function is used for changing the tsf frac register
2242  * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
2243  * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
2244  * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
2245  * HTPHY Formula is 2^26/freq(MHz) e.g.
2246  * For spuron2 - 126MHz -> 2^26/126 = 532610.0
2247  *  - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
2248  * For spuron: 123MHz -> 2^26/123    = 545600.5
2249  *  - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
2250  * For spur off: 120MHz -> 2^26/120    = 559240.5
2251  *  - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
2252  */
2253
2254 void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
2255 {
2256         struct d11regs __iomem *regs = wlc_hw->regs;
2257
2258         if ((wlc_hw->sih->chip == BCM43224_CHIP_ID) ||
2259             (wlc_hw->sih->chip == BCM43225_CHIP_ID)) {
2260                 if (spurmode == WL_SPURAVOID_ON2) {     /* 126Mhz */
2261                         W_REG(&regs->tsf_clk_frac_l, 0x2082);
2262                         W_REG(&regs->tsf_clk_frac_h, 0x8);
2263                 } else if (spurmode == WL_SPURAVOID_ON1) {      /* 123Mhz */
2264                         W_REG(&regs->tsf_clk_frac_l, 0x5341);
2265                         W_REG(&regs->tsf_clk_frac_h, 0x8);
2266                 } else {        /* 120Mhz */
2267                         W_REG(&regs->tsf_clk_frac_l, 0x8889);
2268                         W_REG(&regs->tsf_clk_frac_h, 0x8);
2269                 }
2270         } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2271                 if (spurmode == WL_SPURAVOID_ON1) {     /* 82Mhz */
2272                         W_REG(&regs->tsf_clk_frac_l, 0x7CE0);
2273                         W_REG(&regs->tsf_clk_frac_h, 0xC);
2274                 } else {        /* 80Mhz */
2275                         W_REG(&regs->tsf_clk_frac_l, 0xCCCD);
2276                         W_REG(&regs->tsf_clk_frac_h, 0xC);
2277                 }
2278         }
2279 }
2280
2281 /* Initialize GPIOs that are controlled by D11 core */
2282 static void brcms_c_gpio_init(struct brcms_c_info *wlc)
2283 {
2284         struct brcms_hardware *wlc_hw = wlc->hw;
2285         struct d11regs __iomem *regs;
2286         u32 gc, gm;
2287
2288         regs = wlc_hw->regs;
2289
2290         /* use GPIO select 0 to get all gpio signals from the gpio out reg */
2291         brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
2292
2293         /*
2294          * Common GPIO setup:
2295          *      G0 = LED 0 = WLAN Activity
2296          *      G1 = LED 1 = WLAN 2.4 GHz Radio State
2297          *      G2 = LED 2 = WLAN 5 GHz Radio State
2298          *      G4 = radio disable input (HI enabled, LO disabled)
2299          */
2300
2301         gc = gm = 0;
2302
2303         /* Allocate GPIOs for mimo antenna diversity feature */
2304         if (wlc_hw->antsel_type == ANTSEL_2x3) {
2305                 /* Enable antenna diversity, use 2x3 mode */
2306                 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2307                              MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2308                 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
2309                              MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
2310
2311                 /* init superswitch control */
2312                 wlc_phy_antsel_init(wlc_hw->band->pi, false);
2313
2314         } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
2315                 gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
2316                 /*
2317                  * The board itself is powered by these GPIOs
2318                  * (when not sending pattern) so set them high
2319                  */
2320                 OR_REG(&regs->psm_gpio_oe,
2321                        (BOARD_GPIO_12 | BOARD_GPIO_13));
2322                 OR_REG(&regs->psm_gpio_out,
2323                        (BOARD_GPIO_12 | BOARD_GPIO_13));
2324
2325                 /* Enable antenna diversity, use 2x4 mode */
2326                 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2327                              MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2328                 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
2329                              BRCM_BAND_ALL);
2330
2331                 /* Configure the desired clock to be 4Mhz */
2332                 brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
2333                                    ANTSEL_CLKDIV_4MHZ);
2334         }
2335
2336         /*
2337          * gpio 9 controls the PA. ucode is responsible
2338          * for wiggling out and oe
2339          */
2340         if (wlc_hw->boardflags & BFL_PACTRL)
2341                 gm |= gc |= BOARD_GPIO_PACTRL;
2342
2343         /* apply to gpiocontrol register */
2344         ai_gpiocontrol(wlc_hw->sih, gm, gc, GPIO_DRV_PRIORITY);
2345 }
2346
2347 static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
2348                               const __le32 ucode[], const size_t nbytes)
2349 {
2350         struct d11regs __iomem *regs = wlc_hw->regs;
2351         uint i;
2352         uint count;
2353
2354         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
2355
2356         count = (nbytes / sizeof(u32));
2357
2358         W_REG(&regs->objaddr, (OBJADDR_AUTO_INC | OBJADDR_UCM_SEL));
2359         (void)R_REG(&regs->objaddr);
2360         for (i = 0; i < count; i++)
2361                 W_REG(&regs->objdata, le32_to_cpu(ucode[i]));
2362
2363 }
2364
2365 static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
2366 {
2367         struct brcms_c_info *wlc;
2368         struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
2369
2370         wlc = wlc_hw->wlc;
2371
2372         if (wlc_hw->ucode_loaded)
2373                 return;
2374
2375         if (D11REV_IS(wlc_hw->corerev, 23)) {
2376                 if (BRCMS_ISNPHY(wlc_hw->band)) {
2377                         brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
2378                                           ucode->bcm43xx_16_mimosz);
2379                         wlc_hw->ucode_loaded = true;
2380                 } else
2381                         wiphy_err(wlc->wiphy, "%s: wl%d: unsupported phy in "
2382                                   "corerev %d\n",
2383                                   __func__, wlc_hw->unit, wlc_hw->corerev);
2384         } else if (D11REV_IS(wlc_hw->corerev, 24)) {
2385                 if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2386                         brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
2387                                           ucode->bcm43xx_24_lcnsz);
2388                         wlc_hw->ucode_loaded = true;
2389                 } else {
2390                         wiphy_err(wlc->wiphy, "%s: wl%d: unsupported phy in "
2391                                   "corerev %d\n",
2392                                   __func__, wlc_hw->unit, wlc_hw->corerev);
2393                 }
2394         }
2395 }
2396
2397 void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
2398 {
2399         /* update sw state */
2400         wlc_hw->bmac_phytxant = phytxant;
2401
2402         /* push to ucode if up */
2403         if (!wlc_hw->up)
2404                 return;
2405         brcms_c_ucode_txant_set(wlc_hw);
2406
2407 }
2408
2409 u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
2410 {
2411         return (u16) wlc_hw->wlc->stf->txant;
2412 }
2413
2414 void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
2415 {
2416         wlc_hw->antsel_type = antsel_type;
2417
2418         /* Update the antsel type for phy module to use */
2419         wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
2420 }
2421
2422 static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
2423 {
2424         bool fatal = false;
2425         uint unit;
2426         uint intstatus, idx;
2427         struct d11regs __iomem *regs = wlc_hw->regs;
2428         struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2429
2430         unit = wlc_hw->unit;
2431
2432         for (idx = 0; idx < NFIFO; idx++) {
2433                 /* read intstatus register and ignore any non-error bits */
2434                 intstatus =
2435                     R_REG(&regs->intctrlregs[idx].intstatus) & I_ERRORS;
2436                 if (!intstatus)
2437                         continue;
2438
2439                 BCMMSG(wlc_hw->wlc->wiphy, "wl%d: intstatus%d 0x%x\n",
2440                         unit, idx, intstatus);
2441
2442                 if (intstatus & I_RO) {
2443                         wiphy_err(wiphy, "wl%d: fifo %d: receive fifo "
2444                                   "overflow\n", unit, idx);
2445                         fatal = true;
2446                 }
2447
2448                 if (intstatus & I_PC) {
2449                         wiphy_err(wiphy, "wl%d: fifo %d: descriptor error\n",
2450                                  unit, idx);
2451                         fatal = true;
2452                 }
2453
2454                 if (intstatus & I_PD) {
2455                         wiphy_err(wiphy, "wl%d: fifo %d: data error\n", unit,
2456                                   idx);
2457                         fatal = true;
2458                 }
2459
2460                 if (intstatus & I_DE) {
2461                         wiphy_err(wiphy, "wl%d: fifo %d: descriptor protocol "
2462                                   "error\n", unit, idx);
2463                         fatal = true;
2464                 }
2465
2466                 if (intstatus & I_RU)
2467                         wiphy_err(wiphy, "wl%d: fifo %d: receive descriptor "
2468                                   "underflow\n", idx, unit);
2469
2470                 if (intstatus & I_XU) {
2471                         wiphy_err(wiphy, "wl%d: fifo %d: transmit fifo "
2472                                   "underflow\n", idx, unit);
2473                         fatal = true;
2474                 }
2475
2476                 if (fatal) {
2477                         brcms_c_fatal_error(wlc_hw->wlc);       /* big hammer */
2478                         break;
2479                 } else
2480                         W_REG(&regs->intctrlregs[idx].intstatus,
2481                               intstatus);
2482         }
2483 }
2484
2485 void brcms_c_intrson(struct brcms_c_info *wlc)
2486 {
2487         struct brcms_hardware *wlc_hw = wlc->hw;
2488         wlc->macintmask = wlc->defmacintmask;
2489         W_REG(&wlc_hw->regs->macintmask, wlc->macintmask);
2490 }
2491
2492 /*
2493  * callback for siutils.c, which has only wlc handler, no wl they both check
2494  * up, not only because there is no need to off/restore d11 interrupt but also
2495  * because per-port code may require sync with valid interrupt.
2496  */
2497 static u32 brcms_c_wlintrsoff(struct brcms_c_info *wlc)
2498 {
2499         if (!wlc->hw->up)
2500                 return 0;
2501
2502         return brcms_intrsoff(wlc->wl);
2503 }
2504
2505 static void brcms_c_wlintrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2506 {
2507         if (!wlc->hw->up)
2508                 return;
2509
2510         brcms_intrsrestore(wlc->wl, macintmask);
2511 }
2512
2513 u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
2514 {
2515         struct brcms_hardware *wlc_hw = wlc->hw;
2516         u32 macintmask;
2517
2518         if (!wlc_hw->clk)
2519                 return 0;
2520
2521         macintmask = wlc->macintmask;   /* isr can still happen */
2522
2523         W_REG(&wlc_hw->regs->macintmask, 0);
2524         (void)R_REG(&wlc_hw->regs->macintmask); /* sync readback */
2525         udelay(1);              /* ensure int line is no longer driven */
2526         wlc->macintmask = 0;
2527
2528         /* return previous macintmask; resolve race between us and our isr */
2529         return wlc->macintstatus ? 0 : macintmask;
2530 }
2531
2532 void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2533 {
2534         struct brcms_hardware *wlc_hw = wlc->hw;
2535         if (!wlc_hw->clk)
2536                 return;
2537
2538         wlc->macintmask = macintmask;
2539         W_REG(&wlc_hw->regs->macintmask, wlc->macintmask);
2540 }
2541
2542 static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
2543                                     uint tx_fifo)
2544 {
2545         u8 fifo = 1 << tx_fifo;
2546
2547         /* Two clients of this code, 11h Quiet period and scanning. */
2548
2549         /* only suspend if not already suspended */
2550         if ((wlc_hw->suspended_fifos & fifo) == fifo)
2551                 return;
2552
2553         /* force the core awake only if not already */
2554         if (wlc_hw->suspended_fifos == 0)
2555                 brcms_c_ucode_wake_override_set(wlc_hw,
2556                                                 BRCMS_WAKE_OVERRIDE_TXFIFO);
2557
2558         wlc_hw->suspended_fifos |= fifo;
2559
2560         if (wlc_hw->di[tx_fifo]) {
2561                 /*
2562                  * Suspending AMPDU transmissions in the middle can cause
2563                  * underflow which may result in mismatch between ucode and
2564                  * driver so suspend the mac before suspending the FIFO
2565                  */
2566                 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2567                         brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
2568
2569                 dma_txsuspend(wlc_hw->di[tx_fifo]);
2570
2571                 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2572                         brcms_c_enable_mac(wlc_hw->wlc);
2573         }
2574 }
2575
2576 static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
2577                                    uint tx_fifo)
2578 {
2579         /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
2580          * but need to be done here for PIO otherwise the watchdog will catch
2581          * the inconsistency and fire
2582          */
2583         /* Two clients of this code, 11h Quiet period and scanning. */
2584         if (wlc_hw->di[tx_fifo])
2585                 dma_txresume(wlc_hw->di[tx_fifo]);
2586
2587         /* allow core to sleep again */
2588         if (wlc_hw->suspended_fifos == 0)
2589                 return;
2590         else {
2591                 wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
2592                 if (wlc_hw->suspended_fifos == 0)
2593                         brcms_c_ucode_wake_override_clear(wlc_hw,
2594                                                 BRCMS_WAKE_OVERRIDE_TXFIFO);
2595         }
2596 }
2597
2598 static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool on, u32 flags)
2599 {
2600         const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
2601
2602         if (on) {
2603                 /* suspend tx fifos */
2604                 brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
2605                 brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
2606                 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
2607                 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
2608
2609                 /* zero the address match register so we do not send ACKs */
2610                 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
2611                                        null_ether_addr);
2612         } else {
2613                 /* resume tx fifos */
2614                 brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
2615                 brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
2616                 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
2617                 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
2618
2619                 /* Restore address */
2620                 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
2621                                        wlc_hw->etheraddr);
2622         }
2623
2624         wlc_phy_mute_upd(wlc_hw->band->pi, on, flags);
2625
2626         if (on)
2627                 brcms_c_ucode_mute_override_set(wlc_hw);
2628         else
2629                 brcms_c_ucode_mute_override_clear(wlc_hw);
2630 }
2631
2632 /*
2633  * Read and clear macintmask and macintstatus and intstatus registers.
2634  * This routine should be called with interrupts off
2635  * Return:
2636  *   -1 if brcms_deviceremoved(wlc) evaluates to true;
2637  *   0 if the interrupt is not for us, or we are in some special cases;
2638  *   device interrupt status bits otherwise.
2639  */
2640 static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
2641 {
2642         struct brcms_hardware *wlc_hw = wlc->hw;
2643         struct d11regs __iomem *regs = wlc_hw->regs;
2644         u32 macintstatus;
2645
2646         /* macintstatus includes a DMA interrupt summary bit */
2647         macintstatus = R_REG(&regs->macintstatus);
2648
2649         BCMMSG(wlc->wiphy, "wl%d: macintstatus: 0x%x\n", wlc_hw->unit,
2650                  macintstatus);
2651
2652         /* detect cardbus removed, in power down(suspend) and in reset */
2653         if (brcms_deviceremoved(wlc))
2654                 return -1;
2655
2656         /* brcms_deviceremoved() succeeds even when the core is still resetting,
2657          * handle that case here.
2658          */
2659         if (macintstatus == 0xffffffff)
2660                 return 0;
2661
2662         /* defer unsolicited interrupts */
2663         macintstatus &= (in_isr ? wlc->macintmask : wlc->defmacintmask);
2664
2665         /* if not for us */
2666         if (macintstatus == 0)
2667                 return 0;
2668
2669         /* interrupts are already turned off for CFE build
2670          * Caution: For CFE Turning off the interrupts again has some undesired
2671          * consequences
2672          */
2673         /* turn off the interrupts */
2674         W_REG(&regs->macintmask, 0);
2675         (void)R_REG(&regs->macintmask); /* sync readback */
2676         wlc->macintmask = 0;
2677
2678         /* clear device interrupts */
2679         W_REG(&regs->macintstatus, macintstatus);
2680
2681         /* MI_DMAINT is indication of non-zero intstatus */
2682         if (macintstatus & MI_DMAINT)
2683                 /*
2684                  * only fifo interrupt enabled is I_RI in
2685                  * RX_FIFO. If MI_DMAINT is set, assume it
2686                  * is set and clear the interrupt.
2687                  */
2688                 W_REG(&regs->intctrlregs[RX_FIFO].intstatus,
2689                       DEF_RXINTMASK);
2690
2691         return macintstatus;
2692 }
2693
2694 /* Update wlc->macintstatus and wlc->intstatus[]. */
2695 /* Return true if they are updated successfully. false otherwise */
2696 bool brcms_c_intrsupd(struct brcms_c_info *wlc)
2697 {
2698         u32 macintstatus;
2699
2700         /* read and clear macintstatus and intstatus registers */
2701         macintstatus = wlc_intstatus(wlc, false);
2702
2703         /* device is removed */
2704         if (macintstatus == 0xffffffff)
2705                 return false;
2706
2707         /* update interrupt status in software */
2708         wlc->macintstatus |= macintstatus;
2709
2710         return true;
2711 }
2712
2713 /*
2714  * First-level interrupt processing.
2715  * Return true if this was our interrupt, false otherwise.
2716  * *wantdpc will be set to true if further brcms_c_dpc() processing is required,
2717  * false otherwise.
2718  */
2719 bool brcms_c_isr(struct brcms_c_info *wlc, bool *wantdpc)
2720 {
2721         struct brcms_hardware *wlc_hw = wlc->hw;
2722         u32 macintstatus;
2723
2724         *wantdpc = false;
2725
2726         if (!wlc_hw->up || !wlc->macintmask)
2727                 return false;
2728
2729         /* read and clear macintstatus and intstatus registers */
2730         macintstatus = wlc_intstatus(wlc, true);
2731
2732         if (macintstatus == 0xffffffff)
2733                 wiphy_err(wlc->wiphy, "DEVICEREMOVED detected in the ISR code"
2734                           " path\n");
2735
2736         /* it is not for us */
2737         if (macintstatus == 0)
2738                 return false;
2739
2740         *wantdpc = true;
2741
2742         /* save interrupt status bits */
2743         wlc->macintstatus = macintstatus;
2744
2745         return true;
2746
2747 }
2748
2749 void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
2750 {
2751         struct brcms_hardware *wlc_hw = wlc->hw;
2752         struct d11regs __iomem *regs = wlc_hw->regs;
2753         u32 mc, mi;
2754         struct wiphy *wiphy = wlc->wiphy;
2755
2756         BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
2757                 wlc_hw->band->bandunit);
2758
2759         /*
2760          * Track overlapping suspend requests
2761          */
2762         wlc_hw->mac_suspend_depth++;
2763         if (wlc_hw->mac_suspend_depth > 1)
2764                 return;
2765
2766         /* force the core awake */
2767         brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2768
2769         mc = R_REG(&regs->maccontrol);
2770
2771         if (mc == 0xffffffff) {
2772                 wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
2773                           __func__);
2774                 brcms_down(wlc->wl);
2775                 return;
2776         }
2777         WARN_ON(mc & MCTL_PSM_JMP_0);
2778         WARN_ON(!(mc & MCTL_PSM_RUN));
2779         WARN_ON(!(mc & MCTL_EN_MAC));
2780
2781         mi = R_REG(&regs->macintstatus);
2782         if (mi == 0xffffffff) {
2783                 wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
2784                           __func__);
2785                 brcms_down(wlc->wl);
2786                 return;
2787         }
2788         WARN_ON(mi & MI_MACSSPNDD);
2789
2790         brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
2791
2792         SPINWAIT(!(R_REG(&regs->macintstatus) & MI_MACSSPNDD),
2793                  BRCMS_MAX_MAC_SUSPEND);
2794
2795         if (!(R_REG(&regs->macintstatus) & MI_MACSSPNDD)) {
2796                 wiphy_err(wiphy, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
2797                           " and MI_MACSSPNDD is still not on.\n",
2798                           wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
2799                 wiphy_err(wiphy, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
2800                           "psm_brc 0x%04x\n", wlc_hw->unit,
2801                           R_REG(&regs->psmdebug),
2802                           R_REG(&regs->phydebug),
2803                           R_REG(&regs->psm_brc));
2804         }
2805
2806         mc = R_REG(&regs->maccontrol);
2807         if (mc == 0xffffffff) {
2808                 wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
2809                           __func__);
2810                 brcms_down(wlc->wl);
2811                 return;
2812         }
2813         WARN_ON(mc & MCTL_PSM_JMP_0);
2814         WARN_ON(!(mc & MCTL_PSM_RUN));
2815         WARN_ON(mc & MCTL_EN_MAC);
2816 }
2817
2818 void brcms_c_enable_mac(struct brcms_c_info *wlc)
2819 {
2820         struct brcms_hardware *wlc_hw = wlc->hw;
2821         struct d11regs __iomem *regs = wlc_hw->regs;
2822         u32 mc, mi;
2823
2824         BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
2825                 wlc->band->bandunit);
2826
2827         /*
2828          * Track overlapping suspend requests
2829          */
2830         wlc_hw->mac_suspend_depth--;
2831         if (wlc_hw->mac_suspend_depth > 0)
2832                 return;
2833
2834         mc = R_REG(&regs->maccontrol);
2835         WARN_ON(mc & MCTL_PSM_JMP_0);
2836         WARN_ON(mc & MCTL_EN_MAC);
2837         WARN_ON(!(mc & MCTL_PSM_RUN));
2838
2839         brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
2840         W_REG(&regs->macintstatus, MI_MACSSPNDD);
2841
2842         mc = R_REG(&regs->maccontrol);
2843         WARN_ON(mc & MCTL_PSM_JMP_0);
2844         WARN_ON(!(mc & MCTL_EN_MAC));
2845         WARN_ON(!(mc & MCTL_PSM_RUN));
2846
2847         mi = R_REG(&regs->macintstatus);
2848         WARN_ON(mi & MI_MACSSPNDD);
2849
2850         brcms_c_ucode_wake_override_clear(wlc_hw,
2851                                           BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2852 }
2853
2854 void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
2855 {
2856         wlc_hw->hw_stf_ss_opmode = stf_mode;
2857
2858         if (wlc_hw->clk)
2859                 brcms_upd_ofdm_pctl1_table(wlc_hw);
2860 }
2861
2862 static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
2863 {
2864         struct d11regs __iomem *regs;
2865         u32 w, val;
2866         struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2867
2868         BCMMSG(wiphy, "wl%d\n", wlc_hw->unit);
2869
2870         regs = wlc_hw->regs;
2871
2872         /* Validate dchip register access */
2873
2874         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2875         (void)R_REG(&regs->objaddr);
2876         w = R_REG(&regs->objdata);
2877
2878         /* Can we write and read back a 32bit register? */
2879         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2880         (void)R_REG(&regs->objaddr);
2881         W_REG(&regs->objdata, (u32) 0xaa5555aa);
2882
2883         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2884         (void)R_REG(&regs->objaddr);
2885         val = R_REG(&regs->objdata);
2886         if (val != (u32) 0xaa5555aa) {
2887                 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2888                           "expected 0xaa5555aa\n", wlc_hw->unit, val);
2889                 return false;
2890         }
2891
2892         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2893         (void)R_REG(&regs->objaddr);
2894         W_REG(&regs->objdata, (u32) 0x55aaaa55);
2895
2896         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2897         (void)R_REG(&regs->objaddr);
2898         val = R_REG(&regs->objdata);
2899         if (val != (u32) 0x55aaaa55) {
2900                 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2901                           "expected 0x55aaaa55\n", wlc_hw->unit, val);
2902                 return false;
2903         }
2904
2905         W_REG(&regs->objaddr, OBJADDR_SHM_SEL | 0);
2906         (void)R_REG(&regs->objaddr);
2907         W_REG(&regs->objdata, w);
2908
2909         /* clear CFPStart */
2910         W_REG(&regs->tsf_cfpstart, 0);
2911
2912         w = R_REG(&regs->maccontrol);
2913         if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
2914             (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
2915                 wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
2916                           "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
2917                           (MCTL_IHR_EN | MCTL_WAKE),
2918                           (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
2919                 return false;
2920         }
2921
2922         return true;
2923 }
2924
2925 #define PHYPLL_WAIT_US  100000
2926
2927 void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
2928 {
2929         struct d11regs __iomem *regs;
2930         u32 tmp;
2931
2932         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
2933
2934         tmp = 0;
2935         regs = wlc_hw->regs;
2936
2937         if (on) {
2938                 if ((wlc_hw->sih->chip == BCM4313_CHIP_ID)) {
2939                         OR_REG(&regs->clk_ctl_st,
2940                                (CCS_ERSRC_REQ_HT | CCS_ERSRC_REQ_D11PLL |
2941                                 CCS_ERSRC_REQ_PHYPLL));
2942                         SPINWAIT((R_REG(&regs->clk_ctl_st) &
2943                                   (CCS_ERSRC_AVAIL_HT)) != (CCS_ERSRC_AVAIL_HT),
2944                                  PHYPLL_WAIT_US);
2945
2946                         tmp = R_REG(&regs->clk_ctl_st);
2947                         if ((tmp & (CCS_ERSRC_AVAIL_HT)) !=
2948                             (CCS_ERSRC_AVAIL_HT))
2949                                 wiphy_err(wlc_hw->wlc->wiphy, "%s: turn on PHY"
2950                                           " PLL failed\n", __func__);
2951                 } else {
2952                         OR_REG(&regs->clk_ctl_st,
2953                                (CCS_ERSRC_REQ_D11PLL | CCS_ERSRC_REQ_PHYPLL));
2954                         SPINWAIT((R_REG(&regs->clk_ctl_st) &
2955                                   (CCS_ERSRC_AVAIL_D11PLL |
2956                                    CCS_ERSRC_AVAIL_PHYPLL)) !=
2957                                  (CCS_ERSRC_AVAIL_D11PLL |
2958                                   CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
2959
2960                         tmp = R_REG(&regs->clk_ctl_st);
2961                         if ((tmp &
2962                              (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2963                             !=
2964                             (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2965                                 wiphy_err(wlc_hw->wlc->wiphy, "%s: turn on "
2966                                           "PHY PLL failed\n", __func__);
2967                 }
2968         } else {
2969                 /*
2970                  * Since the PLL may be shared, other cores can still
2971                  * be requesting it; so we'll deassert the request but
2972                  * not wait for status to comply.
2973                  */
2974                 AND_REG(&regs->clk_ctl_st, ~CCS_ERSRC_REQ_PHYPLL);
2975                 tmp = R_REG(&regs->clk_ctl_st);
2976         }
2977 }
2978
2979 void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
2980 {
2981         bool dev_gone;
2982
2983         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
2984
2985         dev_gone = brcms_deviceremoved(wlc_hw->wlc);
2986
2987         if (dev_gone)
2988                 return;
2989
2990         if (wlc_hw->noreset)
2991                 return;
2992
2993         /* radio off */
2994         wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
2995
2996         /* turn off analog core */
2997         wlc_phy_anacore(wlc_hw->band->pi, OFF);
2998
2999         /* turn off PHYPLL to save power */
3000         brcms_b_core_phypll_ctl(wlc_hw, false);
3001
3002         wlc_hw->clk = false;
3003         ai_core_disable(wlc_hw->sih, 0);
3004         wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
3005 }
3006
3007 static void brcms_c_flushqueues(struct brcms_c_info *wlc)
3008 {
3009         struct brcms_hardware *wlc_hw = wlc->hw;
3010         uint i;
3011
3012         /* free any posted tx packets */
3013         for (i = 0; i < NFIFO; i++)
3014                 if (wlc_hw->di[i]) {
3015                         dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
3016                         wlc->core->txpktpend[i] = 0;
3017                         BCMMSG(wlc->wiphy, "pktpend fifo %d clrd\n", i);
3018                 }
3019
3020         /* free any posted rx packets */
3021         dma_rxreclaim(wlc_hw->di[RX_FIFO]);
3022 }
3023
3024 static u16
3025 brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
3026 {
3027         struct d11regs __iomem *regs = wlc_hw->regs;
3028         u16 __iomem *objdata_lo = (u16 __iomem *)&regs->objdata;
3029         u16 __iomem *objdata_hi = objdata_lo + 1;
3030         u16 v;
3031
3032         W_REG(&regs->objaddr, sel | (offset >> 2));
3033         (void)R_REG(&regs->objaddr);
3034         if (offset & 2)
3035                 v = R_REG(objdata_hi);
3036         else
3037                 v = R_REG(objdata_lo);
3038
3039         return v;
3040 }
3041
3042 static void
3043 brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
3044                      u32 sel)
3045 {
3046         struct d11regs __iomem *regs = wlc_hw->regs;
3047         u16 __iomem *objdata_lo = (u16 __iomem *)&regs->objdata;
3048         u16 __iomem *objdata_hi = objdata_lo + 1;
3049
3050         W_REG(&regs->objaddr, sel | (offset >> 2));
3051         (void)R_REG(&regs->objaddr);
3052         if (offset & 2)
3053                 W_REG(objdata_hi, v);
3054         else
3055                 W_REG(objdata_lo, v);
3056 }
3057
3058 /*
3059  * Read a single u16 from shared memory.
3060  * SHM 'offset' needs to be an even address
3061  */
3062 u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
3063 {
3064         return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
3065 }
3066
3067 /*
3068  * Write a single u16 to shared memory.
3069  * SHM 'offset' needs to be an even address
3070  */
3071 void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
3072 {
3073         brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
3074 }
3075
3076 /*
3077  * Copy a buffer to shared memory of specified type .
3078  * SHM 'offset' needs to be an even address and
3079  * Buffer length 'len' must be an even number of bytes
3080  * 'sel' selects the type of memory
3081  */
3082 void
3083 brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
3084                       const void *buf, int len, u32 sel)
3085 {
3086         u16 v;
3087         const u8 *p = (const u8 *)buf;
3088         int i;
3089
3090         if (len <= 0 || (offset & 1) || (len & 1))
3091                 return;
3092
3093         for (i = 0; i < len; i += 2) {
3094                 v = p[i] | (p[i + 1] << 8);
3095                 brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
3096         }
3097 }
3098
3099 /*
3100  * Copy a piece of shared memory of specified type to a buffer .
3101  * SHM 'offset' needs to be an even address and
3102  * Buffer length 'len' must be an even number of bytes
3103  * 'sel' selects the type of memory
3104  */
3105 void
3106 brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
3107                          int len, u32 sel)
3108 {
3109         u16 v;
3110         u8 *p = (u8 *) buf;
3111         int i;
3112
3113         if (len <= 0 || (offset & 1) || (len & 1))
3114                 return;
3115
3116         for (i = 0; i < len; i += 2) {
3117                 v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
3118                 p[i] = v & 0xFF;
3119                 p[i + 1] = (v >> 8) & 0xFF;
3120         }
3121 }
3122
3123 static void brcms_b_copyfrom_vars(struct brcms_hardware *wlc_hw, char **buf,
3124                            uint *len)
3125 {
3126         BCMMSG(wlc_hw->wlc->wiphy, "nvram vars totlen=%d\n",
3127                 wlc_hw->vars_size);
3128
3129         *buf = wlc_hw->vars;
3130         *len = wlc_hw->vars_size;
3131 }
3132
3133 static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
3134                                    u16 SRL, u16 LRL)
3135 {
3136         wlc_hw->SRL = SRL;
3137         wlc_hw->LRL = LRL;
3138
3139         /* write retry limit to SCR, shouldn't need to suspend */
3140         if (wlc_hw->up) {
3141                 W_REG(&wlc_hw->regs->objaddr,
3142                       OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3143                 (void)R_REG(&wlc_hw->regs->objaddr);
3144                 W_REG(&wlc_hw->regs->objdata, wlc_hw->SRL);
3145                 W_REG(&wlc_hw->regs->objaddr,
3146                       OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3147                 (void)R_REG(&wlc_hw->regs->objaddr);
3148                 W_REG(&wlc_hw->regs->objdata, wlc_hw->LRL);
3149         }
3150 }
3151
3152 static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
3153 {
3154         if (set) {
3155                 if (mboolisset(wlc_hw->pllreq, req_bit))
3156                         return;
3157
3158                 mboolset(wlc_hw->pllreq, req_bit);
3159
3160                 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3161                         if (!wlc_hw->sbclk)
3162                                 brcms_b_xtal(wlc_hw, ON);
3163                 }
3164         } else {
3165                 if (!mboolisset(wlc_hw->pllreq, req_bit))
3166                         return;
3167
3168                 mboolclr(wlc_hw->pllreq, req_bit);
3169
3170                 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3171                         if (wlc_hw->sbclk)
3172                                 brcms_b_xtal(wlc_hw, OFF);
3173                 }
3174         }
3175 }
3176
3177 static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
3178 {
3179         wlc_hw->antsel_avail = antsel_avail;
3180 }
3181
3182 /*
3183  * conditions under which the PM bit should be set in outgoing frames
3184  * and STAY_AWAKE is meaningful
3185  */
3186 bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
3187 {
3188         struct brcms_bss_cfg *cfg = wlc->bsscfg;
3189
3190         /* disallow PS when one of the following global conditions meets */
3191         if (!wlc->pub->associated)
3192                 return false;
3193
3194         /* disallow PS when one of these meets when not scanning */
3195         if (wlc->monitor)
3196                 return false;
3197
3198         if (cfg->associated) {
3199                 /*
3200                  * disallow PS when one of the following
3201                  * bsscfg specific conditions meets
3202                  */
3203                 if (!cfg->BSS)
3204                         return false;
3205
3206                 return false;
3207         }
3208
3209         return true;
3210 }
3211
3212 static void brcms_b_reset(struct brcms_hardware *wlc_hw)
3213 {
3214         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
3215
3216         /* reset the core */
3217         if (!brcms_deviceremoved(wlc_hw->wlc))
3218                 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
3219
3220         /* purge the dma rings */
3221         brcms_c_flushqueues(wlc_hw->wlc);
3222 }
3223
3224 void brcms_c_reset(struct brcms_c_info *wlc)
3225 {
3226         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
3227
3228         /* slurp up hw mac counters before core reset */
3229         brcms_c_statsupd(wlc);
3230
3231         /* reset our snapshot of macstat counters */
3232         memset((char *)wlc->core->macstat_snapshot, 0,
3233                 sizeof(struct macstat));
3234
3235         brcms_b_reset(wlc->hw);
3236 }
3237
3238 void brcms_c_fatal_error(struct brcms_c_info *wlc)
3239 {
3240         wiphy_err(wlc->wiphy, "wl%d: fatal error, reinitializing\n",
3241                   wlc->pub->unit);
3242         brcms_init(wlc->wl);
3243 }
3244
3245 /* Return the channel the driver should initialize during brcms_c_init.
3246  * the channel may have to be changed from the currently configured channel
3247  * if other configurations are in conflict (bandlocked, 11n mode disabled,
3248  * invalid channel for current country, etc.)
3249  */
3250 static u16 brcms_c_init_chanspec(struct brcms_c_info *wlc)
3251 {
3252         u16 chanspec =
3253             1 | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE |
3254             WL_CHANSPEC_BAND_2G;
3255
3256         return chanspec;
3257 }
3258
3259 void brcms_c_init_scb(struct scb *scb)
3260 {
3261         int i;
3262
3263         memset(scb, 0, sizeof(struct scb));
3264         scb->flags = SCB_WMECAP | SCB_HTCAP;
3265         for (i = 0; i < NUMPRIO; i++) {
3266                 scb->seqnum[i] = 0;
3267                 scb->seqctl[i] = 0xFFFF;
3268         }
3269
3270         scb->seqctl_nonqos = 0xFFFF;
3271         scb->magic = SCB_MAGIC;
3272 }
3273
3274 /* d11 core init
3275  *   reset PSM
3276  *   download ucode/PCM
3277  *   let ucode run to suspended
3278  *   download ucode inits
3279  *   config other core registers
3280  *   init dma
3281  */
3282 static void brcms_b_coreinit(struct brcms_c_info *wlc)
3283 {
3284         struct brcms_hardware *wlc_hw = wlc->hw;
3285         struct d11regs __iomem *regs;
3286         u32 sflags;
3287         uint bcnint_us;
3288         uint i = 0;
3289         bool fifosz_fixup = false;
3290         int err = 0;
3291         u16 buf[NFIFO];
3292         struct wiphy *wiphy = wlc->wiphy;
3293         struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
3294
3295         regs = wlc_hw->regs;
3296
3297         BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
3298
3299         /* reset PSM */
3300         brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
3301
3302         brcms_ucode_download(wlc_hw);
3303         /*
3304          * FIFOSZ fixup. driver wants to controls the fifo allocation.
3305          */
3306         fifosz_fixup = true;
3307
3308         /* let the PSM run to the suspended state, set mode to BSS STA */
3309         W_REG(&regs->macintstatus, -1);
3310         brcms_b_mctrl(wlc_hw, ~0,
3311                        (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
3312
3313         /* wait for ucode to self-suspend after auto-init */
3314         SPINWAIT(((R_REG(&regs->macintstatus) & MI_MACSSPNDD) == 0),
3315                  1000 * 1000);
3316         if ((R_REG(&regs->macintstatus) & MI_MACSSPNDD) == 0)
3317                 wiphy_err(wiphy, "wl%d: wlc_coreinit: ucode did not self-"
3318                           "suspend!\n", wlc_hw->unit);
3319
3320         brcms_c_gpio_init(wlc);
3321
3322         sflags = ai_core_sflags(wlc_hw->sih, 0, 0);
3323
3324         if (D11REV_IS(wlc_hw->corerev, 23)) {
3325                 if (BRCMS_ISNPHY(wlc_hw->band))
3326                         brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
3327                 else
3328                         wiphy_err(wiphy, "%s: wl%d: unsupported phy in corerev"
3329                                   " %d\n", __func__, wlc_hw->unit,
3330                                   wlc_hw->corerev);
3331         } else if (D11REV_IS(wlc_hw->corerev, 24)) {
3332                 if (BRCMS_ISLCNPHY(wlc_hw->band))
3333                         brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
3334                 else
3335                         wiphy_err(wiphy, "%s: wl%d: unsupported phy in corerev"
3336                                   " %d\n", __func__, wlc_hw->unit,
3337                                   wlc_hw->corerev);
3338         } else {
3339                 wiphy_err(wiphy, "%s: wl%d: unsupported corerev %d\n",
3340                           __func__, wlc_hw->unit, wlc_hw->corerev);
3341         }
3342
3343         /* For old ucode, txfifo sizes needs to be modified(increased) */
3344         if (fifosz_fixup == true)
3345                 brcms_b_corerev_fifofixup(wlc_hw);
3346
3347         /* check txfifo allocations match between ucode and driver */
3348         buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
3349         if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
3350                 i = TX_AC_BE_FIFO;
3351                 err = -1;
3352         }
3353         buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
3354         if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
3355                 i = TX_AC_VI_FIFO;
3356                 err = -1;
3357         }
3358         buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
3359         buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
3360         buf[TX_AC_BK_FIFO] &= 0xff;
3361         if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
3362                 i = TX_AC_BK_FIFO;
3363                 err = -1;
3364         }
3365         if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
3366                 i = TX_AC_VO_FIFO;
3367                 err = -1;
3368         }
3369         buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
3370         buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
3371         buf[TX_BCMC_FIFO] &= 0xff;
3372         if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
3373                 i = TX_BCMC_FIFO;
3374                 err = -1;
3375         }
3376         if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
3377                 i = TX_ATIM_FIFO;
3378                 err = -1;
3379         }
3380         if (err != 0)
3381                 wiphy_err(wiphy, "wlc_coreinit: txfifo mismatch: ucode size %d"
3382                           " driver size %d index %d\n", buf[i],
3383                           wlc_hw->xmtfifo_sz[i], i);
3384
3385         /* make sure we can still talk to the mac */
3386         WARN_ON(R_REG(&regs->maccontrol) == 0xffffffff);
3387
3388         /* band-specific inits done by wlc_bsinit() */
3389
3390         /* Set up frame burst size and antenna swap threshold init values */
3391         brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
3392         brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
3393
3394         /* enable one rx interrupt per received frame */
3395         W_REG(&regs->intrcvlazy[0], (1 << IRL_FC_SHIFT));
3396
3397         /* set the station mode (BSS STA) */
3398         brcms_b_mctrl(wlc_hw,
3399                        (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
3400                        (MCTL_INFRA | MCTL_DISCARD_PMQ));
3401
3402         /* set up Beacon interval */
3403         bcnint_us = 0x8000 << 10;
3404         W_REG(&regs->tsf_cfprep, (bcnint_us << CFPREP_CBI_SHIFT));
3405         W_REG(&regs->tsf_cfpstart, bcnint_us);
3406         W_REG(&regs->macintstatus, MI_GP1);
3407
3408         /* write interrupt mask */
3409         W_REG(&regs->intctrlregs[RX_FIFO].intmask, DEF_RXINTMASK);
3410
3411         /* allow the MAC to control the PHY clock (dynamic on/off) */
3412         brcms_b_macphyclk_set(wlc_hw, ON);
3413
3414         /* program dynamic clock control fast powerup delay register */
3415         wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
3416         W_REG(&regs->scc_fastpwrup_dly, wlc->fastpwrup_dly);
3417
3418         /* tell the ucode the corerev */
3419         brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
3420
3421         /* tell the ucode MAC capabilities */
3422         brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
3423                            (u16) (wlc_hw->machwcap & 0xffff));
3424         brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
3425                            (u16) ((wlc_hw->
3426                                       machwcap >> 16) & 0xffff));
3427
3428         /* write retry limits to SCR, this done after PSM init */
3429         W_REG(&regs->objaddr, OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3430         (void)R_REG(&regs->objaddr);
3431         W_REG(&regs->objdata, wlc_hw->SRL);
3432         W_REG(&regs->objaddr, OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3433         (void)R_REG(&regs->objaddr);
3434         W_REG(&regs->objdata, wlc_hw->LRL);
3435
3436         /* write rate fallback retry limits */
3437         brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
3438         brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
3439
3440         AND_REG(&regs->ifs_ctl, 0x0FFF);
3441         W_REG(&regs->ifs_aifsn, EDCF_AIFSN_MIN);
3442
3443         /* init the tx dma engines */
3444         for (i = 0; i < NFIFO; i++) {
3445                 if (wlc_hw->di[i])
3446                         dma_txinit(wlc_hw->di[i]);
3447         }
3448
3449         /* init the rx dma engine(s) and post receive buffers */
3450         dma_rxinit(wlc_hw->di[RX_FIFO]);
3451         dma_rxfill(wlc_hw->di[RX_FIFO]);
3452 }
3453
3454 void
3455 static brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec,
3456                           bool mute) {
3457         u32 macintmask;
3458         bool fastclk;
3459         struct brcms_c_info *wlc = wlc_hw->wlc;
3460
3461         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
3462
3463         /* request FAST clock if not on */
3464         fastclk = wlc_hw->forcefastclk;
3465         if (!fastclk)
3466                 brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
3467
3468         /* disable interrupts */
3469         macintmask = brcms_intrsoff(wlc->wl);
3470
3471         /* set up the specified band and chanspec */
3472         brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
3473         wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3474
3475         /* do one-time phy inits and calibration */
3476         wlc_phy_cal_init(wlc_hw->band->pi);
3477
3478         /* core-specific initialization */
3479         brcms_b_coreinit(wlc);
3480
3481         /* suspend the tx fifos and mute the phy for preism cac time */
3482         if (mute)
3483                 brcms_b_mute(wlc_hw, ON, PHY_MUTE_FOR_PREISM);
3484
3485         /* band-specific inits */
3486         brcms_b_bsinit(wlc, chanspec);
3487
3488         /* restore macintmask */
3489         brcms_intrsrestore(wlc->wl, macintmask);
3490
3491         /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
3492          * is suspended and brcms_c_enable_mac() will clear this override bit.
3493          */
3494         mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
3495
3496         /*
3497          * initialize mac_suspend_depth to 1 to match ucode
3498          * initial suspended state
3499          */
3500         wlc_hw->mac_suspend_depth = 1;
3501
3502         /* restore the clk */
3503         if (!fastclk)
3504                 brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
3505 }
3506
3507 static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
3508                                      u16 chanspec)
3509 {
3510         /* Save our copy of the chanspec */
3511         wlc->chanspec = chanspec;
3512
3513         /* Set the chanspec and power limits for this locale */
3514         brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
3515
3516         if (wlc->stf->ss_algosel_auto)
3517                 brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
3518                                             chanspec);
3519
3520         brcms_c_stf_ss_update(wlc, wlc->band);
3521
3522 }
3523
3524 static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
3525                                      u16 chanspec)
3526 {
3527         struct brcms_c_rateset default_rateset;
3528         uint parkband;
3529         uint i, band_order[2];
3530
3531         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
3532         /*
3533          * We might have been bandlocked during down and the chip
3534          * power-cycled (hibernate). Figure out the right band to park on
3535          */
3536         if (wlc->bandlocked || wlc->pub->_nbands == 1) {
3537                 /* updated in brcms_c_bandlock() */
3538                 parkband = wlc->band->bandunit;
3539                 band_order[0] = band_order[1] = parkband;
3540         } else {
3541                 /* park on the band of the specified chanspec */
3542                 parkband = chspec_bandunit(chanspec);
3543
3544                 /* order so that parkband initialize last */
3545                 band_order[0] = parkband ^ 1;
3546                 band_order[1] = parkband;
3547         }
3548
3549         /* make each band operational, software state init */
3550         for (i = 0; i < wlc->pub->_nbands; i++) {
3551                 uint j = band_order[i];
3552
3553                 wlc->band = wlc->bandstate[j];
3554
3555                 brcms_default_rateset(wlc, &default_rateset);
3556
3557                 /* fill in hw_rate */
3558                 brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
3559                                    false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
3560                                    (bool) (wlc->pub->_n_enab & SUPPORT_11N));
3561
3562                 /* init basic rate lookup */
3563                 brcms_c_rate_lookup_init(wlc, &default_rateset);
3564         }
3565
3566         /* sync up phy/radio chanspec */
3567         brcms_c_set_phy_chanspec(wlc, chanspec);
3568 }
3569
3570 /*
3571  * ucode, hwmac update
3572  *    Channel dependent updates for ucode and hw
3573  */
3574 static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
3575 {
3576         /* enable or disable any active IBSSs depending on whether or not
3577          * we are on the home channel
3578          */
3579         if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
3580                 if (wlc->pub->associated) {
3581                         /*
3582                          * BMAC_NOTE: This is something that should be fixed
3583                          * in ucode inits. I think that the ucode inits set
3584                          * up the bcn templates and shm values with a bogus
3585                          * beacon. This should not be done in the inits. If
3586                          * ucode needs to set up a beacon for testing, the
3587                          * test routines should write it down, not expect the
3588                          * inits to populate a bogus beacon.
3589                          */
3590                         if (BRCMS_PHY_11N_CAP(wlc->band))
3591                                 brcms_b_write_shm(wlc->hw,
3592                                                 M_BCN_TXTSF_OFFSET, 0);
3593                 }
3594         } else {
3595                 /* disable an active IBSS if we are not on the home channel */
3596         }
3597
3598         /* update the various promisc bits */
3599         brcms_c_mac_bcn_promisc(wlc);
3600         brcms_c_mac_promisc(wlc);
3601 }
3602
3603 /* band-specific init */
3604 static void brcms_c_bsinit(struct brcms_c_info *wlc)
3605 {
3606         BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n",
3607                  wlc->pub->unit, wlc->band->bandunit);
3608
3609         /* write ucode ACK/CTS rate table */
3610         brcms_c_set_ratetable(wlc);
3611
3612         /* update some band specific mac configuration */
3613         brcms_c_ucode_mac_upd(wlc);
3614
3615         /* init antenna selection */
3616         brcms_c_antsel_init(wlc->asi);
3617
3618 }
3619
3620 /* formula:  IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
3621 static int
3622 brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
3623                    bool writeToShm)
3624 {
3625         int idle_busy_ratio_x_16 = 0;
3626         uint offset =
3627             isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
3628             M_TX_IDLE_BUSY_RATIO_X_16_CCK;
3629         if (duty_cycle > 100 || duty_cycle < 0) {
3630                 wiphy_err(wlc->wiphy, "wl%d:  duty cycle value off limit\n",
3631                           wlc->pub->unit);
3632                 return -EINVAL;
3633         }
3634         if (duty_cycle)
3635                 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
3636         /* Only write to shared memory  when wl is up */
3637         if (writeToShm)
3638                 brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
3639
3640         if (isOFDM)
3641                 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
3642         else
3643                 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
3644
3645         return 0;
3646 }
3647
3648 /*
3649  * Initialize the base precedence map for dequeueing
3650  * from txq based on WME settings
3651  */
3652 static void brcms_c_tx_prec_map_init(struct brcms_c_info *wlc)
3653 {
3654         wlc->tx_prec_map = BRCMS_PREC_BMP_ALL;
3655         memset(wlc->fifo2prec_map, 0, NFIFO * sizeof(u16));
3656
3657         wlc->fifo2prec_map[TX_AC_BK_FIFO] = BRCMS_PREC_BMP_AC_BK;
3658         wlc->fifo2prec_map[TX_AC_BE_FIFO] = BRCMS_PREC_BMP_AC_BE;
3659         wlc->fifo2prec_map[TX_AC_VI_FIFO] = BRCMS_PREC_BMP_AC_VI;
3660         wlc->fifo2prec_map[TX_AC_VO_FIFO] = BRCMS_PREC_BMP_AC_VO;
3661 }
3662
3663 static void
3664 brcms_c_txflowcontrol_signal(struct brcms_c_info *wlc,
3665                              struct brcms_txq_info *qi, bool on, int prio)
3666 {
3667         /* transmit flowcontrol is not yet implemented */
3668 }
3669
3670 static void brcms_c_txflowcontrol_reset(struct brcms_c_info *wlc)
3671 {
3672         struct brcms_txq_info *qi;
3673
3674         for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
3675                 if (qi->stopped) {
3676                         brcms_c_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
3677                         qi->stopped = 0;
3678                 }
3679         }
3680 }
3681
3682 void brcms_c_init(struct brcms_c_info *wlc)
3683 {
3684         struct d11regs __iomem *regs;
3685         u16 chanspec;
3686         bool mute = false;
3687
3688         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
3689
3690         regs = wlc->regs;
3691
3692         /*
3693          * This will happen if a big-hammer was executed. In
3694          * that case, we want to go back to the channel that
3695          * we were on and not new channel
3696          */
3697         if (wlc->pub->associated)
3698                 chanspec = wlc->home_chanspec;
3699         else
3700                 chanspec = brcms_c_init_chanspec(wlc);
3701
3702         brcms_b_init(wlc->hw, chanspec, mute);
3703
3704         /* update beacon listen interval */
3705         brcms_c_bcn_li_upd(wlc);
3706
3707         /* write ethernet address to core */
3708         brcms_c_set_mac(wlc->bsscfg);
3709         brcms_c_set_bssid(wlc->bsscfg);
3710
3711         /* Update tsf_cfprep if associated and up */
3712         if (wlc->pub->associated && wlc->bsscfg->up) {
3713                 u32 bi;
3714
3715                 /* get beacon period and convert to uS */
3716                 bi = wlc->bsscfg->current_bss->beacon_period << 10;
3717                 /*
3718                  * update since init path would reset
3719                  * to default value
3720                  */
3721                 W_REG(&regs->tsf_cfprep,
3722                       (bi << CFPREP_CBI_SHIFT));
3723
3724                 /* Update maccontrol PM related bits */
3725                 brcms_c_set_ps_ctrl(wlc);
3726         }
3727
3728         brcms_c_bandinit_ordered(wlc, chanspec);
3729
3730         /* init probe response timeout */
3731         brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
3732
3733         /* init max burst txop (framebursting) */
3734         brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
3735                       (wlc->
3736                        _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
3737
3738         /* initialize maximum allowed duty cycle */
3739         brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
3740         brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
3741
3742         /*
3743          * Update some shared memory locations related to
3744          * max AMPDU size allowed to received
3745          */
3746         brcms_c_ampdu_shm_upd(wlc->ampdu);
3747
3748         /* band-specific inits */
3749         brcms_c_bsinit(wlc);
3750
3751         /* Enable EDCF mode (while the MAC is suspended) */
3752         OR_REG(&regs->ifs_ctl, IFS_USEEDCF);
3753         brcms_c_edcf_setparams(wlc, false);
3754
3755         /* Init precedence maps for empty FIFOs */
3756         brcms_c_tx_prec_map_init(wlc);
3757
3758         /* read the ucode version if we have not yet done so */
3759         if (wlc->ucode_rev == 0) {
3760                 wlc->ucode_rev =
3761                     brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
3762                 wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
3763         }
3764
3765         /* ..now really unleash hell (allow the MAC out of suspend) */
3766         brcms_c_enable_mac(wlc);
3767
3768         /* clear tx flow control */
3769         brcms_c_txflowcontrol_reset(wlc);
3770
3771         /* enable the RF Disable Delay timer */
3772         W_REG(&wlc->regs->rfdisabledly, RFDISABLE_DEFAULT);
3773
3774         /* initialize mpc delay */
3775         wlc->mpc_delay_off = wlc->mpc_dlycnt = BRCMS_MPC_MIN_DELAYCNT;
3776
3777         /*
3778          * Initialize WME parameters; if they haven't been set by some other
3779          * mechanism (IOVar, etc) then read them from the hardware.
3780          */
3781         if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
3782                 /* Uninitialized; read from HW */
3783                 int ac;
3784
3785                 for (ac = 0; ac < AC_COUNT; ac++)
3786                         wlc->wme_retries[ac] =
3787                             brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
3788         }
3789 }
3790
3791 void brcms_c_mac_bcn_promisc_change(struct brcms_c_info *wlc, bool promisc)
3792 {
3793         wlc->bcnmisc_monitor = promisc;
3794         brcms_c_mac_bcn_promisc(wlc);
3795 }
3796
3797 void brcms_c_mac_bcn_promisc(struct brcms_c_info *wlc)
3798 {
3799         if (wlc->bcnmisc_monitor)
3800                 brcms_b_mctrl(wlc->hw, MCTL_BCNS_PROMISC, MCTL_BCNS_PROMISC);
3801         else
3802                 brcms_b_mctrl(wlc->hw, MCTL_BCNS_PROMISC, 0);
3803 }
3804
3805 /* set or clear maccontrol bits MCTL_PROMISC and MCTL_KEEPCONTROL */
3806 void brcms_c_mac_promisc(struct brcms_c_info *wlc)
3807 {
3808         u32 promisc_bits = 0;
3809
3810         /*
3811          * promiscuous mode just sets MCTL_PROMISC
3812          * Note: APs get all BSS traffic without the need to set
3813          * the MCTL_PROMISC bit since all BSS data traffic is
3814          * directed at the AP
3815          */
3816         if (wlc->pub->promisc)
3817                 promisc_bits |= MCTL_PROMISC;
3818
3819         /* monitor mode needs both MCTL_PROMISC and MCTL_KEEPCONTROL
3820          * Note: monitor mode also needs MCTL_BCNS_PROMISC, but that is
3821          * handled in brcms_c_mac_bcn_promisc()
3822          */
3823         if (wlc->monitor)
3824                 promisc_bits |= MCTL_PROMISC | MCTL_KEEPCONTROL;
3825
3826         brcms_b_mctrl(wlc->hw, MCTL_PROMISC | MCTL_KEEPCONTROL, promisc_bits);
3827 }
3828
3829 /* push sw hps and wake state through hardware */
3830 void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
3831 {
3832         u32 v1, v2;
3833         bool hps;
3834         bool awake_before;
3835
3836         hps = brcms_c_ps_allowed(wlc);
3837
3838         BCMMSG(wlc->wiphy, "wl%d: hps %d\n", wlc->pub->unit, hps);
3839
3840         v1 = R_REG(&wlc->regs->maccontrol);
3841         v2 = MCTL_WAKE;
3842         if (hps)
3843                 v2 |= MCTL_HPS;
3844
3845         brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
3846
3847         awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
3848
3849         if (!awake_before)
3850                 brcms_b_wait_for_wake(wlc->hw);
3851
3852 }
3853
3854 /*
3855  * Write this BSS config's MAC address to core.
3856  * Updates RXE match engine.
3857  */
3858 int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
3859 {
3860         int err = 0;
3861         struct brcms_c_info *wlc = bsscfg->wlc;
3862
3863         /* enter the MAC addr into the RXE match registers */
3864         brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, bsscfg->cur_etheraddr);
3865
3866         brcms_c_ampdu_macaddr_upd(wlc);
3867
3868         return err;
3869 }
3870
3871 /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
3872  * Updates RXE match engine.
3873  */
3874 void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
3875 {
3876         /* we need to update BSSID in RXE match registers */
3877         brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
3878 }
3879
3880 static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
3881 {
3882         wlc_hw->shortslot = shortslot;
3883
3884         if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
3885                 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
3886                 brcms_b_update_slot_timing(wlc_hw, shortslot);
3887                 brcms_c_enable_mac(wlc_hw->wlc);
3888         }
3889 }
3890
3891 /*
3892  * Suspend the the MAC and update the slot timing
3893  * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
3894  */
3895 void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
3896 {
3897         /* use the override if it is set */
3898         if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
3899                 shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
3900
3901         if (wlc->shortslot == shortslot)
3902                 return;
3903
3904         wlc->shortslot = shortslot;
3905
3906         brcms_b_set_shortslot(wlc->hw, shortslot);
3907 }
3908
3909 void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
3910 {
3911         if (wlc->home_chanspec != chanspec) {
3912                 wlc->home_chanspec = chanspec;
3913
3914                 if (wlc->bsscfg->associated)
3915                         wlc->bsscfg->current_bss->chanspec = chanspec;
3916         }
3917 }
3918
3919 void
3920 brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
3921                       bool mute, struct txpwr_limits *txpwr)
3922 {
3923         uint bandunit;
3924
3925         BCMMSG(wlc_hw->wlc->wiphy, "wl%d: 0x%x\n", wlc_hw->unit, chanspec);
3926
3927         wlc_hw->chanspec = chanspec;
3928
3929         /* Switch bands if necessary */
3930         if (wlc_hw->_nbands > 1) {
3931                 bandunit = chspec_bandunit(chanspec);
3932                 if (wlc_hw->band->bandunit != bandunit) {
3933                         /* brcms_b_setband disables other bandunit,
3934                          *  use light band switch if not up yet
3935                          */
3936                         if (wlc_hw->up) {
3937                                 wlc_phy_chanspec_radio_set(wlc_hw->
3938                                                            bandstate[bandunit]->
3939                                                            pi, chanspec);
3940                                 brcms_b_setband(wlc_hw, bandunit, chanspec);
3941                         } else {
3942                                 brcms_c_setxband(wlc_hw, bandunit);
3943                         }
3944                 }
3945         }
3946
3947         wlc_phy_initcal_enable(wlc_hw->band->pi, !mute);
3948
3949         if (!wlc_hw->up) {
3950                 if (wlc_hw->clk)
3951                         wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
3952                                                   chanspec);
3953                 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3954         } else {
3955                 wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
3956                 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
3957
3958                 /* Update muting of the channel */
3959                 brcms_b_mute(wlc_hw, mute, 0);
3960         }
3961 }
3962
3963 /* switch to and initialize new band */
3964 static void brcms_c_setband(struct brcms_c_info *wlc,
3965                                            uint bandunit)
3966 {
3967         wlc->band = wlc->bandstate[bandunit];
3968
3969         if (!wlc->pub->up)
3970                 return;
3971
3972         /* wait for at least one beacon before entering sleeping state */
3973         brcms_c_set_ps_ctrl(wlc);
3974
3975         /* band-specific initializations */
3976         brcms_c_bsinit(wlc);
3977 }
3978
3979 void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
3980 {
3981         uint bandunit;
3982         bool switchband = false;
3983         u16 old_chanspec = wlc->chanspec;
3984
3985         if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
3986                 wiphy_err(wlc->wiphy, "wl%d: %s: Bad channel %d\n",
3987                           wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
3988                 return;
3989         }
3990
3991         /* Switch bands if necessary */
3992         if (wlc->pub->_nbands > 1) {
3993                 bandunit = chspec_bandunit(chanspec);
3994                 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
3995                         switchband = true;
3996                         if (wlc->bandlocked) {
3997                                 wiphy_err(wlc->wiphy, "wl%d: %s: chspec %d "
3998                                           "band is locked!\n",
3999                                           wlc->pub->unit, __func__,
4000                                           CHSPEC_CHANNEL(chanspec));
4001                                 return;
4002                         }
4003                         /*
4004                          * should the setband call come after the
4005                          * brcms_b_chanspec() ? if the setband updates
4006                          * (brcms_c_bsinit) use low level calls to inspect and
4007                          * set state, the state inspected may be from the wrong
4008                          * band, or the following brcms_b_set_chanspec() may
4009                          * undo the work.
4010                          */
4011                         brcms_c_setband(wlc, bandunit);
4012                 }
4013         }
4014
4015         /* sync up phy/radio chanspec */
4016         brcms_c_set_phy_chanspec(wlc, chanspec);
4017
4018         /* init antenna selection */
4019         if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
4020                 brcms_c_antsel_init(wlc->asi);
4021
4022                 /* Fix the hardware rateset based on bw.
4023                  * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
4024                  */
4025                 brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
4026                         wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
4027         }
4028
4029         /* update some mac configuration since chanspec changed */
4030         brcms_c_ucode_mac_upd(wlc);
4031 }
4032
4033 u32 brcms_c_lowest_basic_rspec(struct brcms_c_info *wlc,
4034                                       struct brcms_c_rateset *rs)
4035 {
4036         u32 lowest_basic_rspec;
4037         uint i;
4038
4039         /* Use the lowest basic rate */
4040         lowest_basic_rspec = rs->rates[0] & BRCMS_RATE_MASK;
4041         for (i = 0; i < rs->count; i++) {
4042                 if (rs->rates[i] & BRCMS_RATE_FLAG) {
4043                         lowest_basic_rspec = rs->rates[i] & BRCMS_RATE_MASK;
4044                         break;
4045                 }
4046         }
4047
4048         /*
4049          * pick siso/cdd as default for OFDM (note no basic
4050          * rate MCSs are supported yet)
4051          */
4052         if (is_ofdm_rate(lowest_basic_rspec))
4053                 lowest_basic_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
4054
4055         return lowest_basic_rspec;
4056 }
4057
4058 /*
4059  * This function changes the phytxctl for beacon based on current
4060  * beacon ratespec AND txant setting as per this table:
4061  *  ratespec     CCK            ant = wlc->stf->txant
4062  *              OFDM            ant = 3
4063  */
4064 void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
4065                                        u32 bcn_rspec)
4066 {
4067         u16 phyctl;
4068         u16 phytxant = wlc->stf->phytxant;
4069         u16 mask = PHY_TXC_ANT_MASK;
4070
4071         /* for non-siso rates or default setting, use the available chains */
4072         if (BRCMS_PHY_11N_CAP(wlc->band))
4073                 phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
4074
4075         phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
4076         phyctl = (phyctl & ~mask) | phytxant;
4077         brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
4078 }
4079
4080 /*
4081  * centralized protection config change function to simplify debugging, no
4082  * consistency checking this should be called only on changes to avoid overhead
4083  * in periodic function
4084  */
4085 void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
4086 {
4087         BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
4088
4089         switch (idx) {
4090         case BRCMS_PROT_G_SPEC:
4091                 wlc->protection->_g = (bool) val;
4092                 break;
4093         case BRCMS_PROT_G_OVR:
4094                 wlc->protection->g_override = (s8) val;
4095                 break;
4096         case BRCMS_PROT_G_USER:
4097                 wlc->protection->gmode_user = (u8) val;
4098                 break;
4099         case BRCMS_PROT_OVERLAP:
4100                 wlc->protection->overlap = (s8) val;
4101                 break;
4102         case BRCMS_PROT_N_USER:
4103                 wlc->protection->nmode_user = (s8) val;
4104                 break;
4105         case BRCMS_PROT_N_CFG:
4106                 wlc->protection->n_cfg = (s8) val;
4107                 break;
4108         case BRCMS_PROT_N_CFG_OVR:
4109                 wlc->protection->n_cfg_override = (s8) val;
4110                 break;
4111         case BRCMS_PROT_N_NONGF:
4112                 wlc->protection->nongf = (bool) val;
4113                 break;
4114         case BRCMS_PROT_N_NONGF_OVR:
4115                 wlc->protection->nongf_override = (s8) val;
4116                 break;
4117         case BRCMS_PROT_N_PAM_OVR:
4118                 wlc->protection->n_pam_override = (s8) val;
4119                 break;
4120         case BRCMS_PROT_N_OBSS:
4121                 wlc->protection->n_obss = (bool) val;
4122                 break;
4123
4124         default:
4125                 break;
4126         }
4127
4128 }
4129
4130 static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
4131 {
4132         if (wlc->pub->up) {
4133                 brcms_c_update_beacon(wlc);
4134                 brcms_c_update_probe_resp(wlc, true);
4135         }
4136 }
4137
4138 static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
4139 {
4140         wlc->stf->ldpc = val;
4141
4142         if (wlc->pub->up) {
4143                 brcms_c_update_beacon(wlc);
4144                 brcms_c_update_probe_resp(wlc, true);
4145                 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
4146         }
4147 }
4148
4149 void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
4150                        const struct ieee80211_tx_queue_params *params,
4151                        bool suspend)
4152 {
4153         int i;
4154         struct shm_acparams acp_shm;
4155         u16 *shm_entry;
4156
4157         /* Only apply params if the core is out of reset and has clocks */
4158         if (!wlc->clk) {
4159                 wiphy_err(wlc->wiphy, "wl%d: %s : no-clock\n", wlc->pub->unit,
4160                           __func__);
4161                 return;
4162         }
4163
4164         memset((char *)&acp_shm, 0, sizeof(struct shm_acparams));
4165         /* fill in shm ac params struct */
4166         acp_shm.txop = params->txop;
4167         /* convert from units of 32us to us for ucode */
4168         wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
4169             EDCF_TXOP2USEC(acp_shm.txop);
4170         acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
4171
4172         if (aci == AC_VI && acp_shm.txop == 0
4173             && acp_shm.aifs < EDCF_AIFSN_MAX)
4174                 acp_shm.aifs++;
4175
4176         if (acp_shm.aifs < EDCF_AIFSN_MIN
4177             || acp_shm.aifs > EDCF_AIFSN_MAX) {
4178                 wiphy_err(wlc->wiphy, "wl%d: edcf_setparams: bad "
4179                           "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
4180         } else {
4181                 acp_shm.cwmin = params->cw_min;
4182                 acp_shm.cwmax = params->cw_max;
4183                 acp_shm.cwcur = acp_shm.cwmin;
4184                 acp_shm.bslots =
4185                     R_REG(&wlc->regs->tsf_random) & acp_shm.cwcur;
4186                 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
4187                 /* Indicate the new params to the ucode */
4188                 acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
4189                                                   wme_ac2fifo[aci] *
4190                                                   M_EDCF_QLEN +
4191                                                   M_EDCF_STATUS_OFF));
4192                 acp_shm.status |= WME_STATUS_NEWAC;
4193
4194                 /* Fill in shm acparam table */
4195                 shm_entry = (u16 *) &acp_shm;
4196                 for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
4197                         brcms_b_write_shm(wlc->hw,
4198                                           M_EDCF_QINFO +
4199                                           wme_ac2fifo[aci] * M_EDCF_QLEN + i,
4200                                           *shm_entry++);
4201         }
4202
4203         if (suspend)
4204                 brcms_c_suspend_mac_and_wait(wlc);
4205
4206         if (suspend)
4207                 brcms_c_enable_mac(wlc);
4208
4209 }
4210
4211 void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
4212 {
4213         u16 aci;
4214         int i_ac;
4215         struct ieee80211_tx_queue_params txq_pars;
4216         static const struct edcf_acparam default_edcf_acparams[] = {
4217                  {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
4218                  {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
4219                  {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
4220                  {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
4221         }; /* ucode needs these parameters during its initialization */
4222         const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
4223
4224         for (i_ac = 0; i_ac < AC_COUNT; i_ac++, edcf_acp++) {
4225                 /* find out which ac this set of params applies to */
4226                 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
4227
4228                 /* fill in shm ac params struct */
4229                 txq_pars.txop = edcf_acp->TXOP;
4230                 txq_pars.aifs = edcf_acp->ACI;
4231
4232                 /* CWmin = 2^(ECWmin) - 1 */
4233                 txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
4234                 /* CWmax = 2^(ECWmax) - 1 */
4235                 txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
4236                                             >> EDCF_ECWMAX_SHIFT);
4237                 brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
4238         }
4239
4240         if (suspend)
4241                 brcms_c_suspend_mac_and_wait(wlc);
4242
4243         if (suspend)
4244                 brcms_c_enable_mac(wlc);
4245
4246 }
4247
4248 /* maintain LED behavior in down state */
4249 static void brcms_c_down_led_upd(struct brcms_c_info *wlc)
4250 {
4251         /*
4252          * maintain LEDs while in down state, turn on sbclk if
4253          * not available yet. Turn on sbclk if necessary
4254          */
4255         brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_FLIP);
4256         brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_FLIP);
4257 }
4258
4259 static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
4260 {
4261         /* Don't start the timer if HWRADIO feature is disabled */
4262         if (wlc->radio_monitor)
4263                 return;
4264
4265         wlc->radio_monitor = true;
4266         brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
4267         brcms_add_timer(wlc->wl, wlc->radio_timer, TIMER_INTERVAL_RADIOCHK,
4268                         true);
4269 }
4270
4271 void brcms_c_radio_disable(struct brcms_c_info *wlc)
4272 {
4273         if (!wlc->pub->up) {
4274                 brcms_c_down_led_upd(wlc);
4275                 return;
4276         }
4277
4278         brcms_c_radio_monitor_start(wlc);
4279         brcms_down(wlc->wl);
4280 }
4281
4282 static void brcms_c_radio_enable(struct brcms_c_info *wlc)
4283 {
4284         if (wlc->pub->up)
4285                 return;
4286
4287         if (brcms_deviceremoved(wlc))
4288                 return;
4289
4290         brcms_up(wlc->wl);
4291 }
4292
4293 bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
4294 {
4295         if (!wlc->radio_monitor)
4296                 return true;
4297
4298         wlc->radio_monitor = false;
4299         brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
4300         return brcms_del_timer(wlc->wl, wlc->radio_timer);
4301 }
4302
4303 /* read hwdisable state and propagate to wlc flag */
4304 static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
4305 {
4306         if (wlc->pub->hw_off)
4307                 return;
4308
4309         if (brcms_b_radio_read_hwdisabled(wlc->hw))
4310                 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4311         else
4312                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4313 }
4314
4315 /*
4316  * centralized radio disable/enable function,
4317  * invoke radio enable/disable after updating hwradio status
4318  */
4319 static void brcms_c_radio_upd(struct brcms_c_info *wlc)
4320 {
4321         if (wlc->pub->radio_disabled)
4322                 brcms_c_radio_disable(wlc);
4323         else
4324                 brcms_c_radio_enable(wlc);
4325 }
4326
4327 /* update hwradio status and return it */
4328 bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
4329 {
4330         brcms_c_radio_hwdisable_upd(wlc);
4331
4332         return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
4333                         true : false;
4334 }
4335
4336 /* periodical query hw radio button while driver is "down" */
4337 static void brcms_c_radio_timer(void *arg)
4338 {
4339         struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4340
4341         if (brcms_deviceremoved(wlc)) {
4342                 wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n", wlc->pub->unit,
4343                         __func__);
4344                 brcms_down(wlc->wl);
4345                 return;
4346         }
4347
4348         /* cap mpc off count */
4349         if (wlc->mpc_offcnt < BRCMS_MPC_MAX_DELAYCNT)
4350                 wlc->mpc_offcnt++;
4351
4352         brcms_c_radio_hwdisable_upd(wlc);
4353         brcms_c_radio_upd(wlc);
4354 }
4355
4356 /* common low-level watchdog code */
4357 static void brcms_b_watchdog(void *arg)
4358 {
4359         struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4360         struct brcms_hardware *wlc_hw = wlc->hw;
4361
4362         BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
4363
4364         if (!wlc_hw->up)
4365                 return;
4366
4367         /* increment second count */
4368         wlc_hw->now++;
4369
4370         /* Check for FIFO error interrupts */
4371         brcms_b_fifoerrors(wlc_hw);
4372
4373         /* make sure RX dma has buffers */
4374         dma_rxfill(wlc->hw->di[RX_FIFO]);
4375
4376         wlc_phy_watchdog(wlc_hw->band->pi);
4377 }
4378
4379 /* common watchdog code */
4380 static void brcms_c_watchdog(void *arg)
4381 {
4382         struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4383
4384         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
4385
4386         if (!wlc->pub->up)
4387                 return;
4388
4389         if (brcms_deviceremoved(wlc)) {
4390                 wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n", wlc->pub->unit,
4391                           __func__);
4392                 brcms_down(wlc->wl);
4393                 return;
4394         }
4395
4396         /* increment second count */
4397         wlc->pub->now++;
4398
4399         /* delay radio disable */
4400         if (wlc->mpc_delay_off) {
4401                 if (--wlc->mpc_delay_off == 0) {
4402                         mboolset(wlc->pub->radio_disabled,
4403                                  WL_RADIO_MPC_DISABLE);
4404                         if (wlc->mpc && brcms_c_ismpc(wlc))
4405                                 wlc->mpc_offcnt = 0;
4406                 }
4407         }
4408
4409         /* mpc sync */
4410         brcms_c_radio_mpc_upd(wlc);
4411         /* radio sync: sw/hw/mpc --> radio_disable/radio_enable */
4412         brcms_c_radio_hwdisable_upd(wlc);
4413         brcms_c_radio_upd(wlc);
4414         /* if radio is disable, driver may be down, quit here */
4415         if (wlc->pub->radio_disabled)
4416                 return;
4417
4418         brcms_b_watchdog(wlc);
4419
4420         /*
4421          * occasionally sample mac stat counters to
4422          * detect 16-bit counter wrap
4423          */
4424         if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
4425                 brcms_c_statsupd(wlc);
4426
4427         if (BRCMS_ISNPHY(wlc->band) &&
4428             ((wlc->pub->now - wlc->tempsense_lasttime) >=
4429              BRCMS_TEMPSENSE_PERIOD)) {
4430                 wlc->tempsense_lasttime = wlc->pub->now;
4431                 brcms_c_tempsense_upd(wlc);
4432         }
4433 }
4434
4435 static void brcms_c_watchdog_by_timer(void *arg)
4436 {
4437         brcms_c_watchdog(arg);
4438 }
4439
4440 bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
4441 {
4442         wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
4443                 wlc, "watchdog");
4444         if (!wlc->wdtimer) {
4445                 wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for wdtimer "
4446                           "failed\n", unit);
4447                 goto fail;
4448         }
4449
4450         wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
4451                 wlc, "radio");
4452         if (!wlc->radio_timer) {
4453                 wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for radio_timer "
4454                           "failed\n", unit);
4455                 goto fail;
4456         }
4457
4458         return true;
4459
4460  fail:
4461         return false;
4462 }
4463
4464 /*
4465  * Initialize brcms_c_info default values ...
4466  * may get overrides later in this function
4467  */
4468 void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
4469 {
4470         int i;
4471
4472         /* Save our copy of the chanspec */
4473         wlc->chanspec = ch20mhz_chspec(1);
4474
4475         /* various 802.11g modes */
4476         wlc->shortslot = false;
4477         wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
4478
4479         brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
4480         brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
4481
4482         brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
4483                                BRCMS_PROTECTION_AUTO);
4484         brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
4485         brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
4486                                BRCMS_PROTECTION_AUTO);
4487         brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
4488         brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
4489
4490         brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
4491                                BRCMS_PROTECTION_CTL_OVERLAP);
4492
4493         /* 802.11g draft 4.0 NonERP elt advertisement */
4494         wlc->include_legacy_erp = true;
4495
4496         wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
4497         wlc->stf->txant = ANT_TX_DEF;
4498
4499         wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
4500
4501         wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
4502         for (i = 0; i < NFIFO; i++)
4503                 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
4504         wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
4505
4506         /* default rate fallback retry limits */
4507         wlc->SFBL = RETRY_SHORT_FB;
4508         wlc->LFBL = RETRY_LONG_FB;
4509
4510         /* default mac retry limits */
4511         wlc->SRL = RETRY_SHORT_DEF;
4512         wlc->LRL = RETRY_LONG_DEF;
4513
4514         /* WME QoS mode is Auto by default */
4515         wlc->pub->_ampdu = AMPDU_AGG_HOST;
4516         wlc->pub->bcmerror = 0;
4517
4518         /* initialize mpc delay */
4519         wlc->mpc_delay_off = wlc->mpc_dlycnt = BRCMS_MPC_MIN_DELAYCNT;
4520 }
4521
4522 static uint brcms_c_attach_module(struct brcms_c_info *wlc)
4523 {
4524         uint err = 0;
4525         uint unit;
4526         unit = wlc->pub->unit;
4527
4528         wlc->asi = brcms_c_antsel_attach(wlc);
4529         if (wlc->asi == NULL) {
4530                 wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
4531                           "failed\n", unit);
4532                 err = 44;
4533                 goto fail;
4534         }
4535
4536         wlc->ampdu = brcms_c_ampdu_attach(wlc);
4537         if (wlc->ampdu == NULL) {
4538                 wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
4539                           "failed\n", unit);
4540                 err = 50;
4541                 goto fail;
4542         }
4543
4544         if ((brcms_c_stf_attach(wlc) != 0)) {
4545                 wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
4546                           "failed\n", unit);
4547                 err = 68;
4548                 goto fail;
4549         }
4550  fail:
4551         return err;
4552 }
4553
4554 struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
4555 {
4556         return wlc->pub;
4557 }
4558
4559 /* low level attach
4560  *    run backplane attach, init nvram
4561  *    run phy attach
4562  *    initialize software state for each core and band
4563  *    put the whole chip in reset(driver down state), no clock
4564  */
4565 static int brcms_b_attach(struct brcms_c_info *wlc, u16 vendor, u16 device,
4566                           uint unit, bool piomode, void __iomem *regsva,
4567                           struct pci_dev *btparam)
4568 {
4569         struct brcms_hardware *wlc_hw;
4570         struct d11regs __iomem *regs;
4571         char *macaddr = NULL;
4572         char *vars;
4573         uint err = 0;
4574         uint j;
4575         bool wme = false;
4576         struct shared_phy_params sha_params;
4577         struct wiphy *wiphy = wlc->wiphy;
4578         char *var;
4579         unsigned long res;
4580
4581         BCMMSG(wlc->wiphy, "wl%d: vendor 0x%x device 0x%x\n", unit, vendor,
4582                 device);
4583
4584         wme = true;
4585
4586         wlc_hw = wlc->hw;
4587         wlc_hw->wlc = wlc;
4588         wlc_hw->unit = unit;
4589         wlc_hw->band = wlc_hw->bandstate[0];
4590         wlc_hw->_piomode = piomode;
4591
4592         /* populate struct brcms_hardware with default values  */
4593         brcms_b_info_init(wlc_hw);
4594
4595         /*
4596          * Do the hardware portion of the attach. Also initialize software
4597          * state that depends on the particular hardware we are running.
4598          */
4599         wlc_hw->sih = ai_attach(regsva, btparam,
4600                                 &wlc_hw->vars, &wlc_hw->vars_size);
4601         if (wlc_hw->sih == NULL) {
4602                 wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
4603                           unit);
4604                 err = 11;
4605                 goto fail;
4606         }
4607         vars = wlc_hw->vars;
4608
4609         /*
4610          * Get vendid/devid nvram overwrites, which could be different
4611          * than those the BIOS recognizes for devices on PCMCIA_BUS,
4612          * SDIO_BUS, and SROMless devices on PCI_BUS.
4613          */
4614         var = getvar(vars, "vendid");
4615         if (var && !kstrtoul(var, 0, &res)) {
4616                 vendor = (u16)res;
4617                 wiphy_err(wiphy, "Overriding vendor id = 0x%x\n",
4618                           vendor);
4619         }
4620         var = getvar(vars, "devid");
4621         if (var && !kstrtoul(var, 0, &res)) {
4622                 u16 devid = (u16)res;
4623                 if (devid != 0xffff) {
4624                         device = devid;
4625                         wiphy_err(wiphy, "Overriding device id = 0x%x"
4626                                   "\n", device);
4627                 }
4628         }
4629
4630         /* verify again the device is supported */
4631         if (!brcms_c_chipmatch(vendor, device)) {
4632                 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported "
4633                         "vendor/device (0x%x/0x%x)\n",
4634                          unit, vendor, device);
4635                 err = 12;
4636                 goto fail;
4637         }
4638
4639         wlc_hw->vendorid = vendor;
4640         wlc_hw->deviceid = device;
4641
4642         /* set bar0 window to point at D11 core */
4643         wlc_hw->regs = (struct d11regs __iomem *)
4644                                 ai_setcore(wlc_hw->sih, D11_CORE_ID, 0);
4645         wlc_hw->corerev = ai_corerev(wlc_hw->sih);
4646
4647         regs = wlc_hw->regs;
4648
4649         wlc->regs = wlc_hw->regs;
4650
4651         /* validate chip, chiprev and corerev */
4652         if (!brcms_c_isgoodchip(wlc_hw)) {
4653                 err = 13;
4654                 goto fail;
4655         }
4656
4657         /* initialize power control registers */
4658         ai_clkctl_init(wlc_hw->sih);
4659
4660         /* request fastclock and force fastclock for the rest of attach
4661          * bring the d11 core out of reset.
4662          *   For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
4663          *   is still false; But it will be called again inside wlc_corereset,
4664          *   after d11 is out of reset.
4665          */
4666         brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
4667         brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4668
4669         if (!brcms_b_validate_chip_access(wlc_hw)) {
4670                 wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
4671                         "failed\n", unit);
4672                 err = 14;
4673                 goto fail;
4674         }
4675
4676         /* get the board rev, used just below */
4677         j = getintvar(vars, "boardrev");
4678         /* promote srom boardrev of 0xFF to 1 */
4679         if (j == BOARDREV_PROMOTABLE)
4680                 j = BOARDREV_PROMOTED;
4681         wlc_hw->boardrev = (u16) j;
4682         if (!brcms_c_validboardtype(wlc_hw)) {
4683                 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
4684                         "board type (0x%x)" " or revision level (0x%x)\n",
4685                          unit, wlc_hw->sih->boardtype, wlc_hw->boardrev);
4686                 err = 15;
4687                 goto fail;
4688         }
4689         wlc_hw->sromrev = (u8) getintvar(vars, "sromrev");
4690         wlc_hw->boardflags = (u32) getintvar(vars, "boardflags");
4691         wlc_hw->boardflags2 = (u32) getintvar(vars, "boardflags2");
4692
4693         if (wlc_hw->boardflags & BFL_NOPLLDOWN)
4694                 brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
4695
4696         /* check device id(srom, nvram etc.) to set bands */
4697         if (wlc_hw->deviceid == BCM43224_D11N_ID ||
4698             wlc_hw->deviceid == BCM43224_D11N_ID_VEN1)
4699                 /* Dualband boards */
4700                 wlc_hw->_nbands = 2;
4701         else
4702                 wlc_hw->_nbands = 1;
4703
4704         if ((wlc_hw->sih->chip == BCM43225_CHIP_ID))
4705                 wlc_hw->_nbands = 1;
4706
4707         /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
4708          * unconditionally does the init of these values
4709          */
4710         wlc->vendorid = wlc_hw->vendorid;
4711         wlc->deviceid = wlc_hw->deviceid;
4712         wlc->pub->sih = wlc_hw->sih;
4713         wlc->pub->corerev = wlc_hw->corerev;
4714         wlc->pub->sromrev = wlc_hw->sromrev;
4715         wlc->pub->boardrev = wlc_hw->boardrev;
4716         wlc->pub->boardflags = wlc_hw->boardflags;
4717         wlc->pub->boardflags2 = wlc_hw->boardflags2;
4718         wlc->pub->_nbands = wlc_hw->_nbands;
4719
4720         wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
4721
4722         if (wlc_hw->physhim == NULL) {
4723                 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
4724                         "failed\n", unit);
4725                 err = 25;
4726                 goto fail;
4727         }
4728
4729         /* pass all the parameters to wlc_phy_shared_attach in one struct */
4730         sha_params.sih = wlc_hw->sih;
4731         sha_params.physhim = wlc_hw->physhim;
4732         sha_params.unit = unit;
4733         sha_params.corerev = wlc_hw->corerev;
4734         sha_params.vars = vars;
4735         sha_params.vid = wlc_hw->vendorid;
4736         sha_params.did = wlc_hw->deviceid;
4737         sha_params.chip = wlc_hw->sih->chip;
4738         sha_params.chiprev = wlc_hw->sih->chiprev;
4739         sha_params.chippkg = wlc_hw->sih->chippkg;
4740         sha_params.sromrev = wlc_hw->sromrev;
4741         sha_params.boardtype = wlc_hw->sih->boardtype;
4742         sha_params.boardrev = wlc_hw->boardrev;
4743         sha_params.boardvendor = wlc_hw->sih->boardvendor;
4744         sha_params.boardflags = wlc_hw->boardflags;
4745         sha_params.boardflags2 = wlc_hw->boardflags2;
4746         sha_params.buscorerev = wlc_hw->sih->buscorerev;
4747
4748         /* alloc and save pointer to shared phy state area */
4749         wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
4750         if (!wlc_hw->phy_sh) {
4751                 err = 16;
4752                 goto fail;
4753         }
4754
4755         /* initialize software state for each core and band */
4756         for (j = 0; j < wlc_hw->_nbands; j++) {
4757                 /*
4758                  * band0 is always 2.4Ghz
4759                  * band1, if present, is 5Ghz
4760                  */
4761
4762                 brcms_c_setxband(wlc_hw, j);
4763
4764                 wlc_hw->band->bandunit = j;
4765                 wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4766                 wlc->band->bandunit = j;
4767                 wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4768                 wlc->core->coreidx = ai_coreidx(wlc_hw->sih);
4769
4770                 wlc_hw->machwcap = R_REG(&regs->machwcap);
4771                 wlc_hw->machwcap_backup = wlc_hw->machwcap;
4772
4773                 /* init tx fifo size */
4774                 wlc_hw->xmtfifo_sz =
4775                     xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
4776
4777                 /* Get a phy for this band */
4778                 wlc_hw->band->pi =
4779                         wlc_phy_attach(wlc_hw->phy_sh, regs,
4780                                         wlc_hw->band->bandtype, vars,
4781                                         wlc->wiphy);
4782                 if (wlc_hw->band->pi == NULL) {
4783                         wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
4784                                   "attach failed\n", unit);
4785                         err = 17;
4786                         goto fail;
4787                 }
4788
4789                 wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
4790
4791                 wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
4792                                        &wlc_hw->band->phyrev,
4793                                        &wlc_hw->band->radioid,
4794                                        &wlc_hw->band->radiorev);
4795                 wlc_hw->band->abgphy_encore =
4796                     wlc_phy_get_encore(wlc_hw->band->pi);
4797                 wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
4798                 wlc_hw->band->core_flags =
4799                     wlc_phy_get_coreflags(wlc_hw->band->pi);
4800
4801                 /* verify good phy_type & supported phy revision */
4802                 if (BRCMS_ISNPHY(wlc_hw->band)) {
4803                         if (NCONF_HAS(wlc_hw->band->phyrev))
4804                                 goto good_phy;
4805                         else
4806                                 goto bad_phy;
4807                 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
4808                         if (LCNCONF_HAS(wlc_hw->band->phyrev))
4809                                 goto good_phy;
4810                         else
4811                                 goto bad_phy;
4812                 } else {
4813  bad_phy:
4814                         wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
4815                                   "phy type/rev (%d/%d)\n", unit,
4816                                   wlc_hw->band->phytype, wlc_hw->band->phyrev);
4817                         err = 18;
4818                         goto fail;
4819                 }
4820
4821  good_phy:
4822                 /*
4823                  * BMAC_NOTE: wlc->band->pi should not be set below and should
4824                  * be done in the high level attach. However we can not make
4825                  * that change until all low level access is changed to
4826                  * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
4827                  * keeping wlc_hw->band->pi as well for incremental update of
4828                  * low level fns, and cut over low only init when all fns
4829                  * updated.
4830                  */
4831                 wlc->band->pi = wlc_hw->band->pi;
4832                 wlc->band->phytype = wlc_hw->band->phytype;
4833                 wlc->band->phyrev = wlc_hw->band->phyrev;
4834                 wlc->band->radioid = wlc_hw->band->radioid;
4835                 wlc->band->radiorev = wlc_hw->band->radiorev;
4836
4837                 /* default contention windows size limits */
4838                 wlc_hw->band->CWmin = APHY_CWMIN;
4839                 wlc_hw->band->CWmax = PHY_CWMAX;
4840
4841                 if (!brcms_b_attach_dmapio(wlc, j, wme)) {
4842                         err = 19;
4843                         goto fail;
4844                 }
4845         }
4846
4847         /* disable core to match driver "down" state */
4848         brcms_c_coredisable(wlc_hw);
4849
4850         /* Match driver "down" state */
4851         ai_pci_down(wlc_hw->sih);
4852
4853         /* register sb interrupt callback functions */
4854         ai_register_intr_callback(wlc_hw->sih, (void *)brcms_c_wlintrsoff,
4855                                   (void *)brcms_c_wlintrsrestore, NULL, wlc);
4856
4857         /* turn off pll and xtal to match driver "down" state */
4858         brcms_b_xtal(wlc_hw, OFF);
4859
4860         /* *******************************************************************
4861          * The hardware is in the DOWN state at this point. D11 core
4862          * or cores are in reset with clocks off, and the board PLLs
4863          * are off if possible.
4864          *
4865          * Beyond this point, wlc->sbclk == false and chip registers
4866          * should not be touched.
4867          *********************************************************************
4868          */
4869
4870         /* init etheraddr state variables */
4871         macaddr = brcms_c_get_macaddr(wlc_hw);
4872         if (macaddr == NULL) {
4873                 wiphy_err(wiphy, "wl%d: brcms_b_attach: macaddr not found\n",
4874                           unit);
4875                 err = 21;
4876                 goto fail;
4877         }
4878         if (!mac_pton(macaddr, wlc_hw->etheraddr) ||
4879             is_broadcast_ether_addr(wlc_hw->etheraddr) ||
4880             is_zero_ether_addr(wlc_hw->etheraddr)) {
4881                 wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr %s\n",
4882                           unit, macaddr);
4883                 err = 22;
4884                 goto fail;
4885         }
4886
4887         BCMMSG(wlc->wiphy,
4888                  "deviceid 0x%x nbands %d board 0x%x macaddr: %s\n",
4889                  wlc_hw->deviceid, wlc_hw->_nbands,
4890                  wlc_hw->sih->boardtype, macaddr);
4891
4892         return err;
4893
4894  fail:
4895         wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
4896                   err);
4897         return err;
4898 }
4899
4900 static void brcms_c_attach_antgain_init(struct brcms_c_info *wlc)
4901 {
4902         uint unit;
4903         unit = wlc->pub->unit;
4904
4905         if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
4906                 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
4907                 wlc->band->antgain = 8;
4908         } else if (wlc->band->antgain == -1) {
4909                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4910                           " srom, using 2dB\n", unit, __func__);
4911                 wlc->band->antgain = 8;
4912         } else {
4913                 s8 gain, fract;
4914                 /* Older sroms specified gain in whole dbm only.  In order
4915                  * be able to specify qdbm granularity and remain backward
4916                  * compatible the whole dbms are now encoded in only
4917                  * low 6 bits and remaining qdbms are encoded in the hi 2 bits.
4918                  * 6 bit signed number ranges from -32 - 31.
4919                  *
4920                  * Examples:
4921                  * 0x1 = 1 db,
4922                  * 0xc1 = 1.75 db (1 + 3 quarters),
4923                  * 0x3f = -1 (-1 + 0 quarters),
4924                  * 0x7f = -.75 (-1 + 1 quarters) = -3 qdbm.
4925                  * 0xbf = -.50 (-1 + 2 quarters) = -2 qdbm.
4926                  */
4927                 gain = wlc->band->antgain & 0x3f;
4928                 gain <<= 2;     /* Sign extend */
4929                 gain >>= 2;
4930                 fract = (wlc->band->antgain & 0xc0) >> 6;
4931                 wlc->band->antgain = 4 * gain + fract;
4932         }
4933 }
4934
4935 static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
4936 {
4937         int aa;
4938         uint unit;
4939         char *vars;
4940         int bandtype;
4941
4942         unit = wlc->pub->unit;
4943         vars = wlc->pub->vars;
4944         bandtype = wlc->band->bandtype;
4945
4946         /* get antennas available */
4947         aa = (s8) getintvar(vars, bandtype == BRCM_BAND_5G ? "aa5g" : "aa2g");
4948         if (aa == 0)
4949                 aa = (s8) getintvar(vars,
4950                                     bandtype == BRCM_BAND_5G ? "aa1" : "aa0");
4951         if ((aa < 1) || (aa > 15)) {
4952                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4953                           " srom (0x%x), using 3\n", unit, __func__, aa);
4954                 aa = 3;
4955         }
4956
4957         /* reset the defaults if we have a single antenna */
4958         if (aa == 1) {
4959                 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
4960                 wlc->stf->txant = ANT_TX_FORCE_0;
4961         } else if (aa == 2) {
4962                 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
4963                 wlc->stf->txant = ANT_TX_FORCE_1;
4964         } else {
4965         }
4966
4967         /* Compute Antenna Gain */
4968         wlc->band->antgain =
4969             (s8) getintvar(vars, bandtype == BRCM_BAND_5G ? "ag1" : "ag0");
4970         brcms_c_attach_antgain_init(wlc);
4971
4972         return true;
4973 }
4974
4975 static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
4976 {
4977         u16 chanspec;
4978         struct brcms_band *band;
4979         struct brcms_bss_info *bi = wlc->default_bss;
4980
4981         /* init default and target BSS with some sane initial values */
4982         memset((char *)(bi), 0, sizeof(struct brcms_bss_info));
4983         bi->beacon_period = BEACON_INTERVAL_DEFAULT;
4984
4985         /* fill the default channel as the first valid channel
4986          * starting from the 2G channels
4987          */
4988         chanspec = ch20mhz_chspec(1);
4989         wlc->home_chanspec = bi->chanspec = chanspec;
4990
4991         /* find the band of our default channel */
4992         band = wlc->band;
4993         if (wlc->pub->_nbands > 1 &&
4994             band->bandunit != chspec_bandunit(chanspec))
4995                 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
4996
4997         /* init bss rates to the band specific default rate set */
4998         brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
4999                 band->bandtype, false, BRCMS_RATE_MASK_FULL,
5000                 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
5001                 brcms_chspec_bw(chanspec), wlc->stf->txstreams);
5002
5003         if (wlc->pub->_n_enab & SUPPORT_11N)
5004                 bi->flags |= BRCMS_BSS_HT;
5005 }
5006
5007 static struct brcms_txq_info *brcms_c_txq_alloc(struct brcms_c_info *wlc)
5008 {
5009         struct brcms_txq_info *qi, *p;
5010
5011         qi = kzalloc(sizeof(struct brcms_txq_info), GFP_ATOMIC);
5012         if (qi != NULL) {
5013                 /*
5014                  * Have enough room for control packets along with HI watermark
5015                  * Also, add room to txq for total psq packets if all the SCBs
5016                  * leave PS mode. The watermark for flowcontrol to OS packets
5017                  * will remain the same
5018                  */
5019                 brcmu_pktq_init(&qi->q, BRCMS_PREC_COUNT,
5020                           2 * BRCMS_DATAHIWAT + PKTQ_LEN_DEFAULT);
5021
5022                 /* add this queue to the the global list */
5023                 p = wlc->tx_queues;
5024                 if (p == NULL) {
5025                         wlc->tx_queues = qi;
5026                 } else {
5027                         while (p->next != NULL)
5028                                 p = p->next;
5029                         p->next = qi;
5030                 }
5031         }
5032         return qi;
5033 }
5034
5035 static void brcms_c_txq_free(struct brcms_c_info *wlc,
5036                              struct brcms_txq_info *qi)
5037 {
5038         struct brcms_txq_info *p;
5039
5040         if (qi == NULL)
5041                 return;
5042
5043         /* remove the queue from the linked list */
5044         p = wlc->tx_queues;
5045         if (p == qi)
5046                 wlc->tx_queues = p->next;
5047         else {
5048                 while (p != NULL && p->next != qi)
5049                         p = p->next;
5050                 if (p != NULL)
5051                         p->next = p->next->next;
5052         }
5053
5054         kfree(qi);
5055 }
5056
5057 static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
5058 {
5059         uint i;
5060         struct brcms_band *band;
5061
5062         for (i = 0; i < wlc->pub->_nbands; i++) {
5063                 band = wlc->bandstate[i];
5064                 if (band->bandtype == BRCM_BAND_5G) {
5065                         if ((bwcap == BRCMS_N_BW_40ALL)
5066                             || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
5067                                 band->mimo_cap_40 = true;
5068                         else
5069                                 band->mimo_cap_40 = false;
5070                 } else {
5071                         if (bwcap == BRCMS_N_BW_40ALL)
5072                                 band->mimo_cap_40 = true;
5073                         else
5074                                 band->mimo_cap_40 = false;
5075                 }
5076         }
5077 }
5078
5079 /*
5080  * The common driver entry routine. Error codes should be unique
5081  */
5082 struct brcms_c_info *
5083 brcms_c_attach(struct brcms_info *wl, u16 vendor, u16 device, uint unit,
5084                bool piomode, void __iomem *regsva, struct pci_dev *btparam,
5085                uint *perr)
5086 {
5087         struct brcms_c_info *wlc;
5088         uint err = 0;
5089         uint i, j;
5090         struct brcms_pub *pub;
5091
5092         /* allocate struct brcms_c_info state and its substructures */
5093         wlc = (struct brcms_c_info *) brcms_c_attach_malloc(unit, &err, device);
5094         if (wlc == NULL)
5095                 goto fail;
5096         wlc->wiphy = wl->wiphy;
5097         pub = wlc->pub;
5098
5099 #if defined(BCMDBG)
5100         wlc_info_dbg = wlc;
5101 #endif
5102
5103         wlc->band = wlc->bandstate[0];
5104         wlc->core = wlc->corestate;
5105         wlc->wl = wl;
5106         pub->unit = unit;
5107         pub->_piomode = piomode;
5108         wlc->bandinit_pending = false;
5109
5110         /* populate struct brcms_c_info with default values  */
5111         brcms_c_info_init(wlc, unit);
5112
5113         /* update sta/ap related parameters */
5114         brcms_c_ap_upd(wlc);
5115
5116         /*
5117          * low level attach steps(all hw accesses go
5118          * inside, no more in rest of the attach)
5119          */
5120         err = brcms_b_attach(wlc, vendor, device, unit, piomode, regsva,
5121                              btparam);
5122         if (err)
5123                 goto fail;
5124
5125         brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
5126
5127         pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
5128
5129         /* propagate *vars* from BMAC driver to high driver */
5130         brcms_b_copyfrom_vars(wlc->hw, &pub->vars, &wlc->vars_size);
5131
5132
5133         /* set maximum allowed duty cycle */
5134         wlc->tx_duty_cycle_ofdm =
5135             (u16) getintvar(pub->vars, "tx_duty_cycle_ofdm");
5136         wlc->tx_duty_cycle_cck =
5137             (u16) getintvar(pub->vars, "tx_duty_cycle_cck");
5138
5139         brcms_c_stf_phy_chain_calc(wlc);
5140
5141         /* txchain 1: txant 0, txchain 2: txant 1 */
5142         if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
5143                 wlc->stf->txant = wlc->stf->hw_txchain - 1;
5144
5145         /* push to BMAC driver */
5146         wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
5147                                wlc->stf->hw_rxchain);
5148
5149         /* pull up some info resulting from the low attach */
5150         for (i = 0; i < NFIFO; i++)
5151                 wlc->core->txavail[i] = wlc->hw->txavail[i];
5152
5153         memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
5154         memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
5155
5156         for (j = 0; j < wlc->pub->_nbands; j++) {
5157                 wlc->band = wlc->bandstate[j];
5158
5159                 if (!brcms_c_attach_stf_ant_init(wlc)) {
5160                         err = 24;
5161                         goto fail;
5162                 }
5163
5164                 /* default contention windows size limits */
5165                 wlc->band->CWmin = APHY_CWMIN;
5166                 wlc->band->CWmax = PHY_CWMAX;
5167
5168                 /* init gmode value */
5169                 if (wlc->band->bandtype == BRCM_BAND_2G) {
5170                         wlc->band->gmode = GMODE_AUTO;
5171                         brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
5172                                            wlc->band->gmode);
5173                 }
5174
5175                 /* init _n_enab supported mode */
5176                 if (BRCMS_PHY_11N_CAP(wlc->band)) {
5177                         pub->_n_enab = SUPPORT_11N;
5178                         brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
5179                                                    ((pub->_n_enab ==
5180                                                      SUPPORT_11N) ? WL_11N_2x2 :
5181                                                     WL_11N_3x3));
5182                 }
5183
5184                 /* init per-band default rateset, depend on band->gmode */
5185                 brcms_default_rateset(wlc, &wlc->band->defrateset);
5186
5187                 /* fill in hw_rateset */
5188                 brcms_c_rateset_filter(&wlc->band->defrateset,
5189                                    &wlc->band->hw_rateset, false,
5190                                    BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
5191                                    (bool) (wlc->pub->_n_enab & SUPPORT_11N));
5192         }
5193
5194         /*
5195          * update antenna config due to
5196          * wlc->stf->txant/txchain/ant_rx_ovr change
5197          */
5198         brcms_c_stf_phy_txant_upd(wlc);
5199
5200         /* attach each modules */
5201         err = brcms_c_attach_module(wlc);
5202         if (err != 0)
5203                 goto fail;
5204
5205         if (!brcms_c_timers_init(wlc, unit)) {
5206                 wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
5207                           __func__);
5208                 err = 32;
5209                 goto fail;
5210         }
5211
5212         /* depend on rateset, gmode */
5213         wlc->cmi = brcms_c_channel_mgr_attach(wlc);
5214         if (!wlc->cmi) {
5215                 wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
5216                           "\n", unit, __func__);
5217                 err = 33;
5218                 goto fail;
5219         }
5220
5221         /* init default when all parameters are ready, i.e. ->rateset */
5222         brcms_c_bss_default_init(wlc);
5223
5224         /*
5225          * Complete the wlc default state initializations..
5226          */
5227
5228         /* allocate our initial queue */
5229         wlc->pkt_queue = brcms_c_txq_alloc(wlc);
5230         if (wlc->pkt_queue == NULL) {
5231                 wiphy_err(wl->wiphy, "wl%d: %s: failed to malloc tx queue\n",
5232                           unit, __func__);
5233                 err = 100;
5234                 goto fail;
5235         }
5236
5237         wlc->bsscfg->wlc = wlc;
5238
5239         wlc->mimoft = FT_HT;
5240         wlc->mimo_40txbw = AUTO;
5241         wlc->ofdm_40txbw = AUTO;
5242         wlc->cck_40txbw = AUTO;
5243         brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
5244
5245         /* Set default values of SGI */
5246         if (BRCMS_SGI_CAP_PHY(wlc)) {
5247                 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
5248                                                BRCMS_N_SGI_40));
5249         } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
5250                 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
5251                                                BRCMS_N_SGI_40));
5252         } else {
5253                 brcms_c_ht_update_sgi_rx(wlc, 0);
5254         }
5255
5256         /* initialize radio_mpc_disable according to wlc->mpc */
5257         brcms_c_radio_mpc_upd(wlc);
5258         brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
5259
5260         if (perr)
5261                 *perr = 0;
5262
5263         return wlc;
5264
5265  fail:
5266         wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
5267                   unit, __func__, err);
5268         if (wlc)
5269                 brcms_c_detach(wlc);
5270
5271         if (perr)
5272                 *perr = err;
5273         return NULL;
5274 }
5275
5276 static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
5277 {
5278         /* free timer state */
5279         if (wlc->wdtimer) {
5280                 brcms_free_timer(wlc->wl, wlc->wdtimer);
5281                 wlc->wdtimer = NULL;
5282         }
5283         if (wlc->radio_timer) {
5284                 brcms_free_timer(wlc->wl, wlc->radio_timer);
5285                 wlc->radio_timer = NULL;
5286         }
5287 }
5288
5289 static void brcms_c_detach_module(struct brcms_c_info *wlc)
5290 {
5291         if (wlc->asi) {
5292                 brcms_c_antsel_detach(wlc->asi);
5293                 wlc->asi = NULL;
5294         }
5295
5296         if (wlc->ampdu) {
5297                 brcms_c_ampdu_detach(wlc->ampdu);
5298                 wlc->ampdu = NULL;
5299         }
5300
5301         brcms_c_stf_detach(wlc);
5302 }
5303
5304 /*
5305  * low level detach
5306  */
5307 static int brcms_b_detach(struct brcms_c_info *wlc)
5308 {
5309         uint i;
5310         struct brcms_hw_band *band;
5311         struct brcms_hardware *wlc_hw = wlc->hw;
5312         int callbacks;
5313
5314         callbacks = 0;
5315
5316         if (wlc_hw->sih) {
5317                 /*
5318                  * detach interrupt sync mechanism since interrupt is disabled
5319                  * and per-port interrupt object may has been freed. this must
5320                  * be done before sb core switch
5321                  */
5322                 ai_deregister_intr_callback(wlc_hw->sih);
5323                 ai_pci_sleep(wlc_hw->sih);
5324         }
5325
5326         brcms_b_detach_dmapio(wlc_hw);
5327
5328         band = wlc_hw->band;
5329         for (i = 0; i < wlc_hw->_nbands; i++) {
5330                 if (band->pi) {
5331                         /* Detach this band's phy */
5332                         wlc_phy_detach(band->pi);
5333                         band->pi = NULL;
5334                 }
5335                 band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
5336         }
5337
5338         /* Free shared phy state */
5339         kfree(wlc_hw->phy_sh);
5340
5341         wlc_phy_shim_detach(wlc_hw->physhim);
5342
5343         /* free vars */
5344         kfree(wlc_hw->vars);
5345         wlc_hw->vars = NULL;
5346
5347         if (wlc_hw->sih) {
5348                 ai_detach(wlc_hw->sih);
5349                 wlc_hw->sih = NULL;
5350         }
5351
5352         return callbacks;
5353
5354 }
5355
5356 /*
5357  * Return a count of the number of driver callbacks still pending.
5358  *
5359  * General policy is that brcms_c_detach can only dealloc/free software states.
5360  * It can NOT touch hardware registers since the d11core may be in reset and
5361  * clock may not be available.
5362  * One exception is sb register access, which is possible if crystal is turned
5363  * on after "down" state, driver should avoid software timer with the exception
5364  * of radio_monitor.
5365  */
5366 uint brcms_c_detach(struct brcms_c_info *wlc)
5367 {
5368         uint callbacks = 0;
5369
5370         if (wlc == NULL)
5371                 return 0;
5372
5373         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
5374
5375         callbacks += brcms_b_detach(wlc);
5376
5377         /* delete software timers */
5378         if (!brcms_c_radio_monitor_stop(wlc))
5379                 callbacks++;
5380
5381         brcms_c_channel_mgr_detach(wlc->cmi);
5382
5383         brcms_c_timers_deinit(wlc);
5384
5385         brcms_c_detach_module(wlc);
5386
5387
5388         while (wlc->tx_queues != NULL)
5389                 brcms_c_txq_free(wlc, wlc->tx_queues);
5390
5391         brcms_c_detach_mfree(wlc);
5392         return callbacks;
5393 }
5394
5395 /* update state that depends on the current value of "ap" */
5396 void brcms_c_ap_upd(struct brcms_c_info *wlc)
5397 {
5398         /* STA-BSS; short capable */
5399         wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
5400
5401         /* fixup mpc */
5402         wlc->mpc = true;
5403 }
5404
5405 /*
5406  * return true if Minimum Power Consumption should
5407  * be entered, false otherwise
5408  */
5409 bool brcms_c_is_non_delay_mpc(struct brcms_c_info *wlc)
5410 {
5411         return false;
5412 }
5413
5414 bool brcms_c_ismpc(struct brcms_c_info *wlc)
5415 {
5416         return (wlc->mpc_delay_off == 0) && (brcms_c_is_non_delay_mpc(wlc));
5417 }
5418
5419 void brcms_c_radio_mpc_upd(struct brcms_c_info *wlc)
5420 {
5421         bool mpc_radio, radio_state;
5422
5423         /*
5424          * Clear the WL_RADIO_MPC_DISABLE bit when mpc feature is disabled
5425          * in case the WL_RADIO_MPC_DISABLE bit was set. Stop the radio
5426          * monitor also when WL_RADIO_MPC_DISABLE is the only reason that
5427          * the radio is going down.
5428          */
5429         if (!wlc->mpc) {
5430                 if (!wlc->pub->radio_disabled)
5431                         return;
5432                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
5433                 brcms_c_radio_upd(wlc);
5434                 if (!wlc->pub->radio_disabled)
5435                         brcms_c_radio_monitor_stop(wlc);
5436                 return;
5437         }
5438
5439         /*
5440          * sync ismpc logic with WL_RADIO_MPC_DISABLE bit in
5441          * wlc->pub->radio_disabled to go ON, always call radio_upd
5442          * synchronously to go OFF, postpone radio_upd to later when
5443          * context is safe(e.g. watchdog)
5444          */
5445         radio_state =
5446             (mboolisset(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE) ? OFF :
5447              ON);
5448         mpc_radio = (brcms_c_ismpc(wlc) == true) ? OFF : ON;
5449
5450         if (radio_state == ON && mpc_radio == OFF)
5451                 wlc->mpc_delay_off = wlc->mpc_dlycnt;
5452         else if (radio_state == OFF && mpc_radio == ON) {
5453                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
5454                 brcms_c_radio_upd(wlc);
5455                 if (wlc->mpc_offcnt < BRCMS_MPC_THRESHOLD)
5456                         wlc->mpc_dlycnt = BRCMS_MPC_MAX_DELAYCNT;
5457                 else
5458                         wlc->mpc_dlycnt = BRCMS_MPC_MIN_DELAYCNT;
5459         }
5460         /*
5461          * Below logic is meant to capture the transition from mpc off
5462          * to mpc on for reasons other than wlc->mpc_delay_off keeping
5463          * the mpc off. In that case reset wlc->mpc_delay_off to
5464          * wlc->mpc_dlycnt, so that we restart the countdown of mpc_delay_off
5465          */
5466         if ((wlc->prev_non_delay_mpc == false) &&
5467             (brcms_c_is_non_delay_mpc(wlc) == true) && wlc->mpc_delay_off)
5468                 wlc->mpc_delay_off = wlc->mpc_dlycnt;
5469
5470         wlc->prev_non_delay_mpc = brcms_c_is_non_delay_mpc(wlc);
5471 }
5472 /* Initialize just the hardware when coming out of POR or S3/S5 system states */
5473 static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
5474 {
5475         if (wlc_hw->wlc->pub->hw_up)
5476                 return;
5477
5478         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
5479
5480         /*
5481          * Enable pll and xtal, initialize the power control registers,
5482          * and force fastclock for the remainder of brcms_c_up().
5483          */
5484         brcms_b_xtal(wlc_hw, ON);
5485         ai_clkctl_init(wlc_hw->sih);
5486         brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
5487
5488         ai_pci_fixcfg(wlc_hw->sih);
5489
5490         /*
5491          * AI chip doesn't restore bar0win2 on
5492          * hibernation/resume, need sw fixup
5493          */
5494         if ((wlc_hw->sih->chip == BCM43224_CHIP_ID) ||
5495             (wlc_hw->sih->chip == BCM43225_CHIP_ID))
5496                 wlc_hw->regs = (struct d11regs __iomem *)
5497                                 ai_setcore(wlc_hw->sih, D11_CORE_ID, 0);
5498
5499         /*
5500          * Inform phy that a POR reset has occurred so
5501          * it does a complete phy init
5502          */
5503         wlc_phy_por_inform(wlc_hw->band->pi);
5504
5505         wlc_hw->ucode_loaded = false;
5506         wlc_hw->wlc->pub->hw_up = true;
5507
5508         if ((wlc_hw->boardflags & BFL_FEM)
5509             && (wlc_hw->sih->chip == BCM4313_CHIP_ID)) {
5510                 if (!
5511                     (wlc_hw->boardrev >= 0x1250
5512                      && (wlc_hw->boardflags & BFL_FEM_BT)))
5513                         ai_epa_4313war(wlc_hw->sih);
5514         }
5515 }
5516
5517 static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
5518 {
5519         uint coremask;
5520
5521         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
5522
5523         /*
5524          * Enable pll and xtal, initialize the power control registers,
5525          * and force fastclock for the remainder of brcms_c_up().
5526          */
5527         brcms_b_xtal(wlc_hw, ON);
5528         ai_clkctl_init(wlc_hw->sih);
5529         brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
5530
5531         /*
5532          * Configure pci/pcmcia here instead of in brcms_c_attach()
5533          * to allow mfg hotswap:  down, hotswap (chip power cycle), up.
5534          */
5535         coremask = (1 << wlc_hw->wlc->core->coreidx);
5536
5537         ai_pci_setup(wlc_hw->sih, coremask);
5538
5539         /*
5540          * Need to read the hwradio status here to cover the case where the
5541          * system is loaded with the hw radio disabled. We do not want to
5542          * bring the driver up in this case.
5543          */
5544         if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
5545                 /* put SB PCI in down state again */
5546                 ai_pci_down(wlc_hw->sih);
5547                 brcms_b_xtal(wlc_hw, OFF);
5548                 return -ENOMEDIUM;
5549         }
5550
5551         ai_pci_up(wlc_hw->sih);
5552
5553         /* reset the d11 core */
5554         brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
5555
5556         return 0;
5557 }
5558
5559 static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
5560 {
5561         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
5562
5563         wlc_hw->up = true;
5564         wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
5565
5566         /* FULLY enable dynamic power control and d11 core interrupt */
5567         brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
5568         brcms_intrson(wlc_hw->wlc->wl);
5569         return 0;
5570 }
5571
5572 /*
5573  * Write WME tunable parameters for retransmit/max rate
5574  * from wlc struct to ucode
5575  */
5576 static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
5577 {
5578         int ac;
5579
5580         /* Need clock to do this */
5581         if (!wlc->clk)
5582                 return;
5583
5584         for (ac = 0; ac < AC_COUNT; ac++)
5585                 brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
5586                                   wlc->wme_retries[ac]);
5587 }
5588
5589 /* make interface operational */
5590 int brcms_c_up(struct brcms_c_info *wlc)
5591 {
5592         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
5593
5594         /* HW is turned off so don't try to access it */
5595         if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
5596                 return -ENOMEDIUM;
5597
5598         if (!wlc->pub->hw_up) {
5599                 brcms_b_hw_up(wlc->hw);
5600                 wlc->pub->hw_up = true;
5601         }
5602
5603         if ((wlc->pub->boardflags & BFL_FEM)
5604             && (wlc->pub->sih->chip == BCM4313_CHIP_ID)) {
5605                 if (wlc->pub->boardrev >= 0x1250
5606                     && (wlc->pub->boardflags & BFL_FEM_BT))
5607                         brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
5608                                 MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
5609                 else
5610                         brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
5611                                     MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
5612         }
5613
5614         /*
5615          * Need to read the hwradio status here to cover the case where the
5616          * system is loaded with the hw radio disabled. We do not want to bring
5617          * the driver up in this case. If radio is disabled, abort up, lower
5618          * power, start radio timer and return 0(for NDIS) don't call
5619          * radio_update to avoid looping brcms_c_up.
5620          *
5621          * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
5622          */
5623         if (!wlc->pub->radio_disabled) {
5624                 int status = brcms_b_up_prep(wlc->hw);
5625                 if (status == -ENOMEDIUM) {
5626                         if (!mboolisset
5627                             (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
5628                                 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
5629                                 mboolset(wlc->pub->radio_disabled,
5630                                          WL_RADIO_HW_DISABLE);
5631
5632                                 if (bsscfg->enable && bsscfg->BSS)
5633                                         wiphy_err(wlc->wiphy, "wl%d: up"
5634                                                   ": rfdisable -> "
5635                                                   "bsscfg_disable()\n",
5636                                                    wlc->pub->unit);
5637                         }
5638                 }
5639         }
5640
5641         if (wlc->pub->radio_disabled) {
5642                 brcms_c_radio_monitor_start(wlc);
5643                 return 0;
5644         }
5645
5646         /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
5647         wlc->clk = true;
5648
5649         brcms_c_radio_monitor_stop(wlc);
5650
5651         /* Set EDCF hostflags */
5652         brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
5653
5654         brcms_init(wlc->wl);
5655         wlc->pub->up = true;
5656
5657         if (wlc->bandinit_pending) {
5658                 brcms_c_suspend_mac_and_wait(wlc);
5659                 brcms_c_set_chanspec(wlc, wlc->default_bss->chanspec);
5660                 wlc->bandinit_pending = false;
5661                 brcms_c_enable_mac(wlc);
5662         }
5663
5664         brcms_b_up_finish(wlc->hw);
5665
5666         /* Program the TX wme params with the current settings */
5667         brcms_c_wme_retries_write(wlc);
5668
5669         /* start one second watchdog timer */
5670         brcms_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
5671         wlc->WDarmed = true;
5672
5673         /* ensure antenna config is up to date */
5674         brcms_c_stf_phy_txant_upd(wlc);
5675         /* ensure LDPC config is in sync */
5676         brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
5677
5678         return 0;
5679 }
5680
5681 static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
5682 {
5683         uint callbacks = 0;
5684
5685         return callbacks;
5686 }
5687
5688 static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
5689 {
5690         bool dev_gone;
5691         uint callbacks = 0;
5692
5693         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
5694
5695         if (!wlc_hw->up)
5696                 return callbacks;
5697
5698         dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5699
5700         /* disable interrupts */
5701         if (dev_gone)
5702                 wlc_hw->wlc->macintmask = 0;
5703         else {
5704                 /* now disable interrupts */
5705                 brcms_intrsoff(wlc_hw->wlc->wl);
5706
5707                 /* ensure we're running on the pll clock again */
5708                 brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
5709         }
5710         /* down phy at the last of this stage */
5711         callbacks += wlc_phy_down(wlc_hw->band->pi);
5712
5713         return callbacks;
5714 }
5715
5716 static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
5717 {
5718         uint callbacks = 0;
5719         bool dev_gone;
5720
5721         BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
5722
5723         if (!wlc_hw->up)
5724                 return callbacks;
5725
5726         wlc_hw->up = false;
5727         wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
5728
5729         dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5730
5731         if (dev_gone) {
5732                 wlc_hw->sbclk = false;
5733                 wlc_hw->clk = false;
5734                 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
5735
5736                 /* reclaim any posted packets */
5737                 brcms_c_flushqueues(wlc_hw->wlc);
5738         } else {
5739
5740                 /* Reset and disable the core */
5741                 if (ai_iscoreup(wlc_hw->sih)) {
5742                         if (R_REG(&wlc_hw->regs->maccontrol) &
5743                             MCTL_EN_MAC)
5744                                 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
5745                         callbacks += brcms_reset(wlc_hw->wlc->wl);
5746                         brcms_c_coredisable(wlc_hw);
5747                 }
5748
5749                 /* turn off primary xtal and pll */
5750                 if (!wlc_hw->noreset) {
5751                         ai_pci_down(wlc_hw->sih);
5752                         brcms_b_xtal(wlc_hw, OFF);
5753                 }
5754         }
5755
5756         return callbacks;
5757 }
5758
5759 /*
5760  * Mark the interface nonoperational, stop the software mechanisms,
5761  * disable the hardware, free any transient buffer state.
5762  * Return a count of the number of driver callbacks still pending.
5763  */
5764 uint brcms_c_down(struct brcms_c_info *wlc)
5765 {
5766
5767         uint callbacks = 0;
5768         int i;
5769         bool dev_gone = false;
5770         struct brcms_txq_info *qi;
5771
5772         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
5773
5774         /* check if we are already in the going down path */
5775         if (wlc->going_down) {
5776                 wiphy_err(wlc->wiphy, "wl%d: %s: Driver going down so return"
5777                           "\n", wlc->pub->unit, __func__);
5778                 return 0;
5779         }
5780         if (!wlc->pub->up)
5781                 return callbacks;
5782
5783         /* in between, mpc could try to bring down again.. */
5784         wlc->going_down = true;
5785
5786         callbacks += brcms_b_bmac_down_prep(wlc->hw);
5787
5788         dev_gone = brcms_deviceremoved(wlc);
5789
5790         /* Call any registered down handlers */
5791         for (i = 0; i < BRCMS_MAXMODULES; i++) {
5792                 if (wlc->modulecb[i].down_fn)
5793                         callbacks +=
5794                             wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
5795         }
5796
5797         /* cancel the watchdog timer */
5798         if (wlc->WDarmed) {
5799                 if (!brcms_del_timer(wlc->wl, wlc->wdtimer))
5800                         callbacks++;
5801                 wlc->WDarmed = false;
5802         }
5803         /* cancel all other timers */
5804         callbacks += brcms_c_down_del_timer(wlc);
5805
5806         wlc->pub->up = false;
5807
5808         wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
5809
5810         /* clear txq flow control */
5811         brcms_c_txflowcontrol_reset(wlc);
5812
5813         /* flush tx queues */
5814         for (qi = wlc->tx_queues; qi != NULL; qi = qi->next)
5815                 brcmu_pktq_flush(&qi->q, true, NULL, NULL);
5816
5817         callbacks += brcms_b_down_finish(wlc->hw);
5818
5819         /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
5820         wlc->clk = false;
5821
5822         wlc->going_down = false;
5823         return callbacks;
5824 }
5825
5826 /* Set the current gmode configuration */
5827 int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
5828 {
5829         int ret = 0;
5830         uint i;
5831         struct brcms_c_rateset rs;
5832         /* Default to 54g Auto */
5833         /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
5834         s8 shortslot = BRCMS_SHORTSLOT_AUTO;
5835         bool shortslot_restrict = false; /* Restrict association to stations
5836                                           * that support shortslot
5837                                           */
5838         bool ofdm_basic = false;        /* Make 6, 12, and 24 basic rates */
5839         /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
5840         int preamble = BRCMS_PLCP_LONG;
5841         bool preamble_restrict = false; /* Restrict association to stations
5842                                          * that support short preambles
5843                                          */
5844         struct brcms_band *band;
5845
5846         /* if N-support is enabled, allow Gmode set as long as requested
5847          * Gmode is not GMODE_LEGACY_B
5848          */
5849         if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
5850                 return -ENOTSUPP;
5851
5852         /* verify that we are dealing with 2G band and grab the band pointer */
5853         if (wlc->band->bandtype == BRCM_BAND_2G)
5854                 band = wlc->band;
5855         else if ((wlc->pub->_nbands > 1) &&
5856                  (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
5857                 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5858         else
5859                 return -EINVAL;
5860
5861         /* Legacy or bust when no OFDM is supported by regulatory */
5862         if ((brcms_c_channel_locale_flags_in_band(wlc->cmi, band->bandunit) &
5863              BRCMS_NO_OFDM) && (gmode != GMODE_LEGACY_B))
5864                 return -EINVAL;
5865
5866         /* update configuration value */
5867         if (config == true)
5868                 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
5869
5870         /* Clear rateset override */
5871         memset(&rs, 0, sizeof(struct brcms_c_rateset));
5872
5873         switch (gmode) {
5874         case GMODE_LEGACY_B:
5875                 shortslot = BRCMS_SHORTSLOT_OFF;
5876                 brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
5877
5878                 break;
5879
5880         case GMODE_LRS:
5881                 break;
5882
5883         case GMODE_AUTO:
5884                 /* Accept defaults */
5885                 break;
5886
5887         case GMODE_ONLY:
5888                 ofdm_basic = true;
5889                 preamble = BRCMS_PLCP_SHORT;
5890                 preamble_restrict = true;
5891                 break;
5892
5893         case GMODE_PERFORMANCE:
5894                 shortslot = BRCMS_SHORTSLOT_ON;
5895                 shortslot_restrict = true;
5896                 ofdm_basic = true;
5897                 preamble = BRCMS_PLCP_SHORT;
5898                 preamble_restrict = true;
5899                 break;
5900
5901         default:
5902                 /* Error */
5903                 wiphy_err(wlc->wiphy, "wl%d: %s: invalid gmode %d\n",
5904                           wlc->pub->unit, __func__, gmode);
5905                 return -ENOTSUPP;
5906         }
5907
5908         band->gmode = gmode;
5909
5910         wlc->shortslot_override = shortslot;
5911
5912         /* Use the default 11g rateset */
5913         if (!rs.count)
5914                 brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
5915
5916         if (ofdm_basic) {
5917                 for (i = 0; i < rs.count; i++) {
5918                         if (rs.rates[i] == BRCM_RATE_6M
5919                             || rs.rates[i] == BRCM_RATE_12M
5920                             || rs.rates[i] == BRCM_RATE_24M)
5921                                 rs.rates[i] |= BRCMS_RATE_FLAG;
5922                 }
5923         }
5924
5925         /* Set default bss rateset */
5926         wlc->default_bss->rateset.count = rs.count;
5927         memcpy(wlc->default_bss->rateset.rates, rs.rates,
5928                sizeof(wlc->default_bss->rateset.rates));
5929
5930         return ret;
5931 }
5932
5933 static int brcms_c_nmode_validate(struct brcms_c_info *wlc, s32 nmode)
5934 {
5935         int err = 0;
5936
5937         switch (nmode) {
5938
5939         case OFF:
5940                 break;
5941
5942         case AUTO:
5943         case WL_11N_2x2:
5944         case WL_11N_3x3:
5945                 if (!(BRCMS_PHY_11N_CAP(wlc->band)))
5946                         err = -EINVAL;
5947                 break;
5948
5949         default:
5950                 err = -EINVAL;
5951                 break;
5952         }
5953
5954         return err;
5955 }
5956
5957 int brcms_c_set_nmode(struct brcms_c_info *wlc)
5958 {
5959         uint i;
5960         int err;
5961         s32 nmode = AUTO;
5962
5963         err = brcms_c_nmode_validate(wlc, nmode);
5964         if (err)
5965                 return err;
5966
5967         if (wlc->stf->txstreams == WL_11N_3x3)
5968                 nmode = WL_11N_3x3;
5969         else
5970                 nmode = WL_11N_2x2;
5971
5972         /* force GMODE_AUTO if NMODE is ON */
5973         brcms_c_set_gmode(wlc, GMODE_AUTO, true);
5974         if (nmode == WL_11N_3x3)
5975                 wlc->pub->_n_enab = SUPPORT_HT;
5976         else
5977                 wlc->pub->_n_enab = SUPPORT_11N;
5978         wlc->default_bss->flags |= BRCMS_BSS_HT;
5979         /* add the mcs rates to the default and hw ratesets */
5980         brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
5981                               wlc->stf->txstreams);
5982         for (i = 0; i < wlc->pub->_nbands; i++)
5983                 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
5984                        wlc->default_bss->rateset.mcs, MCSSET_LEN);
5985
5986         return 0;
5987 }
5988
5989 static int
5990 brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
5991                              struct brcms_c_rateset *rs_arg)
5992 {
5993         struct brcms_c_rateset rs, new;
5994         uint bandunit;
5995
5996         memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
5997
5998         /* check for bad count value */
5999         if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
6000                 return -EINVAL;
6001
6002         /* try the current band */
6003         bandunit = wlc->band->bandunit;
6004         memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
6005         if (brcms_c_rate_hwrs_filter_sort_validate
6006             (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
6007              wlc->stf->txstreams))
6008                 goto good;
6009
6010         /* try the other band */
6011         if (brcms_is_mband_unlocked(wlc)) {
6012                 bandunit = OTHERBANDUNIT(wlc);
6013                 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
6014                 if (brcms_c_rate_hwrs_filter_sort_validate(&new,
6015                                                        &wlc->
6016                                                        bandstate[bandunit]->
6017                                                        hw_rateset, true,
6018                                                        wlc->stf->txstreams))
6019                         goto good;
6020         }
6021
6022         return -EBADE;
6023
6024  good:
6025         /* apply new rateset */
6026         memcpy(&wlc->default_bss->rateset, &new,
6027                sizeof(struct brcms_c_rateset));
6028         memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
6029                sizeof(struct brcms_c_rateset));
6030         return 0;
6031 }
6032
6033 static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
6034 {
6035         u8 r;
6036         bool war = false;
6037
6038         if (wlc->bsscfg->associated)
6039                 r = wlc->bsscfg->current_bss->rateset.rates[0];
6040         else
6041                 r = wlc->default_bss->rateset.rates[0];
6042
6043         wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
6044 }
6045
6046 int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
6047 {
6048         u16 chspec = ch20mhz_chspec(channel);
6049
6050         if (channel < 0 || channel > MAXCHANNEL)
6051                 return -EINVAL;
6052
6053         if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
6054                 return -EINVAL;
6055
6056
6057         if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
6058                 if (wlc->band->bandunit != chspec_bandunit(chspec))
6059                         wlc->bandinit_pending = true;
6060                 else
6061                         wlc->bandinit_pending = false;
6062         }
6063
6064         wlc->default_bss->chanspec = chspec;
6065         /* brcms_c_BSSinit() will sanitize the rateset before
6066          * using it.. */
6067         if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
6068                 brcms_c_set_home_chanspec(wlc, chspec);
6069                 brcms_c_suspend_mac_and_wait(wlc);
6070                 brcms_c_set_chanspec(wlc, chspec);
6071                 brcms_c_enable_mac(wlc);
6072         }
6073         return 0;
6074 }
6075
6076 int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
6077 {
6078         int ac;
6079
6080         if (srl < 1 || srl > RETRY_SHORT_MAX ||
6081             lrl < 1 || lrl > RETRY_SHORT_MAX)
6082                 return -EINVAL;
6083
6084         wlc->SRL = srl;
6085         wlc->LRL = lrl;
6086
6087         brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
6088
6089         for (ac = 0; ac < AC_COUNT; ac++) {
6090                 wlc->wme_retries[ac] =  SFIELD(wlc->wme_retries[ac],
6091                                                EDCF_SHORT,  wlc->SRL);
6092                 wlc->wme_retries[ac] =  SFIELD(wlc->wme_retries[ac],
6093                                                EDCF_LONG, wlc->LRL);
6094         }
6095         brcms_c_wme_retries_write(wlc);
6096
6097         return 0;
6098 }
6099
6100 void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
6101                                  struct brcm_rateset *currs)
6102 {
6103         struct brcms_c_rateset *rs;
6104
6105         if (wlc->pub->associated)
6106                 rs = &wlc->bsscfg->current_bss->rateset;
6107         else
6108                 rs = &wlc->default_bss->rateset;
6109
6110         /* Copy only legacy rateset section */
6111         currs->count = rs->count;
6112         memcpy(&currs->rates, &rs->rates, rs->count);
6113 }
6114
6115 int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
6116 {
6117         struct brcms_c_rateset internal_rs;
6118         int bcmerror;
6119
6120         if (rs->count > BRCMS_NUMRATES)
6121                 return -ENOBUFS;
6122
6123         memset(&internal_rs, 0, sizeof(struct brcms_c_rateset));
6124
6125         /* Copy only legacy rateset section */
6126         internal_rs.count = rs->count;
6127         memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
6128
6129         /* merge rateset coming in with the current mcsset */
6130         if (wlc->pub->_n_enab & SUPPORT_11N) {
6131                 struct brcms_bss_info *mcsset_bss;
6132                 if (wlc->bsscfg->associated)
6133                         mcsset_bss = wlc->bsscfg->current_bss;
6134                 else
6135                         mcsset_bss = wlc->default_bss;
6136                 memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
6137                        MCSSET_LEN);
6138         }
6139
6140         bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
6141         if (!bcmerror)
6142                 brcms_c_ofdm_rateset_war(wlc);
6143
6144         return bcmerror;
6145 }
6146
6147 int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
6148 {
6149         if (period < DOT11_MIN_BEACON_PERIOD ||
6150             period > DOT11_MAX_BEACON_PERIOD)
6151                 return -EINVAL;
6152
6153         wlc->default_bss->beacon_period = period;
6154         return 0;
6155 }
6156
6157 u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
6158 {
6159         return wlc->band->phytype;
6160 }
6161
6162 void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
6163 {
6164         wlc->shortslot_override = sslot_override;
6165
6166         /*
6167          * shortslot is an 11g feature, so no more work if we are
6168          * currently on the 5G band
6169          */
6170         if (wlc->band->bandtype == BRCM_BAND_5G)
6171                 return;
6172
6173         if (wlc->pub->up && wlc->pub->associated) {
6174                 /* let watchdog or beacon processing update shortslot */
6175         } else if (wlc->pub->up) {
6176                 /* unassociated shortslot is off */
6177                 brcms_c_switch_shortslot(wlc, false);
6178         } else {
6179                 /* driver is down, so just update the brcms_c_info
6180                  * value */
6181                 if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
6182                         wlc->shortslot = false;
6183                 else
6184                         wlc->shortslot =
6185                             (wlc->shortslot_override ==
6186                              BRCMS_SHORTSLOT_ON);
6187         }
6188 }
6189
6190 /*
6191  * register watchdog and down handlers.
6192  */
6193 int brcms_c_module_register(struct brcms_pub *pub,
6194                             const char *name, struct brcms_info *hdl,
6195                             int (*d_fn)(void *handle))
6196 {
6197         struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
6198         int i;
6199
6200         /* find an empty entry and just add, no duplication check! */
6201         for (i = 0; i < BRCMS_MAXMODULES; i++) {
6202                 if (wlc->modulecb[i].name[0] == '\0') {
6203                         strncpy(wlc->modulecb[i].name, name,
6204                                 sizeof(wlc->modulecb[i].name) - 1);
6205                         wlc->modulecb[i].hdl = hdl;
6206                         wlc->modulecb[i].down_fn = d_fn;
6207                         return 0;
6208                 }
6209         }
6210
6211         return -ENOSR;
6212 }
6213
6214 /* unregister module callbacks */
6215 int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
6216                               struct brcms_info *hdl)
6217 {
6218         struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
6219         int i;
6220
6221         if (wlc == NULL)
6222                 return -ENODATA;
6223
6224         for (i = 0; i < BRCMS_MAXMODULES; i++) {
6225                 if (!strcmp(wlc->modulecb[i].name, name) &&
6226                     (wlc->modulecb[i].hdl == hdl)) {
6227                         memset(&wlc->modulecb[i], 0, sizeof(struct modulecb));
6228                         return 0;
6229                 }
6230         }
6231
6232         /* table not found! */
6233         return -ENODATA;
6234 }
6235
6236 #ifdef BCMDBG
6237 static const char * const supr_reason[] = {
6238         "None", "PMQ Entry", "Flush request",
6239         "Previous frag failure", "Channel mismatch",
6240         "Lifetime Expiry", "Underflow"
6241 };
6242
6243 static void brcms_c_print_txs_status(u16 s)
6244 {
6245         printk(KERN_DEBUG "[15:12]  %d  frame attempts\n",
6246                (s & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT);
6247         printk(KERN_DEBUG " [11:8]  %d  rts attempts\n",
6248                (s & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT);
6249         printk(KERN_DEBUG "    [7]  %d  PM mode indicated\n",
6250                ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
6251         printk(KERN_DEBUG "    [6]  %d  intermediate status\n",
6252                ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
6253         printk(KERN_DEBUG "    [5]  %d  AMPDU\n",
6254                (s & TX_STATUS_AMPDU) ? 1 : 0);
6255         printk(KERN_DEBUG "  [4:2]  %d  Frame Suppressed Reason (%s)\n",
6256                ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
6257                supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
6258         printk(KERN_DEBUG "    [1]  %d  acked\n",
6259                ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
6260 }
6261 #endif                          /* BCMDBG */
6262
6263 void brcms_c_print_txstatus(struct tx_status *txs)
6264 {
6265 #if defined(BCMDBG)
6266         u16 s = txs->status;
6267         u16 ackphyrxsh = txs->ackphyrxsh;
6268
6269         printk(KERN_DEBUG "\ntxpkt (MPDU) Complete\n");
6270
6271         printk(KERN_DEBUG "FrameID: %04x   ", txs->frameid);
6272         printk(KERN_DEBUG "TxStatus: %04x", s);
6273         printk(KERN_DEBUG "\n");
6274
6275         brcms_c_print_txs_status(s);
6276
6277         printk(KERN_DEBUG "LastTxTime: %04x ", txs->lasttxtime);
6278         printk(KERN_DEBUG "Seq: %04x ", txs->sequence);
6279         printk(KERN_DEBUG "PHYTxStatus: %04x ", txs->phyerr);
6280         printk(KERN_DEBUG "RxAckRSSI: %04x ",
6281                (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
6282         printk(KERN_DEBUG "RxAckSQ: %04x",
6283                (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
6284         printk(KERN_DEBUG "\n");
6285 #endif                          /* defined(BCMDBG) */
6286 }
6287
6288 void brcms_c_statsupd(struct brcms_c_info *wlc)
6289 {
6290         int i;
6291         struct macstat macstats;
6292 #ifdef BCMDBG
6293         u16 delta;
6294         u16 rxf0ovfl;
6295         u16 txfunfl[NFIFO];
6296 #endif                          /* BCMDBG */
6297
6298         /* if driver down, make no sense to update stats */
6299         if (!wlc->pub->up)
6300                 return;
6301
6302 #ifdef BCMDBG
6303         /* save last rx fifo 0 overflow count */
6304         rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
6305
6306         /* save last tx fifo  underflow count */
6307         for (i = 0; i < NFIFO; i++)
6308                 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
6309 #endif                          /* BCMDBG */
6310
6311         /* Read mac stats from contiguous shared memory */
6312         brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, &macstats,
6313                                 sizeof(struct macstat), OBJADDR_SHM_SEL);
6314
6315 #ifdef BCMDBG
6316         /* check for rx fifo 0 overflow */
6317         delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
6318         if (delta)
6319                 wiphy_err(wlc->wiphy, "wl%d: %u rx fifo 0 overflows!\n",
6320                           wlc->pub->unit, delta);
6321
6322         /* check for tx fifo underflows */
6323         for (i = 0; i < NFIFO; i++) {
6324                 delta =
6325                     (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
6326                               txfunfl[i]);
6327                 if (delta)
6328                         wiphy_err(wlc->wiphy, "wl%d: %u tx fifo %d underflows!"
6329                                   "\n", wlc->pub->unit, delta, i);
6330         }
6331 #endif                          /* BCMDBG */
6332
6333         /* merge counters from dma module */
6334         for (i = 0; i < NFIFO; i++) {
6335                 if (wlc->hw->di[i])
6336                         dma_counterreset(wlc->hw->di[i]);
6337         }
6338 }
6339
6340 bool brcms_c_chipmatch(u16 vendor, u16 device)
6341 {
6342         if (vendor != PCI_VENDOR_ID_BROADCOM) {
6343                 pr_err("chipmatch: unknown vendor id %04x\n", vendor);
6344                 return false;
6345         }
6346
6347         if (device == BCM43224_D11N_ID_VEN1)
6348                 return true;
6349         if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
6350                 return true;
6351         if (device == BCM4313_D11N2G_ID)
6352                 return true;
6353         if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
6354                 return true;
6355
6356         pr_err("chipmatch: unknown device id %04x\n", device);
6357         return false;
6358 }
6359
6360 #if defined(BCMDBG)
6361 void brcms_c_print_txdesc(struct d11txh *txh)
6362 {
6363         u16 mtcl = le16_to_cpu(txh->MacTxControlLow);
6364         u16 mtch = le16_to_cpu(txh->MacTxControlHigh);
6365         u16 mfc = le16_to_cpu(txh->MacFrameControl);
6366         u16 tfest = le16_to_cpu(txh->TxFesTimeNormal);
6367         u16 ptcw = le16_to_cpu(txh->PhyTxControlWord);
6368         u16 ptcw_1 = le16_to_cpu(txh->PhyTxControlWord_1);
6369         u16 ptcw_1_Fbr = le16_to_cpu(txh->PhyTxControlWord_1_Fbr);
6370         u16 ptcw_1_Rts = le16_to_cpu(txh->PhyTxControlWord_1_Rts);
6371         u16 ptcw_1_FbrRts = le16_to_cpu(txh->PhyTxControlWord_1_FbrRts);
6372         u16 mainrates = le16_to_cpu(txh->MainRates);
6373         u16 xtraft = le16_to_cpu(txh->XtraFrameTypes);
6374         u8 *iv = txh->IV;
6375         u8 *ra = txh->TxFrameRA;
6376         u16 tfestfb = le16_to_cpu(txh->TxFesTimeFallback);
6377         u8 *rtspfb = txh->RTSPLCPFallback;
6378         u16 rtsdfb = le16_to_cpu(txh->RTSDurFallback);
6379         u8 *fragpfb = txh->FragPLCPFallback;
6380         u16 fragdfb = le16_to_cpu(txh->FragDurFallback);
6381         u16 mmodelen = le16_to_cpu(txh->MModeLen);
6382         u16 mmodefbrlen = le16_to_cpu(txh->MModeFbrLen);
6383         u16 tfid = le16_to_cpu(txh->TxFrameID);
6384         u16 txs = le16_to_cpu(txh->TxStatus);
6385         u16 mnmpdu = le16_to_cpu(txh->MaxNMpdus);
6386         u16 mabyte = le16_to_cpu(txh->MaxABytes_MRT);
6387         u16 mabyte_f = le16_to_cpu(txh->MaxABytes_FBR);
6388         u16 mmbyte = le16_to_cpu(txh->MinMBytes);
6389
6390         u8 *rtsph = txh->RTSPhyHeader;
6391         struct ieee80211_rts rts = txh->rts_frame;
6392         char hexbuf[256];
6393
6394         /* add plcp header along with txh descriptor */
6395         printk(KERN_DEBUG "Raw TxDesc + plcp header:\n");
6396         print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
6397                              txh, sizeof(struct d11txh) + 48);
6398
6399         printk(KERN_DEBUG "TxCtlLow: %04x ", mtcl);
6400         printk(KERN_DEBUG "TxCtlHigh: %04x ", mtch);
6401         printk(KERN_DEBUG "FC: %04x ", mfc);
6402         printk(KERN_DEBUG "FES Time: %04x\n", tfest);
6403         printk(KERN_DEBUG "PhyCtl: %04x%s ", ptcw,
6404                (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
6405         printk(KERN_DEBUG "PhyCtl_1: %04x ", ptcw_1);
6406         printk(KERN_DEBUG "PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
6407         printk(KERN_DEBUG "PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
6408         printk(KERN_DEBUG "PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
6409         printk(KERN_DEBUG "MainRates: %04x ", mainrates);
6410         printk(KERN_DEBUG "XtraFrameTypes: %04x ", xtraft);
6411         printk(KERN_DEBUG "\n");
6412
6413         brcmu_format_hex(hexbuf, iv, sizeof(txh->IV));
6414         printk(KERN_DEBUG "SecIV:       %s\n", hexbuf);
6415         brcmu_format_hex(hexbuf, ra, sizeof(txh->TxFrameRA));
6416         printk(KERN_DEBUG "RA:          %s\n", hexbuf);
6417
6418         printk(KERN_DEBUG "Fb FES Time: %04x ", tfestfb);
6419         brcmu_format_hex(hexbuf, rtspfb, sizeof(txh->RTSPLCPFallback));
6420         printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
6421         printk(KERN_DEBUG "RTS DUR: %04x ", rtsdfb);
6422         brcmu_format_hex(hexbuf, fragpfb, sizeof(txh->FragPLCPFallback));
6423         printk(KERN_DEBUG "PLCP: %s ", hexbuf);
6424         printk(KERN_DEBUG "DUR: %04x", fragdfb);
6425         printk(KERN_DEBUG "\n");
6426
6427         printk(KERN_DEBUG "MModeLen: %04x ", mmodelen);
6428         printk(KERN_DEBUG "MModeFbrLen: %04x\n", mmodefbrlen);
6429
6430         printk(KERN_DEBUG "FrameID:     %04x\n", tfid);
6431         printk(KERN_DEBUG "TxStatus:    %04x\n", txs);
6432
6433         printk(KERN_DEBUG "MaxNumMpdu:  %04x\n", mnmpdu);
6434         printk(KERN_DEBUG "MaxAggbyte:  %04x\n", mabyte);
6435         printk(KERN_DEBUG "MaxAggbyte_fb:  %04x\n", mabyte_f);
6436         printk(KERN_DEBUG "MinByte:     %04x\n", mmbyte);
6437
6438         brcmu_format_hex(hexbuf, rtsph, sizeof(txh->RTSPhyHeader));
6439         printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
6440         brcmu_format_hex(hexbuf, (u8 *) &rts, sizeof(txh->rts_frame));
6441         printk(KERN_DEBUG "RTS Frame: %s", hexbuf);
6442         printk(KERN_DEBUG "\n");
6443 }
6444 #endif                          /* defined(BCMDBG) */
6445
6446 #if defined(BCMDBG)
6447 void brcms_c_print_rxh(struct d11rxhdr *rxh)
6448 {
6449         u16 len = rxh->RxFrameSize;
6450         u16 phystatus_0 = rxh->PhyRxStatus_0;
6451         u16 phystatus_1 = rxh->PhyRxStatus_1;
6452         u16 phystatus_2 = rxh->PhyRxStatus_2;
6453         u16 phystatus_3 = rxh->PhyRxStatus_3;
6454         u16 macstatus1 = rxh->RxStatus1;
6455         u16 macstatus2 = rxh->RxStatus2;
6456         char flagstr[64];
6457         char lenbuf[20];
6458         static const struct brcmu_bit_desc macstat_flags[] = {
6459                 {RXS_FCSERR, "FCSErr"},
6460                 {RXS_RESPFRAMETX, "Reply"},
6461                 {RXS_PBPRES, "PADDING"},
6462                 {RXS_DECATMPT, "DeCr"},
6463                 {RXS_DECERR, "DeCrErr"},
6464                 {RXS_BCNSENT, "Bcn"},
6465                 {0, NULL}
6466         };
6467
6468         printk(KERN_DEBUG "Raw RxDesc:\n");
6469         print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, rxh,
6470                              sizeof(struct d11rxhdr));
6471
6472         brcmu_format_flags(macstat_flags, macstatus1, flagstr, 64);
6473
6474         snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
6475
6476         printk(KERN_DEBUG "RxFrameSize:     %6s (%d)%s\n", lenbuf, len,
6477                (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
6478         printk(KERN_DEBUG "RxPHYStatus:     %04x %04x %04x %04x\n",
6479                phystatus_0, phystatus_1, phystatus_2, phystatus_3);
6480         printk(KERN_DEBUG "RxMACStatus:     %x %s\n", macstatus1, flagstr);
6481         printk(KERN_DEBUG "RXMACaggtype:    %x\n",
6482                (macstatus2 & RXS_AGGTYPE_MASK));
6483         printk(KERN_DEBUG "RxTSFTime:       %04x\n", rxh->RxTSFTime);
6484 }
6485 #endif                          /* defined(BCMDBG) */
6486
6487 u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
6488 {
6489         u16 table_ptr;
6490         u8 phy_rate, index;
6491
6492         /* get the phy specific rate encoding for the PLCP SIGNAL field */
6493         if (is_ofdm_rate(rate))
6494                 table_ptr = M_RT_DIRMAP_A;
6495         else
6496                 table_ptr = M_RT_DIRMAP_B;
6497
6498         /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
6499          * the index into the rate table.
6500          */
6501         phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
6502         index = phy_rate & 0xf;
6503
6504         /* Find the SHM pointer to the rate table entry by looking in the
6505          * Direct-map Table
6506          */
6507         return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
6508 }
6509
6510 /* Callback for device removed */
6511
6512 /*
6513  * Attempts to queue a packet onto a multiple-precedence queue,
6514  * if necessary evicting a lower precedence packet from the queue.
6515  *
6516  * 'prec' is the precedence number that has already been mapped
6517  * from the packet priority.
6518  *
6519  * Returns true if packet consumed (queued), false if not.
6520  */
6521 static bool brcms_c_prec_enq(struct brcms_c_info *wlc, struct pktq *q,
6522                       struct sk_buff *pkt, int prec)
6523 {
6524         return brcms_c_prec_enq_head(wlc, q, pkt, prec, false);
6525 }
6526
6527 bool
6528 brcms_c_prec_enq_head(struct brcms_c_info *wlc, struct pktq *q,
6529                       struct sk_buff *pkt, int prec, bool head)
6530 {
6531         struct sk_buff *p;
6532         int eprec = -1;         /* precedence to evict from */
6533
6534         /* Determine precedence from which to evict packet, if any */
6535         if (pktq_pfull(q, prec))
6536                 eprec = prec;
6537         else if (pktq_full(q)) {
6538                 p = brcmu_pktq_peek_tail(q, &eprec);
6539                 if (eprec > prec) {
6540                         wiphy_err(wlc->wiphy, "%s: Failing: eprec %d > prec %d"
6541                                   "\n", __func__, eprec, prec);
6542                         return false;
6543                 }
6544         }
6545
6546         /* Evict if needed */
6547         if (eprec >= 0) {
6548                 bool discard_oldest;
6549
6550                 discard_oldest = ac_bitmap_tst(0, eprec);
6551
6552                 /* Refuse newer packet unless configured to discard oldest */
6553                 if (eprec == prec && !discard_oldest) {
6554                         wiphy_err(wlc->wiphy, "%s: No where to go, prec == %d"
6555                                   "\n", __func__, prec);
6556                         return false;
6557                 }
6558
6559                 /* Evict packet according to discard policy */
6560                 p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
6561                         brcmu_pktq_pdeq_tail(q, eprec);
6562                 brcmu_pkt_buf_free_skb(p);
6563         }
6564
6565         /* Enqueue */
6566         if (head)
6567                 p = brcmu_pktq_penq_head(q, prec, pkt);
6568         else
6569                 p = brcmu_pktq_penq(q, prec, pkt);
6570
6571         return true;
6572 }
6573
6574 void brcms_c_txq_enq(struct brcms_c_info *wlc, struct scb *scb,
6575                      struct sk_buff *sdu, uint prec)
6576 {
6577         struct brcms_txq_info *qi = wlc->pkt_queue;     /* Check me */
6578         struct pktq *q = &qi->q;
6579         int prio;
6580
6581         prio = sdu->priority;
6582
6583         if (!brcms_c_prec_enq(wlc, q, sdu, prec)) {
6584                 /*
6585                  * we might hit this condtion in case
6586                  * packet flooding from mac80211 stack
6587                  */
6588                 brcmu_pkt_buf_free_skb(sdu);
6589         }
6590 }
6591
6592 /*
6593  * bcmc_fid_generate:
6594  * Generate frame ID for a BCMC packet.  The frag field is not used
6595  * for MC frames so is used as part of the sequence number.
6596  */
6597 static inline u16
6598 bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
6599                   struct d11txh *txh)
6600 {
6601         u16 frameid;
6602
6603         frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
6604                                                   TXFID_QUEUE_MASK);
6605         frameid |=
6606             (((wlc->
6607                mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6608             TX_BCMC_FIFO;
6609
6610         return frameid;
6611 }
6612
6613 static uint
6614 brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
6615                       u8 preamble_type)
6616 {
6617         uint dur = 0;
6618
6619         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d\n",
6620                 wlc->pub->unit, rspec, preamble_type);
6621         /*
6622          * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
6623          * is less than or equal to the rate of the immediately previous
6624          * frame in the FES
6625          */
6626         rspec = brcms_basic_rate(wlc, rspec);
6627         /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
6628         dur =
6629             brcms_c_calc_frame_time(wlc, rspec, preamble_type,
6630                                 (DOT11_ACK_LEN + FCS_LEN));
6631         return dur;
6632 }
6633
6634 static uint
6635 brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
6636                       u8 preamble_type)
6637 {
6638         BCMMSG(wlc->wiphy, "wl%d: ratespec 0x%x, preamble_type %d\n",
6639                 wlc->pub->unit, rspec, preamble_type);
6640         return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
6641 }
6642
6643 static uint
6644 brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
6645                      u8 preamble_type)
6646 {
6647         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, "
6648                  "preamble_type %d\n", wlc->pub->unit, rspec, preamble_type);
6649         /*
6650          * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
6651          * is less than or equal to the rate of the immediately previous
6652          * frame in the FES
6653          */
6654         rspec = brcms_basic_rate(wlc, rspec);
6655         /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
6656         return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
6657                                    (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
6658                                     FCS_LEN));
6659 }
6660
6661 /* brcms_c_compute_frame_dur()
6662  *
6663  * Calculate the 802.11 MAC header DUR field for MPDU
6664  * DUR for a single frame = 1 SIFS + 1 ACK
6665  * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
6666  *
6667  * rate                 MPDU rate in unit of 500kbps
6668  * next_frag_len        next MPDU length in bytes
6669  * preamble_type        use short/GF or long/MM PLCP header
6670  */
6671 static u16
6672 brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
6673                       u8 preamble_type, uint next_frag_len)
6674 {
6675         u16 dur, sifs;
6676
6677         sifs = get_sifs(wlc->band);
6678
6679         dur = sifs;
6680         dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
6681
6682         if (next_frag_len) {
6683                 /* Double the current DUR to get 2 SIFS + 2 ACKs */
6684                 dur *= 2;
6685                 /* add another SIFS and the frag time */
6686                 dur += sifs;
6687                 dur +=
6688                     (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
6689                                                  next_frag_len);
6690         }
6691         return dur;
6692 }
6693
6694 /* The opposite of brcms_c_calc_frame_time */
6695 static uint
6696 brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
6697                    u8 preamble_type, uint dur)
6698 {
6699         uint nsyms, mac_len, Ndps, kNdps;
6700         uint rate = rspec2rate(ratespec);
6701
6702         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, dur %d\n",
6703                  wlc->pub->unit, ratespec, preamble_type, dur);
6704
6705         if (is_mcs_rate(ratespec)) {
6706                 uint mcs = ratespec & RSPEC_RATE_MASK;
6707                 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
6708                 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
6709                 /* payload calculation matches that of regular ofdm */
6710                 if (wlc->band->bandtype == BRCM_BAND_2G)
6711                         dur -= DOT11_OFDM_SIGNAL_EXTENSION;
6712                 /* kNdbps = kbps * 4 */
6713                 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
6714                                    rspec_issgi(ratespec)) * 4;
6715                 nsyms = dur / APHY_SYMBOL_TIME;
6716                 mac_len =
6717                     ((nsyms * kNdps) -
6718                      ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
6719         } else if (is_ofdm_rate(ratespec)) {
6720                 dur -= APHY_PREAMBLE_TIME;
6721                 dur -= APHY_SIGNAL_TIME;
6722                 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
6723                 Ndps = rate * 2;
6724                 nsyms = dur / APHY_SYMBOL_TIME;
6725                 mac_len =
6726                     ((nsyms * Ndps) -
6727                      (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
6728         } else {
6729                 if (preamble_type & BRCMS_SHORT_PREAMBLE)
6730                         dur -= BPHY_PLCP_SHORT_TIME;
6731                 else
6732                         dur -= BPHY_PLCP_TIME;
6733                 mac_len = dur * rate;
6734                 /* divide out factor of 2 in rate (1/2 mbps) */
6735                 mac_len = mac_len / 8 / 2;
6736         }
6737         return mac_len;
6738 }
6739
6740 static u32
6741 mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
6742                        u32 int_val)
6743 {
6744         u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
6745         u8 rate = int_val & NRATE_RATE_MASK;
6746         u32 rspec;
6747         bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
6748         bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
6749         bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
6750                                   == NRATE_OVERRIDE_MCS_ONLY);
6751         int bcmerror = 0;
6752
6753         if (!ismcs)
6754                 return (u32) rate;
6755
6756         /* validate the combination of rate/mcs/stf is allowed */
6757         if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
6758                 /* mcs only allowed when nmode */
6759                 if (stf > PHY_TXC1_MODE_SDM) {
6760                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid stf\n",
6761                                   wlc->pub->unit, __func__);
6762                         bcmerror = -EINVAL;
6763                         goto done;
6764                 }
6765
6766                 /* mcs 32 is a special case, DUP mode 40 only */
6767                 if (rate == 32) {
6768                         if (!CHSPEC_IS40(wlc->home_chanspec) ||
6769                             ((stf != PHY_TXC1_MODE_SISO)
6770                              && (stf != PHY_TXC1_MODE_CDD))) {
6771                                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid mcs "
6772                                           "32\n", wlc->pub->unit, __func__);
6773                                 bcmerror = -EINVAL;
6774                                 goto done;
6775                         }
6776                         /* mcs > 7 must use stf SDM */
6777                 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
6778                         /* mcs > 7 must use stf SDM */
6779                         if (stf != PHY_TXC1_MODE_SDM) {
6780                                 BCMMSG(wlc->wiphy, "wl%d: enabling "
6781                                        "SDM mode for mcs %d\n",
6782                                        wlc->pub->unit, rate);
6783                                 stf = PHY_TXC1_MODE_SDM;
6784                         }
6785                 } else {
6786                         /*
6787                          * MCS 0-7 may use SISO, CDD, and for
6788                          * phy_rev >= 3 STBC
6789                          */
6790                         if ((stf > PHY_TXC1_MODE_STBC) ||
6791                             (!BRCMS_STBC_CAP_PHY(wlc)
6792                              && (stf == PHY_TXC1_MODE_STBC))) {
6793                                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid STBC"
6794                                           "\n", wlc->pub->unit, __func__);
6795                                 bcmerror = -EINVAL;
6796                                 goto done;
6797                         }
6798                 }
6799         } else if (is_ofdm_rate(rate)) {
6800                 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
6801                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid OFDM\n",
6802                                   wlc->pub->unit, __func__);
6803                         bcmerror = -EINVAL;
6804                         goto done;
6805                 }
6806         } else if (is_cck_rate(rate)) {
6807                 if ((cur_band->bandtype != BRCM_BAND_2G)
6808                     || (stf != PHY_TXC1_MODE_SISO)) {
6809                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid CCK\n",
6810                                   wlc->pub->unit, __func__);
6811                         bcmerror = -EINVAL;
6812                         goto done;
6813                 }
6814         } else {
6815                 wiphy_err(wlc->wiphy, "wl%d: %s: Unknown rate type\n",
6816                           wlc->pub->unit, __func__);
6817                 bcmerror = -EINVAL;
6818                 goto done;
6819         }
6820         /* make sure multiple antennae are available for non-siso rates */
6821         if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
6822                 wiphy_err(wlc->wiphy, "wl%d: %s: SISO antenna but !SISO "
6823                           "request\n", wlc->pub->unit, __func__);
6824                 bcmerror = -EINVAL;
6825                 goto done;
6826         }
6827
6828         rspec = rate;
6829         if (ismcs) {
6830                 rspec |= RSPEC_MIMORATE;
6831                 /* For STBC populate the STC field of the ratespec */
6832                 if (stf == PHY_TXC1_MODE_STBC) {
6833                         u8 stc;
6834                         stc = 1;        /* Nss for single stream is always 1 */
6835                         rspec |= (stc << RSPEC_STC_SHIFT);
6836                 }
6837         }
6838
6839         rspec |= (stf << RSPEC_STF_SHIFT);
6840
6841         if (override_mcs_only)
6842                 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
6843
6844         if (issgi)
6845                 rspec |= RSPEC_SHORT_GI;
6846
6847         if ((rate != 0)
6848             && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
6849                 return rate;
6850
6851         return rspec;
6852 done:
6853         return rate;
6854 }
6855
6856 /*
6857  * Add struct d11txh, struct cck_phy_hdr.
6858  *
6859  * 'p' data must start with 802.11 MAC header
6860  * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
6861  *
6862  * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
6863  *
6864  */
6865 static u16
6866 brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
6867                      struct sk_buff *p, struct scb *scb, uint frag,
6868                      uint nfrags, uint queue, uint next_frag_len)
6869 {
6870         struct ieee80211_hdr *h;
6871         struct d11txh *txh;
6872         u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
6873         int len, phylen, rts_phylen;
6874         u16 mch, phyctl, xfts, mainrates;
6875         u16 seq = 0, mcl = 0, status = 0, frameid = 0;
6876         u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6877         u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6878         bool use_rts = false;
6879         bool use_cts = false;
6880         bool use_rifs = false;
6881         bool short_preamble[2] = { false, false };
6882         u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6883         u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6884         u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
6885         struct ieee80211_rts *rts = NULL;
6886         bool qos;
6887         uint ac;
6888         bool hwtkmic = false;
6889         u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
6890 #define ANTCFG_NONE 0xFF
6891         u8 antcfg = ANTCFG_NONE;
6892         u8 fbantcfg = ANTCFG_NONE;
6893         uint phyctl1_stf = 0;
6894         u16 durid = 0;
6895         struct ieee80211_tx_rate *txrate[2];
6896         int k;
6897         struct ieee80211_tx_info *tx_info;
6898         bool is_mcs;
6899         u16 mimo_txbw;
6900         u8 mimo_preamble_type;
6901
6902         /* locate 802.11 MAC header */
6903         h = (struct ieee80211_hdr *)(p->data);
6904         qos = ieee80211_is_data_qos(h->frame_control);
6905
6906         /* compute length of frame in bytes for use in PLCP computations */
6907         len = brcmu_pkttotlen(p);
6908         phylen = len + FCS_LEN;
6909
6910         /* Get tx_info */
6911         tx_info = IEEE80211_SKB_CB(p);
6912
6913         /* add PLCP */
6914         plcp = skb_push(p, D11_PHY_HDR_LEN);
6915
6916         /* add Broadcom tx descriptor header */
6917         txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
6918         memset(txh, 0, D11_TXH_LEN);
6919
6920         /* setup frameid */
6921         if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
6922                 /* non-AP STA should never use BCMC queue */
6923                 if (queue == TX_BCMC_FIFO) {
6924                         wiphy_err(wlc->wiphy, "wl%d: %s: ASSERT queue == "
6925                                   "TX_BCMC!\n", wlc->pub->unit, __func__);
6926                         frameid = bcmc_fid_generate(wlc, NULL, txh);
6927                 } else {
6928                         /* Increment the counter for first fragment */
6929                         if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
6930                                 scb->seqnum[p->priority]++;
6931
6932                         /* extract fragment number from frame first */
6933                         seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
6934                         seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
6935                         h->seq_ctrl = cpu_to_le16(seq);
6936
6937                         frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6938                             (queue & TXFID_QUEUE_MASK);
6939                 }
6940         }
6941         frameid |= queue & TXFID_QUEUE_MASK;
6942
6943         /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
6944         if (ieee80211_is_beacon(h->frame_control))
6945                 mcl |= TXC_IGNOREPMQ;
6946
6947         txrate[0] = tx_info->control.rates;
6948         txrate[1] = txrate[0] + 1;
6949
6950         /*
6951          * if rate control algorithm didn't give us a fallback
6952          * rate, use the primary rate
6953          */
6954         if (txrate[1]->idx < 0)
6955                 txrate[1] = txrate[0];
6956
6957         for (k = 0; k < hw->max_rates; k++) {
6958                 is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
6959                 if (!is_mcs) {
6960                         if ((txrate[k]->idx >= 0)
6961                             && (txrate[k]->idx <
6962                                 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6963                                 rspec[k] =
6964                                     hw->wiphy->bands[tx_info->band]->
6965                                     bitrates[txrate[k]->idx].hw_value;
6966                                 short_preamble[k] =
6967                                     txrate[k]->
6968                                     flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
6969                                     true : false;
6970                         } else {
6971                                 rspec[k] = BRCM_RATE_1M;
6972                         }
6973                 } else {
6974                         rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
6975                                         NRATE_MCS_INUSE | txrate[k]->idx);
6976                 }
6977
6978                 /*
6979                  * Currently only support same setting for primay and
6980                  * fallback rates. Unify flags for each rate into a
6981                  * single value for the frame
6982                  */
6983                 use_rts |=
6984                     txrate[k]->
6985                     flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
6986                 use_cts |=
6987                     txrate[k]->
6988                     flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
6989
6990
6991                 /*
6992                  * (1) RATE:
6993                  *   determine and validate primary rate
6994                  *   and fallback rates
6995                  */
6996                 if (!rspec_active(rspec[k])) {
6997                         rspec[k] = BRCM_RATE_1M;
6998                 } else {
6999                         if (!is_multicast_ether_addr(h->addr1)) {
7000                                 /* set tx antenna config */
7001                                 brcms_c_antsel_antcfg_get(wlc->asi, false,
7002                                         false, 0, 0, &antcfg, &fbantcfg);
7003                         }
7004                 }
7005         }
7006
7007         phyctl1_stf = wlc->stf->ss_opmode;
7008
7009         if (wlc->pub->_n_enab & SUPPORT_11N) {
7010                 for (k = 0; k < hw->max_rates; k++) {
7011                         /*
7012                          * apply siso/cdd to single stream mcs's or ofdm
7013                          * if rspec is auto selected
7014                          */
7015                         if (((is_mcs_rate(rspec[k]) &&
7016                               is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
7017                              is_ofdm_rate(rspec[k]))
7018                             && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
7019                                 || !(rspec[k] & RSPEC_OVERRIDE))) {
7020                                 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
7021
7022                                 /* For SISO MCS use STBC if possible */
7023                                 if (is_mcs_rate(rspec[k])
7024                                     && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
7025                                         u8 stc;
7026
7027                                         /* Nss for single stream is always 1 */
7028                                         stc = 1;
7029                                         rspec[k] |= (PHY_TXC1_MODE_STBC <<
7030                                                         RSPEC_STF_SHIFT) |
7031                                                     (stc << RSPEC_STC_SHIFT);
7032                                 } else
7033                                         rspec[k] |=
7034                                             (phyctl1_stf << RSPEC_STF_SHIFT);
7035                         }
7036
7037                         /*
7038                          * Is the phy configured to use 40MHZ frames? If
7039                          * so then pick the desired txbw
7040                          */
7041                         if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
7042                                 /* default txbw is 20in40 SB */
7043                                 mimo_ctlchbw = mimo_txbw =
7044                                    CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
7045                                                                  wlc->band->pi))
7046                                    ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
7047
7048                                 if (is_mcs_rate(rspec[k])) {
7049                                         /* mcs 32 must be 40b/w DUP */
7050                                         if ((rspec[k] & RSPEC_RATE_MASK)
7051                                             == 32) {
7052                                                 mimo_txbw =
7053                                                     PHY_TXC1_BW_40MHZ_DUP;
7054                                                 /* use override */
7055                                         } else if (wlc->mimo_40txbw != AUTO)
7056                                                 mimo_txbw = wlc->mimo_40txbw;
7057                                         /* else check if dst is using 40 Mhz */
7058                                         else if (scb->flags & SCB_IS40)
7059                                                 mimo_txbw = PHY_TXC1_BW_40MHZ;
7060                                 } else if (is_ofdm_rate(rspec[k])) {
7061                                         if (wlc->ofdm_40txbw != AUTO)
7062                                                 mimo_txbw = wlc->ofdm_40txbw;
7063                                 } else if (wlc->cck_40txbw != AUTO) {
7064                                         mimo_txbw = wlc->cck_40txbw;
7065                                 }
7066                         } else {
7067                                 /*
7068                                  * mcs32 is 40 b/w only.
7069                                  * This is possible for probe packets on
7070                                  * a STA during SCAN
7071                                  */
7072                                 if ((rspec[k] & RSPEC_RATE_MASK) == 32)
7073                                         /* mcs 0 */
7074                                         rspec[k] = RSPEC_MIMORATE;
7075
7076                                 mimo_txbw = PHY_TXC1_BW_20MHZ;
7077                         }
7078
7079                         /* Set channel width */
7080                         rspec[k] &= ~RSPEC_BW_MASK;
7081                         if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
7082                                 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
7083                         else
7084                                 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
7085
7086                         /* Disable short GI, not supported yet */
7087                         rspec[k] &= ~RSPEC_SHORT_GI;
7088
7089                         mimo_preamble_type = BRCMS_MM_PREAMBLE;
7090                         if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
7091                                 mimo_preamble_type = BRCMS_GF_PREAMBLE;
7092
7093                         if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
7094                             && (!is_mcs_rate(rspec[k]))) {
7095                                 wiphy_err(wlc->wiphy, "wl%d: %s: IEEE80211_TX_"
7096                                           "RC_MCS != is_mcs_rate(rspec)\n",
7097                                           wlc->pub->unit, __func__);
7098                         }
7099
7100                         if (is_mcs_rate(rspec[k])) {
7101                                 preamble_type[k] = mimo_preamble_type;
7102
7103                                 /*
7104                                  * if SGI is selected, then forced mm
7105                                  * for single stream
7106                                  */
7107                                 if ((rspec[k] & RSPEC_SHORT_GI)
7108                                     && is_single_stream(rspec[k] &
7109                                                         RSPEC_RATE_MASK))
7110                                         preamble_type[k] = BRCMS_MM_PREAMBLE;
7111                         }
7112
7113                         /* should be better conditionalized */
7114                         if (!is_mcs_rate(rspec[0])
7115                             && (tx_info->control.rates[0].
7116                                 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
7117                                 preamble_type[k] = BRCMS_SHORT_PREAMBLE;
7118                 }
7119         } else {
7120                 for (k = 0; k < hw->max_rates; k++) {
7121                         /* Set ctrlchbw as 20Mhz */
7122                         rspec[k] &= ~RSPEC_BW_MASK;
7123                         rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
7124
7125                         /* for nphy, stf of ofdm frames must follow policies */
7126                         if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
7127                                 rspec[k] &= ~RSPEC_STF_MASK;
7128                                 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
7129                         }
7130                 }
7131         }
7132
7133         /* Reset these for use with AMPDU's */
7134         txrate[0]->count = 0;
7135         txrate[1]->count = 0;
7136
7137         /* (2) PROTECTION, may change rspec */
7138         if ((ieee80211_is_data(h->frame_control) ||
7139             ieee80211_is_mgmt(h->frame_control)) &&
7140             (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
7141                 use_rts = true;
7142
7143         /* (3) PLCP: determine PLCP header and MAC duration,
7144          * fill struct d11txh */
7145         brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
7146         brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
7147         memcpy(&txh->FragPLCPFallback,
7148                plcp_fallback, sizeof(txh->FragPLCPFallback));
7149
7150         /* Length field now put in CCK FBR CRC field */
7151         if (is_cck_rate(rspec[1])) {
7152                 txh->FragPLCPFallback[4] = phylen & 0xff;
7153                 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
7154         }
7155
7156         /* MIMO-RATE: need validation ?? */
7157         mainrates = is_ofdm_rate(rspec[0]) ?
7158                         D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
7159                         plcp[0];
7160
7161         /* DUR field for main rate */
7162         if (!ieee80211_is_pspoll(h->frame_control) &&
7163             !is_multicast_ether_addr(h->addr1) && !use_rifs) {
7164                 durid =
7165                     brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
7166                                           next_frag_len);
7167                 h->duration_id = cpu_to_le16(durid);
7168         } else if (use_rifs) {
7169                 /* NAV protect to end of next max packet size */
7170                 durid =
7171                     (u16) brcms_c_calc_frame_time(wlc, rspec[0],
7172                                                  preamble_type[0],
7173                                                  DOT11_MAX_FRAG_LEN);
7174                 durid += RIFS_11N_TIME;
7175                 h->duration_id = cpu_to_le16(durid);
7176         }
7177
7178         /* DUR field for fallback rate */
7179         if (ieee80211_is_pspoll(h->frame_control))
7180                 txh->FragDurFallback = h->duration_id;
7181         else if (is_multicast_ether_addr(h->addr1) || use_rifs)
7182                 txh->FragDurFallback = 0;
7183         else {
7184                 durid = brcms_c_compute_frame_dur(wlc, rspec[1],
7185                                               preamble_type[1], next_frag_len);
7186                 txh->FragDurFallback = cpu_to_le16(durid);
7187         }
7188
7189         /* (4) MAC-HDR: MacTxControlLow */
7190         if (frag == 0)
7191                 mcl |= TXC_STARTMSDU;
7192
7193         if (!is_multicast_ether_addr(h->addr1))
7194                 mcl |= TXC_IMMEDACK;
7195
7196         if (wlc->band->bandtype == BRCM_BAND_5G)
7197                 mcl |= TXC_FREQBAND_5G;
7198
7199         if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
7200                 mcl |= TXC_BW_40;
7201
7202         /* set AMIC bit if using hardware TKIP MIC */
7203         if (hwtkmic)
7204                 mcl |= TXC_AMIC;
7205
7206         txh->MacTxControlLow = cpu_to_le16(mcl);
7207
7208         /* MacTxControlHigh */
7209         mch = 0;
7210
7211         /* Set fallback rate preamble type */
7212         if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
7213             (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
7214                 if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
7215                         mch |= TXC_PREAMBLE_DATA_FB_SHORT;
7216         }
7217
7218         /* MacFrameControl */
7219         memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
7220         txh->TxFesTimeNormal = cpu_to_le16(0);
7221
7222         txh->TxFesTimeFallback = cpu_to_le16(0);
7223
7224         /* TxFrameRA */
7225         memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
7226
7227         /* TxFrameID */
7228         txh->TxFrameID = cpu_to_le16(frameid);
7229
7230         /*
7231          * TxStatus, Note the case of recreating the first frag of a suppressed
7232          * frame then we may need to reset the retry cnt's via the status reg
7233          */
7234         txh->TxStatus = cpu_to_le16(status);
7235
7236         /*
7237          * extra fields for ucode AMPDU aggregation, the new fields are added to
7238          * the END of previous structure so that it's compatible in driver.
7239          */
7240         txh->MaxNMpdus = cpu_to_le16(0);
7241         txh->MaxABytes_MRT = cpu_to_le16(0);
7242         txh->MaxABytes_FBR = cpu_to_le16(0);
7243         txh->MinMBytes = cpu_to_le16(0);
7244
7245         /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
7246          * furnish struct d11txh */
7247         /* RTS PLCP header and RTS frame */
7248         if (use_rts || use_cts) {
7249                 if (use_rts && use_cts)
7250                         use_cts = false;
7251
7252                 for (k = 0; k < 2; k++) {
7253                         rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
7254                                                               false,
7255                                                               mimo_ctlchbw);
7256                 }
7257
7258                 if (!is_ofdm_rate(rts_rspec[0]) &&
7259                     !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
7260                       (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
7261                         rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
7262                         mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
7263                 }
7264
7265                 if (!is_ofdm_rate(rts_rspec[1]) &&
7266                     !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
7267                       (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
7268                         rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
7269                         mch |= TXC_PREAMBLE_RTS_FB_SHORT;
7270                 }
7271
7272                 /* RTS/CTS additions to MacTxControlLow */
7273                 if (use_cts) {
7274                         txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
7275                 } else {
7276                         txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
7277                         txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
7278                 }
7279
7280                 /* RTS PLCP header */
7281                 rts_plcp = txh->RTSPhyHeader;
7282                 if (use_cts)
7283                         rts_phylen = DOT11_CTS_LEN + FCS_LEN;
7284                 else
7285                         rts_phylen = DOT11_RTS_LEN + FCS_LEN;
7286
7287                 brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
7288
7289                 /* fallback rate version of RTS PLCP header */
7290                 brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
7291                                  rts_plcp_fallback);
7292                 memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
7293                        sizeof(txh->RTSPLCPFallback));
7294
7295                 /* RTS frame fields... */
7296                 rts = (struct ieee80211_rts *)&txh->rts_frame;
7297
7298                 durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
7299                                                rspec[0], rts_preamble_type[0],
7300                                                preamble_type[0], phylen, false);
7301                 rts->duration = cpu_to_le16(durid);
7302                 /* fallback rate version of RTS DUR field */
7303                 durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
7304                                                rts_rspec[1], rspec[1],
7305                                                rts_preamble_type[1],
7306                                                preamble_type[1], phylen, false);
7307                 txh->RTSDurFallback = cpu_to_le16(durid);
7308
7309                 if (use_cts) {
7310                         rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
7311                                                          IEEE80211_STYPE_CTS);
7312
7313                         memcpy(&rts->ra, &h->addr2, ETH_ALEN);
7314                 } else {
7315                         rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
7316                                                          IEEE80211_STYPE_RTS);
7317
7318                         memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
7319                 }
7320
7321                 /* mainrate
7322                  *    low 8 bits: main frag rate/mcs,
7323                  *    high 8 bits: rts/cts rate/mcs
7324                  */
7325                 mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
7326                                 D11A_PHY_HDR_GRATE(
7327                                         (struct ofdm_phy_hdr *) rts_plcp) :
7328                                 rts_plcp[0]) << 8;
7329         } else {
7330                 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
7331                 memset((char *)&txh->rts_frame, 0,
7332                         sizeof(struct ieee80211_rts));
7333                 memset((char *)txh->RTSPLCPFallback, 0,
7334                       sizeof(txh->RTSPLCPFallback));
7335                 txh->RTSDurFallback = 0;
7336         }
7337
7338 #ifdef SUPPORT_40MHZ
7339         /* add null delimiter count */
7340         if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
7341                 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
7342                    brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
7343
7344 #endif
7345
7346         /*
7347          * Now that RTS/RTS FB preamble types are updated, write
7348          * the final value
7349          */
7350         txh->MacTxControlHigh = cpu_to_le16(mch);
7351
7352         /*
7353          * MainRates (both the rts and frag plcp rates have
7354          * been calculated now)
7355          */
7356         txh->MainRates = cpu_to_le16(mainrates);
7357
7358         /* XtraFrameTypes */
7359         xfts = frametype(rspec[1], wlc->mimoft);
7360         xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
7361         xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
7362         xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
7363                                                              XFTS_CHANNEL_SHIFT;
7364         txh->XtraFrameTypes = cpu_to_le16(xfts);
7365
7366         /* PhyTxControlWord */
7367         phyctl = frametype(rspec[0], wlc->mimoft);
7368         if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
7369             (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
7370                 if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
7371                         phyctl |= PHY_TXC_SHORT_HDR;
7372         }
7373
7374         /* phytxant is properly bit shifted */
7375         phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
7376         txh->PhyTxControlWord = cpu_to_le16(phyctl);
7377
7378         /* PhyTxControlWord_1 */
7379         if (BRCMS_PHY_11N_CAP(wlc->band)) {
7380                 u16 phyctl1 = 0;
7381
7382                 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
7383                 txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
7384                 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
7385                 txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
7386
7387                 if (use_rts || use_cts) {
7388                         phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
7389                         txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
7390                         phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
7391                         txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
7392                 }
7393
7394                 /*
7395                  * For mcs frames, if mixedmode(overloaded with long preamble)
7396                  * is going to be set, fill in non-zero MModeLen and/or
7397                  * MModeFbrLen it will be unnecessary if they are separated
7398                  */
7399                 if (is_mcs_rate(rspec[0]) &&
7400                     (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
7401                         u16 mmodelen =
7402                             brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
7403                         txh->MModeLen = cpu_to_le16(mmodelen);
7404                 }
7405
7406                 if (is_mcs_rate(rspec[1]) &&
7407                     (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
7408                         u16 mmodefbrlen =
7409                             brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
7410                         txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
7411                 }
7412         }
7413
7414         ac = skb_get_queue_mapping(p);
7415         if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
7416                 uint frag_dur, dur, dur_fallback;
7417
7418                 /* WME: Update TXOP threshold */
7419                 if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
7420                         frag_dur =
7421                             brcms_c_calc_frame_time(wlc, rspec[0],
7422                                         preamble_type[0], phylen);
7423
7424                         if (rts) {
7425                                 /* 1 RTS or CTS-to-self frame */
7426                                 dur =
7427                                     brcms_c_calc_cts_time(wlc, rts_rspec[0],
7428                                                       rts_preamble_type[0]);
7429                                 dur_fallback =
7430                                     brcms_c_calc_cts_time(wlc, rts_rspec[1],
7431                                                       rts_preamble_type[1]);
7432                                 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
7433                                 dur += le16_to_cpu(rts->duration);
7434                                 dur_fallback +=
7435                                         le16_to_cpu(txh->RTSDurFallback);
7436                         } else if (use_rifs) {
7437                                 dur = frag_dur;
7438                                 dur_fallback = 0;
7439                         } else {
7440                                 /* frame + SIFS + ACK */
7441                                 dur = frag_dur;
7442                                 dur +=
7443                                     brcms_c_compute_frame_dur(wlc, rspec[0],
7444                                                           preamble_type[0], 0);
7445
7446                                 dur_fallback =
7447                                     brcms_c_calc_frame_time(wlc, rspec[1],
7448                                                         preamble_type[1],
7449                                                         phylen);
7450                                 dur_fallback +=
7451                                     brcms_c_compute_frame_dur(wlc, rspec[1],
7452                                                           preamble_type[1], 0);
7453                         }
7454                         /* NEED to set TxFesTimeNormal (hard) */
7455                         txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
7456                         /*
7457                          * NEED to set fallback rate version of
7458                          * TxFesTimeNormal (hard)
7459                          */
7460                         txh->TxFesTimeFallback =
7461                                 cpu_to_le16((u16) dur_fallback);
7462
7463                         /*
7464                          * update txop byte threshold (txop minus intraframe
7465                          * overhead)
7466                          */
7467                         if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
7468                                 uint newfragthresh;
7469
7470                                 newfragthresh =
7471                                     brcms_c_calc_frame_len(wlc,
7472                                         rspec[0], preamble_type[0],
7473                                         (wlc->edcf_txop[ac] -
7474                                                 (dur - frag_dur)));
7475                                 /* range bound the fragthreshold */
7476                                 if (newfragthresh < DOT11_MIN_FRAG_LEN)
7477                                         newfragthresh =
7478                                             DOT11_MIN_FRAG_LEN;
7479                                 else if (newfragthresh >
7480                                          wlc->usr_fragthresh)
7481                                         newfragthresh =
7482                                             wlc->usr_fragthresh;
7483                                 /* update the fragthresh and do txc update */
7484                                 if (wlc->fragthresh[queue] !=
7485                                     (u16) newfragthresh)
7486                                         wlc->fragthresh[queue] =
7487                                             (u16) newfragthresh;
7488                         } else {
7489                                 wiphy_err(wlc->wiphy, "wl%d: %s txop invalid "
7490                                           "for rate %d\n",
7491                                           wlc->pub->unit, fifo_names[queue],
7492                                           rspec2rate(rspec[0]));
7493                         }
7494
7495                         if (dur > wlc->edcf_txop[ac])
7496                                 wiphy_err(wlc->wiphy, "wl%d: %s: %s txop "
7497                                           "exceeded phylen %d/%d dur %d/%d\n",
7498                                           wlc->pub->unit, __func__,
7499                                           fifo_names[queue],
7500                                           phylen, wlc->fragthresh[queue],
7501                                           dur, wlc->edcf_txop[ac]);
7502                 }
7503         }
7504
7505         return 0;
7506 }
7507
7508 void brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
7509                               struct ieee80211_hw *hw)
7510 {
7511         u8 prio;
7512         uint fifo;
7513         struct scb *scb = &wlc->pri_scb;
7514         struct ieee80211_hdr *d11_header = (struct ieee80211_hdr *)(sdu->data);
7515
7516         /*
7517          * 802.11 standard requires management traffic
7518          * to go at highest priority
7519          */
7520         prio = ieee80211_is_data(d11_header->frame_control) ? sdu->priority :
7521                 MAXPRIO;
7522         fifo = prio2fifo[prio];
7523         if (brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0))
7524                 return;
7525         brcms_c_txq_enq(wlc, scb, sdu, BRCMS_PRIO_TO_PREC(prio));
7526         brcms_c_send_q(wlc);
7527 }
7528
7529 void brcms_c_send_q(struct brcms_c_info *wlc)
7530 {
7531         struct sk_buff *pkt[DOT11_MAXNUMFRAGS];
7532         int prec;
7533         u16 prec_map;
7534         int err = 0, i, count;
7535         uint fifo;
7536         struct brcms_txq_info *qi = wlc->pkt_queue;
7537         struct pktq *q = &qi->q;
7538         struct ieee80211_tx_info *tx_info;
7539
7540         prec_map = wlc->tx_prec_map;
7541
7542         /* Send all the enq'd pkts that we can.
7543          * Dequeue packets with precedence with empty HW fifo only
7544          */
7545         while (prec_map && (pkt[0] = brcmu_pktq_mdeq(q, prec_map, &prec))) {
7546                 tx_info = IEEE80211_SKB_CB(pkt[0]);
7547                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
7548                         err = brcms_c_sendampdu(wlc->ampdu, qi, pkt, prec);
7549                 } else {
7550                         count = 1;
7551                         err = brcms_c_prep_pdu(wlc, pkt[0], &fifo);
7552                         if (!err) {
7553                                 for (i = 0; i < count; i++)
7554                                         brcms_c_txfifo(wlc, fifo, pkt[i], true,
7555                                                        1);
7556                         }
7557                 }
7558
7559                 if (err == -EBUSY) {
7560                         brcmu_pktq_penq_head(q, prec, pkt[0]);
7561                         /*
7562                          * If send failed due to any other reason than a
7563                          * change in HW FIFO condition, quit. Otherwise,
7564                          * read the new prec_map!
7565                          */
7566                         if (prec_map == wlc->tx_prec_map)
7567                                 break;
7568                         prec_map = wlc->tx_prec_map;
7569                 }
7570         }
7571 }
7572
7573 void
7574 brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p,
7575                bool commit, s8 txpktpend)
7576 {
7577         u16 frameid = INVALIDFID;
7578         struct d11txh *txh;
7579
7580         txh = (struct d11txh *) (p->data);
7581
7582         /* When a BC/MC frame is being committed to the BCMC fifo
7583          * via DMA (NOT PIO), update ucode or BSS info as appropriate.
7584          */
7585         if (fifo == TX_BCMC_FIFO)
7586                 frameid = le16_to_cpu(txh->TxFrameID);
7587
7588         /*
7589          * Bump up pending count for if not using rpc. If rpc is
7590          * used, this will be handled in brcms_b_txfifo()
7591          */
7592         if (commit) {
7593                 wlc->core->txpktpend[fifo] += txpktpend;
7594                 BCMMSG(wlc->wiphy, "pktpend inc %d to %d\n",
7595                          txpktpend, wlc->core->txpktpend[fifo]);
7596         }
7597
7598         /* Commit BCMC sequence number in the SHM frame ID location */
7599         if (frameid != INVALIDFID) {
7600                 /*
7601                  * To inform the ucode of the last mcast frame posted
7602                  * so that it can clear moredata bit
7603                  */
7604                 brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
7605         }
7606
7607         if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0)
7608                 wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
7609 }
7610
7611 /*
7612  * Compute PLCP, but only requires actual rate and length of pkt.
7613  * Rate is given in the driver standard multiple of 500 kbps.
7614  * le is set for 11 Mbps rate if necessary.
7615  * Broken out for PRQ.
7616  */
7617
7618 static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
7619                              uint length, u8 *plcp)
7620 {
7621         u16 usec = 0;
7622         u8 le = 0;
7623
7624         switch (rate_500) {
7625         case BRCM_RATE_1M:
7626                 usec = length << 3;
7627                 break;
7628         case BRCM_RATE_2M:
7629                 usec = length << 2;
7630                 break;
7631         case BRCM_RATE_5M5:
7632                 usec = (length << 4) / 11;
7633                 if ((length << 4) - (usec * 11) > 0)
7634                         usec++;
7635                 break;
7636         case BRCM_RATE_11M:
7637                 usec = (length << 3) / 11;
7638                 if ((length << 3) - (usec * 11) > 0) {
7639                         usec++;
7640                         if ((usec * 11) - (length << 3) >= 8)
7641                                 le = D11B_PLCP_SIGNAL_LE;
7642                 }
7643                 break;
7644
7645         default:
7646                 wiphy_err(wlc->wiphy,
7647                           "brcms_c_cck_plcp_set: unsupported rate %d\n",
7648                           rate_500);
7649                 rate_500 = BRCM_RATE_1M;
7650                 usec = length << 3;
7651                 break;
7652         }
7653         /* PLCP signal byte */
7654         plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
7655         /* PLCP service byte */
7656         plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
7657         /* PLCP length u16, little endian */
7658         plcp[2] = usec & 0xff;
7659         plcp[3] = (usec >> 8) & 0xff;
7660         /* PLCP CRC16 */
7661         plcp[4] = 0;
7662         plcp[5] = 0;
7663 }
7664
7665 /* Rate: 802.11 rate code, length: PSDU length in octets */
7666 static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
7667 {
7668         u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
7669         plcp[0] = mcs;
7670         if (rspec_is40mhz(rspec) || (mcs == 32))
7671                 plcp[0] |= MIMO_PLCP_40MHZ;
7672         BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
7673         plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
7674         plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
7675         plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
7676         plcp[5] = 0;
7677 }
7678
7679 /* Rate: 802.11 rate code, length: PSDU length in octets */
7680 static void
7681 brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
7682 {
7683         u8 rate_signal;
7684         u32 tmp = 0;
7685         int rate = rspec2rate(rspec);
7686
7687         /*
7688          * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
7689          * transmitted first
7690          */
7691         rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
7692         memset(plcp, 0, D11_PHY_HDR_LEN);
7693         D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
7694
7695         tmp = (length & 0xfff) << 5;
7696         plcp[2] |= (tmp >> 16) & 0xff;
7697         plcp[1] |= (tmp >> 8) & 0xff;
7698         plcp[0] |= tmp & 0xff;
7699 }
7700
7701 /* Rate: 802.11 rate code, length: PSDU length in octets */
7702 static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
7703                                  uint length, u8 *plcp)
7704 {
7705         int rate = rspec2rate(rspec);
7706
7707         brcms_c_cck_plcp_set(wlc, rate, length, plcp);
7708 }
7709
7710 void
7711 brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
7712                      uint length, u8 *plcp)
7713 {
7714         if (is_mcs_rate(rspec))
7715                 brcms_c_compute_mimo_plcp(rspec, length, plcp);
7716         else if (is_ofdm_rate(rspec))
7717                 brcms_c_compute_ofdm_plcp(rspec, length, plcp);
7718         else
7719                 brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
7720 }
7721
7722 /* brcms_c_compute_rtscts_dur()
7723  *
7724  * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
7725  * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
7726  * DUR for CTS-TO-SELF w/ frame    = 2 SIFS         + next frame time + 1 ACK
7727  *
7728  * cts                  cts-to-self or rts/cts
7729  * rts_rate             rts or cts rate in unit of 500kbps
7730  * rate                 next MPDU rate in unit of 500kbps
7731  * frame_len            next MPDU frame length in bytes
7732  */
7733 u16
7734 brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
7735                            u32 rts_rate,
7736                            u32 frame_rate, u8 rts_preamble_type,
7737                            u8 frame_preamble_type, uint frame_len, bool ba)
7738 {
7739         u16 dur, sifs;
7740
7741         sifs = get_sifs(wlc->band);
7742
7743         if (!cts_only) {
7744                 /* RTS/CTS */
7745                 dur = 3 * sifs;
7746                 dur +=
7747                     (u16) brcms_c_calc_cts_time(wlc, rts_rate,
7748                                                rts_preamble_type);
7749         } else {
7750                 /* CTS-TO-SELF */
7751                 dur = 2 * sifs;
7752         }
7753
7754         dur +=
7755             (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
7756                                          frame_len);
7757         if (ba)
7758                 dur +=
7759                     (u16) brcms_c_calc_ba_time(wlc, frame_rate,
7760                                               BRCMS_SHORT_PREAMBLE);
7761         else
7762                 dur +=
7763                     (u16) brcms_c_calc_ack_time(wlc, frame_rate,
7764                                                frame_preamble_type);
7765         return dur;
7766 }
7767
7768 u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
7769 {
7770         u16 phyctl1 = 0;
7771         u16 bw;
7772
7773         if (BRCMS_ISLCNPHY(wlc->band)) {
7774                 bw = PHY_TXC1_BW_20MHZ;
7775         } else {
7776                 bw = rspec_get_bw(rspec);
7777                 /* 10Mhz is not supported yet */
7778                 if (bw < PHY_TXC1_BW_20MHZ) {
7779                         wiphy_err(wlc->wiphy, "phytxctl1_calc: bw %d is "
7780                                   "not supported yet, set to 20L\n", bw);
7781                         bw = PHY_TXC1_BW_20MHZ;
7782                 }
7783         }
7784
7785         if (is_mcs_rate(rspec)) {
7786                 uint mcs = rspec & RSPEC_RATE_MASK;
7787
7788                 /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
7789                 phyctl1 = rspec_phytxbyte2(rspec);
7790                 /* set the upper byte of phyctl1 */
7791                 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
7792         } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
7793                    && !BRCMS_ISSSLPNPHY(wlc->band)) {
7794                 /*
7795                  * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
7796                  * Data Rate. Eventually MIMOPHY would also be converted to
7797                  * this format
7798                  */
7799                 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
7800                 phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
7801         } else {                /* legacy OFDM/CCK */
7802                 s16 phycfg;
7803                 /* get the phyctl byte from rate phycfg table */
7804                 phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
7805                 if (phycfg == -1) {
7806                         wiphy_err(wlc->wiphy, "phytxctl1_calc: wrong "
7807                                   "legacy OFDM/CCK rate\n");
7808                         phycfg = 0;
7809                 }
7810                 /* set the upper byte of phyctl1 */
7811                 phyctl1 =
7812                     (bw | (phycfg << 8) |
7813                      (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
7814         }
7815         return phyctl1;
7816 }
7817
7818 u32
7819 brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
7820                            bool use_rspec, u16 mimo_ctlchbw)
7821 {
7822         u32 rts_rspec = 0;
7823
7824         if (use_rspec)
7825                 /* use frame rate as rts rate */
7826                 rts_rspec = rspec;
7827         else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
7828                 /* Use 11Mbps as the g protection RTS target rate and fallback.
7829                  * Use the brcms_basic_rate() lookup to find the best basic rate
7830                  * under the target in case 11 Mbps is not Basic.
7831                  * 6 and 9 Mbps are not usually selected by rate selection, but
7832                  * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
7833                  * is more robust.
7834                  */
7835                 rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
7836         else
7837                 /* calculate RTS rate and fallback rate based on the frame rate
7838                  * RTS must be sent at a basic rate since it is a
7839                  * control frame, sec 9.6 of 802.11 spec
7840                  */
7841                 rts_rspec = brcms_basic_rate(wlc, rspec);
7842
7843         if (BRCMS_PHY_11N_CAP(wlc->band)) {
7844                 /* set rts txbw to correct side band */
7845                 rts_rspec &= ~RSPEC_BW_MASK;
7846
7847                 /*
7848                  * if rspec/rspec_fallback is 40MHz, then send RTS on both
7849                  * 20MHz channel (DUP), otherwise send RTS on control channel
7850                  */
7851                 if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
7852                         rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
7853                 else
7854                         rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
7855
7856                 /* pick siso/cdd as default for ofdm */
7857                 if (is_ofdm_rate(rts_rspec)) {
7858                         rts_rspec &= ~RSPEC_STF_MASK;
7859                         rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
7860                 }
7861         }
7862         return rts_rspec;
7863 }
7864
7865 void brcms_c_tbtt(struct brcms_c_info *wlc)
7866 {
7867         if (!wlc->bsscfg->BSS)
7868                 /*
7869                  * DirFrmQ is now valid...defer setting until end
7870                  * of ATIM window
7871                  */
7872                 wlc->qvalid |= MCMD_DIRFRMQVAL;
7873 }
7874
7875 void
7876 brcms_c_txfifo_complete(struct brcms_c_info *wlc, uint fifo, s8 txpktpend)
7877 {
7878         wlc->core->txpktpend[fifo] -= txpktpend;
7879         BCMMSG(wlc->wiphy, "pktpend dec %d to %d\n", txpktpend,
7880                wlc->core->txpktpend[fifo]);
7881
7882         /* There is more room; mark precedences related to this FIFO sendable */
7883         wlc->tx_prec_map |= wlc->fifo2prec_map[fifo];
7884
7885         /* figure out which bsscfg is being worked on... */
7886 }
7887
7888 /* Update beacon listen interval in shared memory */
7889 void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
7890 {
7891         /* wake up every DTIM is the default */
7892         if (wlc->bcn_li_dtim == 1)
7893                 brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
7894         else
7895                 brcms_b_write_shm(wlc->hw, M_BCN_LI,
7896                               (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
7897 }
7898
7899 static void
7900 brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
7901                   u32 *tsf_h_ptr)
7902 {
7903         struct d11regs __iomem *regs = wlc_hw->regs;
7904
7905         /* read the tsf timer low, then high to get an atomic read */
7906         *tsf_l_ptr = R_REG(&regs->tsf_timerlow);
7907         *tsf_h_ptr = R_REG(&regs->tsf_timerhigh);
7908 }
7909
7910 /*
7911  * recover 64bit TSF value from the 16bit TSF value in the rx header
7912  * given the assumption that the TSF passed in header is within 65ms
7913  * of the current tsf.
7914  *
7915  * 6       5       4       4       3       2       1
7916  * 3.......6.......8.......0.......2.......4.......6.......8......0
7917  * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
7918  *
7919  * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
7920  * tsf_l is filled in by brcms_b_recv, which is done earlier in the
7921  * receive call sequence after rx interrupt. Only the higher 16 bits
7922  * are used. Finally, the tsf_h is read from the tsf register.
7923  */
7924 static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
7925                                  struct d11rxhdr *rxh)
7926 {
7927         u32 tsf_h, tsf_l;
7928         u16 rx_tsf_0_15, rx_tsf_16_31;
7929
7930         brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
7931
7932         rx_tsf_16_31 = (u16)(tsf_l >> 16);
7933         rx_tsf_0_15 = rxh->RxTSFTime;
7934
7935         /*
7936          * a greater tsf time indicates the low 16 bits of
7937          * tsf_l wrapped, so decrement the high 16 bits.
7938          */
7939         if ((u16)tsf_l < rx_tsf_0_15) {
7940                 rx_tsf_16_31 -= 1;
7941                 if (rx_tsf_16_31 == 0xffff)
7942                         tsf_h -= 1;
7943         }
7944
7945         return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
7946 }
7947
7948 static void
7949 prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7950                      struct sk_buff *p,
7951                      struct ieee80211_rx_status *rx_status)
7952 {
7953         int preamble;
7954         int channel;
7955         u32 rspec;
7956         unsigned char *plcp;
7957
7958         /* fill in TSF and flag its presence */
7959         rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
7960         rx_status->flag |= RX_FLAG_MACTIME_MPDU;
7961
7962         channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
7963
7964         if (channel > 14) {
7965                 rx_status->band = IEEE80211_BAND_5GHZ;
7966                 rx_status->freq = ieee80211_ofdm_chan_to_freq(
7967                                         WF_CHAN_FACTOR_5_G/2, channel);
7968
7969         } else {
7970                 rx_status->band = IEEE80211_BAND_2GHZ;
7971                 rx_status->freq = ieee80211_dsss_chan_to_freq(channel);
7972         }
7973
7974         rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
7975
7976         /* noise */
7977         /* qual */
7978         rx_status->antenna =
7979                 (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
7980
7981         plcp = p->data;
7982
7983         rspec = brcms_c_compute_rspec(rxh, plcp);
7984         if (is_mcs_rate(rspec)) {
7985                 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
7986                 rx_status->flag |= RX_FLAG_HT;
7987                 if (rspec_is40mhz(rspec))
7988                         rx_status->flag |= RX_FLAG_40MHZ;
7989         } else {
7990                 switch (rspec2rate(rspec)) {
7991                 case BRCM_RATE_1M:
7992                         rx_status->rate_idx = 0;
7993                         break;
7994                 case BRCM_RATE_2M:
7995                         rx_status->rate_idx = 1;
7996                         break;
7997                 case BRCM_RATE_5M5:
7998                         rx_status->rate_idx = 2;
7999                         break;
8000                 case BRCM_RATE_11M:
8001                         rx_status->rate_idx = 3;
8002                         break;
8003                 case BRCM_RATE_6M:
8004                         rx_status->rate_idx = 4;
8005                         break;
8006                 case BRCM_RATE_9M:
8007                         rx_status->rate_idx = 5;
8008                         break;
8009                 case BRCM_RATE_12M:
8010                         rx_status->rate_idx = 6;
8011                         break;
8012                 case BRCM_RATE_18M:
8013                         rx_status->rate_idx = 7;
8014                         break;
8015                 case BRCM_RATE_24M:
8016                         rx_status->rate_idx = 8;
8017                         break;
8018                 case BRCM_RATE_36M:
8019                         rx_status->rate_idx = 9;
8020                         break;
8021                 case BRCM_RATE_48M:
8022                         rx_status->rate_idx = 10;
8023                         break;
8024                 case BRCM_RATE_54M:
8025                         rx_status->rate_idx = 11;
8026                         break;
8027                 default:
8028                         wiphy_err(wlc->wiphy, "%s: Unknown rate\n", __func__);
8029                 }
8030
8031                 /*
8032                  * For 5GHz, we should decrease the index as it is
8033                  * a subset of the 2.4G rates. See bitrates field
8034                  * of brcms_band_5GHz_nphy (in mac80211_if.c).
8035                  */
8036                 if (rx_status->band == IEEE80211_BAND_5GHZ)
8037                         rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
8038
8039                 /* Determine short preamble and rate_idx */
8040                 preamble = 0;
8041                 if (is_cck_rate(rspec)) {
8042                         if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
8043                                 rx_status->flag |= RX_FLAG_SHORTPRE;
8044                 } else if (is_ofdm_rate(rspec)) {
8045                         rx_status->flag |= RX_FLAG_SHORTPRE;
8046                 } else {
8047                         wiphy_err(wlc->wiphy, "%s: Unknown modulation\n",
8048                                   __func__);
8049                 }
8050         }
8051
8052         if (plcp3_issgi(plcp[3]))
8053                 rx_status->flag |= RX_FLAG_SHORT_GI;
8054
8055         if (rxh->RxStatus1 & RXS_DECERR) {
8056                 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
8057                 wiphy_err(wlc->wiphy, "%s:  RX_FLAG_FAILED_PLCP_CRC\n",
8058                           __func__);
8059         }
8060         if (rxh->RxStatus1 & RXS_FCSERR) {
8061                 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
8062                 wiphy_err(wlc->wiphy, "%s:  RX_FLAG_FAILED_FCS_CRC\n",
8063                           __func__);
8064         }
8065 }
8066
8067 static void
8068 brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
8069                 struct sk_buff *p)
8070 {
8071         int len_mpdu;
8072         struct ieee80211_rx_status rx_status;
8073
8074         memset(&rx_status, 0, sizeof(rx_status));
8075         prep_mac80211_status(wlc, rxh, p, &rx_status);
8076
8077         /* mac header+body length, exclude CRC and plcp header */
8078         len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
8079         skb_pull(p, D11_PHY_HDR_LEN);
8080         __skb_trim(p, len_mpdu);
8081
8082         memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
8083         ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
8084 }
8085
8086 /* Process received frames */
8087 /*
8088  * Return true if more frames need to be processed. false otherwise.
8089  * Param 'bound' indicates max. # frames to process before break out.
8090  */
8091 void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
8092 {
8093         struct d11rxhdr *rxh;
8094         struct ieee80211_hdr *h;
8095         uint len;
8096         bool is_amsdu;
8097
8098         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
8099
8100         /* frame starts with rxhdr */
8101         rxh = (struct d11rxhdr *) (p->data);
8102
8103         /* strip off rxhdr */
8104         skb_pull(p, BRCMS_HWRXOFF);
8105
8106         /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
8107         if (rxh->RxStatus1 & RXS_PBPRES) {
8108                 if (p->len < 2) {
8109                         wiphy_err(wlc->wiphy, "wl%d: recv: rcvd runt of "
8110                                   "len %d\n", wlc->pub->unit, p->len);
8111                         goto toss;
8112                 }
8113                 skb_pull(p, 2);
8114         }
8115
8116         h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
8117         len = p->len;
8118
8119         if (rxh->RxStatus1 & RXS_FCSERR) {
8120                 if (wlc->pub->mac80211_state & MAC80211_PROMISC_BCNS) {
8121                         wiphy_err(wlc->wiphy, "FCSERR while scanning******* -"
8122                                   " tossing\n");
8123                         goto toss;
8124                 } else {
8125                         wiphy_err(wlc->wiphy, "RCSERR!!!\n");
8126                         goto toss;
8127                 }
8128         }
8129
8130         /* check received pkt has at least frame control field */
8131         if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
8132                 goto toss;
8133
8134         /* not supporting A-MSDU */
8135         is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
8136         if (is_amsdu)
8137                 goto toss;
8138
8139         brcms_c_recvctl(wlc, rxh, p);
8140         return;
8141
8142  toss:
8143         brcmu_pkt_buf_free_skb(p);
8144 }
8145
8146 /* calculate frame duration for Mixed-mode L-SIG spoofing, return
8147  * number of bytes goes in the length field
8148  *
8149  * Formula given by HT PHY Spec v 1.13
8150  *   len = 3(nsyms + nstream + 3) - 3
8151  */
8152 u16
8153 brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
8154                       uint mac_len)
8155 {
8156         uint nsyms, len = 0, kNdps;
8157
8158         BCMMSG(wlc->wiphy, "wl%d: rate %d, len%d\n",
8159                  wlc->pub->unit, rspec2rate(ratespec), mac_len);
8160
8161         if (is_mcs_rate(ratespec)) {
8162                 uint mcs = ratespec & RSPEC_RATE_MASK;
8163                 int tot_streams = (mcs_2_txstreams(mcs) + 1) +
8164                                   rspec_stc(ratespec);
8165
8166                 /*
8167                  * the payload duration calculation matches that
8168                  * of regular ofdm
8169                  */
8170                 /* 1000Ndbps = kbps * 4 */
8171                 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
8172                                    rspec_issgi(ratespec)) * 4;
8173
8174                 if (rspec_stc(ratespec) == 0)
8175                         nsyms =
8176                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
8177                                   APHY_TAIL_NBITS) * 1000, kNdps);
8178                 else
8179                         /* STBC needs to have even number of symbols */
8180                         nsyms =
8181                             2 *
8182                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
8183                                   APHY_TAIL_NBITS) * 1000, 2 * kNdps);
8184
8185                 /* (+3) account for HT-SIG(2) and HT-STF(1) */
8186                 nsyms += (tot_streams + 3);
8187                 /*
8188                  * 3 bytes/symbol @ legacy 6Mbps rate
8189                  * (-3) excluding service bits and tail bits
8190                  */
8191                 len = (3 * nsyms) - 3;
8192         }
8193
8194         return (u16) len;
8195 }
8196
8197 /*
8198  * calculate frame duration of a given rate and length, return
8199  * time in usec unit
8200  */
8201 uint
8202 brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
8203                         u8 preamble_type, uint mac_len)
8204 {
8205         uint nsyms, dur = 0, Ndps, kNdps;
8206         uint rate = rspec2rate(ratespec);
8207
8208         if (rate == 0) {
8209                 wiphy_err(wlc->wiphy, "wl%d: WAR: using rate of 1 mbps\n",
8210                           wlc->pub->unit);
8211                 rate = BRCM_RATE_1M;
8212         }
8213
8214         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, len%d\n",
8215                  wlc->pub->unit, ratespec, preamble_type, mac_len);
8216
8217         if (is_mcs_rate(ratespec)) {
8218                 uint mcs = ratespec & RSPEC_RATE_MASK;
8219                 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
8220
8221                 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
8222                 if (preamble_type == BRCMS_MM_PREAMBLE)
8223                         dur += PREN_MM_EXT;
8224                 /* 1000Ndbps = kbps * 4 */
8225                 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
8226                                    rspec_issgi(ratespec)) * 4;
8227
8228                 if (rspec_stc(ratespec) == 0)
8229                         nsyms =
8230                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
8231                                   APHY_TAIL_NBITS) * 1000, kNdps);
8232                 else
8233                         /* STBC needs to have even number of symbols */
8234                         nsyms =
8235                             2 *
8236                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
8237                                   APHY_TAIL_NBITS) * 1000, 2 * kNdps);
8238
8239                 dur += APHY_SYMBOL_TIME * nsyms;
8240                 if (wlc->band->bandtype == BRCM_BAND_2G)
8241                         dur += DOT11_OFDM_SIGNAL_EXTENSION;
8242         } else if (is_ofdm_rate(rate)) {
8243                 dur = APHY_PREAMBLE_TIME;
8244                 dur += APHY_SIGNAL_TIME;
8245                 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
8246                 Ndps = rate * 2;
8247                 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
8248                 nsyms =
8249                     CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
8250                          Ndps);
8251                 dur += APHY_SYMBOL_TIME * nsyms;
8252                 if (wlc->band->bandtype == BRCM_BAND_2G)
8253                         dur += DOT11_OFDM_SIGNAL_EXTENSION;
8254         } else {
8255                 /*
8256                  * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
8257                  * will divide out
8258                  */
8259                 mac_len = mac_len * 8 * 2;
8260                 /* calc ceiling of bits/rate = microseconds of air time */
8261                 dur = (mac_len + rate - 1) / rate;
8262                 if (preamble_type & BRCMS_SHORT_PREAMBLE)
8263                         dur += BPHY_PLCP_SHORT_TIME;
8264                 else
8265                         dur += BPHY_PLCP_TIME;
8266         }
8267         return dur;
8268 }
8269
8270 /* derive wlc->band->basic_rate[] table from 'rateset' */
8271 void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
8272                               struct brcms_c_rateset *rateset)
8273 {
8274         u8 rate;
8275         u8 mandatory;
8276         u8 cck_basic = 0;
8277         u8 ofdm_basic = 0;
8278         u8 *br = wlc->band->basic_rate;
8279         uint i;
8280
8281         /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
8282         memset(br, 0, BRCM_MAXRATE + 1);
8283
8284         /* For each basic rate in the rates list, make an entry in the
8285          * best basic lookup.
8286          */
8287         for (i = 0; i < rateset->count; i++) {
8288                 /* only make an entry for a basic rate */
8289                 if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
8290                         continue;
8291
8292                 /* mask off basic bit */
8293                 rate = (rateset->rates[i] & BRCMS_RATE_MASK);
8294
8295                 if (rate > BRCM_MAXRATE) {
8296                         wiphy_err(wlc->wiphy, "brcms_c_rate_lookup_init: "
8297                                   "invalid rate 0x%X in rate set\n",
8298                                   rateset->rates[i]);
8299                         continue;
8300                 }
8301
8302                 br[rate] = rate;
8303         }
8304
8305         /* The rate lookup table now has non-zero entries for each
8306          * basic rate, equal to the basic rate: br[basicN] = basicN
8307          *
8308          * To look up the best basic rate corresponding to any
8309          * particular rate, code can use the basic_rate table
8310          * like this
8311          *
8312          * basic_rate = wlc->band->basic_rate[tx_rate]
8313          *
8314          * Make sure there is a best basic rate entry for
8315          * every rate by walking up the table from low rates
8316          * to high, filling in holes in the lookup table
8317          */
8318
8319         for (i = 0; i < wlc->band->hw_rateset.count; i++) {
8320                 rate = wlc->band->hw_rateset.rates[i];
8321
8322                 if (br[rate] != 0) {
8323                         /* This rate is a basic rate.
8324                          * Keep track of the best basic rate so far by
8325                          * modulation type.
8326                          */
8327                         if (is_ofdm_rate(rate))
8328                                 ofdm_basic = rate;
8329                         else
8330                                 cck_basic = rate;
8331
8332                         continue;
8333                 }
8334
8335                 /* This rate is not a basic rate so figure out the
8336                  * best basic rate less than this rate and fill in
8337                  * the hole in the table
8338                  */
8339
8340                 br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
8341
8342                 if (br[rate] != 0)
8343                         continue;
8344
8345                 if (is_ofdm_rate(rate)) {
8346                         /*
8347                          * In 11g and 11a, the OFDM mandatory rates
8348                          * are 6, 12, and 24 Mbps
8349                          */
8350                         if (rate >= BRCM_RATE_24M)
8351                                 mandatory = BRCM_RATE_24M;
8352                         else if (rate >= BRCM_RATE_12M)
8353                                 mandatory = BRCM_RATE_12M;
8354                         else
8355                                 mandatory = BRCM_RATE_6M;
8356                 } else {
8357                         /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
8358                         mandatory = rate;
8359                 }
8360
8361                 br[rate] = mandatory;
8362         }
8363 }
8364
8365 static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
8366                                    u8 basic_rate)
8367 {
8368         u8 phy_rate, index;
8369         u8 basic_phy_rate, basic_index;
8370         u16 dir_table, basic_table;
8371         u16 basic_ptr;
8372
8373         /* Shared memory address for the table we are reading */
8374         dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
8375
8376         /* Shared memory address for the table we are writing */
8377         basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
8378
8379         /*
8380          * for a given rate, the LS-nibble of the PLCP SIGNAL field is
8381          * the index into the rate table.
8382          */
8383         phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
8384         basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
8385         index = phy_rate & 0xf;
8386         basic_index = basic_phy_rate & 0xf;
8387
8388         /* Find the SHM pointer to the ACK rate entry by looking in the
8389          * Direct-map Table
8390          */
8391         basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
8392
8393         /* Update the SHM BSS-basic-rate-set mapping table with the pointer
8394          * to the correct basic rate for the given incoming rate
8395          */
8396         brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
8397 }
8398
8399 static const struct brcms_c_rateset *
8400 brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
8401 {
8402         const struct brcms_c_rateset *rs_dflt;
8403
8404         if (BRCMS_PHY_11N_CAP(wlc->band)) {
8405                 if (wlc->band->bandtype == BRCM_BAND_5G)
8406                         rs_dflt = &ofdm_mimo_rates;
8407                 else
8408                         rs_dflt = &cck_ofdm_mimo_rates;
8409         } else if (wlc->band->gmode)
8410                 rs_dflt = &cck_ofdm_rates;
8411         else
8412                 rs_dflt = &cck_rates;
8413
8414         return rs_dflt;
8415 }
8416
8417 void brcms_c_set_ratetable(struct brcms_c_info *wlc)
8418 {
8419         const struct brcms_c_rateset *rs_dflt;
8420         struct brcms_c_rateset rs;
8421         u8 rate, basic_rate;
8422         uint i;
8423
8424         rs_dflt = brcms_c_rateset_get_hwrs(wlc);
8425
8426         brcms_c_rateset_copy(rs_dflt, &rs);
8427         brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
8428
8429         /* walk the phy rate table and update SHM basic rate lookup table */
8430         for (i = 0; i < rs.count; i++) {
8431                 rate = rs.rates[i] & BRCMS_RATE_MASK;
8432
8433                 /* for a given rate brcms_basic_rate returns the rate at
8434                  * which a response ACK/CTS should be sent.
8435                  */
8436                 basic_rate = brcms_basic_rate(wlc, rate);
8437                 if (basic_rate == 0)
8438                         /* This should only happen if we are using a
8439                          * restricted rateset.
8440                          */
8441                         basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
8442
8443                 brcms_c_write_rate_shm(wlc, rate, basic_rate);
8444         }
8445 }
8446
8447 /*
8448  * Return true if the specified rate is supported by the specified band.
8449  * BRCM_BAND_AUTO indicates the current band.
8450  */
8451 bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
8452                     bool verbose)
8453 {
8454         struct brcms_c_rateset *hw_rateset;
8455         uint i;
8456
8457         if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
8458                 hw_rateset = &wlc->band->hw_rateset;
8459         else if (wlc->pub->_nbands > 1)
8460                 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
8461         else
8462                 /* other band specified and we are a single band device */
8463                 return false;
8464
8465         /* check if this is a mimo rate */
8466         if (is_mcs_rate(rspec)) {
8467                 if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
8468                         goto error;
8469
8470                 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
8471         }
8472
8473         for (i = 0; i < hw_rateset->count; i++)
8474                 if (hw_rateset->rates[i] == rspec2rate(rspec))
8475                         return true;
8476  error:
8477         if (verbose)
8478                 wiphy_err(wlc->wiphy, "wl%d: valid_rate: rate spec 0x%x "
8479                           "not in hw_rateset\n", wlc->pub->unit, rspec);
8480
8481         return false;
8482 }
8483
8484 void brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
8485 {
8486         const struct brcms_c_rateset *rs_dflt;
8487         struct brcms_c_rateset rs;
8488         u8 rate;
8489         u16 entry_ptr;
8490         u8 plcp[D11_PHY_HDR_LEN];
8491         u16 dur, sifs;
8492         uint i;
8493
8494         sifs = get_sifs(wlc->band);
8495
8496         rs_dflt = brcms_c_rateset_get_hwrs(wlc);
8497
8498         brcms_c_rateset_copy(rs_dflt, &rs);
8499         brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
8500
8501         /*
8502          * walk the phy rate table and update MAC core SHM
8503          * basic rate table entries
8504          */
8505         for (i = 0; i < rs.count; i++) {
8506                 rate = rs.rates[i] & BRCMS_RATE_MASK;
8507
8508                 entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
8509
8510                 /* Calculate the Probe Response PLCP for the given rate */
8511                 brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
8512
8513                 /*
8514                  * Calculate the duration of the Probe Response
8515                  * frame plus SIFS for the MAC
8516                  */
8517                 dur = (u16) brcms_c_calc_frame_time(wlc, rate,
8518                                                 BRCMS_LONG_PREAMBLE, frame_len);
8519                 dur += sifs;
8520
8521                 /* Update the SHM Rate Table entry Probe Response values */
8522                 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
8523                               (u16) (plcp[0] + (plcp[1] << 8)));
8524                 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
8525                               (u16) (plcp[2] + (plcp[3] << 8)));
8526                 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
8527         }
8528 }
8529
8530 /*      Max buffering needed for beacon template/prb resp template is 142 bytes.
8531  *
8532  *      PLCP header is 6 bytes.
8533  *      802.11 A3 header is 24 bytes.
8534  *      Max beacon frame body template length is 112 bytes.
8535  *      Max probe resp frame body template length is 110 bytes.
8536  *
8537  *      *len on input contains the max length of the packet available.
8538  *
8539  *      The *len value is set to the number of bytes in buf used, and starts
8540  *      with the PLCP and included up to, but not including, the 4 byte FCS.
8541  */
8542 static void
8543 brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
8544                          u32 bcn_rspec,
8545                          struct brcms_bss_cfg *cfg, u16 *buf, int *len)
8546 {
8547         static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
8548         struct cck_phy_hdr *plcp;
8549         struct ieee80211_mgmt *h;
8550         int hdr_len, body_len;
8551
8552         hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
8553
8554         /* calc buffer size provided for frame body */
8555         body_len = *len - hdr_len;
8556         /* return actual size */
8557         *len = hdr_len + body_len;
8558
8559         /* format PHY and MAC headers */
8560         memset((char *)buf, 0, hdr_len);
8561
8562         plcp = (struct cck_phy_hdr *) buf;
8563
8564         /*
8565          * PLCP for Probe Response frames are filled in from
8566          * core's rate table
8567          */
8568         if (type == IEEE80211_STYPE_BEACON)
8569                 /* fill in PLCP */
8570                 brcms_c_compute_plcp(wlc, bcn_rspec,
8571                                  (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
8572                                  (u8 *) plcp);
8573
8574         /* "Regular" and 16 MBSS but not for 4 MBSS */
8575         /* Update the phytxctl for the beacon based on the rspec */
8576         brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
8577
8578         h = (struct ieee80211_mgmt *)&plcp[1];
8579
8580         /* fill in 802.11 header */
8581         h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
8582
8583         /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
8584         /* A1 filled in by MAC for prb resp, broadcast for bcn */
8585         if (type == IEEE80211_STYPE_BEACON)
8586                 memcpy(&h->da, &ether_bcast, ETH_ALEN);
8587         memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
8588         memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
8589
8590         /* SEQ filled in by MAC */
8591 }
8592
8593 int brcms_c_get_header_len(void)
8594 {
8595         return TXOFF;
8596 }
8597
8598 /*
8599  * Update all beacons for the system.
8600  */
8601 void brcms_c_update_beacon(struct brcms_c_info *wlc)
8602 {
8603         struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
8604
8605         if (bsscfg->up && !bsscfg->BSS)
8606                 /* Clear the soft intmask */
8607                 wlc->defmacintmask &= ~MI_BCNTPL;
8608 }
8609
8610 /* Write ssid into shared memory */
8611 void brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
8612 {
8613         u8 *ssidptr = cfg->SSID;
8614         u16 base = M_SSID;
8615         u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
8616
8617         /* padding the ssid with zero and copy it into shm */
8618         memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
8619         memcpy(ssidbuf, ssidptr, cfg->SSID_len);
8620
8621         brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
8622         brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
8623 }
8624
8625 void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
8626 {
8627         struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
8628
8629         /* update AP or IBSS probe responses */
8630         if (bsscfg->up && !bsscfg->BSS)
8631                 brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
8632 }
8633
8634 void
8635 brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
8636                               struct brcms_bss_cfg *cfg,
8637                               bool suspend)
8638 {
8639         u16 prb_resp[BCN_TMPL_LEN / 2];
8640         int len = BCN_TMPL_LEN;
8641
8642         /*
8643          * write the probe response to hardware, or save in
8644          * the config structure
8645          */
8646
8647         /* create the probe response template */
8648         brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
8649                                  cfg, prb_resp, &len);
8650
8651         if (suspend)
8652                 brcms_c_suspend_mac_and_wait(wlc);
8653
8654         /* write the probe response into the template region */
8655         brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
8656                                     (len + 3) & ~3, prb_resp);
8657
8658         /* write the length of the probe response frame (+PLCP/-FCS) */
8659         brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
8660
8661         /* write the SSID and SSID length */
8662         brcms_c_shm_ssid_upd(wlc, cfg);
8663
8664         /*
8665          * Write PLCP headers and durations for probe response frames
8666          * at all rates. Use the actual frame length covered by the
8667          * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
8668          * by subtracting the PLCP len and adding the FCS.
8669          */
8670         len += (-D11_PHY_HDR_LEN + FCS_LEN);
8671         brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
8672
8673         if (suspend)
8674                 brcms_c_enable_mac(wlc);
8675 }
8676
8677 /* prepares pdu for transmission. returns BCM error codes */
8678 int brcms_c_prep_pdu(struct brcms_c_info *wlc, struct sk_buff *pdu, uint *fifop)
8679 {
8680         uint fifo;
8681         struct d11txh *txh;
8682         struct ieee80211_hdr *h;
8683         struct scb *scb;
8684
8685         txh = (struct d11txh *) (pdu->data);
8686         h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
8687
8688         /* get the pkt queue info. This was put at brcms_c_sendctl or
8689          * brcms_c_send for PDU */
8690         fifo = le16_to_cpu(txh->TxFrameID) & TXFID_QUEUE_MASK;
8691
8692         scb = NULL;
8693
8694         *fifop = fifo;
8695
8696         /* return if insufficient dma resources */
8697         if (*wlc->core->txavail[fifo] < MAX_DMA_SEGS) {
8698                 /* Mark precedences related to this FIFO, unsendable */
8699                 /* A fifo is full. Clear precedences related to that FIFO */
8700                 wlc->tx_prec_map &= ~(wlc->fifo2prec_map[fifo]);
8701                 return -EBUSY;
8702         }
8703         return 0;
8704 }
8705
8706 void brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
8707 {
8708         brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
8709                 wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
8710                 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
8711                 brcms_chspec_bw(wlc->default_bss->chanspec),
8712                 wlc->stf->txstreams);
8713 }
8714
8715 /* Copy a buffer to shared memory.
8716  * SHM 'offset' needs to be an even address and
8717  * Buffer length 'len' must be an even number of bytes
8718  */
8719 void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset, const void *buf,
8720                         int len)
8721 {
8722         brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8723 }
8724
8725 int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
8726                            uint *blocks)
8727 {
8728         if (fifo >= NFIFO)
8729                 return -EINVAL;
8730
8731         *blocks = wlc_hw->xmtfifo_sz[fifo];
8732
8733         return 0;
8734 }
8735
8736 void
8737 brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
8738                   const u8 *addr)
8739 {
8740         brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
8741         if (match_reg_offset == RCM_BSSID_OFFSET)
8742                 memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
8743 }
8744
8745 /* check for the particular priority flow control bit being set */
8746 bool
8747 brcms_c_txflowcontrol_prio_isset(struct brcms_c_info *wlc,
8748                                  struct brcms_txq_info *q,
8749                                  int prio)
8750 {
8751         uint prio_mask;
8752
8753         if (prio == ALLPRIO)
8754                 prio_mask = TXQ_STOP_FOR_PRIOFC_MASK;
8755         else
8756                 prio_mask = NBITVAL(prio);
8757
8758         return (q->stopped & prio_mask) == prio_mask;
8759 }
8760
8761 /* propagate the flow control to all interfaces using the given tx queue */
8762 void brcms_c_txflowcontrol(struct brcms_c_info *wlc,
8763                            struct brcms_txq_info *qi,
8764                            bool on, int prio)
8765 {
8766         uint prio_bits;
8767         uint cur_bits;
8768
8769         BCMMSG(wlc->wiphy, "flow control kicks in\n");
8770
8771         if (prio == ALLPRIO)
8772                 prio_bits = TXQ_STOP_FOR_PRIOFC_MASK;
8773         else
8774                 prio_bits = NBITVAL(prio);
8775
8776         cur_bits = qi->stopped & prio_bits;
8777
8778         /* Check for the case of no change and return early
8779          * Otherwise update the bit and continue
8780          */
8781         if (on) {
8782                 if (cur_bits == prio_bits)
8783                         return;
8784
8785                 mboolset(qi->stopped, prio_bits);
8786         } else {
8787                 if (cur_bits == 0)
8788                         return;
8789
8790                 mboolclr(qi->stopped, prio_bits);
8791         }
8792
8793         /* If there is a flow control override we will not change the external
8794          * flow control state.
8795          */
8796         if (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK)
8797                 return;
8798
8799         brcms_c_txflowcontrol_signal(wlc, qi, on, prio);
8800 }
8801
8802 void
8803 brcms_c_txflowcontrol_override(struct brcms_c_info *wlc,
8804                                struct brcms_txq_info *qi,
8805                                bool on, uint override)
8806 {
8807         uint prev_override;
8808
8809         prev_override = (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK);
8810
8811         /* Update the flow control bits and do an early return if there is
8812          * no change in the external flow control state.
8813          */
8814         if (on) {
8815                 mboolset(qi->stopped, override);
8816                 /* if there was a previous override bit on, then setting this
8817                  * makes no difference.
8818                  */
8819                 if (prev_override)
8820                         return;
8821
8822                 brcms_c_txflowcontrol_signal(wlc, qi, ON, ALLPRIO);
8823         } else {
8824                 mboolclr(qi->stopped, override);
8825                 /* clearing an override bit will only make a difference for
8826                  * flow control if it was the only bit set. For any other
8827                  * override setting, just return
8828                  */
8829                 if (prev_override != override)
8830                         return;
8831
8832                 if (qi->stopped == 0) {
8833                         brcms_c_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8834                 } else {
8835                         int prio;
8836
8837                         for (prio = MAXPRIO; prio >= 0; prio--) {
8838                                 if (!mboolisset(qi->stopped, NBITVAL(prio)))
8839                                         brcms_c_txflowcontrol_signal(
8840                                                 wlc, qi, OFF, prio);
8841                         }
8842                 }
8843         }
8844 }
8845
8846 /*
8847  * Flag 'scan in progress' to withhold dynamic phy calibration
8848  */
8849 void brcms_c_scan_start(struct brcms_c_info *wlc)
8850 {
8851         wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
8852 }
8853
8854 void brcms_c_scan_stop(struct brcms_c_info *wlc)
8855 {
8856         wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
8857 }
8858
8859 void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
8860 {
8861         wlc->pub->associated = state;
8862         wlc->bsscfg->associated = state;
8863 }
8864
8865 /*
8866  * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
8867  * AMPDU traffic, packets pending in hardware have to be invalidated so that
8868  * when later on hardware releases them, they can be handled appropriately.
8869  */
8870 void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
8871                                struct ieee80211_sta *sta,
8872                                void (*dma_callback_fn))
8873 {
8874         struct dma_pub *dmah;
8875         int i;
8876         for (i = 0; i < NFIFO; i++) {
8877                 dmah = hw->di[i];
8878                 if (dmah != NULL)
8879                         dma_walk_packets(dmah, dma_callback_fn, sta);
8880         }
8881 }
8882
8883 int brcms_c_get_curband(struct brcms_c_info *wlc)
8884 {
8885         return wlc->band->bandunit;
8886 }
8887
8888 void brcms_c_wait_for_tx_completion(struct brcms_c_info *wlc, bool drop)
8889 {
8890         /* flush packet queue when requested */
8891         if (drop)
8892                 brcmu_pktq_flush(&wlc->pkt_queue->q, false, NULL, NULL);
8893
8894         /* wait for queue and DMA fifos to run dry */
8895         while (!pktq_empty(&wlc->pkt_queue->q) || brcms_txpktpendtot(wlc) > 0)
8896                 brcms_msleep(wlc->wl, 1);
8897 }
8898
8899 void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
8900 {
8901         wlc->bcn_li_bcn = interval;
8902         if (wlc->pub->up)
8903                 brcms_c_bcn_li_upd(wlc);
8904 }
8905
8906 int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
8907 {
8908         uint qdbm;
8909
8910         /* Remove override bit and clip to max qdbm value */
8911         qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
8912         return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
8913 }
8914
8915 int brcms_c_get_tx_power(struct brcms_c_info *wlc)
8916 {
8917         uint qdbm;
8918         bool override;
8919
8920         wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
8921
8922         /* Return qdbm units */
8923         return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
8924 }
8925
8926 void brcms_c_set_radio_mpc(struct brcms_c_info *wlc, bool mpc)
8927 {
8928         wlc->mpc = mpc;
8929         brcms_c_radio_mpc_upd(wlc);
8930 }
8931
8932 /*
8933  * Search the name=value vars for a specific one and return its value.
8934  * Returns NULL if not found.
8935  */
8936 char *getvar(char *vars, const char *name)
8937 {
8938         char *s;
8939         int len;
8940
8941         if (!name)
8942                 return NULL;
8943
8944         len = strlen(name);
8945         if (len == 0)
8946                 return NULL;
8947
8948         /* first look in vars[] */
8949         for (s = vars; s && *s;) {
8950                 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
8951                         return &s[len + 1];
8952
8953                 while (*s++)
8954                         ;
8955         }
8956         /* nothing found */
8957         return NULL;
8958 }
8959
8960 /*
8961  * Search the vars for a specific one and return its value as
8962  * an integer. Returns 0 if not found.
8963  */
8964 int getintvar(char *vars, const char *name)
8965 {
8966         char *val;
8967         unsigned long res;
8968
8969         val = getvar(vars, name);
8970         if (val && !kstrtoul(val, 0, &res))
8971                 return res;
8972
8973         return 0;
8974 }