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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
46 #if __FreeBSD_version < 502113
47 #include <sys/sysctl.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 <net80211/ieee80211_var.h>
67 #include <net80211/ieee80211_ioctl.h>
69 #include <dev/wi/if_wavelan_ieee.h>
71 #include <dev/pci/pcireg.h>
72 #include <dev/pci/pcivar.h>
74 #include <compat/ndis/pe_var.h>
75 #include <compat/ndis/resource_var.h>
76 #include <compat/ndis/ntoskrnl_var.h>
77 #include <compat/ndis/hal_var.h>
78 #include <compat/ndis/ndis_var.h>
79 #include <compat/ndis/cfg_var.h>
80 #include <dev/if_ndis/if_ndisvar.h>
85 #include "ndis_driver_data.h"
87 int ndis_attach (device_t);
88 int ndis_detach (device_t);
89 int ndis_suspend (device_t);
90 int ndis_resume (device_t);
91 void ndis_shutdown (device_t);
93 static __stdcall void ndis_txeof (ndis_handle,
94 ndis_packet *, ndis_status);
95 static __stdcall void ndis_rxeof (ndis_handle,
96 ndis_packet **, uint32_t);
97 static __stdcall void ndis_linksts (ndis_handle,
98 ndis_status, void *, uint32_t);
99 static __stdcall void ndis_linksts_done (ndis_handle);
101 static void ndis_intr (void *);
102 static void ndis_intrtask (void *);
103 static void ndis_tick (void *);
104 static void ndis_ticktask (void *);
105 static void ndis_start (struct ifnet *);
106 static void ndis_starttask (void *);
107 static int ndis_ioctl (struct ifnet *, u_long, caddr_t);
108 static int ndis_wi_ioctl_get (struct ifnet *, u_long, caddr_t);
109 static int ndis_wi_ioctl_set (struct ifnet *, u_long, caddr_t);
110 static void ndis_init (void *);
111 static void ndis_stop (struct ndis_softc *);
112 static void ndis_watchdog (struct ifnet *);
113 static int ndis_ifmedia_upd (struct ifnet *);
114 static void ndis_ifmedia_sts (struct ifnet *, struct ifmediareq *);
115 static int ndis_get_assoc (struct ndis_softc *, ndis_wlan_bssid_ex **);
116 static int ndis_probe_offload (struct ndis_softc *);
117 static int ndis_set_offload (struct ndis_softc *);
118 static void ndis_getstate_80211 (struct ndis_softc *);
119 static void ndis_setstate_80211 (struct ndis_softc *);
120 static void ndis_media_status (struct ifnet *, struct ifmediareq *);
122 static void ndis_setmulti (struct ndis_softc *);
123 static void ndis_map_sclist (void *, bus_dma_segment_t *,
124 int, bus_size_t, int);
126 extern struct mtx_pool *ndis_mtxpool;
129 * Program the 64-bit multicast hash filter.
133 struct ndis_softc *sc;
136 struct ifmultiaddr *ifma;
137 int len, mclistsz, error;
140 ifp = &sc->arpcom.ac_if;
142 if (!NDIS_INITIALIZED(sc))
145 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
146 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
147 len = sizeof(sc->ndis_filter);
148 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
149 &sc->ndis_filter, &len);
151 device_printf (sc->ndis_dev,
152 "set filter failed: %d\n", error);
156 if (TAILQ_EMPTY(&ifp->if_multiaddrs))
159 len = sizeof(mclistsz);
160 ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
162 mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
164 if (mclist == NULL) {
165 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
169 sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
172 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
173 if (ifma->ifma_addr->sa_family != AF_LINK)
175 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
176 mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
178 if (len > mclistsz) {
179 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
180 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
185 len = len * ETHER_ADDR_LEN;
186 error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
188 device_printf (sc->ndis_dev, "set mclist failed: %d\n", error);
189 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
190 sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
194 free(mclist, M_TEMP);
196 len = sizeof(sc->ndis_filter);
197 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
198 &sc->ndis_filter, &len);
200 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
207 struct ndis_softc *sc;
209 ndis_task_offload *nto;
210 ndis_task_offload_hdr *ntoh;
211 ndis_task_tcpip_csum *nttc;
215 ifp = &sc->arpcom.ac_if;
217 if (!NDIS_INITIALIZED(sc))
220 /* See if there's anything to set. */
222 error = ndis_probe_offload(sc);
226 if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
229 len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
230 sizeof(ndis_task_tcpip_csum);
232 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
237 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
238 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
239 ntoh->ntoh_offset_firsttask = sizeof(ndis_task_offload_hdr);
240 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
241 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
242 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
244 nto = (ndis_task_offload *)((char *)ntoh +
245 ntoh->ntoh_offset_firsttask);
247 nto->nto_vers = NDIS_TASK_OFFLOAD_VERSION;
248 nto->nto_len = sizeof(ndis_task_offload);
249 nto->nto_task = NDIS_TASK_TCPIP_CSUM;
250 nto->nto_offset_nexttask = 0;
251 nto->nto_taskbuflen = sizeof(ndis_task_tcpip_csum);
253 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
255 if (ifp->if_capenable & IFCAP_TXCSUM)
256 nttc->nttc_v4tx = sc->ndis_v4tx;
258 if (ifp->if_capenable & IFCAP_RXCSUM)
259 nttc->nttc_v4rx = sc->ndis_v4rx;
261 error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
268 ndis_probe_offload(sc)
269 struct ndis_softc *sc;
271 ndis_task_offload *nto;
272 ndis_task_offload_hdr *ntoh;
273 ndis_task_tcpip_csum *nttc = NULL;
275 int len, error, dummy;
277 ifp = &sc->arpcom.ac_if;
280 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
285 ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
290 ntoh->ntoh_vers = NDIS_TASK_OFFLOAD_VERSION;
291 ntoh->ntoh_len = sizeof(ndis_task_offload_hdr);
292 ntoh->ntoh_encapfmt.nef_encaphdrlen = sizeof(struct ether_header);
293 ntoh->ntoh_encapfmt.nef_encap = NDIS_ENCAP_IEEE802_3;
294 ntoh->ntoh_encapfmt.nef_flags = NDIS_ENCAPFLAG_FIXEDHDRLEN;
296 error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
303 if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
308 nto = (ndis_task_offload *)((char *)ntoh +
309 ntoh->ntoh_offset_firsttask);
312 switch (nto->nto_task) {
313 case NDIS_TASK_TCPIP_CSUM:
314 nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
316 /* Don't handle these yet. */
317 case NDIS_TASK_IPSEC:
318 case NDIS_TASK_TCP_LARGESEND:
322 if (nto->nto_offset_nexttask == 0)
324 nto = (ndis_task_offload *)((char *)nto +
325 nto->nto_offset_nexttask);
333 sc->ndis_v4tx = nttc->nttc_v4tx;
334 sc->ndis_v4rx = nttc->nttc_v4rx;
336 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_IP_CSUM)
337 sc->ndis_hwassist |= CSUM_IP;
338 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
339 sc->ndis_hwassist |= CSUM_TCP;
340 if (nttc->nttc_v4tx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
341 sc->ndis_hwassist |= CSUM_UDP;
343 if (sc->ndis_hwassist)
344 ifp->if_capabilities |= IFCAP_TXCSUM;
346 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_IP_CSUM)
347 ifp->if_capabilities |= IFCAP_RXCSUM;
348 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_TCP_CSUM)
349 ifp->if_capabilities |= IFCAP_RXCSUM;
350 if (nttc->nttc_v4rx & NDIS_TCPSUM_FLAGS_UDP_CSUM)
351 ifp->if_capabilities |= IFCAP_RXCSUM;
358 * Attach the interface. Allocate softc structures, do ifmedia
359 * setup and ethernet/BPF attach.
365 u_char eaddr[ETHER_ADDR_LEN];
366 struct ndis_softc *sc;
367 struct ifnet *ifp = NULL;
372 sc = device_get_softc(dev);
374 mtx_init(&sc->ndis_mtx, "ndis softc lock",
375 MTX_NETWORK_LOCK, MTX_DEF);
376 mtx_init(&sc->ndis_intrmtx,
377 "ndis irq lock", MTX_NETWORK_LOCK, MTX_DEF);
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,
385 ndis_intr, sc, &sc->ndis_intrhand);
388 device_printf(dev, "couldn't set up irq\n");
392 if (sc->ndis_iftype == PCMCIABus) {
393 error = ndis_alloc_amem(sc);
395 device_printf(dev, "failed to allocate "
396 "attribute memory\n");
401 sc->ndis_regvals = ndis_regvals;
403 #if __FreeBSD_version < 502113
404 sysctl_ctx_init(&sc->ndis_ctx);
407 /* Create sysctl registry nodes */
408 ndis_create_sysctls(sc);
410 /* Set up driver image in memory. */
412 ndis_load_driver((vm_offset_t)img, sc);
414 /* Tell the user what version of the API the driver is using. */
415 device_printf(dev, "NDIS API version: %d.%d\n",
416 sc->ndis_chars.nmc_version_major,
417 sc->ndis_chars.nmc_version_minor);
419 /* Do resource conversion. */
420 ndis_convert_res(sc);
422 /* Install our RX and TX interrupt handlers. */
423 sc->ndis_block.nmb_senddone_func = ndis_txeof;
424 sc->ndis_block.nmb_pktind_func = ndis_rxeof;
426 /* Call driver's init routine. */
427 if (ndis_init_nic(sc)) {
428 device_printf (dev, "init handler failed\n");
434 * Get station address from the driver.
437 ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
439 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
442 * Figure out if we're allowed to use multipacket sends
443 * with this driver, and if so, how many.
446 if (sc->ndis_chars.nmc_sendsingle_func &&
447 sc->ndis_chars.nmc_sendmulti_func == NULL) {
448 sc->ndis_maxpkts = 1;
450 len = sizeof(sc->ndis_maxpkts);
451 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
452 &sc->ndis_maxpkts, &len);
455 sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
456 sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
458 sc->ndis_txpending = sc->ndis_maxpkts;
461 /* Get supported oid list. */
462 ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
464 /* If the NDIS module requested scatter/gather, init maps. */
469 * See if the OID_802_11_CONFIGURATION OID is
470 * supported by this driver. If it is, then this an 802.11
471 * wireless driver, and we should set up media for wireless.
473 for (i = 0; i < sc->ndis_oidcnt; i++) {
474 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
480 /* Check for task offload support. */
481 ndis_probe_offload(sc);
483 ifp = &sc->arpcom.ac_if;
485 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
486 ifp->if_mtu = ETHERMTU;
487 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
488 ifp->if_ioctl = ndis_ioctl;
489 ifp->if_start = ndis_start;
490 ifp->if_watchdog = ndis_watchdog;
491 ifp->if_init = ndis_init;
492 ifp->if_baudrate = 10000000;
493 #if __FreeBSD_version < 502114
494 ifp->if_snd.ifq_maxlen = 50;
496 IFQ_SET_MAXLEN(&ifp->if_snd, 50);
497 ifp->if_snd.ifq_drv_maxlen = 25;
498 IFQ_SET_READY(&ifp->if_snd);
500 ifp->if_capenable = ifp->if_capabilities;
501 ifp->if_hwassist = sc->ndis_hwassist;
504 if (sc->ndis_80211) {
505 struct ieee80211com *ic = (void *)ifp;
506 ndis_80211_rates_ex rates;
507 struct ndis_80211_nettype_list *ntl;
511 ic->ic_phytype = IEEE80211_T_DS;
512 ic->ic_opmode = IEEE80211_M_STA;
513 ic->ic_caps = IEEE80211_C_IBSS;
514 ic->ic_state = IEEE80211_S_ASSOC;
515 ic->ic_modecaps = (1<<IEEE80211_MODE_AUTO);
517 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
521 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
522 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
529 for (i = 0; i < ntl->ntl_items; i++) {
530 switch (ntl->ntl_type[i]) {
531 case NDIS_80211_NETTYPE_11FH:
532 case NDIS_80211_NETTYPE_11DS:
533 ic->ic_modecaps |= (1<<IEEE80211_MODE_11B);
535 case NDIS_80211_NETTYPE_11OFDM5:
536 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
538 case NDIS_80211_NETTYPE_11OFDM24:
539 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
548 bzero((char *)&rates, len);
549 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
550 (void *)rates, &len);
552 device_printf (dev, "get rates failed: 0x%x\n", r);
554 * Since the supported rates only up to 8 can be supported,
555 * if this is not 802.11b we're just going to be faking it
559 #define TESTSETRATE(x, y) \
562 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) { \
563 if (ic->ic_sup_rates[x].rs_rates[i] == (y)) \
566 if (i == ic->ic_sup_rates[x].rs_nrates) { \
567 ic->ic_sup_rates[x].rs_rates[i] = (y); \
568 ic->ic_sup_rates[x].rs_nrates++; \
572 #define SETRATE(x, y) \
573 ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
575 ic->ic_sup_rates[x].rs_nrates++
577 ic->ic_curmode = IEEE80211_MODE_AUTO;
578 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A))
579 ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
580 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B))
581 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
582 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G))
583 ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
584 for (i = 0; i < len; i++) {
585 switch (rates[i] & IEEE80211_RATE_VAL) {
591 if (!(ic->ic_modecaps &
592 (1<<IEEE80211_MODE_11B))) {
593 /* Lazy-init 802.11b. */
595 (1<<IEEE80211_MODE_11B);
596 ic->ic_sup_rates[IEEE80211_MODE_11B].
599 SETRATE(IEEE80211_MODE_11B, rates[i]);
600 INCRATE(IEEE80211_MODE_11B);
603 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
604 SETRATE(IEEE80211_MODE_11A, rates[i]);
605 INCRATE(IEEE80211_MODE_11A);
607 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
608 SETRATE(IEEE80211_MODE_11G, rates[i]);
609 INCRATE(IEEE80211_MODE_11G);
616 * If the hardware supports 802.11g, it most
617 * likely supports 802.11b and all of the
618 * 802.11b and 802.11g speeds, so maybe we can
619 * just cheat here. Just how in the heck do
620 * we detect turbo modes, though?
622 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
623 TESTSETRATE(IEEE80211_MODE_11B,
624 IEEE80211_RATE_BASIC|2);
625 TESTSETRATE(IEEE80211_MODE_11B,
626 IEEE80211_RATE_BASIC|4);
627 TESTSETRATE(IEEE80211_MODE_11B,
628 IEEE80211_RATE_BASIC|11);
629 TESTSETRATE(IEEE80211_MODE_11B,
630 IEEE80211_RATE_BASIC|22);
632 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
633 TESTSETRATE(IEEE80211_MODE_11G, 47);
634 TESTSETRATE(IEEE80211_MODE_11G, 72);
635 TESTSETRATE(IEEE80211_MODE_11G, 96);
636 TESTSETRATE(IEEE80211_MODE_11G, 108);
638 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
639 TESTSETRATE(IEEE80211_MODE_11A, 47);
640 TESTSETRATE(IEEE80211_MODE_11A, 72);
641 TESTSETRATE(IEEE80211_MODE_11A, 96);
642 TESTSETRATE(IEEE80211_MODE_11A, 108);
647 * Taking yet more guesses here.
649 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
652 if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
653 chanflag |= IEEE80211_CHAN_G;
655 chanflag |= IEEE80211_CHAN_B;
656 if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
658 chanflag = IEEE80211_CHAN_A;
661 ic->ic_channels[i].ic_freq =
662 ieee80211_ieee2mhz(i, chanflag);
663 ic->ic_channels[i].ic_flags = chanflag;
667 r = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &i);
668 if (arg != NDIS_80211_WEPSTAT_NOTSUPPORTED)
669 ic->ic_caps |= IEEE80211_C_WEP;
671 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
673 ic->ic_caps |= IEEE80211_C_PMGT;
674 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
675 ieee80211_ifattach(ifp);
676 ieee80211_media_init(ifp, ieee80211_media_change,
678 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
679 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
681 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
683 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
684 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
685 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
686 ifmedia_add(&sc->ifmedia,
687 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
688 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
689 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
690 ether_ifattach(ifp, eaddr);
693 /* Override the status handler so we can detect link changes. */
694 sc->ndis_block.nmb_status_func = ndis_linksts;
695 sc->ndis_block.nmb_statusdone_func = ndis_linksts_done;
700 /* We're done talking to the NIC for now; halt it. */
707 * Shutdown hardware and free up resources. This can be called any
708 * time after the mutex has been initialized. It is called in both
709 * the error case in attach and the normal detach case so it needs
710 * to be careful about only freeing resources that have actually been
717 struct ndis_softc *sc;
720 sc = device_get_softc(dev);
721 KASSERT(mtx_initialized(&sc->ndis_mtx),
722 ("ndis mutex not initialized"));
723 KASSERT(mtx_initialized(&sc->ndis_intrmtx),
724 ("ndis interrupt mutex not initialized"));
726 ifp = &sc->arpcom.ac_if;
727 ifp->if_flags &= ~IFF_UP;
729 if (device_is_attached(dev)) {
733 ieee80211_ifdetach(ifp);
739 bus_generic_detach(dev);
741 if (sc->ndis_intrhand)
742 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
744 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
746 bus_release_resource(dev, SYS_RES_IOPORT,
747 sc->ndis_io_rid, sc->ndis_res_io);
748 if (sc->ndis_res_mem)
749 bus_release_resource(dev, SYS_RES_MEMORY,
750 sc->ndis_mem_rid, sc->ndis_res_mem);
751 if (sc->ndis_res_altmem)
752 bus_release_resource(dev, SYS_RES_MEMORY,
753 sc->ndis_altmem_rid, sc->ndis_res_altmem);
755 if (sc->ndis_iftype == PCMCIABus)
759 ndis_destroy_dma(sc);
761 if (sc->ndis_txarray)
762 free(sc->ndis_txarray, M_DEVBUF);
765 ifmedia_removeall(&sc->ifmedia);
767 ndis_unload_driver((void *)ifp);
769 if (sc->ndis_iftype == PCIBus)
770 bus_dma_tag_destroy(sc->ndis_parent_tag);
772 #if __FreeBSD_version < 502113
773 sysctl_ctx_free(&sc->ndis_ctx);
776 mtx_destroy(&sc->ndis_mtx);
777 mtx_destroy(&sc->ndis_intrmtx);
786 struct ndis_softc *sc;
789 sc = device_get_softc(dev);
790 ifp = &sc->arpcom.ac_if;
793 if (NDIS_INITIALIZED(sc))
804 struct ndis_softc *sc;
807 sc = device_get_softc(dev);
808 ifp = &sc->arpcom.ac_if;
810 if (NDIS_INITIALIZED(sc))
817 * A frame has been uploaded: pass the resulting mbuf chain up to
818 * the higher level protocols.
820 * When handling received NDIS packets, the 'status' field in the
821 * out-of-band portion of the ndis_packet has special meaning. In the
822 * most common case, the underlying NDIS driver will set this field
823 * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
824 * take posession of it. We then change the status field to
825 * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
826 * and that we will return it at some point in the future via the
827 * return packet handler.
829 * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
830 * this means the driver is running out of packet/buffer resources and
831 * wants to maintain ownership of the packet. In this case, we have to
832 * copy the packet data into local storage and let the driver keep the
835 __stdcall static void
836 ndis_rxeof(adapter, packets, pktcnt)
838 ndis_packet **packets;
841 struct ndis_softc *sc;
842 ndis_miniport_block *block;
845 ndis_tcpip_csum *csum;
850 block = (ndis_miniport_block *)adapter;
851 sc = (struct ndis_softc *)(block->nmb_ifp);
852 ifp = block->nmb_ifp;
854 for (i = 0; i < pktcnt; i++) {
856 /* Stash the softc here so ptom can use it. */
858 if (ndis_ptom(&m0, p)) {
859 device_printf (sc->ndis_dev, "ptom failed\n");
860 if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
861 ndis_return_packet(sc, p);
863 if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
864 m = m_dup(m0, M_DONTWAIT);
866 * NOTE: we want to destroy the mbuf here, but
867 * we don't actually want to return it to the
868 * driver via the return packet handler. By
869 * bumping np_refcnt, we can prevent the
870 * ndis_return_packet() routine from actually
880 p->np_oob.npo_status = NDIS_STATUS_PENDING;
881 m0->m_pkthdr.rcvif = ifp;
884 /* Deal with checksum offload. */
886 if (ifp->if_capenable & IFCAP_RXCSUM &&
887 p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
889 p->np_ext.npe_info[ndis_tcpipcsum_info];
890 csum = (ndis_tcpip_csum *)&s;
891 if (csum->u.ntc_rxflags &
892 NDIS_RXCSUM_IP_PASSED)
893 m0->m_pkthdr.csum_flags |=
894 CSUM_IP_CHECKED|CSUM_IP_VALID;
895 if (csum->u.ntc_rxflags &
896 (NDIS_RXCSUM_TCP_PASSED |
897 NDIS_RXCSUM_UDP_PASSED)) {
898 m0->m_pkthdr.csum_flags |=
899 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
900 m0->m_pkthdr.csum_data = 0xFFFF;
904 (*ifp->if_input)(ifp, m0);
912 * A frame was downloaded to the chip. It's safe for us to clean up
915 __stdcall static void
916 ndis_txeof(adapter, packet, status)
922 struct ndis_softc *sc;
923 ndis_miniport_block *block;
928 block = (ndis_miniport_block *)adapter;
929 sc = (struct ndis_softc *)block->nmb_ifp;
930 ifp = block->nmb_ifp;
933 idx = packet->np_txidx;
935 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
937 ndis_free_packet(packet);
941 sc->ndis_txarray[idx] = NULL;
942 sc->ndis_txpending++;
944 if (status == NDIS_STATUS_SUCCESS)
949 ifp->if_flags &= ~IFF_OACTIVE;
952 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
957 __stdcall static void
958 ndis_linksts(adapter, status, sbuf, slen)
964 ndis_miniport_block *block;
967 block->nmb_getstat = status;
972 __stdcall static void
973 ndis_linksts_done(adapter)
976 ndis_miniport_block *block;
977 struct ndis_softc *sc;
981 ifp = block->nmb_ifp;
985 if (!NDIS_INITIALIZED(sc)) {
990 switch (block->nmb_getstat) {
991 case NDIS_STATUS_MEDIA_CONNECT:
992 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
993 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
995 case NDIS_STATUS_MEDIA_DISCONNECT:
997 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1011 struct ndis_softc *sc;
1016 ifp = &sc->arpcom.ac_if;
1018 irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
1020 FASTCALL1(hal_lower_irql, irql);
1021 mtx_lock(&sc->ndis_intrmtx);
1022 ndis_enable_intr(sc);
1023 mtx_unlock(&sc->ndis_intrmtx);
1032 struct ndis_softc *sc;
1034 int is_our_intr = 0;
1038 ifp = &sc->arpcom.ac_if;
1040 if (sc->ndis_block.nmb_miniportadapterctx == NULL)
1043 mtx_lock(&sc->ndis_intrmtx);
1044 if (sc->ndis_block.nmb_interrupt->ni_isrreq == TRUE)
1045 ndis_isr(sc, &is_our_intr, &call_isr);
1047 ndis_disable_intr(sc);
1050 mtx_unlock(&sc->ndis_intrmtx);
1052 if ((is_our_intr || call_isr))
1053 ndis_sched(ndis_intrtask, ifp, NDIS_SWI);
1062 struct ndis_softc *sc;
1068 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1069 sc->ndis_stat_ch = timeout(ndis_tick, sc, hz *
1070 sc->ndis_block.nmb_checkforhangsecs);
1081 struct ndis_softc *sc;
1082 __stdcall ndis_checkforhang_handler hangfunc;
1084 ndis_media_state linkstate;
1089 hangfunc = sc->ndis_chars.nmc_checkhang_func;
1091 if (hangfunc != NULL) {
1092 rval = hangfunc(sc->ndis_block.nmb_miniportadapterctx);
1099 len = sizeof(linkstate);
1100 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1101 (void *)&linkstate, &len);
1105 if (sc->ndis_link == 0 && linkstate == nmc_connected) {
1106 device_printf(sc->ndis_dev, "link up\n");
1110 ndis_getstate_80211(sc);
1112 #ifdef LINK_STATE_UP
1113 sc->arpcom.ac_if.if_link_state = LINK_STATE_UP;
1114 rt_ifmsg(&(sc->arpcom.ac_if));
1115 #endif /* LINK_STATE_UP */
1118 if (sc->ndis_link == 1 && linkstate == nmc_disconnected) {
1119 device_printf(sc->ndis_dev, "link down\n");
1121 #ifdef LINK_STATE_DOWN
1122 sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN;
1123 rt_ifmsg(&(sc->arpcom.ac_if));
1124 #endif /* LINK_STATE_DOWN */
1133 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1135 bus_dma_segment_t *segs;
1141 struct ndis_sc_list *sclist;
1144 if (error || arg == NULL)
1149 sclist->nsl_frags = nseg;
1151 for (i = 0; i < nseg; i++) {
1152 sclist->nsl_elements[i].nse_addr.np_quad = segs[i].ds_addr;
1153 sclist->nsl_elements[i].nse_len = segs[i].ds_len;
1166 #if __FreeBSD_version < 502114
1167 if (ifp->if_snd.ifq_head != NULL)
1169 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1176 * Main transmit routine. To make NDIS drivers happy, we need to
1177 * transform mbuf chains into NDIS packets and feed them to the
1178 * send packet routines. Most drivers allow you to send several
1179 * packets at once (up to the maxpkts limit). Unfortunately, rather
1180 * that accepting them in the form of a linked list, they expect
1181 * a contiguous array of pointers to packets.
1183 * For those drivers which use the NDIS scatter/gather DMA mechanism,
1184 * we need to perform busdma work here. Those that use map registers
1185 * will do the mapping themselves on a buffer by buffer basis.
1192 struct ndis_softc *sc;
1193 struct mbuf *m = NULL;
1194 ndis_packet **p0 = NULL, *p = NULL;
1195 ndis_tcpip_csum *csum;
1202 if (!sc->ndis_link || ifp->if_flags & IFF_OACTIVE) {
1207 p0 = &sc->ndis_txarray[sc->ndis_txidx];
1209 while(sc->ndis_txpending) {
1210 #if __FreeBSD_version < 502114
1211 IF_DEQUEUE(&ifp->if_snd, m);
1213 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1218 sc->ndis_txarray[sc->ndis_txidx] = NULL;
1220 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1221 #if __FreeBSD_version >= 502114
1222 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1225 #if __FreeBSD_version < 502114
1226 IF_PREPEND(&ifp->if_snd, m);
1232 * Save pointer to original mbuf
1233 * so we can free it later.
1236 p = sc->ndis_txarray[sc->ndis_txidx];
1237 p->np_txidx = sc->ndis_txidx;
1239 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1242 * Do scatter/gather processing, if driver requested it.
1245 bus_dmamap_load_mbuf(sc->ndis_ttag,
1246 sc->ndis_tmaps[sc->ndis_txidx], m,
1247 ndis_map_sclist, &p->np_sclist, BUS_DMA_NOWAIT);
1248 bus_dmamap_sync(sc->ndis_ttag,
1249 sc->ndis_tmaps[sc->ndis_txidx],
1250 BUS_DMASYNC_PREREAD);
1251 p->np_ext.npe_info[ndis_sclist_info] = &p->np_sclist;
1254 /* Handle checksum offload. */
1256 if (ifp->if_capenable & IFCAP_TXCSUM &&
1257 m->m_pkthdr.csum_flags) {
1258 csum = (ndis_tcpip_csum *)
1259 &p->np_ext.npe_info[ndis_tcpipcsum_info];
1260 csum->u.ntc_txflags = NDIS_TXCSUM_DO_IPV4;
1261 if (m->m_pkthdr.csum_flags & CSUM_IP)
1262 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_IP;
1263 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1264 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_TCP;
1265 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1266 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_UDP;
1267 p->np_private.npp_flags = NDIS_PROTOCOL_ID_TCP_IP;
1271 sc->ndis_txpending--;
1276 * If there's a BPF listener, bounce a copy of this frame
1283 * The array that p0 points to must appear contiguous,
1284 * so we must not wrap past the end of sc->ndis_txarray[].
1285 * If it looks like we're about to wrap, break out here
1286 * so the this batch of packets can be transmitted, then
1287 * wait for txeof to ask us to send the rest.
1290 if (sc->ndis_txidx == 0)
1299 if (sc->ndis_txpending == 0)
1300 ifp->if_flags |= IFF_OACTIVE;
1303 * Set a timeout in case the chip goes out to lunch.
1309 if (sc->ndis_maxpkts == 1)
1310 ndis_send_packet(sc, p);
1312 ndis_send_packets(sc, p0, pcnt);
1321 struct ndis_softc *sc = xsc;
1322 struct ifnet *ifp = &sc->arpcom.ac_if;
1326 * Avoid reintializing the link unnecessarily.
1327 * This should be dealt with in a better way by
1328 * fixing the upper layer modules so they don't
1329 * call ifp->if_init() quite as often.
1331 if (sc->ndis_link && sc->ndis_skip)
1335 * Cancel pending I/O and free all RX/TX buffers.
1338 if (ndis_init_nic(sc))
1341 /* Init our MAC address */
1343 /* Program the packet filter */
1345 sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1347 if (ifp->if_flags & IFF_BROADCAST)
1348 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1350 if (ifp->if_flags & IFF_PROMISC)
1351 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1353 i = sizeof(sc->ndis_filter);
1355 error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1356 &sc->ndis_filter, &i);
1359 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1362 * Program the multicast filter, if necessary.
1366 /* Setup task offload. */
1367 ndis_set_offload(sc);
1369 /* Enable interrupts. */
1370 ndis_enable_intr(sc);
1373 ndis_setstate_80211(sc);
1378 sc->ndis_txpending = sc->ndis_maxpkts;
1381 ifp->if_flags |= IFF_RUNNING;
1382 ifp->if_flags &= ~IFF_OACTIVE;
1387 * Some drivers don't set this value. The NDIS spec says
1388 * the default checkforhang timeout is "approximately 2
1389 * seconds." We use 3 seconds, because it seems for some
1390 * drivers, exactly 2 seconds is too fast.
1393 if (sc->ndis_block.nmb_checkforhangsecs == 0)
1394 sc->ndis_block.nmb_checkforhangsecs = 3;
1396 sc->ndis_stat_ch = timeout(ndis_tick, sc,
1397 hz * sc->ndis_block.nmb_checkforhangsecs);
1403 * Set media options.
1406 ndis_ifmedia_upd(ifp)
1409 struct ndis_softc *sc;
1413 if (NDIS_INITIALIZED(sc))
1420 * Report current media status.
1423 ndis_ifmedia_sts(ifp, ifmr)
1425 struct ifmediareq *ifmr;
1427 struct ndis_softc *sc;
1428 uint32_t media_info;
1429 ndis_media_state linkstate;
1432 ifmr->ifm_status = IFM_AVALID;
1433 ifmr->ifm_active = IFM_ETHER;
1436 if (!NDIS_INITIALIZED(sc))
1439 len = sizeof(linkstate);
1440 error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1441 (void *)&linkstate, &len);
1443 len = sizeof(media_info);
1444 error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
1445 (void *)&media_info, &len);
1447 if (linkstate == nmc_connected)
1448 ifmr->ifm_status |= IFM_ACTIVE;
1450 switch(media_info) {
1452 ifmr->ifm_active |= IFM_10_T;
1455 ifmr->ifm_active |= IFM_100_TX;
1458 ifmr->ifm_active |= IFM_1000_T;
1461 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
1469 ndis_setstate_80211(sc)
1470 struct ndis_softc *sc;
1472 struct ieee80211com *ic;
1473 ndis_80211_ssid ssid;
1474 ndis_80211_config config;
1476 int i, rval = 0, len;
1481 ifp = &sc->ic.ic_ac.ac_if;
1483 if (!NDIS_INITIALIZED(sc))
1486 /* Set network infrastructure mode. */
1489 if (ic->ic_opmode == IEEE80211_M_IBSS)
1490 arg = NDIS_80211_NET_INFRA_IBSS;
1492 arg = NDIS_80211_NET_INFRA_BSS;
1494 rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
1497 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
1501 #ifdef IEEE80211_F_WEPON
1502 if (ic->ic_flags & IEEE80211_F_WEPON) {
1504 if (ic->ic_wep_mode >= IEEE80211_WEP_ON) {
1506 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1507 if (ic->ic_nw_keys[i].wk_len) {
1508 bzero((char *)&wep, sizeof(wep));
1509 wep.nw_keylen = ic->ic_nw_keys[i].wk_len;
1511 /* 5 and 13 are the only valid key lengths */
1512 if (ic->ic_nw_keys[i].wk_len < 5)
1514 else if (ic->ic_nw_keys[i].wk_len > 5 &&
1515 ic->ic_nw_keys[i].wk_len < 13)
1519 wep.nw_length = (sizeof(uint32_t) * 3)
1521 if (i == ic->ic_wep_txkey)
1522 wep.nw_keyidx |= NDIS_80211_WEPKEY_TX;
1523 bcopy(ic->ic_nw_keys[i].wk_key,
1524 wep.nw_keydata, wep.nw_length);
1526 rval = ndis_set_info(sc,
1527 OID_802_11_ADD_WEP, &wep, &len);
1529 device_printf(sc->ndis_dev,
1530 "set wepkey failed: %d\n", rval);
1533 arg = NDIS_80211_WEPSTAT_ENABLED;
1535 rval = ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1537 device_printf(sc->ndis_dev,
1538 "enable WEP failed: %d\n", rval);
1539 #ifndef IEEE80211_F_WEPON
1540 if (ic->ic_wep_mode != IEEE80211_WEP_8021X &&
1541 ic->ic_wep_mode != IEEE80211_WEP_ON)
1542 arg = NDIS_80211_PRIVFILT_ACCEPTALL;
1545 arg = NDIS_80211_PRIVFILT_8021XWEP;
1547 rval = ndis_set_info(sc, OID_802_11_PRIVACY_FILTER, &arg, &len);
1548 #ifdef IEEE80211_WEP_8021X /*IEEE80211_F_WEPON*/
1549 /* Accept that we only have "shared" and 802.1x modes. */
1551 if (arg == NDIS_80211_PRIVFILT_ACCEPTALL)
1552 ic->ic_wep_mode = IEEE80211_WEP_MIXED;
1554 ic->ic_wep_mode = IEEE80211_WEP_8021X;
1557 arg = NDIS_80211_AUTHMODE_OPEN;
1559 arg = NDIS_80211_WEPSTAT_DISABLED;
1561 ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1562 arg = NDIS_80211_AUTHMODE_OPEN;
1566 rval = ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
1570 device_printf (sc->ndis_dev, "set auth failed: %d\n", rval);
1574 /* Set network type. */
1578 switch (ic->ic_curmode) {
1579 case IEEE80211_MODE_11A:
1580 arg = NDIS_80211_NETTYPE_11OFDM5;
1582 case IEEE80211_MODE_11B:
1583 arg = NDIS_80211_NETTYPE_11DS;
1585 case IEEE80211_MODE_11G:
1586 arg = NDIS_80211_NETTYPE_11OFDM24;
1589 device_printf(sc->ndis_dev, "unknown mode: %d\n",
1595 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
1598 device_printf (sc->ndis_dev,
1599 "set nettype failed: %d\n", rval);
1603 len = sizeof(config);
1604 bzero((char *)&config, len);
1605 config.nc_length = len;
1606 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1607 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1610 * Some drivers expect us to initialize these values, so
1611 * provide some defaults.
1613 if (config.nc_beaconperiod == 0)
1614 config.nc_beaconperiod = 100;
1615 if (config.nc_atimwin == 0)
1616 config.nc_atimwin = 100;
1617 if (config.nc_fhconfig.ncf_dwelltime == 0)
1618 config.nc_fhconfig.ncf_dwelltime = 200;
1620 if (rval == 0 && ic->ic_ibss_chan != IEEE80211_CHAN_ANYC) {
1623 chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
1624 chanflag = config.nc_dsconfig > 2500000 ? IEEE80211_CHAN_2GHZ :
1625 IEEE80211_CHAN_5GHZ;
1626 if (chan != ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0)) {
1627 config.nc_dsconfig =
1628 ic->ic_ibss_chan->ic_freq * 1000;
1629 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1630 len = sizeof(config);
1631 config.nc_length = len;
1632 config.nc_fhconfig.ncf_length =
1633 sizeof(ndis_80211_config_fh);
1634 rval = ndis_set_info(sc, OID_802_11_CONFIGURATION,
1637 device_printf(sc->ndis_dev, "couldn't change "
1638 "DS config to %ukHz: %d\n",
1639 config.nc_dsconfig, rval);
1642 device_printf(sc->ndis_dev, "couldn't retrieve "
1643 "channel info: %d\n", rval);
1645 /* Set SSID -- always do this last. */
1648 bzero((char *)&ssid, len);
1649 ssid.ns_ssidlen = ic->ic_des_esslen;
1650 if (ssid.ns_ssidlen == 0) {
1651 ssid.ns_ssidlen = 1;
1653 bcopy(ic->ic_des_essid, ssid.ns_ssid, ssid.ns_ssidlen);
1654 rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
1657 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
1663 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1665 struct ieee80211com *ic = (void *)ifp;
1666 struct ieee80211_node *ni = NULL;
1668 imr->ifm_status = IFM_AVALID;
1669 imr->ifm_active = IFM_IEEE80211;
1670 if (ic->ic_state == IEEE80211_S_RUN)
1671 imr->ifm_status |= IFM_ACTIVE;
1672 imr->ifm_active |= IFM_AUTO;
1673 switch (ic->ic_opmode) {
1674 case IEEE80211_M_STA:
1676 /* calculate rate subtype */
1677 imr->ifm_active |= ieee80211_rate2media(ic,
1678 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1680 case IEEE80211_M_IBSS:
1682 /* calculate rate subtype */
1683 imr->ifm_active |= ieee80211_rate2media(ic,
1684 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1685 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1687 case IEEE80211_M_AHDEMO:
1688 /* should not come here */
1690 case IEEE80211_M_HOSTAP:
1691 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1693 case IEEE80211_M_MONITOR:
1694 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1697 switch (ic->ic_curmode) {
1698 case IEEE80211_MODE_11A:
1699 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
1701 case IEEE80211_MODE_11B:
1702 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
1704 case IEEE80211_MODE_11G:
1705 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
1707 case IEEE80211_MODE_TURBO:
1708 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
1709 | IFM_IEEE80211_TURBO;
1715 ndis_get_assoc(sc, assoc)
1716 struct ndis_softc *sc;
1717 ndis_wlan_bssid_ex **assoc;
1719 ndis_80211_bssid_list_ex *bl;
1720 ndis_wlan_bssid_ex *bs;
1721 ndis_80211_macaddr bssid;
1727 len = sizeof(bssid);
1728 error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
1730 device_printf(sc->ndis_dev, "failed to get bssid\n");
1734 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1735 if (error != ENOSPC) {
1736 device_printf(sc->ndis_dev, "bssid_list failed\n");
1740 bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
1741 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1744 device_printf(sc->ndis_dev, "bssid_list failed\n");
1748 bs = (ndis_wlan_bssid_ex *)&bl->nblx_bssid[0];
1749 for (i = 0; i < bl->nblx_items; i++) {
1750 if (bcmp(bs->nwbx_macaddr, bssid, sizeof(bssid)) == 0) {
1751 *assoc = malloc(bs->nwbx_len, M_TEMP, M_NOWAIT);
1752 if (*assoc == NULL) {
1756 bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
1760 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
1768 ndis_getstate_80211(sc)
1769 struct ndis_softc *sc;
1771 struct ieee80211com *ic;
1772 ndis_80211_ssid ssid;
1773 ndis_80211_config config;
1774 ndis_wlan_bssid_ex *bs;
1775 int rval, len, i = 0;
1780 ifp = &sc->ic.ic_ac.ac_if;
1782 if (!NDIS_INITIALIZED(sc))
1786 ic->ic_state = IEEE80211_S_RUN;
1788 ic->ic_state = IEEE80211_S_ASSOC;
1792 * If we're associated, retrieve info on the current bssid.
1794 if ((rval = ndis_get_assoc(sc, &bs)) == 0) {
1795 switch(bs->nwbx_nettype) {
1796 case NDIS_80211_NETTYPE_11FH:
1797 case NDIS_80211_NETTYPE_11DS:
1798 ic->ic_curmode = IEEE80211_MODE_11B;
1800 case NDIS_80211_NETTYPE_11OFDM5:
1801 ic->ic_curmode = IEEE80211_MODE_11A;
1803 case NDIS_80211_NETTYPE_11OFDM24:
1804 ic->ic_curmode = IEEE80211_MODE_11G;
1807 device_printf(sc->ndis_dev,
1808 "unknown nettype %d\n", arg);
1816 bzero((char *)&ssid, len);
1817 rval = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
1820 device_printf (sc->ndis_dev, "get ssid failed: %d\n", rval);
1821 bcopy(ssid.ns_ssid, ic->ic_bss->ni_essid, ssid.ns_ssidlen);
1822 ic->ic_bss->ni_esslen = ssid.ns_ssidlen;
1825 rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
1827 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
1830 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
1831 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
1832 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1833 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1834 IEEE80211_RATE_VAL) == arg / 5000)
1839 if (i == ic->ic_bss->ni_rates.rs_nrates &&
1840 ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
1841 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11G];
1842 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1843 if ((ic->ic_bss->ni_rates.rs_rates[i] &
1844 IEEE80211_RATE_VAL) == arg / 5000)
1849 if (i == ic->ic_bss->ni_rates.rs_nrates)
1850 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
1853 ic->ic_bss->ni_txrate = i;
1855 if (ic->ic_caps & IEEE80211_C_PMGT) {
1857 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
1860 device_printf(sc->ndis_dev,
1861 "get power mode failed: %d\n", rval);
1862 if (arg == NDIS_80211_POWERMODE_CAM)
1863 ic->ic_flags &= ~IEEE80211_F_PMGTON;
1865 ic->ic_flags |= IEEE80211_F_PMGTON;
1868 len = sizeof(config);
1869 bzero((char *)&config, len);
1870 config.nc_length = len;
1871 config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1872 rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);
1876 chan = ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0);
1877 if (chan < 0 || chan >= IEEE80211_CHAN_MAX) {
1878 if (ifp->if_flags & IFF_DEBUG)
1879 device_printf(sc->ndis_dev, "current channel "
1880 "(%uMHz) out of bounds\n",
1881 config.nc_dsconfig / 1000);
1882 ic->ic_bss->ni_chan = &ic->ic_channels[1];
1884 ic->ic_bss->ni_chan = &ic->ic_channels[chan];
1886 device_printf(sc->ndis_dev, "couldn't retrieve "
1887 "channel info: %d\n", rval);
1891 rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1894 device_printf (sc->ndis_dev,
1895 "get wep status failed: %d\n", rval);
1897 if (arg == NDIS_80211_WEPSTAT_ENABLED)
1898 ic->ic_flags |= IEEE80211_F_WEPON;
1900 ic->ic_flags &= ~IEEE80211_F_WEPON;
1906 ndis_ioctl(ifp, command, data)
1911 struct ndis_softc *sc = ifp->if_softc;
1912 struct ifreq *ifr = (struct ifreq *) data;
1919 if (ifp->if_flags & IFF_UP) {
1920 if (ifp->if_flags & IFF_RUNNING &&
1921 ifp->if_flags & IFF_PROMISC &&
1922 !(sc->ndis_if_flags & IFF_PROMISC)) {
1924 NDIS_PACKET_TYPE_PROMISCUOUS;
1925 i = sizeof(sc->ndis_filter);
1926 error = ndis_set_info(sc,
1927 OID_GEN_CURRENT_PACKET_FILTER,
1928 &sc->ndis_filter, &i);
1929 } else if (ifp->if_flags & IFF_RUNNING &&
1930 !(ifp->if_flags & IFF_PROMISC) &&
1931 sc->ndis_if_flags & IFF_PROMISC) {
1933 ~NDIS_PACKET_TYPE_PROMISCUOUS;
1934 i = sizeof(sc->ndis_filter);
1935 error = ndis_set_info(sc,
1936 OID_GEN_CURRENT_PACKET_FILTER,
1937 &sc->ndis_filter, &i);
1941 if (ifp->if_flags & IFF_RUNNING)
1944 sc->ndis_if_flags = ifp->if_flags;
1954 if (sc->ndis_80211) {
1955 error = ieee80211_ioctl(ifp, command, data);
1956 if (error == ENETRESET) {
1957 ndis_setstate_80211(sc);
1962 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1965 ifp->if_capenable = ifr->ifr_reqcap;
1966 if (ifp->if_capenable & IFCAP_TXCSUM)
1967 ifp->if_hwassist = sc->ndis_hwassist;
1969 ifp->if_hwassist = 0;
1970 ndis_set_offload(sc);
1972 case SIOCGIFGENERIC:
1973 case SIOCSIFGENERIC:
1974 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
1975 if (command == SIOCGIFGENERIC)
1976 error = ndis_wi_ioctl_get(ifp, command, data);
1978 error = ndis_wi_ioctl_set(ifp, command, data);
1981 if (error != ENOTTY)
1985 if (sc->ndis_80211) {
1986 error = ieee80211_ioctl(ifp, command, data);
1987 if (error == ENETRESET) {
1988 ndis_setstate_80211(sc);
1992 error = ether_ioctl(ifp, command, data);
1997 /*NDIS_UNLOCK(sc);*/
2003 ndis_wi_ioctl_get(ifp, command, data)
2010 struct ndis_softc *sc;
2011 ndis_80211_bssid_list_ex *bl;
2012 ndis_wlan_bssid_ex *wb;
2013 struct wi_apinfo *api;
2014 int error, i, j, len, maxaps;
2017 ifr = (struct ifreq *)data;
2018 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2022 switch (wreq.wi_type) {
2023 case WI_RID_READ_APS:
2025 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
2028 tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
2030 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
2031 if (error != ENOSPC)
2033 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
2034 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
2039 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
2040 maxaps = MIN(maxaps, bl->nblx_items);
2041 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
2042 *(int *)&wreq.wi_val = maxaps;
2043 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
2044 wb = bl->nblx_bssid;
2046 bzero(api, sizeof(*api));
2047 bcopy(&wb->nwbx_macaddr, &api->bssid,
2048 sizeof(api->bssid));
2049 api->namelen = wb->nwbx_ssid.ns_ssidlen;
2050 bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
2051 if (wb->nwbx_privacy)
2052 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
2053 /* XXX Where can we get noise information? */
2054 api->signal = wb->nwbx_rssi + 149; /* XXX */
2055 api->quality = api->signal;
2057 ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2059 /* In "auto" infrastructure mode, this is useless. */
2060 if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
2061 api->capinfo |= IEEE80211_CAPINFO_IBSS;
2062 if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
2063 j = sizeof(ndis_80211_rates_ex);
2064 /* handle other extended things */
2066 j = sizeof(ndis_80211_rates);
2067 for (i = api->rate = 0; i < j; i++)
2068 api->rate = MAX(api->rate, 5 *
2069 (wb->nwbx_supportedrates[i] & 0x7f));
2071 wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2074 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2084 ndis_wi_ioctl_set(ifp, command, data)
2091 struct ndis_softc *sc;
2095 error = suser(curthread);
2100 ifr = (struct ifreq *)data;
2101 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2105 switch (wreq.wi_type) {
2106 case WI_RID_SCAN_APS:
2107 case WI_RID_SCAN_REQ: /* arguments ignored */
2110 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2124 struct ndis_softc *sc;
2130 device_printf(sc->ndis_dev, "watchdog timeout\n");
2133 ndis_sched((void(*)(void *))ndis_reset_nic, sc, NDIS_TASKQUEUE);
2134 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2140 * Stop the adapter and free any mbufs allocated to the
2145 struct ndis_softc *sc;
2149 ifp = &sc->arpcom.ac_if;
2150 untimeout(ndis_tick, sc, sc->ndis_stat_ch);
2157 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2164 * Stop all chip I/O so that the kernel's probe routines don't
2165 * get confused by errant DMAs when rebooting.
2171 struct ndis_softc *sc;
2173 sc = device_get_softc(dev);
2174 ndis_shutdown_nic(sc);