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