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