AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / sys / dev / netif / bwi / bwimac.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/netif/bwi/bwimac.c,v 1.13 2008/02/15 11:15:38 sephe Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/bitops.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/firmware.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/rman.h>
46 #include <sys/serialize.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50
51 #include <net/ethernet.h>
52 #include <net/if.h>
53 #include <net/bpf.h>
54 #include <net/if_arp.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/ifq_var.h>
58
59 #include <netproto/802_11/ieee80211_radiotap.h>
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
62
63 #include <bus/pci/pcireg.h>
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcidevs.h>
66
67 #include <dev/netif/bwi/if_bwireg.h>
68 #include <dev/netif/bwi/if_bwivar.h>
69 #include <dev/netif/bwi/bwiphy.h>
70 #include <dev/netif/bwi/bwirf.h>
71 #include <dev/netif/bwi/bwimac.h>
72
73 struct bwi_retry_lim {
74         uint16_t        shretry;
75         uint16_t        shretry_fb;
76         uint16_t        lgretry;
77         uint16_t        lgretry_fb;
78 };
79
80 static int      bwi_mac_test(struct bwi_mac *);
81 static int      bwi_mac_get_property(struct bwi_mac *);
82
83 static void     bwi_mac_set_retry_lim(struct bwi_mac *,
84                         const struct bwi_retry_lim *);
85 static void     bwi_mac_set_ackrates(struct bwi_mac *,
86                         const struct ieee80211_rateset *);
87
88 static int      bwi_mac_gpio_init(struct bwi_mac *);
89 static int      bwi_mac_gpio_fini(struct bwi_mac *);
90 static void     bwi_mac_opmode_init(struct bwi_mac *);
91 static void     bwi_mac_hostflags_init(struct bwi_mac *);
92 static void     bwi_mac_bss_param_init(struct bwi_mac *);
93
94 static int      bwi_mac_fw_alloc(struct bwi_mac *);
95 static void     bwi_mac_fw_free(struct bwi_mac *);
96 static int      bwi_mac_fw_load(struct bwi_mac *);
97 static int      bwi_mac_fw_init(struct bwi_mac *);
98 static int      bwi_mac_fw_load_iv(struct bwi_mac *, const struct fw_image *);
99
100 static void     bwi_mac_setup_tpctl(struct bwi_mac *);
101 static void     bwi_mac_adjust_tpctl(struct bwi_mac *, int, int);
102
103 static void     bwi_mac_lock(struct bwi_mac *);
104 static void     bwi_mac_unlock(struct bwi_mac *);
105
106 static const uint8_t bwi_sup_macrev[] = { 2, 4, 5, 6, 7, 9, 10 };
107
108 void
109 bwi_tmplt_write_4(struct bwi_mac *mac, uint32_t ofs, uint32_t val)
110 {
111         struct bwi_softc *sc = mac->mac_sc;
112
113         if (mac->mac_flags & BWI_MAC_F_BSWAP)
114                 val = bswap32(val);
115
116         CSR_WRITE_4(sc, BWI_MAC_TMPLT_CTRL, ofs);
117         CSR_WRITE_4(sc, BWI_MAC_TMPLT_DATA, val);
118 }
119
120 void
121 bwi_hostflags_write(struct bwi_mac *mac, uint64_t flags)
122 {
123         uint64_t val;
124
125         val = flags & 0xffff;
126         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_LO, val);
127
128         val = (flags >> 16) & 0xffff;
129         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_MI, val);
130
131         /* HI has unclear meaning, so leave it as it is */
132 }
133
134 uint64_t
135 bwi_hostflags_read(struct bwi_mac *mac)
136 {
137         uint64_t flags, val;
138
139         /* HI has unclear meaning, so don't touch it */
140         flags = 0;
141
142         val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_MI);
143         flags |= val << 16;
144
145         val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_LO);
146         flags |= val;
147
148         return flags;
149 }
150
151 uint16_t
152 bwi_memobj_read_2(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0)
153 {
154         struct bwi_softc *sc = mac->mac_sc;
155         uint32_t data_reg;
156         int ofs;
157
158         data_reg = BWI_MOBJ_DATA;
159         ofs = ofs0 / 4;
160
161         if (ofs0 % 4 != 0)
162                 data_reg = BWI_MOBJ_DATA_UNALIGN;
163
164         CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
165         return CSR_READ_2(sc, data_reg);
166 }
167
168 uint32_t
169 bwi_memobj_read_4(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0)
170 {
171         struct bwi_softc *sc = mac->mac_sc;
172         int ofs;
173
174         ofs = ofs0 / 4;
175         if (ofs0 % 4 != 0) {
176                 uint32_t ret;
177
178                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
179                 ret = CSR_READ_2(sc, BWI_MOBJ_DATA_UNALIGN);
180                 ret <<= 16;
181
182                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
183                             BWI_MOBJ_CTRL_VAL(obj_id, ofs + 1));
184                 ret |= CSR_READ_2(sc, BWI_MOBJ_DATA);
185
186                 return ret;
187         } else {
188                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
189                 return CSR_READ_4(sc, BWI_MOBJ_DATA);
190         }
191 }
192
193 void
194 bwi_memobj_write_2(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0,
195                    uint16_t v)
196 {
197         struct bwi_softc *sc = mac->mac_sc;
198         uint32_t data_reg;
199         int ofs;
200
201         data_reg = BWI_MOBJ_DATA;
202         ofs = ofs0 / 4;
203
204         if (ofs0 % 4 != 0)
205                 data_reg = BWI_MOBJ_DATA_UNALIGN;
206
207         CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
208         CSR_WRITE_2(sc, data_reg, v);
209 }
210
211 void
212 bwi_memobj_write_4(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0,
213                    uint32_t v)
214 {
215         struct bwi_softc *sc = mac->mac_sc;
216         int ofs;
217
218         ofs = ofs0 / 4;
219         if (ofs0 % 4 != 0) {
220                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
221                 CSR_WRITE_2(sc, BWI_MOBJ_DATA_UNALIGN, v >> 16);
222
223                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
224                             BWI_MOBJ_CTRL_VAL(obj_id, ofs + 1));
225                 CSR_WRITE_2(sc, BWI_MOBJ_DATA, v & 0xffff);
226         } else {
227                 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
228                 CSR_WRITE_4(sc, BWI_MOBJ_DATA, v);
229         }
230 }
231
232 int
233 bwi_mac_lateattach(struct bwi_mac *mac)
234 {
235         int error;
236
237         if (mac->mac_rev >= 5)
238                 CSR_READ_4(mac->mac_sc, BWI_STATE_HI); /* dummy read */
239
240         bwi_mac_reset(mac, 1);
241
242         error = bwi_phy_attach(mac);
243         if (error)
244                 return error;
245
246         error = bwi_rf_attach(mac);
247         if (error)
248                 return error;
249
250         /* Link 11B/G PHY, unlink 11A PHY */
251         if (mac->mac_phy.phy_mode == IEEE80211_MODE_11A)
252                 bwi_mac_reset(mac, 0);
253         else
254                 bwi_mac_reset(mac, 1);
255
256         error = bwi_mac_test(mac);
257         if (error)
258                 return error;
259
260         error = bwi_mac_get_property(mac);
261         if (error)
262                 return error;
263
264         error = bwi_rf_map_txpower(mac);
265         if (error)
266                 return error;
267
268         bwi_rf_off(mac);
269         CSR_WRITE_2(mac->mac_sc, BWI_BBP_ATTEN, BWI_BBP_ATTEN_MAGIC);
270         bwi_regwin_disable(mac->mac_sc, &mac->mac_regwin, 0);
271
272         return 0;
273 }
274
275 int
276 bwi_mac_init(struct bwi_mac *mac)
277 {
278         struct bwi_softc *sc = mac->mac_sc;
279         int error, i;
280
281         /* Clear MAC/PHY/RF states */
282         bwi_mac_setup_tpctl(mac);
283         bwi_rf_clear_state(&mac->mac_rf);
284         bwi_phy_clear_state(&mac->mac_phy);
285
286         /* Enable MAC and linked it to PHY */
287         if (!bwi_regwin_is_enabled(sc, &mac->mac_regwin))
288                 bwi_mac_reset(mac, 1);
289
290         /* Initialize backplane */
291         error = bwi_bus_init(sc, mac);
292         if (error)
293                 return error;
294
295         /* XXX work around for hardware bugs? */
296         if (sc->sc_bus_regwin.rw_rev <= 5 &&
297             sc->sc_bus_regwin.rw_type != BWI_REGWIN_T_BUSPCIE) {
298                 CSR_SETBITS_4(sc, BWI_CONF_LO,
299                 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
300                 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
301         }
302
303         /* Calibrate PHY */
304         error = bwi_phy_calibrate(mac);
305         if (error) {
306                 if_printf(&sc->sc_ic.ic_if, "PHY calibrate failed\n");
307                 return error;
308         }
309
310         /* Prepare to initialize firmware */
311         CSR_WRITE_4(sc, BWI_MAC_STATUS,
312                     BWI_MAC_STATUS_UCODE_JUMP0 |
313                     BWI_MAC_STATUS_IHREN);
314
315         /*
316          * Load and initialize firmwares
317          */
318         error = bwi_mac_fw_alloc(mac);
319         if (error)
320                 return error;
321
322         error = bwi_mac_fw_load(mac);
323         if (error)
324                 return error;
325
326         error = bwi_mac_gpio_init(mac);
327         if (error)
328                 return error;
329
330         error = bwi_mac_fw_init(mac);
331         if (error)
332                 return error;
333
334         /*
335          * Turn on RF
336          */
337         bwi_rf_on(mac);
338
339         /* TODO: LED, hardware rf enabled is only related to LED setting */
340
341         /*
342          * Initialize PHY
343          */
344         CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0);
345         bwi_phy_init(mac);
346
347         /* TODO: interference mitigation */
348
349         /*
350          * Setup antenna mode
351          */
352         bwi_rf_set_ant_mode(mac, mac->mac_rf.rf_ant_mode);
353
354         /*
355          * Initialize operation mode (RX configuration)
356          */
357         bwi_mac_opmode_init(mac);
358
359         /* XXX what's these */
360         if (mac->mac_rev < 3) {
361                 CSR_WRITE_2(sc, 0x60e, 0);
362                 CSR_WRITE_2(sc, 0x610, 0x8000);
363                 CSR_WRITE_2(sc, 0x604, 0);
364                 CSR_WRITE_2(sc, 0x606, 0x200);
365         } else {
366                 CSR_WRITE_4(sc, 0x188, 0x80000000);
367                 CSR_WRITE_4(sc, 0x18c, 0x2000000);
368         }
369
370         /*
371          * Initialize TX/RX interrupts' mask
372          */
373         CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_INTR_TIMER1);
374         for (i = 0; i < BWI_TXRX_NRING; ++i) {
375                 uint32_t intrs;
376
377                 if (BWI_TXRX_IS_RX(i))
378                         intrs = BWI_TXRX_RX_INTRS;
379                 else
380                         intrs = BWI_TXRX_TX_INTRS;
381                 CSR_WRITE_4(sc, BWI_TXRX_INTR_MASK(i), intrs);
382         }
383
384         /* XXX what's this */
385         CSR_SETBITS_4(sc, BWI_STATE_LO, 0x100000);
386
387         /* Setup MAC power up delay */
388         CSR_WRITE_2(sc, BWI_MAC_POWERUP_DELAY, sc->sc_pwron_delay);
389
390         /* Set MAC regwin revision */
391         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_MACREV, mac->mac_rev);
392
393         /*
394          * Initialize host flags
395          */
396         bwi_mac_hostflags_init(mac);
397
398         /*
399          * Initialize BSS parameters
400          */
401         bwi_mac_bss_param_init(mac);
402
403         /*
404          * Initialize TX rings
405          */
406         for (i = 0; i < BWI_TX_NRING; ++i) {
407                 error = sc->sc_init_tx_ring(sc, i);
408                 if (error) {
409                         if_printf(&sc->sc_ic.ic_if,
410                                   "can't initialize %dth TX ring\n", i);
411                         return error;
412                 }
413         }
414
415         /*
416          * Initialize RX ring
417          */
418         error = sc->sc_init_rx_ring(sc);
419         if (error) {
420                 if_printf(&sc->sc_ic.ic_if, "can't initialize RX ring\n");
421                 return error;
422         }
423
424         /*
425          * Initialize TX stats if the current MAC uses that
426          */
427         if (mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) {
428                 error = sc->sc_init_txstats(sc);
429                 if (error) {
430                         if_printf(&sc->sc_ic.ic_if,
431                                   "can't initialize TX stats ring\n");
432                         return error;
433                 }
434         }
435
436         /* XXX what's these */
437         CSR_WRITE_2(sc, 0x612, 0x50);   /* Force Pre-TBTT to 80? */
438         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, 0x416, 0x50);
439         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, 0x414, 0x1f4);
440
441         mac->mac_flags |= BWI_MAC_F_INITED;
442         return 0;
443 }
444
445 void
446 bwi_mac_reset(struct bwi_mac *mac, int link_phy)
447 {
448         struct bwi_softc *sc = mac->mac_sc;
449         uint32_t flags, state_lo, status;
450
451         flags = BWI_STATE_LO_FLAG_PHYRST | BWI_STATE_LO_FLAG_PHYCLKEN;
452         if (link_phy)
453                 flags |= BWI_STATE_LO_FLAG_PHYLNK;
454         bwi_regwin_enable(sc, &mac->mac_regwin, flags);
455         DELAY(2000);
456
457         state_lo = CSR_READ_4(sc, BWI_STATE_LO);
458         state_lo |= BWI_STATE_LO_GATED_CLOCK;
459         state_lo &= ~__SHIFTIN(BWI_STATE_LO_FLAG_PHYRST,
460                                BWI_STATE_LO_FLAGS_MASK);
461         CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
462         /* Flush pending bus write */
463         CSR_READ_4(sc, BWI_STATE_LO);
464         DELAY(1000);
465
466         state_lo &= ~BWI_STATE_LO_GATED_CLOCK;
467         CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
468         /* Flush pending bus write */
469         CSR_READ_4(sc, BWI_STATE_LO);
470         DELAY(1000);
471
472         CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0);
473
474         status = CSR_READ_4(sc, BWI_MAC_STATUS);
475         status |= BWI_MAC_STATUS_IHREN;
476         if (link_phy)
477                 status |= BWI_MAC_STATUS_PHYLNK;
478         else
479                 status &= ~BWI_MAC_STATUS_PHYLNK;
480         CSR_WRITE_4(sc, BWI_MAC_STATUS, status);
481
482         if (link_phy) {
483                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH | BWI_DBG_INIT,
484                         "%s\n", "PHY is linked");
485                 mac->mac_phy.phy_flags |= BWI_PHY_F_LINKED;
486         } else {
487                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH | BWI_DBG_INIT,
488                         "%s\n", "PHY is unlinked");
489                 mac->mac_phy.phy_flags &= ~BWI_PHY_F_LINKED;
490         }
491 }
492
493 void
494 bwi_mac_set_tpctl_11bg(struct bwi_mac *mac, const struct bwi_tpctl *new_tpctl)
495 {
496         struct bwi_rf *rf = &mac->mac_rf;
497         struct bwi_tpctl *tpctl = &mac->mac_tpctl;
498
499         if (new_tpctl != NULL) {
500                 KKASSERT(new_tpctl->bbp_atten <= BWI_BBP_ATTEN_MAX);
501                 KKASSERT(new_tpctl->rf_atten <=
502                          (rf->rf_rev < 6 ? BWI_RF_ATTEN_MAX0
503                                          : BWI_RF_ATTEN_MAX1));
504                 KKASSERT(new_tpctl->tp_ctrl1 <= BWI_TPCTL1_MAX);
505
506                 tpctl->bbp_atten = new_tpctl->bbp_atten;
507                 tpctl->rf_atten = new_tpctl->rf_atten;
508                 tpctl->tp_ctrl1 = new_tpctl->tp_ctrl1;
509         }
510
511         /* Set BBP attenuation */
512         bwi_phy_set_bbp_atten(mac, tpctl->bbp_atten);
513
514         /* Set RF attenuation */
515         RF_WRITE(mac, BWI_RFR_ATTEN, tpctl->rf_atten);
516         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_RF_ATTEN,
517                      tpctl->rf_atten);
518
519         /* Set TX power */
520         if (rf->rf_type == BWI_RF_T_BCM2050) {
521                 RF_FILT_SETBITS(mac, BWI_RFR_TXPWR, ~BWI_RFR_TXPWR1_MASK,
522                         __SHIFTIN(tpctl->tp_ctrl1, BWI_RFR_TXPWR1_MASK));
523         }
524
525         /* Adjust RF Local Oscillator */
526         if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
527                 bwi_rf_lo_adjust(mac, tpctl);
528 }
529
530 static int
531 bwi_mac_test(struct bwi_mac *mac)
532 {
533         struct bwi_softc *sc = mac->mac_sc;
534         uint32_t orig_val, val;
535
536 #define TEST_VAL1       0xaa5555aa
537 #define TEST_VAL2       0x55aaaa55
538
539         /* Save it for later restoring */
540         orig_val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
541
542         /* Test 1 */
543         MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, TEST_VAL1);
544         val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
545         if (val != TEST_VAL1) {
546                 device_printf(sc->sc_dev, "TEST1 failed\n");
547                 return ENXIO;
548         }
549
550         /* Test 2 */
551         MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, TEST_VAL2);
552         val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
553         if (val != TEST_VAL2) {
554                 device_printf(sc->sc_dev, "TEST2 failed\n");
555                 return ENXIO;
556         }
557
558         /* Restore to the original value */
559         MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, orig_val);
560
561         val = CSR_READ_4(sc, BWI_MAC_STATUS);
562         if ((val & ~BWI_MAC_STATUS_PHYLNK) != BWI_MAC_STATUS_IHREN) {
563                 device_printf(sc->sc_dev, "%s failed, MAC status 0x%08x\n",
564                               __func__, val);
565                 return ENXIO;
566         }
567
568         val = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
569         if (val != 0) {
570                 device_printf(sc->sc_dev, "%s failed, intr status %08x\n",
571                               __func__, val);
572                 return ENXIO;
573         }
574
575 #undef TEST_VAL2
576 #undef TEST_VAL1
577
578         return 0;
579 }
580
581 static void
582 bwi_mac_setup_tpctl(struct bwi_mac *mac)
583 {
584         struct bwi_softc *sc = mac->mac_sc;
585         struct bwi_rf *rf = &mac->mac_rf;
586         struct bwi_phy *phy = &mac->mac_phy;
587         struct bwi_tpctl *tpctl = &mac->mac_tpctl;
588
589         /* Calc BBP attenuation */
590         if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev < 6)
591                 tpctl->bbp_atten = 0;
592         else
593                 tpctl->bbp_atten = 2;
594
595         /* Calc TX power CTRL1?? */
596         tpctl->tp_ctrl1 = 0;
597         if (rf->rf_type == BWI_RF_T_BCM2050) {
598                 if (rf->rf_rev == 1)
599                         tpctl->tp_ctrl1 = 3;
600                 else if (rf->rf_rev < 6)
601                         tpctl->tp_ctrl1 = 2;
602                 else if (rf->rf_rev == 8)
603                         tpctl->tp_ctrl1 = 1;
604         }
605
606         /* Empty TX power CTRL2?? */
607         tpctl->tp_ctrl2 = 0xffff;
608
609         /*
610          * Calc RF attenuation
611          */
612         if (phy->phy_mode == IEEE80211_MODE_11A) {
613                 tpctl->rf_atten = 0x60;
614                 goto back;
615         }
616
617         if (BWI_IS_BRCM_BCM4309G(sc) && sc->sc_pci_revid < 0x51) {
618                 tpctl->rf_atten = sc->sc_pci_revid < 0x43 ? 2 : 3;
619                 goto back;
620         }
621
622         tpctl->rf_atten = 5;
623
624         if (rf->rf_type != BWI_RF_T_BCM2050) {
625                 if (rf->rf_type == BWI_RF_T_BCM2053 && rf->rf_rev == 1)
626                         tpctl->rf_atten = 6;
627                 goto back;
628         }
629
630         /*
631          * NB: If we reaches here and the card is BRCM_BCM4309G,
632          *     then the card's PCI revision must >= 0x51
633          */
634
635         /* BCM2050 RF */
636         switch (rf->rf_rev) {
637         case 1:
638                 if (phy->phy_mode == IEEE80211_MODE_11G) {
639                         if (BWI_IS_BRCM_BCM4309G(sc) || BWI_IS_BRCM_BU4306(sc))
640                                 tpctl->rf_atten = 3;
641                         else
642                                 tpctl->rf_atten = 1;
643                 } else {
644                         if (BWI_IS_BRCM_BCM4309G(sc))
645                                 tpctl->rf_atten = 7;
646                         else
647                                 tpctl->rf_atten = 6;
648                 }
649                 break;
650         case 2:
651                 if (phy->phy_mode == IEEE80211_MODE_11G) {
652                         /*
653                          * NOTE: Order of following conditions is critical
654                          */
655                         if (BWI_IS_BRCM_BCM4309G(sc))
656                                 tpctl->rf_atten = 3;
657                         else if (BWI_IS_BRCM_BU4306(sc))
658                                 tpctl->rf_atten = 5;
659                         else if (sc->sc_bbp_id == BWI_BBPID_BCM4320)
660                                 tpctl->rf_atten = 4;
661                         else
662                                 tpctl->rf_atten = 3;
663                 } else {
664                         tpctl->rf_atten = 6;
665                 }
666                 break;
667         case 4:
668         case 5:
669                 tpctl->rf_atten = 1;
670                 break;
671         case 8:
672                 tpctl->rf_atten = 0x1a;
673                 break;
674         }
675 back:
676         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_TXPOWER,
677                 "bbp atten: %u, rf atten: %u, ctrl1: %u, ctrl2: %u\n",
678                 tpctl->bbp_atten, tpctl->rf_atten,
679                 tpctl->tp_ctrl1, tpctl->tp_ctrl2);
680 }
681
682 void
683 bwi_mac_dummy_xmit(struct bwi_mac *mac)
684 {
685 #define PACKET_LEN      5
686         static const uint32_t   packet_11a[PACKET_LEN] =
687         { 0x000201cc, 0x00d40000, 0x00000000, 0x01000000, 0x00000000 };
688         static const uint32_t   packet_11bg[PACKET_LEN] =
689         { 0x000b846e, 0x00d40000, 0x00000000, 0x01000000, 0x00000000 };
690
691         struct bwi_softc *sc = mac->mac_sc;
692         struct bwi_rf *rf = &mac->mac_rf;
693         const uint32_t *packet;
694         uint16_t val_50c;
695         int wait_max, i;
696
697         if (mac->mac_phy.phy_mode == IEEE80211_MODE_11A) {
698                 wait_max = 30;
699                 packet = packet_11a;
700                 val_50c = 1;
701         } else {
702                 wait_max = 250;
703                 packet = packet_11bg;
704                 val_50c = 0;
705         }
706
707         for (i = 0; i < PACKET_LEN; ++i)
708                 TMPLT_WRITE_4(mac, i * 4, packet[i]);
709
710         CSR_READ_4(sc, BWI_MAC_STATUS); /* dummy read */
711
712         CSR_WRITE_2(sc, 0x568, 0);
713         CSR_WRITE_2(sc, 0x7c0, 0);
714         CSR_WRITE_2(sc, 0x50c, val_50c);
715         CSR_WRITE_2(sc, 0x508, 0);
716         CSR_WRITE_2(sc, 0x50a, 0);
717         CSR_WRITE_2(sc, 0x54c, 0);
718         CSR_WRITE_2(sc, 0x56a, 0x14);
719         CSR_WRITE_2(sc, 0x568, 0x826);
720         CSR_WRITE_2(sc, 0x500, 0);
721         CSR_WRITE_2(sc, 0x502, 0x30);
722
723         if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev <= 5)
724                 RF_WRITE(mac, 0x51, 0x17);
725
726         for (i = 0; i < wait_max; ++i) {
727                 if (CSR_READ_2(sc, 0x50e) & 0x80)
728                         break;
729                 DELAY(10);
730         }
731         for (i = 0; i < 10; ++i) {
732                 if (CSR_READ_2(sc, 0x50e) & 0x400)
733                         break;
734                 DELAY(10);
735         }
736         for (i = 0; i < 10; ++i) {
737                 if ((CSR_READ_2(sc, 0x690) & 0x100) == 0)
738                         break;
739                 DELAY(10);
740         }
741
742         if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev <= 5)
743                 RF_WRITE(mac, 0x51, 0x37);
744 #undef PACKET_LEN
745 }
746
747 void
748 bwi_mac_init_tpctl_11bg(struct bwi_mac *mac)
749 {
750         struct bwi_softc *sc = mac->mac_sc;
751         struct bwi_phy *phy = &mac->mac_phy;
752         struct bwi_rf *rf = &mac->mac_rf;
753         struct bwi_tpctl tpctl_orig;
754         int restore_tpctl = 0;
755
756         KKASSERT(phy->phy_mode != IEEE80211_MODE_11A);
757
758         if (BWI_IS_BRCM_BU4306(sc))
759                 return;
760
761         PHY_WRITE(mac, 0x28, 0x8018);
762         CSR_CLRBITS_2(sc, BWI_BBP_ATTEN, 0x20);
763
764         if (phy->phy_mode == IEEE80211_MODE_11G) {
765                 if ((phy->phy_flags & BWI_PHY_F_LINKED) == 0)
766                         return;
767                 PHY_WRITE(mac, 0x47a, 0xc111);
768         }
769         if (mac->mac_flags & BWI_MAC_F_TPCTL_INITED)
770                 return;
771
772         if (phy->phy_mode == IEEE80211_MODE_11B && phy->phy_rev >= 2 &&
773             rf->rf_type == BWI_RF_T_BCM2050) {
774                 RF_SETBITS(mac, 0x76, 0x84);
775         } else {
776                 struct bwi_tpctl tpctl;
777
778                 /* Backup original TX power control variables */
779                 bcopy(&mac->mac_tpctl, &tpctl_orig, sizeof(tpctl_orig));
780                 restore_tpctl = 1;
781
782                 bcopy(&mac->mac_tpctl, &tpctl, sizeof(tpctl));
783                 tpctl.bbp_atten = 11;
784                 tpctl.tp_ctrl1 = 0;
785 #ifdef notyet
786                 if (rf->rf_rev >= 6 && rf->rf_rev <= 8)
787                         tpctl.rf_atten = 31;
788                 else
789 #endif
790                         tpctl.rf_atten = 9;
791
792                 bwi_mac_set_tpctl_11bg(mac, &tpctl);
793         }
794
795         bwi_mac_dummy_xmit(mac);
796
797         mac->mac_flags |= BWI_MAC_F_TPCTL_INITED;
798         rf->rf_base_tssi = PHY_READ(mac, 0x29);
799         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_TXPOWER,
800                 "base tssi %d\n", rf->rf_base_tssi);
801
802         if (abs(rf->rf_base_tssi - rf->rf_idle_tssi) >= 20) {
803                 if_printf(&sc->sc_ic.ic_if, "base tssi measure failed\n");
804                 mac->mac_flags |= BWI_MAC_F_TPCTL_ERROR;
805         }
806
807         if (restore_tpctl)
808                 bwi_mac_set_tpctl_11bg(mac, &tpctl_orig);
809         else
810                 RF_CLRBITS(mac, 0x76, 0x84);
811
812         bwi_rf_clear_tssi(mac);
813 }
814
815 void
816 bwi_mac_detach(struct bwi_mac *mac)
817 {
818         bwi_mac_fw_free(mac);
819 }
820
821 static __inline int
822 bwi_fwimage_is_valid(struct bwi_softc *sc, const struct fw_image *fw,
823                      uint8_t fw_type)
824 {
825         const struct bwi_fwhdr *hdr;
826         struct ifnet *ifp = &sc->sc_ic.ic_if;
827
828         if (fw->fw_imglen < sizeof(*hdr)) {
829                 if_printf(ifp, "invalid firmware (%s): invalid size %zu\n",
830                           fw->fw_name, fw->fw_imglen);
831                 return 0;
832         }
833
834         hdr = (const struct bwi_fwhdr *)fw->fw_image;
835
836         if (fw_type != BWI_FW_T_IV) {
837                 /*
838                  * Don't verify IV's size, it has different meaning
839                  */
840                 if (be32toh(hdr->fw_size) != fw->fw_imglen - sizeof(*hdr)) {
841                         if_printf(ifp,
842                                   "invalid firmware (%s): size mismatch, "
843                                   "fw %u, real %zu\n",
844                                   fw->fw_name, be32toh(hdr->fw_size),
845                                   fw->fw_imglen - sizeof(*hdr));
846                         return 0;
847                 }
848         }
849
850         if (hdr->fw_type != fw_type) {
851                 if_printf(ifp, "invalid firmware (%s): type mismatch, "
852                           "fw \'%c\', target \'%c\'\n", fw->fw_name,
853                           hdr->fw_type, fw_type);
854                 return 0;
855         }
856
857         if (hdr->fw_gen != BWI_FW_GEN_1) {
858                 if_printf(ifp, "invalid firmware (%s): wrong generation, "
859                           "fw %d, target %d\n", fw->fw_name,
860                           hdr->fw_gen, BWI_FW_GEN_1);
861                 return 0;
862         }
863         return 1;
864 }
865
866 /*
867  * XXX Error cleanup
868  */
869 static int
870 bwi_mac_fw_alloc(struct bwi_mac *mac)
871 {
872         struct bwi_softc *sc = mac->mac_sc;
873         struct ifnet *ifp = &sc->sc_ic.ic_if;
874         struct fw_image *img;
875         char fwname[64];
876         int idx;
877
878         /*
879          * NB: serializer need to be released before loading firmware
880          *     image to avoid possible dead lock
881          */
882         ASSERT_SERIALIZED(ifp->if_serializer);
883
884         if (mac->mac_ucode == NULL) {
885                 ksnprintf(fwname, sizeof(fwname), BWI_FW_UCODE_PATH,
886                           sc->sc_fw_version,
887                           mac->mac_rev >= 5 ? 5 : mac->mac_rev);
888
889                 lwkt_serialize_exit(ifp->if_serializer);
890                 img = firmware_image_load(fwname, NULL);
891                 lwkt_serialize_enter(ifp->if_serializer);
892
893                 mac->mac_ucode = img;
894                 if (mac->mac_ucode == NULL) {
895                         if_printf(ifp, "request firmware %s failed\n", fwname);
896                         return ENOMEM;
897                 }
898
899                 if (!bwi_fwimage_is_valid(sc, mac->mac_ucode, BWI_FW_T_UCODE))
900                         return EINVAL;
901         }
902
903         if (mac->mac_pcm == NULL) {
904                 ksnprintf(fwname, sizeof(fwname), BWI_FW_PCM_PATH,
905                           sc->sc_fw_version,
906                           mac->mac_rev < 5 ? 4 : 5);
907
908                 lwkt_serialize_exit(ifp->if_serializer);
909                 img = firmware_image_load(fwname, NULL);
910                 lwkt_serialize_enter(ifp->if_serializer);
911
912                 mac->mac_pcm = img;
913                 if (mac->mac_pcm == NULL) {
914                         if_printf(ifp, "request firmware %s failed\n", fwname);
915                         return ENOMEM;
916                 }
917
918                 if (!bwi_fwimage_is_valid(sc, mac->mac_pcm, BWI_FW_T_PCM))
919                         return EINVAL;
920         }
921
922         if (mac->mac_iv == NULL) {
923                 /* TODO: 11A */
924                 if (mac->mac_rev == 2 || mac->mac_rev == 4) {
925                         idx = 2;
926                 } else if (mac->mac_rev >= 5 && mac->mac_rev <= 10) {
927                         idx = 5;
928                 } else {
929                         if_printf(ifp, "no suitable IV for MAC rev %d\n",
930                                   mac->mac_rev);
931                         return ENODEV;
932                 }
933
934                 ksnprintf(fwname, sizeof(fwname), BWI_FW_IV_PATH,
935                           sc->sc_fw_version, idx);
936
937                 lwkt_serialize_exit(ifp->if_serializer);
938                 img = firmware_image_load(fwname, NULL);
939                 lwkt_serialize_enter(ifp->if_serializer);
940
941                 mac->mac_iv = img;
942                 if (mac->mac_iv == NULL) {
943                         if_printf(ifp, "request firmware %s failed\n", fwname);
944                         return ENOMEM;
945                 }
946                 if (!bwi_fwimage_is_valid(sc, mac->mac_iv, BWI_FW_T_IV))
947                         return EINVAL;
948         }
949
950         if (mac->mac_iv_ext == NULL) {
951                 /* TODO: 11A */
952                 if (mac->mac_rev == 2 || mac->mac_rev == 4 ||
953                     mac->mac_rev >= 11) {
954                         /* No extended IV */
955                         goto back;
956                 } else if (mac->mac_rev >= 5 && mac->mac_rev <= 10) {
957                         idx = 5;
958                 } else {
959                         if_printf(ifp, "no suitible ExtIV for MAC rev %d\n",
960                                   mac->mac_rev);
961                         return ENODEV;
962                 }
963
964                 ksnprintf(fwname, sizeof(fwname), BWI_FW_IV_EXT_PATH,
965                           sc->sc_fw_version, idx);
966
967                 lwkt_serialize_exit(ifp->if_serializer);
968                 img = firmware_image_load(fwname, NULL);
969                 lwkt_serialize_enter(ifp->if_serializer);
970
971                 mac->mac_iv_ext = img;
972                 if (mac->mac_iv_ext == NULL) {
973                         if_printf(ifp, "request firmware %s failed\n", fwname);
974                         return ENOMEM;
975                 }
976                 if (!bwi_fwimage_is_valid(sc, mac->mac_iv_ext, BWI_FW_T_IV))
977                         return EINVAL;
978         }
979 back:
980         return 0;
981 }
982
983 static void
984 bwi_mac_fw_free(struct bwi_mac *mac)
985 {
986         if (mac->mac_ucode != NULL) {
987                 firmware_image_unload(mac->mac_ucode);
988                 mac->mac_ucode = NULL;
989         }
990
991         if (mac->mac_pcm != NULL) {
992                 firmware_image_unload(mac->mac_pcm);
993                 mac->mac_pcm = NULL;
994         }
995
996         if (mac->mac_iv != NULL) {
997                 firmware_image_unload(mac->mac_iv);
998                 mac->mac_iv = NULL;
999         }
1000
1001         if (mac->mac_iv_ext != NULL) {
1002                 firmware_image_unload(mac->mac_iv_ext);
1003                 mac->mac_iv_ext = NULL;
1004         }
1005 }
1006
1007 static int
1008 bwi_mac_fw_load(struct bwi_mac *mac)
1009 {
1010         struct bwi_softc *sc = mac->mac_sc;
1011         struct ifnet *ifp = &sc->sc_ic.ic_if;
1012         const uint32_t *fw;
1013         uint16_t fw_rev;
1014         int fw_len, i;
1015
1016         /*
1017          * Load ucode image
1018          */
1019         fw = (const uint32_t *)
1020              ((const uint8_t *)mac->mac_ucode->fw_image + BWI_FWHDR_SZ);
1021         fw_len = (mac->mac_ucode->fw_imglen - BWI_FWHDR_SZ) / sizeof(uint32_t);
1022
1023         CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1024                     BWI_MOBJ_CTRL_VAL(
1025                     BWI_FW_UCODE_MOBJ | BWI_WR_MOBJ_AUTOINC, 0));
1026         for (i = 0; i < fw_len; ++i) {
1027                 CSR_WRITE_4(sc, BWI_MOBJ_DATA, be32toh(fw[i]));
1028                 DELAY(10);
1029         }
1030
1031         /*
1032          * Load PCM image
1033          */
1034         fw = (const uint32_t *)
1035              ((const uint8_t *)mac->mac_pcm->fw_image + BWI_FWHDR_SZ);
1036         fw_len = (mac->mac_pcm->fw_imglen - BWI_FWHDR_SZ) / sizeof(uint32_t);
1037
1038         CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1039                     BWI_MOBJ_CTRL_VAL(BWI_FW_PCM_MOBJ, 0x01ea));
1040         CSR_WRITE_4(sc, BWI_MOBJ_DATA, 0x4000);
1041
1042         CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1043                     BWI_MOBJ_CTRL_VAL(BWI_FW_PCM_MOBJ, 0x01eb));
1044         for (i = 0; i < fw_len; ++i) {
1045                 CSR_WRITE_4(sc, BWI_MOBJ_DATA, be32toh(fw[i]));
1046                 DELAY(10);
1047         }
1048
1049         CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_ALL_INTRS);
1050         CSR_WRITE_4(sc, BWI_MAC_STATUS,
1051                     BWI_MAC_STATUS_UCODE_START |
1052                     BWI_MAC_STATUS_IHREN |
1053                     BWI_MAC_STATUS_INFRA);
1054
1055 #define NRETRY  200
1056
1057         for (i = 0; i < NRETRY; ++i) {
1058                 uint32_t intr_status;
1059
1060                 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1061                 if (intr_status == BWI_INTR_READY)
1062                         break;
1063                 DELAY(10);
1064         }
1065         if (i == NRETRY) {
1066                 if_printf(ifp, "firmware (ucode&pcm) loading timed out\n");
1067                 return ETIMEDOUT;
1068         }
1069
1070 #undef NRETRY
1071
1072         CSR_READ_4(sc, BWI_MAC_INTR_STATUS);    /* dummy read */
1073
1074         fw_rev = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_FWREV);
1075         if (fw_rev > BWI_FW_VERSION3_REVMAX) {
1076                 if_printf(ifp, "firmware version 4 is not supported yet\n");
1077                 return ENODEV;
1078         }
1079
1080         if_printf(ifp, "firmware rev 0x%04x, patch level 0x%04x\n", fw_rev,
1081                   MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_FWPATCHLV));
1082         return 0;
1083 }
1084
1085 static int
1086 bwi_mac_gpio_init(struct bwi_mac *mac)
1087 {
1088         struct bwi_softc *sc = mac->mac_sc;
1089         struct bwi_regwin *old, *gpio_rw;
1090         uint32_t filt, bits;
1091         int error;
1092
1093         CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_GPOSEL_MASK);
1094         /* TODO:LED */
1095
1096         CSR_SETBITS_2(sc, BWI_MAC_GPIO_MASK, 0xf);
1097
1098         filt = 0x1f;
1099         bits = 0xf;
1100         if (sc->sc_bbp_id == BWI_BBPID_BCM4301) {
1101                 filt |= 0x60;
1102                 bits |= 0x60;
1103         }
1104         if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9) {
1105                 CSR_SETBITS_2(sc, BWI_MAC_GPIO_MASK, 0x200);
1106                 filt |= 0x200;
1107                 bits |= 0x200;
1108         }
1109
1110         gpio_rw = BWI_GPIO_REGWIN(sc);
1111         error = bwi_regwin_switch(sc, gpio_rw, &old);
1112         if (error)
1113                 return error;
1114
1115         CSR_FILT_SETBITS_4(sc, BWI_GPIO_CTRL, filt, bits);
1116
1117         return bwi_regwin_switch(sc, old, NULL);
1118 }
1119
1120 static int
1121 bwi_mac_gpio_fini(struct bwi_mac *mac)
1122 {
1123         struct bwi_softc *sc = mac->mac_sc;
1124         struct bwi_regwin *old, *gpio_rw;
1125         int error;
1126
1127         gpio_rw = BWI_GPIO_REGWIN(sc);
1128         error = bwi_regwin_switch(sc, gpio_rw, &old);
1129         if (error)
1130                 return error;
1131
1132         CSR_WRITE_4(sc, BWI_GPIO_CTRL, 0);
1133
1134         return bwi_regwin_switch(sc, old, NULL);
1135 }
1136
1137 static int
1138 bwi_mac_fw_load_iv(struct bwi_mac *mac, const struct fw_image *fw)
1139 {
1140         struct bwi_softc *sc = mac->mac_sc;
1141         struct ifnet *ifp = &sc->sc_ic.ic_if;
1142         const struct bwi_fwhdr *hdr;
1143         const struct bwi_fw_iv *iv;
1144         int n, i, iv_img_size;
1145
1146         /* Get the number of IVs in the IV image */
1147         hdr = (const struct bwi_fwhdr *)fw->fw_image;
1148         n = be32toh(hdr->fw_iv_cnt);
1149         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_FIRMWARE,
1150                 "IV count %d\n", n);
1151
1152         /* Calculate the IV image size, for later sanity check */
1153         iv_img_size = fw->fw_imglen - sizeof(*hdr);
1154
1155         /* Locate the first IV */
1156         iv = (const struct bwi_fw_iv *)
1157              ((const uint8_t *)fw->fw_image + sizeof(*hdr));
1158
1159         for (i = 0; i < n; ++i) {
1160                 uint16_t iv_ofs, ofs;
1161                 int sz = 0;
1162
1163                 if (iv_img_size < sizeof(iv->iv_ofs)) {
1164                         if_printf(ifp, "invalid IV image, ofs\n");
1165                         return EINVAL;
1166                 }
1167                 iv_img_size -= sizeof(iv->iv_ofs);
1168                 sz += sizeof(iv->iv_ofs);
1169
1170                 iv_ofs = be16toh(iv->iv_ofs);
1171
1172                 ofs = __SHIFTOUT(iv_ofs, BWI_FW_IV_OFS_MASK);
1173                 if (ofs >= 0x1000) {
1174                         if_printf(ifp, "invalid ofs (0x%04x) "
1175                                   "for %dth iv\n", ofs, i);
1176                         return EINVAL;
1177                 }
1178
1179                 if (iv_ofs & BWI_FW_IV_IS_32BIT) {
1180                         uint32_t val32;
1181
1182                         if (iv_img_size < sizeof(iv->iv_val.val32)) {
1183                                 if_printf(ifp, "invalid IV image, val32\n");
1184                                 return EINVAL;
1185                         }
1186                         iv_img_size -= sizeof(iv->iv_val.val32);
1187                         sz += sizeof(iv->iv_val.val32);
1188
1189                         val32 = be32toh(iv->iv_val.val32);
1190                         CSR_WRITE_4(sc, ofs, val32);
1191                 } else {
1192                         uint16_t val16;
1193
1194                         if (iv_img_size < sizeof(iv->iv_val.val16)) {
1195                                 if_printf(ifp, "invalid IV image, val16\n");
1196                                 return EINVAL;
1197                         }
1198                         iv_img_size -= sizeof(iv->iv_val.val16);
1199                         sz += sizeof(iv->iv_val.val16);
1200
1201                         val16 = be16toh(iv->iv_val.val16);
1202                         CSR_WRITE_2(sc, ofs, val16);
1203                 }
1204
1205                 iv = (const struct bwi_fw_iv *)((const uint8_t *)iv + sz);
1206         }
1207
1208         if (iv_img_size != 0) {
1209                 if_printf(ifp, "invalid IV image, size left %d\n", iv_img_size);
1210                 return EINVAL;
1211         }
1212         return 0;
1213 }
1214
1215 static int
1216 bwi_mac_fw_init(struct bwi_mac *mac)
1217 {
1218         struct ifnet *ifp = &mac->mac_sc->sc_ic.ic_if;
1219         int error;
1220
1221         error = bwi_mac_fw_load_iv(mac, mac->mac_iv);
1222         if (error) {
1223                 if_printf(ifp, "load IV failed\n");
1224                 return error;
1225         }
1226
1227         if (mac->mac_iv_ext != NULL) {
1228                 error = bwi_mac_fw_load_iv(mac, mac->mac_iv_ext);
1229                 if (error)
1230                         if_printf(ifp, "load ExtIV failed\n");
1231         }
1232         return error;
1233 }
1234
1235 static void
1236 bwi_mac_opmode_init(struct bwi_mac *mac)
1237 {
1238         struct bwi_softc *sc = mac->mac_sc;
1239         struct ieee80211com *ic = &sc->sc_ic;
1240         uint32_t mac_status;
1241         uint16_t pre_tbtt;
1242
1243         CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_INFRA);
1244         CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_INFRA);
1245         CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PASS_BCN);
1246
1247         /* Set probe resp timeout to infinite */
1248         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_PROBE_RESP_TO, 0);
1249
1250         /*
1251          * TODO: factor out following part
1252          */
1253
1254         mac_status = CSR_READ_4(sc, BWI_MAC_STATUS);
1255         mac_status &= ~(BWI_MAC_STATUS_OPMODE_HOSTAP |
1256                         BWI_MAC_STATUS_PASS_CTL |
1257                         BWI_MAC_STATUS_PASS_BADPLCP |
1258                         BWI_MAC_STATUS_PASS_BADFCS |
1259                         BWI_MAC_STATUS_PROMISC);
1260         mac_status |= BWI_MAC_STATUS_INFRA;
1261
1262         /* Always turn on PROMISC on old hardware */
1263         if (mac->mac_rev < 5)
1264                 mac_status |= BWI_MAC_STATUS_PROMISC;
1265
1266         switch (ic->ic_opmode) {
1267         case IEEE80211_M_IBSS:
1268                 mac_status &= ~BWI_MAC_STATUS_INFRA;
1269                 break;
1270         case IEEE80211_M_HOSTAP:
1271                 mac_status |= BWI_MAC_STATUS_OPMODE_HOSTAP;
1272                 break;
1273         case IEEE80211_M_MONITOR:
1274 #if 0
1275                 /* Do you want data from your microwave oven? */
1276                 mac_status |= BWI_MAC_STATUS_PASS_CTL |
1277                               BWI_MAC_STATUS_PASS_BADPLCP |
1278                               BWI_MAC_STATUS_PASS_BADFCS;
1279 #else
1280                 mac_status |= BWI_MAC_STATUS_PASS_CTL;
1281 #endif
1282                 /* Promisc? */
1283                 break;
1284         default:
1285                 break;
1286         }
1287
1288         if (ic->ic_if.if_flags & IFF_PROMISC)
1289                 mac_status |= BWI_MAC_STATUS_PROMISC;
1290
1291         CSR_WRITE_4(sc, BWI_MAC_STATUS, mac_status);
1292
1293         if (ic->ic_opmode != IEEE80211_M_IBSS &&
1294             ic->ic_opmode != IEEE80211_M_HOSTAP) {
1295                 if (sc->sc_bbp_id == BWI_BBPID_BCM4306 && sc->sc_bbp_rev == 3)
1296                         pre_tbtt = 100;
1297                 else
1298                         pre_tbtt = 50;
1299         } else {
1300                 pre_tbtt = 2;
1301         }
1302         CSR_WRITE_2(sc, BWI_MAC_PRE_TBTT, pre_tbtt);
1303 }
1304
1305 static void
1306 bwi_mac_hostflags_init(struct bwi_mac *mac)
1307 {
1308         struct bwi_softc *sc = mac->mac_sc;
1309         struct bwi_phy *phy = &mac->mac_phy;
1310         struct bwi_rf *rf = &mac->mac_rf;
1311         uint64_t host_flags;
1312
1313         if (phy->phy_mode == IEEE80211_MODE_11A)
1314                 return;
1315
1316         host_flags = HFLAGS_READ(mac);
1317         host_flags |= BWI_HFLAG_SYM_WA;
1318
1319         if (phy->phy_mode == IEEE80211_MODE_11G) {
1320                 if (phy->phy_rev == 1)
1321                         host_flags |= BWI_HFLAG_GDC_WA;
1322                 if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9)
1323                         host_flags |= BWI_HFLAG_OFDM_PA;
1324         } else if (phy->phy_mode == IEEE80211_MODE_11B) {
1325                 if (phy->phy_rev >= 2 && rf->rf_type == BWI_RF_T_BCM2050)
1326                         host_flags &= ~BWI_HFLAG_GDC_WA;
1327         } else {
1328                 panic("unknown PHY mode %u\n", phy->phy_mode);
1329         }
1330
1331         HFLAGS_WRITE(mac, host_flags);
1332 }
1333
1334 static void
1335 bwi_mac_bss_param_init(struct bwi_mac *mac)
1336 {
1337         struct bwi_softc *sc = mac->mac_sc;
1338         struct bwi_phy *phy = &mac->mac_phy;
1339         struct bwi_retry_lim lim;
1340         uint16_t cw_min;
1341
1342         /*
1343          * Set short/long retry limits
1344          */
1345         bzero(&lim, sizeof(lim));
1346         lim.shretry = BWI_SHRETRY;
1347         lim.shretry_fb = BWI_SHRETRY_FB;
1348         lim.lgretry = BWI_LGRETRY;
1349         lim.lgretry_fb = BWI_LGRETRY_FB;
1350         bwi_mac_set_retry_lim(mac, &lim);
1351
1352         /*
1353          * Implicitly prevent firmware from sending probe response
1354          * by setting its "probe response timeout" to 1us.
1355          */
1356         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_PROBE_RESP_TO, 1);
1357
1358         /*
1359          * XXX MAC level acknowledge and CW min/max should depend
1360          * on the char rateset of the IBSS/BSS to join.
1361          */
1362
1363         /*
1364          * Set MAC level acknowledge rates
1365          */
1366         bwi_mac_set_ackrates(mac, &sc->sc_ic.ic_sup_rates[phy->phy_mode]);
1367
1368         /*
1369          * Set CW min
1370          */
1371         if (phy->phy_mode == IEEE80211_MODE_11B)
1372                 cw_min = IEEE80211_CW_MIN_0;
1373         else
1374                 cw_min = IEEE80211_CW_MIN_1;
1375         MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_CWMIN, cw_min);
1376
1377         /*
1378          * Set CW max
1379          */
1380         MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_CWMAX,
1381                      IEEE80211_CW_MAX);
1382 }
1383
1384 static void
1385 bwi_mac_set_retry_lim(struct bwi_mac *mac, const struct bwi_retry_lim *lim)
1386 {
1387         /* Short/Long retry limit */
1388         MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_SHRETRY,
1389                      lim->shretry);
1390         MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_LGRETRY,
1391                      lim->lgretry);
1392
1393         /* Short/Long retry fallback limit */
1394         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_SHRETRY_FB,
1395                      lim->shretry_fb);
1396         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_LGRETEY_FB,
1397                      lim->lgretry_fb);
1398 }
1399
1400 static void
1401 bwi_mac_set_ackrates(struct bwi_mac *mac, const struct ieee80211_rateset *rs)
1402 {
1403         int i;
1404
1405         /* XXX not standard conforming */
1406         for (i = 0; i < rs->rs_nrates; ++i) {
1407                 enum ieee80211_modtype modtype;
1408                 uint16_t ofs;
1409
1410                 modtype = ieee80211_rate2modtype(rs->rs_rates[i]);
1411                 switch (modtype) {
1412                 case IEEE80211_MODTYPE_DS:
1413                         ofs = 0x4c0;
1414                         break;
1415                 case IEEE80211_MODTYPE_OFDM:
1416                         ofs = 0x480;
1417                         break;
1418                 default:
1419                         panic("unsupported modtype %u\n", modtype);
1420                 }
1421                 ofs += (bwi_rate2plcp(rs->rs_rates[i]) & 0xf) * 2;
1422
1423                 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, ofs + 0x20,
1424                              MOBJ_READ_2(mac, BWI_COMM_MOBJ, ofs));
1425         }
1426 }
1427
1428 int
1429 bwi_mac_start(struct bwi_mac *mac)
1430 {
1431         struct bwi_softc *sc = mac->mac_sc;
1432
1433         CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_ENABLE);
1434         CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_INTR_READY);
1435
1436         /* Flush pending bus writes */
1437         CSR_READ_4(sc, BWI_MAC_STATUS);
1438         CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1439
1440         return bwi_mac_config_ps(mac);
1441 }
1442
1443 int
1444 bwi_mac_stop(struct bwi_mac *mac)
1445 {
1446         struct bwi_softc *sc = mac->mac_sc;
1447         int error, i;
1448
1449         error = bwi_mac_config_ps(mac);
1450         if (error)
1451                 return error;
1452
1453         CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_ENABLE);
1454
1455         /* Flush pending bus write */
1456         CSR_READ_4(sc, BWI_MAC_STATUS);
1457
1458 #define NRETRY  10000
1459         for (i = 0; i < NRETRY; ++i) {
1460                 if (CSR_READ_4(sc, BWI_MAC_INTR_STATUS) & BWI_INTR_READY)
1461                         break;
1462                 DELAY(1);
1463         }
1464         if (i == NRETRY) {
1465                 if_printf(&sc->sc_ic.ic_if, "can't stop MAC\n");
1466                 return ETIMEDOUT;
1467         }
1468 #undef NRETRY
1469
1470         return 0;
1471 }
1472
1473 int
1474 bwi_mac_config_ps(struct bwi_mac *mac)
1475 {
1476         struct bwi_softc *sc = mac->mac_sc;
1477         uint32_t status;
1478
1479         status = CSR_READ_4(sc, BWI_MAC_STATUS);
1480
1481         status &= ~BWI_MAC_STATUS_HW_PS;
1482         status |= BWI_MAC_STATUS_WAKEUP;
1483         CSR_WRITE_4(sc, BWI_MAC_STATUS, status);
1484
1485         /* Flush pending bus write */
1486         CSR_READ_4(sc, BWI_MAC_STATUS);
1487
1488         if (mac->mac_rev >= 5) {
1489                 int i;
1490
1491 #define NRETRY  100
1492                 for (i = 0; i < NRETRY; ++i) {
1493                         if (MOBJ_READ_2(mac, BWI_COMM_MOBJ,
1494                             BWI_COMM_MOBJ_UCODE_STATE) != BWI_UCODE_STATE_PS)
1495                                 break;
1496                         DELAY(10);
1497                 }
1498                 if (i == NRETRY) {
1499                         if_printf(&sc->sc_ic.ic_if, "config PS failed\n");
1500                         return ETIMEDOUT;
1501                 }
1502 #undef NRETRY
1503         }
1504         return 0;
1505 }
1506
1507 void
1508 bwi_mac_reset_hwkeys(struct bwi_mac *mac)
1509 {
1510         /* TODO: firmware crypto */
1511         MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_KEYTABLE_OFS);
1512 }
1513
1514 void
1515 bwi_mac_shutdown(struct bwi_mac *mac)
1516 {
1517         struct bwi_softc *sc = mac->mac_sc;
1518         int i;
1519
1520         if (mac->mac_flags & BWI_MAC_F_HAS_TXSTATS)
1521                 sc->sc_free_txstats(sc);
1522
1523         sc->sc_free_rx_ring(sc);
1524
1525         for (i = 0; i < BWI_TX_NRING; ++i)
1526                 sc->sc_free_tx_ring(sc, i);
1527
1528         bwi_rf_off(mac);
1529
1530         /* TODO:LED */
1531
1532         bwi_mac_gpio_fini(mac);
1533
1534         bwi_rf_off(mac); /* XXX again */
1535         CSR_WRITE_2(sc, BWI_BBP_ATTEN, BWI_BBP_ATTEN_MAGIC);
1536         bwi_regwin_disable(sc, &mac->mac_regwin, 0);
1537
1538         mac->mac_flags &= ~BWI_MAC_F_INITED;
1539 }
1540
1541 static int
1542 bwi_mac_get_property(struct bwi_mac *mac)
1543 {
1544         struct bwi_softc *sc = mac->mac_sc;
1545         enum bwi_bus_space old_bus_space;
1546         uint32_t val;
1547
1548         /*
1549          * Byte swap
1550          */
1551         val = CSR_READ_4(sc, BWI_MAC_STATUS);
1552         if (val & BWI_MAC_STATUS_BSWAP) {
1553                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1554                         "need byte swap");
1555                 mac->mac_flags |= BWI_MAC_F_BSWAP;
1556         }
1557
1558         /*
1559          * DMA address space
1560          */
1561         old_bus_space = sc->sc_bus_space;
1562
1563         val = CSR_READ_4(sc, BWI_STATE_HI);
1564         if (__SHIFTOUT(val, BWI_STATE_HI_FLAGS_MASK) &
1565             BWI_STATE_HI_FLAG_64BIT) {
1566                 /* 64bit address */
1567                 sc->sc_bus_space = BWI_BUS_SPACE_64BIT;
1568                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1569                         "64bit bus space");
1570         } else {
1571                 uint32_t txrx_reg = BWI_TXRX_CTRL_BASE + BWI_TX32_CTRL;
1572
1573                 CSR_WRITE_4(sc, txrx_reg, BWI_TXRX32_CTRL_ADDRHI_MASK);
1574                 if (CSR_READ_4(sc, txrx_reg) & BWI_TXRX32_CTRL_ADDRHI_MASK) {
1575                         /* 32bit address */
1576                         sc->sc_bus_space = BWI_BUS_SPACE_32BIT;
1577                         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1578                                 "32bit bus space");
1579                 } else {
1580                         /* 30bit address */
1581                         sc->sc_bus_space = BWI_BUS_SPACE_30BIT;
1582                         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1583                                 "30bit bus space");
1584                 }
1585         }
1586
1587         if (old_bus_space != 0 && old_bus_space != sc->sc_bus_space) {
1588                 device_printf(sc->sc_dev, "MACs bus space mismatch!\n");
1589                 return ENXIO;
1590         }
1591         return 0;
1592 }
1593
1594 void
1595 bwi_mac_updateslot(struct bwi_mac *mac, int shslot)
1596 {
1597         uint16_t slot_time;
1598
1599         if (mac->mac_phy.phy_mode == IEEE80211_MODE_11B)
1600                 return;
1601
1602         if (shslot)
1603                 slot_time = IEEE80211_DUR_SHSLOT;
1604         else
1605                 slot_time = IEEE80211_DUR_SLOT;
1606
1607         CSR_WRITE_2(mac->mac_sc, BWI_MAC_SLOTTIME,
1608                     slot_time + BWI_MAC_SLOTTIME_ADJUST);
1609         MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_SLOTTIME, slot_time);
1610 }
1611
1612 int
1613 bwi_mac_attach(struct bwi_softc *sc, int id, uint8_t rev)
1614 {
1615         struct bwi_mac *mac;
1616         int i;
1617
1618         KKASSERT(sc->sc_nmac <= BWI_MAC_MAX && sc->sc_nmac >= 0);
1619
1620         if (sc->sc_nmac == BWI_MAC_MAX) {
1621                 device_printf(sc->sc_dev, "too many MACs\n");
1622                 return 0;
1623         }
1624
1625         /*
1626          * More than one MAC is only supported by BCM4309
1627          */
1628         if (sc->sc_nmac != 0 &&
1629             pci_get_device(sc->sc_dev) != PCI_PRODUCT_BROADCOM_BCM4309) {
1630                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1631                         "ignore second MAC");
1632                 return 0;
1633         }
1634
1635         mac = &sc->sc_mac[sc->sc_nmac];
1636
1637         /* XXX will this happen? */
1638         if (BWI_REGWIN_EXIST(&mac->mac_regwin)) {
1639                 device_printf(sc->sc_dev, "%dth MAC already attached\n",
1640                               sc->sc_nmac);
1641                 return 0;
1642         }
1643
1644         /*
1645          * Test whether the revision of this MAC is supported
1646          */
1647 #define N(arr)  (int)(sizeof(arr) / sizeof(arr[0]))
1648         for (i = 0; i < N(bwi_sup_macrev); ++i) {
1649                 if (bwi_sup_macrev[i] == rev)
1650                         break;
1651         }
1652         if (i == N(bwi_sup_macrev)) {
1653                 device_printf(sc->sc_dev, "MAC rev %u is "
1654                               "not supported\n", rev);
1655                 return ENXIO;
1656         }
1657 #undef N
1658
1659         BWI_CREATE_MAC(mac, sc, id, rev);
1660         sc->sc_nmac++;
1661
1662         if (mac->mac_rev < 5) {
1663                 mac->mac_flags |= BWI_MAC_F_HAS_TXSTATS;
1664                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1665                         "has TX stats");
1666         } else {
1667                 mac->mac_flags |= BWI_MAC_F_PHYE_RESET;
1668         }
1669
1670         device_printf(sc->sc_dev, "MAC: rev %u\n", rev);
1671         return 0;
1672 }
1673
1674 static __inline void
1675 bwi_mac_balance_atten(int *bbp_atten0, int *rf_atten0)
1676 {
1677         int bbp_atten, rf_atten, rf_atten_lim = -1;
1678
1679         bbp_atten = *bbp_atten0;
1680         rf_atten = *rf_atten0;
1681
1682         /*
1683          * RF attenuation affects TX power BWI_RF_ATTEN_FACTOR times
1684          * as much as BBP attenuation, so we try our best to keep RF
1685          * attenuation within range.  BBP attenuation will be clamped
1686          * later if it is out of range during balancing.
1687          *
1688          * BWI_RF_ATTEN_MAX0 is used as RF attenuation upper limit.
1689          */
1690
1691         /*
1692          * Use BBP attenuation to balance RF attenuation
1693          */
1694         if (rf_atten < 0)
1695                 rf_atten_lim = 0;
1696         else if (rf_atten > BWI_RF_ATTEN_MAX0)
1697                 rf_atten_lim = BWI_RF_ATTEN_MAX0;
1698
1699         if (rf_atten_lim >= 0) {
1700                 bbp_atten += (BWI_RF_ATTEN_FACTOR * (rf_atten - rf_atten_lim));
1701                 rf_atten = rf_atten_lim;
1702         }
1703
1704         /*
1705          * If possible, use RF attenuation to balance BBP attenuation
1706          * NOTE: RF attenuation is still kept within range.
1707          */
1708         while (rf_atten < BWI_RF_ATTEN_MAX0 && bbp_atten > BWI_BBP_ATTEN_MAX) {
1709                 bbp_atten -= BWI_RF_ATTEN_FACTOR;
1710                 ++rf_atten;
1711         }
1712         while (rf_atten > 0 && bbp_atten < 0) {
1713                 bbp_atten += BWI_RF_ATTEN_FACTOR;
1714                 --rf_atten;
1715         }
1716
1717         /* RF attenuation MUST be within range */
1718         KKASSERT(rf_atten >= 0 && rf_atten <= BWI_RF_ATTEN_MAX0);
1719
1720         /*
1721          * Clamp BBP attenuation
1722          */
1723         if (bbp_atten < 0)
1724                 bbp_atten = 0;
1725         else if (bbp_atten > BWI_BBP_ATTEN_MAX)
1726                 bbp_atten = BWI_BBP_ATTEN_MAX;
1727
1728         *rf_atten0 = rf_atten;
1729         *bbp_atten0 = bbp_atten;
1730 }
1731
1732 static void
1733 bwi_mac_adjust_tpctl(struct bwi_mac *mac, int rf_atten_adj, int bbp_atten_adj)
1734 {
1735         struct bwi_softc *sc = mac->mac_sc;
1736         struct bwi_rf *rf = &mac->mac_rf;
1737         struct bwi_tpctl tpctl;
1738         int bbp_atten, rf_atten, tp_ctrl1;
1739
1740         bcopy(&mac->mac_tpctl, &tpctl, sizeof(tpctl));
1741
1742         /* NOTE: Use signed value to do calulation */
1743         bbp_atten = tpctl.bbp_atten;
1744         rf_atten = tpctl.rf_atten;
1745         tp_ctrl1 = tpctl.tp_ctrl1;
1746
1747         bbp_atten += bbp_atten_adj;
1748         rf_atten += rf_atten_adj;
1749
1750         bwi_mac_balance_atten(&bbp_atten, &rf_atten);
1751
1752         if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev == 2) {
1753                 if (rf_atten <= 1) {
1754                         if (tp_ctrl1 == 0) {
1755                                 tp_ctrl1 = 3;
1756                                 bbp_atten += 2;
1757                                 rf_atten += 2;
1758                         } else if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9) {
1759                                 bbp_atten +=
1760                                 (BWI_RF_ATTEN_FACTOR * (rf_atten - 2));
1761                                 rf_atten = 2;
1762                         }
1763                 } else if (rf_atten > 4 && tp_ctrl1 != 0) {
1764                         tp_ctrl1 = 0;
1765                         if (bbp_atten < 3) {
1766                                 bbp_atten += 2;
1767                                 rf_atten -= 3;
1768                         } else {
1769                                 bbp_atten -= 2;
1770                                 rf_atten -= 2;
1771                         }
1772                 }
1773                 bwi_mac_balance_atten(&bbp_atten, &rf_atten);
1774         }
1775
1776         tpctl.bbp_atten = bbp_atten;
1777         tpctl.rf_atten = rf_atten;
1778         tpctl.tp_ctrl1 = tp_ctrl1;
1779
1780         bwi_mac_lock(mac);
1781         bwi_mac_set_tpctl_11bg(mac, &tpctl);
1782         bwi_mac_unlock(mac);
1783 }
1784
1785 /*
1786  * http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower
1787  */
1788 void
1789 bwi_mac_calibrate_txpower(struct bwi_mac *mac, enum bwi_txpwrcb_type type)
1790 {
1791         struct bwi_softc *sc = mac->mac_sc;
1792         struct bwi_rf *rf = &mac->mac_rf;
1793         int8_t tssi[4], tssi_avg, cur_txpwr;
1794         int error, i, ofdm_tssi;
1795         int txpwr_diff, rf_atten_adj, bbp_atten_adj;
1796
1797         if (!sc->sc_txpwr_calib)
1798                 return;
1799
1800         if (mac->mac_flags & BWI_MAC_F_TPCTL_ERROR) {
1801                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1802                         "tpctl error happened, can't set txpower");
1803                 return;
1804         }
1805
1806         if (BWI_IS_BRCM_BU4306(sc)) {
1807                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1808                         "BU4306, can't set txpower");
1809                 return;
1810         }
1811
1812         /*
1813          * Save latest TSSI and reset the related memory objects
1814          */
1815         ofdm_tssi = 0;
1816         error = bwi_rf_get_latest_tssi(mac, tssi, BWI_COMM_MOBJ_TSSI_DS);
1817         if (error) {
1818                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1819                         "no DS tssi");
1820
1821                 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11B) {
1822                         if (type == BWI_TXPWR_FORCE) {
1823                                 rf_atten_adj = 0;
1824                                 bbp_atten_adj = 1;
1825                                 goto calib;
1826                         } else {
1827                                 return;
1828                         }
1829                 }
1830
1831                 error = bwi_rf_get_latest_tssi(mac, tssi,
1832                                 BWI_COMM_MOBJ_TSSI_OFDM);
1833                 if (error) {
1834                         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1835                                 "no OFDM tssi");
1836                         if (type == BWI_TXPWR_FORCE) {
1837                                 rf_atten_adj = 0;
1838                                 bbp_atten_adj = 1;
1839                                 goto calib;
1840                         } else {
1841                                 return;
1842                         }
1843                 }
1844
1845                 for (i = 0; i < 4; ++i) {
1846                         tssi[i] += 0x20;
1847                         tssi[i] &= 0x3f;
1848                 }
1849                 ofdm_tssi = 1;
1850         }
1851         bwi_rf_clear_tssi(mac);
1852
1853         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER,
1854                 "tssi0 %d, tssi1 %d, tssi2 %d, tssi3 %d\n",
1855                 tssi[0], tssi[1], tssi[2], tssi[3]);
1856
1857         /*
1858          * Calculate RF/BBP attenuation adjustment based on
1859          * the difference between desired TX power and sampled
1860          * TX power.
1861          */
1862         /* +8 == "each incremented by 1/2" */
1863         tssi_avg = (tssi[0] + tssi[1] + tssi[2] + tssi[3] + 8) / 4;
1864         if (ofdm_tssi && (HFLAGS_READ(mac) & BWI_HFLAG_PWR_BOOST_DS))
1865                 tssi_avg -= 13;
1866
1867         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "tssi avg %d\n", tssi_avg);
1868
1869         error = bwi_rf_tssi2dbm(mac, tssi_avg, &cur_txpwr);
1870         if (error)
1871                 return;
1872         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "current txpower %d\n",
1873                 cur_txpwr);
1874
1875         txpwr_diff = rf->rf_txpower_max - cur_txpwr; /* XXX ni_txpower */
1876
1877         rf_atten_adj = -howmany(txpwr_diff, 8);
1878         if (type == BWI_TXPWR_INIT) {
1879                 /*
1880                  * Move toward EEPROM max TX power as fast as we can
1881                  */
1882                 bbp_atten_adj = -txpwr_diff;
1883         } else {
1884                 bbp_atten_adj = -(txpwr_diff / 2);
1885         }
1886         bbp_atten_adj -= (BWI_RF_ATTEN_FACTOR * rf_atten_adj);
1887
1888         if (rf_atten_adj == 0 && bbp_atten_adj == 0) {
1889                 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1890                         "no need to adjust RF/BBP attenuation");
1891                 /* TODO: LO */
1892                 return;
1893         }
1894
1895 calib:
1896         DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER,
1897                 "rf atten adjust %d, bbp atten adjust %d\n",
1898                 rf_atten_adj, bbp_atten_adj);
1899         bwi_mac_adjust_tpctl(mac, rf_atten_adj, bbp_atten_adj);
1900         /* TODO: LO */
1901 }
1902
1903 static void
1904 bwi_mac_lock(struct bwi_mac *mac)
1905 {
1906         struct bwi_softc *sc = mac->mac_sc;
1907         struct ieee80211com *ic = &sc->sc_ic;
1908
1909         KKASSERT((mac->mac_flags & BWI_MAC_F_LOCKED) == 0);
1910
1911         if (mac->mac_rev < 3)
1912                 bwi_mac_stop(mac);
1913         else if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1914                 bwi_mac_config_ps(mac);
1915
1916         CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_RFLOCK);
1917
1918         /* Flush pending bus write */
1919         CSR_READ_4(sc, BWI_MAC_STATUS);
1920         DELAY(10);
1921
1922         mac->mac_flags |= BWI_MAC_F_LOCKED;
1923 }
1924
1925 static void
1926 bwi_mac_unlock(struct bwi_mac *mac)
1927 {
1928         struct bwi_softc *sc = mac->mac_sc;
1929         struct ieee80211com *ic = &sc->sc_ic;
1930
1931         KKASSERT(mac->mac_flags & BWI_MAC_F_LOCKED);
1932
1933         CSR_READ_2(sc, BWI_PHYINFO); /* dummy read */
1934
1935         CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_RFLOCK);
1936
1937         if (mac->mac_rev < 3)
1938                 bwi_mac_start(mac);
1939         else if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1940                 bwi_mac_config_ps(mac);
1941
1942         mac->mac_flags &= ~BWI_MAC_F_LOCKED;
1943 }
1944
1945 void
1946 bwi_mac_set_promisc(struct bwi_mac *mac, int promisc)
1947 {
1948         struct bwi_softc *sc = mac->mac_sc;
1949
1950         if (mac->mac_rev < 5) /* Promisc is always on */
1951                 return;
1952
1953         if (promisc)
1954                 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PROMISC);
1955         else
1956                 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PROMISC);
1957 }