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