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