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