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