atw.c revision 1.60 1 /* $NetBSD: atw.c,v 1.60 2004/07/15 07:20:46 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2002, 2003, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Young, by Jason R. Thorpe, and by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Device driver for the ADMtek ADM8211 802.11 MAC/BBP.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: atw.c,v 1.60 2004/07/15 07:20:46 dyoung Exp $");
45
46 #include "bpfilter.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/callout.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58 #include <sys/time.h>
59
60 #include <machine/endian.h>
61
62 #include <uvm/uvm_extern.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_ether.h>
68
69 #include <net80211/ieee80211_var.h>
70 #include <net80211/ieee80211_compat.h>
71 #include <net80211/ieee80211_radiotap.h>
72
73 #if NBPFILTER > 0
74 #include <net/bpf.h>
75 #endif
76
77 #include <machine/bus.h>
78 #include <machine/intr.h>
79
80 #include <dev/ic/atwreg.h>
81 #include <dev/ic/rf3000reg.h>
82 #include <dev/ic/si4136reg.h>
83 #include <dev/ic/atwvar.h>
84 #include <dev/ic/smc93cx6var.h>
85
86 /* XXX TBD open questions
87 *
88 *
89 * When should I set DSSS PAD in reg 0x15 of RF3000? In 1-2Mbps
90 * modes only, or all modes (5.5-11 Mbps CCK modes, too?) Does the MAC
91 * handle this for me?
92 *
93 */
94 /* device attachment
95 *
96 * print TOFS[012]
97 *
98 * device initialization
99 *
100 * clear ATW_FRCTL_MAXPSP to disable max power saving
101 * set ATW_TXBR_ALCUPDATE to enable ALC
102 * set TOFS[012]? (hope not)
103 * disable rx/tx
104 * set ATW_PAR_SWR (software reset)
105 * wait for ATW_PAR_SWR clear
106 * disable interrupts
107 * ack status register
108 * enable interrupts
109 *
110 * rx/tx initialization
111 *
112 * disable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
113 * allocate and init descriptor rings
114 * write ATW_PAR_DSL (descriptor skip length)
115 * write descriptor base addrs: ATW_TDBD, ATW_TDBP, write ATW_RDB
116 * write ATW_NAR_SQ for one/both transmit descriptor rings
117 * write ATW_NAR_SQ for one/both transmit descriptor rings
118 * enable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
119 *
120 * rx/tx end
121 *
122 * stop DMA
123 * disable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
124 * flush tx w/ ATW_NAR_HF
125 *
126 * scan
127 *
128 * initialize rx/tx
129 *
130 * BSS join: (re)association response
131 *
132 * set ATW_FRCTL_AID
133 *
134 * optimizations ???
135 *
136 */
137
138 #define ATW_REFSLAVE /* slavishly do what the reference driver does */
139
140 #define VOODOO_DUR_11_ROUNDING 0x01 /* necessary */
141 #define VOODOO_DUR_2_4_SPECIALCASE 0x02 /* NOT necessary */
142 int atw_voodoo = VOODOO_DUR_11_ROUNDING;
143
144 int atw_bbp_io_enable_delay = 20 * 1000;
145 int atw_bbp_io_disable_delay = 2 * 1000;
146 int atw_writewep_delay = 1000;
147 int atw_beacon_len_adjust = 4;
148 int atw_dwelltime = 200;
149 int atw_xindiv2 = 0;
150
151 #ifdef ATW_DEBUG
152 int atw_debug = 0;
153
154 #define ATW_DPRINTF(x) if (atw_debug > 0) printf x
155 #define ATW_DPRINTF2(x) if (atw_debug > 1) printf x
156 #define ATW_DPRINTF3(x) if (atw_debug > 2) printf x
157 #define DPRINTF(sc, x) if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) printf x
158 #define DPRINTF2(sc, x) if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) ATW_DPRINTF2(x)
159 #define DPRINTF3(sc, x) if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) ATW_DPRINTF3(x)
160
161 static void atw_print_regs(struct atw_softc *, const char *);
162 static void atw_dump_pkt(struct ifnet *, struct mbuf *);
163
164 /* Note well: I never got atw_rf3000_read or atw_si4126_read to work. */
165 # ifdef ATW_BBPDEBUG
166 static int atw_rf3000_read(struct atw_softc *sc, u_int, u_int *);
167 static void atw_rf3000_print(struct atw_softc *);
168 # endif /* ATW_BBPDEBUG */
169
170 # ifdef ATW_SYNDEBUG
171 static int atw_si4126_read(struct atw_softc *, u_int, u_int *);
172 static void atw_si4126_print(struct atw_softc *);
173 # endif /* ATW_SYNDEBUG */
174
175 #else
176 #define ATW_DPRINTF(x)
177 #define ATW_DPRINTF2(x)
178 #define ATW_DPRINTF3(x)
179 #define DPRINTF(sc, x) /* nothing */
180 #define DPRINTF2(sc, x) /* nothing */
181 #define DPRINTF3(sc, x) /* nothing */
182 #endif
183
184 #ifdef ATW_STATS
185 void atw_print_stats(struct atw_softc *);
186 #endif
187
188 void atw_start(struct ifnet *);
189 void atw_watchdog(struct ifnet *);
190 int atw_ioctl(struct ifnet *, u_long, caddr_t);
191 int atw_init(struct ifnet *);
192 void atw_txdrain(struct atw_softc *);
193 void atw_stop(struct ifnet *, int);
194
195 void atw_reset(struct atw_softc *);
196 int atw_read_srom(struct atw_softc *);
197
198 void atw_shutdown(void *);
199
200 void atw_rxdrain(struct atw_softc *);
201 int atw_add_rxbuf(struct atw_softc *, int);
202 void atw_idle(struct atw_softc *, u_int32_t);
203
204 int atw_enable(struct atw_softc *);
205 void atw_disable(struct atw_softc *);
206 void atw_power(int, void *);
207
208 void atw_rxintr(struct atw_softc *);
209 void atw_txintr(struct atw_softc *);
210 void atw_linkintr(struct atw_softc *, u_int32_t);
211
212 static int atw_newstate(struct ieee80211com *, enum ieee80211_state, int);
213 static void atw_tsf(struct atw_softc *);
214 static void atw_start_beacon(struct atw_softc *, int);
215 static void atw_write_wep(struct atw_softc *);
216 static void atw_write_bssid(struct atw_softc *);
217 static void atw_write_ssid(struct atw_softc *);
218 static void atw_write_sup_rates(struct atw_softc *);
219 static void atw_clear_sram(struct atw_softc *);
220 static void atw_write_sram(struct atw_softc *, u_int, u_int8_t *, u_int);
221 static int atw_media_change(struct ifnet *);
222 static void atw_media_status(struct ifnet *, struct ifmediareq *);
223 static void atw_filter_setup(struct atw_softc *);
224 static void atw_frame_setdurs(struct atw_softc *, struct atw_frame *, int, int);
225 static __inline u_int64_t atw_predict_beacon(u_int64_t, u_int32_t);
226 static void atw_recv_beacon(struct ieee80211com *, struct mbuf *,
227 struct ieee80211_node *, int, int, u_int32_t);
228 static void atw_recv_mgmt(struct ieee80211com *, struct mbuf *,
229 struct ieee80211_node *, int, int, u_int32_t);
230 static void atw_node_free(struct ieee80211com *, struct ieee80211_node *);
231 static struct ieee80211_node *atw_node_alloc(struct ieee80211com *);
232
233 static int atw_tune(struct atw_softc *);
234
235 static void atw_rfio_enable(struct atw_softc *, int);
236
237 /* RFMD RF3000 Baseband Processor */
238 static int atw_rf3000_init(struct atw_softc *);
239 static int atw_rf3000_tune(struct atw_softc *, u_int8_t);
240 static int atw_rf3000_write(struct atw_softc *, u_int, u_int);
241 #ifdef ATW_DEBUG
242 static int atw_rf3000_read(struct atw_softc *sc, u_int, u_int *);
243 #endif /* ATW_DEBUG */
244
245 /* Silicon Laboratories Si4126 RF/IF Synthesizer */
246 static int atw_si4126_tune(struct atw_softc *, u_int8_t);
247 static int atw_si4126_write(struct atw_softc *, u_int, u_int);
248 #ifdef ATW_DEBUG
249 static int atw_si4126_read(struct atw_softc *, u_int, u_int *);
250 #endif /* ATW_DEBUG */
251
252 const struct atw_txthresh_tab atw_txthresh_tab_lo[] = ATW_TXTHRESH_TAB_LO_RATE;
253 const struct atw_txthresh_tab atw_txthresh_tab_hi[] = ATW_TXTHRESH_TAB_HI_RATE;
254
255 const char *atw_tx_state[] = {
256 "STOPPED",
257 "RUNNING - read descriptor",
258 "RUNNING - transmitting",
259 "RUNNING - filling fifo", /* XXX */
260 "SUSPENDED",
261 "RUNNING -- write descriptor",
262 "RUNNING -- write last descriptor",
263 "RUNNING - fifo full"
264 };
265
266 const char *atw_rx_state[] = {
267 "STOPPED",
268 "RUNNING - read descriptor",
269 "RUNNING - check this packet, pre-fetch next",
270 "RUNNING - wait for reception",
271 "SUSPENDED",
272 "RUNNING - write descriptor",
273 "RUNNING - flush fifo",
274 "RUNNING - fifo drain"
275 };
276
277 int
278 atw_activate(struct device *self, enum devact act)
279 {
280 struct atw_softc *sc = (struct atw_softc *)self;
281 int rv = 0, s;
282
283 s = splnet();
284 switch (act) {
285 case DVACT_ACTIVATE:
286 rv = EOPNOTSUPP;
287 break;
288
289 case DVACT_DEACTIVATE:
290 if_deactivate(&sc->sc_ic.ic_if);
291 break;
292 }
293 splx(s);
294 return rv;
295 }
296
297 /*
298 * atw_enable:
299 *
300 * Enable the ADM8211 chip.
301 */
302 int
303 atw_enable(struct atw_softc *sc)
304 {
305
306 if (ATW_IS_ENABLED(sc) == 0) {
307 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
308 printf("%s: device enable failed\n",
309 sc->sc_dev.dv_xname);
310 return (EIO);
311 }
312 sc->sc_flags |= ATWF_ENABLED;
313 }
314 return (0);
315 }
316
317 /*
318 * atw_disable:
319 *
320 * Disable the ADM8211 chip.
321 */
322 void
323 atw_disable(struct atw_softc *sc)
324 {
325 if (!ATW_IS_ENABLED(sc))
326 return;
327 if (sc->sc_disable != NULL)
328 (*sc->sc_disable)(sc);
329 sc->sc_flags &= ~ATWF_ENABLED;
330 }
331
332 /* Returns -1 on failure. */
333 int
334 atw_read_srom(struct atw_softc *sc)
335 {
336 struct seeprom_descriptor sd;
337 u_int32_t reg;
338
339 (void)memset(&sd, 0, sizeof(sd));
340
341 reg = ATW_READ(sc, ATW_TEST0);
342
343 if ((reg & (ATW_TEST0_EPNE|ATW_TEST0_EPSNM)) != 0) {
344 printf("%s: bad or missing/bad SROM\n", sc->sc_dev.dv_xname);
345 return -1;
346 }
347
348 switch (reg & ATW_TEST0_EPTYP_MASK) {
349 case ATW_TEST0_EPTYP_93c66:
350 ATW_DPRINTF(("%s: 93c66 SROM\n", sc->sc_dev.dv_xname));
351 sc->sc_sromsz = 512;
352 sd.sd_chip = C56_66;
353 break;
354 case ATW_TEST0_EPTYP_93c46:
355 ATW_DPRINTF(("%s: 93c46 SROM\n", sc->sc_dev.dv_xname));
356 sc->sc_sromsz = 128;
357 sd.sd_chip = C46;
358 break;
359 default:
360 printf("%s: unknown SROM type %d\n", sc->sc_dev.dv_xname,
361 MASK_AND_RSHIFT(reg, ATW_TEST0_EPTYP_MASK));
362 return -1;
363 }
364
365 sc->sc_srom = malloc(sc->sc_sromsz, M_DEVBUF, M_NOWAIT);
366
367 if (sc->sc_srom == NULL) {
368 printf("%s: unable to allocate SROM buffer\n",
369 sc->sc_dev.dv_xname);
370 return -1;
371 }
372
373 (void)memset(sc->sc_srom, 0, sc->sc_sromsz);
374
375 /* ADM8211 has a single 32-bit register for controlling the
376 * 93cx6 SROM. Bit SRS enables the serial port. There is no
377 * "ready" bit. The ADM8211 input/output sense is the reverse
378 * of read_seeprom's.
379 */
380 sd.sd_tag = sc->sc_st;
381 sd.sd_bsh = sc->sc_sh;
382 sd.sd_regsize = 4;
383 sd.sd_control_offset = ATW_SPR;
384 sd.sd_status_offset = ATW_SPR;
385 sd.sd_dataout_offset = ATW_SPR;
386 sd.sd_CK = ATW_SPR_SCLK;
387 sd.sd_CS = ATW_SPR_SCS;
388 sd.sd_DI = ATW_SPR_SDO;
389 sd.sd_DO = ATW_SPR_SDI;
390 sd.sd_MS = ATW_SPR_SRS;
391 sd.sd_RDY = 0;
392
393 if (!read_seeprom(&sd, sc->sc_srom, 0, sc->sc_sromsz/2)) {
394 printf("%s: could not read SROM\n", sc->sc_dev.dv_xname);
395 free(sc->sc_srom, M_DEVBUF);
396 return -1;
397 }
398 #ifdef ATW_DEBUG
399 {
400 int i;
401 ATW_DPRINTF(("\nSerial EEPROM:\n\t"));
402 for (i = 0; i < sc->sc_sromsz/2; i = i + 1) {
403 if (((i % 8) == 0) && (i != 0)) {
404 ATW_DPRINTF(("\n\t"));
405 }
406 ATW_DPRINTF((" 0x%x", sc->sc_srom[i]));
407 }
408 ATW_DPRINTF(("\n"));
409 }
410 #endif /* ATW_DEBUG */
411 return 0;
412 }
413
414 #ifdef ATW_DEBUG
415 static void
416 atw_print_regs(struct atw_softc *sc, const char *where)
417 {
418 #define PRINTREG(sc, reg) \
419 ATW_DPRINTF2(("%s: reg[ " #reg " / %03x ] = %08x\n", \
420 sc->sc_dev.dv_xname, reg, ATW_READ(sc, reg)))
421
422 ATW_DPRINTF2(("%s: %s\n", sc->sc_dev.dv_xname, where));
423
424 PRINTREG(sc, ATW_PAR);
425 PRINTREG(sc, ATW_FRCTL);
426 PRINTREG(sc, ATW_TDR);
427 PRINTREG(sc, ATW_WTDP);
428 PRINTREG(sc, ATW_RDR);
429 PRINTREG(sc, ATW_WRDP);
430 PRINTREG(sc, ATW_RDB);
431 PRINTREG(sc, ATW_CSR3A);
432 PRINTREG(sc, ATW_TDBD);
433 PRINTREG(sc, ATW_TDBP);
434 PRINTREG(sc, ATW_STSR);
435 PRINTREG(sc, ATW_CSR5A);
436 PRINTREG(sc, ATW_NAR);
437 PRINTREG(sc, ATW_CSR6A);
438 PRINTREG(sc, ATW_IER);
439 PRINTREG(sc, ATW_CSR7A);
440 PRINTREG(sc, ATW_LPC);
441 PRINTREG(sc, ATW_TEST1);
442 PRINTREG(sc, ATW_SPR);
443 PRINTREG(sc, ATW_TEST0);
444 PRINTREG(sc, ATW_WCSR);
445 PRINTREG(sc, ATW_WPDR);
446 PRINTREG(sc, ATW_GPTMR);
447 PRINTREG(sc, ATW_GPIO);
448 PRINTREG(sc, ATW_BBPCTL);
449 PRINTREG(sc, ATW_SYNCTL);
450 PRINTREG(sc, ATW_PLCPHD);
451 PRINTREG(sc, ATW_MMIWADDR);
452 PRINTREG(sc, ATW_MMIRADDR1);
453 PRINTREG(sc, ATW_MMIRADDR2);
454 PRINTREG(sc, ATW_TXBR);
455 PRINTREG(sc, ATW_CSR15A);
456 PRINTREG(sc, ATW_ALCSTAT);
457 PRINTREG(sc, ATW_TOFS2);
458 PRINTREG(sc, ATW_CMDR);
459 PRINTREG(sc, ATW_PCIC);
460 PRINTREG(sc, ATW_PMCSR);
461 PRINTREG(sc, ATW_PAR0);
462 PRINTREG(sc, ATW_PAR1);
463 PRINTREG(sc, ATW_MAR0);
464 PRINTREG(sc, ATW_MAR1);
465 PRINTREG(sc, ATW_ATIMDA0);
466 PRINTREG(sc, ATW_ABDA1);
467 PRINTREG(sc, ATW_BSSID0);
468 PRINTREG(sc, ATW_TXLMT);
469 PRINTREG(sc, ATW_MIBCNT);
470 PRINTREG(sc, ATW_BCNT);
471 PRINTREG(sc, ATW_TSFTH);
472 PRINTREG(sc, ATW_TSC);
473 PRINTREG(sc, ATW_SYNRF);
474 PRINTREG(sc, ATW_BPLI);
475 PRINTREG(sc, ATW_CAP0);
476 PRINTREG(sc, ATW_CAP1);
477 PRINTREG(sc, ATW_RMD);
478 PRINTREG(sc, ATW_CFPP);
479 PRINTREG(sc, ATW_TOFS0);
480 PRINTREG(sc, ATW_TOFS1);
481 PRINTREG(sc, ATW_IFST);
482 PRINTREG(sc, ATW_RSPT);
483 PRINTREG(sc, ATW_TSFTL);
484 PRINTREG(sc, ATW_WEPCTL);
485 PRINTREG(sc, ATW_WESK);
486 PRINTREG(sc, ATW_WEPCNT);
487 PRINTREG(sc, ATW_MACTEST);
488 PRINTREG(sc, ATW_FER);
489 PRINTREG(sc, ATW_FEMR);
490 PRINTREG(sc, ATW_FPSR);
491 PRINTREG(sc, ATW_FFER);
492 #undef PRINTREG
493 }
494 #endif /* ATW_DEBUG */
495
496 /*
497 * Finish attaching an ADMtek ADM8211 MAC. Called by bus-specific front-end.
498 */
499 void
500 atw_attach(struct atw_softc *sc)
501 {
502 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
503 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
504 };
505 struct ieee80211com *ic = &sc->sc_ic;
506 struct ifnet *ifp = &ic->ic_if;
507 int country_code, error, i, nrate;
508 u_int32_t reg;
509 static const char *type_strings[] = {"Intersil (not supported)",
510 "RFMD", "Marvel (not supported)"};
511
512 sc->sc_txth = atw_txthresh_tab_lo;
513
514 SIMPLEQ_INIT(&sc->sc_txfreeq);
515 SIMPLEQ_INIT(&sc->sc_txdirtyq);
516
517 #ifdef ATW_DEBUG
518 atw_print_regs(sc, "atw_attach");
519 #endif /* ATW_DEBUG */
520
521 /*
522 * Allocate the control data structures, and create and load the
523 * DMA map for it.
524 */
525 if ((error = bus_dmamem_alloc(sc->sc_dmat,
526 sizeof(struct atw_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
527 1, &sc->sc_cdnseg, 0)) != 0) {
528 printf("%s: unable to allocate control data, error = %d\n",
529 sc->sc_dev.dv_xname, error);
530 goto fail_0;
531 }
532
533 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
534 sizeof(struct atw_control_data), (caddr_t *)&sc->sc_control_data,
535 BUS_DMA_COHERENT)) != 0) {
536 printf("%s: unable to map control data, error = %d\n",
537 sc->sc_dev.dv_xname, error);
538 goto fail_1;
539 }
540
541 if ((error = bus_dmamap_create(sc->sc_dmat,
542 sizeof(struct atw_control_data), 1,
543 sizeof(struct atw_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
544 printf("%s: unable to create control data DMA map, "
545 "error = %d\n", sc->sc_dev.dv_xname, error);
546 goto fail_2;
547 }
548
549 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
550 sc->sc_control_data, sizeof(struct atw_control_data), NULL,
551 0)) != 0) {
552 printf("%s: unable to load control data DMA map, error = %d\n",
553 sc->sc_dev.dv_xname, error);
554 goto fail_3;
555 }
556
557 /*
558 * Create the transmit buffer DMA maps.
559 */
560 sc->sc_ntxsegs = ATW_NTXSEGS;
561 for (i = 0; i < ATW_TXQUEUELEN; i++) {
562 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
563 sc->sc_ntxsegs, MCLBYTES, 0, 0,
564 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
565 printf("%s: unable to create tx DMA map %d, "
566 "error = %d\n", sc->sc_dev.dv_xname, i, error);
567 goto fail_4;
568 }
569 }
570
571 /*
572 * Create the receive buffer DMA maps.
573 */
574 for (i = 0; i < ATW_NRXDESC; i++) {
575 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
576 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
577 printf("%s: unable to create rx DMA map %d, "
578 "error = %d\n", sc->sc_dev.dv_xname, i, error);
579 goto fail_5;
580 }
581 }
582 for (i = 0; i < ATW_NRXDESC; i++) {
583 sc->sc_rxsoft[i].rxs_mbuf = NULL;
584 }
585
586 /* Reset the chip to a known state. */
587 atw_reset(sc);
588
589 if (atw_read_srom(sc) == -1)
590 return;
591
592 sc->sc_rftype = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CSR20],
593 ATW_SR_RFTYPE_MASK);
594
595 sc->sc_bbptype = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CSR20],
596 ATW_SR_BBPTYPE_MASK);
597
598 if (sc->sc_rftype > sizeof(type_strings)/sizeof(type_strings[0])) {
599 printf("%s: unknown RF\n", sc->sc_dev.dv_xname);
600 return;
601 }
602 if (sc->sc_bbptype > sizeof(type_strings)/sizeof(type_strings[0])) {
603 printf("%s: unknown BBP\n", sc->sc_dev.dv_xname);
604 return;
605 }
606
607 printf("%s: %s RF, %s BBP", sc->sc_dev.dv_xname,
608 type_strings[sc->sc_rftype], type_strings[sc->sc_bbptype]);
609
610 /* XXX There exists a Linux driver which seems to use RFType = 0 for
611 * MARVEL. My bug, or theirs?
612 */
613
614 reg = LSHIFT(sc->sc_rftype, ATW_SYNCTL_RFTYPE_MASK);
615
616 switch (sc->sc_rftype) {
617 case ATW_RFTYPE_INTERSIL:
618 reg |= ATW_SYNCTL_CS1;
619 break;
620 case ATW_RFTYPE_RFMD:
621 reg |= ATW_SYNCTL_CS0;
622 break;
623 case ATW_RFTYPE_MARVEL:
624 break;
625 }
626
627 sc->sc_synctl_rd = reg | ATW_SYNCTL_RD;
628 sc->sc_synctl_wr = reg | ATW_SYNCTL_WR;
629
630 reg = LSHIFT(sc->sc_bbptype, ATW_BBPCTL_TYPE_MASK);
631
632 switch (sc->sc_bbptype) {
633 case ATW_BBPTYPE_INTERSIL:
634 reg |= ATW_BBPCTL_TWI;
635 break;
636 case ATW_BBPTYPE_RFMD:
637 reg |= ATW_BBPCTL_RF3KADDR_ADDR | ATW_BBPCTL_NEGEDGE_DO |
638 ATW_BBPCTL_CCA_ACTLO;
639 break;
640 case ATW_BBPTYPE_MARVEL:
641 break;
642 case ATW_C_BBPTYPE_RFMD:
643 printf("%s: ADM8211C MAC/RFMD BBP not supported yet.\n",
644 sc->sc_dev.dv_xname);
645 break;
646 }
647
648 sc->sc_bbpctl_wr = reg | ATW_BBPCTL_WR;
649 sc->sc_bbpctl_rd = reg | ATW_BBPCTL_RD;
650
651 /*
652 * From this point forward, the attachment cannot fail. A failure
653 * before this point releases all resources that may have been
654 * allocated.
655 */
656 sc->sc_flags |= ATWF_ATTACHED /* | ATWF_RTSCTS */;
657
658 ATW_DPRINTF((" SROM MAC %04x%04x%04x",
659 htole16(sc->sc_srom[ATW_SR_MAC00]),
660 htole16(sc->sc_srom[ATW_SR_MAC01]),
661 htole16(sc->sc_srom[ATW_SR_MAC10])));
662
663 country_code = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CTRY_CR29],
664 ATW_SR_CTRY_MASK);
665
666 #define ADD_CHANNEL(_ic, _chan) do { \
667 _ic->ic_channels[_chan].ic_flags = IEEE80211_CHAN_B; \
668 _ic->ic_channels[_chan].ic_freq = \
669 ieee80211_ieee2mhz(_chan, _ic->ic_channels[_chan].ic_flags);\
670 } while (0)
671
672 /* Find available channels */
673 switch (country_code) {
674 case COUNTRY_MMK2: /* 1-14 */
675 ADD_CHANNEL(ic, 14);
676 /*FALLTHROUGH*/
677 case COUNTRY_ETSI: /* 1-13 */
678 for (i = 1; i <= 13; i++)
679 ADD_CHANNEL(ic, i);
680 break;
681 case COUNTRY_FCC: /* 1-11 */
682 case COUNTRY_IC: /* 1-11 */
683 for (i = 1; i <= 11; i++)
684 ADD_CHANNEL(ic, i);
685 break;
686 case COUNTRY_MMK: /* 14 */
687 ADD_CHANNEL(ic, 14);
688 break;
689 case COUNTRY_FRANCE: /* 10-13 */
690 for (i = 10; i <= 13; i++)
691 ADD_CHANNEL(ic, i);
692 break;
693 default: /* assume channels 10-11 */
694 case COUNTRY_SPAIN: /* 10-11 */
695 for (i = 10; i <= 11; i++)
696 ADD_CHANNEL(ic, i);
697 break;
698 }
699
700 /* Read the MAC address. */
701 reg = ATW_READ(sc, ATW_PAR0);
702 ic->ic_myaddr[0] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB0_MASK);
703 ic->ic_myaddr[1] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB1_MASK);
704 ic->ic_myaddr[2] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB2_MASK);
705 ic->ic_myaddr[3] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB3_MASK);
706 reg = ATW_READ(sc, ATW_PAR1);
707 ic->ic_myaddr[4] = MASK_AND_RSHIFT(reg, ATW_PAR1_PAB4_MASK);
708 ic->ic_myaddr[5] = MASK_AND_RSHIFT(reg, ATW_PAR1_PAB5_MASK);
709
710 if (IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
711 printf(" could not get mac address, attach failed\n");
712 return;
713 }
714
715 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
716
717 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
718 ifp->if_softc = sc;
719 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST |
720 IFF_NOTRAILERS;
721 ifp->if_ioctl = atw_ioctl;
722 ifp->if_start = atw_start;
723 ifp->if_watchdog = atw_watchdog;
724 ifp->if_init = atw_init;
725 ifp->if_stop = atw_stop;
726 IFQ_SET_READY(&ifp->if_snd);
727
728 ic->ic_phytype = IEEE80211_T_DS;
729 ic->ic_opmode = IEEE80211_M_STA;
730 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_IBSS |
731 IEEE80211_C_HOSTAP | IEEE80211_C_MONITOR | IEEE80211_C_WEP;
732
733 nrate = 0;
734 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 2;
735 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 4;
736 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 11;
737 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 22;
738 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
739
740 /*
741 * Call MI attach routines.
742 */
743
744 if_attach(ifp);
745 ieee80211_ifattach(ifp);
746
747 sc->sc_newstate = ic->ic_newstate;
748 ic->ic_newstate = atw_newstate;
749
750 sc->sc_recv_mgmt = ic->ic_recv_mgmt;
751 ic->ic_recv_mgmt = atw_recv_mgmt;
752
753 sc->sc_node_free = ic->ic_node_free;
754 ic->ic_node_free = atw_node_free;
755
756 sc->sc_node_alloc = ic->ic_node_alloc;
757 ic->ic_node_alloc = atw_node_alloc;
758
759 /* possibly we should fill in our own sc_send_prresp, since
760 * the ADM8211 is probably sending probe responses in ad hoc
761 * mode.
762 */
763
764 /* complete initialization */
765 ieee80211_media_init(ifp, atw_media_change, atw_media_status);
766 callout_init(&sc->sc_scan_ch);
767
768 #if NBPFILTER > 0
769 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
770 sizeof(struct ieee80211_frame) + 64, &sc->sc_radiobpf);
771 #endif
772
773 /*
774 * Make sure the interface is shutdown during reboot.
775 */
776 sc->sc_sdhook = shutdownhook_establish(atw_shutdown, sc);
777 if (sc->sc_sdhook == NULL)
778 printf("%s: WARNING: unable to establish shutdown hook\n",
779 sc->sc_dev.dv_xname);
780
781 /*
782 * Add a suspend hook to make sure we come back up after a
783 * resume.
784 */
785 sc->sc_powerhook = powerhook_establish(atw_power, sc);
786 if (sc->sc_powerhook == NULL)
787 printf("%s: WARNING: unable to establish power hook\n",
788 sc->sc_dev.dv_xname);
789
790 memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
791 sc->sc_rxtap.ar_ihdr.it_len = sizeof(sc->sc_rxtapu);
792 sc->sc_rxtap.ar_ihdr.it_present = ATW_RX_RADIOTAP_PRESENT;
793
794 memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
795 sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu);
796 sc->sc_txtap.at_ihdr.it_present = ATW_TX_RADIOTAP_PRESENT;
797
798 return;
799
800 /*
801 * Free any resources we've allocated during the failed attach
802 * attempt. Do this in reverse order and fall through.
803 */
804 fail_5:
805 for (i = 0; i < ATW_NRXDESC; i++) {
806 if (sc->sc_rxsoft[i].rxs_dmamap == NULL)
807 continue;
808 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxsoft[i].rxs_dmamap);
809 }
810 fail_4:
811 for (i = 0; i < ATW_TXQUEUELEN; i++) {
812 if (sc->sc_txsoft[i].txs_dmamap == NULL)
813 continue;
814 bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsoft[i].txs_dmamap);
815 }
816 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
817 fail_3:
818 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
819 fail_2:
820 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
821 sizeof(struct atw_control_data));
822 fail_1:
823 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
824 fail_0:
825 return;
826 }
827
828 static struct ieee80211_node *
829 atw_node_alloc(struct ieee80211com *ic)
830 {
831 struct atw_softc *sc = (struct atw_softc *)ic->ic_if.if_softc;
832 struct ieee80211_node *ni = (*sc->sc_node_alloc)(ic);
833
834 DPRINTF(sc, ("%s: alloc node %p\n", sc->sc_dev.dv_xname, ni));
835 return ni;
836 }
837
838 static void
839 atw_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
840 {
841 struct atw_softc *sc = (struct atw_softc *)ic->ic_if.if_softc;
842
843 DPRINTF(sc, ("%s: freeing node %p %s\n", sc->sc_dev.dv_xname, ni,
844 ether_sprintf(ni->ni_bssid)));
845 (*sc->sc_node_free)(ic, ni);
846 }
847
848 /*
849 * atw_reset:
850 *
851 * Perform a soft reset on the ADM8211.
852 */
853 void
854 atw_reset(struct atw_softc *sc)
855 {
856 int i;
857
858 ATW_WRITE(sc, ATW_PAR, ATW_PAR_SWR);
859
860 for (i = 0; i < 10000; i++) {
861 if (ATW_ISSET(sc, ATW_PAR, ATW_PAR_SWR) == 0)
862 break;
863 DELAY(1);
864 }
865
866 DPRINTF2(sc, ("%s: atw_reset %d iterations\n", sc->sc_dev.dv_xname, i));
867
868 if (ATW_ISSET(sc, ATW_PAR, ATW_PAR_SWR))
869 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
870
871 /* Turn off maximum power saving. */
872 ATW_CLR(sc, ATW_FRCTL, ATW_FRCTL_MAXPSP);
873
874 /* Recall EEPROM. */
875 ATW_SET(sc, ATW_TEST0, ATW_TEST0_EPRLD);
876
877 DELAY(10 * 1000);
878
879 /* A reset seems to affect the SRAM contents, so put them into
880 * a known state.
881 */
882 atw_clear_sram(sc);
883
884 memset(sc->sc_bssid, 0, sizeof(sc->sc_bssid));
885 }
886
887 static void
888 atw_clear_sram(struct atw_softc *sc)
889 {
890 memset(sc->sc_sram, 0, sizeof(sc->sc_sram));
891 /* XXX not for revision 0x20. */
892 atw_write_sram(sc, 0, sc->sc_sram, sizeof(sc->sc_sram));
893 }
894
895 /* TBD atw_init
896 *
897 * set MAC based on ic->ic_bss->myaddr
898 * write WEP keys
899 * set TX rate
900 */
901
902 /*
903 * atw_init: [ ifnet interface function ]
904 *
905 * Initialize the interface. Must be called at splnet().
906 */
907 int
908 atw_init(struct ifnet *ifp)
909 {
910 struct atw_softc *sc = ifp->if_softc;
911 struct ieee80211com *ic = &sc->sc_ic;
912 struct atw_txsoft *txs;
913 struct atw_rxsoft *rxs;
914 u_int32_t reg;
915 int i, error = 0;
916
917 if ((error = atw_enable(sc)) != 0)
918 goto out;
919
920 /*
921 * Cancel any pending I/O. This also resets.
922 */
923 atw_stop(ifp, 0);
924
925 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
926 DPRINTF(sc, ("%s: channel %d freq %d flags 0x%04x\n",
927 __func__, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
928 ic->ic_bss->ni_chan->ic_freq, ic->ic_bss->ni_chan->ic_flags));
929
930 /* Turn off APM??? (A binary-only driver does this.)
931 *
932 * Set Rx store-and-forward mode.
933 */
934 reg = ATW_READ(sc, ATW_CMDR);
935 reg &= ~ATW_CMDR_APM;
936 reg &= ~ATW_CMDR_DRT_MASK;
937 reg |= ATW_CMDR_RTE | LSHIFT(0x2, ATW_CMDR_DRT_MASK);
938
939 ATW_WRITE(sc, ATW_CMDR, reg);
940
941 /* Set data rate for PLCP Signal field, 1Mbps = 10 x 100Kb/s.
942 *
943 * XXX a binary-only driver sets a different service field than
944 * 0. why?
945 */
946 reg = ATW_READ(sc, ATW_PLCPHD);
947 reg &= ~(ATW_PLCPHD_SERVICE_MASK|ATW_PLCPHD_SIGNAL_MASK);
948 reg |= LSHIFT(10, ATW_PLCPHD_SIGNAL_MASK) |
949 LSHIFT(0xb0, ATW_PLCPHD_SERVICE_MASK);
950 ATW_WRITE(sc, ATW_PLCPHD, reg);
951
952 /* XXX this magic can probably be figured out from the RFMD docs */
953 reg = LSHIFT(4, ATW_TOFS2_PWR1UP_MASK) | /* 8 ms = 4 * 2 ms */
954 LSHIFT(13, ATW_TOFS2_PWR0PAPE_MASK) | /* 13 us */
955 LSHIFT(8, ATW_TOFS2_PWR1PAPE_MASK) | /* 8 us */
956 LSHIFT(5, ATW_TOFS2_PWR0TRSW_MASK) | /* 5 us */
957 LSHIFT(12, ATW_TOFS2_PWR1TRSW_MASK) | /* 12 us */
958 LSHIFT(13, ATW_TOFS2_PWR0PE2_MASK) | /* 13 us */
959 LSHIFT(4, ATW_TOFS2_PWR1PE2_MASK) | /* 4 us */
960 LSHIFT(5, ATW_TOFS2_PWR0TXPE_MASK); /* 5 us */
961 ATW_WRITE(sc, ATW_TOFS2, reg);
962
963 ATW_WRITE(sc, ATW_TXLMT, LSHIFT(512, ATW_TXLMT_MTMLT_MASK) |
964 LSHIFT(224, ATW_TXLMT_SRTYLIM_MASK));
965
966 /* XXX this resets an Intersil RF front-end? */
967 /* TBD condition on Intersil RFType? */
968 ATW_WRITE(sc, ATW_SYNRF, ATW_SYNRF_INTERSIL_EN);
969 DELAY(10 * 1000);
970 ATW_WRITE(sc, ATW_SYNRF, 0);
971 DELAY(5 * 1000);
972
973 /* 16 TU max duration for contention-free period */
974 reg = ATW_READ(sc, ATW_CFPP) & ~ATW_CFPP_CFPMD;
975 ATW_WRITE(sc, ATW_CFPP, reg | LSHIFT(16, ATW_CFPP_CFPMD));
976
977 /* XXX I guess that the Cardbus clock is 22MHz?
978 * I am assuming that the role of ATW_TOFS0_USCNT is
979 * to divide the bus clock to get a 1MHz clock---the datasheet is not
980 * very clear on this point. It says in the datasheet that it is
981 * possible for the ADM8211 to accomodate bus speeds between 22MHz
982 * and 33MHz; maybe this is the way? I see a binary-only driver write
983 * these values. These values are also the power-on default.
984 */
985 ATW_WRITE(sc, ATW_TOFS0,
986 LSHIFT(22, ATW_TOFS0_USCNT_MASK) |
987 ATW_TOFS0_TUCNT_MASK /* set all bits in TUCNT */);
988
989 /* Initialize interframe spacing. EIFS=0x64 is used by a binary-only
990 * driver. Go figure.
991 */
992 reg = LSHIFT(IEEE80211_DUR_DS_SLOT, ATW_IFST_SLOT_MASK) |
993 LSHIFT(22 * IEEE80211_DUR_DS_SIFS /* # of 22MHz cycles */,
994 ATW_IFST_SIFS_MASK) |
995 LSHIFT(IEEE80211_DUR_DS_DIFS, ATW_IFST_DIFS_MASK) |
996 LSHIFT(0x64 /* IEEE80211_DUR_DS_EIFS */, ATW_IFST_EIFS_MASK);
997
998 ATW_WRITE(sc, ATW_IFST, reg);
999
1000 /* XXX More magic. Might relate to ACK timing. */
1001 ATW_WRITE(sc, ATW_RSPT, LSHIFT(0xffff, ATW_RSPT_MART_MASK) |
1002 LSHIFT(0xff, ATW_RSPT_MIRT_MASK));
1003
1004 /* Set up the MMI read/write addresses for the BBP.
1005 *
1006 * TBD find out the Marvel settings.
1007 */
1008 switch (sc->sc_bbptype) {
1009 case ATW_BBPTYPE_INTERSIL:
1010 ATW_WRITE(sc, ATW_MMIWADDR, ATW_MMIWADDR_INTERSIL);
1011 ATW_WRITE(sc, ATW_MMIRADDR1, ATW_MMIRADDR1_INTERSIL);
1012 ATW_WRITE(sc, ATW_MMIRADDR2, ATW_MMIRADDR2_INTERSIL);
1013 break;
1014 case ATW_BBPTYPE_MARVEL:
1015 break;
1016 case ATW_BBPTYPE_RFMD:
1017 ATW_WRITE(sc, ATW_MMIWADDR, ATW_MMIWADDR_RFMD);
1018 ATW_WRITE(sc, ATW_MMIRADDR1, ATW_MMIRADDR1_RFMD);
1019 ATW_WRITE(sc, ATW_MMIRADDR2, ATW_MMIRADDR2_RFMD);
1020 default:
1021 break;
1022 }
1023
1024 sc->sc_wepctl = 0;
1025 ATW_WRITE(sc, ATW_MACTEST, ATW_MACTEST_MMI_USETXCLK);
1026
1027 if ((error = atw_rf3000_init(sc)) != 0)
1028 goto out;
1029
1030 /*
1031 * Initialize the PCI Access Register.
1032 */
1033 sc->sc_busmode = ATW_PAR_BAR; /* XXX what is this? */
1034
1035 /*
1036 * If we're allowed to do so, use Memory Read Line
1037 * and Memory Read Multiple.
1038 *
1039 * XXX Should we use Memory Write and Invalidate?
1040 */
1041 if (sc->sc_flags & ATWF_MRL)
1042 sc->sc_busmode |= ATW_PAR_MRLE;
1043 if (sc->sc_flags & ATWF_MRM)
1044 sc->sc_busmode |= ATW_PAR_MRME;
1045 if (sc->sc_flags & ATWF_MWI)
1046 sc->sc_busmode |= ATW_PAR_MWIE;
1047 if (sc->sc_maxburst == 0)
1048 sc->sc_maxburst = 8; /* ADM8211 default */
1049
1050 switch (sc->sc_cacheline) {
1051 default:
1052 /* Use burst length. */
1053 break;
1054 case 8:
1055 sc->sc_busmode |= ATW_PAR_CAL_8DW;
1056 break;
1057 case 16:
1058 sc->sc_busmode |= ATW_PAR_CAL_16DW;
1059 break;
1060 case 32:
1061 sc->sc_busmode |= ATW_PAR_CAL_32DW;
1062 break;
1063 }
1064 switch (sc->sc_maxburst) {
1065 case 1:
1066 sc->sc_busmode |= ATW_PAR_PBL_1DW;
1067 break;
1068 case 2:
1069 sc->sc_busmode |= ATW_PAR_PBL_2DW;
1070 break;
1071 case 4:
1072 sc->sc_busmode |= ATW_PAR_PBL_4DW;
1073 break;
1074 case 8:
1075 sc->sc_busmode |= ATW_PAR_PBL_8DW;
1076 break;
1077 case 16:
1078 sc->sc_busmode |= ATW_PAR_PBL_16DW;
1079 break;
1080 case 32:
1081 sc->sc_busmode |= ATW_PAR_PBL_32DW;
1082 break;
1083 default:
1084 sc->sc_busmode |= ATW_PAR_PBL_8DW;
1085 break;
1086 }
1087
1088 ATW_WRITE(sc, ATW_PAR, sc->sc_busmode);
1089 DPRINTF(sc, ("%s: ATW_PAR %08x busmode %08x\n", sc->sc_dev.dv_xname,
1090 ATW_READ(sc, ATW_PAR), sc->sc_busmode));
1091
1092 /*
1093 * Initialize the OPMODE register. We don't write it until
1094 * we're ready to begin the transmit and receive processes.
1095 */
1096 sc->sc_opmode = ATW_NAR_SR | ATW_NAR_ST |
1097 sc->sc_txth[sc->sc_txthresh].txth_opmode;
1098
1099 /*
1100 * Initialize the transmit descriptor ring.
1101 */
1102 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1103 for (i = 0; i < ATW_NTXDESC; i++) {
1104 sc->sc_txdescs[i].at_ctl = 0;
1105 /* no transmit chaining */
1106 sc->sc_txdescs[i].at_flags = 0 /* ATW_TXFLAG_TCH */;
1107 sc->sc_txdescs[i].at_buf2 =
1108 htole32(ATW_CDTXADDR(sc, ATW_NEXTTX(i)));
1109 }
1110 /* use ring mode */
1111 sc->sc_txdescs[ATW_NTXDESC - 1].at_flags |= htole32(ATW_TXFLAG_TER);
1112 ATW_CDTXSYNC(sc, 0, ATW_NTXDESC,
1113 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1114 sc->sc_txfree = ATW_NTXDESC;
1115 sc->sc_txnext = 0;
1116
1117 /*
1118 * Initialize the transmit job descriptors.
1119 */
1120 SIMPLEQ_INIT(&sc->sc_txfreeq);
1121 SIMPLEQ_INIT(&sc->sc_txdirtyq);
1122 for (i = 0; i < ATW_TXQUEUELEN; i++) {
1123 txs = &sc->sc_txsoft[i];
1124 txs->txs_mbuf = NULL;
1125 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1126 }
1127
1128 /*
1129 * Initialize the receive descriptor and receive job
1130 * descriptor rings.
1131 */
1132 for (i = 0; i < ATW_NRXDESC; i++) {
1133 rxs = &sc->sc_rxsoft[i];
1134 if (rxs->rxs_mbuf == NULL) {
1135 if ((error = atw_add_rxbuf(sc, i)) != 0) {
1136 printf("%s: unable to allocate or map rx "
1137 "buffer %d, error = %d\n",
1138 sc->sc_dev.dv_xname, i, error);
1139 /*
1140 * XXX Should attempt to run with fewer receive
1141 * XXX buffers instead of just failing.
1142 */
1143 atw_rxdrain(sc);
1144 goto out;
1145 }
1146 } else
1147 ATW_INIT_RXDESC(sc, i);
1148 }
1149 sc->sc_rxptr = 0;
1150
1151 /* disable all wake-up events */
1152 ATW_CLR(sc, ATW_WCSR, ATW_WCSR_WP1E|ATW_WCSR_WP2E|ATW_WCSR_WP3E|
1153 ATW_WCSR_WP4E|ATW_WCSR_WP5E|ATW_WCSR_TSFTWE|
1154 ATW_WCSR_TIMWE|ATW_WCSR_ATIMWE|ATW_WCSR_KEYWE|
1155 ATW_WCSR_WFRE|ATW_WCSR_MPRE|ATW_WCSR_LSOE);
1156
1157 /* ack all wake-up events */
1158 ATW_SET(sc, ATW_WCSR, 0);
1159
1160 /*
1161 * Initialize the interrupt mask and enable interrupts.
1162 */
1163 /* normal interrupts */
1164 sc->sc_inten = ATW_INTR_TCI | ATW_INTR_TDU | ATW_INTR_RCI |
1165 ATW_INTR_NISS | ATW_INTR_LINKON | ATW_INTR_BCNTC;
1166
1167 /* abnormal interrupts */
1168 sc->sc_inten |= ATW_INTR_TPS | ATW_INTR_TLT | ATW_INTR_TRT |
1169 ATW_INTR_TUF | ATW_INTR_RDU | ATW_INTR_RPS | ATW_INTR_AISS |
1170 ATW_INTR_FBE | ATW_INTR_LINKOFF | ATW_INTR_TSFTF | ATW_INTR_TSCZ;
1171
1172 sc->sc_linkint_mask = ATW_INTR_LINKON | ATW_INTR_LINKOFF |
1173 ATW_INTR_BCNTC | ATW_INTR_TSFTF | ATW_INTR_TSCZ;
1174 sc->sc_rxint_mask = ATW_INTR_RCI | ATW_INTR_RDU;
1175 sc->sc_txint_mask = ATW_INTR_TCI | ATW_INTR_TUF | ATW_INTR_TLT |
1176 ATW_INTR_TRT;
1177
1178 sc->sc_linkint_mask &= sc->sc_inten;
1179 sc->sc_rxint_mask &= sc->sc_inten;
1180 sc->sc_txint_mask &= sc->sc_inten;
1181
1182 ATW_WRITE(sc, ATW_IER, sc->sc_inten);
1183 ATW_WRITE(sc, ATW_STSR, 0xffffffff);
1184
1185 DPRINTF(sc, ("%s: ATW_IER %08x, inten %08x\n",
1186 sc->sc_dev.dv_xname, ATW_READ(sc, ATW_IER), sc->sc_inten));
1187
1188 /*
1189 * Give the transmit and receive rings to the ADM8211.
1190 */
1191 ATW_WRITE(sc, ATW_TDBD, ATW_CDTXADDR(sc, sc->sc_txnext));
1192 ATW_WRITE(sc, ATW_RDB, ATW_CDRXADDR(sc, sc->sc_rxptr));
1193
1194 /* common 802.11 configuration */
1195 ic->ic_flags &= ~IEEE80211_F_IBSSON;
1196 switch (ic->ic_opmode) {
1197 case IEEE80211_M_STA:
1198 break;
1199 case IEEE80211_M_AHDEMO: /* XXX */
1200 case IEEE80211_M_IBSS:
1201 ic->ic_flags |= IEEE80211_F_IBSSON;
1202 /*FALLTHROUGH*/
1203 case IEEE80211_M_HOSTAP: /* XXX */
1204 break;
1205 case IEEE80211_M_MONITOR: /* XXX */
1206 break;
1207 }
1208
1209 atw_start_beacon(sc, 0);
1210
1211 switch (ic->ic_opmode) {
1212 case IEEE80211_M_AHDEMO:
1213 case IEEE80211_M_HOSTAP:
1214 ic->ic_bss->ni_intval = ic->ic_lintval;
1215 ic->ic_bss->ni_rssi = 0;
1216 ic->ic_bss->ni_rstamp = 0;
1217 break;
1218 default: /* XXX */
1219 break;
1220 }
1221
1222 atw_write_ssid(sc);
1223 atw_write_sup_rates(sc);
1224 if (ic->ic_caps & IEEE80211_C_WEP)
1225 atw_write_wep(sc);
1226
1227 /*
1228 * Set the receive filter. This will start the transmit and
1229 * receive processes.
1230 */
1231 atw_filter_setup(sc);
1232
1233 /*
1234 * Start the receive process.
1235 */
1236 ATW_WRITE(sc, ATW_RDR, 0x1);
1237
1238 /*
1239 * Note that the interface is now running.
1240 */
1241 ifp->if_flags |= IFF_RUNNING;
1242 ifp->if_flags &= ~IFF_OACTIVE;
1243 ic->ic_state = IEEE80211_S_INIT;
1244
1245 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1246 error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1247 else
1248 error = ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1249 out:
1250 if (error) {
1251 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1252 ifp->if_timer = 0;
1253 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1254 }
1255 #ifdef ATW_DEBUG
1256 atw_print_regs(sc, "end of init");
1257 #endif /* ATW_DEBUG */
1258
1259 return (error);
1260 }
1261
1262 /* enable == 1: host control of RF3000/Si4126 through ATW_SYNCTL.
1263 * 0: MAC control of RF3000/Si4126.
1264 *
1265 * Applies power, or selects RF front-end? Sets reset condition.
1266 *
1267 * TBD support non-RFMD BBP, non-SiLabs synth.
1268 */
1269 static void
1270 atw_bbp_io_enable(struct atw_softc *sc, int enable)
1271 {
1272 if (enable) {
1273 ATW_WRITE(sc, ATW_SYNRF,
1274 ATW_SYNRF_SELRF|ATW_SYNRF_PE1|ATW_SYNRF_PHYRST);
1275 DELAY(atw_bbp_io_enable_delay);
1276 } else {
1277 ATW_WRITE(sc, ATW_SYNRF, 0);
1278 DELAY(atw_bbp_io_disable_delay); /* shorter for some reason */
1279 }
1280 }
1281
1282 static int
1283 atw_tune(struct atw_softc *sc)
1284 {
1285 int rc;
1286 u_int chan;
1287 struct ieee80211com *ic = &sc->sc_ic;
1288
1289 chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1290 if (chan == IEEE80211_CHAN_ANY)
1291 panic("%s: chan == IEEE80211_CHAN_ANY\n", __func__);
1292
1293 if (chan == sc->sc_cur_chan)
1294 return 0;
1295
1296 DPRINTF(sc, ("%s: chan %d -> %d\n", sc->sc_dev.dv_xname,
1297 sc->sc_cur_chan, chan));
1298
1299 atw_idle(sc, ATW_NAR_SR|ATW_NAR_ST);
1300
1301 atw_si4126_tune(sc, chan);
1302 if ((rc = atw_rf3000_tune(sc, chan)) != 0)
1303 printf("%s: failed to tune channel %d\n", sc->sc_dev.dv_xname,
1304 chan);
1305
1306 ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
1307 DELAY(20 * 1000);
1308 ATW_WRITE(sc, ATW_RDR, 0x1);
1309
1310 if (rc == 0)
1311 sc->sc_cur_chan = chan;
1312
1313 return rc;
1314 }
1315
1316 #ifdef ATW_SYNDEBUG
1317 static void
1318 atw_si4126_print(struct atw_softc *sc)
1319 {
1320 struct ifnet *ifp = &sc->sc_ic.ic_if;
1321 u_int addr, val;
1322
1323 if (atw_debug < 3 || (ifp->if_flags & IFF_DEBUG) == 0)
1324 return;
1325
1326 for (addr = 0; addr <= 8; addr++) {
1327 printf("%s: synth[%d] = ", sc->sc_dev.dv_xname, addr);
1328 if (atw_si4126_read(sc, addr, &val) == 0) {
1329 printf("<unknown> (quitting print-out)\n");
1330 break;
1331 }
1332 printf("%05x\n", val);
1333 }
1334 }
1335 #endif /* ATW_SYNDEBUG */
1336
1337 /* Tune to channel chan by adjusting the Si4126 RF/IF synthesizer.
1338 *
1339 * The RF/IF synthesizer produces two reference frequencies for
1340 * the RF2948B transceiver. The first frequency the RF2948B requires
1341 * is two times the so-called "intermediate frequency" (IF). Since
1342 * a SAW filter on the radio fixes the IF at 374MHz, I program the
1343 * Si4126 to generate IF LO = 374MHz x 2 = 748MHz. The second
1344 * frequency required by the transceiver is the radio frequency
1345 * (RF). This is a superheterodyne transceiver; for f(chan) the
1346 * center frequency of the channel we are tuning, RF = f(chan) -
1347 * IF.
1348 *
1349 * XXX I am told by SiLabs that the Si4126 will accept a broader range
1350 * of XIN than the 2-25MHz mentioned by the datasheet, even *without*
1351 * XINDIV2 = 1. I've tried this (it is necessary to double R) and it
1352 * works, but I have still programmed for XINDIV2 = 1 to be safe.
1353 */
1354 static void
1355 atw_si4126_tune(struct atw_softc *sc, u_int chan)
1356 {
1357 u_int mhz;
1358 u_int R;
1359 u_int32_t gpio;
1360 u_int16_t gain;
1361
1362 #ifdef ATW_SYNDEBUG
1363 atw_si4126_print(sc);
1364 #endif /* ATW_SYNDEBUG */
1365
1366 if (chan == 14)
1367 mhz = 2484;
1368 else
1369 mhz = 2412 + 5 * (chan - 1);
1370
1371 /* Tune IF to 748MHz to suit the IF LO input of the
1372 * RF2494B, which is 2 x IF. No need to set an IF divider
1373 * because an IF in 526MHz - 952MHz is allowed.
1374 *
1375 * XIN is 44.000MHz, so divide it by two to get allowable
1376 * range of 2-25MHz. SiLabs tells me that this is not
1377 * strictly necessary.
1378 */
1379
1380 if (atw_xindiv2)
1381 R = 44;
1382 else
1383 R = 88;
1384
1385 /* Power-up RF, IF synthesizers. */
1386 atw_si4126_write(sc, SI4126_POWER,
1387 SI4126_POWER_PDIB|SI4126_POWER_PDRB);
1388
1389 /* set LPWR, too? */
1390 atw_si4126_write(sc, SI4126_MAIN,
1391 (atw_xindiv2) ? SI4126_MAIN_XINDIV2 : 0);
1392
1393 /* Set the phase-locked loop gain. If RF2 N > 2047, then
1394 * set KP2 to 1.
1395 *
1396 * REFDIF This is different from the reference driver, which
1397 * always sets SI4126_GAIN to 0.
1398 */
1399 gain = LSHIFT(((mhz - 374) > 2047) ? 1 : 0, SI4126_GAIN_KP2_MASK);
1400
1401 atw_si4126_write(sc, SI4126_GAIN, gain);
1402
1403 /* XIN = 44MHz.
1404 *
1405 * If XINDIV2 = 1, IF = N/(2 * R) * XIN. I choose N = 1496,
1406 * R = 44 so that 1496/(2 * 44) * 44MHz = 748MHz.
1407 *
1408 * If XINDIV2 = 0, IF = N/R * XIN. I choose N = 1496, R = 88
1409 * so that 1496/88 * 44MHz = 748MHz.
1410 */
1411 atw_si4126_write(sc, SI4126_IFN, 1496);
1412
1413 atw_si4126_write(sc, SI4126_IFR, R);
1414
1415 #ifndef ATW_REFSLAVE
1416 /* Set RF1 arbitrarily. DO NOT configure RF1 after RF2, because
1417 * then RF1 becomes the active RF synthesizer, even on the Si4126,
1418 * which has no RF1!
1419 */
1420 atw_si4126_write(sc, SI4126_RF1R, R);
1421
1422 atw_si4126_write(sc, SI4126_RF1N, mhz - 374);
1423 #endif
1424
1425 /* N/R * XIN = RF. XIN = 44MHz. We desire RF = mhz - IF,
1426 * where IF = 374MHz. Let's divide XIN to 1MHz. So R = 44.
1427 * Now let's multiply it to mhz. So mhz - IF = N.
1428 */
1429 atw_si4126_write(sc, SI4126_RF2R, R);
1430
1431 atw_si4126_write(sc, SI4126_RF2N, mhz - 374);
1432
1433 /* wait 100us from power-up for RF, IF to settle */
1434 DELAY(100);
1435
1436 gpio = ATW_READ(sc, ATW_GPIO);
1437 gpio &= ~(ATW_GPIO_EN_MASK|ATW_GPIO_O_MASK|ATW_GPIO_I_MASK);
1438 gpio |= LSHIFT(1, ATW_GPIO_EN_MASK);
1439
1440 if ((sc->sc_if.if_flags & IFF_LINK1) != 0 && chan != 14) {
1441 /* Set a Prism RF front-end to a special mode for channel 14?
1442 *
1443 * Apparently the SMC2635W needs this, although I don't think
1444 * it has a Prism RF.
1445 */
1446 gpio |= LSHIFT(1, ATW_GPIO_O_MASK);
1447 }
1448 ATW_WRITE(sc, ATW_GPIO, gpio);
1449
1450 #ifdef ATW_SYNDEBUG
1451 atw_si4126_print(sc);
1452 #endif /* ATW_SYNDEBUG */
1453 }
1454
1455 /* Baseline initialization of RF3000 BBP: set CCA mode and enable antenna
1456 * diversity.
1457 *
1458 * !!!
1459 * !!! Call this w/ Tx/Rx suspended, atw_idle(, ATW_NAR_ST|ATW_NAR_SR).
1460 * !!!
1461 */
1462 static int
1463 atw_rf3000_init(struct atw_softc *sc)
1464 {
1465 int rc = 0;
1466
1467 atw_bbp_io_enable(sc, 1);
1468
1469 /* CCA is acquisition sensitive */
1470 rc = atw_rf3000_write(sc, RF3000_CCACTL,
1471 LSHIFT(RF3000_CCACTL_MODE_BOTH, RF3000_CCACTL_MODE_MASK));
1472
1473 if (rc != 0)
1474 goto out;
1475
1476 /* enable diversity */
1477 rc = atw_rf3000_write(sc, RF3000_DIVCTL, RF3000_DIVCTL_ENABLE);
1478
1479 if (rc != 0)
1480 goto out;
1481
1482 /* sensible setting from a binary-only driver */
1483 rc = atw_rf3000_write(sc, RF3000_GAINCTL,
1484 LSHIFT(0x1d, RF3000_GAINCTL_TXVGC_MASK));
1485
1486 if (rc != 0)
1487 goto out;
1488
1489 /* magic from a binary-only driver */
1490 rc = atw_rf3000_write(sc, RF3000_LOGAINCAL,
1491 LSHIFT(0x38, RF3000_LOGAINCAL_CAL_MASK));
1492
1493 if (rc != 0)
1494 goto out;
1495
1496 rc = atw_rf3000_write(sc, RF3000_HIGAINCAL, RF3000_HIGAINCAL_DSSSPAD);
1497
1498 if (rc != 0)
1499 goto out;
1500
1501 /* XXX Reference driver remarks that Abocom sets this to 50.
1502 * Meaning 0x50, I think.... 50 = 0x32, which would set a bit
1503 * in the "reserved" area of register RF3000_OPTIONS1.
1504 *
1505 * EEPROMs for the ADM8211B contain a setting for this register.
1506 */
1507 rc = atw_rf3000_write(sc, RF3000_OPTIONS1, 0x0);
1508
1509 if (rc != 0)
1510 goto out;
1511
1512 rc = atw_rf3000_write(sc, RF3000_OPTIONS2, RF3000_OPTIONS2_LNAGS_DELAY);
1513
1514 if (rc != 0)
1515 goto out;
1516
1517 out:
1518 atw_bbp_io_enable(sc, 0);
1519 return rc;
1520 }
1521
1522 #ifdef ATW_BBPDEBUG
1523 static void
1524 atw_rf3000_print(struct atw_softc *sc)
1525 {
1526 struct ifnet *ifp = &sc->sc_ic.ic_if;
1527 u_int addr, val;
1528
1529 if (atw_debug < 3 || (ifp->if_flags & IFF_DEBUG) == 0)
1530 return;
1531
1532 for (addr = 0x01; addr <= 0x15; addr++) {
1533 printf("%s: bbp[%d] = \n", sc->sc_dev.dv_xname, addr);
1534 if (atw_rf3000_read(sc, addr, &val) != 0) {
1535 printf("<unknown> (quitting print-out)\n");
1536 break;
1537 }
1538 printf("%08x\n", val);
1539 }
1540 }
1541 #endif /* ATW_BBPDEBUG */
1542
1543 /* Set the power settings on the BBP for channel `chan'. */
1544 static int
1545 atw_rf3000_tune(struct atw_softc *sc, u_int chan)
1546 {
1547 int rc = 0;
1548 u_int32_t reg;
1549 u_int16_t txpower, lpf_cutoff, lna_gs_thresh;
1550
1551 txpower = sc->sc_srom[ATW_SR_TXPOWER(chan)];
1552 lpf_cutoff = sc->sc_srom[ATW_SR_LPF_CUTOFF(chan)];
1553 lna_gs_thresh = sc->sc_srom[ATW_SR_LNA_GS_THRESH(chan)];
1554
1555 /* odd channels: LSB, even channels: MSB */
1556 if (chan % 2 == 1) {
1557 txpower &= 0xFF;
1558 lpf_cutoff &= 0xFF;
1559 lna_gs_thresh &= 0xFF;
1560 } else {
1561 txpower >>= 8;
1562 lpf_cutoff >>= 8;
1563 lna_gs_thresh >>= 8;
1564 }
1565
1566 #ifdef ATW_BBPDEBUG
1567 atw_rf3000_print(sc);
1568 #endif /* ATW_BBPDEBUG */
1569
1570 DPRINTF(sc, ("%s: chan %d txpower %02x, lpf_cutoff %02x, "
1571 "lna_gs_thresh %02x\n",
1572 sc->sc_dev.dv_xname, chan, txpower, lpf_cutoff, lna_gs_thresh));
1573
1574 atw_bbp_io_enable(sc, 1);
1575
1576 if ((rc = atw_rf3000_write(sc, RF3000_GAINCTL,
1577 LSHIFT(txpower, RF3000_GAINCTL_TXVGC_MASK))) != 0)
1578 goto out;
1579
1580 if ((rc = atw_rf3000_write(sc, RF3000_LOGAINCAL, lpf_cutoff)) != 0)
1581 goto out;
1582
1583 if ((rc = atw_rf3000_write(sc, RF3000_HIGAINCAL, lna_gs_thresh)) != 0)
1584 goto out;
1585
1586 rc = atw_rf3000_write(sc, RF3000_OPTIONS1, 0x0);
1587
1588 if (rc != 0)
1589 goto out;
1590
1591 rc = atw_rf3000_write(sc, RF3000_OPTIONS2, RF3000_OPTIONS2_LNAGS_DELAY);
1592
1593 if (rc != 0)
1594 goto out;
1595
1596 #ifdef ATW_BBPDEBUG
1597 atw_rf3000_print(sc);
1598 #endif /* ATW_BBPDEBUG */
1599
1600 out:
1601 atw_bbp_io_enable(sc, 0);
1602
1603 /* set beacon, rts, atim transmit power */
1604 reg = ATW_READ(sc, ATW_PLCPHD);
1605 reg &= ~ATW_PLCPHD_SERVICE_MASK;
1606 reg |= LSHIFT(LSHIFT(txpower, RF3000_GAINCTL_TXVGC_MASK),
1607 ATW_PLCPHD_SERVICE_MASK);
1608 ATW_WRITE(sc, ATW_PLCPHD, reg);
1609 DELAY(2 * 1000);
1610
1611 return rc;
1612 }
1613
1614 /* Write a register on the RF3000 baseband processor using the
1615 * registers provided by the ADM8211 for this purpose.
1616 *
1617 * Return 0 on success.
1618 */
1619 static int
1620 atw_rf3000_write(struct atw_softc *sc, u_int addr, u_int val)
1621 {
1622 u_int32_t reg;
1623 int i;
1624
1625 reg = sc->sc_bbpctl_wr |
1626 LSHIFT(val & 0xff, ATW_BBPCTL_DATA_MASK) |
1627 LSHIFT(addr & 0x7f, ATW_BBPCTL_ADDR_MASK);
1628
1629 for (i = 10; --i >= 0; ) {
1630 ATW_WRITE(sc, ATW_BBPCTL, reg);
1631 DELAY(2000);
1632 if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_WR) == 0)
1633 break;
1634 }
1635
1636 if (i < 0) {
1637 printf("%s: BBPCTL still busy\n", sc->sc_dev.dv_xname);
1638 return ETIMEDOUT;
1639 }
1640 return 0;
1641 }
1642
1643 /* Read a register on the RF3000 baseband processor using the registers
1644 * the ADM8211 provides for this purpose.
1645 *
1646 * The 7-bit register address is addr. Record the 8-bit data in the register
1647 * in *val.
1648 *
1649 * Return 0 on success.
1650 *
1651 * XXX This does not seem to work. The ADM8211 must require more or
1652 * different magic to read the chip than to write it. Possibly some
1653 * of the magic I have derived from a binary-only driver concerns
1654 * the "chip address" (see the RF3000 manual).
1655 */
1656 #ifdef ATW_BBPDEBUG
1657 static int
1658 atw_rf3000_read(struct atw_softc *sc, u_int addr, u_int *val)
1659 {
1660 u_int32_t reg;
1661 int i;
1662
1663 for (i = 1000; --i >= 0; ) {
1664 if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_RD|ATW_BBPCTL_WR) == 0)
1665 break;
1666 DELAY(100);
1667 }
1668
1669 if (i < 0) {
1670 printf("%s: start atw_rf3000_read, BBPCTL busy\n",
1671 sc->sc_dev.dv_xname);
1672 return ETIMEDOUT;
1673 }
1674
1675 reg = sc->sc_bbpctl_rd | LSHIFT(addr & 0x7f, ATW_BBPCTL_ADDR_MASK);
1676
1677 ATW_WRITE(sc, ATW_BBPCTL, reg);
1678
1679 for (i = 1000; --i >= 0; ) {
1680 DELAY(100);
1681 if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_RD) == 0)
1682 break;
1683 }
1684
1685 ATW_CLR(sc, ATW_BBPCTL, ATW_BBPCTL_RD);
1686
1687 if (i < 0) {
1688 printf("%s: atw_rf3000_read wrote %08x; BBPCTL still busy\n",
1689 sc->sc_dev.dv_xname, reg);
1690 return ETIMEDOUT;
1691 }
1692 if (val != NULL)
1693 *val = MASK_AND_RSHIFT(reg, ATW_BBPCTL_DATA_MASK);
1694 return 0;
1695 }
1696 #endif /* ATW_BBPDEBUG */
1697
1698 /* Write a register on the Si4126 RF/IF synthesizer using the registers
1699 * provided by the ADM8211 for that purpose.
1700 *
1701 * val is 18 bits of data, and val is the 4-bit address of the register.
1702 *
1703 * Return 0 on success.
1704 */
1705 static void
1706 atw_si4126_write(struct atw_softc *sc, u_int addr, u_int val)
1707 {
1708 uint32_t bits, mask, reg;
1709 const int nbits = 22;
1710
1711 KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
1712 KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
1713
1714 bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
1715 LSHIFT(addr, SI4126_TWI_ADDR_MASK);
1716
1717 reg = ATW_SYNRF_SELSYN;
1718 /* reference driver: reset Si4126 serial bus to initial
1719 * conditions?
1720 */
1721 ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_LEIF);
1722 ATW_WRITE(sc, ATW_SYNRF, reg);
1723
1724 for (mask = BIT(nbits - 1); mask != 0; mask >>= 1) {
1725 if ((bits & mask) != 0)
1726 reg |= ATW_SYNRF_SYNDATA;
1727 else
1728 reg &= ~ATW_SYNRF_SYNDATA;
1729 ATW_WRITE(sc, ATW_SYNRF, reg);
1730 ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_SYNCLK);
1731 ATW_WRITE(sc, ATW_SYNRF, reg);
1732 }
1733 ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_LEIF);
1734 ATW_WRITE(sc, ATW_SYNRF, 0x0);
1735 }
1736
1737 /* Read 18-bit data from the 4-bit address addr in Si4126
1738 * RF synthesizer and write the data to *val. Return 0 on success.
1739 *
1740 * XXX This does not seem to work. The ADM8211 must require more or
1741 * different magic to read the chip than to write it.
1742 */
1743 #ifdef ATW_SYNDEBUG
1744 static int
1745 atw_si4126_read(struct atw_softc *sc, u_int addr, u_int *val)
1746 {
1747 u_int32_t reg;
1748 int i;
1749
1750 KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
1751
1752 for (i = 1000; --i >= 0; ) {
1753 if (ATW_ISSET(sc, ATW_SYNCTL, ATW_SYNCTL_RD|ATW_SYNCTL_WR) == 0)
1754 break;
1755 DELAY(100);
1756 }
1757
1758 if (i < 0) {
1759 printf("%s: start atw_si4126_read, SYNCTL busy\n",
1760 sc->sc_dev.dv_xname);
1761 return ETIMEDOUT;
1762 }
1763
1764 reg = sc->sc_synctl_rd | LSHIFT(addr, ATW_SYNCTL_DATA_MASK);
1765
1766 ATW_WRITE(sc, ATW_SYNCTL, reg);
1767
1768 for (i = 1000; --i >= 0; ) {
1769 DELAY(100);
1770 if (ATW_ISSET(sc, ATW_SYNCTL, ATW_SYNCTL_RD) == 0)
1771 break;
1772 }
1773
1774 ATW_CLR(sc, ATW_SYNCTL, ATW_SYNCTL_RD);
1775
1776 if (i < 0) {
1777 printf("%s: atw_si4126_read wrote %#08x, SYNCTL still busy\n",
1778 sc->sc_dev.dv_xname, reg);
1779 return ETIMEDOUT;
1780 }
1781 if (val != NULL)
1782 *val = MASK_AND_RSHIFT(ATW_READ(sc, ATW_SYNCTL),
1783 ATW_SYNCTL_DATA_MASK);
1784 return 0;
1785 }
1786 #endif /* ATW_SYNDEBUG */
1787
1788 /* XXX is the endianness correct? test. */
1789 #define atw_calchash(addr) \
1790 (ether_crc32_le((addr), IEEE80211_ADDR_LEN) & BITS(5, 0))
1791
1792 /*
1793 * atw_filter_setup:
1794 *
1795 * Set the ADM8211's receive filter.
1796 */
1797 static void
1798 atw_filter_setup(struct atw_softc *sc)
1799 {
1800 struct ieee80211com *ic = &sc->sc_ic;
1801 struct ethercom *ec = &ic->ic_ec;
1802 struct ifnet *ifp = &sc->sc_ic.ic_if;
1803 int hash;
1804 u_int32_t hashes[2];
1805 struct ether_multi *enm;
1806 struct ether_multistep step;
1807
1808 /* According to comments in tlp_al981_filter_setup
1809 * (dev/ic/tulip.c) the ADMtek AL981 does not like for its
1810 * multicast filter to be set while it is running. Hopefully
1811 * the ADM8211 is not the same!
1812 */
1813 if ((ifp->if_flags & IFF_RUNNING) != 0)
1814 atw_idle(sc, ATW_NAR_SR);
1815
1816 sc->sc_opmode &= ~(ATW_NAR_PR|ATW_NAR_MM);
1817
1818 /* XXX in scan mode, do not filter packets. Maybe this is
1819 * unnecessary.
1820 */
1821 if (ic->ic_state == IEEE80211_S_SCAN ||
1822 (ifp->if_flags & IFF_PROMISC) != 0) {
1823 sc->sc_opmode |= ATW_NAR_PR;
1824 goto allmulti;
1825 }
1826
1827 hashes[0] = hashes[1] = 0x0;
1828
1829 /*
1830 * Program the 64-bit multicast hash filter.
1831 */
1832 ETHER_FIRST_MULTI(step, ec, enm);
1833 while (enm != NULL) {
1834 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1835 ETHER_ADDR_LEN) != 0)
1836 goto allmulti;
1837
1838 hash = atw_calchash(enm->enm_addrlo);
1839 hashes[hash >> 5] |= 1 << (hash & 0x1f);
1840 ETHER_NEXT_MULTI(step, enm);
1841 }
1842 ifp->if_flags &= ~IFF_ALLMULTI;
1843 goto setit;
1844
1845 allmulti:
1846 ifp->if_flags |= IFF_ALLMULTI;
1847 hashes[0] = hashes[1] = 0xffffffff;
1848
1849 setit:
1850 ATW_WRITE(sc, ATW_MAR0, hashes[0]);
1851 ATW_WRITE(sc, ATW_MAR1, hashes[1]);
1852 ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
1853 DELAY(20 * 1000);
1854
1855 DPRINTF(sc, ("%s: ATW_NAR %08x opmode %08x\n", sc->sc_dev.dv_xname,
1856 ATW_READ(sc, ATW_NAR), sc->sc_opmode));
1857 }
1858
1859 /* Tell the ADM8211 our preferred BSSID. The ADM8211 must match
1860 * a beacon's BSSID and SSID against the preferred BSSID and SSID
1861 * before it will raise ATW_INTR_LINKON. When the ADM8211 receives
1862 * no beacon with the preferred BSSID and SSID in the number of
1863 * beacon intervals given in ATW_BPLI, then it raises ATW_INTR_LINKOFF.
1864 */
1865 static void
1866 atw_write_bssid(struct atw_softc *sc)
1867 {
1868 struct ieee80211com *ic = &sc->sc_ic;
1869 u_int8_t *bssid;
1870
1871 bssid = ic->ic_bss->ni_bssid;
1872
1873 ATW_WRITE(sc, ATW_BSSID0,
1874 LSHIFT(bssid[0], ATW_BSSID0_BSSIDB0_MASK) |
1875 LSHIFT(bssid[1], ATW_BSSID0_BSSIDB1_MASK) |
1876 LSHIFT(bssid[2], ATW_BSSID0_BSSIDB2_MASK) |
1877 LSHIFT(bssid[3], ATW_BSSID0_BSSIDB3_MASK));
1878
1879 ATW_WRITE(sc, ATW_ABDA1,
1880 (ATW_READ(sc, ATW_ABDA1) &
1881 ~(ATW_ABDA1_BSSIDB4_MASK|ATW_ABDA1_BSSIDB5_MASK)) |
1882 LSHIFT(bssid[4], ATW_ABDA1_BSSIDB4_MASK) |
1883 LSHIFT(bssid[5], ATW_ABDA1_BSSIDB5_MASK));
1884
1885 DPRINTF(sc, ("%s: BSSID %s -> ", sc->sc_dev.dv_xname,
1886 ether_sprintf(sc->sc_bssid)));
1887 DPRINTF(sc, ("%s\n", ether_sprintf(bssid)));
1888
1889 memcpy(sc->sc_bssid, bssid, sizeof(sc->sc_bssid));
1890 }
1891
1892 /* Write buflen bytes from buf to SRAM starting at the SRAM's ofs'th
1893 * 16-bit word.
1894 */
1895 static void
1896 atw_write_sram(struct atw_softc *sc, u_int ofs, u_int8_t *buf, u_int buflen)
1897 {
1898 u_int i;
1899 u_int8_t *ptr;
1900
1901 memcpy(&sc->sc_sram[ofs], buf, buflen);
1902
1903 if (ofs % 2 != 0) {
1904 ofs--;
1905 buflen++;
1906 }
1907
1908 if (buflen % 2 != 0)
1909 buflen++;
1910
1911 assert(buflen + ofs <= ATW_SRAM_SIZE);
1912
1913 ptr = &sc->sc_sram[ofs];
1914
1915 for (i = 0; i < buflen; i += 2) {
1916 ATW_WRITE(sc, ATW_WEPCTL, ATW_WEPCTL_WR |
1917 LSHIFT((ofs + i) / 2, ATW_WEPCTL_TBLADD_MASK));
1918 DELAY(atw_writewep_delay);
1919
1920 ATW_WRITE(sc, ATW_WESK,
1921 LSHIFT((ptr[i + 1] << 8) | ptr[i], ATW_WESK_DATA_MASK));
1922 DELAY(atw_writewep_delay);
1923 }
1924 ATW_WRITE(sc, ATW_WEPCTL, sc->sc_wepctl); /* restore WEP condition */
1925
1926 if (sc->sc_if.if_flags & IFF_DEBUG) {
1927 int n_octets = 0;
1928 printf("%s: wrote %d bytes at 0x%x wepctl 0x%08x\n",
1929 sc->sc_dev.dv_xname, buflen, ofs, sc->sc_wepctl);
1930 for (i = 0; i < buflen; i++) {
1931 printf(" %02x", ptr[i]);
1932 if (++n_octets % 24 == 0)
1933 printf("\n");
1934 }
1935 if (n_octets % 24 != 0)
1936 printf("\n");
1937 }
1938 }
1939
1940 /* Write WEP keys from the ieee80211com to the ADM8211's SRAM. */
1941 static void
1942 atw_write_wep(struct atw_softc *sc)
1943 {
1944 struct ieee80211com *ic = &sc->sc_ic;
1945 /* SRAM shared-key record format: key0 flags key1 ... key12 */
1946 u_int8_t buf[IEEE80211_WEP_NKID]
1947 [1 /* key[0] */ + 1 /* flags */ + 12 /* key[1 .. 12] */];
1948 u_int32_t reg;
1949 int i;
1950
1951 sc->sc_wepctl = 0;
1952 ATW_WRITE(sc, ATW_WEPCTL, sc->sc_wepctl);
1953
1954 if ((ic->ic_flags & IEEE80211_F_WEPON) == 0)
1955 return;
1956
1957 memset(&buf[0][0], 0, sizeof(buf));
1958
1959 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1960 if (ic->ic_nw_keys[i].wk_len > 5) {
1961 buf[i][1] = ATW_WEP_ENABLED | ATW_WEP_104BIT;
1962 } else if (ic->ic_nw_keys[i].wk_len != 0) {
1963 buf[i][1] = ATW_WEP_ENABLED;
1964 } else {
1965 buf[i][1] = 0;
1966 continue;
1967 }
1968 buf[i][0] = ic->ic_nw_keys[i].wk_key[0];
1969 memcpy(&buf[i][2], &ic->ic_nw_keys[i].wk_key[1],
1970 ic->ic_nw_keys[i].wk_len - 1);
1971 }
1972
1973 reg = ATW_READ(sc, ATW_MACTEST);
1974 reg |= ATW_MACTEST_MMI_USETXCLK | ATW_MACTEST_FORCE_KEYID;
1975 reg &= ~ATW_MACTEST_KEYID_MASK;
1976 reg |= LSHIFT(ic->ic_wep_txkey, ATW_MACTEST_KEYID_MASK);
1977 ATW_WRITE(sc, ATW_MACTEST, reg);
1978
1979 /* RX bypass WEP if revision != 0x20. (I assume revision != 0x20
1980 * throughout.)
1981 */
1982 sc->sc_wepctl = ATW_WEPCTL_WEPENABLE | ATW_WEPCTL_WEPRXBYP;
1983 if (sc->sc_if.if_flags & IFF_LINK2)
1984 sc->sc_wepctl &= ~ATW_WEPCTL_WEPRXBYP;
1985
1986 atw_write_sram(sc, ATW_SRAM_ADDR_SHARED_KEY, (u_int8_t*)&buf[0][0],
1987 sizeof(buf));
1988 }
1989
1990 const struct timeval atw_beacon_mininterval = {1, 0}; /* 1s */
1991
1992 static void
1993 atw_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
1994 struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
1995 {
1996 struct atw_softc *sc = (struct atw_softc*)ic->ic_softc;
1997
1998 switch (subtype) {
1999 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2000 /* do nothing: hardware answers probe request */
2001 break;
2002 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2003 case IEEE80211_FC0_SUBTYPE_BEACON:
2004 atw_recv_beacon(ic, m, ni, subtype, rssi, rstamp);
2005 break;
2006 default:
2007 (*sc->sc_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
2008 break;
2009 }
2010 return;
2011 }
2012
2013 /* In ad hoc mode, atw_recv_beacon is responsible for the coalescence
2014 * of IBSSs with like SSID/channel but different BSSID. It joins the
2015 * oldest IBSS (i.e., with greatest TSF time), since that is the WECA
2016 * convention. Possibly the ADMtek chip does this for us; I will have
2017 * to test to find out.
2018 *
2019 * XXX we should add the duration field of the received beacon to
2020 * the TSF time it contains before comparing it with the ADM8211's
2021 * TSF.
2022 */
2023 static void
2024 atw_recv_beacon(struct ieee80211com *ic, struct mbuf *m0,
2025 struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
2026 {
2027 struct atw_softc *sc;
2028 struct ieee80211_frame *wh;
2029 uint32_t tsftl, tsfth;
2030 uint32_t bcn_tsftl, bcn_tsfth;
2031 int do_print = 0;
2032 union {
2033 uint32_t words[2];
2034 uint8_t tstamp[8];
2035 } u;
2036
2037 sc = (struct atw_softc*)ic->ic_if.if_softc;
2038
2039 if (ic->ic_if.if_flags & IFF_LINK0) {
2040 do_print = (ic->ic_if.if_flags & IFF_DEBUG)
2041 ? 1 : ratecheck(&sc->sc_last_beacon,
2042 &atw_beacon_mininterval);
2043 }
2044
2045 wh = mtod(m0, struct ieee80211_frame *);
2046
2047 (*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
2048
2049 if (ic->ic_state != IEEE80211_S_RUN)
2050 return;
2051
2052 if ((ni = ieee80211_lookup_node(ic, wh->i_addr2,
2053 ic->ic_bss->ni_chan)) == NULL) {
2054 if (do_print)
2055 printf("%s: atw_recv_beacon: no node %s\n",
2056 sc->sc_dev.dv_xname, ether_sprintf(wh->i_addr2));
2057 return;
2058 }
2059
2060 if (ieee80211_match_bss(ic, ni) != 0)
2061 return;
2062
2063 if (memcmp(ni->ni_bssid, ic->ic_bss->ni_bssid, IEEE80211_ADDR_LEN) == 0)
2064 return;
2065
2066 if (do_print)
2067 printf("%s: atw_recv_beacon: bssid mismatch %s\n",
2068 sc->sc_dev.dv_xname, ether_sprintf(ni->ni_bssid));
2069
2070 if (ic->ic_opmode != IEEE80211_M_IBSS)
2071 return;
2072
2073 atw_tsft(sc, &tsfth, &tsftl);
2074
2075 (void)memcpy(&u, &ic->ic_bss->ni_tstamp[0], sizeof(u));
2076 bcn_tsftl = le32toh(u.words[0]);
2077 bcn_tsfth = le32toh(u.words[1]);
2078
2079 if (do_print)
2080 printf("%s: my tsft %" PRIu64 " beacon tsft %" PRIu64 "\n",
2081 sc->sc_dev.dv_xname, ((uint64_t)tsfth << 32) | tsftl,
2082 ((uint64_t)bcn_tsfth << 32) | bcn_tsftl);
2083
2084 /* we are faster, let the other guy catch up */
2085 if (bcn_tsfth < tsfth)
2086 return;
2087 else if (bcn_tsfth == tsfth && bcn_tsftl < tsftl)
2088 return;
2089
2090 if (do_print)
2091 printf("%s: sync TSF with %s\n", sc->sc_dev.dv_xname,
2092 ether_sprintf(wh->i_addr2));
2093
2094 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2095
2096 atw_tsf(sc);
2097
2098 /* negotiate rates with new IBSS */
2099 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE |
2100 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2101 if (ni->ni_rates.rs_nrates == 0) {
2102 printf("%s: rates mismatch, BSSID %s\n", sc->sc_dev.dv_xname,
2103 ether_sprintf(ni->ni_bssid));
2104 return;
2105 }
2106
2107 if (do_print) {
2108 printf("%s: sync BSSID %s -> ", sc->sc_dev.dv_xname,
2109 ether_sprintf(ic->ic_bss->ni_bssid));
2110 printf("%s ", ether_sprintf(ni->ni_bssid));
2111 printf("(from %s)\n", ether_sprintf(wh->i_addr2));
2112 }
2113
2114 (*ic->ic_node_copy)(ic, ic->ic_bss, ni);
2115
2116 atw_write_bssid(sc);
2117 atw_start_beacon(sc, 1);
2118 }
2119
2120 /* Write the SSID in the ieee80211com to the SRAM on the ADM8211.
2121 * In ad hoc mode, the SSID is written to the beacons sent by the
2122 * ADM8211. In both ad hoc and infrastructure mode, beacons received
2123 * with matching SSID affect ATW_INTR_LINKON/ATW_INTR_LINKOFF
2124 * indications.
2125 */
2126 static void
2127 atw_write_ssid(struct atw_softc *sc)
2128 {
2129 struct ieee80211com *ic = &sc->sc_ic;
2130 /* 34 bytes are reserved in ADM8211 SRAM for the SSID, but
2131 * it only expects the element length, not its ID.
2132 */
2133 u_int8_t buf[roundup(1 /* length */ + IEEE80211_NWID_LEN, 2)];
2134
2135 memset(buf, 0, sizeof(buf));
2136 buf[0] = ic->ic_bss->ni_esslen;
2137 memcpy(&buf[1], ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen);
2138
2139 atw_write_sram(sc, ATW_SRAM_ADDR_SSID, buf,
2140 roundup(1 + ic->ic_bss->ni_esslen, 2));
2141 }
2142
2143 /* Write the supported rates in the ieee80211com to the SRAM of the ADM8211.
2144 * In ad hoc mode, the supported rates are written to beacons sent by the
2145 * ADM8211.
2146 */
2147 static void
2148 atw_write_sup_rates(struct atw_softc *sc)
2149 {
2150 struct ieee80211com *ic = &sc->sc_ic;
2151 /* 14 bytes are probably (XXX) reserved in the ADM8211 SRAM for
2152 * supported rates
2153 */
2154 u_int8_t buf[roundup(1 /* length */ + IEEE80211_RATE_SIZE, 2)];
2155
2156 memset(buf, 0, sizeof(buf));
2157
2158 buf[0] = ic->ic_bss->ni_rates.rs_nrates;
2159
2160 memcpy(&buf[1], ic->ic_bss->ni_rates.rs_rates,
2161 ic->ic_bss->ni_rates.rs_nrates);
2162
2163 atw_write_sram(sc, ATW_SRAM_ADDR_SUPRATES, buf, sizeof(buf));
2164 }
2165
2166 /* Start/stop sending beacons. */
2167 void
2168 atw_start_beacon(struct atw_softc *sc, int start)
2169 {
2170 struct ieee80211com *ic = &sc->sc_ic;
2171 uint16_t chan;
2172 uint32_t bcnt, bpli, cap0, cap1, capinfo;
2173 size_t len;
2174
2175 if (ATW_IS_ENABLED(sc) == 0)
2176 return;
2177
2178 /* start beacons */
2179 len = sizeof(struct ieee80211_frame) +
2180 8 /* timestamp */ + 2 /* beacon interval */ +
2181 2 /* capability info */ +
2182 2 + ic->ic_bss->ni_esslen /* SSID element */ +
2183 2 + ic->ic_bss->ni_rates.rs_nrates /* rates element */ +
2184 3 /* DS parameters */ +
2185 IEEE80211_CRC_LEN;
2186
2187 bcnt = ATW_READ(sc, ATW_BCNT) & ~ATW_BCNT_BCNT_MASK;
2188 cap0 = ATW_READ(sc, ATW_CAP0) & ~ATW_CAP0_CHN_MASK;
2189 cap1 = ATW_READ(sc, ATW_CAP1) & ~ATW_CAP1_CAPI_MASK;
2190
2191 ATW_WRITE(sc, ATW_BCNT, bcnt);
2192 ATW_WRITE(sc, ATW_CAP1, cap1);
2193
2194 if (!start)
2195 return;
2196
2197 /* TBD use ni_capinfo */
2198
2199 capinfo = 0;
2200 if (sc->sc_flags & ATWF_SHORT_PREAMBLE)
2201 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2202 if (ic->ic_flags & IEEE80211_F_WEPON)
2203 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2204
2205 switch (ic->ic_opmode) {
2206 case IEEE80211_M_IBSS:
2207 len += 4; /* IBSS parameters */
2208 capinfo |= IEEE80211_CAPINFO_IBSS;
2209 break;
2210 case IEEE80211_M_HOSTAP:
2211 /* XXX 6-byte minimum TIM */
2212 len += atw_beacon_len_adjust;
2213 capinfo |= IEEE80211_CAPINFO_ESS;
2214 break;
2215 default:
2216 return;
2217 }
2218
2219 /* set listen interval
2220 * XXX do software units agree w/ hardware?
2221 */
2222 bpli = LSHIFT(ic->ic_bss->ni_intval, ATW_BPLI_BP_MASK) |
2223 LSHIFT(ic->ic_lintval / ic->ic_bss->ni_intval, ATW_BPLI_LI_MASK);
2224
2225 chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
2226
2227 bcnt |= LSHIFT(len, ATW_BCNT_BCNT_MASK);
2228 cap0 |= LSHIFT(chan, ATW_CAP0_CHN_MASK);
2229 cap1 |= LSHIFT(capinfo, ATW_CAP1_CAPI_MASK);
2230
2231 ATW_WRITE(sc, ATW_BCNT, bcnt);
2232 ATW_WRITE(sc, ATW_BPLI, bpli);
2233 ATW_WRITE(sc, ATW_CAP0, cap0);
2234 ATW_WRITE(sc, ATW_CAP1, cap1);
2235
2236 DPRINTF(sc, ("%s: atw_start_beacon reg[ATW_BCNT] = %08x\n",
2237 sc->sc_dev.dv_xname, bcnt));
2238
2239 DPRINTF(sc, ("%s: atw_start_beacon reg[ATW_CAP1] = %08x\n",
2240 sc->sc_dev.dv_xname, cap1));
2241 }
2242
2243 /* Return the 32 lsb of the last TSFT divisible by ival. */
2244 static __inline uint32_t
2245 atw_last_even_tsft(uint32_t tsfth, uint32_t tsftl, uint32_t ival)
2246 {
2247 /* Following the reference driver's lead, I compute
2248 *
2249 * (uint32_t)((((uint64_t)tsfth << 32) | tsftl) % ival)
2250 *
2251 * without using 64-bit arithmetic, using the following
2252 * relationship:
2253 *
2254 * (0x100000000 * H + L) % m
2255 * = ((0x100000000 % m) * H + L) % m
2256 * = (((0xffffffff + 1) % m) * H + L) % m
2257 * = ((0xffffffff % m + 1 % m) * H + L) % m
2258 * = ((0xffffffff % m + 1) * H + L) % m
2259 */
2260 return ((0xFFFFFFFF % ival + 1) * tsfth + tsftl) % ival;
2261 }
2262
2263 static __inline void
2264 atw_tsft(struct atw_softc *sc, uint32_t *tsfth, uint32_t *tsftl)
2265 {
2266 int i;
2267 for (i = 0; i < 2; i++) {
2268 *tsfth = ATW_READ(sc, ATW_TSFTH);
2269 *tsftl = ATW_READ(sc, ATW_TSFTL);
2270 if (ATW_READ(sc, ATW_TSFTH) == *tsfth)
2271 break;
2272 }
2273 }
2274
2275 /* If we've created an IBSS, write the TSF time in the ADM8211 to
2276 * the ieee80211com.
2277 *
2278 * Predict the next target beacon transmission time (TBTT) and
2279 * write it to the ADM8211.
2280 */
2281 static void
2282 atw_tsf(struct atw_softc *sc)
2283 {
2284 #define TBTTOFS 20 /* TU */
2285
2286 struct ieee80211com *ic = &sc->sc_ic;
2287 uint32_t ival, past_even, tbtt, tsfth, tsftl;
2288 union {
2289 uint32_t words[2];
2290 uint8_t tstamp[8];
2291 } u;
2292
2293 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) ||
2294 ((ic->ic_opmode == IEEE80211_M_IBSS) &&
2295 (ic->ic_flags & IEEE80211_F_SIBSS))) {
2296 atw_tsft(sc, &tsfth, &tsftl);
2297 u.words[0] = htole32(tsftl);
2298 u.words[1] = htole32(tsfth);
2299 (void)memcpy(&ic->ic_bss->ni_tstamp[0], &u,
2300 sizeof(ic->ic_bss->ni_tstamp));
2301 } else {
2302 (void)memcpy(&u, &ic->ic_bss->ni_tstamp[0], sizeof(u));
2303 tsftl = le32toh(u.words[0]);
2304 tsfth = le32toh(u.words[1]);
2305 }
2306
2307 ival = ic->ic_bss->ni_intval * IEEE80211_DUR_TU;
2308
2309 /* We sent/received the last beacon `past' microseconds
2310 * after the interval divided the TSF timer.
2311 */
2312 past_even = tsftl - atw_last_even_tsft(tsfth, tsftl, ival);
2313
2314 /* Skip ten beacons so that the TBTT cannot pass before
2315 * we've programmed it. Ten is an arbitrary number.
2316 */
2317 tbtt = past_even + ival * 10;
2318
2319 ATW_WRITE(sc, ATW_TOFS1,
2320 LSHIFT(1, ATW_TOFS1_TSFTOFSR_MASK) |
2321 LSHIFT(TBTTOFS, ATW_TOFS1_TBTTOFS_MASK) |
2322 LSHIFT(MASK_AND_RSHIFT(tbtt - TBTTOFS * IEEE80211_DUR_TU,
2323 ATW_TBTTPRE_MASK), ATW_TOFS1_TBTTPRE_MASK));
2324 #undef TBTTOFS
2325 }
2326
2327 static void
2328 atw_next_scan(void *arg)
2329 {
2330 struct atw_softc *sc = arg;
2331 struct ieee80211com *ic = &sc->sc_ic;
2332 struct ifnet *ifp = &ic->ic_if;
2333 int s;
2334
2335 /* don't call atw_start w/o network interrupts blocked */
2336 s = splnet();
2337 if (ic->ic_state == IEEE80211_S_SCAN)
2338 ieee80211_next_scan(ifp);
2339 splx(s);
2340 }
2341
2342 /* Synchronize the hardware state with the software state. */
2343 static int
2344 atw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2345 {
2346 struct ifnet *ifp = &ic->ic_if;
2347 struct atw_softc *sc = ifp->if_softc;
2348 enum ieee80211_state ostate;
2349 int error;
2350
2351 ostate = ic->ic_state;
2352
2353 if (nstate == IEEE80211_S_INIT) {
2354 callout_stop(&sc->sc_scan_ch);
2355 sc->sc_cur_chan = IEEE80211_CHAN_ANY;
2356 atw_start_beacon(sc, 0);
2357 return (*sc->sc_newstate)(ic, nstate, arg);
2358 }
2359
2360 if ((error = atw_tune(sc)) != 0)
2361 return error;
2362
2363 switch (nstate) {
2364 case IEEE80211_S_ASSOC:
2365 break;
2366 case IEEE80211_S_INIT:
2367 panic("%s: unexpected state IEEE80211_S_INIT\n", __func__);
2368 break;
2369 case IEEE80211_S_SCAN:
2370 callout_reset(&sc->sc_scan_ch, atw_dwelltime * hz / 1000,
2371 atw_next_scan, sc);
2372
2373 break;
2374 case IEEE80211_S_RUN:
2375 if (ic->ic_opmode == IEEE80211_M_STA)
2376 break;
2377 /*FALLTHROUGH*/
2378 case IEEE80211_S_AUTH:
2379 atw_write_bssid(sc);
2380 atw_write_ssid(sc);
2381 atw_write_sup_rates(sc);
2382
2383 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
2384 ic->ic_opmode == IEEE80211_M_MONITOR)
2385 break;
2386
2387 /* set listen interval
2388 * XXX do software units agree w/ hardware?
2389 */
2390 ATW_WRITE(sc, ATW_BPLI,
2391 LSHIFT(ic->ic_bss->ni_intval, ATW_BPLI_BP_MASK) |
2392 LSHIFT(ic->ic_lintval / ic->ic_bss->ni_intval,
2393 ATW_BPLI_LI_MASK));
2394
2395 DPRINTF(sc, ("%s: reg[ATW_BPLI] = %08x\n",
2396 sc->sc_dev.dv_xname, ATW_READ(sc, ATW_BPLI)));
2397
2398 atw_tsf(sc);
2399 break;
2400 }
2401
2402 if (nstate != IEEE80211_S_SCAN)
2403 callout_stop(&sc->sc_scan_ch);
2404
2405 if (nstate == IEEE80211_S_RUN &&
2406 (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2407 ic->ic_opmode == IEEE80211_M_IBSS))
2408 atw_start_beacon(sc, 1);
2409 else
2410 atw_start_beacon(sc, 0);
2411
2412 error = (*sc->sc_newstate)(ic, nstate, arg);
2413
2414 if (ostate == IEEE80211_S_INIT && nstate == IEEE80211_S_SCAN)
2415 atw_write_bssid(sc);
2416
2417 return error;
2418 }
2419
2420 /*
2421 * atw_add_rxbuf:
2422 *
2423 * Add a receive buffer to the indicated descriptor.
2424 */
2425 int
2426 atw_add_rxbuf(struct atw_softc *sc, int idx)
2427 {
2428 struct atw_rxsoft *rxs = &sc->sc_rxsoft[idx];
2429 struct mbuf *m;
2430 int error;
2431
2432 MGETHDR(m, M_DONTWAIT, MT_DATA);
2433 if (m == NULL)
2434 return (ENOBUFS);
2435
2436 MCLGET(m, M_DONTWAIT);
2437 if ((m->m_flags & M_EXT) == 0) {
2438 m_freem(m);
2439 return (ENOBUFS);
2440 }
2441
2442 if (rxs->rxs_mbuf != NULL)
2443 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2444
2445 rxs->rxs_mbuf = m;
2446
2447 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
2448 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
2449 BUS_DMA_READ|BUS_DMA_NOWAIT);
2450 if (error) {
2451 printf("%s: can't load rx DMA map %d, error = %d\n",
2452 sc->sc_dev.dv_xname, idx, error);
2453 panic("atw_add_rxbuf"); /* XXX */
2454 }
2455
2456 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2457 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2458
2459 ATW_INIT_RXDESC(sc, idx);
2460
2461 return (0);
2462 }
2463
2464 /*
2465 * Release any queued transmit buffers.
2466 */
2467 void
2468 atw_txdrain(struct atw_softc *sc)
2469 {
2470 struct atw_txsoft *txs;
2471
2472 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
2473 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
2474 if (txs->txs_mbuf != NULL) {
2475 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2476 m_freem(txs->txs_mbuf);
2477 txs->txs_mbuf = NULL;
2478 }
2479 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
2480 }
2481 sc->sc_tx_timer = 0;
2482 }
2483
2484 /*
2485 * atw_stop: [ ifnet interface function ]
2486 *
2487 * Stop transmission on the interface.
2488 */
2489 void
2490 atw_stop(struct ifnet *ifp, int disable)
2491 {
2492 struct atw_softc *sc = ifp->if_softc;
2493 struct ieee80211com *ic = &sc->sc_ic;
2494
2495 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2496
2497 /* Disable interrupts. */
2498 ATW_WRITE(sc, ATW_IER, 0);
2499
2500 /* Stop the transmit and receive processes. */
2501 sc->sc_opmode = 0;
2502 ATW_WRITE(sc, ATW_NAR, 0);
2503 DELAY(20 * 1000);
2504 ATW_WRITE(sc, ATW_TDBD, 0);
2505 ATW_WRITE(sc, ATW_TDBP, 0);
2506 ATW_WRITE(sc, ATW_RDB, 0);
2507
2508 atw_txdrain(sc);
2509
2510 if (disable) {
2511 atw_rxdrain(sc);
2512 atw_disable(sc);
2513 }
2514
2515 /*
2516 * Mark the interface down and cancel the watchdog timer.
2517 */
2518 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2519 ifp->if_timer = 0;
2520
2521 if (!disable)
2522 atw_reset(sc);
2523 }
2524
2525 /*
2526 * atw_rxdrain:
2527 *
2528 * Drain the receive queue.
2529 */
2530 void
2531 atw_rxdrain(struct atw_softc *sc)
2532 {
2533 struct atw_rxsoft *rxs;
2534 int i;
2535
2536 for (i = 0; i < ATW_NRXDESC; i++) {
2537 rxs = &sc->sc_rxsoft[i];
2538 if (rxs->rxs_mbuf == NULL)
2539 continue;
2540 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2541 m_freem(rxs->rxs_mbuf);
2542 rxs->rxs_mbuf = NULL;
2543 }
2544 }
2545
2546 /*
2547 * atw_detach:
2548 *
2549 * Detach an ADM8211 interface.
2550 */
2551 int
2552 atw_detach(struct atw_softc *sc)
2553 {
2554 struct ifnet *ifp = &sc->sc_ic.ic_if;
2555 struct atw_rxsoft *rxs;
2556 struct atw_txsoft *txs;
2557 int i;
2558
2559 /*
2560 * Succeed now if there isn't any work to do.
2561 */
2562 if ((sc->sc_flags & ATWF_ATTACHED) == 0)
2563 return (0);
2564
2565 ieee80211_ifdetach(ifp);
2566 if_detach(ifp);
2567
2568 for (i = 0; i < ATW_NRXDESC; i++) {
2569 rxs = &sc->sc_rxsoft[i];
2570 if (rxs->rxs_mbuf != NULL) {
2571 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2572 m_freem(rxs->rxs_mbuf);
2573 rxs->rxs_mbuf = NULL;
2574 }
2575 bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
2576 }
2577 for (i = 0; i < ATW_TXQUEUELEN; i++) {
2578 txs = &sc->sc_txsoft[i];
2579 if (txs->txs_mbuf != NULL) {
2580 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2581 m_freem(txs->txs_mbuf);
2582 txs->txs_mbuf = NULL;
2583 }
2584 bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
2585 }
2586 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
2587 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
2588 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
2589 sizeof(struct atw_control_data));
2590 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
2591
2592 shutdownhook_disestablish(sc->sc_sdhook);
2593 powerhook_disestablish(sc->sc_powerhook);
2594
2595 if (sc->sc_srom)
2596 free(sc->sc_srom, M_DEVBUF);
2597
2598 return (0);
2599 }
2600
2601 /* atw_shutdown: make sure the interface is stopped at reboot time. */
2602 void
2603 atw_shutdown(void *arg)
2604 {
2605 struct atw_softc *sc = arg;
2606
2607 atw_stop(&sc->sc_ic.ic_if, 1);
2608 }
2609
2610 int
2611 atw_intr(void *arg)
2612 {
2613 struct atw_softc *sc = arg;
2614 struct ifnet *ifp = &sc->sc_ic.ic_if;
2615 u_int32_t status, rxstatus, txstatus, linkstatus;
2616 int handled = 0, txthresh;
2617
2618 #ifdef DEBUG
2619 if (ATW_IS_ENABLED(sc) == 0)
2620 panic("%s: atw_intr: not enabled", sc->sc_dev.dv_xname);
2621 #endif
2622
2623 /*
2624 * If the interface isn't running, the interrupt couldn't
2625 * possibly have come from us.
2626 */
2627 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
2628 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
2629 return (0);
2630
2631 for (;;) {
2632 status = ATW_READ(sc, ATW_STSR);
2633
2634 if (status)
2635 ATW_WRITE(sc, ATW_STSR, status);
2636
2637 #ifdef ATW_DEBUG
2638 #define PRINTINTR(flag) do { \
2639 if ((status & flag) != 0) { \
2640 printf("%s" #flag, delim); \
2641 delim = ","; \
2642 } \
2643 } while (0)
2644
2645 if (atw_debug > 1 && status) {
2646 const char *delim = "<";
2647
2648 printf("%s: reg[STSR] = %x",
2649 sc->sc_dev.dv_xname, status);
2650
2651 PRINTINTR(ATW_INTR_FBE);
2652 PRINTINTR(ATW_INTR_LINKOFF);
2653 PRINTINTR(ATW_INTR_LINKON);
2654 PRINTINTR(ATW_INTR_RCI);
2655 PRINTINTR(ATW_INTR_RDU);
2656 PRINTINTR(ATW_INTR_REIS);
2657 PRINTINTR(ATW_INTR_RPS);
2658 PRINTINTR(ATW_INTR_TCI);
2659 PRINTINTR(ATW_INTR_TDU);
2660 PRINTINTR(ATW_INTR_TLT);
2661 PRINTINTR(ATW_INTR_TPS);
2662 PRINTINTR(ATW_INTR_TRT);
2663 PRINTINTR(ATW_INTR_TUF);
2664 PRINTINTR(ATW_INTR_BCNTC);
2665 PRINTINTR(ATW_INTR_ATIME);
2666 PRINTINTR(ATW_INTR_TBTT);
2667 PRINTINTR(ATW_INTR_TSCZ);
2668 PRINTINTR(ATW_INTR_TSFTF);
2669 printf(">\n");
2670 }
2671 #undef PRINTINTR
2672 #endif /* ATW_DEBUG */
2673
2674 if ((status & sc->sc_inten) == 0)
2675 break;
2676
2677 handled = 1;
2678
2679 rxstatus = status & sc->sc_rxint_mask;
2680 txstatus = status & sc->sc_txint_mask;
2681 linkstatus = status & sc->sc_linkint_mask;
2682
2683 if (linkstatus) {
2684 atw_linkintr(sc, linkstatus);
2685 }
2686
2687 if (rxstatus) {
2688 /* Grab any new packets. */
2689 atw_rxintr(sc);
2690
2691 if (rxstatus & ATW_INTR_RDU) {
2692 printf("%s: receive ring overrun\n",
2693 sc->sc_dev.dv_xname);
2694 /* Get the receive process going again. */
2695 ATW_WRITE(sc, ATW_RDR, 0x1);
2696 break;
2697 }
2698 }
2699
2700 if (txstatus) {
2701 /* Sweep up transmit descriptors. */
2702 atw_txintr(sc);
2703
2704 if (txstatus & ATW_INTR_TLT)
2705 DPRINTF(sc, ("%s: tx lifetime exceeded\n",
2706 sc->sc_dev.dv_xname));
2707
2708 if (txstatus & ATW_INTR_TRT)
2709 DPRINTF(sc, ("%s: tx retry limit exceeded\n",
2710 sc->sc_dev.dv_xname));
2711
2712 /* If Tx under-run, increase our transmit threshold
2713 * if another is available.
2714 */
2715 txthresh = sc->sc_txthresh + 1;
2716 if ((txstatus & ATW_INTR_TUF) &&
2717 sc->sc_txth[txthresh].txth_name != NULL) {
2718 /* Idle the transmit process. */
2719 atw_idle(sc, ATW_NAR_ST);
2720
2721 sc->sc_txthresh = txthresh;
2722 sc->sc_opmode &= ~(ATW_NAR_TR_MASK|ATW_NAR_SF);
2723 sc->sc_opmode |=
2724 sc->sc_txth[txthresh].txth_opmode;
2725 printf("%s: transmit underrun; new "
2726 "threshold: %s\n", sc->sc_dev.dv_xname,
2727 sc->sc_txth[txthresh].txth_name);
2728
2729 /* Set the new threshold and restart
2730 * the transmit process.
2731 */
2732 ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
2733 DELAY(20 * 1000);
2734 ATW_WRITE(sc, ATW_RDR, 0x1);
2735 /* XXX Log every Nth underrun from
2736 * XXX now on?
2737 */
2738 }
2739 }
2740
2741 if (status & (ATW_INTR_TPS|ATW_INTR_RPS)) {
2742 if (status & ATW_INTR_TPS)
2743 printf("%s: transmit process stopped\n",
2744 sc->sc_dev.dv_xname);
2745 if (status & ATW_INTR_RPS)
2746 printf("%s: receive process stopped\n",
2747 sc->sc_dev.dv_xname);
2748 (void)atw_init(ifp);
2749 break;
2750 }
2751
2752 if (status & ATW_INTR_FBE) {
2753 printf("%s: fatal bus error\n", sc->sc_dev.dv_xname);
2754 (void)atw_init(ifp);
2755 break;
2756 }
2757
2758 /*
2759 * Not handled:
2760 *
2761 * Transmit buffer unavailable -- normal
2762 * condition, nothing to do, really.
2763 *
2764 * Early receive interrupt -- not available on
2765 * all chips, we just use RI. We also only
2766 * use single-segment receive DMA, so this
2767 * is mostly useless.
2768 *
2769 * TBD others
2770 */
2771 }
2772
2773 /* Try to get more packets going. */
2774 atw_start(ifp);
2775
2776 return (handled);
2777 }
2778
2779 /*
2780 * atw_idle:
2781 *
2782 * Cause the transmit and/or receive processes to go idle.
2783 *
2784 * XXX It seems that the ADM8211 will not signal the end of the Rx/Tx
2785 * process in STSR if I clear SR or ST after the process has already
2786 * ceased. Fair enough. But the Rx process status bits in ATW_TEST0
2787 * do not seem to be too reliable. Perhaps I have the sense of the
2788 * Rx bits switched with the Tx bits?
2789 */
2790 void
2791 atw_idle(struct atw_softc *sc, u_int32_t bits)
2792 {
2793 u_int32_t ackmask = 0, opmode, stsr, test0;
2794 int i, s;
2795
2796 s = splnet();
2797
2798 opmode = sc->sc_opmode & ~bits;
2799
2800 if (bits & ATW_NAR_SR)
2801 ackmask |= ATW_INTR_RPS;
2802
2803 if (bits & ATW_NAR_ST) {
2804 ackmask |= ATW_INTR_TPS;
2805 /* set ATW_NAR_HF to flush TX FIFO. */
2806 opmode |= ATW_NAR_HF;
2807 }
2808
2809 ATW_WRITE(sc, ATW_NAR, opmode);
2810 DELAY(20 * 1000);
2811
2812 for (i = 0; i < 10; i++) {
2813 stsr = ATW_READ(sc, ATW_STSR);
2814 if ((stsr & ackmask) == ackmask)
2815 break;
2816 DELAY(1000);
2817 }
2818
2819 ATW_WRITE(sc, ATW_STSR, stsr & ackmask);
2820
2821 if ((stsr & ackmask) == ackmask)
2822 goto out;
2823
2824 test0 = ATW_READ(sc, ATW_TEST0);
2825
2826 if ((bits & ATW_NAR_ST) != 0 && (stsr & ATW_INTR_TPS) == 0 &&
2827 (test0 & ATW_TEST0_TS_MASK) != ATW_TEST0_TS_STOPPED) {
2828 printf("%s: transmit process not idle [%s]\n",
2829 sc->sc_dev.dv_xname,
2830 atw_tx_state[MASK_AND_RSHIFT(test0, ATW_TEST0_TS_MASK)]);
2831 printf("%s: bits %08x test0 %08x stsr %08x\n",
2832 sc->sc_dev.dv_xname, bits, test0, stsr);
2833 }
2834
2835 if ((bits & ATW_NAR_SR) != 0 && (stsr & ATW_INTR_RPS) == 0 &&
2836 (test0 & ATW_TEST0_RS_MASK) != ATW_TEST0_RS_STOPPED) {
2837 DPRINTF2(sc, ("%s: receive process not idle [%s]\n",
2838 sc->sc_dev.dv_xname,
2839 atw_rx_state[MASK_AND_RSHIFT(test0, ATW_TEST0_RS_MASK)]));
2840 DPRINTF2(sc, ("%s: bits %08x test0 %08x stsr %08x\n",
2841 sc->sc_dev.dv_xname, bits, test0, stsr));
2842 }
2843 out:
2844 if ((bits & ATW_NAR_ST) != 0)
2845 atw_txdrain(sc);
2846 splx(s);
2847 return;
2848 }
2849
2850 /*
2851 * atw_linkintr:
2852 *
2853 * Helper; handle link-status interrupts.
2854 */
2855 void
2856 atw_linkintr(struct atw_softc *sc, u_int32_t linkstatus)
2857 {
2858 struct ieee80211com *ic = &sc->sc_ic;
2859
2860 if (ic->ic_state != IEEE80211_S_RUN)
2861 return;
2862
2863 if (linkstatus & ATW_INTR_LINKON) {
2864 DPRINTF(sc, ("%s: link on\n", sc->sc_dev.dv_xname));
2865 sc->sc_rescan_timer = 0;
2866 } else if (linkstatus & ATW_INTR_LINKOFF) {
2867 DPRINTF(sc, ("%s: link off\n", sc->sc_dev.dv_xname));
2868 if (ic->ic_opmode != IEEE80211_M_STA)
2869 return;
2870 sc->sc_rescan_timer = 3;
2871 ic->ic_if.if_timer = 1;
2872 }
2873 }
2874
2875 /*
2876 * atw_rxintr:
2877 *
2878 * Helper; handle receive interrupts.
2879 */
2880 void
2881 atw_rxintr(struct atw_softc *sc)
2882 {
2883 static int rate_tbl[] = {2, 4, 11, 22, 44};
2884 struct ieee80211com *ic = &sc->sc_ic;
2885 struct ieee80211_node *ni;
2886 struct ieee80211_frame *wh;
2887 struct ifnet *ifp = &ic->ic_if;
2888 struct atw_rxsoft *rxs;
2889 struct mbuf *m;
2890 u_int32_t rxstat;
2891 int i, len, rate, rate0;
2892 u_int32_t rssi;
2893
2894 for (i = sc->sc_rxptr;; i = ATW_NEXTRX(i)) {
2895 rxs = &sc->sc_rxsoft[i];
2896
2897 ATW_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2898
2899 rxstat = le32toh(sc->sc_rxdescs[i].ar_stat);
2900 rssi = le32toh(sc->sc_rxdescs[i].ar_rssi);
2901 rate0 = MASK_AND_RSHIFT(rxstat, ATW_RXSTAT_RXDR_MASK);
2902
2903 if (rxstat & ATW_RXSTAT_OWN)
2904 break; /* We have processed all receive buffers. */
2905
2906 DPRINTF3(sc,
2907 ("%s: rx stat %08x rssi %08x buf1 %08x buf2 %08x\n",
2908 sc->sc_dev.dv_xname,
2909 le32toh(sc->sc_rxdescs[i].ar_stat),
2910 le32toh(sc->sc_rxdescs[i].ar_rssi),
2911 le32toh(sc->sc_rxdescs[i].ar_buf1),
2912 le32toh(sc->sc_rxdescs[i].ar_buf2)));
2913
2914 /*
2915 * Make sure the packet fits in one buffer. This should
2916 * always be the case.
2917 */
2918 if ((rxstat & (ATW_RXSTAT_FS|ATW_RXSTAT_LS)) !=
2919 (ATW_RXSTAT_FS|ATW_RXSTAT_LS)) {
2920 printf("%s: incoming packet spilled, resetting\n",
2921 sc->sc_dev.dv_xname);
2922 (void)atw_init(ifp);
2923 return;
2924 }
2925
2926 /*
2927 * If an error occurred, update stats, clear the status
2928 * word, and leave the packet buffer in place. It will
2929 * simply be reused the next time the ring comes around.
2930 * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
2931 * error.
2932 */
2933
2934 if ((rxstat & ATW_RXSTAT_ES) != 0 &&
2935 ((sc->sc_ic.ic_ec.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
2936 (rxstat & (ATW_RXSTAT_DE | ATW_RXSTAT_SFDE |
2937 ATW_RXSTAT_SIGE | ATW_RXSTAT_CRC16E |
2938 ATW_RXSTAT_RXTOE | ATW_RXSTAT_CRC32E |
2939 ATW_RXSTAT_ICVE)) != 0)) {
2940 #define PRINTERR(bit, str) \
2941 if (rxstat & (bit)) \
2942 printf("%s: receive error: %s\n", \
2943 sc->sc_dev.dv_xname, str)
2944 ifp->if_ierrors++;
2945 PRINTERR(ATW_RXSTAT_DE, "descriptor error");
2946 PRINTERR(ATW_RXSTAT_SFDE, "PLCP SFD error");
2947 PRINTERR(ATW_RXSTAT_SIGE, "PLCP signal error");
2948 PRINTERR(ATW_RXSTAT_CRC16E, "PLCP CRC16 error");
2949 PRINTERR(ATW_RXSTAT_RXTOE, "time-out");
2950 PRINTERR(ATW_RXSTAT_CRC32E, "FCS error");
2951 PRINTERR(ATW_RXSTAT_ICVE, "WEP ICV error");
2952 #undef PRINTERR
2953 ATW_INIT_RXDESC(sc, i);
2954 continue;
2955 }
2956
2957 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2958 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2959
2960 /*
2961 * No errors; receive the packet. Note the ADM8211
2962 * includes the CRC in promiscuous mode.
2963 */
2964 len = MASK_AND_RSHIFT(rxstat, ATW_RXSTAT_FL_MASK);
2965
2966 /*
2967 * Allocate a new mbuf cluster. If that fails, we are
2968 * out of memory, and must drop the packet and recycle
2969 * the buffer that's already attached to this descriptor.
2970 */
2971 m = rxs->rxs_mbuf;
2972 if (atw_add_rxbuf(sc, i) != 0) {
2973 ifp->if_ierrors++;
2974 ATW_INIT_RXDESC(sc, i);
2975 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2976 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2977 continue;
2978 }
2979
2980 ifp->if_ipackets++;
2981 if (sc->sc_opmode & ATW_NAR_PR)
2982 m->m_flags |= M_HASFCS;
2983 m->m_pkthdr.rcvif = ifp;
2984 m->m_pkthdr.len = m->m_len = MIN(m->m_ext.ext_size, len);
2985
2986 if (rate0 >= sizeof(rate_tbl) / sizeof(rate_tbl[0]))
2987 rate = 0;
2988 else
2989 rate = rate_tbl[rate0];
2990
2991 #if NBPFILTER > 0
2992 /* Pass this up to any BPF listeners. */
2993 if (sc->sc_radiobpf != NULL) {
2994 struct atw_rx_radiotap_header *tap = &sc->sc_rxtap;
2995
2996 tap->ar_rate = rate;
2997 tap->ar_chan_freq = ic->ic_bss->ni_chan->ic_freq;
2998 tap->ar_chan_flags = ic->ic_bss->ni_chan->ic_flags;
2999
3000 /* TBD verify units are dB */
3001 tap->ar_antsignal = (int)rssi;
3002 /* TBD tap->ar_flags */
3003
3004 bpf_mtap2(sc->sc_radiobpf, (caddr_t)tap,
3005 tap->ar_ihdr.it_len, m);
3006 }
3007 #endif /* NPBFILTER > 0 */
3008
3009 wh = mtod(m, struct ieee80211_frame *);
3010 ni = ieee80211_find_rxnode(ic, wh);
3011 ieee80211_input(ifp, m, ni, (int)rssi, 0);
3012 /*
3013 * The frame may have caused the node to be marked for
3014 * reclamation (e.g. in response to a DEAUTH message)
3015 * so use free_node here instead of unref_node.
3016 */
3017 if (ni == ic->ic_bss)
3018 ieee80211_unref_node(&ni);
3019 else
3020 ieee80211_free_node(ic, ni);
3021 }
3022
3023 /* Update the receive pointer. */
3024 sc->sc_rxptr = i;
3025 }
3026
3027 /*
3028 * atw_txintr:
3029 *
3030 * Helper; handle transmit interrupts.
3031 */
3032 void
3033 atw_txintr(struct atw_softc *sc)
3034 {
3035 #define TXSTAT_ERRMASK (ATW_TXSTAT_TUF | ATW_TXSTAT_TLT | ATW_TXSTAT_TRT | \
3036 ATW_TXSTAT_TRO | ATW_TXSTAT_SOFBR)
3037 #define TXSTAT_FMT "\20\31ATW_TXSTAT_SOFBR\32ATW_TXSTAT_TRO\33ATW_TXSTAT_TUF" \
3038 "\34ATW_TXSTAT_TRT\35ATW_TXSTAT_TLT"
3039
3040 static char txstat_buf[sizeof("ffffffff<>" TXSTAT_FMT)];
3041 struct ifnet *ifp = &sc->sc_ic.ic_if;
3042 struct atw_txsoft *txs;
3043 u_int32_t txstat;
3044
3045 DPRINTF3(sc, ("%s: atw_txintr: sc_flags 0x%08x\n",
3046 sc->sc_dev.dv_xname, sc->sc_flags));
3047
3048 ifp->if_flags &= ~IFF_OACTIVE;
3049
3050 /*
3051 * Go through our Tx list and free mbufs for those
3052 * frames that have been transmitted.
3053 */
3054 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
3055 ATW_CDTXSYNC(sc, txs->txs_lastdesc, 1,
3056 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3057
3058 #ifdef ATW_DEBUG
3059 if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
3060 int i;
3061 printf(" txsoft %p transmit chain:\n", txs);
3062 ATW_CDTXSYNC(sc, txs->txs_firstdesc,
3063 txs->txs_ndescs - 1,
3064 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3065 for (i = txs->txs_firstdesc;; i = ATW_NEXTTX(i)) {
3066 printf(" descriptor %d:\n", i);
3067 printf(" at_status: 0x%08x\n",
3068 le32toh(sc->sc_txdescs[i].at_stat));
3069 printf(" at_flags: 0x%08x\n",
3070 le32toh(sc->sc_txdescs[i].at_flags));
3071 printf(" at_buf1: 0x%08x\n",
3072 le32toh(sc->sc_txdescs[i].at_buf1));
3073 printf(" at_buf2: 0x%08x\n",
3074 le32toh(sc->sc_txdescs[i].at_buf2));
3075 if (i == txs->txs_lastdesc)
3076 break;
3077 }
3078 }
3079 #endif
3080
3081 txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].at_stat);
3082 if (txstat & ATW_TXSTAT_OWN)
3083 break;
3084
3085 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
3086
3087 sc->sc_txfree += txs->txs_ndescs;
3088
3089 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
3090 0, txs->txs_dmamap->dm_mapsize,
3091 BUS_DMASYNC_POSTWRITE);
3092 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
3093 m_freem(txs->txs_mbuf);
3094 txs->txs_mbuf = NULL;
3095
3096 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
3097
3098 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
3099 (txstat & TXSTAT_ERRMASK) != 0) {
3100 bitmask_snprintf(txstat & TXSTAT_ERRMASK, TXSTAT_FMT,
3101 txstat_buf, sizeof(txstat_buf));
3102 printf("%s: txstat %s %d\n", sc->sc_dev.dv_xname,
3103 txstat_buf,
3104 MASK_AND_RSHIFT(txstat, ATW_TXSTAT_ARC_MASK));
3105 }
3106
3107 /*
3108 * Check for errors and collisions.
3109 */
3110 if (txstat & ATW_TXSTAT_TUF)
3111 sc->sc_stats.ts_tx_tuf++;
3112 if (txstat & ATW_TXSTAT_TLT)
3113 sc->sc_stats.ts_tx_tlt++;
3114 if (txstat & ATW_TXSTAT_TRT)
3115 sc->sc_stats.ts_tx_trt++;
3116 if (txstat & ATW_TXSTAT_TRO)
3117 sc->sc_stats.ts_tx_tro++;
3118 if (txstat & ATW_TXSTAT_SOFBR) {
3119 sc->sc_stats.ts_tx_sofbr++;
3120 }
3121
3122 if ((txstat & ATW_TXSTAT_ES) == 0)
3123 ifp->if_collisions +=
3124 MASK_AND_RSHIFT(txstat, ATW_TXSTAT_ARC_MASK);
3125 else
3126 ifp->if_oerrors++;
3127
3128 ifp->if_opackets++;
3129 }
3130
3131 /*
3132 * If there are no more pending transmissions, cancel the watchdog
3133 * timer.
3134 */
3135 if (txs == NULL)
3136 sc->sc_tx_timer = 0;
3137 #undef TXSTAT_ERRMASK
3138 #undef TXSTAT_FMT
3139 }
3140
3141 /*
3142 * atw_watchdog: [ifnet interface function]
3143 *
3144 * Watchdog timer handler.
3145 */
3146 void
3147 atw_watchdog(struct ifnet *ifp)
3148 {
3149 struct atw_softc *sc = ifp->if_softc;
3150 struct ieee80211com *ic = &sc->sc_ic;
3151
3152 ifp->if_timer = 0;
3153 if (ATW_IS_ENABLED(sc) == 0)
3154 return;
3155
3156 if (sc->sc_rescan_timer) {
3157 if (--sc->sc_rescan_timer == 0)
3158 (void)ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3159 }
3160 if (sc->sc_tx_timer) {
3161 if (--sc->sc_tx_timer == 0 &&
3162 !SIMPLEQ_EMPTY(&sc->sc_txdirtyq)) {
3163 printf("%s: transmit timeout\n", ifp->if_xname);
3164 ifp->if_oerrors++;
3165 (void)atw_init(ifp);
3166 atw_start(ifp);
3167 }
3168 }
3169 if (sc->sc_tx_timer != 0 || sc->sc_rescan_timer != 0)
3170 ifp->if_timer = 1;
3171 ieee80211_watchdog(ifp);
3172 }
3173
3174 /* Compute the 802.11 Duration field and the PLCP Length fields for
3175 * a len-byte frame (HEADER + PAYLOAD + FCS) sent at rate * 500Kbps.
3176 * Write the fields to the ADM8211 Tx header, frm.
3177 *
3178 * TBD use the fragmentation threshold to find the right duration for
3179 * the first & last fragments.
3180 *
3181 * TBD make certain of the duration fields applied by the ADM8211 to each
3182 * fragment. I think that the ADM8211 knows how to subtract the CTS
3183 * duration when ATW_HDRCTL_RTSCTS is clear; that is why I add it regardless.
3184 * I also think that the ADM8211 does *some* arithmetic for us, because
3185 * otherwise I think we would have to set a first duration for CTS/first
3186 * fragment, a second duration for fragments between the first and the
3187 * last, and a third duration for the last fragment.
3188 *
3189 * TBD make certain that duration fields reflect addition of FCS/WEP
3190 * and correct duration arithmetic as necessary.
3191 */
3192 static void
3193 atw_frame_setdurs(struct atw_softc *sc, struct atw_frame *frm, int rate,
3194 int len)
3195 {
3196 int remainder;
3197
3198 /* deal also with encrypted fragments */
3199 if (frm->atw_hdrctl & htole16(ATW_HDRCTL_WEP)) {
3200 DPRINTF2(sc, ("%s: atw_frame_setdurs len += 8\n",
3201 sc->sc_dev.dv_xname));
3202 len += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
3203 IEEE80211_WEP_CRCLEN;
3204 }
3205
3206 /* 802.11 Duration Field for CTS/Data/ACK sequence minus FCS & WEP
3207 * duration (XXX added by MAC?).
3208 */
3209 frm->atw_head_dur = (16 * (len - IEEE80211_CRC_LEN)) / rate;
3210 remainder = (16 * (len - IEEE80211_CRC_LEN)) % rate;
3211
3212 if (rate <= 4)
3213 /* 1-2Mbps WLAN: send ACK/CTS at 1Mbps */
3214 frm->atw_head_dur += 3 * (IEEE80211_DUR_DS_SIFS +
3215 IEEE80211_DUR_DS_SHORT_PREAMBLE +
3216 IEEE80211_DUR_DS_FAST_PLCPHDR) +
3217 IEEE80211_DUR_DS_SLOW_CTS + IEEE80211_DUR_DS_SLOW_ACK;
3218 else
3219 /* 5-11Mbps WLAN: send ACK/CTS at 2Mbps */
3220 frm->atw_head_dur += 3 * (IEEE80211_DUR_DS_SIFS +
3221 IEEE80211_DUR_DS_SHORT_PREAMBLE +
3222 IEEE80211_DUR_DS_FAST_PLCPHDR) +
3223 IEEE80211_DUR_DS_FAST_CTS + IEEE80211_DUR_DS_FAST_ACK;
3224
3225 /* lengthen duration if long preamble */
3226 if ((sc->sc_flags & ATWF_SHORT_PREAMBLE) == 0)
3227 frm->atw_head_dur +=
3228 3 * (IEEE80211_DUR_DS_LONG_PREAMBLE -
3229 IEEE80211_DUR_DS_SHORT_PREAMBLE) +
3230 3 * (IEEE80211_DUR_DS_SLOW_PLCPHDR -
3231 IEEE80211_DUR_DS_FAST_PLCPHDR);
3232
3233 if (remainder != 0)
3234 frm->atw_head_dur++;
3235
3236 if ((atw_voodoo & VOODOO_DUR_2_4_SPECIALCASE) &&
3237 (rate == 2 || rate == 4)) {
3238 /* derived from Linux: how could this be right? */
3239 frm->atw_head_plcplen = frm->atw_head_dur;
3240 } else {
3241 frm->atw_head_plcplen = (16 * len) / rate;
3242 remainder = (80 * len) % (rate * 5);
3243
3244 if (remainder != 0) {
3245 frm->atw_head_plcplen++;
3246
3247 /* XXX magic */
3248 if ((atw_voodoo & VOODOO_DUR_11_ROUNDING) &&
3249 rate == 22 && remainder <= 30)
3250 frm->atw_head_plcplen |= 0x8000;
3251 }
3252 }
3253 frm->atw_tail_plcplen = frm->atw_head_plcplen =
3254 htole16(frm->atw_head_plcplen);
3255 frm->atw_tail_dur = frm->atw_head_dur = htole16(frm->atw_head_dur);
3256 }
3257
3258 #ifdef ATW_DEBUG
3259 static void
3260 atw_dump_pkt(struct ifnet *ifp, struct mbuf *m0)
3261 {
3262 struct atw_softc *sc = ifp->if_softc;
3263 struct mbuf *m;
3264 int i, noctets = 0;
3265
3266 printf("%s: %d-byte packet\n", sc->sc_dev.dv_xname,
3267 m0->m_pkthdr.len);
3268
3269 for (m = m0; m; m = m->m_next) {
3270 if (m->m_len == 0)
3271 continue;
3272 for (i = 0; i < m->m_len; i++) {
3273 printf(" %02x", ((u_int8_t*)m->m_data)[i]);
3274 if (++noctets % 24 == 0)
3275 printf("\n");
3276 }
3277 }
3278 printf("%s%s: %d bytes emitted\n",
3279 (noctets % 24 != 0) ? "\n" : "", sc->sc_dev.dv_xname, noctets);
3280 }
3281 #endif /* ATW_DEBUG */
3282
3283 /*
3284 * atw_start: [ifnet interface function]
3285 *
3286 * Start packet transmission on the interface.
3287 */
3288 void
3289 atw_start(struct ifnet *ifp)
3290 {
3291 struct atw_softc *sc = ifp->if_softc;
3292 struct ieee80211com *ic = &sc->sc_ic;
3293 struct ieee80211_node *ni;
3294 struct ieee80211_frame *wh;
3295 struct atw_frame *hh;
3296 struct mbuf *m0, *m;
3297 struct atw_txsoft *txs, *last_txs;
3298 struct atw_txdesc *txd;
3299 int do_encrypt, rate;
3300 bus_dmamap_t dmamap;
3301 int ctl, error, firsttx, nexttx, lasttx = -1, first, ofree, seg;
3302
3303 DPRINTF2(sc, ("%s: atw_start: sc_flags 0x%08x, if_flags 0x%08x\n",
3304 sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
3305
3306 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
3307 return;
3308
3309 /*
3310 * Remember the previous number of free descriptors and
3311 * the first descriptor we'll use.
3312 */
3313 ofree = sc->sc_txfree;
3314 firsttx = sc->sc_txnext;
3315
3316 DPRINTF2(sc, ("%s: atw_start: txfree %d, txnext %d\n",
3317 sc->sc_dev.dv_xname, ofree, firsttx));
3318
3319 /*
3320 * Loop through the send queue, setting up transmit descriptors
3321 * until we drain the queue, or use up all available transmit
3322 * descriptors.
3323 */
3324 while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
3325 sc->sc_txfree != 0) {
3326
3327 /*
3328 * Grab a packet off the management queue, if it
3329 * is not empty. Otherwise, from the data queue.
3330 */
3331 IF_DEQUEUE(&ic->ic_mgtq, m0);
3332 if (m0 != NULL) {
3333 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
3334 m0->m_pkthdr.rcvif = NULL;
3335 } else {
3336 /* send no data packets until we are associated */
3337 if (ic->ic_state != IEEE80211_S_RUN)
3338 break;
3339 IFQ_DEQUEUE(&ifp->if_snd, m0);
3340 if (m0 == NULL)
3341 break;
3342 #if NBPFILTER > 0
3343 if (ifp->if_bpf != NULL)
3344 bpf_mtap(ifp->if_bpf, m0);
3345 #endif /* NBPFILTER > 0 */
3346 if ((m0 = ieee80211_encap(ifp, m0, &ni)) == NULL) {
3347 ifp->if_oerrors++;
3348 break;
3349 }
3350 }
3351
3352 rate = MAX(ieee80211_get_rate(ic), 2);
3353
3354 #if NBPFILTER > 0
3355 /*
3356 * Pass the packet to any BPF listeners.
3357 */
3358 if (ic->ic_rawbpf != NULL)
3359 bpf_mtap((caddr_t)ic->ic_rawbpf, m0);
3360
3361 if (sc->sc_radiobpf != NULL) {
3362 struct atw_tx_radiotap_header *tap = &sc->sc_txtap;
3363
3364 tap->at_rate = rate;
3365 tap->at_chan_freq = ic->ic_bss->ni_chan->ic_freq;
3366 tap->at_chan_flags = ic->ic_bss->ni_chan->ic_flags;
3367
3368 /* TBD tap->at_flags */
3369
3370 bpf_mtap2(sc->sc_radiobpf, (caddr_t)tap,
3371 tap->at_ihdr.it_len, m0);
3372 }
3373 #endif /* NBPFILTER > 0 */
3374
3375 M_PREPEND(m0, offsetof(struct atw_frame, atw_ihdr), M_DONTWAIT);
3376
3377 if (ni != NULL && ni != ic->ic_bss)
3378 ieee80211_free_node(ic, ni);
3379
3380 if (m0 == NULL) {
3381 ifp->if_oerrors++;
3382 break;
3383 }
3384
3385 /* just to make sure. */
3386 m0 = m_pullup(m0, sizeof(struct atw_frame));
3387
3388 if (m0 == NULL) {
3389 ifp->if_oerrors++;
3390 break;
3391 }
3392
3393 hh = mtod(m0, struct atw_frame *);
3394 wh = &hh->atw_ihdr;
3395
3396 do_encrypt = ((wh->i_fc[1] & IEEE80211_FC1_WEP) != 0) ? 1 : 0;
3397
3398 /* Copy everything we need from the 802.11 header:
3399 * Frame Control; address 1, address 3, or addresses
3400 * 3 and 4. NIC fills in BSSID, SA.
3401 */
3402 if (wh->i_fc[1] & IEEE80211_FC1_DIR_TODS) {
3403 if (wh->i_fc[1] & IEEE80211_FC1_DIR_FROMDS)
3404 panic("%s: illegal WDS frame",
3405 sc->sc_dev.dv_xname);
3406 memcpy(hh->atw_dst, wh->i_addr3, IEEE80211_ADDR_LEN);
3407 } else
3408 memcpy(hh->atw_dst, wh->i_addr1, IEEE80211_ADDR_LEN);
3409
3410 *(u_int16_t*)hh->atw_fc = *(u_int16_t*)wh->i_fc;
3411
3412 /* initialize remaining Tx parameters */
3413 memset(&hh->u, 0, sizeof(hh->u));
3414
3415 hh->atw_rate = rate * 5;
3416 /* XXX this could be incorrect if M_FCS. _encap should
3417 * probably strip FCS just in case it sticks around in
3418 * bridged packets.
3419 */
3420 hh->atw_service = IEEE80211_PLCP_SERVICE; /* XXX guess */
3421 hh->atw_paylen = htole16(m0->m_pkthdr.len -
3422 sizeof(struct atw_frame));
3423
3424 hh->atw_fragthr = htole16(ATW_FRAGTHR_FRAGTHR_MASK);
3425 hh->atw_rtylmt = 3;
3426 hh->atw_hdrctl = htole16(ATW_HDRCTL_UNKNOWN1);
3427 if (do_encrypt) {
3428 hh->atw_hdrctl |= htole16(ATW_HDRCTL_WEP);
3429 hh->atw_keyid = ic->ic_wep_txkey;
3430 }
3431
3432 /* TBD 4-addr frames */
3433 atw_frame_setdurs(sc, hh, rate,
3434 m0->m_pkthdr.len - sizeof(struct atw_frame) +
3435 sizeof(struct ieee80211_frame) + IEEE80211_CRC_LEN);
3436
3437 /* never fragment multicast frames */
3438 if (IEEE80211_IS_MULTICAST(hh->atw_dst)) {
3439 hh->atw_fragthr = htole16(ATW_FRAGTHR_FRAGTHR_MASK);
3440 } else if (sc->sc_flags & ATWF_RTSCTS) {
3441 hh->atw_hdrctl |= htole16(ATW_HDRCTL_RTSCTS);
3442 }
3443
3444 #ifdef ATW_DEBUG
3445 hh->atw_fragnum = 0;
3446
3447 if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
3448 printf("%s: dst = %s, rate = 0x%02x, "
3449 "service = 0x%02x, paylen = 0x%04x\n",
3450 sc->sc_dev.dv_xname, ether_sprintf(hh->atw_dst),
3451 hh->atw_rate, hh->atw_service, hh->atw_paylen);
3452
3453 printf("%s: fc[0] = 0x%02x, fc[1] = 0x%02x, "
3454 "dur1 = 0x%04x, dur2 = 0x%04x, "
3455 "dur3 = 0x%04x, rts_dur = 0x%04x\n",
3456 sc->sc_dev.dv_xname, hh->atw_fc[0], hh->atw_fc[1],
3457 hh->atw_tail_plcplen, hh->atw_head_plcplen,
3458 hh->atw_tail_dur, hh->atw_head_dur);
3459
3460 printf("%s: hdrctl = 0x%04x, fragthr = 0x%04x, "
3461 "fragnum = 0x%02x, rtylmt = 0x%04x\n",
3462 sc->sc_dev.dv_xname, hh->atw_hdrctl,
3463 hh->atw_fragthr, hh->atw_fragnum, hh->atw_rtylmt);
3464
3465 printf("%s: keyid = %d\n",
3466 sc->sc_dev.dv_xname, hh->atw_keyid);
3467
3468 atw_dump_pkt(ifp, m0);
3469 }
3470 #endif /* ATW_DEBUG */
3471
3472 dmamap = txs->txs_dmamap;
3473
3474 /*
3475 * Load the DMA map. Copy and try (once) again if the packet
3476 * didn't fit in the alloted number of segments.
3477 */
3478 for (first = 1;
3479 (error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
3480 BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 && first;
3481 first = 0) {
3482 MGETHDR(m, M_DONTWAIT, MT_DATA);
3483 if (m == NULL) {
3484 printf("%s: unable to allocate Tx mbuf\n",
3485 sc->sc_dev.dv_xname);
3486 break;
3487 }
3488 if (m0->m_pkthdr.len > MHLEN) {
3489 MCLGET(m, M_DONTWAIT);
3490 if ((m->m_flags & M_EXT) == 0) {
3491 printf("%s: unable to allocate Tx "
3492 "cluster\n", sc->sc_dev.dv_xname);
3493 m_freem(m);
3494 break;
3495 }
3496 }
3497 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
3498 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
3499 m_freem(m0);
3500 m0 = m;
3501 m = NULL;
3502 }
3503 if (error != 0) {
3504 printf("%s: unable to load Tx buffer, "
3505 "error = %d\n", sc->sc_dev.dv_xname, error);
3506 m_freem(m0);
3507 break;
3508 }
3509
3510 /*
3511 * Ensure we have enough descriptors free to describe
3512 * the packet.
3513 */
3514 if (dmamap->dm_nsegs > sc->sc_txfree) {
3515 /*
3516 * Not enough free descriptors to transmit
3517 * this packet. Unload the DMA map and
3518 * drop the packet. Notify the upper layer
3519 * that there are no more slots left.
3520 *
3521 * XXX We could allocate an mbuf and copy, but
3522 * XXX it is worth it?
3523 */
3524 ifp->if_flags |= IFF_OACTIVE;
3525 bus_dmamap_unload(sc->sc_dmat, dmamap);
3526 m_freem(m0);
3527 break;
3528 }
3529
3530 /*
3531 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
3532 */
3533
3534 /* Sync the DMA map. */
3535 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
3536 BUS_DMASYNC_PREWRITE);
3537
3538 /* XXX arbitrary retry limit; 8 because I have seen it in
3539 * use already and maybe 0 means "no tries" !
3540 */
3541 ctl = htole32(LSHIFT(8, ATW_TXCTL_TL_MASK));
3542
3543 DPRINTF2(sc, ("%s: TXDR <- max(10, %d)\n",
3544 sc->sc_dev.dv_xname, rate * 5));
3545 ctl |= htole32(LSHIFT(MAX(10, rate * 5), ATW_TXCTL_TXDR_MASK));
3546
3547 /*
3548 * Initialize the transmit descriptors.
3549 */
3550 for (nexttx = sc->sc_txnext, seg = 0;
3551 seg < dmamap->dm_nsegs;
3552 seg++, nexttx = ATW_NEXTTX(nexttx)) {
3553 /*
3554 * If this is the first descriptor we're
3555 * enqueueing, don't set the OWN bit just
3556 * yet. That could cause a race condition.
3557 * We'll do it below.
3558 */
3559 txd = &sc->sc_txdescs[nexttx];
3560 txd->at_ctl = ctl |
3561 ((nexttx == firsttx) ? 0 : htole32(ATW_TXCTL_OWN));
3562
3563 txd->at_buf1 = htole32(dmamap->dm_segs[seg].ds_addr);
3564 txd->at_flags =
3565 htole32(LSHIFT(dmamap->dm_segs[seg].ds_len,
3566 ATW_TXFLAG_TBS1_MASK)) |
3567 ((nexttx == (ATW_NTXDESC - 1))
3568 ? htole32(ATW_TXFLAG_TER) : 0);
3569 lasttx = nexttx;
3570 }
3571
3572 IASSERT(lasttx != -1, ("bad lastx"));
3573 /* Set `first segment' and `last segment' appropriately. */
3574 sc->sc_txdescs[sc->sc_txnext].at_flags |=
3575 htole32(ATW_TXFLAG_FS);
3576 sc->sc_txdescs[lasttx].at_flags |= htole32(ATW_TXFLAG_LS);
3577
3578 #ifdef ATW_DEBUG
3579 if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
3580 printf(" txsoft %p transmit chain:\n", txs);
3581 for (seg = sc->sc_txnext;; seg = ATW_NEXTTX(seg)) {
3582 printf(" descriptor %d:\n", seg);
3583 printf(" at_ctl: 0x%08x\n",
3584 le32toh(sc->sc_txdescs[seg].at_ctl));
3585 printf(" at_flags: 0x%08x\n",
3586 le32toh(sc->sc_txdescs[seg].at_flags));
3587 printf(" at_buf1: 0x%08x\n",
3588 le32toh(sc->sc_txdescs[seg].at_buf1));
3589 printf(" at_buf2: 0x%08x\n",
3590 le32toh(sc->sc_txdescs[seg].at_buf2));
3591 if (seg == lasttx)
3592 break;
3593 }
3594 }
3595 #endif
3596
3597 /* Sync the descriptors we're using. */
3598 ATW_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
3599 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3600
3601 /*
3602 * Store a pointer to the packet so we can free it later,
3603 * and remember what txdirty will be once the packet is
3604 * done.
3605 */
3606 txs->txs_mbuf = m0;
3607 txs->txs_firstdesc = sc->sc_txnext;
3608 txs->txs_lastdesc = lasttx;
3609 txs->txs_ndescs = dmamap->dm_nsegs;
3610
3611 /* Advance the tx pointer. */
3612 sc->sc_txfree -= dmamap->dm_nsegs;
3613 sc->sc_txnext = nexttx;
3614
3615 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
3616 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
3617
3618 last_txs = txs;
3619 }
3620
3621 if (txs == NULL || sc->sc_txfree == 0) {
3622 /* No more slots left; notify upper layer. */
3623 ifp->if_flags |= IFF_OACTIVE;
3624 }
3625
3626 if (sc->sc_txfree != ofree) {
3627 DPRINTF2(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
3628 sc->sc_dev.dv_xname, lasttx, firsttx));
3629 /*
3630 * Cause a transmit interrupt to happen on the
3631 * last packet we enqueued.
3632 */
3633 sc->sc_txdescs[lasttx].at_flags |= htole32(ATW_TXFLAG_IC);
3634 ATW_CDTXSYNC(sc, lasttx, 1,
3635 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3636
3637 /*
3638 * The entire packet chain is set up. Give the
3639 * first descriptor to the chip now.
3640 */
3641 sc->sc_txdescs[firsttx].at_ctl |= htole32(ATW_TXCTL_OWN);
3642 ATW_CDTXSYNC(sc, firsttx, 1,
3643 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3644
3645 /* Wake up the transmitter. */
3646 ATW_WRITE(sc, ATW_TDR, 0x1);
3647
3648 /* Set a watchdog timer in case the chip flakes out. */
3649 sc->sc_tx_timer = 5;
3650 ifp->if_timer = 1;
3651 }
3652 }
3653
3654 /*
3655 * atw_power:
3656 *
3657 * Power management (suspend/resume) hook.
3658 */
3659 void
3660 atw_power(int why, void *arg)
3661 {
3662 struct atw_softc *sc = arg;
3663 struct ifnet *ifp = &sc->sc_ic.ic_if;
3664 int s;
3665
3666 DPRINTF(sc, ("%s: atw_power(%d,)\n", sc->sc_dev.dv_xname, why));
3667
3668 s = splnet();
3669 switch (why) {
3670 case PWR_STANDBY:
3671 /* XXX do nothing. */
3672 break;
3673 case PWR_SUSPEND:
3674 atw_stop(ifp, 0);
3675 if (sc->sc_power != NULL)
3676 (*sc->sc_power)(sc, why);
3677 break;
3678 case PWR_RESUME:
3679 if (ifp->if_flags & IFF_UP) {
3680 if (sc->sc_power != NULL)
3681 (*sc->sc_power)(sc, why);
3682 atw_init(ifp);
3683 }
3684 break;
3685 case PWR_SOFTSUSPEND:
3686 case PWR_SOFTSTANDBY:
3687 case PWR_SOFTRESUME:
3688 break;
3689 }
3690 splx(s);
3691 }
3692
3693 /*
3694 * atw_ioctl: [ifnet interface function]
3695 *
3696 * Handle control requests from the operator.
3697 */
3698 int
3699 atw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3700 {
3701 struct atw_softc *sc = ifp->if_softc;
3702 struct ifreq *ifr = (struct ifreq *)data;
3703 int s, error = 0;
3704
3705 /* XXX monkey see, monkey do. comes from wi_ioctl. */
3706 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
3707 return ENXIO;
3708
3709 s = splnet();
3710
3711 switch (cmd) {
3712 case SIOCSIFFLAGS:
3713 if (ifp->if_flags & IFF_UP) {
3714 if (ATW_IS_ENABLED(sc)) {
3715 /*
3716 * To avoid rescanning another access point,
3717 * do not call atw_init() here. Instead,
3718 * only reflect media settings.
3719 */
3720 atw_filter_setup(sc);
3721 } else
3722 error = atw_init(ifp);
3723 } else if (ATW_IS_ENABLED(sc))
3724 atw_stop(ifp, 1);
3725 break;
3726 case SIOCADDMULTI:
3727 case SIOCDELMULTI:
3728 error = (cmd == SIOCADDMULTI) ?
3729 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
3730 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
3731 if (error == ENETRESET) {
3732 if (ATW_IS_ENABLED(sc))
3733 atw_filter_setup(sc); /* do not rescan */
3734 error = 0;
3735 }
3736 break;
3737 default:
3738 error = ieee80211_ioctl(ifp, cmd, data);
3739 if (error == ENETRESET) {
3740 if (ATW_IS_ENABLED(sc))
3741 error = atw_init(ifp);
3742 else
3743 error = 0;
3744 }
3745 break;
3746 }
3747
3748 /* Try to get more packets going. */
3749 if (ATW_IS_ENABLED(sc))
3750 atw_start(ifp);
3751
3752 splx(s);
3753 return (error);
3754 }
3755
3756 static int
3757 atw_media_change(struct ifnet *ifp)
3758 {
3759 int error;
3760
3761 error = ieee80211_media_change(ifp);
3762 if (error == ENETRESET) {
3763 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
3764 (IFF_RUNNING|IFF_UP))
3765 atw_init(ifp); /* XXX lose error */
3766 error = 0;
3767 }
3768 return error;
3769 }
3770
3771 static void
3772 atw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3773 {
3774 struct atw_softc *sc = ifp->if_softc;
3775
3776 if (ATW_IS_ENABLED(sc) == 0) {
3777 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
3778 imr->ifm_status = 0;
3779 return;
3780 }
3781 ieee80211_media_status(ifp, imr);
3782 }
3783