xref: /freebsd/sys/dev/ral/rt2560.c (revision 6c6c03be2ddb04c54e455122799923deaefa4114)
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(struct ieee80211vap *,
108 			    const uint8_t [IEEE80211_ADDR_LEN]);
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 ieee80211vap *vap,
771 	const uint8_t mac[IEEE80211_ADDR_LEN])
772 {
773 	struct rt2560_node *rn;
774 
775 	rn = malloc(sizeof (struct rt2560_node), M_80211_NODE,
776 	    M_NOWAIT | M_ZERO);
777 
778 	return (rn != NULL) ? &rn->ni : NULL;
779 }
780 
781 static void
782 rt2560_newassoc(struct ieee80211_node *ni, int isnew)
783 {
784 	struct ieee80211vap *vap = ni->ni_vap;
785 
786 	ieee80211_amrr_node_init(&RT2560_VAP(vap)->amrr,
787 	    &RT2560_NODE(ni)->amrr, ni);
788 }
789 
790 static int
791 rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
792 {
793 	struct rt2560_vap *rvp = RT2560_VAP(vap);
794 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
795 	struct rt2560_softc *sc = ifp->if_softc;
796 	int error;
797 
798 	if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
799 		/* abort TSF synchronization */
800 		RAL_WRITE(sc, RT2560_CSR14, 0);
801 
802 		/* turn association led off */
803 		rt2560_update_led(sc, 0, 0);
804 	}
805 
806 	error = rvp->ral_newstate(vap, nstate, arg);
807 
808 	if (error == 0 && nstate == IEEE80211_S_RUN) {
809 		struct ieee80211_node *ni = vap->iv_bss;
810 		struct mbuf *m;
811 
812 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
813 			rt2560_update_plcp(sc);
814 			rt2560_set_basicrates(sc);
815 			rt2560_set_bssid(sc, ni->ni_bssid);
816 		}
817 
818 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
819 		    vap->iv_opmode == IEEE80211_M_IBSS) {
820 			m = ieee80211_beacon_alloc(ni, &rvp->ral_bo);
821 			if (m == NULL) {
822 				if_printf(ifp, "could not allocate beacon\n");
823 				return ENOBUFS;
824 			}
825 			ieee80211_ref_node(ni);
826 			error = rt2560_tx_bcn(sc, m, ni);
827 			if (error != 0)
828 				return error;
829 		}
830 
831 		/* turn assocation led on */
832 		rt2560_update_led(sc, 1, 0);
833 
834 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
835 			rt2560_enable_tsf_sync(sc);
836 	}
837 	return error;
838 }
839 
840 /*
841  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
842  * 93C66).
843  */
844 static uint16_t
845 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
846 {
847 	uint32_t tmp;
848 	uint16_t val;
849 	int n;
850 
851 	/* clock C once before the first command */
852 	RT2560_EEPROM_CTL(sc, 0);
853 
854 	RT2560_EEPROM_CTL(sc, RT2560_S);
855 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
856 	RT2560_EEPROM_CTL(sc, RT2560_S);
857 
858 	/* write start bit (1) */
859 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
860 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
861 
862 	/* write READ opcode (10) */
863 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
864 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
865 	RT2560_EEPROM_CTL(sc, RT2560_S);
866 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
867 
868 	/* write address (A5-A0 or A7-A0) */
869 	n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
870 	for (; n >= 0; n--) {
871 		RT2560_EEPROM_CTL(sc, RT2560_S |
872 		    (((addr >> n) & 1) << RT2560_SHIFT_D));
873 		RT2560_EEPROM_CTL(sc, RT2560_S |
874 		    (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
875 	}
876 
877 	RT2560_EEPROM_CTL(sc, RT2560_S);
878 
879 	/* read data Q15-Q0 */
880 	val = 0;
881 	for (n = 15; n >= 0; n--) {
882 		RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
883 		tmp = RAL_READ(sc, RT2560_CSR21);
884 		val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
885 		RT2560_EEPROM_CTL(sc, RT2560_S);
886 	}
887 
888 	RT2560_EEPROM_CTL(sc, 0);
889 
890 	/* clear Chip Select and clock C */
891 	RT2560_EEPROM_CTL(sc, RT2560_S);
892 	RT2560_EEPROM_CTL(sc, 0);
893 	RT2560_EEPROM_CTL(sc, RT2560_C);
894 
895 	return val;
896 }
897 
898 /*
899  * Some frames were processed by the hardware cipher engine and are ready for
900  * transmission.
901  */
902 static void
903 rt2560_encryption_intr(struct rt2560_softc *sc)
904 {
905 	struct rt2560_tx_desc *desc;
906 	int hw;
907 
908 	/* retrieve last descriptor index processed by cipher engine */
909 	hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
910 	hw /= RT2560_TX_DESC_SIZE;
911 
912 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
913 	    BUS_DMASYNC_POSTREAD);
914 
915 	while (sc->txq.next_encrypt != hw) {
916 		if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
917 			printf("hw encrypt %d, cur_encrypt %d\n", hw,
918 			    sc->txq.cur_encrypt);
919 			break;
920 		}
921 
922 		desc = &sc->txq.desc[sc->txq.next_encrypt];
923 
924 		if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
925 		    (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
926 			break;
927 
928 		/* for TKIP, swap eiv field to fix a bug in ASIC */
929 		if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
930 		    RT2560_TX_CIPHER_TKIP)
931 			desc->eiv = bswap32(desc->eiv);
932 
933 		/* mark the frame ready for transmission */
934 		desc->flags |= htole32(RT2560_TX_VALID);
935 		desc->flags |= htole32(RT2560_TX_BUSY);
936 
937 		DPRINTFN(sc, 15, "encryption done idx=%u\n",
938 		    sc->txq.next_encrypt);
939 
940 		sc->txq.next_encrypt =
941 		    (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
942 	}
943 
944 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
945 	    BUS_DMASYNC_PREWRITE);
946 
947 	/* kick Tx */
948 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
949 }
950 
951 static void
952 rt2560_tx_intr(struct rt2560_softc *sc)
953 {
954 	struct ifnet *ifp = sc->sc_ifp;
955 	struct rt2560_tx_desc *desc;
956 	struct rt2560_tx_data *data;
957 	struct rt2560_node *rn;
958 	struct mbuf *m;
959 	uint32_t flags;
960 	int retrycnt;
961 
962 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
963 	    BUS_DMASYNC_POSTREAD);
964 
965 	for (;;) {
966 		desc = &sc->txq.desc[sc->txq.next];
967 		data = &sc->txq.data[sc->txq.next];
968 
969 		flags = le32toh(desc->flags);
970 		if ((flags & RT2560_TX_BUSY) ||
971 		    (flags & RT2560_TX_CIPHER_BUSY) ||
972 		    !(flags & RT2560_TX_VALID))
973 			break;
974 
975 		rn = (struct rt2560_node *)data->ni;
976 		m = data->m;
977 
978 		switch (flags & RT2560_TX_RESULT_MASK) {
979 		case RT2560_TX_SUCCESS:
980 			DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
981 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
982 				ieee80211_amrr_tx_complete(&rn->amrr,
983 				    IEEE80211_AMRR_SUCCESS, 0);
984 			ifp->if_opackets++;
985 			break;
986 
987 		case RT2560_TX_SUCCESS_RETRY:
988 			retrycnt = RT2560_TX_RETRYCNT(flags);
989 
990 			DPRINTFN(sc, 9, "data frame sent after %u retries\n",
991 			    retrycnt);
992 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
993 				ieee80211_amrr_tx_complete(&rn->amrr,
994 				    IEEE80211_AMRR_SUCCESS, retrycnt);
995 			ifp->if_opackets++;
996 			break;
997 
998 		case RT2560_TX_FAIL_RETRY:
999 			retrycnt = RT2560_TX_RETRYCNT(flags);
1000 
1001 			DPRINTFN(sc, 9, "data frame failed after %d retries\n",
1002 			    retrycnt);
1003 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
1004 				ieee80211_amrr_tx_complete(&rn->amrr,
1005 				    IEEE80211_AMRR_FAILURE, retrycnt);
1006 			ifp->if_oerrors++;
1007 			break;
1008 
1009 		case RT2560_TX_FAIL_INVALID:
1010 		case RT2560_TX_FAIL_OTHER:
1011 		default:
1012 			device_printf(sc->sc_dev, "sending data frame failed "
1013 			    "0x%08x\n", flags);
1014 			ifp->if_oerrors++;
1015 		}
1016 
1017 		bus_dmamap_sync(sc->txq.data_dmat, data->map,
1018 		    BUS_DMASYNC_POSTWRITE);
1019 		bus_dmamap_unload(sc->txq.data_dmat, data->map);
1020 		m_freem(m);
1021 		data->m = NULL;
1022 		ieee80211_free_node(data->ni);
1023 		data->ni = NULL;
1024 
1025 		/* descriptor is no longer valid */
1026 		desc->flags &= ~htole32(RT2560_TX_VALID);
1027 
1028 		DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next);
1029 
1030 		sc->txq.queued--;
1031 		sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1032 	}
1033 
1034 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1035 	    BUS_DMASYNC_PREWRITE);
1036 
1037 	if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1038 		sc->sc_tx_timer = 0;
1039 
1040 	if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1041 		sc->sc_flags &= ~RT2560_F_DATA_OACTIVE;
1042 		if ((sc->sc_flags &
1043 		     (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1044 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1045 		rt2560_start_locked(ifp);
1046 	}
1047 }
1048 
1049 static void
1050 rt2560_prio_intr(struct rt2560_softc *sc)
1051 {
1052 	struct ifnet *ifp = sc->sc_ifp;
1053 	struct rt2560_tx_desc *desc;
1054 	struct rt2560_tx_data *data;
1055 	struct ieee80211_node *ni;
1056 	struct mbuf *m;
1057 	int flags;
1058 
1059 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1060 	    BUS_DMASYNC_POSTREAD);
1061 
1062 	for (;;) {
1063 		desc = &sc->prioq.desc[sc->prioq.next];
1064 		data = &sc->prioq.data[sc->prioq.next];
1065 
1066 		flags = le32toh(desc->flags);
1067 		if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0)
1068 			break;
1069 
1070 		switch (flags & RT2560_TX_RESULT_MASK) {
1071 		case RT2560_TX_SUCCESS:
1072 			DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully");
1073 			break;
1074 
1075 		case RT2560_TX_SUCCESS_RETRY:
1076 			DPRINTFN(sc, 9, "mgt frame sent after %u retries\n",
1077 			    (flags >> 5) & 0x7);
1078 			break;
1079 
1080 		case RT2560_TX_FAIL_RETRY:
1081 			DPRINTFN(sc, 9, "%s\n",
1082 			    "sending mgt frame failed (too much retries)");
1083 			break;
1084 
1085 		case RT2560_TX_FAIL_INVALID:
1086 		case RT2560_TX_FAIL_OTHER:
1087 		default:
1088 			device_printf(sc->sc_dev, "sending mgt frame failed "
1089 			    "0x%08x\n", flags);
1090 			break;
1091 		}
1092 
1093 		bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1094 		    BUS_DMASYNC_POSTWRITE);
1095 		bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1096 
1097 		m = data->m;
1098 		data->m = NULL;
1099 		ni = data->ni;
1100 		data->ni = NULL;
1101 
1102 		/* descriptor is no longer valid */
1103 		desc->flags &= ~htole32(RT2560_TX_VALID);
1104 
1105 		DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next);
1106 
1107 		sc->prioq.queued--;
1108 		sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1109 
1110 		if (m->m_flags & M_TXCB)
1111 			ieee80211_process_callback(ni, m,
1112 				(flags & RT2560_TX_RESULT_MASK) &~
1113 				(RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY));
1114 		m_freem(m);
1115 		ieee80211_free_node(ni);
1116 	}
1117 
1118 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1119 	    BUS_DMASYNC_PREWRITE);
1120 
1121 	if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1122 		sc->sc_tx_timer = 0;
1123 
1124 	if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1125 		sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE;
1126 		if ((sc->sc_flags &
1127 		     (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1128 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1129 		rt2560_start_locked(ifp);
1130 	}
1131 }
1132 
1133 /*
1134  * Some frames were processed by the hardware cipher engine and are ready for
1135  * handoff to the IEEE802.11 layer.
1136  */
1137 static void
1138 rt2560_decryption_intr(struct rt2560_softc *sc)
1139 {
1140 	struct ifnet *ifp = sc->sc_ifp;
1141 	struct ieee80211com *ic = ifp->if_l2com;
1142 	struct rt2560_rx_desc *desc;
1143 	struct rt2560_rx_data *data;
1144 	bus_addr_t physaddr;
1145 	struct ieee80211_frame *wh;
1146 	struct ieee80211_node *ni;
1147 	struct mbuf *mnew, *m;
1148 	int hw, error;
1149 
1150 	/* retrieve last decriptor index processed by cipher engine */
1151 	hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1152 	hw /= RT2560_RX_DESC_SIZE;
1153 
1154 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1155 	    BUS_DMASYNC_POSTREAD);
1156 
1157 	for (; sc->rxq.cur_decrypt != hw;) {
1158 		desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1159 		data = &sc->rxq.data[sc->rxq.cur_decrypt];
1160 
1161 		if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1162 		    (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1163 			break;
1164 
1165 		if (data->drop) {
1166 			ifp->if_ierrors++;
1167 			goto skip;
1168 		}
1169 
1170 		if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1171 		    (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1172 			ifp->if_ierrors++;
1173 			goto skip;
1174 		}
1175 
1176 		/*
1177 		 * Try to allocate a new mbuf for this ring element and load it
1178 		 * before processing the current mbuf. If the ring element
1179 		 * cannot be loaded, drop the received packet and reuse the old
1180 		 * mbuf. In the unlikely case that the old mbuf can't be
1181 		 * reloaded either, explicitly panic.
1182 		 */
1183 		mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1184 		if (mnew == NULL) {
1185 			ifp->if_ierrors++;
1186 			goto skip;
1187 		}
1188 
1189 		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1190 		    BUS_DMASYNC_POSTREAD);
1191 		bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1192 
1193 		error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1194 		    mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1195 		    &physaddr, 0);
1196 		if (error != 0) {
1197 			m_freem(mnew);
1198 
1199 			/* try to reload the old mbuf */
1200 			error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1201 			    mtod(data->m, void *), MCLBYTES,
1202 			    rt2560_dma_map_addr, &physaddr, 0);
1203 			if (error != 0) {
1204 				/* very unlikely that it will fail... */
1205 				panic("%s: could not load old rx mbuf",
1206 				    device_get_name(sc->sc_dev));
1207 			}
1208 			ifp->if_ierrors++;
1209 			goto skip;
1210 		}
1211 
1212 		/*
1213 	 	 * New mbuf successfully loaded, update Rx ring and continue
1214 		 * processing.
1215 		 */
1216 		m = data->m;
1217 		data->m = mnew;
1218 		desc->physaddr = htole32(physaddr);
1219 
1220 		/* finalize mbuf */
1221 		m->m_pkthdr.rcvif = ifp;
1222 		m->m_pkthdr.len = m->m_len =
1223 		    (le32toh(desc->flags) >> 16) & 0xfff;
1224 
1225 		if (bpf_peers_present(ifp->if_bpf)) {
1226 			struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1227 			uint32_t tsf_lo, tsf_hi;
1228 
1229 			/* get timestamp (low and high 32 bits) */
1230 			tsf_hi = RAL_READ(sc, RT2560_CSR17);
1231 			tsf_lo = RAL_READ(sc, RT2560_CSR16);
1232 
1233 			tap->wr_tsf =
1234 			    htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1235 			tap->wr_flags = 0;
1236 			tap->wr_rate = ieee80211_plcp2rate(desc->rate,
1237 			    (desc->flags & htole32(RT2560_RX_OFDM)) ?
1238 				IEEE80211_T_OFDM : IEEE80211_T_CCK);
1239 			tap->wr_antenna = sc->rx_ant;
1240 			tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi);
1241 
1242 			bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
1243 		}
1244 
1245 		sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1246 		RAL_UNLOCK(sc);
1247 		wh = mtod(m, struct ieee80211_frame *);
1248 		ni = ieee80211_find_rxnode(ic,
1249 		    (struct ieee80211_frame_min *)wh);
1250 		if (ni != NULL) {
1251 			(void) ieee80211_input(ni, m,
1252 			    RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0);
1253 			ieee80211_free_node(ni);
1254 		} else
1255 			(void) ieee80211_input_all(ic, m,
1256 			    RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0);
1257 
1258 		RAL_LOCK(sc);
1259 		sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1260 skip:		desc->flags = htole32(RT2560_RX_BUSY);
1261 
1262 		DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1263 
1264 		sc->rxq.cur_decrypt =
1265 		    (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1266 	}
1267 
1268 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1269 	    BUS_DMASYNC_PREWRITE);
1270 }
1271 
1272 /*
1273  * Some frames were received. Pass them to the hardware cipher engine before
1274  * sending them to the 802.11 layer.
1275  */
1276 static void
1277 rt2560_rx_intr(struct rt2560_softc *sc)
1278 {
1279 	struct rt2560_rx_desc *desc;
1280 	struct rt2560_rx_data *data;
1281 
1282 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1283 	    BUS_DMASYNC_POSTREAD);
1284 
1285 	for (;;) {
1286 		desc = &sc->rxq.desc[sc->rxq.cur];
1287 		data = &sc->rxq.data[sc->rxq.cur];
1288 
1289 		if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1290 		    (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1291 			break;
1292 
1293 		data->drop = 0;
1294 
1295 		if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1296 		    (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1297 			/*
1298 			 * This should not happen since we did not request
1299 			 * to receive those frames when we filled RXCSR0.
1300 			 */
1301 			DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1302 			    le32toh(desc->flags));
1303 			data->drop = 1;
1304 		}
1305 
1306 		if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1307 			DPRINTFN(sc, 5, "%s\n", "bad length");
1308 			data->drop = 1;
1309 		}
1310 
1311 		/* mark the frame for decryption */
1312 		desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1313 
1314 		DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1315 
1316 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1317 	}
1318 
1319 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1320 	    BUS_DMASYNC_PREWRITE);
1321 
1322 	/* kick decrypt */
1323 	RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1324 }
1325 
1326 static void
1327 rt2560_beacon_update(struct ieee80211vap *vap, int item)
1328 {
1329 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1330 	struct ieee80211_beacon_offsets *bo = &rvp->ral_bo;
1331 
1332 	setbit(bo->bo_flags, item);
1333 }
1334 
1335 /*
1336  * This function is called periodically in IBSS mode when a new beacon must be
1337  * sent out.
1338  */
1339 static void
1340 rt2560_beacon_expire(struct rt2560_softc *sc)
1341 {
1342 	struct ifnet *ifp = sc->sc_ifp;
1343 	struct ieee80211com *ic = ifp->if_l2com;
1344 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1345 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1346 	struct rt2560_tx_data *data;
1347 
1348 	if (ic->ic_opmode != IEEE80211_M_IBSS &&
1349 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
1350 		return;
1351 
1352 	data = &sc->bcnq.data[sc->bcnq.next];
1353 	/*
1354 	 * Don't send beacon if bsschan isn't set
1355 	 */
1356 	if (data->ni == NULL)
1357 	        return;
1358 
1359 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1360 	bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1361 
1362 	/* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */
1363 	ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1);
1364 
1365 	rt2560_tx_bcn(sc, data->m, data->ni);
1366 
1367 	DPRINTFN(sc, 15, "%s", "beacon expired\n");
1368 
1369 	sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1370 }
1371 
1372 /* ARGSUSED */
1373 static void
1374 rt2560_wakeup_expire(struct rt2560_softc *sc)
1375 {
1376 	DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1377 }
1378 
1379 void
1380 rt2560_intr(void *arg)
1381 {
1382 	struct rt2560_softc *sc = arg;
1383 	struct ifnet *ifp = sc->sc_ifp;
1384 	uint32_t r;
1385 
1386 	RAL_LOCK(sc);
1387 
1388 	/* disable interrupts */
1389 	RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1390 
1391 	/* don't re-enable interrupts if we're shutting down */
1392 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1393 		RAL_UNLOCK(sc);
1394 		return;
1395 	}
1396 
1397 	r = RAL_READ(sc, RT2560_CSR7);
1398 	RAL_WRITE(sc, RT2560_CSR7, r);
1399 
1400 	if (r & RT2560_BEACON_EXPIRE)
1401 		rt2560_beacon_expire(sc);
1402 
1403 	if (r & RT2560_WAKEUP_EXPIRE)
1404 		rt2560_wakeup_expire(sc);
1405 
1406 	if (r & RT2560_ENCRYPTION_DONE)
1407 		rt2560_encryption_intr(sc);
1408 
1409 	if (r & RT2560_TX_DONE)
1410 		rt2560_tx_intr(sc);
1411 
1412 	if (r & RT2560_PRIO_DONE)
1413 		rt2560_prio_intr(sc);
1414 
1415 	if (r & RT2560_DECRYPTION_DONE)
1416 		rt2560_decryption_intr(sc);
1417 
1418 	if (r & RT2560_RX_DONE) {
1419 		rt2560_rx_intr(sc);
1420 		rt2560_encryption_intr(sc);
1421 	}
1422 
1423 	/* re-enable interrupts */
1424 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1425 
1426 	RAL_UNLOCK(sc);
1427 }
1428 
1429 #define RAL_SIFS		10	/* us */
1430 
1431 #define RT2560_TXRX_TURNAROUND	10	/* us */
1432 
1433 static uint8_t
1434 rt2560_plcp_signal(int rate)
1435 {
1436 	switch (rate) {
1437 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1438 	case 12:	return 0xb;
1439 	case 18:	return 0xf;
1440 	case 24:	return 0xa;
1441 	case 36:	return 0xe;
1442 	case 48:	return 0x9;
1443 	case 72:	return 0xd;
1444 	case 96:	return 0x8;
1445 	case 108:	return 0xc;
1446 
1447 	/* CCK rates (NB: not IEEE std, device-specific) */
1448 	case 2:		return 0x0;
1449 	case 4:		return 0x1;
1450 	case 11:	return 0x2;
1451 	case 22:	return 0x3;
1452 	}
1453 	return 0xff;		/* XXX unsupported/unknown rate */
1454 }
1455 
1456 static void
1457 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1458     uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1459 {
1460 	struct ifnet *ifp = sc->sc_ifp;
1461 	struct ieee80211com *ic = ifp->if_l2com;
1462 	uint16_t plcp_length;
1463 	int remainder;
1464 
1465 	desc->flags = htole32(flags);
1466 	desc->flags |= htole32(len << 16);
1467 
1468 	desc->physaddr = htole32(physaddr);
1469 	desc->wme = htole16(
1470 	    RT2560_AIFSN(2) |
1471 	    RT2560_LOGCWMIN(3) |
1472 	    RT2560_LOGCWMAX(8));
1473 
1474 	/* setup PLCP fields */
1475 	desc->plcp_signal  = rt2560_plcp_signal(rate);
1476 	desc->plcp_service = 4;
1477 
1478 	len += IEEE80211_CRC_LEN;
1479 	if (ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM) {
1480 		desc->flags |= htole32(RT2560_TX_OFDM);
1481 
1482 		plcp_length = len & 0xfff;
1483 		desc->plcp_length_hi = plcp_length >> 6;
1484 		desc->plcp_length_lo = plcp_length & 0x3f;
1485 	} else {
1486 		plcp_length = (16 * len + rate - 1) / rate;
1487 		if (rate == 22) {
1488 			remainder = (16 * len) % 22;
1489 			if (remainder != 0 && remainder < 7)
1490 				desc->plcp_service |= RT2560_PLCP_LENGEXT;
1491 		}
1492 		desc->plcp_length_hi = plcp_length >> 8;
1493 		desc->plcp_length_lo = plcp_length & 0xff;
1494 
1495 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1496 			desc->plcp_signal |= 0x08;
1497 	}
1498 
1499 	if (!encrypt)
1500 		desc->flags |= htole32(RT2560_TX_VALID);
1501 	desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1502 			       : htole32(RT2560_TX_BUSY);
1503 }
1504 
1505 static int
1506 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1507     struct ieee80211_node *ni)
1508 {
1509 	struct ieee80211vap *vap = ni->ni_vap;
1510 	struct ieee80211com *ic = ni->ni_ic;
1511 	struct ifnet *ifp = sc->sc_ifp;
1512 	struct rt2560_tx_desc *desc;
1513 	struct rt2560_tx_data *data;
1514 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1515 	int nsegs, rate, error;
1516 
1517 	desc = &sc->bcnq.desc[sc->bcnq.cur];
1518 	data = &sc->bcnq.data[sc->bcnq.cur];
1519 
1520 	/* XXX maybe a separate beacon rate? */
1521 	rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate;
1522 
1523 	error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0,
1524 	    segs, &nsegs, BUS_DMA_NOWAIT);
1525 	if (error != 0) {
1526 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1527 		    error);
1528 		m_freem(m0);
1529 		return error;
1530 	}
1531 
1532 	if (bpf_peers_present(ifp->if_bpf)) {
1533 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1534 
1535 		tap->wt_flags = 0;
1536 		tap->wt_rate = rate;
1537 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1538 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1539 		tap->wt_antenna = sc->tx_ant;
1540 
1541 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1542 	}
1543 
1544 	data->m = m0;
1545 	data->ni = ni;
1546 
1547 	rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1548 	    RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1549 
1550 	DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1551 	    m0->m_pkthdr.len, sc->bcnq.cur, rate);
1552 
1553 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1554 	bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1555 	    BUS_DMASYNC_PREWRITE);
1556 
1557 	sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1558 
1559 	return 0;
1560 }
1561 
1562 static int
1563 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1564     struct ieee80211_node *ni)
1565 {
1566 	struct ieee80211vap *vap = ni->ni_vap;
1567 	struct ieee80211com *ic = ni->ni_ic;
1568 	struct ifnet *ifp = sc->sc_ifp;
1569 	struct rt2560_tx_desc *desc;
1570 	struct rt2560_tx_data *data;
1571 	struct ieee80211_frame *wh;
1572 	struct ieee80211_key *k;
1573 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1574 	uint16_t dur;
1575 	uint32_t flags = 0;
1576 	int nsegs, rate, error;
1577 
1578 	desc = &sc->prioq.desc[sc->prioq.cur];
1579 	data = &sc->prioq.data[sc->prioq.cur];
1580 
1581 	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
1582 
1583 	wh = mtod(m0, struct ieee80211_frame *);
1584 
1585 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1586 		k = ieee80211_crypto_encap(ni, m0);
1587 		if (k == NULL) {
1588 			m_freem(m0);
1589 			return ENOBUFS;
1590 		}
1591 	}
1592 
1593 	error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1594 	    segs, &nsegs, 0);
1595 	if (error != 0) {
1596 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1597 		    error);
1598 		m_freem(m0);
1599 		return error;
1600 	}
1601 
1602 	if (bpf_peers_present(ifp->if_bpf)) {
1603 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1604 
1605 		tap->wt_flags = 0;
1606 		tap->wt_rate = rate;
1607 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1608 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1609 		tap->wt_antenna = sc->tx_ant;
1610 
1611 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1612 	}
1613 
1614 	data->m = m0;
1615 	data->ni = ni;
1616 	/* management frames are not taken into account for amrr */
1617 	data->rix = IEEE80211_FIXED_RATE_NONE;
1618 
1619 	wh = mtod(m0, struct ieee80211_frame *);
1620 
1621 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1622 		flags |= RT2560_TX_ACK;
1623 
1624 		dur = ieee80211_ack_duration(sc->sc_rates,
1625 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1626 		*(uint16_t *)wh->i_dur = htole16(dur);
1627 
1628 		/* tell hardware to add timestamp for probe responses */
1629 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1630 		    IEEE80211_FC0_TYPE_MGT &&
1631 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1632 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1633 			flags |= RT2560_TX_TIMESTAMP;
1634 	}
1635 
1636 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1637 	    segs->ds_addr);
1638 
1639 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1640 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1641 	    BUS_DMASYNC_PREWRITE);
1642 
1643 	DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1644 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1645 
1646 	/* kick prio */
1647 	sc->prioq.queued++;
1648 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1649 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1650 
1651 	return 0;
1652 }
1653 
1654 static int
1655 rt2560_sendprot(struct rt2560_softc *sc,
1656     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1657 {
1658 	struct ieee80211com *ic = ni->ni_ic;
1659 	const struct ieee80211_frame *wh;
1660 	struct rt2560_tx_desc *desc;
1661 	struct rt2560_tx_data *data;
1662 	struct mbuf *mprot;
1663 	int protrate, ackrate, pktlen, flags, isshort, error;
1664 	uint16_t dur;
1665 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1666 	int nsegs;
1667 
1668 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1669 	    ("protection %d", prot));
1670 
1671 	wh = mtod(m, const struct ieee80211_frame *);
1672 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1673 
1674 	protrate = ieee80211_ctl_rate(sc->sc_rates, rate);
1675 	ackrate = ieee80211_ack_rate(sc->sc_rates, rate);
1676 
1677 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1678 	dur = ieee80211_compute_duration(sc->sc_rates, pktlen, rate, isshort)
1679 	    + ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1680 	flags = RT2560_TX_MORE_FRAG;
1681 	if (prot == IEEE80211_PROT_RTSCTS) {
1682 		/* NB: CTS is the same size as an ACK */
1683 		dur += ieee80211_ack_duration(sc->sc_rates, rate, isshort);
1684 		flags |= RT2560_TX_ACK;
1685 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1686 	} else {
1687 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1688 	}
1689 	if (mprot == NULL) {
1690 		/* XXX stat + msg */
1691 		return ENOBUFS;
1692 	}
1693 
1694 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1695 	data = &sc->txq.data[sc->txq.cur_encrypt];
1696 
1697 	error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1698 	    mprot, segs, &nsegs, 0);
1699 	if (error != 0) {
1700 		device_printf(sc->sc_dev,
1701 		    "could not map mbuf (error %d)\n", error);
1702 		m_freem(mprot);
1703 		return error;
1704 	}
1705 
1706 	data->m = mprot;
1707 	data->ni = ieee80211_ref_node(ni);
1708 	/* ctl frames are not taken into account for amrr */
1709 	data->rix = IEEE80211_FIXED_RATE_NONE;
1710 
1711 	rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1712 	    segs->ds_addr);
1713 
1714 	bus_dmamap_sync(sc->txq.data_dmat, data->map,
1715 	    BUS_DMASYNC_PREWRITE);
1716 
1717 	sc->txq.queued++;
1718 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1719 
1720 	return 0;
1721 }
1722 
1723 static int
1724 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1725     struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1726 {
1727 	struct ifnet *ifp = sc->sc_ifp;
1728 	struct ieee80211com *ic = ifp->if_l2com;
1729 	struct rt2560_tx_desc *desc;
1730 	struct rt2560_tx_data *data;
1731 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1732 	uint32_t flags;
1733 	int nsegs, rate, error;
1734 
1735 	desc = &sc->prioq.desc[sc->prioq.cur];
1736 	data = &sc->prioq.data[sc->prioq.cur];
1737 
1738 	rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1739 	/* XXX validate */
1740 	if (rate == 0) {
1741 		/* XXX fall back to mcast/mgmt rate? */
1742 		m_freem(m0);
1743 		return EINVAL;
1744 	}
1745 
1746 	flags = 0;
1747 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1748 		flags |= RT2560_TX_ACK;
1749 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1750 		error = rt2560_sendprot(sc, m0, ni,
1751 		    params->ibp_flags & IEEE80211_BPF_RTS ?
1752 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1753 		    rate);
1754 		if (error) {
1755 			m_freem(m0);
1756 			return error;
1757 		}
1758 		flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1759 	}
1760 
1761 	error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1762 	    segs, &nsegs, 0);
1763 	if (error != 0) {
1764 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1765 		    error);
1766 		m_freem(m0);
1767 		return error;
1768 	}
1769 
1770 	if (bpf_peers_present(ifp->if_bpf)) {
1771 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1772 
1773 		tap->wt_flags = 0;
1774 		tap->wt_rate = rate;
1775 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1776 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1777 		tap->wt_antenna = sc->tx_ant;
1778 
1779 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1780 	}
1781 
1782 	data->m = m0;
1783 	data->ni = ni;
1784 
1785 	/* XXX need to setup descriptor ourself */
1786 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1787 	    rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1788 	    segs->ds_addr);
1789 
1790 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1791 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1792 	    BUS_DMASYNC_PREWRITE);
1793 
1794 	DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1795 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1796 
1797 	/* kick prio */
1798 	sc->prioq.queued++;
1799 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1800 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1801 
1802 	return 0;
1803 }
1804 
1805 static int
1806 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1807     struct ieee80211_node *ni)
1808 {
1809 	struct ieee80211vap *vap = ni->ni_vap;
1810 	struct ieee80211com *ic = ni->ni_ic;
1811 	struct ifnet *ifp = sc->sc_ifp;
1812 	struct rt2560_tx_desc *desc;
1813 	struct rt2560_tx_data *data;
1814 	struct ieee80211_frame *wh;
1815 	const struct ieee80211_txparam *tp;
1816 	struct ieee80211_key *k;
1817 	struct mbuf *mnew;
1818 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1819 	uint16_t dur;
1820 	uint32_t flags;
1821 	int nsegs, rate, error;
1822 
1823 	wh = mtod(m0, struct ieee80211_frame *);
1824 
1825 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1826 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1827 		rate = tp->mcastrate;
1828 	} else if (m0->m_flags & M_EAPOL) {
1829 		rate = tp->mgmtrate;
1830 	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1831 		rate = tp->ucastrate;
1832 	} else {
1833 		(void) ieee80211_amrr_choose(ni, &RT2560_NODE(ni)->amrr);
1834 		rate = ni->ni_txrate;
1835 	}
1836 
1837 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1838 		k = ieee80211_crypto_encap(ni, m0);
1839 		if (k == NULL) {
1840 			m_freem(m0);
1841 			return ENOBUFS;
1842 		}
1843 
1844 		/* packet header may have moved, reset our local pointer */
1845 		wh = mtod(m0, struct ieee80211_frame *);
1846 	}
1847 
1848 	flags = 0;
1849 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1850 		int prot = IEEE80211_PROT_NONE;
1851 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1852 			prot = IEEE80211_PROT_RTSCTS;
1853 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1854 		    ieee80211_rate2phytype(sc->sc_rates, rate) == IEEE80211_T_OFDM)
1855 			prot = ic->ic_protmode;
1856 		if (prot != IEEE80211_PROT_NONE) {
1857 			error = rt2560_sendprot(sc, m0, ni, prot, rate);
1858 			if (error) {
1859 				m_freem(m0);
1860 				return error;
1861 			}
1862 			flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1863 		}
1864 	}
1865 
1866 	data = &sc->txq.data[sc->txq.cur_encrypt];
1867 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1868 
1869 	error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1870 	    segs, &nsegs, 0);
1871 	if (error != 0 && error != EFBIG) {
1872 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1873 		    error);
1874 		m_freem(m0);
1875 		return error;
1876 	}
1877 	if (error != 0) {
1878 		mnew = m_defrag(m0, M_DONTWAIT);
1879 		if (mnew == NULL) {
1880 			device_printf(sc->sc_dev,
1881 			    "could not defragment mbuf\n");
1882 			m_freem(m0);
1883 			return ENOBUFS;
1884 		}
1885 		m0 = mnew;
1886 
1887 		error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1888 		    m0, segs, &nsegs, 0);
1889 		if (error != 0) {
1890 			device_printf(sc->sc_dev,
1891 			    "could not map mbuf (error %d)\n", error);
1892 			m_freem(m0);
1893 			return error;
1894 		}
1895 
1896 		/* packet header may have moved, reset our local pointer */
1897 		wh = mtod(m0, struct ieee80211_frame *);
1898 	}
1899 
1900 	if (bpf_peers_present(ifp->if_bpf)) {
1901 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1902 
1903 		tap->wt_flags = 0;
1904 		tap->wt_rate = rate;
1905 		tap->wt_antenna = sc->tx_ant;
1906 
1907 		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1908 	}
1909 
1910 	data->m = m0;
1911 	data->ni = ni;
1912 
1913 	/* remember link conditions for rate adaptation algorithm */
1914 	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1915 		data->rix = ni->ni_txrate;
1916 		/* XXX probably need last rssi value and not avg */
1917 		data->rssi = ic->ic_node_getrssi(ni);
1918 	} else
1919 		data->rix = IEEE80211_FIXED_RATE_NONE;
1920 
1921 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1922 		flags |= RT2560_TX_ACK;
1923 
1924 		dur = ieee80211_ack_duration(sc->sc_rates,
1925 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1926 		*(uint16_t *)wh->i_dur = htole16(dur);
1927 	}
1928 
1929 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1930 	    segs->ds_addr);
1931 
1932 	bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1933 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1934 	    BUS_DMASYNC_PREWRITE);
1935 
1936 	DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1937 	    m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1938 
1939 	/* kick encrypt */
1940 	sc->txq.queued++;
1941 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1942 	RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1943 
1944 	return 0;
1945 }
1946 
1947 static void
1948 rt2560_start_locked(struct ifnet *ifp)
1949 {
1950 	struct rt2560_softc *sc = ifp->if_softc;
1951 	struct mbuf *m;
1952 	struct ieee80211_node *ni;
1953 
1954 	RAL_LOCK_ASSERT(sc);
1955 
1956 	for (;;) {
1957 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1958 		if (m == NULL)
1959 			break;
1960 		if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1961 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1962 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1963 			sc->sc_flags |= RT2560_F_DATA_OACTIVE;
1964 			break;
1965 		}
1966 
1967 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1968 		m = ieee80211_encap(ni, m);
1969 		if (m == NULL) {
1970 			ieee80211_free_node(ni);
1971 			ifp->if_oerrors++;
1972 			continue;
1973 		}
1974 
1975 		if (rt2560_tx_data(sc, m, ni) != 0) {
1976 			ieee80211_free_node(ni);
1977 			ifp->if_oerrors++;
1978 			break;
1979 		}
1980 
1981 		sc->sc_tx_timer = 5;
1982 	}
1983 }
1984 
1985 static void
1986 rt2560_start(struct ifnet *ifp)
1987 {
1988 	struct rt2560_softc *sc = ifp->if_softc;
1989 
1990 	RAL_LOCK(sc);
1991 	rt2560_start_locked(ifp);
1992 	RAL_UNLOCK(sc);
1993 }
1994 
1995 static void
1996 rt2560_watchdog(void *arg)
1997 {
1998 	struct rt2560_softc *sc = arg;
1999 	struct ifnet *ifp = sc->sc_ifp;
2000 
2001 	RAL_LOCK_ASSERT(sc);
2002 
2003 	KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING, ("not running"));
2004 
2005 	if (sc->sc_invalid)		/* card ejected */
2006 		return;
2007 
2008 	rt2560_encryption_intr(sc);
2009 	rt2560_tx_intr(sc);
2010 
2011 	if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
2012 		if_printf(ifp, "device timeout\n");
2013 		rt2560_init_locked(sc);
2014 		ifp->if_oerrors++;
2015 		/* NB: callout is reset in rt2560_init() */
2016 		return;
2017 	}
2018 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2019 }
2020 
2021 static int
2022 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2023 {
2024 	struct rt2560_softc *sc = ifp->if_softc;
2025 	struct ieee80211com *ic = ifp->if_l2com;
2026 	struct ifreq *ifr = (struct ifreq *) data;
2027 	int error = 0, startall = 0;
2028 
2029 	switch (cmd) {
2030 	case SIOCSIFFLAGS:
2031 		RAL_LOCK(sc);
2032 		if (ifp->if_flags & IFF_UP) {
2033 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2034 				rt2560_init_locked(sc);
2035 				startall = 1;
2036 			} else
2037 				rt2560_update_promisc(ifp);
2038 		} else {
2039 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2040 				rt2560_stop_locked(sc);
2041 		}
2042 		RAL_UNLOCK(sc);
2043 		if (startall)
2044 			ieee80211_start_all(ic);
2045 		break;
2046 	case SIOCGIFMEDIA:
2047 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2048 		break;
2049 	case SIOCGIFADDR:
2050 		error = ether_ioctl(ifp, cmd, data);
2051 		break;
2052 	default:
2053 		error = EINVAL;
2054 		break;
2055 	}
2056 	return error;
2057 }
2058 
2059 static void
2060 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2061 {
2062 	uint32_t tmp;
2063 	int ntries;
2064 
2065 	for (ntries = 0; ntries < 100; ntries++) {
2066 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2067 			break;
2068 		DELAY(1);
2069 	}
2070 	if (ntries == 100) {
2071 		device_printf(sc->sc_dev, "could not write to BBP\n");
2072 		return;
2073 	}
2074 
2075 	tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2076 	RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2077 
2078 	DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
2079 }
2080 
2081 static uint8_t
2082 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2083 {
2084 	uint32_t val;
2085 	int ntries;
2086 
2087 	for (ntries = 0; ntries < 100; ntries++) {
2088 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2089 			break;
2090 		DELAY(1);
2091 	}
2092 	if (ntries == 100) {
2093 		device_printf(sc->sc_dev, "could not read from BBP\n");
2094 		return 0;
2095 	}
2096 
2097 	val = RT2560_BBP_BUSY | reg << 8;
2098 	RAL_WRITE(sc, RT2560_BBPCSR, val);
2099 
2100 	for (ntries = 0; ntries < 100; ntries++) {
2101 		val = RAL_READ(sc, RT2560_BBPCSR);
2102 		if (!(val & RT2560_BBP_BUSY))
2103 			return val & 0xff;
2104 		DELAY(1);
2105 	}
2106 
2107 	device_printf(sc->sc_dev, "could not read from BBP\n");
2108 	return 0;
2109 }
2110 
2111 static void
2112 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2113 {
2114 	uint32_t tmp;
2115 	int ntries;
2116 
2117 	for (ntries = 0; ntries < 100; ntries++) {
2118 		if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2119 			break;
2120 		DELAY(1);
2121 	}
2122 	if (ntries == 100) {
2123 		device_printf(sc->sc_dev, "could not write to RF\n");
2124 		return;
2125 	}
2126 
2127 	tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2128 	    (reg & 0x3);
2129 	RAL_WRITE(sc, RT2560_RFCSR, tmp);
2130 
2131 	/* remember last written value in sc */
2132 	sc->rf_regs[reg] = val;
2133 
2134 	DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2135 }
2136 
2137 static void
2138 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2139 {
2140 	struct ifnet *ifp = sc->sc_ifp;
2141 	struct ieee80211com *ic = ifp->if_l2com;
2142 	uint8_t power, tmp;
2143 	u_int i, chan;
2144 
2145 	chan = ieee80211_chan2ieee(ic, c);
2146 	KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
2147 
2148 	sc->sc_rates = ieee80211_get_ratetable(c);
2149 
2150 	if (IEEE80211_IS_CHAN_2GHZ(c))
2151 		power = min(sc->txpow[chan - 1], 31);
2152 	else
2153 		power = 31;
2154 
2155 	/* adjust txpower using ifconfig settings */
2156 	power -= (100 - ic->ic_txpowlimit) / 8;
2157 
2158 	DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2159 
2160 	switch (sc->rf_rev) {
2161 	case RT2560_RF_2522:
2162 		rt2560_rf_write(sc, RAL_RF1, 0x00814);
2163 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2164 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2165 		break;
2166 
2167 	case RT2560_RF_2523:
2168 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2169 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2170 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2171 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2172 		break;
2173 
2174 	case RT2560_RF_2524:
2175 		rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2176 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2177 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2178 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2179 		break;
2180 
2181 	case RT2560_RF_2525:
2182 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2183 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2184 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2185 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2186 
2187 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2188 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2189 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2190 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2191 		break;
2192 
2193 	case RT2560_RF_2525E:
2194 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2195 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2196 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2197 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2198 		break;
2199 
2200 	case RT2560_RF_2526:
2201 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2202 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2203 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2204 
2205 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2206 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2207 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2208 		break;
2209 
2210 	/* dual-band RF */
2211 	case RT2560_RF_5222:
2212 		for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2213 
2214 		rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2215 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2216 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2217 		rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2218 		break;
2219 	default:
2220  	        printf("unknown ral rev=%d\n", sc->rf_rev);
2221 	}
2222 
2223 	/* XXX */
2224 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2225 		/* set Japan filter bit for channel 14 */
2226 		tmp = rt2560_bbp_read(sc, 70);
2227 
2228 		tmp &= ~RT2560_JAPAN_FILTER;
2229 		if (chan == 14)
2230 			tmp |= RT2560_JAPAN_FILTER;
2231 
2232 		rt2560_bbp_write(sc, 70, tmp);
2233 
2234 		/* clear CRC errors */
2235 		RAL_READ(sc, RT2560_CNT0);
2236 	}
2237 }
2238 
2239 static void
2240 rt2560_set_channel(struct ieee80211com *ic)
2241 {
2242 	struct ifnet *ifp = ic->ic_ifp;
2243 	struct rt2560_softc *sc = ifp->if_softc;
2244 
2245 	RAL_LOCK(sc);
2246 	rt2560_set_chan(sc, ic->ic_curchan);
2247 
2248 	sc->sc_txtap.wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2249 	sc->sc_txtap.wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2250 	sc->sc_rxtap.wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2251 	sc->sc_rxtap.wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2252 	RAL_UNLOCK(sc);
2253 
2254 }
2255 
2256 #if 0
2257 /*
2258  * Disable RF auto-tuning.
2259  */
2260 static void
2261 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2262 {
2263 	uint32_t tmp;
2264 
2265 	if (sc->rf_rev != RT2560_RF_2523) {
2266 		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2267 		rt2560_rf_write(sc, RAL_RF1, tmp);
2268 	}
2269 
2270 	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2271 	rt2560_rf_write(sc, RAL_RF3, tmp);
2272 
2273 	DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2274 }
2275 #endif
2276 
2277 /*
2278  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2279  * synchronization.
2280  */
2281 static void
2282 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2283 {
2284 	struct ifnet *ifp = sc->sc_ifp;
2285 	struct ieee80211com *ic = ifp->if_l2com;
2286 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2287 	uint16_t logcwmin, preload;
2288 	uint32_t tmp;
2289 
2290 	/* first, disable TSF synchronization */
2291 	RAL_WRITE(sc, RT2560_CSR14, 0);
2292 
2293 	tmp = 16 * vap->iv_bss->ni_intval;
2294 	RAL_WRITE(sc, RT2560_CSR12, tmp);
2295 
2296 	RAL_WRITE(sc, RT2560_CSR13, 0);
2297 
2298 	logcwmin = 5;
2299 	preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024;
2300 	tmp = logcwmin << 16 | preload;
2301 	RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2302 
2303 	/* finally, enable TSF synchronization */
2304 	tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2305 	if (ic->ic_opmode == IEEE80211_M_STA)
2306 		tmp |= RT2560_ENABLE_TSF_SYNC(1);
2307 	else
2308 		tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2309 		       RT2560_ENABLE_BEACON_GENERATOR;
2310 	RAL_WRITE(sc, RT2560_CSR14, tmp);
2311 
2312 	DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2313 }
2314 
2315 static void
2316 rt2560_update_plcp(struct rt2560_softc *sc)
2317 {
2318 	struct ifnet *ifp = sc->sc_ifp;
2319 	struct ieee80211com *ic = ifp->if_l2com;
2320 
2321 	/* no short preamble for 1Mbps */
2322 	RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2323 
2324 	if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2325 		/* values taken from the reference driver */
2326 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
2327 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2328 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
2329 	} else {
2330 		/* same values as above or'ed 0x8 */
2331 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
2332 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2333 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
2334 	}
2335 
2336 	DPRINTF(sc, "updating PLCP for %s preamble\n",
2337 	    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long");
2338 }
2339 
2340 /*
2341  * This function can be called by ieee80211_set_shortslottime(). Refer to
2342  * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2343  */
2344 static void
2345 rt2560_update_slot(struct ifnet *ifp)
2346 {
2347 	struct rt2560_softc *sc = ifp->if_softc;
2348 	struct ieee80211com *ic = ifp->if_l2com;
2349 	uint8_t slottime;
2350 	uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2351 	uint32_t tmp;
2352 
2353 #ifndef FORCE_SLOTTIME
2354 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2355 #else
2356 	/*
2357 	 * Setting slot time according to "short slot time" capability
2358 	 * in beacon/probe_resp seems to cause problem to acknowledge
2359 	 * certain AP's data frames transimitted at CCK/DS rates: the
2360 	 * problematic AP keeps retransmitting data frames, probably
2361 	 * because MAC level acks are not received by hardware.
2362 	 * So we cheat a little bit here by claiming we are capable of
2363 	 * "short slot time" but setting hardware slot time to the normal
2364 	 * slot time.  ral(4) does not seem to have trouble to receive
2365 	 * frames transmitted using short slot time even if hardware
2366 	 * slot time is set to normal slot time.  If we didn't use this
2367 	 * trick, we would have to claim that short slot time is not
2368 	 * supported; this would give relative poor RX performance
2369 	 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2370 	 * slot time.
2371 	 */
2372 	slottime = 20;
2373 #endif
2374 
2375 	/* update the MAC slot boundaries */
2376 	tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2377 	tx_pifs = tx_sifs + slottime;
2378 	tx_difs = tx_sifs + 2 * slottime;
2379 	eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2380 
2381 	tmp = RAL_READ(sc, RT2560_CSR11);
2382 	tmp = (tmp & ~0x1f00) | slottime << 8;
2383 	RAL_WRITE(sc, RT2560_CSR11, tmp);
2384 
2385 	tmp = tx_pifs << 16 | tx_sifs;
2386 	RAL_WRITE(sc, RT2560_CSR18, tmp);
2387 
2388 	tmp = eifs << 16 | tx_difs;
2389 	RAL_WRITE(sc, RT2560_CSR19, tmp);
2390 
2391 	DPRINTF(sc, "setting slottime to %uus\n", slottime);
2392 }
2393 
2394 static void
2395 rt2560_set_basicrates(struct rt2560_softc *sc)
2396 {
2397 	struct ifnet *ifp = sc->sc_ifp;
2398 	struct ieee80211com *ic = ifp->if_l2com;
2399 
2400 	/* update basic rate set */
2401 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
2402 		/* 11b basic rates: 1, 2Mbps */
2403 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2404 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2405 		/* 11a basic rates: 6, 12, 24Mbps */
2406 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2407 	} else {
2408 		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2409 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2410 	}
2411 }
2412 
2413 static void
2414 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2415 {
2416 	uint32_t tmp;
2417 
2418 	/* set ON period to 70ms and OFF period to 30ms */
2419 	tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2420 	RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2421 }
2422 
2423 static void
2424 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2425 {
2426 	uint32_t tmp;
2427 
2428 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2429 	RAL_WRITE(sc, RT2560_CSR5, tmp);
2430 
2431 	tmp = bssid[4] | bssid[5] << 8;
2432 	RAL_WRITE(sc, RT2560_CSR6, tmp);
2433 
2434 	DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2435 }
2436 
2437 static void
2438 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2439 {
2440 	uint32_t tmp;
2441 
2442 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2443 	RAL_WRITE(sc, RT2560_CSR3, tmp);
2444 
2445 	tmp = addr[4] | addr[5] << 8;
2446 	RAL_WRITE(sc, RT2560_CSR4, tmp);
2447 
2448 	DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2449 }
2450 
2451 static void
2452 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2453 {
2454 	uint32_t tmp;
2455 
2456 	tmp = RAL_READ(sc, RT2560_CSR3);
2457 	addr[0] = tmp & 0xff;
2458 	addr[1] = (tmp >>  8) & 0xff;
2459 	addr[2] = (tmp >> 16) & 0xff;
2460 	addr[3] = (tmp >> 24);
2461 
2462 	tmp = RAL_READ(sc, RT2560_CSR4);
2463 	addr[4] = tmp & 0xff;
2464 	addr[5] = (tmp >> 8) & 0xff;
2465 }
2466 
2467 static void
2468 rt2560_update_promisc(struct ifnet *ifp)
2469 {
2470 	struct rt2560_softc *sc = ifp->if_softc;
2471 	uint32_t tmp;
2472 
2473 	tmp = RAL_READ(sc, RT2560_RXCSR0);
2474 
2475 	tmp &= ~RT2560_DROP_NOT_TO_ME;
2476 	if (!(ifp->if_flags & IFF_PROMISC))
2477 		tmp |= RT2560_DROP_NOT_TO_ME;
2478 
2479 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2480 
2481 	DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2482 	    "entering" : "leaving");
2483 }
2484 
2485 static const char *
2486 rt2560_get_rf(int rev)
2487 {
2488 	switch (rev) {
2489 	case RT2560_RF_2522:	return "RT2522";
2490 	case RT2560_RF_2523:	return "RT2523";
2491 	case RT2560_RF_2524:	return "RT2524";
2492 	case RT2560_RF_2525:	return "RT2525";
2493 	case RT2560_RF_2525E:	return "RT2525e";
2494 	case RT2560_RF_2526:	return "RT2526";
2495 	case RT2560_RF_5222:	return "RT5222";
2496 	default:		return "unknown";
2497 	}
2498 }
2499 
2500 static void
2501 rt2560_read_config(struct rt2560_softc *sc)
2502 {
2503 	uint16_t val;
2504 	int i;
2505 
2506 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2507 	sc->rf_rev =   (val >> 11) & 0x7;
2508 	sc->hw_radio = (val >> 10) & 0x1;
2509 	sc->led_mode = (val >> 6)  & 0x7;
2510 	sc->rx_ant =   (val >> 4)  & 0x3;
2511 	sc->tx_ant =   (val >> 2)  & 0x3;
2512 	sc->nb_ant =   val & 0x3;
2513 
2514 	/* read default values for BBP registers */
2515 	for (i = 0; i < 16; i++) {
2516 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2517 		if (val == 0 || val == 0xffff)
2518 			continue;
2519 
2520 		sc->bbp_prom[i].reg = val >> 8;
2521 		sc->bbp_prom[i].val = val & 0xff;
2522 	}
2523 
2524 	/* read Tx power for all b/g channels */
2525 	for (i = 0; i < 14 / 2; i++) {
2526 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2527 		sc->txpow[i * 2] = val & 0xff;
2528 		sc->txpow[i * 2 + 1] = val >> 8;
2529 	}
2530 	for (i = 0; i < 14; ++i) {
2531 		if (sc->txpow[i] > 31)
2532 			sc->txpow[i] = 24;
2533 	}
2534 
2535 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2536 	if ((val & 0xff) == 0xff)
2537 		sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2538 	else
2539 		sc->rssi_corr = val & 0xff;
2540 	DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2541 		 sc->rssi_corr, val);
2542 }
2543 
2544 
2545 static void
2546 rt2560_scan_start(struct ieee80211com *ic)
2547 {
2548 	struct ifnet *ifp = ic->ic_ifp;
2549 	struct rt2560_softc *sc = ifp->if_softc;
2550 
2551 	/* abort TSF synchronization */
2552 	RAL_WRITE(sc, RT2560_CSR14, 0);
2553 	rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2554 }
2555 
2556 static void
2557 rt2560_scan_end(struct ieee80211com *ic)
2558 {
2559 	struct ifnet *ifp = ic->ic_ifp;
2560 	struct rt2560_softc *sc = ifp->if_softc;
2561 	struct ieee80211vap *vap = ic->ic_scan->ss_vap;
2562 
2563 	rt2560_enable_tsf_sync(sc);
2564 	/* XXX keep local copy */
2565 	rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2566 }
2567 
2568 static int
2569 rt2560_bbp_init(struct rt2560_softc *sc)
2570 {
2571 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2572 	int i, ntries;
2573 
2574 	/* wait for BBP to be ready */
2575 	for (ntries = 0; ntries < 100; ntries++) {
2576 		if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2577 			break;
2578 		DELAY(1);
2579 	}
2580 	if (ntries == 100) {
2581 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2582 		return EIO;
2583 	}
2584 
2585 	/* initialize BBP registers to default values */
2586 	for (i = 0; i < N(rt2560_def_bbp); i++) {
2587 		rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2588 		    rt2560_def_bbp[i].val);
2589 	}
2590 
2591 	/* initialize BBP registers to values stored in EEPROM */
2592 	for (i = 0; i < 16; i++) {
2593 		if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2594 			break;
2595 		rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2596 	}
2597 	rt2560_bbp_write(sc, 17, 0x48);	/* XXX restore bbp17 */
2598 
2599 	return 0;
2600 #undef N
2601 }
2602 
2603 static void
2604 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2605 {
2606 	uint32_t tmp;
2607 	uint8_t tx;
2608 
2609 	tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2610 	if (antenna == 1)
2611 		tx |= RT2560_BBP_ANTA;
2612 	else if (antenna == 2)
2613 		tx |= RT2560_BBP_ANTB;
2614 	else
2615 		tx |= RT2560_BBP_DIVERSITY;
2616 
2617 	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2618 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2619 	    sc->rf_rev == RT2560_RF_5222)
2620 		tx |= RT2560_BBP_FLIPIQ;
2621 
2622 	rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2623 
2624 	/* update values for CCK and OFDM in BBPCSR1 */
2625 	tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2626 	tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2627 	RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2628 }
2629 
2630 static void
2631 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2632 {
2633 	uint8_t rx;
2634 
2635 	rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2636 	if (antenna == 1)
2637 		rx |= RT2560_BBP_ANTA;
2638 	else if (antenna == 2)
2639 		rx |= RT2560_BBP_ANTB;
2640 	else
2641 		rx |= RT2560_BBP_DIVERSITY;
2642 
2643 	/* need to force no I/Q flip for RF 2525e and 2526 */
2644 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2645 		rx &= ~RT2560_BBP_FLIPIQ;
2646 
2647 	rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2648 }
2649 
2650 static void
2651 rt2560_init_locked(struct rt2560_softc *sc)
2652 {
2653 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2654 	struct ifnet *ifp = sc->sc_ifp;
2655 	struct ieee80211com *ic = ifp->if_l2com;
2656 	uint32_t tmp;
2657 	int i;
2658 
2659 	RAL_LOCK_ASSERT(sc);
2660 
2661 	rt2560_stop_locked(sc);
2662 
2663 	/* setup tx rings */
2664 	tmp = RT2560_PRIO_RING_COUNT << 24 |
2665 	      RT2560_ATIM_RING_COUNT << 16 |
2666 	      RT2560_TX_RING_COUNT   <<  8 |
2667 	      RT2560_TX_DESC_SIZE;
2668 
2669 	/* rings must be initialized in this exact order */
2670 	RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2671 	RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2672 	RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2673 	RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2674 	RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2675 
2676 	/* setup rx ring */
2677 	tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2678 
2679 	RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2680 	RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2681 
2682 	/* initialize MAC registers to default values */
2683 	for (i = 0; i < N(rt2560_def_mac); i++)
2684 		RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2685 
2686 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2687 	rt2560_set_macaddr(sc, ic->ic_myaddr);
2688 
2689 	/* set basic rate set (will be updated later) */
2690 	RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2691 
2692 	rt2560_update_slot(ifp);
2693 	rt2560_update_plcp(sc);
2694 	rt2560_update_led(sc, 0, 0);
2695 
2696 	RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2697 	RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2698 
2699 	if (rt2560_bbp_init(sc) != 0) {
2700 		rt2560_stop(sc);
2701 		RAL_UNLOCK(sc);
2702 		return;
2703 	}
2704 
2705 	rt2560_set_txantenna(sc, sc->tx_ant);
2706 	rt2560_set_rxantenna(sc, sc->rx_ant);
2707 
2708 	/* set default BSS channel */
2709 	rt2560_set_chan(sc, ic->ic_curchan);
2710 
2711 	/* kick Rx */
2712 	tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2713 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2714 		tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2715 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2716 			tmp |= RT2560_DROP_TODS;
2717 		if (!(ifp->if_flags & IFF_PROMISC))
2718 			tmp |= RT2560_DROP_NOT_TO_ME;
2719 	}
2720 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2721 
2722 	/* clear old FCS and Rx FIFO errors */
2723 	RAL_READ(sc, RT2560_CNT0);
2724 	RAL_READ(sc, RT2560_CNT4);
2725 
2726 	/* clear any pending interrupts */
2727 	RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2728 
2729 	/* enable interrupts */
2730 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2731 
2732 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2733 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2734 
2735 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2736 #undef N
2737 }
2738 
2739 static void
2740 rt2560_init(void *priv)
2741 {
2742 	struct rt2560_softc *sc = priv;
2743 	struct ifnet *ifp = sc->sc_ifp;
2744 	struct ieee80211com *ic = ifp->if_l2com;
2745 
2746 	RAL_LOCK(sc);
2747 	rt2560_init_locked(sc);
2748 	RAL_UNLOCK(sc);
2749 
2750 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2751 		ieee80211_start_all(ic);		/* start all vap's */
2752 }
2753 
2754 static void
2755 rt2560_stop_locked(struct rt2560_softc *sc)
2756 {
2757 	struct ifnet *ifp = sc->sc_ifp;
2758 	volatile int *flags = &sc->sc_flags;
2759 
2760 	RAL_LOCK_ASSERT(sc);
2761 
2762 	while (*flags & RT2560_F_INPUT_RUNNING)
2763 		msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10);
2764 
2765 	callout_stop(&sc->watchdog_ch);
2766 	sc->sc_tx_timer = 0;
2767 
2768 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2769 		ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2770 
2771 		/* abort Tx */
2772 		RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2773 
2774 		/* disable Rx */
2775 		RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2776 
2777 		/* reset ASIC (imply reset BBP) */
2778 		RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2779 		RAL_WRITE(sc, RT2560_CSR1, 0);
2780 
2781 		/* disable interrupts */
2782 		RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2783 
2784 		/* reset Tx and Rx rings */
2785 		rt2560_reset_tx_ring(sc, &sc->txq);
2786 		rt2560_reset_tx_ring(sc, &sc->atimq);
2787 		rt2560_reset_tx_ring(sc, &sc->prioq);
2788 		rt2560_reset_tx_ring(sc, &sc->bcnq);
2789 		rt2560_reset_rx_ring(sc, &sc->rxq);
2790 	}
2791 	sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2792 }
2793 
2794 void
2795 rt2560_stop(void *arg)
2796 {
2797 	struct rt2560_softc *sc = arg;
2798 
2799 	RAL_LOCK(sc);
2800 	rt2560_stop_locked(sc);
2801 	RAL_UNLOCK(sc);
2802 }
2803 
2804 static int
2805 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2806 	const struct ieee80211_bpf_params *params)
2807 {
2808 	struct ieee80211com *ic = ni->ni_ic;
2809 	struct ifnet *ifp = ic->ic_ifp;
2810 	struct rt2560_softc *sc = ifp->if_softc;
2811 
2812 	RAL_LOCK(sc);
2813 
2814 	/* prevent management frames from being sent if we're not ready */
2815 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2816 		RAL_UNLOCK(sc);
2817 		m_freem(m);
2818 		ieee80211_free_node(ni);
2819 		return ENETDOWN;
2820 	}
2821 	if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2822 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2823 		sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2824 		RAL_UNLOCK(sc);
2825 		m_freem(m);
2826 		ieee80211_free_node(ni);
2827 		return ENOBUFS;		/* XXX */
2828 	}
2829 
2830 	ifp->if_opackets++;
2831 
2832 	if (params == NULL) {
2833 		/*
2834 		 * Legacy path; interpret frame contents to decide
2835 		 * precisely how to send the frame.
2836 		 */
2837 		if (rt2560_tx_mgt(sc, m, ni) != 0)
2838 			goto bad;
2839 	} else {
2840 		/*
2841 		 * Caller supplied explicit parameters to use in
2842 		 * sending the frame.
2843 		 */
2844 		if (rt2560_tx_raw(sc, m, ni, params))
2845 			goto bad;
2846 	}
2847 	sc->sc_tx_timer = 5;
2848 
2849 	RAL_UNLOCK(sc);
2850 
2851 	return 0;
2852 bad:
2853 	ifp->if_oerrors++;
2854 	ieee80211_free_node(ni);
2855 	RAL_UNLOCK(sc);
2856 	return EIO;		/* XXX */
2857 }
2858