staging: brcm80211: fix build with BCMDBG unset.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / brcmsmac / wlc_mac80211.c
blobb05a491f3f62106a8f95fb7daad5cda28be2d183
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
16 #include <linux/kernel.h>
17 #include <linux/ctype.h>
18 #include <linux/etherdevice.h>
19 #include <bcmdefs.h>
20 #include <bcmdevs.h>
21 #include <wlc_cfg.h>
22 #include <osl.h>
23 #include <bcmutils.h>
24 #include <bcmwifi.h>
25 #include <siutils.h>
26 #include <bcmendian.h>
27 #include <pcicfg.h>
28 #include <bcmsrom.h>
29 #include <wlioctl.h>
30 #include <sbhndpio.h>
31 #include <sbhnddma.h>
32 #include <hnddma.h>
33 #include <hndpmu.h>
34 #include <d11.h>
35 #include <wlc_rate.h>
36 #include <wlc_pub.h>
37 #include <wlc_key.h>
38 #include <wlc_bsscfg.h>
39 #include <wlc_channel.h>
40 #include <wlc_event.h>
41 #include <wlc_mac80211.h>
42 #include <wlc_bmac.h>
43 #include <wlc_scb.h>
44 #include <wlc_phy_hal.h>
45 #include <wlc_phy_shim.h>
46 #include <wlc_antsel.h>
47 #include <wlc_stf.h>
48 #include <wlc_ampdu.h>
49 #include <wlc_event.h>
50 #include <wl_export.h>
51 #include "d11ucode_ext.h"
52 #include <wlc_alloc.h>
53 #include <net/mac80211.h>
54 #include <wl_dbg.h>
58 * WPA(2) definitions
60 #define RSN_CAP_4_REPLAY_CNTRS 2
61 #define RSN_CAP_16_REPLAY_CNTRS 3
63 #define WPA_CAP_4_REPLAY_CNTRS RSN_CAP_4_REPLAY_CNTRS
64 #define WPA_CAP_16_REPLAY_CNTRS RSN_CAP_16_REPLAY_CNTRS
67 * buffer length needed for wlc_format_ssid
68 * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
70 #define SSID_FMT_BUF_LEN ((4 * IEEE80211_MAX_SSID_LEN) + 1)
72 #define TIMER_INTERVAL_WATCHDOG 1000 /* watchdog timer, in unit of ms */
73 #define TIMER_INTERVAL_RADIOCHK 800 /* radio monitor timer, in unit of ms */
75 #ifndef WLC_MPC_MAX_DELAYCNT
76 #define WLC_MPC_MAX_DELAYCNT 10 /* Max MPC timeout, in unit of watchdog */
77 #endif
78 #define WLC_MPC_MIN_DELAYCNT 1 /* Min MPC timeout, in unit of watchdog */
79 #define WLC_MPC_THRESHOLD 3 /* MPC count threshold level */
81 #define BEACON_INTERVAL_DEFAULT 100 /* beacon interval, in unit of 1024TU */
82 #define DTIM_INTERVAL_DEFAULT 3 /* DTIM interval, in unit of beacon interval */
84 /* Scale down delays to accommodate QT slow speed */
85 #define BEACON_INTERVAL_DEF_QT 20 /* beacon interval, in unit of 1024TU */
86 #define DTIM_INTERVAL_DEF_QT 1 /* DTIM interval, in unit of beacon interval */
88 #define TBTT_ALIGN_LEEWAY_US 100 /* min leeway before first TBTT in us */
91 * driver maintains internal 'tick'(wlc->pub->now) which increments in 1s OS timer(soft
92 * watchdog) it is not a wall clock and won't increment when driver is in "down" state
93 * this low resolution driver tick can be used for maintenance tasks such as phy
94 * calibration and scb update
97 /* watchdog trigger mode: OSL timer or TBTT */
98 #define WLC_WATCHDOG_TBTT(wlc) \
99 (wlc->stas_associated > 0 && wlc->PM != PM_OFF && wlc->pub->align_wd_tbtt)
101 /* To inform the ucode of the last mcast frame posted so that it can clear moredata bit */
102 #define BCMCFID(wlc, fid) wlc_bmac_write_shm((wlc)->hw, M_BCMC_FID, (fid))
104 #define WLC_WAR16165(wlc) (wlc->pub->sih->bustype == PCI_BUS && \
105 (!AP_ENAB(wlc->pub)) && (wlc->war16165))
107 /* debug/trace */
108 uint wl_msg_level =
109 #if defined(BCMDBG)
110 WL_ERROR_VAL;
111 #else
113 #endif /* BCMDBG */
115 /* Find basic rate for a given rate */
116 #define WLC_BASIC_RATE(wlc, rspec) (IS_MCS(rspec) ? \
117 (wlc)->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK].leg_ofdm] : \
118 (wlc)->band->basic_rate[rspec & RSPEC_RATE_MASK])
120 #define FRAMETYPE(r, mimoframe) (IS_MCS(r) ? mimoframe : (IS_CCK(r) ? FT_CCK : FT_OFDM))
122 #define RFDISABLE_DEFAULT 10000000 /* rfdisable delay timer 500 ms, runs of ALP clock */
124 #define WLC_TEMPSENSE_PERIOD 10 /* 10 second timeout */
126 #define SCAN_IN_PROGRESS(x) 0
128 #define EPI_VERSION_NUM 0x054b0b00
130 #ifdef BCMDBG
131 /* pointer to most recently allocated wl/wlc */
132 static struct wlc_info *wlc_info_dbg = (struct wlc_info *) (NULL);
133 #endif
135 /* IOVar table */
137 /* Parameter IDs, for use only internally to wlc -- in the wlc_iovars
138 * table and by the wlc_doiovar() function. No ordering is imposed:
139 * the table is keyed by name, and the function uses a switch.
141 enum {
142 IOV_MPC = 1,
143 IOV_RTSTHRESH,
144 IOV_QTXPOWER,
145 IOV_BCN_LI_BCN, /* Beacon listen interval in # of beacons */
146 IOV_LAST /* In case of a need to check max ID number */
149 const bcm_iovar_t wlc_iovars[] = {
150 {"mpc", IOV_MPC, (0), IOVT_BOOL, 0},
151 {"rtsthresh", IOV_RTSTHRESH, (IOVF_WHL), IOVT_UINT16, 0},
152 {"qtxpower", IOV_QTXPOWER, (IOVF_WHL), IOVT_UINT32, 0},
153 {"bcn_li_bcn", IOV_BCN_LI_BCN, (0), IOVT_UINT8, 0},
154 {NULL, 0, 0, 0, 0}
157 const u8 prio2fifo[NUMPRIO] = {
158 TX_AC_BE_FIFO, /* 0 BE AC_BE Best Effort */
159 TX_AC_BK_FIFO, /* 1 BK AC_BK Background */
160 TX_AC_BK_FIFO, /* 2 -- AC_BK Background */
161 TX_AC_BE_FIFO, /* 3 EE AC_BE Best Effort */
162 TX_AC_VI_FIFO, /* 4 CL AC_VI Video */
163 TX_AC_VI_FIFO, /* 5 VI AC_VI Video */
164 TX_AC_VO_FIFO, /* 6 VO AC_VO Voice */
165 TX_AC_VO_FIFO /* 7 NC AC_VO Voice */
168 /* precedences numbers for wlc queues. These are twice as may levels as
169 * 802.1D priorities.
170 * Odd numbers are used for HI priority traffic at same precedence levels
171 * These constants are used ONLY by wlc_prio2prec_map. Do not use them elsewhere.
173 #define _WLC_PREC_NONE 0 /* None = - */
174 #define _WLC_PREC_BK 2 /* BK - Background */
175 #define _WLC_PREC_BE 4 /* BE - Best-effort */
176 #define _WLC_PREC_EE 6 /* EE - Excellent-effort */
177 #define _WLC_PREC_CL 8 /* CL - Controlled Load */
178 #define _WLC_PREC_VI 10 /* Vi - Video */
179 #define _WLC_PREC_VO 12 /* Vo - Voice */
180 #define _WLC_PREC_NC 14 /* NC - Network Control */
182 /* 802.1D Priority to precedence queue mapping */
183 const u8 wlc_prio2prec_map[] = {
184 _WLC_PREC_BE, /* 0 BE - Best-effort */
185 _WLC_PREC_BK, /* 1 BK - Background */
186 _WLC_PREC_NONE, /* 2 None = - */
187 _WLC_PREC_EE, /* 3 EE - Excellent-effort */
188 _WLC_PREC_CL, /* 4 CL - Controlled Load */
189 _WLC_PREC_VI, /* 5 Vi - Video */
190 _WLC_PREC_VO, /* 6 Vo - Voice */
191 _WLC_PREC_NC, /* 7 NC - Network Control */
194 /* Sanity check for tx_prec_map and fifo synchup
195 * Either there are some packets pending for the fifo, else if fifo is empty then
196 * all the corresponding precmap bits should be set
198 #define WLC_TX_FIFO_CHECK(wlc, fifo) (TXPKTPENDGET((wlc), (fifo)) || \
199 (TXPKTPENDGET((wlc), (fifo)) == 0 && \
200 ((wlc)->tx_prec_map & (wlc)->fifo2prec_map[(fifo)]) == \
201 (wlc)->fifo2prec_map[(fifo)]))
203 /* TX FIFO number to WME/802.1E Access Category */
204 const u8 wme_fifo2ac[] = { AC_BK, AC_BE, AC_VI, AC_VO, AC_BE, AC_BE };
206 /* WME/802.1E Access Category to TX FIFO number */
207 static const u8 wme_ac2fifo[] = { 1, 0, 2, 3 };
209 static bool in_send_q = false;
211 /* Shared memory location index for various AC params */
212 #define wme_shmemacindex(ac) wme_ac2fifo[ac]
214 #ifdef BCMDBG
215 static const char *fifo_names[] = {
216 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
217 const char *aci_names[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
218 #endif
220 static const u8 acbitmap2maxprio[] = {
221 PRIO_8021D_BE, PRIO_8021D_BE, PRIO_8021D_BK, PRIO_8021D_BK,
222 PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI,
223 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO,
224 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO
227 /* currently the best mechanism for determining SIFS is the band in use */
228 #define SIFS(band) ((band)->bandtype == WLC_BAND_5G ? APHY_SIFS_TIME : BPHY_SIFS_TIME);
230 /* value for # replay counters currently supported */
231 #define WLC_REPLAY_CNTRS_VALUE WPA_CAP_16_REPLAY_CNTRS
233 /* local prototypes */
234 static u16 BCMFASTPATH wlc_d11hdrs_mac80211(struct wlc_info *wlc,
235 struct ieee80211_hw *hw,
236 struct sk_buff *p,
237 struct scb *scb, uint frag,
238 uint nfrags, uint queue,
239 uint next_frag_len,
240 wsec_key_t *key,
241 ratespec_t rspec_override);
243 static void wlc_ctrupd_cache(u16 cur_stat, u16 *macstat_snapshot, u32 *macstat);
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 int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg);
252 static int wlc_iovar_rangecheck(struct wlc_info *wlc, u32 val,
253 const bcm_iovar_t *vi);
254 static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc);
256 /* send and receive */
257 static wlc_txq_info_t *wlc_txq_alloc(struct wlc_info *wlc,
258 struct osl_info *osh);
259 static void wlc_txq_free(struct wlc_info *wlc, struct osl_info *osh,
260 wlc_txq_info_t *qi);
261 static void wlc_txflowcontrol_signal(struct wlc_info *wlc, wlc_txq_info_t *qi,
262 bool on, int prio);
263 static void wlc_txflowcontrol_reset(struct wlc_info *wlc);
264 static u16 wlc_compute_airtime(struct wlc_info *wlc, ratespec_t rspec,
265 uint length);
266 static void wlc_compute_cck_plcp(ratespec_t rate, uint length, u8 *plcp);
267 static void wlc_compute_ofdm_plcp(ratespec_t rate, uint length, u8 *plcp);
268 static void wlc_compute_mimo_plcp(ratespec_t rate, uint length, u8 *plcp);
269 static u16 wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate,
270 u8 preamble_type, uint next_frag_len);
271 static void wlc_recvctl(struct wlc_info *wlc, struct osl_info *osh,
272 d11rxhdr_t *rxh, struct sk_buff *p);
273 static uint wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t rate,
274 u8 preamble_type, uint dur);
275 static uint wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rate,
276 u8 preamble_type);
277 static uint wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rate,
278 u8 preamble_type);
279 /* interrupt, up/down, band */
280 static void wlc_setband(struct wlc_info *wlc, uint bandunit);
281 static chanspec_t wlc_init_chanspec(struct wlc_info *wlc);
282 static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec);
283 static void wlc_bsinit(struct wlc_info *wlc);
284 static int wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
285 bool writeToShm);
286 static void wlc_radio_hwdisable_upd(struct wlc_info *wlc);
287 static bool wlc_radio_monitor_start(struct wlc_info *wlc);
288 static void wlc_radio_timer(void *arg);
289 static void wlc_radio_enable(struct wlc_info *wlc);
290 static void wlc_radio_upd(struct wlc_info *wlc);
292 /* scan, association, BSS */
293 static uint wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rate,
294 u8 preamble_type);
295 static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap);
296 static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val);
297 static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val);
298 static void wlc_war16165(struct wlc_info *wlc, bool tx);
300 static void wlc_process_eventq(void *arg);
301 static void wlc_wme_retries_write(struct wlc_info *wlc);
302 static bool wlc_attach_stf_ant_init(struct wlc_info *wlc);
303 static uint wlc_attach_module(struct wlc_info *wlc);
304 static void wlc_detach_module(struct wlc_info *wlc);
305 static void wlc_timers_deinit(struct wlc_info *wlc);
306 static void wlc_down_led_upd(struct wlc_info *wlc);
307 static uint wlc_down_del_timer(struct wlc_info *wlc);
308 static void wlc_ofdm_rateset_war(struct wlc_info *wlc);
309 static int _wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
310 struct wlc_if *wlcif);
312 #if defined(BCMDBG)
313 void wlc_get_rcmta(struct wlc_info *wlc, int idx, u8 *addr)
315 d11regs_t *regs = wlc->regs;
316 u32 v32;
317 struct osl_info *osh;
319 WL_TRACE("wl%d: %s\n", WLCWLUNIT(wlc), __func__);
321 ASSERT(wlc->pub->corerev > 4);
323 osh = wlc->osh;
325 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | (idx * 2)));
326 (void)R_REG(osh, &regs->objaddr);
327 v32 = R_REG(osh, &regs->objdata);
328 addr[0] = (u8) v32;
329 addr[1] = (u8) (v32 >> 8);
330 addr[2] = (u8) (v32 >> 16);
331 addr[3] = (u8) (v32 >> 24);
332 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | ((idx * 2) + 1)));
333 (void)R_REG(osh, &regs->objaddr);
334 v32 = R_REG(osh, (volatile u16 *)&regs->objdata);
335 addr[4] = (u8) v32;
336 addr[5] = (u8) (v32 >> 8);
338 #endif /* defined(BCMDBG) */
340 /* keep the chip awake if needed */
341 bool wlc_stay_awake(struct wlc_info *wlc)
343 return true;
346 /* conditions under which the PM bit should be set in outgoing frames and STAY_AWAKE is meaningful
348 bool wlc_ps_allowed(struct wlc_info *wlc)
350 int idx;
351 wlc_bsscfg_t *cfg;
353 /* disallow PS when one of the following global conditions meets */
354 if (!wlc->pub->associated || !wlc->PMenabled || wlc->PM_override)
355 return false;
357 /* disallow PS when one of these meets when not scanning */
358 if (!wlc->PMblocked) {
359 if (AP_ACTIVE(wlc) || wlc->monitor)
360 return false;
363 FOREACH_AS_STA(wlc, idx, cfg) {
364 /* disallow PS when one of the following bsscfg specific conditions meets */
365 if (!cfg->BSS || !WLC_PORTOPEN(cfg))
366 return false;
368 if (!cfg->dtim_programmed)
369 return false;
372 return true;
375 void wlc_reset(struct wlc_info *wlc)
377 WL_TRACE("wl%d: wlc_reset\n", wlc->pub->unit);
379 wlc->check_for_unaligned_tbtt = false;
381 /* slurp up hw mac counters before core reset */
382 if (WLC_UPDATE_STATS(wlc)) {
383 wlc_statsupd(wlc);
385 /* reset our snapshot of macstat counters */
386 memset((char *)wlc->core->macstat_snapshot, 0,
387 sizeof(macstat_t));
390 wlc_bmac_reset(wlc->hw);
391 wlc_ampdu_reset(wlc->ampdu);
392 wlc->txretried = 0;
396 void wlc_fatal_error(struct wlc_info *wlc)
398 WL_ERROR("wl%d: fatal error, reinitializing\n", wlc->pub->unit);
399 wl_init(wlc->wl);
402 /* Return the channel the driver should initialize during wlc_init.
403 * the channel may have to be changed from the currently configured channel
404 * if other configurations are in conflict (bandlocked, 11n mode disabled,
405 * invalid channel for current country, etc.)
407 static chanspec_t wlc_init_chanspec(struct wlc_info *wlc)
409 chanspec_t chanspec =
410 1 | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE |
411 WL_CHANSPEC_BAND_2G;
413 /* make sure the channel is on the supported band if we are band-restricted */
414 if (wlc->bandlocked || NBANDS(wlc) == 1) {
415 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
417 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
418 return chanspec;
421 struct scb global_scb;
423 static void wlc_init_scb(struct wlc_info *wlc, struct scb *scb)
425 int i;
426 scb->flags = SCB_WMECAP | SCB_HTCAP;
427 for (i = 0; i < NUMPRIO; i++)
428 scb->seqnum[i] = 0;
431 void wlc_init(struct wlc_info *wlc)
433 d11regs_t *regs;
434 chanspec_t chanspec;
435 int i;
436 wlc_bsscfg_t *bsscfg;
437 bool mute = false;
439 WL_TRACE("wl%d: wlc_init\n", wlc->pub->unit);
441 regs = wlc->regs;
443 /* This will happen if a big-hammer was executed. In that case, we want to go back
444 * to the channel that we were on and not new channel
446 if (wlc->pub->associated)
447 chanspec = wlc->home_chanspec;
448 else
449 chanspec = wlc_init_chanspec(wlc);
451 wlc_bmac_init(wlc->hw, chanspec, mute);
453 wlc->seckeys = wlc_bmac_read_shm(wlc->hw, M_SECRXKEYS_PTR) * 2;
454 if (D11REV_GE(wlc->pub->corerev, 15) && (wlc->machwcap & MCAP_TKIPMIC))
455 wlc->tkmickeys =
456 wlc_bmac_read_shm(wlc->hw, M_TKMICKEYS_PTR) * 2;
458 /* update beacon listen interval */
459 wlc_bcn_li_upd(wlc);
460 wlc->bcn_wait_prd =
461 (u8) (wlc_bmac_read_shm(wlc->hw, M_NOSLPZNATDTIM) >> 10);
462 ASSERT(wlc->bcn_wait_prd > 0);
464 /* the world is new again, so is our reported rate */
465 wlc_reprate_init(wlc);
467 /* write ethernet address to core */
468 FOREACH_BSS(wlc, i, bsscfg) {
469 wlc_set_mac(bsscfg);
470 wlc_set_bssid(bsscfg);
473 /* Update tsf_cfprep if associated and up */
474 if (wlc->pub->associated) {
475 FOREACH_BSS(wlc, i, bsscfg) {
476 if (bsscfg->up) {
477 u32 bi;
479 /* get beacon period from bsscfg and convert to uS */
480 bi = bsscfg->current_bss->beacon_period << 10;
481 /* update the tsf_cfprep register */
482 /* since init path would reset to default value */
483 W_REG(wlc->osh, &regs->tsf_cfprep,
484 (bi << CFPREP_CBI_SHIFT));
486 /* Update maccontrol PM related bits */
487 wlc_set_ps_ctrl(wlc);
489 break;
494 wlc_key_hw_init_all(wlc);
496 wlc_bandinit_ordered(wlc, chanspec);
498 wlc_init_scb(wlc, &global_scb);
500 /* init probe response timeout */
501 wlc_write_shm(wlc, M_PRS_MAXTIME, wlc->prb_resp_timeout);
503 /* init max burst txop (framebursting) */
504 wlc_write_shm(wlc, M_MBURST_TXOP,
505 (wlc->
506 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
508 /* initialize maximum allowed duty cycle */
509 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
510 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
512 /* Update some shared memory locations related to max AMPDU size allowed to received */
513 wlc_ampdu_shm_upd(wlc->ampdu);
515 /* band-specific inits */
516 wlc_bsinit(wlc);
518 /* Enable EDCF mode (while the MAC is suspended) */
519 if (EDCF_ENAB(wlc->pub)) {
520 OR_REG(wlc->osh, &regs->ifs_ctl, IFS_USEEDCF);
521 wlc_edcf_setparams(wlc->cfg, false);
524 /* Init precedence maps for empty FIFOs */
525 wlc_tx_prec_map_init(wlc);
527 /* read the ucode version if we have not yet done so */
528 if (wlc->ucode_rev == 0) {
529 wlc->ucode_rev =
530 wlc_read_shm(wlc, M_BOM_REV_MAJOR) << NBITS(u16);
531 wlc->ucode_rev |= wlc_read_shm(wlc, M_BOM_REV_MINOR);
534 /* ..now really unleash hell (allow the MAC out of suspend) */
535 wlc_enable_mac(wlc);
537 /* clear tx flow control */
538 wlc_txflowcontrol_reset(wlc);
540 /* clear tx data fifo suspends */
541 wlc->tx_suspended = false;
543 /* enable the RF Disable Delay timer */
544 if (D11REV_GE(wlc->pub->corerev, 10))
545 W_REG(wlc->osh, &wlc->regs->rfdisabledly, RFDISABLE_DEFAULT);
547 /* initialize mpc delay */
548 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
551 * Initialize WME parameters; if they haven't been set by some other
552 * mechanism (IOVar, etc) then read them from the hardware.
554 if (WLC_WME_RETRY_SHORT_GET(wlc, 0) == 0) { /* Unintialized; read from HW */
555 int ac;
557 ASSERT(wlc->clk);
558 for (ac = 0; ac < AC_COUNT; ac++) {
559 wlc->wme_retries[ac] =
560 wlc_read_shm(wlc, M_AC_TXLMT_ADDR(ac));
565 void wlc_mac_bcn_promisc_change(struct wlc_info *wlc, bool promisc)
567 wlc->bcnmisc_monitor = promisc;
568 wlc_mac_bcn_promisc(wlc);
571 void wlc_mac_bcn_promisc(struct wlc_info *wlc)
573 if ((AP_ENAB(wlc->pub) && (N_ENAB(wlc->pub) || wlc->band->gmode)) ||
574 wlc->bcnmisc_ibss || wlc->bcnmisc_scan || wlc->bcnmisc_monitor)
575 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, MCTL_BCNS_PROMISC);
576 else
577 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, 0);
580 /* set or clear maccontrol bits MCTL_PROMISC and MCTL_KEEPCONTROL */
581 void wlc_mac_promisc(struct wlc_info *wlc)
583 u32 promisc_bits = 0;
585 /* promiscuous mode just sets MCTL_PROMISC
586 * Note: APs get all BSS traffic without the need to set the MCTL_PROMISC bit
587 * since all BSS data traffic is directed at the AP
589 if (PROMISC_ENAB(wlc->pub) && !AP_ENAB(wlc->pub) && !wlc->wet)
590 promisc_bits |= MCTL_PROMISC;
592 /* monitor mode needs both MCTL_PROMISC and MCTL_KEEPCONTROL
593 * Note: monitor mode also needs MCTL_BCNS_PROMISC, but that is
594 * handled in wlc_mac_bcn_promisc()
596 if (MONITOR_ENAB(wlc))
597 promisc_bits |= MCTL_PROMISC | MCTL_KEEPCONTROL;
599 wlc_mctrl(wlc, MCTL_PROMISC | MCTL_KEEPCONTROL, promisc_bits);
602 /* check if hps and wake states of sw and hw are in sync */
603 bool wlc_ps_check(struct wlc_info *wlc)
605 bool res = true;
606 bool hps, wake;
607 bool wake_ok;
609 if (!AP_ACTIVE(wlc)) {
610 volatile u32 tmp;
611 tmp = R_REG(wlc->osh, &wlc->regs->maccontrol);
613 /* If deviceremoved is detected, then don't take any action as this can be called
614 * in any context. Assume that caller will take care of the condition. This is just
615 * to avoid assert
617 if (tmp == 0xffffffff) {
618 WL_ERROR("wl%d: %s: dead chip\n",
619 wlc->pub->unit, __func__);
620 return DEVICEREMOVED(wlc);
623 hps = PS_ALLOWED(wlc);
625 if (hps != ((tmp & MCTL_HPS) != 0)) {
626 int idx;
627 wlc_bsscfg_t *cfg;
628 WL_ERROR("wl%d: hps not sync, sw %d, maccontrol 0x%x\n",
629 wlc->pub->unit, hps, tmp);
630 FOREACH_BSS(wlc, idx, cfg) {
631 if (!BSSCFG_STA(cfg))
632 continue;
635 res = false;
637 /* For a monolithic build the wake check can be exact since it looks at wake
638 * override bits. The MCTL_WAKE bit should match the 'wake' value.
640 wake = STAY_AWAKE(wlc) || wlc->hw->wake_override;
641 wake_ok = (wake == ((tmp & MCTL_WAKE) != 0));
642 if (hps && !wake_ok) {
643 WL_ERROR("wl%d: wake not sync, sw %d maccontrol 0x%x\n",
644 wlc->pub->unit, wake, tmp);
645 res = false;
648 ASSERT(res);
649 return res;
652 /* push sw hps and wake state through hardware */
653 void wlc_set_ps_ctrl(struct wlc_info *wlc)
655 u32 v1, v2;
656 bool hps, wake;
657 bool awake_before;
659 hps = PS_ALLOWED(wlc);
660 wake = hps ? (STAY_AWAKE(wlc)) : true;
662 WL_TRACE("wl%d: wlc_set_ps_ctrl: hps %d wake %d\n",
663 wlc->pub->unit, hps, wake);
665 v1 = R_REG(wlc->osh, &wlc->regs->maccontrol);
666 v2 = 0;
667 if (hps)
668 v2 |= MCTL_HPS;
669 if (wake)
670 v2 |= MCTL_WAKE;
672 wlc_mctrl(wlc, MCTL_WAKE | MCTL_HPS, v2);
674 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
676 if (wake && !awake_before)
677 wlc_bmac_wait_for_wake(wlc->hw);
682 * Write this BSS config's MAC address to core.
683 * Updates RXE match engine.
685 int wlc_set_mac(wlc_bsscfg_t *cfg)
687 int err = 0;
688 struct wlc_info *wlc = cfg->wlc;
690 if (cfg == wlc->cfg) {
691 /* enter the MAC addr into the RXE match registers */
692 wlc_set_addrmatch(wlc, RCM_MAC_OFFSET, cfg->cur_etheraddr);
695 wlc_ampdu_macaddr_upd(wlc);
697 return err;
700 /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
701 * Updates RXE match engine.
703 void wlc_set_bssid(wlc_bsscfg_t *cfg)
705 struct wlc_info *wlc = cfg->wlc;
707 /* if primary config, we need to update BSSID in RXE match registers */
708 if (cfg == wlc->cfg) {
709 wlc_set_addrmatch(wlc, RCM_BSSID_OFFSET, cfg->BSSID);
711 #ifdef SUPPORT_HWKEYS
712 else if (BSSCFG_STA(cfg) && cfg->BSS) {
713 wlc_rcmta_add_bssid(wlc, cfg);
715 #endif
719 * Suspend the the MAC and update the slot timing
720 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
722 void wlc_switch_shortslot(struct wlc_info *wlc, bool shortslot)
724 int idx;
725 wlc_bsscfg_t *cfg;
727 ASSERT(wlc->band->gmode);
729 /* use the override if it is set */
730 if (wlc->shortslot_override != WLC_SHORTSLOT_AUTO)
731 shortslot = (wlc->shortslot_override == WLC_SHORTSLOT_ON);
733 if (wlc->shortslot == shortslot)
734 return;
736 wlc->shortslot = shortslot;
738 /* update the capability based on current shortslot mode */
739 FOREACH_BSS(wlc, idx, cfg) {
740 if (!cfg->associated)
741 continue;
742 cfg->current_bss->capability &=
743 ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
744 if (wlc->shortslot)
745 cfg->current_bss->capability |=
746 WLAN_CAPABILITY_SHORT_SLOT_TIME;
749 wlc_bmac_set_shortslot(wlc->hw, shortslot);
752 static u8 wlc_local_constraint_qdbm(struct wlc_info *wlc)
754 u8 local;
755 s16 local_max;
757 local = WLC_TXPWR_MAX;
758 if (wlc->pub->associated &&
759 (wf_chspec_ctlchan(wlc->chanspec) ==
760 wf_chspec_ctlchan(wlc->home_chanspec))) {
762 /* get the local power constraint if we are on the AP's
763 * channel [802.11h, 7.3.2.13]
765 /* Clamp the value between 0 and WLC_TXPWR_MAX w/o overflowing the target */
766 local_max =
767 (wlc->txpwr_local_max -
768 wlc->txpwr_local_constraint) * WLC_TXPWR_DB_FACTOR;
769 if (local_max > 0 && local_max < WLC_TXPWR_MAX)
770 return (u8) local_max;
771 if (local_max < 0)
772 return 0;
775 return local;
778 /* propagate home chanspec to all bsscfgs in case bsscfg->current_bss->chanspec is referenced */
779 void wlc_set_home_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
781 if (wlc->home_chanspec != chanspec) {
782 int idx;
783 wlc_bsscfg_t *cfg;
785 wlc->home_chanspec = chanspec;
787 FOREACH_BSS(wlc, idx, cfg) {
788 if (!cfg->associated)
789 continue;
790 cfg->target_bss->chanspec = chanspec;
791 cfg->current_bss->chanspec = chanspec;
797 static void wlc_set_phy_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
799 /* Save our copy of the chanspec */
800 wlc->chanspec = chanspec;
802 /* Set the chanspec and power limits for this locale after computing
803 * any 11h local tx power constraints.
805 wlc_channel_set_chanspec(wlc->cmi, chanspec,
806 wlc_local_constraint_qdbm(wlc));
808 if (wlc->stf->ss_algosel_auto)
809 wlc_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
810 chanspec);
812 wlc_stf_ss_update(wlc, wlc->band);
816 void wlc_set_chanspec(struct wlc_info *wlc, chanspec_t chanspec)
818 uint bandunit;
819 bool switchband = false;
820 chanspec_t old_chanspec = wlc->chanspec;
822 if (!wlc_valid_chanspec_db(wlc->cmi, chanspec)) {
823 WL_ERROR("wl%d: %s: Bad channel %d\n",
824 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
825 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
826 return;
829 /* Switch bands if necessary */
830 if (NBANDS(wlc) > 1) {
831 bandunit = CHSPEC_WLCBANDUNIT(chanspec);
832 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
833 switchband = true;
834 if (wlc->bandlocked) {
835 WL_ERROR("wl%d: %s: chspec %d band is locked!\n",
836 wlc->pub->unit, __func__,
837 CHSPEC_CHANNEL(chanspec));
838 return;
840 /* BMAC_NOTE: should the setband call come after the wlc_bmac_chanspec() ?
841 * if the setband updates (wlc_bsinit) use low level calls to inspect and
842 * set state, the state inspected may be from the wrong band, or the
843 * following wlc_bmac_set_chanspec() may undo the work.
845 wlc_setband(wlc, bandunit);
849 ASSERT(N_ENAB(wlc->pub) || !CHSPEC_IS40(chanspec));
851 /* sync up phy/radio chanspec */
852 wlc_set_phy_chanspec(wlc, chanspec);
854 /* init antenna selection */
855 if (CHSPEC_WLC_BW(old_chanspec) != CHSPEC_WLC_BW(chanspec)) {
856 if (WLANTSEL_ENAB(wlc))
857 wlc_antsel_init(wlc->asi);
859 /* Fix the hardware rateset based on bw.
860 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
862 wlc_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
863 wlc->band->
864 mimo_cap_40 ? CHSPEC_WLC_BW(chanspec)
865 : 0);
868 /* update some mac configuration since chanspec changed */
869 wlc_ucode_mac_upd(wlc);
872 #if defined(BCMDBG)
873 static int wlc_get_current_txpwr(struct wlc_info *wlc, void *pwr, uint len)
875 txpwr_limits_t txpwr;
876 tx_power_t power;
877 tx_power_legacy_t *old_power = NULL;
878 int r, c;
879 uint qdbm;
880 bool override;
882 if (len == sizeof(tx_power_legacy_t))
883 old_power = (tx_power_legacy_t *) pwr;
884 else if (len < sizeof(tx_power_t))
885 return BCME_BUFTOOSHORT;
887 memset(&power, 0, sizeof(tx_power_t));
889 power.chanspec = WLC_BAND_PI_RADIO_CHANSPEC;
890 if (wlc->pub->associated)
891 power.local_chanspec = wlc->home_chanspec;
893 /* Return the user target tx power limits for the various rates. Note wlc_phy.c's
894 * public interface only implements getting and setting a single value for all of
895 * rates, so we need to fill the array ourselves.
897 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
898 for (r = 0; r < WL_TX_POWER_RATES; r++) {
899 power.user_limit[r] = (u8) qdbm;
902 power.local_max = wlc->txpwr_local_max * WLC_TXPWR_DB_FACTOR;
903 power.local_constraint =
904 wlc->txpwr_local_constraint * WLC_TXPWR_DB_FACTOR;
906 power.antgain[0] = wlc->bandstate[BAND_2G_INDEX]->antgain;
907 power.antgain[1] = wlc->bandstate[BAND_5G_INDEX]->antgain;
909 wlc_channel_reg_limits(wlc->cmi, power.chanspec, &txpwr);
911 #if WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK
912 #error "WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK"
913 #endif
915 /* CCK tx power limits */
916 for (c = 0, r = WL_TX_POWER_CCK_FIRST; c < WL_TX_POWER_CCK_NUM;
917 c++, r++)
918 power.reg_limit[r] = txpwr.cck[c];
920 #if WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM
921 #error "WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM"
922 #endif
924 /* 20 MHz OFDM SISO tx power limits */
925 for (c = 0, r = WL_TX_POWER_OFDM_FIRST; c < WL_TX_POWER_OFDM_NUM;
926 c++, r++)
927 power.reg_limit[r] = txpwr.ofdm[c];
929 if (WLC_PHY_11N_CAP(wlc->band)) {
931 /* 20 MHz OFDM CDD tx power limits */
932 for (c = 0, r = WL_TX_POWER_OFDM20_CDD_FIRST;
933 c < WL_TX_POWER_OFDM_NUM; c++, r++)
934 power.reg_limit[r] = txpwr.ofdm_cdd[c];
936 /* 40 MHz OFDM SISO tx power limits */
937 for (c = 0, r = WL_TX_POWER_OFDM40_SISO_FIRST;
938 c < WL_TX_POWER_OFDM_NUM; c++, r++)
939 power.reg_limit[r] = txpwr.ofdm_40_siso[c];
941 /* 40 MHz OFDM CDD tx power limits */
942 for (c = 0, r = WL_TX_POWER_OFDM40_CDD_FIRST;
943 c < WL_TX_POWER_OFDM_NUM; c++, r++)
944 power.reg_limit[r] = txpwr.ofdm_40_cdd[c];
946 #if WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM
947 #error "WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM"
948 #endif
950 /* 20MHz MCS0-7 SISO tx power limits */
951 for (c = 0, r = WL_TX_POWER_MCS20_SISO_FIRST;
952 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
953 power.reg_limit[r] = txpwr.mcs_20_siso[c];
955 /* 20MHz MCS0-7 CDD tx power limits */
956 for (c = 0, r = WL_TX_POWER_MCS20_CDD_FIRST;
957 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
958 power.reg_limit[r] = txpwr.mcs_20_cdd[c];
960 /* 20MHz MCS0-7 STBC tx power limits */
961 for (c = 0, r = WL_TX_POWER_MCS20_STBC_FIRST;
962 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
963 power.reg_limit[r] = txpwr.mcs_20_stbc[c];
965 /* 40MHz MCS0-7 SISO tx power limits */
966 for (c = 0, r = WL_TX_POWER_MCS40_SISO_FIRST;
967 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
968 power.reg_limit[r] = txpwr.mcs_40_siso[c];
970 /* 40MHz MCS0-7 CDD tx power limits */
971 for (c = 0, r = WL_TX_POWER_MCS40_CDD_FIRST;
972 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
973 power.reg_limit[r] = txpwr.mcs_40_cdd[c];
975 /* 40MHz MCS0-7 STBC tx power limits */
976 for (c = 0, r = WL_TX_POWER_MCS40_STBC_FIRST;
977 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
978 power.reg_limit[r] = txpwr.mcs_40_stbc[c];
980 #if WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM
981 #error "WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM"
982 #endif
984 /* 20MHz MCS8-15 SDM tx power limits */
985 for (c = 0, r = WL_TX_POWER_MCS20_SDM_FIRST;
986 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
987 power.reg_limit[r] = txpwr.mcs_20_mimo[c];
989 /* 40MHz MCS8-15 SDM tx power limits */
990 for (c = 0, r = WL_TX_POWER_MCS40_SDM_FIRST;
991 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
992 power.reg_limit[r] = txpwr.mcs_40_mimo[c];
994 /* MCS 32 */
995 power.reg_limit[WL_TX_POWER_MCS_32] = txpwr.mcs32;
998 wlc_phy_txpower_get_current(wlc->band->pi, &power,
999 CHSPEC_CHANNEL(power.chanspec));
1001 /* copy the tx_power_t struct to the return buffer,
1002 * or convert to a tx_power_legacy_t struct
1004 if (!old_power) {
1005 bcopy(&power, pwr, sizeof(tx_power_t));
1006 } else {
1007 int band_idx = CHSPEC_IS2G(power.chanspec) ? 0 : 1;
1009 memset(old_power, 0, sizeof(tx_power_legacy_t));
1011 old_power->txpwr_local_max = power.local_max;
1012 old_power->txpwr_local_constraint = power.local_constraint;
1013 if (CHSPEC_IS2G(power.chanspec)) {
1014 old_power->txpwr_chan_reg_max = txpwr.cck[0];
1015 old_power->txpwr_est_Pout[band_idx] =
1016 power.est_Pout_cck;
1017 old_power->txpwr_est_Pout_gofdm = power.est_Pout[0];
1018 } else {
1019 old_power->txpwr_chan_reg_max = txpwr.ofdm[0];
1020 old_power->txpwr_est_Pout[band_idx] = power.est_Pout[0];
1022 old_power->txpwr_antgain[0] = power.antgain[0];
1023 old_power->txpwr_antgain[1] = power.antgain[1];
1025 for (r = 0; r < NUM_PWRCTRL_RATES; r++) {
1026 old_power->txpwr_band_max[r] = power.user_limit[r];
1027 old_power->txpwr_limit[r] = power.reg_limit[r];
1028 old_power->txpwr_target[band_idx][r] = power.target[r];
1029 if (CHSPEC_IS2G(power.chanspec))
1030 old_power->txpwr_bphy_cck_max[r] =
1031 power.board_limit[r];
1032 else
1033 old_power->txpwr_aphy_max[r] =
1034 power.board_limit[r];
1038 return 0;
1040 #endif /* defined(BCMDBG) */
1042 static u32 wlc_watchdog_backup_bi(struct wlc_info *wlc)
1044 u32 bi;
1045 bi = 2 * wlc->cfg->current_bss->dtim_period *
1046 wlc->cfg->current_bss->beacon_period;
1047 if (wlc->bcn_li_dtim)
1048 bi *= wlc->bcn_li_dtim;
1049 else if (wlc->bcn_li_bcn)
1050 /* recalculate bi based on bcn_li_bcn */
1051 bi = 2 * wlc->bcn_li_bcn * wlc->cfg->current_bss->beacon_period;
1053 if (bi < 2 * TIMER_INTERVAL_WATCHDOG)
1054 bi = 2 * TIMER_INTERVAL_WATCHDOG;
1055 return bi;
1058 /* Change to run the watchdog either from a periodic timer or from tbtt handler.
1059 * Call watchdog from tbtt handler if tbtt is true, watchdog timer otherwise.
1061 void wlc_watchdog_upd(struct wlc_info *wlc, bool tbtt)
1063 /* make sure changing watchdog driver is allowed */
1064 if (!wlc->pub->up || !wlc->pub->align_wd_tbtt)
1065 return;
1066 if (!tbtt && wlc->WDarmed) {
1067 wl_del_timer(wlc->wl, wlc->wdtimer);
1068 wlc->WDarmed = false;
1071 /* stop watchdog timer and use tbtt interrupt to drive watchdog */
1072 if (tbtt && wlc->WDarmed) {
1073 wl_del_timer(wlc->wl, wlc->wdtimer);
1074 wlc->WDarmed = false;
1075 wlc->WDlast = OSL_SYSUPTIME();
1077 /* arm watchdog timer and drive the watchdog there */
1078 else if (!tbtt && !wlc->WDarmed) {
1079 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
1080 true);
1081 wlc->WDarmed = true;
1083 if (tbtt && !wlc->WDarmed) {
1084 wl_add_timer(wlc->wl, wlc->wdtimer, wlc_watchdog_backup_bi(wlc),
1085 true);
1086 wlc->WDarmed = true;
1090 ratespec_t wlc_lowest_basic_rspec(struct wlc_info *wlc, wlc_rateset_t *rs)
1092 ratespec_t lowest_basic_rspec;
1093 uint i;
1095 /* Use the lowest basic rate */
1096 lowest_basic_rspec = rs->rates[0] & RATE_MASK;
1097 for (i = 0; i < rs->count; i++) {
1098 if (rs->rates[i] & WLC_RATE_FLAG) {
1099 lowest_basic_rspec = rs->rates[i] & RATE_MASK;
1100 break;
1103 #if NCONF
1104 /* pick siso/cdd as default for OFDM (note no basic rate MCSs are supported yet) */
1105 if (IS_OFDM(lowest_basic_rspec)) {
1106 lowest_basic_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
1108 #endif
1110 return lowest_basic_rspec;
1113 /* This function changes the phytxctl for beacon based on current beacon ratespec AND txant
1114 * setting as per this table:
1115 * ratespec CCK ant = wlc->stf->txant
1116 * OFDM ant = 3
1118 void wlc_beacon_phytxctl_txant_upd(struct wlc_info *wlc, ratespec_t bcn_rspec)
1120 u16 phyctl;
1121 u16 phytxant = wlc->stf->phytxant;
1122 u16 mask = PHY_TXC_ANT_MASK;
1124 /* for non-siso rates or default setting, use the available chains */
1125 if (WLC_PHY_11N_CAP(wlc->band)) {
1126 phytxant = wlc_stf_phytxchain_sel(wlc, bcn_rspec);
1129 phyctl = wlc_read_shm(wlc, M_BCN_PCTLWD);
1130 phyctl = (phyctl & ~mask) | phytxant;
1131 wlc_write_shm(wlc, M_BCN_PCTLWD, phyctl);
1134 /* centralized protection config change function to simplify debugging, no consistency checking
1135 * this should be called only on changes to avoid overhead in periodic function
1137 void wlc_protection_upd(struct wlc_info *wlc, uint idx, int val)
1139 WL_TRACE("wlc_protection_upd: idx %d, val %d\n", idx, val);
1141 switch (idx) {
1142 case WLC_PROT_G_SPEC:
1143 wlc->protection->_g = (bool) val;
1144 break;
1145 case WLC_PROT_G_OVR:
1146 wlc->protection->g_override = (s8) val;
1147 break;
1148 case WLC_PROT_G_USER:
1149 wlc->protection->gmode_user = (u8) val;
1150 break;
1151 case WLC_PROT_OVERLAP:
1152 wlc->protection->overlap = (s8) val;
1153 break;
1154 case WLC_PROT_N_USER:
1155 wlc->protection->nmode_user = (s8) val;
1156 break;
1157 case WLC_PROT_N_CFG:
1158 wlc->protection->n_cfg = (s8) val;
1159 break;
1160 case WLC_PROT_N_CFG_OVR:
1161 wlc->protection->n_cfg_override = (s8) val;
1162 break;
1163 case WLC_PROT_N_NONGF:
1164 wlc->protection->nongf = (bool) val;
1165 break;
1166 case WLC_PROT_N_NONGF_OVR:
1167 wlc->protection->nongf_override = (s8) val;
1168 break;
1169 case WLC_PROT_N_PAM_OVR:
1170 wlc->protection->n_pam_override = (s8) val;
1171 break;
1172 case WLC_PROT_N_OBSS:
1173 wlc->protection->n_obss = (bool) val;
1174 break;
1176 default:
1177 ASSERT(0);
1178 break;
1183 static void wlc_ht_update_sgi_rx(struct wlc_info *wlc, int val)
1185 wlc->ht_cap.cap_info &= ~(IEEE80211_HT_CAP_SGI_20 |
1186 IEEE80211_HT_CAP_SGI_40);
1187 wlc->ht_cap.cap_info |= (val & WLC_N_SGI_20) ?
1188 IEEE80211_HT_CAP_SGI_20 : 0;
1189 wlc->ht_cap.cap_info |= (val & WLC_N_SGI_40) ?
1190 IEEE80211_HT_CAP_SGI_40 : 0;
1192 if (wlc->pub->up) {
1193 wlc_update_beacon(wlc);
1194 wlc_update_probe_resp(wlc, true);
1198 static void wlc_ht_update_ldpc(struct wlc_info *wlc, s8 val)
1200 wlc->stf->ldpc = val;
1202 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_LDPC_CODING;
1203 if (wlc->stf->ldpc != OFF)
1204 wlc->ht_cap.cap_info |= IEEE80211_HT_CAP_LDPC_CODING;
1206 if (wlc->pub->up) {
1207 wlc_update_beacon(wlc);
1208 wlc_update_probe_resp(wlc, true);
1209 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
1214 * ucode, hwmac update
1215 * Channel dependent updates for ucode and hw
1217 static void wlc_ucode_mac_upd(struct wlc_info *wlc)
1219 /* enable or disable any active IBSSs depending on whether or not
1220 * we are on the home channel
1222 if (wlc->home_chanspec == WLC_BAND_PI_RADIO_CHANSPEC) {
1223 if (wlc->pub->associated) {
1224 /* BMAC_NOTE: This is something that should be fixed in ucode inits.
1225 * I think that the ucode inits set up the bcn templates and shm values
1226 * with a bogus beacon. This should not be done in the inits. If ucode needs
1227 * to set up a beacon for testing, the test routines should write it down,
1228 * not expect the inits to populate a bogus beacon.
1230 if (WLC_PHY_11N_CAP(wlc->band)) {
1231 wlc_write_shm(wlc, M_BCN_TXTSF_OFFSET,
1232 wlc->band->bcntsfoff);
1235 } else {
1236 /* disable an active IBSS if we are not on the home channel */
1239 /* update the various promisc bits */
1240 wlc_mac_bcn_promisc(wlc);
1241 wlc_mac_promisc(wlc);
1244 static void wlc_bandinit_ordered(struct wlc_info *wlc, chanspec_t chanspec)
1246 wlc_rateset_t default_rateset;
1247 uint parkband;
1248 uint i, band_order[2];
1250 WL_TRACE("wl%d: wlc_bandinit_ordered\n", wlc->pub->unit);
1252 * We might have been bandlocked during down and the chip power-cycled (hibernate).
1253 * figure out the right band to park on
1255 if (wlc->bandlocked || NBANDS(wlc) == 1) {
1256 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
1258 parkband = wlc->band->bandunit; /* updated in wlc_bandlock() */
1259 band_order[0] = band_order[1] = parkband;
1260 } else {
1261 /* park on the band of the specified chanspec */
1262 parkband = CHSPEC_WLCBANDUNIT(chanspec);
1264 /* order so that parkband initialize last */
1265 band_order[0] = parkband ^ 1;
1266 band_order[1] = parkband;
1269 /* make each band operational, software state init */
1270 for (i = 0; i < NBANDS(wlc); i++) {
1271 uint j = band_order[i];
1273 wlc->band = wlc->bandstate[j];
1275 wlc_default_rateset(wlc, &default_rateset);
1277 /* fill in hw_rate */
1278 wlc_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
1279 false, WLC_RATES_CCK_OFDM, RATE_MASK,
1280 (bool) N_ENAB(wlc->pub));
1282 /* init basic rate lookup */
1283 wlc_rate_lookup_init(wlc, &default_rateset);
1286 /* sync up phy/radio chanspec */
1287 wlc_set_phy_chanspec(wlc, chanspec);
1290 /* band-specific init */
1291 static void WLBANDINITFN(wlc_bsinit) (struct wlc_info *wlc)
1293 WL_TRACE("wl%d: wlc_bsinit: bandunit %d\n",
1294 wlc->pub->unit, wlc->band->bandunit);
1296 /* write ucode ACK/CTS rate table */
1297 wlc_set_ratetable(wlc);
1299 /* update some band specific mac configuration */
1300 wlc_ucode_mac_upd(wlc);
1302 /* init antenna selection */
1303 if (WLANTSEL_ENAB(wlc))
1304 wlc_antsel_init(wlc->asi);
1308 /* switch to and initialize new band */
1309 static void WLBANDINITFN(wlc_setband) (struct wlc_info *wlc, uint bandunit)
1311 int idx;
1312 wlc_bsscfg_t *cfg;
1314 ASSERT(NBANDS(wlc) > 1);
1315 ASSERT(!wlc->bandlocked);
1316 ASSERT(bandunit != wlc->band->bandunit || wlc->bandinit_pending);
1318 wlc->band = wlc->bandstate[bandunit];
1320 if (!wlc->pub->up)
1321 return;
1323 /* wait for at least one beacon before entering sleeping state */
1324 wlc->PMawakebcn = true;
1325 FOREACH_AS_STA(wlc, idx, cfg)
1326 cfg->PMawakebcn = true;
1327 wlc_set_ps_ctrl(wlc);
1329 /* band-specific initializations */
1330 wlc_bsinit(wlc);
1333 /* Initialize a WME Parameter Info Element with default STA parameters from WMM Spec, Table 12 */
1334 void wlc_wme_initparams_sta(struct wlc_info *wlc, wme_param_ie_t *pe)
1336 static const wme_param_ie_t stadef = {
1337 WME_OUI,
1338 WME_TYPE,
1339 WME_SUBTYPE_PARAM_IE,
1340 WME_VER,
1344 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA,
1345 HTOL16(EDCF_AC_BE_TXOP_STA)},
1346 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA,
1347 HTOL16(EDCF_AC_BK_TXOP_STA)},
1348 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA,
1349 HTOL16(EDCF_AC_VI_TXOP_STA)},
1350 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA,
1351 HTOL16(EDCF_AC_VO_TXOP_STA)}
1355 ASSERT(sizeof(*pe) == WME_PARAM_IE_LEN);
1356 memcpy(pe, &stadef, sizeof(*pe));
1359 void wlc_wme_setparams(struct wlc_info *wlc, u16 aci, void *arg, bool suspend)
1361 int i;
1362 shm_acparams_t acp_shm;
1363 u16 *shm_entry;
1364 struct ieee80211_tx_queue_params *params = arg;
1366 ASSERT(wlc);
1368 /* Only apply params if the core is out of reset and has clocks */
1369 if (!wlc->clk) {
1370 WL_ERROR("wl%d: %s : no-clock\n", wlc->pub->unit, __func__);
1371 return;
1375 * AP uses AC params from wme_param_ie_ap.
1376 * AP advertises AC params from wme_param_ie.
1377 * STA uses AC params from wme_param_ie.
1380 wlc->wme_admctl = 0;
1382 do {
1383 memset((char *)&acp_shm, 0, sizeof(shm_acparams_t));
1384 /* find out which ac this set of params applies to */
1385 ASSERT(aci < AC_COUNT);
1386 /* set the admission control policy for this AC */
1387 /* wlc->wme_admctl |= 1 << aci; *//* should be set ?? seems like off by default */
1389 /* fill in shm ac params struct */
1390 acp_shm.txop = ltoh16(params->txop);
1391 /* convert from units of 32us to us for ucode */
1392 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
1393 EDCF_TXOP2USEC(acp_shm.txop);
1394 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
1396 if (aci == AC_VI && acp_shm.txop == 0
1397 && acp_shm.aifs < EDCF_AIFSN_MAX)
1398 acp_shm.aifs++;
1400 if (acp_shm.aifs < EDCF_AIFSN_MIN
1401 || acp_shm.aifs > EDCF_AIFSN_MAX) {
1402 WL_ERROR("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1403 wlc->pub->unit, acp_shm.aifs);
1404 continue;
1407 acp_shm.cwmin = params->cw_min;
1408 acp_shm.cwmax = params->cw_max;
1409 acp_shm.cwcur = acp_shm.cwmin;
1410 acp_shm.bslots =
1411 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1412 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1413 /* Indicate the new params to the ucode */
1414 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1415 wme_shmemacindex(aci) *
1416 M_EDCF_QLEN +
1417 M_EDCF_STATUS_OFF));
1418 acp_shm.status |= WME_STATUS_NEWAC;
1420 /* Fill in shm acparam table */
1421 shm_entry = (u16 *) &acp_shm;
1422 for (i = 0; i < (int)sizeof(shm_acparams_t); i += 2)
1423 wlc_write_shm(wlc,
1424 M_EDCF_QINFO +
1425 wme_shmemacindex(aci) * M_EDCF_QLEN + i,
1426 *shm_entry++);
1428 } while (0);
1430 if (suspend)
1431 wlc_suspend_mac_and_wait(wlc);
1433 if (suspend)
1434 wlc_enable_mac(wlc);
1438 void wlc_edcf_setparams(wlc_bsscfg_t *cfg, bool suspend)
1440 struct wlc_info *wlc = cfg->wlc;
1441 uint aci, i, j;
1442 edcf_acparam_t *edcf_acp;
1443 shm_acparams_t acp_shm;
1444 u16 *shm_entry;
1446 ASSERT(cfg);
1447 ASSERT(wlc);
1449 /* Only apply params if the core is out of reset and has clocks */
1450 if (!wlc->clk)
1451 return;
1454 * AP uses AC params from wme_param_ie_ap.
1455 * AP advertises AC params from wme_param_ie.
1456 * STA uses AC params from wme_param_ie.
1459 edcf_acp = (edcf_acparam_t *) &wlc->wme_param_ie.acparam[0];
1461 wlc->wme_admctl = 0;
1463 for (i = 0; i < AC_COUNT; i++, edcf_acp++) {
1464 memset((char *)&acp_shm, 0, sizeof(shm_acparams_t));
1465 /* find out which ac this set of params applies to */
1466 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
1467 ASSERT(aci < AC_COUNT);
1468 /* set the admission control policy for this AC */
1469 if (edcf_acp->ACI & EDCF_ACM_MASK) {
1470 wlc->wme_admctl |= 1 << aci;
1473 /* fill in shm ac params struct */
1474 acp_shm.txop = ltoh16(edcf_acp->TXOP);
1475 /* convert from units of 32us to us for ucode */
1476 wlc->edcf_txop[aci] = acp_shm.txop =
1477 EDCF_TXOP2USEC(acp_shm.txop);
1478 acp_shm.aifs = (edcf_acp->ACI & EDCF_AIFSN_MASK);
1480 if (aci == AC_VI && acp_shm.txop == 0
1481 && acp_shm.aifs < EDCF_AIFSN_MAX)
1482 acp_shm.aifs++;
1484 if (acp_shm.aifs < EDCF_AIFSN_MIN
1485 || acp_shm.aifs > EDCF_AIFSN_MAX) {
1486 WL_ERROR("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1487 wlc->pub->unit, acp_shm.aifs);
1488 continue;
1491 /* CWmin = 2^(ECWmin) - 1 */
1492 acp_shm.cwmin = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
1493 /* CWmax = 2^(ECWmax) - 1 */
1494 acp_shm.cwmax = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
1495 >> EDCF_ECWMAX_SHIFT);
1496 acp_shm.cwcur = acp_shm.cwmin;
1497 acp_shm.bslots =
1498 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1499 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1500 /* Indicate the new params to the ucode */
1501 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1502 wme_shmemacindex(aci) *
1503 M_EDCF_QLEN +
1504 M_EDCF_STATUS_OFF));
1505 acp_shm.status |= WME_STATUS_NEWAC;
1507 /* Fill in shm acparam table */
1508 shm_entry = (u16 *) &acp_shm;
1509 for (j = 0; j < (int)sizeof(shm_acparams_t); j += 2)
1510 wlc_write_shm(wlc,
1511 M_EDCF_QINFO +
1512 wme_shmemacindex(aci) * M_EDCF_QLEN + j,
1513 *shm_entry++);
1516 if (suspend)
1517 wlc_suspend_mac_and_wait(wlc);
1519 if (AP_ENAB(wlc->pub) && WME_ENAB(wlc->pub)) {
1520 wlc_update_beacon(wlc);
1521 wlc_update_probe_resp(wlc, false);
1524 if (suspend)
1525 wlc_enable_mac(wlc);
1529 bool wlc_timers_init(struct wlc_info *wlc, int unit)
1531 wlc->wdtimer = wl_init_timer(wlc->wl, wlc_watchdog_by_timer,
1532 wlc, "watchdog");
1533 if (!wlc->wdtimer) {
1534 WL_ERROR("wl%d: wl_init_timer for wdtimer failed\n", unit);
1535 goto fail;
1538 wlc->radio_timer = wl_init_timer(wlc->wl, wlc_radio_timer,
1539 wlc, "radio");
1540 if (!wlc->radio_timer) {
1541 WL_ERROR("wl%d: wl_init_timer for radio_timer failed\n", unit);
1542 goto fail;
1545 return true;
1547 fail:
1548 return false;
1552 * Initialize wlc_info default values ...
1553 * may get overrides later in this function
1555 void wlc_info_init(struct wlc_info *wlc, int unit)
1557 int i;
1558 /* Assume the device is there until proven otherwise */
1559 wlc->device_present = true;
1561 /* set default power output percentage to 100 percent */
1562 wlc->txpwr_percent = 100;
1564 /* Save our copy of the chanspec */
1565 wlc->chanspec = CH20MHZ_CHSPEC(1);
1567 /* initialize CCK preamble mode to unassociated state */
1568 wlc->shortpreamble = false;
1570 wlc->legacy_probe = true;
1572 /* various 802.11g modes */
1573 wlc->shortslot = false;
1574 wlc->shortslot_override = WLC_SHORTSLOT_AUTO;
1576 wlc->barker_overlap_control = true;
1577 wlc->barker_preamble = WLC_BARKER_SHORT_ALLOWED;
1578 wlc->txburst_limit_override = AUTO;
1580 wlc_protection_upd(wlc, WLC_PROT_G_OVR, WLC_PROTECTION_AUTO);
1581 wlc_protection_upd(wlc, WLC_PROT_G_SPEC, false);
1583 wlc_protection_upd(wlc, WLC_PROT_N_CFG_OVR, WLC_PROTECTION_AUTO);
1584 wlc_protection_upd(wlc, WLC_PROT_N_CFG, WLC_N_PROTECTION_OFF);
1585 wlc_protection_upd(wlc, WLC_PROT_N_NONGF_OVR, WLC_PROTECTION_AUTO);
1586 wlc_protection_upd(wlc, WLC_PROT_N_NONGF, false);
1587 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR, AUTO);
1589 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, WLC_PROTECTION_CTL_OVERLAP);
1591 /* 802.11g draft 4.0 NonERP elt advertisement */
1592 wlc->include_legacy_erp = true;
1594 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
1595 wlc->stf->txant = ANT_TX_DEF;
1597 wlc->prb_resp_timeout = WLC_PRB_RESP_TIMEOUT;
1599 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
1600 for (i = 0; i < NFIFO; i++)
1601 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
1602 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
1604 /* default rate fallback retry limits */
1605 wlc->SFBL = RETRY_SHORT_FB;
1606 wlc->LFBL = RETRY_LONG_FB;
1608 /* default mac retry limits */
1609 wlc->SRL = RETRY_SHORT_DEF;
1610 wlc->LRL = RETRY_LONG_DEF;
1612 /* init PM state */
1613 wlc->PM = PM_OFF; /* User's setting of PM mode through IOCTL */
1614 wlc->PM_override = false; /* Prevents from going to PM if our AP is 'ill' */
1615 wlc->PMenabled = false; /* Current PM state */
1616 wlc->PMpending = false; /* Tracks whether STA indicated PM in the last attempt */
1617 wlc->PMblocked = false; /* To allow blocking going into PM during RM and scans */
1619 /* In WMM Auto mode, PM is allowed if association is a UAPSD association */
1620 wlc->WME_PM_blocked = false;
1622 /* Init wme queuing method */
1623 wlc->wme_prec_queuing = false;
1625 /* Overrides for the core to stay awake under zillion conditions Look for STAY_AWAKE */
1626 wlc->wake = false;
1627 /* Are we waiting for a response to PS-Poll that we sent */
1628 wlc->PSpoll = false;
1630 /* APSD defaults */
1631 wlc->wme_apsd = true;
1632 wlc->apsd_sta_usp = false;
1633 wlc->apsd_trigger_timeout = 0; /* disable the trigger timer */
1634 wlc->apsd_trigger_ac = AC_BITMAP_ALL;
1636 /* Set flag to indicate that hw keys should be used when available. */
1637 wlc->wsec_swkeys = false;
1639 /* init the 4 static WEP default keys */
1640 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
1641 wlc->wsec_keys[i] = wlc->wsec_def_keys[i];
1642 wlc->wsec_keys[i]->idx = (u8) i;
1645 wlc->_regulatory_domain = false; /* 802.11d */
1647 /* WME QoS mode is Auto by default */
1648 wlc->pub->_wme = AUTO;
1650 #ifdef BCMSDIODEV_ENABLED
1651 wlc->pub->_priofc = true; /* enable priority flow control for sdio dongle */
1652 #endif
1654 wlc->pub->_ampdu = AMPDU_AGG_HOST;
1655 wlc->pub->bcmerror = 0;
1656 wlc->ibss_allowed = true;
1657 wlc->ibss_coalesce_allowed = true;
1658 wlc->pub->_coex = ON;
1660 /* intialize mpc delay */
1661 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
1663 wlc->pr80838_war = true;
1666 static bool wlc_state_bmac_sync(struct wlc_info *wlc)
1668 wlc_bmac_state_t state_bmac;
1670 if (wlc_bmac_state_get(wlc->hw, &state_bmac) != 0)
1671 return false;
1673 wlc->machwcap = state_bmac.machwcap;
1674 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR,
1675 (s8) state_bmac.preamble_ovr);
1677 return true;
1680 static uint wlc_attach_module(struct wlc_info *wlc)
1682 uint err = 0;
1683 uint unit;
1684 unit = wlc->pub->unit;
1686 wlc->asi = wlc_antsel_attach(wlc, wlc->osh, wlc->pub, wlc->hw);
1687 if (wlc->asi == NULL) {
1688 WL_ERROR("wl%d: wlc_attach: wlc_antsel_attach failed\n", unit);
1689 err = 44;
1690 goto fail;
1693 wlc->ampdu = wlc_ampdu_attach(wlc);
1694 if (wlc->ampdu == NULL) {
1695 WL_ERROR("wl%d: wlc_attach: wlc_ampdu_attach failed\n", unit);
1696 err = 50;
1697 goto fail;
1700 /* Initialize event queue; needed before following calls */
1701 wlc->eventq =
1702 wlc_eventq_attach(wlc->pub, wlc, wlc->wl, wlc_process_eventq);
1703 if (wlc->eventq == NULL) {
1704 WL_ERROR("wl%d: wlc_attach: wlc_eventq_attachfailed\n", unit);
1705 err = 57;
1706 goto fail;
1709 if ((wlc_stf_attach(wlc) != 0)) {
1710 WL_ERROR("wl%d: wlc_attach: wlc_stf_attach failed\n", unit);
1711 err = 68;
1712 goto fail;
1714 fail:
1715 return err;
1718 struct wlc_pub *wlc_pub(void *wlc)
1720 return ((struct wlc_info *) wlc)->pub;
1723 #define CHIP_SUPPORTS_11N(wlc) 1
1726 * The common driver entry routine. Error codes should be unique
1728 void *wlc_attach(void *wl, u16 vendor, u16 device, uint unit, bool piomode,
1729 struct osl_info *osh, void *regsva, uint bustype,
1730 void *btparam, uint *perr)
1732 struct wlc_info *wlc;
1733 uint err = 0;
1734 uint j;
1735 struct wlc_pub *pub;
1736 wlc_txq_info_t *qi;
1737 uint n_disabled;
1739 WL_NONE("wl%d: %s: vendor 0x%x device 0x%x\n",
1740 unit, __func__, vendor, device);
1742 ASSERT(WSEC_MAX_RCMTA_KEYS <= WSEC_MAX_KEYS);
1743 ASSERT(WSEC_MAX_DEFAULT_KEYS == WLC_DEFAULT_KEYS);
1745 /* some code depends on packed structures */
1746 ASSERT(sizeof(struct ethhdr) == ETH_HLEN);
1747 ASSERT(sizeof(d11regs_t) == SI_CORE_SIZE);
1748 ASSERT(sizeof(ofdm_phy_hdr_t) == D11_PHY_HDR_LEN);
1749 ASSERT(sizeof(cck_phy_hdr_t) == D11_PHY_HDR_LEN);
1750 ASSERT(sizeof(d11txh_t) == D11_TXH_LEN);
1751 ASSERT(sizeof(d11rxhdr_t) == RXHDR_LEN);
1752 ASSERT(sizeof(struct ieee80211_hdr) == DOT11_A4_HDR_LEN);
1753 ASSERT(sizeof(struct ieee80211_rts) == DOT11_RTS_LEN);
1754 ASSERT(sizeof(tx_status_t) == TXSTATUS_LEN);
1755 ASSERT(sizeof(struct ieee80211_ht_cap) == HT_CAP_IE_LEN);
1756 #ifdef BRCM_FULLMAC
1757 ASSERT(offsetof(wl_scan_params_t, channel_list) ==
1758 WL_SCAN_PARAMS_FIXED_SIZE);
1759 #endif
1760 ASSERT(IS_ALIGNED(offsetof(wsec_key_t, data), sizeof(u32)));
1761 ASSERT(ISPOWEROF2(MA_WINDOW_SZ));
1763 ASSERT(sizeof(wlc_d11rxhdr_t) <= WL_HWRXOFF);
1766 * Number of replay counters value used in WPA IE must match # rxivs
1767 * supported in wsec_key_t struct. See 802.11i/D3.0 sect. 7.3.2.17
1768 * 'RSN Information Element' figure 8 for this mapping.
1770 ASSERT((WPA_CAP_16_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1771 && 16 == WLC_NUMRXIVS)
1772 || (WPA_CAP_4_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1773 && 4 == WLC_NUMRXIVS));
1775 /* allocate struct wlc_info state and its substructures */
1776 wlc = (struct wlc_info *) wlc_attach_malloc(osh, unit, &err, device);
1777 if (wlc == NULL)
1778 goto fail;
1779 wlc->osh = osh;
1780 pub = wlc->pub;
1782 #if defined(BCMDBG)
1783 wlc_info_dbg = wlc;
1784 #endif
1786 wlc->band = wlc->bandstate[0];
1787 wlc->core = wlc->corestate;
1788 wlc->wl = wl;
1789 pub->unit = unit;
1790 pub->osh = osh;
1791 wlc->btparam = btparam;
1792 pub->_piomode = piomode;
1793 wlc->bandinit_pending = false;
1794 /* By default restrict TKIP associations from 11n STA's */
1795 wlc->ht_wsec_restriction = WLC_HT_TKIP_RESTRICT;
1797 /* populate struct wlc_info with default values */
1798 wlc_info_init(wlc, unit);
1800 /* update sta/ap related parameters */
1801 wlc_ap_upd(wlc);
1803 /* 11n_disable nvram */
1804 n_disabled = getintvar(pub->vars, "11n_disable");
1806 /* register a module (to handle iovars) */
1807 wlc_module_register(wlc->pub, wlc_iovars, "wlc_iovars", wlc,
1808 wlc_doiovar, NULL, NULL);
1810 /* low level attach steps(all hw accesses go inside, no more in rest of the attach) */
1811 err = wlc_bmac_attach(wlc, vendor, device, unit, piomode, osh, regsva,
1812 bustype, btparam);
1813 if (err)
1814 goto fail;
1816 /* for some states, due to different info pointer(e,g, wlc, wlc_hw) or master/slave split,
1817 * HIGH driver(both monolithic and HIGH_ONLY) needs to sync states FROM BMAC portion driver
1819 if (!wlc_state_bmac_sync(wlc)) {
1820 err = 20;
1821 goto fail;
1824 pub->phy_11ncapable = WLC_PHY_11N_CAP(wlc->band);
1826 /* propagate *vars* from BMAC driver to high driver */
1827 wlc_bmac_copyfrom_vars(wlc->hw, &pub->vars, &wlc->vars_size);
1830 /* set maximum allowed duty cycle */
1831 wlc->tx_duty_cycle_ofdm =
1832 (u16) getintvar(pub->vars, "tx_duty_cycle_ofdm");
1833 wlc->tx_duty_cycle_cck =
1834 (u16) getintvar(pub->vars, "tx_duty_cycle_cck");
1836 wlc_stf_phy_chain_calc(wlc);
1838 /* txchain 1: txant 0, txchain 2: txant 1 */
1839 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
1840 wlc->stf->txant = wlc->stf->hw_txchain - 1;
1842 /* push to BMAC driver */
1843 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
1844 wlc->stf->hw_rxchain);
1846 /* pull up some info resulting from the low attach */
1848 int i;
1849 for (i = 0; i < NFIFO; i++)
1850 wlc->core->txavail[i] = wlc->hw->txavail[i];
1853 wlc_bmac_hw_etheraddr(wlc->hw, wlc->perm_etheraddr);
1855 bcopy((char *)&wlc->perm_etheraddr, (char *)&pub->cur_etheraddr,
1856 ETH_ALEN);
1858 for (j = 0; j < NBANDS(wlc); j++) {
1859 /* Use band 1 for single band 11a */
1860 if (IS_SINGLEBAND_5G(wlc->deviceid))
1861 j = BAND_5G_INDEX;
1863 wlc->band = wlc->bandstate[j];
1865 if (!wlc_attach_stf_ant_init(wlc)) {
1866 err = 24;
1867 goto fail;
1870 /* default contention windows size limits */
1871 wlc->band->CWmin = APHY_CWMIN;
1872 wlc->band->CWmax = PHY_CWMAX;
1874 /* init gmode value */
1875 if (BAND_2G(wlc->band->bandtype)) {
1876 wlc->band->gmode = GMODE_AUTO;
1877 wlc_protection_upd(wlc, WLC_PROT_G_USER,
1878 wlc->band->gmode);
1881 /* init _n_enab supported mode */
1882 if (WLC_PHY_11N_CAP(wlc->band) && CHIP_SUPPORTS_11N(wlc)) {
1883 if (n_disabled & WLFEATURE_DISABLE_11N) {
1884 pub->_n_enab = OFF;
1885 wlc_protection_upd(wlc, WLC_PROT_N_USER, OFF);
1886 } else {
1887 pub->_n_enab = SUPPORT_11N;
1888 wlc_protection_upd(wlc, WLC_PROT_N_USER,
1889 ((pub->_n_enab ==
1890 SUPPORT_11N) ? WL_11N_2x2 :
1891 WL_11N_3x3));
1895 /* init per-band default rateset, depend on band->gmode */
1896 wlc_default_rateset(wlc, &wlc->band->defrateset);
1898 /* fill in hw_rateset (used early by WLC_SET_RATESET) */
1899 wlc_rateset_filter(&wlc->band->defrateset,
1900 &wlc->band->hw_rateset, false,
1901 WLC_RATES_CCK_OFDM, RATE_MASK,
1902 (bool) N_ENAB(wlc->pub));
1905 /* update antenna config due to wlc->stf->txant/txchain/ant_rx_ovr change */
1906 wlc_stf_phy_txant_upd(wlc);
1908 /* attach each modules */
1909 err = wlc_attach_module(wlc);
1910 if (err != 0)
1911 goto fail;
1913 if (!wlc_timers_init(wlc, unit)) {
1914 WL_ERROR("wl%d: %s: wlc_init_timer failed\n", unit, __func__);
1915 err = 32;
1916 goto fail;
1919 /* depend on rateset, gmode */
1920 wlc->cmi = wlc_channel_mgr_attach(wlc);
1921 if (!wlc->cmi) {
1922 WL_ERROR("wl%d: %s: wlc_channel_mgr_attach failed\n",
1923 unit, __func__);
1924 err = 33;
1925 goto fail;
1928 /* init default when all parameters are ready, i.e. ->rateset */
1929 wlc_bss_default_init(wlc);
1932 * Complete the wlc default state initializations..
1935 /* allocate our initial queue */
1936 qi = wlc_txq_alloc(wlc, osh);
1937 if (qi == NULL) {
1938 WL_ERROR("wl%d: %s: failed to malloc tx queue\n",
1939 unit, __func__);
1940 err = 100;
1941 goto fail;
1943 wlc->active_queue = qi;
1945 wlc->bsscfg[0] = wlc->cfg;
1946 wlc->cfg->_idx = 0;
1947 wlc->cfg->wlc = wlc;
1948 pub->txmaxpkts = MAXTXPKTS;
1950 WLCNTSET(pub->_cnt->version, WL_CNT_T_VERSION);
1951 WLCNTSET(pub->_cnt->length, sizeof(wl_cnt_t));
1953 WLCNTSET(pub->_wme_cnt->version, WL_WME_CNT_VERSION);
1954 WLCNTSET(pub->_wme_cnt->length, sizeof(wl_wme_cnt_t));
1956 wlc_wme_initparams_sta(wlc, &wlc->wme_param_ie);
1958 wlc->mimoft = FT_HT;
1959 wlc->ht_cap.cap_info = HT_CAP;
1960 if (HT_ENAB(wlc->pub))
1961 wlc->stf->ldpc = AUTO;
1963 wlc->mimo_40txbw = AUTO;
1964 wlc->ofdm_40txbw = AUTO;
1965 wlc->cck_40txbw = AUTO;
1966 wlc_update_mimo_band_bwcap(wlc, WLC_N_BW_20IN2G_40IN5G);
1968 /* Enable setting the RIFS Mode bit by default in HT Info IE */
1969 wlc->rifs_advert = AUTO;
1971 /* Set default values of SGI */
1972 if (WLC_SGI_CAP_PHY(wlc)) {
1973 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1974 wlc->sgi_tx = AUTO;
1975 } else if (WLCISSSLPNPHY(wlc->band)) {
1976 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
1977 wlc->sgi_tx = AUTO;
1978 } else {
1979 wlc_ht_update_sgi_rx(wlc, 0);
1980 wlc->sgi_tx = OFF;
1983 /* *******nvram 11n config overrides Start ********* */
1985 /* apply the sgi override from nvram conf */
1986 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_TX)
1987 wlc->sgi_tx = OFF;
1989 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_RX)
1990 wlc_ht_update_sgi_rx(wlc, 0);
1992 /* apply the stbc override from nvram conf */
1993 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_TX) {
1994 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
1995 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
1996 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_TX_STBC;
1998 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_RX)
1999 wlc_stf_stbc_rx_set(wlc, HT_CAP_RX_STBC_NO);
2001 /* apply the GF override from nvram conf */
2002 if (n_disabled & WLFEATURE_DISABLE_11N_GF)
2003 wlc->ht_cap.cap_info &= ~IEEE80211_HT_CAP_GRN_FLD;
2005 /* initialize radio_mpc_disable according to wlc->mpc */
2006 wlc_radio_mpc_upd(wlc);
2008 if (WLANTSEL_ENAB(wlc)) {
2009 if ((wlc->pub->sih->chip) == BCM43235_CHIP_ID) {
2010 if ((getintvar(wlc->pub->vars, "aa2g") == 7) ||
2011 (getintvar(wlc->pub->vars, "aa5g") == 7)) {
2012 wlc_bmac_antsel_set(wlc->hw, 1);
2014 } else {
2015 wlc_bmac_antsel_set(wlc->hw, wlc->asi->antsel_avail);
2019 if (perr)
2020 *perr = 0;
2022 return (void *)wlc;
2024 fail:
2025 WL_ERROR("wl%d: %s: failed with err %d\n", unit, __func__, err);
2026 if (wlc)
2027 wlc_detach(wlc);
2029 if (perr)
2030 *perr = err;
2031 return NULL;
2034 static void wlc_attach_antgain_init(struct wlc_info *wlc)
2036 uint unit;
2037 unit = wlc->pub->unit;
2039 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
2040 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
2041 wlc->band->antgain = 8;
2042 } else if (wlc->band->antgain == -1) {
2043 WL_ERROR("wl%d: %s: Invalid antennas available in srom, using 2dB\n",
2044 unit, __func__);
2045 wlc->band->antgain = 8;
2046 } else {
2047 s8 gain, fract;
2048 /* Older sroms specified gain in whole dbm only. In order
2049 * be able to specify qdbm granularity and remain backward compatible
2050 * the whole dbms are now encoded in only low 6 bits and remaining qdbms
2051 * are encoded in the hi 2 bits. 6 bit signed number ranges from
2052 * -32 - 31. Examples: 0x1 = 1 db,
2053 * 0xc1 = 1.75 db (1 + 3 quarters),
2054 * 0x3f = -1 (-1 + 0 quarters),
2055 * 0x7f = -.75 (-1 in low 6 bits + 1 quarters in hi 2 bits) = -3 qdbm.
2056 * 0xbf = -.50 (-1 in low 6 bits + 2 quarters in hi 2 bits) = -2 qdbm.
2058 gain = wlc->band->antgain & 0x3f;
2059 gain <<= 2; /* Sign extend */
2060 gain >>= 2;
2061 fract = (wlc->band->antgain & 0xc0) >> 6;
2062 wlc->band->antgain = 4 * gain + fract;
2066 static bool wlc_attach_stf_ant_init(struct wlc_info *wlc)
2068 int aa;
2069 uint unit;
2070 char *vars;
2071 int bandtype;
2073 unit = wlc->pub->unit;
2074 vars = wlc->pub->vars;
2075 bandtype = wlc->band->bandtype;
2077 /* get antennas available */
2078 aa = (s8) getintvar(vars, (BAND_5G(bandtype) ? "aa5g" : "aa2g"));
2079 if (aa == 0)
2080 aa = (s8) getintvar(vars,
2081 (BAND_5G(bandtype) ? "aa1" : "aa0"));
2082 if ((aa < 1) || (aa > 15)) {
2083 WL_ERROR("wl%d: %s: Invalid antennas available in srom (0x%x), using 3\n",
2084 unit, __func__, aa);
2085 aa = 3;
2088 /* reset the defaults if we have a single antenna */
2089 if (aa == 1) {
2090 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
2091 wlc->stf->txant = ANT_TX_FORCE_0;
2092 } else if (aa == 2) {
2093 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
2094 wlc->stf->txant = ANT_TX_FORCE_1;
2095 } else {
2098 /* Compute Antenna Gain */
2099 wlc->band->antgain =
2100 (s8) getintvar(vars, (BAND_5G(bandtype) ? "ag1" : "ag0"));
2101 wlc_attach_antgain_init(wlc);
2103 return true;
2107 static void wlc_timers_deinit(struct wlc_info *wlc)
2109 /* free timer state */
2110 if (wlc->wdtimer) {
2111 wl_free_timer(wlc->wl, wlc->wdtimer);
2112 wlc->wdtimer = NULL;
2114 if (wlc->radio_timer) {
2115 wl_free_timer(wlc->wl, wlc->radio_timer);
2116 wlc->radio_timer = NULL;
2120 static void wlc_detach_module(struct wlc_info *wlc)
2122 if (wlc->asi) {
2123 wlc_antsel_detach(wlc->asi);
2124 wlc->asi = NULL;
2127 if (wlc->ampdu) {
2128 wlc_ampdu_detach(wlc->ampdu);
2129 wlc->ampdu = NULL;
2132 wlc_stf_detach(wlc);
2136 * Return a count of the number of driver callbacks still pending.
2138 * General policy is that wlc_detach can only dealloc/free software states. It can NOT
2139 * touch hardware registers since the d11core may be in reset and clock may not be available.
2140 * One exception is sb register access, which is possible if crystal is turned on
2141 * After "down" state, driver should avoid software timer with the exception of radio_monitor.
2143 uint wlc_detach(struct wlc_info *wlc)
2145 uint i;
2146 uint callbacks = 0;
2148 if (wlc == NULL)
2149 return 0;
2151 WL_TRACE("wl%d: %s\n", wlc->pub->unit, __func__);
2153 ASSERT(!wlc->pub->up);
2155 callbacks += wlc_bmac_detach(wlc);
2157 /* delete software timers */
2158 if (!wlc_radio_monitor_stop(wlc))
2159 callbacks++;
2161 if (wlc->eventq) {
2162 wlc_eventq_detach(wlc->eventq);
2163 wlc->eventq = NULL;
2166 wlc_channel_mgr_detach(wlc->cmi);
2168 wlc_timers_deinit(wlc);
2170 wlc_detach_module(wlc);
2172 /* free other state */
2175 #ifdef BCMDBG
2176 if (wlc->country_ie_override) {
2177 kfree(wlc->country_ie_override);
2178 wlc->country_ie_override = NULL;
2180 #endif /* BCMDBG */
2183 /* free dumpcb list */
2184 dumpcb_t *prev, *ptr;
2185 prev = ptr = wlc->dumpcb_head;
2186 while (ptr) {
2187 ptr = prev->next;
2188 kfree(prev);
2189 prev = ptr;
2191 wlc->dumpcb_head = NULL;
2194 /* Detach from iovar manager */
2195 wlc_module_unregister(wlc->pub, "wlc_iovars", wlc);
2197 while (wlc->tx_queues != NULL) {
2198 wlc_txq_free(wlc, wlc->osh, wlc->tx_queues);
2202 * consistency check: wlc_module_register/wlc_module_unregister calls
2203 * should match therefore nothing should be left here.
2205 for (i = 0; i < WLC_MAXMODULES; i++)
2206 ASSERT(wlc->modulecb[i].name[0] == '\0');
2208 wlc_detach_mfree(wlc, wlc->osh);
2209 return callbacks;
2212 /* update state that depends on the current value of "ap" */
2213 void wlc_ap_upd(struct wlc_info *wlc)
2215 if (AP_ENAB(wlc->pub))
2216 wlc->PLCPHdr_override = WLC_PLCP_AUTO; /* AP: short not allowed, but not enforced */
2217 else
2218 wlc->PLCPHdr_override = WLC_PLCP_SHORT; /* STA-BSS; short capable */
2220 /* disable vlan_mode on AP since some legacy STAs cannot rx tagged pkts */
2221 wlc->vlan_mode = AP_ENAB(wlc->pub) ? OFF : AUTO;
2223 /* fixup mpc */
2224 wlc->mpc = true;
2227 /* read hwdisable state and propagate to wlc flag */
2228 static void wlc_radio_hwdisable_upd(struct wlc_info *wlc)
2230 if (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO || wlc->pub->hw_off)
2231 return;
2233 if (wlc_bmac_radio_read_hwdisabled(wlc->hw)) {
2234 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2235 } else {
2236 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2240 /* return true if Minimum Power Consumption should be entered, false otherwise */
2241 bool wlc_is_non_delay_mpc(struct wlc_info *wlc)
2243 return false;
2246 bool wlc_ismpc(struct wlc_info *wlc)
2248 return (wlc->mpc_delay_off == 0) && (wlc_is_non_delay_mpc(wlc));
2251 void wlc_radio_mpc_upd(struct wlc_info *wlc)
2253 bool mpc_radio, radio_state;
2256 * Clear the WL_RADIO_MPC_DISABLE bit when mpc feature is disabled
2257 * in case the WL_RADIO_MPC_DISABLE bit was set. Stop the radio
2258 * monitor also when WL_RADIO_MPC_DISABLE is the only reason that
2259 * the radio is going down.
2261 if (!wlc->mpc) {
2262 if (!wlc->pub->radio_disabled)
2263 return;
2264 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2265 wlc_radio_upd(wlc);
2266 if (!wlc->pub->radio_disabled)
2267 wlc_radio_monitor_stop(wlc);
2268 return;
2272 * sync ismpc logic with WL_RADIO_MPC_DISABLE bit in wlc->pub->radio_disabled
2273 * to go ON, always call radio_upd synchronously
2274 * to go OFF, postpone radio_upd to later when context is safe(e.g. watchdog)
2276 radio_state =
2277 (mboolisset(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE) ? OFF :
2278 ON);
2279 mpc_radio = (wlc_ismpc(wlc) == true) ? OFF : ON;
2281 if (radio_state == ON && mpc_radio == OFF)
2282 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2283 else if (radio_state == OFF && mpc_radio == ON) {
2284 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2285 wlc_radio_upd(wlc);
2286 if (wlc->mpc_offcnt < WLC_MPC_THRESHOLD) {
2287 wlc->mpc_dlycnt = WLC_MPC_MAX_DELAYCNT;
2288 } else
2289 wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
2290 wlc->mpc_dur += OSL_SYSUPTIME() - wlc->mpc_laston_ts;
2292 /* Below logic is meant to capture the transition from mpc off to mpc on for reasons
2293 * other than wlc->mpc_delay_off keeping the mpc off. In that case reset
2294 * wlc->mpc_delay_off to wlc->mpc_dlycnt, so that we restart the countdown of mpc_delay_off
2296 if ((wlc->prev_non_delay_mpc == false) &&
2297 (wlc_is_non_delay_mpc(wlc) == true) && wlc->mpc_delay_off) {
2298 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2300 wlc->prev_non_delay_mpc = wlc_is_non_delay_mpc(wlc);
2304 * centralized radio disable/enable function,
2305 * invoke radio enable/disable after updating hwradio status
2307 static void wlc_radio_upd(struct wlc_info *wlc)
2311 /* maintain LED behavior in down state */
2312 static void wlc_down_led_upd(struct wlc_info *wlc)
2314 ASSERT(!wlc->pub->up);
2316 /* maintain LEDs while in down state, turn on sbclk if not available yet */
2317 /* turn on sbclk if necessary */
2318 if (!AP_ENAB(wlc->pub)) {
2319 wlc_pllreq(wlc, true, WLC_PLLREQ_FLIP);
2321 wlc_pllreq(wlc, false, WLC_PLLREQ_FLIP);
2325 /* update hwradio status and return it */
2326 bool wlc_check_radio_disabled(struct wlc_info *wlc)
2328 wlc_radio_hwdisable_upd(wlc);
2330 return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ? true : false;
2333 void wlc_radio_disable(struct wlc_info *wlc)
2335 if (!wlc->pub->up) {
2336 wlc_down_led_upd(wlc);
2337 return;
2340 wlc_radio_monitor_start(wlc);
2341 wl_down(wlc->wl);
2344 static void wlc_radio_enable(struct wlc_info *wlc)
2346 if (wlc->pub->up)
2347 return;
2349 if (DEVICEREMOVED(wlc))
2350 return;
2352 if (!wlc->down_override) { /* imposed by wl down/out ioctl */
2353 wl_up(wlc->wl);
2357 /* periodical query hw radio button while driver is "down" */
2358 static void wlc_radio_timer(void *arg)
2360 struct wlc_info *wlc = (struct wlc_info *) arg;
2362 if (DEVICEREMOVED(wlc)) {
2363 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
2364 wl_down(wlc->wl);
2365 return;
2368 /* cap mpc off count */
2369 if (wlc->mpc_offcnt < WLC_MPC_MAX_DELAYCNT)
2370 wlc->mpc_offcnt++;
2372 /* validate all the reasons driver could be down and running this radio_timer */
2373 ASSERT(wlc->pub->radio_disabled || wlc->down_override);
2374 wlc_radio_hwdisable_upd(wlc);
2375 wlc_radio_upd(wlc);
2378 static bool wlc_radio_monitor_start(struct wlc_info *wlc)
2380 /* Don't start the timer if HWRADIO feature is disabled */
2381 if (wlc->radio_monitor || (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO))
2382 return true;
2384 wlc->radio_monitor = true;
2385 wlc_pllreq(wlc, true, WLC_PLLREQ_RADIO_MON);
2386 wl_add_timer(wlc->wl, wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
2387 return true;
2390 bool wlc_radio_monitor_stop(struct wlc_info *wlc)
2392 if (!wlc->radio_monitor)
2393 return true;
2395 ASSERT((wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO) !=
2396 WL_SWFL_NOHWRADIO);
2398 wlc->radio_monitor = false;
2399 wlc_pllreq(wlc, false, WLC_PLLREQ_RADIO_MON);
2400 return wl_del_timer(wlc->wl, wlc->radio_timer);
2403 /* bring the driver down, but don't reset hardware */
2404 void wlc_out(struct wlc_info *wlc)
2406 wlc_bmac_set_noreset(wlc->hw, true);
2407 wlc_radio_upd(wlc);
2408 wl_down(wlc->wl);
2409 wlc_bmac_set_noreset(wlc->hw, false);
2411 /* core clk is true in BMAC driver due to noreset, need to mirror it in HIGH */
2412 wlc->clk = true;
2414 /* This will make sure that when 'up' is done
2415 * after 'out' it'll restore hardware (especially gpios)
2417 wlc->pub->hw_up = false;
2420 #if defined(BCMDBG)
2421 /* Verify the sanity of wlc->tx_prec_map. This can be done only by making sure that
2422 * if there is no packet pending for the FIFO, then the corresponding prec bits should be set
2423 * in prec_map. Of course, ignore this rule when block_datafifo is set
2425 static bool wlc_tx_prec_map_verify(struct wlc_info *wlc)
2427 /* For non-WME, both fifos have overlapping prec_map. So it's an error only if both
2428 * fail the check.
2430 if (!EDCF_ENAB(wlc->pub)) {
2431 if (!(WLC_TX_FIFO_CHECK(wlc, TX_DATA_FIFO) ||
2432 WLC_TX_FIFO_CHECK(wlc, TX_CTL_FIFO)))
2433 return false;
2434 else
2435 return true;
2438 return WLC_TX_FIFO_CHECK(wlc, TX_AC_BK_FIFO)
2439 && WLC_TX_FIFO_CHECK(wlc, TX_AC_BE_FIFO)
2440 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VI_FIFO)
2441 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VO_FIFO);
2443 #endif /* BCMDBG */
2445 static void wlc_watchdog_by_timer(void *arg)
2447 struct wlc_info *wlc = (struct wlc_info *) arg;
2448 wlc_watchdog(arg);
2449 if (WLC_WATCHDOG_TBTT(wlc)) {
2450 /* set to normal osl watchdog period */
2451 wl_del_timer(wlc->wl, wlc->wdtimer);
2452 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
2453 true);
2457 /* common watchdog code */
2458 static void wlc_watchdog(void *arg)
2460 struct wlc_info *wlc = (struct wlc_info *) arg;
2461 int i;
2462 wlc_bsscfg_t *cfg;
2464 WL_TRACE("wl%d: wlc_watchdog\n", wlc->pub->unit);
2466 if (!wlc->pub->up)
2467 return;
2469 if (DEVICEREMOVED(wlc)) {
2470 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
2471 wl_down(wlc->wl);
2472 return;
2475 /* increment second count */
2476 wlc->pub->now++;
2478 /* delay radio disable */
2479 if (wlc->mpc_delay_off) {
2480 if (--wlc->mpc_delay_off == 0) {
2481 mboolset(wlc->pub->radio_disabled,
2482 WL_RADIO_MPC_DISABLE);
2483 if (wlc->mpc && wlc_ismpc(wlc))
2484 wlc->mpc_offcnt = 0;
2485 wlc->mpc_laston_ts = OSL_SYSUPTIME();
2489 /* mpc sync */
2490 wlc_radio_mpc_upd(wlc);
2491 /* radio sync: sw/hw/mpc --> radio_disable/radio_enable */
2492 wlc_radio_hwdisable_upd(wlc);
2493 wlc_radio_upd(wlc);
2494 /* if ismpc, driver should be in down state if up/down is allowed */
2495 if (wlc->mpc && wlc_ismpc(wlc))
2496 ASSERT(!wlc->pub->up);
2497 /* if radio is disable, driver may be down, quit here */
2498 if (wlc->pub->radio_disabled)
2499 return;
2501 wlc_bmac_watchdog(wlc);
2503 /* occasionally sample mac stat counters to detect 16-bit counter wrap */
2504 if ((WLC_UPDATE_STATS(wlc))
2505 && (!(wlc->pub->now % SW_TIMER_MAC_STAT_UPD)))
2506 wlc_statsupd(wlc);
2508 /* Manage TKIP countermeasures timers */
2509 FOREACH_BSS(wlc, i, cfg) {
2510 if (cfg->tk_cm_dt) {
2511 cfg->tk_cm_dt--;
2513 if (cfg->tk_cm_bt) {
2514 cfg->tk_cm_bt--;
2518 /* Call any registered watchdog handlers */
2519 for (i = 0; i < WLC_MAXMODULES; i++) {
2520 if (wlc->modulecb[i].watchdog_fn)
2521 wlc->modulecb[i].watchdog_fn(wlc->modulecb[i].hdl);
2524 if (WLCISNPHY(wlc->band) && !wlc->pub->tempsense_disable &&
2525 ((wlc->pub->now - wlc->tempsense_lasttime) >=
2526 WLC_TEMPSENSE_PERIOD)) {
2527 wlc->tempsense_lasttime = wlc->pub->now;
2528 wlc_tempsense_upd(wlc);
2530 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
2531 ASSERT(wlc_bmac_taclear(wlc->hw, true));
2533 /* Verify that tx_prec_map and fifos are in sync to avoid lock ups */
2534 ASSERT(wlc_tx_prec_map_verify(wlc));
2536 ASSERT(wlc_ps_check(wlc));
2539 /* make interface operational */
2540 int wlc_up(struct wlc_info *wlc)
2542 WL_TRACE("wl%d: %s:\n", wlc->pub->unit, __func__);
2544 /* HW is turned off so don't try to access it */
2545 if (wlc->pub->hw_off || DEVICEREMOVED(wlc))
2546 return BCME_RADIOOFF;
2548 if (!wlc->pub->hw_up) {
2549 wlc_bmac_hw_up(wlc->hw);
2550 wlc->pub->hw_up = true;
2553 if ((wlc->pub->boardflags & BFL_FEM)
2554 && (wlc->pub->sih->chip == BCM4313_CHIP_ID)) {
2555 if (wlc->pub->boardrev >= 0x1250
2556 && (wlc->pub->boardflags & BFL_FEM_BT)) {
2557 wlc_mhf(wlc, MHF5, MHF5_4313_GPIOCTRL,
2558 MHF5_4313_GPIOCTRL, WLC_BAND_ALL);
2559 } else {
2560 wlc_mhf(wlc, MHF4, MHF4_EXTPA_ENABLE, MHF4_EXTPA_ENABLE,
2561 WLC_BAND_ALL);
2566 * Need to read the hwradio status here to cover the case where the system
2567 * is loaded with the hw radio disabled. We do not want to bring the driver up in this case.
2568 * if radio is disabled, abort up, lower power, start radio timer and return 0(for NDIS)
2569 * don't call radio_update to avoid looping wlc_up.
2571 * wlc_bmac_up_prep() returns either 0 or BCME_RADIOOFF only
2573 if (!wlc->pub->radio_disabled) {
2574 int status = wlc_bmac_up_prep(wlc->hw);
2575 if (status == BCME_RADIOOFF) {
2576 if (!mboolisset
2577 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
2578 int idx;
2579 wlc_bsscfg_t *bsscfg;
2580 mboolset(wlc->pub->radio_disabled,
2581 WL_RADIO_HW_DISABLE);
2583 FOREACH_BSS(wlc, idx, bsscfg) {
2584 if (!BSSCFG_STA(bsscfg)
2585 || !bsscfg->enable || !bsscfg->BSS)
2586 continue;
2587 WL_ERROR("wl%d.%d: wlc_up: rfdisable -> " "wlc_bsscfg_disable()\n",
2588 wlc->pub->unit, idx);
2591 } else
2592 ASSERT(!status);
2595 if (wlc->pub->radio_disabled) {
2596 wlc_radio_monitor_start(wlc);
2597 return 0;
2600 /* wlc_bmac_up_prep has done wlc_corereset(). so clk is on, set it */
2601 wlc->clk = true;
2603 wlc_radio_monitor_stop(wlc);
2605 /* Set EDCF hostflags */
2606 if (EDCF_ENAB(wlc->pub)) {
2607 wlc_mhf(wlc, MHF1, MHF1_EDCF, MHF1_EDCF, WLC_BAND_ALL);
2608 } else {
2609 wlc_mhf(wlc, MHF1, MHF1_EDCF, 0, WLC_BAND_ALL);
2612 if (WLC_WAR16165(wlc))
2613 wlc_mhf(wlc, MHF2, MHF2_PCISLOWCLKWAR, MHF2_PCISLOWCLKWAR,
2614 WLC_BAND_ALL);
2616 wl_init(wlc->wl);
2617 wlc->pub->up = true;
2619 if (wlc->bandinit_pending) {
2620 wlc_suspend_mac_and_wait(wlc);
2621 wlc_set_chanspec(wlc, wlc->default_bss->chanspec);
2622 wlc->bandinit_pending = false;
2623 wlc_enable_mac(wlc);
2626 wlc_bmac_up_finish(wlc->hw);
2628 /* other software states up after ISR is running */
2629 /* start APs that were to be brought up but are not up yet */
2630 /* if (AP_ENAB(wlc->pub)) wlc_restart_ap(wlc->ap); */
2632 /* Program the TX wme params with the current settings */
2633 wlc_wme_retries_write(wlc);
2635 /* start one second watchdog timer */
2636 ASSERT(!wlc->WDarmed);
2637 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
2638 wlc->WDarmed = true;
2640 /* ensure antenna config is up to date */
2641 wlc_stf_phy_txant_upd(wlc);
2642 /* ensure LDPC config is in sync */
2643 wlc_ht_update_ldpc(wlc, wlc->stf->ldpc);
2645 return 0;
2648 /* Initialize the base precedence map for dequeueing from txq based on WME settings */
2649 static void wlc_tx_prec_map_init(struct wlc_info *wlc)
2651 wlc->tx_prec_map = WLC_PREC_BMP_ALL;
2652 memset(wlc->fifo2prec_map, 0, NFIFO * sizeof(u16));
2654 /* For non-WME, both fifos have overlapping MAXPRIO. So just disable all precedences
2655 * if either is full.
2657 if (!EDCF_ENAB(wlc->pub)) {
2658 wlc->fifo2prec_map[TX_DATA_FIFO] = WLC_PREC_BMP_ALL;
2659 wlc->fifo2prec_map[TX_CTL_FIFO] = WLC_PREC_BMP_ALL;
2660 } else {
2661 wlc->fifo2prec_map[TX_AC_BK_FIFO] = WLC_PREC_BMP_AC_BK;
2662 wlc->fifo2prec_map[TX_AC_BE_FIFO] = WLC_PREC_BMP_AC_BE;
2663 wlc->fifo2prec_map[TX_AC_VI_FIFO] = WLC_PREC_BMP_AC_VI;
2664 wlc->fifo2prec_map[TX_AC_VO_FIFO] = WLC_PREC_BMP_AC_VO;
2668 static uint wlc_down_del_timer(struct wlc_info *wlc)
2670 uint callbacks = 0;
2672 return callbacks;
2676 * Mark the interface nonoperational, stop the software mechanisms,
2677 * disable the hardware, free any transient buffer state.
2678 * Return a count of the number of driver callbacks still pending.
2680 uint wlc_down(struct wlc_info *wlc)
2683 uint callbacks = 0;
2684 int i;
2685 bool dev_gone = false;
2686 wlc_txq_info_t *qi;
2688 WL_TRACE("wl%d: %s:\n", wlc->pub->unit, __func__);
2690 /* check if we are already in the going down path */
2691 if (wlc->going_down) {
2692 WL_ERROR("wl%d: %s: Driver going down so return\n",
2693 wlc->pub->unit, __func__);
2694 return 0;
2696 if (!wlc->pub->up)
2697 return callbacks;
2699 /* in between, mpc could try to bring down again.. */
2700 wlc->going_down = true;
2702 callbacks += wlc_bmac_down_prep(wlc->hw);
2704 dev_gone = DEVICEREMOVED(wlc);
2706 /* Call any registered down handlers */
2707 for (i = 0; i < WLC_MAXMODULES; i++) {
2708 if (wlc->modulecb[i].down_fn)
2709 callbacks +=
2710 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
2713 /* cancel the watchdog timer */
2714 if (wlc->WDarmed) {
2715 if (!wl_del_timer(wlc->wl, wlc->wdtimer))
2716 callbacks++;
2717 wlc->WDarmed = false;
2719 /* cancel all other timers */
2720 callbacks += wlc_down_del_timer(wlc);
2722 /* interrupt must have been blocked */
2723 ASSERT((wlc->macintmask == 0) || !wlc->pub->up);
2725 wlc->pub->up = false;
2727 wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
2729 /* clear txq flow control */
2730 wlc_txflowcontrol_reset(wlc);
2732 /* flush tx queues */
2733 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
2734 pktq_flush(wlc->osh, &qi->q, true, NULL, 0);
2735 ASSERT(pktq_empty(&qi->q));
2738 /* flush event queue.
2739 * Should be the last thing done after all the events are generated
2740 * Just delivers the events synchronously instead of waiting for a timer
2742 callbacks += wlc_eventq_down(wlc->eventq);
2744 callbacks += wlc_bmac_down_finish(wlc->hw);
2746 /* wlc_bmac_down_finish has done wlc_coredisable(). so clk is off */
2747 wlc->clk = false;
2750 /* Verify all packets are flushed from the driver */
2751 if (wlc->osh->pktalloced != 0) {
2752 WL_ERROR("%d packets not freed at wlc_down!!!!!!\n",
2753 wlc->osh->pktalloced);
2755 #ifdef BCMDBG
2756 /* Since all the packets should have been freed,
2757 * all callbacks should have been called
2759 for (i = 1; i <= wlc->pub->tunables->maxpktcb; i++)
2760 ASSERT(wlc->pkt_callback[i].fn == NULL);
2761 #endif
2762 wlc->going_down = false;
2763 return callbacks;
2766 /* Set the current gmode configuration */
2767 int wlc_set_gmode(struct wlc_info *wlc, u8 gmode, bool config)
2769 int ret = 0;
2770 uint i;
2771 wlc_rateset_t rs;
2772 /* Default to 54g Auto */
2773 s8 shortslot = WLC_SHORTSLOT_AUTO; /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
2774 bool shortslot_restrict = false; /* Restrict association to stations that support shortslot
2776 bool ignore_bcns = true; /* Ignore legacy beacons on the same channel */
2777 bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
2778 int preamble = WLC_PLCP_LONG; /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
2779 bool preamble_restrict = false; /* Restrict association to stations that support short
2780 * preambles
2782 struct wlcband *band;
2784 /* if N-support is enabled, allow Gmode set as long as requested
2785 * Gmode is not GMODE_LEGACY_B
2787 if (N_ENAB(wlc->pub) && gmode == GMODE_LEGACY_B)
2788 return BCME_UNSUPPORTED;
2790 /* verify that we are dealing with 2G band and grab the band pointer */
2791 if (wlc->band->bandtype == WLC_BAND_2G)
2792 band = wlc->band;
2793 else if ((NBANDS(wlc) > 1) &&
2794 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == WLC_BAND_2G))
2795 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
2796 else
2797 return BCME_BADBAND;
2799 /* Legacy or bust when no OFDM is supported by regulatory */
2800 if ((wlc_channel_locale_flags_in_band(wlc->cmi, band->bandunit) &
2801 WLC_NO_OFDM) && (gmode != GMODE_LEGACY_B))
2802 return BCME_RANGE;
2804 /* update configuration value */
2805 if (config == true)
2806 wlc_protection_upd(wlc, WLC_PROT_G_USER, gmode);
2808 /* Clear supported rates filter */
2809 memset(&wlc->sup_rates_override, 0, sizeof(wlc_rateset_t));
2811 /* Clear rateset override */
2812 memset(&rs, 0, sizeof(wlc_rateset_t));
2814 switch (gmode) {
2815 case GMODE_LEGACY_B:
2816 shortslot = WLC_SHORTSLOT_OFF;
2817 wlc_rateset_copy(&gphy_legacy_rates, &rs);
2819 break;
2821 case GMODE_LRS:
2822 if (AP_ENAB(wlc->pub))
2823 wlc_rateset_copy(&cck_rates, &wlc->sup_rates_override);
2824 break;
2826 case GMODE_AUTO:
2827 /* Accept defaults */
2828 break;
2830 case GMODE_ONLY:
2831 ofdm_basic = true;
2832 preamble = WLC_PLCP_SHORT;
2833 preamble_restrict = true;
2834 break;
2836 case GMODE_PERFORMANCE:
2837 if (AP_ENAB(wlc->pub)) /* Put all rates into the Supported Rates element */
2838 wlc_rateset_copy(&cck_ofdm_rates,
2839 &wlc->sup_rates_override);
2841 shortslot = WLC_SHORTSLOT_ON;
2842 shortslot_restrict = true;
2843 ofdm_basic = true;
2844 preamble = WLC_PLCP_SHORT;
2845 preamble_restrict = true;
2846 break;
2848 default:
2849 /* Error */
2850 WL_ERROR("wl%d: %s: invalid gmode %d\n",
2851 wlc->pub->unit, __func__, gmode);
2852 return BCME_UNSUPPORTED;
2856 * If we are switching to gmode == GMODE_LEGACY_B,
2857 * clean up rate info that may refer to OFDM rates.
2859 if ((gmode == GMODE_LEGACY_B) && (band->gmode != GMODE_LEGACY_B)) {
2860 band->gmode = gmode;
2861 if (band->rspec_override && !IS_CCK(band->rspec_override)) {
2862 band->rspec_override = 0;
2863 wlc_reprate_init(wlc);
2865 if (band->mrspec_override && !IS_CCK(band->mrspec_override)) {
2866 band->mrspec_override = 0;
2870 band->gmode = gmode;
2872 wlc->ignore_bcns = ignore_bcns;
2874 wlc->shortslot_override = shortslot;
2876 if (AP_ENAB(wlc->pub)) {
2877 /* wlc->ap->shortslot_restrict = shortslot_restrict; */
2878 wlc->PLCPHdr_override =
2879 (preamble !=
2880 WLC_PLCP_LONG) ? WLC_PLCP_SHORT : WLC_PLCP_AUTO;
2883 if ((AP_ENAB(wlc->pub) && preamble != WLC_PLCP_LONG)
2884 || preamble == WLC_PLCP_SHORT)
2885 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
2886 else
2887 wlc->default_bss->capability &= ~WLAN_CAPABILITY_SHORT_PREAMBLE;
2889 /* Update shortslot capability bit for AP and IBSS */
2890 if ((AP_ENAB(wlc->pub) && shortslot == WLC_SHORTSLOT_AUTO) ||
2891 shortslot == WLC_SHORTSLOT_ON)
2892 wlc->default_bss->capability |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
2893 else
2894 wlc->default_bss->capability &=
2895 ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
2897 /* Use the default 11g rateset */
2898 if (!rs.count)
2899 wlc_rateset_copy(&cck_ofdm_rates, &rs);
2901 if (ofdm_basic) {
2902 for (i = 0; i < rs.count; i++) {
2903 if (rs.rates[i] == WLC_RATE_6M
2904 || rs.rates[i] == WLC_RATE_12M
2905 || rs.rates[i] == WLC_RATE_24M)
2906 rs.rates[i] |= WLC_RATE_FLAG;
2910 /* Set default bss rateset */
2911 wlc->default_bss->rateset.count = rs.count;
2912 bcopy((char *)rs.rates, (char *)wlc->default_bss->rateset.rates,
2913 sizeof(wlc->default_bss->rateset.rates));
2915 return ret;
2918 static int wlc_nmode_validate(struct wlc_info *wlc, s32 nmode)
2920 int err = 0;
2922 switch (nmode) {
2924 case OFF:
2925 break;
2927 case AUTO:
2928 case WL_11N_2x2:
2929 case WL_11N_3x3:
2930 if (!(WLC_PHY_11N_CAP(wlc->band)))
2931 err = BCME_BADBAND;
2932 break;
2934 default:
2935 err = BCME_RANGE;
2936 break;
2939 return err;
2942 int wlc_set_nmode(struct wlc_info *wlc, s32 nmode)
2944 uint i;
2945 int err;
2947 err = wlc_nmode_validate(wlc, nmode);
2948 ASSERT(err == 0);
2949 if (err)
2950 return err;
2952 switch (nmode) {
2953 case OFF:
2954 wlc->pub->_n_enab = OFF;
2955 wlc->default_bss->flags &= ~WLC_BSS_HT;
2956 /* delete the mcs rates from the default and hw ratesets */
2957 wlc_rateset_mcs_clear(&wlc->default_bss->rateset);
2958 for (i = 0; i < NBANDS(wlc); i++) {
2959 memset(wlc->bandstate[i]->hw_rateset.mcs, 0,
2960 MCSSET_LEN);
2961 if (IS_MCS(wlc->band->rspec_override)) {
2962 wlc->bandstate[i]->rspec_override = 0;
2963 wlc_reprate_init(wlc);
2965 if (IS_MCS(wlc->band->mrspec_override))
2966 wlc->bandstate[i]->mrspec_override = 0;
2968 break;
2970 case AUTO:
2971 if (wlc->stf->txstreams == WL_11N_3x3)
2972 nmode = WL_11N_3x3;
2973 else
2974 nmode = WL_11N_2x2;
2975 case WL_11N_2x2:
2976 case WL_11N_3x3:
2977 ASSERT(WLC_PHY_11N_CAP(wlc->band));
2978 /* force GMODE_AUTO if NMODE is ON */
2979 wlc_set_gmode(wlc, GMODE_AUTO, true);
2980 if (nmode == WL_11N_3x3)
2981 wlc->pub->_n_enab = SUPPORT_HT;
2982 else
2983 wlc->pub->_n_enab = SUPPORT_11N;
2984 wlc->default_bss->flags |= WLC_BSS_HT;
2985 /* add the mcs rates to the default and hw ratesets */
2986 wlc_rateset_mcs_build(&wlc->default_bss->rateset,
2987 wlc->stf->txstreams);
2988 for (i = 0; i < NBANDS(wlc); i++)
2989 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
2990 wlc->default_bss->rateset.mcs, MCSSET_LEN);
2991 break;
2993 default:
2994 ASSERT(0);
2995 break;
2998 return err;
3001 static int wlc_set_rateset(struct wlc_info *wlc, wlc_rateset_t *rs_arg)
3003 wlc_rateset_t rs, new;
3004 uint bandunit;
3006 bcopy((char *)rs_arg, (char *)&rs, sizeof(wlc_rateset_t));
3008 /* check for bad count value */
3009 if ((rs.count == 0) || (rs.count > WLC_NUMRATES))
3010 return BCME_BADRATESET;
3012 /* try the current band */
3013 bandunit = wlc->band->bandunit;
3014 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3015 if (wlc_rate_hwrs_filter_sort_validate
3016 (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
3017 wlc->stf->txstreams))
3018 goto good;
3020 /* try the other band */
3021 if (IS_MBAND_UNLOCKED(wlc)) {
3022 bandunit = OTHERBANDUNIT(wlc);
3023 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3024 if (wlc_rate_hwrs_filter_sort_validate(&new,
3025 &wlc->
3026 bandstate[bandunit]->
3027 hw_rateset, true,
3028 wlc->stf->txstreams))
3029 goto good;
3032 return BCME_ERROR;
3034 good:
3035 /* apply new rateset */
3036 bcopy((char *)&new, (char *)&wlc->default_bss->rateset,
3037 sizeof(wlc_rateset_t));
3038 bcopy((char *)&new, (char *)&wlc->bandstate[bandunit]->defrateset,
3039 sizeof(wlc_rateset_t));
3040 return 0;
3043 /* simplified integer set interface for common ioctl handler */
3044 int wlc_set(struct wlc_info *wlc, int cmd, int arg)
3046 return wlc_ioctl(wlc, cmd, (void *)&arg, sizeof(arg), NULL);
3049 /* simplified integer get interface for common ioctl handler */
3050 int wlc_get(struct wlc_info *wlc, int cmd, int *arg)
3052 return wlc_ioctl(wlc, cmd, arg, sizeof(int), NULL);
3055 static void wlc_ofdm_rateset_war(struct wlc_info *wlc)
3057 u8 r;
3058 bool war = false;
3060 if (wlc->cfg->associated)
3061 r = wlc->cfg->current_bss->rateset.rates[0];
3062 else
3063 r = wlc->default_bss->rateset.rates[0];
3065 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
3067 return;
3071 wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
3072 struct wlc_if *wlcif)
3074 return _wlc_ioctl(wlc, cmd, arg, len, wlcif);
3077 /* common ioctl handler. return: 0=ok, -1=error, positive=particular error */
3078 static int
3079 _wlc_ioctl(struct wlc_info *wlc, int cmd, void *arg, int len,
3080 struct wlc_if *wlcif)
3082 int val, *pval;
3083 bool bool_val;
3084 int bcmerror;
3085 d11regs_t *regs;
3086 uint i;
3087 struct scb *nextscb;
3088 bool ta_ok;
3089 uint band;
3090 rw_reg_t *r;
3091 wlc_bsscfg_t *bsscfg;
3092 struct osl_info *osh;
3093 wlc_bss_info_t *current_bss;
3095 /* update bsscfg pointer */
3096 bsscfg = NULL; /* XXX: Hack bsscfg to be size one and use this globally */
3097 current_bss = NULL;
3099 /* initialize the following to get rid of compiler warning */
3100 nextscb = NULL;
3101 ta_ok = false;
3102 band = 0;
3103 r = NULL;
3105 /* If the device is turned off, then it's not "removed" */
3106 if (!wlc->pub->hw_off && DEVICEREMOVED(wlc)) {
3107 WL_ERROR("wl%d: %s: dead chip\n", wlc->pub->unit, __func__);
3108 wl_down(wlc->wl);
3109 return BCME_ERROR;
3112 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
3114 /* default argument is generic integer */
3115 pval = arg ? (int *)arg:NULL;
3117 /* This will prevent the misaligned access */
3118 if (pval && (u32) len >= sizeof(val))
3119 bcopy(pval, &val, sizeof(val));
3120 else
3121 val = 0;
3123 /* bool conversion to avoid duplication below */
3124 bool_val = val != 0;
3126 if (cmd != WLC_SET_CHANNEL)
3127 WL_NONE("WLC_IOCTL: cmd %d val 0x%x (%d) len %d\n",
3128 cmd, (uint)val, val, len);
3130 bcmerror = 0;
3131 regs = wlc->regs;
3132 osh = wlc->osh;
3134 /* A few commands don't need any arguments; all the others do. */
3135 switch (cmd) {
3136 case WLC_UP:
3137 case WLC_OUT:
3138 case WLC_DOWN:
3139 case WLC_DISASSOC:
3140 case WLC_RESTART:
3141 case WLC_REBOOT:
3142 case WLC_START_CHANNEL_QA:
3143 case WLC_INIT:
3144 break;
3146 default:
3147 if ((arg == NULL) || (len <= 0)) {
3148 WL_ERROR("wl%d: %s: Command %d needs arguments\n",
3149 wlc->pub->unit, __func__, cmd);
3150 bcmerror = BCME_BADARG;
3151 goto done;
3155 switch (cmd) {
3157 #if defined(BCMDBG)
3158 case WLC_GET_MSGLEVEL:
3159 *pval = wl_msg_level;
3160 break;
3162 case WLC_SET_MSGLEVEL:
3163 wl_msg_level = val;
3164 break;
3165 #endif
3167 case WLC_GET_INSTANCE:
3168 *pval = wlc->pub->unit;
3169 break;
3171 case WLC_GET_CHANNEL:{
3172 channel_info_t *ci = (channel_info_t *) arg;
3174 ASSERT(len > (int)sizeof(ci));
3176 ci->hw_channel =
3177 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC);
3178 ci->target_channel =
3179 CHSPEC_CHANNEL(wlc->default_bss->chanspec);
3180 ci->scan_channel = 0;
3182 break;
3185 case WLC_SET_CHANNEL:{
3186 chanspec_t chspec = CH20MHZ_CHSPEC(val);
3188 if (val < 0 || val > MAXCHANNEL) {
3189 bcmerror = BCME_OUTOFRANGECHAN;
3190 break;
3193 if (!wlc_valid_chanspec_db(wlc->cmi, chspec)) {
3194 bcmerror = BCME_BADCHAN;
3195 break;
3198 if (!wlc->pub->up && IS_MBAND_UNLOCKED(wlc)) {
3199 if (wlc->band->bandunit !=
3200 CHSPEC_WLCBANDUNIT(chspec))
3201 wlc->bandinit_pending = true;
3202 else
3203 wlc->bandinit_pending = false;
3206 wlc->default_bss->chanspec = chspec;
3207 /* wlc_BSSinit() will sanitize the rateset before using it.. */
3208 if (wlc->pub->up && !wlc->pub->associated &&
3209 (WLC_BAND_PI_RADIO_CHANSPEC != chspec)) {
3210 wlc_set_home_chanspec(wlc, chspec);
3211 wlc_suspend_mac_and_wait(wlc);
3212 wlc_set_chanspec(wlc, chspec);
3213 wlc_enable_mac(wlc);
3215 break;
3218 #if defined(BCMDBG)
3219 case WLC_GET_UCFLAGS:
3220 if (!wlc->pub->up) {
3221 bcmerror = BCME_NOTUP;
3222 break;
3225 /* optional band is stored in the second integer of incoming buffer */
3226 band =
3227 (len <
3228 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3230 /* bcmerror checking */
3231 bcmerror = wlc_iocregchk(wlc, band);
3232 if (bcmerror)
3233 break;
3235 if (val >= MHFMAX) {
3236 bcmerror = BCME_RANGE;
3237 break;
3240 *pval = wlc_bmac_mhf_get(wlc->hw, (u8) val, WLC_BAND_AUTO);
3241 break;
3243 case WLC_SET_UCFLAGS:
3244 if (!wlc->pub->up) {
3245 bcmerror = BCME_NOTUP;
3246 break;
3249 /* optional band is stored in the second integer of incoming buffer */
3250 band =
3251 (len <
3252 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3254 /* bcmerror checking */
3255 bcmerror = wlc_iocregchk(wlc, band);
3256 if (bcmerror)
3257 break;
3259 i = (u16) val;
3260 if (i >= MHFMAX) {
3261 bcmerror = BCME_RANGE;
3262 break;
3265 wlc_mhf(wlc, (u8) i, 0xffff, (u16) (val >> NBITS(u16)),
3266 WLC_BAND_AUTO);
3267 break;
3269 case WLC_GET_SHMEM:
3270 ta_ok = true;
3272 /* optional band is stored in the second integer of incoming buffer */
3273 band =
3274 (len <
3275 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3277 /* bcmerror checking */
3278 bcmerror = wlc_iocregchk(wlc, band);
3279 if (bcmerror)
3280 break;
3282 if (val & 1) {
3283 bcmerror = BCME_BADADDR;
3284 break;
3287 *pval = wlc_read_shm(wlc, (u16) val);
3288 break;
3290 case WLC_SET_SHMEM:
3291 ta_ok = true;
3293 /* optional band is stored in the second integer of incoming buffer */
3294 band =
3295 (len <
3296 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3298 /* bcmerror checking */
3299 bcmerror = wlc_iocregchk(wlc, band);
3300 if (bcmerror)
3301 break;
3303 if (val & 1) {
3304 bcmerror = BCME_BADADDR;
3305 break;
3308 wlc_write_shm(wlc, (u16) val,
3309 (u16) (val >> NBITS(u16)));
3310 break;
3312 case WLC_R_REG: /* MAC registers */
3313 ta_ok = true;
3314 r = (rw_reg_t *) arg;
3315 band = WLC_BAND_AUTO;
3317 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3318 bcmerror = BCME_BUFTOOSHORT;
3319 break;
3322 if (len >= (int)sizeof(rw_reg_t))
3323 band = r->band;
3325 /* bcmerror checking */
3326 bcmerror = wlc_iocregchk(wlc, band);
3327 if (bcmerror)
3328 break;
3330 if ((r->byteoff + r->size) > sizeof(d11regs_t)) {
3331 bcmerror = BCME_BADADDR;
3332 break;
3334 if (r->size == sizeof(u32))
3335 r->val =
3336 R_REG(osh,
3337 (u32 *)((unsigned char *)(unsigned long)regs +
3338 r->byteoff));
3339 else if (r->size == sizeof(u16))
3340 r->val =
3341 R_REG(osh,
3342 (u16 *)((unsigned char *)(unsigned long)regs +
3343 r->byteoff));
3344 else
3345 bcmerror = BCME_BADADDR;
3346 break;
3348 case WLC_W_REG:
3349 ta_ok = true;
3350 r = (rw_reg_t *) arg;
3351 band = WLC_BAND_AUTO;
3353 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3354 bcmerror = BCME_BUFTOOSHORT;
3355 break;
3358 if (len >= (int)sizeof(rw_reg_t))
3359 band = r->band;
3361 /* bcmerror checking */
3362 bcmerror = wlc_iocregchk(wlc, band);
3363 if (bcmerror)
3364 break;
3366 if (r->byteoff + r->size > sizeof(d11regs_t)) {
3367 bcmerror = BCME_BADADDR;
3368 break;
3370 if (r->size == sizeof(u32))
3371 W_REG(osh,
3372 (u32 *)((unsigned char *)(unsigned long) regs +
3373 r->byteoff), r->val);
3374 else if (r->size == sizeof(u16))
3375 W_REG(osh,
3376 (u16 *)((unsigned char *)(unsigned long) regs +
3377 r->byteoff), r->val);
3378 else
3379 bcmerror = BCME_BADADDR;
3380 break;
3381 #endif /* BCMDBG */
3383 case WLC_GET_TXANT:
3384 *pval = wlc->stf->txant;
3385 break;
3387 case WLC_SET_TXANT:
3388 bcmerror = wlc_stf_ant_txant_validate(wlc, (s8) val);
3389 if (bcmerror < 0)
3390 break;
3392 wlc->stf->txant = (s8) val;
3394 /* if down, we are done */
3395 if (!wlc->pub->up)
3396 break;
3398 wlc_suspend_mac_and_wait(wlc);
3400 wlc_stf_phy_txant_upd(wlc);
3401 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
3403 wlc_enable_mac(wlc);
3405 break;
3407 case WLC_GET_ANTDIV:{
3408 u8 phy_antdiv;
3410 /* return configured value if core is down */
3411 if (!wlc->pub->up) {
3412 *pval = wlc->stf->ant_rx_ovr;
3414 } else {
3415 if (wlc_phy_ant_rxdiv_get
3416 (wlc->band->pi, &phy_antdiv))
3417 *pval = (int)phy_antdiv;
3418 else
3419 *pval = (int)wlc->stf->ant_rx_ovr;
3422 break;
3424 case WLC_SET_ANTDIV:
3425 /* values are -1=driver default, 0=force0, 1=force1, 2=start1, 3=start0 */
3426 if ((val < -1) || (val > 3)) {
3427 bcmerror = BCME_RANGE;
3428 break;
3431 if (val == -1)
3432 val = ANT_RX_DIV_DEF;
3434 wlc->stf->ant_rx_ovr = (u8) val;
3435 wlc_phy_ant_rxdiv_set(wlc->band->pi, (u8) val);
3436 break;
3438 case WLC_GET_RX_ANT:{ /* get latest used rx antenna */
3439 u16 rxstatus;
3441 if (!wlc->pub->up) {
3442 bcmerror = BCME_NOTUP;
3443 break;
3446 rxstatus = R_REG(wlc->osh, &wlc->regs->phyrxstatus0);
3447 if (rxstatus == 0xdead || rxstatus == (u16) -1) {
3448 bcmerror = BCME_ERROR;
3449 break;
3451 *pval = (rxstatus & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
3452 break;
3455 #if defined(BCMDBG)
3456 case WLC_GET_UCANTDIV:
3457 if (!wlc->clk) {
3458 bcmerror = BCME_NOCLK;
3459 break;
3462 *pval =
3463 (wlc_bmac_mhf_get(wlc->hw, MHF1, WLC_BAND_AUTO) &
3464 MHF1_ANTDIV);
3465 break;
3467 case WLC_SET_UCANTDIV:{
3468 if (!wlc->pub->up) {
3469 bcmerror = BCME_NOTUP;
3470 break;
3473 /* if multiband, band must be locked */
3474 if (IS_MBAND_UNLOCKED(wlc)) {
3475 bcmerror = BCME_NOTBANDLOCKED;
3476 break;
3479 /* 4322 supports antdiv in phy, no need to set it to ucode */
3480 if (WLCISNPHY(wlc->band)
3481 && D11REV_IS(wlc->pub->corerev, 16)) {
3482 WL_ERROR("wl%d: can't set ucantdiv for 4322\n",
3483 wlc->pub->unit);
3484 bcmerror = BCME_UNSUPPORTED;
3485 } else
3486 wlc_mhf(wlc, MHF1, MHF1_ANTDIV,
3487 (val ? MHF1_ANTDIV : 0), WLC_BAND_AUTO);
3488 break;
3490 #endif /* defined(BCMDBG) */
3492 case WLC_GET_SRL:
3493 *pval = wlc->SRL;
3494 break;
3496 case WLC_SET_SRL:
3497 if (val >= 1 && val <= RETRY_SHORT_MAX) {
3498 int ac;
3499 wlc->SRL = (u16) val;
3501 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3503 for (ac = 0; ac < AC_COUNT; ac++) {
3504 WLC_WME_RETRY_SHORT_SET(wlc, ac, wlc->SRL);
3506 wlc_wme_retries_write(wlc);
3507 } else
3508 bcmerror = BCME_RANGE;
3509 break;
3511 case WLC_GET_LRL:
3512 *pval = wlc->LRL;
3513 break;
3515 case WLC_SET_LRL:
3516 if (val >= 1 && val <= 255) {
3517 int ac;
3518 wlc->LRL = (u16) val;
3520 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3522 for (ac = 0; ac < AC_COUNT; ac++) {
3523 WLC_WME_RETRY_LONG_SET(wlc, ac, wlc->LRL);
3525 wlc_wme_retries_write(wlc);
3526 } else
3527 bcmerror = BCME_RANGE;
3528 break;
3530 case WLC_GET_CWMIN:
3531 *pval = wlc->band->CWmin;
3532 break;
3534 case WLC_SET_CWMIN:
3535 if (!wlc->clk) {
3536 bcmerror = BCME_NOCLK;
3537 break;
3540 if (val >= 1 && val <= 255) {
3541 wlc_set_cwmin(wlc, (u16) val);
3542 } else
3543 bcmerror = BCME_RANGE;
3544 break;
3546 case WLC_GET_CWMAX:
3547 *pval = wlc->band->CWmax;
3548 break;
3550 case WLC_SET_CWMAX:
3551 if (!wlc->clk) {
3552 bcmerror = BCME_NOCLK;
3553 break;
3556 if (val >= 255 && val <= 2047) {
3557 wlc_set_cwmax(wlc, (u16) val);
3558 } else
3559 bcmerror = BCME_RANGE;
3560 break;
3562 case WLC_GET_RADIO: /* use mask if don't want to expose some internal bits */
3563 *pval = wlc->pub->radio_disabled;
3564 break;
3566 case WLC_SET_RADIO:{ /* 32 bits input, higher 16 bits are mask, lower 16 bits are value to
3567 * set
3569 u16 radiomask, radioval;
3570 uint validbits =
3571 WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE;
3572 mbool new = 0;
3574 radiomask = (val & 0xffff0000) >> 16;
3575 radioval = val & 0x0000ffff;
3577 if ((radiomask == 0) || (radiomask & ~validbits)
3578 || (radioval & ~validbits)
3579 || ((radioval & ~radiomask) != 0)) {
3580 WL_ERROR("SET_RADIO with wrong bits 0x%x\n",
3581 val);
3582 bcmerror = BCME_RANGE;
3583 break;
3586 new =
3587 (wlc->pub->radio_disabled & ~radiomask) | radioval;
3588 wlc->pub->radio_disabled = new;
3590 wlc_radio_hwdisable_upd(wlc);
3591 wlc_radio_upd(wlc);
3592 break;
3595 case WLC_GET_PHYTYPE:
3596 *pval = WLC_PHYTYPE(wlc->band->phytype);
3597 break;
3599 #if defined(BCMDBG)
3600 case WLC_GET_KEY:
3601 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc))) {
3602 wl_wsec_key_t key;
3604 wsec_key_t *src_key = wlc->wsec_keys[val];
3606 if (len < (int)sizeof(key)) {
3607 bcmerror = BCME_BUFTOOSHORT;
3608 break;
3611 memset((char *)&key, 0, sizeof(key));
3612 if (src_key) {
3613 key.index = src_key->id;
3614 key.len = src_key->len;
3615 bcopy(src_key->data, key.data, key.len);
3616 key.algo = src_key->algo;
3617 if (WSEC_SOFTKEY(wlc, src_key, bsscfg))
3618 key.flags |= WL_SOFT_KEY;
3619 if (src_key->flags & WSEC_PRIMARY_KEY)
3620 key.flags |= WL_PRIMARY_KEY;
3622 bcopy(src_key->ea, key.ea,
3623 ETH_ALEN);
3626 bcopy((char *)&key, arg, sizeof(key));
3627 } else
3628 bcmerror = BCME_BADKEYIDX;
3629 break;
3630 #endif /* defined(BCMDBG) */
3632 case WLC_SET_KEY:
3633 bcmerror =
3634 wlc_iovar_op(wlc, "wsec_key", NULL, 0, arg, len, IOV_SET,
3635 wlcif);
3636 break;
3638 case WLC_GET_KEY_SEQ:{
3639 wsec_key_t *key;
3641 if (len < DOT11_WPA_KEY_RSC_LEN) {
3642 bcmerror = BCME_BUFTOOSHORT;
3643 break;
3646 /* Return the key's tx iv as an EAPOL sequence counter.
3647 * This will be used to supply the RSC value to a supplicant.
3648 * The format is 8 bytes, with least significant in seq[0].
3651 key = WSEC_KEY(wlc, val);
3652 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc)) &&
3653 (key != NULL)) {
3654 u8 seq[DOT11_WPA_KEY_RSC_LEN];
3655 u16 lo;
3656 u32 hi;
3657 /* group keys in WPA-NONE (IBSS only, AES and TKIP) use a global TXIV */
3658 if ((bsscfg->WPA_auth & WPA_AUTH_NONE) &&
3659 is_zero_ether_addr(key->ea)) {
3660 lo = bsscfg->wpa_none_txiv.lo;
3661 hi = bsscfg->wpa_none_txiv.hi;
3662 } else {
3663 lo = key->txiv.lo;
3664 hi = key->txiv.hi;
3667 /* format the buffer, low to high */
3668 seq[0] = lo & 0xff;
3669 seq[1] = (lo >> 8) & 0xff;
3670 seq[2] = hi & 0xff;
3671 seq[3] = (hi >> 8) & 0xff;
3672 seq[4] = (hi >> 16) & 0xff;
3673 seq[5] = (hi >> 24) & 0xff;
3674 seq[6] = 0;
3675 seq[7] = 0;
3677 bcopy((char *)seq, arg, sizeof(seq));
3678 } else {
3679 bcmerror = BCME_BADKEYIDX;
3681 break;
3684 case WLC_GET_CURR_RATESET:{
3685 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3686 wlc_rateset_t *rs;
3688 if (bsscfg->associated)
3689 rs = &current_bss->rateset;
3690 else
3691 rs = &wlc->default_bss->rateset;
3693 if (len < (int)(rs->count + sizeof(rs->count))) {
3694 bcmerror = BCME_BUFTOOSHORT;
3695 break;
3698 /* Copy only legacy rateset section */
3699 ret_rs->count = rs->count;
3700 bcopy(&rs->rates, &ret_rs->rates, rs->count);
3701 break;
3704 case WLC_GET_RATESET:{
3705 wlc_rateset_t rs;
3706 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3708 memset(&rs, 0, sizeof(wlc_rateset_t));
3709 wlc_default_rateset(wlc, (wlc_rateset_t *) &rs);
3711 if (len < (int)(rs.count + sizeof(rs.count))) {
3712 bcmerror = BCME_BUFTOOSHORT;
3713 break;
3716 /* Copy only legacy rateset section */
3717 ret_rs->count = rs.count;
3718 bcopy(&rs.rates, &ret_rs->rates, rs.count);
3719 break;
3722 case WLC_SET_RATESET:{
3723 wlc_rateset_t rs;
3724 wl_rateset_t *in_rs = (wl_rateset_t *) arg;
3726 if (len < (int)(in_rs->count + sizeof(in_rs->count))) {
3727 bcmerror = BCME_BUFTOOSHORT;
3728 break;
3731 if (in_rs->count > WLC_NUMRATES) {
3732 bcmerror = BCME_BUFTOOLONG;
3733 break;
3736 memset(&rs, 0, sizeof(wlc_rateset_t));
3738 /* Copy only legacy rateset section */
3739 rs.count = in_rs->count;
3740 bcopy(&in_rs->rates, &rs.rates, rs.count);
3742 /* merge rateset coming in with the current mcsset */
3743 if (N_ENAB(wlc->pub)) {
3744 if (bsscfg->associated)
3745 bcopy(&current_bss->rateset.mcs[0],
3746 rs.mcs, MCSSET_LEN);
3747 else
3748 bcopy(&wlc->default_bss->rateset.mcs[0],
3749 rs.mcs, MCSSET_LEN);
3752 bcmerror = wlc_set_rateset(wlc, &rs);
3754 if (!bcmerror)
3755 wlc_ofdm_rateset_war(wlc);
3757 break;
3760 case WLC_GET_BCNPRD:
3761 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3762 *pval = current_bss->beacon_period;
3763 else
3764 *pval = wlc->default_bss->beacon_period;
3765 break;
3767 case WLC_SET_BCNPRD:
3768 /* range [1, 0xffff] */
3769 if (val >= DOT11_MIN_BEACON_PERIOD
3770 && val <= DOT11_MAX_BEACON_PERIOD) {
3771 wlc->default_bss->beacon_period = (u16) val;
3772 } else
3773 bcmerror = BCME_RANGE;
3774 break;
3776 case WLC_GET_DTIMPRD:
3777 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3778 *pval = current_bss->dtim_period;
3779 else
3780 *pval = wlc->default_bss->dtim_period;
3781 break;
3783 case WLC_SET_DTIMPRD:
3784 /* range [1, 0xff] */
3785 if (val >= DOT11_MIN_DTIM_PERIOD
3786 && val <= DOT11_MAX_DTIM_PERIOD) {
3787 wlc->default_bss->dtim_period = (u8) val;
3788 } else
3789 bcmerror = BCME_RANGE;
3790 break;
3792 #ifdef SUPPORT_PS
3793 case WLC_GET_PM:
3794 *pval = wlc->PM;
3795 break;
3797 case WLC_SET_PM:
3798 if ((val >= PM_OFF) && (val <= PM_MAX)) {
3799 wlc->PM = (u8) val;
3800 if (wlc->pub->up) {
3802 /* Change watchdog driver to align watchdog with tbtt if possible */
3803 wlc_watchdog_upd(wlc, PS_ALLOWED(wlc));
3804 } else
3805 bcmerror = BCME_ERROR;
3806 break;
3807 #endif /* SUPPORT_PS */
3809 #ifdef SUPPORT_PS
3810 #ifdef BCMDBG
3811 case WLC_GET_WAKE:
3812 if (AP_ENAB(wlc->pub)) {
3813 bcmerror = BCME_NOTSTA;
3814 break;
3816 *pval = wlc->wake;
3817 break;
3819 case WLC_SET_WAKE:
3820 if (AP_ENAB(wlc->pub)) {
3821 bcmerror = BCME_NOTSTA;
3822 break;
3825 wlc->wake = val ? true : false;
3827 /* if down, we're done */
3828 if (!wlc->pub->up)
3829 break;
3831 /* apply to the mac */
3832 wlc_set_ps_ctrl(wlc);
3833 break;
3834 #endif /* BCMDBG */
3835 #endif /* SUPPORT_PS */
3837 case WLC_GET_REVINFO:
3838 bcmerror = wlc_get_revision_info(wlc, arg, (uint) len);
3839 break;
3841 case WLC_GET_AP:
3842 *pval = (int)AP_ENAB(wlc->pub);
3843 break;
3845 case WLC_GET_ATIM:
3846 if (bsscfg->associated)
3847 *pval = (int)current_bss->atim_window;
3848 else
3849 *pval = (int)wlc->default_bss->atim_window;
3850 break;
3852 case WLC_SET_ATIM:
3853 wlc->default_bss->atim_window = (u32) val;
3854 break;
3856 case WLC_GET_PKTCNTS:{
3857 get_pktcnt_t *pktcnt = (get_pktcnt_t *) pval;
3858 if (WLC_UPDATE_STATS(wlc))
3859 wlc_statsupd(wlc);
3860 pktcnt->rx_good_pkt = WLCNTVAL(wlc->pub->_cnt->rxframe);
3861 pktcnt->rx_bad_pkt = WLCNTVAL(wlc->pub->_cnt->rxerror);
3862 pktcnt->tx_good_pkt =
3863 WLCNTVAL(wlc->pub->_cnt->txfrmsnt);
3864 pktcnt->tx_bad_pkt =
3865 WLCNTVAL(wlc->pub->_cnt->txerror) +
3866 WLCNTVAL(wlc->pub->_cnt->txfail);
3867 if (len >= (int)sizeof(get_pktcnt_t)) {
3868 /* Be backward compatible - only if buffer is large enough */
3869 pktcnt->rx_ocast_good_pkt =
3870 WLCNTVAL(wlc->pub->_cnt->rxmfrmocast);
3872 break;
3875 #ifdef SUPPORT_HWKEY
3876 case WLC_GET_WSEC:
3877 bcmerror =
3878 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_GET,
3879 wlcif);
3880 break;
3882 case WLC_SET_WSEC:
3883 bcmerror =
3884 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_SET,
3885 wlcif);
3886 break;
3888 case WLC_GET_WPA_AUTH:
3889 *pval = (int)bsscfg->WPA_auth;
3890 break;
3892 case WLC_SET_WPA_AUTH:
3893 /* change of WPA_Auth modifies the PS_ALLOWED state */
3894 if (BSSCFG_STA(bsscfg)) {
3895 bsscfg->WPA_auth = (u16) val;
3896 } else
3897 bsscfg->WPA_auth = (u16) val;
3898 break;
3899 #endif /* SUPPORT_HWKEY */
3901 case WLC_GET_BANDLIST:
3902 /* count of number of bands, followed by each band type */
3903 *pval++ = NBANDS(wlc);
3904 *pval++ = wlc->band->bandtype;
3905 if (NBANDS(wlc) > 1)
3906 *pval++ = wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype;
3907 break;
3909 case WLC_GET_BAND:
3910 *pval = wlc->bandlocked ? wlc->band->bandtype : WLC_BAND_AUTO;
3911 break;
3913 case WLC_GET_PHYLIST:
3915 unsigned char *cp = arg;
3916 if (len < 3) {
3917 bcmerror = BCME_BUFTOOSHORT;
3918 break;
3921 if (WLCISNPHY(wlc->band)) {
3922 *cp++ = 'n';
3923 } else if (WLCISLCNPHY(wlc->band)) {
3924 *cp++ = 'c';
3925 } else if (WLCISSSLPNPHY(wlc->band)) {
3926 *cp++ = 's';
3928 *cp = '\0';
3929 break;
3932 case WLC_GET_SHORTSLOT:
3933 *pval = wlc->shortslot;
3934 break;
3936 case WLC_GET_SHORTSLOT_OVERRIDE:
3937 *pval = wlc->shortslot_override;
3938 break;
3940 case WLC_SET_SHORTSLOT_OVERRIDE:
3941 if ((val != WLC_SHORTSLOT_AUTO) &&
3942 (val != WLC_SHORTSLOT_OFF) && (val != WLC_SHORTSLOT_ON)) {
3943 bcmerror = BCME_RANGE;
3944 break;
3947 wlc->shortslot_override = (s8) val;
3949 /* shortslot is an 11g feature, so no more work if we are
3950 * currently on the 5G band
3952 if (BAND_5G(wlc->band->bandtype))
3953 break;
3955 if (wlc->pub->up && wlc->pub->associated) {
3956 /* let watchdog or beacon processing update shortslot */
3957 } else if (wlc->pub->up) {
3958 /* unassociated shortslot is off */
3959 wlc_switch_shortslot(wlc, false);
3960 } else {
3961 /* driver is down, so just update the wlc_info value */
3962 if (wlc->shortslot_override == WLC_SHORTSLOT_AUTO) {
3963 wlc->shortslot = false;
3964 } else {
3965 wlc->shortslot =
3966 (wlc->shortslot_override ==
3967 WLC_SHORTSLOT_ON);
3971 break;
3973 case WLC_GET_LEGACY_ERP:
3974 *pval = wlc->include_legacy_erp;
3975 break;
3977 case WLC_SET_LEGACY_ERP:
3978 if (wlc->include_legacy_erp == bool_val)
3979 break;
3981 wlc->include_legacy_erp = bool_val;
3983 if (AP_ENAB(wlc->pub) && wlc->clk) {
3984 wlc_update_beacon(wlc);
3985 wlc_update_probe_resp(wlc, true);
3987 break;
3989 case WLC_GET_GMODE:
3990 if (wlc->band->bandtype == WLC_BAND_2G)
3991 *pval = wlc->band->gmode;
3992 else if (NBANDS(wlc) > 1)
3993 *pval = wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode;
3994 break;
3996 case WLC_SET_GMODE:
3997 if (!wlc->pub->associated)
3998 bcmerror = wlc_set_gmode(wlc, (u8) val, true);
3999 else {
4000 bcmerror = BCME_ASSOCIATED;
4001 break;
4003 break;
4005 case WLC_GET_GMODE_PROTECTION:
4006 *pval = wlc->protection->_g;
4007 break;
4009 case WLC_GET_PROTECTION_CONTROL:
4010 *pval = wlc->protection->overlap;
4011 break;
4013 case WLC_SET_PROTECTION_CONTROL:
4014 if ((val != WLC_PROTECTION_CTL_OFF) &&
4015 (val != WLC_PROTECTION_CTL_LOCAL) &&
4016 (val != WLC_PROTECTION_CTL_OVERLAP)) {
4017 bcmerror = BCME_RANGE;
4018 break;
4021 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, (s8) val);
4023 /* Current g_protection will sync up to the specified control alg in watchdog
4024 * if the driver is up and associated.
4025 * If the driver is down or not associated, the control setting has no effect.
4027 break;
4029 case WLC_GET_GMODE_PROTECTION_OVERRIDE:
4030 *pval = wlc->protection->g_override;
4031 break;
4033 case WLC_SET_GMODE_PROTECTION_OVERRIDE:
4034 if ((val != WLC_PROTECTION_AUTO) &&
4035 (val != WLC_PROTECTION_OFF) && (val != WLC_PROTECTION_ON)) {
4036 bcmerror = BCME_RANGE;
4037 break;
4040 wlc_protection_upd(wlc, WLC_PROT_G_OVR, (s8) val);
4042 break;
4044 case WLC_SET_SUP_RATESET_OVERRIDE:{
4045 wlc_rateset_t rs, new;
4047 /* copyin */
4048 if (len < (int)sizeof(wlc_rateset_t)) {
4049 bcmerror = BCME_BUFTOOSHORT;
4050 break;
4052 bcopy((char *)arg, (char *)&rs, sizeof(wlc_rateset_t));
4054 /* check for bad count value */
4055 if (rs.count > WLC_NUMRATES) {
4056 bcmerror = BCME_BADRATESET; /* invalid rateset */
4057 break;
4060 /* this command is only appropriate for gmode operation */
4061 if (!(wlc->band->gmode ||
4062 ((NBANDS(wlc) > 1)
4063 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4064 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4065 break;
4068 /* check for an empty rateset to clear the override */
4069 if (rs.count == 0) {
4070 memset(&wlc->sup_rates_override, 0,
4071 sizeof(wlc_rateset_t));
4072 break;
4075 /* validate rateset by comparing pre and post sorted against 11g hw rates */
4076 wlc_rateset_filter(&rs, &new, false, WLC_RATES_CCK_OFDM,
4077 RATE_MASK, BSS_N_ENAB(wlc, bsscfg));
4078 wlc_rate_hwrs_filter_sort_validate(&new,
4079 &cck_ofdm_rates,
4080 false,
4081 wlc->stf->txstreams);
4082 if (rs.count != new.count) {
4083 bcmerror = BCME_BADRATESET; /* invalid rateset */
4084 break;
4087 /* apply new rateset to the override */
4088 bcopy((char *)&new, (char *)&wlc->sup_rates_override,
4089 sizeof(wlc_rateset_t));
4091 /* update bcn and probe resp if needed */
4092 if (wlc->pub->up && AP_ENAB(wlc->pub)
4093 && wlc->pub->associated) {
4094 wlc_update_beacon(wlc);
4095 wlc_update_probe_resp(wlc, true);
4097 break;
4100 case WLC_GET_SUP_RATESET_OVERRIDE:
4101 /* this command is only appropriate for gmode operation */
4102 if (!(wlc->band->gmode ||
4103 ((NBANDS(wlc) > 1)
4104 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4105 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4106 break;
4108 if (len < (int)sizeof(wlc_rateset_t)) {
4109 bcmerror = BCME_BUFTOOSHORT;
4110 break;
4112 bcopy((char *)&wlc->sup_rates_override, (char *)arg,
4113 sizeof(wlc_rateset_t));
4115 break;
4117 case WLC_GET_PRB_RESP_TIMEOUT:
4118 *pval = wlc->prb_resp_timeout;
4119 break;
4121 case WLC_SET_PRB_RESP_TIMEOUT:
4122 if (wlc->pub->up) {
4123 bcmerror = BCME_NOTDOWN;
4124 break;
4126 if (val < 0 || val >= 0xFFFF) {
4127 bcmerror = BCME_RANGE; /* bad value */
4128 break;
4130 wlc->prb_resp_timeout = (u16) val;
4131 break;
4133 case WLC_GET_KEY_PRIMARY:{
4134 wsec_key_t *key;
4136 /* treat the 'val' parm as the key id */
4137 key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4138 if (key != NULL) {
4139 *pval = key->id == val ? true : false;
4140 } else {
4141 bcmerror = BCME_BADKEYIDX;
4143 break;
4146 case WLC_SET_KEY_PRIMARY:{
4147 wsec_key_t *key, *old_key;
4149 bcmerror = BCME_BADKEYIDX;
4151 /* treat the 'val' parm as the key id */
4152 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
4153 key = bsscfg->bss_def_keys[i];
4154 if (key != NULL && key->id == val) {
4155 old_key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4156 if (old_key != NULL)
4157 old_key->flags &=
4158 ~WSEC_PRIMARY_KEY;
4159 key->flags |= WSEC_PRIMARY_KEY;
4160 bsscfg->wsec_index = i;
4161 bcmerror = BCME_OK;
4164 break;
4167 #ifdef BCMDBG
4168 case WLC_INIT:
4169 wl_init(wlc->wl);
4170 break;
4171 #endif
4173 case WLC_SET_VAR:
4174 case WLC_GET_VAR:{
4175 char *name;
4176 /* validate the name value */
4177 name = (char *)arg;
4178 for (i = 0; i < (uint) len && *name != '\0';
4179 i++, name++)
4182 if (i == (uint) len) {
4183 bcmerror = BCME_BUFTOOSHORT;
4184 break;
4186 i++; /* include the null in the string length */
4188 if (cmd == WLC_GET_VAR) {
4189 bcmerror =
4190 wlc_iovar_op(wlc, arg,
4191 (void *)((s8 *) arg + i),
4192 len - i, arg, len, IOV_GET,
4193 wlcif);
4194 } else
4195 bcmerror =
4196 wlc_iovar_op(wlc, arg, NULL, 0,
4197 (void *)((s8 *) arg + i),
4198 len - i, IOV_SET, wlcif);
4200 break;
4203 case WLC_SET_WSEC_PMK:
4204 bcmerror = BCME_UNSUPPORTED;
4205 break;
4207 #if defined(BCMDBG)
4208 case WLC_CURRENT_PWR:
4209 if (!wlc->pub->up)
4210 bcmerror = BCME_NOTUP;
4211 else
4212 bcmerror = wlc_get_current_txpwr(wlc, arg, len);
4213 break;
4214 #endif
4216 case WLC_LAST:
4217 WL_ERROR("%s: WLC_LAST\n", __func__);
4219 done:
4221 if (bcmerror) {
4222 if (VALID_BCMERROR(bcmerror))
4223 wlc->pub->bcmerror = bcmerror;
4224 else {
4225 bcmerror = 0;
4229 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
4230 /* In hw_off condition, IOCTLs that reach here are deemed safe but taclear would
4231 * certainly result in getting -1 for register reads. So skip ta_clear altogether
4233 if (!(wlc->pub->hw_off))
4234 ASSERT(wlc_bmac_taclear(wlc->hw, ta_ok) || !ta_ok);
4236 return bcmerror;
4239 #if defined(BCMDBG)
4240 /* consolidated register access ioctl error checking */
4241 int wlc_iocregchk(struct wlc_info *wlc, uint band)
4243 /* if band is specified, it must be the current band */
4244 if ((band != WLC_BAND_AUTO) && (band != (uint) wlc->band->bandtype))
4245 return BCME_BADBAND;
4247 /* if multiband and band is not specified, band must be locked */
4248 if ((band == WLC_BAND_AUTO) && IS_MBAND_UNLOCKED(wlc))
4249 return BCME_NOTBANDLOCKED;
4251 /* must have core clocks */
4252 if (!wlc->clk)
4253 return BCME_NOCLK;
4255 return 0;
4257 #endif /* defined(BCMDBG) */
4259 #if defined(BCMDBG)
4260 /* For some ioctls, make sure that the pi pointer matches the current phy */
4261 int wlc_iocpichk(struct wlc_info *wlc, uint phytype)
4263 if (wlc->band->phytype != phytype)
4264 return BCME_BADBAND;
4265 return 0;
4267 #endif
4269 /* Look up the given var name in the given table */
4270 static const bcm_iovar_t *wlc_iovar_lookup(const bcm_iovar_t *table,
4271 const char *name)
4273 const bcm_iovar_t *vi;
4274 const char *lookup_name;
4276 /* skip any ':' delimited option prefixes */
4277 lookup_name = strrchr(name, ':');
4278 if (lookup_name != NULL)
4279 lookup_name++;
4280 else
4281 lookup_name = name;
4283 ASSERT(table != NULL);
4285 for (vi = table; vi->name; vi++) {
4286 if (!strcmp(vi->name, lookup_name))
4287 return vi;
4289 /* ran to end of table */
4291 return NULL; /* var name not found */
4294 /* simplified integer get interface for common WLC_GET_VAR ioctl handler */
4295 int wlc_iovar_getint(struct wlc_info *wlc, const char *name, int *arg)
4297 return wlc_iovar_op(wlc, name, NULL, 0, arg, sizeof(s32), IOV_GET,
4298 NULL);
4301 /* simplified integer set interface for common WLC_SET_VAR ioctl handler */
4302 int wlc_iovar_setint(struct wlc_info *wlc, const char *name, int arg)
4304 return wlc_iovar_op(wlc, name, NULL, 0, (void *)&arg, sizeof(arg),
4305 IOV_SET, NULL);
4308 /* simplified s8 get interface for common WLC_GET_VAR ioctl handler */
4309 int wlc_iovar_gets8(struct wlc_info *wlc, const char *name, s8 *arg)
4311 int iovar_int;
4312 int err;
4314 err =
4315 wlc_iovar_op(wlc, name, NULL, 0, &iovar_int, sizeof(iovar_int),
4316 IOV_GET, NULL);
4317 if (!err)
4318 *arg = (s8) iovar_int;
4320 return err;
4324 * register iovar table, watchdog and down handlers.
4325 * calling function must keep 'iovars' until wlc_module_unregister is called.
4326 * 'iovar' must have the last entry's name field being NULL as terminator.
4328 int wlc_module_register(struct wlc_pub *pub, const bcm_iovar_t *iovars,
4329 const char *name, void *hdl, iovar_fn_t i_fn,
4330 watchdog_fn_t w_fn, down_fn_t d_fn)
4332 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
4333 int i;
4335 ASSERT(name != NULL);
4336 ASSERT(i_fn != NULL || w_fn != NULL || d_fn != NULL);
4338 /* find an empty entry and just add, no duplication check! */
4339 for (i = 0; i < WLC_MAXMODULES; i++) {
4340 if (wlc->modulecb[i].name[0] == '\0') {
4341 strncpy(wlc->modulecb[i].name, name,
4342 sizeof(wlc->modulecb[i].name) - 1);
4343 wlc->modulecb[i].iovars = iovars;
4344 wlc->modulecb[i].hdl = hdl;
4345 wlc->modulecb[i].iovar_fn = i_fn;
4346 wlc->modulecb[i].watchdog_fn = w_fn;
4347 wlc->modulecb[i].down_fn = d_fn;
4348 return 0;
4352 /* it is time to increase the capacity */
4353 ASSERT(i < WLC_MAXMODULES);
4354 return BCME_NORESOURCE;
4357 /* unregister module callbacks */
4358 int wlc_module_unregister(struct wlc_pub *pub, const char *name, void *hdl)
4360 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
4361 int i;
4363 if (wlc == NULL)
4364 return BCME_NOTFOUND;
4366 ASSERT(name != NULL);
4368 for (i = 0; i < WLC_MAXMODULES; i++) {
4369 if (!strcmp(wlc->modulecb[i].name, name) &&
4370 (wlc->modulecb[i].hdl == hdl)) {
4371 memset(&wlc->modulecb[i], 0, sizeof(modulecb_t));
4372 return 0;
4376 /* table not found! */
4377 return BCME_NOTFOUND;
4380 /* Write WME tunable parameters for retransmit/max rate from wlc struct to ucode */
4381 static void wlc_wme_retries_write(struct wlc_info *wlc)
4383 int ac;
4385 /* Need clock to do this */
4386 if (!wlc->clk)
4387 return;
4389 for (ac = 0; ac < AC_COUNT; ac++) {
4390 wlc_write_shm(wlc, M_AC_TXLMT_ADDR(ac), wlc->wme_retries[ac]);
4394 /* Get or set an iovar. The params/p_len pair specifies any additional
4395 * qualifying parameters (e.g. an "element index") for a get, while the
4396 * arg/len pair is the buffer for the value to be set or retrieved.
4397 * Operation (get/set) is specified by the last argument.
4398 * interface context provided by wlcif
4400 * All pointers may point into the same buffer.
4403 wlc_iovar_op(struct wlc_info *wlc, const char *name,
4404 void *params, int p_len, void *arg, int len,
4405 bool set, struct wlc_if *wlcif)
4407 int err = 0;
4408 int val_size;
4409 const bcm_iovar_t *vi = NULL;
4410 u32 actionid;
4411 int i;
4413 ASSERT(name != NULL);
4415 ASSERT(len >= 0);
4417 /* Get MUST have return space */
4418 ASSERT(set || (arg && len));
4420 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
4422 /* Set does NOT take qualifiers */
4423 ASSERT(!set || (!params && !p_len));
4425 if (!set && (len == sizeof(int)) &&
4426 !(IS_ALIGNED((unsigned long)(arg), (uint) sizeof(int)))) {
4427 WL_ERROR("wl%d: %s unaligned get ptr for %s\n",
4428 wlc->pub->unit, __func__, name);
4429 ASSERT(0);
4432 /* find the given iovar name */
4433 for (i = 0; i < WLC_MAXMODULES; i++) {
4434 if (!wlc->modulecb[i].iovars)
4435 continue;
4436 vi = wlc_iovar_lookup(wlc->modulecb[i].iovars, name);
4437 if (vi)
4438 break;
4440 /* iovar name not found */
4441 if (i >= WLC_MAXMODULES) {
4442 err = BCME_UNSUPPORTED;
4443 goto exit;
4446 /* set up 'params' pointer in case this is a set command so that
4447 * the convenience int and bool code can be common to set and get
4449 if (params == NULL) {
4450 params = arg;
4451 p_len = len;
4454 if (vi->type == IOVT_VOID)
4455 val_size = 0;
4456 else if (vi->type == IOVT_BUFFER)
4457 val_size = len;
4458 else
4459 /* all other types are integer sized */
4460 val_size = sizeof(int);
4462 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
4464 /* Do the actual parameter implementation */
4465 err = wlc->modulecb[i].iovar_fn(wlc->modulecb[i].hdl, vi, actionid,
4466 name, params, p_len, arg, len, val_size,
4467 wlcif);
4469 exit:
4470 return err;
4474 wlc_iovar_check(struct wlc_pub *pub, const bcm_iovar_t *vi, void *arg, int len,
4475 bool set)
4477 struct wlc_info *wlc = (struct wlc_info *) pub->wlc;
4478 int err = 0;
4479 s32 int_val = 0;
4481 /* check generic condition flags */
4482 if (set) {
4483 if (((vi->flags & IOVF_SET_DOWN) && wlc->pub->up) ||
4484 ((vi->flags & IOVF_SET_UP) && !wlc->pub->up)) {
4485 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4486 } else if ((vi->flags & IOVF_SET_BAND)
4487 && IS_MBAND_UNLOCKED(wlc)) {
4488 err = BCME_NOTBANDLOCKED;
4489 } else if ((vi->flags & IOVF_SET_CLK) && !wlc->clk) {
4490 err = BCME_NOCLK;
4492 } else {
4493 if (((vi->flags & IOVF_GET_DOWN) && wlc->pub->up) ||
4494 ((vi->flags & IOVF_GET_UP) && !wlc->pub->up)) {
4495 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4496 } else if ((vi->flags & IOVF_GET_BAND)
4497 && IS_MBAND_UNLOCKED(wlc)) {
4498 err = BCME_NOTBANDLOCKED;
4499 } else if ((vi->flags & IOVF_GET_CLK) && !wlc->clk) {
4500 err = BCME_NOCLK;
4504 if (err)
4505 goto exit;
4507 /* length check on io buf */
4508 err = bcm_iovar_lencheck(vi, arg, len, set);
4509 if (err)
4510 goto exit;
4512 /* On set, check value ranges for integer types */
4513 if (set) {
4514 switch (vi->type) {
4515 case IOVT_BOOL:
4516 case IOVT_INT8:
4517 case IOVT_INT16:
4518 case IOVT_INT32:
4519 case IOVT_UINT8:
4520 case IOVT_UINT16:
4521 case IOVT_UINT32:
4522 bcopy(arg, &int_val, sizeof(int));
4523 err = wlc_iovar_rangecheck(wlc, int_val, vi);
4524 break;
4527 exit:
4528 return err;
4531 /* handler for iovar table wlc_iovars */
4533 * IMPLEMENTATION NOTE: In order to avoid checking for get/set in each
4534 * iovar case, the switch statement maps the iovar id into separate get
4535 * and set values. If you add a new iovar to the switch you MUST use
4536 * IOV_GVAL and/or IOV_SVAL in the case labels to avoid conflict with
4537 * another case.
4538 * Please use params for additional qualifying parameters.
4541 wlc_doiovar(void *hdl, const bcm_iovar_t *vi, u32 actionid,
4542 const char *name, void *params, uint p_len, void *arg, int len,
4543 int val_size, struct wlc_if *wlcif)
4545 struct wlc_info *wlc = hdl;
4546 wlc_bsscfg_t *bsscfg;
4547 int err = 0;
4548 s32 int_val = 0;
4549 s32 int_val2 = 0;
4550 s32 *ret_int_ptr;
4551 bool bool_val;
4552 bool bool_val2;
4553 wlc_bss_info_t *current_bss;
4555 WL_TRACE("wl%d: %s\n", wlc->pub->unit, __func__);
4557 bsscfg = NULL;
4558 current_bss = NULL;
4560 err = wlc_iovar_check(wlc->pub, vi, arg, len, IOV_ISSET(actionid));
4561 if (err != 0)
4562 return err;
4564 /* convenience int and bool vals for first 8 bytes of buffer */
4565 if (p_len >= (int)sizeof(int_val))
4566 bcopy(params, &int_val, sizeof(int_val));
4568 if (p_len >= (int)sizeof(int_val) * 2)
4569 bcopy((void *)((unsigned long)params + sizeof(int_val)), &int_val2,
4570 sizeof(int_val));
4572 /* convenience int ptr for 4-byte gets (requires int aligned arg) */
4573 ret_int_ptr = (s32 *) arg;
4575 bool_val = (int_val != 0) ? true : false;
4576 bool_val2 = (int_val2 != 0) ? true : false;
4578 WL_TRACE("wl%d: %s: id %d\n",
4579 wlc->pub->unit, __func__, IOV_ID(actionid));
4580 /* Do the actual parameter implementation */
4581 switch (actionid) {
4582 case IOV_SVAL(IOV_RTSTHRESH):
4583 wlc->RTSThresh = int_val;
4584 break;
4586 case IOV_GVAL(IOV_QTXPOWER):{
4587 uint qdbm;
4588 bool override;
4590 err = wlc_phy_txpower_get(wlc->band->pi, &qdbm,
4591 &override);
4592 if (err != BCME_OK)
4593 return err;
4595 /* Return qdbm units */
4596 *ret_int_ptr =
4597 qdbm | (override ? WL_TXPWR_OVERRIDE : 0);
4598 break;
4601 /* As long as override is false, this only sets the *user* targets.
4602 User can twiddle this all he wants with no harm.
4603 wlc_phy_txpower_set() explicitly sets override to false if
4604 not internal or test.
4606 case IOV_SVAL(IOV_QTXPOWER):{
4607 u8 qdbm;
4608 bool override;
4610 /* Remove override bit and clip to max qdbm value */
4611 qdbm = (u8)min_t(u32, (int_val & ~WL_TXPWR_OVERRIDE), 0xff);
4612 /* Extract override setting */
4613 override = (int_val & WL_TXPWR_OVERRIDE) ? true : false;
4614 err =
4615 wlc_phy_txpower_set(wlc->band->pi, qdbm, override);
4616 break;
4619 case IOV_GVAL(IOV_MPC):
4620 *ret_int_ptr = (s32) wlc->mpc;
4621 break;
4623 case IOV_SVAL(IOV_MPC):
4624 wlc->mpc = bool_val;
4625 wlc_radio_mpc_upd(wlc);
4627 break;
4629 case IOV_GVAL(IOV_BCN_LI_BCN):
4630 *ret_int_ptr = wlc->bcn_li_bcn;
4631 break;
4633 case IOV_SVAL(IOV_BCN_LI_BCN):
4634 wlc->bcn_li_bcn = (u8) int_val;
4635 if (wlc->pub->up)
4636 wlc_bcn_li_upd(wlc);
4637 break;
4639 default:
4640 WL_ERROR("wl%d: %s: unsupported\n", wlc->pub->unit, __func__);
4641 err = BCME_UNSUPPORTED;
4642 break;
4645 goto exit; /* avoid unused label warning */
4647 exit:
4648 return err;
4651 static int
4652 wlc_iovar_rangecheck(struct wlc_info *wlc, u32 val, const bcm_iovar_t *vi)
4654 int err = 0;
4655 u32 min_val = 0;
4656 u32 max_val = 0;
4658 /* Only ranged integers are checked */
4659 switch (vi->type) {
4660 case IOVT_INT32:
4661 max_val |= 0x7fffffff;
4662 /* fall through */
4663 case IOVT_INT16:
4664 max_val |= 0x00007fff;
4665 /* fall through */
4666 case IOVT_INT8:
4667 max_val |= 0x0000007f;
4668 min_val = ~max_val;
4669 if (vi->flags & IOVF_NTRL)
4670 min_val = 1;
4671 else if (vi->flags & IOVF_WHL)
4672 min_val = 0;
4673 /* Signed values are checked against max_val and min_val */
4674 if ((s32) val < (s32) min_val
4675 || (s32) val > (s32) max_val)
4676 err = BCME_RANGE;
4677 break;
4679 case IOVT_UINT32:
4680 max_val |= 0xffffffff;
4681 /* fall through */
4682 case IOVT_UINT16:
4683 max_val |= 0x0000ffff;
4684 /* fall through */
4685 case IOVT_UINT8:
4686 max_val |= 0x000000ff;
4687 if (vi->flags & IOVF_NTRL)
4688 min_val = 1;
4689 if ((val < min_val) || (val > max_val))
4690 err = BCME_RANGE;
4691 break;
4694 return err;
4697 #ifdef BCMDBG
4698 static const char *supr_reason[] = {
4699 "None", "PMQ Entry", "Flush request",
4700 "Previous frag failure", "Channel mismatch",
4701 "Lifetime Expiry", "Underflow"
4704 static void wlc_print_txs_status(u16 s)
4706 printf("[15:12] %d frame attempts\n", (s & TX_STATUS_FRM_RTX_MASK) >>
4707 TX_STATUS_FRM_RTX_SHIFT);
4708 printf(" [11:8] %d rts attempts\n", (s & TX_STATUS_RTS_RTX_MASK) >>
4709 TX_STATUS_RTS_RTX_SHIFT);
4710 printf(" [7] %d PM mode indicated\n",
4711 ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
4712 printf(" [6] %d intermediate status\n",
4713 ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
4714 printf(" [5] %d AMPDU\n", (s & TX_STATUS_AMPDU) ? 1 : 0);
4715 printf(" [4:2] %d Frame Suppressed Reason (%s)\n",
4716 ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
4717 supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
4718 printf(" [1] %d acked\n", ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
4720 #endif /* BCMDBG */
4722 void wlc_print_txstatus(tx_status_t *txs)
4724 #if defined(BCMDBG)
4725 u16 s = txs->status;
4726 u16 ackphyrxsh = txs->ackphyrxsh;
4728 printf("\ntxpkt (MPDU) Complete\n");
4730 printf("FrameID: %04x ", txs->frameid);
4731 printf("TxStatus: %04x", s);
4732 printf("\n");
4733 #ifdef BCMDBG
4734 wlc_print_txs_status(s);
4735 #endif
4736 printf("LastTxTime: %04x ", txs->lasttxtime);
4737 printf("Seq: %04x ", txs->sequence);
4738 printf("PHYTxStatus: %04x ", txs->phyerr);
4739 printf("RxAckRSSI: %04x ",
4740 (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
4741 printf("RxAckSQ: %04x", (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
4742 printf("\n");
4743 #endif /* defined(BCMDBG) */
4746 #define MACSTATUPD(name) \
4747 wlc_ctrupd_cache(macstats.name, &wlc->core->macstat_snapshot->name, &wlc->pub->_cnt->name)
4749 void wlc_statsupd(struct wlc_info *wlc)
4751 int i;
4752 #ifdef BCMDBG
4753 u16 delta;
4754 u16 rxf0ovfl;
4755 u16 txfunfl[NFIFO];
4756 #endif /* BCMDBG */
4758 /* if driver down, make no sense to update stats */
4759 if (!wlc->pub->up)
4760 return;
4762 #ifdef BCMDBG
4763 /* save last rx fifo 0 overflow count */
4764 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
4766 /* save last tx fifo underflow count */
4767 for (i = 0; i < NFIFO; i++)
4768 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
4769 #endif /* BCMDBG */
4771 #ifdef BCMDBG
4772 /* check for rx fifo 0 overflow */
4773 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
4774 if (delta)
4775 WL_ERROR("wl%d: %u rx fifo 0 overflows!\n",
4776 wlc->pub->unit, delta);
4778 /* check for tx fifo underflows */
4779 for (i = 0; i < NFIFO; i++) {
4780 delta =
4781 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
4782 txfunfl[i]);
4783 if (delta)
4784 WL_ERROR("wl%d: %u tx fifo %d underflows!\n",
4785 wlc->pub->unit, delta, i);
4787 #endif /* BCMDBG */
4789 /* dot11 counter update */
4791 WLCNTSET(wlc->pub->_cnt->txrts,
4792 (wlc->pub->_cnt->rxctsucast -
4793 wlc->pub->_cnt->d11cnt_txrts_off));
4794 WLCNTSET(wlc->pub->_cnt->rxcrc,
4795 (wlc->pub->_cnt->rxbadfcs - wlc->pub->_cnt->d11cnt_rxcrc_off));
4796 WLCNTSET(wlc->pub->_cnt->txnocts,
4797 ((wlc->pub->_cnt->txrtsfrm - wlc->pub->_cnt->rxctsucast) -
4798 wlc->pub->_cnt->d11cnt_txnocts_off));
4800 /* merge counters from dma module */
4801 for (i = 0; i < NFIFO; i++) {
4802 if (wlc->hw->di[i]) {
4803 WLCNTADD(wlc->pub->_cnt->txnobuf,
4804 (wlc->hw->di[i])->txnobuf);
4805 WLCNTADD(wlc->pub->_cnt->rxnobuf,
4806 (wlc->hw->di[i])->rxnobuf);
4807 WLCNTADD(wlc->pub->_cnt->rxgiant,
4808 (wlc->hw->di[i])->rxgiants);
4809 dma_counterreset(wlc->hw->di[i]);
4814 * Aggregate transmit and receive errors that probably resulted
4815 * in the loss of a frame are computed on the fly.
4817 WLCNTSET(wlc->pub->_cnt->txerror,
4818 wlc->pub->_cnt->txnobuf + wlc->pub->_cnt->txnoassoc +
4819 wlc->pub->_cnt->txuflo + wlc->pub->_cnt->txrunt +
4820 wlc->pub->_cnt->dmade + wlc->pub->_cnt->dmada +
4821 wlc->pub->_cnt->dmape);
4822 WLCNTSET(wlc->pub->_cnt->rxerror,
4823 wlc->pub->_cnt->rxoflo + wlc->pub->_cnt->rxnobuf +
4824 wlc->pub->_cnt->rxfragerr + wlc->pub->_cnt->rxrunt +
4825 wlc->pub->_cnt->rxgiant + wlc->pub->_cnt->rxnoscb +
4826 wlc->pub->_cnt->rxbadsrcmac);
4827 for (i = 0; i < NFIFO; i++)
4828 WLCNTADD(wlc->pub->_cnt->rxerror, wlc->pub->_cnt->rxuflo[i]);
4831 bool wlc_chipmatch(u16 vendor, u16 device)
4833 if (vendor != VENDOR_BROADCOM) {
4834 WL_ERROR("wlc_chipmatch: unknown vendor id %04x\n", vendor);
4835 return false;
4838 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
4839 return true;
4841 if (device == BCM4313_D11N2G_ID)
4842 return true;
4843 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
4844 return true;
4846 WL_ERROR("wlc_chipmatch: unknown device id %04x\n", device);
4847 return false;
4850 #if defined(BCMDBG)
4851 void wlc_print_txdesc(d11txh_t *txh)
4853 u16 mtcl = ltoh16(txh->MacTxControlLow);
4854 u16 mtch = ltoh16(txh->MacTxControlHigh);
4855 u16 mfc = ltoh16(txh->MacFrameControl);
4856 u16 tfest = ltoh16(txh->TxFesTimeNormal);
4857 u16 ptcw = ltoh16(txh->PhyTxControlWord);
4858 u16 ptcw_1 = ltoh16(txh->PhyTxControlWord_1);
4859 u16 ptcw_1_Fbr = ltoh16(txh->PhyTxControlWord_1_Fbr);
4860 u16 ptcw_1_Rts = ltoh16(txh->PhyTxControlWord_1_Rts);
4861 u16 ptcw_1_FbrRts = ltoh16(txh->PhyTxControlWord_1_FbrRts);
4862 u16 mainrates = ltoh16(txh->MainRates);
4863 u16 xtraft = ltoh16(txh->XtraFrameTypes);
4864 u8 *iv = txh->IV;
4865 u8 *ra = txh->TxFrameRA;
4866 u16 tfestfb = ltoh16(txh->TxFesTimeFallback);
4867 u8 *rtspfb = txh->RTSPLCPFallback;
4868 u16 rtsdfb = ltoh16(txh->RTSDurFallback);
4869 u8 *fragpfb = txh->FragPLCPFallback;
4870 u16 fragdfb = ltoh16(txh->FragDurFallback);
4871 u16 mmodelen = ltoh16(txh->MModeLen);
4872 u16 mmodefbrlen = ltoh16(txh->MModeFbrLen);
4873 u16 tfid = ltoh16(txh->TxFrameID);
4874 u16 txs = ltoh16(txh->TxStatus);
4875 u16 mnmpdu = ltoh16(txh->MaxNMpdus);
4876 u16 mabyte = ltoh16(txh->MaxABytes_MRT);
4877 u16 mabyte_f = ltoh16(txh->MaxABytes_FBR);
4878 u16 mmbyte = ltoh16(txh->MinMBytes);
4880 u8 *rtsph = txh->RTSPhyHeader;
4881 struct ieee80211_rts rts = txh->rts_frame;
4882 char hexbuf[256];
4884 /* add plcp header along with txh descriptor */
4885 prhex("Raw TxDesc + plcp header", (unsigned char *) txh, sizeof(d11txh_t) + 48);
4887 printf("TxCtlLow: %04x ", mtcl);
4888 printf("TxCtlHigh: %04x ", mtch);
4889 printf("FC: %04x ", mfc);
4890 printf("FES Time: %04x\n", tfest);
4891 printf("PhyCtl: %04x%s ", ptcw,
4892 (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
4893 printf("PhyCtl_1: %04x ", ptcw_1);
4894 printf("PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
4895 printf("PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
4896 printf("PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
4897 printf("MainRates: %04x ", mainrates);
4898 printf("XtraFrameTypes: %04x ", xtraft);
4899 printf("\n");
4901 bcm_format_hex(hexbuf, iv, sizeof(txh->IV));
4902 printf("SecIV: %s\n", hexbuf);
4903 bcm_format_hex(hexbuf, ra, sizeof(txh->TxFrameRA));
4904 printf("RA: %s\n", hexbuf);
4906 printf("Fb FES Time: %04x ", tfestfb);
4907 bcm_format_hex(hexbuf, rtspfb, sizeof(txh->RTSPLCPFallback));
4908 printf("RTS PLCP: %s ", hexbuf);
4909 printf("RTS DUR: %04x ", rtsdfb);
4910 bcm_format_hex(hexbuf, fragpfb, sizeof(txh->FragPLCPFallback));
4911 printf("PLCP: %s ", hexbuf);
4912 printf("DUR: %04x", fragdfb);
4913 printf("\n");
4915 printf("MModeLen: %04x ", mmodelen);
4916 printf("MModeFbrLen: %04x\n", mmodefbrlen);
4918 printf("FrameID: %04x\n", tfid);
4919 printf("TxStatus: %04x\n", txs);
4921 printf("MaxNumMpdu: %04x\n", mnmpdu);
4922 printf("MaxAggbyte: %04x\n", mabyte);
4923 printf("MaxAggbyte_fb: %04x\n", mabyte_f);
4924 printf("MinByte: %04x\n", mmbyte);
4926 bcm_format_hex(hexbuf, rtsph, sizeof(txh->RTSPhyHeader));
4927 printf("RTS PLCP: %s ", hexbuf);
4928 bcm_format_hex(hexbuf, (u8 *) &rts, sizeof(txh->rts_frame));
4929 printf("RTS Frame: %s", hexbuf);
4930 printf("\n");
4933 #endif /* defined(BCMDBG) */
4935 #if defined(BCMDBG)
4936 void wlc_print_rxh(d11rxhdr_t *rxh)
4938 u16 len = rxh->RxFrameSize;
4939 u16 phystatus_0 = rxh->PhyRxStatus_0;
4940 u16 phystatus_1 = rxh->PhyRxStatus_1;
4941 u16 phystatus_2 = rxh->PhyRxStatus_2;
4942 u16 phystatus_3 = rxh->PhyRxStatus_3;
4943 u16 macstatus1 = rxh->RxStatus1;
4944 u16 macstatus2 = rxh->RxStatus2;
4945 char flagstr[64];
4946 char lenbuf[20];
4947 static const bcm_bit_desc_t macstat_flags[] = {
4948 {RXS_FCSERR, "FCSErr"},
4949 {RXS_RESPFRAMETX, "Reply"},
4950 {RXS_PBPRES, "PADDING"},
4951 {RXS_DECATMPT, "DeCr"},
4952 {RXS_DECERR, "DeCrErr"},
4953 {RXS_BCNSENT, "Bcn"},
4954 {0, NULL}
4957 prhex("Raw RxDesc", (unsigned char *) rxh, sizeof(d11rxhdr_t));
4959 bcm_format_flags(macstat_flags, macstatus1, flagstr, 64);
4961 snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
4963 printf("RxFrameSize: %6s (%d)%s\n", lenbuf, len,
4964 (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
4965 printf("RxPHYStatus: %04x %04x %04x %04x\n",
4966 phystatus_0, phystatus_1, phystatus_2, phystatus_3);
4967 printf("RxMACStatus: %x %s\n", macstatus1, flagstr);
4968 printf("RXMACaggtype: %x\n", (macstatus2 & RXS_AGGTYPE_MASK));
4969 printf("RxTSFTime: %04x\n", rxh->RxTSFTime);
4971 #endif /* defined(BCMDBG) */
4973 #if defined(BCMDBG)
4974 int wlc_format_ssid(char *buf, const unsigned char ssid[], uint ssid_len)
4976 uint i, c;
4977 char *p = buf;
4978 char *endp = buf + SSID_FMT_BUF_LEN;
4980 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4981 ssid_len = IEEE80211_MAX_SSID_LEN;
4983 for (i = 0; i < ssid_len; i++) {
4984 c = (uint) ssid[i];
4985 if (c == '\\') {
4986 *p++ = '\\';
4987 *p++ = '\\';
4988 } else if (isprint((unsigned char) c)) {
4989 *p++ = (char)c;
4990 } else {
4991 p += snprintf(p, (endp - p), "\\x%02X", c);
4994 *p = '\0';
4995 ASSERT(p < endp);
4997 return (int)(p - buf);
4999 #endif /* defined(BCMDBG) */
5001 u16 wlc_rate_shm_offset(struct wlc_info *wlc, u8 rate)
5003 return wlc_bmac_rate_shm_offset(wlc->hw, rate);
5006 /* Callback for device removed */
5009 * Attempts to queue a packet onto a multiple-precedence queue,
5010 * if necessary evicting a lower precedence packet from the queue.
5012 * 'prec' is the precedence number that has already been mapped
5013 * from the packet priority.
5015 * Returns true if packet consumed (queued), false if not.
5017 bool BCMFASTPATH
5018 wlc_prec_enq(struct wlc_info *wlc, struct pktq *q, void *pkt, int prec)
5020 return wlc_prec_enq_head(wlc, q, pkt, prec, false);
5023 bool BCMFASTPATH
5024 wlc_prec_enq_head(struct wlc_info *wlc, struct pktq *q, struct sk_buff *pkt,
5025 int prec, bool head)
5027 struct sk_buff *p;
5028 int eprec = -1; /* precedence to evict from */
5030 /* Determine precedence from which to evict packet, if any */
5031 if (pktq_pfull(q, prec))
5032 eprec = prec;
5033 else if (pktq_full(q)) {
5034 p = pktq_peek_tail(q, &eprec);
5035 ASSERT(p != NULL);
5036 if (eprec > prec) {
5037 WL_ERROR("%s: Failing: eprec %d > prec %d\n",
5038 __func__, eprec, prec);
5039 return false;
5043 /* Evict if needed */
5044 if (eprec >= 0) {
5045 bool discard_oldest;
5047 /* Detect queueing to unconfigured precedence */
5048 ASSERT(!pktq_pempty(q, eprec));
5050 discard_oldest = AC_BITMAP_TST(wlc->wme_dp, eprec);
5052 /* Refuse newer packet unless configured to discard oldest */
5053 if (eprec == prec && !discard_oldest) {
5054 WL_ERROR("%s: No where to go, prec == %d\n",
5055 __func__, prec);
5056 return false;
5059 /* Evict packet according to discard policy */
5060 p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q,
5061 eprec);
5062 ASSERT(p != NULL);
5064 /* Increment wme stats */
5065 if (WME_ENAB(wlc->pub)) {
5066 WLCNTINCR(wlc->pub->_wme_cnt->
5067 tx_failed[WME_PRIO2AC(p->priority)].packets);
5068 WLCNTADD(wlc->pub->_wme_cnt->
5069 tx_failed[WME_PRIO2AC(p->priority)].bytes,
5070 pkttotlen(wlc->osh, p));
5073 ASSERT(0);
5074 pkt_buf_free_skb(wlc->osh, p, true);
5075 WLCNTINCR(wlc->pub->_cnt->txnobuf);
5078 /* Enqueue */
5079 if (head)
5080 p = pktq_penq_head(q, prec, pkt);
5081 else
5082 p = pktq_penq(q, prec, pkt);
5083 ASSERT(p != NULL);
5085 return true;
5088 void BCMFASTPATH wlc_txq_enq(void *ctx, struct scb *scb, struct sk_buff *sdu,
5089 uint prec)
5091 struct wlc_info *wlc = (struct wlc_info *) ctx;
5092 wlc_txq_info_t *qi = wlc->active_queue; /* Check me */
5093 struct pktq *q = &qi->q;
5094 int prio;
5096 prio = sdu->priority;
5098 ASSERT(pktq_max(q) >= wlc->pub->tunables->datahiwat);
5100 if (!wlc_prec_enq(wlc, q, sdu, prec)) {
5101 if (!EDCF_ENAB(wlc->pub)
5102 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL))
5103 WL_ERROR("wl%d: wlc_txq_enq: txq overflow\n",
5104 wlc->pub->unit);
5106 /* ASSERT(9 == 8); *//* XXX we might hit this condtion in case packet flooding from mac80211 stack */
5107 pkt_buf_free_skb(wlc->osh, sdu, true);
5108 WLCNTINCR(wlc->pub->_cnt->txnobuf);
5111 /* Check if flow control needs to be turned on after enqueuing the packet
5112 * Don't turn on flow control if EDCF is enabled. Driver would make the decision on what
5113 * to drop instead of relying on stack to make the right decision
5115 if (!EDCF_ENAB(wlc->pub)
5116 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5117 if (pktq_len(q) >= wlc->pub->tunables->datahiwat) {
5118 wlc_txflowcontrol(wlc, qi, ON, ALLPRIO);
5120 } else if (wlc->pub->_priofc) {
5121 if (pktq_plen(q, wlc_prio2prec_map[prio]) >=
5122 wlc->pub->tunables->datahiwat) {
5123 wlc_txflowcontrol(wlc, qi, ON, prio);
5128 bool BCMFASTPATH
5129 wlc_sendpkt_mac80211(struct wlc_info *wlc, struct sk_buff *sdu,
5130 struct ieee80211_hw *hw)
5132 u8 prio;
5133 uint fifo;
5134 void *pkt;
5135 struct scb *scb = &global_scb;
5136 struct ieee80211_hdr *d11_header = (struct ieee80211_hdr *)(sdu->data);
5137 u16 type, fc;
5139 ASSERT(sdu);
5141 fc = ltoh16(d11_header->frame_control);
5142 type = (fc & IEEE80211_FCTL_FTYPE);
5144 /* 802.11 standard requires management traffic to go at highest priority */
5145 prio = (type == IEEE80211_FTYPE_DATA ? sdu->priority : MAXPRIO);
5146 fifo = prio2fifo[prio];
5148 ASSERT((uint) skb_headroom(sdu) >= TXOFF);
5149 ASSERT(!(sdu->next));
5150 ASSERT(!(sdu->prev));
5151 ASSERT(fifo < NFIFO);
5153 pkt = sdu;
5154 if (unlikely
5155 (wlc_d11hdrs_mac80211(wlc, hw, pkt, scb, 0, 1, fifo, 0, NULL, 0)))
5156 return -EINVAL;
5157 wlc_txq_enq(wlc, scb, pkt, WLC_PRIO_TO_PREC(prio));
5158 wlc_send_q(wlc, wlc->active_queue);
5160 WLCNTINCR(wlc->pub->_cnt->ieee_tx);
5161 return 0;
5164 void BCMFASTPATH wlc_send_q(struct wlc_info *wlc, wlc_txq_info_t *qi)
5166 struct sk_buff *pkt[DOT11_MAXNUMFRAGS];
5167 int prec;
5168 u16 prec_map;
5169 int err = 0, i, count;
5170 uint fifo;
5171 struct pktq *q = &qi->q;
5172 struct ieee80211_tx_info *tx_info;
5174 /* only do work for the active queue */
5175 if (qi != wlc->active_queue)
5176 return;
5178 if (in_send_q)
5179 return;
5180 else
5181 in_send_q = true;
5183 prec_map = wlc->tx_prec_map;
5185 /* Send all the enq'd pkts that we can.
5186 * Dequeue packets with precedence with empty HW fifo only
5188 while (prec_map && (pkt[0] = pktq_mdeq(q, prec_map, &prec))) {
5189 tx_info = IEEE80211_SKB_CB(pkt[0]);
5190 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
5191 err = wlc_sendampdu(wlc->ampdu, qi, pkt, prec);
5192 } else {
5193 count = 1;
5194 err = wlc_prep_pdu(wlc, pkt[0], &fifo);
5195 if (!err) {
5196 for (i = 0; i < count; i++) {
5197 wlc_txfifo(wlc, fifo, pkt[i], true, 1);
5202 if (err == BCME_BUSY) {
5203 pktq_penq_head(q, prec, pkt[0]);
5204 /* If send failed due to any other reason than a change in
5205 * HW FIFO condition, quit. Otherwise, read the new prec_map!
5207 if (prec_map == wlc->tx_prec_map)
5208 break;
5209 prec_map = wlc->tx_prec_map;
5213 /* Check if flow control needs to be turned off after sending the packet */
5214 if (!EDCF_ENAB(wlc->pub)
5215 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5216 if (wlc_txflowcontrol_prio_isset(wlc, qi, ALLPRIO)
5217 && (pktq_len(q) < wlc->pub->tunables->datahiwat / 2)) {
5218 wlc_txflowcontrol(wlc, qi, OFF, ALLPRIO);
5220 } else if (wlc->pub->_priofc) {
5221 int prio;
5222 for (prio = MAXPRIO; prio >= 0; prio--) {
5223 if (wlc_txflowcontrol_prio_isset(wlc, qi, prio) &&
5224 (pktq_plen(q, wlc_prio2prec_map[prio]) <
5225 wlc->pub->tunables->datahiwat / 2)) {
5226 wlc_txflowcontrol(wlc, qi, OFF, prio);
5230 in_send_q = false;
5234 * bcmc_fid_generate:
5235 * Generate frame ID for a BCMC packet. The frag field is not used
5236 * for MC frames so is used as part of the sequence number.
5238 static inline u16
5239 bcmc_fid_generate(struct wlc_info *wlc, wlc_bsscfg_t *bsscfg, d11txh_t *txh)
5241 u16 frameid;
5243 frameid = ltoh16(txh->TxFrameID) & ~(TXFID_SEQ_MASK | TXFID_QUEUE_MASK);
5244 frameid |=
5245 (((wlc->
5246 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5247 TX_BCMC_FIFO;
5249 return frameid;
5252 void BCMFASTPATH
5253 wlc_txfifo(struct wlc_info *wlc, uint fifo, struct sk_buff *p, bool commit,
5254 s8 txpktpend)
5256 u16 frameid = INVALIDFID;
5257 d11txh_t *txh;
5259 ASSERT(fifo < NFIFO);
5260 txh = (d11txh_t *) (p->data);
5262 /* When a BC/MC frame is being committed to the BCMC fifo via DMA (NOT PIO), update
5263 * ucode or BSS info as appropriate.
5265 if (fifo == TX_BCMC_FIFO) {
5266 frameid = ltoh16(txh->TxFrameID);
5270 if (WLC_WAR16165(wlc))
5271 wlc_war16165(wlc, true);
5274 /* Bump up pending count for if not using rpc. If rpc is used, this will be handled
5275 * in wlc_bmac_txfifo()
5277 if (commit) {
5278 TXPKTPENDINC(wlc, fifo, txpktpend);
5279 WL_TRACE("wlc_txfifo, pktpend inc %d to %d\n",
5280 txpktpend, TXPKTPENDGET(wlc, fifo));
5283 /* Commit BCMC sequence number in the SHM frame ID location */
5284 if (frameid != INVALIDFID)
5285 BCMCFID(wlc, frameid);
5287 if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0) {
5288 WL_ERROR("wlc_txfifo: fatal, toss frames !!!\n");
5292 static u16
5293 wlc_compute_airtime(struct wlc_info *wlc, ratespec_t rspec, uint length)
5295 u16 usec = 0;
5296 uint mac_rate = RSPEC2RATE(rspec);
5297 uint nsyms;
5299 if (IS_MCS(rspec)) {
5300 /* not supported yet */
5301 ASSERT(0);
5302 } else if (IS_OFDM(rspec)) {
5303 /* nsyms = Ceiling(Nbits / (Nbits/sym))
5305 * Nbits = length * 8
5306 * Nbits/sym = Mbps * 4 = mac_rate * 2
5308 nsyms = CEIL((length * 8), (mac_rate * 2));
5310 /* usec = symbols * usec/symbol */
5311 usec = (u16) (nsyms * APHY_SYMBOL_TIME);
5312 return usec;
5313 } else {
5314 switch (mac_rate) {
5315 case WLC_RATE_1M:
5316 usec = length << 3;
5317 break;
5318 case WLC_RATE_2M:
5319 usec = length << 2;
5320 break;
5321 case WLC_RATE_5M5:
5322 usec = (length << 4) / 11;
5323 break;
5324 case WLC_RATE_11M:
5325 usec = (length << 3) / 11;
5326 break;
5327 default:
5328 WL_ERROR("wl%d: wlc_compute_airtime: unsupported rspec 0x%x\n",
5329 wlc->pub->unit, rspec);
5330 ASSERT((const char *)"Bad phy_rate" == NULL);
5331 break;
5335 return usec;
5338 void BCMFASTPATH
5339 wlc_compute_plcp(struct wlc_info *wlc, ratespec_t rspec, uint length, u8 *plcp)
5341 if (IS_MCS(rspec)) {
5342 wlc_compute_mimo_plcp(rspec, length, plcp);
5343 } else if (IS_OFDM(rspec)) {
5344 wlc_compute_ofdm_plcp(rspec, length, plcp);
5345 } else {
5346 wlc_compute_cck_plcp(rspec, length, plcp);
5348 return;
5351 /* Rate: 802.11 rate code, length: PSDU length in octets */
5352 static void wlc_compute_mimo_plcp(ratespec_t rspec, uint length, u8 *plcp)
5354 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
5355 ASSERT(IS_MCS(rspec));
5356 plcp[0] = mcs;
5357 if (RSPEC_IS40MHZ(rspec) || (mcs == 32))
5358 plcp[0] |= MIMO_PLCP_40MHZ;
5359 WLC_SET_MIMO_PLCP_LEN(plcp, length);
5360 plcp[3] = RSPEC_MIMOPLCP3(rspec); /* rspec already holds this byte */
5361 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
5362 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
5363 plcp[5] = 0;
5366 /* Rate: 802.11 rate code, length: PSDU length in octets */
5367 static void BCMFASTPATH
5368 wlc_compute_ofdm_plcp(ratespec_t rspec, u32 length, u8 *plcp)
5370 u8 rate_signal;
5371 u32 tmp = 0;
5372 int rate = RSPEC2RATE(rspec);
5374 ASSERT(IS_OFDM(rspec));
5376 /* encode rate per 802.11a-1999 sec 17.3.4.1, with lsb transmitted first */
5377 rate_signal = rate_info[rate] & RATE_MASK;
5378 ASSERT(rate_signal != 0);
5380 memset(plcp, 0, D11_PHY_HDR_LEN);
5381 D11A_PHY_HDR_SRATE((ofdm_phy_hdr_t *) plcp, rate_signal);
5383 tmp = (length & 0xfff) << 5;
5384 plcp[2] |= (tmp >> 16) & 0xff;
5385 plcp[1] |= (tmp >> 8) & 0xff;
5386 plcp[0] |= tmp & 0xff;
5388 return;
5392 * Compute PLCP, but only requires actual rate and length of pkt.
5393 * Rate is given in the driver standard multiple of 500 kbps.
5394 * le is set for 11 Mbps rate if necessary.
5395 * Broken out for PRQ.
5398 static void wlc_cck_plcp_set(int rate_500, uint length, u8 *plcp)
5400 u16 usec = 0;
5401 u8 le = 0;
5403 switch (rate_500) {
5404 case WLC_RATE_1M:
5405 usec = length << 3;
5406 break;
5407 case WLC_RATE_2M:
5408 usec = length << 2;
5409 break;
5410 case WLC_RATE_5M5:
5411 usec = (length << 4) / 11;
5412 if ((length << 4) - (usec * 11) > 0)
5413 usec++;
5414 break;
5415 case WLC_RATE_11M:
5416 usec = (length << 3) / 11;
5417 if ((length << 3) - (usec * 11) > 0) {
5418 usec++;
5419 if ((usec * 11) - (length << 3) >= 8)
5420 le = D11B_PLCP_SIGNAL_LE;
5422 break;
5424 default:
5425 WL_ERROR("wlc_cck_plcp_set: unsupported rate %d\n", rate_500);
5426 rate_500 = WLC_RATE_1M;
5427 usec = length << 3;
5428 break;
5430 /* PLCP signal byte */
5431 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
5432 /* PLCP service byte */
5433 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
5434 /* PLCP length u16, little endian */
5435 plcp[2] = usec & 0xff;
5436 plcp[3] = (usec >> 8) & 0xff;
5437 /* PLCP CRC16 */
5438 plcp[4] = 0;
5439 plcp[5] = 0;
5442 /* Rate: 802.11 rate code, length: PSDU length in octets */
5443 static void wlc_compute_cck_plcp(ratespec_t rspec, uint length, u8 *plcp)
5445 int rate = RSPEC2RATE(rspec);
5447 ASSERT(IS_CCK(rspec));
5449 wlc_cck_plcp_set(rate, length, plcp);
5452 /* wlc_compute_frame_dur()
5454 * Calculate the 802.11 MAC header DUR field for MPDU
5455 * DUR for a single frame = 1 SIFS + 1 ACK
5456 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5458 * rate MPDU rate in unit of 500kbps
5459 * next_frag_len next MPDU length in bytes
5460 * preamble_type use short/GF or long/MM PLCP header
5462 static u16 BCMFASTPATH
5463 wlc_compute_frame_dur(struct wlc_info *wlc, ratespec_t rate, u8 preamble_type,
5464 uint next_frag_len)
5466 u16 dur, sifs;
5468 sifs = SIFS(wlc->band);
5470 dur = sifs;
5471 dur += (u16) wlc_calc_ack_time(wlc, rate, preamble_type);
5473 if (next_frag_len) {
5474 /* Double the current DUR to get 2 SIFS + 2 ACKs */
5475 dur *= 2;
5476 /* add another SIFS and the frag time */
5477 dur += sifs;
5478 dur +=
5479 (u16) wlc_calc_frame_time(wlc, rate, preamble_type,
5480 next_frag_len);
5482 return dur;
5485 /* wlc_compute_rtscts_dur()
5487 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
5488 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
5489 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
5491 * cts cts-to-self or rts/cts
5492 * rts_rate rts or cts rate in unit of 500kbps
5493 * rate next MPDU rate in unit of 500kbps
5494 * frame_len next MPDU frame length in bytes
5496 u16 BCMFASTPATH
5497 wlc_compute_rtscts_dur(struct wlc_info *wlc, bool cts_only, ratespec_t rts_rate,
5498 ratespec_t frame_rate, u8 rts_preamble_type,
5499 u8 frame_preamble_type, uint frame_len, bool ba)
5501 u16 dur, sifs;
5503 sifs = SIFS(wlc->band);
5505 if (!cts_only) { /* RTS/CTS */
5506 dur = 3 * sifs;
5507 dur +=
5508 (u16) wlc_calc_cts_time(wlc, rts_rate,
5509 rts_preamble_type);
5510 } else { /* CTS-TO-SELF */
5511 dur = 2 * sifs;
5514 dur +=
5515 (u16) wlc_calc_frame_time(wlc, frame_rate, frame_preamble_type,
5516 frame_len);
5517 if (ba)
5518 dur +=
5519 (u16) wlc_calc_ba_time(wlc, frame_rate,
5520 WLC_SHORT_PREAMBLE);
5521 else
5522 dur +=
5523 (u16) wlc_calc_ack_time(wlc, frame_rate,
5524 frame_preamble_type);
5525 return dur;
5528 static bool wlc_phy_rspec_check(struct wlc_info *wlc, u16 bw, ratespec_t rspec)
5530 if (IS_MCS(rspec)) {
5531 uint mcs = rspec & RSPEC_RATE_MASK;
5533 if (mcs < 8) {
5534 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5535 } else if ((mcs >= 8) && (mcs <= 23)) {
5536 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SDM);
5537 } else if (mcs == 32) {
5538 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5539 ASSERT(bw == PHY_TXC1_BW_40MHZ_DUP);
5541 } else if (IS_OFDM(rspec)) {
5542 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_STBC);
5543 } else {
5544 ASSERT(IS_CCK(rspec));
5546 ASSERT((bw == PHY_TXC1_BW_20MHZ)
5547 || (bw == PHY_TXC1_BW_20MHZ_UP));
5548 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SISO);
5551 return true;
5554 u16 BCMFASTPATH wlc_phytxctl1_calc(struct wlc_info *wlc, ratespec_t rspec)
5556 u16 phyctl1 = 0;
5557 u16 bw;
5559 if (WLCISLCNPHY(wlc->band)) {
5560 bw = PHY_TXC1_BW_20MHZ;
5561 } else {
5562 bw = RSPEC_GET_BW(rspec);
5563 /* 10Mhz is not supported yet */
5564 if (bw < PHY_TXC1_BW_20MHZ) {
5565 WL_ERROR("wlc_phytxctl1_calc: bw %d is not supported yet, set to 20L\n",
5566 bw);
5567 bw = PHY_TXC1_BW_20MHZ;
5570 wlc_phy_rspec_check(wlc, bw, rspec);
5573 if (IS_MCS(rspec)) {
5574 uint mcs = rspec & RSPEC_RATE_MASK;
5576 /* bw, stf, coding-type is part of RSPEC_PHYTXBYTE2 returns */
5577 phyctl1 = RSPEC_PHYTXBYTE2(rspec);
5578 /* set the upper byte of phyctl1 */
5579 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
5580 } else if (IS_CCK(rspec) && !WLCISLCNPHY(wlc->band)
5581 && !WLCISSSLPNPHY(wlc->band)) {
5582 /* In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate */
5583 /* Eventually MIMOPHY would also be converted to this format */
5584 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
5585 phyctl1 = (bw | (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5586 } else { /* legacy OFDM/CCK */
5587 s16 phycfg;
5588 /* get the phyctl byte from rate phycfg table */
5589 phycfg = wlc_rate_legacy_phyctl(RSPEC2RATE(rspec));
5590 if (phycfg == -1) {
5591 WL_ERROR("wlc_phytxctl1_calc: wrong legacy OFDM/CCK rate\n");
5592 ASSERT(0);
5593 phycfg = 0;
5595 /* set the upper byte of phyctl1 */
5596 phyctl1 =
5597 (bw | (phycfg << 8) |
5598 (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5601 #ifdef BCMDBG
5602 /* phy clock must support 40Mhz if tx descriptor uses it */
5603 if ((phyctl1 & PHY_TXC1_BW_MASK) >= PHY_TXC1_BW_40MHZ) {
5604 ASSERT(CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ);
5605 ASSERT(wlc->chanspec == wlc_phy_chanspec_get(wlc->band->pi));
5607 #endif /* BCMDBG */
5608 return phyctl1;
5611 ratespec_t BCMFASTPATH
5612 wlc_rspec_to_rts_rspec(struct wlc_info *wlc, ratespec_t rspec, bool use_rspec,
5613 u16 mimo_ctlchbw)
5615 ratespec_t rts_rspec = 0;
5617 if (use_rspec) {
5618 /* use frame rate as rts rate */
5619 rts_rspec = rspec;
5621 } else if (wlc->band->gmode && wlc->protection->_g && !IS_CCK(rspec)) {
5622 /* Use 11Mbps as the g protection RTS target rate and fallback.
5623 * Use the WLC_BASIC_RATE() lookup to find the best basic rate under the
5624 * target in case 11 Mbps is not Basic.
5625 * 6 and 9 Mbps are not usually selected by rate selection, but even
5626 * if the OFDM rate we are protecting is 6 or 9 Mbps, 11 is more robust.
5628 rts_rspec = WLC_BASIC_RATE(wlc, WLC_RATE_11M);
5629 } else {
5630 /* calculate RTS rate and fallback rate based on the frame rate
5631 * RTS must be sent at a basic rate since it is a
5632 * control frame, sec 9.6 of 802.11 spec
5634 rts_rspec = WLC_BASIC_RATE(wlc, rspec);
5637 if (WLC_PHY_11N_CAP(wlc->band)) {
5638 /* set rts txbw to correct side band */
5639 rts_rspec &= ~RSPEC_BW_MASK;
5641 /* if rspec/rspec_fallback is 40MHz, then send RTS on both 20MHz channel
5642 * (DUP), otherwise send RTS on control channel
5644 if (RSPEC_IS40MHZ(rspec) && !IS_CCK(rts_rspec))
5645 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
5646 else
5647 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
5649 /* pick siso/cdd as default for ofdm */
5650 if (IS_OFDM(rts_rspec)) {
5651 rts_rspec &= ~RSPEC_STF_MASK;
5652 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
5655 return rts_rspec;
5659 * Add d11txh_t, cck_phy_hdr_t.
5661 * 'p' data must start with 802.11 MAC header
5662 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
5664 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
5667 static u16 BCMFASTPATH
5668 wlc_d11hdrs_mac80211(struct wlc_info *wlc, struct ieee80211_hw *hw,
5669 struct sk_buff *p, struct scb *scb, uint frag,
5670 uint nfrags, uint queue, uint next_frag_len,
5671 wsec_key_t *key, ratespec_t rspec_override)
5673 struct ieee80211_hdr *h;
5674 d11txh_t *txh;
5675 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
5676 struct osl_info *osh;
5677 int len, phylen, rts_phylen;
5678 u16 fc, type, frameid, mch, phyctl, xfts, mainrates;
5679 u16 seq = 0, mcl = 0, status = 0;
5680 ratespec_t rspec[2] = { WLC_RATE_1M, WLC_RATE_1M }, rts_rspec[2] = {
5681 WLC_RATE_1M, WLC_RATE_1M};
5682 bool use_rts = false;
5683 bool use_cts = false;
5684 bool use_rifs = false;
5685 bool short_preamble[2] = { false, false };
5686 u8 preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5687 u8 rts_preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5688 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
5689 struct ieee80211_rts *rts = NULL;
5690 bool qos;
5691 uint ac;
5692 u32 rate_val[2];
5693 bool hwtkmic = false;
5694 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
5695 #ifdef WLANTSEL
5696 #define ANTCFG_NONE 0xFF
5697 u8 antcfg = ANTCFG_NONE;
5698 u8 fbantcfg = ANTCFG_NONE;
5699 #endif
5700 uint phyctl1_stf = 0;
5701 u16 durid = 0;
5702 struct ieee80211_tx_rate *txrate[2];
5703 int k;
5704 struct ieee80211_tx_info *tx_info;
5705 bool is_mcs[2];
5706 u16 mimo_txbw;
5707 u8 mimo_preamble_type;
5709 frameid = 0;
5711 ASSERT(queue < NFIFO);
5713 osh = wlc->osh;
5715 /* locate 802.11 MAC header */
5716 h = (struct ieee80211_hdr *)(p->data);
5717 fc = ltoh16(h->frame_control);
5718 type = (fc & IEEE80211_FCTL_FTYPE);
5720 qos = (type == IEEE80211_FTYPE_DATA &&
5721 FC_SUBTYPE_ANY_QOS(fc));
5723 /* compute length of frame in bytes for use in PLCP computations */
5724 len = pkttotlen(osh, p);
5725 phylen = len + FCS_LEN;
5727 /* If WEP enabled, add room in phylen for the additional bytes of
5728 * ICV which MAC generates. We do NOT add the additional bytes to
5729 * the packet itself, thus phylen = packet length + ICV_LEN + FCS_LEN
5730 * in this case
5732 if (key) {
5733 phylen += key->icv_len;
5736 /* Get tx_info */
5737 tx_info = IEEE80211_SKB_CB(p);
5738 ASSERT(tx_info);
5740 /* add PLCP */
5741 plcp = skb_push(p, D11_PHY_HDR_LEN);
5743 /* add Broadcom tx descriptor header */
5744 txh = (d11txh_t *) skb_push(p, D11_TXH_LEN);
5745 memset((char *)txh, 0, D11_TXH_LEN);
5747 /* setup frameid */
5748 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
5749 /* non-AP STA should never use BCMC queue */
5750 ASSERT(queue != TX_BCMC_FIFO);
5751 if (queue == TX_BCMC_FIFO) {
5752 WL_ERROR("wl%d: %s: ASSERT queue == TX_BCMC!\n",
5753 WLCWLUNIT(wlc), __func__);
5754 frameid = bcmc_fid_generate(wlc, NULL, txh);
5755 } else {
5756 /* Increment the counter for first fragment */
5757 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
5758 SCB_SEQNUM(scb, p->priority)++;
5761 /* extract fragment number from frame first */
5762 seq = ltoh16(seq) & FRAGNUM_MASK;
5763 seq |= (SCB_SEQNUM(scb, p->priority) << SEQNUM_SHIFT);
5764 h->seq_ctrl = htol16(seq);
5766 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5767 (queue & TXFID_QUEUE_MASK);
5770 frameid |= queue & TXFID_QUEUE_MASK;
5772 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
5773 if (SCB_PS(scb) || ((fc & FC_KIND_MASK) == FC_BEACON))
5774 mcl |= TXC_IGNOREPMQ;
5776 ASSERT(hw->max_rates <= IEEE80211_TX_MAX_RATES);
5777 ASSERT(hw->max_rates == 2);
5779 txrate[0] = tx_info->control.rates;
5780 txrate[1] = txrate[0] + 1;
5782 ASSERT(txrate[0]->idx >= 0);
5783 /* if rate control algorithm didn't give us a fallback rate, use the primary rate */
5784 if (txrate[1]->idx < 0) {
5785 txrate[1] = txrate[0];
5788 for (k = 0; k < hw->max_rates; k++) {
5789 is_mcs[k] =
5790 txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
5791 if (!is_mcs[k]) {
5792 ASSERT(!(tx_info->flags & IEEE80211_TX_CTL_AMPDU));
5793 if ((txrate[k]->idx >= 0)
5794 && (txrate[k]->idx <
5795 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
5796 rate_val[k] =
5797 hw->wiphy->bands[tx_info->band]->
5798 bitrates[txrate[k]->idx].hw_value;
5799 short_preamble[k] =
5800 txrate[k]->
5801 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
5802 true : false;
5803 } else {
5804 ASSERT((txrate[k]->idx >= 0) &&
5805 (txrate[k]->idx <
5806 hw->wiphy->bands[tx_info->band]->
5807 n_bitrates));
5808 rate_val[k] = WLC_RATE_1M;
5810 } else {
5811 rate_val[k] = txrate[k]->idx;
5813 /* Currently only support same setting for primay and fallback rates.
5814 * Unify flags for each rate into a single value for the frame
5816 use_rts |=
5817 txrate[k]->
5818 flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
5819 use_cts |=
5820 txrate[k]->
5821 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
5823 if (is_mcs[k])
5824 rate_val[k] |= NRATE_MCS_INUSE;
5826 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band, rate_val[k]);
5828 /* (1) RATE: determine and validate primary rate and fallback rates */
5829 if (!RSPEC_ACTIVE(rspec[k])) {
5830 ASSERT(RSPEC_ACTIVE(rspec[k]));
5831 rspec[k] = WLC_RATE_1M;
5832 } else {
5833 if (WLANTSEL_ENAB(wlc) &&
5834 !is_multicast_ether_addr(h->addr1)) {
5835 /* set tx antenna config */
5836 wlc_antsel_antcfg_get(wlc->asi, false, false, 0,
5837 0, &antcfg, &fbantcfg);
5842 phyctl1_stf = wlc->stf->ss_opmode;
5844 if (N_ENAB(wlc->pub)) {
5845 for (k = 0; k < hw->max_rates; k++) {
5846 /* apply siso/cdd to single stream mcs's or ofdm if rspec is auto selected */
5847 if (((IS_MCS(rspec[k]) &&
5848 IS_SINGLE_STREAM(rspec[k] & RSPEC_RATE_MASK)) ||
5849 IS_OFDM(rspec[k]))
5850 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
5851 || !(rspec[k] & RSPEC_OVERRIDE))) {
5852 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
5854 /* For SISO MCS use STBC if possible */
5855 if (IS_MCS(rspec[k])
5856 && WLC_STF_SS_STBC_TX(wlc, scb)) {
5857 u8 stc;
5859 ASSERT(WLC_STBC_CAP_PHY(wlc));
5860 stc = 1; /* Nss for single stream is always 1 */
5861 rspec[k] |=
5862 (PHY_TXC1_MODE_STBC <<
5863 RSPEC_STF_SHIFT) | (stc <<
5864 RSPEC_STC_SHIFT);
5865 } else
5866 rspec[k] |=
5867 (phyctl1_stf << RSPEC_STF_SHIFT);
5870 /* Is the phy configured to use 40MHZ frames? If so then pick the desired txbw */
5871 if (CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ) {
5872 /* default txbw is 20in40 SB */
5873 mimo_ctlchbw = mimo_txbw =
5874 CHSPEC_SB_UPPER(WLC_BAND_PI_RADIO_CHANSPEC)
5875 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
5877 if (IS_MCS(rspec[k])) {
5878 /* mcs 32 must be 40b/w DUP */
5879 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
5880 mimo_txbw =
5881 PHY_TXC1_BW_40MHZ_DUP;
5882 /* use override */
5883 } else if (wlc->mimo_40txbw != AUTO)
5884 mimo_txbw = wlc->mimo_40txbw;
5885 /* else check if dst is using 40 Mhz */
5886 else if (scb->flags & SCB_IS40)
5887 mimo_txbw = PHY_TXC1_BW_40MHZ;
5888 } else if (IS_OFDM(rspec[k])) {
5889 if (wlc->ofdm_40txbw != AUTO)
5890 mimo_txbw = wlc->ofdm_40txbw;
5891 } else {
5892 ASSERT(IS_CCK(rspec[k]));
5893 if (wlc->cck_40txbw != AUTO)
5894 mimo_txbw = wlc->cck_40txbw;
5896 } else {
5897 /* mcs32 is 40 b/w only.
5898 * This is possible for probe packets on a STA during SCAN
5900 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
5901 /* mcs 0 */
5902 rspec[k] = RSPEC_MIMORATE;
5904 mimo_txbw = PHY_TXC1_BW_20MHZ;
5907 /* Set channel width */
5908 rspec[k] &= ~RSPEC_BW_MASK;
5909 if ((k == 0) || ((k > 0) && IS_MCS(rspec[k])))
5910 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
5911 else
5912 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
5914 /* Set Short GI */
5915 #ifdef NOSGIYET
5916 if (IS_MCS(rspec[k])
5917 && (txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
5918 rspec[k] |= RSPEC_SHORT_GI;
5919 else if (!(txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
5920 rspec[k] &= ~RSPEC_SHORT_GI;
5921 #else
5922 rspec[k] &= ~RSPEC_SHORT_GI;
5923 #endif
5925 mimo_preamble_type = WLC_MM_PREAMBLE;
5926 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
5927 mimo_preamble_type = WLC_GF_PREAMBLE;
5929 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
5930 && (!IS_MCS(rspec[k]))) {
5931 WL_ERROR("wl%d: %s: IEEE80211_TX_RC_MCS != IS_MCS(rspec)\n",
5932 WLCWLUNIT(wlc), __func__);
5933 ASSERT(0 && "Rate mismatch");
5936 if (IS_MCS(rspec[k])) {
5937 preamble_type[k] = mimo_preamble_type;
5939 /* if SGI is selected, then forced mm for single stream */
5940 if ((rspec[k] & RSPEC_SHORT_GI)
5941 && IS_SINGLE_STREAM(rspec[k] &
5942 RSPEC_RATE_MASK)) {
5943 preamble_type[k] = WLC_MM_PREAMBLE;
5947 /* mimo bw field MUST now be valid in the rspec (it affects duration calculations) */
5948 ASSERT(VALID_RATE_DBG(wlc, rspec[0]));
5950 /* should be better conditionalized */
5951 if (!IS_MCS(rspec[0])
5952 && (tx_info->control.rates[0].
5953 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
5954 preamble_type[k] = WLC_SHORT_PREAMBLE;
5956 ASSERT(!IS_MCS(rspec[0])
5957 || WLC_IS_MIMO_PREAMBLE(preamble_type[k]));
5959 } else {
5960 for (k = 0; k < hw->max_rates; k++) {
5961 /* Set ctrlchbw as 20Mhz */
5962 ASSERT(!IS_MCS(rspec[k]));
5963 rspec[k] &= ~RSPEC_BW_MASK;
5964 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
5966 /* for nphy, stf of ofdm frames must follow policies */
5967 if (WLCISNPHY(wlc->band) && IS_OFDM(rspec[k])) {
5968 rspec[k] &= ~RSPEC_STF_MASK;
5969 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
5974 /* Reset these for use with AMPDU's */
5975 txrate[0]->count = 0;
5976 txrate[1]->count = 0;
5978 /* (2) PROTECTION, may change rspec */
5979 if ((ieee80211_is_data(fc) || ieee80211_is_mgmt(fc)) &&
5980 (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
5981 use_rts = true;
5983 /* (3) PLCP: determine PLCP header and MAC duration, fill d11txh_t */
5984 wlc_compute_plcp(wlc, rspec[0], phylen, plcp);
5985 wlc_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
5986 bcopy(plcp_fallback, (char *)&txh->FragPLCPFallback,
5987 sizeof(txh->FragPLCPFallback));
5989 /* Length field now put in CCK FBR CRC field */
5990 if (IS_CCK(rspec[1])) {
5991 txh->FragPLCPFallback[4] = phylen & 0xff;
5992 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
5995 /* MIMO-RATE: need validation ?? */
5996 mainrates =
5997 IS_OFDM(rspec[0]) ? D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) plcp) :
5998 plcp[0];
6000 /* DUR field for main rate */
6001 if ((fc != FC_PS_POLL) &&
6002 !is_multicast_ether_addr(h->addr1) && !use_rifs) {
6003 durid =
6004 wlc_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6005 next_frag_len);
6006 h->duration_id = htol16(durid);
6007 } else if (use_rifs) {
6008 /* NAV protect to end of next max packet size */
6009 durid =
6010 (u16) wlc_calc_frame_time(wlc, rspec[0],
6011 preamble_type[0],
6012 DOT11_MAX_FRAG_LEN);
6013 durid += RIFS_11N_TIME;
6014 h->duration_id = htol16(durid);
6017 /* DUR field for fallback rate */
6018 if (fc == FC_PS_POLL)
6019 txh->FragDurFallback = h->duration_id;
6020 else if (is_multicast_ether_addr(h->addr1) || use_rifs)
6021 txh->FragDurFallback = 0;
6022 else {
6023 durid = wlc_compute_frame_dur(wlc, rspec[1],
6024 preamble_type[1], next_frag_len);
6025 txh->FragDurFallback = htol16(durid);
6028 /* (4) MAC-HDR: MacTxControlLow */
6029 if (frag == 0)
6030 mcl |= TXC_STARTMSDU;
6032 if (!is_multicast_ether_addr(h->addr1))
6033 mcl |= TXC_IMMEDACK;
6035 if (BAND_5G(wlc->band->bandtype))
6036 mcl |= TXC_FREQBAND_5G;
6038 if (CHSPEC_IS40(WLC_BAND_PI_RADIO_CHANSPEC))
6039 mcl |= TXC_BW_40;
6041 /* set AMIC bit if using hardware TKIP MIC */
6042 if (hwtkmic)
6043 mcl |= TXC_AMIC;
6045 txh->MacTxControlLow = htol16(mcl);
6047 /* MacTxControlHigh */
6048 mch = 0;
6050 /* Set fallback rate preamble type */
6051 if ((preamble_type[1] == WLC_SHORT_PREAMBLE) ||
6052 (preamble_type[1] == WLC_GF_PREAMBLE)) {
6053 ASSERT((preamble_type[1] == WLC_GF_PREAMBLE) ||
6054 (!IS_MCS(rspec[1])));
6055 if (RSPEC2RATE(rspec[1]) != WLC_RATE_1M)
6056 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6059 /* MacFrameControl */
6060 bcopy((char *)&h->frame_control, (char *)&txh->MacFrameControl,
6061 sizeof(u16));
6062 txh->TxFesTimeNormal = htol16(0);
6064 txh->TxFesTimeFallback = htol16(0);
6066 /* TxFrameRA */
6067 bcopy((char *)&h->addr1, (char *)&txh->TxFrameRA, ETH_ALEN);
6069 /* TxFrameID */
6070 txh->TxFrameID = htol16(frameid);
6072 /* TxStatus, Note the case of recreating the first frag of a suppressed frame
6073 * then we may need to reset the retry cnt's via the status reg
6075 txh->TxStatus = htol16(status);
6077 if (D11REV_GE(wlc->pub->corerev, 16)) {
6078 /* extra fields for ucode AMPDU aggregation, the new fields are added to
6079 * the END of previous structure so that it's compatible in driver.
6080 * In old rev ucode, these fields should be ignored
6082 txh->MaxNMpdus = htol16(0);
6083 txh->MaxABytes_MRT = htol16(0);
6084 txh->MaxABytes_FBR = htol16(0);
6085 txh->MinMBytes = htol16(0);
6088 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration, furnish d11txh_t */
6089 /* RTS PLCP header and RTS frame */
6090 if (use_rts || use_cts) {
6091 if (use_rts && use_cts)
6092 use_cts = false;
6094 for (k = 0; k < 2; k++) {
6095 rts_rspec[k] = wlc_rspec_to_rts_rspec(wlc, rspec[k],
6096 false,
6097 mimo_ctlchbw);
6100 if (!IS_OFDM(rts_rspec[0]) &&
6101 !((RSPEC2RATE(rts_rspec[0]) == WLC_RATE_1M) ||
6102 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6103 rts_preamble_type[0] = WLC_SHORT_PREAMBLE;
6104 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6107 if (!IS_OFDM(rts_rspec[1]) &&
6108 !((RSPEC2RATE(rts_rspec[1]) == WLC_RATE_1M) ||
6109 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6110 rts_preamble_type[1] = WLC_SHORT_PREAMBLE;
6111 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6114 /* RTS/CTS additions to MacTxControlLow */
6115 if (use_cts) {
6116 txh->MacTxControlLow |= htol16(TXC_SENDCTS);
6117 } else {
6118 txh->MacTxControlLow |= htol16(TXC_SENDRTS);
6119 txh->MacTxControlLow |= htol16(TXC_LONGFRAME);
6122 /* RTS PLCP header */
6123 ASSERT(IS_ALIGNED((unsigned long)txh->RTSPhyHeader, sizeof(u16)));
6124 rts_plcp = txh->RTSPhyHeader;
6125 if (use_cts)
6126 rts_phylen = DOT11_CTS_LEN + FCS_LEN;
6127 else
6128 rts_phylen = DOT11_RTS_LEN + FCS_LEN;
6130 wlc_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6132 /* fallback rate version of RTS PLCP header */
6133 wlc_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6134 rts_plcp_fallback);
6135 bcopy(rts_plcp_fallback, (char *)&txh->RTSPLCPFallback,
6136 sizeof(txh->RTSPLCPFallback));
6138 /* RTS frame fields... */
6139 rts = (struct ieee80211_rts *)&txh->rts_frame;
6141 durid = wlc_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6142 rspec[0], rts_preamble_type[0],
6143 preamble_type[0], phylen, false);
6144 rts->duration = htol16(durid);
6145 /* fallback rate version of RTS DUR field */
6146 durid = wlc_compute_rtscts_dur(wlc, use_cts,
6147 rts_rspec[1], rspec[1],
6148 rts_preamble_type[1],
6149 preamble_type[1], phylen, false);
6150 txh->RTSDurFallback = htol16(durid);
6152 if (use_cts) {
6153 rts->frame_control = htol16(FC_CTS);
6154 bcopy((char *)&h->addr2, (char *)&rts->ra, ETH_ALEN);
6155 } else {
6156 rts->frame_control = htol16((u16) FC_RTS);
6157 bcopy((char *)&h->addr1, (char *)&rts->ra,
6158 2 * ETH_ALEN);
6161 /* mainrate
6162 * low 8 bits: main frag rate/mcs,
6163 * high 8 bits: rts/cts rate/mcs
6165 mainrates |= (IS_OFDM(rts_rspec[0]) ?
6166 D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) rts_plcp) :
6167 rts_plcp[0]) << 8;
6168 } else {
6169 memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6170 memset((char *)&txh->rts_frame, 0,
6171 sizeof(struct ieee80211_rts));
6172 memset((char *)txh->RTSPLCPFallback, 0,
6173 sizeof(txh->RTSPLCPFallback));
6174 txh->RTSDurFallback = 0;
6177 #ifdef SUPPORT_40MHZ
6178 /* add null delimiter count */
6179 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && IS_MCS(rspec)) {
6180 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6181 wlc_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6183 #endif
6185 /* Now that RTS/RTS FB preamble types are updated, write the final value */
6186 txh->MacTxControlHigh = htol16(mch);
6188 /* MainRates (both the rts and frag plcp rates have been calculated now) */
6189 txh->MainRates = htol16(mainrates);
6191 /* XtraFrameTypes */
6192 xfts = FRAMETYPE(rspec[1], wlc->mimoft);
6193 xfts |= (FRAMETYPE(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6194 xfts |= (FRAMETYPE(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6195 xfts |=
6196 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC) << XFTS_CHANNEL_SHIFT;
6197 txh->XtraFrameTypes = htol16(xfts);
6199 /* PhyTxControlWord */
6200 phyctl = FRAMETYPE(rspec[0], wlc->mimoft);
6201 if ((preamble_type[0] == WLC_SHORT_PREAMBLE) ||
6202 (preamble_type[0] == WLC_GF_PREAMBLE)) {
6203 ASSERT((preamble_type[0] == WLC_GF_PREAMBLE)
6204 || !IS_MCS(rspec[0]));
6205 if (RSPEC2RATE(rspec[0]) != WLC_RATE_1M)
6206 phyctl |= PHY_TXC_SHORT_HDR;
6207 WLCNTINCR(wlc->pub->_cnt->txprshort);
6210 /* phytxant is properly bit shifted */
6211 phyctl |= wlc_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6212 txh->PhyTxControlWord = htol16(phyctl);
6214 /* PhyTxControlWord_1 */
6215 if (WLC_PHY_11N_CAP(wlc->band)) {
6216 u16 phyctl1 = 0;
6218 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[0]);
6219 txh->PhyTxControlWord_1 = htol16(phyctl1);
6220 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[1]);
6221 txh->PhyTxControlWord_1_Fbr = htol16(phyctl1);
6223 if (use_rts || use_cts) {
6224 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[0]);
6225 txh->PhyTxControlWord_1_Rts = htol16(phyctl1);
6226 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[1]);
6227 txh->PhyTxControlWord_1_FbrRts = htol16(phyctl1);
6231 * For mcs frames, if mixedmode(overloaded with long preamble) is going to be set,
6232 * fill in non-zero MModeLen and/or MModeFbrLen
6233 * it will be unnecessary if they are separated
6235 if (IS_MCS(rspec[0]) && (preamble_type[0] == WLC_MM_PREAMBLE)) {
6236 u16 mmodelen =
6237 wlc_calc_lsig_len(wlc, rspec[0], phylen);
6238 txh->MModeLen = htol16(mmodelen);
6241 if (IS_MCS(rspec[1]) && (preamble_type[1] == WLC_MM_PREAMBLE)) {
6242 u16 mmodefbrlen =
6243 wlc_calc_lsig_len(wlc, rspec[1], phylen);
6244 txh->MModeFbrLen = htol16(mmodefbrlen);
6248 if (IS_MCS(rspec[0]))
6249 ASSERT(IS_MCS(rspec[1]));
6251 ASSERT(!IS_MCS(rspec[0]) ||
6252 ((preamble_type[0] == WLC_MM_PREAMBLE) == (txh->MModeLen != 0)));
6253 ASSERT(!IS_MCS(rspec[1]) ||
6254 ((preamble_type[1] == WLC_MM_PREAMBLE) ==
6255 (txh->MModeFbrLen != 0)));
6257 ac = wme_fifo2ac[queue];
6258 if (SCB_WME(scb) && qos && wlc->edcf_txop[ac]) {
6259 uint frag_dur, dur, dur_fallback;
6261 ASSERT(!is_multicast_ether_addr(h->addr1));
6263 /* WME: Update TXOP threshold */
6264 if ((!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) && (frag == 0)) {
6265 frag_dur =
6266 wlc_calc_frame_time(wlc, rspec[0], preamble_type[0],
6267 phylen);
6269 if (rts) {
6270 /* 1 RTS or CTS-to-self frame */
6271 dur =
6272 wlc_calc_cts_time(wlc, rts_rspec[0],
6273 rts_preamble_type[0]);
6274 dur_fallback =
6275 wlc_calc_cts_time(wlc, rts_rspec[1],
6276 rts_preamble_type[1]);
6277 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
6278 dur += ltoh16(rts->duration);
6279 dur_fallback += ltoh16(txh->RTSDurFallback);
6280 } else if (use_rifs) {
6281 dur = frag_dur;
6282 dur_fallback = 0;
6283 } else {
6284 /* frame + SIFS + ACK */
6285 dur = frag_dur;
6286 dur +=
6287 wlc_compute_frame_dur(wlc, rspec[0],
6288 preamble_type[0], 0);
6290 dur_fallback =
6291 wlc_calc_frame_time(wlc, rspec[1],
6292 preamble_type[1],
6293 phylen);
6294 dur_fallback +=
6295 wlc_compute_frame_dur(wlc, rspec[1],
6296 preamble_type[1], 0);
6298 /* NEED to set TxFesTimeNormal (hard) */
6299 txh->TxFesTimeNormal = htol16((u16) dur);
6300 /* NEED to set fallback rate version of TxFesTimeNormal (hard) */
6301 txh->TxFesTimeFallback = htol16((u16) dur_fallback);
6303 /* update txop byte threshold (txop minus intraframe overhead) */
6304 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6306 uint newfragthresh;
6308 newfragthresh =
6309 wlc_calc_frame_len(wlc, rspec[0],
6310 preamble_type[0],
6311 (wlc->
6312 edcf_txop[ac] -
6313 (dur -
6314 frag_dur)));
6315 /* range bound the fragthreshold */
6316 if (newfragthresh < DOT11_MIN_FRAG_LEN)
6317 newfragthresh =
6318 DOT11_MIN_FRAG_LEN;
6319 else if (newfragthresh >
6320 wlc->usr_fragthresh)
6321 newfragthresh =
6322 wlc->usr_fragthresh;
6323 /* update the fragthresh and do txc update */
6324 if (wlc->fragthresh[queue] !=
6325 (u16) newfragthresh) {
6326 wlc->fragthresh[queue] =
6327 (u16) newfragthresh;
6330 #if defined(BCMDBG)
6331 } else
6332 WL_ERROR("wl%d: %s txop invalid for rate %d\n",
6333 wlc->pub->unit, fifo_names[queue],
6334 RSPEC2RATE(rspec[0]));
6336 if (dur > wlc->edcf_txop[ac])
6337 WL_ERROR("wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
6338 wlc->pub->unit, __func__,
6339 fifo_names[queue],
6340 phylen, wlc->fragthresh[queue],
6341 dur, wlc->edcf_txop[ac]);
6342 #else
6344 #endif
6348 return 0;
6351 void wlc_tbtt(struct wlc_info *wlc, d11regs_t *regs)
6353 wlc_bsscfg_t *cfg = wlc->cfg;
6355 WLCNTINCR(wlc->pub->_cnt->tbtt);
6357 if (BSSCFG_STA(cfg)) {
6358 /* run watchdog here if the watchdog timer is not armed */
6359 if (WLC_WATCHDOG_TBTT(wlc)) {
6360 u32 cur, delta;
6361 if (wlc->WDarmed) {
6362 wl_del_timer(wlc->wl, wlc->wdtimer);
6363 wlc->WDarmed = false;
6366 cur = OSL_SYSUPTIME();
6367 delta = cur > wlc->WDlast ? cur - wlc->WDlast :
6368 (u32) ~0 - wlc->WDlast + cur + 1;
6369 if (delta >= TIMER_INTERVAL_WATCHDOG) {
6370 wlc_watchdog((void *)wlc);
6371 wlc->WDlast = cur;
6374 wl_add_timer(wlc->wl, wlc->wdtimer,
6375 wlc_watchdog_backup_bi(wlc), true);
6376 wlc->WDarmed = true;
6380 if (!cfg->BSS) {
6381 /* DirFrmQ is now valid...defer setting until end of ATIM window */
6382 wlc->qvalid |= MCMD_DIRFRMQVAL;
6386 /* GP timer is a freerunning 32 bit counter, decrements at 1 us rate */
6387 void wlc_hwtimer_gptimer_set(struct wlc_info *wlc, uint us)
6389 ASSERT(wlc->pub->corerev >= 3); /* no gptimer in earlier revs */
6390 W_REG(wlc->osh, &wlc->regs->gptimer, us);
6393 void wlc_hwtimer_gptimer_abort(struct wlc_info *wlc)
6395 ASSERT(wlc->pub->corerev >= 3);
6396 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6399 static void wlc_hwtimer_gptimer_cb(struct wlc_info *wlc)
6401 /* when interrupt is generated, the counter is loaded with last value
6402 * written and continue to decrement. So it has to be cleaned first
6404 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6408 * This fn has all the high level dpc processing from wlc_dpc.
6409 * POLICY: no macinstatus change, no bounding loop.
6410 * All dpc bounding should be handled in BMAC dpc, like txstatus and rxint
6412 void wlc_high_dpc(struct wlc_info *wlc, u32 macintstatus)
6414 d11regs_t *regs = wlc->regs;
6415 #ifdef BCMDBG
6416 char flagstr[128];
6417 static const bcm_bit_desc_t int_flags[] = {
6418 {MI_MACSSPNDD, "MACSSPNDD"},
6419 {MI_BCNTPL, "BCNTPL"},
6420 {MI_TBTT, "TBTT"},
6421 {MI_BCNSUCCESS, "BCNSUCCESS"},
6422 {MI_BCNCANCLD, "BCNCANCLD"},
6423 {MI_ATIMWINEND, "ATIMWINEND"},
6424 {MI_PMQ, "PMQ"},
6425 {MI_NSPECGEN_0, "NSPECGEN_0"},
6426 {MI_NSPECGEN_1, "NSPECGEN_1"},
6427 {MI_MACTXERR, "MACTXERR"},
6428 {MI_NSPECGEN_3, "NSPECGEN_3"},
6429 {MI_PHYTXERR, "PHYTXERR"},
6430 {MI_PME, "PME"},
6431 {MI_GP0, "GP0"},
6432 {MI_GP1, "GP1"},
6433 {MI_DMAINT, "DMAINT"},
6434 {MI_TXSTOP, "TXSTOP"},
6435 {MI_CCA, "CCA"},
6436 {MI_BG_NOISE, "BG_NOISE"},
6437 {MI_DTIM_TBTT, "DTIM_TBTT"},
6438 {MI_PRQ, "PRQ"},
6439 {MI_PWRUP, "PWRUP"},
6440 {MI_RFDISABLE, "RFDISABLE"},
6441 {MI_TFS, "TFS"},
6442 {MI_PHYCHANGED, "PHYCHANGED"},
6443 {MI_TO, "TO"},
6444 {0, NULL}
6447 if (macintstatus & ~(MI_TBTT | MI_TXSTOP)) {
6448 bcm_format_flags(int_flags, macintstatus, flagstr,
6449 sizeof(flagstr));
6450 WL_TRACE("wl%d: macintstatus 0x%x %s\n",
6451 wlc->pub->unit, macintstatus, flagstr);
6453 #endif /* BCMDBG */
6455 if (macintstatus & MI_PRQ) {
6456 /* Process probe request FIFO */
6457 ASSERT(0 && "PRQ Interrupt in non-MBSS");
6460 /* TBTT indication */
6461 /* ucode only gives either TBTT or DTIM_TBTT, not both */
6462 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
6463 wlc_tbtt(wlc, regs);
6465 if (macintstatus & MI_GP0) {
6466 WL_ERROR("wl%d: PSM microcode watchdog fired at %d (seconds). Resetting.\n",
6467 wlc->pub->unit, wlc->pub->now);
6469 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
6470 __func__, wlc->pub->sih->chip,
6471 wlc->pub->sih->chiprev);
6473 WLCNTINCR(wlc->pub->_cnt->psmwds);
6475 /* big hammer */
6476 wl_init(wlc->wl);
6479 /* gptimer timeout */
6480 if (macintstatus & MI_TO) {
6481 wlc_hwtimer_gptimer_cb(wlc);
6484 if (macintstatus & MI_RFDISABLE) {
6485 WL_ERROR("wl%d: MAC Detected a change on the RF Disable Input 0x%x\n",
6486 wlc->pub->unit,
6487 R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD);
6488 /* delay the cleanup to wl_down in IBSS case */
6489 if ((R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD)) {
6490 int idx;
6491 wlc_bsscfg_t *bsscfg;
6492 FOREACH_BSS(wlc, idx, bsscfg) {
6493 if (!BSSCFG_STA(bsscfg) || !bsscfg->enable
6494 || !bsscfg->BSS)
6495 continue;
6496 WL_ERROR("wl%d: wlc_dpc: rfdisable -> wlc_bsscfg_disable()\n",
6497 wlc->pub->unit);
6502 /* send any enq'd tx packets. Just makes sure to jump start tx */
6503 if (!pktq_empty(&wlc->active_queue->q))
6504 wlc_send_q(wlc, wlc->active_queue);
6506 ASSERT(wlc_ps_check(wlc));
6509 static void *wlc_15420war(struct wlc_info *wlc, uint queue)
6511 struct hnddma_pub *di;
6512 void *p;
6514 ASSERT(queue < NFIFO);
6516 if ((D11REV_IS(wlc->pub->corerev, 4))
6517 || (D11REV_GT(wlc->pub->corerev, 6)))
6518 return NULL;
6520 di = wlc->hw->di[queue];
6521 ASSERT(di != NULL);
6523 /* get next packet, ignoring XmtStatus.Curr */
6524 p = dma_getnexttxp(di, HNDDMA_RANGE_ALL);
6526 /* sw block tx dma */
6527 dma_txblock(di);
6529 /* if tx ring is now empty, reset and re-init the tx dma channel */
6530 if (dma_txactive(wlc->hw->di[queue]) == 0) {
6531 WLCNTINCR(wlc->pub->_cnt->txdmawar);
6532 if (!dma_txreset(di))
6533 WL_ERROR("wl%d: %s: dma_txreset[%d]: cannot stop dma\n",
6534 wlc->pub->unit, __func__, queue);
6535 dma_txinit(di);
6537 return p;
6540 static void wlc_war16165(struct wlc_info *wlc, bool tx)
6542 if (tx) {
6543 /* the post-increment is used in STAY_AWAKE macro */
6544 if (wlc->txpend16165war++ == 0)
6545 wlc_set_ps_ctrl(wlc);
6546 } else {
6547 wlc->txpend16165war--;
6548 if (wlc->txpend16165war == 0)
6549 wlc_set_ps_ctrl(wlc);
6553 /* process an individual tx_status_t */
6554 /* WLC_HIGH_API */
6555 bool BCMFASTPATH
6556 wlc_dotxstatus(struct wlc_info *wlc, tx_status_t *txs, u32 frm_tx2)
6558 struct sk_buff *p;
6559 uint queue;
6560 d11txh_t *txh;
6561 struct scb *scb = NULL;
6562 bool free_pdu;
6563 struct osl_info *osh;
6564 int tx_rts, tx_frame_count, tx_rts_count;
6565 uint totlen, supr_status;
6566 bool lastframe;
6567 struct ieee80211_hdr *h;
6568 u16 fc;
6569 u16 mcl;
6570 struct ieee80211_tx_info *tx_info;
6571 struct ieee80211_tx_rate *txrate;
6572 int i;
6574 (void)(frm_tx2); /* Compiler reference to avoid unused variable warning */
6576 /* discard intermediate indications for ucode with one legitimate case:
6577 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange, but the subsequent
6578 * tx of DATA failed. so it will start rts/cts from the beginning (resetting the rts
6579 * transmission count)
6581 if (!(txs->status & TX_STATUS_AMPDU)
6582 && (txs->status & TX_STATUS_INTERMEDIATE)) {
6583 WLCNTADD(wlc->pub->_cnt->txnoack,
6584 ((txs->
6585 status & TX_STATUS_FRM_RTX_MASK) >>
6586 TX_STATUS_FRM_RTX_SHIFT));
6587 WL_ERROR("%s: INTERMEDIATE but not AMPDU\n", __func__);
6588 return false;
6591 osh = wlc->osh;
6592 queue = txs->frameid & TXFID_QUEUE_MASK;
6593 ASSERT(queue < NFIFO);
6594 if (queue >= NFIFO) {
6595 p = NULL;
6596 goto fatal;
6599 p = GETNEXTTXP(wlc, queue);
6600 if (WLC_WAR16165(wlc))
6601 wlc_war16165(wlc, false);
6602 if (p == NULL)
6603 p = wlc_15420war(wlc, queue);
6604 ASSERT(p != NULL);
6605 if (p == NULL)
6606 goto fatal;
6608 txh = (d11txh_t *) (p->data);
6609 mcl = ltoh16(txh->MacTxControlLow);
6611 if (txs->phyerr) {
6612 WL_ERROR("phyerr 0x%x, rate 0x%x\n",
6613 txs->phyerr, txh->MainRates);
6614 #if defined(BCMDBG)
6615 wlc_print_txdesc(txh);
6616 #endif
6617 wlc_print_txstatus(txs);
6620 ASSERT(txs->frameid == htol16(txh->TxFrameID));
6621 if (txs->frameid != htol16(txh->TxFrameID))
6622 goto fatal;
6624 tx_info = IEEE80211_SKB_CB(p);
6625 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
6626 fc = ltoh16(h->frame_control);
6628 scb = (struct scb *)tx_info->control.sta->drv_priv;
6630 if (N_ENAB(wlc->pub)) {
6631 u8 *plcp = (u8 *) (txh + 1);
6632 if (PLCP3_ISSGI(plcp[3]))
6633 WLCNTINCR(wlc->pub->_cnt->txmpdu_sgi);
6634 if (PLCP3_ISSTBC(plcp[3]))
6635 WLCNTINCR(wlc->pub->_cnt->txmpdu_stbc);
6638 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
6639 ASSERT((mcl & TXC_AMPDU_MASK) != TXC_AMPDU_NONE);
6640 wlc_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
6641 return false;
6644 supr_status = txs->status & TX_STATUS_SUPR_MASK;
6645 if (supr_status == TX_STATUS_SUPR_BADCH)
6646 WL_NONE("%s: Pkt tx suppressed, possibly channel %d\n",
6647 __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
6649 tx_rts = htol16(txh->MacTxControlLow) & TXC_SENDRTS;
6650 tx_frame_count =
6651 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
6652 tx_rts_count =
6653 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
6655 lastframe = (fc & IEEE80211_FCTL_MOREFRAGS) == 0;
6657 if (!lastframe) {
6658 WL_ERROR("Not last frame!\n");
6659 } else {
6660 u16 sfbl, lfbl;
6661 ieee80211_tx_info_clear_status(tx_info);
6662 if (queue < AC_COUNT) {
6663 sfbl = WLC_WME_RETRY_SFB_GET(wlc, wme_fifo2ac[queue]);
6664 lfbl = WLC_WME_RETRY_LFB_GET(wlc, wme_fifo2ac[queue]);
6665 } else {
6666 sfbl = wlc->SFBL;
6667 lfbl = wlc->LFBL;
6670 txrate = tx_info->status.rates;
6671 /* FIXME: this should use a combination of sfbl, lfbl depending on frame length and RTS setting */
6672 if ((tx_frame_count > sfbl) && (txrate[1].idx >= 0)) {
6673 /* rate selection requested a fallback rate and we used it */
6674 txrate->count = lfbl;
6675 txrate[1].count = tx_frame_count - lfbl;
6676 } else {
6677 /* rate selection did not request fallback rate, or we didn't need it */
6678 txrate->count = tx_frame_count;
6679 /* rc80211_minstrel.c:minstrel_tx_status() expects unused rates to be marked with idx = -1 */
6680 txrate[1].idx = -1;
6681 txrate[1].count = 0;
6684 /* clear the rest of the rates */
6685 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
6686 txrate[i].idx = -1;
6687 txrate[i].count = 0;
6690 if (txs->status & TX_STATUS_ACK_RCV)
6691 tx_info->flags |= IEEE80211_TX_STAT_ACK;
6694 totlen = pkttotlen(osh, p);
6695 free_pdu = true;
6697 wlc_txfifo_complete(wlc, queue, 1);
6699 if (lastframe) {
6700 p->next = NULL;
6701 p->prev = NULL;
6702 wlc->txretried = 0;
6703 /* remove PLCP & Broadcom tx descriptor header */
6704 skb_pull(p, D11_PHY_HDR_LEN);
6705 skb_pull(p, D11_TXH_LEN);
6706 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
6707 WLCNTINCR(wlc->pub->_cnt->ieee_tx_status);
6708 } else {
6709 WL_ERROR("%s: Not last frame => not calling tx_status\n",
6710 __func__);
6713 return false;
6715 fatal:
6716 ASSERT(0);
6717 if (p)
6718 pkt_buf_free_skb(osh, p, true);
6720 return true;
6724 void BCMFASTPATH
6725 wlc_txfifo_complete(struct wlc_info *wlc, uint fifo, s8 txpktpend)
6727 TXPKTPENDDEC(wlc, fifo, txpktpend);
6728 WL_TRACE("wlc_txfifo_complete, pktpend dec %d to %d\n",
6729 txpktpend, TXPKTPENDGET(wlc, fifo));
6731 /* There is more room; mark precedences related to this FIFO sendable */
6732 WLC_TX_FIFO_ENAB(wlc, fifo);
6733 ASSERT(TXPKTPENDGET(wlc, fifo) >= 0);
6735 if (!TXPKTPENDTOT(wlc)) {
6736 if (wlc->block_datafifo & DATA_BLOCK_TX_SUPR)
6737 wlc_bsscfg_tx_check(wlc);
6740 /* Clear MHF2_TXBCMC_NOW flag if BCMC fifo has drained */
6741 if (AP_ENAB(wlc->pub) &&
6742 wlc->bcmcfifo_drain && !TXPKTPENDGET(wlc, TX_BCMC_FIFO)) {
6743 wlc->bcmcfifo_drain = false;
6744 wlc_mhf(wlc, MHF2, MHF2_TXBCMC_NOW, 0, WLC_BAND_AUTO);
6747 /* figure out which bsscfg is being worked on... */
6750 /* Given the beacon interval in kus, and a 64 bit TSF in us,
6751 * return the offset (in us) of the TSF from the last TBTT
6753 u32 wlc_calc_tbtt_offset(u32 bp, u32 tsf_h, u32 tsf_l)
6755 u32 k, btklo, btkhi, offset;
6757 /* TBTT is always an even multiple of the beacon_interval,
6758 * so the TBTT less than or equal to the beacon timestamp is
6759 * the beacon timestamp minus the beacon timestamp modulo
6760 * the beacon interval.
6762 * TBTT = BT - (BT % BIu)
6763 * = (BTk - (BTk % BP)) * 2^10
6765 * BT = beacon timestamp (usec, 64bits)
6766 * BTk = beacon timestamp (Kusec, 54bits)
6767 * BP = beacon interval (Kusec, 16bits)
6768 * BIu = BP * 2^10 = beacon interval (usec, 26bits)
6770 * To keep the calculations in u32s, the modulo operation
6771 * on the high part of BT needs to be done in parts using the
6772 * relations:
6773 * X*Y mod Z = ((X mod Z) * (Y mod Z)) mod Z
6774 * and
6775 * (X + Y) mod Z = ((X mod Z) + (Y mod Z)) mod Z
6777 * So, if BTk[n] = u16 n [0,3] of BTk.
6778 * BTk % BP = SUM((BTk[n] * 2^16n) % BP , 0<=n<4) % BP
6779 * and the SUM term can be broken down:
6780 * (BTk[n] * 2^16n) % BP
6781 * (BTk[n] * (2^16n % BP)) % BP
6783 * Create a set of power of 2 mod BP constants:
6784 * K[n] = 2^(16n) % BP
6785 * = (K[n-1] * 2^16) % BP
6786 * K[2] = 2^32 % BP = ((2^16 % BP) * 2^16) % BP
6788 * BTk % BP = BTk[0-1] % BP +
6789 * (BTk[2] * K[2]) % BP +
6790 * (BTk[3] * K[3]) % BP
6792 * Since K[n] < 2^16 and BTk[n] is < 2^16, then BTk[n] * K[n] < 2^32
6795 /* BTk = BT >> 10, btklo = BTk[0-3], bkthi = BTk[4-6] */
6796 btklo = (tsf_h << 22) | (tsf_l >> 10);
6797 btkhi = tsf_h >> 10;
6799 /* offset = BTk % BP */
6800 offset = btklo % bp;
6802 /* K[2] = ((2^16 % BP) * 2^16) % BP */
6803 k = (u32) (1 << 16) % bp;
6804 k = (u32) (k * 1 << 16) % (u32) bp;
6806 /* offset += (BTk[2] * K[2]) % BP */
6807 offset += ((btkhi & 0xffff) * k) % bp;
6809 /* BTk[3] */
6810 btkhi = btkhi >> 16;
6812 /* k[3] = (K[2] * 2^16) % BP */
6813 k = (k << 16) % bp;
6815 /* offset += (BTk[3] * K[3]) % BP */
6816 offset += ((btkhi & 0xffff) * k) % bp;
6818 offset = offset % bp;
6820 /* convert offset from kus to us by shifting up 10 bits and
6821 * add in the low 10 bits of tsf that we ignored
6823 offset = (offset << 10) + (tsf_l & 0x3FF);
6825 return offset;
6828 /* Update beacon listen interval in shared memory */
6829 void wlc_bcn_li_upd(struct wlc_info *wlc)
6831 if (AP_ENAB(wlc->pub))
6832 return;
6834 /* wake up every DTIM is the default */
6835 if (wlc->bcn_li_dtim == 1)
6836 wlc_write_shm(wlc, M_BCN_LI, 0);
6837 else
6838 wlc_write_shm(wlc, M_BCN_LI,
6839 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
6842 static void
6843 prep_mac80211_status(struct wlc_info *wlc, d11rxhdr_t *rxh, struct sk_buff *p,
6844 struct ieee80211_rx_status *rx_status)
6846 u32 tsf_l, tsf_h;
6847 wlc_d11rxhdr_t *wlc_rxh = (wlc_d11rxhdr_t *) rxh;
6848 int preamble;
6849 int channel;
6850 ratespec_t rspec;
6851 unsigned char *plcp;
6853 wlc_read_tsf(wlc, &tsf_l, &tsf_h); /* mactime */
6854 rx_status->mactime = tsf_h;
6855 rx_status->mactime <<= 32;
6856 rx_status->mactime |= tsf_l;
6857 rx_status->flag |= RX_FLAG_TSFT;
6859 channel = WLC_CHAN_CHANNEL(rxh->RxChan);
6861 /* XXX Channel/badn needs to be filtered against whether we are single/dual band card */
6862 if (channel > 14) {
6863 rx_status->band = IEEE80211_BAND_5GHZ;
6864 rx_status->freq = ieee80211_ofdm_chan_to_freq(
6865 WF_CHAN_FACTOR_5_G/2, channel);
6867 } else {
6868 rx_status->band = IEEE80211_BAND_2GHZ;
6869 rx_status->freq = ieee80211_dsss_chan_to_freq(channel);
6872 rx_status->signal = wlc_rxh->rssi; /* signal */
6874 /* noise */
6875 /* qual */
6876 rx_status->antenna = (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0; /* ant */
6878 plcp = p->data;
6880 rspec = wlc_compute_rspec(rxh, plcp);
6881 if (IS_MCS(rspec)) {
6882 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
6883 rx_status->flag |= RX_FLAG_HT;
6884 if (RSPEC_IS40MHZ(rspec))
6885 rx_status->flag |= RX_FLAG_40MHZ;
6886 } else {
6887 switch (RSPEC2RATE(rspec)) {
6888 case WLC_RATE_1M:
6889 rx_status->rate_idx = 0;
6890 break;
6891 case WLC_RATE_2M:
6892 rx_status->rate_idx = 1;
6893 break;
6894 case WLC_RATE_5M5:
6895 rx_status->rate_idx = 2;
6896 break;
6897 case WLC_RATE_11M:
6898 rx_status->rate_idx = 3;
6899 break;
6900 case WLC_RATE_6M:
6901 rx_status->rate_idx = 4;
6902 break;
6903 case WLC_RATE_9M:
6904 rx_status->rate_idx = 5;
6905 break;
6906 case WLC_RATE_12M:
6907 rx_status->rate_idx = 6;
6908 break;
6909 case WLC_RATE_18M:
6910 rx_status->rate_idx = 7;
6911 break;
6912 case WLC_RATE_24M:
6913 rx_status->rate_idx = 8;
6914 break;
6915 case WLC_RATE_36M:
6916 rx_status->rate_idx = 9;
6917 break;
6918 case WLC_RATE_48M:
6919 rx_status->rate_idx = 10;
6920 break;
6921 case WLC_RATE_54M:
6922 rx_status->rate_idx = 11;
6923 break;
6924 default:
6925 WL_ERROR("%s: Unknown rate\n", __func__);
6928 /* Determine short preamble and rate_idx */
6929 preamble = 0;
6930 if (IS_CCK(rspec)) {
6931 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
6932 WL_ERROR("Short CCK\n");
6933 rx_status->flag |= RX_FLAG_SHORTPRE;
6934 } else if (IS_OFDM(rspec)) {
6935 rx_status->flag |= RX_FLAG_SHORTPRE;
6936 } else {
6937 WL_ERROR("%s: Unknown modulation\n", __func__);
6941 if (PLCP3_ISSGI(plcp[3]))
6942 rx_status->flag |= RX_FLAG_SHORT_GI;
6944 if (rxh->RxStatus1 & RXS_DECERR) {
6945 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
6946 WL_ERROR("%s: RX_FLAG_FAILED_PLCP_CRC\n", __func__);
6948 if (rxh->RxStatus1 & RXS_FCSERR) {
6949 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
6950 WL_ERROR("%s: RX_FLAG_FAILED_FCS_CRC\n", __func__);
6954 static void
6955 wlc_recvctl(struct wlc_info *wlc, struct osl_info *osh, d11rxhdr_t *rxh,
6956 struct sk_buff *p)
6958 int len_mpdu;
6959 struct ieee80211_rx_status rx_status;
6960 #if defined(BCMDBG)
6961 struct sk_buff *skb = p;
6962 #endif /* BCMDBG */
6963 /* Todo:
6964 * Cache plcp for first MPDU of AMPD and use chacched version for INTERMEDIATE.
6965 * Test for INTERMEDIATE like so:
6966 * if (!(plcp[0] | plcp[1] | plcp[2]))
6969 memset(&rx_status, 0, sizeof(rx_status));
6970 prep_mac80211_status(wlc, rxh, p, &rx_status);
6972 /* mac header+body length, exclude CRC and plcp header */
6973 len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
6974 skb_pull(p, D11_PHY_HDR_LEN);
6975 __skb_trim(p, len_mpdu);
6977 ASSERT(!(p->next));
6978 ASSERT(!(p->prev));
6980 ASSERT(IS_ALIGNED((unsigned long)skb->data, 2));
6982 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
6983 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
6985 WLCNTINCR(wlc->pub->_cnt->ieee_rx);
6986 osh->pktalloced--;
6987 return;
6990 void wlc_bss_list_free(struct wlc_info *wlc, wlc_bss_list_t *bss_list)
6992 uint index;
6993 wlc_bss_info_t *bi;
6995 if (!bss_list) {
6996 WL_ERROR("%s: Attempting to free NULL list\n", __func__);
6997 return;
6999 /* inspect all BSS descriptor */
7000 for (index = 0; index < bss_list->count; index++) {
7001 bi = bss_list->ptrs[index];
7002 if (bi) {
7003 kfree(bi);
7004 bss_list->ptrs[index] = NULL;
7007 bss_list->count = 0;
7010 /* Process received frames */
7012 * Return true if more frames need to be processed. false otherwise.
7013 * Param 'bound' indicates max. # frames to process before break out.
7015 /* WLC_HIGH_API */
7016 void BCMFASTPATH wlc_recv(struct wlc_info *wlc, struct sk_buff *p)
7018 d11rxhdr_t *rxh;
7019 struct ieee80211_hdr *h;
7020 struct osl_info *osh;
7021 u16 fc;
7022 uint len;
7023 bool is_amsdu;
7025 WL_TRACE("wl%d: wlc_recv\n", wlc->pub->unit);
7027 osh = wlc->osh;
7029 /* frame starts with rxhdr */
7030 rxh = (d11rxhdr_t *) (p->data);
7032 /* strip off rxhdr */
7033 skb_pull(p, wlc->hwrxoff);
7035 /* fixup rx header endianness */
7036 ltoh16_buf((void *)rxh, sizeof(d11rxhdr_t));
7038 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7039 if (rxh->RxStatus1 & RXS_PBPRES) {
7040 if (p->len < 2) {
7041 WLCNTINCR(wlc->pub->_cnt->rxrunt);
7042 WL_ERROR("wl%d: wlc_recv: rcvd runt of len %d\n",
7043 wlc->pub->unit, p->len);
7044 goto toss;
7046 skb_pull(p, 2);
7049 h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
7050 len = p->len;
7052 if (rxh->RxStatus1 & RXS_FCSERR) {
7053 if (wlc->pub->mac80211_state & MAC80211_PROMISC_BCNS) {
7054 WL_ERROR("FCSERR while scanning******* - tossing\n");
7055 goto toss;
7056 } else {
7057 WL_ERROR("RCSERR!!!\n");
7058 goto toss;
7062 /* check received pkt has at least frame control field */
7063 if (len >= D11_PHY_HDR_LEN + sizeof(h->frame_control)) {
7064 fc = ltoh16(h->frame_control);
7065 } else {
7066 WLCNTINCR(wlc->pub->_cnt->rxrunt);
7067 goto toss;
7070 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7072 /* explicitly test bad src address to avoid sending bad deauth */
7073 if (!is_amsdu) {
7074 /* CTS and ACK CTL frames are w/o a2 */
7075 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
7076 (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
7077 if ((is_zero_ether_addr(h->addr2) ||
7078 is_multicast_ether_addr(h->addr2))) {
7079 WL_ERROR("wl%d: %s: dropping a frame with "
7080 "invalid src mac address, a2: %pM\n",
7081 wlc->pub->unit, __func__, h->addr2);
7082 WLCNTINCR(wlc->pub->_cnt->rxbadsrcmac);
7083 goto toss;
7085 WLCNTINCR(wlc->pub->_cnt->rxfrag);
7089 /* due to sheer numbers, toss out probe reqs for now */
7090 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
7091 if ((fc & FC_KIND_MASK) == FC_PROBE_REQ)
7092 goto toss;
7095 if (is_amsdu) {
7096 WL_ERROR("%s: is_amsdu causing toss\n", __func__);
7097 goto toss;
7100 wlc_recvctl(wlc, osh, rxh, p);
7101 return;
7103 toss:
7104 pkt_buf_free_skb(osh, p, false);
7107 /* calculate frame duration for Mixed-mode L-SIG spoofing, return
7108 * number of bytes goes in the length field
7110 * Formula given by HT PHY Spec v 1.13
7111 * len = 3(nsyms + nstream + 3) - 3
7113 u16 BCMFASTPATH
7114 wlc_calc_lsig_len(struct wlc_info *wlc, ratespec_t ratespec, uint mac_len)
7116 uint nsyms, len = 0, kNdps;
7118 WL_TRACE("wl%d: wlc_calc_lsig_len: rate %d, len%d\n",
7119 wlc->pub->unit, RSPEC2RATE(ratespec), mac_len);
7121 if (IS_MCS(ratespec)) {
7122 uint mcs = ratespec & RSPEC_RATE_MASK;
7123 /* MCS_TXS(mcs) returns num tx streams - 1 */
7124 int tot_streams = (MCS_TXS(mcs) + 1) + RSPEC_STC(ratespec);
7126 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7127 /* the payload duration calculation matches that of regular ofdm */
7128 /* 1000Ndbps = kbps * 4 */
7129 kNdps =
7130 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7131 RSPEC_ISSGI(ratespec)) * 4;
7133 if (RSPEC_STC(ratespec) == 0)
7134 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7135 nsyms =
7136 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7137 APHY_TAIL_NBITS) * 1000, kNdps);
7138 else
7139 /* STBC needs to have even number of symbols */
7140 nsyms =
7142 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7143 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7145 nsyms += (tot_streams + 3); /* (+3) account for HT-SIG(2) and HT-STF(1) */
7146 /* 3 bytes/symbol @ legacy 6Mbps rate */
7147 len = (3 * nsyms) - 3; /* (-3) excluding service bits and tail bits */
7150 return (u16) len;
7153 /* calculate frame duration of a given rate and length, return time in usec unit */
7154 uint BCMFASTPATH
7155 wlc_calc_frame_time(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
7156 uint mac_len)
7158 uint nsyms, dur = 0, Ndps, kNdps;
7159 uint rate = RSPEC2RATE(ratespec);
7161 if (rate == 0) {
7162 ASSERT(0);
7163 WL_ERROR("wl%d: WAR: using rate of 1 mbps\n", wlc->pub->unit);
7164 rate = WLC_RATE_1M;
7167 WL_TRACE("wl%d: wlc_calc_frame_time: rspec 0x%x, preamble_type %d, len%d\n",
7168 wlc->pub->unit, ratespec, preamble_type, mac_len);
7170 if (IS_MCS(ratespec)) {
7171 uint mcs = ratespec & RSPEC_RATE_MASK;
7172 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7173 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7174 ASSERT(WLC_IS_MIMO_PREAMBLE(preamble_type));
7176 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7177 if (preamble_type == WLC_MM_PREAMBLE)
7178 dur += PREN_MM_EXT;
7179 /* 1000Ndbps = kbps * 4 */
7180 kNdps =
7181 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7182 RSPEC_ISSGI(ratespec)) * 4;
7184 if (RSPEC_STC(ratespec) == 0)
7185 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7186 nsyms =
7187 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7188 APHY_TAIL_NBITS) * 1000, kNdps);
7189 else
7190 /* STBC needs to have even number of symbols */
7191 nsyms =
7193 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7194 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7196 dur += APHY_SYMBOL_TIME * nsyms;
7197 if (BAND_2G(wlc->band->bandtype))
7198 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7199 } else if (IS_OFDM(rate)) {
7200 dur = APHY_PREAMBLE_TIME;
7201 dur += APHY_SIGNAL_TIME;
7202 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7203 Ndps = rate * 2;
7204 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7205 nsyms =
7206 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
7207 Ndps);
7208 dur += APHY_SYMBOL_TIME * nsyms;
7209 if (BAND_2G(wlc->band->bandtype))
7210 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7211 } else {
7212 /* calc # bits * 2 so factor of 2 in rate (1/2 mbps) will divide out */
7213 mac_len = mac_len * 8 * 2;
7214 /* calc ceiling of bits/rate = microseconds of air time */
7215 dur = (mac_len + rate - 1) / rate;
7216 if (preamble_type & WLC_SHORT_PREAMBLE)
7217 dur += BPHY_PLCP_SHORT_TIME;
7218 else
7219 dur += BPHY_PLCP_TIME;
7221 return dur;
7224 /* The opposite of wlc_calc_frame_time */
7225 static uint
7226 wlc_calc_frame_len(struct wlc_info *wlc, ratespec_t ratespec, u8 preamble_type,
7227 uint dur)
7229 uint nsyms, mac_len, Ndps, kNdps;
7230 uint rate = RSPEC2RATE(ratespec);
7232 WL_TRACE("wl%d: wlc_calc_frame_len: rspec 0x%x, preamble_type %d, dur %d\n",
7233 wlc->pub->unit, ratespec, preamble_type, dur);
7235 if (IS_MCS(ratespec)) {
7236 uint mcs = ratespec & RSPEC_RATE_MASK;
7237 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7238 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7239 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7240 /* payload calculation matches that of regular ofdm */
7241 if (BAND_2G(wlc->band->bandtype))
7242 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
7243 /* kNdbps = kbps * 4 */
7244 kNdps =
7245 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7246 RSPEC_ISSGI(ratespec)) * 4;
7247 nsyms = dur / APHY_SYMBOL_TIME;
7248 mac_len =
7249 ((nsyms * kNdps) -
7250 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
7251 } else if (IS_OFDM(ratespec)) {
7252 dur -= APHY_PREAMBLE_TIME;
7253 dur -= APHY_SIGNAL_TIME;
7254 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7255 Ndps = rate * 2;
7256 nsyms = dur / APHY_SYMBOL_TIME;
7257 mac_len =
7258 ((nsyms * Ndps) -
7259 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
7260 } else {
7261 if (preamble_type & WLC_SHORT_PREAMBLE)
7262 dur -= BPHY_PLCP_SHORT_TIME;
7263 else
7264 dur -= BPHY_PLCP_TIME;
7265 mac_len = dur * rate;
7266 /* divide out factor of 2 in rate (1/2 mbps) */
7267 mac_len = mac_len / 8 / 2;
7269 return mac_len;
7272 static uint
7273 wlc_calc_ba_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
7275 WL_TRACE("wl%d: wlc_calc_ba_time: rspec 0x%x, preamble_type %d\n",
7276 wlc->pub->unit, rspec, preamble_type);
7277 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7278 * or equal to the rate of the immediately previous frame in the FES
7280 rspec = WLC_BASIC_RATE(wlc, rspec);
7281 ASSERT(VALID_RATE_DBG(wlc, rspec));
7283 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
7284 return wlc_calc_frame_time(wlc, rspec, preamble_type,
7285 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
7286 FCS_LEN));
7289 static uint BCMFASTPATH
7290 wlc_calc_ack_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
7292 uint dur = 0;
7294 WL_TRACE("wl%d: wlc_calc_ack_time: rspec 0x%x, preamble_type %d\n",
7295 wlc->pub->unit, rspec, preamble_type);
7296 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7297 * or equal to the rate of the immediately previous frame in the FES
7299 rspec = WLC_BASIC_RATE(wlc, rspec);
7300 ASSERT(VALID_RATE_DBG(wlc, rspec));
7302 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
7303 dur =
7304 wlc_calc_frame_time(wlc, rspec, preamble_type,
7305 (DOT11_ACK_LEN + FCS_LEN));
7306 return dur;
7309 static uint
7310 wlc_calc_cts_time(struct wlc_info *wlc, ratespec_t rspec, u8 preamble_type)
7312 WL_TRACE("wl%d: wlc_calc_cts_time: ratespec 0x%x, preamble_type %d\n",
7313 wlc->pub->unit, rspec, preamble_type);
7314 return wlc_calc_ack_time(wlc, rspec, preamble_type);
7317 /* derive wlc->band->basic_rate[] table from 'rateset' */
7318 void wlc_rate_lookup_init(struct wlc_info *wlc, wlc_rateset_t *rateset)
7320 u8 rate;
7321 u8 mandatory;
7322 u8 cck_basic = 0;
7323 u8 ofdm_basic = 0;
7324 u8 *br = wlc->band->basic_rate;
7325 uint i;
7327 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
7328 memset(br, 0, WLC_MAXRATE + 1);
7330 /* For each basic rate in the rates list, make an entry in the
7331 * best basic lookup.
7333 for (i = 0; i < rateset->count; i++) {
7334 /* only make an entry for a basic rate */
7335 if (!(rateset->rates[i] & WLC_RATE_FLAG))
7336 continue;
7338 /* mask off basic bit */
7339 rate = (rateset->rates[i] & RATE_MASK);
7341 if (rate > WLC_MAXRATE) {
7342 WL_ERROR("wlc_rate_lookup_init: invalid rate 0x%X in rate set\n",
7343 rateset->rates[i]);
7344 continue;
7347 br[rate] = rate;
7350 /* The rate lookup table now has non-zero entries for each
7351 * basic rate, equal to the basic rate: br[basicN] = basicN
7353 * To look up the best basic rate corresponding to any
7354 * particular rate, code can use the basic_rate table
7355 * like this
7357 * basic_rate = wlc->band->basic_rate[tx_rate]
7359 * Make sure there is a best basic rate entry for
7360 * every rate by walking up the table from low rates
7361 * to high, filling in holes in the lookup table
7364 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
7365 rate = wlc->band->hw_rateset.rates[i];
7366 ASSERT(rate <= WLC_MAXRATE);
7368 if (br[rate] != 0) {
7369 /* This rate is a basic rate.
7370 * Keep track of the best basic rate so far by
7371 * modulation type.
7373 if (IS_OFDM(rate))
7374 ofdm_basic = rate;
7375 else
7376 cck_basic = rate;
7378 continue;
7381 /* This rate is not a basic rate so figure out the
7382 * best basic rate less than this rate and fill in
7383 * the hole in the table
7386 br[rate] = IS_OFDM(rate) ? ofdm_basic : cck_basic;
7388 if (br[rate] != 0)
7389 continue;
7391 if (IS_OFDM(rate)) {
7392 /* In 11g and 11a, the OFDM mandatory rates are 6, 12, and 24 Mbps */
7393 if (rate >= WLC_RATE_24M)
7394 mandatory = WLC_RATE_24M;
7395 else if (rate >= WLC_RATE_12M)
7396 mandatory = WLC_RATE_12M;
7397 else
7398 mandatory = WLC_RATE_6M;
7399 } else {
7400 /* In 11b, all the CCK rates are mandatory 1 - 11 Mbps */
7401 mandatory = rate;
7404 br[rate] = mandatory;
7408 static void wlc_write_rate_shm(struct wlc_info *wlc, u8 rate, u8 basic_rate)
7410 u8 phy_rate, index;
7411 u8 basic_phy_rate, basic_index;
7412 u16 dir_table, basic_table;
7413 u16 basic_ptr;
7415 /* Shared memory address for the table we are reading */
7416 dir_table = IS_OFDM(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
7418 /* Shared memory address for the table we are writing */
7419 basic_table = IS_OFDM(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
7422 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
7423 * the index into the rate table.
7425 phy_rate = rate_info[rate] & RATE_MASK;
7426 basic_phy_rate = rate_info[basic_rate] & RATE_MASK;
7427 index = phy_rate & 0xf;
7428 basic_index = basic_phy_rate & 0xf;
7430 /* Find the SHM pointer to the ACK rate entry by looking in the
7431 * Direct-map Table
7433 basic_ptr = wlc_read_shm(wlc, (dir_table + basic_index * 2));
7435 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
7436 * to the correct basic rate for the given incoming rate
7438 wlc_write_shm(wlc, (basic_table + index * 2), basic_ptr);
7441 static const wlc_rateset_t *wlc_rateset_get_hwrs(struct wlc_info *wlc)
7443 const wlc_rateset_t *rs_dflt;
7445 if (WLC_PHY_11N_CAP(wlc->band)) {
7446 if (BAND_5G(wlc->band->bandtype))
7447 rs_dflt = &ofdm_mimo_rates;
7448 else
7449 rs_dflt = &cck_ofdm_mimo_rates;
7450 } else if (wlc->band->gmode)
7451 rs_dflt = &cck_ofdm_rates;
7452 else
7453 rs_dflt = &cck_rates;
7455 return rs_dflt;
7458 void wlc_set_ratetable(struct wlc_info *wlc)
7460 const wlc_rateset_t *rs_dflt;
7461 wlc_rateset_t rs;
7462 u8 rate, basic_rate;
7463 uint i;
7465 rs_dflt = wlc_rateset_get_hwrs(wlc);
7466 ASSERT(rs_dflt != NULL);
7468 wlc_rateset_copy(rs_dflt, &rs);
7469 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7471 /* walk the phy rate table and update SHM basic rate lookup table */
7472 for (i = 0; i < rs.count; i++) {
7473 rate = rs.rates[i] & RATE_MASK;
7475 /* for a given rate WLC_BASIC_RATE returns the rate at
7476 * which a response ACK/CTS should be sent.
7478 basic_rate = WLC_BASIC_RATE(wlc, rate);
7479 if (basic_rate == 0) {
7480 /* This should only happen if we are using a
7481 * restricted rateset.
7483 basic_rate = rs.rates[0] & RATE_MASK;
7486 wlc_write_rate_shm(wlc, rate, basic_rate);
7491 * Return true if the specified rate is supported by the specified band.
7492 * WLC_BAND_AUTO indicates the current band.
7494 bool wlc_valid_rate(struct wlc_info *wlc, ratespec_t rspec, int band,
7495 bool verbose)
7497 wlc_rateset_t *hw_rateset;
7498 uint i;
7500 if ((band == WLC_BAND_AUTO) || (band == wlc->band->bandtype)) {
7501 hw_rateset = &wlc->band->hw_rateset;
7502 } else if (NBANDS(wlc) > 1) {
7503 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
7504 } else {
7505 /* other band specified and we are a single band device */
7506 return false;
7509 /* check if this is a mimo rate */
7510 if (IS_MCS(rspec)) {
7511 if (!VALID_MCS((rspec & RSPEC_RATE_MASK)))
7512 goto error;
7514 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
7517 for (i = 0; i < hw_rateset->count; i++)
7518 if (hw_rateset->rates[i] == RSPEC2RATE(rspec))
7519 return true;
7520 error:
7521 if (verbose) {
7522 WL_ERROR("wl%d: wlc_valid_rate: rate spec 0x%x not in hw_rateset\n",
7523 wlc->pub->unit, rspec);
7526 return false;
7529 static void wlc_update_mimo_band_bwcap(struct wlc_info *wlc, u8 bwcap)
7531 uint i;
7532 struct wlcband *band;
7534 for (i = 0; i < NBANDS(wlc); i++) {
7535 if (IS_SINGLEBAND_5G(wlc->deviceid))
7536 i = BAND_5G_INDEX;
7537 band = wlc->bandstate[i];
7538 if (band->bandtype == WLC_BAND_5G) {
7539 if ((bwcap == WLC_N_BW_40ALL)
7540 || (bwcap == WLC_N_BW_20IN2G_40IN5G))
7541 band->mimo_cap_40 = true;
7542 else
7543 band->mimo_cap_40 = false;
7544 } else {
7545 ASSERT(band->bandtype == WLC_BAND_2G);
7546 if (bwcap == WLC_N_BW_40ALL)
7547 band->mimo_cap_40 = true;
7548 else
7549 band->mimo_cap_40 = false;
7553 wlc->mimo_band_bwcap = bwcap;
7556 void wlc_mod_prb_rsp_rate_table(struct wlc_info *wlc, uint frame_len)
7558 const wlc_rateset_t *rs_dflt;
7559 wlc_rateset_t rs;
7560 u8 rate;
7561 u16 entry_ptr;
7562 u8 plcp[D11_PHY_HDR_LEN];
7563 u16 dur, sifs;
7564 uint i;
7566 sifs = SIFS(wlc->band);
7568 rs_dflt = wlc_rateset_get_hwrs(wlc);
7569 ASSERT(rs_dflt != NULL);
7571 wlc_rateset_copy(rs_dflt, &rs);
7572 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7574 /* walk the phy rate table and update MAC core SHM basic rate table entries */
7575 for (i = 0; i < rs.count; i++) {
7576 rate = rs.rates[i] & RATE_MASK;
7578 entry_ptr = wlc_rate_shm_offset(wlc, rate);
7580 /* Calculate the Probe Response PLCP for the given rate */
7581 wlc_compute_plcp(wlc, rate, frame_len, plcp);
7583 /* Calculate the duration of the Probe Response frame plus SIFS for the MAC */
7584 dur =
7585 (u16) wlc_calc_frame_time(wlc, rate, WLC_LONG_PREAMBLE,
7586 frame_len);
7587 dur += sifs;
7589 /* Update the SHM Rate Table entry Probe Response values */
7590 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS,
7591 (u16) (plcp[0] + (plcp[1] << 8)));
7592 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7593 (u16) (plcp[2] + (plcp[3] << 8)));
7594 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_DUR_POS, dur);
7599 wlc_compute_bcntsfoff(struct wlc_info *wlc, ratespec_t rspec,
7600 bool short_preamble, bool phydelay)
7602 uint bcntsfoff = 0;
7604 if (IS_MCS(rspec)) {
7605 WL_ERROR("wl%d: recd beacon with mcs rate; rspec 0x%x\n",
7606 wlc->pub->unit, rspec);
7607 } else if (IS_OFDM(rspec)) {
7608 /* tx delay from MAC through phy to air (2.1 usec) +
7609 * phy header time (preamble + PLCP SIGNAL == 20 usec) +
7610 * PLCP SERVICE + MAC header time (SERVICE + FC + DUR + A1 + A2 + A3 + SEQ == 26
7611 * bytes at beacon rate)
7613 bcntsfoff += phydelay ? D11A_PHY_TX_DELAY : 0;
7614 bcntsfoff += APHY_PREAMBLE_TIME + APHY_SIGNAL_TIME;
7615 bcntsfoff +=
7616 wlc_compute_airtime(wlc, rspec,
7617 APHY_SERVICE_NBITS / 8 +
7618 DOT11_MAC_HDR_LEN);
7619 } else {
7620 /* tx delay from MAC through phy to air (3.4 usec) +
7621 * phy header time (long preamble + PLCP == 192 usec) +
7622 * MAC header time (FC + DUR + A1 + A2 + A3 + SEQ == 24 bytes at beacon rate)
7624 bcntsfoff += phydelay ? D11B_PHY_TX_DELAY : 0;
7625 bcntsfoff +=
7626 short_preamble ? D11B_PHY_SPREHDR_TIME :
7627 D11B_PHY_LPREHDR_TIME;
7628 bcntsfoff += wlc_compute_airtime(wlc, rspec, DOT11_MAC_HDR_LEN);
7630 return (u16) (bcntsfoff);
7633 /* Max buffering needed for beacon template/prb resp template is 142 bytes.
7635 * PLCP header is 6 bytes.
7636 * 802.11 A3 header is 24 bytes.
7637 * Max beacon frame body template length is 112 bytes.
7638 * Max probe resp frame body template length is 110 bytes.
7640 * *len on input contains the max length of the packet available.
7642 * The *len value is set to the number of bytes in buf used, and starts with the PLCP
7643 * and included up to, but not including, the 4 byte FCS.
7645 static void
7646 wlc_bcn_prb_template(struct wlc_info *wlc, uint type, ratespec_t bcn_rspec,
7647 wlc_bsscfg_t *cfg, u16 *buf, int *len)
7649 static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
7650 cck_phy_hdr_t *plcp;
7651 struct ieee80211_mgmt *h;
7652 int hdr_len, body_len;
7654 ASSERT(*len >= 142);
7655 ASSERT(type == FC_BEACON || type == FC_PROBE_RESP);
7657 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
7658 hdr_len = DOT11_MAC_HDR_LEN;
7659 else
7660 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7661 body_len = *len - hdr_len; /* calc buffer size provided for frame body */
7663 *len = hdr_len + body_len; /* return actual size */
7665 /* format PHY and MAC headers */
7666 memset((char *)buf, 0, hdr_len);
7668 plcp = (cck_phy_hdr_t *) buf;
7670 /* PLCP for Probe Response frames are filled in from core's rate table */
7671 if (type == FC_BEACON && !MBSS_BCN_ENAB(cfg)) {
7672 /* fill in PLCP */
7673 wlc_compute_plcp(wlc, bcn_rspec,
7674 (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
7675 (u8 *) plcp);
7678 /* "Regular" and 16 MBSS but not for 4 MBSS */
7679 /* Update the phytxctl for the beacon based on the rspec */
7680 if (!SOFTBCN_ENAB(cfg))
7681 wlc_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7683 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
7684 h = (struct ieee80211_mgmt *)&plcp[0];
7685 else
7686 h = (struct ieee80211_mgmt *)&plcp[1];
7688 /* fill in 802.11 header */
7689 h->frame_control = htol16((u16) type);
7691 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7692 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7693 if (type == FC_BEACON)
7694 bcopy((const char *)&ether_bcast, (char *)&h->da,
7695 ETH_ALEN);
7696 bcopy((char *)&cfg->cur_etheraddr, (char *)&h->sa, ETH_ALEN);
7697 bcopy((char *)&cfg->BSSID, (char *)&h->bssid, ETH_ALEN);
7699 /* SEQ filled in by MAC */
7701 return;
7704 int wlc_get_header_len()
7706 return TXOFF;
7709 /* Update a beacon for a particular BSS
7710 * For MBSS, this updates the software template and sets "latest" to the index of the
7711 * template updated.
7712 * Otherwise, it updates the hardware template.
7714 void wlc_bss_update_beacon(struct wlc_info *wlc, wlc_bsscfg_t *cfg)
7716 int len = BCN_TMPL_LEN;
7718 /* Clear the soft intmask */
7719 wlc->defmacintmask &= ~MI_BCNTPL;
7721 if (!cfg->up) { /* Only allow updates on an UP bss */
7722 return;
7725 if (MBSS_BCN_ENAB(cfg)) { /* Optimize: Some of if/else could be combined */
7726 } else if (HWBCN_ENAB(cfg)) { /* Hardware beaconing for this config */
7727 u16 bcn[BCN_TMPL_LEN / 2];
7728 u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
7729 d11regs_t *regs = wlc->regs;
7730 struct osl_info *osh = NULL;
7732 osh = wlc->osh;
7734 /* Check if both templates are in use, if so sched. an interrupt
7735 * that will call back into this routine
7737 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7738 /* clear any previous status */
7739 W_REG(osh, &regs->macintstatus, MI_BCNTPL);
7741 /* Check that after scheduling the interrupt both of the
7742 * templates are still busy. if not clear the int. & remask
7744 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7745 wlc->defmacintmask |= MI_BCNTPL;
7746 return;
7749 wlc->bcn_rspec =
7750 wlc_lowest_basic_rspec(wlc, &cfg->current_bss->rateset);
7751 ASSERT(wlc_valid_rate
7752 (wlc, wlc->bcn_rspec,
7753 CHSPEC_IS2G(cfg->current_bss->
7754 chanspec) ? WLC_BAND_2G : WLC_BAND_5G,
7755 true));
7757 /* update the template and ucode shm */
7758 wlc_bcn_prb_template(wlc, FC_BEACON, wlc->bcn_rspec, cfg, bcn,
7759 &len);
7760 wlc_write_hw_bcntemplates(wlc, bcn, len, false);
7765 * Update all beacons for the system.
7767 void wlc_update_beacon(struct wlc_info *wlc)
7769 int idx;
7770 wlc_bsscfg_t *bsscfg;
7772 /* update AP or IBSS beacons */
7773 FOREACH_BSS(wlc, idx, bsscfg) {
7774 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7775 wlc_bss_update_beacon(wlc, bsscfg);
7779 /* Write ssid into shared memory */
7780 void wlc_shm_ssid_upd(struct wlc_info *wlc, wlc_bsscfg_t *cfg)
7782 u8 *ssidptr = cfg->SSID;
7783 u16 base = M_SSID;
7784 u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
7786 /* padding the ssid with zero and copy it into shm */
7787 memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
7788 bcopy(ssidptr, ssidbuf, cfg->SSID_len);
7790 wlc_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
7792 if (!MBSS_BCN_ENAB(cfg))
7793 wlc_write_shm(wlc, M_SSIDLEN, (u16) cfg->SSID_len);
7796 void wlc_update_probe_resp(struct wlc_info *wlc, bool suspend)
7798 int idx;
7799 wlc_bsscfg_t *bsscfg;
7801 /* update AP or IBSS probe responses */
7802 FOREACH_BSS(wlc, idx, bsscfg) {
7803 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7804 wlc_bss_update_probe_resp(wlc, bsscfg, suspend);
7808 void
7809 wlc_bss_update_probe_resp(struct wlc_info *wlc, wlc_bsscfg_t *cfg, bool suspend)
7811 u16 prb_resp[BCN_TMPL_LEN / 2];
7812 int len = BCN_TMPL_LEN;
7814 /* write the probe response to hardware, or save in the config structure */
7815 if (!MBSS_PRB_ENAB(cfg)) {
7817 /* create the probe response template */
7818 wlc_bcn_prb_template(wlc, FC_PROBE_RESP, 0, cfg, prb_resp,
7819 &len);
7821 if (suspend)
7822 wlc_suspend_mac_and_wait(wlc);
7824 /* write the probe response into the template region */
7825 wlc_bmac_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7826 (len + 3) & ~3, prb_resp);
7828 /* write the length of the probe response frame (+PLCP/-FCS) */
7829 wlc_write_shm(wlc, M_PRB_RESP_FRM_LEN, (u16) len);
7831 /* write the SSID and SSID length */
7832 wlc_shm_ssid_upd(wlc, cfg);
7835 * Write PLCP headers and durations for probe response frames at all rates.
7836 * Use the actual frame length covered by the PLCP header for the call to
7837 * wlc_mod_prb_rsp_rate_table() by subtracting the PLCP len and adding the FCS.
7839 len += (-D11_PHY_HDR_LEN + FCS_LEN);
7840 wlc_mod_prb_rsp_rate_table(wlc, (u16) len);
7842 if (suspend)
7843 wlc_enable_mac(wlc);
7844 } else { /* Generating probe resp in sw; update local template */
7845 ASSERT(0 && "No software probe response support without MBSS");
7849 /* prepares pdu for transmission. returns BCM error codes */
7850 int wlc_prep_pdu(struct wlc_info *wlc, struct sk_buff *pdu, uint *fifop)
7852 struct osl_info *osh;
7853 uint fifo;
7854 d11txh_t *txh;
7855 struct ieee80211_hdr *h;
7856 struct scb *scb;
7857 u16 fc;
7859 osh = wlc->osh;
7861 ASSERT(pdu);
7862 txh = (d11txh_t *) (pdu->data);
7863 ASSERT(txh);
7864 h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
7865 ASSERT(h);
7866 fc = ltoh16(h->frame_control);
7868 /* get the pkt queue info. This was put at wlc_sendctl or wlc_send for PDU */
7869 fifo = ltoh16(txh->TxFrameID) & TXFID_QUEUE_MASK;
7871 scb = NULL;
7873 *fifop = fifo;
7875 /* return if insufficient dma resources */
7876 if (TXAVAIL(wlc, fifo) < MAX_DMA_SEGS) {
7877 /* Mark precedences related to this FIFO, unsendable */
7878 WLC_TX_FIFO_CLEAR(wlc, fifo);
7879 return BCME_BUSY;
7882 if ((ltoh16(txh->MacFrameControl) & IEEE80211_FCTL_FTYPE) !=
7883 IEEE80211_FTYPE_DATA)
7884 WLCNTINCR(wlc->pub->_cnt->txctl);
7886 return 0;
7889 /* init tx reported rate mechanism */
7890 void wlc_reprate_init(struct wlc_info *wlc)
7892 int i;
7893 wlc_bsscfg_t *bsscfg;
7895 FOREACH_BSS(wlc, i, bsscfg) {
7896 wlc_bsscfg_reprate_init(bsscfg);
7900 /* per bsscfg init tx reported rate mechanism */
7901 void wlc_bsscfg_reprate_init(wlc_bsscfg_t *bsscfg)
7903 bsscfg->txrspecidx = 0;
7904 memset((char *)bsscfg->txrspec, 0, sizeof(bsscfg->txrspec));
7907 /* Retrieve a consolidated set of revision information,
7908 * typically for the WLC_GET_REVINFO ioctl
7910 int wlc_get_revision_info(struct wlc_info *wlc, void *buf, uint len)
7912 wlc_rev_info_t *rinfo = (wlc_rev_info_t *) buf;
7914 if (len < WL_REV_INFO_LEGACY_LENGTH)
7915 return BCME_BUFTOOSHORT;
7917 rinfo->vendorid = wlc->vendorid;
7918 rinfo->deviceid = wlc->deviceid;
7919 rinfo->radiorev = (wlc->band->radiorev << IDCODE_REV_SHIFT) |
7920 (wlc->band->radioid << IDCODE_ID_SHIFT);
7921 rinfo->chiprev = wlc->pub->sih->chiprev;
7922 rinfo->corerev = wlc->pub->corerev;
7923 rinfo->boardid = wlc->pub->sih->boardtype;
7924 rinfo->boardvendor = wlc->pub->sih->boardvendor;
7925 rinfo->boardrev = wlc->pub->boardrev;
7926 rinfo->ucoderev = wlc->ucode_rev;
7927 rinfo->driverrev = EPI_VERSION_NUM;
7928 rinfo->bus = wlc->pub->sih->bustype;
7929 rinfo->chipnum = wlc->pub->sih->chip;
7931 if (len >= (offsetof(wlc_rev_info_t, chippkg))) {
7932 rinfo->phytype = wlc->band->phytype;
7933 rinfo->phyrev = wlc->band->phyrev;
7934 rinfo->anarev = 0; /* obsolete stuff, suppress */
7937 if (len >= sizeof(*rinfo)) {
7938 rinfo->chippkg = wlc->pub->sih->chippkg;
7941 return BCME_OK;
7944 void wlc_default_rateset(struct wlc_info *wlc, wlc_rateset_t *rs)
7946 wlc_rateset_default(rs, NULL, wlc->band->phytype, wlc->band->bandtype,
7947 false, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
7948 CHSPEC_WLC_BW(wlc->default_bss->chanspec),
7949 wlc->stf->txstreams);
7952 static void wlc_bss_default_init(struct wlc_info *wlc)
7954 chanspec_t chanspec;
7955 struct wlcband *band;
7956 wlc_bss_info_t *bi = wlc->default_bss;
7958 /* init default and target BSS with some sane initial values */
7959 memset((char *)(bi), 0, sizeof(wlc_bss_info_t));
7960 bi->beacon_period = ISSIM_ENAB(wlc->pub->sih) ? BEACON_INTERVAL_DEF_QT :
7961 BEACON_INTERVAL_DEFAULT;
7962 bi->dtim_period = ISSIM_ENAB(wlc->pub->sih) ? DTIM_INTERVAL_DEF_QT :
7963 DTIM_INTERVAL_DEFAULT;
7965 /* fill the default channel as the first valid channel
7966 * starting from the 2G channels
7968 chanspec = CH20MHZ_CHSPEC(1);
7969 ASSERT(chanspec != INVCHANSPEC);
7971 wlc->home_chanspec = bi->chanspec = chanspec;
7973 /* find the band of our default channel */
7974 band = wlc->band;
7975 if (NBANDS(wlc) > 1 && band->bandunit != CHSPEC_WLCBANDUNIT(chanspec))
7976 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
7978 /* init bss rates to the band specific default rate set */
7979 wlc_rateset_default(&bi->rateset, NULL, band->phytype, band->bandtype,
7980 false, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
7981 CHSPEC_WLC_BW(chanspec), wlc->stf->txstreams);
7983 if (N_ENAB(wlc->pub))
7984 bi->flags |= WLC_BSS_HT;
7987 /* Deferred event processing */
7988 static void wlc_process_eventq(void *arg)
7990 struct wlc_info *wlc = (struct wlc_info *) arg;
7991 wlc_event_t *etmp;
7993 while ((etmp = wlc_eventq_deq(wlc->eventq))) {
7994 /* Perform OS specific event processing */
7995 wl_event(wlc->wl, etmp->event.ifname, etmp);
7996 if (etmp->data) {
7997 kfree(etmp->data);
7998 etmp->data = NULL;
8000 wlc_event_free(wlc->eventq, etmp);
8004 void
8005 wlc_uint64_sub(u32 *a_high, u32 *a_low, u32 b_high, u32 b_low)
8007 if (b_low > *a_low) {
8008 /* low half needs a carry */
8009 b_high += 1;
8011 *a_low -= b_low;
8012 *a_high -= b_high;
8015 static ratespec_t
8016 mac80211_wlc_set_nrate(struct wlc_info *wlc, struct wlcband *cur_band,
8017 u32 int_val)
8019 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
8020 u8 rate = int_val & NRATE_RATE_MASK;
8021 ratespec_t rspec;
8022 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
8023 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
8024 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
8025 == NRATE_OVERRIDE_MCS_ONLY);
8026 int bcmerror = 0;
8028 if (!ismcs) {
8029 return (ratespec_t) rate;
8032 /* validate the combination of rate/mcs/stf is allowed */
8033 if (N_ENAB(wlc->pub) && ismcs) {
8034 /* mcs only allowed when nmode */
8035 if (stf > PHY_TXC1_MODE_SDM) {
8036 WL_ERROR("wl%d: %s: Invalid stf\n",
8037 WLCWLUNIT(wlc), __func__);
8038 bcmerror = BCME_RANGE;
8039 goto done;
8042 /* mcs 32 is a special case, DUP mode 40 only */
8043 if (rate == 32) {
8044 if (!CHSPEC_IS40(wlc->home_chanspec) ||
8045 ((stf != PHY_TXC1_MODE_SISO)
8046 && (stf != PHY_TXC1_MODE_CDD))) {
8047 WL_ERROR("wl%d: %s: Invalid mcs 32\n",
8048 WLCWLUNIT(wlc), __func__);
8049 bcmerror = BCME_RANGE;
8050 goto done;
8052 /* mcs > 7 must use stf SDM */
8053 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
8054 /* mcs > 7 must use stf SDM */
8055 if (stf != PHY_TXC1_MODE_SDM) {
8056 WL_TRACE("wl%d: %s: enabling SDM mode for mcs %d\n",
8057 WLCWLUNIT(wlc), __func__, rate);
8058 stf = PHY_TXC1_MODE_SDM;
8060 } else {
8061 /* MCS 0-7 may use SISO, CDD, and for phy_rev >= 3 STBC */
8062 if ((stf > PHY_TXC1_MODE_STBC) ||
8063 (!WLC_STBC_CAP_PHY(wlc)
8064 && (stf == PHY_TXC1_MODE_STBC))) {
8065 WL_ERROR("wl%d: %s: Invalid STBC\n",
8066 WLCWLUNIT(wlc), __func__);
8067 bcmerror = BCME_RANGE;
8068 goto done;
8071 } else if (IS_OFDM(rate)) {
8072 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
8073 WL_ERROR("wl%d: %s: Invalid OFDM\n",
8074 WLCWLUNIT(wlc), __func__);
8075 bcmerror = BCME_RANGE;
8076 goto done;
8078 } else if (IS_CCK(rate)) {
8079 if ((cur_band->bandtype != WLC_BAND_2G)
8080 || (stf != PHY_TXC1_MODE_SISO)) {
8081 WL_ERROR("wl%d: %s: Invalid CCK\n",
8082 WLCWLUNIT(wlc), __func__);
8083 bcmerror = BCME_RANGE;
8084 goto done;
8086 } else {
8087 WL_ERROR("wl%d: %s: Unknown rate type\n",
8088 WLCWLUNIT(wlc), __func__);
8089 bcmerror = BCME_RANGE;
8090 goto done;
8092 /* make sure multiple antennae are available for non-siso rates */
8093 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
8094 WL_ERROR("wl%d: %s: SISO antenna but !SISO request\n",
8095 WLCWLUNIT(wlc), __func__);
8096 bcmerror = BCME_RANGE;
8097 goto done;
8100 rspec = rate;
8101 if (ismcs) {
8102 rspec |= RSPEC_MIMORATE;
8103 /* For STBC populate the STC field of the ratespec */
8104 if (stf == PHY_TXC1_MODE_STBC) {
8105 u8 stc;
8106 stc = 1; /* Nss for single stream is always 1 */
8107 rspec |= (stc << RSPEC_STC_SHIFT);
8111 rspec |= (stf << RSPEC_STF_SHIFT);
8113 if (override_mcs_only)
8114 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
8116 if (issgi)
8117 rspec |= RSPEC_SHORT_GI;
8119 if ((rate != 0)
8120 && !wlc_valid_rate(wlc, rspec, cur_band->bandtype, true)) {
8121 return rate;
8124 return rspec;
8125 done:
8126 WL_ERROR("Hoark\n");
8127 return rate;
8130 /* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
8131 static int
8132 wlc_duty_cycle_set(struct wlc_info *wlc, int duty_cycle, bool isOFDM,
8133 bool writeToShm)
8135 int idle_busy_ratio_x_16 = 0;
8136 uint offset =
8137 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
8138 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
8139 if (duty_cycle > 100 || duty_cycle < 0) {
8140 WL_ERROR("wl%d: duty cycle value off limit\n", wlc->pub->unit);
8141 return BCME_RANGE;
8143 if (duty_cycle)
8144 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
8145 /* Only write to shared memory when wl is up */
8146 if (writeToShm)
8147 wlc_write_shm(wlc, offset, (u16) idle_busy_ratio_x_16);
8149 if (isOFDM)
8150 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
8151 else
8152 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
8154 return BCME_OK;
8157 /* Read a single u16 from shared memory.
8158 * SHM 'offset' needs to be an even address
8160 u16 wlc_read_shm(struct wlc_info *wlc, uint offset)
8162 return wlc_bmac_read_shm(wlc->hw, offset);
8165 /* Write a single u16 to shared memory.
8166 * SHM 'offset' needs to be an even address
8168 void wlc_write_shm(struct wlc_info *wlc, uint offset, u16 v)
8170 wlc_bmac_write_shm(wlc->hw, offset, v);
8173 /* Set a range of shared memory to a value.
8174 * SHM 'offset' needs to be an even address and
8175 * Range length 'len' must be an even number of bytes
8177 void wlc_set_shm(struct wlc_info *wlc, uint offset, u16 v, int len)
8179 /* offset and len need to be even */
8180 ASSERT((offset & 1) == 0);
8181 ASSERT((len & 1) == 0);
8183 if (len <= 0)
8184 return;
8186 wlc_bmac_set_shm(wlc->hw, offset, v, len);
8189 /* Copy a buffer to shared memory.
8190 * SHM 'offset' needs to be an even address and
8191 * Buffer length 'len' must be an even number of bytes
8193 void wlc_copyto_shm(struct wlc_info *wlc, uint offset, const void *buf, int len)
8195 /* offset and len need to be even */
8196 ASSERT((offset & 1) == 0);
8197 ASSERT((len & 1) == 0);
8199 if (len <= 0)
8200 return;
8201 wlc_bmac_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8205 /* Copy from shared memory to a buffer.
8206 * SHM 'offset' needs to be an even address and
8207 * Buffer length 'len' must be an even number of bytes
8209 void wlc_copyfrom_shm(struct wlc_info *wlc, uint offset, void *buf, int len)
8211 /* offset and len need to be even */
8212 ASSERT((offset & 1) == 0);
8213 ASSERT((len & 1) == 0);
8215 if (len <= 0)
8216 return;
8218 wlc_bmac_copyfrom_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8221 /* wrapper BMAC functions to for HIGH driver access */
8222 void wlc_mctrl(struct wlc_info *wlc, u32 mask, u32 val)
8224 wlc_bmac_mctrl(wlc->hw, mask, val);
8227 void wlc_corereset(struct wlc_info *wlc, u32 flags)
8229 wlc_bmac_corereset(wlc->hw, flags);
8232 void wlc_mhf(struct wlc_info *wlc, u8 idx, u16 mask, u16 val, int bands)
8234 wlc_bmac_mhf(wlc->hw, idx, mask, val, bands);
8237 u16 wlc_mhf_get(struct wlc_info *wlc, u8 idx, int bands)
8239 return wlc_bmac_mhf_get(wlc->hw, idx, bands);
8242 int wlc_xmtfifo_sz_get(struct wlc_info *wlc, uint fifo, uint *blocks)
8244 return wlc_bmac_xmtfifo_sz_get(wlc->hw, fifo, blocks);
8247 void wlc_write_template_ram(struct wlc_info *wlc, int offset, int len,
8248 void *buf)
8250 wlc_bmac_write_template_ram(wlc->hw, offset, len, buf);
8253 void wlc_write_hw_bcntemplates(struct wlc_info *wlc, void *bcn, int len,
8254 bool both)
8256 wlc_bmac_write_hw_bcntemplates(wlc->hw, bcn, len, both);
8259 void
8260 wlc_set_addrmatch(struct wlc_info *wlc, int match_reg_offset,
8261 const u8 *addr)
8263 wlc_bmac_set_addrmatch(wlc->hw, match_reg_offset, addr);
8266 void wlc_set_rcmta(struct wlc_info *wlc, int idx, const u8 *addr)
8268 wlc_bmac_set_rcmta(wlc->hw, idx, addr);
8271 void wlc_read_tsf(struct wlc_info *wlc, u32 *tsf_l_ptr, u32 *tsf_h_ptr)
8273 wlc_bmac_read_tsf(wlc->hw, tsf_l_ptr, tsf_h_ptr);
8276 void wlc_set_cwmin(struct wlc_info *wlc, u16 newmin)
8278 wlc->band->CWmin = newmin;
8279 wlc_bmac_set_cwmin(wlc->hw, newmin);
8282 void wlc_set_cwmax(struct wlc_info *wlc, u16 newmax)
8284 wlc->band->CWmax = newmax;
8285 wlc_bmac_set_cwmax(wlc->hw, newmax);
8288 void wlc_fifoerrors(struct wlc_info *wlc)
8291 wlc_bmac_fifoerrors(wlc->hw);
8294 /* Search mem rw utilities */
8296 void wlc_pllreq(struct wlc_info *wlc, bool set, mbool req_bit)
8298 wlc_bmac_pllreq(wlc->hw, set, req_bit);
8301 void wlc_reset_bmac_done(struct wlc_info *wlc)
8305 void wlc_ht_mimops_cap_update(struct wlc_info *wlc, u8 mimops_mode)
8307 wlc->ht_cap.cap_info &= ~HT_CAP_MIMO_PS_MASK;
8308 wlc->ht_cap.cap_info |= (mimops_mode << IEEE80211_HT_CAP_SM_PS_SHIFT);
8310 if (AP_ENAB(wlc->pub) && wlc->clk) {
8311 wlc_update_beacon(wlc);
8312 wlc_update_probe_resp(wlc, true);
8316 /* check for the particular priority flow control bit being set */
8317 bool
8318 wlc_txflowcontrol_prio_isset(struct wlc_info *wlc, wlc_txq_info_t *q, int prio)
8320 uint prio_mask;
8322 if (prio == ALLPRIO) {
8323 prio_mask = TXQ_STOP_FOR_PRIOFC_MASK;
8324 } else {
8325 ASSERT(prio >= 0 && prio <= MAXPRIO);
8326 prio_mask = NBITVAL(prio);
8329 return (q->stopped & prio_mask) == prio_mask;
8332 /* propogate the flow control to all interfaces using the given tx queue */
8333 void wlc_txflowcontrol(struct wlc_info *wlc, wlc_txq_info_t *qi,
8334 bool on, int prio)
8336 uint prio_bits;
8337 uint cur_bits;
8339 WL_ERROR("%s: flow control kicks in\n", __func__);
8341 if (prio == ALLPRIO) {
8342 prio_bits = TXQ_STOP_FOR_PRIOFC_MASK;
8343 } else {
8344 ASSERT(prio >= 0 && prio <= MAXPRIO);
8345 prio_bits = NBITVAL(prio);
8348 cur_bits = qi->stopped & prio_bits;
8350 /* Check for the case of no change and return early
8351 * Otherwise update the bit and continue
8353 if (on) {
8354 if (cur_bits == prio_bits) {
8355 return;
8357 mboolset(qi->stopped, prio_bits);
8358 } else {
8359 if (cur_bits == 0) {
8360 return;
8362 mboolclr(qi->stopped, prio_bits);
8365 /* If there is a flow control override we will not change the external
8366 * flow control state.
8368 if (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK) {
8369 return;
8372 wlc_txflowcontrol_signal(wlc, qi, on, prio);
8375 void
8376 wlc_txflowcontrol_override(struct wlc_info *wlc, wlc_txq_info_t *qi, bool on,
8377 uint override)
8379 uint prev_override;
8381 ASSERT(override != 0);
8382 ASSERT((override & TXQ_STOP_FOR_PRIOFC_MASK) == 0);
8384 prev_override = (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK);
8386 /* Update the flow control bits and do an early return if there is
8387 * no change in the external flow control state.
8389 if (on) {
8390 mboolset(qi->stopped, override);
8391 /* if there was a previous override bit on, then setting this
8392 * makes no difference.
8394 if (prev_override) {
8395 return;
8398 wlc_txflowcontrol_signal(wlc, qi, ON, ALLPRIO);
8399 } else {
8400 mboolclr(qi->stopped, override);
8401 /* clearing an override bit will only make a difference for
8402 * flow control if it was the only bit set. For any other
8403 * override setting, just return
8405 if (prev_override != override) {
8406 return;
8409 if (qi->stopped == 0) {
8410 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8411 } else {
8412 int prio;
8414 for (prio = MAXPRIO; prio >= 0; prio--) {
8415 if (!mboolisset(qi->stopped, NBITVAL(prio)))
8416 wlc_txflowcontrol_signal(wlc, qi, OFF,
8417 prio);
8423 static void wlc_txflowcontrol_reset(struct wlc_info *wlc)
8425 wlc_txq_info_t *qi;
8427 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
8428 if (qi->stopped) {
8429 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8430 qi->stopped = 0;
8435 static void
8436 wlc_txflowcontrol_signal(struct wlc_info *wlc, wlc_txq_info_t *qi, bool on,
8437 int prio)
8439 struct wlc_if *wlcif;
8441 for (wlcif = wlc->wlcif_list; wlcif != NULL; wlcif = wlcif->next) {
8442 if (wlcif->qi == qi && wlcif->flags & WLC_IF_LINKED)
8443 wl_txflowcontrol(wlc->wl, wlcif->wlif, on, prio);
8447 static wlc_txq_info_t *wlc_txq_alloc(struct wlc_info *wlc, struct osl_info *osh)
8449 wlc_txq_info_t *qi, *p;
8451 qi = (wlc_txq_info_t *) wlc_calloc(osh, wlc->pub->unit,
8452 sizeof(wlc_txq_info_t));
8453 if (qi == NULL) {
8454 return NULL;
8457 /* Have enough room for control packets along with HI watermark */
8458 /* Also, add room to txq for total psq packets if all the SCBs leave PS mode */
8459 /* The watermark for flowcontrol to OS packets will remain the same */
8460 pktq_init(&qi->q, WLC_PREC_COUNT,
8461 (2 * wlc->pub->tunables->datahiwat) + PKTQ_LEN_DEFAULT +
8462 wlc->pub->psq_pkts_total);
8464 /* add this queue to the the global list */
8465 p = wlc->tx_queues;
8466 if (p == NULL) {
8467 wlc->tx_queues = qi;
8468 } else {
8469 while (p->next != NULL)
8470 p = p->next;
8471 p->next = qi;
8474 return qi;
8477 static void wlc_txq_free(struct wlc_info *wlc, struct osl_info *osh,
8478 wlc_txq_info_t *qi)
8480 wlc_txq_info_t *p;
8482 if (qi == NULL)
8483 return;
8485 /* remove the queue from the linked list */
8486 p = wlc->tx_queues;
8487 if (p == qi)
8488 wlc->tx_queues = p->next;
8489 else {
8490 while (p != NULL && p->next != qi)
8491 p = p->next;
8492 ASSERT(p->next == qi);
8493 if (p != NULL)
8494 p->next = p->next->next;
8497 kfree(qi);