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