awi.c revision 1.47 1 /* $NetBSD: awi.c,v 1.47 2002/09/27 05:36:06 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.47 2002/09/27 05:36:06 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 if (ime->ifm_media & IFM_IEEE80211_HOSTAP) {
882 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
883 ic->ic_opmode = IEEE80211_M_HOSTAP;
884 sc->sc_no_bssid = 0;
885 sc->sc_adhoc_ap = 0;
886 error = ENETRESET;
887 }
888 } else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
889 if (ic->ic_opmode != IEEE80211_M_ADHOC ||
890 sc->sc_adhoc_ap != 0) {
891 ic->ic_opmode = IEEE80211_M_ADHOC;
892 sc->sc_adhoc_ap = 0;
893 error = ENETRESET;
894 }
895 if (ic->ic_phytype == IEEE80211_T_DS &&
896 (ime->ifm_media & IFM_FLAG0)) {
897 if (sc->sc_no_bssid == 0) {
898 sc->sc_no_bssid = 1;
899 error = ENETRESET;
900 }
901 } else {
902 if (sc->sc_no_bssid) {
903 sc->sc_no_bssid = 0;
904 error = ENETRESET;
905 }
906 }
907 } else if (ime->ifm_media & IFM_FLAG0) {
908 if (ic->ic_opmode != IEEE80211_M_ADHOC ||
909 sc->sc_adhoc_ap == 0) {
910 ic->ic_opmode = IEEE80211_M_ADHOC;
911 sc->sc_adhoc_ap = 1;
912 sc->sc_no_bssid = 0;
913 error = ENETRESET;
914 }
915 } else {
916 if (ic->ic_opmode != IEEE80211_M_STA) {
917 ic->ic_opmode = IEEE80211_M_STA;
918 sc->sc_no_bssid = 0;
919 sc->sc_adhoc_ap = 0;
920 error = ENETRESET;
921 }
922 }
923 if (error == ENETRESET) {
924 if (sc->sc_enabled)
925 error = awi_init(ifp);
926 else
927 error = 0;
928 }
929 return error;
930 }
931
932 static void
933 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
934 {
935 struct awi_softc *sc = ifp->if_softc;
936 struct ieee80211com *ic = &sc->sc_ic;
937 int rate;
938
939 imr->ifm_status = IFM_AVALID;
940 if (ic->ic_state == IEEE80211_S_RUN)
941 imr->ifm_status |= IFM_ACTIVE;
942 imr->ifm_active = IFM_IEEE80211;
943 if (ic->ic_state == IEEE80211_S_RUN)
944 rate = ic->ic_bss.ni_rates[ic->ic_bss.ni_txrate] &
945 IEEE80211_RATE_VAL;
946 else {
947 if (ic->ic_fixed_rate == -1)
948 rate = 0;
949 else
950 rate = ic->ic_sup_rates[ic->ic_fixed_rate] &
951 IEEE80211_RATE_VAL;
952 }
953 imr->ifm_active |= ieee80211_rate2media(rate, ic->ic_phytype);
954 switch (ic->ic_opmode) {
955 case IEEE80211_M_STA:
956 break;
957 case IEEE80211_M_ADHOC:
958 if (sc->sc_adhoc_ap)
959 imr->ifm_active |= IFM_FLAG0;
960 else {
961 imr->ifm_active |= IFM_IEEE80211_ADHOC;
962 if (sc->sc_no_bssid)
963 imr->ifm_active |= IFM_FLAG0;
964 }
965 break;
966 case IEEE80211_M_HOSTAP:
967 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
968 break;
969 }
970 }
971
972 static int
973 awi_mode_init(struct awi_softc *sc)
974 {
975 struct ifnet *ifp = &sc->sc_ic.ic_if;
976 int n, error;
977 struct ether_multi *enm;
978 struct ether_multistep step;
979
980 /* reinitialize muticast filter */
981 n = 0;
982 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
983 if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
984 (ifp->if_flags & IFF_PROMISC)) {
985 sc->sc_mib_mac.aPromiscuous_Enable = 1;
986 goto set_mib;
987 }
988 sc->sc_mib_mac.aPromiscuous_Enable = 0;
989 ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
990 while (enm != NULL) {
991 if (n == AWI_GROUP_ADDR_SIZE ||
992 !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi))
993 goto set_mib;
994 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
995 enm->enm_addrlo);
996 n++;
997 ETHER_NEXT_MULTI(step, enm);
998 }
999 for (; n < AWI_GROUP_ADDR_SIZE; n++)
1000 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1001 IEEE80211_ADDR_LEN);
1002 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1003
1004 set_mib:
1005 if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1006 ifp->if_flags &= ~IFF_ALLMULTI;
1007 else
1008 ifp->if_flags |= IFF_ALLMULTI;
1009 sc->sc_mib_mgt.Wep_Required =
1010 (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? AWI_WEP_ON : AWI_WEP_OFF;
1011
1012 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1013 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1014 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1015 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1016 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1017 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1018 return error;
1019 }
1020 return 0;
1021 }
1022
1023 static void
1024 awi_rx_int(struct awi_softc *sc)
1025 {
1026 struct ifnet *ifp = &sc->sc_ic.ic_if;
1027 u_int8_t state, rate, rssi;
1028 u_int16_t len;
1029 u_int32_t frame, next, rstamp, rxoff;
1030 struct mbuf *m;
1031
1032 rxoff = sc->sc_rxdoff;
1033 for (;;) {
1034 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1035 if (state & AWI_RXD_ST_OWN)
1036 break;
1037 if (!(state & AWI_RXD_ST_CONSUMED)) {
1038 if (sc->sc_substate != AWI_ST_NONE)
1039 goto rx_next;
1040 if (state & AWI_RXD_ST_RXERROR) {
1041 ifp->if_ierrors++;
1042 goto rx_next;
1043 }
1044 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1045 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1046 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1047 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1048 0x7fff;
1049 rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1050 m = awi_devget(sc, frame, len);
1051 if (m == NULL) {
1052 ifp->if_ierrors++;
1053 goto rx_next;
1054 }
1055 if (state & AWI_RXD_ST_LF) {
1056 /* TODO check my bss */
1057 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1058 sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1059 sc->sc_rx_timer = 10;
1060 ifp->if_timer = 1;
1061 }
1062 if ((ifp->if_flags & IFF_DEBUG) &&
1063 (ifp->if_flags & IFF_LINK2))
1064 ieee80211_dump_pkt(m->m_data, m->m_len,
1065 rate / 5, rssi);
1066 if ((ifp->if_flags & IFF_LINK0) ||
1067 sc->sc_adhoc_ap)
1068 m = awi_ether_modcap(sc, m);
1069 if (m == NULL)
1070 ifp->if_ierrors++;
1071 else
1072 ieee80211_input(ifp, m, rssi, rstamp);
1073 } else
1074 sc->sc_rxpend = m;
1075 rx_next:
1076 state |= AWI_RXD_ST_CONSUMED;
1077 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1078 }
1079 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1080 if (next & AWI_RXD_NEXT_LAST)
1081 break;
1082 /* make sure the next pointer is correct */
1083 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1084 break;
1085 state |= AWI_RXD_ST_OWN;
1086 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1087 rxoff = next & 0x7fff;
1088 }
1089 sc->sc_rxdoff = rxoff;
1090 }
1091
1092 static void
1093 awi_tx_int(struct awi_softc *sc)
1094 {
1095 struct ifnet *ifp = &sc->sc_ic.ic_if;
1096 u_int8_t flags;
1097
1098 while (sc->sc_txdone != sc->sc_txnext) {
1099 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1100 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1101 break;
1102 if (flags & AWI_TXD_ST_ERROR)
1103 ifp->if_oerrors++;
1104 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1105 0x7fff;
1106 }
1107 DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1108 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1109 sc->sc_tx_timer = 0;
1110 ifp->if_flags &= ~IFF_OACTIVE;
1111 awi_start(ifp);
1112 }
1113
1114 static struct mbuf *
1115 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1116 {
1117 struct ifnet *ifp = &sc->sc_ic.ic_if;
1118 struct mbuf *m;
1119 struct mbuf *top, **mp;
1120 u_int tlen;
1121
1122 top = sc->sc_rxpend;
1123 mp = ⊤
1124 if (top != NULL) {
1125 sc->sc_rxpend = NULL;
1126 top->m_pkthdr.len += len;
1127 m = top;
1128 while (*mp != NULL) {
1129 m = *mp;
1130 mp = &m->m_next;
1131 }
1132 if (m->m_flags & M_EXT)
1133 tlen = m->m_ext.ext_size;
1134 else if (m->m_flags & M_PKTHDR)
1135 tlen = MHLEN;
1136 else
1137 tlen = MLEN;
1138 tlen -= m->m_len;
1139 if (tlen > len)
1140 tlen = len;
1141 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1142 off += tlen;
1143 len -= tlen;
1144 }
1145
1146 while (len > 0) {
1147 if (top == NULL) {
1148 MGETHDR(m, M_DONTWAIT, MT_DATA);
1149 if (m == NULL)
1150 return NULL;
1151 m->m_pkthdr.rcvif = ifp;
1152 m->m_pkthdr.len = len;
1153 m->m_len = MHLEN;
1154 m->m_flags |= M_HASFCS;
1155 } else {
1156 MGET(m, M_DONTWAIT, MT_DATA);
1157 if (m == NULL) {
1158 m_freem(top);
1159 return NULL;
1160 }
1161 m->m_len = MLEN;
1162 }
1163 if (len >= MINCLSIZE) {
1164 MCLGET(m, M_DONTWAIT);
1165 if (m->m_flags & M_EXT)
1166 m->m_len = m->m_ext.ext_size;
1167 }
1168 if (top == NULL) {
1169 int hdrlen = sizeof(struct ieee80211_frame) +
1170 sizeof(struct llc);
1171 caddr_t newdata = (caddr_t)
1172 ALIGN(m->m_data + hdrlen) - hdrlen;
1173 m->m_len -= newdata - m->m_data;
1174 m->m_data = newdata;
1175 }
1176 if (m->m_len > len)
1177 m->m_len = len;
1178 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1179 off += m->m_len;
1180 len -= m->m_len;
1181 *mp = m;
1182 mp = &m->m_next;
1183 }
1184 return top;
1185 }
1186
1187 /*
1188 * Initialize hardware and start firmware to accept commands.
1189 * Called everytime after power on firmware.
1190 */
1191
1192 static int
1193 awi_hw_init(struct awi_softc *sc)
1194 {
1195 u_int8_t status;
1196 u_int16_t intmask;
1197 int i, error;
1198
1199 sc->sc_enab_intr = 0;
1200 sc->sc_invalid = 0; /* XXX: really? */
1201 awi_drvstate(sc, AWI_DRV_RESET);
1202
1203 /* reset firmware */
1204 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1205 DELAY(100);
1206 awi_write_1(sc, AWI_SELFTEST, 0);
1207 awi_write_1(sc, AWI_CMD, 0);
1208 awi_write_1(sc, AWI_BANNER, 0);
1209 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1210 DELAY(100);
1211
1212 /* wait for selftest completion */
1213 for (i = 0; ; i++) {
1214 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1215 printf("%s: failed to complete selftest (timeout)\n",
1216 sc->sc_dev.dv_xname);
1217 return ENXIO;
1218 }
1219 status = awi_read_1(sc, AWI_SELFTEST);
1220 if ((status & 0xf0) == 0xf0)
1221 break;
1222 if (sc->sc_cansleep) {
1223 sc->sc_sleep_cnt++;
1224 (void)tsleep(sc, PWAIT, "awitst", 1);
1225 sc->sc_sleep_cnt--;
1226 } else {
1227 DELAY(1000*1000/hz);
1228 }
1229 }
1230 if (status != AWI_SELFTEST_PASSED) {
1231 printf("%s: failed to complete selftest (code %x)\n",
1232 sc->sc_dev.dv_xname, status);
1233 return ENXIO;
1234 }
1235
1236 /* check banner to confirm firmware write it */
1237 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1238 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1239 printf("%s: failed to complete selftest (bad banner)\n",
1240 sc->sc_dev.dv_xname);
1241 for (i = 0; i < AWI_BANNER_LEN; i++)
1242 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1243 printf("\n");
1244 return ENXIO;
1245 }
1246
1247 /* initializing interrupt */
1248 sc->sc_enab_intr = 1;
1249 error = awi_intr_lock(sc);
1250 if (error)
1251 return error;
1252 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1253 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1254 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1255 awi_write_1(sc, AWI_INTMASK2, 0);
1256 awi_write_1(sc, AWI_INTSTAT, 0);
1257 awi_write_1(sc, AWI_INTSTAT2, 0);
1258 awi_intr_unlock(sc);
1259 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1260
1261 /* issuing interface test command */
1262 error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1263 if (error) {
1264 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1265 if (error == ENXIO)
1266 printf(" (no hardware)\n");
1267 else if (error != EWOULDBLOCK)
1268 printf(" (error %d)\n", error);
1269 else if (sc->sc_cansleep)
1270 printf(" (lost interrupt)\n");
1271 else
1272 printf(" (command timeout)\n");
1273 return error;
1274 }
1275
1276 /* Initialize VBM */
1277 awi_write_1(sc, AWI_VBM_OFFSET, 0);
1278 awi_write_1(sc, AWI_VBM_LENGTH, 1);
1279 awi_write_1(sc, AWI_VBM_BITMAP, 0);
1280 return 0;
1281 }
1282
1283 /*
1284 * Extract the factory default MIB value from firmware and assign the driver
1285 * default value.
1286 * Called once at attaching the interface.
1287 */
1288
1289 static int
1290 awi_init_mibs(struct awi_softc *sc)
1291 {
1292 int i, error;
1293 struct awi_chanset *cs;
1294
1295 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1296 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1297 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1298 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1299 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1300 printf("%s: failed to get default mib value (error %d)\n",
1301 sc->sc_dev.dv_xname, error);
1302 return error;
1303 }
1304
1305 memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1306 for (cs = awi_chanset; ; cs++) {
1307 if (cs->cs_type == 0) {
1308 printf("%s: failed to set available channel\n",
1309 sc->sc_dev.dv_xname);
1310 return ENXIO;
1311 }
1312 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1313 cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1314 break;
1315 }
1316 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1317 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1318 setbit(sc->sc_ic.ic_chan_avail,
1319 IEEE80211_FH_CHAN(i % 3 + 1, i));
1320 /*
1321 * According to the IEEE 802.11 specification,
1322 * hop pattern parameter for FH phy should be
1323 * incremented by 3 for given hop chanset, i.e.,
1324 * the chanset parameter is calculated for given
1325 * hop patter. However, BayStack 650 Access Points
1326 * apparently use fixed hop chanset parameter value
1327 * 1 for any hop pattern. So we also try this
1328 * combination of hop chanset and pattern.
1329 */
1330 setbit(sc->sc_ic.ic_chan_avail,
1331 IEEE80211_FH_CHAN(1, i));
1332 }
1333 } else {
1334 for (i = cs->cs_min; i <= cs->cs_max; i++)
1335 setbit(sc->sc_ic.ic_chan_avail, i);
1336 }
1337 sc->sc_cur_chan = cs->cs_def;
1338
1339 sc->sc_mib_local.Fragmentation_Dis = 1;
1340 sc->sc_mib_local.Add_PLCP_Dis = 0;
1341 sc->sc_mib_local.MAC_Hdr_Prsv = 1;
1342 sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1343 sc->sc_mib_local.Re_Assembly_Dis = 1;
1344 sc->sc_mib_local.Strip_PLCP_Dis = 0;
1345 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1346 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1347 sc->sc_mib_local.Check_Seq_Cntl_Dis = 1;
1348 sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1349 sc->sc_mib_local.Network_Mode = 1;
1350 sc->sc_mib_local.PWD_Lvl = 0;
1351 sc->sc_mib_local.CFP_Mode = 0;
1352
1353 /* allocate buffers */
1354 sc->sc_txbase = AWI_BUFFERS;
1355 sc->sc_txend = sc->sc_txbase +
1356 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1357 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1358 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1359 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1360 sc->sc_txend - sc->sc_txbase);
1361 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1362 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1363 AWI_BUFFERS_END - sc->sc_txend);
1364 sc->sc_mib_local.Acting_as_AP = 0;
1365 sc->sc_mib_local.Fill_CFP = 0;
1366
1367 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1368 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1369
1370 sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1371 sc->sc_mib_mgt.aDTIM_Period = 1;
1372 LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1373 return 0;
1374 }
1375
1376 static int
1377 awi_chan_check(void *arg, u_char *chanreq)
1378 {
1379 struct awi_softc *sc = arg;
1380 int i;
1381 struct awi_chanset *cs;
1382 u_char chanlist[(IEEE80211_CHAN_MAX+1)/NBBY];
1383
1384 for (cs = awi_chanset; cs->cs_type != 0; cs++) {
1385 if (cs->cs_type != sc->sc_mib_phy.IEEE_PHY_Type)
1386 continue;
1387 memset(chanlist, 0, sizeof(chanlist));
1388 for (i = 0; ; i++) {
1389 if (i == IEEE80211_CHAN_MAX) {
1390 sc->sc_mib_phy.aCurrent_Reg_Domain =
1391 cs->cs_region;
1392 memcpy(sc->sc_ic.ic_chan_avail, chanlist,
1393 sizeof(sc->sc_ic.ic_chan_avail));
1394 sc->sc_ic.ic_bss.ni_chan = cs->cs_def;
1395 sc->sc_cur_chan = cs->cs_def;
1396 return 0;
1397 }
1398 if (i >= cs->cs_min && i <= cs->cs_max)
1399 setbit(chanlist, i);
1400 else if (isset(chanreq, i))
1401 break;
1402 }
1403 }
1404 return EINVAL;
1405 }
1406
1407 static int
1408 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1409 {
1410 int error;
1411 u_int8_t size, *ptr;
1412
1413 switch (mib) {
1414 case AWI_MIB_LOCAL:
1415 ptr = (u_int8_t *)&sc->sc_mib_local;
1416 size = sizeof(sc->sc_mib_local);
1417 break;
1418 case AWI_MIB_ADDR:
1419 ptr = (u_int8_t *)&sc->sc_mib_addr;
1420 size = sizeof(sc->sc_mib_addr);
1421 break;
1422 case AWI_MIB_MAC:
1423 ptr = (u_int8_t *)&sc->sc_mib_mac;
1424 size = sizeof(sc->sc_mib_mac);
1425 break;
1426 case AWI_MIB_STAT:
1427 ptr = (u_int8_t *)&sc->sc_mib_stat;
1428 size = sizeof(sc->sc_mib_stat);
1429 break;
1430 case AWI_MIB_MGT:
1431 ptr = (u_int8_t *)&sc->sc_mib_mgt;
1432 size = sizeof(sc->sc_mib_mgt);
1433 break;
1434 case AWI_MIB_PHY:
1435 ptr = (u_int8_t *)&sc->sc_mib_phy;
1436 size = sizeof(sc->sc_mib_phy);
1437 break;
1438 default:
1439 return EINVAL;
1440 }
1441 if (sc->sc_cmd_inprog) {
1442 if ((error = awi_cmd_wait(sc)) != 0) {
1443 if (error == EWOULDBLOCK)
1444 DPRINTF(("awi_mib: cmd %d inprog",
1445 sc->sc_cmd_inprog));
1446 return error;
1447 }
1448 }
1449 sc->sc_cmd_inprog = cmd;
1450 if (cmd == AWI_CMD_SET_MIB)
1451 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1452 awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1453 awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1454 awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1455 if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1456 return error;
1457 if (cmd == AWI_CMD_GET_MIB) {
1458 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1459 #ifdef AWI_DEBUG
1460 if (awi_debug) {
1461 int i;
1462
1463 printf("awi_mib: #%d:", mib);
1464 for (i = 0; i < size; i++)
1465 printf(" %02x", ptr[i]);
1466 printf("\n");
1467 }
1468 #endif
1469 }
1470 return 0;
1471 }
1472
1473 static int
1474 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1475 {
1476 u_int8_t status;
1477 int error = 0;
1478 #ifdef AWI_DEBUG
1479 static const char *cmdname[] = {
1480 "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1481 "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1482 };
1483 #endif
1484
1485 #ifdef AWI_DEBUG
1486 if (awi_debug > 1) {
1487 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1488 printf("awi_cmd: #%d", cmd);
1489 else
1490 printf("awi_cmd: %s", cmdname[cmd]);
1491 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1492 }
1493 #endif
1494 sc->sc_cmd_inprog = cmd;
1495 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1496 awi_write_1(sc, AWI_CMD, cmd);
1497 if (wflag == AWI_NOWAIT)
1498 return EINPROGRESS;
1499 if ((error = awi_cmd_wait(sc)) != 0)
1500 return error;
1501 status = awi_read_1(sc, AWI_CMD_STATUS);
1502 awi_write_1(sc, AWI_CMD, 0);
1503 switch (status) {
1504 case AWI_STAT_OK:
1505 break;
1506 case AWI_STAT_BADPARM:
1507 return EINVAL;
1508 default:
1509 printf("%s: command %d failed %x\n",
1510 sc->sc_dev.dv_xname, cmd, status);
1511 return ENXIO;
1512 }
1513 return 0;
1514 }
1515
1516 static int
1517 awi_cmd_wait(struct awi_softc *sc)
1518 {
1519 int i, error = 0;
1520
1521 i = 0;
1522 while (sc->sc_cmd_inprog) {
1523 if (sc->sc_invalid)
1524 return ENXIO;
1525 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1526 printf("%s: failed to access hardware\n",
1527 sc->sc_dev.dv_xname);
1528 sc->sc_invalid = 1;
1529 return ENXIO;
1530 }
1531 if (sc->sc_cansleep) {
1532 sc->sc_sleep_cnt++;
1533 error = tsleep(sc, PWAIT, "awicmd",
1534 AWI_CMD_TIMEOUT*hz/1000);
1535 sc->sc_sleep_cnt--;
1536 } else {
1537 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1538 awi_cmd_done(sc);
1539 break;
1540 }
1541 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1542 error = EWOULDBLOCK;
1543 else
1544 DELAY(10);
1545 }
1546 if (error)
1547 break;
1548 }
1549 if (error) {
1550 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1551 sc->sc_cmd_inprog, error));
1552 }
1553 return error;
1554 }
1555
1556 static void
1557 awi_cmd_done(struct awi_softc *sc)
1558 {
1559 u_int8_t cmd, status;
1560
1561 status = awi_read_1(sc, AWI_CMD_STATUS);
1562 if (status == AWI_STAT_IDLE)
1563 return; /* stray interrupt */
1564
1565 cmd = sc->sc_cmd_inprog;
1566 sc->sc_cmd_inprog = 0;
1567 wakeup(sc);
1568 awi_write_1(sc, AWI_CMD, 0);
1569
1570 if (status != AWI_STAT_OK) {
1571 printf("%s: command %d failed %x\n",
1572 sc->sc_dev.dv_xname, cmd, status);
1573 sc->sc_substate = AWI_ST_NONE;
1574 return;
1575 }
1576 if (sc->sc_substate != AWI_ST_NONE)
1577 (void)ieee80211_new_state(&sc->sc_ic.ic_if, sc->sc_nstate, -1);
1578 }
1579
1580 static int
1581 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1582 {
1583 u_int32_t txd, ntxd, frame;
1584
1585 txd = sc->sc_txnext;
1586 frame = txd + AWI_TXD_SIZE;
1587 if (frame + len > sc->sc_txend)
1588 frame = sc->sc_txbase;
1589 ntxd = frame + len;
1590 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1591 ntxd = sc->sc_txbase;
1592 *framep = frame;
1593 *ntxdp = ntxd;
1594 /*
1595 * Determine if there are any room in ring buffer.
1596 * --- send wait, === new data, +++ conflict (ENOBUFS)
1597 * base........................end
1598 * done----txd=====ntxd OK
1599 * --txd=====done++++ntxd-- full
1600 * --txd=====ntxd done-- OK
1601 * ==ntxd done----txd=== OK
1602 * ==done++++ntxd----txd=== full
1603 * ++ntxd txd=====done++ full
1604 */
1605 if (txd < ntxd) {
1606 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1607 return ENOBUFS;
1608 } else {
1609 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1610 return ENOBUFS;
1611 }
1612 return 0;
1613 }
1614
1615 static int
1616 awi_lock(struct awi_softc *sc)
1617 {
1618 int error = 0;
1619
1620 if (curproc == NULL) {
1621 /*
1622 * XXX
1623 * Though driver ioctl should be called with context,
1624 * KAME ipv6 stack calls ioctl in interrupt for now.
1625 * We simply abort the request if there are other
1626 * ioctl requests in progress.
1627 */
1628 if (sc->sc_busy) {
1629 return EWOULDBLOCK;
1630 if (sc->sc_invalid)
1631 return ENXIO;
1632 }
1633 sc->sc_busy = 1;
1634 sc->sc_cansleep = 0;
1635 return 0;
1636 }
1637 while (sc->sc_busy) {
1638 if (sc->sc_invalid)
1639 return ENXIO;
1640 sc->sc_sleep_cnt++;
1641 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1642 sc->sc_sleep_cnt--;
1643 if (error)
1644 return error;
1645 }
1646 sc->sc_busy = 1;
1647 sc->sc_cansleep = 1;
1648 return 0;
1649 }
1650
1651 static void
1652 awi_unlock(struct awi_softc *sc)
1653 {
1654 sc->sc_busy = 0;
1655 sc->sc_cansleep = 0;
1656 if (sc->sc_sleep_cnt)
1657 wakeup(sc);
1658 }
1659
1660 static int
1661 awi_intr_lock(struct awi_softc *sc)
1662 {
1663 u_int8_t status;
1664 int i, retry;
1665
1666 status = 1;
1667 for (retry = 0; retry < 10; retry++) {
1668 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1669 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1670 break;
1671 DELAY(5);
1672 }
1673 if (status != 0)
1674 break;
1675 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1676 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1677 break;
1678 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1679 }
1680 if (status != 0) {
1681 printf("%s: failed to lock interrupt\n",
1682 sc->sc_dev.dv_xname);
1683 return ENXIO;
1684 }
1685 return 0;
1686 }
1687
1688 static void
1689 awi_intr_unlock(struct awi_softc *sc)
1690 {
1691
1692 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1693 }
1694
1695 static int
1696 awi_newstate(void *arg, enum ieee80211_state nstate)
1697 {
1698 struct awi_softc *sc = arg;
1699 struct ieee80211com *ic = &sc->sc_ic;
1700 struct ieee80211_node *ni;
1701 struct ifnet *ifp = &ic->ic_if;
1702 int error;
1703 u_int8_t newmode;
1704 enum ieee80211_state ostate;
1705 #ifdef AWI_DEBUG
1706 static const char *stname[] =
1707 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1708 static const char *substname[] =
1709 { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1710 "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1711 #endif /* AWI_DEBUG */
1712
1713 ostate = ic->ic_state;
1714 DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1715 stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1716
1717 /* set LED */
1718 switch (nstate) {
1719 case IEEE80211_S_INIT:
1720 awi_drvstate(sc, AWI_DRV_RESET);
1721 break;
1722 case IEEE80211_S_SCAN:
1723 if (ic->ic_opmode == IEEE80211_M_ADHOC)
1724 awi_drvstate(sc, AWI_DRV_ADHSC);
1725 else
1726 awi_drvstate(sc, AWI_DRV_INFSY);
1727 break;
1728 case IEEE80211_S_AUTH:
1729 awi_drvstate(sc, AWI_DRV_INFSY);
1730 break;
1731 case IEEE80211_S_ASSOC:
1732 awi_drvstate(sc, AWI_DRV_INFAUTH);
1733 break;
1734 case IEEE80211_S_RUN:
1735 if (ic->ic_opmode == IEEE80211_M_ADHOC)
1736 awi_drvstate(sc, AWI_DRV_ADHSY);
1737 else
1738 awi_drvstate(sc, AWI_DRV_INFASSOC);
1739 break;
1740 }
1741
1742 if (nstate == IEEE80211_S_INIT) {
1743 sc->sc_substate = AWI_ST_NONE;
1744 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1745 return 0;
1746 }
1747
1748 /* state transition */
1749 if (nstate == IEEE80211_S_SCAN) {
1750 /* SCAN substate */
1751 if (sc->sc_substate == AWI_ST_NONE) {
1752 sc->sc_nstate = nstate; /* next state in transition */
1753 sc->sc_substate = AWI_ST_SCAN_INIT;
1754 }
1755 switch (sc->sc_substate) {
1756 case AWI_ST_SCAN_INIT:
1757 sc->sc_substate = AWI_ST_SCAN_SETMIB;
1758 switch (ostate) {
1759 case IEEE80211_S_RUN:
1760 /* beacon miss */
1761 if (ifp->if_flags & IFF_DEBUG)
1762 printf("%s: no recent beacons from %s;"
1763 " rescanning\n",
1764 ifp->if_xname,
1765 ether_sprintf(ic->ic_bss.ni_bssid));
1766 /* FALLTHRU */
1767 case IEEE80211_S_AUTH:
1768 case IEEE80211_S_ASSOC:
1769 /* timeout restart scan */
1770 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
1771 ieee80211_free_node(ic, ni);
1772 /* FALLTHRU */
1773 case IEEE80211_S_INIT:
1774 ic->ic_flags |= IEEE80211_F_ASCAN;
1775 ic->ic_scan_timer = 0;
1776 /* FALLTHRU */
1777 case IEEE80211_S_SCAN:
1778 /* scan next */
1779 break;
1780 }
1781 if (ic->ic_flags & IEEE80211_F_ASCAN)
1782 newmode = AWI_SCAN_ACTIVE;
1783 else
1784 newmode = AWI_SCAN_PASSIVE;
1785 if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1786 sc->sc_mib_mgt.aScan_Mode = newmode;
1787 if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1788 AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1789 break;
1790 }
1791 /* FALLTHRU */
1792 case AWI_ST_SCAN_SETMIB:
1793 sc->sc_substate = AWI_ST_SCAN_SCCMD;
1794 if (sc->sc_cmd_inprog) {
1795 if ((error = awi_cmd_wait(sc)) != 0)
1796 break;
1797 }
1798 sc->sc_cmd_inprog = AWI_CMD_SCAN;
1799 ni = &ic->ic_bss;
1800 awi_write_2(sc, AWI_CA_SCAN_DURATION,
1801 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1802 AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1803 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1804 awi_write_1(sc, AWI_CA_SCAN_SET,
1805 IEEE80211_FH_CHANSET(ni->ni_chan));
1806 awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1807 IEEE80211_FH_CHANPAT(ni->ni_chan));
1808 awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1809 } else {
1810 awi_write_1(sc, AWI_CA_SCAN_SET, ni->ni_chan);
1811 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1812 awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1813 }
1814 awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1815 sc->sc_cur_chan = ni->ni_chan;
1816 if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1817 != 0)
1818 break;
1819 /* FALLTHRU */
1820 case AWI_ST_SCAN_SCCMD:
1821 if (ic->ic_scan_timer == 0)
1822 ic->ic_scan_timer =
1823 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1824 IEEE80211_ASCAN_WAIT : IEEE80211_PSCAN_WAIT;
1825 ifp->if_timer = 1;
1826 ic->ic_state = nstate;
1827 sc->sc_substate = AWI_ST_NONE;
1828 error = EINPROGRESS;
1829 break;
1830 default:
1831 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1832 stname[nstate], substname[sc->sc_substate]));
1833 sc->sc_substate = AWI_ST_NONE;
1834 error = EIO;
1835 break;
1836 }
1837 return error;
1838 }
1839
1840 if (ostate == IEEE80211_S_SCAN) {
1841 /* set SSID and channel */
1842 /* substate */
1843 if (sc->sc_substate == AWI_ST_NONE) {
1844 sc->sc_nstate = nstate; /* next state in transition */
1845 sc->sc_substate = AWI_ST_SUB_INIT;
1846 }
1847 ni = &ic->ic_bss;
1848 switch (sc->sc_substate) {
1849 case AWI_ST_SUB_INIT:
1850 sc->sc_substate = AWI_ST_SUB_SETSS;
1851 IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
1852 ni->ni_bssid);
1853 memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
1854 AWI_ESS_ID_SIZE);
1855 sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
1856 IEEE80211_ELEMID_SSID;
1857 sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
1858 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
1859 ni->ni_essid, ni->ni_esslen);
1860 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
1861 ni->ni_intval);
1862 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
1863 AWI_NOWAIT)) != 0)
1864 break;
1865 /* FALLTHRU */
1866 case AWI_ST_SUB_SETSS:
1867 sc->sc_substate = AWI_ST_SUB_SYNC;
1868 if (sc->sc_cmd_inprog) {
1869 if ((error = awi_cmd_wait(sc)) != 0)
1870 break;
1871 }
1872 sc->sc_cmd_inprog = AWI_CMD_SYNC;
1873 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1874 awi_write_1(sc, AWI_CA_SYNC_SET,
1875 IEEE80211_FH_CHANSET(ni->ni_chan));
1876 awi_write_1(sc, AWI_CA_SYNC_PATTERN,
1877 IEEE80211_FH_CHANPAT(ni->ni_chan));
1878 awi_write_1(sc, AWI_CA_SYNC_IDX,
1879 ni->ni_fhindex);
1880 awi_write_2(sc, AWI_CA_SYNC_DWELL,
1881 ni->ni_fhdwell);
1882 } else {
1883 awi_write_1(sc, AWI_CA_SYNC_SET, ni->ni_chan);
1884 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
1885 awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
1886 awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
1887 }
1888 if ((ic->ic_flags & IEEE80211_F_SIBSS) &&
1889 !sc->sc_no_bssid)
1890 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
1891 else
1892 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
1893 awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
1894 awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
1895 ni->ni_tstamp, 8);
1896 awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
1897 sc->sc_cur_chan = ni->ni_chan;
1898 if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
1899 != 0)
1900 break;
1901 /* FALLTHRU */
1902 case AWI_ST_SUB_SYNC:
1903 sc->sc_substate = AWI_ST_NONE;
1904 if (ic->ic_flags & IEEE80211_F_SIBSS) {
1905 if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
1906 AWI_MIB_MGT, AWI_WAIT)) != 0)
1907 break;
1908 IEEE80211_ADDR_COPY(ni->ni_bssid,
1909 &sc->sc_mib_mgt.aCurrent_BSS_ID);
1910 } else {
1911 if (nstate == IEEE80211_S_RUN) {
1912 sc->sc_rx_timer = 10;
1913 ifp->if_timer = 1;
1914 }
1915 }
1916 error = 0;
1917 break;
1918 default:
1919 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1920 stname[nstate], substname[sc->sc_substate]));
1921 sc->sc_substate = AWI_ST_NONE;
1922 error = EIO;
1923 break;
1924 }
1925 return error;
1926 }
1927
1928 sc->sc_substate = AWI_ST_NONE;
1929
1930 return 0;
1931 }
1932
1933 static struct mbuf *
1934 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
1935 {
1936 struct ieee80211com *ic = &sc->sc_ic;
1937 struct ieee80211_node *ni = &ic->ic_bss;
1938 struct ether_header *eh;
1939 struct ieee80211_frame *wh;
1940
1941 if (m->m_len < sizeof(struct ether_header)) {
1942 m = m_pullup(m, sizeof(struct ether_header));
1943 if (m == NULL)
1944 return NULL;
1945 }
1946 eh = mtod(m, struct ether_header *);
1947 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1948 if (m == NULL)
1949 return NULL;
1950 wh = mtod(m, struct ieee80211_frame *);
1951 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1952 *(u_int16_t *)wh->i_dur = 0;
1953 *(u_int16_t *)wh->i_seq =
1954 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
1955 ni->ni_txseq++;
1956 if (ic->ic_opmode == IEEE80211_M_ADHOC) {
1957 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1958 if (sc->sc_adhoc_ap)
1959 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1960 else
1961 IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
1962 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
1963 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1964 } else {
1965 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1966 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1967 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
1968 IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
1969 }
1970 return m;
1971 }
1972
1973 static struct mbuf *
1974 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
1975 {
1976 struct ieee80211com *ic = &sc->sc_ic;
1977 struct ether_header eh;
1978 struct ieee80211_frame wh;
1979 struct llc *llc;
1980
1981 if (m->m_len < sizeof(wh) + sizeof(eh)) {
1982 m = m_pullup(m, sizeof(wh) + sizeof(eh));
1983 if (m == NULL)
1984 return NULL;
1985 }
1986 memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
1987 if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
1988 return m;
1989 memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
1990 m_adj(m, sizeof(eh) - sizeof(*llc));
1991 if (ic->ic_opmode == IEEE80211_M_ADHOC)
1992 IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
1993 memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
1994 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
1995 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1996 llc->llc_control = LLC_UI;
1997 llc->llc_snap.org_code[0] = 0;
1998 llc->llc_snap.org_code[1] = 0;
1999 llc->llc_snap.org_code[2] = 0;
2000 llc->llc_snap.ether_type = eh.ether_type;
2001 return m;
2002 }
2003