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