blob: 8ef02dca8f8cab02eca81a71de91bac67612451b [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
Joe Perches02f77192012-01-15 00:38:44 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Arend van Spriel5b435de2011-10-05 13:19:03 +020019#include <linux/pci_ids.h>
20#include <linux/if_ether.h>
Seth Forsheeedc76512012-06-16 07:47:56 -050021#include <net/cfg80211.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020022#include <net/mac80211.h>
23#include <brcm_hw_ids.h>
24#include <aiutils.h>
25#include <chipcommon.h>
26#include "rate.h"
27#include "scb.h"
28#include "phy/phy_hal.h"
29#include "channel.h"
30#include "antsel.h"
31#include "stf.h"
32#include "ampdu.h"
33#include "mac80211_if.h"
34#include "ucode_loader.h"
35#include "main.h"
Alwin Beukers23038212011-10-18 14:02:58 +020036#include "soc.h"
Seth Forsheee041f652012-11-15 08:07:56 -060037#include "dma.h"
Seth Forsheeb353dda2012-11-15 08:08:03 -060038#include "debug.h"
Seth Forsheecdf43522012-11-15 08:08:09 -060039#include "brcms_trace_events.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020040
Arend van Spriel5b435de2011-10-05 13:19:03 +020041/* watchdog timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020042#define TIMER_INTERVAL_WATCHDOG 1000
Arend van Spriel5b435de2011-10-05 13:19:03 +020043/* radio monitor timer, in unit of ms */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020044#define TIMER_INTERVAL_RADIOCHK 800
Arend van Spriel5b435de2011-10-05 13:19:03 +020045
Arend van Spriel5b435de2011-10-05 13:19:03 +020046/* beacon interval, in unit of 1024TU */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020047#define BEACON_INTERVAL_DEFAULT 100
Arend van Spriel5b435de2011-10-05 13:19:03 +020048
49/* n-mode support capability */
50/* 2x2 includes both 1x1 & 2x2 devices
51 * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
52 * control it independently
53 */
54#define WL_11N_2x2 1
55#define WL_11N_3x3 3
56#define WL_11N_4x4 4
57
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020058#define EDCF_ACI_MASK 0x60
59#define EDCF_ACI_SHIFT 5
60#define EDCF_ECWMIN_MASK 0x0f
61#define EDCF_ECWMAX_SHIFT 4
62#define EDCF_AIFSN_MASK 0x0f
63#define EDCF_AIFSN_MAX 15
64#define EDCF_ECWMAX_MASK 0xf0
Arend van Spriel5b435de2011-10-05 13:19:03 +020065
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020066#define EDCF_AC_BE_TXOP_STA 0x0000
67#define EDCF_AC_BK_TXOP_STA 0x0000
68#define EDCF_AC_VO_ACI_STA 0x62
69#define EDCF_AC_VO_ECW_STA 0x32
70#define EDCF_AC_VI_ACI_STA 0x42
71#define EDCF_AC_VI_ECW_STA 0x43
72#define EDCF_AC_BK_ECW_STA 0xA4
73#define EDCF_AC_VI_TXOP_STA 0x005e
74#define EDCF_AC_VO_TXOP_STA 0x002f
75#define EDCF_AC_BE_ACI_STA 0x03
76#define EDCF_AC_BE_ECW_STA 0xA4
77#define EDCF_AC_BK_ACI_STA 0x27
78#define EDCF_AC_VO_TXOP_AP 0x002f
Arend van Spriel5b435de2011-10-05 13:19:03 +020079
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020080#define EDCF_TXOP2USEC(txop) ((txop) << 5)
81#define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
Arend van Spriel5b435de2011-10-05 13:19:03 +020082
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020083#define APHY_SYMBOL_TIME 4
84#define APHY_PREAMBLE_TIME 16
85#define APHY_SIGNAL_TIME 4
86#define APHY_SIFS_TIME 16
87#define APHY_SERVICE_NBITS 16
88#define APHY_TAIL_NBITS 6
89#define BPHY_SIFS_TIME 10
90#define BPHY_PLCP_SHORT_TIME 96
Arend van Spriel5b435de2011-10-05 13:19:03 +020091
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020092#define PREN_PREAMBLE 24
93#define PREN_MM_EXT 12
94#define PREN_PREAMBLE_EXT 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020095
96#define DOT11_MAC_HDR_LEN 24
Alwin Beukers73ffc2f2011-10-18 14:02:57 +020097#define DOT11_ACK_LEN 10
98#define DOT11_BA_LEN 4
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#define DOT11_OFDM_SIGNAL_EXTENSION 6
100#define DOT11_MIN_FRAG_LEN 256
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200101#define DOT11_RTS_LEN 16
102#define DOT11_CTS_LEN 10
Arend van Spriel5b435de2011-10-05 13:19:03 +0200103#define DOT11_BA_BITMAP_LEN 128
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200104#define DOT11_MAXNUMFRAGS 16
Arend van Spriel5b435de2011-10-05 13:19:03 +0200105#define DOT11_MAX_FRAG_LEN 2346
106
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200107#define BPHY_PLCP_TIME 192
108#define RIFS_11N_TIME 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200109
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200110/* length of the BCN template area */
111#define BCN_TMPL_LEN 512
Arend van Spriel5b435de2011-10-05 13:19:03 +0200112
113/* brcms_bss_info flag bit values */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200114#define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200115
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200116/* chip rx buffer offset */
117#define BRCMS_HWRXOFF 38
Arend van Spriel5b435de2011-10-05 13:19:03 +0200118
119/* rfdisable delay timer 500 ms, runs of ALP clock */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200120#define RFDISABLE_DEFAULT 10000000
Arend van Spriel5b435de2011-10-05 13:19:03 +0200121
122#define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
123
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200124/* synthpu_dly times in us */
125#define SYNTHPU_DLY_APHY_US 3700
126#define SYNTHPU_DLY_BPHY_US 1050
127#define SYNTHPU_DLY_NPHY_US 2048
128#define SYNTHPU_DLY_LPPHY_US 300
Arend van Spriel5b435de2011-10-05 13:19:03 +0200129
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200130#define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200131
132/* Per-AC retry limit register definitions; uses defs.h bitfield macros */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200133#define EDCF_SHORT_S 0
134#define EDCF_SFB_S 4
135#define EDCF_LONG_S 8
136#define EDCF_LFB_S 12
137#define EDCF_SHORT_M BITFIELD_MASK(4)
138#define EDCF_SFB_M BITFIELD_MASK(4)
139#define EDCF_LONG_M BITFIELD_MASK(4)
140#define EDCF_LFB_M BITFIELD_MASK(4)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200141
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200142#define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
143#define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
144#define RETRY_LONG_DEF 4 /* Default Long retry count */
145#define RETRY_SHORT_FB 3 /* Short count for fb rate */
146#define RETRY_LONG_FB 2 /* Long count for fb rate */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200147
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200148#define APHY_CWMIN 15
149#define PHY_CWMAX 1023
Arend van Spriel5b435de2011-10-05 13:19:03 +0200150
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200151#define EDCF_AIFSN_MIN 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200152
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200153#define FRAGNUM_MASK 0xF
Arend van Spriel5b435de2011-10-05 13:19:03 +0200154
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200155#define APHY_SLOT_TIME 9
156#define BPHY_SLOT_TIME 20
Arend van Spriel5b435de2011-10-05 13:19:03 +0200157
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200158#define WL_SPURAVOID_OFF 0
159#define WL_SPURAVOID_ON1 1
160#define WL_SPURAVOID_ON2 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200161
162/* invalid core flags, use the saved coreflags */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200163#define BRCMS_USE_COREFLAGS 0xffffffff
Arend van Spriel5b435de2011-10-05 13:19:03 +0200164
165/* values for PLCPHdr_override */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200166#define BRCMS_PLCP_AUTO -1
167#define BRCMS_PLCP_SHORT 0
168#define BRCMS_PLCP_LONG 1
Arend van Spriel5b435de2011-10-05 13:19:03 +0200169
170/* values for g_protection_override and n_protection_override */
171#define BRCMS_PROTECTION_AUTO -1
172#define BRCMS_PROTECTION_OFF 0
173#define BRCMS_PROTECTION_ON 1
174#define BRCMS_PROTECTION_MMHDR_ONLY 2
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200175#define BRCMS_PROTECTION_CTS_ONLY 3
Arend van Spriel5b435de2011-10-05 13:19:03 +0200176
177/* values for g_protection_control and n_protection_control */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200178#define BRCMS_PROTECTION_CTL_OFF 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200179#define BRCMS_PROTECTION_CTL_LOCAL 1
180#define BRCMS_PROTECTION_CTL_OVERLAP 2
181
182/* values for n_protection */
183#define BRCMS_N_PROTECTION_OFF 0
184#define BRCMS_N_PROTECTION_OPTIONAL 1
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200185#define BRCMS_N_PROTECTION_20IN40 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200186#define BRCMS_N_PROTECTION_MIXEDMODE 3
187
188/* values for band specific 40MHz capabilities */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200189#define BRCMS_N_BW_20ALL 0
190#define BRCMS_N_BW_40ALL 1
191#define BRCMS_N_BW_20IN2G_40IN5G 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200192
193/* bitflags for SGI support (sgi_rx iovar) */
194#define BRCMS_N_SGI_20 0x01
195#define BRCMS_N_SGI_40 0x02
196
197/* defines used by the nrate iovar */
198/* MSC in use,indicates b0-6 holds an mcs */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200199#define NRATE_MCS_INUSE 0x00000080
Arend van Spriel5b435de2011-10-05 13:19:03 +0200200/* rate/mcs value */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200201#define NRATE_RATE_MASK 0x0000007f
Arend van Spriel5b435de2011-10-05 13:19:03 +0200202/* stf mode mask: siso, cdd, stbc, sdm */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200203#define NRATE_STF_MASK 0x0000ff00
Arend van Spriel5b435de2011-10-05 13:19:03 +0200204/* stf mode shift */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200205#define NRATE_STF_SHIFT 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200206/* bit indicate to override mcs only */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200207#define NRATE_OVERRIDE_MCS_ONLY 0x40000000
208#define NRATE_SGI_MASK 0x00800000 /* sgi mode */
209#define NRATE_SGI_SHIFT 23 /* sgi mode */
210#define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
211#define NRATE_LDPC_SHIFT 22 /* ldpc shift */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200212
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200213#define NRATE_STF_SISO 0 /* stf mode SISO */
214#define NRATE_STF_CDD 1 /* stf mode CDD */
215#define NRATE_STF_STBC 2 /* stf mode STBC */
216#define NRATE_STF_SDM 3 /* stf mode SDM */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200217
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200218#define MAX_DMA_SEGS 4
Arend van Spriel5b435de2011-10-05 13:19:03 +0200219
Seth Forshee75be3e22012-11-15 08:07:58 -0600220/* # of entries in Tx FIFO */
221#define NTXD 64
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222/* Max # of entries in Rx FIFO based on 4kb page size */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200223#define NRXD 256
Arend van Spriel5b435de2011-10-05 13:19:03 +0200224
Seth Forsheee041f652012-11-15 08:07:56 -0600225/* Amount of headroom to leave in Tx FIFO */
226#define TX_HEADROOM 4
227
Arend van Spriel5b435de2011-10-05 13:19:03 +0200228/* try to keep this # rbufs posted to the chip */
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200229#define NRXBUFPOST 32
Arend van Spriel5b435de2011-10-05 13:19:03 +0200230
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200231/* max # frames to process in brcms_c_recv() */
232#define RXBND 8
233/* max # tx status to process in wlc_txstatus() */
234#define TXSBND 8
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235
Alwin Beukers44760652011-10-12 20:51:31 +0200236/* brcmu_format_flags() bit description structure */
237struct brcms_c_bit_desc {
238 u32 bit;
239 const char *name;
240};
241
Arend van Spriel5b435de2011-10-05 13:19:03 +0200242/*
243 * The following table lists the buffer memory allocated to xmt fifos in HW.
244 * the size is in units of 256bytes(one block), total size is HW dependent
245 * ucode has default fifo partition, sw can overwrite if necessary
246 *
247 * This is documented in twiki under the topic UcodeTxFifo. Please ensure
248 * the twiki is updated before making changes.
249 */
250
251/* Starting corerev for the fifo size table */
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200252#define XMTFIFOTBL_STARTREV 17
Arend van Spriel5b435de2011-10-05 13:19:03 +0200253
254struct d11init {
255 __le16 addr;
256 __le16 size;
257 __le32 value;
258};
259
Arend van Spriel5b435de2011-10-05 13:19:03 +0200260struct edcf_acparam {
261 u8 ACI;
262 u8 ECW;
263 u16 TXOP;
264} __packed;
265
Arend van Spriel5b435de2011-10-05 13:19:03 +0200266/* debug/trace */
Seth Forsheeb0341742012-11-15 08:08:01 -0600267uint brcm_msg_level;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200268
269/* TX FIFO number to WME/802.1E Access Category */
Arend van Sprielb7eec422011-11-10 20:30:18 +0100270static const u8 wme_fifo2ac[] = {
271 IEEE80211_AC_BK,
272 IEEE80211_AC_BE,
273 IEEE80211_AC_VI,
274 IEEE80211_AC_VO,
275 IEEE80211_AC_BE,
276 IEEE80211_AC_BE
277};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200278
Arend van Sprielb7eec422011-11-10 20:30:18 +0100279/* ieee80211 Access Category to TX FIFO number */
280static const u8 wme_ac2fifo[] = {
281 TX_AC_VO_FIFO,
282 TX_AC_VI_FIFO,
283 TX_AC_BE_FIFO,
284 TX_AC_BK_FIFO
285};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200286
Arend van Spriel5b435de2011-10-05 13:19:03 +0200287static const u16 xmtfifo_sz[][NFIFO] = {
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200288 /* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
289 {20, 192, 192, 21, 17, 5},
290 /* corerev 18: */
291 {0, 0, 0, 0, 0, 0},
292 /* corerev 19: */
293 {0, 0, 0, 0, 0, 0},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200294 /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
295 {20, 192, 192, 21, 17, 5},
296 /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
297 {9, 58, 22, 14, 14, 5},
298 /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
299 {20, 192, 192, 21, 17, 5},
300 /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
301 {20, 192, 192, 21, 17, 5},
302 /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
303 {9, 58, 22, 14, 14, 5},
Hauke Mehrtens093cd332012-07-02 20:15:51 +0200304 /* corerev 25: */
305 {0, 0, 0, 0, 0, 0},
306 /* corerev 26: */
307 {0, 0, 0, 0, 0, 0},
308 /* corerev 27: */
309 {0, 0, 0, 0, 0, 0},
310 /* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
311 {9, 58, 22, 14, 14, 5},
Arend van Spriel5b435de2011-10-05 13:19:03 +0200312};
313
Joe Perches8ae74652012-01-15 00:38:38 -0800314#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200315static const char * const fifo_names[] = {
316 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
317#else
318static const char fifo_names[6][0];
319#endif
320
Joe Perches8ae74652012-01-15 00:38:38 -0800321#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200322/* pointer to most recently allocated wl/wlc */
323static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
324#endif
325
Seth Forshee32d0f122012-11-15 08:07:55 -0600326/* Mapping of ieee80211 AC numbers to tx fifos */
327static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
328 [IEEE80211_AC_VO] = TX_AC_VO_FIFO,
329 [IEEE80211_AC_VI] = TX_AC_VI_FIFO,
330 [IEEE80211_AC_BE] = TX_AC_BE_FIFO,
331 [IEEE80211_AC_BK] = TX_AC_BK_FIFO,
332};
333
Seth Forsheee041f652012-11-15 08:07:56 -0600334/* Mapping of tx fifos to ieee80211 AC numbers */
335static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
336 [TX_AC_BK_FIFO] = IEEE80211_AC_BK,
337 [TX_AC_BE_FIFO] = IEEE80211_AC_BE,
338 [TX_AC_VI_FIFO] = IEEE80211_AC_VI,
339 [TX_AC_VO_FIFO] = IEEE80211_AC_VO,
340};
341
Seth Forshee32d0f122012-11-15 08:07:55 -0600342static u8 brcms_ac_to_fifo(u8 ac)
343{
344 if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
345 return TX_AC_BE_FIFO;
346 return ac_to_fifo_mapping[ac];
347}
348
Seth Forsheee041f652012-11-15 08:07:56 -0600349static u8 brcms_fifo_to_ac(u8 fifo)
350{
351 if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
352 return IEEE80211_AC_BE;
353 return fifo_to_ac_mapping[fifo];
354}
355
Alwin Beukers73ffc2f2011-10-18 14:02:57 +0200356/* Find basic rate for a given rate */
357static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
358{
359 if (is_mcs_rate(rspec))
360 return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
361 .leg_ofdm];
362 return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
363}
364
365static u16 frametype(u32 rspec, u8 mimoframe)
366{
367 if (is_mcs_rate(rspec))
368 return mimoframe;
369 return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
370}
371
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200372/* currently the best mechanism for determining SIFS is the band in use */
373static u16 get_sifs(struct brcms_band *band)
374{
375 return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
376 BPHY_SIFS_TIME;
377}
378
379/*
380 * Detect Card removed.
381 * Even checking an sbconfig register read will not false trigger when the core
382 * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
383 * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
384 * reg with fixed 0/1 pattern (some platforms return all 0).
385 * If clocks are present, call the sb routine which will figure out if the
386 * device is removed.
387 */
388static bool brcms_deviceremoved(struct brcms_c_info *wlc)
389{
Arend van Spriel16d28122011-12-08 15:06:51 -0800390 u32 macctrl;
391
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200392 if (!wlc->hw->clk)
393 return ai_deviceremoved(wlc->hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -0800394 macctrl = bcma_read32(wlc->hw->d11core,
395 D11REGOFFS(maccontrol));
396 return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200397}
398
399/* sum the individual fifo tx pending packet counts */
Seth Forsheee041f652012-11-15 08:07:56 -0600400static int brcms_txpktpendtot(struct brcms_c_info *wlc)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200401{
Seth Forsheee041f652012-11-15 08:07:56 -0600402 int i;
403 int pending = 0;
404
405 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
406 if (wlc->hw->di[i])
407 pending += dma_txpending(wlc->hw->di[i]);
408 return pending;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200409}
410
411static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
412{
413 return wlc->pub->_nbands > 1 && !wlc->bandlocked;
414}
415
416static int brcms_chspec_bw(u16 chanspec)
417{
418 if (CHSPEC_IS40(chanspec))
419 return BRCMS_40_MHZ;
420 if (CHSPEC_IS20(chanspec))
421 return BRCMS_20_MHZ;
422
423 return BRCMS_10_MHZ;
424}
425
Arend van Spriel5b435de2011-10-05 13:19:03 +0200426static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
427{
428 if (cfg == NULL)
429 return;
430
431 kfree(cfg->current_bss);
432 kfree(cfg);
433}
434
435static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
436{
437 if (wlc == NULL)
438 return;
439
440 brcms_c_bsscfg_mfree(wlc->bsscfg);
441 kfree(wlc->pub);
442 kfree(wlc->modulecb);
443 kfree(wlc->default_bss);
444 kfree(wlc->protection);
445 kfree(wlc->stf);
446 kfree(wlc->bandstate[0]);
447 kfree(wlc->corestate->macstat_snapshot);
448 kfree(wlc->corestate);
449 kfree(wlc->hw->bandstate[0]);
450 kfree(wlc->hw);
451
452 /* free the wlc */
453 kfree(wlc);
454 wlc = NULL;
455}
456
457static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
458{
459 struct brcms_bss_cfg *cfg;
460
461 cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
462 if (cfg == NULL)
463 goto fail;
464
465 cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
466 if (cfg->current_bss == NULL)
467 goto fail;
468
469 return cfg;
470
471 fail:
472 brcms_c_bsscfg_mfree(cfg);
473 return NULL;
474}
475
476static struct brcms_c_info *
477brcms_c_attach_malloc(uint unit, uint *err, uint devid)
478{
479 struct brcms_c_info *wlc;
480
481 wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
482 if (wlc == NULL) {
483 *err = 1002;
484 goto fail;
485 }
486
487 /* allocate struct brcms_c_pub state structure */
488 wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
489 if (wlc->pub == NULL) {
490 *err = 1003;
491 goto fail;
492 }
493 wlc->pub->wlc = wlc;
494
495 /* allocate struct brcms_hardware state structure */
496
497 wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
498 if (wlc->hw == NULL) {
499 *err = 1005;
500 goto fail;
501 }
502 wlc->hw->wlc = wlc;
503
504 wlc->hw->bandstate[0] =
505 kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
506 if (wlc->hw->bandstate[0] == NULL) {
507 *err = 1006;
508 goto fail;
509 } else {
510 int i;
511
512 for (i = 1; i < MAXBANDS; i++)
513 wlc->hw->bandstate[i] = (struct brcms_hw_band *)
514 ((unsigned long)wlc->hw->bandstate[0] +
515 (sizeof(struct brcms_hw_band) * i));
516 }
517
518 wlc->modulecb =
519 kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
520 if (wlc->modulecb == NULL) {
521 *err = 1009;
522 goto fail;
523 }
524
525 wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
526 if (wlc->default_bss == NULL) {
527 *err = 1010;
528 goto fail;
529 }
530
531 wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
532 if (wlc->bsscfg == NULL) {
533 *err = 1011;
534 goto fail;
535 }
536
537 wlc->protection = kzalloc(sizeof(struct brcms_protection),
538 GFP_ATOMIC);
539 if (wlc->protection == NULL) {
540 *err = 1016;
541 goto fail;
542 }
543
544 wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
545 if (wlc->stf == NULL) {
546 *err = 1017;
547 goto fail;
548 }
549
550 wlc->bandstate[0] =
551 kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
552 if (wlc->bandstate[0] == NULL) {
553 *err = 1025;
554 goto fail;
555 } else {
556 int i;
557
558 for (i = 1; i < MAXBANDS; i++)
559 wlc->bandstate[i] = (struct brcms_band *)
560 ((unsigned long)wlc->bandstate[0]
561 + (sizeof(struct brcms_band)*i));
562 }
563
564 wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
565 if (wlc->corestate == NULL) {
566 *err = 1026;
567 goto fail;
568 }
569
570 wlc->corestate->macstat_snapshot =
571 kzalloc(sizeof(struct macstat), GFP_ATOMIC);
572 if (wlc->corestate->macstat_snapshot == NULL) {
573 *err = 1027;
574 goto fail;
575 }
576
577 return wlc;
578
579 fail:
580 brcms_c_detach_mfree(wlc);
581 return NULL;
582}
583
584/*
585 * Update the slot timing for standard 11b/g (20us slots)
586 * or shortslot 11g (9us slots)
587 * The PSM needs to be suspended for this call.
588 */
589static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
590 bool shortslot)
591{
Arend van Spriel16d28122011-12-08 15:06:51 -0800592 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200593
594 if (shortslot) {
595 /* 11g short slot: 11a timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800596 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200597 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
598 } else {
599 /* 11g long slot: 11b timing */
Arend van Spriel16d28122011-12-08 15:06:51 -0800600 bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200601 brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
602 }
603}
604
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200605/*
606 * calculate frame duration of a given rate and length, return
607 * time in usec unit
608 */
Arend van Spriel094b1992011-10-18 14:03:07 +0200609static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
610 u8 preamble_type, uint mac_len)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200611{
612 uint nsyms, dur = 0, Ndps, kNdps;
613 uint rate = rspec2rate(ratespec);
614
615 if (rate == 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600616 brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200617 wlc->pub->unit);
618 rate = BRCM_RATE_1M;
619 }
620
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200621 if (is_mcs_rate(ratespec)) {
622 uint mcs = ratespec & RSPEC_RATE_MASK;
623 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
624
625 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
626 if (preamble_type == BRCMS_MM_PREAMBLE)
627 dur += PREN_MM_EXT;
628 /* 1000Ndbps = kbps * 4 */
629 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
630 rspec_issgi(ratespec)) * 4;
631
632 if (rspec_stc(ratespec) == 0)
633 nsyms =
634 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
635 APHY_TAIL_NBITS) * 1000, kNdps);
636 else
637 /* STBC needs to have even number of symbols */
638 nsyms =
639 2 *
640 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
641 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
642
643 dur += APHY_SYMBOL_TIME * nsyms;
644 if (wlc->band->bandtype == BRCM_BAND_2G)
645 dur += DOT11_OFDM_SIGNAL_EXTENSION;
646 } else if (is_ofdm_rate(rate)) {
647 dur = APHY_PREAMBLE_TIME;
648 dur += APHY_SIGNAL_TIME;
649 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
650 Ndps = rate * 2;
651 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
652 nsyms =
653 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
654 Ndps);
655 dur += APHY_SYMBOL_TIME * nsyms;
656 if (wlc->band->bandtype == BRCM_BAND_2G)
657 dur += DOT11_OFDM_SIGNAL_EXTENSION;
658 } else {
659 /*
660 * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
661 * will divide out
662 */
663 mac_len = mac_len * 8 * 2;
664 /* calc ceiling of bits/rate = microseconds of air time */
665 dur = (mac_len + rate - 1) / rate;
666 if (preamble_type & BRCMS_SHORT_PREAMBLE)
667 dur += BPHY_PLCP_SHORT_TIME;
668 else
669 dur += BPHY_PLCP_TIME;
670 }
671 return dur;
672}
673
Arend van Spriel5b435de2011-10-05 13:19:03 +0200674static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
675 const struct d11init *inits)
676{
Arend van Spriel16d28122011-12-08 15:06:51 -0800677 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200678 int i;
Arend van Spriel16d28122011-12-08 15:06:51 -0800679 uint offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200680 u16 size;
681 u32 value;
682
Seth Forsheeb353dda2012-11-15 08:08:03 -0600683 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200684
Arend van Spriel5b435de2011-10-05 13:19:03 +0200685 for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
686 size = le16_to_cpu(inits[i].size);
Arend van Spriel16d28122011-12-08 15:06:51 -0800687 offset = le16_to_cpu(inits[i].addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200688 value = le32_to_cpu(inits[i].value);
689 if (size == 2)
Arend van Spriel16d28122011-12-08 15:06:51 -0800690 bcma_write16(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200691 else if (size == 4)
Arend van Spriel16d28122011-12-08 15:06:51 -0800692 bcma_write32(core, offset, value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200693 else
694 break;
695 }
696}
697
698static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
699{
700 u8 idx;
701 u16 addr[] = {
702 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
703 M_HOST_FLAGS5
704 };
705
706 for (idx = 0; idx < MHFMAX; idx++)
707 brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
708}
709
710static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
711{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200712 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
713
714 /* init microcode host flags */
715 brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
716
717 /* do band-specific ucode IHR, SHM, and SCR inits */
Hauke Mehrtens6f80f012012-12-07 00:35:53 +0100718 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200719 if (BRCMS_ISNPHY(wlc_hw->band))
720 brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
721 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600722 brcms_err(wlc_hw->d11core,
723 "%s: wl%d: unsupported phy in corerev %d\n",
724 __func__, wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200725 wlc_hw->corerev);
726 } else {
727 if (D11REV_IS(wlc_hw->corerev, 24)) {
728 if (BRCMS_ISLCNPHY(wlc_hw->band))
729 brcms_c_write_inits(wlc_hw,
730 ucode->d11lcn0bsinitvals24);
731 else
Seth Forsheeb353dda2012-11-15 08:08:03 -0600732 brcms_err(wlc_hw->d11core,
733 "%s: wl%d: unsupported phy in core rev %d\n",
734 __func__, wlc_hw->unit,
735 wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600737 brcms_err(wlc_hw->d11core,
738 "%s: wl%d: unsupported corerev %d\n",
739 __func__, wlc_hw->unit, wlc_hw->corerev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200740 }
741 }
742}
743
Arend van Spriela8779e42011-12-08 15:06:58 -0800744static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
745{
746 struct bcma_device *core = wlc_hw->d11core;
747 u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
748
749 bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
750}
751
Arend van Spriel5b435de2011-10-05 13:19:03 +0200752static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
753{
Seth Forsheeb353dda2012-11-15 08:08:03 -0600754 brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200755
756 wlc_hw->phyclk = clk;
757
758 if (OFF == clk) { /* clear gmode bit, put phy into reset */
759
Arend van Spriela8779e42011-12-08 15:06:58 -0800760 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
761 (SICF_PRST | SICF_FGC));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200762 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800763 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 udelay(1);
765
766 } else { /* take phy out of reset */
767
Arend van Spriela8779e42011-12-08 15:06:58 -0800768 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200769 udelay(1);
Arend van Spriela8779e42011-12-08 15:06:58 -0800770 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771 udelay(1);
772
773 }
774}
775
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200776/* low-level band switch utility routine */
777static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
778{
Seth Forshee913911f2012-11-15 08:08:04 -0600779 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
780 bandunit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200781
782 wlc_hw->band = wlc_hw->bandstate[bandunit];
783
784 /*
785 * BMAC_NOTE:
786 * until we eliminate need for wlc->band refs in low level code
787 */
788 wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
789
790 /* set gmode core flag */
Arend van Spriela8779e42011-12-08 15:06:58 -0800791 if (wlc_hw->sbclk && !wlc_hw->noreset) {
792 u32 gmode = 0;
793
794 if (bandunit == 0)
795 gmode = SICF_GMODE;
796
797 brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
798 }
Alwin Beukers94bdc2a2011-10-12 20:51:13 +0200799}
800
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801/* switch to new band but leave it inactive */
802static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
803{
804 struct brcms_hardware *wlc_hw = wlc->hw;
805 u32 macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -0800806 u32 macctrl;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200807
Seth Forshee913911f2012-11-15 08:08:04 -0600808 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel16d28122011-12-08 15:06:51 -0800809 macctrl = bcma_read32(wlc_hw->d11core,
810 D11REGOFFS(maccontrol));
811 WARN_ON((macctrl & MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812
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{
Seth Forsheee041f652012-11-15 08:07:56 -0600830 struct sk_buff *p = NULL;
831 uint queue = NFIFO;
832 struct dma_pub *dma = NULL;
Seth Forsheecdf43522012-11-15 08:08:09 -0600833 struct d11txh *txh = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200834 struct scb *scb = NULL;
835 bool free_pdu;
836 int tx_rts, tx_frame_count, tx_rts_count;
837 uint totlen, supr_status;
838 bool lastframe;
839 struct ieee80211_hdr *h;
840 u16 mcl;
841 struct ieee80211_tx_info *tx_info;
842 struct ieee80211_tx_rate *txrate;
843 int i;
Seth Forsheee041f652012-11-15 08:07:56 -0600844 bool fatal = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200845
Seth Forsheecdf43522012-11-15 08:08:09 -0600846 trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
847 txs->frameid, txs->status, txs->lasttxtime,
848 txs->sequence, txs->phyerr, txs->ackphyrxsh);
849
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850 /* discard intermediate indications for ucode with one legitimate case:
851 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
852 * but the subsequent tx of DATA failed. so it will start rts/cts
853 * from the beginning (resetting the rts transmission count)
854 */
855 if (!(txs->status & TX_STATUS_AMPDU)
856 && (txs->status & TX_STATUS_INTERMEDIATE)) {
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600857 brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600858 fatal = false;
859 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860 }
861
862 queue = txs->frameid & TXFID_QUEUE_MASK;
Seth Forsheecdf43522012-11-15 08:08:09 -0600863 if (queue >= NFIFO) {
864 brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
Seth Forsheee041f652012-11-15 08:07:56 -0600865 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600866 }
Seth Forsheee041f652012-11-15 08:07:56 -0600867
868 dma = wlc->hw->di[queue];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200869
870 p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
Seth Forsheecdf43522012-11-15 08:08:09 -0600871 if (p == NULL) {
872 brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600873 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600874 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875
876 txh = (struct d11txh *) (p->data);
877 mcl = le16_to_cpu(txh->MacTxControlLow);
878
Seth Forsheecdf43522012-11-15 08:08:09 -0600879 if (txs->phyerr)
880 brcms_err(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
881 txs->phyerr, txh->MainRates);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200882
Seth Forsheecdf43522012-11-15 08:08:09 -0600883 if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
884 brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
Seth Forsheee041f652012-11-15 08:07:56 -0600885 goto out;
Seth Forsheecdf43522012-11-15 08:08:09 -0600886 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 tx_info = IEEE80211_SKB_CB(p);
888 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
889
Thomas Huehn644e8c02012-07-10 14:01:37 +0200890 if (tx_info->rate_driver_data[0])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200891 scb = &wlc->pri_scb;
892
893 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
894 brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
Seth Forsheee041f652012-11-15 08:07:56 -0600895 fatal = false;
896 goto out;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897 }
898
Seth Forsheecdf43522012-11-15 08:08:09 -0600899 /*
900 * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
901 * frames; this traces them for the rest.
902 */
903 trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
904
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905 supr_status = txs->status & TX_STATUS_SUPR_MASK;
Seth Forsheecdf43522012-11-15 08:08:09 -0600906 if (supr_status == TX_STATUS_SUPR_BADCH) {
907 unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600908 brcms_dbg_tx(wlc->hw->d11core,
Seth Forsheecdf43522012-11-15 08:08:09 -0600909 "Pkt tx suppressed, dest chan %u, current %d\n",
910 (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
Seth Forshee5ce58bb2012-11-15 08:08:05 -0600911 CHSPEC_CHANNEL(wlc->default_bss->chanspec));
Seth Forsheecdf43522012-11-15 08:08:09 -0600912 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913
914 tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
915 tx_frame_count =
916 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
917 tx_rts_count =
918 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
919
920 lastframe = !ieee80211_has_morefrags(h->frame_control);
921
922 if (!lastframe) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600923 brcms_err(wlc->hw->d11core, "Not last frame!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200924 } else {
925 /*
926 * Set information to be consumed by Minstrel ht.
927 *
928 * The "fallback limit" is the number of tx attempts a given
929 * MPDU is sent at the "primary" rate. Tx attempts beyond that
930 * limit are sent at the "secondary" rate.
931 * A 'short frame' does not exceed RTS treshold.
932 */
933 u16 sfbl, /* Short Frame Rate Fallback Limit */
934 lfbl, /* Long Frame Rate Fallback Limit */
935 fbl;
936
Arend van Sprielb7eec422011-11-10 20:30:18 +0100937 if (queue < IEEE80211_NUM_ACS) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200938 sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
939 EDCF_SFB);
940 lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
941 EDCF_LFB);
942 } else {
943 sfbl = wlc->SFBL;
944 lfbl = wlc->LFBL;
945 }
946
947 txrate = tx_info->status.rates;
948 if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
949 fbl = lfbl;
950 else
951 fbl = sfbl;
952
953 ieee80211_tx_info_clear_status(tx_info);
954
955 if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
956 /*
957 * rate selection requested a fallback rate
958 * and we used it
959 */
960 txrate[0].count = fbl;
961 txrate[1].count = tx_frame_count - fbl;
962 } else {
963 /*
964 * rate selection did not request fallback rate, or
965 * we didn't need it
966 */
967 txrate[0].count = tx_frame_count;
968 /*
969 * rc80211_minstrel.c:minstrel_tx_status() expects
970 * unused rates to be marked with idx = -1
971 */
972 txrate[1].idx = -1;
973 txrate[1].count = 0;
974 }
975
976 /* clear the rest of the rates */
977 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
978 txrate[i].idx = -1;
979 txrate[i].count = 0;
980 }
981
982 if (txs->status & TX_STATUS_ACK_RCV)
983 tx_info->flags |= IEEE80211_TX_STAT_ACK;
984 }
985
Arend van Sprielad4d71f2011-11-10 20:30:26 +0100986 totlen = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200987 free_pdu = true;
988
Arend van Spriel5b435de2011-10-05 13:19:03 +0200989 if (lastframe) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200990 /* remove PLCP & Broadcom tx descriptor header */
991 skb_pull(p, D11_PHY_HDR_LEN);
992 skb_pull(p, D11_TXH_LEN);
993 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
994 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600995 brcms_err(wlc->hw->d11core,
996 "%s: Not last frame => not calling tx_status\n",
997 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200998 }
999
Seth Forsheee041f652012-11-15 08:07:56 -06001000 fatal = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001001
Seth Forsheee041f652012-11-15 08:07:56 -06001002 out:
Seth Forsheecdf43522012-11-15 08:08:09 -06001003 if (fatal) {
1004 if (txh)
1005 trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
1006 sizeof(*txh));
1007 if (p)
1008 brcmu_pkt_buf_free_skb(p);
1009 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001010
Seth Forsheee041f652012-11-15 08:07:56 -06001011 if (dma && queue < NFIFO) {
1012 u16 ac_queue = brcms_fifo_to_ac(queue);
1013 if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
1014 ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
1015 ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
1016 dma_kick_tx(dma);
1017 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001018
Seth Forsheee041f652012-11-15 08:07:56 -06001019 return fatal;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001020}
1021
1022/* process tx completion events in BMAC
1023 * Return true if more tx status need to be processed. false otherwise.
1024 */
1025static bool
1026brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
1027{
Arend van Spriel16d28122011-12-08 15:06:51 -08001028 struct bcma_device *core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001029 struct tx_status txstatus, *txs;
1030 u32 s1, s2;
1031 uint n = 0;
1032 /*
1033 * Param 'max_tx_num' indicates max. # tx status to process before
1034 * break out.
1035 */
1036 uint max_tx_num = bound ? TXSBND : -1;
1037
Arend van Spriel5b435de2011-10-05 13:19:03 +02001038 txs = &txstatus;
Arend van Spriel16d28122011-12-08 15:06:51 -08001039 core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001040 *fatal = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001041
Arend van Spriel0e33e482013-01-22 22:47:40 +01001042 while (n < max_tx_num) {
1043 s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001044 if (s1 == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001045 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
1046 __func__);
Piotr Haber57fe5042012-11-28 21:44:07 +01001047 *fatal = true;
1048 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001049 }
Arend van Spriel0e33e482013-01-22 22:47:40 +01001050 /* only process when valid */
1051 if (!(s1 & TXS_V))
1052 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001053
Arend van Spriel0e33e482013-01-22 22:47:40 +01001054 s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001055 txs->status = s1 & TXS_STATUS_MASK;
1056 txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
1057 txs->sequence = s2 & TXS_SEQ_MASK;
1058 txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
1059 txs->lasttxtime = 0;
1060
1061 *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
Arend van Spriel0e33e482013-01-22 22:47:40 +01001062 if (*fatal == true)
1063 return false;
Piotr Haber57fe5042012-11-28 21:44:07 +01001064 n++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001065 }
1066
Arend van Spriel0e33e482013-01-22 22:47:40 +01001067 return n >= max_tx_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001068}
1069
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001070static void brcms_c_tbtt(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001071{
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02001072 if (!wlc->bsscfg->BSS)
1073 /*
1074 * DirFrmQ is now valid...defer setting until end
1075 * of ATIM window
1076 */
1077 wlc->qvalid |= MCMD_DIRFRMQVAL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001078}
1079
1080/* set initial host flags value */
1081static void
1082brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
1083{
1084 struct brcms_hardware *wlc_hw = wlc->hw;
1085
1086 memset(mhfs, 0, MHFMAX * sizeof(u16));
1087
1088 mhfs[MHF2] |= mhf2_init;
1089
1090 /* prohibit use of slowclock on multifunction boards */
1091 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
1092 mhfs[MHF1] |= MHF1_FORCEFASTCLK;
1093
1094 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
1095 mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
1096 mhfs[MHF1] |= MHF1_IQSWAP_WAR;
1097 }
1098}
1099
Arend van Spriele81da652011-12-08 15:06:53 -08001100static uint
1101dmareg(uint direction, uint fifonum)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001102{
1103 if (direction == DMA_TX)
Arend van Spriele81da652011-12-08 15:06:53 -08001104 return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
1105 return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001106}
1107
1108static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
1109{
1110 uint i;
1111 char name[8];
1112 /*
1113 * ucode host flag 2 needed for pio mode, independent of band and fifo
1114 */
1115 u16 pio_mhf2 = 0;
1116 struct brcms_hardware *wlc_hw = wlc->hw;
1117 uint unit = wlc_hw->unit;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001118
1119 /* name and offsets for dma_attach */
1120 snprintf(name, sizeof(name), "wl%d", unit);
1121
1122 if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
1123 int dma_attach_err = 0;
1124
1125 /*
1126 * FIFO 0
1127 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
1128 * RX: RX_FIFO (RX data packets)
1129 */
Seth Forsheee041f652012-11-15 08:07:56 -06001130 wlc_hw->di[0] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001131 (wme ? dmareg(DMA_TX, 0) : 0),
1132 dmareg(DMA_RX, 0),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001133 (wme ? NTXD : 0), NRXD,
1134 RXBUFSZ, -1, NRXBUFPOST,
Seth Forshee90123e02012-11-15 08:08:07 -06001135 BRCMS_HWRXOFF);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001136 dma_attach_err |= (NULL == wlc_hw->di[0]);
1137
1138 /*
1139 * FIFO 1
1140 * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
1141 * (legacy) TX_DATA_FIFO (TX data packets)
1142 * RX: UNUSED
1143 */
Seth Forsheee041f652012-11-15 08:07:56 -06001144 wlc_hw->di[1] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001145 dmareg(DMA_TX, 1), 0,
Seth Forshee90123e02012-11-15 08:08:07 -06001146 NTXD, 0, 0, -1, 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001147 dma_attach_err |= (NULL == wlc_hw->di[1]);
1148
1149 /*
1150 * FIFO 2
1151 * TX: TX_AC_VI_FIFO (TX AC Video data packets)
1152 * RX: UNUSED
1153 */
Seth Forsheee041f652012-11-15 08:07:56 -06001154 wlc_hw->di[2] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001155 dmareg(DMA_TX, 2), 0,
Seth Forshee90123e02012-11-15 08:08:07 -06001156 NTXD, 0, 0, -1, 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001157 dma_attach_err |= (NULL == wlc_hw->di[2]);
1158 /*
1159 * FIFO 3
1160 * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
1161 * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
1162 */
Seth Forsheee041f652012-11-15 08:07:56 -06001163 wlc_hw->di[3] = dma_attach(name, wlc,
Arend van Spriele81da652011-12-08 15:06:53 -08001164 dmareg(DMA_TX, 3),
1165 0, NTXD, 0, 0, -1,
Seth Forshee90123e02012-11-15 08:08:07 -06001166 0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001167 dma_attach_err |= (NULL == wlc_hw->di[3]);
1168/* Cleaner to leave this as if with AP defined */
1169
1170 if (dma_attach_err) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001171 brcms_err(wlc_hw->d11core,
1172 "wl%d: wlc_attach: dma_attach failed\n",
1173 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001174 return false;
1175 }
1176
1177 /* get pointer to dma engine tx flow control variable */
1178 for (i = 0; i < NFIFO; i++)
1179 if (wlc_hw->di[i])
1180 wlc_hw->txavail[i] =
1181 (uint *) dma_getvar(wlc_hw->di[i],
1182 "&txavail");
1183 }
1184
1185 /* initial ucode host flags */
1186 brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
1187
1188 return true;
1189}
1190
1191static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
1192{
1193 uint j;
1194
1195 for (j = 0; j < NFIFO; j++) {
1196 if (wlc_hw->di[j]) {
1197 dma_detach(wlc_hw->di[j]);
1198 wlc_hw->di[j] = NULL;
1199 }
1200 }
1201}
1202
1203/*
1204 * Initialize brcms_c_info default values ...
1205 * may get overrides later in this function
1206 * BMAC_NOTES, move low out and resolve the dangling ones
1207 */
1208static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
1209{
1210 struct brcms_c_info *wlc = wlc_hw->wlc;
1211
1212 /* set default sw macintmask value */
1213 wlc->defmacintmask = DEF_MACINTMASK;
1214
1215 /* various 802.11g modes */
1216 wlc_hw->shortslot = false;
1217
1218 wlc_hw->SFBL = RETRY_SHORT_FB;
1219 wlc_hw->LFBL = RETRY_LONG_FB;
1220
1221 /* default mac retry limits */
1222 wlc_hw->SRL = RETRY_SHORT_DEF;
1223 wlc_hw->LRL = RETRY_LONG_DEF;
1224 wlc_hw->chanspec = ch20mhz_chspec(1);
1225}
1226
1227static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
1228{
1229 /* delay before first read of ucode state */
1230 udelay(40);
1231
1232 /* wait until ucode is no longer asleep */
1233 SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
1234 DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
1235}
1236
1237/* control chip clock to save power, enable dynamic clock or force fast clock */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001238static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001239{
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001240 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001241 /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
1242 * on backplane, but mac core will still run on ALP(not HT) when
1243 * it enters powersave mode, which means the FCA bit may not be
1244 * set. Should wakeup mac if driver wants it to run on HT.
1245 */
1246
1247 if (wlc_hw->clk) {
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001248 if (mode == BCMA_CLKMODE_FAST) {
Arend van Spriel16d28122011-12-08 15:06:51 -08001249 bcma_set32(wlc_hw->d11core,
1250 D11REGOFFS(clk_ctl_st),
1251 CCS_FORCEHT);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001252
1253 udelay(64);
1254
Arend van Spriel16d28122011-12-08 15:06:51 -08001255 SPINWAIT(
1256 ((bcma_read32(wlc_hw->d11core,
1257 D11REGOFFS(clk_ctl_st)) &
1258 CCS_HTAVAIL) == 0),
1259 PMU_MAX_TRANSITION_DLY);
1260 WARN_ON(!(bcma_read32(wlc_hw->d11core,
1261 D11REGOFFS(clk_ctl_st)) &
1262 CCS_HTAVAIL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001263 } else {
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001264 if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
Arend van Spriel16d28122011-12-08 15:06:51 -08001265 (bcma_read32(wlc_hw->d11core,
1266 D11REGOFFS(clk_ctl_st)) &
1267 (CCS_FORCEHT | CCS_HTAREQ)))
1268 SPINWAIT(
1269 ((bcma_read32(wlc_hw->d11core,
1270 offsetof(struct d11regs,
1271 clk_ctl_st)) &
1272 CCS_HTAVAIL) == 0),
1273 PMU_MAX_TRANSITION_DLY);
1274 bcma_mask32(wlc_hw->d11core,
1275 D11REGOFFS(clk_ctl_st),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001276 ~CCS_FORCEHT);
1277 }
1278 }
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001279 wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001280 } else {
1281
1282 /* old chips w/o PMU, force HT through cc,
1283 * then use FCA to verify mac is running fast clock
1284 */
1285
1286 wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
1287
1288 /* check fast clock is available (if core is not in reset) */
1289 if (wlc_hw->forcefastclk && wlc_hw->clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001290 WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02001291 SISF_FCLKA));
1292
1293 /*
1294 * keep the ucode wake bit on if forcefastclk is on since we
1295 * do not want ucode to put us back to slow clock when it dozes
1296 * for PM mode. Code below matches the wake override bit with
1297 * current forcefastclk state. Only setting bit in wake_override
1298 * instead of waking ucode immediately since old code had this
1299 * behavior. Older code set wlc->forcefastclk but only had the
1300 * wake happen if the wakup_ucode work (protected by an up
1301 * check) was executed just below.
1302 */
1303 if (wlc_hw->forcefastclk)
1304 mboolset(wlc_hw->wake_override,
1305 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1306 else
1307 mboolclr(wlc_hw->wake_override,
1308 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1309 }
1310}
1311
1312/* set or clear ucode host flag bits
1313 * it has an optimization for no-change write
1314 * it only writes through shared memory when the core has clock;
1315 * pre-CLK changes should use wlc_write_mhf to get around the optimization
1316 *
1317 *
1318 * bands values are: BRCM_BAND_AUTO <--- Current band only
1319 * BRCM_BAND_5G <--- 5G band only
1320 * BRCM_BAND_2G <--- 2G band only
1321 * BRCM_BAND_ALL <--- All bands
1322 */
1323void
1324brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
1325 int bands)
1326{
1327 u16 save;
1328 u16 addr[MHFMAX] = {
1329 M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
1330 M_HOST_FLAGS5
1331 };
1332 struct brcms_hw_band *band;
1333
1334 if ((val & ~mask) || idx >= MHFMAX)
1335 return; /* error condition */
1336
1337 switch (bands) {
1338 /* Current band only or all bands,
1339 * then set the band to current band
1340 */
1341 case BRCM_BAND_AUTO:
1342 case BRCM_BAND_ALL:
1343 band = wlc_hw->band;
1344 break;
1345 case BRCM_BAND_5G:
1346 band = wlc_hw->bandstate[BAND_5G_INDEX];
1347 break;
1348 case BRCM_BAND_2G:
1349 band = wlc_hw->bandstate[BAND_2G_INDEX];
1350 break;
1351 default:
1352 band = NULL; /* error condition */
1353 }
1354
1355 if (band) {
1356 save = band->mhfs[idx];
1357 band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
1358
1359 /* optimization: only write through if changed, and
1360 * changed band is the current band
1361 */
1362 if (wlc_hw->clk && (band->mhfs[idx] != save)
1363 && (band == wlc_hw->band))
1364 brcms_b_write_shm(wlc_hw, addr[idx],
1365 (u16) band->mhfs[idx]);
1366 }
1367
1368 if (bands == BRCM_BAND_ALL) {
1369 wlc_hw->bandstate[0]->mhfs[idx] =
1370 (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
1371 wlc_hw->bandstate[1]->mhfs[idx] =
1372 (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
1373 }
1374}
1375
1376/* set the maccontrol register to desired reset state and
1377 * initialize the sw cache of the register
1378 */
1379static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
1380{
1381 /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
1382 wlc_hw->maccontrol = 0;
1383 wlc_hw->suspended_fifos = 0;
1384 wlc_hw->wake_override = 0;
1385 wlc_hw->mute_override = 0;
1386 brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
1387}
1388
1389/*
1390 * write the software state of maccontrol and
1391 * overrides to the maccontrol register
1392 */
1393static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
1394{
1395 u32 maccontrol = wlc_hw->maccontrol;
1396
1397 /* OR in the wake bit if overridden */
1398 if (wlc_hw->wake_override)
1399 maccontrol |= MCTL_WAKE;
1400
1401 /* set AP and INFRA bits for mute if needed */
1402 if (wlc_hw->mute_override) {
1403 maccontrol &= ~(MCTL_AP);
1404 maccontrol |= MCTL_INFRA;
1405 }
1406
Arend van Spriel16d28122011-12-08 15:06:51 -08001407 bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
1408 maccontrol);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001409}
1410
1411/* set or clear maccontrol bits */
1412void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
1413{
1414 u32 maccontrol;
1415 u32 new_maccontrol;
1416
1417 if (val & ~mask)
1418 return; /* error condition */
1419 maccontrol = wlc_hw->maccontrol;
1420 new_maccontrol = (maccontrol & ~mask) | val;
1421
1422 /* if the new maccontrol value is the same as the old, nothing to do */
1423 if (new_maccontrol == maccontrol)
1424 return;
1425
1426 /* something changed, cache the new value */
1427 wlc_hw->maccontrol = new_maccontrol;
1428
1429 /* write the new values with overrides applied */
1430 brcms_c_mctrl_write(wlc_hw);
1431}
1432
1433void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
1434 u32 override_bit)
1435{
1436 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
1437 mboolset(wlc_hw->wake_override, override_bit);
1438 return;
1439 }
1440
1441 mboolset(wlc_hw->wake_override, override_bit);
1442
1443 brcms_c_mctrl_write(wlc_hw);
1444 brcms_b_wait_for_wake(wlc_hw);
1445}
1446
1447void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
1448 u32 override_bit)
1449{
1450 mboolclr(wlc_hw->wake_override, override_bit);
1451
1452 if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
1453 return;
1454
1455 brcms_c_mctrl_write(wlc_hw);
1456}
1457
1458/* When driver needs ucode to stop beaconing, it has to make sure that
1459 * MCTL_AP is clear and MCTL_INFRA is set
1460 * Mode MCTL_AP MCTL_INFRA
1461 * AP 1 1
1462 * STA 0 1 <--- This will ensure no beacons
1463 * IBSS 0 0
1464 */
1465static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
1466{
1467 wlc_hw->mute_override = 1;
1468
1469 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1470 * override, then there is no change to write
1471 */
1472 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1473 return;
1474
1475 brcms_c_mctrl_write(wlc_hw);
1476}
1477
1478/* Clear the override on AP and INFRA bits */
1479static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
1480{
1481 if (wlc_hw->mute_override == 0)
1482 return;
1483
1484 wlc_hw->mute_override = 0;
1485
1486 /* if maccontrol already has AP == 0 and INFRA == 1 without this
1487 * override, then there is no change to write
1488 */
1489 if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1490 return;
1491
1492 brcms_c_mctrl_write(wlc_hw);
1493}
1494
1495/*
1496 * Write a MAC address to the given match reg offset in the RXE match engine.
1497 */
1498static void
1499brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
1500 const u8 *addr)
1501{
Arend van Spriel16d28122011-12-08 15:06:51 -08001502 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001503 u16 mac_l;
1504 u16 mac_m;
1505 u16 mac_h;
1506
Seth Forshee5ce58bb2012-11-15 08:08:05 -06001507 brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001508
Arend van Spriel5b435de2011-10-05 13:19:03 +02001509 mac_l = addr[0] | (addr[1] << 8);
1510 mac_m = addr[2] | (addr[3] << 8);
1511 mac_h = addr[4] | (addr[5] << 8);
1512
1513 /* enter the MAC addr into the RXE match registers */
Arend van Spriel16d28122011-12-08 15:06:51 -08001514 bcma_write16(core, D11REGOFFS(rcm_ctl),
1515 RCM_INC_DATA | match_reg_offset);
1516 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
1517 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
1518 bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001519}
1520
1521void
1522brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
1523 void *buf)
1524{
Arend van Spriel16d28122011-12-08 15:06:51 -08001525 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 u32 word;
1527 __le32 word_le;
1528 __be32 word_be;
1529 bool be_bit;
Seth Forsheeb353dda2012-11-15 08:08:03 -06001530 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001531
Arend van Spriel16d28122011-12-08 15:06:51 -08001532 bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533
1534 /* if MCTL_BIGEND bit set in mac control register,
1535 * the chip swaps data in fifo, as well as data in
1536 * template ram
1537 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001538 be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001539
1540 while (len > 0) {
1541 memcpy(&word, buf, sizeof(u32));
1542
1543 if (be_bit) {
1544 word_be = cpu_to_be32(word);
1545 word = *(u32 *)&word_be;
1546 } else {
1547 word_le = cpu_to_le32(word);
1548 word = *(u32 *)&word_le;
1549 }
1550
Arend van Spriel16d28122011-12-08 15:06:51 -08001551 bcma_write32(core, D11REGOFFS(tplatewrdata), word);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001552
1553 buf = (u8 *) buf + sizeof(u32);
1554 len -= sizeof(u32);
1555 }
1556}
1557
1558static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
1559{
1560 wlc_hw->band->CWmin = newmin;
1561
Arend van Spriel16d28122011-12-08 15:06:51 -08001562 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1563 OBJADDR_SCR_SEL | S_DOT11_CWMIN);
1564 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1565 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001566}
1567
1568static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
1569{
1570 wlc_hw->band->CWmax = newmax;
1571
Arend van Spriel16d28122011-12-08 15:06:51 -08001572 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1573 OBJADDR_SCR_SEL | S_DOT11_CWMAX);
1574 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1575 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001576}
1577
1578void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
1579{
1580 bool fastclk;
1581
1582 /* request FAST clock if not on */
1583 fastclk = wlc_hw->forcefastclk;
1584 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001585 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001586
1587 wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
1588
1589 brcms_b_phy_reset(wlc_hw);
1590 wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
1591
1592 /* restore the clk */
1593 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02001594 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001595}
1596
1597static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
1598{
1599 u16 v;
1600 struct brcms_c_info *wlc = wlc_hw->wlc;
1601 /* update SYNTHPU_DLY */
1602
1603 if (BRCMS_ISLCNPHY(wlc->band))
1604 v = SYNTHPU_DLY_LPPHY_US;
1605 else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
1606 v = SYNTHPU_DLY_NPHY_US;
1607 else
1608 v = SYNTHPU_DLY_BPHY_US;
1609
1610 brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
1611}
1612
1613static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
1614{
1615 u16 phyctl;
1616 u16 phytxant = wlc_hw->bmac_phytxant;
1617 u16 mask = PHY_TXC_ANT_MASK;
1618
1619 /* set the Probe Response frame phy control word */
1620 phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
1621 phyctl = (phyctl & ~mask) | phytxant;
1622 brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
1623
1624 /* set the Response (ACK/CTS) frame phy control word */
1625 phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
1626 phyctl = (phyctl & ~mask) | phytxant;
1627 brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
1628}
1629
1630static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
1631 u8 rate)
1632{
1633 uint i;
1634 u8 plcp_rate = 0;
1635 struct plcp_signal_rate_lookup {
1636 u8 rate;
1637 u8 signal_rate;
1638 };
1639 /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
1640 const struct plcp_signal_rate_lookup rate_lookup[] = {
1641 {BRCM_RATE_6M, 0xB},
1642 {BRCM_RATE_9M, 0xF},
1643 {BRCM_RATE_12M, 0xA},
1644 {BRCM_RATE_18M, 0xE},
1645 {BRCM_RATE_24M, 0x9},
1646 {BRCM_RATE_36M, 0xD},
1647 {BRCM_RATE_48M, 0x8},
1648 {BRCM_RATE_54M, 0xC}
1649 };
1650
1651 for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
1652 if (rate == rate_lookup[i].rate) {
1653 plcp_rate = rate_lookup[i].signal_rate;
1654 break;
1655 }
1656 }
1657
1658 /* Find the SHM pointer to the rate table entry by looking in the
1659 * Direct-map Table
1660 */
1661 return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
1662}
1663
1664static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
1665{
1666 u8 rate;
1667 u8 rates[8] = {
1668 BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
1669 BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
1670 };
1671 u16 entry_ptr;
1672 u16 pctl1;
1673 uint i;
1674
1675 if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
1676 return;
1677
1678 /* walk the phy rate table and update the entries */
1679 for (i = 0; i < ARRAY_SIZE(rates); i++) {
1680 rate = rates[i];
1681
1682 entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
1683
1684 /* read the SHM Rate Table entry OFDM PCTL1 values */
1685 pctl1 =
1686 brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
1687
1688 /* modify the value */
1689 pctl1 &= ~PHY_TXC1_MODE_MASK;
1690 pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
1691
1692 /* Update the SHM Rate Table entry OFDM PCTL1 values */
1693 brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
1694 pctl1);
1695 }
1696}
1697
1698/* band-specific init */
1699static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
1700{
1701 struct brcms_hardware *wlc_hw = wlc->hw;
1702
Seth Forshee913911f2012-11-15 08:08:04 -06001703 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
1704 wlc_hw->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001705
1706 brcms_c_ucode_bsinit(wlc_hw);
1707
1708 wlc_phy_init(wlc_hw->band->pi, chanspec);
1709
1710 brcms_c_ucode_txant_set(wlc_hw);
1711
1712 /*
1713 * cwmin is band-specific, update hardware
1714 * with value for current band
1715 */
1716 brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
1717 brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
1718
1719 brcms_b_update_slot_timing(wlc_hw,
1720 wlc_hw->band->bandtype == BRCM_BAND_5G ?
1721 true : wlc_hw->shortslot);
1722
1723 /* write phytype and phyvers */
1724 brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
1725 brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
1726
1727 /*
1728 * initialize the txphyctl1 rate table since
1729 * shmem is shared between bands
1730 */
1731 brcms_upd_ofdm_pctl1_table(wlc_hw);
1732
1733 brcms_b_upd_synthpu(wlc_hw);
1734}
1735
1736/* Perform a soft reset of the PHY PLL */
1737void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
1738{
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001739 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
1740 ~0, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001741 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001742 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1743 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001744 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001745 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1746 0x4, 4);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001747 udelay(1);
Arend van Spriel7d8e18e2011-12-08 15:06:56 -08001748 ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1749 0x4, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001750 udelay(1);
1751}
1752
1753/* light way to turn on phy clock without reset for NPHY only
1754 * refer to brcms_b_core_phy_clk for full version
1755 */
1756void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
1757{
1758 /* support(necessary for NPHY and HYPHY) only */
1759 if (!BRCMS_ISNPHY(wlc_hw->band))
1760 return;
1761
1762 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001763 brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001764 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001765 brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001766
1767}
1768
1769void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
1770{
1771 if (ON == clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001772 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001773 else
Arend van Spriela8779e42011-12-08 15:06:58 -08001774 brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001775}
1776
1777void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
1778{
1779 struct brcms_phy_pub *pih = wlc_hw->band->pi;
1780 u32 phy_bw_clkbits;
1781 bool phy_in_reset = false;
1782
Seth Forsheeb353dda2012-11-15 08:08:03 -06001783 brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001784
1785 if (pih == NULL)
1786 return;
1787
1788 phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
1789
1790 /* Specific reset sequence required for NPHY rev 3 and 4 */
1791 if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
1792 NREV_LE(wlc_hw->band->phyrev, 4)) {
1793 /* Set the PHY bandwidth */
Arend van Spriela8779e42011-12-08 15:06:58 -08001794 brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001795
1796 udelay(1);
1797
1798 /* Perform a soft reset of the PHY PLL */
1799 brcms_b_core_phypll_reset(wlc_hw);
1800
1801 /* reset the PHY */
Arend van Spriela8779e42011-12-08 15:06:58 -08001802 brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
1803 (SICF_PRST | SICF_PCLKE));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804 phy_in_reset = true;
1805 } else {
Arend van Spriela8779e42011-12-08 15:06:58 -08001806 brcms_b_core_ioctl(wlc_hw,
1807 (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
1808 (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001809 }
1810
1811 udelay(2);
1812 brcms_b_core_phy_clk(wlc_hw, ON);
1813
1814 if (pih)
1815 wlc_phy_anacore(pih, ON);
1816}
1817
1818/* switch to and initialize new band */
1819static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
1820 u16 chanspec) {
1821 struct brcms_c_info *wlc = wlc_hw->wlc;
1822 u32 macintmask;
1823
1824 /* Enable the d11 core before accessing it */
Arend van Spriela8779e42011-12-08 15:06:58 -08001825 if (!bcma_core_is_enabled(wlc_hw->d11core)) {
1826 bcma_core_enable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001827 brcms_c_mctrl_reset(wlc_hw);
1828 }
1829
1830 macintmask = brcms_c_setband_inact(wlc, bandunit);
1831
1832 if (!wlc_hw->up)
1833 return;
1834
1835 brcms_b_core_phy_clk(wlc_hw, ON);
1836
1837 /* band-specific initializations */
1838 brcms_b_bsinit(wlc, chanspec);
1839
1840 /*
1841 * If there are any pending software interrupt bits,
1842 * then replace these with a harmless nonzero value
1843 * so brcms_c_dpc() will re-enable interrupts when done.
1844 */
1845 if (wlc->macintstatus)
1846 wlc->macintstatus = MI_DMAINT;
1847
1848 /* restore macintmask */
1849 brcms_intrsrestore(wlc->wl, macintmask);
1850
1851 /* ucode should still be suspended.. */
Arend van Spriel16d28122011-12-08 15:06:51 -08001852 WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
1853 MCTL_EN_MAC) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001854}
1855
Arend van Spriel5b435de2011-10-05 13:19:03 +02001856static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
1857{
1858
1859 /* reject unsupported corerev */
1860 if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
1861 wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
1862 wlc_hw->corerev);
1863 return false;
1864 }
1865
1866 return true;
1867}
1868
1869/* Validate some board info parameters */
1870static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
1871{
1872 uint boardrev = wlc_hw->boardrev;
1873
1874 /* 4 bits each for board type, major, minor, and tiny version */
1875 uint brt = (boardrev & 0xf000) >> 12;
1876 uint b0 = (boardrev & 0xf00) >> 8;
1877 uint b1 = (boardrev & 0xf0) >> 4;
1878 uint b2 = boardrev & 0xf;
1879
1880 /* voards from other vendors are always considered valid */
Arend van Sprielb2ffec42011-12-08 15:06:45 -08001881 if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001882 return true;
1883
1884 /* do some boardrev sanity checks when boardvendor is Broadcom */
1885 if (boardrev == 0)
1886 return false;
1887
1888 if (boardrev <= 0xff)
1889 return true;
1890
1891 if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
1892 || (b2 > 9))
1893 return false;
1894
1895 return true;
1896}
1897
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001898static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
Arend van Spriel5b435de2011-10-05 13:19:03 +02001899{
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001900 struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001901
1902 /* If macaddr exists, use it (Sromrev4, CIS, ...). */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001903 if (!is_zero_ether_addr(sprom->il0mac)) {
1904 memcpy(etheraddr, sprom->il0mac, 6);
1905 return;
1906 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001907
1908 if (wlc_hw->_nbands > 1)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001909 memcpy(etheraddr, sprom->et1mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001910 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02001911 memcpy(etheraddr, sprom->il0mac, 6);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001912}
1913
1914/* power both the pll and external oscillator on/off */
1915static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
1916{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001917 brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001918
1919 /*
1920 * dont power down if plldown is false or
1921 * we must poll hw radio disable
1922 */
1923 if (!want && wlc_hw->pllreq)
1924 return;
1925
Arend van Spriel5b435de2011-10-05 13:19:03 +02001926 wlc_hw->sbclk = want;
1927 if (!wlc_hw->sbclk) {
1928 wlc_hw->clk = false;
1929 if (wlc_hw->band && wlc_hw->band->pi)
1930 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
1931 }
1932}
1933
1934/*
1935 * Return true if radio is disabled, otherwise false.
1936 * hw radio disable signal is an external pin, users activate it asynchronously
1937 * this function could be called when driver is down and w/o clock
1938 * it operates on different registers depending on corerev and boardflag.
1939 */
1940static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
1941{
1942 bool v, clk, xtal;
Arend van Spriela8779e42011-12-08 15:06:58 -08001943 u32 flags = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001944
1945 xtal = wlc_hw->sbclk;
1946 if (!xtal)
1947 brcms_b_xtal(wlc_hw, ON);
1948
1949 /* may need to take core out of reset first */
1950 clk = wlc_hw->clk;
1951 if (!clk) {
1952 /*
1953 * mac no longer enables phyclk automatically when driver
1954 * accesses phyreg throughput mac. This can be skipped since
1955 * only mac reg is accessed below
1956 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02001957 if (D11REV_GE(wlc_hw->corerev, 18))
1958 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001959
1960 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08001961 * TODO: test suspend/resume
1962 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02001963 * AI chip doesn't restore bar0win2 on
1964 * hibernation/resume, need sw fixup
1965 */
Arend van Spriel16d28122011-12-08 15:06:51 -08001966
Arend van Spriela8779e42011-12-08 15:06:58 -08001967 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001968 brcms_c_mctrl_reset(wlc_hw);
1969 }
1970
Arend van Spriel16d28122011-12-08 15:06:51 -08001971 v = ((bcma_read32(wlc_hw->d11core,
1972 D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001973
1974 /* put core back into reset */
1975 if (!clk)
Arend van Spriela8779e42011-12-08 15:06:58 -08001976 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001977
1978 if (!xtal)
1979 brcms_b_xtal(wlc_hw, OFF);
1980
1981 return v;
1982}
1983
1984static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
1985{
1986 struct dma_pub *di = wlc_hw->di[fifo];
1987 return dma_rxreset(di);
1988}
1989
1990/* d11 core reset
1991 * ensure fask clock during reset
1992 * reset dma
1993 * reset d11(out of reset)
1994 * reset phy(out of reset)
1995 * clear software macintstatus for fresh new start
1996 * one testing hack wlc_hw->noreset will bypass the d11/phy reset
1997 */
1998void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
1999{
Arend van Spriel5b435de2011-10-05 13:19:03 +02002000 uint i;
2001 bool fastclk;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002002
2003 if (flags == BRCMS_USE_COREFLAGS)
2004 flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
2005
Seth Forsheeb353dda2012-11-15 08:08:03 -06002006 brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002007
Arend van Spriel5b435de2011-10-05 13:19:03 +02002008 /* request FAST clock if not on */
2009 fastclk = wlc_hw->forcefastclk;
2010 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002011 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002012
2013 /* reset the dma engines except first time thru */
Arend van Spriela8779e42011-12-08 15:06:58 -08002014 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015 for (i = 0; i < NFIFO; i++)
2016 if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002017 brcms_err(wlc_hw->d11core, "wl%d: %s: "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002018 "dma_txreset[%d]: cannot stop dma\n",
2019 wlc_hw->unit, __func__, i);
2020
2021 if ((wlc_hw->di[RX_FIFO])
2022 && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002023 brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002024 "[%d]: cannot stop dma\n",
2025 wlc_hw->unit, __func__, RX_FIFO);
2026 }
2027 /* if noreset, just stop the psm and return */
2028 if (wlc_hw->noreset) {
2029 wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
2030 brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
2031 return;
2032 }
2033
2034 /*
2035 * mac no longer enables phyclk automatically when driver accesses
2036 * phyreg throughput mac, AND phy_reset is skipped at early stage when
2037 * band->pi is invalid. need to enable PHY CLK
2038 */
Hauke Mehrtens0d3b9dd2012-06-30 15:16:15 +02002039 if (D11REV_GE(wlc_hw->corerev, 18))
2040 flags |= SICF_PCLKE;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002041
2042 /*
2043 * reset the core
2044 * In chips with PMU, the fastclk request goes through d11 core
2045 * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
2046 *
2047 * This adds some delay and we can optimize it by also requesting
2048 * fastclk through chipcommon during this period if necessary. But
2049 * that has to work coordinate with other driver like mips/arm since
2050 * they may touch chipcommon as well.
2051 */
2052 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002053 bcma_core_enable(wlc_hw->d11core, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002054 wlc_hw->clk = true;
2055 if (wlc_hw->band && wlc_hw->band->pi)
2056 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
2057
2058 brcms_c_mctrl_reset(wlc_hw);
2059
Arend van Sprielb2ffec42011-12-08 15:06:45 -08002060 if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002061 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002062
2063 brcms_b_phy_reset(wlc_hw);
2064
2065 /* turn on PHY_PLL */
2066 brcms_b_core_phypll_ctl(wlc_hw, true);
2067
2068 /* clear sw intstatus */
2069 wlc_hw->wlc->macintstatus = 0;
2070
2071 /* restore the clk setting */
2072 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02002073 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002074}
2075
2076/* txfifo sizes needs to be modified(increased) since the newer cores
2077 * have more memory.
2078 */
2079static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
2080{
Arend van Spriel16d28122011-12-08 15:06:51 -08002081 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002082 u16 fifo_nu;
2083 u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
2084 u16 txfifo_def, txfifo_def1;
2085 u16 txfifo_cmd;
2086
2087 /* tx fifos start at TXFIFO_START_BLK from the Base address */
2088 txfifo_startblk = TXFIFO_START_BLK;
2089
2090 /* sequence of operations: reset fifo, set fifo size, reset fifo */
2091 for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
2092
2093 txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
2094 txfifo_def = (txfifo_startblk & 0xff) |
2095 (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
2096 txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
2097 ((((txfifo_endblk -
2098 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
2099 txfifo_cmd =
2100 TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
2101
Arend van Spriel16d28122011-12-08 15:06:51 -08002102 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
2103 bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
2104 bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002105
Arend van Spriel16d28122011-12-08 15:06:51 -08002106 bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002107
2108 txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
2109 }
2110 /*
2111 * need to propagate to shm location to be in sync since ucode/hw won't
2112 * do this
2113 */
2114 brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
2115 wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
2116 brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
2117 wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
2118 brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
2119 ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
2120 xmtfifo_sz[TX_AC_BK_FIFO]));
2121 brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
2122 ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
2123 xmtfifo_sz[TX_BCMC_FIFO]));
2124}
2125
2126/* This function is used for changing the tsf frac register
2127 * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
2128 * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
2129 * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
2130 * HTPHY Formula is 2^26/freq(MHz) e.g.
2131 * For spuron2 - 126MHz -> 2^26/126 = 532610.0
2132 * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
2133 * For spuron: 123MHz -> 2^26/123 = 545600.5
2134 * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
2135 * For spur off: 120MHz -> 2^26/120 = 559240.5
2136 * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
2137 */
2138
2139void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
2140{
Arend van Spriel16d28122011-12-08 15:06:51 -08002141 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002142
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002143 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
2144 (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002145 if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002146 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
2147 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002148 } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002149 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
2150 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002151 } else { /* 120Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002152 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
2153 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002154 }
2155 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2156 if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002157 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
2158 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002159 } else { /* 80Mhz */
Arend van Spriel16d28122011-12-08 15:06:51 -08002160 bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
2161 bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002162 }
2163 }
2164}
2165
2166/* Initialize GPIOs that are controlled by D11 core */
2167static void brcms_c_gpio_init(struct brcms_c_info *wlc)
2168{
2169 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002170 u32 gc, gm;
2171
Arend van Spriel5b435de2011-10-05 13:19:03 +02002172 /* use GPIO select 0 to get all gpio signals from the gpio out reg */
2173 brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
2174
2175 /*
2176 * Common GPIO setup:
2177 * G0 = LED 0 = WLAN Activity
2178 * G1 = LED 1 = WLAN 2.4 GHz Radio State
2179 * G2 = LED 2 = WLAN 5 GHz Radio State
2180 * G4 = radio disable input (HI enabled, LO disabled)
2181 */
2182
2183 gc = gm = 0;
2184
2185 /* Allocate GPIOs for mimo antenna diversity feature */
2186 if (wlc_hw->antsel_type == ANTSEL_2x3) {
2187 /* Enable antenna diversity, use 2x3 mode */
2188 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2189 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2190 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
2191 MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
2192
2193 /* init superswitch control */
2194 wlc_phy_antsel_init(wlc_hw->band->pi, false);
2195
2196 } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
2197 gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
2198 /*
2199 * The board itself is powered by these GPIOs
2200 * (when not sending pattern) so set them high
2201 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002202 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
2203 (BOARD_GPIO_12 | BOARD_GPIO_13));
2204 bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
2205 (BOARD_GPIO_12 | BOARD_GPIO_13));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002206
2207 /* Enable antenna diversity, use 2x4 mode */
2208 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2209 MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2210 brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
2211 BRCM_BAND_ALL);
2212
2213 /* Configure the desired clock to be 4Mhz */
2214 brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
2215 ANTSEL_CLKDIV_4MHZ);
2216 }
2217
2218 /*
2219 * gpio 9 controls the PA. ucode is responsible
2220 * for wiggling out and oe
2221 */
2222 if (wlc_hw->boardflags & BFL_PACTRL)
2223 gm |= gc |= BOARD_GPIO_PACTRL;
2224
2225 /* apply to gpiocontrol register */
Hauke Mehrtensfa0b8232012-04-29 02:50:34 +02002226 bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002227}
2228
2229static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
2230 const __le32 ucode[], const size_t nbytes)
2231{
Arend van Spriel16d28122011-12-08 15:06:51 -08002232 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002233 uint i;
2234 uint count;
2235
Seth Forsheeb353dda2012-11-15 08:08:03 -06002236 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002237
2238 count = (nbytes / sizeof(u32));
2239
Arend van Spriel16d28122011-12-08 15:06:51 -08002240 bcma_write32(core, D11REGOFFS(objaddr),
2241 OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
2242 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002243 for (i = 0; i < count; i++)
Arend van Spriel16d28122011-12-08 15:06:51 -08002244 bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002245
2246}
2247
2248static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
2249{
2250 struct brcms_c_info *wlc;
2251 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
2252
2253 wlc = wlc_hw->wlc;
2254
2255 if (wlc_hw->ucode_loaded)
2256 return;
2257
Hauke Mehrtens6f80f012012-12-07 00:35:53 +01002258 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002259 if (BRCMS_ISNPHY(wlc_hw->band)) {
2260 brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
2261 ucode->bcm43xx_16_mimosz);
2262 wlc_hw->ucode_loaded = true;
2263 } else
Seth Forsheeb353dda2012-11-15 08:08:03 -06002264 brcms_err(wlc_hw->d11core,
2265 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002266 __func__, wlc_hw->unit, wlc_hw->corerev);
2267 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
2268 if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2269 brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
2270 ucode->bcm43xx_24_lcnsz);
2271 wlc_hw->ucode_loaded = true;
2272 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002273 brcms_err(wlc_hw->d11core,
2274 "%s: wl%d: unsupported phy in corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002275 __func__, wlc_hw->unit, wlc_hw->corerev);
2276 }
2277 }
2278}
2279
2280void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
2281{
2282 /* update sw state */
2283 wlc_hw->bmac_phytxant = phytxant;
2284
2285 /* push to ucode if up */
2286 if (!wlc_hw->up)
2287 return;
2288 brcms_c_ucode_txant_set(wlc_hw);
2289
2290}
2291
2292u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
2293{
2294 return (u16) wlc_hw->wlc->stf->txant;
2295}
2296
2297void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
2298{
2299 wlc_hw->antsel_type = antsel_type;
2300
2301 /* Update the antsel type for phy module to use */
2302 wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
2303}
2304
2305static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
2306{
2307 bool fatal = false;
2308 uint unit;
2309 uint intstatus, idx;
Arend van Spriel16d28122011-12-08 15:06:51 -08002310 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002311
2312 unit = wlc_hw->unit;
2313
2314 for (idx = 0; idx < NFIFO; idx++) {
2315 /* read intstatus register and ignore any non-error bits */
2316 intstatus =
Arend van Spriel16d28122011-12-08 15:06:51 -08002317 bcma_read32(core,
2318 D11REGOFFS(intctrlregs[idx].intstatus)) &
2319 I_ERRORS;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002320 if (!intstatus)
2321 continue;
2322
Seth Forshee229a41d2012-11-15 08:08:06 -06002323 brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
2324 unit, idx, intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002325
2326 if (intstatus & I_RO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002327 brcms_err(core, "wl%d: fifo %d: receive fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002328 "overflow\n", unit, idx);
2329 fatal = true;
2330 }
2331
2332 if (intstatus & I_PC) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002333 brcms_err(core, "wl%d: fifo %d: descriptor error\n",
2334 unit, idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002335 fatal = true;
2336 }
2337
2338 if (intstatus & I_PD) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002339 brcms_err(core, "wl%d: fifo %d: data error\n", unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002340 idx);
2341 fatal = true;
2342 }
2343
2344 if (intstatus & I_DE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002345 brcms_err(core, "wl%d: fifo %d: descriptor protocol "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002346 "error\n", unit, idx);
2347 fatal = true;
2348 }
2349
2350 if (intstatus & I_RU)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002351 brcms_err(core, "wl%d: fifo %d: receive descriptor "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002352 "underflow\n", idx, unit);
2353
2354 if (intstatus & I_XU) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002355 brcms_err(core, "wl%d: fifo %d: transmit fifo "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002356 "underflow\n", idx, unit);
2357 fatal = true;
2358 }
2359
2360 if (fatal) {
Roland Vossenc261bdf2011-10-18 14:03:04 +02002361 brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002362 break;
2363 } else
Arend van Spriel16d28122011-12-08 15:06:51 -08002364 bcma_write32(core,
2365 D11REGOFFS(intctrlregs[idx].intstatus),
2366 intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002367 }
2368}
2369
2370void brcms_c_intrson(struct brcms_c_info *wlc)
2371{
2372 struct brcms_hardware *wlc_hw = wlc->hw;
2373 wlc->macintmask = wlc->defmacintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002374 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002375}
2376
Arend van Spriel5b435de2011-10-05 13:19:03 +02002377u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
2378{
2379 struct brcms_hardware *wlc_hw = wlc->hw;
2380 u32 macintmask;
2381
2382 if (!wlc_hw->clk)
2383 return 0;
2384
2385 macintmask = wlc->macintmask; /* isr can still happen */
2386
Arend van Spriel16d28122011-12-08 15:06:51 -08002387 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
2388 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002389 udelay(1); /* ensure int line is no longer driven */
2390 wlc->macintmask = 0;
2391
2392 /* return previous macintmask; resolve race between us and our isr */
2393 return wlc->macintstatus ? 0 : macintmask;
2394}
2395
2396void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2397{
2398 struct brcms_hardware *wlc_hw = wlc->hw;
2399 if (!wlc_hw->clk)
2400 return;
2401
2402 wlc->macintmask = macintmask;
Arend van Spriel16d28122011-12-08 15:06:51 -08002403 bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002404}
2405
Roland Vossendc460122011-10-21 16:16:28 +02002406/* assumes that the d11 MAC is enabled */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002407static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
2408 uint tx_fifo)
2409{
2410 u8 fifo = 1 << tx_fifo;
2411
2412 /* Two clients of this code, 11h Quiet period and scanning. */
2413
2414 /* only suspend if not already suspended */
2415 if ((wlc_hw->suspended_fifos & fifo) == fifo)
2416 return;
2417
2418 /* force the core awake only if not already */
2419 if (wlc_hw->suspended_fifos == 0)
2420 brcms_c_ucode_wake_override_set(wlc_hw,
2421 BRCMS_WAKE_OVERRIDE_TXFIFO);
2422
2423 wlc_hw->suspended_fifos |= fifo;
2424
2425 if (wlc_hw->di[tx_fifo]) {
2426 /*
2427 * Suspending AMPDU transmissions in the middle can cause
2428 * underflow which may result in mismatch between ucode and
2429 * driver so suspend the mac before suspending the FIFO
2430 */
2431 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2432 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
2433
2434 dma_txsuspend(wlc_hw->di[tx_fifo]);
2435
2436 if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2437 brcms_c_enable_mac(wlc_hw->wlc);
2438 }
2439}
2440
2441static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
2442 uint tx_fifo)
2443{
2444 /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
2445 * but need to be done here for PIO otherwise the watchdog will catch
2446 * the inconsistency and fire
2447 */
2448 /* Two clients of this code, 11h Quiet period and scanning. */
2449 if (wlc_hw->di[tx_fifo])
2450 dma_txresume(wlc_hw->di[tx_fifo]);
2451
2452 /* allow core to sleep again */
2453 if (wlc_hw->suspended_fifos == 0)
2454 return;
2455 else {
2456 wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
2457 if (wlc_hw->suspended_fifos == 0)
2458 brcms_c_ucode_wake_override_clear(wlc_hw,
2459 BRCMS_WAKE_OVERRIDE_TXFIFO);
2460 }
2461}
2462
Roland Vossena8bc4912011-10-21 16:16:25 +02002463/* precondition: requires the mac core to be enabled */
Roland Vossenc6c44892011-10-21 16:16:26 +02002464static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002465{
2466 static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Arend van Sprielb180b102013-01-02 15:22:36 +01002467 u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002468
Roland Vossenc6c44892011-10-21 16:16:26 +02002469 if (mute_tx) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002470 /* suspend tx fifos */
2471 brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
2472 brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
2473 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
2474 brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
2475
2476 /* zero the address match register so we do not send ACKs */
Arend van Sprielb180b102013-01-02 15:22:36 +01002477 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002478 } else {
2479 /* resume tx fifos */
2480 brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
2481 brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
2482 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
2483 brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
2484
2485 /* Restore address */
Arend van Sprielb180b102013-01-02 15:22:36 +01002486 brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002487 }
2488
Roland Vossenc6c44892011-10-21 16:16:26 +02002489 wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002490
Roland Vossenc6c44892011-10-21 16:16:26 +02002491 if (mute_tx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002492 brcms_c_ucode_mute_override_set(wlc_hw);
2493 else
2494 brcms_c_ucode_mute_override_clear(wlc_hw);
2495}
2496
Roland Vossendc460122011-10-21 16:16:28 +02002497void
2498brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
2499{
2500 brcms_b_mute(wlc->hw, mute_tx);
2501}
2502
Arend van Spriel5b435de2011-10-05 13:19:03 +02002503/*
2504 * Read and clear macintmask and macintstatus and intstatus registers.
2505 * This routine should be called with interrupts off
2506 * Return:
2507 * -1 if brcms_deviceremoved(wlc) evaluates to true;
2508 * 0 if the interrupt is not for us, or we are in some special cases;
2509 * device interrupt status bits otherwise.
2510 */
2511static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
2512{
2513 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002514 struct bcma_device *core = wlc_hw->d11core;
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002515 u32 macintstatus, mask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002516
2517 /* macintstatus includes a DMA interrupt summary bit */
Arend van Spriel16d28122011-12-08 15:06:51 -08002518 macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002519 mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002520
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002521 trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522
2523 /* detect cardbus removed, in power down(suspend) and in reset */
2524 if (brcms_deviceremoved(wlc))
2525 return -1;
2526
2527 /* brcms_deviceremoved() succeeds even when the core is still resetting,
2528 * handle that case here.
2529 */
2530 if (macintstatus == 0xffffffff)
2531 return 0;
2532
2533 /* defer unsolicited interrupts */
Seth Forsheee3c0d8a2012-11-15 08:08:10 -06002534 macintstatus &= mask;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002535
2536 /* if not for us */
2537 if (macintstatus == 0)
2538 return 0;
2539
Arend van Spriel5b435de2011-10-05 13:19:03 +02002540 /* turn off the interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002541 bcma_write32(core, D11REGOFFS(macintmask), 0);
2542 (void)bcma_read32(core, D11REGOFFS(macintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002543 wlc->macintmask = 0;
2544
2545 /* clear device interrupts */
Arend van Spriel16d28122011-12-08 15:06:51 -08002546 bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547
2548 /* MI_DMAINT is indication of non-zero intstatus */
2549 if (macintstatus & MI_DMAINT)
2550 /*
2551 * only fifo interrupt enabled is I_RI in
2552 * RX_FIFO. If MI_DMAINT is set, assume it
2553 * is set and clear the interrupt.
2554 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002555 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
2556 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002557
2558 return macintstatus;
2559}
2560
2561/* Update wlc->macintstatus and wlc->intstatus[]. */
2562/* Return true if they are updated successfully. false otherwise */
2563bool brcms_c_intrsupd(struct brcms_c_info *wlc)
2564{
2565 u32 macintstatus;
2566
2567 /* read and clear macintstatus and intstatus registers */
2568 macintstatus = wlc_intstatus(wlc, false);
2569
2570 /* device is removed */
2571 if (macintstatus == 0xffffffff)
2572 return false;
2573
2574 /* update interrupt status in software */
2575 wlc->macintstatus |= macintstatus;
2576
2577 return true;
2578}
2579
2580/*
2581 * First-level interrupt processing.
Piotr Haber94d99022012-11-28 21:44:06 +01002582 * Return true if this was our interrupt
2583 * and if further brcms_c_dpc() processing is required,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002584 * false otherwise.
2585 */
Piotr Haber94d99022012-11-28 21:44:06 +01002586bool brcms_c_isr(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002587{
2588 struct brcms_hardware *wlc_hw = wlc->hw;
2589 u32 macintstatus;
2590
Arend van Spriel5b435de2011-10-05 13:19:03 +02002591 if (!wlc_hw->up || !wlc->macintmask)
2592 return false;
2593
2594 /* read and clear macintstatus and intstatus registers */
2595 macintstatus = wlc_intstatus(wlc, true);
2596
Piotr Haber94d99022012-11-28 21:44:06 +01002597 if (macintstatus == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002598 brcms_err(wlc_hw->d11core,
2599 "DEVICEREMOVED detected in the ISR code path\n");
Piotr Haber94d99022012-11-28 21:44:06 +01002600 return false;
2601 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002602
2603 /* it is not for us */
2604 if (macintstatus == 0)
2605 return false;
2606
Arend van Spriel5b435de2011-10-05 13:19:03 +02002607 /* save interrupt status bits */
2608 wlc->macintstatus = macintstatus;
2609
2610 return true;
2611
2612}
2613
2614void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
2615{
2616 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002617 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002618 u32 mc, mi;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002619
Seth Forshee913911f2012-11-15 08:08:04 -06002620 brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2621 wlc_hw->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002622
2623 /*
2624 * Track overlapping suspend requests
2625 */
2626 wlc_hw->mac_suspend_depth++;
2627 if (wlc_hw->mac_suspend_depth > 1)
2628 return;
2629
2630 /* force the core awake */
2631 brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2632
Arend van Spriel16d28122011-12-08 15:06:51 -08002633 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002634
2635 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002636 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002637 __func__);
2638 brcms_down(wlc->wl);
2639 return;
2640 }
2641 WARN_ON(mc & MCTL_PSM_JMP_0);
2642 WARN_ON(!(mc & MCTL_PSM_RUN));
2643 WARN_ON(!(mc & MCTL_EN_MAC));
2644
Arend van Spriel16d28122011-12-08 15:06:51 -08002645 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002646 if (mi == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002647 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002648 __func__);
2649 brcms_down(wlc->wl);
2650 return;
2651 }
2652 WARN_ON(mi & MI_MACSSPNDD);
2653
2654 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
2655
Arend van Spriel16d28122011-12-08 15:06:51 -08002656 SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
Arend van Spriel5b435de2011-10-05 13:19:03 +02002657 BRCMS_MAX_MAC_SUSPEND);
2658
Arend van Spriel16d28122011-12-08 15:06:51 -08002659 if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002660 brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
Arend van Spriel5b435de2011-10-05 13:19:03 +02002661 " and MI_MACSSPNDD is still not on.\n",
2662 wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
Seth Forsheeb353dda2012-11-15 08:08:03 -06002663 brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002664 "psm_brc 0x%04x\n", wlc_hw->unit,
Arend van Spriel16d28122011-12-08 15:06:51 -08002665 bcma_read32(core, D11REGOFFS(psmdebug)),
2666 bcma_read32(core, D11REGOFFS(phydebug)),
2667 bcma_read16(core, D11REGOFFS(psm_brc)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002668 }
2669
Arend van Spriel16d28122011-12-08 15:06:51 -08002670 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002671 if (mc == 0xffffffff) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06002672 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002673 __func__);
2674 brcms_down(wlc->wl);
2675 return;
2676 }
2677 WARN_ON(mc & MCTL_PSM_JMP_0);
2678 WARN_ON(!(mc & MCTL_PSM_RUN));
2679 WARN_ON(mc & MCTL_EN_MAC);
2680}
2681
2682void brcms_c_enable_mac(struct brcms_c_info *wlc)
2683{
2684 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08002685 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002686 u32 mc, mi;
2687
Seth Forshee913911f2012-11-15 08:08:04 -06002688 brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2689 wlc->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002690
2691 /*
2692 * Track overlapping suspend requests
2693 */
2694 wlc_hw->mac_suspend_depth--;
2695 if (wlc_hw->mac_suspend_depth > 0)
2696 return;
2697
Arend van Spriel16d28122011-12-08 15:06:51 -08002698 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002699 WARN_ON(mc & MCTL_PSM_JMP_0);
2700 WARN_ON(mc & MCTL_EN_MAC);
2701 WARN_ON(!(mc & MCTL_PSM_RUN));
2702
2703 brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
Arend van Spriel16d28122011-12-08 15:06:51 -08002704 bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002705
Arend van Spriel16d28122011-12-08 15:06:51 -08002706 mc = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002707 WARN_ON(mc & MCTL_PSM_JMP_0);
2708 WARN_ON(!(mc & MCTL_EN_MAC));
2709 WARN_ON(!(mc & MCTL_PSM_RUN));
2710
Arend van Spriel16d28122011-12-08 15:06:51 -08002711 mi = bcma_read32(core, D11REGOFFS(macintstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002712 WARN_ON(mi & MI_MACSSPNDD);
2713
2714 brcms_c_ucode_wake_override_clear(wlc_hw,
2715 BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2716}
2717
2718void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
2719{
2720 wlc_hw->hw_stf_ss_opmode = stf_mode;
2721
2722 if (wlc_hw->clk)
2723 brcms_upd_ofdm_pctl1_table(wlc_hw);
2724}
2725
2726static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
2727{
Arend van Spriel16d28122011-12-08 15:06:51 -08002728 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002729 u32 w, val;
2730 struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2731
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732 /* Validate dchip register access */
2733
Arend van Spriel16d28122011-12-08 15:06:51 -08002734 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2735 (void)bcma_read32(core, D11REGOFFS(objaddr));
2736 w = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002737
2738 /* Can we write and read back a 32bit register? */
Arend van Spriel16d28122011-12-08 15:06:51 -08002739 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2740 (void)bcma_read32(core, D11REGOFFS(objaddr));
2741 bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002742
Arend van Spriel16d28122011-12-08 15:06:51 -08002743 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2744 (void)bcma_read32(core, D11REGOFFS(objaddr));
2745 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002746 if (val != (u32) 0xaa5555aa) {
2747 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2748 "expected 0xaa5555aa\n", wlc_hw->unit, val);
2749 return false;
2750 }
2751
Arend van Spriel16d28122011-12-08 15:06:51 -08002752 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2753 (void)bcma_read32(core, D11REGOFFS(objaddr));
2754 bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002755
Arend van Spriel16d28122011-12-08 15:06:51 -08002756 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2757 (void)bcma_read32(core, D11REGOFFS(objaddr));
2758 val = bcma_read32(core, D11REGOFFS(objdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002759 if (val != (u32) 0x55aaaa55) {
2760 wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2761 "expected 0x55aaaa55\n", wlc_hw->unit, val);
2762 return false;
2763 }
2764
Arend van Spriel16d28122011-12-08 15:06:51 -08002765 bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2766 (void)bcma_read32(core, D11REGOFFS(objaddr));
2767 bcma_write32(core, D11REGOFFS(objdata), w);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002768
2769 /* clear CFPStart */
Arend van Spriel16d28122011-12-08 15:06:51 -08002770 bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002771
Arend van Spriel16d28122011-12-08 15:06:51 -08002772 w = bcma_read32(core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002773 if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
2774 (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
2775 wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
2776 "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
2777 (MCTL_IHR_EN | MCTL_WAKE),
2778 (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
2779 return false;
2780 }
2781
2782 return true;
2783}
2784
2785#define PHYPLL_WAIT_US 100000
2786
2787void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
2788{
Arend van Spriel16d28122011-12-08 15:06:51 -08002789 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002790 u32 tmp;
2791
Seth Forsheeb353dda2012-11-15 08:08:03 -06002792 brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002793
2794 tmp = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002795
2796 if (on) {
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02002797 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08002798 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2799 CCS_ERSRC_REQ_HT |
2800 CCS_ERSRC_REQ_D11PLL |
2801 CCS_ERSRC_REQ_PHYPLL);
2802 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
2803 CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002804 PHYPLL_WAIT_US);
2805
Arend van Spriel16d28122011-12-08 15:06:51 -08002806 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
2807 if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
Seth Forsheeb353dda2012-11-15 08:08:03 -06002808 brcms_err(core, "%s: turn on PHY PLL failed\n",
2809 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002810 } else {
Arend van Spriel16d28122011-12-08 15:06:51 -08002811 bcma_set32(core, D11REGOFFS(clk_ctl_st),
2812 tmp | CCS_ERSRC_REQ_D11PLL |
2813 CCS_ERSRC_REQ_PHYPLL);
2814 SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02002815 (CCS_ERSRC_AVAIL_D11PLL |
2816 CCS_ERSRC_AVAIL_PHYPLL)) !=
2817 (CCS_ERSRC_AVAIL_D11PLL |
2818 CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
2819
Arend van Spriel16d28122011-12-08 15:06:51 -08002820 tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002821 if ((tmp &
2822 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2823 !=
2824 (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
Seth Forsheeb353dda2012-11-15 08:08:03 -06002825 brcms_err(core, "%s: turn on PHY PLL failed\n",
2826 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002827 }
2828 } else {
2829 /*
2830 * Since the PLL may be shared, other cores can still
2831 * be requesting it; so we'll deassert the request but
2832 * not wait for status to comply.
2833 */
Arend van Spriel16d28122011-12-08 15:06:51 -08002834 bcma_mask32(core, D11REGOFFS(clk_ctl_st),
2835 ~CCS_ERSRC_REQ_PHYPLL);
2836 (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002837 }
2838}
2839
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02002840static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002841{
2842 bool dev_gone;
2843
Seth Forsheeb353dda2012-11-15 08:08:03 -06002844 brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002845
2846 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
2847
2848 if (dev_gone)
2849 return;
2850
2851 if (wlc_hw->noreset)
2852 return;
2853
2854 /* radio off */
2855 wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
2856
2857 /* turn off analog core */
2858 wlc_phy_anacore(wlc_hw->band->pi, OFF);
2859
2860 /* turn off PHYPLL to save power */
2861 brcms_b_core_phypll_ctl(wlc_hw, false);
2862
2863 wlc_hw->clk = false;
Arend van Spriela8779e42011-12-08 15:06:58 -08002864 bcma_core_disable(wlc_hw->d11core, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002865 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
2866}
2867
2868static void brcms_c_flushqueues(struct brcms_c_info *wlc)
2869{
2870 struct brcms_hardware *wlc_hw = wlc->hw;
2871 uint i;
2872
2873 /* free any posted tx packets */
Seth Forsheee041f652012-11-15 08:07:56 -06002874 for (i = 0; i < NFIFO; i++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002875 if (wlc_hw->di[i]) {
2876 dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
Seth Forsheee041f652012-11-15 08:07:56 -06002877 if (i < TX_BCMC_FIFO)
2878 ieee80211_wake_queue(wlc->pub->ieee_hw,
2879 brcms_fifo_to_ac(i));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002880 }
Seth Forsheee041f652012-11-15 08:07:56 -06002881 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002882
2883 /* free any posted rx packets */
2884 dma_rxreclaim(wlc_hw->di[RX_FIFO]);
2885}
2886
2887static u16
2888brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
2889{
Arend van Spriel16d28122011-12-08 15:06:51 -08002890 struct bcma_device *core = wlc_hw->d11core;
2891 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002892
Arend van Spriel16d28122011-12-08 15:06:51 -08002893 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2894 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002895 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002896 objoff += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002897
Arend van Spriel16d28122011-12-08 15:06:51 -08002898 return bcma_read16(core, objoff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002899}
2900
2901static void
2902brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
2903 u32 sel)
2904{
Arend van Spriel16d28122011-12-08 15:06:51 -08002905 struct bcma_device *core = wlc_hw->d11core;
2906 u16 objoff = D11REGOFFS(objdata);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002907
Arend van Spriel16d28122011-12-08 15:06:51 -08002908 bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2909 (void)bcma_read32(core, D11REGOFFS(objaddr));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002910 if (offset & 2)
Arend van Spriel16d28122011-12-08 15:06:51 -08002911 objoff += 2;
2912
Hauke Mehrtens512ae052012-12-07 17:04:13 +01002913 bcma_wflush16(core, objoff, v);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002914}
2915
2916/*
2917 * Read a single u16 from shared memory.
2918 * SHM 'offset' needs to be an even address
2919 */
2920u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
2921{
2922 return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
2923}
2924
2925/*
2926 * Write a single u16 to shared memory.
2927 * SHM 'offset' needs to be an even address
2928 */
2929void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
2930{
2931 brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
2932}
2933
2934/*
2935 * Copy a buffer to shared memory of specified type .
2936 * SHM 'offset' needs to be an even address and
2937 * Buffer length 'len' must be an even number of bytes
2938 * 'sel' selects the type of memory
2939 */
2940void
2941brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
2942 const void *buf, int len, u32 sel)
2943{
2944 u16 v;
2945 const u8 *p = (const u8 *)buf;
2946 int i;
2947
2948 if (len <= 0 || (offset & 1) || (len & 1))
2949 return;
2950
2951 for (i = 0; i < len; i += 2) {
2952 v = p[i] | (p[i + 1] << 8);
2953 brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
2954 }
2955}
2956
2957/*
2958 * Copy a piece of shared memory of specified type to a buffer .
2959 * SHM 'offset' needs to be an even address and
2960 * Buffer length 'len' must be an even number of bytes
2961 * 'sel' selects the type of memory
2962 */
2963void
2964brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
2965 int len, u32 sel)
2966{
2967 u16 v;
2968 u8 *p = (u8 *) buf;
2969 int i;
2970
2971 if (len <= 0 || (offset & 1) || (len & 1))
2972 return;
2973
2974 for (i = 0; i < len; i += 2) {
2975 v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
2976 p[i] = v & 0xFF;
2977 p[i + 1] = (v >> 8) & 0xFF;
2978 }
2979}
2980
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02002981/* Copy a buffer to shared memory.
2982 * SHM 'offset' needs to be an even address and
2983 * Buffer length 'len' must be an even number of bytes
2984 */
2985static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
2986 const void *buf, int len)
2987{
2988 brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
2989}
2990
Arend van Spriel5b435de2011-10-05 13:19:03 +02002991static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
2992 u16 SRL, u16 LRL)
2993{
2994 wlc_hw->SRL = SRL;
2995 wlc_hw->LRL = LRL;
2996
2997 /* write retry limit to SCR, shouldn't need to suspend */
2998 if (wlc_hw->up) {
Arend van Spriel16d28122011-12-08 15:06:51 -08002999 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3000 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3001 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3002 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
3003 bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
3004 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3005 (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
3006 bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003007 }
3008}
3009
3010static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
3011{
3012 if (set) {
3013 if (mboolisset(wlc_hw->pllreq, req_bit))
3014 return;
3015
3016 mboolset(wlc_hw->pllreq, req_bit);
3017
3018 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3019 if (!wlc_hw->sbclk)
3020 brcms_b_xtal(wlc_hw, ON);
3021 }
3022 } else {
3023 if (!mboolisset(wlc_hw->pllreq, req_bit))
3024 return;
3025
3026 mboolclr(wlc_hw->pllreq, req_bit);
3027
3028 if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3029 if (wlc_hw->sbclk)
3030 brcms_b_xtal(wlc_hw, OFF);
3031 }
3032 }
3033}
3034
3035static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
3036{
3037 wlc_hw->antsel_avail = antsel_avail;
3038}
3039
3040/*
3041 * conditions under which the PM bit should be set in outgoing frames
3042 * and STAY_AWAKE is meaningful
3043 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003044static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003045{
3046 struct brcms_bss_cfg *cfg = wlc->bsscfg;
3047
3048 /* disallow PS when one of the following global conditions meets */
3049 if (!wlc->pub->associated)
3050 return false;
3051
3052 /* disallow PS when one of these meets when not scanning */
Alwin Beukersbe667662011-11-22 17:21:43 -08003053 if (wlc->filter_flags & FIF_PROMISC_IN_BSS)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003054 return false;
3055
3056 if (cfg->associated) {
3057 /*
3058 * disallow PS when one of the following
3059 * bsscfg specific conditions meets
3060 */
3061 if (!cfg->BSS)
3062 return false;
3063
3064 return false;
3065 }
3066
3067 return true;
3068}
3069
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003070static void brcms_c_statsupd(struct brcms_c_info *wlc)
3071{
3072 int i;
3073 struct macstat macstats;
Joe Perches8ae74652012-01-15 00:38:38 -08003074#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003075 u16 delta;
3076 u16 rxf0ovfl;
3077 u16 txfunfl[NFIFO];
Joe Perches8ae74652012-01-15 00:38:38 -08003078#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003079
3080 /* if driver down, make no sense to update stats */
3081 if (!wlc->pub->up)
3082 return;
3083
Joe Perches8ae74652012-01-15 00:38:38 -08003084#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003085 /* save last rx fifo 0 overflow count */
3086 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
3087
3088 /* save last tx fifo underflow count */
3089 for (i = 0; i < NFIFO; i++)
3090 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
Joe Perches8ae74652012-01-15 00:38:38 -08003091#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003092
3093 /* Read mac stats from contiguous shared memory */
3094 brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, &macstats,
3095 sizeof(struct macstat), OBJADDR_SHM_SEL);
3096
Joe Perches8ae74652012-01-15 00:38:38 -08003097#ifdef DEBUG
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003098 /* check for rx fifo 0 overflow */
3099 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
3100 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003101 brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003102 wlc->pub->unit, delta);
3103
3104 /* check for tx fifo underflows */
3105 for (i = 0; i < NFIFO; i++) {
3106 delta =
3107 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
3108 txfunfl[i]);
3109 if (delta)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003110 brcms_err(wlc->hw->d11core,
3111 "wl%d: %u tx fifo %d underflows!\n",
3112 wlc->pub->unit, delta, i);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003113 }
Joe Perches8ae74652012-01-15 00:38:38 -08003114#endif /* DEBUG */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003115
3116 /* merge counters from dma module */
3117 for (i = 0; i < NFIFO; i++) {
3118 if (wlc->hw->di[i])
3119 dma_counterreset(wlc->hw->di[i]);
3120 }
3121}
3122
Arend van Spriel5b435de2011-10-05 13:19:03 +02003123static void brcms_b_reset(struct brcms_hardware *wlc_hw)
3124{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003125 /* reset the core */
3126 if (!brcms_deviceremoved(wlc_hw->wlc))
3127 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
3128
3129 /* purge the dma rings */
3130 brcms_c_flushqueues(wlc_hw->wlc);
3131}
3132
3133void brcms_c_reset(struct brcms_c_info *wlc)
3134{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003135 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003136
3137 /* slurp up hw mac counters before core reset */
3138 brcms_c_statsupd(wlc);
3139
3140 /* reset our snapshot of macstat counters */
Joe Perchesb56e6812013-02-13 17:33:21 -08003141 memset(wlc->core->macstat_snapshot, 0, sizeof(struct macstat));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003142
3143 brcms_b_reset(wlc->hw);
3144}
3145
Arend van Spriel5b435de2011-10-05 13:19:03 +02003146void brcms_c_init_scb(struct scb *scb)
3147{
3148 int i;
3149
3150 memset(scb, 0, sizeof(struct scb));
3151 scb->flags = SCB_WMECAP | SCB_HTCAP;
3152 for (i = 0; i < NUMPRIO; i++) {
3153 scb->seqnum[i] = 0;
3154 scb->seqctl[i] = 0xFFFF;
3155 }
3156
3157 scb->seqctl_nonqos = 0xFFFF;
3158 scb->magic = SCB_MAGIC;
3159}
3160
3161/* d11 core init
3162 * reset PSM
3163 * download ucode/PCM
3164 * let ucode run to suspended
3165 * download ucode inits
3166 * config other core registers
3167 * init dma
3168 */
3169static void brcms_b_coreinit(struct brcms_c_info *wlc)
3170{
3171 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08003172 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003173 u32 sflags;
Arend van Spriel16d28122011-12-08 15:06:51 -08003174 u32 bcnint_us;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003175 uint i = 0;
3176 bool fifosz_fixup = false;
3177 int err = 0;
3178 u16 buf[NFIFO];
Arend van Spriel5b435de2011-10-05 13:19:03 +02003179 struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
3180
Seth Forsheeb353dda2012-11-15 08:08:03 -06003181 brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003182
3183 /* reset PSM */
3184 brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
3185
3186 brcms_ucode_download(wlc_hw);
3187 /*
3188 * FIFOSZ fixup. driver wants to controls the fifo allocation.
3189 */
3190 fifosz_fixup = true;
3191
3192 /* let the PSM run to the suspended state, set mode to BSS STA */
Arend van Spriel16d28122011-12-08 15:06:51 -08003193 bcma_write32(core, D11REGOFFS(macintstatus), -1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003194 brcms_b_mctrl(wlc_hw, ~0,
3195 (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
3196
3197 /* wait for ucode to self-suspend after auto-init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003198 SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
3199 MI_MACSSPNDD) == 0), 1000 * 1000);
3200 if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003201 brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003202 "suspend!\n", wlc_hw->unit);
3203
3204 brcms_c_gpio_init(wlc);
3205
Arend van Spriela8779e42011-12-08 15:06:58 -08003206 sflags = bcma_aread32(core, BCMA_IOST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003207
Hauke Mehrtens6f80f012012-12-07 00:35:53 +01003208 if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003209 if (BRCMS_ISNPHY(wlc_hw->band))
3210 brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
3211 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003212 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003213 " %d\n", __func__, wlc_hw->unit,
3214 wlc_hw->corerev);
3215 } else if (D11REV_IS(wlc_hw->corerev, 24)) {
3216 if (BRCMS_ISLCNPHY(wlc_hw->band))
3217 brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
3218 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06003219 brcms_err(core, "%s: wl%d: unsupported phy in corerev"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003220 " %d\n", __func__, wlc_hw->unit,
3221 wlc_hw->corerev);
3222 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003223 brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003224 __func__, wlc_hw->unit, wlc_hw->corerev);
3225 }
3226
3227 /* For old ucode, txfifo sizes needs to be modified(increased) */
Joe Perches23677ce2012-02-09 11:17:23 +00003228 if (fifosz_fixup)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003229 brcms_b_corerev_fifofixup(wlc_hw);
3230
3231 /* check txfifo allocations match between ucode and driver */
3232 buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
3233 if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
3234 i = TX_AC_BE_FIFO;
3235 err = -1;
3236 }
3237 buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
3238 if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
3239 i = TX_AC_VI_FIFO;
3240 err = -1;
3241 }
3242 buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
3243 buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
3244 buf[TX_AC_BK_FIFO] &= 0xff;
3245 if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
3246 i = TX_AC_BK_FIFO;
3247 err = -1;
3248 }
3249 if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
3250 i = TX_AC_VO_FIFO;
3251 err = -1;
3252 }
3253 buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
3254 buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
3255 buf[TX_BCMC_FIFO] &= 0xff;
3256 if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
3257 i = TX_BCMC_FIFO;
3258 err = -1;
3259 }
3260 if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
3261 i = TX_ATIM_FIFO;
3262 err = -1;
3263 }
3264 if (err != 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -06003265 brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
Arend van Spriel5b435de2011-10-05 13:19:03 +02003266 " driver size %d index %d\n", buf[i],
3267 wlc_hw->xmtfifo_sz[i], i);
3268
3269 /* make sure we can still talk to the mac */
Arend van Spriel16d28122011-12-08 15:06:51 -08003270 WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003271
3272 /* band-specific inits done by wlc_bsinit() */
3273
3274 /* Set up frame burst size and antenna swap threshold init values */
3275 brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
3276 brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
3277
3278 /* enable one rx interrupt per received frame */
Arend van Spriel16d28122011-12-08 15:06:51 -08003279 bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003280
3281 /* set the station mode (BSS STA) */
3282 brcms_b_mctrl(wlc_hw,
3283 (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
3284 (MCTL_INFRA | MCTL_DISCARD_PMQ));
3285
3286 /* set up Beacon interval */
3287 bcnint_us = 0x8000 << 10;
Arend van Spriel16d28122011-12-08 15:06:51 -08003288 bcma_write32(core, D11REGOFFS(tsf_cfprep),
3289 (bcnint_us << CFPREP_CBI_SHIFT));
3290 bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
3291 bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003292
3293 /* write interrupt mask */
Arend van Spriel16d28122011-12-08 15:06:51 -08003294 bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
3295 DEF_RXINTMASK);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003296
3297 /* allow the MAC to control the PHY clock (dynamic on/off) */
3298 brcms_b_macphyclk_set(wlc_hw, ON);
3299
3300 /* program dynamic clock control fast powerup delay register */
3301 wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
Arend van Spriel16d28122011-12-08 15:06:51 -08003302 bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003303
3304 /* tell the ucode the corerev */
3305 brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
3306
3307 /* tell the ucode MAC capabilities */
3308 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
3309 (u16) (wlc_hw->machwcap & 0xffff));
3310 brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
3311 (u16) ((wlc_hw->
3312 machwcap >> 16) & 0xffff));
3313
3314 /* write retry limits to SCR, this done after PSM init */
Arend van Spriel16d28122011-12-08 15:06:51 -08003315 bcma_write32(core, D11REGOFFS(objaddr),
3316 OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3317 (void)bcma_read32(core, D11REGOFFS(objaddr));
3318 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
3319 bcma_write32(core, D11REGOFFS(objaddr),
3320 OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3321 (void)bcma_read32(core, D11REGOFFS(objaddr));
3322 bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003323
3324 /* write rate fallback retry limits */
3325 brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
3326 brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
3327
Arend van Spriel16d28122011-12-08 15:06:51 -08003328 bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
3329 bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003330
3331 /* init the tx dma engines */
3332 for (i = 0; i < NFIFO; i++) {
3333 if (wlc_hw->di[i])
3334 dma_txinit(wlc_hw->di[i]);
3335 }
3336
3337 /* init the rx dma engine(s) and post receive buffers */
3338 dma_rxinit(wlc_hw->di[RX_FIFO]);
3339 dma_rxfill(wlc_hw->di[RX_FIFO]);
3340}
3341
3342void
Roland Vossena8bc4912011-10-21 16:16:25 +02003343static brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003344 u32 macintmask;
3345 bool fastclk;
3346 struct brcms_c_info *wlc = wlc_hw->wlc;
3347
Arend van Spriel5b435de2011-10-05 13:19:03 +02003348 /* request FAST clock if not on */
3349 fastclk = wlc_hw->forcefastclk;
3350 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003351 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003352
3353 /* disable interrupts */
3354 macintmask = brcms_intrsoff(wlc->wl);
3355
3356 /* set up the specified band and chanspec */
3357 brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
3358 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3359
3360 /* do one-time phy inits and calibration */
3361 wlc_phy_cal_init(wlc_hw->band->pi);
3362
3363 /* core-specific initialization */
3364 brcms_b_coreinit(wlc);
3365
Arend van Spriel5b435de2011-10-05 13:19:03 +02003366 /* band-specific inits */
3367 brcms_b_bsinit(wlc, chanspec);
3368
3369 /* restore macintmask */
3370 brcms_intrsrestore(wlc->wl, macintmask);
3371
3372 /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
3373 * is suspended and brcms_c_enable_mac() will clear this override bit.
3374 */
3375 mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
3376
3377 /*
3378 * initialize mac_suspend_depth to 1 to match ucode
3379 * initial suspended state
3380 */
3381 wlc_hw->mac_suspend_depth = 1;
3382
3383 /* restore the clk */
3384 if (!fastclk)
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02003385 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003386}
3387
3388static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
3389 u16 chanspec)
3390{
3391 /* Save our copy of the chanspec */
3392 wlc->chanspec = chanspec;
3393
3394 /* Set the chanspec and power limits for this locale */
3395 brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
3396
3397 if (wlc->stf->ss_algosel_auto)
3398 brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
3399 chanspec);
3400
3401 brcms_c_stf_ss_update(wlc, wlc->band);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003402}
Arend van Spriel5b435de2011-10-05 13:19:03 +02003403
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003404static void
3405brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
3406{
3407 brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
3408 wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
3409 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
3410 brcms_chspec_bw(wlc->default_bss->chanspec),
3411 wlc->stf->txstreams);
3412}
3413
3414/* derive wlc->band->basic_rate[] table from 'rateset' */
3415static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
3416 struct brcms_c_rateset *rateset)
3417{
3418 u8 rate;
3419 u8 mandatory;
3420 u8 cck_basic = 0;
3421 u8 ofdm_basic = 0;
3422 u8 *br = wlc->band->basic_rate;
3423 uint i;
3424
3425 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
3426 memset(br, 0, BRCM_MAXRATE + 1);
3427
3428 /* For each basic rate in the rates list, make an entry in the
3429 * best basic lookup.
3430 */
3431 for (i = 0; i < rateset->count; i++) {
3432 /* only make an entry for a basic rate */
3433 if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
3434 continue;
3435
3436 /* mask off basic bit */
3437 rate = (rateset->rates[i] & BRCMS_RATE_MASK);
3438
3439 if (rate > BRCM_MAXRATE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003440 brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003441 "invalid rate 0x%X in rate set\n",
3442 rateset->rates[i]);
3443 continue;
3444 }
3445
3446 br[rate] = rate;
3447 }
3448
3449 /* The rate lookup table now has non-zero entries for each
3450 * basic rate, equal to the basic rate: br[basicN] = basicN
3451 *
3452 * To look up the best basic rate corresponding to any
3453 * particular rate, code can use the basic_rate table
3454 * like this
3455 *
3456 * basic_rate = wlc->band->basic_rate[tx_rate]
3457 *
3458 * Make sure there is a best basic rate entry for
3459 * every rate by walking up the table from low rates
3460 * to high, filling in holes in the lookup table
3461 */
3462
3463 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
3464 rate = wlc->band->hw_rateset.rates[i];
3465
3466 if (br[rate] != 0) {
3467 /* This rate is a basic rate.
3468 * Keep track of the best basic rate so far by
3469 * modulation type.
3470 */
3471 if (is_ofdm_rate(rate))
3472 ofdm_basic = rate;
3473 else
3474 cck_basic = rate;
3475
3476 continue;
3477 }
3478
3479 /* This rate is not a basic rate so figure out the
3480 * best basic rate less than this rate and fill in
3481 * the hole in the table
3482 */
3483
3484 br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
3485
3486 if (br[rate] != 0)
3487 continue;
3488
3489 if (is_ofdm_rate(rate)) {
3490 /*
3491 * In 11g and 11a, the OFDM mandatory rates
3492 * are 6, 12, and 24 Mbps
3493 */
3494 if (rate >= BRCM_RATE_24M)
3495 mandatory = BRCM_RATE_24M;
3496 else if (rate >= BRCM_RATE_12M)
3497 mandatory = BRCM_RATE_12M;
3498 else
3499 mandatory = BRCM_RATE_6M;
3500 } else {
3501 /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
3502 mandatory = rate;
3503 }
3504
3505 br[rate] = mandatory;
3506 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003507}
3508
3509static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
3510 u16 chanspec)
3511{
3512 struct brcms_c_rateset default_rateset;
3513 uint parkband;
3514 uint i, band_order[2];
3515
Arend van Spriel5b435de2011-10-05 13:19:03 +02003516 /*
3517 * We might have been bandlocked during down and the chip
3518 * power-cycled (hibernate). Figure out the right band to park on
3519 */
3520 if (wlc->bandlocked || wlc->pub->_nbands == 1) {
3521 /* updated in brcms_c_bandlock() */
3522 parkband = wlc->band->bandunit;
3523 band_order[0] = band_order[1] = parkband;
3524 } else {
3525 /* park on the band of the specified chanspec */
3526 parkband = chspec_bandunit(chanspec);
3527
3528 /* order so that parkband initialize last */
3529 band_order[0] = parkband ^ 1;
3530 band_order[1] = parkband;
3531 }
3532
3533 /* make each band operational, software state init */
3534 for (i = 0; i < wlc->pub->_nbands; i++) {
3535 uint j = band_order[i];
3536
3537 wlc->band = wlc->bandstate[j];
3538
3539 brcms_default_rateset(wlc, &default_rateset);
3540
3541 /* fill in hw_rate */
3542 brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
3543 false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
3544 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
3545
3546 /* init basic rate lookup */
3547 brcms_c_rate_lookup_init(wlc, &default_rateset);
3548 }
3549
3550 /* sync up phy/radio chanspec */
3551 brcms_c_set_phy_chanspec(wlc, chanspec);
3552}
3553
Alwin Beukers02a588a2011-11-10 20:30:28 +01003554/*
Alwin Beukersbe667662011-11-22 17:21:43 -08003555 * Set or clear filtering related maccontrol bits based on
3556 * specified filter flags
Alwin Beukers02a588a2011-11-10 20:30:28 +01003557 */
Alwin Beukersbe667662011-11-22 17:21:43 -08003558void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003559{
Alwin Beukers02a588a2011-11-10 20:30:28 +01003560 u32 promisc_bits = 0;
3561
Alwin Beukersbe667662011-11-22 17:21:43 -08003562 wlc->filter_flags = filter_flags;
3563
3564 if (filter_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
3565 promisc_bits |= MCTL_PROMISC;
3566
3567 if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
Alwin Beukers02a588a2011-11-10 20:30:28 +01003568 promisc_bits |= MCTL_BCNS_PROMISC;
3569
Alwin Beukersbe667662011-11-22 17:21:43 -08003570 if (filter_flags & FIF_FCSFAIL)
3571 promisc_bits |= MCTL_KEEPBADFCS;
3572
3573 if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
3574 promisc_bits |= MCTL_KEEPCONTROL;
Alwin Beukers02a588a2011-11-10 20:30:28 +01003575
3576 brcms_b_mctrl(wlc->hw,
Alwin Beukersbe667662011-11-22 17:21:43 -08003577 MCTL_PROMISC | MCTL_BCNS_PROMISC |
3578 MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
3579 promisc_bits);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003580}
3581
Arend van Spriel5b435de2011-10-05 13:19:03 +02003582/*
3583 * ucode, hwmac update
3584 * Channel dependent updates for ucode and hw
3585 */
3586static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
3587{
3588 /* enable or disable any active IBSSs depending on whether or not
3589 * we are on the home channel
3590 */
3591 if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
3592 if (wlc->pub->associated) {
3593 /*
3594 * BMAC_NOTE: This is something that should be fixed
3595 * in ucode inits. I think that the ucode inits set
3596 * up the bcn templates and shm values with a bogus
3597 * beacon. This should not be done in the inits. If
3598 * ucode needs to set up a beacon for testing, the
3599 * test routines should write it down, not expect the
3600 * inits to populate a bogus beacon.
3601 */
3602 if (BRCMS_PHY_11N_CAP(wlc->band))
3603 brcms_b_write_shm(wlc->hw,
3604 M_BCN_TXTSF_OFFSET, 0);
3605 }
3606 } else {
3607 /* disable an active IBSS if we are not on the home channel */
3608 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609}
3610
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003611static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
3612 u8 basic_rate)
3613{
3614 u8 phy_rate, index;
3615 u8 basic_phy_rate, basic_index;
3616 u16 dir_table, basic_table;
3617 u16 basic_ptr;
3618
3619 /* Shared memory address for the table we are reading */
3620 dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
3621
3622 /* Shared memory address for the table we are writing */
3623 basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
3624
3625 /*
3626 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
3627 * the index into the rate table.
3628 */
3629 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
3630 basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
3631 index = phy_rate & 0xf;
3632 basic_index = basic_phy_rate & 0xf;
3633
3634 /* Find the SHM pointer to the ACK rate entry by looking in the
3635 * Direct-map Table
3636 */
3637 basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
3638
3639 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
3640 * to the correct basic rate for the given incoming rate
3641 */
3642 brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
3643}
3644
3645static const struct brcms_c_rateset *
3646brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
3647{
3648 const struct brcms_c_rateset *rs_dflt;
3649
3650 if (BRCMS_PHY_11N_CAP(wlc->band)) {
3651 if (wlc->band->bandtype == BRCM_BAND_5G)
3652 rs_dflt = &ofdm_mimo_rates;
3653 else
3654 rs_dflt = &cck_ofdm_mimo_rates;
3655 } else if (wlc->band->gmode)
3656 rs_dflt = &cck_ofdm_rates;
3657 else
3658 rs_dflt = &cck_rates;
3659
3660 return rs_dflt;
3661}
3662
3663static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
3664{
3665 const struct brcms_c_rateset *rs_dflt;
3666 struct brcms_c_rateset rs;
3667 u8 rate, basic_rate;
3668 uint i;
3669
3670 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
3671
3672 brcms_c_rateset_copy(rs_dflt, &rs);
3673 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
3674
3675 /* walk the phy rate table and update SHM basic rate lookup table */
3676 for (i = 0; i < rs.count; i++) {
3677 rate = rs.rates[i] & BRCMS_RATE_MASK;
3678
3679 /* for a given rate brcms_basic_rate returns the rate at
3680 * which a response ACK/CTS should be sent.
3681 */
3682 basic_rate = brcms_basic_rate(wlc, rate);
3683 if (basic_rate == 0)
3684 /* This should only happen if we are using a
3685 * restricted rateset.
3686 */
3687 basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
3688
3689 brcms_c_write_rate_shm(wlc, rate, basic_rate);
3690 }
3691}
3692
Arend van Spriel5b435de2011-10-05 13:19:03 +02003693/* band-specific init */
3694static void brcms_c_bsinit(struct brcms_c_info *wlc)
3695{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003696 brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
3697 wlc->pub->unit, wlc->band->bandunit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003698
3699 /* write ucode ACK/CTS rate table */
3700 brcms_c_set_ratetable(wlc);
3701
3702 /* update some band specific mac configuration */
3703 brcms_c_ucode_mac_upd(wlc);
3704
3705 /* init antenna selection */
3706 brcms_c_antsel_init(wlc->asi);
3707
3708}
3709
3710/* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
3711static int
3712brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
3713 bool writeToShm)
3714{
3715 int idle_busy_ratio_x_16 = 0;
3716 uint offset =
3717 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
3718 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
3719 if (duty_cycle > 100 || duty_cycle < 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003720 brcms_err(wlc->hw->d11core,
3721 "wl%d: duty cycle value off limit\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003722 wlc->pub->unit);
3723 return -EINVAL;
3724 }
3725 if (duty_cycle)
3726 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
3727 /* Only write to shared memory when wl is up */
3728 if (writeToShm)
3729 brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
3730
3731 if (isOFDM)
3732 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
3733 else
3734 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
3735
3736 return 0;
3737}
3738
Arend van Spriel5b435de2011-10-05 13:19:03 +02003739/* push sw hps and wake state through hardware */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003740static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003741{
3742 u32 v1, v2;
3743 bool hps;
3744 bool awake_before;
3745
3746 hps = brcms_c_ps_allowed(wlc);
3747
Seth Forshee913911f2012-11-15 08:08:04 -06003748 brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
3749 hps);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003750
Arend van Spriel16d28122011-12-08 15:06:51 -08003751 v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003752 v2 = MCTL_WAKE;
3753 if (hps)
3754 v2 |= MCTL_HPS;
3755
3756 brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
3757
3758 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
3759
3760 if (!awake_before)
3761 brcms_b_wait_for_wake(wlc->hw);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003762}
3763
3764/*
3765 * Write this BSS config's MAC address to core.
3766 * Updates RXE match engine.
3767 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003768static int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003769{
3770 int err = 0;
3771 struct brcms_c_info *wlc = bsscfg->wlc;
3772
3773 /* enter the MAC addr into the RXE match registers */
3774 brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, bsscfg->cur_etheraddr);
3775
3776 brcms_c_ampdu_macaddr_upd(wlc);
3777
3778 return err;
3779}
3780
3781/* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
3782 * Updates RXE match engine.
3783 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003784static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003785{
3786 /* we need to update BSSID in RXE match registers */
3787 brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
3788}
3789
3790static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
3791{
3792 wlc_hw->shortslot = shortslot;
3793
3794 if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
3795 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
3796 brcms_b_update_slot_timing(wlc_hw, shortslot);
3797 brcms_c_enable_mac(wlc_hw->wlc);
3798 }
3799}
3800
3801/*
3802 * Suspend the the MAC and update the slot timing
3803 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
3804 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003805static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003806{
3807 /* use the override if it is set */
3808 if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
3809 shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
3810
3811 if (wlc->shortslot == shortslot)
3812 return;
3813
3814 wlc->shortslot = shortslot;
3815
3816 brcms_b_set_shortslot(wlc->hw, shortslot);
3817}
3818
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003819static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003820{
3821 if (wlc->home_chanspec != chanspec) {
3822 wlc->home_chanspec = chanspec;
3823
3824 if (wlc->bsscfg->associated)
3825 wlc->bsscfg->current_bss->chanspec = chanspec;
3826 }
3827}
3828
3829void
3830brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
Roland Vossenc6c44892011-10-21 16:16:26 +02003831 bool mute_tx, struct txpwr_limits *txpwr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003832{
3833 uint bandunit;
3834
Seth Forshee913911f2012-11-15 08:08:04 -06003835 brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
3836 chanspec);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003837
3838 wlc_hw->chanspec = chanspec;
3839
3840 /* Switch bands if necessary */
3841 if (wlc_hw->_nbands > 1) {
3842 bandunit = chspec_bandunit(chanspec);
3843 if (wlc_hw->band->bandunit != bandunit) {
3844 /* brcms_b_setband disables other bandunit,
3845 * use light band switch if not up yet
3846 */
3847 if (wlc_hw->up) {
3848 wlc_phy_chanspec_radio_set(wlc_hw->
3849 bandstate[bandunit]->
3850 pi, chanspec);
3851 brcms_b_setband(wlc_hw, bandunit, chanspec);
3852 } else {
3853 brcms_c_setxband(wlc_hw, bandunit);
3854 }
3855 }
3856 }
3857
Roland Vossenc6c44892011-10-21 16:16:26 +02003858 wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003859
3860 if (!wlc_hw->up) {
3861 if (wlc_hw->clk)
3862 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
3863 chanspec);
3864 wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3865 } else {
3866 wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
3867 wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
3868
3869 /* Update muting of the channel */
Roland Vossenc6c44892011-10-21 16:16:26 +02003870 brcms_b_mute(wlc_hw, mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003871 }
3872}
3873
3874/* switch to and initialize new band */
3875static void brcms_c_setband(struct brcms_c_info *wlc,
3876 uint bandunit)
3877{
3878 wlc->band = wlc->bandstate[bandunit];
3879
3880 if (!wlc->pub->up)
3881 return;
3882
3883 /* wait for at least one beacon before entering sleeping state */
3884 brcms_c_set_ps_ctrl(wlc);
3885
3886 /* band-specific initializations */
3887 brcms_c_bsinit(wlc);
3888}
3889
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02003890static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003891{
3892 uint bandunit;
3893 bool switchband = false;
3894 u16 old_chanspec = wlc->chanspec;
3895
3896 if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003897 brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003898 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
3899 return;
3900 }
3901
3902 /* Switch bands if necessary */
3903 if (wlc->pub->_nbands > 1) {
3904 bandunit = chspec_bandunit(chanspec);
3905 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
3906 switchband = true;
3907 if (wlc->bandlocked) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06003908 brcms_err(wlc->hw->d11core,
3909 "wl%d: %s: chspec %d band is locked!\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003910 wlc->pub->unit, __func__,
3911 CHSPEC_CHANNEL(chanspec));
3912 return;
3913 }
3914 /*
3915 * should the setband call come after the
3916 * brcms_b_chanspec() ? if the setband updates
3917 * (brcms_c_bsinit) use low level calls to inspect and
3918 * set state, the state inspected may be from the wrong
3919 * band, or the following brcms_b_set_chanspec() may
3920 * undo the work.
3921 */
3922 brcms_c_setband(wlc, bandunit);
3923 }
3924 }
3925
3926 /* sync up phy/radio chanspec */
3927 brcms_c_set_phy_chanspec(wlc, chanspec);
3928
3929 /* init antenna selection */
3930 if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
3931 brcms_c_antsel_init(wlc->asi);
3932
3933 /* Fix the hardware rateset based on bw.
3934 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
3935 */
3936 brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
3937 wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
3938 }
3939
3940 /* update some mac configuration since chanspec changed */
3941 brcms_c_ucode_mac_upd(wlc);
3942}
3943
Arend van Spriel5b435de2011-10-05 13:19:03 +02003944/*
3945 * This function changes the phytxctl for beacon based on current
3946 * beacon ratespec AND txant setting as per this table:
3947 * ratespec CCK ant = wlc->stf->txant
3948 * OFDM ant = 3
3949 */
3950void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
3951 u32 bcn_rspec)
3952{
3953 u16 phyctl;
3954 u16 phytxant = wlc->stf->phytxant;
3955 u16 mask = PHY_TXC_ANT_MASK;
3956
3957 /* for non-siso rates or default setting, use the available chains */
3958 if (BRCMS_PHY_11N_CAP(wlc->band))
3959 phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
3960
3961 phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
3962 phyctl = (phyctl & ~mask) | phytxant;
3963 brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
3964}
3965
3966/*
3967 * centralized protection config change function to simplify debugging, no
3968 * consistency checking this should be called only on changes to avoid overhead
3969 * in periodic function
3970 */
3971void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
3972{
Seth Forsheeb353dda2012-11-15 08:08:03 -06003973 /*
3974 * Cannot use brcms_dbg_* here because this function is called
3975 * before wlc is sufficiently initialized.
3976 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003977 BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
3978
3979 switch (idx) {
3980 case BRCMS_PROT_G_SPEC:
3981 wlc->protection->_g = (bool) val;
3982 break;
3983 case BRCMS_PROT_G_OVR:
3984 wlc->protection->g_override = (s8) val;
3985 break;
3986 case BRCMS_PROT_G_USER:
3987 wlc->protection->gmode_user = (u8) val;
3988 break;
3989 case BRCMS_PROT_OVERLAP:
3990 wlc->protection->overlap = (s8) val;
3991 break;
3992 case BRCMS_PROT_N_USER:
3993 wlc->protection->nmode_user = (s8) val;
3994 break;
3995 case BRCMS_PROT_N_CFG:
3996 wlc->protection->n_cfg = (s8) val;
3997 break;
3998 case BRCMS_PROT_N_CFG_OVR:
3999 wlc->protection->n_cfg_override = (s8) val;
4000 break;
4001 case BRCMS_PROT_N_NONGF:
4002 wlc->protection->nongf = (bool) val;
4003 break;
4004 case BRCMS_PROT_N_NONGF_OVR:
4005 wlc->protection->nongf_override = (s8) val;
4006 break;
4007 case BRCMS_PROT_N_PAM_OVR:
4008 wlc->protection->n_pam_override = (s8) val;
4009 break;
4010 case BRCMS_PROT_N_OBSS:
4011 wlc->protection->n_obss = (bool) val;
4012 break;
4013
4014 default:
4015 break;
4016 }
4017
4018}
4019
4020static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
4021{
4022 if (wlc->pub->up) {
4023 brcms_c_update_beacon(wlc);
4024 brcms_c_update_probe_resp(wlc, true);
4025 }
4026}
4027
4028static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
4029{
4030 wlc->stf->ldpc = val;
4031
4032 if (wlc->pub->up) {
4033 brcms_c_update_beacon(wlc);
4034 brcms_c_update_probe_resp(wlc, true);
4035 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
4036 }
4037}
4038
4039void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
4040 const struct ieee80211_tx_queue_params *params,
4041 bool suspend)
4042{
4043 int i;
4044 struct shm_acparams acp_shm;
4045 u16 *shm_entry;
4046
4047 /* Only apply params if the core is out of reset and has clocks */
4048 if (!wlc->clk) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004049 brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
4050 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004051 return;
4052 }
4053
Joe Perchesb56e6812013-02-13 17:33:21 -08004054 memset(&acp_shm, 0, sizeof(struct shm_acparams));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004055 /* fill in shm ac params struct */
4056 acp_shm.txop = params->txop;
4057 /* convert from units of 32us to us for ucode */
4058 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
4059 EDCF_TXOP2USEC(acp_shm.txop);
4060 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
4061
Arend van Sprielb7eec422011-11-10 20:30:18 +01004062 if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
Arend van Spriel5b435de2011-10-05 13:19:03 +02004063 && acp_shm.aifs < EDCF_AIFSN_MAX)
4064 acp_shm.aifs++;
4065
4066 if (acp_shm.aifs < EDCF_AIFSN_MIN
4067 || acp_shm.aifs > EDCF_AIFSN_MAX) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004068 brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
Arend van Spriel5b435de2011-10-05 13:19:03 +02004069 "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
4070 } else {
4071 acp_shm.cwmin = params->cw_min;
4072 acp_shm.cwmax = params->cw_max;
4073 acp_shm.cwcur = acp_shm.cwmin;
4074 acp_shm.bslots =
Arend van Spriel16d28122011-12-08 15:06:51 -08004075 bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
4076 acp_shm.cwcur;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004077 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
4078 /* Indicate the new params to the ucode */
4079 acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
4080 wme_ac2fifo[aci] *
4081 M_EDCF_QLEN +
4082 M_EDCF_STATUS_OFF));
4083 acp_shm.status |= WME_STATUS_NEWAC;
4084
4085 /* Fill in shm acparam table */
4086 shm_entry = (u16 *) &acp_shm;
4087 for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
4088 brcms_b_write_shm(wlc->hw,
4089 M_EDCF_QINFO +
4090 wme_ac2fifo[aci] * M_EDCF_QLEN + i,
4091 *shm_entry++);
4092 }
4093
4094 if (suspend) {
4095 brcms_c_suspend_mac_and_wait(wlc);
4096 brcms_c_enable_mac(wlc);
4097 }
4098}
4099
Arend van Spriel094b1992011-10-18 14:03:07 +02004100static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004101{
4102 u16 aci;
4103 int i_ac;
4104 struct ieee80211_tx_queue_params txq_pars;
4105 static const struct edcf_acparam default_edcf_acparams[] = {
4106 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
4107 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
4108 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
4109 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
4110 }; /* ucode needs these parameters during its initialization */
4111 const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
4112
Arend van Sprielb7eec422011-11-10 20:30:18 +01004113 for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004114 /* find out which ac this set of params applies to */
4115 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
4116
4117 /* fill in shm ac params struct */
4118 txq_pars.txop = edcf_acp->TXOP;
4119 txq_pars.aifs = edcf_acp->ACI;
4120
4121 /* CWmin = 2^(ECWmin) - 1 */
4122 txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
4123 /* CWmax = 2^(ECWmax) - 1 */
4124 txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
4125 >> EDCF_ECWMAX_SHIFT);
4126 brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
4127 }
4128
4129 if (suspend) {
4130 brcms_c_suspend_mac_and_wait(wlc);
4131 brcms_c_enable_mac(wlc);
4132 }
4133}
4134
Arend van Spriel5b435de2011-10-05 13:19:03 +02004135static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
4136{
4137 /* Don't start the timer if HWRADIO feature is disabled */
4138 if (wlc->radio_monitor)
4139 return;
4140
4141 wlc->radio_monitor = true;
4142 brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004143 brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004144}
4145
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004146static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004147{
4148 if (!wlc->radio_monitor)
4149 return true;
4150
4151 wlc->radio_monitor = false;
4152 brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004153 return brcms_del_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004154}
4155
4156/* read hwdisable state and propagate to wlc flag */
4157static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
4158{
4159 if (wlc->pub->hw_off)
4160 return;
4161
4162 if (brcms_b_radio_read_hwdisabled(wlc->hw))
4163 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4164 else
4165 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4166}
4167
Arend van Spriel5b435de2011-10-05 13:19:03 +02004168/* update hwradio status and return it */
4169bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
4170{
4171 brcms_c_radio_hwdisable_upd(wlc);
4172
4173 return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
4174 true : false;
4175}
4176
4177/* periodical query hw radio button while driver is "down" */
4178static void brcms_c_radio_timer(void *arg)
4179{
4180 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4181
4182 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004183 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4184 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004185 brcms_down(wlc->wl);
4186 return;
4187 }
4188
Arend van Spriel5b435de2011-10-05 13:19:03 +02004189 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004190}
4191
4192/* common low-level watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004193static void brcms_b_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004194{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004195 struct brcms_hardware *wlc_hw = wlc->hw;
4196
Arend van Spriel5b435de2011-10-05 13:19:03 +02004197 if (!wlc_hw->up)
4198 return;
4199
4200 /* increment second count */
4201 wlc_hw->now++;
4202
4203 /* Check for FIFO error interrupts */
4204 brcms_b_fifoerrors(wlc_hw);
4205
4206 /* make sure RX dma has buffers */
4207 dma_rxfill(wlc->hw->di[RX_FIFO]);
4208
4209 wlc_phy_watchdog(wlc_hw->band->pi);
4210}
4211
4212/* common watchdog code */
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004213static void brcms_c_watchdog(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004214{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004215 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004216
4217 if (!wlc->pub->up)
4218 return;
4219
4220 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06004221 brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4222 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223 brcms_down(wlc->wl);
4224 return;
4225 }
4226
4227 /* increment second count */
4228 wlc->pub->now++;
4229
Arend van Spriel5b435de2011-10-05 13:19:03 +02004230 brcms_c_radio_hwdisable_upd(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004231 /* if radio is disable, driver may be down, quit here */
4232 if (wlc->pub->radio_disabled)
4233 return;
4234
4235 brcms_b_watchdog(wlc);
4236
4237 /*
4238 * occasionally sample mac stat counters to
4239 * detect 16-bit counter wrap
4240 */
4241 if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
4242 brcms_c_statsupd(wlc);
4243
4244 if (BRCMS_ISNPHY(wlc->band) &&
4245 ((wlc->pub->now - wlc->tempsense_lasttime) >=
4246 BRCMS_TEMPSENSE_PERIOD)) {
4247 wlc->tempsense_lasttime = wlc->pub->now;
4248 brcms_c_tempsense_upd(wlc);
4249 }
4250}
4251
4252static void brcms_c_watchdog_by_timer(void *arg)
4253{
Hauke Mehrtensa5fed0c2012-06-30 15:16:14 +02004254 struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4255
4256 brcms_c_watchdog(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004257}
4258
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004259static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004260{
4261 wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
4262 wlc, "watchdog");
4263 if (!wlc->wdtimer) {
4264 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for wdtimer "
4265 "failed\n", unit);
4266 goto fail;
4267 }
4268
4269 wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
4270 wlc, "radio");
4271 if (!wlc->radio_timer) {
4272 wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for radio_timer "
4273 "failed\n", unit);
4274 goto fail;
4275 }
4276
4277 return true;
4278
4279 fail:
4280 return false;
4281}
4282
4283/*
4284 * Initialize brcms_c_info default values ...
4285 * may get overrides later in this function
4286 */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004287static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004288{
4289 int i;
4290
4291 /* Save our copy of the chanspec */
4292 wlc->chanspec = ch20mhz_chspec(1);
4293
4294 /* various 802.11g modes */
4295 wlc->shortslot = false;
4296 wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
4297
4298 brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
4299 brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
4300
4301 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
4302 BRCMS_PROTECTION_AUTO);
4303 brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
4304 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
4305 BRCMS_PROTECTION_AUTO);
4306 brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
4307 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
4308
4309 brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
4310 BRCMS_PROTECTION_CTL_OVERLAP);
4311
4312 /* 802.11g draft 4.0 NonERP elt advertisement */
4313 wlc->include_legacy_erp = true;
4314
4315 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
4316 wlc->stf->txant = ANT_TX_DEF;
4317
4318 wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
4319
4320 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
4321 for (i = 0; i < NFIFO; i++)
4322 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
4323 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
4324
4325 /* default rate fallback retry limits */
4326 wlc->SFBL = RETRY_SHORT_FB;
4327 wlc->LFBL = RETRY_LONG_FB;
4328
4329 /* default mac retry limits */
4330 wlc->SRL = RETRY_SHORT_DEF;
4331 wlc->LRL = RETRY_LONG_DEF;
4332
4333 /* WME QoS mode is Auto by default */
4334 wlc->pub->_ampdu = AMPDU_AGG_HOST;
4335 wlc->pub->bcmerror = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004336}
4337
4338static uint brcms_c_attach_module(struct brcms_c_info *wlc)
4339{
4340 uint err = 0;
4341 uint unit;
4342 unit = wlc->pub->unit;
4343
4344 wlc->asi = brcms_c_antsel_attach(wlc);
4345 if (wlc->asi == NULL) {
4346 wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
4347 "failed\n", unit);
4348 err = 44;
4349 goto fail;
4350 }
4351
4352 wlc->ampdu = brcms_c_ampdu_attach(wlc);
4353 if (wlc->ampdu == NULL) {
4354 wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
4355 "failed\n", unit);
4356 err = 50;
4357 goto fail;
4358 }
4359
4360 if ((brcms_c_stf_attach(wlc) != 0)) {
4361 wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
4362 "failed\n", unit);
4363 err = 68;
4364 goto fail;
4365 }
4366 fail:
4367 return err;
4368}
4369
4370struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
4371{
4372 return wlc->pub;
4373}
4374
4375/* low level attach
4376 * run backplane attach, init nvram
4377 * run phy attach
4378 * initialize software state for each core and band
4379 * put the whole chip in reset(driver down state), no clock
4380 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08004381static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
4382 uint unit, bool piomode)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004383{
4384 struct brcms_hardware *wlc_hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004385 uint err = 0;
4386 uint j;
4387 bool wme = false;
4388 struct shared_phy_params sha_params;
4389 struct wiphy *wiphy = wlc->wiphy;
Arend van Sprielb63337a2011-12-08 15:06:47 -08004390 struct pci_dev *pcidev = core->bus->host_pci;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004391 struct ssb_sprom *sprom = &core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004392
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004393 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
Seth Forsheeb353dda2012-11-15 08:08:03 -06004394 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4395 pcidev->vendor,
4396 pcidev->device);
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004397 else
Seth Forsheeb353dda2012-11-15 08:08:03 -06004398 brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4399 core->bus->boardinfo.vendor,
4400 core->bus->boardinfo.type);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004401
4402 wme = true;
4403
4404 wlc_hw = wlc->hw;
4405 wlc_hw->wlc = wlc;
4406 wlc_hw->unit = unit;
4407 wlc_hw->band = wlc_hw->bandstate[0];
4408 wlc_hw->_piomode = piomode;
4409
4410 /* populate struct brcms_hardware with default values */
4411 brcms_b_info_init(wlc_hw);
4412
4413 /*
4414 * Do the hardware portion of the attach. Also initialize software
4415 * state that depends on the particular hardware we are running.
4416 */
Arend van Spriel28a53442011-12-08 15:06:49 -08004417 wlc_hw->sih = ai_attach(core->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004418 if (wlc_hw->sih == NULL) {
4419 wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
4420 unit);
4421 err = 11;
4422 goto fail;
4423 }
4424
4425 /* verify again the device is supported */
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02004426 if (!brcms_c_chipmatch(core)) {
4427 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
4428 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004429 err = 12;
4430 goto fail;
4431 }
4432
Hauke Mehrtensa06f2102012-04-29 02:50:42 +02004433 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
4434 wlc_hw->vendorid = pcidev->vendor;
4435 wlc_hw->deviceid = pcidev->device;
4436 } else {
4437 wlc_hw->vendorid = core->bus->boardinfo.vendor;
4438 wlc_hw->deviceid = core->bus->boardinfo.type;
4439 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004440
Arend van Spriel16d28122011-12-08 15:06:51 -08004441 wlc_hw->d11core = core;
4442 wlc_hw->corerev = core->id.rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004443
4444 /* validate chip, chiprev and corerev */
4445 if (!brcms_c_isgoodchip(wlc_hw)) {
4446 err = 13;
4447 goto fail;
4448 }
4449
4450 /* initialize power control registers */
4451 ai_clkctl_init(wlc_hw->sih);
4452
4453 /* request fastclock and force fastclock for the rest of attach
4454 * bring the d11 core out of reset.
4455 * For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
4456 * is still false; But it will be called again inside wlc_corereset,
4457 * after d11 is out of reset.
4458 */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004459 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004460 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4461
4462 if (!brcms_b_validate_chip_access(wlc_hw)) {
4463 wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
4464 "failed\n", unit);
4465 err = 14;
4466 goto fail;
4467 }
4468
4469 /* get the board rev, used just below */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004470 j = sprom->board_rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004471 /* promote srom boardrev of 0xFF to 1 */
4472 if (j == BOARDREV_PROMOTABLE)
4473 j = BOARDREV_PROMOTED;
4474 wlc_hw->boardrev = (u16) j;
4475 if (!brcms_c_validboardtype(wlc_hw)) {
4476 wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004477 "board type (0x%x)" " or revision level (0x%x)\n",
4478 unit, ai_get_boardtype(wlc_hw->sih),
4479 wlc_hw->boardrev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004480 err = 15;
4481 goto fail;
4482 }
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004483 wlc_hw->sromrev = sprom->revision;
4484 wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
4485 wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004486
4487 if (wlc_hw->boardflags & BFL_NOPLLDOWN)
4488 brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
4489
4490 /* check device id(srom, nvram etc.) to set bands */
4491 if (wlc_hw->deviceid == BCM43224_D11N_ID ||
Hauke Mehrtens9dd4ea52012-12-07 17:10:03 +01004492 wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
4493 wlc_hw->deviceid == BCM43224_CHIP_ID)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004494 /* Dualband boards */
4495 wlc_hw->_nbands = 2;
4496 else
4497 wlc_hw->_nbands = 1;
4498
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004499 if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
Arend van Spriel5b435de2011-10-05 13:19:03 +02004500 wlc_hw->_nbands = 1;
4501
4502 /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
4503 * unconditionally does the init of these values
4504 */
4505 wlc->vendorid = wlc_hw->vendorid;
4506 wlc->deviceid = wlc_hw->deviceid;
4507 wlc->pub->sih = wlc_hw->sih;
4508 wlc->pub->corerev = wlc_hw->corerev;
4509 wlc->pub->sromrev = wlc_hw->sromrev;
4510 wlc->pub->boardrev = wlc_hw->boardrev;
4511 wlc->pub->boardflags = wlc_hw->boardflags;
4512 wlc->pub->boardflags2 = wlc_hw->boardflags2;
4513 wlc->pub->_nbands = wlc_hw->_nbands;
4514
4515 wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
4516
4517 if (wlc_hw->physhim == NULL) {
4518 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
4519 "failed\n", unit);
4520 err = 25;
4521 goto fail;
4522 }
4523
4524 /* pass all the parameters to wlc_phy_shared_attach in one struct */
4525 sha_params.sih = wlc_hw->sih;
4526 sha_params.physhim = wlc_hw->physhim;
4527 sha_params.unit = unit;
4528 sha_params.corerev = wlc_hw->corerev;
4529 sha_params.vid = wlc_hw->vendorid;
4530 sha_params.did = wlc_hw->deviceid;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004531 sha_params.chip = ai_get_chip_id(wlc_hw->sih);
4532 sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
4533 sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004534 sha_params.sromrev = wlc_hw->sromrev;
Arend van Sprielb2ffec42011-12-08 15:06:45 -08004535 sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004536 sha_params.boardrev = wlc_hw->boardrev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004537 sha_params.boardflags = wlc_hw->boardflags;
4538 sha_params.boardflags2 = wlc_hw->boardflags2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004539
4540 /* alloc and save pointer to shared phy state area */
4541 wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
4542 if (!wlc_hw->phy_sh) {
4543 err = 16;
4544 goto fail;
4545 }
4546
4547 /* initialize software state for each core and band */
4548 for (j = 0; j < wlc_hw->_nbands; j++) {
4549 /*
4550 * band0 is always 2.4Ghz
4551 * band1, if present, is 5Ghz
4552 */
4553
4554 brcms_c_setxband(wlc_hw, j);
4555
4556 wlc_hw->band->bandunit = j;
4557 wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4558 wlc->band->bandunit = j;
4559 wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
Arend van Spriel3b758a62011-12-12 15:15:09 -08004560 wlc->core->coreidx = core->core_index;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004561
Arend van Spriel16d28122011-12-08 15:06:51 -08004562 wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004563 wlc_hw->machwcap_backup = wlc_hw->machwcap;
4564
4565 /* init tx fifo size */
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004566 WARN_ON((wlc_hw->corerev - XMTFIFOTBL_STARTREV) < 0 ||
4567 (wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
4568 ARRAY_SIZE(xmtfifo_sz));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004569 wlc_hw->xmtfifo_sz =
4570 xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
Hauke Mehrtens093cd332012-07-02 20:15:51 +02004571 WARN_ON(!wlc_hw->xmtfifo_sz[0]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004572
4573 /* Get a phy for this band */
4574 wlc_hw->band->pi =
Arend van Spriel4b006b12011-12-08 15:06:54 -08004575 wlc_phy_attach(wlc_hw->phy_sh, core,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004576 wlc_hw->band->bandtype,
4577 wlc->wiphy);
4578 if (wlc_hw->band->pi == NULL) {
4579 wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
4580 "attach failed\n", unit);
4581 err = 17;
4582 goto fail;
4583 }
4584
4585 wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
4586
4587 wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
4588 &wlc_hw->band->phyrev,
4589 &wlc_hw->band->radioid,
4590 &wlc_hw->band->radiorev);
4591 wlc_hw->band->abgphy_encore =
4592 wlc_phy_get_encore(wlc_hw->band->pi);
4593 wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
4594 wlc_hw->band->core_flags =
4595 wlc_phy_get_coreflags(wlc_hw->band->pi);
4596
4597 /* verify good phy_type & supported phy revision */
4598 if (BRCMS_ISNPHY(wlc_hw->band)) {
4599 if (NCONF_HAS(wlc_hw->band->phyrev))
4600 goto good_phy;
4601 else
4602 goto bad_phy;
4603 } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
4604 if (LCNCONF_HAS(wlc_hw->band->phyrev))
4605 goto good_phy;
4606 else
4607 goto bad_phy;
4608 } else {
4609 bad_phy:
4610 wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
4611 "phy type/rev (%d/%d)\n", unit,
4612 wlc_hw->band->phytype, wlc_hw->band->phyrev);
4613 err = 18;
4614 goto fail;
4615 }
4616
4617 good_phy:
4618 /*
4619 * BMAC_NOTE: wlc->band->pi should not be set below and should
4620 * be done in the high level attach. However we can not make
4621 * that change until all low level access is changed to
4622 * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
4623 * keeping wlc_hw->band->pi as well for incremental update of
4624 * low level fns, and cut over low only init when all fns
4625 * updated.
4626 */
4627 wlc->band->pi = wlc_hw->band->pi;
4628 wlc->band->phytype = wlc_hw->band->phytype;
4629 wlc->band->phyrev = wlc_hw->band->phyrev;
4630 wlc->band->radioid = wlc_hw->band->radioid;
4631 wlc->band->radiorev = wlc_hw->band->radiorev;
4632
4633 /* default contention windows size limits */
4634 wlc_hw->band->CWmin = APHY_CWMIN;
4635 wlc_hw->band->CWmax = PHY_CWMAX;
4636
4637 if (!brcms_b_attach_dmapio(wlc, j, wme)) {
4638 err = 19;
4639 goto fail;
4640 }
4641 }
4642
4643 /* disable core to match driver "down" state */
4644 brcms_c_coredisable(wlc_hw);
4645
4646 /* Match driver "down" state */
4647 ai_pci_down(wlc_hw->sih);
4648
Arend van Spriel5b435de2011-10-05 13:19:03 +02004649 /* turn off pll and xtal to match driver "down" state */
4650 brcms_b_xtal(wlc_hw, OFF);
4651
4652 /* *******************************************************************
4653 * The hardware is in the DOWN state at this point. D11 core
4654 * or cores are in reset with clocks off, and the board PLLs
4655 * are off if possible.
4656 *
4657 * Beyond this point, wlc->sbclk == false and chip registers
4658 * should not be touched.
4659 *********************************************************************
4660 */
4661
4662 /* init etheraddr state variables */
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004663 brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
4664
4665 if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
Arend van Spriel5b435de2011-10-05 13:19:03 +02004666 is_zero_ether_addr(wlc_hw->etheraddr)) {
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004667 wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
4668 unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004669 err = 22;
4670 goto fail;
4671 }
4672
Seth Forsheeb353dda2012-11-15 08:08:03 -06004673 brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
4674 wlc_hw->deviceid, wlc_hw->_nbands,
4675 ai_get_boardtype(wlc_hw->sih));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004676
4677 return err;
4678
4679 fail:
4680 wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
4681 err);
4682 return err;
4683}
4684
4685static void brcms_c_attach_antgain_init(struct brcms_c_info *wlc)
4686{
4687 uint unit;
4688 unit = wlc->pub->unit;
4689
4690 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
4691 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
4692 wlc->band->antgain = 8;
4693 } else if (wlc->band->antgain == -1) {
4694 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4695 " srom, using 2dB\n", unit, __func__);
4696 wlc->band->antgain = 8;
4697 } else {
4698 s8 gain, fract;
4699 /* Older sroms specified gain in whole dbm only. In order
4700 * be able to specify qdbm granularity and remain backward
4701 * compatible the whole dbms are now encoded in only
4702 * low 6 bits and remaining qdbms are encoded in the hi 2 bits.
4703 * 6 bit signed number ranges from -32 - 31.
4704 *
4705 * Examples:
4706 * 0x1 = 1 db,
4707 * 0xc1 = 1.75 db (1 + 3 quarters),
4708 * 0x3f = -1 (-1 + 0 quarters),
4709 * 0x7f = -.75 (-1 + 1 quarters) = -3 qdbm.
4710 * 0xbf = -.50 (-1 + 2 quarters) = -2 qdbm.
4711 */
4712 gain = wlc->band->antgain & 0x3f;
4713 gain <<= 2; /* Sign extend */
4714 gain >>= 2;
4715 fract = (wlc->band->antgain & 0xc0) >> 6;
4716 wlc->band->antgain = 4 * gain + fract;
4717 }
4718}
4719
4720static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
4721{
4722 int aa;
4723 uint unit;
4724 int bandtype;
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004725 struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004726
4727 unit = wlc->pub->unit;
4728 bandtype = wlc->band->bandtype;
4729
4730 /* get antennas available */
4731 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004732 aa = sprom->ant_available_a;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004733 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004734 aa = sprom->ant_available_bg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004735
4736 if ((aa < 1) || (aa > 15)) {
4737 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4738 " srom (0x%x), using 3\n", unit, __func__, aa);
4739 aa = 3;
4740 }
4741
4742 /* reset the defaults if we have a single antenna */
4743 if (aa == 1) {
4744 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
4745 wlc->stf->txant = ANT_TX_FORCE_0;
4746 } else if (aa == 2) {
4747 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
4748 wlc->stf->txant = ANT_TX_FORCE_1;
4749 } else {
4750 }
4751
4752 /* Compute Antenna Gain */
4753 if (bandtype == BRCM_BAND_5G)
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004754 wlc->band->antgain = sprom->antenna_gain.a1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004755 else
Hauke Mehrtens898d3c32012-04-29 02:50:25 +02004756 wlc->band->antgain = sprom->antenna_gain.a0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004757
4758 brcms_c_attach_antgain_init(wlc);
4759
4760 return true;
4761}
4762
4763static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
4764{
4765 u16 chanspec;
4766 struct brcms_band *band;
4767 struct brcms_bss_info *bi = wlc->default_bss;
4768
4769 /* init default and target BSS with some sane initial values */
Joe Perchesb56e6812013-02-13 17:33:21 -08004770 memset(bi, 0, sizeof(*bi));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004771 bi->beacon_period = BEACON_INTERVAL_DEFAULT;
4772
4773 /* fill the default channel as the first valid channel
4774 * starting from the 2G channels
4775 */
4776 chanspec = ch20mhz_chspec(1);
4777 wlc->home_chanspec = bi->chanspec = chanspec;
4778
4779 /* find the band of our default channel */
4780 band = wlc->band;
4781 if (wlc->pub->_nbands > 1 &&
4782 band->bandunit != chspec_bandunit(chanspec))
4783 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
4784
4785 /* init bss rates to the band specific default rate set */
4786 brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
4787 band->bandtype, false, BRCMS_RATE_MASK_FULL,
4788 (bool) (wlc->pub->_n_enab & SUPPORT_11N),
4789 brcms_chspec_bw(chanspec), wlc->stf->txstreams);
4790
4791 if (wlc->pub->_n_enab & SUPPORT_11N)
4792 bi->flags |= BRCMS_BSS_HT;
4793}
4794
Arend van Spriel5b435de2011-10-05 13:19:03 +02004795static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
4796{
4797 uint i;
4798 struct brcms_band *band;
4799
4800 for (i = 0; i < wlc->pub->_nbands; i++) {
4801 band = wlc->bandstate[i];
4802 if (band->bandtype == BRCM_BAND_5G) {
4803 if ((bwcap == BRCMS_N_BW_40ALL)
4804 || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
4805 band->mimo_cap_40 = true;
4806 else
4807 band->mimo_cap_40 = false;
4808 } else {
4809 if (bwcap == BRCMS_N_BW_40ALL)
4810 band->mimo_cap_40 = true;
4811 else
4812 band->mimo_cap_40 = false;
4813 }
4814 }
4815}
4816
Arend van Spriel5b435de2011-10-05 13:19:03 +02004817static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
4818{
4819 /* free timer state */
4820 if (wlc->wdtimer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004821 brcms_free_timer(wlc->wdtimer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004822 wlc->wdtimer = NULL;
4823 }
4824 if (wlc->radio_timer) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02004825 brcms_free_timer(wlc->radio_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004826 wlc->radio_timer = NULL;
4827 }
4828}
4829
4830static void brcms_c_detach_module(struct brcms_c_info *wlc)
4831{
4832 if (wlc->asi) {
4833 brcms_c_antsel_detach(wlc->asi);
4834 wlc->asi = NULL;
4835 }
4836
4837 if (wlc->ampdu) {
4838 brcms_c_ampdu_detach(wlc->ampdu);
4839 wlc->ampdu = NULL;
4840 }
4841
4842 brcms_c_stf_detach(wlc);
4843}
4844
4845/*
4846 * low level detach
4847 */
4848static int brcms_b_detach(struct brcms_c_info *wlc)
4849{
4850 uint i;
4851 struct brcms_hw_band *band;
4852 struct brcms_hardware *wlc_hw = wlc->hw;
4853 int callbacks;
4854
4855 callbacks = 0;
4856
Arend van Spriel5b435de2011-10-05 13:19:03 +02004857 brcms_b_detach_dmapio(wlc_hw);
4858
4859 band = wlc_hw->band;
4860 for (i = 0; i < wlc_hw->_nbands; i++) {
4861 if (band->pi) {
4862 /* Detach this band's phy */
4863 wlc_phy_detach(band->pi);
4864 band->pi = NULL;
4865 }
4866 band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
4867 }
4868
4869 /* Free shared phy state */
4870 kfree(wlc_hw->phy_sh);
4871
4872 wlc_phy_shim_detach(wlc_hw->physhim);
4873
4874 if (wlc_hw->sih) {
4875 ai_detach(wlc_hw->sih);
4876 wlc_hw->sih = NULL;
4877 }
4878
4879 return callbacks;
4880
4881}
4882
4883/*
4884 * Return a count of the number of driver callbacks still pending.
4885 *
4886 * General policy is that brcms_c_detach can only dealloc/free software states.
4887 * It can NOT touch hardware registers since the d11core may be in reset and
4888 * clock may not be available.
4889 * One exception is sb register access, which is possible if crystal is turned
4890 * on after "down" state, driver should avoid software timer with the exception
4891 * of radio_monitor.
4892 */
4893uint brcms_c_detach(struct brcms_c_info *wlc)
4894{
4895 uint callbacks = 0;
4896
4897 if (wlc == NULL)
4898 return 0;
4899
Arend van Spriel5b435de2011-10-05 13:19:03 +02004900 callbacks += brcms_b_detach(wlc);
4901
4902 /* delete software timers */
4903 if (!brcms_c_radio_monitor_stop(wlc))
4904 callbacks++;
4905
4906 brcms_c_channel_mgr_detach(wlc->cmi);
4907
4908 brcms_c_timers_deinit(wlc);
4909
4910 brcms_c_detach_module(wlc);
4911
Arend van Spriel5b435de2011-10-05 13:19:03 +02004912 brcms_c_detach_mfree(wlc);
4913 return callbacks;
4914}
4915
4916/* update state that depends on the current value of "ap" */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02004917static void brcms_c_ap_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004918{
4919 /* STA-BSS; short capable */
4920 wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004921}
4922
Arend van Spriel5b435de2011-10-05 13:19:03 +02004923/* Initialize just the hardware when coming out of POR or S3/S5 system states */
4924static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
4925{
4926 if (wlc_hw->wlc->pub->hw_up)
4927 return;
4928
Seth Forsheeb353dda2012-11-15 08:08:03 -06004929 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004930
4931 /*
4932 * Enable pll and xtal, initialize the power control registers,
4933 * and force fastclock for the remainder of brcms_c_up().
4934 */
4935 brcms_b_xtal(wlc_hw, ON);
4936 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004937 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004938
Arend van Spriel5b435de2011-10-05 13:19:03 +02004939 /*
Arend van Spriel3b758a62011-12-12 15:15:09 -08004940 * TODO: test suspend/resume
4941 *
Arend van Spriel5b435de2011-10-05 13:19:03 +02004942 * AI chip doesn't restore bar0win2 on
4943 * hibernation/resume, need sw fixup
4944 */
Arend van Spriel5b435de2011-10-05 13:19:03 +02004945
4946 /*
4947 * Inform phy that a POR reset has occurred so
4948 * it does a complete phy init
4949 */
4950 wlc_phy_por_inform(wlc_hw->band->pi);
4951
4952 wlc_hw->ucode_loaded = false;
4953 wlc_hw->wlc->pub->hw_up = true;
4954
4955 if ((wlc_hw->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02004956 && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004957 if (!
4958 (wlc_hw->boardrev >= 0x1250
4959 && (wlc_hw->boardflags & BFL_FEM_BT)))
4960 ai_epa_4313war(wlc_hw->sih);
4961 }
4962}
4963
4964static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
4965{
Seth Forsheeb353dda2012-11-15 08:08:03 -06004966 brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004967
4968 /*
4969 * Enable pll and xtal, initialize the power control registers,
4970 * and force fastclock for the remainder of brcms_c_up().
4971 */
4972 brcms_b_xtal(wlc_hw, ON);
4973 ai_clkctl_init(wlc_hw->sih);
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02004974 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004975
4976 /*
4977 * Configure pci/pcmcia here instead of in brcms_c_attach()
4978 * to allow mfg hotswap: down, hotswap (chip power cycle), up.
4979 */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004980 bcma_core_pci_irq_ctl(&wlc_hw->d11core->bus->drv_pci[0], wlc_hw->d11core,
Hauke Mehrtensb30ee752012-04-29 02:50:32 +02004981 true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004982
4983 /*
4984 * Need to read the hwradio status here to cover the case where the
4985 * system is loaded with the hw radio disabled. We do not want to
4986 * bring the driver up in this case.
4987 */
4988 if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
4989 /* put SB PCI in down state again */
4990 ai_pci_down(wlc_hw->sih);
4991 brcms_b_xtal(wlc_hw, OFF);
4992 return -ENOMEDIUM;
4993 }
4994
4995 ai_pci_up(wlc_hw->sih);
4996
4997 /* reset the d11 core */
4998 brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4999
5000 return 0;
5001}
5002
5003static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
5004{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005005 wlc_hw->up = true;
5006 wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
5007
5008 /* FULLY enable dynamic power control and d11 core interrupt */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005009 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005010 brcms_intrson(wlc_hw->wlc->wl);
5011 return 0;
5012}
5013
5014/*
5015 * Write WME tunable parameters for retransmit/max rate
5016 * from wlc struct to ucode
5017 */
5018static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
5019{
5020 int ac;
5021
5022 /* Need clock to do this */
5023 if (!wlc->clk)
5024 return;
5025
Arend van Sprielb7eec422011-11-10 20:30:18 +01005026 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005027 brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
5028 wlc->wme_retries[ac]);
5029}
5030
5031/* make interface operational */
5032int brcms_c_up(struct brcms_c_info *wlc)
5033{
Seth Forshee91691292012-06-16 07:47:49 -05005034 struct ieee80211_channel *ch;
5035
Seth Forsheeb353dda2012-11-15 08:08:03 -06005036 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005037
5038 /* HW is turned off so don't try to access it */
5039 if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
5040 return -ENOMEDIUM;
5041
5042 if (!wlc->pub->hw_up) {
5043 brcms_b_hw_up(wlc->hw);
5044 wlc->pub->hw_up = true;
5045 }
5046
5047 if ((wlc->pub->boardflags & BFL_FEM)
Hauke Mehrtens1ef1a572012-06-30 15:16:13 +02005048 && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005049 if (wlc->pub->boardrev >= 0x1250
5050 && (wlc->pub->boardflags & BFL_FEM_BT))
5051 brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
5052 MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
5053 else
5054 brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
5055 MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
5056 }
5057
5058 /*
5059 * Need to read the hwradio status here to cover the case where the
5060 * system is loaded with the hw radio disabled. We do not want to bring
5061 * the driver up in this case. If radio is disabled, abort up, lower
5062 * power, start radio timer and return 0(for NDIS) don't call
5063 * radio_update to avoid looping brcms_c_up.
5064 *
5065 * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
5066 */
5067 if (!wlc->pub->radio_disabled) {
5068 int status = brcms_b_up_prep(wlc->hw);
5069 if (status == -ENOMEDIUM) {
5070 if (!mboolisset
5071 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
5072 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
5073 mboolset(wlc->pub->radio_disabled,
5074 WL_RADIO_HW_DISABLE);
5075
5076 if (bsscfg->enable && bsscfg->BSS)
Seth Forsheeb353dda2012-11-15 08:08:03 -06005077 brcms_err(wlc->hw->d11core,
5078 "wl%d: up: rfdisable -> "
Arend van Spriel5b435de2011-10-05 13:19:03 +02005079 "bsscfg_disable()\n",
5080 wlc->pub->unit);
5081 }
5082 }
5083 }
5084
5085 if (wlc->pub->radio_disabled) {
5086 brcms_c_radio_monitor_start(wlc);
5087 return 0;
5088 }
5089
5090 /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
5091 wlc->clk = true;
5092
5093 brcms_c_radio_monitor_stop(wlc);
5094
5095 /* Set EDCF hostflags */
5096 brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
5097
5098 brcms_init(wlc->wl);
5099 wlc->pub->up = true;
5100
5101 if (wlc->bandinit_pending) {
Seth Forshee91691292012-06-16 07:47:49 -05005102 ch = wlc->pub->ieee_hw->conf.channel;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005103 brcms_c_suspend_mac_and_wait(wlc);
Seth Forshee91691292012-06-16 07:47:49 -05005104 brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005105 wlc->bandinit_pending = false;
5106 brcms_c_enable_mac(wlc);
5107 }
5108
5109 brcms_b_up_finish(wlc->hw);
5110
5111 /* Program the TX wme params with the current settings */
5112 brcms_c_wme_retries_write(wlc);
5113
5114 /* start one second watchdog timer */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005115 brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005116 wlc->WDarmed = true;
5117
5118 /* ensure antenna config is up to date */
5119 brcms_c_stf_phy_txant_upd(wlc);
5120 /* ensure LDPC config is in sync */
5121 brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
5122
5123 return 0;
5124}
5125
5126static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
5127{
5128 uint callbacks = 0;
5129
5130 return callbacks;
5131}
5132
5133static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
5134{
5135 bool dev_gone;
5136 uint callbacks = 0;
5137
Arend van Spriel5b435de2011-10-05 13:19:03 +02005138 if (!wlc_hw->up)
5139 return callbacks;
5140
5141 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5142
5143 /* disable interrupts */
5144 if (dev_gone)
5145 wlc_hw->wlc->macintmask = 0;
5146 else {
5147 /* now disable interrupts */
5148 brcms_intrsoff(wlc_hw->wlc->wl);
5149
5150 /* ensure we're running on the pll clock again */
Hauke Mehrtens712e3c12012-04-29 02:50:35 +02005151 brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005152 }
5153 /* down phy at the last of this stage */
5154 callbacks += wlc_phy_down(wlc_hw->band->pi);
5155
5156 return callbacks;
5157}
5158
5159static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
5160{
5161 uint callbacks = 0;
5162 bool dev_gone;
5163
Arend van Spriel5b435de2011-10-05 13:19:03 +02005164 if (!wlc_hw->up)
5165 return callbacks;
5166
5167 wlc_hw->up = false;
5168 wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
5169
5170 dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5171
5172 if (dev_gone) {
5173 wlc_hw->sbclk = false;
5174 wlc_hw->clk = false;
5175 wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
5176
5177 /* reclaim any posted packets */
5178 brcms_c_flushqueues(wlc_hw->wlc);
5179 } else {
5180
5181 /* Reset and disable the core */
Arend van Spriela8779e42011-12-08 15:06:58 -08005182 if (bcma_core_is_enabled(wlc_hw->d11core)) {
Arend van Spriel16d28122011-12-08 15:06:51 -08005183 if (bcma_read32(wlc_hw->d11core,
5184 D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005185 brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
5186 callbacks += brcms_reset(wlc_hw->wlc->wl);
5187 brcms_c_coredisable(wlc_hw);
5188 }
5189
5190 /* turn off primary xtal and pll */
5191 if (!wlc_hw->noreset) {
5192 ai_pci_down(wlc_hw->sih);
5193 brcms_b_xtal(wlc_hw, OFF);
5194 }
5195 }
5196
5197 return callbacks;
5198}
5199
5200/*
5201 * Mark the interface nonoperational, stop the software mechanisms,
5202 * disable the hardware, free any transient buffer state.
5203 * Return a count of the number of driver callbacks still pending.
5204 */
5205uint brcms_c_down(struct brcms_c_info *wlc)
5206{
5207
5208 uint callbacks = 0;
5209 int i;
5210 bool dev_gone = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005211
Seth Forsheeb353dda2012-11-15 08:08:03 -06005212 brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005213
5214 /* check if we are already in the going down path */
5215 if (wlc->going_down) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005216 brcms_err(wlc->hw->d11core,
5217 "wl%d: %s: Driver going down so return\n",
5218 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005219 return 0;
5220 }
5221 if (!wlc->pub->up)
5222 return callbacks;
5223
Arend van Spriel5b435de2011-10-05 13:19:03 +02005224 wlc->going_down = true;
5225
5226 callbacks += brcms_b_bmac_down_prep(wlc->hw);
5227
5228 dev_gone = brcms_deviceremoved(wlc);
5229
5230 /* Call any registered down handlers */
5231 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5232 if (wlc->modulecb[i].down_fn)
5233 callbacks +=
5234 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
5235 }
5236
5237 /* cancel the watchdog timer */
5238 if (wlc->WDarmed) {
Roland Vossenbe69c4e2011-10-12 20:51:11 +02005239 if (!brcms_del_timer(wlc->wdtimer))
Arend van Spriel5b435de2011-10-05 13:19:03 +02005240 callbacks++;
5241 wlc->WDarmed = false;
5242 }
5243 /* cancel all other timers */
5244 callbacks += brcms_c_down_del_timer(wlc);
5245
5246 wlc->pub->up = false;
5247
5248 wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
5249
Arend van Spriel5b435de2011-10-05 13:19:03 +02005250 callbacks += brcms_b_down_finish(wlc->hw);
5251
5252 /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
5253 wlc->clk = false;
5254
5255 wlc->going_down = false;
5256 return callbacks;
5257}
5258
5259/* Set the current gmode configuration */
5260int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
5261{
5262 int ret = 0;
5263 uint i;
5264 struct brcms_c_rateset rs;
5265 /* Default to 54g Auto */
5266 /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
5267 s8 shortslot = BRCMS_SHORTSLOT_AUTO;
5268 bool shortslot_restrict = false; /* Restrict association to stations
5269 * that support shortslot
5270 */
5271 bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
5272 /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
5273 int preamble = BRCMS_PLCP_LONG;
5274 bool preamble_restrict = false; /* Restrict association to stations
5275 * that support short preambles
5276 */
5277 struct brcms_band *band;
5278
5279 /* if N-support is enabled, allow Gmode set as long as requested
5280 * Gmode is not GMODE_LEGACY_B
5281 */
5282 if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
5283 return -ENOTSUPP;
5284
5285 /* verify that we are dealing with 2G band and grab the band pointer */
5286 if (wlc->band->bandtype == BRCM_BAND_2G)
5287 band = wlc->band;
5288 else if ((wlc->pub->_nbands > 1) &&
5289 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
5290 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5291 else
5292 return -EINVAL;
5293
Arend van Spriel5b435de2011-10-05 13:19:03 +02005294 /* update configuration value */
Joe Perches23677ce2012-02-09 11:17:23 +00005295 if (config)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005296 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
5297
5298 /* Clear rateset override */
Joe Perchesb56e6812013-02-13 17:33:21 -08005299 memset(&rs, 0, sizeof(rs));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005300
5301 switch (gmode) {
5302 case GMODE_LEGACY_B:
5303 shortslot = BRCMS_SHORTSLOT_OFF;
5304 brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
5305
5306 break;
5307
5308 case GMODE_LRS:
5309 break;
5310
5311 case GMODE_AUTO:
5312 /* Accept defaults */
5313 break;
5314
5315 case GMODE_ONLY:
5316 ofdm_basic = true;
5317 preamble = BRCMS_PLCP_SHORT;
5318 preamble_restrict = true;
5319 break;
5320
5321 case GMODE_PERFORMANCE:
5322 shortslot = BRCMS_SHORTSLOT_ON;
5323 shortslot_restrict = true;
5324 ofdm_basic = true;
5325 preamble = BRCMS_PLCP_SHORT;
5326 preamble_restrict = true;
5327 break;
5328
5329 default:
5330 /* Error */
Seth Forsheeb353dda2012-11-15 08:08:03 -06005331 brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005332 wlc->pub->unit, __func__, gmode);
5333 return -ENOTSUPP;
5334 }
5335
5336 band->gmode = gmode;
5337
5338 wlc->shortslot_override = shortslot;
5339
5340 /* Use the default 11g rateset */
5341 if (!rs.count)
5342 brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
5343
5344 if (ofdm_basic) {
5345 for (i = 0; i < rs.count; i++) {
5346 if (rs.rates[i] == BRCM_RATE_6M
5347 || rs.rates[i] == BRCM_RATE_12M
5348 || rs.rates[i] == BRCM_RATE_24M)
5349 rs.rates[i] |= BRCMS_RATE_FLAG;
5350 }
5351 }
5352
5353 /* Set default bss rateset */
5354 wlc->default_bss->rateset.count = rs.count;
5355 memcpy(wlc->default_bss->rateset.rates, rs.rates,
5356 sizeof(wlc->default_bss->rateset.rates));
5357
5358 return ret;
5359}
5360
5361int brcms_c_set_nmode(struct brcms_c_info *wlc)
5362{
5363 uint i;
5364 s32 nmode = AUTO;
5365
5366 if (wlc->stf->txstreams == WL_11N_3x3)
5367 nmode = WL_11N_3x3;
5368 else
5369 nmode = WL_11N_2x2;
5370
5371 /* force GMODE_AUTO if NMODE is ON */
5372 brcms_c_set_gmode(wlc, GMODE_AUTO, true);
5373 if (nmode == WL_11N_3x3)
5374 wlc->pub->_n_enab = SUPPORT_HT;
5375 else
5376 wlc->pub->_n_enab = SUPPORT_11N;
5377 wlc->default_bss->flags |= BRCMS_BSS_HT;
5378 /* add the mcs rates to the default and hw ratesets */
5379 brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
5380 wlc->stf->txstreams);
5381 for (i = 0; i < wlc->pub->_nbands; i++)
5382 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
5383 wlc->default_bss->rateset.mcs, MCSSET_LEN);
5384
5385 return 0;
5386}
5387
5388static int
5389brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
5390 struct brcms_c_rateset *rs_arg)
5391{
5392 struct brcms_c_rateset rs, new;
5393 uint bandunit;
5394
5395 memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
5396
5397 /* check for bad count value */
5398 if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
5399 return -EINVAL;
5400
5401 /* try the current band */
5402 bandunit = wlc->band->bandunit;
5403 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5404 if (brcms_c_rate_hwrs_filter_sort_validate
5405 (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
5406 wlc->stf->txstreams))
5407 goto good;
5408
5409 /* try the other band */
5410 if (brcms_is_mband_unlocked(wlc)) {
5411 bandunit = OTHERBANDUNIT(wlc);
5412 memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5413 if (brcms_c_rate_hwrs_filter_sort_validate(&new,
5414 &wlc->
5415 bandstate[bandunit]->
5416 hw_rateset, true,
5417 wlc->stf->txstreams))
5418 goto good;
5419 }
5420
5421 return -EBADE;
5422
5423 good:
5424 /* apply new rateset */
5425 memcpy(&wlc->default_bss->rateset, &new,
5426 sizeof(struct brcms_c_rateset));
5427 memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
5428 sizeof(struct brcms_c_rateset));
5429 return 0;
5430}
5431
5432static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
5433{
5434 u8 r;
5435 bool war = false;
5436
5437 if (wlc->bsscfg->associated)
5438 r = wlc->bsscfg->current_bss->rateset.rates[0];
5439 else
5440 r = wlc->default_bss->rateset.rates[0];
5441
5442 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
5443}
5444
5445int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
5446{
5447 u16 chspec = ch20mhz_chspec(channel);
5448
5449 if (channel < 0 || channel > MAXCHANNEL)
5450 return -EINVAL;
5451
5452 if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
5453 return -EINVAL;
5454
5455
5456 if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
5457 if (wlc->band->bandunit != chspec_bandunit(chspec))
5458 wlc->bandinit_pending = true;
5459 else
5460 wlc->bandinit_pending = false;
5461 }
5462
5463 wlc->default_bss->chanspec = chspec;
5464 /* brcms_c_BSSinit() will sanitize the rateset before
5465 * using it.. */
5466 if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
5467 brcms_c_set_home_chanspec(wlc, chspec);
5468 brcms_c_suspend_mac_and_wait(wlc);
5469 brcms_c_set_chanspec(wlc, chspec);
5470 brcms_c_enable_mac(wlc);
5471 }
5472 return 0;
5473}
5474
5475int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
5476{
5477 int ac;
5478
5479 if (srl < 1 || srl > RETRY_SHORT_MAX ||
5480 lrl < 1 || lrl > RETRY_SHORT_MAX)
5481 return -EINVAL;
5482
5483 wlc->SRL = srl;
5484 wlc->LRL = lrl;
5485
5486 brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
5487
Arend van Sprielb7eec422011-11-10 20:30:18 +01005488 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02005489 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5490 EDCF_SHORT, wlc->SRL);
5491 wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
5492 EDCF_LONG, wlc->LRL);
5493 }
5494 brcms_c_wme_retries_write(wlc);
5495
5496 return 0;
5497}
5498
5499void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
5500 struct brcm_rateset *currs)
5501{
5502 struct brcms_c_rateset *rs;
5503
5504 if (wlc->pub->associated)
5505 rs = &wlc->bsscfg->current_bss->rateset;
5506 else
5507 rs = &wlc->default_bss->rateset;
5508
5509 /* Copy only legacy rateset section */
5510 currs->count = rs->count;
5511 memcpy(&currs->rates, &rs->rates, rs->count);
5512}
5513
5514int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
5515{
5516 struct brcms_c_rateset internal_rs;
5517 int bcmerror;
5518
5519 if (rs->count > BRCMS_NUMRATES)
5520 return -ENOBUFS;
5521
Joe Perchesb56e6812013-02-13 17:33:21 -08005522 memset(&internal_rs, 0, sizeof(internal_rs));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005523
5524 /* Copy only legacy rateset section */
5525 internal_rs.count = rs->count;
5526 memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
5527
5528 /* merge rateset coming in with the current mcsset */
5529 if (wlc->pub->_n_enab & SUPPORT_11N) {
5530 struct brcms_bss_info *mcsset_bss;
5531 if (wlc->bsscfg->associated)
5532 mcsset_bss = wlc->bsscfg->current_bss;
5533 else
5534 mcsset_bss = wlc->default_bss;
5535 memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
5536 MCSSET_LEN);
5537 }
5538
5539 bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
5540 if (!bcmerror)
5541 brcms_c_ofdm_rateset_war(wlc);
5542
5543 return bcmerror;
5544}
5545
5546int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
5547{
Tim Gardner708eb542013-02-07 12:35:35 -07005548 if (period == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005549 return -EINVAL;
5550
5551 wlc->default_bss->beacon_period = period;
5552 return 0;
5553}
5554
5555u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
5556{
5557 return wlc->band->phytype;
5558}
5559
5560void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
5561{
5562 wlc->shortslot_override = sslot_override;
5563
5564 /*
5565 * shortslot is an 11g feature, so no more work if we are
5566 * currently on the 5G band
5567 */
5568 if (wlc->band->bandtype == BRCM_BAND_5G)
5569 return;
5570
5571 if (wlc->pub->up && wlc->pub->associated) {
5572 /* let watchdog or beacon processing update shortslot */
5573 } else if (wlc->pub->up) {
5574 /* unassociated shortslot is off */
5575 brcms_c_switch_shortslot(wlc, false);
5576 } else {
5577 /* driver is down, so just update the brcms_c_info
5578 * value */
5579 if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
5580 wlc->shortslot = false;
5581 else
5582 wlc->shortslot =
5583 (wlc->shortslot_override ==
5584 BRCMS_SHORTSLOT_ON);
5585 }
5586}
5587
5588/*
5589 * register watchdog and down handlers.
5590 */
5591int brcms_c_module_register(struct brcms_pub *pub,
5592 const char *name, struct brcms_info *hdl,
5593 int (*d_fn)(void *handle))
5594{
5595 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5596 int i;
5597
5598 /* find an empty entry and just add, no duplication check! */
5599 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5600 if (wlc->modulecb[i].name[0] == '\0') {
5601 strncpy(wlc->modulecb[i].name, name,
5602 sizeof(wlc->modulecb[i].name) - 1);
5603 wlc->modulecb[i].hdl = hdl;
5604 wlc->modulecb[i].down_fn = d_fn;
5605 return 0;
5606 }
5607 }
5608
5609 return -ENOSR;
5610}
5611
5612/* unregister module callbacks */
5613int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
5614 struct brcms_info *hdl)
5615{
5616 struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5617 int i;
5618
5619 if (wlc == NULL)
5620 return -ENODATA;
5621
5622 for (i = 0; i < BRCMS_MAXMODULES; i++) {
5623 if (!strcmp(wlc->modulecb[i].name, name) &&
5624 (wlc->modulecb[i].hdl == hdl)) {
Joe Perchesb56e6812013-02-13 17:33:21 -08005625 memset(&wlc->modulecb[i], 0, sizeof(wlc->modulecb[i]));
Arend van Spriel5b435de2011-10-05 13:19:03 +02005626 return 0;
5627 }
5628 }
5629
5630 /* table not found! */
5631 return -ENODATA;
5632}
5633
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005634static bool brcms_c_chipmatch_pci(struct bcma_device *core)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005635{
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005636 struct pci_dev *pcidev = core->bus->host_pci;
5637 u16 vendor = pcidev->vendor;
5638 u16 device = pcidev->device;
5639
Arend van Spriel5b435de2011-10-05 13:19:03 +02005640 if (vendor != PCI_VENDOR_ID_BROADCOM) {
Joe Perches02f77192012-01-15 00:38:44 -08005641 pr_err("unknown vendor id %04x\n", vendor);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005642 return false;
5643 }
5644
Hauke Mehrtens9dd4ea52012-12-07 17:10:03 +01005645 if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
Arend van Spriel5b435de2011-10-05 13:19:03 +02005646 return true;
5647 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
5648 return true;
5649 if (device == BCM4313_D11N2G_ID)
5650 return true;
5651 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
5652 return true;
5653
Joe Perches02f77192012-01-15 00:38:44 -08005654 pr_err("unknown device id %04x\n", device);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005655 return false;
5656}
5657
Hauke Mehrtenscacaa642012-06-30 15:16:19 +02005658static bool brcms_c_chipmatch_soc(struct bcma_device *core)
5659{
5660 struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
5661
5662 if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
5663 return true;
5664
5665 pr_err("unknown chip id %04x\n", chipinfo->id);
5666 return false;
5667}
5668
5669bool brcms_c_chipmatch(struct bcma_device *core)
5670{
5671 switch (core->bus->hosttype) {
5672 case BCMA_HOSTTYPE_PCI:
5673 return brcms_c_chipmatch_pci(core);
5674 case BCMA_HOSTTYPE_SOC:
5675 return brcms_c_chipmatch_soc(core);
5676 default:
5677 pr_err("unknown host type: %i\n", core->bus->hosttype);
5678 return false;
5679 }
5680}
5681
Arend van Spriel5b435de2011-10-05 13:19:03 +02005682u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
5683{
5684 u16 table_ptr;
5685 u8 phy_rate, index;
5686
5687 /* get the phy specific rate encoding for the PLCP SIGNAL field */
5688 if (is_ofdm_rate(rate))
5689 table_ptr = M_RT_DIRMAP_A;
5690 else
5691 table_ptr = M_RT_DIRMAP_B;
5692
5693 /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
5694 * the index into the rate table.
5695 */
5696 phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
5697 index = phy_rate & 0xf;
5698
5699 /* Find the SHM pointer to the rate table entry by looking in the
5700 * Direct-map Table
5701 */
5702 return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
5703}
5704
Arend van Spriel5b435de2011-10-05 13:19:03 +02005705/*
5706 * bcmc_fid_generate:
5707 * Generate frame ID for a BCMC packet. The frag field is not used
5708 * for MC frames so is used as part of the sequence number.
5709 */
5710static inline u16
5711bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
5712 struct d11txh *txh)
5713{
5714 u16 frameid;
5715
5716 frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
5717 TXFID_QUEUE_MASK);
5718 frameid |=
5719 (((wlc->
5720 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5721 TX_BCMC_FIFO;
5722
5723 return frameid;
5724}
5725
5726static uint
5727brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
5728 u8 preamble_type)
5729{
5730 uint dur = 0;
5731
Arend van Spriel5b435de2011-10-05 13:19:03 +02005732 /*
5733 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5734 * is less than or equal to the rate of the immediately previous
5735 * frame in the FES
5736 */
5737 rspec = brcms_basic_rate(wlc, rspec);
5738 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
5739 dur =
5740 brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5741 (DOT11_ACK_LEN + FCS_LEN));
5742 return dur;
5743}
5744
5745static uint
5746brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
5747 u8 preamble_type)
5748{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005749 return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
5750}
5751
5752static uint
5753brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
5754 u8 preamble_type)
5755{
Arend van Spriel5b435de2011-10-05 13:19:03 +02005756 /*
5757 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5758 * is less than or equal to the rate of the immediately previous
5759 * frame in the FES
5760 */
5761 rspec = brcms_basic_rate(wlc, rspec);
5762 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
5763 return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5764 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
5765 FCS_LEN));
5766}
5767
5768/* brcms_c_compute_frame_dur()
5769 *
5770 * Calculate the 802.11 MAC header DUR field for MPDU
5771 * DUR for a single frame = 1 SIFS + 1 ACK
5772 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5773 *
5774 * rate MPDU rate in unit of 500kbps
5775 * next_frag_len next MPDU length in bytes
5776 * preamble_type use short/GF or long/MM PLCP header
5777 */
5778static u16
5779brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
5780 u8 preamble_type, uint next_frag_len)
5781{
5782 u16 dur, sifs;
5783
5784 sifs = get_sifs(wlc->band);
5785
5786 dur = sifs;
5787 dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
5788
5789 if (next_frag_len) {
5790 /* Double the current DUR to get 2 SIFS + 2 ACKs */
5791 dur *= 2;
5792 /* add another SIFS and the frag time */
5793 dur += sifs;
5794 dur +=
5795 (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
5796 next_frag_len);
5797 }
5798 return dur;
5799}
5800
5801/* The opposite of brcms_c_calc_frame_time */
5802static uint
5803brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
5804 u8 preamble_type, uint dur)
5805{
5806 uint nsyms, mac_len, Ndps, kNdps;
5807 uint rate = rspec2rate(ratespec);
5808
Arend van Spriel5b435de2011-10-05 13:19:03 +02005809 if (is_mcs_rate(ratespec)) {
5810 uint mcs = ratespec & RSPEC_RATE_MASK;
5811 int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
5812 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
5813 /* payload calculation matches that of regular ofdm */
5814 if (wlc->band->bandtype == BRCM_BAND_2G)
5815 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
5816 /* kNdbps = kbps * 4 */
5817 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
5818 rspec_issgi(ratespec)) * 4;
5819 nsyms = dur / APHY_SYMBOL_TIME;
5820 mac_len =
5821 ((nsyms * kNdps) -
5822 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
5823 } else if (is_ofdm_rate(ratespec)) {
5824 dur -= APHY_PREAMBLE_TIME;
5825 dur -= APHY_SIGNAL_TIME;
5826 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
5827 Ndps = rate * 2;
5828 nsyms = dur / APHY_SYMBOL_TIME;
5829 mac_len =
5830 ((nsyms * Ndps) -
5831 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
5832 } else {
5833 if (preamble_type & BRCMS_SHORT_PREAMBLE)
5834 dur -= BPHY_PLCP_SHORT_TIME;
5835 else
5836 dur -= BPHY_PLCP_TIME;
5837 mac_len = dur * rate;
5838 /* divide out factor of 2 in rate (1/2 mbps) */
5839 mac_len = mac_len / 8 / 2;
5840 }
5841 return mac_len;
5842}
5843
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02005844/*
5845 * Return true if the specified rate is supported by the specified band.
5846 * BRCM_BAND_AUTO indicates the current band.
5847 */
5848static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
5849 bool verbose)
5850{
5851 struct brcms_c_rateset *hw_rateset;
5852 uint i;
5853
5854 if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
5855 hw_rateset = &wlc->band->hw_rateset;
5856 else if (wlc->pub->_nbands > 1)
5857 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
5858 else
5859 /* other band specified and we are a single band device */
5860 return false;
5861
5862 /* check if this is a mimo rate */
5863 if (is_mcs_rate(rspec)) {
5864 if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
5865 goto error;
5866
5867 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
5868 }
5869
5870 for (i = 0; i < hw_rateset->count; i++)
5871 if (hw_rateset->rates[i] == rspec2rate(rspec))
5872 return true;
5873 error:
5874 if (verbose)
Seth Forsheeb353dda2012-11-15 08:08:03 -06005875 brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02005876 "not in hw_rateset\n", wlc->pub->unit, rspec);
5877
5878 return false;
5879}
5880
Arend van Spriel5b435de2011-10-05 13:19:03 +02005881static u32
5882mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
5883 u32 int_val)
5884{
Seth Forsheeb353dda2012-11-15 08:08:03 -06005885 struct bcma_device *core = wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02005886 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
5887 u8 rate = int_val & NRATE_RATE_MASK;
5888 u32 rspec;
5889 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
5890 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
5891 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
5892 == NRATE_OVERRIDE_MCS_ONLY);
5893 int bcmerror = 0;
5894
5895 if (!ismcs)
5896 return (u32) rate;
5897
5898 /* validate the combination of rate/mcs/stf is allowed */
5899 if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
5900 /* mcs only allowed when nmode */
5901 if (stf > PHY_TXC1_MODE_SDM) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005902 brcms_err(core, "wl%d: %s: Invalid stf\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005903 wlc->pub->unit, __func__);
5904 bcmerror = -EINVAL;
5905 goto done;
5906 }
5907
5908 /* mcs 32 is a special case, DUP mode 40 only */
5909 if (rate == 32) {
5910 if (!CHSPEC_IS40(wlc->home_chanspec) ||
5911 ((stf != PHY_TXC1_MODE_SISO)
5912 && (stf != PHY_TXC1_MODE_CDD))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005913 brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
5914 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005915 bcmerror = -EINVAL;
5916 goto done;
5917 }
5918 /* mcs > 7 must use stf SDM */
5919 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
5920 /* mcs > 7 must use stf SDM */
5921 if (stf != PHY_TXC1_MODE_SDM) {
Seth Forshee913911f2012-11-15 08:08:04 -06005922 brcms_dbg_mac80211(core, "wl%d: enabling "
5923 "SDM mode for mcs %d\n",
5924 wlc->pub->unit, rate);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005925 stf = PHY_TXC1_MODE_SDM;
5926 }
5927 } else {
5928 /*
5929 * MCS 0-7 may use SISO, CDD, and for
5930 * phy_rev >= 3 STBC
5931 */
5932 if ((stf > PHY_TXC1_MODE_STBC) ||
5933 (!BRCMS_STBC_CAP_PHY(wlc)
5934 && (stf == PHY_TXC1_MODE_STBC))) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005935 brcms_err(core, "wl%d: %s: Invalid STBC\n",
5936 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02005937 bcmerror = -EINVAL;
5938 goto done;
5939 }
5940 }
5941 } else if (is_ofdm_rate(rate)) {
5942 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005943 brcms_err(core, "wl%d: %s: Invalid OFDM\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005944 wlc->pub->unit, __func__);
5945 bcmerror = -EINVAL;
5946 goto done;
5947 }
5948 } else if (is_cck_rate(rate)) {
5949 if ((cur_band->bandtype != BRCM_BAND_2G)
5950 || (stf != PHY_TXC1_MODE_SISO)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005951 brcms_err(core, "wl%d: %s: Invalid CCK\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005952 wlc->pub->unit, __func__);
5953 bcmerror = -EINVAL;
5954 goto done;
5955 }
5956 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005957 brcms_err(core, "wl%d: %s: Unknown rate type\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02005958 wlc->pub->unit, __func__);
5959 bcmerror = -EINVAL;
5960 goto done;
5961 }
5962 /* make sure multiple antennae are available for non-siso rates */
5963 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06005964 brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
Arend van Spriel5b435de2011-10-05 13:19:03 +02005965 "request\n", wlc->pub->unit, __func__);
5966 bcmerror = -EINVAL;
5967 goto done;
5968 }
5969
5970 rspec = rate;
5971 if (ismcs) {
5972 rspec |= RSPEC_MIMORATE;
5973 /* For STBC populate the STC field of the ratespec */
5974 if (stf == PHY_TXC1_MODE_STBC) {
5975 u8 stc;
5976 stc = 1; /* Nss for single stream is always 1 */
5977 rspec |= (stc << RSPEC_STC_SHIFT);
5978 }
5979 }
5980
5981 rspec |= (stf << RSPEC_STF_SHIFT);
5982
5983 if (override_mcs_only)
5984 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
5985
5986 if (issgi)
5987 rspec |= RSPEC_SHORT_GI;
5988
5989 if ((rate != 0)
5990 && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
5991 return rate;
5992
5993 return rspec;
5994done:
5995 return rate;
5996}
5997
5998/*
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02005999 * Compute PLCP, but only requires actual rate and length of pkt.
6000 * Rate is given in the driver standard multiple of 500 kbps.
6001 * le is set for 11 Mbps rate if necessary.
6002 * Broken out for PRQ.
6003 */
6004
6005static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
6006 uint length, u8 *plcp)
6007{
6008 u16 usec = 0;
6009 u8 le = 0;
6010
6011 switch (rate_500) {
6012 case BRCM_RATE_1M:
6013 usec = length << 3;
6014 break;
6015 case BRCM_RATE_2M:
6016 usec = length << 2;
6017 break;
6018 case BRCM_RATE_5M5:
6019 usec = (length << 4) / 11;
6020 if ((length << 4) - (usec * 11) > 0)
6021 usec++;
6022 break;
6023 case BRCM_RATE_11M:
6024 usec = (length << 3) / 11;
6025 if ((length << 3) - (usec * 11) > 0) {
6026 usec++;
6027 if ((usec * 11) - (length << 3) >= 8)
6028 le = D11B_PLCP_SIGNAL_LE;
6029 }
6030 break;
6031
6032 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06006033 brcms_err(wlc->hw->d11core,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006034 "brcms_c_cck_plcp_set: unsupported rate %d\n",
6035 rate_500);
6036 rate_500 = BRCM_RATE_1M;
6037 usec = length << 3;
6038 break;
6039 }
6040 /* PLCP signal byte */
6041 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
6042 /* PLCP service byte */
6043 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
6044 /* PLCP length u16, little endian */
6045 plcp[2] = usec & 0xff;
6046 plcp[3] = (usec >> 8) & 0xff;
6047 /* PLCP CRC16 */
6048 plcp[4] = 0;
6049 plcp[5] = 0;
6050}
6051
6052/* Rate: 802.11 rate code, length: PSDU length in octets */
6053static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
6054{
6055 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
6056 plcp[0] = mcs;
6057 if (rspec_is40mhz(rspec) || (mcs == 32))
6058 plcp[0] |= MIMO_PLCP_40MHZ;
6059 BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
6060 plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
6061 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
6062 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
6063 plcp[5] = 0;
6064}
6065
6066/* Rate: 802.11 rate code, length: PSDU length in octets */
6067static void
6068brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
6069{
6070 u8 rate_signal;
6071 u32 tmp = 0;
6072 int rate = rspec2rate(rspec);
6073
6074 /*
6075 * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
6076 * transmitted first
6077 */
6078 rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
6079 memset(plcp, 0, D11_PHY_HDR_LEN);
6080 D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
6081
6082 tmp = (length & 0xfff) << 5;
6083 plcp[2] |= (tmp >> 16) & 0xff;
6084 plcp[1] |= (tmp >> 8) & 0xff;
6085 plcp[0] |= tmp & 0xff;
6086}
6087
6088/* Rate: 802.11 rate code, length: PSDU length in octets */
6089static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
6090 uint length, u8 *plcp)
6091{
6092 int rate = rspec2rate(rspec);
6093
6094 brcms_c_cck_plcp_set(wlc, rate, length, plcp);
6095}
6096
6097static void
6098brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
6099 uint length, u8 *plcp)
6100{
6101 if (is_mcs_rate(rspec))
6102 brcms_c_compute_mimo_plcp(rspec, length, plcp);
6103 else if (is_ofdm_rate(rspec))
6104 brcms_c_compute_ofdm_plcp(rspec, length, plcp);
6105 else
6106 brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
6107}
6108
6109/* brcms_c_compute_rtscts_dur()
6110 *
6111 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
6112 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
6113 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
6114 *
6115 * cts cts-to-self or rts/cts
6116 * rts_rate rts or cts rate in unit of 500kbps
6117 * rate next MPDU rate in unit of 500kbps
6118 * frame_len next MPDU frame length in bytes
6119 */
6120u16
6121brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
6122 u32 rts_rate,
6123 u32 frame_rate, u8 rts_preamble_type,
6124 u8 frame_preamble_type, uint frame_len, bool ba)
6125{
6126 u16 dur, sifs;
6127
6128 sifs = get_sifs(wlc->band);
6129
6130 if (!cts_only) {
6131 /* RTS/CTS */
6132 dur = 3 * sifs;
6133 dur +=
6134 (u16) brcms_c_calc_cts_time(wlc, rts_rate,
6135 rts_preamble_type);
6136 } else {
6137 /* CTS-TO-SELF */
6138 dur = 2 * sifs;
6139 }
6140
6141 dur +=
6142 (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
6143 frame_len);
6144 if (ba)
6145 dur +=
6146 (u16) brcms_c_calc_ba_time(wlc, frame_rate,
6147 BRCMS_SHORT_PREAMBLE);
6148 else
6149 dur +=
6150 (u16) brcms_c_calc_ack_time(wlc, frame_rate,
6151 frame_preamble_type);
6152 return dur;
6153}
6154
6155static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
6156{
6157 u16 phyctl1 = 0;
6158 u16 bw;
6159
6160 if (BRCMS_ISLCNPHY(wlc->band)) {
6161 bw = PHY_TXC1_BW_20MHZ;
6162 } else {
6163 bw = rspec_get_bw(rspec);
6164 /* 10Mhz is not supported yet */
6165 if (bw < PHY_TXC1_BW_20MHZ) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006166 brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006167 "not supported yet, set to 20L\n", bw);
6168 bw = PHY_TXC1_BW_20MHZ;
6169 }
6170 }
6171
6172 if (is_mcs_rate(rspec)) {
6173 uint mcs = rspec & RSPEC_RATE_MASK;
6174
6175 /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
6176 phyctl1 = rspec_phytxbyte2(rspec);
6177 /* set the upper byte of phyctl1 */
6178 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
6179 } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
6180 && !BRCMS_ISSSLPNPHY(wlc->band)) {
6181 /*
6182 * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
6183 * Data Rate. Eventually MIMOPHY would also be converted to
6184 * this format
6185 */
6186 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
6187 phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6188 } else { /* legacy OFDM/CCK */
6189 s16 phycfg;
6190 /* get the phyctl byte from rate phycfg table */
6191 phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
6192 if (phycfg == -1) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006193 brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006194 "legacy OFDM/CCK rate\n");
6195 phycfg = 0;
6196 }
6197 /* set the upper byte of phyctl1 */
6198 phyctl1 =
6199 (bw | (phycfg << 8) |
6200 (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6201 }
6202 return phyctl1;
6203}
6204
6205/*
Arend van Spriel5b435de2011-10-05 13:19:03 +02006206 * Add struct d11txh, struct cck_phy_hdr.
6207 *
6208 * 'p' data must start with 802.11 MAC header
6209 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
6210 *
6211 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
6212 *
6213 */
6214static u16
6215brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
6216 struct sk_buff *p, struct scb *scb, uint frag,
6217 uint nfrags, uint queue, uint next_frag_len)
6218{
6219 struct ieee80211_hdr *h;
6220 struct d11txh *txh;
6221 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
6222 int len, phylen, rts_phylen;
6223 u16 mch, phyctl, xfts, mainrates;
6224 u16 seq = 0, mcl = 0, status = 0, frameid = 0;
6225 u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6226 u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6227 bool use_rts = false;
6228 bool use_cts = false;
6229 bool use_rifs = false;
6230 bool short_preamble[2] = { false, false };
6231 u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6232 u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6233 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
6234 struct ieee80211_rts *rts = NULL;
6235 bool qos;
6236 uint ac;
6237 bool hwtkmic = false;
6238 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
6239#define ANTCFG_NONE 0xFF
6240 u8 antcfg = ANTCFG_NONE;
6241 u8 fbantcfg = ANTCFG_NONE;
6242 uint phyctl1_stf = 0;
6243 u16 durid = 0;
6244 struct ieee80211_tx_rate *txrate[2];
6245 int k;
6246 struct ieee80211_tx_info *tx_info;
6247 bool is_mcs;
6248 u16 mimo_txbw;
6249 u8 mimo_preamble_type;
6250
6251 /* locate 802.11 MAC header */
6252 h = (struct ieee80211_hdr *)(p->data);
6253 qos = ieee80211_is_data_qos(h->frame_control);
6254
6255 /* compute length of frame in bytes for use in PLCP computations */
Arend van Sprielad4d71f2011-11-10 20:30:26 +01006256 len = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006257 phylen = len + FCS_LEN;
6258
6259 /* Get tx_info */
6260 tx_info = IEEE80211_SKB_CB(p);
6261
6262 /* add PLCP */
6263 plcp = skb_push(p, D11_PHY_HDR_LEN);
6264
6265 /* add Broadcom tx descriptor header */
6266 txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
6267 memset(txh, 0, D11_TXH_LEN);
6268
6269 /* setup frameid */
6270 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
6271 /* non-AP STA should never use BCMC queue */
6272 if (queue == TX_BCMC_FIFO) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06006273 brcms_err(wlc->hw->d11core,
6274 "wl%d: %s: ASSERT queue == TX_BCMC!\n",
6275 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006276 frameid = bcmc_fid_generate(wlc, NULL, txh);
6277 } else {
6278 /* Increment the counter for first fragment */
6279 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
6280 scb->seqnum[p->priority]++;
6281
6282 /* extract fragment number from frame first */
6283 seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
6284 seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
6285 h->seq_ctrl = cpu_to_le16(seq);
6286
6287 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6288 (queue & TXFID_QUEUE_MASK);
6289 }
6290 }
6291 frameid |= queue & TXFID_QUEUE_MASK;
6292
6293 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
6294 if (ieee80211_is_beacon(h->frame_control))
6295 mcl |= TXC_IGNOREPMQ;
6296
6297 txrate[0] = tx_info->control.rates;
6298 txrate[1] = txrate[0] + 1;
6299
6300 /*
6301 * if rate control algorithm didn't give us a fallback
6302 * rate, use the primary rate
6303 */
6304 if (txrate[1]->idx < 0)
6305 txrate[1] = txrate[0];
6306
6307 for (k = 0; k < hw->max_rates; k++) {
6308 is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
6309 if (!is_mcs) {
6310 if ((txrate[k]->idx >= 0)
6311 && (txrate[k]->idx <
6312 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6313 rspec[k] =
6314 hw->wiphy->bands[tx_info->band]->
6315 bitrates[txrate[k]->idx].hw_value;
6316 short_preamble[k] =
6317 txrate[k]->
6318 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
6319 true : false;
6320 } else {
6321 rspec[k] = BRCM_RATE_1M;
6322 }
6323 } else {
6324 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
6325 NRATE_MCS_INUSE | txrate[k]->idx);
6326 }
6327
6328 /*
6329 * Currently only support same setting for primay and
6330 * fallback rates. Unify flags for each rate into a
6331 * single value for the frame
6332 */
6333 use_rts |=
6334 txrate[k]->
6335 flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
6336 use_cts |=
6337 txrate[k]->
6338 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
6339
6340
6341 /*
6342 * (1) RATE:
6343 * determine and validate primary rate
6344 * and fallback rates
6345 */
6346 if (!rspec_active(rspec[k])) {
6347 rspec[k] = BRCM_RATE_1M;
6348 } else {
6349 if (!is_multicast_ether_addr(h->addr1)) {
6350 /* set tx antenna config */
6351 brcms_c_antsel_antcfg_get(wlc->asi, false,
6352 false, 0, 0, &antcfg, &fbantcfg);
6353 }
6354 }
6355 }
6356
6357 phyctl1_stf = wlc->stf->ss_opmode;
6358
6359 if (wlc->pub->_n_enab & SUPPORT_11N) {
6360 for (k = 0; k < hw->max_rates; k++) {
6361 /*
6362 * apply siso/cdd to single stream mcs's or ofdm
6363 * if rspec is auto selected
6364 */
6365 if (((is_mcs_rate(rspec[k]) &&
6366 is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
6367 is_ofdm_rate(rspec[k]))
6368 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
6369 || !(rspec[k] & RSPEC_OVERRIDE))) {
6370 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
6371
6372 /* For SISO MCS use STBC if possible */
6373 if (is_mcs_rate(rspec[k])
6374 && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
6375 u8 stc;
6376
6377 /* Nss for single stream is always 1 */
6378 stc = 1;
6379 rspec[k] |= (PHY_TXC1_MODE_STBC <<
6380 RSPEC_STF_SHIFT) |
6381 (stc << RSPEC_STC_SHIFT);
6382 } else
6383 rspec[k] |=
6384 (phyctl1_stf << RSPEC_STF_SHIFT);
6385 }
6386
6387 /*
6388 * Is the phy configured to use 40MHZ frames? If
6389 * so then pick the desired txbw
6390 */
6391 if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
6392 /* default txbw is 20in40 SB */
6393 mimo_ctlchbw = mimo_txbw =
6394 CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
6395 wlc->band->pi))
6396 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
6397
6398 if (is_mcs_rate(rspec[k])) {
6399 /* mcs 32 must be 40b/w DUP */
6400 if ((rspec[k] & RSPEC_RATE_MASK)
6401 == 32) {
6402 mimo_txbw =
6403 PHY_TXC1_BW_40MHZ_DUP;
6404 /* use override */
6405 } else if (wlc->mimo_40txbw != AUTO)
6406 mimo_txbw = wlc->mimo_40txbw;
6407 /* else check if dst is using 40 Mhz */
6408 else if (scb->flags & SCB_IS40)
6409 mimo_txbw = PHY_TXC1_BW_40MHZ;
6410 } else if (is_ofdm_rate(rspec[k])) {
6411 if (wlc->ofdm_40txbw != AUTO)
6412 mimo_txbw = wlc->ofdm_40txbw;
6413 } else if (wlc->cck_40txbw != AUTO) {
6414 mimo_txbw = wlc->cck_40txbw;
6415 }
6416 } else {
6417 /*
6418 * mcs32 is 40 b/w only.
6419 * This is possible for probe packets on
6420 * a STA during SCAN
6421 */
6422 if ((rspec[k] & RSPEC_RATE_MASK) == 32)
6423 /* mcs 0 */
6424 rspec[k] = RSPEC_MIMORATE;
6425
6426 mimo_txbw = PHY_TXC1_BW_20MHZ;
6427 }
6428
6429 /* Set channel width */
6430 rspec[k] &= ~RSPEC_BW_MASK;
6431 if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
6432 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
6433 else
6434 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6435
6436 /* Disable short GI, not supported yet */
6437 rspec[k] &= ~RSPEC_SHORT_GI;
6438
6439 mimo_preamble_type = BRCMS_MM_PREAMBLE;
6440 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6441 mimo_preamble_type = BRCMS_GF_PREAMBLE;
6442
6443 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6444 && (!is_mcs_rate(rspec[k]))) {
Joe Perchese81c7e92013-02-13 17:33:20 -08006445 brcms_warn(wlc->hw->d11core,
6446 "wl%d: %s: IEEE80211_TX_RC_MCS != is_mcs_rate(rspec)\n",
6447 wlc->pub->unit, __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006448 }
6449
6450 if (is_mcs_rate(rspec[k])) {
6451 preamble_type[k] = mimo_preamble_type;
6452
6453 /*
6454 * if SGI is selected, then forced mm
6455 * for single stream
6456 */
6457 if ((rspec[k] & RSPEC_SHORT_GI)
6458 && is_single_stream(rspec[k] &
6459 RSPEC_RATE_MASK))
6460 preamble_type[k] = BRCMS_MM_PREAMBLE;
6461 }
6462
6463 /* should be better conditionalized */
6464 if (!is_mcs_rate(rspec[0])
6465 && (tx_info->control.rates[0].
6466 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6467 preamble_type[k] = BRCMS_SHORT_PREAMBLE;
6468 }
6469 } else {
6470 for (k = 0; k < hw->max_rates; k++) {
6471 /* Set ctrlchbw as 20Mhz */
6472 rspec[k] &= ~RSPEC_BW_MASK;
6473 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6474
6475 /* for nphy, stf of ofdm frames must follow policies */
6476 if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
6477 rspec[k] &= ~RSPEC_STF_MASK;
6478 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6479 }
6480 }
6481 }
6482
6483 /* Reset these for use with AMPDU's */
6484 txrate[0]->count = 0;
6485 txrate[1]->count = 0;
6486
6487 /* (2) PROTECTION, may change rspec */
6488 if ((ieee80211_is_data(h->frame_control) ||
6489 ieee80211_is_mgmt(h->frame_control)) &&
6490 (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
6491 use_rts = true;
6492
6493 /* (3) PLCP: determine PLCP header and MAC duration,
6494 * fill struct d11txh */
6495 brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
6496 brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6497 memcpy(&txh->FragPLCPFallback,
6498 plcp_fallback, sizeof(txh->FragPLCPFallback));
6499
6500 /* Length field now put in CCK FBR CRC field */
6501 if (is_cck_rate(rspec[1])) {
6502 txh->FragPLCPFallback[4] = phylen & 0xff;
6503 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6504 }
6505
6506 /* MIMO-RATE: need validation ?? */
6507 mainrates = is_ofdm_rate(rspec[0]) ?
6508 D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
6509 plcp[0];
6510
6511 /* DUR field for main rate */
6512 if (!ieee80211_is_pspoll(h->frame_control) &&
6513 !is_multicast_ether_addr(h->addr1) && !use_rifs) {
6514 durid =
6515 brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6516 next_frag_len);
6517 h->duration_id = cpu_to_le16(durid);
6518 } else if (use_rifs) {
6519 /* NAV protect to end of next max packet size */
6520 durid =
6521 (u16) brcms_c_calc_frame_time(wlc, rspec[0],
6522 preamble_type[0],
6523 DOT11_MAX_FRAG_LEN);
6524 durid += RIFS_11N_TIME;
6525 h->duration_id = cpu_to_le16(durid);
6526 }
6527
6528 /* DUR field for fallback rate */
6529 if (ieee80211_is_pspoll(h->frame_control))
6530 txh->FragDurFallback = h->duration_id;
6531 else if (is_multicast_ether_addr(h->addr1) || use_rifs)
6532 txh->FragDurFallback = 0;
6533 else {
6534 durid = brcms_c_compute_frame_dur(wlc, rspec[1],
6535 preamble_type[1], next_frag_len);
6536 txh->FragDurFallback = cpu_to_le16(durid);
6537 }
6538
6539 /* (4) MAC-HDR: MacTxControlLow */
6540 if (frag == 0)
6541 mcl |= TXC_STARTMSDU;
6542
6543 if (!is_multicast_ether_addr(h->addr1))
6544 mcl |= TXC_IMMEDACK;
6545
6546 if (wlc->band->bandtype == BRCM_BAND_5G)
6547 mcl |= TXC_FREQBAND_5G;
6548
6549 if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
6550 mcl |= TXC_BW_40;
6551
6552 /* set AMIC bit if using hardware TKIP MIC */
6553 if (hwtkmic)
6554 mcl |= TXC_AMIC;
6555
6556 txh->MacTxControlLow = cpu_to_le16(mcl);
6557
6558 /* MacTxControlHigh */
6559 mch = 0;
6560
6561 /* Set fallback rate preamble type */
6562 if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
6563 (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
6564 if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
6565 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6566 }
6567
6568 /* MacFrameControl */
6569 memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
6570 txh->TxFesTimeNormal = cpu_to_le16(0);
6571
6572 txh->TxFesTimeFallback = cpu_to_le16(0);
6573
6574 /* TxFrameRA */
6575 memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
6576
6577 /* TxFrameID */
6578 txh->TxFrameID = cpu_to_le16(frameid);
6579
6580 /*
6581 * TxStatus, Note the case of recreating the first frag of a suppressed
6582 * frame then we may need to reset the retry cnt's via the status reg
6583 */
6584 txh->TxStatus = cpu_to_le16(status);
6585
6586 /*
6587 * extra fields for ucode AMPDU aggregation, the new fields are added to
6588 * the END of previous structure so that it's compatible in driver.
6589 */
6590 txh->MaxNMpdus = cpu_to_le16(0);
6591 txh->MaxABytes_MRT = cpu_to_le16(0);
6592 txh->MaxABytes_FBR = cpu_to_le16(0);
6593 txh->MinMBytes = cpu_to_le16(0);
6594
6595 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
6596 * furnish struct d11txh */
6597 /* RTS PLCP header and RTS frame */
6598 if (use_rts || use_cts) {
6599 if (use_rts && use_cts)
6600 use_cts = false;
6601
6602 for (k = 0; k < 2; k++) {
6603 rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
6604 false,
6605 mimo_ctlchbw);
6606 }
6607
6608 if (!is_ofdm_rate(rts_rspec[0]) &&
6609 !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
6610 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6611 rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
6612 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6613 }
6614
6615 if (!is_ofdm_rate(rts_rspec[1]) &&
6616 !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
6617 (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6618 rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
6619 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6620 }
6621
6622 /* RTS/CTS additions to MacTxControlLow */
6623 if (use_cts) {
6624 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
6625 } else {
6626 txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
6627 txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
6628 }
6629
6630 /* RTS PLCP header */
6631 rts_plcp = txh->RTSPhyHeader;
6632 if (use_cts)
6633 rts_phylen = DOT11_CTS_LEN + FCS_LEN;
6634 else
6635 rts_phylen = DOT11_RTS_LEN + FCS_LEN;
6636
6637 brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6638
6639 /* fallback rate version of RTS PLCP header */
6640 brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6641 rts_plcp_fallback);
6642 memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
6643 sizeof(txh->RTSPLCPFallback));
6644
6645 /* RTS frame fields... */
6646 rts = (struct ieee80211_rts *)&txh->rts_frame;
6647
6648 durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6649 rspec[0], rts_preamble_type[0],
6650 preamble_type[0], phylen, false);
6651 rts->duration = cpu_to_le16(durid);
6652 /* fallback rate version of RTS DUR field */
6653 durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
6654 rts_rspec[1], rspec[1],
6655 rts_preamble_type[1],
6656 preamble_type[1], phylen, false);
6657 txh->RTSDurFallback = cpu_to_le16(durid);
6658
6659 if (use_cts) {
6660 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6661 IEEE80211_STYPE_CTS);
6662
6663 memcpy(&rts->ra, &h->addr2, ETH_ALEN);
6664 } else {
6665 rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6666 IEEE80211_STYPE_RTS);
6667
6668 memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
6669 }
6670
6671 /* mainrate
6672 * low 8 bits: main frag rate/mcs,
6673 * high 8 bits: rts/cts rate/mcs
6674 */
6675 mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
6676 D11A_PHY_HDR_GRATE(
6677 (struct ofdm_phy_hdr *) rts_plcp) :
6678 rts_plcp[0]) << 8;
6679 } else {
Joe Perchesb56e6812013-02-13 17:33:21 -08006680 memset(txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6681 memset(&txh->rts_frame, 0, sizeof(struct ieee80211_rts));
6682 memset(txh->RTSPLCPFallback, 0, sizeof(txh->RTSPLCPFallback));
Arend van Spriel5b435de2011-10-05 13:19:03 +02006683 txh->RTSDurFallback = 0;
6684 }
6685
6686#ifdef SUPPORT_40MHZ
6687 /* add null delimiter count */
6688 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
6689 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6690 brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6691
6692#endif
6693
6694 /*
6695 * Now that RTS/RTS FB preamble types are updated, write
6696 * the final value
6697 */
6698 txh->MacTxControlHigh = cpu_to_le16(mch);
6699
6700 /*
6701 * MainRates (both the rts and frag plcp rates have
6702 * been calculated now)
6703 */
6704 txh->MainRates = cpu_to_le16(mainrates);
6705
6706 /* XtraFrameTypes */
6707 xfts = frametype(rspec[1], wlc->mimoft);
6708 xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6709 xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6710 xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
6711 XFTS_CHANNEL_SHIFT;
6712 txh->XtraFrameTypes = cpu_to_le16(xfts);
6713
6714 /* PhyTxControlWord */
6715 phyctl = frametype(rspec[0], wlc->mimoft);
6716 if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
6717 (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
6718 if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
6719 phyctl |= PHY_TXC_SHORT_HDR;
6720 }
6721
6722 /* phytxant is properly bit shifted */
6723 phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6724 txh->PhyTxControlWord = cpu_to_le16(phyctl);
6725
6726 /* PhyTxControlWord_1 */
6727 if (BRCMS_PHY_11N_CAP(wlc->band)) {
6728 u16 phyctl1 = 0;
6729
6730 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
6731 txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
6732 phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
6733 txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
6734
6735 if (use_rts || use_cts) {
6736 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
6737 txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
6738 phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
6739 txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
6740 }
6741
6742 /*
6743 * For mcs frames, if mixedmode(overloaded with long preamble)
6744 * is going to be set, fill in non-zero MModeLen and/or
6745 * MModeFbrLen it will be unnecessary if they are separated
6746 */
6747 if (is_mcs_rate(rspec[0]) &&
6748 (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
6749 u16 mmodelen =
6750 brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
6751 txh->MModeLen = cpu_to_le16(mmodelen);
6752 }
6753
6754 if (is_mcs_rate(rspec[1]) &&
6755 (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
6756 u16 mmodefbrlen =
6757 brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
6758 txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
6759 }
6760 }
6761
6762 ac = skb_get_queue_mapping(p);
6763 if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
6764 uint frag_dur, dur, dur_fallback;
6765
6766 /* WME: Update TXOP threshold */
6767 if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
6768 frag_dur =
6769 brcms_c_calc_frame_time(wlc, rspec[0],
6770 preamble_type[0], phylen);
6771
6772 if (rts) {
6773 /* 1 RTS or CTS-to-self frame */
6774 dur =
6775 brcms_c_calc_cts_time(wlc, rts_rspec[0],
6776 rts_preamble_type[0]);
6777 dur_fallback =
6778 brcms_c_calc_cts_time(wlc, rts_rspec[1],
6779 rts_preamble_type[1]);
6780 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
6781 dur += le16_to_cpu(rts->duration);
6782 dur_fallback +=
6783 le16_to_cpu(txh->RTSDurFallback);
6784 } else if (use_rifs) {
6785 dur = frag_dur;
6786 dur_fallback = 0;
6787 } else {
6788 /* frame + SIFS + ACK */
6789 dur = frag_dur;
6790 dur +=
6791 brcms_c_compute_frame_dur(wlc, rspec[0],
6792 preamble_type[0], 0);
6793
6794 dur_fallback =
6795 brcms_c_calc_frame_time(wlc, rspec[1],
6796 preamble_type[1],
6797 phylen);
6798 dur_fallback +=
6799 brcms_c_compute_frame_dur(wlc, rspec[1],
6800 preamble_type[1], 0);
6801 }
6802 /* NEED to set TxFesTimeNormal (hard) */
6803 txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
6804 /*
6805 * NEED to set fallback rate version of
6806 * TxFesTimeNormal (hard)
6807 */
6808 txh->TxFesTimeFallback =
6809 cpu_to_le16((u16) dur_fallback);
6810
6811 /*
6812 * update txop byte threshold (txop minus intraframe
6813 * overhead)
6814 */
6815 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6816 uint newfragthresh;
6817
6818 newfragthresh =
6819 brcms_c_calc_frame_len(wlc,
6820 rspec[0], preamble_type[0],
6821 (wlc->edcf_txop[ac] -
6822 (dur - frag_dur)));
6823 /* range bound the fragthreshold */
6824 if (newfragthresh < DOT11_MIN_FRAG_LEN)
6825 newfragthresh =
6826 DOT11_MIN_FRAG_LEN;
6827 else if (newfragthresh >
6828 wlc->usr_fragthresh)
6829 newfragthresh =
6830 wlc->usr_fragthresh;
6831 /* update the fragthresh and do txc update */
6832 if (wlc->fragthresh[queue] !=
6833 (u16) newfragthresh)
6834 wlc->fragthresh[queue] =
6835 (u16) newfragthresh;
6836 } else {
Joe Perchese81c7e92013-02-13 17:33:20 -08006837 brcms_warn(wlc->hw->d11core,
6838 "wl%d: %s txop invalid for rate %d\n",
6839 wlc->pub->unit, fifo_names[queue],
6840 rspec2rate(rspec[0]));
Arend van Spriel5b435de2011-10-05 13:19:03 +02006841 }
6842
6843 if (dur > wlc->edcf_txop[ac])
Joe Perchese81c7e92013-02-13 17:33:20 -08006844 brcms_warn(wlc->hw->d11core,
6845 "wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
6846 wlc->pub->unit, __func__,
6847 fifo_names[queue],
6848 phylen, wlc->fragthresh[queue],
6849 dur, wlc->edcf_txop[ac]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02006850 }
6851 }
6852
6853 return 0;
6854}
6855
Seth Forsheee041f652012-11-15 08:07:56 -06006856static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
Arend van Spriel5b435de2011-10-05 13:19:03 +02006857{
Seth Forsheee041f652012-11-15 08:07:56 -06006858 struct dma_pub *dma;
6859 int fifo, ret = -ENOSPC;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006860 struct d11txh *txh;
Seth Forsheee041f652012-11-15 08:07:56 -06006861 u16 frameid = INVALIDFID;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006862
Seth Forsheee041f652012-11-15 08:07:56 -06006863 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
6864 dma = wlc->hw->di[fifo];
6865 txh = (struct d11txh *)(skb->data);
6866
6867 if (dma->txavail == 0) {
6868 /*
6869 * We sometimes get a frame from mac80211 after stopping
6870 * the queues. This only ever seems to be a single frame
6871 * and is seems likely to be a race. TX_HEADROOM should
6872 * ensure that we have enough space to handle these stray
6873 * packets, so warn if there isn't. If we're out of space
6874 * in the tx ring and the tx queue isn't stopped then
6875 * we've really got a bug; warn loudly if that happens.
6876 */
Seth Forsheeb353dda2012-11-15 08:08:03 -06006877 brcms_warn(wlc->hw->d11core,
Seth Forsheee041f652012-11-15 08:07:56 -06006878 "Received frame for tx with no space in DMA ring\n");
6879 WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
6880 skb_get_queue_mapping(skb)));
6881 return -ENOSPC;
6882 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02006883
6884 /* When a BC/MC frame is being committed to the BCMC fifo
6885 * via DMA (NOT PIO), update ucode or BSS info as appropriate.
6886 */
6887 if (fifo == TX_BCMC_FIFO)
6888 frameid = le16_to_cpu(txh->TxFrameID);
6889
Arend van Spriel5b435de2011-10-05 13:19:03 +02006890 /* Commit BCMC sequence number in the SHM frame ID location */
6891 if (frameid != INVALIDFID) {
6892 /*
6893 * To inform the ucode of the last mcast frame posted
6894 * so that it can clear moredata bit
6895 */
6896 brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
6897 }
6898
Seth Forsheee041f652012-11-15 08:07:56 -06006899 ret = brcms_c_txfifo(wlc, fifo, skb);
6900 /*
6901 * The only reason for brcms_c_txfifo to fail is because
6902 * there weren't any DMA descriptors, but we've already
6903 * checked for that. So if it does fail yell loudly.
6904 */
6905 WARN_ON_ONCE(ret);
6906
6907 return ret;
6908}
6909
Piotr Haberc4dea352012-11-28 21:44:05 +01006910bool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
Seth Forsheee041f652012-11-15 08:07:56 -06006911 struct ieee80211_hw *hw)
6912{
6913 uint fifo;
6914 struct scb *scb = &wlc->pri_scb;
6915
6916 fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
Piotr Haberc4dea352012-11-28 21:44:05 +01006917 brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
6918 if (!brcms_c_tx(wlc, sdu))
6919 return true;
6920
6921 /* packet discarded */
6922 dev_kfree_skb_any(sdu);
6923 return false;
Seth Forsheee041f652012-11-15 08:07:56 -06006924}
6925
6926int
6927brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
6928{
6929 struct dma_pub *dma = wlc->hw->di[fifo];
6930 int ret;
6931 u16 queue;
6932
6933 ret = dma_txfast(wlc, dma, p);
6934 if (ret < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02006935 wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
Seth Forsheee041f652012-11-15 08:07:56 -06006936
6937 /*
6938 * Stop queue if DMA ring is full. Reserve some free descriptors,
6939 * as we sometimes receive a frame from mac80211 after the queues
6940 * are stopped.
6941 */
6942 queue = skb_get_queue_mapping(p);
6943 if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
6944 !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
6945 ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
6946
6947 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02006948}
6949
Arend van Spriel5b435de2011-10-05 13:19:03 +02006950u32
6951brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
6952 bool use_rspec, u16 mimo_ctlchbw)
6953{
6954 u32 rts_rspec = 0;
6955
6956 if (use_rspec)
6957 /* use frame rate as rts rate */
6958 rts_rspec = rspec;
6959 else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
6960 /* Use 11Mbps as the g protection RTS target rate and fallback.
6961 * Use the brcms_basic_rate() lookup to find the best basic rate
6962 * under the target in case 11 Mbps is not Basic.
6963 * 6 and 9 Mbps are not usually selected by rate selection, but
6964 * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
6965 * is more robust.
6966 */
6967 rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
6968 else
6969 /* calculate RTS rate and fallback rate based on the frame rate
6970 * RTS must be sent at a basic rate since it is a
6971 * control frame, sec 9.6 of 802.11 spec
6972 */
6973 rts_rspec = brcms_basic_rate(wlc, rspec);
6974
6975 if (BRCMS_PHY_11N_CAP(wlc->band)) {
6976 /* set rts txbw to correct side band */
6977 rts_rspec &= ~RSPEC_BW_MASK;
6978
6979 /*
6980 * if rspec/rspec_fallback is 40MHz, then send RTS on both
6981 * 20MHz channel (DUP), otherwise send RTS on control channel
6982 */
6983 if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
6984 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
6985 else
6986 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6987
6988 /* pick siso/cdd as default for ofdm */
6989 if (is_ofdm_rate(rts_rspec)) {
6990 rts_rspec &= ~RSPEC_STF_MASK;
6991 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
6992 }
6993 }
6994 return rts_rspec;
6995}
6996
Arend van Spriel5b435de2011-10-05 13:19:03 +02006997/* Update beacon listen interval in shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02006998static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02006999{
7000 /* wake up every DTIM is the default */
7001 if (wlc->bcn_li_dtim == 1)
7002 brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
7003 else
7004 brcms_b_write_shm(wlc->hw, M_BCN_LI,
7005 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
7006}
7007
7008static void
7009brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
7010 u32 *tsf_h_ptr)
7011{
Arend van Spriel16d28122011-12-08 15:06:51 -08007012 struct bcma_device *core = wlc_hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007013
7014 /* read the tsf timer low, then high to get an atomic read */
Arend van Spriel16d28122011-12-08 15:06:51 -08007015 *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
7016 *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
Arend van Spriel5b435de2011-10-05 13:19:03 +02007017}
7018
7019/*
7020 * recover 64bit TSF value from the 16bit TSF value in the rx header
7021 * given the assumption that the TSF passed in header is within 65ms
7022 * of the current tsf.
7023 *
7024 * 6 5 4 4 3 2 1
7025 * 3.......6.......8.......0.......2.......4.......6.......8......0
7026 * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
7027 *
7028 * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
7029 * tsf_l is filled in by brcms_b_recv, which is done earlier in the
7030 * receive call sequence after rx interrupt. Only the higher 16 bits
7031 * are used. Finally, the tsf_h is read from the tsf register.
7032 */
7033static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
7034 struct d11rxhdr *rxh)
7035{
7036 u32 tsf_h, tsf_l;
7037 u16 rx_tsf_0_15, rx_tsf_16_31;
7038
7039 brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
7040
7041 rx_tsf_16_31 = (u16)(tsf_l >> 16);
7042 rx_tsf_0_15 = rxh->RxTSFTime;
7043
7044 /*
7045 * a greater tsf time indicates the low 16 bits of
7046 * tsf_l wrapped, so decrement the high 16 bits.
7047 */
7048 if ((u16)tsf_l < rx_tsf_0_15) {
7049 rx_tsf_16_31 -= 1;
7050 if (rx_tsf_16_31 == 0xffff)
7051 tsf_h -= 1;
7052 }
7053
7054 return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
7055}
7056
7057static void
7058prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7059 struct sk_buff *p,
7060 struct ieee80211_rx_status *rx_status)
7061{
7062 int preamble;
7063 int channel;
7064 u32 rspec;
7065 unsigned char *plcp;
7066
7067 /* fill in TSF and flag its presence */
7068 rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
Thomas Pedersenf4bda332012-11-13 10:46:27 -08007069 rx_status->flag |= RX_FLAG_MACTIME_START;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007070
7071 channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
7072
Johannes Berg858a4552012-07-24 17:35:57 +02007073 rx_status->band =
7074 channel > 14 ? IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
7075 rx_status->freq =
7076 ieee80211_channel_to_frequency(channel, rx_status->band);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007077
7078 rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
7079
7080 /* noise */
7081 /* qual */
7082 rx_status->antenna =
7083 (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
7084
7085 plcp = p->data;
7086
7087 rspec = brcms_c_compute_rspec(rxh, plcp);
7088 if (is_mcs_rate(rspec)) {
7089 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
7090 rx_status->flag |= RX_FLAG_HT;
7091 if (rspec_is40mhz(rspec))
7092 rx_status->flag |= RX_FLAG_40MHZ;
7093 } else {
7094 switch (rspec2rate(rspec)) {
7095 case BRCM_RATE_1M:
7096 rx_status->rate_idx = 0;
7097 break;
7098 case BRCM_RATE_2M:
7099 rx_status->rate_idx = 1;
7100 break;
7101 case BRCM_RATE_5M5:
7102 rx_status->rate_idx = 2;
7103 break;
7104 case BRCM_RATE_11M:
7105 rx_status->rate_idx = 3;
7106 break;
7107 case BRCM_RATE_6M:
7108 rx_status->rate_idx = 4;
7109 break;
7110 case BRCM_RATE_9M:
7111 rx_status->rate_idx = 5;
7112 break;
7113 case BRCM_RATE_12M:
7114 rx_status->rate_idx = 6;
7115 break;
7116 case BRCM_RATE_18M:
7117 rx_status->rate_idx = 7;
7118 break;
7119 case BRCM_RATE_24M:
7120 rx_status->rate_idx = 8;
7121 break;
7122 case BRCM_RATE_36M:
7123 rx_status->rate_idx = 9;
7124 break;
7125 case BRCM_RATE_48M:
7126 rx_status->rate_idx = 10;
7127 break;
7128 case BRCM_RATE_54M:
7129 rx_status->rate_idx = 11;
7130 break;
7131 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -06007132 brcms_err(wlc->hw->d11core,
7133 "%s: Unknown rate\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007134 }
7135
7136 /*
7137 * For 5GHz, we should decrease the index as it is
7138 * a subset of the 2.4G rates. See bitrates field
7139 * of brcms_band_5GHz_nphy (in mac80211_if.c).
7140 */
7141 if (rx_status->band == IEEE80211_BAND_5GHZ)
7142 rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
7143
7144 /* Determine short preamble and rate_idx */
7145 preamble = 0;
7146 if (is_cck_rate(rspec)) {
7147 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
7148 rx_status->flag |= RX_FLAG_SHORTPRE;
7149 } else if (is_ofdm_rate(rspec)) {
7150 rx_status->flag |= RX_FLAG_SHORTPRE;
7151 } else {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007152 brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007153 __func__);
7154 }
7155 }
7156
7157 if (plcp3_issgi(plcp[3]))
7158 rx_status->flag |= RX_FLAG_SHORT_GI;
7159
7160 if (rxh->RxStatus1 & RXS_DECERR) {
7161 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007162 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007163 __func__);
7164 }
7165 if (rxh->RxStatus1 & RXS_FCSERR) {
7166 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
Seth Forsheeb353dda2012-11-15 08:08:03 -06007167 brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_FCS_CRC\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02007168 __func__);
7169 }
7170}
7171
7172static void
7173brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7174 struct sk_buff *p)
7175{
7176 int len_mpdu;
7177 struct ieee80211_rx_status rx_status;
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007178 struct ieee80211_hdr *hdr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007179
7180 memset(&rx_status, 0, sizeof(rx_status));
7181 prep_mac80211_status(wlc, rxh, p, &rx_status);
7182
7183 /* mac header+body length, exclude CRC and plcp header */
7184 len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
7185 skb_pull(p, D11_PHY_HDR_LEN);
7186 __skb_trim(p, len_mpdu);
7187
Arend van Sprielbadc4f02012-04-11 11:52:51 +02007188 /* unmute transmit */
7189 if (wlc->hw->suspended_fifos) {
7190 hdr = (struct ieee80211_hdr *)p->data;
7191 if (ieee80211_is_beacon(hdr->frame_control))
7192 brcms_b_mute(wlc->hw, false);
7193 }
7194
Arend van Spriel5b435de2011-10-05 13:19:03 +02007195 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7196 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7197}
7198
Arend van Spriel5b435de2011-10-05 13:19:03 +02007199/* calculate frame duration for Mixed-mode L-SIG spoofing, return
7200 * number of bytes goes in the length field
7201 *
7202 * Formula given by HT PHY Spec v 1.13
7203 * len = 3(nsyms + nstream + 3) - 3
7204 */
7205u16
7206brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
7207 uint mac_len)
7208{
7209 uint nsyms, len = 0, kNdps;
7210
Arend van Spriel5b435de2011-10-05 13:19:03 +02007211 if (is_mcs_rate(ratespec)) {
7212 uint mcs = ratespec & RSPEC_RATE_MASK;
7213 int tot_streams = (mcs_2_txstreams(mcs) + 1) +
7214 rspec_stc(ratespec);
7215
7216 /*
7217 * the payload duration calculation matches that
7218 * of regular ofdm
7219 */
7220 /* 1000Ndbps = kbps * 4 */
7221 kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
7222 rspec_issgi(ratespec)) * 4;
7223
7224 if (rspec_stc(ratespec) == 0)
7225 nsyms =
7226 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7227 APHY_TAIL_NBITS) * 1000, kNdps);
7228 else
7229 /* STBC needs to have even number of symbols */
7230 nsyms =
7231 2 *
7232 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7233 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7234
7235 /* (+3) account for HT-SIG(2) and HT-STF(1) */
7236 nsyms += (tot_streams + 3);
7237 /*
7238 * 3 bytes/symbol @ legacy 6Mbps rate
7239 * (-3) excluding service bits and tail bits
7240 */
7241 len = (3 * nsyms) - 3;
7242 }
7243
7244 return (u16) len;
7245}
7246
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007247static void
7248brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007249{
7250 const struct brcms_c_rateset *rs_dflt;
7251 struct brcms_c_rateset rs;
7252 u8 rate;
7253 u16 entry_ptr;
7254 u8 plcp[D11_PHY_HDR_LEN];
7255 u16 dur, sifs;
7256 uint i;
7257
7258 sifs = get_sifs(wlc->band);
7259
7260 rs_dflt = brcms_c_rateset_get_hwrs(wlc);
7261
7262 brcms_c_rateset_copy(rs_dflt, &rs);
7263 brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7264
7265 /*
7266 * walk the phy rate table and update MAC core SHM
7267 * basic rate table entries
7268 */
7269 for (i = 0; i < rs.count; i++) {
7270 rate = rs.rates[i] & BRCMS_RATE_MASK;
7271
7272 entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
7273
7274 /* Calculate the Probe Response PLCP for the given rate */
7275 brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
7276
7277 /*
7278 * Calculate the duration of the Probe Response
7279 * frame plus SIFS for the MAC
7280 */
7281 dur = (u16) brcms_c_calc_frame_time(wlc, rate,
7282 BRCMS_LONG_PREAMBLE, frame_len);
7283 dur += sifs;
7284
7285 /* Update the SHM Rate Table entry Probe Response values */
7286 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
7287 (u16) (plcp[0] + (plcp[1] << 8)));
7288 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7289 (u16) (plcp[2] + (plcp[3] << 8)));
7290 brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
7291 }
7292}
7293
7294/* Max buffering needed for beacon template/prb resp template is 142 bytes.
7295 *
7296 * PLCP header is 6 bytes.
7297 * 802.11 A3 header is 24 bytes.
7298 * Max beacon frame body template length is 112 bytes.
7299 * Max probe resp frame body template length is 110 bytes.
7300 *
7301 * *len on input contains the max length of the packet available.
7302 *
7303 * The *len value is set to the number of bytes in buf used, and starts
7304 * with the PLCP and included up to, but not including, the 4 byte FCS.
7305 */
7306static void
7307brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
7308 u32 bcn_rspec,
7309 struct brcms_bss_cfg *cfg, u16 *buf, int *len)
7310{
7311 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
7312 struct cck_phy_hdr *plcp;
7313 struct ieee80211_mgmt *h;
7314 int hdr_len, body_len;
7315
7316 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7317
7318 /* calc buffer size provided for frame body */
7319 body_len = *len - hdr_len;
7320 /* return actual size */
7321 *len = hdr_len + body_len;
7322
7323 /* format PHY and MAC headers */
Joe Perchesb56e6812013-02-13 17:33:21 -08007324 memset(buf, 0, hdr_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007325
7326 plcp = (struct cck_phy_hdr *) buf;
7327
7328 /*
7329 * PLCP for Probe Response frames are filled in from
7330 * core's rate table
7331 */
7332 if (type == IEEE80211_STYPE_BEACON)
7333 /* fill in PLCP */
7334 brcms_c_compute_plcp(wlc, bcn_rspec,
7335 (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
7336 (u8 *) plcp);
7337
7338 /* "Regular" and 16 MBSS but not for 4 MBSS */
7339 /* Update the phytxctl for the beacon based on the rspec */
7340 brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7341
7342 h = (struct ieee80211_mgmt *)&plcp[1];
7343
7344 /* fill in 802.11 header */
7345 h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
7346
7347 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7348 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7349 if (type == IEEE80211_STYPE_BEACON)
7350 memcpy(&h->da, &ether_bcast, ETH_ALEN);
7351 memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
7352 memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
7353
7354 /* SEQ filled in by MAC */
7355}
7356
7357int brcms_c_get_header_len(void)
7358{
7359 return TXOFF;
7360}
7361
7362/*
7363 * Update all beacons for the system.
7364 */
7365void brcms_c_update_beacon(struct brcms_c_info *wlc)
7366{
7367 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7368
7369 if (bsscfg->up && !bsscfg->BSS)
7370 /* Clear the soft intmask */
7371 wlc->defmacintmask &= ~MI_BCNTPL;
7372}
7373
7374/* Write ssid into shared memory */
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007375static void
7376brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007377{
7378 u8 *ssidptr = cfg->SSID;
7379 u16 base = M_SSID;
7380 u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
7381
7382 /* padding the ssid with zero and copy it into shm */
7383 memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
7384 memcpy(ssidbuf, ssidptr, cfg->SSID_len);
7385
7386 brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
7387 brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
7388}
7389
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007390static void
Arend van Spriel5b435de2011-10-05 13:19:03 +02007391brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
7392 struct brcms_bss_cfg *cfg,
7393 bool suspend)
7394{
Tim Gardner0d61c912013-02-07 13:28:09 -07007395 u16 *prb_resp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02007396 int len = BCN_TMPL_LEN;
7397
Tim Gardner0d61c912013-02-07 13:28:09 -07007398 prb_resp = kmalloc(BCN_TMPL_LEN, GFP_ATOMIC);
7399 if (!prb_resp)
7400 return;
7401
Arend van Spriel5b435de2011-10-05 13:19:03 +02007402 /*
7403 * write the probe response to hardware, or save in
7404 * the config structure
7405 */
7406
7407 /* create the probe response template */
7408 brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
7409 cfg, prb_resp, &len);
7410
7411 if (suspend)
7412 brcms_c_suspend_mac_and_wait(wlc);
7413
7414 /* write the probe response into the template region */
7415 brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7416 (len + 3) & ~3, prb_resp);
7417
7418 /* write the length of the probe response frame (+PLCP/-FCS) */
7419 brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
7420
7421 /* write the SSID and SSID length */
7422 brcms_c_shm_ssid_upd(wlc, cfg);
7423
7424 /*
7425 * Write PLCP headers and durations for probe response frames
7426 * at all rates. Use the actual frame length covered by the
7427 * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
7428 * by subtracting the PLCP len and adding the FCS.
7429 */
7430 len += (-D11_PHY_HDR_LEN + FCS_LEN);
7431 brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
7432
7433 if (suspend)
7434 brcms_c_enable_mac(wlc);
Tim Gardner0d61c912013-02-07 13:28:09 -07007435
7436 kfree(prb_resp);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007437}
7438
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007439void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
7440{
7441 struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7442
7443 /* update AP or IBSS probe responses */
7444 if (bsscfg->up && !bsscfg->BSS)
7445 brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
7446}
7447
Arend van Spriel5b435de2011-10-05 13:19:03 +02007448int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
7449 uint *blocks)
7450{
7451 if (fifo >= NFIFO)
7452 return -EINVAL;
7453
7454 *blocks = wlc_hw->xmtfifo_sz[fifo];
7455
7456 return 0;
7457}
7458
7459void
7460brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
7461 const u8 *addr)
7462{
7463 brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
7464 if (match_reg_offset == RCM_BSSID_OFFSET)
7465 memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
7466}
7467
Arend van Spriel5b435de2011-10-05 13:19:03 +02007468/*
7469 * Flag 'scan in progress' to withhold dynamic phy calibration
7470 */
7471void brcms_c_scan_start(struct brcms_c_info *wlc)
7472{
7473 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
7474}
7475
7476void brcms_c_scan_stop(struct brcms_c_info *wlc)
7477{
7478 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
7479}
7480
7481void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
7482{
7483 wlc->pub->associated = state;
7484 wlc->bsscfg->associated = state;
7485}
7486
7487/*
7488 * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
7489 * AMPDU traffic, packets pending in hardware have to be invalidated so that
7490 * when later on hardware releases them, they can be handled appropriately.
7491 */
7492void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
7493 struct ieee80211_sta *sta,
7494 void (*dma_callback_fn))
7495{
7496 struct dma_pub *dmah;
7497 int i;
7498 for (i = 0; i < NFIFO; i++) {
7499 dmah = hw->di[i];
7500 if (dmah != NULL)
7501 dma_walk_packets(dmah, dma_callback_fn, sta);
7502 }
7503}
7504
7505int brcms_c_get_curband(struct brcms_c_info *wlc)
7506{
7507 return wlc->band->bandunit;
7508}
7509
Arend van Spriel7b2385b2013-02-02 14:36:50 +01007510bool brcms_c_tx_flush_completed(struct brcms_c_info *wlc)
Arend van Spriel5b435de2011-10-05 13:19:03 +02007511{
Seth Forsheee041f652012-11-15 08:07:56 -06007512 int i;
Stanislaw Gruszkaf96b08a2012-01-17 12:38:50 +01007513
Seth Forsheee041f652012-11-15 08:07:56 -06007514 /* Kick DMA to send any pending AMPDU */
7515 for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
7516 if (wlc->hw->di[i])
Arend van Spriel7b2385b2013-02-02 14:36:50 +01007517 dma_kick_tx(wlc->hw->di[i]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007518
Arend van Spriel7b2385b2013-02-02 14:36:50 +01007519 return !brcms_txpktpendtot(wlc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02007520}
7521
7522void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
7523{
7524 wlc->bcn_li_bcn = interval;
7525 if (wlc->pub->up)
7526 brcms_c_bcn_li_upd(wlc);
7527}
7528
7529int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
7530{
7531 uint qdbm;
7532
7533 /* Remove override bit and clip to max qdbm value */
7534 qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
7535 return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
7536}
7537
7538int brcms_c_get_tx_power(struct brcms_c_info *wlc)
7539{
7540 uint qdbm;
7541 bool override;
7542
7543 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
7544
7545 /* Return qdbm units */
7546 return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
7547}
7548
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007549/* Process received frames */
7550/*
7551 * Return true if more frames need to be processed. false otherwise.
7552 * Param 'bound' indicates max. # frames to process before break out.
7553 */
7554static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
7555{
7556 struct d11rxhdr *rxh;
7557 struct ieee80211_hdr *h;
7558 uint len;
7559 bool is_amsdu;
7560
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007561 /* frame starts with rxhdr */
7562 rxh = (struct d11rxhdr *) (p->data);
7563
7564 /* strip off rxhdr */
7565 skb_pull(p, BRCMS_HWRXOFF);
7566
7567 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7568 if (rxh->RxStatus1 & RXS_PBPRES) {
7569 if (p->len < 2) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007570 brcms_err(wlc->hw->d11core,
7571 "wl%d: recv: rcvd runt of len %d\n",
7572 wlc->pub->unit, p->len);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007573 goto toss;
7574 }
7575 skb_pull(p, 2);
7576 }
7577
7578 h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
7579 len = p->len;
7580
7581 if (rxh->RxStatus1 & RXS_FCSERR) {
Alwin Beukersbe667662011-11-22 17:21:43 -08007582 if (!(wlc->filter_flags & FIF_FCSFAIL))
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007583 goto toss;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007584 }
7585
7586 /* check received pkt has at least frame control field */
7587 if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
7588 goto toss;
7589
7590 /* not supporting A-MSDU */
7591 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7592 if (is_amsdu)
7593 goto toss;
7594
7595 brcms_c_recvctl(wlc, rxh, p);
7596 return;
7597
7598 toss:
7599 brcmu_pkt_buf_free_skb(p);
7600}
7601
7602/* Process received frames */
7603/*
7604 * Return true if more frames need to be processed. false otherwise.
7605 * Param 'bound' indicates max. # frames to process before break out.
7606 */
7607static bool
7608brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
7609{
7610 struct sk_buff *p;
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007611 struct sk_buff *next = NULL;
7612 struct sk_buff_head recv_frames;
7613
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007614 uint n = 0;
7615 uint bound_limit = bound ? RXBND : -1;
Geert Uytterhoevenc2397bb2012-12-22 22:07:14 +01007616 bool morepending = false;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007617
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007618 skb_queue_head_init(&recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007619
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007620 /* gather received frames */
Piotr Haber57fe5042012-11-28 21:44:07 +01007621 do {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007622 /* !give others some time to run! */
Piotr Haber57fe5042012-11-28 21:44:07 +01007623 if (n >= bound_limit)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007624 break;
Piotr Haber57fe5042012-11-28 21:44:07 +01007625
7626 morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
7627 n++;
7628 } while (morepending);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007629
7630 /* post more rbufs */
7631 dma_rxfill(wlc_hw->di[fifo]);
7632
7633 /* process each frame */
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007634 skb_queue_walk_safe(&recv_frames, p, next) {
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007635 struct d11rxhdr_le *rxh_le;
7636 struct d11rxhdr *rxh;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007637
Arend van Spriel3fd172d2011-10-21 16:16:31 +02007638 skb_unlink(p, &recv_frames);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007639 rxh_le = (struct d11rxhdr_le *)p->data;
7640 rxh = (struct d11rxhdr *)p->data;
7641
7642 /* fixup rx header endianness */
7643 rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
7644 rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
7645 rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
7646 rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
7647 rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
7648 rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
7649 rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
7650 rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
7651 rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
7652 rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
7653 rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
7654
7655 brcms_c_recv(wlc_hw->wlc, p);
7656 }
7657
Piotr Haber57fe5042012-11-28 21:44:07 +01007658 return morepending;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007659}
7660
7661/* second-level interrupt processing
7662 * Return true if another dpc needs to be re-scheduled. false otherwise.
7663 * Param 'bounded' indicates if applicable loops should be bounded.
7664 */
7665bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
7666{
7667 u32 macintstatus;
7668 struct brcms_hardware *wlc_hw = wlc->hw;
Arend van Spriel16d28122011-12-08 15:06:51 -08007669 struct bcma_device *core = wlc_hw->d11core;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007670
7671 if (brcms_deviceremoved(wlc)) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007672 brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007673 __func__);
7674 brcms_down(wlc->wl);
7675 return false;
7676 }
7677
7678 /* grab and clear the saved software intstatus bits */
7679 macintstatus = wlc->macintstatus;
7680 wlc->macintstatus = 0;
7681
Seth Forshee229a41d2012-11-15 08:08:06 -06007682 brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
7683 wlc_hw->unit, macintstatus);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007684
7685 WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
7686
7687 /* tx status */
7688 if (macintstatus & MI_TFS) {
7689 bool fatal;
7690 if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
7691 wlc->macintstatus |= MI_TFS;
7692 if (fatal) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007693 brcms_err(core, "MI_TFS: fatal\n");
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007694 goto fatal;
7695 }
7696 }
7697
7698 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
7699 brcms_c_tbtt(wlc);
7700
7701 /* ATIM window end */
7702 if (macintstatus & MI_ATIMWINEND) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007703 brcms_dbg_info(core, "end of ATIM window\n");
Arend van Spriel16d28122011-12-08 15:06:51 -08007704 bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007705 wlc->qvalid = 0;
7706 }
7707
7708 /*
7709 * received data or control frame, MI_DMAINT is
7710 * indication of RX_FIFO interrupt
7711 */
7712 if (macintstatus & MI_DMAINT)
7713 if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
7714 wlc->macintstatus |= MI_DMAINT;
7715
7716 /* noise sample collected */
7717 if (macintstatus & MI_BG_NOISE)
7718 wlc_phy_noise_sample_intr(wlc_hw->band->pi);
7719
7720 if (macintstatus & MI_GP0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007721 brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007722 "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007723
7724 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
Arend van Sprielb2ffec42011-12-08 15:06:45 -08007725 __func__, ai_get_chip_id(wlc_hw->sih),
7726 ai_get_chiprev(wlc_hw->sih));
Roland Vossenc261bdf2011-10-18 14:03:04 +02007727 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007728 }
7729
7730 /* gptimer timeout */
7731 if (macintstatus & MI_TO)
Arend van Spriel16d28122011-12-08 15:06:51 -08007732 bcma_write32(core, D11REGOFFS(gptimer), 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007733
7734 if (macintstatus & MI_RFDISABLE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06007735 brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
7736 " RF Disable Input\n", wlc_hw->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007737 brcms_rfkill_set_hw_state(wlc->wl);
7738 }
7739
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007740 /* it isn't done and needs to be resched if macintstatus is non-zero */
7741 return wlc->macintstatus != 0;
7742
7743 fatal:
Roland Vossenc261bdf2011-10-18 14:03:04 +02007744 brcms_fatal_error(wlc_hw->wlc->wl);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007745 return wlc->macintstatus != 0;
7746}
7747
Roland Vossendc460122011-10-21 16:16:28 +02007748void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007749{
Arend van Spriel16d28122011-12-08 15:06:51 -08007750 struct bcma_device *core = wlc->hw->d11core;
Seth Forshee91691292012-06-16 07:47:49 -05007751 struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007752 u16 chanspec;
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007753
Seth Forsheeb353dda2012-11-15 08:08:03 -06007754 brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007755
Seth Forshee91691292012-06-16 07:47:49 -05007756 chanspec = ch20mhz_chspec(ch->hw_value);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007757
Roland Vossena8bc4912011-10-21 16:16:25 +02007758 brcms_b_init(wlc->hw, chanspec);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007759
7760 /* update beacon listen interval */
7761 brcms_c_bcn_li_upd(wlc);
7762
7763 /* write ethernet address to core */
7764 brcms_c_set_mac(wlc->bsscfg);
7765 brcms_c_set_bssid(wlc->bsscfg);
7766
7767 /* Update tsf_cfprep if associated and up */
7768 if (wlc->pub->associated && wlc->bsscfg->up) {
7769 u32 bi;
7770
7771 /* get beacon period and convert to uS */
7772 bi = wlc->bsscfg->current_bss->beacon_period << 10;
7773 /*
7774 * update since init path would reset
7775 * to default value
7776 */
Arend van Spriel16d28122011-12-08 15:06:51 -08007777 bcma_write32(core, D11REGOFFS(tsf_cfprep),
7778 bi << CFPREP_CBI_SHIFT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007779
7780 /* Update maccontrol PM related bits */
7781 brcms_c_set_ps_ctrl(wlc);
7782 }
7783
7784 brcms_c_bandinit_ordered(wlc, chanspec);
7785
7786 /* init probe response timeout */
7787 brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
7788
7789 /* init max burst txop (framebursting) */
7790 brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
7791 (wlc->
7792 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
7793
7794 /* initialize maximum allowed duty cycle */
7795 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
7796 brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
7797
7798 /*
7799 * Update some shared memory locations related to
7800 * max AMPDU size allowed to received
7801 */
7802 brcms_c_ampdu_shm_upd(wlc->ampdu);
7803
7804 /* band-specific inits */
7805 brcms_c_bsinit(wlc);
7806
7807 /* Enable EDCF mode (while the MAC is suspended) */
Arend van Spriel16d28122011-12-08 15:06:51 -08007808 bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007809 brcms_c_edcf_setparams(wlc, false);
7810
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007811 /* read the ucode version if we have not yet done so */
7812 if (wlc->ucode_rev == 0) {
7813 wlc->ucode_rev =
7814 brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
7815 wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
7816 }
7817
7818 /* ..now really unleash hell (allow the MAC out of suspend) */
7819 brcms_c_enable_mac(wlc);
7820
Roland Vossena8bc4912011-10-21 16:16:25 +02007821 /* suspend the tx fifos and mute the phy for preism cac time */
7822 if (mute_tx)
Roland Vossenc6c44892011-10-21 16:16:26 +02007823 brcms_b_mute(wlc->hw, true);
Roland Vossena8bc4912011-10-21 16:16:25 +02007824
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007825 /* enable the RF Disable Delay timer */
Arend van Spriel16d28122011-12-08 15:06:51 -08007826 bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007827
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007828 /*
7829 * Initialize WME parameters; if they haven't been set by some other
7830 * mechanism (IOVar, etc) then read them from the hardware.
7831 */
7832 if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
7833 /* Uninitialized; read from HW */
7834 int ac;
7835
Arend van Sprielb7eec422011-11-10 20:30:18 +01007836 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007837 wlc->wme_retries[ac] =
7838 brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
7839 }
7840}
7841
7842/*
7843 * The common driver entry routine. Error codes should be unique
7844 */
7845struct brcms_c_info *
Arend van Sprielb63337a2011-12-08 15:06:47 -08007846brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
7847 bool piomode, uint *perr)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007848{
7849 struct brcms_c_info *wlc;
7850 uint err = 0;
7851 uint i, j;
7852 struct brcms_pub *pub;
7853
7854 /* allocate struct brcms_c_info state and its substructures */
Joe Perches2c208892012-06-04 12:44:17 +00007855 wlc = brcms_c_attach_malloc(unit, &err, 0);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007856 if (wlc == NULL)
7857 goto fail;
7858 wlc->wiphy = wl->wiphy;
7859 pub = wlc->pub;
7860
Joe Perches8ae74652012-01-15 00:38:38 -08007861#if defined(DEBUG)
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007862 wlc_info_dbg = wlc;
7863#endif
7864
7865 wlc->band = wlc->bandstate[0];
7866 wlc->core = wlc->corestate;
7867 wlc->wl = wl;
7868 pub->unit = unit;
7869 pub->_piomode = piomode;
7870 wlc->bandinit_pending = false;
7871
7872 /* populate struct brcms_c_info with default values */
7873 brcms_c_info_init(wlc, unit);
7874
7875 /* update sta/ap related parameters */
7876 brcms_c_ap_upd(wlc);
7877
7878 /*
7879 * low level attach steps(all hw accesses go
7880 * inside, no more in rest of the attach)
7881 */
Arend van Sprielb63337a2011-12-08 15:06:47 -08007882 err = brcms_b_attach(wlc, core, unit, piomode);
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007883 if (err)
7884 goto fail;
7885
7886 brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
7887
7888 pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
7889
7890 /* disable allowed duty cycle */
7891 wlc->tx_duty_cycle_ofdm = 0;
7892 wlc->tx_duty_cycle_cck = 0;
7893
7894 brcms_c_stf_phy_chain_calc(wlc);
7895
7896 /* txchain 1: txant 0, txchain 2: txant 1 */
7897 if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
7898 wlc->stf->txant = wlc->stf->hw_txchain - 1;
7899
7900 /* push to BMAC driver */
7901 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
7902 wlc->stf->hw_rxchain);
7903
7904 /* pull up some info resulting from the low attach */
7905 for (i = 0; i < NFIFO; i++)
7906 wlc->core->txavail[i] = wlc->hw->txavail[i];
7907
7908 memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7909 memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7910
7911 for (j = 0; j < wlc->pub->_nbands; j++) {
7912 wlc->band = wlc->bandstate[j];
7913
7914 if (!brcms_c_attach_stf_ant_init(wlc)) {
7915 err = 24;
7916 goto fail;
7917 }
7918
7919 /* default contention windows size limits */
7920 wlc->band->CWmin = APHY_CWMIN;
7921 wlc->band->CWmax = PHY_CWMAX;
7922
7923 /* init gmode value */
7924 if (wlc->band->bandtype == BRCM_BAND_2G) {
7925 wlc->band->gmode = GMODE_AUTO;
7926 brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
7927 wlc->band->gmode);
7928 }
7929
7930 /* init _n_enab supported mode */
7931 if (BRCMS_PHY_11N_CAP(wlc->band)) {
7932 pub->_n_enab = SUPPORT_11N;
7933 brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
7934 ((pub->_n_enab ==
7935 SUPPORT_11N) ? WL_11N_2x2 :
7936 WL_11N_3x3));
7937 }
7938
7939 /* init per-band default rateset, depend on band->gmode */
7940 brcms_default_rateset(wlc, &wlc->band->defrateset);
7941
7942 /* fill in hw_rateset */
7943 brcms_c_rateset_filter(&wlc->band->defrateset,
7944 &wlc->band->hw_rateset, false,
7945 BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
7946 (bool) (wlc->pub->_n_enab & SUPPORT_11N));
7947 }
7948
7949 /*
7950 * update antenna config due to
7951 * wlc->stf->txant/txchain/ant_rx_ovr change
7952 */
7953 brcms_c_stf_phy_txant_upd(wlc);
7954
7955 /* attach each modules */
7956 err = brcms_c_attach_module(wlc);
7957 if (err != 0)
7958 goto fail;
7959
7960 if (!brcms_c_timers_init(wlc, unit)) {
7961 wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
7962 __func__);
7963 err = 32;
7964 goto fail;
7965 }
7966
7967 /* depend on rateset, gmode */
7968 wlc->cmi = brcms_c_channel_mgr_attach(wlc);
7969 if (!wlc->cmi) {
7970 wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
7971 "\n", unit, __func__);
7972 err = 33;
7973 goto fail;
7974 }
7975
7976 /* init default when all parameters are ready, i.e. ->rateset */
7977 brcms_c_bss_default_init(wlc);
7978
7979 /*
7980 * Complete the wlc default state initializations..
7981 */
7982
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02007983 wlc->bsscfg->wlc = wlc;
7984
7985 wlc->mimoft = FT_HT;
7986 wlc->mimo_40txbw = AUTO;
7987 wlc->ofdm_40txbw = AUTO;
7988 wlc->cck_40txbw = AUTO;
7989 brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
7990
7991 /* Set default values of SGI */
7992 if (BRCMS_SGI_CAP_PHY(wlc)) {
7993 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
7994 BRCMS_N_SGI_40));
7995 } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
7996 brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
7997 BRCMS_N_SGI_40));
7998 } else {
7999 brcms_c_ht_update_sgi_rx(wlc, 0);
8000 }
8001
Alwin Beukers94bdc2a2011-10-12 20:51:13 +02008002 brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
8003
8004 if (perr)
8005 *perr = 0;
8006
8007 return wlc;
8008
8009 fail:
8010 wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
8011 unit, __func__, err);
8012 if (wlc)
8013 brcms_c_detach(wlc);
8014
8015 if (perr)
8016 *perr = err;
8017 return NULL;
8018}