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