2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $Id: if_wi.c,v 1.9 1999/08/21 06:24:15 msmith Exp $
36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
38 * Written by Bill Paul <wpaul@ctr.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
59 * This driver supports both the PCMCIA and ISA versions of the
60 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
61 * anything of the sort: it's actually a PCMCIA bridge adapter
62 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
63 * inserted. Consequently, you need to use the pccard support for
64 * both the ISA and PCMCIA adapters.
67 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
68 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/eventhandler.h>
77 #include <sys/sockio.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/socket.h>
84 #include <net/if_arp.h>
85 #include <net/ethernet.h>
86 #include <net/if_dl.h>
87 #include <net/if_media.h>
88 #include <net/if_types.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/if_ether.h>
102 #include <machine/clock.h>
103 #include <machine/md_var.h>
104 #include <machine/bus_pio.h>
105 #include <machine/bus.h>
107 #include <i386/isa/isa_device.h>
108 #include <i386/isa/icu.h>
109 #include <i386/isa/if_wireg.h>
110 #include <machine/if_wavelan_ieee.h>
113 #include <sys/select.h>
114 #include <pccard/cardinfo.h>
115 #include <pccard/slot.h>
119 static const char rcsid[] =
120 "$Id: if_wi.c,v 1.9 1999/08/21 06:24:15 msmith Exp $";
123 static struct wi_softc wi_softc[NWI];
126 static u_int8_t wi_mcast_addr[6] = { 0x01, 0x60, 0x1D, 0x00, 0x01, 0x00 };
129 static int wi_probe __P((struct isa_device *));
130 static int wi_attach __P((struct isa_device *));
132 static ointhand2_t wi_intr;
134 static void wi_reset __P((struct wi_softc *));
135 static int wi_ioctl __P((struct ifnet *, u_long, caddr_t));
136 static void wi_init __P((void *));
137 static void wi_start __P((struct ifnet *));
138 static void wi_stop __P((struct wi_softc *));
139 static void wi_watchdog __P((struct ifnet *));
140 static void wi_shutdown __P((void *, int));
141 static void wi_rxeof __P((struct wi_softc *));
142 static void wi_txeof __P((struct wi_softc *, int));
143 static void wi_update_stats __P((struct wi_softc *));
144 static void wi_setmulti __P((struct wi_softc *));
146 static int wi_cmd __P((struct wi_softc *, int, int));
147 static int wi_read_record __P((struct wi_softc *, struct wi_ltv_gen *));
148 static int wi_write_record __P((struct wi_softc *, struct wi_ltv_gen *));
149 static int wi_read_data __P((struct wi_softc *, int,
151 static int wi_write_data __P((struct wi_softc *, int,
153 static int wi_seek __P((struct wi_softc *, int, int, int));
154 static int wi_alloc_nicmem __P((struct wi_softc *, int, int *));
155 static void wi_inquire __P((void *));
156 static void wi_setdef __P((struct wi_softc *, struct wi_req *));
157 static int wi_mgmt_xmit __P((struct wi_softc *, caddr_t, int));
159 struct isa_driver widriver = {
167 static int wi_pccard_init __P((struct pccard_devinfo *));
168 static void wi_pccard_unload __P((struct pccard_devinfo *));
169 static int wi_pccard_intr __P((struct pccard_devinfo *));
172 PCCARD_MODULE(wi, wi_pccard_init, wi_pccard_unload,
173 wi_pccard_intr, 0, net_imask);
175 static struct pccard_device wi_info = {
180 0, /* Attributes - presently unused */
181 &net_imask /* Interrupt mask for device */
182 /* XXX - Should this also include net_imask? */
185 DATA_SET(pccarddrv_set, wi_info);
188 /* Initialize the PCCARD. */
189 static int wi_pccard_init(sc_p)
190 struct pccard_devinfo *sc_p;
196 if (sc_p->isahd.id_unit >= NWI)
199 sc = &wi_softc[sc_p->isahd.id_unit];
201 sc->wi_unit = sc_p->isahd.id_unit;
202 sc->wi_bhandle = sc_p->isahd.id_iobase;
203 sc->wi_btag = I386_BUS_SPACE_IO;
205 /* Make sure interrupts are disabled. */
206 CSR_WRITE_2(sc, WI_INT_EN, 0);
207 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
209 /* Grr. IRQ is encoded as a bitmask. */
210 irq = sc_p->isahd.id_irq;
211 for (i = 0; i < 32; i++) {
218 * Print a nice probe message to let the operator
219 * know something interesting is happening.
221 printf("wi%d: <WaveLAN/IEEE 802.11> at 0x%x-0x%x irq %d on isa\n",
222 sc_p->isahd.id_unit, sc_p->isahd.id_iobase,
223 sc_p->isahd.id_iobase + WI_IOSIZ - 1, i);
225 if (wi_attach(&sc_p->isahd))
231 static void wi_pccard_unload(sc_p)
232 struct pccard_devinfo *sc_p;
237 sc = &wi_softc[sc_p->isahd.id_unit];
238 ifp = &sc->arpcom.ac_if;
241 printf("wi%d: already unloaded\n", sc_p->isahd.id_unit);
245 ifp->if_flags &= ~IFF_RUNNING;
248 printf("wi%d: unloaded\n", sc_p->isahd.id_unit);
253 static int wi_pccard_intr(sc_p)
254 struct pccard_devinfo *sc_p;
256 wi_intr(sc_p->isahd.id_unit);
261 static int wi_probe(isa_dev)
262 struct isa_device *isa_dev;
265 * The ISA WaveLAN/IEEE card is actually not an ISA card:
266 * it's a PCMCIA card plugged into a PCMCIA bridge adapter
267 * that fits into an ISA slot. Consequently, we will always
268 * be using the pccard support to probe and attach these
269 * devices, so we can never actually probe one from here.
274 static int wi_attach(isa_dev)
275 struct isa_device *isa_dev;
278 struct wi_ltv_macaddr mac;
279 struct wi_ltv_gen gen;
281 char ifname[IFNAMSIZ];
284 isa_dev->id_ointr = wi_intr;
286 sc = &wi_softc[isa_dev->id_unit];
287 ifp = &sc->arpcom.ac_if;
292 /* Read the station address. */
293 mac.wi_type = WI_RID_MAC_NODE;
295 wi_read_record(sc, (struct wi_ltv_gen *)&mac);
296 bcopy((char *)&mac.wi_mac_addr,
297 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
299 printf("wi%d: Ethernet address: %6D\n", sc->wi_unit,
300 sc->arpcom.ac_enaddr, ":");
303 ifp->if_unit = sc->wi_unit;
305 ifp->if_mtu = ETHERMTU;
306 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
307 ifp->if_ioctl = wi_ioctl;
308 ifp->if_output = ether_output;
309 ifp->if_start = wi_start;
310 ifp->if_watchdog = wi_watchdog;
311 ifp->if_init = wi_init;
312 ifp->if_baudrate = 10000000;
313 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
315 bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
316 bcopy(WI_DEFAULT_NODENAME, sc->wi_node_name,
317 sizeof(WI_DEFAULT_NODENAME) - 1);
319 bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
320 bcopy(WI_DEFAULT_NETNAME, sc->wi_net_name,
321 sizeof(WI_DEFAULT_NETNAME) - 1);
323 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
324 bcopy(WI_DEFAULT_IBSS, sc->wi_ibss_name,
325 sizeof(WI_DEFAULT_IBSS) - 1);
327 sc->wi_portnum = WI_DEFAULT_PORT;
328 sc->wi_ptype = WI_PORTTYPE_ADHOC;
329 sc->wi_ap_density = WI_DEFAULT_AP_DENSITY;
330 sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH;
331 sc->wi_tx_rate = WI_DEFAULT_TX_RATE;
332 sc->wi_max_data_len = WI_DEFAULT_DATALEN;
333 sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS;
334 sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED;
335 sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP;
338 * Read the default channel from the NIC. This may vary
339 * depending on the country where the NIC was purchased, so
340 * we can't hard-code a default and expect it to work for
343 gen.wi_type = WI_RID_OWN_CHNL;
345 wi_read_record(sc, &gen);
346 sc->wi_channel = gen.wi_val;
348 bzero((char *)&sc->wi_stats, sizeof(sc->wi_stats));
354 * If this logical interface has already been attached,
355 * don't attach it again or chaos will ensue.
357 sprintf(ifname, "wi%d", sc->wi_unit);
359 if (ifunit(ifname) == NULL) {
360 callout_handle_init(&sc->wi_stat_ch);
362 * Call MI attach routines.
368 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
371 EVENTHANDLER_REGISTER(shutdown_post_sync, wi_shutdown, sc,
372 SHUTDOWN_PRI_DEFAULT);
378 static void wi_rxeof(sc)
382 struct ether_header *eh;
383 struct wi_frame rx_frame;
387 ifp = &sc->arpcom.ac_if;
389 id = CSR_READ_2(sc, WI_RX_FID);
391 /* First read in the frame header */
392 if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) {
397 if (rx_frame.wi_status & WI_STAT_ERRSTAT) {
402 MGETHDR(m, M_DONTWAIT, MT_DATA);
407 MCLGET(m, M_DONTWAIT);
408 if (!(m->m_flags & M_EXT)) {
414 eh = mtod(m, struct ether_header *);
415 m->m_pkthdr.rcvif = ifp;
417 if (rx_frame.wi_status == WI_STAT_1042 ||
418 rx_frame.wi_status == WI_STAT_TUNNEL ||
419 rx_frame.wi_status == WI_STAT_WMP_MSG) {
420 if((rx_frame.wi_dat_len + WI_SNAPHDR_LEN) > MCLBYTES) {
421 printf("wi%d: oversized packet received "
422 "(wi_dat_len=%d, wi_status=0x%x)\n", sc->wi_unit,
423 rx_frame.wi_dat_len, rx_frame.wi_status);
428 m->m_pkthdr.len = m->m_len =
429 rx_frame.wi_dat_len + WI_SNAPHDR_LEN;
431 bcopy((char *)&rx_frame.wi_addr1,
432 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
433 bcopy((char *)&rx_frame.wi_addr2,
434 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
435 bcopy((char *)&rx_frame.wi_type,
436 (char *)&eh->ether_type, sizeof(u_int16_t));
438 if (wi_read_data(sc, id, WI_802_11_OFFSET,
439 mtod(m, caddr_t) + sizeof(struct ether_header),
446 if((rx_frame.wi_dat_len +
447 sizeof(struct ether_header)) > MCLBYTES) {
448 printf("wi%d: oversized packet received "
449 "(wi_dat_len=%d, wi_status=0x%x)\n", sc->wi_unit,
450 rx_frame.wi_dat_len, rx_frame.wi_status);
455 m->m_pkthdr.len = m->m_len =
456 rx_frame.wi_dat_len + sizeof(struct ether_header);
458 if (wi_read_data(sc, id, WI_802_3_OFFSET,
459 mtod(m, caddr_t), m->m_len + 2)) {
469 /* Handle BPF listeners. */
472 if (ifp->if_flags & IFF_PROMISC &&
473 (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
474 ETHER_ADDR_LEN) && (eh->ether_dhost[0] & 1) == 0)) {
481 /* Receive packet. */
482 m_adj(m, sizeof(struct ether_header));
483 ether_input(ifp, eh, m);
488 static void wi_txeof(sc, status)
494 ifp = &sc->arpcom.ac_if;
497 ifp->if_flags &= ~IFF_OACTIVE;
499 if (status & WI_EV_TX_EXC)
514 ifp = &sc->arpcom.ac_if;
516 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
518 /* Don't do this while we're transmitting */
519 if (ifp->if_flags & IFF_OACTIVE)
522 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS);
527 void wi_update_stats(sc)
530 struct wi_ltv_gen gen;
537 ifp = &sc->arpcom.ac_if;
539 id = CSR_READ_2(sc, WI_INFO_FID);
541 wi_read_data(sc, id, 0, (char *)&gen, 4);
543 if (gen.wi_type != WI_INFO_COUNTERS ||
544 gen.wi_len > (sizeof(sc->wi_stats) / 4) + 1)
547 ptr = (u_int32_t *)&sc->wi_stats;
549 for (i = 0; i < gen.wi_len - 1; i++) {
550 t = CSR_READ_2(sc, WI_DATA1);
551 #ifdef WI_HERMES_STATS_WAR
558 ifp->if_collisions = sc->wi_stats.wi_tx_single_retries +
559 sc->wi_stats.wi_tx_multi_retries +
560 sc->wi_stats.wi_tx_retry_limit;
572 sc = &wi_softc[unit];
573 ifp = &sc->arpcom.ac_if;
575 if (!(ifp->if_flags & IFF_UP)) {
576 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
577 CSR_WRITE_2(sc, WI_INT_EN, 0);
581 /* Disable interrupts. */
582 CSR_WRITE_2(sc, WI_INT_EN, 0);
584 status = CSR_READ_2(sc, WI_EVENT_STAT);
585 CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS);
587 if (status & WI_EV_RX) {
589 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
592 if (status & WI_EV_TX) {
593 wi_txeof(sc, status);
594 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
597 if (status & WI_EV_ALLOC) {
599 id = CSR_READ_2(sc, WI_ALLOC_FID);
600 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
601 if (id == sc->wi_tx_data_id)
602 wi_txeof(sc, status);
605 if (status & WI_EV_INFO) {
607 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
610 if (status & WI_EV_TX_EXC) {
611 wi_txeof(sc, status);
612 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
615 if (status & WI_EV_INFO_DROP) {
616 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP);
619 /* Re-enable interrupts. */
620 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
622 if (ifp->if_snd.ifq_head != NULL)
628 static int wi_cmd(sc, cmd, val)
635 CSR_WRITE_2(sc, WI_PARAM0, val);
636 CSR_WRITE_2(sc, WI_COMMAND, cmd);
638 for (i = 0; i < WI_TIMEOUT; i++) {
640 * Wait for 'command complete' bit to be
641 * set in the event status register.
643 s = CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD;
645 /* Ack the event and read result code. */
646 s = CSR_READ_2(sc, WI_STATUS);
647 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
649 if ((s & WI_CMD_CODE_MASK) != (cmd & WI_CMD_CODE_MASK))
652 if (s & WI_STAT_CMD_RESULT)
664 static void wi_reset(sc)
667 if (wi_cmd(sc, WI_CMD_INI, 0))
668 printf("wi%d: init failed\n", sc->wi_unit);
669 CSR_WRITE_2(sc, WI_INT_EN, 0);
670 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
672 /* Calibrate timer. */
673 WI_SETVAL(WI_RID_TICK_TIME, 8);
679 * Read an LTV record from the NIC.
681 static int wi_read_record(sc, ltv)
683 struct wi_ltv_gen *ltv;
688 /* Tell the NIC to enter record read mode. */
689 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type))
692 /* Seek to the record. */
693 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
697 * Read the length and record type and make sure they
698 * match what we expect (this verifies that we have enough
699 * room to hold all of the returned data).
701 len = CSR_READ_2(sc, WI_DATA1);
702 if (len > ltv->wi_len)
704 code = CSR_READ_2(sc, WI_DATA1);
705 if (code != ltv->wi_type)
711 /* Now read the data. */
713 for (i = 0; i < ltv->wi_len - 1; i++)
714 ptr[i] = CSR_READ_2(sc, WI_DATA1);
720 * Same as read, except we inject data instead of reading it.
722 static int wi_write_record(sc, ltv)
724 struct wi_ltv_gen *ltv;
729 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
732 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len);
733 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type);
736 for (i = 0; i < ltv->wi_len - 1; i++)
737 CSR_WRITE_2(sc, WI_DATA1, ptr[i]);
739 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type))
745 static int wi_seek(sc, id, off, chan)
762 printf("wi%d: invalid data path: %x\n", sc->wi_unit, chan);
766 CSR_WRITE_2(sc, selreg, id);
767 CSR_WRITE_2(sc, offreg, off);
769 for (i = 0; i < WI_TIMEOUT; i++) {
770 if (!(CSR_READ_2(sc, offreg) & (WI_OFF_BUSY|WI_OFF_ERR)))
780 static int wi_read_data(sc, id, off, buf, len)
789 if (wi_seek(sc, id, off, WI_BAP1))
792 ptr = (u_int16_t *)buf;
793 for (i = 0; i < len / 2; i++)
794 ptr[i] = CSR_READ_2(sc, WI_DATA1);
800 * According to the comments in the HCF Light code, there is a bug in
801 * the Hermes (or possibly in certain Hermes firmware revisions) where
802 * the chip's internal autoincrement counter gets thrown off during
803 * data writes: the autoincrement is missed, causing one data word to
804 * be overwritten and subsequent words to be written to the wrong memory
805 * locations. The end result is that we could end up transmitting bogus
806 * frames without realizing it. The workaround for this is to write a
807 * couple of extra guard words after the end of the transfer, then
808 * attempt to read then back. If we fail to locate the guard words where
809 * we expect them, we preform the transfer over again.
811 static int wi_write_data(sc, id, off, buf, len)
820 #ifdef WI_HERMES_AUTOINC_WAR
824 if (wi_seek(sc, id, off, WI_BAP0))
827 ptr = (u_int16_t *)buf;
828 for (i = 0; i < (len / 2); i++)
829 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
831 #ifdef WI_HERMES_AUTOINC_WAR
832 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
833 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
835 if (wi_seek(sc, id, off + len, WI_BAP0))
838 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
839 CSR_READ_2(sc, WI_DATA0) != 0x5678)
847 * Allocate a region of memory inside the NIC and zero
850 static int wi_alloc_nicmem(sc, len, id)
857 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len)) {
858 printf("wi%d: failed to allocate %d bytes on NIC\n",
863 for (i = 0; i < WI_TIMEOUT; i++) {
864 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
871 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
872 *id = CSR_READ_2(sc, WI_ALLOC_FID);
874 if (wi_seek(sc, *id, 0, WI_BAP0))
877 for (i = 0; i < len / 2; i++)
878 CSR_WRITE_2(sc, WI_DATA0, 0);
883 static void wi_setmulti(sc)
888 struct ifmultiaddr *ifma;
889 struct wi_ltv_mcast mcast;
891 ifp = &sc->arpcom.ac_if;
893 bzero((char *)&mcast, sizeof(mcast));
895 mcast.wi_type = WI_RID_MCAST;
896 mcast.wi_len = (3 * 16) + 1;
898 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
899 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
903 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
904 ifma = ifma->ifma_link.le_next) {
905 if (ifma->ifma_addr->sa_family != AF_LINK)
908 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
909 (char *)&mcast.wi_mcast[i], ETHER_ADDR_LEN);
912 bzero((char *)&mcast, sizeof(mcast));
917 mcast.wi_len = (i * 3) + 1;
918 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
923 static void wi_setdef(sc, wreq)
927 struct sockaddr_dl *sdl;
931 ifp = &sc->arpcom.ac_if;
933 switch(wreq->wi_type) {
934 case WI_RID_MAC_NODE:
935 ifa = ifnet_addrs[ifp->if_index - 1];
936 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
937 bcopy((char *)&wreq->wi_val, (char *)&sc->arpcom.ac_enaddr,
939 bcopy((char *)&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN);
941 case WI_RID_PORTTYPE:
942 sc->wi_ptype = wreq->wi_val[0];
945 sc->wi_tx_rate = wreq->wi_val[0];
947 case WI_RID_MAX_DATALEN:
948 sc->wi_max_data_len = wreq->wi_val[0];
950 case WI_RID_RTS_THRESH:
951 sc->wi_rts_thresh = wreq->wi_val[0];
953 case WI_RID_SYSTEM_SCALE:
954 sc->wi_ap_density = wreq->wi_val[0];
956 case WI_RID_CREATE_IBSS:
957 sc->wi_create_ibss = wreq->wi_val[0];
959 case WI_RID_OWN_CHNL:
960 sc->wi_channel = wreq->wi_val[0];
962 case WI_RID_NODENAME:
963 bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
964 bcopy((char *)&wreq->wi_val[1], sc->wi_node_name, 30);
966 case WI_RID_DESIRED_SSID:
967 bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
968 bcopy((char *)&wreq->wi_val[1], sc->wi_net_name, 30);
970 case WI_RID_OWN_SSID:
971 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
972 bcopy((char *)&wreq->wi_val[1], sc->wi_ibss_name, 30);
974 case WI_RID_PM_ENABLED:
975 sc->wi_pm_enabled = wreq->wi_val[0];
977 case WI_RID_MAX_SLEEP:
978 sc->wi_max_sleep = wreq->wi_val[0];
984 /* Reinitialize WaveLAN. */
990 static int wi_ioctl(ifp, command, data)
1003 ifr = (struct ifreq *)data;
1012 error = ether_ioctl(ifp, command, data);
1015 if (ifp->if_flags & IFF_UP) {
1016 if (ifp->if_flags & IFF_RUNNING &&
1017 ifp->if_flags & IFF_PROMISC &&
1018 !(sc->wi_if_flags & IFF_PROMISC)) {
1019 WI_SETVAL(WI_RID_PROMISC, 1);
1020 } else if (ifp->if_flags & IFF_RUNNING &&
1021 !(ifp->if_flags & IFF_PROMISC) &&
1022 sc->wi_if_flags & IFF_PROMISC) {
1023 WI_SETVAL(WI_RID_PROMISC, 0);
1027 if (ifp->if_flags & IFF_RUNNING) {
1031 sc->wi_if_flags = ifp->if_flags;
1040 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1043 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1044 bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val,
1045 sizeof(sc->wi_stats));
1046 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1048 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq)) {
1053 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1056 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1059 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1062 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1063 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1066 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1068 wi_setdef(sc, &wreq);
1081 static void wi_init(xsc)
1084 struct wi_softc *sc = xsc;
1085 struct ifnet *ifp = &sc->arpcom.ac_if;
1087 struct wi_ltv_macaddr mac;
1095 if (ifp->if_flags & IFF_RUNNING)
1100 /* Program max data length. */
1101 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
1103 /* Enable/disable IBSS creation. */
1104 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
1106 /* Set the port type. */
1107 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
1109 /* Program the RTS/CTS threshold. */
1110 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
1112 /* Program the TX rate */
1113 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
1115 /* Access point density */
1116 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
1118 /* Power Management Enabled */
1119 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
1121 /* Power Managment Max Sleep */
1122 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
1124 /* Specify the IBSS name */
1125 WI_SETSTR(WI_RID_OWN_SSID, sc->wi_ibss_name);
1127 /* Specify the network name */
1128 WI_SETSTR(WI_RID_DESIRED_SSID, sc->wi_net_name);
1130 /* Specify the frequency to use */
1131 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
1133 /* Program the nodename. */
1134 WI_SETSTR(WI_RID_NODENAME, sc->wi_node_name);
1136 /* Set our MAC address. */
1138 mac.wi_type = WI_RID_MAC_NODE;
1139 bcopy((char *)&sc->arpcom.ac_enaddr,
1140 (char *)&mac.wi_mac_addr, ETHER_ADDR_LEN);
1141 wi_write_record(sc, (struct wi_ltv_gen *)&mac);
1143 /* Initialize promisc mode. */
1144 if (ifp->if_flags & IFF_PROMISC) {
1145 WI_SETVAL(WI_RID_PROMISC, 1);
1147 WI_SETVAL(WI_RID_PROMISC, 0);
1150 /* Set multicast filter. */
1153 /* Enable desired port */
1154 wi_cmd(sc, WI_CMD_ENABLE|sc->wi_portnum, 0);
1156 if (wi_alloc_nicmem(sc, 1518 + sizeof(struct wi_frame) + 8, &id))
1157 printf("wi%d: tx buffer allocation failed\n", sc->wi_unit);
1158 sc->wi_tx_data_id = id;
1160 if (wi_alloc_nicmem(sc, 1518 + sizeof(struct wi_frame) + 8, &id))
1161 printf("wi%d: mgmt. buffer allocation failed\n", sc->wi_unit);
1162 sc->wi_tx_mgmt_id = id;
1164 /* enable interrupts */
1165 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
1169 ifp->if_flags |= IFF_RUNNING;
1170 ifp->if_flags &= ~IFF_OACTIVE;
1172 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
1177 static void wi_start(ifp)
1180 struct wi_softc *sc;
1182 struct wi_frame tx_frame;
1183 struct ether_header *eh;
1191 if (ifp->if_flags & IFF_OACTIVE)
1194 IF_DEQUEUE(&ifp->if_snd, m0);
1198 bzero((char *)&tx_frame, sizeof(tx_frame));
1199 id = sc->wi_tx_data_id;
1200 eh = mtod(m0, struct ether_header *);
1203 * Use RFC1042 encoding for IP and ARP datagrams,
1204 * 802.3 for anything else.
1206 if (ntohs(eh->ether_type) == ETHERTYPE_IP ||
1207 ntohs(eh->ether_type) == ETHERTYPE_ARP ||
1208 ntohs(eh->ether_type) == ETHERTYPE_REVARP) {
1209 bcopy((char *)&eh->ether_dhost,
1210 (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN);
1211 bcopy((char *)&eh->ether_shost,
1212 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
1213 bcopy((char *)&eh->ether_dhost,
1214 (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN);
1215 bcopy((char *)&eh->ether_shost,
1216 (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN);
1218 tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN;
1219 tx_frame.wi_frame_ctl = WI_FTYPE_DATA;
1220 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
1221 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
1222 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1223 tx_frame.wi_type = eh->ether_type;
1225 m_copydata(m0, sizeof(struct ether_header),
1226 m0->m_pkthdr.len - sizeof(struct ether_header),
1227 (caddr_t)&sc->wi_txbuf);
1229 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1230 sizeof(struct wi_frame));
1231 wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf,
1232 (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2);
1234 tx_frame.wi_dat_len = m0->m_pkthdr.len;
1236 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf);
1238 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1239 sizeof(struct wi_frame));
1240 wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf,
1241 m0->m_pkthdr.len + 2);
1246 * If there's a BPF listner, bounce a copy of
1247 * this frame to him.
1255 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id))
1256 printf("wi%d: xmit failed\n", sc->wi_unit);
1258 ifp->if_flags |= IFF_OACTIVE;
1261 * Set a timeout in case the chip goes out to lunch.
1268 static int wi_mgmt_xmit(sc, data, len)
1269 struct wi_softc *sc;
1273 struct wi_frame tx_frame;
1275 struct wi_80211_hdr *hdr;
1281 hdr = (struct wi_80211_hdr *)data;
1282 dptr = data + sizeof(struct wi_80211_hdr);
1284 bzero((char *)&tx_frame, sizeof(tx_frame));
1285 id = sc->wi_tx_mgmt_id;
1287 bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl,
1288 sizeof(struct wi_80211_hdr));
1290 tx_frame.wi_dat_len = len - WI_SNAPHDR_LEN;
1291 tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN);
1293 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
1294 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
1295 (len - sizeof(struct wi_80211_hdr)) + 2);
1297 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) {
1298 printf("wi%d: xmit failed\n", sc->wi_unit);
1305 static void wi_stop(sc)
1306 struct wi_softc *sc;
1313 ifp = &sc->arpcom.ac_if;
1315 CSR_WRITE_2(sc, WI_INT_EN, 0);
1316 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0);
1318 untimeout(wi_inquire, sc, sc->wi_stat_ch);
1320 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1325 static void wi_watchdog(ifp)
1328 struct wi_softc *sc;
1332 printf("wi%d: device timeout\n", sc->wi_unit);
1341 static void wi_shutdown(arg, howto)
1345 struct wi_softc *sc;