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