awi.c revision 1.58 1 /* $NetBSD: awi.c,v 1.58 2003/11/02 11:07:45 wiz Exp $ */
2
3 /*-
4 * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
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 * Driver for AMD 802.11 firmware.
40 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
41 *
42 * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
43 */
44
45 /*
46 * todo:
47 * - flush tx queue on resynch.
48 * - clear oactive on "down".
49 * - rewrite copy-into-mbuf code
50 * - mgmt state machine gets stuck retransmitting assoc requests.
51 * - multicast filter.
52 * - fix device reset so it's more likely to work
53 * - show status goo through ifmedia.
54 *
55 * more todo:
56 * - deal with more 802.11 frames.
57 * - send reassoc request
58 * - deal with reassoc response
59 * - send/deal with disassociation
60 * - deal with "full" access points (no room for me).
61 * - power save mode
62 *
63 * later:
64 * - SSID preferences
65 * - need ioctls for poking at the MIBs
66 * - implement ad-hoc mode (including bss creation).
67 * - decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
68 * (focus on inf. mode since that will be needed for ietf)
69 * - deal with DH vs. FH versions of the card
70 * - deal with faster cards (2mb/s)
71 * - ?WEP goo (mmm, rc4) (it looks not particularly useful).
72 * - ifmedia revision.
73 * - common 802.11 mibish things.
74 * - common 802.11 media layer.
75 */
76
77 /*
78 * Driver for AMD 802.11 PCnetMobile firmware.
79 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
80 *
81 * The initial version of the driver was written by
82 * Bill Sommerfeld <sommerfeld (at) netbsd.org>.
83 * Then the driver module completely rewritten to support cards with DS phy
84 * and to support adhoc mode by Atsushi Onoe <onoe (at) netbsd.org>
85 */
86
87 #include <sys/cdefs.h>
88 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.58 2003/11/02 11:07:45 wiz Exp $");
89
90 #include "opt_inet.h"
91 #include "bpfilter.h"
92
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/kernel.h>
96 #include <sys/mbuf.h>
97 #include <sys/malloc.h>
98 #include <sys/proc.h>
99 #include <sys/socket.h>
100 #include <sys/sockio.h>
101 #include <sys/errno.h>
102 #include <sys/device.h>
103
104 #include <net/if.h>
105 #include <net/if_dl.h>
106 #include <net/if_ether.h>
107 #include <net/if_media.h>
108 #include <net/if_llc.h>
109
110 #include <net80211/ieee80211_var.h>
111 #include <net80211/ieee80211_compat.h>
112
113 #ifdef INET
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #ifdef __NetBSD__
117 #include <netinet/if_inarp.h>
118 #else
119 #include <netinet/if_ether.h>
120 #endif
121 #endif
122
123 #if NBPFILTER > 0
124 #include <net/bpf.h>
125 #endif
126
127 #include <machine/cpu.h>
128 #include <machine/bus.h>
129 #include <machine/intr.h>
130
131 #include <dev/ic/am79c930reg.h>
132 #include <dev/ic/am79c930var.h>
133 #include <dev/ic/awireg.h>
134 #include <dev/ic/awivar.h>
135
136 static int awi_init(struct ifnet *);
137 static void awi_stop(struct ifnet *, int);
138 static void awi_start(struct ifnet *);
139 static void awi_watchdog(struct ifnet *);
140 static int awi_ioctl(struct ifnet *, u_long, caddr_t);
141 static int awi_media_change(struct ifnet *);
142 static void awi_media_status(struct ifnet *, struct ifmediareq *);
143 static int awi_mode_init(struct awi_softc *);
144 static void awi_rx_int(struct awi_softc *);
145 static void awi_tx_int(struct awi_softc *);
146 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
147 static int awi_hw_init(struct awi_softc *);
148 static int awi_init_mibs(struct awi_softc *);
149 static int awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
150 static int awi_cmd(struct awi_softc *, u_int8_t, int);
151 static int awi_cmd_wait(struct awi_softc *);
152 static void awi_cmd_done(struct awi_softc *);
153 static int awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
154 static int awi_lock(struct awi_softc *);
155 static void awi_unlock(struct awi_softc *);
156 static int awi_intr_lock(struct awi_softc *);
157 static void awi_intr_unlock(struct awi_softc *);
158 static int awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
159 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
160 struct ieee80211_node *, int, int, u_int32_t);
161 static int awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
162 int);
163 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
164 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
165
166 /* unaligned little endian access */
167 #define LE_READ_2(p) \
168 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))
169 #define LE_READ_4(p) \
170 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
171 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
172 #define LE_WRITE_2(p, v) \
173 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
174 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)))
175 #define LE_WRITE_4(p, v) \
176 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
177 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)), \
178 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)), \
179 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
180
181 struct awi_chanset awi_chanset[] = {
182 /* PHY type domain min max def */
183 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP, 6, 17, 6 },
184 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES, 0, 26, 1 },
185 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR, 0, 32, 1 },
186 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US, 0, 77, 1 },
187 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA, 0, 77, 1 },
188 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU, 0, 77, 1 },
189 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
190 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
191 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
192 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US, 1, 11, 3 },
193 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA, 1, 11, 3 },
194 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU, 1, 13, 3 },
195 { 0, 0 }
196 };
197
198 #ifdef AWI_DEBUG
199 int awi_debug;
200
201 #define DPRINTF(X) if (awi_debug) printf X
202 #define DPRINTF2(X) if (awi_debug > 1) printf X
203 #else
204 #define DPRINTF(X)
205 #define DPRINTF2(X)
206 #endif
207
208 int
209 awi_attach(struct awi_softc *sc)
210 {
211 struct ieee80211com *ic = &sc->sc_ic;
212 struct ifnet *ifp = &ic->ic_if;
213 int s, i, error, nrate;
214 int mword;
215
216 s = splnet();
217 sc->sc_busy = 1;
218 sc->sc_substate = AWI_ST_NONE;
219 if ((error = awi_hw_init(sc)) != 0) {
220 sc->sc_invalid = 1;
221 splx(s);
222 return error;
223 }
224 error = awi_init_mibs(sc);
225 if (error != 0) {
226 sc->sc_invalid = 1;
227 splx(s);
228 return error;
229 }
230 ifp->if_softc = sc;
231 ifp->if_flags =
232 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
233 ifp->if_ioctl = awi_ioctl;
234 ifp->if_start = awi_start;
235 ifp->if_init = awi_init;
236 ifp->if_stop = awi_stop;
237 ifp->if_watchdog = awi_watchdog;
238 IFQ_SET_READY(&ifp->if_snd);
239 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
240
241 ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
242 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
243 ic->ic_phytype = IEEE80211_T_FH;
244 else {
245 ic->ic_phytype = IEEE80211_T_DS;
246 ic->ic_caps |= IEEE80211_C_AHDEMO;
247 }
248 ic->ic_opmode = IEEE80211_M_STA;
249 nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
250 memcpy(ic->ic_sup_rates[IEEE80211_MODE_FH].rs_rates,
251 sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
252 ic->ic_sup_rates[IEEE80211_MODE_FH].rs_nrates = nrate;
253 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
254
255 printf("%s: IEEE802.11 %s (firmware %s)\n", sc->sc_dev.dv_xname,
256 (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
257 printf("%s: 802.11 address: %s\n", sc->sc_dev.dv_xname,
258 ether_sprintf(ic->ic_myaddr));
259
260 if_attach(ifp);
261 ieee80211_ifattach(ifp);
262
263 sc->sc_newstate = ic->ic_newstate;
264 ic->ic_newstate = awi_newstate;
265 #if 0
266 ic->ic_chancheck = awi_chan_check;
267 #endif
268
269 sc->sc_recv_mgmt = ic->ic_recv_mgmt;
270 ic->ic_recv_mgmt = awi_recv_mgmt;
271
272 sc->sc_send_mgmt = ic->ic_send_mgmt;
273 ic->ic_send_mgmt = awi_send_mgmt;
274
275 ieee80211_media_init(ifp, awi_media_change, awi_media_status);
276
277 /* Melco compatibility mode. */
278 #define ADD(s, o) ifmedia_add(&ic->ic_media, \
279 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
280 ADD(IFM_AUTO, IFM_FLAG0);
281
282 for (i = 0; i < nrate; i++) {
283 mword = ieee80211_rate2media(ic,
284 ic->ic_sup_rates[IEEE80211_MODE_FH].rs_rates[i],
285 (ic->ic_phytype == IEEE80211_T_FH) ? IEEE80211_MODE_FH
286 : IEEE80211_MODE_11B);
287 if (mword == 0)
288 continue;
289 ADD(mword, IFM_FLAG0);
290 }
291 #undef ADD
292
293 if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
294 printf("%s: WARNING: unable to establish shutdown hook\n",
295 sc->sc_dev.dv_xname);
296 if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
297 printf("%s: WARNING: unable to establish power hook\n",
298 sc->sc_dev.dv_xname);
299 sc->sc_attached = 1;
300 splx(s);
301
302 /* ready to accept ioctl */
303 awi_unlock(sc);
304
305 return 0;
306 }
307
308 int
309 awi_detach(struct awi_softc *sc)
310 {
311 struct ifnet *ifp = &sc->sc_ic.ic_if;
312 int s;
313
314 if (!sc->sc_attached)
315 return 0;
316
317 s = splnet();
318 sc->sc_invalid = 1;
319 awi_stop(ifp, 1);
320 while (sc->sc_sleep_cnt > 0) {
321 wakeup(sc);
322 (void)tsleep(sc, PWAIT, "awidet", 1);
323 }
324 ieee80211_ifdetach(ifp);
325 if_detach(ifp);
326 shutdownhook_disestablish(sc->sc_sdhook);
327 powerhook_disestablish(sc->sc_powerhook);
328 splx(s);
329 return 0;
330 }
331
332 int
333 awi_activate(struct device *self, enum devact act)
334 {
335 struct awi_softc *sc = (struct awi_softc *)self;
336 struct ifnet *ifp = &sc->sc_ic.ic_if;
337 int s, error = 0;
338
339 s = splnet();
340 switch (act) {
341 case DVACT_ACTIVATE:
342 error = EOPNOTSUPP;
343 break;
344 case DVACT_DEACTIVATE:
345 sc->sc_invalid = 1;
346 if_deactivate(ifp);
347 break;
348 }
349 splx(s);
350 return error;
351 }
352
353 void
354 awi_power(int why, void *arg)
355 {
356 struct awi_softc *sc = arg;
357 struct ifnet *ifp = &sc->sc_ic.ic_if;
358 int s;
359 int ocansleep;
360
361 DPRINTF(("awi_power: %d\n", why));
362 s = splnet();
363 ocansleep = sc->sc_cansleep;
364 sc->sc_cansleep = 0;
365 switch (why) {
366 case PWR_SUSPEND:
367 case PWR_STANDBY:
368 awi_stop(ifp, 1);
369 break;
370 case PWR_RESUME:
371 if (ifp->if_flags & IFF_UP) {
372 awi_init(ifp);
373 (void)awi_intr(sc); /* make sure */
374 }
375 break;
376 case PWR_SOFTSUSPEND:
377 case PWR_SOFTSTANDBY:
378 case PWR_SOFTRESUME:
379 break;
380 }
381 sc->sc_cansleep = ocansleep;
382 splx(s);
383 }
384
385 void
386 awi_shutdown(void *arg)
387 {
388 struct awi_softc *sc = arg;
389 struct ifnet *ifp = &sc->sc_ic.ic_if;
390
391 if (sc->sc_attached)
392 awi_stop(ifp, 1);
393 }
394
395 int
396 awi_intr(void *arg)
397 {
398 struct awi_softc *sc = arg;
399 u_int16_t status;
400 int handled = 0, ocansleep;
401 #ifdef AWI_DEBUG
402 static const char *intname[] = {
403 "CMD", "RX", "TX", "SCAN_CMPLT",
404 "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
405 "TXDATA", "TXBCAST", "TXPS", "TXCF",
406 "TXMGT", "#13", "RXDATA", "RXMGT"
407 };
408 #endif
409
410 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
411 return 0;
412
413 am79c930_gcr_setbits(&sc->sc_chip,
414 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
415 awi_write_1(sc, AWI_DIS_PWRDN, 1);
416 ocansleep = sc->sc_cansleep;
417 sc->sc_cansleep = 0;
418
419 for (;;) {
420 if (awi_intr_lock(sc) != 0)
421 break;
422 status = awi_read_1(sc, AWI_INTSTAT);
423 awi_write_1(sc, AWI_INTSTAT, 0);
424 awi_write_1(sc, AWI_INTSTAT, 0);
425 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
426 awi_write_1(sc, AWI_INTSTAT2, 0);
427 DELAY(10);
428 awi_intr_unlock(sc);
429 if (!sc->sc_cmd_inprog)
430 status &= ~AWI_INT_CMD; /* make sure */
431 if (status == 0)
432 break;
433 #ifdef AWI_DEBUG
434 if (awi_debug > 1) {
435 int i;
436
437 printf("awi_intr: status 0x%04x", status);
438 for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
439 i++) {
440 if (status & (1 << i))
441 printf(" %s", intname[i]);
442 }
443 printf("\n");
444 }
445 #endif
446 handled = 1;
447 if (status & AWI_INT_RX)
448 awi_rx_int(sc);
449 if (status & AWI_INT_TX)
450 awi_tx_int(sc);
451 if (status & AWI_INT_CMD)
452 awi_cmd_done(sc);
453 if (status & AWI_INT_SCAN_CMPLT) {
454 if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
455 sc->sc_substate == AWI_ST_NONE)
456 ieee80211_next_scan(&sc->sc_ic.ic_if);
457 }
458 }
459 sc->sc_cansleep = ocansleep;
460 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
461 awi_write_1(sc, AWI_DIS_PWRDN, 0);
462 return handled;
463 }
464
465 static int
466 awi_init(struct ifnet *ifp)
467 {
468 struct awi_softc *sc = ifp->if_softc;
469 struct ieee80211com *ic = &sc->sc_ic;
470 struct ieee80211_node *ni = ic->ic_bss;
471 int error;
472
473 DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
474 if (sc->sc_enabled) {
475 awi_stop(ifp, 0);
476 } else {
477 if (sc->sc_enable)
478 (*sc->sc_enable)(sc);
479 sc->sc_enabled = 1;
480 if ((error = awi_hw_init(sc)) != 0) {
481 awi_stop(ifp, 1);
482 return error;
483 }
484 }
485 ic->ic_state = IEEE80211_S_INIT;
486
487 ic->ic_flags &= ~IEEE80211_F_IBSSON;
488 switch (ic->ic_opmode) {
489 case IEEE80211_M_STA:
490 sc->sc_mib_local.Network_Mode = 1;
491 sc->sc_mib_local.Acting_as_AP = 0;
492 break;
493 case IEEE80211_M_IBSS:
494 ic->ic_flags |= IEEE80211_F_IBSSON;
495 case IEEE80211_M_AHDEMO:
496 sc->sc_mib_local.Network_Mode = 0;
497 sc->sc_mib_local.Acting_as_AP = 0;
498 break;
499 case IEEE80211_M_HOSTAP:
500 sc->sc_mib_local.Network_Mode = 1;
501 sc->sc_mib_local.Acting_as_AP = 1;
502 break;
503 case IEEE80211_M_MONITOR:
504 return ENODEV;
505 }
506 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
507 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
508 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
509 sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen;
510 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid,
511 ic->ic_des_esslen);
512
513 if ((error = awi_mode_init(sc)) != 0) {
514 DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
515 awi_stop(ifp, 1);
516 return error;
517 }
518
519 /* start transmitter */
520 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
521 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
522 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
523 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
524 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
525 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
526 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
527 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
528 awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
529 awi_write_4(sc, AWI_CA_TX_MGT, 0);
530 awi_write_4(sc, AWI_CA_TX_BCAST, 0);
531 awi_write_4(sc, AWI_CA_TX_PS, 0);
532 awi_write_4(sc, AWI_CA_TX_CF, 0);
533 if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
534 DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
535 awi_stop(ifp, 1);
536 return error;
537 }
538
539 /* start receiver */
540 if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
541 DPRINTF(("awi_init: failed to start receiver: %d\n", error));
542 awi_stop(ifp, 1);
543 return error;
544 }
545 sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
546 sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
547
548 ifp->if_flags |= IFF_RUNNING;
549 ifp->if_flags &= ~IFF_OACTIVE;
550 ic->ic_state = IEEE80211_S_INIT;
551
552 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
553 ic->ic_opmode == IEEE80211_M_HOSTAP) {
554 ni->ni_chan = ic->ic_ibss_chan;
555 ni->ni_intval = ic->ic_lintval;
556 ni->ni_rssi = 0;
557 ni->ni_rstamp = 0;
558 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
559 ni->ni_rates =
560 ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
561 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
562 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
563 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
564 ni->ni_esslen = ic->ic_des_esslen;
565 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
566 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
567 if (ic->ic_phytype == IEEE80211_T_FH) {
568 ni->ni_fhdwell = 200; /* XXX */
569 ni->ni_fhindex = 1;
570 }
571 } else {
572 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
573 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
574 ni->ni_esslen = 0;
575 }
576 if (ic->ic_flags & IEEE80211_F_WEPON)
577 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
578 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
579 ic->ic_flags |= IEEE80211_F_SIBSS;
580 ic->ic_state = IEEE80211_S_SCAN; /*XXX*/
581 sc->sc_substate = AWI_ST_NONE;
582 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
583 } else {
584 /* XXX check sc->sc_cur_chan */
585 ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
586 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
587 }
588 return 0;
589 }
590
591 static void
592 awi_stop(struct ifnet *ifp, int disable)
593 {
594 struct awi_softc *sc = ifp->if_softc;
595
596 if (!sc->sc_enabled)
597 return;
598
599 DPRINTF(("awi_stop(%d)\n", disable));
600
601 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
602
603 if (!sc->sc_invalid) {
604 if (sc->sc_cmd_inprog)
605 (void)awi_cmd_wait(sc);
606 (void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
607 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
608 awi_write_1(sc, AWI_CA_FTX_DATA, 1);
609 awi_write_1(sc, AWI_CA_FTX_MGT, 0);
610 awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
611 awi_write_1(sc, AWI_CA_FTX_PS, 0);
612 awi_write_1(sc, AWI_CA_FTX_CF, 0);
613 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
614 }
615 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
616 ifp->if_timer = 0;
617 sc->sc_tx_timer = sc->sc_rx_timer = 0;
618 if (sc->sc_rxpend != NULL) {
619 m_freem(sc->sc_rxpend);
620 sc->sc_rxpend = NULL;
621 }
622 IFQ_PURGE(&ifp->if_snd);
623
624 if (disable) {
625 if (sc->sc_disable)
626 (*sc->sc_disable)(sc);
627 sc->sc_enabled = 0;
628 }
629 }
630
631 static void
632 awi_start(struct ifnet *ifp)
633 {
634 struct awi_softc *sc = ifp->if_softc;
635 struct ieee80211com *ic = &sc->sc_ic;
636 struct ieee80211_node *ni;
637 struct ieee80211_frame *wh;
638 struct mbuf *m, *m0;
639 int len, dowep;
640 u_int32_t txd, frame, ntxd;
641 u_int8_t rate;
642
643 if (!sc->sc_enabled || sc->sc_invalid)
644 return;
645
646 for (;;) {
647 txd = sc->sc_txnext;
648 IF_POLL(&ic->ic_mgtq, m0);
649 dowep = 0;
650 if (m0 != NULL) {
651 len = m0->m_pkthdr.len;
652 if (awi_next_txd(sc, len, &frame, &ntxd)) {
653 ifp->if_flags |= IFF_OACTIVE;
654 break;
655 }
656 IF_DEQUEUE(&ic->ic_mgtq, m0);
657 } else {
658 if (ic->ic_state != IEEE80211_S_RUN)
659 break;
660 IFQ_POLL(&ifp->if_snd, m0);
661 if (m0 == NULL)
662 break;
663 /*
664 * Need to calculate the real length to determine
665 * if the transmit buffer has a room for the packet.
666 */
667 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
668 if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
669 len += sizeof(struct llc) -
670 sizeof(struct ether_header);
671 if (ic->ic_flags & IEEE80211_F_WEPON) {
672 dowep = 1;
673 len += IEEE80211_WEP_IVLEN +
674 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
675 }
676 if (awi_next_txd(sc, len, &frame, &ntxd)) {
677 ifp->if_flags |= IFF_OACTIVE;
678 break;
679 }
680 IFQ_DEQUEUE(&ifp->if_snd, m0);
681 ifp->if_opackets++;
682 #if NBPFILTER > 0
683 if (ifp->if_bpf)
684 bpf_mtap(ifp->if_bpf, m0);
685 #endif
686 if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
687 m0 = awi_ether_encap(sc, m0);
688 else
689 m0 = ieee80211_encap(ifp, m0, &ni);
690 if (m0 == NULL) {
691 ifp->if_oerrors++;
692 continue;
693 }
694 if (ni != NULL && ni != ic->ic_bss)
695 ieee80211_free_node(ic, ni);
696 wh = mtod(m0, struct ieee80211_frame *);
697 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
698 (ic->ic_opmode == IEEE80211_M_HOSTAP ||
699 ic->ic_opmode == IEEE80211_M_IBSS) &&
700 sc->sc_adhoc_ap == 0 &&
701 (ifp->if_flags & IFF_LINK0) == 0 &&
702 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
703 IEEE80211_FC0_TYPE_DATA && ni == NULL) {
704 m_freem(m0);
705 ifp->if_oerrors++;
706 continue;
707 }
708 }
709 #if NBPFILTER > 0
710 if (ic->ic_rawbpf)
711 bpf_mtap(ic->ic_rawbpf, m0);
712 #endif
713 if (dowep) {
714 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
715 ifp->if_oerrors++;
716 continue;
717 }
718 }
719 #ifdef DIAGNOSTIC
720 if (m0->m_pkthdr.len != len) {
721 printf("%s: length %d should be %d\n",
722 ifp->if_xname, m0->m_pkthdr.len, len);
723 m_freem(m0);
724 ifp->if_oerrors++;
725 continue;
726 }
727 #endif
728
729 if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
730 ieee80211_dump_pkt(m0->m_data, m0->m_len,
731 ic->ic_bss->ni_rates.
732 rs_rates[ic->ic_bss->ni_txrate] &
733 IEEE80211_RATE_VAL, -1);
734
735 for (m = m0, len = 0; m != NULL; m = m->m_next) {
736 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
737 m->m_len);
738 len += m->m_len;
739 }
740 m_freem(m0);
741 rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
742 IEEE80211_RATE_VAL) * 5;
743 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
744 awi_write_4(sc, txd + AWI_TXD_START, frame);
745 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
746 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
747 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
748 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
749 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
750 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
751 sc->sc_txnext = ntxd;
752
753 sc->sc_tx_timer = 5;
754 ifp->if_timer = 1;
755 }
756 }
757
758 static void
759 awi_watchdog(struct ifnet *ifp)
760 {
761 struct awi_softc *sc = ifp->if_softc;
762 u_int32_t prevdone;
763 int ocansleep;
764
765 ifp->if_timer = 0;
766 if (!sc->sc_enabled || sc->sc_invalid)
767 return;
768
769 ocansleep = sc->sc_cansleep;
770 sc->sc_cansleep = 0;
771 if (sc->sc_tx_timer) {
772 if (--sc->sc_tx_timer == 0) {
773 printf("%s: device timeout\n", ifp->if_xname);
774 prevdone = sc->sc_txdone;
775 awi_tx_int(sc);
776 if (sc->sc_txdone == prevdone) {
777 ifp->if_oerrors++;
778 awi_init(ifp);
779 goto out;
780 }
781 }
782 ifp->if_timer = 1;
783 }
784 if (sc->sc_rx_timer) {
785 if (--sc->sc_rx_timer == 0) {
786 if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
787 ieee80211_new_state(&sc->sc_ic,
788 IEEE80211_S_SCAN, -1);
789 goto out;
790 }
791 } else
792 ifp->if_timer = 1;
793 }
794 /* TODO: rate control */
795 ieee80211_watchdog(ifp);
796 out:
797 sc->sc_cansleep = ocansleep;
798 }
799
800 static int
801 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
802 {
803 struct awi_softc *sc = ifp->if_softc;
804 struct ifreq *ifr = (struct ifreq *)data;
805 int s, error;
806
807 s = splnet();
808 /* serialize ioctl, since we may sleep */
809 if ((error = awi_lock(sc)) != 0)
810 goto cantlock;
811
812 switch (cmd) {
813 case SIOCSIFFLAGS:
814 if (ifp->if_flags & IFF_UP) {
815 if (sc->sc_enabled) {
816 /*
817 * To avoid rescanning another access point,
818 * do not call awi_init() here. Instead,
819 * only reflect promisc mode settings.
820 */
821 error = awi_mode_init(sc);
822 } else
823 error = awi_init(ifp);
824 } else if (sc->sc_enabled)
825 awi_stop(ifp, 1);
826 break;
827 case SIOCSIFMEDIA:
828 case SIOCGIFMEDIA:
829 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ic.ic_media, cmd);
830 break;
831 case SIOCADDMULTI:
832 case SIOCDELMULTI:
833 error = (cmd == SIOCADDMULTI) ?
834 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
835 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
836 if (error == ENETRESET) {
837 /* do not rescan */
838 if (sc->sc_enabled)
839 error = awi_mode_init(sc);
840 else
841 error = 0;
842 }
843 break;
844 default:
845 error = ieee80211_ioctl(ifp, cmd, data);
846 if (error == ENETRESET) {
847 if (sc->sc_enabled)
848 error = awi_init(ifp);
849 else
850 error = 0;
851 }
852 break;
853 }
854 awi_unlock(sc);
855 cantlock:
856 splx(s);
857 return error;
858 }
859
860 /*
861 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
862 *
863 * TBD factor with ieee80211_media_change
864 */
865 static int
866 awi_media_change(struct ifnet *ifp)
867 {
868 struct awi_softc *sc = ifp->if_softc;
869 struct ieee80211com *ic = &sc->sc_ic;
870 struct ifmedia_entry *ime;
871 enum ieee80211_opmode newmode;
872 int i, rate, newadhoc_ap, error = 0;
873
874 ime = ic->ic_media.ifm_cur;
875 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
876 i = -1;
877 } else {
878 struct ieee80211_rateset *rs =
879 &ic->ic_sup_rates[ieee80211_chan2mode(ic,
880 ic->ic_bss->ni_chan)];
881 rate = ieee80211_media2rate(ime->ifm_media);
882 if (rate == 0)
883 return EINVAL;
884 for (i = 0; i < rs->rs_nrates; i++) {
885 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
886 break;
887 }
888 if (i == rs->rs_nrates)
889 return EINVAL;
890 }
891 if (ic->ic_fixed_rate != i) {
892 ic->ic_fixed_rate = i;
893 error = ENETRESET;
894 }
895
896 /*
897 * combination of mediaopt
898 *
899 * hostap adhoc flag0 opmode adhoc_ap comment
900 * + - - HOSTAP 0 HostAP
901 * - + - IBSS 0 IBSS
902 * - + + AHDEMO 0 WaveLAN adhoc
903 * - - + IBSS 1 Melco old Sta
904 * also LINK0
905 * - - - STA 0 Infra Station
906 */
907 newadhoc_ap = 0;
908 if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
909 newmode = IEEE80211_M_HOSTAP;
910 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
911 if (ic->ic_phytype == IEEE80211_T_DS &&
912 (ime->ifm_media & IFM_FLAG0))
913 newmode = IEEE80211_M_AHDEMO;
914 else
915 newmode = IEEE80211_M_IBSS;
916 } else if (ime->ifm_media & IFM_FLAG0) {
917 newmode = IEEE80211_M_IBSS;
918 newadhoc_ap = 1;
919 } else
920 newmode = IEEE80211_M_STA;
921 if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
922 ic->ic_opmode = newmode;
923 sc->sc_adhoc_ap = newadhoc_ap;
924 error = ENETRESET;
925 }
926
927 if (error == ENETRESET) {
928 if (sc->sc_enabled)
929 error = awi_init(ifp);
930 else
931 error = 0;
932 }
933 return error;
934 }
935
936 static void
937 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
938 {
939 struct awi_softc *sc = ifp->if_softc;
940 struct ieee80211com *ic = &sc->sc_ic;
941 int rate;
942
943 imr->ifm_status = IFM_AVALID;
944 if (ic->ic_state == IEEE80211_S_RUN)
945 imr->ifm_status |= IFM_ACTIVE;
946 imr->ifm_active = IFM_IEEE80211;
947 if (ic->ic_state == IEEE80211_S_RUN) {
948 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
949 IEEE80211_RATE_VAL;
950 } else {
951 if (ic->ic_fixed_rate == -1)
952 rate = 0;
953 else
954 rate = ic->ic_sup_rates[
955 ieee80211_chan2mode(ic, ic->ic_bss->ni_chan)].
956 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
957 }
958 imr->ifm_active |= ieee80211_rate2media(ic, rate,
959 (ic->ic_phytype == IEEE80211_T_FH) ? IEEE80211_MODE_FH
960 : IEEE80211_MODE_11B);
961 switch (ic->ic_opmode) {
962 case IEEE80211_M_MONITOR: /* we should never reach here */
963 break;
964 case IEEE80211_M_STA:
965 break;
966 case IEEE80211_M_IBSS:
967 if (sc->sc_adhoc_ap)
968 imr->ifm_active |= IFM_FLAG0;
969 else
970 imr->ifm_active |= IFM_IEEE80211_ADHOC;
971 break;
972 case IEEE80211_M_AHDEMO:
973 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
974 break;
975 case IEEE80211_M_HOSTAP:
976 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
977 break;
978 }
979 }
980
981 static int
982 awi_mode_init(struct awi_softc *sc)
983 {
984 struct ifnet *ifp = &sc->sc_ic.ic_if;
985 int n, error;
986 struct ether_multi *enm;
987 struct ether_multistep step;
988
989 /* reinitialize muticast filter */
990 n = 0;
991 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
992 if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
993 (ifp->if_flags & IFF_PROMISC)) {
994 sc->sc_mib_mac.aPromiscuous_Enable = 1;
995 goto set_mib;
996 }
997 sc->sc_mib_mac.aPromiscuous_Enable = 0;
998 ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
999 while (enm != NULL) {
1000 if (n == AWI_GROUP_ADDR_SIZE ||
1001 !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi))
1002 goto set_mib;
1003 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1004 enm->enm_addrlo);
1005 n++;
1006 ETHER_NEXT_MULTI(step, enm);
1007 }
1008 for (; n < AWI_GROUP_ADDR_SIZE; n++)
1009 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1010 IEEE80211_ADDR_LEN);
1011 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1012
1013 set_mib:
1014 if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1015 ifp->if_flags &= ~IFF_ALLMULTI;
1016 else
1017 ifp->if_flags |= IFF_ALLMULTI;
1018 sc->sc_mib_mgt.Wep_Required =
1019 (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? AWI_WEP_ON : AWI_WEP_OFF;
1020
1021 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1022 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1023 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1024 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1025 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1026 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1027 return error;
1028 }
1029 return 0;
1030 }
1031
1032 static void
1033 awi_rx_int(struct awi_softc *sc)
1034 {
1035 struct ieee80211com *ic = &sc->sc_ic;
1036 struct ifnet *ifp = &ic->ic_if;
1037 struct ieee80211_frame *wh;
1038 struct ieee80211_node *ni;
1039 u_int8_t state, rate, rssi;
1040 u_int16_t len;
1041 u_int32_t frame, next, rstamp, rxoff;
1042 struct mbuf *m;
1043
1044 rxoff = sc->sc_rxdoff;
1045 for (;;) {
1046 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1047 if (state & AWI_RXD_ST_OWN)
1048 break;
1049 if (!(state & AWI_RXD_ST_CONSUMED)) {
1050 if (sc->sc_substate != AWI_ST_NONE)
1051 goto rx_next;
1052 if (state & AWI_RXD_ST_RXERROR) {
1053 ifp->if_ierrors++;
1054 goto rx_next;
1055 }
1056 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1057 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1058 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1059 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1060 0x7fff;
1061 rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1062 m = awi_devget(sc, frame, len);
1063 if (m == NULL) {
1064 ifp->if_ierrors++;
1065 goto rx_next;
1066 }
1067 if (state & AWI_RXD_ST_LF) {
1068 /* TODO check my bss */
1069 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1070 sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1071 sc->sc_rx_timer = 10;
1072 ifp->if_timer = 1;
1073 }
1074 if ((ifp->if_flags & IFF_DEBUG) &&
1075 (ifp->if_flags & IFF_LINK2))
1076 ieee80211_dump_pkt(m->m_data, m->m_len,
1077 rate / 5, rssi);
1078 if ((ifp->if_flags & IFF_LINK0) ||
1079 sc->sc_adhoc_ap)
1080 m = awi_ether_modcap(sc, m);
1081 else
1082 m = m_pullup(m, sizeof(*wh));
1083 if (m == NULL) {
1084 ifp->if_ierrors++;
1085 goto rx_next;
1086 }
1087 wh = mtod(m, struct ieee80211_frame *);
1088 ni = ieee80211_find_rxnode(ic, wh);
1089 ieee80211_input(ifp, m, ni, rssi, rstamp);
1090 /*
1091 * The frame may have caused the
1092 * node to be marked for reclamation
1093 * (e.g. in response to a DEAUTH
1094 * message) so use free_node here
1095 * instead of unref_node.
1096 */
1097 if (ni == ic->ic_bss)
1098 ieee80211_unref_node(&ni);
1099 else
1100 ieee80211_free_node(ic, ni);
1101 } else
1102 sc->sc_rxpend = m;
1103 rx_next:
1104 state |= AWI_RXD_ST_CONSUMED;
1105 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1106 }
1107 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1108 if (next & AWI_RXD_NEXT_LAST)
1109 break;
1110 /* make sure the next pointer is correct */
1111 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1112 break;
1113 state |= AWI_RXD_ST_OWN;
1114 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1115 rxoff = next & 0x7fff;
1116 }
1117 sc->sc_rxdoff = rxoff;
1118 }
1119
1120 static void
1121 awi_tx_int(struct awi_softc *sc)
1122 {
1123 struct ifnet *ifp = &sc->sc_ic.ic_if;
1124 u_int8_t flags;
1125
1126 while (sc->sc_txdone != sc->sc_txnext) {
1127 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1128 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1129 break;
1130 if (flags & AWI_TXD_ST_ERROR)
1131 ifp->if_oerrors++;
1132 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1133 0x7fff;
1134 }
1135 DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1136 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1137 sc->sc_tx_timer = 0;
1138 ifp->if_flags &= ~IFF_OACTIVE;
1139 awi_start(ifp);
1140 }
1141
1142 static struct mbuf *
1143 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1144 {
1145 struct ifnet *ifp = &sc->sc_ic.ic_if;
1146 struct mbuf *m;
1147 struct mbuf *top, **mp;
1148 u_int tlen;
1149
1150 top = sc->sc_rxpend;
1151 mp = ⊤
1152 if (top != NULL) {
1153 sc->sc_rxpend = NULL;
1154 top->m_pkthdr.len += len;
1155 m = top;
1156 while (*mp != NULL) {
1157 m = *mp;
1158 mp = &m->m_next;
1159 }
1160 if (m->m_flags & M_EXT)
1161 tlen = m->m_ext.ext_size;
1162 else if (m->m_flags & M_PKTHDR)
1163 tlen = MHLEN;
1164 else
1165 tlen = MLEN;
1166 tlen -= m->m_len;
1167 if (tlen > len)
1168 tlen = len;
1169 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1170 off += tlen;
1171 len -= tlen;
1172 }
1173
1174 while (len > 0) {
1175 if (top == NULL) {
1176 MGETHDR(m, M_DONTWAIT, MT_DATA);
1177 if (m == NULL)
1178 return NULL;
1179 m->m_pkthdr.rcvif = ifp;
1180 m->m_pkthdr.len = len;
1181 m->m_len = MHLEN;
1182 m->m_flags |= M_HASFCS;
1183 } else {
1184 MGET(m, M_DONTWAIT, MT_DATA);
1185 if (m == NULL) {
1186 m_freem(top);
1187 return NULL;
1188 }
1189 m->m_len = MLEN;
1190 }
1191 if (len >= MINCLSIZE) {
1192 MCLGET(m, M_DONTWAIT);
1193 if (m->m_flags & M_EXT)
1194 m->m_len = m->m_ext.ext_size;
1195 }
1196 if (top == NULL) {
1197 int hdrlen = sizeof(struct ieee80211_frame) +
1198 sizeof(struct llc);
1199 caddr_t newdata = (caddr_t)
1200 ALIGN(m->m_data + hdrlen) - hdrlen;
1201 m->m_len -= newdata - m->m_data;
1202 m->m_data = newdata;
1203 }
1204 if (m->m_len > len)
1205 m->m_len = len;
1206 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1207 off += m->m_len;
1208 len -= m->m_len;
1209 *mp = m;
1210 mp = &m->m_next;
1211 }
1212 return top;
1213 }
1214
1215 /*
1216 * Initialize hardware and start firmware to accept commands.
1217 * Called everytime after power on firmware.
1218 */
1219
1220 static int
1221 awi_hw_init(struct awi_softc *sc)
1222 {
1223 u_int8_t status;
1224 u_int16_t intmask;
1225 int i, error;
1226
1227 sc->sc_enab_intr = 0;
1228 sc->sc_invalid = 0; /* XXX: really? */
1229 awi_drvstate(sc, AWI_DRV_RESET);
1230
1231 /* reset firmware */
1232 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1233 DELAY(100);
1234 awi_write_1(sc, AWI_SELFTEST, 0);
1235 awi_write_1(sc, AWI_CMD, 0);
1236 awi_write_1(sc, AWI_BANNER, 0);
1237 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1238 DELAY(100);
1239
1240 /* wait for selftest completion */
1241 for (i = 0; ; i++) {
1242 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1243 printf("%s: failed to complete selftest (timeout)\n",
1244 sc->sc_dev.dv_xname);
1245 return ENXIO;
1246 }
1247 status = awi_read_1(sc, AWI_SELFTEST);
1248 if ((status & 0xf0) == 0xf0)
1249 break;
1250 if (sc->sc_cansleep) {
1251 sc->sc_sleep_cnt++;
1252 (void)tsleep(sc, PWAIT, "awitst", 1);
1253 sc->sc_sleep_cnt--;
1254 } else {
1255 DELAY(1000*1000/hz);
1256 }
1257 }
1258 if (status != AWI_SELFTEST_PASSED) {
1259 printf("%s: failed to complete selftest (code %x)\n",
1260 sc->sc_dev.dv_xname, status);
1261 return ENXIO;
1262 }
1263
1264 /* check banner to confirm firmware write it */
1265 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1266 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1267 printf("%s: failed to complete selftest (bad banner)\n",
1268 sc->sc_dev.dv_xname);
1269 for (i = 0; i < AWI_BANNER_LEN; i++)
1270 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1271 printf("\n");
1272 return ENXIO;
1273 }
1274
1275 /* initializing interrupt */
1276 sc->sc_enab_intr = 1;
1277 error = awi_intr_lock(sc);
1278 if (error)
1279 return error;
1280 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1281 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1282 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1283 awi_write_1(sc, AWI_INTMASK2, 0);
1284 awi_write_1(sc, AWI_INTSTAT, 0);
1285 awi_write_1(sc, AWI_INTSTAT2, 0);
1286 awi_intr_unlock(sc);
1287 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1288
1289 /* issuing interface test command */
1290 error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1291 if (error) {
1292 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1293 if (error == ENXIO)
1294 printf(" (no hardware)\n");
1295 else if (error != EWOULDBLOCK)
1296 printf(" (error %d)\n", error);
1297 else if (sc->sc_cansleep)
1298 printf(" (lost interrupt)\n");
1299 else
1300 printf(" (command timeout)\n");
1301 return error;
1302 }
1303
1304 /* Initialize VBM */
1305 awi_write_1(sc, AWI_VBM_OFFSET, 0);
1306 awi_write_1(sc, AWI_VBM_LENGTH, 1);
1307 awi_write_1(sc, AWI_VBM_BITMAP, 0);
1308 return 0;
1309 }
1310
1311 /*
1312 * Extract the factory default MIB value from firmware and assign the driver
1313 * default value.
1314 * Called once at attaching the interface.
1315 */
1316
1317 static int
1318 awi_init_mibs(struct awi_softc *sc)
1319 {
1320 int chan, i, error;
1321 struct ieee80211com *ic = &sc->sc_ic;
1322 struct awi_chanset *cs;
1323
1324 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1325 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1326 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1327 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1328 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1329 printf("%s: failed to get default mib value (error %d)\n",
1330 sc->sc_dev.dv_xname, error);
1331 return error;
1332 }
1333
1334 memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1335 for (cs = awi_chanset; ; cs++) {
1336 if (cs->cs_type == 0) {
1337 printf("%s: failed to set available channel\n",
1338 sc->sc_dev.dv_xname);
1339 return ENXIO;
1340 }
1341 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1342 cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1343 break;
1344 }
1345 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1346 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1347 chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
1348 setbit(sc->sc_ic.ic_chan_avail, chan);
1349 /* XXX for FHSS, does frequency matter? */
1350 ic->ic_channels[chan].ic_freq = 0;
1351 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1352 /*
1353 * According to the IEEE 802.11 specification,
1354 * hop pattern parameter for FH phy should be
1355 * incremented by 3 for given hop chanset, i.e.,
1356 * the chanset parameter is calculated for given
1357 * hop patter. However, BayStack 650 Access Points
1358 * apparently use fixed hop chanset parameter value
1359 * 1 for any hop pattern. So we also try this
1360 * combination of hop chanset and pattern.
1361 */
1362 chan = IEEE80211_FH_CHAN(1, i);
1363 setbit(sc->sc_ic.ic_chan_avail, chan);
1364 ic->ic_channels[chan].ic_freq = 0; /* XXX */
1365 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1366 }
1367 } else {
1368 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1369 setbit(sc->sc_ic.ic_chan_avail, i);
1370 ic->ic_channels[i].ic_freq =
1371 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
1372 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
1373 }
1374 }
1375 sc->sc_cur_chan = cs->cs_def;
1376
1377 sc->sc_mib_local.Fragmentation_Dis = 1;
1378 sc->sc_mib_local.Add_PLCP_Dis = 0;
1379 sc->sc_mib_local.MAC_Hdr_Prsv = 1;
1380 sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1381 sc->sc_mib_local.Re_Assembly_Dis = 1;
1382 sc->sc_mib_local.Strip_PLCP_Dis = 0;
1383 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1384 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1385 sc->sc_mib_local.Check_Seq_Cntl_Dis = 1;
1386 sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1387 sc->sc_mib_local.Network_Mode = 1;
1388 sc->sc_mib_local.PWD_Lvl = 0;
1389 sc->sc_mib_local.CFP_Mode = 0;
1390
1391 /* allocate buffers */
1392 sc->sc_txbase = AWI_BUFFERS;
1393 sc->sc_txend = sc->sc_txbase +
1394 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1395 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1396 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1397 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1398 sc->sc_txend - sc->sc_txbase);
1399 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1400 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1401 AWI_BUFFERS_END - sc->sc_txend);
1402 sc->sc_mib_local.Acting_as_AP = 0;
1403 sc->sc_mib_local.Fill_CFP = 0;
1404
1405 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1406 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1407
1408 sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1409 sc->sc_mib_mgt.aDTIM_Period = 1;
1410 LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1411 return 0;
1412 }
1413
1414 static int
1415 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1416 {
1417 int error;
1418 u_int8_t size, *ptr;
1419
1420 switch (mib) {
1421 case AWI_MIB_LOCAL:
1422 ptr = (u_int8_t *)&sc->sc_mib_local;
1423 size = sizeof(sc->sc_mib_local);
1424 break;
1425 case AWI_MIB_ADDR:
1426 ptr = (u_int8_t *)&sc->sc_mib_addr;
1427 size = sizeof(sc->sc_mib_addr);
1428 break;
1429 case AWI_MIB_MAC:
1430 ptr = (u_int8_t *)&sc->sc_mib_mac;
1431 size = sizeof(sc->sc_mib_mac);
1432 break;
1433 case AWI_MIB_STAT:
1434 ptr = (u_int8_t *)&sc->sc_mib_stat;
1435 size = sizeof(sc->sc_mib_stat);
1436 break;
1437 case AWI_MIB_MGT:
1438 ptr = (u_int8_t *)&sc->sc_mib_mgt;
1439 size = sizeof(sc->sc_mib_mgt);
1440 break;
1441 case AWI_MIB_PHY:
1442 ptr = (u_int8_t *)&sc->sc_mib_phy;
1443 size = sizeof(sc->sc_mib_phy);
1444 break;
1445 default:
1446 return EINVAL;
1447 }
1448 if (sc->sc_cmd_inprog) {
1449 if ((error = awi_cmd_wait(sc)) != 0) {
1450 if (error == EWOULDBLOCK)
1451 DPRINTF(("awi_mib: cmd %d inprog",
1452 sc->sc_cmd_inprog));
1453 return error;
1454 }
1455 }
1456 sc->sc_cmd_inprog = cmd;
1457 if (cmd == AWI_CMD_SET_MIB)
1458 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1459 awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1460 awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1461 awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1462 if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1463 return error;
1464 if (cmd == AWI_CMD_GET_MIB) {
1465 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1466 #ifdef AWI_DEBUG
1467 if (awi_debug) {
1468 int i;
1469
1470 printf("awi_mib: #%d:", mib);
1471 for (i = 0; i < size; i++)
1472 printf(" %02x", ptr[i]);
1473 printf("\n");
1474 }
1475 #endif
1476 }
1477 return 0;
1478 }
1479
1480 static int
1481 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1482 {
1483 u_int8_t status;
1484 int error = 0;
1485 #ifdef AWI_DEBUG
1486 static const char *cmdname[] = {
1487 "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1488 "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1489 };
1490 #endif
1491
1492 #ifdef AWI_DEBUG
1493 if (awi_debug > 1) {
1494 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1495 printf("awi_cmd: #%d", cmd);
1496 else
1497 printf("awi_cmd: %s", cmdname[cmd]);
1498 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1499 }
1500 #endif
1501 sc->sc_cmd_inprog = cmd;
1502 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1503 awi_write_1(sc, AWI_CMD, cmd);
1504 if (wflag == AWI_NOWAIT)
1505 return EINPROGRESS;
1506 if ((error = awi_cmd_wait(sc)) != 0)
1507 return error;
1508 status = awi_read_1(sc, AWI_CMD_STATUS);
1509 awi_write_1(sc, AWI_CMD, 0);
1510 switch (status) {
1511 case AWI_STAT_OK:
1512 break;
1513 case AWI_STAT_BADPARM:
1514 return EINVAL;
1515 default:
1516 printf("%s: command %d failed %x\n",
1517 sc->sc_dev.dv_xname, cmd, status);
1518 return ENXIO;
1519 }
1520 return 0;
1521 }
1522
1523 static int
1524 awi_cmd_wait(struct awi_softc *sc)
1525 {
1526 int i, error = 0;
1527
1528 i = 0;
1529 while (sc->sc_cmd_inprog) {
1530 if (sc->sc_invalid)
1531 return ENXIO;
1532 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1533 printf("%s: failed to access hardware\n",
1534 sc->sc_dev.dv_xname);
1535 sc->sc_invalid = 1;
1536 return ENXIO;
1537 }
1538 if (sc->sc_cansleep) {
1539 sc->sc_sleep_cnt++;
1540 error = tsleep(sc, PWAIT, "awicmd",
1541 AWI_CMD_TIMEOUT*hz/1000);
1542 sc->sc_sleep_cnt--;
1543 } else {
1544 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1545 awi_cmd_done(sc);
1546 break;
1547 }
1548 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1549 error = EWOULDBLOCK;
1550 else
1551 DELAY(10);
1552 }
1553 if (error)
1554 break;
1555 }
1556 if (error) {
1557 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1558 sc->sc_cmd_inprog, error));
1559 }
1560 return error;
1561 }
1562
1563 static void
1564 awi_cmd_done(struct awi_softc *sc)
1565 {
1566 u_int8_t cmd, status;
1567
1568 status = awi_read_1(sc, AWI_CMD_STATUS);
1569 if (status == AWI_STAT_IDLE)
1570 return; /* stray interrupt */
1571
1572 cmd = sc->sc_cmd_inprog;
1573 sc->sc_cmd_inprog = 0;
1574 wakeup(sc);
1575 awi_write_1(sc, AWI_CMD, 0);
1576
1577 if (status != AWI_STAT_OK) {
1578 printf("%s: command %d failed %x\n",
1579 sc->sc_dev.dv_xname, cmd, status);
1580 sc->sc_substate = AWI_ST_NONE;
1581 return;
1582 }
1583 if (sc->sc_substate != AWI_ST_NONE)
1584 (void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
1585 }
1586
1587 static int
1588 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1589 {
1590 u_int32_t txd, ntxd, frame;
1591
1592 txd = sc->sc_txnext;
1593 frame = txd + AWI_TXD_SIZE;
1594 if (frame + len > sc->sc_txend)
1595 frame = sc->sc_txbase;
1596 ntxd = frame + len;
1597 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1598 ntxd = sc->sc_txbase;
1599 *framep = frame;
1600 *ntxdp = ntxd;
1601 /*
1602 * Determine if there are any room in ring buffer.
1603 * --- send wait, === new data, +++ conflict (ENOBUFS)
1604 * base........................end
1605 * done----txd=====ntxd OK
1606 * --txd=====done++++ntxd-- full
1607 * --txd=====ntxd done-- OK
1608 * ==ntxd done----txd=== OK
1609 * ==done++++ntxd----txd=== full
1610 * ++ntxd txd=====done++ full
1611 */
1612 if (txd < ntxd) {
1613 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1614 return ENOBUFS;
1615 } else {
1616 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1617 return ENOBUFS;
1618 }
1619 return 0;
1620 }
1621
1622 static int
1623 awi_lock(struct awi_softc *sc)
1624 {
1625 int error = 0;
1626
1627 if (curlwp == NULL) {
1628 /*
1629 * XXX
1630 * Though driver ioctl should be called with context,
1631 * KAME ipv6 stack calls ioctl in interrupt for now.
1632 * We simply abort the request if there are other
1633 * ioctl requests in progress.
1634 */
1635 if (sc->sc_busy) {
1636 if (sc->sc_invalid)
1637 return ENXIO;
1638 return EWOULDBLOCK;
1639 }
1640 sc->sc_busy = 1;
1641 sc->sc_cansleep = 0;
1642 return 0;
1643 }
1644 while (sc->sc_busy) {
1645 if (sc->sc_invalid)
1646 return ENXIO;
1647 sc->sc_sleep_cnt++;
1648 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1649 sc->sc_sleep_cnt--;
1650 if (error)
1651 return error;
1652 }
1653 sc->sc_busy = 1;
1654 sc->sc_cansleep = 1;
1655 return 0;
1656 }
1657
1658 static void
1659 awi_unlock(struct awi_softc *sc)
1660 {
1661 sc->sc_busy = 0;
1662 sc->sc_cansleep = 0;
1663 if (sc->sc_sleep_cnt)
1664 wakeup(sc);
1665 }
1666
1667 static int
1668 awi_intr_lock(struct awi_softc *sc)
1669 {
1670 u_int8_t status;
1671 int i, retry;
1672
1673 status = 1;
1674 for (retry = 0; retry < 10; retry++) {
1675 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1676 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1677 break;
1678 DELAY(5);
1679 }
1680 if (status != 0)
1681 break;
1682 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1683 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1684 break;
1685 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1686 }
1687 if (status != 0) {
1688 printf("%s: failed to lock interrupt\n",
1689 sc->sc_dev.dv_xname);
1690 return ENXIO;
1691 }
1692 return 0;
1693 }
1694
1695 static void
1696 awi_intr_unlock(struct awi_softc *sc)
1697 {
1698
1699 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1700 }
1701
1702 static int
1703 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1704 {
1705 struct awi_softc *sc = ic->ic_softc;
1706 struct ieee80211_node *ni;
1707 struct ifnet *ifp = &ic->ic_if;
1708 int error;
1709 u_int8_t newmode;
1710 enum ieee80211_state ostate;
1711 #ifdef AWI_DEBUG
1712 static const char *stname[] =
1713 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1714 static const char *substname[] =
1715 { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1716 "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1717 #endif /* AWI_DEBUG */
1718
1719 ostate = ic->ic_state;
1720 DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1721 stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1722
1723 /* set LED */
1724 switch (nstate) {
1725 case IEEE80211_S_INIT:
1726 awi_drvstate(sc, AWI_DRV_RESET);
1727 break;
1728 case IEEE80211_S_SCAN:
1729 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1730 ic->ic_opmode == IEEE80211_M_AHDEMO)
1731 awi_drvstate(sc, AWI_DRV_ADHSC);
1732 else
1733 awi_drvstate(sc, AWI_DRV_INFSY);
1734 break;
1735 case IEEE80211_S_AUTH:
1736 awi_drvstate(sc, AWI_DRV_INFSY);
1737 break;
1738 case IEEE80211_S_ASSOC:
1739 awi_drvstate(sc, AWI_DRV_INFAUTH);
1740 break;
1741 case IEEE80211_S_RUN:
1742 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1743 ic->ic_opmode == IEEE80211_M_AHDEMO)
1744 awi_drvstate(sc, AWI_DRV_ADHSY);
1745 else
1746 awi_drvstate(sc, AWI_DRV_INFASSOC);
1747 break;
1748 }
1749
1750 if (nstate == IEEE80211_S_INIT) {
1751 sc->sc_substate = AWI_ST_NONE;
1752 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1753 return (*sc->sc_newstate)(ic, nstate, arg);
1754 }
1755
1756 /* state transition */
1757 if (nstate == IEEE80211_S_SCAN) {
1758 /* SCAN substate */
1759 if (sc->sc_substate == AWI_ST_NONE) {
1760 sc->sc_nstate = nstate; /* next state in transition */
1761 sc->sc_substate = AWI_ST_SCAN_INIT;
1762 }
1763 switch (sc->sc_substate) {
1764 case AWI_ST_SCAN_INIT:
1765 sc->sc_substate = AWI_ST_SCAN_SETMIB;
1766 switch (ostate) {
1767 case IEEE80211_S_RUN:
1768 /* beacon miss */
1769 if (ifp->if_flags & IFF_DEBUG)
1770 printf("%s: no recent beacons from %s;"
1771 " rescanning\n",
1772 ifp->if_xname,
1773 ether_sprintf(ic->ic_bss->ni_bssid));
1774 /* FALLTHRU */
1775 case IEEE80211_S_AUTH:
1776 case IEEE80211_S_ASSOC:
1777 /* timeout restart scan */
1778 ieee80211_free_allnodes(ic);
1779 /* FALLTHRU */
1780 case IEEE80211_S_INIT:
1781 ic->ic_flags |= IEEE80211_F_ASCAN;
1782 /* FALLTHRU */
1783 case IEEE80211_S_SCAN:
1784 /* scan next */
1785 break;
1786 }
1787 if (ic->ic_flags & IEEE80211_F_ASCAN)
1788 newmode = AWI_SCAN_ACTIVE;
1789 else
1790 newmode = AWI_SCAN_PASSIVE;
1791 if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1792 sc->sc_mib_mgt.aScan_Mode = newmode;
1793 if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1794 AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1795 break;
1796 }
1797 /* FALLTHRU */
1798 case AWI_ST_SCAN_SETMIB:
1799 sc->sc_substate = AWI_ST_SCAN_SCCMD;
1800 if (sc->sc_cmd_inprog) {
1801 if ((error = awi_cmd_wait(sc)) != 0)
1802 break;
1803 }
1804 sc->sc_cmd_inprog = AWI_CMD_SCAN;
1805 ni = ic->ic_bss;
1806 awi_write_2(sc, AWI_CA_SCAN_DURATION,
1807 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1808 AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1809 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1810 awi_write_1(sc, AWI_CA_SCAN_SET,
1811 IEEE80211_FH_CHANSET(
1812 ieee80211_chan2ieee(ic, ni->ni_chan)));
1813 awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1814 IEEE80211_FH_CHANPAT(
1815 ieee80211_chan2ieee(ic, ni->ni_chan)));
1816 awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1817 } else {
1818 awi_write_1(sc, AWI_CA_SCAN_SET,
1819 ieee80211_chan2ieee(ic, ni->ni_chan));
1820 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1821 awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1822 }
1823 awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1824 sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1825 if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1826 != 0)
1827 break;
1828 /* FALLTHRU */
1829 case AWI_ST_SCAN_SCCMD:
1830 ic->ic_state = nstate;
1831 sc->sc_substate = AWI_ST_NONE;
1832 error = EINPROGRESS;
1833 break;
1834 default:
1835 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1836 stname[nstate], substname[sc->sc_substate]));
1837 sc->sc_substate = AWI_ST_NONE;
1838 error = EIO;
1839 break;
1840 }
1841 goto out;
1842 }
1843
1844 if (ostate == IEEE80211_S_SCAN) {
1845 /* set SSID and channel */
1846 /* substate */
1847 if (sc->sc_substate == AWI_ST_NONE) {
1848 sc->sc_nstate = nstate; /* next state in transition */
1849 sc->sc_substate = AWI_ST_SUB_INIT;
1850 }
1851 ni = ic->ic_bss;
1852 switch (sc->sc_substate) {
1853 case AWI_ST_SUB_INIT:
1854 sc->sc_substate = AWI_ST_SUB_SETSS;
1855 IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
1856 ni->ni_bssid);
1857 memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
1858 AWI_ESS_ID_SIZE);
1859 sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
1860 IEEE80211_ELEMID_SSID;
1861 sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
1862 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
1863 ni->ni_essid, ni->ni_esslen);
1864 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
1865 ni->ni_intval);
1866 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
1867 AWI_NOWAIT)) != 0)
1868 break;
1869 /* FALLTHRU */
1870 case AWI_ST_SUB_SETSS:
1871 sc->sc_substate = AWI_ST_SUB_SYNC;
1872 if (sc->sc_cmd_inprog) {
1873 if ((error = awi_cmd_wait(sc)) != 0)
1874 break;
1875 }
1876 sc->sc_cmd_inprog = AWI_CMD_SYNC;
1877 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1878 awi_write_1(sc, AWI_CA_SYNC_SET,
1879 IEEE80211_FH_CHANSET(
1880 ieee80211_chan2ieee(ic, ni->ni_chan)));
1881 awi_write_1(sc, AWI_CA_SYNC_PATTERN,
1882 IEEE80211_FH_CHANPAT(
1883 ieee80211_chan2ieee(ic, ni->ni_chan)));
1884 awi_write_1(sc, AWI_CA_SYNC_IDX,
1885 ni->ni_fhindex);
1886 awi_write_2(sc, AWI_CA_SYNC_DWELL,
1887 ni->ni_fhdwell);
1888 } else {
1889 awi_write_1(sc, AWI_CA_SYNC_SET,
1890 ieee80211_chan2ieee(ic, ni->ni_chan));
1891 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
1892 awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
1893 awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
1894 }
1895 if (ic->ic_flags & IEEE80211_F_SIBSS)
1896 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
1897 else
1898 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
1899 awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
1900 awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
1901 ni->ni_tstamp, 8);
1902 awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
1903 sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1904 if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
1905 != 0)
1906 break;
1907 /* FALLTHRU */
1908 case AWI_ST_SUB_SYNC:
1909 sc->sc_substate = AWI_ST_NONE;
1910 if (ic->ic_flags & IEEE80211_F_SIBSS) {
1911 if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
1912 AWI_MIB_MGT, AWI_WAIT)) != 0)
1913 break;
1914 IEEE80211_ADDR_COPY(ni->ni_bssid,
1915 &sc->sc_mib_mgt.aCurrent_BSS_ID);
1916 } else {
1917 if (nstate == IEEE80211_S_RUN) {
1918 sc->sc_rx_timer = 10;
1919 ifp->if_timer = 1;
1920 }
1921 }
1922 error = 0;
1923 break;
1924 default:
1925 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1926 stname[nstate], substname[sc->sc_substate]));
1927 sc->sc_substate = AWI_ST_NONE;
1928 error = EIO;
1929 break;
1930 }
1931 goto out;
1932 }
1933
1934 sc->sc_substate = AWI_ST_NONE;
1935
1936 return (*sc->sc_newstate)(ic, nstate, arg);
1937 out:
1938 if (error != 0)
1939 return error;
1940 return (*sc->sc_newstate)(ic, nstate, arg);
1941 }
1942
1943 static void
1944 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
1945 struct ieee80211_node *ni,
1946 int subtype, int rssi, u_int32_t rstamp)
1947 {
1948 struct awi_softc *sc = ic->ic_softc;
1949
1950 /* probe request is handled by hardware */
1951 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
1952 return;
1953 (*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
1954 }
1955
1956 static int
1957 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1958 int type, int arg)
1959 {
1960 struct awi_softc *sc = ic->ic_softc;
1961
1962 /* probe request is handled by hardware */
1963 if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
1964 return 0;
1965 return (*sc->sc_send_mgmt)(ic, ni, type, arg);
1966 }
1967
1968 static struct mbuf *
1969 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
1970 {
1971 struct ieee80211com *ic = &sc->sc_ic;
1972 struct ieee80211_node *ni = ic->ic_bss;
1973 struct ether_header *eh;
1974 struct ieee80211_frame *wh;
1975
1976 if (m->m_len < sizeof(struct ether_header)) {
1977 m = m_pullup(m, sizeof(struct ether_header));
1978 if (m == NULL)
1979 return NULL;
1980 }
1981 eh = mtod(m, struct ether_header *);
1982 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1983 if (m == NULL)
1984 return NULL;
1985 wh = mtod(m, struct ieee80211_frame *);
1986 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1987 *(u_int16_t *)wh->i_dur = 0;
1988 *(u_int16_t *)wh->i_seq =
1989 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
1990 ni->ni_txseq++;
1991 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1992 ic->ic_opmode == IEEE80211_M_AHDEMO) {
1993 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1994 if (sc->sc_adhoc_ap)
1995 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1996 else
1997 IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
1998 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
1999 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2000 } else {
2001 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
2002 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
2003 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2004 IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
2005 }
2006 return m;
2007 }
2008
2009 static struct mbuf *
2010 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
2011 {
2012 struct ieee80211com *ic = &sc->sc_ic;
2013 struct ether_header eh;
2014 struct ieee80211_frame wh;
2015 struct llc *llc;
2016
2017 if (m->m_len < sizeof(wh) + sizeof(eh)) {
2018 m = m_pullup(m, sizeof(wh) + sizeof(eh));
2019 if (m == NULL)
2020 return NULL;
2021 }
2022 memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
2023 if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
2024 return m;
2025 memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
2026 m_adj(m, sizeof(eh) - sizeof(*llc));
2027 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2028 ic->ic_opmode == IEEE80211_M_AHDEMO)
2029 IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
2030 memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
2031 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
2032 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
2033 llc->llc_control = LLC_UI;
2034 llc->llc_snap.org_code[0] = 0;
2035 llc->llc_snap.org_code[1] = 0;
2036 llc->llc_snap.org_code[2] = 0;
2037 llc->llc_snap.ether_type = eh.ether_type;
2038 return m;
2039 }
2040