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