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