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