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