xref: /dragonfly/sys/dev/netif/ral/rt2560.c (revision b58f1e66)
1 /*	$FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $	*/
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  * $FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $
20  * $DragonFly$
21  */
22 
23 
24 /*-
25  * Ralink Technology RT2560 chipset driver
26  * http://www.ralinktech.com/
27  */
28 
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #include <sys/sockio.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/endian.h>
42 #include <sys/rman.h>
43 
44 #include <net/bpf.h>
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ifq_var.h>
52 
53 #include <netproto/802_11/ieee80211_var.h>
54 #include <netproto/802_11/ieee80211_radiotap.h>
55 #include <netproto/802_11/ieee80211_regdomain.h>
56 #include <netproto/802_11/ieee80211_ratectl.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/if_ether.h>
63 
64 #include <dev/netif/ral/rt2560reg.h>
65 #include <dev/netif/ral/rt2560var.h>
66 
67 #define RT2560_RSSI(sc, rssi)					\
68 	((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ?	\
69 	 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
70 
71 #define RAL_DEBUG
72 #ifdef RAL_DEBUG
73 #define DPRINTF(sc, fmt, ...) do {				\
74 	if (sc->sc_debug > 0)					\
75 		kprintf(fmt, __VA_ARGS__);			\
76 } while (0)
77 #define DPRINTFN(sc, n, fmt, ...) do {				\
78 	if (sc->sc_debug >= (n))				\
79 		kprintf(fmt, __VA_ARGS__);			\
80 } while (0)
81 #else
82 #define DPRINTF(sc, fmt, ...)
83 #define DPRINTFN(sc, n, fmt, ...)
84 #endif
85 
86 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *,
87 			    const char name[IFNAMSIZ], int unit, int opmode,
88 			    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
89 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
90 static void		rt2560_vap_delete(struct ieee80211vap *);
91 static void		rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
92 			    int);
93 static int		rt2560_alloc_tx_ring(struct rt2560_softc *,
94 			    struct rt2560_tx_ring *, int);
95 static void		rt2560_reset_tx_ring(struct rt2560_softc *,
96 			    struct rt2560_tx_ring *);
97 static void		rt2560_free_tx_ring(struct rt2560_softc *,
98 			    struct rt2560_tx_ring *);
99 static int		rt2560_alloc_rx_ring(struct rt2560_softc *,
100 			    struct rt2560_rx_ring *, int);
101 static void		rt2560_reset_rx_ring(struct rt2560_softc *,
102 			    struct rt2560_rx_ring *);
103 static void		rt2560_free_rx_ring(struct rt2560_softc *,
104 			    struct rt2560_rx_ring *);
105 static int		rt2560_newstate(struct ieee80211vap *,
106 			    enum ieee80211_state, int);
107 static uint16_t		rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
108 static void		rt2560_encryption_intr(struct rt2560_softc *);
109 static void		rt2560_tx_intr(struct rt2560_softc *);
110 static void		rt2560_prio_intr(struct rt2560_softc *);
111 static void		rt2560_decryption_intr(struct rt2560_softc *);
112 static void		rt2560_rx_intr(struct rt2560_softc *);
113 static void		rt2560_beacon_update(struct ieee80211vap *, int item);
114 static void		rt2560_beacon_expire(struct rt2560_softc *);
115 static void		rt2560_wakeup_expire(struct rt2560_softc *);
116 static void		rt2560_scan_start(struct ieee80211com *);
117 static void		rt2560_scan_end(struct ieee80211com *);
118 static void		rt2560_set_channel(struct ieee80211com *);
119 static void		rt2560_setup_tx_desc(struct rt2560_softc *,
120 			    struct rt2560_tx_desc *, uint32_t, int, int, int,
121 			    bus_addr_t);
122 static int		rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
123 			    struct ieee80211_node *);
124 static int		rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
125 			    struct ieee80211_node *);
126 static int		rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
127 			    struct ieee80211_node *);
128 static void		rt2560_start_locked(struct ifnet *);
129 static void		rt2560_start(struct ifnet *);
130 static void		rt2560_watchdog_callout(void *);
131 static int		rt2560_ioctl(struct ifnet *, u_long, caddr_t,
132     			    struct ucred *);
133 static void		rt2560_bbp_write(struct rt2560_softc *, uint8_t,
134 			    uint8_t);
135 static uint8_t		rt2560_bbp_read(struct rt2560_softc *, uint8_t);
136 static void		rt2560_rf_write(struct rt2560_softc *, uint8_t,
137 			    uint32_t);
138 static void		rt2560_set_chan(struct rt2560_softc *,
139 			    struct ieee80211_channel *);
140 #if 0
141 static void		rt2560_disable_rf_tune(struct rt2560_softc *);
142 #endif
143 static void		rt2560_enable_tsf_sync(struct rt2560_softc *);
144 static void		rt2560_enable_tsf(struct rt2560_softc *);
145 static void		rt2560_update_plcp(struct rt2560_softc *);
146 static void		rt2560_update_slot(struct ifnet *);
147 static void		rt2560_set_basicrates(struct rt2560_softc *);
148 static void		rt2560_update_led(struct rt2560_softc *, int, int);
149 static void		rt2560_set_bssid(struct rt2560_softc *, const uint8_t *);
150 static void		rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
151 static void		rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
152 static void		rt2560_update_promisc(struct ifnet *);
153 static const char	*rt2560_get_rf(int);
154 static void		rt2560_read_config(struct rt2560_softc *);
155 static int		rt2560_bbp_init(struct rt2560_softc *);
156 static void		rt2560_set_txantenna(struct rt2560_softc *, int);
157 static void		rt2560_set_rxantenna(struct rt2560_softc *, int);
158 static void		rt2560_init_locked(struct rt2560_softc *);
159 static void		rt2560_init(void *);
160 static void		rt2560_stop_locked(struct rt2560_softc *);
161 static int		rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *,
162 				const struct ieee80211_bpf_params *);
163 
164 static const struct {
165 	uint32_t	reg;
166 	uint32_t	val;
167 } rt2560_def_mac[] = {
168 	RT2560_DEF_MAC
169 };
170 
171 static const struct {
172 	uint8_t	reg;
173 	uint8_t	val;
174 } rt2560_def_bbp[] = {
175 	RT2560_DEF_BBP
176 };
177 
178 static const uint32_t rt2560_rf2522_r2[]    = RT2560_RF2522_R2;
179 static const uint32_t rt2560_rf2523_r2[]    = RT2560_RF2523_R2;
180 static const uint32_t rt2560_rf2524_r2[]    = RT2560_RF2524_R2;
181 static const uint32_t rt2560_rf2525_r2[]    = RT2560_RF2525_R2;
182 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
183 static const uint32_t rt2560_rf2525e_r2[]   = RT2560_RF2525E_R2;
184 static const uint32_t rt2560_rf2526_r2[]    = RT2560_RF2526_R2;
185 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
186 
187 static const struct {
188 	uint8_t		chan;
189 	uint32_t	r1, r2, r4;
190 } rt2560_rf5222[] = {
191 	RT2560_RF5222
192 };
193 
194 int
195 rt2560_attach(device_t dev, int id)
196 {
197 	struct rt2560_softc *sc = device_get_softc(dev);
198 	struct ieee80211com *ic;
199 	struct ifnet *ifp;
200 	int error;
201 	uint8_t bands;
202 	uint8_t macaddr[IEEE80211_ADDR_LEN];
203 	struct sysctl_ctx_list *ctx;
204 	struct sysctl_oid *tree;
205 
206 	sc->sc_dev = dev;
207 
208 	callout_init(&sc->watchdog_ch);
209 
210 	/* retrieve RT2560 rev. no */
211 	sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
212 
213 	/* retrieve RF rev. no and various other things from EEPROM */
214 	rt2560_read_config(sc);
215 
216 	device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
217 	    sc->asic_rev, rt2560_get_rf(sc->rf_rev));
218 
219 	/*
220 	 * Allocate Tx and Rx rings.
221 	 */
222 	error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
223 	if (error != 0) {
224 		device_printf(sc->sc_dev, "could not allocate Tx ring\n");
225 		goto fail1;
226 	}
227 
228 	error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
229 	if (error != 0) {
230 		device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
231 		goto fail2;
232 	}
233 
234 	error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
235 	if (error != 0) {
236 		device_printf(sc->sc_dev, "could not allocate Prio ring\n");
237 		goto fail3;
238 	}
239 
240 	error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
241 	if (error != 0) {
242 		device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
243 		goto fail4;
244 	}
245 
246 	error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
247 	if (error != 0) {
248 		device_printf(sc->sc_dev, "could not allocate Rx ring\n");
249 		goto fail5;
250 	}
251 
252 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
253 	if (ifp == NULL) {
254 		device_printf(sc->sc_dev, "can not if_alloc()\n");
255 		goto fail6;
256 	}
257 	ic = ifp->if_l2com;
258 
259 	/* retrieve MAC address */
260 	rt2560_get_macaddr(sc, macaddr);
261 
262 	ifp->if_softc = sc;
263 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
264 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
265 	ifp->if_init = rt2560_init;
266 	ifp->if_ioctl = rt2560_ioctl;
267 	ifp->if_start = rt2560_start;
268 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
269 	ifq_set_ready(&ifp->if_snd);
270 
271 	ic->ic_ifp = ifp;
272 	ic->ic_opmode = IEEE80211_M_STA;
273 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
274 
275 	/* set device capabilities */
276 	ic->ic_caps =
277 		  IEEE80211_C_STA		/* station mode */
278 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
279 		| IEEE80211_C_HOSTAP		/* hostap mode */
280 		| IEEE80211_C_MONITOR		/* monitor mode */
281 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
282 		| IEEE80211_C_WDS		/* 4-address traffic works */
283 		| IEEE80211_C_MBSS		/* mesh point link mode */
284 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
285 		| IEEE80211_C_SHSLOT		/* short slot time supported */
286 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
287 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
288 #ifdef notyet
289 		| IEEE80211_C_TXFRAG		/* handle tx frags */
290 #endif
291 		;
292 
293 	bands = 0;
294 	setbit(&bands, IEEE80211_MODE_11B);
295 	setbit(&bands, IEEE80211_MODE_11G);
296 	if (sc->rf_rev == RT2560_RF_5222)
297 		setbit(&bands, IEEE80211_MODE_11A);
298 	ieee80211_init_channels(ic, NULL, &bands);
299 
300 	ieee80211_ifattach(ic, macaddr);
301 	ic->ic_raw_xmit = rt2560_raw_xmit;
302 	ic->ic_updateslot = rt2560_update_slot;
303 	ic->ic_update_promisc = rt2560_update_promisc;
304 	ic->ic_scan_start = rt2560_scan_start;
305 	ic->ic_scan_end = rt2560_scan_end;
306 	ic->ic_set_channel = rt2560_set_channel;
307 
308 	ic->ic_vap_create = rt2560_vap_create;
309 	ic->ic_vap_delete = rt2560_vap_delete;
310 
311 	ieee80211_radiotap_attach(ic,
312 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
313 		RT2560_TX_RADIOTAP_PRESENT,
314 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
315 		RT2560_RX_RADIOTAP_PRESENT);
316 
317 	/*
318 	 * Add a few sysctl knobs.
319 	 */
320 	ctx = &sc->sc_sysctl_ctx;
321 	sysctl_ctx_init(ctx);
322 	tree = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw),
323 				OID_AUTO,
324 				device_get_nameunit(sc->sc_dev),
325 				CTLFLAG_RD, 0, "");
326         if (tree == NULL) {
327                 device_printf(sc->sc_dev, "can't add sysctl node\n");
328                 goto fail6;
329         }
330 
331 #ifdef RAL_DEBUG
332 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
333 	    "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
334 #endif
335 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
336 	    "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
337 
338 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
339 	    "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
340 
341 	if (bootverbose)
342 		ieee80211_announce(ic);
343 
344 	return 0;
345 
346 fail6:	rt2560_free_rx_ring(sc, &sc->rxq);
347 fail5:	rt2560_free_tx_ring(sc, &sc->bcnq);
348 fail4:	rt2560_free_tx_ring(sc, &sc->prioq);
349 fail3:	rt2560_free_tx_ring(sc, &sc->atimq);
350 fail2:	rt2560_free_tx_ring(sc, &sc->txq);
351 fail1:
352 
353 	return ENXIO;
354 }
355 
356 int
357 rt2560_detach(void *xsc)
358 {
359 	struct rt2560_softc *sc = xsc;
360 	struct ifnet *ifp = sc->sc_ifp;
361 	struct ieee80211com *ic = ifp->if_l2com;
362 
363 	rt2560_stop(sc);
364 
365 	ieee80211_ifdetach(ic);
366 
367 	rt2560_free_tx_ring(sc, &sc->txq);
368 	rt2560_free_tx_ring(sc, &sc->atimq);
369 	rt2560_free_tx_ring(sc, &sc->prioq);
370 	rt2560_free_tx_ring(sc, &sc->bcnq);
371 	rt2560_free_rx_ring(sc, &sc->rxq);
372 
373 	if_free(ifp);
374 
375 	return 0;
376 }
377 
378 static struct ieee80211vap *
379 rt2560_vap_create(struct ieee80211com *ic,
380 	const char name[IFNAMSIZ], int unit, int 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 *) kmalloc(sizeof(struct rt2560_vap),
421 	    M_80211_VAP, M_INTWAIT | 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 	kfree(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(ring->desc_dmat, 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, &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 = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
507 	    M_INTWAIT | 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(ring->data_dmat, 1, 0,
515 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
516 	    MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, &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 		kfree(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(ring->desc_dmat, 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, &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 = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
650 	    M_INTWAIT | 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(ring->data_dmat, 1, 0,
661 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
662 	    1, MCLBYTES, 0, &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(MB_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 		kfree(ring->data, M_DEVBUF);
755 	}
756 
757 	if (ring->data_dmat != NULL)
758 		bus_dma_tag_destroy(ring->data_dmat);
759 }
760 
761 static int
762 rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
763 {
764 	struct rt2560_vap *rvp = RT2560_VAP(vap);
765 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
766 	struct rt2560_softc *sc = ifp->if_softc;
767 	int error;
768 
769 	if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
770 		/* abort TSF synchronization */
771 		RAL_WRITE(sc, RT2560_CSR14, 0);
772 
773 		/* turn association led off */
774 		rt2560_update_led(sc, 0, 0);
775 	}
776 
777 	error = rvp->ral_newstate(vap, nstate, arg);
778 
779 	if (error == 0 && nstate == IEEE80211_S_RUN) {
780 		struct ieee80211_node *ni = vap->iv_bss;
781 		struct mbuf *m;
782 
783 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
784 			rt2560_update_plcp(sc);
785 			rt2560_set_basicrates(sc);
786 			rt2560_set_bssid(sc, ni->ni_bssid);
787 		}
788 
789 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
790 		    vap->iv_opmode == IEEE80211_M_IBSS ||
791 		    vap->iv_opmode == IEEE80211_M_MBSS) {
792 			m = ieee80211_beacon_alloc(ni, &rvp->ral_bo);
793 			if (m == NULL) {
794 				if_printf(ifp, "could not allocate beacon\n");
795 				return ENOBUFS;
796 			}
797 			ieee80211_ref_node(ni);
798 			error = rt2560_tx_bcn(sc, m, ni);
799 			if (error != 0)
800 				return error;
801 		}
802 
803 		/* turn assocation led on */
804 		rt2560_update_led(sc, 1, 0);
805 
806 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
807 			rt2560_enable_tsf_sync(sc);
808 		else
809 			rt2560_enable_tsf(sc);
810 	}
811 	return error;
812 }
813 
814 /*
815  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
816  * 93C66).
817  */
818 static uint16_t
819 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
820 {
821 	uint32_t tmp;
822 	uint16_t val;
823 	int n;
824 
825 	/* clock C once before the first command */
826 	RT2560_EEPROM_CTL(sc, 0);
827 
828 	RT2560_EEPROM_CTL(sc, RT2560_S);
829 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
830 	RT2560_EEPROM_CTL(sc, RT2560_S);
831 
832 	/* write start bit (1) */
833 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
834 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
835 
836 	/* write READ opcode (10) */
837 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
838 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
839 	RT2560_EEPROM_CTL(sc, RT2560_S);
840 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
841 
842 	/* write address (A5-A0 or A7-A0) */
843 	n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
844 	for (; n >= 0; n--) {
845 		RT2560_EEPROM_CTL(sc, RT2560_S |
846 		    (((addr >> n) & 1) << RT2560_SHIFT_D));
847 		RT2560_EEPROM_CTL(sc, RT2560_S |
848 		    (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
849 	}
850 
851 	RT2560_EEPROM_CTL(sc, RT2560_S);
852 
853 	/* read data Q15-Q0 */
854 	val = 0;
855 	for (n = 15; n >= 0; n--) {
856 		RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
857 		tmp = RAL_READ(sc, RT2560_CSR21);
858 		val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
859 		RT2560_EEPROM_CTL(sc, RT2560_S);
860 	}
861 
862 	RT2560_EEPROM_CTL(sc, 0);
863 
864 	/* clear Chip Select and clock C */
865 	RT2560_EEPROM_CTL(sc, RT2560_S);
866 	RT2560_EEPROM_CTL(sc, 0);
867 	RT2560_EEPROM_CTL(sc, RT2560_C);
868 
869 	return val;
870 }
871 
872 /*
873  * Some frames were processed by the hardware cipher engine and are ready for
874  * transmission.
875  */
876 static void
877 rt2560_encryption_intr(struct rt2560_softc *sc)
878 {
879 	struct rt2560_tx_desc *desc;
880 	int hw;
881 
882 	/* retrieve last descriptor index processed by cipher engine */
883 	hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
884 	hw /= RT2560_TX_DESC_SIZE;
885 
886 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
887 	    BUS_DMASYNC_POSTREAD);
888 
889 	while (sc->txq.next_encrypt != hw) {
890 		if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
891 			kprintf("hw encrypt %d, cur_encrypt %d\n", hw,
892 			    sc->txq.cur_encrypt);
893 			break;
894 		}
895 
896 		desc = &sc->txq.desc[sc->txq.next_encrypt];
897 
898 		if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
899 		    (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
900 			break;
901 
902 		/* for TKIP, swap eiv field to fix a bug in ASIC */
903 		if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
904 		    RT2560_TX_CIPHER_TKIP)
905 			desc->eiv = bswap32(desc->eiv);
906 
907 		/* mark the frame ready for transmission */
908 		desc->flags |= htole32(RT2560_TX_VALID);
909 		desc->flags |= htole32(RT2560_TX_BUSY);
910 
911 		DPRINTFN(sc, 15, "encryption done idx=%u\n",
912 		    sc->txq.next_encrypt);
913 
914 		sc->txq.next_encrypt =
915 		    (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
916 	}
917 
918 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
919 	    BUS_DMASYNC_PREWRITE);
920 
921 	/* kick Tx */
922 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
923 }
924 
925 static void
926 rt2560_tx_intr(struct rt2560_softc *sc)
927 {
928 	struct ifnet *ifp = sc->sc_ifp;
929 	struct rt2560_tx_desc *desc;
930 	struct rt2560_tx_data *data;
931 	struct mbuf *m;
932 	uint32_t flags;
933 	int retrycnt;
934 	struct ieee80211vap *vap;
935 	struct ieee80211_node *ni;
936 
937 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
938 	    BUS_DMASYNC_POSTREAD);
939 
940 	for (;;) {
941 		desc = &sc->txq.desc[sc->txq.next];
942 		data = &sc->txq.data[sc->txq.next];
943 
944 		flags = le32toh(desc->flags);
945 		if ((flags & RT2560_TX_BUSY) ||
946 		    (flags & RT2560_TX_CIPHER_BUSY) ||
947 		    !(flags & RT2560_TX_VALID))
948 			break;
949 
950 		m = data->m;
951 		ni = data->ni;
952 		vap = ni->ni_vap;
953 
954 		switch (flags & RT2560_TX_RESULT_MASK) {
955 		case RT2560_TX_SUCCESS:
956 			retrycnt = 0;
957 
958 			DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
959 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
960 				ieee80211_ratectl_tx_complete(vap, ni,
961 				    IEEE80211_RATECTL_TX_SUCCESS,
962 				    &retrycnt, NULL);
963 			ifp->if_opackets++;
964 			break;
965 
966 		case RT2560_TX_SUCCESS_RETRY:
967 			retrycnt = RT2560_TX_RETRYCNT(flags);
968 
969 			DPRINTFN(sc, 9, "data frame sent after %u retries\n",
970 			    retrycnt);
971 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
972 				ieee80211_ratectl_tx_complete(vap, ni,
973 				    IEEE80211_RATECTL_TX_SUCCESS,
974 				    &retrycnt, NULL);
975 			ifp->if_opackets++;
976 			break;
977 
978 		case RT2560_TX_FAIL_RETRY:
979 			retrycnt = RT2560_TX_RETRYCNT(flags);
980 
981 			DPRINTFN(sc, 9, "data frame failed after %d retries\n",
982 			    retrycnt);
983 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
984 				ieee80211_ratectl_tx_complete(vap, ni,
985 				    IEEE80211_RATECTL_TX_FAILURE,
986 				    &retrycnt, NULL);
987 			ifp->if_oerrors++;
988 			break;
989 
990 		case RT2560_TX_FAIL_INVALID:
991 		case RT2560_TX_FAIL_OTHER:
992 		default:
993 			device_printf(sc->sc_dev, "sending data frame failed "
994 			    "0x%08x\n", flags);
995 			ifp->if_oerrors++;
996 		}
997 
998 		bus_dmamap_sync(sc->txq.data_dmat, data->map,
999 		    BUS_DMASYNC_POSTWRITE);
1000 		bus_dmamap_unload(sc->txq.data_dmat, data->map);
1001 		m_freem(m);
1002 		data->m = NULL;
1003 		ieee80211_free_node(data->ni);
1004 		data->ni = NULL;
1005 		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_flags &= ~IFF_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_flags &= ~IFF_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 			ifp->if_ierrors++;
1150 			goto skip;
1151 		}
1152 
1153 		if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1154 		    (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1155 			ifp->if_ierrors++;
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(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1167 		if (mnew == NULL) {
1168 			ifp->if_ierrors++;
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 			ifp->if_ierrors++;
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 		wh = mtod(m, struct ieee80211_frame *);
1231 		ni = ieee80211_find_rxnode(ic,
1232 		    (struct ieee80211_frame_min *)wh);
1233 		if (ni != NULL) {
1234 			(void) ieee80211_input(ni, m, rssi, nf);
1235 			ieee80211_free_node(ni);
1236 		} else
1237 			(void) ieee80211_input_all(ic, m, rssi, nf);
1238 
1239 		sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1240 skip:		desc->flags = htole32(RT2560_RX_BUSY);
1241 
1242 		DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1243 
1244 		sc->rxq.cur_decrypt =
1245 		    (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1246 	}
1247 
1248 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1249 	    BUS_DMASYNC_PREWRITE);
1250 }
1251 
1252 /*
1253  * Some frames were received. Pass them to the hardware cipher engine before
1254  * sending them to the 802.11 layer.
1255  */
1256 static void
1257 rt2560_rx_intr(struct rt2560_softc *sc)
1258 {
1259 	struct rt2560_rx_desc *desc;
1260 	struct rt2560_rx_data *data;
1261 
1262 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1263 	    BUS_DMASYNC_POSTREAD);
1264 
1265 	for (;;) {
1266 		desc = &sc->rxq.desc[sc->rxq.cur];
1267 		data = &sc->rxq.data[sc->rxq.cur];
1268 
1269 		if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1270 		    (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1271 			break;
1272 
1273 		data->drop = 0;
1274 
1275 		if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1276 		    (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1277 			/*
1278 			 * This should not happen since we did not request
1279 			 * to receive those frames when we filled RXCSR0.
1280 			 */
1281 			DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1282 			    le32toh(desc->flags));
1283 			data->drop = 1;
1284 		}
1285 
1286 		if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1287 			DPRINTFN(sc, 5, "%s\n", "bad length");
1288 			data->drop = 1;
1289 		}
1290 
1291 		/* mark the frame for decryption */
1292 		desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1293 
1294 		DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1295 
1296 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1297 	}
1298 
1299 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1300 	    BUS_DMASYNC_PREWRITE);
1301 
1302 	/* kick decrypt */
1303 	RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1304 }
1305 
1306 static void
1307 rt2560_beacon_update(struct ieee80211vap *vap, int item)
1308 {
1309 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1310 	struct ieee80211_beacon_offsets *bo = &rvp->ral_bo;
1311 
1312 	setbit(bo->bo_flags, item);
1313 }
1314 
1315 /*
1316  * This function is called periodically in IBSS mode when a new beacon must be
1317  * sent out.
1318  */
1319 static void
1320 rt2560_beacon_expire(struct rt2560_softc *sc)
1321 {
1322 	struct ifnet *ifp = sc->sc_ifp;
1323 	struct ieee80211com *ic = ifp->if_l2com;
1324 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1325 	struct rt2560_vap *rvp = RT2560_VAP(vap);
1326 	struct rt2560_tx_data *data;
1327 
1328 	if (ic->ic_opmode != IEEE80211_M_IBSS &&
1329 	    ic->ic_opmode != IEEE80211_M_HOSTAP &&
1330 	    ic->ic_opmode != IEEE80211_M_MBSS)
1331 		return;
1332 
1333 	data = &sc->bcnq.data[sc->bcnq.next];
1334 	/*
1335 	 * Don't send beacon if bsschan isn't set
1336 	 */
1337 	if (data->ni == NULL)
1338 	        return;
1339 
1340 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1341 	bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1342 
1343 	/* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */
1344 	ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1);
1345 
1346 	rt2560_tx_bcn(sc, data->m, data->ni);
1347 
1348 	DPRINTFN(sc, 15, "%s", "beacon expired\n");
1349 
1350 	sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1351 }
1352 
1353 /* ARGSUSED */
1354 static void
1355 rt2560_wakeup_expire(struct rt2560_softc *sc)
1356 {
1357 	DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1358 }
1359 
1360 void
1361 rt2560_intr(void *arg)
1362 {
1363 	struct rt2560_softc *sc = arg;
1364 	struct ifnet *ifp = sc->sc_ifp;
1365 	uint32_t r;
1366 
1367 	/* disable interrupts */
1368 	RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1369 
1370 	/* don't re-enable interrupts if we're shutting down */
1371 	if (!(ifp->if_flags & IFF_RUNNING)) {
1372 		return;
1373 	}
1374 
1375 	r = RAL_READ(sc, RT2560_CSR7);
1376 	RAL_WRITE(sc, RT2560_CSR7, r);
1377 
1378 	if (r & RT2560_BEACON_EXPIRE)
1379 		rt2560_beacon_expire(sc);
1380 
1381 	if (r & RT2560_WAKEUP_EXPIRE)
1382 		rt2560_wakeup_expire(sc);
1383 
1384 	if (r & RT2560_ENCRYPTION_DONE)
1385 		rt2560_encryption_intr(sc);
1386 
1387 	if (r & RT2560_TX_DONE)
1388 		rt2560_tx_intr(sc);
1389 
1390 	if (r & RT2560_PRIO_DONE)
1391 		rt2560_prio_intr(sc);
1392 
1393 	if (r & RT2560_DECRYPTION_DONE)
1394 		rt2560_decryption_intr(sc);
1395 
1396 	if (r & RT2560_RX_DONE) {
1397 		rt2560_rx_intr(sc);
1398 		rt2560_encryption_intr(sc);
1399 	}
1400 
1401 	/* re-enable interrupts */
1402 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1403 }
1404 
1405 #define RAL_SIFS		10	/* us */
1406 
1407 #define RT2560_TXRX_TURNAROUND	10	/* us */
1408 
1409 static uint8_t
1410 rt2560_plcp_signal(int rate)
1411 {
1412 	switch (rate) {
1413 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1414 	case 12:	return 0xb;
1415 	case 18:	return 0xf;
1416 	case 24:	return 0xa;
1417 	case 36:	return 0xe;
1418 	case 48:	return 0x9;
1419 	case 72:	return 0xd;
1420 	case 96:	return 0x8;
1421 	case 108:	return 0xc;
1422 
1423 	/* CCK rates (NB: not IEEE std, device-specific) */
1424 	case 2:		return 0x0;
1425 	case 4:		return 0x1;
1426 	case 11:	return 0x2;
1427 	case 22:	return 0x3;
1428 	}
1429 	return 0xff;		/* XXX unsupported/unknown rate */
1430 }
1431 
1432 static void
1433 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1434     uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1435 {
1436 	struct ifnet *ifp = sc->sc_ifp;
1437 	struct ieee80211com *ic = ifp->if_l2com;
1438 	uint16_t plcp_length;
1439 	int remainder;
1440 
1441 	desc->flags = htole32(flags);
1442 	desc->flags |= htole32(len << 16);
1443 
1444 	desc->physaddr = htole32(physaddr);
1445 	desc->wme = htole16(
1446 	    RT2560_AIFSN(2) |
1447 	    RT2560_LOGCWMIN(3) |
1448 	    RT2560_LOGCWMAX(8));
1449 
1450 	/* setup PLCP fields */
1451 	desc->plcp_signal  = rt2560_plcp_signal(rate);
1452 	desc->plcp_service = 4;
1453 
1454 	len += IEEE80211_CRC_LEN;
1455 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1456 		desc->flags |= htole32(RT2560_TX_OFDM);
1457 
1458 		plcp_length = len & 0xfff;
1459 		desc->plcp_length_hi = plcp_length >> 6;
1460 		desc->plcp_length_lo = plcp_length & 0x3f;
1461 	} else {
1462 		plcp_length = (16 * len + rate - 1) / rate;
1463 		if (rate == 22) {
1464 			remainder = (16 * len) % 22;
1465 			if (remainder != 0 && remainder < 7)
1466 				desc->plcp_service |= RT2560_PLCP_LENGEXT;
1467 		}
1468 		desc->plcp_length_hi = plcp_length >> 8;
1469 		desc->plcp_length_lo = plcp_length & 0xff;
1470 
1471 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1472 			desc->plcp_signal |= 0x08;
1473 	}
1474 
1475 	if (!encrypt)
1476 		desc->flags |= htole32(RT2560_TX_VALID);
1477 	desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1478 			       : htole32(RT2560_TX_BUSY);
1479 }
1480 
1481 static int
1482 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1483     struct ieee80211_node *ni)
1484 {
1485 	struct ieee80211vap *vap = ni->ni_vap;
1486 	struct rt2560_tx_desc *desc;
1487 	struct rt2560_tx_data *data;
1488 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1489 	int nsegs, rate, error;
1490 
1491 	desc = &sc->bcnq.desc[sc->bcnq.cur];
1492 	data = &sc->bcnq.data[sc->bcnq.cur];
1493 
1494 	/* XXX maybe a separate beacon rate? */
1495 	rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate;
1496 
1497 	error = bus_dmamap_load_mbuf_segment(sc->bcnq.data_dmat, data->map, m0,
1498 	    segs, 1, &nsegs, BUS_DMA_NOWAIT);
1499 	if (error != 0) {
1500 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1501 		    error);
1502 		m_freem(m0);
1503 		return error;
1504 	}
1505 
1506 	if (ieee80211_radiotap_active_vap(vap)) {
1507 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1508 
1509 		tap->wt_flags = 0;
1510 		tap->wt_rate = rate;
1511 		tap->wt_antenna = sc->tx_ant;
1512 
1513 		ieee80211_radiotap_tx(vap, m0);
1514 	}
1515 
1516 	data->m = m0;
1517 	data->ni = ni;
1518 
1519 	rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1520 	    RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1521 
1522 	DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1523 	    m0->m_pkthdr.len, sc->bcnq.cur, rate);
1524 
1525 	bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1526 	bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1527 	    BUS_DMASYNC_PREWRITE);
1528 
1529 	sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1530 
1531 	return 0;
1532 }
1533 
1534 static int
1535 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1536     struct ieee80211_node *ni)
1537 {
1538 	struct ieee80211vap *vap = ni->ni_vap;
1539 	struct ieee80211com *ic = ni->ni_ic;
1540 	struct rt2560_tx_desc *desc;
1541 	struct rt2560_tx_data *data;
1542 	struct ieee80211_frame *wh;
1543 	struct ieee80211_key *k;
1544 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1545 	uint16_t dur;
1546 	uint32_t flags = 0;
1547 	int nsegs, rate, error;
1548 
1549 	desc = &sc->prioq.desc[sc->prioq.cur];
1550 	data = &sc->prioq.data[sc->prioq.cur];
1551 
1552 	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
1553 
1554 	wh = mtod(m0, struct ieee80211_frame *);
1555 
1556 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1557 		k = ieee80211_crypto_encap(ni, m0);
1558 		if (k == NULL) {
1559 			m_freem(m0);
1560 			return ENOBUFS;
1561 		}
1562 	}
1563 
1564 	error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0,
1565 	    segs, 1, &nsegs, 0);
1566 	if (error != 0) {
1567 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1568 		    error);
1569 		m_freem(m0);
1570 		return error;
1571 	}
1572 
1573 	if (ieee80211_radiotap_active_vap(vap)) {
1574 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1575 
1576 		tap->wt_flags = 0;
1577 		tap->wt_rate = rate;
1578 		tap->wt_antenna = sc->tx_ant;
1579 
1580 		ieee80211_radiotap_tx(vap, m0);
1581 	}
1582 
1583 	data->m = m0;
1584 	data->ni = ni;
1585 	/* management frames are not taken into account for amrr */
1586 	data->rix = IEEE80211_FIXED_RATE_NONE;
1587 
1588 	wh = mtod(m0, struct ieee80211_frame *);
1589 
1590 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1591 		flags |= RT2560_TX_ACK;
1592 
1593 		dur = ieee80211_ack_duration(ic->ic_rt,
1594 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1595 		*(uint16_t *)wh->i_dur = htole16(dur);
1596 
1597 		/* tell hardware to add timestamp for probe responses */
1598 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1599 		    IEEE80211_FC0_TYPE_MGT &&
1600 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1601 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1602 			flags |= RT2560_TX_TIMESTAMP;
1603 	}
1604 
1605 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1606 	    segs->ds_addr);
1607 
1608 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1609 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1610 	    BUS_DMASYNC_PREWRITE);
1611 
1612 	DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1613 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1614 
1615 	/* kick prio */
1616 	sc->prioq.queued++;
1617 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1618 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1619 
1620 	return 0;
1621 }
1622 
1623 static int
1624 rt2560_sendprot(struct rt2560_softc *sc,
1625     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1626 {
1627 	struct ieee80211com *ic = ni->ni_ic;
1628 	const struct ieee80211_frame *wh;
1629 	struct rt2560_tx_desc *desc;
1630 	struct rt2560_tx_data *data;
1631 	struct mbuf *mprot;
1632 	int protrate, ackrate, pktlen, flags, isshort, error;
1633 	uint16_t dur;
1634 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1635 	int nsegs;
1636 
1637 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1638 	    ("protection %d", prot));
1639 
1640 	wh = mtod(m, const struct ieee80211_frame *);
1641 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1642 
1643 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1644 	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1645 
1646 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1647 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1648 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1649 	flags = RT2560_TX_MORE_FRAG;
1650 	if (prot == IEEE80211_PROT_RTSCTS) {
1651 		/* NB: CTS is the same size as an ACK */
1652 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1653 		flags |= RT2560_TX_ACK;
1654 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1655 	} else {
1656 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1657 	}
1658 	if (mprot == NULL) {
1659 		/* XXX stat + msg */
1660 		return ENOBUFS;
1661 	}
1662 
1663 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1664 	data = &sc->txq.data[sc->txq.cur_encrypt];
1665 
1666 	error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map,
1667 	    mprot, segs, 1, &nsegs, 0);
1668 	if (error != 0) {
1669 		device_printf(sc->sc_dev,
1670 		    "could not map mbuf (error %d)\n", error);
1671 		m_freem(mprot);
1672 		return error;
1673 	}
1674 
1675 	data->m = mprot;
1676 	data->ni = ieee80211_ref_node(ni);
1677 	/* ctl frames are not taken into account for amrr */
1678 	data->rix = IEEE80211_FIXED_RATE_NONE;
1679 
1680 	rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1681 	    segs->ds_addr);
1682 
1683 	bus_dmamap_sync(sc->txq.data_dmat, data->map,
1684 	    BUS_DMASYNC_PREWRITE);
1685 
1686 	sc->txq.queued++;
1687 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1688 
1689 	return 0;
1690 }
1691 
1692 static int
1693 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1694     struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1695 {
1696 	struct ieee80211vap *vap = ni->ni_vap;
1697 	struct ieee80211com *ic = ni->ni_ic;
1698 	struct rt2560_tx_desc *desc;
1699 	struct rt2560_tx_data *data;
1700 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1701 	uint32_t flags;
1702 	int nsegs, rate, error;
1703 
1704 	desc = &sc->prioq.desc[sc->prioq.cur];
1705 	data = &sc->prioq.data[sc->prioq.cur];
1706 
1707 	rate = params->ibp_rate0;
1708 	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1709 		/* XXX fall back to mcast/mgmt rate? */
1710 		m_freem(m0);
1711 		return EINVAL;
1712 	}
1713 
1714 	flags = 0;
1715 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1716 		flags |= RT2560_TX_ACK;
1717 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1718 		error = rt2560_sendprot(sc, m0, ni,
1719 		    params->ibp_flags & IEEE80211_BPF_RTS ?
1720 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1721 		    rate);
1722 		if (error) {
1723 			m_freem(m0);
1724 			return error;
1725 		}
1726 		flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1727 	}
1728 
1729 	error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0,
1730 	    segs, 1, &nsegs, 0);
1731 	if (error != 0) {
1732 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1733 		    error);
1734 		m_freem(m0);
1735 		return error;
1736 	}
1737 
1738 	if (ieee80211_radiotap_active_vap(vap)) {
1739 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1740 
1741 		tap->wt_flags = 0;
1742 		tap->wt_rate = rate;
1743 		tap->wt_antenna = sc->tx_ant;
1744 
1745 		ieee80211_radiotap_tx(ni->ni_vap, m0);
1746 	}
1747 
1748 	data->m = m0;
1749 	data->ni = ni;
1750 
1751 	/* XXX need to setup descriptor ourself */
1752 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1753 	    rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1754 	    segs->ds_addr);
1755 
1756 	bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1757 	bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1758 	    BUS_DMASYNC_PREWRITE);
1759 
1760 	DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1761 	    m0->m_pkthdr.len, sc->prioq.cur, rate);
1762 
1763 	/* kick prio */
1764 	sc->prioq.queued++;
1765 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1766 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1767 
1768 	return 0;
1769 }
1770 
1771 static int
1772 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1773     struct ieee80211_node *ni)
1774 {
1775 	struct ieee80211vap *vap = ni->ni_vap;
1776 	struct ieee80211com *ic = ni->ni_ic;
1777 	struct rt2560_tx_desc *desc;
1778 	struct rt2560_tx_data *data;
1779 	struct ieee80211_frame *wh;
1780 	const struct ieee80211_txparam *tp;
1781 	struct ieee80211_key *k;
1782 	struct mbuf *mnew;
1783 	bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1784 	uint16_t dur;
1785 	uint32_t flags;
1786 	int nsegs, rate, error;
1787 
1788 	wh = mtod(m0, struct ieee80211_frame *);
1789 
1790 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1791 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1792 		rate = tp->mcastrate;
1793 	} else if (m0->m_flags & M_EAPOL) {
1794 		rate = tp->mgmtrate;
1795 	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1796 		rate = tp->ucastrate;
1797 	} else {
1798 		(void) ieee80211_ratectl_rate(ni, NULL, 0);
1799 		rate = ni->ni_txrate;
1800 	}
1801 
1802 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1803 		k = ieee80211_crypto_encap(ni, m0);
1804 		if (k == NULL) {
1805 			m_freem(m0);
1806 			return ENOBUFS;
1807 		}
1808 
1809 		/* packet header may have moved, reset our local pointer */
1810 		wh = mtod(m0, struct ieee80211_frame *);
1811 	}
1812 
1813 	flags = 0;
1814 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1815 		int prot = IEEE80211_PROT_NONE;
1816 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1817 			prot = IEEE80211_PROT_RTSCTS;
1818 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1819 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1820 			prot = ic->ic_protmode;
1821 		if (prot != IEEE80211_PROT_NONE) {
1822 			error = rt2560_sendprot(sc, m0, ni, prot, rate);
1823 			if (error) {
1824 				m_freem(m0);
1825 				return error;
1826 			}
1827 			flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1828 		}
1829 	}
1830 
1831 	data = &sc->txq.data[sc->txq.cur_encrypt];
1832 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
1833 
1834 	error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map, m0,
1835 	    segs, 1, &nsegs, 0);
1836 	if (error != 0 && error != EFBIG) {
1837 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1838 		    error);
1839 		m_freem(m0);
1840 		return error;
1841 	}
1842 	if (error != 0) {
1843 		mnew = m_defrag(m0, MB_DONTWAIT);
1844 		if (mnew == NULL) {
1845 			device_printf(sc->sc_dev,
1846 			    "could not defragment mbuf\n");
1847 			m_freem(m0);
1848 			return ENOBUFS;
1849 		}
1850 		m0 = mnew;
1851 
1852 		error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map,
1853 		    m0, segs, 1, &nsegs, 0);
1854 		if (error != 0) {
1855 			device_printf(sc->sc_dev,
1856 			    "could not map mbuf (error %d)\n", error);
1857 			m_freem(m0);
1858 			return error;
1859 		}
1860 
1861 		/* packet header may have moved, reset our local pointer */
1862 		wh = mtod(m0, struct ieee80211_frame *);
1863 	}
1864 
1865 	if (ieee80211_radiotap_active_vap(vap)) {
1866 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1867 
1868 		tap->wt_flags = 0;
1869 		tap->wt_rate = rate;
1870 		tap->wt_antenna = sc->tx_ant;
1871 
1872 		ieee80211_radiotap_tx(vap, m0);
1873 	}
1874 
1875 	data->m = m0;
1876 	data->ni = ni;
1877 
1878 	/* remember link conditions for rate adaptation algorithm */
1879 	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1880 		data->rix = ni->ni_txrate;
1881 		/* XXX probably need last rssi value and not avg */
1882 		data->rssi = ic->ic_node_getrssi(ni);
1883 	} else
1884 		data->rix = IEEE80211_FIXED_RATE_NONE;
1885 
1886 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1887 		flags |= RT2560_TX_ACK;
1888 
1889 		dur = ieee80211_ack_duration(ic->ic_rt,
1890 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1891 		*(uint16_t *)wh->i_dur = htole16(dur);
1892 	}
1893 
1894 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1895 	    segs->ds_addr);
1896 
1897 	bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1898 	bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1899 	    BUS_DMASYNC_PREWRITE);
1900 
1901 	DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1902 	    m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1903 
1904 	/* kick encrypt */
1905 	sc->txq.queued++;
1906 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1907 	RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1908 
1909 	return 0;
1910 }
1911 
1912 static void
1913 rt2560_start_locked(struct ifnet *ifp)
1914 {
1915 	struct rt2560_softc *sc = ifp->if_softc;
1916 	struct mbuf *m;
1917 	struct ieee80211_node *ni;
1918 
1919 	for (;;) {
1920 		IF_DEQUEUE(&ifp->if_snd, m);
1921 		if (m == NULL)
1922 			break;
1923 		if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1924 			IF_PREPEND(&ifp->if_snd, m);
1925 			ifp->if_flags |= IFF_OACTIVE;
1926 			sc->sc_flags |= RT2560_F_DATA_OACTIVE;
1927 			break;
1928 		}
1929 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1930 		if (rt2560_tx_data(sc, m, ni) != 0) {
1931 			ieee80211_free_node(ni);
1932 			ifp->if_oerrors++;
1933 			break;
1934 		}
1935 
1936 		sc->sc_tx_timer = 5;
1937 	}
1938 }
1939 
1940 static void
1941 rt2560_start(struct ifnet *ifp)
1942 {
1943 	rt2560_start_locked(ifp);
1944 }
1945 
1946 static void
1947 rt2560_watchdog_callout(void *arg)
1948 {
1949 	struct rt2560_softc *sc = arg;
1950 	struct ifnet *ifp = sc->sc_ifp;
1951 
1952 	KASSERT(ifp->if_flags & IFF_RUNNING, ("not running"));
1953 
1954 	if (sc->sc_invalid)		/* card ejected */
1955 		return;
1956 
1957 	rt2560_encryption_intr(sc);
1958 	rt2560_tx_intr(sc);
1959 
1960 	if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
1961 		if_printf(ifp, "device timeout\n");
1962 		rt2560_init_locked(sc);
1963 		ifp->if_oerrors++;
1964 		/* NB: callout is reset in rt2560_init() */
1965 		return;
1966 	}
1967 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc);
1968 }
1969 
1970 static int
1971 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1972 {
1973 	struct rt2560_softc *sc = ifp->if_softc;
1974 	struct ieee80211com *ic = ifp->if_l2com;
1975 	struct ifreq *ifr = (struct ifreq *) data;
1976 	int error = 0, startall = 0;
1977 
1978 	switch (cmd) {
1979 	case SIOCSIFFLAGS:
1980 		if (ifp->if_flags & IFF_UP) {
1981 			if ((ifp->if_flags & IFF_RUNNING) == 0) {
1982 				rt2560_init_locked(sc);
1983 				startall = 1;
1984 			} else
1985 				rt2560_update_promisc(ifp);
1986 		} else {
1987 			if (ifp->if_flags & IFF_RUNNING)
1988 				rt2560_stop_locked(sc);
1989 		}
1990 		if (startall)
1991 			ieee80211_start_all(ic);
1992 		break;
1993 	case SIOCGIFMEDIA:
1994 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1995 		break;
1996 	case SIOCGIFADDR:
1997 		error = ether_ioctl(ifp, cmd, data);
1998 		break;
1999 	default:
2000 		error = EINVAL;
2001 		break;
2002 	}
2003 	return error;
2004 }
2005 
2006 static void
2007 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2008 {
2009 	uint32_t tmp;
2010 	int ntries;
2011 
2012 	for (ntries = 0; ntries < 100; ntries++) {
2013 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2014 			break;
2015 		DELAY(1);
2016 	}
2017 	if (ntries == 100) {
2018 		device_printf(sc->sc_dev, "could not write to BBP\n");
2019 		return;
2020 	}
2021 
2022 	tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2023 	RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2024 
2025 	DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
2026 }
2027 
2028 static uint8_t
2029 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2030 {
2031 	uint32_t val;
2032 	int ntries;
2033 
2034 	for (ntries = 0; ntries < 100; ntries++) {
2035 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2036 			break;
2037 		DELAY(1);
2038 	}
2039 	if (ntries == 100) {
2040 		device_printf(sc->sc_dev, "could not read from BBP\n");
2041 		return 0;
2042 	}
2043 
2044 	val = RT2560_BBP_BUSY | reg << 8;
2045 	RAL_WRITE(sc, RT2560_BBPCSR, val);
2046 
2047 	for (ntries = 0; ntries < 100; ntries++) {
2048 		val = RAL_READ(sc, RT2560_BBPCSR);
2049 		if (!(val & RT2560_BBP_BUSY))
2050 			return val & 0xff;
2051 		DELAY(1);
2052 	}
2053 
2054 	device_printf(sc->sc_dev, "could not read from BBP\n");
2055 	return 0;
2056 }
2057 
2058 static void
2059 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2060 {
2061 	uint32_t tmp;
2062 	int ntries;
2063 
2064 	for (ntries = 0; ntries < 100; ntries++) {
2065 		if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2066 			break;
2067 		DELAY(1);
2068 	}
2069 	if (ntries == 100) {
2070 		device_printf(sc->sc_dev, "could not write to RF\n");
2071 		return;
2072 	}
2073 
2074 	tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2075 	    (reg & 0x3);
2076 	RAL_WRITE(sc, RT2560_RFCSR, tmp);
2077 
2078 	/* remember last written value in sc */
2079 	sc->rf_regs[reg] = val;
2080 
2081 	DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2082 }
2083 
2084 static void
2085 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2086 {
2087 	struct ifnet *ifp = sc->sc_ifp;
2088 	struct ieee80211com *ic = ifp->if_l2com;
2089 	uint8_t power, tmp;
2090 	u_int i, chan;
2091 
2092 	chan = ieee80211_chan2ieee(ic, c);
2093 	KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
2094 
2095 	if (IEEE80211_IS_CHAN_2GHZ(c))
2096 		power = min(sc->txpow[chan - 1], 31);
2097 	else
2098 		power = 31;
2099 
2100 	/* adjust txpower using ifconfig settings */
2101 	power -= (100 - ic->ic_txpowlimit) / 8;
2102 
2103 	DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2104 
2105 	switch (sc->rf_rev) {
2106 	case RT2560_RF_2522:
2107 		rt2560_rf_write(sc, RAL_RF1, 0x00814);
2108 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2109 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2110 		break;
2111 
2112 	case RT2560_RF_2523:
2113 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2114 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2115 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2116 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2117 		break;
2118 
2119 	case RT2560_RF_2524:
2120 		rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2121 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2122 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2123 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2124 		break;
2125 
2126 	case RT2560_RF_2525:
2127 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2128 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2129 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2130 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2131 
2132 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2133 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2134 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2135 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2136 		break;
2137 
2138 	case RT2560_RF_2525E:
2139 		rt2560_rf_write(sc, RAL_RF1, 0x08808);
2140 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2141 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2142 		rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2143 		break;
2144 
2145 	case RT2560_RF_2526:
2146 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2147 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2148 		rt2560_rf_write(sc, RAL_RF1, 0x08804);
2149 
2150 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2151 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2152 		rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2153 		break;
2154 
2155 	/* dual-band RF */
2156 	case RT2560_RF_5222:
2157 		for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2158 
2159 		rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2160 		rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2161 		rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2162 		rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2163 		break;
2164 	default:
2165  	        kprintf("unknown ral rev=%d\n", sc->rf_rev);
2166 	}
2167 
2168 	/* XXX */
2169 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2170 		/* set Japan filter bit for channel 14 */
2171 		tmp = rt2560_bbp_read(sc, 70);
2172 
2173 		tmp &= ~RT2560_JAPAN_FILTER;
2174 		if (chan == 14)
2175 			tmp |= RT2560_JAPAN_FILTER;
2176 
2177 		rt2560_bbp_write(sc, 70, tmp);
2178 
2179 		/* clear CRC errors */
2180 		RAL_READ(sc, RT2560_CNT0);
2181 	}
2182 }
2183 
2184 static void
2185 rt2560_set_channel(struct ieee80211com *ic)
2186 {
2187 	struct ifnet *ifp = ic->ic_ifp;
2188 	struct rt2560_softc *sc = ifp->if_softc;
2189 
2190 	rt2560_set_chan(sc, ic->ic_curchan);
2191 
2192 }
2193 
2194 #if 0
2195 /*
2196  * Disable RF auto-tuning.
2197  */
2198 static void
2199 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2200 {
2201 	uint32_t tmp;
2202 
2203 	if (sc->rf_rev != RT2560_RF_2523) {
2204 		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2205 		rt2560_rf_write(sc, RAL_RF1, tmp);
2206 	}
2207 
2208 	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2209 	rt2560_rf_write(sc, RAL_RF3, tmp);
2210 
2211 	DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2212 }
2213 #endif
2214 
2215 /*
2216  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2217  * synchronization.
2218  */
2219 static void
2220 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2221 {
2222 	struct ifnet *ifp = sc->sc_ifp;
2223 	struct ieee80211com *ic = ifp->if_l2com;
2224 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2225 	uint16_t logcwmin, preload;
2226 	uint32_t tmp;
2227 
2228 	/* first, disable TSF synchronization */
2229 	RAL_WRITE(sc, RT2560_CSR14, 0);
2230 
2231 	tmp = 16 * vap->iv_bss->ni_intval;
2232 	RAL_WRITE(sc, RT2560_CSR12, tmp);
2233 
2234 	RAL_WRITE(sc, RT2560_CSR13, 0);
2235 
2236 	logcwmin = 5;
2237 	preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024;
2238 	tmp = logcwmin << 16 | preload;
2239 	RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2240 
2241 	/* finally, enable TSF synchronization */
2242 	tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2243 	if (ic->ic_opmode == IEEE80211_M_STA)
2244 		tmp |= RT2560_ENABLE_TSF_SYNC(1);
2245 	else
2246 		tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2247 		       RT2560_ENABLE_BEACON_GENERATOR;
2248 	RAL_WRITE(sc, RT2560_CSR14, tmp);
2249 
2250 	DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2251 }
2252 
2253 static void
2254 rt2560_enable_tsf(struct rt2560_softc *sc)
2255 {
2256 	RAL_WRITE(sc, RT2560_CSR14, 0);
2257 	RAL_WRITE(sc, RT2560_CSR14,
2258 	    RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF);
2259 }
2260 
2261 static void
2262 rt2560_update_plcp(struct rt2560_softc *sc)
2263 {
2264 	struct ifnet *ifp = sc->sc_ifp;
2265 	struct ieee80211com *ic = ifp->if_l2com;
2266 
2267 	/* no short preamble for 1Mbps */
2268 	RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2269 
2270 	if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2271 		/* values taken from the reference driver */
2272 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
2273 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2274 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
2275 	} else {
2276 		/* same values as above or'ed 0x8 */
2277 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
2278 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2279 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
2280 	}
2281 
2282 	DPRINTF(sc, "updating PLCP for %s preamble\n",
2283 	    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long");
2284 }
2285 
2286 /*
2287  * This function can be called by ieee80211_set_shortslottime(). Refer to
2288  * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2289  */
2290 static void
2291 rt2560_update_slot(struct ifnet *ifp)
2292 {
2293 	struct rt2560_softc *sc = ifp->if_softc;
2294 	struct ieee80211com *ic = ifp->if_l2com;
2295 	uint8_t slottime;
2296 	uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2297 	uint32_t tmp;
2298 
2299 #ifndef FORCE_SLOTTIME
2300 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2301 #else
2302 	/*
2303 	 * Setting slot time according to "short slot time" capability
2304 	 * in beacon/probe_resp seems to cause problem to acknowledge
2305 	 * certain AP's data frames transimitted at CCK/DS rates: the
2306 	 * problematic AP keeps retransmitting data frames, probably
2307 	 * because MAC level acks are not received by hardware.
2308 	 * So we cheat a little bit here by claiming we are capable of
2309 	 * "short slot time" but setting hardware slot time to the normal
2310 	 * slot time.  ral(4) does not seem to have trouble to receive
2311 	 * frames transmitted using short slot time even if hardware
2312 	 * slot time is set to normal slot time.  If we didn't use this
2313 	 * trick, we would have to claim that short slot time is not
2314 	 * supported; this would give relative poor RX performance
2315 	 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2316 	 * slot time.
2317 	 */
2318 	slottime = 20;
2319 #endif
2320 
2321 	/* update the MAC slot boundaries */
2322 	tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2323 	tx_pifs = tx_sifs + slottime;
2324 	tx_difs = tx_sifs + 2 * slottime;
2325 	eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2326 
2327 	tmp = RAL_READ(sc, RT2560_CSR11);
2328 	tmp = (tmp & ~0x1f00) | slottime << 8;
2329 	RAL_WRITE(sc, RT2560_CSR11, tmp);
2330 
2331 	tmp = tx_pifs << 16 | tx_sifs;
2332 	RAL_WRITE(sc, RT2560_CSR18, tmp);
2333 
2334 	tmp = eifs << 16 | tx_difs;
2335 	RAL_WRITE(sc, RT2560_CSR19, tmp);
2336 
2337 	DPRINTF(sc, "setting slottime to %uus\n", slottime);
2338 }
2339 
2340 static void
2341 rt2560_set_basicrates(struct rt2560_softc *sc)
2342 {
2343 	struct ifnet *ifp = sc->sc_ifp;
2344 	struct ieee80211com *ic = ifp->if_l2com;
2345 
2346 	/* update basic rate set */
2347 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
2348 		/* 11b basic rates: 1, 2Mbps */
2349 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2350 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2351 		/* 11a basic rates: 6, 12, 24Mbps */
2352 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2353 	} else {
2354 		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2355 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2356 	}
2357 }
2358 
2359 static void
2360 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2361 {
2362 	uint32_t tmp;
2363 
2364 	/* set ON period to 70ms and OFF period to 30ms */
2365 	tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2366 	RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2367 }
2368 
2369 static void
2370 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2371 {
2372 	uint32_t tmp;
2373 
2374 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2375 	RAL_WRITE(sc, RT2560_CSR5, tmp);
2376 
2377 	tmp = bssid[4] | bssid[5] << 8;
2378 	RAL_WRITE(sc, RT2560_CSR6, tmp);
2379 
2380 	DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2381 }
2382 
2383 static void
2384 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2385 {
2386 	uint32_t tmp;
2387 
2388 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2389 	RAL_WRITE(sc, RT2560_CSR3, tmp);
2390 
2391 	tmp = addr[4] | addr[5] << 8;
2392 	RAL_WRITE(sc, RT2560_CSR4, tmp);
2393 
2394 	DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2395 }
2396 
2397 static void
2398 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2399 {
2400 	uint32_t tmp;
2401 
2402 	tmp = RAL_READ(sc, RT2560_CSR3);
2403 	addr[0] = tmp & 0xff;
2404 	addr[1] = (tmp >>  8) & 0xff;
2405 	addr[2] = (tmp >> 16) & 0xff;
2406 	addr[3] = (tmp >> 24);
2407 
2408 	tmp = RAL_READ(sc, RT2560_CSR4);
2409 	addr[4] = tmp & 0xff;
2410 	addr[5] = (tmp >> 8) & 0xff;
2411 }
2412 
2413 static void
2414 rt2560_update_promisc(struct ifnet *ifp)
2415 {
2416 	struct rt2560_softc *sc = ifp->if_softc;
2417 	uint32_t tmp;
2418 
2419 	tmp = RAL_READ(sc, RT2560_RXCSR0);
2420 
2421 	tmp &= ~RT2560_DROP_NOT_TO_ME;
2422 	if (!(ifp->if_flags & IFF_PROMISC))
2423 		tmp |= RT2560_DROP_NOT_TO_ME;
2424 
2425 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2426 
2427 	DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2428 	    "entering" : "leaving");
2429 }
2430 
2431 static const char *
2432 rt2560_get_rf(int rev)
2433 {
2434 	switch (rev) {
2435 	case RT2560_RF_2522:	return "RT2522";
2436 	case RT2560_RF_2523:	return "RT2523";
2437 	case RT2560_RF_2524:	return "RT2524";
2438 	case RT2560_RF_2525:	return "RT2525";
2439 	case RT2560_RF_2525E:	return "RT2525e";
2440 	case RT2560_RF_2526:	return "RT2526";
2441 	case RT2560_RF_5222:	return "RT5222";
2442 	default:		return "unknown";
2443 	}
2444 }
2445 
2446 static void
2447 rt2560_read_config(struct rt2560_softc *sc)
2448 {
2449 	uint16_t val;
2450 	int i;
2451 
2452 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2453 	sc->rf_rev =   (val >> 11) & 0x7;
2454 	sc->hw_radio = (val >> 10) & 0x1;
2455 	sc->led_mode = (val >> 6)  & 0x7;
2456 	sc->rx_ant =   (val >> 4)  & 0x3;
2457 	sc->tx_ant =   (val >> 2)  & 0x3;
2458 	sc->nb_ant =   val & 0x3;
2459 
2460 	/* read default values for BBP registers */
2461 	for (i = 0; i < 16; i++) {
2462 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2463 		if (val == 0 || val == 0xffff)
2464 			continue;
2465 
2466 		sc->bbp_prom[i].reg = val >> 8;
2467 		sc->bbp_prom[i].val = val & 0xff;
2468 	}
2469 
2470 	/* read Tx power for all b/g channels */
2471 	for (i = 0; i < 14 / 2; i++) {
2472 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2473 		sc->txpow[i * 2] = val & 0xff;
2474 		sc->txpow[i * 2 + 1] = val >> 8;
2475 	}
2476 	for (i = 0; i < 14; ++i) {
2477 		if (sc->txpow[i] > 31)
2478 			sc->txpow[i] = 24;
2479 	}
2480 
2481 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2482 	if ((val & 0xff) == 0xff)
2483 		sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2484 	else
2485 		sc->rssi_corr = val & 0xff;
2486 	DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2487 		 sc->rssi_corr, val);
2488 }
2489 
2490 
2491 static void
2492 rt2560_scan_start(struct ieee80211com *ic)
2493 {
2494 	struct ifnet *ifp = ic->ic_ifp;
2495 	struct rt2560_softc *sc = ifp->if_softc;
2496 
2497 	/* abort TSF synchronization */
2498 	RAL_WRITE(sc, RT2560_CSR14, 0);
2499 	rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2500 }
2501 
2502 static void
2503 rt2560_scan_end(struct ieee80211com *ic)
2504 {
2505 	struct ifnet *ifp = ic->ic_ifp;
2506 	struct rt2560_softc *sc = ifp->if_softc;
2507 	struct ieee80211vap *vap = ic->ic_scan->ss_vap;
2508 
2509 	rt2560_enable_tsf_sync(sc);
2510 	/* XXX keep local copy */
2511 	rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2512 }
2513 
2514 static int
2515 rt2560_bbp_init(struct rt2560_softc *sc)
2516 {
2517 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2518 	int i, ntries;
2519 
2520 	/* wait for BBP to be ready */
2521 	for (ntries = 0; ntries < 100; ntries++) {
2522 		if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2523 			break;
2524 		DELAY(1);
2525 	}
2526 	if (ntries == 100) {
2527 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2528 		return EIO;
2529 	}
2530 
2531 	/* initialize BBP registers to default values */
2532 	for (i = 0; i < N(rt2560_def_bbp); i++) {
2533 		rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2534 		    rt2560_def_bbp[i].val);
2535 	}
2536 
2537 	/* initialize BBP registers to values stored in EEPROM */
2538 	for (i = 0; i < 16; i++) {
2539 		if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2540 			break;
2541 		rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2542 	}
2543 	rt2560_bbp_write(sc, 17, 0x48);	/* XXX restore bbp17 */
2544 
2545 	return 0;
2546 #undef N
2547 }
2548 
2549 static void
2550 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2551 {
2552 	uint32_t tmp;
2553 	uint8_t tx;
2554 
2555 	tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2556 	if (antenna == 1)
2557 		tx |= RT2560_BBP_ANTA;
2558 	else if (antenna == 2)
2559 		tx |= RT2560_BBP_ANTB;
2560 	else
2561 		tx |= RT2560_BBP_DIVERSITY;
2562 
2563 	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2564 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2565 	    sc->rf_rev == RT2560_RF_5222)
2566 		tx |= RT2560_BBP_FLIPIQ;
2567 
2568 	rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2569 
2570 	/* update values for CCK and OFDM in BBPCSR1 */
2571 	tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2572 	tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2573 	RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2574 }
2575 
2576 static void
2577 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2578 {
2579 	uint8_t rx;
2580 
2581 	rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2582 	if (antenna == 1)
2583 		rx |= RT2560_BBP_ANTA;
2584 	else if (antenna == 2)
2585 		rx |= RT2560_BBP_ANTB;
2586 	else
2587 		rx |= RT2560_BBP_DIVERSITY;
2588 
2589 	/* need to force no I/Q flip for RF 2525e and 2526 */
2590 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2591 		rx &= ~RT2560_BBP_FLIPIQ;
2592 
2593 	rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2594 }
2595 
2596 static void
2597 rt2560_init_locked(struct rt2560_softc *sc)
2598 {
2599 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2600 	struct ifnet *ifp = sc->sc_ifp;
2601 	struct ieee80211com *ic = ifp->if_l2com;
2602 	uint32_t tmp;
2603 	int i;
2604 
2605 	rt2560_stop_locked(sc);
2606 
2607 	/* setup tx rings */
2608 	tmp = RT2560_PRIO_RING_COUNT << 24 |
2609 	      RT2560_ATIM_RING_COUNT << 16 |
2610 	      RT2560_TX_RING_COUNT   <<  8 |
2611 	      RT2560_TX_DESC_SIZE;
2612 
2613 	/* rings must be initialized in this exact order */
2614 	RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2615 	RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2616 	RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2617 	RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2618 	RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2619 
2620 	/* setup rx ring */
2621 	tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2622 
2623 	RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2624 	RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2625 
2626 	/* initialize MAC registers to default values */
2627 	for (i = 0; i < N(rt2560_def_mac); i++)
2628 		RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2629 
2630 	rt2560_set_macaddr(sc, IF_LLADDR(ifp));
2631 
2632 	/* set basic rate set (will be updated later) */
2633 	RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2634 
2635 	rt2560_update_slot(ifp);
2636 	rt2560_update_plcp(sc);
2637 	rt2560_update_led(sc, 0, 0);
2638 
2639 	RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2640 	RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2641 
2642 	if (rt2560_bbp_init(sc) != 0) {
2643 		rt2560_stop(sc);
2644 		return;
2645 	}
2646 
2647 	rt2560_set_txantenna(sc, sc->tx_ant);
2648 	rt2560_set_rxantenna(sc, sc->rx_ant);
2649 
2650 	/* set default BSS channel */
2651 	rt2560_set_chan(sc, ic->ic_curchan);
2652 
2653 	/* kick Rx */
2654 	tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2655 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2656 		tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2657 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
2658 		    ic->ic_opmode != IEEE80211_M_MBSS)
2659 			tmp |= RT2560_DROP_TODS;
2660 		if (!(ifp->if_flags & IFF_PROMISC))
2661 			tmp |= RT2560_DROP_NOT_TO_ME;
2662 	}
2663 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2664 
2665 	/* clear old FCS and Rx FIFO errors */
2666 	RAL_READ(sc, RT2560_CNT0);
2667 	RAL_READ(sc, RT2560_CNT4);
2668 
2669 	/* clear any pending interrupts */
2670 	RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2671 
2672 	/* enable interrupts */
2673 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2674 
2675 	ifp->if_flags &= ~IFF_OACTIVE;
2676 	ifp->if_flags |= IFF_RUNNING;
2677 
2678 	callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc);
2679 #undef N
2680 }
2681 
2682 static void
2683 rt2560_init(void *priv)
2684 {
2685 	struct rt2560_softc *sc = priv;
2686 	struct ifnet *ifp = sc->sc_ifp;
2687 	struct ieee80211com *ic = ifp->if_l2com;
2688 
2689 	rt2560_init_locked(sc);
2690 
2691 	if (ifp->if_flags & IFF_RUNNING)
2692 		ieee80211_start_all(ic);		/* start all vap's */
2693 }
2694 
2695 static void
2696 rt2560_stop_locked(struct rt2560_softc *sc)
2697 {
2698 	struct ifnet *ifp = sc->sc_ifp;
2699 	volatile int *flags = &sc->sc_flags;
2700 
2701 	while (*flags & RT2560_F_INPUT_RUNNING)
2702 		zsleep(sc, &wlan_global_serializer, 0, "ralrunning", hz/10);
2703 
2704 	callout_stop(&sc->watchdog_ch);
2705 	sc->sc_tx_timer = 0;
2706 
2707 	if (ifp->if_flags & IFF_RUNNING) {
2708 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2709 
2710 		/* abort Tx */
2711 		RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2712 
2713 		/* disable Rx */
2714 		RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2715 
2716 		/* reset ASIC (imply reset BBP) */
2717 		RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2718 		RAL_WRITE(sc, RT2560_CSR1, 0);
2719 
2720 		/* disable interrupts */
2721 		RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2722 
2723 		/* reset Tx and Rx rings */
2724 		rt2560_reset_tx_ring(sc, &sc->txq);
2725 		rt2560_reset_tx_ring(sc, &sc->atimq);
2726 		rt2560_reset_tx_ring(sc, &sc->prioq);
2727 		rt2560_reset_tx_ring(sc, &sc->bcnq);
2728 		rt2560_reset_rx_ring(sc, &sc->rxq);
2729 	}
2730 	sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2731 }
2732 
2733 void
2734 rt2560_stop(void *arg)
2735 {
2736 	struct rt2560_softc *sc = arg;
2737 
2738 	rt2560_stop_locked(sc);
2739 }
2740 
2741 static int
2742 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2743 	const struct ieee80211_bpf_params *params)
2744 {
2745 	struct ieee80211com *ic = ni->ni_ic;
2746 	struct ifnet *ifp = ic->ic_ifp;
2747 	struct rt2560_softc *sc = ifp->if_softc;
2748 
2749 	/* prevent management frames from being sent if we're not ready */
2750 	if (!(ifp->if_flags & IFF_RUNNING)) {
2751 		m_freem(m);
2752 		ieee80211_free_node(ni);
2753 		return ENETDOWN;
2754 	}
2755 	if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2756 		ifp->if_flags |= IFF_OACTIVE;
2757 		sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2758 		m_freem(m);
2759 		ieee80211_free_node(ni);
2760 		return ENOBUFS;		/* XXX */
2761 	}
2762 
2763 	ifp->if_opackets++;
2764 
2765 	if (params == NULL) {
2766 		/*
2767 		 * Legacy path; interpret frame contents to decide
2768 		 * precisely how to send the frame.
2769 		 */
2770 		if (rt2560_tx_mgt(sc, m, ni) != 0)
2771 			goto bad;
2772 	} else {
2773 		/*
2774 		 * Caller supplied explicit parameters to use in
2775 		 * sending the frame.
2776 		 */
2777 		if (rt2560_tx_raw(sc, m, ni, params))
2778 			goto bad;
2779 	}
2780 	sc->sc_tx_timer = 5;
2781 
2782 	return 0;
2783 bad:
2784 	ifp->if_oerrors++;
2785 	ieee80211_free_node(ni);
2786 	return EIO;		/* XXX */
2787 }
2788