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