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