staging: brcm80211: remove unnecessary cast in wlc_d11hdrs_mac80211
[deliverable/linux.git] / drivers / staging / brcm80211 / brcmsmac / wlc_mac80211.c
CommitLineData
a9533e7e
HP
1/*
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 */
3327989a 16#include <linux/kernel.h>
a1c16ed2 17#include <linux/ctype.h>
3ca5ada5 18#include <linux/etherdevice.h>
6677eaa3 19#include <linux/string.h>
a9533e7e 20#include <bcmdefs.h>
a52ba66c 21#include <bcmdevs.h>
a1c16ed2 22#include <wlc_cfg.h>
a9533e7e
HP
23#include <osl.h>
24#include <bcmutils.h>
25#include <bcmwifi.h>
26#include <siutils.h>
27#include <bcmendian.h>
a9533e7e
HP
28#include <pcicfg.h>
29#include <bcmsrom.h>
30#include <wlioctl.h>
a52ba66c 31#include <sbhndpio.h>
a9533e7e
HP
32#include <sbhnddma.h>
33#include <hnddma.h>
34#include <hndpmu.h>
35#include <d11.h>
36#include <wlc_rate.h>
37#include <wlc_pub.h>
38#include <wlc_key.h>
39#include <wlc_bsscfg.h>
40#include <wlc_channel.h>
69ec303a 41#include <wlc_event.h>
a9533e7e
HP
42#include <wlc_mac80211.h>
43#include <wlc_bmac.h>
44#include <wlc_scb.h>
45#include <wlc_phy_hal.h>
46#include <wlc_phy_shim.h>
47#include <wlc_antsel.h>
48#include <wlc_stf.h>
49#include <wlc_ampdu.h>
50#include <wlc_event.h>
51#include <wl_export.h>
a9533e7e 52#include "d11ucode_ext.h"
a9533e7e
HP
53#include <wlc_alloc.h>
54#include <net/mac80211.h>
69ec303a 55#include <wl_dbg.h>
a9533e7e 56
e4cf544e
AS
57/*
58 * Disable statistics counting for WME
59 */
60#define WLCNTSET(a, b)
61#define WLCNTINCR(a)
62#define WLCNTADD(a, b)
a9533e7e 63
35af8764
AS
64/*
65 * WPA(2) definitions
66 */
67#define RSN_CAP_4_REPLAY_CNTRS 2
68#define RSN_CAP_16_REPLAY_CNTRS 3
69
70#define WPA_CAP_4_REPLAY_CNTRS RSN_CAP_4_REPLAY_CNTRS
71#define WPA_CAP_16_REPLAY_CNTRS RSN_CAP_16_REPLAY_CNTRS
72
a9533e7e
HP
73/*
74 * buffer length needed for wlc_format_ssid
75 * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
76 */
3726ed4d 77#define SSID_FMT_BUF_LEN ((4 * IEEE80211_MAX_SSID_LEN) + 1)
a9533e7e
HP
78
79#define TIMER_INTERVAL_WATCHDOG 1000 /* watchdog timer, in unit of ms */
80#define TIMER_INTERVAL_RADIOCHK 800 /* radio monitor timer, in unit of ms */
81
82#ifndef WLC_MPC_MAX_DELAYCNT
83#define WLC_MPC_MAX_DELAYCNT 10 /* Max MPC timeout, in unit of watchdog */
84#endif
85#define WLC_MPC_MIN_DELAYCNT 1 /* Min MPC timeout, in unit of watchdog */
86#define WLC_MPC_THRESHOLD 3 /* MPC count threshold level */
87
88#define BEACON_INTERVAL_DEFAULT 100 /* beacon interval, in unit of 1024TU */
89#define DTIM_INTERVAL_DEFAULT 3 /* DTIM interval, in unit of beacon interval */
90
91/* Scale down delays to accommodate QT slow speed */
92#define BEACON_INTERVAL_DEF_QT 20 /* beacon interval, in unit of 1024TU */
93#define DTIM_INTERVAL_DEF_QT 1 /* DTIM interval, in unit of beacon interval */
94
95#define TBTT_ALIGN_LEEWAY_US 100 /* min leeway before first TBTT in us */
96
97/*
98 * driver maintains internal 'tick'(wlc->pub->now) which increments in 1s OS timer(soft
99 * watchdog) it is not a wall clock and won't increment when driver is in "down" state
100 * this low resolution driver tick can be used for maintenance tasks such as phy
101 * calibration and scb update
102 */
103
104/* watchdog trigger mode: OSL timer or TBTT */
105#define WLC_WATCHDOG_TBTT(wlc) \
106 (wlc->stas_associated > 0 && wlc->PM != PM_OFF && wlc->pub->align_wd_tbtt)
107
108/* To inform the ucode of the last mcast frame posted so that it can clear moredata bit */
109#define BCMCFID(wlc, fid) wlc_bmac_write_shm((wlc)->hw, M_BCMC_FID, (fid))
110
fa7a1db2 111#define WLC_WAR16165(wlc) (wlc->pub->sih->bustype == PCI_BUS && \
a9533e7e 112 (!AP_ENAB(wlc->pub)) && (wlc->war16165))
a9533e7e
HP
113
114/* debug/trace */
115uint wl_msg_level =
116#if defined(BCMDBG)
117 WL_ERROR_VAL;
118#else
119 0;
120#endif /* BCMDBG */
121
122/* Find basic rate for a given rate */
123#define WLC_BASIC_RATE(wlc, rspec) (IS_MCS(rspec) ? \
124 (wlc)->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK].leg_ofdm] : \
125 (wlc)->band->basic_rate[rspec & RSPEC_RATE_MASK])
126
127#define FRAMETYPE(r, mimoframe) (IS_MCS(r) ? mimoframe : (IS_CCK(r) ? FT_CCK : FT_OFDM))
128
129#define RFDISABLE_DEFAULT 10000000 /* rfdisable delay timer 500 ms, runs of ALP clock */
130
131#define WLC_TEMPSENSE_PERIOD 10 /* 10 second timeout */
132
133#define SCAN_IN_PROGRESS(x) 0
134
4766ae6c
BR
135#define EPI_VERSION_NUM 0x054b0b00
136
a9533e7e
HP
137#ifdef BCMDBG
138/* pointer to most recently allocated wl/wlc */
c6a9e1fc 139static struct wlc_info *wlc_info_dbg = (struct wlc_info *) (NULL);
a9533e7e
HP
140#endif
141
a9533e7e
HP
142/* IOVar table */
143
144/* Parameter IDs, for use only internally to wlc -- in the wlc_iovars
145 * table and by the wlc_doiovar() function. No ordering is imposed:
146 * the table is keyed by name, and the function uses a switch.
147 */
148enum {
149 IOV_MPC = 1,
fd821d1e 150 IOV_RTSTHRESH,
a9533e7e
HP
151 IOV_QTXPOWER,
152 IOV_BCN_LI_BCN, /* Beacon listen interval in # of beacons */
153 IOV_LAST /* In case of a need to check max ID number */
154};
155
156const bcm_iovar_t wlc_iovars[] = {
fd821d1e
AS
157 {"mpc", IOV_MPC, (0), IOVT_BOOL, 0},
158 {"rtsthresh", IOV_RTSTHRESH, (IOVF_WHL), IOVT_UINT16, 0},
159 {"qtxpower", IOV_QTXPOWER, (IOVF_WHL), IOVT_UINT32, 0},
160 {"bcn_li_bcn", IOV_BCN_LI_BCN, (0), IOVT_UINT8, 0},
a9533e7e
HP
161 {NULL, 0, 0, 0, 0}
162};
163
41feb5ed 164const u8 prio2fifo[NUMPRIO] = {
a9533e7e
HP
165 TX_AC_BE_FIFO, /* 0 BE AC_BE Best Effort */
166 TX_AC_BK_FIFO, /* 1 BK AC_BK Background */
167 TX_AC_BK_FIFO, /* 2 -- AC_BK Background */
168 TX_AC_BE_FIFO, /* 3 EE AC_BE Best Effort */
169 TX_AC_VI_FIFO, /* 4 CL AC_VI Video */
170 TX_AC_VI_FIFO, /* 5 VI AC_VI Video */
171 TX_AC_VO_FIFO, /* 6 VO AC_VO Voice */
172 TX_AC_VO_FIFO /* 7 NC AC_VO Voice */
173};
174
175/* precedences numbers for wlc queues. These are twice as may levels as
176 * 802.1D priorities.
177 * Odd numbers are used for HI priority traffic at same precedence levels
178 * These constants are used ONLY by wlc_prio2prec_map. Do not use them elsewhere.
179 */
180#define _WLC_PREC_NONE 0 /* None = - */
181#define _WLC_PREC_BK 2 /* BK - Background */
182#define _WLC_PREC_BE 4 /* BE - Best-effort */
183#define _WLC_PREC_EE 6 /* EE - Excellent-effort */
184#define _WLC_PREC_CL 8 /* CL - Controlled Load */
185#define _WLC_PREC_VI 10 /* Vi - Video */
186#define _WLC_PREC_VO 12 /* Vo - Voice */
187#define _WLC_PREC_NC 14 /* NC - Network Control */
188
189/* 802.1D Priority to precedence queue mapping */
41feb5ed 190const u8 wlc_prio2prec_map[] = {
a9533e7e
HP
191 _WLC_PREC_BE, /* 0 BE - Best-effort */
192 _WLC_PREC_BK, /* 1 BK - Background */
193 _WLC_PREC_NONE, /* 2 None = - */
194 _WLC_PREC_EE, /* 3 EE - Excellent-effort */
195 _WLC_PREC_CL, /* 4 CL - Controlled Load */
196 _WLC_PREC_VI, /* 5 Vi - Video */
197 _WLC_PREC_VO, /* 6 Vo - Voice */
198 _WLC_PREC_NC, /* 7 NC - Network Control */
199};
200
201/* Sanity check for tx_prec_map and fifo synchup
202 * Either there are some packets pending for the fifo, else if fifo is empty then
203 * all the corresponding precmap bits should be set
204 */
205#define WLC_TX_FIFO_CHECK(wlc, fifo) (TXPKTPENDGET((wlc), (fifo)) || \
206 (TXPKTPENDGET((wlc), (fifo)) == 0 && \
207 ((wlc)->tx_prec_map & (wlc)->fifo2prec_map[(fifo)]) == \
208 (wlc)->fifo2prec_map[(fifo)]))
209
210/* TX FIFO number to WME/802.1E Access Category */
41feb5ed 211const u8 wme_fifo2ac[] = { AC_BK, AC_BE, AC_VI, AC_VO, AC_BE, AC_BE };
a9533e7e
HP
212
213/* WME/802.1E Access Category to TX FIFO number */
41feb5ed 214static const u8 wme_ac2fifo[] = { 1, 0, 2, 3 };
a9533e7e 215
0965ae88 216static bool in_send_q = false;
a9533e7e
HP
217
218/* Shared memory location index for various AC params */
219#define wme_shmemacindex(ac) wme_ac2fifo[ac]
220
221#ifdef BCMDBG
e5c4536f
JC
222static const char *fifo_names[] = {
223 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
a9533e7e
HP
224const char *aci_names[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
225#endif
226
41feb5ed 227static const u8 acbitmap2maxprio[] = {
a9533e7e
HP
228 PRIO_8021D_BE, PRIO_8021D_BE, PRIO_8021D_BK, PRIO_8021D_BK,
229 PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI,
230 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO,
231 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO
232};
233
234/* currently the best mechanism for determining SIFS is the band in use */
235#define SIFS(band) ((band)->bandtype == WLC_BAND_5G ? APHY_SIFS_TIME : BPHY_SIFS_TIME);
236
237/* value for # replay counters currently supported */
238#define WLC_REPLAY_CNTRS_VALUE WPA_CAP_16_REPLAY_CNTRS
239
240/* local prototypes */
c6a9e1fc 241static u16 BCMFASTPATH wlc_d11hdrs_mac80211(struct wlc_info *wlc,
c26b1378
AS
242 struct ieee80211_hw *hw,
243 struct sk_buff *p,
a9533e7e
HP
244 struct scb *scb, uint frag,
245 uint nfrags, uint queue,
246 uint next_frag_len,
7cc4a4c0 247 wsec_key_t *key,
a9533e7e 248 ratespec_t rspec_override);
c26b1378 249
fd821d1e 250static void wlc_ctrupd_cache(u16 cur_stat, u16 *macstat_snapshot, u32 *macstat);
c6a9e1fc
RV
251static void wlc_bss_default_init(struct wlc_info *wlc);
252static void wlc_ucode_mac_upd(struct wlc_info *wlc);
253static ratespec_t mac80211_wlc_set_nrate(struct wlc_info *wlc,
f077f718 254 struct wlcband *cur_band, u32 int_val);
c6a9e1fc 255static void wlc_tx_prec_map_init(struct wlc_info *wlc);
a9533e7e
HP
256static void wlc_watchdog(void *arg);
257static void wlc_watchdog_by_timer(void *arg);
c6a9e1fc
RV
258static int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg);
259static int wlc_iovar_rangecheck(struct wlc_info *wlc, u32 val,
7cc4a4c0 260 const bcm_iovar_t *vi);
c6a9e1fc 261static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc);
a9533e7e 262
a9533e7e 263/* send and receive */
c6a9e1fc
RV
264static wlc_txq_info_t *wlc_txq_alloc(struct wlc_info *wlc,
265 struct osl_info *osh);
266static void wlc_txq_free(struct wlc_info *wlc, struct osl_info *osh,
e69284f2 267 wlc_txq_info_t *qi);
c6a9e1fc 268static void wlc_txflowcontrol_signal(struct wlc_info *wlc, wlc_txq_info_t *qi,
a9533e7e 269 bool on, int prio);
c6a9e1fc
RV
270static void wlc_txflowcontrol_reset(struct wlc_info *wlc);
271static u16 wlc_compute_airtime(struct wlc_info *wlc, ratespec_t rspec,
a9533e7e 272 uint length);
41feb5ed
GKH
273static void wlc_compute_cck_plcp(ratespec_t rate, uint length, u8 *plcp);
274static void wlc_compute_ofdm_plcp(ratespec_t rate, uint length, u8 *plcp);
275static void wlc_compute_mimo_plcp(ratespec_t rate, uint length, u8 *plcp);
c6a9e1fc 276static u16 wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate,
41feb5ed 277 u8 preamble_type, uint next_frag_len);
c6a9e1fc
RV
278static void wlc_recvctl(struct wlc_info *wlc, struct osl_info *osh,
279 d11rxhdr_t *rxh, struct sk_buff *p);
280static uint wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t rate,
41feb5ed 281 u8 preamble_type, uint dur);
c6a9e1fc 282static uint wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rate,
41feb5ed 283 u8 preamble_type);
c6a9e1fc 284static uint wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rate,
41feb5ed 285 u8 preamble_type);
a9533e7e 286/* interrupt, up/down, band */
c6a9e1fc
RV
287static void wlc_setband(struct wlc_info *wlc, uint bandunit);
288static chanspec_t wlc_init_chanspec(struct wlc_info *wlc);
289static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec);
290static void wlc_bsinit(struct wlc_info *wlc);
291static int wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
a9533e7e 292 bool writeToShm);
c6a9e1fc
RV
293static void wlc_radio_hwdisable_upd(struct wlc_info *wlc);
294static bool wlc_radio_monitor_start(struct wlc_info *wlc);
a9533e7e 295static void wlc_radio_timer(void *arg);
c6a9e1fc
RV
296static void wlc_radio_enable(struct wlc_info *wlc);
297static void wlc_radio_upd(struct wlc_info *wlc);
a9533e7e
HP
298
299/* scan, association, BSS */
c6a9e1fc 300static uint wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rate,
41feb5ed 301 u8 preamble_type);
c6a9e1fc
RV
302static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap);
303static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val);
304static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val);
305static void wlc_war16165(struct wlc_info *wlc, bool tx);
a9533e7e
HP
306
307static void wlc_process_eventq(void *arg);
c6a9e1fc
RV
308static void wlc_wme_retries_write(struct wlc_info *wlc);
309static bool wlc_attach_stf_ant_init(struct wlc_info *wlc);
310static uint wlc_attach_module(struct wlc_info *wlc);
311static void wlc_detach_module(struct wlc_info *wlc);
312static void wlc_timers_deinit(struct wlc_info *wlc);
313static void wlc_down_led_upd(struct wlc_info *wlc);
314static uint wlc_down_del_timer(struct wlc_info *wlc);
315static void wlc_ofdm_rateset_war(struct wlc_info *wlc);
316static int _wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
a9533e7e 317 struct wlc_if *wlcif);
a9533e7e
HP
318
319#if defined(BCMDBG)
a44d4236 320void wlc_get_rcmta(struct wlc_info *wlc, int idx, u8 *addr)
a9533e7e
HP
321{
322 d11regs_t *regs = wlc->regs;
66cbd3ab 323 u32 v32;
e69284f2 324 struct osl_info *osh;
a9533e7e 325
f4528696 326 WL_TRACE("wl%d: %s\n", WLCWLUNIT(wlc), __func__);
a9533e7e
HP
327
328 ASSERT(wlc->pub->corerev > 4);
329
330 osh = wlc->osh;
331
332 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | (idx * 2)));
333 (void)R_REG(osh, &regs->objaddr);
334 v32 = R_REG(osh, &regs->objdata);
a44d4236
AS
335 addr[0] = (u8) v32;
336 addr[1] = (u8) (v32 >> 8);
337 addr[2] = (u8) (v32 >> 16);
338 addr[3] = (u8) (v32 >> 24);
a9533e7e
HP
339 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | ((idx * 2) + 1)));
340 (void)R_REG(osh, &regs->objaddr);
e88cf8eb 341 v32 = R_REG(osh, (volatile u16 *)&regs->objdata);
a44d4236
AS
342 addr[4] = (u8) v32;
343 addr[5] = (u8) (v32 >> 8);
a9533e7e
HP
344}
345#endif /* defined(BCMDBG) */
346
347/* keep the chip awake if needed */
c6a9e1fc 348bool wlc_stay_awake(struct wlc_info *wlc)
a9533e7e 349{
0f0881b0 350 return true;
a9533e7e
HP
351}
352
353/* conditions under which the PM bit should be set in outgoing frames and STAY_AWAKE is meaningful
354 */
c6a9e1fc 355bool wlc_ps_allowed(struct wlc_info *wlc)
a9533e7e
HP
356{
357 int idx;
358 wlc_bsscfg_t *cfg;
359
360 /* disallow PS when one of the following global conditions meets */
361 if (!wlc->pub->associated || !wlc->PMenabled || wlc->PM_override)
0965ae88 362 return false;
a9533e7e
HP
363
364 /* disallow PS when one of these meets when not scanning */
365 if (!wlc->PMblocked) {
366 if (AP_ACTIVE(wlc) || wlc->monitor)
0965ae88 367 return false;
a9533e7e
HP
368 }
369
370 FOREACH_AS_STA(wlc, idx, cfg) {
371 /* disallow PS when one of the following bsscfg specific conditions meets */
372 if (!cfg->BSS || !WLC_PORTOPEN(cfg))
0965ae88 373 return false;
a9533e7e
HP
374
375 if (!cfg->dtim_programmed)
0965ae88 376 return false;
a9533e7e
HP
377 }
378
0f0881b0 379 return true;
a9533e7e
HP
380}
381
c6a9e1fc 382void wlc_reset(struct wlc_info *wlc)
a2627bc0 383{
f4528696 384 WL_TRACE("wl%d: wlc_reset\n", wlc->pub->unit);
a9533e7e 385
0965ae88 386 wlc->check_for_unaligned_tbtt = false;
a9533e7e
HP
387
388 /* slurp up hw mac counters before core reset */
e4cf544e 389 wlc_statsupd(wlc);
a9533e7e 390
e4cf544e
AS
391 /* reset our snapshot of macstat counters */
392 memset((char *)wlc->core->macstat_snapshot, 0,
393 sizeof(macstat_t));
a9533e7e
HP
394
395 wlc_bmac_reset(wlc->hw);
396 wlc_ampdu_reset(wlc->ampdu);
397 wlc->txretried = 0;
398
a9533e7e
HP
399}
400
c6a9e1fc 401void wlc_fatal_error(struct wlc_info *wlc)
a9533e7e 402{
f4528696 403 WL_ERROR("wl%d: fatal error, reinitializing\n", wlc->pub->unit);
a9533e7e
HP
404 wl_init(wlc->wl);
405}
406
407/* Return the channel the driver should initialize during wlc_init.
408 * the channel may have to be changed from the currently configured channel
409 * if other configurations are in conflict (bandlocked, 11n mode disabled,
410 * invalid channel for current country, etc.)
411 */
c6a9e1fc 412static chanspec_t wlc_init_chanspec(struct wlc_info *wlc)
a2627bc0 413{
a9533e7e
HP
414 chanspec_t chanspec =
415 1 | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE |
416 WL_CHANSPEC_BAND_2G;
417
418 /* make sure the channel is on the supported band if we are band-restricted */
419 if (wlc->bandlocked || NBANDS(wlc) == 1) {
420 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
421 }
422 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
423 return chanspec;
424}
425
426struct scb global_scb;
427
c6a9e1fc 428static void wlc_init_scb(struct wlc_info *wlc, struct scb *scb)
a9533e7e
HP
429{
430 int i;
431 scb->flags = SCB_WMECAP | SCB_HTCAP;
432 for (i = 0; i < NUMPRIO; i++)
433 scb->seqnum[i] = 0;
434}
435
c6a9e1fc 436void wlc_init(struct wlc_info *wlc)
a2627bc0 437{
a9533e7e
HP
438 d11regs_t *regs;
439 chanspec_t chanspec;
440 int i;
441 wlc_bsscfg_t *bsscfg;
0965ae88 442 bool mute = false;
a9533e7e 443
f4528696 444 WL_TRACE("wl%d: wlc_init\n", wlc->pub->unit);
a9533e7e
HP
445
446 regs = wlc->regs;
447
448 /* This will happen if a big-hammer was executed. In that case, we want to go back
449 * to the channel that we were on and not new channel
450 */
451 if (wlc->pub->associated)
452 chanspec = wlc->home_chanspec;
453 else
454 chanspec = wlc_init_chanspec(wlc);
455
456 wlc_bmac_init(wlc->hw, chanspec, mute);
457
458 wlc->seckeys = wlc_bmac_read_shm(wlc->hw, M_SECRXKEYS_PTR) * 2;
459 if (D11REV_GE(wlc->pub->corerev, 15) && (wlc->machwcap & MCAP_TKIPMIC))
460 wlc->tkmickeys =
461 wlc_bmac_read_shm(wlc->hw, M_TKMICKEYS_PTR) * 2;
462
463 /* update beacon listen interval */
464 wlc_bcn_li_upd(wlc);
465 wlc->bcn_wait_prd =
41feb5ed 466 (u8) (wlc_bmac_read_shm(wlc->hw, M_NOSLPZNATDTIM) >> 10);
a9533e7e
HP
467 ASSERT(wlc->bcn_wait_prd > 0);
468
469 /* the world is new again, so is our reported rate */
470 wlc_reprate_init(wlc);
471
472 /* write ethernet address to core */
473 FOREACH_BSS(wlc, i, bsscfg) {
474 wlc_set_mac(bsscfg);
475 wlc_set_bssid(bsscfg);
476 }
477
478 /* Update tsf_cfprep if associated and up */
479 if (wlc->pub->associated) {
480 FOREACH_BSS(wlc, i, bsscfg) {
481 if (bsscfg->up) {
66cbd3ab 482 u32 bi;
a9533e7e
HP
483
484 /* get beacon period from bsscfg and convert to uS */
485 bi = bsscfg->current_bss->beacon_period << 10;
486 /* update the tsf_cfprep register */
487 /* since init path would reset to default value */
488 W_REG(wlc->osh, &regs->tsf_cfprep,
489 (bi << CFPREP_CBI_SHIFT));
490
491 /* Update maccontrol PM related bits */
492 wlc_set_ps_ctrl(wlc);
493
494 break;
495 }
496 }
497 }
498
499 wlc_key_hw_init_all(wlc);
500
501 wlc_bandinit_ordered(wlc, chanspec);
502
503 wlc_init_scb(wlc, &global_scb);
504
505 /* init probe response timeout */
506 wlc_write_shm(wlc, M_PRS_MAXTIME, wlc->prb_resp_timeout);
507
508 /* init max burst txop (framebursting) */
509 wlc_write_shm(wlc, M_MBURST_TXOP,
510 (wlc->
511 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
512
513 /* initialize maximum allowed duty cycle */
0f0881b0 514 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
0965ae88 515 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
a9533e7e
HP
516
517 /* Update some shared memory locations related to max AMPDU size allowed to received */
518 wlc_ampdu_shm_upd(wlc->ampdu);
519
520 /* band-specific inits */
521 wlc_bsinit(wlc);
522
523 /* Enable EDCF mode (while the MAC is suspended) */
524 if (EDCF_ENAB(wlc->pub)) {
525 OR_REG(wlc->osh, &regs->ifs_ctl, IFS_USEEDCF);
0965ae88 526 wlc_edcf_setparams(wlc->cfg, false);
a9533e7e
HP
527 }
528
529 /* Init precedence maps for empty FIFOs */
530 wlc_tx_prec_map_init(wlc);
531
532 /* read the ucode version if we have not yet done so */
533 if (wlc->ucode_rev == 0) {
534 wlc->ucode_rev =
7d4df48e 535 wlc_read_shm(wlc, M_BOM_REV_MAJOR) << NBITS(u16);
a9533e7e
HP
536 wlc->ucode_rev |= wlc_read_shm(wlc, M_BOM_REV_MINOR);
537 }
538
539 /* ..now really unleash hell (allow the MAC out of suspend) */
540 wlc_enable_mac(wlc);
541
542 /* clear tx flow control */
543 wlc_txflowcontrol_reset(wlc);
544
545 /* clear tx data fifo suspends */
0965ae88 546 wlc->tx_suspended = false;
a9533e7e
HP
547
548 /* enable the RF Disable Delay timer */
549 if (D11REV_GE(wlc->pub->corerev, 10))
550 W_REG(wlc->osh, &wlc->regs->rfdisabledly, RFDISABLE_DEFAULT);
551
552 /* initialize mpc delay */
553 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
554
555 /*
556 * Initialize WME parameters; if they haven't been set by some other
557 * mechanism (IOVar, etc) then read them from the hardware.
558 */
559 if (WLC_WME_RETRY_SHORT_GET(wlc, 0) == 0) { /* Unintialized; read from HW */
560 int ac;
561
562 ASSERT(wlc->clk);
563 for (ac = 0; ac < AC_COUNT; ac++) {
564 wlc->wme_retries[ac] =
565 wlc_read_shm(wlc, M_AC_TXLMT_ADDR(ac));
566 }
567 }
568}
569
c6a9e1fc 570void wlc_mac_bcn_promisc_change(struct wlc_info *wlc, bool promisc)
a9533e7e
HP
571{
572 wlc->bcnmisc_monitor = promisc;
573 wlc_mac_bcn_promisc(wlc);
574}
575
c6a9e1fc 576void wlc_mac_bcn_promisc(struct wlc_info *wlc)
a9533e7e
HP
577{
578 if ((AP_ENAB(wlc->pub) && (N_ENAB(wlc->pub) || wlc->band->gmode)) ||
579 wlc->bcnmisc_ibss || wlc->bcnmisc_scan || wlc->bcnmisc_monitor)
580 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, MCTL_BCNS_PROMISC);
581 else
582 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, 0);
583}
584
585/* set or clear maccontrol bits MCTL_PROMISC and MCTL_KEEPCONTROL */
c6a9e1fc 586void wlc_mac_promisc(struct wlc_info *wlc)
a9533e7e 587{
66cbd3ab 588 u32 promisc_bits = 0;
a9533e7e
HP
589
590 /* promiscuous mode just sets MCTL_PROMISC
591 * Note: APs get all BSS traffic without the need to set the MCTL_PROMISC bit
592 * since all BSS data traffic is directed at the AP
593 */
594 if (PROMISC_ENAB(wlc->pub) && !AP_ENAB(wlc->pub) && !wlc->wet)
595 promisc_bits |= MCTL_PROMISC;
596
597 /* monitor mode needs both MCTL_PROMISC and MCTL_KEEPCONTROL
598 * Note: monitor mode also needs MCTL_BCNS_PROMISC, but that is
599 * handled in wlc_mac_bcn_promisc()
600 */
601 if (MONITOR_ENAB(wlc))
602 promisc_bits |= MCTL_PROMISC | MCTL_KEEPCONTROL;
603
604 wlc_mctrl(wlc, MCTL_PROMISC | MCTL_KEEPCONTROL, promisc_bits);
605}
606
607/* check if hps and wake states of sw and hw are in sync */
c6a9e1fc 608bool wlc_ps_check(struct wlc_info *wlc)
a9533e7e 609{
0f0881b0 610 bool res = true;
a9533e7e
HP
611 bool hps, wake;
612 bool wake_ok;
613
614 if (!AP_ACTIVE(wlc)) {
66cbd3ab 615 volatile u32 tmp;
a9533e7e
HP
616 tmp = R_REG(wlc->osh, &wlc->regs->maccontrol);
617
618 /* If deviceremoved is detected, then don't take any action as this can be called
619 * in any context. Assume that caller will take care of the condition. This is just
620 * to avoid assert
621 */
622 if (tmp == 0xffffffff) {
f4528696
JP
623 WL_ERROR("wl%d: %s: dead chip\n",
624 wlc->pub->unit, __func__);
a9533e7e
HP
625 return DEVICEREMOVED(wlc);
626 }
627
628 hps = PS_ALLOWED(wlc);
629
630 if (hps != ((tmp & MCTL_HPS) != 0)) {
631 int idx;
632 wlc_bsscfg_t *cfg;
f4528696
JP
633 WL_ERROR("wl%d: hps not sync, sw %d, maccontrol 0x%x\n",
634 wlc->pub->unit, hps, tmp);
a9533e7e
HP
635 FOREACH_BSS(wlc, idx, cfg) {
636 if (!BSSCFG_STA(cfg))
637 continue;
638 }
639
0965ae88 640 res = false;
a9533e7e 641 }
a9533e7e
HP
642 /* For a monolithic build the wake check can be exact since it looks at wake
643 * override bits. The MCTL_WAKE bit should match the 'wake' value.
644 */
645 wake = STAY_AWAKE(wlc) || wlc->hw->wake_override;
646 wake_ok = (wake == ((tmp & MCTL_WAKE) != 0));
a9533e7e 647 if (hps && !wake_ok) {
f4528696
JP
648 WL_ERROR("wl%d: wake not sync, sw %d maccontrol 0x%x\n",
649 wlc->pub->unit, wake, tmp);
0965ae88 650 res = false;
a9533e7e
HP
651 }
652 }
653 ASSERT(res);
654 return res;
655}
656
657/* push sw hps and wake state through hardware */
c6a9e1fc 658void wlc_set_ps_ctrl(struct wlc_info *wlc)
a9533e7e 659{
66cbd3ab 660 u32 v1, v2;
a9533e7e
HP
661 bool hps, wake;
662 bool awake_before;
663
664 hps = PS_ALLOWED(wlc);
0f0881b0 665 wake = hps ? (STAY_AWAKE(wlc)) : true;
a9533e7e 666
f4528696
JP
667 WL_TRACE("wl%d: wlc_set_ps_ctrl: hps %d wake %d\n",
668 wlc->pub->unit, hps, wake);
a9533e7e
HP
669
670 v1 = R_REG(wlc->osh, &wlc->regs->maccontrol);
671 v2 = 0;
672 if (hps)
673 v2 |= MCTL_HPS;
674 if (wake)
675 v2 |= MCTL_WAKE;
676
677 wlc_mctrl(wlc, MCTL_WAKE | MCTL_HPS, v2);
678
679 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
680
681 if (wake && !awake_before)
682 wlc_bmac_wait_for_wake(wlc->hw);
683
684}
685
686/*
687 * Write this BSS config's MAC address to core.
688 * Updates RXE match engine.
689 */
7cc4a4c0 690int wlc_set_mac(wlc_bsscfg_t *cfg)
a9533e7e
HP
691{
692 int err = 0;
c6a9e1fc 693 struct wlc_info *wlc = cfg->wlc;
a9533e7e
HP
694
695 if (cfg == wlc->cfg) {
696 /* enter the MAC addr into the RXE match registers */
a44d4236 697 wlc_set_addrmatch(wlc, RCM_MAC_OFFSET, cfg->cur_etheraddr);
a9533e7e
HP
698 }
699
700 wlc_ampdu_macaddr_upd(wlc);
701
702 return err;
703}
704
705/* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
706 * Updates RXE match engine.
707 */
7cc4a4c0 708void wlc_set_bssid(wlc_bsscfg_t *cfg)
a9533e7e 709{
c6a9e1fc 710 struct wlc_info *wlc = cfg->wlc;
a9533e7e
HP
711
712 /* if primary config, we need to update BSSID in RXE match registers */
713 if (cfg == wlc->cfg) {
a44d4236 714 wlc_set_addrmatch(wlc, RCM_BSSID_OFFSET, cfg->BSSID);
a9533e7e
HP
715 }
716#ifdef SUPPORT_HWKEYS
717 else if (BSSCFG_STA(cfg) && cfg->BSS) {
718 wlc_rcmta_add_bssid(wlc, cfg);
719 }
720#endif
721}
722
723/*
724 * Suspend the the MAC and update the slot timing
725 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
726 */
c6a9e1fc 727void wlc_switch_shortslot(struct wlc_info *wlc, bool shortslot)
a9533e7e
HP
728{
729 int idx;
730 wlc_bsscfg_t *cfg;
731
732 ASSERT(wlc->band->gmode);
733
734 /* use the override if it is set */
735 if (wlc->shortslot_override != WLC_SHORTSLOT_AUTO)
736 shortslot = (wlc->shortslot_override == WLC_SHORTSLOT_ON);
737
738 if (wlc->shortslot == shortslot)
739 return;
740
741 wlc->shortslot = shortslot;
742
743 /* update the capability based on current shortslot mode */
744 FOREACH_BSS(wlc, idx, cfg) {
745 if (!cfg->associated)
746 continue;
04795017
RV
747 cfg->current_bss->capability &=
748 ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
a9533e7e 749 if (wlc->shortslot)
04795017
RV
750 cfg->current_bss->capability |=
751 WLAN_CAPABILITY_SHORT_SLOT_TIME;
a9533e7e
HP
752 }
753
754 wlc_bmac_set_shortslot(wlc->hw, shortslot);
755}
756
c6a9e1fc 757static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc)
a9533e7e 758{
41feb5ed 759 u8 local;
e59fe083 760 s16 local_max;
a9533e7e
HP
761
762 local = WLC_TXPWR_MAX;
763 if (wlc->pub->associated &&
764 (wf_chspec_ctlchan(wlc->chanspec) ==
765 wf_chspec_ctlchan(wlc->home_chanspec))) {
766
767 /* get the local power constraint if we are on the AP's
768 * channel [802.11h, 7.3.2.13]
769 */
770 /* Clamp the value between 0 and WLC_TXPWR_MAX w/o overflowing the target */
771 local_max =
772 (wlc->txpwr_local_max -
773 wlc->txpwr_local_constraint) * WLC_TXPWR_DB_FACTOR;
774 if (local_max > 0 && local_max < WLC_TXPWR_MAX)
41feb5ed 775 return (u8) local_max;
a9533e7e
HP
776 if (local_max < 0)
777 return 0;
778 }
779
780 return local;
781}
782
783/* propagate home chanspec to all bsscfgs in case bsscfg->current_bss->chanspec is referenced */
c6a9e1fc 784void wlc_set_home_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
a9533e7e
HP
785{
786 if (wlc->home_chanspec != chanspec) {
787 int idx;
788 wlc_bsscfg_t *cfg;
789
790 wlc->home_chanspec = chanspec;
791
792 FOREACH_BSS(wlc, idx, cfg) {
793 if (!cfg->associated)
794 continue;
795 cfg->target_bss->chanspec = chanspec;
796 cfg->current_bss->chanspec = chanspec;
797 }
798
799 }
800}
801
c6a9e1fc 802static void wlc_set_phy_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
a9533e7e
HP
803{
804 /* Save our copy of the chanspec */
805 wlc->chanspec = chanspec;
806
807 /* Set the chanspec and power limits for this locale after computing
808 * any 11h local tx power constraints.
809 */
810 wlc_channel_set_chanspec(wlc->cmi, chanspec,
811 wlc_local_constraint_qdbm(wlc));
812
813 if (wlc->stf->ss_algosel_auto)
814 wlc_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
815 chanspec);
816
817 wlc_stf_ss_update(wlc, wlc->band);
818
819}
820
c6a9e1fc 821void wlc_set_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
a9533e7e
HP
822{
823 uint bandunit;
0965ae88 824 bool switchband = false;
a9533e7e
HP
825 chanspec_t old_chanspec = wlc->chanspec;
826
827 if (!wlc_valid_chanspec_db(wlc->cmi, chanspec)) {
f4528696
JP
828 WL_ERROR("wl%d: %s: Bad channel %d\n",
829 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
a9533e7e
HP
830 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
831 return;
832 }
833
834 /* Switch bands if necessary */
835 if (NBANDS(wlc) > 1) {
836 bandunit = CHSPEC_WLCBANDUNIT(chanspec);
837 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
0f0881b0 838 switchband = true;
a9533e7e 839 if (wlc->bandlocked) {
f4528696
JP
840 WL_ERROR("wl%d: %s: chspec %d band is locked!\n",
841 wlc->pub->unit, __func__,
842 CHSPEC_CHANNEL(chanspec));
a9533e7e
HP
843 return;
844 }
845 /* BMAC_NOTE: should the setband call come after the wlc_bmac_chanspec() ?
846 * if the setband updates (wlc_bsinit) use low level calls to inspect and
847 * set state, the state inspected may be from the wrong band, or the
848 * following wlc_bmac_set_chanspec() may undo the work.
849 */
850 wlc_setband(wlc, bandunit);
851 }
852 }
853
854 ASSERT(N_ENAB(wlc->pub) || !CHSPEC_IS40(chanspec));
855
856 /* sync up phy/radio chanspec */
857 wlc_set_phy_chanspec(wlc, chanspec);
858
859 /* init antenna selection */
860 if (CHSPEC_WLC_BW(old_chanspec) != CHSPEC_WLC_BW(chanspec)) {
861 if (WLANTSEL_ENAB(wlc))
862 wlc_antsel_init(wlc->asi);
863
864 /* Fix the hardware rateset based on bw.
865 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
866 */
867 wlc_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
868 wlc->band->
869 mimo_cap_40 ? CHSPEC_WLC_BW(chanspec)
870 : 0);
871 }
872
873 /* update some mac configuration since chanspec changed */
874 wlc_ucode_mac_upd(wlc);
875}
876
877#if defined(BCMDBG)
c6a9e1fc 878static int wlc_get_current_txpwr(struct wlc_info *wlc, void *pwr, uint len)
a9533e7e
HP
879{
880 txpwr_limits_t txpwr;
881 tx_power_t power;
882 tx_power_legacy_t *old_power = NULL;
883 int r, c;
884 uint qdbm;
885 bool override;
886
887 if (len == sizeof(tx_power_legacy_t))
888 old_power = (tx_power_legacy_t *) pwr;
889 else if (len < sizeof(tx_power_t))
890 return BCME_BUFTOOSHORT;
891
9249ede9 892 memset(&power, 0, sizeof(tx_power_t));
a9533e7e
HP
893
894 power.chanspec = WLC_BAND_PI_RADIO_CHANSPEC;
895 if (wlc->pub->associated)
896 power.local_chanspec = wlc->home_chanspec;
897
898 /* Return the user target tx power limits for the various rates. Note wlc_phy.c's
899 * public interface only implements getting and setting a single value for all of
900 * rates, so we need to fill the array ourselves.
901 */
902 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
903 for (r = 0; r < WL_TX_POWER_RATES; r++) {
41feb5ed 904 power.user_limit[r] = (u8) qdbm;
a9533e7e
HP
905 }
906
907 power.local_max = wlc->txpwr_local_max * WLC_TXPWR_DB_FACTOR;
908 power.local_constraint =
909 wlc->txpwr_local_constraint * WLC_TXPWR_DB_FACTOR;
910
911 power.antgain[0] = wlc->bandstate[BAND_2G_INDEX]->antgain;
912 power.antgain[1] = wlc->bandstate[BAND_5G_INDEX]->antgain;
913
914 wlc_channel_reg_limits(wlc->cmi, power.chanspec, &txpwr);
915
916#if WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK
917#error "WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK"
918#endif
919
920 /* CCK tx power limits */
921 for (c = 0, r = WL_TX_POWER_CCK_FIRST; c < WL_TX_POWER_CCK_NUM;
922 c++, r++)
923 power.reg_limit[r] = txpwr.cck[c];
924
925#if WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM
926#error "WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM"
927#endif
928
929 /* 20 MHz OFDM SISO tx power limits */
930 for (c = 0, r = WL_TX_POWER_OFDM_FIRST; c < WL_TX_POWER_OFDM_NUM;
931 c++, r++)
932 power.reg_limit[r] = txpwr.ofdm[c];
933
934 if (WLC_PHY_11N_CAP(wlc->band)) {
935
936 /* 20 MHz OFDM CDD tx power limits */
937 for (c = 0, r = WL_TX_POWER_OFDM20_CDD_FIRST;
938 c < WL_TX_POWER_OFDM_NUM; c++, r++)
939 power.reg_limit[r] = txpwr.ofdm_cdd[c];
940
941 /* 40 MHz OFDM SISO tx power limits */
942 for (c = 0, r = WL_TX_POWER_OFDM40_SISO_FIRST;
943 c < WL_TX_POWER_OFDM_NUM; c++, r++)
944 power.reg_limit[r] = txpwr.ofdm_40_siso[c];
945
946 /* 40 MHz OFDM CDD tx power limits */
947 for (c = 0, r = WL_TX_POWER_OFDM40_CDD_FIRST;
948 c < WL_TX_POWER_OFDM_NUM; c++, r++)
949 power.reg_limit[r] = txpwr.ofdm_40_cdd[c];
950
951#if WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM
952#error "WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM"
953#endif
954
955 /* 20MHz MCS0-7 SISO tx power limits */
956 for (c = 0, r = WL_TX_POWER_MCS20_SISO_FIRST;
957 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
958 power.reg_limit[r] = txpwr.mcs_20_siso[c];
959
960 /* 20MHz MCS0-7 CDD tx power limits */
961 for (c = 0, r = WL_TX_POWER_MCS20_CDD_FIRST;
962 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
963 power.reg_limit[r] = txpwr.mcs_20_cdd[c];
964
965 /* 20MHz MCS0-7 STBC tx power limits */
966 for (c = 0, r = WL_TX_POWER_MCS20_STBC_FIRST;
967 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
968 power.reg_limit[r] = txpwr.mcs_20_stbc[c];
969
970 /* 40MHz MCS0-7 SISO tx power limits */
971 for (c = 0, r = WL_TX_POWER_MCS40_SISO_FIRST;
972 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
973 power.reg_limit[r] = txpwr.mcs_40_siso[c];
974
975 /* 40MHz MCS0-7 CDD tx power limits */
976 for (c = 0, r = WL_TX_POWER_MCS40_CDD_FIRST;
977 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
978 power.reg_limit[r] = txpwr.mcs_40_cdd[c];
979
980 /* 40MHz MCS0-7 STBC tx power limits */
981 for (c = 0, r = WL_TX_POWER_MCS40_STBC_FIRST;
982 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
983 power.reg_limit[r] = txpwr.mcs_40_stbc[c];
984
985#if WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM
986#error "WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM"
987#endif
988
989 /* 20MHz MCS8-15 SDM tx power limits */
990 for (c = 0, r = WL_TX_POWER_MCS20_SDM_FIRST;
991 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
992 power.reg_limit[r] = txpwr.mcs_20_mimo[c];
993
994 /* 40MHz MCS8-15 SDM tx power limits */
995 for (c = 0, r = WL_TX_POWER_MCS40_SDM_FIRST;
996 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
997 power.reg_limit[r] = txpwr.mcs_40_mimo[c];
998
999 /* MCS 32 */
1000 power.reg_limit[WL_TX_POWER_MCS_32] = txpwr.mcs32;
1001 }
1002
1003 wlc_phy_txpower_get_current(wlc->band->pi, &power,
1004 CHSPEC_CHANNEL(power.chanspec));
1005
1006 /* copy the tx_power_t struct to the return buffer,
1007 * or convert to a tx_power_legacy_t struct
1008 */
1009 if (!old_power) {
1010 bcopy(&power, pwr, sizeof(tx_power_t));
1011 } else {
1012 int band_idx = CHSPEC_IS2G(power.chanspec) ? 0 : 1;
1013
9249ede9 1014 memset(old_power, 0, sizeof(tx_power_legacy_t));
a9533e7e
HP
1015
1016 old_power->txpwr_local_max = power.local_max;
1017 old_power->txpwr_local_constraint = power.local_constraint;
1018 if (CHSPEC_IS2G(power.chanspec)) {
1019 old_power->txpwr_chan_reg_max = txpwr.cck[0];
1020 old_power->txpwr_est_Pout[band_idx] =
1021 power.est_Pout_cck;
1022 old_power->txpwr_est_Pout_gofdm = power.est_Pout[0];
1023 } else {
1024 old_power->txpwr_chan_reg_max = txpwr.ofdm[0];
1025 old_power->txpwr_est_Pout[band_idx] = power.est_Pout[0];
1026 }
1027 old_power->txpwr_antgain[0] = power.antgain[0];
1028 old_power->txpwr_antgain[1] = power.antgain[1];
1029
1030 for (r = 0; r < NUM_PWRCTRL_RATES; r++) {
1031 old_power->txpwr_band_max[r] = power.user_limit[r];
1032 old_power->txpwr_limit[r] = power.reg_limit[r];
1033 old_power->txpwr_target[band_idx][r] = power.target[r];
1034 if (CHSPEC_IS2G(power.chanspec))
1035 old_power->txpwr_bphy_cck_max[r] =
1036 power.board_limit[r];
1037 else
1038 old_power->txpwr_aphy_max[r] =
1039 power.board_limit[r];
1040 }
1041 }
1042
1043 return 0;
1044}
1045#endif /* defined(BCMDBG) */
1046
c6a9e1fc 1047static u32 wlc_watchdog_backup_bi(struct wlc_info *wlc)
a9533e7e 1048{
66cbd3ab 1049 u32 bi;
a9533e7e
HP
1050 bi = 2 * wlc->cfg->current_bss->dtim_period *
1051 wlc->cfg->current_bss->beacon_period;
1052 if (wlc->bcn_li_dtim)
1053 bi *= wlc->bcn_li_dtim;
1054 else if (wlc->bcn_li_bcn)
1055 /* recalculate bi based on bcn_li_bcn */
1056 bi = 2 * wlc->bcn_li_bcn * wlc->cfg->current_bss->beacon_period;
1057
1058 if (bi < 2 * TIMER_INTERVAL_WATCHDOG)
1059 bi = 2 * TIMER_INTERVAL_WATCHDOG;
1060 return bi;
1061}
1062
1063/* Change to run the watchdog either from a periodic timer or from tbtt handler.
0f0881b0 1064 * Call watchdog from tbtt handler if tbtt is true, watchdog timer otherwise.
a9533e7e 1065 */
c6a9e1fc 1066void wlc_watchdog_upd(struct wlc_info *wlc, bool tbtt)
a9533e7e
HP
1067{
1068 /* make sure changing watchdog driver is allowed */
1069 if (!wlc->pub->up || !wlc->pub->align_wd_tbtt)
1070 return;
1071 if (!tbtt && wlc->WDarmed) {
1072 wl_del_timer(wlc->wl, wlc->wdtimer);
0965ae88 1073 wlc->WDarmed = false;
a9533e7e
HP
1074 }
1075
1076 /* stop watchdog timer and use tbtt interrupt to drive watchdog */
1077 if (tbtt && wlc->WDarmed) {
1078 wl_del_timer(wlc->wl, wlc->wdtimer);
0965ae88 1079 wlc->WDarmed = false;
a9533e7e
HP
1080 wlc->WDlast = OSL_SYSUPTIME();
1081 }
1082 /* arm watchdog timer and drive the watchdog there */
1083 else if (!tbtt && !wlc->WDarmed) {
1084 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
0f0881b0
GKH
1085 true);
1086 wlc->WDarmed = true;
a9533e7e
HP
1087 }
1088 if (tbtt && !wlc->WDarmed) {
1089 wl_add_timer(wlc->wl, wlc->wdtimer, wlc_watchdog_backup_bi(wlc),
0f0881b0
GKH
1090 true);
1091 wlc->WDarmed = true;
a9533e7e
HP
1092 }
1093}
1094
c6a9e1fc 1095ratespec_t wlc_lowest_basic_rspec(struct wlc_info *wlc, wlc_rateset_t *rs)
a9533e7e
HP
1096{
1097 ratespec_t lowest_basic_rspec;
1098 uint i;
1099
1100 /* Use the lowest basic rate */
1101 lowest_basic_rspec = rs->rates[0] & RATE_MASK;
1102 for (i = 0; i < rs->count; i++) {
1103 if (rs->rates[i] & WLC_RATE_FLAG) {
1104 lowest_basic_rspec = rs->rates[i] & RATE_MASK;
1105 break;
1106 }
1107 }
1108#if NCONF
1109 /* pick siso/cdd as default for OFDM (note no basic rate MCSs are supported yet) */
1110 if (IS_OFDM(lowest_basic_rspec)) {
1111 lowest_basic_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
1112 }
1113#endif
1114
90ea2296 1115 return lowest_basic_rspec;
a9533e7e
HP
1116}
1117
1118/* This function changes the phytxctl for beacon based on current beacon ratespec AND txant
1119 * setting as per this table:
1120 * ratespec CCK ant = wlc->stf->txant
1121 * OFDM ant = 3
1122 */
c6a9e1fc 1123void wlc_beacon_phytxctl_txant_upd(struct wlc_info *wlc, ratespec_t bcn_rspec)
a9533e7e 1124{
7d4df48e
GKH
1125 u16 phyctl;
1126 u16 phytxant = wlc->stf->phytxant;
1127 u16 mask = PHY_TXC_ANT_MASK;
a9533e7e
HP
1128
1129 /* for non-siso rates or default setting, use the available chains */
1130 if (WLC_PHY_11N_CAP(wlc->band)) {
1131 phytxant = wlc_stf_phytxchain_sel(wlc, bcn_rspec);
1132 }
1133
1134 phyctl = wlc_read_shm(wlc, M_BCN_PCTLWD);
1135 phyctl = (phyctl & ~mask) | phytxant;
1136 wlc_write_shm(wlc, M_BCN_PCTLWD, phyctl);
1137}
1138
1139/* centralized protection config change function to simplify debugging, no consistency checking
1140 * this should be called only on changes to avoid overhead in periodic function
1141*/
c6a9e1fc 1142void wlc_protection_upd(struct wlc_info *wlc, uint idx, int val)
a9533e7e 1143{
f4528696 1144 WL_TRACE("wlc_protection_upd: idx %d, val %d\n", idx, val);
a9533e7e
HP
1145
1146 switch (idx) {
1147 case WLC_PROT_G_SPEC:
1148 wlc->protection->_g = (bool) val;
1149 break;
1150 case WLC_PROT_G_OVR:
562c8850 1151 wlc->protection->g_override = (s8) val;
a9533e7e
HP
1152 break;
1153 case WLC_PROT_G_USER:
41feb5ed 1154 wlc->protection->gmode_user = (u8) val;
a9533e7e
HP
1155 break;
1156 case WLC_PROT_OVERLAP:
562c8850 1157 wlc->protection->overlap = (s8) val;
a9533e7e
HP
1158 break;
1159 case WLC_PROT_N_USER:
562c8850 1160 wlc->protection->nmode_user = (s8) val;
a9533e7e
HP
1161 break;
1162 case WLC_PROT_N_CFG:
562c8850 1163 wlc->protection->n_cfg = (s8) val;
a9533e7e
HP
1164 break;
1165 case WLC_PROT_N_CFG_OVR:
562c8850 1166 wlc->protection->n_cfg_override = (s8) val;
a9533e7e
HP
1167 break;
1168 case WLC_PROT_N_NONGF:
1169 wlc->protection->nongf = (bool) val;
1170 break;
1171 case WLC_PROT_N_NONGF_OVR:
562c8850 1172 wlc->protection->nongf_override = (s8) val;
a9533e7e
HP
1173 break;
1174 case WLC_PROT_N_PAM_OVR:
562c8850 1175 wlc->protection->n_pam_override = (s8) val;
a9533e7e
HP
1176 break;
1177 case WLC_PROT_N_OBSS:
1178 wlc->protection->n_obss = (bool) val;
1179 break;
1180
1181 default:
1182 ASSERT(0);
1183 break;
1184 }
1185
1186}
1187
c6a9e1fc 1188static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val)
a9533e7e 1189{
651bd3a9
RV
1190 wlc->ht_cap.cap_info &= ~(IEEE80211_HT_CAP_SGI_20 |
1191 IEEE80211_HT_CAP_SGI_40);
1192 wlc->ht_cap.cap_info |= (val & WLC_N_SGI_20) ?
1193 IEEE80211_HT_CAP_SGI_20 : 0;
1194 wlc->ht_cap.cap_info |= (val & WLC_N_SGI_40) ?
1195 IEEE80211_HT_CAP_SGI_40 : 0;
a9533e7e
HP
1196
1197 if (wlc->pub->up) {
1198 wlc_update_beacon(wlc);
0f0881b0 1199 wlc_update_probe_resp(wlc, true);
a9533e7e
HP
1200 }
1201}
1202
c6a9e1fc 1203static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val)
a9533e7e
HP
1204{
1205 wlc->stf->ldpc = val;
1206
651bd3a9 1207 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_LDPC_CODING;
a9533e7e 1208 if (wlc->stf->ldpc != OFF)
651bd3a9 1209 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_LDPC_CODING;
a9533e7e
HP
1210
1211 if (wlc->pub->up) {
1212 wlc_update_beacon(wlc);
0f0881b0 1213 wlc_update_probe_resp(wlc, true);
0965ae88 1214 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
a9533e7e
HP
1215 }
1216}
1217
1218/*
1219 * ucode, hwmac update
1220 * Channel dependent updates for ucode and hw
1221 */
c6a9e1fc 1222static void wlc_ucode_mac_upd(struct wlc_info *wlc)
a9533e7e
HP
1223{
1224 /* enable or disable any active IBSSs depending on whether or not
1225 * we are on the home channel
1226 */
1227 if (wlc->home_chanspec == WLC_BAND_PI_RADIO_CHANSPEC) {
1228 if (wlc->pub->associated) {
1229 /* BMAC_NOTE: This is something that should be fixed in ucode inits.
1230 * I think that the ucode inits set up the bcn templates and shm values
1231 * with a bogus beacon. This should not be done in the inits. If ucode needs
1232 * to set up a beacon for testing, the test routines should write it down,
1233 * not expect the inits to populate a bogus beacon.
1234 */
1235 if (WLC_PHY_11N_CAP(wlc->band)) {
1236 wlc_write_shm(wlc, M_BCN_TXTSF_OFFSET,
1237 wlc->band->bcntsfoff);
1238 }
1239 }
1240 } else {
1241 /* disable an active IBSS if we are not on the home channel */
1242 }
1243
1244 /* update the various promisc bits */
1245 wlc_mac_bcn_promisc(wlc);
1246 wlc_mac_promisc(wlc);
1247}
1248
c6a9e1fc 1249static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec)
a9533e7e
HP
1250{
1251 wlc_rateset_t default_rateset;
1252 uint parkband;
1253 uint i, band_order[2];
1254
f4528696 1255 WL_TRACE("wl%d: wlc_bandinit_ordered\n", wlc->pub->unit);
a9533e7e
HP
1256 /*
1257 * We might have been bandlocked during down and the chip power-cycled (hibernate).
1258 * figure out the right band to park on
1259 */
1260 if (wlc->bandlocked || NBANDS(wlc) == 1) {
1261 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
1262
1263 parkband = wlc->band->bandunit; /* updated in wlc_bandlock() */
1264 band_order[0] = band_order[1] = parkband;
1265 } else {
1266 /* park on the band of the specified chanspec */
1267 parkband = CHSPEC_WLCBANDUNIT(chanspec);
1268
1269 /* order so that parkband initialize last */
1270 band_order[0] = parkband ^ 1;
1271 band_order[1] = parkband;
1272 }
1273
1274 /* make each band operational, software state init */
1275 for (i = 0; i < NBANDS(wlc); i++) {
1276 uint j = band_order[i];
1277
1278 wlc->band = wlc->bandstate[j];
1279
1280 wlc_default_rateset(wlc, &default_rateset);
1281
1282 /* fill in hw_rate */
1283 wlc_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
0965ae88 1284 false, WLC_RATES_CCK_OFDM, RATE_MASK,
a9533e7e
HP
1285 (bool) N_ENAB(wlc->pub));
1286
1287 /* init basic rate lookup */
1288 wlc_rate_lookup_init(wlc, &default_rateset);
1289 }
1290
1291 /* sync up phy/radio chanspec */
1292 wlc_set_phy_chanspec(wlc, chanspec);
1293}
1294
1295/* band-specific init */
c6a9e1fc 1296static void WLBANDINITFN(wlc_bsinit) (struct wlc_info *wlc)
a2627bc0 1297{
f4528696
JP
1298 WL_TRACE("wl%d: wlc_bsinit: bandunit %d\n",
1299 wlc->pub->unit, wlc->band->bandunit);
a9533e7e
HP
1300
1301 /* write ucode ACK/CTS rate table */
1302 wlc_set_ratetable(wlc);
1303
1304 /* update some band specific mac configuration */
1305 wlc_ucode_mac_upd(wlc);
1306
1307 /* init antenna selection */
1308 if (WLANTSEL_ENAB(wlc))
1309 wlc_antsel_init(wlc->asi);
1310
1311}
1312
1313/* switch to and initialize new band */
c6a9e1fc 1314static void WLBANDINITFN(wlc_setband) (struct wlc_info *wlc, uint bandunit)
a2627bc0 1315{
a9533e7e
HP
1316 int idx;
1317 wlc_bsscfg_t *cfg;
1318
1319 ASSERT(NBANDS(wlc) > 1);
1320 ASSERT(!wlc->bandlocked);
1321 ASSERT(bandunit != wlc->band->bandunit || wlc->bandinit_pending);
1322
1323 wlc->band = wlc->bandstate[bandunit];
1324
1325 if (!wlc->pub->up)
1326 return;
1327
1328 /* wait for at least one beacon before entering sleeping state */
0f0881b0 1329 wlc->PMawakebcn = true;
a9533e7e 1330 FOREACH_AS_STA(wlc, idx, cfg)
0f0881b0 1331 cfg->PMawakebcn = true;
a9533e7e
HP
1332 wlc_set_ps_ctrl(wlc);
1333
1334 /* band-specific initializations */
1335 wlc_bsinit(wlc);
1336}
1337
1338/* Initialize a WME Parameter Info Element with default STA parameters from WMM Spec, Table 12 */
c6a9e1fc 1339void wlc_wme_initparams_sta(struct wlc_info *wlc, wme_param_ie_t *pe)
a9533e7e
HP
1340{
1341 static const wme_param_ie_t stadef = {
1342 WME_OUI,
1343 WME_TYPE,
1344 WME_SUBTYPE_PARAM_IE,
1345 WME_VER,
1346 0,
1347 0,
1348 {
1349 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA,
1350 HTOL16(EDCF_AC_BE_TXOP_STA)},
1351 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA,
1352 HTOL16(EDCF_AC_BK_TXOP_STA)},
1353 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA,
1354 HTOL16(EDCF_AC_VI_TXOP_STA)},
1355 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA,
1356 HTOL16(EDCF_AC_VO_TXOP_STA)}
1357 }
1358 };
1359
1360 ASSERT(sizeof(*pe) == WME_PARAM_IE_LEN);
1361 memcpy(pe, &stadef, sizeof(*pe));
1362}
1363
c6a9e1fc 1364void wlc_wme_setparams(struct wlc_info *wlc, u16 aci, void *arg, bool suspend)
a9533e7e
HP
1365{
1366 int i;
1367 shm_acparams_t acp_shm;
7d4df48e 1368 u16 *shm_entry;
a9533e7e
HP
1369 struct ieee80211_tx_queue_params *params = arg;
1370
1371 ASSERT(wlc);
1372
1373 /* Only apply params if the core is out of reset and has clocks */
1374 if (!wlc->clk) {
f4528696 1375 WL_ERROR("wl%d: %s : no-clock\n", wlc->pub->unit, __func__);
a9533e7e
HP
1376 return;
1377 }
1378
1379 /*
1380 * AP uses AC params from wme_param_ie_ap.
1381 * AP advertises AC params from wme_param_ie.
1382 * STA uses AC params from wme_param_ie.
1383 */
1384
1385 wlc->wme_admctl = 0;
1386
1387 do {
9249ede9 1388 memset((char *)&acp_shm, 0, sizeof(shm_acparams_t));
a9533e7e
HP
1389 /* find out which ac this set of params applies to */
1390 ASSERT(aci < AC_COUNT);
1391 /* set the admission control policy for this AC */
1392 /* wlc->wme_admctl |= 1 << aci; *//* should be set ?? seems like off by default */
1393
1394 /* fill in shm ac params struct */
1395 acp_shm.txop = ltoh16(params->txop);
1396 /* convert from units of 32us to us for ucode */
1397 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
1398 EDCF_TXOP2USEC(acp_shm.txop);
1399 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
1400
1401 if (aci == AC_VI && acp_shm.txop == 0
1402 && acp_shm.aifs < EDCF_AIFSN_MAX)
1403 acp_shm.aifs++;
1404
1405 if (acp_shm.aifs < EDCF_AIFSN_MIN
1406 || acp_shm.aifs > EDCF_AIFSN_MAX) {
f4528696
JP
1407 WL_ERROR("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1408 wlc->pub->unit, acp_shm.aifs);
a9533e7e
HP
1409 continue;
1410 }
1411
1412 acp_shm.cwmin = params->cw_min;
1413 acp_shm.cwmax = params->cw_max;
1414 acp_shm.cwcur = acp_shm.cwmin;
1415 acp_shm.bslots =
1416 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1417 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1418 /* Indicate the new params to the ucode */
1419 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1420 wme_shmemacindex(aci) *
1421 M_EDCF_QLEN +
1422 M_EDCF_STATUS_OFF));
1423 acp_shm.status |= WME_STATUS_NEWAC;
1424
1425 /* Fill in shm acparam table */
7d4df48e 1426 shm_entry = (u16 *) &acp_shm;
a9533e7e
HP
1427 for (i = 0; i < (int)sizeof(shm_acparams_t); i += 2)
1428 wlc_write_shm(wlc,
1429 M_EDCF_QINFO +
1430 wme_shmemacindex(aci) * M_EDCF_QLEN + i,
1431 *shm_entry++);
1432
1433 } while (0);
1434
1435 if (suspend)
1436 wlc_suspend_mac_and_wait(wlc);
1437
1438 if (suspend)
1439 wlc_enable_mac(wlc);
1440
1441}
1442
7cc4a4c0 1443void wlc_edcf_setparams(wlc_bsscfg_t *cfg, bool suspend)
a9533e7e 1444{
c6a9e1fc 1445 struct wlc_info *wlc = cfg->wlc;
a9533e7e
HP
1446 uint aci, i, j;
1447 edcf_acparam_t *edcf_acp;
1448 shm_acparams_t acp_shm;
7d4df48e 1449 u16 *shm_entry;
a9533e7e
HP
1450
1451 ASSERT(cfg);
1452 ASSERT(wlc);
1453
1454 /* Only apply params if the core is out of reset and has clocks */
1455 if (!wlc->clk)
1456 return;
1457
1458 /*
1459 * AP uses AC params from wme_param_ie_ap.
1460 * AP advertises AC params from wme_param_ie.
1461 * STA uses AC params from wme_param_ie.
1462 */
1463
29c4275a 1464 edcf_acp = (edcf_acparam_t *) &wlc->wme_param_ie.acparam[0];
a9533e7e
HP
1465
1466 wlc->wme_admctl = 0;
1467
1468 for (i = 0; i < AC_COUNT; i++, edcf_acp++) {
9249ede9 1469 memset((char *)&acp_shm, 0, sizeof(shm_acparams_t));
a9533e7e
HP
1470 /* find out which ac this set of params applies to */
1471 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
1472 ASSERT(aci < AC_COUNT);
1473 /* set the admission control policy for this AC */
1474 if (edcf_acp->ACI & EDCF_ACM_MASK) {
1475 wlc->wme_admctl |= 1 << aci;
1476 }
1477
1478 /* fill in shm ac params struct */
1479 acp_shm.txop = ltoh16(edcf_acp->TXOP);
1480 /* convert from units of 32us to us for ucode */
1481 wlc->edcf_txop[aci] = acp_shm.txop =
1482 EDCF_TXOP2USEC(acp_shm.txop);
1483 acp_shm.aifs = (edcf_acp->ACI & EDCF_AIFSN_MASK);
1484
1485 if (aci == AC_VI && acp_shm.txop == 0
1486 && acp_shm.aifs < EDCF_AIFSN_MAX)
1487 acp_shm.aifs++;
1488
1489 if (acp_shm.aifs < EDCF_AIFSN_MIN
1490 || acp_shm.aifs > EDCF_AIFSN_MAX) {
f4528696
JP
1491 WL_ERROR("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1492 wlc->pub->unit, acp_shm.aifs);
a9533e7e
HP
1493 continue;
1494 }
1495
1496 /* CWmin = 2^(ECWmin) - 1 */
1497 acp_shm.cwmin = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
1498 /* CWmax = 2^(ECWmax) - 1 */
1499 acp_shm.cwmax = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
1500 >> EDCF_ECWMAX_SHIFT);
1501 acp_shm.cwcur = acp_shm.cwmin;
1502 acp_shm.bslots =
1503 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1504 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1505 /* Indicate the new params to the ucode */
1506 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1507 wme_shmemacindex(aci) *
1508 M_EDCF_QLEN +
1509 M_EDCF_STATUS_OFF));
1510 acp_shm.status |= WME_STATUS_NEWAC;
1511
1512 /* Fill in shm acparam table */
7d4df48e 1513 shm_entry = (u16 *) &acp_shm;
a9533e7e
HP
1514 for (j = 0; j < (int)sizeof(shm_acparams_t); j += 2)
1515 wlc_write_shm(wlc,
1516 M_EDCF_QINFO +
1517 wme_shmemacindex(aci) * M_EDCF_QLEN + j,
1518 *shm_entry++);
1519 }
1520
1521 if (suspend)
1522 wlc_suspend_mac_and_wait(wlc);
1523
1524 if (AP_ENAB(wlc->pub) && WME_ENAB(wlc->pub)) {
1525 wlc_update_beacon(wlc);
0965ae88 1526 wlc_update_probe_resp(wlc, false);
a9533e7e
HP
1527 }
1528
1529 if (suspend)
1530 wlc_enable_mac(wlc);
1531
1532}
1533
c6a9e1fc 1534bool wlc_timers_init(struct wlc_info *wlc, int unit)
a2627bc0 1535{
ca8c1e59
JC
1536 wlc->wdtimer = wl_init_timer(wlc->wl, wlc_watchdog_by_timer,
1537 wlc, "watchdog");
1538 if (!wlc->wdtimer) {
f4528696 1539 WL_ERROR("wl%d: wl_init_timer for wdtimer failed\n", unit);
a9533e7e
HP
1540 goto fail;
1541 }
1542
ca8c1e59
JC
1543 wlc->radio_timer = wl_init_timer(wlc->wl, wlc_radio_timer,
1544 wlc, "radio");
1545 if (!wlc->radio_timer) {
f4528696 1546 WL_ERROR("wl%d: wl_init_timer for radio_timer failed\n", unit);
a9533e7e
HP
1547 goto fail;
1548 }
1549
0f0881b0 1550 return true;
a9533e7e
HP
1551
1552 fail:
0965ae88 1553 return false;
a9533e7e
HP
1554}
1555
1556/*
1557 * Initialize wlc_info default values ...
1558 * may get overrides later in this function
1559 */
c6a9e1fc 1560void wlc_info_init(struct wlc_info *wlc, int unit)
a2627bc0 1561{
a9533e7e
HP
1562 int i;
1563 /* Assume the device is there until proven otherwise */
0f0881b0 1564 wlc->device_present = true;
a9533e7e
HP
1565
1566 /* set default power output percentage to 100 percent */
1567 wlc->txpwr_percent = 100;
1568
1569 /* Save our copy of the chanspec */
1570 wlc->chanspec = CH20MHZ_CHSPEC(1);
1571
1572 /* initialize CCK preamble mode to unassociated state */
0965ae88 1573 wlc->shortpreamble = false;
a9533e7e 1574
0f0881b0 1575 wlc->legacy_probe = true;
a9533e7e
HP
1576
1577 /* various 802.11g modes */
0965ae88 1578 wlc->shortslot = false;
a9533e7e
HP
1579 wlc->shortslot_override = WLC_SHORTSLOT_AUTO;
1580
0f0881b0 1581 wlc->barker_overlap_control = true;
a9533e7e
HP
1582 wlc->barker_preamble = WLC_BARKER_SHORT_ALLOWED;
1583 wlc->txburst_limit_override = AUTO;
1584
1585 wlc_protection_upd(wlc, WLC_PROT_G_OVR, WLC_PROTECTION_AUTO);
0965ae88 1586 wlc_protection_upd(wlc, WLC_PROT_G_SPEC, false);
a9533e7e
HP
1587
1588 wlc_protection_upd(wlc, WLC_PROT_N_CFG_OVR, WLC_PROTECTION_AUTO);
1589 wlc_protection_upd(wlc, WLC_PROT_N_CFG, WLC_N_PROTECTION_OFF);
1590 wlc_protection_upd(wlc, WLC_PROT_N_NONGF_OVR, WLC_PROTECTION_AUTO);
0965ae88 1591 wlc_protection_upd(wlc, WLC_PROT_N_NONGF, false);
a9533e7e
HP
1592 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR, AUTO);
1593
1594 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, WLC_PROTECTION_CTL_OVERLAP);
1595
1596 /* 802.11g draft 4.0 NonERP elt advertisement */
0f0881b0 1597 wlc->include_legacy_erp = true;
a9533e7e
HP
1598
1599 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
1600 wlc->stf->txant = ANT_TX_DEF;
1601
1602 wlc->prb_resp_timeout = WLC_PRB_RESP_TIMEOUT;
1603
1604 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
1605 for (i = 0; i < NFIFO; i++)
1606 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
1607 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
1608
1609 /* default rate fallback retry limits */
1610 wlc->SFBL = RETRY_SHORT_FB;
1611 wlc->LFBL = RETRY_LONG_FB;
1612
1613 /* default mac retry limits */
1614 wlc->SRL = RETRY_SHORT_DEF;
1615 wlc->LRL = RETRY_LONG_DEF;
1616
1617 /* init PM state */
1618 wlc->PM = PM_OFF; /* User's setting of PM mode through IOCTL */
0965ae88
GKH
1619 wlc->PM_override = false; /* Prevents from going to PM if our AP is 'ill' */
1620 wlc->PMenabled = false; /* Current PM state */
1621 wlc->PMpending = false; /* Tracks whether STA indicated PM in the last attempt */
1622 wlc->PMblocked = false; /* To allow blocking going into PM during RM and scans */
a9533e7e
HP
1623
1624 /* In WMM Auto mode, PM is allowed if association is a UAPSD association */
0965ae88 1625 wlc->WME_PM_blocked = false;
a9533e7e
HP
1626
1627 /* Init wme queuing method */
0965ae88 1628 wlc->wme_prec_queuing = false;
a9533e7e
HP
1629
1630 /* Overrides for the core to stay awake under zillion conditions Look for STAY_AWAKE */
0965ae88 1631 wlc->wake = false;
a9533e7e 1632 /* Are we waiting for a response to PS-Poll that we sent */
0965ae88 1633 wlc->PSpoll = false;
a9533e7e
HP
1634
1635 /* APSD defaults */
0f0881b0 1636 wlc->wme_apsd = true;
0965ae88 1637 wlc->apsd_sta_usp = false;
a9533e7e
HP
1638 wlc->apsd_trigger_timeout = 0; /* disable the trigger timer */
1639 wlc->apsd_trigger_ac = AC_BITMAP_ALL;
1640
1641 /* Set flag to indicate that hw keys should be used when available. */
0965ae88 1642 wlc->wsec_swkeys = false;
a9533e7e
HP
1643
1644 /* init the 4 static WEP default keys */
1645 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
1646 wlc->wsec_keys[i] = wlc->wsec_def_keys[i];
41feb5ed 1647 wlc->wsec_keys[i]->idx = (u8) i;
a9533e7e
HP
1648 }
1649
0965ae88 1650 wlc->_regulatory_domain = false; /* 802.11d */
a9533e7e
HP
1651
1652 /* WME QoS mode is Auto by default */
1653 wlc->pub->_wme = AUTO;
1654
1655#ifdef BCMSDIODEV_ENABLED
0f0881b0 1656 wlc->pub->_priofc = true; /* enable priority flow control for sdio dongle */
a9533e7e
HP
1657#endif
1658
1659 wlc->pub->_ampdu = AMPDU_AGG_HOST;
1660 wlc->pub->bcmerror = 0;
0f0881b0
GKH
1661 wlc->ibss_allowed = true;
1662 wlc->ibss_coalesce_allowed = true;
a9533e7e
HP
1663 wlc->pub->_coex = ON;
1664
1665 /* intialize mpc delay */
1666 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
1667
0f0881b0 1668 wlc->pr80838_war = true;
a9533e7e
HP
1669}
1670
c6a9e1fc 1671static bool wlc_state_bmac_sync(struct wlc_info *wlc)
a9533e7e
HP
1672{
1673 wlc_bmac_state_t state_bmac;
1674
1675 if (wlc_bmac_state_get(wlc->hw, &state_bmac) != 0)
0965ae88 1676 return false;
a9533e7e
HP
1677
1678 wlc->machwcap = state_bmac.machwcap;
1679 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR,
562c8850 1680 (s8) state_bmac.preamble_ovr);
a9533e7e 1681
0f0881b0 1682 return true;
a9533e7e
HP
1683}
1684
c6a9e1fc 1685static uint wlc_attach_module(struct wlc_info *wlc)
a2627bc0 1686{
a9533e7e
HP
1687 uint err = 0;
1688 uint unit;
1689 unit = wlc->pub->unit;
1690
ca8c1e59
JC
1691 wlc->asi = wlc_antsel_attach(wlc, wlc->osh, wlc->pub, wlc->hw);
1692 if (wlc->asi == NULL) {
f4528696 1693 WL_ERROR("wl%d: wlc_attach: wlc_antsel_attach failed\n", unit);
a9533e7e
HP
1694 err = 44;
1695 goto fail;
1696 }
1697
ca8c1e59
JC
1698 wlc->ampdu = wlc_ampdu_attach(wlc);
1699 if (wlc->ampdu == NULL) {
f4528696 1700 WL_ERROR("wl%d: wlc_attach: wlc_ampdu_attach failed\n", unit);
a9533e7e
HP
1701 err = 50;
1702 goto fail;
1703 }
1704
1705 /* Initialize event queue; needed before following calls */
1706 wlc->eventq =
1707 wlc_eventq_attach(wlc->pub, wlc, wlc->wl, wlc_process_eventq);
1708 if (wlc->eventq == NULL) {
f4528696 1709 WL_ERROR("wl%d: wlc_attach: wlc_eventq_attachfailed\n", unit);
a9533e7e
HP
1710 err = 57;
1711 goto fail;
1712 }
1713
1714 if ((wlc_stf_attach(wlc) != 0)) {
f4528696 1715 WL_ERROR("wl%d: wlc_attach: wlc_stf_attach failed\n", unit);
a9533e7e
HP
1716 err = 68;
1717 goto fail;
1718 }
1719 fail:
1720 return err;
1721}
1722
08db27dc 1723struct wlc_pub *wlc_pub(void *wlc)
a9533e7e 1724{
c6a9e1fc 1725 return ((struct wlc_info *) wlc)->pub;
a9533e7e
HP
1726}
1727
1728#define CHIP_SUPPORTS_11N(wlc) 1
1729
1730/*
1731 * The common driver entry routine. Error codes should be unique
1732 */
0d2f0724 1733void *wlc_attach(void *wl, u16 vendor, u16 device, uint unit, bool piomode,
e69284f2
BR
1734 struct osl_info *osh, void *regsva, uint bustype,
1735 void *btparam, uint *perr)
0d2f0724 1736{
c6a9e1fc 1737 struct wlc_info *wlc;
a9533e7e
HP
1738 uint err = 0;
1739 uint j;
08db27dc 1740 struct wlc_pub *pub;
a9533e7e
HP
1741 wlc_txq_info_t *qi;
1742 uint n_disabled;
1743
f4528696
JP
1744 WL_NONE("wl%d: %s: vendor 0x%x device 0x%x\n",
1745 unit, __func__, vendor, device);
a9533e7e
HP
1746
1747 ASSERT(WSEC_MAX_RCMTA_KEYS <= WSEC_MAX_KEYS);
1748 ASSERT(WSEC_MAX_DEFAULT_KEYS == WLC_DEFAULT_KEYS);
1749
1750 /* some code depends on packed structures */
e2582ad8 1751 ASSERT(sizeof(struct ethhdr) == ETH_HLEN);
a9533e7e
HP
1752 ASSERT(sizeof(d11regs_t) == SI_CORE_SIZE);
1753 ASSERT(sizeof(ofdm_phy_hdr_t) == D11_PHY_HDR_LEN);
1754 ASSERT(sizeof(cck_phy_hdr_t) == D11_PHY_HDR_LEN);
1755 ASSERT(sizeof(d11txh_t) == D11_TXH_LEN);
1756 ASSERT(sizeof(d11rxhdr_t) == RXHDR_LEN);
3e9796f9 1757 ASSERT(sizeof(struct ieee80211_hdr) == DOT11_A4_HDR_LEN);
f3dc3ea4 1758 ASSERT(sizeof(struct ieee80211_rts) == DOT11_RTS_LEN);
a9533e7e 1759 ASSERT(sizeof(tx_status_t) == TXSTATUS_LEN);
651bd3a9 1760 ASSERT(sizeof(struct ieee80211_ht_cap) == HT_CAP_IE_LEN);
eb816036 1761#ifdef BRCM_FULLMAC
ce0f1b8c 1762 ASSERT(offsetof(wl_scan_params_t, channel_list) ==
a9533e7e 1763 WL_SCAN_PARAMS_FIXED_SIZE);
eb816036 1764#endif
36c63ff6 1765 ASSERT(IS_ALIGNED(offsetof(wsec_key_t, data), sizeof(u32)));
a9533e7e
HP
1766 ASSERT(ISPOWEROF2(MA_WINDOW_SZ));
1767
1768 ASSERT(sizeof(wlc_d11rxhdr_t) <= WL_HWRXOFF);
1769
1770 /*
1771 * Number of replay counters value used in WPA IE must match # rxivs
1772 * supported in wsec_key_t struct. See 802.11i/D3.0 sect. 7.3.2.17
1773 * 'RSN Information Element' figure 8 for this mapping.
1774 */
1775 ASSERT((WPA_CAP_16_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1776 && 16 == WLC_NUMRXIVS)
1777 || (WPA_CAP_4_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1778 && 4 == WLC_NUMRXIVS));
1779
c6a9e1fc
RV
1780 /* allocate struct wlc_info state and its substructures */
1781 wlc = (struct wlc_info *) wlc_attach_malloc(osh, unit, &err, device);
ca8c1e59 1782 if (wlc == NULL)
a9533e7e
HP
1783 goto fail;
1784 wlc->osh = osh;
1785 pub = wlc->pub;
1786
1787#if defined(BCMDBG)
1788 wlc_info_dbg = wlc;
1789#endif
1790
1791 wlc->band = wlc->bandstate[0];
1792 wlc->core = wlc->corestate;
1793 wlc->wl = wl;
1794 pub->unit = unit;
1795 pub->osh = osh;
1796 wlc->btparam = btparam;
1797 pub->_piomode = piomode;
0965ae88 1798 wlc->bandinit_pending = false;
a9533e7e
HP
1799 /* By default restrict TKIP associations from 11n STA's */
1800 wlc->ht_wsec_restriction = WLC_HT_TKIP_RESTRICT;
1801
c6a9e1fc 1802 /* populate struct wlc_info with default values */
a9533e7e
HP
1803 wlc_info_init(wlc, unit);
1804
1805 /* update sta/ap related parameters */
1806 wlc_ap_upd(wlc);
1807
1808 /* 11n_disable nvram */
1809 n_disabled = getintvar(pub->vars, "11n_disable");
1810
1811 /* register a module (to handle iovars) */
1812 wlc_module_register(wlc->pub, wlc_iovars, "wlc_iovars", wlc,
1813 wlc_doiovar, NULL, NULL);
1814
1815 /* low level attach steps(all hw accesses go inside, no more in rest of the attach) */
1816 err = wlc_bmac_attach(wlc, vendor, device, unit, piomode, osh, regsva,
1817 bustype, btparam);
1818 if (err)
1819 goto fail;
1820
1821 /* for some states, due to different info pointer(e,g, wlc, wlc_hw) or master/slave split,
1822 * HIGH driver(both monolithic and HIGH_ONLY) needs to sync states FROM BMAC portion driver
1823 */
1824 if (!wlc_state_bmac_sync(wlc)) {
1825 err = 20;
1826 goto fail;
1827 }
1828
1829 pub->phy_11ncapable = WLC_PHY_11N_CAP(wlc->band);
1830
1831 /* propagate *vars* from BMAC driver to high driver */
1832 wlc_bmac_copyfrom_vars(wlc->hw, &pub->vars, &wlc->vars_size);
1833
a9533e7e
HP
1834
1835 /* set maximum allowed duty cycle */
1836 wlc->tx_duty_cycle_ofdm =
7d4df48e 1837 (u16) getintvar(pub->vars, "tx_duty_cycle_ofdm");
a9533e7e 1838 wlc->tx_duty_cycle_cck =
7d4df48e 1839 (u16) getintvar(pub->vars, "tx_duty_cycle_cck");
a9533e7e
HP
1840
1841 wlc_stf_phy_chain_calc(wlc);
1842
1843 /* txchain 1: txant 0, txchain 2: txant 1 */
1844 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
1845 wlc->stf->txant = wlc->stf->hw_txchain - 1;
1846
1847 /* push to BMAC driver */
1848 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
1849 wlc->stf->hw_rxchain);
1850
a9533e7e
HP
1851 /* pull up some info resulting from the low attach */
1852 {
1853 int i;
1854 for (i = 0; i < NFIFO; i++)
1855 wlc->core->txavail[i] = wlc->hw->txavail[i];
1856 }
a9533e7e 1857
a44d4236 1858 wlc_bmac_hw_etheraddr(wlc->hw, wlc->perm_etheraddr);
a9533e7e
HP
1859
1860 bcopy((char *)&wlc->perm_etheraddr, (char *)&pub->cur_etheraddr,
b8d63078 1861 ETH_ALEN);
a9533e7e
HP
1862
1863 for (j = 0; j < NBANDS(wlc); j++) {
1864 /* Use band 1 for single band 11a */
1865 if (IS_SINGLEBAND_5G(wlc->deviceid))
1866 j = BAND_5G_INDEX;
1867
1868 wlc->band = wlc->bandstate[j];
1869
1870 if (!wlc_attach_stf_ant_init(wlc)) {
1871 err = 24;
1872 goto fail;
1873 }
1874
1875 /* default contention windows size limits */
1876 wlc->band->CWmin = APHY_CWMIN;
1877 wlc->band->CWmax = PHY_CWMAX;
1878
1879 /* init gmode value */
1880 if (BAND_2G(wlc->band->bandtype)) {
1881 wlc->band->gmode = GMODE_AUTO;
1882 wlc_protection_upd(wlc, WLC_PROT_G_USER,
1883 wlc->band->gmode);
1884 }
1885
1886 /* init _n_enab supported mode */
1887 if (WLC_PHY_11N_CAP(wlc->band) && CHIP_SUPPORTS_11N(wlc)) {
1888 if (n_disabled & WLFEATURE_DISABLE_11N) {
1889 pub->_n_enab = OFF;
1890 wlc_protection_upd(wlc, WLC_PROT_N_USER, OFF);
1891 } else {
1892 pub->_n_enab = SUPPORT_11N;
1893 wlc_protection_upd(wlc, WLC_PROT_N_USER,
1894 ((pub->_n_enab ==
1895 SUPPORT_11N) ? WL_11N_2x2 :
1896 WL_11N_3x3));
1897 }
1898 }
1899
1900 /* init per-band default rateset, depend on band->gmode */
1901 wlc_default_rateset(wlc, &wlc->band->defrateset);
1902
1903 /* fill in hw_rateset (used early by WLC_SET_RATESET) */
1904 wlc_rateset_filter(&wlc->band->defrateset,
0965ae88 1905 &wlc->band->hw_rateset, false,
a9533e7e
HP
1906 WLC_RATES_CCK_OFDM, RATE_MASK,
1907 (bool) N_ENAB(wlc->pub));
1908 }
1909
1910 /* update antenna config due to wlc->stf->txant/txchain/ant_rx_ovr change */
1911 wlc_stf_phy_txant_upd(wlc);
1912
1913 /* attach each modules */
1914 err = wlc_attach_module(wlc);
1915 if (err != 0)
1916 goto fail;
1917
1918 if (!wlc_timers_init(wlc, unit)) {
f4528696 1919 WL_ERROR("wl%d: %s: wlc_init_timer failed\n", unit, __func__);
a9533e7e
HP
1920 err = 32;
1921 goto fail;
1922 }
1923
1924 /* depend on rateset, gmode */
1925 wlc->cmi = wlc_channel_mgr_attach(wlc);
1926 if (!wlc->cmi) {
f4528696
JP
1927 WL_ERROR("wl%d: %s: wlc_channel_mgr_attach failed\n",
1928 unit, __func__);
a9533e7e
HP
1929 err = 33;
1930 goto fail;
1931 }
1932
1933 /* init default when all parameters are ready, i.e. ->rateset */
1934 wlc_bss_default_init(wlc);
1935
1936 /*
1937 * Complete the wlc default state initializations..
1938 */
1939
1940 /* allocate our initial queue */
1941 qi = wlc_txq_alloc(wlc, osh);
1942 if (qi == NULL) {
f4528696
JP
1943 WL_ERROR("wl%d: %s: failed to malloc tx queue\n",
1944 unit, __func__);
a9533e7e
HP
1945 err = 100;
1946 goto fail;
1947 }
1948 wlc->active_queue = qi;
1949
1950 wlc->bsscfg[0] = wlc->cfg;
1951 wlc->cfg->_idx = 0;
1952 wlc->cfg->wlc = wlc;
1953 pub->txmaxpkts = MAXTXPKTS;
1954
e4cf544e
AS
1955 pub->_cnt->version = WL_CNT_T_VERSION;
1956 pub->_cnt->length = sizeof(struct wl_cnt);
a9533e7e
HP
1957
1958 WLCNTSET(pub->_wme_cnt->version, WL_WME_CNT_VERSION);
1959 WLCNTSET(pub->_wme_cnt->length, sizeof(wl_wme_cnt_t));
1960
1961 wlc_wme_initparams_sta(wlc, &wlc->wme_param_ie);
1962
1963 wlc->mimoft = FT_HT;
651bd3a9 1964 wlc->ht_cap.cap_info = HT_CAP;
a9533e7e
HP
1965 if (HT_ENAB(wlc->pub))
1966 wlc->stf->ldpc = AUTO;
1967
1968 wlc->mimo_40txbw = AUTO;
1969 wlc->ofdm_40txbw = AUTO;
1970 wlc->cck_40txbw = AUTO;
1971 wlc_update_mimo_band_bwcap(wlc, WLC_N_BW_20IN2G_40IN5G);
1972
1973 /* Enable setting the RIFS Mode bit by default in HT Info IE */
1974 wlc->rifs_advert = AUTO;
1975
1976 /* Set default values of SGI */
1977 if (WLC_SGI_CAP_PHY(wlc)) {
1978 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1979 wlc->sgi_tx = AUTO;
1980 } else if (WLCISSSLPNPHY(wlc->band)) {
1981 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1982 wlc->sgi_tx = AUTO;
1983 } else {
1984 wlc_ht_update_sgi_rx(wlc, 0);
1985 wlc->sgi_tx = OFF;
1986 }
1987
1988 /* *******nvram 11n config overrides Start ********* */
1989
1990 /* apply the sgi override from nvram conf */
1991 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_TX)
1992 wlc->sgi_tx = OFF;
1993
1994 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_RX)
1995 wlc_ht_update_sgi_rx(wlc, 0);
1996
1997 /* apply the stbc override from nvram conf */
1998 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_TX) {
1999 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
2000 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
651bd3a9 2001 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
a9533e7e
HP
2002 }
2003 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_RX)
2004 wlc_stf_stbc_rx_set(wlc, HT_CAP_RX_STBC_NO);
2005
2006 /* apply the GF override from nvram conf */
2007 if (n_disabled & WLFEATURE_DISABLE_11N_GF)
651bd3a9 2008 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_GRN_FLD;
a9533e7e
HP
2009
2010 /* initialize radio_mpc_disable according to wlc->mpc */
2011 wlc_radio_mpc_upd(wlc);
2012
2013 if (WLANTSEL_ENAB(wlc)) {
dfa26436 2014 if ((wlc->pub->sih->chip) == BCM43235_CHIP_ID) {
a9533e7e
HP
2015 if ((getintvar(wlc->pub->vars, "aa2g") == 7) ||
2016 (getintvar(wlc->pub->vars, "aa5g") == 7)) {
2017 wlc_bmac_antsel_set(wlc->hw, 1);
2018 }
2019 } else {
2020 wlc_bmac_antsel_set(wlc->hw, wlc->asi->antsel_avail);
2021 }
2022 }
2023
2024 if (perr)
2025 *perr = 0;
2026
90ea2296 2027 return (void *)wlc;
a9533e7e
HP
2028
2029 fail:
f4528696 2030 WL_ERROR("wl%d: %s: failed with err %d\n", unit, __func__, err);
a9533e7e
HP
2031 if (wlc)
2032 wlc_detach(wlc);
2033
2034 if (perr)
2035 *perr = err;
90ea2296 2036 return NULL;
a9533e7e
HP
2037}
2038
c6a9e1fc 2039static void wlc_attach_antgain_init(struct wlc_info *wlc)
a2627bc0 2040{
a9533e7e
HP
2041 uint unit;
2042 unit = wlc->pub->unit;
2043
2044 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
2045 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
2046 wlc->band->antgain = 8;
2047 } else if (wlc->band->antgain == -1) {
f4528696
JP
2048 WL_ERROR("wl%d: %s: Invalid antennas available in srom, using 2dB\n",
2049 unit, __func__);
a9533e7e
HP
2050 wlc->band->antgain = 8;
2051 } else {
562c8850 2052 s8 gain, fract;
a9533e7e
HP
2053 /* Older sroms specified gain in whole dbm only. In order
2054 * be able to specify qdbm granularity and remain backward compatible
2055 * the whole dbms are now encoded in only low 6 bits and remaining qdbms
2056 * are encoded in the hi 2 bits. 6 bit signed number ranges from
2057 * -32 - 31. Examples: 0x1 = 1 db,
2058 * 0xc1 = 1.75 db (1 + 3 quarters),
2059 * 0x3f = -1 (-1 + 0 quarters),
2060 * 0x7f = -.75 (-1 in low 6 bits + 1 quarters in hi 2 bits) = -3 qdbm.
2061 * 0xbf = -.50 (-1 in low 6 bits + 2 quarters in hi 2 bits) = -2 qdbm.
2062 */
2063 gain = wlc->band->antgain & 0x3f;
2064 gain <<= 2; /* Sign extend */
2065 gain >>= 2;
2066 fract = (wlc->band->antgain & 0xc0) >> 6;
2067 wlc->band->antgain = 4 * gain + fract;
2068 }
2069}
2070
c6a9e1fc 2071static bool wlc_attach_stf_ant_init(struct wlc_info *wlc)
a2627bc0 2072{
a9533e7e
HP
2073 int aa;
2074 uint unit;
2075 char *vars;
2076 int bandtype;
2077
2078 unit = wlc->pub->unit;
2079 vars = wlc->pub->vars;
2080 bandtype = wlc->band->bandtype;
2081
2082 /* get antennas available */
562c8850 2083 aa = (s8) getintvar(vars, (BAND_5G(bandtype) ? "aa5g" : "aa2g"));
a9533e7e 2084 if (aa == 0)
562c8850 2085 aa = (s8) getintvar(vars,
a9533e7e
HP
2086 (BAND_5G(bandtype) ? "aa1" : "aa0"));
2087 if ((aa < 1) || (aa > 15)) {
f4528696
JP
2088 WL_ERROR("wl%d: %s: Invalid antennas available in srom (0x%x), using 3\n",
2089 unit, __func__, aa);
a9533e7e
HP
2090 aa = 3;
2091 }
2092
2093 /* reset the defaults if we have a single antenna */
2094 if (aa == 1) {
2095 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
2096 wlc->stf->txant = ANT_TX_FORCE_0;
2097 } else if (aa == 2) {
2098 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
2099 wlc->stf->txant = ANT_TX_FORCE_1;
2100 } else {
2101 }
2102
2103 /* Compute Antenna Gain */
2104 wlc->band->antgain =
562c8850 2105 (s8) getintvar(vars, (BAND_5G(bandtype) ? "ag1" : "ag0"));
a9533e7e
HP
2106 wlc_attach_antgain_init(wlc);
2107
0f0881b0 2108 return true;
a9533e7e
HP
2109}
2110
a9533e7e 2111
c6a9e1fc 2112static void wlc_timers_deinit(struct wlc_info *wlc)
a2627bc0 2113{
a9533e7e
HP
2114 /* free timer state */
2115 if (wlc->wdtimer) {
2116 wl_free_timer(wlc->wl, wlc->wdtimer);
2117 wlc->wdtimer = NULL;
2118 }
2119 if (wlc->radio_timer) {
2120 wl_free_timer(wlc->wl, wlc->radio_timer);
2121 wlc->radio_timer = NULL;
2122 }
2123}
2124
c6a9e1fc 2125static void wlc_detach_module(struct wlc_info *wlc)
a2627bc0 2126{
a9533e7e
HP
2127 if (wlc->asi) {
2128 wlc_antsel_detach(wlc->asi);
2129 wlc->asi = NULL;
2130 }
2131
2132 if (wlc->ampdu) {
2133 wlc_ampdu_detach(wlc->ampdu);
2134 wlc->ampdu = NULL;
2135 }
2136
2137 wlc_stf_detach(wlc);
2138}
2139
2140/*
2141 * Return a count of the number of driver callbacks still pending.
2142 *
2143 * General policy is that wlc_detach can only dealloc/free software states. It can NOT
2144 * touch hardware registers since the d11core may be in reset and clock may not be available.
2145 * One exception is sb register access, which is possible if crystal is turned on
2146 * After "down" state, driver should avoid software timer with the exception of radio_monitor.
2147 */
c6a9e1fc 2148uint wlc_detach(struct wlc_info *wlc)
a2627bc0 2149{
a9533e7e
HP
2150 uint i;
2151 uint callbacks = 0;
2152
2153 if (wlc == NULL)
2154 return 0;
2155
f4528696 2156 WL_TRACE("wl%d: %s\n", wlc->pub->unit, __func__);
a9533e7e
HP
2157
2158 ASSERT(!wlc->pub->up);
2159
2160 callbacks += wlc_bmac_detach(wlc);
2161
2162 /* delete software timers */
2163 if (!wlc_radio_monitor_stop(wlc))
2164 callbacks++;
2165
2166 if (wlc->eventq) {
2167 wlc_eventq_detach(wlc->eventq);
2168 wlc->eventq = NULL;
2169 }
2170
2171 wlc_channel_mgr_detach(wlc->cmi);
2172
2173 wlc_timers_deinit(wlc);
2174
2175 wlc_detach_module(wlc);
2176
2177 /* free other state */
2178
a9533e7e
HP
2179
2180#ifdef BCMDBG
2181 if (wlc->country_ie_override) {
182acb3c 2182 kfree(wlc->country_ie_override);
a9533e7e
HP
2183 wlc->country_ie_override = NULL;
2184 }
2185#endif /* BCMDBG */
2186
2187 {
2188 /* free dumpcb list */
2189 dumpcb_t *prev, *ptr;
2190 prev = ptr = wlc->dumpcb_head;
2191 while (ptr) {
2192 ptr = prev->next;
182acb3c 2193 kfree(prev);
a9533e7e
HP
2194 prev = ptr;
2195 }
2196 wlc->dumpcb_head = NULL;
2197 }
2198
2199 /* Detach from iovar manager */
2200 wlc_module_unregister(wlc->pub, "wlc_iovars", wlc);
2201
a9533e7e
HP
2202 while (wlc->tx_queues != NULL) {
2203 wlc_txq_free(wlc, wlc->osh, wlc->tx_queues);
2204 }
2205
2206 /*
2207 * consistency check: wlc_module_register/wlc_module_unregister calls
2208 * should match therefore nothing should be left here.
2209 */
2210 for (i = 0; i < WLC_MAXMODULES; i++)
2211 ASSERT(wlc->modulecb[i].name[0] == '\0');
2212
2213 wlc_detach_mfree(wlc, wlc->osh);
90ea2296 2214 return callbacks;
a9533e7e
HP
2215}
2216
2217/* update state that depends on the current value of "ap" */
c6a9e1fc 2218void wlc_ap_upd(struct wlc_info *wlc)
a9533e7e
HP
2219{
2220 if (AP_ENAB(wlc->pub))
2221 wlc->PLCPHdr_override = WLC_PLCP_AUTO; /* AP: short not allowed, but not enforced */
2222 else
2223 wlc->PLCPHdr_override = WLC_PLCP_SHORT; /* STA-BSS; short capable */
2224
2225 /* disable vlan_mode on AP since some legacy STAs cannot rx tagged pkts */
2226 wlc->vlan_mode = AP_ENAB(wlc->pub) ? OFF : AUTO;
2227
2228 /* fixup mpc */
0f0881b0 2229 wlc->mpc = true;
a9533e7e
HP
2230}
2231
2232/* read hwdisable state and propagate to wlc flag */
c6a9e1fc 2233static void wlc_radio_hwdisable_upd(struct wlc_info *wlc)
a9533e7e
HP
2234{
2235 if (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO || wlc->pub->hw_off)
2236 return;
2237
2238 if (wlc_bmac_radio_read_hwdisabled(wlc->hw)) {
2239 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2240 } else {
2241 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2242 }
2243}
2244
0965ae88 2245/* return true if Minimum Power Consumption should be entered, false otherwise */
c6a9e1fc 2246bool wlc_is_non_delay_mpc(struct wlc_info *wlc)
a9533e7e 2247{
0965ae88 2248 return false;
a9533e7e
HP
2249}
2250
c6a9e1fc 2251bool wlc_ismpc(struct wlc_info *wlc)
a9533e7e 2252{
90ea2296 2253 return (wlc->mpc_delay_off == 0) && (wlc_is_non_delay_mpc(wlc));
a9533e7e
HP
2254}
2255
c6a9e1fc 2256void wlc_radio_mpc_upd(struct wlc_info *wlc)
a9533e7e
HP
2257{
2258 bool mpc_radio, radio_state;
2259
2260 /*
2261 * Clear the WL_RADIO_MPC_DISABLE bit when mpc feature is disabled
2262 * in case the WL_RADIO_MPC_DISABLE bit was set. Stop the radio
2263 * monitor also when WL_RADIO_MPC_DISABLE is the only reason that
2264 * the radio is going down.
2265 */
2266 if (!wlc->mpc) {
2267 if (!wlc->pub->radio_disabled)
2268 return;
2269 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2270 wlc_radio_upd(wlc);
2271 if (!wlc->pub->radio_disabled)
2272 wlc_radio_monitor_stop(wlc);
2273 return;
2274 }
2275
2276 /*
2277 * sync ismpc logic with WL_RADIO_MPC_DISABLE bit in wlc->pub->radio_disabled
2278 * to go ON, always call radio_upd synchronously
2279 * to go OFF, postpone radio_upd to later when context is safe(e.g. watchdog)
2280 */
2281 radio_state =
2282 (mboolisset(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE) ? OFF :
2283 ON);
0f0881b0 2284 mpc_radio = (wlc_ismpc(wlc) == true) ? OFF : ON;
a9533e7e
HP
2285
2286 if (radio_state == ON && mpc_radio == OFF)
2287 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2288 else if (radio_state == OFF && mpc_radio == ON) {
2289 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2290 wlc_radio_upd(wlc);
2291 if (wlc->mpc_offcnt < WLC_MPC_THRESHOLD) {
2292 wlc->mpc_dlycnt = WLC_MPC_MAX_DELAYCNT;
2293 } else
2294 wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
2295 wlc->mpc_dur += OSL_SYSUPTIME() - wlc->mpc_laston_ts;
2296 }
2297 /* Below logic is meant to capture the transition from mpc off to mpc on for reasons
2298 * other than wlc->mpc_delay_off keeping the mpc off. In that case reset
2299 * wlc->mpc_delay_off to wlc->mpc_dlycnt, so that we restart the countdown of mpc_delay_off
2300 */
0965ae88 2301 if ((wlc->prev_non_delay_mpc == false) &&
0f0881b0 2302 (wlc_is_non_delay_mpc(wlc) == true) && wlc->mpc_delay_off) {
a9533e7e
HP
2303 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2304 }
2305 wlc->prev_non_delay_mpc = wlc_is_non_delay_mpc(wlc);
2306}
2307
2308/*
2309 * centralized radio disable/enable function,
2310 * invoke radio enable/disable after updating hwradio status
2311 */
c6a9e1fc 2312static void wlc_radio_upd(struct wlc_info *wlc)
a9533e7e 2313{
a9533e7e
HP
2314}
2315
2316/* maintain LED behavior in down state */
c6a9e1fc 2317static void wlc_down_led_upd(struct wlc_info *wlc)
a9533e7e
HP
2318{
2319 ASSERT(!wlc->pub->up);
2320
2321 /* maintain LEDs while in down state, turn on sbclk if not available yet */
2322 /* turn on sbclk if necessary */
2323 if (!AP_ENAB(wlc->pub)) {
0f0881b0 2324 wlc_pllreq(wlc, true, WLC_PLLREQ_FLIP);
a9533e7e 2325
0965ae88 2326 wlc_pllreq(wlc, false, WLC_PLLREQ_FLIP);
a9533e7e
HP
2327 }
2328}
2329
5abb04a6
AS
2330/* update hwradio status and return it */
2331bool wlc_check_radio_disabled(struct wlc_info *wlc)
2332{
2333 wlc_radio_hwdisable_upd(wlc);
2334
2335 return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ? true : false;
2336}
2337
c6a9e1fc 2338void wlc_radio_disable(struct wlc_info *wlc)
a9533e7e
HP
2339{
2340 if (!wlc->pub->up) {
2341 wlc_down_led_upd(wlc);
2342 return;
2343 }
2344
2345 wlc_radio_monitor_start(wlc);
2346 wl_down(wlc->wl);
2347}
2348
c6a9e1fc 2349static void wlc_radio_enable(struct wlc_info *wlc)
a9533e7e
HP
2350{
2351 if (wlc->pub->up)
2352 return;
2353
2354 if (DEVICEREMOVED(wlc))
2355 return;
2356
2357 if (!wlc->down_override) { /* imposed by wl down/out ioctl */
2358 wl_up(wlc->wl);
2359 }
2360}
2361
2362/* periodical query hw radio button while driver is "down" */
2363static void wlc_radio_timer(void *arg)
2364{
c6a9e1fc 2365 struct wlc_info *wlc = (struct wlc_info *) arg;
a9533e7e
HP
2366
2367 if (DEVICEREMOVED(wlc)) {
f4528696 2368 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
a9533e7e
HP
2369 wl_down(wlc->wl);
2370 return;
2371 }
2372
2373 /* cap mpc off count */
2374 if (wlc->mpc_offcnt < WLC_MPC_MAX_DELAYCNT)
2375 wlc->mpc_offcnt++;
2376
2377 /* validate all the reasons driver could be down and running this radio_timer */
2378 ASSERT(wlc->pub->radio_disabled || wlc->down_override);
2379 wlc_radio_hwdisable_upd(wlc);
2380 wlc_radio_upd(wlc);
2381}
2382
c6a9e1fc 2383static bool wlc_radio_monitor_start(struct wlc_info *wlc)
a9533e7e
HP
2384{
2385 /* Don't start the timer if HWRADIO feature is disabled */
2386 if (wlc->radio_monitor || (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO))
0f0881b0 2387 return true;
a9533e7e 2388
0f0881b0
GKH
2389 wlc->radio_monitor = true;
2390 wlc_pllreq(wlc, true, WLC_PLLREQ_RADIO_MON);
2391 wl_add_timer(wlc->wl, wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
2392 return true;
a9533e7e
HP
2393}
2394
c6a9e1fc 2395bool wlc_radio_monitor_stop(struct wlc_info *wlc)
a9533e7e
HP
2396{
2397 if (!wlc->radio_monitor)
0f0881b0 2398 return true;
a9533e7e
HP
2399
2400 ASSERT((wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO) !=
2401 WL_SWFL_NOHWRADIO);
2402
0965ae88
GKH
2403 wlc->radio_monitor = false;
2404 wlc_pllreq(wlc, false, WLC_PLLREQ_RADIO_MON);
90ea2296 2405 return wl_del_timer(wlc->wl, wlc->radio_timer);
a9533e7e
HP
2406}
2407
2408/* bring the driver down, but don't reset hardware */
c6a9e1fc 2409void wlc_out(struct wlc_info *wlc)
a9533e7e 2410{
0f0881b0 2411 wlc_bmac_set_noreset(wlc->hw, true);
a9533e7e
HP
2412 wlc_radio_upd(wlc);
2413 wl_down(wlc->wl);
0965ae88 2414 wlc_bmac_set_noreset(wlc->hw, false);
a9533e7e 2415
0f0881b0
GKH
2416 /* core clk is true in BMAC driver due to noreset, need to mirror it in HIGH */
2417 wlc->clk = true;
a9533e7e
HP
2418
2419 /* This will make sure that when 'up' is done
2420 * after 'out' it'll restore hardware (especially gpios)
2421 */
0965ae88 2422 wlc->pub->hw_up = false;
a9533e7e
HP
2423}
2424
2425#if defined(BCMDBG)
2426/* Verify the sanity of wlc->tx_prec_map. This can be done only by making sure that
2427 * if there is no packet pending for the FIFO, then the corresponding prec bits should be set
2428 * in prec_map. Of course, ignore this rule when block_datafifo is set
2429 */
c6a9e1fc 2430static bool wlc_tx_prec_map_verify(struct wlc_info *wlc)
a9533e7e
HP
2431{
2432 /* For non-WME, both fifos have overlapping prec_map. So it's an error only if both
2433 * fail the check.
2434 */
2435 if (!EDCF_ENAB(wlc->pub)) {
2436 if (!(WLC_TX_FIFO_CHECK(wlc, TX_DATA_FIFO) ||
2437 WLC_TX_FIFO_CHECK(wlc, TX_CTL_FIFO)))
0965ae88 2438 return false;
a9533e7e 2439 else
0f0881b0 2440 return true;
a9533e7e
HP
2441 }
2442
90ea2296 2443 return WLC_TX_FIFO_CHECK(wlc, TX_AC_BK_FIFO)
a9533e7e
HP
2444 && WLC_TX_FIFO_CHECK(wlc, TX_AC_BE_FIFO)
2445 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VI_FIFO)
90ea2296 2446 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VO_FIFO);
a9533e7e
HP
2447}
2448#endif /* BCMDBG */
2449
2450static void wlc_watchdog_by_timer(void *arg)
2451{
c6a9e1fc 2452 struct wlc_info *wlc = (struct wlc_info *) arg;
a9533e7e
HP
2453 wlc_watchdog(arg);
2454 if (WLC_WATCHDOG_TBTT(wlc)) {
2455 /* set to normal osl watchdog period */
2456 wl_del_timer(wlc->wl, wlc->wdtimer);
2457 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
0f0881b0 2458 true);
a9533e7e
HP
2459 }
2460}
2461
2462/* common watchdog code */
2463static void wlc_watchdog(void *arg)
2464{
c6a9e1fc 2465 struct wlc_info *wlc = (struct wlc_info *) arg;
a9533e7e
HP
2466 int i;
2467 wlc_bsscfg_t *cfg;
2468
f4528696 2469 WL_TRACE("wl%d: wlc_watchdog\n", wlc->pub->unit);
a9533e7e
HP
2470
2471 if (!wlc->pub->up)
2472 return;
2473
2474 if (DEVICEREMOVED(wlc)) {
f4528696 2475 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
a9533e7e
HP
2476 wl_down(wlc->wl);
2477 return;
2478 }
2479
2480 /* increment second count */
2481 wlc->pub->now++;
2482
2483 /* delay radio disable */
2484 if (wlc->mpc_delay_off) {
2485 if (--wlc->mpc_delay_off == 0) {
2486 mboolset(wlc->pub->radio_disabled,
2487 WL_RADIO_MPC_DISABLE);
2488 if (wlc->mpc && wlc_ismpc(wlc))
2489 wlc->mpc_offcnt = 0;
2490 wlc->mpc_laston_ts = OSL_SYSUPTIME();
2491 }
2492 }
2493
2494 /* mpc sync */
2495 wlc_radio_mpc_upd(wlc);
2496 /* radio sync: sw/hw/mpc --> radio_disable/radio_enable */
2497 wlc_radio_hwdisable_upd(wlc);
2498 wlc_radio_upd(wlc);
2499 /* if ismpc, driver should be in down state if up/down is allowed */
2500 if (wlc->mpc && wlc_ismpc(wlc))
2501 ASSERT(!wlc->pub->up);
2502 /* if radio is disable, driver may be down, quit here */
2503 if (wlc->pub->radio_disabled)
2504 return;
2505
a9533e7e 2506 wlc_bmac_watchdog(wlc);
a9533e7e
HP
2507
2508 /* occasionally sample mac stat counters to detect 16-bit counter wrap */
e4cf544e 2509 if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
a9533e7e
HP
2510 wlc_statsupd(wlc);
2511
2512 /* Manage TKIP countermeasures timers */
2513 FOREACH_BSS(wlc, i, cfg) {
2514 if (cfg->tk_cm_dt) {
2515 cfg->tk_cm_dt--;
2516 }
2517 if (cfg->tk_cm_bt) {
2518 cfg->tk_cm_bt--;
2519 }
2520 }
2521
2522 /* Call any registered watchdog handlers */
2523 for (i = 0; i < WLC_MAXMODULES; i++) {
2524 if (wlc->modulecb[i].watchdog_fn)
2525 wlc->modulecb[i].watchdog_fn(wlc->modulecb[i].hdl);
2526 }
2527
2528 if (WLCISNPHY(wlc->band) && !wlc->pub->tempsense_disable &&
2529 ((wlc->pub->now - wlc->tempsense_lasttime) >=
2530 WLC_TEMPSENSE_PERIOD)) {
2531 wlc->tempsense_lasttime = wlc->pub->now;
2532 wlc_tempsense_upd(wlc);
2533 }
a9533e7e 2534 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
0f0881b0 2535 ASSERT(wlc_bmac_taclear(wlc->hw, true));
a9533e7e
HP
2536
2537 /* Verify that tx_prec_map and fifos are in sync to avoid lock ups */
2538 ASSERT(wlc_tx_prec_map_verify(wlc));
2539
2540 ASSERT(wlc_ps_check(wlc));
2541}
2542
2543/* make interface operational */
c6a9e1fc 2544int wlc_up(struct wlc_info *wlc)
a2627bc0 2545{
f4528696 2546 WL_TRACE("wl%d: %s:\n", wlc->pub->unit, __func__);
a9533e7e
HP
2547
2548 /* HW is turned off so don't try to access it */
2549 if (wlc->pub->hw_off || DEVICEREMOVED(wlc))
2550 return BCME_RADIOOFF;
2551
2552 if (!wlc->pub->hw_up) {
2553 wlc_bmac_hw_up(wlc->hw);
0f0881b0 2554 wlc->pub->hw_up = true;
a9533e7e
HP
2555 }
2556
2557 if ((wlc->pub->boardflags & BFL_FEM)
dfa26436 2558 && (wlc->pub->sih->chip == BCM4313_CHIP_ID)) {
a9533e7e
HP
2559 if (wlc->pub->boardrev >= 0x1250
2560 && (wlc->pub->boardflags & BFL_FEM_BT)) {
2561 wlc_mhf(wlc, MHF5, MHF5_4313_GPIOCTRL,
2562 MHF5_4313_GPIOCTRL, WLC_BAND_ALL);
2563 } else {
2564 wlc_mhf(wlc, MHF4, MHF4_EXTPA_ENABLE, MHF4_EXTPA_ENABLE,
2565 WLC_BAND_ALL);
2566 }
2567 }
2568
2569 /*
2570 * Need to read the hwradio status here to cover the case where the system
2571 * is loaded with the hw radio disabled. We do not want to bring the driver up in this case.
2572 * if radio is disabled, abort up, lower power, start radio timer and return 0(for NDIS)
2573 * don't call radio_update to avoid looping wlc_up.
2574 *
2575 * wlc_bmac_up_prep() returns either 0 or BCME_RADIOOFF only
2576 */
2577 if (!wlc->pub->radio_disabled) {
2578 int status = wlc_bmac_up_prep(wlc->hw);
2579 if (status == BCME_RADIOOFF) {
2580 if (!mboolisset
2581 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
2582 int idx;
2583 wlc_bsscfg_t *bsscfg;
2584 mboolset(wlc->pub->radio_disabled,
2585 WL_RADIO_HW_DISABLE);
2586
2587 FOREACH_BSS(wlc, idx, bsscfg) {
2588 if (!BSSCFG_STA(bsscfg)
2589 || !bsscfg->enable || !bsscfg->BSS)
2590 continue;
f4528696
JP
2591 WL_ERROR("wl%d.%d: wlc_up: rfdisable -> " "wlc_bsscfg_disable()\n",
2592 wlc->pub->unit, idx);
a9533e7e
HP
2593 }
2594 }
2595 } else
2596 ASSERT(!status);
2597 }
2598
2599 if (wlc->pub->radio_disabled) {
2600 wlc_radio_monitor_start(wlc);
2601 return 0;
2602 }
2603
2604 /* wlc_bmac_up_prep has done wlc_corereset(). so clk is on, set it */
0f0881b0 2605 wlc->clk = true;
a9533e7e
HP
2606
2607 wlc_radio_monitor_stop(wlc);
2608
2609 /* Set EDCF hostflags */
2610 if (EDCF_ENAB(wlc->pub)) {
2611 wlc_mhf(wlc, MHF1, MHF1_EDCF, MHF1_EDCF, WLC_BAND_ALL);
2612 } else {
2613 wlc_mhf(wlc, MHF1, MHF1_EDCF, 0, WLC_BAND_ALL);
2614 }
2615
2616 if (WLC_WAR16165(wlc))
2617 wlc_mhf(wlc, MHF2, MHF2_PCISLOWCLKWAR, MHF2_PCISLOWCLKWAR,
2618 WLC_BAND_ALL);
2619
2620 wl_init(wlc->wl);
0f0881b0 2621 wlc->pub->up = true;
a9533e7e
HP
2622
2623 if (wlc->bandinit_pending) {
2624 wlc_suspend_mac_and_wait(wlc);
2625 wlc_set_chanspec(wlc, wlc->default_bss->chanspec);
0965ae88 2626 wlc->bandinit_pending = false;
a9533e7e
HP
2627 wlc_enable_mac(wlc);
2628 }
2629
2630 wlc_bmac_up_finish(wlc->hw);
2631
2632 /* other software states up after ISR is running */
2633 /* start APs that were to be brought up but are not up yet */
2634 /* if (AP_ENAB(wlc->pub)) wlc_restart_ap(wlc->ap); */
2635
2636 /* Program the TX wme params with the current settings */
2637 wlc_wme_retries_write(wlc);
2638
2639 /* start one second watchdog timer */
2640 ASSERT(!wlc->WDarmed);
0f0881b0
GKH
2641 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
2642 wlc->WDarmed = true;
a9533e7e
HP
2643
2644 /* ensure antenna config is up to date */
2645 wlc_stf_phy_txant_upd(wlc);
2646 /* ensure LDPC config is in sync */
2647 wlc_ht_update_ldpc(wlc, wlc->stf->ldpc);
2648
90ea2296 2649 return 0;
a9533e7e
HP
2650}
2651
2652/* Initialize the base precedence map for dequeueing from txq based on WME settings */
c6a9e1fc 2653static void wlc_tx_prec_map_init(struct wlc_info *wlc)
a2627bc0 2654{
a9533e7e 2655 wlc->tx_prec_map = WLC_PREC_BMP_ALL;
9249ede9 2656 memset(wlc->fifo2prec_map, 0, NFIFO * sizeof(u16));
a9533e7e
HP
2657
2658 /* For non-WME, both fifos have overlapping MAXPRIO. So just disable all precedences
2659 * if either is full.
2660 */
2661 if (!EDCF_ENAB(wlc->pub)) {
2662 wlc->fifo2prec_map[TX_DATA_FIFO] = WLC_PREC_BMP_ALL;
2663 wlc->fifo2prec_map[TX_CTL_FIFO] = WLC_PREC_BMP_ALL;
2664 } else {
2665 wlc->fifo2prec_map[TX_AC_BK_FIFO] = WLC_PREC_BMP_AC_BK;
2666 wlc->fifo2prec_map[TX_AC_BE_FIFO] = WLC_PREC_BMP_AC_BE;
2667 wlc->fifo2prec_map[TX_AC_VI_FIFO] = WLC_PREC_BMP_AC_VI;
2668 wlc->fifo2prec_map[TX_AC_VO_FIFO] = WLC_PREC_BMP_AC_VO;
2669 }
2670}
2671
c6a9e1fc 2672static uint wlc_down_del_timer(struct wlc_info *wlc)
a2627bc0 2673{
a9533e7e
HP
2674 uint callbacks = 0;
2675
2676 return callbacks;
2677}
2678
2679/*
2680 * Mark the interface nonoperational, stop the software mechanisms,
2681 * disable the hardware, free any transient buffer state.
2682 * Return a count of the number of driver callbacks still pending.
2683 */
c6a9e1fc 2684uint wlc_down(struct wlc_info *wlc)
a2627bc0 2685{
a9533e7e
HP
2686
2687 uint callbacks = 0;
2688 int i;
0965ae88 2689 bool dev_gone = false;
a9533e7e
HP
2690 wlc_txq_info_t *qi;
2691
f4528696 2692 WL_TRACE("wl%d: %s:\n", wlc->pub->unit, __func__);
a9533e7e
HP
2693
2694 /* check if we are already in the going down path */
2695 if (wlc->going_down) {
f4528696
JP
2696 WL_ERROR("wl%d: %s: Driver going down so return\n",
2697 wlc->pub->unit, __func__);
a9533e7e
HP
2698 return 0;
2699 }
2700 if (!wlc->pub->up)
90ea2296 2701 return callbacks;
a9533e7e
HP
2702
2703 /* in between, mpc could try to bring down again.. */
0f0881b0 2704 wlc->going_down = true;
a9533e7e
HP
2705
2706 callbacks += wlc_bmac_down_prep(wlc->hw);
2707
2708 dev_gone = DEVICEREMOVED(wlc);
2709
2710 /* Call any registered down handlers */
2711 for (i = 0; i < WLC_MAXMODULES; i++) {
2712 if (wlc->modulecb[i].down_fn)
2713 callbacks +=
2714 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
2715 }
2716
2717 /* cancel the watchdog timer */
2718 if (wlc->WDarmed) {
2719 if (!wl_del_timer(wlc->wl, wlc->wdtimer))
2720 callbacks++;
0965ae88 2721 wlc->WDarmed = false;
a9533e7e
HP
2722 }
2723 /* cancel all other timers */
2724 callbacks += wlc_down_del_timer(wlc);
2725
2726 /* interrupt must have been blocked */
2727 ASSERT((wlc->macintmask == 0) || !wlc->pub->up);
2728
0965ae88 2729 wlc->pub->up = false;
a9533e7e 2730
0965ae88 2731 wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
a9533e7e
HP
2732
2733 /* clear txq flow control */
2734 wlc_txflowcontrol_reset(wlc);
2735
2736 /* flush tx queues */
2737 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
0f0881b0 2738 pktq_flush(wlc->osh, &qi->q, true, NULL, 0);
a9533e7e
HP
2739 ASSERT(pktq_empty(&qi->q));
2740 }
2741
2742 /* flush event queue.
2743 * Should be the last thing done after all the events are generated
2744 * Just delivers the events synchronously instead of waiting for a timer
2745 */
2746 callbacks += wlc_eventq_down(wlc->eventq);
2747
2748 callbacks += wlc_bmac_down_finish(wlc->hw);
2749
2750 /* wlc_bmac_down_finish has done wlc_coredisable(). so clk is off */
0965ae88 2751 wlc->clk = false;
a9533e7e 2752
a9533e7e
HP
2753
2754 /* Verify all packets are flushed from the driver */
7a9b48b3 2755 if (wlc->osh->pktalloced != 0) {
f4528696
JP
2756 WL_ERROR("%d packets not freed at wlc_down!!!!!!\n",
2757 wlc->osh->pktalloced);
a9533e7e
HP
2758 }
2759#ifdef BCMDBG
2760 /* Since all the packets should have been freed,
2761 * all callbacks should have been called
2762 */
2763 for (i = 1; i <= wlc->pub->tunables->maxpktcb; i++)
2764 ASSERT(wlc->pkt_callback[i].fn == NULL);
2765#endif
0965ae88 2766 wlc->going_down = false;
90ea2296 2767 return callbacks;
a9533e7e
HP
2768}
2769
2770/* Set the current gmode configuration */
c6a9e1fc 2771int wlc_set_gmode(struct wlc_info *wlc, u8 gmode, bool config)
a9533e7e
HP
2772{
2773 int ret = 0;
2774 uint i;
2775 wlc_rateset_t rs;
2776 /* Default to 54g Auto */
562c8850 2777 s8 shortslot = WLC_SHORTSLOT_AUTO; /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
0965ae88 2778 bool shortslot_restrict = false; /* Restrict association to stations that support shortslot
a9533e7e 2779 */
0f0881b0 2780 bool ignore_bcns = true; /* Ignore legacy beacons on the same channel */
0965ae88 2781 bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
a9533e7e 2782 int preamble = WLC_PLCP_LONG; /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
0965ae88 2783 bool preamble_restrict = false; /* Restrict association to stations that support short
a9533e7e
HP
2784 * preambles
2785 */
f077f718 2786 struct wlcband *band;
a9533e7e
HP
2787
2788 /* if N-support is enabled, allow Gmode set as long as requested
2789 * Gmode is not GMODE_LEGACY_B
2790 */
2791 if (N_ENAB(wlc->pub) && gmode == GMODE_LEGACY_B)
2792 return BCME_UNSUPPORTED;
2793
2794 /* verify that we are dealing with 2G band and grab the band pointer */
2795 if (wlc->band->bandtype == WLC_BAND_2G)
2796 band = wlc->band;
2797 else if ((NBANDS(wlc) > 1) &&
2798 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == WLC_BAND_2G))
2799 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
2800 else
2801 return BCME_BADBAND;
2802
2803 /* Legacy or bust when no OFDM is supported by regulatory */
2804 if ((wlc_channel_locale_flags_in_band(wlc->cmi, band->bandunit) &
2805 WLC_NO_OFDM) && (gmode != GMODE_LEGACY_B))
2806 return BCME_RANGE;
2807
2808 /* update configuration value */
0f0881b0 2809 if (config == true)
a9533e7e
HP
2810 wlc_protection_upd(wlc, WLC_PROT_G_USER, gmode);
2811
2812 /* Clear supported rates filter */
9249ede9 2813 memset(&wlc->sup_rates_override, 0, sizeof(wlc_rateset_t));
a9533e7e
HP
2814
2815 /* Clear rateset override */
9249ede9 2816 memset(&rs, 0, sizeof(wlc_rateset_t));
a9533e7e
HP
2817
2818 switch (gmode) {
2819 case GMODE_LEGACY_B:
2820 shortslot = WLC_SHORTSLOT_OFF;
2821 wlc_rateset_copy(&gphy_legacy_rates, &rs);
2822
2823 break;
2824
2825 case GMODE_LRS:
2826 if (AP_ENAB(wlc->pub))
2827 wlc_rateset_copy(&cck_rates, &wlc->sup_rates_override);
2828 break;
2829
2830 case GMODE_AUTO:
2831 /* Accept defaults */
2832 break;
2833
2834 case GMODE_ONLY:
0f0881b0 2835 ofdm_basic = true;
a9533e7e 2836 preamble = WLC_PLCP_SHORT;
0f0881b0 2837 preamble_restrict = true;
a9533e7e
HP
2838 break;
2839
2840 case GMODE_PERFORMANCE:
2841 if (AP_ENAB(wlc->pub)) /* Put all rates into the Supported Rates element */
2842 wlc_rateset_copy(&cck_ofdm_rates,
2843 &wlc->sup_rates_override);
2844
2845 shortslot = WLC_SHORTSLOT_ON;
0f0881b0
GKH
2846 shortslot_restrict = true;
2847 ofdm_basic = true;
a9533e7e 2848 preamble = WLC_PLCP_SHORT;
0f0881b0 2849 preamble_restrict = true;
a9533e7e
HP
2850 break;
2851
2852 default:
2853 /* Error */
f4528696
JP
2854 WL_ERROR("wl%d: %s: invalid gmode %d\n",
2855 wlc->pub->unit, __func__, gmode);
a9533e7e
HP
2856 return BCME_UNSUPPORTED;
2857 }
2858
2859 /*
2860 * If we are switching to gmode == GMODE_LEGACY_B,
2861 * clean up rate info that may refer to OFDM rates.
2862 */
2863 if ((gmode == GMODE_LEGACY_B) && (band->gmode != GMODE_LEGACY_B)) {
2864 band->gmode = gmode;
2865 if (band->rspec_override && !IS_CCK(band->rspec_override)) {
2866 band->rspec_override = 0;
2867 wlc_reprate_init(wlc);
2868 }
2869 if (band->mrspec_override && !IS_CCK(band->mrspec_override)) {
2870 band->mrspec_override = 0;
2871 }
2872 }
2873
2874 band->gmode = gmode;
2875
2876 wlc->ignore_bcns = ignore_bcns;
2877
2878 wlc->shortslot_override = shortslot;
2879
2880 if (AP_ENAB(wlc->pub)) {
2881 /* wlc->ap->shortslot_restrict = shortslot_restrict; */
2882 wlc->PLCPHdr_override =
2883 (preamble !=
2884 WLC_PLCP_LONG) ? WLC_PLCP_SHORT : WLC_PLCP_AUTO;
2885 }
2886
2887 if ((AP_ENAB(wlc->pub) && preamble != WLC_PLCP_LONG)
2888 || preamble == WLC_PLCP_SHORT)
04795017 2889 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
a9533e7e 2890 else
04795017 2891 wlc->default_bss->capability &= ~WLAN_CAPABILITY_SHORT_PREAMBLE;
a9533e7e
HP
2892
2893 /* Update shortslot capability bit for AP and IBSS */
2894 if ((AP_ENAB(wlc->pub) && shortslot == WLC_SHORTSLOT_AUTO) ||
2895 shortslot == WLC_SHORTSLOT_ON)
04795017 2896 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
a9533e7e 2897 else
04795017
RV
2898 wlc->default_bss->capability &=
2899 ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
a9533e7e
HP
2900
2901 /* Use the default 11g rateset */
2902 if (!rs.count)
2903 wlc_rateset_copy(&cck_ofdm_rates, &rs);
2904
2905 if (ofdm_basic) {
2906 for (i = 0; i < rs.count; i++) {
2907 if (rs.rates[i] == WLC_RATE_6M
2908 || rs.rates[i] == WLC_RATE_12M
2909 || rs.rates[i] == WLC_RATE_24M)
2910 rs.rates[i] |= WLC_RATE_FLAG;
2911 }
2912 }
2913
2914 /* Set default bss rateset */
2915 wlc->default_bss->rateset.count = rs.count;
2916 bcopy((char *)rs.rates, (char *)wlc->default_bss->rateset.rates,
2917 sizeof(wlc->default_bss->rateset.rates));
2918
2919 return ret;
2920}
2921
c6a9e1fc 2922static int wlc_nmode_validate(struct wlc_info *wlc, s32 nmode)
a9533e7e
HP
2923{
2924 int err = 0;
2925
2926 switch (nmode) {
2927
2928 case OFF:
2929 break;
2930
2931 case AUTO:
2932 case WL_11N_2x2:
2933 case WL_11N_3x3:
2934 if (!(WLC_PHY_11N_CAP(wlc->band)))
2935 err = BCME_BADBAND;
2936 break;
2937
2938 default:
2939 err = BCME_RANGE;
2940 break;
2941 }
2942
2943 return err;
2944}
2945
c6a9e1fc 2946int wlc_set_nmode(struct wlc_info *wlc, s32 nmode)
a9533e7e
HP
2947{
2948 uint i;
2949 int err;
2950
2951 err = wlc_nmode_validate(wlc, nmode);
2952 ASSERT(err == 0);
2953 if (err)
2954 return err;
2955
2956 switch (nmode) {
2957 case OFF:
2958 wlc->pub->_n_enab = OFF;
2959 wlc->default_bss->flags &= ~WLC_BSS_HT;
2960 /* delete the mcs rates from the default and hw ratesets */
2961 wlc_rateset_mcs_clear(&wlc->default_bss->rateset);
2962 for (i = 0; i < NBANDS(wlc); i++) {
2963 memset(wlc->bandstate[i]->hw_rateset.mcs, 0,
2964 MCSSET_LEN);
2965 if (IS_MCS(wlc->band->rspec_override)) {
2966 wlc->bandstate[i]->rspec_override = 0;
2967 wlc_reprate_init(wlc);
2968 }
2969 if (IS_MCS(wlc->band->mrspec_override))
2970 wlc->bandstate[i]->mrspec_override = 0;
2971 }
2972 break;
2973
2974 case AUTO:
2975 if (wlc->stf->txstreams == WL_11N_3x3)
2976 nmode = WL_11N_3x3;
2977 else
2978 nmode = WL_11N_2x2;
2979 case WL_11N_2x2:
2980 case WL_11N_3x3:
2981 ASSERT(WLC_PHY_11N_CAP(wlc->band));
2982 /* force GMODE_AUTO if NMODE is ON */
0f0881b0 2983 wlc_set_gmode(wlc, GMODE_AUTO, true);
a9533e7e
HP
2984 if (nmode == WL_11N_3x3)
2985 wlc->pub->_n_enab = SUPPORT_HT;
2986 else
2987 wlc->pub->_n_enab = SUPPORT_11N;
2988 wlc->default_bss->flags |= WLC_BSS_HT;
2989 /* add the mcs rates to the default and hw ratesets */
2990 wlc_rateset_mcs_build(&wlc->default_bss->rateset,
2991 wlc->stf->txstreams);
2992 for (i = 0; i < NBANDS(wlc); i++)
2993 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
2994 wlc->default_bss->rateset.mcs, MCSSET_LEN);
2995 break;
2996
2997 default:
2998 ASSERT(0);
2999 break;
3000 }
3001
3002 return err;
3003}
3004
c6a9e1fc 3005static int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg)
a9533e7e
HP
3006{
3007 wlc_rateset_t rs, new;
3008 uint bandunit;
3009
3010 bcopy((char *)rs_arg, (char *)&rs, sizeof(wlc_rateset_t));
3011
3012 /* check for bad count value */
3013 if ((rs.count == 0) || (rs.count > WLC_NUMRATES))
3014 return BCME_BADRATESET;
3015
3016 /* try the current band */
3017 bandunit = wlc->band->bandunit;
3018 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3019 if (wlc_rate_hwrs_filter_sort_validate
0f0881b0 3020 (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
a9533e7e
HP
3021 wlc->stf->txstreams))
3022 goto good;
3023
3024 /* try the other band */
3025 if (IS_MBAND_UNLOCKED(wlc)) {
3026 bandunit = OTHERBANDUNIT(wlc);
3027 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3028 if (wlc_rate_hwrs_filter_sort_validate(&new,
3029 &wlc->
3030 bandstate[bandunit]->
0f0881b0 3031 hw_rateset, true,
a9533e7e
HP
3032 wlc->stf->txstreams))
3033 goto good;
3034 }
3035
3036 return BCME_ERROR;
3037
3038 good:
3039 /* apply new rateset */
3040 bcopy((char *)&new, (char *)&wlc->default_bss->rateset,
3041 sizeof(wlc_rateset_t));
3042 bcopy((char *)&new, (char *)&wlc->bandstate[bandunit]->defrateset,
3043 sizeof(wlc_rateset_t));
90ea2296 3044 return 0;
a9533e7e
HP
3045}
3046
3047/* simplified integer set interface for common ioctl handler */
c6a9e1fc 3048int wlc_set(struct wlc_info *wlc, int cmd, int arg)
a9533e7e
HP
3049{
3050 return wlc_ioctl(wlc, cmd, (void *)&arg, sizeof(arg), NULL);
3051}
3052
3053/* simplified integer get interface for common ioctl handler */
c6a9e1fc 3054int wlc_get(struct wlc_info *wlc, int cmd, int *arg)
a9533e7e
HP
3055{
3056 return wlc_ioctl(wlc, cmd, arg, sizeof(int), NULL);
3057}
3058
c6a9e1fc 3059static void wlc_ofdm_rateset_war(struct wlc_info *wlc)
a9533e7e 3060{
41feb5ed 3061 u8 r;
0965ae88 3062 bool war = false;
a9533e7e
HP
3063
3064 if (wlc->cfg->associated)
3065 r = wlc->cfg->current_bss->rateset.rates[0];
3066 else
3067 r = wlc->default_bss->rateset.rates[0];
3068
3069 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
3070
3071 return;
3072}
3073
3074int
c6a9e1fc
RV
3075wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
3076 struct wlc_if *wlcif)
a9533e7e 3077{
90ea2296 3078 return _wlc_ioctl(wlc, cmd, arg, len, wlcif);
a9533e7e
HP
3079}
3080
3081/* common ioctl handler. return: 0=ok, -1=error, positive=particular error */
3082static int
c6a9e1fc
RV
3083_wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
3084 struct wlc_if *wlcif)
a9533e7e
HP
3085{
3086 int val, *pval;
3087 bool bool_val;
3088 int bcmerror;
3089 d11regs_t *regs;
3090 uint i;
3091 struct scb *nextscb;
3092 bool ta_ok;
3093 uint band;
3094 rw_reg_t *r;
3095 wlc_bsscfg_t *bsscfg;
e69284f2 3096 struct osl_info *osh;
a9533e7e
HP
3097 wlc_bss_info_t *current_bss;
3098
3099 /* update bsscfg pointer */
3100 bsscfg = NULL; /* XXX: Hack bsscfg to be size one and use this globally */
3101 current_bss = NULL;
3102
3103 /* initialize the following to get rid of compiler warning */
3104 nextscb = NULL;
0965ae88 3105 ta_ok = false;
a9533e7e
HP
3106 band = 0;
3107 r = NULL;
3108
3109 /* If the device is turned off, then it's not "removed" */
3110 if (!wlc->pub->hw_off && DEVICEREMOVED(wlc)) {
f4528696 3111 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
a9533e7e
HP
3112 wl_down(wlc->wl);
3113 return BCME_ERROR;
3114 }
3115
3116 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
3117
3118 /* default argument is generic integer */
2ae3b7ea 3119 pval = arg ? (int *)arg:NULL;
a9533e7e
HP
3120
3121 /* This will prevent the misaligned access */
66cbd3ab 3122 if (pval && (u32) len >= sizeof(val))
a9533e7e
HP
3123 bcopy(pval, &val, sizeof(val));
3124 else
3125 val = 0;
3126
3127 /* bool conversion to avoid duplication below */
3128 bool_val = val != 0;
3129
3130 if (cmd != WLC_SET_CHANNEL)
f4528696
JP
3131 WL_NONE("WLC_IOCTL: cmd %d val 0x%x (%d) len %d\n",
3132 cmd, (uint)val, val, len);
a9533e7e
HP
3133
3134 bcmerror = 0;
3135 regs = wlc->regs;
3136 osh = wlc->osh;
3137
3138 /* A few commands don't need any arguments; all the others do. */
3139 switch (cmd) {
3140 case WLC_UP:
3141 case WLC_OUT:
3142 case WLC_DOWN:
3143 case WLC_DISASSOC:
3144 case WLC_RESTART:
3145 case WLC_REBOOT:
3146 case WLC_START_CHANNEL_QA:
3147 case WLC_INIT:
3148 break;
3149
3150 default:
3151 if ((arg == NULL) || (len <= 0)) {
f4528696
JP
3152 WL_ERROR("wl%d: %s: Command %d needs arguments\n",
3153 wlc->pub->unit, __func__, cmd);
a9533e7e
HP
3154 bcmerror = BCME_BADARG;
3155 goto done;
3156 }
3157 }
3158
3159 switch (cmd) {
3160
3161#if defined(BCMDBG)
3162 case WLC_GET_MSGLEVEL:
3163 *pval = wl_msg_level;
3164 break;
3165
3166 case WLC_SET_MSGLEVEL:
3167 wl_msg_level = val;
3168 break;
3169#endif
3170
3171 case WLC_GET_INSTANCE:
3172 *pval = wlc->pub->unit;
3173 break;
3174
3175 case WLC_GET_CHANNEL:{
3176 channel_info_t *ci = (channel_info_t *) arg;
3177
3178 ASSERT(len > (int)sizeof(ci));
3179
3180 ci->hw_channel =
3181 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC);
3182 ci->target_channel =
3183 CHSPEC_CHANNEL(wlc->default_bss->chanspec);
3184 ci->scan_channel = 0;
3185
3186 break;
3187 }
3188
3189 case WLC_SET_CHANNEL:{
3190 chanspec_t chspec = CH20MHZ_CHSPEC(val);
3191
3192 if (val < 0 || val > MAXCHANNEL) {
3193 bcmerror = BCME_OUTOFRANGECHAN;
3194 break;
3195 }
3196
3197 if (!wlc_valid_chanspec_db(wlc->cmi, chspec)) {
3198 bcmerror = BCME_BADCHAN;
3199 break;
3200 }
3201
3202 if (!wlc->pub->up && IS_MBAND_UNLOCKED(wlc)) {
3203 if (wlc->band->bandunit !=
3204 CHSPEC_WLCBANDUNIT(chspec))
0f0881b0 3205 wlc->bandinit_pending = true;
a9533e7e 3206 else
0965ae88 3207 wlc->bandinit_pending = false;
a9533e7e
HP
3208 }
3209
3210 wlc->default_bss->chanspec = chspec;
3211 /* wlc_BSSinit() will sanitize the rateset before using it.. */
3212 if (wlc->pub->up && !wlc->pub->associated &&
3213 (WLC_BAND_PI_RADIO_CHANSPEC != chspec)) {
3214 wlc_set_home_chanspec(wlc, chspec);
3215 wlc_suspend_mac_and_wait(wlc);
3216 wlc_set_chanspec(wlc, chspec);
3217 wlc_enable_mac(wlc);
3218 }
a9533e7e
HP
3219 break;
3220 }
3221
3222#if defined(BCMDBG)
3223 case WLC_GET_UCFLAGS:
3224 if (!wlc->pub->up) {
3225 bcmerror = BCME_NOTUP;
3226 break;
3227 }
3228
3229 /* optional band is stored in the second integer of incoming buffer */
3230 band =
3231 (len <
3232 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3233
3234 /* bcmerror checking */
ca8c1e59
JC
3235 bcmerror = wlc_iocregchk(wlc, band);
3236 if (bcmerror)
a9533e7e
HP
3237 break;
3238
3239 if (val >= MHFMAX) {
3240 bcmerror = BCME_RANGE;
3241 break;
3242 }
3243
41feb5ed 3244 *pval = wlc_bmac_mhf_get(wlc->hw, (u8) val, WLC_BAND_AUTO);
a9533e7e
HP
3245 break;
3246
3247 case WLC_SET_UCFLAGS:
3248 if (!wlc->pub->up) {
3249 bcmerror = BCME_NOTUP;
3250 break;
3251 }
3252
3253 /* optional band is stored in the second integer of incoming buffer */
3254 band =
3255 (len <
3256 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3257
3258 /* bcmerror checking */
ca8c1e59
JC
3259 bcmerror = wlc_iocregchk(wlc, band);
3260 if (bcmerror)
a9533e7e
HP
3261 break;
3262
7d4df48e 3263 i = (u16) val;
a9533e7e
HP
3264 if (i >= MHFMAX) {
3265 bcmerror = BCME_RANGE;
3266 break;
3267 }
3268
7d4df48e 3269 wlc_mhf(wlc, (u8) i, 0xffff, (u16) (val >> NBITS(u16)),
a9533e7e
HP
3270 WLC_BAND_AUTO);
3271 break;
3272
3273 case WLC_GET_SHMEM:
0f0881b0 3274 ta_ok = true;
a9533e7e
HP
3275
3276 /* optional band is stored in the second integer of incoming buffer */
3277 band =
3278 (len <
3279 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3280
3281 /* bcmerror checking */
ca8c1e59
JC
3282 bcmerror = wlc_iocregchk(wlc, band);
3283 if (bcmerror)
a9533e7e
HP
3284 break;
3285
3286 if (val & 1) {
3287 bcmerror = BCME_BADADDR;
3288 break;
3289 }
3290
7d4df48e 3291 *pval = wlc_read_shm(wlc, (u16) val);
a9533e7e
HP
3292 break;
3293
3294 case WLC_SET_SHMEM:
0f0881b0 3295 ta_ok = true;
a9533e7e
HP
3296
3297 /* optional band is stored in the second integer of incoming buffer */
3298 band =
3299 (len <
3300 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3301
3302 /* bcmerror checking */
ca8c1e59
JC
3303 bcmerror = wlc_iocregchk(wlc, band);
3304 if (bcmerror)
a9533e7e
HP
3305 break;
3306
3307 if (val & 1) {
3308 bcmerror = BCME_BADADDR;
3309 break;
3310 }
3311
7d4df48e
GKH
3312 wlc_write_shm(wlc, (u16) val,
3313 (u16) (val >> NBITS(u16)));
a9533e7e
HP
3314 break;
3315
3316 case WLC_R_REG: /* MAC registers */
0f0881b0 3317 ta_ok = true;
a9533e7e
HP
3318 r = (rw_reg_t *) arg;
3319 band = WLC_BAND_AUTO;
3320
3321 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3322 bcmerror = BCME_BUFTOOSHORT;
3323 break;
3324 }
3325
3326 if (len >= (int)sizeof(rw_reg_t))
3327 band = r->band;
3328
3329 /* bcmerror checking */
ca8c1e59
JC
3330 bcmerror = wlc_iocregchk(wlc, band);
3331 if (bcmerror)
a9533e7e
HP
3332 break;
3333
3334 if ((r->byteoff + r->size) > sizeof(d11regs_t)) {
3335 bcmerror = BCME_BADADDR;
3336 break;
3337 }
66cbd3ab 3338 if (r->size == sizeof(u32))
a9533e7e
HP
3339 r->val =
3340 R_REG(osh,
f024c48a 3341 (u32 *)((unsigned char *)(unsigned long)regs +
a9533e7e 3342 r->byteoff));
7d4df48e 3343 else if (r->size == sizeof(u16))
a9533e7e
HP
3344 r->val =
3345 R_REG(osh,
f024c48a 3346 (u16 *)((unsigned char *)(unsigned long)regs +
a9533e7e
HP
3347 r->byteoff));
3348 else
3349 bcmerror = BCME_BADADDR;
3350 break;
3351
3352 case WLC_W_REG:
0f0881b0 3353 ta_ok = true;
a9533e7e
HP
3354 r = (rw_reg_t *) arg;
3355 band = WLC_BAND_AUTO;
3356
3357 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3358 bcmerror = BCME_BUFTOOSHORT;
3359 break;
3360 }
3361
3362 if (len >= (int)sizeof(rw_reg_t))
3363 band = r->band;
3364
3365 /* bcmerror checking */
ca8c1e59
JC
3366 bcmerror = wlc_iocregchk(wlc, band);
3367 if (bcmerror)
a9533e7e
HP
3368 break;
3369
3370 if (r->byteoff + r->size > sizeof(d11regs_t)) {
3371 bcmerror = BCME_BADADDR;
3372 break;
3373 }
66cbd3ab 3374 if (r->size == sizeof(u32))
a9533e7e 3375 W_REG(osh,
f024c48a 3376 (u32 *)((unsigned char *)(unsigned long) regs +
a9533e7e 3377 r->byteoff), r->val);
7d4df48e 3378 else if (r->size == sizeof(u16))
a9533e7e 3379 W_REG(osh,
f024c48a 3380 (u16 *)((unsigned char *)(unsigned long) regs +
a9533e7e
HP
3381 r->byteoff), r->val);
3382 else
3383 bcmerror = BCME_BADADDR;
3384 break;
3385#endif /* BCMDBG */
3386
3387 case WLC_GET_TXANT:
3388 *pval = wlc->stf->txant;
3389 break;
3390
3391 case WLC_SET_TXANT:
562c8850 3392 bcmerror = wlc_stf_ant_txant_validate(wlc, (s8) val);
a9533e7e
HP
3393 if (bcmerror < 0)
3394 break;
3395
562c8850 3396 wlc->stf->txant = (s8) val;
a9533e7e
HP
3397
3398 /* if down, we are done */
3399 if (!wlc->pub->up)
3400 break;
3401
3402 wlc_suspend_mac_and_wait(wlc);
3403
3404 wlc_stf_phy_txant_upd(wlc);
3405 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
3406
3407 wlc_enable_mac(wlc);
3408
3409 break;
3410
3411 case WLC_GET_ANTDIV:{
41feb5ed 3412 u8 phy_antdiv;
a9533e7e
HP
3413
3414 /* return configured value if core is down */
3415 if (!wlc->pub->up) {
3416 *pval = wlc->stf->ant_rx_ovr;
3417
3418 } else {
3419 if (wlc_phy_ant_rxdiv_get
3420 (wlc->band->pi, &phy_antdiv))
3421 *pval = (int)phy_antdiv;
3422 else
3423 *pval = (int)wlc->stf->ant_rx_ovr;
3424 }
3425
3426 break;
3427 }
3428 case WLC_SET_ANTDIV:
3429 /* values are -1=driver default, 0=force0, 1=force1, 2=start1, 3=start0 */
3430 if ((val < -1) || (val > 3)) {
3431 bcmerror = BCME_RANGE;
3432 break;
3433 }
3434
3435 if (val == -1)
3436 val = ANT_RX_DIV_DEF;
3437
41feb5ed
GKH
3438 wlc->stf->ant_rx_ovr = (u8) val;
3439 wlc_phy_ant_rxdiv_set(wlc->band->pi, (u8) val);
a9533e7e
HP
3440 break;
3441
3442 case WLC_GET_RX_ANT:{ /* get latest used rx antenna */
7d4df48e 3443 u16 rxstatus;
a9533e7e
HP
3444
3445 if (!wlc->pub->up) {
3446 bcmerror = BCME_NOTUP;
3447 break;
3448 }
3449
3450 rxstatus = R_REG(wlc->osh, &wlc->regs->phyrxstatus0);
7d4df48e 3451 if (rxstatus == 0xdead || rxstatus == (u16) -1) {
a9533e7e
HP
3452 bcmerror = BCME_ERROR;
3453 break;
3454 }
3455 *pval = (rxstatus & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
3456 break;
3457 }
3458
3459#if defined(BCMDBG)
3460 case WLC_GET_UCANTDIV:
3461 if (!wlc->clk) {
3462 bcmerror = BCME_NOCLK;
3463 break;
3464 }
3465
3466 *pval =
3467 (wlc_bmac_mhf_get(wlc->hw, MHF1, WLC_BAND_AUTO) &
3468 MHF1_ANTDIV);
3469 break;
3470
3471 case WLC_SET_UCANTDIV:{
3472 if (!wlc->pub->up) {
3473 bcmerror = BCME_NOTUP;
3474 break;
3475 }
3476
3477 /* if multiband, band must be locked */
3478 if (IS_MBAND_UNLOCKED(wlc)) {
3479 bcmerror = BCME_NOTBANDLOCKED;
3480 break;
3481 }
3482
3483 /* 4322 supports antdiv in phy, no need to set it to ucode */
3484 if (WLCISNPHY(wlc->band)
3485 && D11REV_IS(wlc->pub->corerev, 16)) {
f4528696
JP
3486 WL_ERROR("wl%d: can't set ucantdiv for 4322\n",
3487 wlc->pub->unit);
a9533e7e
HP
3488 bcmerror = BCME_UNSUPPORTED;
3489 } else
3490 wlc_mhf(wlc, MHF1, MHF1_ANTDIV,
3491 (val ? MHF1_ANTDIV : 0), WLC_BAND_AUTO);
3492 break;
3493 }
3494#endif /* defined(BCMDBG) */
3495
3496 case WLC_GET_SRL:
3497 *pval = wlc->SRL;
3498 break;
3499
3500 case WLC_SET_SRL:
3501 if (val >= 1 && val <= RETRY_SHORT_MAX) {
3502 int ac;
7d4df48e 3503 wlc->SRL = (u16) val;
a9533e7e
HP
3504
3505 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3506
3507 for (ac = 0; ac < AC_COUNT; ac++) {
3508 WLC_WME_RETRY_SHORT_SET(wlc, ac, wlc->SRL);
3509 }
3510 wlc_wme_retries_write(wlc);
3511 } else
3512 bcmerror = BCME_RANGE;
3513 break;
3514
3515 case WLC_GET_LRL:
3516 *pval = wlc->LRL;
3517 break;
3518
3519 case WLC_SET_LRL:
3520 if (val >= 1 && val <= 255) {
3521 int ac;
7d4df48e 3522 wlc->LRL = (u16) val;
a9533e7e
HP
3523
3524 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3525
3526 for (ac = 0; ac < AC_COUNT; ac++) {
3527 WLC_WME_RETRY_LONG_SET(wlc, ac, wlc->LRL);
3528 }
3529 wlc_wme_retries_write(wlc);
3530 } else
3531 bcmerror = BCME_RANGE;
3532 break;
3533
3534 case WLC_GET_CWMIN:
3535 *pval = wlc->band->CWmin;
3536 break;
3537
3538 case WLC_SET_CWMIN:
3539 if (!wlc->clk) {
3540 bcmerror = BCME_NOCLK;
3541 break;
3542 }
3543
3544 if (val >= 1 && val <= 255) {
7d4df48e 3545 wlc_set_cwmin(wlc, (u16) val);
a9533e7e
HP
3546 } else
3547 bcmerror = BCME_RANGE;
3548 break;
3549
3550 case WLC_GET_CWMAX:
3551 *pval = wlc->band->CWmax;
3552 break;
3553
3554 case WLC_SET_CWMAX:
3555 if (!wlc->clk) {
3556 bcmerror = BCME_NOCLK;
3557 break;
3558 }
3559
3560 if (val >= 255 && val <= 2047) {
7d4df48e 3561 wlc_set_cwmax(wlc, (u16) val);
a9533e7e
HP
3562 } else
3563 bcmerror = BCME_RANGE;
3564 break;
3565
3566 case WLC_GET_RADIO: /* use mask if don't want to expose some internal bits */
3567 *pval = wlc->pub->radio_disabled;
3568 break;
3569
3570 case WLC_SET_RADIO:{ /* 32 bits input, higher 16 bits are mask, lower 16 bits are value to
3571 * set
3572 */
7d4df48e 3573 u16 radiomask, radioval;
a9533e7e
HP
3574 uint validbits =
3575 WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE;
3576 mbool new = 0;
3577
3578 radiomask = (val & 0xffff0000) >> 16;
3579 radioval = val & 0x0000ffff;
3580
3581 if ((radiomask == 0) || (radiomask & ~validbits)
3582 || (radioval & ~validbits)
3583 || ((radioval & ~radiomask) != 0)) {
f4528696
JP
3584 WL_ERROR("SET_RADIO with wrong bits 0x%x\n",
3585 val);
a9533e7e
HP
3586 bcmerror = BCME_RANGE;
3587 break;
3588 }
3589
3590 new =
3591 (wlc->pub->radio_disabled & ~radiomask) | radioval;
3592 wlc->pub->radio_disabled = new;
3593
3594 wlc_radio_hwdisable_upd(wlc);
3595 wlc_radio_upd(wlc);
3596 break;
3597 }
3598
3599 case WLC_GET_PHYTYPE:
3600 *pval = WLC_PHYTYPE(wlc->band->phytype);
3601 break;
3602
3603#if defined(BCMDBG)
3604 case WLC_GET_KEY:
3605 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc))) {
3606 wl_wsec_key_t key;
3607
3608 wsec_key_t *src_key = wlc->wsec_keys[val];
3609
3610 if (len < (int)sizeof(key)) {
3611 bcmerror = BCME_BUFTOOSHORT;
3612 break;
3613 }
3614
9249ede9 3615 memset((char *)&key, 0, sizeof(key));
a9533e7e
HP
3616 if (src_key) {
3617 key.index = src_key->id;
3618 key.len = src_key->len;
3619 bcopy(src_key->data, key.data, key.len);
3620 key.algo = src_key->algo;
3621 if (WSEC_SOFTKEY(wlc, src_key, bsscfg))
3622 key.flags |= WL_SOFT_KEY;
3623 if (src_key->flags & WSEC_PRIMARY_KEY)
3624 key.flags |= WL_PRIMARY_KEY;
3625
a44d4236 3626 bcopy(src_key->ea, key.ea,
b8d63078 3627 ETH_ALEN);
a9533e7e
HP
3628 }
3629
3630 bcopy((char *)&key, arg, sizeof(key));
3631 } else
3632 bcmerror = BCME_BADKEYIDX;
3633 break;
3634#endif /* defined(BCMDBG) */
3635
3636 case WLC_SET_KEY:
3637 bcmerror =
3638 wlc_iovar_op(wlc, "wsec_key", NULL, 0, arg, len, IOV_SET,
3639 wlcif);
3640 break;
3641
3642 case WLC_GET_KEY_SEQ:{
3643 wsec_key_t *key;
3644
3645 if (len < DOT11_WPA_KEY_RSC_LEN) {
3646 bcmerror = BCME_BUFTOOSHORT;
3647 break;
3648 }
3649
3650 /* Return the key's tx iv as an EAPOL sequence counter.
3651 * This will be used to supply the RSC value to a supplicant.
3652 * The format is 8 bytes, with least significant in seq[0].
3653 */
3654
ca8c1e59 3655 key = WSEC_KEY(wlc, val);
a9533e7e 3656 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc)) &&
ca8c1e59 3657 (key != NULL)) {
41feb5ed 3658 u8 seq[DOT11_WPA_KEY_RSC_LEN];
7d4df48e 3659 u16 lo;
66cbd3ab 3660 u32 hi;
a9533e7e 3661 /* group keys in WPA-NONE (IBSS only, AES and TKIP) use a global TXIV */
ed956868 3662 if ((bsscfg->WPA_auth & WPA_AUTH_NONE) &&
a44d4236 3663 is_zero_ether_addr(key->ea)) {
a9533e7e
HP
3664 lo = bsscfg->wpa_none_txiv.lo;
3665 hi = bsscfg->wpa_none_txiv.hi;
3666 } else {
3667 lo = key->txiv.lo;
3668 hi = key->txiv.hi;
3669 }
3670
3671 /* format the buffer, low to high */
3672 seq[0] = lo & 0xff;
3673 seq[1] = (lo >> 8) & 0xff;
3674 seq[2] = hi & 0xff;
3675 seq[3] = (hi >> 8) & 0xff;
3676 seq[4] = (hi >> 16) & 0xff;
3677 seq[5] = (hi >> 24) & 0xff;
3678 seq[6] = 0;
3679 seq[7] = 0;
3680
3681 bcopy((char *)seq, arg, sizeof(seq));
3682 } else {
3683 bcmerror = BCME_BADKEYIDX;
3684 }
3685 break;
3686 }
3687
3688 case WLC_GET_CURR_RATESET:{
3689 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3690 wlc_rateset_t *rs;
3691
3692 if (bsscfg->associated)
3693 rs = &current_bss->rateset;
3694 else
3695 rs = &wlc->default_bss->rateset;
3696
3697 if (len < (int)(rs->count + sizeof(rs->count))) {
3698 bcmerror = BCME_BUFTOOSHORT;
3699 break;
3700 }
3701
3702 /* Copy only legacy rateset section */
3703 ret_rs->count = rs->count;
3704 bcopy(&rs->rates, &ret_rs->rates, rs->count);
3705 break;
3706 }
3707
3708 case WLC_GET_RATESET:{
3709 wlc_rateset_t rs;
3710 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3711
9249ede9 3712 memset(&rs, 0, sizeof(wlc_rateset_t));
29c4275a 3713 wlc_default_rateset(wlc, (wlc_rateset_t *) &rs);
a9533e7e
HP
3714
3715 if (len < (int)(rs.count + sizeof(rs.count))) {
3716 bcmerror = BCME_BUFTOOSHORT;
3717 break;
3718 }
3719
3720 /* Copy only legacy rateset section */
3721 ret_rs->count = rs.count;
3722 bcopy(&rs.rates, &ret_rs->rates, rs.count);
3723 break;
3724 }
3725
3726 case WLC_SET_RATESET:{
3727 wlc_rateset_t rs;
3728 wl_rateset_t *in_rs = (wl_rateset_t *) arg;
3729
3730 if (len < (int)(in_rs->count + sizeof(in_rs->count))) {
3731 bcmerror = BCME_BUFTOOSHORT;
3732 break;
3733 }
3734
3735 if (in_rs->count > WLC_NUMRATES) {
3736 bcmerror = BCME_BUFTOOLONG;
3737 break;
3738 }
3739
9249ede9 3740 memset(&rs, 0, sizeof(wlc_rateset_t));
a9533e7e
HP
3741
3742 /* Copy only legacy rateset section */
3743 rs.count = in_rs->count;
3744 bcopy(&in_rs->rates, &rs.rates, rs.count);
3745
3746 /* merge rateset coming in with the current mcsset */
3747 if (N_ENAB(wlc->pub)) {
3748 if (bsscfg->associated)
3749 bcopy(&current_bss->rateset.mcs[0],
3750 rs.mcs, MCSSET_LEN);
3751 else
3752 bcopy(&wlc->default_bss->rateset.mcs[0],
3753 rs.mcs, MCSSET_LEN);
3754 }
3755
3756 bcmerror = wlc_set_rateset(wlc, &rs);
3757
3758 if (!bcmerror)
3759 wlc_ofdm_rateset_war(wlc);
3760
3761 break;
3762 }
3763
3764 case WLC_GET_BCNPRD:
3765 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3766 *pval = current_bss->beacon_period;
3767 else
3768 *pval = wlc->default_bss->beacon_period;
3769 break;
3770
3771 case WLC_SET_BCNPRD:
3772 /* range [1, 0xffff] */
3773 if (val >= DOT11_MIN_BEACON_PERIOD
3774 && val <= DOT11_MAX_BEACON_PERIOD) {
7d4df48e 3775 wlc->default_bss->beacon_period = (u16) val;
a9533e7e
HP
3776 } else
3777 bcmerror = BCME_RANGE;
3778 break;
3779
3780 case WLC_GET_DTIMPRD:
3781 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3782 *pval = current_bss->dtim_period;
3783 else
3784 *pval = wlc->default_bss->dtim_period;
3785 break;
3786
3787 case WLC_SET_DTIMPRD:
3788 /* range [1, 0xff] */
3789 if (val >= DOT11_MIN_DTIM_PERIOD
3790 && val <= DOT11_MAX_DTIM_PERIOD) {
41feb5ed 3791 wlc->default_bss->dtim_period = (u8) val;
a9533e7e
HP
3792 } else
3793 bcmerror = BCME_RANGE;
3794 break;
3795
3796#ifdef SUPPORT_PS
3797 case WLC_GET_PM:
3798 *pval = wlc->PM;
3799 break;
3800
3801 case WLC_SET_PM:
3802 if ((val >= PM_OFF) && (val <= PM_MAX)) {
41feb5ed 3803 wlc->PM = (u8) val;
a9533e7e
HP
3804 if (wlc->pub->up) {
3805 }
3806 /* Change watchdog driver to align watchdog with tbtt if possible */
3807 wlc_watchdog_upd(wlc, PS_ALLOWED(wlc));
3808 } else
3809 bcmerror = BCME_ERROR;
3810 break;
3811#endif /* SUPPORT_PS */
3812
3813#ifdef SUPPORT_PS
3814#ifdef BCMDBG
3815 case WLC_GET_WAKE:
3816 if (AP_ENAB(wlc->pub)) {
3817 bcmerror = BCME_NOTSTA;
3818 break;
3819 }
3820 *pval = wlc->wake;
3821 break;
3822
3823 case WLC_SET_WAKE:
3824 if (AP_ENAB(wlc->pub)) {
3825 bcmerror = BCME_NOTSTA;
3826 break;
3827 }
3828
0965ae88 3829 wlc->wake = val ? true : false;
a9533e7e
HP
3830
3831 /* if down, we're done */
3832 if (!wlc->pub->up)
3833 break;
3834
3835 /* apply to the mac */
3836 wlc_set_ps_ctrl(wlc);
3837 break;
3838#endif /* BCMDBG */
3839#endif /* SUPPORT_PS */
3840
3841 case WLC_GET_REVINFO:
3842 bcmerror = wlc_get_revision_info(wlc, arg, (uint) len);
3843 break;
3844
3845 case WLC_GET_AP:
3846 *pval = (int)AP_ENAB(wlc->pub);
3847 break;
3848
3849 case WLC_GET_ATIM:
3850 if (bsscfg->associated)
3851 *pval = (int)current_bss->atim_window;
3852 else
3853 *pval = (int)wlc->default_bss->atim_window;
3854 break;
3855
3856 case WLC_SET_ATIM:
66cbd3ab 3857 wlc->default_bss->atim_window = (u32) val;
a9533e7e
HP
3858 break;
3859
3860 case WLC_GET_PKTCNTS:{
3861 get_pktcnt_t *pktcnt = (get_pktcnt_t *) pval;
e4cf544e
AS
3862 wlc_statsupd(wlc);
3863 pktcnt->rx_good_pkt = wlc->pub->_cnt->rxframe;
3864 pktcnt->rx_bad_pkt = wlc->pub->_cnt->rxerror;
a9533e7e 3865 pktcnt->tx_good_pkt =
e4cf544e 3866 wlc->pub->_cnt->txfrmsnt;
a9533e7e 3867 pktcnt->tx_bad_pkt =
e4cf544e
AS
3868 wlc->pub->_cnt->txerror +
3869 wlc->pub->_cnt->txfail;
a9533e7e
HP
3870 if (len >= (int)sizeof(get_pktcnt_t)) {
3871 /* Be backward compatible - only if buffer is large enough */
3872 pktcnt->rx_ocast_good_pkt =
e4cf544e 3873 wlc->pub->_cnt->rxmfrmocast;
a9533e7e
HP
3874 }
3875 break;
3876 }
3877
3878#ifdef SUPPORT_HWKEY
3879 case WLC_GET_WSEC:
3880 bcmerror =
3881 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_GET,
3882 wlcif);
3883 break;
3884
3885 case WLC_SET_WSEC:
3886 bcmerror =
3887 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_SET,
3888 wlcif);
3889 break;
3890
3891 case WLC_GET_WPA_AUTH:
3892 *pval = (int)bsscfg->WPA_auth;
3893 break;
3894
3895 case WLC_SET_WPA_AUTH:
3896 /* change of WPA_Auth modifies the PS_ALLOWED state */
3897 if (BSSCFG_STA(bsscfg)) {
7d4df48e 3898 bsscfg->WPA_auth = (u16) val;
a9533e7e 3899 } else
7d4df48e 3900 bsscfg->WPA_auth = (u16) val;
a9533e7e
HP
3901 break;
3902#endif /* SUPPORT_HWKEY */
3903
3904 case WLC_GET_BANDLIST:
3905 /* count of number of bands, followed by each band type */
3906 *pval++ = NBANDS(wlc);
3907 *pval++ = wlc->band->bandtype;
3908 if (NBANDS(wlc) > 1)
3909 *pval++ = wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype;
3910 break;
3911
3912 case WLC_GET_BAND:
3913 *pval = wlc->bandlocked ? wlc->band->bandtype : WLC_BAND_AUTO;
3914 break;
3915
3916 case WLC_GET_PHYLIST:
3917 {
580a0bd9 3918 unsigned char *cp = arg;
a9533e7e
HP
3919 if (len < 3) {
3920 bcmerror = BCME_BUFTOOSHORT;
3921 break;
3922 }
3923
3924 if (WLCISNPHY(wlc->band)) {
3925 *cp++ = 'n';
3926 } else if (WLCISLCNPHY(wlc->band)) {
3927 *cp++ = 'c';
3928 } else if (WLCISSSLPNPHY(wlc->band)) {
3929 *cp++ = 's';
3930 }
3931 *cp = '\0';
3932 break;
3933 }
3934
3935 case WLC_GET_SHORTSLOT:
3936 *pval = wlc->shortslot;
3937 break;
3938
3939 case WLC_GET_SHORTSLOT_OVERRIDE:
3940 *pval = wlc->shortslot_override;
3941 break;
3942
3943 case WLC_SET_SHORTSLOT_OVERRIDE:
3944 if ((val != WLC_SHORTSLOT_AUTO) &&
3945 (val != WLC_SHORTSLOT_OFF) && (val != WLC_SHORTSLOT_ON)) {
3946 bcmerror = BCME_RANGE;
3947 break;
3948 }
3949
562c8850 3950 wlc->shortslot_override = (s8) val;
a9533e7e
HP
3951
3952 /* shortslot is an 11g feature, so no more work if we are
3953 * currently on the 5G band
3954 */
3955 if (BAND_5G(wlc->band->bandtype))
3956 break;
3957
3958 if (wlc->pub->up && wlc->pub->associated) {
3959 /* let watchdog or beacon processing update shortslot */
3960 } else if (wlc->pub->up) {
3961 /* unassociated shortslot is off */
0965ae88 3962 wlc_switch_shortslot(wlc, false);
a9533e7e
HP
3963 } else {
3964 /* driver is down, so just update the wlc_info value */
3965 if (wlc->shortslot_override == WLC_SHORTSLOT_AUTO) {
0965ae88 3966 wlc->shortslot = false;
a9533e7e
HP
3967 } else {
3968 wlc->shortslot =
3969 (wlc->shortslot_override ==
3970 WLC_SHORTSLOT_ON);
3971 }
3972 }
3973
3974 break;
3975
3976 case WLC_GET_LEGACY_ERP:
3977 *pval = wlc->include_legacy_erp;
3978 break;
3979
3980 case WLC_SET_LEGACY_ERP:
3981 if (wlc->include_legacy_erp == bool_val)
3982 break;
3983
3984 wlc->include_legacy_erp = bool_val;
3985
3986 if (AP_ENAB(wlc->pub) && wlc->clk) {
3987 wlc_update_beacon(wlc);
0f0881b0 3988 wlc_update_probe_resp(wlc, true);
a9533e7e
HP
3989 }
3990 break;
3991
3992 case WLC_GET_GMODE:
3993 if (wlc->band->bandtype == WLC_BAND_2G)
3994 *pval = wlc->band->gmode;
3995 else if (NBANDS(wlc) > 1)
3996 *pval = wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode;
3997 break;
3998
3999 case WLC_SET_GMODE:
4000 if (!wlc->pub->associated)
0f0881b0 4001 bcmerror = wlc_set_gmode(wlc, (u8) val, true);
a9533e7e
HP
4002 else {
4003 bcmerror = BCME_ASSOCIATED;
4004 break;
4005 }
4006 break;
4007
4008 case WLC_GET_GMODE_PROTECTION:
4009 *pval = wlc->protection->_g;
4010 break;
4011
4012 case WLC_GET_PROTECTION_CONTROL:
4013 *pval = wlc->protection->overlap;
4014 break;
4015
4016 case WLC_SET_PROTECTION_CONTROL:
4017 if ((val != WLC_PROTECTION_CTL_OFF) &&
4018 (val != WLC_PROTECTION_CTL_LOCAL) &&
4019 (val != WLC_PROTECTION_CTL_OVERLAP)) {
4020 bcmerror = BCME_RANGE;
4021 break;
4022 }
4023
562c8850 4024 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, (s8) val);
a9533e7e
HP
4025
4026 /* Current g_protection will sync up to the specified control alg in watchdog
4027 * if the driver is up and associated.
4028 * If the driver is down or not associated, the control setting has no effect.
4029 */
4030 break;
4031
4032 case WLC_GET_GMODE_PROTECTION_OVERRIDE:
4033 *pval = wlc->protection->g_override;
4034 break;
4035
4036 case WLC_SET_GMODE_PROTECTION_OVERRIDE:
4037 if ((val != WLC_PROTECTION_AUTO) &&
4038 (val != WLC_PROTECTION_OFF) && (val != WLC_PROTECTION_ON)) {
4039 bcmerror = BCME_RANGE;
4040 break;
4041 }
4042
562c8850 4043 wlc_protection_upd(wlc, WLC_PROT_G_OVR, (s8) val);
a9533e7e
HP
4044
4045 break;
4046
4047 case WLC_SET_SUP_RATESET_OVERRIDE:{
4048 wlc_rateset_t rs, new;
4049
4050 /* copyin */
4051 if (len < (int)sizeof(wlc_rateset_t)) {
4052 bcmerror = BCME_BUFTOOSHORT;
4053 break;
4054 }
4055 bcopy((char *)arg, (char *)&rs, sizeof(wlc_rateset_t));
4056
4057 /* check for bad count value */
4058 if (rs.count > WLC_NUMRATES) {
4059 bcmerror = BCME_BADRATESET; /* invalid rateset */
4060 break;
4061 }
4062
4063 /* this command is only appropriate for gmode operation */
4064 if (!(wlc->band->gmode ||
4065 ((NBANDS(wlc) > 1)
4066 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4067 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4068 break;
4069 }
4070
4071 /* check for an empty rateset to clear the override */
4072 if (rs.count == 0) {
9249ede9 4073 memset(&wlc->sup_rates_override, 0,
a9533e7e
HP
4074 sizeof(wlc_rateset_t));
4075 break;
4076 }
4077
4078 /* validate rateset by comparing pre and post sorted against 11g hw rates */
0965ae88 4079 wlc_rateset_filter(&rs, &new, false, WLC_RATES_CCK_OFDM,
a9533e7e
HP
4080 RATE_MASK, BSS_N_ENAB(wlc, bsscfg));
4081 wlc_rate_hwrs_filter_sort_validate(&new,
4082 &cck_ofdm_rates,
0965ae88 4083 false,
a9533e7e
HP
4084 wlc->stf->txstreams);
4085 if (rs.count != new.count) {
4086 bcmerror = BCME_BADRATESET; /* invalid rateset */
4087 break;
4088 }
4089
4090 /* apply new rateset to the override */
4091 bcopy((char *)&new, (char *)&wlc->sup_rates_override,
4092 sizeof(wlc_rateset_t));
4093
4094 /* update bcn and probe resp if needed */
4095 if (wlc->pub->up && AP_ENAB(wlc->pub)
4096 && wlc->pub->associated) {
4097 wlc_update_beacon(wlc);
0f0881b0 4098 wlc_update_probe_resp(wlc, true);
a9533e7e
HP
4099 }
4100 break;
4101 }
4102
4103 case WLC_GET_SUP_RATESET_OVERRIDE:
4104 /* this command is only appropriate for gmode operation */
4105 if (!(wlc->band->gmode ||
4106 ((NBANDS(wlc) > 1)
4107 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4108 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4109 break;
4110 }
4111 if (len < (int)sizeof(wlc_rateset_t)) {
4112 bcmerror = BCME_BUFTOOSHORT;
4113 break;
4114 }
4115 bcopy((char *)&wlc->sup_rates_override, (char *)arg,
4116 sizeof(wlc_rateset_t));
4117
4118 break;
4119
4120 case WLC_GET_PRB_RESP_TIMEOUT:
4121 *pval = wlc->prb_resp_timeout;
4122 break;
4123
4124 case WLC_SET_PRB_RESP_TIMEOUT:
4125 if (wlc->pub->up) {
4126 bcmerror = BCME_NOTDOWN;
4127 break;
4128 }
4129 if (val < 0 || val >= 0xFFFF) {
4130 bcmerror = BCME_RANGE; /* bad value */
4131 break;
4132 }
7d4df48e 4133 wlc->prb_resp_timeout = (u16) val;
a9533e7e
HP
4134 break;
4135
4136 case WLC_GET_KEY_PRIMARY:{
4137 wsec_key_t *key;
4138
4139 /* treat the 'val' parm as the key id */
ca8c1e59
JC
4140 key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4141 if (key != NULL) {
0965ae88 4142 *pval = key->id == val ? true : false;
a9533e7e
HP
4143 } else {
4144 bcmerror = BCME_BADKEYIDX;
4145 }
4146 break;
4147 }
4148
4149 case WLC_SET_KEY_PRIMARY:{
4150 wsec_key_t *key, *old_key;
4151
4152 bcmerror = BCME_BADKEYIDX;
4153
4154 /* treat the 'val' parm as the key id */
4155 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
ca8c1e59
JC
4156 key = bsscfg->bss_def_keys[i];
4157 if (key != NULL && key->id == val) {
4158 old_key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4159 if (old_key != NULL)
a9533e7e
HP
4160 old_key->flags &=
4161 ~WSEC_PRIMARY_KEY;
4162 key->flags |= WSEC_PRIMARY_KEY;
4163 bsscfg->wsec_index = i;
4164 bcmerror = BCME_OK;
4165 }
4166 }
4167 break;
4168 }
4169
4170#ifdef BCMDBG
4171 case WLC_INIT:
4172 wl_init(wlc->wl);
4173 break;
4174#endif
4175
4176 case WLC_SET_VAR:
4177 case WLC_GET_VAR:{
4178 char *name;
4179 /* validate the name value */
4180 name = (char *)arg;
4181 for (i = 0; i < (uint) len && *name != '\0';
62145822
JC
4182 i++, name++)
4183 ;
a9533e7e
HP
4184
4185 if (i == (uint) len) {
4186 bcmerror = BCME_BUFTOOSHORT;
4187 break;
4188 }
4189 i++; /* include the null in the string length */
4190
4191 if (cmd == WLC_GET_VAR) {
4192 bcmerror =
4193 wlc_iovar_op(wlc, arg,
562c8850 4194 (void *)((s8 *) arg + i),
a9533e7e
HP
4195 len - i, arg, len, IOV_GET,
4196 wlcif);
4197 } else
4198 bcmerror =
4199 wlc_iovar_op(wlc, arg, NULL, 0,
562c8850 4200 (void *)((s8 *) arg + i),
a9533e7e
HP
4201 len - i, IOV_SET, wlcif);
4202
4203 break;
4204 }
4205
4206 case WLC_SET_WSEC_PMK:
4207 bcmerror = BCME_UNSUPPORTED;
4208 break;
4209
4210#if defined(BCMDBG)
4211 case WLC_CURRENT_PWR:
4212 if (!wlc->pub->up)
4213 bcmerror = BCME_NOTUP;
4214 else
4215 bcmerror = wlc_get_current_txpwr(wlc, arg, len);
4216 break;
4217#endif
4218
4219 case WLC_LAST:
f4528696 4220 WL_ERROR("%s: WLC_LAST\n", __func__);
a9533e7e
HP
4221 }
4222 done:
4223
4224 if (bcmerror) {
4225 if (VALID_BCMERROR(bcmerror))
4226 wlc->pub->bcmerror = bcmerror;
4227 else {
4228 bcmerror = 0;
4229 }
4230
4231 }
a9533e7e
HP
4232 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
4233 /* In hw_off condition, IOCTLs that reach here are deemed safe but taclear would
4234 * certainly result in getting -1 for register reads. So skip ta_clear altogether
4235 */
4236 if (!(wlc->pub->hw_off))
4237 ASSERT(wlc_bmac_taclear(wlc->hw, ta_ok) || !ta_ok);
a9533e7e 4238
90ea2296 4239 return bcmerror;
a9533e7e
HP
4240}
4241
4242#if defined(BCMDBG)
4243/* consolidated register access ioctl error checking */
c6a9e1fc 4244int wlc_iocregchk(struct wlc_info *wlc, uint band)
a9533e7e
HP
4245{
4246 /* if band is specified, it must be the current band */
4247 if ((band != WLC_BAND_AUTO) && (band != (uint) wlc->band->bandtype))
90ea2296 4248 return BCME_BADBAND;
a9533e7e
HP
4249
4250 /* if multiband and band is not specified, band must be locked */
4251 if ((band == WLC_BAND_AUTO) && IS_MBAND_UNLOCKED(wlc))
90ea2296 4252 return BCME_NOTBANDLOCKED;
a9533e7e
HP
4253
4254 /* must have core clocks */
4255 if (!wlc->clk)
90ea2296 4256 return BCME_NOCLK;
a9533e7e 4257
90ea2296 4258 return 0;
a9533e7e
HP
4259}
4260#endif /* defined(BCMDBG) */
4261
4262#if defined(BCMDBG)
4263/* For some ioctls, make sure that the pi pointer matches the current phy */
c6a9e1fc 4264int wlc_iocpichk(struct wlc_info *wlc, uint phytype)
a9533e7e
HP
4265{
4266 if (wlc->band->phytype != phytype)
4267 return BCME_BADBAND;
4268 return 0;
4269}
4270#endif
4271
4272/* Look up the given var name in the given table */
7cc4a4c0 4273static const bcm_iovar_t *wlc_iovar_lookup(const bcm_iovar_t *table,
a9533e7e
HP
4274 const char *name)
4275{
4276 const bcm_iovar_t *vi;
4277 const char *lookup_name;
4278
4279 /* skip any ':' delimited option prefixes */
4280 lookup_name = strrchr(name, ':');
4281 if (lookup_name != NULL)
4282 lookup_name++;
4283 else
4284 lookup_name = name;
4285
4286 ASSERT(table != NULL);
4287
4288 for (vi = table; vi->name; vi++) {
4289 if (!strcmp(vi->name, lookup_name))
4290 return vi;
4291 }
4292 /* ran to end of table */
4293
4294 return NULL; /* var name not found */
4295}
4296
4297/* simplified integer get interface for common WLC_GET_VAR ioctl handler */
c6a9e1fc 4298int wlc_iovar_getint(struct wlc_info *wlc, const char *name, int *arg)
a9533e7e 4299{
3e26416e 4300 return wlc_iovar_op(wlc, name, NULL, 0, arg, sizeof(s32), IOV_GET,
a9533e7e
HP
4301 NULL);
4302}
4303
4304/* simplified integer set interface for common WLC_SET_VAR ioctl handler */
c6a9e1fc 4305int wlc_iovar_setint(struct wlc_info *wlc, const char *name, int arg)
a9533e7e
HP
4306{
4307 return wlc_iovar_op(wlc, name, NULL, 0, (void *)&arg, sizeof(arg),
4308 IOV_SET, NULL);
4309}
4310
562c8850 4311/* simplified s8 get interface for common WLC_GET_VAR ioctl handler */
c6a9e1fc 4312int wlc_iovar_gets8(struct wlc_info *wlc, const char *name, s8 *arg)
a9533e7e
HP
4313{
4314 int iovar_int;
4315 int err;
4316
4317 err =
4318 wlc_iovar_op(wlc, name, NULL, 0, &iovar_int, sizeof(iovar_int),
4319 IOV_GET, NULL);
4320 if (!err)
562c8850 4321 *arg = (s8) iovar_int;
a9533e7e
HP
4322
4323 return err;
4324}
4325
4326/*
4327 * register iovar table, watchdog and down handlers.
4328 * calling function must keep 'iovars' until wlc_module_unregister is called.
4329 * 'iovar' must have the last entry's name field being NULL as terminator.
4330 */
08db27dc 4331int wlc_module_register(struct wlc_pub *pub, const bcm_iovar_t *iovars,
0d2f0724
GKH
4332 const char *name, void *hdl, iovar_fn_t i_fn,
4333 watchdog_fn_t w_fn, down_fn_t d_fn)
4334{
c6a9e1fc 4335 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
a9533e7e
HP
4336 int i;
4337
4338 ASSERT(name != NULL);
4339 ASSERT(i_fn != NULL || w_fn != NULL || d_fn != NULL);
4340
4341 /* find an empty entry and just add, no duplication check! */
4342 for (i = 0; i < WLC_MAXMODULES; i++) {
4343 if (wlc->modulecb[i].name[0] == '\0') {
4344 strncpy(wlc->modulecb[i].name, name,
4345 sizeof(wlc->modulecb[i].name) - 1);
4346 wlc->modulecb[i].iovars = iovars;
4347 wlc->modulecb[i].hdl = hdl;
4348 wlc->modulecb[i].iovar_fn = i_fn;
4349 wlc->modulecb[i].watchdog_fn = w_fn;
4350 wlc->modulecb[i].down_fn = d_fn;
4351 return 0;
4352 }
4353 }
4354
4355 /* it is time to increase the capacity */
4356 ASSERT(i < WLC_MAXMODULES);
4357 return BCME_NORESOURCE;
4358}
4359
4360/* unregister module callbacks */
08db27dc 4361int wlc_module_unregister(struct wlc_pub *pub, const char *name, void *hdl)
0d2f0724 4362{
c6a9e1fc 4363 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
a9533e7e
HP
4364 int i;
4365
4366 if (wlc == NULL)
4367 return BCME_NOTFOUND;
4368
4369 ASSERT(name != NULL);
4370
4371 for (i = 0; i < WLC_MAXMODULES; i++) {
4372 if (!strcmp(wlc->modulecb[i].name, name) &&
4373 (wlc->modulecb[i].hdl == hdl)) {
9249ede9 4374 memset(&wlc->modulecb[i], 0, sizeof(modulecb_t));
a9533e7e
HP
4375 return 0;
4376 }
4377 }
4378
4379 /* table not found! */
4380 return BCME_NOTFOUND;
4381}
4382
4383/* Write WME tunable parameters for retransmit/max rate from wlc struct to ucode */
c6a9e1fc 4384static void wlc_wme_retries_write(struct wlc_info *wlc)
a9533e7e
HP
4385{
4386 int ac;
4387
4388 /* Need clock to do this */
4389 if (!wlc->clk)
4390 return;
4391
4392 for (ac = 0; ac < AC_COUNT; ac++) {
4393 wlc_write_shm(wlc, M_AC_TXLMT_ADDR(ac), wlc->wme_retries[ac]);
4394 }
4395}
4396
4397/* Get or set an iovar. The params/p_len pair specifies any additional
4398 * qualifying parameters (e.g. an "element index") for a get, while the
4399 * arg/len pair is the buffer for the value to be set or retrieved.
4400 * Operation (get/set) is specified by the last argument.
4401 * interface context provided by wlcif
4402 *
4403 * All pointers may point into the same buffer.
4404 */
4405int
c6a9e1fc 4406wlc_iovar_op(struct wlc_info *wlc, const char *name,
a9533e7e
HP
4407 void *params, int p_len, void *arg, int len,
4408 bool set, struct wlc_if *wlcif)
4409{
4410 int err = 0;
4411 int val_size;
4412 const bcm_iovar_t *vi = NULL;
66cbd3ab 4413 u32 actionid;
a9533e7e
HP
4414 int i;
4415
4416 ASSERT(name != NULL);
4417
4418 ASSERT(len >= 0);
4419
4420 /* Get MUST have return space */
4421 ASSERT(set || (arg && len));
4422
4423 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
4424
4425 /* Set does NOT take qualifiers */
4426 ASSERT(!set || (!params && !p_len));
4427
4428 if (!set && (len == sizeof(int)) &&
f024c48a 4429 !(IS_ALIGNED((unsigned long)(arg), (uint) sizeof(int)))) {
f4528696
JP
4430 WL_ERROR("wl%d: %s unaligned get ptr for %s\n",
4431 wlc->pub->unit, __func__, name);
a9533e7e
HP
4432 ASSERT(0);
4433 }
4434
4435 /* find the given iovar name */
4436 for (i = 0; i < WLC_MAXMODULES; i++) {
4437 if (!wlc->modulecb[i].iovars)
4438 continue;
ca8c1e59
JC
4439 vi = wlc_iovar_lookup(wlc->modulecb[i].iovars, name);
4440 if (vi)
a9533e7e
HP
4441 break;
4442 }
4443 /* iovar name not found */
4444 if (i >= WLC_MAXMODULES) {
4445 err = BCME_UNSUPPORTED;
a9533e7e
HP
4446 goto exit;
4447 }
4448
4449 /* set up 'params' pointer in case this is a set command so that
4450 * the convenience int and bool code can be common to set and get
4451 */
4452 if (params == NULL) {
4453 params = arg;
4454 p_len = len;
4455 }
4456
4457 if (vi->type == IOVT_VOID)
4458 val_size = 0;
4459 else if (vi->type == IOVT_BUFFER)
4460 val_size = len;
4461 else
4462 /* all other types are integer sized */
4463 val_size = sizeof(int);
4464
4465 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
4466
4467 /* Do the actual parameter implementation */
4468 err = wlc->modulecb[i].iovar_fn(wlc->modulecb[i].hdl, vi, actionid,
4469 name, params, p_len, arg, len, val_size,
4470 wlcif);
4471
4472 exit:
4473 return err;
4474}
4475
4476int
08db27dc 4477wlc_iovar_check(struct wlc_pub *pub, const bcm_iovar_t *vi, void *arg, int len,
a9533e7e
HP
4478 bool set)
4479{
c6a9e1fc 4480 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
a9533e7e 4481 int err = 0;
3e26416e 4482 s32 int_val = 0;
a9533e7e
HP
4483
4484 /* check generic condition flags */
4485 if (set) {
4486 if (((vi->flags & IOVF_SET_DOWN) && wlc->pub->up) ||
4487 ((vi->flags & IOVF_SET_UP) && !wlc->pub->up)) {
4488 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4489 } else if ((vi->flags & IOVF_SET_BAND)
4490 && IS_MBAND_UNLOCKED(wlc)) {
4491 err = BCME_NOTBANDLOCKED;
4492 } else if ((vi->flags & IOVF_SET_CLK) && !wlc->clk) {
4493 err = BCME_NOCLK;
4494 }
4495 } else {
4496 if (((vi->flags & IOVF_GET_DOWN) && wlc->pub->up) ||
4497 ((vi->flags & IOVF_GET_UP) && !wlc->pub->up)) {
4498 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4499 } else if ((vi->flags & IOVF_GET_BAND)
4500 && IS_MBAND_UNLOCKED(wlc)) {
4501 err = BCME_NOTBANDLOCKED;
4502 } else if ((vi->flags & IOVF_GET_CLK) && !wlc->clk) {
4503 err = BCME_NOCLK;
4504 }
4505 }
4506
4507 if (err)
4508 goto exit;
4509
4510 /* length check on io buf */
ca8c1e59
JC
4511 err = bcm_iovar_lencheck(vi, arg, len, set);
4512 if (err)
a9533e7e
HP
4513 goto exit;
4514
4515 /* On set, check value ranges for integer types */
4516 if (set) {
4517 switch (vi->type) {
4518 case IOVT_BOOL:
4519 case IOVT_INT8:
4520 case IOVT_INT16:
4521 case IOVT_INT32:
4522 case IOVT_UINT8:
4523 case IOVT_UINT16:
4524 case IOVT_UINT32:
4525 bcopy(arg, &int_val, sizeof(int));
4526 err = wlc_iovar_rangecheck(wlc, int_val, vi);
4527 break;
4528 }
4529 }
4530 exit:
4531 return err;
4532}
4533
4534/* handler for iovar table wlc_iovars */
4535/*
4536 * IMPLEMENTATION NOTE: In order to avoid checking for get/set in each
4537 * iovar case, the switch statement maps the iovar id into separate get
4538 * and set values. If you add a new iovar to the switch you MUST use
4539 * IOV_GVAL and/or IOV_SVAL in the case labels to avoid conflict with
4540 * another case.
4541 * Please use params for additional qualifying parameters.
4542 */
4543int
66cbd3ab 4544wlc_doiovar(void *hdl, const bcm_iovar_t *vi, u32 actionid,
a9533e7e
HP
4545 const char *name, void *params, uint p_len, void *arg, int len,
4546 int val_size, struct wlc_if *wlcif)
4547{
c6a9e1fc 4548 struct wlc_info *wlc = hdl;
a9533e7e
HP
4549 wlc_bsscfg_t *bsscfg;
4550 int err = 0;
3e26416e
GKH
4551 s32 int_val = 0;
4552 s32 int_val2 = 0;
4553 s32 *ret_int_ptr;
a9533e7e
HP
4554 bool bool_val;
4555 bool bool_val2;
4556 wlc_bss_info_t *current_bss;
4557
f4528696 4558 WL_TRACE("wl%d: %s\n", wlc->pub->unit, __func__);
a9533e7e
HP
4559
4560 bsscfg = NULL;
4561 current_bss = NULL;
4562
ca8c1e59
JC
4563 err = wlc_iovar_check(wlc->pub, vi, arg, len, IOV_ISSET(actionid));
4564 if (err != 0)
a9533e7e
HP
4565 return err;
4566
4567 /* convenience int and bool vals for first 8 bytes of buffer */
4568 if (p_len >= (int)sizeof(int_val))
4569 bcopy(params, &int_val, sizeof(int_val));
4570
4571 if (p_len >= (int)sizeof(int_val) * 2)
f024c48a 4572 bcopy((void *)((unsigned long)params + sizeof(int_val)), &int_val2,
a9533e7e
HP
4573 sizeof(int_val));
4574
4575 /* convenience int ptr for 4-byte gets (requires int aligned arg) */
3e26416e 4576 ret_int_ptr = (s32 *) arg;
a9533e7e 4577
0965ae88
GKH
4578 bool_val = (int_val != 0) ? true : false;
4579 bool_val2 = (int_val2 != 0) ? true : false;
a9533e7e 4580
f4528696
JP
4581 WL_TRACE("wl%d: %s: id %d\n",
4582 wlc->pub->unit, __func__, IOV_ID(actionid));
a9533e7e
HP
4583 /* Do the actual parameter implementation */
4584 switch (actionid) {
fd821d1e
AS
4585 case IOV_SVAL(IOV_RTSTHRESH):
4586 wlc->RTSThresh = int_val;
4587 break;
a9533e7e
HP
4588
4589 case IOV_GVAL(IOV_QTXPOWER):{
4590 uint qdbm;
4591 bool override;
4592
ca8c1e59
JC
4593 err = wlc_phy_txpower_get(wlc->band->pi, &qdbm,
4594 &override);
4595 if (err != BCME_OK)
a9533e7e
HP
4596 return err;
4597
4598 /* Return qdbm units */
4599 *ret_int_ptr =
4600 qdbm | (override ? WL_TXPWR_OVERRIDE : 0);
4601 break;
4602 }
4603
4604 /* As long as override is false, this only sets the *user* targets.
4605 User can twiddle this all he wants with no harm.
4606 wlc_phy_txpower_set() explicitly sets override to false if
4607 not internal or test.
4608 */
4609 case IOV_SVAL(IOV_QTXPOWER):{
41feb5ed 4610 u8 qdbm;
a9533e7e
HP
4611 bool override;
4612
4613 /* Remove override bit and clip to max qdbm value */
a300ce95 4614 qdbm = (u8)min_t(u32, (int_val & ~WL_TXPWR_OVERRIDE), 0xff);
a9533e7e 4615 /* Extract override setting */
0965ae88 4616 override = (int_val & WL_TXPWR_OVERRIDE) ? true : false;
a9533e7e
HP
4617 err =
4618 wlc_phy_txpower_set(wlc->band->pi, qdbm, override);
4619 break;
4620 }
4621
4622 case IOV_GVAL(IOV_MPC):
3e26416e 4623 *ret_int_ptr = (s32) wlc->mpc;
a9533e7e
HP
4624 break;
4625
4626 case IOV_SVAL(IOV_MPC):
4627 wlc->mpc = bool_val;
4628 wlc_radio_mpc_upd(wlc);
4629
4630 break;
4631
4632 case IOV_GVAL(IOV_BCN_LI_BCN):
4633 *ret_int_ptr = wlc->bcn_li_bcn;
4634 break;
4635
4636 case IOV_SVAL(IOV_BCN_LI_BCN):
41feb5ed 4637 wlc->bcn_li_bcn = (u8) int_val;
a9533e7e
HP
4638 if (wlc->pub->up)
4639 wlc_bcn_li_upd(wlc);
4640 break;
4641
4642 default:
f4528696 4643 WL_ERROR("wl%d: %s: unsupported\n", wlc->pub->unit, __func__);
a9533e7e
HP
4644 err = BCME_UNSUPPORTED;
4645 break;
4646 }
4647
4648 goto exit; /* avoid unused label warning */
4649
4650 exit:
4651 return err;
4652}
4653
4654static int
c6a9e1fc 4655wlc_iovar_rangecheck(struct wlc_info *wlc, u32 val, const bcm_iovar_t *vi)
a9533e7e
HP
4656{
4657 int err = 0;
66cbd3ab
GKH
4658 u32 min_val = 0;
4659 u32 max_val = 0;
a9533e7e
HP
4660
4661 /* Only ranged integers are checked */
4662 switch (vi->type) {
4663 case IOVT_INT32:
4664 max_val |= 0x7fffffff;
4665 /* fall through */
4666 case IOVT_INT16:
4667 max_val |= 0x00007fff;
4668 /* fall through */
4669 case IOVT_INT8:
4670 max_val |= 0x0000007f;
4671 min_val = ~max_val;
4672 if (vi->flags & IOVF_NTRL)
4673 min_val = 1;
4674 else if (vi->flags & IOVF_WHL)
4675 min_val = 0;
4676 /* Signed values are checked against max_val and min_val */
3e26416e
GKH
4677 if ((s32) val < (s32) min_val
4678 || (s32) val > (s32) max_val)
a9533e7e
HP
4679 err = BCME_RANGE;
4680 break;
4681
4682 case IOVT_UINT32:
4683 max_val |= 0xffffffff;
4684 /* fall through */
4685 case IOVT_UINT16:
4686 max_val |= 0x0000ffff;
4687 /* fall through */
4688 case IOVT_UINT8:
4689 max_val |= 0x000000ff;
4690 if (vi->flags & IOVF_NTRL)
4691 min_val = 1;
4692 if ((val < min_val) || (val > max_val))
4693 err = BCME_RANGE;
4694 break;
4695 }
4696
4697 return err;
4698}
4699
a9533e7e
HP
4700#ifdef BCMDBG
4701static const char *supr_reason[] = {
4702 "None", "PMQ Entry", "Flush request",
4703 "Previous frag failure", "Channel mismatch",
4704 "Lifetime Expiry", "Underflow"
4705};
4706
7d4df48e 4707static void wlc_print_txs_status(u16 s)
a9533e7e 4708{
0bef7748
AS
4709 printk(KERN_DEBUG "[15:12] %d frame attempts\n",
4710 (s & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT);
4711 printk(KERN_DEBUG " [11:8] %d rts attempts\n",
4712 (s & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT);
4713 printk(KERN_DEBUG " [7] %d PM mode indicated\n",
a9533e7e 4714 ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
0bef7748 4715 printk(KERN_DEBUG " [6] %d intermediate status\n",
a9533e7e 4716 ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
0bef7748
AS
4717 printk(KERN_DEBUG " [5] %d AMPDU\n",
4718 (s & TX_STATUS_AMPDU) ? 1 : 0);
4719 printk(KERN_DEBUG " [4:2] %d Frame Suppressed Reason (%s)\n",
a9533e7e
HP
4720 ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
4721 supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
0bef7748
AS
4722 printk(KERN_DEBUG " [1] %d acked\n",
4723 ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
a9533e7e
HP
4724}
4725#endif /* BCMDBG */
4726
7cc4a4c0 4727void wlc_print_txstatus(tx_status_t *txs)
a9533e7e
HP
4728{
4729#if defined(BCMDBG)
7d4df48e
GKH
4730 u16 s = txs->status;
4731 u16 ackphyrxsh = txs->ackphyrxsh;
a9533e7e 4732
0bef7748
AS
4733 printk(KERN_DEBUG "\ntxpkt (MPDU) Complete\n");
4734
4735 printk(KERN_DEBUG "FrameID: %04x ", txs->frameid);
4736 printk(KERN_DEBUG "TxStatus: %04x", s);
4737 printk(KERN_DEBUG "\n");
a9533e7e 4738
a9533e7e 4739 wlc_print_txs_status(s);
0bef7748
AS
4740
4741 printk(KERN_DEBUG "LastTxTime: %04x ", txs->lasttxtime);
4742 printk(KERN_DEBUG "Seq: %04x ", txs->sequence);
4743 printk(KERN_DEBUG "PHYTxStatus: %04x ", txs->phyerr);
4744 printk(KERN_DEBUG "RxAckRSSI: %04x ",
a9533e7e 4745 (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
0bef7748
AS
4746 printk(KERN_DEBUG "RxAckSQ: %04x",
4747 (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
4748 printk(KERN_DEBUG "\n");
a9533e7e
HP
4749#endif /* defined(BCMDBG) */
4750}
4751
e4cf544e
AS
4752static void
4753wlc_ctrupd_cache(u16 cur_stat, u16 *macstat_snapshot, u32 *macstat)
4754{
4755 u16 v;
4756 u16 delta;
4757
4758 v = ltoh16(cur_stat);
4759 delta = (u16)(v - *macstat_snapshot);
4760
4761 if (delta != 0) {
4762 *macstat += delta;
4763 *macstat_snapshot = v;
4764 }
4765}
4766
a9533e7e
HP
4767#define MACSTATUPD(name) \
4768 wlc_ctrupd_cache(macstats.name, &wlc->core->macstat_snapshot->name, &wlc->pub->_cnt->name)
4769
c6a9e1fc 4770void wlc_statsupd(struct wlc_info *wlc)
a9533e7e
HP
4771{
4772 int i;
e4cf544e 4773 macstat_t macstats;
a9533e7e 4774#ifdef BCMDBG
7d4df48e
GKH
4775 u16 delta;
4776 u16 rxf0ovfl;
4777 u16 txfunfl[NFIFO];
a9533e7e
HP
4778#endif /* BCMDBG */
4779
4780 /* if driver down, make no sense to update stats */
4781 if (!wlc->pub->up)
4782 return;
4783
4784#ifdef BCMDBG
4785 /* save last rx fifo 0 overflow count */
4786 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
4787
4788 /* save last tx fifo underflow count */
4789 for (i = 0; i < NFIFO; i++)
4790 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
4791#endif /* BCMDBG */
4792
e4cf544e
AS
4793 /* Read mac stats from contiguous shared memory */
4794 wlc_bmac_copyfrom_shm(wlc->hw, M_UCODE_MACSTAT,
4795 &macstats, sizeof(macstat_t));
4796
4797 /* update mac stats */
4798 MACSTATUPD(txallfrm);
4799 MACSTATUPD(txrtsfrm);
4800 MACSTATUPD(txctsfrm);
4801 MACSTATUPD(txackfrm);
4802 MACSTATUPD(txdnlfrm);
4803 MACSTATUPD(txbcnfrm);
4804 for (i = 0; i < NFIFO; i++)
4805 MACSTATUPD(txfunfl[i]);
4806 MACSTATUPD(txtplunfl);
4807 MACSTATUPD(txphyerr);
4808 MACSTATUPD(rxfrmtoolong);
4809 MACSTATUPD(rxfrmtooshrt);
4810 MACSTATUPD(rxinvmachdr);
4811 MACSTATUPD(rxbadfcs);
4812 MACSTATUPD(rxbadplcp);
4813 MACSTATUPD(rxcrsglitch);
4814 MACSTATUPD(rxstrt);
4815 MACSTATUPD(rxdfrmucastmbss);
4816 MACSTATUPD(rxmfrmucastmbss);
4817 MACSTATUPD(rxcfrmucast);
4818 MACSTATUPD(rxrtsucast);
4819 MACSTATUPD(rxctsucast);
4820 MACSTATUPD(rxackucast);
4821 MACSTATUPD(rxdfrmocast);
4822 MACSTATUPD(rxmfrmocast);
4823 MACSTATUPD(rxcfrmocast);
4824 MACSTATUPD(rxrtsocast);
4825 MACSTATUPD(rxctsocast);
4826 MACSTATUPD(rxdfrmmcast);
4827 MACSTATUPD(rxmfrmmcast);
4828 MACSTATUPD(rxcfrmmcast);
4829 MACSTATUPD(rxbeaconmbss);
4830 MACSTATUPD(rxdfrmucastobss);
4831 MACSTATUPD(rxbeaconobss);
4832 MACSTATUPD(rxrsptmout);
4833 MACSTATUPD(bcntxcancl);
4834 MACSTATUPD(rxf0ovfl);
4835 MACSTATUPD(rxf1ovfl);
4836 MACSTATUPD(rxf2ovfl);
4837 MACSTATUPD(txsfovfl);
4838 MACSTATUPD(pmqovfl);
4839 MACSTATUPD(rxcgprqfrm);
4840 MACSTATUPD(rxcgprsqovfl);
4841 MACSTATUPD(txcgprsfail);
4842 MACSTATUPD(txcgprssuc);
4843 MACSTATUPD(prs_timeout);
4844 MACSTATUPD(rxnack);
4845 MACSTATUPD(frmscons);
4846 MACSTATUPD(txnack);
4847 MACSTATUPD(txglitch_nack);
4848 MACSTATUPD(txburst);
4849 MACSTATUPD(phywatchdog);
4850 MACSTATUPD(pktengrxducast);
4851 MACSTATUPD(pktengrxdmcast);
4852
a9533e7e
HP
4853#ifdef BCMDBG
4854 /* check for rx fifo 0 overflow */
7d4df48e 4855 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
a9533e7e 4856 if (delta)
f4528696
JP
4857 WL_ERROR("wl%d: %u rx fifo 0 overflows!\n",
4858 wlc->pub->unit, delta);
a9533e7e
HP
4859
4860 /* check for tx fifo underflows */
4861 for (i = 0; i < NFIFO; i++) {
4862 delta =
7d4df48e 4863 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
a9533e7e
HP
4864 txfunfl[i]);
4865 if (delta)
f4528696
JP
4866 WL_ERROR("wl%d: %u tx fifo %d underflows!\n",
4867 wlc->pub->unit, delta, i);
a9533e7e
HP
4868 }
4869#endif /* BCMDBG */
4870
4871 /* dot11 counter update */
4872
4873 WLCNTSET(wlc->pub->_cnt->txrts,
4874 (wlc->pub->_cnt->rxctsucast -
4875 wlc->pub->_cnt->d11cnt_txrts_off));
4876 WLCNTSET(wlc->pub->_cnt->rxcrc,
4877 (wlc->pub->_cnt->rxbadfcs - wlc->pub->_cnt->d11cnt_rxcrc_off));
4878 WLCNTSET(wlc->pub->_cnt->txnocts,
4879 ((wlc->pub->_cnt->txrtsfrm - wlc->pub->_cnt->rxctsucast) -
4880 wlc->pub->_cnt->d11cnt_txnocts_off));
4881
4882 /* merge counters from dma module */
4883 for (i = 0; i < NFIFO; i++) {
4884 if (wlc->hw->di[i]) {
4885 WLCNTADD(wlc->pub->_cnt->txnobuf,
4886 (wlc->hw->di[i])->txnobuf);
4887 WLCNTADD(wlc->pub->_cnt->rxnobuf,
4888 (wlc->hw->di[i])->rxnobuf);
4889 WLCNTADD(wlc->pub->_cnt->rxgiant,
4890 (wlc->hw->di[i])->rxgiants);
4891 dma_counterreset(wlc->hw->di[i]);
4892 }
4893 }
4894
4895 /*
4896 * Aggregate transmit and receive errors that probably resulted
4897 * in the loss of a frame are computed on the fly.
4898 */
4899 WLCNTSET(wlc->pub->_cnt->txerror,
4900 wlc->pub->_cnt->txnobuf + wlc->pub->_cnt->txnoassoc +
4901 wlc->pub->_cnt->txuflo + wlc->pub->_cnt->txrunt +
4902 wlc->pub->_cnt->dmade + wlc->pub->_cnt->dmada +
4903 wlc->pub->_cnt->dmape);
4904 WLCNTSET(wlc->pub->_cnt->rxerror,
4905 wlc->pub->_cnt->rxoflo + wlc->pub->_cnt->rxnobuf +
4906 wlc->pub->_cnt->rxfragerr + wlc->pub->_cnt->rxrunt +
4907 wlc->pub->_cnt->rxgiant + wlc->pub->_cnt->rxnoscb +
4908 wlc->pub->_cnt->rxbadsrcmac);
4909 for (i = 0; i < NFIFO; i++)
e4cf544e 4910 wlc->pub->_cnt->rxerror += wlc->pub->_cnt->rxuflo[i];
a9533e7e
HP
4911}
4912
7d4df48e 4913bool wlc_chipmatch(u16 vendor, u16 device)
a9533e7e
HP
4914{
4915 if (vendor != VENDOR_BROADCOM) {
f4528696 4916 WL_ERROR("wlc_chipmatch: unknown vendor id %04x\n", vendor);
0965ae88 4917 return false;
a9533e7e
HP
4918 }
4919
4920 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
0f0881b0 4921 return true;
a9533e7e
HP
4922
4923 if (device == BCM4313_D11N2G_ID)
0f0881b0 4924 return true;
a9533e7e 4925 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
0f0881b0 4926 return true;
a9533e7e 4927
f4528696 4928 WL_ERROR("wlc_chipmatch: unknown device id %04x\n", device);
0965ae88 4929 return false;
a9533e7e
HP
4930}
4931
a9533e7e 4932#if defined(BCMDBG)
7cc4a4c0 4933void wlc_print_txdesc(d11txh_t *txh)
a9533e7e 4934{
7d4df48e
GKH
4935 u16 mtcl = ltoh16(txh->MacTxControlLow);
4936 u16 mtch = ltoh16(txh->MacTxControlHigh);
4937 u16 mfc = ltoh16(txh->MacFrameControl);
4938 u16 tfest = ltoh16(txh->TxFesTimeNormal);
4939 u16 ptcw = ltoh16(txh->PhyTxControlWord);
4940 u16 ptcw_1 = ltoh16(txh->PhyTxControlWord_1);
4941 u16 ptcw_1_Fbr = ltoh16(txh->PhyTxControlWord_1_Fbr);
4942 u16 ptcw_1_Rts = ltoh16(txh->PhyTxControlWord_1_Rts);
4943 u16 ptcw_1_FbrRts = ltoh16(txh->PhyTxControlWord_1_FbrRts);
4944 u16 mainrates = ltoh16(txh->MainRates);
4945 u16 xtraft = ltoh16(txh->XtraFrameTypes);
41feb5ed
GKH
4946 u8 *iv = txh->IV;
4947 u8 *ra = txh->TxFrameRA;
7d4df48e 4948 u16 tfestfb = ltoh16(txh->TxFesTimeFallback);
41feb5ed 4949 u8 *rtspfb = txh->RTSPLCPFallback;
7d4df48e 4950 u16 rtsdfb = ltoh16(txh->RTSDurFallback);
41feb5ed 4951 u8 *fragpfb = txh->FragPLCPFallback;
7d4df48e
GKH
4952 u16 fragdfb = ltoh16(txh->FragDurFallback);
4953 u16 mmodelen = ltoh16(txh->MModeLen);
4954 u16 mmodefbrlen = ltoh16(txh->MModeFbrLen);
4955 u16 tfid = ltoh16(txh->TxFrameID);
4956 u16 txs = ltoh16(txh->TxStatus);
4957 u16 mnmpdu = ltoh16(txh->MaxNMpdus);
4958 u16 mabyte = ltoh16(txh->MaxABytes_MRT);
4959 u16 mabyte_f = ltoh16(txh->MaxABytes_FBR);
4960 u16 mmbyte = ltoh16(txh->MinMBytes);
a9533e7e 4961
41feb5ed 4962 u8 *rtsph = txh->RTSPhyHeader;
f3dc3ea4 4963 struct ieee80211_rts rts = txh->rts_frame;
a9533e7e
HP
4964 char hexbuf[256];
4965
4966 /* add plcp header along with txh descriptor */
580a0bd9 4967 prhex("Raw TxDesc + plcp header", (unsigned char *) txh, sizeof(d11txh_t) + 48);
a9533e7e 4968
0bef7748
AS
4969 printk(KERN_DEBUG "TxCtlLow: %04x ", mtcl);
4970 printk(KERN_DEBUG "TxCtlHigh: %04x ", mtch);
4971 printk(KERN_DEBUG "FC: %04x ", mfc);
4972 printk(KERN_DEBUG "FES Time: %04x\n", tfest);
4973 printk(KERN_DEBUG "PhyCtl: %04x%s ", ptcw,
a9533e7e 4974 (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
0bef7748
AS
4975 printk(KERN_DEBUG "PhyCtl_1: %04x ", ptcw_1);
4976 printk(KERN_DEBUG "PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
4977 printk(KERN_DEBUG "PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
4978 printk(KERN_DEBUG "PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
4979 printk(KERN_DEBUG "MainRates: %04x ", mainrates);
4980 printk(KERN_DEBUG "XtraFrameTypes: %04x ", xtraft);
4981 printk(KERN_DEBUG "\n");
a9533e7e
HP
4982
4983 bcm_format_hex(hexbuf, iv, sizeof(txh->IV));
0bef7748 4984 printk(KERN_DEBUG "SecIV: %s\n", hexbuf);
a9533e7e 4985 bcm_format_hex(hexbuf, ra, sizeof(txh->TxFrameRA));
0bef7748 4986 printk(KERN_DEBUG "RA: %s\n", hexbuf);
a9533e7e 4987
0bef7748 4988 printk(KERN_DEBUG "Fb FES Time: %04x ", tfestfb);
a9533e7e 4989 bcm_format_hex(hexbuf, rtspfb, sizeof(txh->RTSPLCPFallback));
0bef7748
AS
4990 printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
4991 printk(KERN_DEBUG "RTS DUR: %04x ", rtsdfb);
a9533e7e 4992 bcm_format_hex(hexbuf, fragpfb, sizeof(txh->FragPLCPFallback));
0bef7748
AS
4993 printk(KERN_DEBUG "PLCP: %s ", hexbuf);
4994 printk(KERN_DEBUG "DUR: %04x", fragdfb);
4995 printk(KERN_DEBUG "\n");
a9533e7e 4996
0bef7748
AS
4997 printk(KERN_DEBUG "MModeLen: %04x ", mmodelen);
4998 printk(KERN_DEBUG "MModeFbrLen: %04x\n", mmodefbrlen);
a9533e7e 4999
0bef7748
AS
5000 printk(KERN_DEBUG "FrameID: %04x\n", tfid);
5001 printk(KERN_DEBUG "TxStatus: %04x\n", txs);
a9533e7e 5002
0bef7748
AS
5003 printk(KERN_DEBUG "MaxNumMpdu: %04x\n", mnmpdu);
5004 printk(KERN_DEBUG "MaxAggbyte: %04x\n", mabyte);
5005 printk(KERN_DEBUG "MaxAggbyte_fb: %04x\n", mabyte_f);
5006 printk(KERN_DEBUG "MinByte: %04x\n", mmbyte);
a9533e7e
HP
5007
5008 bcm_format_hex(hexbuf, rtsph, sizeof(txh->RTSPhyHeader));
0bef7748 5009 printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
41feb5ed 5010 bcm_format_hex(hexbuf, (u8 *) &rts, sizeof(txh->rts_frame));
0bef7748
AS
5011 printk(KERN_DEBUG "RTS Frame: %s", hexbuf);
5012 printk(KERN_DEBUG "\n");
a9533e7e
HP
5013}
5014#endif /* defined(BCMDBG) */
5015
5016#if defined(BCMDBG)
7cc4a4c0 5017void wlc_print_rxh(d11rxhdr_t *rxh)
a9533e7e 5018{
7d4df48e
GKH
5019 u16 len = rxh->RxFrameSize;
5020 u16 phystatus_0 = rxh->PhyRxStatus_0;
5021 u16 phystatus_1 = rxh->PhyRxStatus_1;
5022 u16 phystatus_2 = rxh->PhyRxStatus_2;
5023 u16 phystatus_3 = rxh->PhyRxStatus_3;
5024 u16 macstatus1 = rxh->RxStatus1;
5025 u16 macstatus2 = rxh->RxStatus2;
a9533e7e
HP
5026 char flagstr[64];
5027 char lenbuf[20];
5028 static const bcm_bit_desc_t macstat_flags[] = {
5029 {RXS_FCSERR, "FCSErr"},
5030 {RXS_RESPFRAMETX, "Reply"},
5031 {RXS_PBPRES, "PADDING"},
5032 {RXS_DECATMPT, "DeCr"},
5033 {RXS_DECERR, "DeCrErr"},
5034 {RXS_BCNSENT, "Bcn"},
5035 {0, NULL}
5036 };
5037
580a0bd9 5038 prhex("Raw RxDesc", (unsigned char *) rxh, sizeof(d11rxhdr_t));
a9533e7e
HP
5039
5040 bcm_format_flags(macstat_flags, macstatus1, flagstr, 64);
5041
5042 snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
5043
0bef7748 5044 printk(KERN_DEBUG "RxFrameSize: %6s (%d)%s\n", lenbuf, len,
a9533e7e 5045 (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
0bef7748 5046 printk(KERN_DEBUG "RxPHYStatus: %04x %04x %04x %04x\n",
a9533e7e 5047 phystatus_0, phystatus_1, phystatus_2, phystatus_3);
0bef7748
AS
5048 printk(KERN_DEBUG "RxMACStatus: %x %s\n", macstatus1, flagstr);
5049 printk(KERN_DEBUG "RXMACaggtype: %x\n",
5050 (macstatus2 & RXS_AGGTYPE_MASK));
5051 printk(KERN_DEBUG "RxTSFTime: %04x\n", rxh->RxTSFTime);
a9533e7e 5052}
a9533e7e
HP
5053#endif /* defined(BCMDBG) */
5054
5055#if defined(BCMDBG)
580a0bd9 5056int wlc_format_ssid(char *buf, const unsigned char ssid[], uint ssid_len)
a9533e7e
HP
5057{
5058 uint i, c;
5059 char *p = buf;
5060 char *endp = buf + SSID_FMT_BUF_LEN;
5061
3726ed4d
RV
5062 if (ssid_len > IEEE80211_MAX_SSID_LEN)
5063 ssid_len = IEEE80211_MAX_SSID_LEN;
a9533e7e
HP
5064
5065 for (i = 0; i < ssid_len; i++) {
5066 c = (uint) ssid[i];
5067 if (c == '\\') {
5068 *p++ = '\\';
5069 *p++ = '\\';
580a0bd9 5070 } else if (isprint((unsigned char) c)) {
a9533e7e
HP
5071 *p++ = (char)c;
5072 } else {
5073 p += snprintf(p, (endp - p), "\\x%02X", c);
5074 }
5075 }
5076 *p = '\0';
5077 ASSERT(p < endp);
5078
5079 return (int)(p - buf);
5080}
5081#endif /* defined(BCMDBG) */
5082
c6a9e1fc 5083u16 wlc_rate_shm_offset(struct wlc_info *wlc, u8 rate)
a9533e7e 5084{
90ea2296 5085 return wlc_bmac_rate_shm_offset(wlc->hw, rate);
a9533e7e
HP
5086}
5087
5088/* Callback for device removed */
a9533e7e
HP
5089
5090/*
5091 * Attempts to queue a packet onto a multiple-precedence queue,
5092 * if necessary evicting a lower precedence packet from the queue.
5093 *
5094 * 'prec' is the precedence number that has already been mapped
5095 * from the packet priority.
5096 *
0965ae88 5097 * Returns true if packet consumed (queued), false if not.
a9533e7e
HP
5098 */
5099bool BCMFASTPATH
c6a9e1fc 5100wlc_prec_enq(struct wlc_info *wlc, struct pktq *q, void *pkt, int prec)
a9533e7e 5101{
0965ae88 5102 return wlc_prec_enq_head(wlc, q, pkt, prec, false);
a9533e7e
HP
5103}
5104
5105bool BCMFASTPATH
c6a9e1fc 5106wlc_prec_enq_head(struct wlc_info *wlc, struct pktq *q, struct sk_buff *pkt,
c26b1378 5107 int prec, bool head)
a9533e7e 5108{
c26b1378 5109 struct sk_buff *p;
a9533e7e
HP
5110 int eprec = -1; /* precedence to evict from */
5111
5112 /* Determine precedence from which to evict packet, if any */
5113 if (pktq_pfull(q, prec))
5114 eprec = prec;
5115 else if (pktq_full(q)) {
5116 p = pktq_peek_tail(q, &eprec);
5117 ASSERT(p != NULL);
5118 if (eprec > prec) {
f4528696
JP
5119 WL_ERROR("%s: Failing: eprec %d > prec %d\n",
5120 __func__, eprec, prec);
0965ae88 5121 return false;
a9533e7e
HP
5122 }
5123 }
5124
5125 /* Evict if needed */
5126 if (eprec >= 0) {
5127 bool discard_oldest;
5128
5129 /* Detect queueing to unconfigured precedence */
5130 ASSERT(!pktq_pempty(q, eprec));
5131
5132 discard_oldest = AC_BITMAP_TST(wlc->wme_dp, eprec);
5133
5134 /* Refuse newer packet unless configured to discard oldest */
5135 if (eprec == prec && !discard_oldest) {
f4528696
JP
5136 WL_ERROR("%s: No where to go, prec == %d\n",
5137 __func__, prec);
0965ae88 5138 return false;
a9533e7e
HP
5139 }
5140
5141 /* Evict packet according to discard policy */
5142 p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q,
5143 eprec);
5144 ASSERT(p != NULL);
5145
5146 /* Increment wme stats */
5147 if (WME_ENAB(wlc->pub)) {
5148 WLCNTINCR(wlc->pub->_wme_cnt->
54991ad6 5149 tx_failed[WME_PRIO2AC(p->priority)].packets);
a9533e7e 5150 WLCNTADD(wlc->pub->_wme_cnt->
54991ad6 5151 tx_failed[WME_PRIO2AC(p->priority)].bytes,
a9533e7e
HP
5152 pkttotlen(wlc->osh, p));
5153 }
5154
5155 ASSERT(0);
85385764 5156 pkt_buf_free_skb(wlc->osh, p, true);
e4cf544e 5157 wlc->pub->_cnt->txnobuf++;
a9533e7e
HP
5158 }
5159
5160 /* Enqueue */
5161 if (head)
5162 p = pktq_penq_head(q, prec, pkt);
5163 else
5164 p = pktq_penq(q, prec, pkt);
5165 ASSERT(p != NULL);
5166
0f0881b0 5167 return true;
a9533e7e
HP
5168}
5169
c26b1378
AS
5170void BCMFASTPATH wlc_txq_enq(void *ctx, struct scb *scb, struct sk_buff *sdu,
5171 uint prec)
a9533e7e 5172{
c6a9e1fc 5173 struct wlc_info *wlc = (struct wlc_info *) ctx;
a9533e7e
HP
5174 wlc_txq_info_t *qi = wlc->active_queue; /* Check me */
5175 struct pktq *q = &qi->q;
5176 int prio;
5177
54991ad6 5178 prio = sdu->priority;
a9533e7e
HP
5179
5180 ASSERT(pktq_max(q) >= wlc->pub->tunables->datahiwat);
5181
5182 if (!wlc_prec_enq(wlc, q, sdu, prec)) {
5183 if (!EDCF_ENAB(wlc->pub)
5184 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL))
f4528696
JP
5185 WL_ERROR("wl%d: wlc_txq_enq: txq overflow\n",
5186 wlc->pub->unit);
a9533e7e
HP
5187
5188 /* ASSERT(9 == 8); *//* XXX we might hit this condtion in case packet flooding from mac80211 stack */
85385764 5189 pkt_buf_free_skb(wlc->osh, sdu, true);
e4cf544e 5190 wlc->pub->_cnt->txnobuf++;
a9533e7e
HP
5191 }
5192
5193 /* Check if flow control needs to be turned on after enqueuing the packet
5194 * Don't turn on flow control if EDCF is enabled. Driver would make the decision on what
5195 * to drop instead of relying on stack to make the right decision
5196 */
5197 if (!EDCF_ENAB(wlc->pub)
5198 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5199 if (pktq_len(q) >= wlc->pub->tunables->datahiwat) {
5200 wlc_txflowcontrol(wlc, qi, ON, ALLPRIO);
5201 }
5202 } else if (wlc->pub->_priofc) {
5203 if (pktq_plen(q, wlc_prio2prec_map[prio]) >=
5204 wlc->pub->tunables->datahiwat) {
5205 wlc_txflowcontrol(wlc, qi, ON, prio);
5206 }
5207 }
5208}
5209
5210bool BCMFASTPATH
c6a9e1fc 5211wlc_sendpkt_mac80211(struct wlc_info *wlc, struct sk_buff *sdu,
c26b1378 5212 struct ieee80211_hw *hw)
a9533e7e 5213{
41feb5ed 5214 u8 prio;
a9533e7e
HP
5215 uint fifo;
5216 void *pkt;
5217 struct scb *scb = &global_scb;
3e9796f9 5218 struct ieee80211_hdr *d11_header = (struct ieee80211_hdr *)(sdu->data);
7d4df48e 5219 u16 type, fc;
a9533e7e
HP
5220
5221 ASSERT(sdu);
5222
3e9796f9 5223 fc = ltoh16(d11_header->frame_control);
d83b2a8a 5224 type = (fc & IEEE80211_FCTL_FTYPE);
a9533e7e
HP
5225
5226 /* 802.11 standard requires management traffic to go at highest priority */
d83b2a8a 5227 prio = (type == IEEE80211_FTYPE_DATA ? sdu->priority : MAXPRIO);
a9533e7e
HP
5228 fifo = prio2fifo[prio];
5229
3be727c8 5230 ASSERT((uint) skb_headroom(sdu) >= TXOFF);
54991ad6
AS
5231 ASSERT(!(sdu->next));
5232 ASSERT(!(sdu->prev));
a9533e7e
HP
5233 ASSERT(fifo < NFIFO);
5234
5235 pkt = sdu;
5236 if (unlikely
5237 (wlc_d11hdrs_mac80211(wlc, hw, pkt, scb, 0, 1, fifo, 0, NULL, 0)))
5238 return -EINVAL;
5239 wlc_txq_enq(wlc, scb, pkt, WLC_PRIO_TO_PREC(prio));
5240 wlc_send_q(wlc, wlc->active_queue);
5241
e4cf544e 5242 wlc->pub->_cnt->ieee_tx++;
a9533e7e
HP
5243 return 0;
5244}
5245
c6a9e1fc 5246void BCMFASTPATH wlc_send_q(struct wlc_info *wlc, wlc_txq_info_t *qi)
a9533e7e 5247{
c26b1378 5248 struct sk_buff *pkt[DOT11_MAXNUMFRAGS];
a9533e7e 5249 int prec;
7d4df48e 5250 u16 prec_map;
a9533e7e
HP
5251 int err = 0, i, count;
5252 uint fifo;
5253 struct pktq *q = &qi->q;
5254 struct ieee80211_tx_info *tx_info;
5255
5256 /* only do work for the active queue */
5257 if (qi != wlc->active_queue)
5258 return;
5259
5260 if (in_send_q)
5261 return;
5262 else
0f0881b0 5263 in_send_q = true;
a9533e7e
HP
5264
5265 prec_map = wlc->tx_prec_map;
5266
5267 /* Send all the enq'd pkts that we can.
5268 * Dequeue packets with precedence with empty HW fifo only
5269 */
5270 while (prec_map && (pkt[0] = pktq_mdeq(q, prec_map, &prec))) {
5271 tx_info = IEEE80211_SKB_CB(pkt[0]);
5272 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
5273 err = wlc_sendampdu(wlc->ampdu, qi, pkt, prec);
5274 } else {
5275 count = 1;
5276 err = wlc_prep_pdu(wlc, pkt[0], &fifo);
5277 if (!err) {
5278 for (i = 0; i < count; i++) {
0f0881b0 5279 wlc_txfifo(wlc, fifo, pkt[i], true, 1);
a9533e7e
HP
5280 }
5281 }
5282 }
5283
5284 if (err == BCME_BUSY) {
5285 pktq_penq_head(q, prec, pkt[0]);
5286 /* If send failed due to any other reason than a change in
5287 * HW FIFO condition, quit. Otherwise, read the new prec_map!
5288 */
5289 if (prec_map == wlc->tx_prec_map)
5290 break;
5291 prec_map = wlc->tx_prec_map;
5292 }
5293 }
5294
5295 /* Check if flow control needs to be turned off after sending the packet */
5296 if (!EDCF_ENAB(wlc->pub)
5297 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5298 if (wlc_txflowcontrol_prio_isset(wlc, qi, ALLPRIO)
5299 && (pktq_len(q) < wlc->pub->tunables->datahiwat / 2)) {
5300 wlc_txflowcontrol(wlc, qi, OFF, ALLPRIO);
5301 }
5302 } else if (wlc->pub->_priofc) {
5303 int prio;
5304 for (prio = MAXPRIO; prio >= 0; prio--) {
5305 if (wlc_txflowcontrol_prio_isset(wlc, qi, prio) &&
5306 (pktq_plen(q, wlc_prio2prec_map[prio]) <
5307 wlc->pub->tunables->datahiwat / 2)) {
5308 wlc_txflowcontrol(wlc, qi, OFF, prio);
5309 }
5310 }
5311 }
0965ae88 5312 in_send_q = false;
a9533e7e
HP
5313}
5314
5315/*
5316 * bcmc_fid_generate:
5317 * Generate frame ID for a BCMC packet. The frag field is not used
5318 * for MC frames so is used as part of the sequence number.
5319 */
7d4df48e 5320static inline u16
c6a9e1fc 5321bcmc_fid_generate(struct wlc_info *wlc, wlc_bsscfg_t *bsscfg, d11txh_t *txh)
a9533e7e 5322{
7d4df48e 5323 u16 frameid;
a9533e7e
HP
5324
5325 frameid = ltoh16(txh->TxFrameID) & ~(TXFID_SEQ_MASK | TXFID_QUEUE_MASK);
5326 frameid |=
5327 (((wlc->
5328 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5329 TX_BCMC_FIFO;
5330
5331 return frameid;
5332}
5333
5334void BCMFASTPATH
c6a9e1fc 5335wlc_txfifo(struct wlc_info *wlc, uint fifo, struct sk_buff *p, bool commit,
c26b1378 5336 s8 txpktpend)
a9533e7e 5337{
7d4df48e 5338 u16 frameid = INVALIDFID;
a9533e7e
HP
5339 d11txh_t *txh;
5340
5341 ASSERT(fifo < NFIFO);
54991ad6 5342 txh = (d11txh_t *) (p->data);
a9533e7e
HP
5343
5344 /* When a BC/MC frame is being committed to the BCMC fifo via DMA (NOT PIO), update
5345 * ucode or BSS info as appropriate.
5346 */
5347 if (fifo == TX_BCMC_FIFO) {
5348 frameid = ltoh16(txh->TxFrameID);
5349
5350 }
5351
5352 if (WLC_WAR16165(wlc))
0f0881b0 5353 wlc_war16165(wlc, true);
a9533e7e 5354
a9533e7e
HP
5355
5356 /* Bump up pending count for if not using rpc. If rpc is used, this will be handled
5357 * in wlc_bmac_txfifo()
5358 */
5359 if (commit) {
5360 TXPKTPENDINC(wlc, fifo, txpktpend);
f4528696
JP
5361 WL_TRACE("wlc_txfifo, pktpend inc %d to %d\n",
5362 txpktpend, TXPKTPENDGET(wlc, fifo));
a9533e7e
HP
5363 }
5364
5365 /* Commit BCMC sequence number in the SHM frame ID location */
5366 if (frameid != INVALIDFID)
5367 BCMCFID(wlc, frameid);
5368
5369 if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0) {
f4528696 5370 WL_ERROR("wlc_txfifo: fatal, toss frames !!!\n");
a9533e7e 5371 }
a9533e7e
HP
5372}
5373
7d4df48e 5374static u16
c6a9e1fc 5375wlc_compute_airtime(struct wlc_info *wlc, ratespec_t rspec, uint length)
a9533e7e 5376{
7d4df48e 5377 u16 usec = 0;
a9533e7e
HP
5378 uint mac_rate = RSPEC2RATE(rspec);
5379 uint nsyms;
5380
5381 if (IS_MCS(rspec)) {
5382 /* not supported yet */
5383 ASSERT(0);
5384 } else if (IS_OFDM(rspec)) {
5385 /* nsyms = Ceiling(Nbits / (Nbits/sym))
5386 *
5387 * Nbits = length * 8
5388 * Nbits/sym = Mbps * 4 = mac_rate * 2
5389 */
5390 nsyms = CEIL((length * 8), (mac_rate * 2));
5391
5392 /* usec = symbols * usec/symbol */
7d4df48e 5393 usec = (u16) (nsyms * APHY_SYMBOL_TIME);
90ea2296 5394 return usec;
a9533e7e
HP
5395 } else {
5396 switch (mac_rate) {
5397 case WLC_RATE_1M:
5398 usec = length << 3;
5399 break;
5400 case WLC_RATE_2M:
5401 usec = length << 2;
5402 break;
5403 case WLC_RATE_5M5:
5404 usec = (length << 4) / 11;
5405 break;
5406 case WLC_RATE_11M:
5407 usec = (length << 3) / 11;
5408 break;
5409 default:
f4528696
JP
5410 WL_ERROR("wl%d: wlc_compute_airtime: unsupported rspec 0x%x\n",
5411 wlc->pub->unit, rspec);
a9533e7e
HP
5412 ASSERT((const char *)"Bad phy_rate" == NULL);
5413 break;
5414 }
5415 }
5416
90ea2296 5417 return usec;
a9533e7e
HP
5418}
5419
5420void BCMFASTPATH
c6a9e1fc 5421wlc_compute_plcp(struct wlc_info *wlc, ratespec_t rspec, uint length, u8 *plcp)
a9533e7e
HP
5422{
5423 if (IS_MCS(rspec)) {
5424 wlc_compute_mimo_plcp(rspec, length, plcp);
5425 } else if (IS_OFDM(rspec)) {
5426 wlc_compute_ofdm_plcp(rspec, length, plcp);
5427 } else {
5428 wlc_compute_cck_plcp(rspec, length, plcp);
5429 }
5430 return;
5431}
5432
5433/* Rate: 802.11 rate code, length: PSDU length in octets */
41feb5ed 5434static void wlc_compute_mimo_plcp(ratespec_t rspec, uint length, u8 *plcp)
a9533e7e 5435{
41feb5ed 5436 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
a9533e7e
HP
5437 ASSERT(IS_MCS(rspec));
5438 plcp[0] = mcs;
5439 if (RSPEC_IS40MHZ(rspec) || (mcs == 32))
5440 plcp[0] |= MIMO_PLCP_40MHZ;
5441 WLC_SET_MIMO_PLCP_LEN(plcp, length);
5442 plcp[3] = RSPEC_MIMOPLCP3(rspec); /* rspec already holds this byte */
5443 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
5444 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
5445 plcp[5] = 0;
5446}
5447
5448/* Rate: 802.11 rate code, length: PSDU length in octets */
5449static void BCMFASTPATH
66cbd3ab 5450wlc_compute_ofdm_plcp(ratespec_t rspec, u32 length, u8 *plcp)
a9533e7e 5451{
41feb5ed 5452 u8 rate_signal;
66cbd3ab 5453 u32 tmp = 0;
a9533e7e
HP
5454 int rate = RSPEC2RATE(rspec);
5455
5456 ASSERT(IS_OFDM(rspec));
5457
5458 /* encode rate per 802.11a-1999 sec 17.3.4.1, with lsb transmitted first */
5459 rate_signal = rate_info[rate] & RATE_MASK;
5460 ASSERT(rate_signal != 0);
5461
9249ede9 5462 memset(plcp, 0, D11_PHY_HDR_LEN);
a9533e7e
HP
5463 D11A_PHY_HDR_SRATE((ofdm_phy_hdr_t *) plcp, rate_signal);
5464
5465 tmp = (length & 0xfff) << 5;
5466 plcp[2] |= (tmp >> 16) & 0xff;
5467 plcp[1] |= (tmp >> 8) & 0xff;
5468 plcp[0] |= tmp & 0xff;
5469
5470 return;
5471}
5472
5473/*
5474 * Compute PLCP, but only requires actual rate and length of pkt.
5475 * Rate is given in the driver standard multiple of 500 kbps.
5476 * le is set for 11 Mbps rate if necessary.
5477 * Broken out for PRQ.
5478 */
5479
41feb5ed 5480static void wlc_cck_plcp_set(int rate_500, uint length, u8 *plcp)
a9533e7e 5481{
7d4df48e 5482 u16 usec = 0;
41feb5ed 5483 u8 le = 0;
a9533e7e
HP
5484
5485 switch (rate_500) {
5486 case WLC_RATE_1M:
5487 usec = length << 3;
5488 break;
5489 case WLC_RATE_2M:
5490 usec = length << 2;
5491 break;
5492 case WLC_RATE_5M5:
5493 usec = (length << 4) / 11;
5494 if ((length << 4) - (usec * 11) > 0)
5495 usec++;
5496 break;
5497 case WLC_RATE_11M:
5498 usec = (length << 3) / 11;
5499 if ((length << 3) - (usec * 11) > 0) {
5500 usec++;
5501 if ((usec * 11) - (length << 3) >= 8)
5502 le = D11B_PLCP_SIGNAL_LE;
5503 }
5504 break;
5505
5506 default:
f4528696 5507 WL_ERROR("wlc_cck_plcp_set: unsupported rate %d\n", rate_500);
a9533e7e
HP
5508 rate_500 = WLC_RATE_1M;
5509 usec = length << 3;
5510 break;
5511 }
5512 /* PLCP signal byte */
5513 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
5514 /* PLCP service byte */
41feb5ed 5515 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
7d4df48e 5516 /* PLCP length u16, little endian */
a9533e7e
HP
5517 plcp[2] = usec & 0xff;
5518 plcp[3] = (usec >> 8) & 0xff;
5519 /* PLCP CRC16 */
5520 plcp[4] = 0;
5521 plcp[5] = 0;
5522}
5523
5524/* Rate: 802.11 rate code, length: PSDU length in octets */
41feb5ed 5525static void wlc_compute_cck_plcp(ratespec_t rspec, uint length, u8 *plcp)
a9533e7e
HP
5526{
5527 int rate = RSPEC2RATE(rspec);
5528
5529 ASSERT(IS_CCK(rspec));
5530
5531 wlc_cck_plcp_set(rate, length, plcp);
5532}
5533
5534/* wlc_compute_frame_dur()
5535 *
5536 * Calculate the 802.11 MAC header DUR field for MPDU
5537 * DUR for a single frame = 1 SIFS + 1 ACK
5538 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5539 *
5540 * rate MPDU rate in unit of 500kbps
5541 * next_frag_len next MPDU length in bytes
5542 * preamble_type use short/GF or long/MM PLCP header
5543 */
7d4df48e 5544static u16 BCMFASTPATH
c6a9e1fc 5545wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate, u8 preamble_type,
a9533e7e
HP
5546 uint next_frag_len)
5547{
7d4df48e 5548 u16 dur, sifs;
a9533e7e
HP
5549
5550 sifs = SIFS(wlc->band);
5551
5552 dur = sifs;
7d4df48e 5553 dur += (u16) wlc_calc_ack_time(wlc, rate, preamble_type);
a9533e7e
HP
5554
5555 if (next_frag_len) {
5556 /* Double the current DUR to get 2 SIFS + 2 ACKs */
5557 dur *= 2;
5558 /* add another SIFS and the frag time */
5559 dur += sifs;
5560 dur +=
7d4df48e 5561 (u16) wlc_calc_frame_time(wlc, rate, preamble_type,
a9533e7e
HP
5562 next_frag_len);
5563 }
90ea2296 5564 return dur;
a9533e7e
HP
5565}
5566
5567/* wlc_compute_rtscts_dur()
5568 *
5569 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
5570 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
5571 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
5572 *
5573 * cts cts-to-self or rts/cts
5574 * rts_rate rts or cts rate in unit of 500kbps
5575 * rate next MPDU rate in unit of 500kbps
5576 * frame_len next MPDU frame length in bytes
5577 */
7d4df48e 5578u16 BCMFASTPATH
c6a9e1fc 5579wlc_compute_rtscts_dur(struct wlc_info *wlc, bool cts_only, ratespec_t rts_rate,
41feb5ed
GKH
5580 ratespec_t frame_rate, u8 rts_preamble_type,
5581 u8 frame_preamble_type, uint frame_len, bool ba)
a9533e7e 5582{
7d4df48e 5583 u16 dur, sifs;
a9533e7e
HP
5584
5585 sifs = SIFS(wlc->band);
5586
5587 if (!cts_only) { /* RTS/CTS */
5588 dur = 3 * sifs;
5589 dur +=
7d4df48e 5590 (u16) wlc_calc_cts_time(wlc, rts_rate,
a9533e7e
HP
5591 rts_preamble_type);
5592 } else { /* CTS-TO-SELF */
5593 dur = 2 * sifs;
5594 }
5595
5596 dur +=
7d4df48e 5597 (u16) wlc_calc_frame_time(wlc, frame_rate, frame_preamble_type,
a9533e7e
HP
5598 frame_len);
5599 if (ba)
5600 dur +=
7d4df48e 5601 (u16) wlc_calc_ba_time(wlc, frame_rate,
a9533e7e
HP
5602 WLC_SHORT_PREAMBLE);
5603 else
5604 dur +=
7d4df48e 5605 (u16) wlc_calc_ack_time(wlc, frame_rate,
a9533e7e 5606 frame_preamble_type);
90ea2296 5607 return dur;
a9533e7e
HP
5608}
5609
c6a9e1fc 5610static bool wlc_phy_rspec_check(struct wlc_info *wlc, u16 bw, ratespec_t rspec)
a9533e7e
HP
5611{
5612 if (IS_MCS(rspec)) {
5613 uint mcs = rspec & RSPEC_RATE_MASK;
5614
5615 if (mcs < 8) {
5616 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5617 } else if ((mcs >= 8) && (mcs <= 23)) {
5618 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SDM);
5619 } else if (mcs == 32) {
5620 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5621 ASSERT(bw == PHY_TXC1_BW_40MHZ_DUP);
5622 }
5623 } else if (IS_OFDM(rspec)) {
5624 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_STBC);
5625 } else {
5626 ASSERT(IS_CCK(rspec));
5627
5628 ASSERT((bw == PHY_TXC1_BW_20MHZ)
5629 || (bw == PHY_TXC1_BW_20MHZ_UP));
5630 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SISO);
5631 }
5632
0f0881b0 5633 return true;
a9533e7e
HP
5634}
5635
c6a9e1fc 5636u16 BCMFASTPATH wlc_phytxctl1_calc(struct wlc_info *wlc, ratespec_t rspec)
a9533e7e 5637{
7d4df48e
GKH
5638 u16 phyctl1 = 0;
5639 u16 bw;
a9533e7e
HP
5640
5641 if (WLCISLCNPHY(wlc->band)) {
5642 bw = PHY_TXC1_BW_20MHZ;
5643 } else {
5644 bw = RSPEC_GET_BW(rspec);
5645 /* 10Mhz is not supported yet */
5646 if (bw < PHY_TXC1_BW_20MHZ) {
f4528696
JP
5647 WL_ERROR("wlc_phytxctl1_calc: bw %d is not supported yet, set to 20L\n",
5648 bw);
a9533e7e
HP
5649 bw = PHY_TXC1_BW_20MHZ;
5650 }
5651
5652 wlc_phy_rspec_check(wlc, bw, rspec);
5653 }
5654
5655 if (IS_MCS(rspec)) {
5656 uint mcs = rspec & RSPEC_RATE_MASK;
5657
5658 /* bw, stf, coding-type is part of RSPEC_PHYTXBYTE2 returns */
5659 phyctl1 = RSPEC_PHYTXBYTE2(rspec);
5660 /* set the upper byte of phyctl1 */
5661 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
5662 } else if (IS_CCK(rspec) && !WLCISLCNPHY(wlc->band)
5663 && !WLCISSSLPNPHY(wlc->band)) {
5664 /* In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate */
5665 /* Eventually MIMOPHY would also be converted to this format */
5666 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
5667 phyctl1 = (bw | (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5668 } else { /* legacy OFDM/CCK */
e59fe083 5669 s16 phycfg;
a9533e7e 5670 /* get the phyctl byte from rate phycfg table */
ca8c1e59
JC
5671 phycfg = wlc_rate_legacy_phyctl(RSPEC2RATE(rspec));
5672 if (phycfg == -1) {
f4528696 5673 WL_ERROR("wlc_phytxctl1_calc: wrong legacy OFDM/CCK rate\n");
a9533e7e
HP
5674 ASSERT(0);
5675 phycfg = 0;
5676 }
5677 /* set the upper byte of phyctl1 */
5678 phyctl1 =
5679 (bw | (phycfg << 8) |
5680 (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5681 }
5682
5683#ifdef BCMDBG
5684 /* phy clock must support 40Mhz if tx descriptor uses it */
5685 if ((phyctl1 & PHY_TXC1_BW_MASK) >= PHY_TXC1_BW_40MHZ) {
5686 ASSERT(CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ);
a9533e7e 5687 ASSERT(wlc->chanspec == wlc_phy_chanspec_get(wlc->band->pi));
a9533e7e
HP
5688 }
5689#endif /* BCMDBG */
5690 return phyctl1;
5691}
5692
5693ratespec_t BCMFASTPATH
c6a9e1fc 5694wlc_rspec_to_rts_rspec(struct wlc_info *wlc, ratespec_t rspec, bool use_rspec,
7d4df48e 5695 u16 mimo_ctlchbw)
a9533e7e
HP
5696{
5697 ratespec_t rts_rspec = 0;
5698
5699 if (use_rspec) {
5700 /* use frame rate as rts rate */
5701 rts_rspec = rspec;
5702
5703 } else if (wlc->band->gmode && wlc->protection->_g && !IS_CCK(rspec)) {
5704 /* Use 11Mbps as the g protection RTS target rate and fallback.
5705 * Use the WLC_BASIC_RATE() lookup to find the best basic rate under the
5706 * target in case 11 Mbps is not Basic.
5707 * 6 and 9 Mbps are not usually selected by rate selection, but even
5708 * if the OFDM rate we are protecting is 6 or 9 Mbps, 11 is more robust.
5709 */
5710 rts_rspec = WLC_BASIC_RATE(wlc, WLC_RATE_11M);
5711 } else {
5712 /* calculate RTS rate and fallback rate based on the frame rate
5713 * RTS must be sent at a basic rate since it is a
5714 * control frame, sec 9.6 of 802.11 spec
5715 */
5716 rts_rspec = WLC_BASIC_RATE(wlc, rspec);
5717 }
5718
5719 if (WLC_PHY_11N_CAP(wlc->band)) {
5720 /* set rts txbw to correct side band */
5721 rts_rspec &= ~RSPEC_BW_MASK;
5722
5723 /* if rspec/rspec_fallback is 40MHz, then send RTS on both 20MHz channel
5724 * (DUP), otherwise send RTS on control channel
5725 */
5726 if (RSPEC_IS40MHZ(rspec) && !IS_CCK(rts_rspec))
5727 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
5728 else
5729 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
5730
5731 /* pick siso/cdd as default for ofdm */
5732 if (IS_OFDM(rts_rspec)) {
5733 rts_rspec &= ~RSPEC_STF_MASK;
5734 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
5735 }
5736 }
5737 return rts_rspec;
5738}
5739
5740/*
5741 * Add d11txh_t, cck_phy_hdr_t.
5742 *
5743 * 'p' data must start with 802.11 MAC header
5744 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
5745 *
5746 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
5747 *
5748 */
7d4df48e 5749static u16 BCMFASTPATH
c6a9e1fc 5750wlc_d11hdrs_mac80211(struct wlc_info *wlc, struct ieee80211_hw *hw,
c26b1378 5751 struct sk_buff *p, struct scb *scb, uint frag,
a9533e7e 5752 uint nfrags, uint queue, uint next_frag_len,
7cc4a4c0 5753 wsec_key_t *key, ratespec_t rspec_override)
a9533e7e 5754{
3e9796f9 5755 struct ieee80211_hdr *h;
a9533e7e 5756 d11txh_t *txh;
41feb5ed 5757 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
e69284f2 5758 struct osl_info *osh;
a9533e7e 5759 int len, phylen, rts_phylen;
7d4df48e
GKH
5760 u16 fc, type, frameid, mch, phyctl, xfts, mainrates;
5761 u16 seq = 0, mcl = 0, status = 0;
a9533e7e
HP
5762 ratespec_t rspec[2] = { WLC_RATE_1M, WLC_RATE_1M }, rts_rspec[2] = {
5763 WLC_RATE_1M, WLC_RATE_1M};
0965ae88
GKH
5764 bool use_rts = false;
5765 bool use_cts = false;
5766 bool use_rifs = false;
5767 bool short_preamble[2] = { false, false };
41feb5ed
GKH
5768 u8 preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5769 u8 rts_preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5770 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
f3dc3ea4 5771 struct ieee80211_rts *rts = NULL;
a9533e7e
HP
5772 bool qos;
5773 uint ac;
66cbd3ab 5774 u32 rate_val[2];
0965ae88 5775 bool hwtkmic = false;
7d4df48e 5776 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
a9533e7e
HP
5777#ifdef WLANTSEL
5778#define ANTCFG_NONE 0xFF
41feb5ed
GKH
5779 u8 antcfg = ANTCFG_NONE;
5780 u8 fbantcfg = ANTCFG_NONE;
a9533e7e
HP
5781#endif
5782 uint phyctl1_stf = 0;
7d4df48e 5783 u16 durid = 0;
a9533e7e
HP
5784 struct ieee80211_tx_rate *txrate[2];
5785 int k;
5786 struct ieee80211_tx_info *tx_info;
5787 bool is_mcs[2];
7d4df48e 5788 u16 mimo_txbw;
41feb5ed 5789 u8 mimo_preamble_type;
a9533e7e
HP
5790
5791 frameid = 0;
5792
5793 ASSERT(queue < NFIFO);
5794
5795 osh = wlc->osh;
5796
5797 /* locate 802.11 MAC header */
3e9796f9
RV
5798 h = (struct ieee80211_hdr *)(p->data);
5799 fc = ltoh16(h->frame_control);
d83b2a8a 5800 type = (fc & IEEE80211_FCTL_FTYPE);
a9533e7e 5801
d83b2a8a
RV
5802 qos = (type == IEEE80211_FTYPE_DATA &&
5803 FC_SUBTYPE_ANY_QOS(fc));
a9533e7e
HP
5804
5805 /* compute length of frame in bytes for use in PLCP computations */
5806 len = pkttotlen(osh, p);
3726ed4d 5807 phylen = len + FCS_LEN;
a9533e7e
HP
5808
5809 /* If WEP enabled, add room in phylen for the additional bytes of
5810 * ICV which MAC generates. We do NOT add the additional bytes to
5811 * the packet itself, thus phylen = packet length + ICV_LEN + FCS_LEN
5812 * in this case
5813 */
5814 if (key) {
5815 phylen += key->icv_len;
5816 }
5817
5818 /* Get tx_info */
5819 tx_info = IEEE80211_SKB_CB(p);
5820 ASSERT(tx_info);
5821
5822 /* add PLCP */
c303ecbd 5823 plcp = skb_push(p, D11_PHY_HDR_LEN);
a9533e7e
HP
5824
5825 /* add Broadcom tx descriptor header */
c303ecbd 5826 txh = (d11txh_t *) skb_push(p, D11_TXH_LEN);
cf60191a 5827 memset(txh, 0, D11_TXH_LEN);
a9533e7e
HP
5828
5829 /* setup frameid */
5830 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
5831 /* non-AP STA should never use BCMC queue */
5832 ASSERT(queue != TX_BCMC_FIFO);
5833 if (queue == TX_BCMC_FIFO) {
f4528696
JP
5834 WL_ERROR("wl%d: %s: ASSERT queue == TX_BCMC!\n",
5835 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
5836 frameid = bcmc_fid_generate(wlc, NULL, txh);
5837 } else {
5838 /* Increment the counter for first fragment */
5839 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
54991ad6 5840 SCB_SEQNUM(scb, p->priority)++;
a9533e7e
HP
5841 }
5842
5843 /* extract fragment number from frame first */
5844 seq = ltoh16(seq) & FRAGNUM_MASK;
54991ad6 5845 seq |= (SCB_SEQNUM(scb, p->priority) << SEQNUM_SHIFT);
3e9796f9 5846 h->seq_ctrl = htol16(seq);
a9533e7e
HP
5847
5848 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5849 (queue & TXFID_QUEUE_MASK);
5850 }
5851 }
5852 frameid |= queue & TXFID_QUEUE_MASK;
5853
5854 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
5855 if (SCB_PS(scb) || ((fc & FC_KIND_MASK) == FC_BEACON))
5856 mcl |= TXC_IGNOREPMQ;
5857
5858 ASSERT(hw->max_rates <= IEEE80211_TX_MAX_RATES);
5859 ASSERT(hw->max_rates == 2);
5860
5861 txrate[0] = tx_info->control.rates;
5862 txrate[1] = txrate[0] + 1;
5863
5864 ASSERT(txrate[0]->idx >= 0);
5865 /* if rate control algorithm didn't give us a fallback rate, use the primary rate */
5866 if (txrate[1]->idx < 0) {
5867 txrate[1] = txrate[0];
5868 }
a9533e7e
HP
5869
5870 for (k = 0; k < hw->max_rates; k++) {
5871 is_mcs[k] =
0965ae88 5872 txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
a9533e7e
HP
5873 if (!is_mcs[k]) {
5874 ASSERT(!(tx_info->flags & IEEE80211_TX_CTL_AMPDU));
5875 if ((txrate[k]->idx >= 0)
5876 && (txrate[k]->idx <
5877 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
5878 rate_val[k] =
5879 hw->wiphy->bands[tx_info->band]->
5880 bitrates[txrate[k]->idx].hw_value;
5881 short_preamble[k] =
5882 txrate[k]->
5883 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
0965ae88 5884 true : false;
a9533e7e
HP
5885 } else {
5886 ASSERT((txrate[k]->idx >= 0) &&
5887 (txrate[k]->idx <
5888 hw->wiphy->bands[tx_info->band]->
5889 n_bitrates));
5890 rate_val[k] = WLC_RATE_1M;
5891 }
5892 } else {
5893 rate_val[k] = txrate[k]->idx;
5894 }
5895 /* Currently only support same setting for primay and fallback rates.
5896 * Unify flags for each rate into a single value for the frame
5897 */
5898 use_rts |=
5899 txrate[k]->
0965ae88 5900 flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
a9533e7e
HP
5901 use_cts |=
5902 txrate[k]->
0965ae88 5903 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
a9533e7e
HP
5904
5905 if (is_mcs[k])
5906 rate_val[k] |= NRATE_MCS_INUSE;
5907
5908 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band, rate_val[k]);
5909
5910 /* (1) RATE: determine and validate primary rate and fallback rates */
5911 if (!RSPEC_ACTIVE(rspec[k])) {
5912 ASSERT(RSPEC_ACTIVE(rspec[k]));
5913 rspec[k] = WLC_RATE_1M;
5914 } else {
3ca5ada5 5915 if (WLANTSEL_ENAB(wlc) &&
3e9796f9 5916 !is_multicast_ether_addr(h->addr1)) {
a9533e7e 5917 /* set tx antenna config */
0965ae88 5918 wlc_antsel_antcfg_get(wlc->asi, false, false, 0,
a9533e7e
HP
5919 0, &antcfg, &fbantcfg);
5920 }
5921 }
5922 }
5923
5924 phyctl1_stf = wlc->stf->ss_opmode;
5925
5926 if (N_ENAB(wlc->pub)) {
5927 for (k = 0; k < hw->max_rates; k++) {
5928 /* apply siso/cdd to single stream mcs's or ofdm if rspec is auto selected */
5929 if (((IS_MCS(rspec[k]) &&
5930 IS_SINGLE_STREAM(rspec[k] & RSPEC_RATE_MASK)) ||
5931 IS_OFDM(rspec[k]))
5932 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
5933 || !(rspec[k] & RSPEC_OVERRIDE))) {
5934 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
5935
5936 /* For SISO MCS use STBC if possible */
5937 if (IS_MCS(rspec[k])
5938 && WLC_STF_SS_STBC_TX(wlc, scb)) {
41feb5ed 5939 u8 stc;
a9533e7e
HP
5940
5941 ASSERT(WLC_STBC_CAP_PHY(wlc));
5942 stc = 1; /* Nss for single stream is always 1 */
5943 rspec[k] |=
5944 (PHY_TXC1_MODE_STBC <<
5945 RSPEC_STF_SHIFT) | (stc <<
5946 RSPEC_STC_SHIFT);
5947 } else
5948 rspec[k] |=
5949 (phyctl1_stf << RSPEC_STF_SHIFT);
5950 }
5951
5952 /* Is the phy configured to use 40MHZ frames? If so then pick the desired txbw */
5953 if (CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ) {
5954 /* default txbw is 20in40 SB */
5955 mimo_ctlchbw = mimo_txbw =
5956 CHSPEC_SB_UPPER(WLC_BAND_PI_RADIO_CHANSPEC)
5957 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
5958
5959 if (IS_MCS(rspec[k])) {
5960 /* mcs 32 must be 40b/w DUP */
5961 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
5962 mimo_txbw =
5963 PHY_TXC1_BW_40MHZ_DUP;
5964 /* use override */
5965 } else if (wlc->mimo_40txbw != AUTO)
5966 mimo_txbw = wlc->mimo_40txbw;
5967 /* else check if dst is using 40 Mhz */
5968 else if (scb->flags & SCB_IS40)
5969 mimo_txbw = PHY_TXC1_BW_40MHZ;
5970 } else if (IS_OFDM(rspec[k])) {
5971 if (wlc->ofdm_40txbw != AUTO)
5972 mimo_txbw = wlc->ofdm_40txbw;
5973 } else {
5974 ASSERT(IS_CCK(rspec[k]));
5975 if (wlc->cck_40txbw != AUTO)
5976 mimo_txbw = wlc->cck_40txbw;
5977 }
5978 } else {
5979 /* mcs32 is 40 b/w only.
5980 * This is possible for probe packets on a STA during SCAN
5981 */
5982 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
5983 /* mcs 0 */
5984 rspec[k] = RSPEC_MIMORATE;
5985 }
5986 mimo_txbw = PHY_TXC1_BW_20MHZ;
5987 }
5988
5989 /* Set channel width */
5990 rspec[k] &= ~RSPEC_BW_MASK;
5991 if ((k == 0) || ((k > 0) && IS_MCS(rspec[k])))
5992 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
5993 else
5994 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
5995
5996 /* Set Short GI */
5997#ifdef NOSGIYET
5998 if (IS_MCS(rspec[k])
5999 && (txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
6000 rspec[k] |= RSPEC_SHORT_GI;
6001 else if (!(txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
6002 rspec[k] &= ~RSPEC_SHORT_GI;
6003#else
6004 rspec[k] &= ~RSPEC_SHORT_GI;
6005#endif
6006
6007 mimo_preamble_type = WLC_MM_PREAMBLE;
6008 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6009 mimo_preamble_type = WLC_GF_PREAMBLE;
6010
6011 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6012 && (!IS_MCS(rspec[k]))) {
f4528696
JP
6013 WL_ERROR("wl%d: %s: IEEE80211_TX_RC_MCS != IS_MCS(rspec)\n",
6014 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
6015 ASSERT(0 && "Rate mismatch");
6016 }
6017
6018 if (IS_MCS(rspec[k])) {
6019 preamble_type[k] = mimo_preamble_type;
6020
6021 /* if SGI is selected, then forced mm for single stream */
6022 if ((rspec[k] & RSPEC_SHORT_GI)
6023 && IS_SINGLE_STREAM(rspec[k] &
6024 RSPEC_RATE_MASK)) {
6025 preamble_type[k] = WLC_MM_PREAMBLE;
6026 }
6027 }
6028
6029 /* mimo bw field MUST now be valid in the rspec (it affects duration calculations) */
6030 ASSERT(VALID_RATE_DBG(wlc, rspec[0]));
6031
6032 /* should be better conditionalized */
6033 if (!IS_MCS(rspec[0])
6034 && (tx_info->control.rates[0].
6035 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6036 preamble_type[k] = WLC_SHORT_PREAMBLE;
6037
6038 ASSERT(!IS_MCS(rspec[0])
6039 || WLC_IS_MIMO_PREAMBLE(preamble_type[k]));
6040 }
6041 } else {
6042 for (k = 0; k < hw->max_rates; k++) {
6043 /* Set ctrlchbw as 20Mhz */
6044 ASSERT(!IS_MCS(rspec[k]));
6045 rspec[k] &= ~RSPEC_BW_MASK;
6046 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6047
6048 /* for nphy, stf of ofdm frames must follow policies */
6049 if (WLCISNPHY(wlc->band) && IS_OFDM(rspec[k])) {
6050 rspec[k] &= ~RSPEC_STF_MASK;
6051 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6052 }
6053 }
6054 }
6055
6056 /* Reset these for use with AMPDU's */
6057 txrate[0]->count = 0;
6058 txrate[1]->count = 0;
6059
fd821d1e
AS
6060 /* (2) PROTECTION, may change rspec */
6061 if ((ieee80211_is_data(fc) || ieee80211_is_mgmt(fc)) &&
6062 (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
6063 use_rts = true;
6064
a9533e7e
HP
6065 /* (3) PLCP: determine PLCP header and MAC duration, fill d11txh_t */
6066 wlc_compute_plcp(wlc, rspec[0], phylen, plcp);
6067 wlc_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6068 bcopy(plcp_fallback, (char *)&txh->FragPLCPFallback,
6069 sizeof(txh->FragPLCPFallback));
6070
6071 /* Length field now put in CCK FBR CRC field */
6072 if (IS_CCK(rspec[1])) {
6073 txh->FragPLCPFallback[4] = phylen & 0xff;
6074 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6075 }
6076
6077 /* MIMO-RATE: need validation ?? */
6078 mainrates =
6079 IS_OFDM(rspec[0]) ? D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) plcp) :
6080 plcp[0];
6081
6082 /* DUR field for main rate */
3ca5ada5 6083 if ((fc != FC_PS_POLL) &&
3e9796f9 6084 !is_multicast_ether_addr(h->addr1) && !use_rifs) {
a9533e7e
HP
6085 durid =
6086 wlc_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6087 next_frag_len);
3e9796f9 6088 h->duration_id = htol16(durid);
a9533e7e
HP
6089 } else if (use_rifs) {
6090 /* NAV protect to end of next max packet size */
6091 durid =
7d4df48e 6092 (u16) wlc_calc_frame_time(wlc, rspec[0],
a9533e7e
HP
6093 preamble_type[0],
6094 DOT11_MAX_FRAG_LEN);
6095 durid += RIFS_11N_TIME;
3e9796f9 6096 h->duration_id = htol16(durid);
a9533e7e
HP
6097 }
6098
6099 /* DUR field for fallback rate */
6100 if (fc == FC_PS_POLL)
3e9796f9
RV
6101 txh->FragDurFallback = h->duration_id;
6102 else if (is_multicast_ether_addr(h->addr1) || use_rifs)
a9533e7e
HP
6103 txh->FragDurFallback = 0;
6104 else {
6105 durid = wlc_compute_frame_dur(wlc, rspec[1],
6106 preamble_type[1], next_frag_len);
6107 txh->FragDurFallback = htol16(durid);
6108 }
6109
6110 /* (4) MAC-HDR: MacTxControlLow */
6111 if (frag == 0)
6112 mcl |= TXC_STARTMSDU;
6113
3e9796f9 6114 if (!is_multicast_ether_addr(h->addr1))
a9533e7e
HP
6115 mcl |= TXC_IMMEDACK;
6116
6117 if (BAND_5G(wlc->band->bandtype))
6118 mcl |= TXC_FREQBAND_5G;
6119
6120 if (CHSPEC_IS40(WLC_BAND_PI_RADIO_CHANSPEC))
6121 mcl |= TXC_BW_40;
6122
6123 /* set AMIC bit if using hardware TKIP MIC */
6124 if (hwtkmic)
6125 mcl |= TXC_AMIC;
6126
6127 txh->MacTxControlLow = htol16(mcl);
6128
6129 /* MacTxControlHigh */
6130 mch = 0;
6131
6132 /* Set fallback rate preamble type */
6133 if ((preamble_type[1] == WLC_SHORT_PREAMBLE) ||
6134 (preamble_type[1] == WLC_GF_PREAMBLE)) {
6135 ASSERT((preamble_type[1] == WLC_GF_PREAMBLE) ||
6136 (!IS_MCS(rspec[1])));
6137 if (RSPEC2RATE(rspec[1]) != WLC_RATE_1M)
6138 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6139 }
6140
6141 /* MacFrameControl */
3e9796f9
RV
6142 bcopy((char *)&h->frame_control, (char *)&txh->MacFrameControl,
6143 sizeof(u16));
a9533e7e
HP
6144 txh->TxFesTimeNormal = htol16(0);
6145
6146 txh->TxFesTimeFallback = htol16(0);
6147
6148 /* TxFrameRA */
3e9796f9 6149 bcopy((char *)&h->addr1, (char *)&txh->TxFrameRA, ETH_ALEN);
a9533e7e
HP
6150
6151 /* TxFrameID */
6152 txh->TxFrameID = htol16(frameid);
6153
6154 /* TxStatus, Note the case of recreating the first frag of a suppressed frame
6155 * then we may need to reset the retry cnt's via the status reg
6156 */
6157 txh->TxStatus = htol16(status);
6158
6159 if (D11REV_GE(wlc->pub->corerev, 16)) {
6160 /* extra fields for ucode AMPDU aggregation, the new fields are added to
6161 * the END of previous structure so that it's compatible in driver.
6162 * In old rev ucode, these fields should be ignored
6163 */
6164 txh->MaxNMpdus = htol16(0);
6165 txh->MaxABytes_MRT = htol16(0);
6166 txh->MaxABytes_FBR = htol16(0);
6167 txh->MinMBytes = htol16(0);
6168 }
6169
6170 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration, furnish d11txh_t */
6171 /* RTS PLCP header and RTS frame */
6172 if (use_rts || use_cts) {
6173 if (use_rts && use_cts)
0965ae88 6174 use_cts = false;
a9533e7e
HP
6175
6176 for (k = 0; k < 2; k++) {
6177 rts_rspec[k] = wlc_rspec_to_rts_rspec(wlc, rspec[k],
0965ae88 6178 false,
a9533e7e
HP
6179 mimo_ctlchbw);
6180 }
6181
6182 if (!IS_OFDM(rts_rspec[0]) &&
6183 !((RSPEC2RATE(rts_rspec[0]) == WLC_RATE_1M) ||
6184 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6185 rts_preamble_type[0] = WLC_SHORT_PREAMBLE;
6186 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6187 }
6188
6189 if (!IS_OFDM(rts_rspec[1]) &&
6190 !((RSPEC2RATE(rts_rspec[1]) == WLC_RATE_1M) ||
6191 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6192 rts_preamble_type[1] = WLC_SHORT_PREAMBLE;
6193 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6194 }
6195
6196 /* RTS/CTS additions to MacTxControlLow */
6197 if (use_cts) {
6198 txh->MacTxControlLow |= htol16(TXC_SENDCTS);
6199 } else {
6200 txh->MacTxControlLow |= htol16(TXC_SENDRTS);
6201 txh->MacTxControlLow |= htol16(TXC_LONGFRAME);
6202 }
6203
6204 /* RTS PLCP header */
f024c48a 6205 ASSERT(IS_ALIGNED((unsigned long)txh->RTSPhyHeader, sizeof(u16)));
a9533e7e
HP
6206 rts_plcp = txh->RTSPhyHeader;
6207 if (use_cts)
3726ed4d 6208 rts_phylen = DOT11_CTS_LEN + FCS_LEN;
a9533e7e 6209 else
3726ed4d 6210 rts_phylen = DOT11_RTS_LEN + FCS_LEN;
a9533e7e
HP
6211
6212 wlc_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6213
6214 /* fallback rate version of RTS PLCP header */
6215 wlc_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6216 rts_plcp_fallback);
6217 bcopy(rts_plcp_fallback, (char *)&txh->RTSPLCPFallback,
6218 sizeof(txh->RTSPLCPFallback));
6219
6220 /* RTS frame fields... */
f3dc3ea4 6221 rts = (struct ieee80211_rts *)&txh->rts_frame;
a9533e7e
HP
6222
6223 durid = wlc_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6224 rspec[0], rts_preamble_type[0],
0965ae88 6225 preamble_type[0], phylen, false);
f3dc3ea4 6226 rts->duration = htol16(durid);
a9533e7e
HP
6227 /* fallback rate version of RTS DUR field */
6228 durid = wlc_compute_rtscts_dur(wlc, use_cts,
6229 rts_rspec[1], rspec[1],
6230 rts_preamble_type[1],
0965ae88 6231 preamble_type[1], phylen, false);
a9533e7e
HP
6232 txh->RTSDurFallback = htol16(durid);
6233
6234 if (use_cts) {
f3dc3ea4 6235 rts->frame_control = htol16(FC_CTS);
3e9796f9 6236 bcopy((char *)&h->addr2, (char *)&rts->ra, ETH_ALEN);
a9533e7e 6237 } else {
f3dc3ea4 6238 rts->frame_control = htol16((u16) FC_RTS);
3e9796f9 6239 bcopy((char *)&h->addr1, (char *)&rts->ra,
b8d63078 6240 2 * ETH_ALEN);
a9533e7e
HP
6241 }
6242
6243 /* mainrate
6244 * low 8 bits: main frag rate/mcs,
6245 * high 8 bits: rts/cts rate/mcs
6246 */
6247 mainrates |= (IS_OFDM(rts_rspec[0]) ?
6248 D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) rts_plcp) :
6249 rts_plcp[0]) << 8;
6250 } else {
9249ede9
BR
6251 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6252 memset((char *)&txh->rts_frame, 0,
f3dc3ea4 6253 sizeof(struct ieee80211_rts));
9249ede9 6254 memset((char *)txh->RTSPLCPFallback, 0,
a9533e7e
HP
6255 sizeof(txh->RTSPLCPFallback));
6256 txh->RTSDurFallback = 0;
6257 }
6258
6259#ifdef SUPPORT_40MHZ
6260 /* add null delimiter count */
6261 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && IS_MCS(rspec)) {
6262 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6263 wlc_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6264 }
6265#endif
6266
6267 /* Now that RTS/RTS FB preamble types are updated, write the final value */
6268 txh->MacTxControlHigh = htol16(mch);
6269
6270 /* MainRates (both the rts and frag plcp rates have been calculated now) */
6271 txh->MainRates = htol16(mainrates);
6272
6273 /* XtraFrameTypes */
6274 xfts = FRAMETYPE(rspec[1], wlc->mimoft);
6275 xfts |= (FRAMETYPE(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6276 xfts |= (FRAMETYPE(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6277 xfts |=
6278 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC) << XFTS_CHANNEL_SHIFT;
6279 txh->XtraFrameTypes = htol16(xfts);
6280
6281 /* PhyTxControlWord */
6282 phyctl = FRAMETYPE(rspec[0], wlc->mimoft);
6283 if ((preamble_type[0] == WLC_SHORT_PREAMBLE) ||
6284 (preamble_type[0] == WLC_GF_PREAMBLE)) {
6285 ASSERT((preamble_type[0] == WLC_GF_PREAMBLE)
6286 || !IS_MCS(rspec[0]));
6287 if (RSPEC2RATE(rspec[0]) != WLC_RATE_1M)
6288 phyctl |= PHY_TXC_SHORT_HDR;
e4cf544e 6289 wlc->pub->_cnt->txprshort++;
a9533e7e
HP
6290 }
6291
6292 /* phytxant is properly bit shifted */
6293 phyctl |= wlc_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6294 txh->PhyTxControlWord = htol16(phyctl);
6295
6296 /* PhyTxControlWord_1 */
6297 if (WLC_PHY_11N_CAP(wlc->band)) {
7d4df48e 6298 u16 phyctl1 = 0;
a9533e7e
HP
6299
6300 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[0]);
6301 txh->PhyTxControlWord_1 = htol16(phyctl1);
6302 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[1]);
6303 txh->PhyTxControlWord_1_Fbr = htol16(phyctl1);
6304
6305 if (use_rts || use_cts) {
6306 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[0]);
6307 txh->PhyTxControlWord_1_Rts = htol16(phyctl1);
6308 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[1]);
6309 txh->PhyTxControlWord_1_FbrRts = htol16(phyctl1);
6310 }
6311
6312 /*
6313 * For mcs frames, if mixedmode(overloaded with long preamble) is going to be set,
6314 * fill in non-zero MModeLen and/or MModeFbrLen
6315 * it will be unnecessary if they are separated
6316 */
6317 if (IS_MCS(rspec[0]) && (preamble_type[0] == WLC_MM_PREAMBLE)) {
7d4df48e 6318 u16 mmodelen =
a9533e7e
HP
6319 wlc_calc_lsig_len(wlc, rspec[0], phylen);
6320 txh->MModeLen = htol16(mmodelen);
6321 }
6322
6323 if (IS_MCS(rspec[1]) && (preamble_type[1] == WLC_MM_PREAMBLE)) {
7d4df48e 6324 u16 mmodefbrlen =
a9533e7e
HP
6325 wlc_calc_lsig_len(wlc, rspec[1], phylen);
6326 txh->MModeFbrLen = htol16(mmodefbrlen);
6327 }
6328 }
6329
6330 if (IS_MCS(rspec[0]))
6331 ASSERT(IS_MCS(rspec[1]));
6332
6333 ASSERT(!IS_MCS(rspec[0]) ||
6334 ((preamble_type[0] == WLC_MM_PREAMBLE) == (txh->MModeLen != 0)));
6335 ASSERT(!IS_MCS(rspec[1]) ||
6336 ((preamble_type[1] == WLC_MM_PREAMBLE) ==
6337 (txh->MModeFbrLen != 0)));
6338
ca8c1e59
JC
6339 ac = wme_fifo2ac[queue];
6340 if (SCB_WME(scb) && qos && wlc->edcf_txop[ac]) {
a9533e7e
HP
6341 uint frag_dur, dur, dur_fallback;
6342
3e9796f9 6343 ASSERT(!is_multicast_ether_addr(h->addr1));
a9533e7e
HP
6344
6345 /* WME: Update TXOP threshold */
6346 if ((!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) && (frag == 0)) {
6347 frag_dur =
6348 wlc_calc_frame_time(wlc, rspec[0], preamble_type[0],
6349 phylen);
6350
6351 if (rts) {
6352 /* 1 RTS or CTS-to-self frame */
6353 dur =
6354 wlc_calc_cts_time(wlc, rts_rspec[0],
6355 rts_preamble_type[0]);
6356 dur_fallback =
6357 wlc_calc_cts_time(wlc, rts_rspec[1],
6358 rts_preamble_type[1]);
6359 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
f3dc3ea4 6360 dur += ltoh16(rts->duration);
a9533e7e
HP
6361 dur_fallback += ltoh16(txh->RTSDurFallback);
6362 } else if (use_rifs) {
6363 dur = frag_dur;
6364 dur_fallback = 0;
6365 } else {
6366 /* frame + SIFS + ACK */
6367 dur = frag_dur;
6368 dur +=
6369 wlc_compute_frame_dur(wlc, rspec[0],
6370 preamble_type[0], 0);
6371
6372 dur_fallback =
6373 wlc_calc_frame_time(wlc, rspec[1],
6374 preamble_type[1],
6375 phylen);
6376 dur_fallback +=
6377 wlc_compute_frame_dur(wlc, rspec[1],
6378 preamble_type[1], 0);
6379 }
6380 /* NEED to set TxFesTimeNormal (hard) */
7d4df48e 6381 txh->TxFesTimeNormal = htol16((u16) dur);
a9533e7e 6382 /* NEED to set fallback rate version of TxFesTimeNormal (hard) */
7d4df48e 6383 txh->TxFesTimeFallback = htol16((u16) dur_fallback);
a9533e7e
HP
6384
6385 /* update txop byte threshold (txop minus intraframe overhead) */
6386 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6387 {
6388 uint newfragthresh;
6389
6390 newfragthresh =
6391 wlc_calc_frame_len(wlc, rspec[0],
6392 preamble_type[0],
6393 (wlc->
6394 edcf_txop[ac] -
6395 (dur -
6396 frag_dur)));
6397 /* range bound the fragthreshold */
6398 if (newfragthresh < DOT11_MIN_FRAG_LEN)
6399 newfragthresh =
6400 DOT11_MIN_FRAG_LEN;
6401 else if (newfragthresh >
6402 wlc->usr_fragthresh)
6403 newfragthresh =
6404 wlc->usr_fragthresh;
6405 /* update the fragthresh and do txc update */
6406 if (wlc->fragthresh[queue] !=
7d4df48e 6407 (u16) newfragthresh) {
a9533e7e 6408 wlc->fragthresh[queue] =
7d4df48e 6409 (u16) newfragthresh;
a9533e7e
HP
6410 }
6411 }
26e80554 6412#if defined(BCMDBG)
a9533e7e 6413 } else
f4528696
JP
6414 WL_ERROR("wl%d: %s txop invalid for rate %d\n",
6415 wlc->pub->unit, fifo_names[queue],
6416 RSPEC2RATE(rspec[0]));
a9533e7e
HP
6417
6418 if (dur > wlc->edcf_txop[ac])
f4528696
JP
6419 WL_ERROR("wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
6420 wlc->pub->unit, __func__,
6421 fifo_names[queue],
6422 phylen, wlc->fragthresh[queue],
6423 dur, wlc->edcf_txop[ac]);
26e80554
HM
6424#else
6425 }
6426#endif
a9533e7e
HP
6427 }
6428 }
6429
6430 return 0;
6431}
6432
c6a9e1fc 6433void wlc_tbtt(struct wlc_info *wlc, d11regs_t *regs)
a9533e7e
HP
6434{
6435 wlc_bsscfg_t *cfg = wlc->cfg;
6436
e4cf544e 6437 wlc->pub->_cnt->tbtt++;
a9533e7e
HP
6438
6439 if (BSSCFG_STA(cfg)) {
6440 /* run watchdog here if the watchdog timer is not armed */
6441 if (WLC_WATCHDOG_TBTT(wlc)) {
66cbd3ab 6442 u32 cur, delta;
a9533e7e
HP
6443 if (wlc->WDarmed) {
6444 wl_del_timer(wlc->wl, wlc->wdtimer);
0965ae88 6445 wlc->WDarmed = false;
a9533e7e
HP
6446 }
6447
6448 cur = OSL_SYSUPTIME();
6449 delta = cur > wlc->WDlast ? cur - wlc->WDlast :
66cbd3ab 6450 (u32) ~0 - wlc->WDlast + cur + 1;
a9533e7e
HP
6451 if (delta >= TIMER_INTERVAL_WATCHDOG) {
6452 wlc_watchdog((void *)wlc);
6453 wlc->WDlast = cur;
6454 }
6455
6456 wl_add_timer(wlc->wl, wlc->wdtimer,
0f0881b0
GKH
6457 wlc_watchdog_backup_bi(wlc), true);
6458 wlc->WDarmed = true;
a9533e7e
HP
6459 }
6460 }
6461
6462 if (!cfg->BSS) {
6463 /* DirFrmQ is now valid...defer setting until end of ATIM window */
6464 wlc->qvalid |= MCMD_DIRFRMQVAL;
6465 }
6466}
6467
6468/* GP timer is a freerunning 32 bit counter, decrements at 1 us rate */
c6a9e1fc 6469void wlc_hwtimer_gptimer_set(struct wlc_info *wlc, uint us)
a9533e7e
HP
6470{
6471 ASSERT(wlc->pub->corerev >= 3); /* no gptimer in earlier revs */
6472 W_REG(wlc->osh, &wlc->regs->gptimer, us);
6473}
6474
c6a9e1fc 6475void wlc_hwtimer_gptimer_abort(struct wlc_info *wlc)
a9533e7e
HP
6476{
6477 ASSERT(wlc->pub->corerev >= 3);
6478 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6479}
6480
c6a9e1fc 6481static void wlc_hwtimer_gptimer_cb(struct wlc_info *wlc)
a9533e7e
HP
6482{
6483 /* when interrupt is generated, the counter is loaded with last value
6484 * written and continue to decrement. So it has to be cleaned first
6485 */
6486 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6487}
6488
6489/*
6490 * This fn has all the high level dpc processing from wlc_dpc.
6491 * POLICY: no macinstatus change, no bounding loop.
6492 * All dpc bounding should be handled in BMAC dpc, like txstatus and rxint
6493 */
c6a9e1fc 6494void wlc_high_dpc(struct wlc_info *wlc, u32 macintstatus)
a9533e7e
HP
6495{
6496 d11regs_t *regs = wlc->regs;
6497#ifdef BCMDBG
6498 char flagstr[128];
6499 static const bcm_bit_desc_t int_flags[] = {
6500 {MI_MACSSPNDD, "MACSSPNDD"},
6501 {MI_BCNTPL, "BCNTPL"},
6502 {MI_TBTT, "TBTT"},
6503 {MI_BCNSUCCESS, "BCNSUCCESS"},
6504 {MI_BCNCANCLD, "BCNCANCLD"},
6505 {MI_ATIMWINEND, "ATIMWINEND"},
6506 {MI_PMQ, "PMQ"},
6507 {MI_NSPECGEN_0, "NSPECGEN_0"},
6508 {MI_NSPECGEN_1, "NSPECGEN_1"},
6509 {MI_MACTXERR, "MACTXERR"},
6510 {MI_NSPECGEN_3, "NSPECGEN_3"},
6511 {MI_PHYTXERR, "PHYTXERR"},
6512 {MI_PME, "PME"},
6513 {MI_GP0, "GP0"},
6514 {MI_GP1, "GP1"},
6515 {MI_DMAINT, "DMAINT"},
6516 {MI_TXSTOP, "TXSTOP"},
6517 {MI_CCA, "CCA"},
6518 {MI_BG_NOISE, "BG_NOISE"},
6519 {MI_DTIM_TBTT, "DTIM_TBTT"},
6520 {MI_PRQ, "PRQ"},
6521 {MI_PWRUP, "PWRUP"},
6522 {MI_RFDISABLE, "RFDISABLE"},
6523 {MI_TFS, "TFS"},
6524 {MI_PHYCHANGED, "PHYCHANGED"},
6525 {MI_TO, "TO"},
6526 {0, NULL}
6527 };
6528
6529 if (macintstatus & ~(MI_TBTT | MI_TXSTOP)) {
6530 bcm_format_flags(int_flags, macintstatus, flagstr,
6531 sizeof(flagstr));
f4528696
JP
6532 WL_TRACE("wl%d: macintstatus 0x%x %s\n",
6533 wlc->pub->unit, macintstatus, flagstr);
a9533e7e
HP
6534 }
6535#endif /* BCMDBG */
6536
6537 if (macintstatus & MI_PRQ) {
6538 /* Process probe request FIFO */
6539 ASSERT(0 && "PRQ Interrupt in non-MBSS");
6540 }
6541
6542 /* TBTT indication */
6543 /* ucode only gives either TBTT or DTIM_TBTT, not both */
6544 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
6545 wlc_tbtt(wlc, regs);
6546
6547 if (macintstatus & MI_GP0) {
f4528696
JP
6548 WL_ERROR("wl%d: PSM microcode watchdog fired at %d (seconds). Resetting.\n",
6549 wlc->pub->unit, wlc->pub->now);
a9533e7e
HP
6550
6551 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
dfa26436 6552 __func__, wlc->pub->sih->chip,
ff29ee8f 6553 wlc->pub->sih->chiprev);
a9533e7e 6554
e4cf544e 6555 wlc->pub->_cnt->psmwds++;
a9533e7e
HP
6556
6557 /* big hammer */
6558 wl_init(wlc->wl);
6559 }
6560
6561 /* gptimer timeout */
6562 if (macintstatus & MI_TO) {
6563 wlc_hwtimer_gptimer_cb(wlc);
6564 }
6565
6566 if (macintstatus & MI_RFDISABLE) {
f4528696
JP
6567 WL_ERROR("wl%d: MAC Detected a change on the RF Disable Input 0x%x\n",
6568 wlc->pub->unit,
6569 R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD);
a9533e7e
HP
6570 /* delay the cleanup to wl_down in IBSS case */
6571 if ((R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD)) {
6572 int idx;
6573 wlc_bsscfg_t *bsscfg;
6574 FOREACH_BSS(wlc, idx, bsscfg) {
6575 if (!BSSCFG_STA(bsscfg) || !bsscfg->enable
6576 || !bsscfg->BSS)
6577 continue;
f4528696
JP
6578 WL_ERROR("wl%d: wlc_dpc: rfdisable -> wlc_bsscfg_disable()\n",
6579 wlc->pub->unit);
a9533e7e
HP
6580 }
6581 }
6582 }
6583
6584 /* send any enq'd tx packets. Just makes sure to jump start tx */
6585 if (!pktq_empty(&wlc->active_queue->q))
6586 wlc_send_q(wlc, wlc->active_queue);
6587
a9533e7e 6588 ASSERT(wlc_ps_check(wlc));
a9533e7e
HP
6589}
6590
c6a9e1fc 6591static void *wlc_15420war(struct wlc_info *wlc, uint queue)
a9533e7e 6592{
17d76651 6593 struct hnddma_pub *di;
a9533e7e
HP
6594 void *p;
6595
6596 ASSERT(queue < NFIFO);
6597
6598 if ((D11REV_IS(wlc->pub->corerev, 4))
6599 || (D11REV_GT(wlc->pub->corerev, 6)))
90ea2296 6600 return NULL;
a9533e7e
HP
6601
6602 di = wlc->hw->di[queue];
6603 ASSERT(di != NULL);
6604
6605 /* get next packet, ignoring XmtStatus.Curr */
6606 p = dma_getnexttxp(di, HNDDMA_RANGE_ALL);
6607
6608 /* sw block tx dma */
6609 dma_txblock(di);
6610
6611 /* if tx ring is now empty, reset and re-init the tx dma channel */
6612 if (dma_txactive(wlc->hw->di[queue]) == 0) {
e4cf544e 6613 wlc->pub->_cnt->txdmawar++;
a9533e7e 6614 if (!dma_txreset(di))
f4528696
JP
6615 WL_ERROR("wl%d: %s: dma_txreset[%d]: cannot stop dma\n",
6616 wlc->pub->unit, __func__, queue);
a9533e7e
HP
6617 dma_txinit(di);
6618 }
90ea2296 6619 return p;
a9533e7e
HP
6620}
6621
c6a9e1fc 6622static void wlc_war16165(struct wlc_info *wlc, bool tx)
a9533e7e
HP
6623{
6624 if (tx) {
6625 /* the post-increment is used in STAY_AWAKE macro */
6626 if (wlc->txpend16165war++ == 0)
6627 wlc_set_ps_ctrl(wlc);
6628 } else {
6629 wlc->txpend16165war--;
6630 if (wlc->txpend16165war == 0)
6631 wlc_set_ps_ctrl(wlc);
6632 }
6633}
6634
6635/* process an individual tx_status_t */
6636/* WLC_HIGH_API */
6637bool BCMFASTPATH
c6a9e1fc 6638wlc_dotxstatus(struct wlc_info *wlc, tx_status_t *txs, u32 frm_tx2)
a9533e7e 6639{
c26b1378 6640 struct sk_buff *p;
a9533e7e
HP
6641 uint queue;
6642 d11txh_t *txh;
6643 struct scb *scb = NULL;
6644 bool free_pdu;
e69284f2 6645 struct osl_info *osh;
a9533e7e
HP
6646 int tx_rts, tx_frame_count, tx_rts_count;
6647 uint totlen, supr_status;
6648 bool lastframe;
3e9796f9 6649 struct ieee80211_hdr *h;
7d4df48e
GKH
6650 u16 fc;
6651 u16 mcl;
a9533e7e
HP
6652 struct ieee80211_tx_info *tx_info;
6653 struct ieee80211_tx_rate *txrate;
6654 int i;
6655
6656 (void)(frm_tx2); /* Compiler reference to avoid unused variable warning */
6657
6658 /* discard intermediate indications for ucode with one legitimate case:
6659 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange, but the subsequent
6660 * tx of DATA failed. so it will start rts/cts from the beginning (resetting the rts
6661 * transmission count)
6662 */
6663 if (!(txs->status & TX_STATUS_AMPDU)
6664 && (txs->status & TX_STATUS_INTERMEDIATE)) {
6665 WLCNTADD(wlc->pub->_cnt->txnoack,
6666 ((txs->
6667 status & TX_STATUS_FRM_RTX_MASK) >>
6668 TX_STATUS_FRM_RTX_SHIFT));
f4528696 6669 WL_ERROR("%s: INTERMEDIATE but not AMPDU\n", __func__);
0965ae88 6670 return false;
a9533e7e
HP
6671 }
6672
6673 osh = wlc->osh;
6674 queue = txs->frameid & TXFID_QUEUE_MASK;
6675 ASSERT(queue < NFIFO);
6676 if (queue >= NFIFO) {
6677 p = NULL;
6678 goto fatal;
6679 }
6680
6681 p = GETNEXTTXP(wlc, queue);
6682 if (WLC_WAR16165(wlc))
0965ae88 6683 wlc_war16165(wlc, false);
a9533e7e
HP
6684 if (p == NULL)
6685 p = wlc_15420war(wlc, queue);
6686 ASSERT(p != NULL);
6687 if (p == NULL)
6688 goto fatal;
6689
54991ad6 6690 txh = (d11txh_t *) (p->data);
a9533e7e
HP
6691 mcl = ltoh16(txh->MacTxControlLow);
6692
6693 if (txs->phyerr) {
f4528696
JP
6694 WL_ERROR("phyerr 0x%x, rate 0x%x\n",
6695 txs->phyerr, txh->MainRates);
26e80554 6696#if defined(BCMDBG)
a9533e7e 6697 wlc_print_txdesc(txh);
26e80554 6698#endif
a9533e7e
HP
6699 wlc_print_txstatus(txs);
6700 }
6701
6702 ASSERT(txs->frameid == htol16(txh->TxFrameID));
6703 if (txs->frameid != htol16(txh->TxFrameID))
6704 goto fatal;
6705
6706 tx_info = IEEE80211_SKB_CB(p);
3e9796f9
RV
6707 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
6708 fc = ltoh16(h->frame_control);
a9533e7e
HP
6709
6710 scb = (struct scb *)tx_info->control.sta->drv_priv;
6711
6712 if (N_ENAB(wlc->pub)) {
41feb5ed 6713 u8 *plcp = (u8 *) (txh + 1);
a9533e7e 6714 if (PLCP3_ISSGI(plcp[3]))
e4cf544e 6715 wlc->pub->_cnt->txmpdu_sgi++;
a9533e7e 6716 if (PLCP3_ISSTBC(plcp[3]))
e4cf544e 6717 wlc->pub->_cnt->txmpdu_stbc++;
a9533e7e
HP
6718 }
6719
6720 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
6721 ASSERT((mcl & TXC_AMPDU_MASK) != TXC_AMPDU_NONE);
6722 wlc_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
0965ae88 6723 return false;
a9533e7e
HP
6724 }
6725
6726 supr_status = txs->status & TX_STATUS_SUPR_MASK;
6727 if (supr_status == TX_STATUS_SUPR_BADCH)
f4528696
JP
6728 WL_NONE("%s: Pkt tx suppressed, possibly channel %d\n",
6729 __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
a9533e7e
HP
6730
6731 tx_rts = htol16(txh->MacTxControlLow) & TXC_SENDRTS;
6732 tx_frame_count =
6733 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
6734 tx_rts_count =
6735 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
6736
3726ed4d 6737 lastframe = (fc & IEEE80211_FCTL_MOREFRAGS) == 0;
a9533e7e
HP
6738
6739 if (!lastframe) {
f4528696 6740 WL_ERROR("Not last frame!\n");
a9533e7e 6741 } else {
7d4df48e 6742 u16 sfbl, lfbl;
a9533e7e
HP
6743 ieee80211_tx_info_clear_status(tx_info);
6744 if (queue < AC_COUNT) {
6745 sfbl = WLC_WME_RETRY_SFB_GET(wlc, wme_fifo2ac[queue]);
6746 lfbl = WLC_WME_RETRY_LFB_GET(wlc, wme_fifo2ac[queue]);
6747 } else {
6748 sfbl = wlc->SFBL;
6749 lfbl = wlc->LFBL;
6750 }
6751
6752 txrate = tx_info->status.rates;
6753 /* FIXME: this should use a combination of sfbl, lfbl depending on frame length and RTS setting */
6754 if ((tx_frame_count > sfbl) && (txrate[1].idx >= 0)) {
6755 /* rate selection requested a fallback rate and we used it */
6756 txrate->count = lfbl;
6757 txrate[1].count = tx_frame_count - lfbl;
6758 } else {
6759 /* rate selection did not request fallback rate, or we didn't need it */
6760 txrate->count = tx_frame_count;
6761 /* rc80211_minstrel.c:minstrel_tx_status() expects unused rates to be marked with idx = -1 */
6762 txrate[1].idx = -1;
6763 txrate[1].count = 0;
6764 }
6765
6766 /* clear the rest of the rates */
6767 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
6768 txrate[i].idx = -1;
6769 txrate[i].count = 0;
6770 }
6771
6772 if (txs->status & TX_STATUS_ACK_RCV)
6773 tx_info->flags |= IEEE80211_TX_STAT_ACK;
6774 }
6775
6776 totlen = pkttotlen(osh, p);
0f0881b0 6777 free_pdu = true;
a9533e7e
HP
6778
6779 wlc_txfifo_complete(wlc, queue, 1);
6780
6781 if (lastframe) {
54991ad6
AS
6782 p->next = NULL;
6783 p->prev = NULL;
a9533e7e
HP
6784 wlc->txretried = 0;
6785 /* remove PLCP & Broadcom tx descriptor header */
c303ecbd
AS
6786 skb_pull(p, D11_PHY_HDR_LEN);
6787 skb_pull(p, D11_TXH_LEN);
a9533e7e 6788 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
e4cf544e 6789 wlc->pub->_cnt->ieee_tx_status++;
a9533e7e 6790 } else {
f4528696
JP
6791 WL_ERROR("%s: Not last frame => not calling tx_status\n",
6792 __func__);
a9533e7e
HP
6793 }
6794
0965ae88 6795 return false;
a9533e7e
HP
6796
6797 fatal:
6798 ASSERT(0);
6799 if (p)
85385764 6800 pkt_buf_free_skb(osh, p, true);
a9533e7e 6801
0f0881b0 6802 return true;
a9533e7e
HP
6803
6804}
6805
6806void BCMFASTPATH
c6a9e1fc 6807wlc_txfifo_complete(struct wlc_info *wlc, uint fifo, s8 txpktpend)
a9533e7e
HP
6808{
6809 TXPKTPENDDEC(wlc, fifo, txpktpend);
f4528696
JP
6810 WL_TRACE("wlc_txfifo_complete, pktpend dec %d to %d\n",
6811 txpktpend, TXPKTPENDGET(wlc, fifo));
a9533e7e
HP
6812
6813 /* There is more room; mark precedences related to this FIFO sendable */
6814 WLC_TX_FIFO_ENAB(wlc, fifo);
6815 ASSERT(TXPKTPENDGET(wlc, fifo) >= 0);
6816
6817 if (!TXPKTPENDTOT(wlc)) {
6818 if (wlc->block_datafifo & DATA_BLOCK_TX_SUPR)
6819 wlc_bsscfg_tx_check(wlc);
6820 }
6821
6822 /* Clear MHF2_TXBCMC_NOW flag if BCMC fifo has drained */
6823 if (AP_ENAB(wlc->pub) &&
6824 wlc->bcmcfifo_drain && !TXPKTPENDGET(wlc, TX_BCMC_FIFO)) {
0965ae88 6825 wlc->bcmcfifo_drain = false;
a9533e7e
HP
6826 wlc_mhf(wlc, MHF2, MHF2_TXBCMC_NOW, 0, WLC_BAND_AUTO);
6827 }
6828
6829 /* figure out which bsscfg is being worked on... */
6830}
6831
6832/* Given the beacon interval in kus, and a 64 bit TSF in us,
6833 * return the offset (in us) of the TSF from the last TBTT
6834 */
66cbd3ab 6835u32 wlc_calc_tbtt_offset(u32 bp, u32 tsf_h, u32 tsf_l)
a9533e7e 6836{
66cbd3ab 6837 u32 k, btklo, btkhi, offset;
a9533e7e
HP
6838
6839 /* TBTT is always an even multiple of the beacon_interval,
6840 * so the TBTT less than or equal to the beacon timestamp is
6841 * the beacon timestamp minus the beacon timestamp modulo
6842 * the beacon interval.
6843 *
6844 * TBTT = BT - (BT % BIu)
6845 * = (BTk - (BTk % BP)) * 2^10
6846 *
6847 * BT = beacon timestamp (usec, 64bits)
6848 * BTk = beacon timestamp (Kusec, 54bits)
6849 * BP = beacon interval (Kusec, 16bits)
6850 * BIu = BP * 2^10 = beacon interval (usec, 26bits)
6851 *
66cbd3ab 6852 * To keep the calculations in u32s, the modulo operation
a9533e7e
HP
6853 * on the high part of BT needs to be done in parts using the
6854 * relations:
6855 * X*Y mod Z = ((X mod Z) * (Y mod Z)) mod Z
6856 * and
6857 * (X + Y) mod Z = ((X mod Z) + (Y mod Z)) mod Z
6858 *
7d4df48e 6859 * So, if BTk[n] = u16 n [0,3] of BTk.
a9533e7e
HP
6860 * BTk % BP = SUM((BTk[n] * 2^16n) % BP , 0<=n<4) % BP
6861 * and the SUM term can be broken down:
6862 * (BTk[n] * 2^16n) % BP
6863 * (BTk[n] * (2^16n % BP)) % BP
6864 *
6865 * Create a set of power of 2 mod BP constants:
6866 * K[n] = 2^(16n) % BP
6867 * = (K[n-1] * 2^16) % BP
6868 * K[2] = 2^32 % BP = ((2^16 % BP) * 2^16) % BP
6869 *
6870 * BTk % BP = BTk[0-1] % BP +
6871 * (BTk[2] * K[2]) % BP +
6872 * (BTk[3] * K[3]) % BP
6873 *
6874 * Since K[n] < 2^16 and BTk[n] is < 2^16, then BTk[n] * K[n] < 2^32
6875 */
6876
6877 /* BTk = BT >> 10, btklo = BTk[0-3], bkthi = BTk[4-6] */
6878 btklo = (tsf_h << 22) | (tsf_l >> 10);
6879 btkhi = tsf_h >> 10;
6880
6881 /* offset = BTk % BP */
6882 offset = btklo % bp;
6883
6884 /* K[2] = ((2^16 % BP) * 2^16) % BP */
66cbd3ab
GKH
6885 k = (u32) (1 << 16) % bp;
6886 k = (u32) (k * 1 << 16) % (u32) bp;
a9533e7e
HP
6887
6888 /* offset += (BTk[2] * K[2]) % BP */
6889 offset += ((btkhi & 0xffff) * k) % bp;
6890
6891 /* BTk[3] */
6892 btkhi = btkhi >> 16;
6893
6894 /* k[3] = (K[2] * 2^16) % BP */
6895 k = (k << 16) % bp;
6896
6897 /* offset += (BTk[3] * K[3]) % BP */
6898 offset += ((btkhi & 0xffff) * k) % bp;
6899
6900 offset = offset % bp;
6901
6902 /* convert offset from kus to us by shifting up 10 bits and
6903 * add in the low 10 bits of tsf that we ignored
6904 */
6905 offset = (offset << 10) + (tsf_l & 0x3FF);
6906
6907 return offset;
6908}
6909
6910/* Update beacon listen interval in shared memory */
c6a9e1fc 6911void wlc_bcn_li_upd(struct wlc_info *wlc)
a9533e7e
HP
6912{
6913 if (AP_ENAB(wlc->pub))
6914 return;
6915
6916 /* wake up every DTIM is the default */
6917 if (wlc->bcn_li_dtim == 1)
6918 wlc_write_shm(wlc, M_BCN_LI, 0);
6919 else
6920 wlc_write_shm(wlc, M_BCN_LI,
6921 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
6922}
6923
6924static void
c6a9e1fc 6925prep_mac80211_status(struct wlc_info *wlc, d11rxhdr_t *rxh, struct sk_buff *p,
a9533e7e
HP
6926 struct ieee80211_rx_status *rx_status)
6927{
66cbd3ab 6928 u32 tsf_l, tsf_h;
a9533e7e
HP
6929 wlc_d11rxhdr_t *wlc_rxh = (wlc_d11rxhdr_t *) rxh;
6930 int preamble;
6931 int channel;
6932 ratespec_t rspec;
580a0bd9 6933 unsigned char *plcp;
a9533e7e
HP
6934
6935 wlc_read_tsf(wlc, &tsf_l, &tsf_h); /* mactime */
6936 rx_status->mactime = tsf_h;
6937 rx_status->mactime <<= 32;
6938 rx_status->mactime |= tsf_l;
6939 rx_status->flag |= RX_FLAG_TSFT;
6940
6941 channel = WLC_CHAN_CHANNEL(rxh->RxChan);
6942
6943 /* XXX Channel/badn needs to be filtered against whether we are single/dual band card */
6944 if (channel > 14) {
6945 rx_status->band = IEEE80211_BAND_5GHZ;
d69a1358
RV
6946 rx_status->freq = ieee80211_ofdm_chan_to_freq(
6947 WF_CHAN_FACTOR_5_G/2, channel);
6948
a9533e7e
HP
6949 } else {
6950 rx_status->band = IEEE80211_BAND_2GHZ;
1b1d36b6 6951 rx_status->freq = ieee80211_dsss_chan_to_freq(channel);
a9533e7e
HP
6952 }
6953
6954 rx_status->signal = wlc_rxh->rssi; /* signal */
6955
6956 /* noise */
6957 /* qual */
6958 rx_status->antenna = (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0; /* ant */
6959
54991ad6 6960 plcp = p->data;
a9533e7e
HP
6961
6962 rspec = wlc_compute_rspec(rxh, plcp);
6963 if (IS_MCS(rspec)) {
6964 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
6965 rx_status->flag |= RX_FLAG_HT;
6966 if (RSPEC_IS40MHZ(rspec))
6967 rx_status->flag |= RX_FLAG_40MHZ;
6968 } else {
6969 switch (RSPEC2RATE(rspec)) {
6970 case WLC_RATE_1M:
6971 rx_status->rate_idx = 0;
6972 break;
6973 case WLC_RATE_2M:
6974 rx_status->rate_idx = 1;
6975 break;
6976 case WLC_RATE_5M5:
6977 rx_status->rate_idx = 2;
6978 break;
6979 case WLC_RATE_11M:
6980 rx_status->rate_idx = 3;
6981 break;
6982 case WLC_RATE_6M:
6983 rx_status->rate_idx = 4;
6984 break;
6985 case WLC_RATE_9M:
6986 rx_status->rate_idx = 5;
6987 break;
6988 case WLC_RATE_12M:
6989 rx_status->rate_idx = 6;
6990 break;
6991 case WLC_RATE_18M:
6992 rx_status->rate_idx = 7;
6993 break;
6994 case WLC_RATE_24M:
6995 rx_status->rate_idx = 8;
6996 break;
6997 case WLC_RATE_36M:
6998 rx_status->rate_idx = 9;
6999 break;
7000 case WLC_RATE_48M:
7001 rx_status->rate_idx = 10;
7002 break;
7003 case WLC_RATE_54M:
7004 rx_status->rate_idx = 11;
7005 break;
7006 default:
f4528696 7007 WL_ERROR("%s: Unknown rate\n", __func__);
a9533e7e
HP
7008 }
7009
7010 /* Determine short preamble and rate_idx */
7011 preamble = 0;
7012 if (IS_CCK(rspec)) {
7013 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
f4528696 7014 WL_ERROR("Short CCK\n");
a9533e7e
HP
7015 rx_status->flag |= RX_FLAG_SHORTPRE;
7016 } else if (IS_OFDM(rspec)) {
7017 rx_status->flag |= RX_FLAG_SHORTPRE;
7018 } else {
f4528696 7019 WL_ERROR("%s: Unknown modulation\n", __func__);
a9533e7e
HP
7020 }
7021 }
7022
7023 if (PLCP3_ISSGI(plcp[3]))
7024 rx_status->flag |= RX_FLAG_SHORT_GI;
7025
7026 if (rxh->RxStatus1 & RXS_DECERR) {
7027 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
f4528696 7028 WL_ERROR("%s: RX_FLAG_FAILED_PLCP_CRC\n", __func__);
a9533e7e
HP
7029 }
7030 if (rxh->RxStatus1 & RXS_FCSERR) {
7031 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
f4528696 7032 WL_ERROR("%s: RX_FLAG_FAILED_FCS_CRC\n", __func__);
a9533e7e
HP
7033 }
7034}
7035
a9533e7e 7036static void
c6a9e1fc 7037wlc_recvctl(struct wlc_info *wlc, struct osl_info *osh, d11rxhdr_t *rxh,
c26b1378 7038 struct sk_buff *p)
a9533e7e
HP
7039{
7040 int len_mpdu;
7041 struct ieee80211_rx_status rx_status;
7042#if defined(BCMDBG)
7043 struct sk_buff *skb = p;
7044#endif /* BCMDBG */
7045 /* Todo:
7046 * Cache plcp for first MPDU of AMPD and use chacched version for INTERMEDIATE.
7047 * Test for INTERMEDIATE like so:
7048 * if (!(plcp[0] | plcp[1] | plcp[2]))
7049 */
7050
7051 memset(&rx_status, 0, sizeof(rx_status));
7052 prep_mac80211_status(wlc, rxh, p, &rx_status);
7053
7054 /* mac header+body length, exclude CRC and plcp header */
3726ed4d 7055 len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
c303ecbd 7056 skb_pull(p, D11_PHY_HDR_LEN);
2cb8ada6 7057 __skb_trim(p, len_mpdu);
a9533e7e 7058
54991ad6
AS
7059 ASSERT(!(p->next));
7060 ASSERT(!(p->prev));
a9533e7e 7061
f024c48a 7062 ASSERT(IS_ALIGNED((unsigned long)skb->data, 2));
a9533e7e
HP
7063
7064 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7065 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7066
e4cf544e 7067 wlc->pub->_cnt->ieee_rx++;
7a9b48b3 7068 osh->pktalloced--;
a9533e7e
HP
7069 return;
7070}
7071
c6a9e1fc 7072void wlc_bss_list_free(struct wlc_info *wlc, wlc_bss_list_t *bss_list)
a9533e7e
HP
7073{
7074 uint index;
7075 wlc_bss_info_t *bi;
7076
7077 if (!bss_list) {
f4528696 7078 WL_ERROR("%s: Attempting to free NULL list\n", __func__);
a9533e7e
HP
7079 return;
7080 }
7081 /* inspect all BSS descriptor */
7082 for (index = 0; index < bss_list->count; index++) {
7083 bi = bss_list->ptrs[index];
7084 if (bi) {
182acb3c 7085 kfree(bi);
a9533e7e
HP
7086 bss_list->ptrs[index] = NULL;
7087 }
7088 }
7089 bss_list->count = 0;
7090}
7091
7092/* Process received frames */
7093/*
0965ae88 7094 * Return true if more frames need to be processed. false otherwise.
a9533e7e
HP
7095 * Param 'bound' indicates max. # frames to process before break out.
7096 */
7097/* WLC_HIGH_API */
c6a9e1fc 7098void BCMFASTPATH wlc_recv(struct wlc_info *wlc, struct sk_buff *p)
a9533e7e
HP
7099{
7100 d11rxhdr_t *rxh;
3e9796f9 7101 struct ieee80211_hdr *h;
e69284f2 7102 struct osl_info *osh;
7d4df48e 7103 u16 fc;
a9533e7e
HP
7104 uint len;
7105 bool is_amsdu;
a9533e7e 7106
f4528696 7107 WL_TRACE("wl%d: wlc_recv\n", wlc->pub->unit);
a9533e7e
HP
7108
7109 osh = wlc->osh;
7110
7111 /* frame starts with rxhdr */
54991ad6 7112 rxh = (d11rxhdr_t *) (p->data);
a9533e7e
HP
7113
7114 /* strip off rxhdr */
c303ecbd 7115 skb_pull(p, wlc->hwrxoff);
a9533e7e
HP
7116
7117 /* fixup rx header endianness */
7118 ltoh16_buf((void *)rxh, sizeof(d11rxhdr_t));
7119
7120 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7121 if (rxh->RxStatus1 & RXS_PBPRES) {
54991ad6 7122 if (p->len < 2) {
e4cf544e 7123 wlc->pub->_cnt->rxrunt++;
f4528696
JP
7124 WL_ERROR("wl%d: wlc_recv: rcvd runt of len %d\n",
7125 wlc->pub->unit, p->len);
a9533e7e
HP
7126 goto toss;
7127 }
c303ecbd 7128 skb_pull(p, 2);
a9533e7e
HP
7129 }
7130
3e9796f9 7131 h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
54991ad6 7132 len = p->len;
a9533e7e
HP
7133
7134 if (rxh->RxStatus1 & RXS_FCSERR) {
7135 if (wlc->pub->mac80211_state & MAC80211_PROMISC_BCNS) {
f4528696 7136 WL_ERROR("FCSERR while scanning******* - tossing\n");
a9533e7e
HP
7137 goto toss;
7138 } else {
f4528696 7139 WL_ERROR("RCSERR!!!\n");
a9533e7e
HP
7140 goto toss;
7141 }
7142 }
7143
7144 /* check received pkt has at least frame control field */
3e9796f9
RV
7145 if (len >= D11_PHY_HDR_LEN + sizeof(h->frame_control)) {
7146 fc = ltoh16(h->frame_control);
a9533e7e 7147 } else {
e4cf544e 7148 wlc->pub->_cnt->rxrunt++;
a9533e7e
HP
7149 goto toss;
7150 }
7151
7152 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7153
7154 /* explicitly test bad src address to avoid sending bad deauth */
7155 if (!is_amsdu) {
7156 /* CTS and ACK CTL frames are w/o a2 */
d83b2a8a
RV
7157 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
7158 (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3e9796f9
RV
7159 if ((is_zero_ether_addr(h->addr2) ||
7160 is_multicast_ether_addr(h->addr2))) {
d83b2a8a
RV
7161 WL_ERROR("wl%d: %s: dropping a frame with "
7162 "invalid src mac address, a2: %pM\n",
7163 wlc->pub->unit, __func__, h->addr2);
e4cf544e 7164 wlc->pub->_cnt->rxbadsrcmac++;
a9533e7e
HP
7165 goto toss;
7166 }
e4cf544e 7167 wlc->pub->_cnt->rxfrag++;
a9533e7e
HP
7168 }
7169 }
7170
7171 /* due to sheer numbers, toss out probe reqs for now */
d83b2a8a 7172 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
a9533e7e
HP
7173 if ((fc & FC_KIND_MASK) == FC_PROBE_REQ)
7174 goto toss;
7175 }
7176
7177 if (is_amsdu) {
f4528696 7178 WL_ERROR("%s: is_amsdu causing toss\n", __func__);
a9533e7e
HP
7179 goto toss;
7180 }
7181
7182 wlc_recvctl(wlc, osh, rxh, p);
7183 return;
7184
7185 toss:
85385764 7186 pkt_buf_free_skb(osh, p, false);
a9533e7e
HP
7187}
7188
7189/* calculate frame duration for Mixed-mode L-SIG spoofing, return
7190 * number of bytes goes in the length field
7191 *
7192 * Formula given by HT PHY Spec v 1.13
7193 * len = 3(nsyms + nstream + 3) - 3
7194 */
7d4df48e 7195u16 BCMFASTPATH
c6a9e1fc 7196wlc_calc_lsig_len(struct wlc_info *wlc, ratespec_t ratespec, uint mac_len)
a9533e7e
HP
7197{
7198 uint nsyms, len = 0, kNdps;
7199
f4528696
JP
7200 WL_TRACE("wl%d: wlc_calc_lsig_len: rate %d, len%d\n",
7201 wlc->pub->unit, RSPEC2RATE(ratespec), mac_len);
a9533e7e
HP
7202
7203 if (IS_MCS(ratespec)) {
7204 uint mcs = ratespec & RSPEC_RATE_MASK;
7205 /* MCS_TXS(mcs) returns num tx streams - 1 */
7206 int tot_streams = (MCS_TXS(mcs) + 1) + RSPEC_STC(ratespec);
7207
7208 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7209 /* the payload duration calculation matches that of regular ofdm */
7210 /* 1000Ndbps = kbps * 4 */
7211 kNdps =
7212 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7213 RSPEC_ISSGI(ratespec)) * 4;
7214
7215 if (RSPEC_STC(ratespec) == 0)
7216 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7217 nsyms =
7218 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7219 APHY_TAIL_NBITS) * 1000, kNdps);
7220 else
7221 /* STBC needs to have even number of symbols */
7222 nsyms =
7223 2 *
7224 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7225 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7226
7227 nsyms += (tot_streams + 3); /* (+3) account for HT-SIG(2) and HT-STF(1) */
7228 /* 3 bytes/symbol @ legacy 6Mbps rate */
7229 len = (3 * nsyms) - 3; /* (-3) excluding service bits and tail bits */
7230 }
7231
7d4df48e 7232 return (u16) len;
a9533e7e
HP
7233}
7234
7235/* calculate frame duration of a given rate and length, return time in usec unit */
7236uint BCMFASTPATH
c6a9e1fc 7237wlc_calc_frame_time(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
a9533e7e
HP
7238 uint mac_len)
7239{
7240 uint nsyms, dur = 0, Ndps, kNdps;
7241 uint rate = RSPEC2RATE(ratespec);
7242
7243 if (rate == 0) {
7244 ASSERT(0);
f4528696 7245 WL_ERROR("wl%d: WAR: using rate of 1 mbps\n", wlc->pub->unit);
a9533e7e
HP
7246 rate = WLC_RATE_1M;
7247 }
7248
f4528696
JP
7249 WL_TRACE("wl%d: wlc_calc_frame_time: rspec 0x%x, preamble_type %d, len%d\n",
7250 wlc->pub->unit, ratespec, preamble_type, mac_len);
a9533e7e
HP
7251
7252 if (IS_MCS(ratespec)) {
7253 uint mcs = ratespec & RSPEC_RATE_MASK;
7254 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7255 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7256 ASSERT(WLC_IS_MIMO_PREAMBLE(preamble_type));
7257
7258 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7259 if (preamble_type == WLC_MM_PREAMBLE)
7260 dur += PREN_MM_EXT;
7261 /* 1000Ndbps = kbps * 4 */
7262 kNdps =
7263 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7264 RSPEC_ISSGI(ratespec)) * 4;
7265
7266 if (RSPEC_STC(ratespec) == 0)
7267 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7268 nsyms =
7269 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7270 APHY_TAIL_NBITS) * 1000, kNdps);
7271 else
7272 /* STBC needs to have even number of symbols */
7273 nsyms =
7274 2 *
7275 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7276 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7277
7278 dur += APHY_SYMBOL_TIME * nsyms;
7279 if (BAND_2G(wlc->band->bandtype))
7280 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7281 } else if (IS_OFDM(rate)) {
7282 dur = APHY_PREAMBLE_TIME;
7283 dur += APHY_SIGNAL_TIME;
7284 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7285 Ndps = rate * 2;
7286 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7287 nsyms =
7288 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
7289 Ndps);
7290 dur += APHY_SYMBOL_TIME * nsyms;
7291 if (BAND_2G(wlc->band->bandtype))
7292 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7293 } else {
7294 /* calc # bits * 2 so factor of 2 in rate (1/2 mbps) will divide out */
7295 mac_len = mac_len * 8 * 2;
7296 /* calc ceiling of bits/rate = microseconds of air time */
7297 dur = (mac_len + rate - 1) / rate;
7298 if (preamble_type & WLC_SHORT_PREAMBLE)
7299 dur += BPHY_PLCP_SHORT_TIME;
7300 else
7301 dur += BPHY_PLCP_TIME;
7302 }
7303 return dur;
7304}
7305
7306/* The opposite of wlc_calc_frame_time */
7307static uint
c6a9e1fc 7308wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
a9533e7e
HP
7309 uint dur)
7310{
7311 uint nsyms, mac_len, Ndps, kNdps;
7312 uint rate = RSPEC2RATE(ratespec);
7313
f4528696
JP
7314 WL_TRACE("wl%d: wlc_calc_frame_len: rspec 0x%x, preamble_type %d, dur %d\n",
7315 wlc->pub->unit, ratespec, preamble_type, dur);
a9533e7e
HP
7316
7317 if (IS_MCS(ratespec)) {
7318 uint mcs = ratespec & RSPEC_RATE_MASK;
7319 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7320 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7321 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7322 /* payload calculation matches that of regular ofdm */
7323 if (BAND_2G(wlc->band->bandtype))
7324 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
7325 /* kNdbps = kbps * 4 */
7326 kNdps =
7327 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7328 RSPEC_ISSGI(ratespec)) * 4;
7329 nsyms = dur / APHY_SYMBOL_TIME;
7330 mac_len =
7331 ((nsyms * kNdps) -
7332 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
7333 } else if (IS_OFDM(ratespec)) {
7334 dur -= APHY_PREAMBLE_TIME;
7335 dur -= APHY_SIGNAL_TIME;
7336 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7337 Ndps = rate * 2;
7338 nsyms = dur / APHY_SYMBOL_TIME;
7339 mac_len =
7340 ((nsyms * Ndps) -
7341 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
7342 } else {
7343 if (preamble_type & WLC_SHORT_PREAMBLE)
7344 dur -= BPHY_PLCP_SHORT_TIME;
7345 else
7346 dur -= BPHY_PLCP_TIME;
7347 mac_len = dur * rate;
7348 /* divide out factor of 2 in rate (1/2 mbps) */
7349 mac_len = mac_len / 8 / 2;
7350 }
7351 return mac_len;
7352}
7353
7354static uint
c6a9e1fc 7355wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
a9533e7e 7356{
f4528696
JP
7357 WL_TRACE("wl%d: wlc_calc_ba_time: rspec 0x%x, preamble_type %d\n",
7358 wlc->pub->unit, rspec, preamble_type);
a9533e7e
HP
7359 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7360 * or equal to the rate of the immediately previous frame in the FES
7361 */
7362 rspec = WLC_BASIC_RATE(wlc, rspec);
7363 ASSERT(VALID_RATE_DBG(wlc, rspec));
7364
7365 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
7366 return wlc_calc_frame_time(wlc, rspec, preamble_type,
7367 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
3726ed4d 7368 FCS_LEN));
a9533e7e
HP
7369}
7370
7371static uint BCMFASTPATH
c6a9e1fc 7372wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
a9533e7e
HP
7373{
7374 uint dur = 0;
7375
f4528696
JP
7376 WL_TRACE("wl%d: wlc_calc_ack_time: rspec 0x%x, preamble_type %d\n",
7377 wlc->pub->unit, rspec, preamble_type);
a9533e7e
HP
7378 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7379 * or equal to the rate of the immediately previous frame in the FES
7380 */
7381 rspec = WLC_BASIC_RATE(wlc, rspec);
7382 ASSERT(VALID_RATE_DBG(wlc, rspec));
7383
7384 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
7385 dur =
7386 wlc_calc_frame_time(wlc, rspec, preamble_type,
3726ed4d 7387 (DOT11_ACK_LEN + FCS_LEN));
a9533e7e
HP
7388 return dur;
7389}
7390
7391static uint
c6a9e1fc 7392wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
a9533e7e 7393{
f4528696
JP
7394 WL_TRACE("wl%d: wlc_calc_cts_time: ratespec 0x%x, preamble_type %d\n",
7395 wlc->pub->unit, rspec, preamble_type);
a9533e7e
HP
7396 return wlc_calc_ack_time(wlc, rspec, preamble_type);
7397}
7398
7399/* derive wlc->band->basic_rate[] table from 'rateset' */
c6a9e1fc 7400void wlc_rate_lookup_init(struct wlc_info *wlc, wlc_rateset_t *rateset)
a9533e7e 7401{
41feb5ed
GKH
7402 u8 rate;
7403 u8 mandatory;
7404 u8 cck_basic = 0;
7405 u8 ofdm_basic = 0;
7406 u8 *br = wlc->band->basic_rate;
a9533e7e
HP
7407 uint i;
7408
7409 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
9249ede9 7410 memset(br, 0, WLC_MAXRATE + 1);
a9533e7e
HP
7411
7412 /* For each basic rate in the rates list, make an entry in the
7413 * best basic lookup.
7414 */
7415 for (i = 0; i < rateset->count; i++) {
7416 /* only make an entry for a basic rate */
7417 if (!(rateset->rates[i] & WLC_RATE_FLAG))
7418 continue;
7419
7420 /* mask off basic bit */
7421 rate = (rateset->rates[i] & RATE_MASK);
7422
7423 if (rate > WLC_MAXRATE) {
f4528696
JP
7424 WL_ERROR("wlc_rate_lookup_init: invalid rate 0x%X in rate set\n",
7425 rateset->rates[i]);
a9533e7e
HP
7426 continue;
7427 }
7428
7429 br[rate] = rate;
7430 }
7431
7432 /* The rate lookup table now has non-zero entries for each
7433 * basic rate, equal to the basic rate: br[basicN] = basicN
7434 *
7435 * To look up the best basic rate corresponding to any
7436 * particular rate, code can use the basic_rate table
7437 * like this
7438 *
7439 * basic_rate = wlc->band->basic_rate[tx_rate]
7440 *
7441 * Make sure there is a best basic rate entry for
7442 * every rate by walking up the table from low rates
7443 * to high, filling in holes in the lookup table
7444 */
7445
7446 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
7447 rate = wlc->band->hw_rateset.rates[i];
7448 ASSERT(rate <= WLC_MAXRATE);
7449
7450 if (br[rate] != 0) {
7451 /* This rate is a basic rate.
7452 * Keep track of the best basic rate so far by
7453 * modulation type.
7454 */
7455 if (IS_OFDM(rate))
7456 ofdm_basic = rate;
7457 else
7458 cck_basic = rate;
7459
7460 continue;
7461 }
7462
7463 /* This rate is not a basic rate so figure out the
7464 * best basic rate less than this rate and fill in
7465 * the hole in the table
7466 */
7467
7468 br[rate] = IS_OFDM(rate) ? ofdm_basic : cck_basic;
7469
7470 if (br[rate] != 0)
7471 continue;
7472
7473 if (IS_OFDM(rate)) {
7474 /* In 11g and 11a, the OFDM mandatory rates are 6, 12, and 24 Mbps */
7475 if (rate >= WLC_RATE_24M)
7476 mandatory = WLC_RATE_24M;
7477 else if (rate >= WLC_RATE_12M)
7478 mandatory = WLC_RATE_12M;
7479 else
7480 mandatory = WLC_RATE_6M;
7481 } else {
7482 /* In 11b, all the CCK rates are mandatory 1 - 11 Mbps */
7483 mandatory = rate;
7484 }
7485
7486 br[rate] = mandatory;
7487 }
7488}
7489
c6a9e1fc 7490static void wlc_write_rate_shm(struct wlc_info *wlc, u8 rate, u8 basic_rate)
a9533e7e 7491{
41feb5ed
GKH
7492 u8 phy_rate, index;
7493 u8 basic_phy_rate, basic_index;
7d4df48e
GKH
7494 u16 dir_table, basic_table;
7495 u16 basic_ptr;
a9533e7e
HP
7496
7497 /* Shared memory address for the table we are reading */
7498 dir_table = IS_OFDM(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
7499
7500 /* Shared memory address for the table we are writing */
7501 basic_table = IS_OFDM(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
7502
7503 /*
7504 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
7505 * the index into the rate table.
7506 */
7507 phy_rate = rate_info[rate] & RATE_MASK;
7508 basic_phy_rate = rate_info[basic_rate] & RATE_MASK;
7509 index = phy_rate & 0xf;
7510 basic_index = basic_phy_rate & 0xf;
7511
7512 /* Find the SHM pointer to the ACK rate entry by looking in the
7513 * Direct-map Table
7514 */
7515 basic_ptr = wlc_read_shm(wlc, (dir_table + basic_index * 2));
7516
7517 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
7518 * to the correct basic rate for the given incoming rate
7519 */
7520 wlc_write_shm(wlc, (basic_table + index * 2), basic_ptr);
7521}
7522
c6a9e1fc 7523static const wlc_rateset_t *wlc_rateset_get_hwrs(struct wlc_info *wlc)
a9533e7e
HP
7524{
7525 const wlc_rateset_t *rs_dflt;
7526
7527 if (WLC_PHY_11N_CAP(wlc->band)) {
7528 if (BAND_5G(wlc->band->bandtype))
7529 rs_dflt = &ofdm_mimo_rates;
7530 else
7531 rs_dflt = &cck_ofdm_mimo_rates;
7532 } else if (wlc->band->gmode)
7533 rs_dflt = &cck_ofdm_rates;
7534 else
7535 rs_dflt = &cck_rates;
7536
7537 return rs_dflt;
7538}
7539
c6a9e1fc 7540void wlc_set_ratetable(struct wlc_info *wlc)
a9533e7e
HP
7541{
7542 const wlc_rateset_t *rs_dflt;
7543 wlc_rateset_t rs;
41feb5ed 7544 u8 rate, basic_rate;
a9533e7e
HP
7545 uint i;
7546
7547 rs_dflt = wlc_rateset_get_hwrs(wlc);
7548 ASSERT(rs_dflt != NULL);
7549
7550 wlc_rateset_copy(rs_dflt, &rs);
7551 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7552
7553 /* walk the phy rate table and update SHM basic rate lookup table */
7554 for (i = 0; i < rs.count; i++) {
7555 rate = rs.rates[i] & RATE_MASK;
7556
7557 /* for a given rate WLC_BASIC_RATE returns the rate at
7558 * which a response ACK/CTS should be sent.
7559 */
7560 basic_rate = WLC_BASIC_RATE(wlc, rate);
7561 if (basic_rate == 0) {
7562 /* This should only happen if we are using a
7563 * restricted rateset.
7564 */
7565 basic_rate = rs.rates[0] & RATE_MASK;
7566 }
7567
7568 wlc_write_rate_shm(wlc, rate, basic_rate);
7569 }
7570}
7571
7572/*
7573 * Return true if the specified rate is supported by the specified band.
7574 * WLC_BAND_AUTO indicates the current band.
7575 */
c6a9e1fc
RV
7576bool wlc_valid_rate(struct wlc_info *wlc, ratespec_t rspec, int band,
7577 bool verbose)
a9533e7e
HP
7578{
7579 wlc_rateset_t *hw_rateset;
7580 uint i;
7581
7582 if ((band == WLC_BAND_AUTO) || (band == wlc->band->bandtype)) {
7583 hw_rateset = &wlc->band->hw_rateset;
7584 } else if (NBANDS(wlc) > 1) {
7585 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
7586 } else {
7587 /* other band specified and we are a single band device */
0965ae88 7588 return false;
a9533e7e
HP
7589 }
7590
7591 /* check if this is a mimo rate */
7592 if (IS_MCS(rspec)) {
7593 if (!VALID_MCS((rspec & RSPEC_RATE_MASK)))
7594 goto error;
7595
7596 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
7597 }
7598
7599 for (i = 0; i < hw_rateset->count; i++)
7600 if (hw_rateset->rates[i] == RSPEC2RATE(rspec))
0f0881b0 7601 return true;
a9533e7e
HP
7602 error:
7603 if (verbose) {
f4528696
JP
7604 WL_ERROR("wl%d: wlc_valid_rate: rate spec 0x%x not in hw_rateset\n",
7605 wlc->pub->unit, rspec);
a9533e7e
HP
7606 }
7607
0965ae88 7608 return false;
a9533e7e
HP
7609}
7610
c6a9e1fc 7611static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap)
a9533e7e
HP
7612{
7613 uint i;
f077f718 7614 struct wlcband *band;
a9533e7e
HP
7615
7616 for (i = 0; i < NBANDS(wlc); i++) {
7617 if (IS_SINGLEBAND_5G(wlc->deviceid))
7618 i = BAND_5G_INDEX;
7619 band = wlc->bandstate[i];
7620 if (band->bandtype == WLC_BAND_5G) {
7621 if ((bwcap == WLC_N_BW_40ALL)
7622 || (bwcap == WLC_N_BW_20IN2G_40IN5G))
0f0881b0 7623 band->mimo_cap_40 = true;
a9533e7e 7624 else
0965ae88 7625 band->mimo_cap_40 = false;
a9533e7e
HP
7626 } else {
7627 ASSERT(band->bandtype == WLC_BAND_2G);
7628 if (bwcap == WLC_N_BW_40ALL)
0f0881b0 7629 band->mimo_cap_40 = true;
a9533e7e 7630 else
0965ae88 7631 band->mimo_cap_40 = false;
a9533e7e
HP
7632 }
7633 }
7634
7635 wlc->mimo_band_bwcap = bwcap;
7636}
7637
c6a9e1fc 7638void wlc_mod_prb_rsp_rate_table(struct wlc_info *wlc, uint frame_len)
a9533e7e
HP
7639{
7640 const wlc_rateset_t *rs_dflt;
7641 wlc_rateset_t rs;
41feb5ed 7642 u8 rate;
7d4df48e 7643 u16 entry_ptr;
41feb5ed 7644 u8 plcp[D11_PHY_HDR_LEN];
7d4df48e 7645 u16 dur, sifs;
a9533e7e
HP
7646 uint i;
7647
7648 sifs = SIFS(wlc->band);
7649
7650 rs_dflt = wlc_rateset_get_hwrs(wlc);
7651 ASSERT(rs_dflt != NULL);
7652
7653 wlc_rateset_copy(rs_dflt, &rs);
7654 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7655
7656 /* walk the phy rate table and update MAC core SHM basic rate table entries */
7657 for (i = 0; i < rs.count; i++) {
7658 rate = rs.rates[i] & RATE_MASK;
7659
7660 entry_ptr = wlc_rate_shm_offset(wlc, rate);
7661
7662 /* Calculate the Probe Response PLCP for the given rate */
7663 wlc_compute_plcp(wlc, rate, frame_len, plcp);
7664
7665 /* Calculate the duration of the Probe Response frame plus SIFS for the MAC */
7666 dur =
7d4df48e 7667 (u16) wlc_calc_frame_time(wlc, rate, WLC_LONG_PREAMBLE,
a9533e7e
HP
7668 frame_len);
7669 dur += sifs;
7670
7671 /* Update the SHM Rate Table entry Probe Response values */
7672 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS,
7d4df48e 7673 (u16) (plcp[0] + (plcp[1] << 8)));
a9533e7e 7674 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7d4df48e 7675 (u16) (plcp[2] + (plcp[3] << 8)));
a9533e7e
HP
7676 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_DUR_POS, dur);
7677 }
7678}
7679
7d4df48e 7680u16
c6a9e1fc
RV
7681wlc_compute_bcntsfoff(struct wlc_info *wlc, ratespec_t rspec,
7682 bool short_preamble, bool phydelay)
a9533e7e
HP
7683{
7684 uint bcntsfoff = 0;
7685
7686 if (IS_MCS(rspec)) {
f4528696
JP
7687 WL_ERROR("wl%d: recd beacon with mcs rate; rspec 0x%x\n",
7688 wlc->pub->unit, rspec);
a9533e7e
HP
7689 } else if (IS_OFDM(rspec)) {
7690 /* tx delay from MAC through phy to air (2.1 usec) +
7691 * phy header time (preamble + PLCP SIGNAL == 20 usec) +
7692 * PLCP SERVICE + MAC header time (SERVICE + FC + DUR + A1 + A2 + A3 + SEQ == 26
7693 * bytes at beacon rate)
7694 */
7695 bcntsfoff += phydelay ? D11A_PHY_TX_DELAY : 0;
7696 bcntsfoff += APHY_PREAMBLE_TIME + APHY_SIGNAL_TIME;
7697 bcntsfoff +=
7698 wlc_compute_airtime(wlc, rspec,
7699 APHY_SERVICE_NBITS / 8 +
7700 DOT11_MAC_HDR_LEN);
7701 } else {
7702 /* tx delay from MAC through phy to air (3.4 usec) +
7703 * phy header time (long preamble + PLCP == 192 usec) +
7704 * MAC header time (FC + DUR + A1 + A2 + A3 + SEQ == 24 bytes at beacon rate)
7705 */
7706 bcntsfoff += phydelay ? D11B_PHY_TX_DELAY : 0;
7707 bcntsfoff +=
7708 short_preamble ? D11B_PHY_SPREHDR_TIME :
7709 D11B_PHY_LPREHDR_TIME;
7710 bcntsfoff += wlc_compute_airtime(wlc, rspec, DOT11_MAC_HDR_LEN);
7711 }
7d4df48e 7712 return (u16) (bcntsfoff);
a9533e7e
HP
7713}
7714
7715/* Max buffering needed for beacon template/prb resp template is 142 bytes.
7716 *
7717 * PLCP header is 6 bytes.
7718 * 802.11 A3 header is 24 bytes.
7719 * Max beacon frame body template length is 112 bytes.
7720 * Max probe resp frame body template length is 110 bytes.
7721 *
7722 * *len on input contains the max length of the packet available.
7723 *
7724 * The *len value is set to the number of bytes in buf used, and starts with the PLCP
7725 * and included up to, but not including, the 4 byte FCS.
7726 */
7727static void
c6a9e1fc 7728wlc_bcn_prb_template(struct wlc_info *wlc, uint type, ratespec_t bcn_rspec,
7d4df48e 7729 wlc_bsscfg_t *cfg, u16 *buf, int *len)
a9533e7e 7730{
a44d4236 7731 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
a9533e7e 7732 cck_phy_hdr_t *plcp;
392308d0 7733 struct ieee80211_mgmt *h;
a9533e7e
HP
7734 int hdr_len, body_len;
7735
7736 ASSERT(*len >= 142);
7737 ASSERT(type == FC_BEACON || type == FC_PROBE_RESP);
7738
7739 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
7740 hdr_len = DOT11_MAC_HDR_LEN;
7741 else
7742 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7743 body_len = *len - hdr_len; /* calc buffer size provided for frame body */
7744
7745 *len = hdr_len + body_len; /* return actual size */
7746
7747 /* format PHY and MAC headers */
9249ede9 7748 memset((char *)buf, 0, hdr_len);
a9533e7e
HP
7749
7750 plcp = (cck_phy_hdr_t *) buf;
7751
7752 /* PLCP for Probe Response frames are filled in from core's rate table */
7753 if (type == FC_BEACON && !MBSS_BCN_ENAB(cfg)) {
7754 /* fill in PLCP */
7755 wlc_compute_plcp(wlc, bcn_rspec,
3726ed4d 7756 (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
41feb5ed 7757 (u8 *) plcp);
a9533e7e
HP
7758
7759 }
7760 /* "Regular" and 16 MBSS but not for 4 MBSS */
7761 /* Update the phytxctl for the beacon based on the rspec */
7762 if (!SOFTBCN_ENAB(cfg))
7763 wlc_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7764
7765 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
392308d0 7766 h = (struct ieee80211_mgmt *)&plcp[0];
a9533e7e 7767 else
392308d0 7768 h = (struct ieee80211_mgmt *)&plcp[1];
a9533e7e
HP
7769
7770 /* fill in 802.11 header */
392308d0 7771 h->frame_control = htol16((u16) type);
a9533e7e
HP
7772
7773 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7774 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7775 if (type == FC_BEACON)
7776 bcopy((const char *)&ether_bcast, (char *)&h->da,
b8d63078
JP
7777 ETH_ALEN);
7778 bcopy((char *)&cfg->cur_etheraddr, (char *)&h->sa, ETH_ALEN);
7779 bcopy((char *)&cfg->BSSID, (char *)&h->bssid, ETH_ALEN);
a9533e7e
HP
7780
7781 /* SEQ filled in by MAC */
7782
7783 return;
7784}
7785
7786int wlc_get_header_len()
7787{
7788 return TXOFF;
7789}
7790
7791/* Update a beacon for a particular BSS
7792 * For MBSS, this updates the software template and sets "latest" to the index of the
7793 * template updated.
7794 * Otherwise, it updates the hardware template.
7795 */
c6a9e1fc 7796void wlc_bss_update_beacon(struct wlc_info *wlc, wlc_bsscfg_t *cfg)
a9533e7e
HP
7797{
7798 int len = BCN_TMPL_LEN;
7799
7800 /* Clear the soft intmask */
7801 wlc->defmacintmask &= ~MI_BCNTPL;
7802
7803 if (!cfg->up) { /* Only allow updates on an UP bss */
7804 return;
7805 }
7806
7807 if (MBSS_BCN_ENAB(cfg)) { /* Optimize: Some of if/else could be combined */
7808 } else if (HWBCN_ENAB(cfg)) { /* Hardware beaconing for this config */
7d4df48e 7809 u16 bcn[BCN_TMPL_LEN / 2];
66cbd3ab 7810 u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
a9533e7e 7811 d11regs_t *regs = wlc->regs;
e69284f2 7812 struct osl_info *osh = NULL;
a9533e7e
HP
7813
7814 osh = wlc->osh;
7815
7816 /* Check if both templates are in use, if so sched. an interrupt
7817 * that will call back into this routine
7818 */
7819 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7820 /* clear any previous status */
7821 W_REG(osh, &regs->macintstatus, MI_BCNTPL);
7822 }
7823 /* Check that after scheduling the interrupt both of the
7824 * templates are still busy. if not clear the int. & remask
7825 */
7826 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7827 wlc->defmacintmask |= MI_BCNTPL;
7828 return;
7829 }
7830
7831 wlc->bcn_rspec =
7832 wlc_lowest_basic_rspec(wlc, &cfg->current_bss->rateset);
7833 ASSERT(wlc_valid_rate
7834 (wlc, wlc->bcn_rspec,
7835 CHSPEC_IS2G(cfg->current_bss->
7836 chanspec) ? WLC_BAND_2G : WLC_BAND_5G,
0f0881b0 7837 true));
a9533e7e
HP
7838
7839 /* update the template and ucode shm */
7840 wlc_bcn_prb_template(wlc, FC_BEACON, wlc->bcn_rspec, cfg, bcn,
7841 &len);
0965ae88 7842 wlc_write_hw_bcntemplates(wlc, bcn, len, false);
a9533e7e
HP
7843 }
7844}
7845
7846/*
7847 * Update all beacons for the system.
7848 */
c6a9e1fc 7849void wlc_update_beacon(struct wlc_info *wlc)
a9533e7e
HP
7850{
7851 int idx;
7852 wlc_bsscfg_t *bsscfg;
7853
7854 /* update AP or IBSS beacons */
7855 FOREACH_BSS(wlc, idx, bsscfg) {
7856 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7857 wlc_bss_update_beacon(wlc, bsscfg);
7858 }
7859}
7860
7861/* Write ssid into shared memory */
c6a9e1fc 7862void wlc_shm_ssid_upd(struct wlc_info *wlc, wlc_bsscfg_t *cfg)
a9533e7e 7863{
41feb5ed 7864 u8 *ssidptr = cfg->SSID;
7d4df48e 7865 u16 base = M_SSID;
3726ed4d 7866 u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
a9533e7e
HP
7867
7868 /* padding the ssid with zero and copy it into shm */
3726ed4d 7869 memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
a9533e7e
HP
7870 bcopy(ssidptr, ssidbuf, cfg->SSID_len);
7871
3726ed4d 7872 wlc_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
a9533e7e
HP
7873
7874 if (!MBSS_BCN_ENAB(cfg))
7d4df48e 7875 wlc_write_shm(wlc, M_SSIDLEN, (u16) cfg->SSID_len);
a9533e7e
HP
7876}
7877
c6a9e1fc 7878void wlc_update_probe_resp(struct wlc_info *wlc, bool suspend)
a9533e7e
HP
7879{
7880 int idx;
7881 wlc_bsscfg_t *bsscfg;
7882
7883 /* update AP or IBSS probe responses */
7884 FOREACH_BSS(wlc, idx, bsscfg) {
7885 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7886 wlc_bss_update_probe_resp(wlc, bsscfg, suspend);
7887 }
7888}
7889
7890void
c6a9e1fc 7891wlc_bss_update_probe_resp(struct wlc_info *wlc, wlc_bsscfg_t *cfg, bool suspend)
a9533e7e 7892{
7d4df48e 7893 u16 prb_resp[BCN_TMPL_LEN / 2];
a9533e7e
HP
7894 int len = BCN_TMPL_LEN;
7895
7896 /* write the probe response to hardware, or save in the config structure */
7897 if (!MBSS_PRB_ENAB(cfg)) {
7898
7899 /* create the probe response template */
7900 wlc_bcn_prb_template(wlc, FC_PROBE_RESP, 0, cfg, prb_resp,
7901 &len);
7902
7903 if (suspend)
7904 wlc_suspend_mac_and_wait(wlc);
7905
7906 /* write the probe response into the template region */
7907 wlc_bmac_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7908 (len + 3) & ~3, prb_resp);
7909
7910 /* write the length of the probe response frame (+PLCP/-FCS) */
7d4df48e 7911 wlc_write_shm(wlc, M_PRB_RESP_FRM_LEN, (u16) len);
a9533e7e
HP
7912
7913 /* write the SSID and SSID length */
7914 wlc_shm_ssid_upd(wlc, cfg);
7915
7916 /*
7917 * Write PLCP headers and durations for probe response frames at all rates.
7918 * Use the actual frame length covered by the PLCP header for the call to
7919 * wlc_mod_prb_rsp_rate_table() by subtracting the PLCP len and adding the FCS.
7920 */
3726ed4d 7921 len += (-D11_PHY_HDR_LEN + FCS_LEN);
7d4df48e 7922 wlc_mod_prb_rsp_rate_table(wlc, (u16) len);
a9533e7e
HP
7923
7924 if (suspend)
7925 wlc_enable_mac(wlc);
7926 } else { /* Generating probe resp in sw; update local template */
7927 ASSERT(0 && "No software probe response support without MBSS");
7928 }
7929}
7930
7931/* prepares pdu for transmission. returns BCM error codes */
c6a9e1fc 7932int wlc_prep_pdu(struct wlc_info *wlc, struct sk_buff *pdu, uint *fifop)
a9533e7e 7933{
e69284f2 7934 struct osl_info *osh;
a9533e7e
HP
7935 uint fifo;
7936 d11txh_t *txh;
3e9796f9 7937 struct ieee80211_hdr *h;
a9533e7e 7938 struct scb *scb;
7d4df48e 7939 u16 fc;
a9533e7e
HP
7940
7941 osh = wlc->osh;
7942
7943 ASSERT(pdu);
54991ad6 7944 txh = (d11txh_t *) (pdu->data);
a9533e7e 7945 ASSERT(txh);
3e9796f9 7946 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
a9533e7e 7947 ASSERT(h);
3e9796f9 7948 fc = ltoh16(h->frame_control);
a9533e7e
HP
7949
7950 /* get the pkt queue info. This was put at wlc_sendctl or wlc_send for PDU */
7951 fifo = ltoh16(txh->TxFrameID) & TXFID_QUEUE_MASK;
7952
7953 scb = NULL;
7954
7955 *fifop = fifo;
7956
7957 /* return if insufficient dma resources */
7958 if (TXAVAIL(wlc, fifo) < MAX_DMA_SEGS) {
7959 /* Mark precedences related to this FIFO, unsendable */
7960 WLC_TX_FIFO_CLEAR(wlc, fifo);
7961 return BCME_BUSY;
7962 }
7963
d83b2a8a
RV
7964 if ((ltoh16(txh->MacFrameControl) & IEEE80211_FCTL_FTYPE) !=
7965 IEEE80211_FTYPE_DATA)
e4cf544e 7966 wlc->pub->_cnt->txctl++;
a9533e7e
HP
7967
7968 return 0;
7969}
7970
7971/* init tx reported rate mechanism */
c6a9e1fc 7972void wlc_reprate_init(struct wlc_info *wlc)
a9533e7e
HP
7973{
7974 int i;
7975 wlc_bsscfg_t *bsscfg;
7976
7977 FOREACH_BSS(wlc, i, bsscfg) {
7978 wlc_bsscfg_reprate_init(bsscfg);
7979 }
7980}
7981
7982/* per bsscfg init tx reported rate mechanism */
7cc4a4c0 7983void wlc_bsscfg_reprate_init(wlc_bsscfg_t *bsscfg)
a9533e7e
HP
7984{
7985 bsscfg->txrspecidx = 0;
9249ede9 7986 memset((char *)bsscfg->txrspec, 0, sizeof(bsscfg->txrspec));
a9533e7e
HP
7987}
7988
7989/* Retrieve a consolidated set of revision information,
7990 * typically for the WLC_GET_REVINFO ioctl
7991 */
c6a9e1fc 7992int wlc_get_revision_info(struct wlc_info *wlc, void *buf, uint len)
a9533e7e
HP
7993{
7994 wlc_rev_info_t *rinfo = (wlc_rev_info_t *) buf;
7995
7996 if (len < WL_REV_INFO_LEGACY_LENGTH)
7997 return BCME_BUFTOOSHORT;
7998
7999 rinfo->vendorid = wlc->vendorid;
8000 rinfo->deviceid = wlc->deviceid;
8001 rinfo->radiorev = (wlc->band->radiorev << IDCODE_REV_SHIFT) |
8002 (wlc->band->radioid << IDCODE_ID_SHIFT);
8003 rinfo->chiprev = wlc->pub->sih->chiprev;
8004 rinfo->corerev = wlc->pub->corerev;
8005 rinfo->boardid = wlc->pub->sih->boardtype;
8006 rinfo->boardvendor = wlc->pub->sih->boardvendor;
8007 rinfo->boardrev = wlc->pub->boardrev;
8008 rinfo->ucoderev = wlc->ucode_rev;
8009 rinfo->driverrev = EPI_VERSION_NUM;
8010 rinfo->bus = wlc->pub->sih->bustype;
8011 rinfo->chipnum = wlc->pub->sih->chip;
8012
ce0f1b8c 8013 if (len >= (offsetof(wlc_rev_info_t, chippkg))) {
a9533e7e
HP
8014 rinfo->phytype = wlc->band->phytype;
8015 rinfo->phyrev = wlc->band->phyrev;
8016 rinfo->anarev = 0; /* obsolete stuff, suppress */
8017 }
8018
8019 if (len >= sizeof(*rinfo)) {
8020 rinfo->chippkg = wlc->pub->sih->chippkg;
8021 }
8022
8023 return BCME_OK;
8024}
8025
c6a9e1fc 8026void wlc_default_rateset(struct wlc_info *wlc, wlc_rateset_t *rs)
a9533e7e
HP
8027{
8028 wlc_rateset_default(rs, NULL, wlc->band->phytype, wlc->band->bandtype,
0965ae88 8029 false, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
a9533e7e
HP
8030 CHSPEC_WLC_BW(wlc->default_bss->chanspec),
8031 wlc->stf->txstreams);
8032}
8033
c6a9e1fc 8034static void wlc_bss_default_init(struct wlc_info *wlc)
a2627bc0 8035{
a9533e7e 8036 chanspec_t chanspec;
f077f718 8037 struct wlcband *band;
a9533e7e
HP
8038 wlc_bss_info_t *bi = wlc->default_bss;
8039
8040 /* init default and target BSS with some sane initial values */
9249ede9 8041 memset((char *)(bi), 0, sizeof(wlc_bss_info_t));
a9533e7e
HP
8042 bi->beacon_period = ISSIM_ENAB(wlc->pub->sih) ? BEACON_INTERVAL_DEF_QT :
8043 BEACON_INTERVAL_DEFAULT;
8044 bi->dtim_period = ISSIM_ENAB(wlc->pub->sih) ? DTIM_INTERVAL_DEF_QT :
8045 DTIM_INTERVAL_DEFAULT;
8046
8047 /* fill the default channel as the first valid channel
8048 * starting from the 2G channels
8049 */
8050 chanspec = CH20MHZ_CHSPEC(1);
8051 ASSERT(chanspec != INVCHANSPEC);
8052
8053 wlc->home_chanspec = bi->chanspec = chanspec;
8054
8055 /* find the band of our default channel */
8056 band = wlc->band;
8057 if (NBANDS(wlc) > 1 && band->bandunit != CHSPEC_WLCBANDUNIT(chanspec))
8058 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
8059
8060 /* init bss rates to the band specific default rate set */
8061 wlc_rateset_default(&bi->rateset, NULL, band->phytype, band->bandtype,
0965ae88 8062 false, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
a9533e7e
HP
8063 CHSPEC_WLC_BW(chanspec), wlc->stf->txstreams);
8064
8065 if (N_ENAB(wlc->pub))
8066 bi->flags |= WLC_BSS_HT;
8067}
8068
8069/* Deferred event processing */
8070static void wlc_process_eventq(void *arg)
8071{
c6a9e1fc 8072 struct wlc_info *wlc = (struct wlc_info *) arg;
a9533e7e
HP
8073 wlc_event_t *etmp;
8074
8075 while ((etmp = wlc_eventq_deq(wlc->eventq))) {
8076 /* Perform OS specific event processing */
8077 wl_event(wlc->wl, etmp->event.ifname, etmp);
8078 if (etmp->data) {
182acb3c 8079 kfree(etmp->data);
a9533e7e
HP
8080 etmp->data = NULL;
8081 }
8082 wlc_event_free(wlc->eventq, etmp);
8083 }
8084}
8085
8086void
66cbd3ab 8087wlc_uint64_sub(u32 *a_high, u32 *a_low, u32 b_high, u32 b_low)
a9533e7e
HP
8088{
8089 if (b_low > *a_low) {
8090 /* low half needs a carry */
8091 b_high += 1;
8092 }
8093 *a_low -= b_low;
8094 *a_high -= b_high;
8095}
8096
8097static ratespec_t
f077f718
RV
8098mac80211_wlc_set_nrate(struct wlc_info *wlc, struct wlcband *cur_band,
8099 u32 int_val)
a9533e7e 8100{
41feb5ed
GKH
8101 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
8102 u8 rate = int_val & NRATE_RATE_MASK;
a9533e7e
HP
8103 ratespec_t rspec;
8104 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
8105 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
8106 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
8107 == NRATE_OVERRIDE_MCS_ONLY);
8108 int bcmerror = 0;
8109
8110 if (!ismcs) {
8111 return (ratespec_t) rate;
8112 }
8113
8114 /* validate the combination of rate/mcs/stf is allowed */
8115 if (N_ENAB(wlc->pub) && ismcs) {
8116 /* mcs only allowed when nmode */
8117 if (stf > PHY_TXC1_MODE_SDM) {
f4528696
JP
8118 WL_ERROR("wl%d: %s: Invalid stf\n",
8119 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8120 bcmerror = BCME_RANGE;
8121 goto done;
8122 }
8123
8124 /* mcs 32 is a special case, DUP mode 40 only */
8125 if (rate == 32) {
8126 if (!CHSPEC_IS40(wlc->home_chanspec) ||
8127 ((stf != PHY_TXC1_MODE_SISO)
8128 && (stf != PHY_TXC1_MODE_CDD))) {
f4528696
JP
8129 WL_ERROR("wl%d: %s: Invalid mcs 32\n",
8130 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8131 bcmerror = BCME_RANGE;
8132 goto done;
8133 }
8134 /* mcs > 7 must use stf SDM */
8135 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
8136 /* mcs > 7 must use stf SDM */
8137 if (stf != PHY_TXC1_MODE_SDM) {
f4528696
JP
8138 WL_TRACE("wl%d: %s: enabling SDM mode for mcs %d\n",
8139 WLCWLUNIT(wlc), __func__, rate);
a9533e7e
HP
8140 stf = PHY_TXC1_MODE_SDM;
8141 }
8142 } else {
8143 /* MCS 0-7 may use SISO, CDD, and for phy_rev >= 3 STBC */
8144 if ((stf > PHY_TXC1_MODE_STBC) ||
8145 (!WLC_STBC_CAP_PHY(wlc)
8146 && (stf == PHY_TXC1_MODE_STBC))) {
f4528696
JP
8147 WL_ERROR("wl%d: %s: Invalid STBC\n",
8148 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8149 bcmerror = BCME_RANGE;
8150 goto done;
8151 }
8152 }
8153 } else if (IS_OFDM(rate)) {
8154 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
f4528696
JP
8155 WL_ERROR("wl%d: %s: Invalid OFDM\n",
8156 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8157 bcmerror = BCME_RANGE;
8158 goto done;
8159 }
8160 } else if (IS_CCK(rate)) {
8161 if ((cur_band->bandtype != WLC_BAND_2G)
8162 || (stf != PHY_TXC1_MODE_SISO)) {
f4528696
JP
8163 WL_ERROR("wl%d: %s: Invalid CCK\n",
8164 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8165 bcmerror = BCME_RANGE;
8166 goto done;
8167 }
8168 } else {
f4528696
JP
8169 WL_ERROR("wl%d: %s: Unknown rate type\n",
8170 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8171 bcmerror = BCME_RANGE;
8172 goto done;
8173 }
8174 /* make sure multiple antennae are available for non-siso rates */
8175 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
f4528696
JP
8176 WL_ERROR("wl%d: %s: SISO antenna but !SISO request\n",
8177 WLCWLUNIT(wlc), __func__);
a9533e7e
HP
8178 bcmerror = BCME_RANGE;
8179 goto done;
8180 }
8181
8182 rspec = rate;
8183 if (ismcs) {
8184 rspec |= RSPEC_MIMORATE;
8185 /* For STBC populate the STC field of the ratespec */
8186 if (stf == PHY_TXC1_MODE_STBC) {
41feb5ed 8187 u8 stc;
a9533e7e
HP
8188 stc = 1; /* Nss for single stream is always 1 */
8189 rspec |= (stc << RSPEC_STC_SHIFT);
8190 }
8191 }
8192
8193 rspec |= (stf << RSPEC_STF_SHIFT);
8194
8195 if (override_mcs_only)
8196 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
8197
8198 if (issgi)
8199 rspec |= RSPEC_SHORT_GI;
8200
8201 if ((rate != 0)
0f0881b0 8202 && !wlc_valid_rate(wlc, rspec, cur_band->bandtype, true)) {
a9533e7e
HP
8203 return rate;
8204 }
8205
8206 return rspec;
8207 done:
f4528696 8208 WL_ERROR("Hoark\n");
a9533e7e
HP
8209 return rate;
8210}
8211
8212/* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
8213static int
c6a9e1fc 8214wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
a9533e7e
HP
8215 bool writeToShm)
8216{
8217 int idle_busy_ratio_x_16 = 0;
8218 uint offset =
8219 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
8220 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
8221 if (duty_cycle > 100 || duty_cycle < 0) {
f4528696 8222 WL_ERROR("wl%d: duty cycle value off limit\n", wlc->pub->unit);
a9533e7e
HP
8223 return BCME_RANGE;
8224 }
8225 if (duty_cycle)
8226 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
8227 /* Only write to shared memory when wl is up */
8228 if (writeToShm)
7d4df48e 8229 wlc_write_shm(wlc, offset, (u16) idle_busy_ratio_x_16);
a9533e7e
HP
8230
8231 if (isOFDM)
7d4df48e 8232 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
a9533e7e 8233 else
7d4df48e 8234 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
a9533e7e
HP
8235
8236 return BCME_OK;
8237}
8238
7d4df48e 8239/* Read a single u16 from shared memory.
a9533e7e
HP
8240 * SHM 'offset' needs to be an even address
8241 */
c6a9e1fc 8242u16 wlc_read_shm(struct wlc_info *wlc, uint offset)
a9533e7e
HP
8243{
8244 return wlc_bmac_read_shm(wlc->hw, offset);
8245}
8246
7d4df48e 8247/* Write a single u16 to shared memory.
a9533e7e
HP
8248 * SHM 'offset' needs to be an even address
8249 */
c6a9e1fc 8250void wlc_write_shm(struct wlc_info *wlc, uint offset, u16 v)
a9533e7e
HP
8251{
8252 wlc_bmac_write_shm(wlc->hw, offset, v);
8253}
8254
8255/* Set a range of shared memory to a value.
8256 * SHM 'offset' needs to be an even address and
8257 * Range length 'len' must be an even number of bytes
8258 */
c6a9e1fc 8259void wlc_set_shm(struct wlc_info *wlc, uint offset, u16 v, int len)
a9533e7e
HP
8260{
8261 /* offset and len need to be even */
8262 ASSERT((offset & 1) == 0);
8263 ASSERT((len & 1) == 0);
8264
8265 if (len <= 0)
8266 return;
8267
8268 wlc_bmac_set_shm(wlc->hw, offset, v, len);
8269}
8270
8271/* Copy a buffer to shared memory.
8272 * SHM 'offset' needs to be an even address and
8273 * Buffer length 'len' must be an even number of bytes
8274 */
c6a9e1fc 8275void wlc_copyto_shm(struct wlc_info *wlc, uint offset, const void *buf, int len)
a9533e7e
HP
8276{
8277 /* offset and len need to be even */
8278 ASSERT((offset & 1) == 0);
8279 ASSERT((len & 1) == 0);
8280
8281 if (len <= 0)
8282 return;
8283 wlc_bmac_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8284
8285}
8286
8287/* Copy from shared memory to a buffer.
8288 * SHM 'offset' needs to be an even address and
8289 * Buffer length 'len' must be an even number of bytes
8290 */
c6a9e1fc 8291void wlc_copyfrom_shm(struct wlc_info *wlc, uint offset, void *buf, int len)
a9533e7e
HP
8292{
8293 /* offset and len need to be even */
8294 ASSERT((offset & 1) == 0);
8295 ASSERT((len & 1) == 0);
8296
8297 if (len <= 0)
8298 return;
8299
8300 wlc_bmac_copyfrom_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8301}
8302
8303/* wrapper BMAC functions to for HIGH driver access */
c6a9e1fc 8304void wlc_mctrl(struct wlc_info *wlc, u32 mask, u32 val)
a9533e7e
HP
8305{
8306 wlc_bmac_mctrl(wlc->hw, mask, val);
8307}
8308
c6a9e1fc 8309void wlc_corereset(struct wlc_info *wlc, u32 flags)
a9533e7e
HP
8310{
8311 wlc_bmac_corereset(wlc->hw, flags);
8312}
8313
c6a9e1fc 8314void wlc_mhf(struct wlc_info *wlc, u8 idx, u16 mask, u16 val, int bands)
a9533e7e
HP
8315{
8316 wlc_bmac_mhf(wlc->hw, idx, mask, val, bands);
8317}
8318
c6a9e1fc 8319u16 wlc_mhf_get(struct wlc_info *wlc, u8 idx, int bands)
a9533e7e
HP
8320{
8321 return wlc_bmac_mhf_get(wlc->hw, idx, bands);
8322}
8323
c6a9e1fc 8324int wlc_xmtfifo_sz_get(struct wlc_info *wlc, uint fifo, uint *blocks)
a9533e7e
HP
8325{
8326 return wlc_bmac_xmtfifo_sz_get(wlc->hw, fifo, blocks);
8327}
8328
c6a9e1fc
RV
8329void wlc_write_template_ram(struct wlc_info *wlc, int offset, int len,
8330 void *buf)
a9533e7e
HP
8331{
8332 wlc_bmac_write_template_ram(wlc->hw, offset, len, buf);
8333}
8334
c6a9e1fc
RV
8335void wlc_write_hw_bcntemplates(struct wlc_info *wlc, void *bcn, int len,
8336 bool both)
a9533e7e
HP
8337{
8338 wlc_bmac_write_hw_bcntemplates(wlc->hw, bcn, len, both);
8339}
8340
8341void
c6a9e1fc 8342wlc_set_addrmatch(struct wlc_info *wlc, int match_reg_offset,
a44d4236 8343 const u8 *addr)
a9533e7e
HP
8344{
8345 wlc_bmac_set_addrmatch(wlc->hw, match_reg_offset, addr);
6677eaa3
AS
8346 if (match_reg_offset == RCM_BSSID_OFFSET)
8347 memcpy(wlc->cfg->BSSID, addr, ETH_ALEN);
a9533e7e
HP
8348}
8349
a44d4236 8350void wlc_set_rcmta(struct wlc_info *wlc, int idx, const u8 *addr)
a9533e7e
HP
8351{
8352 wlc_bmac_set_rcmta(wlc->hw, idx, addr);
8353}
8354
c6a9e1fc 8355void wlc_read_tsf(struct wlc_info *wlc, u32 *tsf_l_ptr, u32 *tsf_h_ptr)
a9533e7e
HP
8356{
8357 wlc_bmac_read_tsf(wlc->hw, tsf_l_ptr, tsf_h_ptr);
8358}
8359
c6a9e1fc 8360void wlc_set_cwmin(struct wlc_info *wlc, u16 newmin)
a9533e7e
HP
8361{
8362 wlc->band->CWmin = newmin;
8363 wlc_bmac_set_cwmin(wlc->hw, newmin);
8364}
8365
c6a9e1fc 8366void wlc_set_cwmax(struct wlc_info *wlc, u16 newmax)
a9533e7e
HP
8367{
8368 wlc->band->CWmax = newmax;
8369 wlc_bmac_set_cwmax(wlc->hw, newmax);
8370}
8371
c6a9e1fc 8372void wlc_fifoerrors(struct wlc_info *wlc)
a9533e7e
HP
8373{
8374
8375 wlc_bmac_fifoerrors(wlc->hw);
8376}
8377
8378/* Search mem rw utilities */
8379
c6a9e1fc 8380void wlc_pllreq(struct wlc_info *wlc, bool set, mbool req_bit)
a9533e7e
HP
8381{
8382 wlc_bmac_pllreq(wlc->hw, set, req_bit);
8383}
8384
c6a9e1fc 8385void wlc_reset_bmac_done(struct wlc_info *wlc)
a9533e7e 8386{
a9533e7e
HP
8387}
8388
c6a9e1fc 8389void wlc_ht_mimops_cap_update(struct wlc_info *wlc, u8 mimops_mode)
a9533e7e 8390{
651bd3a9
RV
8391 wlc->ht_cap.cap_info &= ~HT_CAP_MIMO_PS_MASK;
8392 wlc->ht_cap.cap_info |= (mimops_mode << IEEE80211_HT_CAP_SM_PS_SHIFT);
a9533e7e
HP
8393
8394 if (AP_ENAB(wlc->pub) && wlc->clk) {
8395 wlc_update_beacon(wlc);
0f0881b0 8396 wlc_update_probe_resp(wlc, true);
a9533e7e
HP
8397 }
8398}
8399
8400/* check for the particular priority flow control bit being set */
8401bool
c6a9e1fc 8402wlc_txflowcontrol_prio_isset(struct wlc_info *wlc, wlc_txq_info_t *q, int prio)
a9533e7e
HP
8403{
8404 uint prio_mask;
8405
8406 if (prio == ALLPRIO) {
8407 prio_mask = TXQ_STOP_FOR_PRIOFC_MASK;
8408 } else {
8409 ASSERT(prio >= 0 && prio <= MAXPRIO);
8410 prio_mask = NBITVAL(prio);
8411 }
8412
8413 return (q->stopped & prio_mask) == prio_mask;
8414}
8415
8416/* propogate the flow control to all interfaces using the given tx queue */
c6a9e1fc
RV
8417void wlc_txflowcontrol(struct wlc_info *wlc, wlc_txq_info_t *qi,
8418 bool on, int prio)
a9533e7e
HP
8419{
8420 uint prio_bits;
8421 uint cur_bits;
8422
4a079150 8423 WL_TRACE("%s: flow control kicks in\n", __func__);
a9533e7e
HP
8424
8425 if (prio == ALLPRIO) {
8426 prio_bits = TXQ_STOP_FOR_PRIOFC_MASK;
8427 } else {
8428 ASSERT(prio >= 0 && prio <= MAXPRIO);
8429 prio_bits = NBITVAL(prio);
8430 }
8431
8432 cur_bits = qi->stopped & prio_bits;
8433
8434 /* Check for the case of no change and return early
8435 * Otherwise update the bit and continue
8436 */
8437 if (on) {
8438 if (cur_bits == prio_bits) {
8439 return;
8440 }
8441 mboolset(qi->stopped, prio_bits);
8442 } else {
8443 if (cur_bits == 0) {
8444 return;
8445 }
8446 mboolclr(qi->stopped, prio_bits);
8447 }
8448
8449 /* If there is a flow control override we will not change the external
8450 * flow control state.
8451 */
8452 if (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK) {
8453 return;
8454 }
8455
8456 wlc_txflowcontrol_signal(wlc, qi, on, prio);
8457}
8458
8459void
c6a9e1fc 8460wlc_txflowcontrol_override(struct wlc_info *wlc, wlc_txq_info_t *qi, bool on,
a9533e7e
HP
8461 uint override)
8462{
8463 uint prev_override;
8464
8465 ASSERT(override != 0);
8466 ASSERT((override & TXQ_STOP_FOR_PRIOFC_MASK) == 0);
8467
8468 prev_override = (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK);
8469
8470 /* Update the flow control bits and do an early return if there is
8471 * no change in the external flow control state.
8472 */
8473 if (on) {
8474 mboolset(qi->stopped, override);
8475 /* if there was a previous override bit on, then setting this
8476 * makes no difference.
8477 */
8478 if (prev_override) {
8479 return;
8480 }
8481
8482 wlc_txflowcontrol_signal(wlc, qi, ON, ALLPRIO);
8483 } else {
8484 mboolclr(qi->stopped, override);
8485 /* clearing an override bit will only make a difference for
8486 * flow control if it was the only bit set. For any other
8487 * override setting, just return
8488 */
8489 if (prev_override != override) {
8490 return;
8491 }
8492
8493 if (qi->stopped == 0) {
8494 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8495 } else {
8496 int prio;
8497
8498 for (prio = MAXPRIO; prio >= 0; prio--) {
8499 if (!mboolisset(qi->stopped, NBITVAL(prio)))
8500 wlc_txflowcontrol_signal(wlc, qi, OFF,
8501 prio);
8502 }
8503 }
8504 }
8505}
8506
c6a9e1fc 8507static void wlc_txflowcontrol_reset(struct wlc_info *wlc)
a9533e7e
HP
8508{
8509 wlc_txq_info_t *qi;
8510
8511 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
8512 if (qi->stopped) {
8513 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8514 qi->stopped = 0;
8515 }
8516 }
8517}
8518
8519static void
c6a9e1fc 8520wlc_txflowcontrol_signal(struct wlc_info *wlc, wlc_txq_info_t *qi, bool on,
a9533e7e
HP
8521 int prio)
8522{
4dc79de1 8523 struct wlc_if *wlcif;
a9533e7e
HP
8524
8525 for (wlcif = wlc->wlcif_list; wlcif != NULL; wlcif = wlcif->next) {
8526 if (wlcif->qi == qi && wlcif->flags & WLC_IF_LINKED)
8527 wl_txflowcontrol(wlc->wl, wlcif->wlif, on, prio);
8528 }
8529}
8530
c6a9e1fc 8531static wlc_txq_info_t *wlc_txq_alloc(struct wlc_info *wlc, struct osl_info *osh)
a9533e7e
HP
8532{
8533 wlc_txq_info_t *qi, *p;
8534
8535 qi = (wlc_txq_info_t *) wlc_calloc(osh, wlc->pub->unit,
8536 sizeof(wlc_txq_info_t));
8537 if (qi == NULL) {
8538 return NULL;
8539 }
8540
8541 /* Have enough room for control packets along with HI watermark */
8542 /* Also, add room to txq for total psq packets if all the SCBs leave PS mode */
8543 /* The watermark for flowcontrol to OS packets will remain the same */
8544 pktq_init(&qi->q, WLC_PREC_COUNT,
8545 (2 * wlc->pub->tunables->datahiwat) + PKTQ_LEN_DEFAULT +
8546 wlc->pub->psq_pkts_total);
8547
8548 /* add this queue to the the global list */
8549 p = wlc->tx_queues;
8550 if (p == NULL) {
8551 wlc->tx_queues = qi;
8552 } else {
8553 while (p->next != NULL)
8554 p = p->next;
8555 p->next = qi;
8556 }
8557
8558 return qi;
8559}
8560
c6a9e1fc 8561static void wlc_txq_free(struct wlc_info *wlc, struct osl_info *osh,
e69284f2 8562 wlc_txq_info_t *qi)
a9533e7e
HP
8563{
8564 wlc_txq_info_t *p;
8565
8566 if (qi == NULL)
8567 return;
8568
8569 /* remove the queue from the linked list */
8570 p = wlc->tx_queues;
8571 if (p == qi)
8572 wlc->tx_queues = p->next;
8573 else {
8574 while (p != NULL && p->next != qi)
8575 p = p->next;
8576 ASSERT(p->next == qi);
8577 if (p != NULL)
8578 p->next = p->next->next;
8579 }
8580
182acb3c 8581 kfree(qi);
a9533e7e 8582}
6a3be6e6
RV
8583
8584/*
8585 * Flag 'scan in progress' to withold dynamic phy calibration
8586 */
8587void wlc_scan_start(struct wlc_info *wlc)
8588{
8589 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
8590}
8591
8592void wlc_scan_stop(struct wlc_info *wlc)
8593{
8594 wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
8595}
d8a1fb44
AS
8596
8597void wlc_associate_upd(struct wlc_info *wlc, bool state)
8598{
8599 wlc->pub->associated = state;
8600 wlc->cfg->associated = state;
8601}
This page took 0.836027 seconds and 5 git commands to generate.