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