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