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