]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ral/rt2560.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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         bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1641         uint16_t dur;
1642         uint32_t flags = 0;
1643         int nsegs, rate, error;
1644
1645         desc = &sc->prioq.desc[sc->prioq.cur];
1646         data = &sc->prioq.data[sc->prioq.cur];
1647
1648         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1649
1650         error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1651             segs, &nsegs, 0);
1652         if (error != 0) {
1653                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1654                     error);
1655                 m_freem(m0);
1656                 return error;
1657         }
1658
1659         if (bpf_peers_present(sc->sc_drvbpf)) {
1660                 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1661
1662                 tap->wt_flags = 0;
1663                 tap->wt_rate = rate;
1664                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1665                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1666                 tap->wt_antenna = sc->tx_ant;
1667
1668                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1669         }
1670
1671         data->m = m0;
1672         data->ni = ni;
1673
1674         wh = mtod(m0, struct ieee80211_frame *);
1675
1676         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1677                 flags |= RT2560_TX_ACK;
1678
1679                 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1680                       RAL_SIFS;
1681                 *(uint16_t *)wh->i_dur = htole16(dur);
1682
1683                 /* tell hardware to add timestamp for probe responses */
1684                 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1685                     IEEE80211_FC0_TYPE_MGT &&
1686                     (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1687                     IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1688                         flags |= RT2560_TX_TIMESTAMP;
1689         }
1690
1691         rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1692             segs->ds_addr);
1693
1694         bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1695         bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1696             BUS_DMASYNC_PREWRITE);
1697
1698         DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1699             m0->m_pkthdr.len, sc->prioq.cur, rate));
1700
1701         /* kick prio */
1702         sc->prioq.queued++;
1703         sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1704         RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1705
1706         return 0;
1707 }
1708
1709 static int
1710 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1711     struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1712 {
1713         struct ieee80211com *ic = &sc->sc_ic;
1714         struct rt2560_tx_desc *desc;
1715         struct rt2560_tx_data *data;
1716         bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1717         uint32_t flags;
1718         int nsegs, rate, error;
1719
1720         desc = &sc->prioq.desc[sc->prioq.cur];
1721         data = &sc->prioq.data[sc->prioq.cur];
1722
1723         rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1724         /* XXX validate */
1725         if (rate == 0) {
1726                 m_freem(m0);
1727                 return EINVAL;
1728         }
1729
1730         error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1731             segs, &nsegs, 0);
1732         if (error != 0) {
1733                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1734                     error);
1735                 m_freem(m0);
1736                 return error;
1737         }
1738
1739         if (bpf_peers_present(sc->sc_drvbpf)) {
1740                 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1741
1742                 tap->wt_flags = 0;
1743                 tap->wt_rate = rate;
1744                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1745                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1746                 tap->wt_antenna = sc->tx_ant;
1747
1748                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1749         }
1750
1751         data->m = m0;
1752         data->ni = ni;
1753
1754         flags = 0;
1755         if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1756                 flags |= RT2560_TX_ACK;
1757
1758         /* XXX need to setup descriptor ourself */
1759         rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1760             rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1761             segs->ds_addr);
1762
1763         bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1764         bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1765             BUS_DMASYNC_PREWRITE);
1766
1767         DPRINTFN(10, ("sending raw frame len=%u idx=%u rate=%u\n",
1768             m0->m_pkthdr.len, sc->prioq.cur, rate));
1769
1770         /* kick prio */
1771         sc->prioq.queued++;
1772         sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1773         RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1774
1775         return 0;
1776 }
1777
1778 /*
1779  * Build a RTS control frame.
1780  */
1781 static struct mbuf *
1782 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1783     uint16_t dur)
1784 {
1785         struct ieee80211_frame_rts *rts;
1786         struct mbuf *m;
1787
1788         MGETHDR(m, M_DONTWAIT, MT_DATA);
1789         if (m == NULL) {
1790                 sc->sc_ic.ic_stats.is_tx_nobuf++;
1791                 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1792                 return NULL;
1793         }
1794
1795         rts = mtod(m, struct ieee80211_frame_rts *);
1796
1797         rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1798             IEEE80211_FC0_SUBTYPE_RTS;
1799         rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1800         *(uint16_t *)rts->i_dur = htole16(dur);
1801         IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1802         IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1803
1804         m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts);
1805
1806         return m;
1807 }
1808
1809 static int
1810 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1811     struct ieee80211_node *ni)
1812 {
1813         struct ieee80211com *ic = &sc->sc_ic;
1814         struct rt2560_tx_desc *desc;
1815         struct rt2560_tx_data *data;
1816         struct rt2560_node *rn;
1817         struct ieee80211_frame *wh;
1818         struct ieee80211_key *k;
1819         struct mbuf *mnew;
1820         bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1821         uint16_t dur;
1822         uint32_t flags = 0;
1823         int nsegs, rate, error;
1824
1825         wh = mtod(m0, struct ieee80211_frame *);
1826
1827         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1828                 rate = ic->ic_fixed_rate;
1829         } else {
1830                 struct ieee80211_rateset *rs;
1831
1832                 rs = &ni->ni_rates;
1833                 rn = (struct rt2560_node *)ni;
1834                 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh,
1835                     m0->m_pkthdr.len, NULL, 0);
1836                 rate = rs->rs_rates[ni->ni_txrate];
1837         }
1838         rate &= IEEE80211_RATE_VAL;
1839
1840         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1841                 k = ieee80211_crypto_encap(ic, ni, m0);
1842                 if (k == NULL) {
1843                         m_freem(m0);
1844                         return ENOBUFS;
1845                 }
1846
1847                 /* packet header may have moved, reset our local pointer */
1848                 wh = mtod(m0, struct ieee80211_frame *);
1849         }
1850
1851         /*
1852          * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1853          * for directed frames only when the length of the MPDU is greater
1854          * than the length threshold indicated by [...]" ic_rtsthreshold.
1855          */
1856         if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1857             m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1858                 struct mbuf *m;
1859                 uint16_t dur;
1860                 int rtsrate, ackrate;
1861
1862                 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1863                 ackrate = rt2560_ack_rate(ic, rate);
1864
1865                 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1866                       rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1867                       rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1868                       3 * RAL_SIFS;
1869
1870                 m = rt2560_get_rts(sc, wh, dur);
1871
1872                 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1873                 data = &sc->txq.data[sc->txq.cur_encrypt];
1874
1875                 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1876                     m, segs, &nsegs, 0);
1877                 if (error != 0) {
1878                         device_printf(sc->sc_dev,
1879                             "could not map mbuf (error %d)\n", error);
1880                         m_freem(m);
1881                         m_freem(m0);
1882                         return error;
1883                 }
1884
1885                 /* avoid multiple free() of the same node for each fragment */
1886                 ieee80211_ref_node(ni);
1887
1888                 data->m = m;
1889                 data->ni = ni;
1890
1891                 /* RTS frames are not taken into account for rssadapt */
1892                 data->id.id_node = NULL;
1893
1894                 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1895                     RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1,
1896                     segs->ds_addr);
1897
1898                 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1899                     BUS_DMASYNC_PREWRITE);
1900
1901                 sc->txq.queued++;
1902                 sc->txq.cur_encrypt =
1903                     (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1904
1905                 /*
1906                  * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1907                  * asynchronous data frame shall be transmitted after the CTS
1908                  * frame and a SIFS period.
1909                  */
1910                 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1911         }
1912
1913         data = &sc->txq.data[sc->txq.cur_encrypt];
1914         desc = &sc->txq.desc[sc->txq.cur_encrypt];
1915
1916         error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1917             segs, &nsegs, 0);
1918         if (error != 0 && error != EFBIG) {
1919                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1920                     error);
1921                 m_freem(m0);
1922                 return error;
1923         }
1924         if (error != 0) {
1925                 mnew = m_defrag(m0, M_DONTWAIT);
1926                 if (mnew == NULL) {
1927                         device_printf(sc->sc_dev,
1928                             "could not defragment mbuf\n");
1929                         m_freem(m0);
1930                         return ENOBUFS;
1931                 }
1932                 m0 = mnew;
1933
1934                 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1935                     m0, segs, &nsegs, 0);
1936                 if (error != 0) {
1937                         device_printf(sc->sc_dev,
1938                             "could not map mbuf (error %d)\n", error);
1939                         m_freem(m0);
1940                         return error;
1941                 }
1942
1943                 /* packet header may have moved, reset our local pointer */
1944                 wh = mtod(m0, struct ieee80211_frame *);
1945         }
1946
1947         if (bpf_peers_present(sc->sc_drvbpf)) {
1948                 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1949
1950                 tap->wt_flags = 0;
1951                 tap->wt_rate = rate;
1952                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1953                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1954                 tap->wt_antenna = sc->tx_ant;
1955
1956                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1957         }
1958
1959         data->m = m0;
1960         data->ni = ni;
1961
1962         /* remember link conditions for rate adaptation algorithm */
1963         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1964                 data->id.id_len = m0->m_pkthdr.len;
1965                 data->id.id_rateidx = ni->ni_txrate;
1966                 data->id.id_node = ni;
1967                 data->id.id_rssi = ni->ni_rssi;
1968         } else
1969                 data->id.id_node = NULL;
1970
1971         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1972                 flags |= RT2560_TX_ACK;
1973
1974                 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
1975                     ic->ic_flags) + RAL_SIFS;
1976                 *(uint16_t *)wh->i_dur = htole16(dur);
1977         }
1978
1979         rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1980             segs->ds_addr);
1981
1982         bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1983         bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1984             BUS_DMASYNC_PREWRITE);
1985
1986         DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1987             m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1988
1989         /* kick encrypt */
1990         sc->txq.queued++;
1991         sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1992         RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1993
1994         return 0;
1995 }
1996
1997 static void
1998 rt2560_start(struct ifnet *ifp)
1999 {
2000         struct rt2560_softc *sc = ifp->if_softc;
2001         struct ieee80211com *ic = &sc->sc_ic;
2002         struct mbuf *m0;
2003         struct ether_header *eh;
2004         struct ieee80211_node *ni;
2005
2006         RAL_LOCK(sc);
2007
2008         /* prevent management frames from being sent if we're not ready */
2009         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2010                 RAL_UNLOCK(sc);
2011                 return;
2012         }
2013
2014         for (;;) {
2015                 IF_POLL(&ic->ic_mgtq, m0);
2016                 if (m0 != NULL) {
2017                         if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2018                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2019                                 sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2020                                 break;
2021                         }
2022                         IF_DEQUEUE(&ic->ic_mgtq, m0);
2023
2024                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2025                         m0->m_pkthdr.rcvif = NULL;
2026
2027                         if (bpf_peers_present(ic->ic_rawbpf))
2028                                 bpf_mtap(ic->ic_rawbpf, m0);
2029
2030                         if (rt2560_tx_mgt(sc, m0, ni) != 0) {
2031                                 ieee80211_free_node(ni);
2032                                 break;
2033                         }
2034                 } else {
2035                         if (ic->ic_state != IEEE80211_S_RUN)
2036                                 break;
2037                         IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2038                         if (m0 == NULL)
2039                                 break;
2040                         if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2041                                 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2042                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2043                                 sc->sc_flags |= RT2560_F_DATA_OACTIVE;
2044                                 break;
2045                         }
2046                         /*
2047                          * Cancel any background scan.
2048                          */
2049                         if (ic->ic_flags & IEEE80211_F_SCAN)
2050                                 ieee80211_cancel_scan(ic);
2051
2052                         if (m0->m_len < sizeof (struct ether_header) &&
2053                             !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2054                                 continue;
2055
2056                         eh = mtod(m0, struct ether_header *);
2057                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2058                         if (ni == NULL) {
2059                                 m_freem(m0);
2060                                 continue;
2061                         }
2062                         if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
2063                             (m0->m_flags & M_PWR_SAV) == 0) {
2064                                 /*
2065                                  * Station in power save mode; pass the frame
2066                                  * to the 802.11 layer and continue.  We'll get
2067                                  * the frame back when the time is right.
2068                                  */
2069                                 ieee80211_pwrsave(ni, m0);
2070                                 /*
2071                                  * If we're in power save mode 'cuz of a bg
2072                                  * scan cancel it so the traffic can flow.
2073                                  * The packet we just queued will automatically
2074                                  * get sent when we drop out of power save.
2075                                  * XXX locking
2076                                  */
2077                                 if (ic->ic_flags & IEEE80211_F_SCAN)
2078                                         ieee80211_cancel_scan(ic);
2079                                 ieee80211_free_node(ni);
2080                                 continue;
2081                         }
2082
2083                         BPF_MTAP(ifp, m0);
2084
2085                         m0 = ieee80211_encap(ic, m0, ni);
2086                         if (m0 == NULL) {
2087                                 ieee80211_free_node(ni);
2088                                 continue;
2089                         }
2090                         
2091                         if (bpf_peers_present(ic->ic_rawbpf))
2092                                 bpf_mtap(ic->ic_rawbpf, m0);
2093
2094                         if (rt2560_tx_data(sc, m0, ni) != 0) {
2095                                 ieee80211_free_node(ni);
2096                                 ifp->if_oerrors++;
2097                                 break;
2098                         }
2099                 }
2100
2101                 sc->sc_tx_timer = 5;
2102                 ic->ic_lastdata = ticks;
2103         }
2104
2105         RAL_UNLOCK(sc);
2106 }
2107
2108 static void
2109 rt2560_watchdog(void *arg)
2110 {
2111         struct rt2560_softc *sc = arg;
2112         struct ifnet *ifp = sc->sc_ifp;
2113
2114         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2115                 return;
2116
2117         rt2560_encryption_intr(sc);
2118         rt2560_tx_intr(sc);
2119
2120         if (sc->sc_tx_timer > 0) {
2121                 if (--sc->sc_tx_timer == 0) {
2122                         device_printf(sc->sc_dev, "device timeout\n");
2123                         rt2560_init(sc);
2124                         ifp->if_oerrors++;
2125                         /* watchdog timeout is set in rt2560_init() */
2126                         return;
2127                 }
2128         }
2129         callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2130 }
2131
2132 /*
2133  * This function allows for fast channel switching in monitor mode (used by
2134  * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2135  * generate a new beacon frame.
2136  */
2137 static int
2138 rt2560_reset(struct ifnet *ifp)
2139 {
2140         struct rt2560_softc *sc = ifp->if_softc;
2141         struct ieee80211com *ic = &sc->sc_ic;
2142
2143         if (ic->ic_opmode != IEEE80211_M_MONITOR)
2144                 return ENETRESET;
2145
2146         rt2560_set_chan(sc, ic->ic_curchan);
2147
2148         return 0;
2149 }
2150
2151 static int
2152 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2153 {
2154         struct rt2560_softc *sc = ifp->if_softc;
2155         struct ieee80211com *ic = &sc->sc_ic;
2156         int error = 0;
2157
2158
2159
2160         switch (cmd) {
2161         case SIOCSIFFLAGS:
2162                 if (ifp->if_flags & IFF_UP) {
2163                         RAL_LOCK(sc);
2164                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2165                                 rt2560_update_promisc(sc);
2166                         else
2167                                 rt2560_init(sc);
2168                         RAL_UNLOCK(sc);
2169                 } else {
2170                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2171                                 rt2560_stop(sc);
2172                 }
2173
2174                 break;
2175
2176         default:
2177                 error = ieee80211_ioctl(ic, cmd, data);
2178         }
2179
2180         if (error == ENETRESET) {
2181                 if ((ifp->if_flags & IFF_UP) &&
2182                     (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
2183                     (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2184                         rt2560_init(sc);
2185                 error = 0;
2186         }
2187
2188
2189         return error;
2190 }
2191
2192 static void
2193 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2194 {
2195         uint32_t tmp;
2196         int ntries;
2197
2198         for (ntries = 0; ntries < 100; ntries++) {
2199                 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2200                         break;
2201                 DELAY(1);
2202         }
2203         if (ntries == 100) {
2204                 device_printf(sc->sc_dev, "could not write to BBP\n");
2205                 return;
2206         }
2207
2208         tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2209         RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2210
2211         DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2212 }
2213
2214 static uint8_t
2215 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2216 {
2217         uint32_t val;
2218         int ntries;
2219
2220         for (ntries = 0; ntries < 100; ntries++) {
2221                 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2222                         break;
2223                 DELAY(1);
2224         }
2225         if (ntries == 100) {
2226                 device_printf(sc->sc_dev, "could not read from BBP\n");
2227                 return 0;
2228         }
2229
2230         val = RT2560_BBP_BUSY | reg << 8;
2231         RAL_WRITE(sc, RT2560_BBPCSR, val);
2232
2233         for (ntries = 0; ntries < 100; ntries++) {
2234                 val = RAL_READ(sc, RT2560_BBPCSR);
2235                 if (!(val & RT2560_BBP_BUSY))
2236                         return val & 0xff;
2237                 DELAY(1);
2238         }
2239
2240         device_printf(sc->sc_dev, "could not read from BBP\n");
2241         return 0;
2242 }
2243
2244 static void
2245 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2246 {
2247         uint32_t tmp;
2248         int ntries;
2249
2250         for (ntries = 0; ntries < 100; ntries++) {
2251                 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2252                         break;
2253                 DELAY(1);
2254         }
2255         if (ntries == 100) {
2256                 device_printf(sc->sc_dev, "could not write to RF\n");
2257                 return;
2258         }
2259
2260         tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2261             (reg & 0x3);
2262         RAL_WRITE(sc, RT2560_RFCSR, tmp);
2263
2264         /* remember last written value in sc */
2265         sc->rf_regs[reg] = val;
2266
2267         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2268 }
2269
2270 static void
2271 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2272 {
2273         struct ieee80211com *ic = &sc->sc_ic;
2274         uint8_t power, tmp;
2275         u_int i, chan;
2276
2277         chan = ieee80211_chan2ieee(ic, c);
2278         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2279                 return;
2280
2281         if (IEEE80211_IS_CHAN_2GHZ(c))
2282                 power = min(sc->txpow[chan - 1], 31);
2283         else
2284                 power = 31;
2285
2286         /* adjust txpower using ifconfig settings */
2287         power -= (100 - ic->ic_txpowlimit) / 8;
2288
2289         DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2290
2291         switch (sc->rf_rev) {
2292         case RT2560_RF_2522:
2293                 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2294                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2295                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2296                 break;
2297
2298         case RT2560_RF_2523:
2299                 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2300                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2301                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2302                 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2303                 break;
2304
2305         case RT2560_RF_2524:
2306                 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2307                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2308                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2309                 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2310                 break;
2311
2312         case RT2560_RF_2525:
2313                 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2314                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2315                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2316                 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2317
2318                 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2319                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2320                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2321                 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2322                 break;
2323
2324         case RT2560_RF_2525E:
2325                 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2326                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2327                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2328                 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2329                 break;
2330
2331         case RT2560_RF_2526:
2332                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2333                 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2334                 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2335
2336                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2337                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2338                 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2339                 break;
2340
2341         /* dual-band RF */
2342         case RT2560_RF_5222:
2343                 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2344
2345                 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2346                 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2347                 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2348                 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2349                 break;
2350         default: 
2351                 printf("unknown ral rev=%d\n", sc->rf_rev);
2352         }
2353
2354         if (ic->ic_state != IEEE80211_S_SCAN) {
2355                 /* set Japan filter bit for channel 14 */
2356                 tmp = rt2560_bbp_read(sc, 70);
2357
2358                 tmp &= ~RT2560_JAPAN_FILTER;
2359                 if (chan == 14)
2360                         tmp |= RT2560_JAPAN_FILTER;
2361
2362                 rt2560_bbp_write(sc, 70, tmp);
2363
2364                 /* clear CRC errors */
2365                 RAL_READ(sc, RT2560_CNT0);
2366         }
2367 }
2368
2369 static void
2370 rt2560_set_channel(struct ieee80211com *ic)
2371 {
2372         struct ifnet *ifp = ic->ic_ifp;
2373         struct rt2560_softc *sc = ifp->if_softc;
2374
2375         RAL_LOCK(sc);
2376         rt2560_set_chan(sc, ic->ic_curchan);
2377         RAL_UNLOCK(sc);
2378
2379 }
2380
2381 #if 0
2382 /*
2383  * Disable RF auto-tuning.
2384  */
2385 static void
2386 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2387 {
2388         uint32_t tmp;
2389
2390         if (sc->rf_rev != RT2560_RF_2523) {
2391                 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2392                 rt2560_rf_write(sc, RAL_RF1, tmp);
2393         }
2394
2395         tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2396         rt2560_rf_write(sc, RAL_RF3, tmp);
2397
2398         DPRINTFN(2, ("disabling RF autotune\n"));
2399 }
2400 #endif
2401
2402 /*
2403  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2404  * synchronization.
2405  */
2406 static void
2407 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2408 {
2409         struct ieee80211com *ic = &sc->sc_ic;
2410         uint16_t logcwmin, preload;
2411         uint32_t tmp;
2412
2413         /* first, disable TSF synchronization */
2414         RAL_WRITE(sc, RT2560_CSR14, 0);
2415
2416         tmp = 16 * ic->ic_bss->ni_intval;
2417         RAL_WRITE(sc, RT2560_CSR12, tmp);
2418
2419         RAL_WRITE(sc, RT2560_CSR13, 0);
2420
2421         logcwmin = 5;
2422         preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2423         tmp = logcwmin << 16 | preload;
2424         RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2425
2426         /* finally, enable TSF synchronization */
2427         tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2428         if (ic->ic_opmode == IEEE80211_M_STA)
2429                 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2430         else
2431                 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2432                        RT2560_ENABLE_BEACON_GENERATOR;
2433         RAL_WRITE(sc, RT2560_CSR14, tmp);
2434
2435         DPRINTF(("enabling TSF synchronization\n"));
2436 }
2437
2438 static void
2439 rt2560_update_plcp(struct rt2560_softc *sc)
2440 {
2441         struct ieee80211com *ic = &sc->sc_ic;
2442
2443         /* no short preamble for 1Mbps */
2444         RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2445
2446         if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2447                 /* values taken from the reference driver */
2448                 RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
2449                 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2450                 RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
2451         } else {
2452                 /* same values as above or'ed 0x8 */
2453                 RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
2454                 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2455                 RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
2456         }
2457
2458         DPRINTF(("updating PLCP for %s preamble\n",
2459             (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2460 }
2461
2462 /*
2463  * This function can be called by ieee80211_set_shortslottime(). Refer to
2464  * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2465  */
2466 static void
2467 rt2560_update_slot(struct ifnet *ifp)
2468 {
2469         struct rt2560_softc *sc = ifp->if_softc;
2470         struct ieee80211com *ic = &sc->sc_ic;
2471         uint8_t slottime;
2472         uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2473         uint32_t tmp;
2474
2475 #ifndef FORCE_SLOTTIME
2476         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2477 #else
2478         /*
2479          * Setting slot time according to "short slot time" capability
2480          * in beacon/probe_resp seems to cause problem to acknowledge
2481          * certain AP's data frames transimitted at CCK/DS rates: the
2482          * problematic AP keeps retransmitting data frames, probably
2483          * because MAC level acks are not received by hardware.
2484          * So we cheat a little bit here by claiming we are capable of
2485          * "short slot time" but setting hardware slot time to the normal
2486          * slot time.  ral(4) does not seem to have trouble to receive
2487          * frames transmitted using short slot time even if hardware
2488          * slot time is set to normal slot time.  If we didn't use this
2489          * trick, we would have to claim that short slot time is not
2490          * supported; this would give relative poor RX performance
2491          * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2492          * slot time.
2493          */
2494         slottime = 20;
2495 #endif
2496
2497         /* update the MAC slot boundaries */
2498         tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2499         tx_pifs = tx_sifs + slottime;
2500         tx_difs = tx_sifs + 2 * slottime;
2501         eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2502
2503         tmp = RAL_READ(sc, RT2560_CSR11);
2504         tmp = (tmp & ~0x1f00) | slottime << 8;
2505         RAL_WRITE(sc, RT2560_CSR11, tmp);
2506
2507         tmp = tx_pifs << 16 | tx_sifs;
2508         RAL_WRITE(sc, RT2560_CSR18, tmp);
2509
2510         tmp = eifs << 16 | tx_difs;
2511         RAL_WRITE(sc, RT2560_CSR19, tmp);
2512
2513         DPRINTF(("setting slottime to %uus\n", slottime));
2514 }
2515
2516 static void
2517 rt2560_set_basicrates(struct rt2560_softc *sc)
2518 {
2519         struct ieee80211com *ic = &sc->sc_ic;
2520
2521         /* update basic rate set */
2522         if (ic->ic_curmode == IEEE80211_MODE_11B) {
2523                 /* 11b basic rates: 1, 2Mbps */
2524                 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2525         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2526                 /* 11a basic rates: 6, 12, 24Mbps */
2527                 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2528         } else {
2529                 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2530                 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2531         }
2532 }
2533
2534 static void
2535 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2536 {
2537         uint32_t tmp;
2538
2539         /* set ON period to 70ms and OFF period to 30ms */
2540         tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2541         RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2542 }
2543
2544 static void
2545 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2546 {
2547         uint32_t tmp;
2548
2549         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2550         RAL_WRITE(sc, RT2560_CSR5, tmp);
2551
2552         tmp = bssid[4] | bssid[5] << 8;
2553         RAL_WRITE(sc, RT2560_CSR6, tmp);
2554
2555         DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2556 }
2557
2558 static void
2559 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2560 {
2561         uint32_t tmp;
2562
2563         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2564         RAL_WRITE(sc, RT2560_CSR3, tmp);
2565
2566         tmp = addr[4] | addr[5] << 8;
2567         RAL_WRITE(sc, RT2560_CSR4, tmp);
2568
2569         DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2570 }
2571
2572 static void
2573 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2574 {
2575         uint32_t tmp;
2576
2577         tmp = RAL_READ(sc, RT2560_CSR3);
2578         addr[0] = tmp & 0xff;
2579         addr[1] = (tmp >>  8) & 0xff;
2580         addr[2] = (tmp >> 16) & 0xff;
2581         addr[3] = (tmp >> 24);
2582
2583         tmp = RAL_READ(sc, RT2560_CSR4);
2584         addr[4] = tmp & 0xff;
2585         addr[5] = (tmp >> 8) & 0xff;
2586 }
2587
2588 static void
2589 rt2560_update_promisc(struct rt2560_softc *sc)
2590 {
2591         struct ifnet *ifp = sc->sc_ic.ic_ifp;
2592         uint32_t tmp;
2593
2594         tmp = RAL_READ(sc, RT2560_RXCSR0);
2595
2596         tmp &= ~RT2560_DROP_NOT_TO_ME;
2597         if (!(ifp->if_flags & IFF_PROMISC))
2598                 tmp |= RT2560_DROP_NOT_TO_ME;
2599
2600         RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2601
2602         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2603             "entering" : "leaving"));
2604 }
2605
2606 static const char *
2607 rt2560_get_rf(int rev)
2608 {
2609         switch (rev) {
2610         case RT2560_RF_2522:    return "RT2522";
2611         case RT2560_RF_2523:    return "RT2523";
2612         case RT2560_RF_2524:    return "RT2524";
2613         case RT2560_RF_2525:    return "RT2525";
2614         case RT2560_RF_2525E:   return "RT2525e";
2615         case RT2560_RF_2526:    return "RT2526";
2616         case RT2560_RF_5222:    return "RT5222";
2617         default:                return "unknown";
2618         }
2619 }
2620
2621 static void
2622 rt2560_read_config(struct rt2560_softc *sc)
2623 {
2624         uint16_t val;
2625         int i;
2626
2627         val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2628         sc->rf_rev =   (val >> 11) & 0x7;
2629         sc->hw_radio = (val >> 10) & 0x1;
2630         sc->led_mode = (val >> 6)  & 0x7;
2631         sc->rx_ant =   (val >> 4)  & 0x3;
2632         sc->tx_ant =   (val >> 2)  & 0x3;
2633         sc->nb_ant =   val & 0x3;
2634
2635         /* read default values for BBP registers */
2636         for (i = 0; i < 16; i++) {
2637                 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2638                 if (val == 0 || val == 0xffff)
2639                         continue;
2640
2641                 sc->bbp_prom[i].reg = val >> 8;
2642                 sc->bbp_prom[i].val = val & 0xff;
2643         }
2644
2645         /* read Tx power for all b/g channels */
2646         for (i = 0; i < 14 / 2; i++) {
2647                 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2648                 sc->txpow[i * 2] = val & 0xff;
2649                 sc->txpow[i * 2 + 1] = val >> 8;
2650         }
2651         for (i = 0; i < 14; ++i) {
2652                 if (sc->txpow[i] > 31)
2653                         sc->txpow[i] = 24;
2654         }
2655
2656         val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2657         if ((val & 0xff) == 0xff)
2658                 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2659         else
2660                 sc->rssi_corr = val & 0xff;
2661         DPRINTF(("rssi correction %d, calibrate 0x%02x\n",
2662                  sc->rssi_corr, val));
2663 }
2664
2665
2666 static void
2667 rt2560_scan_start(struct ieee80211com *ic)
2668 {
2669         struct ifnet *ifp = ic->ic_ifp;
2670         struct rt2560_softc *sc = ifp->if_softc;
2671
2672         /* abort TSF synchronization */
2673         RAL_WRITE(sc, RT2560_CSR14, 0);
2674         rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2675 }
2676
2677 static void
2678 rt2560_scan_end(struct ieee80211com *ic)
2679 {
2680         struct ifnet *ifp = ic->ic_ifp;
2681         struct rt2560_softc *sc = ifp->if_softc;
2682
2683         rt2560_enable_tsf_sync(sc);
2684         /* XXX keep local copy */
2685         rt2560_set_bssid(sc, ic->ic_bss->ni_bssid);
2686 }
2687
2688 static int
2689 rt2560_bbp_init(struct rt2560_softc *sc)
2690 {
2691 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2692         int i, ntries;
2693
2694         /* wait for BBP to be ready */
2695         for (ntries = 0; ntries < 100; ntries++) {
2696                 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2697                         break;
2698                 DELAY(1);
2699         }
2700         if (ntries == 100) {
2701                 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2702                 return EIO;
2703         }
2704
2705         /* initialize BBP registers to default values */
2706         for (i = 0; i < N(rt2560_def_bbp); i++) {
2707                 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2708                     rt2560_def_bbp[i].val);
2709         }
2710
2711         /* initialize BBP registers to values stored in EEPROM */
2712         for (i = 0; i < 16; i++) {
2713                 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2714                         break;
2715                 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2716         }
2717         rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */
2718
2719         return 0;
2720 #undef N
2721 }
2722
2723 static void
2724 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2725 {
2726         uint32_t tmp;
2727         uint8_t tx;
2728
2729         tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2730         if (antenna == 1)
2731                 tx |= RT2560_BBP_ANTA;
2732         else if (antenna == 2)
2733                 tx |= RT2560_BBP_ANTB;
2734         else
2735                 tx |= RT2560_BBP_DIVERSITY;
2736
2737         /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2738         if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2739             sc->rf_rev == RT2560_RF_5222)
2740                 tx |= RT2560_BBP_FLIPIQ;
2741
2742         rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2743
2744         /* update values for CCK and OFDM in BBPCSR1 */
2745         tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2746         tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2747         RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2748 }
2749
2750 static void
2751 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2752 {
2753         uint8_t rx;
2754
2755         rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2756         if (antenna == 1)
2757                 rx |= RT2560_BBP_ANTA;
2758         else if (antenna == 2)
2759                 rx |= RT2560_BBP_ANTB;
2760         else
2761                 rx |= RT2560_BBP_DIVERSITY;
2762
2763         /* need to force no I/Q flip for RF 2525e and 2526 */
2764         if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2765                 rx &= ~RT2560_BBP_FLIPIQ;
2766
2767         rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2768 }
2769
2770 static void
2771 rt2560_init(void *priv)
2772 {
2773 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2774         struct rt2560_softc *sc = priv;
2775         struct ieee80211com *ic = &sc->sc_ic;
2776         struct ifnet *ifp = ic->ic_ifp;
2777         uint32_t tmp;
2778         int i;
2779
2780
2781
2782         rt2560_stop(sc);
2783
2784         RAL_LOCK(sc);
2785         /* setup tx rings */
2786         tmp = RT2560_PRIO_RING_COUNT << 24 |
2787               RT2560_ATIM_RING_COUNT << 16 |
2788               RT2560_TX_RING_COUNT   <<  8 |
2789               RT2560_TX_DESC_SIZE;
2790
2791         /* rings must be initialized in this exact order */
2792         RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2793         RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2794         RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2795         RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2796         RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2797
2798         /* setup rx ring */
2799         tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2800
2801         RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2802         RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2803
2804         /* initialize MAC registers to default values */
2805         for (i = 0; i < N(rt2560_def_mac); i++)
2806                 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2807
2808         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2809         rt2560_set_macaddr(sc, ic->ic_myaddr);
2810
2811         /* set basic rate set (will be updated later) */
2812         RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2813
2814         rt2560_update_slot(ifp);
2815         rt2560_update_plcp(sc);
2816         rt2560_update_led(sc, 0, 0);
2817
2818         RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2819         RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2820
2821         if (rt2560_bbp_init(sc) != 0) {
2822                 rt2560_stop(sc);
2823                 RAL_UNLOCK(sc);
2824                 return;
2825         }
2826
2827         rt2560_set_txantenna(sc, sc->tx_ant);
2828         rt2560_set_rxantenna(sc, sc->rx_ant);
2829
2830         /* set default BSS channel */
2831         rt2560_set_chan(sc, ic->ic_curchan);
2832
2833         /* kick Rx */
2834         tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2835         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2836                 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2837                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2838                         tmp |= RT2560_DROP_TODS;
2839                 if (!(ifp->if_flags & IFF_PROMISC))
2840                         tmp |= RT2560_DROP_NOT_TO_ME;
2841         }
2842         RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2843
2844         /* clear old FCS and Rx FIFO errors */
2845         RAL_READ(sc, RT2560_CNT0);
2846         RAL_READ(sc, RT2560_CNT4);
2847
2848         /* clear any pending interrupts */
2849         RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2850
2851         /* enable interrupts */
2852         RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2853
2854         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2855         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2856
2857         callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2858
2859         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2860                 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2861                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2862         } else
2863                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2864
2865         RAL_UNLOCK(sc);
2866 #undef N
2867 }
2868
2869 void
2870 rt2560_stop(void *arg)
2871 {
2872         struct rt2560_softc *sc = arg;
2873         struct ieee80211com *ic = &sc->sc_ic;
2874         struct ifnet *ifp = ic->ic_ifp;
2875         volatile int *flags = &sc->sc_flags;
2876
2877         while (*flags & RT2560_F_INPUT_RUNNING) {
2878                 tsleep(sc, 0, "ralrunning", hz/10);
2879         }
2880
2881         RAL_LOCK(sc);
2882
2883         callout_stop(&sc->watchdog_ch);
2884
2885         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2886                 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2887                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2888
2889                 /* abort Tx */
2890                 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2891                 
2892                 /* disable Rx */
2893                 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2894
2895                 /* reset ASIC (imply reset BBP) */
2896                 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2897                 RAL_WRITE(sc, RT2560_CSR1, 0);
2898
2899                 /* disable interrupts */
2900                 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2901                 
2902                 /* reset Tx and Rx rings */
2903                 rt2560_reset_tx_ring(sc, &sc->txq);
2904                 rt2560_reset_tx_ring(sc, &sc->atimq);
2905                 rt2560_reset_tx_ring(sc, &sc->prioq);
2906                 rt2560_reset_tx_ring(sc, &sc->bcnq);
2907                 rt2560_reset_rx_ring(sc, &sc->rxq);
2908         }
2909         sc->sc_tx_timer = 0;
2910         sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2911
2912         RAL_UNLOCK(sc);
2913 }
2914
2915 static int
2916 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2917         const struct ieee80211_bpf_params *params)
2918 {
2919         struct ieee80211com *ic = ni->ni_ic;
2920         struct ifnet *ifp = ic->ic_ifp;
2921         struct rt2560_softc *sc = ifp->if_softc;
2922
2923         RAL_LOCK(sc);
2924
2925         /* prevent management frames from being sent if we're not ready */
2926         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2927                 RAL_UNLOCK(sc);
2928                 m_freem(m);
2929                 ieee80211_free_node(ni);
2930                 return ENETDOWN;
2931         }
2932         if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2933                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2934                 RAL_UNLOCK(sc);
2935                 m_freem(m);
2936                 ieee80211_free_node(ni);
2937                 return ENOBUFS;         /* XXX */
2938         }
2939
2940         if (bpf_peers_present(ic->ic_rawbpf))
2941                 bpf_mtap(ic->ic_rawbpf, m);
2942
2943         ifp->if_opackets++;
2944
2945         if (params == NULL) {
2946                 /*
2947                  * Legacy path; interpret frame contents to decide
2948                  * precisely how to send the frame.
2949                  */
2950                 if (rt2560_tx_mgt(sc, m, ni) != 0)
2951                         goto bad;
2952         } else {
2953                 /*
2954                  * Caller supplied explicit parameters to use in
2955                  * sending the frame.
2956                  */
2957                 if (rt2560_tx_raw(sc, m, ni, params))
2958                         goto bad;
2959         }
2960         sc->sc_tx_timer = 5;
2961
2962         RAL_UNLOCK(sc);
2963
2964         return 0;
2965 bad:
2966         ifp->if_oerrors++;
2967         ieee80211_free_node(ni);
2968         RAL_UNLOCK(sc);
2969         return EIO;             /* XXX */
2970 }