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