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