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