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