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