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