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