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