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