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