xref: /freebsd/sys/dev/ral/rt2560.c (revision 2227a3e9e1a0bcba8481a8067ee8c4b9a96fdda3)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 /*-
24  * Ralink Technology RT2560 chipset driver
25  * http://www.ralinktech.com/
26  */
27 
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
31 #include <sys/mbuf.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/endian.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45 
46 #include <net/bpf.h>
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 
54 #include <net80211/ieee80211_var.h>
55 #include <net80211/ieee80211_phy.h>
56 #include <net80211/ieee80211_radiotap.h>
57 #include <net80211/ieee80211_regdomain.h>
58 #include <net80211/ieee80211_amrr.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/if_ether.h>
65 
66 #include <dev/ral/rt2560reg.h>
67 #include <dev/ral/rt2560var.h>
68 
69 #define RT2560_RSSI(sc, rssi)					\
70 	((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ?	\
71 	 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
72 
73 #define RAL_DEBUG
74 #ifdef RAL_DEBUG
75 #define DPRINTF(sc, fmt, ...) do {				\
76 	if (sc->sc_debug > 0)					\
77 		printf(fmt, __VA_ARGS__);			\
78 } while (0)
79 #define DPRINTFN(sc, n, fmt, ...) do {				\
80 	if (sc->sc_debug >= (n))				\
81 		printf(fmt, __VA_ARGS__);			\
82 } while (0)
83 #else
84 #define DPRINTF(sc, fmt, ...)
85 #define DPRINTFN(sc, n, fmt, ...)
86 #endif
87 
88 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *,
89 			    const char name[IFNAMSIZ], int unit, int opmode,
90 			    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
91 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
92 static void		rt2560_vap_delete(struct ieee80211vap *);
93 static void		rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
94 			    int);
95 static int		rt2560_alloc_tx_ring(struct rt2560_softc *,
96 			    struct rt2560_tx_ring *, int);
97 static void		rt2560_reset_tx_ring(struct rt2560_softc *,
98 			    struct rt2560_tx_ring *);
99 static void		rt2560_free_tx_ring(struct rt2560_softc *,
100 			    struct rt2560_tx_ring *);
101 static int		rt2560_alloc_rx_ring(struct rt2560_softc *,
102 			    struct rt2560_rx_ring *, int);
103 static void		rt2560_reset_rx_ring(struct rt2560_softc *,
104 			    struct rt2560_rx_ring *);
105 static void		rt2560_free_rx_ring(struct rt2560_softc *,
106 			    struct rt2560_rx_ring *);
107 static struct		ieee80211_node *rt2560_node_alloc(
108 			    struct ieee80211_node_table *);
109 static void		rt2560_newassoc(struct ieee80211_node *, int);
110 static int		rt2560_newstate(struct ieee80211vap *,
111 			    enum ieee80211_state, int);
112 static uint16_t		rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
113 static void		rt2560_encryption_intr(struct rt2560_softc *);
114 static void		rt2560_tx_intr(struct rt2560_softc *);
115 static void		rt2560_prio_intr(struct rt2560_softc *);
116 static void		rt2560_decryption_intr(struct rt2560_softc *);
117 static void		rt2560_rx_intr(struct rt2560_softc *);
118 static void		rt2560_beacon_update(struct ieee80211vap *, int item);
119 static void		rt2560_beacon_expire(struct rt2560_softc *);
120 static void		rt2560_wakeup_expire(struct rt2560_softc *);
121 static void		rt2560_scan_start(struct ieee80211com *);
122 static void		rt2560_scan_end(struct ieee80211com *);
123 static void		rt2560_set_channel(struct ieee80211com *);
124 static void		rt2560_setup_tx_desc(struct rt2560_softc *,
125 			    struct rt2560_tx_desc *, uint32_t, int, int, int,
126 			    bus_addr_t);
127 static int		rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
128 			    struct ieee80211_node *);
129 static int		rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
130 			    struct ieee80211_node *);
131 static int		rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
132 			    struct ieee80211_node *);
133 static void		rt2560_start_locked(struct ifnet *);
134 static void		rt2560_start(struct ifnet *);
135 static void		rt2560_watchdog(void *);
136 static int		rt2560_ioctl(struct ifnet *, u_long, caddr_t);
137 static void		rt2560_bbp_write(struct rt2560_softc *, uint8_t,
138 			    uint8_t);
139 static uint8_t		rt2560_bbp_read(struct rt2560_softc *, uint8_t);
140 static void		rt2560_rf_write(struct rt2560_softc *, uint8_t,
141 			    uint32_t);
142 static void		rt2560_set_chan(struct rt2560_softc *,
143 			    struct ieee80211_channel *);
144 #if 0
145 static void		rt2560_disable_rf_tune(struct rt2560_softc *);
146 #endif
147 static void		rt2560_enable_tsf_sync(struct rt2560_softc *);
148 static void		rt2560_update_plcp(struct rt2560_softc *);
149 static void		rt2560_update_slot(struct ifnet *);
150 static void		rt2560_set_basicrates(struct rt2560_softc *);
151 static void		rt2560_update_led(struct rt2560_softc *, int, int);
152 static void		rt2560_set_bssid(struct rt2560_softc *, const uint8_t *);
153 static void		rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
154 static void		rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
155 static void		rt2560_update_promisc(struct ifnet *);
156 static const char	*rt2560_get_rf(int);
157 static void		rt2560_read_config(struct rt2560_softc *);
158 static int		rt2560_bbp_init(struct rt2560_softc *);
159 static void		rt2560_set_txantenna(struct rt2560_softc *, int);
160 static void		rt2560_set_rxantenna(struct rt2560_softc *, int);
161 static void		rt2560_init_locked(struct rt2560_softc *);
162 static void		rt2560_init(void *);
163 static void		rt2560_stop_locked(struct rt2560_softc *);
164 static int		rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *,
165 				const struct ieee80211_bpf_params *);
166 
167 static const struct {
168 	uint32_t	reg;
169 	uint32_t	val;
170 } rt2560_def_mac[] = {
171 	RT2560_DEF_MAC
172 };
173 
174 static const struct {
175 	uint8_t	reg;
176 	uint8_t	val;
177 } rt2560_def_bbp[] = {
178 	RT2560_DEF_BBP
179 };
180 
181 static const uint32_t rt2560_rf2522_r2[]    = RT2560_RF2522_R2;
182 static const uint32_t rt2560_rf2523_r2[]    = RT2560_RF2523_R2;
183 static const uint32_t rt2560_rf2524_r2[]    = RT2560_RF2524_R2;
184 static const uint32_t rt2560_rf2525_r2[]    = RT2560_RF2525_R2;
185 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
186 static const uint32_t rt2560_rf2525e_r2[]   = RT2560_RF2525E_R2;
187 static const uint32_t rt2560_rf2526_r2[]    = RT2560_RF2526_R2;
188 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
189 
190 static const struct {
191 	uint8_t		chan;
192 	uint32_t	r1, r2, r4;
193 } rt2560_rf5222[] = {
194 	RT2560_RF5222
195 };
196 
197 int
198 rt2560_attach(device_t dev, int id)
199 {
200 	struct rt2560_softc *sc = device_get_softc(dev);
201 	struct ieee80211com *ic;
202 	struct ifnet *ifp;
203 	int error;
204 	uint8_t bands;
205 
206 	sc->sc_dev = dev;
207 
208 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
209 	    MTX_DEF | MTX_RECURSE);
210 
211 	callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
212 
213 	/* retrieve RT2560 rev. no */
214 	sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
215 
216 	/* retrieve RF rev. no and various other things from EEPROM */
217 	rt2560_read_config(sc);
218 
219 	device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
220 	    sc->asic_rev, rt2560_get_rf(sc->rf_rev));
221 
222 	/*
223 	 * Allocate Tx and Rx rings.
224 	 */
225 	error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
226 	if (error != 0) {
227 		device_printf(sc->sc_dev, "could not allocate Tx ring\n");
228 		goto fail1;
229 	}
230 
231 	error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
232 	if (error != 0) {
233 		device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
234 		goto fail2;
235 	}
236 
237 	error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
238 	if (error != 0) {
239 		device_printf(sc->sc_dev, "could not allocate Prio ring\n");
240 		goto fail3;
241 	}
242 
243 	error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
244 	if (error != 0) {
245 		device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
246 		goto fail4;
247 	}
248 
249 	error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
250 	if (error != 0) {
251 		device_printf(sc->sc_dev, "could not allocate Rx ring\n");
252 		goto fail5;
253 	}
254 
255 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
256 	if (ifp == NULL) {
257 		device_printf(sc->sc_dev, "can not if_alloc()\n");
258 		goto fail6;
259 	}
260 	ic = ifp->if_l2com;
261 
262 	/* retrieve MAC address */
263 	rt2560_get_macaddr(sc, ic->ic_myaddr);
264 
265 	ifp->if_softc = sc;
266 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
267 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
268 	ifp->if_init = rt2560_init;
269 	ifp->if_ioctl = rt2560_ioctl;
270 	ifp->if_start = rt2560_start;
271 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
272 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
273 	IFQ_SET_READY(&ifp->if_snd);
274 
275 	ic->ic_ifp = ifp;
276 	ic->ic_opmode = IEEE80211_M_STA;
277 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
278 
279 	/* set device capabilities */
280 	ic->ic_caps =
281 		  IEEE80211_C_STA		/* station mode */
282 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
283 		| IEEE80211_C_HOSTAP		/* hostap mode */
284 		| IEEE80211_C_MONITOR		/* monitor mode */
285 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
286 		| IEEE80211_C_WDS		/* 4-address traffic works */
287 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
288 		| IEEE80211_C_SHSLOT		/* short slot time supported */
289 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
290 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
291 #ifdef notyet
292 		| IEEE80211_C_TXFRAG		/* handle tx frags */
293 #endif
294 		;
295 
296 	bands = 0;
297 	setbit(&bands, IEEE80211_MODE_11B);
298 	setbit(&bands, IEEE80211_MODE_11G);
299 	if (sc->rf_rev == RT2560_RF_5222)
300 		setbit(&bands, IEEE80211_MODE_11A);
301 	ieee80211_init_channels(ic, NULL, &bands);
302 
303 	ieee80211_ifattach(ic);
304 	ic->ic_newassoc = rt2560_newassoc;
305 	ic->ic_raw_xmit = rt2560_raw_xmit;
306 	ic->ic_updateslot = rt2560_update_slot;
307 	ic->ic_update_promisc = rt2560_update_promisc;
308 	ic->ic_node_alloc = rt2560_node_alloc;
309 	ic->ic_scan_start = rt2560_scan_start;
310 	ic->ic_scan_end = rt2560_scan_end;
311 	ic->ic_set_channel = rt2560_set_channel;
312 
313 	ic->ic_vap_create = rt2560_vap_create;
314 	ic->ic_vap_delete = rt2560_vap_delete;
315 
316 	bpfattach(ifp, DLT_IEEE802_11_RADIO,
317 	    sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap));
318 
319 	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
320 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
321 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
322 
323 	sc->sc_txtap_len = sizeof sc->sc_txtap;
324 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
325 	sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
326 
327 	/*
328 	 * Add a few sysctl knobs.
329 	 */
330 #ifdef RAL_DEBUG
331 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
332 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
333 	    "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
334 #endif
335 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
336 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
337 	    "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
338 
339 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
340 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
341 	    "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
342 
343 	if (bootverbose)
344 		ieee80211_announce(ic);
345 
346 	return 0;
347 
348 fail6:	rt2560_free_rx_ring(sc, &sc->rxq);
349 fail5:	rt2560_free_tx_ring(sc, &sc->bcnq);
350 fail4:	rt2560_free_tx_ring(sc, &sc->prioq);
351 fail3:	rt2560_free_tx_ring(sc, &sc->atimq);
352 fail2:	rt2560_free_tx_ring(sc, &sc->txq);
353 fail1:	mtx_destroy(&sc->sc_mtx);
354 
355 	return ENXIO;
356 }
357 
358 int
359 rt2560_detach(void *xsc)
360 {
361 	struct rt2560_softc *sc = xsc;
362 	struct ifnet *ifp = sc->sc_ifp;
363 	struct ieee80211com *ic = ifp->if_l2com;
364 
365 	rt2560_stop(sc);
366 
367 	bpfdetach(ifp);
368 	ieee80211_ifdetach(ic);
369 
370 	rt2560_free_tx_ring(sc, &sc->txq);
371 	rt2560_free_tx_ring(sc, &sc->atimq);
372 	rt2560_free_tx_ring(sc, &sc->prioq);
373 	rt2560_free_tx_ring(sc, &sc->bcnq);
374 	rt2560_free_rx_ring(sc, &sc->rxq);
375 
376 	if_free(ifp);
377 
378 	mtx_destroy(&sc->sc_mtx);
379 
380 	return 0;
381 }
382 
383 static struct ieee80211vap *
384 rt2560_vap_create(struct ieee80211com *ic,
385 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
386 	const uint8_t bssid[IEEE80211_ADDR_LEN],
387 	const uint8_t mac[IEEE80211_ADDR_LEN])
388 {
389 	struct ifnet *ifp = ic->ic_ifp;
390 	struct rt2560_vap *rvp;
391 	struct ieee80211vap *vap;
392 
393 	switch (opmode) {
394 	case IEEE80211_M_STA:
395 	case IEEE80211_M_IBSS:
396 	case IEEE80211_M_AHDEMO:
397 	case IEEE80211_M_MONITOR:
398 	case IEEE80211_M_HOSTAP:
399 		if (!TAILQ_EMPTY(&ic->ic_vaps)) {
400 			if_printf(ifp, "only 1 vap supported\n");
401 			return NULL;
402 		}
403 		if (opmode == IEEE80211_M_STA)
404 			flags |= IEEE80211_CLONE_NOBEACONS;
405 		break;
406 	case IEEE80211_M_WDS:
407 		if (TAILQ_EMPTY(&ic->ic_vaps) ||
408 		    ic->ic_opmode != IEEE80211_M_HOSTAP) {
409 			if_printf(ifp, "wds only supported in ap mode\n");
410 			return NULL;
411 		}
412 		/*
413 		 * Silently remove any request for a unique
414 		 * bssid; WDS vap's always share the local
415 		 * mac address.
416 		 */
417 		flags &= ~IEEE80211_CLONE_BSSID;
418 		break;
419 	default:
420 		if_printf(ifp, "unknown opmode %d\n", opmode);
421 		return NULL;
422 	}
423 	rvp = (struct rt2560_vap *) malloc(sizeof(struct rt2560_vap),
424 	    M_80211_VAP, M_NOWAIT | M_ZERO);
425 	if (rvp == NULL)
426 		return NULL;
427 	vap = &rvp->ral_vap;
428 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
429 
430 	/* override state transition machine */
431 	rvp->ral_newstate = vap->iv_newstate;
432 	vap->iv_newstate = rt2560_newstate;
433 	vap->iv_update_beacon = rt2560_beacon_update;
434 
435 	ieee80211_amrr_init(&rvp->amrr, vap,
436 	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
437 	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
438 	    500 /* ms */);
439 
440 	/* complete setup */
441 	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
442 	if (TAILQ_FIRST(&ic->ic_vaps) == vap)
443 		ic->ic_opmode = opmode;
444 	return vap;
445 }
446 
447 static void
448 rt2560_vap_delete(struct ieee80211vap *vap)
449 {
450 	struct rt2560_vap *rvp = RT2560_VAP(vap);
451 
452 	ieee80211_amrr_cleanup(&rvp->amrr);
453 	ieee80211_vap_detach(vap);
454 	free(rvp, M_80211_VAP);
455 }
456 
457 void
458 rt2560_resume(void *xsc)
459 {
460 	struct rt2560_softc *sc = xsc;
461 	struct ifnet *ifp = sc->sc_ifp;
462 
463 	if (ifp->if_flags & IFF_UP)
464 		rt2560_init(sc);
465 }
466 
467 static void
468 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
469 {
470 	if (error != 0)
471 		return;
472 
473 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
474 
475 	*(bus_addr_t *)arg = segs[0].ds_addr;
476 }
477 
478 static int
479 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
480     int count)
481 {
482 	int i, error;
483 
484 	ring->count = count;
485 	ring->queued = 0;
486 	ring->cur = ring->next = 0;
487 	ring->cur_encrypt = ring->next_encrypt = 0;
488 
489 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
490 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
491 	    count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE,
492 	    0, NULL, NULL, &ring->desc_dmat);
493 	if (error != 0) {
494 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
495 		goto fail;
496 	}
497 
498 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
499 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
500 	if (error != 0) {
501 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
502 		goto fail;
503 	}
504 
505 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
506 	    count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
507 	    0);
508 	if (error != 0) {
509 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
510 		goto fail;
511 	}
512 
513 	ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
514 	    M_NOWAIT | M_ZERO);
515 	if (ring->data == NULL) {
516 		device_printf(sc->sc_dev, "could not allocate soft data\n");
517 		error = ENOMEM;
518 		goto fail;
519 	}
520 
521 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
522 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
523 	    MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL,
524 	    &ring->data_dmat);
525 	if (error != 0) {
526 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
527 		goto fail;
528 	}
529 
530 	for (i = 0; i < count; i++) {
531 		error = bus_dmamap_create(ring->data_dmat, 0,
532 		    &ring->data[i].map);
533 		if (error != 0) {
534 			device_printf(sc->sc_dev, "could not create DMA map\n");
535 			goto fail;
536 		}
537 	}
538 
539 	return 0;
540 
541 fail:	rt2560_free_tx_ring(sc, ring);
542 	return error;
543 }
544 
545 static void
546 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
547 {
548 	struct rt2560_tx_desc *desc;
549 	struct rt2560_tx_data *data;
550 	int i;
551 
552 	for (i = 0; i < ring->count; i++) {
553 		desc = &ring->desc[i];
554 		data = &ring->data[i];
555 
556 		if (data->m != NULL) {
557 			bus_dmamap_sync(ring->data_dmat, data->map,
558 			    BUS_DMASYNC_POSTWRITE);
559 			bus_dmamap_unload(ring->data_dmat, data->map);
560 			m_freem(data->m);
561 			data->m = NULL;
562 		}
563 
564 		if (data->ni != NULL) {
565 			ieee80211_free_node(data->ni);
566 			data->ni = NULL;
567 		}
568 
569 		desc->flags = 0;
570 	}
571 
572 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
573 
574 	ring->queued = 0;
575 	ring->cur = ring->next = 0;
576 	ring->cur_encrypt = ring->next_encrypt = 0;
577 }
578 
579 static void
580 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
581 {
582 	struct rt2560_tx_data *data;
583 	int i;
584 
585 	if (ring->desc != NULL) {
586 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
587 		    BUS_DMASYNC_POSTWRITE);
588 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
589 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
590 	}
591 
592 	if (ring->desc_dmat != NULL)
593 		bus_dma_tag_destroy(ring->desc_dmat);
594 
595 	if (ring->data != NULL) {
596 		for (i = 0; i < ring->count; i++) {
597 			data = &ring->data[i];
598 
599 			if (data->m != NULL) {
600 				bus_dmamap_sync(ring->data_dmat, data->map,
601 				    BUS_DMASYNC_POSTWRITE);
602 				bus_dmamap_unload(ring->data_dmat, data->map);
603 				m_freem(data->m);
604 			}
605 
606 			if (data->ni != NULL)
607 				ieee80211_free_node(data->ni);
608 
609 			if (data->map != NULL)
610 				bus_dmamap_destroy(ring->data_dmat, data->map);
611 		}
612 
613 		free(ring->data, M_DEVBUF);
614 	}
615 
616 	if (ring->data_dmat != NULL)
617 		bus_dma_tag_destroy(ring->data_dmat);
618 }
619 
620 static int
621 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
622     int count)
623 {
624 	struct rt2560_rx_desc *desc;
625 	struct rt2560_rx_data *data;
626 	bus_addr_t physaddr;
627 	int i, error;
628 
629 	ring->count = count;
630 	ring->cur = ring->next = 0;
631 	ring->cur_decrypt = 0;
632 
633 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
634 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
635 	    count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE,
636 	    0, NULL, NULL, &ring->desc_dmat);
637 	if (error != 0) {
638 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
639 		goto fail;
640 	}
641 
642 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
643 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
644 	if (error != 0) {
645 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
646 		goto fail;
647 	}
648 
649 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
650 	    count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
651 	    0);
652 	if (error != 0) {
653 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
654 		goto fail;
655 	}
656 
657 	ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
658 	    M_NOWAIT | M_ZERO);
659 	if (ring->data == NULL) {
660 		device_printf(sc->sc_dev, "could not allocate soft data\n");
661 		error = ENOMEM;
662 		goto fail;
663 	}
664 
665 	/*
666 	 * Pre-allocate Rx buffers and populate Rx ring.
667 	 */
668 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
669 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
670 	    1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
671 	if (error != 0) {
672 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
673 		goto fail;
674 	}
675 
676 	for (i = 0; i < count; i++) {
677 		desc = &sc->rxq.desc[i];
678 		data = &sc->rxq.data[i];
679 
680 		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
681 		if (error != 0) {
682 			device_printf(sc->sc_dev, "could not create DMA map\n");
683 			goto fail;
684 		}
685 
686 		data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
687 		if (data->m == NULL) {
688 			device_printf(sc->sc_dev,
689 			    "could not allocate rx mbuf\n");
690 			error = ENOMEM;
691 			goto fail;
692 		}
693 
694 		error = bus_dmamap_load(ring->data_dmat, data->map,
695 		    mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
696 		    &physaddr, 0);
697 		if (error != 0) {
698 			device_printf(sc->sc_dev,
699 			    "could not load rx buf DMA map");
700 			goto fail;
701 		}
702 
703 		desc->flags = htole32(RT2560_RX_BUSY);
704 		desc->physaddr = htole32(physaddr);
705 	}
706 
707 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
708 
709 	return 0;
710 
711 fail:	rt2560_free_rx_ring(sc, ring);
712 	return error;
713 }
714 
715 static void
716 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
717 {
718 	int i;
719 
720 	for (i = 0; i < ring->count; i++) {
721 		ring->desc[i].flags = htole32(RT2560_RX_BUSY);
722 		ring->data[i].drop = 0;
723 	}
724 
725 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
726 
727 	ring->cur = ring->next = 0;
728 	ring->cur_decrypt = 0;
729 }
730 
731 static void
732 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
733 {
734 	struct rt2560_rx_data *data;
735 	int i;
736 
737 	if (ring->desc != NULL) {
738 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
739 		    BUS_DMASYNC_POSTWRITE);
740 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
741 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
742 	}
743 
744 	if (ring->desc_dmat != NULL)
745 		bus_dma_tag_destroy(ring->desc_dmat);
746 
747 	if (ring->data != NULL) {
748 		for (i = 0; i < ring->count; i++) {
749 			data = &ring->data[i];
750 
751 			if (data->m != NULL) {
752 				bus_dmamap_sync(ring->data_dmat, data->map,
753 				    BUS_DMASYNC_POSTREAD);
754 				bus_dmamap_unload(ring->data_dmat, data->map);
755 				m_freem(data->m);
756 			}
757 
758 			if (data->map != NULL)
759 				bus_dmamap_destroy(ring->data_dmat, data->map);
760 		}
761 
762 		free(ring->data, M_DEVBUF);
763 	}
764 
765 	if (ring->data_dmat != NULL)
766 		bus_dma_tag_destroy(ring->data_dmat);
767 }
768 
769 static struct ieee80211_node *
770 rt2560_node_alloc(struct ieee80211_node_table *nt)
771 {
772 	struct rt2560_node *rn;
773 
774 	rn = malloc(sizeof (struct rt2560_node), M_80211_NODE,
775 	    M_NOWAIT | M_ZERO);
776 
777 	return (rn != NULL) ? &rn->ni : NULL;
778 }
779 
780 static void
781 rt2560_newassoc(struct ieee80211_node *ni, int isnew)
782 {
783 	struct ieee80211vap *vap = ni->ni_vap;
784 
785 	ieee80211_amrr_node_init(&RT2560_VAP(vap)->amrr,
786 	    &RT2560_NODE(ni)->amrr, ni);
787 }
788 
789 static int
790 rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
791 {
792 	struct rt2560_vap *rvp = RT2560_VAP(vap);
793 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
794 	struct rt2560_softc *sc = ifp->if_softc;
795 	int error;
796 
797 	if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
798 		/* abort TSF synchronization */
799 		RAL_WRITE(sc, RT2560_CSR14, 0);
800 
801 		/* turn association led off */
802 		rt2560_update_led(sc, 0, 0);
803 	}
804 
805 	error = rvp->ral_newstate(vap, nstate, arg);
806 
807 	if (error == 0 && nstate == IEEE80211_S_RUN) {
808 		struct ieee80211_node *ni = vap->iv_bss;
809 		struct mbuf *m;
810 
811 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
812 			rt2560_update_plcp(sc);
813 			rt2560_set_basicrates(sc);
814 			rt2560_set_bssid(sc, ni->ni_bssid);
815 		}
816 
817 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
818 		    vap->iv_opmode == IEEE80211_M_IBSS) {
819 			m = ieee80211_beacon_alloc(ni, &rvp->ral_bo);
820 			if (m == NULL) {
821 				if_printf(ifp, "could not allocate beacon\n");
822 				return ENOBUFS;
823 			}
824 			ieee80211_ref_node(ni);
825 			error = rt2560_tx_bcn(sc, m, ni);
826 			if (error != 0)
827 				return error;
828 		}
829 
830 		/* turn assocation led on */
831 		rt2560_update_led(sc, 1, 0);
832 
833 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
834 			if (vap->iv_opmode == IEEE80211_M_STA) {
835 				/* fake a join to init the tx rate */
836 				rt2560_newassoc(ni, 1);
837 			}
838 			rt2560_enable_tsf_sync(sc);
839 		}
840 	}
841 	return error;
842 }
843 
844 /*
845  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
846  * 93C66).
847  */
848 static uint16_t
849 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
850 {
851 	uint32_t tmp;
852 	uint16_t val;
853 	int n;
854 
855 	/* clock C once before the first command */
856 	RT2560_EEPROM_CTL(sc, 0);
857 
858 	RT2560_EEPROM_CTL(sc, RT2560_S);
859 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
860 	RT2560_EEPROM_CTL(sc, RT2560_S);
861 
862 	/* write start bit (1) */
863 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
864 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
865 
866 	/* write READ opcode (10) */
867 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
868 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
869 	RT2560_EEPROM_CTL(sc, RT2560_S);
870 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
871 
872 	/* write address (A5-A0 or A7-A0) */
873 	n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
874 	for (; n >= 0; n--) {
875 		RT2560_EEPROM_CTL(sc, RT2560_S |
876 		    (((addr >> n) & 1) << RT2560_SHIFT_D));
877 		RT2560_EEPROM_CTL(sc, RT2560_S |
878 		    (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
879 	}
880 
881 	RT2560_EEPROM_CTL(sc, RT2560_S);
882 
883 	/* read data Q15-Q0 */
884 	val = 0;
885 	for (n = 15; n >= 0; n--) {
886 		RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
887 		tmp = RAL_READ(sc, RT2560_CSR21);
888 		val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
889 		RT2560_EEPROM_CTL(sc, RT2560_S);
890 	}
891 
892 	RT2560_EEPROM_CTL(sc, 0);
893 
894 	/* clear Chip Select and clock C */
895 	RT2560_EEPROM_CTL(sc, RT2560_S);
896 	RT2560_EEPROM_CTL(sc, 0);
897 	RT2560_EEPROM_CTL(sc, RT2560_C);
898 
899 	return val;
900 }
901 
902 /*
903  * Some frames were processed by the hardware cipher engine and are ready for
904  * transmission.
905  */
906 static void
907 rt2560_encryption_intr(struct rt2560_softc *sc)
908 {
909 	struct rt2560_tx_desc *desc;
910 	int hw;
911 
912 	/* retrieve last descriptor index processed by cipher engine */
913 	hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
914 	hw /= RT2560_TX_DESC_SIZE;
915 
916 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
917 	    BUS_DMASYNC_POSTREAD);
918 
919 	while (sc->txq.next_encrypt != hw) {
920 		if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
921 			printf("hw encrypt %d, cur_encrypt %d\n", hw,
922 			    sc->txq.cur_encrypt);
923 			break;
924 		}
925 
926 		desc = &sc->txq.desc[sc->txq.next_encrypt];
927 
928 		if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
929 		    (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
930 			break;
931 
932 		/* for TKIP, swap eiv field to fix a bug in ASIC */
933 		if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
934 		    RT2560_TX_CIPHER_TKIP)
935 			desc->eiv = bswap32(desc->eiv);
936 
937 		/* mark the frame ready for transmission */
938 		desc->flags |= htole32(RT2560_TX_VALID);
939 		desc->flags |= htole32(RT2560_TX_BUSY);
940 
941 		DPRINTFN(sc, 15, "encryption done idx=%u\n",
942 		    sc->txq.next_encrypt);
943 
944 		sc->txq.next_encrypt =
945 		    (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
946 	}
947 
948 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
949 	    BUS_DMASYNC_PREWRITE);
950 
951 	/* kick Tx */
952 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
953 }
954 
955 static void
956 rt2560_tx_intr(struct rt2560_softc *sc)
957 {
958 	struct ifnet *ifp = sc->sc_ifp;
959 	struct rt2560_tx_desc *desc;
960 	struct rt2560_tx_data *data;
961 	struct rt2560_node *rn;
962 	struct mbuf *m;
963 	uint32_t flags;
964 	int retrycnt;
965 
966 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
967 	    BUS_DMASYNC_POSTREAD);
968 
969 	for (;;) {
970 		desc = &sc->txq.desc[sc->txq.next];
971 		data = &sc->txq.data[sc->txq.next];
972 
973 		flags = le32toh(desc->flags);
974 		if ((flags & RT2560_TX_BUSY) ||
975 		    (flags & RT2560_TX_CIPHER_BUSY) ||
976 		    !(flags & RT2560_TX_VALID))
977 			break;
978 
979 		rn = (struct rt2560_node *)data->ni;
980 		m = data->m;
981 
982 		switch (flags & RT2560_TX_RESULT_MASK) {
983 		case RT2560_TX_SUCCESS:
984 			DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
985 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
986 				ieee80211_amrr_tx_complete(&rn->amrr,
987 				    IEEE80211_AMRR_SUCCESS, 0);
988 			ifp->if_opackets++;
989 			break;
990 
991 		case RT2560_TX_SUCCESS_RETRY:
992 			retrycnt = RT2560_TX_RETRYCNT(flags);
993 
994 			DPRINTFN(sc, 9, "data frame sent after %u retries\n",
995 			    retrycnt);
996 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
997 				ieee80211_amrr_tx_complete(&rn->amrr,
998 				    IEEE80211_AMRR_SUCCESS, retrycnt);
999 			ifp->if_opackets++;
1000 			break;
1001 
1002 		case RT2560_TX_FAIL_RETRY:
1003 			retrycnt = RT2560_TX_RETRYCNT(flags);
1004 
1005 			DPRINTFN(sc, 9, "data frame failed after %d retries\n",
1006 			    retrycnt);
1007 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
1008 				ieee80211_amrr_tx_complete(&rn->amrr,
1009 				    IEEE80211_AMRR_FAILURE, retrycnt);
1010 			ifp->if_oerrors++;
1011 			break;
1012 
1013 		case RT2560_TX_FAIL_INVALID:
1014 		case RT2560_TX_FAIL_OTHER:
1015 		default:
1016 			device_printf(sc->sc_dev, "sending data frame failed "
1017 			    "0x%08x\n", flags);
1018 			ifp->if_oerrors++;
1019 		}
1020 
1021 		bus_dmamap_sync(sc->txq.data_dmat, data->map,
1022 		    BUS_DMASYNC_POSTWRITE);
1023 		bus_dmamap_unload(sc->txq.data_dmat, data->map);
1024 		m_freem(m);
1025 		data->m = NULL;
1026 		ieee80211_free_node(data->ni);
1027 		data->ni = NULL;
1028 
1029 		/* descriptor is no longer valid */
1030 		desc->flags &= ~htole32(RT2560_TX_VALID);
1031 
1032 		DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next);
1033 
1034 		sc->txq.queued--;
1035 		sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1036 	}
1037 
1038 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1039 	    BUS_DMASYNC_PREWRITE);
1040 
1041 	if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1042 		sc->sc_tx_timer = 0;
1043 
1044 	if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1045 		sc->sc_flags &= ~RT2560_F_DATA_OACTIVE;
1046 		if ((sc->sc_flags &
1047 		     (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1048 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1049 		rt2560_start_locked(ifp);
1050 	}
1051 }
1052 
1053 static void
1054 rt2560_prio_intr(struct rt2560_softc *sc)
1055 {
1056 	struct ifnet *ifp = sc->sc_ifp;
1057 	struct rt2560_tx_desc *desc;
1058 	struct rt2560_tx_data *data;
1059 	struct ieee80211_node *ni;
1060 	struct mbuf *m;
1061 	int flags;
1062 
1063 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1064 	    BUS_DMASYNC_POSTREAD);
1065 
1066 	for (;;) {
1067 		desc = &sc->prioq.desc[sc->prioq.next];
1068 		data = &sc->prioq.data[sc->prioq.next];
1069 
1070 		flags = le32toh(desc->flags);
1071 		if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0)
1072 			break;
1073 
1074 		switch (flags & RT2560_TX_RESULT_MASK) {
1075 		case RT2560_TX_SUCCESS:
1076 			DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully");
1077 			break;
1078 
1079 		case RT2560_TX_SUCCESS_RETRY:
1080 			DPRINTFN(sc, 9, "mgt frame sent after %u retries\n",
1081 			    (flags >> 5) & 0x7);
1082 			break;
1083 
1084 		case RT2560_TX_FAIL_RETRY:
1085 			DPRINTFN(sc, 9, "%s\n",
1086 			    "sending mgt frame failed (too much retries)");
1087 			break;
1088 
1089 		case RT2560_TX_FAIL_INVALID:
1090 		case RT2560_TX_FAIL_OTHER:
1091 		default:
1092 			device_printf(sc->sc_dev, "sending mgt frame failed "
1093 			    "0x%08x\n", flags);
1094 			break;
1095 		}
1096 
1097 		bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1098 		    BUS_DMASYNC_POSTWRITE);
1099 		bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1100 
1101 		m = data->m;
1102 		data->m = NULL;
1103 		ni = data->ni;
1104 		data->ni = NULL;
1105 
1106 		/* descriptor is no longer valid */
1107 		desc->flags &= ~htole32(RT2560_TX_VALID);
1108 
1109 		DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next);
1110 
1111 		sc->prioq.queued--;
1112 		sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1113 
1114 		if (m->m_flags & M_TXCB)
1115 			ieee80211_process_callback(ni, m,
1116 				(flags & RT2560_TX_RESULT_MASK) &~
1117 				(RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY));
1118 		m_freem(m);
1119 		ieee80211_free_node(ni);
1120 	}
1121 
1122 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1123 	    BUS_DMASYNC_PREWRITE);
1124 
1125 	if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1126 		sc->sc_tx_timer = 0;
1127 
1128 	if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1129 		sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE;
1130 		if ((sc->sc_flags &
1131 		     (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1132 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1133 		rt2560_start_locked(ifp);
1134 	}
1135 }
1136 
1137 /*
1138  * Some frames were processed by the hardware cipher engine and are ready for
1139  * handoff to the IEEE802.11 layer.
1140  */
1141 static void
1142 rt2560_decryption_intr(struct rt2560_softc *sc)
1143 {
1144 	struct ifnet *ifp = sc->sc_ifp;
1145 	struct ieee80211com *ic = ifp->if_l2com;
1146 	struct rt2560_rx_desc *desc;
1147 	struct rt2560_rx_data *data;
1148 	bus_addr_t physaddr;
1149 	struct ieee80211_frame *wh;
1150 	struct ieee80211_node *ni;
1151 	struct mbuf *mnew, *m;
1152 	int hw, error;
1153 
1154 	/* retrieve last decriptor index processed by cipher engine */
1155 	hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1156 	hw /= RT2560_RX_DESC_SIZE;
1157 
1158 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1159 	    BUS_DMASYNC_POSTREAD);
1160 
1161 	for (; sc->rxq.cur_decrypt != hw;) {
1162 		desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1163 		data = &sc->rxq.data[sc->rxq.cur_decrypt];
1164 
1165 		if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1166 		    (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1167 			break;
1168 
1169 		if (data->drop) {
1170 			ifp->if_ierrors++;
1171 			goto skip;
1172 		}
1173 
1174 		if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1175 		    (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1176 			ifp->if_ierrors++;
1177 			goto skip;
1178 		}
1179 
1180 		/*
1181 		 * Try to allocate a new mbuf for this ring element and load it
1182 		 * before processing the current mbuf. If the ring element
1183 		 * cannot be loaded, drop the received packet and reuse the old
1184 		 * mbuf. In the unlikely case that the old mbuf can't be
1185 		 * reloaded either, explicitly panic.
1186 		 */
1187 		mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1188 		if (mnew == NULL) {
1189 			ifp->if_ierrors++;
1190 			goto skip;
1191 		}
1192 
1193 		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1194 		    BUS_DMASYNC_POSTREAD);
1195 		bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1196 
1197 		error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1198 		    mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1199 		    &physaddr, 0);
1200 		if (error != 0) {
1201 			m_freem(mnew);
1202 
1203 			/* try to reload the old mbuf */
1204 			error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1205 			    mtod(data->m, void *), MCLBYTES,
1206 			    rt2560_dma_map_addr, &physaddr, 0);
1207 			if (error != 0) {
1208 				/* very unlikely that it will fail... */
1209 				panic("%s: could not load old rx mbuf",
1210 				    device_get_name(sc->sc_dev));
1211 			}
1212 			ifp->if_ierrors++;
1213 			goto skip;
1214 		}
1215 
1216 		/*
1217 	 	 * New mbuf successfully loaded, update Rx ring and continue
1218 		 * processing.
1219 		 */
1220 		m = data->m;
1221 		data->m = mnew;
1222 		desc->physaddr = htole32(physaddr);
1223 
1224 		/* finalize mbuf */
1225 		m->m_pkthdr.rcvif = ifp;
1226 		m->m_pkthdr.len = m->m_len =
1227 		    (le32toh(desc->flags) >> 16) & 0xfff;
1228 
1229 		if (bpf_peers_present(ifp->if_bpf)) {
1230 			struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1231 			uint32_t tsf_lo, tsf_hi;
1232 
1233 			/* get timestamp (low and high 32 bits) */
1234 			tsf_hi = RAL_READ(sc, RT2560_CSR17);
1235 			tsf_lo = RAL_READ(sc, RT2560_CSR16);
1236 
1237 			tap->wr_tsf =
1238 			    htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1239 			tap->wr_flags = 0;
1240 			tap->wr_rate = ieee80211_plcp2rate(desc->rate,
1241 			    (desc->flags & htole32(RT2560_RX_OFDM)) ?
1242 				IEEE80211_T_OFDM : IEEE80211_T_CCK);
1243 			tap->wr_antenna = sc->rx_ant;
1244 			tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi);
1245 
1246 			bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
1247 		}
1248 
1249 		sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1250 		RAL_UNLOCK(sc);
1251 		wh = mtod(m, struct ieee80211_frame *);
1252 		ni = ieee80211_find_rxnode(ic,
1253 		    (struct ieee80211_frame_min *)wh);
1254 		if (ni != NULL) {
1255 			(void) ieee80211_input(ni, m,
1256 			    RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0);
1257 			ieee80211_free_node(ni);
1258 		} else
1259 			(void) ieee80211_input_all(ic, m,
1260 			    RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0);
1261 
1262 		RAL_LOCK(sc);
1263 		sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1264 skip:		desc->flags = htole32(RT2560_RX_BUSY);
1265 
1266 		DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1267 
1268 		sc->rxq.cur_decrypt =
1269 		    (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1270 	}
1271 
1272 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1273 	    BUS_DMASYNC_PREWRITE);
1274 }
1275 
1276 /*
1277  * Some frames were received. Pass them to the hardware cipher engine before
1278  * sending them to the 802.11 layer.
1279  */
1280 static void
1281 rt2560_rx_intr(struct rt2560_softc *sc)
1282 {
1283 	struct rt2560_rx_desc *desc;
1284 	struct rt2560_rx_data *data;
1285 
1286 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1287 	    BUS_DMASYNC_POSTREAD);
1288 
1289 	for (;;) {
1290 		desc = &sc->rxq.desc[sc->rxq.cur];
1291 		data = &sc->rxq.data[sc->rxq.cur];
1292 
1293 		if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1294 		    (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1295 			break;
1296 
1297 		data->drop = 0;
1298 
1299 		if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1300 		    (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1301 			/*
1302 			 * This should not happen since we did not request
1303 			 * to receive those frames when we filled RXCSR0.
1304 			 */
1305 			DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1306 			    le32toh(desc->flags));
1307 			data->drop = 1;
1308 		}
1309 
1310 		if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1311 			DPRINTFN(sc, 5, "%s\n", "bad length");
1312 			data->drop = 1;
1313 		}
1314 
1315 		/* mark the frame for decryption */
1316 		desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1317 
1318 		DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1319 
1320 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1321 	}
1322 
1323 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1324 	    BUS_DMASYNC_PREWRITE);
1325 
1326 	/* kick decrypt */
1327 	RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1328 }
1329 
1330 static void
1331 rt2560_beacon_update(struct ieee80211vap *vap, int item)
1332 {
1333 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1334 	struct ieee80211_beacon_offsets *bo = &rvp->ral_bo;
1335 
1336 	setbit(bo->bo_flags, item);
1337 }
1338 
1339 /*
1340  * This function is called periodically in IBSS mode when a new beacon must be
1341  * sent out.
1342  */
1343 static void
1344 rt2560_beacon_expire(struct rt2560_softc *sc)
1345 {
1346 	struct ifnet *ifp = sc->sc_ifp;
1347 	struct ieee80211com *ic = ifp->if_l2com;
1348 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1349 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1350 	struct rt2560_tx_data *data;
1351 
1352 	if (ic->ic_opmode != IEEE80211_M_IBSS &&
1353 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
1354 		return;
1355 
1356 	data = &sc->bcnq.data[sc->bcnq.next];
1357 	/*
1358 	 * Don't send beacon if bsschan isn't set
1359 	 */
1360 	if (data->ni == NULL)
1361 	        return;
1362 
1363 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1364 	bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1365 
1366 	/* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */
1367 	ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1);
1368 
1369 	rt2560_tx_bcn(sc, data->m, data->ni);
1370 
1371 	DPRINTFN(sc, 15, "%s", "beacon expired\n");
1372 
1373 	sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1374 }
1375 
1376 /* ARGSUSED */
1377 static void
1378 rt2560_wakeup_expire(struct rt2560_softc *sc)
1379 {
1380 	DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1381 }
1382 
1383 void
1384 rt2560_intr(void *arg)
1385 {
1386 	struct rt2560_softc *sc = arg;
1387 	struct ifnet *ifp = sc->sc_ifp;
1388 	uint32_t r;
1389 
1390 	RAL_LOCK(sc);
1391 
1392 	/* disable interrupts */
1393 	RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1394 
1395 	/* don't re-enable interrupts if we're shutting down */
1396 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1397 		RAL_UNLOCK(sc);
1398 		return;
1399 	}
1400 
1401 	r = RAL_READ(sc, RT2560_CSR7);
1402 	RAL_WRITE(sc, RT2560_CSR7, r);
1403 
1404 	if (r & RT2560_BEACON_EXPIRE)
1405 		rt2560_beacon_expire(sc);
1406 
1407 	if (r & RT2560_WAKEUP_EXPIRE)
1408 		rt2560_wakeup_expire(sc);
1409 
1410 	if (r & RT2560_ENCRYPTION_DONE)
1411 		rt2560_encryption_intr(sc);
1412 
1413 	if (r & RT2560_TX_DONE)
1414 		rt2560_tx_intr(sc);
1415 
1416 	if (r & RT2560_PRIO_DONE)
1417 		rt2560_prio_intr(sc);
1418 
1419 	if (r & RT2560_DECRYPTION_DONE)
1420 		rt2560_decryption_intr(sc);
1421 
1422 	if (r & RT2560_RX_DONE) {
1423 		rt2560_rx_intr(sc);
1424 		rt2560_encryption_intr(sc);
1425 	}
1426 
1427 	/* re-enable interrupts */
1428 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1429 
1430 	RAL_UNLOCK(sc);
1431 }
1432 
1433 #define RAL_SIFS		10	/* us */
1434 
1435 #define RT2560_TXRX_TURNAROUND	10	/* us */
1436 
1437 static uint8_t
1438 rt2560_plcp_signal(int rate)
1439 {
1440 	switch (rate) {
1441 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1442 	case 12:	return 0xb;
1443 	case 18:	return 0xf;
1444 	case 24:	return 0xa;
1445 	case 36:	return 0xe;
1446 	case 48:	return 0x9;
1447 	case 72:	return 0xd;
1448 	case 96:	return 0x8;
1449 	case 108:	return 0xc;
1450 
1451 	/* CCK rates (NB: not IEEE std, device-specific) */
1452 	case 2:		return 0x0;
1453 	case 4:		return 0x1;
1454 	case 11:	return 0x2;
1455 	case 22:	return 0x3;
1456 	}
1457 	return 0xff;		/* XXX unsupported/unknown rate */
1458 }
1459 
1460 static void
1461 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1462     uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1463 {
1464 	struct ifnet *ifp = sc->sc_ifp;
1465 	struct ieee80211com *ic = ifp->if_l2com;
1466 	uint16_t plcp_length;
1467 	int remainder;
1468 
1469 	desc->flags = htole32(flags);
1470 	desc->flags |= htole32(len << 16);
1471 
1472 	desc->physaddr = htole32(physaddr);
1473 	desc->wme = htole16(
1474 	    RT2560_AIFSN(2) |
1475 	    RT2560_LOGCWMIN(3) |
1476 	    RT2560_LOGCWMAX(8));
1477 
1478 	/* setup PLCP fields */
1479 	desc->plcp_signal  = rt2560_plcp_signal(rate);
1480 	desc->plcp_service = 4;
1481 
1482 	len += IEEE80211_CRC_LEN;
1483 	if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
1484 		desc->flags |= htole32(RT2560_TX_OFDM);
1485 
1486 		plcp_length = len & 0xfff;
1487 		desc->plcp_length_hi = plcp_length >> 6;
1488 		desc->plcp_length_lo = plcp_length & 0x3f;
1489 	} else {
1490 		plcp_length = (16 * len + rate - 1) / rate;
1491 		if (rate == 22) {
1492 			remainder = (16 * len) % 22;
1493 			if (remainder != 0 && remainder < 7)
1494 				desc->plcp_service |= RT2560_PLCP_LENGEXT;
1495 		}
1496 		desc->plcp_length_hi = plcp_length >> 8;
1497 		desc->plcp_length_lo = plcp_length & 0xff;
1498 
1499 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1500 			desc->plcp_signal |= 0x08;
1501 	}
1502 
1503 	if (!encrypt)
1504 		desc->flags |= htole32(RT2560_TX_VALID);
1505 	desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1506 			       : htole32(RT2560_TX_BUSY);
1507 }
1508 
1509 static int
1510 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1511     struct ieee80211_node *ni)
1512 {
1513 	struct ieee80211vap *vap = ni->ni_vap;
1514 	struct ieee80211com *ic = ni->ni_ic;
1515 	struct ifnet *ifp = sc->sc_ifp;
1516 	struct rt2560_tx_desc *desc;
1517 	struct rt2560_tx_data *data;
1518 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1519 	int nsegs, rate, error;
1520 
1521 	desc = &sc->bcnq.desc[sc->bcnq.cur];
1522 	data = &sc->bcnq.data[sc->bcnq.cur];
1523 
1524 	/* XXX maybe a separate beacon rate? */
1525 	rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate;
1526 
1527 	error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0,
1528 	    segs, &nsegs, BUS_DMA_NOWAIT);
1529 	if (error != 0) {
1530 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1531 		    error);
1532 		m_freem(m0);
1533 		return error;
1534 	}
1535 
1536 	if (bpf_peers_present(ifp->if_bpf)) {
1537 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1538 
1539 		tap->wt_flags = 0;
1540 		tap->wt_rate = rate;
1541 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1542 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1543 		tap->wt_antenna = sc->tx_ant;
1544 
1545 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1546 	}
1547 
1548 	data->m = m0;
1549 	data->ni = ni;
1550 
1551 	rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1552 	    RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1553 
1554 	DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1555 	    m0->m_pkthdr.len, sc->bcnq.cur, rate);
1556 
1557 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1558 	bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1559 	    BUS_DMASYNC_PREWRITE);
1560 
1561 	sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1562 
1563 	return 0;
1564 }
1565 
1566 static int
1567 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1568     struct ieee80211_node *ni)
1569 {
1570 	struct ieee80211vap *vap = ni->ni_vap;
1571 	struct ieee80211com *ic = ni->ni_ic;
1572 	struct ifnet *ifp = sc->sc_ifp;
1573 	struct rt2560_tx_desc *desc;
1574 	struct rt2560_tx_data *data;
1575 	struct ieee80211_frame *wh;
1576 	struct ieee80211_key *k;
1577 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1578 	uint16_t dur;
1579 	uint32_t flags = 0;
1580 	int nsegs, rate, error;
1581 
1582 	desc = &sc->prioq.desc[sc->prioq.cur];
1583 	data = &sc->prioq.data[sc->prioq.cur];
1584 
1585 	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
1586 
1587 	wh = mtod(m0, struct ieee80211_frame *);
1588 
1589 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1590 		k = ieee80211_crypto_encap(ni, m0);
1591 		if (k == NULL) {
1592 			m_freem(m0);
1593 			return ENOBUFS;
1594 		}
1595 	}
1596 
1597 	error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1598 	    segs, &nsegs, 0);
1599 	if (error != 0) {
1600 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1601 		    error);
1602 		m_freem(m0);
1603 		return error;
1604 	}
1605 
1606 	if (bpf_peers_present(ifp->if_bpf)) {
1607 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1608 
1609 		tap->wt_flags = 0;
1610 		tap->wt_rate = rate;
1611 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1612 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1613 		tap->wt_antenna = sc->tx_ant;
1614 
1615 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1616 	}
1617 
1618 	data->m = m0;
1619 	data->ni = ni;
1620 	/* management frames are not taken into account for amrr */
1621 	data->rix = IEEE80211_FIXED_RATE_NONE;
1622 
1623 	wh = mtod(m0, struct ieee80211_frame *);
1624 
1625 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1626 		flags |= RT2560_TX_ACK;
1627 
1628 		dur = ieee80211_ack_duration(sc->sc_rates,
1629 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1630 		*(uint16_t *)wh->i_dur = htole16(dur);
1631 
1632 		/* tell hardware to add timestamp for probe responses */
1633 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1634 		    IEEE80211_FC0_TYPE_MGT &&
1635 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1636 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1637 			flags |= RT2560_TX_TIMESTAMP;
1638 	}
1639 
1640 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1641 	    segs->ds_addr);
1642 
1643 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1644 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1645 	    BUS_DMASYNC_PREWRITE);
1646 
1647 	DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1648 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1649 
1650 	/* kick prio */
1651 	sc->prioq.queued++;
1652 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1653 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1654 
1655 	return 0;
1656 }
1657 
1658 static int
1659 rt2560_sendprot(struct rt2560_softc *sc,
1660     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1661 {
1662 	struct ieee80211com *ic = ni->ni_ic;
1663 	const struct ieee80211_frame *wh;
1664 	struct rt2560_tx_desc *desc;
1665 	struct rt2560_tx_data *data;
1666 	struct mbuf *mprot;
1667 	int protrate, ackrate, pktlen, flags, isshort, error;
1668 	uint16_t dur;
1669 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1670 	int nsegs;
1671 
1672 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1673 	    ("protection %d", prot));
1674 
1675 	wh = mtod(m, const struct ieee80211_frame *);
1676 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1677 
1678 	protrate = ieee80211_ctl_rate(sc->sc_rates, rate);
1679 	ackrate = ieee80211_ack_rate(sc->sc_rates, rate);
1680 
1681 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1682 	dur = ieee80211_compute_duration(sc->sc_rates, pktlen, rate, isshort)
1683 	    + ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1684 	flags = RT2560_TX_MORE_FRAG;
1685 	if (prot == IEEE80211_PROT_RTSCTS) {
1686 		/* NB: CTS is the same size as an ACK */
1687 		dur += ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1688 		flags |= RT2560_TX_ACK;
1689 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1690 	} else {
1691 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1692 	}
1693 	if (mprot == NULL) {
1694 		/* XXX stat + msg */
1695 		return ENOBUFS;
1696 	}
1697 
1698 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1699 	data = &sc->txq.data[sc->txq.cur_encrypt];
1700 
1701 	error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1702 	    mprot, segs, &nsegs, 0);
1703 	if (error != 0) {
1704 		device_printf(sc->sc_dev,
1705 		    "could not map mbuf (error %d)\n", error);
1706 		m_freem(mprot);
1707 		return error;
1708 	}
1709 
1710 	data->m = mprot;
1711 	data->ni = ieee80211_ref_node(ni);
1712 	/* ctl frames are not taken into account for amrr */
1713 	data->rix = IEEE80211_FIXED_RATE_NONE;
1714 
1715 	rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1716 	    segs->ds_addr);
1717 
1718 	bus_dmamap_sync(sc->txq.data_dmat, data->map,
1719 	    BUS_DMASYNC_PREWRITE);
1720 
1721 	sc->txq.queued++;
1722 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1723 
1724 	return 0;
1725 }
1726 
1727 static int
1728 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1729     struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1730 {
1731 	struct ifnet *ifp = sc->sc_ifp;
1732 	struct ieee80211com *ic = ifp->if_l2com;
1733 	struct rt2560_tx_desc *desc;
1734 	struct rt2560_tx_data *data;
1735 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1736 	uint32_t flags;
1737 	int nsegs, rate, error;
1738 
1739 	desc = &sc->prioq.desc[sc->prioq.cur];
1740 	data = &sc->prioq.data[sc->prioq.cur];
1741 
1742 	rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1743 	/* XXX validate */
1744 	if (rate == 0) {
1745 		/* XXX fall back to mcast/mgmt rate? */
1746 		m_freem(m0);
1747 		return EINVAL;
1748 	}
1749 
1750 	flags = 0;
1751 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1752 		flags |= RT2560_TX_ACK;
1753 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1754 		error = rt2560_sendprot(sc, m0, ni,
1755 		    params->ibp_flags & IEEE80211_BPF_RTS ?
1756 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1757 		    rate);
1758 		if (error) {
1759 			m_freem(m0);
1760 			return error;
1761 		}
1762 		flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1763 	}
1764 
1765 	error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1766 	    segs, &nsegs, 0);
1767 	if (error != 0) {
1768 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1769 		    error);
1770 		m_freem(m0);
1771 		return error;
1772 	}
1773 
1774 	if (bpf_peers_present(ifp->if_bpf)) {
1775 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1776 
1777 		tap->wt_flags = 0;
1778 		tap->wt_rate = rate;
1779 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1780 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1781 		tap->wt_antenna = sc->tx_ant;
1782 
1783 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1784 	}
1785 
1786 	data->m = m0;
1787 	data->ni = ni;
1788 
1789 	/* XXX need to setup descriptor ourself */
1790 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1791 	    rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1792 	    segs->ds_addr);
1793 
1794 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1795 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1796 	    BUS_DMASYNC_PREWRITE);
1797 
1798 	DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1799 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1800 
1801 	/* kick prio */
1802 	sc->prioq.queued++;
1803 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1804 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1805 
1806 	return 0;
1807 }
1808 
1809 static int
1810 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1811     struct ieee80211_node *ni)
1812 {
1813 	struct ieee80211vap *vap = ni->ni_vap;
1814 	struct ieee80211com *ic = ni->ni_ic;
1815 	struct ifnet *ifp = sc->sc_ifp;
1816 	struct rt2560_tx_desc *desc;
1817 	struct rt2560_tx_data *data;
1818 	struct ieee80211_frame *wh;
1819 	const struct ieee80211_txparam *tp;
1820 	struct ieee80211_key *k;
1821 	struct mbuf *mnew;
1822 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1823 	uint16_t dur;
1824 	uint32_t flags;
1825 	int nsegs, rate, error;
1826 
1827 	wh = mtod(m0, struct ieee80211_frame *);
1828 
1829 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1830 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1831 		rate = tp->mcastrate;
1832 	} else if (m0->m_flags & M_EAPOL) {
1833 		rate = tp->mgmtrate;
1834 	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1835 		rate = tp->ucastrate;
1836 	} else {
1837 		(void) ieee80211_amrr_choose(ni, &RT2560_NODE(ni)->amrr);
1838 		rate = ni->ni_txrate;
1839 	}
1840 
1841 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1842 		k = ieee80211_crypto_encap(ni, m0);
1843 		if (k == NULL) {
1844 			m_freem(m0);
1845 			return ENOBUFS;
1846 		}
1847 
1848 		/* packet header may have moved, reset our local pointer */
1849 		wh = mtod(m0, struct ieee80211_frame *);
1850 	}
1851 
1852 	flags = 0;
1853 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1854 		int prot = IEEE80211_PROT_NONE;
1855 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1856 			prot = IEEE80211_PROT_RTSCTS;
1857 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1858 		    ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
1859 			prot = ic->ic_protmode;
1860 		if (prot != IEEE80211_PROT_NONE) {
1861 			error = rt2560_sendprot(sc, m0, ni, prot, rate);
1862 			if (error) {
1863 				m_freem(m0);
1864 				return error;
1865 			}
1866 			flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1867 		}
1868 	}
1869 
1870 	data = &sc->txq.data[sc->txq.cur_encrypt];
1871 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1872 
1873 	error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1874 	    segs, &nsegs, 0);
1875 	if (error != 0 && error != EFBIG) {
1876 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1877 		    error);
1878 		m_freem(m0);
1879 		return error;
1880 	}
1881 	if (error != 0) {
1882 		mnew = m_defrag(m0, M_DONTWAIT);
1883 		if (mnew == NULL) {
1884 			device_printf(sc->sc_dev,
1885 			    "could not defragment mbuf\n");
1886 			m_freem(m0);
1887 			return ENOBUFS;
1888 		}
1889 		m0 = mnew;
1890 
1891 		error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1892 		    m0, segs, &nsegs, 0);
1893 		if (error != 0) {
1894 			device_printf(sc->sc_dev,
1895 			    "could not map mbuf (error %d)\n", error);
1896 			m_freem(m0);
1897 			return error;
1898 		}
1899 
1900 		/* packet header may have moved, reset our local pointer */
1901 		wh = mtod(m0, struct ieee80211_frame *);
1902 	}
1903 
1904 	if (bpf_peers_present(ifp->if_bpf)) {
1905 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1906 
1907 		tap->wt_flags = 0;
1908 		tap->wt_rate = rate;
1909 		tap->wt_antenna = sc->tx_ant;
1910 
1911 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1912 	}
1913 
1914 	data->m = m0;
1915 	data->ni = ni;
1916 
1917 	/* remember link conditions for rate adaptation algorithm */
1918 	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1919 		data->rix = ni->ni_txrate;
1920 		/* XXX probably need last rssi value and not avg */
1921 		data->rssi = ic->ic_node_getrssi(ni);
1922 	} else
1923 		data->rix = IEEE80211_FIXED_RATE_NONE;
1924 
1925 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1926 		flags |= RT2560_TX_ACK;
1927 
1928 		dur = ieee80211_ack_duration(sc->sc_rates,
1929 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1930 		*(uint16_t *)wh->i_dur = htole16(dur);
1931 	}
1932 
1933 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1934 	    segs->ds_addr);
1935 
1936 	bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1937 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1938 	    BUS_DMASYNC_PREWRITE);
1939 
1940 	DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1941 	    m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1942 
1943 	/* kick encrypt */
1944 	sc->txq.queued++;
1945 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1946 	RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1947 
1948 	return 0;
1949 }
1950 
1951 static void
1952 rt2560_start_locked(struct ifnet *ifp)
1953 {
1954 	struct rt2560_softc *sc = ifp->if_softc;
1955 	struct mbuf *m;
1956 	struct ieee80211_node *ni;
1957 
1958 	RAL_LOCK_ASSERT(sc);
1959 
1960 	for (;;) {
1961 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1962 		if (m == NULL)
1963 			break;
1964 		if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1965 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1966 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1967 			sc->sc_flags |= RT2560_F_DATA_OACTIVE;
1968 			break;
1969 		}
1970 
1971 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1972 		m = ieee80211_encap(ni, m);
1973 		if (m == NULL) {
1974 			ieee80211_free_node(ni);
1975 			ifp->if_oerrors++;
1976 			continue;
1977 		}
1978 
1979 		if (rt2560_tx_data(sc, m, ni) != 0) {
1980 			ieee80211_free_node(ni);
1981 			ifp->if_oerrors++;
1982 			break;
1983 		}
1984 
1985 		sc->sc_tx_timer = 5;
1986 	}
1987 }
1988 
1989 static void
1990 rt2560_start(struct ifnet *ifp)
1991 {
1992 	struct rt2560_softc *sc = ifp->if_softc;
1993 
1994 	RAL_LOCK(sc);
1995 	rt2560_start_locked(ifp);
1996 	RAL_UNLOCK(sc);
1997 }
1998 
1999 static void
2000 rt2560_watchdog(void *arg)
2001 {
2002 	struct rt2560_softc *sc = arg;
2003 	struct ifnet *ifp = sc->sc_ifp;
2004 
2005 	RAL_LOCK_ASSERT(sc);
2006 
2007 	KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING, ("not running"));
2008 
2009 	if (sc->sc_invalid)		/* card ejected */
2010 		return;
2011 
2012 	rt2560_encryption_intr(sc);
2013 	rt2560_tx_intr(sc);
2014 
2015 	if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
2016 		if_printf(ifp, "device timeout\n");
2017 		rt2560_init_locked(sc);
2018 		ifp->if_oerrors++;
2019 		/* NB: callout is reset in rt2560_init() */
2020 		return;
2021 	}
2022 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2023 }
2024 
2025 static int
2026 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2027 {
2028 	struct rt2560_softc *sc = ifp->if_softc;
2029 	struct ieee80211com *ic = ifp->if_l2com;
2030 	struct ifreq *ifr = (struct ifreq *) data;
2031 	int error = 0, startall = 0;
2032 
2033 	switch (cmd) {
2034 	case SIOCSIFFLAGS:
2035 		RAL_LOCK(sc);
2036 		if (ifp->if_flags & IFF_UP) {
2037 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2038 				rt2560_init_locked(sc);
2039 				startall = 1;
2040 			} else
2041 				rt2560_update_promisc(ifp);
2042 		} else {
2043 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2044 				rt2560_stop_locked(sc);
2045 		}
2046 		RAL_UNLOCK(sc);
2047 		if (startall)
2048 			ieee80211_start_all(ic);
2049 		break;
2050 	case SIOCGIFMEDIA:
2051 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2052 		break;
2053 	case SIOCGIFADDR:
2054 		error = ether_ioctl(ifp, cmd, data);
2055 		break;
2056 	default:
2057 		error = EINVAL;
2058 		break;
2059 	}
2060 	return error;
2061 }
2062 
2063 static void
2064 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2065 {
2066 	uint32_t tmp;
2067 	int ntries;
2068 
2069 	for (ntries = 0; ntries < 100; ntries++) {
2070 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2071 			break;
2072 		DELAY(1);
2073 	}
2074 	if (ntries == 100) {
2075 		device_printf(sc->sc_dev, "could not write to BBP\n");
2076 		return;
2077 	}
2078 
2079 	tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2080 	RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2081 
2082 	DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
2083 }
2084 
2085 static uint8_t
2086 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2087 {
2088 	uint32_t val;
2089 	int ntries;
2090 
2091 	for (ntries = 0; ntries < 100; ntries++) {
2092 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2093 			break;
2094 		DELAY(1);
2095 	}
2096 	if (ntries == 100) {
2097 		device_printf(sc->sc_dev, "could not read from BBP\n");
2098 		return 0;
2099 	}
2100 
2101 	val = RT2560_BBP_BUSY | reg << 8;
2102 	RAL_WRITE(sc, RT2560_BBPCSR, val);
2103 
2104 	for (ntries = 0; ntries < 100; ntries++) {
2105 		val = RAL_READ(sc, RT2560_BBPCSR);
2106 		if (!(val & RT2560_BBP_BUSY))
2107 			return val & 0xff;
2108 		DELAY(1);
2109 	}
2110 
2111 	device_printf(sc->sc_dev, "could not read from BBP\n");
2112 	return 0;
2113 }
2114 
2115 static void
2116 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2117 {
2118 	uint32_t tmp;
2119 	int ntries;
2120 
2121 	for (ntries = 0; ntries < 100; ntries++) {
2122 		if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2123 			break;
2124 		DELAY(1);
2125 	}
2126 	if (ntries == 100) {
2127 		device_printf(sc->sc_dev, "could not write to RF\n");
2128 		return;
2129 	}
2130 
2131 	tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2132 	    (reg & 0x3);
2133 	RAL_WRITE(sc, RT2560_RFCSR, tmp);
2134 
2135 	/* remember last written value in sc */
2136 	sc->rf_regs[reg] = val;
2137 
2138 	DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2139 }
2140 
2141 static void
2142 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2143 {
2144 	struct ifnet *ifp = sc->sc_ifp;
2145 	struct ieee80211com *ic = ifp->if_l2com;
2146 	uint8_t power, tmp;
2147 	u_int i, chan;
2148 
2149 	chan = ieee80211_chan2ieee(ic, c);
2150 	KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
2151 
2152 	sc->sc_rates = ieee80211_get_ratetable(c);
2153 
2154 	if (IEEE80211_IS_CHAN_2GHZ(c))
2155 		power = min(sc->txpow[chan - 1], 31);
2156 	else
2157 		power = 31;
2158 
2159 	/* adjust txpower using ifconfig settings */
2160 	power -= (100 - ic->ic_txpowlimit) / 8;
2161 
2162 	DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2163 
2164 	switch (sc->rf_rev) {
2165 	case RT2560_RF_2522:
2166 		rt2560_rf_write(sc, RAL_RF1, 0x00814);
2167 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2168 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2169 		break;
2170 
2171 	case RT2560_RF_2523:
2172 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2173 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2174 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2175 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2176 		break;
2177 
2178 	case RT2560_RF_2524:
2179 		rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2180 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2181 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2182 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2183 		break;
2184 
2185 	case RT2560_RF_2525:
2186 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2187 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2188 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2189 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2190 
2191 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2192 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2193 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2194 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2195 		break;
2196 
2197 	case RT2560_RF_2525E:
2198 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2199 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2200 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2201 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2202 		break;
2203 
2204 	case RT2560_RF_2526:
2205 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2206 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2207 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2208 
2209 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2210 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2211 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2212 		break;
2213 
2214 	/* dual-band RF */
2215 	case RT2560_RF_5222:
2216 		for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2217 
2218 		rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2219 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2220 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2221 		rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2222 		break;
2223 	default:
2224  	        printf("unknown ral rev=%d\n", sc->rf_rev);
2225 	}
2226 
2227 	/* XXX */
2228 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2229 		/* set Japan filter bit for channel 14 */
2230 		tmp = rt2560_bbp_read(sc, 70);
2231 
2232 		tmp &= ~RT2560_JAPAN_FILTER;
2233 		if (chan == 14)
2234 			tmp |= RT2560_JAPAN_FILTER;
2235 
2236 		rt2560_bbp_write(sc, 70, tmp);
2237 
2238 		/* clear CRC errors */
2239 		RAL_READ(sc, RT2560_CNT0);
2240 	}
2241 }
2242 
2243 static void
2244 rt2560_set_channel(struct ieee80211com *ic)
2245 {
2246 	struct ifnet *ifp = ic->ic_ifp;
2247 	struct rt2560_softc *sc = ifp->if_softc;
2248 
2249 	RAL_LOCK(sc);
2250 	rt2560_set_chan(sc, ic->ic_curchan);
2251 
2252 	sc->sc_txtap.wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2253 	sc->sc_txtap.wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2254 	sc->sc_rxtap.wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2255 	sc->sc_rxtap.wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2256 	RAL_UNLOCK(sc);
2257 
2258 }
2259 
2260 #if 0
2261 /*
2262  * Disable RF auto-tuning.
2263  */
2264 static void
2265 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2266 {
2267 	uint32_t tmp;
2268 
2269 	if (sc->rf_rev != RT2560_RF_2523) {
2270 		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2271 		rt2560_rf_write(sc, RAL_RF1, tmp);
2272 	}
2273 
2274 	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2275 	rt2560_rf_write(sc, RAL_RF3, tmp);
2276 
2277 	DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2278 }
2279 #endif
2280 
2281 /*
2282  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2283  * synchronization.
2284  */
2285 static void
2286 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2287 {
2288 	struct ifnet *ifp = sc->sc_ifp;
2289 	struct ieee80211com *ic = ifp->if_l2com;
2290 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2291 	uint16_t logcwmin, preload;
2292 	uint32_t tmp;
2293 
2294 	/* first, disable TSF synchronization */
2295 	RAL_WRITE(sc, RT2560_CSR14, 0);
2296 
2297 	tmp = 16 * vap->iv_bss->ni_intval;
2298 	RAL_WRITE(sc, RT2560_CSR12, tmp);
2299 
2300 	RAL_WRITE(sc, RT2560_CSR13, 0);
2301 
2302 	logcwmin = 5;
2303 	preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024;
2304 	tmp = logcwmin << 16 | preload;
2305 	RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2306 
2307 	/* finally, enable TSF synchronization */
2308 	tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2309 	if (ic->ic_opmode == IEEE80211_M_STA)
2310 		tmp |= RT2560_ENABLE_TSF_SYNC(1);
2311 	else
2312 		tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2313 		       RT2560_ENABLE_BEACON_GENERATOR;
2314 	RAL_WRITE(sc, RT2560_CSR14, tmp);
2315 
2316 	DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2317 }
2318 
2319 static void
2320 rt2560_update_plcp(struct rt2560_softc *sc)
2321 {
2322 	struct ifnet *ifp = sc->sc_ifp;
2323 	struct ieee80211com *ic = ifp->if_l2com;
2324 
2325 	/* no short preamble for 1Mbps */
2326 	RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2327 
2328 	if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2329 		/* values taken from the reference driver */
2330 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
2331 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2332 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
2333 	} else {
2334 		/* same values as above or'ed 0x8 */
2335 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
2336 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2337 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
2338 	}
2339 
2340 	DPRINTF(sc, "updating PLCP for %s preamble\n",
2341 	    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long");
2342 }
2343 
2344 /*
2345  * This function can be called by ieee80211_set_shortslottime(). Refer to
2346  * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2347  */
2348 static void
2349 rt2560_update_slot(struct ifnet *ifp)
2350 {
2351 	struct rt2560_softc *sc = ifp->if_softc;
2352 	struct ieee80211com *ic = ifp->if_l2com;
2353 	uint8_t slottime;
2354 	uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2355 	uint32_t tmp;
2356 
2357 #ifndef FORCE_SLOTTIME
2358 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2359 #else
2360 	/*
2361 	 * Setting slot time according to "short slot time" capability
2362 	 * in beacon/probe_resp seems to cause problem to acknowledge
2363 	 * certain AP's data frames transimitted at CCK/DS rates: the
2364 	 * problematic AP keeps retransmitting data frames, probably
2365 	 * because MAC level acks are not received by hardware.
2366 	 * So we cheat a little bit here by claiming we are capable of
2367 	 * "short slot time" but setting hardware slot time to the normal
2368 	 * slot time.  ral(4) does not seem to have trouble to receive
2369 	 * frames transmitted using short slot time even if hardware
2370 	 * slot time is set to normal slot time.  If we didn't use this
2371 	 * trick, we would have to claim that short slot time is not
2372 	 * supported; this would give relative poor RX performance
2373 	 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2374 	 * slot time.
2375 	 */
2376 	slottime = 20;
2377 #endif
2378 
2379 	/* update the MAC slot boundaries */
2380 	tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2381 	tx_pifs = tx_sifs + slottime;
2382 	tx_difs = tx_sifs + 2 * slottime;
2383 	eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2384 
2385 	tmp = RAL_READ(sc, RT2560_CSR11);
2386 	tmp = (tmp & ~0x1f00) | slottime << 8;
2387 	RAL_WRITE(sc, RT2560_CSR11, tmp);
2388 
2389 	tmp = tx_pifs << 16 | tx_sifs;
2390 	RAL_WRITE(sc, RT2560_CSR18, tmp);
2391 
2392 	tmp = eifs << 16 | tx_difs;
2393 	RAL_WRITE(sc, RT2560_CSR19, tmp);
2394 
2395 	DPRINTF(sc, "setting slottime to %uus\n", slottime);
2396 }
2397 
2398 static void
2399 rt2560_set_basicrates(struct rt2560_softc *sc)
2400 {
2401 	struct ifnet *ifp = sc->sc_ifp;
2402 	struct ieee80211com *ic = ifp->if_l2com;
2403 
2404 	/* update basic rate set */
2405 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
2406 		/* 11b basic rates: 1, 2Mbps */
2407 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2408 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2409 		/* 11a basic rates: 6, 12, 24Mbps */
2410 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2411 	} else {
2412 		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2413 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2414 	}
2415 }
2416 
2417 static void
2418 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2419 {
2420 	uint32_t tmp;
2421 
2422 	/* set ON period to 70ms and OFF period to 30ms */
2423 	tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2424 	RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2425 }
2426 
2427 static void
2428 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2429 {
2430 	uint32_t tmp;
2431 
2432 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2433 	RAL_WRITE(sc, RT2560_CSR5, tmp);
2434 
2435 	tmp = bssid[4] | bssid[5] << 8;
2436 	RAL_WRITE(sc, RT2560_CSR6, tmp);
2437 
2438 	DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2439 }
2440 
2441 static void
2442 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2443 {
2444 	uint32_t tmp;
2445 
2446 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2447 	RAL_WRITE(sc, RT2560_CSR3, tmp);
2448 
2449 	tmp = addr[4] | addr[5] << 8;
2450 	RAL_WRITE(sc, RT2560_CSR4, tmp);
2451 
2452 	DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2453 }
2454 
2455 static void
2456 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2457 {
2458 	uint32_t tmp;
2459 
2460 	tmp = RAL_READ(sc, RT2560_CSR3);
2461 	addr[0] = tmp & 0xff;
2462 	addr[1] = (tmp >>  8) & 0xff;
2463 	addr[2] = (tmp >> 16) & 0xff;
2464 	addr[3] = (tmp >> 24);
2465 
2466 	tmp = RAL_READ(sc, RT2560_CSR4);
2467 	addr[4] = tmp & 0xff;
2468 	addr[5] = (tmp >> 8) & 0xff;
2469 }
2470 
2471 static void
2472 rt2560_update_promisc(struct ifnet *ifp)
2473 {
2474 	struct rt2560_softc *sc = ifp->if_softc;
2475 	uint32_t tmp;
2476 
2477 	tmp = RAL_READ(sc, RT2560_RXCSR0);
2478 
2479 	tmp &= ~RT2560_DROP_NOT_TO_ME;
2480 	if (!(ifp->if_flags & IFF_PROMISC))
2481 		tmp |= RT2560_DROP_NOT_TO_ME;
2482 
2483 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2484 
2485 	DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2486 	    "entering" : "leaving");
2487 }
2488 
2489 static const char *
2490 rt2560_get_rf(int rev)
2491 {
2492 	switch (rev) {
2493 	case RT2560_RF_2522:	return "RT2522";
2494 	case RT2560_RF_2523:	return "RT2523";
2495 	case RT2560_RF_2524:	return "RT2524";
2496 	case RT2560_RF_2525:	return "RT2525";
2497 	case RT2560_RF_2525E:	return "RT2525e";
2498 	case RT2560_RF_2526:	return "RT2526";
2499 	case RT2560_RF_5222:	return "RT5222";
2500 	default:		return "unknown";
2501 	}
2502 }
2503 
2504 static void
2505 rt2560_read_config(struct rt2560_softc *sc)
2506 {
2507 	uint16_t val;
2508 	int i;
2509 
2510 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2511 	sc->rf_rev =   (val >> 11) & 0x7;
2512 	sc->hw_radio = (val >> 10) & 0x1;
2513 	sc->led_mode = (val >> 6)  & 0x7;
2514 	sc->rx_ant =   (val >> 4)  & 0x3;
2515 	sc->tx_ant =   (val >> 2)  & 0x3;
2516 	sc->nb_ant =   val & 0x3;
2517 
2518 	/* read default values for BBP registers */
2519 	for (i = 0; i < 16; i++) {
2520 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2521 		if (val == 0 || val == 0xffff)
2522 			continue;
2523 
2524 		sc->bbp_prom[i].reg = val >> 8;
2525 		sc->bbp_prom[i].val = val & 0xff;
2526 	}
2527 
2528 	/* read Tx power for all b/g channels */
2529 	for (i = 0; i < 14 / 2; i++) {
2530 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2531 		sc->txpow[i * 2] = val & 0xff;
2532 		sc->txpow[i * 2 + 1] = val >> 8;
2533 	}
2534 	for (i = 0; i < 14; ++i) {
2535 		if (sc->txpow[i] > 31)
2536 			sc->txpow[i] = 24;
2537 	}
2538 
2539 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2540 	if ((val & 0xff) == 0xff)
2541 		sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2542 	else
2543 		sc->rssi_corr = val & 0xff;
2544 	DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2545 		 sc->rssi_corr, val);
2546 }
2547 
2548 
2549 static void
2550 rt2560_scan_start(struct ieee80211com *ic)
2551 {
2552 	struct ifnet *ifp = ic->ic_ifp;
2553 	struct rt2560_softc *sc = ifp->if_softc;
2554 
2555 	/* abort TSF synchronization */
2556 	RAL_WRITE(sc, RT2560_CSR14, 0);
2557 	rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2558 }
2559 
2560 static void
2561 rt2560_scan_end(struct ieee80211com *ic)
2562 {
2563 	struct ifnet *ifp = ic->ic_ifp;
2564 	struct rt2560_softc *sc = ifp->if_softc;
2565 	struct ieee80211vap *vap = ic->ic_scan->ss_vap;
2566 
2567 	rt2560_enable_tsf_sync(sc);
2568 	/* XXX keep local copy */
2569 	rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2570 }
2571 
2572 static int
2573 rt2560_bbp_init(struct rt2560_softc *sc)
2574 {
2575 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2576 	int i, ntries;
2577 
2578 	/* wait for BBP to be ready */
2579 	for (ntries = 0; ntries < 100; ntries++) {
2580 		if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2581 			break;
2582 		DELAY(1);
2583 	}
2584 	if (ntries == 100) {
2585 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2586 		return EIO;
2587 	}
2588 
2589 	/* initialize BBP registers to default values */
2590 	for (i = 0; i < N(rt2560_def_bbp); i++) {
2591 		rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2592 		    rt2560_def_bbp[i].val);
2593 	}
2594 
2595 	/* initialize BBP registers to values stored in EEPROM */
2596 	for (i = 0; i < 16; i++) {
2597 		if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2598 			break;
2599 		rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2600 	}
2601 	rt2560_bbp_write(sc, 17, 0x48);	/* XXX restore bbp17 */
2602 
2603 	return 0;
2604 #undef N
2605 }
2606 
2607 static void
2608 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2609 {
2610 	uint32_t tmp;
2611 	uint8_t tx;
2612 
2613 	tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2614 	if (antenna == 1)
2615 		tx |= RT2560_BBP_ANTA;
2616 	else if (antenna == 2)
2617 		tx |= RT2560_BBP_ANTB;
2618 	else
2619 		tx |= RT2560_BBP_DIVERSITY;
2620 
2621 	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2622 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2623 	    sc->rf_rev == RT2560_RF_5222)
2624 		tx |= RT2560_BBP_FLIPIQ;
2625 
2626 	rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2627 
2628 	/* update values for CCK and OFDM in BBPCSR1 */
2629 	tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2630 	tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2631 	RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2632 }
2633 
2634 static void
2635 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2636 {
2637 	uint8_t rx;
2638 
2639 	rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2640 	if (antenna == 1)
2641 		rx |= RT2560_BBP_ANTA;
2642 	else if (antenna == 2)
2643 		rx |= RT2560_BBP_ANTB;
2644 	else
2645 		rx |= RT2560_BBP_DIVERSITY;
2646 
2647 	/* need to force no I/Q flip for RF 2525e and 2526 */
2648 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2649 		rx &= ~RT2560_BBP_FLIPIQ;
2650 
2651 	rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2652 }
2653 
2654 static void
2655 rt2560_init_locked(struct rt2560_softc *sc)
2656 {
2657 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2658 	struct ifnet *ifp = sc->sc_ifp;
2659 	struct ieee80211com *ic = ifp->if_l2com;
2660 	uint32_t tmp;
2661 	int i;
2662 
2663 	RAL_LOCK_ASSERT(sc);
2664 
2665 	rt2560_stop_locked(sc);
2666 
2667 	/* setup tx rings */
2668 	tmp = RT2560_PRIO_RING_COUNT << 24 |
2669 	      RT2560_ATIM_RING_COUNT << 16 |
2670 	      RT2560_TX_RING_COUNT   <<  8 |
2671 	      RT2560_TX_DESC_SIZE;
2672 
2673 	/* rings must be initialized in this exact order */
2674 	RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2675 	RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2676 	RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2677 	RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2678 	RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2679 
2680 	/* setup rx ring */
2681 	tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2682 
2683 	RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2684 	RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2685 
2686 	/* initialize MAC registers to default values */
2687 	for (i = 0; i < N(rt2560_def_mac); i++)
2688 		RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2689 
2690 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2691 	rt2560_set_macaddr(sc, ic->ic_myaddr);
2692 
2693 	/* set basic rate set (will be updated later) */
2694 	RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2695 
2696 	rt2560_update_slot(ifp);
2697 	rt2560_update_plcp(sc);
2698 	rt2560_update_led(sc, 0, 0);
2699 
2700 	RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2701 	RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2702 
2703 	if (rt2560_bbp_init(sc) != 0) {
2704 		rt2560_stop(sc);
2705 		RAL_UNLOCK(sc);
2706 		return;
2707 	}
2708 
2709 	rt2560_set_txantenna(sc, sc->tx_ant);
2710 	rt2560_set_rxantenna(sc, sc->rx_ant);
2711 
2712 	/* set default BSS channel */
2713 	rt2560_set_chan(sc, ic->ic_curchan);
2714 
2715 	/* kick Rx */
2716 	tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2717 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2718 		tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2719 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2720 			tmp |= RT2560_DROP_TODS;
2721 		if (!(ifp->if_flags & IFF_PROMISC))
2722 			tmp |= RT2560_DROP_NOT_TO_ME;
2723 	}
2724 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2725 
2726 	/* clear old FCS and Rx FIFO errors */
2727 	RAL_READ(sc, RT2560_CNT0);
2728 	RAL_READ(sc, RT2560_CNT4);
2729 
2730 	/* clear any pending interrupts */
2731 	RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2732 
2733 	/* enable interrupts */
2734 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2735 
2736 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2737 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2738 
2739 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2740 #undef N
2741 }
2742 
2743 static void
2744 rt2560_init(void *priv)
2745 {
2746 	struct rt2560_softc *sc = priv;
2747 	struct ifnet *ifp = sc->sc_ifp;
2748 	struct ieee80211com *ic = ifp->if_l2com;
2749 
2750 	RAL_LOCK(sc);
2751 	rt2560_init_locked(sc);
2752 	RAL_UNLOCK(sc);
2753 
2754 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2755 		ieee80211_start_all(ic);		/* start all vap's */
2756 }
2757 
2758 static void
2759 rt2560_stop_locked(struct rt2560_softc *sc)
2760 {
2761 	struct ifnet *ifp = sc->sc_ifp;
2762 	volatile int *flags = &sc->sc_flags;
2763 
2764 	RAL_LOCK_ASSERT(sc);
2765 
2766 	while (*flags & RT2560_F_INPUT_RUNNING)
2767 		msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10);
2768 
2769 	callout_stop(&sc->watchdog_ch);
2770 	sc->sc_tx_timer = 0;
2771 
2772 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2773 		ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2774 
2775 		/* abort Tx */
2776 		RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2777 
2778 		/* disable Rx */
2779 		RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2780 
2781 		/* reset ASIC (imply reset BBP) */
2782 		RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2783 		RAL_WRITE(sc, RT2560_CSR1, 0);
2784 
2785 		/* disable interrupts */
2786 		RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2787 
2788 		/* reset Tx and Rx rings */
2789 		rt2560_reset_tx_ring(sc, &sc->txq);
2790 		rt2560_reset_tx_ring(sc, &sc->atimq);
2791 		rt2560_reset_tx_ring(sc, &sc->prioq);
2792 		rt2560_reset_tx_ring(sc, &sc->bcnq);
2793 		rt2560_reset_rx_ring(sc, &sc->rxq);
2794 	}
2795 	sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2796 }
2797 
2798 void
2799 rt2560_stop(void *arg)
2800 {
2801 	struct rt2560_softc *sc = arg;
2802 
2803 	RAL_LOCK(sc);
2804 	rt2560_stop_locked(sc);
2805 	RAL_UNLOCK(sc);
2806 }
2807 
2808 static int
2809 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2810 	const struct ieee80211_bpf_params *params)
2811 {
2812 	struct ieee80211com *ic = ni->ni_ic;
2813 	struct ifnet *ifp = ic->ic_ifp;
2814 	struct rt2560_softc *sc = ifp->if_softc;
2815 
2816 	RAL_LOCK(sc);
2817 
2818 	/* prevent management frames from being sent if we're not ready */
2819 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2820 		RAL_UNLOCK(sc);
2821 		m_freem(m);
2822 		ieee80211_free_node(ni);
2823 		return ENETDOWN;
2824 	}
2825 	if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2826 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2827 		sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2828 		RAL_UNLOCK(sc);
2829 		m_freem(m);
2830 		ieee80211_free_node(ni);
2831 		return ENOBUFS;		/* XXX */
2832 	}
2833 
2834 	ifp->if_opackets++;
2835 
2836 	if (params == NULL) {
2837 		/*
2838 		 * Legacy path; interpret frame contents to decide
2839 		 * precisely how to send the frame.
2840 		 */
2841 		if (rt2560_tx_mgt(sc, m, ni) != 0)
2842 			goto bad;
2843 	} else {
2844 		/*
2845 		 * Caller supplied explicit parameters to use in
2846 		 * sending the frame.
2847 		 */
2848 		if (rt2560_tx_raw(sc, m, ni, params))
2849 			goto bad;
2850 	}
2851 	sc->sc_tx_timer = 5;
2852 
2853 	RAL_UNLOCK(sc);
2854 
2855 	return 0;
2856 bad:
2857 	ifp->if_oerrors++;
2858 	ieee80211_free_node(ni);
2859 	RAL_UNLOCK(sc);
2860 	return EIO;		/* XXX */
2861 }
2862