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