3 * Bill Paul <wpaul@windriver.com>. 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 * $FreeBSD: src/sys/dev/if_ndis/if_ndis.c,v 1.65 2004/07/07 17:46:30 wpaul Exp $
33 * $DragonFly: src/sys/dev/netif/ndis/if_ndis.c,v 1.7 2005/08/11 03:22:57 corecode Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
45 #if __FreeBSD_version < 502113
46 #include <sys/sysctl.h>
50 #include <net/ifq_var.h>
51 #include <net/if_arp.h>
52 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/route.h>
59 #include <machine/bus_memio.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
66 #include <netproto/802_11/ieee80211_var.h>
67 #include <netproto/802_11/ieee80211_ioctl.h>
69 #include <netproto/802_11/if_wavelan_ieee.h>
71 #include <bus/pci/pcireg.h>
72 #include <bus/pci/pcivar.h>
74 #include <sys/thread2.h>
76 #include <emulation/ndis/regcall.h>
77 #include <emulation/ndis/pe_var.h>
78 #include <emulation/ndis/resource_var.h>
79 #include <emulation/ndis/ntoskrnl_var.h>
80 #include <emulation/ndis/hal_var.h>
81 #include <emulation/ndis/ndis_var.h>
82 #include <emulation/ndis/cfg_var.h>
83 #include "if_ndisvar.h"
88 #include "ndis_driver_data.h"
90 int ndis_attach (device_t);
91 int ndis_detach (device_t);
92 int ndis_suspend (device_t);
93 int ndis_resume (device_t);
94 void ndis_shutdown (device_t);
96 static __stdcall void ndis_txeof (ndis_handle,
97 ndis_packet *, ndis_status);
98 static __stdcall void ndis_rxeof (ndis_handle,
99 ndis_packet **, uint32_t);
100 static __stdcall void ndis_linksts (ndis_handle,
101 ndis_status, void *, uint32_t);
102 static __stdcall void ndis_linksts_done (ndis_handle);
104 static void ndis_intr (void *);
105 static void ndis_intrtask (void *);
106 static void ndis_tick (void *);
107 static void ndis_ticktask (void *);
108 static void ndis_start (struct ifnet *);
109 static void ndis_starttask (void *);
110 static int ndis_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
111 static int ndis_wi_ioctl_get (struct ifnet *, u_long, caddr_t);
112 static int ndis_wi_ioctl_set (struct ifnet *, u_long, caddr_t);
113 static void ndis_init (void *);
114 static void ndis_stop (struct ndis_softc *);
115 static void ndis_watchdog (struct ifnet *);
116 static int ndis_ifmedia_upd (struct ifnet *);
117 static void ndis_ifmedia_sts (struct ifnet *, struct ifmediareq *);
118 static int ndis_get_assoc (struct ndis_softc *, ndis_wlan_bssid_ex **);
119 static int ndis_probe_offload (struct ndis_softc *);
120 static int ndis_set_offload (struct ndis_softc *);
121 static void ndis_getstate_80211 (struct ndis_softc *);
122 static void ndis_setstate_80211 (struct ndis_softc *);
123 static void ndis_media_status (struct ifnet *, struct ifmediareq *);
125 static void ndis_setmulti (struct ndis_softc *);
126 static void ndis_map_sclist (void *, bus_dma_segment_t *,
127 int, bus_size_t, int);
130 * Program the 64-bit multicast hash filter.
134 struct ndis_softc *sc;
137 struct ifmultiaddr *ifma;
138 int len, mclistsz, error;
141 ifp = &sc->arpcom.ac_if;
143 if (!NDIS_INITIALIZED(sc))
146 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
147 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
148 len = sizeof(sc->ndis_filter);
149 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
150 &sc->ndis_filter, &len);
152 device_printf (sc->ndis_dev,
153 "set filter failed: %d\n", error);
157 if (LIST_EMPTY(&ifp->if_multiaddrs))
160 len = sizeof(mclistsz);
161 ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
163 mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
165 if (mclist == NULL) {
166 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
170 sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
173 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
174 if (ifma->ifma_addr->sa_family != AF_LINK)
176 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
177 mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
179 if (len > mclistsz) {
180 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
181 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
186 len = len * ETHER_ADDR_LEN;
187 error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
189 device_printf (sc->ndis_dev, "set mclist failed: %d\n", error);
190 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
191 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
195 free(mclist, M_TEMP);
197 len = sizeof(sc->ndis_filter);
198 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
199 &sc->ndis_filter, &len);
201 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
208 struct ndis_softc *sc;
210 ndis_task_offload *nto;
211 ndis_task_offload_hdr *ntoh;
212 ndis_task_tcpip_csum *nttc;
216 ifp = &sc->arpcom.ac_if;
218 if (!NDIS_INITIALIZED(sc))
221 /* See if there's anything to set. */
223 error = ndis_probe_offload(sc);
227 if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
230 len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
231 sizeof(ndis_task_tcpip_csum);
233 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
238 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
239 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
240 ntoh->ntoh_offset_firsttask = sizeof(ndis_task_offload_hdr);
241 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
242 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
243 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
245 nto = (ndis_task_offload *)((char *)ntoh +
246 ntoh->ntoh_offset_firsttask);
248 nto->nto_vers = NDIS_TASK_OFFLOAD_VERSION;
249 nto->nto_len = sizeof(ndis_task_offload);
250 nto->nto_task = NDIS_TASK_TCPIP_CSUM;
251 nto->nto_offset_nexttask = 0;
252 nto->nto_taskbuflen = sizeof(ndis_task_tcpip_csum);
254 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
256 if (ifp->if_capenable & IFCAP_TXCSUM)
257 nttc->nttc_v4tx = sc->ndis_v4tx;
259 if (ifp->if_capenable & IFCAP_RXCSUM)
260 nttc->nttc_v4rx = sc->ndis_v4rx;
262 error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
269 ndis_probe_offload(sc)
270 struct ndis_softc *sc;
272 ndis_task_offload *nto;
273 ndis_task_offload_hdr *ntoh;
274 ndis_task_tcpip_csum *nttc = NULL;
276 int len, error, dummy;
278 ifp = &sc->arpcom.ac_if;
281 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
286 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
291 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
292 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
293 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
294 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
295 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
297 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
304 if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
309 nto = (ndis_task_offload *)((char *)ntoh +
310 ntoh->ntoh_offset_firsttask);
313 switch (nto->nto_task) {
314 case NDIS_TASK_TCPIP_CSUM:
315 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
317 /* Don't handle these yet. */
318 case NDIS_TASK_IPSEC:
319 case NDIS_TASK_TCP_LARGESEND:
323 if (nto->nto_offset_nexttask == 0)
325 nto = (ndis_task_offload *)((char *)nto +
326 nto->nto_offset_nexttask);
334 sc->ndis_v4tx = nttc->nttc_v4tx;
335 sc->ndis_v4rx = nttc->nttc_v4rx;
337 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_IP_CSUM)
338 sc->ndis_hwassist |= CSUM_IP;
339 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
340 sc->ndis_hwassist |= CSUM_TCP;
341 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
342 sc->ndis_hwassist |= CSUM_UDP;
344 if (sc->ndis_hwassist)
345 ifp->if_capabilities |= IFCAP_TXCSUM;
347 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_IP_CSUM)
348 ifp->if_capabilities |= IFCAP_RXCSUM;
349 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
350 ifp->if_capabilities |= IFCAP_RXCSUM;
351 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
352 ifp->if_capabilities |= IFCAP_RXCSUM;
359 * Attach the interface. Allocate softc structures, do ifmedia
360 * setup and ethernet/BPF attach.
366 u_char eaddr[ETHER_ADDR_LEN];
367 struct ndis_softc *sc;
368 struct ifnet *ifp = NULL;
373 sc = device_get_softc(dev);
375 callout_init(&sc->ndis_stat_timer);
376 NDIS_LOCK_INIT(&sc->ndis_lock);
377 NDIS_LOCK_INIT(&sc->ndis_intrlock);
380 * Hook interrupt early, since calling the driver's
381 * init routine may trigger an interrupt.
384 error = bus_setup_intr(dev, sc->ndis_irq, INTR_TYPE_NET | INTR_MPSAFE,
386 &sc->ndis_intrhand, NULL);
389 device_printf(dev, "couldn't set up irq\n");
393 sc->ndis_regvals = ndis_regvals;
395 #if __FreeBSD_version < 502113
396 sysctl_ctx_init(&sc->ndis_ctx);
399 /* Create sysctl registry nodes */
400 ndis_create_sysctls(sc);
402 /* Set up driver image in memory. */
404 ndis_load_driver((vm_offset_t)img, sc);
406 /* Tell the user what version of the API the driver is using. */
407 device_printf(dev, "NDIS API version: %d.%d\n",
408 sc->ndis_chars.nmc_version_major,
409 sc->ndis_chars.nmc_version_minor);
411 /* Do resource conversion. */
412 ndis_convert_res(sc);
414 /* Install our RX and TX interrupt handlers. */
415 sc->ndis_block.nmb_senddone_func = ndis_txeof;
416 sc->ndis_block.nmb_pktind_func = ndis_rxeof;
418 /* Call driver's init routine. */
419 if (ndis_init_nic(sc)) {
420 device_printf (dev, "init handler failed\n");
426 * Get station address from the driver.
429 ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
431 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
434 * Figure out of we're allowed to use multipacket sends
435 * with this driver, and if so, how many.
438 if (sc->ndis_chars.nmc_sendsingle_func &&
439 sc->ndis_chars.nmc_sendmulti_func == NULL) {
440 sc->ndis_maxpkts = 1;
442 len = sizeof(sc->ndis_maxpkts);
443 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
444 &sc->ndis_maxpkts, &len);
447 sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
448 sc->ndis_maxpkts, M_DEVBUF, M_WAITOK|M_ZERO);
450 sc->ndis_txpending = sc->ndis_maxpkts;
453 /* Get supported oid list. */
454 ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
456 /* If the NDIS module requested scatter/gather, init maps. */
461 * See if the OID_802_11_CONFIGURATION OID is
462 * supported by this driver. If it is, then this an 802.11
463 * wireless driver, and we should set up media for wireless.
465 for (i = 0; i < sc->ndis_oidcnt; i++) {
466 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
472 /* Check for task offload support. */
473 ndis_probe_offload(sc);
475 ifp = &sc->arpcom.ac_if;
477 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
478 ifp->if_mtu = ETHERMTU;
479 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
480 ifp->if_ioctl = ndis_ioctl;
481 ifp->if_start = ndis_start;
482 ifp->if_watchdog = ndis_watchdog;
483 ifp->if_init = ndis_init;
484 ifp->if_baudrate = 10000000;
485 ifq_set_maxlen(&ifp->if_snd, 50);
486 ifq_set_ready(&ifp->if_snd);
487 ifp->if_capenable = ifp->if_capabilities;
488 ifp->if_hwassist = sc->ndis_hwassist;
491 if (sc->ndis_80211) {
492 struct ieee80211com *ic = (void *)ifp;
493 ndis_80211_rates_ex rates;
494 struct ndis_80211_nettype_list *ntl;
498 ic->ic_phytype = IEEE80211_T_DS;
499 ic->ic_opmode = IEEE80211_M_STA;
500 ic->ic_caps = IEEE80211_C_IBSS;
501 ic->ic_state = IEEE80211_S_ASSOC;
502 ic->ic_modecaps = (1<<IEEE80211_MODE_AUTO);
504 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
508 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
509 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
516 for (i = 0; i < ntl->ntl_items; i++) {
517 switch (ntl->ntl_type[i]) {
518 case NDIS_80211_NETTYPE_11FH:
519 case NDIS_80211_NETTYPE_11DS:
520 ic->ic_modecaps |= (1<<IEEE80211_MODE_11B);
522 case NDIS_80211_NETTYPE_11OFDM5:
523 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
525 case NDIS_80211_NETTYPE_11OFDM24:
526 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
535 bzero((char *)&rates, len);
536 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
537 (void *)rates, &len);
539 device_printf (dev, "get rates failed: 0x%x\n", r);
541 * Since the supported rates only up to 8 can be supported,
542 * if this is not 802.11b we're just going to be faking it
546 #define TESTSETRATE(x, y) \
549 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) { \
550 if (ic->ic_sup_rates[x].rs_rates[i] == (y)) \
553 if (i == ic->ic_sup_rates[x].rs_nrates) { \
554 ic->ic_sup_rates[x].rs_rates[i] = (y); \
555 ic->ic_sup_rates[x].rs_nrates++; \
559 #define SETRATE(x, y) \
560 ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
562 ic->ic_sup_rates[x].rs_nrates++
564 ic->ic_curmode = IEEE80211_MODE_AUTO;
565 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A))
566 ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
567 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B))
568 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
569 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G))
570 ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
571 for (i = 0; i < len; i++) {
572 switch (rates[i] & IEEE80211_RATE_VAL) {
578 if (!(ic->ic_modecaps &
579 (1<<IEEE80211_MODE_11B))) {
580 /* Lazy-init 802.11b. */
582 (1<<IEEE80211_MODE_11B);
583 ic->ic_sup_rates[IEEE80211_MODE_11B].
586 SETRATE(IEEE80211_MODE_11B, rates[i]);
587 INCRATE(IEEE80211_MODE_11B);
590 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
591 SETRATE(IEEE80211_MODE_11A, rates[i]);
592 INCRATE(IEEE80211_MODE_11A);
594 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
595 SETRATE(IEEE80211_MODE_11G, rates[i]);
596 INCRATE(IEEE80211_MODE_11G);
603 * If the hardware supports 802.11g, it most
604 * likely supports 802.11b and all of the
605 * 802.11b and 802.11g speeds, so maybe we can
606 * just cheat here. Just how in the heck do
607 * we detect turbo modes, though?
609 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
610 TESTSETRATE(IEEE80211_MODE_11B,
611 IEEE80211_RATE_BASIC|2);
612 TESTSETRATE(IEEE80211_MODE_11B,
613 IEEE80211_RATE_BASIC|4);
614 TESTSETRATE(IEEE80211_MODE_11B,
615 IEEE80211_RATE_BASIC|11);
616 TESTSETRATE(IEEE80211_MODE_11B,
617 IEEE80211_RATE_BASIC|22);
619 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
620 TESTSETRATE(IEEE80211_MODE_11G, 47);
621 TESTSETRATE(IEEE80211_MODE_11G, 72);
622 TESTSETRATE(IEEE80211_MODE_11G, 96);
623 TESTSETRATE(IEEE80211_MODE_11G, 108);
625 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
626 TESTSETRATE(IEEE80211_MODE_11A, 47);
627 TESTSETRATE(IEEE80211_MODE_11A, 72);
628 TESTSETRATE(IEEE80211_MODE_11A, 96);
629 TESTSETRATE(IEEE80211_MODE_11A, 108);
634 * Taking yet more guesses here.
636 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
639 if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
640 chanflag |= IEEE80211_CHAN_G;
642 chanflag |= IEEE80211_CHAN_B;
643 if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
645 chanflag = IEEE80211_CHAN_A;
648 ic->ic_channels[i].ic_freq =
649 ieee80211_ieee2mhz(i, chanflag);
650 ic->ic_channels[i].ic_flags = chanflag;
654 r = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &i);
655 if (arg != NDIS_80211_WEPSTAT_NOTSUPPORTED)
656 ic->ic_caps |= IEEE80211_C_WEP;
658 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
660 ic->ic_caps |= IEEE80211_C_PMGT;
661 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
662 ieee80211_ifattach(ifp);
663 ieee80211_media_init(ifp, ieee80211_media_change,
665 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
666 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
668 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
670 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
671 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
672 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
673 ifmedia_add(&sc->ifmedia,
674 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
675 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
676 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
677 ether_ifattach(ifp, eaddr);
680 /* Override the status handler so we can detect link changes. */
681 sc->ndis_block.nmb_status_func = ndis_linksts;
682 sc->ndis_block.nmb_statusdone_func = ndis_linksts_done;
687 /* We're done talking to the NIC for now; halt it. */
694 * Shutdown hardware and free up resources. This can be called any
695 * time after the mutex has been initialized. It is called in both
696 * the error case in attach and the normal detach case so it needs
697 * to be careful about only freeing resources that have actually been
704 struct ndis_softc *sc;
708 sc = device_get_softc(dev);
710 ifp = &sc->arpcom.ac_if;
711 ifp->if_flags &= ~IFF_UP;
713 if (device_is_attached(dev)) {
717 ieee80211_ifdetach(ifp);
723 bus_generic_detach(dev);
725 if (sc->ndis_intrhand)
726 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
728 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
730 bus_release_resource(dev, SYS_RES_IOPORT,
731 sc->ndis_io_rid, sc->ndis_res_io);
732 if (sc->ndis_res_mem)
733 bus_release_resource(dev, SYS_RES_MEMORY,
734 sc->ndis_mem_rid, sc->ndis_res_mem);
735 if (sc->ndis_res_altmem)
736 bus_release_resource(dev, SYS_RES_MEMORY,
737 sc->ndis_altmem_rid, sc->ndis_res_altmem);
740 ndis_destroy_dma(sc);
742 ndis_unload_driver((void *)ifp);
744 if (sc->ndis_iftype == PCIBus)
745 bus_dma_tag_destroy(sc->ndis_parent_tag);
747 #if __FreeBSD_version < 502113
748 sysctl_ctx_free(&sc->ndis_ctx);
751 NDIS_LOCK_DESTROY(&sc->ndis_lock);
752 NDIS_LOCK_DESTROY(&sc->ndis_intrlock);
761 struct ndis_softc *sc;
764 sc = device_get_softc(dev);
765 ifp = &sc->arpcom.ac_if;
768 if (NDIS_INITIALIZED(sc))
779 struct ndis_softc *sc;
782 sc = device_get_softc(dev);
783 ifp = &sc->arpcom.ac_if;
785 if (NDIS_INITIALIZED(sc))
792 * A frame has been uploaded: pass the resulting mbuf chain up to
793 * the higher level protocols.
795 * When handling received NDIS packets, the 'status' field in the
796 * out-of-band portion of the ndis_packet has special meaning. In the
797 * most common case, the underlying NDIS driver will set this field
798 * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
799 * take posession of it. We then change the status field to
800 * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
801 * and that we will return it at some point in the future via the
802 * return packet handler.
804 * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
805 * this means the driver is running out of packet/buffer resources and
806 * wants to maintain ownership of the packet. In this case, we have to
807 * copy the packet data into local storage and let the driver keep the
810 __stdcall static void
811 ndis_rxeof(adapter, packets, pktcnt)
813 ndis_packet **packets;
816 struct ndis_softc *sc;
817 ndis_miniport_block *block;
820 ndis_tcpip_csum *csum;
825 block = (ndis_miniport_block *)adapter;
826 sc = (struct ndis_softc *)(block->nmb_ifp);
827 ifp = block->nmb_ifp;
829 for (i = 0; i < pktcnt; i++) {
831 /* Stash the softc here so ptom can use it. */
833 if (ndis_ptom(&m0, p)) {
834 device_printf (sc->ndis_dev, "ptom failed\n");
835 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
836 ndis_return_packet(sc, p);
838 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
839 m = m_dup(m0, MB_DONTWAIT);
841 * NOTE: we want to destroy the mbuf here, but
842 * we don't actually want to return it to the
843 * driver via the return packet handler. By
844 * bumping np_refcnt, we can prevent the
845 * ndis_return_packet() routine from actually
855 p->np_oob.npo_status = NDIS_STATUS_PENDING;
856 m0->m_pkthdr.rcvif = ifp;
859 /* Deal with checksum offload. */
861 if (ifp->if_capenable & IFCAP_RXCSUM &&
862 p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
864 p->np_ext.npe_info[ndis_tcpipcsum_info];
865 csum = (ndis_tcpip_csum *)&s;
866 if (csum->u.ntc_rxflags &
867 NDIS_RXCSUM_IP_PASSED)
868 m0->m_pkthdr.csum_flags |=
869 CSUM_IP_CHECKED|CSUM_IP_VALID;
870 if (csum->u.ntc_rxflags &
871 (NDIS_RXCSUM_TCP_PASSED |
872 NDIS_RXCSUM_UDP_PASSED)) {
873 m0->m_pkthdr.csum_flags |=
874 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
875 m0->m_pkthdr.csum_data = 0xFFFF;
879 (*ifp->if_input)(ifp, m0);
887 * A frame was downloaded to the chip. It's safe for us to clean up
890 __stdcall static void
891 ndis_txeof(adapter, packet, status)
897 struct ndis_softc *sc;
898 ndis_miniport_block *block;
904 block = (ndis_miniport_block *)adapter;
905 sc = (struct ndis_softc *)block->nmb_ifp;
906 ifp = block->nmb_ifp;
909 idx = packet->np_txidx;
911 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
913 ndis_free_packet(packet);
917 sc->ndis_txarray[idx] = NULL;
918 sc->ndis_txpending++;
920 if (status == NDIS_STATUS_SUCCESS)
925 ifp->if_flags &= ~IFF_OACTIVE;
928 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
933 __stdcall static void
934 ndis_linksts(adapter, status, sbuf, slen)
940 ndis_miniport_block *block;
943 block->nmb_getstat = status;
948 __stdcall static void
949 ndis_linksts_done(adapter)
952 ndis_miniport_block *block;
953 struct ndis_softc *sc;
957 ifp = block->nmb_ifp;
960 if (!NDIS_INITIALIZED(sc))
963 switch (block->nmb_getstat) {
964 case NDIS_STATUS_MEDIA_CONNECT:
965 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
966 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
968 case NDIS_STATUS_MEDIA_DISCONNECT:
970 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
983 struct ndis_softc *sc;
989 ifp = &sc->arpcom.ac_if;
991 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
993 FASTCALL1(hal_lower_irql, irql);
995 ndis_enable_intr(sc);
1005 struct ndis_softc *sc;
1007 int is_our_intr = 0;
1012 ifp = &sc->arpcom.ac_if;
1014 if (sc->ndis_block.nmb_miniportadapterctx == NULL)
1018 if (sc->ndis_block.nmb_interrupt->ni_isrreq == TRUE)
1019 ndis_isr(sc, &is_our_intr, &call_isr);
1021 ndis_disable_intr(sc);
1024 NDIS_INTRUNLOCK(sc);
1026 if ((is_our_intr || call_isr))
1027 ndis_sched(ndis_intrtask, ifp, NDIS_SWI);
1036 struct ndis_softc *sc;
1040 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1041 callout_reset(&sc->ndis_stat_timer,
1042 hz *sc->ndis_block.nmb_checkforhangsecs, ndis_tick, sc);
1049 struct ndis_softc *sc;
1050 __stdcall ndis_checkforhang_handler hangfunc;
1052 ndis_media_state linkstate;
1058 hangfunc = sc->ndis_chars.nmc_checkhang_func;
1060 if (hangfunc != NULL) {
1061 rval = hangfunc(sc->ndis_block.nmb_miniportadapterctx);
1068 len = sizeof(linkstate);
1069 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1070 (void *)&linkstate, &len);
1074 if (sc->ndis_link == 0 && linkstate == nmc_connected) {
1075 device_printf(sc->ndis_dev, "link up\n");
1079 ndis_getstate_80211(sc);
1081 #ifdef LINK_STATE_UP
1082 sc->arpcom.ac_if.if_link_state = LINK_STATE_UP;
1083 rt_ifmsg(&(sc->arpcom.ac_if));
1084 #endif /* LINK_STATE_UP */
1087 if (sc->ndis_link == 1 && linkstate == nmc_disconnected) {
1088 device_printf(sc->ndis_dev, "link down\n");
1090 #ifdef LINK_STATE_DOWN
1091 sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN;
1092 rt_ifmsg(&(sc->arpcom.ac_if));
1093 #endif /* LINK_STATE_DOWN */
1102 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1104 bus_dma_segment_t *segs;
1110 struct ndis_sc_list *sclist;
1113 if (error || arg == NULL)
1118 sclist->nsl_frags = nseg;
1120 for (i = 0; i < nseg; i++) {
1121 sclist->nsl_elements[i].nse_addr.np_quad = segs[i].ds_addr;
1122 sclist->nsl_elements[i].nse_len = segs[i].ds_len;
1135 if (!ifq_is_empty(&ifp->if_snd))
1141 * Main transmit routine. To make NDIS drivers happy, we need to
1142 * transform mbuf chains into NDIS packets and feed them to the
1143 * send packet routines. Most drivers allow you to send several
1144 * packets at once (up to the maxpkts limit). Unfortunately, rather
1145 * that accepting them in the form of a linked list, they expect
1146 * a contiguous array of pointers to packets.
1148 * For those drivers which use the NDIS scatter/gather DMA mechanism,
1149 * we need to perform busdma work here. Those that use map registers
1150 * will do the mapping themselves on a buffer by buffer basis.
1157 struct ndis_softc *sc;
1158 struct mbuf *m = NULL;
1159 ndis_packet **p0 = NULL, *p = NULL;
1160 ndis_tcpip_csum *csum;
1168 if (!sc->ndis_link || ifp->if_flags & IFF_OACTIVE) {
1173 p0 = &sc->ndis_txarray[sc->ndis_txidx];
1175 while(sc->ndis_txpending) {
1176 m = ifq_poll(&ifp->if_snd);
1180 sc->ndis_txarray[sc->ndis_txidx] = NULL;
1182 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1186 m = ifq_dequeue(&ifp->if_snd);
1189 * Save pointer to original mbuf
1190 * so we can free it later.
1193 p = sc->ndis_txarray[sc->ndis_txidx];
1194 p->np_txidx = sc->ndis_txidx;
1196 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1199 * Do scatter/gather processing, if driver requested it.
1202 bus_dmamap_load_mbuf(sc->ndis_ttag,
1203 sc->ndis_tmaps[sc->ndis_txidx], m,
1204 ndis_map_sclist, &p->np_sclist, BUS_DMA_NOWAIT);
1205 bus_dmamap_sync(sc->ndis_ttag,
1206 sc->ndis_tmaps[sc->ndis_txidx],
1207 BUS_DMASYNC_PREREAD);
1208 p->np_ext.npe_info[ndis_sclist_info] = &p->np_sclist;
1211 /* Handle checksum offload. */
1213 if (ifp->if_capenable & IFCAP_TXCSUM &&
1214 m->m_pkthdr.csum_flags) {
1215 csum = (ndis_tcpip_csum *)
1216 &p->np_ext.npe_info[ndis_tcpipcsum_info];
1217 csum->u.ntc_txflags = NDIS_TXCSUM_DO_IPV4;
1218 if (m->m_pkthdr.csum_flags & CSUM_IP)
1219 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_IP;
1220 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1221 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_TCP;
1222 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1223 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_UDP;
1224 p->np_private.npp_flags = NDIS_PROTOCOL_ID_TCP_IP;
1228 sc->ndis_txpending--;
1233 * If there's a BPF listener, bounce a copy of this frame
1240 * The array that p0 points to must appear contiguous,
1241 * so we must not wrap past the end of sc->ndis_txarray[].
1242 * If it looks like we're about to wrap, break out here
1243 * so the this batch of packets can be transmitted, then
1244 * wait for txeof to ask us to send the rest.
1247 if (sc->ndis_txidx == 0)
1251 if (sc->ndis_txpending == 0)
1252 ifp->if_flags |= IFF_OACTIVE;
1255 * Set a timeout in case the chip goes out to lunch.
1261 if (sc->ndis_maxpkts == 1)
1262 ndis_send_packet(sc, p);
1264 ndis_send_packets(sc, p0, pcnt);
1273 struct ndis_softc *sc = xsc;
1274 struct ifnet *ifp = &sc->arpcom.ac_if;
1279 * Avoid reintializing the link unnecessarily.
1280 * This should be dealt with in a better way by
1281 * fixing the upper layer modules so they don't
1282 * call ifp->if_init() quite as often.
1284 if (sc->ndis_link && sc->ndis_skip)
1288 * Cancel pending I/O and free all RX/TX buffers.
1291 if (ndis_init_nic(sc))
1294 /* Init our MAC address */
1296 /* Program the packet filter */
1298 sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1300 if (ifp->if_flags & IFF_BROADCAST)
1301 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1303 if (ifp->if_flags & IFF_PROMISC)
1304 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1306 i = sizeof(sc->ndis_filter);
1308 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1309 &sc->ndis_filter, &i);
1312 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1315 * Program the multicast filter, if necessary.
1319 /* Setup task offload. */
1320 ndis_set_offload(sc);
1322 /* Enable interrupts. */
1323 ndis_enable_intr(sc);
1326 ndis_setstate_80211(sc);
1331 sc->ndis_txpending = sc->ndis_maxpkts;
1334 ifp->if_flags |= IFF_RUNNING;
1335 ifp->if_flags &= ~IFF_OACTIVE;
1340 * Some drivers don't set this value. The NDIS spec says
1341 * the default checkforhang timeout is "approximately 2
1342 * seconds." We use 3 seconds, because it seems for some
1343 * drivers, exactly 2 seconds is too fast.
1346 if (sc->ndis_block.nmb_checkforhangsecs == 0)
1347 sc->ndis_block.nmb_checkforhangsecs = 3;
1349 callout_reset(&sc->ndis_stat_timer,
1350 hz * sc->ndis_block.nmb_checkforhangsecs, ndis_tick, sc);
1354 * Set media options.
1357 ndis_ifmedia_upd(ifp)
1360 struct ndis_softc *sc;
1364 if (NDIS_INITIALIZED(sc))
1371 * Report current media status.
1374 ndis_ifmedia_sts(ifp, ifmr)
1376 struct ifmediareq *ifmr;
1378 struct ndis_softc *sc;
1379 uint32_t media_info;
1380 ndis_media_state linkstate;
1383 ifmr->ifm_status = IFM_AVALID;
1384 ifmr->ifm_active = IFM_ETHER;
1387 if (!NDIS_INITIALIZED(sc))
1390 len = sizeof(linkstate);
1391 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1392 (void *)&linkstate, &len);
1394 len = sizeof(media_info);
1395 error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
1396 (void *)&media_info, &len);
1398 if (linkstate == nmc_connected)
1399 ifmr->ifm_status |= IFM_ACTIVE;
1401 switch(media_info) {
1403 ifmr->ifm_active |= IFM_10_T;
1406 ifmr->ifm_active |= IFM_100_TX;
1409 ifmr->ifm_active |= IFM_1000_T;
1412 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
1420 ndis_setstate_80211(sc)
1421 struct ndis_softc *sc;
1423 struct ieee80211com *ic;
1424 ndis_80211_ssid ssid;
1425 ndis_80211_config config;
1427 int i, rval = 0, len;
1432 ifp = &sc->ic.ic_ac.ac_if;
1434 if (!NDIS_INITIALIZED(sc))
1437 /* Set network infrastructure mode. */
1440 if (ic->ic_opmode == IEEE80211_M_IBSS)
1441 arg = NDIS_80211_NET_INFRA_IBSS;
1443 arg = NDIS_80211_NET_INFRA_BSS;
1445 rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
1448 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
1452 #ifdef IEEE80211_F_WEPON
1453 if (ic->ic_flags & IEEE80211_F_WEPON) {
1455 if (ic->ic_wep_mode >= IEEE80211_WEP_ON) {
1457 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1458 if (ic->ic_nw_keys[i].wk_len) {
1459 bzero((char *)&wep, sizeof(wep));
1460 wep.nw_keylen = ic->ic_nw_keys[i].wk_len;
1462 /* 5 and 13 are the only valid key lengths */
1463 if (ic->ic_nw_keys[i].wk_len < 5)
1465 else if (ic->ic_nw_keys[i].wk_len > 5 &&
1466 ic->ic_nw_keys[i].wk_len < 13)
1470 wep.nw_length = (sizeof(uint32_t) * 3)
1472 if (i == ic->ic_wep_txkey)
1473 wep.nw_keyidx |= NDIS_80211_WEPKEY_TX;
1474 bcopy(ic->ic_nw_keys[i].wk_key,
1475 wep.nw_keydata, wep.nw_length);
1477 rval = ndis_set_info(sc,
1478 OID_802_11_ADD_WEP, &wep, &len);
1480 device_printf(sc->ndis_dev,
1481 "set wepkey failed: %d\n", rval);
1484 arg = NDIS_80211_WEPSTAT_ENABLED;
1486 rval = ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1488 device_printf(sc->ndis_dev,
1489 "enable WEP failed: %d\n", rval);
1490 #ifndef IEEE80211_F_WEPON
1491 if (ic->ic_wep_mode != IEEE80211_WEP_8021X &&
1492 ic->ic_wep_mode != IEEE80211_WEP_ON)
1493 arg = NDIS_80211_PRIVFILT_ACCEPTALL;
1496 arg = NDIS_80211_PRIVFILT_8021XWEP;
1498 rval = ndis_set_info(sc, OID_802_11_PRIVACY_FILTER, &arg, &len);
1499 #ifdef IEEE80211_WEP_8021X /*IEEE80211_F_WEPON*/
1500 /* Accept that we only have "shared" and 802.1x modes. */
1502 if (arg == NDIS_80211_PRIVFILT_ACCEPTALL)
1503 ic->ic_wep_mode = IEEE80211_WEP_MIXED;
1505 ic->ic_wep_mode = IEEE80211_WEP_8021X;
1508 arg = NDIS_80211_AUTHMODE_OPEN;
1510 arg = NDIS_80211_WEPSTAT_DISABLED;
1512 ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1513 arg = NDIS_80211_AUTHMODE_OPEN;
1517 rval = ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
1521 device_printf (sc->ndis_dev, "set auth failed: %d\n", rval);
1525 /* Set network type. */
1529 switch (ic->ic_curmode) {
1530 case IEEE80211_MODE_11A:
1531 arg = NDIS_80211_NETTYPE_11OFDM5;
1533 case IEEE80211_MODE_11B:
1534 arg = NDIS_80211_NETTYPE_11DS;
1536 case IEEE80211_MODE_11G:
1537 arg = NDIS_80211_NETTYPE_11OFDM24;
1540 device_printf(sc->ndis_dev, "unknown mode: %d\n",
1546 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
1549 device_printf (sc->ndis_dev,
1550 "set nettype failed: %d\n", rval);
1554 len = sizeof(config);
1555 bzero((char *)&config, len);
1556 config.nc_length = len;
1557 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1558 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1561 * Some drivers expect us to initialize these values, so
1562 * provide some defaults.
1564 if (config.nc_beaconperiod == 0)
1565 config.nc_beaconperiod = 100;
1566 if (config.nc_atimwin == 0)
1567 config.nc_atimwin = 100;
1568 if (config.nc_fhconfig.ncf_dwelltime == 0)
1569 config.nc_fhconfig.ncf_dwelltime = 200;
1571 if (rval == 0 && ic->ic_ibss_chan != IEEE80211_CHAN_ANYC) {
1574 chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
1575 chanflag = config.nc_dsconfig > 2500000 ? IEEE80211_CHAN_2GHZ :
1576 IEEE80211_CHAN_5GHZ;
1577 if (chan != ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0)) {
1578 config.nc_dsconfig =
1579 ic->ic_ibss_chan->ic_freq * 1000;
1580 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1581 len = sizeof(config);
1582 config.nc_length = len;
1583 config.nc_fhconfig.ncf_length =
1584 sizeof(ndis_80211_config_fh);
1585 rval = ndis_set_info(sc, OID_802_11_CONFIGURATION,
1588 device_printf(sc->ndis_dev, "couldn't change "
1589 "DS config to %ukHz: %d\n",
1590 config.nc_dsconfig, rval);
1593 device_printf(sc->ndis_dev, "couldn't retrieve "
1594 "channel info: %d\n", rval);
1596 /* Set SSID -- always do this last. */
1599 bzero((char *)&ssid, len);
1600 ssid.ns_ssidlen = ic->ic_des_esslen;
1601 if (ssid.ns_ssidlen == 0) {
1602 ssid.ns_ssidlen = 1;
1604 bcopy(ic->ic_des_essid, ssid.ns_ssid, ssid.ns_ssidlen);
1605 rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
1608 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
1614 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1616 struct ieee80211com *ic = (void *)ifp;
1617 struct ieee80211_node *ni = NULL;
1619 imr->ifm_status = IFM_AVALID;
1620 imr->ifm_active = IFM_IEEE80211;
1621 if (ic->ic_state == IEEE80211_S_RUN)
1622 imr->ifm_status |= IFM_ACTIVE;
1623 imr->ifm_active |= IFM_AUTO;
1624 switch (ic->ic_opmode) {
1625 case IEEE80211_M_STA:
1627 /* calculate rate subtype */
1628 imr->ifm_active |= ieee80211_rate2media(ic,
1629 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1631 case IEEE80211_M_IBSS:
1633 /* calculate rate subtype */
1634 imr->ifm_active |= ieee80211_rate2media(ic,
1635 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1636 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1638 case IEEE80211_M_AHDEMO:
1639 /* should not come here */
1641 case IEEE80211_M_HOSTAP:
1642 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1644 case IEEE80211_M_MONITOR:
1645 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1648 switch (ic->ic_curmode) {
1649 case IEEE80211_MODE_11A:
1650 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
1652 case IEEE80211_MODE_11B:
1653 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
1655 case IEEE80211_MODE_11G:
1656 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
1658 case IEEE80211_MODE_TURBO:
1659 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
1660 | IFM_IEEE80211_TURBO;
1666 ndis_get_assoc(sc, assoc)
1667 struct ndis_softc *sc;
1668 ndis_wlan_bssid_ex **assoc;
1670 ndis_80211_bssid_list_ex *bl;
1671 ndis_wlan_bssid_ex *bs;
1672 ndis_80211_macaddr bssid;
1678 len = sizeof(bssid);
1679 error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
1681 device_printf(sc->ndis_dev, "failed to get bssid\n");
1685 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1686 if (error != ENOSPC) {
1687 device_printf(sc->ndis_dev, "bssid_list failed\n");
1691 bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
1694 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1697 device_printf(sc->ndis_dev, "bssid_list failed\n");
1701 bs = (ndis_wlan_bssid_ex *)&bl->nblx_bssid[0];
1702 for (i = 0; i < bl->nblx_items; i++) {
1703 if (bcmp(bs->nwbx_macaddr, bssid, sizeof(bssid)) == 0) {
1704 *assoc = malloc(bs->nwbx_len, M_TEMP, M_NOWAIT);
1705 if (*assoc == NULL) {
1709 bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
1713 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
1721 ndis_getstate_80211(sc)
1722 struct ndis_softc *sc;
1724 struct ieee80211com *ic;
1725 ndis_80211_ssid ssid;
1726 ndis_80211_config config;
1727 ndis_wlan_bssid_ex *bs;
1728 int rval, len, i = 0;
1733 ifp = &sc->ic.ic_ac.ac_if;
1735 if (!NDIS_INITIALIZED(sc))
1739 ic->ic_state = IEEE80211_S_RUN;
1741 ic->ic_state = IEEE80211_S_ASSOC;
1745 * If we're associated, retrieve info on the current bssid.
1747 if ((rval = ndis_get_assoc(sc, &bs)) == 0) {
1748 switch(bs->nwbx_nettype) {
1749 case NDIS_80211_NETTYPE_11FH:
1750 case NDIS_80211_NETTYPE_11DS:
1751 ic->ic_curmode = IEEE80211_MODE_11B;
1753 case NDIS_80211_NETTYPE_11OFDM5:
1754 ic->ic_curmode = IEEE80211_MODE_11A;
1756 case NDIS_80211_NETTYPE_11OFDM24:
1757 ic->ic_curmode = IEEE80211_MODE_11G;
1760 device_printf(sc->ndis_dev,
1761 "unknown nettype %d\n", arg);
1769 bzero((char *)&ssid, len);
1770 rval = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
1773 device_printf (sc->ndis_dev, "get ssid failed: %d\n", rval);
1774 bcopy(ssid.ns_ssid, ic->ic_bss->ni_essid, ssid.ns_ssidlen);
1775 ic->ic_bss->ni_esslen = ssid.ns_ssidlen;
1778 rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
1780 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
1783 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
1784 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
1785 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1786 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1787 IEEE80211_RATE_VAL) == arg / 5000)
1792 if (i == ic->ic_bss->ni_rates.rs_nrates &&
1793 ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
1794 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11G];
1795 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1796 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1797 IEEE80211_RATE_VAL) == arg / 5000)
1802 if (i == ic->ic_bss->ni_rates.rs_nrates)
1803 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
1806 ic->ic_bss->ni_txrate = i;
1808 if (ic->ic_caps & IEEE80211_C_PMGT) {
1810 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
1813 device_printf(sc->ndis_dev,
1814 "get power mode failed: %d\n", rval);
1815 if (arg == NDIS_80211_POWERMODE_CAM)
1816 ic->ic_flags &= ~IEEE80211_F_PMGTON;
1818 ic->ic_flags |= IEEE80211_F_PMGTON;
1821 len = sizeof(config);
1822 bzero((char *)&config, len);
1823 config.nc_length = len;
1824 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1825 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1829 chan = ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0);
1830 if (chan < 0 || chan >= IEEE80211_CHAN_MAX) {
1831 if (ifp->if_flags & IFF_DEBUG)
1832 device_printf(sc->ndis_dev, "current channel "
1833 "(%uMHz) out of bounds\n",
1834 config.nc_dsconfig / 1000);
1835 ic->ic_bss->ni_chan = &ic->ic_channels[1];
1837 ic->ic_bss->ni_chan = &ic->ic_channels[chan];
1839 device_printf(sc->ndis_dev, "couldn't retrieve "
1840 "channel info: %d\n", rval);
1844 rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1847 device_printf (sc->ndis_dev,
1848 "get wep status failed: %d\n", rval);
1850 if (arg == NDIS_80211_WEPSTAT_ENABLED)
1851 ic->ic_flags |= IEEE80211_F_WEPON;
1853 ic->ic_flags &= ~IEEE80211_F_WEPON;
1859 ndis_ioctl(ifp, command, data, cr)
1865 struct ndis_softc *sc = ifp->if_softc;
1866 struct ifreq *ifr = (struct ifreq *) data;
1873 if (ifp->if_flags & IFF_UP) {
1874 if (ifp->if_flags & IFF_RUNNING &&
1875 ifp->if_flags & IFF_PROMISC &&
1876 !(sc->ndis_if_flags & IFF_PROMISC)) {
1878 NDIS_PACKET_TYPE_PROMISCUOUS;
1879 i = sizeof(sc->ndis_filter);
1880 error = ndis_set_info(sc,
1881 OID_GEN_CURRENT_PACKET_FILTER,
1882 &sc->ndis_filter, &i);
1883 } else if (ifp->if_flags & IFF_RUNNING &&
1884 !(ifp->if_flags & IFF_PROMISC) &&
1885 sc->ndis_if_flags & IFF_PROMISC) {
1887 ~NDIS_PACKET_TYPE_PROMISCUOUS;
1888 i = sizeof(sc->ndis_filter);
1889 error = ndis_set_info(sc,
1890 OID_GEN_CURRENT_PACKET_FILTER,
1891 &sc->ndis_filter, &i);
1895 if (ifp->if_flags & IFF_RUNNING)
1898 sc->ndis_if_flags = ifp->if_flags;
1908 if (sc->ndis_80211) {
1909 error = ieee80211_ioctl(ifp, command, data, cr);
1910 if (error == ENETRESET) {
1911 ndis_setstate_80211(sc);
1916 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1919 ifp->if_capenable = ifr->ifr_reqcap;
1920 if (ifp->if_capenable & IFCAP_TXCSUM)
1921 ifp->if_hwassist = sc->ndis_hwassist;
1923 ifp->if_hwassist = 0;
1924 ndis_set_offload(sc);
1926 case SIOCGIFGENERIC:
1927 case SIOCSIFGENERIC:
1928 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
1929 if (command == SIOCGIFGENERIC)
1930 error = ndis_wi_ioctl_get(ifp, command, data);
1932 error = ndis_wi_ioctl_set(ifp, command, data);
1935 if (error != ENOTTY)
1939 if (sc->ndis_80211) {
1940 error = ieee80211_ioctl(ifp, command, data, cr);
1941 if (error == ENETRESET) {
1942 ndis_setstate_80211(sc);
1946 error = ether_ioctl(ifp, command, data);
1951 /*NDIS_UNLOCK(sc);*/
1957 ndis_wi_ioctl_get(ifp, command, data)
1964 struct ndis_softc *sc;
1965 ndis_80211_bssid_list_ex *bl;
1966 ndis_wlan_bssid_ex *wb;
1967 struct wi_apinfo *api;
1968 int error, i, j, len, maxaps;
1971 ifr = (struct ifreq *)data;
1972 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1976 switch (wreq.wi_type) {
1977 case WI_RID_READ_APS:
1979 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
1982 tsleep(&error, PCATCH, "ssidscan", hz * 2);
1984 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1985 if (error != ENOSPC)
1987 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
1988 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1993 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
1994 maxaps = MIN(maxaps, bl->nblx_items);
1995 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
1996 *(int *)&wreq.wi_val = maxaps;
1997 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
1998 wb = bl->nblx_bssid;
2000 bzero(api, sizeof(*api));
2001 bcopy(&wb->nwbx_macaddr, &api->bssid,
2002 sizeof(api->bssid));
2003 api->namelen = wb->nwbx_ssid.ns_ssidlen;
2004 bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
2005 if (wb->nwbx_privacy)
2006 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
2007 /* XXX Where can we get noise information? */
2008 api->signal = wb->nwbx_rssi + 149; /* XXX */
2009 api->quality = api->signal;
2011 ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2013 /* In "auto" infrastructure mode, this is useless. */
2014 if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
2015 api->capinfo |= IEEE80211_CAPINFO_IBSS;
2016 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
2017 j = sizeof(ndis_80211_rates_ex);
2018 /* handle other extended things */
2020 j = sizeof(ndis_80211_rates);
2021 for (i = api->rate = 0; i < j; i++)
2022 api->rate = MAX(api->rate, 5 *
2023 (wb->nwbx_supportedrates[i] & 0x7f));
2025 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2028 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2038 ndis_wi_ioctl_set(ifp, command, data)
2045 struct ndis_softc *sc;
2049 error = suser(curthread);
2054 ifr = (struct ifreq *)data;
2055 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2059 switch (wreq.wi_type) {
2060 case WI_RID_SCAN_APS:
2061 case WI_RID_SCAN_REQ: /* arguments ignored */
2064 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2078 struct ndis_softc *sc;
2085 device_printf(sc->ndis_dev, "watchdog timeout\n");
2089 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2095 * Stop the adapter and free any mbufs allocated to the
2100 struct ndis_softc *sc;
2105 ifp = &sc->arpcom.ac_if;
2106 callout_stop(&sc->ndis_stat_timer);
2113 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2120 * Stop all chip I/O so that the kernel's probe routines don't
2121 * get confused by errant DMAs when rebooting.
2127 struct ndis_softc *sc;
2129 sc = device_get_softc(dev);
2130 ndis_shutdown_nic(sc);