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