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