rt2560.c revision 1.17 1 /* $NetBSD: rt2560.c,v 1.17 2008/01/09 20:21:23 degroote 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.17 2008/01/09 20:21:23 degroote 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 /*
1458 * This function is called periodically in IBSS mode when a new beacon must be
1459 * sent out.
1460 */
1461 static void
1462 rt2560_beacon_expire(struct rt2560_softc *sc)
1463 {
1464 struct ieee80211com *ic = &sc->sc_ic;
1465 struct rt2560_tx_data *data;
1466
1467 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1468 ic->ic_opmode != IEEE80211_M_HOSTAP)
1469 return;
1470
1471 data = &sc->bcnq.data[sc->bcnq.next];
1472
1473 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1474 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1475 bus_dmamap_unload(sc->sc_dmat, data->map);
1476
1477 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1478
1479 #if NBPFILTER > 0
1480 if (ic->ic_rawbpf != NULL)
1481 bpf_mtap(ic->ic_rawbpf, data->m);
1482 #endif
1483 rt2560_tx_bcn(sc, data->m, data->ni);
1484
1485 DPRINTFN(15, ("beacon expired\n"));
1486
1487 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1488 }
1489
1490 static void
1491 rt2560_wakeup_expire(struct rt2560_softc *sc)
1492 {
1493 DPRINTFN(15, ("wakeup expired\n"));
1494 }
1495
1496 int
1497 rt2560_intr(void *arg)
1498 {
1499 struct rt2560_softc *sc = arg;
1500 struct ifnet *ifp = &sc->sc_if;
1501 uint32_t r;
1502
1503 if (!device_is_active(&sc->sc_dev))
1504 return 0;
1505
1506 /* disable interrupts */
1507 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1508
1509 /* don't re-enable interrupts if we're shutting down */
1510 if (!(ifp->if_flags & IFF_RUNNING))
1511 return 0;
1512
1513 r = RAL_READ(sc, RT2560_CSR7);
1514 RAL_WRITE(sc, RT2560_CSR7, r);
1515
1516 if (r & RT2560_BEACON_EXPIRE)
1517 rt2560_beacon_expire(sc);
1518
1519 if (r & RT2560_WAKEUP_EXPIRE)
1520 rt2560_wakeup_expire(sc);
1521
1522 if (r & RT2560_ENCRYPTION_DONE)
1523 rt2560_encryption_intr(sc);
1524
1525 if (r & RT2560_TX_DONE)
1526 rt2560_tx_intr(sc);
1527
1528 if (r & RT2560_PRIO_DONE)
1529 rt2560_prio_intr(sc);
1530
1531 if (r & RT2560_DECRYPTION_DONE)
1532 rt2560_decryption_intr(sc);
1533
1534 if (r & RT2560_RX_DONE)
1535 rt2560_rx_intr(sc);
1536
1537 /* re-enable interrupts */
1538 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1539
1540 return 1;
1541 }
1542
1543 /* quickly determine if a given rate is CCK or OFDM */
1544 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1545
1546 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1547 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1548
1549 #define RAL_SIFS 10 /* us */
1550
1551 #define RT2560_RXTX_TURNAROUND 10 /* us */
1552
1553 /*
1554 * This function is only used by the Rx radiotap code. It returns the rate at
1555 * which a given frame was received.
1556 */
1557 #if NBPFILTER > 0
1558 static uint8_t
1559 rt2560_rxrate(struct rt2560_rx_desc *desc)
1560 {
1561 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1562 /* reverse function of rt2560_plcp_signal */
1563 switch (desc->rate) {
1564 case 0xb: return 12;
1565 case 0xf: return 18;
1566 case 0xa: return 24;
1567 case 0xe: return 36;
1568 case 0x9: return 48;
1569 case 0xd: return 72;
1570 case 0x8: return 96;
1571 case 0xc: return 108;
1572 }
1573 } else {
1574 if (desc->rate == 10)
1575 return 2;
1576 if (desc->rate == 20)
1577 return 4;
1578 if (desc->rate == 55)
1579 return 11;
1580 if (desc->rate == 110)
1581 return 22;
1582 }
1583 return 2; /* should not get there */
1584 }
1585 #endif
1586
1587 /*
1588 * Return the expected ack rate for a frame transmitted at rate `rate'.
1589 * XXX: this should depend on the destination node basic rate set.
1590 */
1591 static int
1592 rt2560_ack_rate(struct ieee80211com *ic, int rate)
1593 {
1594 switch (rate) {
1595 /* CCK rates */
1596 case 2:
1597 return 2;
1598 case 4:
1599 case 11:
1600 case 22:
1601 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1602
1603 /* OFDM rates */
1604 case 12:
1605 case 18:
1606 return 12;
1607 case 24:
1608 case 36:
1609 return 24;
1610 case 48:
1611 case 72:
1612 case 96:
1613 case 108:
1614 return 48;
1615 }
1616
1617 /* default to 1Mbps */
1618 return 2;
1619 }
1620
1621 /*
1622 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1623 * The function automatically determines the operating mode depending on the
1624 * given rate. `flags' indicates whether short preamble is in use or not.
1625 */
1626 static uint16_t
1627 rt2560_txtime(int len, int rate, uint32_t flags)
1628 {
1629 uint16_t txtime;
1630
1631 if (RAL_RATE_IS_OFDM(rate)) {
1632 /* IEEE Std 802.11a-1999, pp. 37 */
1633 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1634 txtime = 16 + 4 + 4 * txtime + 6;
1635 } else {
1636 /* IEEE Std 802.11b-1999, pp. 28 */
1637 txtime = (16 * len + rate - 1) / rate;
1638 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1639 txtime += 72 + 24;
1640 else
1641 txtime += 144 + 48;
1642 }
1643 return txtime;
1644 }
1645
1646 static uint8_t
1647 rt2560_plcp_signal(int rate)
1648 {
1649 switch (rate) {
1650 /* CCK rates (returned values are device-dependent) */
1651 case 2: return 0x0;
1652 case 4: return 0x1;
1653 case 11: return 0x2;
1654 case 22: return 0x3;
1655
1656 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1657 case 12: return 0xb;
1658 case 18: return 0xf;
1659 case 24: return 0xa;
1660 case 36: return 0xe;
1661 case 48: return 0x9;
1662 case 72: return 0xd;
1663 case 96: return 0x8;
1664 case 108: return 0xc;
1665
1666 /* unsupported rates (should not get there) */
1667 default: return 0xff;
1668 }
1669 }
1670
1671 static void
1672 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1673 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1674 {
1675 struct ieee80211com *ic = &sc->sc_ic;
1676 uint16_t plcp_length;
1677 int remainder;
1678
1679 desc->flags = htole32(flags);
1680 desc->flags |= htole32(len << 16);
1681 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) :
1682 htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1683
1684 desc->physaddr = htole32(physaddr);
1685 desc->wme = htole16(
1686 RT2560_AIFSN(2) |
1687 RT2560_LOGCWMIN(3) |
1688 RT2560_LOGCWMAX(8));
1689
1690 /* setup PLCP fields */
1691 desc->plcp_signal = rt2560_plcp_signal(rate);
1692 desc->plcp_service = 4;
1693
1694 len += IEEE80211_CRC_LEN;
1695 if (RAL_RATE_IS_OFDM(rate)) {
1696 desc->flags |= htole32(RT2560_TX_OFDM);
1697
1698 plcp_length = len & 0xfff;
1699 desc->plcp_length_hi = plcp_length >> 6;
1700 desc->plcp_length_lo = plcp_length & 0x3f;
1701 } else {
1702 plcp_length = (16 * len + rate - 1) / rate;
1703 if (rate == 22) {
1704 remainder = (16 * len) % 22;
1705 if (remainder != 0 && remainder < 7)
1706 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1707 }
1708 desc->plcp_length_hi = plcp_length >> 8;
1709 desc->plcp_length_lo = plcp_length & 0xff;
1710
1711 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1712 desc->plcp_signal |= 0x08;
1713 }
1714 }
1715
1716 static int
1717 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1718 struct ieee80211_node *ni)
1719 {
1720 struct rt2560_tx_desc *desc;
1721 struct rt2560_tx_data *data;
1722 int rate, error;
1723
1724 desc = &sc->bcnq.desc[sc->bcnq.cur];
1725 data = &sc->bcnq.data[sc->bcnq.cur];
1726
1727 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1728
1729 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1730 BUS_DMA_NOWAIT);
1731 if (error != 0) {
1732 printf("%s: could not map mbuf (error %d)\n",
1733 sc->sc_dev.dv_xname, error);
1734 m_freem(m0);
1735 return error;
1736 }
1737
1738 data->m = m0;
1739 data->ni = ni;
1740
1741 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1742 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0,
1743 data->map->dm_segs->ds_addr);
1744
1745 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1746 BUS_DMASYNC_PREWRITE);
1747 bus_dmamap_sync(sc->sc_dmat, sc->bcnq.map,
1748 sc->bcnq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1749 BUS_DMASYNC_PREWRITE);
1750
1751 return 0;
1752 }
1753
1754 static int
1755 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1756 struct ieee80211_node *ni)
1757 {
1758 struct ieee80211com *ic = &sc->sc_ic;
1759 struct rt2560_tx_desc *desc;
1760 struct rt2560_tx_data *data;
1761 struct ieee80211_frame *wh;
1762 struct ieee80211_key *k;
1763 uint16_t dur;
1764 uint32_t flags = 0;
1765 int rate, error;
1766
1767 desc = &sc->prioq.desc[sc->prioq.cur];
1768 data = &sc->prioq.data[sc->prioq.cur];
1769
1770 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1771
1772 wh = mtod(m0, struct ieee80211_frame *);
1773
1774 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1775 k = ieee80211_crypto_encap(ic, ni, m0);
1776 if (k == NULL) {
1777 m_freem(m0);
1778 return ENOBUFS;
1779 }
1780 }
1781
1782 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1783 BUS_DMA_NOWAIT);
1784 if (error != 0) {
1785 printf("%s: could not map mbuf (error %d)\n",
1786 sc->sc_dev.dv_xname, error);
1787 m_freem(m0);
1788 return error;
1789 }
1790
1791 #if NBPFILTER > 0
1792 if (sc->sc_drvbpf != NULL) {
1793 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1794
1795 tap->wt_flags = 0;
1796 tap->wt_rate = rate;
1797 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1798 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1799 tap->wt_antenna = sc->tx_ant;
1800
1801 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1802 }
1803 #endif
1804
1805 data->m = m0;
1806 data->ni = ni;
1807
1808 wh = mtod(m0, struct ieee80211_frame *);
1809
1810 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1811 flags |= RT2560_TX_ACK;
1812
1813 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1814 RAL_SIFS;
1815 *(uint16_t *)wh->i_dur = htole16(dur);
1816
1817 /* tell hardware to add timestamp for probe responses */
1818 if ((wh->i_fc[0] &
1819 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1820 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1821 flags |= RT2560_TX_TIMESTAMP;
1822 }
1823
1824 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1825 data->map->dm_segs->ds_addr);
1826
1827 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1828 BUS_DMASYNC_PREWRITE);
1829 bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
1830 sc->prioq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1831 BUS_DMASYNC_PREWRITE);
1832
1833 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1834 m0->m_pkthdr.len, sc->prioq.cur, rate));
1835
1836 /* kick prio */
1837 sc->prioq.queued++;
1838 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1839 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1840
1841 return 0;
1842 }
1843
1844 /*
1845 * Build a RTS control frame.
1846 */
1847 static struct mbuf *
1848 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1849 uint16_t dur)
1850 {
1851 struct ieee80211_frame_rts *rts;
1852 struct mbuf *m;
1853
1854 MGETHDR(m, M_DONTWAIT, MT_DATA);
1855 if (m == NULL) {
1856 sc->sc_ic.ic_stats.is_tx_nobuf++;
1857 printf("%s: could not allocate RTS frame\n",
1858 sc->sc_dev.dv_xname);
1859 return NULL;
1860 }
1861
1862 rts = mtod(m, struct ieee80211_frame_rts *);
1863
1864 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1865 IEEE80211_FC0_SUBTYPE_RTS;
1866 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1867 *(uint16_t *)rts->i_dur = htole16(dur);
1868 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1869 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1870
1871 m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts);
1872
1873 return m;
1874 }
1875
1876 static int
1877 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1878 struct ieee80211_node *ni)
1879 {
1880 struct ieee80211com *ic = &sc->sc_ic;
1881 struct rt2560_tx_desc *desc;
1882 struct rt2560_tx_data *data;
1883 struct rt2560_node *rn;
1884 struct ieee80211_rateset *rs;
1885 struct ieee80211_frame *wh;
1886 struct ieee80211_key *k;
1887 struct mbuf *mnew;
1888 uint16_t dur;
1889 uint32_t flags = 0;
1890 int rate, error;
1891
1892 wh = mtod(m0, struct ieee80211_frame *);
1893
1894 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1895 rs = &ic->ic_sup_rates[ic->ic_curmode];
1896 rate = rs->rs_rates[ic->ic_fixed_rate];
1897 } else {
1898 rs = &ni->ni_rates;
1899 rn = (struct rt2560_node *)ni;
1900 ni->ni_txrate = ieee80211_rssadapt_choose(&rn->rssadapt, rs,
1901 wh, m0->m_pkthdr.len, -1, NULL, 0);
1902 rate = rs->rs_rates[ni->ni_txrate];
1903 }
1904 rate &= IEEE80211_RATE_VAL;
1905
1906 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1907 k = ieee80211_crypto_encap(ic, ni, m0);
1908 if (k == NULL) {
1909 m_freem(m0);
1910 return ENOBUFS;
1911 }
1912
1913 /* packet header may have moved, reset our local pointer */
1914 wh = mtod(m0, struct ieee80211_frame *);
1915 }
1916
1917 /*
1918 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1919 * for directed frames only when the length of the MPDU is greater
1920 * than the length threshold indicated by [...]" ic_rtsthreshold.
1921 */
1922 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1923 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1924 struct mbuf *m;
1925 int rtsrate, ackrate;
1926
1927 rtsrate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1928 ackrate = rt2560_ack_rate(ic, rate);
1929
1930 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1931 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1932 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1933 3 * RAL_SIFS;
1934
1935 m = rt2560_get_rts(sc, wh, dur);
1936
1937 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1938 data = &sc->txq.data[sc->txq.cur_encrypt];
1939
1940 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
1941 BUS_DMA_NOWAIT);
1942 if (error != 0) {
1943 printf("%s: could not map mbuf (error %d)\n",
1944 sc->sc_dev.dv_xname, error);
1945 m_freem(m);
1946 m_freem(m0);
1947 return error;
1948 }
1949
1950 /* avoid multiple free() of the same node for each fragment */
1951 ieee80211_ref_node(ni);
1952
1953 data->m = m;
1954 data->ni = ni;
1955
1956 /* RTS frames are not taken into account for rssadapt */
1957 data->id.id_node = NULL;
1958
1959 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1960 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1,
1961 data->map->dm_segs->ds_addr);
1962
1963 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1964 data->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1965 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1966 sc->txq.cur_encrypt * RT2560_TX_DESC_SIZE,
1967 RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1968
1969 sc->txq.queued++;
1970 sc->txq.cur_encrypt =
1971 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1972
1973 /*
1974 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1975 * asynchronous data frame shall be transmitted after the CTS
1976 * frame and a SIFS period.
1977 */
1978 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1979 }
1980
1981 data = &sc->txq.data[sc->txq.cur_encrypt];
1982 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1983
1984 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1985 BUS_DMA_NOWAIT);
1986 if (error != 0 && error != EFBIG) {
1987 printf("%s: could not map mbuf (error %d)\n",
1988 sc->sc_dev.dv_xname, error);
1989 m_freem(m0);
1990 return error;
1991 }
1992 if (error != 0) {
1993 /* too many fragments, linearize */
1994
1995 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1996 if (mnew == NULL) {
1997 m_freem(m0);
1998 return ENOMEM;
1999 }
2000
2001 M_COPY_PKTHDR(mnew, m0);
2002 if (m0->m_pkthdr.len > MHLEN) {
2003 MCLGET(mnew, M_DONTWAIT);
2004 if (!(mnew->m_flags & M_EXT)) {
2005 m_freem(m0);
2006 m_freem(mnew);
2007 return ENOMEM;
2008 }
2009 }
2010
2011 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
2012 m_freem(m0);
2013 mnew->m_len = mnew->m_pkthdr.len;
2014 m0 = mnew;
2015
2016 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2017 BUS_DMA_NOWAIT);
2018 if (error != 0) {
2019 printf("%s: could not map mbuf (error %d)\n",
2020 sc->sc_dev.dv_xname, error);
2021 m_freem(m0);
2022 return error;
2023 }
2024
2025 /* packet header have moved, reset our local pointer */
2026 wh = mtod(m0, struct ieee80211_frame *);
2027 }
2028
2029 #if NBPFILTER > 0
2030 if (sc->sc_drvbpf != NULL) {
2031 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
2032
2033 tap->wt_flags = 0;
2034 tap->wt_rate = rate;
2035 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
2036 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
2037 tap->wt_antenna = sc->tx_ant;
2038
2039 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2040 }
2041 #endif
2042
2043 data->m = m0;
2044 data->ni = ni;
2045
2046 /* remember link conditions for rate adaptation algorithm */
2047 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
2048 data->id.id_len = m0->m_pkthdr.len;
2049 data->id.id_rateidx = ni->ni_txrate;
2050 data->id.id_node = ni;
2051 data->id.id_rssi = ni->ni_rssi;
2052 } else
2053 data->id.id_node = NULL;
2054
2055 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2056 flags |= RT2560_TX_ACK;
2057
2058 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
2059 ic->ic_flags) + RAL_SIFS;
2060 *(uint16_t *)wh->i_dur = htole16(dur);
2061 }
2062
2063 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
2064 data->map->dm_segs->ds_addr);
2065
2066 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
2067 BUS_DMASYNC_PREWRITE);
2068 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
2069 sc->txq.cur_encrypt * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
2070 BUS_DMASYNC_PREWRITE);
2071
2072 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
2073 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
2074
2075 /* kick encrypt */
2076 sc->txq.queued++;
2077 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
2078 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
2079
2080 return 0;
2081 }
2082
2083 static void
2084 rt2560_start(struct ifnet *ifp)
2085 {
2086 struct rt2560_softc *sc = ifp->if_softc;
2087 struct ieee80211com *ic = &sc->sc_ic;
2088 struct mbuf *m0;
2089 struct ieee80211_node *ni;
2090 struct ether_header *eh;
2091
2092 /*
2093 * net80211 may still try to send management frames even if the
2094 * IFF_RUNNING flag is not set...
2095 */
2096 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2097 return;
2098
2099 for (;;) {
2100 IF_POLL(&ic->ic_mgtq, m0);
2101 if (m0 != NULL) {
2102 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2103 ifp->if_flags |= IFF_OACTIVE;
2104 break;
2105 }
2106 IF_DEQUEUE(&ic->ic_mgtq, m0);
2107 if (m0 == NULL)
2108 break;
2109
2110 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2111 m0->m_pkthdr.rcvif = NULL;
2112 #if NBPFILTER > 0
2113 if (ic->ic_rawbpf != NULL)
2114 bpf_mtap(ic->ic_rawbpf, m0);
2115 #endif
2116 if (rt2560_tx_mgt(sc, m0, ni) != 0)
2117 break;
2118
2119 } else {
2120 if (ic->ic_state != IEEE80211_S_RUN)
2121 break;
2122 IFQ_DEQUEUE(&ifp->if_snd, m0);
2123 if (m0 == NULL)
2124 break;
2125 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2126 ifp->if_flags |= IFF_OACTIVE;
2127 break;
2128 }
2129
2130 if (m0->m_len < sizeof (struct ether_header) &&
2131 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2132 continue;
2133
2134 eh = mtod(m0, struct ether_header *);
2135 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2136 if (ni == NULL) {
2137 m_freem(m0);
2138 continue;
2139 }
2140 #if NBPFILTER > 0
2141 if (ifp->if_bpf != NULL)
2142 bpf_mtap(ifp->if_bpf, m0);
2143 #endif
2144
2145 m0 = ieee80211_encap(ic, m0, ni);
2146 if (m0 == NULL) {
2147 ieee80211_free_node(ni);
2148 continue;
2149 }
2150
2151 #if NBPFILTER > 0
2152 if (ic->ic_rawbpf != NULL)
2153 bpf_mtap(ic->ic_rawbpf, m0);
2154
2155 #endif
2156 if (rt2560_tx_data(sc, m0, ni) != 0) {
2157 ieee80211_free_node(ni);
2158 ifp->if_oerrors++;
2159 break;
2160 }
2161 }
2162
2163 sc->sc_tx_timer = 5;
2164 ifp->if_timer = 1;
2165 }
2166 }
2167
2168 static void
2169 rt2560_watchdog(struct ifnet *ifp)
2170 {
2171 struct rt2560_softc *sc = ifp->if_softc;
2172
2173 ifp->if_timer = 0;
2174
2175 if (sc->sc_tx_timer > 0) {
2176 if (--sc->sc_tx_timer == 0) {
2177 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
2178 rt2560_init(ifp);
2179 ifp->if_oerrors++;
2180 return;
2181 }
2182 ifp->if_timer = 1;
2183 }
2184
2185 ieee80211_watchdog(&sc->sc_ic);
2186 }
2187
2188 /*
2189 * This function allows for fast channel switching in monitor mode (used by
2190 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2191 * generate a new beacon frame.
2192 */
2193 static int
2194 rt2560_reset(struct ifnet *ifp)
2195 {
2196 struct rt2560_softc *sc = ifp->if_softc;
2197 struct ieee80211com *ic = &sc->sc_ic;
2198
2199 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2200 return ENETRESET;
2201
2202 rt2560_set_chan(sc, ic->ic_curchan);
2203
2204 return 0;
2205 }
2206
2207 int
2208 rt2560_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2209 {
2210 struct rt2560_softc *sc = ifp->if_softc;
2211 struct ieee80211com *ic = &sc->sc_ic;
2212 int s, error = 0;
2213
2214 s = splnet();
2215
2216 switch (cmd) {
2217 case SIOCSIFFLAGS:
2218 if (ifp->if_flags & IFF_UP) {
2219 if (ifp->if_flags & IFF_RUNNING)
2220 rt2560_update_promisc(sc);
2221 else
2222 rt2560_init(ifp);
2223 } else {
2224 if (ifp->if_flags & IFF_RUNNING)
2225 rt2560_stop(ifp, 1);
2226 }
2227 break;
2228
2229 case SIOCADDMULTI:
2230 case SIOCDELMULTI:
2231 /* XXX no h/w multicast filter? --dyoung */
2232 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET)
2233 error = 0;
2234 break;
2235
2236 case SIOCS80211CHANNEL:
2237 /*
2238 * This allows for fast channel switching in monitor mode
2239 * (used by kismet). In IBSS mode, we must explicitly reset
2240 * the interface to generate a new beacon frame.
2241 */
2242 error = ieee80211_ioctl(ic, cmd, data);
2243 if (error == ENETRESET &&
2244 ic->ic_opmode == IEEE80211_M_MONITOR) {
2245 rt2560_set_chan(sc, ic->ic_ibss_chan);
2246 error = 0;
2247 }
2248 break;
2249
2250 default:
2251 error = ieee80211_ioctl(ic, cmd, data);
2252 }
2253
2254 if (error == ENETRESET) {
2255 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2256 (IFF_UP | IFF_RUNNING))
2257 rt2560_init(ifp);
2258 error = 0;
2259 }
2260
2261 splx(s);
2262
2263 return error;
2264 }
2265
2266 static void
2267 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2268 {
2269 uint32_t tmp;
2270 int ntries;
2271
2272 for (ntries = 0; ntries < 100; ntries++) {
2273 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2274 break;
2275 DELAY(1);
2276 }
2277 if (ntries == 100) {
2278 printf("%s: could not write to BBP\n", sc->sc_dev.dv_xname);
2279 return;
2280 }
2281
2282 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2283 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2284
2285 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2286 }
2287
2288 static uint8_t
2289 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2290 {
2291 uint32_t val;
2292 int ntries;
2293
2294 val = RT2560_BBP_BUSY | reg << 8;
2295 RAL_WRITE(sc, RT2560_BBPCSR, val);
2296
2297 for (ntries = 0; ntries < 100; ntries++) {
2298 val = RAL_READ(sc, RT2560_BBPCSR);
2299 if (!(val & RT2560_BBP_BUSY))
2300 return val & 0xff;
2301 DELAY(1);
2302 }
2303
2304 printf("%s: could not read from BBP\n", sc->sc_dev.dv_xname);
2305 return 0;
2306 }
2307
2308 static void
2309 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2310 {
2311 uint32_t tmp;
2312 int ntries;
2313
2314 for (ntries = 0; ntries < 100; ntries++) {
2315 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2316 break;
2317 DELAY(1);
2318 }
2319 if (ntries == 100) {
2320 printf("%s: could not write to RF\n", sc->sc_dev.dv_xname);
2321 return;
2322 }
2323
2324 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2325 (reg & 0x3);
2326 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2327
2328 /* remember last written value in sc */
2329 sc->rf_regs[reg] = val;
2330
2331 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2332 }
2333
2334 static void
2335 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2336 {
2337 struct ieee80211com *ic = &sc->sc_ic;
2338 uint8_t power, tmp;
2339 u_int i, chan;
2340
2341 chan = ieee80211_chan2ieee(ic, c);
2342 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2343 return;
2344
2345 if (IEEE80211_IS_CHAN_2GHZ(c))
2346 power = min(sc->txpow[chan - 1], 31);
2347 else
2348 power = 31;
2349
2350 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2351
2352 switch (sc->rf_rev) {
2353 case RT2560_RF_2522:
2354 rt2560_rf_write(sc, RT2560_RF1, 0x00814);
2355 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2522_r2[chan - 1]);
2356 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2357 break;
2358
2359 case RT2560_RF_2523:
2360 rt2560_rf_write(sc, RT2560_RF1, 0x08804);
2361 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2523_r2[chan - 1]);
2362 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x38044);
2363 rt2560_rf_write(sc, RT2560_RF4,
2364 (chan == 14) ? 0x00280 : 0x00286);
2365 break;
2366
2367 case RT2560_RF_2524:
2368 rt2560_rf_write(sc, RT2560_RF1, 0x0c808);
2369 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2524_r2[chan - 1]);
2370 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2371 rt2560_rf_write(sc, RT2560_RF4,
2372 (chan == 14) ? 0x00280 : 0x00286);
2373 break;
2374
2375 case RT2560_RF_2525:
2376 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2377 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2378 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2379 rt2560_rf_write(sc, RT2560_RF4,
2380 (chan == 14) ? 0x00280 : 0x00286);
2381
2382 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2383 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_r2[chan - 1]);
2384 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2385 rt2560_rf_write(sc, RT2560_RF4,
2386 (chan == 14) ? 0x00280 : 0x00286);
2387 break;
2388
2389 case RT2560_RF_2525E:
2390 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2391 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525e_r2[chan - 1]);
2392 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2393 rt2560_rf_write(sc, RT2560_RF4,
2394 (chan == 14) ? 0x00286 : 0x00282);
2395 break;
2396
2397 case RT2560_RF_2526:
2398 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2399 rt2560_rf_write(sc, RT2560_RF4,
2400 (chan & 1) ? 0x00386 : 0x00381);
2401 rt2560_rf_write(sc, RT2560_RF1, 0x08804);
2402
2403 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_r2[chan - 1]);
2404 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2405 rt2560_rf_write(sc, RT2560_RF4,
2406 (chan & 1) ? 0x00386 : 0x00381);
2407 break;
2408
2409 /* dual-band RF */
2410 case RT2560_RF_5222:
2411 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2412
2413 rt2560_rf_write(sc, RT2560_RF1, rt2560_rf5222[i].r1);
2414 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf5222[i].r2);
2415 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2416 rt2560_rf_write(sc, RT2560_RF4, rt2560_rf5222[i].r4);
2417 break;
2418 }
2419
2420 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
2421 ic->ic_state != IEEE80211_S_SCAN) {
2422 /* set Japan filter bit for channel 14 */
2423 tmp = rt2560_bbp_read(sc, 70);
2424
2425 tmp &= ~RT2560_JAPAN_FILTER;
2426 if (chan == 14)
2427 tmp |= RT2560_JAPAN_FILTER;
2428
2429 rt2560_bbp_write(sc, 70, tmp);
2430
2431 DELAY(1000); /* RF needs a 1ms delay here */
2432 rt2560_disable_rf_tune(sc);
2433
2434 /* clear CRC errors */
2435 RAL_READ(sc, RT2560_CNT0);
2436 }
2437 }
2438
2439 /*
2440 * Disable RF auto-tuning.
2441 */
2442 static void
2443 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2444 {
2445 uint32_t tmp;
2446
2447 if (sc->rf_rev != RT2560_RF_2523) {
2448 tmp = sc->rf_regs[RT2560_RF1] & ~RT2560_RF1_AUTOTUNE;
2449 rt2560_rf_write(sc, RT2560_RF1, tmp);
2450 }
2451
2452 tmp = sc->rf_regs[RT2560_RF3] & ~RT2560_RF3_AUTOTUNE;
2453 rt2560_rf_write(sc, RT2560_RF3, tmp);
2454
2455 DPRINTFN(2, ("disabling RF autotune\n"));
2456 }
2457
2458 /*
2459 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2460 * synchronization.
2461 */
2462 static void
2463 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2464 {
2465 struct ieee80211com *ic = &sc->sc_ic;
2466 uint16_t logcwmin, preload;
2467 uint32_t tmp;
2468
2469 /* first, disable TSF synchronization */
2470 RAL_WRITE(sc, RT2560_CSR14, 0);
2471
2472 tmp = 16 * ic->ic_bss->ni_intval;
2473 RAL_WRITE(sc, RT2560_CSR12, tmp);
2474
2475 RAL_WRITE(sc, RT2560_CSR13, 0);
2476
2477 logcwmin = 5;
2478 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2479 tmp = logcwmin << 16 | preload;
2480 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2481
2482 /* finally, enable TSF synchronization */
2483 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2484 if (ic->ic_opmode == IEEE80211_M_STA)
2485 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2486 else
2487 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2488 RT2560_ENABLE_BEACON_GENERATOR;
2489 RAL_WRITE(sc, RT2560_CSR14, tmp);
2490
2491 DPRINTF(("enabling TSF synchronization\n"));
2492 }
2493
2494 static void
2495 rt2560_update_plcp(struct rt2560_softc *sc)
2496 {
2497 struct ieee80211com *ic = &sc->sc_ic;
2498
2499 /* no short preamble for 1Mbps */
2500 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2501
2502 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2503 /* values taken from the reference driver */
2504 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2505 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2506 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2507 } else {
2508 /* same values as above or'ed 0x8 */
2509 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2510 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2511 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2512 }
2513
2514 DPRINTF(("updating PLCP for %s preamble\n",
2515 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2516 }
2517
2518 /*
2519 * IEEE 802.11a uses short slot time. Refer to IEEE Std 802.11-1999 pp. 85 to
2520 * know how these values are computed.
2521 */
2522 static void
2523 rt2560_update_slot(struct ifnet *ifp)
2524 {
2525 struct rt2560_softc *sc = ifp->if_softc;
2526 struct ieee80211com *ic = &sc->sc_ic;
2527 uint8_t slottime;
2528 uint16_t sifs, pifs, difs, eifs;
2529 uint32_t tmp;
2530
2531 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2532
2533 /* define the MAC slot boundaries */
2534 sifs = RAL_SIFS - RT2560_RXTX_TURNAROUND;
2535 pifs = sifs + slottime;
2536 difs = sifs + 2 * slottime;
2537 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2538
2539 tmp = RAL_READ(sc, RT2560_CSR11);
2540 tmp = (tmp & ~0x1f00) | slottime << 8;
2541 RAL_WRITE(sc, RT2560_CSR11, tmp);
2542
2543 tmp = pifs << 16 | sifs;
2544 RAL_WRITE(sc, RT2560_CSR18, tmp);
2545
2546 tmp = eifs << 16 | difs;
2547 RAL_WRITE(sc, RT2560_CSR19, tmp);
2548
2549 DPRINTF(("setting slottime to %uus\n", slottime));
2550 }
2551
2552 static void
2553 rt2560_set_basicrates(struct rt2560_softc *sc)
2554 {
2555 struct ieee80211com *ic = &sc->sc_ic;
2556
2557 /* update basic rate set */
2558 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2559 /* 11b basic rates: 1, 2Mbps */
2560 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2561 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
2562 /* 11a basic rates: 6, 12, 24Mbps */
2563 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2564 } else {
2565 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2566 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2567 }
2568 }
2569
2570 static void
2571 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2572 {
2573 uint32_t tmp;
2574
2575 /* set ON period to 70ms and OFF period to 30ms */
2576 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2577 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2578 }
2579
2580 static void
2581 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2582 {
2583 uint32_t tmp;
2584
2585 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2586 RAL_WRITE(sc, RT2560_CSR5, tmp);
2587
2588 tmp = bssid[4] | bssid[5] << 8;
2589 RAL_WRITE(sc, RT2560_CSR6, tmp);
2590
2591 DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
2592 }
2593
2594 static void
2595 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2596 {
2597 uint32_t tmp;
2598
2599 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2600 RAL_WRITE(sc, RT2560_CSR3, tmp);
2601
2602 tmp = addr[4] | addr[5] << 8;
2603 RAL_WRITE(sc, RT2560_CSR4, tmp);
2604
2605 DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
2606 }
2607
2608 static void
2609 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2610 {
2611 uint32_t tmp;
2612
2613 tmp = RAL_READ(sc, RT2560_CSR3);
2614 addr[0] = tmp & 0xff;
2615 addr[1] = (tmp >> 8) & 0xff;
2616 addr[2] = (tmp >> 16) & 0xff;
2617 addr[3] = (tmp >> 24);
2618
2619 tmp = RAL_READ(sc, RT2560_CSR4);
2620 addr[4] = tmp & 0xff;
2621 addr[5] = (tmp >> 8) & 0xff;
2622 }
2623
2624 static void
2625 rt2560_update_promisc(struct rt2560_softc *sc)
2626 {
2627 struct ifnet *ifp = &sc->sc_if;
2628 uint32_t tmp;
2629
2630 tmp = RAL_READ(sc, RT2560_RXCSR0);
2631
2632 tmp &= ~RT2560_DROP_NOT_TO_ME;
2633 if (!(ifp->if_flags & IFF_PROMISC))
2634 tmp |= RT2560_DROP_NOT_TO_ME;
2635
2636 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2637
2638 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2639 "entering" : "leaving"));
2640 }
2641
2642 static void
2643 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2644 {
2645 uint32_t tmp;
2646 uint8_t tx;
2647
2648 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2649 if (antenna == 1)
2650 tx |= RT2560_BBP_ANTA;
2651 else if (antenna == 2)
2652 tx |= RT2560_BBP_ANTB;
2653 else
2654 tx |= RT2560_BBP_DIVERSITY;
2655
2656 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2657 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2658 sc->rf_rev == RT2560_RF_5222)
2659 tx |= RT2560_BBP_FLIPIQ;
2660
2661 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2662
2663 /* update values for CCK and OFDM in BBPCSR1 */
2664 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2665 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2666 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2667 }
2668
2669 static void
2670 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2671 {
2672 uint8_t rx;
2673
2674 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2675 if (antenna == 1)
2676 rx |= RT2560_BBP_ANTA;
2677 else if (antenna == 2)
2678 rx |= RT2560_BBP_ANTB;
2679 else
2680 rx |= RT2560_BBP_DIVERSITY;
2681
2682 /* need to force no I/Q flip for RF 2525e and 2526 */
2683 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2684 rx &= ~RT2560_BBP_FLIPIQ;
2685
2686 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2687 }
2688
2689 static const char *
2690 rt2560_get_rf(int rev)
2691 {
2692 switch (rev) {
2693 case RT2560_RF_2522: return "RT2522";
2694 case RT2560_RF_2523: return "RT2523";
2695 case RT2560_RF_2524: return "RT2524";
2696 case RT2560_RF_2525: return "RT2525";
2697 case RT2560_RF_2525E: return "RT2525e";
2698 case RT2560_RF_2526: return "RT2526";
2699 case RT2560_RF_5222: return "RT5222";
2700 default: return "unknown";
2701 }
2702 }
2703
2704 static void
2705 rt2560_read_eeprom(struct rt2560_softc *sc)
2706 {
2707 uint16_t val;
2708 int i;
2709
2710 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2711 sc->rf_rev = (val >> 11) & 0x1f;
2712 sc->hw_radio = (val >> 10) & 0x1;
2713 sc->led_mode = (val >> 6) & 0x7;
2714 sc->rx_ant = (val >> 4) & 0x3;
2715 sc->tx_ant = (val >> 2) & 0x3;
2716 sc->nb_ant = val & 0x3;
2717
2718 /* read default values for BBP registers */
2719 for (i = 0; i < 16; i++) {
2720 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2721 sc->bbp_prom[i].reg = val >> 8;
2722 sc->bbp_prom[i].val = val & 0xff;
2723 }
2724
2725 /* read Tx power for all b/g channels */
2726 for (i = 0; i < 14 / 2; i++) {
2727 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2728 sc->txpow[i * 2] = val >> 8;
2729 sc->txpow[i * 2 + 1] = val & 0xff;
2730 }
2731 }
2732
2733 static int
2734 rt2560_bbp_init(struct rt2560_softc *sc)
2735 {
2736 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2737 int i, ntries;
2738
2739 /* wait for BBP to be ready */
2740 for (ntries = 0; ntries < 100; ntries++) {
2741 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2742 break;
2743 DELAY(1);
2744 }
2745 if (ntries == 100) {
2746 printf("%s: timeout waiting for BBP\n", sc->sc_dev.dv_xname);
2747 return EIO;
2748 }
2749
2750 /* initialize BBP registers to default values */
2751 for (i = 0; i < N(rt2560_def_bbp); i++) {
2752 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2753 rt2560_def_bbp[i].val);
2754 }
2755 #if 0
2756 /* initialize BBP registers to values stored in EEPROM */
2757 for (i = 0; i < 16; i++) {
2758 if (sc->bbp_prom[i].reg == 0xff)
2759 continue;
2760 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2761 }
2762 #endif
2763
2764 return 0;
2765 #undef N
2766 }
2767
2768 static int
2769 rt2560_init(struct ifnet *ifp)
2770 {
2771 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2772 struct rt2560_softc *sc = ifp->if_softc;
2773 struct ieee80211com *ic = &sc->sc_ic;
2774 uint32_t tmp;
2775 int i;
2776
2777 /* for CardBus, power on the socket */
2778 if (!(sc->sc_flags & RT2560_ENABLED)) {
2779 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
2780 printf("%s: could not enable device\n",
2781 sc->sc_dev.dv_xname);
2782 return EIO;
2783 }
2784 sc->sc_flags |= RT2560_ENABLED;
2785 }
2786
2787 rt2560_stop(ifp, 1);
2788
2789 /* setup tx rings */
2790 tmp = RT2560_PRIO_RING_COUNT << 24 |
2791 RT2560_ATIM_RING_COUNT << 16 |
2792 RT2560_TX_RING_COUNT << 8 |
2793 RT2560_TX_DESC_SIZE;
2794
2795 /* rings _must_ be initialized in this _exact_ order! */
2796 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2797 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2798 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2799 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2800 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2801
2802 /* setup rx ring */
2803 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2804
2805 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2806 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2807
2808 /* initialize MAC registers to default values */
2809 for (i = 0; i < N(rt2560_def_mac); i++)
2810 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2811
2812 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2813 rt2560_set_macaddr(sc, ic->ic_myaddr);
2814
2815 /* set basic rate set (will be updated later) */
2816 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2817
2818 rt2560_update_slot(ifp);
2819 rt2560_update_plcp(sc);
2820 rt2560_update_led(sc, 0, 0);
2821
2822 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2823 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2824
2825 if (rt2560_bbp_init(sc) != 0) {
2826 rt2560_stop(ifp, 1);
2827 return EIO;
2828 }
2829
2830 rt2560_set_txantenna(sc, 1);
2831 rt2560_set_rxantenna(sc, 1);
2832
2833 /* set default BSS channel */
2834 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2835 rt2560_set_chan(sc, ic->ic_bss->ni_chan);
2836
2837 /* kick Rx */
2838 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2839 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2840 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2841 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2842 tmp |= RT2560_DROP_TODS;
2843 if (!(ifp->if_flags & IFF_PROMISC))
2844 tmp |= RT2560_DROP_NOT_TO_ME;
2845 }
2846 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2847
2848 /* clear old FCS and Rx FIFO errors */
2849 RAL_READ(sc, RT2560_CNT0);
2850 RAL_READ(sc, RT2560_CNT4);
2851
2852 /* clear any pending interrupts */
2853 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2854
2855 /* enable interrupts */
2856 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2857
2858 ifp->if_flags &= ~IFF_OACTIVE;
2859 ifp->if_flags |= IFF_RUNNING;
2860
2861 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2862 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2863 else
2864 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2865
2866 return 0;
2867 #undef N
2868 }
2869
2870 static void
2871 rt2560_stop(struct ifnet *ifp, int disable)
2872 {
2873 struct rt2560_softc *sc = ifp->if_softc;
2874 struct ieee80211com *ic = &sc->sc_ic;
2875
2876 sc->sc_tx_timer = 0;
2877 ifp->if_timer = 0;
2878 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2879
2880 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2881
2882 /* abort Tx */
2883 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2884
2885 /* disable Rx */
2886 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2887
2888 /* reset ASIC (and thus, BBP) */
2889 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2890 RAL_WRITE(sc, RT2560_CSR1, 0);
2891
2892 /* disable interrupts */
2893 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2894
2895 /* clear any pending interrupt */
2896 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2897
2898 /* reset Tx and Rx rings */
2899 rt2560_reset_tx_ring(sc, &sc->txq);
2900 rt2560_reset_tx_ring(sc, &sc->atimq);
2901 rt2560_reset_tx_ring(sc, &sc->prioq);
2902 rt2560_reset_tx_ring(sc, &sc->bcnq);
2903 rt2560_reset_rx_ring(sc, &sc->rxq);
2904
2905 }
2906