staging: brcm80211: Purge unused packet engine
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / sys / wlc_mac80211.c
blob7bdd60efa544075ed3caa3164f4da5a9a094cbd7
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.
17 #include <wlc_cfg.h>
18 #include <typedefs.h>
19 #include <linux/kernel.h>
20 #include <linuxver.h>
21 #include <bcmdefs.h>
22 #include <osl.h>
23 #include <linux/ctype.h>
24 #include <bcmutils.h>
25 #include <bcmwifi.h>
26 #include <siutils.h>
27 #include <bcmendian.h>
28 #include <proto/wpa.h>
29 #include <pcicfg.h>
30 #include <bcmsrom.h>
31 #include <wlioctl.h>
32 #include <epivers.h>
33 #include <bcmwpa.h>
34 #include <sbhnddma.h>
35 #include <hnddma.h>
36 #include <hndpmu.h>
37 #include <d11.h>
38 #include <wlc_rate.h>
39 #include <wlc_pub.h>
40 #include <wlc_key.h>
41 #include <wlc_bsscfg.h>
42 #include <wlc_channel.h>
43 #include <wlc_mac80211.h>
44 #include <wlc_bmac.h>
45 #include <wlc_scb.h>
46 #include <wlc_phy_hal.h>
47 #include <wlc_phy_shim.h>
48 #include <wlc_antsel.h>
49 #include <wlc_stf.h>
50 #include <wlc_ampdu.h>
51 #include <wlc_event.h>
52 #include <wl_export.h>
53 #ifdef BCMSDIO
54 #include <bcmsdh.h>
55 #else
56 #include "d11ucode_ext.h"
57 #endif
58 #ifdef WLC_HIGH_ONLY
59 #include <bcm_rpc_tp.h>
60 #include <bcm_rpc.h>
61 #include <bcm_xdr.h>
62 #include <wlc_rpc.h>
63 #include <wlc_rpctx.h>
64 #endif /* WLC_HIGH_ONLY */
65 #include <wlc_alloc.h>
66 #include <net/mac80211.h>
68 #ifdef WLC_HIGH_ONLY
69 #undef R_REG
70 #undef W_REG
71 #define R_REG(osh, r) RPC_READ_REG(osh, r)
72 #define W_REG(osh, r, v) RPC_WRITE_REG(osh, r, v)
73 #endif
76 * buffer length needed for wlc_format_ssid
77 * 32 SSID chars, max of 4 chars for each SSID char "\xFF", plus NULL.
79 #define SSID_FMT_BUF_LEN ((4 * DOT11_MAX_SSID_LEN) + 1)
81 #define TIMER_INTERVAL_WATCHDOG 1000 /* watchdog timer, in unit of ms */
82 #define TIMER_INTERVAL_RADIOCHK 800 /* radio monitor timer, in unit of ms */
84 #ifndef WLC_MPC_MAX_DELAYCNT
85 #define WLC_MPC_MAX_DELAYCNT 10 /* Max MPC timeout, in unit of watchdog */
86 #endif
87 #define WLC_MPC_MIN_DELAYCNT 1 /* Min MPC timeout, in unit of watchdog */
88 #define WLC_MPC_THRESHOLD 3 /* MPC count threshold level */
90 #define BEACON_INTERVAL_DEFAULT 100 /* beacon interval, in unit of 1024TU */
91 #define DTIM_INTERVAL_DEFAULT 3 /* DTIM interval, in unit of beacon interval */
93 /* Scale down delays to accommodate QT slow speed */
94 #define BEACON_INTERVAL_DEF_QT 20 /* beacon interval, in unit of 1024TU */
95 #define DTIM_INTERVAL_DEF_QT 1 /* DTIM interval, in unit of beacon interval */
97 #define TBTT_ALIGN_LEEWAY_US 100 /* min leeway before first TBTT in us */
100 * driver maintains internal 'tick'(wlc->pub->now) which increments in 1s OS timer(soft
101 * watchdog) it is not a wall clock and won't increment when driver is in "down" state
102 * this low resolution driver tick can be used for maintenance tasks such as phy
103 * calibration and scb update
106 /* watchdog trigger mode: OSL timer or TBTT */
107 #define WLC_WATCHDOG_TBTT(wlc) \
108 (wlc->stas_associated > 0 && wlc->PM != PM_OFF && wlc->pub->align_wd_tbtt)
110 /* To inform the ucode of the last mcast frame posted so that it can clear moredata bit */
111 #define BCMCFID(wlc, fid) wlc_bmac_write_shm((wlc)->hw, M_BCMC_FID, (fid))
113 #ifndef WLC_HIGH_ONLY
114 #define WLC_WAR16165(wlc) (BUSTYPE(wlc->pub->sih->bustype) == PCI_BUS && \
115 (!AP_ENAB(wlc->pub)) && (wlc->war16165))
116 #else
117 #define WLC_WAR16165(wlc) (FALSE)
118 #endif /* WLC_HIGH_ONLY */
120 /* debug/trace */
121 uint wl_msg_level =
122 #if defined(BCMDBG)
123 WL_ERROR_VAL;
124 #else
126 #endif /* BCMDBG */
128 /* Find basic rate for a given rate */
129 #define WLC_BASIC_RATE(wlc, rspec) (IS_MCS(rspec) ? \
130 (wlc)->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK].leg_ofdm] : \
131 (wlc)->band->basic_rate[rspec & RSPEC_RATE_MASK])
133 #define FRAMETYPE(r, mimoframe) (IS_MCS(r) ? mimoframe : (IS_CCK(r) ? FT_CCK : FT_OFDM))
135 #define RFDISABLE_DEFAULT 10000000 /* rfdisable delay timer 500 ms, runs of ALP clock */
137 #define WLC_TEMPSENSE_PERIOD 10 /* 10 second timeout */
139 #define SCAN_IN_PROGRESS(x) 0
141 #ifdef BCMDBG
142 /* pointer to most recently allocated wl/wlc */
143 static wlc_info_t *wlc_info_dbg = (wlc_info_t *) (NULL);
144 #endif
146 #if defined(BCMDBG)
147 struct wlc_id_name_entry {
148 int id;
149 const char *name;
151 typedef struct wlc_id_name_entry wlc_id_name_table_t[];
152 #endif
154 /* IOVar table */
156 /* Parameter IDs, for use only internally to wlc -- in the wlc_iovars
157 * table and by the wlc_doiovar() function. No ordering is imposed:
158 * the table is keyed by name, and the function uses a switch.
160 enum {
161 IOV_MPC = 1,
162 IOV_QTXPOWER,
163 IOV_BCN_LI_BCN, /* Beacon listen interval in # of beacons */
164 IOV_LAST /* In case of a need to check max ID number */
167 const bcm_iovar_t wlc_iovars[] = {
168 {"mpc", IOV_MPC, (IOVF_OPEN_ALLOW), IOVT_BOOL, 0},
169 {"qtxpower", IOV_QTXPOWER, (IOVF_WHL | IOVF_OPEN_ALLOW), IOVT_UINT32,
171 {"bcn_li_bcn", IOV_BCN_LI_BCN, 0, IOVT_UINT8, 0},
172 {NULL, 0, 0, 0, 0}
175 const u8 prio2fifo[NUMPRIO] = {
176 TX_AC_BE_FIFO, /* 0 BE AC_BE Best Effort */
177 TX_AC_BK_FIFO, /* 1 BK AC_BK Background */
178 TX_AC_BK_FIFO, /* 2 -- AC_BK Background */
179 TX_AC_BE_FIFO, /* 3 EE AC_BE Best Effort */
180 TX_AC_VI_FIFO, /* 4 CL AC_VI Video */
181 TX_AC_VI_FIFO, /* 5 VI AC_VI Video */
182 TX_AC_VO_FIFO, /* 6 VO AC_VO Voice */
183 TX_AC_VO_FIFO /* 7 NC AC_VO Voice */
186 /* precedences numbers for wlc queues. These are twice as may levels as
187 * 802.1D priorities.
188 * Odd numbers are used for HI priority traffic at same precedence levels
189 * These constants are used ONLY by wlc_prio2prec_map. Do not use them elsewhere.
191 #define _WLC_PREC_NONE 0 /* None = - */
192 #define _WLC_PREC_BK 2 /* BK - Background */
193 #define _WLC_PREC_BE 4 /* BE - Best-effort */
194 #define _WLC_PREC_EE 6 /* EE - Excellent-effort */
195 #define _WLC_PREC_CL 8 /* CL - Controlled Load */
196 #define _WLC_PREC_VI 10 /* Vi - Video */
197 #define _WLC_PREC_VO 12 /* Vo - Voice */
198 #define _WLC_PREC_NC 14 /* NC - Network Control */
200 /* 802.1D Priority to precedence queue mapping */
201 const u8 wlc_prio2prec_map[] = {
202 _WLC_PREC_BE, /* 0 BE - Best-effort */
203 _WLC_PREC_BK, /* 1 BK - Background */
204 _WLC_PREC_NONE, /* 2 None = - */
205 _WLC_PREC_EE, /* 3 EE - Excellent-effort */
206 _WLC_PREC_CL, /* 4 CL - Controlled Load */
207 _WLC_PREC_VI, /* 5 Vi - Video */
208 _WLC_PREC_VO, /* 6 Vo - Voice */
209 _WLC_PREC_NC, /* 7 NC - Network Control */
212 /* Sanity check for tx_prec_map and fifo synchup
213 * Either there are some packets pending for the fifo, else if fifo is empty then
214 * all the corresponding precmap bits should be set
216 #define WLC_TX_FIFO_CHECK(wlc, fifo) (TXPKTPENDGET((wlc), (fifo)) || \
217 (TXPKTPENDGET((wlc), (fifo)) == 0 && \
218 ((wlc)->tx_prec_map & (wlc)->fifo2prec_map[(fifo)]) == \
219 (wlc)->fifo2prec_map[(fifo)]))
221 /* TX FIFO number to WME/802.1E Access Category */
222 const u8 wme_fifo2ac[] = { AC_BK, AC_BE, AC_VI, AC_VO, AC_BE, AC_BE };
224 /* WME/802.1E Access Category to TX FIFO number */
225 static const u8 wme_ac2fifo[] = { 1, 0, 2, 3 };
227 static bool in_send_q = FALSE;
229 /* Shared memory location index for various AC params */
230 #define wme_shmemacindex(ac) wme_ac2fifo[ac]
232 #ifdef BCMDBG
233 static const char *fifo_names[] = {
234 "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
235 const char *aci_names[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" };
236 #endif
238 static const u8 acbitmap2maxprio[] = {
239 PRIO_8021D_BE, PRIO_8021D_BE, PRIO_8021D_BK, PRIO_8021D_BK,
240 PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI, PRIO_8021D_VI,
241 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO,
242 PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO, PRIO_8021D_VO
245 /* currently the best mechanism for determining SIFS is the band in use */
246 #define SIFS(band) ((band)->bandtype == WLC_BAND_5G ? APHY_SIFS_TIME : BPHY_SIFS_TIME);
248 /* value for # replay counters currently supported */
249 #define WLC_REPLAY_CNTRS_VALUE WPA_CAP_16_REPLAY_CNTRS
251 /* local prototypes */
252 extern void wlc_txq_enq(void *ctx, struct scb *scb, void *sdu, uint prec);
253 static u16 BCMFASTPATH wlc_d11hdrs_mac80211(wlc_info_t *wlc,
254 struct ieee80211_hw *hw, void *p,
255 struct scb *scb, uint frag,
256 uint nfrags, uint queue,
257 uint next_frag_len,
258 wsec_key_t *key,
259 ratespec_t rspec_override);
260 bool wlc_sendpkt_mac80211(wlc_info_t *wlc, void *sdu, struct ieee80211_hw *hw);
261 void wlc_wme_setparams(wlc_info_t *wlc, u16 aci, void *arg, bool suspend);
262 static void wlc_bss_default_init(wlc_info_t *wlc);
263 static void wlc_ucode_mac_upd(wlc_info_t *wlc);
264 static ratespec_t mac80211_wlc_set_nrate(wlc_info_t *wlc, wlcband_t *cur_band,
265 u32 int_val);
266 static void wlc_tx_prec_map_init(wlc_info_t *wlc);
267 static void wlc_watchdog(void *arg);
268 static void wlc_watchdog_by_timer(void *arg);
269 static int wlc_set_rateset(wlc_info_t *wlc, wlc_rateset_t *rs_arg);
270 static int wlc_iovar_rangecheck(wlc_info_t *wlc, u32 val,
271 const bcm_iovar_t *vi);
272 static u8 wlc_local_constraint_qdbm(wlc_info_t *wlc);
274 /* send and receive */
275 static wlc_txq_info_t *wlc_txq_alloc(wlc_info_t *wlc, osl_t *osh);
276 static void wlc_txq_free(wlc_info_t *wlc, osl_t *osh, wlc_txq_info_t *qi);
277 static void wlc_txflowcontrol_signal(wlc_info_t *wlc, wlc_txq_info_t *qi,
278 bool on, int prio);
279 static void wlc_txflowcontrol_reset(wlc_info_t *wlc);
280 static u16 wlc_compute_airtime(wlc_info_t *wlc, ratespec_t rspec,
281 uint length);
282 static void wlc_compute_cck_plcp(ratespec_t rate, uint length, u8 *plcp);
283 static void wlc_compute_ofdm_plcp(ratespec_t rate, uint length, u8 *plcp);
284 static void wlc_compute_mimo_plcp(ratespec_t rate, uint length, u8 *plcp);
285 static u16 wlc_compute_frame_dur(wlc_info_t *wlc, ratespec_t rate,
286 u8 preamble_type, uint next_frag_len);
287 static void wlc_recvctl(wlc_info_t *wlc, osl_t *osh, d11rxhdr_t *rxh,
288 void *p);
289 static uint wlc_calc_frame_len(wlc_info_t *wlc, ratespec_t rate,
290 u8 preamble_type, uint dur);
291 static uint wlc_calc_ack_time(wlc_info_t *wlc, ratespec_t rate,
292 u8 preamble_type);
293 static uint wlc_calc_cts_time(wlc_info_t *wlc, ratespec_t rate,
294 u8 preamble_type);
295 /* interrupt, up/down, band */
296 static void wlc_setband(wlc_info_t *wlc, uint bandunit);
297 static chanspec_t wlc_init_chanspec(wlc_info_t *wlc);
298 static void wlc_bandinit_ordered(wlc_info_t *wlc, chanspec_t chanspec);
299 static void wlc_bsinit(wlc_info_t *wlc);
300 static int wlc_duty_cycle_set(wlc_info_t *wlc, int duty_cycle, bool isOFDM,
301 bool writeToShm);
302 static void wlc_radio_hwdisable_upd(wlc_info_t *wlc);
303 static bool wlc_radio_monitor_start(wlc_info_t *wlc);
304 static void wlc_radio_timer(void *arg);
305 static void wlc_radio_enable(wlc_info_t *wlc);
306 static void wlc_radio_upd(wlc_info_t *wlc);
308 /* scan, association, BSS */
309 static uint wlc_calc_ba_time(wlc_info_t *wlc, ratespec_t rate,
310 u8 preamble_type);
311 static void wlc_update_mimo_band_bwcap(wlc_info_t *wlc, u8 bwcap);
312 static void wlc_ht_update_sgi_rx(wlc_info_t *wlc, int val);
313 void wlc_ht_mimops_cap_update(wlc_info_t *wlc, u8 mimops_mode);
314 static void wlc_ht_update_ldpc(wlc_info_t *wlc, s8 val);
315 static void wlc_war16165(wlc_info_t *wlc, bool tx);
317 static void wlc_process_eventq(void *arg);
318 static void wlc_wme_retries_write(wlc_info_t *wlc);
319 static bool wlc_attach_stf_ant_init(wlc_info_t *wlc);
320 static uint wlc_attach_module(wlc_info_t *wlc);
321 static void wlc_detach_module(wlc_info_t *wlc);
322 static void wlc_timers_deinit(wlc_info_t *wlc);
323 static void wlc_down_led_upd(wlc_info_t *wlc);
324 static uint wlc_down_del_timer(wlc_info_t *wlc);
325 static void wlc_ofdm_rateset_war(wlc_info_t *wlc);
326 static int _wlc_ioctl(wlc_info_t *wlc, int cmd, void *arg, int len,
327 struct wlc_if *wlcif);
329 #if defined(BCMDBG)
330 void wlc_get_rcmta(wlc_info_t *wlc, int idx, struct ether_addr *addr)
332 d11regs_t *regs = wlc->regs;
333 u32 v32;
334 osl_t *osh;
336 WL_TRACE(("wl%d: %s\n", WLCWLUNIT(wlc), __func__));
338 ASSERT(wlc->pub->corerev > 4);
340 osh = wlc->osh;
342 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | (idx * 2)));
343 (void)R_REG(osh, &regs->objaddr);
344 v32 = R_REG(osh, &regs->objdata);
345 addr->octet[0] = (u8) v32;
346 addr->octet[1] = (u8) (v32 >> 8);
347 addr->octet[2] = (u8) (v32 >> 16);
348 addr->octet[3] = (u8) (v32 >> 24);
349 W_REG(osh, &regs->objaddr, (OBJADDR_RCMTA_SEL | ((idx * 2) + 1)));
350 (void)R_REG(osh, &regs->objaddr);
351 v32 = R_REG(osh, (volatile u16 *)(uintptr) & regs->objdata);
352 addr->octet[4] = (u8) v32;
353 addr->octet[5] = (u8) (v32 >> 8);
355 #endif /* defined(BCMDBG) */
357 /* keep the chip awake if needed */
358 bool wlc_stay_awake(wlc_info_t *wlc)
360 return TRUE;
363 /* conditions under which the PM bit should be set in outgoing frames and STAY_AWAKE is meaningful
365 bool wlc_ps_allowed(wlc_info_t *wlc)
367 int idx;
368 wlc_bsscfg_t *cfg;
370 /* disallow PS when one of the following global conditions meets */
371 if (!wlc->pub->associated || !wlc->PMenabled || wlc->PM_override)
372 return FALSE;
374 /* disallow PS when one of these meets when not scanning */
375 if (!wlc->PMblocked) {
376 if (AP_ACTIVE(wlc) || wlc->monitor)
377 return FALSE;
380 FOREACH_AS_STA(wlc, idx, cfg) {
381 /* disallow PS when one of the following bsscfg specific conditions meets */
382 if (!cfg->BSS || !WLC_PORTOPEN(cfg))
383 return FALSE;
385 if (!cfg->dtim_programmed)
386 return FALSE;
389 return TRUE;
392 void BCMINITFN(wlc_reset) (wlc_info_t *wlc)
394 WL_TRACE(("wl%d: wlc_reset\n", wlc->pub->unit));
396 wlc->check_for_unaligned_tbtt = FALSE;
398 /* slurp up hw mac counters before core reset */
399 if (WLC_UPDATE_STATS(wlc)) {
400 wlc_statsupd(wlc);
402 /* reset our snapshot of macstat counters */
403 bzero((char *)wlc->core->macstat_snapshot, sizeof(macstat_t));
406 wlc_bmac_reset(wlc->hw);
407 wlc_ampdu_reset(wlc->ampdu);
408 wlc->txretried = 0;
410 #ifdef WLC_HIGH_ONLY
411 /* Need to set a flag(to be cleared asynchronously by BMAC driver with high call)
412 * in order to prevent wlc_rpctx_txreclaim() from screwing wlc_rpctx_getnexttxp(),
413 * which could be invoked by already QUEUED high call(s) from BMAC driver before
414 * wlc_bmac_reset() finishes.
415 * It's not needed before in monolithic driver model because d11core interrupts would
416 * have been cleared instantly in wlc_bmac_reset() and no txstatus interrupt
417 * will come to driver to fetch those flushed dma pkt pointers.
419 wlc->reset_bmac_pending = TRUE;
421 wlc_rpctx_txreclaim(wlc->rpctx);
423 wlc_stf_phy_txant_upd(wlc);
424 wlc_phy_ant_rxdiv_set(wlc->band->pi, wlc->stf->ant_rx_ovr);
425 #endif
428 void wlc_fatal_error(wlc_info_t *wlc)
430 WL_ERROR(("wl%d: fatal error, reinitializing\n", wlc->pub->unit));
431 wl_init(wlc->wl);
434 /* Return the channel the driver should initialize during wlc_init.
435 * the channel may have to be changed from the currently configured channel
436 * if other configurations are in conflict (bandlocked, 11n mode disabled,
437 * invalid channel for current country, etc.)
439 static chanspec_t BCMINITFN(wlc_init_chanspec) (wlc_info_t *wlc)
441 chanspec_t chanspec =
442 1 | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE |
443 WL_CHANSPEC_BAND_2G;
445 /* make sure the channel is on the supported band if we are band-restricted */
446 if (wlc->bandlocked || NBANDS(wlc) == 1) {
447 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
449 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
450 return chanspec;
453 struct scb global_scb;
455 static void wlc_init_scb(wlc_info_t *wlc, struct scb *scb)
457 int i;
458 scb->flags = SCB_WMECAP | SCB_HTCAP;
459 for (i = 0; i < NUMPRIO; i++)
460 scb->seqnum[i] = 0;
463 void BCMINITFN(wlc_init) (wlc_info_t *wlc)
465 d11regs_t *regs;
466 chanspec_t chanspec;
467 int i;
468 wlc_bsscfg_t *bsscfg;
469 bool mute = FALSE;
471 WL_TRACE(("wl%d: wlc_init\n", wlc->pub->unit));
473 regs = wlc->regs;
475 /* This will happen if a big-hammer was executed. In that case, we want to go back
476 * to the channel that we were on and not new channel
478 if (wlc->pub->associated)
479 chanspec = wlc->home_chanspec;
480 else
481 chanspec = wlc_init_chanspec(wlc);
483 wlc_bmac_init(wlc->hw, chanspec, mute);
485 wlc->seckeys = wlc_bmac_read_shm(wlc->hw, M_SECRXKEYS_PTR) * 2;
486 if (D11REV_GE(wlc->pub->corerev, 15) && (wlc->machwcap & MCAP_TKIPMIC))
487 wlc->tkmickeys =
488 wlc_bmac_read_shm(wlc->hw, M_TKMICKEYS_PTR) * 2;
490 /* update beacon listen interval */
491 wlc_bcn_li_upd(wlc);
492 wlc->bcn_wait_prd =
493 (u8) (wlc_bmac_read_shm(wlc->hw, M_NOSLPZNATDTIM) >> 10);
494 ASSERT(wlc->bcn_wait_prd > 0);
496 /* the world is new again, so is our reported rate */
497 wlc_reprate_init(wlc);
499 /* write ethernet address to core */
500 FOREACH_BSS(wlc, i, bsscfg) {
501 wlc_set_mac(bsscfg);
502 wlc_set_bssid(bsscfg);
505 /* Update tsf_cfprep if associated and up */
506 if (wlc->pub->associated) {
507 FOREACH_BSS(wlc, i, bsscfg) {
508 if (bsscfg->up) {
509 u32 bi;
511 /* get beacon period from bsscfg and convert to uS */
512 bi = bsscfg->current_bss->beacon_period << 10;
513 /* update the tsf_cfprep register */
514 /* since init path would reset to default value */
515 W_REG(wlc->osh, &regs->tsf_cfprep,
516 (bi << CFPREP_CBI_SHIFT));
518 /* Update maccontrol PM related bits */
519 wlc_set_ps_ctrl(wlc);
521 break;
526 wlc_key_hw_init_all(wlc);
528 wlc_bandinit_ordered(wlc, chanspec);
530 wlc_init_scb(wlc, &global_scb);
532 /* init probe response timeout */
533 wlc_write_shm(wlc, M_PRS_MAXTIME, wlc->prb_resp_timeout);
535 /* init max burst txop (framebursting) */
536 wlc_write_shm(wlc, M_MBURST_TXOP,
537 (wlc->
538 _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
540 /* initialize maximum allowed duty cycle */
541 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, TRUE, TRUE);
542 wlc_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, FALSE, TRUE);
544 /* Update some shared memory locations related to max AMPDU size allowed to received */
545 wlc_ampdu_shm_upd(wlc->ampdu);
547 /* band-specific inits */
548 wlc_bsinit(wlc);
550 /* Enable EDCF mode (while the MAC is suspended) */
551 if (EDCF_ENAB(wlc->pub)) {
552 OR_REG(wlc->osh, &regs->ifs_ctl, IFS_USEEDCF);
553 wlc_edcf_setparams(wlc->cfg, FALSE);
556 /* Init precedence maps for empty FIFOs */
557 wlc_tx_prec_map_init(wlc);
559 /* read the ucode version if we have not yet done so */
560 if (wlc->ucode_rev == 0) {
561 wlc->ucode_rev =
562 wlc_read_shm(wlc, M_BOM_REV_MAJOR) << NBITS(u16);
563 wlc->ucode_rev |= wlc_read_shm(wlc, M_BOM_REV_MINOR);
566 /* ..now really unleash hell (allow the MAC out of suspend) */
567 wlc_enable_mac(wlc);
569 /* clear tx flow control */
570 wlc_txflowcontrol_reset(wlc);
572 /* clear tx data fifo suspends */
573 wlc->tx_suspended = FALSE;
575 /* enable the RF Disable Delay timer */
576 if (D11REV_GE(wlc->pub->corerev, 10))
577 W_REG(wlc->osh, &wlc->regs->rfdisabledly, RFDISABLE_DEFAULT);
579 /* initialize mpc delay */
580 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
583 * Initialize WME parameters; if they haven't been set by some other
584 * mechanism (IOVar, etc) then read them from the hardware.
586 if (WLC_WME_RETRY_SHORT_GET(wlc, 0) == 0) { /* Unintialized; read from HW */
587 int ac;
589 ASSERT(wlc->clk);
590 for (ac = 0; ac < AC_COUNT; ac++) {
591 wlc->wme_retries[ac] =
592 wlc_read_shm(wlc, M_AC_TXLMT_ADDR(ac));
597 void wlc_mac_bcn_promisc_change(wlc_info_t *wlc, bool promisc)
599 wlc->bcnmisc_monitor = promisc;
600 wlc_mac_bcn_promisc(wlc);
603 void wlc_mac_bcn_promisc(wlc_info_t *wlc)
605 if ((AP_ENAB(wlc->pub) && (N_ENAB(wlc->pub) || wlc->band->gmode)) ||
606 wlc->bcnmisc_ibss || wlc->bcnmisc_scan || wlc->bcnmisc_monitor)
607 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, MCTL_BCNS_PROMISC);
608 else
609 wlc_mctrl(wlc, MCTL_BCNS_PROMISC, 0);
612 /* set or clear maccontrol bits MCTL_PROMISC and MCTL_KEEPCONTROL */
613 void wlc_mac_promisc(wlc_info_t *wlc)
615 u32 promisc_bits = 0;
617 /* promiscuous mode just sets MCTL_PROMISC
618 * Note: APs get all BSS traffic without the need to set the MCTL_PROMISC bit
619 * since all BSS data traffic is directed at the AP
621 if (PROMISC_ENAB(wlc->pub) && !AP_ENAB(wlc->pub) && !wlc->wet)
622 promisc_bits |= MCTL_PROMISC;
624 /* monitor mode needs both MCTL_PROMISC and MCTL_KEEPCONTROL
625 * Note: monitor mode also needs MCTL_BCNS_PROMISC, but that is
626 * handled in wlc_mac_bcn_promisc()
628 if (MONITOR_ENAB(wlc))
629 promisc_bits |= MCTL_PROMISC | MCTL_KEEPCONTROL;
631 wlc_mctrl(wlc, MCTL_PROMISC | MCTL_KEEPCONTROL, promisc_bits);
634 /* check if hps and wake states of sw and hw are in sync */
635 bool wlc_ps_check(wlc_info_t *wlc)
637 bool res = TRUE;
638 bool hps, wake;
639 bool wake_ok;
641 if (!AP_ACTIVE(wlc)) {
642 volatile u32 tmp;
643 tmp = R_REG(wlc->osh, &wlc->regs->maccontrol);
645 /* If deviceremoved is detected, then don't take any action as this can be called
646 * in any context. Assume that caller will take care of the condition. This is just
647 * to avoid assert
649 if (tmp == 0xffffffff) {
650 WL_ERROR(("wl%d: %s: dead chip\n", wlc->pub->unit,
651 __func__));
652 return DEVICEREMOVED(wlc);
655 hps = PS_ALLOWED(wlc);
657 if (hps != ((tmp & MCTL_HPS) != 0)) {
658 int idx;
659 wlc_bsscfg_t *cfg;
660 WL_ERROR(("wl%d: hps not sync, sw %d, maccontrol 0x%x\n", wlc->pub->unit, hps, tmp));
661 FOREACH_BSS(wlc, idx, cfg) {
662 if (!BSSCFG_STA(cfg))
663 continue;
666 res = FALSE;
668 #ifdef WLC_LOW
669 /* For a monolithic build the wake check can be exact since it looks at wake
670 * override bits. The MCTL_WAKE bit should match the 'wake' value.
672 wake = STAY_AWAKE(wlc) || wlc->hw->wake_override;
673 wake_ok = (wake == ((tmp & MCTL_WAKE) != 0));
674 #else
675 /* For a split build we will not have access to any wake overrides from the low
676 * level. The check can only make sure the MCTL_WAKE bit is on if the high
677 * level 'wake' value is true. If the high level 'wake' is false, the MCTL_WAKE
678 * may be either true or false due to the low level override.
680 wake = STAY_AWAKE(wlc);
681 wake_ok = (wake && ((tmp & MCTL_WAKE) != 0)) || !wake;
682 #endif
683 if (hps && !wake_ok) {
684 WL_ERROR(("wl%d: wake not sync, sw %d maccontrol 0x%x\n", wlc->pub->unit, wake, tmp));
685 res = FALSE;
688 ASSERT(res);
689 return res;
692 /* push sw hps and wake state through hardware */
693 void wlc_set_ps_ctrl(wlc_info_t *wlc)
695 u32 v1, v2;
696 bool hps, wake;
697 bool awake_before;
699 hps = PS_ALLOWED(wlc);
700 wake = hps ? (STAY_AWAKE(wlc)) : TRUE;
702 WL_TRACE(("wl%d: wlc_set_ps_ctrl: hps %d wake %d\n", wlc->pub->unit,
703 hps, wake));
705 v1 = R_REG(wlc->osh, &wlc->regs->maccontrol);
706 v2 = 0;
707 if (hps)
708 v2 |= MCTL_HPS;
709 if (wake)
710 v2 |= MCTL_WAKE;
712 wlc_mctrl(wlc, MCTL_WAKE | MCTL_HPS, v2);
714 awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
716 if (wake && !awake_before)
717 wlc_bmac_wait_for_wake(wlc->hw);
722 * Write this BSS config's MAC address to core.
723 * Updates RXE match engine.
725 int wlc_set_mac(wlc_bsscfg_t *cfg)
727 int err = 0;
728 wlc_info_t *wlc = cfg->wlc;
730 if (cfg == wlc->cfg) {
731 /* enter the MAC addr into the RXE match registers */
732 wlc_set_addrmatch(wlc, RCM_MAC_OFFSET, &cfg->cur_etheraddr);
735 wlc_ampdu_macaddr_upd(wlc);
737 return err;
740 /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
741 * Updates RXE match engine.
743 void wlc_set_bssid(wlc_bsscfg_t *cfg)
745 wlc_info_t *wlc = cfg->wlc;
747 /* if primary config, we need to update BSSID in RXE match registers */
748 if (cfg == wlc->cfg) {
749 wlc_set_addrmatch(wlc, RCM_BSSID_OFFSET, &cfg->BSSID);
751 #ifdef SUPPORT_HWKEYS
752 else if (BSSCFG_STA(cfg) && cfg->BSS) {
753 wlc_rcmta_add_bssid(wlc, cfg);
755 #endif
759 * Suspend the the MAC and update the slot timing
760 * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
762 void wlc_switch_shortslot(wlc_info_t *wlc, bool shortslot)
764 int idx;
765 wlc_bsscfg_t *cfg;
767 ASSERT(wlc->band->gmode);
769 /* use the override if it is set */
770 if (wlc->shortslot_override != WLC_SHORTSLOT_AUTO)
771 shortslot = (wlc->shortslot_override == WLC_SHORTSLOT_ON);
773 if (wlc->shortslot == shortslot)
774 return;
776 wlc->shortslot = shortslot;
778 /* update the capability based on current shortslot mode */
779 FOREACH_BSS(wlc, idx, cfg) {
780 if (!cfg->associated)
781 continue;
782 cfg->current_bss->capability &= ~DOT11_CAP_SHORTSLOT;
783 if (wlc->shortslot)
784 cfg->current_bss->capability |= DOT11_CAP_SHORTSLOT;
787 wlc_bmac_set_shortslot(wlc->hw, shortslot);
790 static u8 wlc_local_constraint_qdbm(wlc_info_t *wlc)
792 u8 local;
793 s16 local_max;
795 local = WLC_TXPWR_MAX;
796 if (wlc->pub->associated &&
797 (wf_chspec_ctlchan(wlc->chanspec) ==
798 wf_chspec_ctlchan(wlc->home_chanspec))) {
800 /* get the local power constraint if we are on the AP's
801 * channel [802.11h, 7.3.2.13]
803 /* Clamp the value between 0 and WLC_TXPWR_MAX w/o overflowing the target */
804 local_max =
805 (wlc->txpwr_local_max -
806 wlc->txpwr_local_constraint) * WLC_TXPWR_DB_FACTOR;
807 if (local_max > 0 && local_max < WLC_TXPWR_MAX)
808 return (u8) local_max;
809 if (local_max < 0)
810 return 0;
813 return local;
816 /* propagate home chanspec to all bsscfgs in case bsscfg->current_bss->chanspec is referenced */
817 void wlc_set_home_chanspec(wlc_info_t *wlc, chanspec_t chanspec)
819 if (wlc->home_chanspec != chanspec) {
820 int idx;
821 wlc_bsscfg_t *cfg;
823 wlc->home_chanspec = chanspec;
825 FOREACH_BSS(wlc, idx, cfg) {
826 if (!cfg->associated)
827 continue;
828 cfg->target_bss->chanspec = chanspec;
829 cfg->current_bss->chanspec = chanspec;
835 static void wlc_set_phy_chanspec(wlc_info_t *wlc, chanspec_t chanspec)
837 /* Save our copy of the chanspec */
838 wlc->chanspec = chanspec;
840 /* Set the chanspec and power limits for this locale after computing
841 * any 11h local tx power constraints.
843 wlc_channel_set_chanspec(wlc->cmi, chanspec,
844 wlc_local_constraint_qdbm(wlc));
846 if (wlc->stf->ss_algosel_auto)
847 wlc_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
848 chanspec);
850 wlc_stf_ss_update(wlc, wlc->band);
854 void wlc_set_chanspec(wlc_info_t *wlc, chanspec_t chanspec)
856 uint bandunit;
857 bool switchband = FALSE;
858 chanspec_t old_chanspec = wlc->chanspec;
860 if (!wlc_valid_chanspec_db(wlc->cmi, chanspec)) {
861 WL_ERROR(("wl%d: %s: Bad channel %d\n",
862 wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec)));
863 ASSERT(wlc_valid_chanspec_db(wlc->cmi, chanspec));
864 return;
867 /* Switch bands if necessary */
868 if (NBANDS(wlc) > 1) {
869 bandunit = CHSPEC_WLCBANDUNIT(chanspec);
870 if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
871 switchband = TRUE;
872 if (wlc->bandlocked) {
873 WL_ERROR(("wl%d: %s: chspec %d band is locked!\n", wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec)));
874 return;
876 /* BMAC_NOTE: should the setband call come after the wlc_bmac_chanspec() ?
877 * if the setband updates (wlc_bsinit) use low level calls to inspect and
878 * set state, the state inspected may be from the wrong band, or the
879 * following wlc_bmac_set_chanspec() may undo the work.
881 wlc_setband(wlc, bandunit);
885 ASSERT(N_ENAB(wlc->pub) || !CHSPEC_IS40(chanspec));
887 /* sync up phy/radio chanspec */
888 wlc_set_phy_chanspec(wlc, chanspec);
890 /* init antenna selection */
891 if (CHSPEC_WLC_BW(old_chanspec) != CHSPEC_WLC_BW(chanspec)) {
892 if (WLANTSEL_ENAB(wlc))
893 wlc_antsel_init(wlc->asi);
895 /* Fix the hardware rateset based on bw.
896 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
898 wlc_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
899 wlc->band->
900 mimo_cap_40 ? CHSPEC_WLC_BW(chanspec)
901 : 0);
904 /* update some mac configuration since chanspec changed */
905 wlc_ucode_mac_upd(wlc);
908 #if defined(BCMDBG)
909 static int wlc_get_current_txpwr(wlc_info_t *wlc, void *pwr, uint len)
911 txpwr_limits_t txpwr;
912 tx_power_t power;
913 tx_power_legacy_t *old_power = NULL;
914 int r, c;
915 uint qdbm;
916 bool override;
918 if (len == sizeof(tx_power_legacy_t))
919 old_power = (tx_power_legacy_t *) pwr;
920 else if (len < sizeof(tx_power_t))
921 return BCME_BUFTOOSHORT;
923 bzero(&power, sizeof(tx_power_t));
925 power.chanspec = WLC_BAND_PI_RADIO_CHANSPEC;
926 if (wlc->pub->associated)
927 power.local_chanspec = wlc->home_chanspec;
929 /* Return the user target tx power limits for the various rates. Note wlc_phy.c's
930 * public interface only implements getting and setting a single value for all of
931 * rates, so we need to fill the array ourselves.
933 wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
934 for (r = 0; r < WL_TX_POWER_RATES; r++) {
935 power.user_limit[r] = (u8) qdbm;
938 power.local_max = wlc->txpwr_local_max * WLC_TXPWR_DB_FACTOR;
939 power.local_constraint =
940 wlc->txpwr_local_constraint * WLC_TXPWR_DB_FACTOR;
942 power.antgain[0] = wlc->bandstate[BAND_2G_INDEX]->antgain;
943 power.antgain[1] = wlc->bandstate[BAND_5G_INDEX]->antgain;
945 wlc_channel_reg_limits(wlc->cmi, power.chanspec, &txpwr);
947 #if WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK
948 #error "WL_TX_POWER_CCK_NUM != WLC_NUM_RATES_CCK"
949 #endif
951 /* CCK tx power limits */
952 for (c = 0, r = WL_TX_POWER_CCK_FIRST; c < WL_TX_POWER_CCK_NUM;
953 c++, r++)
954 power.reg_limit[r] = txpwr.cck[c];
956 #if WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM
957 #error "WL_TX_POWER_OFDM_NUM != WLC_NUM_RATES_OFDM"
958 #endif
960 /* 20 MHz OFDM SISO tx power limits */
961 for (c = 0, r = WL_TX_POWER_OFDM_FIRST; c < WL_TX_POWER_OFDM_NUM;
962 c++, r++)
963 power.reg_limit[r] = txpwr.ofdm[c];
965 if (WLC_PHY_11N_CAP(wlc->band)) {
967 /* 20 MHz OFDM CDD tx power limits */
968 for (c = 0, r = WL_TX_POWER_OFDM20_CDD_FIRST;
969 c < WL_TX_POWER_OFDM_NUM; c++, r++)
970 power.reg_limit[r] = txpwr.ofdm_cdd[c];
972 /* 40 MHz OFDM SISO tx power limits */
973 for (c = 0, r = WL_TX_POWER_OFDM40_SISO_FIRST;
974 c < WL_TX_POWER_OFDM_NUM; c++, r++)
975 power.reg_limit[r] = txpwr.ofdm_40_siso[c];
977 /* 40 MHz OFDM CDD tx power limits */
978 for (c = 0, r = WL_TX_POWER_OFDM40_CDD_FIRST;
979 c < WL_TX_POWER_OFDM_NUM; c++, r++)
980 power.reg_limit[r] = txpwr.ofdm_40_cdd[c];
982 #if WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM
983 #error "WL_TX_POWER_MCS_1_STREAM_NUM != WLC_NUM_RATES_MCS_1_STREAM"
984 #endif
986 /* 20MHz MCS0-7 SISO tx power limits */
987 for (c = 0, r = WL_TX_POWER_MCS20_SISO_FIRST;
988 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
989 power.reg_limit[r] = txpwr.mcs_20_siso[c];
991 /* 20MHz MCS0-7 CDD tx power limits */
992 for (c = 0, r = WL_TX_POWER_MCS20_CDD_FIRST;
993 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
994 power.reg_limit[r] = txpwr.mcs_20_cdd[c];
996 /* 20MHz MCS0-7 STBC tx power limits */
997 for (c = 0, r = WL_TX_POWER_MCS20_STBC_FIRST;
998 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
999 power.reg_limit[r] = txpwr.mcs_20_stbc[c];
1001 /* 40MHz MCS0-7 SISO tx power limits */
1002 for (c = 0, r = WL_TX_POWER_MCS40_SISO_FIRST;
1003 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
1004 power.reg_limit[r] = txpwr.mcs_40_siso[c];
1006 /* 40MHz MCS0-7 CDD tx power limits */
1007 for (c = 0, r = WL_TX_POWER_MCS40_CDD_FIRST;
1008 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
1009 power.reg_limit[r] = txpwr.mcs_40_cdd[c];
1011 /* 40MHz MCS0-7 STBC tx power limits */
1012 for (c = 0, r = WL_TX_POWER_MCS40_STBC_FIRST;
1013 c < WLC_NUM_RATES_MCS_1_STREAM; c++, r++)
1014 power.reg_limit[r] = txpwr.mcs_40_stbc[c];
1016 #if WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM
1017 #error "WL_TX_POWER_MCS_2_STREAM_NUM != WLC_NUM_RATES_MCS_2_STREAM"
1018 #endif
1020 /* 20MHz MCS8-15 SDM tx power limits */
1021 for (c = 0, r = WL_TX_POWER_MCS20_SDM_FIRST;
1022 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
1023 power.reg_limit[r] = txpwr.mcs_20_mimo[c];
1025 /* 40MHz MCS8-15 SDM tx power limits */
1026 for (c = 0, r = WL_TX_POWER_MCS40_SDM_FIRST;
1027 c < WLC_NUM_RATES_MCS_2_STREAM; c++, r++)
1028 power.reg_limit[r] = txpwr.mcs_40_mimo[c];
1030 /* MCS 32 */
1031 power.reg_limit[WL_TX_POWER_MCS_32] = txpwr.mcs32;
1034 wlc_phy_txpower_get_current(wlc->band->pi, &power,
1035 CHSPEC_CHANNEL(power.chanspec));
1037 /* copy the tx_power_t struct to the return buffer,
1038 * or convert to a tx_power_legacy_t struct
1040 if (!old_power) {
1041 bcopy(&power, pwr, sizeof(tx_power_t));
1042 } else {
1043 int band_idx = CHSPEC_IS2G(power.chanspec) ? 0 : 1;
1045 bzero(old_power, sizeof(tx_power_legacy_t));
1047 old_power->txpwr_local_max = power.local_max;
1048 old_power->txpwr_local_constraint = power.local_constraint;
1049 if (CHSPEC_IS2G(power.chanspec)) {
1050 old_power->txpwr_chan_reg_max = txpwr.cck[0];
1051 old_power->txpwr_est_Pout[band_idx] =
1052 power.est_Pout_cck;
1053 old_power->txpwr_est_Pout_gofdm = power.est_Pout[0];
1054 } else {
1055 old_power->txpwr_chan_reg_max = txpwr.ofdm[0];
1056 old_power->txpwr_est_Pout[band_idx] = power.est_Pout[0];
1058 old_power->txpwr_antgain[0] = power.antgain[0];
1059 old_power->txpwr_antgain[1] = power.antgain[1];
1061 for (r = 0; r < NUM_PWRCTRL_RATES; r++) {
1062 old_power->txpwr_band_max[r] = power.user_limit[r];
1063 old_power->txpwr_limit[r] = power.reg_limit[r];
1064 old_power->txpwr_target[band_idx][r] = power.target[r];
1065 if (CHSPEC_IS2G(power.chanspec))
1066 old_power->txpwr_bphy_cck_max[r] =
1067 power.board_limit[r];
1068 else
1069 old_power->txpwr_aphy_max[r] =
1070 power.board_limit[r];
1074 return 0;
1076 #endif /* defined(BCMDBG) */
1078 static u32 wlc_watchdog_backup_bi(wlc_info_t *wlc)
1080 u32 bi;
1081 bi = 2 * wlc->cfg->current_bss->dtim_period *
1082 wlc->cfg->current_bss->beacon_period;
1083 if (wlc->bcn_li_dtim)
1084 bi *= wlc->bcn_li_dtim;
1085 else if (wlc->bcn_li_bcn)
1086 /* recalculate bi based on bcn_li_bcn */
1087 bi = 2 * wlc->bcn_li_bcn * wlc->cfg->current_bss->beacon_period;
1089 if (bi < 2 * TIMER_INTERVAL_WATCHDOG)
1090 bi = 2 * TIMER_INTERVAL_WATCHDOG;
1091 return bi;
1094 /* Change to run the watchdog either from a periodic timer or from tbtt handler.
1095 * Call watchdog from tbtt handler if tbtt is TRUE, watchdog timer otherwise.
1097 void wlc_watchdog_upd(wlc_info_t *wlc, bool tbtt)
1099 /* make sure changing watchdog driver is allowed */
1100 if (!wlc->pub->up || !wlc->pub->align_wd_tbtt)
1101 return;
1102 if (!tbtt && wlc->WDarmed) {
1103 wl_del_timer(wlc->wl, wlc->wdtimer);
1104 wlc->WDarmed = FALSE;
1107 /* stop watchdog timer and use tbtt interrupt to drive watchdog */
1108 if (tbtt && wlc->WDarmed) {
1109 wl_del_timer(wlc->wl, wlc->wdtimer);
1110 wlc->WDarmed = FALSE;
1111 wlc->WDlast = OSL_SYSUPTIME();
1113 /* arm watchdog timer and drive the watchdog there */
1114 else if (!tbtt && !wlc->WDarmed) {
1115 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
1116 TRUE);
1117 wlc->WDarmed = TRUE;
1119 if (tbtt && !wlc->WDarmed) {
1120 wl_add_timer(wlc->wl, wlc->wdtimer, wlc_watchdog_backup_bi(wlc),
1121 TRUE);
1122 wlc->WDarmed = TRUE;
1126 ratespec_t wlc_lowest_basic_rspec(wlc_info_t *wlc, wlc_rateset_t *rs)
1128 ratespec_t lowest_basic_rspec;
1129 uint i;
1131 /* Use the lowest basic rate */
1132 lowest_basic_rspec = rs->rates[0] & RATE_MASK;
1133 for (i = 0; i < rs->count; i++) {
1134 if (rs->rates[i] & WLC_RATE_FLAG) {
1135 lowest_basic_rspec = rs->rates[i] & RATE_MASK;
1136 break;
1139 #if NCONF
1140 /* pick siso/cdd as default for OFDM (note no basic rate MCSs are supported yet) */
1141 if (IS_OFDM(lowest_basic_rspec)) {
1142 lowest_basic_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
1144 #endif
1146 return lowest_basic_rspec;
1149 /* This function changes the phytxctl for beacon based on current beacon ratespec AND txant
1150 * setting as per this table:
1151 * ratespec CCK ant = wlc->stf->txant
1152 * OFDM ant = 3
1154 void wlc_beacon_phytxctl_txant_upd(wlc_info_t *wlc, ratespec_t bcn_rspec)
1156 u16 phyctl;
1157 u16 phytxant = wlc->stf->phytxant;
1158 u16 mask = PHY_TXC_ANT_MASK;
1160 /* for non-siso rates or default setting, use the available chains */
1161 if (WLC_PHY_11N_CAP(wlc->band)) {
1162 phytxant = wlc_stf_phytxchain_sel(wlc, bcn_rspec);
1165 phyctl = wlc_read_shm(wlc, M_BCN_PCTLWD);
1166 phyctl = (phyctl & ~mask) | phytxant;
1167 wlc_write_shm(wlc, M_BCN_PCTLWD, phyctl);
1170 /* centralized protection config change function to simplify debugging, no consistency checking
1171 * this should be called only on changes to avoid overhead in periodic function
1173 void wlc_protection_upd(wlc_info_t *wlc, uint idx, int val)
1175 WL_TRACE(("wlc_protection_upd: idx %d, val %d\n", idx, val));
1177 switch (idx) {
1178 case WLC_PROT_G_SPEC:
1179 wlc->protection->_g = (bool) val;
1180 break;
1181 case WLC_PROT_G_OVR:
1182 wlc->protection->g_override = (s8) val;
1183 break;
1184 case WLC_PROT_G_USER:
1185 wlc->protection->gmode_user = (u8) val;
1186 break;
1187 case WLC_PROT_OVERLAP:
1188 wlc->protection->overlap = (s8) val;
1189 break;
1190 case WLC_PROT_N_USER:
1191 wlc->protection->nmode_user = (s8) val;
1192 break;
1193 case WLC_PROT_N_CFG:
1194 wlc->protection->n_cfg = (s8) val;
1195 break;
1196 case WLC_PROT_N_CFG_OVR:
1197 wlc->protection->n_cfg_override = (s8) val;
1198 break;
1199 case WLC_PROT_N_NONGF:
1200 wlc->protection->nongf = (bool) val;
1201 break;
1202 case WLC_PROT_N_NONGF_OVR:
1203 wlc->protection->nongf_override = (s8) val;
1204 break;
1205 case WLC_PROT_N_PAM_OVR:
1206 wlc->protection->n_pam_override = (s8) val;
1207 break;
1208 case WLC_PROT_N_OBSS:
1209 wlc->protection->n_obss = (bool) val;
1210 break;
1212 default:
1213 ASSERT(0);
1214 break;
1219 static void wlc_ht_update_sgi_rx(wlc_info_t *wlc, int val)
1221 wlc->ht_cap.cap &= ~(HT_CAP_SHORT_GI_20 | HT_CAP_SHORT_GI_40);
1222 wlc->ht_cap.cap |= (val & WLC_N_SGI_20) ? HT_CAP_SHORT_GI_20 : 0;
1223 wlc->ht_cap.cap |= (val & WLC_N_SGI_40) ? HT_CAP_SHORT_GI_40 : 0;
1225 if (wlc->pub->up) {
1226 wlc_update_beacon(wlc);
1227 wlc_update_probe_resp(wlc, TRUE);
1231 static void wlc_ht_update_ldpc(wlc_info_t *wlc, s8 val)
1233 wlc->stf->ldpc = val;
1235 wlc->ht_cap.cap &= ~HT_CAP_LDPC_CODING;
1236 if (wlc->stf->ldpc != OFF)
1237 wlc->ht_cap.cap |= HT_CAP_LDPC_CODING;
1239 if (wlc->pub->up) {
1240 wlc_update_beacon(wlc);
1241 wlc_update_probe_resp(wlc, TRUE);
1242 wlc_phy_ldpc_override_set(wlc->band->pi, (val ? TRUE : FALSE));
1247 * ucode, hwmac update
1248 * Channel dependent updates for ucode and hw
1250 static void wlc_ucode_mac_upd(wlc_info_t *wlc)
1252 /* enable or disable any active IBSSs depending on whether or not
1253 * we are on the home channel
1255 if (wlc->home_chanspec == WLC_BAND_PI_RADIO_CHANSPEC) {
1256 if (wlc->pub->associated) {
1257 /* BMAC_NOTE: This is something that should be fixed in ucode inits.
1258 * I think that the ucode inits set up the bcn templates and shm values
1259 * with a bogus beacon. This should not be done in the inits. If ucode needs
1260 * to set up a beacon for testing, the test routines should write it down,
1261 * not expect the inits to populate a bogus beacon.
1263 if (WLC_PHY_11N_CAP(wlc->band)) {
1264 wlc_write_shm(wlc, M_BCN_TXTSF_OFFSET,
1265 wlc->band->bcntsfoff);
1268 } else {
1269 /* disable an active IBSS if we are not on the home channel */
1272 /* update the various promisc bits */
1273 wlc_mac_bcn_promisc(wlc);
1274 wlc_mac_promisc(wlc);
1277 static void wlc_bandinit_ordered(wlc_info_t *wlc, chanspec_t chanspec)
1279 wlc_rateset_t default_rateset;
1280 uint parkband;
1281 uint i, band_order[2];
1283 WL_TRACE(("wl%d: wlc_bandinit_ordered\n", wlc->pub->unit));
1285 * We might have been bandlocked during down and the chip power-cycled (hibernate).
1286 * figure out the right band to park on
1288 if (wlc->bandlocked || NBANDS(wlc) == 1) {
1289 ASSERT(CHSPEC_WLCBANDUNIT(chanspec) == wlc->band->bandunit);
1291 parkband = wlc->band->bandunit; /* updated in wlc_bandlock() */
1292 band_order[0] = band_order[1] = parkband;
1293 } else {
1294 /* park on the band of the specified chanspec */
1295 parkband = CHSPEC_WLCBANDUNIT(chanspec);
1297 /* order so that parkband initialize last */
1298 band_order[0] = parkband ^ 1;
1299 band_order[1] = parkband;
1302 /* make each band operational, software state init */
1303 for (i = 0; i < NBANDS(wlc); i++) {
1304 uint j = band_order[i];
1306 wlc->band = wlc->bandstate[j];
1308 wlc_default_rateset(wlc, &default_rateset);
1310 /* fill in hw_rate */
1311 wlc_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
1312 FALSE, WLC_RATES_CCK_OFDM, RATE_MASK,
1313 (bool) N_ENAB(wlc->pub));
1315 /* init basic rate lookup */
1316 wlc_rate_lookup_init(wlc, &default_rateset);
1319 /* sync up phy/radio chanspec */
1320 wlc_set_phy_chanspec(wlc, chanspec);
1323 /* band-specific init */
1324 static void WLBANDINITFN(wlc_bsinit) (wlc_info_t *wlc)
1326 WL_TRACE(("wl%d: wlc_bsinit: bandunit %d\n", wlc->pub->unit,
1327 wlc->band->bandunit));
1329 /* write ucode ACK/CTS rate table */
1330 wlc_set_ratetable(wlc);
1332 /* update some band specific mac configuration */
1333 wlc_ucode_mac_upd(wlc);
1335 /* init antenna selection */
1336 if (WLANTSEL_ENAB(wlc))
1337 wlc_antsel_init(wlc->asi);
1341 /* switch to and initialize new band */
1342 static void WLBANDINITFN(wlc_setband) (wlc_info_t *wlc, uint bandunit)
1344 int idx;
1345 wlc_bsscfg_t *cfg;
1347 ASSERT(NBANDS(wlc) > 1);
1348 ASSERT(!wlc->bandlocked);
1349 ASSERT(bandunit != wlc->band->bandunit || wlc->bandinit_pending);
1351 wlc->band = wlc->bandstate[bandunit];
1353 if (!wlc->pub->up)
1354 return;
1356 /* wait for at least one beacon before entering sleeping state */
1357 wlc->PMawakebcn = TRUE;
1358 FOREACH_AS_STA(wlc, idx, cfg)
1359 cfg->PMawakebcn = TRUE;
1360 wlc_set_ps_ctrl(wlc);
1362 /* band-specific initializations */
1363 wlc_bsinit(wlc);
1366 /* Initialize a WME Parameter Info Element with default STA parameters from WMM Spec, Table 12 */
1367 void wlc_wme_initparams_sta(wlc_info_t *wlc, wme_param_ie_t *pe)
1369 static const wme_param_ie_t stadef = {
1370 WME_OUI,
1371 WME_TYPE,
1372 WME_SUBTYPE_PARAM_IE,
1373 WME_VER,
1377 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA,
1378 HTOL16(EDCF_AC_BE_TXOP_STA)},
1379 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA,
1380 HTOL16(EDCF_AC_BK_TXOP_STA)},
1381 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA,
1382 HTOL16(EDCF_AC_VI_TXOP_STA)},
1383 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA,
1384 HTOL16(EDCF_AC_VO_TXOP_STA)}
1388 ASSERT(sizeof(*pe) == WME_PARAM_IE_LEN);
1389 memcpy(pe, &stadef, sizeof(*pe));
1392 void wlc_wme_setparams(wlc_info_t *wlc, u16 aci, void *arg, bool suspend)
1394 int i;
1395 shm_acparams_t acp_shm;
1396 u16 *shm_entry;
1397 struct ieee80211_tx_queue_params *params = arg;
1399 ASSERT(wlc);
1401 /* Only apply params if the core is out of reset and has clocks */
1402 if (!wlc->clk) {
1403 WL_ERROR(("wl%d: %s : no-clock\n", wlc->pub->unit, __func__));
1404 return;
1408 * AP uses AC params from wme_param_ie_ap.
1409 * AP advertises AC params from wme_param_ie.
1410 * STA uses AC params from wme_param_ie.
1413 wlc->wme_admctl = 0;
1415 do {
1416 bzero((char *)&acp_shm, sizeof(shm_acparams_t));
1417 /* find out which ac this set of params applies to */
1418 ASSERT(aci < AC_COUNT);
1419 /* set the admission control policy for this AC */
1420 /* wlc->wme_admctl |= 1 << aci; *//* should be set ?? seems like off by default */
1422 /* fill in shm ac params struct */
1423 acp_shm.txop = ltoh16(params->txop);
1424 /* convert from units of 32us to us for ucode */
1425 wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
1426 EDCF_TXOP2USEC(acp_shm.txop);
1427 acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
1429 if (aci == AC_VI && acp_shm.txop == 0
1430 && acp_shm.aifs < EDCF_AIFSN_MAX)
1431 acp_shm.aifs++;
1433 if (acp_shm.aifs < EDCF_AIFSN_MIN
1434 || acp_shm.aifs > EDCF_AIFSN_MAX) {
1435 WL_ERROR(("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1436 wlc->pub->unit, acp_shm.aifs));
1437 continue;
1440 acp_shm.cwmin = params->cw_min;
1441 acp_shm.cwmax = params->cw_max;
1442 acp_shm.cwcur = acp_shm.cwmin;
1443 acp_shm.bslots =
1444 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1445 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1446 /* Indicate the new params to the ucode */
1447 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1448 wme_shmemacindex(aci) *
1449 M_EDCF_QLEN +
1450 M_EDCF_STATUS_OFF));
1451 acp_shm.status |= WME_STATUS_NEWAC;
1453 /* Fill in shm acparam table */
1454 shm_entry = (u16 *) &acp_shm;
1455 for (i = 0; i < (int)sizeof(shm_acparams_t); i += 2)
1456 wlc_write_shm(wlc,
1457 M_EDCF_QINFO +
1458 wme_shmemacindex(aci) * M_EDCF_QLEN + i,
1459 *shm_entry++);
1461 } while (0);
1463 if (suspend)
1464 wlc_suspend_mac_and_wait(wlc);
1466 if (suspend)
1467 wlc_enable_mac(wlc);
1471 void wlc_edcf_setparams(wlc_bsscfg_t *cfg, bool suspend)
1473 wlc_info_t *wlc = cfg->wlc;
1474 uint aci, i, j;
1475 edcf_acparam_t *edcf_acp;
1476 shm_acparams_t acp_shm;
1477 u16 *shm_entry;
1479 ASSERT(cfg);
1480 ASSERT(wlc);
1482 /* Only apply params if the core is out of reset and has clocks */
1483 if (!wlc->clk)
1484 return;
1487 * AP uses AC params from wme_param_ie_ap.
1488 * AP advertises AC params from wme_param_ie.
1489 * STA uses AC params from wme_param_ie.
1492 edcf_acp = (edcf_acparam_t *) &wlc->wme_param_ie.acparam[0];
1494 wlc->wme_admctl = 0;
1496 for (i = 0; i < AC_COUNT; i++, edcf_acp++) {
1497 bzero((char *)&acp_shm, sizeof(shm_acparams_t));
1498 /* find out which ac this set of params applies to */
1499 aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
1500 ASSERT(aci < AC_COUNT);
1501 /* set the admission control policy for this AC */
1502 if (edcf_acp->ACI & EDCF_ACM_MASK) {
1503 wlc->wme_admctl |= 1 << aci;
1506 /* fill in shm ac params struct */
1507 acp_shm.txop = ltoh16(edcf_acp->TXOP);
1508 /* convert from units of 32us to us for ucode */
1509 wlc->edcf_txop[aci] = acp_shm.txop =
1510 EDCF_TXOP2USEC(acp_shm.txop);
1511 acp_shm.aifs = (edcf_acp->ACI & EDCF_AIFSN_MASK);
1513 if (aci == AC_VI && acp_shm.txop == 0
1514 && acp_shm.aifs < EDCF_AIFSN_MAX)
1515 acp_shm.aifs++;
1517 if (acp_shm.aifs < EDCF_AIFSN_MIN
1518 || acp_shm.aifs > EDCF_AIFSN_MAX) {
1519 WL_ERROR(("wl%d: wlc_edcf_setparams: bad aifs %d\n",
1520 wlc->pub->unit, acp_shm.aifs));
1521 continue;
1524 /* CWmin = 2^(ECWmin) - 1 */
1525 acp_shm.cwmin = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
1526 /* CWmax = 2^(ECWmax) - 1 */
1527 acp_shm.cwmax = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
1528 >> EDCF_ECWMAX_SHIFT);
1529 acp_shm.cwcur = acp_shm.cwmin;
1530 acp_shm.bslots =
1531 R_REG(wlc->osh, &wlc->regs->tsf_random) & acp_shm.cwcur;
1532 acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
1533 /* Indicate the new params to the ucode */
1534 acp_shm.status = wlc_read_shm(wlc, (M_EDCF_QINFO +
1535 wme_shmemacindex(aci) *
1536 M_EDCF_QLEN +
1537 M_EDCF_STATUS_OFF));
1538 acp_shm.status |= WME_STATUS_NEWAC;
1540 /* Fill in shm acparam table */
1541 shm_entry = (u16 *) &acp_shm;
1542 for (j = 0; j < (int)sizeof(shm_acparams_t); j += 2)
1543 wlc_write_shm(wlc,
1544 M_EDCF_QINFO +
1545 wme_shmemacindex(aci) * M_EDCF_QLEN + j,
1546 *shm_entry++);
1549 if (suspend)
1550 wlc_suspend_mac_and_wait(wlc);
1552 if (AP_ENAB(wlc->pub) && WME_ENAB(wlc->pub)) {
1553 wlc_update_beacon(wlc);
1554 wlc_update_probe_resp(wlc, FALSE);
1557 if (suspend)
1558 wlc_enable_mac(wlc);
1562 bool wlc_timers_init(wlc_info_t *wlc, int unit)
1564 wlc->wdtimer = wl_init_timer(wlc->wl, wlc_watchdog_by_timer,
1565 wlc, "watchdog");
1566 if (!wlc->wdtimer) {
1567 WL_ERROR(("wl%d: wl_init_timer for wdtimer failed\n", unit));
1568 goto fail;
1571 wlc->radio_timer = wl_init_timer(wlc->wl, wlc_radio_timer,
1572 wlc, "radio");
1573 if (!wlc->radio_timer) {
1574 WL_ERROR(("wl%d: wl_init_timer for radio_timer failed\n",
1575 unit));
1576 goto fail;
1579 return TRUE;
1581 fail:
1582 return FALSE;
1586 * Initialize wlc_info default values ...
1587 * may get overrides later in this function
1589 void wlc_info_init(wlc_info_t *wlc, int unit)
1591 int i;
1592 /* Assume the device is there until proven otherwise */
1593 wlc->device_present = TRUE;
1595 /* set default power output percentage to 100 percent */
1596 wlc->txpwr_percent = 100;
1598 /* Save our copy of the chanspec */
1599 wlc->chanspec = CH20MHZ_CHSPEC(1);
1601 /* initialize CCK preamble mode to unassociated state */
1602 wlc->shortpreamble = FALSE;
1604 wlc->legacy_probe = TRUE;
1606 /* various 802.11g modes */
1607 wlc->shortslot = FALSE;
1608 wlc->shortslot_override = WLC_SHORTSLOT_AUTO;
1610 wlc->barker_overlap_control = TRUE;
1611 wlc->barker_preamble = WLC_BARKER_SHORT_ALLOWED;
1612 wlc->txburst_limit_override = AUTO;
1614 wlc_protection_upd(wlc, WLC_PROT_G_OVR, WLC_PROTECTION_AUTO);
1615 wlc_protection_upd(wlc, WLC_PROT_G_SPEC, FALSE);
1617 wlc_protection_upd(wlc, WLC_PROT_N_CFG_OVR, WLC_PROTECTION_AUTO);
1618 wlc_protection_upd(wlc, WLC_PROT_N_CFG, WLC_N_PROTECTION_OFF);
1619 wlc_protection_upd(wlc, WLC_PROT_N_NONGF_OVR, WLC_PROTECTION_AUTO);
1620 wlc_protection_upd(wlc, WLC_PROT_N_NONGF, FALSE);
1621 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR, AUTO);
1623 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, WLC_PROTECTION_CTL_OVERLAP);
1625 /* 802.11g draft 4.0 NonERP elt advertisement */
1626 wlc->include_legacy_erp = TRUE;
1628 wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
1629 wlc->stf->txant = ANT_TX_DEF;
1631 wlc->prb_resp_timeout = WLC_PRB_RESP_TIMEOUT;
1633 wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
1634 for (i = 0; i < NFIFO; i++)
1635 wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
1636 wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
1638 /* default rate fallback retry limits */
1639 wlc->SFBL = RETRY_SHORT_FB;
1640 wlc->LFBL = RETRY_LONG_FB;
1642 /* default mac retry limits */
1643 wlc->SRL = RETRY_SHORT_DEF;
1644 wlc->LRL = RETRY_LONG_DEF;
1646 /* init PM state */
1647 wlc->PM = PM_OFF; /* User's setting of PM mode through IOCTL */
1648 wlc->PM_override = FALSE; /* Prevents from going to PM if our AP is 'ill' */
1649 wlc->PMenabled = FALSE; /* Current PM state */
1650 wlc->PMpending = FALSE; /* Tracks whether STA indicated PM in the last attempt */
1651 wlc->PMblocked = FALSE; /* To allow blocking going into PM during RM and scans */
1653 /* In WMM Auto mode, PM is allowed if association is a UAPSD association */
1654 wlc->WME_PM_blocked = FALSE;
1656 /* Init wme queuing method */
1657 wlc->wme_prec_queuing = FALSE;
1659 /* Overrides for the core to stay awake under zillion conditions Look for STAY_AWAKE */
1660 wlc->wake = FALSE;
1661 /* Are we waiting for a response to PS-Poll that we sent */
1662 wlc->PSpoll = FALSE;
1664 /* APSD defaults */
1665 wlc->wme_apsd = TRUE;
1666 wlc->apsd_sta_usp = FALSE;
1667 wlc->apsd_trigger_timeout = 0; /* disable the trigger timer */
1668 wlc->apsd_trigger_ac = AC_BITMAP_ALL;
1670 /* Set flag to indicate that hw keys should be used when available. */
1671 wlc->wsec_swkeys = FALSE;
1673 /* init the 4 static WEP default keys */
1674 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
1675 wlc->wsec_keys[i] = wlc->wsec_def_keys[i];
1676 wlc->wsec_keys[i]->idx = (u8) i;
1679 wlc->_regulatory_domain = FALSE; /* 802.11d */
1681 /* WME QoS mode is Auto by default */
1682 wlc->pub->_wme = AUTO;
1684 #ifdef BCMSDIODEV_ENABLED
1685 wlc->pub->_priofc = TRUE; /* enable priority flow control for sdio dongle */
1686 #endif
1688 wlc->pub->_ampdu = AMPDU_AGG_HOST;
1689 wlc->pub->bcmerror = 0;
1690 wlc->ibss_allowed = TRUE;
1691 wlc->ibss_coalesce_allowed = TRUE;
1692 wlc->pub->_coex = ON;
1694 /* intialize mpc delay */
1695 wlc->mpc_delay_off = wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
1697 wlc->pr80838_war = TRUE;
1700 static bool wlc_state_bmac_sync(wlc_info_t *wlc)
1702 wlc_bmac_state_t state_bmac;
1704 if (wlc_bmac_state_get(wlc->hw, &state_bmac) != 0)
1705 return FALSE;
1707 wlc->machwcap = state_bmac.machwcap;
1708 wlc_protection_upd(wlc, WLC_PROT_N_PAM_OVR,
1709 (s8) state_bmac.preamble_ovr);
1711 return TRUE;
1714 static uint wlc_attach_module(wlc_info_t *wlc)
1716 uint err = 0;
1717 uint unit;
1718 unit = wlc->pub->unit;
1720 wlc->asi = wlc_antsel_attach(wlc, wlc->osh, wlc->pub, wlc->hw);
1721 if (wlc->asi == NULL) {
1722 WL_ERROR(("wl%d: wlc_attach: wlc_antsel_attach failed\n",
1723 unit));
1724 err = 44;
1725 goto fail;
1728 wlc->ampdu = wlc_ampdu_attach(wlc);
1729 if (wlc->ampdu == NULL) {
1730 WL_ERROR(("wl%d: wlc_attach: wlc_ampdu_attach failed\n", unit));
1731 err = 50;
1732 goto fail;
1735 /* Initialize event queue; needed before following calls */
1736 wlc->eventq =
1737 wlc_eventq_attach(wlc->pub, wlc, wlc->wl, wlc_process_eventq);
1738 if (wlc->eventq == NULL) {
1739 WL_ERROR(("wl%d: wlc_attach: wlc_eventq_attachfailed\n", unit));
1740 err = 57;
1741 goto fail;
1744 if ((wlc_stf_attach(wlc) != 0)) {
1745 WL_ERROR(("wl%d: wlc_attach: wlc_stf_attach failed\n", unit));
1746 err = 68;
1747 goto fail;
1749 fail:
1750 return err;
1753 wlc_pub_t *wlc_pub(void *wlc)
1755 return ((wlc_info_t *) wlc)->pub;
1758 #define CHIP_SUPPORTS_11N(wlc) 1
1761 * The common driver entry routine. Error codes should be unique
1763 void *wlc_attach(void *wl, u16 vendor, u16 device, uint unit, bool piomode,
1764 osl_t *osh, void *regsva, uint bustype, void *btparam,
1765 uint *perr)
1767 wlc_info_t *wlc;
1768 uint err = 0;
1769 uint j;
1770 wlc_pub_t *pub;
1771 wlc_txq_info_t *qi;
1772 uint n_disabled;
1774 WL_NONE(("wl%d: %s: vendor 0x%x device 0x%x\n", unit, __func__, vendor,
1775 device));
1777 ASSERT(WSEC_MAX_RCMTA_KEYS <= WSEC_MAX_KEYS);
1778 ASSERT(WSEC_MAX_DEFAULT_KEYS == WLC_DEFAULT_KEYS);
1780 /* some code depends on packed structures */
1781 ASSERT(sizeof(struct ether_addr) == ETHER_ADDR_LEN);
1782 ASSERT(sizeof(struct ether_header) == ETHER_HDR_LEN);
1783 ASSERT(sizeof(d11regs_t) == SI_CORE_SIZE);
1784 ASSERT(sizeof(ofdm_phy_hdr_t) == D11_PHY_HDR_LEN);
1785 ASSERT(sizeof(cck_phy_hdr_t) == D11_PHY_HDR_LEN);
1786 ASSERT(sizeof(d11txh_t) == D11_TXH_LEN);
1787 ASSERT(sizeof(d11rxhdr_t) == RXHDR_LEN);
1788 ASSERT(sizeof(struct dot11_header) == DOT11_A4_HDR_LEN);
1789 ASSERT(sizeof(struct dot11_rts_frame) == DOT11_RTS_LEN);
1790 ASSERT(sizeof(struct dot11_management_header) == DOT11_MGMT_HDR_LEN);
1791 ASSERT(sizeof(struct dot11_bcn_prb) == DOT11_BCN_PRB_LEN);
1792 ASSERT(sizeof(tx_status_t) == TXSTATUS_LEN);
1793 ASSERT(sizeof(ht_cap_ie_t) == HT_CAP_IE_LEN);
1794 ASSERT(offsetof(wl_scan_params_t, channel_list) ==
1795 WL_SCAN_PARAMS_FIXED_SIZE);
1796 ASSERT(IS_ALIGNED(offsetof(wsec_key_t, data), sizeof(u32)));
1797 ASSERT(ISPOWEROF2(MA_WINDOW_SZ));
1799 ASSERT(sizeof(wlc_d11rxhdr_t) <= WL_HWRXOFF);
1802 * Number of replay counters value used in WPA IE must match # rxivs
1803 * supported in wsec_key_t struct. See 802.11i/D3.0 sect. 7.3.2.17
1804 * 'RSN Information Element' figure 8 for this mapping.
1806 ASSERT((WPA_CAP_16_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1807 && 16 == WLC_NUMRXIVS)
1808 || (WPA_CAP_4_REPLAY_CNTRS == WLC_REPLAY_CNTRS_VALUE
1809 && 4 == WLC_NUMRXIVS));
1811 /* allocate wlc_info_t state and its substructures */
1812 wlc = (wlc_info_t *) wlc_attach_malloc(osh, unit, &err, device);
1813 if (wlc == NULL)
1814 goto fail;
1815 wlc->osh = osh;
1816 pub = wlc->pub;
1818 #if defined(BCMDBG)
1819 wlc_info_dbg = wlc;
1820 #endif
1822 wlc->band = wlc->bandstate[0];
1823 wlc->core = wlc->corestate;
1824 wlc->wl = wl;
1825 pub->unit = unit;
1826 pub->osh = osh;
1827 wlc->btparam = btparam;
1828 pub->_piomode = piomode;
1829 wlc->bandinit_pending = FALSE;
1830 /* By default restrict TKIP associations from 11n STA's */
1831 wlc->ht_wsec_restriction = WLC_HT_TKIP_RESTRICT;
1833 /* populate wlc_info_t with default values */
1834 wlc_info_init(wlc, unit);
1836 /* update sta/ap related parameters */
1837 wlc_ap_upd(wlc);
1839 /* 11n_disable nvram */
1840 n_disabled = getintvar(pub->vars, "11n_disable");
1842 /* register a module (to handle iovars) */
1843 wlc_module_register(wlc->pub, wlc_iovars, "wlc_iovars", wlc,
1844 wlc_doiovar, NULL, NULL);
1846 /* low level attach steps(all hw accesses go inside, no more in rest of the attach) */
1847 err = wlc_bmac_attach(wlc, vendor, device, unit, piomode, osh, regsva,
1848 bustype, btparam);
1849 if (err)
1850 goto fail;
1852 /* for some states, due to different info pointer(e,g, wlc, wlc_hw) or master/slave split,
1853 * HIGH driver(both monolithic and HIGH_ONLY) needs to sync states FROM BMAC portion driver
1855 if (!wlc_state_bmac_sync(wlc)) {
1856 err = 20;
1857 goto fail;
1860 pub->phy_11ncapable = WLC_PHY_11N_CAP(wlc->band);
1862 /* propagate *vars* from BMAC driver to high driver */
1863 wlc_bmac_copyfrom_vars(wlc->hw, &pub->vars, &wlc->vars_size);
1865 #ifdef WLC_HIGH_ONLY
1866 WL_TRACE(("nvram : vars %p , vars_size %d\n", pub->vars,
1867 wlc->vars_size));
1868 #endif
1870 /* set maximum allowed duty cycle */
1871 wlc->tx_duty_cycle_ofdm =
1872 (u16) getintvar(pub->vars, "tx_duty_cycle_ofdm");
1873 wlc->tx_duty_cycle_cck =
1874 (u16) getintvar(pub->vars, "tx_duty_cycle_cck");
1876 wlc_stf_phy_chain_calc(wlc);
1878 /* txchain 1: txant 0, txchain 2: txant 1 */
1879 if (WLCISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
1880 wlc->stf->txant = wlc->stf->hw_txchain - 1;
1882 /* push to BMAC driver */
1883 wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
1884 wlc->stf->hw_rxchain);
1886 #ifdef WLC_LOW
1887 /* pull up some info resulting from the low attach */
1889 int i;
1890 for (i = 0; i < NFIFO; i++)
1891 wlc->core->txavail[i] = wlc->hw->txavail[i];
1893 #endif /* WLC_LOW */
1895 wlc_bmac_hw_etheraddr(wlc->hw, &wlc->perm_etheraddr);
1897 bcopy((char *)&wlc->perm_etheraddr, (char *)&pub->cur_etheraddr,
1898 ETHER_ADDR_LEN);
1900 for (j = 0; j < NBANDS(wlc); j++) {
1901 /* Use band 1 for single band 11a */
1902 if (IS_SINGLEBAND_5G(wlc->deviceid))
1903 j = BAND_5G_INDEX;
1905 wlc->band = wlc->bandstate[j];
1907 if (!wlc_attach_stf_ant_init(wlc)) {
1908 err = 24;
1909 goto fail;
1912 /* default contention windows size limits */
1913 wlc->band->CWmin = APHY_CWMIN;
1914 wlc->band->CWmax = PHY_CWMAX;
1916 /* init gmode value */
1917 if (BAND_2G(wlc->band->bandtype)) {
1918 wlc->band->gmode = GMODE_AUTO;
1919 wlc_protection_upd(wlc, WLC_PROT_G_USER,
1920 wlc->band->gmode);
1923 /* init _n_enab supported mode */
1924 if (WLC_PHY_11N_CAP(wlc->band) && CHIP_SUPPORTS_11N(wlc)) {
1925 if (n_disabled & WLFEATURE_DISABLE_11N) {
1926 pub->_n_enab = OFF;
1927 wlc_protection_upd(wlc, WLC_PROT_N_USER, OFF);
1928 } else {
1929 pub->_n_enab = SUPPORT_11N;
1930 wlc_protection_upd(wlc, WLC_PROT_N_USER,
1931 ((pub->_n_enab ==
1932 SUPPORT_11N) ? WL_11N_2x2 :
1933 WL_11N_3x3));
1937 /* init per-band default rateset, depend on band->gmode */
1938 wlc_default_rateset(wlc, &wlc->band->defrateset);
1940 /* fill in hw_rateset (used early by WLC_SET_RATESET) */
1941 wlc_rateset_filter(&wlc->band->defrateset,
1942 &wlc->band->hw_rateset, FALSE,
1943 WLC_RATES_CCK_OFDM, RATE_MASK,
1944 (bool) N_ENAB(wlc->pub));
1947 /* update antenna config due to wlc->stf->txant/txchain/ant_rx_ovr change */
1948 wlc_stf_phy_txant_upd(wlc);
1950 /* attach each modules */
1951 err = wlc_attach_module(wlc);
1952 if (err != 0)
1953 goto fail;
1955 if (!wlc_timers_init(wlc, unit)) {
1956 WL_ERROR(("wl%d: %s: wlc_init_timer failed\n", unit, __func__));
1957 err = 32;
1958 goto fail;
1961 /* depend on rateset, gmode */
1962 wlc->cmi = wlc_channel_mgr_attach(wlc);
1963 if (!wlc->cmi) {
1964 WL_ERROR(("wl%d: %s: wlc_channel_mgr_attach failed\n", unit,
1965 __func__));
1966 err = 33;
1967 goto fail;
1970 /* init default when all parameters are ready, i.e. ->rateset */
1971 wlc_bss_default_init(wlc);
1974 * Complete the wlc default state initializations..
1977 /* allocate our initial queue */
1978 qi = wlc_txq_alloc(wlc, osh);
1979 if (qi == NULL) {
1980 WL_ERROR(("wl%d: %s: failed to malloc tx queue\n", unit,
1981 __func__));
1982 err = 100;
1983 goto fail;
1985 wlc->active_queue = qi;
1987 wlc->bsscfg[0] = wlc->cfg;
1988 wlc->cfg->_idx = 0;
1989 wlc->cfg->wlc = wlc;
1990 pub->txmaxpkts = MAXTXPKTS;
1992 WLCNTSET(pub->_cnt->version, WL_CNT_T_VERSION);
1993 WLCNTSET(pub->_cnt->length, sizeof(wl_cnt_t));
1995 WLCNTSET(pub->_wme_cnt->version, WL_WME_CNT_VERSION);
1996 WLCNTSET(pub->_wme_cnt->length, sizeof(wl_wme_cnt_t));
1998 wlc_wme_initparams_sta(wlc, &wlc->wme_param_ie);
2000 wlc->mimoft = FT_HT;
2001 wlc->ht_cap.cap = HT_CAP;
2002 if (HT_ENAB(wlc->pub))
2003 wlc->stf->ldpc = AUTO;
2005 wlc->mimo_40txbw = AUTO;
2006 wlc->ofdm_40txbw = AUTO;
2007 wlc->cck_40txbw = AUTO;
2008 wlc_update_mimo_band_bwcap(wlc, WLC_N_BW_20IN2G_40IN5G);
2010 /* Enable setting the RIFS Mode bit by default in HT Info IE */
2011 wlc->rifs_advert = AUTO;
2013 /* Set default values of SGI */
2014 if (WLC_SGI_CAP_PHY(wlc)) {
2015 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
2016 wlc->sgi_tx = AUTO;
2017 } else if (WLCISSSLPNPHY(wlc->band)) {
2018 wlc_ht_update_sgi_rx(wlc, (WLC_N_SGI_20 | WLC_N_SGI_40));
2019 wlc->sgi_tx = AUTO;
2020 } else {
2021 wlc_ht_update_sgi_rx(wlc, 0);
2022 wlc->sgi_tx = OFF;
2025 /* *******nvram 11n config overrides Start ********* */
2027 /* apply the sgi override from nvram conf */
2028 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_TX)
2029 wlc->sgi_tx = OFF;
2031 if (n_disabled & WLFEATURE_DISABLE_11N_SGI_RX)
2032 wlc_ht_update_sgi_rx(wlc, 0);
2034 /* apply the stbc override from nvram conf */
2035 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_TX) {
2036 wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
2037 wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
2038 wlc->ht_cap.cap &= ~HT_CAP_TX_STBC;
2040 if (n_disabled & WLFEATURE_DISABLE_11N_STBC_RX)
2041 wlc_stf_stbc_rx_set(wlc, HT_CAP_RX_STBC_NO);
2043 /* apply the GF override from nvram conf */
2044 if (n_disabled & WLFEATURE_DISABLE_11N_GF)
2045 wlc->ht_cap.cap &= ~HT_CAP_GF;
2047 /* initialize radio_mpc_disable according to wlc->mpc */
2048 wlc_radio_mpc_upd(wlc);
2050 if (WLANTSEL_ENAB(wlc)) {
2051 if ((CHIPID(wlc->pub->sih->chip)) == BCM43235_CHIP_ID) {
2052 if ((getintvar(wlc->pub->vars, "aa2g") == 7) ||
2053 (getintvar(wlc->pub->vars, "aa5g") == 7)) {
2054 wlc_bmac_antsel_set(wlc->hw, 1);
2056 } else {
2057 wlc_bmac_antsel_set(wlc->hw, wlc->asi->antsel_avail);
2061 if (perr)
2062 *perr = 0;
2064 return (void *)wlc;
2066 fail:
2067 WL_ERROR(("wl%d: %s: failed with err %d\n", unit, __func__, err));
2068 if (wlc)
2069 wlc_detach(wlc);
2071 if (perr)
2072 *perr = err;
2073 return NULL;
2076 static void BCMNMIATTACHFN(wlc_attach_antgain_init) (wlc_info_t *wlc)
2078 uint unit;
2079 unit = wlc->pub->unit;
2081 if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
2082 /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
2083 wlc->band->antgain = 8;
2084 } else if (wlc->band->antgain == -1) {
2085 WL_ERROR(("wl%d: %s: Invalid antennas available in srom, using 2dB\n", unit, __func__));
2086 wlc->band->antgain = 8;
2087 } else {
2088 s8 gain, fract;
2089 /* Older sroms specified gain in whole dbm only. In order
2090 * be able to specify qdbm granularity and remain backward compatible
2091 * the whole dbms are now encoded in only low 6 bits and remaining qdbms
2092 * are encoded in the hi 2 bits. 6 bit signed number ranges from
2093 * -32 - 31. Examples: 0x1 = 1 db,
2094 * 0xc1 = 1.75 db (1 + 3 quarters),
2095 * 0x3f = -1 (-1 + 0 quarters),
2096 * 0x7f = -.75 (-1 in low 6 bits + 1 quarters in hi 2 bits) = -3 qdbm.
2097 * 0xbf = -.50 (-1 in low 6 bits + 2 quarters in hi 2 bits) = -2 qdbm.
2099 gain = wlc->band->antgain & 0x3f;
2100 gain <<= 2; /* Sign extend */
2101 gain >>= 2;
2102 fract = (wlc->band->antgain & 0xc0) >> 6;
2103 wlc->band->antgain = 4 * gain + fract;
2107 static bool wlc_attach_stf_ant_init(wlc_info_t *wlc)
2109 int aa;
2110 uint unit;
2111 char *vars;
2112 int bandtype;
2114 unit = wlc->pub->unit;
2115 vars = wlc->pub->vars;
2116 bandtype = wlc->band->bandtype;
2118 /* get antennas available */
2119 aa = (s8) getintvar(vars, (BAND_5G(bandtype) ? "aa5g" : "aa2g"));
2120 if (aa == 0)
2121 aa = (s8) getintvar(vars,
2122 (BAND_5G(bandtype) ? "aa1" : "aa0"));
2123 if ((aa < 1) || (aa > 15)) {
2124 WL_ERROR(("wl%d: %s: Invalid antennas available in srom (0x%x), using 3.\n", unit, __func__, aa));
2125 aa = 3;
2128 /* reset the defaults if we have a single antenna */
2129 if (aa == 1) {
2130 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
2131 wlc->stf->txant = ANT_TX_FORCE_0;
2132 } else if (aa == 2) {
2133 wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
2134 wlc->stf->txant = ANT_TX_FORCE_1;
2135 } else {
2138 /* Compute Antenna Gain */
2139 wlc->band->antgain =
2140 (s8) getintvar(vars, (BAND_5G(bandtype) ? "ag1" : "ag0"));
2141 wlc_attach_antgain_init(wlc);
2143 return TRUE;
2146 #ifdef WLC_HIGH_ONLY
2147 /* HIGH_ONLY bmac_attach, which sync over LOW_ONLY bmac_attach states */
2148 int wlc_bmac_attach(wlc_info_t *wlc, u16 vendor, u16 device, uint unit,
2149 bool piomode, osl_t *osh, void *regsva, uint bustype,
2150 void *btparam)
2152 wlc_bmac_revinfo_t revinfo;
2153 uint idx = 0;
2154 rpc_info_t *rpc = (rpc_info_t *) btparam;
2156 ASSERT(bustype == RPC_BUS);
2158 /* install the rpc handle in the various state structures used by stub RPC functions */
2159 wlc->rpc = rpc;
2160 wlc->hw->rpc = rpc;
2161 wlc->hw->osh = osh;
2163 wlc->regs = 0;
2165 wlc->rpctx = wlc_rpctx_attach(wlc->pub, wlc);
2166 if (wlc->rpctx == NULL)
2167 return -1;
2170 * FIFO 0
2171 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
2173 /* Always initialized */
2174 ASSERT(NRPCTXBUFPOST <= NTXD);
2175 wlc_rpctx_fifoinit(wlc->rpctx, TX_DATA_FIFO, NRPCTXBUFPOST);
2176 wlc_rpctx_fifoinit(wlc->rpctx, TX_CTL_FIFO, NRPCTXBUFPOST);
2177 wlc_rpctx_fifoinit(wlc->rpctx, TX_BCMC_FIFO, NRPCTXBUFPOST);
2179 /* VI and BK inited only if WME */
2180 if (WME_ENAB(wlc->pub)) {
2181 wlc_rpctx_fifoinit(wlc->rpctx, TX_AC_BK_FIFO, NRPCTXBUFPOST);
2182 wlc_rpctx_fifoinit(wlc->rpctx, TX_AC_VI_FIFO, NRPCTXBUFPOST);
2185 /* Allocate SB handle */
2186 wlc->pub->sih = osl_malloc(wlc->osh, sizeof(si_t));
2187 if (!wlc->pub->sih)
2188 return -1;
2189 bzero(wlc->pub->sih, sizeof(si_t));
2191 /* sync up revinfo with BMAC */
2192 bzero(&revinfo, sizeof(wlc_bmac_revinfo_t));
2193 if (wlc_bmac_revinfo_get(wlc->hw, &revinfo) != 0)
2194 return -1;
2195 wlc->vendorid = (u16) revinfo.vendorid;
2196 wlc->deviceid = (u16) revinfo.deviceid;
2198 wlc->pub->boardrev = (u16) revinfo.boardrev;
2199 wlc->pub->corerev = revinfo.corerev;
2200 wlc->pub->sromrev = (u8) revinfo.sromrev;
2201 wlc->pub->sih->chiprev = revinfo.chiprev;
2202 wlc->pub->sih->chip = revinfo.chip;
2203 wlc->pub->sih->chippkg = revinfo.chippkg;
2204 wlc->pub->sih->boardtype = revinfo.boardtype;
2205 wlc->pub->sih->boardvendor = revinfo.boardvendor;
2206 wlc->pub->sih->bustype = revinfo.bustype;
2207 wlc->pub->sih->buscoretype = revinfo.buscoretype;
2208 wlc->pub->sih->buscorerev = revinfo.buscorerev;
2209 wlc->pub->sih->issim = (bool) revinfo.issim;
2210 wlc->pub->sih->rpc = rpc;
2212 if (revinfo.nbands == 0 || revinfo.nbands > 2)
2213 return -1;
2214 wlc->pub->_nbands = revinfo.nbands;
2216 for (idx = 0; idx < wlc->pub->_nbands; idx++) {
2217 uint bandunit, bandtype; /* To access bandstate */
2218 wlc_phy_t *pi = osl_malloc(wlc->osh, sizeof(wlc_phy_t));
2220 if (!pi)
2221 return -1;
2222 bzero(pi, sizeof(wlc_phy_t));
2223 pi->rpc = rpc;
2225 bandunit = revinfo.band[idx].bandunit;
2226 bandtype = revinfo.band[idx].bandtype;
2227 wlc->bandstate[bandunit]->radiorev =
2228 (u8) revinfo.band[idx].radiorev;
2229 wlc->bandstate[bandunit]->phytype =
2230 (u16) revinfo.band[idx].phytype;
2231 wlc->bandstate[bandunit]->phyrev =
2232 (u16) revinfo.band[idx].phyrev;
2233 wlc->bandstate[bandunit]->radioid =
2234 (u16) revinfo.band[idx].radioid;
2235 wlc->bandstate[bandunit]->abgphy_encore =
2236 revinfo.band[idx].abgphy_encore;
2238 wlc->bandstate[bandunit]->pi = pi;
2239 wlc->bandstate[bandunit]->bandunit = bandunit;
2240 wlc->bandstate[bandunit]->bandtype = bandtype;
2243 /* misc stuff */
2245 return 0;
2248 /* Free the convenience handles */
2249 int wlc_bmac_detach(wlc_info_t *wlc)
2251 uint idx;
2253 if (wlc->pub->sih) {
2254 osl_mfree(wlc->osh, (void *)wlc->pub->sih, sizeof(si_t));
2255 wlc->pub->sih = NULL;
2258 for (idx = 0; idx < MAXBANDS; idx++)
2259 if (wlc->bandstate[idx]->pi) {
2260 osl_mfree(wlc->osh, wlc->bandstate[idx]->pi,
2261 sizeof(wlc_phy_t));
2262 wlc->bandstate[idx]->pi = NULL;
2265 if (wlc->rpctx) {
2266 wlc_rpctx_detach(wlc->rpctx);
2267 wlc->rpctx = NULL;
2270 return 0;
2274 #endif /* WLC_HIGH_ONLY */
2276 static void wlc_timers_deinit(wlc_info_t *wlc)
2278 /* free timer state */
2279 if (wlc->wdtimer) {
2280 wl_free_timer(wlc->wl, wlc->wdtimer);
2281 wlc->wdtimer = NULL;
2283 if (wlc->radio_timer) {
2284 wl_free_timer(wlc->wl, wlc->radio_timer);
2285 wlc->radio_timer = NULL;
2289 static void wlc_detach_module(wlc_info_t *wlc)
2291 if (wlc->asi) {
2292 wlc_antsel_detach(wlc->asi);
2293 wlc->asi = NULL;
2296 if (wlc->ampdu) {
2297 wlc_ampdu_detach(wlc->ampdu);
2298 wlc->ampdu = NULL;
2301 wlc_stf_detach(wlc);
2305 * Return a count of the number of driver callbacks still pending.
2307 * General policy is that wlc_detach can only dealloc/free software states. It can NOT
2308 * touch hardware registers since the d11core may be in reset and clock may not be available.
2309 * One exception is sb register access, which is possible if crystal is turned on
2310 * After "down" state, driver should avoid software timer with the exception of radio_monitor.
2312 uint wlc_detach(wlc_info_t *wlc)
2314 uint i;
2315 uint callbacks = 0;
2317 if (wlc == NULL)
2318 return 0;
2320 WL_TRACE(("wl%d: %s\n", wlc->pub->unit, __func__));
2322 ASSERT(!wlc->pub->up);
2324 callbacks += wlc_bmac_detach(wlc);
2326 /* delete software timers */
2327 if (!wlc_radio_monitor_stop(wlc))
2328 callbacks++;
2330 if (wlc->eventq) {
2331 wlc_eventq_detach(wlc->eventq);
2332 wlc->eventq = NULL;
2335 wlc_channel_mgr_detach(wlc->cmi);
2337 wlc_timers_deinit(wlc);
2339 wlc_detach_module(wlc);
2341 /* free other state */
2343 #ifdef WLC_HIGH_ONLY
2344 /* High-Only driver has an allocated copy of vars, monolithic just
2345 * references the wlc->hw->vars which is freed in wlc_bmac_detach()
2347 if (wlc->pub->vars) {
2348 osl_mfree(wlc->osh, wlc->pub->vars, wlc->vars_size);
2349 wlc->pub->vars = NULL;
2351 #endif
2353 #ifdef BCMDBG
2354 if (wlc->country_ie_override) {
2355 osl_mfree(wlc->osh, wlc->country_ie_override,
2356 wlc->country_ie_override->len + TLV_HDR_LEN);
2357 wlc->country_ie_override = NULL;
2359 #endif /* BCMDBG */
2362 /* free dumpcb list */
2363 dumpcb_t *prev, *ptr;
2364 prev = ptr = wlc->dumpcb_head;
2365 while (ptr) {
2366 ptr = prev->next;
2367 osl_mfree(wlc->osh, prev, sizeof(dumpcb_t));
2368 prev = ptr;
2370 wlc->dumpcb_head = NULL;
2373 /* Detach from iovar manager */
2374 wlc_module_unregister(wlc->pub, "wlc_iovars", wlc);
2377 if (wlc->ap) {
2378 wlc_ap_detach(wlc->ap);
2379 wlc->ap = NULL;
2383 while (wlc->tx_queues != NULL) {
2384 wlc_txq_free(wlc, wlc->osh, wlc->tx_queues);
2388 * consistency check: wlc_module_register/wlc_module_unregister calls
2389 * should match therefore nothing should be left here.
2391 for (i = 0; i < WLC_MAXMODULES; i++)
2392 ASSERT(wlc->modulecb[i].name[0] == '\0');
2394 wlc_detach_mfree(wlc, wlc->osh);
2395 return callbacks;
2398 /* update state that depends on the current value of "ap" */
2399 void wlc_ap_upd(wlc_info_t *wlc)
2401 if (AP_ENAB(wlc->pub))
2402 wlc->PLCPHdr_override = WLC_PLCP_AUTO; /* AP: short not allowed, but not enforced */
2403 else
2404 wlc->PLCPHdr_override = WLC_PLCP_SHORT; /* STA-BSS; short capable */
2406 /* disable vlan_mode on AP since some legacy STAs cannot rx tagged pkts */
2407 wlc->vlan_mode = AP_ENAB(wlc->pub) ? OFF : AUTO;
2409 /* fixup mpc */
2410 wlc->mpc = TRUE;
2413 /* read hwdisable state and propagate to wlc flag */
2414 static void wlc_radio_hwdisable_upd(wlc_info_t *wlc)
2416 if (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO || wlc->pub->hw_off)
2417 return;
2419 if (wlc_bmac_radio_read_hwdisabled(wlc->hw)) {
2420 mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2421 } else {
2422 mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
2426 /* return TRUE if Minimum Power Consumption should be entered, FALSE otherwise */
2427 bool wlc_is_non_delay_mpc(wlc_info_t *wlc)
2429 return FALSE;
2432 bool wlc_ismpc(wlc_info_t *wlc)
2434 return (wlc->mpc_delay_off == 0) && (wlc_is_non_delay_mpc(wlc));
2437 void wlc_radio_mpc_upd(wlc_info_t *wlc)
2439 bool mpc_radio, radio_state;
2442 * Clear the WL_RADIO_MPC_DISABLE bit when mpc feature is disabled
2443 * in case the WL_RADIO_MPC_DISABLE bit was set. Stop the radio
2444 * monitor also when WL_RADIO_MPC_DISABLE is the only reason that
2445 * the radio is going down.
2447 if (!wlc->mpc) {
2448 if (!wlc->pub->radio_disabled)
2449 return;
2450 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2451 wlc_radio_upd(wlc);
2452 if (!wlc->pub->radio_disabled)
2453 wlc_radio_monitor_stop(wlc);
2454 return;
2458 * sync ismpc logic with WL_RADIO_MPC_DISABLE bit in wlc->pub->radio_disabled
2459 * to go ON, always call radio_upd synchronously
2460 * to go OFF, postpone radio_upd to later when context is safe(e.g. watchdog)
2462 radio_state =
2463 (mboolisset(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE) ? OFF :
2464 ON);
2465 mpc_radio = (wlc_ismpc(wlc) == TRUE) ? OFF : ON;
2467 if (radio_state == ON && mpc_radio == OFF)
2468 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2469 else if (radio_state == OFF && mpc_radio == ON) {
2470 mboolclr(wlc->pub->radio_disabled, WL_RADIO_MPC_DISABLE);
2471 wlc_radio_upd(wlc);
2472 if (wlc->mpc_offcnt < WLC_MPC_THRESHOLD) {
2473 wlc->mpc_dlycnt = WLC_MPC_MAX_DELAYCNT;
2474 } else
2475 wlc->mpc_dlycnt = WLC_MPC_MIN_DELAYCNT;
2476 wlc->mpc_dur += OSL_SYSUPTIME() - wlc->mpc_laston_ts;
2478 /* Below logic is meant to capture the transition from mpc off to mpc on for reasons
2479 * other than wlc->mpc_delay_off keeping the mpc off. In that case reset
2480 * wlc->mpc_delay_off to wlc->mpc_dlycnt, so that we restart the countdown of mpc_delay_off
2482 if ((wlc->prev_non_delay_mpc == FALSE) &&
2483 (wlc_is_non_delay_mpc(wlc) == TRUE) && wlc->mpc_delay_off) {
2484 wlc->mpc_delay_off = wlc->mpc_dlycnt;
2486 wlc->prev_non_delay_mpc = wlc_is_non_delay_mpc(wlc);
2490 * centralized radio disable/enable function,
2491 * invoke radio enable/disable after updating hwradio status
2493 static void wlc_radio_upd(wlc_info_t *wlc)
2495 if (wlc->pub->radio_disabled)
2496 wlc_radio_disable(wlc);
2497 else
2498 wlc_radio_enable(wlc);
2501 /* maintain LED behavior in down state */
2502 static void wlc_down_led_upd(wlc_info_t *wlc)
2504 ASSERT(!wlc->pub->up);
2506 /* maintain LEDs while in down state, turn on sbclk if not available yet */
2507 /* turn on sbclk if necessary */
2508 if (!AP_ENAB(wlc->pub)) {
2509 wlc_pllreq(wlc, TRUE, WLC_PLLREQ_FLIP);
2511 wlc_pllreq(wlc, FALSE, WLC_PLLREQ_FLIP);
2515 void wlc_radio_disable(wlc_info_t *wlc)
2517 if (!wlc->pub->up) {
2518 wlc_down_led_upd(wlc);
2519 return;
2522 wlc_radio_monitor_start(wlc);
2523 wl_down(wlc->wl);
2526 static void wlc_radio_enable(wlc_info_t *wlc)
2528 if (wlc->pub->up)
2529 return;
2531 if (DEVICEREMOVED(wlc))
2532 return;
2534 if (!wlc->down_override) { /* imposed by wl down/out ioctl */
2535 wl_up(wlc->wl);
2539 /* periodical query hw radio button while driver is "down" */
2540 static void wlc_radio_timer(void *arg)
2542 wlc_info_t *wlc = (wlc_info_t *) arg;
2544 if (DEVICEREMOVED(wlc)) {
2545 WL_ERROR(("wl%d: %s: dead chip\n", wlc->pub->unit, __func__));
2546 wl_down(wlc->wl);
2547 return;
2550 /* cap mpc off count */
2551 if (wlc->mpc_offcnt < WLC_MPC_MAX_DELAYCNT)
2552 wlc->mpc_offcnt++;
2554 /* validate all the reasons driver could be down and running this radio_timer */
2555 ASSERT(wlc->pub->radio_disabled || wlc->down_override);
2556 wlc_radio_hwdisable_upd(wlc);
2557 wlc_radio_upd(wlc);
2560 static bool wlc_radio_monitor_start(wlc_info_t *wlc)
2562 /* Don't start the timer if HWRADIO feature is disabled */
2563 if (wlc->radio_monitor || (wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO))
2564 return TRUE;
2566 wlc->radio_monitor = TRUE;
2567 wlc_pllreq(wlc, TRUE, WLC_PLLREQ_RADIO_MON);
2568 wl_add_timer(wlc->wl, wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, TRUE);
2569 return TRUE;
2572 bool wlc_radio_monitor_stop(wlc_info_t *wlc)
2574 if (!wlc->radio_monitor)
2575 return TRUE;
2577 ASSERT((wlc->pub->wlfeatureflag & WL_SWFL_NOHWRADIO) !=
2578 WL_SWFL_NOHWRADIO);
2580 wlc->radio_monitor = FALSE;
2581 wlc_pllreq(wlc, FALSE, WLC_PLLREQ_RADIO_MON);
2582 return wl_del_timer(wlc->wl, wlc->radio_timer);
2585 /* bring the driver down, but don't reset hardware */
2586 void wlc_out(wlc_info_t *wlc)
2588 wlc_bmac_set_noreset(wlc->hw, TRUE);
2589 wlc_radio_upd(wlc);
2590 wl_down(wlc->wl);
2591 wlc_bmac_set_noreset(wlc->hw, FALSE);
2593 /* core clk is TRUE in BMAC driver due to noreset, need to mirror it in HIGH */
2594 wlc->clk = TRUE;
2596 /* This will make sure that when 'up' is done
2597 * after 'out' it'll restore hardware (especially gpios)
2599 wlc->pub->hw_up = FALSE;
2602 #if defined(BCMDBG)
2603 /* Verify the sanity of wlc->tx_prec_map. This can be done only by making sure that
2604 * if there is no packet pending for the FIFO, then the corresponding prec bits should be set
2605 * in prec_map. Of course, ignore this rule when block_datafifo is set
2607 static bool wlc_tx_prec_map_verify(wlc_info_t *wlc)
2609 /* For non-WME, both fifos have overlapping prec_map. So it's an error only if both
2610 * fail the check.
2612 if (!EDCF_ENAB(wlc->pub)) {
2613 if (!(WLC_TX_FIFO_CHECK(wlc, TX_DATA_FIFO) ||
2614 WLC_TX_FIFO_CHECK(wlc, TX_CTL_FIFO)))
2615 return FALSE;
2616 else
2617 return TRUE;
2620 return WLC_TX_FIFO_CHECK(wlc, TX_AC_BK_FIFO)
2621 && WLC_TX_FIFO_CHECK(wlc, TX_AC_BE_FIFO)
2622 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VI_FIFO)
2623 && WLC_TX_FIFO_CHECK(wlc, TX_AC_VO_FIFO);
2625 #endif /* BCMDBG */
2627 static void wlc_watchdog_by_timer(void *arg)
2629 wlc_info_t *wlc = (wlc_info_t *) arg;
2630 wlc_watchdog(arg);
2631 if (WLC_WATCHDOG_TBTT(wlc)) {
2632 /* set to normal osl watchdog period */
2633 wl_del_timer(wlc->wl, wlc->wdtimer);
2634 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG,
2635 TRUE);
2639 /* common watchdog code */
2640 static void wlc_watchdog(void *arg)
2642 wlc_info_t *wlc = (wlc_info_t *) arg;
2643 int i;
2644 wlc_bsscfg_t *cfg;
2646 WL_TRACE(("wl%d: wlc_watchdog\n", wlc->pub->unit));
2648 if (!wlc->pub->up)
2649 return;
2651 if (DEVICEREMOVED(wlc)) {
2652 WL_ERROR(("wl%d: %s: dead chip\n", wlc->pub->unit, __func__));
2653 wl_down(wlc->wl);
2654 return;
2657 /* increment second count */
2658 wlc->pub->now++;
2660 /* delay radio disable */
2661 if (wlc->mpc_delay_off) {
2662 if (--wlc->mpc_delay_off == 0) {
2663 mboolset(wlc->pub->radio_disabled,
2664 WL_RADIO_MPC_DISABLE);
2665 if (wlc->mpc && wlc_ismpc(wlc))
2666 wlc->mpc_offcnt = 0;
2667 wlc->mpc_laston_ts = OSL_SYSUPTIME();
2671 /* mpc sync */
2672 wlc_radio_mpc_upd(wlc);
2673 /* radio sync: sw/hw/mpc --> radio_disable/radio_enable */
2674 wlc_radio_hwdisable_upd(wlc);
2675 wlc_radio_upd(wlc);
2676 /* if ismpc, driver should be in down state if up/down is allowed */
2677 if (wlc->mpc && wlc_ismpc(wlc))
2678 ASSERT(!wlc->pub->up);
2679 /* if radio is disable, driver may be down, quit here */
2680 if (wlc->pub->radio_disabled)
2681 return;
2683 #ifdef WLC_LOW
2684 wlc_bmac_watchdog(wlc);
2685 #endif
2686 #ifdef WLC_HIGH_ONLY
2687 /* maintenance */
2688 wlc_bmac_rpc_watchdog(wlc);
2689 #endif
2691 /* occasionally sample mac stat counters to detect 16-bit counter wrap */
2692 if ((WLC_UPDATE_STATS(wlc))
2693 && (!(wlc->pub->now % SW_TIMER_MAC_STAT_UPD)))
2694 wlc_statsupd(wlc);
2696 /* Manage TKIP countermeasures timers */
2697 FOREACH_BSS(wlc, i, cfg) {
2698 if (cfg->tk_cm_dt) {
2699 cfg->tk_cm_dt--;
2701 if (cfg->tk_cm_bt) {
2702 cfg->tk_cm_bt--;
2706 /* Call any registered watchdog handlers */
2707 for (i = 0; i < WLC_MAXMODULES; i++) {
2708 if (wlc->modulecb[i].watchdog_fn)
2709 wlc->modulecb[i].watchdog_fn(wlc->modulecb[i].hdl);
2712 if (WLCISNPHY(wlc->band) && !wlc->pub->tempsense_disable &&
2713 ((wlc->pub->now - wlc->tempsense_lasttime) >=
2714 WLC_TEMPSENSE_PERIOD)) {
2715 wlc->tempsense_lasttime = wlc->pub->now;
2716 wlc_tempsense_upd(wlc);
2718 #ifdef WLC_LOW
2719 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
2720 ASSERT(wlc_bmac_taclear(wlc->hw, TRUE));
2721 #endif
2723 /* Verify that tx_prec_map and fifos are in sync to avoid lock ups */
2724 ASSERT(wlc_tx_prec_map_verify(wlc));
2726 ASSERT(wlc_ps_check(wlc));
2729 /* make interface operational */
2730 int BCMINITFN(wlc_up) (wlc_info_t *wlc)
2732 WL_TRACE(("wl%d: %s:\n", wlc->pub->unit, __func__));
2734 /* HW is turned off so don't try to access it */
2735 if (wlc->pub->hw_off || DEVICEREMOVED(wlc))
2736 return BCME_RADIOOFF;
2738 if (!wlc->pub->hw_up) {
2739 wlc_bmac_hw_up(wlc->hw);
2740 wlc->pub->hw_up = TRUE;
2743 if ((wlc->pub->boardflags & BFL_FEM)
2744 && (CHIPID(wlc->pub->sih->chip) == BCM4313_CHIP_ID)) {
2745 if (wlc->pub->boardrev >= 0x1250
2746 && (wlc->pub->boardflags & BFL_FEM_BT)) {
2747 wlc_mhf(wlc, MHF5, MHF5_4313_GPIOCTRL,
2748 MHF5_4313_GPIOCTRL, WLC_BAND_ALL);
2749 } else {
2750 wlc_mhf(wlc, MHF4, MHF4_EXTPA_ENABLE, MHF4_EXTPA_ENABLE,
2751 WLC_BAND_ALL);
2756 * Need to read the hwradio status here to cover the case where the system
2757 * is loaded with the hw radio disabled. We do not want to bring the driver up in this case.
2758 * if radio is disabled, abort up, lower power, start radio timer and return 0(for NDIS)
2759 * don't call radio_update to avoid looping wlc_up.
2761 * wlc_bmac_up_prep() returns either 0 or BCME_RADIOOFF only
2763 if (!wlc->pub->radio_disabled) {
2764 int status = wlc_bmac_up_prep(wlc->hw);
2765 if (status == BCME_RADIOOFF) {
2766 if (!mboolisset
2767 (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
2768 int idx;
2769 wlc_bsscfg_t *bsscfg;
2770 mboolset(wlc->pub->radio_disabled,
2771 WL_RADIO_HW_DISABLE);
2773 FOREACH_BSS(wlc, idx, bsscfg) {
2774 if (!BSSCFG_STA(bsscfg)
2775 || !bsscfg->enable || !bsscfg->BSS)
2776 continue;
2777 WL_ERROR(("wl%d.%d: wlc_up: rfdisable -> " "wlc_bsscfg_disable()\n", wlc->pub->unit, idx));
2780 } else
2781 ASSERT(!status);
2784 if (wlc->pub->radio_disabled) {
2785 wlc_radio_monitor_start(wlc);
2786 return 0;
2789 /* wlc_bmac_up_prep has done wlc_corereset(). so clk is on, set it */
2790 wlc->clk = TRUE;
2792 wlc_radio_monitor_stop(wlc);
2794 /* Set EDCF hostflags */
2795 if (EDCF_ENAB(wlc->pub)) {
2796 wlc_mhf(wlc, MHF1, MHF1_EDCF, MHF1_EDCF, WLC_BAND_ALL);
2797 } else {
2798 wlc_mhf(wlc, MHF1, MHF1_EDCF, 0, WLC_BAND_ALL);
2801 if (WLC_WAR16165(wlc))
2802 wlc_mhf(wlc, MHF2, MHF2_PCISLOWCLKWAR, MHF2_PCISLOWCLKWAR,
2803 WLC_BAND_ALL);
2805 wl_init(wlc->wl);
2806 wlc->pub->up = TRUE;
2808 if (wlc->bandinit_pending) {
2809 wlc_suspend_mac_and_wait(wlc);
2810 wlc_set_chanspec(wlc, wlc->default_bss->chanspec);
2811 wlc->bandinit_pending = FALSE;
2812 wlc_enable_mac(wlc);
2815 wlc_bmac_up_finish(wlc->hw);
2817 /* other software states up after ISR is running */
2818 /* start APs that were to be brought up but are not up yet */
2819 /* if (AP_ENAB(wlc->pub)) wlc_restart_ap(wlc->ap); */
2821 /* Program the TX wme params with the current settings */
2822 wlc_wme_retries_write(wlc);
2824 /* start one second watchdog timer */
2825 ASSERT(!wlc->WDarmed);
2826 wl_add_timer(wlc->wl, wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, TRUE);
2827 wlc->WDarmed = TRUE;
2829 /* ensure antenna config is up to date */
2830 wlc_stf_phy_txant_upd(wlc);
2831 /* ensure LDPC config is in sync */
2832 wlc_ht_update_ldpc(wlc, wlc->stf->ldpc);
2834 return 0;
2837 /* Initialize the base precedence map for dequeueing from txq based on WME settings */
2838 static void BCMINITFN(wlc_tx_prec_map_init) (wlc_info_t *wlc)
2840 wlc->tx_prec_map = WLC_PREC_BMP_ALL;
2841 bzero(wlc->fifo2prec_map, sizeof(u16) * NFIFO);
2843 /* For non-WME, both fifos have overlapping MAXPRIO. So just disable all precedences
2844 * if either is full.
2846 if (!EDCF_ENAB(wlc->pub)) {
2847 wlc->fifo2prec_map[TX_DATA_FIFO] = WLC_PREC_BMP_ALL;
2848 wlc->fifo2prec_map[TX_CTL_FIFO] = WLC_PREC_BMP_ALL;
2849 } else {
2850 wlc->fifo2prec_map[TX_AC_BK_FIFO] = WLC_PREC_BMP_AC_BK;
2851 wlc->fifo2prec_map[TX_AC_BE_FIFO] = WLC_PREC_BMP_AC_BE;
2852 wlc->fifo2prec_map[TX_AC_VI_FIFO] = WLC_PREC_BMP_AC_VI;
2853 wlc->fifo2prec_map[TX_AC_VO_FIFO] = WLC_PREC_BMP_AC_VO;
2857 static uint BCMUNINITFN(wlc_down_del_timer) (wlc_info_t *wlc)
2859 uint callbacks = 0;
2861 return callbacks;
2865 * Mark the interface nonoperational, stop the software mechanisms,
2866 * disable the hardware, free any transient buffer state.
2867 * Return a count of the number of driver callbacks still pending.
2869 uint BCMUNINITFN(wlc_down) (wlc_info_t *wlc)
2872 uint callbacks = 0;
2873 int i;
2874 bool dev_gone = FALSE;
2875 wlc_txq_info_t *qi;
2877 WL_TRACE(("wl%d: %s:\n", wlc->pub->unit, __func__));
2879 /* check if we are already in the going down path */
2880 if (wlc->going_down) {
2881 WL_ERROR(("wl%d: %s: Driver going down so return\n",
2882 wlc->pub->unit, __func__));
2883 return 0;
2885 if (!wlc->pub->up)
2886 return callbacks;
2888 /* in between, mpc could try to bring down again.. */
2889 wlc->going_down = TRUE;
2891 callbacks += wlc_bmac_down_prep(wlc->hw);
2893 dev_gone = DEVICEREMOVED(wlc);
2895 /* Call any registered down handlers */
2896 for (i = 0; i < WLC_MAXMODULES; i++) {
2897 if (wlc->modulecb[i].down_fn)
2898 callbacks +=
2899 wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
2902 /* cancel the watchdog timer */
2903 if (wlc->WDarmed) {
2904 if (!wl_del_timer(wlc->wl, wlc->wdtimer))
2905 callbacks++;
2906 wlc->WDarmed = FALSE;
2908 /* cancel all other timers */
2909 callbacks += wlc_down_del_timer(wlc);
2911 /* interrupt must have been blocked */
2912 ASSERT((wlc->macintmask == 0) || !wlc->pub->up);
2914 wlc->pub->up = FALSE;
2916 wlc_phy_mute_upd(wlc->band->pi, FALSE, PHY_MUTE_ALL);
2918 /* clear txq flow control */
2919 wlc_txflowcontrol_reset(wlc);
2921 /* flush tx queues */
2922 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
2923 pktq_flush(wlc->osh, &qi->q, TRUE, NULL, 0);
2924 ASSERT(pktq_empty(&qi->q));
2927 /* flush event queue.
2928 * Should be the last thing done after all the events are generated
2929 * Just delivers the events synchronously instead of waiting for a timer
2931 callbacks += wlc_eventq_down(wlc->eventq);
2933 callbacks += wlc_bmac_down_finish(wlc->hw);
2935 /* wlc_bmac_down_finish has done wlc_coredisable(). so clk is off */
2936 wlc->clk = FALSE;
2938 #ifdef WLC_HIGH_ONLY
2939 wlc_rpctx_txreclaim(wlc->rpctx);
2940 #endif
2942 /* Verify all packets are flushed from the driver */
2943 if (PKTALLOCED(wlc->osh) != 0) {
2944 WL_ERROR(("%d packets not freed at wlc_down!!!!!!\n",
2945 PKTALLOCED(wlc->osh)));
2947 #ifdef BCMDBG
2948 /* Since all the packets should have been freed,
2949 * all callbacks should have been called
2951 for (i = 1; i <= wlc->pub->tunables->maxpktcb; i++)
2952 ASSERT(wlc->pkt_callback[i].fn == NULL);
2953 #endif
2954 wlc->going_down = FALSE;
2955 return callbacks;
2958 /* Set the current gmode configuration */
2959 int wlc_set_gmode(wlc_info_t *wlc, u8 gmode, bool config)
2961 int ret = 0;
2962 uint i;
2963 wlc_rateset_t rs;
2964 /* Default to 54g Auto */
2965 s8 shortslot = WLC_SHORTSLOT_AUTO; /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
2966 bool shortslot_restrict = FALSE; /* Restrict association to stations that support shortslot
2968 bool ignore_bcns = TRUE; /* Ignore legacy beacons on the same channel */
2969 bool ofdm_basic = FALSE; /* Make 6, 12, and 24 basic rates */
2970 int preamble = WLC_PLCP_LONG; /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
2971 bool preamble_restrict = FALSE; /* Restrict association to stations that support short
2972 * preambles
2974 wlcband_t *band;
2976 /* if N-support is enabled, allow Gmode set as long as requested
2977 * Gmode is not GMODE_LEGACY_B
2979 if (N_ENAB(wlc->pub) && gmode == GMODE_LEGACY_B)
2980 return BCME_UNSUPPORTED;
2982 /* verify that we are dealing with 2G band and grab the band pointer */
2983 if (wlc->band->bandtype == WLC_BAND_2G)
2984 band = wlc->band;
2985 else if ((NBANDS(wlc) > 1) &&
2986 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == WLC_BAND_2G))
2987 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
2988 else
2989 return BCME_BADBAND;
2991 /* Legacy or bust when no OFDM is supported by regulatory */
2992 if ((wlc_channel_locale_flags_in_band(wlc->cmi, band->bandunit) &
2993 WLC_NO_OFDM) && (gmode != GMODE_LEGACY_B))
2994 return BCME_RANGE;
2996 /* update configuration value */
2997 if (config == TRUE)
2998 wlc_protection_upd(wlc, WLC_PROT_G_USER, gmode);
3000 /* Clear supported rates filter */
3001 bzero(&wlc->sup_rates_override, sizeof(wlc_rateset_t));
3003 /* Clear rateset override */
3004 bzero(&rs, sizeof(wlc_rateset_t));
3006 switch (gmode) {
3007 case GMODE_LEGACY_B:
3008 shortslot = WLC_SHORTSLOT_OFF;
3009 wlc_rateset_copy(&gphy_legacy_rates, &rs);
3011 break;
3013 case GMODE_LRS:
3014 if (AP_ENAB(wlc->pub))
3015 wlc_rateset_copy(&cck_rates, &wlc->sup_rates_override);
3016 break;
3018 case GMODE_AUTO:
3019 /* Accept defaults */
3020 break;
3022 case GMODE_ONLY:
3023 ofdm_basic = TRUE;
3024 preamble = WLC_PLCP_SHORT;
3025 preamble_restrict = TRUE;
3026 break;
3028 case GMODE_PERFORMANCE:
3029 if (AP_ENAB(wlc->pub)) /* Put all rates into the Supported Rates element */
3030 wlc_rateset_copy(&cck_ofdm_rates,
3031 &wlc->sup_rates_override);
3033 shortslot = WLC_SHORTSLOT_ON;
3034 shortslot_restrict = TRUE;
3035 ofdm_basic = TRUE;
3036 preamble = WLC_PLCP_SHORT;
3037 preamble_restrict = TRUE;
3038 break;
3040 default:
3041 /* Error */
3042 WL_ERROR(("wl%d: %s: invalid gmode %d\n", wlc->pub->unit,
3043 __func__, gmode));
3044 return BCME_UNSUPPORTED;
3048 * If we are switching to gmode == GMODE_LEGACY_B,
3049 * clean up rate info that may refer to OFDM rates.
3051 if ((gmode == GMODE_LEGACY_B) && (band->gmode != GMODE_LEGACY_B)) {
3052 band->gmode = gmode;
3053 if (band->rspec_override && !IS_CCK(band->rspec_override)) {
3054 band->rspec_override = 0;
3055 wlc_reprate_init(wlc);
3057 if (band->mrspec_override && !IS_CCK(band->mrspec_override)) {
3058 band->mrspec_override = 0;
3062 band->gmode = gmode;
3064 wlc->ignore_bcns = ignore_bcns;
3066 wlc->shortslot_override = shortslot;
3068 if (AP_ENAB(wlc->pub)) {
3069 /* wlc->ap->shortslot_restrict = shortslot_restrict; */
3070 wlc->PLCPHdr_override =
3071 (preamble !=
3072 WLC_PLCP_LONG) ? WLC_PLCP_SHORT : WLC_PLCP_AUTO;
3075 if ((AP_ENAB(wlc->pub) && preamble != WLC_PLCP_LONG)
3076 || preamble == WLC_PLCP_SHORT)
3077 wlc->default_bss->capability |= DOT11_CAP_SHORT;
3078 else
3079 wlc->default_bss->capability &= ~DOT11_CAP_SHORT;
3081 /* Update shortslot capability bit for AP and IBSS */
3082 if ((AP_ENAB(wlc->pub) && shortslot == WLC_SHORTSLOT_AUTO) ||
3083 shortslot == WLC_SHORTSLOT_ON)
3084 wlc->default_bss->capability |= DOT11_CAP_SHORTSLOT;
3085 else
3086 wlc->default_bss->capability &= ~DOT11_CAP_SHORTSLOT;
3088 /* Use the default 11g rateset */
3089 if (!rs.count)
3090 wlc_rateset_copy(&cck_ofdm_rates, &rs);
3092 if (ofdm_basic) {
3093 for (i = 0; i < rs.count; i++) {
3094 if (rs.rates[i] == WLC_RATE_6M
3095 || rs.rates[i] == WLC_RATE_12M
3096 || rs.rates[i] == WLC_RATE_24M)
3097 rs.rates[i] |= WLC_RATE_FLAG;
3101 /* Set default bss rateset */
3102 wlc->default_bss->rateset.count = rs.count;
3103 bcopy((char *)rs.rates, (char *)wlc->default_bss->rateset.rates,
3104 sizeof(wlc->default_bss->rateset.rates));
3106 return ret;
3109 static int wlc_nmode_validate(wlc_info_t *wlc, s32 nmode)
3111 int err = 0;
3113 switch (nmode) {
3115 case OFF:
3116 break;
3118 case AUTO:
3119 case WL_11N_2x2:
3120 case WL_11N_3x3:
3121 if (!(WLC_PHY_11N_CAP(wlc->band)))
3122 err = BCME_BADBAND;
3123 break;
3125 default:
3126 err = BCME_RANGE;
3127 break;
3130 return err;
3133 int wlc_set_nmode(wlc_info_t *wlc, s32 nmode)
3135 uint i;
3136 int err;
3138 err = wlc_nmode_validate(wlc, nmode);
3139 ASSERT(err == 0);
3140 if (err)
3141 return err;
3143 switch (nmode) {
3144 case OFF:
3145 wlc->pub->_n_enab = OFF;
3146 wlc->default_bss->flags &= ~WLC_BSS_HT;
3147 /* delete the mcs rates from the default and hw ratesets */
3148 wlc_rateset_mcs_clear(&wlc->default_bss->rateset);
3149 for (i = 0; i < NBANDS(wlc); i++) {
3150 memset(wlc->bandstate[i]->hw_rateset.mcs, 0,
3151 MCSSET_LEN);
3152 if (IS_MCS(wlc->band->rspec_override)) {
3153 wlc->bandstate[i]->rspec_override = 0;
3154 wlc_reprate_init(wlc);
3156 if (IS_MCS(wlc->band->mrspec_override))
3157 wlc->bandstate[i]->mrspec_override = 0;
3159 break;
3161 case AUTO:
3162 if (wlc->stf->txstreams == WL_11N_3x3)
3163 nmode = WL_11N_3x3;
3164 else
3165 nmode = WL_11N_2x2;
3166 case WL_11N_2x2:
3167 case WL_11N_3x3:
3168 ASSERT(WLC_PHY_11N_CAP(wlc->band));
3169 /* force GMODE_AUTO if NMODE is ON */
3170 wlc_set_gmode(wlc, GMODE_AUTO, TRUE);
3171 if (nmode == WL_11N_3x3)
3172 wlc->pub->_n_enab = SUPPORT_HT;
3173 else
3174 wlc->pub->_n_enab = SUPPORT_11N;
3175 wlc->default_bss->flags |= WLC_BSS_HT;
3176 /* add the mcs rates to the default and hw ratesets */
3177 wlc_rateset_mcs_build(&wlc->default_bss->rateset,
3178 wlc->stf->txstreams);
3179 for (i = 0; i < NBANDS(wlc); i++)
3180 memcpy(wlc->bandstate[i]->hw_rateset.mcs,
3181 wlc->default_bss->rateset.mcs, MCSSET_LEN);
3182 break;
3184 default:
3185 ASSERT(0);
3186 break;
3189 return err;
3192 static int wlc_set_rateset(wlc_info_t *wlc, wlc_rateset_t *rs_arg)
3194 wlc_rateset_t rs, new;
3195 uint bandunit;
3197 bcopy((char *)rs_arg, (char *)&rs, sizeof(wlc_rateset_t));
3199 /* check for bad count value */
3200 if ((rs.count == 0) || (rs.count > WLC_NUMRATES))
3201 return BCME_BADRATESET;
3203 /* try the current band */
3204 bandunit = wlc->band->bandunit;
3205 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3206 if (wlc_rate_hwrs_filter_sort_validate
3207 (&new, &wlc->bandstate[bandunit]->hw_rateset, TRUE,
3208 wlc->stf->txstreams))
3209 goto good;
3211 /* try the other band */
3212 if (IS_MBAND_UNLOCKED(wlc)) {
3213 bandunit = OTHERBANDUNIT(wlc);
3214 bcopy((char *)&rs, (char *)&new, sizeof(wlc_rateset_t));
3215 if (wlc_rate_hwrs_filter_sort_validate(&new,
3216 &wlc->
3217 bandstate[bandunit]->
3218 hw_rateset, TRUE,
3219 wlc->stf->txstreams))
3220 goto good;
3223 return BCME_ERROR;
3225 good:
3226 /* apply new rateset */
3227 bcopy((char *)&new, (char *)&wlc->default_bss->rateset,
3228 sizeof(wlc_rateset_t));
3229 bcopy((char *)&new, (char *)&wlc->bandstate[bandunit]->defrateset,
3230 sizeof(wlc_rateset_t));
3231 return 0;
3234 /* simplified integer set interface for common ioctl handler */
3235 int wlc_set(wlc_info_t *wlc, int cmd, int arg)
3237 return wlc_ioctl(wlc, cmd, (void *)&arg, sizeof(arg), NULL);
3240 /* simplified integer get interface for common ioctl handler */
3241 int wlc_get(wlc_info_t *wlc, int cmd, int *arg)
3243 return wlc_ioctl(wlc, cmd, arg, sizeof(int), NULL);
3246 static void wlc_ofdm_rateset_war(wlc_info_t *wlc)
3248 u8 r;
3249 bool war = FALSE;
3251 if (wlc->cfg->associated)
3252 r = wlc->cfg->current_bss->rateset.rates[0];
3253 else
3254 r = wlc->default_bss->rateset.rates[0];
3256 wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
3258 return;
3262 wlc_ioctl(wlc_info_t *wlc, int cmd, void *arg, int len, struct wlc_if *wlcif)
3264 return _wlc_ioctl(wlc, cmd, arg, len, wlcif);
3267 /* common ioctl handler. return: 0=ok, -1=error, positive=particular error */
3268 static int
3269 _wlc_ioctl(wlc_info_t *wlc, int cmd, void *arg, int len, struct wlc_if *wlcif)
3271 int val, *pval;
3272 bool bool_val;
3273 int bcmerror;
3274 d11regs_t *regs;
3275 uint i;
3276 struct scb *nextscb;
3277 bool ta_ok;
3278 uint band;
3279 rw_reg_t *r;
3280 wlc_bsscfg_t *bsscfg;
3281 osl_t *osh;
3282 wlc_bss_info_t *current_bss;
3284 /* update bsscfg pointer */
3285 bsscfg = NULL; /* XXX: Hack bsscfg to be size one and use this globally */
3286 current_bss = NULL;
3288 /* initialize the following to get rid of compiler warning */
3289 nextscb = NULL;
3290 ta_ok = FALSE;
3291 band = 0;
3292 r = NULL;
3294 /* If the device is turned off, then it's not "removed" */
3295 if (!wlc->pub->hw_off && DEVICEREMOVED(wlc)) {
3296 WL_ERROR(("wl%d: %s: dead chip\n", wlc->pub->unit, __func__));
3297 wl_down(wlc->wl);
3298 return BCME_ERROR;
3301 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
3303 /* default argument is generic integer */
3304 pval = arg ? (int *)arg:NULL;
3306 /* This will prevent the misaligned access */
3307 if (pval && (u32) len >= sizeof(val))
3308 bcopy(pval, &val, sizeof(val));
3309 else
3310 val = 0;
3312 /* bool conversion to avoid duplication below */
3313 bool_val = val != 0;
3315 if (cmd != WLC_SET_CHANNEL)
3316 WL_NONE(("WLC_IOCTL: cmd %d val 0x%x (%d) len %d\n", cmd,
3317 (uint) val, val, len));
3319 bcmerror = 0;
3320 regs = wlc->regs;
3321 osh = wlc->osh;
3323 /* A few commands don't need any arguments; all the others do. */
3324 switch (cmd) {
3325 case WLC_UP:
3326 case WLC_OUT:
3327 case WLC_DOWN:
3328 case WLC_DISASSOC:
3329 case WLC_RESTART:
3330 case WLC_REBOOT:
3331 case WLC_START_CHANNEL_QA:
3332 case WLC_INIT:
3333 break;
3335 default:
3336 if ((arg == NULL) || (len <= 0)) {
3337 WL_ERROR(("wl%d: %s: Command %d needs arguments\n",
3338 wlc->pub->unit, __func__, cmd));
3339 bcmerror = BCME_BADARG;
3340 goto done;
3344 switch (cmd) {
3346 #if defined(BCMDBG)
3347 case WLC_GET_MSGLEVEL:
3348 *pval = wl_msg_level;
3349 break;
3351 case WLC_SET_MSGLEVEL:
3352 wl_msg_level = val;
3353 break;
3354 #endif
3356 case WLC_GET_INSTANCE:
3357 *pval = wlc->pub->unit;
3358 break;
3360 case WLC_GET_CHANNEL:{
3361 channel_info_t *ci = (channel_info_t *) arg;
3363 ASSERT(len > (int)sizeof(ci));
3365 ci->hw_channel =
3366 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC);
3367 ci->target_channel =
3368 CHSPEC_CHANNEL(wlc->default_bss->chanspec);
3369 ci->scan_channel = 0;
3371 break;
3374 case WLC_SET_CHANNEL:{
3375 chanspec_t chspec = CH20MHZ_CHSPEC(val);
3377 if (val < 0 || val > MAXCHANNEL) {
3378 bcmerror = BCME_OUTOFRANGECHAN;
3379 break;
3382 if (!wlc_valid_chanspec_db(wlc->cmi, chspec)) {
3383 bcmerror = BCME_BADCHAN;
3384 break;
3387 if (!wlc->pub->up && IS_MBAND_UNLOCKED(wlc)) {
3388 if (wlc->band->bandunit !=
3389 CHSPEC_WLCBANDUNIT(chspec))
3390 wlc->bandinit_pending = TRUE;
3391 else
3392 wlc->bandinit_pending = FALSE;
3395 wlc->default_bss->chanspec = chspec;
3396 /* wlc_BSSinit() will sanitize the rateset before using it.. */
3397 if (wlc->pub->up && !wlc->pub->associated &&
3398 (WLC_BAND_PI_RADIO_CHANSPEC != chspec)) {
3399 wlc_set_home_chanspec(wlc, chspec);
3400 wlc_suspend_mac_and_wait(wlc);
3401 wlc_set_chanspec(wlc, chspec);
3402 wlc_enable_mac(wlc);
3404 #ifdef WLC_HIGH_ONLY
3405 /* delay for channel change */
3406 msleep(50);
3407 #endif
3408 break;
3411 #if defined(BCMDBG)
3412 case WLC_GET_UCFLAGS:
3413 if (!wlc->pub->up) {
3414 bcmerror = BCME_NOTUP;
3415 break;
3418 /* optional band is stored in the second integer of incoming buffer */
3419 band =
3420 (len <
3421 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3423 /* bcmerror checking */
3424 bcmerror = wlc_iocregchk(wlc, band);
3425 if (bcmerror)
3426 break;
3428 if (val >= MHFMAX) {
3429 bcmerror = BCME_RANGE;
3430 break;
3433 *pval = wlc_bmac_mhf_get(wlc->hw, (u8) val, WLC_BAND_AUTO);
3434 break;
3436 case WLC_SET_UCFLAGS:
3437 if (!wlc->pub->up) {
3438 bcmerror = BCME_NOTUP;
3439 break;
3442 /* optional band is stored in the second integer of incoming buffer */
3443 band =
3444 (len <
3445 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3447 /* bcmerror checking */
3448 bcmerror = wlc_iocregchk(wlc, band);
3449 if (bcmerror)
3450 break;
3452 i = (u16) val;
3453 if (i >= MHFMAX) {
3454 bcmerror = BCME_RANGE;
3455 break;
3458 wlc_mhf(wlc, (u8) i, 0xffff, (u16) (val >> NBITS(u16)),
3459 WLC_BAND_AUTO);
3460 break;
3462 case WLC_GET_SHMEM:
3463 ta_ok = TRUE;
3465 /* optional band is stored in the second integer of incoming buffer */
3466 band =
3467 (len <
3468 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3470 /* bcmerror checking */
3471 bcmerror = wlc_iocregchk(wlc, band);
3472 if (bcmerror)
3473 break;
3475 if (val & 1) {
3476 bcmerror = BCME_BADADDR;
3477 break;
3480 *pval = wlc_read_shm(wlc, (u16) val);
3481 break;
3483 case WLC_SET_SHMEM:
3484 ta_ok = TRUE;
3486 /* optional band is stored in the second integer of incoming buffer */
3487 band =
3488 (len <
3489 (int)(2 * sizeof(int))) ? WLC_BAND_AUTO : ((int *)arg)[1];
3491 /* bcmerror checking */
3492 bcmerror = wlc_iocregchk(wlc, band);
3493 if (bcmerror)
3494 break;
3496 if (val & 1) {
3497 bcmerror = BCME_BADADDR;
3498 break;
3501 wlc_write_shm(wlc, (u16) val,
3502 (u16) (val >> NBITS(u16)));
3503 break;
3505 case WLC_R_REG: /* MAC registers */
3506 ta_ok = TRUE;
3507 r = (rw_reg_t *) arg;
3508 band = WLC_BAND_AUTO;
3510 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3511 bcmerror = BCME_BUFTOOSHORT;
3512 break;
3515 if (len >= (int)sizeof(rw_reg_t))
3516 band = r->band;
3518 /* bcmerror checking */
3519 bcmerror = wlc_iocregchk(wlc, band);
3520 if (bcmerror)
3521 break;
3523 if ((r->byteoff + r->size) > sizeof(d11regs_t)) {
3524 bcmerror = BCME_BADADDR;
3525 break;
3527 if (r->size == sizeof(u32))
3528 r->val =
3529 R_REG(osh,
3530 (u32 *) ((unsigned char *) (uintptr) regs +
3531 r->byteoff));
3532 else if (r->size == sizeof(u16))
3533 r->val =
3534 R_REG(osh,
3535 (u16 *) ((unsigned char *) (uintptr) regs +
3536 r->byteoff));
3537 else
3538 bcmerror = BCME_BADADDR;
3539 break;
3541 case WLC_W_REG:
3542 ta_ok = TRUE;
3543 r = (rw_reg_t *) arg;
3544 band = WLC_BAND_AUTO;
3546 if (len < (int)(sizeof(rw_reg_t) - sizeof(uint))) {
3547 bcmerror = BCME_BUFTOOSHORT;
3548 break;
3551 if (len >= (int)sizeof(rw_reg_t))
3552 band = r->band;
3554 /* bcmerror checking */
3555 bcmerror = wlc_iocregchk(wlc, band);
3556 if (bcmerror)
3557 break;
3559 if (r->byteoff + r->size > sizeof(d11regs_t)) {
3560 bcmerror = BCME_BADADDR;
3561 break;
3563 if (r->size == sizeof(u32))
3564 W_REG(osh,
3565 (u32 *) ((unsigned char *) (uintptr) regs +
3566 r->byteoff), r->val);
3567 else if (r->size == sizeof(u16))
3568 W_REG(osh,
3569 (u16 *) ((unsigned char *) (uintptr) regs +
3570 r->byteoff), r->val);
3571 else
3572 bcmerror = BCME_BADADDR;
3573 break;
3574 #endif /* BCMDBG */
3576 case WLC_GET_TXANT:
3577 *pval = wlc->stf->txant;
3578 break;
3580 case WLC_SET_TXANT:
3581 bcmerror = wlc_stf_ant_txant_validate(wlc, (s8) val);
3582 if (bcmerror < 0)
3583 break;
3585 wlc->stf->txant = (s8) val;
3587 /* if down, we are done */
3588 if (!wlc->pub->up)
3589 break;
3591 wlc_suspend_mac_and_wait(wlc);
3593 wlc_stf_phy_txant_upd(wlc);
3594 wlc_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
3596 wlc_enable_mac(wlc);
3598 break;
3600 case WLC_GET_ANTDIV:{
3601 u8 phy_antdiv;
3603 /* return configured value if core is down */
3604 if (!wlc->pub->up) {
3605 *pval = wlc->stf->ant_rx_ovr;
3607 } else {
3608 if (wlc_phy_ant_rxdiv_get
3609 (wlc->band->pi, &phy_antdiv))
3610 *pval = (int)phy_antdiv;
3611 else
3612 *pval = (int)wlc->stf->ant_rx_ovr;
3615 break;
3617 case WLC_SET_ANTDIV:
3618 /* values are -1=driver default, 0=force0, 1=force1, 2=start1, 3=start0 */
3619 if ((val < -1) || (val > 3)) {
3620 bcmerror = BCME_RANGE;
3621 break;
3624 if (val == -1)
3625 val = ANT_RX_DIV_DEF;
3627 wlc->stf->ant_rx_ovr = (u8) val;
3628 wlc_phy_ant_rxdiv_set(wlc->band->pi, (u8) val);
3629 break;
3631 case WLC_GET_RX_ANT:{ /* get latest used rx antenna */
3632 u16 rxstatus;
3634 if (!wlc->pub->up) {
3635 bcmerror = BCME_NOTUP;
3636 break;
3639 rxstatus = R_REG(wlc->osh, &wlc->regs->phyrxstatus0);
3640 if (rxstatus == 0xdead || rxstatus == (u16) -1) {
3641 bcmerror = BCME_ERROR;
3642 break;
3644 *pval = (rxstatus & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
3645 break;
3648 #if defined(BCMDBG)
3649 case WLC_GET_UCANTDIV:
3650 if (!wlc->clk) {
3651 bcmerror = BCME_NOCLK;
3652 break;
3655 *pval =
3656 (wlc_bmac_mhf_get(wlc->hw, MHF1, WLC_BAND_AUTO) &
3657 MHF1_ANTDIV);
3658 break;
3660 case WLC_SET_UCANTDIV:{
3661 if (!wlc->pub->up) {
3662 bcmerror = BCME_NOTUP;
3663 break;
3666 /* if multiband, band must be locked */
3667 if (IS_MBAND_UNLOCKED(wlc)) {
3668 bcmerror = BCME_NOTBANDLOCKED;
3669 break;
3672 /* 4322 supports antdiv in phy, no need to set it to ucode */
3673 if (WLCISNPHY(wlc->band)
3674 && D11REV_IS(wlc->pub->corerev, 16)) {
3675 WL_ERROR(("wl%d: can't set ucantdiv for 4322\n",
3676 wlc->pub->unit));
3677 bcmerror = BCME_UNSUPPORTED;
3678 } else
3679 wlc_mhf(wlc, MHF1, MHF1_ANTDIV,
3680 (val ? MHF1_ANTDIV : 0), WLC_BAND_AUTO);
3681 break;
3683 #endif /* defined(BCMDBG) */
3685 case WLC_GET_SRL:
3686 *pval = wlc->SRL;
3687 break;
3689 case WLC_SET_SRL:
3690 if (val >= 1 && val <= RETRY_SHORT_MAX) {
3691 int ac;
3692 wlc->SRL = (u16) val;
3694 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3696 for (ac = 0; ac < AC_COUNT; ac++) {
3697 WLC_WME_RETRY_SHORT_SET(wlc, ac, wlc->SRL);
3699 wlc_wme_retries_write(wlc);
3700 } else
3701 bcmerror = BCME_RANGE;
3702 break;
3704 case WLC_GET_LRL:
3705 *pval = wlc->LRL;
3706 break;
3708 case WLC_SET_LRL:
3709 if (val >= 1 && val <= 255) {
3710 int ac;
3711 wlc->LRL = (u16) val;
3713 wlc_bmac_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
3715 for (ac = 0; ac < AC_COUNT; ac++) {
3716 WLC_WME_RETRY_LONG_SET(wlc, ac, wlc->LRL);
3718 wlc_wme_retries_write(wlc);
3719 } else
3720 bcmerror = BCME_RANGE;
3721 break;
3723 case WLC_GET_CWMIN:
3724 *pval = wlc->band->CWmin;
3725 break;
3727 case WLC_SET_CWMIN:
3728 if (!wlc->clk) {
3729 bcmerror = BCME_NOCLK;
3730 break;
3733 if (val >= 1 && val <= 255) {
3734 wlc_set_cwmin(wlc, (u16) val);
3735 } else
3736 bcmerror = BCME_RANGE;
3737 break;
3739 case WLC_GET_CWMAX:
3740 *pval = wlc->band->CWmax;
3741 break;
3743 case WLC_SET_CWMAX:
3744 if (!wlc->clk) {
3745 bcmerror = BCME_NOCLK;
3746 break;
3749 if (val >= 255 && val <= 2047) {
3750 wlc_set_cwmax(wlc, (u16) val);
3751 } else
3752 bcmerror = BCME_RANGE;
3753 break;
3755 case WLC_GET_RADIO: /* use mask if don't want to expose some internal bits */
3756 *pval = wlc->pub->radio_disabled;
3757 break;
3759 case WLC_SET_RADIO:{ /* 32 bits input, higher 16 bits are mask, lower 16 bits are value to
3760 * set
3762 u16 radiomask, radioval;
3763 uint validbits =
3764 WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE;
3765 mbool new = 0;
3767 radiomask = (val & 0xffff0000) >> 16;
3768 radioval = val & 0x0000ffff;
3770 if ((radiomask == 0) || (radiomask & ~validbits)
3771 || (radioval & ~validbits)
3772 || ((radioval & ~radiomask) != 0)) {
3773 WL_ERROR(("SET_RADIO with wrong bits 0x%x\n",
3774 val));
3775 bcmerror = BCME_RANGE;
3776 break;
3779 new =
3780 (wlc->pub->radio_disabled & ~radiomask) | radioval;
3781 wlc->pub->radio_disabled = new;
3783 wlc_radio_hwdisable_upd(wlc);
3784 wlc_radio_upd(wlc);
3785 break;
3788 case WLC_GET_PHYTYPE:
3789 *pval = WLC_PHYTYPE(wlc->band->phytype);
3790 break;
3792 #if defined(BCMDBG)
3793 case WLC_GET_KEY:
3794 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc))) {
3795 wl_wsec_key_t key;
3797 wsec_key_t *src_key = wlc->wsec_keys[val];
3799 if (len < (int)sizeof(key)) {
3800 bcmerror = BCME_BUFTOOSHORT;
3801 break;
3804 bzero((char *)&key, sizeof(key));
3805 if (src_key) {
3806 key.index = src_key->id;
3807 key.len = src_key->len;
3808 bcopy(src_key->data, key.data, key.len);
3809 key.algo = src_key->algo;
3810 if (WSEC_SOFTKEY(wlc, src_key, bsscfg))
3811 key.flags |= WL_SOFT_KEY;
3812 if (src_key->flags & WSEC_PRIMARY_KEY)
3813 key.flags |= WL_PRIMARY_KEY;
3815 bcopy(src_key->ea.octet, key.ea.octet,
3816 ETHER_ADDR_LEN);
3819 bcopy((char *)&key, arg, sizeof(key));
3820 } else
3821 bcmerror = BCME_BADKEYIDX;
3822 break;
3823 #endif /* defined(BCMDBG) */
3825 case WLC_SET_KEY:
3826 bcmerror =
3827 wlc_iovar_op(wlc, "wsec_key", NULL, 0, arg, len, IOV_SET,
3828 wlcif);
3829 break;
3831 case WLC_GET_KEY_SEQ:{
3832 wsec_key_t *key;
3834 if (len < DOT11_WPA_KEY_RSC_LEN) {
3835 bcmerror = BCME_BUFTOOSHORT;
3836 break;
3839 /* Return the key's tx iv as an EAPOL sequence counter.
3840 * This will be used to supply the RSC value to a supplicant.
3841 * The format is 8 bytes, with least significant in seq[0].
3844 key = WSEC_KEY(wlc, val);
3845 if ((val >= 0) && (val < WLC_MAX_WSEC_KEYS(wlc)) &&
3846 (key != NULL)) {
3847 u8 seq[DOT11_WPA_KEY_RSC_LEN];
3848 u16 lo;
3849 u32 hi;
3850 /* group keys in WPA-NONE (IBSS only, AES and TKIP) use a global TXIV */
3851 if ((bsscfg->WPA_auth & WPA_AUTH_NONE)
3852 && ETHER_ISNULLADDR(&key->ea)) {
3853 lo = bsscfg->wpa_none_txiv.lo;
3854 hi = bsscfg->wpa_none_txiv.hi;
3855 } else {
3856 lo = key->txiv.lo;
3857 hi = key->txiv.hi;
3860 /* format the buffer, low to high */
3861 seq[0] = lo & 0xff;
3862 seq[1] = (lo >> 8) & 0xff;
3863 seq[2] = hi & 0xff;
3864 seq[3] = (hi >> 8) & 0xff;
3865 seq[4] = (hi >> 16) & 0xff;
3866 seq[5] = (hi >> 24) & 0xff;
3867 seq[6] = 0;
3868 seq[7] = 0;
3870 bcopy((char *)seq, arg, sizeof(seq));
3871 } else {
3872 bcmerror = BCME_BADKEYIDX;
3874 break;
3877 case WLC_GET_CURR_RATESET:{
3878 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3879 wlc_rateset_t *rs;
3881 if (bsscfg->associated)
3882 rs = &current_bss->rateset;
3883 else
3884 rs = &wlc->default_bss->rateset;
3886 if (len < (int)(rs->count + sizeof(rs->count))) {
3887 bcmerror = BCME_BUFTOOSHORT;
3888 break;
3891 /* Copy only legacy rateset section */
3892 ret_rs->count = rs->count;
3893 bcopy(&rs->rates, &ret_rs->rates, rs->count);
3894 break;
3897 case WLC_GET_RATESET:{
3898 wlc_rateset_t rs;
3899 wl_rateset_t *ret_rs = (wl_rateset_t *) arg;
3901 bzero(&rs, sizeof(wlc_rateset_t));
3902 wlc_default_rateset(wlc, (wlc_rateset_t *) &rs);
3904 if (len < (int)(rs.count + sizeof(rs.count))) {
3905 bcmerror = BCME_BUFTOOSHORT;
3906 break;
3909 /* Copy only legacy rateset section */
3910 ret_rs->count = rs.count;
3911 bcopy(&rs.rates, &ret_rs->rates, rs.count);
3912 break;
3915 case WLC_SET_RATESET:{
3916 wlc_rateset_t rs;
3917 wl_rateset_t *in_rs = (wl_rateset_t *) arg;
3919 if (len < (int)(in_rs->count + sizeof(in_rs->count))) {
3920 bcmerror = BCME_BUFTOOSHORT;
3921 break;
3924 if (in_rs->count > WLC_NUMRATES) {
3925 bcmerror = BCME_BUFTOOLONG;
3926 break;
3929 bzero(&rs, sizeof(wlc_rateset_t));
3931 /* Copy only legacy rateset section */
3932 rs.count = in_rs->count;
3933 bcopy(&in_rs->rates, &rs.rates, rs.count);
3935 /* merge rateset coming in with the current mcsset */
3936 if (N_ENAB(wlc->pub)) {
3937 if (bsscfg->associated)
3938 bcopy(&current_bss->rateset.mcs[0],
3939 rs.mcs, MCSSET_LEN);
3940 else
3941 bcopy(&wlc->default_bss->rateset.mcs[0],
3942 rs.mcs, MCSSET_LEN);
3945 bcmerror = wlc_set_rateset(wlc, &rs);
3947 if (!bcmerror)
3948 wlc_ofdm_rateset_war(wlc);
3950 break;
3953 case WLC_GET_BCNPRD:
3954 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3955 *pval = current_bss->beacon_period;
3956 else
3957 *pval = wlc->default_bss->beacon_period;
3958 break;
3960 case WLC_SET_BCNPRD:
3961 /* range [1, 0xffff] */
3962 if (val >= DOT11_MIN_BEACON_PERIOD
3963 && val <= DOT11_MAX_BEACON_PERIOD) {
3964 wlc->default_bss->beacon_period = (u16) val;
3965 } else
3966 bcmerror = BCME_RANGE;
3967 break;
3969 case WLC_GET_DTIMPRD:
3970 if (BSSCFG_STA(bsscfg) && bsscfg->BSS && bsscfg->associated)
3971 *pval = current_bss->dtim_period;
3972 else
3973 *pval = wlc->default_bss->dtim_period;
3974 break;
3976 case WLC_SET_DTIMPRD:
3977 /* range [1, 0xff] */
3978 if (val >= DOT11_MIN_DTIM_PERIOD
3979 && val <= DOT11_MAX_DTIM_PERIOD) {
3980 wlc->default_bss->dtim_period = (u8) val;
3981 } else
3982 bcmerror = BCME_RANGE;
3983 break;
3985 #ifdef SUPPORT_PS
3986 case WLC_GET_PM:
3987 *pval = wlc->PM;
3988 break;
3990 case WLC_SET_PM:
3991 if ((val >= PM_OFF) && (val <= PM_MAX)) {
3992 wlc->PM = (u8) val;
3993 if (wlc->pub->up) {
3995 /* Change watchdog driver to align watchdog with tbtt if possible */
3996 wlc_watchdog_upd(wlc, PS_ALLOWED(wlc));
3997 } else
3998 bcmerror = BCME_ERROR;
3999 break;
4000 #endif /* SUPPORT_PS */
4002 #ifdef SUPPORT_PS
4003 #ifdef BCMDBG
4004 case WLC_GET_WAKE:
4005 if (AP_ENAB(wlc->pub)) {
4006 bcmerror = BCME_NOTSTA;
4007 break;
4009 *pval = wlc->wake;
4010 break;
4012 case WLC_SET_WAKE:
4013 if (AP_ENAB(wlc->pub)) {
4014 bcmerror = BCME_NOTSTA;
4015 break;
4018 wlc->wake = val ? TRUE : FALSE;
4020 /* if down, we're done */
4021 if (!wlc->pub->up)
4022 break;
4024 /* apply to the mac */
4025 wlc_set_ps_ctrl(wlc);
4026 break;
4027 #endif /* BCMDBG */
4028 #endif /* SUPPORT_PS */
4030 case WLC_GET_REVINFO:
4031 bcmerror = wlc_get_revision_info(wlc, arg, (uint) len);
4032 break;
4034 case WLC_GET_AP:
4035 *pval = (int)AP_ENAB(wlc->pub);
4036 break;
4038 case WLC_GET_ATIM:
4039 if (bsscfg->associated)
4040 *pval = (int)current_bss->atim_window;
4041 else
4042 *pval = (int)wlc->default_bss->atim_window;
4043 break;
4045 case WLC_SET_ATIM:
4046 wlc->default_bss->atim_window = (u32) val;
4047 break;
4049 case WLC_GET_PKTCNTS:{
4050 get_pktcnt_t *pktcnt = (get_pktcnt_t *) pval;
4051 if (WLC_UPDATE_STATS(wlc))
4052 wlc_statsupd(wlc);
4053 pktcnt->rx_good_pkt = WLCNTVAL(wlc->pub->_cnt->rxframe);
4054 pktcnt->rx_bad_pkt = WLCNTVAL(wlc->pub->_cnt->rxerror);
4055 pktcnt->tx_good_pkt =
4056 WLCNTVAL(wlc->pub->_cnt->txfrmsnt);
4057 pktcnt->tx_bad_pkt =
4058 WLCNTVAL(wlc->pub->_cnt->txerror) +
4059 WLCNTVAL(wlc->pub->_cnt->txfail);
4060 if (len >= (int)sizeof(get_pktcnt_t)) {
4061 /* Be backward compatible - only if buffer is large enough */
4062 pktcnt->rx_ocast_good_pkt =
4063 WLCNTVAL(wlc->pub->_cnt->rxmfrmocast);
4065 break;
4068 #ifdef SUPPORT_HWKEY
4069 case WLC_GET_WSEC:
4070 bcmerror =
4071 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_GET,
4072 wlcif);
4073 break;
4075 case WLC_SET_WSEC:
4076 bcmerror =
4077 wlc_iovar_op(wlc, "wsec", NULL, 0, arg, len, IOV_SET,
4078 wlcif);
4079 break;
4081 case WLC_GET_WPA_AUTH:
4082 *pval = (int)bsscfg->WPA_auth;
4083 break;
4085 case WLC_SET_WPA_AUTH:
4086 /* change of WPA_Auth modifies the PS_ALLOWED state */
4087 if (BSSCFG_STA(bsscfg)) {
4088 bsscfg->WPA_auth = (u16) val;
4089 } else
4090 bsscfg->WPA_auth = (u16) val;
4091 break;
4092 #endif /* SUPPORT_HWKEY */
4094 case WLC_GET_BANDLIST:
4095 /* count of number of bands, followed by each band type */
4096 *pval++ = NBANDS(wlc);
4097 *pval++ = wlc->band->bandtype;
4098 if (NBANDS(wlc) > 1)
4099 *pval++ = wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype;
4100 break;
4102 case WLC_GET_BAND:
4103 *pval = wlc->bandlocked ? wlc->band->bandtype : WLC_BAND_AUTO;
4104 break;
4106 case WLC_GET_PHYLIST:
4108 unsigned char *cp = arg;
4109 if (len < 3) {
4110 bcmerror = BCME_BUFTOOSHORT;
4111 break;
4114 if (WLCISNPHY(wlc->band)) {
4115 *cp++ = 'n';
4116 } else if (WLCISLCNPHY(wlc->band)) {
4117 *cp++ = 'c';
4118 } else if (WLCISSSLPNPHY(wlc->band)) {
4119 *cp++ = 's';
4121 *cp = '\0';
4122 break;
4125 case WLC_GET_SHORTSLOT:
4126 *pval = wlc->shortslot;
4127 break;
4129 case WLC_GET_SHORTSLOT_OVERRIDE:
4130 *pval = wlc->shortslot_override;
4131 break;
4133 case WLC_SET_SHORTSLOT_OVERRIDE:
4134 if ((val != WLC_SHORTSLOT_AUTO) &&
4135 (val != WLC_SHORTSLOT_OFF) && (val != WLC_SHORTSLOT_ON)) {
4136 bcmerror = BCME_RANGE;
4137 break;
4140 wlc->shortslot_override = (s8) val;
4142 /* shortslot is an 11g feature, so no more work if we are
4143 * currently on the 5G band
4145 if (BAND_5G(wlc->band->bandtype))
4146 break;
4148 if (wlc->pub->up && wlc->pub->associated) {
4149 /* let watchdog or beacon processing update shortslot */
4150 } else if (wlc->pub->up) {
4151 /* unassociated shortslot is off */
4152 wlc_switch_shortslot(wlc, FALSE);
4153 } else {
4154 /* driver is down, so just update the wlc_info value */
4155 if (wlc->shortslot_override == WLC_SHORTSLOT_AUTO) {
4156 wlc->shortslot = FALSE;
4157 } else {
4158 wlc->shortslot =
4159 (wlc->shortslot_override ==
4160 WLC_SHORTSLOT_ON);
4164 break;
4166 case WLC_GET_LEGACY_ERP:
4167 *pval = wlc->include_legacy_erp;
4168 break;
4170 case WLC_SET_LEGACY_ERP:
4171 if (wlc->include_legacy_erp == bool_val)
4172 break;
4174 wlc->include_legacy_erp = bool_val;
4176 if (AP_ENAB(wlc->pub) && wlc->clk) {
4177 wlc_update_beacon(wlc);
4178 wlc_update_probe_resp(wlc, TRUE);
4180 break;
4182 case WLC_GET_GMODE:
4183 if (wlc->band->bandtype == WLC_BAND_2G)
4184 *pval = wlc->band->gmode;
4185 else if (NBANDS(wlc) > 1)
4186 *pval = wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode;
4187 break;
4189 case WLC_SET_GMODE:
4190 if (!wlc->pub->associated)
4191 bcmerror = wlc_set_gmode(wlc, (u8) val, TRUE);
4192 else {
4193 bcmerror = BCME_ASSOCIATED;
4194 break;
4196 break;
4198 case WLC_GET_GMODE_PROTECTION:
4199 *pval = wlc->protection->_g;
4200 break;
4202 case WLC_GET_PROTECTION_CONTROL:
4203 *pval = wlc->protection->overlap;
4204 break;
4206 case WLC_SET_PROTECTION_CONTROL:
4207 if ((val != WLC_PROTECTION_CTL_OFF) &&
4208 (val != WLC_PROTECTION_CTL_LOCAL) &&
4209 (val != WLC_PROTECTION_CTL_OVERLAP)) {
4210 bcmerror = BCME_RANGE;
4211 break;
4214 wlc_protection_upd(wlc, WLC_PROT_OVERLAP, (s8) val);
4216 /* Current g_protection will sync up to the specified control alg in watchdog
4217 * if the driver is up and associated.
4218 * If the driver is down or not associated, the control setting has no effect.
4220 break;
4222 case WLC_GET_GMODE_PROTECTION_OVERRIDE:
4223 *pval = wlc->protection->g_override;
4224 break;
4226 case WLC_SET_GMODE_PROTECTION_OVERRIDE:
4227 if ((val != WLC_PROTECTION_AUTO) &&
4228 (val != WLC_PROTECTION_OFF) && (val != WLC_PROTECTION_ON)) {
4229 bcmerror = BCME_RANGE;
4230 break;
4233 wlc_protection_upd(wlc, WLC_PROT_G_OVR, (s8) val);
4235 break;
4237 case WLC_SET_SUP_RATESET_OVERRIDE:{
4238 wlc_rateset_t rs, new;
4240 /* copyin */
4241 if (len < (int)sizeof(wlc_rateset_t)) {
4242 bcmerror = BCME_BUFTOOSHORT;
4243 break;
4245 bcopy((char *)arg, (char *)&rs, sizeof(wlc_rateset_t));
4247 /* check for bad count value */
4248 if (rs.count > WLC_NUMRATES) {
4249 bcmerror = BCME_BADRATESET; /* invalid rateset */
4250 break;
4253 /* this command is only appropriate for gmode operation */
4254 if (!(wlc->band->gmode ||
4255 ((NBANDS(wlc) > 1)
4256 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4257 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4258 break;
4261 /* check for an empty rateset to clear the override */
4262 if (rs.count == 0) {
4263 bzero(&wlc->sup_rates_override,
4264 sizeof(wlc_rateset_t));
4265 break;
4268 /* validate rateset by comparing pre and post sorted against 11g hw rates */
4269 wlc_rateset_filter(&rs, &new, FALSE, WLC_RATES_CCK_OFDM,
4270 RATE_MASK, BSS_N_ENAB(wlc, bsscfg));
4271 wlc_rate_hwrs_filter_sort_validate(&new,
4272 &cck_ofdm_rates,
4273 FALSE,
4274 wlc->stf->txstreams);
4275 if (rs.count != new.count) {
4276 bcmerror = BCME_BADRATESET; /* invalid rateset */
4277 break;
4280 /* apply new rateset to the override */
4281 bcopy((char *)&new, (char *)&wlc->sup_rates_override,
4282 sizeof(wlc_rateset_t));
4284 /* update bcn and probe resp if needed */
4285 if (wlc->pub->up && AP_ENAB(wlc->pub)
4286 && wlc->pub->associated) {
4287 wlc_update_beacon(wlc);
4288 wlc_update_probe_resp(wlc, TRUE);
4290 break;
4293 case WLC_GET_SUP_RATESET_OVERRIDE:
4294 /* this command is only appropriate for gmode operation */
4295 if (!(wlc->band->gmode ||
4296 ((NBANDS(wlc) > 1)
4297 && wlc->bandstate[OTHERBANDUNIT(wlc)]->gmode))) {
4298 bcmerror = BCME_BADBAND; /* gmode only command when not in gmode */
4299 break;
4301 if (len < (int)sizeof(wlc_rateset_t)) {
4302 bcmerror = BCME_BUFTOOSHORT;
4303 break;
4305 bcopy((char *)&wlc->sup_rates_override, (char *)arg,
4306 sizeof(wlc_rateset_t));
4308 break;
4310 case WLC_GET_PRB_RESP_TIMEOUT:
4311 *pval = wlc->prb_resp_timeout;
4312 break;
4314 case WLC_SET_PRB_RESP_TIMEOUT:
4315 if (wlc->pub->up) {
4316 bcmerror = BCME_NOTDOWN;
4317 break;
4319 if (val < 0 || val >= 0xFFFF) {
4320 bcmerror = BCME_RANGE; /* bad value */
4321 break;
4323 wlc->prb_resp_timeout = (u16) val;
4324 break;
4326 case WLC_GET_KEY_PRIMARY:{
4327 wsec_key_t *key;
4329 /* treat the 'val' parm as the key id */
4330 key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4331 if (key != NULL) {
4332 *pval = key->id == val ? TRUE : FALSE;
4333 } else {
4334 bcmerror = BCME_BADKEYIDX;
4336 break;
4339 case WLC_SET_KEY_PRIMARY:{
4340 wsec_key_t *key, *old_key;
4342 bcmerror = BCME_BADKEYIDX;
4344 /* treat the 'val' parm as the key id */
4345 for (i = 0; i < WSEC_MAX_DEFAULT_KEYS; i++) {
4346 key = bsscfg->bss_def_keys[i];
4347 if (key != NULL && key->id == val) {
4348 old_key = WSEC_BSS_DEFAULT_KEY(bsscfg);
4349 if (old_key != NULL)
4350 old_key->flags &=
4351 ~WSEC_PRIMARY_KEY;
4352 key->flags |= WSEC_PRIMARY_KEY;
4353 bsscfg->wsec_index = i;
4354 bcmerror = BCME_OK;
4357 break;
4360 #ifdef BCMDBG
4361 case WLC_INIT:
4362 wl_init(wlc->wl);
4363 break;
4364 #endif
4366 case WLC_SET_VAR:
4367 case WLC_GET_VAR:{
4368 char *name;
4369 /* validate the name value */
4370 name = (char *)arg;
4371 for (i = 0; i < (uint) len && *name != '\0';
4372 i++, name++)
4375 if (i == (uint) len) {
4376 bcmerror = BCME_BUFTOOSHORT;
4377 break;
4379 i++; /* include the null in the string length */
4381 if (cmd == WLC_GET_VAR) {
4382 bcmerror =
4383 wlc_iovar_op(wlc, arg,
4384 (void *)((s8 *) arg + i),
4385 len - i, arg, len, IOV_GET,
4386 wlcif);
4387 } else
4388 bcmerror =
4389 wlc_iovar_op(wlc, arg, NULL, 0,
4390 (void *)((s8 *) arg + i),
4391 len - i, IOV_SET, wlcif);
4393 break;
4396 case WLC_SET_WSEC_PMK:
4397 bcmerror = BCME_UNSUPPORTED;
4398 break;
4400 #if defined(BCMDBG)
4401 case WLC_CURRENT_PWR:
4402 if (!wlc->pub->up)
4403 bcmerror = BCME_NOTUP;
4404 else
4405 bcmerror = wlc_get_current_txpwr(wlc, arg, len);
4406 break;
4407 #endif
4409 case WLC_LAST:
4410 WL_ERROR(("%s: WLC_LAST\n", __func__));
4412 done:
4414 if (bcmerror) {
4415 if (VALID_BCMERROR(bcmerror))
4416 wlc->pub->bcmerror = bcmerror;
4417 else {
4418 bcmerror = 0;
4422 #ifdef WLC_LOW
4423 /* BMAC_NOTE: for HIGH_ONLY driver, this seems being called after RPC bus failed */
4424 /* In hw_off condition, IOCTLs that reach here are deemed safe but taclear would
4425 * certainly result in getting -1 for register reads. So skip ta_clear altogether
4427 if (!(wlc->pub->hw_off))
4428 ASSERT(wlc_bmac_taclear(wlc->hw, ta_ok) || !ta_ok);
4429 #endif
4431 return bcmerror;
4434 #if defined(BCMDBG)
4435 /* consolidated register access ioctl error checking */
4436 int wlc_iocregchk(wlc_info_t *wlc, uint band)
4438 /* if band is specified, it must be the current band */
4439 if ((band != WLC_BAND_AUTO) && (band != (uint) wlc->band->bandtype))
4440 return BCME_BADBAND;
4442 /* if multiband and band is not specified, band must be locked */
4443 if ((band == WLC_BAND_AUTO) && IS_MBAND_UNLOCKED(wlc))
4444 return BCME_NOTBANDLOCKED;
4446 /* must have core clocks */
4447 if (!wlc->clk)
4448 return BCME_NOCLK;
4450 return 0;
4452 #endif /* defined(BCMDBG) */
4454 #if defined(BCMDBG)
4455 /* For some ioctls, make sure that the pi pointer matches the current phy */
4456 int wlc_iocpichk(wlc_info_t *wlc, uint phytype)
4458 if (wlc->band->phytype != phytype)
4459 return BCME_BADBAND;
4460 return 0;
4462 #endif
4464 /* Look up the given var name in the given table */
4465 static const bcm_iovar_t *wlc_iovar_lookup(const bcm_iovar_t *table,
4466 const char *name)
4468 const bcm_iovar_t *vi;
4469 const char *lookup_name;
4471 /* skip any ':' delimited option prefixes */
4472 lookup_name = strrchr(name, ':');
4473 if (lookup_name != NULL)
4474 lookup_name++;
4475 else
4476 lookup_name = name;
4478 ASSERT(table != NULL);
4480 for (vi = table; vi->name; vi++) {
4481 if (!strcmp(vi->name, lookup_name))
4482 return vi;
4484 /* ran to end of table */
4486 return NULL; /* var name not found */
4489 /* simplified integer get interface for common WLC_GET_VAR ioctl handler */
4490 int wlc_iovar_getint(wlc_info_t *wlc, const char *name, int *arg)
4492 return wlc_iovar_op(wlc, name, NULL, 0, arg, sizeof(s32), IOV_GET,
4493 NULL);
4496 /* simplified integer set interface for common WLC_SET_VAR ioctl handler */
4497 int wlc_iovar_setint(wlc_info_t *wlc, const char *name, int arg)
4499 return wlc_iovar_op(wlc, name, NULL, 0, (void *)&arg, sizeof(arg),
4500 IOV_SET, NULL);
4503 /* simplified s8 get interface for common WLC_GET_VAR ioctl handler */
4504 int wlc_iovar_gets8(wlc_info_t *wlc, const char *name, s8 *arg)
4506 int iovar_int;
4507 int err;
4509 err =
4510 wlc_iovar_op(wlc, name, NULL, 0, &iovar_int, sizeof(iovar_int),
4511 IOV_GET, NULL);
4512 if (!err)
4513 *arg = (s8) iovar_int;
4515 return err;
4519 * register iovar table, watchdog and down handlers.
4520 * calling function must keep 'iovars' until wlc_module_unregister is called.
4521 * 'iovar' must have the last entry's name field being NULL as terminator.
4523 int wlc_module_register(wlc_pub_t *pub, const bcm_iovar_t *iovars,
4524 const char *name, void *hdl, iovar_fn_t i_fn,
4525 watchdog_fn_t w_fn, down_fn_t d_fn)
4527 wlc_info_t *wlc = (wlc_info_t *) pub->wlc;
4528 int i;
4530 ASSERT(name != NULL);
4531 ASSERT(i_fn != NULL || w_fn != NULL || d_fn != NULL);
4533 /* find an empty entry and just add, no duplication check! */
4534 for (i = 0; i < WLC_MAXMODULES; i++) {
4535 if (wlc->modulecb[i].name[0] == '\0') {
4536 strncpy(wlc->modulecb[i].name, name,
4537 sizeof(wlc->modulecb[i].name) - 1);
4538 wlc->modulecb[i].iovars = iovars;
4539 wlc->modulecb[i].hdl = hdl;
4540 wlc->modulecb[i].iovar_fn = i_fn;
4541 wlc->modulecb[i].watchdog_fn = w_fn;
4542 wlc->modulecb[i].down_fn = d_fn;
4543 return 0;
4547 /* it is time to increase the capacity */
4548 ASSERT(i < WLC_MAXMODULES);
4549 return BCME_NORESOURCE;
4552 /* unregister module callbacks */
4553 int wlc_module_unregister(wlc_pub_t *pub, const char *name, void *hdl)
4555 wlc_info_t *wlc = (wlc_info_t *) pub->wlc;
4556 int i;
4558 if (wlc == NULL)
4559 return BCME_NOTFOUND;
4561 ASSERT(name != NULL);
4563 for (i = 0; i < WLC_MAXMODULES; i++) {
4564 if (!strcmp(wlc->modulecb[i].name, name) &&
4565 (wlc->modulecb[i].hdl == hdl)) {
4566 bzero(&wlc->modulecb[i], sizeof(modulecb_t));
4567 return 0;
4571 /* table not found! */
4572 return BCME_NOTFOUND;
4575 /* Write WME tunable parameters for retransmit/max rate from wlc struct to ucode */
4576 static void wlc_wme_retries_write(wlc_info_t *wlc)
4578 int ac;
4580 /* Need clock to do this */
4581 if (!wlc->clk)
4582 return;
4584 for (ac = 0; ac < AC_COUNT; ac++) {
4585 wlc_write_shm(wlc, M_AC_TXLMT_ADDR(ac), wlc->wme_retries[ac]);
4589 /* Get or set an iovar. The params/p_len pair specifies any additional
4590 * qualifying parameters (e.g. an "element index") for a get, while the
4591 * arg/len pair is the buffer for the value to be set or retrieved.
4592 * Operation (get/set) is specified by the last argument.
4593 * interface context provided by wlcif
4595 * All pointers may point into the same buffer.
4598 wlc_iovar_op(wlc_info_t *wlc, const char *name,
4599 void *params, int p_len, void *arg, int len,
4600 bool set, struct wlc_if *wlcif)
4602 int err = 0;
4603 int val_size;
4604 const bcm_iovar_t *vi = NULL;
4605 u32 actionid;
4606 int i;
4608 ASSERT(name != NULL);
4610 ASSERT(len >= 0);
4612 /* Get MUST have return space */
4613 ASSERT(set || (arg && len));
4615 ASSERT(!(wlc->pub->hw_off && wlc->pub->up));
4617 /* Set does NOT take qualifiers */
4618 ASSERT(!set || (!params && !p_len));
4620 if (!set && (len == sizeof(int)) &&
4621 !(IS_ALIGNED((uintptr) (arg), (uint) sizeof(int)))) {
4622 WL_ERROR(("wl%d: %s unaligned get ptr for %s\n",
4623 wlc->pub->unit, __func__, name));
4624 ASSERT(0);
4627 /* find the given iovar name */
4628 for (i = 0; i < WLC_MAXMODULES; i++) {
4629 if (!wlc->modulecb[i].iovars)
4630 continue;
4631 vi = wlc_iovar_lookup(wlc->modulecb[i].iovars, name);
4632 if (vi)
4633 break;
4635 /* iovar name not found */
4636 if (i >= WLC_MAXMODULES) {
4637 err = BCME_UNSUPPORTED;
4638 #ifdef WLC_HIGH_ONLY
4639 err =
4640 bcmsdh_iovar_op(wlc->btparam, name, params, p_len, arg, len,
4641 set);
4642 #endif
4643 goto exit;
4646 /* set up 'params' pointer in case this is a set command so that
4647 * the convenience int and bool code can be common to set and get
4649 if (params == NULL) {
4650 params = arg;
4651 p_len = len;
4654 if (vi->type == IOVT_VOID)
4655 val_size = 0;
4656 else if (vi->type == IOVT_BUFFER)
4657 val_size = len;
4658 else
4659 /* all other types are integer sized */
4660 val_size = sizeof(int);
4662 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
4664 /* Do the actual parameter implementation */
4665 err = wlc->modulecb[i].iovar_fn(wlc->modulecb[i].hdl, vi, actionid,
4666 name, params, p_len, arg, len, val_size,
4667 wlcif);
4669 exit:
4670 return err;
4674 wlc_iovar_check(wlc_pub_t *pub, const bcm_iovar_t *vi, void *arg, int len,
4675 bool set)
4677 wlc_info_t *wlc = (wlc_info_t *) pub->wlc;
4678 int err = 0;
4679 s32 int_val = 0;
4681 /* check generic condition flags */
4682 if (set) {
4683 if (((vi->flags & IOVF_SET_DOWN) && wlc->pub->up) ||
4684 ((vi->flags & IOVF_SET_UP) && !wlc->pub->up)) {
4685 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4686 } else if ((vi->flags & IOVF_SET_BAND)
4687 && IS_MBAND_UNLOCKED(wlc)) {
4688 err = BCME_NOTBANDLOCKED;
4689 } else if ((vi->flags & IOVF_SET_CLK) && !wlc->clk) {
4690 err = BCME_NOCLK;
4692 } else {
4693 if (((vi->flags & IOVF_GET_DOWN) && wlc->pub->up) ||
4694 ((vi->flags & IOVF_GET_UP) && !wlc->pub->up)) {
4695 err = (wlc->pub->up ? BCME_NOTDOWN : BCME_NOTUP);
4696 } else if ((vi->flags & IOVF_GET_BAND)
4697 && IS_MBAND_UNLOCKED(wlc)) {
4698 err = BCME_NOTBANDLOCKED;
4699 } else if ((vi->flags & IOVF_GET_CLK) && !wlc->clk) {
4700 err = BCME_NOCLK;
4704 if (err)
4705 goto exit;
4707 /* length check on io buf */
4708 err = bcm_iovar_lencheck(vi, arg, len, set);
4709 if (err)
4710 goto exit;
4712 /* On set, check value ranges for integer types */
4713 if (set) {
4714 switch (vi->type) {
4715 case IOVT_BOOL:
4716 case IOVT_INT8:
4717 case IOVT_INT16:
4718 case IOVT_INT32:
4719 case IOVT_UINT8:
4720 case IOVT_UINT16:
4721 case IOVT_UINT32:
4722 bcopy(arg, &int_val, sizeof(int));
4723 err = wlc_iovar_rangecheck(wlc, int_val, vi);
4724 break;
4727 exit:
4728 return err;
4731 /* handler for iovar table wlc_iovars */
4733 * IMPLEMENTATION NOTE: In order to avoid checking for get/set in each
4734 * iovar case, the switch statement maps the iovar id into separate get
4735 * and set values. If you add a new iovar to the switch you MUST use
4736 * IOV_GVAL and/or IOV_SVAL in the case labels to avoid conflict with
4737 * another case.
4738 * Please use params for additional qualifying parameters.
4741 wlc_doiovar(void *hdl, const bcm_iovar_t *vi, u32 actionid,
4742 const char *name, void *params, uint p_len, void *arg, int len,
4743 int val_size, struct wlc_if *wlcif)
4745 wlc_info_t *wlc = hdl;
4746 wlc_bsscfg_t *bsscfg;
4747 int err = 0;
4748 s32 int_val = 0;
4749 s32 int_val2 = 0;
4750 s32 *ret_int_ptr;
4751 bool bool_val;
4752 bool bool_val2;
4753 wlc_bss_info_t *current_bss;
4755 WL_TRACE(("wl%d: %s\n", wlc->pub->unit, __func__));
4757 bsscfg = NULL;
4758 current_bss = NULL;
4760 err = wlc_iovar_check(wlc->pub, vi, arg, len, IOV_ISSET(actionid));
4761 if (err != 0)
4762 return err;
4764 /* convenience int and bool vals for first 8 bytes of buffer */
4765 if (p_len >= (int)sizeof(int_val))
4766 bcopy(params, &int_val, sizeof(int_val));
4768 if (p_len >= (int)sizeof(int_val) * 2)
4769 bcopy((void *)((uintptr) params + sizeof(int_val)), &int_val2,
4770 sizeof(int_val));
4772 /* convenience int ptr for 4-byte gets (requires int aligned arg) */
4773 ret_int_ptr = (s32 *) arg;
4775 bool_val = (int_val != 0) ? TRUE : FALSE;
4776 bool_val2 = (int_val2 != 0) ? TRUE : FALSE;
4778 WL_TRACE(("wl%d: %s: id %d\n", wlc->pub->unit, __func__,
4779 IOV_ID(actionid)));
4780 /* Do the actual parameter implementation */
4781 switch (actionid) {
4783 case IOV_GVAL(IOV_QTXPOWER):{
4784 uint qdbm;
4785 bool override;
4787 err = wlc_phy_txpower_get(wlc->band->pi, &qdbm,
4788 &override);
4789 if (err != BCME_OK)
4790 return err;
4792 /* Return qdbm units */
4793 *ret_int_ptr =
4794 qdbm | (override ? WL_TXPWR_OVERRIDE : 0);
4795 break;
4798 /* As long as override is false, this only sets the *user* targets.
4799 User can twiddle this all he wants with no harm.
4800 wlc_phy_txpower_set() explicitly sets override to false if
4801 not internal or test.
4803 case IOV_SVAL(IOV_QTXPOWER):{
4804 u8 qdbm;
4805 bool override;
4807 /* Remove override bit and clip to max qdbm value */
4808 qdbm = (u8)min_t(u32, (int_val & ~WL_TXPWR_OVERRIDE), 0xff);
4809 /* Extract override setting */
4810 override = (int_val & WL_TXPWR_OVERRIDE) ? TRUE : FALSE;
4811 err =
4812 wlc_phy_txpower_set(wlc->band->pi, qdbm, override);
4813 break;
4816 case IOV_GVAL(IOV_MPC):
4817 *ret_int_ptr = (s32) wlc->mpc;
4818 break;
4820 case IOV_SVAL(IOV_MPC):
4821 wlc->mpc = bool_val;
4822 wlc_radio_mpc_upd(wlc);
4824 break;
4826 case IOV_GVAL(IOV_BCN_LI_BCN):
4827 *ret_int_ptr = wlc->bcn_li_bcn;
4828 break;
4830 case IOV_SVAL(IOV_BCN_LI_BCN):
4831 wlc->bcn_li_bcn = (u8) int_val;
4832 if (wlc->pub->up)
4833 wlc_bcn_li_upd(wlc);
4834 break;
4836 default:
4837 WL_ERROR(("wl%d: %s: unsupported\n", wlc->pub->unit, __func__));
4838 err = BCME_UNSUPPORTED;
4839 break;
4842 goto exit; /* avoid unused label warning */
4844 exit:
4845 return err;
4848 static int
4849 wlc_iovar_rangecheck(wlc_info_t *wlc, u32 val, const bcm_iovar_t *vi)
4851 int err = 0;
4852 u32 min_val = 0;
4853 u32 max_val = 0;
4855 /* Only ranged integers are checked */
4856 switch (vi->type) {
4857 case IOVT_INT32:
4858 max_val |= 0x7fffffff;
4859 /* fall through */
4860 case IOVT_INT16:
4861 max_val |= 0x00007fff;
4862 /* fall through */
4863 case IOVT_INT8:
4864 max_val |= 0x0000007f;
4865 min_val = ~max_val;
4866 if (vi->flags & IOVF_NTRL)
4867 min_val = 1;
4868 else if (vi->flags & IOVF_WHL)
4869 min_val = 0;
4870 /* Signed values are checked against max_val and min_val */
4871 if ((s32) val < (s32) min_val
4872 || (s32) val > (s32) max_val)
4873 err = BCME_RANGE;
4874 break;
4876 case IOVT_UINT32:
4877 max_val |= 0xffffffff;
4878 /* fall through */
4879 case IOVT_UINT16:
4880 max_val |= 0x0000ffff;
4881 /* fall through */
4882 case IOVT_UINT8:
4883 max_val |= 0x000000ff;
4884 if (vi->flags & IOVF_NTRL)
4885 min_val = 1;
4886 if ((val < min_val) || (val > max_val))
4887 err = BCME_RANGE;
4888 break;
4891 return err;
4894 #ifdef BCMDBG
4895 static const char *supr_reason[] = {
4896 "None", "PMQ Entry", "Flush request",
4897 "Previous frag failure", "Channel mismatch",
4898 "Lifetime Expiry", "Underflow"
4901 static void wlc_print_txs_status(u16 s)
4903 printf("[15:12] %d frame attempts\n", (s & TX_STATUS_FRM_RTX_MASK) >>
4904 TX_STATUS_FRM_RTX_SHIFT);
4905 printf(" [11:8] %d rts attempts\n", (s & TX_STATUS_RTS_RTX_MASK) >>
4906 TX_STATUS_RTS_RTX_SHIFT);
4907 printf(" [7] %d PM mode indicated\n",
4908 ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
4909 printf(" [6] %d intermediate status\n",
4910 ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
4911 printf(" [5] %d AMPDU\n", (s & TX_STATUS_AMPDU) ? 1 : 0);
4912 printf(" [4:2] %d Frame Suppressed Reason (%s)\n",
4913 ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
4914 supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
4915 printf(" [1] %d acked\n", ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
4917 #endif /* BCMDBG */
4919 void wlc_print_txstatus(tx_status_t *txs)
4921 #if defined(BCMDBG)
4922 u16 s = txs->status;
4923 u16 ackphyrxsh = txs->ackphyrxsh;
4925 printf("\ntxpkt (MPDU) Complete\n");
4927 printf("FrameID: %04x ", txs->frameid);
4928 printf("TxStatus: %04x", s);
4929 printf("\n");
4930 #ifdef BCMDBG
4931 wlc_print_txs_status(s);
4932 #endif
4933 printf("LastTxTime: %04x ", txs->lasttxtime);
4934 printf("Seq: %04x ", txs->sequence);
4935 printf("PHYTxStatus: %04x ", txs->phyerr);
4936 printf("RxAckRSSI: %04x ",
4937 (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
4938 printf("RxAckSQ: %04x", (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
4939 printf("\n");
4940 #endif /* defined(BCMDBG) */
4943 #define MACSTATUPD(name) \
4944 wlc_ctrupd_cache(macstats.name, &wlc->core->macstat_snapshot->name, &wlc->pub->_cnt->name)
4946 void wlc_statsupd(wlc_info_t *wlc)
4948 int i;
4949 #ifdef BCMDBG
4950 u16 delta;
4951 u16 rxf0ovfl;
4952 u16 txfunfl[NFIFO];
4953 #endif /* BCMDBG */
4955 /* if driver down, make no sense to update stats */
4956 if (!wlc->pub->up)
4957 return;
4959 #ifdef BCMDBG
4960 /* save last rx fifo 0 overflow count */
4961 rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
4963 /* save last tx fifo underflow count */
4964 for (i = 0; i < NFIFO; i++)
4965 txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
4966 #endif /* BCMDBG */
4968 #ifdef BCMDBG
4969 /* check for rx fifo 0 overflow */
4970 delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
4971 if (delta)
4972 WL_ERROR(("wl%d: %u rx fifo 0 overflows!\n", wlc->pub->unit,
4973 delta));
4975 /* check for tx fifo underflows */
4976 for (i = 0; i < NFIFO; i++) {
4977 delta =
4978 (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
4979 txfunfl[i]);
4980 if (delta)
4981 WL_ERROR(("wl%d: %u tx fifo %d underflows!\n",
4982 wlc->pub->unit, delta, i));
4984 #endif /* BCMDBG */
4986 /* dot11 counter update */
4988 WLCNTSET(wlc->pub->_cnt->txrts,
4989 (wlc->pub->_cnt->rxctsucast -
4990 wlc->pub->_cnt->d11cnt_txrts_off));
4991 WLCNTSET(wlc->pub->_cnt->rxcrc,
4992 (wlc->pub->_cnt->rxbadfcs - wlc->pub->_cnt->d11cnt_rxcrc_off));
4993 WLCNTSET(wlc->pub->_cnt->txnocts,
4994 ((wlc->pub->_cnt->txrtsfrm - wlc->pub->_cnt->rxctsucast) -
4995 wlc->pub->_cnt->d11cnt_txnocts_off));
4997 /* merge counters from dma module */
4998 for (i = 0; i < NFIFO; i++) {
4999 if (wlc->hw->di[i]) {
5000 WLCNTADD(wlc->pub->_cnt->txnobuf,
5001 (wlc->hw->di[i])->txnobuf);
5002 WLCNTADD(wlc->pub->_cnt->rxnobuf,
5003 (wlc->hw->di[i])->rxnobuf);
5004 WLCNTADD(wlc->pub->_cnt->rxgiant,
5005 (wlc->hw->di[i])->rxgiants);
5006 dma_counterreset(wlc->hw->di[i]);
5011 * Aggregate transmit and receive errors that probably resulted
5012 * in the loss of a frame are computed on the fly.
5014 WLCNTSET(wlc->pub->_cnt->txerror,
5015 wlc->pub->_cnt->txnobuf + wlc->pub->_cnt->txnoassoc +
5016 wlc->pub->_cnt->txuflo + wlc->pub->_cnt->txrunt +
5017 wlc->pub->_cnt->dmade + wlc->pub->_cnt->dmada +
5018 wlc->pub->_cnt->dmape);
5019 WLCNTSET(wlc->pub->_cnt->rxerror,
5020 wlc->pub->_cnt->rxoflo + wlc->pub->_cnt->rxnobuf +
5021 wlc->pub->_cnt->rxfragerr + wlc->pub->_cnt->rxrunt +
5022 wlc->pub->_cnt->rxgiant + wlc->pub->_cnt->rxnoscb +
5023 wlc->pub->_cnt->rxbadsrcmac);
5024 for (i = 0; i < NFIFO; i++)
5025 WLCNTADD(wlc->pub->_cnt->rxerror, wlc->pub->_cnt->rxuflo[i]);
5028 bool wlc_chipmatch(u16 vendor, u16 device)
5030 if (vendor != VENDOR_BROADCOM) {
5031 WL_ERROR(("wlc_chipmatch: unknown vendor id %04x\n", vendor));
5032 return FALSE;
5035 if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
5036 return TRUE;
5038 if (device == BCM4313_D11N2G_ID)
5039 return TRUE;
5040 if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
5041 return TRUE;
5043 WL_ERROR(("wlc_chipmatch: unknown device id %04x\n", device));
5044 return FALSE;
5047 #if defined(BCMDBG)
5048 void wlc_print_txdesc(d11txh_t *txh)
5050 u16 mtcl = ltoh16(txh->MacTxControlLow);
5051 u16 mtch = ltoh16(txh->MacTxControlHigh);
5052 u16 mfc = ltoh16(txh->MacFrameControl);
5053 u16 tfest = ltoh16(txh->TxFesTimeNormal);
5054 u16 ptcw = ltoh16(txh->PhyTxControlWord);
5055 u16 ptcw_1 = ltoh16(txh->PhyTxControlWord_1);
5056 u16 ptcw_1_Fbr = ltoh16(txh->PhyTxControlWord_1_Fbr);
5057 u16 ptcw_1_Rts = ltoh16(txh->PhyTxControlWord_1_Rts);
5058 u16 ptcw_1_FbrRts = ltoh16(txh->PhyTxControlWord_1_FbrRts);
5059 u16 mainrates = ltoh16(txh->MainRates);
5060 u16 xtraft = ltoh16(txh->XtraFrameTypes);
5061 u8 *iv = txh->IV;
5062 u8 *ra = txh->TxFrameRA;
5063 u16 tfestfb = ltoh16(txh->TxFesTimeFallback);
5064 u8 *rtspfb = txh->RTSPLCPFallback;
5065 u16 rtsdfb = ltoh16(txh->RTSDurFallback);
5066 u8 *fragpfb = txh->FragPLCPFallback;
5067 u16 fragdfb = ltoh16(txh->FragDurFallback);
5068 u16 mmodelen = ltoh16(txh->MModeLen);
5069 u16 mmodefbrlen = ltoh16(txh->MModeFbrLen);
5070 u16 tfid = ltoh16(txh->TxFrameID);
5071 u16 txs = ltoh16(txh->TxStatus);
5072 u16 mnmpdu = ltoh16(txh->MaxNMpdus);
5073 u16 mabyte = ltoh16(txh->MaxABytes_MRT);
5074 u16 mabyte_f = ltoh16(txh->MaxABytes_FBR);
5075 u16 mmbyte = ltoh16(txh->MinMBytes);
5077 u8 *rtsph = txh->RTSPhyHeader;
5078 struct dot11_rts_frame rts = txh->rts_frame;
5079 char hexbuf[256];
5081 /* add plcp header along with txh descriptor */
5082 prhex("Raw TxDesc + plcp header", (unsigned char *) txh, sizeof(d11txh_t) + 48);
5084 printf("TxCtlLow: %04x ", mtcl);
5085 printf("TxCtlHigh: %04x ", mtch);
5086 printf("FC: %04x ", mfc);
5087 printf("FES Time: %04x\n", tfest);
5088 printf("PhyCtl: %04x%s ", ptcw,
5089 (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
5090 printf("PhyCtl_1: %04x ", ptcw_1);
5091 printf("PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
5092 printf("PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
5093 printf("PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
5094 printf("MainRates: %04x ", mainrates);
5095 printf("XtraFrameTypes: %04x ", xtraft);
5096 printf("\n");
5098 bcm_format_hex(hexbuf, iv, sizeof(txh->IV));
5099 printf("SecIV: %s\n", hexbuf);
5100 bcm_format_hex(hexbuf, ra, sizeof(txh->TxFrameRA));
5101 printf("RA: %s\n", hexbuf);
5103 printf("Fb FES Time: %04x ", tfestfb);
5104 bcm_format_hex(hexbuf, rtspfb, sizeof(txh->RTSPLCPFallback));
5105 printf("RTS PLCP: %s ", hexbuf);
5106 printf("RTS DUR: %04x ", rtsdfb);
5107 bcm_format_hex(hexbuf, fragpfb, sizeof(txh->FragPLCPFallback));
5108 printf("PLCP: %s ", hexbuf);
5109 printf("DUR: %04x", fragdfb);
5110 printf("\n");
5112 printf("MModeLen: %04x ", mmodelen);
5113 printf("MModeFbrLen: %04x\n", mmodefbrlen);
5115 printf("FrameID: %04x\n", tfid);
5116 printf("TxStatus: %04x\n", txs);
5118 printf("MaxNumMpdu: %04x\n", mnmpdu);
5119 printf("MaxAggbyte: %04x\n", mabyte);
5120 printf("MaxAggbyte_fb: %04x\n", mabyte_f);
5121 printf("MinByte: %04x\n", mmbyte);
5123 bcm_format_hex(hexbuf, rtsph, sizeof(txh->RTSPhyHeader));
5124 printf("RTS PLCP: %s ", hexbuf);
5125 bcm_format_hex(hexbuf, (u8 *) &rts, sizeof(txh->rts_frame));
5126 printf("RTS Frame: %s", hexbuf);
5127 printf("\n");
5130 #endif /* defined(BCMDBG) */
5132 #if defined(BCMDBG)
5133 void wlc_print_rxh(d11rxhdr_t *rxh)
5135 u16 len = rxh->RxFrameSize;
5136 u16 phystatus_0 = rxh->PhyRxStatus_0;
5137 u16 phystatus_1 = rxh->PhyRxStatus_1;
5138 u16 phystatus_2 = rxh->PhyRxStatus_2;
5139 u16 phystatus_3 = rxh->PhyRxStatus_3;
5140 u16 macstatus1 = rxh->RxStatus1;
5141 u16 macstatus2 = rxh->RxStatus2;
5142 char flagstr[64];
5143 char lenbuf[20];
5144 static const bcm_bit_desc_t macstat_flags[] = {
5145 {RXS_FCSERR, "FCSErr"},
5146 {RXS_RESPFRAMETX, "Reply"},
5147 {RXS_PBPRES, "PADDING"},
5148 {RXS_DECATMPT, "DeCr"},
5149 {RXS_DECERR, "DeCrErr"},
5150 {RXS_BCNSENT, "Bcn"},
5151 {0, NULL}
5154 prhex("Raw RxDesc", (unsigned char *) rxh, sizeof(d11rxhdr_t));
5156 bcm_format_flags(macstat_flags, macstatus1, flagstr, 64);
5158 snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
5160 printf("RxFrameSize: %6s (%d)%s\n", lenbuf, len,
5161 (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
5162 printf("RxPHYStatus: %04x %04x %04x %04x\n",
5163 phystatus_0, phystatus_1, phystatus_2, phystatus_3);
5164 printf("RxMACStatus: %x %s\n", macstatus1, flagstr);
5165 printf("RXMACaggtype: %x\n", (macstatus2 & RXS_AGGTYPE_MASK));
5166 printf("RxTSFTime: %04x\n", rxh->RxTSFTime);
5168 #endif /* defined(BCMDBG) */
5170 #if defined(BCMDBG)
5171 int wlc_format_ssid(char *buf, const unsigned char ssid[], uint ssid_len)
5173 uint i, c;
5174 char *p = buf;
5175 char *endp = buf + SSID_FMT_BUF_LEN;
5177 if (ssid_len > DOT11_MAX_SSID_LEN)
5178 ssid_len = DOT11_MAX_SSID_LEN;
5180 for (i = 0; i < ssid_len; i++) {
5181 c = (uint) ssid[i];
5182 if (c == '\\') {
5183 *p++ = '\\';
5184 *p++ = '\\';
5185 } else if (isprint((unsigned char) c)) {
5186 *p++ = (char)c;
5187 } else {
5188 p += snprintf(p, (endp - p), "\\x%02X", c);
5191 *p = '\0';
5192 ASSERT(p < endp);
5194 return (int)(p - buf);
5196 #endif /* defined(BCMDBG) */
5198 u16 wlc_rate_shm_offset(wlc_info_t *wlc, u8 rate)
5200 return wlc_bmac_rate_shm_offset(wlc->hw, rate);
5203 /* Callback for device removed */
5204 #if defined(WLC_HIGH_ONLY)
5205 void wlc_device_removed(void *arg)
5207 wlc_info_t *wlc = (wlc_info_t *) arg;
5209 wlc->device_present = FALSE;
5211 #endif /* WLC_HIGH_ONLY */
5214 * Attempts to queue a packet onto a multiple-precedence queue,
5215 * if necessary evicting a lower precedence packet from the queue.
5217 * 'prec' is the precedence number that has already been mapped
5218 * from the packet priority.
5220 * Returns TRUE if packet consumed (queued), FALSE if not.
5222 bool BCMFASTPATH
5223 wlc_prec_enq(wlc_info_t *wlc, struct pktq *q, void *pkt, int prec)
5225 return wlc_prec_enq_head(wlc, q, pkt, prec, FALSE);
5228 bool BCMFASTPATH
5229 wlc_prec_enq_head(wlc_info_t *wlc, struct pktq *q, void *pkt, int prec,
5230 bool head)
5232 void *p;
5233 int eprec = -1; /* precedence to evict from */
5235 /* Determine precedence from which to evict packet, if any */
5236 if (pktq_pfull(q, prec))
5237 eprec = prec;
5238 else if (pktq_full(q)) {
5239 p = pktq_peek_tail(q, &eprec);
5240 ASSERT(p != NULL);
5241 if (eprec > prec) {
5242 WL_ERROR(("%s: Failing: eprec %d > prec %d\n", __func__,
5243 eprec, prec));
5244 return FALSE;
5248 /* Evict if needed */
5249 if (eprec >= 0) {
5250 bool discard_oldest;
5252 /* Detect queueing to unconfigured precedence */
5253 ASSERT(!pktq_pempty(q, eprec));
5255 discard_oldest = AC_BITMAP_TST(wlc->wme_dp, eprec);
5257 /* Refuse newer packet unless configured to discard oldest */
5258 if (eprec == prec && !discard_oldest) {
5259 WL_ERROR(("%s: No where to go, prec == %d\n", __func__,
5260 prec));
5261 return FALSE;
5264 /* Evict packet according to discard policy */
5265 p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q,
5266 eprec);
5267 ASSERT(p != NULL);
5269 /* Increment wme stats */
5270 if (WME_ENAB(wlc->pub)) {
5271 WLCNTINCR(wlc->pub->_wme_cnt->
5272 tx_failed[WME_PRIO2AC(PKTPRIO(p))].packets);
5273 WLCNTADD(wlc->pub->_wme_cnt->
5274 tx_failed[WME_PRIO2AC(PKTPRIO(p))].bytes,
5275 pkttotlen(wlc->osh, p));
5278 ASSERT(0);
5279 PKTFREE(wlc->osh, p, TRUE);
5280 WLCNTINCR(wlc->pub->_cnt->txnobuf);
5283 /* Enqueue */
5284 if (head)
5285 p = pktq_penq_head(q, prec, pkt);
5286 else
5287 p = pktq_penq(q, prec, pkt);
5288 ASSERT(p != NULL);
5290 return TRUE;
5293 void BCMFASTPATH wlc_txq_enq(void *ctx, struct scb *scb, void *sdu, uint prec)
5295 wlc_info_t *wlc = (wlc_info_t *) ctx;
5296 wlc_txq_info_t *qi = wlc->active_queue; /* Check me */
5297 struct pktq *q = &qi->q;
5298 int prio;
5300 prio = PKTPRIO(sdu);
5302 ASSERT(pktq_max(q) >= wlc->pub->tunables->datahiwat);
5304 if (!wlc_prec_enq(wlc, q, sdu, prec)) {
5305 if (!EDCF_ENAB(wlc->pub)
5306 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL))
5307 WL_ERROR(("wl%d: wlc_txq_enq: txq overflow\n",
5308 wlc->pub->unit));
5310 /* ASSERT(9 == 8); *//* XXX we might hit this condtion in case packet flooding from mac80211 stack */
5311 PKTFREE(wlc->osh, sdu, TRUE);
5312 WLCNTINCR(wlc->pub->_cnt->txnobuf);
5315 /* Check if flow control needs to be turned on after enqueuing the packet
5316 * Don't turn on flow control if EDCF is enabled. Driver would make the decision on what
5317 * to drop instead of relying on stack to make the right decision
5319 if (!EDCF_ENAB(wlc->pub)
5320 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5321 if (pktq_len(q) >= wlc->pub->tunables->datahiwat) {
5322 wlc_txflowcontrol(wlc, qi, ON, ALLPRIO);
5324 } else if (wlc->pub->_priofc) {
5325 if (pktq_plen(q, wlc_prio2prec_map[prio]) >=
5326 wlc->pub->tunables->datahiwat) {
5327 wlc_txflowcontrol(wlc, qi, ON, prio);
5332 bool BCMFASTPATH
5333 wlc_sendpkt_mac80211(wlc_info_t *wlc, void *sdu, struct ieee80211_hw *hw)
5335 u8 prio;
5336 uint fifo;
5337 void *pkt;
5338 struct scb *scb = &global_scb;
5339 struct dot11_header *d11_header = (struct dot11_header *)PKTDATA(sdu);
5340 u16 type, fc;
5342 ASSERT(sdu);
5344 fc = ltoh16(d11_header->fc);
5345 type = FC_TYPE(fc);
5347 /* 802.11 standard requires management traffic to go at highest priority */
5348 prio = (type == FC_TYPE_DATA ? PKTPRIO(sdu) : MAXPRIO);
5349 fifo = prio2fifo[prio];
5351 ASSERT((uint) PKTHEADROOM(sdu) >= TXOFF);
5352 ASSERT(!PKTSHARED(sdu));
5353 ASSERT(!PKTNEXT(sdu));
5354 ASSERT(!PKTLINK(sdu));
5355 ASSERT(fifo < NFIFO);
5357 pkt = sdu;
5358 if (unlikely
5359 (wlc_d11hdrs_mac80211(wlc, hw, pkt, scb, 0, 1, fifo, 0, NULL, 0)))
5360 return -EINVAL;
5361 wlc_txq_enq(wlc, scb, pkt, WLC_PRIO_TO_PREC(prio));
5362 wlc_send_q(wlc, wlc->active_queue);
5364 WLCNTINCR(wlc->pub->_cnt->ieee_tx);
5365 return 0;
5368 void BCMFASTPATH wlc_send_q(wlc_info_t *wlc, wlc_txq_info_t *qi)
5370 void *pkt[DOT11_MAXNUMFRAGS];
5371 int prec;
5372 u16 prec_map;
5373 int err = 0, i, count;
5374 uint fifo;
5375 struct pktq *q = &qi->q;
5376 struct ieee80211_tx_info *tx_info;
5378 /* only do work for the active queue */
5379 if (qi != wlc->active_queue)
5380 return;
5382 if (in_send_q)
5383 return;
5384 else
5385 in_send_q = TRUE;
5387 prec_map = wlc->tx_prec_map;
5389 /* Send all the enq'd pkts that we can.
5390 * Dequeue packets with precedence with empty HW fifo only
5392 while (prec_map && (pkt[0] = pktq_mdeq(q, prec_map, &prec))) {
5393 tx_info = IEEE80211_SKB_CB(pkt[0]);
5394 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
5395 err = wlc_sendampdu(wlc->ampdu, qi, pkt, prec);
5396 } else {
5397 count = 1;
5398 err = wlc_prep_pdu(wlc, pkt[0], &fifo);
5399 if (!err) {
5400 for (i = 0; i < count; i++) {
5401 wlc_txfifo(wlc, fifo, pkt[i], TRUE, 1);
5406 if (err == BCME_BUSY) {
5407 pktq_penq_head(q, prec, pkt[0]);
5408 /* If send failed due to any other reason than a change in
5409 * HW FIFO condition, quit. Otherwise, read the new prec_map!
5411 if (prec_map == wlc->tx_prec_map)
5412 break;
5413 prec_map = wlc->tx_prec_map;
5417 /* Check if flow control needs to be turned off after sending the packet */
5418 if (!EDCF_ENAB(wlc->pub)
5419 || (wlc->pub->wlfeatureflag & WL_SWFL_FLOWCONTROL)) {
5420 if (wlc_txflowcontrol_prio_isset(wlc, qi, ALLPRIO)
5421 && (pktq_len(q) < wlc->pub->tunables->datahiwat / 2)) {
5422 wlc_txflowcontrol(wlc, qi, OFF, ALLPRIO);
5424 } else if (wlc->pub->_priofc) {
5425 int prio;
5426 for (prio = MAXPRIO; prio >= 0; prio--) {
5427 if (wlc_txflowcontrol_prio_isset(wlc, qi, prio) &&
5428 (pktq_plen(q, wlc_prio2prec_map[prio]) <
5429 wlc->pub->tunables->datahiwat / 2)) {
5430 wlc_txflowcontrol(wlc, qi, OFF, prio);
5434 in_send_q = FALSE;
5438 * bcmc_fid_generate:
5439 * Generate frame ID for a BCMC packet. The frag field is not used
5440 * for MC frames so is used as part of the sequence number.
5442 static inline u16
5443 bcmc_fid_generate(wlc_info_t *wlc, wlc_bsscfg_t *bsscfg, d11txh_t *txh)
5445 u16 frameid;
5447 frameid = ltoh16(txh->TxFrameID) & ~(TXFID_SEQ_MASK | TXFID_QUEUE_MASK);
5448 frameid |=
5449 (((wlc->
5450 mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5451 TX_BCMC_FIFO;
5453 return frameid;
5456 void BCMFASTPATH
5457 wlc_txfifo(wlc_info_t *wlc, uint fifo, void *p, bool commit, s8 txpktpend)
5459 u16 frameid = INVALIDFID;
5460 d11txh_t *txh;
5462 ASSERT(fifo < NFIFO);
5463 txh = (d11txh_t *) PKTDATA(p);
5465 /* When a BC/MC frame is being committed to the BCMC fifo via DMA (NOT PIO), update
5466 * ucode or BSS info as appropriate.
5468 if (fifo == TX_BCMC_FIFO) {
5469 frameid = ltoh16(txh->TxFrameID);
5473 if (WLC_WAR16165(wlc))
5474 wlc_war16165(wlc, TRUE);
5476 #ifdef WLC_HIGH_ONLY
5477 if (RPCTX_ENAB(wlc->pub)) {
5478 (void)wlc_rpctx_tx(wlc->rpctx, fifo, p, commit, frameid,
5479 txpktpend);
5480 return;
5482 #else
5484 /* Bump up pending count for if not using rpc. If rpc is used, this will be handled
5485 * in wlc_bmac_txfifo()
5487 if (commit) {
5488 TXPKTPENDINC(wlc, fifo, txpktpend);
5489 WL_TRACE(("wlc_txfifo, pktpend inc %d to %d\n", txpktpend,
5490 TXPKTPENDGET(wlc, fifo)));
5493 /* Commit BCMC sequence number in the SHM frame ID location */
5494 if (frameid != INVALIDFID)
5495 BCMCFID(wlc, frameid);
5497 if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0) {
5498 WL_ERROR(("wlc_txfifo: fatal, toss frames !!!\n"));
5500 #endif /* WLC_HIGH_ONLY */
5503 static u16
5504 wlc_compute_airtime(wlc_info_t *wlc, ratespec_t rspec, uint length)
5506 u16 usec = 0;
5507 uint mac_rate = RSPEC2RATE(rspec);
5508 uint nsyms;
5510 if (IS_MCS(rspec)) {
5511 /* not supported yet */
5512 ASSERT(0);
5513 } else if (IS_OFDM(rspec)) {
5514 /* nsyms = Ceiling(Nbits / (Nbits/sym))
5516 * Nbits = length * 8
5517 * Nbits/sym = Mbps * 4 = mac_rate * 2
5519 nsyms = CEIL((length * 8), (mac_rate * 2));
5521 /* usec = symbols * usec/symbol */
5522 usec = (u16) (nsyms * APHY_SYMBOL_TIME);
5523 return usec;
5524 } else {
5525 switch (mac_rate) {
5526 case WLC_RATE_1M:
5527 usec = length << 3;
5528 break;
5529 case WLC_RATE_2M:
5530 usec = length << 2;
5531 break;
5532 case WLC_RATE_5M5:
5533 usec = (length << 4) / 11;
5534 break;
5535 case WLC_RATE_11M:
5536 usec = (length << 3) / 11;
5537 break;
5538 default:
5539 WL_ERROR(("wl%d: wlc_compute_airtime: unsupported rspec 0x%x\n", wlc->pub->unit, rspec));
5540 ASSERT((const char *)"Bad phy_rate" == NULL);
5541 break;
5545 return usec;
5548 void BCMFASTPATH
5549 wlc_compute_plcp(wlc_info_t *wlc, ratespec_t rspec, uint length, u8 *plcp)
5551 if (IS_MCS(rspec)) {
5552 wlc_compute_mimo_plcp(rspec, length, plcp);
5553 } else if (IS_OFDM(rspec)) {
5554 wlc_compute_ofdm_plcp(rspec, length, plcp);
5555 } else {
5556 wlc_compute_cck_plcp(rspec, length, plcp);
5558 return;
5561 /* Rate: 802.11 rate code, length: PSDU length in octets */
5562 static void wlc_compute_mimo_plcp(ratespec_t rspec, uint length, u8 *plcp)
5564 u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
5565 ASSERT(IS_MCS(rspec));
5566 plcp[0] = mcs;
5567 if (RSPEC_IS40MHZ(rspec) || (mcs == 32))
5568 plcp[0] |= MIMO_PLCP_40MHZ;
5569 WLC_SET_MIMO_PLCP_LEN(plcp, length);
5570 plcp[3] = RSPEC_MIMOPLCP3(rspec); /* rspec already holds this byte */
5571 plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
5572 plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
5573 plcp[5] = 0;
5576 /* Rate: 802.11 rate code, length: PSDU length in octets */
5577 static void BCMFASTPATH
5578 wlc_compute_ofdm_plcp(ratespec_t rspec, u32 length, u8 *plcp)
5580 u8 rate_signal;
5581 u32 tmp = 0;
5582 int rate = RSPEC2RATE(rspec);
5584 ASSERT(IS_OFDM(rspec));
5586 /* encode rate per 802.11a-1999 sec 17.3.4.1, with lsb transmitted first */
5587 rate_signal = rate_info[rate] & RATE_MASK;
5588 ASSERT(rate_signal != 0);
5590 bzero(plcp, D11_PHY_HDR_LEN);
5591 D11A_PHY_HDR_SRATE((ofdm_phy_hdr_t *) plcp, rate_signal);
5593 tmp = (length & 0xfff) << 5;
5594 plcp[2] |= (tmp >> 16) & 0xff;
5595 plcp[1] |= (tmp >> 8) & 0xff;
5596 plcp[0] |= tmp & 0xff;
5598 return;
5602 * Compute PLCP, but only requires actual rate and length of pkt.
5603 * Rate is given in the driver standard multiple of 500 kbps.
5604 * le is set for 11 Mbps rate if necessary.
5605 * Broken out for PRQ.
5608 static void wlc_cck_plcp_set(int rate_500, uint length, u8 *plcp)
5610 u16 usec = 0;
5611 u8 le = 0;
5613 switch (rate_500) {
5614 case WLC_RATE_1M:
5615 usec = length << 3;
5616 break;
5617 case WLC_RATE_2M:
5618 usec = length << 2;
5619 break;
5620 case WLC_RATE_5M5:
5621 usec = (length << 4) / 11;
5622 if ((length << 4) - (usec * 11) > 0)
5623 usec++;
5624 break;
5625 case WLC_RATE_11M:
5626 usec = (length << 3) / 11;
5627 if ((length << 3) - (usec * 11) > 0) {
5628 usec++;
5629 if ((usec * 11) - (length << 3) >= 8)
5630 le = D11B_PLCP_SIGNAL_LE;
5632 break;
5634 default:
5635 WL_ERROR(("wlc_cck_plcp_set: unsupported rate %d\n", rate_500));
5636 rate_500 = WLC_RATE_1M;
5637 usec = length << 3;
5638 break;
5640 /* PLCP signal byte */
5641 plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
5642 /* PLCP service byte */
5643 plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
5644 /* PLCP length u16, little endian */
5645 plcp[2] = usec & 0xff;
5646 plcp[3] = (usec >> 8) & 0xff;
5647 /* PLCP CRC16 */
5648 plcp[4] = 0;
5649 plcp[5] = 0;
5652 /* Rate: 802.11 rate code, length: PSDU length in octets */
5653 static void wlc_compute_cck_plcp(ratespec_t rspec, uint length, u8 *plcp)
5655 int rate = RSPEC2RATE(rspec);
5657 ASSERT(IS_CCK(rspec));
5659 wlc_cck_plcp_set(rate, length, plcp);
5662 /* wlc_compute_frame_dur()
5664 * Calculate the 802.11 MAC header DUR field for MPDU
5665 * DUR for a single frame = 1 SIFS + 1 ACK
5666 * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5668 * rate MPDU rate in unit of 500kbps
5669 * next_frag_len next MPDU length in bytes
5670 * preamble_type use short/GF or long/MM PLCP header
5672 static u16 BCMFASTPATH
5673 wlc_compute_frame_dur(wlc_info_t *wlc, ratespec_t rate, u8 preamble_type,
5674 uint next_frag_len)
5676 u16 dur, sifs;
5678 sifs = SIFS(wlc->band);
5680 dur = sifs;
5681 dur += (u16) wlc_calc_ack_time(wlc, rate, preamble_type);
5683 if (next_frag_len) {
5684 /* Double the current DUR to get 2 SIFS + 2 ACKs */
5685 dur *= 2;
5686 /* add another SIFS and the frag time */
5687 dur += sifs;
5688 dur +=
5689 (u16) wlc_calc_frame_time(wlc, rate, preamble_type,
5690 next_frag_len);
5692 return dur;
5695 /* wlc_compute_rtscts_dur()
5697 * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
5698 * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
5699 * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
5701 * cts cts-to-self or rts/cts
5702 * rts_rate rts or cts rate in unit of 500kbps
5703 * rate next MPDU rate in unit of 500kbps
5704 * frame_len next MPDU frame length in bytes
5706 u16 BCMFASTPATH
5707 wlc_compute_rtscts_dur(wlc_info_t *wlc, bool cts_only, ratespec_t rts_rate,
5708 ratespec_t frame_rate, u8 rts_preamble_type,
5709 u8 frame_preamble_type, uint frame_len, bool ba)
5711 u16 dur, sifs;
5713 sifs = SIFS(wlc->band);
5715 if (!cts_only) { /* RTS/CTS */
5716 dur = 3 * sifs;
5717 dur +=
5718 (u16) wlc_calc_cts_time(wlc, rts_rate,
5719 rts_preamble_type);
5720 } else { /* CTS-TO-SELF */
5721 dur = 2 * sifs;
5724 dur +=
5725 (u16) wlc_calc_frame_time(wlc, frame_rate, frame_preamble_type,
5726 frame_len);
5727 if (ba)
5728 dur +=
5729 (u16) wlc_calc_ba_time(wlc, frame_rate,
5730 WLC_SHORT_PREAMBLE);
5731 else
5732 dur +=
5733 (u16) wlc_calc_ack_time(wlc, frame_rate,
5734 frame_preamble_type);
5735 return dur;
5738 static bool wlc_phy_rspec_check(wlc_info_t *wlc, u16 bw, ratespec_t rspec)
5740 if (IS_MCS(rspec)) {
5741 uint mcs = rspec & RSPEC_RATE_MASK;
5743 if (mcs < 8) {
5744 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5745 } else if ((mcs >= 8) && (mcs <= 23)) {
5746 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SDM);
5747 } else if (mcs == 32) {
5748 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_SDM);
5749 ASSERT(bw == PHY_TXC1_BW_40MHZ_DUP);
5751 } else if (IS_OFDM(rspec)) {
5752 ASSERT(RSPEC_STF(rspec) < PHY_TXC1_MODE_STBC);
5753 } else {
5754 ASSERT(IS_CCK(rspec));
5756 ASSERT((bw == PHY_TXC1_BW_20MHZ)
5757 || (bw == PHY_TXC1_BW_20MHZ_UP));
5758 ASSERT(RSPEC_STF(rspec) == PHY_TXC1_MODE_SISO);
5761 return TRUE;
5764 u16 BCMFASTPATH wlc_phytxctl1_calc(wlc_info_t *wlc, ratespec_t rspec)
5766 u16 phyctl1 = 0;
5767 u16 bw;
5769 if (WLCISLCNPHY(wlc->band)) {
5770 bw = PHY_TXC1_BW_20MHZ;
5771 } else {
5772 bw = RSPEC_GET_BW(rspec);
5773 /* 10Mhz is not supported yet */
5774 if (bw < PHY_TXC1_BW_20MHZ) {
5775 WL_ERROR(("wlc_phytxctl1_calc: bw %d is not supported yet, set to 20L\n", bw));
5776 bw = PHY_TXC1_BW_20MHZ;
5779 wlc_phy_rspec_check(wlc, bw, rspec);
5782 if (IS_MCS(rspec)) {
5783 uint mcs = rspec & RSPEC_RATE_MASK;
5785 /* bw, stf, coding-type is part of RSPEC_PHYTXBYTE2 returns */
5786 phyctl1 = RSPEC_PHYTXBYTE2(rspec);
5787 /* set the upper byte of phyctl1 */
5788 phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
5789 } else if (IS_CCK(rspec) && !WLCISLCNPHY(wlc->band)
5790 && !WLCISSSLPNPHY(wlc->band)) {
5791 /* In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate */
5792 /* Eventually MIMOPHY would also be converted to this format */
5793 /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
5794 phyctl1 = (bw | (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5795 } else { /* legacy OFDM/CCK */
5796 s16 phycfg;
5797 /* get the phyctl byte from rate phycfg table */
5798 phycfg = wlc_rate_legacy_phyctl(RSPEC2RATE(rspec));
5799 if (phycfg == -1) {
5800 WL_ERROR(("wlc_phytxctl1_calc: wrong legacy OFDM/CCK rate\n"));
5801 ASSERT(0);
5802 phycfg = 0;
5804 /* set the upper byte of phyctl1 */
5805 phyctl1 =
5806 (bw | (phycfg << 8) |
5807 (RSPEC_STF(rspec) << PHY_TXC1_MODE_SHIFT));
5810 #ifdef BCMDBG
5811 /* phy clock must support 40Mhz if tx descriptor uses it */
5812 if ((phyctl1 & PHY_TXC1_BW_MASK) >= PHY_TXC1_BW_40MHZ) {
5813 ASSERT(CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ);
5814 #ifndef WLC_HIGH_ONLY
5815 ASSERT(wlc->chanspec == wlc_phy_chanspec_get(wlc->band->pi));
5816 #endif
5818 #endif /* BCMDBG */
5819 return phyctl1;
5822 ratespec_t BCMFASTPATH
5823 wlc_rspec_to_rts_rspec(wlc_info_t *wlc, ratespec_t rspec, bool use_rspec,
5824 u16 mimo_ctlchbw)
5826 ratespec_t rts_rspec = 0;
5828 if (use_rspec) {
5829 /* use frame rate as rts rate */
5830 rts_rspec = rspec;
5832 } else if (wlc->band->gmode && wlc->protection->_g && !IS_CCK(rspec)) {
5833 /* Use 11Mbps as the g protection RTS target rate and fallback.
5834 * Use the WLC_BASIC_RATE() lookup to find the best basic rate under the
5835 * target in case 11 Mbps is not Basic.
5836 * 6 and 9 Mbps are not usually selected by rate selection, but even
5837 * if the OFDM rate we are protecting is 6 or 9 Mbps, 11 is more robust.
5839 rts_rspec = WLC_BASIC_RATE(wlc, WLC_RATE_11M);
5840 } else {
5841 /* calculate RTS rate and fallback rate based on the frame rate
5842 * RTS must be sent at a basic rate since it is a
5843 * control frame, sec 9.6 of 802.11 spec
5845 rts_rspec = WLC_BASIC_RATE(wlc, rspec);
5848 if (WLC_PHY_11N_CAP(wlc->band)) {
5849 /* set rts txbw to correct side band */
5850 rts_rspec &= ~RSPEC_BW_MASK;
5852 /* if rspec/rspec_fallback is 40MHz, then send RTS on both 20MHz channel
5853 * (DUP), otherwise send RTS on control channel
5855 if (RSPEC_IS40MHZ(rspec) && !IS_CCK(rts_rspec))
5856 rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
5857 else
5858 rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
5860 /* pick siso/cdd as default for ofdm */
5861 if (IS_OFDM(rts_rspec)) {
5862 rts_rspec &= ~RSPEC_STF_MASK;
5863 rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
5866 return rts_rspec;
5870 * Add d11txh_t, cck_phy_hdr_t.
5872 * 'p' data must start with 802.11 MAC header
5873 * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
5875 * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
5878 static u16 BCMFASTPATH
5879 wlc_d11hdrs_mac80211(wlc_info_t *wlc, struct ieee80211_hw *hw,
5880 void *p, struct scb *scb, uint frag,
5881 uint nfrags, uint queue, uint next_frag_len,
5882 wsec_key_t *key, ratespec_t rspec_override)
5884 struct dot11_header *h;
5885 d11txh_t *txh;
5886 u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
5887 osl_t *osh;
5888 int len, phylen, rts_phylen;
5889 u16 fc, type, frameid, mch, phyctl, xfts, mainrates;
5890 u16 seq = 0, mcl = 0, status = 0;
5891 ratespec_t rspec[2] = { WLC_RATE_1M, WLC_RATE_1M }, rts_rspec[2] = {
5892 WLC_RATE_1M, WLC_RATE_1M};
5893 bool use_rts = FALSE;
5894 bool use_cts = FALSE;
5895 bool use_rifs = FALSE;
5896 bool short_preamble[2] = { FALSE, FALSE };
5897 u8 preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5898 u8 rts_preamble_type[2] = { WLC_LONG_PREAMBLE, WLC_LONG_PREAMBLE };
5899 u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
5900 struct dot11_rts_frame *rts = NULL;
5901 bool qos;
5902 uint ac;
5903 u32 rate_val[2];
5904 bool hwtkmic = FALSE;
5905 u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
5906 #ifdef WLANTSEL
5907 #define ANTCFG_NONE 0xFF
5908 u8 antcfg = ANTCFG_NONE;
5909 u8 fbantcfg = ANTCFG_NONE;
5910 #endif
5911 uint phyctl1_stf = 0;
5912 u16 durid = 0;
5913 struct ieee80211_tx_rate *txrate[2];
5914 int k;
5915 struct ieee80211_tx_info *tx_info;
5916 bool is_mcs[2];
5917 u16 mimo_txbw;
5918 u8 mimo_preamble_type;
5920 frameid = 0;
5922 ASSERT(queue < NFIFO);
5924 osh = wlc->osh;
5926 /* locate 802.11 MAC header */
5927 h = (struct dot11_header *)PKTDATA(p);
5928 fc = ltoh16(h->fc);
5929 type = FC_TYPE(fc);
5931 qos = (type == FC_TYPE_DATA && FC_SUBTYPE_ANY_QOS(FC_SUBTYPE(fc)));
5933 /* compute length of frame in bytes for use in PLCP computations */
5934 len = pkttotlen(osh, p);
5935 phylen = len + DOT11_FCS_LEN;
5937 /* If WEP enabled, add room in phylen for the additional bytes of
5938 * ICV which MAC generates. We do NOT add the additional bytes to
5939 * the packet itself, thus phylen = packet length + ICV_LEN + FCS_LEN
5940 * in this case
5942 if (key) {
5943 phylen += key->icv_len;
5946 /* Get tx_info */
5947 tx_info = IEEE80211_SKB_CB(p);
5948 ASSERT(tx_info);
5950 /* add PLCP */
5951 plcp = PKTPUSH(p, D11_PHY_HDR_LEN);
5953 /* add Broadcom tx descriptor header */
5954 txh = (d11txh_t *) PKTPUSH(p, D11_TXH_LEN);
5955 bzero((char *)txh, D11_TXH_LEN);
5957 /* setup frameid */
5958 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
5959 /* non-AP STA should never use BCMC queue */
5960 ASSERT(queue != TX_BCMC_FIFO);
5961 if (queue == TX_BCMC_FIFO) {
5962 WL_ERROR(("wl%d: %s: ASSERT queue == TX_BCMC!\n",
5963 WLCWLUNIT(wlc), __func__));
5964 frameid = bcmc_fid_generate(wlc, NULL, txh);
5965 } else {
5966 /* Increment the counter for first fragment */
5967 if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
5968 SCB_SEQNUM(scb, PKTPRIO(p))++;
5971 /* extract fragment number from frame first */
5972 seq = ltoh16(seq) & FRAGNUM_MASK;
5973 seq |= (SCB_SEQNUM(scb, PKTPRIO(p)) << SEQNUM_SHIFT);
5974 h->seq = htol16(seq);
5976 frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5977 (queue & TXFID_QUEUE_MASK);
5980 frameid |= queue & TXFID_QUEUE_MASK;
5982 /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
5983 if (SCB_PS(scb) || ((fc & FC_KIND_MASK) == FC_BEACON))
5984 mcl |= TXC_IGNOREPMQ;
5986 ASSERT(hw->max_rates <= IEEE80211_TX_MAX_RATES);
5987 ASSERT(hw->max_rates == 2);
5989 txrate[0] = tx_info->control.rates;
5990 txrate[1] = txrate[0] + 1;
5992 ASSERT(txrate[0]->idx >= 0);
5993 /* if rate control algorithm didn't give us a fallback rate, use the primary rate */
5994 if (txrate[1]->idx < 0) {
5995 txrate[1] = txrate[0];
5997 #ifdef WLC_HIGH_ONLY
5998 /* Double protection , just in case */
5999 if (txrate[0]->idx > HIGHEST_SINGLE_STREAM_MCS)
6000 txrate[0]->idx = HIGHEST_SINGLE_STREAM_MCS;
6001 if (txrate[1]->idx > HIGHEST_SINGLE_STREAM_MCS)
6002 txrate[1]->idx = HIGHEST_SINGLE_STREAM_MCS;
6003 #endif
6005 for (k = 0; k < hw->max_rates; k++) {
6006 is_mcs[k] =
6007 txrate[k]->flags & IEEE80211_TX_RC_MCS ? TRUE : FALSE;
6008 if (!is_mcs[k]) {
6009 ASSERT(!(tx_info->flags & IEEE80211_TX_CTL_AMPDU));
6010 if ((txrate[k]->idx >= 0)
6011 && (txrate[k]->idx <
6012 hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6013 rate_val[k] =
6014 hw->wiphy->bands[tx_info->band]->
6015 bitrates[txrate[k]->idx].hw_value;
6016 short_preamble[k] =
6017 txrate[k]->
6018 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
6019 TRUE : FALSE;
6020 } else {
6021 ASSERT((txrate[k]->idx >= 0) &&
6022 (txrate[k]->idx <
6023 hw->wiphy->bands[tx_info->band]->
6024 n_bitrates));
6025 rate_val[k] = WLC_RATE_1M;
6027 } else {
6028 rate_val[k] = txrate[k]->idx;
6030 /* Currently only support same setting for primay and fallback rates.
6031 * Unify flags for each rate into a single value for the frame
6033 use_rts |=
6034 txrate[k]->
6035 flags & IEEE80211_TX_RC_USE_RTS_CTS ? TRUE : FALSE;
6036 use_cts |=
6037 txrate[k]->
6038 flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? TRUE : FALSE;
6040 if (is_mcs[k])
6041 rate_val[k] |= NRATE_MCS_INUSE;
6043 rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band, rate_val[k]);
6045 /* (1) RATE: determine and validate primary rate and fallback rates */
6046 if (!RSPEC_ACTIVE(rspec[k])) {
6047 ASSERT(RSPEC_ACTIVE(rspec[k]));
6048 rspec[k] = WLC_RATE_1M;
6049 } else {
6050 if (WLANTSEL_ENAB(wlc) && !ETHER_ISMULTI(&h->a1)) {
6051 /* set tx antenna config */
6052 wlc_antsel_antcfg_get(wlc->asi, FALSE, FALSE, 0,
6053 0, &antcfg, &fbantcfg);
6058 phyctl1_stf = wlc->stf->ss_opmode;
6060 if (N_ENAB(wlc->pub)) {
6061 for (k = 0; k < hw->max_rates; k++) {
6062 /* apply siso/cdd to single stream mcs's or ofdm if rspec is auto selected */
6063 if (((IS_MCS(rspec[k]) &&
6064 IS_SINGLE_STREAM(rspec[k] & RSPEC_RATE_MASK)) ||
6065 IS_OFDM(rspec[k]))
6066 && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
6067 || !(rspec[k] & RSPEC_OVERRIDE))) {
6068 rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
6070 /* For SISO MCS use STBC if possible */
6071 if (IS_MCS(rspec[k])
6072 && WLC_STF_SS_STBC_TX(wlc, scb)) {
6073 u8 stc;
6075 ASSERT(WLC_STBC_CAP_PHY(wlc));
6076 stc = 1; /* Nss for single stream is always 1 */
6077 rspec[k] |=
6078 (PHY_TXC1_MODE_STBC <<
6079 RSPEC_STF_SHIFT) | (stc <<
6080 RSPEC_STC_SHIFT);
6081 } else
6082 rspec[k] |=
6083 (phyctl1_stf << RSPEC_STF_SHIFT);
6086 /* Is the phy configured to use 40MHZ frames? If so then pick the desired txbw */
6087 if (CHSPEC_WLC_BW(wlc->chanspec) == WLC_40_MHZ) {
6088 /* default txbw is 20in40 SB */
6089 mimo_ctlchbw = mimo_txbw =
6090 CHSPEC_SB_UPPER(WLC_BAND_PI_RADIO_CHANSPEC)
6091 ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
6093 if (IS_MCS(rspec[k])) {
6094 /* mcs 32 must be 40b/w DUP */
6095 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
6096 mimo_txbw =
6097 PHY_TXC1_BW_40MHZ_DUP;
6098 /* use override */
6099 } else if (wlc->mimo_40txbw != AUTO)
6100 mimo_txbw = wlc->mimo_40txbw;
6101 /* else check if dst is using 40 Mhz */
6102 else if (scb->flags & SCB_IS40)
6103 mimo_txbw = PHY_TXC1_BW_40MHZ;
6104 } else if (IS_OFDM(rspec[k])) {
6105 if (wlc->ofdm_40txbw != AUTO)
6106 mimo_txbw = wlc->ofdm_40txbw;
6107 } else {
6108 ASSERT(IS_CCK(rspec[k]));
6109 if (wlc->cck_40txbw != AUTO)
6110 mimo_txbw = wlc->cck_40txbw;
6112 } else {
6113 /* mcs32 is 40 b/w only.
6114 * This is possible for probe packets on a STA during SCAN
6116 if ((rspec[k] & RSPEC_RATE_MASK) == 32) {
6117 /* mcs 0 */
6118 rspec[k] = RSPEC_MIMORATE;
6120 mimo_txbw = PHY_TXC1_BW_20MHZ;
6123 /* Set channel width */
6124 rspec[k] &= ~RSPEC_BW_MASK;
6125 if ((k == 0) || ((k > 0) && IS_MCS(rspec[k])))
6126 rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
6127 else
6128 rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6130 /* Set Short GI */
6131 #ifdef NOSGIYET
6132 if (IS_MCS(rspec[k])
6133 && (txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
6134 rspec[k] |= RSPEC_SHORT_GI;
6135 else if (!(txrate[k]->flags & IEEE80211_TX_RC_SHORT_GI))
6136 rspec[k] &= ~RSPEC_SHORT_GI;
6137 #else
6138 rspec[k] &= ~RSPEC_SHORT_GI;
6139 #endif
6141 mimo_preamble_type = WLC_MM_PREAMBLE;
6142 if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6143 mimo_preamble_type = WLC_GF_PREAMBLE;
6145 if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6146 && (!IS_MCS(rspec[k]))) {
6147 WL_ERROR(("wl%d: %s: IEEE80211_TX_RC_MCS != IS_MCS(rspec)\n", WLCWLUNIT(wlc), __func__));
6148 ASSERT(0 && "Rate mismatch");
6151 if (IS_MCS(rspec[k])) {
6152 preamble_type[k] = mimo_preamble_type;
6154 /* if SGI is selected, then forced mm for single stream */
6155 if ((rspec[k] & RSPEC_SHORT_GI)
6156 && IS_SINGLE_STREAM(rspec[k] &
6157 RSPEC_RATE_MASK)) {
6158 preamble_type[k] = WLC_MM_PREAMBLE;
6162 /* mimo bw field MUST now be valid in the rspec (it affects duration calculations) */
6163 ASSERT(VALID_RATE_DBG(wlc, rspec[0]));
6165 /* should be better conditionalized */
6166 if (!IS_MCS(rspec[0])
6167 && (tx_info->control.rates[0].
6168 flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6169 preamble_type[k] = WLC_SHORT_PREAMBLE;
6171 ASSERT(!IS_MCS(rspec[0])
6172 || WLC_IS_MIMO_PREAMBLE(preamble_type[k]));
6174 } else {
6175 for (k = 0; k < hw->max_rates; k++) {
6176 /* Set ctrlchbw as 20Mhz */
6177 ASSERT(!IS_MCS(rspec[k]));
6178 rspec[k] &= ~RSPEC_BW_MASK;
6179 rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6181 /* for nphy, stf of ofdm frames must follow policies */
6182 if (WLCISNPHY(wlc->band) && IS_OFDM(rspec[k])) {
6183 rspec[k] &= ~RSPEC_STF_MASK;
6184 rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6189 /* Reset these for use with AMPDU's */
6190 txrate[0]->count = 0;
6191 txrate[1]->count = 0;
6193 /* (3) PLCP: determine PLCP header and MAC duration, fill d11txh_t */
6194 wlc_compute_plcp(wlc, rspec[0], phylen, plcp);
6195 wlc_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6196 bcopy(plcp_fallback, (char *)&txh->FragPLCPFallback,
6197 sizeof(txh->FragPLCPFallback));
6199 /* Length field now put in CCK FBR CRC field */
6200 if (IS_CCK(rspec[1])) {
6201 txh->FragPLCPFallback[4] = phylen & 0xff;
6202 txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6205 /* MIMO-RATE: need validation ?? */
6206 mainrates =
6207 IS_OFDM(rspec[0]) ? D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) plcp) :
6208 plcp[0];
6210 /* DUR field for main rate */
6211 if ((fc != FC_PS_POLL) && !ETHER_ISMULTI(&h->a1) && !use_rifs) {
6212 durid =
6213 wlc_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6214 next_frag_len);
6215 h->durid = htol16(durid);
6216 } else if (use_rifs) {
6217 /* NAV protect to end of next max packet size */
6218 durid =
6219 (u16) wlc_calc_frame_time(wlc, rspec[0],
6220 preamble_type[0],
6221 DOT11_MAX_FRAG_LEN);
6222 durid += RIFS_11N_TIME;
6223 h->durid = htol16(durid);
6226 /* DUR field for fallback rate */
6227 if (fc == FC_PS_POLL)
6228 txh->FragDurFallback = h->durid;
6229 else if (ETHER_ISMULTI(&h->a1) || use_rifs)
6230 txh->FragDurFallback = 0;
6231 else {
6232 durid = wlc_compute_frame_dur(wlc, rspec[1],
6233 preamble_type[1], next_frag_len);
6234 txh->FragDurFallback = htol16(durid);
6237 /* (4) MAC-HDR: MacTxControlLow */
6238 if (frag == 0)
6239 mcl |= TXC_STARTMSDU;
6241 if (!ETHER_ISMULTI(&h->a1))
6242 mcl |= TXC_IMMEDACK;
6244 if (BAND_5G(wlc->band->bandtype))
6245 mcl |= TXC_FREQBAND_5G;
6247 if (CHSPEC_IS40(WLC_BAND_PI_RADIO_CHANSPEC))
6248 mcl |= TXC_BW_40;
6250 /* set AMIC bit if using hardware TKIP MIC */
6251 if (hwtkmic)
6252 mcl |= TXC_AMIC;
6254 txh->MacTxControlLow = htol16(mcl);
6256 /* MacTxControlHigh */
6257 mch = 0;
6259 /* Set fallback rate preamble type */
6260 if ((preamble_type[1] == WLC_SHORT_PREAMBLE) ||
6261 (preamble_type[1] == WLC_GF_PREAMBLE)) {
6262 ASSERT((preamble_type[1] == WLC_GF_PREAMBLE) ||
6263 (!IS_MCS(rspec[1])));
6264 if (RSPEC2RATE(rspec[1]) != WLC_RATE_1M)
6265 mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6268 /* MacFrameControl */
6269 bcopy((char *)&h->fc, (char *)&txh->MacFrameControl, sizeof(u16));
6271 txh->TxFesTimeNormal = htol16(0);
6273 txh->TxFesTimeFallback = htol16(0);
6275 /* TxFrameRA */
6276 bcopy((char *)&h->a1, (char *)&txh->TxFrameRA, ETHER_ADDR_LEN);
6278 /* TxFrameID */
6279 txh->TxFrameID = htol16(frameid);
6281 /* TxStatus, Note the case of recreating the first frag of a suppressed frame
6282 * then we may need to reset the retry cnt's via the status reg
6284 txh->TxStatus = htol16(status);
6286 if (D11REV_GE(wlc->pub->corerev, 16)) {
6287 /* extra fields for ucode AMPDU aggregation, the new fields are added to
6288 * the END of previous structure so that it's compatible in driver.
6289 * In old rev ucode, these fields should be ignored
6291 txh->MaxNMpdus = htol16(0);
6292 txh->MaxABytes_MRT = htol16(0);
6293 txh->MaxABytes_FBR = htol16(0);
6294 txh->MinMBytes = htol16(0);
6297 /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration, furnish d11txh_t */
6298 /* RTS PLCP header and RTS frame */
6299 if (use_rts || use_cts) {
6300 if (use_rts && use_cts)
6301 use_cts = FALSE;
6303 for (k = 0; k < 2; k++) {
6304 rts_rspec[k] = wlc_rspec_to_rts_rspec(wlc, rspec[k],
6305 FALSE,
6306 mimo_ctlchbw);
6309 if (!IS_OFDM(rts_rspec[0]) &&
6310 !((RSPEC2RATE(rts_rspec[0]) == WLC_RATE_1M) ||
6311 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6312 rts_preamble_type[0] = WLC_SHORT_PREAMBLE;
6313 mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6316 if (!IS_OFDM(rts_rspec[1]) &&
6317 !((RSPEC2RATE(rts_rspec[1]) == WLC_RATE_1M) ||
6318 (wlc->PLCPHdr_override == WLC_PLCP_LONG))) {
6319 rts_preamble_type[1] = WLC_SHORT_PREAMBLE;
6320 mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6323 /* RTS/CTS additions to MacTxControlLow */
6324 if (use_cts) {
6325 txh->MacTxControlLow |= htol16(TXC_SENDCTS);
6326 } else {
6327 txh->MacTxControlLow |= htol16(TXC_SENDRTS);
6328 txh->MacTxControlLow |= htol16(TXC_LONGFRAME);
6331 /* RTS PLCP header */
6332 ASSERT(IS_ALIGNED((uintptr) txh->RTSPhyHeader, sizeof(u16)));
6333 rts_plcp = txh->RTSPhyHeader;
6334 if (use_cts)
6335 rts_phylen = DOT11_CTS_LEN + DOT11_FCS_LEN;
6336 else
6337 rts_phylen = DOT11_RTS_LEN + DOT11_FCS_LEN;
6339 wlc_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6341 /* fallback rate version of RTS PLCP header */
6342 wlc_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6343 rts_plcp_fallback);
6344 bcopy(rts_plcp_fallback, (char *)&txh->RTSPLCPFallback,
6345 sizeof(txh->RTSPLCPFallback));
6347 /* RTS frame fields... */
6348 rts = (struct dot11_rts_frame *)&txh->rts_frame;
6350 durid = wlc_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6351 rspec[0], rts_preamble_type[0],
6352 preamble_type[0], phylen, FALSE);
6353 rts->durid = htol16(durid);
6354 /* fallback rate version of RTS DUR field */
6355 durid = wlc_compute_rtscts_dur(wlc, use_cts,
6356 rts_rspec[1], rspec[1],
6357 rts_preamble_type[1],
6358 preamble_type[1], phylen, FALSE);
6359 txh->RTSDurFallback = htol16(durid);
6361 if (use_cts) {
6362 rts->fc = htol16(FC_CTS);
6363 bcopy((char *)&h->a2, (char *)&rts->ra, ETHER_ADDR_LEN);
6364 } else {
6365 rts->fc = htol16((u16) FC_RTS);
6366 bcopy((char *)&h->a1, (char *)&rts->ra,
6367 2 * ETHER_ADDR_LEN);
6370 /* mainrate
6371 * low 8 bits: main frag rate/mcs,
6372 * high 8 bits: rts/cts rate/mcs
6374 mainrates |= (IS_OFDM(rts_rspec[0]) ?
6375 D11A_PHY_HDR_GRATE((ofdm_phy_hdr_t *) rts_plcp) :
6376 rts_plcp[0]) << 8;
6377 } else {
6378 bzero((char *)txh->RTSPhyHeader, D11_PHY_HDR_LEN);
6379 bzero((char *)&txh->rts_frame, sizeof(struct dot11_rts_frame));
6380 bzero((char *)txh->RTSPLCPFallback,
6381 sizeof(txh->RTSPLCPFallback));
6382 txh->RTSDurFallback = 0;
6385 #ifdef SUPPORT_40MHZ
6386 /* add null delimiter count */
6387 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && IS_MCS(rspec)) {
6388 txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6389 wlc_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6391 #endif
6393 /* Now that RTS/RTS FB preamble types are updated, write the final value */
6394 txh->MacTxControlHigh = htol16(mch);
6396 /* MainRates (both the rts and frag plcp rates have been calculated now) */
6397 txh->MainRates = htol16(mainrates);
6399 /* XtraFrameTypes */
6400 xfts = FRAMETYPE(rspec[1], wlc->mimoft);
6401 xfts |= (FRAMETYPE(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6402 xfts |= (FRAMETYPE(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6403 xfts |=
6404 CHSPEC_CHANNEL(WLC_BAND_PI_RADIO_CHANSPEC) << XFTS_CHANNEL_SHIFT;
6405 txh->XtraFrameTypes = htol16(xfts);
6407 /* PhyTxControlWord */
6408 phyctl = FRAMETYPE(rspec[0], wlc->mimoft);
6409 if ((preamble_type[0] == WLC_SHORT_PREAMBLE) ||
6410 (preamble_type[0] == WLC_GF_PREAMBLE)) {
6411 ASSERT((preamble_type[0] == WLC_GF_PREAMBLE)
6412 || !IS_MCS(rspec[0]));
6413 if (RSPEC2RATE(rspec[0]) != WLC_RATE_1M)
6414 phyctl |= PHY_TXC_SHORT_HDR;
6415 WLCNTINCR(wlc->pub->_cnt->txprshort);
6418 /* phytxant is properly bit shifted */
6419 phyctl |= wlc_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6420 txh->PhyTxControlWord = htol16(phyctl);
6422 /* PhyTxControlWord_1 */
6423 if (WLC_PHY_11N_CAP(wlc->band)) {
6424 u16 phyctl1 = 0;
6426 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[0]);
6427 txh->PhyTxControlWord_1 = htol16(phyctl1);
6428 phyctl1 = wlc_phytxctl1_calc(wlc, rspec[1]);
6429 txh->PhyTxControlWord_1_Fbr = htol16(phyctl1);
6431 if (use_rts || use_cts) {
6432 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[0]);
6433 txh->PhyTxControlWord_1_Rts = htol16(phyctl1);
6434 phyctl1 = wlc_phytxctl1_calc(wlc, rts_rspec[1]);
6435 txh->PhyTxControlWord_1_FbrRts = htol16(phyctl1);
6439 * For mcs frames, if mixedmode(overloaded with long preamble) is going to be set,
6440 * fill in non-zero MModeLen and/or MModeFbrLen
6441 * it will be unnecessary if they are separated
6443 if (IS_MCS(rspec[0]) && (preamble_type[0] == WLC_MM_PREAMBLE)) {
6444 u16 mmodelen =
6445 wlc_calc_lsig_len(wlc, rspec[0], phylen);
6446 txh->MModeLen = htol16(mmodelen);
6449 if (IS_MCS(rspec[1]) && (preamble_type[1] == WLC_MM_PREAMBLE)) {
6450 u16 mmodefbrlen =
6451 wlc_calc_lsig_len(wlc, rspec[1], phylen);
6452 txh->MModeFbrLen = htol16(mmodefbrlen);
6456 if (IS_MCS(rspec[0]))
6457 ASSERT(IS_MCS(rspec[1]));
6459 ASSERT(!IS_MCS(rspec[0]) ||
6460 ((preamble_type[0] == WLC_MM_PREAMBLE) == (txh->MModeLen != 0)));
6461 ASSERT(!IS_MCS(rspec[1]) ||
6462 ((preamble_type[1] == WLC_MM_PREAMBLE) ==
6463 (txh->MModeFbrLen != 0)));
6465 ac = wme_fifo2ac[queue];
6466 if (SCB_WME(scb) && qos && wlc->edcf_txop[ac]) {
6467 uint frag_dur, dur, dur_fallback;
6469 ASSERT(!ETHER_ISMULTI(&h->a1));
6471 /* WME: Update TXOP threshold */
6472 if ((!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) && (frag == 0)) {
6473 frag_dur =
6474 wlc_calc_frame_time(wlc, rspec[0], preamble_type[0],
6475 phylen);
6477 if (rts) {
6478 /* 1 RTS or CTS-to-self frame */
6479 dur =
6480 wlc_calc_cts_time(wlc, rts_rspec[0],
6481 rts_preamble_type[0]);
6482 dur_fallback =
6483 wlc_calc_cts_time(wlc, rts_rspec[1],
6484 rts_preamble_type[1]);
6485 /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
6486 dur += ltoh16(rts->durid);
6487 dur_fallback += ltoh16(txh->RTSDurFallback);
6488 } else if (use_rifs) {
6489 dur = frag_dur;
6490 dur_fallback = 0;
6491 } else {
6492 /* frame + SIFS + ACK */
6493 dur = frag_dur;
6494 dur +=
6495 wlc_compute_frame_dur(wlc, rspec[0],
6496 preamble_type[0], 0);
6498 dur_fallback =
6499 wlc_calc_frame_time(wlc, rspec[1],
6500 preamble_type[1],
6501 phylen);
6502 dur_fallback +=
6503 wlc_compute_frame_dur(wlc, rspec[1],
6504 preamble_type[1], 0);
6506 /* NEED to set TxFesTimeNormal (hard) */
6507 txh->TxFesTimeNormal = htol16((u16) dur);
6508 /* NEED to set fallback rate version of TxFesTimeNormal (hard) */
6509 txh->TxFesTimeFallback = htol16((u16) dur_fallback);
6511 /* update txop byte threshold (txop minus intraframe overhead) */
6512 if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6514 uint newfragthresh;
6516 newfragthresh =
6517 wlc_calc_frame_len(wlc, rspec[0],
6518 preamble_type[0],
6519 (wlc->
6520 edcf_txop[ac] -
6521 (dur -
6522 frag_dur)));
6523 /* range bound the fragthreshold */
6524 if (newfragthresh < DOT11_MIN_FRAG_LEN)
6525 newfragthresh =
6526 DOT11_MIN_FRAG_LEN;
6527 else if (newfragthresh >
6528 wlc->usr_fragthresh)
6529 newfragthresh =
6530 wlc->usr_fragthresh;
6531 /* update the fragthresh and do txc update */
6532 if (wlc->fragthresh[queue] !=
6533 (u16) newfragthresh) {
6534 wlc->fragthresh[queue] =
6535 (u16) newfragthresh;
6538 } else
6539 WL_ERROR(("wl%d: %s txop invalid for rate %d\n",
6540 wlc->pub->unit, fifo_names[queue],
6541 RSPEC2RATE(rspec[0])));
6543 if (dur > wlc->edcf_txop[ac])
6544 WL_ERROR(("wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n", wlc->pub->unit, __func__, fifo_names[queue], phylen, wlc->fragthresh[queue], dur, wlc->edcf_txop[ac]));
6548 return 0;
6551 void wlc_tbtt(wlc_info_t *wlc, d11regs_t *regs)
6553 wlc_bsscfg_t *cfg = wlc->cfg;
6555 WLCNTINCR(wlc->pub->_cnt->tbtt);
6557 if (BSSCFG_STA(cfg)) {
6558 /* run watchdog here if the watchdog timer is not armed */
6559 if (WLC_WATCHDOG_TBTT(wlc)) {
6560 u32 cur, delta;
6561 if (wlc->WDarmed) {
6562 wl_del_timer(wlc->wl, wlc->wdtimer);
6563 wlc->WDarmed = FALSE;
6566 cur = OSL_SYSUPTIME();
6567 delta = cur > wlc->WDlast ? cur - wlc->WDlast :
6568 (u32) ~0 - wlc->WDlast + cur + 1;
6569 if (delta >= TIMER_INTERVAL_WATCHDOG) {
6570 wlc_watchdog((void *)wlc);
6571 wlc->WDlast = cur;
6574 wl_add_timer(wlc->wl, wlc->wdtimer,
6575 wlc_watchdog_backup_bi(wlc), TRUE);
6576 wlc->WDarmed = TRUE;
6580 if (!cfg->BSS) {
6581 /* DirFrmQ is now valid...defer setting until end of ATIM window */
6582 wlc->qvalid |= MCMD_DIRFRMQVAL;
6586 /* GP timer is a freerunning 32 bit counter, decrements at 1 us rate */
6587 void wlc_hwtimer_gptimer_set(wlc_info_t *wlc, uint us)
6589 ASSERT(wlc->pub->corerev >= 3); /* no gptimer in earlier revs */
6590 W_REG(wlc->osh, &wlc->regs->gptimer, us);
6593 void wlc_hwtimer_gptimer_abort(wlc_info_t *wlc)
6595 ASSERT(wlc->pub->corerev >= 3);
6596 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6599 static void wlc_hwtimer_gptimer_cb(wlc_info_t *wlc)
6601 /* when interrupt is generated, the counter is loaded with last value
6602 * written and continue to decrement. So it has to be cleaned first
6604 W_REG(wlc->osh, &wlc->regs->gptimer, 0);
6608 * This fn has all the high level dpc processing from wlc_dpc.
6609 * POLICY: no macinstatus change, no bounding loop.
6610 * All dpc bounding should be handled in BMAC dpc, like txstatus and rxint
6612 void wlc_high_dpc(wlc_info_t *wlc, u32 macintstatus)
6614 d11regs_t *regs = wlc->regs;
6615 #ifdef BCMDBG
6616 char flagstr[128];
6617 static const bcm_bit_desc_t int_flags[] = {
6618 {MI_MACSSPNDD, "MACSSPNDD"},
6619 {MI_BCNTPL, "BCNTPL"},
6620 {MI_TBTT, "TBTT"},
6621 {MI_BCNSUCCESS, "BCNSUCCESS"},
6622 {MI_BCNCANCLD, "BCNCANCLD"},
6623 {MI_ATIMWINEND, "ATIMWINEND"},
6624 {MI_PMQ, "PMQ"},
6625 {MI_NSPECGEN_0, "NSPECGEN_0"},
6626 {MI_NSPECGEN_1, "NSPECGEN_1"},
6627 {MI_MACTXERR, "MACTXERR"},
6628 {MI_NSPECGEN_3, "NSPECGEN_3"},
6629 {MI_PHYTXERR, "PHYTXERR"},
6630 {MI_PME, "PME"},
6631 {MI_GP0, "GP0"},
6632 {MI_GP1, "GP1"},
6633 {MI_DMAINT, "DMAINT"},
6634 {MI_TXSTOP, "TXSTOP"},
6635 {MI_CCA, "CCA"},
6636 {MI_BG_NOISE, "BG_NOISE"},
6637 {MI_DTIM_TBTT, "DTIM_TBTT"},
6638 {MI_PRQ, "PRQ"},
6639 {MI_PWRUP, "PWRUP"},
6640 {MI_RFDISABLE, "RFDISABLE"},
6641 {MI_TFS, "TFS"},
6642 {MI_PHYCHANGED, "PHYCHANGED"},
6643 {MI_TO, "TO"},
6644 {0, NULL}
6647 if (macintstatus & ~(MI_TBTT | MI_TXSTOP)) {
6648 bcm_format_flags(int_flags, macintstatus, flagstr,
6649 sizeof(flagstr));
6650 WL_TRACE(("wl%d: macintstatus 0x%x %s\n", wlc->pub->unit,
6651 macintstatus, flagstr));
6653 #endif /* BCMDBG */
6655 if (macintstatus & MI_PRQ) {
6656 /* Process probe request FIFO */
6657 ASSERT(0 && "PRQ Interrupt in non-MBSS");
6660 /* TBTT indication */
6661 /* ucode only gives either TBTT or DTIM_TBTT, not both */
6662 if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
6663 wlc_tbtt(wlc, regs);
6665 if (macintstatus & MI_GP0) {
6666 WL_ERROR(("wl%d: PSM microcode watchdog fired at %d (seconds). Resetting.\n", wlc->pub->unit, wlc->pub->now));
6668 printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
6669 __func__, CHIPID(wlc->pub->sih->chip),
6670 CHIPREV(wlc->pub->sih->chiprev));
6672 WLCNTINCR(wlc->pub->_cnt->psmwds);
6674 /* big hammer */
6675 wl_init(wlc->wl);
6678 /* gptimer timeout */
6679 if (macintstatus & MI_TO) {
6680 wlc_hwtimer_gptimer_cb(wlc);
6683 if (macintstatus & MI_RFDISABLE) {
6684 WL_ERROR(("wl%d: MAC Detected a change on the RF Disable Input 0x%x\n", wlc->pub->unit, R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD));
6685 /* delay the cleanup to wl_down in IBSS case */
6686 if ((R_REG(wlc->osh, &regs->phydebug) & PDBG_RFD)) {
6687 int idx;
6688 wlc_bsscfg_t *bsscfg;
6689 FOREACH_BSS(wlc, idx, bsscfg) {
6690 if (!BSSCFG_STA(bsscfg) || !bsscfg->enable
6691 || !bsscfg->BSS)
6692 continue;
6693 WL_ERROR(("wl%d: wlc_dpc: rfdisable -> wlc_bsscfg_disable()\n", wlc->pub->unit));
6698 /* send any enq'd tx packets. Just makes sure to jump start tx */
6699 if (!pktq_empty(&wlc->active_queue->q))
6700 wlc_send_q(wlc, wlc->active_queue);
6702 #ifndef WLC_HIGH_ONLY
6703 ASSERT(wlc_ps_check(wlc));
6704 #endif
6707 static void *wlc_15420war(wlc_info_t *wlc, uint queue)
6709 hnddma_t *di;
6710 void *p;
6712 ASSERT(queue < NFIFO);
6714 if ((D11REV_IS(wlc->pub->corerev, 4))
6715 || (D11REV_GT(wlc->pub->corerev, 6)))
6716 return NULL;
6718 di = wlc->hw->di[queue];
6719 ASSERT(di != NULL);
6721 /* get next packet, ignoring XmtStatus.Curr */
6722 p = dma_getnexttxp(di, HNDDMA_RANGE_ALL);
6724 /* sw block tx dma */
6725 dma_txblock(di);
6727 /* if tx ring is now empty, reset and re-init the tx dma channel */
6728 if (dma_txactive(wlc->hw->di[queue]) == 0) {
6729 WLCNTINCR(wlc->pub->_cnt->txdmawar);
6730 if (!dma_txreset(di))
6731 WL_ERROR(("wl%d: %s: dma_txreset[%d]: cannot stop dma\n", wlc->pub->unit, __func__, queue));
6732 dma_txinit(di);
6734 return p;
6737 static void wlc_war16165(wlc_info_t *wlc, bool tx)
6739 if (tx) {
6740 /* the post-increment is used in STAY_AWAKE macro */
6741 if (wlc->txpend16165war++ == 0)
6742 wlc_set_ps_ctrl(wlc);
6743 } else {
6744 wlc->txpend16165war--;
6745 if (wlc->txpend16165war == 0)
6746 wlc_set_ps_ctrl(wlc);
6750 /* process an individual tx_status_t */
6751 /* WLC_HIGH_API */
6752 bool BCMFASTPATH
6753 wlc_dotxstatus(wlc_info_t *wlc, tx_status_t *txs, u32 frm_tx2)
6755 void *p;
6756 uint queue;
6757 d11txh_t *txh;
6758 struct scb *scb = NULL;
6759 bool free_pdu;
6760 osl_t *osh;
6761 int tx_rts, tx_frame_count, tx_rts_count;
6762 uint totlen, supr_status;
6763 bool lastframe;
6764 struct dot11_header *h;
6765 u16 fc;
6766 u16 mcl;
6767 struct ieee80211_tx_info *tx_info;
6768 struct ieee80211_tx_rate *txrate;
6769 int i;
6771 (void)(frm_tx2); /* Compiler reference to avoid unused variable warning */
6773 /* discard intermediate indications for ucode with one legitimate case:
6774 * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange, but the subsequent
6775 * tx of DATA failed. so it will start rts/cts from the beginning (resetting the rts
6776 * transmission count)
6778 if (!(txs->status & TX_STATUS_AMPDU)
6779 && (txs->status & TX_STATUS_INTERMEDIATE)) {
6780 WLCNTADD(wlc->pub->_cnt->txnoack,
6781 ((txs->
6782 status & TX_STATUS_FRM_RTX_MASK) >>
6783 TX_STATUS_FRM_RTX_SHIFT));
6784 WL_ERROR(("%s: INTERMEDIATE but not AMPDU\n", __func__));
6785 return FALSE;
6788 osh = wlc->osh;
6789 queue = txs->frameid & TXFID_QUEUE_MASK;
6790 ASSERT(queue < NFIFO);
6791 if (queue >= NFIFO) {
6792 p = NULL;
6793 goto fatal;
6796 p = GETNEXTTXP(wlc, queue);
6797 if (WLC_WAR16165(wlc))
6798 wlc_war16165(wlc, FALSE);
6799 if (p == NULL)
6800 p = wlc_15420war(wlc, queue);
6801 ASSERT(p != NULL);
6802 if (p == NULL)
6803 goto fatal;
6805 txh = (d11txh_t *) PKTDATA(p);
6806 mcl = ltoh16(txh->MacTxControlLow);
6808 if (txs->phyerr) {
6809 WL_ERROR(("phyerr 0x%x, rate 0x%x\n", txs->phyerr,
6810 txh->MainRates));
6811 wlc_print_txdesc(txh);
6812 wlc_print_txstatus(txs);
6815 ASSERT(txs->frameid == htol16(txh->TxFrameID));
6816 if (txs->frameid != htol16(txh->TxFrameID))
6817 goto fatal;
6819 tx_info = IEEE80211_SKB_CB(p);
6820 h = (struct dot11_header *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
6821 fc = ltoh16(h->fc);
6823 scb = (struct scb *)tx_info->control.sta->drv_priv;
6825 if (N_ENAB(wlc->pub)) {
6826 u8 *plcp = (u8 *) (txh + 1);
6827 if (PLCP3_ISSGI(plcp[3]))
6828 WLCNTINCR(wlc->pub->_cnt->txmpdu_sgi);
6829 if (PLCP3_ISSTBC(plcp[3]))
6830 WLCNTINCR(wlc->pub->_cnt->txmpdu_stbc);
6833 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
6834 ASSERT((mcl & TXC_AMPDU_MASK) != TXC_AMPDU_NONE);
6835 wlc_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
6836 return FALSE;
6839 supr_status = txs->status & TX_STATUS_SUPR_MASK;
6840 if (supr_status == TX_STATUS_SUPR_BADCH)
6841 WL_NONE(("%s: Pkt tx suppressed, possibly channel %d\n",
6842 __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec)));
6844 tx_rts = htol16(txh->MacTxControlLow) & TXC_SENDRTS;
6845 tx_frame_count =
6846 (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
6847 tx_rts_count =
6848 (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
6850 lastframe = (fc & FC_MOREFRAG) == 0;
6852 if (!lastframe) {
6853 WL_ERROR(("Not last frame!\n"));
6854 } else {
6855 u16 sfbl, lfbl;
6856 ieee80211_tx_info_clear_status(tx_info);
6857 if (queue < AC_COUNT) {
6858 sfbl = WLC_WME_RETRY_SFB_GET(wlc, wme_fifo2ac[queue]);
6859 lfbl = WLC_WME_RETRY_LFB_GET(wlc, wme_fifo2ac[queue]);
6860 } else {
6861 sfbl = wlc->SFBL;
6862 lfbl = wlc->LFBL;
6865 txrate = tx_info->status.rates;
6866 /* FIXME: this should use a combination of sfbl, lfbl depending on frame length and RTS setting */
6867 if ((tx_frame_count > sfbl) && (txrate[1].idx >= 0)) {
6868 /* rate selection requested a fallback rate and we used it */
6869 txrate->count = lfbl;
6870 txrate[1].count = tx_frame_count - lfbl;
6871 } else {
6872 /* rate selection did not request fallback rate, or we didn't need it */
6873 txrate->count = tx_frame_count;
6874 /* rc80211_minstrel.c:minstrel_tx_status() expects unused rates to be marked with idx = -1 */
6875 txrate[1].idx = -1;
6876 txrate[1].count = 0;
6879 /* clear the rest of the rates */
6880 for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
6881 txrate[i].idx = -1;
6882 txrate[i].count = 0;
6885 if (txs->status & TX_STATUS_ACK_RCV)
6886 tx_info->flags |= IEEE80211_TX_STAT_ACK;
6889 totlen = pkttotlen(osh, p);
6890 free_pdu = TRUE;
6892 wlc_txfifo_complete(wlc, queue, 1);
6894 if (lastframe) {
6895 PKTSETNEXT(p, NULL);
6896 PKTSETLINK(p, NULL);
6897 wlc->txretried = 0;
6898 /* remove PLCP & Broadcom tx descriptor header */
6899 PKTPULL(p, D11_PHY_HDR_LEN);
6900 PKTPULL(p, D11_TXH_LEN);
6901 ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
6902 WLCNTINCR(wlc->pub->_cnt->ieee_tx_status);
6903 } else {
6904 WL_ERROR(("%s: Not last frame => not calling tx_status\n",
6905 __func__));
6908 return FALSE;
6910 fatal:
6911 ASSERT(0);
6912 if (p)
6913 PKTFREE(osh, p, TRUE);
6915 #ifdef WLC_HIGH_ONLY
6916 /* If this is a split driver, do the big-hammer here.
6917 * If this is a monolithic driver, wlc_bmac.c:wlc_dpc() will do the big-hammer.
6919 wl_init(wlc->wl);
6920 #endif
6921 return TRUE;
6925 void BCMFASTPATH
6926 wlc_txfifo_complete(wlc_info_t *wlc, uint fifo, s8 txpktpend)
6928 TXPKTPENDDEC(wlc, fifo, txpktpend);
6929 WL_TRACE(("wlc_txfifo_complete, pktpend dec %d to %d\n", txpktpend,
6930 TXPKTPENDGET(wlc, fifo)));
6932 /* There is more room; mark precedences related to this FIFO sendable */
6933 WLC_TX_FIFO_ENAB(wlc, fifo);
6934 ASSERT(TXPKTPENDGET(wlc, fifo) >= 0);
6936 if (!TXPKTPENDTOT(wlc)) {
6937 if (wlc->block_datafifo & DATA_BLOCK_TX_SUPR)
6938 wlc_bsscfg_tx_check(wlc);
6941 /* Clear MHF2_TXBCMC_NOW flag if BCMC fifo has drained */
6942 if (AP_ENAB(wlc->pub) &&
6943 wlc->bcmcfifo_drain && !TXPKTPENDGET(wlc, TX_BCMC_FIFO)) {
6944 wlc->bcmcfifo_drain = FALSE;
6945 wlc_mhf(wlc, MHF2, MHF2_TXBCMC_NOW, 0, WLC_BAND_AUTO);
6948 /* figure out which bsscfg is being worked on... */
6951 /* Given the beacon interval in kus, and a 64 bit TSF in us,
6952 * return the offset (in us) of the TSF from the last TBTT
6954 u32 wlc_calc_tbtt_offset(u32 bp, u32 tsf_h, u32 tsf_l)
6956 u32 k, btklo, btkhi, offset;
6958 /* TBTT is always an even multiple of the beacon_interval,
6959 * so the TBTT less than or equal to the beacon timestamp is
6960 * the beacon timestamp minus the beacon timestamp modulo
6961 * the beacon interval.
6963 * TBTT = BT - (BT % BIu)
6964 * = (BTk - (BTk % BP)) * 2^10
6966 * BT = beacon timestamp (usec, 64bits)
6967 * BTk = beacon timestamp (Kusec, 54bits)
6968 * BP = beacon interval (Kusec, 16bits)
6969 * BIu = BP * 2^10 = beacon interval (usec, 26bits)
6971 * To keep the calculations in u32s, the modulo operation
6972 * on the high part of BT needs to be done in parts using the
6973 * relations:
6974 * X*Y mod Z = ((X mod Z) * (Y mod Z)) mod Z
6975 * and
6976 * (X + Y) mod Z = ((X mod Z) + (Y mod Z)) mod Z
6978 * So, if BTk[n] = u16 n [0,3] of BTk.
6979 * BTk % BP = SUM((BTk[n] * 2^16n) % BP , 0<=n<4) % BP
6980 * and the SUM term can be broken down:
6981 * (BTk[n] * 2^16n) % BP
6982 * (BTk[n] * (2^16n % BP)) % BP
6984 * Create a set of power of 2 mod BP constants:
6985 * K[n] = 2^(16n) % BP
6986 * = (K[n-1] * 2^16) % BP
6987 * K[2] = 2^32 % BP = ((2^16 % BP) * 2^16) % BP
6989 * BTk % BP = BTk[0-1] % BP +
6990 * (BTk[2] * K[2]) % BP +
6991 * (BTk[3] * K[3]) % BP
6993 * Since K[n] < 2^16 and BTk[n] is < 2^16, then BTk[n] * K[n] < 2^32
6996 /* BTk = BT >> 10, btklo = BTk[0-3], bkthi = BTk[4-6] */
6997 btklo = (tsf_h << 22) | (tsf_l >> 10);
6998 btkhi = tsf_h >> 10;
7000 /* offset = BTk % BP */
7001 offset = btklo % bp;
7003 /* K[2] = ((2^16 % BP) * 2^16) % BP */
7004 k = (u32) (1 << 16) % bp;
7005 k = (u32) (k * 1 << 16) % (u32) bp;
7007 /* offset += (BTk[2] * K[2]) % BP */
7008 offset += ((btkhi & 0xffff) * k) % bp;
7010 /* BTk[3] */
7011 btkhi = btkhi >> 16;
7013 /* k[3] = (K[2] * 2^16) % BP */
7014 k = (k << 16) % bp;
7016 /* offset += (BTk[3] * K[3]) % BP */
7017 offset += ((btkhi & 0xffff) * k) % bp;
7019 offset = offset % bp;
7021 /* convert offset from kus to us by shifting up 10 bits and
7022 * add in the low 10 bits of tsf that we ignored
7024 offset = (offset << 10) + (tsf_l & 0x3FF);
7026 return offset;
7029 /* Update beacon listen interval in shared memory */
7030 void wlc_bcn_li_upd(wlc_info_t *wlc)
7032 if (AP_ENAB(wlc->pub))
7033 return;
7035 /* wake up every DTIM is the default */
7036 if (wlc->bcn_li_dtim == 1)
7037 wlc_write_shm(wlc, M_BCN_LI, 0);
7038 else
7039 wlc_write_shm(wlc, M_BCN_LI,
7040 (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
7043 static void
7044 prep_mac80211_status(wlc_info_t *wlc, d11rxhdr_t *rxh, void *p,
7045 struct ieee80211_rx_status *rx_status)
7047 u32 tsf_l, tsf_h;
7048 wlc_d11rxhdr_t *wlc_rxh = (wlc_d11rxhdr_t *) rxh;
7049 int preamble;
7050 int channel;
7051 ratespec_t rspec;
7052 unsigned char *plcp;
7054 wlc_read_tsf(wlc, &tsf_l, &tsf_h); /* mactime */
7055 rx_status->mactime = tsf_h;
7056 rx_status->mactime <<= 32;
7057 rx_status->mactime |= tsf_l;
7058 rx_status->flag |= RX_FLAG_TSFT;
7060 channel = WLC_CHAN_CHANNEL(rxh->RxChan);
7062 /* XXX Channel/badn needs to be filtered against whether we are single/dual band card */
7063 if (channel > 14) {
7064 rx_status->band = IEEE80211_BAND_5GHZ;
7065 rx_status->freq = wf_channel2mhz(channel, WF_CHAN_FACTOR_5_G);
7066 } else {
7067 rx_status->band = IEEE80211_BAND_2GHZ;
7068 rx_status->freq = wf_channel2mhz(channel, WF_CHAN_FACTOR_2_4_G);
7071 rx_status->signal = wlc_rxh->rssi; /* signal */
7073 /* noise */
7074 /* qual */
7075 rx_status->antenna = (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0; /* ant */
7077 plcp = PKTDATA(p);
7079 rspec = wlc_compute_rspec(rxh, plcp);
7080 if (IS_MCS(rspec)) {
7081 rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
7082 rx_status->flag |= RX_FLAG_HT;
7083 if (RSPEC_IS40MHZ(rspec))
7084 rx_status->flag |= RX_FLAG_40MHZ;
7085 } else {
7086 switch (RSPEC2RATE(rspec)) {
7087 case WLC_RATE_1M:
7088 rx_status->rate_idx = 0;
7089 break;
7090 case WLC_RATE_2M:
7091 rx_status->rate_idx = 1;
7092 break;
7093 case WLC_RATE_5M5:
7094 rx_status->rate_idx = 2;
7095 break;
7096 case WLC_RATE_11M:
7097 rx_status->rate_idx = 3;
7098 break;
7099 case WLC_RATE_6M:
7100 rx_status->rate_idx = 4;
7101 break;
7102 case WLC_RATE_9M:
7103 rx_status->rate_idx = 5;
7104 break;
7105 case WLC_RATE_12M:
7106 rx_status->rate_idx = 6;
7107 break;
7108 case WLC_RATE_18M:
7109 rx_status->rate_idx = 7;
7110 break;
7111 case WLC_RATE_24M:
7112 rx_status->rate_idx = 8;
7113 break;
7114 case WLC_RATE_36M:
7115 rx_status->rate_idx = 9;
7116 break;
7117 case WLC_RATE_48M:
7118 rx_status->rate_idx = 10;
7119 break;
7120 case WLC_RATE_54M:
7121 rx_status->rate_idx = 11;
7122 break;
7123 default:
7124 WL_ERROR(("%s: Unknown rate\n", __func__));
7127 /* Determine short preamble and rate_idx */
7128 preamble = 0;
7129 if (IS_CCK(rspec)) {
7130 if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
7131 WL_ERROR(("Short CCK\n"));
7132 rx_status->flag |= RX_FLAG_SHORTPRE;
7133 } else if (IS_OFDM(rspec)) {
7134 rx_status->flag |= RX_FLAG_SHORTPRE;
7135 } else {
7136 WL_ERROR(("%s: Unknown modulation\n", __func__));
7140 if (PLCP3_ISSGI(plcp[3]))
7141 rx_status->flag |= RX_FLAG_SHORT_GI;
7143 if (rxh->RxStatus1 & RXS_DECERR) {
7144 rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
7145 WL_ERROR(("%s: RX_FLAG_FAILED_PLCP_CRC\n", __func__));
7147 if (rxh->RxStatus1 & RXS_FCSERR) {
7148 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
7149 WL_ERROR(("%s: RX_FLAG_FAILED_FCS_CRC\n", __func__));
7153 static void
7154 wlc_recvctl(wlc_info_t *wlc, osl_t *osh, d11rxhdr_t *rxh, void *p)
7156 int len_mpdu;
7157 struct ieee80211_rx_status rx_status;
7158 #if defined(BCMDBG)
7159 struct sk_buff *skb = p;
7160 #endif /* BCMDBG */
7161 /* Todo:
7162 * Cache plcp for first MPDU of AMPD and use chacched version for INTERMEDIATE.
7163 * Test for INTERMEDIATE like so:
7164 * if (!(plcp[0] | plcp[1] | plcp[2]))
7167 memset(&rx_status, 0, sizeof(rx_status));
7168 prep_mac80211_status(wlc, rxh, p, &rx_status);
7170 /* mac header+body length, exclude CRC and plcp header */
7171 len_mpdu = PKTLEN(p) - D11_PHY_HDR_LEN - DOT11_FCS_LEN;
7172 PKTPULL(p, D11_PHY_HDR_LEN);
7173 PKTSETLEN(p, len_mpdu);
7175 ASSERT(!PKTNEXT(p));
7176 ASSERT(!PKTLINK(p));
7178 ASSERT(IS_ALIGNED((uintptr) skb->data, 2));
7180 memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7181 ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7183 WLCNTINCR(wlc->pub->_cnt->ieee_rx);
7184 PKTUNALLOC(osh);
7185 return;
7188 void wlc_bss_list_free(wlc_info_t *wlc, wlc_bss_list_t *bss_list)
7190 uint index;
7191 wlc_bss_info_t *bi;
7193 if (!bss_list) {
7194 WL_ERROR(("%s: Attempting to free NULL list\n", __func__));
7195 return;
7197 /* inspect all BSS descriptor */
7198 for (index = 0; index < bss_list->count; index++) {
7199 bi = bss_list->ptrs[index];
7200 if (bi) {
7201 if (bi->bcn_prb) {
7202 osl_mfree(wlc->osh, bi->bcn_prb,
7203 bi->bcn_prb_len);
7205 osl_mfree(wlc->osh, bi, sizeof(wlc_bss_info_t));
7206 bss_list->ptrs[index] = NULL;
7209 bss_list->count = 0;
7212 /* Process received frames */
7214 * Return TRUE if more frames need to be processed. FALSE otherwise.
7215 * Param 'bound' indicates max. # frames to process before break out.
7217 /* WLC_HIGH_API */
7218 void BCMFASTPATH wlc_recv(wlc_info_t *wlc, void *p)
7220 d11rxhdr_t *rxh;
7221 struct dot11_header *h;
7222 osl_t *osh;
7223 u16 fc;
7224 uint len;
7225 bool is_amsdu;
7226 #ifdef BCMDBG
7227 char eabuf[ETHER_ADDR_STR_LEN];
7228 #endif
7230 WL_TRACE(("wl%d: wlc_recv\n", wlc->pub->unit));
7232 osh = wlc->osh;
7234 /* frame starts with rxhdr */
7235 rxh = (d11rxhdr_t *) PKTDATA(p);
7237 /* strip off rxhdr */
7238 PKTPULL(p, wlc->hwrxoff);
7240 /* fixup rx header endianness */
7241 ltoh16_buf((void *)rxh, sizeof(d11rxhdr_t));
7243 /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7244 if (rxh->RxStatus1 & RXS_PBPRES) {
7245 if (PKTLEN(p) < 2) {
7246 WLCNTINCR(wlc->pub->_cnt->rxrunt);
7247 WL_ERROR(("wl%d: wlc_recv: rcvd runt of len %d\n",
7248 wlc->pub->unit, PKTLEN(p)));
7249 goto toss;
7251 PKTPULL(p, 2);
7254 h = (struct dot11_header *)(PKTDATA(p) + D11_PHY_HDR_LEN);
7255 len = PKTLEN(p);
7257 if (rxh->RxStatus1 & RXS_FCSERR) {
7258 if (wlc->pub->mac80211_state & MAC80211_PROMISC_BCNS) {
7259 WL_ERROR(("FCSERR while scanning******* - tossing\n"));
7260 goto toss;
7261 } else {
7262 WL_ERROR(("RCSERR!!!\n"));
7263 goto toss;
7267 /* check received pkt has at least frame control field */
7268 if (len >= D11_PHY_HDR_LEN + sizeof(h->fc)) {
7269 fc = ltoh16(h->fc);
7270 } else {
7271 WLCNTINCR(wlc->pub->_cnt->rxrunt);
7272 goto toss;
7275 is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7277 /* explicitly test bad src address to avoid sending bad deauth */
7278 if (!is_amsdu) {
7279 /* CTS and ACK CTL frames are w/o a2 */
7280 if (FC_TYPE(fc) == FC_TYPE_DATA || FC_TYPE(fc) == FC_TYPE_MNG) {
7281 if ((ETHER_ISNULLADDR(&h->a2) || ETHER_ISMULTI(&h->a2))) {
7282 WL_ERROR(("wl%d: %s: dropping a frame with invalid" " src mac address, a2: %s\n", wlc->pub->unit, __func__, bcm_ether_ntoa(&h->a2, eabuf)));
7283 WLCNTINCR(wlc->pub->_cnt->rxbadsrcmac);
7284 goto toss;
7286 WLCNTINCR(wlc->pub->_cnt->rxfrag);
7290 /* due to sheer numbers, toss out probe reqs for now */
7291 if (FC_TYPE(fc) == FC_TYPE_MNG) {
7292 if ((fc & FC_KIND_MASK) == FC_PROBE_REQ)
7293 goto toss;
7296 if (is_amsdu) {
7297 WL_ERROR(("%s: is_amsdu causing toss\n", __func__));
7298 goto toss;
7301 wlc_recvctl(wlc, osh, rxh, p);
7302 return;
7304 toss:
7305 PKTFREE(osh, p, FALSE);
7308 /* calculate frame duration for Mixed-mode L-SIG spoofing, return
7309 * number of bytes goes in the length field
7311 * Formula given by HT PHY Spec v 1.13
7312 * len = 3(nsyms + nstream + 3) - 3
7314 u16 BCMFASTPATH
7315 wlc_calc_lsig_len(wlc_info_t *wlc, ratespec_t ratespec, uint mac_len)
7317 uint nsyms, len = 0, kNdps;
7319 WL_TRACE(("wl%d: wlc_calc_lsig_len: rate %d, len%d\n", wlc->pub->unit,
7320 RSPEC2RATE(ratespec), mac_len));
7322 if (IS_MCS(ratespec)) {
7323 uint mcs = ratespec & RSPEC_RATE_MASK;
7324 /* MCS_TXS(mcs) returns num tx streams - 1 */
7325 int tot_streams = (MCS_TXS(mcs) + 1) + RSPEC_STC(ratespec);
7327 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7328 /* the payload duration calculation matches that of regular ofdm */
7329 /* 1000Ndbps = kbps * 4 */
7330 kNdps =
7331 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7332 RSPEC_ISSGI(ratespec)) * 4;
7334 if (RSPEC_STC(ratespec) == 0)
7335 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7336 nsyms =
7337 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7338 APHY_TAIL_NBITS) * 1000, kNdps);
7339 else
7340 /* STBC needs to have even number of symbols */
7341 nsyms =
7343 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7344 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7346 nsyms += (tot_streams + 3); /* (+3) account for HT-SIG(2) and HT-STF(1) */
7347 /* 3 bytes/symbol @ legacy 6Mbps rate */
7348 len = (3 * nsyms) - 3; /* (-3) excluding service bits and tail bits */
7351 return (u16) len;
7354 /* calculate frame duration of a given rate and length, return time in usec unit */
7355 uint BCMFASTPATH
7356 wlc_calc_frame_time(wlc_info_t *wlc, ratespec_t ratespec, u8 preamble_type,
7357 uint mac_len)
7359 uint nsyms, dur = 0, Ndps, kNdps;
7360 uint rate = RSPEC2RATE(ratespec);
7362 if (rate == 0) {
7363 ASSERT(0);
7364 WL_ERROR(("wl%d: WAR: using rate of 1 mbps\n", wlc->pub->unit));
7365 rate = WLC_RATE_1M;
7368 WL_TRACE(("wl%d: wlc_calc_frame_time: rspec 0x%x, preamble_type %d, len%d\n", wlc->pub->unit, ratespec, preamble_type, mac_len));
7370 if (IS_MCS(ratespec)) {
7371 uint mcs = ratespec & RSPEC_RATE_MASK;
7372 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7373 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7374 ASSERT(WLC_IS_MIMO_PREAMBLE(preamble_type));
7376 dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7377 if (preamble_type == WLC_MM_PREAMBLE)
7378 dur += PREN_MM_EXT;
7379 /* 1000Ndbps = kbps * 4 */
7380 kNdps =
7381 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7382 RSPEC_ISSGI(ratespec)) * 4;
7384 if (RSPEC_STC(ratespec) == 0)
7385 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7386 nsyms =
7387 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7388 APHY_TAIL_NBITS) * 1000, kNdps);
7389 else
7390 /* STBC needs to have even number of symbols */
7391 nsyms =
7393 CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7394 APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7396 dur += APHY_SYMBOL_TIME * nsyms;
7397 if (BAND_2G(wlc->band->bandtype))
7398 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7399 } else if (IS_OFDM(rate)) {
7400 dur = APHY_PREAMBLE_TIME;
7401 dur += APHY_SIGNAL_TIME;
7402 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7403 Ndps = rate * 2;
7404 /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
7405 nsyms =
7406 CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
7407 Ndps);
7408 dur += APHY_SYMBOL_TIME * nsyms;
7409 if (BAND_2G(wlc->band->bandtype))
7410 dur += DOT11_OFDM_SIGNAL_EXTENSION;
7411 } else {
7412 /* calc # bits * 2 so factor of 2 in rate (1/2 mbps) will divide out */
7413 mac_len = mac_len * 8 * 2;
7414 /* calc ceiling of bits/rate = microseconds of air time */
7415 dur = (mac_len + rate - 1) / rate;
7416 if (preamble_type & WLC_SHORT_PREAMBLE)
7417 dur += BPHY_PLCP_SHORT_TIME;
7418 else
7419 dur += BPHY_PLCP_TIME;
7421 return dur;
7424 /* The opposite of wlc_calc_frame_time */
7425 static uint
7426 wlc_calc_frame_len(wlc_info_t *wlc, ratespec_t ratespec, u8 preamble_type,
7427 uint dur)
7429 uint nsyms, mac_len, Ndps, kNdps;
7430 uint rate = RSPEC2RATE(ratespec);
7432 WL_TRACE(("wl%d: wlc_calc_frame_len: rspec 0x%x, preamble_type %d, dur %d\n", wlc->pub->unit, ratespec, preamble_type, dur));
7434 if (IS_MCS(ratespec)) {
7435 uint mcs = ratespec & RSPEC_RATE_MASK;
7436 int tot_streams = MCS_TXS(mcs) + RSPEC_STC(ratespec);
7437 ASSERT(WLC_PHY_11N_CAP(wlc->band));
7438 dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
7439 /* payload calculation matches that of regular ofdm */
7440 if (BAND_2G(wlc->band->bandtype))
7441 dur -= DOT11_OFDM_SIGNAL_EXTENSION;
7442 /* kNdbps = kbps * 4 */
7443 kNdps =
7444 MCS_RATE(mcs, RSPEC_IS40MHZ(ratespec),
7445 RSPEC_ISSGI(ratespec)) * 4;
7446 nsyms = dur / APHY_SYMBOL_TIME;
7447 mac_len =
7448 ((nsyms * kNdps) -
7449 ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
7450 } else if (IS_OFDM(ratespec)) {
7451 dur -= APHY_PREAMBLE_TIME;
7452 dur -= APHY_SIGNAL_TIME;
7453 /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
7454 Ndps = rate * 2;
7455 nsyms = dur / APHY_SYMBOL_TIME;
7456 mac_len =
7457 ((nsyms * Ndps) -
7458 (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
7459 } else {
7460 if (preamble_type & WLC_SHORT_PREAMBLE)
7461 dur -= BPHY_PLCP_SHORT_TIME;
7462 else
7463 dur -= BPHY_PLCP_TIME;
7464 mac_len = dur * rate;
7465 /* divide out factor of 2 in rate (1/2 mbps) */
7466 mac_len = mac_len / 8 / 2;
7468 return mac_len;
7471 static uint
7472 wlc_calc_ba_time(wlc_info_t *wlc, ratespec_t rspec, u8 preamble_type)
7474 WL_TRACE(("wl%d: wlc_calc_ba_time: rspec 0x%x, preamble_type %d\n",
7475 wlc->pub->unit, rspec, preamble_type));
7476 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7477 * or equal to the rate of the immediately previous frame in the FES
7479 rspec = WLC_BASIC_RATE(wlc, rspec);
7480 ASSERT(VALID_RATE_DBG(wlc, rspec));
7482 /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
7483 return wlc_calc_frame_time(wlc, rspec, preamble_type,
7484 (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
7485 DOT11_FCS_LEN));
7488 static uint BCMFASTPATH
7489 wlc_calc_ack_time(wlc_info_t *wlc, ratespec_t rspec, u8 preamble_type)
7491 uint dur = 0;
7493 WL_TRACE(("wl%d: wlc_calc_ack_time: rspec 0x%x, preamble_type %d\n",
7494 wlc->pub->unit, rspec, preamble_type));
7495 /* Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that is less than
7496 * or equal to the rate of the immediately previous frame in the FES
7498 rspec = WLC_BASIC_RATE(wlc, rspec);
7499 ASSERT(VALID_RATE_DBG(wlc, rspec));
7501 /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
7502 dur =
7503 wlc_calc_frame_time(wlc, rspec, preamble_type,
7504 (DOT11_ACK_LEN + DOT11_FCS_LEN));
7505 return dur;
7508 static uint
7509 wlc_calc_cts_time(wlc_info_t *wlc, ratespec_t rspec, u8 preamble_type)
7511 WL_TRACE(("wl%d: wlc_calc_cts_time: ratespec 0x%x, preamble_type %d\n",
7512 wlc->pub->unit, rspec, preamble_type));
7513 return wlc_calc_ack_time(wlc, rspec, preamble_type);
7516 /* derive wlc->band->basic_rate[] table from 'rateset' */
7517 void wlc_rate_lookup_init(wlc_info_t *wlc, wlc_rateset_t *rateset)
7519 u8 rate;
7520 u8 mandatory;
7521 u8 cck_basic = 0;
7522 u8 ofdm_basic = 0;
7523 u8 *br = wlc->band->basic_rate;
7524 uint i;
7526 /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
7527 bzero(br, WLC_MAXRATE + 1);
7529 /* For each basic rate in the rates list, make an entry in the
7530 * best basic lookup.
7532 for (i = 0; i < rateset->count; i++) {
7533 /* only make an entry for a basic rate */
7534 if (!(rateset->rates[i] & WLC_RATE_FLAG))
7535 continue;
7537 /* mask off basic bit */
7538 rate = (rateset->rates[i] & RATE_MASK);
7540 if (rate > WLC_MAXRATE) {
7541 WL_ERROR(("wlc_rate_lookup_init: invalid rate 0x%X in rate set\n", rateset->rates[i]));
7542 continue;
7545 br[rate] = rate;
7548 /* The rate lookup table now has non-zero entries for each
7549 * basic rate, equal to the basic rate: br[basicN] = basicN
7551 * To look up the best basic rate corresponding to any
7552 * particular rate, code can use the basic_rate table
7553 * like this
7555 * basic_rate = wlc->band->basic_rate[tx_rate]
7557 * Make sure there is a best basic rate entry for
7558 * every rate by walking up the table from low rates
7559 * to high, filling in holes in the lookup table
7562 for (i = 0; i < wlc->band->hw_rateset.count; i++) {
7563 rate = wlc->band->hw_rateset.rates[i];
7564 ASSERT(rate <= WLC_MAXRATE);
7566 if (br[rate] != 0) {
7567 /* This rate is a basic rate.
7568 * Keep track of the best basic rate so far by
7569 * modulation type.
7571 if (IS_OFDM(rate))
7572 ofdm_basic = rate;
7573 else
7574 cck_basic = rate;
7576 continue;
7579 /* This rate is not a basic rate so figure out the
7580 * best basic rate less than this rate and fill in
7581 * the hole in the table
7584 br[rate] = IS_OFDM(rate) ? ofdm_basic : cck_basic;
7586 if (br[rate] != 0)
7587 continue;
7589 if (IS_OFDM(rate)) {
7590 /* In 11g and 11a, the OFDM mandatory rates are 6, 12, and 24 Mbps */
7591 if (rate >= WLC_RATE_24M)
7592 mandatory = WLC_RATE_24M;
7593 else if (rate >= WLC_RATE_12M)
7594 mandatory = WLC_RATE_12M;
7595 else
7596 mandatory = WLC_RATE_6M;
7597 } else {
7598 /* In 11b, all the CCK rates are mandatory 1 - 11 Mbps */
7599 mandatory = rate;
7602 br[rate] = mandatory;
7606 static void wlc_write_rate_shm(wlc_info_t *wlc, u8 rate, u8 basic_rate)
7608 u8 phy_rate, index;
7609 u8 basic_phy_rate, basic_index;
7610 u16 dir_table, basic_table;
7611 u16 basic_ptr;
7613 /* Shared memory address for the table we are reading */
7614 dir_table = IS_OFDM(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
7616 /* Shared memory address for the table we are writing */
7617 basic_table = IS_OFDM(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
7620 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
7621 * the index into the rate table.
7623 phy_rate = rate_info[rate] & RATE_MASK;
7624 basic_phy_rate = rate_info[basic_rate] & RATE_MASK;
7625 index = phy_rate & 0xf;
7626 basic_index = basic_phy_rate & 0xf;
7628 /* Find the SHM pointer to the ACK rate entry by looking in the
7629 * Direct-map Table
7631 basic_ptr = wlc_read_shm(wlc, (dir_table + basic_index * 2));
7633 /* Update the SHM BSS-basic-rate-set mapping table with the pointer
7634 * to the correct basic rate for the given incoming rate
7636 wlc_write_shm(wlc, (basic_table + index * 2), basic_ptr);
7639 static const wlc_rateset_t *wlc_rateset_get_hwrs(wlc_info_t *wlc)
7641 const wlc_rateset_t *rs_dflt;
7643 if (WLC_PHY_11N_CAP(wlc->band)) {
7644 if (BAND_5G(wlc->band->bandtype))
7645 rs_dflt = &ofdm_mimo_rates;
7646 else
7647 rs_dflt = &cck_ofdm_mimo_rates;
7648 } else if (wlc->band->gmode)
7649 rs_dflt = &cck_ofdm_rates;
7650 else
7651 rs_dflt = &cck_rates;
7653 return rs_dflt;
7656 void wlc_set_ratetable(wlc_info_t *wlc)
7658 const wlc_rateset_t *rs_dflt;
7659 wlc_rateset_t rs;
7660 u8 rate, basic_rate;
7661 uint i;
7663 rs_dflt = wlc_rateset_get_hwrs(wlc);
7664 ASSERT(rs_dflt != NULL);
7666 wlc_rateset_copy(rs_dflt, &rs);
7667 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7669 /* walk the phy rate table and update SHM basic rate lookup table */
7670 for (i = 0; i < rs.count; i++) {
7671 rate = rs.rates[i] & RATE_MASK;
7673 /* for a given rate WLC_BASIC_RATE returns the rate at
7674 * which a response ACK/CTS should be sent.
7676 basic_rate = WLC_BASIC_RATE(wlc, rate);
7677 if (basic_rate == 0) {
7678 /* This should only happen if we are using a
7679 * restricted rateset.
7681 basic_rate = rs.rates[0] & RATE_MASK;
7684 wlc_write_rate_shm(wlc, rate, basic_rate);
7689 * Return true if the specified rate is supported by the specified band.
7690 * WLC_BAND_AUTO indicates the current band.
7692 bool wlc_valid_rate(wlc_info_t *wlc, ratespec_t rspec, int band, bool verbose)
7694 wlc_rateset_t *hw_rateset;
7695 uint i;
7697 if ((band == WLC_BAND_AUTO) || (band == wlc->band->bandtype)) {
7698 hw_rateset = &wlc->band->hw_rateset;
7699 } else if (NBANDS(wlc) > 1) {
7700 hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
7701 } else {
7702 /* other band specified and we are a single band device */
7703 return FALSE;
7706 /* check if this is a mimo rate */
7707 if (IS_MCS(rspec)) {
7708 if (!VALID_MCS((rspec & RSPEC_RATE_MASK)))
7709 goto error;
7711 return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
7714 for (i = 0; i < hw_rateset->count; i++)
7715 if (hw_rateset->rates[i] == RSPEC2RATE(rspec))
7716 return TRUE;
7717 error:
7718 if (verbose) {
7719 WL_ERROR(("wl%d: wlc_valid_rate: rate spec 0x%x not in hw_rateset\n", wlc->pub->unit, rspec));
7722 return FALSE;
7725 static void wlc_update_mimo_band_bwcap(wlc_info_t *wlc, u8 bwcap)
7727 uint i;
7728 wlcband_t *band;
7730 for (i = 0; i < NBANDS(wlc); i++) {
7731 if (IS_SINGLEBAND_5G(wlc->deviceid))
7732 i = BAND_5G_INDEX;
7733 band = wlc->bandstate[i];
7734 if (band->bandtype == WLC_BAND_5G) {
7735 if ((bwcap == WLC_N_BW_40ALL)
7736 || (bwcap == WLC_N_BW_20IN2G_40IN5G))
7737 band->mimo_cap_40 = TRUE;
7738 else
7739 band->mimo_cap_40 = FALSE;
7740 } else {
7741 ASSERT(band->bandtype == WLC_BAND_2G);
7742 if (bwcap == WLC_N_BW_40ALL)
7743 band->mimo_cap_40 = TRUE;
7744 else
7745 band->mimo_cap_40 = FALSE;
7749 wlc->mimo_band_bwcap = bwcap;
7752 void wlc_mod_prb_rsp_rate_table(wlc_info_t *wlc, uint frame_len)
7754 const wlc_rateset_t *rs_dflt;
7755 wlc_rateset_t rs;
7756 u8 rate;
7757 u16 entry_ptr;
7758 u8 plcp[D11_PHY_HDR_LEN];
7759 u16 dur, sifs;
7760 uint i;
7762 sifs = SIFS(wlc->band);
7764 rs_dflt = wlc_rateset_get_hwrs(wlc);
7765 ASSERT(rs_dflt != NULL);
7767 wlc_rateset_copy(rs_dflt, &rs);
7768 wlc_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7770 /* walk the phy rate table and update MAC core SHM basic rate table entries */
7771 for (i = 0; i < rs.count; i++) {
7772 rate = rs.rates[i] & RATE_MASK;
7774 entry_ptr = wlc_rate_shm_offset(wlc, rate);
7776 /* Calculate the Probe Response PLCP for the given rate */
7777 wlc_compute_plcp(wlc, rate, frame_len, plcp);
7779 /* Calculate the duration of the Probe Response frame plus SIFS for the MAC */
7780 dur =
7781 (u16) wlc_calc_frame_time(wlc, rate, WLC_LONG_PREAMBLE,
7782 frame_len);
7783 dur += sifs;
7785 /* Update the SHM Rate Table entry Probe Response values */
7786 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS,
7787 (u16) (plcp[0] + (plcp[1] << 8)));
7788 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7789 (u16) (plcp[2] + (plcp[3] << 8)));
7790 wlc_write_shm(wlc, entry_ptr + M_RT_PRS_DUR_POS, dur);
7795 wlc_compute_bcntsfoff(wlc_info_t *wlc, ratespec_t rspec, bool short_preamble,
7796 bool phydelay)
7798 uint bcntsfoff = 0;
7800 if (IS_MCS(rspec)) {
7801 WL_ERROR(("wl%d: recd beacon with mcs rate; rspec 0x%x\n",
7802 wlc->pub->unit, rspec));
7803 } else if (IS_OFDM(rspec)) {
7804 /* tx delay from MAC through phy to air (2.1 usec) +
7805 * phy header time (preamble + PLCP SIGNAL == 20 usec) +
7806 * PLCP SERVICE + MAC header time (SERVICE + FC + DUR + A1 + A2 + A3 + SEQ == 26
7807 * bytes at beacon rate)
7809 bcntsfoff += phydelay ? D11A_PHY_TX_DELAY : 0;
7810 bcntsfoff += APHY_PREAMBLE_TIME + APHY_SIGNAL_TIME;
7811 bcntsfoff +=
7812 wlc_compute_airtime(wlc, rspec,
7813 APHY_SERVICE_NBITS / 8 +
7814 DOT11_MAC_HDR_LEN);
7815 } else {
7816 /* tx delay from MAC through phy to air (3.4 usec) +
7817 * phy header time (long preamble + PLCP == 192 usec) +
7818 * MAC header time (FC + DUR + A1 + A2 + A3 + SEQ == 24 bytes at beacon rate)
7820 bcntsfoff += phydelay ? D11B_PHY_TX_DELAY : 0;
7821 bcntsfoff +=
7822 short_preamble ? D11B_PHY_SPREHDR_TIME :
7823 D11B_PHY_LPREHDR_TIME;
7824 bcntsfoff += wlc_compute_airtime(wlc, rspec, DOT11_MAC_HDR_LEN);
7826 return (u16) (bcntsfoff);
7829 /* Max buffering needed for beacon template/prb resp template is 142 bytes.
7831 * PLCP header is 6 bytes.
7832 * 802.11 A3 header is 24 bytes.
7833 * Max beacon frame body template length is 112 bytes.
7834 * Max probe resp frame body template length is 110 bytes.
7836 * *len on input contains the max length of the packet available.
7838 * The *len value is set to the number of bytes in buf used, and starts with the PLCP
7839 * and included up to, but not including, the 4 byte FCS.
7841 static void
7842 wlc_bcn_prb_template(wlc_info_t *wlc, uint type, ratespec_t bcn_rspec,
7843 wlc_bsscfg_t *cfg, u16 *buf, int *len)
7845 cck_phy_hdr_t *plcp;
7846 struct dot11_management_header *h;
7847 int hdr_len, body_len;
7849 ASSERT(*len >= 142);
7850 ASSERT(type == FC_BEACON || type == FC_PROBE_RESP);
7852 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
7853 hdr_len = DOT11_MAC_HDR_LEN;
7854 else
7855 hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
7856 body_len = *len - hdr_len; /* calc buffer size provided for frame body */
7858 *len = hdr_len + body_len; /* return actual size */
7860 /* format PHY and MAC headers */
7861 bzero((char *)buf, hdr_len);
7863 plcp = (cck_phy_hdr_t *) buf;
7865 /* PLCP for Probe Response frames are filled in from core's rate table */
7866 if (type == FC_BEACON && !MBSS_BCN_ENAB(cfg)) {
7867 /* fill in PLCP */
7868 wlc_compute_plcp(wlc, bcn_rspec,
7869 (DOT11_MAC_HDR_LEN + body_len + DOT11_FCS_LEN),
7870 (u8 *) plcp);
7873 /* "Regular" and 16 MBSS but not for 4 MBSS */
7874 /* Update the phytxctl for the beacon based on the rspec */
7875 if (!SOFTBCN_ENAB(cfg))
7876 wlc_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
7878 if (MBSS_BCN_ENAB(cfg) && type == FC_BEACON)
7879 h = (struct dot11_management_header *)&plcp[0];
7880 else
7881 h = (struct dot11_management_header *)&plcp[1];
7883 /* fill in 802.11 header */
7884 h->fc = htol16((u16) type);
7886 /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
7887 /* A1 filled in by MAC for prb resp, broadcast for bcn */
7888 if (type == FC_BEACON)
7889 bcopy((const char *)&ether_bcast, (char *)&h->da,
7890 ETHER_ADDR_LEN);
7891 bcopy((char *)&cfg->cur_etheraddr, (char *)&h->sa, ETHER_ADDR_LEN);
7892 bcopy((char *)&cfg->BSSID, (char *)&h->bssid, ETHER_ADDR_LEN);
7894 /* SEQ filled in by MAC */
7896 return;
7899 int wlc_get_header_len()
7901 return TXOFF;
7904 /* Update a beacon for a particular BSS
7905 * For MBSS, this updates the software template and sets "latest" to the index of the
7906 * template updated.
7907 * Otherwise, it updates the hardware template.
7909 void wlc_bss_update_beacon(wlc_info_t *wlc, wlc_bsscfg_t *cfg)
7911 int len = BCN_TMPL_LEN;
7913 /* Clear the soft intmask */
7914 wlc->defmacintmask &= ~MI_BCNTPL;
7916 if (!cfg->up) { /* Only allow updates on an UP bss */
7917 return;
7920 if (MBSS_BCN_ENAB(cfg)) { /* Optimize: Some of if/else could be combined */
7921 } else if (HWBCN_ENAB(cfg)) { /* Hardware beaconing for this config */
7922 u16 bcn[BCN_TMPL_LEN / 2];
7923 u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
7924 d11regs_t *regs = wlc->regs;
7925 osl_t *osh = NULL;
7927 osh = wlc->osh;
7929 /* Check if both templates are in use, if so sched. an interrupt
7930 * that will call back into this routine
7932 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7933 /* clear any previous status */
7934 W_REG(osh, &regs->macintstatus, MI_BCNTPL);
7936 /* Check that after scheduling the interrupt both of the
7937 * templates are still busy. if not clear the int. & remask
7939 if ((R_REG(osh, &regs->maccommand) & both_valid) == both_valid) {
7940 wlc->defmacintmask |= MI_BCNTPL;
7941 return;
7944 wlc->bcn_rspec =
7945 wlc_lowest_basic_rspec(wlc, &cfg->current_bss->rateset);
7946 ASSERT(wlc_valid_rate
7947 (wlc, wlc->bcn_rspec,
7948 CHSPEC_IS2G(cfg->current_bss->
7949 chanspec) ? WLC_BAND_2G : WLC_BAND_5G,
7950 TRUE));
7952 /* update the template and ucode shm */
7953 wlc_bcn_prb_template(wlc, FC_BEACON, wlc->bcn_rspec, cfg, bcn,
7954 &len);
7955 wlc_write_hw_bcntemplates(wlc, bcn, len, FALSE);
7960 * Update all beacons for the system.
7962 void wlc_update_beacon(wlc_info_t *wlc)
7964 int idx;
7965 wlc_bsscfg_t *bsscfg;
7967 /* update AP or IBSS beacons */
7968 FOREACH_BSS(wlc, idx, bsscfg) {
7969 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7970 wlc_bss_update_beacon(wlc, bsscfg);
7974 /* Write ssid into shared memory */
7975 void wlc_shm_ssid_upd(wlc_info_t *wlc, wlc_bsscfg_t *cfg)
7977 u8 *ssidptr = cfg->SSID;
7978 u16 base = M_SSID;
7979 u8 ssidbuf[DOT11_MAX_SSID_LEN];
7981 /* padding the ssid with zero and copy it into shm */
7982 bzero(ssidbuf, DOT11_MAX_SSID_LEN);
7983 bcopy(ssidptr, ssidbuf, cfg->SSID_len);
7985 wlc_copyto_shm(wlc, base, ssidbuf, DOT11_MAX_SSID_LEN);
7987 if (!MBSS_BCN_ENAB(cfg))
7988 wlc_write_shm(wlc, M_SSIDLEN, (u16) cfg->SSID_len);
7991 void wlc_update_probe_resp(wlc_info_t *wlc, bool suspend)
7993 int idx;
7994 wlc_bsscfg_t *bsscfg;
7996 /* update AP or IBSS probe responses */
7997 FOREACH_BSS(wlc, idx, bsscfg) {
7998 if (bsscfg->up && (BSSCFG_AP(bsscfg) || !bsscfg->BSS))
7999 wlc_bss_update_probe_resp(wlc, bsscfg, suspend);
8003 void
8004 wlc_bss_update_probe_resp(wlc_info_t *wlc, wlc_bsscfg_t *cfg, bool suspend)
8006 u16 prb_resp[BCN_TMPL_LEN / 2];
8007 int len = BCN_TMPL_LEN;
8009 /* write the probe response to hardware, or save in the config structure */
8010 if (!MBSS_PRB_ENAB(cfg)) {
8012 /* create the probe response template */
8013 wlc_bcn_prb_template(wlc, FC_PROBE_RESP, 0, cfg, prb_resp,
8014 &len);
8016 if (suspend)
8017 wlc_suspend_mac_and_wait(wlc);
8019 /* write the probe response into the template region */
8020 wlc_bmac_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
8021 (len + 3) & ~3, prb_resp);
8023 /* write the length of the probe response frame (+PLCP/-FCS) */
8024 wlc_write_shm(wlc, M_PRB_RESP_FRM_LEN, (u16) len);
8026 /* write the SSID and SSID length */
8027 wlc_shm_ssid_upd(wlc, cfg);
8030 * Write PLCP headers and durations for probe response frames at all rates.
8031 * Use the actual frame length covered by the PLCP header for the call to
8032 * wlc_mod_prb_rsp_rate_table() by subtracting the PLCP len and adding the FCS.
8034 len += (-D11_PHY_HDR_LEN + DOT11_FCS_LEN);
8035 wlc_mod_prb_rsp_rate_table(wlc, (u16) len);
8037 if (suspend)
8038 wlc_enable_mac(wlc);
8039 } else { /* Generating probe resp in sw; update local template */
8040 ASSERT(0 && "No software probe response support without MBSS");
8044 /* prepares pdu for transmission. returns BCM error codes */
8045 int wlc_prep_pdu(wlc_info_t *wlc, void *pdu, uint *fifop)
8047 osl_t *osh;
8048 uint fifo;
8049 d11txh_t *txh;
8050 struct dot11_header *h;
8051 struct scb *scb;
8052 u16 fc;
8054 osh = wlc->osh;
8056 ASSERT(pdu);
8057 txh = (d11txh_t *) PKTDATA(pdu);
8058 ASSERT(txh);
8059 h = (struct dot11_header *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
8060 ASSERT(h);
8061 fc = ltoh16(h->fc);
8063 /* get the pkt queue info. This was put at wlc_sendctl or wlc_send for PDU */
8064 fifo = ltoh16(txh->TxFrameID) & TXFID_QUEUE_MASK;
8066 scb = NULL;
8068 *fifop = fifo;
8070 /* return if insufficient dma resources */
8071 if (TXAVAIL(wlc, fifo) < MAX_DMA_SEGS) {
8072 /* Mark precedences related to this FIFO, unsendable */
8073 WLC_TX_FIFO_CLEAR(wlc, fifo);
8074 return BCME_BUSY;
8077 if (FC_TYPE(ltoh16(txh->MacFrameControl)) != FC_TYPE_DATA)
8078 WLCNTINCR(wlc->pub->_cnt->txctl);
8080 return 0;
8083 /* init tx reported rate mechanism */
8084 void wlc_reprate_init(wlc_info_t *wlc)
8086 int i;
8087 wlc_bsscfg_t *bsscfg;
8089 FOREACH_BSS(wlc, i, bsscfg) {
8090 wlc_bsscfg_reprate_init(bsscfg);
8094 /* per bsscfg init tx reported rate mechanism */
8095 void wlc_bsscfg_reprate_init(wlc_bsscfg_t *bsscfg)
8097 bsscfg->txrspecidx = 0;
8098 bzero((char *)bsscfg->txrspec, sizeof(bsscfg->txrspec));
8101 /* Retrieve a consolidated set of revision information,
8102 * typically for the WLC_GET_REVINFO ioctl
8104 int wlc_get_revision_info(wlc_info_t *wlc, void *buf, uint len)
8106 wlc_rev_info_t *rinfo = (wlc_rev_info_t *) buf;
8108 if (len < WL_REV_INFO_LEGACY_LENGTH)
8109 return BCME_BUFTOOSHORT;
8111 rinfo->vendorid = wlc->vendorid;
8112 rinfo->deviceid = wlc->deviceid;
8113 rinfo->radiorev = (wlc->band->radiorev << IDCODE_REV_SHIFT) |
8114 (wlc->band->radioid << IDCODE_ID_SHIFT);
8115 rinfo->chiprev = wlc->pub->sih->chiprev;
8116 rinfo->corerev = wlc->pub->corerev;
8117 rinfo->boardid = wlc->pub->sih->boardtype;
8118 rinfo->boardvendor = wlc->pub->sih->boardvendor;
8119 rinfo->boardrev = wlc->pub->boardrev;
8120 rinfo->ucoderev = wlc->ucode_rev;
8121 rinfo->driverrev = EPI_VERSION_NUM;
8122 rinfo->bus = wlc->pub->sih->bustype;
8123 rinfo->chipnum = wlc->pub->sih->chip;
8125 if (len >= (offsetof(wlc_rev_info_t, chippkg))) {
8126 rinfo->phytype = wlc->band->phytype;
8127 rinfo->phyrev = wlc->band->phyrev;
8128 rinfo->anarev = 0; /* obsolete stuff, suppress */
8131 if (len >= sizeof(*rinfo)) {
8132 rinfo->chippkg = wlc->pub->sih->chippkg;
8135 return BCME_OK;
8138 void wlc_default_rateset(wlc_info_t *wlc, wlc_rateset_t *rs)
8140 wlc_rateset_default(rs, NULL, wlc->band->phytype, wlc->band->bandtype,
8141 FALSE, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
8142 CHSPEC_WLC_BW(wlc->default_bss->chanspec),
8143 wlc->stf->txstreams);
8146 static void wlc_bss_default_init(wlc_info_t *wlc)
8148 chanspec_t chanspec;
8149 wlcband_t *band;
8150 wlc_bss_info_t *bi = wlc->default_bss;
8152 /* init default and target BSS with some sane initial values */
8153 bzero((char *)(bi), sizeof(wlc_bss_info_t));
8154 bi->beacon_period = ISSIM_ENAB(wlc->pub->sih) ? BEACON_INTERVAL_DEF_QT :
8155 BEACON_INTERVAL_DEFAULT;
8156 bi->dtim_period = ISSIM_ENAB(wlc->pub->sih) ? DTIM_INTERVAL_DEF_QT :
8157 DTIM_INTERVAL_DEFAULT;
8159 /* fill the default channel as the first valid channel
8160 * starting from the 2G channels
8162 chanspec = CH20MHZ_CHSPEC(1);
8163 ASSERT(chanspec != INVCHANSPEC);
8165 wlc->home_chanspec = bi->chanspec = chanspec;
8167 /* find the band of our default channel */
8168 band = wlc->band;
8169 if (NBANDS(wlc) > 1 && band->bandunit != CHSPEC_WLCBANDUNIT(chanspec))
8170 band = wlc->bandstate[OTHERBANDUNIT(wlc)];
8172 /* init bss rates to the band specific default rate set */
8173 wlc_rateset_default(&bi->rateset, NULL, band->phytype, band->bandtype,
8174 FALSE, RATE_MASK_FULL, (bool) N_ENAB(wlc->pub),
8175 CHSPEC_WLC_BW(chanspec), wlc->stf->txstreams);
8177 if (N_ENAB(wlc->pub))
8178 bi->flags |= WLC_BSS_HT;
8181 /* Deferred event processing */
8182 static void wlc_process_eventq(void *arg)
8184 wlc_info_t *wlc = (wlc_info_t *) arg;
8185 wlc_event_t *etmp;
8187 while ((etmp = wlc_eventq_deq(wlc->eventq))) {
8188 /* Perform OS specific event processing */
8189 wl_event(wlc->wl, etmp->event.ifname, etmp);
8190 if (etmp->data) {
8191 osl_mfree(wlc->osh, etmp->data, etmp->event.datalen);
8192 etmp->data = NULL;
8194 wlc_event_free(wlc->eventq, etmp);
8198 void
8199 wlc_uint64_sub(u32 *a_high, u32 *a_low, u32 b_high, u32 b_low)
8201 if (b_low > *a_low) {
8202 /* low half needs a carry */
8203 b_high += 1;
8205 *a_low -= b_low;
8206 *a_high -= b_high;
8209 static ratespec_t
8210 mac80211_wlc_set_nrate(wlc_info_t *wlc, wlcband_t *cur_band, u32 int_val)
8212 u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
8213 u8 rate = int_val & NRATE_RATE_MASK;
8214 ratespec_t rspec;
8215 bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
8216 bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
8217 bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
8218 == NRATE_OVERRIDE_MCS_ONLY);
8219 int bcmerror = 0;
8221 if (!ismcs) {
8222 return (ratespec_t) rate;
8225 /* validate the combination of rate/mcs/stf is allowed */
8226 if (N_ENAB(wlc->pub) && ismcs) {
8227 /* mcs only allowed when nmode */
8228 if (stf > PHY_TXC1_MODE_SDM) {
8229 WL_ERROR(("wl%d: %s: Invalid stf\n", WLCWLUNIT(wlc),
8230 __func__));
8231 bcmerror = BCME_RANGE;
8232 goto done;
8235 /* mcs 32 is a special case, DUP mode 40 only */
8236 if (rate == 32) {
8237 if (!CHSPEC_IS40(wlc->home_chanspec) ||
8238 ((stf != PHY_TXC1_MODE_SISO)
8239 && (stf != PHY_TXC1_MODE_CDD))) {
8240 WL_ERROR(("wl%d: %s: Invalid mcs 32\n",
8241 WLCWLUNIT(wlc), __func__));
8242 bcmerror = BCME_RANGE;
8243 goto done;
8245 /* mcs > 7 must use stf SDM */
8246 } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
8247 /* mcs > 7 must use stf SDM */
8248 if (stf != PHY_TXC1_MODE_SDM) {
8249 WL_TRACE(("wl%d: %s: enabling SDM mode for mcs %d\n", WLCWLUNIT(wlc), __func__, rate));
8250 stf = PHY_TXC1_MODE_SDM;
8252 } else {
8253 /* MCS 0-7 may use SISO, CDD, and for phy_rev >= 3 STBC */
8254 if ((stf > PHY_TXC1_MODE_STBC) ||
8255 (!WLC_STBC_CAP_PHY(wlc)
8256 && (stf == PHY_TXC1_MODE_STBC))) {
8257 WL_ERROR(("wl%d: %s: Invalid STBC\n",
8258 WLCWLUNIT(wlc), __func__));
8259 bcmerror = BCME_RANGE;
8260 goto done;
8263 } else if (IS_OFDM(rate)) {
8264 if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
8265 WL_ERROR(("wl%d: %s: Invalid OFDM\n", WLCWLUNIT(wlc),
8266 __func__));
8267 bcmerror = BCME_RANGE;
8268 goto done;
8270 } else if (IS_CCK(rate)) {
8271 if ((cur_band->bandtype != WLC_BAND_2G)
8272 || (stf != PHY_TXC1_MODE_SISO)) {
8273 WL_ERROR(("wl%d: %s: Invalid CCK\n", WLCWLUNIT(wlc),
8274 __func__));
8275 bcmerror = BCME_RANGE;
8276 goto done;
8278 } else {
8279 WL_ERROR(("wl%d: %s: Unknown rate type\n", WLCWLUNIT(wlc),
8280 __func__));
8281 bcmerror = BCME_RANGE;
8282 goto done;
8284 /* make sure multiple antennae are available for non-siso rates */
8285 if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
8286 WL_ERROR(("wl%d: %s: SISO antenna but !SISO request\n",
8287 WLCWLUNIT(wlc), __func__));
8288 bcmerror = BCME_RANGE;
8289 goto done;
8292 rspec = rate;
8293 if (ismcs) {
8294 rspec |= RSPEC_MIMORATE;
8295 /* For STBC populate the STC field of the ratespec */
8296 if (stf == PHY_TXC1_MODE_STBC) {
8297 u8 stc;
8298 stc = 1; /* Nss for single stream is always 1 */
8299 rspec |= (stc << RSPEC_STC_SHIFT);
8303 rspec |= (stf << RSPEC_STF_SHIFT);
8305 if (override_mcs_only)
8306 rspec |= RSPEC_OVERRIDE_MCS_ONLY;
8308 if (issgi)
8309 rspec |= RSPEC_SHORT_GI;
8311 if ((rate != 0)
8312 && !wlc_valid_rate(wlc, rspec, cur_band->bandtype, TRUE)) {
8313 return rate;
8316 return rspec;
8317 done:
8318 WL_ERROR(("Hoark\n"));
8319 return rate;
8322 /* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
8323 static int
8324 wlc_duty_cycle_set(wlc_info_t *wlc, int duty_cycle, bool isOFDM,
8325 bool writeToShm)
8327 int idle_busy_ratio_x_16 = 0;
8328 uint offset =
8329 isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
8330 M_TX_IDLE_BUSY_RATIO_X_16_CCK;
8331 if (duty_cycle > 100 || duty_cycle < 0) {
8332 WL_ERROR(("wl%d: duty cycle value off limit\n",
8333 wlc->pub->unit));
8334 return BCME_RANGE;
8336 if (duty_cycle)
8337 idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
8338 /* Only write to shared memory when wl is up */
8339 if (writeToShm)
8340 wlc_write_shm(wlc, offset, (u16) idle_busy_ratio_x_16);
8342 if (isOFDM)
8343 wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
8344 else
8345 wlc->tx_duty_cycle_cck = (u16) duty_cycle;
8347 return BCME_OK;
8350 /* Read a single u16 from shared memory.
8351 * SHM 'offset' needs to be an even address
8353 u16 wlc_read_shm(wlc_info_t *wlc, uint offset)
8355 return wlc_bmac_read_shm(wlc->hw, offset);
8358 /* Write a single u16 to shared memory.
8359 * SHM 'offset' needs to be an even address
8361 void wlc_write_shm(wlc_info_t *wlc, uint offset, u16 v)
8363 wlc_bmac_write_shm(wlc->hw, offset, v);
8366 /* Set a range of shared memory to a value.
8367 * SHM 'offset' needs to be an even address and
8368 * Range length 'len' must be an even number of bytes
8370 void wlc_set_shm(wlc_info_t *wlc, uint offset, u16 v, int len)
8372 /* offset and len need to be even */
8373 ASSERT((offset & 1) == 0);
8374 ASSERT((len & 1) == 0);
8376 if (len <= 0)
8377 return;
8379 wlc_bmac_set_shm(wlc->hw, offset, v, len);
8382 /* Copy a buffer to shared memory.
8383 * SHM 'offset' needs to be an even address and
8384 * Buffer length 'len' must be an even number of bytes
8386 void wlc_copyto_shm(wlc_info_t *wlc, uint offset, const void *buf, int len)
8388 /* offset and len need to be even */
8389 ASSERT((offset & 1) == 0);
8390 ASSERT((len & 1) == 0);
8392 if (len <= 0)
8393 return;
8394 wlc_bmac_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8398 /* Copy from shared memory to a buffer.
8399 * SHM 'offset' needs to be an even address and
8400 * Buffer length 'len' must be an even number of bytes
8402 void wlc_copyfrom_shm(wlc_info_t *wlc, uint offset, void *buf, int len)
8404 /* offset and len need to be even */
8405 ASSERT((offset & 1) == 0);
8406 ASSERT((len & 1) == 0);
8408 if (len <= 0)
8409 return;
8411 wlc_bmac_copyfrom_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
8414 /* wrapper BMAC functions to for HIGH driver access */
8415 void wlc_mctrl(wlc_info_t *wlc, u32 mask, u32 val)
8417 wlc_bmac_mctrl(wlc->hw, mask, val);
8420 void wlc_corereset(wlc_info_t *wlc, u32 flags)
8422 wlc_bmac_corereset(wlc->hw, flags);
8425 void wlc_mhf(wlc_info_t *wlc, u8 idx, u16 mask, u16 val, int bands)
8427 wlc_bmac_mhf(wlc->hw, idx, mask, val, bands);
8430 u16 wlc_mhf_get(wlc_info_t *wlc, u8 idx, int bands)
8432 return wlc_bmac_mhf_get(wlc->hw, idx, bands);
8435 int wlc_xmtfifo_sz_get(wlc_info_t *wlc, uint fifo, uint *blocks)
8437 return wlc_bmac_xmtfifo_sz_get(wlc->hw, fifo, blocks);
8440 void wlc_write_template_ram(wlc_info_t *wlc, int offset, int len, void *buf)
8442 wlc_bmac_write_template_ram(wlc->hw, offset, len, buf);
8445 void wlc_write_hw_bcntemplates(wlc_info_t *wlc, void *bcn, int len, bool both)
8447 wlc_bmac_write_hw_bcntemplates(wlc->hw, bcn, len, both);
8450 void
8451 wlc_set_addrmatch(wlc_info_t *wlc, int match_reg_offset,
8452 const struct ether_addr *addr)
8454 wlc_bmac_set_addrmatch(wlc->hw, match_reg_offset, addr);
8457 void wlc_set_rcmta(wlc_info_t *wlc, int idx, const struct ether_addr *addr)
8459 wlc_bmac_set_rcmta(wlc->hw, idx, addr);
8462 void wlc_read_tsf(wlc_info_t *wlc, u32 *tsf_l_ptr, u32 *tsf_h_ptr)
8464 wlc_bmac_read_tsf(wlc->hw, tsf_l_ptr, tsf_h_ptr);
8467 void wlc_set_cwmin(wlc_info_t *wlc, u16 newmin)
8469 wlc->band->CWmin = newmin;
8470 wlc_bmac_set_cwmin(wlc->hw, newmin);
8473 void wlc_set_cwmax(wlc_info_t *wlc, u16 newmax)
8475 wlc->band->CWmax = newmax;
8476 wlc_bmac_set_cwmax(wlc->hw, newmax);
8479 void wlc_fifoerrors(wlc_info_t *wlc)
8482 wlc_bmac_fifoerrors(wlc->hw);
8485 /* Search mem rw utilities */
8487 void wlc_pllreq(wlc_info_t *wlc, bool set, mbool req_bit)
8489 wlc_bmac_pllreq(wlc->hw, set, req_bit);
8492 void wlc_reset_bmac_done(wlc_info_t *wlc)
8494 #ifdef WLC_HIGH_ONLY
8495 wlc->reset_bmac_pending = FALSE;
8496 #endif
8499 void wlc_ht_mimops_cap_update(wlc_info_t *wlc, u8 mimops_mode)
8501 wlc->ht_cap.cap &= ~HT_CAP_MIMO_PS_MASK;
8502 wlc->ht_cap.cap |= (mimops_mode << HT_CAP_MIMO_PS_SHIFT);
8504 if (AP_ENAB(wlc->pub) && wlc->clk) {
8505 wlc_update_beacon(wlc);
8506 wlc_update_probe_resp(wlc, TRUE);
8510 /* check for the particular priority flow control bit being set */
8511 bool
8512 wlc_txflowcontrol_prio_isset(wlc_info_t *wlc, wlc_txq_info_t *q, int prio)
8514 uint prio_mask;
8516 if (prio == ALLPRIO) {
8517 prio_mask = TXQ_STOP_FOR_PRIOFC_MASK;
8518 } else {
8519 ASSERT(prio >= 0 && prio <= MAXPRIO);
8520 prio_mask = NBITVAL(prio);
8523 return (q->stopped & prio_mask) == prio_mask;
8526 /* propogate the flow control to all interfaces using the given tx queue */
8527 void wlc_txflowcontrol(wlc_info_t *wlc, wlc_txq_info_t *qi, bool on, int prio)
8529 uint prio_bits;
8530 uint cur_bits;
8532 WL_ERROR(("%s: flow contro kicks in\n", __func__));
8534 if (prio == ALLPRIO) {
8535 prio_bits = TXQ_STOP_FOR_PRIOFC_MASK;
8536 } else {
8537 ASSERT(prio >= 0 && prio <= MAXPRIO);
8538 prio_bits = NBITVAL(prio);
8541 cur_bits = qi->stopped & prio_bits;
8543 /* Check for the case of no change and return early
8544 * Otherwise update the bit and continue
8546 if (on) {
8547 if (cur_bits == prio_bits) {
8548 return;
8550 mboolset(qi->stopped, prio_bits);
8551 } else {
8552 if (cur_bits == 0) {
8553 return;
8555 mboolclr(qi->stopped, prio_bits);
8558 /* If there is a flow control override we will not change the external
8559 * flow control state.
8561 if (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK) {
8562 return;
8565 wlc_txflowcontrol_signal(wlc, qi, on, prio);
8568 void
8569 wlc_txflowcontrol_override(wlc_info_t *wlc, wlc_txq_info_t *qi, bool on,
8570 uint override)
8572 uint prev_override;
8574 ASSERT(override != 0);
8575 ASSERT((override & TXQ_STOP_FOR_PRIOFC_MASK) == 0);
8577 prev_override = (qi->stopped & ~TXQ_STOP_FOR_PRIOFC_MASK);
8579 /* Update the flow control bits and do an early return if there is
8580 * no change in the external flow control state.
8582 if (on) {
8583 mboolset(qi->stopped, override);
8584 /* if there was a previous override bit on, then setting this
8585 * makes no difference.
8587 if (prev_override) {
8588 return;
8591 wlc_txflowcontrol_signal(wlc, qi, ON, ALLPRIO);
8592 } else {
8593 mboolclr(qi->stopped, override);
8594 /* clearing an override bit will only make a difference for
8595 * flow control if it was the only bit set. For any other
8596 * override setting, just return
8598 if (prev_override != override) {
8599 return;
8602 if (qi->stopped == 0) {
8603 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8604 } else {
8605 int prio;
8607 for (prio = MAXPRIO; prio >= 0; prio--) {
8608 if (!mboolisset(qi->stopped, NBITVAL(prio)))
8609 wlc_txflowcontrol_signal(wlc, qi, OFF,
8610 prio);
8616 static void wlc_txflowcontrol_reset(wlc_info_t *wlc)
8618 wlc_txq_info_t *qi;
8620 for (qi = wlc->tx_queues; qi != NULL; qi = qi->next) {
8621 if (qi->stopped) {
8622 wlc_txflowcontrol_signal(wlc, qi, OFF, ALLPRIO);
8623 qi->stopped = 0;
8628 static void
8629 wlc_txflowcontrol_signal(wlc_info_t *wlc, wlc_txq_info_t *qi, bool on,
8630 int prio)
8632 wlc_if_t *wlcif;
8634 for (wlcif = wlc->wlcif_list; wlcif != NULL; wlcif = wlcif->next) {
8635 if (wlcif->qi == qi && wlcif->flags & WLC_IF_LINKED)
8636 wl_txflowcontrol(wlc->wl, wlcif->wlif, on, prio);
8640 static wlc_txq_info_t *wlc_txq_alloc(wlc_info_t *wlc, osl_t *osh)
8642 wlc_txq_info_t *qi, *p;
8644 qi = (wlc_txq_info_t *) wlc_calloc(osh, wlc->pub->unit,
8645 sizeof(wlc_txq_info_t));
8646 if (qi == NULL) {
8647 return NULL;
8650 /* Have enough room for control packets along with HI watermark */
8651 /* Also, add room to txq for total psq packets if all the SCBs leave PS mode */
8652 /* The watermark for flowcontrol to OS packets will remain the same */
8653 pktq_init(&qi->q, WLC_PREC_COUNT,
8654 (2 * wlc->pub->tunables->datahiwat) + PKTQ_LEN_DEFAULT +
8655 wlc->pub->psq_pkts_total);
8657 /* add this queue to the the global list */
8658 p = wlc->tx_queues;
8659 if (p == NULL) {
8660 wlc->tx_queues = qi;
8661 } else {
8662 while (p->next != NULL)
8663 p = p->next;
8664 p->next = qi;
8667 return qi;
8670 static void wlc_txq_free(wlc_info_t *wlc, osl_t *osh, wlc_txq_info_t *qi)
8672 wlc_txq_info_t *p;
8674 if (qi == NULL)
8675 return;
8677 /* remove the queue from the linked list */
8678 p = wlc->tx_queues;
8679 if (p == qi)
8680 wlc->tx_queues = p->next;
8681 else {
8682 while (p != NULL && p->next != qi)
8683 p = p->next;
8684 ASSERT(p->next == qi);
8685 if (p != NULL)
8686 p->next = p->next->next;
8689 osl_mfree(osh, qi, sizeof(wlc_txq_info_t));