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