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