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