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