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