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