xref: /titanic_44/usr/src/uts/common/io/rwn/rt2860.c (revision b5a2d8455dfa3190fc977c4bec53e91c99012767)
1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2007, 2008
8  *	Damien Bergamini <damien.bergamini@free.fr>
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 /*
24  * Ralink Technology RT2860 chipset driver
25  * http://www.ralinktech.com/
26  */
27 
28 #include <sys/types.h>
29 #include <sys/byteorder.h>
30 #include <sys/conf.h>
31 #include <sys/cmn_err.h>
32 #include <sys/stat.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/strsubr.h>
36 #include <inet/common.h>
37 #include <sys/note.h>
38 #include <sys/stream.h>
39 #include <sys/strsun.h>
40 #include <sys/modctl.h>
41 #include <sys/devops.h>
42 #include <sys/mac_provider.h>
43 #include <sys/mac_wifi.h>
44 #include <sys/net80211.h>
45 #include <sys/net80211_proto.h>
46 #include <sys/varargs.h>
47 #include <sys/pci.h>
48 #include <sys/crypto/common.h>
49 #include <sys/crypto/api.h>
50 #include <inet/wifi_ioctl.h>
51 
52 #include "rt2860_reg.h"
53 #include "rt2860_var.h"
54 
55 #define	RT2860_DBG_80211	(1 << 0)
56 #define	RT2860_DBG_DMA		(1 << 1)
57 #define	RT2860_DBG_EEPROM	(1 << 2)
58 #define	RT2860_DBG_FW		(1 << 3)
59 #define	RT2860_DBG_HW		(1 << 4)
60 #define	RT2860_DBG_INTR		(1 << 5)
61 #define	RT2860_DBG_RX		(1 << 6)
62 #define	RT2860_DBG_SCAN		(1 << 7)
63 #define	RT2860_DBG_TX		(1 << 8)
64 #define	RT2860_DBG_RADIO	(1 << 9)
65 #define	RT2860_DBG_RESUME	(1 << 10)
66 #define	RT2860_DBG_MSG		(1 << 11)
67 
68 uint32_t rt2860_dbg_flags = 0x0;
69 
70 #ifdef DEBUG
71 #define	RWN_DEBUG \
72 	rt2860_debug
73 #else
74 #define	RWN_DEBUG
75 #endif
76 
77 static void *rt2860_soft_state_p = NULL;
78 static uint8_t rt2860_fw_bin [] = {
79 #include "fw-rt2860/rt2860.ucode"
80 };
81 
82 static const struct ieee80211_rateset rt2860_rateset_11b =
83 	{ 4, { 2, 4, 11, 22 } };
84 
85 static const struct ieee80211_rateset rt2860_rateset_11g =
86 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
87 
88 static const struct {
89 	uint32_t	reg;
90 	uint32_t	val;
91 } rt2860_def_mac[] = {
92 	RT2860_DEF_MAC
93 };
94 
95 static const struct {
96 	uint8_t	reg;
97 	uint8_t	val;
98 } rt2860_def_bbp[] = {
99 	RT2860_DEF_BBP
100 };
101 
102 static const struct rfprog {
103 	uint8_t		chan;
104 	uint32_t	r1, r2, r3, r4;
105 } rt2860_rf2850[] = {
106 	RT2860_RF2850
107 };
108 
109 /*
110  * PIO access attributes for registers
111  */
112 static ddi_device_acc_attr_t rwn_csr_accattr = {
113 	DDI_DEVICE_ATTR_V0,
114 	DDI_STRUCTURE_LE_ACC,
115 	DDI_STRICTORDER_ACC
116 };
117 
118 /*
119  * DMA access attributes for descriptors: NOT to be byte swapped.
120  */
121 static ddi_device_acc_attr_t rt2860_desc_accattr = {
122 	DDI_DEVICE_ATTR_V0,
123 	DDI_STRUCTURE_LE_ACC,
124 	DDI_STRICTORDER_ACC
125 };
126 
127 static ddi_device_acc_attr_t rt2860_buf_accattr = {
128 	DDI_DEVICE_ATTR_V0,
129 	DDI_NEVERSWAP_ACC,
130 	DDI_STRICTORDER_ACC,
131 	DDI_DEFAULT_ACC
132 };
133 
134 /*
135  * Describes the chip's DMA engine
136  */
137 static ddi_dma_attr_t rt2860_dma_attr = {
138 	DMA_ATTR_V0,			/* dma_attr version */
139 	0x0,				/* dma_attr_addr_lo */
140 	0xffffffffU,			/* dma_attr_addr_hi */
141 	0xffffffffU,			/* dma_attr_count_max */
142 	16,				/* dma_attr_align */
143 	0x00000fff,			/* dma_attr_burstsizes */
144 	1,				/* dma_attr_minxfer */
145 	0xffffffffU,			/* dma_attr_maxxfer */
146 	0xffffffffU,			/* dma_attr_seg */
147 	1,				/* dma_attr_sgllen */
148 	1,				/* dma_attr_granular */
149 	0				/* dma_attr_flags */
150 };
151 
152 static uint16_t	rt2860_eeprom_read(struct rt2860_softc *, uint8_t);
153 static int 	rt2860_read_eeprom(struct rt2860_softc *);
154 const char	*rt2860_get_rf(uint8_t);
155 static int	rt2860_alloc_dma_mem(dev_info_t *, ddi_dma_attr_t *, size_t,
156 		    ddi_device_acc_attr_t *, uint_t, uint_t, struct dma_area *);
157 static void	rt2860_free_dma_mem(struct dma_area *);
158 static int	rt2860_alloc_tx_ring(struct rt2860_softc *,
159 		    struct rt2860_tx_ring *);
160 static void	rt2860_free_tx_ring(struct rt2860_softc *,
161 		    struct rt2860_tx_ring *);
162 static int	rt2860_alloc_rx_ring(struct rt2860_softc *,
163 		    struct rt2860_rx_ring *);
164 static void	rt2860_free_rx_ring(struct rt2860_softc *,
165 		    struct rt2860_rx_ring *);
166 static int	rt2860_alloc_tx_pool(struct rt2860_softc *);
167 static void	rt2860_free_tx_pool(struct rt2860_softc *);
168 static uint16_t	rt2860_txtime(int, int, uint32_t);
169 static int	rt2860_ack_rate(struct ieee80211com *, int);
170 static int	rt2860_send(ieee80211com_t *, mblk_t *, uint8_t);
171 static uint8_t	rt2860_maxrssi_chain(struct rt2860_softc *,
172 		    const struct rt2860_rxwi *);
173 static void	rt2860_drain_stats_fifo(struct rt2860_softc *);
174 static void	rt2860_tx_intr(struct rt2860_softc *, int);
175 static void	rt2860_rx_intr(struct rt2860_softc *);
176 static uint_t	rt2860_softintr(caddr_t);
177 static uint_t	rt2860_intr(caddr_t);
178 static void	rt2860_set_region_4(struct rt2860_softc *,
179 		    uint32_t, uint32_t, int);
180 static int	rt2860_load_microcode(struct rt2860_softc *);
181 static void	rt2860_set_macaddr(struct rt2860_softc *, const uint8_t *);
182 static int	rt2860_bbp_init(struct rt2860_softc *);
183 static uint8_t	rt2860_mcu_bbp_read(struct rt2860_softc *, uint8_t);
184 static void	rt2860_mcu_bbp_write(struct rt2860_softc *, uint8_t, uint8_t);
185 static int	rt2860_mcu_cmd(struct rt2860_softc *, uint8_t, uint16_t);
186 static void	rt2860_rf_write(struct rt2860_softc *, uint8_t, uint32_t);
187 static void	rt2860_select_chan_group(struct rt2860_softc *, int);
188 static void	rt2860_set_chan(struct rt2860_softc *,
189 		    struct ieee80211_channel *);
190 static void	rt2860_updateprot(struct ieee80211com *);
191 static void	rt2860_set_leds(struct rt2860_softc *, uint16_t);
192 static void	rt2860_next_scan(void *);
193 static void	rt2860_iter_func(void *, struct ieee80211_node *);
194 static void	rt2860_updateslot(struct rt2860_softc *);
195 static uint8_t	rt2860_rate2mcs(uint8_t);
196 static void	rt2860_enable_mrr(struct rt2860_softc *);
197 static void	rt2860_set_txpreamble(struct rt2860_softc *);
198 static void	rt2860_set_basicrates(struct rt2860_softc *);
199 static void	rt2860_set_bssid(struct rt2860_softc *, const uint8_t *);
200 static void	rt2860_amrr_node_init(const struct rt2860_amrr *,
201 		    struct rt2860_amrr_node *);
202 static void	rt2860_amrr_choose(struct rt2860_amrr *,
203 		    struct ieee80211_node *, struct rt2860_amrr_node *);
204 static void	rt2860_newassoc(struct ieee80211com *, struct ieee80211_node *,
205 		    int);
206 static void	rt2860_enable_tsf_sync(struct rt2860_softc *);
207 static int	rt2860_newstate(struct ieee80211com *,
208 		    enum ieee80211_state, int);
209 static int	rt2860_init(struct rt2860_softc *);
210 static void	rt2860_stop(struct rt2860_softc *);
211 static int	rt2860_quiesce(dev_info_t *t);
212 
213 /*
214  * device operations
215  */
216 static int rt2860_attach(dev_info_t *, ddi_attach_cmd_t);
217 static int rt2860_detach(dev_info_t *, ddi_detach_cmd_t);
218 
219 /*
220  * Module Loading Data & Entry Points
221  */
222 DDI_DEFINE_STREAM_OPS(rwn_dev_ops, nulldev, nulldev, rt2860_attach,
223     rt2860_detach, nodev, NULL, D_MP, NULL, rt2860_quiesce);
224 
225 static struct modldrv rwn_modldrv = {
226 	&mod_driverops,		/* Type of module.  This one is a driver */
227 	"Ralink RT2700/2800 driver v1.2",	/* short description */
228 	&rwn_dev_ops		/* driver specific ops */
229 };
230 
231 static struct modlinkage modlinkage = {
232 	MODREV_1,
233 	(void *)&rwn_modldrv,
234 	NULL
235 };
236 
237 static int	rt2860_m_stat(void *,  uint_t, uint64_t *);
238 static int	rt2860_m_start(void *);
239 static void	rt2860_m_stop(void *);
240 static int	rt2860_m_promisc(void *, boolean_t);
241 static int	rt2860_m_multicst(void *, boolean_t, const uint8_t *);
242 static int	rt2860_m_unicst(void *, const uint8_t *);
243 static mblk_t	*rt2860_m_tx(void *, mblk_t *);
244 static void	rt2860_m_ioctl(void *, queue_t *, mblk_t *);
245 static int	rt2860_m_setprop(void *arg, const char *pr_name,
246 		    mac_prop_id_t wldp_pr_num,
247 		    uint_t wldp_length, const void *wldp_buf);
248 static int	rt2860_m_getprop(void *arg, const char *pr_name,
249 		    mac_prop_id_t wldp_pr_num, uint_t pr_flags,
250 		    uint_t wldp_length, void *wldp_buf, uint_t *);
251 
252 static mac_callbacks_t rt2860_m_callbacks = {
253 	MC_IOCTL | MC_SETPROP | MC_GETPROP,
254 	rt2860_m_stat,
255 	rt2860_m_start,
256 	rt2860_m_stop,
257 	rt2860_m_promisc,
258 	rt2860_m_multicst,
259 	rt2860_m_unicst,
260 	rt2860_m_tx,
261 	rt2860_m_ioctl,
262 	NULL,
263 	NULL,
264 	NULL,
265 	rt2860_m_setprop,
266 	rt2860_m_getprop
267 };
268 
269 #ifdef DEBUG
270 void
271 rt2860_debug(uint32_t dbg_flags, const int8_t *fmt, ...)
272 {
273 	va_list args;
274 
275 	if (dbg_flags & rt2860_dbg_flags) {
276 		va_start(args, fmt);
277 		vcmn_err(CE_CONT, fmt, args);
278 		va_end(args);
279 	}
280 }
281 #endif
282 
283 const char *
284 rt2860_get_rf(uint8_t rev)
285 {
286 	switch (rev) {
287 	case RT2860_RF_2820:	return "RT2820";
288 	case RT2860_RF_2850:	return "RT2850";
289 	case RT2860_RF_2720:	return "RT2720";
290 	case RT2860_RF_2750:	return "RT2750";
291 	default:		return "unknown";
292 	}
293 }
294 
295 /*
296  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46,
297  * 93C66 or 93C86).
298  */
299 static uint16_t
300 rt2860_eeprom_read(struct rt2860_softc *sc, uint8_t addr)
301 {
302 	int		n;
303 	uint16_t	val;
304 	uint32_t	tmp;
305 
306 	/* clock C once before the first command */
307 	RT2860_EEPROM_CTL(sc, 0);
308 
309 	RT2860_EEPROM_CTL(sc, RT2860_S);
310 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_C);
311 	RT2860_EEPROM_CTL(sc, RT2860_S);
312 
313 	/* write start bit (1) */
314 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_D);
315 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_D | RT2860_C);
316 
317 	/* write READ opcode (10) */
318 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_D);
319 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_D | RT2860_C);
320 	RT2860_EEPROM_CTL(sc, RT2860_S);
321 	RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_C);
322 
323 	/* write address (A5-A0 or A7-A0) */
324 	n = ((RT2860_READ(sc, RT2860_PCI_EECTRL) & 0x30) == 0) ? 5 : 7;
325 	for (; n >= 0; n--) {
326 		RT2860_EEPROM_CTL(sc, RT2860_S |
327 		    (((addr >> n) & 1) << RT2860_SHIFT_D));
328 		RT2860_EEPROM_CTL(sc, RT2860_S |
329 		    (((addr >> n) & 1) << RT2860_SHIFT_D) | RT2860_C);
330 	}
331 
332 	RT2860_EEPROM_CTL(sc, RT2860_S);
333 
334 	/* read data Q15-Q0 */
335 	val = 0;
336 	for (n = 15; n >= 0; n--) {
337 		RT2860_EEPROM_CTL(sc, RT2860_S | RT2860_C);
338 		tmp = RT2860_READ(sc, RT2860_PCI_EECTRL);
339 		val |= ((tmp & RT2860_Q) >> RT2860_SHIFT_Q) << n;
340 		RT2860_EEPROM_CTL(sc, RT2860_S);
341 	}
342 
343 	RT2860_EEPROM_CTL(sc, 0);
344 
345 	/* clear Chip Select and clock C */
346 	RT2860_EEPROM_CTL(sc, RT2860_S);
347 	RT2860_EEPROM_CTL(sc, 0);
348 	RT2860_EEPROM_CTL(sc, RT2860_C);
349 
350 	return (val);
351 }
352 
353 /*
354  * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
355  * Used to adjust per-rate Tx power registers.
356  */
357 static inline uint32_t
358 b4inc(uint32_t b32, int8_t delta)
359 {
360 	int8_t i, b4;
361 
362 	for (i = 0; i < 8; i++) {
363 		b4 = b32 & 0xf;
364 		b4 += delta;
365 		if (b4 < 0)
366 			b4 = 0;
367 		else if (b4 > 0xf)
368 			b4 = 0xf;
369 		b32 = b32 >> 4 | b4 << 28;
370 	}
371 	return (b32);
372 }
373 
374 static int
375 rt2860_read_eeprom(struct rt2860_softc *sc)
376 {
377 	struct ieee80211com	*ic = &sc->sc_ic;
378 	int			ridx, ant, i;
379 	int8_t			delta_2ghz, delta_5ghz;
380 	uint16_t		val;
381 
382 	/* read EEPROM version */
383 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_VERSION);
384 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
385 	    "EEPROM rev=%d, FAE=%d\n",
386 	    val & 0xff, val >> 8);
387 
388 	/* read MAC address */
389 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_MAC01);
390 	ic->ic_macaddr[0] = val & 0xff;
391 	ic->ic_macaddr[1] = val >> 8;
392 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_MAC23);
393 	ic->ic_macaddr[2] = val & 0xff;
394 	ic->ic_macaddr[3] = val >> 8;
395 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_MAC45);
396 	ic->ic_macaddr[4] = val & 0xff;
397 	ic->ic_macaddr[5] = val >> 8;
398 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
399 	    "MAC address is: %x:%x:%x:%x:%x:%x\n",
400 	    ic->ic_macaddr[0], ic->ic_macaddr[1],
401 	    ic->ic_macaddr[2], ic->ic_macaddr[3],
402 	    ic->ic_macaddr[4], ic->ic_macaddr[5]);
403 
404 	/* read country code */
405 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_COUNTRY);
406 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
407 	    "EEPROM region code=0x%04x\n", val);
408 
409 	/* read default BBP settings */
410 	for (i = 0; i < 8; i++) {
411 		val = rt2860_eeprom_read(sc, RT2860_EEPROM_BBP_BASE + i);
412 		sc->bbp[i].val = val & 0xff;
413 		sc->bbp[i].reg = val >> 8;
414 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
415 		    "BBP%d=0x%02x\n",
416 		    sc->bbp[i].reg, sc->bbp[i].val);
417 	}
418 
419 	/* read RF frequency offset from EEPROM */
420 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_FREQ_LEDS);
421 	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
422 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
423 	    "EEPROM freq offset %d\n", sc->freq & 0xff);
424 
425 	if ((sc->leds = val >> 8) != 0xff) {
426 		/* read LEDs operating mode */
427 		sc->led[0] = rt2860_eeprom_read(sc, RT2860_EEPROM_LED1);
428 		sc->led[1] = rt2860_eeprom_read(sc, RT2860_EEPROM_LED2);
429 		sc->led[2] = rt2860_eeprom_read(sc, RT2860_EEPROM_LED3);
430 	} else {
431 		/* broken EEPROM, use default settings */
432 		sc->leds = 0x01;
433 		sc->led[0] = 0x5555;
434 		sc->led[1] = 0x2221;
435 		sc->led[2] = 0xa9f8;
436 	}
437 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
438 	    "EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
439 	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
440 
441 	/* read RF information */
442 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_ANTENNA);
443 	if (val == 0xffff) {
444 		/* broken EEPROM, default to RF2820 1T2R */
445 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
446 		    "invalid EEPROM antenna info, using default\n");
447 		sc->rf_rev = RT2860_RF_2820;
448 		sc->ntxchains = 1;
449 		sc->nrxchains = 2;
450 	} else {
451 		sc->rf_rev = (val >> 8) & 0xf;
452 		sc->ntxchains = (val >> 4) & 0xf;
453 		sc->nrxchains = val & 0xf;
454 	}
455 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
456 	    "EEPROM RF rev=0x%02x chains=%dT%dR\n",
457 	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
458 
459 	/* check if RF supports automatic Tx access gain control */
460 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_CONFIG);
461 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
462 	    "EEPROM CFG 0x%04x\n", val);
463 	if ((val & 0xff) != 0xff)
464 		sc->calib_2ghz = sc->calib_5ghz = 0; /* XXX (val >> 1) & 1 */;
465 
466 	if (sc->sc_flags & RT2860_ADVANCED_PS) {
467 		/* read PCIe power save level */
468 		val = rt2860_eeprom_read(sc, RT2860_EEPROM_PCIE_PSLEVEL);
469 		if ((val & 0xff) != 0xff) {
470 			sc->pslevel = val & 0x3;
471 			val = rt2860_eeprom_read(sc, RT2860_EEPROM_REV);
472 			if (val >> 8 != 0x92 || !(val & 0x80))
473 				sc->pslevel = MIN(sc->pslevel, 1);
474 			RWN_DEBUG(RT2860_DBG_EEPROM,
475 			    "rwn: rt2860_read_eeprom(): "
476 			    "EEPROM PCIe PS Level=%d\n",
477 			    sc->pslevel);
478 		}
479 	}
480 	/* read power settings for 2GHz channels */
481 	for (i = 0; i < 14; i += 2) {
482 		val = rt2860_eeprom_read(sc,
483 		    RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2);
484 		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
485 		sc->txpow1[i + 1] = (int8_t)(val >> 8);
486 
487 		val = rt2860_eeprom_read(sc,
488 		    RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2);
489 		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
490 		sc->txpow2[i + 1] = (int8_t)(val >> 8);
491 	}
492 	/* fix broken Tx power entries */
493 	for (i = 0; i < 14; i++) {
494 		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
495 			sc->txpow1[i] = 5;
496 		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
497 			sc->txpow2[i] = 5;
498 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
499 		    "chan %d: power1=%d, power2=%d\n",
500 		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
501 	}
502 	/* read power settings for 5GHz channels */
503 	for (i = 0; i < 36; i += 2) {
504 		val = rt2860_eeprom_read(sc,
505 		    RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2);
506 		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
507 		sc->txpow1[i + 15] = (int8_t)(val >> 8);
508 
509 		val = rt2860_eeprom_read(sc,
510 		    RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2);
511 		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
512 		sc->txpow2[i + 15] = (int8_t)(val >> 8);
513 	}
514 	/* fix broken Tx power entries */
515 	for (i = 0; i < 36; i++) {
516 		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
517 			sc->txpow1[14 + i] = 5;
518 		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
519 			sc->txpow2[14 + i] = 5;
520 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
521 		    "chan %d: power1=%d, power2=%d\n",
522 		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
523 		    sc->txpow2[14 + i]);
524 	}
525 
526 	/* read Tx power compensation for each Tx rate */
527 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_DELTAPWR);
528 	delta_2ghz = delta_5ghz = 0;
529 	if ((val & 0xff) != 0xff && (val & 0x80)) {
530 		delta_2ghz = val & 0xf;
531 		if (!(val & 0x40))	/* negative number */
532 			delta_2ghz = -delta_2ghz;
533 	}
534 	val >>= 8;
535 	if ((val & 0xff) != 0xff && (val & 0x80)) {
536 		delta_5ghz = val & 0xf;
537 		if (!(val & 0x40))	/* negative number */
538 			delta_5ghz = -delta_5ghz;
539 	}
540 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
541 	    "power compensation=%d (2GHz), %d (5GHz) \n",
542 	    delta_2ghz, delta_5ghz);
543 
544 	for (ridx = 0; ridx < 5; ridx++) {
545 		uint32_t	reg;
546 
547 		val = rt2860_eeprom_read(sc, RT2860_EEPROM_RPWR + ridx);
548 		reg = (uint32_t)val << 16;
549 		val = rt2860_eeprom_read(sc, RT2860_EEPROM_RPWR + ridx + 1);
550 		reg |= val;
551 
552 		sc->txpow20mhz[ridx] = reg;
553 		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
554 		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
555 
556 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
557 		    "ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
558 		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
559 		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
560 	}
561 
562 	/* read factory-calibrated samples for temperature compensation */
563 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI1_2GHZ);
564 	sc->tssi_2ghz[0] = val & 0xff;	/* [-4] */
565 	sc->tssi_2ghz[1] = val >> 8;	/* [-3] */
566 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI2_2GHZ);
567 	sc->tssi_2ghz[2] = val & 0xff;	/* [-2] */
568 	sc->tssi_2ghz[3] = val >> 8;	/* [-1] */
569 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI3_2GHZ);
570 	sc->tssi_2ghz[4] = val & 0xff;	/* [+0] */
571 	sc->tssi_2ghz[5] = val >> 8;	/* [+1] */
572 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI4_2GHZ);
573 	sc->tssi_2ghz[6] = val & 0xff;	/* [+2] */
574 	sc->tssi_2ghz[7] = val >> 8;	/* [+3] */
575 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI5_2GHZ);
576 	sc->tssi_2ghz[8] = val & 0xff;	/* [+4] */
577 	sc->step_2ghz = val >> 8;
578 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
579 	    "TSSI 2GHz: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x "
580 	    "0x%02x 0x%02x step=%d\n", sc->tssi_2ghz[0], sc->tssi_2ghz[1],
581 	    sc->tssi_2ghz[2], sc->tssi_2ghz[3], sc->tssi_2ghz[4],
582 	    sc->tssi_2ghz[5], sc->tssi_2ghz[6], sc->tssi_2ghz[7],
583 	    sc->tssi_2ghz[8], sc->step_2ghz);
584 	/* check that ref value is correct, otherwise disable calibration */
585 	if (sc->tssi_2ghz[4] == 0xff)
586 		sc->calib_2ghz = 0;
587 
588 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI1_5GHZ);
589 	sc->tssi_5ghz[0] = val & 0xff;	/* [-4] */
590 	sc->tssi_5ghz[1] = val >> 8;	/* [-3] */
591 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI2_5GHZ);
592 	sc->tssi_5ghz[2] = val & 0xff;	/* [-2] */
593 	sc->tssi_5ghz[3] = val >> 8;	/* [-1] */
594 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI3_5GHZ);
595 	sc->tssi_5ghz[4] = val & 0xff;	/* [+0] */
596 	sc->tssi_5ghz[5] = val >> 8;	/* [+1] */
597 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI4_5GHZ);
598 	sc->tssi_5ghz[6] = val & 0xff;	/* [+2] */
599 	sc->tssi_5ghz[7] = val >> 8;	/* [+3] */
600 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_TSSI5_5GHZ);
601 	sc->tssi_5ghz[8] = val & 0xff;	/* [+4] */
602 	sc->step_5ghz = val >> 8;
603 	RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
604 	    "TSSI 5GHz: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x "
605 	    "0x%02x 0x%02x step=%d\n", sc->tssi_5ghz[0], sc->tssi_5ghz[1],
606 	    sc->tssi_5ghz[2], sc->tssi_5ghz[3], sc->tssi_5ghz[4],
607 	    sc->tssi_5ghz[5], sc->tssi_5ghz[6], sc->tssi_5ghz[7],
608 	    sc->tssi_5ghz[8], sc->step_5ghz);
609 	/* check that ref value is correct, otherwise disable calibration */
610 	if (sc->tssi_5ghz[4] == 0xff)
611 		sc->calib_5ghz = 0;
612 
613 	/* read RSSI offsets and LNA gains from EEPROM */
614 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_RSSI1_2GHZ);
615 	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
616 	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
617 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_RSSI2_2GHZ);
618 	sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
619 	sc->lna[2] = val >> 8;		/* channel group 2 */
620 
621 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_RSSI1_5GHZ);
622 	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
623 	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
624 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_RSSI2_5GHZ);
625 	sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
626 	sc->lna[3] = val >> 8;		/* channel group 3 */
627 
628 	val = rt2860_eeprom_read(sc, RT2860_EEPROM_LNA);
629 	sc->lna[0] = val & 0xff;	/* channel group 0 */
630 	sc->lna[1] = val >> 8;		/* channel group 1 */
631 
632 	/* fix broken 5GHz LNA entries */
633 	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
634 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
635 		    "invalid LNA for channel group %d\n", 2);
636 		sc->lna[2] = sc->lna[1];
637 	}
638 	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
639 		RWN_DEBUG(RT2860_DBG_EEPROM, "rwn: rt2860_read_eeprom(): "
640 		    "invalid LNA for channel group %d\n", 3);
641 		sc->lna[3] = sc->lna[1];
642 	}
643 
644 	/* fix broken RSSI offset entries */
645 	for (ant = 0; ant < 3; ant++) {
646 		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
647 			RWN_DEBUG(RT2860_DBG_EEPROM,
648 			    "rwn: rt2860_read_eeprom(): "
649 			    "invalid RSSI%d offset: %d (2GHz)\n",
650 			    ant + 1, sc->rssi_2ghz[ant]);
651 			sc->rssi_2ghz[ant] = 0;
652 		}
653 		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
654 			RWN_DEBUG(RT2860_DBG_EEPROM,
655 			    "rwn: rt2860_read_eeprom(): "
656 			    "invalid RSSI%d offset: %d (2GHz)\n",
657 			    ant + 1, sc->rssi_5ghz[ant]);
658 			sc->rssi_5ghz[ant] = 0;
659 		}
660 	}
661 
662 	return (RT2860_SUCCESS);
663 }
664 
665 /*
666  * Allocate an DMA memory and a DMA handle for accessing it
667  */
668 static int
669 rt2860_alloc_dma_mem(dev_info_t *devinfo, ddi_dma_attr_t *dma_attr,
670 	size_t memsize, ddi_device_acc_attr_t *attr_p, uint_t alloc_flags,
671 	uint_t bind_flags, struct dma_area *dma_p)
672 {
673 	int	err;
674 
675 	/*
676 	 * Allocate handle
677 	 */
678 	err = ddi_dma_alloc_handle(devinfo, dma_attr,
679 	    DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
680 	if (err != DDI_SUCCESS) {
681 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rwn_allo_dma_mem(): "
682 		    "failed to alloc handle\n");
683 		goto fail1;
684 	}
685 
686 	/*
687 	 * Allocate memory
688 	 */
689 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
690 	    alloc_flags, DDI_DMA_SLEEP, NULL, &dma_p->mem_va,
691 	    &dma_p->alength, &dma_p->acc_hdl);
692 	if (err != DDI_SUCCESS) {
693 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rwn_alloc_dma_mem(): "
694 		    "failed to alloc mem\n");
695 		goto fail2;
696 	}
697 
698 	/*
699 	 * Bind the two together
700 	 */
701 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
702 	    dma_p->mem_va, dma_p->alength, bind_flags,
703 	    DDI_DMA_SLEEP, NULL, &dma_p->cookie, &dma_p->ncookies);
704 	if (err != DDI_DMA_MAPPED) {
705 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rwn_alloc_dma_mem(): "
706 		    "failed to bind handle\n");
707 		goto fail3;
708 	}
709 
710 	if (dma_p->ncookies != 1) {
711 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rwn_alloc_dma_mem(): "
712 		    "failed to alloc cookies\n");
713 		goto fail4;
714 	}
715 
716 	dma_p->nslots = ~0U;
717 	dma_p->size = ~0U;
718 	dma_p->token = ~0U;
719 	dma_p->offset = 0;
720 	return (DDI_SUCCESS);
721 
722 fail4:
723 	(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
724 fail3:
725 	ddi_dma_mem_free(&dma_p->acc_hdl);
726 fail2:
727 	ddi_dma_free_handle(&dma_p->dma_hdl);
728 fail1:
729 	return (err);
730 }
731 
732 static void
733 rt2860_free_dma_mem(struct dma_area *dma_p)
734 {
735 	if (dma_p->dma_hdl != NULL) {
736 		(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
737 		if (dma_p->acc_hdl != NULL) {
738 			ddi_dma_mem_free(&dma_p->acc_hdl);
739 			dma_p->acc_hdl = NULL;
740 		}
741 		ddi_dma_free_handle(&dma_p->dma_hdl);
742 		dma_p->ncookies = 0;
743 		dma_p->dma_hdl = NULL;
744 	}
745 }
746 
747 /*ARGSUSED*/
748 static int
749 rt2860_alloc_tx_ring(struct rt2860_softc *sc, struct rt2860_tx_ring *ring)
750 {
751 	int	size, err;
752 
753 	size = RT2860_TX_RING_COUNT * sizeof (struct rt2860_txd);
754 
755 	err = rt2860_alloc_dma_mem(sc->sc_dev, &rt2860_dma_attr, size,
756 	    &rt2860_desc_accattr, DDI_DMA_CONSISTENT,
757 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
758 	    &ring->txdesc_dma);
759 	if (err != DDI_SUCCESS) {
760 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_alloc_tx_ring(): "
761 		    "failed to alloc dma mem\n");
762 		goto fail1;
763 	}
764 
765 	ring->txd = (struct rt2860_txd *)ring->txdesc_dma.mem_va;
766 	ring->paddr = ring->txdesc_dma.cookie.dmac_address;
767 
768 	ring->cur = 0;
769 	ring->next = 0;
770 	ring->queued = 0;
771 
772 	(void) bzero(ring->txd, size);
773 	RT2860_DMA_SYNC(ring->txdesc_dma, DDI_DMA_SYNC_FORDEV);
774 	return (DDI_SUCCESS);
775 fail1:
776 	return (err);
777 }
778 
779 void
780 rt2860_reset_tx_ring(struct rt2860_softc *sc, struct rt2860_tx_ring *ring)
781 {
782 	struct rt2860_tx_data *data;
783 	int i;
784 
785 	for (i = 0; i < RT2860_TX_RING_COUNT; i++) {
786 		ring->txd[i].sdl0 &= ~LE_16(RT2860_TX_DDONE);
787 
788 		if ((data = ring->data[i]) == NULL)
789 			continue;	/* nothing mapped in this slot */
790 
791 		/* by pass if it's quiesced */
792 		if (!(sc->sc_flags & RT2860_F_QUIESCE))
793 			RT2860_DMA_SYNC(data->txbuf_dma, DDI_DMA_SYNC_FORDEV);
794 
795 		if (data->ni != NULL) {
796 			ieee80211_free_node(data->ni);
797 			data->ni = NULL;	/* node already freed */
798 		}
799 
800 		SLIST_INSERT_HEAD(&sc->data_pool, data, next);
801 		ring->data[i] = NULL;
802 	}
803 
804 	/* by pass if it's quiesced */
805 	if (!(sc->sc_flags & RT2860_F_QUIESCE))
806 		RT2860_DMA_SYNC(ring->txdesc_dma, DDI_DMA_SYNC_FORDEV);
807 
808 	ring->queued = 0;
809 	ring->cur = ring->next = 0;
810 }
811 
812 /*ARGSUSED*/
813 static void
814 rt2860_free_tx_ring(struct rt2860_softc *sc, struct rt2860_tx_ring *ring)
815 {
816 	if (ring->txd != NULL) {
817 		rt2860_free_dma_mem(&ring->txdesc_dma);
818 	}
819 }
820 
821 static int
822 rt2860_alloc_rx_ring(struct rt2860_softc *sc, struct rt2860_rx_ring *ring)
823 {
824 	struct rt2860_rx_data	*data;
825 	struct rt2860_rxd	*rxd;
826 	int			i, err, size, datalen;
827 
828 	size = RT2860_RX_RING_COUNT * sizeof (struct rt2860_rxd);
829 
830 	err = rt2860_alloc_dma_mem(sc->sc_dev, &rt2860_dma_attr, size,
831 	    &rt2860_desc_accattr, DDI_DMA_CONSISTENT,
832 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
833 	    &ring->rxdesc_dma);
834 	if (err != DDI_SUCCESS) {
835 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_alloc_rx_ring(): "
836 		    "failed to alloc dma mem\n");
837 		goto fail1;
838 	}
839 
840 	ring->rxd = (struct rt2860_rxd *)ring->rxdesc_dma.mem_va;
841 	ring->paddr = ring->rxdesc_dma.cookie.dmac_address;
842 	bzero(ring->rxd, size);
843 
844 	/*
845 	 * Pre-allocate Rx buffers and populate Rx ring.
846 	 */
847 	datalen = RT2860_RX_RING_COUNT * sizeof (struct rt2860_rx_data);
848 	bzero(ring->data, datalen);
849 	for (i = 0; i < RT2860_RX_RING_COUNT; i++) {
850 		rxd = &ring->rxd[i];
851 		data = &ring->data[i];
852 		/* alloc DMA memory */
853 		(void) rt2860_alloc_dma_mem(sc->sc_dev, &rt2860_dma_attr,
854 		    sc->sc_dmabuf_size,
855 		    &rt2860_buf_accattr,
856 		    DDI_DMA_STREAMING,
857 		    DDI_DMA_READ | DDI_DMA_STREAMING,
858 		    &data->rxbuf_dma);
859 		rxd->sdp0 = LE_32(data->rxbuf_dma.cookie.dmac_address);
860 		rxd->sdl0 = LE_16(sc->sc_dmabuf_size);
861 	}
862 
863 	ring->cur = 0;
864 
865 	RT2860_DMA_SYNC(ring->rxdesc_dma, DDI_DMA_SYNC_FORDEV);
866 	return (DDI_SUCCESS);
867 fail2:
868 	rt2860_free_dma_mem(&ring->rxdesc_dma);
869 fail1:
870 	return (err);
871 }
872 
873 /*ARGSUSED*/
874 void
875 rt2860_reset_rx_ring(struct rt2860_softc *sc, struct rt2860_rx_ring *ring)
876 {
877 	int i;
878 
879 	for (i = 0; i < RT2860_RX_RING_COUNT; i++)
880 		ring->rxd[i].sdl0 &= ~LE_16(RT2860_RX_DDONE);
881 
882 	RT2860_DMA_SYNC(ring->rxdesc_dma, DDI_DMA_SYNC_FORDEV);
883 
884 	ring->cur = 0;
885 }
886 
887 /*ARGSUSED*/
888 static void
889 rt2860_free_rx_ring(struct rt2860_softc *sc, struct rt2860_rx_ring *ring)
890 {
891 	struct rt2860_rx_data	*data;
892 	int			i, count;
893 
894 	if (ring->rxd != NULL)
895 		rt2860_free_dma_mem(&ring->rxdesc_dma);
896 
897 	count = RT2860_RX_RING_COUNT;
898 	if (ring->data != NULL) {
899 		for (i = 0; i < count; i++) {
900 			data = &ring->data[i];
901 			rt2860_free_dma_mem(&data->rxbuf_dma);
902 		}
903 	}
904 }
905 
906 static int
907 rt2860_alloc_tx_pool(struct rt2860_softc *sc)
908 {
909 	struct rt2860_tx_data	*data;
910 	int			i, err, size;
911 
912 	size = RT2860_TX_POOL_COUNT * sizeof (struct rt2860_txwi);
913 
914 	/* init data_pool early in case of failure.. */
915 	SLIST_INIT(&sc->data_pool);
916 
917 	err = rt2860_alloc_dma_mem(sc->sc_dev, &rt2860_dma_attr, size,
918 	    &rt2860_desc_accattr, DDI_DMA_CONSISTENT,
919 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
920 	    &sc->txpool_dma);
921 	if (err != DDI_SUCCESS) {
922 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_alloc_tx_pool(): "
923 		    "failed to alloc dma mem\n");
924 		goto fail1;
925 	}
926 
927 	sc->txwi = (struct rt2860_txwi *)sc->txpool_dma.mem_va;
928 	(void) bzero(sc->txwi, size);
929 	RT2860_DMA_SYNC(sc->txpool_dma, DDI_DMA_SYNC_FORDEV);
930 
931 	for (i = 0; i < RT2860_TX_POOL_COUNT; i++) {
932 		data = &sc->data[i];
933 
934 		err = rt2860_alloc_dma_mem(sc->sc_dev, &rt2860_dma_attr,
935 		    sc->sc_dmabuf_size,
936 		    &rt2860_buf_accattr, DDI_DMA_CONSISTENT,
937 		    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
938 		    &data->txbuf_dma);
939 		if (err != DDI_SUCCESS) {
940 			RWN_DEBUG(RT2860_DBG_DMA,
941 			    "rwn: rt2860_alloc_tx_pool(): "
942 			    "failed to alloc dma mem\n");
943 			goto fail2;
944 		}
945 		data->txwi = &sc->txwi[i];
946 		data->paddr = sc->txpool_dma.cookie.dmac_address +
947 		    i * sizeof (struct rt2860_txwi);
948 
949 		SLIST_INSERT_HEAD(&sc->data_pool, data, next);
950 	}
951 	return (DDI_SUCCESS);
952 fail2:
953 	rt2860_free_dma_mem(&sc->txpool_dma);
954 fail1:
955 	return (err);
956 }
957 
958 static void
959 rt2860_free_tx_pool(struct rt2860_softc *sc)
960 {
961 	struct rt2860_tx_data	*data;
962 	int	i;
963 
964 	if (sc->txwi != NULL) {
965 		rt2860_free_dma_mem(&sc->txpool_dma);
966 	}
967 
968 	for (i = 0; i < RT2860_TX_POOL_COUNT; i++) {
969 		data = &sc->data[i];
970 		rt2860_free_dma_mem(&data->txbuf_dma);
971 	}
972 }
973 
974 /* quickly determine if a given rate is CCK or OFDM */
975 #define	RT2860_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
976 
977 #define	RT2860_ACK_SIZE		14	/* 10 + 4(FCS) */
978 #define	RT2860_SIFS_TIME	10
979 
980 static uint8_t
981 rt2860_rate2mcs(uint8_t rate)
982 {
983 	switch (rate) {
984 	/* CCK rates */
985 	case 2:
986 		return (0);
987 	case 4:
988 		return (1);
989 	case 11:
990 		return (2);
991 	case 22:
992 		return (3);
993 	/* OFDM rates */
994 	case 12:
995 		return (0);
996 	case 18:
997 		return (1);
998 	case 24:
999 		return (2);
1000 	case 36:
1001 		return (3);
1002 	case 48:
1003 		return (4);
1004 	case 72:
1005 		return (5);
1006 	case 96:
1007 		return (6);
1008 	case 108:
1009 		return (7);
1010 	}
1011 
1012 	return (0);	/* shouldn't get there */
1013 }
1014 
1015 /*
1016  * Return the expected ack rate for a frame transmitted at rate `rate'.
1017  */
1018 static int
1019 rt2860_ack_rate(struct ieee80211com *ic, int rate)
1020 {
1021 	switch (rate) {
1022 	/* CCK rates */
1023 	case 2:
1024 		return (2);
1025 	case 4:
1026 	case 11:
1027 	case 22:
1028 		return ((ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate);
1029 
1030 	/* OFDM rates */
1031 	case 12:
1032 	case 18:
1033 		return (12);
1034 	case 24:
1035 	case 36:
1036 		return (24);
1037 	case 48:
1038 	case 72:
1039 	case 96:
1040 	case 108:
1041 		return (48);
1042 	}
1043 
1044 	/* default to 1Mbps */
1045 	return (2);
1046 }
1047 
1048 
1049 /*
1050  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1051  * The function automatically determines the operating mode depending on the
1052  * given rate. `flags' indicates whether short preamble is in use or not.
1053  */
1054 static uint16_t
1055 rt2860_txtime(int len, int rate, uint32_t flags)
1056 {
1057 	uint16_t	txtime;
1058 
1059 	if (RT2860_RATE_IS_OFDM(rate)) {
1060 		/* IEEE Std 802.11g-2003, pp. 44 */
1061 		txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1062 		txtime = 16 + 4 + 4 * txtime + 6;
1063 	} else {
1064 		/* IEEE Std 802.11b-1999, pp. 28 */
1065 		txtime = (16 * len + rate - 1) / rate;
1066 		if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1067 			txtime +=  72 + 24;
1068 		else
1069 			txtime += 144 + 48;
1070 	}
1071 	return (txtime);
1072 }
1073 
1074 static int
1075 rt2860_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
1076 {
1077 	struct rt2860_softc	*sc = (struct rt2860_softc *)ic;
1078 	struct rt2860_tx_ring	*ring;
1079 	struct rt2860_tx_data	*data;
1080 	struct rt2860_txd	*txd;
1081 	struct rt2860_txwi	*txwi;
1082 	struct ieee80211_frame	*wh;
1083 	struct ieee80211_node	*ni;
1084 	int			qid, off, rate, err;
1085 	int			mblen, pktlen;
1086 	uint_t			hdrlen;
1087 	uint8_t			mcs, pid, qsel;
1088 	uint16_t		dur;
1089 	mblk_t			*m, *m0;
1090 
1091 	err = DDI_SUCCESS;
1092 
1093 	mutex_enter(&sc->sc_txlock);
1094 	if (RT2860_IS_SUSPEND(sc)) {
1095 		err = ENXIO;
1096 		goto fail1;
1097 	}
1098 
1099 	if ((type & IEEE80211_FC0_TYPE_MASK) !=
1100 	    IEEE80211_FC0_TYPE_DATA)
1101 		qid = sc->mgtqid;
1102 	else
1103 		qid = EDCA_AC_BE;
1104 	ring = &sc->txq[qid];
1105 
1106 	if (SLIST_EMPTY(&sc->data_pool) || (ring->queued > 15)) {
1107 		sc->sc_need_sched = 1;
1108 		sc->sc_tx_nobuf++;
1109 		err = ENOMEM;
1110 		goto fail1;
1111 	}
1112 
1113 	/* the data pool contains at least one element, pick the first */
1114 	data = SLIST_FIRST(&sc->data_pool);
1115 
1116 	m = allocb(msgdsize(mp) + 32, BPRI_MED);
1117 	if (m == NULL) {
1118 		RWN_DEBUG(RT2860_DBG_TX, "rwn: rt2860_send():"
1119 		    "rt2860_mgmt_send: can't alloc mblk.\n");
1120 		err = DDI_FAILURE;
1121 		goto fail1;
1122 	}
1123 
1124 	for (off = 0, m0 = mp; m0 != NULL; m0 = m0->b_cont) {
1125 		mblen = MBLKL(m0);
1126 		(void) bcopy(m0->b_rptr, m->b_rptr + off, mblen);
1127 		off += mblen;
1128 	}
1129 	m->b_wptr += off;
1130 
1131 	wh = (struct ieee80211_frame *)m->b_rptr;
1132 	ni = ieee80211_find_txnode(ic, wh->i_addr1);
1133 	if (ni == NULL) {
1134 		err = DDI_FAILURE;
1135 		sc->sc_tx_err++;
1136 		goto fail2;
1137 	}
1138 
1139 	if ((type & IEEE80211_FC0_TYPE_MASK) ==
1140 	    IEEE80211_FC0_TYPE_DATA)
1141 		(void) ieee80211_encap(ic, m, ni);
1142 
1143 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1144 		struct ieee80211_key *k;
1145 		k = ieee80211_crypto_encap(ic, m);
1146 		if (k == NULL) {
1147 			sc->sc_tx_err++;
1148 			err = DDI_FAILURE;
1149 			goto fail3;
1150 		}
1151 		/* packet header may have moved, reset our local pointer */
1152 		wh = (struct ieee80211_frame *)m->b_rptr;
1153 	}
1154 	pktlen = msgdsize(m);
1155 	hdrlen = sizeof (*wh);
1156 
1157 	/* pickup a rate */
1158 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1159 	    ((type & IEEE80211_FC0_TYPE_MASK) !=
1160 	    IEEE80211_FC0_TYPE_DATA))
1161 		rate = ni->in_rates.ir_rates[0];
1162 	else {
1163 		if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1164 			rate = ic->ic_fixed_rate;
1165 		else
1166 			rate = ni->in_rates.ir_rates[ni->in_txrate];
1167 	}
1168 	rate &= IEEE80211_RATE_VAL;
1169 
1170 	/* get MCS code from rate */
1171 	mcs = rt2860_rate2mcs(rate);
1172 
1173 	/* setup TX Wireless Information */
1174 	txwi = data->txwi;
1175 	(void) bzero(txwi, sizeof (struct rt2860_txwi));
1176 	txwi->wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
1177 	    RT2860_AID2WCID(ni->in_associd) : 0xff;
1178 	txwi->len = LE_16(pktlen);
1179 	if (!RT2860_RATE_IS_OFDM(rate)) {
1180 		txwi->phy = LE_16(RT2860_PHY_CCK);
1181 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1182 			mcs |= RT2860_PHY_SHPRE;
1183 	} else
1184 		txwi->phy = LE_16(RT2860_PHY_OFDM);
1185 	txwi->phy |= LE_16(mcs);
1186 
1187 	/*
1188 	 * We store the MCS code into the driver-private PacketID field.
1189 	 * The PacketID is latched into TX_STAT_FIFO when Tx completes so
1190 	 * that we know at which initial rate the frame was transmitted.
1191 	 * We add 1 to the MCS code because setting the PacketID field to
1192 	 * 0 means that we don't want feedback in TX_STAT_FIFO.
1193 	 */
1194 	pid = (mcs + 1) & 0xf;
1195 	txwi->len |= LE_16(pid << RT2860_TX_PID_SHIFT);
1196 
1197 	/* check if RTS/CTS or CTS-to-self protection is required */
1198 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1199 	    (pktlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold ||
1200 	    ((ic->ic_flags &
1201 	    IEEE80211_F_USEPROT) && RT2860_RATE_IS_OFDM(rate))))
1202 		txwi->txop = RT2860_TX_TXOP_HT;
1203 	else
1204 		txwi->txop = RT2860_TX_TXOP_BACKOFF;
1205 
1206 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1207 		txwi->xflags |= RT2860_TX_ACK;
1208 
1209 		dur = rt2860_txtime(RT2860_ACK_SIZE, rt2860_ack_rate(ic, rate),
1210 		    ic->ic_flags) + sc->sifs;
1211 		*(uint16_t *)wh->i_dur = LE_16(dur);
1212 	}
1213 
1214 	/* copy and trim 802.11 header */
1215 	bcopy(wh, &txwi->wh, hdrlen);
1216 	m->b_rptr += hdrlen;
1217 	bcopy(m->b_rptr, data->txbuf_dma.mem_va, pktlen - hdrlen);
1218 
1219 	qsel = (qid < EDCA_NUM_AC) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_MGMT;
1220 
1221 	/* first segment is TXWI + 802.11 header */
1222 	txd = &ring->txd[ring->cur];
1223 	txd->sdp0 = LE_32(data->paddr);
1224 	txd->sdl0 = LE_16(16 + hdrlen);
1225 	txd->flags = qsel;
1226 
1227 	/* finalize last segment */
1228 	txd->sdp1 = LE_32(data->txbuf_dma.cookie.dmac_address);
1229 	txd->sdl1 = LE_16(pktlen - hdrlen | RT2860_TX_LS1);
1230 
1231 	/* remove from the free pool and link it into the SW Tx slot */
1232 	SLIST_REMOVE_HEAD(&sc->data_pool, next);
1233 	data->ni = ieee80211_ref_node(ni);
1234 	ring->data[ring->cur] = data;
1235 
1236 	(void) ddi_dma_sync(sc->txpool_dma.dma_hdl,
1237 	    _PTRDIFF(txwi, sc->txwi),
1238 	    (hdrlen + 16 + 2),
1239 	    DDI_DMA_SYNC_FORDEV);
1240 	RT2860_DMA_SYNC(data->txbuf_dma, DDI_DMA_SYNC_FORDEV);
1241 	RT2860_DMA_SYNC(ring->txdesc_dma, DDI_DMA_SYNC_FORDEV);
1242 
1243 	RWN_DEBUG(RT2860_DBG_TX, "rwn: rt2860_send():"
1244 	    "sending frame qid=%d wcid=%d rate=%d cur = %x\n",
1245 	    qid, txwi->wcid, rate, ring->cur);
1246 
1247 	ring->queued++;
1248 	ring->cur = (ring->cur + 1) % RT2860_TX_RING_COUNT;
1249 
1250 	/* kick Tx */
1251 	RT2860_WRITE(sc, RT2860_TX_CTX_IDX(qid), ring->cur);
1252 
1253 	sc->sc_tx_timer = 5;
1254 
1255 	ic->ic_stats.is_tx_frags++;
1256 	ic->ic_stats.is_tx_bytes += pktlen;
1257 
1258 fail3:
1259 	ieee80211_free_node(ni);
1260 fail2:
1261 	freemsg(m);
1262 fail1:
1263 	if ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA ||
1264 	    err == DDI_SUCCESS)
1265 		freemsg(mp);
1266 	mutex_exit(&sc->sc_txlock);
1267 	return (err);
1268 }
1269 
1270 /*
1271  * This function is called periodically (every 200ms) during scanning to
1272  * switch from one channel to another.
1273  */
1274 static void
1275 rt2860_next_scan(void *arg)
1276 {
1277 	struct rt2860_softc *sc = (struct rt2860_softc *)arg;
1278 	struct ieee80211com *ic = &sc->sc_ic;
1279 
1280 	if (ic->ic_state == IEEE80211_S_SCAN)
1281 		(void) ieee80211_next_scan(ic);
1282 }
1283 
1284 static void
1285 rt2860_updateslot(struct rt2860_softc *sc)
1286 {
1287 	struct ieee80211com *ic = &sc->sc_ic;
1288 	uint32_t tmp;
1289 
1290 	tmp = RT2860_READ(sc, RT2860_BKOFF_SLOT_CFG);
1291 	tmp &= ~0xff;
1292 	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1293 	RT2860_WRITE(sc, RT2860_BKOFF_SLOT_CFG, tmp);
1294 }
1295 
1296 static void
1297 rt2860_iter_func(void *arg, struct ieee80211_node *ni)
1298 {
1299 	struct rt2860_softc *sc = (struct rt2860_softc *)arg;
1300 	uint8_t wcid;
1301 
1302 	wcid = RT2860_AID2WCID(ni->in_associd);
1303 	rt2860_amrr_choose(&sc->amrr, ni, &sc->amn[wcid]);
1304 }
1305 
1306 static void
1307 rt2860_updatestats(void *arg)
1308 {
1309 	struct rt2860_softc *sc = (struct rt2860_softc *)arg;
1310 	struct ieee80211com *ic = &sc->sc_ic;
1311 
1312 	if (ic->ic_opmode == IEEE80211_M_STA)
1313 		rt2860_iter_func(sc, ic->ic_bss);
1314 	else
1315 		ieee80211_iterate_nodes(&ic->ic_sta, rt2860_iter_func, arg);
1316 
1317 	sc->sc_rssadapt_id = timeout(rt2860_updatestats, (void *)sc,
1318 	    drv_usectohz(500 * 1000));
1319 }
1320 
1321 static void
1322 rt2860_enable_mrr(struct rt2860_softc *sc)
1323 {
1324 #define	CCK(mcs)	(mcs)
1325 #define	OFDM(mcs)	((uint32_t)1 << 3 | (mcs))
1326 	RT2860_WRITE(sc, RT2860_LG_FBK_CFG0,
1327 	    OFDM(6) << 28 |	/* 54->48 */
1328 	    OFDM(5) << 24 |	/* 48->36 */
1329 	    OFDM(4) << 20 |	/* 36->24 */
1330 	    OFDM(3) << 16 |	/* 24->18 */
1331 	    OFDM(2) << 12 |	/* 18->12 */
1332 	    OFDM(1) <<  8 |	/* 12-> 9 */
1333 	    OFDM(0) <<  4 |	/*  9-> 6 */
1334 	    OFDM(0));		/*  6-> 6 */
1335 
1336 	RT2860_WRITE(sc, RT2860_LG_FBK_CFG1,
1337 	    CCK(2) << 12 |	/* 11->5.5 */
1338 	    CCK(1) <<  8 |	/* 5.5-> 2 */
1339 	    CCK(0) <<  4 |	/*   2-> 1 */
1340 	    CCK(0));		/*   1-> 1 */
1341 #undef OFDM
1342 #undef CCK
1343 }
1344 
1345 static void
1346 rt2860_set_txpreamble(struct rt2860_softc *sc)
1347 {
1348 	uint32_t tmp;
1349 
1350 	tmp = RT2860_READ(sc, RT2860_AUTO_RSP_CFG);
1351 	tmp &= ~RT2860_CCK_SHORT_EN;
1352 	if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1353 		tmp |= RT2860_CCK_SHORT_EN;
1354 	RT2860_WRITE(sc, RT2860_AUTO_RSP_CFG, tmp);
1355 }
1356 
1357 static void
1358 rt2860_set_bssid(struct rt2860_softc *sc, const uint8_t *bssid)
1359 {
1360 	RT2860_WRITE(sc, RT2860_MAC_BSSID_DW0,
1361 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
1362 	RT2860_WRITE(sc, RT2860_MAC_BSSID_DW1,
1363 	    bssid[4] | bssid[5] << 8);
1364 }
1365 
1366 static void
1367 rt2860_set_basicrates(struct rt2860_softc *sc)
1368 {
1369 	struct ieee80211com *ic = &sc->sc_ic;
1370 
1371 	/* set basic rates mask */
1372 	if (ic->ic_curmode == IEEE80211_MODE_11B)
1373 		RT2860_WRITE(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
1374 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
1375 		RT2860_WRITE(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
1376 	else	/* 11g */
1377 		RT2860_WRITE(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
1378 }
1379 
1380 static void
1381 rt2860_amrr_node_init(const struct rt2860_amrr *amrr,
1382     struct rt2860_amrr_node *amn)
1383 {
1384 	amn->amn_success = 0;
1385 	amn->amn_recovery = 0;
1386 	amn->amn_txcnt = amn->amn_retrycnt = 0;
1387 	amn->amn_success_threshold = amrr->amrr_min_success_threshold;
1388 }
1389 
1390 static void
1391 rt2860_amrr_choose(struct rt2860_amrr *amrr, struct ieee80211_node *ni,
1392     struct rt2860_amrr_node *amn)
1393 {
1394 #define	RV(rate)	((rate) & IEEE80211_RATE_VAL)
1395 #define	is_success(amn)	\
1396 	((amn)->amn_retrycnt < (amn)->amn_txcnt / 10)
1397 #define	is_failure(amn)	\
1398 	((amn)->amn_retrycnt > (amn)->amn_txcnt / 3)
1399 #define	is_enough(amn)		\
1400 	((amn)->amn_txcnt > 10)
1401 #define	is_min_rate(ni)		\
1402 	((ni)->in_txrate == 0)
1403 #define	is_max_rate(ni)		\
1404 	((ni)->in_txrate == (ni)->in_rates.ir_nrates - 1)
1405 #define	increase_rate(ni)	\
1406 	((ni)->in_txrate++)
1407 #define	decrease_rate(ni)	\
1408 	((ni)->in_txrate--)
1409 #define	reset_cnt(amn)		\
1410 	{ (amn)->amn_txcnt = (amn)->amn_retrycnt = 0; }
1411 
1412 	int need_change = 0;
1413 
1414 	if (is_success(amn) && is_enough(amn)) {
1415 		amn->amn_success++;
1416 		if (amn->amn_success >= amn->amn_success_threshold &&
1417 		    !is_max_rate(ni)) {
1418 			amn->amn_recovery = 1;
1419 			amn->amn_success = 0;
1420 			increase_rate(ni);
1421 			RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_amrr_choose(): "
1422 			    "increase rate = %d, #tx = %d, #retries = %d\n",
1423 			    RV(ni->in_rates.ir_rates[ni->in_txrate]),
1424 			    amn->amn_txcnt, amn->amn_retrycnt);
1425 			need_change = 1;
1426 		} else {
1427 			amn->amn_recovery = 0;
1428 		}
1429 	} else if (is_failure(amn)) {
1430 		amn->amn_success = 0;
1431 		if (!is_min_rate(ni)) {
1432 			if (amn->amn_recovery) {
1433 				amn->amn_success_threshold *= 2;
1434 				if (amn->amn_success_threshold >
1435 				    amrr->amrr_max_success_threshold)
1436 					amn->amn_success_threshold =
1437 					    amrr->amrr_max_success_threshold;
1438 			} else {
1439 				amn->amn_success_threshold =
1440 				    amrr->amrr_min_success_threshold;
1441 			}
1442 			decrease_rate(ni);
1443 			RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_amrr_choose(): "
1444 			    "decrease rate = %d, #tx = %d, #retries = %d\n",
1445 			    RV(ni->in_rates.ir_rates[ni->in_txrate]),
1446 			    amn->amn_txcnt, amn->amn_retrycnt);
1447 			need_change = 1;
1448 		}
1449 		amn->amn_recovery = 0;
1450 	}
1451 
1452 	if (is_enough(amn) || need_change)
1453 		reset_cnt(amn);
1454 #undef RV
1455 }
1456 
1457 static void
1458 rt2860_newassoc(struct ieee80211com *ic, struct ieee80211_node *in, int isnew)
1459 {
1460 	struct rt2860_softc *sc = (struct rt2860_softc *)ic;
1461 	uint32_t off;
1462 	uint8_t *fptr, wcid = 0;
1463 	int i;
1464 
1465 	if (isnew && in->in_associd != 0) {
1466 		/* only interested in true associations */
1467 		wcid = RT2860_AID2WCID(in->in_associd);
1468 
1469 		/* init WCID table entry */
1470 		off = RT2860_WCID_ENTRY(wcid);
1471 		fptr = in->in_macaddr;
1472 		for (i = 0; i < IEEE80211_ADDR_LEN; i++)
1473 			rt2860_mem_write1(sc, off++, *fptr++);
1474 	}
1475 	rt2860_amrr_node_init(&sc->amrr, &sc->amn[wcid]);
1476 
1477 	/* set rate to some reasonable initial value */
1478 	i = in->in_rates.ir_nrates - 1;
1479 	for (; i > 0 && (in->in_rates.ir_rates[i] & IEEE80211_RATE_VAL) > 72; )
1480 		i--;
1481 	in->in_txrate = i;
1482 
1483 	RWN_DEBUG(RT2860_DBG_80211, "rwn: rt2860_newassoc(): "
1484 	    "new assoc isnew=%d WCID=%d, initial rate=%d\n",
1485 	    isnew, wcid,
1486 	    in->in_rates.ir_rates[i] & IEEE80211_RATE_VAL);
1487 	RWN_DEBUG(RT2860_DBG_80211, "rwn: rt2860_newassoc(): "
1488 	    "addr=%x:%x:%x:%x:%x:%x\n",
1489 	    in->in_macaddr[0], in->in_macaddr[1], in->in_macaddr[2],
1490 	    in->in_macaddr[3], in->in_macaddr[4], in->in_macaddr[5]);
1491 }
1492 
1493 void
1494 rt2860_enable_tsf_sync(struct rt2860_softc *sc)
1495 {
1496 	struct ieee80211com *ic = &sc->sc_ic;
1497 	uint32_t tmp;
1498 
1499 	tmp = RT2860_READ(sc, RT2860_BCN_TIME_CFG);
1500 
1501 	tmp &= ~0x1fffff;
1502 	tmp |= ic->ic_bss->in_intval * 16;
1503 	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
1504 	if (ic->ic_opmode == IEEE80211_M_STA) {
1505 		/*
1506 		 * Local TSF is always updated with remote TSF on beacon
1507 		 * reception.
1508 		 */
1509 		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
1510 	}
1511 
1512 	RT2860_WRITE(sc, RT2860_BCN_TIME_CFG, tmp);
1513 }
1514 
1515 static int
1516 rt2860_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1517 {
1518 	struct rt2860_softc	*sc = (struct rt2860_softc *)ic;
1519 	enum ieee80211_state	ostate;
1520 	int			err;
1521 	uint32_t		tmp;
1522 
1523 	ostate = ic->ic_state;
1524 	RWN_DEBUG(RT2860_DBG_80211, "rwn: rt2860_newstate(): "
1525 	    "%x -> %x!\n", ostate, nstate);
1526 
1527 	RT2860_GLOCK(sc);
1528 	if (sc->sc_scan_id != 0) {
1529 		(void) untimeout(sc->sc_scan_id);
1530 		sc->sc_scan_id = 0;
1531 	}
1532 	if (sc->sc_rssadapt_id != 0) {
1533 		(void) untimeout(sc->sc_rssadapt_id);
1534 		sc->sc_rssadapt_id = 0;
1535 	}
1536 	if (ostate == IEEE80211_S_RUN) {
1537 		/* turn link LED off */
1538 		rt2860_set_leds(sc, RT2860_LED_RADIO);
1539 	}
1540 
1541 	switch (nstate) {
1542 	case IEEE80211_S_INIT:
1543 		if (ostate == IEEE80211_S_RUN) {
1544 			/* abort TSF synchronization */
1545 			tmp = RT2860_READ(sc, RT2860_BCN_TIME_CFG);
1546 			RT2860_WRITE(sc, RT2860_BCN_TIME_CFG,
1547 			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1548 			    RT2860_TBTT_TIMER_EN));
1549 		}
1550 		break;
1551 
1552 	case IEEE80211_S_SCAN:
1553 		rt2860_set_chan(sc, ic->ic_curchan);
1554 		sc->sc_scan_id = timeout(rt2860_next_scan, (void *)sc,
1555 		    drv_usectohz(200000));
1556 		break;
1557 
1558 	case IEEE80211_S_AUTH:
1559 	case IEEE80211_S_ASSOC:
1560 		rt2860_set_chan(sc, ic->ic_curchan);
1561 		break;
1562 
1563 	case IEEE80211_S_RUN:
1564 		rt2860_set_chan(sc, ic->ic_curchan);
1565 
1566 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1567 			rt2860_updateslot(sc);
1568 			rt2860_enable_mrr(sc);
1569 			rt2860_set_txpreamble(sc);
1570 			rt2860_set_basicrates(sc);
1571 			rt2860_set_bssid(sc, ic->ic_bss->in_bssid);
1572 		}
1573 		if (ic->ic_opmode == IEEE80211_M_STA) {
1574 			/* fake a join to init the tx rate */
1575 			rt2860_newassoc(ic, ic->ic_bss, 1);
1576 		}
1577 
1578 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1579 			rt2860_enable_tsf_sync(sc);
1580 			sc->sc_rssadapt_id = timeout(rt2860_updatestats,
1581 			    (void *)sc, drv_usectohz(500 * 1000));
1582 		}
1583 
1584 		/* turn link LED on */
1585 		rt2860_set_leds(sc, RT2860_LED_RADIO |
1586 		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1587 		    RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1588 		break;
1589 	}
1590 
1591 	RT2860_GUNLOCK(sc);
1592 
1593 	err = sc->sc_newstate(ic, nstate, arg);
1594 
1595 	return (err);
1596 }
1597 
1598 /*
1599  * Return the Rx chain with the highest RSSI for a given frame.
1600  */
1601 static uint8_t
1602 rt2860_maxrssi_chain(struct rt2860_softc *sc, const struct rt2860_rxwi *rxwi)
1603 {
1604 	uint8_t rxchain = 0;
1605 
1606 	if (sc->nrxchains > 1)
1607 		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
1608 			rxchain = 1;
1609 	if (sc->nrxchains > 2)
1610 		if (rxwi->rssi[2] > rxwi->rssi[rxchain])
1611 			rxchain = 2;
1612 
1613 	return (rxchain);
1614 }
1615 
1616 static void
1617 rt2860_drain_stats_fifo(struct rt2860_softc *sc)
1618 {
1619 	struct rt2860_amrr_node *amn;
1620 	uint32_t stat;
1621 	uint8_t wcid, mcs, pid;
1622 
1623 	/* drain Tx status FIFO (maxsize = 16) */
1624 	while ((stat = RT2860_READ(sc, RT2860_TX_STAT_FIFO)) & RT2860_TXQ_VLD) {
1625 		RWN_DEBUG(RT2860_DBG_TX, "rwn: rt2860_drain_stats_fifo(): "
1626 		    "tx stat 0x%08\n", stat);
1627 
1628 		wcid = (stat >> 8) & 0xff;
1629 
1630 		/* if no ACK was requested, no feedback is available */
1631 		if (!(stat & RT2860_TXQ_ACKREQ) || wcid == 0xff)
1632 			continue;
1633 		/* update per-STA AMRR stats */
1634 		amn = &sc->amn[wcid];
1635 		amn->amn_txcnt++;
1636 		if (stat & RT2860_TXQ_OK) {
1637 			/*
1638 			 * Check if there were retries, ie if the Tx success
1639 			 * rate is different from the requested rate.  Note
1640 			 * that it works only because we do not allow rate
1641 			 * fallback from OFDM to CCK.
1642 			 */
1643 			mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
1644 			pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
1645 			if (mcs + 1 != pid)
1646 				amn->amn_retrycnt++;
1647 		} else
1648 			amn->amn_retrycnt++;
1649 	}
1650 }
1651 
1652 /*ARGSUSED*/
1653 static void
1654 rt2860_tx_intr(struct rt2860_softc *sc, int qid)
1655 {
1656 	struct rt2860_tx_ring	*ring = &sc->txq[qid];
1657 	struct ieee80211com	*ic = &sc->sc_ic;
1658 	uint32_t hw;
1659 
1660 	rt2860_drain_stats_fifo(sc);
1661 
1662 	mutex_enter(&sc->sc_txlock);
1663 	hw = RT2860_READ(sc, RT2860_TX_DTX_IDX(qid));
1664 	RWN_DEBUG(RT2860_DBG_TX, "rwn: rwn_tx_intr():"
1665 	    "hw = %x, ring->next = %x, queued = %d\n",
1666 	    hw, ring->next, ring->queued);
1667 	while (ring->next != hw) {
1668 		struct rt2860_txd *txd = &ring->txd[ring->next];
1669 		struct rt2860_tx_data *data = ring->data[ring->next];
1670 
1671 		if (data != NULL) {
1672 			RT2860_DMA_SYNC(data->txbuf_dma, DDI_DMA_SYNC_FORDEV);
1673 			if (data->ni != NULL) {
1674 				ieee80211_free_node(data->ni);
1675 				data->ni = NULL;
1676 			}
1677 			SLIST_INSERT_HEAD(&sc->data_pool, data, next);
1678 			ring->data[ring->next] = NULL;
1679 		}
1680 
1681 		txd->sdl0 &= ~LE_16(RT2860_TX_DDONE);
1682 
1683 		(void) ddi_dma_sync(ring->txdesc_dma.dma_hdl,
1684 		    ring->next * sizeof (struct rt2860_txd),
1685 		    sizeof (struct rt2860_txd),
1686 		    DDI_DMA_SYNC_FORDEV);
1687 
1688 		ring->queued--;
1689 		ring->next = (ring->next + 1) % RT2860_TX_RING_COUNT;
1690 
1691 		if (sc->sc_need_sched &&
1692 		    (ring->queued < RT2860_TX_RING_COUNT)) {
1693 			sc->sc_need_sched = 0;
1694 			mac_tx_update(ic->ic_mach);
1695 		}
1696 	}
1697 	sc->sc_tx_timer = 0;
1698 	mutex_exit(&sc->sc_txlock);
1699 }
1700 
1701 static void
1702 rt2860_rx_intr(struct rt2860_softc *sc)
1703 {
1704 	struct ieee80211com	*ic = &sc->sc_ic;
1705 	struct ieee80211_node	*ni;
1706 	struct ieee80211_frame	*wh;
1707 	int	pktlen;
1708 	uint8_t ant, rssi, *rxbuf;
1709 	mblk_t	*mp0;
1710 
1711 	mutex_enter(&sc->sc_rxlock);
1712 	for (;;) {
1713 		struct rt2860_rx_data *data = &sc->rxq.data[sc->rxq.cur];
1714 		struct rt2860_rxd *rxd = &sc->rxq.rxd[sc->rxq.cur];
1715 		struct rt2860_rxwi *rxwi;
1716 
1717 		(void) ddi_dma_sync(sc->rxq.rxdesc_dma.dma_hdl,
1718 		    sc->rxq.cur * sizeof (struct rt2860_rxd),
1719 		    sizeof (struct rt2860_rxd),
1720 		    DDI_DMA_SYNC_FORKERNEL);
1721 
1722 		if (!(rxd->sdl0 & LE_16(RT2860_RX_DDONE))) {
1723 			RWN_DEBUG(RT2860_DBG_RX, "rwn: rt2860_rx_intr(): "
1724 			    "rx done!\n");
1725 			break;
1726 		}
1727 
1728 		if (rxd->flags &
1729 		    LE_32(RT2860_RX_CRCERR | RT2860_RX_ICVERR)) {
1730 			RWN_DEBUG(RT2860_DBG_RX, "rwn: rt2860_rx_intr(): "
1731 			    "rx crc error & rx icv error!\n");
1732 			sc->sc_rx_err++;
1733 			goto skip;
1734 		}
1735 
1736 		if (rxd->flags & LE_32(RT2860_RX_MICERR)) {
1737 			RWN_DEBUG(RT2860_DBG_RX, "rwn: rt2860_rx_intr(): "
1738 			    "rx mic error!\n");
1739 			sc->sc_rx_err++;
1740 			goto skip;
1741 		}
1742 
1743 		(void) ddi_dma_sync(data->rxbuf_dma.dma_hdl,
1744 		    data->rxbuf_dma.offset,
1745 		    data->rxbuf_dma.alength,
1746 		    DDI_DMA_SYNC_FORCPU);
1747 
1748 		rxbuf = (uint8_t *)data->rxbuf_dma.mem_va;
1749 		rxd->sdp0 = LE_32(data->rxbuf_dma.cookie.dmac_address);
1750 		rxwi = (struct rt2860_rxwi *)rxbuf;
1751 		rxbuf = (uint8_t *)(rxwi + 1);
1752 		pktlen = LE_16(rxwi->len) & 0xfff;
1753 
1754 		mp0 = allocb(sc->sc_dmabuf_size, BPRI_MED);
1755 		if (mp0 == NULL) {
1756 			RWN_DEBUG(RT2860_DBG_RX, "rwn: rt2860_rx_intr():"
1757 			    "alloc mblk error\n");
1758 			sc->sc_rx_nobuf++;
1759 			goto skip;
1760 		}
1761 		bcopy(rxbuf, mp0->b_rptr, pktlen);
1762 		mp0->b_wptr += pktlen;
1763 
1764 		wh = (struct ieee80211_frame *)mp0->b_rptr;
1765 
1766 		/* HW may insert 2 padding bytes after 802.11 header */
1767 		if (rxd->flags & LE_32(RT2860_RX_L2PAD)) {
1768 			RWN_DEBUG(RT2860_DBG_RX, "rwn: rt2860_rx_intr():"
1769 			    "2 padding bytes after 80211 header!\n");
1770 		}
1771 
1772 		ant = rt2860_maxrssi_chain(sc, rxwi);
1773 		rssi = RT2860_RSSI_OFFSET - rxwi->rssi[ant];
1774 		/* grab a reference to the source node */
1775 		ni = ieee80211_find_rxnode(ic, wh);
1776 
1777 		ieee80211_input(ic, mp0, ni, rssi, 0);
1778 
1779 		/* node is no longer needed */
1780 		ieee80211_free_node(ni);
1781 skip:
1782 		rxd->sdl0 &= ~LE_16(RT2860_RX_DDONE);
1783 
1784 		(void) ddi_dma_sync(sc->rxq.rxdesc_dma.dma_hdl,
1785 		    sc->rxq.cur * sizeof (struct rt2860_rxd),
1786 		    sizeof (struct rt2860_rxd),
1787 		    DDI_DMA_SYNC_FORDEV);
1788 
1789 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2860_RX_RING_COUNT;
1790 	}
1791 	mutex_exit(&sc->sc_rxlock);
1792 
1793 	/* tell HW what we have processed */
1794 	RT2860_WRITE(sc, RT2860_RX_CALC_IDX,
1795 	    (sc->rxq.cur - 1) % RT2860_RX_RING_COUNT);
1796 }
1797 
1798 static uint_t
1799 rt2860_softintr(caddr_t data)
1800 {
1801 	struct rt2860_softc *sc = (struct rt2860_softc *)data;
1802 
1803 	/*
1804 	 * Check if the soft interrupt is triggered by another
1805 	 * driver at the same level.
1806 	 */
1807 	RT2860_GLOCK(sc);
1808 	if (sc->sc_rx_pend) {
1809 		sc->sc_rx_pend = 0;
1810 		RT2860_GUNLOCK(sc);
1811 		rt2860_rx_intr(sc);
1812 		return (DDI_INTR_CLAIMED);
1813 	}
1814 	RT2860_GUNLOCK(sc);
1815 
1816 	return (DDI_INTR_UNCLAIMED);
1817 }
1818 
1819 static uint_t
1820 rt2860_intr(caddr_t arg)
1821 {
1822 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
1823 	uint32_t		r;
1824 
1825 	RT2860_GLOCK(sc);
1826 
1827 	if ((!RT2860_IS_RUNNING(sc)) || RT2860_IS_SUSPEND(sc)) {
1828 		/*
1829 		 * The hardware is not ready/present, don't touch anything.
1830 		 * Note this can happen early on if the IRQ is shared.
1831 		 */
1832 		RT2860_GUNLOCK(sc);
1833 		return (DDI_INTR_UNCLAIMED);
1834 	}
1835 
1836 	r = RT2860_READ(sc, RT2860_INT_STATUS);
1837 	if (r == 0xffffffff) {
1838 		RT2860_GUNLOCK(sc);
1839 		return (DDI_INTR_UNCLAIMED);
1840 	}
1841 	if (r == 0) {
1842 		RT2860_GUNLOCK(sc);
1843 		return (DDI_INTR_UNCLAIMED);
1844 	}
1845 
1846 	/* acknowledge interrupts */
1847 	RT2860_WRITE(sc, RT2860_INT_STATUS, r);
1848 
1849 	if (r & RT2860_TX_COHERENT)
1850 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr()"
1851 		    "RT2860_TX_COHERENT\n");
1852 
1853 	if (r & RT2860_RX_COHERENT)
1854 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr()"
1855 		    "RT2860_RX_COHERENT\n");
1856 
1857 	if (r & RT2860_MAC_INT_2) {
1858 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1859 		    "RT2860_MAC_INT_2\n");
1860 		rt2860_drain_stats_fifo(sc);
1861 	}
1862 
1863 	if (r & RT2860_TX_DONE_INT5) {
1864 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1865 		    "RT2860_TX_DONE_INT5\n");
1866 		rt2860_tx_intr(sc, 5);
1867 	}
1868 
1869 	if (r & RT2860_RX_DONE_INT) {
1870 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr()"
1871 		    "RT2860_RX_INT\n");
1872 		sc->sc_rx_pend = 1;
1873 		ddi_trigger_softintr(sc->sc_softintr_hdl);
1874 	}
1875 
1876 	if (r & RT2860_TX_DONE_INT4) {
1877 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1878 		    "RT2860_TX_DONE_INT4\n");
1879 		rt2860_tx_intr(sc, 4);
1880 	}
1881 
1882 	if (r & RT2860_TX_DONE_INT3) {
1883 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1884 		    "RT2860_TX_DONE_INT3\n");
1885 		rt2860_tx_intr(sc, 3);
1886 	}
1887 
1888 	if (r & RT2860_TX_DONE_INT2) {
1889 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1890 		    "RT2860_TX_DONE_INT2\n");
1891 		rt2860_tx_intr(sc, 2);
1892 	}
1893 
1894 	if (r & RT2860_TX_DONE_INT1) {
1895 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1896 		    "RT2860_TX_DONE_INT1\n");
1897 		rt2860_tx_intr(sc, 1);
1898 	}
1899 
1900 	if (r & RT2860_TX_DONE_INT0) {
1901 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1902 		    "RT2860_TX_DONE_INT0\n");
1903 		rt2860_tx_intr(sc, 0);
1904 	}
1905 
1906 	if (r & RT2860_MAC_INT_0) {
1907 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1908 		    "RT2860_MAC_INT_0\n");
1909 		struct ieee80211com *ic = &sc->sc_ic;
1910 		/* check if protection mode has changed */
1911 		if ((sc->sc_ic_flags ^ ic->ic_flags) & IEEE80211_F_USEPROT) {
1912 			rt2860_updateprot(ic);
1913 			sc->sc_ic_flags = ic->ic_flags;
1914 		}
1915 	}
1916 
1917 	if (r & RT2860_MAC_INT_3)
1918 		RWN_DEBUG(RT2860_DBG_INTR, "rwn: rt2860_intr(): "
1919 		    "RT2860_MAC_INT_3\n");
1920 
1921 	RT2860_GUNLOCK(sc);
1922 
1923 	return (DDI_INTR_CLAIMED);
1924 }
1925 
1926 static void
1927 rt2860_set_region_4(struct rt2860_softc *sc,
1928     uint32_t addr, uint32_t data, int size)
1929 {
1930 	for (; size > 0; size--, data++, addr += 4)
1931 		ddi_put32((sc)->sc_io_handle,
1932 		    (uint32_t *)((uintptr_t)(sc)->sc_io_base + addr), data);
1933 }
1934 
1935 static int
1936 rt2860_load_microcode(struct rt2860_softc *sc)
1937 {
1938 	int		ntries;
1939 	size_t		size;
1940 	uint8_t		*ucode, *fptr;
1941 	uint32_t	off, i;
1942 
1943 	ucode = rt2860_fw_bin;
1944 	size = sizeof (rt2860_fw_bin);
1945 	RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_load_microcode(): "
1946 	    "The size of ucode is: %x\n", size);
1947 
1948 	/* set "host program ram write selection" bit */
1949 	RT2860_WRITE(sc, RT2860_SYS_CTRL, RT2860_HST_PM_SEL);
1950 	/* write microcode image */
1951 	fptr = ucode;
1952 	off = RT2860_FW_BASE;
1953 	for (i = 0; i < size; i++) {
1954 		rt2860_mem_write1(sc, off++, *fptr++);
1955 	}
1956 	/* kick microcontroller unit */
1957 	RT2860_WRITE(sc, RT2860_SYS_CTRL, 0);
1958 	RT2860_WRITE(sc, RT2860_SYS_CTRL, RT2860_MCU_RESET);
1959 
1960 	RT2860_WRITE(sc, RT2860_H2M_BBPAGENT, 0);
1961 	RT2860_WRITE(sc, RT2860_H2M_MAILBOX, 0);
1962 
1963 	/* wait until microcontroller is ready */
1964 	for (ntries = 0; ntries < 1000; ntries++) {
1965 		if (RT2860_READ(sc, RT2860_SYS_CTRL) & RT2860_MCU_READY)
1966 			break;
1967 		DELAY(1000);
1968 	}
1969 	if (ntries == 1000) {
1970 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_load_microcode(): "
1971 		    "timeout waiting for MCU to initialie\n");
1972 		return (ETIMEDOUT);
1973 	}
1974 
1975 	return (0);
1976 }
1977 
1978 static void
1979 rt2860_set_macaddr(struct rt2860_softc *sc, const uint8_t *addr)
1980 {
1981 	RT2860_WRITE(sc, RT2860_MAC_ADDR_DW0,
1982 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
1983 	RT2860_WRITE(sc, RT2860_MAC_ADDR_DW1,
1984 	    addr[4] | addr[5] << 8);
1985 }
1986 
1987 /*
1988  * Send a command to the 8051 microcontroller unit.
1989  */
1990 static int
1991 rt2860_mcu_cmd(struct rt2860_softc *sc, uint8_t cmd, uint16_t arg)
1992 {
1993 	int	ntries;
1994 
1995 	for (ntries = 0; ntries < 100; ntries++) {
1996 		if (!(RT2860_READ(sc, RT2860_H2M_MAILBOX) & RT2860_H2M_BUSY))
1997 			break;
1998 		DELAY(2);
1999 	}
2000 	if (ntries == 100)
2001 		return (EIO);
2002 
2003 	RT2860_WRITE(sc, RT2860_H2M_MAILBOX,
2004 	    RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg);
2005 	RT2860_WRITE(sc, RT2860_HOST_CMD, cmd);
2006 
2007 	return (RT2860_SUCCESS);
2008 }
2009 
2010 /*
2011  * Reading and writing from/to the BBP is different from RT2560 and RT2661.
2012  * We access the BBP through the 8051 microcontroller unit which means that
2013  * the microcode must be loaded first.
2014  */
2015 static uint8_t
2016 rt2860_mcu_bbp_read(struct rt2860_softc *sc, uint8_t reg)
2017 {
2018 	uint32_t val;
2019 	int ntries;
2020 
2021 	for (ntries = 0; ntries < 100; ntries++) {
2022 		if (!(RT2860_READ(sc,
2023 		    RT2860_H2M_BBPAGENT) & RT2860_BBP_CSR_KICK))
2024 			break;
2025 		DELAY(1);
2026 	}
2027 	if (ntries == 100) {
2028 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_mcu_bbp_read():"
2029 		    "could not read from BBP through MCU\n");
2030 		return (0);
2031 	}
2032 
2033 	RT2860_WRITE(sc, RT2860_H2M_BBPAGENT, RT2860_BBP_RW_PARALLEL |
2034 	    RT2860_BBP_CSR_KICK | RT2860_BBP_CSR_READ | reg << 8);
2035 
2036 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_BBP, 0);
2037 	DELAY(1000);
2038 
2039 	for (ntries = 0; ntries < 100; ntries++) {
2040 		val = RT2860_READ(sc, RT2860_H2M_BBPAGENT);
2041 		if (!(val & RT2860_BBP_CSR_KICK))
2042 			return (val & 0xff);
2043 		DELAY(1);
2044 	}
2045 	RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_mcu_bbp_read():"
2046 	    "could not read from BBP through MCU\n");
2047 
2048 	return (0);
2049 }
2050 
2051 static void
2052 rt2860_mcu_bbp_write(struct rt2860_softc *sc, uint8_t reg, uint8_t val)
2053 {
2054 	int ntries;
2055 
2056 	for (ntries = 0; ntries < 100; ntries++) {
2057 		if (!(RT2860_READ(sc,
2058 		    RT2860_H2M_BBPAGENT) & RT2860_BBP_CSR_KICK))
2059 			break;
2060 		DELAY(1);
2061 	}
2062 	if (ntries == 100) {
2063 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_mcu_bbp_write():"
2064 		    "could not write to BBP through MCU\n");
2065 		return;
2066 	}
2067 
2068 	RT2860_WRITE(sc, RT2860_H2M_BBPAGENT, RT2860_BBP_RW_PARALLEL |
2069 	    RT2860_BBP_CSR_KICK | reg << 8 | val);
2070 
2071 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_BBP, 0);
2072 	DELAY(1000);
2073 }
2074 
2075 static int
2076 rt2860_bbp_init(struct rt2860_softc *sc)
2077 {
2078 	int i, ntries;
2079 
2080 	/* wait for BBP to wake up */
2081 	for (ntries = 0; ntries < 20; ntries++) {
2082 		uint8_t bbp0 = rt2860_mcu_bbp_read(sc, 0);
2083 		if (bbp0 != 0 && bbp0 != 0xff)
2084 			break;
2085 	}
2086 	if (ntries == 20) {
2087 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_bbp_init():"
2088 		    "timeout waiting for BBP to wake up\n");
2089 		return (ETIMEDOUT);
2090 	}
2091 
2092 	/* initialize BBP registers to default values */
2093 	for (i = 0; i < 12; i++) {
2094 		rt2860_mcu_bbp_write(sc, rt2860_def_bbp[i].reg,
2095 		    rt2860_def_bbp[i].val);
2096 	}
2097 
2098 	/* fix BBP69 and BBP73 for RT2860C */
2099 	if (sc->mac_rev == 0x28600100) {
2100 		rt2860_mcu_bbp_write(sc, 69, 0x16);
2101 		rt2860_mcu_bbp_write(sc, 73, 0x12);
2102 	}
2103 
2104 	return (0);
2105 }
2106 
2107 static void
2108 rt2860_rf_write(struct rt2860_softc *sc, uint8_t reg, uint32_t val)
2109 {
2110 	uint32_t tmp;
2111 	int ntries;
2112 
2113 	for (ntries = 0; ntries < 100; ntries++) {
2114 		if (!(RT2860_READ(sc, RT2860_RF_CSR_CFG0) & RT2860_RF_REG_CTRL))
2115 			break;
2116 		DELAY(1);
2117 	}
2118 	if (ntries == 100) {
2119 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rwn_init()"
2120 		    "could not write to RF\n");
2121 		return;
2122 	}
2123 
2124 	/* RF registers are 24-bit on the RT2860 */
2125 	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
2126 	    (val & 0x3fffff) << 2 | (reg & 3);
2127 	RT2860_WRITE(sc, RT2860_RF_CSR_CFG0, tmp);
2128 }
2129 
2130 static void
2131 rt2860_select_chan_group(struct rt2860_softc *sc, int group)
2132 {
2133 	uint32_t tmp;
2134 
2135 	rt2860_mcu_bbp_write(sc, 62, 0x37 - sc->lna[group]);
2136 	rt2860_mcu_bbp_write(sc, 63, 0x37 - sc->lna[group]);
2137 	rt2860_mcu_bbp_write(sc, 64, 0x37 - sc->lna[group]);
2138 	rt2860_mcu_bbp_write(sc, 82, (group == 0) ? 0x62 : 0xf2);
2139 
2140 	tmp = RT2860_READ(sc, RT2860_TX_BAND_CFG);
2141 	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
2142 	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
2143 	RT2860_WRITE(sc, RT2860_TX_BAND_CFG, tmp);
2144 
2145 	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
2146 	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN;
2147 	if (group == 0) {	/* 2GHz */
2148 		tmp |= RT2860_PA_PE_G0_EN | RT2860_LNA_PE_G0_EN;
2149 		if (sc->ntxchains > 1)
2150 			tmp |= RT2860_PA_PE_G1_EN;
2151 		if (sc->nrxchains > 1)
2152 			tmp |= RT2860_LNA_PE_G1_EN;
2153 	} else {		/* 5GHz */
2154 		tmp |= RT2860_PA_PE_A0_EN | RT2860_LNA_PE_A0_EN;
2155 		if (sc->ntxchains > 1)
2156 			tmp |= RT2860_PA_PE_A1_EN;
2157 		if (sc->nrxchains > 1)
2158 			tmp |= RT2860_LNA_PE_A1_EN;
2159 	}
2160 	RT2860_WRITE(sc, RT2860_TX_PIN_CFG, tmp);
2161 
2162 	rt2860_mcu_bbp_write(sc, 66, 0x2e + sc->lna[group]);
2163 }
2164 static void
2165 rt2860_set_chan(struct rt2860_softc *sc, struct ieee80211_channel *c)
2166 {
2167 	struct ieee80211com	*ic = &sc->sc_ic;
2168 	const struct rfprog	*rfprog = rt2860_rf2850;
2169 	uint_t			i, chan, group;
2170 	uint8_t			txpow1, txpow2;
2171 	uint32_t		r2, r3, r4;
2172 
2173 	chan = ieee80211_chan2ieee(ic, c);
2174 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2175 		RWN_DEBUG(RT2860_DBG_FW, "Unkonwn channel!\n");
2176 		return;
2177 	}
2178 
2179 	/* find the settings for this channel (we know it exists) */
2180 	for (i = 0; rfprog[i].chan != chan; )
2181 		i++;
2182 
2183 	r2 = rfprog[i].r2;
2184 	if (sc->ntxchains == 1)
2185 		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
2186 	if (sc->nrxchains == 1)
2187 		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
2188 	else if (sc->nrxchains == 2)
2189 		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
2190 
2191 	/* use Tx power values from EEPROM */
2192 	txpow1 = sc->txpow1[i];
2193 	txpow2 = sc->txpow2[i];
2194 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2195 		txpow1 = txpow1 << 1 | 1;
2196 		txpow2 = txpow2 << 1 | 1;
2197 	}
2198 	r3 = rfprog[i].r3 | txpow1 << 7;
2199 	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
2200 
2201 	rt2860_rf_write(sc, RAL_RF1, rfprog[i].r1);
2202 	rt2860_rf_write(sc, RAL_RF2, r2);
2203 	rt2860_rf_write(sc, RAL_RF3, r3);
2204 	rt2860_rf_write(sc, RAL_RF4, r4);
2205 
2206 	DELAY(200);
2207 
2208 	rt2860_rf_write(sc, RAL_RF1, rfprog[i].r1);
2209 	rt2860_rf_write(sc, RAL_RF2, r2);
2210 	rt2860_rf_write(sc, RAL_RF3, r3 | 1);
2211 	rt2860_rf_write(sc, RAL_RF4, r4);
2212 
2213 	DELAY(200);
2214 
2215 	rt2860_rf_write(sc, RAL_RF1, rfprog[i].r1);
2216 	rt2860_rf_write(sc, RAL_RF2, r2);
2217 	rt2860_rf_write(sc, RAL_RF3, r3);
2218 	rt2860_rf_write(sc, RAL_RF4, r4);
2219 
2220 	/* 802.11a uses a 16 microseconds short interframe space */
2221 	sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
2222 
2223 	/* determine channel group */
2224 	if (chan <= 14)
2225 		group = 0;
2226 	else if (chan <= 64)
2227 		group = 1;
2228 	else if (chan <= 128)
2229 		group = 2;
2230 	else
2231 		group = 3;
2232 
2233 	/* XXX necessary only when group has changed! */
2234 	rt2860_select_chan_group(sc, group);
2235 
2236 	DELAY(1000);
2237 }
2238 
2239 static void
2240 rt2860_updateprot(struct ieee80211com *ic)
2241 {
2242 	struct rt2860_softc *sc = (struct rt2860_softc *)ic;
2243 	uint32_t tmp;
2244 
2245 	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
2246 	/* setup protection frame rate (MCS code) */
2247 	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ? 0 : 3;
2248 
2249 	/* CCK frames don't require protection */
2250 	RT2860_WRITE(sc, RT2860_CCK_PROT_CFG, tmp);
2251 
2252 	if (ic->ic_flags & IEEE80211_F_USEPROT) {
2253 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2254 			tmp |= RT2860_PROT_CTRL_RTS_CTS;
2255 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2256 			tmp |= RT2860_PROT_CTRL_CTS;
2257 	}
2258 	RT2860_WRITE(sc, RT2860_OFDM_PROT_CFG, tmp);
2259 }
2260 
2261 static void
2262 rt2860_set_leds(struct rt2860_softc *sc, uint16_t which)
2263 {
2264 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
2265 	    which | (sc->leds & 0x7f));
2266 }
2267 
2268 static int
2269 rt2860_init(struct rt2860_softc *sc)
2270 {
2271 #define	N(a)	(sizeof (a) / sizeof ((a)[0]))
2272 	struct ieee80211com	*ic;
2273 	int			i, err, qid, ridx, ntries;
2274 	uint8_t			bbp1, bbp3;
2275 	uint32_t		tmp;
2276 
2277 	ic = &sc->sc_ic;
2278 
2279 	rt2860_stop(sc);
2280 	tmp = RT2860_READ(sc, RT2860_WPDMA_GLO_CFG);
2281 	tmp &= 0xff0;
2282 	RT2860_WRITE(sc, RT2860_WPDMA_GLO_CFG, tmp | RT2860_TX_WB_DDONE);
2283 
2284 	RT2860_WRITE(sc, RT2860_WPDMA_RST_IDX, 0xffffffff);
2285 
2286 	/* PBF hardware reset */
2287 	RT2860_WRITE(sc, RT2860_SYS_CTRL, 0xe1f);
2288 	RT2860_WRITE(sc, RT2860_SYS_CTRL, 0xe00);
2289 
2290 	if (!(sc->sc_flags & RT2860_FWLOADED)) {
2291 		if ((err = rt2860_load_microcode(sc)) != 0) {
2292 			RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_init(): "
2293 			    "could not load 8051 microcode\n");
2294 			rt2860_stop(sc);
2295 			return (err);
2296 		}
2297 		RT2860_GLOCK(sc);
2298 		sc->sc_flags |= RT2860_FWLOADED;
2299 		RT2860_GUNLOCK(sc);
2300 	}
2301 
2302 	rt2860_set_macaddr(sc, ic->ic_macaddr);
2303 
2304 	/* init Tx power for all Tx rates (from EEPROM) */
2305 	for (ridx = 0; ridx < 5; ridx++) {
2306 		if (sc->txpow20mhz[ridx] == 0xffffffff)
2307 			continue;
2308 		RT2860_WRITE(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
2309 	}
2310 
2311 	for (ntries = 0; ntries < 100; ntries++) {
2312 		tmp = RT2860_READ(sc, RT2860_WPDMA_GLO_CFG);
2313 		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
2314 			break;
2315 		DELAY(1000);
2316 	}
2317 	if (ntries == 100) {
2318 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_init():"
2319 		    "timeout waiting for DMA engine\n");
2320 		rt2860_stop(sc);
2321 		return (ETIMEDOUT);
2322 	}
2323 	tmp &= 0xff0;
2324 	RT2860_WRITE(sc, RT2860_WPDMA_GLO_CFG, tmp | RT2860_TX_WB_DDONE);
2325 
2326 	/* reset Rx ring and all 6 Tx rings */
2327 	RT2860_WRITE(sc, RT2860_WPDMA_RST_IDX, 0x1003f);
2328 
2329 	/* PBF hardware reset */
2330 	RT2860_WRITE(sc, RT2860_SYS_CTRL, 0xe1f);
2331 	RT2860_WRITE(sc, RT2860_SYS_CTRL, 0xe00);
2332 
2333 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL,
2334 	    RT2860_BBP_HRST | RT2860_MAC_SRST);
2335 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL, 0);
2336 
2337 	for (i = 0; i < N(rt2860_def_mac); i++)
2338 		RT2860_WRITE(sc, rt2860_def_mac[i].reg, rt2860_def_mac[i].val);
2339 
2340 	/* wait while MAC is busy */
2341 	for (ntries = 0; ntries < 100; ntries++) {
2342 		if (!(RT2860_READ(sc, RT2860_MAC_STATUS_REG) &
2343 		    (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
2344 			break;
2345 		DELAY(1000);
2346 	}
2347 	if (ntries == 100) {
2348 		RWN_DEBUG(RT2860_DBG_FW, "rwn: rt2860_init():"
2349 		    "timeout waiting for MAC\n");
2350 		rt2860_stop(sc);
2351 		return (ETIMEDOUT);
2352 	}
2353 
2354 	/* clear Host to MCU mailbox */
2355 	RT2860_WRITE(sc, RT2860_H2M_BBPAGENT, 0);
2356 	RT2860_WRITE(sc, RT2860_H2M_MAILBOX, 0);
2357 
2358 	if ((err = rt2860_bbp_init(sc)) != 0) {
2359 		rt2860_stop(sc);
2360 		return (err);
2361 	}
2362 
2363 	/* init Tx rings (4 EDCAs + HCCA + Mgt) */
2364 	for (qid = 0; qid < 6; qid++) {
2365 		RT2860_WRITE(sc, RT2860_TX_BASE_PTR(qid), sc->txq[qid].paddr);
2366 		RT2860_WRITE(sc, RT2860_TX_MAX_CNT(qid), RT2860_TX_RING_COUNT);
2367 		RT2860_WRITE(sc, RT2860_TX_CTX_IDX(qid), 0);
2368 	}
2369 
2370 	/* init Rx ring */
2371 	RT2860_WRITE(sc, RT2860_RX_BASE_PTR, sc->rxq.paddr);
2372 	RT2860_WRITE(sc, RT2860_RX_MAX_CNT, RT2860_RX_RING_COUNT);
2373 	RT2860_WRITE(sc, RT2860_RX_CALC_IDX, RT2860_RX_RING_COUNT - 1);
2374 
2375 	/* setup maximum buffer sizes */
2376 	RT2860_WRITE(sc, RT2860_MAX_LEN_CFG, 1 << 12 |
2377 	    (sc->sc_dmabuf_size - sizeof (struct rt2860_rxwi) - 2));
2378 
2379 	for (ntries = 0; ntries < 100; ntries++) {
2380 		tmp = RT2860_READ(sc, RT2860_WPDMA_GLO_CFG);
2381 		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
2382 			break;
2383 		DELAY(1000);
2384 	}
2385 	if (ntries == 100) {
2386 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_init():"
2387 		    "timeout waiting for DMA engine\n");
2388 		rt2860_stop(sc);
2389 		return (ETIMEDOUT);
2390 	}
2391 	tmp &= 0xff0;
2392 	RT2860_WRITE(sc, RT2860_WPDMA_GLO_CFG, tmp | RT2860_TX_WB_DDONE);
2393 
2394 	/* disable interrupts mitigation */
2395 	RT2860_WRITE(sc, RT2860_DELAY_INT_CFG, 0);
2396 
2397 	/* write vendor-specific BBP values (from EEPROM) */
2398 	for (i = 0; i < 8; i++) {
2399 		if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
2400 			continue;
2401 		rt2860_mcu_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
2402 	}
2403 
2404 	/* send LEDs operating mode to microcontroller */
2405 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
2406 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
2407 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
2408 
2409 	/* disable non-existing Rx chains */
2410 	bbp3 = rt2860_mcu_bbp_read(sc, 3);
2411 	bbp3 &= ~(1 << 3 | 1 << 4);
2412 	if (sc->nrxchains == 2)
2413 		bbp3 |= 1 << 3;
2414 	else if (sc->nrxchains == 3)
2415 		bbp3 |= 1 << 4;
2416 	rt2860_mcu_bbp_write(sc, 3, bbp3);
2417 
2418 	/* disable non-existing Tx chains */
2419 	bbp1 = rt2860_mcu_bbp_read(sc, 1);
2420 	if (sc->ntxchains == 1)
2421 		bbp1 &= ~(1 << 3 | 1 << 4);
2422 	rt2860_mcu_bbp_write(sc, 1, bbp1);
2423 
2424 	/* select default channel */
2425 	rt2860_set_chan(sc, ic->ic_curchan);
2426 
2427 	/* XXX not clear what the following 8051 command does.. */
2428 	(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_BOOT, 0);
2429 
2430 	/* set RTS threshold */
2431 	tmp = RT2860_READ(sc, RT2860_TX_RTS_CFG);
2432 	tmp &= ~0xffff00;
2433 	tmp |= ic->ic_rtsthreshold << 8;
2434 
2435 	/* setup initial protection mode */
2436 	sc->sc_ic_flags = ic->ic_flags;
2437 	rt2860_updateprot(ic);
2438 
2439 	/* enable Tx/Rx DMA engine */
2440 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
2441 	for (ntries = 0; ntries < 200; ntries++) {
2442 		tmp = RT2860_READ(sc, RT2860_WPDMA_GLO_CFG);
2443 		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
2444 			break;
2445 		DELAY(1000);
2446 	}
2447 	if (ntries == 200) {
2448 		RWN_DEBUG(RT2860_DBG_DMA, "rwn: rt2860_int():"
2449 		    "timeout waiting for DMA engine\n");
2450 		rt2860_stop(sc);
2451 		return (ETIMEDOUT);
2452 	}
2453 
2454 	DELAY(50);
2455 
2456 	tmp |= RT2860_TX_WB_DDONE | RT2860_RX_DMA_EN | RT2860_TX_DMA_EN |
2457 	    RT2860_WPDMA_BT_SIZE64 << RT2860_WPDMA_BT_SIZE_SHIFT;
2458 	RT2860_WRITE(sc, RT2860_WPDMA_GLO_CFG, tmp);
2459 
2460 	/* turn radio LED on */
2461 	rt2860_set_leds(sc, RT2860_LED_RADIO);
2462 
2463 	/* set Rx filter */
2464 	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
2465 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2466 		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
2467 		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
2468 		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
2469 		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
2470 		if (ic->ic_opmode == IEEE80211_M_STA)
2471 			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
2472 	}
2473 	RT2860_WRITE(sc, RT2860_RX_FILTR_CFG, tmp);
2474 
2475 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL,
2476 	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
2477 
2478 	/* clear pending interrupts */
2479 	RT2860_WRITE(sc, RT2860_INT_STATUS, 0xffffffff);
2480 	/* enable interrupts */
2481 	RT2860_WRITE(sc, RT2860_INT_MASK, 0x3fffc);
2482 
2483 	if (sc->sc_flags & RT2860_ADVANCED_PS)
2484 		(void) rt2860_mcu_cmd(sc, RT2860_MCU_CMD_PSLEVEL, sc->pslevel);
2485 
2486 	return (DDI_SUCCESS);
2487 }
2488 
2489 static int
2490 rt2860_quiesce(dev_info_t *dip)
2491 {
2492 	struct rt2860_softc	*sc;
2493 
2494 	sc = ddi_get_soft_state(rt2860_soft_state_p, ddi_get_instance(dip));
2495 	if (sc == NULL)
2496 		return (DDI_FAILURE);
2497 
2498 #ifdef DEBUG
2499 	rt2860_dbg_flags = 0;
2500 #endif
2501 
2502 	/*
2503 	 * No more blocking is allowed while we are in quiesce(9E) entry point
2504 	 */
2505 	sc->sc_flags |= RT2860_F_QUIESCE;
2506 
2507 	/*
2508 	 * Disable and mask all interrupts
2509 	 */
2510 	rt2860_stop(sc);
2511 	return (DDI_SUCCESS);
2512 }
2513 
2514 static void
2515 rt2860_stop(struct rt2860_softc *sc)
2516 {
2517 	int		qid;
2518 	uint32_t	tmp;
2519 
2520 	/* by pass if it's quiesced */
2521 	if (!(sc->sc_flags & RT2860_F_QUIESCE))
2522 		RT2860_GLOCK(sc);
2523 	if (sc->sc_flags == RT2860_F_RUNNING)
2524 		rt2860_set_leds(sc, 0);	/* turn all LEDs off */
2525 	sc->sc_tx_timer = 0;
2526 	/* by pass if it's quiesced */
2527 	if (!(sc->sc_flags & RT2860_F_QUIESCE))
2528 		RT2860_GUNLOCK(sc);
2529 
2530 	/* clear RX WCID search table */
2531 	rt2860_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
2532 	/* clear pairwise key table */
2533 	rt2860_set_region_4(sc, RT2860_PKEY(0), 0, 2048);
2534 	/* clear IV/EIV table */
2535 	rt2860_set_region_4(sc, RT2860_IVEIV(0), 0, 512);
2536 	/* clear WCID attribute table */
2537 	rt2860_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 256);
2538 	/* clear shared key table */
2539 	rt2860_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
2540 	/* clear shared key mode */
2541 	rt2860_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
2542 
2543 	/* disable interrupts */
2544 	RT2860_WRITE(sc, RT2860_INT_MASK, 0);
2545 
2546 	/* disable Rx */
2547 	tmp = RT2860_READ(sc, RT2860_MAC_SYS_CTRL);
2548 	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
2549 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL, tmp);
2550 
2551 	/* reset adapter */
2552 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL,
2553 	    RT2860_BBP_HRST | RT2860_MAC_SRST);
2554 	RT2860_WRITE(sc, RT2860_MAC_SYS_CTRL, 0);
2555 
2556 	/* reset Tx and Rx rings (and reclaim TXWIs) */
2557 	for (qid = 0; qid < 6; qid++)
2558 		rt2860_reset_tx_ring(sc, &sc->txq[qid]);
2559 	rt2860_reset_rx_ring(sc, &sc->rxq);
2560 
2561 	/* by pass if it's quiesced */
2562 	if (!(sc->sc_flags & RT2860_F_QUIESCE))
2563 		RT2860_GLOCK(sc);
2564 	sc->sc_flags &= ~RT2860_UPD_BEACON;
2565 	/* by pass if it's quiesced */
2566 	if (!(sc->sc_flags & RT2860_F_QUIESCE))
2567 		RT2860_GUNLOCK(sc);
2568 }
2569 
2570 static int
2571 rt2860_m_start(void *arg)
2572 {
2573 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
2574 	struct ieee80211com	*ic = &sc->sc_ic;
2575 	int			err;
2576 
2577 	err = rt2860_init(sc);
2578 	if (err != DDI_SUCCESS) {
2579 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_m_start():"
2580 		    "Hardware initialization failed\n");
2581 		goto fail1;
2582 	}
2583 
2584 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2585 
2586 	RT2860_GLOCK(sc);
2587 	sc->sc_flags |= RT2860_F_RUNNING;
2588 	RT2860_GUNLOCK(sc);
2589 
2590 	return (err);
2591 fail1:
2592 	rt2860_stop(sc);
2593 	return (err);
2594 }
2595 
2596 static void
2597 rt2860_m_stop(void *arg)
2598 {
2599 	struct rt2860_softc *sc = (struct rt2860_softc *)arg;
2600 
2601 	(void) rt2860_stop(sc);
2602 
2603 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
2604 
2605 	RT2860_GLOCK(sc);
2606 	sc->sc_flags &= ~RT2860_F_RUNNING;
2607 	RT2860_GUNLOCK(sc);
2608 }
2609 
2610 static void
2611 rt2860_m_ioctl(void* arg, queue_t *wq, mblk_t *mp)
2612 {
2613 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
2614 	struct ieee80211com	*ic = &sc->sc_ic;
2615 	int			err;
2616 
2617 	err = ieee80211_ioctl(ic, wq, mp);
2618 	RT2860_GLOCK(sc);
2619 	if (err == ENETRESET) {
2620 		if (ic->ic_des_esslen) {
2621 			if (RT2860_IS_RUNNING(sc)) {
2622 				RT2860_GUNLOCK(sc);
2623 				(void) rt2860_init(sc);
2624 				(void) ieee80211_new_state(ic,
2625 				    IEEE80211_S_SCAN, -1);
2626 				RT2860_GLOCK(sc);
2627 			}
2628 		}
2629 	}
2630 	RT2860_GUNLOCK(sc);
2631 }
2632 
2633 /*
2634  * Call back function for get/set proporty
2635  */
2636 static int
2637 rt2860_m_getprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_num,
2638     uint_t pr_flags, uint_t wldp_length, void *wldp_buf, uint_t *perm)
2639 {
2640 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
2641 	int			err = 0;
2642 
2643 	err = ieee80211_getprop(&sc->sc_ic, pr_name, wldp_pr_num,
2644 	    pr_flags, wldp_length, wldp_buf, perm);
2645 
2646 	return (err);
2647 }
2648 
2649 static int
2650 rt2860_m_setprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_num,
2651     uint_t wldp_length, const void *wldp_buf)
2652 {
2653 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
2654 	ieee80211com_t		*ic = &sc->sc_ic;
2655 	int			err;
2656 
2657 	err = ieee80211_setprop(ic, pr_name, wldp_pr_num, wldp_length,
2658 	    wldp_buf);
2659 	RT2860_GLOCK(sc);
2660 	if (err == ENETRESET) {
2661 		if (ic->ic_des_esslen) {
2662 			if (RT2860_IS_RUNNING(sc)) {
2663 				RT2860_GUNLOCK(sc);
2664 				(void) rt2860_init(sc);
2665 				(void) ieee80211_new_state(ic,
2666 				    IEEE80211_S_SCAN, -1);
2667 				RT2860_GLOCK(sc);
2668 			}
2669 		}
2670 		err = 0;
2671 	}
2672 	RT2860_GUNLOCK(sc);
2673 	return (err);
2674 }
2675 
2676 static mblk_t *
2677 rt2860_m_tx(void *arg, mblk_t *mp)
2678 {
2679 	struct rt2860_softc	*sc = (struct rt2860_softc *)arg;
2680 	struct ieee80211com	*ic = &sc->sc_ic;
2681 	mblk_t			*next;
2682 
2683 	if (RT2860_IS_SUSPEND(sc)) {
2684 		freemsgchain(mp);
2685 		return (NULL);
2686 	}
2687 
2688 	/*
2689 	 * No data frames go out unless we're associated; this
2690 	 * should not happen as the 802.11 layer does not enable
2691 	 * the xmit queue until we enter the RUN state.
2692 	 */
2693 	if (ic->ic_state != IEEE80211_S_RUN) {
2694 		RWN_DEBUG(RT2860_DBG_TX, "rwn: rt2860_tx_data(): "
2695 		    "discard, state %u\n", ic->ic_state);
2696 		freemsgchain(mp);
2697 		return (NULL);
2698 	}
2699 
2700 	while (mp != NULL) {
2701 		next = mp->b_next;
2702 		mp->b_next = NULL;
2703 		if (rt2860_send(ic, mp, IEEE80211_FC0_TYPE_DATA) !=
2704 		    DDI_SUCCESS) {
2705 			mp->b_next = next;
2706 			break;
2707 		}
2708 		mp = next;
2709 	}
2710 	return (mp);
2711 }
2712 
2713 /*ARGSUSED*/
2714 static int
2715 rt2860_m_unicst(void *arg, const uint8_t *macaddr)
2716 {
2717 	return (ENOTSUP);
2718 }
2719 
2720 /*ARGSUSED*/
2721 static int
2722 rt2860_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
2723 {
2724 	return (ENOTSUP);
2725 }
2726 
2727 /*ARGSUSED*/
2728 static int
2729 rt2860_m_promisc(void *arg, boolean_t on)
2730 {
2731 	return (0);
2732 }
2733 
2734 static int
2735 rt2860_m_stat(void *arg, uint_t stat, uint64_t *val)
2736 {
2737 	struct rt2860_softc	*sc  = (struct rt2860_softc *)arg;
2738 	ieee80211com_t		*ic = &sc->sc_ic;
2739 	ieee80211_node_t	*ni = ic->ic_bss;
2740 	struct ieee80211_rateset *rs = &ni->in_rates;
2741 
2742 	RT2860_GLOCK(sc);
2743 	switch (stat) {
2744 	case MAC_STAT_IFSPEED:
2745 		*val = ((ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) ?
2746 		    (rs->ir_rates[ni->in_txrate] & IEEE80211_RATE_VAL)
2747 		    : ic->ic_fixed_rate) / 2 * 1000000;
2748 		break;
2749 	case MAC_STAT_NOXMTBUF:
2750 		*val = sc->sc_tx_nobuf;
2751 		break;
2752 	case MAC_STAT_NORCVBUF:
2753 		*val = sc->sc_rx_nobuf;
2754 		break;
2755 	case MAC_STAT_IERRORS:
2756 		*val = sc->sc_rx_err;
2757 		break;
2758 	case MAC_STAT_RBYTES:
2759 		*val = ic->ic_stats.is_rx_bytes;
2760 		break;
2761 	case MAC_STAT_IPACKETS:
2762 		*val = ic->ic_stats.is_rx_frags;
2763 		break;
2764 	case MAC_STAT_OBYTES:
2765 		*val = ic->ic_stats.is_tx_bytes;
2766 		break;
2767 	case MAC_STAT_OPACKETS:
2768 		*val = ic->ic_stats.is_tx_frags;
2769 		break;
2770 	case MAC_STAT_OERRORS:
2771 	case WIFI_STAT_TX_FAILED:
2772 		*val = sc->sc_tx_err;
2773 		break;
2774 	case WIFI_STAT_TX_RETRANS:
2775 		*val = sc->sc_tx_retries;
2776 		break;
2777 	case WIFI_STAT_FCS_ERRORS:
2778 	case WIFI_STAT_WEP_ERRORS:
2779 	case WIFI_STAT_TX_FRAGS:
2780 	case WIFI_STAT_MCAST_TX:
2781 	case WIFI_STAT_RTS_SUCCESS:
2782 	case WIFI_STAT_RTS_FAILURE:
2783 	case WIFI_STAT_ACK_FAILURE:
2784 	case WIFI_STAT_RX_FRAGS:
2785 	case WIFI_STAT_MCAST_RX:
2786 	case WIFI_STAT_RX_DUPS:
2787 		RT2860_GUNLOCK(sc);
2788 		return (ieee80211_stat(ic, stat, val));
2789 	default:
2790 		RT2860_GUNLOCK(sc);
2791 		return (ENOTSUP);
2792 	}
2793 	RT2860_GUNLOCK(sc);
2794 
2795 	return (0);
2796 }
2797 
2798 static int
2799 rt2860_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2800 {
2801 	struct rt2860_softc	*sc;
2802 	struct ieee80211com	*ic;
2803 	int			i, err, qid, ntries, instance;
2804 	uint8_t			cachelsz;
2805 	uint16_t		command, vendor_id, device_id;
2806 	char			strbuf[32];
2807 	wifi_data_t		wd = { 0 };
2808 	mac_register_t		*macp;
2809 
2810 	switch (cmd) {
2811 	case DDI_ATTACH:
2812 		break;
2813 	case DDI_RESUME:
2814 		sc = ddi_get_soft_state(rt2860_soft_state_p,
2815 		    ddi_get_instance(devinfo));
2816 		ASSERT(sc != NULL);
2817 		RT2860_GLOCK(sc);
2818 		sc->sc_flags &= ~RT2860_F_SUSPEND;
2819 		RT2860_GUNLOCK(sc);
2820 		if (RT2860_IS_RUNNING(sc))
2821 			(void) rt2860_init(sc);
2822 		RWN_DEBUG(RT2860_DBG_RESUME, "rwn: rt2860_attach(): "
2823 		    "resume now\n");
2824 		return (DDI_SUCCESS);
2825 	default:
2826 		return (DDI_FAILURE);
2827 	}
2828 
2829 	instance = ddi_get_instance(devinfo);
2830 
2831 	err = ddi_soft_state_zalloc(rt2860_soft_state_p, instance);
2832 	if (err != DDI_SUCCESS) {
2833 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2834 		    "unable to alloc soft_state_p\n");
2835 		return (err);
2836 	}
2837 
2838 	sc = ddi_get_soft_state(rt2860_soft_state_p, instance);
2839 	ic = (ieee80211com_t *)&sc->sc_ic;
2840 	sc->sc_dev = devinfo;
2841 
2842 	/* pci configuration */
2843 	err = ddi_regs_map_setup(devinfo, 0, &sc->sc_cfg_base, 0, 0,
2844 	    &rwn_csr_accattr, &sc->sc_cfg_handle);
2845 	if (err != DDI_SUCCESS) {
2846 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2847 		    "ddi_regs_map_setup() failed");
2848 		goto fail1;
2849 	}
2850 
2851 	cachelsz = ddi_get8(sc->sc_cfg_handle,
2852 	    (uint8_t *)(sc->sc_cfg_base + PCI_CONF_CACHE_LINESZ));
2853 	if (cachelsz == 0)
2854 		cachelsz = 0x10;
2855 	sc->sc_cachelsz = cachelsz << 2;
2856 	sc->sc_dmabuf_size = roundup(IEEE80211_MAX_LEN, sc->sc_cachelsz);
2857 
2858 	vendor_id = ddi_get16(sc->sc_cfg_handle,
2859 	    (uint16_t *)((uintptr_t)(sc->sc_cfg_base) + PCI_CONF_VENID));
2860 	device_id = ddi_get16(sc->sc_cfg_handle,
2861 	    (uint16_t *)((uintptr_t)(sc->sc_cfg_base) + PCI_CONF_DEVID));
2862 	RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2863 	    "vendor 0x%x, device id 0x%x, cache size %d\n",
2864 	    vendor_id, device_id, cachelsz);
2865 
2866 	/*
2867 	 * Enable response to memory space accesses,
2868 	 * and enabe bus master.
2869 	 */
2870 	command = PCI_COMM_MAE | PCI_COMM_ME;
2871 	ddi_put16(sc->sc_cfg_handle,
2872 	    (uint16_t *)((uintptr_t)(sc->sc_cfg_base) + PCI_CONF_COMM),
2873 	    command);
2874 	ddi_put8(sc->sc_cfg_handle,
2875 	    (uint8_t *)(sc->sc_cfg_base + PCI_CONF_LATENCY_TIMER), 0xa8);
2876 	ddi_put8(sc->sc_cfg_handle,
2877 	    (uint8_t *)(sc->sc_cfg_base + PCI_CONF_ILINE), 0x10);
2878 
2879 	/* pci i/o space */
2880 	err = ddi_regs_map_setup(devinfo, 1,
2881 	    &sc->sc_io_base, 0, 0, &rwn_csr_accattr, &sc->sc_io_handle);
2882 	if (err != DDI_SUCCESS) {
2883 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2884 		    "ddi_regs_map_setup() failed");
2885 		goto fail2;
2886 	}
2887 	RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2888 	    "PCI configuration is done successfully\n");
2889 
2890 	sc->amrr.amrr_min_success_threshold =  1;
2891 	sc->amrr.amrr_max_success_threshold = 15;
2892 
2893 	/* wait for NIC to initialize */
2894 	for (ntries = 0; ntries < 100; ntries++) {
2895 		sc->mac_rev = RT2860_READ(sc, RT2860_ASIC_VER_ID);
2896 		if (sc->mac_rev != 0 && sc->mac_rev != 0xffffffff)
2897 			break;
2898 		DELAY(10);
2899 	}
2900 	if (ntries == 100) {
2901 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2902 		    "timeout waiting for NIC initialize\n");
2903 		return (DDI_FAILURE);
2904 	}
2905 
2906 	if ((sc->mac_rev >> 16) != 0x2860 &&
2907 	    (device_id == PRODUCT_RALINK_RT2890 ||
2908 	    device_id == PRODUCT_RALINK_RT2790 ||
2909 	    device_id == PRODUCT_AWT_RT2890))
2910 		sc->sc_flags |= RT2860_ADVANCED_PS;
2911 
2912 	/* retrieve RF rev. no and various other things from EEPROM */
2913 	(void) rt2860_read_eeprom(sc);
2914 	RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2915 	    "MAC/BBP RT%X (rev 0x%04X), RF %s (%dT%dR)\n",
2916 	    sc->mac_rev >> 16, sc->mac_rev & 0xffff,
2917 	    rt2860_get_rf(sc->rf_rev), sc->ntxchains, sc->nrxchains);
2918 
2919 	/*
2920 	 * Allocate Tx (4 EDCAs + HCCA + Mgt) and Rx rings.
2921 	 */
2922 	for (qid = 0; qid < 6; qid++) {
2923 		if ((err = rt2860_alloc_tx_ring(sc, &sc->txq[qid])) != 0) {
2924 			RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2925 			    "could not allocate Tx ring %d\n", qid);
2926 			goto fail3;
2927 		}
2928 	}
2929 
2930 	if ((err = rt2860_alloc_rx_ring(sc, &sc->rxq)) != 0) {
2931 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2932 		    "could not allocte Rx ring\n");
2933 		goto fail4;
2934 	}
2935 
2936 	if ((err = rt2860_alloc_tx_pool(sc)) != 0) {
2937 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2938 		    "could not allocte Tx pool\n");
2939 		goto fail5;
2940 	}
2941 
2942 	mutex_init(&sc->sc_genlock, NULL, MUTEX_DRIVER, NULL);
2943 	mutex_init(&sc->sc_txlock, NULL, MUTEX_DRIVER, NULL);
2944 	mutex_init(&sc->sc_rxlock, NULL, MUTEX_DRIVER, NULL);
2945 
2946 	/* mgmt ring is broken on RT2860C, use EDCA AC VO ring instead */
2947 	sc->mgtqid = (sc->mac_rev == 0x28600100) ? EDCA_AC_VO : 5;
2948 	RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach():"
2949 	    "mgtqid = %x\n", sc->mgtqid);
2950 
2951 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
2952 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
2953 	ic->ic_state = IEEE80211_S_INIT;
2954 
2955 	/* set device capabilities */
2956 	ic->ic_caps =
2957 	    IEEE80211_C_TXPMGT |	/* tx power management */
2958 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
2959 	    IEEE80211_C_SHSLOT;		/* short slot time supported */
2960 
2961 	/* WPA/WPA2 support */
2962 	ic->ic_caps |= IEEE80211_C_WPA; /* Support WPA/WPA2 */
2963 
2964 	/* set supported .11b and .11g rates */
2965 	ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2860_rateset_11b;
2966 	ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2860_rateset_11g;
2967 
2968 	/* set supported .11b and .11g channels (1 through 14) */
2969 	for (i = 1; i <= 14; i++) {
2970 		ic->ic_sup_channels[i].ich_freq =
2971 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
2972 		ic->ic_sup_channels[i].ich_flags =
2973 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
2974 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
2975 	}
2976 
2977 	ic->ic_maxrssi = 63;
2978 	ic->ic_xmit = rt2860_send;
2979 
2980 	ieee80211_attach(ic);
2981 
2982 	/* register WPA door */
2983 	ieee80211_register_door(ic, ddi_driver_name(devinfo),
2984 	    ddi_get_instance(devinfo));
2985 
2986 	/* override state transition machine */
2987 	sc->sc_newstate = ic->ic_newstate;
2988 	ic->ic_newstate = rt2860_newstate;
2989 	ieee80211_media_init(ic);
2990 	ic->ic_def_txkey = 0;
2991 
2992 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW,
2993 	    &sc->sc_softintr_hdl, NULL, 0, rt2860_softintr, (caddr_t)sc);
2994 	if (err != DDI_SUCCESS) {
2995 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
2996 		    "ddi_add_softintr() failed");
2997 		goto fail8;
2998 	}
2999 
3000 	err = ddi_get_iblock_cookie(devinfo, 0, &sc->sc_iblock);
3001 	if (err != DDI_SUCCESS) {
3002 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
3003 		    "Can not get iblock cookie for INT\n");
3004 		goto fail7;
3005 	}
3006 
3007 	err = ddi_add_intr(devinfo, 0, NULL, NULL, rt2860_intr, (caddr_t)sc);
3008 	if (err != DDI_SUCCESS) {
3009 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
3010 		    "unable to add device interrupt handler\n");
3011 		goto fail7;
3012 	}
3013 
3014 	/*
3015 	 * Provide initial settings for the WiFi plugin; whenever this
3016 	 * information changes, we need to call mac_plugindata_update()
3017 	 */
3018 	wd.wd_opmode = ic->ic_opmode;
3019 	wd.wd_secalloc = WIFI_SEC_NONE;
3020 	IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_bss->in_bssid);
3021 
3022 	if ((macp = mac_alloc(MAC_VERSION)) == NULL) {
3023 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
3024 		    "MAC version mismatch\n");
3025 		goto fail9;
3026 	}
3027 
3028 	macp->m_type_ident	= MAC_PLUGIN_IDENT_WIFI;
3029 	macp->m_driver		= sc;
3030 	macp->m_dip		= devinfo;
3031 	macp->m_src_addr	= ic->ic_macaddr;
3032 	macp->m_callbacks	= &rt2860_m_callbacks;
3033 	macp->m_min_sdu		= 0;
3034 	macp->m_max_sdu		= IEEE80211_MTU;
3035 	macp->m_pdata		= &wd;
3036 	macp->m_pdata_size	= sizeof (wd);
3037 
3038 	err = mac_register(macp, &ic->ic_mach);
3039 	mac_free(macp);
3040 	if (err != 0) {
3041 		RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach(): "
3042 		    "mac_register err %x\n", err);
3043 		goto fail9;
3044 	}
3045 
3046 	/*
3047 	 * Create minor node of type DDI_NT_NET_WIFI
3048 	 */
3049 	(void) snprintf(strbuf, sizeof (strbuf), "%s%d",
3050 	    "rwn", instance);
3051 	err = ddi_create_minor_node(devinfo, strbuf, S_IFCHR,
3052 	    instance + 1, DDI_NT_NET_WIFI, 0);
3053 
3054 	/*
3055 	 * Notify link is down now
3056 	 */
3057 	mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
3058 
3059 	sc->sc_flags &= ~RT2860_F_RUNNING;
3060 
3061 	RWN_DEBUG(RT2860_DBG_MSG, "rwn: rt2860_attach() successfully.\n");
3062 	return (DDI_SUCCESS);
3063 fail9:
3064 	ddi_remove_softintr(sc->sc_softintr_hdl);
3065 fail8:
3066 	ddi_remove_intr(devinfo, 0, sc->sc_iblock);
3067 fail7:
3068 	mutex_destroy(&sc->sc_genlock);
3069 	mutex_destroy(&sc->sc_txlock);
3070 	mutex_destroy(&sc->sc_rxlock);
3071 fail6:
3072 	rt2860_free_tx_pool(sc);
3073 fail5:
3074 	rt2860_free_rx_ring(sc, &sc->rxq);
3075 fail4:
3076 	while (--qid >= 0)
3077 		rt2860_free_tx_ring(sc, &sc->txq[qid]);
3078 fail3:
3079 	ddi_regs_map_free(&sc->sc_io_handle);
3080 fail2:
3081 	ddi_regs_map_free(&sc->sc_cfg_handle);
3082 fail1:
3083 	return (err);
3084 }
3085 
3086 static int
3087 rt2860_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
3088 {
3089 	struct rt2860_softc	*sc;
3090 	int			qid;
3091 
3092 	sc = ddi_get_soft_state(rt2860_soft_state_p, ddi_get_instance(devinfo));
3093 
3094 	switch (cmd) {
3095 	case DDI_DETACH:
3096 		break;
3097 	case DDI_SUSPEND:
3098 		if (RT2860_IS_RUNNING(sc))
3099 			rt2860_stop(sc);
3100 		RT2860_GLOCK(sc);
3101 		sc->sc_flags &= ~RT2860_FWLOADED;
3102 		sc->sc_flags |= RT2860_F_SUSPEND;
3103 		RT2860_GUNLOCK(sc);
3104 		RWN_DEBUG(RT2860_DBG_RESUME, "rwn: rt2860_detach(): "
3105 		    "suspend now\n");
3106 		return (DDI_SUCCESS);
3107 	default:
3108 		return (DDI_FAILURE);
3109 	}
3110 
3111 	if (mac_disable(sc->sc_ic.ic_mach) != 0)
3112 		return (DDI_FAILURE);
3113 
3114 	rt2860_stop(sc);
3115 
3116 	/*
3117 	 * Unregister from the MAC layer subsystem
3118 	 */
3119 	(void) mac_unregister(sc->sc_ic.ic_mach);
3120 
3121 	ddi_remove_intr(devinfo, 0, sc->sc_iblock);
3122 	ddi_remove_softintr(sc->sc_softintr_hdl);
3123 
3124 	/*
3125 	 * detach ieee80211 layer
3126 	 */
3127 	ieee80211_detach(&sc->sc_ic);
3128 
3129 	rt2860_free_tx_pool(sc);
3130 	rt2860_free_rx_ring(sc, &sc->rxq);
3131 	for (qid = 0; qid < 6; qid++)
3132 		rt2860_free_tx_ring(sc, &sc->txq[qid]);
3133 
3134 	ddi_regs_map_free(&sc->sc_io_handle);
3135 
3136 	mutex_destroy(&sc->sc_genlock);
3137 	mutex_destroy(&sc->sc_txlock);
3138 	mutex_destroy(&sc->sc_rxlock);
3139 
3140 	ddi_remove_minor_node(devinfo, NULL);
3141 	ddi_soft_state_free(rt2860_soft_state_p, ddi_get_instance(devinfo));
3142 
3143 	return (DDI_SUCCESS);
3144 }
3145 
3146 int
3147 _info(struct modinfo *modinfop)
3148 {
3149 	return (mod_info(&modlinkage, modinfop));
3150 }
3151 
3152 int
3153 _init(void)
3154 {
3155 	int status;
3156 
3157 	status = ddi_soft_state_init(&rt2860_soft_state_p,
3158 	    sizeof (struct rt2860_softc), 1);
3159 	if (status != 0)
3160 		return (status);
3161 
3162 	mac_init_ops(&rwn_dev_ops, "rwn");
3163 	status = mod_install(&modlinkage);
3164 	if (status != 0) {
3165 		mac_fini_ops(&rwn_dev_ops);
3166 		ddi_soft_state_fini(&rt2860_soft_state_p);
3167 	}
3168 	return (status);
3169 }
3170 
3171 int
3172 _fini(void)
3173 {
3174 	int status;
3175 
3176 	status = mod_remove(&modlinkage);
3177 	if (status == 0) {
3178 		mac_fini_ops(&rwn_dev_ops);
3179 		ddi_soft_state_fini(&rt2860_soft_state_p);
3180 	}
3181 	return (status);
3182 }
3183