xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmsmac/main.c (revision 2fd192668620d0b367561c66df5dfe31de958dce)
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/pci_ids.h>
21 #include <linux/if_ether.h>
22 #include <net/cfg80211.h>
23 #include <net/mac80211.h>
24 #include <brcm_hw_ids.h>
25 #include <aiutils.h>
26 #include <chipcommon.h>
27 #include "rate.h"
28 #include "scb.h"
29 #include "phy/phy_hal.h"
30 #include "channel.h"
31 #include "antsel.h"
32 #include "stf.h"
33 #include "ampdu.h"
34 #include "mac80211_if.h"
35 #include "ucode_loader.h"
36 #include "main.h"
37 #include "soc.h"
38 #include "dma.h"
39 #include "debug.h"
40 #include "brcms_trace_events.h"
41 
42 /* watchdog timer, in unit of ms */
43 #define TIMER_INTERVAL_WATCHDOG		1000
44 /* radio monitor timer, in unit of ms */
45 #define TIMER_INTERVAL_RADIOCHK		800
46 
47 /* beacon interval, in unit of 1024TU */
48 #define BEACON_INTERVAL_DEFAULT		100
49 
50 /* n-mode support capability */
51 /* 2x2 includes both 1x1 & 2x2 devices
52  * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
53  * control it independently
54  */
55 #define WL_11N_2x2			1
56 #define WL_11N_3x3			3
57 #define WL_11N_4x4			4
58 
59 #define EDCF_ACI_MASK			0x60
60 #define EDCF_ACI_SHIFT			5
61 #define EDCF_ECWMIN_MASK		0x0f
62 #define EDCF_ECWMAX_SHIFT		4
63 #define EDCF_AIFSN_MASK			0x0f
64 #define EDCF_AIFSN_MAX			15
65 #define EDCF_ECWMAX_MASK		0xf0
66 
67 #define EDCF_AC_BE_TXOP_STA		0x0000
68 #define EDCF_AC_BK_TXOP_STA		0x0000
69 #define EDCF_AC_VO_ACI_STA		0x62
70 #define EDCF_AC_VO_ECW_STA		0x32
71 #define EDCF_AC_VI_ACI_STA		0x42
72 #define EDCF_AC_VI_ECW_STA		0x43
73 #define EDCF_AC_BK_ECW_STA		0xA4
74 #define EDCF_AC_VI_TXOP_STA		0x005e
75 #define EDCF_AC_VO_TXOP_STA		0x002f
76 #define EDCF_AC_BE_ACI_STA		0x03
77 #define EDCF_AC_BE_ECW_STA		0xA4
78 #define EDCF_AC_BK_ACI_STA		0x27
79 #define EDCF_AC_VO_TXOP_AP		0x002f
80 
81 #define EDCF_TXOP2USEC(txop)		((txop) << 5)
82 #define EDCF_ECW2CW(exp)		((1 << (exp)) - 1)
83 
84 #define APHY_SYMBOL_TIME		4
85 #define APHY_PREAMBLE_TIME		16
86 #define APHY_SIGNAL_TIME		4
87 #define APHY_SIFS_TIME			16
88 #define APHY_SERVICE_NBITS		16
89 #define APHY_TAIL_NBITS			6
90 #define BPHY_SIFS_TIME			10
91 #define BPHY_PLCP_SHORT_TIME		96
92 
93 #define PREN_PREAMBLE			24
94 #define PREN_MM_EXT			12
95 #define PREN_PREAMBLE_EXT		4
96 
97 #define DOT11_MAC_HDR_LEN		24
98 #define DOT11_ACK_LEN			10
99 #define DOT11_BA_LEN			4
100 #define DOT11_OFDM_SIGNAL_EXTENSION	6
101 #define DOT11_MIN_FRAG_LEN		256
102 #define DOT11_RTS_LEN			16
103 #define DOT11_CTS_LEN			10
104 #define DOT11_BA_BITMAP_LEN		128
105 #define DOT11_MAXNUMFRAGS		16
106 #define DOT11_MAX_FRAG_LEN		2346
107 
108 #define BPHY_PLCP_TIME			192
109 #define RIFS_11N_TIME			2
110 
111 /* length of the BCN template area */
112 #define BCN_TMPL_LEN			512
113 
114 /* brcms_bss_info flag bit values */
115 #define BRCMS_BSS_HT			0x0020	/* BSS is HT (MIMO) capable */
116 
117 /* chip rx buffer offset */
118 #define BRCMS_HWRXOFF			38
119 
120 /* rfdisable delay timer 500 ms, runs of ALP clock */
121 #define RFDISABLE_DEFAULT		10000000
122 
123 #define BRCMS_TEMPSENSE_PERIOD		10	/* 10 second timeout */
124 
125 /* synthpu_dly times in us */
126 #define SYNTHPU_DLY_APHY_US		3700
127 #define SYNTHPU_DLY_BPHY_US		1050
128 #define SYNTHPU_DLY_NPHY_US		2048
129 #define SYNTHPU_DLY_LPPHY_US		300
130 
131 #define ANTCNT				10	/* vanilla M_MAX_ANTCNT val */
132 
133 /* Per-AC retry limit register definitions; uses defs.h bitfield macros */
134 #define EDCF_SHORT_S			0
135 #define EDCF_SFB_S			4
136 #define EDCF_LONG_S			8
137 #define EDCF_LFB_S			12
138 #define EDCF_SHORT_M			BITFIELD_MASK(4)
139 #define EDCF_SFB_M			BITFIELD_MASK(4)
140 #define EDCF_LONG_M			BITFIELD_MASK(4)
141 #define EDCF_LFB_M			BITFIELD_MASK(4)
142 
143 #define RETRY_SHORT_DEF			7	/* Default Short retry Limit */
144 #define RETRY_SHORT_MAX			255	/* Maximum Short retry Limit */
145 #define RETRY_LONG_DEF			4	/* Default Long retry count */
146 #define RETRY_SHORT_FB			3	/* Short count for fb rate */
147 #define RETRY_LONG_FB			2	/* Long count for fb rate */
148 
149 #define APHY_CWMIN			15
150 #define PHY_CWMAX			1023
151 
152 #define EDCF_AIFSN_MIN			1
153 
154 #define FRAGNUM_MASK			0xF
155 
156 #define APHY_SLOT_TIME			9
157 #define BPHY_SLOT_TIME			20
158 
159 #define WL_SPURAVOID_OFF		0
160 #define WL_SPURAVOID_ON1		1
161 #define WL_SPURAVOID_ON2		2
162 
163 /* invalid core flags, use the saved coreflags */
164 #define BRCMS_USE_COREFLAGS		0xffffffff
165 
166 /* values for PLCPHdr_override */
167 #define BRCMS_PLCP_AUTO			-1
168 #define BRCMS_PLCP_SHORT		0
169 #define BRCMS_PLCP_LONG			1
170 
171 /* values for g_protection_override and n_protection_override */
172 #define BRCMS_PROTECTION_AUTO		-1
173 #define BRCMS_PROTECTION_OFF		0
174 #define BRCMS_PROTECTION_ON		1
175 #define BRCMS_PROTECTION_MMHDR_ONLY	2
176 #define BRCMS_PROTECTION_CTS_ONLY	3
177 
178 /* values for g_protection_control and n_protection_control */
179 #define BRCMS_PROTECTION_CTL_OFF	0
180 #define BRCMS_PROTECTION_CTL_LOCAL	1
181 #define BRCMS_PROTECTION_CTL_OVERLAP	2
182 
183 /* values for n_protection */
184 #define BRCMS_N_PROTECTION_OFF		0
185 #define BRCMS_N_PROTECTION_OPTIONAL	1
186 #define BRCMS_N_PROTECTION_20IN40	2
187 #define BRCMS_N_PROTECTION_MIXEDMODE	3
188 
189 /* values for band specific 40MHz capabilities */
190 #define BRCMS_N_BW_20ALL		0
191 #define BRCMS_N_BW_40ALL		1
192 #define BRCMS_N_BW_20IN2G_40IN5G	2
193 
194 /* bitflags for SGI support (sgi_rx iovar) */
195 #define BRCMS_N_SGI_20			0x01
196 #define BRCMS_N_SGI_40			0x02
197 
198 /* defines used by the nrate iovar */
199 /* MSC in use,indicates b0-6 holds an mcs */
200 #define NRATE_MCS_INUSE			0x00000080
201 /* rate/mcs value */
202 #define NRATE_RATE_MASK			0x0000007f
203 /* stf mode mask: siso, cdd, stbc, sdm */
204 #define NRATE_STF_MASK			0x0000ff00
205 /* stf mode shift */
206 #define NRATE_STF_SHIFT			8
207 /* bit indicate to override mcs only */
208 #define NRATE_OVERRIDE_MCS_ONLY		0x40000000
209 #define NRATE_SGI_MASK			0x00800000	/* sgi mode */
210 #define NRATE_SGI_SHIFT			23		/* sgi mode */
211 #define NRATE_LDPC_CODING		0x00400000	/* adv coding in use */
212 #define NRATE_LDPC_SHIFT		22		/* ldpc shift */
213 
214 #define NRATE_STF_SISO			0		/* stf mode SISO */
215 #define NRATE_STF_CDD			1		/* stf mode CDD */
216 #define NRATE_STF_STBC			2		/* stf mode STBC */
217 #define NRATE_STF_SDM			3		/* stf mode SDM */
218 
219 #define MAX_DMA_SEGS			4
220 
221 /* # of entries in Tx FIFO */
222 #define NTXD				64
223 /* Max # of entries in Rx FIFO based on 4kb page size */
224 #define NRXD				256
225 
226 /* Amount of headroom to leave in Tx FIFO */
227 #define TX_HEADROOM			4
228 
229 /* try to keep this # rbufs posted to the chip */
230 #define NRXBUFPOST			32
231 
232 /* max # frames to process in brcms_c_recv() */
233 #define RXBND				8
234 /* max # tx status to process in wlc_txstatus() */
235 #define TXSBND				8
236 
237 /*
238  * The following table lists the buffer memory allocated to xmt fifos in HW.
239  * the size is in units of 256bytes(one block), total size is HW dependent
240  * ucode has default fifo partition, sw can overwrite if necessary
241  *
242  * This is documented in twiki under the topic UcodeTxFifo. Please ensure
243  * the twiki is updated before making changes.
244  */
245 
246 /* Starting corerev for the fifo size table */
247 #define XMTFIFOTBL_STARTREV	17
248 
249 struct d11init {
250 	__le16 addr;
251 	__le16 size;
252 	__le32 value;
253 };
254 
255 struct edcf_acparam {
256 	u8 ACI;
257 	u8 ECW;
258 	u16 TXOP;
259 } __packed;
260 
261 /* debug/trace */
262 uint brcm_msg_level;
263 
264 /* TX FIFO number to WME/802.1E Access Category */
265 static const u8 wme_fifo2ac[] = {
266 	IEEE80211_AC_BK,
267 	IEEE80211_AC_BE,
268 	IEEE80211_AC_VI,
269 	IEEE80211_AC_VO,
270 	IEEE80211_AC_BE,
271 	IEEE80211_AC_BE
272 };
273 
274 /* ieee80211 Access Category to TX FIFO number */
275 static const u8 wme_ac2fifo[] = {
276 	TX_AC_VO_FIFO,
277 	TX_AC_VI_FIFO,
278 	TX_AC_BE_FIFO,
279 	TX_AC_BK_FIFO
280 };
281 
282 static const u16 xmtfifo_sz[][NFIFO] = {
283 	/* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
284 	{20, 192, 192, 21, 17, 5},
285 	/* corerev 18: */
286 	{0, 0, 0, 0, 0, 0},
287 	/* corerev 19: */
288 	{0, 0, 0, 0, 0, 0},
289 	/* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
290 	{20, 192, 192, 21, 17, 5},
291 	/* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
292 	{9, 58, 22, 14, 14, 5},
293 	/* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
294 	{20, 192, 192, 21, 17, 5},
295 	/* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
296 	{20, 192, 192, 21, 17, 5},
297 	/* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
298 	{9, 58, 22, 14, 14, 5},
299 	/* corerev 25: */
300 	{0, 0, 0, 0, 0, 0},
301 	/* corerev 26: */
302 	{0, 0, 0, 0, 0, 0},
303 	/* corerev 27: */
304 	{0, 0, 0, 0, 0, 0},
305 	/* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
306 	{9, 58, 22, 14, 14, 5},
307 };
308 
309 #ifdef DEBUG
310 static const char * const fifo_names[] = {
311 	"AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
312 #else
313 static const char fifo_names[6][1];
314 #endif
315 
316 #ifdef DEBUG
317 /* pointer to most recently allocated wl/wlc */
318 static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
319 #endif
320 
321 /* Mapping of ieee80211 AC numbers to tx fifos */
322 static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
323 	[IEEE80211_AC_VO]	= TX_AC_VO_FIFO,
324 	[IEEE80211_AC_VI]	= TX_AC_VI_FIFO,
325 	[IEEE80211_AC_BE]	= TX_AC_BE_FIFO,
326 	[IEEE80211_AC_BK]	= TX_AC_BK_FIFO,
327 };
328 
329 /* Mapping of tx fifos to ieee80211 AC numbers */
330 static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
331 	[TX_AC_BK_FIFO]	= IEEE80211_AC_BK,
332 	[TX_AC_BE_FIFO]	= IEEE80211_AC_BE,
333 	[TX_AC_VI_FIFO]	= IEEE80211_AC_VI,
334 	[TX_AC_VO_FIFO]	= IEEE80211_AC_VO,
335 };
336 
337 static u8 brcms_ac_to_fifo(u8 ac)
338 {
339 	if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
340 		return TX_AC_BE_FIFO;
341 	return ac_to_fifo_mapping[ac];
342 }
343 
344 static u8 brcms_fifo_to_ac(u8 fifo)
345 {
346 	if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
347 		return IEEE80211_AC_BE;
348 	return fifo_to_ac_mapping[fifo];
349 }
350 
351 /* Find basic rate for a given rate */
352 static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
353 {
354 	if (is_mcs_rate(rspec))
355 		return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
356 		       .leg_ofdm];
357 	return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
358 }
359 
360 static u16 frametype(u32 rspec, u8 mimoframe)
361 {
362 	if (is_mcs_rate(rspec))
363 		return mimoframe;
364 	return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
365 }
366 
367 /* currently the best mechanism for determining SIFS is the band in use */
368 static u16 get_sifs(struct brcms_band *band)
369 {
370 	return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
371 				 BPHY_SIFS_TIME;
372 }
373 
374 /*
375  * Detect Card removed.
376  * Even checking an sbconfig register read will not false trigger when the core
377  * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
378  * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
379  * reg with fixed 0/1 pattern (some platforms return all 0).
380  * If clocks are present, call the sb routine which will figure out if the
381  * device is removed.
382  */
383 static bool brcms_deviceremoved(struct brcms_c_info *wlc)
384 {
385 	u32 macctrl;
386 
387 	if (!wlc->hw->clk)
388 		return ai_deviceremoved(wlc->hw->sih);
389 	macctrl = bcma_read32(wlc->hw->d11core,
390 			      D11REGOFFS(maccontrol));
391 	return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
392 }
393 
394 /* sum the individual fifo tx pending packet counts */
395 static int brcms_txpktpendtot(struct brcms_c_info *wlc)
396 {
397 	int i;
398 	int pending = 0;
399 
400 	for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
401 		if (wlc->hw->di[i])
402 			pending += dma_txpending(wlc->hw->di[i]);
403 	return pending;
404 }
405 
406 static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
407 {
408 	return wlc->pub->_nbands > 1 && !wlc->bandlocked;
409 }
410 
411 static int brcms_chspec_bw(u16 chanspec)
412 {
413 	if (CHSPEC_IS40(chanspec))
414 		return BRCMS_40_MHZ;
415 	if (CHSPEC_IS20(chanspec))
416 		return BRCMS_20_MHZ;
417 
418 	return BRCMS_10_MHZ;
419 }
420 
421 static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
422 {
423 	if (wlc == NULL)
424 		return;
425 
426 	kfree(wlc->bsscfg);
427 	kfree(wlc->pub);
428 	kfree(wlc->modulecb);
429 	kfree(wlc->default_bss);
430 	kfree(wlc->protection);
431 	kfree(wlc->stf);
432 	kfree(wlc->bandstate[0]);
433 	if (wlc->corestate)
434 		kfree(wlc->corestate->macstat_snapshot);
435 	kfree(wlc->corestate);
436 	if (wlc->hw)
437 		kfree(wlc->hw->bandstate[0]);
438 	kfree(wlc->hw);
439 	if (wlc->beacon)
440 		dev_kfree_skb_any(wlc->beacon);
441 	if (wlc->probe_resp)
442 		dev_kfree_skb_any(wlc->probe_resp);
443 
444 	kfree(wlc);
445 }
446 
447 static struct brcms_c_info *
448 brcms_c_attach_malloc(uint unit, uint *err, uint devid)
449 {
450 	struct brcms_c_info *wlc;
451 
452 	wlc = kzalloc_obj(*wlc, GFP_ATOMIC);
453 	if (wlc == NULL) {
454 		*err = 1002;
455 		goto fail;
456 	}
457 
458 	/* allocate struct brcms_c_pub state structure */
459 	wlc->pub = kzalloc_obj(*wlc->pub, GFP_ATOMIC);
460 	if (wlc->pub == NULL) {
461 		*err = 1003;
462 		goto fail;
463 	}
464 	wlc->pub->wlc = wlc;
465 
466 	/* allocate struct brcms_hardware state structure */
467 
468 	wlc->hw = kzalloc_obj(*wlc->hw, GFP_ATOMIC);
469 	if (wlc->hw == NULL) {
470 		*err = 1005;
471 		goto fail;
472 	}
473 	wlc->hw->wlc = wlc;
474 
475 	wlc->hw->bandstate[0] =
476 		kzalloc_objs(struct brcms_hw_band, MAXBANDS, GFP_ATOMIC);
477 	if (wlc->hw->bandstate[0] == NULL) {
478 		*err = 1006;
479 		goto fail;
480 	} else {
481 		int i;
482 
483 		for (i = 1; i < MAXBANDS; i++)
484 			wlc->hw->bandstate[i] = (struct brcms_hw_band *)
485 			    ((unsigned long)wlc->hw->bandstate[0] +
486 			     (sizeof(struct brcms_hw_band) * i));
487 	}
488 
489 	wlc->modulecb =
490 		kzalloc_objs(struct modulecb, BRCMS_MAXMODULES, GFP_ATOMIC);
491 	if (wlc->modulecb == NULL) {
492 		*err = 1009;
493 		goto fail;
494 	}
495 
496 	wlc->default_bss = kzalloc_obj(*wlc->default_bss, GFP_ATOMIC);
497 	if (wlc->default_bss == NULL) {
498 		*err = 1010;
499 		goto fail;
500 	}
501 
502 	wlc->bsscfg = kzalloc_obj(*wlc->bsscfg, GFP_ATOMIC);
503 	if (wlc->bsscfg == NULL) {
504 		*err = 1011;
505 		goto fail;
506 	}
507 
508 	wlc->protection = kzalloc_obj(*wlc->protection, GFP_ATOMIC);
509 	if (wlc->protection == NULL) {
510 		*err = 1016;
511 		goto fail;
512 	}
513 
514 	wlc->stf = kzalloc_obj(*wlc->stf, GFP_ATOMIC);
515 	if (wlc->stf == NULL) {
516 		*err = 1017;
517 		goto fail;
518 	}
519 
520 	wlc->bandstate[0] =
521 		kzalloc_objs(*wlc->bandstate[0], MAXBANDS, GFP_ATOMIC);
522 	if (wlc->bandstate[0] == NULL) {
523 		*err = 1025;
524 		goto fail;
525 	} else {
526 		int i;
527 
528 		for (i = 1; i < MAXBANDS; i++)
529 			wlc->bandstate[i] = (struct brcms_band *)
530 				((unsigned long)wlc->bandstate[0]
531 				+ (sizeof(struct brcms_band)*i));
532 	}
533 
534 	wlc->corestate = kzalloc_obj(*wlc->corestate, GFP_ATOMIC);
535 	if (wlc->corestate == NULL) {
536 		*err = 1026;
537 		goto fail;
538 	}
539 
540 	wlc->corestate->macstat_snapshot = kzalloc_obj(*wlc->corestate->macstat_snapshot,
541 						       GFP_ATOMIC);
542 	if (wlc->corestate->macstat_snapshot == NULL) {
543 		*err = 1027;
544 		goto fail;
545 	}
546 
547 	return wlc;
548 
549  fail:
550 	brcms_c_detach_mfree(wlc);
551 	return NULL;
552 }
553 
554 /*
555  * Update the slot timing for standard 11b/g (20us slots)
556  * or shortslot 11g (9us slots)
557  * The PSM needs to be suspended for this call.
558  */
559 static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
560 					bool shortslot)
561 {
562 	struct bcma_device *core = wlc_hw->d11core;
563 
564 	if (shortslot) {
565 		/* 11g short slot: 11a timing */
566 		bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
567 		brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
568 	} else {
569 		/* 11g long slot: 11b timing */
570 		bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
571 		brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
572 	}
573 }
574 
575 /*
576  * calculate frame duration of a given rate and length, return
577  * time in usec unit
578  */
579 static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
580 				    u8 preamble_type, uint mac_len)
581 {
582 	uint nsyms, dur = 0, Ndps, kNdps;
583 	uint rate = rspec2rate(ratespec);
584 
585 	if (rate == 0) {
586 		brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
587 			  wlc->pub->unit);
588 		rate = BRCM_RATE_1M;
589 	}
590 
591 	if (is_mcs_rate(ratespec)) {
592 		uint mcs = ratespec & RSPEC_RATE_MASK;
593 		int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
594 
595 		dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
596 		if (preamble_type == BRCMS_MM_PREAMBLE)
597 			dur += PREN_MM_EXT;
598 		/* 1000Ndbps = kbps * 4 */
599 		kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
600 				   rspec_issgi(ratespec)) * 4;
601 
602 		if (rspec_stc(ratespec) == 0)
603 			nsyms =
604 			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
605 				  APHY_TAIL_NBITS) * 1000, kNdps);
606 		else
607 			/* STBC needs to have even number of symbols */
608 			nsyms =
609 			    2 *
610 			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
611 				  APHY_TAIL_NBITS) * 1000, 2 * kNdps);
612 
613 		dur += APHY_SYMBOL_TIME * nsyms;
614 		if (wlc->band->bandtype == BRCM_BAND_2G)
615 			dur += DOT11_OFDM_SIGNAL_EXTENSION;
616 	} else if (is_ofdm_rate(rate)) {
617 		dur = APHY_PREAMBLE_TIME;
618 		dur += APHY_SIGNAL_TIME;
619 		/* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
620 		Ndps = rate * 2;
621 		/* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
622 		nsyms =
623 		    CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
624 			 Ndps);
625 		dur += APHY_SYMBOL_TIME * nsyms;
626 		if (wlc->band->bandtype == BRCM_BAND_2G)
627 			dur += DOT11_OFDM_SIGNAL_EXTENSION;
628 	} else {
629 		/*
630 		 * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
631 		 * will divide out
632 		 */
633 		mac_len = mac_len * 8 * 2;
634 		/* calc ceiling of bits/rate = microseconds of air time */
635 		dur = (mac_len + rate - 1) / rate;
636 		if (preamble_type & BRCMS_SHORT_PREAMBLE)
637 			dur += BPHY_PLCP_SHORT_TIME;
638 		else
639 			dur += BPHY_PLCP_TIME;
640 	}
641 	return dur;
642 }
643 
644 static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
645 				const struct d11init *inits)
646 {
647 	struct bcma_device *core = wlc_hw->d11core;
648 	int i;
649 	uint offset;
650 	u16 size;
651 	u32 value;
652 
653 	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
654 
655 	for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
656 		size = le16_to_cpu(inits[i].size);
657 		offset = le16_to_cpu(inits[i].addr);
658 		value = le32_to_cpu(inits[i].value);
659 		if (size == 2)
660 			bcma_write16(core, offset, value);
661 		else if (size == 4)
662 			bcma_write32(core, offset, value);
663 		else
664 			break;
665 	}
666 }
667 
668 static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
669 {
670 	u8 idx;
671 	static const u16 addr[] = {
672 		M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
673 		M_HOST_FLAGS5
674 	};
675 
676 	for (idx = 0; idx < MHFMAX; idx++)
677 		brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
678 }
679 
680 static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
681 {
682 	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
683 
684 	/* init microcode host flags */
685 	brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
686 
687 	/* do band-specific ucode IHR, SHM, and SCR inits */
688 	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
689 		if (BRCMS_ISNPHY(wlc_hw->band))
690 			brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
691 		else
692 			brcms_err(wlc_hw->d11core,
693 				  "%s: wl%d: unsupported phy in corerev %d\n",
694 				  __func__, wlc_hw->unit,
695 				  wlc_hw->corerev);
696 	} else {
697 		if (D11REV_IS(wlc_hw->corerev, 24)) {
698 			if (BRCMS_ISLCNPHY(wlc_hw->band))
699 				brcms_c_write_inits(wlc_hw,
700 						    ucode->d11lcn0bsinitvals24);
701 			else
702 				brcms_err(wlc_hw->d11core,
703 					  "%s: wl%d: unsupported phy in core rev %d\n",
704 					  __func__, wlc_hw->unit,
705 					  wlc_hw->corerev);
706 		} else {
707 			brcms_err(wlc_hw->d11core,
708 				  "%s: wl%d: unsupported corerev %d\n",
709 				  __func__, wlc_hw->unit, wlc_hw->corerev);
710 		}
711 	}
712 }
713 
714 static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
715 {
716 	struct bcma_device *core = wlc_hw->d11core;
717 	u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
718 
719 	bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
720 }
721 
722 static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
723 {
724 	brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
725 
726 	wlc_hw->phyclk = clk;
727 
728 	if (OFF == clk) {	/* clear gmode bit, put phy into reset */
729 
730 		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
731 				   (SICF_PRST | SICF_FGC));
732 		udelay(1);
733 		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
734 		udelay(1);
735 
736 	} else {		/* take phy out of reset */
737 
738 		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
739 		udelay(1);
740 		brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
741 		udelay(1);
742 
743 	}
744 }
745 
746 /* low-level band switch utility routine */
747 static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
748 {
749 	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
750 			   bandunit);
751 
752 	wlc_hw->band = wlc_hw->bandstate[bandunit];
753 
754 	/*
755 	 * BMAC_NOTE:
756 	 *   until we eliminate need for wlc->band refs in low level code
757 	 */
758 	wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
759 
760 	/* set gmode core flag */
761 	if (wlc_hw->sbclk && !wlc_hw->noreset) {
762 		u32 gmode = 0;
763 
764 		if (bandunit == 0)
765 			gmode = SICF_GMODE;
766 
767 		brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
768 	}
769 }
770 
771 /* switch to new band but leave it inactive */
772 static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
773 {
774 	struct brcms_hardware *wlc_hw = wlc->hw;
775 	u32 macintmask;
776 	u32 macctrl;
777 
778 	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
779 	macctrl = bcma_read32(wlc_hw->d11core,
780 			      D11REGOFFS(maccontrol));
781 	WARN_ON((macctrl & MCTL_EN_MAC) != 0);
782 
783 	/* disable interrupts */
784 	macintmask = brcms_intrsoff(wlc->wl);
785 
786 	/* radio off */
787 	wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
788 
789 	brcms_b_core_phy_clk(wlc_hw, OFF);
790 
791 	brcms_c_setxband(wlc_hw, bandunit);
792 
793 	return macintmask;
794 }
795 
796 /* process an individual struct tx_status */
797 static bool
798 brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
799 {
800 	struct sk_buff *p = NULL;
801 	uint queue = NFIFO;
802 	struct dma_pub *dma = NULL;
803 	struct d11txh *txh = NULL;
804 	struct scb *scb = NULL;
805 	int tx_frame_count;
806 	uint supr_status;
807 	bool lastframe;
808 	struct ieee80211_hdr *h;
809 	struct ieee80211_tx_info *tx_info;
810 	struct ieee80211_tx_rate *txrate;
811 	int i;
812 	bool fatal = true;
813 
814 	trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
815 			     txs->frameid, txs->status, txs->lasttxtime,
816 			     txs->sequence, txs->phyerr, txs->ackphyrxsh);
817 
818 	/* discard intermediate indications for ucode with one legitimate case:
819 	 *   e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
820 	 *   but the subsequent tx of DATA failed. so it will start rts/cts
821 	 *   from the beginning (resetting the rts transmission count)
822 	 */
823 	if (!(txs->status & TX_STATUS_AMPDU)
824 	    && (txs->status & TX_STATUS_INTERMEDIATE)) {
825 		brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
826 		fatal = false;
827 		goto out;
828 	}
829 
830 	queue = txs->frameid & TXFID_QUEUE_MASK;
831 	if (queue >= NFIFO) {
832 		brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
833 		goto out;
834 	}
835 
836 	dma = wlc->hw->di[queue];
837 
838 	p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
839 	if (p == NULL) {
840 		brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
841 		goto out;
842 	}
843 
844 	txh = (struct d11txh *) (p->data);
845 
846 	if (txs->phyerr)
847 		brcms_dbg_tx(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
848 			     txs->phyerr, txh->MainRates);
849 
850 	if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
851 		brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
852 		goto out;
853 	}
854 	tx_info = IEEE80211_SKB_CB(p);
855 	h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
856 
857 	if (tx_info->rate_driver_data[0])
858 		scb = &wlc->pri_scb;
859 
860 	if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
861 		brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
862 		fatal = false;
863 		goto out;
864 	}
865 
866 	/*
867 	 * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
868 	 * frames; this traces them for the rest.
869 	 */
870 	trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
871 
872 	supr_status = txs->status & TX_STATUS_SUPR_MASK;
873 	if (supr_status == TX_STATUS_SUPR_BADCH) {
874 		unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
875 		brcms_dbg_tx(wlc->hw->d11core,
876 			     "Pkt tx suppressed, dest chan %u, current %d\n",
877 			     (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
878 			     CHSPEC_CHANNEL(wlc->default_bss->chanspec));
879 	}
880 
881 	tx_frame_count =
882 	    (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
883 
884 	lastframe = !ieee80211_has_morefrags(h->frame_control);
885 
886 	if (!lastframe) {
887 		brcms_err(wlc->hw->d11core, "Not last frame!\n");
888 	} else {
889 		/*
890 		 * Set information to be consumed by Minstrel ht.
891 		 *
892 		 * The "fallback limit" is the number of tx attempts a given
893 		 * MPDU is sent at the "primary" rate. Tx attempts beyond that
894 		 * limit are sent at the "secondary" rate.
895 		 * A 'short frame' does not exceed RTS threshold.
896 		 */
897 		u16 sfbl,	/* Short Frame Rate Fallback Limit */
898 		    lfbl,	/* Long Frame Rate Fallback Limit */
899 		    fbl;
900 
901 		if (queue < IEEE80211_NUM_ACS) {
902 			sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
903 				      EDCF_SFB);
904 			lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
905 				      EDCF_LFB);
906 		} else {
907 			sfbl = wlc->SFBL;
908 			lfbl = wlc->LFBL;
909 		}
910 
911 		txrate = tx_info->status.rates;
912 		if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
913 			fbl = lfbl;
914 		else
915 			fbl = sfbl;
916 
917 		ieee80211_tx_info_clear_status(tx_info);
918 
919 		if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
920 			/*
921 			 * rate selection requested a fallback rate
922 			 * and we used it
923 			 */
924 			txrate[0].count = fbl;
925 			txrate[1].count = tx_frame_count - fbl;
926 		} else {
927 			/*
928 			 * rate selection did not request fallback rate, or
929 			 * we didn't need it
930 			 */
931 			txrate[0].count = tx_frame_count;
932 			/*
933 			 * rc80211_minstrel.c:minstrel_tx_status() expects
934 			 * unused rates to be marked with idx = -1
935 			 */
936 			txrate[1].idx = -1;
937 			txrate[1].count = 0;
938 		}
939 
940 		/* clear the rest of the rates */
941 		for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
942 			txrate[i].idx = -1;
943 			txrate[i].count = 0;
944 		}
945 
946 		if (txs->status & TX_STATUS_ACK_RCV)
947 			tx_info->flags |= IEEE80211_TX_STAT_ACK;
948 	}
949 
950 	if (lastframe) {
951 		/* remove PLCP & Broadcom tx descriptor header */
952 		skb_pull(p, D11_PHY_HDR_LEN);
953 		skb_pull(p, D11_TXH_LEN);
954 		ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
955 	} else {
956 		brcms_err(wlc->hw->d11core,
957 			  "%s: Not last frame => not calling tx_status\n",
958 			  __func__);
959 	}
960 
961 	fatal = false;
962 
963  out:
964 	if (fatal) {
965 		if (txh)
966 			trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
967 					   sizeof(*txh));
968 		brcmu_pkt_buf_free_skb(p);
969 	}
970 
971 	if (dma && queue < NFIFO) {
972 		u16 ac_queue = brcms_fifo_to_ac(queue);
973 		if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
974 		    ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
975 			ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
976 		dma_kick_tx(dma);
977 	}
978 
979 	return fatal;
980 }
981 
982 /* process tx completion events in BMAC
983  * Return true if more tx status need to be processed. false otherwise.
984  */
985 static bool
986 brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
987 {
988 	struct bcma_device *core;
989 	struct tx_status txstatus, *txs;
990 	u32 s1, s2;
991 	uint n = 0;
992 	/*
993 	 * Param 'max_tx_num' indicates max. # tx status to process before
994 	 * break out.
995 	 */
996 	uint max_tx_num = bound ? TXSBND : -1;
997 
998 	txs = &txstatus;
999 	core = wlc_hw->d11core;
1000 	*fatal = false;
1001 
1002 	while (n < max_tx_num) {
1003 		s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
1004 		if (s1 == 0xffffffff) {
1005 			brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
1006 				  __func__);
1007 			*fatal = true;
1008 			return false;
1009 		}
1010 		/* only process when valid */
1011 		if (!(s1 & TXS_V))
1012 			break;
1013 
1014 		s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
1015 		txs->status = s1 & TXS_STATUS_MASK;
1016 		txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
1017 		txs->sequence = s2 & TXS_SEQ_MASK;
1018 		txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
1019 		txs->lasttxtime = 0;
1020 
1021 		*fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
1022 		if (*fatal)
1023 			return false;
1024 		n++;
1025 	}
1026 
1027 	return n >= max_tx_num;
1028 }
1029 
1030 static void brcms_c_tbtt(struct brcms_c_info *wlc)
1031 {
1032 	if (wlc->bsscfg->type == BRCMS_TYPE_ADHOC)
1033 		/*
1034 		 * DirFrmQ is now valid...defer setting until end
1035 		 * of ATIM window
1036 		 */
1037 		wlc->qvalid |= MCMD_DIRFRMQVAL;
1038 }
1039 
1040 /* set initial host flags value */
1041 static void
1042 brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
1043 {
1044 	struct brcms_hardware *wlc_hw = wlc->hw;
1045 
1046 	memset(mhfs, 0, MHFMAX * sizeof(u16));
1047 
1048 	mhfs[MHF2] |= mhf2_init;
1049 
1050 	/* prohibit use of slowclock on multifunction boards */
1051 	if (wlc_hw->boardflags & BFL_NOPLLDOWN)
1052 		mhfs[MHF1] |= MHF1_FORCEFASTCLK;
1053 
1054 	if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
1055 		mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
1056 		mhfs[MHF1] |= MHF1_IQSWAP_WAR;
1057 	}
1058 }
1059 
1060 static uint
1061 dmareg(uint direction, uint fifonum)
1062 {
1063 	if (direction == DMA_TX)
1064 		return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
1065 	return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
1066 }
1067 
1068 static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
1069 {
1070 	uint i;
1071 	char name[8];
1072 	/*
1073 	 * ucode host flag 2 needed for pio mode, independent of band and fifo
1074 	 */
1075 	u16 pio_mhf2 = 0;
1076 	struct brcms_hardware *wlc_hw = wlc->hw;
1077 	uint unit = wlc_hw->unit;
1078 
1079 	/* name and offsets for dma_attach */
1080 	snprintf(name, sizeof(name), "wl%d", unit);
1081 
1082 	if (wlc_hw->di[0] == NULL) {	/* Init FIFOs */
1083 		int dma_attach_err = 0;
1084 
1085 		/*
1086 		 * FIFO 0
1087 		 * TX: TX_AC_BK_FIFO (TX AC Background data packets)
1088 		 * RX: RX_FIFO (RX data packets)
1089 		 */
1090 		wlc_hw->di[0] = dma_attach(name, wlc,
1091 					   (wme ? dmareg(DMA_TX, 0) : 0),
1092 					   dmareg(DMA_RX, 0),
1093 					   (wme ? NTXD : 0), NRXD,
1094 					   RXBUFSZ, -1, NRXBUFPOST,
1095 					   BRCMS_HWRXOFF);
1096 		dma_attach_err |= (NULL == wlc_hw->di[0]);
1097 
1098 		/*
1099 		 * FIFO 1
1100 		 * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
1101 		 *   (legacy) TX_DATA_FIFO (TX data packets)
1102 		 * RX: UNUSED
1103 		 */
1104 		wlc_hw->di[1] = dma_attach(name, wlc,
1105 					   dmareg(DMA_TX, 1), 0,
1106 					   NTXD, 0, 0, -1, 0, 0);
1107 		dma_attach_err |= (NULL == wlc_hw->di[1]);
1108 
1109 		/*
1110 		 * FIFO 2
1111 		 * TX: TX_AC_VI_FIFO (TX AC Video data packets)
1112 		 * RX: UNUSED
1113 		 */
1114 		wlc_hw->di[2] = dma_attach(name, wlc,
1115 					   dmareg(DMA_TX, 2), 0,
1116 					   NTXD, 0, 0, -1, 0, 0);
1117 		dma_attach_err |= (NULL == wlc_hw->di[2]);
1118 		/*
1119 		 * FIFO 3
1120 		 * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
1121 		 *   (legacy) TX_CTL_FIFO (TX control & mgmt packets)
1122 		 */
1123 		wlc_hw->di[3] = dma_attach(name, wlc,
1124 					   dmareg(DMA_TX, 3),
1125 					   0, NTXD, 0, 0, -1,
1126 					   0, 0);
1127 		dma_attach_err |= (NULL == wlc_hw->di[3]);
1128 /* Cleaner to leave this as if with AP defined */
1129 
1130 		if (dma_attach_err) {
1131 			brcms_err(wlc_hw->d11core,
1132 				  "wl%d: wlc_attach: dma_attach failed\n",
1133 				  unit);
1134 			return false;
1135 		}
1136 
1137 		/* get pointer to dma engine tx flow control variable */
1138 		for (i = 0; i < NFIFO; i++)
1139 			if (wlc_hw->di[i])
1140 				wlc_hw->txavail[i] =
1141 				    (uint *) dma_getvar(wlc_hw->di[i],
1142 							"&txavail");
1143 	}
1144 
1145 	/* initial ucode host flags */
1146 	brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
1147 
1148 	return true;
1149 }
1150 
1151 static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
1152 {
1153 	uint j;
1154 
1155 	for (j = 0; j < NFIFO; j++) {
1156 		if (wlc_hw->di[j]) {
1157 			dma_detach(wlc_hw->di[j]);
1158 			wlc_hw->di[j] = NULL;
1159 		}
1160 	}
1161 }
1162 
1163 /*
1164  * Initialize brcms_c_info default values ...
1165  * may get overrides later in this function
1166  *  BMAC_NOTES, move low out and resolve the dangling ones
1167  */
1168 static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
1169 {
1170 	struct brcms_c_info *wlc = wlc_hw->wlc;
1171 
1172 	/* set default sw macintmask value */
1173 	wlc->defmacintmask = DEF_MACINTMASK;
1174 
1175 	/* various 802.11g modes */
1176 	wlc_hw->shortslot = false;
1177 
1178 	wlc_hw->SFBL = RETRY_SHORT_FB;
1179 	wlc_hw->LFBL = RETRY_LONG_FB;
1180 
1181 	/* default mac retry limits */
1182 	wlc_hw->SRL = RETRY_SHORT_DEF;
1183 	wlc_hw->LRL = RETRY_LONG_DEF;
1184 	wlc_hw->chanspec = ch20mhz_chspec(1);
1185 }
1186 
1187 static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
1188 {
1189 	/* delay before first read of ucode state */
1190 	udelay(40);
1191 
1192 	/* wait until ucode is no longer asleep */
1193 	SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
1194 		  DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
1195 }
1196 
1197 /* control chip clock to save power, enable dynamic clock or force fast clock */
1198 static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
1199 {
1200 	if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
1201 		/* new chips with PMU, CCS_FORCEHT will distribute the HT clock
1202 		 * on backplane, but mac core will still run on ALP(not HT) when
1203 		 * it enters powersave mode, which means the FCA bit may not be
1204 		 * set. Should wakeup mac if driver wants it to run on HT.
1205 		 */
1206 
1207 		if (wlc_hw->clk) {
1208 			if (mode == BCMA_CLKMODE_FAST) {
1209 				bcma_set32(wlc_hw->d11core,
1210 					   D11REGOFFS(clk_ctl_st),
1211 					   CCS_FORCEHT);
1212 
1213 				udelay(64);
1214 
1215 				SPINWAIT(
1216 				    ((bcma_read32(wlc_hw->d11core,
1217 				      D11REGOFFS(clk_ctl_st)) &
1218 				      CCS_HTAVAIL) == 0),
1219 				      PMU_MAX_TRANSITION_DLY);
1220 				WARN_ON(!(bcma_read32(wlc_hw->d11core,
1221 					D11REGOFFS(clk_ctl_st)) &
1222 					CCS_HTAVAIL));
1223 			} else {
1224 				if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
1225 				    (bcma_read32(wlc_hw->d11core,
1226 					D11REGOFFS(clk_ctl_st)) &
1227 					(CCS_FORCEHT | CCS_HTAREQ)))
1228 					SPINWAIT(
1229 					    ((bcma_read32(wlc_hw->d11core,
1230 					      offsetof(struct d11regs,
1231 						       clk_ctl_st)) &
1232 					      CCS_HTAVAIL) == 0),
1233 					      PMU_MAX_TRANSITION_DLY);
1234 				bcma_mask32(wlc_hw->d11core,
1235 					D11REGOFFS(clk_ctl_st),
1236 					~CCS_FORCEHT);
1237 			}
1238 		}
1239 		wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
1240 	} else {
1241 
1242 		/* old chips w/o PMU, force HT through cc,
1243 		 * then use FCA to verify mac is running fast clock
1244 		 */
1245 
1246 		wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
1247 
1248 		/* check fast clock is available (if core is not in reset) */
1249 		if (wlc_hw->forcefastclk && wlc_hw->clk)
1250 			WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
1251 				  SISF_FCLKA));
1252 
1253 		/*
1254 		 * keep the ucode wake bit on if forcefastclk is on since we
1255 		 * do not want ucode to put us back to slow clock when it dozes
1256 		 * for PM mode. Code below matches the wake override bit with
1257 		 * current forcefastclk state. Only setting bit in wake_override
1258 		 * instead of waking ucode immediately since old code had this
1259 		 * behavior. Older code set wlc->forcefastclk but only had the
1260 		 * wake happen if the wakup_ucode work (protected by an up
1261 		 * check) was executed just below.
1262 		 */
1263 		if (wlc_hw->forcefastclk)
1264 			mboolset(wlc_hw->wake_override,
1265 				 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1266 		else
1267 			mboolclr(wlc_hw->wake_override,
1268 				 BRCMS_WAKE_OVERRIDE_FORCEFAST);
1269 	}
1270 }
1271 
1272 /* set or clear ucode host flag bits
1273  * it has an optimization for no-change write
1274  * it only writes through shared memory when the core has clock;
1275  * pre-CLK changes should use wlc_write_mhf to get around the optimization
1276  *
1277  *
1278  * bands values are: BRCM_BAND_AUTO <--- Current band only
1279  *                   BRCM_BAND_5G   <--- 5G band only
1280  *                   BRCM_BAND_2G   <--- 2G band only
1281  *                   BRCM_BAND_ALL  <--- All bands
1282  */
1283 void
1284 brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
1285 	     int bands)
1286 {
1287 	u16 save;
1288 	u16 addr[MHFMAX] = {
1289 		M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
1290 		M_HOST_FLAGS5
1291 	};
1292 	struct brcms_hw_band *band;
1293 
1294 	if ((val & ~mask) || idx >= MHFMAX)
1295 		return; /* error condition */
1296 
1297 	switch (bands) {
1298 		/* Current band only or all bands,
1299 		 * then set the band to current band
1300 		 */
1301 	case BRCM_BAND_AUTO:
1302 	case BRCM_BAND_ALL:
1303 		band = wlc_hw->band;
1304 		break;
1305 	case BRCM_BAND_5G:
1306 		band = wlc_hw->bandstate[BAND_5G_INDEX];
1307 		break;
1308 	case BRCM_BAND_2G:
1309 		band = wlc_hw->bandstate[BAND_2G_INDEX];
1310 		break;
1311 	default:
1312 		band = NULL;	/* error condition */
1313 	}
1314 
1315 	if (band) {
1316 		save = band->mhfs[idx];
1317 		band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
1318 
1319 		/* optimization: only write through if changed, and
1320 		 * changed band is the current band
1321 		 */
1322 		if (wlc_hw->clk && (band->mhfs[idx] != save)
1323 		    && (band == wlc_hw->band))
1324 			brcms_b_write_shm(wlc_hw, addr[idx],
1325 					   (u16) band->mhfs[idx]);
1326 	}
1327 
1328 	if (bands == BRCM_BAND_ALL) {
1329 		wlc_hw->bandstate[0]->mhfs[idx] =
1330 		    (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
1331 		wlc_hw->bandstate[1]->mhfs[idx] =
1332 		    (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
1333 	}
1334 }
1335 
1336 /* set the maccontrol register to desired reset state and
1337  * initialize the sw cache of the register
1338  */
1339 static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
1340 {
1341 	/* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
1342 	wlc_hw->maccontrol = 0;
1343 	wlc_hw->suspended_fifos = 0;
1344 	wlc_hw->wake_override = 0;
1345 	wlc_hw->mute_override = 0;
1346 	brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
1347 }
1348 
1349 /*
1350  * write the software state of maccontrol and
1351  * overrides to the maccontrol register
1352  */
1353 static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
1354 {
1355 	u32 maccontrol = wlc_hw->maccontrol;
1356 
1357 	/* OR in the wake bit if overridden */
1358 	if (wlc_hw->wake_override)
1359 		maccontrol |= MCTL_WAKE;
1360 
1361 	/* set AP and INFRA bits for mute if needed */
1362 	if (wlc_hw->mute_override) {
1363 		maccontrol &= ~(MCTL_AP);
1364 		maccontrol |= MCTL_INFRA;
1365 	}
1366 
1367 	bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
1368 		     maccontrol);
1369 }
1370 
1371 /* set or clear maccontrol bits */
1372 void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
1373 {
1374 	u32 maccontrol;
1375 	u32 new_maccontrol;
1376 
1377 	if (val & ~mask)
1378 		return; /* error condition */
1379 	maccontrol = wlc_hw->maccontrol;
1380 	new_maccontrol = (maccontrol & ~mask) | val;
1381 
1382 	/* if the new maccontrol value is the same as the old, nothing to do */
1383 	if (new_maccontrol == maccontrol)
1384 		return;
1385 
1386 	/* something changed, cache the new value */
1387 	wlc_hw->maccontrol = new_maccontrol;
1388 
1389 	/* write the new values with overrides applied */
1390 	brcms_c_mctrl_write(wlc_hw);
1391 }
1392 
1393 void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
1394 				 u32 override_bit)
1395 {
1396 	if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
1397 		mboolset(wlc_hw->wake_override, override_bit);
1398 		return;
1399 	}
1400 
1401 	mboolset(wlc_hw->wake_override, override_bit);
1402 
1403 	brcms_c_mctrl_write(wlc_hw);
1404 	brcms_b_wait_for_wake(wlc_hw);
1405 }
1406 
1407 void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
1408 				   u32 override_bit)
1409 {
1410 	mboolclr(wlc_hw->wake_override, override_bit);
1411 
1412 	if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
1413 		return;
1414 
1415 	brcms_c_mctrl_write(wlc_hw);
1416 }
1417 
1418 /* When driver needs ucode to stop beaconing, it has to make sure that
1419  * MCTL_AP is clear and MCTL_INFRA is set
1420  * Mode           MCTL_AP        MCTL_INFRA
1421  * AP                1              1
1422  * STA               0              1 <--- This will ensure no beacons
1423  * IBSS              0              0
1424  */
1425 static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
1426 {
1427 	wlc_hw->mute_override = 1;
1428 
1429 	/* if maccontrol already has AP == 0 and INFRA == 1 without this
1430 	 * override, then there is no change to write
1431 	 */
1432 	if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1433 		return;
1434 
1435 	brcms_c_mctrl_write(wlc_hw);
1436 }
1437 
1438 /* Clear the override on AP and INFRA bits */
1439 static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
1440 {
1441 	if (wlc_hw->mute_override == 0)
1442 		return;
1443 
1444 	wlc_hw->mute_override = 0;
1445 
1446 	/* if maccontrol already has AP == 0 and INFRA == 1 without this
1447 	 * override, then there is no change to write
1448 	 */
1449 	if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
1450 		return;
1451 
1452 	brcms_c_mctrl_write(wlc_hw);
1453 }
1454 
1455 /*
1456  * Write a MAC address to the given match reg offset in the RXE match engine.
1457  */
1458 static void
1459 brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
1460 		       const u8 *addr)
1461 {
1462 	struct bcma_device *core = wlc_hw->d11core;
1463 	u16 mac_l;
1464 	u16 mac_m;
1465 	u16 mac_h;
1466 
1467 	brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
1468 
1469 	mac_l = addr[0] | (addr[1] << 8);
1470 	mac_m = addr[2] | (addr[3] << 8);
1471 	mac_h = addr[4] | (addr[5] << 8);
1472 
1473 	/* enter the MAC addr into the RXE match registers */
1474 	bcma_write16(core, D11REGOFFS(rcm_ctl),
1475 		     RCM_INC_DATA | match_reg_offset);
1476 	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
1477 	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
1478 	bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
1479 }
1480 
1481 void
1482 brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
1483 			    void *buf)
1484 {
1485 	struct bcma_device *core = wlc_hw->d11core;
1486 	u32 word;
1487 	__le32 word_le;
1488 	__be32 word_be;
1489 	bool be_bit;
1490 	brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
1491 
1492 	bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
1493 
1494 	/* if MCTL_BIGEND bit set in mac control register,
1495 	 * the chip swaps data in fifo, as well as data in
1496 	 * template ram
1497 	 */
1498 	be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
1499 
1500 	while (len > 0) {
1501 		memcpy(&word, buf, sizeof(u32));
1502 
1503 		if (be_bit) {
1504 			word_be = cpu_to_be32(word);
1505 			word = *(u32 *)&word_be;
1506 		} else {
1507 			word_le = cpu_to_le32(word);
1508 			word = *(u32 *)&word_le;
1509 		}
1510 
1511 		bcma_write32(core, D11REGOFFS(tplatewrdata), word);
1512 
1513 		buf = (u8 *) buf + sizeof(u32);
1514 		len -= sizeof(u32);
1515 	}
1516 }
1517 
1518 static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
1519 {
1520 	wlc_hw->band->CWmin = newmin;
1521 
1522 	bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1523 		     OBJADDR_SCR_SEL | S_DOT11_CWMIN);
1524 	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1525 	bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
1526 }
1527 
1528 static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
1529 {
1530 	wlc_hw->band->CWmax = newmax;
1531 
1532 	bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
1533 		     OBJADDR_SCR_SEL | S_DOT11_CWMAX);
1534 	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
1535 	bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
1536 }
1537 
1538 void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
1539 {
1540 	bool fastclk;
1541 
1542 	/* request FAST clock if not on */
1543 	fastclk = wlc_hw->forcefastclk;
1544 	if (!fastclk)
1545 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
1546 
1547 	wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
1548 
1549 	brcms_b_phy_reset(wlc_hw);
1550 	wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
1551 
1552 	/* restore the clk */
1553 	if (!fastclk)
1554 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
1555 }
1556 
1557 static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
1558 {
1559 	u16 v;
1560 	struct brcms_c_info *wlc = wlc_hw->wlc;
1561 	/* update SYNTHPU_DLY */
1562 
1563 	if (BRCMS_ISLCNPHY(wlc->band))
1564 		v = SYNTHPU_DLY_LPPHY_US;
1565 	else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
1566 		v = SYNTHPU_DLY_NPHY_US;
1567 	else
1568 		v = SYNTHPU_DLY_BPHY_US;
1569 
1570 	brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
1571 }
1572 
1573 static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
1574 {
1575 	u16 phyctl;
1576 	u16 phytxant = wlc_hw->bmac_phytxant;
1577 	u16 mask = PHY_TXC_ANT_MASK;
1578 
1579 	/* set the Probe Response frame phy control word */
1580 	phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
1581 	phyctl = (phyctl & ~mask) | phytxant;
1582 	brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
1583 
1584 	/* set the Response (ACK/CTS) frame phy control word */
1585 	phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
1586 	phyctl = (phyctl & ~mask) | phytxant;
1587 	brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
1588 }
1589 
1590 static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
1591 					 u8 rate)
1592 {
1593 	uint i;
1594 	u8 plcp_rate = 0;
1595 	struct plcp_signal_rate_lookup {
1596 		u8 rate;
1597 		u8 signal_rate;
1598 	};
1599 	/* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
1600 	const struct plcp_signal_rate_lookup rate_lookup[] = {
1601 		{BRCM_RATE_6M, 0xB},
1602 		{BRCM_RATE_9M, 0xF},
1603 		{BRCM_RATE_12M, 0xA},
1604 		{BRCM_RATE_18M, 0xE},
1605 		{BRCM_RATE_24M, 0x9},
1606 		{BRCM_RATE_36M, 0xD},
1607 		{BRCM_RATE_48M, 0x8},
1608 		{BRCM_RATE_54M, 0xC}
1609 	};
1610 
1611 	for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
1612 		if (rate == rate_lookup[i].rate) {
1613 			plcp_rate = rate_lookup[i].signal_rate;
1614 			break;
1615 		}
1616 	}
1617 
1618 	/* Find the SHM pointer to the rate table entry by looking in the
1619 	 * Direct-map Table
1620 	 */
1621 	return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
1622 }
1623 
1624 static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
1625 {
1626 	u8 rate;
1627 	u8 rates[8] = {
1628 		BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
1629 		BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
1630 	};
1631 	u16 entry_ptr;
1632 	u16 pctl1;
1633 	uint i;
1634 
1635 	if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
1636 		return;
1637 
1638 	/* walk the phy rate table and update the entries */
1639 	for (i = 0; i < ARRAY_SIZE(rates); i++) {
1640 		rate = rates[i];
1641 
1642 		entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
1643 
1644 		/* read the SHM Rate Table entry OFDM PCTL1 values */
1645 		pctl1 =
1646 		    brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
1647 
1648 		/* modify the value */
1649 		pctl1 &= ~PHY_TXC1_MODE_MASK;
1650 		pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
1651 
1652 		/* Update the SHM Rate Table entry OFDM PCTL1 values */
1653 		brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
1654 				   pctl1);
1655 	}
1656 }
1657 
1658 /* band-specific init */
1659 static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
1660 {
1661 	struct brcms_hardware *wlc_hw = wlc->hw;
1662 
1663 	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
1664 			   wlc_hw->band->bandunit);
1665 
1666 	brcms_c_ucode_bsinit(wlc_hw);
1667 
1668 	wlc_phy_init(wlc_hw->band->pi, chanspec);
1669 
1670 	brcms_c_ucode_txant_set(wlc_hw);
1671 
1672 	/*
1673 	 * cwmin is band-specific, update hardware
1674 	 * with value for current band
1675 	 */
1676 	brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
1677 	brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
1678 
1679 	brcms_b_update_slot_timing(wlc_hw,
1680 				   wlc_hw->band->bandtype == BRCM_BAND_5G ?
1681 				   true : wlc_hw->shortslot);
1682 
1683 	/* write phytype and phyvers */
1684 	brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
1685 	brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
1686 
1687 	/*
1688 	 * initialize the txphyctl1 rate table since
1689 	 * shmem is shared between bands
1690 	 */
1691 	brcms_upd_ofdm_pctl1_table(wlc_hw);
1692 
1693 	brcms_b_upd_synthpu(wlc_hw);
1694 }
1695 
1696 /* Perform a soft reset of the PHY PLL */
1697 void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
1698 {
1699 	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
1700 		  ~0, 0);
1701 	udelay(1);
1702 	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1703 		  0x4, 0);
1704 	udelay(1);
1705 	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1706 		  0x4, 4);
1707 	udelay(1);
1708 	ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
1709 		  0x4, 0);
1710 	udelay(1);
1711 }
1712 
1713 /* light way to turn on phy clock without reset for NPHY only
1714  *  refer to brcms_b_core_phy_clk for full version
1715  */
1716 void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
1717 {
1718 	/* support(necessary for NPHY and HYPHY) only */
1719 	if (!BRCMS_ISNPHY(wlc_hw->band))
1720 		return;
1721 
1722 	if (ON == clk)
1723 		brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
1724 	else
1725 		brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
1726 
1727 }
1728 
1729 void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
1730 {
1731 	if (ON == clk)
1732 		brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
1733 	else
1734 		brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
1735 }
1736 
1737 void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
1738 {
1739 	struct brcms_phy_pub *pih = wlc_hw->band->pi;
1740 	u32 phy_bw_clkbits;
1741 
1742 	brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
1743 
1744 	if (pih == NULL)
1745 		return;
1746 
1747 	phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
1748 
1749 	/* Specific reset sequence required for NPHY rev 3 and 4 */
1750 	if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
1751 	    NREV_LE(wlc_hw->band->phyrev, 4)) {
1752 		/* Set the PHY bandwidth */
1753 		brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
1754 
1755 		udelay(1);
1756 
1757 		/* Perform a soft reset of the PHY PLL */
1758 		brcms_b_core_phypll_reset(wlc_hw);
1759 
1760 		/* reset the PHY */
1761 		brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
1762 				   (SICF_PRST | SICF_PCLKE));
1763 	} else {
1764 		brcms_b_core_ioctl(wlc_hw,
1765 				   (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
1766 				   (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
1767 	}
1768 
1769 	udelay(2);
1770 	brcms_b_core_phy_clk(wlc_hw, ON);
1771 
1772 	wlc_phy_anacore(pih, ON);
1773 }
1774 
1775 /* switch to and initialize new band */
1776 static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
1777 			    u16 chanspec) {
1778 	struct brcms_c_info *wlc = wlc_hw->wlc;
1779 	u32 macintmask;
1780 
1781 	/* Enable the d11 core before accessing it */
1782 	if (!bcma_core_is_enabled(wlc_hw->d11core)) {
1783 		bcma_core_enable(wlc_hw->d11core, 0);
1784 		brcms_c_mctrl_reset(wlc_hw);
1785 	}
1786 
1787 	macintmask = brcms_c_setband_inact(wlc, bandunit);
1788 
1789 	if (!wlc_hw->up)
1790 		return;
1791 
1792 	brcms_b_core_phy_clk(wlc_hw, ON);
1793 
1794 	/* band-specific initializations */
1795 	brcms_b_bsinit(wlc, chanspec);
1796 
1797 	/*
1798 	 * If there are any pending software interrupt bits,
1799 	 * then replace these with a harmless nonzero value
1800 	 * so brcms_c_dpc() will re-enable interrupts when done.
1801 	 */
1802 	if (wlc->macintstatus)
1803 		wlc->macintstatus = MI_DMAINT;
1804 
1805 	/* restore macintmask */
1806 	brcms_intrsrestore(wlc->wl, macintmask);
1807 
1808 	/* ucode should still be suspended.. */
1809 	WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
1810 		 MCTL_EN_MAC) != 0);
1811 }
1812 
1813 static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
1814 {
1815 
1816 	/* reject unsupported corerev */
1817 	if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
1818 		wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
1819 			  wlc_hw->corerev);
1820 		return false;
1821 	}
1822 
1823 	return true;
1824 }
1825 
1826 /* Validate some board info parameters */
1827 static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
1828 {
1829 	uint boardrev = wlc_hw->boardrev;
1830 
1831 	/* 4 bits each for board type, major, minor, and tiny version */
1832 	uint brt = (boardrev & 0xf000) >> 12;
1833 	uint b0 = (boardrev & 0xf00) >> 8;
1834 	uint b1 = (boardrev & 0xf0) >> 4;
1835 	uint b2 = boardrev & 0xf;
1836 
1837 	/* voards from other vendors are always considered valid */
1838 	if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
1839 		return true;
1840 
1841 	/* do some boardrev sanity checks when boardvendor is Broadcom */
1842 	if (boardrev == 0)
1843 		return false;
1844 
1845 	if (boardrev <= 0xff)
1846 		return true;
1847 
1848 	if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
1849 		|| (b2 > 9))
1850 		return false;
1851 
1852 	return true;
1853 }
1854 
1855 static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
1856 {
1857 	struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
1858 
1859 	/* If macaddr exists, use it (Sromrev4, CIS, ...). */
1860 	if (!is_zero_ether_addr(sprom->il0mac)) {
1861 		memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
1862 		return;
1863 	}
1864 
1865 	if (wlc_hw->_nbands > 1)
1866 		memcpy(etheraddr, sprom->et1mac, ETH_ALEN);
1867 	else
1868 		memcpy(etheraddr, sprom->il0mac, ETH_ALEN);
1869 }
1870 
1871 /* power both the pll and external oscillator on/off */
1872 static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
1873 {
1874 	brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
1875 
1876 	/*
1877 	 * dont power down if plldown is false or
1878 	 * we must poll hw radio disable
1879 	 */
1880 	if (!want && wlc_hw->pllreq)
1881 		return;
1882 
1883 	wlc_hw->sbclk = want;
1884 	if (!wlc_hw->sbclk) {
1885 		wlc_hw->clk = false;
1886 		if (wlc_hw->band && wlc_hw->band->pi)
1887 			wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
1888 	}
1889 }
1890 
1891 /*
1892  * Return true if radio is disabled, otherwise false.
1893  * hw radio disable signal is an external pin, users activate it asynchronously
1894  * this function could be called when driver is down and w/o clock
1895  * it operates on different registers depending on corerev and boardflag.
1896  */
1897 static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
1898 {
1899 	bool v, clk, xtal;
1900 	u32 flags = 0;
1901 
1902 	xtal = wlc_hw->sbclk;
1903 	if (!xtal)
1904 		brcms_b_xtal(wlc_hw, ON);
1905 
1906 	/* may need to take core out of reset first */
1907 	clk = wlc_hw->clk;
1908 	if (!clk) {
1909 		/*
1910 		 * mac no longer enables phyclk automatically when driver
1911 		 * accesses phyreg throughput mac. This can be skipped since
1912 		 * only mac reg is accessed below
1913 		 */
1914 		if (D11REV_GE(wlc_hw->corerev, 18))
1915 			flags |= SICF_PCLKE;
1916 
1917 		/*
1918 		 * TODO: test suspend/resume
1919 		 *
1920 		 * AI chip doesn't restore bar0win2 on
1921 		 * hibernation/resume, need sw fixup
1922 		 */
1923 
1924 		bcma_core_enable(wlc_hw->d11core, flags);
1925 		brcms_c_mctrl_reset(wlc_hw);
1926 	}
1927 
1928 	v = ((bcma_read32(wlc_hw->d11core,
1929 			  D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
1930 
1931 	/* put core back into reset */
1932 	if (!clk)
1933 		bcma_core_disable(wlc_hw->d11core, 0);
1934 
1935 	if (!xtal)
1936 		brcms_b_xtal(wlc_hw, OFF);
1937 
1938 	return v;
1939 }
1940 
1941 static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
1942 {
1943 	struct dma_pub *di = wlc_hw->di[fifo];
1944 	return dma_rxreset(di);
1945 }
1946 
1947 /* d11 core reset
1948  *   ensure fask clock during reset
1949  *   reset dma
1950  *   reset d11(out of reset)
1951  *   reset phy(out of reset)
1952  *   clear software macintstatus for fresh new start
1953  * one testing hack wlc_hw->noreset will bypass the d11/phy reset
1954  */
1955 void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
1956 {
1957 	uint i;
1958 	bool fastclk;
1959 
1960 	if (flags == BRCMS_USE_COREFLAGS)
1961 		flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
1962 
1963 	brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
1964 
1965 	/* request FAST clock if not on  */
1966 	fastclk = wlc_hw->forcefastclk;
1967 	if (!fastclk)
1968 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
1969 
1970 	/* reset the dma engines except first time thru */
1971 	if (bcma_core_is_enabled(wlc_hw->d11core)) {
1972 		for (i = 0; i < NFIFO; i++)
1973 			if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
1974 				brcms_err(wlc_hw->d11core, "wl%d: %s: "
1975 					  "dma_txreset[%d]: cannot stop dma\n",
1976 					   wlc_hw->unit, __func__, i);
1977 
1978 		if ((wlc_hw->di[RX_FIFO])
1979 		    && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
1980 			brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
1981 				  "[%d]: cannot stop dma\n",
1982 				  wlc_hw->unit, __func__, RX_FIFO);
1983 	}
1984 	/* if noreset, just stop the psm and return */
1985 	if (wlc_hw->noreset) {
1986 		wlc_hw->wlc->macintstatus = 0;	/* skip wl_dpc after down */
1987 		brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
1988 		return;
1989 	}
1990 
1991 	/*
1992 	 * mac no longer enables phyclk automatically when driver accesses
1993 	 * phyreg throughput mac, AND phy_reset is skipped at early stage when
1994 	 * band->pi is invalid. need to enable PHY CLK
1995 	 */
1996 	if (D11REV_GE(wlc_hw->corerev, 18))
1997 		flags |= SICF_PCLKE;
1998 
1999 	/*
2000 	 * reset the core
2001 	 * In chips with PMU, the fastclk request goes through d11 core
2002 	 * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
2003 	 *
2004 	 * This adds some delay and we can optimize it by also requesting
2005 	 * fastclk through chipcommon during this period if necessary. But
2006 	 * that has to work coordinate with other driver like mips/arm since
2007 	 * they may touch chipcommon as well.
2008 	 */
2009 	wlc_hw->clk = false;
2010 	bcma_core_enable(wlc_hw->d11core, flags);
2011 	wlc_hw->clk = true;
2012 	if (wlc_hw->band && wlc_hw->band->pi)
2013 		wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
2014 
2015 	brcms_c_mctrl_reset(wlc_hw);
2016 
2017 	if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
2018 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
2019 
2020 	brcms_b_phy_reset(wlc_hw);
2021 
2022 	/* turn on PHY_PLL */
2023 	brcms_b_core_phypll_ctl(wlc_hw, true);
2024 
2025 	/* clear sw intstatus */
2026 	wlc_hw->wlc->macintstatus = 0;
2027 
2028 	/* restore the clk setting */
2029 	if (!fastclk)
2030 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
2031 }
2032 
2033 /* txfifo sizes needs to be modified(increased) since the newer cores
2034  * have more memory.
2035  */
2036 static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
2037 {
2038 	struct bcma_device *core = wlc_hw->d11core;
2039 	u16 fifo_nu;
2040 	u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
2041 	u16 txfifo_def, txfifo_def1;
2042 	u16 txfifo_cmd;
2043 
2044 	/* tx fifos start at TXFIFO_START_BLK from the Base address */
2045 	txfifo_startblk = TXFIFO_START_BLK;
2046 
2047 	/* sequence of operations:  reset fifo, set fifo size, reset fifo */
2048 	for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
2049 
2050 		txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
2051 		txfifo_def = (txfifo_startblk & 0xff) |
2052 		    (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
2053 		txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
2054 		    ((((txfifo_endblk -
2055 			1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
2056 		txfifo_cmd =
2057 		    TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
2058 
2059 		bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
2060 		bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
2061 		bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
2062 
2063 		bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
2064 
2065 		txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
2066 	}
2067 	/*
2068 	 * need to propagate to shm location to be in sync since ucode/hw won't
2069 	 * do this
2070 	 */
2071 	brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
2072 			   wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
2073 	brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
2074 			   wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
2075 	brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
2076 			   ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
2077 			    xmtfifo_sz[TX_AC_BK_FIFO]));
2078 	brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
2079 			   ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
2080 			    xmtfifo_sz[TX_BCMC_FIFO]));
2081 }
2082 
2083 /* This function is used for changing the tsf frac register
2084  * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
2085  * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
2086  * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
2087  * HTPHY Formula is 2^26/freq(MHz) e.g.
2088  * For spuron2 - 126MHz -> 2^26/126 = 532610.0
2089  *  - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
2090  * For spuron: 123MHz -> 2^26/123    = 545600.5
2091  *  - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
2092  * For spur off: 120MHz -> 2^26/120    = 559240.5
2093  *  - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
2094  */
2095 
2096 void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
2097 {
2098 	struct bcma_device *core = wlc_hw->d11core;
2099 
2100 	if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
2101 	    (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
2102 		if (spurmode == WL_SPURAVOID_ON2) {	/* 126Mhz */
2103 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
2104 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
2105 		} else if (spurmode == WL_SPURAVOID_ON1) {	/* 123Mhz */
2106 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
2107 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
2108 		} else {	/* 120Mhz */
2109 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
2110 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
2111 		}
2112 	} else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2113 		if (spurmode == WL_SPURAVOID_ON1) {	/* 82Mhz */
2114 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
2115 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
2116 		} else {	/* 80Mhz */
2117 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
2118 			bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
2119 		}
2120 	}
2121 }
2122 
2123 void brcms_c_start_station(struct brcms_c_info *wlc, u8 *addr)
2124 {
2125 	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
2126 	wlc->bsscfg->type = BRCMS_TYPE_STATION;
2127 }
2128 
2129 void brcms_c_start_ap(struct brcms_c_info *wlc, u8 *addr, const u8 *bssid,
2130 		      u8 *ssid, size_t ssid_len)
2131 {
2132 	brcms_c_set_ssid(wlc, ssid, ssid_len);
2133 
2134 	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
2135 	memcpy(wlc->bsscfg->BSSID, bssid, sizeof(wlc->bsscfg->BSSID));
2136 	wlc->bsscfg->type = BRCMS_TYPE_AP;
2137 
2138 	brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, MCTL_AP | MCTL_INFRA);
2139 }
2140 
2141 void brcms_c_start_adhoc(struct brcms_c_info *wlc, u8 *addr)
2142 {
2143 	memcpy(wlc->pub->cur_etheraddr, addr, sizeof(wlc->pub->cur_etheraddr));
2144 	wlc->bsscfg->type = BRCMS_TYPE_ADHOC;
2145 
2146 	brcms_b_mctrl(wlc->hw, MCTL_AP | MCTL_INFRA, 0);
2147 }
2148 
2149 /* Initialize GPIOs that are controlled by D11 core */
2150 static void brcms_c_gpio_init(struct brcms_c_info *wlc)
2151 {
2152 	struct brcms_hardware *wlc_hw = wlc->hw;
2153 	u32 gc, gm;
2154 
2155 	/* use GPIO select 0 to get all gpio signals from the gpio out reg */
2156 	brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
2157 
2158 	/*
2159 	 * Common GPIO setup:
2160 	 *      G0 = LED 0 = WLAN Activity
2161 	 *      G1 = LED 1 = WLAN 2.4 GHz Radio State
2162 	 *      G2 = LED 2 = WLAN 5 GHz Radio State
2163 	 *      G4 = radio disable input (HI enabled, LO disabled)
2164 	 */
2165 
2166 	gc = gm = 0;
2167 
2168 	/* Allocate GPIOs for mimo antenna diversity feature */
2169 	if (wlc_hw->antsel_type == ANTSEL_2x3) {
2170 		/* Enable antenna diversity, use 2x3 mode */
2171 		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2172 			     MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2173 		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
2174 			     MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
2175 
2176 		/* init superswitch control */
2177 		wlc_phy_antsel_init(wlc_hw->band->pi, false);
2178 
2179 	} else if (wlc_hw->antsel_type == ANTSEL_2x4) {
2180 		gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
2181 		/*
2182 		 * The board itself is powered by these GPIOs
2183 		 * (when not sending pattern) so set them high
2184 		 */
2185 		bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
2186 			   (BOARD_GPIO_12 | BOARD_GPIO_13));
2187 		bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
2188 			   (BOARD_GPIO_12 | BOARD_GPIO_13));
2189 
2190 		/* Enable antenna diversity, use 2x4 mode */
2191 		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
2192 			     MHF3_ANTSEL_EN, BRCM_BAND_ALL);
2193 		brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
2194 			     BRCM_BAND_ALL);
2195 
2196 		/* Configure the desired clock to be 4Mhz */
2197 		brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
2198 				   ANTSEL_CLKDIV_4MHZ);
2199 	}
2200 
2201 	/*
2202 	 * gpio 9 controls the PA. ucode is responsible
2203 	 * for wiggling out and oe
2204 	 */
2205 	if (wlc_hw->boardflags & BFL_PACTRL)
2206 		gm |= gc |= BOARD_GPIO_PACTRL;
2207 
2208 	/* apply to gpiocontrol register */
2209 	bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
2210 }
2211 
2212 static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
2213 			      const __le32 ucode[], const size_t nbytes)
2214 {
2215 	struct bcma_device *core = wlc_hw->d11core;
2216 	uint i;
2217 	uint count;
2218 
2219 	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
2220 
2221 	count = (nbytes / sizeof(u32));
2222 
2223 	bcma_write32(core, D11REGOFFS(objaddr),
2224 		     OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
2225 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2226 	for (i = 0; i < count; i++)
2227 		bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
2228 
2229 }
2230 
2231 static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
2232 {
2233 	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
2234 
2235 	if (wlc_hw->ucode_loaded)
2236 		return;
2237 
2238 	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
2239 		if (BRCMS_ISNPHY(wlc_hw->band)) {
2240 			brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
2241 					  ucode->bcm43xx_16_mimosz);
2242 			wlc_hw->ucode_loaded = true;
2243 		} else
2244 			brcms_err(wlc_hw->d11core,
2245 				  "%s: wl%d: unsupported phy in corerev %d\n",
2246 				  __func__, wlc_hw->unit, wlc_hw->corerev);
2247 	} else if (D11REV_IS(wlc_hw->corerev, 24)) {
2248 		if (BRCMS_ISLCNPHY(wlc_hw->band)) {
2249 			brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
2250 					  ucode->bcm43xx_24_lcnsz);
2251 			wlc_hw->ucode_loaded = true;
2252 		} else {
2253 			brcms_err(wlc_hw->d11core,
2254 				  "%s: wl%d: unsupported phy in corerev %d\n",
2255 				  __func__, wlc_hw->unit, wlc_hw->corerev);
2256 		}
2257 	}
2258 }
2259 
2260 void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
2261 {
2262 	/* update sw state */
2263 	wlc_hw->bmac_phytxant = phytxant;
2264 
2265 	/* push to ucode if up */
2266 	if (!wlc_hw->up)
2267 		return;
2268 	brcms_c_ucode_txant_set(wlc_hw);
2269 
2270 }
2271 
2272 u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
2273 {
2274 	return (u16) wlc_hw->wlc->stf->txant;
2275 }
2276 
2277 void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
2278 {
2279 	wlc_hw->antsel_type = antsel_type;
2280 
2281 	/* Update the antsel type for phy module to use */
2282 	wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
2283 }
2284 
2285 static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
2286 {
2287 	bool fatal = false;
2288 	uint unit;
2289 	uint intstatus, idx;
2290 	struct bcma_device *core = wlc_hw->d11core;
2291 
2292 	unit = wlc_hw->unit;
2293 
2294 	for (idx = 0; idx < NFIFO; idx++) {
2295 		/* read intstatus register and ignore any non-error bits */
2296 		intstatus =
2297 			bcma_read32(core,
2298 				    D11REGOFFS(intctrlregs[idx].intstatus)) &
2299 			I_ERRORS;
2300 		if (!intstatus)
2301 			continue;
2302 
2303 		brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
2304 			      unit, idx, intstatus);
2305 
2306 		if (intstatus & I_RO) {
2307 			brcms_err(core, "wl%d: fifo %d: receive fifo "
2308 				  "overflow\n", unit, idx);
2309 			fatal = true;
2310 		}
2311 
2312 		if (intstatus & I_PC) {
2313 			brcms_err(core, "wl%d: fifo %d: descriptor error\n",
2314 				  unit, idx);
2315 			fatal = true;
2316 		}
2317 
2318 		if (intstatus & I_PD) {
2319 			brcms_err(core, "wl%d: fifo %d: data error\n", unit,
2320 				  idx);
2321 			fatal = true;
2322 		}
2323 
2324 		if (intstatus & I_DE) {
2325 			brcms_err(core, "wl%d: fifo %d: descriptor protocol "
2326 				  "error\n", unit, idx);
2327 			fatal = true;
2328 		}
2329 
2330 		if (intstatus & I_RU)
2331 			brcms_err(core, "wl%d: fifo %d: receive descriptor "
2332 				  "underflow\n", idx, unit);
2333 
2334 		if (intstatus & I_XU) {
2335 			brcms_err(core, "wl%d: fifo %d: transmit fifo "
2336 				  "underflow\n", idx, unit);
2337 			fatal = true;
2338 		}
2339 
2340 		if (fatal) {
2341 			brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
2342 			break;
2343 		} else
2344 			bcma_write32(core,
2345 				     D11REGOFFS(intctrlregs[idx].intstatus),
2346 				     intstatus);
2347 	}
2348 }
2349 
2350 void brcms_c_intrson(struct brcms_c_info *wlc)
2351 {
2352 	struct brcms_hardware *wlc_hw = wlc->hw;
2353 	wlc->macintmask = wlc->defmacintmask;
2354 	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
2355 }
2356 
2357 u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
2358 {
2359 	struct brcms_hardware *wlc_hw = wlc->hw;
2360 	u32 macintmask;
2361 
2362 	if (!wlc_hw->clk)
2363 		return 0;
2364 
2365 	macintmask = wlc->macintmask;	/* isr can still happen */
2366 
2367 	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
2368 	(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
2369 	udelay(1);		/* ensure int line is no longer driven */
2370 	wlc->macintmask = 0;
2371 
2372 	/* return previous macintmask; resolve race between us and our isr */
2373 	return wlc->macintstatus ? 0 : macintmask;
2374 }
2375 
2376 void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
2377 {
2378 	struct brcms_hardware *wlc_hw = wlc->hw;
2379 	if (!wlc_hw->clk)
2380 		return;
2381 
2382 	wlc->macintmask = macintmask;
2383 	bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
2384 }
2385 
2386 /* assumes that the d11 MAC is enabled */
2387 static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
2388 				    uint tx_fifo)
2389 {
2390 	u8 fifo = 1 << tx_fifo;
2391 
2392 	/* Two clients of this code, 11h Quiet period and scanning. */
2393 
2394 	/* only suspend if not already suspended */
2395 	if ((wlc_hw->suspended_fifos & fifo) == fifo)
2396 		return;
2397 
2398 	/* force the core awake only if not already */
2399 	if (wlc_hw->suspended_fifos == 0)
2400 		brcms_c_ucode_wake_override_set(wlc_hw,
2401 						BRCMS_WAKE_OVERRIDE_TXFIFO);
2402 
2403 	wlc_hw->suspended_fifos |= fifo;
2404 
2405 	if (wlc_hw->di[tx_fifo]) {
2406 		/*
2407 		 * Suspending AMPDU transmissions in the middle can cause
2408 		 * underflow which may result in mismatch between ucode and
2409 		 * driver so suspend the mac before suspending the FIFO
2410 		 */
2411 		if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2412 			brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
2413 
2414 		dma_txsuspend(wlc_hw->di[tx_fifo]);
2415 
2416 		if (BRCMS_PHY_11N_CAP(wlc_hw->band))
2417 			brcms_c_enable_mac(wlc_hw->wlc);
2418 	}
2419 }
2420 
2421 static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
2422 				   uint tx_fifo)
2423 {
2424 	/* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
2425 	 * but need to be done here for PIO otherwise the watchdog will catch
2426 	 * the inconsistency and fire
2427 	 */
2428 	/* Two clients of this code, 11h Quiet period and scanning. */
2429 	if (wlc_hw->di[tx_fifo])
2430 		dma_txresume(wlc_hw->di[tx_fifo]);
2431 
2432 	/* allow core to sleep again */
2433 	if (wlc_hw->suspended_fifos == 0)
2434 		return;
2435 	else {
2436 		wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
2437 		if (wlc_hw->suspended_fifos == 0)
2438 			brcms_c_ucode_wake_override_clear(wlc_hw,
2439 						BRCMS_WAKE_OVERRIDE_TXFIFO);
2440 	}
2441 }
2442 
2443 /* precondition: requires the mac core to be enabled */
2444 static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
2445 {
2446 	static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
2447 	u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
2448 
2449 	if (mute_tx) {
2450 		/* suspend tx fifos */
2451 		brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
2452 		brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
2453 		brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
2454 		brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
2455 
2456 		/* zero the address match register so we do not send ACKs */
2457 		brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
2458 	} else {
2459 		/* resume tx fifos */
2460 		brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
2461 		brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
2462 		brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
2463 		brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
2464 
2465 		/* Restore address */
2466 		brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
2467 	}
2468 
2469 	wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
2470 
2471 	if (mute_tx)
2472 		brcms_c_ucode_mute_override_set(wlc_hw);
2473 	else
2474 		brcms_c_ucode_mute_override_clear(wlc_hw);
2475 }
2476 
2477 void
2478 brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
2479 {
2480 	brcms_b_mute(wlc->hw, mute_tx);
2481 }
2482 
2483 /*
2484  * Read and clear macintmask and macintstatus and intstatus registers.
2485  * This routine should be called with interrupts off
2486  * Return:
2487  *   -1 if brcms_deviceremoved(wlc) evaluates to true;
2488  *   0 if the interrupt is not for us, or we are in some special cases;
2489  *   device interrupt status bits otherwise.
2490  */
2491 static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
2492 {
2493 	struct brcms_hardware *wlc_hw = wlc->hw;
2494 	struct bcma_device *core = wlc_hw->d11core;
2495 	u32 macintstatus, mask;
2496 
2497 	/* macintstatus includes a DMA interrupt summary bit */
2498 	macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
2499 	mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
2500 
2501 	trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
2502 
2503 	/* detect cardbus removed, in power down(suspend) and in reset */
2504 	if (brcms_deviceremoved(wlc))
2505 		return -1;
2506 
2507 	/* brcms_deviceremoved() succeeds even when the core is still resetting,
2508 	 * handle that case here.
2509 	 */
2510 	if (macintstatus == 0xffffffff)
2511 		return 0;
2512 
2513 	/* defer unsolicited interrupts */
2514 	macintstatus &= mask;
2515 
2516 	/* if not for us */
2517 	if (macintstatus == 0)
2518 		return 0;
2519 
2520 	/* turn off the interrupts */
2521 	bcma_write32(core, D11REGOFFS(macintmask), 0);
2522 	(void)bcma_read32(core, D11REGOFFS(macintmask));
2523 	wlc->macintmask = 0;
2524 
2525 	/* clear device interrupts */
2526 	bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
2527 
2528 	/* MI_DMAINT is indication of non-zero intstatus */
2529 	if (macintstatus & MI_DMAINT)
2530 		/*
2531 		 * only fifo interrupt enabled is I_RI in
2532 		 * RX_FIFO. If MI_DMAINT is set, assume it
2533 		 * is set and clear the interrupt.
2534 		 */
2535 		bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
2536 			     DEF_RXINTMASK);
2537 
2538 	return macintstatus;
2539 }
2540 
2541 /* Update wlc->macintstatus and wlc->intstatus[]. */
2542 /* Return true if they are updated successfully. false otherwise */
2543 bool brcms_c_intrsupd(struct brcms_c_info *wlc)
2544 {
2545 	u32 macintstatus;
2546 
2547 	/* read and clear macintstatus and intstatus registers */
2548 	macintstatus = wlc_intstatus(wlc, false);
2549 
2550 	/* device is removed */
2551 	if (macintstatus == 0xffffffff)
2552 		return false;
2553 
2554 	/* update interrupt status in software */
2555 	wlc->macintstatus |= macintstatus;
2556 
2557 	return true;
2558 }
2559 
2560 /*
2561  * First-level interrupt processing.
2562  * Return true if this was our interrupt
2563  * and if further brcms_c_dpc() processing is required,
2564  * false otherwise.
2565  */
2566 bool brcms_c_isr(struct brcms_c_info *wlc)
2567 {
2568 	struct brcms_hardware *wlc_hw = wlc->hw;
2569 	u32 macintstatus;
2570 
2571 	if (!wlc_hw->up || !wlc->macintmask)
2572 		return false;
2573 
2574 	/* read and clear macintstatus and intstatus registers */
2575 	macintstatus = wlc_intstatus(wlc, true);
2576 
2577 	if (macintstatus == 0xffffffff) {
2578 		brcms_err(wlc_hw->d11core,
2579 			  "DEVICEREMOVED detected in the ISR code path\n");
2580 		return false;
2581 	}
2582 
2583 	/* it is not for us */
2584 	if (macintstatus == 0)
2585 		return false;
2586 
2587 	/* save interrupt status bits */
2588 	wlc->macintstatus = macintstatus;
2589 
2590 	return true;
2591 
2592 }
2593 
2594 void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
2595 {
2596 	struct brcms_hardware *wlc_hw = wlc->hw;
2597 	struct bcma_device *core = wlc_hw->d11core;
2598 	u32 mc, mi;
2599 
2600 	brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2601 			   wlc_hw->band->bandunit);
2602 
2603 	/*
2604 	 * Track overlapping suspend requests
2605 	 */
2606 	wlc_hw->mac_suspend_depth++;
2607 	if (wlc_hw->mac_suspend_depth > 1)
2608 		return;
2609 
2610 	/* force the core awake */
2611 	brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2612 
2613 	mc = bcma_read32(core, D11REGOFFS(maccontrol));
2614 
2615 	if (mc == 0xffffffff) {
2616 		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
2617 			  __func__);
2618 		brcms_down(wlc->wl);
2619 		return;
2620 	}
2621 	WARN_ON(mc & MCTL_PSM_JMP_0);
2622 	WARN_ON(!(mc & MCTL_PSM_RUN));
2623 	WARN_ON(!(mc & MCTL_EN_MAC));
2624 
2625 	mi = bcma_read32(core, D11REGOFFS(macintstatus));
2626 	if (mi == 0xffffffff) {
2627 		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
2628 			  __func__);
2629 		brcms_down(wlc->wl);
2630 		return;
2631 	}
2632 	WARN_ON(mi & MI_MACSSPNDD);
2633 
2634 	brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
2635 
2636 	SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
2637 		 BRCMS_MAX_MAC_SUSPEND);
2638 
2639 	if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
2640 		brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
2641 			  " and MI_MACSSPNDD is still not on.\n",
2642 			  wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
2643 		brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
2644 			  "psm_brc 0x%04x\n", wlc_hw->unit,
2645 			  bcma_read32(core, D11REGOFFS(psmdebug)),
2646 			  bcma_read32(core, D11REGOFFS(phydebug)),
2647 			  bcma_read16(core, D11REGOFFS(psm_brc)));
2648 	}
2649 
2650 	mc = bcma_read32(core, D11REGOFFS(maccontrol));
2651 	if (mc == 0xffffffff) {
2652 		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
2653 			  __func__);
2654 		brcms_down(wlc->wl);
2655 		return;
2656 	}
2657 	WARN_ON(mc & MCTL_PSM_JMP_0);
2658 	WARN_ON(!(mc & MCTL_PSM_RUN));
2659 	WARN_ON(mc & MCTL_EN_MAC);
2660 }
2661 
2662 void brcms_c_enable_mac(struct brcms_c_info *wlc)
2663 {
2664 	struct brcms_hardware *wlc_hw = wlc->hw;
2665 	struct bcma_device *core = wlc_hw->d11core;
2666 	u32 mc, mi;
2667 
2668 	brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
2669 			   wlc->band->bandunit);
2670 
2671 	/*
2672 	 * Track overlapping suspend requests
2673 	 */
2674 	wlc_hw->mac_suspend_depth--;
2675 	if (wlc_hw->mac_suspend_depth > 0)
2676 		return;
2677 
2678 	mc = bcma_read32(core, D11REGOFFS(maccontrol));
2679 	WARN_ON(mc & MCTL_PSM_JMP_0);
2680 	WARN_ON(mc & MCTL_EN_MAC);
2681 	WARN_ON(!(mc & MCTL_PSM_RUN));
2682 
2683 	brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
2684 	bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
2685 
2686 	mc = bcma_read32(core, D11REGOFFS(maccontrol));
2687 	WARN_ON(mc & MCTL_PSM_JMP_0);
2688 	WARN_ON(!(mc & MCTL_EN_MAC));
2689 	WARN_ON(!(mc & MCTL_PSM_RUN));
2690 
2691 	mi = bcma_read32(core, D11REGOFFS(macintstatus));
2692 	WARN_ON(mi & MI_MACSSPNDD);
2693 
2694 	brcms_c_ucode_wake_override_clear(wlc_hw,
2695 					  BRCMS_WAKE_OVERRIDE_MACSUSPEND);
2696 }
2697 
2698 void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
2699 {
2700 	wlc_hw->hw_stf_ss_opmode = stf_mode;
2701 
2702 	if (wlc_hw->clk)
2703 		brcms_upd_ofdm_pctl1_table(wlc_hw);
2704 }
2705 
2706 static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
2707 {
2708 	struct bcma_device *core = wlc_hw->d11core;
2709 	u32 w, val;
2710 	struct wiphy *wiphy = wlc_hw->wlc->wiphy;
2711 
2712 	/* Validate dchip register access */
2713 
2714 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2715 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2716 	w = bcma_read32(core, D11REGOFFS(objdata));
2717 
2718 	/* Can we write and read back a 32bit register? */
2719 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2720 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2721 	bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
2722 
2723 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2724 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2725 	val = bcma_read32(core, D11REGOFFS(objdata));
2726 	if (val != (u32) 0xaa5555aa) {
2727 		wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2728 			  "expected 0xaa5555aa\n", wlc_hw->unit, val);
2729 		return false;
2730 	}
2731 
2732 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2733 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2734 	bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
2735 
2736 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2737 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2738 	val = bcma_read32(core, D11REGOFFS(objdata));
2739 	if (val != (u32) 0x55aaaa55) {
2740 		wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
2741 			  "expected 0x55aaaa55\n", wlc_hw->unit, val);
2742 		return false;
2743 	}
2744 
2745 	bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
2746 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2747 	bcma_write32(core, D11REGOFFS(objdata), w);
2748 
2749 	/* clear CFPStart */
2750 	bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
2751 
2752 	w = bcma_read32(core, D11REGOFFS(maccontrol));
2753 	if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
2754 	    (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
2755 		wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
2756 			  "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
2757 			  (MCTL_IHR_EN | MCTL_WAKE),
2758 			  (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
2759 		return false;
2760 	}
2761 
2762 	return true;
2763 }
2764 
2765 #define PHYPLL_WAIT_US	100000
2766 
2767 void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
2768 {
2769 	struct bcma_device *core = wlc_hw->d11core;
2770 	u32 tmp;
2771 
2772 	brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
2773 
2774 	tmp = 0;
2775 
2776 	if (on) {
2777 		if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
2778 			bcma_set32(core, D11REGOFFS(clk_ctl_st),
2779 				   CCS_ERSRC_REQ_HT |
2780 				   CCS_ERSRC_REQ_D11PLL |
2781 				   CCS_ERSRC_REQ_PHYPLL);
2782 			SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
2783 				  CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
2784 				 PHYPLL_WAIT_US);
2785 
2786 			tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
2787 			if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
2788 				brcms_err(core, "%s: turn on PHY PLL failed\n",
2789 					  __func__);
2790 		} else {
2791 			bcma_set32(core, D11REGOFFS(clk_ctl_st),
2792 				   tmp | CCS_ERSRC_REQ_D11PLL |
2793 				   CCS_ERSRC_REQ_PHYPLL);
2794 			SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
2795 				  (CCS_ERSRC_AVAIL_D11PLL |
2796 				   CCS_ERSRC_AVAIL_PHYPLL)) !=
2797 				 (CCS_ERSRC_AVAIL_D11PLL |
2798 				  CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
2799 
2800 			tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
2801 			if ((tmp &
2802 			     (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2803 			    !=
2804 			    (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
2805 				brcms_err(core, "%s: turn on PHY PLL failed\n",
2806 					  __func__);
2807 		}
2808 	} else {
2809 		/*
2810 		 * Since the PLL may be shared, other cores can still
2811 		 * be requesting it; so we'll deassert the request but
2812 		 * not wait for status to comply.
2813 		 */
2814 		bcma_mask32(core, D11REGOFFS(clk_ctl_st),
2815 			    ~CCS_ERSRC_REQ_PHYPLL);
2816 		(void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
2817 	}
2818 }
2819 
2820 static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
2821 {
2822 	bool dev_gone;
2823 
2824 	brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
2825 
2826 	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
2827 
2828 	if (dev_gone)
2829 		return;
2830 
2831 	if (wlc_hw->noreset)
2832 		return;
2833 
2834 	/* radio off */
2835 	wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
2836 
2837 	/* turn off analog core */
2838 	wlc_phy_anacore(wlc_hw->band->pi, OFF);
2839 
2840 	/* turn off PHYPLL to save power */
2841 	brcms_b_core_phypll_ctl(wlc_hw, false);
2842 
2843 	wlc_hw->clk = false;
2844 	bcma_core_disable(wlc_hw->d11core, 0);
2845 	wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
2846 }
2847 
2848 static void brcms_c_flushqueues(struct brcms_c_info *wlc)
2849 {
2850 	struct brcms_hardware *wlc_hw = wlc->hw;
2851 	uint i;
2852 
2853 	/* free any posted tx packets */
2854 	for (i = 0; i < NFIFO; i++) {
2855 		if (wlc_hw->di[i]) {
2856 			dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
2857 			if (i < TX_BCMC_FIFO)
2858 				ieee80211_wake_queue(wlc->pub->ieee_hw,
2859 						     brcms_fifo_to_ac(i));
2860 		}
2861 	}
2862 
2863 	/* free any posted rx packets */
2864 	dma_rxreclaim(wlc_hw->di[RX_FIFO]);
2865 }
2866 
2867 static u16
2868 brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
2869 {
2870 	struct bcma_device *core = wlc_hw->d11core;
2871 	u16 objoff = D11REGOFFS(objdata);
2872 
2873 	bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2874 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2875 	if (offset & 2)
2876 		objoff += 2;
2877 
2878 	return bcma_read16(core, objoff);
2879 }
2880 
2881 static void
2882 brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
2883 		     u32 sel)
2884 {
2885 	struct bcma_device *core = wlc_hw->d11core;
2886 	u16 objoff = D11REGOFFS(objdata);
2887 
2888 	bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
2889 	(void)bcma_read32(core, D11REGOFFS(objaddr));
2890 	if (offset & 2)
2891 		objoff += 2;
2892 
2893 	bcma_wflush16(core, objoff, v);
2894 }
2895 
2896 /*
2897  * Read a single u16 from shared memory.
2898  * SHM 'offset' needs to be an even address
2899  */
2900 u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
2901 {
2902 	return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
2903 }
2904 
2905 /*
2906  * Write a single u16 to shared memory.
2907  * SHM 'offset' needs to be an even address
2908  */
2909 void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
2910 {
2911 	brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
2912 }
2913 
2914 /*
2915  * Copy a buffer to shared memory of specified type .
2916  * SHM 'offset' needs to be an even address and
2917  * Buffer length 'len' must be an even number of bytes
2918  * 'sel' selects the type of memory
2919  */
2920 void
2921 brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
2922 		      const void *buf, int len, u32 sel)
2923 {
2924 	u16 v;
2925 	const u8 *p = (const u8 *)buf;
2926 	int i;
2927 
2928 	if (len <= 0 || (offset & 1) || (len & 1))
2929 		return;
2930 
2931 	for (i = 0; i < len; i += 2) {
2932 		v = p[i] | (p[i + 1] << 8);
2933 		brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
2934 	}
2935 }
2936 
2937 /*
2938  * Copy a piece of shared memory of specified type to a buffer .
2939  * SHM 'offset' needs to be an even address and
2940  * Buffer length 'len' must be an even number of bytes
2941  * 'sel' selects the type of memory
2942  */
2943 void
2944 brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
2945 			 int len, u32 sel)
2946 {
2947 	u16 v;
2948 	u8 *p = (u8 *) buf;
2949 	int i;
2950 
2951 	if (len <= 0 || (offset & 1) || (len & 1))
2952 		return;
2953 
2954 	for (i = 0; i < len; i += 2) {
2955 		v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
2956 		p[i] = v & 0xFF;
2957 		p[i + 1] = (v >> 8) & 0xFF;
2958 	}
2959 }
2960 
2961 /* Copy a buffer to shared memory.
2962  * SHM 'offset' needs to be an even address and
2963  * Buffer length 'len' must be an even number of bytes
2964  */
2965 static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
2966 			const void *buf, int len)
2967 {
2968 	brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
2969 }
2970 
2971 static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
2972 				   u16 SRL, u16 LRL)
2973 {
2974 	wlc_hw->SRL = SRL;
2975 	wlc_hw->LRL = LRL;
2976 
2977 	/* write retry limit to SCR, shouldn't need to suspend */
2978 	if (wlc_hw->up) {
2979 		bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
2980 			     OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
2981 		(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
2982 		bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
2983 		bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
2984 			     OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
2985 		(void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
2986 		bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
2987 	}
2988 }
2989 
2990 static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
2991 {
2992 	if (set) {
2993 		if (mboolisset(wlc_hw->pllreq, req_bit))
2994 			return;
2995 
2996 		mboolset(wlc_hw->pllreq, req_bit);
2997 
2998 		if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
2999 			if (!wlc_hw->sbclk)
3000 				brcms_b_xtal(wlc_hw, ON);
3001 		}
3002 	} else {
3003 		if (!mboolisset(wlc_hw->pllreq, req_bit))
3004 			return;
3005 
3006 		mboolclr(wlc_hw->pllreq, req_bit);
3007 
3008 		if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
3009 			if (wlc_hw->sbclk)
3010 				brcms_b_xtal(wlc_hw, OFF);
3011 		}
3012 	}
3013 }
3014 
3015 static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
3016 {
3017 	wlc_hw->antsel_avail = antsel_avail;
3018 }
3019 
3020 /*
3021  * conditions under which the PM bit should be set in outgoing frames
3022  * and STAY_AWAKE is meaningful
3023  */
3024 static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
3025 {
3026 	/* not supporting PS so always return false for now */
3027 	return false;
3028 }
3029 
3030 static void brcms_c_statsupd(struct brcms_c_info *wlc)
3031 {
3032 	int i;
3033 	struct macstat *macstats;
3034 #ifdef DEBUG
3035 	u16 delta;
3036 	u16 rxf0ovfl;
3037 	u16 txfunfl[NFIFO];
3038 #endif				/* DEBUG */
3039 
3040 	/* if driver down, make no sense to update stats */
3041 	if (!wlc->pub->up)
3042 		return;
3043 
3044 	macstats = wlc->core->macstat_snapshot;
3045 
3046 #ifdef DEBUG
3047 	/* save last rx fifo 0 overflow count */
3048 	rxf0ovfl = macstats->rxf0ovfl;
3049 
3050 	/* save last tx fifo  underflow count */
3051 	for (i = 0; i < NFIFO; i++)
3052 		txfunfl[i] = macstats->txfunfl[i];
3053 #endif				/* DEBUG */
3054 
3055 	/* Read mac stats from contiguous shared memory */
3056 	brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, macstats,
3057 				sizeof(*macstats), OBJADDR_SHM_SEL);
3058 
3059 #ifdef DEBUG
3060 	/* check for rx fifo 0 overflow */
3061 	delta = (u16)(macstats->rxf0ovfl - rxf0ovfl);
3062 	if (delta)
3063 		brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
3064 			  wlc->pub->unit, delta);
3065 
3066 	/* check for tx fifo underflows */
3067 	for (i = 0; i < NFIFO; i++) {
3068 		delta = macstats->txfunfl[i] - txfunfl[i];
3069 		if (delta)
3070 			brcms_err(wlc->hw->d11core,
3071 				  "wl%d: %u tx fifo %d underflows!\n",
3072 				  wlc->pub->unit, delta, i);
3073 	}
3074 #endif				/* DEBUG */
3075 
3076 	/* merge counters from dma module */
3077 	for (i = 0; i < NFIFO; i++) {
3078 		if (wlc->hw->di[i])
3079 			dma_counterreset(wlc->hw->di[i]);
3080 	}
3081 }
3082 
3083 static void brcms_b_reset(struct brcms_hardware *wlc_hw)
3084 {
3085 	/* reset the core */
3086 	if (!brcms_deviceremoved(wlc_hw->wlc))
3087 		brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
3088 
3089 	/* purge the dma rings */
3090 	brcms_c_flushqueues(wlc_hw->wlc);
3091 }
3092 
3093 void brcms_c_reset(struct brcms_c_info *wlc)
3094 {
3095 	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
3096 
3097 	/* slurp up hw mac counters before core reset */
3098 	brcms_c_statsupd(wlc);
3099 
3100 	/* reset our snapshot of macstat counters */
3101 	memset(wlc->core->macstat_snapshot, 0, sizeof(struct macstat));
3102 
3103 	brcms_b_reset(wlc->hw);
3104 }
3105 
3106 void brcms_c_init_scb(struct scb *scb)
3107 {
3108 	int i;
3109 
3110 	memset(scb, 0, sizeof(struct scb));
3111 	scb->flags = SCB_WMECAP | SCB_HTCAP;
3112 	for (i = 0; i < NUMPRIO; i++) {
3113 		scb->seqnum[i] = 0;
3114 	}
3115 
3116 	scb->magic = SCB_MAGIC;
3117 }
3118 
3119 /* d11 core init
3120  *   reset PSM
3121  *   download ucode/PCM
3122  *   let ucode run to suspended
3123  *   download ucode inits
3124  *   config other core registers
3125  *   init dma
3126  */
3127 static void brcms_b_coreinit(struct brcms_c_info *wlc)
3128 {
3129 	struct brcms_hardware *wlc_hw = wlc->hw;
3130 	struct bcma_device *core = wlc_hw->d11core;
3131 	u32 bcnint_us;
3132 	uint i = 0;
3133 	bool fifosz_fixup = false;
3134 	int err = 0;
3135 	u16 buf[NFIFO];
3136 	struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
3137 
3138 	brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
3139 
3140 	/* reset PSM */
3141 	brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
3142 
3143 	brcms_ucode_download(wlc_hw);
3144 	/*
3145 	 * FIFOSZ fixup. driver wants to controls the fifo allocation.
3146 	 */
3147 	fifosz_fixup = true;
3148 
3149 	/* let the PSM run to the suspended state, set mode to BSS STA */
3150 	bcma_write32(core, D11REGOFFS(macintstatus), -1);
3151 	brcms_b_mctrl(wlc_hw, ~0,
3152 		       (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
3153 
3154 	/* wait for ucode to self-suspend after auto-init */
3155 	SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
3156 		   MI_MACSSPNDD) == 0), 1000 * 1000);
3157 	if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
3158 		brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
3159 			  "suspend!\n", wlc_hw->unit);
3160 
3161 	brcms_c_gpio_init(wlc);
3162 
3163 	bcma_aread32(core, BCMA_IOST);
3164 
3165 	if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
3166 		if (BRCMS_ISNPHY(wlc_hw->band))
3167 			brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
3168 		else
3169 			brcms_err(core, "%s: wl%d: unsupported phy in corerev"
3170 				  " %d\n", __func__, wlc_hw->unit,
3171 				  wlc_hw->corerev);
3172 	} else if (D11REV_IS(wlc_hw->corerev, 24)) {
3173 		if (BRCMS_ISLCNPHY(wlc_hw->band))
3174 			brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
3175 		else
3176 			brcms_err(core, "%s: wl%d: unsupported phy in corerev"
3177 				  " %d\n", __func__, wlc_hw->unit,
3178 				  wlc_hw->corerev);
3179 	} else {
3180 		brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
3181 			  __func__, wlc_hw->unit, wlc_hw->corerev);
3182 	}
3183 
3184 	/* For old ucode, txfifo sizes needs to be modified(increased) */
3185 	if (fifosz_fixup)
3186 		brcms_b_corerev_fifofixup(wlc_hw);
3187 
3188 	/* check txfifo allocations match between ucode and driver */
3189 	buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
3190 	if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
3191 		i = TX_AC_BE_FIFO;
3192 		err = -1;
3193 	}
3194 	buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
3195 	if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
3196 		i = TX_AC_VI_FIFO;
3197 		err = -1;
3198 	}
3199 	buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
3200 	buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
3201 	buf[TX_AC_BK_FIFO] &= 0xff;
3202 	if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
3203 		i = TX_AC_BK_FIFO;
3204 		err = -1;
3205 	}
3206 	if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
3207 		i = TX_AC_VO_FIFO;
3208 		err = -1;
3209 	}
3210 	buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
3211 	buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
3212 	buf[TX_BCMC_FIFO] &= 0xff;
3213 	if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
3214 		i = TX_BCMC_FIFO;
3215 		err = -1;
3216 	}
3217 	if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
3218 		i = TX_ATIM_FIFO;
3219 		err = -1;
3220 	}
3221 	if (err != 0)
3222 		brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
3223 			  " driver size %d index %d\n", buf[i],
3224 			  wlc_hw->xmtfifo_sz[i], i);
3225 
3226 	/* make sure we can still talk to the mac */
3227 	WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
3228 
3229 	/* band-specific inits done by wlc_bsinit() */
3230 
3231 	/* Set up frame burst size and antenna swap threshold init values */
3232 	brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
3233 	brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
3234 
3235 	/* enable one rx interrupt per received frame */
3236 	bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
3237 
3238 	/* set the station mode (BSS STA) */
3239 	brcms_b_mctrl(wlc_hw,
3240 		       (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
3241 		       (MCTL_INFRA | MCTL_DISCARD_PMQ));
3242 
3243 	/* set up Beacon interval */
3244 	bcnint_us = 0x8000 << 10;
3245 	bcma_write32(core, D11REGOFFS(tsf_cfprep),
3246 		     (bcnint_us << CFPREP_CBI_SHIFT));
3247 	bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
3248 	bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
3249 
3250 	/* write interrupt mask */
3251 	bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
3252 		     DEF_RXINTMASK);
3253 
3254 	/* allow the MAC to control the PHY clock (dynamic on/off) */
3255 	brcms_b_macphyclk_set(wlc_hw, ON);
3256 
3257 	/* program dynamic clock control fast powerup delay register */
3258 	wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
3259 	bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
3260 
3261 	/* tell the ucode the corerev */
3262 	brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
3263 
3264 	/* tell the ucode MAC capabilities */
3265 	brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
3266 			   (u16) (wlc_hw->machwcap & 0xffff));
3267 	brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
3268 			   (u16) ((wlc_hw->
3269 				      machwcap >> 16) & 0xffff));
3270 
3271 	/* write retry limits to SCR, this done after PSM init */
3272 	bcma_write32(core, D11REGOFFS(objaddr),
3273 		     OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
3274 	(void)bcma_read32(core, D11REGOFFS(objaddr));
3275 	bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
3276 	bcma_write32(core, D11REGOFFS(objaddr),
3277 		     OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
3278 	(void)bcma_read32(core, D11REGOFFS(objaddr));
3279 	bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
3280 
3281 	/* write rate fallback retry limits */
3282 	brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
3283 	brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
3284 
3285 	bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
3286 	bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
3287 
3288 	/* init the tx dma engines */
3289 	for (i = 0; i < NFIFO; i++) {
3290 		if (wlc_hw->di[i])
3291 			dma_txinit(wlc_hw->di[i]);
3292 	}
3293 
3294 	/* init the rx dma engine(s) and post receive buffers */
3295 	dma_rxinit(wlc_hw->di[RX_FIFO]);
3296 	dma_rxfill(wlc_hw->di[RX_FIFO]);
3297 }
3298 
3299 static void brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec)
3300 {
3301 	u32 macintmask;
3302 	bool fastclk;
3303 	struct brcms_c_info *wlc = wlc_hw->wlc;
3304 
3305 	/* request FAST clock if not on */
3306 	fastclk = wlc_hw->forcefastclk;
3307 	if (!fastclk)
3308 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
3309 
3310 	/* disable interrupts */
3311 	macintmask = brcms_intrsoff(wlc->wl);
3312 
3313 	/* set up the specified band and chanspec */
3314 	brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
3315 	wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3316 
3317 	/* do one-time phy inits and calibration */
3318 	wlc_phy_cal_init(wlc_hw->band->pi);
3319 
3320 	/* core-specific initialization */
3321 	brcms_b_coreinit(wlc);
3322 
3323 	/* band-specific inits */
3324 	brcms_b_bsinit(wlc, chanspec);
3325 
3326 	/* restore macintmask */
3327 	brcms_intrsrestore(wlc->wl, macintmask);
3328 
3329 	/* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
3330 	 * is suspended and brcms_c_enable_mac() will clear this override bit.
3331 	 */
3332 	mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
3333 
3334 	/*
3335 	 * initialize mac_suspend_depth to 1 to match ucode
3336 	 * initial suspended state
3337 	 */
3338 	wlc_hw->mac_suspend_depth = 1;
3339 
3340 	/* restore the clk */
3341 	if (!fastclk)
3342 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
3343 }
3344 
3345 static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
3346 				     u16 chanspec)
3347 {
3348 	/* Save our copy of the chanspec */
3349 	wlc->chanspec = chanspec;
3350 
3351 	/* Set the chanspec and power limits for this locale */
3352 	brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
3353 
3354 	if (wlc->stf->ss_algosel_auto)
3355 		brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
3356 					    chanspec);
3357 
3358 	brcms_c_stf_ss_update(wlc, wlc->band);
3359 }
3360 
3361 static void
3362 brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
3363 {
3364 	brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
3365 		wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
3366 		(bool) (wlc->pub->_n_enab & SUPPORT_11N),
3367 		brcms_chspec_bw(wlc->default_bss->chanspec),
3368 		wlc->stf->txstreams);
3369 }
3370 
3371 /* derive wlc->band->basic_rate[] table from 'rateset' */
3372 static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
3373 			      struct brcms_c_rateset *rateset)
3374 {
3375 	u8 rate;
3376 	u8 mandatory;
3377 	u8 cck_basic = 0;
3378 	u8 ofdm_basic = 0;
3379 	u8 *br = wlc->band->basic_rate;
3380 	uint i;
3381 
3382 	/* incoming rates are in 500kbps units as in 802.11 Supported Rates */
3383 	memset(br, 0, BRCM_MAXRATE + 1);
3384 
3385 	/* For each basic rate in the rates list, make an entry in the
3386 	 * best basic lookup.
3387 	 */
3388 	for (i = 0; i < rateset->count; i++) {
3389 		/* only make an entry for a basic rate */
3390 		if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
3391 			continue;
3392 
3393 		/* mask off basic bit */
3394 		rate = (rateset->rates[i] & BRCMS_RATE_MASK);
3395 
3396 		if (rate > BRCM_MAXRATE) {
3397 			brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
3398 				  "invalid rate 0x%X in rate set\n",
3399 				  rateset->rates[i]);
3400 			continue;
3401 		}
3402 
3403 		br[rate] = rate;
3404 	}
3405 
3406 	/* The rate lookup table now has non-zero entries for each
3407 	 * basic rate, equal to the basic rate: br[basicN] = basicN
3408 	 *
3409 	 * To look up the best basic rate corresponding to any
3410 	 * particular rate, code can use the basic_rate table
3411 	 * like this
3412 	 *
3413 	 * basic_rate = wlc->band->basic_rate[tx_rate]
3414 	 *
3415 	 * Make sure there is a best basic rate entry for
3416 	 * every rate by walking up the table from low rates
3417 	 * to high, filling in holes in the lookup table
3418 	 */
3419 
3420 	for (i = 0; i < wlc->band->hw_rateset.count; i++) {
3421 		rate = wlc->band->hw_rateset.rates[i];
3422 
3423 		if (br[rate] != 0) {
3424 			/* This rate is a basic rate.
3425 			 * Keep track of the best basic rate so far by
3426 			 * modulation type.
3427 			 */
3428 			if (is_ofdm_rate(rate))
3429 				ofdm_basic = rate;
3430 			else
3431 				cck_basic = rate;
3432 
3433 			continue;
3434 		}
3435 
3436 		/* This rate is not a basic rate so figure out the
3437 		 * best basic rate less than this rate and fill in
3438 		 * the hole in the table
3439 		 */
3440 
3441 		br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
3442 
3443 		if (br[rate] != 0)
3444 			continue;
3445 
3446 		if (is_ofdm_rate(rate)) {
3447 			/*
3448 			 * In 11g and 11a, the OFDM mandatory rates
3449 			 * are 6, 12, and 24 Mbps
3450 			 */
3451 			if (rate >= BRCM_RATE_24M)
3452 				mandatory = BRCM_RATE_24M;
3453 			else if (rate >= BRCM_RATE_12M)
3454 				mandatory = BRCM_RATE_12M;
3455 			else
3456 				mandatory = BRCM_RATE_6M;
3457 		} else {
3458 			/* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
3459 			mandatory = rate;
3460 		}
3461 
3462 		br[rate] = mandatory;
3463 	}
3464 }
3465 
3466 static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
3467 				     u16 chanspec)
3468 {
3469 	struct brcms_c_rateset default_rateset;
3470 	uint parkband;
3471 	uint i, band_order[2];
3472 
3473 	/*
3474 	 * We might have been bandlocked during down and the chip
3475 	 * power-cycled (hibernate). Figure out the right band to park on
3476 	 */
3477 	if (wlc->bandlocked || wlc->pub->_nbands == 1) {
3478 		/* updated in brcms_c_bandlock() */
3479 		parkband = wlc->band->bandunit;
3480 		band_order[0] = band_order[1] = parkband;
3481 	} else {
3482 		/* park on the band of the specified chanspec */
3483 		parkband = chspec_bandunit(chanspec);
3484 
3485 		/* order so that parkband initialize last */
3486 		band_order[0] = parkband ^ 1;
3487 		band_order[1] = parkband;
3488 	}
3489 
3490 	/* make each band operational, software state init */
3491 	for (i = 0; i < wlc->pub->_nbands; i++) {
3492 		uint j = band_order[i];
3493 
3494 		wlc->band = wlc->bandstate[j];
3495 
3496 		brcms_default_rateset(wlc, &default_rateset);
3497 
3498 		/* fill in hw_rate */
3499 		brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
3500 				   false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
3501 				   (bool) (wlc->pub->_n_enab & SUPPORT_11N));
3502 
3503 		/* init basic rate lookup */
3504 		brcms_c_rate_lookup_init(wlc, &default_rateset);
3505 	}
3506 
3507 	/* sync up phy/radio chanspec */
3508 	brcms_c_set_phy_chanspec(wlc, chanspec);
3509 }
3510 
3511 /*
3512  * Set or clear filtering related maccontrol bits based on
3513  * specified filter flags
3514  */
3515 void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
3516 {
3517 	u32 promisc_bits = 0;
3518 
3519 	wlc->filter_flags = filter_flags;
3520 
3521 	if (filter_flags & FIF_OTHER_BSS)
3522 		promisc_bits |= MCTL_PROMISC;
3523 
3524 	if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
3525 		promisc_bits |= MCTL_BCNS_PROMISC;
3526 
3527 	if (filter_flags & FIF_FCSFAIL)
3528 		promisc_bits |= MCTL_KEEPBADFCS;
3529 
3530 	if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
3531 		promisc_bits |= MCTL_KEEPCONTROL;
3532 
3533 	brcms_b_mctrl(wlc->hw,
3534 		MCTL_PROMISC | MCTL_BCNS_PROMISC |
3535 		MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
3536 		promisc_bits);
3537 }
3538 
3539 /*
3540  * ucode, hwmac update
3541  *    Channel dependent updates for ucode and hw
3542  */
3543 static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
3544 {
3545 	/* enable or disable any active IBSSs depending on whether or not
3546 	 * we are on the home channel
3547 	 */
3548 	if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
3549 		if (wlc->pub->associated) {
3550 			/*
3551 			 * BMAC_NOTE: This is something that should be fixed
3552 			 * in ucode inits. I think that the ucode inits set
3553 			 * up the bcn templates and shm values with a bogus
3554 			 * beacon. This should not be done in the inits. If
3555 			 * ucode needs to set up a beacon for testing, the
3556 			 * test routines should write it down, not expect the
3557 			 * inits to populate a bogus beacon.
3558 			 */
3559 			if (BRCMS_PHY_11N_CAP(wlc->band))
3560 				brcms_b_write_shm(wlc->hw,
3561 						M_BCN_TXTSF_OFFSET, 0);
3562 		}
3563 	} else {
3564 		/* disable an active IBSS if we are not on the home channel */
3565 	}
3566 }
3567 
3568 static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
3569 				   u8 basic_rate)
3570 {
3571 	u8 phy_rate, index;
3572 	u8 basic_phy_rate, basic_index;
3573 	u16 dir_table, basic_table;
3574 	u16 basic_ptr;
3575 
3576 	/* Shared memory address for the table we are reading */
3577 	dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
3578 
3579 	/* Shared memory address for the table we are writing */
3580 	basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
3581 
3582 	/*
3583 	 * for a given rate, the LS-nibble of the PLCP SIGNAL field is
3584 	 * the index into the rate table.
3585 	 */
3586 	phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
3587 	basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
3588 	index = phy_rate & 0xf;
3589 	basic_index = basic_phy_rate & 0xf;
3590 
3591 	/* Find the SHM pointer to the ACK rate entry by looking in the
3592 	 * Direct-map Table
3593 	 */
3594 	basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
3595 
3596 	/* Update the SHM BSS-basic-rate-set mapping table with the pointer
3597 	 * to the correct basic rate for the given incoming rate
3598 	 */
3599 	brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
3600 }
3601 
3602 static const struct brcms_c_rateset *
3603 brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
3604 {
3605 	const struct brcms_c_rateset *rs_dflt;
3606 
3607 	if (BRCMS_PHY_11N_CAP(wlc->band)) {
3608 		if (wlc->band->bandtype == BRCM_BAND_5G)
3609 			rs_dflt = &ofdm_mimo_rates;
3610 		else
3611 			rs_dflt = &cck_ofdm_mimo_rates;
3612 	} else if (wlc->band->gmode)
3613 		rs_dflt = &cck_ofdm_rates;
3614 	else
3615 		rs_dflt = &cck_rates;
3616 
3617 	return rs_dflt;
3618 }
3619 
3620 static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
3621 {
3622 	const struct brcms_c_rateset *rs_dflt;
3623 	struct brcms_c_rateset rs;
3624 	u8 rate, basic_rate;
3625 	uint i;
3626 
3627 	rs_dflt = brcms_c_rateset_get_hwrs(wlc);
3628 
3629 	brcms_c_rateset_copy(rs_dflt, &rs);
3630 	brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
3631 
3632 	/* walk the phy rate table and update SHM basic rate lookup table */
3633 	for (i = 0; i < rs.count; i++) {
3634 		rate = rs.rates[i] & BRCMS_RATE_MASK;
3635 
3636 		/* for a given rate brcms_basic_rate returns the rate at
3637 		 * which a response ACK/CTS should be sent.
3638 		 */
3639 		basic_rate = brcms_basic_rate(wlc, rate);
3640 		if (basic_rate == 0)
3641 			/* This should only happen if we are using a
3642 			 * restricted rateset.
3643 			 */
3644 			basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
3645 
3646 		brcms_c_write_rate_shm(wlc, rate, basic_rate);
3647 	}
3648 }
3649 
3650 /* band-specific init */
3651 static void brcms_c_bsinit(struct brcms_c_info *wlc)
3652 {
3653 	brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
3654 		       wlc->pub->unit, wlc->band->bandunit);
3655 
3656 	/* write ucode ACK/CTS rate table */
3657 	brcms_c_set_ratetable(wlc);
3658 
3659 	/* update some band specific mac configuration */
3660 	brcms_c_ucode_mac_upd(wlc);
3661 
3662 	/* init antenna selection */
3663 	brcms_c_antsel_init(wlc->asi);
3664 
3665 }
3666 
3667 /* formula:  IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
3668 static int
3669 brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
3670 		   bool writeToShm)
3671 {
3672 	int idle_busy_ratio_x_16 = 0;
3673 	uint offset =
3674 	    isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
3675 	    M_TX_IDLE_BUSY_RATIO_X_16_CCK;
3676 	if (duty_cycle > 100 || duty_cycle < 0) {
3677 		brcms_err(wlc->hw->d11core,
3678 			  "wl%d:  duty cycle value off limit\n",
3679 			  wlc->pub->unit);
3680 		return -EINVAL;
3681 	}
3682 	if (duty_cycle)
3683 		idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
3684 	/* Only write to shared memory  when wl is up */
3685 	if (writeToShm)
3686 		brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
3687 
3688 	if (isOFDM)
3689 		wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
3690 	else
3691 		wlc->tx_duty_cycle_cck = (u16) duty_cycle;
3692 
3693 	return 0;
3694 }
3695 
3696 /* push sw hps and wake state through hardware */
3697 static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
3698 {
3699 	u32 v1, v2;
3700 	bool hps;
3701 	bool awake_before;
3702 
3703 	hps = brcms_c_ps_allowed(wlc);
3704 
3705 	brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
3706 			   hps);
3707 
3708 	v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
3709 	v2 = MCTL_WAKE;
3710 	if (hps)
3711 		v2 |= MCTL_HPS;
3712 
3713 	brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
3714 
3715 	awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
3716 
3717 	if (!awake_before)
3718 		brcms_b_wait_for_wake(wlc->hw);
3719 }
3720 
3721 /*
3722  * Write this BSS config's MAC address to core.
3723  * Updates RXE match engine.
3724  */
3725 static void brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
3726 {
3727 	struct brcms_c_info *wlc = bsscfg->wlc;
3728 
3729 	/* enter the MAC addr into the RXE match registers */
3730 	brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, wlc->pub->cur_etheraddr);
3731 
3732 	brcms_c_ampdu_macaddr_upd(wlc);
3733 }
3734 
3735 /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
3736  * Updates RXE match engine.
3737  */
3738 static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
3739 {
3740 	/* we need to update BSSID in RXE match registers */
3741 	brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
3742 }
3743 
3744 void brcms_c_set_ssid(struct brcms_c_info *wlc, u8 *ssid, size_t ssid_len)
3745 {
3746 	u8 len = min_t(u8, sizeof(wlc->bsscfg->SSID), ssid_len);
3747 	memset(wlc->bsscfg->SSID, 0, sizeof(wlc->bsscfg->SSID));
3748 
3749 	memcpy(wlc->bsscfg->SSID, ssid, len);
3750 	wlc->bsscfg->SSID_len = len;
3751 }
3752 
3753 static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
3754 {
3755 	wlc_hw->shortslot = shortslot;
3756 
3757 	if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
3758 		brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
3759 		brcms_b_update_slot_timing(wlc_hw, shortslot);
3760 		brcms_c_enable_mac(wlc_hw->wlc);
3761 	}
3762 }
3763 
3764 /*
3765  * Suspend the MAC and update the slot timing
3766  * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
3767  */
3768 static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
3769 {
3770 	/* use the override if it is set */
3771 	if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
3772 		shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
3773 
3774 	if (wlc->shortslot == shortslot)
3775 		return;
3776 
3777 	wlc->shortslot = shortslot;
3778 
3779 	brcms_b_set_shortslot(wlc->hw, shortslot);
3780 }
3781 
3782 static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
3783 {
3784 	if (wlc->home_chanspec != chanspec) {
3785 		wlc->home_chanspec = chanspec;
3786 
3787 		if (wlc->pub->associated)
3788 			wlc->bsscfg->current_bss.chanspec = chanspec;
3789 	}
3790 }
3791 
3792 void
3793 brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
3794 		      bool mute_tx, struct txpwr_limits *txpwr)
3795 {
3796 	uint bandunit;
3797 
3798 	brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
3799 			   chanspec);
3800 
3801 	wlc_hw->chanspec = chanspec;
3802 
3803 	/* Switch bands if necessary */
3804 	if (wlc_hw->_nbands > 1) {
3805 		bandunit = chspec_bandunit(chanspec);
3806 		if (wlc_hw->band->bandunit != bandunit) {
3807 			/* brcms_b_setband disables other bandunit,
3808 			 *  use light band switch if not up yet
3809 			 */
3810 			if (wlc_hw->up) {
3811 				wlc_phy_chanspec_radio_set(wlc_hw->
3812 							   bandstate[bandunit]->
3813 							   pi, chanspec);
3814 				brcms_b_setband(wlc_hw, bandunit, chanspec);
3815 			} else {
3816 				brcms_c_setxband(wlc_hw, bandunit);
3817 			}
3818 		}
3819 	}
3820 
3821 	wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
3822 
3823 	if (!wlc_hw->up) {
3824 		if (wlc_hw->clk)
3825 			wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
3826 						  chanspec);
3827 		wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
3828 	} else {
3829 		wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
3830 		wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
3831 
3832 		/* Update muting of the channel */
3833 		brcms_b_mute(wlc_hw, mute_tx);
3834 	}
3835 }
3836 
3837 /* switch to and initialize new band */
3838 static void brcms_c_setband(struct brcms_c_info *wlc,
3839 					   uint bandunit)
3840 {
3841 	wlc->band = wlc->bandstate[bandunit];
3842 
3843 	if (!wlc->pub->up)
3844 		return;
3845 
3846 	/* wait for at least one beacon before entering sleeping state */
3847 	brcms_c_set_ps_ctrl(wlc);
3848 
3849 	/* band-specific initializations */
3850 	brcms_c_bsinit(wlc);
3851 }
3852 
3853 static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
3854 {
3855 	uint bandunit;
3856 	u16 old_chanspec = wlc->chanspec;
3857 
3858 	if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
3859 		brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
3860 			  wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
3861 		return;
3862 	}
3863 
3864 	/* Switch bands if necessary */
3865 	if (wlc->pub->_nbands > 1) {
3866 		bandunit = chspec_bandunit(chanspec);
3867 		if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
3868 			if (wlc->bandlocked) {
3869 				brcms_err(wlc->hw->d11core,
3870 					  "wl%d: %s: chspec %d band is locked!\n",
3871 					  wlc->pub->unit, __func__,
3872 					  CHSPEC_CHANNEL(chanspec));
3873 				return;
3874 			}
3875 			/*
3876 			 * should the setband call come after the
3877 			 * brcms_b_chanspec() ? if the setband updates
3878 			 * (brcms_c_bsinit) use low level calls to inspect and
3879 			 * set state, the state inspected may be from the wrong
3880 			 * band, or the following brcms_b_set_chanspec() may
3881 			 * undo the work.
3882 			 */
3883 			brcms_c_setband(wlc, bandunit);
3884 		}
3885 	}
3886 
3887 	/* sync up phy/radio chanspec */
3888 	brcms_c_set_phy_chanspec(wlc, chanspec);
3889 
3890 	/* init antenna selection */
3891 	if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
3892 		brcms_c_antsel_init(wlc->asi);
3893 
3894 		/* Fix the hardware rateset based on bw.
3895 		 * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
3896 		 */
3897 		brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
3898 			wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
3899 	}
3900 
3901 	/* update some mac configuration since chanspec changed */
3902 	brcms_c_ucode_mac_upd(wlc);
3903 }
3904 
3905 /*
3906  * This function changes the phytxctl for beacon based on current
3907  * beacon ratespec AND txant setting as per this table:
3908  *  ratespec     CCK		ant = wlc->stf->txant
3909  *		OFDM		ant = 3
3910  */
3911 void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
3912 				       u32 bcn_rspec)
3913 {
3914 	u16 phyctl;
3915 	u16 phytxant = wlc->stf->phytxant;
3916 	u16 mask = PHY_TXC_ANT_MASK;
3917 
3918 	/* for non-siso rates or default setting, use the available chains */
3919 	if (BRCMS_PHY_11N_CAP(wlc->band))
3920 		phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
3921 
3922 	phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
3923 	phyctl = (phyctl & ~mask) | phytxant;
3924 	brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
3925 }
3926 
3927 /*
3928  * centralized protection config change function to simplify debugging, no
3929  * consistency checking this should be called only on changes to avoid overhead
3930  * in periodic function
3931  */
3932 void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
3933 {
3934 	/*
3935 	 * Cannot use brcms_dbg_* here because this function is called
3936 	 * before wlc is sufficiently initialized.
3937 	 */
3938 	BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
3939 
3940 	switch (idx) {
3941 	case BRCMS_PROT_G_SPEC:
3942 		wlc->protection->_g = (bool) val;
3943 		break;
3944 	case BRCMS_PROT_G_OVR:
3945 		wlc->protection->g_override = (s8) val;
3946 		break;
3947 	case BRCMS_PROT_G_USER:
3948 		wlc->protection->gmode_user = (u8) val;
3949 		break;
3950 	case BRCMS_PROT_OVERLAP:
3951 		wlc->protection->overlap = (s8) val;
3952 		break;
3953 	case BRCMS_PROT_N_USER:
3954 		wlc->protection->nmode_user = (s8) val;
3955 		break;
3956 	case BRCMS_PROT_N_CFG:
3957 		wlc->protection->n_cfg = (s8) val;
3958 		break;
3959 	case BRCMS_PROT_N_CFG_OVR:
3960 		wlc->protection->n_cfg_override = (s8) val;
3961 		break;
3962 	case BRCMS_PROT_N_NONGF:
3963 		wlc->protection->nongf = (bool) val;
3964 		break;
3965 	case BRCMS_PROT_N_NONGF_OVR:
3966 		wlc->protection->nongf_override = (s8) val;
3967 		break;
3968 	case BRCMS_PROT_N_PAM_OVR:
3969 		wlc->protection->n_pam_override = (s8) val;
3970 		break;
3971 	case BRCMS_PROT_N_OBSS:
3972 		wlc->protection->n_obss = (bool) val;
3973 		break;
3974 
3975 	default:
3976 		break;
3977 	}
3978 
3979 }
3980 
3981 static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
3982 {
3983 	if (wlc->pub->up) {
3984 		brcms_c_update_beacon(wlc);
3985 		brcms_c_update_probe_resp(wlc, true);
3986 	}
3987 }
3988 
3989 static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
3990 {
3991 	wlc->stf->ldpc = val;
3992 
3993 	if (wlc->pub->up) {
3994 		brcms_c_update_beacon(wlc);
3995 		brcms_c_update_probe_resp(wlc, true);
3996 		wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
3997 	}
3998 }
3999 
4000 void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
4001 		       const struct ieee80211_tx_queue_params *params,
4002 		       bool suspend)
4003 {
4004 	int i;
4005 	struct shm_acparams acp_shm;
4006 	u16 *shm_entry;
4007 
4008 	/* Only apply params if the core is out of reset and has clocks */
4009 	if (!wlc->clk) {
4010 		brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
4011 			  wlc->pub->unit, __func__);
4012 		return;
4013 	}
4014 
4015 	memset(&acp_shm, 0, sizeof(struct shm_acparams));
4016 	/* fill in shm ac params struct */
4017 	acp_shm.txop = params->txop;
4018 	/* convert from units of 32us to us for ucode */
4019 	wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
4020 	    EDCF_TXOP2USEC(acp_shm.txop);
4021 	acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
4022 
4023 	if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
4024 	    && acp_shm.aifs < EDCF_AIFSN_MAX)
4025 		acp_shm.aifs++;
4026 
4027 	if (acp_shm.aifs < EDCF_AIFSN_MIN
4028 	    || acp_shm.aifs > EDCF_AIFSN_MAX) {
4029 		brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
4030 			  "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
4031 	} else {
4032 		acp_shm.cwmin = params->cw_min;
4033 		acp_shm.cwmax = params->cw_max;
4034 		acp_shm.cwcur = acp_shm.cwmin;
4035 		acp_shm.bslots =
4036 			bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
4037 			acp_shm.cwcur;
4038 		acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
4039 		/* Indicate the new params to the ucode */
4040 		acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
4041 						  wme_ac2fifo[aci] *
4042 						  M_EDCF_QLEN +
4043 						  M_EDCF_STATUS_OFF));
4044 		acp_shm.status |= WME_STATUS_NEWAC;
4045 
4046 		/* Fill in shm acparam table */
4047 		shm_entry = (u16 *) &acp_shm;
4048 		for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
4049 			brcms_b_write_shm(wlc->hw,
4050 					  M_EDCF_QINFO +
4051 					  wme_ac2fifo[aci] * M_EDCF_QLEN + i,
4052 					  *shm_entry++);
4053 	}
4054 
4055 	if (suspend)
4056 		brcms_c_suspend_mac_and_wait(wlc);
4057 
4058 	brcms_c_update_beacon(wlc);
4059 	brcms_c_update_probe_resp(wlc, false);
4060 
4061 	if (suspend)
4062 		brcms_c_enable_mac(wlc);
4063 }
4064 
4065 static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
4066 {
4067 	u16 aci;
4068 	int i_ac;
4069 	struct ieee80211_tx_queue_params txq_pars;
4070 	static const struct edcf_acparam default_edcf_acparams[] = {
4071 		 {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
4072 		 {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
4073 		 {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
4074 		 {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
4075 	}; /* ucode needs these parameters during its initialization */
4076 	const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
4077 
4078 	for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
4079 		/* find out which ac this set of params applies to */
4080 		aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
4081 
4082 		/* fill in shm ac params struct */
4083 		txq_pars.txop = edcf_acp->TXOP;
4084 		txq_pars.aifs = edcf_acp->ACI;
4085 
4086 		/* CWmin = 2^(ECWmin) - 1 */
4087 		txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
4088 		/* CWmax = 2^(ECWmax) - 1 */
4089 		txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
4090 					    >> EDCF_ECWMAX_SHIFT);
4091 		brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
4092 	}
4093 
4094 	if (suspend) {
4095 		brcms_c_suspend_mac_and_wait(wlc);
4096 		brcms_c_enable_mac(wlc);
4097 	}
4098 }
4099 
4100 static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
4101 {
4102 	/* Don't start the timer if HWRADIO feature is disabled */
4103 	if (wlc->radio_monitor)
4104 		return;
4105 
4106 	wlc->radio_monitor = true;
4107 	brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
4108 	brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
4109 }
4110 
4111 static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
4112 {
4113 	if (!wlc->radio_monitor)
4114 		return true;
4115 
4116 	wlc->radio_monitor = false;
4117 	brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
4118 	return brcms_del_timer(wlc->radio_timer);
4119 }
4120 
4121 /* read hwdisable state and propagate to wlc flag */
4122 static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
4123 {
4124 	if (wlc->pub->hw_off)
4125 		return;
4126 
4127 	if (brcms_b_radio_read_hwdisabled(wlc->hw))
4128 		mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4129 	else
4130 		mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
4131 }
4132 
4133 /* update hwradio status and return it */
4134 bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
4135 {
4136 	brcms_c_radio_hwdisable_upd(wlc);
4137 
4138 	return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
4139 			true : false;
4140 }
4141 
4142 /* periodical query hw radio button while driver is "down" */
4143 static void brcms_c_radio_timer(void *arg)
4144 {
4145 	struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4146 
4147 	if (brcms_deviceremoved(wlc)) {
4148 		brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4149 			  wlc->pub->unit, __func__);
4150 		brcms_down(wlc->wl);
4151 		return;
4152 	}
4153 
4154 	brcms_c_radio_hwdisable_upd(wlc);
4155 }
4156 
4157 /* common low-level watchdog code */
4158 static void brcms_b_watchdog(struct brcms_c_info *wlc)
4159 {
4160 	struct brcms_hardware *wlc_hw = wlc->hw;
4161 
4162 	if (!wlc_hw->up)
4163 		return;
4164 
4165 	/* increment second count */
4166 	wlc_hw->now++;
4167 
4168 	/* Check for FIFO error interrupts */
4169 	brcms_b_fifoerrors(wlc_hw);
4170 
4171 	/* make sure RX dma has buffers */
4172 	dma_rxfill(wlc->hw->di[RX_FIFO]);
4173 
4174 	wlc_phy_watchdog(wlc_hw->band->pi);
4175 }
4176 
4177 /* common watchdog code */
4178 static void brcms_c_watchdog(struct brcms_c_info *wlc)
4179 {
4180 	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
4181 
4182 	if (!wlc->pub->up)
4183 		return;
4184 
4185 	if (brcms_deviceremoved(wlc)) {
4186 		brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
4187 			  wlc->pub->unit, __func__);
4188 		brcms_down(wlc->wl);
4189 		return;
4190 	}
4191 
4192 	/* increment second count */
4193 	wlc->pub->now++;
4194 
4195 	brcms_c_radio_hwdisable_upd(wlc);
4196 	/* if radio is disable, driver may be down, quit here */
4197 	if (wlc->pub->radio_disabled)
4198 		return;
4199 
4200 	brcms_b_watchdog(wlc);
4201 
4202 	/*
4203 	 * occasionally sample mac stat counters to
4204 	 * detect 16-bit counter wrap
4205 	 */
4206 	if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
4207 		brcms_c_statsupd(wlc);
4208 
4209 	if (BRCMS_ISNPHY(wlc->band) &&
4210 	    ((wlc->pub->now - wlc->tempsense_lasttime) >=
4211 	     BRCMS_TEMPSENSE_PERIOD)) {
4212 		wlc->tempsense_lasttime = wlc->pub->now;
4213 		brcms_c_tempsense_upd(wlc);
4214 	}
4215 }
4216 
4217 static void brcms_c_watchdog_by_timer(void *arg)
4218 {
4219 	struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
4220 
4221 	brcms_c_watchdog(wlc);
4222 }
4223 
4224 static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
4225 {
4226 	wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
4227 		wlc, "watchdog");
4228 	if (!wlc->wdtimer) {
4229 		wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for wdtimer "
4230 			  "failed\n", unit);
4231 		goto fail;
4232 	}
4233 
4234 	wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
4235 		wlc, "radio");
4236 	if (!wlc->radio_timer) {
4237 		wiphy_err(wlc->wiphy, "wl%d:  wl_init_timer for radio_timer "
4238 			  "failed\n", unit);
4239 		goto fail;
4240 	}
4241 
4242 	return true;
4243 
4244  fail:
4245 	return false;
4246 }
4247 
4248 /*
4249  * Initialize brcms_c_info default values ...
4250  * may get overrides later in this function
4251  */
4252 static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
4253 {
4254 	int i;
4255 
4256 	/* Save our copy of the chanspec */
4257 	wlc->chanspec = ch20mhz_chspec(1);
4258 
4259 	/* various 802.11g modes */
4260 	wlc->shortslot = false;
4261 	wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
4262 
4263 	brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
4264 	brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
4265 
4266 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
4267 			       BRCMS_PROTECTION_AUTO);
4268 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
4269 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
4270 			       BRCMS_PROTECTION_AUTO);
4271 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
4272 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
4273 
4274 	brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
4275 			       BRCMS_PROTECTION_CTL_OVERLAP);
4276 
4277 	/* 802.11g draft 4.0 NonERP elt advertisement */
4278 	wlc->include_legacy_erp = true;
4279 
4280 	wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
4281 	wlc->stf->txant = ANT_TX_DEF;
4282 
4283 	wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
4284 
4285 	wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
4286 	for (i = 0; i < NFIFO; i++)
4287 		wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
4288 	wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
4289 
4290 	/* default rate fallback retry limits */
4291 	wlc->SFBL = RETRY_SHORT_FB;
4292 	wlc->LFBL = RETRY_LONG_FB;
4293 
4294 	/* default mac retry limits */
4295 	wlc->SRL = RETRY_SHORT_DEF;
4296 	wlc->LRL = RETRY_LONG_DEF;
4297 
4298 	/* WME QoS mode is Auto by default */
4299 	wlc->pub->_ampdu = AMPDU_AGG_HOST;
4300 }
4301 
4302 static uint brcms_c_attach_module(struct brcms_c_info *wlc)
4303 {
4304 	uint err = 0;
4305 	uint unit;
4306 	unit = wlc->pub->unit;
4307 
4308 	wlc->asi = brcms_c_antsel_attach(wlc);
4309 	if (wlc->asi == NULL) {
4310 		wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
4311 			  "failed\n", unit);
4312 		err = 44;
4313 		goto fail;
4314 	}
4315 
4316 	wlc->ampdu = brcms_c_ampdu_attach(wlc);
4317 	if (wlc->ampdu == NULL) {
4318 		wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
4319 			  "failed\n", unit);
4320 		err = 50;
4321 		goto fail;
4322 	}
4323 
4324 	if ((brcms_c_stf_attach(wlc) != 0)) {
4325 		wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
4326 			  "failed\n", unit);
4327 		err = 68;
4328 		goto fail;
4329 	}
4330  fail:
4331 	return err;
4332 }
4333 
4334 struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
4335 {
4336 	return wlc->pub;
4337 }
4338 
4339 /* low level attach
4340  *    run backplane attach, init nvram
4341  *    run phy attach
4342  *    initialize software state for each core and band
4343  *    put the whole chip in reset(driver down state), no clock
4344  */
4345 static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
4346 			  uint unit, bool piomode)
4347 {
4348 	struct brcms_hardware *wlc_hw;
4349 	uint err = 0;
4350 	uint j;
4351 	bool wme = false;
4352 	struct shared_phy_params sha_params;
4353 	struct wiphy *wiphy = wlc->wiphy;
4354 	struct pci_dev *pcidev = core->bus->host_pci;
4355 	struct ssb_sprom *sprom = &core->bus->sprom;
4356 
4357 	if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
4358 		brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4359 			       pcidev->vendor,
4360 			       pcidev->device);
4361 	else
4362 		brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
4363 			       core->bus->boardinfo.vendor,
4364 			       core->bus->boardinfo.type);
4365 
4366 	wme = true;
4367 
4368 	wlc_hw = wlc->hw;
4369 	wlc_hw->wlc = wlc;
4370 	wlc_hw->unit = unit;
4371 	wlc_hw->band = wlc_hw->bandstate[0];
4372 	wlc_hw->_piomode = piomode;
4373 
4374 	/* populate struct brcms_hardware with default values  */
4375 	brcms_b_info_init(wlc_hw);
4376 
4377 	/*
4378 	 * Do the hardware portion of the attach. Also initialize software
4379 	 * state that depends on the particular hardware we are running.
4380 	 */
4381 	wlc_hw->sih = ai_attach(core->bus);
4382 	if (wlc_hw->sih == NULL) {
4383 		wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
4384 			  unit);
4385 		err = 11;
4386 		goto fail;
4387 	}
4388 
4389 	/* verify again the device is supported */
4390 	if (!brcms_c_chipmatch(core)) {
4391 		wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
4392 			 unit);
4393 		err = 12;
4394 		goto fail;
4395 	}
4396 
4397 	if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
4398 		wlc_hw->vendorid = pcidev->vendor;
4399 		wlc_hw->deviceid = pcidev->device;
4400 	} else {
4401 		wlc_hw->vendorid = core->bus->boardinfo.vendor;
4402 		wlc_hw->deviceid = core->bus->boardinfo.type;
4403 	}
4404 
4405 	wlc_hw->d11core = core;
4406 	wlc_hw->corerev = core->id.rev;
4407 
4408 	/* validate chip, chiprev and corerev */
4409 	if (!brcms_c_isgoodchip(wlc_hw)) {
4410 		err = 13;
4411 		goto fail;
4412 	}
4413 
4414 	/* initialize power control registers */
4415 	ai_clkctl_init(wlc_hw->sih);
4416 
4417 	/* request fastclock and force fastclock for the rest of attach
4418 	 * bring the d11 core out of reset.
4419 	 *   For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
4420 	 *   is still false; But it will be called again inside wlc_corereset,
4421 	 *   after d11 is out of reset.
4422 	 */
4423 	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
4424 	brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4425 
4426 	if (!brcms_b_validate_chip_access(wlc_hw)) {
4427 		wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
4428 			"failed\n", unit);
4429 		err = 14;
4430 		goto fail;
4431 	}
4432 
4433 	/* get the board rev, used just below */
4434 	j = sprom->board_rev;
4435 	/* promote srom boardrev of 0xFF to 1 */
4436 	if (j == BOARDREV_PROMOTABLE)
4437 		j = BOARDREV_PROMOTED;
4438 	wlc_hw->boardrev = (u16) j;
4439 	if (!brcms_c_validboardtype(wlc_hw)) {
4440 		wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
4441 			  "board type (0x%x)" " or revision level (0x%x)\n",
4442 			  unit, ai_get_boardtype(wlc_hw->sih),
4443 			  wlc_hw->boardrev);
4444 		err = 15;
4445 		goto fail;
4446 	}
4447 	wlc_hw->sromrev = sprom->revision;
4448 	wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
4449 	wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
4450 
4451 	if (wlc_hw->boardflags & BFL_NOPLLDOWN)
4452 		brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
4453 
4454 	/* check device id(srom, nvram etc.) to set bands */
4455 	if (wlc_hw->deviceid == BCM43224_D11N_ID ||
4456 	    wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
4457 	    wlc_hw->deviceid == BCM43224_CHIP_ID)
4458 		/* Dualband boards */
4459 		wlc_hw->_nbands = 2;
4460 	else
4461 		wlc_hw->_nbands = 1;
4462 
4463 	if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
4464 		wlc_hw->_nbands = 1;
4465 
4466 	/* BMAC_NOTE: remove init of pub values when brcms_c_attach()
4467 	 * unconditionally does the init of these values
4468 	 */
4469 	wlc->vendorid = wlc_hw->vendorid;
4470 	wlc->deviceid = wlc_hw->deviceid;
4471 	wlc->pub->sih = wlc_hw->sih;
4472 	wlc->pub->corerev = wlc_hw->corerev;
4473 	wlc->pub->sromrev = wlc_hw->sromrev;
4474 	wlc->pub->boardrev = wlc_hw->boardrev;
4475 	wlc->pub->boardflags = wlc_hw->boardflags;
4476 	wlc->pub->boardflags2 = wlc_hw->boardflags2;
4477 	wlc->pub->_nbands = wlc_hw->_nbands;
4478 
4479 	wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
4480 
4481 	if (wlc_hw->physhim == NULL) {
4482 		wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
4483 			"failed\n", unit);
4484 		err = 25;
4485 		goto fail;
4486 	}
4487 
4488 	/* pass all the parameters to wlc_phy_shared_attach in one struct */
4489 	sha_params.sih = wlc_hw->sih;
4490 	sha_params.physhim = wlc_hw->physhim;
4491 	sha_params.unit = unit;
4492 	sha_params.corerev = wlc_hw->corerev;
4493 	sha_params.vid = wlc_hw->vendorid;
4494 	sha_params.did = wlc_hw->deviceid;
4495 	sha_params.chip = ai_get_chip_id(wlc_hw->sih);
4496 	sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
4497 	sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
4498 	sha_params.sromrev = wlc_hw->sromrev;
4499 	sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
4500 	sha_params.boardrev = wlc_hw->boardrev;
4501 	sha_params.boardflags = wlc_hw->boardflags;
4502 	sha_params.boardflags2 = wlc_hw->boardflags2;
4503 
4504 	/* alloc and save pointer to shared phy state area */
4505 	wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
4506 	if (!wlc_hw->phy_sh) {
4507 		err = 16;
4508 		goto fail;
4509 	}
4510 
4511 	/* initialize software state for each core and band */
4512 	for (j = 0; j < wlc_hw->_nbands; j++) {
4513 		/*
4514 		 * band0 is always 2.4Ghz
4515 		 * band1, if present, is 5Ghz
4516 		 */
4517 
4518 		brcms_c_setxband(wlc_hw, j);
4519 
4520 		wlc_hw->band->bandunit = j;
4521 		wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4522 		wlc->band->bandunit = j;
4523 		wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
4524 		wlc->core->coreidx = core->core_index;
4525 
4526 		wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
4527 		wlc_hw->machwcap_backup = wlc_hw->machwcap;
4528 
4529 		/* init tx fifo size */
4530 		WARN_ON(wlc_hw->corerev < XMTFIFOTBL_STARTREV ||
4531 			(wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
4532 				ARRAY_SIZE(xmtfifo_sz));
4533 		wlc_hw->xmtfifo_sz =
4534 		    xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
4535 		WARN_ON(!wlc_hw->xmtfifo_sz[0]);
4536 
4537 		/* Get a phy for this band */
4538 		wlc_hw->band->pi =
4539 			wlc_phy_attach(wlc_hw->phy_sh, core,
4540 				       wlc_hw->band->bandtype,
4541 				       wlc->wiphy);
4542 		if (wlc_hw->band->pi == NULL) {
4543 			wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
4544 				  "attach failed\n", unit);
4545 			err = 17;
4546 			goto fail;
4547 		}
4548 
4549 		wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
4550 
4551 		wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
4552 				       &wlc_hw->band->phyrev,
4553 				       &wlc_hw->band->radioid,
4554 				       &wlc_hw->band->radiorev);
4555 		wlc_hw->band->abgphy_encore =
4556 		    wlc_phy_get_encore(wlc_hw->band->pi);
4557 		wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
4558 		wlc_hw->band->core_flags =
4559 		    wlc_phy_get_coreflags(wlc_hw->band->pi);
4560 
4561 		/* verify good phy_type & supported phy revision */
4562 		if (BRCMS_ISNPHY(wlc_hw->band)) {
4563 			if (NCONF_HAS(wlc_hw->band->phyrev))
4564 				goto good_phy;
4565 			else
4566 				goto bad_phy;
4567 		} else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
4568 			if (LCNCONF_HAS(wlc_hw->band->phyrev))
4569 				goto good_phy;
4570 			else
4571 				goto bad_phy;
4572 		} else {
4573  bad_phy:
4574 			wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
4575 				  "phy type/rev (%d/%d)\n", unit,
4576 				  wlc_hw->band->phytype, wlc_hw->band->phyrev);
4577 			err = 18;
4578 			goto fail;
4579 		}
4580 
4581  good_phy:
4582 		/*
4583 		 * BMAC_NOTE: wlc->band->pi should not be set below and should
4584 		 * be done in the high level attach. However we can not make
4585 		 * that change until all low level access is changed to
4586 		 * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
4587 		 * keeping wlc_hw->band->pi as well for incremental update of
4588 		 * low level fns, and cut over low only init when all fns
4589 		 * updated.
4590 		 */
4591 		wlc->band->pi = wlc_hw->band->pi;
4592 		wlc->band->phytype = wlc_hw->band->phytype;
4593 		wlc->band->phyrev = wlc_hw->band->phyrev;
4594 		wlc->band->radioid = wlc_hw->band->radioid;
4595 		wlc->band->radiorev = wlc_hw->band->radiorev;
4596 		brcms_dbg_info(core, "wl%d: phy %u/%u radio %x/%u\n", unit,
4597 			       wlc->band->phytype, wlc->band->phyrev,
4598 			       wlc->band->radioid, wlc->band->radiorev);
4599 		/* default contention windows size limits */
4600 		wlc_hw->band->CWmin = APHY_CWMIN;
4601 		wlc_hw->band->CWmax = PHY_CWMAX;
4602 
4603 		if (!brcms_b_attach_dmapio(wlc, j, wme)) {
4604 			err = 19;
4605 			goto fail;
4606 		}
4607 	}
4608 
4609 	/* disable core to match driver "down" state */
4610 	brcms_c_coredisable(wlc_hw);
4611 
4612 	/* Match driver "down" state */
4613 	bcma_host_pci_down(wlc_hw->d11core->bus);
4614 
4615 	/* turn off pll and xtal to match driver "down" state */
4616 	brcms_b_xtal(wlc_hw, OFF);
4617 
4618 	/* *******************************************************************
4619 	 * The hardware is in the DOWN state at this point. D11 core
4620 	 * or cores are in reset with clocks off, and the board PLLs
4621 	 * are off if possible.
4622 	 *
4623 	 * Beyond this point, wlc->sbclk == false and chip registers
4624 	 * should not be touched.
4625 	 *********************************************************************
4626 	 */
4627 
4628 	/* init etheraddr state variables */
4629 	brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
4630 
4631 	if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
4632 	    is_zero_ether_addr(wlc_hw->etheraddr)) {
4633 		wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
4634 			  unit);
4635 		err = 22;
4636 		goto fail;
4637 	}
4638 
4639 	brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
4640 		       wlc_hw->deviceid, wlc_hw->_nbands,
4641 		       ai_get_boardtype(wlc_hw->sih));
4642 
4643 	return err;
4644 
4645  fail:
4646 	wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
4647 		  err);
4648 	return err;
4649 }
4650 
4651 static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
4652 {
4653 	int aa;
4654 	uint unit;
4655 	int bandtype;
4656 	struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
4657 
4658 	unit = wlc->pub->unit;
4659 	bandtype = wlc->band->bandtype;
4660 
4661 	/* get antennas available */
4662 	if (bandtype == BRCM_BAND_5G)
4663 		aa = sprom->ant_available_a;
4664 	else
4665 		aa = sprom->ant_available_bg;
4666 
4667 	if ((aa < 1) || (aa > 15)) {
4668 		wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
4669 			  " srom (0x%x), using 3\n", unit, __func__, aa);
4670 		aa = 3;
4671 	}
4672 
4673 	/* reset the defaults if we have a single antenna */
4674 	if (aa == 1) {
4675 		wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
4676 		wlc->stf->txant = ANT_TX_FORCE_0;
4677 	} else if (aa == 2) {
4678 		wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
4679 		wlc->stf->txant = ANT_TX_FORCE_1;
4680 	} else {
4681 	}
4682 
4683 	/* Compute Antenna Gain */
4684 	if (bandtype == BRCM_BAND_5G)
4685 		wlc->band->antgain = sprom->antenna_gain.a1;
4686 	else
4687 		wlc->band->antgain = sprom->antenna_gain.a0;
4688 
4689 	return true;
4690 }
4691 
4692 static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
4693 {
4694 	u16 chanspec;
4695 	struct brcms_band *band;
4696 	struct brcms_bss_info *bi = wlc->default_bss;
4697 
4698 	/* init default and target BSS with some sane initial values */
4699 	memset(bi, 0, sizeof(*bi));
4700 	bi->beacon_period = BEACON_INTERVAL_DEFAULT;
4701 
4702 	/* fill the default channel as the first valid channel
4703 	 * starting from the 2G channels
4704 	 */
4705 	chanspec = ch20mhz_chspec(1);
4706 	wlc->home_chanspec = bi->chanspec = chanspec;
4707 
4708 	/* find the band of our default channel */
4709 	band = wlc->band;
4710 	if (wlc->pub->_nbands > 1 &&
4711 	    band->bandunit != chspec_bandunit(chanspec))
4712 		band = wlc->bandstate[OTHERBANDUNIT(wlc)];
4713 
4714 	/* init bss rates to the band specific default rate set */
4715 	brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
4716 		band->bandtype, false, BRCMS_RATE_MASK_FULL,
4717 		(bool) (wlc->pub->_n_enab & SUPPORT_11N),
4718 		brcms_chspec_bw(chanspec), wlc->stf->txstreams);
4719 
4720 	if (wlc->pub->_n_enab & SUPPORT_11N)
4721 		bi->flags |= BRCMS_BSS_HT;
4722 }
4723 
4724 static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
4725 {
4726 	uint i;
4727 	struct brcms_band *band;
4728 
4729 	for (i = 0; i < wlc->pub->_nbands; i++) {
4730 		band = wlc->bandstate[i];
4731 		if (band->bandtype == BRCM_BAND_5G) {
4732 			if ((bwcap == BRCMS_N_BW_40ALL)
4733 			    || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
4734 				band->mimo_cap_40 = true;
4735 			else
4736 				band->mimo_cap_40 = false;
4737 		} else {
4738 			if (bwcap == BRCMS_N_BW_40ALL)
4739 				band->mimo_cap_40 = true;
4740 			else
4741 				band->mimo_cap_40 = false;
4742 		}
4743 	}
4744 }
4745 
4746 static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
4747 {
4748 	/* free timer state */
4749 	if (wlc->wdtimer) {
4750 		brcms_free_timer(wlc->wdtimer);
4751 		wlc->wdtimer = NULL;
4752 	}
4753 	if (wlc->radio_timer) {
4754 		brcms_free_timer(wlc->radio_timer);
4755 		wlc->radio_timer = NULL;
4756 	}
4757 }
4758 
4759 static void brcms_c_detach_module(struct brcms_c_info *wlc)
4760 {
4761 	if (wlc->asi) {
4762 		brcms_c_antsel_detach(wlc->asi);
4763 		wlc->asi = NULL;
4764 	}
4765 
4766 	if (wlc->ampdu) {
4767 		brcms_c_ampdu_detach(wlc->ampdu);
4768 		wlc->ampdu = NULL;
4769 	}
4770 
4771 	brcms_c_stf_detach(wlc);
4772 }
4773 
4774 /*
4775  * low level detach
4776  */
4777 static void brcms_b_detach(struct brcms_c_info *wlc)
4778 {
4779 	uint i;
4780 	struct brcms_hw_band *band;
4781 	struct brcms_hardware *wlc_hw = wlc->hw;
4782 
4783 	brcms_b_detach_dmapio(wlc_hw);
4784 
4785 	band = wlc_hw->band;
4786 	for (i = 0; i < wlc_hw->_nbands; i++) {
4787 		if (band->pi) {
4788 			/* Detach this band's phy */
4789 			wlc_phy_detach(band->pi);
4790 			band->pi = NULL;
4791 		}
4792 		band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
4793 	}
4794 
4795 	/* Free shared phy state */
4796 	kfree(wlc_hw->phy_sh);
4797 
4798 	wlc_phy_shim_detach(wlc_hw->physhim);
4799 
4800 	if (wlc_hw->sih) {
4801 		ai_detach(wlc_hw->sih);
4802 		wlc_hw->sih = NULL;
4803 	}
4804 }
4805 
4806 /*
4807  * Return a count of the number of driver callbacks still pending.
4808  *
4809  * General policy is that brcms_c_detach can only dealloc/free software states.
4810  * It can NOT touch hardware registers since the d11core may be in reset and
4811  * clock may not be available.
4812  * One exception is sb register access, which is possible if crystal is turned
4813  * on after "down" state, driver should avoid software timer with the exception
4814  * of radio_monitor.
4815  */
4816 uint brcms_c_detach(struct brcms_c_info *wlc)
4817 {
4818 	uint callbacks;
4819 
4820 	if (wlc == NULL)
4821 		return 0;
4822 
4823 	brcms_b_detach(wlc);
4824 
4825 	/* delete software timers */
4826 	callbacks = 0;
4827 	if (!brcms_c_radio_monitor_stop(wlc))
4828 		callbacks++;
4829 
4830 	brcms_c_channel_mgr_detach(wlc->cmi);
4831 
4832 	brcms_c_timers_deinit(wlc);
4833 
4834 	brcms_c_detach_module(wlc);
4835 
4836 	brcms_c_detach_mfree(wlc);
4837 	return callbacks;
4838 }
4839 
4840 /* update state that depends on the current value of "ap" */
4841 static void brcms_c_ap_upd(struct brcms_c_info *wlc)
4842 {
4843 	/* STA-BSS; short capable */
4844 	wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
4845 }
4846 
4847 /* Initialize just the hardware when coming out of POR or S3/S5 system states */
4848 static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
4849 {
4850 	if (wlc_hw->wlc->pub->hw_up)
4851 		return;
4852 
4853 	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
4854 
4855 	/*
4856 	 * Enable pll and xtal, initialize the power control registers,
4857 	 * and force fastclock for the remainder of brcms_c_up().
4858 	 */
4859 	brcms_b_xtal(wlc_hw, ON);
4860 	ai_clkctl_init(wlc_hw->sih);
4861 	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
4862 
4863 	/*
4864 	 * TODO: test suspend/resume
4865 	 *
4866 	 * AI chip doesn't restore bar0win2 on
4867 	 * hibernation/resume, need sw fixup
4868 	 */
4869 
4870 	/*
4871 	 * Inform phy that a POR reset has occurred so
4872 	 * it does a complete phy init
4873 	 */
4874 	wlc_phy_por_inform(wlc_hw->band->pi);
4875 
4876 	wlc_hw->ucode_loaded = false;
4877 	wlc_hw->wlc->pub->hw_up = true;
4878 
4879 	if ((wlc_hw->boardflags & BFL_FEM)
4880 	    && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
4881 		if (!
4882 		    (wlc_hw->boardrev >= 0x1250
4883 		     && (wlc_hw->boardflags & BFL_FEM_BT)))
4884 			ai_epa_4313war(wlc_hw->sih);
4885 	}
4886 }
4887 
4888 static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
4889 {
4890 	brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
4891 
4892 	/*
4893 	 * Enable pll and xtal, initialize the power control registers,
4894 	 * and force fastclock for the remainder of brcms_c_up().
4895 	 */
4896 	brcms_b_xtal(wlc_hw, ON);
4897 	ai_clkctl_init(wlc_hw->sih);
4898 	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
4899 
4900 	/*
4901 	 * Configure pci/pcmcia here instead of in brcms_c_attach()
4902 	 * to allow mfg hotswap:  down, hotswap (chip power cycle), up.
4903 	 */
4904 	bcma_host_pci_irq_ctl(wlc_hw->d11core->bus, wlc_hw->d11core,
4905 			      true);
4906 
4907 	/*
4908 	 * Need to read the hwradio status here to cover the case where the
4909 	 * system is loaded with the hw radio disabled. We do not want to
4910 	 * bring the driver up in this case.
4911 	 */
4912 	if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
4913 		/* put SB PCI in down state again */
4914 		bcma_host_pci_down(wlc_hw->d11core->bus);
4915 		brcms_b_xtal(wlc_hw, OFF);
4916 		return -ENOMEDIUM;
4917 	}
4918 
4919 	bcma_host_pci_up(wlc_hw->d11core->bus);
4920 
4921 	/* reset the d11 core */
4922 	brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
4923 
4924 	return 0;
4925 }
4926 
4927 static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
4928 {
4929 	wlc_hw->up = true;
4930 	wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
4931 
4932 	/* FULLY enable dynamic power control and d11 core interrupt */
4933 	brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
4934 	brcms_intrson(wlc_hw->wlc->wl);
4935 	return 0;
4936 }
4937 
4938 /*
4939  * Write WME tunable parameters for retransmit/max rate
4940  * from wlc struct to ucode
4941  */
4942 static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
4943 {
4944 	int ac;
4945 
4946 	/* Need clock to do this */
4947 	if (!wlc->clk)
4948 		return;
4949 
4950 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
4951 		brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
4952 				  wlc->wme_retries[ac]);
4953 }
4954 
4955 /* make interface operational */
4956 int brcms_c_up(struct brcms_c_info *wlc)
4957 {
4958 	struct ieee80211_channel *ch;
4959 
4960 	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
4961 
4962 	/* HW is turned off so don't try to access it */
4963 	if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
4964 		return -ENOMEDIUM;
4965 
4966 	if (!wlc->pub->hw_up) {
4967 		brcms_b_hw_up(wlc->hw);
4968 		wlc->pub->hw_up = true;
4969 	}
4970 
4971 	if ((wlc->pub->boardflags & BFL_FEM)
4972 	    && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
4973 		if (wlc->pub->boardrev >= 0x1250
4974 		    && (wlc->pub->boardflags & BFL_FEM_BT))
4975 			brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
4976 				MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
4977 		else
4978 			brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
4979 				    MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
4980 	}
4981 
4982 	/*
4983 	 * Need to read the hwradio status here to cover the case where the
4984 	 * system is loaded with the hw radio disabled. We do not want to bring
4985 	 * the driver up in this case. If radio is disabled, abort up, lower
4986 	 * power, start radio timer and return 0(for NDIS) don't call
4987 	 * radio_update to avoid looping brcms_c_up.
4988 	 *
4989 	 * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
4990 	 */
4991 	if (!wlc->pub->radio_disabled) {
4992 		int status = brcms_b_up_prep(wlc->hw);
4993 		if (status == -ENOMEDIUM) {
4994 			if (!mboolisset
4995 			    (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
4996 				struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
4997 				mboolset(wlc->pub->radio_disabled,
4998 					 WL_RADIO_HW_DISABLE);
4999 				if (bsscfg->type == BRCMS_TYPE_STATION ||
5000 				    bsscfg->type == BRCMS_TYPE_ADHOC)
5001 					brcms_err(wlc->hw->d11core,
5002 						  "wl%d: up: rfdisable -> "
5003 						  "bsscfg_disable()\n",
5004 						   wlc->pub->unit);
5005 			}
5006 		}
5007 	}
5008 
5009 	if (wlc->pub->radio_disabled) {
5010 		brcms_c_radio_monitor_start(wlc);
5011 		return 0;
5012 	}
5013 
5014 	/* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
5015 	wlc->clk = true;
5016 
5017 	brcms_c_radio_monitor_stop(wlc);
5018 
5019 	/* Set EDCF hostflags */
5020 	brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
5021 
5022 	brcms_init(wlc->wl);
5023 	wlc->pub->up = true;
5024 
5025 	if (wlc->bandinit_pending) {
5026 		ch = wlc->pub->ieee_hw->conf.chandef.chan;
5027 		brcms_c_suspend_mac_and_wait(wlc);
5028 		brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
5029 		wlc->bandinit_pending = false;
5030 		brcms_c_enable_mac(wlc);
5031 	}
5032 
5033 	brcms_b_up_finish(wlc->hw);
5034 
5035 	/* Program the TX wme params with the current settings */
5036 	brcms_c_wme_retries_write(wlc);
5037 
5038 	/* start one second watchdog timer */
5039 	brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
5040 	wlc->WDarmed = true;
5041 
5042 	/* ensure antenna config is up to date */
5043 	brcms_c_stf_phy_txant_upd(wlc);
5044 	/* ensure LDPC config is in sync */
5045 	brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
5046 
5047 	return 0;
5048 }
5049 
5050 static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
5051 {
5052 	bool dev_gone;
5053 	uint callbacks = 0;
5054 
5055 	if (!wlc_hw->up)
5056 		return callbacks;
5057 
5058 	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5059 
5060 	/* disable interrupts */
5061 	if (dev_gone)
5062 		wlc_hw->wlc->macintmask = 0;
5063 	else {
5064 		/* now disable interrupts */
5065 		brcms_intrsoff(wlc_hw->wlc->wl);
5066 
5067 		/* ensure we're running on the pll clock again */
5068 		brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
5069 	}
5070 	/* down phy at the last of this stage */
5071 	callbacks += wlc_phy_down(wlc_hw->band->pi);
5072 
5073 	return callbacks;
5074 }
5075 
5076 static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
5077 {
5078 	uint callbacks = 0;
5079 	bool dev_gone;
5080 
5081 	if (!wlc_hw->up)
5082 		return callbacks;
5083 
5084 	wlc_hw->up = false;
5085 	wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
5086 
5087 	dev_gone = brcms_deviceremoved(wlc_hw->wlc);
5088 
5089 	if (dev_gone) {
5090 		wlc_hw->sbclk = false;
5091 		wlc_hw->clk = false;
5092 		wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
5093 
5094 		/* reclaim any posted packets */
5095 		brcms_c_flushqueues(wlc_hw->wlc);
5096 	} else {
5097 
5098 		/* Reset and disable the core */
5099 		if (bcma_core_is_enabled(wlc_hw->d11core)) {
5100 			if (bcma_read32(wlc_hw->d11core,
5101 					D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
5102 				brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
5103 			callbacks += brcms_reset(wlc_hw->wlc->wl);
5104 			brcms_c_coredisable(wlc_hw);
5105 		}
5106 
5107 		/* turn off primary xtal and pll */
5108 		if (!wlc_hw->noreset) {
5109 			bcma_host_pci_down(wlc_hw->d11core->bus);
5110 			brcms_b_xtal(wlc_hw, OFF);
5111 		}
5112 	}
5113 
5114 	return callbacks;
5115 }
5116 
5117 /*
5118  * Mark the interface nonoperational, stop the software mechanisms,
5119  * disable the hardware, free any transient buffer state.
5120  * Return a count of the number of driver callbacks still pending.
5121  */
5122 uint brcms_c_down(struct brcms_c_info *wlc)
5123 {
5124 
5125 	uint callbacks = 0;
5126 	int i;
5127 
5128 	brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
5129 
5130 	/* check if we are already in the going down path */
5131 	if (wlc->going_down) {
5132 		brcms_err(wlc->hw->d11core,
5133 			  "wl%d: %s: Driver going down so return\n",
5134 			  wlc->pub->unit, __func__);
5135 		return 0;
5136 	}
5137 	if (!wlc->pub->up)
5138 		return callbacks;
5139 
5140 	wlc->going_down = true;
5141 
5142 	callbacks += brcms_b_bmac_down_prep(wlc->hw);
5143 
5144 	brcms_deviceremoved(wlc);
5145 
5146 	/* Call any registered down handlers */
5147 	for (i = 0; i < BRCMS_MAXMODULES; i++) {
5148 		if (wlc->modulecb[i].down_fn)
5149 			callbacks +=
5150 			    wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
5151 	}
5152 
5153 	/* cancel the watchdog timer */
5154 	if (wlc->WDarmed) {
5155 		if (!brcms_del_timer(wlc->wdtimer))
5156 			callbacks++;
5157 		wlc->WDarmed = false;
5158 	}
5159 
5160 	wlc->pub->up = false;
5161 
5162 	wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
5163 
5164 	callbacks += brcms_b_down_finish(wlc->hw);
5165 
5166 	/* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
5167 	wlc->clk = false;
5168 
5169 	wlc->going_down = false;
5170 	return callbacks;
5171 }
5172 
5173 /* Set the current gmode configuration */
5174 int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
5175 {
5176 	int ret = 0;
5177 	uint i;
5178 	struct brcms_c_rateset rs;
5179 	/* Default to 54g Auto */
5180 	/* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
5181 	s8 shortslot = BRCMS_SHORTSLOT_AUTO;
5182 	bool ofdm_basic = false;	/* Make 6, 12, and 24 basic rates */
5183 	struct brcms_band *band;
5184 
5185 	/* if N-support is enabled, allow Gmode set as long as requested
5186 	 * Gmode is not GMODE_LEGACY_B
5187 	 */
5188 	if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
5189 		return -ENOTSUPP;
5190 
5191 	/* verify that we are dealing with 2G band and grab the band pointer */
5192 	if (wlc->band->bandtype == BRCM_BAND_2G)
5193 		band = wlc->band;
5194 	else if ((wlc->pub->_nbands > 1) &&
5195 		 (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
5196 		band = wlc->bandstate[OTHERBANDUNIT(wlc)];
5197 	else
5198 		return -EINVAL;
5199 
5200 	/* update configuration value */
5201 	if (config)
5202 		brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
5203 
5204 	/* Clear rateset override */
5205 	memset(&rs, 0, sizeof(rs));
5206 
5207 	switch (gmode) {
5208 	case GMODE_LEGACY_B:
5209 		shortslot = BRCMS_SHORTSLOT_OFF;
5210 		brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
5211 
5212 		break;
5213 
5214 	case GMODE_LRS:
5215 		break;
5216 
5217 	case GMODE_AUTO:
5218 		/* Accept defaults */
5219 		break;
5220 
5221 	case GMODE_ONLY:
5222 		ofdm_basic = true;
5223 		break;
5224 
5225 	case GMODE_PERFORMANCE:
5226 		shortslot = BRCMS_SHORTSLOT_ON;
5227 		ofdm_basic = true;
5228 		break;
5229 
5230 	default:
5231 		/* Error */
5232 		brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
5233 			  wlc->pub->unit, __func__, gmode);
5234 		return -ENOTSUPP;
5235 	}
5236 
5237 	band->gmode = gmode;
5238 
5239 	wlc->shortslot_override = shortslot;
5240 
5241 	/* Use the default 11g rateset */
5242 	if (!rs.count)
5243 		brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
5244 
5245 	if (ofdm_basic) {
5246 		for (i = 0; i < rs.count; i++) {
5247 			if (rs.rates[i] == BRCM_RATE_6M
5248 			    || rs.rates[i] == BRCM_RATE_12M
5249 			    || rs.rates[i] == BRCM_RATE_24M)
5250 				rs.rates[i] |= BRCMS_RATE_FLAG;
5251 		}
5252 	}
5253 
5254 	/* Set default bss rateset */
5255 	wlc->default_bss->rateset.count = rs.count;
5256 	memcpy(wlc->default_bss->rateset.rates, rs.rates,
5257 	       sizeof(wlc->default_bss->rateset.rates));
5258 
5259 	return ret;
5260 }
5261 
5262 int brcms_c_set_nmode(struct brcms_c_info *wlc)
5263 {
5264 	uint i;
5265 	s32 nmode = AUTO;
5266 
5267 	if (wlc->stf->txstreams == WL_11N_3x3)
5268 		nmode = WL_11N_3x3;
5269 	else
5270 		nmode = WL_11N_2x2;
5271 
5272 	/* force GMODE_AUTO if NMODE is ON */
5273 	brcms_c_set_gmode(wlc, GMODE_AUTO, true);
5274 	if (nmode == WL_11N_3x3)
5275 		wlc->pub->_n_enab = SUPPORT_HT;
5276 	else
5277 		wlc->pub->_n_enab = SUPPORT_11N;
5278 	wlc->default_bss->flags |= BRCMS_BSS_HT;
5279 	/* add the mcs rates to the default and hw ratesets */
5280 	brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
5281 			      wlc->stf->txstreams);
5282 	for (i = 0; i < wlc->pub->_nbands; i++)
5283 		memcpy(wlc->bandstate[i]->hw_rateset.mcs,
5284 		       wlc->default_bss->rateset.mcs, MCSSET_LEN);
5285 
5286 	return 0;
5287 }
5288 
5289 static int
5290 brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
5291 			     struct brcms_c_rateset *rs_arg)
5292 {
5293 	struct brcms_c_rateset rs, new;
5294 	uint bandunit;
5295 
5296 	memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
5297 
5298 	/* check for bad count value */
5299 	if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
5300 		return -EINVAL;
5301 
5302 	/* try the current band */
5303 	bandunit = wlc->band->bandunit;
5304 	memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5305 	if (brcms_c_rate_hwrs_filter_sort_validate
5306 	    (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
5307 	     wlc->stf->txstreams))
5308 		goto good;
5309 
5310 	/* try the other band */
5311 	if (brcms_is_mband_unlocked(wlc)) {
5312 		bandunit = OTHERBANDUNIT(wlc);
5313 		memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
5314 		if (brcms_c_rate_hwrs_filter_sort_validate(&new,
5315 						       &wlc->
5316 						       bandstate[bandunit]->
5317 						       hw_rateset, true,
5318 						       wlc->stf->txstreams))
5319 			goto good;
5320 	}
5321 
5322 	return -EBADE;
5323 
5324  good:
5325 	/* apply new rateset */
5326 	memcpy(&wlc->default_bss->rateset, &new,
5327 	       sizeof(struct brcms_c_rateset));
5328 	memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
5329 	       sizeof(struct brcms_c_rateset));
5330 	return 0;
5331 }
5332 
5333 static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
5334 {
5335 	wlc_phy_ofdm_rateset_war(wlc->band->pi, false);
5336 }
5337 
5338 int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
5339 {
5340 	u16 chspec = ch20mhz_chspec(channel);
5341 
5342 	if (channel > MAXCHANNEL)
5343 		return -EINVAL;
5344 
5345 	if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
5346 		return -EINVAL;
5347 
5348 
5349 	if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
5350 		if (wlc->band->bandunit != chspec_bandunit(chspec))
5351 			wlc->bandinit_pending = true;
5352 		else
5353 			wlc->bandinit_pending = false;
5354 	}
5355 
5356 	wlc->default_bss->chanspec = chspec;
5357 	/* brcms_c_BSSinit() will sanitize the rateset before
5358 	 * using it.. */
5359 	if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
5360 		brcms_c_set_home_chanspec(wlc, chspec);
5361 		brcms_c_suspend_mac_and_wait(wlc);
5362 		brcms_c_set_chanspec(wlc, chspec);
5363 		brcms_c_enable_mac(wlc);
5364 	}
5365 	return 0;
5366 }
5367 
5368 int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
5369 {
5370 	int ac;
5371 
5372 	if (srl < 1 || srl > RETRY_SHORT_MAX ||
5373 	    lrl < 1 || lrl > RETRY_SHORT_MAX)
5374 		return -EINVAL;
5375 
5376 	wlc->SRL = srl;
5377 	wlc->LRL = lrl;
5378 
5379 	brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
5380 
5381 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
5382 		wlc->wme_retries[ac] =	SFIELD(wlc->wme_retries[ac],
5383 					       EDCF_SHORT,  wlc->SRL);
5384 		wlc->wme_retries[ac] =	SFIELD(wlc->wme_retries[ac],
5385 					       EDCF_LONG, wlc->LRL);
5386 	}
5387 	brcms_c_wme_retries_write(wlc);
5388 
5389 	return 0;
5390 }
5391 
5392 void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
5393 				 struct brcm_rateset *currs)
5394 {
5395 	struct brcms_c_rateset *rs;
5396 
5397 	if (wlc->pub->associated)
5398 		rs = &wlc->bsscfg->current_bss.rateset;
5399 	else
5400 		rs = &wlc->default_bss->rateset;
5401 
5402 	/* Copy only legacy rateset section */
5403 	currs->count = rs->count;
5404 	memcpy(&currs->rates, &rs->rates, rs->count);
5405 }
5406 
5407 int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
5408 {
5409 	struct brcms_c_rateset internal_rs;
5410 	int bcmerror;
5411 
5412 	if (rs->count > BRCMS_NUMRATES)
5413 		return -ENOBUFS;
5414 
5415 	memset(&internal_rs, 0, sizeof(internal_rs));
5416 
5417 	/* Copy only legacy rateset section */
5418 	internal_rs.count = rs->count;
5419 	memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
5420 
5421 	/* merge rateset coming in with the current mcsset */
5422 	if (wlc->pub->_n_enab & SUPPORT_11N) {
5423 		struct brcms_bss_info *mcsset_bss;
5424 		if (wlc->pub->associated)
5425 			mcsset_bss = &wlc->bsscfg->current_bss;
5426 		else
5427 			mcsset_bss = wlc->default_bss;
5428 		memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
5429 		       MCSSET_LEN);
5430 	}
5431 
5432 	bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
5433 	if (!bcmerror)
5434 		brcms_c_ofdm_rateset_war(wlc);
5435 
5436 	return bcmerror;
5437 }
5438 
5439 static void brcms_c_time_lock(struct brcms_c_info *wlc)
5440 {
5441 	bcma_set32(wlc->hw->d11core, D11REGOFFS(maccontrol), MCTL_TBTTHOLD);
5442 	/* Commit the write */
5443 	bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
5444 }
5445 
5446 static void brcms_c_time_unlock(struct brcms_c_info *wlc)
5447 {
5448 	bcma_mask32(wlc->hw->d11core, D11REGOFFS(maccontrol), ~MCTL_TBTTHOLD);
5449 	/* Commit the write */
5450 	bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
5451 }
5452 
5453 int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
5454 {
5455 	u32 bcnint_us;
5456 
5457 	if (period == 0)
5458 		return -EINVAL;
5459 
5460 	wlc->default_bss->beacon_period = period;
5461 
5462 	bcnint_us = period << 10;
5463 	brcms_c_time_lock(wlc);
5464 	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfprep),
5465 		     (bcnint_us << CFPREP_CBI_SHIFT));
5466 	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_cfpstart), bcnint_us);
5467 	brcms_c_time_unlock(wlc);
5468 
5469 	return 0;
5470 }
5471 
5472 u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
5473 {
5474 	return wlc->band->phytype;
5475 }
5476 
5477 void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
5478 {
5479 	wlc->shortslot_override = sslot_override;
5480 
5481 	/*
5482 	 * shortslot is an 11g feature, so no more work if we are
5483 	 * currently on the 5G band
5484 	 */
5485 	if (wlc->band->bandtype == BRCM_BAND_5G)
5486 		return;
5487 
5488 	if (wlc->pub->up && wlc->pub->associated) {
5489 		/* let watchdog or beacon processing update shortslot */
5490 	} else if (wlc->pub->up) {
5491 		/* unassociated shortslot is off */
5492 		brcms_c_switch_shortslot(wlc, false);
5493 	} else {
5494 		/* driver is down, so just update the brcms_c_info
5495 		 * value */
5496 		if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
5497 			wlc->shortslot = false;
5498 		else
5499 			wlc->shortslot =
5500 			    (wlc->shortslot_override ==
5501 			     BRCMS_SHORTSLOT_ON);
5502 	}
5503 }
5504 
5505 /*
5506  * register watchdog and down handlers.
5507  */
5508 int brcms_c_module_register(struct brcms_pub *pub,
5509 			    const char *name, struct brcms_info *hdl,
5510 			    int (*d_fn)(void *handle))
5511 {
5512 	struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5513 	int i;
5514 
5515 	/* find an empty entry and just add, no duplication check! */
5516 	for (i = 0; i < BRCMS_MAXMODULES; i++) {
5517 		if (wlc->modulecb[i].name[0] == '\0') {
5518 			strscpy(wlc->modulecb[i].name, name,
5519 				sizeof(wlc->modulecb[i].name));
5520 			wlc->modulecb[i].hdl = hdl;
5521 			wlc->modulecb[i].down_fn = d_fn;
5522 			return 0;
5523 		}
5524 	}
5525 
5526 	return -ENOSR;
5527 }
5528 
5529 /* unregister module callbacks */
5530 int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
5531 			      struct brcms_info *hdl)
5532 {
5533 	struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
5534 	int i;
5535 
5536 	if (wlc == NULL)
5537 		return -ENODATA;
5538 
5539 	for (i = 0; i < BRCMS_MAXMODULES; i++) {
5540 		if (!strcmp(wlc->modulecb[i].name, name) &&
5541 		    (wlc->modulecb[i].hdl == hdl)) {
5542 			memset(&wlc->modulecb[i], 0, sizeof(wlc->modulecb[i]));
5543 			return 0;
5544 		}
5545 	}
5546 
5547 	/* table not found! */
5548 	return -ENODATA;
5549 }
5550 
5551 static bool brcms_c_chipmatch_pci(struct bcma_device *core)
5552 {
5553 	struct pci_dev *pcidev = core->bus->host_pci;
5554 	u16 vendor = pcidev->vendor;
5555 	u16 device = pcidev->device;
5556 
5557 	if (vendor != PCI_VENDOR_ID_BROADCOM) {
5558 		pr_err("unknown vendor id %04x\n", vendor);
5559 		return false;
5560 	}
5561 
5562 	if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
5563 		return true;
5564 	if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
5565 		return true;
5566 	if (device == BCM4313_D11N2G_ID || device == BCM4313_CHIP_ID)
5567 		return true;
5568 	if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
5569 		return true;
5570 
5571 	pr_err("unknown device id %04x\n", device);
5572 	return false;
5573 }
5574 
5575 static bool brcms_c_chipmatch_soc(struct bcma_device *core)
5576 {
5577 	struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
5578 
5579 	if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
5580 		return true;
5581 
5582 	pr_err("unknown chip id %04x\n", chipinfo->id);
5583 	return false;
5584 }
5585 
5586 bool brcms_c_chipmatch(struct bcma_device *core)
5587 {
5588 	switch (core->bus->hosttype) {
5589 	case BCMA_HOSTTYPE_PCI:
5590 		return brcms_c_chipmatch_pci(core);
5591 	case BCMA_HOSTTYPE_SOC:
5592 		return brcms_c_chipmatch_soc(core);
5593 	default:
5594 		pr_err("unknown host type: %i\n", core->bus->hosttype);
5595 		return false;
5596 	}
5597 }
5598 
5599 u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
5600 {
5601 	u16 table_ptr;
5602 	u8 phy_rate, index;
5603 
5604 	/* get the phy specific rate encoding for the PLCP SIGNAL field */
5605 	if (is_ofdm_rate(rate))
5606 		table_ptr = M_RT_DIRMAP_A;
5607 	else
5608 		table_ptr = M_RT_DIRMAP_B;
5609 
5610 	/* for a given rate, the LS-nibble of the PLCP SIGNAL field is
5611 	 * the index into the rate table.
5612 	 */
5613 	phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
5614 	index = phy_rate & 0xf;
5615 
5616 	/* Find the SHM pointer to the rate table entry by looking in the
5617 	 * Direct-map Table
5618 	 */
5619 	return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
5620 }
5621 
5622 /*
5623  * bcmc_fid_generate:
5624  * Generate frame ID for a BCMC packet.  The frag field is not used
5625  * for MC frames so is used as part of the sequence number.
5626  */
5627 static inline u16
5628 bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
5629 		  struct d11txh *txh)
5630 {
5631 	u16 frameid;
5632 
5633 	frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
5634 						  TXFID_QUEUE_MASK);
5635 	frameid |=
5636 	    (((wlc->
5637 	       mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
5638 	    TX_BCMC_FIFO;
5639 
5640 	return frameid;
5641 }
5642 
5643 static uint
5644 brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
5645 		      u8 preamble_type)
5646 {
5647 	uint dur = 0;
5648 
5649 	/*
5650 	 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5651 	 * is less than or equal to the rate of the immediately previous
5652 	 * frame in the FES
5653 	 */
5654 	rspec = brcms_basic_rate(wlc, rspec);
5655 	/* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
5656 	dur =
5657 	    brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5658 				(DOT11_ACK_LEN + FCS_LEN));
5659 	return dur;
5660 }
5661 
5662 static uint
5663 brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
5664 		      u8 preamble_type)
5665 {
5666 	return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
5667 }
5668 
5669 static uint
5670 brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
5671 		     u8 preamble_type)
5672 {
5673 	/*
5674 	 * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
5675 	 * is less than or equal to the rate of the immediately previous
5676 	 * frame in the FES
5677 	 */
5678 	rspec = brcms_basic_rate(wlc, rspec);
5679 	/* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
5680 	return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
5681 				   (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
5682 				    FCS_LEN));
5683 }
5684 
5685 /* brcms_c_compute_frame_dur()
5686  *
5687  * Calculate the 802.11 MAC header DUR field for MPDU
5688  * DUR for a single frame = 1 SIFS + 1 ACK
5689  * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
5690  *
5691  * rate			MPDU rate in unit of 500kbps
5692  * next_frag_len	next MPDU length in bytes
5693  * preamble_type	use short/GF or long/MM PLCP header
5694  */
5695 static u16
5696 brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
5697 		      u8 preamble_type, uint next_frag_len)
5698 {
5699 	u16 dur, sifs;
5700 
5701 	sifs = get_sifs(wlc->band);
5702 
5703 	dur = sifs;
5704 	dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
5705 
5706 	if (next_frag_len) {
5707 		/* Double the current DUR to get 2 SIFS + 2 ACKs */
5708 		dur *= 2;
5709 		/* add another SIFS and the frag time */
5710 		dur += sifs;
5711 		dur +=
5712 		    (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
5713 						 next_frag_len);
5714 	}
5715 	return dur;
5716 }
5717 
5718 /* The opposite of brcms_c_calc_frame_time */
5719 static uint
5720 brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
5721 		   u8 preamble_type, uint dur)
5722 {
5723 	uint nsyms, mac_len, Ndps, kNdps;
5724 	uint rate = rspec2rate(ratespec);
5725 
5726 	if (is_mcs_rate(ratespec)) {
5727 		uint mcs = ratespec & RSPEC_RATE_MASK;
5728 		int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
5729 		dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
5730 		/* payload calculation matches that of regular ofdm */
5731 		if (wlc->band->bandtype == BRCM_BAND_2G)
5732 			dur -= DOT11_OFDM_SIGNAL_EXTENSION;
5733 		/* kNdbps = kbps * 4 */
5734 		kNdps =	mcs_2_rate(mcs, rspec_is40mhz(ratespec),
5735 				   rspec_issgi(ratespec)) * 4;
5736 		nsyms = dur / APHY_SYMBOL_TIME;
5737 		mac_len =
5738 		    ((nsyms * kNdps) -
5739 		     ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
5740 	} else if (is_ofdm_rate(ratespec)) {
5741 		dur -= APHY_PREAMBLE_TIME;
5742 		dur -= APHY_SIGNAL_TIME;
5743 		/* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
5744 		Ndps = rate * 2;
5745 		nsyms = dur / APHY_SYMBOL_TIME;
5746 		mac_len =
5747 		    ((nsyms * Ndps) -
5748 		     (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
5749 	} else {
5750 		if (preamble_type & BRCMS_SHORT_PREAMBLE)
5751 			dur -= BPHY_PLCP_SHORT_TIME;
5752 		else
5753 			dur -= BPHY_PLCP_TIME;
5754 		mac_len = dur * rate;
5755 		/* divide out factor of 2 in rate (1/2 mbps) */
5756 		mac_len = mac_len / 8 / 2;
5757 	}
5758 	return mac_len;
5759 }
5760 
5761 /*
5762  * Return true if the specified rate is supported by the specified band.
5763  * BRCM_BAND_AUTO indicates the current band.
5764  */
5765 static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
5766 		    bool verbose)
5767 {
5768 	struct brcms_c_rateset *hw_rateset;
5769 	uint i;
5770 
5771 	if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
5772 		hw_rateset = &wlc->band->hw_rateset;
5773 	else if (wlc->pub->_nbands > 1)
5774 		hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
5775 	else
5776 		/* other band specified and we are a single band device */
5777 		return false;
5778 
5779 	/* check if this is a mimo rate */
5780 	if (is_mcs_rate(rspec)) {
5781 		if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
5782 			goto error;
5783 
5784 		return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
5785 	}
5786 
5787 	for (i = 0; i < hw_rateset->count; i++)
5788 		if (hw_rateset->rates[i] == rspec2rate(rspec))
5789 			return true;
5790  error:
5791 	if (verbose)
5792 		brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
5793 			  "not in hw_rateset\n", wlc->pub->unit, rspec);
5794 
5795 	return false;
5796 }
5797 
5798 static u32
5799 mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
5800 		       u32 int_val)
5801 {
5802 	struct bcma_device *core = wlc->hw->d11core;
5803 	u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
5804 	u8 rate = int_val & NRATE_RATE_MASK;
5805 	u32 rspec;
5806 	bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
5807 	bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
5808 	bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
5809 				  == NRATE_OVERRIDE_MCS_ONLY);
5810 
5811 	if (!ismcs)
5812 		return (u32) rate;
5813 
5814 	/* validate the combination of rate/mcs/stf is allowed */
5815 	if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
5816 		/* mcs only allowed when nmode */
5817 		if (stf > PHY_TXC1_MODE_SDM) {
5818 			brcms_err(core, "wl%d: %s: Invalid stf\n",
5819 				  wlc->pub->unit, __func__);
5820 			goto done;
5821 		}
5822 
5823 		/* mcs 32 is a special case, DUP mode 40 only */
5824 		if (rate == 32) {
5825 			if (!CHSPEC_IS40(wlc->home_chanspec) ||
5826 			    ((stf != PHY_TXC1_MODE_SISO)
5827 			     && (stf != PHY_TXC1_MODE_CDD))) {
5828 				brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
5829 					  wlc->pub->unit, __func__);
5830 				goto done;
5831 			}
5832 			/* mcs > 7 must use stf SDM */
5833 		} else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
5834 			/* mcs > 7 must use stf SDM */
5835 			if (stf != PHY_TXC1_MODE_SDM) {
5836 				brcms_dbg_mac80211(core, "wl%d: enabling "
5837 						   "SDM mode for mcs %d\n",
5838 						   wlc->pub->unit, rate);
5839 				stf = PHY_TXC1_MODE_SDM;
5840 			}
5841 		} else {
5842 			/*
5843 			 * MCS 0-7 may use SISO, CDD, and for
5844 			 * phy_rev >= 3 STBC
5845 			 */
5846 			if ((stf > PHY_TXC1_MODE_STBC) ||
5847 			    (!BRCMS_STBC_CAP_PHY(wlc)
5848 			     && (stf == PHY_TXC1_MODE_STBC))) {
5849 				brcms_err(core, "wl%d: %s: Invalid STBC\n",
5850 					  wlc->pub->unit, __func__);
5851 				goto done;
5852 			}
5853 		}
5854 	} else if (is_ofdm_rate(rate)) {
5855 		if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
5856 			brcms_err(core, "wl%d: %s: Invalid OFDM\n",
5857 				  wlc->pub->unit, __func__);
5858 			goto done;
5859 		}
5860 	} else if (is_cck_rate(rate)) {
5861 		if ((cur_band->bandtype != BRCM_BAND_2G)
5862 		    || (stf != PHY_TXC1_MODE_SISO)) {
5863 			brcms_err(core, "wl%d: %s: Invalid CCK\n",
5864 				  wlc->pub->unit, __func__);
5865 			goto done;
5866 		}
5867 	} else {
5868 		brcms_err(core, "wl%d: %s: Unknown rate type\n",
5869 			  wlc->pub->unit, __func__);
5870 		goto done;
5871 	}
5872 	/* make sure multiple antennae are available for non-siso rates */
5873 	if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
5874 		brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
5875 			  "request\n", wlc->pub->unit, __func__);
5876 		goto done;
5877 	}
5878 
5879 	rspec = rate;
5880 	if (ismcs) {
5881 		rspec |= RSPEC_MIMORATE;
5882 		/* For STBC populate the STC field of the ratespec */
5883 		if (stf == PHY_TXC1_MODE_STBC) {
5884 			u8 stc;
5885 			stc = 1;	/* Nss for single stream is always 1 */
5886 			rspec |= (stc << RSPEC_STC_SHIFT);
5887 		}
5888 	}
5889 
5890 	rspec |= (stf << RSPEC_STF_SHIFT);
5891 
5892 	if (override_mcs_only)
5893 		rspec |= RSPEC_OVERRIDE_MCS_ONLY;
5894 
5895 	if (issgi)
5896 		rspec |= RSPEC_SHORT_GI;
5897 
5898 	if ((rate != 0)
5899 	    && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
5900 		return rate;
5901 
5902 	return rspec;
5903 done:
5904 	return rate;
5905 }
5906 
5907 /*
5908  * Compute PLCP, but only requires actual rate and length of pkt.
5909  * Rate is given in the driver standard multiple of 500 kbps.
5910  * le is set for 11 Mbps rate if necessary.
5911  * Broken out for PRQ.
5912  */
5913 
5914 static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
5915 			     uint length, u8 *plcp)
5916 {
5917 	u16 usec = 0;
5918 	u8 le = 0;
5919 
5920 	switch (rate_500) {
5921 	case BRCM_RATE_1M:
5922 		usec = length << 3;
5923 		break;
5924 	case BRCM_RATE_2M:
5925 		usec = length << 2;
5926 		break;
5927 	case BRCM_RATE_5M5:
5928 		usec = (length << 4) / 11;
5929 		if ((length << 4) - (usec * 11) > 0)
5930 			usec++;
5931 		break;
5932 	case BRCM_RATE_11M:
5933 		usec = (length << 3) / 11;
5934 		if ((length << 3) - (usec * 11) > 0) {
5935 			usec++;
5936 			if ((usec * 11) - (length << 3) >= 8)
5937 				le = D11B_PLCP_SIGNAL_LE;
5938 		}
5939 		break;
5940 
5941 	default:
5942 		brcms_err(wlc->hw->d11core,
5943 			  "brcms_c_cck_plcp_set: unsupported rate %d\n",
5944 			  rate_500);
5945 		rate_500 = BRCM_RATE_1M;
5946 		usec = length << 3;
5947 		break;
5948 	}
5949 	/* PLCP signal byte */
5950 	plcp[0] = rate_500 * 5;	/* r (500kbps) * 5 == r (100kbps) */
5951 	/* PLCP service byte */
5952 	plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
5953 	/* PLCP length u16, little endian */
5954 	plcp[2] = usec & 0xff;
5955 	plcp[3] = (usec >> 8) & 0xff;
5956 	/* PLCP CRC16 */
5957 	plcp[4] = 0;
5958 	plcp[5] = 0;
5959 }
5960 
5961 /* Rate: 802.11 rate code, length: PSDU length in octets */
5962 static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
5963 {
5964 	u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
5965 	plcp[0] = mcs;
5966 	if (rspec_is40mhz(rspec) || (mcs == 32))
5967 		plcp[0] |= MIMO_PLCP_40MHZ;
5968 	BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
5969 	plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
5970 	plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
5971 	plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
5972 	plcp[5] = 0;
5973 }
5974 
5975 /* Rate: 802.11 rate code, length: PSDU length in octets */
5976 static void
5977 brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
5978 {
5979 	u8 rate_signal;
5980 	u32 tmp = 0;
5981 	int rate = rspec2rate(rspec);
5982 
5983 	/*
5984 	 * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
5985 	 * transmitted first
5986 	 */
5987 	rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
5988 	memset(plcp, 0, D11_PHY_HDR_LEN);
5989 	D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
5990 
5991 	tmp = (length & 0xfff) << 5;
5992 	plcp[2] |= (tmp >> 16) & 0xff;
5993 	plcp[1] |= (tmp >> 8) & 0xff;
5994 	plcp[0] |= tmp & 0xff;
5995 }
5996 
5997 /* Rate: 802.11 rate code, length: PSDU length in octets */
5998 static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
5999 				 uint length, u8 *plcp)
6000 {
6001 	int rate = rspec2rate(rspec);
6002 
6003 	brcms_c_cck_plcp_set(wlc, rate, length, plcp);
6004 }
6005 
6006 static void
6007 brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
6008 		     uint length, u8 *plcp)
6009 {
6010 	if (is_mcs_rate(rspec))
6011 		brcms_c_compute_mimo_plcp(rspec, length, plcp);
6012 	else if (is_ofdm_rate(rspec))
6013 		brcms_c_compute_ofdm_plcp(rspec, length, plcp);
6014 	else
6015 		brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
6016 }
6017 
6018 /* brcms_c_compute_rtscts_dur()
6019  *
6020  * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
6021  * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
6022  * DUR for CTS-TO-SELF w/ frame    = 2 SIFS         + next frame time + 1 ACK
6023  *
6024  * cts			cts-to-self or rts/cts
6025  * rts_rate		rts or cts rate in unit of 500kbps
6026  * rate			next MPDU rate in unit of 500kbps
6027  * frame_len		next MPDU frame length in bytes
6028  */
6029 u16
6030 brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
6031 			   u32 rts_rate,
6032 			   u32 frame_rate, u8 rts_preamble_type,
6033 			   u8 frame_preamble_type, uint frame_len, bool ba)
6034 {
6035 	u16 dur, sifs;
6036 
6037 	sifs = get_sifs(wlc->band);
6038 
6039 	if (!cts_only) {
6040 		/* RTS/CTS */
6041 		dur = 3 * sifs;
6042 		dur +=
6043 		    (u16) brcms_c_calc_cts_time(wlc, rts_rate,
6044 					       rts_preamble_type);
6045 	} else {
6046 		/* CTS-TO-SELF */
6047 		dur = 2 * sifs;
6048 	}
6049 
6050 	dur +=
6051 	    (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
6052 					 frame_len);
6053 	if (ba)
6054 		dur +=
6055 		    (u16) brcms_c_calc_ba_time(wlc, frame_rate,
6056 					      BRCMS_SHORT_PREAMBLE);
6057 	else
6058 		dur +=
6059 		    (u16) brcms_c_calc_ack_time(wlc, frame_rate,
6060 					       frame_preamble_type);
6061 	return dur;
6062 }
6063 
6064 static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
6065 {
6066 	u16 phyctl1 = 0;
6067 	u16 bw;
6068 
6069 	if (BRCMS_ISLCNPHY(wlc->band)) {
6070 		bw = PHY_TXC1_BW_20MHZ;
6071 	} else {
6072 		bw = rspec_get_bw(rspec);
6073 		/* 10Mhz is not supported yet */
6074 		if (bw < PHY_TXC1_BW_20MHZ) {
6075 			brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
6076 				  "not supported yet, set to 20L\n", bw);
6077 			bw = PHY_TXC1_BW_20MHZ;
6078 		}
6079 	}
6080 
6081 	if (is_mcs_rate(rspec)) {
6082 		uint mcs = rspec & RSPEC_RATE_MASK;
6083 
6084 		/* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
6085 		phyctl1 = rspec_phytxbyte2(rspec);
6086 		/* set the upper byte of phyctl1 */
6087 		phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
6088 	} else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
6089 		   && !BRCMS_ISSSLPNPHY(wlc->band)) {
6090 		/*
6091 		 * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
6092 		 * Data Rate. Eventually MIMOPHY would also be converted to
6093 		 * this format
6094 		 */
6095 		/* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
6096 		phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6097 	} else {		/* legacy OFDM/CCK */
6098 		s16 phycfg;
6099 		/* get the phyctl byte from rate phycfg table */
6100 		phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
6101 		if (phycfg == -1) {
6102 			brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
6103 				  "legacy OFDM/CCK rate\n");
6104 			phycfg = 0;
6105 		}
6106 		/* set the upper byte of phyctl1 */
6107 		phyctl1 =
6108 		    (bw | (phycfg << 8) |
6109 		     (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
6110 	}
6111 	return phyctl1;
6112 }
6113 
6114 /*
6115  * Add struct d11txh, struct cck_phy_hdr.
6116  *
6117  * 'p' data must start with 802.11 MAC header
6118  * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
6119  *
6120  * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
6121  *
6122  */
6123 static u16
6124 brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
6125 		     struct sk_buff *p, struct scb *scb, uint frag,
6126 		     uint nfrags, uint queue, uint next_frag_len)
6127 {
6128 	struct ieee80211_hdr *h;
6129 	struct d11txh *txh;
6130 	u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
6131 	int len, phylen, rts_phylen;
6132 	u16 mch, phyctl, xfts, mainrates;
6133 	u16 seq = 0, mcl = 0, status = 0, frameid = 0;
6134 	u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6135 	u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
6136 	bool use_rts = false;
6137 	bool use_cts = false;
6138 	bool use_rifs = false;
6139 	u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6140 	u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
6141 	u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
6142 	struct ieee80211_rts *rts = NULL;
6143 	bool qos;
6144 	uint ac;
6145 	bool hwtkmic = false;
6146 	u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
6147 #define ANTCFG_NONE 0xFF
6148 	u8 antcfg = ANTCFG_NONE;
6149 	u8 fbantcfg = ANTCFG_NONE;
6150 	uint phyctl1_stf = 0;
6151 	u16 durid = 0;
6152 	struct ieee80211_tx_rate *txrate[2];
6153 	int k;
6154 	struct ieee80211_tx_info *tx_info;
6155 	bool is_mcs;
6156 	u16 mimo_txbw;
6157 	u8 mimo_preamble_type;
6158 
6159 	/* locate 802.11 MAC header */
6160 	h = (struct ieee80211_hdr *)(p->data);
6161 	qos = ieee80211_is_data_qos(h->frame_control);
6162 
6163 	/* compute length of frame in bytes for use in PLCP computations */
6164 	len = p->len;
6165 	phylen = len + FCS_LEN;
6166 
6167 	/* Get tx_info */
6168 	tx_info = IEEE80211_SKB_CB(p);
6169 
6170 	/* add PLCP */
6171 	plcp = skb_push(p, D11_PHY_HDR_LEN);
6172 
6173 	/* add Broadcom tx descriptor header */
6174 	txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
6175 	memset(txh, 0, D11_TXH_LEN);
6176 
6177 	/* setup frameid */
6178 	if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
6179 		/* non-AP STA should never use BCMC queue */
6180 		if (queue == TX_BCMC_FIFO) {
6181 			brcms_err(wlc->hw->d11core,
6182 				  "wl%d: %s: ASSERT queue == TX_BCMC!\n",
6183 				  wlc->pub->unit, __func__);
6184 			frameid = bcmc_fid_generate(wlc, NULL, txh);
6185 		} else {
6186 			/* Increment the counter for first fragment */
6187 			if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
6188 				scb->seqnum[p->priority]++;
6189 
6190 			/* extract fragment number from frame first */
6191 			seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
6192 			seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
6193 			h->seq_ctrl = cpu_to_le16(seq);
6194 
6195 			frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
6196 			    (queue & TXFID_QUEUE_MASK);
6197 		}
6198 	}
6199 	frameid |= queue & TXFID_QUEUE_MASK;
6200 
6201 	/* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
6202 	if (ieee80211_is_beacon(h->frame_control))
6203 		mcl |= TXC_IGNOREPMQ;
6204 
6205 	txrate[0] = tx_info->control.rates;
6206 	txrate[1] = txrate[0] + 1;
6207 
6208 	/*
6209 	 * if rate control algorithm didn't give us a fallback
6210 	 * rate, use the primary rate
6211 	 */
6212 	if (txrate[1]->idx < 0)
6213 		txrate[1] = txrate[0];
6214 
6215 	for (k = 0; k < hw->max_rates; k++) {
6216 		is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
6217 		if (!is_mcs) {
6218 			if ((txrate[k]->idx >= 0)
6219 			    && (txrate[k]->idx <
6220 				hw->wiphy->bands[tx_info->band]->n_bitrates)) {
6221 				rspec[k] =
6222 				    hw->wiphy->bands[tx_info->band]->
6223 				    bitrates[txrate[k]->idx].hw_value;
6224 			} else {
6225 				rspec[k] = BRCM_RATE_1M;
6226 			}
6227 		} else {
6228 			rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
6229 					NRATE_MCS_INUSE | txrate[k]->idx);
6230 		}
6231 
6232 		/*
6233 		 * Currently only support same setting for primary and
6234 		 * fallback rates. Unify flags for each rate into a
6235 		 * single value for the frame
6236 		 */
6237 		use_rts |=
6238 		    txrate[k]->
6239 		    flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
6240 		use_cts |=
6241 		    txrate[k]->
6242 		    flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
6243 
6244 
6245 		/*
6246 		 * (1) RATE:
6247 		 *   determine and validate primary rate
6248 		 *   and fallback rates
6249 		 */
6250 		if (!rspec_active(rspec[k])) {
6251 			rspec[k] = BRCM_RATE_1M;
6252 		} else {
6253 			if (!is_multicast_ether_addr(h->addr1)) {
6254 				/* set tx antenna config */
6255 				brcms_c_antsel_antcfg_get(wlc->asi, false,
6256 					false, 0, 0, &antcfg, &fbantcfg);
6257 			}
6258 		}
6259 	}
6260 
6261 	phyctl1_stf = wlc->stf->ss_opmode;
6262 
6263 	if (wlc->pub->_n_enab & SUPPORT_11N) {
6264 		for (k = 0; k < hw->max_rates; k++) {
6265 			/*
6266 			 * apply siso/cdd to single stream mcs's or ofdm
6267 			 * if rspec is auto selected
6268 			 */
6269 			if (((is_mcs_rate(rspec[k]) &&
6270 			      is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
6271 			     is_ofdm_rate(rspec[k]))
6272 			    && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
6273 				|| !(rspec[k] & RSPEC_OVERRIDE))) {
6274 				rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
6275 
6276 				/* For SISO MCS use STBC if possible */
6277 				if (is_mcs_rate(rspec[k])
6278 				    && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
6279 					u8 stc;
6280 
6281 					/* Nss for single stream is always 1 */
6282 					stc = 1;
6283 					rspec[k] |= (PHY_TXC1_MODE_STBC <<
6284 							RSPEC_STF_SHIFT) |
6285 						    (stc << RSPEC_STC_SHIFT);
6286 				} else
6287 					rspec[k] |=
6288 					    (phyctl1_stf << RSPEC_STF_SHIFT);
6289 			}
6290 
6291 			/*
6292 			 * Is the phy configured to use 40MHZ frames? If
6293 			 * so then pick the desired txbw
6294 			 */
6295 			if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
6296 				/* default txbw is 20in40 SB */
6297 				mimo_ctlchbw = mimo_txbw =
6298 				   CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
6299 								 wlc->band->pi))
6300 				   ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
6301 
6302 				if (is_mcs_rate(rspec[k])) {
6303 					/* mcs 32 must be 40b/w DUP */
6304 					if ((rspec[k] & RSPEC_RATE_MASK)
6305 					    == 32) {
6306 						mimo_txbw =
6307 						    PHY_TXC1_BW_40MHZ_DUP;
6308 						/* use override */
6309 					} else if (wlc->mimo_40txbw != AUTO)
6310 						mimo_txbw = wlc->mimo_40txbw;
6311 					/* else check if dst is using 40 Mhz */
6312 					else if (scb->flags & SCB_IS40)
6313 						mimo_txbw = PHY_TXC1_BW_40MHZ;
6314 				} else if (is_ofdm_rate(rspec[k])) {
6315 					if (wlc->ofdm_40txbw != AUTO)
6316 						mimo_txbw = wlc->ofdm_40txbw;
6317 				} else if (wlc->cck_40txbw != AUTO) {
6318 					mimo_txbw = wlc->cck_40txbw;
6319 				}
6320 			} else {
6321 				/*
6322 				 * mcs32 is 40 b/w only.
6323 				 * This is possible for probe packets on
6324 				 * a STA during SCAN
6325 				 */
6326 				if ((rspec[k] & RSPEC_RATE_MASK) == 32)
6327 					/* mcs 0 */
6328 					rspec[k] = RSPEC_MIMORATE;
6329 
6330 				mimo_txbw = PHY_TXC1_BW_20MHZ;
6331 			}
6332 
6333 			/* Set channel width */
6334 			rspec[k] &= ~RSPEC_BW_MASK;
6335 			if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
6336 				rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
6337 			else
6338 				rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6339 
6340 			/* Disable short GI, not supported yet */
6341 			rspec[k] &= ~RSPEC_SHORT_GI;
6342 
6343 			mimo_preamble_type = BRCMS_MM_PREAMBLE;
6344 			if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
6345 				mimo_preamble_type = BRCMS_GF_PREAMBLE;
6346 
6347 			if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
6348 			    && (!is_mcs_rate(rspec[k]))) {
6349 				brcms_warn(wlc->hw->d11core,
6350 					   "wl%d: %s: IEEE80211_TX_RC_MCS != is_mcs_rate(rspec)\n",
6351 					   wlc->pub->unit, __func__);
6352 			}
6353 
6354 			if (is_mcs_rate(rspec[k])) {
6355 				preamble_type[k] = mimo_preamble_type;
6356 
6357 				/*
6358 				 * if SGI is selected, then forced mm
6359 				 * for single stream
6360 				 */
6361 				if ((rspec[k] & RSPEC_SHORT_GI)
6362 				    && is_single_stream(rspec[k] &
6363 							RSPEC_RATE_MASK))
6364 					preamble_type[k] = BRCMS_MM_PREAMBLE;
6365 			}
6366 
6367 			/* should be better conditionalized */
6368 			if (!is_mcs_rate(rspec[0])
6369 			    && (tx_info->control.rates[0].
6370 				flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
6371 				preamble_type[k] = BRCMS_SHORT_PREAMBLE;
6372 		}
6373 	} else {
6374 		for (k = 0; k < hw->max_rates; k++) {
6375 			/* Set ctrlchbw as 20Mhz */
6376 			rspec[k] &= ~RSPEC_BW_MASK;
6377 			rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
6378 
6379 			/* for nphy, stf of ofdm frames must follow policies */
6380 			if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
6381 				rspec[k] &= ~RSPEC_STF_MASK;
6382 				rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
6383 			}
6384 		}
6385 	}
6386 
6387 	/* Reset these for use with AMPDU's */
6388 	txrate[0]->count = 0;
6389 	txrate[1]->count = 0;
6390 
6391 	/* (2) PROTECTION, may change rspec */
6392 	if ((ieee80211_is_data(h->frame_control) ||
6393 	    ieee80211_is_mgmt(h->frame_control)) &&
6394 	    (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
6395 		use_rts = true;
6396 
6397 	/* (3) PLCP: determine PLCP header and MAC duration,
6398 	 * fill struct d11txh */
6399 	brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
6400 	brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
6401 	memcpy(&txh->FragPLCPFallback,
6402 	       plcp_fallback, sizeof(txh->FragPLCPFallback));
6403 
6404 	/* Length field now put in CCK FBR CRC field */
6405 	if (is_cck_rate(rspec[1])) {
6406 		txh->FragPLCPFallback[4] = phylen & 0xff;
6407 		txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
6408 	}
6409 
6410 	/* MIMO-RATE: need validation ?? */
6411 	mainrates = is_ofdm_rate(rspec[0]) ?
6412 			D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
6413 			plcp[0];
6414 
6415 	/* DUR field for main rate */
6416 	if (!ieee80211_is_pspoll(h->frame_control) &&
6417 	    !is_multicast_ether_addr(h->addr1) && !use_rifs) {
6418 		durid =
6419 		    brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
6420 					  next_frag_len);
6421 		h->duration_id = cpu_to_le16(durid);
6422 	} else if (use_rifs) {
6423 		/* NAV protect to end of next max packet size */
6424 		durid =
6425 		    (u16) brcms_c_calc_frame_time(wlc, rspec[0],
6426 						 preamble_type[0],
6427 						 DOT11_MAX_FRAG_LEN);
6428 		durid += RIFS_11N_TIME;
6429 		h->duration_id = cpu_to_le16(durid);
6430 	}
6431 
6432 	/* DUR field for fallback rate */
6433 	if (ieee80211_is_pspoll(h->frame_control))
6434 		txh->FragDurFallback = h->duration_id;
6435 	else if (is_multicast_ether_addr(h->addr1) || use_rifs)
6436 		txh->FragDurFallback = 0;
6437 	else {
6438 		durid = brcms_c_compute_frame_dur(wlc, rspec[1],
6439 					      preamble_type[1], next_frag_len);
6440 		txh->FragDurFallback = cpu_to_le16(durid);
6441 	}
6442 
6443 	/* (4) MAC-HDR: MacTxControlLow */
6444 	if (frag == 0)
6445 		mcl |= TXC_STARTMSDU;
6446 
6447 	if (!is_multicast_ether_addr(h->addr1))
6448 		mcl |= TXC_IMMEDACK;
6449 
6450 	if (wlc->band->bandtype == BRCM_BAND_5G)
6451 		mcl |= TXC_FREQBAND_5G;
6452 
6453 	if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
6454 		mcl |= TXC_BW_40;
6455 
6456 	/* set AMIC bit if using hardware TKIP MIC */
6457 	if (hwtkmic)
6458 		mcl |= TXC_AMIC;
6459 
6460 	txh->MacTxControlLow = cpu_to_le16(mcl);
6461 
6462 	/* MacTxControlHigh */
6463 	mch = 0;
6464 
6465 	/* Set fallback rate preamble type */
6466 	if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
6467 	    (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
6468 		if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
6469 			mch |= TXC_PREAMBLE_DATA_FB_SHORT;
6470 	}
6471 
6472 	/* MacFrameControl */
6473 	memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
6474 	txh->TxFesTimeNormal = cpu_to_le16(0);
6475 
6476 	txh->TxFesTimeFallback = cpu_to_le16(0);
6477 
6478 	/* TxFrameRA */
6479 	memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
6480 
6481 	/* TxFrameID */
6482 	txh->TxFrameID = cpu_to_le16(frameid);
6483 
6484 	/*
6485 	 * TxStatus, Note the case of recreating the first frag of a suppressed
6486 	 * frame then we may need to reset the retry cnt's via the status reg
6487 	 */
6488 	txh->TxStatus = cpu_to_le16(status);
6489 
6490 	/*
6491 	 * extra fields for ucode AMPDU aggregation, the new fields are added to
6492 	 * the END of previous structure so that it's compatible in driver.
6493 	 */
6494 	txh->MaxNMpdus = cpu_to_le16(0);
6495 	txh->MaxABytes_MRT = cpu_to_le16(0);
6496 	txh->MaxABytes_FBR = cpu_to_le16(0);
6497 	txh->MinMBytes = cpu_to_le16(0);
6498 
6499 	/* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
6500 	 * furnish struct d11txh */
6501 	/* RTS PLCP header and RTS frame */
6502 	if (use_rts || use_cts) {
6503 		if (use_rts && use_cts)
6504 			use_cts = false;
6505 
6506 		for (k = 0; k < 2; k++) {
6507 			rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
6508 							      false,
6509 							      mimo_ctlchbw);
6510 		}
6511 
6512 		if (!is_ofdm_rate(rts_rspec[0]) &&
6513 		    !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
6514 		      (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6515 			rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
6516 			mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
6517 		}
6518 
6519 		if (!is_ofdm_rate(rts_rspec[1]) &&
6520 		    !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
6521 		      (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
6522 			rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
6523 			mch |= TXC_PREAMBLE_RTS_FB_SHORT;
6524 		}
6525 
6526 		/* RTS/CTS additions to MacTxControlLow */
6527 		if (use_cts) {
6528 			txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
6529 		} else {
6530 			txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
6531 			txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
6532 		}
6533 
6534 		/* RTS PLCP header */
6535 		rts_plcp = txh->RTSPhyHeader;
6536 		if (use_cts)
6537 			rts_phylen = DOT11_CTS_LEN + FCS_LEN;
6538 		else
6539 			rts_phylen = DOT11_RTS_LEN + FCS_LEN;
6540 
6541 		brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
6542 
6543 		/* fallback rate version of RTS PLCP header */
6544 		brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
6545 				 rts_plcp_fallback);
6546 		memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
6547 		       sizeof(txh->RTSPLCPFallback));
6548 
6549 		/* RTS frame fields... */
6550 		rts = (struct ieee80211_rts *)&txh->rts_frame;
6551 
6552 		durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
6553 					       rspec[0], rts_preamble_type[0],
6554 					       preamble_type[0], phylen, false);
6555 		rts->duration = cpu_to_le16(durid);
6556 		/* fallback rate version of RTS DUR field */
6557 		durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
6558 					       rts_rspec[1], rspec[1],
6559 					       rts_preamble_type[1],
6560 					       preamble_type[1], phylen, false);
6561 		txh->RTSDurFallback = cpu_to_le16(durid);
6562 
6563 		if (use_cts) {
6564 			rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6565 							 IEEE80211_STYPE_CTS);
6566 
6567 			memcpy(&rts->ra, &h->addr2, ETH_ALEN);
6568 		} else {
6569 			rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6570 							 IEEE80211_STYPE_RTS);
6571 
6572 			memcpy(&rts->ra, &h->addr1, ETH_ALEN);
6573 			memcpy(&rts->ta, &h->addr2, ETH_ALEN);
6574 		}
6575 
6576 		/* mainrate
6577 		 *    low 8 bits: main frag rate/mcs,
6578 		 *    high 8 bits: rts/cts rate/mcs
6579 		 */
6580 		mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
6581 				D11A_PHY_HDR_GRATE(
6582 					(struct ofdm_phy_hdr *) rts_plcp) :
6583 				rts_plcp[0]) << 8;
6584 	} else {
6585 		memset(txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
6586 		memset(&txh->rts_frame, 0, sizeof(struct ieee80211_rts));
6587 		memset(txh->RTSPLCPFallback, 0, sizeof(txh->RTSPLCPFallback));
6588 		txh->RTSDurFallback = 0;
6589 	}
6590 
6591 #ifdef SUPPORT_40MHZ
6592 	/* add null delimiter count */
6593 	if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
6594 		txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
6595 		   brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
6596 
6597 #endif
6598 
6599 	/*
6600 	 * Now that RTS/RTS FB preamble types are updated, write
6601 	 * the final value
6602 	 */
6603 	txh->MacTxControlHigh = cpu_to_le16(mch);
6604 
6605 	/*
6606 	 * MainRates (both the rts and frag plcp rates have
6607 	 * been calculated now)
6608 	 */
6609 	txh->MainRates = cpu_to_le16(mainrates);
6610 
6611 	/* XtraFrameTypes */
6612 	xfts = frametype(rspec[1], wlc->mimoft);
6613 	xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
6614 	xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
6615 	xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
6616 							     XFTS_CHANNEL_SHIFT;
6617 	txh->XtraFrameTypes = cpu_to_le16(xfts);
6618 
6619 	/* PhyTxControlWord */
6620 	phyctl = frametype(rspec[0], wlc->mimoft);
6621 	if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
6622 	    (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
6623 		if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
6624 			phyctl |= PHY_TXC_SHORT_HDR;
6625 	}
6626 
6627 	/* phytxant is properly bit shifted */
6628 	phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
6629 	txh->PhyTxControlWord = cpu_to_le16(phyctl);
6630 
6631 	/* PhyTxControlWord_1 */
6632 	if (BRCMS_PHY_11N_CAP(wlc->band)) {
6633 		u16 phyctl1 = 0;
6634 
6635 		phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
6636 		txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
6637 		phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
6638 		txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
6639 
6640 		if (use_rts || use_cts) {
6641 			phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
6642 			txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
6643 			phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
6644 			txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
6645 		}
6646 
6647 		/*
6648 		 * For mcs frames, if mixedmode(overloaded with long preamble)
6649 		 * is going to be set, fill in non-zero MModeLen and/or
6650 		 * MModeFbrLen it will be unnecessary if they are separated
6651 		 */
6652 		if (is_mcs_rate(rspec[0]) &&
6653 		    (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
6654 			u16 mmodelen =
6655 			    brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
6656 			txh->MModeLen = cpu_to_le16(mmodelen);
6657 		}
6658 
6659 		if (is_mcs_rate(rspec[1]) &&
6660 		    (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
6661 			u16 mmodefbrlen =
6662 			    brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
6663 			txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
6664 		}
6665 	}
6666 
6667 	ac = skb_get_queue_mapping(p);
6668 	if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
6669 		uint frag_dur, dur, dur_fallback;
6670 
6671 		/* WME: Update TXOP threshold */
6672 		if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
6673 			frag_dur =
6674 			    brcms_c_calc_frame_time(wlc, rspec[0],
6675 					preamble_type[0], phylen);
6676 
6677 			if (rts) {
6678 				/* 1 RTS or CTS-to-self frame */
6679 				dur =
6680 				    brcms_c_calc_cts_time(wlc, rts_rspec[0],
6681 						      rts_preamble_type[0]);
6682 				dur_fallback =
6683 				    brcms_c_calc_cts_time(wlc, rts_rspec[1],
6684 						      rts_preamble_type[1]);
6685 				/* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
6686 				dur += le16_to_cpu(rts->duration);
6687 				dur_fallback +=
6688 					le16_to_cpu(txh->RTSDurFallback);
6689 			} else if (use_rifs) {
6690 				dur = frag_dur;
6691 				dur_fallback = 0;
6692 			} else {
6693 				/* frame + SIFS + ACK */
6694 				dur = frag_dur;
6695 				dur +=
6696 				    brcms_c_compute_frame_dur(wlc, rspec[0],
6697 							  preamble_type[0], 0);
6698 
6699 				dur_fallback =
6700 				    brcms_c_calc_frame_time(wlc, rspec[1],
6701 							preamble_type[1],
6702 							phylen);
6703 				dur_fallback +=
6704 				    brcms_c_compute_frame_dur(wlc, rspec[1],
6705 							  preamble_type[1], 0);
6706 			}
6707 			/* NEED to set TxFesTimeNormal (hard) */
6708 			txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
6709 			/*
6710 			 * NEED to set fallback rate version of
6711 			 * TxFesTimeNormal (hard)
6712 			 */
6713 			txh->TxFesTimeFallback =
6714 				cpu_to_le16((u16) dur_fallback);
6715 
6716 			/*
6717 			 * update txop byte threshold (txop minus intraframe
6718 			 * overhead)
6719 			 */
6720 			if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
6721 				uint newfragthresh;
6722 
6723 				newfragthresh =
6724 				    brcms_c_calc_frame_len(wlc,
6725 					rspec[0], preamble_type[0],
6726 					(wlc->edcf_txop[ac] -
6727 						(dur - frag_dur)));
6728 				/* range bound the fragthreshold */
6729 				if (newfragthresh < DOT11_MIN_FRAG_LEN)
6730 					newfragthresh =
6731 					    DOT11_MIN_FRAG_LEN;
6732 				else if (newfragthresh >
6733 					 wlc->usr_fragthresh)
6734 					newfragthresh =
6735 					    wlc->usr_fragthresh;
6736 				/* update the fragthresh and do txc update */
6737 				if (wlc->fragthresh[queue] !=
6738 				    (u16) newfragthresh)
6739 					wlc->fragthresh[queue] =
6740 					    (u16) newfragthresh;
6741 			} else {
6742 				brcms_warn(wlc->hw->d11core,
6743 					   "wl%d: %s txop invalid for rate %d\n",
6744 					   wlc->pub->unit, fifo_names[queue],
6745 					   rspec2rate(rspec[0]));
6746 			}
6747 
6748 			if (dur > wlc->edcf_txop[ac])
6749 				brcms_warn(wlc->hw->d11core,
6750 					   "wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
6751 					   wlc->pub->unit, __func__,
6752 					   fifo_names[queue],
6753 					   phylen, wlc->fragthresh[queue],
6754 					   dur, wlc->edcf_txop[ac]);
6755 		}
6756 	}
6757 
6758 	return 0;
6759 }
6760 
6761 static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
6762 {
6763 	struct dma_pub *dma;
6764 	int fifo, ret = -ENOSPC;
6765 	struct d11txh *txh;
6766 	u16 frameid = INVALIDFID;
6767 
6768 	fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
6769 	dma = wlc->hw->di[fifo];
6770 	txh = (struct d11txh *)(skb->data);
6771 
6772 	if (dma->txavail == 0) {
6773 		/*
6774 		 * We sometimes get a frame from mac80211 after stopping
6775 		 * the queues. This only ever seems to be a single frame
6776 		 * and is seems likely to be a race. TX_HEADROOM should
6777 		 * ensure that we have enough space to handle these stray
6778 		 * packets, so warn if there isn't. If we're out of space
6779 		 * in the tx ring and the tx queue isn't stopped then
6780 		 * we've really got a bug; warn loudly if that happens.
6781 		 */
6782 		brcms_warn(wlc->hw->d11core,
6783 			   "Received frame for tx with no space in DMA ring\n");
6784 		WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
6785 						 skb_get_queue_mapping(skb)));
6786 		return -ENOSPC;
6787 	}
6788 
6789 	/* When a BC/MC frame is being committed to the BCMC fifo
6790 	 * via DMA (NOT PIO), update ucode or BSS info as appropriate.
6791 	 */
6792 	if (fifo == TX_BCMC_FIFO)
6793 		frameid = le16_to_cpu(txh->TxFrameID);
6794 
6795 	/* Commit BCMC sequence number in the SHM frame ID location */
6796 	if (frameid != INVALIDFID) {
6797 		/*
6798 		 * To inform the ucode of the last mcast frame posted
6799 		 * so that it can clear moredata bit
6800 		 */
6801 		brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
6802 	}
6803 
6804 	ret = brcms_c_txfifo(wlc, fifo, skb);
6805 	/*
6806 	 * The only reason for brcms_c_txfifo to fail is because
6807 	 * there weren't any DMA descriptors, but we've already
6808 	 * checked for that. So if it does fail yell loudly.
6809 	 */
6810 	WARN_ON_ONCE(ret);
6811 
6812 	return ret;
6813 }
6814 
6815 bool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
6816 			      struct ieee80211_hw *hw)
6817 {
6818 	uint fifo;
6819 	struct scb *scb = &wlc->pri_scb;
6820 
6821 	fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
6822 	brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
6823 	if (!brcms_c_tx(wlc, sdu))
6824 		return true;
6825 
6826 	/* packet discarded */
6827 	dev_kfree_skb_any(sdu);
6828 	return false;
6829 }
6830 
6831 int
6832 brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
6833 {
6834 	struct dma_pub *dma = wlc->hw->di[fifo];
6835 	int ret;
6836 	u16 queue;
6837 
6838 	ret = dma_txfast(wlc, dma, p);
6839 	if (ret	< 0)
6840 		wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
6841 
6842 	/*
6843 	 * Stop queue if DMA ring is full. Reserve some free descriptors,
6844 	 * as we sometimes receive a frame from mac80211 after the queues
6845 	 * are stopped.
6846 	 */
6847 	queue = skb_get_queue_mapping(p);
6848 	if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
6849 	    !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
6850 		ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
6851 
6852 	return ret;
6853 }
6854 
6855 u32
6856 brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
6857 			   bool use_rspec, u16 mimo_ctlchbw)
6858 {
6859 	u32 rts_rspec = 0;
6860 
6861 	if (use_rspec)
6862 		/* use frame rate as rts rate */
6863 		rts_rspec = rspec;
6864 	else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
6865 		/* Use 11Mbps as the g protection RTS target rate and fallback.
6866 		 * Use the brcms_basic_rate() lookup to find the best basic rate
6867 		 * under the target in case 11 Mbps is not Basic.
6868 		 * 6 and 9 Mbps are not usually selected by rate selection, but
6869 		 * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
6870 		 * is more robust.
6871 		 */
6872 		rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
6873 	else
6874 		/* calculate RTS rate and fallback rate based on the frame rate
6875 		 * RTS must be sent at a basic rate since it is a
6876 		 * control frame, sec 9.6 of 802.11 spec
6877 		 */
6878 		rts_rspec = brcms_basic_rate(wlc, rspec);
6879 
6880 	if (BRCMS_PHY_11N_CAP(wlc->band)) {
6881 		/* set rts txbw to correct side band */
6882 		rts_rspec &= ~RSPEC_BW_MASK;
6883 
6884 		/*
6885 		 * if rspec/rspec_fallback is 40MHz, then send RTS on both
6886 		 * 20MHz channel (DUP), otherwise send RTS on control channel
6887 		 */
6888 		if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
6889 			rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
6890 		else
6891 			rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
6892 
6893 		/* pick siso/cdd as default for ofdm */
6894 		if (is_ofdm_rate(rts_rspec)) {
6895 			rts_rspec &= ~RSPEC_STF_MASK;
6896 			rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
6897 		}
6898 	}
6899 	return rts_rspec;
6900 }
6901 
6902 /* Update beacon listen interval in shared memory */
6903 static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
6904 {
6905 	/* wake up every DTIM is the default */
6906 	if (wlc->bcn_li_dtim == 1)
6907 		brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
6908 	else
6909 		brcms_b_write_shm(wlc->hw, M_BCN_LI,
6910 			      (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
6911 }
6912 
6913 static void
6914 brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
6915 		  u32 *tsf_h_ptr)
6916 {
6917 	struct bcma_device *core = wlc_hw->d11core;
6918 
6919 	/* read the tsf timer low, then high to get an atomic read */
6920 	*tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
6921 	*tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
6922 }
6923 
6924 /*
6925  * recover 64bit TSF value from the 16bit TSF value in the rx header
6926  * given the assumption that the TSF passed in header is within 65ms
6927  * of the current tsf.
6928  *
6929  * 6       5       4       4       3       2       1
6930  * 3.......6.......8.......0.......2.......4.......6.......8......0
6931  * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
6932  *
6933  * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
6934  * tsf_l is filled in by brcms_b_recv, which is done earlier in the
6935  * receive call sequence after rx interrupt. Only the higher 16 bits
6936  * are used. Finally, the tsf_h is read from the tsf register.
6937  */
6938 static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
6939 				 struct d11rxhdr *rxh)
6940 {
6941 	u32 tsf_h, tsf_l;
6942 	u16 rx_tsf_0_15, rx_tsf_16_31;
6943 
6944 	brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
6945 
6946 	rx_tsf_16_31 = (u16)(tsf_l >> 16);
6947 	rx_tsf_0_15 = rxh->RxTSFTime;
6948 
6949 	/*
6950 	 * a greater tsf time indicates the low 16 bits of
6951 	 * tsf_l wrapped, so decrement the high 16 bits.
6952 	 */
6953 	if ((u16)tsf_l < rx_tsf_0_15) {
6954 		rx_tsf_16_31 -= 1;
6955 		if (rx_tsf_16_31 == 0xffff)
6956 			tsf_h -= 1;
6957 	}
6958 
6959 	return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
6960 }
6961 
6962 static void
6963 prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
6964 		     struct sk_buff *p,
6965 		     struct ieee80211_rx_status *rx_status)
6966 {
6967 	int channel;
6968 	u32 rspec;
6969 	unsigned char *plcp;
6970 
6971 	/* fill in TSF and flag its presence */
6972 	rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
6973 	rx_status->flag |= RX_FLAG_MACTIME_START;
6974 
6975 	channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
6976 
6977 	rx_status->band =
6978 		channel > 14 ? NL80211_BAND_5GHZ : NL80211_BAND_2GHZ;
6979 	rx_status->freq =
6980 		ieee80211_channel_to_frequency(channel, rx_status->band);
6981 
6982 	rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
6983 
6984 	/* noise */
6985 	/* qual */
6986 	rx_status->antenna =
6987 		(rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
6988 
6989 	plcp = p->data;
6990 
6991 	rspec = brcms_c_compute_rspec(rxh, plcp);
6992 	if (is_mcs_rate(rspec)) {
6993 		rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
6994 		rx_status->encoding = RX_ENC_HT;
6995 		if (rspec_is40mhz(rspec))
6996 			rx_status->bw = RATE_INFO_BW_40;
6997 	} else {
6998 		switch (rspec2rate(rspec)) {
6999 		case BRCM_RATE_1M:
7000 			rx_status->rate_idx = 0;
7001 			break;
7002 		case BRCM_RATE_2M:
7003 			rx_status->rate_idx = 1;
7004 			break;
7005 		case BRCM_RATE_5M5:
7006 			rx_status->rate_idx = 2;
7007 			break;
7008 		case BRCM_RATE_11M:
7009 			rx_status->rate_idx = 3;
7010 			break;
7011 		case BRCM_RATE_6M:
7012 			rx_status->rate_idx = 4;
7013 			break;
7014 		case BRCM_RATE_9M:
7015 			rx_status->rate_idx = 5;
7016 			break;
7017 		case BRCM_RATE_12M:
7018 			rx_status->rate_idx = 6;
7019 			break;
7020 		case BRCM_RATE_18M:
7021 			rx_status->rate_idx = 7;
7022 			break;
7023 		case BRCM_RATE_24M:
7024 			rx_status->rate_idx = 8;
7025 			break;
7026 		case BRCM_RATE_36M:
7027 			rx_status->rate_idx = 9;
7028 			break;
7029 		case BRCM_RATE_48M:
7030 			rx_status->rate_idx = 10;
7031 			break;
7032 		case BRCM_RATE_54M:
7033 			rx_status->rate_idx = 11;
7034 			break;
7035 		default:
7036 			brcms_err(wlc->hw->d11core,
7037 				  "%s: Unknown rate\n", __func__);
7038 		}
7039 
7040 		/*
7041 		 * For 5GHz, we should decrease the index as it is
7042 		 * a subset of the 2.4G rates. See bitrates field
7043 		 * of brcms_band_5GHz_nphy (in mac80211_if.c).
7044 		 */
7045 		if (rx_status->band == NL80211_BAND_5GHZ)
7046 			rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
7047 
7048 		/* Determine short preamble and rate_idx */
7049 		if (is_cck_rate(rspec)) {
7050 			if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
7051 				rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
7052 		} else if (is_ofdm_rate(rspec)) {
7053 			rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
7054 		} else {
7055 			brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
7056 				  __func__);
7057 		}
7058 	}
7059 
7060 	if (plcp3_issgi(plcp[3]))
7061 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
7062 
7063 	if (rxh->RxStatus1 & RXS_DECERR) {
7064 		rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
7065 		brcms_err(wlc->hw->d11core, "%s:  RX_FLAG_FAILED_PLCP_CRC\n",
7066 			  __func__);
7067 	}
7068 	if (rxh->RxStatus1 & RXS_FCSERR) {
7069 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
7070 		brcms_err(wlc->hw->d11core, "%s:  RX_FLAG_FAILED_FCS_CRC\n",
7071 			  __func__);
7072 	}
7073 }
7074 
7075 static void
7076 brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
7077 		struct sk_buff *p)
7078 {
7079 	int len_mpdu;
7080 	struct ieee80211_rx_status rx_status;
7081 	struct ieee80211_hdr *hdr;
7082 
7083 	memset(&rx_status, 0, sizeof(rx_status));
7084 	prep_mac80211_status(wlc, rxh, p, &rx_status);
7085 
7086 	/* mac header+body length, exclude CRC and plcp header */
7087 	len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
7088 	skb_pull(p, D11_PHY_HDR_LEN);
7089 	__skb_trim(p, len_mpdu);
7090 
7091 	/* unmute transmit */
7092 	if (wlc->hw->suspended_fifos) {
7093 		hdr = (struct ieee80211_hdr *)p->data;
7094 		if (ieee80211_is_beacon(hdr->frame_control))
7095 			brcms_b_mute(wlc->hw, false);
7096 	}
7097 
7098 	memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
7099 	ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
7100 }
7101 
7102 /* calculate frame duration for Mixed-mode L-SIG spoofing, return
7103  * number of bytes goes in the length field
7104  *
7105  * Formula given by HT PHY Spec v 1.13
7106  *   len = 3(nsyms + nstream + 3) - 3
7107  */
7108 u16
7109 brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
7110 		      uint mac_len)
7111 {
7112 	uint nsyms, len = 0, kNdps;
7113 
7114 	if (is_mcs_rate(ratespec)) {
7115 		uint mcs = ratespec & RSPEC_RATE_MASK;
7116 		int tot_streams = (mcs_2_txstreams(mcs) + 1) +
7117 				  rspec_stc(ratespec);
7118 
7119 		/*
7120 		 * the payload duration calculation matches that
7121 		 * of regular ofdm
7122 		 */
7123 		/* 1000Ndbps = kbps * 4 */
7124 		kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
7125 				   rspec_issgi(ratespec)) * 4;
7126 
7127 		if (rspec_stc(ratespec) == 0)
7128 			nsyms =
7129 			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7130 				  APHY_TAIL_NBITS) * 1000, kNdps);
7131 		else
7132 			/* STBC needs to have even number of symbols */
7133 			nsyms =
7134 			    2 *
7135 			    CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
7136 				  APHY_TAIL_NBITS) * 1000, 2 * kNdps);
7137 
7138 		/* (+3) account for HT-SIG(2) and HT-STF(1) */
7139 		nsyms += (tot_streams + 3);
7140 		/*
7141 		 * 3 bytes/symbol @ legacy 6Mbps rate
7142 		 * (-3) excluding service bits and tail bits
7143 		 */
7144 		len = (3 * nsyms) - 3;
7145 	}
7146 
7147 	return (u16) len;
7148 }
7149 
7150 static void
7151 brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
7152 {
7153 	const struct brcms_c_rateset *rs_dflt;
7154 	struct brcms_c_rateset rs;
7155 	u8 rate;
7156 	u16 entry_ptr;
7157 	u8 plcp[D11_PHY_HDR_LEN];
7158 	u16 dur, sifs;
7159 	uint i;
7160 
7161 	sifs = get_sifs(wlc->band);
7162 
7163 	rs_dflt = brcms_c_rateset_get_hwrs(wlc);
7164 
7165 	brcms_c_rateset_copy(rs_dflt, &rs);
7166 	brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
7167 
7168 	/*
7169 	 * walk the phy rate table and update MAC core SHM
7170 	 * basic rate table entries
7171 	 */
7172 	for (i = 0; i < rs.count; i++) {
7173 		rate = rs.rates[i] & BRCMS_RATE_MASK;
7174 
7175 		entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
7176 
7177 		/* Calculate the Probe Response PLCP for the given rate */
7178 		brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
7179 
7180 		/*
7181 		 * Calculate the duration of the Probe Response
7182 		 * frame plus SIFS for the MAC
7183 		 */
7184 		dur = (u16) brcms_c_calc_frame_time(wlc, rate,
7185 						BRCMS_LONG_PREAMBLE, frame_len);
7186 		dur += sifs;
7187 
7188 		/* Update the SHM Rate Table entry Probe Response values */
7189 		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
7190 			      (u16) (plcp[0] + (plcp[1] << 8)));
7191 		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
7192 			      (u16) (plcp[2] + (plcp[3] << 8)));
7193 		brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
7194 	}
7195 }
7196 
7197 int brcms_c_get_header_len(void)
7198 {
7199 	return TXOFF;
7200 }
7201 
7202 static void brcms_c_beacon_write(struct brcms_c_info *wlc,
7203 				 struct sk_buff *beacon, u16 tim_offset,
7204 				 u16 dtim_period, bool bcn0, bool bcn1)
7205 {
7206 	size_t len;
7207 	struct ieee80211_tx_info *tx_info;
7208 	struct brcms_hardware *wlc_hw = wlc->hw;
7209 	struct ieee80211_hw *ieee_hw = brcms_c_pub(wlc)->ieee_hw;
7210 
7211 	/* Get tx_info */
7212 	tx_info = IEEE80211_SKB_CB(beacon);
7213 
7214 	len = min_t(size_t, beacon->len, BCN_TMPL_LEN);
7215 	wlc->bcn_rspec = ieee80211_get_tx_rate(ieee_hw, tx_info)->hw_value;
7216 
7217 	brcms_c_compute_plcp(wlc, wlc->bcn_rspec,
7218 			     len + FCS_LEN - D11_PHY_HDR_LEN, beacon->data);
7219 
7220 	/* "Regular" and 16 MBSS but not for 4 MBSS */
7221 	/* Update the phytxctl for the beacon based on the rspec */
7222 	brcms_c_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
7223 
7224 	if (bcn0) {
7225 		/* write the probe response into the template region */
7226 		brcms_b_write_template_ram(wlc_hw, T_BCN0_TPL_BASE,
7227 					    (len + 3) & ~3, beacon->data);
7228 
7229 		/* write beacon length to SCR */
7230 		brcms_b_write_shm(wlc_hw, M_BCN0_FRM_BYTESZ, (u16) len);
7231 	}
7232 	if (bcn1) {
7233 		/* write the probe response into the template region */
7234 		brcms_b_write_template_ram(wlc_hw, T_BCN1_TPL_BASE,
7235 					    (len + 3) & ~3, beacon->data);
7236 
7237 		/* write beacon length to SCR */
7238 		brcms_b_write_shm(wlc_hw, M_BCN1_FRM_BYTESZ, (u16) len);
7239 	}
7240 
7241 	if (tim_offset != 0) {
7242 		brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
7243 				  tim_offset + D11B_PHY_HDR_LEN);
7244 		brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, dtim_period);
7245 	} else {
7246 		brcms_b_write_shm(wlc_hw, M_TIMBPOS_INBEACON,
7247 				  len + D11B_PHY_HDR_LEN);
7248 		brcms_b_write_shm(wlc_hw, M_DOT11_DTIMPERIOD, 0);
7249 	}
7250 }
7251 
7252 static void brcms_c_update_beacon_hw(struct brcms_c_info *wlc,
7253 				     struct sk_buff *beacon, u16 tim_offset,
7254 				     u16 dtim_period)
7255 {
7256 	struct brcms_hardware *wlc_hw = wlc->hw;
7257 	struct bcma_device *core = wlc_hw->d11core;
7258 
7259 	/* Hardware beaconing for this config */
7260 	u32 both_valid = MCMD_BCN0VLD | MCMD_BCN1VLD;
7261 
7262 	/* Check if both templates are in use, if so sched. an interrupt
7263 	 *      that will call back into this routine
7264 	 */
7265 	if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid)
7266 		/* clear any previous status */
7267 		bcma_write32(core, D11REGOFFS(macintstatus), MI_BCNTPL);
7268 
7269 	if (wlc->beacon_template_virgin) {
7270 		wlc->beacon_template_virgin = false;
7271 		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
7272 				     true);
7273 		/* mark beacon0 valid */
7274 		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
7275 		return;
7276 	}
7277 
7278 	/* Check that after scheduling the interrupt both of the
7279 	 *      templates are still busy. if not clear the int. & remask
7280 	 */
7281 	if ((bcma_read32(core, D11REGOFFS(maccommand)) & both_valid) == both_valid) {
7282 		wlc->defmacintmask |= MI_BCNTPL;
7283 		return;
7284 	}
7285 
7286 	if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN0VLD)) {
7287 		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period, true,
7288 				     false);
7289 		/* mark beacon0 valid */
7290 		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN0VLD);
7291 		return;
7292 	}
7293 	if (!(bcma_read32(core, D11REGOFFS(maccommand)) & MCMD_BCN1VLD)) {
7294 		brcms_c_beacon_write(wlc, beacon, tim_offset, dtim_period,
7295 				     false, true);
7296 		/* mark beacon0 valid */
7297 		bcma_set32(core, D11REGOFFS(maccommand), MCMD_BCN1VLD);
7298 	}
7299 }
7300 
7301 /*
7302  * Update all beacons for the system.
7303  */
7304 void brcms_c_update_beacon(struct brcms_c_info *wlc)
7305 {
7306 	struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7307 
7308 	if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
7309 			     bsscfg->type == BRCMS_TYPE_ADHOC)) {
7310 		/* Clear the soft intmask */
7311 		wlc->defmacintmask &= ~MI_BCNTPL;
7312 		if (!wlc->beacon)
7313 			return;
7314 		brcms_c_update_beacon_hw(wlc, wlc->beacon,
7315 					 wlc->beacon_tim_offset,
7316 					 wlc->beacon_dtim_period);
7317 	}
7318 }
7319 
7320 void brcms_c_set_new_beacon(struct brcms_c_info *wlc, struct sk_buff *beacon,
7321 			    u16 tim_offset, u16 dtim_period)
7322 {
7323 	if (!beacon)
7324 		return;
7325 	if (wlc->beacon)
7326 		dev_kfree_skb_any(wlc->beacon);
7327 	wlc->beacon = beacon;
7328 
7329 	/* add PLCP */
7330 	skb_push(wlc->beacon, D11_PHY_HDR_LEN);
7331 	wlc->beacon_tim_offset = tim_offset;
7332 	wlc->beacon_dtim_period = dtim_period;
7333 	brcms_c_update_beacon(wlc);
7334 }
7335 
7336 void brcms_c_set_new_probe_resp(struct brcms_c_info *wlc,
7337 				struct sk_buff *probe_resp)
7338 {
7339 	if (!probe_resp)
7340 		return;
7341 	if (wlc->probe_resp)
7342 		dev_kfree_skb_any(wlc->probe_resp);
7343 	wlc->probe_resp = probe_resp;
7344 
7345 	/* add PLCP */
7346 	skb_push(wlc->probe_resp, D11_PHY_HDR_LEN);
7347 	brcms_c_update_probe_resp(wlc, false);
7348 }
7349 
7350 void brcms_c_enable_probe_resp(struct brcms_c_info *wlc, bool enable)
7351 {
7352 	/*
7353 	 * prevent ucode from sending probe responses by setting the timeout
7354 	 * to 1, it can not send it in that time frame.
7355 	 */
7356 	wlc->prb_resp_timeout = enable ? BRCMS_PRB_RESP_TIMEOUT : 1;
7357 	brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
7358 	/* TODO: if (enable) => also deactivate receiving of probe request */
7359 }
7360 
7361 /* Write ssid into shared memory */
7362 static void
7363 brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
7364 {
7365 	u8 *ssidptr = cfg->SSID;
7366 	u16 base = M_SSID;
7367 	u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
7368 
7369 	/* padding the ssid with zero and copy it into shm */
7370 	memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
7371 	memcpy(ssidbuf, ssidptr, cfg->SSID_len);
7372 
7373 	brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
7374 	brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
7375 }
7376 
7377 static void
7378 brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
7379 			      struct brcms_bss_cfg *cfg,
7380 			      struct sk_buff *probe_resp,
7381 			      bool suspend)
7382 {
7383 	int len;
7384 
7385 	len = min_t(size_t, probe_resp->len, BCN_TMPL_LEN);
7386 
7387 	if (suspend)
7388 		brcms_c_suspend_mac_and_wait(wlc);
7389 
7390 	/* write the probe response into the template region */
7391 	brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
7392 				    (len + 3) & ~3, probe_resp->data);
7393 
7394 	/* write the length of the probe response frame (+PLCP/-FCS) */
7395 	brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
7396 
7397 	/* write the SSID and SSID length */
7398 	brcms_c_shm_ssid_upd(wlc, cfg);
7399 
7400 	/*
7401 	 * Write PLCP headers and durations for probe response frames
7402 	 * at all rates. Use the actual frame length covered by the
7403 	 * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
7404 	 * by subtracting the PLCP len and adding the FCS.
7405 	 */
7406 	brcms_c_mod_prb_rsp_rate_table(wlc,
7407 				      (u16)len + FCS_LEN - D11_PHY_HDR_LEN);
7408 
7409 	if (suspend)
7410 		brcms_c_enable_mac(wlc);
7411 }
7412 
7413 void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
7414 {
7415 	struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
7416 
7417 	/* update AP or IBSS probe responses */
7418 	if (wlc->pub->up && (bsscfg->type == BRCMS_TYPE_AP ||
7419 			     bsscfg->type == BRCMS_TYPE_ADHOC)) {
7420 		if (!wlc->probe_resp)
7421 			return;
7422 		brcms_c_bss_update_probe_resp(wlc, bsscfg, wlc->probe_resp,
7423 					      suspend);
7424 	}
7425 }
7426 
7427 int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
7428 			   uint *blocks)
7429 {
7430 	if (fifo >= NFIFO)
7431 		return -EINVAL;
7432 
7433 	*blocks = wlc_hw->xmtfifo_sz[fifo];
7434 
7435 	return 0;
7436 }
7437 
7438 void
7439 brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
7440 		  const u8 *addr)
7441 {
7442 	brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
7443 	if (match_reg_offset == RCM_BSSID_OFFSET)
7444 		memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
7445 }
7446 
7447 /*
7448  * Flag 'scan in progress' to withhold dynamic phy calibration
7449  */
7450 void brcms_c_scan_start(struct brcms_c_info *wlc)
7451 {
7452 	wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
7453 }
7454 
7455 void brcms_c_scan_stop(struct brcms_c_info *wlc)
7456 {
7457 	wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
7458 }
7459 
7460 void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
7461 {
7462 	wlc->pub->associated = state;
7463 }
7464 
7465 /*
7466  * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
7467  * AMPDU traffic, packets pending in hardware have to be invalidated so that
7468  * when later on hardware releases them, they can be handled appropriately.
7469  */
7470 void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
7471 			       struct ieee80211_sta *sta,
7472 			       void (*dma_callback_fn))
7473 {
7474 	struct dma_pub *dmah;
7475 	int i;
7476 	for (i = 0; i < NFIFO; i++) {
7477 		dmah = hw->di[i];
7478 		if (dmah != NULL)
7479 			dma_walk_packets(dmah, dma_callback_fn, sta);
7480 	}
7481 }
7482 
7483 int brcms_c_get_curband(struct brcms_c_info *wlc)
7484 {
7485 	return wlc->band->bandunit;
7486 }
7487 
7488 bool brcms_c_tx_flush_completed(struct brcms_c_info *wlc)
7489 {
7490 	int i;
7491 
7492 	/* Kick DMA to send any pending AMPDU */
7493 	for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
7494 		if (wlc->hw->di[i])
7495 			dma_kick_tx(wlc->hw->di[i]);
7496 
7497 	return !brcms_txpktpendtot(wlc);
7498 }
7499 
7500 void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
7501 {
7502 	wlc->bcn_li_bcn = interval;
7503 	if (wlc->pub->up)
7504 		brcms_c_bcn_li_upd(wlc);
7505 }
7506 
7507 u64 brcms_c_tsf_get(struct brcms_c_info *wlc)
7508 {
7509 	u32 tsf_h, tsf_l;
7510 	u64 tsf;
7511 
7512 	brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
7513 
7514 	tsf = tsf_h;
7515 	tsf <<= 32;
7516 	tsf |= tsf_l;
7517 
7518 	return tsf;
7519 }
7520 
7521 void brcms_c_tsf_set(struct brcms_c_info *wlc, u64 tsf)
7522 {
7523 	u32 tsf_h, tsf_l;
7524 
7525 	brcms_c_time_lock(wlc);
7526 
7527 	tsf_l = tsf;
7528 	tsf_h = (tsf >> 32);
7529 
7530 	/* read the tsf timer low, then high to get an atomic read */
7531 	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerlow), tsf_l);
7532 	bcma_write32(wlc->hw->d11core, D11REGOFFS(tsf_timerhigh), tsf_h);
7533 
7534 	brcms_c_time_unlock(wlc);
7535 }
7536 
7537 int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
7538 {
7539 	uint qdbm;
7540 
7541 	/* Remove override bit and clip to max qdbm value */
7542 	qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
7543 	return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
7544 }
7545 
7546 int brcms_c_get_tx_power(struct brcms_c_info *wlc)
7547 {
7548 	uint qdbm;
7549 	bool override;
7550 
7551 	wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
7552 
7553 	/* Return qdbm units */
7554 	return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
7555 }
7556 
7557 /* Process received frames */
7558 /*
7559  * Return true if more frames need to be processed. false otherwise.
7560  * Param 'bound' indicates max. # frames to process before break out.
7561  */
7562 static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
7563 {
7564 	struct d11rxhdr *rxh;
7565 	struct ieee80211_hdr *h;
7566 	uint len;
7567 	bool is_amsdu;
7568 
7569 	/* frame starts with rxhdr */
7570 	rxh = (struct d11rxhdr *) (p->data);
7571 
7572 	/* strip off rxhdr */
7573 	skb_pull(p, BRCMS_HWRXOFF);
7574 
7575 	/* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
7576 	if (rxh->RxStatus1 & RXS_PBPRES) {
7577 		if (p->len < 2) {
7578 			brcms_err(wlc->hw->d11core,
7579 				  "wl%d: recv: rcvd runt of len %d\n",
7580 				  wlc->pub->unit, p->len);
7581 			goto toss;
7582 		}
7583 		skb_pull(p, 2);
7584 	}
7585 
7586 	h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
7587 	len = p->len;
7588 
7589 	if (rxh->RxStatus1 & RXS_FCSERR) {
7590 		if (!(wlc->filter_flags & FIF_FCSFAIL))
7591 			goto toss;
7592 	}
7593 
7594 	/* check received pkt has at least frame control field */
7595 	if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
7596 		goto toss;
7597 
7598 	/* not supporting A-MSDU */
7599 	is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
7600 	if (is_amsdu)
7601 		goto toss;
7602 
7603 	brcms_c_recvctl(wlc, rxh, p);
7604 	return;
7605 
7606  toss:
7607 	brcmu_pkt_buf_free_skb(p);
7608 }
7609 
7610 /* Process received frames */
7611 /*
7612  * Return true if more frames need to be processed. false otherwise.
7613  * Param 'bound' indicates max. # frames to process before break out.
7614  */
7615 static bool
7616 brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
7617 {
7618 	struct sk_buff *p;
7619 	struct sk_buff *next = NULL;
7620 	struct sk_buff_head recv_frames;
7621 
7622 	uint n = 0;
7623 	uint bound_limit = bound ? RXBND : -1;
7624 	bool morepending = false;
7625 
7626 	skb_queue_head_init(&recv_frames);
7627 
7628 	/* gather received frames */
7629 	do {
7630 		/* !give others some time to run! */
7631 		if (n >= bound_limit)
7632 			break;
7633 
7634 		morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
7635 		n++;
7636 	} while (morepending);
7637 
7638 	/* post more rbufs */
7639 	dma_rxfill(wlc_hw->di[fifo]);
7640 
7641 	/* process each frame */
7642 	skb_queue_walk_safe(&recv_frames, p, next) {
7643 		struct d11rxhdr_le *rxh_le;
7644 		struct d11rxhdr *rxh;
7645 
7646 		skb_unlink(p, &recv_frames);
7647 		rxh_le = (struct d11rxhdr_le *)p->data;
7648 		rxh = (struct d11rxhdr *)p->data;
7649 
7650 		/* fixup rx header endianness */
7651 		rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
7652 		rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
7653 		rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
7654 		rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
7655 		rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
7656 		rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
7657 		rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
7658 		rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
7659 		rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
7660 		rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
7661 		rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
7662 
7663 		brcms_c_recv(wlc_hw->wlc, p);
7664 	}
7665 
7666 	return morepending;
7667 }
7668 
7669 /* second-level interrupt processing
7670  *   Return true if another dpc needs to be re-scheduled. false otherwise.
7671  *   Param 'bounded' indicates if applicable loops should be bounded.
7672  */
7673 bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
7674 {
7675 	u32 macintstatus;
7676 	struct brcms_hardware *wlc_hw = wlc->hw;
7677 	struct bcma_device *core = wlc_hw->d11core;
7678 
7679 	if (brcms_deviceremoved(wlc)) {
7680 		brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
7681 			  __func__);
7682 		brcms_down(wlc->wl);
7683 		return false;
7684 	}
7685 
7686 	/* grab and clear the saved software intstatus bits */
7687 	macintstatus = wlc->macintstatus;
7688 	wlc->macintstatus = 0;
7689 
7690 	brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
7691 		      wlc_hw->unit, macintstatus);
7692 
7693 	WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
7694 
7695 	/* tx status */
7696 	if (macintstatus & MI_TFS) {
7697 		bool fatal;
7698 		if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
7699 			wlc->macintstatus |= MI_TFS;
7700 		if (fatal) {
7701 			brcms_err(core, "MI_TFS: fatal\n");
7702 			goto fatal;
7703 		}
7704 	}
7705 
7706 	if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
7707 		brcms_c_tbtt(wlc);
7708 
7709 	/* ATIM window end */
7710 	if (macintstatus & MI_ATIMWINEND) {
7711 		brcms_dbg_info(core, "end of ATIM window\n");
7712 		bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
7713 		wlc->qvalid = 0;
7714 	}
7715 
7716 	/*
7717 	 * received data or control frame, MI_DMAINT is
7718 	 * indication of RX_FIFO interrupt
7719 	 */
7720 	if (macintstatus & MI_DMAINT)
7721 		if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
7722 			wlc->macintstatus |= MI_DMAINT;
7723 
7724 	/* noise sample collected */
7725 	if (macintstatus & MI_BG_NOISE)
7726 		wlc_phy_noise_sample_intr(wlc_hw->band->pi);
7727 
7728 	if (macintstatus & MI_GP0) {
7729 		brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
7730 			  "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
7731 
7732 		printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
7733 			    __func__, ai_get_chip_id(wlc_hw->sih),
7734 			    ai_get_chiprev(wlc_hw->sih));
7735 		brcms_fatal_error(wlc_hw->wlc->wl);
7736 	}
7737 
7738 	/* gptimer timeout */
7739 	if (macintstatus & MI_TO)
7740 		bcma_write32(core, D11REGOFFS(gptimer), 0);
7741 
7742 	if (macintstatus & MI_RFDISABLE) {
7743 		brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
7744 			       " RF Disable Input\n", wlc_hw->unit);
7745 		brcms_rfkill_set_hw_state(wlc->wl);
7746 	}
7747 
7748 	/* BCN template is available */
7749 	if (macintstatus & MI_BCNTPL)
7750 		brcms_c_update_beacon(wlc);
7751 
7752 	/* it isn't done and needs to be resched if macintstatus is non-zero */
7753 	return wlc->macintstatus != 0;
7754 
7755  fatal:
7756 	brcms_fatal_error(wlc_hw->wlc->wl);
7757 	return wlc->macintstatus != 0;
7758 }
7759 
7760 void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
7761 {
7762 	struct bcma_device *core = wlc->hw->d11core;
7763 	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.chandef.chan;
7764 	u16 chanspec;
7765 
7766 	brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
7767 
7768 	chanspec = ch20mhz_chspec(ch->hw_value);
7769 
7770 	brcms_b_init(wlc->hw, chanspec);
7771 
7772 	/* update beacon listen interval */
7773 	brcms_c_bcn_li_upd(wlc);
7774 
7775 	/* write ethernet address to core */
7776 	brcms_c_set_mac(wlc->bsscfg);
7777 	brcms_c_set_bssid(wlc->bsscfg);
7778 
7779 	/* Update tsf_cfprep if associated and up */
7780 	if (wlc->pub->associated && wlc->pub->up) {
7781 		u32 bi;
7782 
7783 		/* get beacon period and convert to uS */
7784 		bi = wlc->bsscfg->current_bss.beacon_period << 10;
7785 		/*
7786 		 * update since init path would reset
7787 		 * to default value
7788 		 */
7789 		bcma_write32(core, D11REGOFFS(tsf_cfprep),
7790 			     bi << CFPREP_CBI_SHIFT);
7791 
7792 		/* Update maccontrol PM related bits */
7793 		brcms_c_set_ps_ctrl(wlc);
7794 	}
7795 
7796 	brcms_c_bandinit_ordered(wlc, chanspec);
7797 
7798 	/* init probe response timeout */
7799 	brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
7800 
7801 	/* init max burst txop (framebursting) */
7802 	brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
7803 		      (wlc->
7804 		       _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
7805 
7806 	/* initialize maximum allowed duty cycle */
7807 	brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
7808 	brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
7809 
7810 	/*
7811 	 * Update some shared memory locations related to
7812 	 * max AMPDU size allowed to received
7813 	 */
7814 	brcms_c_ampdu_shm_upd(wlc->ampdu);
7815 
7816 	/* band-specific inits */
7817 	brcms_c_bsinit(wlc);
7818 
7819 	/* Enable EDCF mode (while the MAC is suspended) */
7820 	bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
7821 	brcms_c_edcf_setparams(wlc, false);
7822 
7823 	/* read the ucode version if we have not yet done so */
7824 	if (wlc->ucode_rev == 0) {
7825 		u16 rev;
7826 		u16 patch;
7827 
7828 		rev = brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR);
7829 		patch = brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
7830 		wlc->ucode_rev = (rev << NBITS(u16)) | patch;
7831 		snprintf(wlc->wiphy->fw_version,
7832 			 sizeof(wlc->wiphy->fw_version), "%u.%u", rev, patch);
7833 	}
7834 
7835 	/* ..now really unleash hell (allow the MAC out of suspend) */
7836 	brcms_c_enable_mac(wlc);
7837 
7838 	/* suspend the tx fifos and mute the phy for preism cac time */
7839 	if (mute_tx)
7840 		brcms_b_mute(wlc->hw, true);
7841 
7842 	/* enable the RF Disable Delay timer */
7843 	bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
7844 
7845 	/*
7846 	 * Initialize WME parameters; if they haven't been set by some other
7847 	 * mechanism (IOVar, etc) then read them from the hardware.
7848 	 */
7849 	if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
7850 		/* Uninitialized; read from HW */
7851 		int ac;
7852 
7853 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
7854 			wlc->wme_retries[ac] =
7855 			    brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
7856 	}
7857 }
7858 
7859 /*
7860  * The common driver entry routine. Error codes should be unique
7861  */
7862 struct brcms_c_info *
7863 brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
7864 	       bool piomode, uint *perr)
7865 {
7866 	struct brcms_c_info *wlc;
7867 	uint err = 0;
7868 	uint i, j;
7869 	struct brcms_pub *pub;
7870 
7871 	/* allocate struct brcms_c_info state and its substructures */
7872 	wlc = brcms_c_attach_malloc(unit, &err, 0);
7873 	if (wlc == NULL)
7874 		goto fail;
7875 	wlc->wiphy = wl->wiphy;
7876 	pub = wlc->pub;
7877 
7878 #if defined(DEBUG)
7879 	wlc_info_dbg = wlc;
7880 #endif
7881 
7882 	wlc->band = wlc->bandstate[0];
7883 	wlc->core = wlc->corestate;
7884 	wlc->wl = wl;
7885 	pub->unit = unit;
7886 	pub->_piomode = piomode;
7887 	wlc->bandinit_pending = false;
7888 	wlc->beacon_template_virgin = true;
7889 
7890 	/* populate struct brcms_c_info with default values  */
7891 	brcms_c_info_init(wlc, unit);
7892 
7893 	/* update sta/ap related parameters */
7894 	brcms_c_ap_upd(wlc);
7895 
7896 	/*
7897 	 * low level attach steps(all hw accesses go
7898 	 * inside, no more in rest of the attach)
7899 	 */
7900 	err = brcms_b_attach(wlc, core, unit, piomode);
7901 	if (err)
7902 		goto fail;
7903 
7904 	brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
7905 
7906 	pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
7907 
7908 	/* disable allowed duty cycle */
7909 	wlc->tx_duty_cycle_ofdm = 0;
7910 	wlc->tx_duty_cycle_cck = 0;
7911 
7912 	brcms_c_stf_phy_chain_calc(wlc);
7913 
7914 	/* txchain 1: txant 0, txchain 2: txant 1 */
7915 	if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
7916 		wlc->stf->txant = wlc->stf->hw_txchain - 1;
7917 
7918 	/* push to BMAC driver */
7919 	wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
7920 			       wlc->stf->hw_rxchain);
7921 
7922 	/* pull up some info resulting from the low attach */
7923 	for (i = 0; i < NFIFO; i++)
7924 		wlc->core->txavail[i] = wlc->hw->txavail[i];
7925 
7926 	memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7927 	memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
7928 
7929 	for (j = 0; j < wlc->pub->_nbands; j++) {
7930 		wlc->band = wlc->bandstate[j];
7931 
7932 		if (!brcms_c_attach_stf_ant_init(wlc)) {
7933 			err = 24;
7934 			goto fail;
7935 		}
7936 
7937 		/* default contention windows size limits */
7938 		wlc->band->CWmin = APHY_CWMIN;
7939 		wlc->band->CWmax = PHY_CWMAX;
7940 
7941 		/* init gmode value */
7942 		if (wlc->band->bandtype == BRCM_BAND_2G) {
7943 			wlc->band->gmode = GMODE_AUTO;
7944 			brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
7945 					   wlc->band->gmode);
7946 		}
7947 
7948 		/* init _n_enab supported mode */
7949 		if (BRCMS_PHY_11N_CAP(wlc->band)) {
7950 			pub->_n_enab = SUPPORT_11N;
7951 			brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
7952 						   ((pub->_n_enab ==
7953 						     SUPPORT_11N) ? WL_11N_2x2 :
7954 						    WL_11N_3x3));
7955 		}
7956 
7957 		/* init per-band default rateset, depend on band->gmode */
7958 		brcms_default_rateset(wlc, &wlc->band->defrateset);
7959 
7960 		/* fill in hw_rateset */
7961 		brcms_c_rateset_filter(&wlc->band->defrateset,
7962 				   &wlc->band->hw_rateset, false,
7963 				   BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
7964 				   (bool) (wlc->pub->_n_enab & SUPPORT_11N));
7965 	}
7966 
7967 	/*
7968 	 * update antenna config due to
7969 	 * wlc->stf->txant/txchain/ant_rx_ovr change
7970 	 */
7971 	brcms_c_stf_phy_txant_upd(wlc);
7972 
7973 	/* attach each modules */
7974 	err = brcms_c_attach_module(wlc);
7975 	if (err != 0)
7976 		goto fail;
7977 
7978 	if (!brcms_c_timers_init(wlc, unit)) {
7979 		wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
7980 			  __func__);
7981 		err = 32;
7982 		goto fail;
7983 	}
7984 
7985 	/* depend on rateset, gmode */
7986 	wlc->cmi = brcms_c_channel_mgr_attach(wlc);
7987 	if (!wlc->cmi) {
7988 		wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
7989 			  "\n", unit, __func__);
7990 		err = 33;
7991 		goto fail;
7992 	}
7993 
7994 	/* init default when all parameters are ready, i.e. ->rateset */
7995 	brcms_c_bss_default_init(wlc);
7996 
7997 	/*
7998 	 * Complete the wlc default state initializations..
7999 	 */
8000 
8001 	wlc->bsscfg->wlc = wlc;
8002 
8003 	wlc->mimoft = FT_HT;
8004 	wlc->mimo_40txbw = AUTO;
8005 	wlc->ofdm_40txbw = AUTO;
8006 	wlc->cck_40txbw = AUTO;
8007 	brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
8008 
8009 	/* Set default values of SGI */
8010 	if (BRCMS_SGI_CAP_PHY(wlc)) {
8011 		brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8012 					       BRCMS_N_SGI_40));
8013 	} else if (BRCMS_ISSSLPNPHY(wlc->band)) {
8014 		brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
8015 					       BRCMS_N_SGI_40));
8016 	} else {
8017 		brcms_c_ht_update_sgi_rx(wlc, 0);
8018 	}
8019 
8020 	brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
8021 
8022 	if (perr)
8023 		*perr = 0;
8024 
8025 	return wlc;
8026 
8027  fail:
8028 	wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
8029 		  unit, __func__, err);
8030 	if (wlc)
8031 		brcms_c_detach(wlc);
8032 
8033 	if (perr)
8034 		*perr = err;
8035 	return NULL;
8036 }
8037