]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmsmac/wlc_main.c
staging: brcm80211: added support for more bcm43224 based boards
[mv-sheeva.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 <linux/pci_ids.h>
20 #include <net/mac80211.h>
21
22 #include <bcmdefs.h>
23 #include <bcmdevs.h>
24 #include <bcmutils.h>
25 #include <bcmwifi.h>
26 #include <bcmnvram.h>
27 #include <aiutils.h>
28 #include <pcicfg.h>
29 #include <bcmsrom.h>
30 #include <sbhnddma.h>
31 #include <hnddma.h>
32
33 #include "wlc_pmu.h"
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_antsel.h"
48 #include "wlc_stf.h"
49 #include "wlc_ampdu.h"
50 #include "wl_export.h"
51 #include "wlc_alloc.h"
52 #include "wl_dbg.h"
53 #include "wl_mac80211.h"
54
55 /*
56  * WPA(2) definitions
57  */
58 #define RSN_CAP_4_REPLAY_CNTRS          2
59 #define RSN_CAP_16_REPLAY_CNTRS         3
60
61 #define WPA_CAP_4_REPLAY_CNTRS          RSN_CAP_4_REPLAY_CNTRS
62 #define WPA_CAP_16_REPLAY_CNTRS         RSN_CAP_16_REPLAY_CNTRS
63
64 /*
65  * Indication for txflowcontrol that all priority bits in
66  * TXQ_STOP_FOR_PRIOFC_MASK are to be considered.
67  */
68 #define ALLPRIO         -1
69
70 /*
71  * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
72  */
73 #define SSID_FMT_BUF_LEN        ((4 * IEEE80211_MAX_SSID_LEN) + 1)
74
75 #define TIMER_INTERVAL_WATCHDOG 1000    /* watchdog timer, in unit of ms */
76 #define TIMER_INTERVAL_RADIOCHK 800     /* radio monitor timer, in unit of ms */
77
78 #ifndef WLC_MPC_MAX_DELAYCNT
79 #define WLC_MPC_MAX_DELAYCNT    10      /* Max MPC timeout, in unit of watchdog */
80 #endif
81 #define WLC_MPC_MIN_DELAYCNT    1       /* Min MPC timeout, in unit of watchdog */
82 #define WLC_MPC_THRESHOLD       3       /* MPC count threshold level */
83
84 #define BEACON_INTERVAL_DEFAULT 100     /* beacon interval, in unit of 1024TU */
85 #define DTIM_INTERVAL_DEFAULT   3       /* DTIM interval, in unit of beacon interval */
86
87 /* Scale down delays to accommodate QT slow speed */
88 #define BEACON_INTERVAL_DEF_QT  20      /* beacon interval, in unit of 1024TU */
89 #define DTIM_INTERVAL_DEF_QT    1       /* DTIM interval, in unit of beacon interval */
90
91 #define TBTT_ALIGN_LEEWAY_US    100     /* min leeway before first TBTT in us */
92
93 /* Software feature flag defines used by wlfeatureflag */
94 #define WL_SWFL_NOHWRADIO       0x0004
95 #define WL_SWFL_FLOWCONTROL     0x0008  /* Enable backpressure to OS stack */
96 #define WL_SWFL_WLBSSSORT       0x0010  /* Per-port supports sorting of BSS */
97
98 /* n-mode support capability */
99 /* 2x2 includes both 1x1 & 2x2 devices
100  * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
101  * control it independently
102  */
103 #define WL_11N_2x2                      1
104 #define WL_11N_3x3                      3
105 #define WL_11N_4x4                      4
106
107 /* define 11n feature disable flags */
108 #define WLFEATURE_DISABLE_11N           0x00000001
109 #define WLFEATURE_DISABLE_11N_STBC_TX   0x00000002
110 #define WLFEATURE_DISABLE_11N_STBC_RX   0x00000004
111 #define WLFEATURE_DISABLE_11N_SGI_TX    0x00000008
112 #define WLFEATURE_DISABLE_11N_SGI_RX    0x00000010
113 #define WLFEATURE_DISABLE_11N_AMPDU_TX  0x00000020
114 #define WLFEATURE_DISABLE_11N_AMPDU_RX  0x00000040
115 #define WLFEATURE_DISABLE_11N_GF        0x00000080
116
117 /*
118  * driver maintains internal 'tick'(wlc->pub->now) which increments in 1s OS timer(soft
119  * watchdog) it is not a wall clock and won't increment when driver is in "down" state
120  * this low resolution driver tick can be used for maintenance tasks such as phy
121  * calibration and scb update
122  */
123
124 /* To inform the ucode of the last mcast frame posted so that it can clear moredata bit */
125 #define BCMCFID(wlc, fid) wlc_bmac_write_shm((wlc)->hw, M_BCMC_FID, (fid))
126
127 #define WLC_WAR16165(wlc) (wlc->pub->sih->bustype == PCI_BUS && \
128                                 (!AP_ENAB(wlc->pub)) && (wlc->war16165))
129
130 /* debug/trace */
131 uint wl_msg_level =
132 #if defined(BCMDBG)
133     WL_ERROR_VAL;
134 #else
135     0;
136 #endif                          /* BCMDBG */
137
138 /* Find basic rate for a given rate */
139 #define WLC_BASIC_RATE(wlc, rspec)      (IS_MCS(rspec) ? \
140                         (wlc)->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK].leg_ofdm] : \
141                         (wlc)->band->basic_rate[rspec & RSPEC_RATE_MASK])
142
143 #define FRAMETYPE(r, mimoframe) (IS_MCS(r) ? mimoframe  : (IS_CCK(r) ? FT_CCK : FT_OFDM))
144
145 #define RFDISABLE_DEFAULT       10000000        /* rfdisable delay timer 500 ms, runs of ALP clock */
146
147 #define WLC_TEMPSENSE_PERIOD            10      /* 10 second timeout */
148
149 #define SCAN_IN_PROGRESS(x)     0
150
151 #define EPI_VERSION_NUM         0x054b0b00
152
153 #ifdef BCMDBG
154 /* pointer to most recently allocated wl/wlc */
155 static struct wlc_info *wlc_info_dbg = (struct wlc_info *) (NULL);
156 #endif
157
158 const u8 prio2fifo[NUMPRIO] = {
159         TX_AC_BE_FIFO,          /* 0    BE      AC_BE   Best Effort */
160         TX_AC_BK_FIFO,          /* 1    BK      AC_BK   Background */
161         TX_AC_BK_FIFO,          /* 2    --      AC_BK   Background */
162         TX_AC_BE_FIFO,          /* 3    EE      AC_BE   Best Effort */
163         TX_AC_VI_FIFO,          /* 4    CL      AC_VI   Video */
164         TX_AC_VI_FIFO,          /* 5    VI      AC_VI   Video */
165         TX_AC_VO_FIFO,          /* 6    VO      AC_VO   Voice */
166         TX_AC_VO_FIFO           /* 7    NC      AC_VO   Voice */
167 };
168
169 /* precedences numbers for wlc queues. These are twice as may levels as
170  * 802.1D priorities.
171  * Odd numbers are used for HI priority traffic at same precedence levels
172  * These constants are used ONLY by wlc_prio2prec_map.  Do not use them elsewhere.
173  */
174 #define _WLC_PREC_NONE          0       /* None = - */
175 #define _WLC_PREC_BK            2       /* BK - Background */
176 #define _WLC_PREC_BE            4       /* BE - Best-effort */
177 #define _WLC_PREC_EE            6       /* EE - Excellent-effort */
178 #define _WLC_PREC_CL            8       /* CL - Controlled Load */
179 #define _WLC_PREC_VI            10      /* Vi - Video */
180 #define _WLC_PREC_VO            12      /* Vo - Voice */
181 #define _WLC_PREC_NC            14      /* NC - Network Control */
182
183 /* 802.1D Priority to precedence queue mapping */
184 const u8 wlc_prio2prec_map[] = {
185         _WLC_PREC_BE,           /* 0 BE - Best-effort */
186         _WLC_PREC_BK,           /* 1 BK - Background */
187         _WLC_PREC_NONE,         /* 2 None = - */
188         _WLC_PREC_EE,           /* 3 EE - Excellent-effort */
189         _WLC_PREC_CL,           /* 4 CL - Controlled Load */
190         _WLC_PREC_VI,           /* 5 Vi - Video */
191         _WLC_PREC_VO,           /* 6 Vo - Voice */
192         _WLC_PREC_NC,           /* 7 NC - Network Control */
193 };
194
195 /* Sanity check for tx_prec_map and fifo synchup
196  * Either there are some packets pending for the fifo, else if fifo is empty then
197  * all the corresponding precmap bits should be set
198  */
199 #define WLC_TX_FIFO_CHECK(wlc, fifo) (TXPKTPENDGET((wlc), (fifo)) ||    \
200         (TXPKTPENDGET((wlc), (fifo)) == 0 && \
201         ((wlc)->tx_prec_map & (wlc)->fifo2prec_map[(fifo)]) == \
202         (wlc)->fifo2prec_map[(fifo)]))
203
204 /* TX FIFO number to WME/802.1E Access Category */
205 const u8 wme_fifo2ac[] = { AC_BK, AC_BE, AC_VI, AC_VO, AC_BE, AC_BE };
206
207 /* WME/802.1E Access Category to TX FIFO number */
208 static const u8 wme_ac2fifo[] = { 1, 0, 2, 3 };
209
210 static bool in_send_q = false;
211
212 /* Shared memory location index for various AC params */
213 #define wme_shmemacindex(ac)    wme_ac2fifo[ac]
214
215 #ifdef BCMDBG
216 static const char *fifo_names[] = {
217         "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
218 #else
219 static const char fifo_names[6][0];
220 #endif
221
222 static const u8 acbitmap2maxprio[] = {
223         PRIO_8021D_BE, PRIO_8021D_BE, PRIO_8021D_BK, PRIO_8021D_BK,
224         PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI,
225         PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO,
226         PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO
227 };
228
229 /* currently the best mechanism for determining SIFS is the band in use */
230 #define SIFS(band) ((band)->bandtype == WLC_BAND_5G ? APHY_SIFS_TIME : BPHY_SIFS_TIME);
231
232 /* value for # replay counters currently supported */
233 #define WLC_REPLAY_CNTRS_VALUE  WPA_CAP_16_REPLAY_CNTRS
234
235 /* local prototypes */
236 static u16 wlc_d11hdrs_mac80211(struct wlc_info *wlc,
237                                                struct ieee80211_hw *hw,
238                                                struct sk_buff *p,
239                                                struct scb *scb, uint frag,
240                                                uint nfrags, uint queue,
241                                                uint next_frag_len,
242                                                wsec_key_t *key,
243                                                ratespec_t rspec_override);
244 static void wlc_bss_default_init(struct wlc_info *wlc);
245 static void wlc_ucode_mac_upd(struct wlc_info *wlc);
246 static ratespec_t mac80211_wlc_set_nrate(struct wlc_info *wlc,
247                                          struct wlcband *cur_band, u32 int_val);
248 static void wlc_tx_prec_map_init(struct wlc_info *wlc);
249 static void wlc_watchdog(void *arg);
250 static void wlc_watchdog_by_timer(void *arg);
251 static u16 wlc_rate_shm_offset(struct wlc_info *wlc, u8 rate);
252 static int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg);
253 static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc);
254
255 /* send and receive */
256 static struct wlc_txq_info *wlc_txq_alloc(struct wlc_info *wlc);
257 static void wlc_txq_free(struct wlc_info *wlc,
258                          struct wlc_txq_info *qi);
259 static void wlc_txflowcontrol_signal(struct wlc_info *wlc,
260                                      struct wlc_txq_info *qi,
261                                      bool on, int prio);
262 static void wlc_txflowcontrol_reset(struct wlc_info *wlc);
263 static void wlc_compute_cck_plcp(struct wlc_info *wlc, ratespec_t rate,
264                                  uint length, u8 *plcp);
265 static void wlc_compute_ofdm_plcp(ratespec_t rate, uint length, u8 *plcp);
266 static void wlc_compute_mimo_plcp(ratespec_t rate, uint length, u8 *plcp);
267 static u16 wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate,
268                                     u8 preamble_type, uint next_frag_len);
269 static u64 wlc_recover_tsf64(struct wlc_info *wlc, struct wlc_d11rxhdr *rxh);
270 static void wlc_recvctl(struct wlc_info *wlc,
271                         d11rxhdr_t *rxh, struct sk_buff *p);
272 static uint wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t rate,
273                                u8 preamble_type, uint dur);
274 static uint wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rate,
275                               u8 preamble_type);
276 static uint wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rate,
277                               u8 preamble_type);
278 /* interrupt, up/down, band */
279 static void wlc_setband(struct wlc_info *wlc, uint bandunit);
280 static chanspec_t wlc_init_chanspec(struct wlc_info *wlc);
281 static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec);
282 static void wlc_bsinit(struct wlc_info *wlc);
283 static int wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
284                               bool writeToShm);
285 static void wlc_radio_hwdisable_upd(struct wlc_info *wlc);
286 static bool wlc_radio_monitor_start(struct wlc_info *wlc);
287 static void wlc_radio_timer(void *arg);
288 static void wlc_radio_enable(struct wlc_info *wlc);
289 static void wlc_radio_upd(struct wlc_info *wlc);
290
291 /* scan, association, BSS */
292 static uint wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rate,
293                              u8 preamble_type);
294 static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap);
295 static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val);
296 static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val);
297 static void wlc_war16165(struct wlc_info *wlc, bool tx);
298
299 static void wlc_wme_retries_write(struct wlc_info *wlc);
300 static bool wlc_attach_stf_ant_init(struct wlc_info *wlc);
301 static uint wlc_attach_module(struct wlc_info *wlc);
302 static void wlc_detach_module(struct wlc_info *wlc);
303 static void wlc_timers_deinit(struct wlc_info *wlc);
304 static void wlc_down_led_upd(struct wlc_info *wlc);
305 static uint wlc_down_del_timer(struct wlc_info *wlc);
306 static void wlc_ofdm_rateset_war(struct wlc_info *wlc);
307 static int _wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
308                       struct wlc_if *wlcif);
309
310 /* conditions under which the PM bit should be set in outgoing frames and STAY_AWAKE is meaningful
311  */
312 bool wlc_ps_allowed(struct wlc_info *wlc)
313 {
314         int idx;
315         struct wlc_bsscfg *cfg;
316
317         /* disallow PS when one of the following global conditions meets */
318         if (!wlc->pub->associated)
319                 return false;
320
321         /* disallow PS when one of these meets when not scanning */
322         if (AP_ACTIVE(wlc) || wlc->monitor)
323                 return false;
324
325         FOREACH_AS_STA(wlc, idx, cfg) {
326                 /* disallow PS when one of the following bsscfg specific conditions meets */
327                 if (!cfg->BSS || !WLC_PORTOPEN(cfg))
328                         return false;
329
330                 if (!cfg->dtim_programmed)
331                         return false;
332         }
333
334         return true;
335 }
336
337 void wlc_reset(struct wlc_info *wlc)
338 {
339         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
340
341         /* slurp up hw mac counters before core reset */
342         wlc_statsupd(wlc);
343
344         /* reset our snapshot of macstat counters */
345         memset((char *)wlc->core->macstat_snapshot, 0,
346                 sizeof(macstat_t));
347
348         wlc_bmac_reset(wlc->hw);
349 }
350
351 void wlc_fatal_error(struct wlc_info *wlc)
352 {
353         wiphy_err(wlc->wiphy, "wl%d: fatal error, reinitializing\n",
354                   wlc->pub->unit);
355         wl_init(wlc->wl);
356 }
357
358 /* Return the channel the driver should initialize during wlc_init.
359  * the channel may have to be changed from the currently configured channel
360  * if other configurations are in conflict (bandlocked, 11n mode disabled,
361  * invalid channel for current country, etc.)
362  */
363 static chanspec_t wlc_init_chanspec(struct wlc_info *wlc)
364 {
365         chanspec_t chanspec =
366             1 | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE |
367             WL_CHANSPEC_BAND_2G;
368
369         return chanspec;
370 }
371
372 struct scb global_scb;
373
374 static void wlc_init_scb(struct wlc_info *wlc, struct scb *scb)
375 {
376         int i;
377         scb->flags = SCB_WMECAP | SCB_HTCAP;
378         for (i = 0; i < NUMPRIO; i++)
379                 scb->seqnum[i] = 0;
380 }
381
382 void wlc_init(struct wlc_info *wlc)
383 {
384         d11regs_t *regs;
385         chanspec_t chanspec;
386         int i;
387         struct wlc_bsscfg *bsscfg;
388         bool mute = false;
389
390         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
391
392         regs = wlc->regs;
393
394         /* This will happen if a big-hammer was executed. In that case, we want to go back
395          * to the channel that we were on and not new channel
396          */
397         if (wlc->pub->associated)
398                 chanspec = wlc->home_chanspec;
399         else
400                 chanspec = wlc_init_chanspec(wlc);
401
402         wlc_bmac_init(wlc->hw, chanspec, mute);
403
404         /* update beacon listen interval */
405         wlc_bcn_li_upd(wlc);
406
407         /* the world is new again, so is our reported rate */
408         wlc_reprate_init(wlc);
409
410         /* write ethernet address to core */
411         FOREACH_BSS(wlc, i, bsscfg) {
412                 wlc_set_mac(bsscfg);
413                 wlc_set_bssid(bsscfg);
414         }
415
416         /* Update tsf_cfprep if associated and up */
417         if (wlc->pub->associated) {
418                 FOREACH_BSS(wlc, i, bsscfg) {
419                         if (bsscfg->up) {
420                                 u32 bi;
421
422                                 /* get beacon period and convert to uS */
423                                 bi = bsscfg->current_bss->beacon_period << 10;
424                                 /*
425                                  * update since init path would reset
426                                  * to default value
427                                  */
428                                 W_REG(&regs->tsf_cfprep,
429                                       (bi << CFPREP_CBI_SHIFT));
430
431                                 /* Update maccontrol PM related bits */
432                                 wlc_set_ps_ctrl(wlc);
433
434                                 break;
435                         }
436                 }
437         }
438
439         wlc_key_hw_init_all(wlc);
440
441         wlc_bandinit_ordered(wlc, chanspec);
442
443         wlc_init_scb(wlc, &global_scb);
444
445         /* init probe response timeout */
446         wlc_write_shm(wlc, M_PRS_MAXTIME, wlc->prb_resp_timeout);
447
448         /* init max burst txop (framebursting) */
449         wlc_write_shm(wlc, M_MBURST_TXOP,
450                       (wlc->
451                        _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
452
453         /* initialize maximum allowed duty cycle */
454         wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
455         wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
456
457         /* Update some shared memory locations related to max AMPDU size allowed to received */
458         wlc_ampdu_shm_upd(wlc->ampdu);
459
460         /* band-specific inits */
461         wlc_bsinit(wlc);
462
463         /* Enable EDCF mode (while the MAC is suspended) */
464         if (EDCF_ENAB(wlc->pub)) {
465                 OR_REG(&regs->ifs_ctl, IFS_USEEDCF);
466                 wlc_edcf_setparams(wlc, false);
467         }
468
469         /* Init precedence maps for empty FIFOs */
470         wlc_tx_prec_map_init(wlc);
471
472         /* read the ucode version if we have not yet done so */
473         if (wlc->ucode_rev == 0) {
474                 wlc->ucode_rev =
475                     wlc_read_shm(wlc, M_BOM_REV_MAJOR) << NBITS(u16);
476                 wlc->ucode_rev |= wlc_read_shm(wlc, M_BOM_REV_MINOR);
477         }
478
479         /* ..now really unleash hell (allow the MAC out of suspend) */
480         wlc_enable_mac(wlc);
481
482         /* clear tx flow control */
483         wlc_txflowcontrol_reset(wlc);
484
485         /* clear tx data fifo suspends */
486         wlc->tx_suspended = false;
487
488         /* enable the RF Disable Delay timer */
489         W_REG(&wlc->regs->rfdisabledly, RFDISABLE_DEFAULT);
490
491         /* initialize mpc delay */
492         wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
493
494         /*
495          * Initialize WME parameters; if they haven't been set by some other
496          * mechanism (IOVar, etc) then read them from the hardware.
497          */
498         if (WLC_WME_RETRY_SHORT_GET(wlc, 0) == 0) {     /* Uninitialized; read from HW */
499                 int ac;
500
501                 for (ac = 0; ac < AC_COUNT; ac++) {
502                         wlc->wme_retries[ac] =
503                             wlc_read_shm(wlc, M_AC_TXLMT_ADDR(ac));
504                 }
505         }
506 }
507
508 void wlc_mac_bcn_promisc_change(struct wlc_info *wlc, bool promisc)
509 {
510         wlc->bcnmisc_monitor = promisc;
511         wlc_mac_bcn_promisc(wlc);
512 }
513
514 void wlc_mac_bcn_promisc(struct wlc_info *wlc)
515 {
516         if ((AP_ENAB(wlc->pub) && (N_ENAB(wlc->pub) || wlc->band->gmode)) ||
517             wlc->bcnmisc_ibss || wlc->bcnmisc_scan || wlc->bcnmisc_monitor)
518                 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, MCTL_BCNS_PROMISC);
519         else
520                 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, 0);
521 }
522
523 /* set or clear maccontrol bits MCTL_PROMISC and MCTL_KEEPCONTROL */
524 void wlc_mac_promisc(struct wlc_info *wlc)
525 {
526         u32 promisc_bits = 0;
527
528         /* promiscuous mode just sets MCTL_PROMISC
529          * Note: APs get all BSS traffic without the need to set the MCTL_PROMISC bit
530          * since all BSS data traffic is directed at the AP
531          */
532         if (PROMISC_ENAB(wlc->pub) && !AP_ENAB(wlc->pub))
533                 promisc_bits |= MCTL_PROMISC;
534
535         /* monitor mode needs both MCTL_PROMISC and MCTL_KEEPCONTROL
536          * Note: monitor mode also needs MCTL_BCNS_PROMISC, but that is
537          * handled in wlc_mac_bcn_promisc()
538          */
539         if (MONITOR_ENAB(wlc))
540                 promisc_bits |= MCTL_PROMISC | MCTL_KEEPCONTROL;
541
542         wlc_mctrl(wlc, MCTL_PROMISC | MCTL_KEEPCONTROL, promisc_bits);
543 }
544
545 /* push sw hps and wake state through hardware */
546 void wlc_set_ps_ctrl(struct wlc_info *wlc)
547 {
548         u32 v1, v2;
549         bool hps;
550         bool awake_before;
551
552         hps = PS_ALLOWED(wlc);
553
554         BCMMSG(wlc->wiphy, "wl%d: hps %d\n", wlc->pub->unit, hps);
555
556         v1 = R_REG(&wlc->regs->maccontrol);
557         v2 = MCTL_WAKE;
558         if (hps)
559                 v2 |= MCTL_HPS;
560
561         wlc_mctrl(wlc, MCTL_WAKE | MCTL_HPS, v2);
562
563         awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
564
565         if (!awake_before)
566                 wlc_bmac_wait_for_wake(wlc->hw);
567
568 }
569
570 /*
571  * Write this BSS config's MAC address to core.
572  * Updates RXE match engine.
573  */
574 int wlc_set_mac(struct wlc_bsscfg *cfg)
575 {
576         int err = 0;
577         struct wlc_info *wlc = cfg->wlc;
578
579         if (cfg == wlc->cfg) {
580                 /* enter the MAC addr into the RXE match registers */
581                 wlc_set_addrmatch(wlc, RCM_MAC_OFFSET, cfg->cur_etheraddr);
582         }
583
584         wlc_ampdu_macaddr_upd(wlc);
585
586         return err;
587 }
588
589 /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
590  * Updates RXE match engine.
591  */
592 void wlc_set_bssid(struct wlc_bsscfg *cfg)
593 {
594         struct wlc_info *wlc = cfg->wlc;
595
596         /* if primary config, we need to update BSSID in RXE match registers */
597         if (cfg == wlc->cfg) {
598                 wlc_set_addrmatch(wlc, RCM_BSSID_OFFSET, cfg->BSSID);
599         }
600 #ifdef SUPPORT_HWKEYS
601         else if (BSSCFG_STA(cfg) && cfg->BSS) {
602                 wlc_rcmta_add_bssid(wlc, cfg);
603         }
604 #endif
605 }
606
607 /*
608  * Suspend the the MAC and update the slot timing
609  * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
610  */
611 void wlc_switch_shortslot(struct wlc_info *wlc, bool shortslot)
612 {
613         int idx;
614         struct wlc_bsscfg *cfg;
615
616         /* use the override if it is set */
617         if (wlc->shortslot_override != WLC_SHORTSLOT_AUTO)
618                 shortslot = (wlc->shortslot_override == WLC_SHORTSLOT_ON);
619
620         if (wlc->shortslot == shortslot)
621                 return;
622
623         wlc->shortslot = shortslot;
624
625         /* update the capability based on current shortslot mode */
626         FOREACH_BSS(wlc, idx, cfg) {
627                 if (!cfg->associated)
628                         continue;
629                 cfg->current_bss->capability &=
630                                         ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
631                 if (wlc->shortslot)
632                         cfg->current_bss->capability |=
633                                         WLAN_CAPABILITY_SHORT_SLOT_TIME;
634         }
635
636         wlc_bmac_set_shortslot(wlc->hw, shortslot);
637 }
638
639 static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc)
640 {
641         u8 local;
642         s16 local_max;
643
644         local = WLC_TXPWR_MAX;
645         if (wlc->pub->associated &&
646             (bcm_chspec_ctlchan(wlc->chanspec) ==
647              bcm_chspec_ctlchan(wlc->home_chanspec))) {
648
649                 /* get the local power constraint if we are on the AP's
650                  * channel [802.11h, 7.3.2.13]
651                  */
652                 /* Clamp the value between 0 and WLC_TXPWR_MAX w/o overflowing the target */
653                 local_max =
654                     (wlc->txpwr_local_max -
655                      wlc->txpwr_local_constraint) * WLC_TXPWR_DB_FACTOR;
656                 if (local_max > 0 && local_max < WLC_TXPWR_MAX)
657                         return (u8) local_max;
658                 if (local_max < 0)
659                         return 0;
660         }
661
662         return local;
663 }
664
665 /* propagate home chanspec to all bsscfgs in case bsscfg->current_bss->chanspec is referenced */
666 void wlc_set_home_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
667 {
668         if (wlc->home_chanspec != chanspec) {
669                 int idx;
670                 struct wlc_bsscfg *cfg;
671
672                 wlc->home_chanspec = chanspec;
673
674                 FOREACH_BSS(wlc, idx, cfg) {
675                         if (!cfg->associated)
676                                 continue;
677
678                         cfg->current_bss->chanspec = chanspec;
679                 }
680
681         }
682 }
683
684 static void wlc_set_phy_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
685 {
686         /* Save our copy of the chanspec */
687         wlc->chanspec = chanspec;
688
689         /* Set the chanspec and power limits for this locale after computing
690          * any 11h local tx power constraints.
691          */
692         wlc_channel_set_chanspec(wlc->cmi, chanspec,
693                                  wlc_local_constraint_qdbm(wlc));
694
695         if (wlc->stf->ss_algosel_auto)
696                 wlc_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
697                                             chanspec);
698
699         wlc_stf_ss_update(wlc, wlc->band);
700
701 }
702
703 void wlc_set_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
704 {
705         uint bandunit;
706         bool switchband = false;
707         chanspec_t old_chanspec = wlc->chanspec;
708
709         if (!wlc_valid_chanspec_db(wlc->cmi, chanspec)) {
710                 wiphy_err(wlc->wiphy, "wl%d: %s: Bad channel %d\n",
711                           wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
712                 return;
713         }
714
715         /* Switch bands if necessary */
716         if (NBANDS(wlc) > 1) {
717                 bandunit = CHSPEC_WLCBANDUNIT(chanspec);
718                 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
719                         switchband = true;
720                         if (wlc->bandlocked) {
721                                 wiphy_err(wlc->wiphy, "wl%d: %s: chspec %d "
722                                           "band is locked!\n",
723                                           wlc->pub->unit, __func__,
724                                           CHSPEC_CHANNEL(chanspec));
725                                 return;
726                         }
727                         /* BMAC_NOTE: should the setband call come after the wlc_bmac_chanspec() ?
728                          * if the setband updates (wlc_bsinit) use low level calls to inspect and
729                          * set state, the state inspected may be from the wrong band, or the
730                          * following wlc_bmac_set_chanspec() may undo the work.
731                          */
732                         wlc_setband(wlc, bandunit);
733                 }
734         }
735
736         /* sync up phy/radio chanspec */
737         wlc_set_phy_chanspec(wlc, chanspec);
738
739         /* init antenna selection */
740         if (CHSPEC_WLC_BW(old_chanspec) != CHSPEC_WLC_BW(chanspec)) {
741                 wlc_antsel_init(wlc->asi);
742
743                 /* Fix the hardware rateset based on bw.
744                  * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
745                  */
746                 wlc_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
747                                           wlc->band->
748                                           mimo_cap_40 ? CHSPEC_WLC_BW(chanspec)
749                                           : 0);
750         }
751
752         /* update some mac configuration since chanspec changed */
753         wlc_ucode_mac_upd(wlc);
754 }
755
756 ratespec_t wlc_lowest_basic_rspec(struct wlc_info *wlc, wlc_rateset_t *rs)
757 {
758         ratespec_t lowest_basic_rspec;
759         uint i;
760
761         /* Use the lowest basic rate */
762         lowest_basic_rspec = rs->rates[0] & WLC_RATE_MASK;
763         for (i = 0; i < rs->count; i++) {
764                 if (rs->rates[i] & WLC_RATE_FLAG) {
765                         lowest_basic_rspec = rs->rates[i] & WLC_RATE_MASK;
766                         break;
767                 }
768         }
769 #if NCONF
770         /* pick siso/cdd as default for OFDM (note no basic rate MCSs are supported yet) */
771         if (IS_OFDM(lowest_basic_rspec)) {
772                 lowest_basic_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
773         }
774 #endif
775
776         return lowest_basic_rspec;
777 }
778
779 /* This function changes the phytxctl for beacon based on current beacon ratespec AND txant
780  * setting as per this table:
781  *  ratespec     CCK            ant = wlc->stf->txant
782  *              OFDM            ant = 3
783  */
784 void wlc_beacon_phytxctl_txant_upd(struct wlc_info *wlc, ratespec_t bcn_rspec)
785 {
786         u16 phyctl;
787         u16 phytxant = wlc->stf->phytxant;
788         u16 mask = PHY_TXC_ANT_MASK;
789
790         /* for non-siso rates or default setting, use the available chains */
791         if (WLC_PHY_11N_CAP(wlc->band)) {
792                 phytxant = wlc_stf_phytxchain_sel(wlc, bcn_rspec);
793         }
794
795         phyctl = wlc_read_shm(wlc, M_BCN_PCTLWD);
796         phyctl = (phyctl & ~mask) | phytxant;
797         wlc_write_shm(wlc, M_BCN_PCTLWD, phyctl);
798 }
799
800 /* centralized protection config change function to simplify debugging, no consistency checking
801  * this should be called only on changes to avoid overhead in periodic function
802 */
803 void wlc_protection_upd(struct wlc_info *wlc, uint idx, int val)
804 {
805         BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
806
807         switch (idx) {
808         case WLC_PROT_G_SPEC:
809                 wlc->protection->_g = (bool) val;
810                 break;
811         case WLC_PROT_G_OVR:
812                 wlc->protection->g_override = (s8) val;
813                 break;
814         case WLC_PROT_G_USER:
815                 wlc->protection->gmode_user = (u8) val;
816                 break;
817         case WLC_PROT_OVERLAP:
818                 wlc->protection->overlap = (s8) val;
819                 break;
820         case WLC_PROT_N_USER:
821                 wlc->protection->nmode_user = (s8) val;
822                 break;
823         case WLC_PROT_N_CFG:
824                 wlc->protection->n_cfg = (s8) val;
825                 break;
826         case WLC_PROT_N_CFG_OVR:
827                 wlc->protection->n_cfg_override = (s8) val;
828                 break;
829         case WLC_PROT_N_NONGF:
830                 wlc->protection->nongf = (bool) val;
831                 break;
832         case WLC_PROT_N_NONGF_OVR:
833                 wlc->protection->nongf_override = (s8) val;
834                 break;
835         case WLC_PROT_N_PAM_OVR:
836                 wlc->protection->n_pam_override = (s8) val;
837                 break;
838         case WLC_PROT_N_OBSS:
839                 wlc->protection->n_obss = (bool) val;
840                 break;
841
842         default:
843                 break;
844         }
845
846 }
847
848 static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val)
849 {
850         wlc->ht_cap.cap_info &= ~(IEEE80211_HT_CAP_SGI_20 |
851                                         IEEE80211_HT_CAP_SGI_40);
852         wlc->ht_cap.cap_info |= (val & WLC_N_SGI_20) ?
853                                         IEEE80211_HT_CAP_SGI_20 : 0;
854         wlc->ht_cap.cap_info |= (val & WLC_N_SGI_40) ?
855                                         IEEE80211_HT_CAP_SGI_40 : 0;
856
857         if (wlc->pub->up) {
858                 wlc_update_beacon(wlc);
859                 wlc_update_probe_resp(wlc, true);
860         }
861 }
862
863 static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val)
864 {
865         wlc->stf->ldpc = val;
866
867         wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_LDPC_CODING;
868         if (wlc->stf->ldpc != OFF)
869                 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_LDPC_CODING;
870
871         if (wlc->pub->up) {
872                 wlc_update_beacon(wlc);
873                 wlc_update_probe_resp(wlc, true);
874                 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
875         }
876 }
877
878 /*
879  * ucode, hwmac update
880  *    Channel dependent updates for ucode and hw
881  */
882 static void wlc_ucode_mac_upd(struct wlc_info *wlc)
883 {
884         /* enable or disable any active IBSSs depending on whether or not
885          * we are on the home channel
886          */
887         if (wlc->home_chanspec == WLC_BAND_PI_RADIO_CHANSPEC) {
888                 if (wlc->pub->associated) {
889                         /* BMAC_NOTE: This is something that should be fixed in ucode inits.
890                          * I think that the ucode inits set up the bcn templates and shm values
891                          * with a bogus beacon. This should not be done in the inits. If ucode needs
892                          * to set up a beacon for testing, the test routines should write it down,
893                          * not expect the inits to populate a bogus beacon.
894                          */
895                         if (WLC_PHY_11N_CAP(wlc->band)) {
896                                 wlc_write_shm(wlc, M_BCN_TXTSF_OFFSET,
897                                               wlc->band->bcntsfoff);
898                         }
899                 }
900         } else {
901                 /* disable an active IBSS if we are not on the home channel */
902         }
903
904         /* update the various promisc bits */
905         wlc_mac_bcn_promisc(wlc);
906         wlc_mac_promisc(wlc);
907 }
908
909 static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec)
910 {
911         wlc_rateset_t default_rateset;
912         uint parkband;
913         uint i, band_order[2];
914
915         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
916         /*
917          * We might have been bandlocked during down and the chip power-cycled (hibernate).
918          * figure out the right band to park on
919          */
920         if (wlc->bandlocked || NBANDS(wlc) == 1) {
921                 parkband = wlc->band->bandunit; /* updated in wlc_bandlock() */
922                 band_order[0] = band_order[1] = parkband;
923         } else {
924                 /* park on the band of the specified chanspec */
925                 parkband = CHSPEC_WLCBANDUNIT(chanspec);
926
927                 /* order so that parkband initialize last */
928                 band_order[0] = parkband ^ 1;
929                 band_order[1] = parkband;
930         }
931
932         /* make each band operational, software state init */
933         for (i = 0; i < NBANDS(wlc); i++) {
934                 uint j = band_order[i];
935
936                 wlc->band = wlc->bandstate[j];
937
938                 wlc_default_rateset(wlc, &default_rateset);
939
940                 /* fill in hw_rate */
941                 wlc_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
942                                    false, WLC_RATES_CCK_OFDM, WLC_RATE_MASK,
943                                    (bool) N_ENAB(wlc->pub));
944
945                 /* init basic rate lookup */
946                 wlc_rate_lookup_init(wlc, &default_rateset);
947         }
948
949         /* sync up phy/radio chanspec */
950         wlc_set_phy_chanspec(wlc, chanspec);
951 }
952
953 /* band-specific init */
954 static void WLBANDINITFN(wlc_bsinit) (struct wlc_info *wlc)
955 {
956         BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n",
957                  wlc->pub->unit, wlc->band->bandunit);
958
959         /* write ucode ACK/CTS rate table */
960         wlc_set_ratetable(wlc);
961
962         /* update some band specific mac configuration */
963         wlc_ucode_mac_upd(wlc);
964
965         /* init antenna selection */
966         wlc_antsel_init(wlc->asi);
967
968 }
969
970 /* switch to and initialize new band */
971 static void WLBANDINITFN(wlc_setband) (struct wlc_info *wlc, uint bandunit)
972 {
973         int idx;
974         struct wlc_bsscfg *cfg;
975
976         wlc->band = wlc->bandstate[bandunit];
977
978         if (!wlc->pub->up)
979                 return;
980
981         /* wait for at least one beacon before entering sleeping state */
982         FOREACH_AS_STA(wlc, idx, cfg)
983             cfg->PMawakebcn = true;
984         wlc_set_ps_ctrl(wlc);
985
986         /* band-specific initializations */
987         wlc_bsinit(wlc);
988 }
989
990 /* Initialize a WME Parameter Info Element with default STA parameters from WMM Spec, Table 12 */
991 void wlc_wme_initparams_sta(struct wlc_info *wlc, wme_param_ie_t *pe)
992 {
993         static const wme_param_ie_t stadef = {
994                 WME_OUI,
995                 WME_TYPE,
996                 WME_SUBTYPE_PARAM_IE,
997                 WME_VER,
998                 0,
999                 0,
1000                 {
1001                  {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA,
1002                   cpu_to_le16(EDCF_AC_BE_TXOP_STA)},
1003                  {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA,
1004                   cpu_to_le16(EDCF_AC_BK_TXOP_STA)},
1005                  {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA,
1006                   cpu_to_le16(EDCF_AC_VI_TXOP_STA)},
1007                  {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA,
1008                   cpu_to_le16(EDCF_AC_VO_TXOP_STA)}
1009                  }
1010         };
1011         memcpy(pe, &stadef, sizeof(*pe));
1012 }
1013
1014 void wlc_wme_setparams(struct wlc_info *wlc, u16 aci,
1015                        const struct ieee80211_tx_queue_params *params,
1016                        bool suspend)
1017 {
1018         int i;
1019         shm_acparams_t acp_shm;
1020         u16 *shm_entry;
1021
1022         /* Only apply params if the core is out of reset and has clocks */
1023         if (!wlc->clk) {
1024                 wiphy_err(wlc->wiphy, "wl%d: %s : no-clock\n", wlc->pub->unit,
1025                           __func__);
1026                 return;
1027         }
1028
1029         do {
1030                 memset((char *)&acp_shm, 0, sizeof(shm_acparams_t));
1031                 /* fill in shm ac params struct */
1032                 acp_shm.txop = le16_to_cpu(params->txop);
1033                 /* convert from units of 32us to us for ucode */
1034                 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
1035                     EDCF_TXOP2USEC(acp_shm.txop);
1036                 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
1037
1038                 if (aci == AC_VI && acp_shm.txop == 0
1039                     && acp_shm.aifs < EDCF_AIFSN_MAX)
1040                         acp_shm.aifs++;
1041
1042                 if (acp_shm.aifs < EDCF_AIFSN_MIN
1043                     || acp_shm.aifs > EDCF_AIFSN_MAX) {
1044                         wiphy_err(wlc->wiphy, "wl%d: wlc_edcf_setparams: bad "
1045                                   "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
1046                         continue;
1047                 }
1048
1049                 acp_shm.cwmin = params->cw_min;
1050                 acp_shm.cwmax = params->cw_max;
1051                 acp_shm.cwcur = acp_shm.cwmin;
1052                 acp_shm.bslots =
1053                     R_REG(&wlc->regs->tsf_random) & acp_shm.cwcur;
1054                 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1055                 /* Indicate the new params to the ucode */
1056                 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1057                                                     wme_shmemacindex(aci) *
1058                                                     M_EDCF_QLEN +
1059                                                     M_EDCF_STATUS_OFF));
1060                 acp_shm.status |= WME_STATUS_NEWAC;
1061
1062                 /* Fill in shm acparam table */
1063                 shm_entry = (u16 *) &acp_shm;
1064                 for (i = 0; i < (int)sizeof(shm_acparams_t); i += 2)
1065                         wlc_write_shm(wlc,
1066                                       M_EDCF_QINFO +
1067                                       wme_shmemacindex(aci) * M_EDCF_QLEN + i,
1068                                       *shm_entry++);
1069
1070         } while (0);
1071
1072         if (suspend)
1073                 wlc_suspend_mac_and_wait(wlc);
1074
1075         if (suspend)
1076                 wlc_enable_mac(wlc);
1077
1078 }
1079
1080 void wlc_edcf_setparams(struct wlc_info *wlc, bool suspend)
1081 {
1082         u16 aci;
1083         int i_ac;
1084         edcf_acparam_t *edcf_acp;
1085
1086         struct ieee80211_tx_queue_params txq_pars;
1087         struct ieee80211_tx_queue_params *params = &txq_pars;
1088
1089         /*
1090          * AP uses AC params from wme_param_ie_ap.
1091          * AP advertises AC params from wme_param_ie.
1092          * STA uses AC params from wme_param_ie.
1093          */
1094
1095         edcf_acp = (edcf_acparam_t *) &wlc->wme_param_ie.acparam[0];
1096
1097         for (i_ac = 0; i_ac < AC_COUNT; i_ac++, edcf_acp++) {
1098                 /* find out which ac this set of params applies to */
1099                 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
1100
1101                 /* fill in shm ac params struct */
1102                 params->txop = edcf_acp->TXOP;
1103                 params->aifs = edcf_acp->ACI;
1104
1105                 /* CWmin = 2^(ECWmin) - 1 */
1106                 params->cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
1107                 /* CWmax = 2^(ECWmax) - 1 */
1108                 params->cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
1109                                             >> EDCF_ECWMAX_SHIFT);
1110                 wlc_wme_setparams(wlc, aci, params, suspend);
1111         }
1112
1113         if (suspend)
1114                 wlc_suspend_mac_and_wait(wlc);
1115
1116         if (AP_ENAB(wlc->pub) && WME_ENAB(wlc->pub)) {
1117                 wlc_update_beacon(wlc);
1118                 wlc_update_probe_resp(wlc, false);
1119         }
1120
1121         if (suspend)
1122                 wlc_enable_mac(wlc);
1123
1124 }
1125
1126 bool wlc_timers_init(struct wlc_info *wlc, int unit)
1127 {
1128         wlc->wdtimer = wl_init_timer(wlc->wl, wlc_watchdog_by_timer,
1129                 wlc, "watchdog");
1130         if (!wlc->wdtimer) {
1131                 wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for wdtimer "
1132                           "failed\n", unit);
1133                 goto fail;
1134         }
1135
1136         wlc->radio_timer = wl_init_timer(wlc->wl, wlc_radio_timer,
1137                 wlc, "radio");
1138         if (!wlc->radio_timer) {
1139                 wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for radio_timer "
1140                           "failed\n", unit);
1141                 goto fail;
1142         }
1143
1144         return true;
1145
1146  fail:
1147         return false;
1148 }
1149
1150 /*
1151  * Initialize wlc_info default values ...
1152  * may get overrides later in this function
1153  */
1154 void wlc_info_init(struct wlc_info *wlc, int unit)
1155 {
1156         int i;
1157         /* Assume the device is there until proven otherwise */
1158         wlc->device_present = true;
1159
1160         /* Save our copy of the chanspec */
1161         wlc->chanspec = CH20MHZ_CHSPEC(1);
1162
1163         /* various 802.11g modes */
1164         wlc->shortslot = false;
1165         wlc->shortslot_override = WLC_SHORTSLOT_AUTO;
1166
1167         wlc_protection_upd(wlc, WLC_PROT_G_OVR, WLC_PROTECTION_AUTO);
1168         wlc_protection_upd(wlc, WLC_PROT_G_SPEC, false);
1169
1170         wlc_protection_upd(wlc, WLC_PROT_N_CFG_OVR, WLC_PROTECTION_AUTO);
1171         wlc_protection_upd(wlc, WLC_PROT_N_CFG, WLC_N_PROTECTION_OFF);
1172         wlc_protection_upd(wlc, WLC_PROT_N_NONGF_OVR, WLC_PROTECTION_AUTO);
1173         wlc_protection_upd(wlc, WLC_PROT_N_NONGF, false);
1174         wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR, AUTO);
1175
1176         wlc_protection_upd(wlc, WLC_PROT_OVERLAP, WLC_PROTECTION_CTL_OVERLAP);
1177
1178         /* 802.11g draft 4.0 NonERP elt advertisement */
1179         wlc->include_legacy_erp = true;
1180
1181         wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
1182         wlc->stf->txant = ANT_TX_DEF;
1183
1184         wlc->prb_resp_timeout = WLC_PRB_RESP_TIMEOUT;
1185
1186         wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
1187         for (i = 0; i < NFIFO; i++)
1188                 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
1189         wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
1190
1191         /* default rate fallback retry limits */
1192         wlc->SFBL = RETRY_SHORT_FB;
1193         wlc->LFBL = RETRY_LONG_FB;
1194
1195         /* default mac retry limits */
1196         wlc->SRL = RETRY_SHORT_DEF;
1197         wlc->LRL = RETRY_LONG_DEF;
1198
1199         /* Set flag to indicate that hw keys should be used when available. */
1200         wlc->wsec_swkeys = false;
1201
1202         /* init the 4 static WEP default keys */
1203         for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
1204                 wlc->wsec_keys[i] = wlc->wsec_def_keys[i];
1205                 wlc->wsec_keys[i]->idx = (u8) i;
1206         }
1207
1208         /* WME QoS mode is Auto by default */
1209         wlc->pub->_wme = AUTO;
1210
1211 #ifdef BCMSDIODEV_ENABLED
1212         wlc->pub->_priofc = true;       /* enable priority flow control for sdio dongle */
1213 #endif
1214
1215         wlc->pub->_ampdu = AMPDU_AGG_HOST;
1216         wlc->pub->bcmerror = 0;
1217         wlc->pub->_coex = ON;
1218
1219         /* initialize mpc delay */
1220         wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
1221 }
1222
1223 static bool wlc_state_bmac_sync(struct wlc_info *wlc)
1224 {
1225         wlc_bmac_state_t state_bmac;
1226
1227         if (wlc_bmac_state_get(wlc->hw, &state_bmac) != 0)
1228                 return false;
1229
1230         wlc->machwcap = state_bmac.machwcap;
1231         wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR,
1232                            (s8) state_bmac.preamble_ovr);
1233
1234         return true;
1235 }
1236
1237 static uint wlc_attach_module(struct wlc_info *wlc)
1238 {
1239         uint err = 0;
1240         uint unit;
1241         unit = wlc->pub->unit;
1242
1243         wlc->asi = wlc_antsel_attach(wlc);
1244         if (wlc->asi == NULL) {
1245                 wiphy_err(wlc->wiphy, "wl%d: wlc_attach: wlc_antsel_attach "
1246                           "failed\n", unit);
1247                 err = 44;
1248                 goto fail;
1249         }
1250
1251         wlc->ampdu = wlc_ampdu_attach(wlc);
1252         if (wlc->ampdu == NULL) {
1253                 wiphy_err(wlc->wiphy, "wl%d: wlc_attach: wlc_ampdu_attach "
1254                           "failed\n", unit);
1255                 err = 50;
1256                 goto fail;
1257         }
1258
1259         if ((wlc_stf_attach(wlc) != 0)) {
1260                 wiphy_err(wlc->wiphy, "wl%d: wlc_attach: wlc_stf_attach "
1261                           "failed\n", unit);
1262                 err = 68;
1263                 goto fail;
1264         }
1265  fail:
1266         return err;
1267 }
1268
1269 struct wlc_pub *wlc_pub(void *wlc)
1270 {
1271         return ((struct wlc_info *) wlc)->pub;
1272 }
1273
1274 #define CHIP_SUPPORTS_11N(wlc)  1
1275
1276 /*
1277  * The common driver entry routine. Error codes should be unique
1278  */
1279 void *wlc_attach(struct wl_info *wl, u16 vendor, u16 device, uint unit,
1280                  bool piomode, void *regsva, uint bustype, void *btparam,
1281                  uint *perr)
1282 {
1283         struct wlc_info *wlc;
1284         uint err = 0;
1285         uint j;
1286         struct wlc_pub *pub;
1287         uint n_disabled;
1288
1289         /* allocate struct wlc_info state and its substructures */
1290         wlc = (struct wlc_info *) wlc_attach_malloc(unit, &err, device);
1291         if (wlc == NULL)
1292                 goto fail;
1293         wlc->wiphy = wl->wiphy;
1294         pub = wlc->pub;
1295
1296 #if defined(BCMDBG)
1297         wlc_info_dbg = wlc;
1298 #endif
1299
1300         wlc->band = wlc->bandstate[0];
1301         wlc->core = wlc->corestate;
1302         wlc->wl = wl;
1303         pub->unit = unit;
1304         pub->_piomode = piomode;
1305         wlc->bandinit_pending = false;
1306
1307         /* populate struct wlc_info with default values  */
1308         wlc_info_init(wlc, unit);
1309
1310         /* update sta/ap related parameters */
1311         wlc_ap_upd(wlc);
1312
1313         /* 11n_disable nvram */
1314         n_disabled = getintvar(pub->vars, "11n_disable");
1315
1316         /*
1317          * low level attach steps(all hw accesses go
1318          * inside, no more in rest of the attach)
1319          */
1320         err = wlc_bmac_attach(wlc, vendor, device, unit, piomode, regsva,
1321                               bustype, btparam);
1322         if (err)
1323                 goto fail;
1324
1325         /* for some states, due to different info pointer(e,g, wlc, wlc_hw) or master/slave split,
1326          * HIGH driver(both monolithic and HIGH_ONLY) needs to sync states FROM BMAC portion driver
1327          */
1328         if (!wlc_state_bmac_sync(wlc)) {
1329                 err = 20;
1330                 goto fail;
1331         }
1332
1333         pub->phy_11ncapable = WLC_PHY_11N_CAP(wlc->band);
1334
1335         /* propagate *vars* from BMAC driver to high driver */
1336         wlc_bmac_copyfrom_vars(wlc->hw, &pub->vars, &wlc->vars_size);
1337
1338
1339         /* set maximum allowed duty cycle */
1340         wlc->tx_duty_cycle_ofdm =
1341             (u16) getintvar(pub->vars, "tx_duty_cycle_ofdm");
1342         wlc->tx_duty_cycle_cck =
1343             (u16) getintvar(pub->vars, "tx_duty_cycle_cck");
1344
1345         wlc_stf_phy_chain_calc(wlc);
1346
1347         /* txchain 1: txant 0, txchain 2: txant 1 */
1348         if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
1349                 wlc->stf->txant = wlc->stf->hw_txchain - 1;
1350
1351         /* push to BMAC driver */
1352         wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
1353                                wlc->stf->hw_rxchain);
1354
1355         /* pull up some info resulting from the low attach */
1356         {
1357                 int i;
1358                 for (i = 0; i < NFIFO; i++)
1359                         wlc->core->txavail[i] = wlc->hw->txavail[i];
1360         }
1361
1362         wlc_bmac_hw_etheraddr(wlc->hw, wlc->perm_etheraddr);
1363
1364         memcpy(&pub->cur_etheraddr, &wlc->perm_etheraddr, ETH_ALEN);
1365
1366         for (j = 0; j < NBANDS(wlc); j++) {
1367                 /* Use band 1 for single band 11a */
1368                 if (IS_SINGLEBAND_5G(wlc->deviceid))
1369                         j = BAND_5G_INDEX;
1370
1371                 wlc->band = wlc->bandstate[j];
1372
1373                 if (!wlc_attach_stf_ant_init(wlc)) {
1374                         err = 24;
1375                         goto fail;
1376                 }
1377
1378                 /* default contention windows size limits */
1379                 wlc->band->CWmin = APHY_CWMIN;
1380                 wlc->band->CWmax = PHY_CWMAX;
1381
1382                 /* init gmode value */
1383                 if (BAND_2G(wlc->band->bandtype)) {
1384                         wlc->band->gmode = GMODE_AUTO;
1385                         wlc_protection_upd(wlc, WLC_PROT_G_USER,
1386                                            wlc->band->gmode);
1387                 }
1388
1389                 /* init _n_enab supported mode */
1390                 if (WLC_PHY_11N_CAP(wlc->band) && CHIP_SUPPORTS_11N(wlc)) {
1391                         if (n_disabled & WLFEATURE_DISABLE_11N) {
1392                                 pub->_n_enab = OFF;
1393                                 wlc_protection_upd(wlc, WLC_PROT_N_USER, OFF);
1394                         } else {
1395                                 pub->_n_enab = SUPPORT_11N;
1396                                 wlc_protection_upd(wlc, WLC_PROT_N_USER,
1397                                                    ((pub->_n_enab ==
1398                                                      SUPPORT_11N) ? WL_11N_2x2 :
1399                                                     WL_11N_3x3));
1400                         }
1401                 }
1402
1403                 /* init per-band default rateset, depend on band->gmode */
1404                 wlc_default_rateset(wlc, &wlc->band->defrateset);
1405
1406                 /* fill in hw_rateset (used early by WLC_SET_RATESET) */
1407                 wlc_rateset_filter(&wlc->band->defrateset,
1408                                    &wlc->band->hw_rateset, false,
1409                                    WLC_RATES_CCK_OFDM, WLC_RATE_MASK,
1410                                    (bool) N_ENAB(wlc->pub));
1411         }
1412
1413         /* update antenna config due to wlc->stf->txant/txchain/ant_rx_ovr change */
1414         wlc_stf_phy_txant_upd(wlc);
1415
1416         /* attach each modules */
1417         err = wlc_attach_module(wlc);
1418         if (err != 0)
1419                 goto fail;
1420
1421         if (!wlc_timers_init(wlc, unit)) {
1422                 wiphy_err(wl->wiphy, "wl%d: %s: wlc_init_timer failed\n", unit,
1423                           __func__);
1424                 err = 32;
1425                 goto fail;
1426         }
1427
1428         /* depend on rateset, gmode */
1429         wlc->cmi = wlc_channel_mgr_attach(wlc);
1430         if (!wlc->cmi) {
1431                 wiphy_err(wl->wiphy, "wl%d: %s: wlc_channel_mgr_attach failed"
1432                           "\n", unit, __func__);
1433                 err = 33;
1434                 goto fail;
1435         }
1436
1437         /* init default when all parameters are ready, i.e. ->rateset */
1438         wlc_bss_default_init(wlc);
1439
1440         /*
1441          * Complete the wlc default state initializations..
1442          */
1443
1444         /* allocate our initial queue */
1445         wlc->pkt_queue = wlc_txq_alloc(wlc);
1446         if (wlc->pkt_queue == NULL) {
1447                 wiphy_err(wl->wiphy, "wl%d: %s: failed to malloc tx queue\n",
1448                           unit, __func__);
1449                 err = 100;
1450                 goto fail;
1451         }
1452
1453         wlc->bsscfg[0] = wlc->cfg;
1454         wlc->cfg->_idx = 0;
1455         wlc->cfg->wlc = wlc;
1456         pub->txmaxpkts = MAXTXPKTS;
1457
1458         wlc_wme_initparams_sta(wlc, &wlc->wme_param_ie);
1459
1460         wlc->mimoft = FT_HT;
1461         wlc->ht_cap.cap_info = HT_CAP;
1462         if (HT_ENAB(wlc->pub))
1463                 wlc->stf->ldpc = AUTO;
1464
1465         wlc->mimo_40txbw = AUTO;
1466         wlc->ofdm_40txbw = AUTO;
1467         wlc->cck_40txbw = AUTO;
1468         wlc_update_mimo_band_bwcap(wlc, WLC_N_BW_20IN2G_40IN5G);
1469
1470         /* Set default values of SGI */
1471         if (WLC_SGI_CAP_PHY(wlc)) {
1472                 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1473                 wlc->sgi_tx = AUTO;
1474         } else if (WLCISSSLPNPHY(wlc->band)) {
1475                 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1476                 wlc->sgi_tx = AUTO;
1477         } else {
1478                 wlc_ht_update_sgi_rx(wlc, 0);
1479                 wlc->sgi_tx = OFF;
1480         }
1481
1482         /* *******nvram 11n config overrides Start ********* */
1483
1484         /* apply the sgi override from nvram conf */
1485         if (n_disabled & WLFEATURE_DISABLE_11N_SGI_TX)
1486                 wlc->sgi_tx = OFF;
1487
1488         if (n_disabled & WLFEATURE_DISABLE_11N_SGI_RX)
1489                 wlc_ht_update_sgi_rx(wlc, 0);
1490
1491         /* apply the stbc override from nvram conf */
1492         if (n_disabled & WLFEATURE_DISABLE_11N_STBC_TX) {
1493                 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
1494                 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
1495                 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
1496         }
1497         if (n_disabled & WLFEATURE_DISABLE_11N_STBC_RX)
1498                 wlc_stf_stbc_rx_set(wlc, HT_CAP_RX_STBC_NO);
1499
1500         /* apply the GF override from nvram conf */
1501         if (n_disabled & WLFEATURE_DISABLE_11N_GF)
1502                 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_GRN_FLD;
1503
1504         /* initialize radio_mpc_disable according to wlc->mpc */
1505         wlc_radio_mpc_upd(wlc);
1506
1507         if ((wlc->pub->sih->chip) == BCM43235_CHIP_ID) {
1508                 if ((getintvar(wlc->pub->vars, "aa2g") == 7) ||
1509                     (getintvar(wlc->pub->vars, "aa5g") == 7)) {
1510                         wlc_bmac_antsel_set(wlc->hw, 1);
1511                 }
1512         } else {
1513                 wlc_bmac_antsel_set(wlc->hw, wlc->asi->antsel_avail);
1514         }
1515
1516         if (perr)
1517                 *perr = 0;
1518
1519         return (void *)wlc;
1520
1521  fail:
1522         wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
1523                   unit, __func__, err);
1524         if (wlc)
1525                 wlc_detach(wlc);
1526
1527         if (perr)
1528                 *perr = err;
1529         return NULL;
1530 }
1531
1532 static void wlc_attach_antgain_init(struct wlc_info *wlc)
1533 {
1534         uint unit;
1535         unit = wlc->pub->unit;
1536
1537         if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
1538                 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
1539                 wlc->band->antgain = 8;
1540         } else if (wlc->band->antgain == -1) {
1541                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
1542                           " srom, using 2dB\n", unit, __func__);
1543                 wlc->band->antgain = 8;
1544         } else {
1545                 s8 gain, fract;
1546                 /* Older sroms specified gain in whole dbm only.  In order
1547                  * be able to specify qdbm granularity and remain backward compatible
1548                  * the whole dbms are now encoded in only low 6 bits and remaining qdbms
1549                  * are encoded in the hi 2 bits. 6 bit signed number ranges from
1550                  * -32 - 31. Examples: 0x1 = 1 db,
1551                  * 0xc1 = 1.75 db (1 + 3 quarters),
1552                  * 0x3f = -1 (-1 + 0 quarters),
1553                  * 0x7f = -.75 (-1 in low 6 bits + 1 quarters in hi 2 bits) = -3 qdbm.
1554                  * 0xbf = -.50 (-1 in low 6 bits + 2 quarters in hi 2 bits) = -2 qdbm.
1555                  */
1556                 gain = wlc->band->antgain & 0x3f;
1557                 gain <<= 2;     /* Sign extend */
1558                 gain >>= 2;
1559                 fract = (wlc->band->antgain & 0xc0) >> 6;
1560                 wlc->band->antgain = 4 * gain + fract;
1561         }
1562 }
1563
1564 static bool wlc_attach_stf_ant_init(struct wlc_info *wlc)
1565 {
1566         int aa;
1567         uint unit;
1568         char *vars;
1569         int bandtype;
1570
1571         unit = wlc->pub->unit;
1572         vars = wlc->pub->vars;
1573         bandtype = wlc->band->bandtype;
1574
1575         /* get antennas available */
1576         aa = (s8) getintvar(vars, (BAND_5G(bandtype) ? "aa5g" : "aa2g"));
1577         if (aa == 0)
1578                 aa = (s8) getintvar(vars,
1579                                       (BAND_5G(bandtype) ? "aa1" : "aa0"));
1580         if ((aa < 1) || (aa > 15)) {
1581                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
1582                           " srom (0x%x), using 3\n", unit, __func__, aa);
1583                 aa = 3;
1584         }
1585
1586         /* reset the defaults if we have a single antenna */
1587         if (aa == 1) {
1588                 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
1589                 wlc->stf->txant = ANT_TX_FORCE_0;
1590         } else if (aa == 2) {
1591                 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
1592                 wlc->stf->txant = ANT_TX_FORCE_1;
1593         } else {
1594         }
1595
1596         /* Compute Antenna Gain */
1597         wlc->band->antgain =
1598             (s8) getintvar(vars, (BAND_5G(bandtype) ? "ag1" : "ag0"));
1599         wlc_attach_antgain_init(wlc);
1600
1601         return true;
1602 }
1603
1604
1605 static void wlc_timers_deinit(struct wlc_info *wlc)
1606 {
1607         /* free timer state */
1608         if (wlc->wdtimer) {
1609                 wl_free_timer(wlc->wl, wlc->wdtimer);
1610                 wlc->wdtimer = NULL;
1611         }
1612         if (wlc->radio_timer) {
1613                 wl_free_timer(wlc->wl, wlc->radio_timer);
1614                 wlc->radio_timer = NULL;
1615         }
1616 }
1617
1618 static void wlc_detach_module(struct wlc_info *wlc)
1619 {
1620         if (wlc->asi) {
1621                 wlc_antsel_detach(wlc->asi);
1622                 wlc->asi = NULL;
1623         }
1624
1625         if (wlc->ampdu) {
1626                 wlc_ampdu_detach(wlc->ampdu);
1627                 wlc->ampdu = NULL;
1628         }
1629
1630         wlc_stf_detach(wlc);
1631 }
1632
1633 /*
1634  * Return a count of the number of driver callbacks still pending.
1635  *
1636  * General policy is that wlc_detach can only dealloc/free software states. It can NOT
1637  *  touch hardware registers since the d11core may be in reset and clock may not be available.
1638  *    One exception is sb register access, which is possible if crystal is turned on
1639  * After "down" state, driver should avoid software timer with the exception of radio_monitor.
1640  */
1641 uint wlc_detach(struct wlc_info *wlc)
1642 {
1643         uint callbacks = 0;
1644
1645         if (wlc == NULL)
1646                 return 0;
1647
1648         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
1649
1650         callbacks += wlc_bmac_detach(wlc);
1651
1652         /* delete software timers */
1653         if (!wlc_radio_monitor_stop(wlc))
1654                 callbacks++;
1655
1656         wlc_channel_mgr_detach(wlc->cmi);
1657
1658         wlc_timers_deinit(wlc);
1659
1660         wlc_detach_module(wlc);
1661
1662
1663         while (wlc->tx_queues != NULL)
1664                 wlc_txq_free(wlc, wlc->tx_queues);
1665
1666         wlc_detach_mfree(wlc);
1667         return callbacks;
1668 }
1669
1670 /* update state that depends on the current value of "ap" */
1671 void wlc_ap_upd(struct wlc_info *wlc)
1672 {
1673         if (AP_ENAB(wlc->pub))
1674                 wlc->PLCPHdr_override = WLC_PLCP_AUTO;  /* AP: short not allowed, but not enforced */
1675         else
1676                 wlc->PLCPHdr_override = WLC_PLCP_SHORT; /* STA-BSS; short capable */
1677
1678         /* fixup mpc */
1679         wlc->mpc = true;
1680 }
1681
1682 /* read hwdisable state and propagate to wlc flag */
1683 static void wlc_radio_hwdisable_upd(struct wlc_info *wlc)
1684 {
1685         if (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO || wlc->pub->hw_off)
1686                 return;
1687
1688         if (wlc_bmac_radio_read_hwdisabled(wlc->hw)) {
1689                 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
1690         } else {
1691                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
1692         }
1693 }
1694
1695 /* return true if Minimum Power Consumption should be entered, false otherwise */
1696 bool wlc_is_non_delay_mpc(struct wlc_info *wlc)
1697 {
1698         return false;
1699 }
1700
1701 bool wlc_ismpc(struct wlc_info *wlc)
1702 {
1703         return (wlc->mpc_delay_off == 0) && (wlc_is_non_delay_mpc(wlc));
1704 }
1705
1706 void wlc_radio_mpc_upd(struct wlc_info *wlc)
1707 {
1708         bool mpc_radio, radio_state;
1709
1710         /*
1711          * Clear the WL_RADIO_MPC_DISABLE bit when mpc feature is disabled
1712          * in case the WL_RADIO_MPC_DISABLE bit was set. Stop the radio
1713          * monitor also when WL_RADIO_MPC_DISABLE is the only reason that
1714          * the radio is going down.
1715          */
1716         if (!wlc->mpc) {
1717                 if (!wlc->pub->radio_disabled)
1718                         return;
1719                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
1720                 wlc_radio_upd(wlc);
1721                 if (!wlc->pub->radio_disabled)
1722                         wlc_radio_monitor_stop(wlc);
1723                 return;
1724         }
1725
1726         /*
1727          * sync ismpc logic with WL_RADIO_MPC_DISABLE bit in wlc->pub->radio_disabled
1728          * to go ON, always call radio_upd synchronously
1729          * to go OFF, postpone radio_upd to later when context is safe(e.g. watchdog)
1730          */
1731         radio_state =
1732             (mboolisset(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE) ? OFF :
1733              ON);
1734         mpc_radio = (wlc_ismpc(wlc) == true) ? OFF : ON;
1735
1736         if (radio_state == ON && mpc_radio == OFF)
1737                 wlc->mpc_delay_off = wlc->mpc_dlycnt;
1738         else if (radio_state == OFF && mpc_radio == ON) {
1739                 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
1740                 wlc_radio_upd(wlc);
1741                 if (wlc->mpc_offcnt < WLC_MPC_THRESHOLD) {
1742                         wlc->mpc_dlycnt = WLC_MPC_MAX_DELAYCNT;
1743                 } else
1744                         wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
1745                 wlc->mpc_dur += OSL_SYSUPTIME() - wlc->mpc_laston_ts;
1746         }
1747         /* Below logic is meant to capture the transition from mpc off to mpc on for reasons
1748          * other than wlc->mpc_delay_off keeping the mpc off. In that case reset
1749          * wlc->mpc_delay_off to wlc->mpc_dlycnt, so that we restart the countdown of mpc_delay_off
1750          */
1751         if ((wlc->prev_non_delay_mpc == false) &&
1752             (wlc_is_non_delay_mpc(wlc) == true) && wlc->mpc_delay_off) {
1753                 wlc->mpc_delay_off = wlc->mpc_dlycnt;
1754         }
1755         wlc->prev_non_delay_mpc = wlc_is_non_delay_mpc(wlc);
1756 }
1757
1758 /*
1759  * centralized radio disable/enable function,
1760  * invoke radio enable/disable after updating hwradio status
1761  */
1762 static void wlc_radio_upd(struct wlc_info *wlc)
1763 {
1764         if (wlc->pub->radio_disabled) {
1765                 wlc_radio_disable(wlc);
1766         } else {
1767                 wlc_radio_enable(wlc);
1768         }
1769 }
1770
1771 /* maintain LED behavior in down state */
1772 static void wlc_down_led_upd(struct wlc_info *wlc)
1773 {
1774         /* maintain LEDs while in down state, turn on sbclk if not available yet */
1775         /* turn on sbclk if necessary */
1776         if (!AP_ENAB(wlc->pub)) {
1777                 wlc_pllreq(wlc, true, WLC_PLLREQ_FLIP);
1778
1779                 wlc_pllreq(wlc, false, WLC_PLLREQ_FLIP);
1780         }
1781 }
1782
1783 /* update hwradio status and return it */
1784 bool wlc_check_radio_disabled(struct wlc_info *wlc)
1785 {
1786         wlc_radio_hwdisable_upd(wlc);
1787
1788         return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ? true : false;
1789 }
1790
1791 void wlc_radio_disable(struct wlc_info *wlc)
1792 {
1793         if (!wlc->pub->up) {
1794                 wlc_down_led_upd(wlc);
1795                 return;
1796         }
1797
1798         wlc_radio_monitor_start(wlc);
1799         wl_down(wlc->wl);
1800 }
1801
1802 static void wlc_radio_enable(struct wlc_info *wlc)
1803 {
1804         if (wlc->pub->up)
1805                 return;
1806
1807         if (DEVICEREMOVED(wlc))
1808                 return;
1809
1810         wl_up(wlc->wl);
1811 }
1812
1813 /* periodical query hw radio button while driver is "down" */
1814 static void wlc_radio_timer(void *arg)
1815 {
1816         struct wlc_info *wlc = (struct wlc_info *) arg;
1817
1818         if (DEVICEREMOVED(wlc)) {
1819                 wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n", wlc->pub->unit,
1820                         __func__);
1821                 wl_down(wlc->wl);
1822                 return;
1823         }
1824
1825         /* cap mpc off count */
1826         if (wlc->mpc_offcnt < WLC_MPC_MAX_DELAYCNT)
1827                 wlc->mpc_offcnt++;
1828
1829         wlc_radio_hwdisable_upd(wlc);
1830         wlc_radio_upd(wlc);
1831 }
1832
1833 static bool wlc_radio_monitor_start(struct wlc_info *wlc)
1834 {
1835         /* Don't start the timer if HWRADIO feature is disabled */
1836         if (wlc->radio_monitor || (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO))
1837                 return true;
1838
1839         wlc->radio_monitor = true;
1840         wlc_pllreq(wlc, true, WLC_PLLREQ_RADIO_MON);
1841         wl_add_timer(wlc->wl, wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
1842         return true;
1843 }
1844
1845 bool wlc_radio_monitor_stop(struct wlc_info *wlc)
1846 {
1847         if (!wlc->radio_monitor)
1848                 return true;
1849
1850         wlc->radio_monitor = false;
1851         wlc_pllreq(wlc, false, WLC_PLLREQ_RADIO_MON);
1852         return wl_del_timer(wlc->wl, wlc->radio_timer);
1853 }
1854
1855 static void wlc_watchdog_by_timer(void *arg)
1856 {
1857         wlc_watchdog(arg);
1858 }
1859
1860 /* common watchdog code */
1861 static void wlc_watchdog(void *arg)
1862 {
1863         struct wlc_info *wlc = (struct wlc_info *) arg;
1864         int i;
1865         struct wlc_bsscfg *cfg;
1866
1867         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
1868
1869         if (!wlc->pub->up)
1870                 return;
1871
1872         if (DEVICEREMOVED(wlc)) {
1873                 wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n", wlc->pub->unit,
1874                           __func__);
1875                 wl_down(wlc->wl);
1876                 return;
1877         }
1878
1879         /* increment second count */
1880         wlc->pub->now++;
1881
1882         /* delay radio disable */
1883         if (wlc->mpc_delay_off) {
1884                 if (--wlc->mpc_delay_off == 0) {
1885                         mboolset(wlc->pub->radio_disabled,
1886                                  WL_RADIO_MPC_DISABLE);
1887                         if (wlc->mpc && wlc_ismpc(wlc))
1888                                 wlc->mpc_offcnt = 0;
1889                         wlc->mpc_laston_ts = OSL_SYSUPTIME();
1890                 }
1891         }
1892
1893         /* mpc sync */
1894         wlc_radio_mpc_upd(wlc);
1895         /* radio sync: sw/hw/mpc --> radio_disable/radio_enable */
1896         wlc_radio_hwdisable_upd(wlc);
1897         wlc_radio_upd(wlc);
1898         /* if radio is disable, driver may be down, quit here */
1899         if (wlc->pub->radio_disabled)
1900                 return;
1901
1902         wlc_bmac_watchdog(wlc);
1903
1904         /* occasionally sample mac stat counters to detect 16-bit counter wrap */
1905         if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
1906                 wlc_statsupd(wlc);
1907
1908         /* Manage TKIP countermeasures timers */
1909         FOREACH_BSS(wlc, i, cfg) {
1910                 if (cfg->tk_cm_dt) {
1911                         cfg->tk_cm_dt--;
1912                 }
1913                 if (cfg->tk_cm_bt) {
1914                         cfg->tk_cm_bt--;
1915                 }
1916         }
1917
1918         /* Call any registered watchdog handlers */
1919         for (i = 0; i < WLC_MAXMODULES; i++) {
1920                 if (wlc->modulecb[i].watchdog_fn)
1921                         wlc->modulecb[i].watchdog_fn(wlc->modulecb[i].hdl);
1922         }
1923
1924         if (WLCISNPHY(wlc->band) && !wlc->pub->tempsense_disable &&
1925             ((wlc->pub->now - wlc->tempsense_lasttime) >=
1926              WLC_TEMPSENSE_PERIOD)) {
1927                 wlc->tempsense_lasttime = wlc->pub->now;
1928                 wlc_tempsense_upd(wlc);
1929         }
1930 }
1931
1932 /* make interface operational */
1933 int wlc_up(struct wlc_info *wlc)
1934 {
1935         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
1936
1937         /* HW is turned off so don't try to access it */
1938         if (wlc->pub->hw_off || DEVICEREMOVED(wlc))
1939                 return -ENOMEDIUM;
1940
1941         if (!wlc->pub->hw_up) {
1942                 wlc_bmac_hw_up(wlc->hw);
1943                 wlc->pub->hw_up = true;
1944         }
1945
1946         if ((wlc->pub->boardflags & BFL_FEM)
1947             && (wlc->pub->sih->chip == BCM4313_CHIP_ID)) {
1948                 if (wlc->pub->boardrev >= 0x1250
1949                     && (wlc->pub->boardflags & BFL_FEM_BT)) {
1950                         wlc_mhf(wlc, MHF5, MHF5_4313_GPIOCTRL,
1951                                 MHF5_4313_GPIOCTRL, WLC_BAND_ALL);
1952                 } else {
1953                         wlc_mhf(wlc, MHF4, MHF4_EXTPA_ENABLE, MHF4_EXTPA_ENABLE,
1954                                 WLC_BAND_ALL);
1955                 }
1956         }
1957
1958         /*
1959          * Need to read the hwradio status here to cover the case where the system
1960          * is loaded with the hw radio disabled. We do not want to bring the driver up in this case.
1961          * if radio is disabled, abort up, lower power, start radio timer and return 0(for NDIS)
1962          * don't call radio_update to avoid looping wlc_up.
1963          *
1964          * wlc_bmac_up_prep() returns either 0 or -BCME_RADIOOFF only
1965          */
1966         if (!wlc->pub->radio_disabled) {
1967                 int status = wlc_bmac_up_prep(wlc->hw);
1968                 if (status == -ENOMEDIUM) {
1969                         if (!mboolisset
1970                             (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
1971                                 int idx;
1972                                 struct wlc_bsscfg *bsscfg;
1973                                 mboolset(wlc->pub->radio_disabled,
1974                                          WL_RADIO_HW_DISABLE);
1975
1976                                 FOREACH_BSS(wlc, idx, bsscfg) {
1977                                         if (!BSSCFG_STA(bsscfg)
1978                                             || !bsscfg->enable || !bsscfg->BSS)
1979                                                 continue;
1980                                         wiphy_err(wlc->wiphy, "wl%d.%d: wlc_up"
1981                                                   ": rfdisable -> "
1982                                                   "wlc_bsscfg_disable()\n",
1983                                                    wlc->pub->unit, idx);
1984                                 }
1985                         }
1986                 }
1987         }
1988
1989         if (wlc->pub->radio_disabled) {
1990                 wlc_radio_monitor_start(wlc);
1991                 return 0;
1992         }
1993
1994         /* wlc_bmac_up_prep has done wlc_corereset(). so clk is on, set it */
1995         wlc->clk = true;
1996
1997         wlc_radio_monitor_stop(wlc);
1998
1999         /* Set EDCF hostflags */
2000         if (EDCF_ENAB(wlc->pub)) {
2001                 wlc_mhf(wlc, MHF1, MHF1_EDCF, MHF1_EDCF, WLC_BAND_ALL);
2002         } else {
2003                 wlc_mhf(wlc, MHF1, MHF1_EDCF, 0, WLC_BAND_ALL);
2004         }
2005
2006         if (WLC_WAR16165(wlc))
2007                 wlc_mhf(wlc, MHF2, MHF2_PCISLOWCLKWAR, MHF2_PCISLOWCLKWAR,
2008                         WLC_BAND_ALL);
2009
2010         wl_init(wlc->wl);
2011         wlc->pub->up = true;
2012
2013         if (wlc->bandinit_pending) {
2014                 wlc_suspend_mac_and_wait(wlc);
2015                 wlc_set_chanspec(wlc, wlc->default_bss->chanspec);
2016                 wlc->bandinit_pending = false;
2017                 wlc_enable_mac(wlc);
2018         }
2019
2020         wlc_bmac_up_finish(wlc->hw);
2021
2022         /* other software states up after ISR is running */
2023         /* start APs that were to be brought up but are not up  yet */
2024         /* if (AP_ENAB(wlc->pub)) wlc_restart_ap(wlc->ap); */
2025
2026         /* Program the TX wme params with the current settings */
2027         wlc_wme_retries_write(wlc);
2028
2029         /* start one second watchdog timer */
2030         wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
2031         wlc->WDarmed = true;
2032
2033         /* ensure antenna config is up to date */
2034         wlc_stf_phy_txant_upd(wlc);
2035         /* ensure LDPC config is in sync */
2036         wlc_ht_update_ldpc(wlc, wlc->stf->ldpc);
2037
2038         return 0;
2039 }
2040
2041 /* Initialize the base precedence map for dequeueing from txq based on WME settings */
2042 static void wlc_tx_prec_map_init(struct wlc_info *wlc)
2043 {
2044         wlc->tx_prec_map = WLC_PREC_BMP_ALL;
2045         memset(wlc->fifo2prec_map, 0, NFIFO * sizeof(u16));
2046
2047         /* For non-WME, both fifos have overlapping MAXPRIO. So just disable all precedences
2048          * if either is full.
2049          */
2050         if (!EDCF_ENAB(wlc->pub)) {
2051                 wlc->fifo2prec_map[TX_DATA_FIFO] = WLC_PREC_BMP_ALL;
2052                 wlc->fifo2prec_map[TX_CTL_FIFO] = WLC_PREC_BMP_ALL;
2053         } else {
2054                 wlc->fifo2prec_map[TX_AC_BK_FIFO] = WLC_PREC_BMP_AC_BK;
2055                 wlc->fifo2prec_map[TX_AC_BE_FIFO] = WLC_PREC_BMP_AC_BE;
2056                 wlc->fifo2prec_map[TX_AC_VI_FIFO] = WLC_PREC_BMP_AC_VI;
2057                 wlc->fifo2prec_map[TX_AC_VO_FIFO] = WLC_PREC_BMP_AC_VO;
2058         }
2059 }
2060
2061 static uint wlc_down_del_timer(struct wlc_info *wlc)
2062 {
2063         uint callbacks = 0;
2064
2065         return callbacks;
2066 }
2067
2068 /*
2069  * Mark the interface nonoperational, stop the software mechanisms,
2070  * disable the hardware, free any transient buffer state.
2071  * Return a count of the number of driver callbacks still pending.
2072  */
2073 uint wlc_down(struct wlc_info *wlc)
2074 {
2075
2076         uint callbacks = 0;
2077         int i;
2078         bool dev_gone = false;
2079         struct wlc_txq_info *qi;
2080
2081         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
2082
2083         /* check if we are already in the going down path */
2084         if (wlc->going_down) {
2085                 wiphy_err(wlc->wiphy, "wl%d: %s: Driver going down so return"
2086                           "\n", wlc->pub->unit, __func__);
2087                 return 0;
2088         }
2089         if (!wlc->pub->up)
2090                 return callbacks;
2091
2092         /* in between, mpc could try to bring down again.. */
2093         wlc->going_down = true;
2094
2095         callbacks += wlc_bmac_down_prep(wlc->hw);
2096
2097         dev_gone = DEVICEREMOVED(wlc);
2098
2099         /* Call any registered down handlers */
2100         for (i = 0; i < WLC_MAXMODULES; i++) {
2101                 if (wlc->modulecb[i].down_fn)
2102                         callbacks +=
2103                             wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
2104         }
2105
2106         /* cancel the watchdog timer */
2107         if (wlc->WDarmed) {
2108                 if (!wl_del_timer(wlc->wl, wlc->wdtimer))
2109                         callbacks++;
2110                 wlc->WDarmed = false;
2111         }
2112         /* cancel all other timers */
2113         callbacks += wlc_down_del_timer(wlc);
2114
2115         wlc->pub->up = false;
2116
2117         wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
2118
2119         /* clear txq flow control */
2120         wlc_txflowcontrol_reset(wlc);
2121
2122         /* flush tx queues */
2123         for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
2124                 bcm_pktq_flush(&qi->q, true, NULL, NULL);
2125         }
2126
2127         callbacks += wlc_bmac_down_finish(wlc->hw);
2128
2129         /* wlc_bmac_down_finish has done wlc_coredisable(). so clk is off */
2130         wlc->clk = false;
2131
2132         wlc->going_down = false;
2133         return callbacks;
2134 }
2135
2136 /* Set the current gmode configuration */
2137 int wlc_set_gmode(struct wlc_info *wlc, u8 gmode, bool config)
2138 {
2139         int ret = 0;
2140         uint i;
2141         wlc_rateset_t rs;
2142         /* Default to 54g Auto */
2143         s8 shortslot = WLC_SHORTSLOT_AUTO;      /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
2144         bool shortslot_restrict = false;        /* Restrict association to stations that support shortslot
2145                                                  */
2146         bool ofdm_basic = false;        /* Make 6, 12, and 24 basic rates */
2147         int preamble = WLC_PLCP_LONG;   /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
2148         bool preamble_restrict = false; /* Restrict association to stations that support short
2149                                          * preambles
2150                                          */
2151         struct wlcband *band;
2152
2153         /* if N-support is enabled, allow Gmode set as long as requested
2154          * Gmode is not GMODE_LEGACY_B
2155          */
2156         if (N_ENAB(wlc->pub) && gmode == GMODE_LEGACY_B)
2157                 return -ENOTSUPP;
2158
2159         /* verify that we are dealing with 2G band and grab the band pointer */
2160         if (wlc->band->bandtype == WLC_BAND_2G)
2161                 band = wlc->band;
2162         else if ((NBANDS(wlc) > 1) &&
2163                  (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == WLC_BAND_2G))
2164                 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
2165         else
2166                 return -EINVAL;
2167
2168         /* Legacy or bust when no OFDM is supported by regulatory */
2169         if ((wlc_channel_locale_flags_in_band(wlc->cmi, band->bandunit) &
2170              WLC_NO_OFDM) && (gmode != GMODE_LEGACY_B))
2171                 return -EINVAL;
2172
2173         /* update configuration value */
2174         if (config == true)
2175                 wlc_protection_upd(wlc, WLC_PROT_G_USER, gmode);
2176
2177         /* Clear supported rates filter */
2178         memset(&wlc->sup_rates_override, 0, sizeof(wlc_rateset_t));
2179
2180         /* Clear rateset override */
2181         memset(&rs, 0, sizeof(wlc_rateset_t));
2182
2183         switch (gmode) {
2184         case GMODE_LEGACY_B:
2185                 shortslot = WLC_SHORTSLOT_OFF;
2186                 wlc_rateset_copy(&gphy_legacy_rates, &rs);
2187
2188                 break;
2189
2190         case GMODE_LRS:
2191                 if (AP_ENAB(wlc->pub))
2192                         wlc_rateset_copy(&cck_rates, &wlc->sup_rates_override);
2193                 break;
2194
2195         case GMODE_AUTO:
2196                 /* Accept defaults */
2197                 break;
2198
2199         case GMODE_ONLY:
2200                 ofdm_basic = true;
2201                 preamble = WLC_PLCP_SHORT;
2202                 preamble_restrict = true;
2203                 break;
2204
2205         case GMODE_PERFORMANCE:
2206                 if (AP_ENAB(wlc->pub))  /* Put all rates into the Supported Rates element */
2207                         wlc_rateset_copy(&cck_ofdm_rates,
2208                                          &wlc->sup_rates_override);
2209
2210                 shortslot = WLC_SHORTSLOT_ON;
2211                 shortslot_restrict = true;
2212                 ofdm_basic = true;
2213                 preamble = WLC_PLCP_SHORT;
2214                 preamble_restrict = true;
2215                 break;
2216
2217         default:
2218                 /* Error */
2219                 wiphy_err(wlc->wiphy, "wl%d: %s: invalid gmode %d\n",
2220                           wlc->pub->unit, __func__, gmode);
2221                 return -ENOTSUPP;
2222         }
2223
2224         /*
2225          * If we are switching to gmode == GMODE_LEGACY_B,
2226          * clean up rate info that may refer to OFDM rates.
2227          */
2228         if ((gmode == GMODE_LEGACY_B) && (band->gmode != GMODE_LEGACY_B)) {
2229                 band->gmode = gmode;
2230                 if (band->rspec_override && !IS_CCK(band->rspec_override)) {
2231                         band->rspec_override = 0;
2232                         wlc_reprate_init(wlc);
2233                 }
2234                 if (band->mrspec_override && !IS_CCK(band->mrspec_override)) {
2235                         band->mrspec_override = 0;
2236                 }
2237         }
2238
2239         band->gmode = gmode;
2240
2241         wlc->shortslot_override = shortslot;
2242
2243         if (AP_ENAB(wlc->pub)) {
2244                 /* wlc->ap->shortslot_restrict = shortslot_restrict; */
2245                 wlc->PLCPHdr_override =
2246                     (preamble !=
2247                      WLC_PLCP_LONG) ? WLC_PLCP_SHORT : WLC_PLCP_AUTO;
2248         }
2249
2250         if ((AP_ENAB(wlc->pub) && preamble != WLC_PLCP_LONG)
2251             || preamble == WLC_PLCP_SHORT)
2252                 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
2253         else
2254                 wlc->default_bss->capability &= ~WLAN_CAPABILITY_SHORT_PREAMBLE;
2255
2256         /* Update shortslot capability bit for AP and IBSS */
2257         if ((AP_ENAB(wlc->pub) && shortslot == WLC_SHORTSLOT_AUTO) ||
2258             shortslot == WLC_SHORTSLOT_ON)
2259                 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
2260         else
2261                 wlc->default_bss->capability &=
2262                                         ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
2263
2264         /* Use the default 11g rateset */
2265         if (!rs.count)
2266                 wlc_rateset_copy(&cck_ofdm_rates, &rs);
2267
2268         if (ofdm_basic) {
2269                 for (i = 0; i < rs.count; i++) {
2270                         if (rs.rates[i] == WLC_RATE_6M
2271                             || rs.rates[i] == WLC_RATE_12M
2272                             || rs.rates[i] == WLC_RATE_24M)
2273                                 rs.rates[i] |= WLC_RATE_FLAG;
2274                 }
2275         }
2276
2277         /* Set default bss rateset */
2278         wlc->default_bss->rateset.count = rs.count;
2279         memcpy(wlc->default_bss->rateset.rates, rs.rates, 
2280                sizeof(wlc->default_bss->rateset.rates));
2281
2282         return ret;
2283 }
2284
2285 static int wlc_nmode_validate(struct wlc_info *wlc, s32 nmode)
2286 {
2287         int err = 0;
2288
2289         switch (nmode) {
2290
2291         case OFF:
2292                 break;
2293
2294         case AUTO:
2295         case WL_11N_2x2:
2296         case WL_11N_3x3:
2297                 if (!(WLC_PHY_11N_CAP(wlc->band)))
2298                         err = -EINVAL;
2299                 break;
2300
2301         default:
2302                 err = -EINVAL;
2303                 break;
2304         }
2305
2306         return err;
2307 }
2308
2309 int wlc_set_nmode(struct wlc_info *wlc, s32 nmode)
2310 {
2311         uint i;
2312         int err;
2313
2314         err = wlc_nmode_validate(wlc, nmode);
2315         if (err)
2316                 return err;
2317
2318         switch (nmode) {
2319         case OFF:
2320                 wlc->pub->_n_enab = OFF;
2321                 wlc->default_bss->flags &= ~WLC_BSS_HT;
2322                 /* delete the mcs rates from the default and hw ratesets */
2323                 wlc_rateset_mcs_clear(&wlc->default_bss->rateset);
2324                 for (i = 0; i < NBANDS(wlc); i++) {
2325                         memset(wlc->bandstate[i]->hw_rateset.mcs, 0,
2326                                MCSSET_LEN);
2327                         if (IS_MCS(wlc->band->rspec_override)) {
2328                                 wlc->bandstate[i]->rspec_override = 0;
2329                                 wlc_reprate_init(wlc);
2330                         }
2331                         if (IS_MCS(wlc->band->mrspec_override))
2332                                 wlc->bandstate[i]->mrspec_override = 0;
2333                 }
2334                 break;
2335
2336         case AUTO:
2337                 if (wlc->stf->txstreams == WL_11N_3x3)
2338                         nmode = WL_11N_3x3;
2339                 else
2340                         nmode = WL_11N_2x2;
2341         case WL_11N_2x2:
2342         case WL_11N_3x3:
2343                 /* force GMODE_AUTO if NMODE is ON */
2344                 wlc_set_gmode(wlc, GMODE_AUTO, true);
2345                 if (nmode == WL_11N_3x3)
2346                         wlc->pub->_n_enab = SUPPORT_HT;
2347                 else
2348                         wlc->pub->_n_enab = SUPPORT_11N;
2349                 wlc->default_bss->flags |= WLC_BSS_HT;
2350                 /* add the mcs rates to the default and hw ratesets */
2351                 wlc_rateset_mcs_build(&wlc->default_bss->rateset,
2352                                       wlc->stf->txstreams);
2353                 for (i = 0; i < NBANDS(wlc); i++)
2354                         memcpy(wlc->bandstate[i]->hw_rateset.mcs,
2355                                wlc->default_bss->rateset.mcs, MCSSET_LEN);
2356                 break;
2357
2358         default:
2359                 break;
2360         }
2361
2362         return err;
2363 }
2364
2365 static int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg)
2366 {
2367         wlc_rateset_t rs, new;
2368         uint bandunit;
2369
2370         memcpy(&rs, rs_arg, sizeof(wlc_rateset_t));
2371
2372         /* check for bad count value */
2373         if ((rs.count == 0) || (rs.count > WLC_NUMRATES))
2374                 return -EINVAL;
2375
2376         /* try the current band */
2377         bandunit = wlc->band->bandunit;
2378         memcpy(&new, &rs, sizeof(wlc_rateset_t));
2379         if (wlc_rate_hwrs_filter_sort_validate
2380             (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
2381              wlc->stf->txstreams))
2382                 goto good;
2383
2384         /* try the other band */
2385         if (IS_MBAND_UNLOCKED(wlc)) {
2386                 bandunit = OTHERBANDUNIT(wlc);
2387                 memcpy(&new, &rs, sizeof(wlc_rateset_t));
2388                 if (wlc_rate_hwrs_filter_sort_validate(&new,
2389                                                        &wlc->
2390                                                        bandstate[bandunit]->
2391                                                        hw_rateset, true,
2392                                                        wlc->stf->txstreams))
2393                         goto good;
2394         }
2395
2396         return -EBADE;
2397
2398  good:
2399         /* apply new rateset */
2400         memcpy(&wlc->default_bss->rateset, &new, sizeof(wlc_rateset_t));
2401         memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
2402                sizeof(wlc_rateset_t));
2403         return 0;
2404 }
2405
2406 /* simplified integer set interface for common ioctl handler */
2407 int wlc_set(struct wlc_info *wlc, int cmd, int arg)
2408 {
2409         return wlc_ioctl(wlc, cmd, (void *)&arg, sizeof(arg), NULL);
2410 }
2411
2412 /* simplified integer get interface for common ioctl handler */
2413 int wlc_get(struct wlc_info *wlc, int cmd, int *arg)
2414 {
2415         return wlc_ioctl(wlc, cmd, arg, sizeof(int), NULL);
2416 }
2417
2418 static void wlc_ofdm_rateset_war(struct wlc_info *wlc)
2419 {
2420         u8 r;
2421         bool war = false;
2422
2423         if (wlc->cfg->associated)
2424                 r = wlc->cfg->current_bss->rateset.rates[0];
2425         else
2426                 r = wlc->default_bss->rateset.rates[0];
2427
2428         wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
2429
2430         return;
2431 }
2432
2433 int
2434 wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
2435           struct wlc_if *wlcif)
2436 {
2437         return _wlc_ioctl(wlc, cmd, arg, len, wlcif);
2438 }
2439
2440 /* common ioctl handler. return: 0=ok, -1=error, positive=particular error */
2441 static int
2442 _wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
2443            struct wlc_if *wlcif)
2444 {
2445         int val, *pval;
2446         bool bool_val;
2447         int bcmerror;
2448         d11regs_t *regs;
2449         struct scb *nextscb;
2450         bool ta_ok;
2451         uint band;
2452         struct wlc_bsscfg *bsscfg;
2453         wlc_bss_info_t *current_bss;
2454
2455         /* update bsscfg pointer */
2456         bsscfg = wlc->cfg;
2457         current_bss = bsscfg->current_bss;
2458
2459         /* initialize the following to get rid of compiler warning */
2460         nextscb = NULL;
2461         ta_ok = false;
2462         band = 0;
2463
2464         /* If the device is turned off, then it's not "removed" */
2465         if (!wlc->pub->hw_off && DEVICEREMOVED(wlc)) {
2466                 wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n", wlc->pub->unit,
2467                           __func__);
2468                 wl_down(wlc->wl);
2469                 return -EBADE;
2470         }
2471
2472         /* default argument is generic integer */
2473         pval = arg ? (int *)arg:NULL;
2474
2475         /* This will prevent the misaligned access */
2476         if (pval && (u32) len >= sizeof(val))
2477                 memcpy(&val, pval, sizeof(val));
2478         else
2479                 val = 0;
2480
2481         /* bool conversion to avoid duplication below */
2482         bool_val = val != 0;
2483         bcmerror = 0;
2484         regs = wlc->regs;
2485
2486         if ((arg == NULL) || (len <= 0)) {
2487                 wiphy_err(wlc->wiphy, "wl%d: %s: Command %d needs arguments\n",
2488                           wlc->pub->unit, __func__, cmd);
2489                 bcmerror = -EINVAL;
2490                 goto done;
2491         }
2492
2493         switch (cmd) {
2494
2495         case WLC_SET_CHANNEL:{
2496                         chanspec_t chspec = CH20MHZ_CHSPEC(val);
2497
2498                         if (val < 0 || val > MAXCHANNEL) {
2499                                 bcmerror = -EINVAL;
2500                                 break;
2501                         }
2502
2503                         if (!wlc_valid_chanspec_db(wlc->cmi, chspec)) {
2504                                 bcmerror = -EINVAL;
2505                                 break;
2506                         }
2507
2508                         if (!wlc->pub->up && IS_MBAND_UNLOCKED(wlc)) {
2509                                 if (wlc->band->bandunit !=
2510                                     CHSPEC_WLCBANDUNIT(chspec))
2511                                         wlc->bandinit_pending = true;
2512                                 else
2513                                         wlc->bandinit_pending = false;
2514                         }
2515
2516                         wlc->default_bss->chanspec = chspec;
2517                         /* wlc_BSSinit() will sanitize the rateset before using it.. */
2518                         if (wlc->pub->up &&
2519                             (WLC_BAND_PI_RADIO_CHANSPEC != chspec)) {
2520                                 wlc_set_home_chanspec(wlc, chspec);
2521                                 wlc_suspend_mac_and_wait(wlc);
2522                                 wlc_set_chanspec(wlc, chspec);
2523                                 wlc_enable_mac(wlc);
2524                         }
2525                         break;
2526                 }
2527
2528         case WLC_SET_SRL:
2529                 if (val >= 1 && val <= RETRY_SHORT_MAX) {
2530                         int ac;
2531                         wlc->SRL = (u16) val;
2532
2533                         wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
2534
2535                         for (ac = 0; ac < AC_COUNT; ac++) {
2536                                 WLC_WME_RETRY_SHORT_SET(wlc, ac, wlc->SRL);
2537                         }
2538                         wlc_wme_retries_write(wlc);
2539                 } else
2540                         bcmerror = -EINVAL;
2541                 break;
2542
2543         case WLC_SET_LRL:
2544                 if (val >= 1 && val <= 255) {
2545                         int ac;
2546                         wlc->LRL = (u16) val;
2547
2548                         wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
2549
2550                         for (ac = 0; ac < AC_COUNT; ac++) {
2551                                 WLC_WME_RETRY_LONG_SET(wlc, ac, wlc->LRL);
2552                         }
2553                         wlc_wme_retries_write(wlc);
2554                 } else
2555                         bcmerror = -EINVAL;
2556                 break;
2557
2558         case WLC_GET_CURR_RATESET:{
2559                         wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
2560                         wlc_rateset_t *rs;
2561
2562                         if (wlc->pub->associated)
2563                                 rs = &current_bss->rateset;
2564                         else
2565                                 rs = &wlc->default_bss->rateset;
2566
2567                         if (len < (int)(rs->count + sizeof(rs->count))) {
2568                                 bcmerror = -EOVERFLOW;
2569                                 break;
2570                         }
2571
2572                         /* Copy only legacy rateset section */
2573                         ret_rs->count = rs->count;
2574                         memcpy(&ret_rs->rates, &rs->rates, rs->count);
2575                         break;
2576                 }
2577
2578         case WLC_SET_RATESET:{
2579                         wlc_rateset_t rs;
2580                         wl_rateset_t *in_rs = (wl_rateset_t *) arg;
2581
2582                         if (len < (int)(in_rs->count + sizeof(in_rs->count))) {
2583                                 bcmerror = -EOVERFLOW;
2584                                 break;
2585                         }
2586
2587                         if (in_rs->count > WLC_NUMRATES) {
2588                                 bcmerror = -ENOBUFS;
2589                                 break;
2590                         }
2591
2592                         memset(&rs, 0, sizeof(wlc_rateset_t));
2593
2594                         /* Copy only legacy rateset section */
2595                         rs.count = in_rs->count;
2596                         memcpy(&rs.rates, &in_rs->rates, rs.count);
2597
2598                         /* merge rateset coming in with the current mcsset */
2599                         if (N_ENAB(wlc->pub)) {
2600                                 if (bsscfg->associated)
2601                                         memcpy(rs.mcs,
2602                                                &current_bss->rateset.mcs[0],
2603                                                MCSSET_LEN);
2604                                 else
2605                                         memcpy(rs.mcs,
2606                                                &wlc->default_bss->rateset.mcs[0],
2607                                                MCSSET_LEN);
2608                         }
2609
2610                         bcmerror = wlc_set_rateset(wlc, &rs);
2611
2612                         if (!bcmerror)
2613                                 wlc_ofdm_rateset_war(wlc);
2614
2615                         break;
2616                 }
2617
2618         case WLC_SET_BCNPRD:
2619                 /* range [1, 0xffff] */
2620                 if (val >= DOT11_MIN_BEACON_PERIOD
2621                     && val <= DOT11_MAX_BEACON_PERIOD) {
2622                         wlc->default_bss->beacon_period = (u16) val;
2623                 } else
2624                         bcmerror = -EINVAL;
2625                 break;
2626
2627         case WLC_GET_PHYLIST:
2628                 {
2629                         unsigned char *cp = arg;
2630                         if (len < 3) {
2631                                 bcmerror = -EOVERFLOW;
2632                                 break;
2633                         }
2634
2635                         if (WLCISNPHY(wlc->band)) {
2636                                 *cp++ = 'n';
2637                         } else if (WLCISLCNPHY(wlc->band)) {
2638                                 *cp++ = 'c';
2639                         } else if (WLCISSSLPNPHY(wlc->band)) {
2640                                 *cp++ = 's';
2641                         }
2642                         *cp = '\0';
2643                         break;
2644                 }
2645
2646         case WLC_SET_SHORTSLOT_OVERRIDE:
2647                 if ((val != WLC_SHORTSLOT_AUTO) &&
2648                     (val != WLC_SHORTSLOT_OFF) && (val != WLC_SHORTSLOT_ON)) {
2649                         bcmerror = -EINVAL;
2650                         break;
2651                 }
2652
2653                 wlc->shortslot_override = (s8) val;
2654
2655                 /* shortslot is an 11g feature, so no more work if we are
2656                  * currently on the 5G band
2657                  */
2658                 if (BAND_5G(wlc->band->bandtype))
2659                         break;
2660
2661                 if (wlc->pub->up && wlc->pub->associated) {
2662                         /* let watchdog or beacon processing update shortslot */
2663                 } else if (wlc->pub->up) {
2664                         /* unassociated shortslot is off */
2665                         wlc_switch_shortslot(wlc, false);
2666                 } else {
2667                         /* driver is down, so just update the wlc_info value */
2668                         if (wlc->shortslot_override == WLC_SHORTSLOT_AUTO) {
2669                                 wlc->shortslot = false;
2670                         } else {
2671                                 wlc->shortslot =
2672                                     (wlc->shortslot_override ==
2673                                      WLC_SHORTSLOT_ON);
2674                         }
2675                 }
2676
2677                 break;
2678
2679         }
2680  done:
2681
2682         if (bcmerror)
2683                 wlc->pub->bcmerror = bcmerror;
2684
2685         return bcmerror;
2686 }
2687
2688 /*
2689  * register watchdog and down handlers.
2690  */
2691 int wlc_module_register(struct wlc_pub *pub,
2692                         const char *name, void *hdl,
2693                         watchdog_fn_t w_fn, down_fn_t d_fn)
2694 {
2695         struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
2696         int i;
2697
2698         /* find an empty entry and just add, no duplication check! */
2699         for (i = 0; i < WLC_MAXMODULES; i++) {
2700                 if (wlc->modulecb[i].name[0] == '\0') {
2701                         strncpy(wlc->modulecb[i].name, name,
2702                                 sizeof(wlc->modulecb[i].name) - 1);
2703                         wlc->modulecb[i].hdl = hdl;
2704                         wlc->modulecb[i].watchdog_fn = w_fn;
2705                         wlc->modulecb[i].down_fn = d_fn;
2706                         return 0;
2707                 }
2708         }
2709
2710         return -ENOSR;
2711 }
2712
2713 /* unregister module callbacks */
2714 int wlc_module_unregister(struct wlc_pub *pub, const char *name, void *hdl)
2715 {
2716         struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
2717         int i;
2718
2719         if (wlc == NULL)
2720                 return -ENODATA;
2721
2722         for (i = 0; i < WLC_MAXMODULES; i++) {
2723                 if (!strcmp(wlc->modulecb[i].name, name) &&
2724                     (wlc->modulecb[i].hdl == hdl)) {
2725                         memset(&wlc->modulecb[i], 0, sizeof(struct modulecb));
2726                         return 0;
2727                 }
2728         }
2729
2730         /* table not found! */
2731         return -ENODATA;
2732 }
2733
2734 /* Write WME tunable parameters for retransmit/max rate from wlc struct to ucode */
2735 static void wlc_wme_retries_write(struct wlc_info *wlc)
2736 {
2737         int ac;
2738
2739         /* Need clock to do this */
2740         if (!wlc->clk)
2741                 return;
2742
2743         for (ac = 0; ac < AC_COUNT; ac++) {
2744                 wlc_write_shm(wlc, M_AC_TXLMT_ADDR(ac), wlc->wme_retries[ac]);
2745         }
2746 }
2747
2748 #ifdef BCMDBG
2749 static const char *supr_reason[] = {
2750         "None", "PMQ Entry", "Flush request",
2751         "Previous frag failure", "Channel mismatch",
2752         "Lifetime Expiry", "Underflow"
2753 };
2754
2755 static void wlc_print_txs_status(u16 s)
2756 {
2757         printk(KERN_DEBUG "[15:12]  %d  frame attempts\n",
2758                (s & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT);
2759         printk(KERN_DEBUG " [11:8]  %d  rts attempts\n",
2760                (s & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT);
2761         printk(KERN_DEBUG "    [7]  %d  PM mode indicated\n",
2762                ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
2763         printk(KERN_DEBUG "    [6]  %d  intermediate status\n",
2764                ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
2765         printk(KERN_DEBUG "    [5]  %d  AMPDU\n",
2766                (s & TX_STATUS_AMPDU) ? 1 : 0);
2767         printk(KERN_DEBUG "  [4:2]  %d  Frame Suppressed Reason (%s)\n",
2768                ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
2769                supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
2770         printk(KERN_DEBUG "    [1]  %d  acked\n",
2771                ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
2772 }
2773 #endif                          /* BCMDBG */
2774
2775 void wlc_print_txstatus(tx_status_t *txs)
2776 {
2777 #if defined(BCMDBG)
2778         u16 s = txs->status;
2779         u16 ackphyrxsh = txs->ackphyrxsh;
2780
2781         printk(KERN_DEBUG "\ntxpkt (MPDU) Complete\n");
2782
2783         printk(KERN_DEBUG "FrameID: %04x   ", txs->frameid);
2784         printk(KERN_DEBUG "TxStatus: %04x", s);
2785         printk(KERN_DEBUG "\n");
2786
2787         wlc_print_txs_status(s);
2788
2789         printk(KERN_DEBUG "LastTxTime: %04x ", txs->lasttxtime);
2790         printk(KERN_DEBUG "Seq: %04x ", txs->sequence);
2791         printk(KERN_DEBUG "PHYTxStatus: %04x ", txs->phyerr);
2792         printk(KERN_DEBUG "RxAckRSSI: %04x ",
2793                (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
2794         printk(KERN_DEBUG "RxAckSQ: %04x",
2795                (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
2796         printk(KERN_DEBUG "\n");
2797 #endif                          /* defined(BCMDBG) */
2798 }
2799
2800 void wlc_statsupd(struct wlc_info *wlc)
2801 {
2802         int i;
2803         macstat_t macstats;
2804 #ifdef BCMDBG
2805         u16 delta;
2806         u16 rxf0ovfl;
2807         u16 txfunfl[NFIFO];
2808 #endif                          /* BCMDBG */
2809
2810         /* if driver down, make no sense to update stats */
2811         if (!wlc->pub->up)
2812                 return;
2813
2814 #ifdef BCMDBG
2815         /* save last rx fifo 0 overflow count */
2816         rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
2817
2818         /* save last tx fifo  underflow count */
2819         for (i = 0; i < NFIFO; i++)
2820                 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
2821 #endif                          /* BCMDBG */
2822
2823         /* Read mac stats from contiguous shared memory */
2824         wlc_bmac_copyfrom_shm(wlc->hw, M_UCODE_MACSTAT,
2825                               &macstats, sizeof(macstat_t));
2826
2827 #ifdef BCMDBG
2828         /* check for rx fifo 0 overflow */
2829         delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
2830         if (delta)
2831                 wiphy_err(wlc->wiphy, "wl%d: %u rx fifo 0 overflows!\n",
2832                           wlc->pub->unit, delta);
2833
2834         /* check for tx fifo underflows */
2835         for (i = 0; i < NFIFO; i++) {
2836                 delta =
2837                     (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
2838                               txfunfl[i]);
2839                 if (delta)
2840                         wiphy_err(wlc->wiphy, "wl%d: %u tx fifo %d underflows!"
2841                                   "\n", wlc->pub->unit, delta, i);
2842         }
2843 #endif                          /* BCMDBG */
2844
2845         /* merge counters from dma module */
2846         for (i = 0; i < NFIFO; i++) {
2847                 if (wlc->hw->di[i]) {
2848                         dma_counterreset(wlc->hw->di[i]);
2849                 }
2850         }
2851 }
2852
2853 bool wlc_chipmatch(u16 vendor, u16 device)
2854 {
2855         if (vendor != PCI_VENDOR_ID_BROADCOM) {
2856                 pr_err("wlc_chipmatch: unknown vendor id %04x\n", vendor);
2857                 return false;
2858         }
2859
2860         if (device == BCM43224_D11N_ID_VEN1)
2861                 return true;
2862         if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
2863                 return true;
2864         if (device == BCM4313_D11N2G_ID)
2865                 return true;
2866         if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
2867                 return true;
2868
2869         pr_err("wlc_chipmatch: unknown device id %04x\n", device);
2870         return false;
2871 }
2872
2873 #if defined(BCMDBG)
2874 void wlc_print_txdesc(d11txh_t *txh)
2875 {
2876         u16 mtcl = le16_to_cpu(txh->MacTxControlLow);
2877         u16 mtch = le16_to_cpu(txh->MacTxControlHigh);
2878         u16 mfc = le16_to_cpu(txh->MacFrameControl);
2879         u16 tfest = le16_to_cpu(txh->TxFesTimeNormal);
2880         u16 ptcw = le16_to_cpu(txh->PhyTxControlWord);
2881         u16 ptcw_1 = le16_to_cpu(txh->PhyTxControlWord_1);
2882         u16 ptcw_1_Fbr = le16_to_cpu(txh->PhyTxControlWord_1_Fbr);
2883         u16 ptcw_1_Rts = le16_to_cpu(txh->PhyTxControlWord_1_Rts);
2884         u16 ptcw_1_FbrRts = le16_to_cpu(txh->PhyTxControlWord_1_FbrRts);
2885         u16 mainrates = le16_to_cpu(txh->MainRates);
2886         u16 xtraft = le16_to_cpu(txh->XtraFrameTypes);
2887         u8 *iv = txh->IV;
2888         u8 *ra = txh->TxFrameRA;
2889         u16 tfestfb = le16_to_cpu(txh->TxFesTimeFallback);
2890         u8 *rtspfb = txh->RTSPLCPFallback;
2891         u16 rtsdfb = le16_to_cpu(txh->RTSDurFallback);
2892         u8 *fragpfb = txh->FragPLCPFallback;
2893         u16 fragdfb = le16_to_cpu(txh->FragDurFallback);
2894         u16 mmodelen = le16_to_cpu(txh->MModeLen);
2895         u16 mmodefbrlen = le16_to_cpu(txh->MModeFbrLen);
2896         u16 tfid = le16_to_cpu(txh->TxFrameID);
2897         u16 txs = le16_to_cpu(txh->TxStatus);
2898         u16 mnmpdu = le16_to_cpu(txh->MaxNMpdus);
2899         u16 mabyte = le16_to_cpu(txh->MaxABytes_MRT);
2900         u16 mabyte_f = le16_to_cpu(txh->MaxABytes_FBR);
2901         u16 mmbyte = le16_to_cpu(txh->MinMBytes);
2902
2903         u8 *rtsph = txh->RTSPhyHeader;
2904         struct ieee80211_rts rts = txh->rts_frame;
2905         char hexbuf[256];
2906
2907         /* add plcp header along with txh descriptor */
2908         printk(KERN_DEBUG "Raw TxDesc + plcp header:\n");
2909         print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
2910                              txh, sizeof(d11txh_t) + 48);
2911
2912         printk(KERN_DEBUG "TxCtlLow: %04x ", mtcl);
2913         printk(KERN_DEBUG "TxCtlHigh: %04x ", mtch);
2914         printk(KERN_DEBUG "FC: %04x ", mfc);
2915         printk(KERN_DEBUG "FES Time: %04x\n", tfest);
2916         printk(KERN_DEBUG "PhyCtl: %04x%s ", ptcw,
2917                (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
2918         printk(KERN_DEBUG "PhyCtl_1: %04x ", ptcw_1);
2919         printk(KERN_DEBUG "PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
2920         printk(KERN_DEBUG "PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
2921         printk(KERN_DEBUG "PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
2922         printk(KERN_DEBUG "MainRates: %04x ", mainrates);
2923         printk(KERN_DEBUG "XtraFrameTypes: %04x ", xtraft);
2924         printk(KERN_DEBUG "\n");
2925
2926         bcm_format_hex(hexbuf, iv, sizeof(txh->IV));
2927         printk(KERN_DEBUG "SecIV:       %s\n", hexbuf);
2928         bcm_format_hex(hexbuf, ra, sizeof(txh->TxFrameRA));
2929         printk(KERN_DEBUG "RA:          %s\n", hexbuf);
2930
2931         printk(KERN_DEBUG "Fb FES Time: %04x ", tfestfb);
2932         bcm_format_hex(hexbuf, rtspfb, sizeof(txh->RTSPLCPFallback));
2933         printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
2934         printk(KERN_DEBUG "RTS DUR: %04x ", rtsdfb);
2935         bcm_format_hex(hexbuf, fragpfb, sizeof(txh->FragPLCPFallback));
2936         printk(KERN_DEBUG "PLCP: %s ", hexbuf);
2937         printk(KERN_DEBUG "DUR: %04x", fragdfb);
2938         printk(KERN_DEBUG "\n");
2939
2940         printk(KERN_DEBUG "MModeLen: %04x ", mmodelen);
2941         printk(KERN_DEBUG "MModeFbrLen: %04x\n", mmodefbrlen);
2942
2943         printk(KERN_DEBUG "FrameID:     %04x\n", tfid);
2944         printk(KERN_DEBUG "TxStatus:    %04x\n", txs);
2945
2946         printk(KERN_DEBUG "MaxNumMpdu:  %04x\n", mnmpdu);
2947         printk(KERN_DEBUG "MaxAggbyte:  %04x\n", mabyte);
2948         printk(KERN_DEBUG "MaxAggbyte_fb:  %04x\n", mabyte_f);
2949         printk(KERN_DEBUG "MinByte:     %04x\n", mmbyte);
2950
2951         bcm_format_hex(hexbuf, rtsph, sizeof(txh->RTSPhyHeader));
2952         printk(KERN_DEBUG "RTS PLCP: %s ", hexbuf);
2953         bcm_format_hex(hexbuf, (u8 *) &rts, sizeof(txh->rts_frame));
2954         printk(KERN_DEBUG "RTS Frame: %s", hexbuf);
2955         printk(KERN_DEBUG "\n");
2956 }
2957 #endif                          /* defined(BCMDBG) */
2958
2959 #if defined(BCMDBG)
2960 void wlc_print_rxh(d11rxhdr_t *rxh)
2961 {
2962         u16 len = rxh->RxFrameSize;
2963         u16 phystatus_0 = rxh->PhyRxStatus_0;
2964         u16 phystatus_1 = rxh->PhyRxStatus_1;
2965         u16 phystatus_2 = rxh->PhyRxStatus_2;
2966         u16 phystatus_3 = rxh->PhyRxStatus_3;
2967         u16 macstatus1 = rxh->RxStatus1;
2968         u16 macstatus2 = rxh->RxStatus2;
2969         char flagstr[64];
2970         char lenbuf[20];
2971         static const bcm_bit_desc_t macstat_flags[] = {
2972                 {RXS_FCSERR, "FCSErr"},
2973                 {RXS_RESPFRAMETX, "Reply"},
2974                 {RXS_PBPRES, "PADDING"},
2975                 {RXS_DECATMPT, "DeCr"},
2976                 {RXS_DECERR, "DeCrErr"},
2977                 {RXS_BCNSENT, "Bcn"},
2978                 {0, NULL}
2979         };
2980
2981         printk(KERN_DEBUG "Raw RxDesc:\n");
2982         print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, rxh, sizeof(d11rxhdr_t));
2983
2984         bcm_format_flags(macstat_flags, macstatus1, flagstr, 64);
2985
2986         snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
2987
2988         printk(KERN_DEBUG "RxFrameSize:     %6s (%d)%s\n", lenbuf, len,
2989                (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
2990         printk(KERN_DEBUG "RxPHYStatus:     %04x %04x %04x %04x\n",
2991                phystatus_0, phystatus_1, phystatus_2, phystatus_3);
2992         printk(KERN_DEBUG "RxMACStatus:     %x %s\n", macstatus1, flagstr);
2993         printk(KERN_DEBUG "RXMACaggtype:    %x\n",
2994                (macstatus2 & RXS_AGGTYPE_MASK));
2995         printk(KERN_DEBUG "RxTSFTime:       %04x\n", rxh->RxTSFTime);
2996 }
2997 #endif                          /* defined(BCMDBG) */
2998
2999 static u16 wlc_rate_shm_offset(struct wlc_info *wlc, u8 rate)
3000 {
3001         return wlc_bmac_rate_shm_offset(wlc->hw, rate);
3002 }
3003
3004 /* Callback for device removed */
3005
3006 /*
3007  * Attempts to queue a packet onto a multiple-precedence queue,
3008  * if necessary evicting a lower precedence packet from the queue.
3009  *
3010  * 'prec' is the precedence number that has already been mapped
3011  * from the packet priority.
3012  *
3013  * Returns true if packet consumed (queued), false if not.
3014  */
3015 bool
3016 wlc_prec_enq(struct wlc_info *wlc, struct pktq *q, void *pkt, int prec)
3017 {
3018         return wlc_prec_enq_head(wlc, q, pkt, prec, false);
3019 }
3020
3021 bool
3022 wlc_prec_enq_head(struct wlc_info *wlc, struct pktq *q, struct sk_buff *pkt,
3023                   int prec, bool head)
3024 {
3025         struct sk_buff *p;
3026         int eprec = -1;         /* precedence to evict from */
3027
3028         /* Determine precedence from which to evict packet, if any */
3029         if (pktq_pfull(q, prec))
3030                 eprec = prec;
3031         else if (pktq_full(q)) {
3032                 p = bcm_pktq_peek_tail(q, &eprec);
3033                 if (eprec > prec) {
3034                         wiphy_err(wlc->wiphy, "%s: Failing: eprec %d > prec %d"
3035                                   "\n", __func__, eprec, prec);
3036                         return false;
3037                 }
3038         }
3039
3040         /* Evict if needed */
3041         if (eprec >= 0) {
3042                 bool discard_oldest;
3043
3044                 discard_oldest = AC_BITMAP_TST(wlc->wme_dp, eprec);
3045
3046                 /* Refuse newer packet unless configured to discard oldest */
3047                 if (eprec == prec && !discard_oldest) {
3048                         wiphy_err(wlc->wiphy, "%s: No where to go, prec == %d"
3049                                   "\n", __func__, prec);
3050                         return false;
3051                 }
3052
3053                 /* Evict packet according to discard policy */
3054                 p = discard_oldest ? bcm_pktq_pdeq(q, eprec) :
3055                         bcm_pktq_pdeq_tail(q, eprec);
3056                 bcm_pkt_buf_free_skb(p);
3057         }
3058
3059         /* Enqueue */
3060         if (head)
3061                 p = bcm_pktq_penq_head(q, prec, pkt);
3062         else
3063                 p = bcm_pktq_penq(q, prec, pkt);
3064
3065         return true;
3066 }
3067
3068 void wlc_txq_enq(void *ctx, struct scb *scb, struct sk_buff *sdu,
3069                              uint prec)
3070 {
3071         struct wlc_info *wlc = (struct wlc_info *) ctx;
3072         struct wlc_txq_info *qi = wlc->pkt_queue;       /* Check me */
3073         struct pktq *q = &qi->q;
3074         int prio;
3075
3076         prio = sdu->priority;
3077
3078         if (!wlc_prec_enq(wlc, q, sdu, prec)) {
3079                 if (!EDCF_ENAB(wlc->pub)
3080                     || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL))
3081                         wiphy_err(wlc->wiphy, "wl%d: wlc_txq_enq: txq overflow"
3082                                   "\n", wlc->pub->unit);
3083
3084                 /*
3085                  * XXX we might hit this condtion in case
3086                  * packet flooding from mac80211 stack
3087                  */
3088                 bcm_pkt_buf_free_skb(sdu);
3089         }
3090
3091         /* Check if flow control needs to be turned on after enqueuing the packet
3092          *   Don't turn on flow control if EDCF is enabled. Driver would make the decision on what
3093          *   to drop instead of relying on stack to make the right decision
3094          */
3095         if (!EDCF_ENAB(wlc->pub)
3096             || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
3097                 if (pktq_len(q) >= wlc->pub->tunables->datahiwat) {
3098                         wlc_txflowcontrol(wlc, qi, ON, ALLPRIO);
3099                 }
3100         } else if (wlc->pub->_priofc) {
3101                 if (pktq_plen(q, wlc_prio2prec_map[prio]) >=
3102                     wlc->pub->tunables->datahiwat) {
3103                         wlc_txflowcontrol(wlc, qi, ON, prio);
3104                 }
3105         }
3106 }
3107
3108 bool
3109 wlc_sendpkt_mac80211(struct wlc_info *wlc, struct sk_buff *sdu,
3110                      struct ieee80211_hw *hw)
3111 {
3112         u8 prio;
3113         uint fifo;
3114         void *pkt;
3115         struct scb *scb = &global_scb;
3116         struct ieee80211_hdr *d11_header = (struct ieee80211_hdr *)(sdu->data);
3117
3118         /* 802.11 standard requires management traffic to go at highest priority */
3119         prio = ieee80211_is_data(d11_header->frame_control) ? sdu->priority :
3120                 MAXPRIO;
3121         fifo = prio2fifo[prio];
3122         pkt = sdu;
3123         if (unlikely
3124             (wlc_d11hdrs_mac80211(wlc, hw, pkt, scb, 0, 1, fifo, 0, NULL, 0)))
3125                 return -EINVAL;
3126         wlc_txq_enq(wlc, scb, pkt, WLC_PRIO_TO_PREC(prio));
3127         wlc_send_q(wlc);
3128         return 0;
3129 }
3130
3131 void wlc_send_q(struct wlc_info *wlc)
3132 {
3133         struct sk_buff *pkt[DOT11_MAXNUMFRAGS];
3134         int prec;
3135         u16 prec_map;
3136         int err = 0, i, count;
3137         uint fifo;
3138         struct wlc_txq_info *qi = wlc->pkt_queue;
3139         struct pktq *q = &qi->q;
3140         struct ieee80211_tx_info *tx_info;
3141
3142         if (in_send_q)
3143                 return;
3144         else
3145                 in_send_q = true;
3146
3147         prec_map = wlc->tx_prec_map;
3148
3149         /* Send all the enq'd pkts that we can.
3150          * Dequeue packets with precedence with empty HW fifo only
3151          */
3152         while (prec_map && (pkt[0] = bcm_pktq_mdeq(q, prec_map, &prec))) {
3153                 tx_info = IEEE80211_SKB_CB(pkt[0]);
3154                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
3155                         err = wlc_sendampdu(wlc->ampdu, qi, pkt, prec);
3156                 } else {
3157                         count = 1;
3158                         err = wlc_prep_pdu(wlc, pkt[0], &fifo);
3159                         if (!err) {
3160                                 for (i = 0; i < count; i++) {
3161                                         wlc_txfifo(wlc, fifo, pkt[i], true, 1);
3162                                 }
3163                         }
3164                 }
3165
3166                 if (err == -EBUSY) {
3167                         bcm_pktq_penq_head(q, prec, pkt[0]);
3168                         /* If send failed due to any other reason than a change in
3169                          * HW FIFO condition, quit. Otherwise, read the new prec_map!
3170                          */
3171                         if (prec_map == wlc->tx_prec_map)
3172                                 break;
3173                         prec_map = wlc->tx_prec_map;
3174                 }
3175         }
3176
3177         /* Check if flow control needs to be turned off after sending the packet */
3178         if (!EDCF_ENAB(wlc->pub)
3179             || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
3180                 if (wlc_txflowcontrol_prio_isset(wlc, qi, ALLPRIO)
3181                     && (pktq_len(q) < wlc->pub->tunables->datahiwat / 2)) {
3182                         wlc_txflowcontrol(wlc, qi, OFF, ALLPRIO);
3183                 }
3184         } else if (wlc->pub->_priofc) {
3185                 int prio;
3186                 for (prio = MAXPRIO; prio >= 0; prio--) {
3187                         if (wlc_txflowcontrol_prio_isset(wlc, qi, prio) &&
3188                             (pktq_plen(q, wlc_prio2prec_map[prio]) <
3189                              wlc->pub->tunables->datahiwat / 2)) {
3190                                 wlc_txflowcontrol(wlc, qi, OFF, prio);
3191                         }
3192                 }
3193         }
3194         in_send_q = false;
3195 }
3196
3197 /*
3198  * bcmc_fid_generate:
3199  * Generate frame ID for a BCMC packet.  The frag field is not used
3200  * for MC frames so is used as part of the sequence number.
3201  */
3202 static inline u16
3203 bcmc_fid_generate(struct wlc_info *wlc, struct wlc_bsscfg *bsscfg,
3204                   d11txh_t *txh)
3205 {
3206         u16 frameid;
3207
3208         frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
3209                                                   TXFID_QUEUE_MASK);
3210         frameid |=
3211             (((wlc->
3212                mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
3213             TX_BCMC_FIFO;
3214
3215         return frameid;
3216 }
3217
3218 void
3219 wlc_txfifo(struct wlc_info *wlc, uint fifo, struct sk_buff *p, bool commit,
3220            s8 txpktpend)
3221 {
3222         u16 frameid = INVALIDFID;
3223         d11txh_t *txh;
3224
3225         txh = (d11txh_t *) (p->data);
3226
3227         /* When a BC/MC frame is being committed to the BCMC fifo via DMA (NOT PIO), update
3228          * ucode or BSS info as appropriate.
3229          */
3230         if (fifo == TX_BCMC_FIFO) {
3231                 frameid = le16_to_cpu(txh->TxFrameID);
3232
3233         }
3234
3235         if (WLC_WAR16165(wlc))
3236                 wlc_war16165(wlc, true);
3237
3238
3239         /* Bump up pending count for if not using rpc. If rpc is used, this will be handled
3240          * in wlc_bmac_txfifo()
3241          */
3242         if (commit) {
3243                 TXPKTPENDINC(wlc, fifo, txpktpend);
3244                 BCMMSG(wlc->wiphy, "pktpend inc %d to %d\n",
3245                          txpktpend, TXPKTPENDGET(wlc, fifo));
3246         }
3247
3248         /* Commit BCMC sequence number in the SHM frame ID location */
3249         if (frameid != INVALIDFID)
3250                 BCMCFID(wlc, frameid);
3251
3252         if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0) {
3253                 wiphy_err(wlc->wiphy, "wlc_txfifo: fatal, toss frames !!!\n");
3254         }
3255 }
3256
3257 void
3258 wlc_compute_plcp(struct wlc_info *wlc, ratespec_t rspec, uint length, u8 *plcp)
3259 {
3260         if (IS_MCS(rspec)) {
3261                 wlc_compute_mimo_plcp(rspec, length, plcp);
3262         } else if (IS_OFDM(rspec)) {
3263                 wlc_compute_ofdm_plcp(rspec, length, plcp);
3264         } else {
3265                 wlc_compute_cck_plcp(wlc, rspec, length, plcp);
3266         }
3267         return;
3268 }
3269
3270 /* Rate: 802.11 rate code, length: PSDU length in octets */
3271 static void wlc_compute_mimo_plcp(ratespec_t rspec, uint length, u8 *plcp)
3272 {
3273         u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
3274         plcp[0] = mcs;
3275         if (RSPEC_IS40MHZ(rspec) || (mcs == 32))
3276                 plcp[0] |= MIMO_PLCP_40MHZ;
3277         WLC_SET_MIMO_PLCP_LEN(plcp, length);
3278         plcp[3] = RSPEC_MIMOPLCP3(rspec);       /* rspec already holds this byte */
3279         plcp[3] |= 0x7;         /* set smoothing, not sounding ppdu & reserved */
3280         plcp[4] = 0;            /* number of extension spatial streams bit 0 & 1 */
3281         plcp[5] = 0;
3282 }
3283
3284 /* Rate: 802.11 rate code, length: PSDU length in octets */
3285 static void
3286 wlc_compute_ofdm_plcp(ratespec_t rspec, u32 length, u8 *plcp)
3287 {
3288         u8 rate_signal;
3289         u32 tmp = 0;
3290         int rate = RSPEC2RATE(rspec);
3291
3292         /* encode rate per 802.11a-1999 sec 17.3.4.1, with lsb transmitted first */
3293         rate_signal = rate_info[rate] & WLC_RATE_MASK;
3294         memset(plcp, 0, D11_PHY_HDR_LEN);
3295         D11A_PHY_HDR_SRATE((ofdm_phy_hdr_t *) plcp, rate_signal);
3296
3297         tmp = (length & 0xfff) << 5;
3298         plcp[2] |= (tmp >> 16) & 0xff;
3299         plcp[1] |= (tmp >> 8) & 0xff;
3300         plcp[0] |= tmp & 0xff;
3301
3302         return;
3303 }
3304
3305 /*
3306  * Compute PLCP, but only requires actual rate and length of pkt.
3307  * Rate is given in the driver standard multiple of 500 kbps.
3308  * le is set for 11 Mbps rate if necessary.
3309  * Broken out for PRQ.
3310  */
3311
3312 static void wlc_cck_plcp_set(struct wlc_info *wlc, int rate_500, uint length,
3313                              u8 *plcp)
3314 {
3315         u16 usec = 0;
3316         u8 le = 0;
3317
3318         switch (rate_500) {
3319         case WLC_RATE_1M:
3320                 usec = length << 3;
3321                 break;
3322         case WLC_RATE_2M:
3323                 usec = length << 2;
3324                 break;
3325         case WLC_RATE_5M5:
3326                 usec = (length << 4) / 11;
3327                 if ((length << 4) - (usec * 11) > 0)
3328                         usec++;
3329                 break;
3330         case WLC_RATE_11M:
3331                 usec = (length << 3) / 11;
3332                 if ((length << 3) - (usec * 11) > 0) {
3333                         usec++;
3334                         if ((usec * 11) - (length << 3) >= 8)
3335                                 le = D11B_PLCP_SIGNAL_LE;
3336                 }
3337                 break;
3338
3339         default:
3340                 wiphy_err(wlc->wiphy, "wlc_cck_plcp_set: unsupported rate %d"
3341                           "\n", rate_500);
3342                 rate_500 = WLC_RATE_1M;
3343                 usec = length << 3;
3344                 break;
3345         }
3346         /* PLCP signal byte */
3347         plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
3348         /* PLCP service byte */
3349         plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
3350         /* PLCP length u16, little endian */
3351         plcp[2] = usec & 0xff;
3352         plcp[3] = (usec >> 8) & 0xff;
3353         /* PLCP CRC16 */
3354         plcp[4] = 0;
3355         plcp[5] = 0;
3356 }
3357
3358 /* Rate: 802.11 rate code, length: PSDU length in octets */
3359 static void wlc_compute_cck_plcp(struct wlc_info *wlc, ratespec_t rspec,
3360                                  uint length, u8 *plcp)
3361 {
3362         int rate = RSPEC2RATE(rspec);
3363
3364         wlc_cck_plcp_set(wlc, rate, length, plcp);
3365 }
3366
3367 /* wlc_compute_frame_dur()
3368  *
3369  * Calculate the 802.11 MAC header DUR field for MPDU
3370  * DUR for a single frame = 1 SIFS + 1 ACK
3371  * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
3372  *
3373  * rate                 MPDU rate in unit of 500kbps
3374  * next_frag_len        next MPDU length in bytes
3375  * preamble_type        use short/GF or long/MM PLCP header
3376  */
3377 static u16
3378 wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate, u8 preamble_type,
3379                       uint next_frag_len)
3380 {
3381         u16 dur, sifs;
3382
3383         sifs = SIFS(wlc->band);
3384
3385         dur = sifs;
3386         dur += (u16) wlc_calc_ack_time(wlc, rate, preamble_type);
3387
3388         if (next_frag_len) {
3389                 /* Double the current DUR to get 2 SIFS + 2 ACKs */
3390                 dur *= 2;
3391                 /* add another SIFS and the frag time */
3392                 dur += sifs;
3393                 dur +=
3394                     (u16) wlc_calc_frame_time(wlc, rate, preamble_type,
3395                                                  next_frag_len);
3396         }
3397         return dur;
3398 }
3399
3400 /* wlc_compute_rtscts_dur()
3401  *
3402  * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
3403  * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
3404  * DUR for CTS-TO-SELF w/ frame    = 2 SIFS         + next frame time + 1 ACK
3405  *
3406  * cts                  cts-to-self or rts/cts
3407  * rts_rate             rts or cts rate in unit of 500kbps
3408  * rate                 next MPDU rate in unit of 500kbps
3409  * frame_len            next MPDU frame length in bytes
3410  */
3411 u16
3412 wlc_compute_rtscts_dur(struct wlc_info *wlc, bool cts_only, ratespec_t rts_rate,
3413                        ratespec_t frame_rate, u8 rts_preamble_type,
3414                        u8 frame_preamble_type, uint frame_len, bool ba)
3415 {
3416         u16 dur, sifs;
3417
3418         sifs = SIFS(wlc->band);
3419
3420         if (!cts_only) {        /* RTS/CTS */
3421                 dur = 3 * sifs;
3422                 dur +=
3423                     (u16) wlc_calc_cts_time(wlc, rts_rate,
3424                                                rts_preamble_type);
3425         } else {                /* CTS-TO-SELF */
3426                 dur = 2 * sifs;
3427         }
3428
3429         dur +=
3430             (u16) wlc_calc_frame_time(wlc, frame_rate, frame_preamble_type,
3431                                          frame_len);
3432         if (ba)
3433                 dur +=
3434                     (u16) wlc_calc_ba_time(wlc, frame_rate,
3435                                               WLC_SHORT_PREAMBLE);
3436         else
3437                 dur +=
3438                     (u16) wlc_calc_ack_time(wlc, frame_rate,
3439                                                frame_preamble_type);
3440         return dur;
3441 }
3442
3443 u16 wlc_phytxctl1_calc(struct wlc_info *wlc, ratespec_t rspec)
3444 {
3445         u16 phyctl1 = 0;
3446         u16 bw;
3447
3448         if (WLCISLCNPHY(wlc->band)) {
3449                 bw = PHY_TXC1_BW_20MHZ;
3450         } else {
3451                 bw = RSPEC_GET_BW(rspec);
3452                 /* 10Mhz is not supported yet */
3453                 if (bw < PHY_TXC1_BW_20MHZ) {
3454                         wiphy_err(wlc->wiphy, "wlc_phytxctl1_calc: bw %d is "
3455                                   "not supported yet, set to 20L\n", bw);
3456                         bw = PHY_TXC1_BW_20MHZ;
3457                 }
3458         }
3459
3460         if (IS_MCS(rspec)) {
3461                 uint mcs = rspec & RSPEC_RATE_MASK;
3462
3463                 /* bw, stf, coding-type is part of RSPEC_PHYTXBYTE2 returns */
3464                 phyctl1 = RSPEC_PHYTXBYTE2(rspec);
3465                 /* set the upper byte of phyctl1 */
3466                 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
3467         } else if (IS_CCK(rspec) && !WLCISLCNPHY(wlc->band)
3468                    && !WLCISSSLPNPHY(wlc->band)) {
3469                 /* In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate */
3470                 /* Eventually MIMOPHY would also be converted to this format */
3471                 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
3472                 phyctl1 = (bw | (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
3473         } else {                /* legacy OFDM/CCK */
3474                 s16 phycfg;
3475                 /* get the phyctl byte from rate phycfg table */
3476                 phycfg = wlc_rate_legacy_phyctl(RSPEC2RATE(rspec));
3477                 if (phycfg == -1) {
3478                         wiphy_err(wlc->wiphy, "wlc_phytxctl1_calc: wrong "
3479                                   "legacy OFDM/CCK rate\n");
3480                         phycfg = 0;
3481                 }
3482                 /* set the upper byte of phyctl1 */
3483                 phyctl1 =
3484                     (bw | (phycfg << 8) |
3485                      (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
3486         }
3487         return phyctl1;
3488 }
3489
3490 ratespec_t
3491 wlc_rspec_to_rts_rspec(struct wlc_info *wlc, ratespec_t rspec, bool use_rspec,
3492                        u16 mimo_ctlchbw)
3493 {
3494         ratespec_t rts_rspec = 0;
3495
3496         if (use_rspec) {
3497                 /* use frame rate as rts rate */
3498                 rts_rspec = rspec;
3499
3500         } else if (wlc->band->gmode && wlc->protection->_g && !IS_CCK(rspec)) {
3501                 /* Use 11Mbps as the g protection RTS target rate and fallback.
3502                  * Use the WLC_BASIC_RATE() lookup to find the best basic rate under the
3503                  * target in case 11 Mbps is not Basic.
3504                  * 6 and 9 Mbps are not usually selected by rate selection, but even
3505                  * if the OFDM rate we are protecting is 6 or 9 Mbps, 11 is more robust.
3506                  */
3507                 rts_rspec = WLC_BASIC_RATE(wlc, WLC_RATE_11M);
3508         } else {
3509                 /* calculate RTS rate and fallback rate based on the frame rate
3510                  * RTS must be sent at a basic rate since it is a
3511                  * control frame, sec 9.6 of 802.11 spec
3512                  */
3513                 rts_rspec = WLC_BASIC_RATE(wlc, rspec);
3514         }
3515
3516         if (WLC_PHY_11N_CAP(wlc->band)) {
3517                 /* set rts txbw to correct side band */
3518                 rts_rspec &= ~RSPEC_BW_MASK;
3519
3520                 /* if rspec/rspec_fallback is 40MHz, then send RTS on both 20MHz channel
3521                  * (DUP), otherwise send RTS on control channel
3522                  */
3523                 if (RSPEC_IS40MHZ(rspec) && !IS_CCK(rts_rspec))
3524                         rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
3525                 else
3526                         rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
3527
3528                 /* pick siso/cdd as default for ofdm */
3529                 if (IS_OFDM(rts_rspec)) {
3530                         rts_rspec &= ~RSPEC_STF_MASK;
3531                         rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
3532                 }
3533         }
3534         return rts_rspec;
3535 }
3536
3537 /*
3538  * Add d11txh_t, cck_phy_hdr_t.
3539  *
3540  * 'p' data must start with 802.11 MAC header
3541  * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
3542  *
3543  * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
3544  *
3545  */
3546 static u16
3547 wlc_d11hdrs_mac80211(struct wlc_info *wlc, struct ieee80211_hw *hw,
3548                      struct sk_buff *p, struct scb *scb, uint frag,
3549                      uint nfrags, uint queue, uint next_frag_len,
3550                      wsec_key_t *key, ratespec_t rspec_override)
3551 {
3552         struct ieee80211_hdr *h;
3553         d11txh_t *txh;
3554         u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
3555         int len, phylen, rts_phylen;
3556         u16 mch, phyctl, xfts, mainrates;
3557         u16 seq = 0, mcl = 0, status = 0, frameid = 0;
3558         ratespec_t rspec[2] = { WLC_RATE_1M, WLC_RATE_1M }, rts_rspec[2] = {
3559         WLC_RATE_1M, WLC_RATE_1M};
3560         bool use_rts = false;
3561         bool use_cts = false;
3562         bool use_rifs = false;
3563         bool short_preamble[2] = { false, false };
3564         u8 preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
3565         u8 rts_preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
3566         u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
3567         struct ieee80211_rts *rts = NULL;
3568         bool qos;
3569         uint ac;
3570         u32 rate_val[2];
3571         bool hwtkmic = false;
3572         u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
3573 #define ANTCFG_NONE 0xFF
3574         u8 antcfg = ANTCFG_NONE;
3575         u8 fbantcfg = ANTCFG_NONE;
3576         uint phyctl1_stf = 0;
3577         u16 durid = 0;
3578         struct ieee80211_tx_rate *txrate[2];
3579         int k;
3580         struct ieee80211_tx_info *tx_info;
3581         bool is_mcs[2];
3582         u16 mimo_txbw;
3583         u8 mimo_preamble_type;
3584
3585         /* locate 802.11 MAC header */
3586         h = (struct ieee80211_hdr *)(p->data);
3587         qos = ieee80211_is_data_qos(h->frame_control);
3588
3589         /* compute length of frame in bytes for use in PLCP computations */
3590         len = bcm_pkttotlen(p);
3591         phylen = len + FCS_LEN;
3592
3593         /* If WEP enabled, add room in phylen for the additional bytes of
3594          * ICV which MAC generates.  We do NOT add the additional bytes to
3595          * the packet itself, thus phylen = packet length + ICV_LEN + FCS_LEN
3596          * in this case
3597          */
3598         if (key) {
3599                 phylen += key->icv_len;
3600         }
3601
3602         /* Get tx_info */
3603         tx_info = IEEE80211_SKB_CB(p);
3604
3605         /* add PLCP */
3606         plcp = skb_push(p, D11_PHY_HDR_LEN);
3607
3608         /* add Broadcom tx descriptor header */
3609         txh = (d11txh_t *) skb_push(p, D11_TXH_LEN);
3610         memset(txh, 0, D11_TXH_LEN);
3611
3612         /* setup frameid */
3613         if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
3614                 /* non-AP STA should never use BCMC queue */
3615                 if (queue == TX_BCMC_FIFO) {
3616                         wiphy_err(wlc->wiphy, "wl%d: %s: ASSERT queue == "
3617                                   "TX_BCMC!\n", WLCWLUNIT(wlc), __func__);
3618                         frameid = bcmc_fid_generate(wlc, NULL, txh);
3619                 } else {
3620                         /* Increment the counter for first fragment */
3621                         if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
3622                                 SCB_SEQNUM(scb, p->priority)++;
3623                         }
3624
3625                         /* extract fragment number from frame first */
3626                         seq = le16_to_cpu(seq) & FRAGNUM_MASK;
3627                         seq |= (SCB_SEQNUM(scb, p->priority) << SEQNUM_SHIFT);
3628                         h->seq_ctrl = cpu_to_le16(seq);
3629
3630                         frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
3631                             (queue & TXFID_QUEUE_MASK);
3632                 }
3633         }
3634         frameid |= queue & TXFID_QUEUE_MASK;
3635
3636         /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
3637         if (SCB_PS(scb) || ieee80211_is_beacon(h->frame_control))
3638                 mcl |= TXC_IGNOREPMQ;
3639
3640         txrate[0] = tx_info->control.rates;
3641         txrate[1] = txrate[0] + 1;
3642
3643         /* if rate control algorithm didn't give us a fallback rate, use the primary rate */
3644         if (txrate[1]->idx < 0) {
3645                 txrate[1] = txrate[0];
3646         }
3647
3648         for (k = 0; k < hw->max_rates; k++) {
3649                 is_mcs[k] =
3650                     txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
3651                 if (!is_mcs[k]) {
3652                         if ((txrate[k]->idx >= 0)
3653                             && (txrate[k]->idx <
3654                                 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
3655                                 rate_val[k] =
3656                                     hw->wiphy->bands[tx_info->band]->
3657                                     bitrates[txrate[k]->idx].hw_value;
3658                                 short_preamble[k] =
3659                                     txrate[k]->
3660                                     flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
3661                                     true : false;
3662                         } else {
3663                                 rate_val[k] = WLC_RATE_1M;
3664                         }
3665                 } else {
3666                         rate_val[k] = txrate[k]->idx;
3667                 }
3668                 /* Currently only support same setting for primay and fallback rates.
3669                  * Unify flags for each rate into a single value for the frame
3670                  */
3671                 use_rts |=
3672                     txrate[k]->
3673                     flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
3674                 use_cts |=
3675                     txrate[k]->
3676                     flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
3677
3678                 if (is_mcs[k])
3679                         rate_val[k] |= NRATE_MCS_INUSE;
3680
3681                 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band, rate_val[k]);
3682
3683                 /* (1) RATE: determine and validate primary rate and fallback rates */
3684                 if (!RSPEC_ACTIVE(rspec[k])) {
3685                         rspec[k] = WLC_RATE_1M;
3686                 } else {
3687                         if (!is_multicast_ether_addr(h->addr1)) {
3688                                 /* set tx antenna config */
3689                                 wlc_antsel_antcfg_get(wlc->asi, false, false, 0,
3690                                                       0, &antcfg, &fbantcfg);
3691                         }
3692                 }
3693         }
3694
3695         phyctl1_stf = wlc->stf->ss_opmode;
3696
3697         if (N_ENAB(wlc->pub)) {
3698                 for (k = 0; k < hw->max_rates; k++) {
3699                         /* apply siso/cdd to single stream mcs's or ofdm if rspec is auto selected */
3700                         if (((IS_MCS(rspec[k]) &&
3701                               IS_SINGLE_STREAM(rspec[k] & RSPEC_RATE_MASK)) ||
3702                              IS_OFDM(rspec[k]))
3703                             && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
3704                                 || !(rspec[k] & RSPEC_OVERRIDE))) {
3705                                 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
3706
3707                                 /* For SISO MCS use STBC if possible */
3708                                 if (IS_MCS(rspec[k])
3709                                     && WLC_STF_SS_STBC_TX(wlc, scb)) {
3710                                         u8 stc;
3711
3712                                         stc = 1;        /* Nss for single stream is always 1 */
3713                                         rspec[k] |=
3714                                             (PHY_TXC1_MODE_STBC <<
3715                                              RSPEC_STF_SHIFT) | (stc <<
3716                                                                  RSPEC_STC_SHIFT);
3717                                 } else
3718                                         rspec[k] |=
3719                                             (phyctl1_stf << RSPEC_STF_SHIFT);
3720                         }
3721
3722                         /* Is the phy configured to use 40MHZ frames? If so then pick the desired txbw */
3723                         if (CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ) {
3724                                 /* default txbw is 20in40 SB */
3725                                 mimo_ctlchbw = mimo_txbw =
3726                                     CHSPEC_SB_UPPER(WLC_BAND_PI_RADIO_CHANSPEC)
3727                                     ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
3728
3729                                 if (IS_MCS(rspec[k])) {
3730                                         /* mcs 32 must be 40b/w DUP */
3731                                         if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
3732                                                 mimo_txbw =
3733                                                     PHY_TXC1_BW_40MHZ_DUP;
3734                                                 /* use override */
3735                                         } else if (wlc->mimo_40txbw != AUTO)
3736                                                 mimo_txbw = wlc->mimo_40txbw;
3737                                         /* else check if dst is using 40 Mhz */
3738                                         else if (scb->flags & SCB_IS40)
3739                                                 mimo_txbw = PHY_TXC1_BW_40MHZ;
3740                                 } else if (IS_OFDM(rspec[k])) {
3741                                         if (wlc->ofdm_40txbw != AUTO)
3742                                                 mimo_txbw = wlc->ofdm_40txbw;
3743                                 } else {
3744                                         if (wlc->cck_40txbw != AUTO)
3745                                                 mimo_txbw = wlc->cck_40txbw;
3746                                 }
3747                         } else {
3748                                 /* mcs32 is 40 b/w only.
3749                                  * This is possible for probe packets on a STA during SCAN
3750                                  */
3751                                 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
3752                                         /* mcs 0 */
3753                                         rspec[k] = RSPEC_MIMORATE;
3754                                 }
3755                                 mimo_txbw = PHY_TXC1_BW_20MHZ;
3756                         }
3757
3758                         /* Set channel width */
3759                         rspec[k] &= ~RSPEC_BW_MASK;
3760                         if ((k == 0) || ((k > 0) && IS_MCS(rspec[k])))
3761                                 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
3762                         else
3763                                 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
3764
3765                         /* Set Short GI */
3766 #ifdef NOSGIYET
3767                         if (IS_MCS(rspec[k])
3768                             && (txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
3769                                 rspec[k] |= RSPEC_SHORT_GI;
3770                         else if (!(txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
3771                                 rspec[k] &= ~RSPEC_SHORT_GI;
3772 #else
3773                         rspec[k] &= ~RSPEC_SHORT_GI;
3774 #endif
3775
3776                         mimo_preamble_type = WLC_MM_PREAMBLE;
3777                         if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
3778                                 mimo_preamble_type = WLC_GF_PREAMBLE;
3779
3780                         if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
3781                             && (!IS_MCS(rspec[k]))) {
3782                                 wiphy_err(wlc->wiphy, "wl%d: %s: IEEE80211_TX_"
3783                                           "RC_MCS != IS_MCS(rspec)\n",
3784                                           WLCWLUNIT(wlc), __func__);
3785                         }
3786
3787                         if (IS_MCS(rspec[k])) {
3788                                 preamble_type[k] = mimo_preamble_type;
3789
3790                                 /* if SGI is selected, then forced mm for single stream */
3791                                 if ((rspec[k] & RSPEC_SHORT_GI)
3792                                     && IS_SINGLE_STREAM(rspec[k] &
3793                                                         RSPEC_RATE_MASK)) {
3794                                         preamble_type[k] = WLC_MM_PREAMBLE;
3795                                 }
3796                         }
3797
3798                         /* should be better conditionalized */
3799                         if (!IS_MCS(rspec[0])
3800                             && (tx_info->control.rates[0].
3801                                 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
3802                                 preamble_type[k] = WLC_SHORT_PREAMBLE;
3803                 }
3804         } else {
3805                 for (k = 0; k < hw->max_rates; k++) {
3806                         /* Set ctrlchbw as 20Mhz */
3807                         rspec[k] &= ~RSPEC_BW_MASK;
3808                         rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
3809
3810                         /* for nphy, stf of ofdm frames must follow policies */
3811                         if (WLCISNPHY(wlc->band) && IS_OFDM(rspec[k])) {
3812                                 rspec[k] &= ~RSPEC_STF_MASK;
3813                                 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
3814                         }
3815                 }
3816         }
3817
3818         /* Reset these for use with AMPDU's */
3819         txrate[0]->count = 0;
3820         txrate[1]->count = 0;
3821
3822         /* (2) PROTECTION, may change rspec */
3823         if ((ieee80211_is_data(h->frame_control) ||
3824             ieee80211_is_mgmt(h->frame_control)) &&
3825             (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
3826                 use_rts = true;
3827
3828         /* (3) PLCP: determine PLCP header and MAC duration, fill d11txh_t */
3829         wlc_compute_plcp(wlc, rspec[0], phylen, plcp);
3830         wlc_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
3831         memcpy(&txh->FragPLCPFallback,
3832                plcp_fallback, sizeof(txh->FragPLCPFallback));
3833
3834         /* Length field now put in CCK FBR CRC field */
3835         if (IS_CCK(rspec[1])) {
3836                 txh->FragPLCPFallback[4] = phylen & 0xff;
3837                 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
3838         }
3839
3840         /* MIMO-RATE: need validation ?? */
3841         mainrates =
3842             IS_OFDM(rspec[0]) ? D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) plcp) :
3843             plcp[0];
3844
3845         /* DUR field for main rate */
3846         if (!ieee80211_is_pspoll(h->frame_control) &&
3847             !is_multicast_ether_addr(h->addr1) && !use_rifs) {
3848                 durid =
3849                     wlc_compute_frame_dur(wlc, rspec[0], preamble_type[0],
3850                                           next_frag_len);
3851                 h->duration_id = cpu_to_le16(durid);
3852         } else if (use_rifs) {
3853                 /* NAV protect to end of next max packet size */
3854                 durid =
3855                     (u16) wlc_calc_frame_time(wlc, rspec[0],
3856                                                  preamble_type[0],
3857                                                  DOT11_MAX_FRAG_LEN);
3858                 durid += RIFS_11N_TIME;
3859                 h->duration_id = cpu_to_le16(durid);
3860         }
3861
3862         /* DUR field for fallback rate */
3863         if (ieee80211_is_pspoll(h->frame_control))
3864                 txh->FragDurFallback = h->duration_id;
3865         else if (is_multicast_ether_addr(h->addr1) || use_rifs)
3866                 txh->FragDurFallback = 0;
3867         else {
3868                 durid = wlc_compute_frame_dur(wlc, rspec[1],
3869                                               preamble_type[1], next_frag_len);
3870                 txh->FragDurFallback = cpu_to_le16(durid);
3871         }
3872
3873         /* (4) MAC-HDR: MacTxControlLow */
3874         if (frag == 0)
3875                 mcl |= TXC_STARTMSDU;
3876
3877         if (!is_multicast_ether_addr(h->addr1))
3878                 mcl |= TXC_IMMEDACK;
3879
3880         if (BAND_5G(wlc->band->bandtype))
3881                 mcl |= TXC_FREQBAND_5G;
3882
3883         if (CHSPEC_IS40(WLC_BAND_PI_RADIO_CHANSPEC))
3884                 mcl |= TXC_BW_40;
3885
3886         /* set AMIC bit if using hardware TKIP MIC */
3887         if (hwtkmic)
3888                 mcl |= TXC_AMIC;
3889
3890         txh->MacTxControlLow = cpu_to_le16(mcl);
3891
3892         /* MacTxControlHigh */
3893         mch = 0;
3894
3895         /* Set fallback rate preamble type */
3896         if ((preamble_type[1] == WLC_SHORT_PREAMBLE) ||
3897             (preamble_type[1] == WLC_GF_PREAMBLE)) {
3898                 if (RSPEC2RATE(rspec[1]) != WLC_RATE_1M)
3899                         mch |= TXC_PREAMBLE_DATA_FB_SHORT;
3900         }
3901
3902         /* MacFrameControl */
3903         memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
3904         txh->TxFesTimeNormal = cpu_to_le16(0);
3905
3906         txh->TxFesTimeFallback = cpu_to_le16(0);
3907
3908         /* TxFrameRA */
3909         memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
3910
3911         /* TxFrameID */
3912         txh->TxFrameID = cpu_to_le16(frameid);
3913
3914         /* TxStatus, Note the case of recreating the first frag of a suppressed frame
3915          * then we may need to reset the retry cnt's via the status reg
3916          */
3917         txh->TxStatus = cpu_to_le16(status);
3918
3919         /* extra fields for ucode AMPDU aggregation, the new fields are added to
3920          * the END of previous structure so that it's compatible in driver.
3921          */
3922         txh->MaxNMpdus = cpu_to_le16(0);
3923         txh->MaxABytes_MRT = cpu_to_le16(0);
3924         txh->MaxABytes_FBR = cpu_to_le16(0);
3925         txh->MinMBytes = cpu_to_le16(0);
3926
3927         /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration, furnish d11txh_t */
3928         /* RTS PLCP header and RTS frame */
3929         if (use_rts || use_cts) {
3930                 if (use_rts && use_cts)
3931                         use_cts = false;
3932
3933                 for (k = 0; k < 2; k++) {
3934                         rts_rspec[k] = wlc_rspec_to_rts_rspec(wlc, rspec[k],
3935                                                               false,
3936                                                               mimo_ctlchbw);
3937                 }
3938
3939                 if (!IS_OFDM(rts_rspec[0]) &&
3940                     !((RSPEC2RATE(rts_rspec[0]) == WLC_RATE_1M) ||
3941                       (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
3942                         rts_preamble_type[0] = WLC_SHORT_PREAMBLE;
3943                         mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
3944                 }
3945
3946                 if (!IS_OFDM(rts_rspec[1]) &&
3947                     !((RSPEC2RATE(rts_rspec[1]) == WLC_RATE_1M) ||
3948                       (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
3949                         rts_preamble_type[1] = WLC_SHORT_PREAMBLE;
3950                         mch |= TXC_PREAMBLE_RTS_FB_SHORT;
3951                 }
3952
3953                 /* RTS/CTS additions to MacTxControlLow */
3954                 if (use_cts) {
3955                         txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
3956                 } else {
3957                         txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
3958                         txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
3959                 }
3960
3961                 /* RTS PLCP header */
3962                 rts_plcp = txh->RTSPhyHeader;
3963                 if (use_cts)
3964                         rts_phylen = DOT11_CTS_LEN + FCS_LEN;
3965                 else
3966                         rts_phylen = DOT11_RTS_LEN + FCS_LEN;
3967
3968                 wlc_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
3969
3970                 /* fallback rate version of RTS PLCP header */
3971                 wlc_compute_plcp(wlc, rts_rspec[1], rts_phylen,
3972                                  rts_plcp_fallback);
3973                 memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
3974                        sizeof(txh->RTSPLCPFallback));
3975
3976                 /* RTS frame fields... */
3977                 rts = (struct ieee80211_rts *)&txh->rts_frame;
3978
3979                 durid = wlc_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
3980                                                rspec[0], rts_preamble_type[0],
3981                                                preamble_type[0], phylen, false);
3982                 rts->duration = cpu_to_le16(durid);
3983                 /* fallback rate version of RTS DUR field */
3984                 durid = wlc_compute_rtscts_dur(wlc, use_cts,
3985                                                rts_rspec[1], rspec[1],
3986                                                rts_preamble_type[1],
3987                                                preamble_type[1], phylen, false);
3988                 txh->RTSDurFallback = cpu_to_le16(durid);
3989
3990                 if (use_cts) {
3991                         rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
3992                                                          IEEE80211_STYPE_CTS);
3993
3994                         memcpy(&rts->ra, &h->addr2, ETH_ALEN);
3995                 } else {
3996                         rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
3997                                                          IEEE80211_STYPE_RTS);
3998
3999                         memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
4000                 }
4001
4002                 /* mainrate
4003                  *    low 8 bits: main frag rate/mcs,
4004                  *    high 8 bits: rts/cts rate/mcs
4005                  */
4006                 mainrates |= (IS_OFDM(rts_rspec[0]) ?
4007                               D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) rts_plcp) :
4008                               rts_plcp[0]) << 8;
4009         } else {
4010                 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
4011                 memset((char *)&txh->rts_frame, 0,
4012                         sizeof(struct ieee80211_rts));
4013                 memset((char *)txh->RTSPLCPFallback, 0,
4014                       sizeof(txh->RTSPLCPFallback));
4015                 txh->RTSDurFallback = 0;
4016         }
4017
4018 #ifdef SUPPORT_40MHZ
4019         /* add null delimiter count */
4020         if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && IS_MCS(rspec)) {
4021                 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
4022                     wlc_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
4023         }
4024 #endif
4025
4026         /* Now that RTS/RTS FB preamble types are updated, write the final value */
4027         txh->MacTxControlHigh = cpu_to_le16(mch);
4028
4029         /* MainRates (both the rts and frag plcp rates have been calculated now) */
4030         txh->MainRates = cpu_to_le16(mainrates);
4031
4032         /* XtraFrameTypes */
4033         xfts = FRAMETYPE(rspec[1], wlc->mimoft);
4034         xfts |= (FRAMETYPE(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
4035         xfts |= (FRAMETYPE(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
4036         xfts |=
4037             CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC) << XFTS_CHANNEL_SHIFT;
4038         txh->XtraFrameTypes = cpu_to_le16(xfts);
4039
4040         /* PhyTxControlWord */
4041         phyctl = FRAMETYPE(rspec[0], wlc->mimoft);
4042         if ((preamble_type[0] == WLC_SHORT_PREAMBLE) ||
4043             (preamble_type[0] == WLC_GF_PREAMBLE)) {
4044                 if (RSPEC2RATE(rspec[0]) != WLC_RATE_1M)
4045                         phyctl |= PHY_TXC_SHORT_HDR;
4046         }
4047
4048         /* phytxant is properly bit shifted */
4049         phyctl |= wlc_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
4050         txh->PhyTxControlWord = cpu_to_le16(phyctl);
4051
4052         /* PhyTxControlWord_1 */
4053         if (WLC_PHY_11N_CAP(wlc->band)) {
4054                 u16 phyctl1 = 0;
4055
4056                 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[0]);
4057                 txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
4058                 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[1]);
4059                 txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
4060
4061                 if (use_rts || use_cts) {
4062                         phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[0]);
4063                         txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
4064                         phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[1]);
4065                         txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
4066                 }
4067
4068                 /*
4069                  * For mcs frames, if mixedmode(overloaded with long preamble) is going to be set,
4070                  * fill in non-zero MModeLen and/or MModeFbrLen
4071                  *  it will be unnecessary if they are separated
4072                  */
4073                 if (IS_MCS(rspec[0]) && (preamble_type[0] == WLC_MM_PREAMBLE)) {
4074                         u16 mmodelen =
4075                             wlc_calc_lsig_len(wlc, rspec[0], phylen);
4076                         txh->MModeLen = cpu_to_le16(mmodelen);
4077                 }
4078
4079                 if (IS_MCS(rspec[1]) && (preamble_type[1] == WLC_MM_PREAMBLE)) {
4080                         u16 mmodefbrlen =
4081                             wlc_calc_lsig_len(wlc, rspec[1], phylen);
4082                         txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
4083                 }
4084         }
4085
4086         ac = skb_get_queue_mapping(p);
4087         if (SCB_WME(scb) && qos && wlc->edcf_txop[ac]) {
4088                 uint frag_dur, dur, dur_fallback;
4089
4090                 /* WME: Update TXOP threshold */
4091                 if ((!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) && (frag == 0)) {
4092                         frag_dur =
4093                             wlc_calc_frame_time(wlc, rspec[0], preamble_type[0],
4094                                                 phylen);
4095
4096                         if (rts) {
4097                                 /* 1 RTS or CTS-to-self frame */
4098                                 dur =
4099                                     wlc_calc_cts_time(wlc, rts_rspec[0],
4100                                                       rts_preamble_type[0]);
4101                                 dur_fallback =
4102                                     wlc_calc_cts_time(wlc, rts_rspec[1],
4103                                                       rts_preamble_type[1]);
4104                                 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
4105                                 dur += le16_to_cpu(rts->duration);
4106                                 dur_fallback +=
4107                                         le16_to_cpu(txh->RTSDurFallback);
4108                         } else if (use_rifs) {
4109                                 dur = frag_dur;
4110                                 dur_fallback = 0;
4111                         } else {
4112                                 /* frame + SIFS + ACK */
4113                                 dur = frag_dur;
4114                                 dur +=
4115                                     wlc_compute_frame_dur(wlc, rspec[0],
4116                                                           preamble_type[0], 0);
4117
4118                                 dur_fallback =
4119                                     wlc_calc_frame_time(wlc, rspec[1],
4120                                                         preamble_type[1],
4121                                                         phylen);
4122                                 dur_fallback +=
4123                                     wlc_compute_frame_dur(wlc, rspec[1],
4124                                                           preamble_type[1], 0);
4125                         }
4126                         /* NEED to set TxFesTimeNormal (hard) */
4127                         txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
4128                         /* NEED to set fallback rate version of TxFesTimeNormal (hard) */
4129                         txh->TxFesTimeFallback =
4130                                 cpu_to_le16((u16) dur_fallback);
4131
4132                         /* update txop byte threshold (txop minus intraframe overhead) */
4133                         if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
4134                                 {
4135                                         uint newfragthresh;
4136
4137                                         newfragthresh =
4138                                             wlc_calc_frame_len(wlc, rspec[0],
4139                                                                preamble_type[0],
4140                                                                (wlc->
4141                                                                 edcf_txop[ac] -
4142                                                                 (dur -
4143                                                                  frag_dur)));
4144                                         /* range bound the fragthreshold */
4145                                         if (newfragthresh < DOT11_MIN_FRAG_LEN)
4146                                                 newfragthresh =
4147                                                     DOT11_MIN_FRAG_LEN;
4148                                         else if (newfragthresh >
4149                                                  wlc->usr_fragthresh)
4150                                                 newfragthresh =
4151                                                     wlc->usr_fragthresh;
4152                                         /* update the fragthresh and do txc update */
4153                                         if (wlc->fragthresh[queue] !=
4154                                             (u16) newfragthresh) {
4155                                                 wlc->fragthresh[queue] =
4156                                                     (u16) newfragthresh;
4157                                         }
4158                                 }
4159                         } else
4160                                 wiphy_err(wlc->wiphy, "wl%d: %s txop invalid "
4161                                           "for rate %d\n",
4162                                           wlc->pub->unit, fifo_names[queue],
4163                                           RSPEC2RATE(rspec[0]));
4164
4165                         if (dur > wlc->edcf_txop[ac])
4166                                 wiphy_err(wlc->wiphy, "wl%d: %s: %s txop "
4167                                           "exceeded phylen %d/%d dur %d/%d\n",
4168                                           wlc->pub->unit, __func__,
4169                                           fifo_names[queue],
4170                                           phylen, wlc->fragthresh[queue],
4171                                           dur, wlc->edcf_txop[ac]);
4172                 }
4173         }
4174
4175         return 0;
4176 }
4177
4178 void wlc_tbtt(struct wlc_info *wlc, d11regs_t *regs)
4179 {
4180         struct wlc_bsscfg *cfg = wlc->cfg;
4181
4182         if (!cfg->BSS) {
4183                 /* DirFrmQ is now valid...defer setting until end of ATIM window */
4184                 wlc->qvalid |= MCMD_DIRFRMQVAL;
4185         }
4186 }
4187
4188 static void wlc_war16165(struct wlc_info *wlc, bool tx)
4189 {
4190         if (tx) {
4191                 /* the post-increment is used in STAY_AWAKE macro */
4192                 if (wlc->txpend16165war++ == 0)
4193                         wlc_set_ps_ctrl(wlc);
4194         } else {
4195                 wlc->txpend16165war--;
4196                 if (wlc->txpend16165war == 0)
4197                         wlc_set_ps_ctrl(wlc);
4198         }
4199 }
4200
4201 /* process an individual tx_status_t */
4202 /* WLC_HIGH_API */
4203 bool
4204 wlc_dotxstatus(struct wlc_info *wlc, tx_status_t *txs, u32 frm_tx2)
4205 {
4206         struct sk_buff *p;
4207         uint queue;
4208         d11txh_t *txh;
4209         struct scb *scb = NULL;
4210         bool free_pdu;
4211         int tx_rts, tx_frame_count, tx_rts_count;
4212         uint totlen, supr_status;
4213         bool lastframe;
4214         struct ieee80211_hdr *h;
4215         u16 mcl;
4216         struct ieee80211_tx_info *tx_info;
4217         struct ieee80211_tx_rate *txrate;
4218         int i;
4219
4220         (void)(frm_tx2);        /* Compiler reference to avoid unused variable warning */
4221
4222         /* discard intermediate indications for ucode with one legitimate case:
4223          *   e.g. if "useRTS" is set. ucode did a successful rts/cts exchange, but the subsequent
4224          *   tx of DATA failed. so it will start rts/cts from the beginning (resetting the rts
4225          *   transmission count)
4226          */
4227         if (!(txs->status & TX_STATUS_AMPDU)
4228             && (txs->status & TX_STATUS_INTERMEDIATE)) {
4229                 wiphy_err(wlc->wiphy, "%s: INTERMEDIATE but not AMPDU\n",
4230                           __func__);
4231                 return false;
4232         }
4233
4234         queue = txs->frameid & TXFID_QUEUE_MASK;
4235         if (queue >= NFIFO) {
4236                 p = NULL;
4237                 goto fatal;
4238         }
4239
4240         p = GETNEXTTXP(wlc, queue);
4241         if (WLC_WAR16165(wlc))
4242                 wlc_war16165(wlc, false);
4243         if (p == NULL)
4244                 goto fatal;
4245
4246         txh = (d11txh_t *) (p->data);
4247         mcl = le16_to_cpu(txh->MacTxControlLow);
4248
4249         if (txs->phyerr) {
4250                 if (WL_ERROR_ON()) {
4251                         wiphy_err(wlc->wiphy, "phyerr 0x%x, rate 0x%x\n",
4252                                   txs->phyerr, txh->MainRates);
4253                         wlc_print_txdesc(txh);
4254                 }
4255                 wlc_print_txstatus(txs);
4256         }
4257
4258         if (txs->frameid != cpu_to_le16(txh->TxFrameID))
4259                 goto fatal;
4260         tx_info = IEEE80211_SKB_CB(p);
4261         h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
4262
4263         if (tx_info->control.sta)
4264                 scb = (struct scb *)tx_info->control.sta->drv_priv;
4265
4266         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
4267                 wlc_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
4268                 return false;
4269         }
4270
4271         supr_status = txs->status & TX_STATUS_SUPR_MASK;
4272         if (supr_status == TX_STATUS_SUPR_BADCH)
4273                 BCMMSG(wlc->wiphy,
4274                        "%s: Pkt tx suppressed, possibly channel %d\n",
4275                        __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
4276
4277         tx_rts = cpu_to_le16(txh->MacTxControlLow) & TXC_SENDRTS;
4278         tx_frame_count =
4279             (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
4280         tx_rts_count =
4281             (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
4282
4283         lastframe = !ieee80211_has_morefrags(h->frame_control);
4284
4285         if (!lastframe) {
4286                 wiphy_err(wlc->wiphy, "Not last frame!\n");
4287         } else {
4288                 u16 sfbl, lfbl;
4289                 ieee80211_tx_info_clear_status(tx_info);
4290                 if (queue < AC_COUNT) {
4291                         sfbl = WLC_WME_RETRY_SFB_GET(wlc, wme_fifo2ac[queue]);
4292                         lfbl = WLC_WME_RETRY_LFB_GET(wlc, wme_fifo2ac[queue]);
4293                 } else {
4294                         sfbl = wlc->SFBL;
4295                         lfbl = wlc->LFBL;
4296                 }
4297
4298                 txrate = tx_info->status.rates;
4299                 /* FIXME: this should use a combination of sfbl, lfbl depending on frame length and RTS setting */
4300                 if ((tx_frame_count > sfbl) && (txrate[1].idx >= 0)) {
4301                         /* rate selection requested a fallback rate and we used it */
4302                         txrate->count = lfbl;
4303                         txrate[1].count = tx_frame_count - lfbl;
4304                 } else {
4305                         /* rate selection did not request fallback rate, or we didn't need it */
4306                         txrate->count = tx_frame_count;
4307                         /* rc80211_minstrel.c:minstrel_tx_status() expects unused rates to be marked with idx = -1 */
4308                         txrate[1].idx = -1;
4309                         txrate[1].count = 0;
4310                 }
4311
4312                 /* clear the rest of the rates */
4313                 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
4314                         txrate[i].idx = -1;
4315                         txrate[i].count = 0;
4316                 }
4317
4318                 if (txs->status & TX_STATUS_ACK_RCV)
4319                         tx_info->flags |= IEEE80211_TX_STAT_ACK;
4320         }
4321
4322         totlen = bcm_pkttotlen(p);
4323         free_pdu = true;
4324
4325         wlc_txfifo_complete(wlc, queue, 1);
4326
4327         if (lastframe) {
4328                 p->next = NULL;
4329                 p->prev = NULL;
4330                 /* remove PLCP & Broadcom tx descriptor header */
4331                 skb_pull(p, D11_PHY_HDR_LEN);
4332                 skb_pull(p, D11_TXH_LEN);
4333                 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
4334         } else {
4335                 wiphy_err(wlc->wiphy, "%s: Not last frame => not calling "
4336                           "tx_status\n", __func__);
4337         }
4338
4339         return false;
4340
4341  fatal:
4342         if (p)
4343                 bcm_pkt_buf_free_skb(p);
4344
4345         return true;
4346
4347 }
4348
4349 void
4350 wlc_txfifo_complete(struct wlc_info *wlc, uint fifo, s8 txpktpend)
4351 {
4352         TXPKTPENDDEC(wlc, fifo, txpktpend);
4353         BCMMSG(wlc->wiphy, "pktpend dec %d to %d\n", txpktpend,
4354                 TXPKTPENDGET(wlc, fifo));
4355
4356         /* There is more room; mark precedences related to this FIFO sendable */
4357         WLC_TX_FIFO_ENAB(wlc, fifo);
4358
4359         /* Clear MHF2_TXBCMC_NOW flag if BCMC fifo has drained */
4360         if (AP_ENAB(wlc->pub) &&
4361             !TXPKTPENDGET(wlc, TX_BCMC_FIFO)) {
4362                 wlc_mhf(wlc, MHF2, MHF2_TXBCMC_NOW, 0, WLC_BAND_AUTO);
4363         }
4364
4365         /* figure out which bsscfg is being worked on... */
4366 }
4367
4368 /* Update beacon listen interval in shared memory */
4369 void wlc_bcn_li_upd(struct wlc_info *wlc)
4370 {
4371         if (AP_ENAB(wlc->pub))
4372                 return;
4373
4374         /* wake up every DTIM is the default */
4375         if (wlc->bcn_li_dtim == 1)
4376                 wlc_write_shm(wlc, M_BCN_LI, 0);
4377         else
4378                 wlc_write_shm(wlc, M_BCN_LI,
4379                               (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
4380 }
4381
4382 /*
4383  * recover 64bit TSF value from the 16bit TSF value in the rx header
4384  * given the assumption that the TSF passed in header is within 65ms
4385  * of the current tsf.
4386  *
4387  * 6       5       4       4       3       2       1
4388  * 3.......6.......8.......0.......2.......4.......6.......8......0
4389  * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
4390  *
4391  * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
4392  * tsf_l is filled in by wlc_bmac_recv, which is done earlier in the
4393  * receive call sequence after rx interrupt. Only the higher 16 bits
4394  * are used. Finally, the tsf_h is read from the tsf register.
4395  */
4396 static u64 wlc_recover_tsf64(struct wlc_info *wlc, struct wlc_d11rxhdr *rxh)
4397 {
4398         u32 tsf_h, tsf_l;
4399         u16 rx_tsf_0_15, rx_tsf_16_31;
4400
4401         wlc_bmac_read_tsf(wlc->hw, &tsf_l, &tsf_h);
4402
4403         rx_tsf_16_31 = (u16)(tsf_l >> 16);
4404         rx_tsf_0_15 = rxh->rxhdr.RxTSFTime;
4405
4406         /*
4407          * a greater tsf time indicates the low 16 bits of
4408          * tsf_l wrapped, so decrement the high 16 bits.
4409          */
4410         if ((u16)tsf_l < rx_tsf_0_15) {
4411                 rx_tsf_16_31 -= 1;
4412                 if (rx_tsf_16_31 == 0xffff)
4413                         tsf_h -= 1;
4414         }
4415
4416         return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
4417 }
4418
4419 static void
4420 prep_mac80211_status(struct wlc_info *wlc, d11rxhdr_t *rxh, struct sk_buff *p,
4421                      struct ieee80211_rx_status *rx_status)
4422 {
4423         wlc_d11rxhdr_t *wlc_rxh = (wlc_d11rxhdr_t *) rxh;
4424         int preamble;
4425         int channel;
4426         ratespec_t rspec;
4427         unsigned char *plcp;
4428
4429         /* fill in TSF and flag its presence */
4430         rx_status->mactime = wlc_recover_tsf64(wlc, wlc_rxh);
4431         rx_status->flag |= RX_FLAG_MACTIME_MPDU;
4432
4433         channel = WLC_CHAN_CHANNEL(rxh->RxChan);
4434
4435         if (channel > 14) {
4436                 rx_status->band = IEEE80211_BAND_5GHZ;
4437                 rx_status->freq = ieee80211_ofdm_chan_to_freq(
4438                                         WF_CHAN_FACTOR_5_G/2, channel);
4439
4440         } else {
4441                 rx_status->band = IEEE80211_BAND_2GHZ;
4442                 rx_status->freq = ieee80211_dsss_chan_to_freq(channel);
4443         }
4444
4445         rx_status->signal = wlc_rxh->rssi;      /* signal */
4446
4447         /* noise */
4448         /* qual */
4449         rx_status->antenna = (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;      /* ant */
4450
4451         plcp = p->data;
4452
4453         rspec = wlc_compute_rspec(rxh, plcp);
4454         if (IS_MCS(rspec)) {
4455                 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
4456                 rx_status->flag |= RX_FLAG_HT;
4457                 if (RSPEC_IS40MHZ(rspec))
4458                         rx_status->flag |= RX_FLAG_40MHZ;
4459         } else {
4460                 switch (RSPEC2RATE(rspec)) {
4461                 case WLC_RATE_1M:
4462                         rx_status->rate_idx = 0;
4463                         break;
4464                 case WLC_RATE_2M:
4465                         rx_status->rate_idx = 1;
4466                         break;
4467                 case WLC_RATE_5M5:
4468                         rx_status->rate_idx = 2;
4469                         break;
4470                 case WLC_RATE_11M:
4471                         rx_status->rate_idx = 3;
4472                         break;
4473                 case WLC_RATE_6M:
4474                         rx_status->rate_idx = 4;
4475                         break;
4476                 case WLC_RATE_9M:
4477                         rx_status->rate_idx = 5;
4478                         break;
4479                 case WLC_RATE_12M:
4480                         rx_status->rate_idx = 6;
4481                         break;
4482                 case WLC_RATE_18M:
4483                         rx_status->rate_idx = 7;
4484                         break;
4485                 case WLC_RATE_24M:
4486                         rx_status->rate_idx = 8;
4487                         break;
4488                 case WLC_RATE_36M:
4489                         rx_status->rate_idx = 9;
4490                         break;
4491                 case WLC_RATE_48M:
4492                         rx_status->rate_idx = 10;
4493                         break;
4494                 case WLC_RATE_54M:
4495                         rx_status->rate_idx = 11;
4496                         break;
4497                 default:
4498                         wiphy_err(wlc->wiphy, "%s: Unknown rate\n", __func__);
4499                 }
4500
4501                 /* Determine short preamble and rate_idx */
4502                 preamble = 0;
4503                 if (IS_CCK(rspec)) {
4504                         if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
4505                                 rx_status->flag |= RX_FLAG_SHORTPRE;
4506                 } else if (IS_OFDM(rspec)) {
4507                         rx_status->flag |= RX_FLAG_SHORTPRE;
4508                 } else {
4509                         wiphy_err(wlc->wiphy, "%s: Unknown modulation\n",
4510                                   __func__);
4511                 }
4512         }
4513
4514         if (PLCP3_ISSGI(plcp[3]))
4515                 rx_status->flag |= RX_FLAG_SHORT_GI;
4516
4517         if (rxh->RxStatus1 & RXS_DECERR) {
4518                 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
4519                 wiphy_err(wlc->wiphy, "%s:  RX_FLAG_FAILED_PLCP_CRC\n",
4520                           __func__);
4521         }
4522         if (rxh->RxStatus1 & RXS_FCSERR) {
4523                 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
4524                 wiphy_err(wlc->wiphy, "%s:  RX_FLAG_FAILED_FCS_CRC\n",
4525                           __func__);
4526         }
4527 }
4528
4529 static void
4530 wlc_recvctl(struct wlc_info *wlc, d11rxhdr_t *rxh, struct sk_buff *p)
4531 {
4532         int len_mpdu;
4533         struct ieee80211_rx_status rx_status;
4534
4535         memset(&rx_status, 0, sizeof(rx_status));
4536         prep_mac80211_status(wlc, rxh, p, &rx_status);
4537
4538         /* mac header+body length, exclude CRC and plcp header */
4539         len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
4540         skb_pull(p, D11_PHY_HDR_LEN);
4541         __skb_trim(p, len_mpdu);
4542
4543         memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
4544         ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
4545         return;
4546 }
4547
4548 /* Process received frames */
4549 /*
4550  * Return true if more frames need to be processed. false otherwise.
4551  * Param 'bound' indicates max. # frames to process before break out.
4552  */
4553 /* WLC_HIGH_API */
4554 void wlc_recv(struct wlc_info *wlc, struct sk_buff *p)
4555 {
4556         d11rxhdr_t *rxh;
4557         struct ieee80211_hdr *h;
4558         uint len;
4559         bool is_amsdu;
4560
4561         BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
4562
4563         /* frame starts with rxhdr */
4564         rxh = (d11rxhdr_t *) (p->data);
4565
4566         /* strip off rxhdr */
4567         skb_pull(p, WL_HWRXOFF);
4568
4569         /* fixup rx header endianness */
4570         rxh->RxFrameSize = le16_to_cpu(rxh->RxFrameSize);
4571         rxh->PhyRxStatus_0 = le16_to_cpu(rxh->PhyRxStatus_0);
4572         rxh->PhyRxStatus_1 = le16_to_cpu(rxh->PhyRxStatus_1);
4573         rxh->PhyRxStatus_2 = le16_to_cpu(rxh->PhyRxStatus_2);
4574         rxh->PhyRxStatus_3 = le16_to_cpu(rxh->PhyRxStatus_3);
4575         rxh->PhyRxStatus_4 = le16_to_cpu(rxh->PhyRxStatus_4);
4576         rxh->PhyRxStatus_5 = le16_to_cpu(rxh->PhyRxStatus_5);
4577         rxh->RxStatus1 = le16_to_cpu(rxh->RxStatus1);
4578         rxh->RxStatus2 = le16_to_cpu(rxh->RxStatus2);
4579         rxh->RxTSFTime = le16_to_cpu(rxh->RxTSFTime);
4580         rxh->RxChan = le16_to_cpu(rxh->RxChan);
4581
4582         /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
4583         if (rxh->RxStatus1 & RXS_PBPRES) {
4584                 if (p->len < 2) {
4585                         wiphy_err(wlc->wiphy, "wl%d: wlc_recv: rcvd runt of "
4586                                   "len %d\n", wlc->pub->unit, p->len);
4587                         goto toss;
4588                 }
4589                 skb_pull(p, 2);
4590         }
4591
4592         h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
4593         len = p->len;
4594
4595         if (rxh->RxStatus1 & RXS_FCSERR) {
4596                 if (wlc->pub->mac80211_state & MAC80211_PROMISC_BCNS) {
4597                         wiphy_err(wlc->wiphy, "FCSERR while scanning******* -"
4598                                   " tossing\n");
4599                         goto toss;
4600                 } else {
4601                         wiphy_err(wlc->wiphy, "RCSERR!!!\n");
4602                         goto toss;
4603                 }
4604         }
4605
4606         /* check received pkt has at least frame control field */
4607         if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control)) {
4608                 goto toss;
4609         }
4610
4611         is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
4612
4613         /* explicitly test bad src address to avoid sending bad deauth */
4614         if (!is_amsdu) {
4615                 /* CTS and ACK CTL frames are w/o a2 */
4616
4617                 if (ieee80211_is_data(h->frame_control) ||
4618                     ieee80211_is_mgmt(h->frame_control)) {
4619                         if ((is_zero_ether_addr(h->addr2) ||
4620                              is_multicast_ether_addr(h->addr2))) {
4621                                 wiphy_err(wlc->wiphy, "wl%d: %s: dropping a "
4622                                           "frame with invalid src mac address,"
4623                                           " a2: %pM\n",
4624                                          wlc->pub->unit, __func__, h->addr2);
4625                                 goto toss;
4626                         }
4627                 }
4628         }
4629
4630         /* due to sheer numbers, toss out probe reqs for now */
4631         if (ieee80211_is_probe_req(h->frame_control))
4632                 goto toss;
4633
4634         if (is_amsdu)
4635                 goto toss;
4636
4637         wlc_recvctl(wlc, rxh, p);
4638         return;
4639
4640  toss:
4641         bcm_pkt_buf_free_skb(p);
4642 }
4643
4644 /* calculate frame duration for Mixed-mode L-SIG spoofing, return
4645  * number of bytes goes in the length field
4646  *
4647  * Formula given by HT PHY Spec v 1.13
4648  *   len = 3(nsyms + nstream + 3) - 3
4649  */
4650 u16
4651 wlc_calc_lsig_len(struct wlc_info *wlc, ratespec_t ratespec, uint mac_len)
4652 {
4653         uint nsyms, len = 0, kNdps;
4654
4655         BCMMSG(wlc->wiphy, "wl%d: rate %d, len%d\n",
4656                  wlc->pub->unit, RSPEC2RATE(ratespec), mac_len);
4657
4658         if (IS_MCS(ratespec)) {
4659                 uint mcs = ratespec & RSPEC_RATE_MASK;
4660                 /* MCS_TXS(mcs) returns num tx streams - 1 */
4661                 int tot_streams = (MCS_TXS(mcs) + 1) + RSPEC_STC(ratespec);
4662
4663                 /* the payload duration calculation matches that of regular ofdm */
4664                 /* 1000Ndbps = kbps * 4 */
4665                 kNdps =
4666                     MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
4667                              RSPEC_ISSGI(ratespec)) * 4;
4668
4669                 if (RSPEC_STC(ratespec) == 0)
4670                         /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
4671                         nsyms =
4672                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
4673                                   APHY_TAIL_NBITS) * 1000, kNdps);
4674                 else
4675                         /* STBC needs to have even number of symbols */
4676                         nsyms =
4677                             2 *
4678                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
4679                                   APHY_TAIL_NBITS) * 1000, 2 * kNdps);
4680
4681                 nsyms += (tot_streams + 3);     /* (+3) account for HT-SIG(2) and HT-STF(1) */
4682                 /* 3 bytes/symbol @ legacy 6Mbps rate */
4683                 len = (3 * nsyms) - 3;  /* (-3) excluding service bits and tail bits */
4684         }
4685
4686         return (u16) len;
4687 }
4688
4689 /* calculate frame duration of a given rate and length, return time in usec unit */
4690 uint
4691 wlc_calc_frame_time(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
4692                     uint mac_len)
4693 {
4694         uint nsyms, dur = 0, Ndps, kNdps;
4695         uint rate = RSPEC2RATE(ratespec);
4696
4697         if (rate == 0) {
4698                 wiphy_err(wlc->wiphy, "wl%d: WAR: using rate of 1 mbps\n",
4699                           wlc->pub->unit);
4700                 rate = WLC_RATE_1M;
4701         }
4702
4703         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, len%d\n",
4704                  wlc->pub->unit, ratespec, preamble_type, mac_len);
4705
4706         if (IS_MCS(ratespec)) {
4707                 uint mcs = ratespec & RSPEC_RATE_MASK;
4708                 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
4709
4710                 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
4711                 if (preamble_type == WLC_MM_PREAMBLE)
4712                         dur += PREN_MM_EXT;
4713                 /* 1000Ndbps = kbps * 4 */
4714                 kNdps =
4715                     MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
4716                              RSPEC_ISSGI(ratespec)) * 4;
4717
4718                 if (RSPEC_STC(ratespec) == 0)
4719                         /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
4720                         nsyms =
4721                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
4722                                   APHY_TAIL_NBITS) * 1000, kNdps);
4723                 else
4724                         /* STBC needs to have even number of symbols */
4725                         nsyms =
4726                             2 *
4727                             CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
4728                                   APHY_TAIL_NBITS) * 1000, 2 * kNdps);
4729
4730                 dur += APHY_SYMBOL_TIME * nsyms;
4731                 if (BAND_2G(wlc->band->bandtype))
4732                         dur += DOT11_OFDM_SIGNAL_EXTENSION;
4733         } else if (IS_OFDM(rate)) {
4734                 dur = APHY_PREAMBLE_TIME;
4735                 dur += APHY_SIGNAL_TIME;
4736                 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
4737                 Ndps = rate * 2;
4738                 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
4739                 nsyms =
4740                     CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
4741                          Ndps);
4742                 dur += APHY_SYMBOL_TIME * nsyms;
4743                 if (BAND_2G(wlc->band->bandtype))
4744                         dur += DOT11_OFDM_SIGNAL_EXTENSION;
4745         } else {
4746                 /* calc # bits * 2 so factor of 2 in rate (1/2 mbps) will divide out */
4747                 mac_len = mac_len * 8 * 2;
4748                 /* calc ceiling of bits/rate = microseconds of air time */
4749                 dur = (mac_len + rate - 1) / rate;
4750                 if (preamble_type & WLC_SHORT_PREAMBLE)
4751                         dur += BPHY_PLCP_SHORT_TIME;
4752                 else
4753                         dur += BPHY_PLCP_TIME;
4754         }
4755         return dur;
4756 }
4757
4758 /* The opposite of wlc_calc_frame_time */
4759 static uint
4760 wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
4761                    uint dur)
4762 {
4763         uint nsyms, mac_len, Ndps, kNdps;
4764         uint rate = RSPEC2RATE(ratespec);
4765
4766         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, dur %d\n",
4767                  wlc->pub->unit, ratespec, preamble_type, dur);
4768
4769         if (IS_MCS(ratespec)) {
4770                 uint mcs = ratespec & RSPEC_RATE_MASK;
4771                 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
4772                 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
4773                 /* payload calculation matches that of regular ofdm */
4774                 if (BAND_2G(wlc->band->bandtype))
4775                         dur -= DOT11_OFDM_SIGNAL_EXTENSION;
4776                 /* kNdbps = kbps * 4 */
4777                 kNdps =
4778                     MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
4779                              RSPEC_ISSGI(ratespec)) * 4;
4780                 nsyms = dur / APHY_SYMBOL_TIME;
4781                 mac_len =
4782                     ((nsyms * kNdps) -
4783                      ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
4784         } else if (IS_OFDM(ratespec)) {
4785                 dur -= APHY_PREAMBLE_TIME;
4786                 dur -= APHY_SIGNAL_TIME;
4787                 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
4788                 Ndps = rate * 2;
4789                 nsyms = dur / APHY_SYMBOL_TIME;
4790                 mac_len =
4791                     ((nsyms * Ndps) -
4792                      (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
4793         } else {
4794                 if (preamble_type & WLC_SHORT_PREAMBLE)
4795                         dur -= BPHY_PLCP_SHORT_TIME;
4796                 else
4797                         dur -= BPHY_PLCP_TIME;
4798                 mac_len = dur * rate;
4799                 /* divide out factor of 2 in rate (1/2 mbps) */
4800                 mac_len = mac_len / 8 / 2;
4801         }
4802         return mac_len;
4803 }
4804
4805 static uint
4806 wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
4807 {
4808         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, "
4809                  "preamble_type %d\n", wlc->pub->unit, rspec, preamble_type);
4810         /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
4811          * or equal to the rate of the immediately previous frame in the FES
4812          */
4813         rspec = WLC_BASIC_RATE(wlc, rspec);
4814         /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
4815         return wlc_calc_frame_time(wlc, rspec, preamble_type,
4816                                    (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
4817                                     FCS_LEN));
4818 }
4819
4820 static uint
4821 wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
4822 {
4823         uint dur = 0;
4824
4825         BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d\n",
4826                 wlc->pub->unit, rspec, preamble_type);
4827         /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
4828          * or equal to the rate of the immediately previous frame in the FES
4829          */
4830         rspec = WLC_BASIC_RATE(wlc, rspec);
4831         /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
4832         dur =
4833             wlc_calc_frame_time(wlc, rspec, preamble_type,
4834                                 (DOT11_ACK_LEN + FCS_LEN));
4835         return dur;
4836 }
4837
4838 static uint
4839 wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
4840 {
4841         BCMMSG(wlc->wiphy, "wl%d: ratespec 0x%x, preamble_type %d\n",
4842                 wlc->pub->unit, rspec, preamble_type);
4843         return wlc_calc_ack_time(wlc, rspec, preamble_type);
4844 }
4845
4846 /* derive wlc->band->basic_rate[] table from 'rateset' */
4847 void wlc_rate_lookup_init(struct wlc_info *wlc, wlc_rateset_t *rateset)
4848 {
4849         u8 rate;
4850         u8 mandatory;
4851         u8 cck_basic = 0;
4852         u8 ofdm_basic = 0;
4853         u8 *br = wlc->band->basic_rate;
4854         uint i;
4855
4856         /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
4857         memset(br, 0, WLC_MAXRATE + 1);
4858
4859         /* For each basic rate in the rates list, make an entry in the
4860          * best basic lookup.
4861          */
4862         for (i = 0; i < rateset->count; i++) {
4863                 /* only make an entry for a basic rate */
4864                 if (!(rateset->rates[i] & WLC_RATE_FLAG))
4865                         continue;
4866
4867                 /* mask off basic bit */
4868                 rate = (rateset->rates[i] & WLC_RATE_MASK);
4869
4870                 if (rate > WLC_MAXRATE) {
4871                         wiphy_err(wlc->wiphy, "wlc_rate_lookup_init: invalid "
4872                                   "rate 0x%X in rate set\n",
4873                                   rateset->rates[i]);
4874                         continue;
4875                 }
4876
4877                 br[rate] = rate;
4878         }
4879
4880         /* The rate lookup table now has non-zero entries for each
4881          * basic rate, equal to the basic rate: br[basicN] = basicN
4882          *
4883          * To look up the best basic rate corresponding to any
4884          * particular rate, code can use the basic_rate table
4885          * like this
4886          *
4887          * basic_rate = wlc->band->basic_rate[tx_rate]
4888          *
4889          * Make sure there is a best basic rate entry for
4890          * every rate by walking up the table from low rates
4891          * to high, filling in holes in the lookup table
4892          */
4893
4894         for (i = 0; i < wlc->band->hw_rateset.count; i++) {
4895                 rate = wlc->band->hw_rateset.rates[i];
4896
4897                 if (br[rate] != 0) {
4898                         /* This rate is a basic rate.
4899                          * Keep track of the best basic rate so far by
4900                          * modulation type.
4901                          */
4902                         if (IS_OFDM(rate))
4903                                 ofdm_basic = rate;
4904                         else
4905                                 cck_basic = rate;
4906
4907                         continue;
4908                 }
4909
4910                 /* This rate is not a basic rate so figure out the
4911                  * best basic rate less than this rate and fill in
4912                  * the hole in the table
4913                  */
4914
4915                 br[rate] = IS_OFDM(rate) ? ofdm_basic : cck_basic;
4916
4917                 if (br[rate] != 0)
4918                         continue;
4919
4920                 if (IS_OFDM(rate)) {
4921                         /* In 11g and 11a, the OFDM mandatory rates are 6, 12, and 24 Mbps */
4922                         if (rate >= WLC_RATE_24M)
4923                                 mandatory = WLC_RATE_24M;
4924                         else if (rate >= WLC_RATE_12M)
4925                                 mandatory = WLC_RATE_12M;
4926                         else
4927                                 mandatory = WLC_RATE_6M;
4928                 } else {
4929                         /* In 11b, all the CCK rates are mandatory 1 - 11 Mbps */
4930                         mandatory = rate;
4931                 }
4932
4933                 br[rate] = mandatory;
4934         }
4935 }
4936
4937 static void wlc_write_rate_shm(struct wlc_info *wlc, u8 rate, u8 basic_rate)
4938 {
4939         u8 phy_rate, index;
4940         u8 basic_phy_rate, basic_index;
4941         u16 dir_table, basic_table;
4942         u16 basic_ptr;
4943
4944         /* Shared memory address for the table we are reading */
4945         dir_table = IS_OFDM(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
4946
4947         /* Shared memory address for the table we are writing */
4948         basic_table = IS_OFDM(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
4949
4950         /*
4951          * for a given rate, the LS-nibble of the PLCP SIGNAL field is
4952          * the index into the rate table.
4953          */
4954         phy_rate = rate_info[rate] & WLC_RATE_MASK;
4955         basic_phy_rate = rate_info[basic_rate] & WLC_RATE_MASK;
4956         index = phy_rate & 0xf;
4957         basic_index = basic_phy_rate & 0xf;
4958
4959         /* Find the SHM pointer to the ACK rate entry by looking in the
4960          * Direct-map Table
4961          */
4962         basic_ptr = wlc_read_shm(wlc, (dir_table + basic_index * 2));
4963
4964         /* Update the SHM BSS-basic-rate-set mapping table with the pointer
4965          * to the correct basic rate for the given incoming rate
4966          */
4967         wlc_write_shm(wlc, (basic_table + index * 2), basic_ptr);
4968 }
4969
4970 static const wlc_rateset_t *wlc_rateset_get_hwrs(struct wlc_info *wlc)
4971 {
4972         const wlc_rateset_t *rs_dflt;
4973
4974         if (WLC_PHY_11N_CAP(wlc->band)) {
4975                 if (BAND_5G(wlc->band->bandtype))
4976                         rs_dflt = &ofdm_mimo_rates;
4977                 else
4978                         rs_dflt = &cck_ofdm_mimo_rates;
4979         } else if (wlc->band->gmode)
4980                 rs_dflt = &cck_ofdm_rates;
4981         else
4982                 rs_dflt = &cck_rates;
4983
4984         return rs_dflt;
4985 }
4986
4987 void wlc_set_ratetable(struct wlc_info *wlc)
4988 {
4989         const wlc_rateset_t *rs_dflt;
4990         wlc_rateset_t rs;
4991         u8 rate, basic_rate;
4992         uint i;
4993
4994         rs_dflt = wlc_rateset_get_hwrs(wlc);
4995
4996         wlc_rateset_copy(rs_dflt, &rs);
4997         wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
4998
4999         /* walk the phy rate table and update SHM basic rate lookup table */
5000         for (i = 0; i < rs.count; i++) {
5001                 rate = rs.rates[i] & WLC_RATE_MASK;
5002
5003                 /* for a given rate WLC_BASIC_RATE returns the rate at
5004                  * which a response ACK/CTS should be sent.
5005                  */
5006                 basic_rate = WLC_BASIC_RATE(wlc, rate);
5007                 if (basic_rate == 0) {
5008                         /* This should only happen if we are using a
5009                          * restricted rateset.
5010                          */
5011                         basic_rate = rs.rates[0] & WLC_RATE_MASK;
5012                 }
5013
5014                 wlc_write_rate_shm(wlc, rate, basic_rate);
5015         }
5016 }
5017
5018 /*
5019  * Return true if the specified rate is supported by the specified band.
5020  * WLC_BAND_AUTO indicates the current band.
5021  */
5022 bool wlc_valid_rate(struct wlc_info *wlc, ratespec_t rspec, int band,
5023                     bool verbose)
5024 {
5025         wlc_rateset_t *hw_rateset;
5026         uint i;
5027
5028         if ((band == WLC_BAND_AUTO) || (band == wlc->band->bandtype)) {
5029                 hw_rateset = &wlc->band->hw_rateset;
5030         } else if (NBANDS(wlc) > 1) {
5031                 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
5032         } else {
5033                 /* other band specified and we are a single band device */
5034                 return false;
5035         }
5036
5037         /* check if this is a mimo rate */
5038         if (IS_MCS(rspec)) {
5039                 if (!VALID_MCS((rspec & RSPEC_RATE_MASK)))
5040                         goto error;
5041
5042                 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
5043         }
5044
5045         for (i = 0; i < hw_rateset->count; i++)
5046                 if (hw_rateset->rates[i] == RSPEC2RATE(rspec))
5047                         return true;
5048  error:
5049         if (verbose) {
5050                 wiphy_err(wlc->wiphy, "wl%d: wlc_valid_rate: rate spec 0x%x "
5051                           "not in hw_rateset\n", wlc->pub->unit, rspec);
5052         }
5053
5054         return false;
5055 }
5056
5057 static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap)
5058 {
5059         uint i;
5060         struct wlcband *band;
5061
5062         for (i = 0; i < NBANDS(wlc); i++) {
5063                 if (IS_SINGLEBAND_5G(wlc->deviceid))
5064                         i = BAND_5G_INDEX;
5065                 band = wlc->bandstate[i];
5066                 if (band->bandtype == WLC_BAND_5G) {
5067                         if ((bwcap == WLC_N_BW_40ALL)
5068                             || (bwcap == WLC_N_BW_20IN2G_40IN5G))
5069                                 band->mimo_cap_40 = true;
5070                         else
5071                                 band->mimo_cap_40 = false;
5072                 } else {
5073                         if (bwcap == WLC_N_BW_40ALL)
5074                                 band->mimo_cap_40 = true;
5075                         else
5076                                 band->mimo_cap_40 = false;
5077                 }
5078         }
5079 }
5080
5081 void wlc_mod_prb_rsp_rate_table(struct wlc_info *wlc, uint frame_len)
5082 {
5083         const wlc_rateset_t *rs_dflt;
5084         wlc_rateset_t rs;
5085         u8 rate;
5086         u16 entry_ptr;
5087         u8 plcp[D11_PHY_HDR_LEN];
5088         u16 dur, sifs;
5089         uint i;
5090
5091         sifs = SIFS(wlc->band);
5092
5093         rs_dflt = wlc_rateset_get_hwrs(wlc);
5094
5095         wlc_rateset_copy(rs_dflt, &rs);
5096         wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
5097
5098         /* walk the phy rate table and update MAC core SHM basic rate table entries */
5099         for (i = 0; i < rs.count; i++) {
5100                 rate = rs.rates[i] & WLC_RATE_MASK;
5101
5102                 entry_ptr = wlc_rate_shm_offset(wlc, rate);
5103
5104                 /* Calculate the Probe Response PLCP for the given rate */
5105                 wlc_compute_plcp(wlc, rate, frame_len, plcp);
5106
5107                 /* Calculate the duration of the Probe Response frame plus SIFS for the MAC */
5108                 dur =
5109                     (u16) wlc_calc_frame_time(wlc, rate, WLC_LONG_PREAMBLE,
5110                                                  frame_len);
5111                 dur += sifs;
5112
5113                 /* Update the SHM Rate Table entry Probe Response values */
5114                 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS,
5115                               (u16) (plcp[0] + (plcp[1] << 8)));
5116                 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS + 2,
5117                               (u16) (plcp[2] + (plcp[3] << 8)));
5118                 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_DUR_POS, dur);
5119         }
5120 }
5121
5122 /*      Max buffering needed for beacon template/prb resp template is 142 bytes.
5123  *
5124  *      PLCP header is 6 bytes.
5125  *      802.11 A3 header is 24 bytes.
5126  *      Max beacon frame body template length is 112 bytes.
5127  *      Max probe resp frame body template length is 110 bytes.
5128  *
5129  *      *len on input contains the max length of the packet available.
5130  *
5131  *      The *len value is set to the number of bytes in buf used, and starts with the PLCP
5132  *      and included up to, but not including, the 4 byte FCS.
5133  */
5134 static void
5135 wlc_bcn_prb_template(struct wlc_info *wlc, u16 type, ratespec_t bcn_rspec,
5136                      struct wlc_bsscfg *cfg, u16 *buf, int *len)
5137 {
5138         static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
5139         cck_phy_hdr_t *plcp;
5140         struct ieee80211_mgmt *h;
5141         int hdr_len, body_len;
5142
5143         if (MBSS_BCN_ENAB(cfg) && type == IEEE80211_STYPE_BEACON)
5144                 hdr_len = DOT11_MAC_HDR_LEN;
5145         else
5146                 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
5147         body_len = *len - hdr_len;      /* calc buffer size provided for frame body */
5148
5149         *len = hdr_len + body_len;      /* return actual size */
5150
5151         /* format PHY and MAC headers */
5152         memset((char *)buf, 0, hdr_len);
5153
5154         plcp = (cck_phy_hdr_t *) buf;
5155
5156         /* PLCP for Probe Response frames are filled in from core's rate table */
5157         if (type == IEEE80211_STYPE_BEACON && !MBSS_BCN_ENAB(cfg)) {
5158                 /* fill in PLCP */
5159                 wlc_compute_plcp(wlc, bcn_rspec,
5160                                  (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
5161                                  (u8 *) plcp);
5162
5163         }
5164         /* "Regular" and 16 MBSS but not for 4 MBSS */
5165         /* Update the phytxctl for the beacon based on the rspec */
5166         if (!SOFTBCN_ENAB(cfg))
5167                 wlc_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
5168
5169         if (MBSS_BCN_ENAB(cfg) && type == IEEE80211_STYPE_BEACON)
5170                 h = (struct ieee80211_mgmt *)&plcp[0];
5171         else
5172                 h = (struct ieee80211_mgmt *)&plcp[1];
5173
5174         /* fill in 802.11 header */
5175         h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
5176
5177         /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
5178         /* A1 filled in by MAC for prb resp, broadcast for bcn */
5179         if (type == IEEE80211_STYPE_BEACON)
5180                 memcpy(&h->da, &ether_bcast, ETH_ALEN);
5181         memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
5182         memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
5183
5184         /* SEQ filled in by MAC */
5185
5186         return;
5187 }
5188
5189 int wlc_get_header_len()
5190 {
5191         return TXOFF;
5192 }
5193
5194 /* Update a beacon for a particular BSS
5195  * For MBSS, this updates the software template and sets "latest" to the index of the
5196  * template updated.
5197  * Otherwise, it updates the hardware template.
5198  */
5199 void wlc_bss_update_beacon(struct wlc_info *wlc, struct wlc_bsscfg *cfg)
5200 {
5201         int len = BCN_TMPL_LEN;
5202
5203         /* Clear the soft intmask */
5204         wlc->defmacintmask &= ~MI_BCNTPL;
5205
5206         if (!cfg->up) {         /* Only allow updates on an UP bss */
5207                 return;
5208         }
5209
5210         /* Optimize:  Some of if/else could be combined */
5211         if (!MBSS_BCN_ENAB(cfg) && HWBCN_ENAB(cfg)) {
5212                 /* Hardware beaconing for this config */
5213                 u16 bcn[BCN_TMPL_LEN / 2];
5214                 u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
5215                 d11regs_t *regs = wlc->regs;
5216
5217                 /* Check if both templates are in use, if so sched. an interrupt
5218                  *      that will call back into this routine
5219                  */
5220                 if ((R_REG(&regs->maccommand) & both_valid) == both_valid) {
5221                         /* clear any previous status */
5222                         W_REG(&regs->macintstatus, MI_BCNTPL);
5223                 }
5224                 /* Check that after scheduling the interrupt both of the
5225                  *      templates are still busy. if not clear the int. & remask
5226                  */
5227                 if ((R_REG(&regs->maccommand) & both_valid) == both_valid) {
5228                         wlc->defmacintmask |= MI_BCNTPL;
5229                         return;
5230                 }
5231
5232                 wlc->bcn_rspec =
5233                     wlc_lowest_basic_rspec(wlc, &cfg->current_bss->rateset);
5234                 /* update the template and ucode shm */
5235                 wlc_bcn_prb_template(wlc, IEEE80211_STYPE_BEACON,
5236                                      wlc->bcn_rspec, cfg, bcn, &len);
5237                 wlc_write_hw_bcntemplates(wlc, bcn, len, false);
5238         }
5239 }
5240
5241 /*
5242  * Update all beacons for the system.
5243  */
5244 void wlc_update_beacon(struct wlc_info *wlc)
5245 {
5246         int idx;
5247         struct wlc_bsscfg *bsscfg;
5248
5249         /* update AP or IBSS beacons */
5250         FOREACH_BSS(wlc, idx, bsscfg) {
5251                 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
5252                         wlc_bss_update_beacon(wlc, bsscfg);
5253         }
5254 }
5255
5256 /* Write ssid into shared memory */
5257 void wlc_shm_ssid_upd(struct wlc_info *wlc, struct wlc_bsscfg *cfg)
5258 {
5259         u8 *ssidptr = cfg->SSID;
5260         u16 base = M_SSID;
5261         u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
5262
5263         /* padding the ssid with zero and copy it into shm */
5264         memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
5265         memcpy(ssidbuf, ssidptr, cfg->SSID_len);
5266
5267         wlc_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
5268
5269         if (!MBSS_BCN_ENAB(cfg))
5270                 wlc_write_shm(wlc, M_SSIDLEN, (u16) cfg->SSID_len);
5271 }
5272
5273 void wlc_update_probe_resp(struct wlc_info *wlc, bool suspend)
5274 {
5275         int idx;
5276         struct wlc_bsscfg *bsscfg;
5277
5278         /* update AP or IBSS probe responses */
5279         FOREACH_BSS(wlc, idx, bsscfg) {
5280                 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
5281                         wlc_bss_update_probe_resp(wlc, bsscfg, suspend);
5282         }
5283 }
5284
5285 void
5286 wlc_bss_update_probe_resp(struct wlc_info *wlc, struct wlc_bsscfg *cfg,
5287                           bool suspend)
5288 {
5289         u16 prb_resp[BCN_TMPL_LEN / 2];
5290         int len = BCN_TMPL_LEN;
5291
5292         /* write the probe response to hardware, or save in the config structure */
5293         if (!MBSS_PRB_ENAB(cfg)) {
5294
5295                 /* create the probe response template */
5296                 wlc_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0, cfg,
5297                                      prb_resp, &len);
5298
5299                 if (suspend)
5300                         wlc_suspend_mac_and_wait(wlc);
5301
5302                 /* write the probe response into the template region */
5303                 wlc_bmac_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
5304                                             (len + 3) & ~3, prb_resp);
5305
5306                 /* write the length of the probe response frame (+PLCP/-FCS) */
5307                 wlc_write_shm(wlc, M_PRB_RESP_FRM_LEN, (u16) len);
5308
5309                 /* write the SSID and SSID length */
5310                 wlc_shm_ssid_upd(wlc, cfg);
5311
5312                 /*
5313                  * Write PLCP headers and durations for probe response frames at all rates.
5314                  * Use the actual frame length covered by the PLCP header for the call to
5315                  * wlc_mod_prb_rsp_rate_table() by subtracting the PLCP len and adding the FCS.
5316                  */
5317                 len += (-D11_PHY_HDR_LEN + FCS_LEN);
5318                 wlc_mod_prb_rsp_rate_table(wlc, (u16) len);
5319
5320                 if (suspend)
5321                         wlc_enable_mac(wlc);
5322         } else {                /* Generating probe resp in sw; update local template */
5323                 /* error: No software probe response support without MBSS */
5324         }
5325 }
5326
5327 /* prepares pdu for transmission. returns BCM error codes */
5328 int wlc_prep_pdu(struct wlc_info *wlc, struct sk_buff *pdu, uint *fifop)
5329 {
5330         uint fifo;
5331         d11txh_t *txh;
5332         struct ieee80211_hdr *h;
5333         struct scb *scb;
5334
5335         txh = (d11txh_t *) (pdu->data);
5336         h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
5337
5338         /* get the pkt queue info. This was put at wlc_sendctl or wlc_send for PDU */
5339         fifo = le16_to_cpu(txh->TxFrameID) & TXFID_QUEUE_MASK;
5340
5341         scb = NULL;
5342
5343         *fifop = fifo;
5344
5345         /* return if insufficient dma resources */
5346         if (TXAVAIL(wlc, fifo) < MAX_DMA_SEGS) {
5347                 /* Mark precedences related to this FIFO, unsendable */
5348                 WLC_TX_FIFO_CLEAR(wlc, fifo);
5349                 return -EBUSY;
5350         }
5351         return 0;
5352 }
5353
5354 /* init tx reported rate mechanism */
5355 void wlc_reprate_init(struct wlc_info *wlc)
5356 {
5357         int i;
5358         struct wlc_bsscfg *bsscfg;
5359
5360         FOREACH_BSS(wlc, i, bsscfg) {
5361                 wlc_bsscfg_reprate_init(bsscfg);
5362         }
5363 }
5364
5365 /* per bsscfg init tx reported rate mechanism */
5366 void wlc_bsscfg_reprate_init(struct wlc_bsscfg *bsscfg)
5367 {
5368         bsscfg->txrspecidx = 0;
5369         memset((char *)bsscfg->txrspec, 0, sizeof(bsscfg->txrspec));
5370 }
5371
5372 void wlc_default_rateset(struct wlc_info *wlc, wlc_rateset_t *rs)
5373 {
5374         wlc_rateset_default(rs, NULL, wlc->band->phytype, wlc->band->bandtype,
5375                             false, WLC_RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
5376                             CHSPEC_WLC_BW(wlc->default_bss->chanspec),
5377                             wlc->stf->txstreams);
5378 }
5379
5380 static void wlc_bss_default_init(struct wlc_info *wlc)
5381 {
5382         chanspec_t chanspec;
5383         struct wlcband *band;
5384         wlc_bss_info_t *bi = wlc->default_bss;
5385
5386         /* init default and target BSS with some sane initial values */
5387         memset((char *)(bi), 0, sizeof(wlc_bss_info_t));
5388         bi->beacon_period = ISSIM_ENAB(wlc->pub->sih) ? BEACON_INTERVAL_DEF_QT :
5389             BEACON_INTERVAL_DEFAULT;
5390         bi->dtim_period = ISSIM_ENAB(wlc->pub->sih) ? DTIM_INTERVAL_DEF_QT :
5391             DTIM_INTERVAL_DEFAULT;
5392
5393         /* fill the default channel as the first valid channel
5394          * starting from the 2G channels
5395          */
5396         chanspec = CH20MHZ_CHSPEC(1);
5397         wlc->home_chanspec = bi->chanspec = chanspec;
5398
5399         /* find the band of our default channel */
5400         band = wlc->band;
5401         if (NBANDS(wlc) > 1 && band->bandunit != CHSPEC_WLCBANDUNIT(chanspec))
5402                 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5403
5404         /* init bss rates to the band specific default rate set */
5405         wlc_rateset_default(&bi->rateset, NULL, band->phytype, band->bandtype,
5406                             false, WLC_RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
5407                             CHSPEC_WLC_BW(chanspec), wlc->stf->txstreams);
5408
5409         if (N_ENAB(wlc->pub))
5410                 bi->flags |= WLC_BSS_HT;
5411 }
5412
5413 static ratespec_t
5414 mac80211_wlc_set_nrate(struct wlc_info *wlc, struct wlcband *cur_band,
5415                        u32 int_val)
5416 {
5417         u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
5418         u8 rate = int_val & NRATE_RATE_MASK;
5419         ratespec_t rspec;
5420         bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
5421         bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
5422         bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
5423                                   == NRATE_OVERRIDE_MCS_ONLY);
5424         int bcmerror = 0;
5425
5426         if (!ismcs) {
5427                 return (ratespec_t) rate;
5428         }
5429
5430         /* validate the combination of rate/mcs/stf is allowed */
5431         if (N_ENAB(wlc->pub) && ismcs) {
5432                 /* mcs only allowed when nmode */
5433                 if (stf > PHY_TXC1_MODE_SDM) {
5434                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid stf\n",
5435                                  WLCWLUNIT(wlc), __func__);
5436                         bcmerror = -EINVAL;
5437                         goto done;
5438                 }
5439
5440                 /* mcs 32 is a special case, DUP mode 40 only */
5441                 if (rate == 32) {
5442                         if (!CHSPEC_IS40(wlc->home_chanspec) ||
5443                             ((stf != PHY_TXC1_MODE_SISO)
5444                              && (stf != PHY_TXC1_MODE_CDD))) {
5445                                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid mcs "
5446                                           "32\n", WLCWLUNIT(wlc), __func__);
5447                                 bcmerror = -EINVAL;
5448                                 goto done;
5449                         }
5450                         /* mcs > 7 must use stf SDM */
5451                 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
5452                         /* mcs > 7 must use stf SDM */
5453                         if (stf != PHY_TXC1_MODE_SDM) {
5454                                 BCMMSG(wlc->wiphy, "wl%d: enabling "
5455                                          "SDM mode for mcs %d\n",
5456                                          WLCWLUNIT(wlc), rate);
5457                                 stf = PHY_TXC1_MODE_SDM;
5458                         }
5459                 } else {
5460                         /* MCS 0-7 may use SISO, CDD, and for phy_rev >= 3 STBC */
5461                         if ((stf > PHY_TXC1_MODE_STBC) ||
5462                             (!WLC_STBC_CAP_PHY(wlc)
5463                              && (stf == PHY_TXC1_MODE_STBC))) {
5464                                 wiphy_err(wlc->wiphy, "wl%d: %s: Invalid STBC"
5465                                           "\n", WLCWLUNIT(wlc), __func__);
5466                                 bcmerror = -EINVAL;
5467                                 goto done;
5468                         }
5469                 }
5470         } else if (IS_OFDM(rate)) {
5471                 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
5472                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid OFDM\n",
5473                                   WLCWLUNIT(wlc), __func__);
5474                         bcmerror = -EINVAL;
5475                         goto done;
5476                 }
5477         } else if (IS_CCK(rate)) {
5478                 if ((cur_band->bandtype != WLC_BAND_2G)
5479                     || (stf != PHY_TXC1_MODE_SISO)) {
5480                         wiphy_err(wlc->wiphy, "wl%d: %s: Invalid CCK\n",
5481                                   WLCWLUNIT(wlc), __func__);
5482                         bcmerror = -EINVAL;
5483                         goto done;
5484                 }
5485         } else {
5486                 wiphy_err(wlc->wiphy, "wl%d: %s: Unknown rate type\n",
5487                           WLCWLUNIT(wlc), __func__);
5488                 bcmerror = -EINVAL;
5489                 goto done;
5490         }
5491         /* make sure multiple antennae are available for non-siso rates */
5492         if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
5493                 wiphy_err(wlc->wiphy, "wl%d: %s: SISO antenna but !SISO "
5494                           "request\n", WLCWLUNIT(wlc), __func__);
5495                 bcmerror = -EINVAL;
5496                 goto done;
5497         }
5498
5499         rspec = rate;
5500         if (ismcs) {
5501                 rspec |= RSPEC_MIMORATE;
5502                 /* For STBC populate the STC field of the ratespec */
5503                 if (stf == PHY_TXC1_MODE_STBC) {
5504                         u8 stc;
5505                         stc = 1;        /* Nss for single stream is always 1 */
5506                         rspec |= (stc << RSPEC_STC_SHIFT);
5507                 }
5508         }
5509
5510         rspec |= (stf << RSPEC_STF_SHIFT);
5511
5512         if (override_mcs_only)
5513                 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
5514
5515         if (issgi)
5516                 rspec |= RSPEC_SHORT_GI;
5517
5518         if ((rate != 0)
5519             && !wlc_valid_rate(wlc, rspec, cur_band->bandtype, true)) {
5520                 return rate;
5521         }
5522
5523         return rspec;
5524 done:
5525         return rate;
5526 }
5527
5528 /* formula:  IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
5529 static int
5530 wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
5531                    bool writeToShm)
5532 {
5533         int idle_busy_ratio_x_16 = 0;
5534         uint offset =
5535             isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
5536             M_TX_IDLE_BUSY_RATIO_X_16_CCK;
5537         if (duty_cycle > 100 || duty_cycle < 0) {
5538                 wiphy_err(wlc->wiphy, "wl%d:  duty cycle value off limit\n",
5539                           wlc->pub->unit);
5540                 return -EINVAL;
5541         }
5542         if (duty_cycle)
5543                 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
5544         /* Only write to shared memory  when wl is up */
5545         if (writeToShm)
5546                 wlc_write_shm(wlc, offset, (u16) idle_busy_ratio_x_16);
5547
5548         if (isOFDM)
5549                 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
5550         else
5551                 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
5552
5553         return 0;
5554 }
5555
5556 /* Read a single u16 from shared memory.
5557  * SHM 'offset' needs to be an even address
5558  */
5559 u16 wlc_read_shm(struct wlc_info *wlc, uint offset)
5560 {
5561         return wlc_bmac_read_shm(wlc->hw, offset);
5562 }
5563
5564 /* Write a single u16 to shared memory.
5565  * SHM 'offset' needs to be an even address
5566  */
5567 void wlc_write_shm(struct wlc_info *wlc, uint offset, u16 v)
5568 {
5569         wlc_bmac_write_shm(wlc->hw, offset, v);
5570 }
5571
5572 /* Copy a buffer to shared memory.
5573  * SHM 'offset' needs to be an even address and
5574  * Buffer length 'len' must be an even number of bytes
5575  */
5576 void wlc_copyto_shm(struct wlc_info *wlc, uint offset, const void *buf, int len)
5577 {
5578         /* offset and len need to be even */
5579         if (len <= 0 || (offset & 1) || (len & 1))
5580                 return;
5581
5582         wlc_bmac_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
5583
5584 }
5585
5586 /* wrapper BMAC functions to for HIGH driver access */
5587 void wlc_mctrl(struct wlc_info *wlc, u32 mask, u32 val)
5588 {
5589         wlc_bmac_mctrl(wlc->hw, mask, val);
5590 }
5591
5592 void wlc_mhf(struct wlc_info *wlc, u8 idx, u16 mask, u16 val, int bands)
5593 {
5594         wlc_bmac_mhf(wlc->hw, idx, mask, val, bands);
5595 }
5596
5597 int wlc_xmtfifo_sz_get(struct wlc_info *wlc, uint fifo, uint *blocks)
5598 {
5599         return wlc_bmac_xmtfifo_sz_get(wlc->hw, fifo, blocks);
5600 }
5601
5602 void wlc_write_template_ram(struct wlc_info *wlc, int offset, int len,
5603                             void *buf)
5604 {
5605         wlc_bmac_write_template_ram(wlc->hw, offset, len, buf);
5606 }
5607
5608 void wlc_write_hw_bcntemplates(struct wlc_info *wlc, void *bcn, int len,
5609                                bool both)
5610 {
5611         wlc_bmac_write_hw_bcntemplates(wlc->hw, bcn, len, both);
5612 }
5613
5614 void
5615 wlc_set_addrmatch(struct wlc_info *wlc, int match_reg_offset,
5616                   const u8 *addr)
5617 {
5618         wlc_bmac_set_addrmatch(wlc->hw, match_reg_offset, addr);
5619         if (match_reg_offset == RCM_BSSID_OFFSET)
5620                 memcpy(wlc->cfg->BSSID, addr, ETH_ALEN);
5621 }
5622
5623 void wlc_pllreq(struct wlc_info *wlc, bool set, mbool req_bit)
5624 {
5625         wlc_bmac_pllreq(wlc->hw, set, req_bit);
5626 }
5627
5628 void wlc_reset_bmac_done(struct wlc_info *wlc)
5629 {
5630 }
5631
5632 /* check for the particular priority flow control bit being set */
5633 bool
5634 wlc_txflowcontrol_prio_isset(struct wlc_info *wlc, struct wlc_txq_info *q,
5635                              int prio)
5636 {
5637         uint prio_mask;
5638
5639         if (prio == ALLPRIO) {
5640                 prio_mask = TXQ_STOP_FOR_PRIOFC_MASK;
5641         } else {
5642                 prio_mask = NBITVAL(prio);
5643         }
5644
5645         return (q->stopped & prio_mask) == prio_mask;
5646 }
5647
5648 /* propagate the flow control to all interfaces using the given tx queue */
5649 void wlc_txflowcontrol(struct wlc_info *wlc, struct wlc_txq_info *qi,
5650                        bool on, int prio)
5651 {
5652         uint prio_bits;
5653         uint cur_bits;
5654
5655         BCMMSG(wlc->wiphy, "flow control kicks in\n");
5656
5657         if (prio == ALLPRIO) {
5658                 prio_bits = TXQ_STOP_FOR_PRIOFC_MASK;
5659         } else {
5660                 prio_bits = NBITVAL(prio);
5661         }
5662
5663         cur_bits = qi->stopped & prio_bits;
5664
5665         /* Check for the case of no change and return early
5666          * Otherwise update the bit and continue
5667          */
5668         if (on) {
5669                 if (cur_bits == prio_bits) {
5670                         return;
5671                 }
5672                 mboolset(qi->stopped, prio_bits);
5673         } else {
5674                 if (cur_bits == 0) {
5675                         return;
5676                 }
5677                 mboolclr(qi->stopped, prio_bits);
5678         }
5679
5680         /* If there is a flow control override we will not change the external
5681          * flow control state.
5682          */
5683         if (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK) {
5684                 return;
5685         }
5686
5687         wlc_txflowcontrol_signal(wlc, qi, on, prio);
5688 }
5689
5690 void
5691 wlc_txflowcontrol_override(struct wlc_info *wlc, struct wlc_txq_info *qi,
5692                            bool on, uint override)
5693 {
5694         uint prev_override;
5695
5696         prev_override = (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK);
5697
5698         /* Update the flow control bits and do an early return if there is
5699          * no change in the external flow control state.
5700          */
5701         if (on) {
5702                 mboolset(qi->stopped, override);
5703                 /* if there was a previous override bit on, then setting this
5704                  * makes no difference.
5705                  */
5706                 if (prev_override) {
5707                         return;
5708                 }
5709
5710                 wlc_txflowcontrol_signal(wlc, qi, ON, ALLPRIO);
5711         } else {
5712                 mboolclr(qi->stopped, override);
5713                 /* clearing an override bit will only make a difference for
5714                  * flow control if it was the only bit set. For any other
5715                  * override setting, just return
5716                  */
5717                 if (prev_override != override) {
5718                         return;
5719                 }
5720
5721                 if (qi->stopped == 0) {
5722                         wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
5723                 } else {
5724                         int prio;
5725
5726                         for (prio = MAXPRIO; prio >= 0; prio--) {
5727                                 if (!mboolisset(qi->stopped, NBITVAL(prio)))
5728                                         wlc_txflowcontrol_signal(wlc, qi, OFF,
5729                                                                  prio);
5730                         }
5731                 }
5732         }
5733 }
5734
5735 static void wlc_txflowcontrol_reset(struct wlc_info *wlc)
5736 {
5737         struct wlc_txq_info *qi;
5738
5739         for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
5740                 if (qi->stopped) {
5741                         wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
5742                         qi->stopped = 0;
5743                 }
5744         }
5745 }
5746
5747 static void
5748 wlc_txflowcontrol_signal(struct wlc_info *wlc, struct wlc_txq_info *qi, bool on,
5749                          int prio)
5750 {
5751 #ifdef NON_FUNCTIONAL
5752         /* wlcif_list is never filled so this function is not functional */
5753         struct wlc_if *wlcif;
5754
5755         for (wlcif = wlc->wlcif_list; wlcif != NULL; wlcif = wlcif->next) {
5756                 if (wlcif->qi == qi && wlcif->flags & WLC_IF_LINKED)
5757                         wl_txflowcontrol(wlc->wl, wlcif->wlif, on, prio);
5758         }
5759 #endif
5760 }
5761
5762 static struct wlc_txq_info *wlc_txq_alloc(struct wlc_info *wlc)
5763 {
5764         struct wlc_txq_info *qi, *p;
5765
5766         qi = kzalloc(sizeof(struct wlc_txq_info), GFP_ATOMIC);
5767         if (qi != NULL) {
5768                 /*
5769                  * Have enough room for control packets along with HI watermark
5770                  * Also, add room to txq for total psq packets if all the SCBs
5771                  * leave PS mode. The watermark for flowcontrol to OS packets
5772                  * will remain the same
5773                  */
5774                 bcm_pktq_init(&qi->q, WLC_PREC_COUNT,
5775                           (2 * wlc->pub->tunables->datahiwat) + PKTQ_LEN_DEFAULT
5776                           + wlc->pub->psq_pkts_total);
5777
5778                 /* add this queue to the the global list */
5779                 p = wlc->tx_queues;
5780                 if (p == NULL) {
5781                         wlc->tx_queues = qi;
5782                 } else {
5783                         while (p->next != NULL)
5784                                 p = p->next;
5785                         p->next = qi;
5786                 }
5787         }
5788         return qi;
5789 }
5790
5791 static void wlc_txq_free(struct wlc_info *wlc, struct wlc_txq_info *qi)
5792 {
5793         struct wlc_txq_info *p;
5794
5795         if (qi == NULL)
5796                 return;
5797
5798         /* remove the queue from the linked list */
5799         p = wlc->tx_queues;
5800         if (p == qi)
5801                 wlc->tx_queues = p->next;
5802         else {
5803                 while (p != NULL && p->next != qi)
5804                         p = p->next;
5805                 if (p != NULL)
5806                         p->next = p->next->next;
5807         }
5808
5809         kfree(qi);
5810 }
5811
5812 /*
5813  * Flag 'scan in progress' to withhold dynamic phy calibration
5814  */
5815 void wlc_scan_start(struct wlc_info *wlc)
5816 {
5817         wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
5818 }
5819
5820 void wlc_scan_stop(struct wlc_info *wlc)
5821 {
5822         wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
5823 }
5824
5825 void wlc_associate_upd(struct wlc_info *wlc, bool state)
5826 {
5827         wlc->pub->associated = state;
5828         wlc->cfg->associated = state;
5829 }
5830
5831 /*
5832  * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
5833  * AMPDU traffic, packets pending in hardware have to be invalidated so that
5834  * when later on hardware releases them, they can be handled appropriately.
5835  */
5836 void wlc_inval_dma_pkts(struct wlc_hw_info *hw,
5837                                struct ieee80211_sta *sta,
5838                                void (*dma_callback_fn))
5839 {
5840         struct hnddma_pub *dmah;
5841         int i;
5842         for (i = 0; i < NFIFO; i++) {
5843                 dmah = hw->di[i];
5844                 if (dmah != NULL)
5845                         dma_walk_packets(dmah, dma_callback_fn, sta);
5846         }
5847 }
5848
5849 int wlc_get_curband(struct wlc_info *wlc)
5850 {
5851         return wlc->band->bandunit;
5852 }
5853
5854 void wlc_wait_for_tx_completion(struct wlc_info *wlc, bool drop)
5855 {
5856         /* flush packet queue when requested */
5857         if (drop)
5858                 bcm_pktq_flush(&wlc->pkt_queue->q, false, NULL, NULL);
5859
5860         /* wait for queue and DMA fifos to run dry */
5861         while (!pktq_empty(&wlc->pkt_queue->q) ||
5862                TXPKTPENDTOT(wlc) > 0) {
5863                 wl_msleep(wlc->wl, 1);
5864         }
5865 }
5866
5867 int wlc_set_par(struct wlc_info *wlc, enum wlc_par_id par_id, int int_val)
5868 {
5869         int err = 0;
5870
5871         switch (par_id) {
5872         case IOV_BCN_LI_BCN:
5873                 wlc->bcn_li_bcn = (u8) int_val;
5874                 if (wlc->pub->up)
5875                         wlc_bcn_li_upd(wlc);
5876                 break;
5877                 /* As long as override is false, this only sets the *user*
5878                    targets. User can twiddle this all he wants with no harm.
5879                    wlc_phy_txpower_set() explicitly sets override to false if
5880                    not internal or test.
5881                  */
5882         case IOV_QTXPOWER:{
5883                 u8 qdbm;
5884                 bool override;
5885
5886                 /* Remove override bit and clip to max qdbm value */
5887                 qdbm = (u8)min_t(u32, (int_val & ~WL_TXPWR_OVERRIDE), 0xff);
5888                 /* Extract override setting */
5889                 override = (int_val & WL_TXPWR_OVERRIDE) ? true : false;
5890                 err =
5891                     wlc_phy_txpower_set(wlc->band->pi, qdbm, override);
5892                 break;
5893                 }
5894         case IOV_MPC:
5895                 wlc->mpc = (bool)int_val;
5896                 wlc_radio_mpc_upd(wlc);
5897                 break;
5898         default:
5899                 err = -ENOTSUPP;
5900         }
5901         return err;
5902 }
5903
5904 int wlc_get_par(struct wlc_info *wlc, enum wlc_par_id par_id, int *ret_int_ptr)
5905 {
5906         int err = 0;
5907
5908         switch (par_id) {
5909         case IOV_BCN_LI_BCN:
5910                 *ret_int_ptr = wlc->bcn_li_bcn;
5911                 break;
5912         case IOV_QTXPOWER: {
5913                 uint qdbm;
5914                 bool override;
5915
5916                 err = wlc_phy_txpower_get(wlc->band->pi, &qdbm,
5917                         &override);
5918                 if (err != 0)
5919                         return err;
5920
5921                 /* Return qdbm units */
5922                 *ret_int_ptr =
5923                     qdbm | (override ? WL_TXPWR_OVERRIDE : 0);
5924                 break;
5925                 }
5926         case IOV_MPC:
5927                 *ret_int_ptr = (s32) wlc->mpc;
5928                 break;
5929         default:
5930                 err = -ENOTSUPP;
5931         }
5932         return err;
5933 }