Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / netif / ndis / if_ndis.c
1 /*
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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.
31  *
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.4 2005/02/19 00:55:44 joerg Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <sys/proc.h>
45 #if __FreeBSD_version < 502113
46 #include <sys/sysctl.h>
47 #endif
48
49 #include <net/if.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>
56
57 #include <net/bpf.h>
58
59 #include <machine/bus_memio.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 #include <sys/bus.h>
64 #include <sys/rman.h>
65
66 #include <netproto/802_11/ieee80211_var.h>
67 #include <netproto/802_11/ieee80211_ioctl.h>
68
69 #include <netproto/802_11/if_wavelan_ieee.h>
70
71 #include <bus/pci/pcireg.h>
72 #include <bus/pci/pcivar.h>
73
74 #include <sys/thread2.h>
75
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"
84
85 #define NDIS_IMAGE
86 #define NDIS_REGVALS
87
88 #include "ndis_driver_data.h"
89
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);
95
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);
103
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 *);
124
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);
128
129 /*
130  * Program the 64-bit multicast hash filter.
131  */
132 static void
133 ndis_setmulti(sc)
134         struct ndis_softc       *sc;
135 {
136         struct ifnet            *ifp;
137         struct ifmultiaddr      *ifma;
138         int                     len, mclistsz, error;
139         uint8_t                 *mclist;
140
141         ifp = &sc->arpcom.ac_if;
142
143         if (!NDIS_INITIALIZED(sc))
144                 return;
145
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);
151                 if (error)
152                         device_printf (sc->ndis_dev,
153                             "set filter failed: %d\n", error);
154                 return;
155         }
156
157         if (LIST_EMPTY(&ifp->if_multiaddrs))
158                 return;
159
160         len = sizeof(mclistsz);
161         ndis_get_info(sc, OID_802_3_MAXIMUM_LIST_SIZE, &mclistsz, &len);
162
163         mclist = malloc(ETHER_ADDR_LEN * mclistsz, M_TEMP, M_NOWAIT|M_ZERO);
164
165         if (mclist == NULL) {
166                 sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
167                 goto out;
168         }
169
170         sc->ndis_filter |= NDIS_PACKET_TYPE_MULTICAST;
171
172         len = 0;
173         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
174                 if (ifma->ifma_addr->sa_family != AF_LINK)
175                         continue;
176                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
177                     mclist + (ETHER_ADDR_LEN * len), ETHER_ADDR_LEN);
178                 len++;
179                 if (len > mclistsz) {
180                         sc->ndis_filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
181                         sc->ndis_filter &= ~NDIS_PACKET_TYPE_MULTICAST;
182                         goto out;
183                 }
184         }
185
186         len = len * ETHER_ADDR_LEN;
187         error = ndis_set_info(sc, OID_802_3_MULTICAST_LIST, mclist, &len);
188         if (error) {
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;
192         }
193
194 out:
195         free(mclist, M_TEMP);
196
197         len = sizeof(sc->ndis_filter);
198         error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
199             &sc->ndis_filter, &len);
200         if (error)
201                 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
202
203         return;
204 }
205
206 static int
207 ndis_set_offload(sc)
208         struct ndis_softc       *sc;
209 {
210         ndis_task_offload       *nto;
211         ndis_task_offload_hdr   *ntoh;
212         ndis_task_tcpip_csum    *nttc;
213         struct ifnet            *ifp;
214         int                     len, error;
215
216         ifp = &sc->arpcom.ac_if;
217
218         if (!NDIS_INITIALIZED(sc))
219                 return(EINVAL);
220
221         /* See if there's anything to set. */
222
223         error = ndis_probe_offload(sc);
224         if (error)
225                 return(error);
226                 
227         if (sc->ndis_hwassist == 0 && ifp->if_capabilities == 0)
228                 return(0);
229
230         len = sizeof(ndis_task_offload_hdr) + sizeof(ndis_task_offload) +
231             sizeof(ndis_task_tcpip_csum);
232
233         ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
234
235         if (ntoh == NULL)
236                 return(ENOMEM);
237
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;
244
245         nto = (ndis_task_offload *)((char *)ntoh +
246             ntoh->ntoh_offset_firsttask);
247
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);
253
254         nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
255
256         if (ifp->if_capenable & IFCAP_TXCSUM)
257                 nttc->nttc_v4tx = sc->ndis_v4tx;
258
259         if (ifp->if_capenable & IFCAP_RXCSUM)
260                 nttc->nttc_v4rx = sc->ndis_v4rx;
261
262         error = ndis_set_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
263         free(ntoh, M_TEMP);
264
265         return(error);
266 }
267
268 static int
269 ndis_probe_offload(sc)
270         struct ndis_softc       *sc;
271 {
272         ndis_task_offload       *nto;
273         ndis_task_offload_hdr   *ntoh;
274         ndis_task_tcpip_csum    *nttc = NULL;
275         struct ifnet            *ifp;
276         int                     len, error, dummy;
277
278         ifp = &sc->arpcom.ac_if;
279
280         len = sizeof(dummy);
281         error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, &dummy, &len);
282
283         if (error != ENOSPC)
284                 return(error);
285
286         ntoh = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
287
288         if (ntoh == NULL)
289                 return(ENOMEM);
290
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;
296
297         error = ndis_get_info(sc, OID_TCP_TASK_OFFLOAD, ntoh, &len);
298
299         if (error) {
300                 free(ntoh, M_TEMP);
301                 return(error);
302         }
303
304         if (ntoh->ntoh_vers != NDIS_TASK_OFFLOAD_VERSION) {
305                 free(ntoh, M_TEMP);
306                 return(EINVAL);
307         }
308
309         nto = (ndis_task_offload *)((char *)ntoh +
310             ntoh->ntoh_offset_firsttask);
311
312         while (1) {
313                 switch (nto->nto_task) {
314                 case NDIS_TASK_TCPIP_CSUM:
315                         nttc = (ndis_task_tcpip_csum *)nto->nto_taskbuf;
316                         break;
317                 /* Don't handle these yet. */
318                 case NDIS_TASK_IPSEC:
319                 case NDIS_TASK_TCP_LARGESEND:
320                 default:
321                         break;
322                 }
323                 if (nto->nto_offset_nexttask == 0)
324                         break;
325                 nto = (ndis_task_offload *)((char *)nto +
326                     nto->nto_offset_nexttask);
327         }
328
329         if (nttc == NULL) {
330                 free(ntoh, M_TEMP);
331                 return(ENOENT);
332         }
333
334         sc->ndis_v4tx = nttc->nttc_v4tx;
335         sc->ndis_v4rx = nttc->nttc_v4rx;
336
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;
343
344         if (sc->ndis_hwassist)
345                 ifp->if_capabilities |= IFCAP_TXCSUM;
346
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;
353
354         free(ntoh, M_TEMP);
355         return(0);
356 }
357
358 /*
359  * Attach the interface. Allocate softc structures, do ifmedia
360  * setup and ethernet/BPF attach.
361  */
362 int
363 ndis_attach(dev)
364         device_t                dev;
365 {
366         u_char                  eaddr[ETHER_ADDR_LEN];
367         struct ndis_softc       *sc;
368         struct ifnet            *ifp = NULL;
369         void                    *img;
370         int                     error = 0, len;
371         int                     i;
372
373         sc = device_get_softc(dev);
374
375         callout_init(&sc->ndis_stat_timer);
376         NDIS_LOCK_INIT(&sc->ndis_lock);
377         NDIS_LOCK_INIT(&sc->ndis_intrlock);
378
379         /*
380          * Hook interrupt early, since calling the driver's
381          * init routine may trigger an interrupt.
382          */
383
384         error = bus_setup_intr(dev, sc->ndis_irq, INTR_TYPE_NET | INTR_MPSAFE,
385             ndis_intr, sc, &sc->ndis_intrhand);
386
387         if (error) {
388                 device_printf(dev, "couldn't set up irq\n");
389                 goto fail;
390         }
391
392         sc->ndis_regvals = ndis_regvals;
393
394 #if __FreeBSD_version < 502113
395         sysctl_ctx_init(&sc->ndis_ctx);
396
397 #endif
398         /* Create sysctl registry nodes */
399         ndis_create_sysctls(sc);
400
401         /* Set up driver image in memory. */
402         img = drv_data;
403         ndis_load_driver((vm_offset_t)img, sc);
404
405         /* Tell the user what version of the API the driver is using. */
406         device_printf(dev, "NDIS API version: %d.%d\n",
407             sc->ndis_chars.nmc_version_major,
408             sc->ndis_chars.nmc_version_minor);
409
410         /* Do resource conversion. */
411         ndis_convert_res(sc);
412
413         /* Install our RX and TX interrupt handlers. */
414         sc->ndis_block.nmb_senddone_func = ndis_txeof;
415         sc->ndis_block.nmb_pktind_func = ndis_rxeof;
416
417         /* Call driver's init routine. */
418         if (ndis_init_nic(sc)) {
419                 device_printf (dev, "init handler failed\n");
420                 error = ENXIO;
421                 goto fail;
422         }
423
424         /*
425          * Get station address from the driver.
426          */
427         len = sizeof(eaddr);
428         ndis_get_info(sc, OID_802_3_CURRENT_ADDRESS, &eaddr, &len);
429
430         bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
431
432         /*
433          * Figure out of we're allowed to use multipacket sends
434          * with this driver, and if so, how many.
435          */
436
437         if (sc->ndis_chars.nmc_sendsingle_func &&
438             sc->ndis_chars.nmc_sendmulti_func == NULL) {
439                 sc->ndis_maxpkts = 1;
440         } else {
441                 len = sizeof(sc->ndis_maxpkts);
442                 ndis_get_info(sc, OID_GEN_MAXIMUM_SEND_PACKETS,
443                     &sc->ndis_maxpkts, &len);
444         }
445
446         sc->ndis_txarray = malloc(sizeof(ndis_packet *) *
447             sc->ndis_maxpkts, M_DEVBUF, M_NOWAIT|M_ZERO);
448
449         sc->ndis_txpending = sc->ndis_maxpkts;
450
451         sc->ndis_oidcnt = 0;
452         /* Get supported oid list. */
453         ndis_get_supported_oids(sc, &sc->ndis_oids, &sc->ndis_oidcnt);
454
455         /* If the NDIS module requested scatter/gather, init maps. */
456         if (sc->ndis_sc)
457                 ndis_init_dma(sc);
458
459         /*
460          * See if the OID_802_11_CONFIGURATION OID is
461          * supported by this driver. If it is, then this an 802.11
462          * wireless driver, and we should set up media for wireless.
463          */
464         for (i = 0; i < sc->ndis_oidcnt; i++) {
465                 if (sc->ndis_oids[i] == OID_802_11_CONFIGURATION) {
466                         sc->ndis_80211++;
467                         break;
468                 }
469         }
470
471         /* Check for task offload support. */
472         ndis_probe_offload(sc);
473
474         ifp = &sc->arpcom.ac_if;
475         ifp->if_softc = sc;
476         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
477         ifp->if_mtu = ETHERMTU;
478         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
479         ifp->if_ioctl = ndis_ioctl;
480         ifp->if_start = ndis_start;
481         ifp->if_watchdog = ndis_watchdog;
482         ifp->if_init = ndis_init;
483         ifp->if_baudrate = 10000000;
484         ifq_set_maxlen(&ifp->if_snd, 50);
485         ifq_set_ready(&ifp->if_snd);
486         ifp->if_capenable = ifp->if_capabilities;
487         ifp->if_hwassist = sc->ndis_hwassist;
488
489         /* Do media setup */
490         if (sc->ndis_80211) {
491                 struct ieee80211com     *ic = (void *)ifp;
492                 ndis_80211_rates_ex     rates;
493                 struct ndis_80211_nettype_list *ntl;
494                 uint32_t                arg;
495                 int                     r;
496
497                 ic->ic_phytype = IEEE80211_T_DS;
498                 ic->ic_opmode = IEEE80211_M_STA;
499                 ic->ic_caps = IEEE80211_C_IBSS;
500                 ic->ic_state = IEEE80211_S_ASSOC;
501                 ic->ic_modecaps = (1<<IEEE80211_MODE_AUTO);
502                 len = 0;
503                 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
504                     NULL, &len);
505                 if (r != ENOSPC)
506                         goto nonettypes;
507                 ntl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
508                 r = ndis_get_info(sc, OID_802_11_NETWORK_TYPES_SUPPORTED,
509                     ntl, &len);
510                 if (r != 0) {
511                         free(ntl, M_DEVBUF);
512                         goto nonettypes;
513                 }
514
515                 for (i = 0; i < ntl->ntl_items; i++) {
516                         switch (ntl->ntl_type[i]) {
517                         case NDIS_80211_NETTYPE_11FH:
518                         case NDIS_80211_NETTYPE_11DS:
519                                 ic->ic_modecaps |= (1<<IEEE80211_MODE_11B);
520                                 break;
521                         case NDIS_80211_NETTYPE_11OFDM5:
522                                 ic->ic_modecaps |= (1<<IEEE80211_MODE_11A);
523                                 break;
524                         case NDIS_80211_NETTYPE_11OFDM24:
525                                 ic->ic_modecaps |= (1<<IEEE80211_MODE_11G);
526                                 break;
527                         default:
528                                 break;
529                         }
530                 }
531                 free(ntl, M_DEVBUF);
532 nonettypes:
533                 len = sizeof(rates);
534                 bzero((char *)&rates, len);
535                 r = ndis_get_info(sc, OID_802_11_SUPPORTED_RATES,
536                     (void *)rates, &len);
537                 if (r)
538                         device_printf (dev, "get rates failed: 0x%x\n", r);
539                 /*
540                  * Since the supported rates only up to 8 can be supported,
541                  * if this is not 802.11b we're just going to be faking it
542                  * all up to heck.
543                  */
544
545 #define TESTSETRATE(x, y)                                               \
546         do {                                                            \
547                 int                     i;                              \
548                 for (i = 0; i < ic->ic_sup_rates[x].rs_nrates; i++) {   \
549                         if (ic->ic_sup_rates[x].rs_rates[i] == (y))     \
550                                 break;                                  \
551                 }                                                       \
552                 if (i == ic->ic_sup_rates[x].rs_nrates) {               \
553                         ic->ic_sup_rates[x].rs_rates[i] = (y);          \
554                         ic->ic_sup_rates[x].rs_nrates++;                \
555                 }                                                       \
556         } while (0)
557
558 #define SETRATE(x, y)   \
559         ic->ic_sup_rates[x].rs_rates[ic->ic_sup_rates[x].rs_nrates] = (y)
560 #define INCRATE(x)      \
561         ic->ic_sup_rates[x].rs_nrates++
562
563                 ic->ic_curmode = IEEE80211_MODE_AUTO;
564                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A))
565                         ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates = 0;
566                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B))
567                         ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = 0;
568                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G))
569                         ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates = 0;
570                 for (i = 0; i < len; i++) {
571                         switch (rates[i] & IEEE80211_RATE_VAL) {
572                         case 2:
573                         case 4:
574                         case 11:
575                         case 10:
576                         case 22:
577                                 if (!(ic->ic_modecaps &
578                                     (1<<IEEE80211_MODE_11B))) {
579                                         /* Lazy-init 802.11b. */
580                                         ic->ic_modecaps |=
581                                             (1<<IEEE80211_MODE_11B);
582                                         ic->ic_sup_rates[IEEE80211_MODE_11B].
583                                             rs_nrates = 0;
584                                 }
585                                 SETRATE(IEEE80211_MODE_11B, rates[i]);
586                                 INCRATE(IEEE80211_MODE_11B);
587                                 break;
588                         default:
589                                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
590                                         SETRATE(IEEE80211_MODE_11A, rates[i]);
591                                         INCRATE(IEEE80211_MODE_11A);
592                                 }
593                                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
594                                         SETRATE(IEEE80211_MODE_11G, rates[i]);
595                                         INCRATE(IEEE80211_MODE_11G);
596                                 }
597                                 break;
598                         }
599                 }
600
601                 /*
602                  * If the hardware supports 802.11g, it most
603                  * likely supports 802.11b and all of the
604                  * 802.11b and 802.11g speeds, so maybe we can
605                  * just cheat here.  Just how in the heck do
606                  * we detect turbo modes, though?
607                  */
608                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
609                         TESTSETRATE(IEEE80211_MODE_11B,
610                             IEEE80211_RATE_BASIC|2);
611                         TESTSETRATE(IEEE80211_MODE_11B,
612                             IEEE80211_RATE_BASIC|4);
613                         TESTSETRATE(IEEE80211_MODE_11B,
614                             IEEE80211_RATE_BASIC|11);
615                         TESTSETRATE(IEEE80211_MODE_11B,
616                             IEEE80211_RATE_BASIC|22);
617                 }
618                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
619                         TESTSETRATE(IEEE80211_MODE_11G, 47);
620                         TESTSETRATE(IEEE80211_MODE_11G, 72);
621                         TESTSETRATE(IEEE80211_MODE_11G, 96);
622                         TESTSETRATE(IEEE80211_MODE_11G, 108);
623                 }
624                 if (ic->ic_modecaps & (1<<IEEE80211_MODE_11A)) {
625                         TESTSETRATE(IEEE80211_MODE_11A, 47);
626                         TESTSETRATE(IEEE80211_MODE_11A, 72);
627                         TESTSETRATE(IEEE80211_MODE_11A, 96);
628                         TESTSETRATE(IEEE80211_MODE_11A, 108);
629                 }
630 #undef SETRATE
631 #undef INCRATE
632                 /*
633                  * Taking yet more guesses here.
634                  */
635                 for (i = 1; i < IEEE80211_CHAN_MAX; i++) {
636                         int chanflag = 0;
637
638                         if (ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates)
639                                 chanflag |= IEEE80211_CHAN_G;
640                         if (i <= 14)
641                                 chanflag |= IEEE80211_CHAN_B;
642                         if (ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates &&
643                             i > 14)
644                                 chanflag = IEEE80211_CHAN_A;
645                         if (chanflag == 0)
646                                 break;
647                         ic->ic_channels[i].ic_freq =
648                             ieee80211_ieee2mhz(i, chanflag);
649                         ic->ic_channels[i].ic_flags = chanflag;
650                 }
651
652                 i = sizeof(arg);
653                 r = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &i);
654                 if (arg != NDIS_80211_WEPSTAT_NOTSUPPORTED)
655                         ic->ic_caps |= IEEE80211_C_WEP;
656                 i = sizeof(arg);
657                 r = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &i);
658                 if (r == 0)
659                         ic->ic_caps |= IEEE80211_C_PMGT;
660                 bcopy(eaddr, &ic->ic_myaddr, sizeof(eaddr));
661                 ieee80211_ifattach(ifp);
662                 ieee80211_media_init(ifp, ieee80211_media_change,
663                     ndis_media_status);
664                 ic->ic_ibss_chan = IEEE80211_CHAN_ANYC;
665                 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
666         } else {
667                 ifmedia_init(&sc->ifmedia, IFM_IMASK, ndis_ifmedia_upd,
668                     ndis_ifmedia_sts);
669                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
670                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
671                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
672                 ifmedia_add(&sc->ifmedia,
673                     IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
674                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
675                 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
676                 ether_ifattach(ifp, eaddr);
677         }
678
679         /* Override the status handler so we can detect link changes. */
680         sc->ndis_block.nmb_status_func = ndis_linksts;
681         sc->ndis_block.nmb_statusdone_func = ndis_linksts_done;
682 fail:
683         if (error)
684                 ndis_detach(dev);
685         else
686                 /* We're done talking to the NIC for now; halt it. */
687                 ndis_halt_nic(sc);
688
689         return(error);
690 }
691
692 /*
693  * Shutdown hardware and free up resources. This can be called any
694  * time after the mutex has been initialized. It is called in both
695  * the error case in attach and the normal detach case so it needs
696  * to be careful about only freeing resources that have actually been
697  * allocated.
698  */
699 int
700 ndis_detach(dev)
701         device_t                dev;
702 {
703         struct ndis_softc       *sc;
704         struct ifnet            *ifp;
705         NDIS_LOCK_INFO;
706
707         sc = device_get_softc(dev);
708         NDIS_LOCK(sc);
709         ifp = &sc->arpcom.ac_if;
710         ifp->if_flags &= ~IFF_UP;
711
712         if (device_is_attached(dev)) {
713                 NDIS_UNLOCK(sc);
714                 ndis_stop(sc);
715                 if (sc->ndis_80211)
716                         ieee80211_ifdetach(ifp);
717                 else
718                         ether_ifdetach(ifp);
719         } else
720                 NDIS_UNLOCK(sc);
721
722         bus_generic_detach(dev);
723
724         if (sc->ndis_intrhand)
725                 bus_teardown_intr(dev, sc->ndis_irq, sc->ndis_intrhand);
726         if (sc->ndis_irq)
727                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ndis_irq);
728         if (sc->ndis_res_io)
729                 bus_release_resource(dev, SYS_RES_IOPORT,
730                     sc->ndis_io_rid, sc->ndis_res_io);
731         if (sc->ndis_res_mem)
732                 bus_release_resource(dev, SYS_RES_MEMORY,
733                     sc->ndis_mem_rid, sc->ndis_res_mem);
734         if (sc->ndis_res_altmem)
735                 bus_release_resource(dev, SYS_RES_MEMORY,
736                     sc->ndis_altmem_rid, sc->ndis_res_altmem);
737
738         if (sc->ndis_sc)
739                 ndis_destroy_dma(sc);
740
741         ndis_unload_driver((void *)ifp);
742
743         if (sc->ndis_iftype == PCIBus)
744                 bus_dma_tag_destroy(sc->ndis_parent_tag);
745
746 #if __FreeBSD_version < 502113
747         sysctl_ctx_free(&sc->ndis_ctx);
748
749 #endif
750         NDIS_LOCK_DESTROY(&sc->ndis_lock);
751         NDIS_LOCK_DESTROY(&sc->ndis_intrlock);
752
753         return(0);
754 }
755
756 int
757 ndis_suspend(dev)
758         device_t                dev;
759 {
760         struct ndis_softc       *sc;
761         struct ifnet            *ifp;
762
763         sc = device_get_softc(dev);
764         ifp = &sc->arpcom.ac_if;
765
766 #ifdef notdef
767         if (NDIS_INITIALIZED(sc))
768                 ndis_stop(sc);
769 #endif
770
771         return(0);
772 }
773
774 int
775 ndis_resume(dev)
776         device_t                dev;
777 {
778         struct ndis_softc       *sc;
779         struct ifnet            *ifp;
780
781         sc = device_get_softc(dev);
782         ifp = &sc->arpcom.ac_if;
783
784         if (NDIS_INITIALIZED(sc))
785                 ndis_init(sc);
786
787         return(0);
788 }
789
790 /*
791  * A frame has been uploaded: pass the resulting mbuf chain up to
792  * the higher level protocols.
793  *
794  * When handling received NDIS packets, the 'status' field in the
795  * out-of-band portion of the ndis_packet has special meaning. In the
796  * most common case, the underlying NDIS driver will set this field
797  * to NDIS_STATUS_SUCCESS, which indicates that it's ok for us to
798  * take posession of it. We then change the status field to
799  * NDIS_STATUS_PENDING to tell the driver that we now own the packet,
800  * and that we will return it at some point in the future via the
801  * return packet handler.
802  *
803  * If the driver hands us a packet with a status of NDIS_STATUS_RESOURCES,
804  * this means the driver is running out of packet/buffer resources and
805  * wants to maintain ownership of the packet. In this case, we have to
806  * copy the packet data into local storage and let the driver keep the
807  * packet.
808  */
809 __stdcall static void
810 ndis_rxeof(adapter, packets, pktcnt)
811         ndis_handle             adapter;
812         ndis_packet             **packets;
813         uint32_t                pktcnt;
814 {
815         struct ndis_softc       *sc;
816         ndis_miniport_block     *block;
817         ndis_packet             *p;
818         uint32_t                s;
819         ndis_tcpip_csum         *csum;
820         struct ifnet            *ifp;
821         struct mbuf             *m0, *m;
822         int                     i;
823
824         block = (ndis_miniport_block *)adapter;
825         sc = (struct ndis_softc *)(block->nmb_ifp);
826         ifp = block->nmb_ifp;
827
828         for (i = 0; i < pktcnt; i++) {
829                 p = packets[i];
830                 /* Stash the softc here so ptom can use it. */
831                 p->np_softc = sc;
832                 if (ndis_ptom(&m0, p)) {
833                         device_printf (sc->ndis_dev, "ptom failed\n");
834                         if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
835                                 ndis_return_packet(sc, p);
836                 } else {
837                         if (p->np_oob.npo_status == NDIS_STATUS_RESOURCES) {
838                                 m = m_dup(m0, MB_DONTWAIT);
839                                 /*
840                                  * NOTE: we want to destroy the mbuf here, but
841                                  * we don't actually want to return it to the
842                                  * driver via the return packet handler. By
843                                  * bumping np_refcnt, we can prevent the
844                                  * ndis_return_packet() routine from actually
845                                  * doing anything.
846                                  */
847                                 p->np_refcnt++;
848                                 m_freem(m0);
849                                 if (m == NULL)
850                                         ifp->if_ierrors++;
851                                 else
852                                         m0 = m;
853                         } else
854                                 p->np_oob.npo_status = NDIS_STATUS_PENDING;
855                         m0->m_pkthdr.rcvif = ifp;
856                         ifp->if_ipackets++;
857
858                         /* Deal with checksum offload. */
859
860                         if (ifp->if_capenable & IFCAP_RXCSUM &&
861                             p->np_ext.npe_info[ndis_tcpipcsum_info] != NULL) {
862                                 s = (uintptr_t)
863                                     p->np_ext.npe_info[ndis_tcpipcsum_info];
864                                 csum = (ndis_tcpip_csum *)&s;
865                                 if (csum->u.ntc_rxflags &
866                                     NDIS_RXCSUM_IP_PASSED)
867                                         m0->m_pkthdr.csum_flags |=
868                                             CSUM_IP_CHECKED|CSUM_IP_VALID;
869                                 if (csum->u.ntc_rxflags &
870                                     (NDIS_RXCSUM_TCP_PASSED |
871                                     NDIS_RXCSUM_UDP_PASSED)) {
872                                         m0->m_pkthdr.csum_flags |=
873                                             CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
874                                         m0->m_pkthdr.csum_data = 0xFFFF;
875                                 }
876                         }
877
878                         (*ifp->if_input)(ifp, m0);
879                 }
880         }
881
882         return;
883 }
884
885 /*
886  * A frame was downloaded to the chip. It's safe for us to clean up
887  * the list buffers.
888  */
889 __stdcall static void
890 ndis_txeof(adapter, packet, status)
891         ndis_handle             adapter;
892         ndis_packet             *packet;
893         ndis_status             status;
894
895 {
896         struct ndis_softc       *sc;
897         ndis_miniport_block     *block;
898         struct ifnet            *ifp;
899         int                     idx;
900         struct mbuf             *m;
901         NDIS_LOCK_INFO;
902
903         block = (ndis_miniport_block *)adapter;
904         sc = (struct ndis_softc *)block->nmb_ifp;
905         ifp = block->nmb_ifp;
906
907         m = packet->np_m0;
908         idx = packet->np_txidx;
909         if (sc->ndis_sc)
910                 bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
911
912         ndis_free_packet(packet);
913         m_freem(m);
914
915         NDIS_LOCK(sc);
916         sc->ndis_txarray[idx] = NULL;
917         sc->ndis_txpending++;
918
919         if (status == NDIS_STATUS_SUCCESS)
920                 ifp->if_opackets++;
921         else
922                 ifp->if_oerrors++;
923         ifp->if_timer = 0;
924         ifp->if_flags &= ~IFF_OACTIVE;
925         NDIS_UNLOCK(sc);
926
927         ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
928
929         return;
930 }
931
932 __stdcall static void
933 ndis_linksts(adapter, status, sbuf, slen)
934         ndis_handle             adapter;
935         ndis_status             status;
936         void                    *sbuf;
937         uint32_t                slen;
938 {
939         ndis_miniport_block     *block;
940
941         block = adapter;
942         block->nmb_getstat = status;
943
944         return;
945 }
946
947 __stdcall static void
948 ndis_linksts_done(adapter)
949         ndis_handle             adapter;
950 {
951         ndis_miniport_block     *block;
952         struct ndis_softc       *sc;
953         struct ifnet            *ifp;
954
955         block = adapter;
956         ifp = block->nmb_ifp;
957         sc = ifp->if_softc;
958
959         if (!NDIS_INITIALIZED(sc))
960                 return;
961
962         switch (block->nmb_getstat) {
963         case NDIS_STATUS_MEDIA_CONNECT:
964                 ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
965                 ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
966                 break;
967         case NDIS_STATUS_MEDIA_DISCONNECT:
968                 if (sc->ndis_link)
969                         ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
970                 break;
971         default:
972                 break;
973         }
974
975         return;
976 }
977
978 static void
979 ndis_intrtask(arg)
980         void                    *arg;
981 {
982         struct ndis_softc       *sc;
983         struct ifnet            *ifp;
984         uint8_t                 irql;
985         NDIS_LOCK_INFO;
986
987         sc = arg;
988         ifp = &sc->arpcom.ac_if;
989
990         irql = FASTCALL1(hal_raise_irql, DISPATCH_LEVEL);
991         ndis_intrhand(sc);
992         FASTCALL1(hal_lower_irql, irql);
993         NDIS_INTRLOCK(sc);
994         ndis_enable_intr(sc);
995         NDIS_INTRUNLOCK(sc);
996
997         return;
998 }
999
1000 static void
1001 ndis_intr(arg)
1002         void                    *arg;
1003 {
1004         struct ndis_softc       *sc;
1005         struct ifnet            *ifp;
1006         int                     is_our_intr = 0;
1007         int                     call_isr = 0;
1008         NDIS_LOCK_INFO;
1009
1010         sc = arg;
1011         ifp = &sc->arpcom.ac_if;
1012
1013         if (sc->ndis_block.nmb_miniportadapterctx == NULL)
1014                 return;
1015
1016         NDIS_INTRLOCK(sc);
1017         if (sc->ndis_block.nmb_interrupt->ni_isrreq == TRUE)
1018                 ndis_isr(sc, &is_our_intr, &call_isr);
1019         else {
1020                 ndis_disable_intr(sc);
1021                 call_isr = 1;
1022         }
1023         NDIS_INTRUNLOCK(sc);
1024
1025         if ((is_our_intr || call_isr))
1026                 ndis_sched(ndis_intrtask, ifp, NDIS_SWI);
1027
1028         return;
1029 }
1030
1031 static void
1032 ndis_tick(xsc)
1033         void                    *xsc;
1034 {
1035         struct ndis_softc       *sc;
1036
1037         sc = xsc;
1038
1039         ndis_sched(ndis_ticktask, sc, NDIS_TASKQUEUE);
1040         callout_reset(&sc->ndis_stat_timer,
1041                       hz *sc->ndis_block.nmb_checkforhangsecs, ndis_tick, sc);
1042 }
1043
1044 static void
1045 ndis_ticktask(xsc)
1046         void                    *xsc;
1047 {
1048         struct ndis_softc       *sc;
1049         __stdcall ndis_checkforhang_handler hangfunc;
1050         uint8_t                 rval;
1051         ndis_media_state        linkstate;
1052         int                     error, len;
1053         NDIS_LOCK_INFO;
1054
1055         sc = xsc;
1056
1057         hangfunc = sc->ndis_chars.nmc_checkhang_func;
1058
1059         if (hangfunc != NULL) {
1060                 rval = hangfunc(sc->ndis_block.nmb_miniportadapterctx);
1061                 if (rval == TRUE) {
1062                         ndis_reset_nic(sc);
1063                         return;
1064                 }
1065         }
1066
1067         len = sizeof(linkstate);
1068         error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1069             (void *)&linkstate, &len);
1070
1071         NDIS_LOCK(sc);
1072
1073         if (sc->ndis_link == 0 && linkstate == nmc_connected) {
1074                 device_printf(sc->ndis_dev, "link up\n");
1075                 sc->ndis_link = 1;
1076                 NDIS_UNLOCK(sc);
1077                 if (sc->ndis_80211)
1078                         ndis_getstate_80211(sc);
1079                 NDIS_LOCK(sc);
1080 #ifdef LINK_STATE_UP
1081                 sc->arpcom.ac_if.if_link_state = LINK_STATE_UP;
1082                 rt_ifmsg(&(sc->arpcom.ac_if));
1083 #endif /* LINK_STATE_UP */
1084         }
1085
1086         if (sc->ndis_link == 1 && linkstate == nmc_disconnected) {
1087                 device_printf(sc->ndis_dev, "link down\n");
1088                 sc->ndis_link = 0;
1089 #ifdef LINK_STATE_DOWN
1090                 sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN;
1091                 rt_ifmsg(&(sc->arpcom.ac_if));
1092 #endif /* LINK_STATE_DOWN */
1093         }
1094
1095         NDIS_UNLOCK(sc);
1096
1097         return;
1098 }
1099
1100 static void
1101 ndis_map_sclist(arg, segs, nseg, mapsize, error)
1102         void                    *arg;
1103         bus_dma_segment_t       *segs;
1104         int                     nseg;
1105         bus_size_t              mapsize;
1106         int                     error;
1107
1108 {
1109         struct ndis_sc_list     *sclist;
1110         int                     i;
1111
1112         if (error || arg == NULL)
1113                 return;
1114
1115         sclist = arg;
1116
1117         sclist->nsl_frags = nseg;
1118
1119         for (i = 0; i < nseg; i++) {
1120                 sclist->nsl_elements[i].nse_addr.np_quad = segs[i].ds_addr;
1121                 sclist->nsl_elements[i].nse_len = segs[i].ds_len;
1122         }
1123
1124         return;
1125 }
1126
1127 static void
1128 ndis_starttask(arg)
1129         void                    *arg;
1130 {
1131         struct ifnet            *ifp;
1132
1133         ifp = arg;
1134         if (!ifq_is_empty(&ifp->if_snd))
1135                 ndis_start(ifp);
1136         return;
1137 }
1138
1139 /*
1140  * Main transmit routine. To make NDIS drivers happy, we need to
1141  * transform mbuf chains into NDIS packets and feed them to the
1142  * send packet routines. Most drivers allow you to send several
1143  * packets at once (up to the maxpkts limit). Unfortunately, rather
1144  * that accepting them in the form of a linked list, they expect
1145  * a contiguous array of pointers to packets.
1146  *
1147  * For those drivers which use the NDIS scatter/gather DMA mechanism,
1148  * we need to perform busdma work here. Those that use map registers
1149  * will do the mapping themselves on a buffer by buffer basis.
1150  */
1151
1152 static void
1153 ndis_start(ifp)
1154         struct ifnet            *ifp;
1155 {
1156         struct ndis_softc       *sc;
1157         struct mbuf             *m = NULL;
1158         ndis_packet             **p0 = NULL, *p = NULL;
1159         ndis_tcpip_csum         *csum;
1160         int                     pcnt = 0;
1161         NDIS_LOCK_INFO;
1162
1163         sc = ifp->if_softc;
1164
1165         NDIS_LOCK(sc);
1166
1167         if (!sc->ndis_link || ifp->if_flags & IFF_OACTIVE) {
1168                 NDIS_UNLOCK(sc);
1169                 return;
1170         }
1171
1172         p0 = &sc->ndis_txarray[sc->ndis_txidx];
1173
1174         while(sc->ndis_txpending) {
1175                 m = ifq_poll(&ifp->if_snd);
1176                 if (m == NULL)
1177                         break;
1178
1179                 sc->ndis_txarray[sc->ndis_txidx] = NULL;
1180
1181                 if (ndis_mtop(m, &sc->ndis_txarray[sc->ndis_txidx])) {
1182                         NDIS_UNLOCK(sc);
1183                         return;
1184                 }
1185                 m = ifq_dequeue(&ifp->if_snd);
1186
1187                 /*
1188                  * Save pointer to original mbuf
1189                  * so we can free it later.
1190                  */
1191
1192                 p = sc->ndis_txarray[sc->ndis_txidx];
1193                 p->np_txidx = sc->ndis_txidx;
1194                 p->np_m0 = m;
1195                 p->np_oob.npo_status = NDIS_STATUS_PENDING;
1196
1197                 /*
1198                  * Do scatter/gather processing, if driver requested it.
1199                  */
1200                 if (sc->ndis_sc) {
1201                         bus_dmamap_load_mbuf(sc->ndis_ttag,
1202                             sc->ndis_tmaps[sc->ndis_txidx], m,
1203                             ndis_map_sclist, &p->np_sclist, BUS_DMA_NOWAIT);
1204                         bus_dmamap_sync(sc->ndis_ttag,
1205                             sc->ndis_tmaps[sc->ndis_txidx],
1206                             BUS_DMASYNC_PREREAD);
1207                         p->np_ext.npe_info[ndis_sclist_info] = &p->np_sclist;
1208                 }
1209
1210                 /* Handle checksum offload. */
1211
1212                 if (ifp->if_capenable & IFCAP_TXCSUM &&
1213                     m->m_pkthdr.csum_flags) {
1214                         csum = (ndis_tcpip_csum *)
1215                                 &p->np_ext.npe_info[ndis_tcpipcsum_info];
1216                         csum->u.ntc_txflags = NDIS_TXCSUM_DO_IPV4;
1217                         if (m->m_pkthdr.csum_flags & CSUM_IP)
1218                                 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_IP;
1219                         if (m->m_pkthdr.csum_flags & CSUM_TCP)
1220                                 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_TCP;
1221                         if (m->m_pkthdr.csum_flags & CSUM_UDP)
1222                                 csum->u.ntc_txflags |= NDIS_TXCSUM_DO_UDP;
1223                         p->np_private.npp_flags = NDIS_PROTOCOL_ID_TCP_IP;
1224                 }
1225
1226                 NDIS_INC(sc);
1227                 sc->ndis_txpending--;
1228
1229                 pcnt++;
1230
1231                 /*
1232                  * If there's a BPF listener, bounce a copy of this frame
1233                  * to him.
1234                  */
1235
1236                 BPF_MTAP(ifp, m);
1237
1238                 /*
1239                  * The array that p0 points to must appear contiguous,
1240                  * so we must not wrap past the end of sc->ndis_txarray[].
1241                  * If it looks like we're about to wrap, break out here
1242                  * so the this batch of packets can be transmitted, then
1243                  * wait for txeof to ask us to send the rest.
1244                  */
1245
1246                 if (sc->ndis_txidx == 0)
1247                         break;
1248         }
1249
1250         if (sc->ndis_txpending == 0)
1251                 ifp->if_flags |= IFF_OACTIVE;
1252
1253         /*
1254          * Set a timeout in case the chip goes out to lunch.
1255          */
1256         ifp->if_timer = 5;
1257
1258         NDIS_UNLOCK(sc);
1259
1260         if (sc->ndis_maxpkts == 1)
1261                 ndis_send_packet(sc, p);
1262         else
1263                 ndis_send_packets(sc, p0, pcnt);
1264
1265         return;
1266 }
1267
1268 static void
1269 ndis_init(xsc)
1270         void                    *xsc;
1271 {
1272         struct ndis_softc       *sc = xsc;
1273         struct ifnet            *ifp = &sc->arpcom.ac_if;
1274         int                     i, error;
1275         NDIS_LOCK_INFO;
1276
1277         /*
1278          * Avoid reintializing the link unnecessarily.
1279          * This should be dealt with in a better way by
1280          * fixing the upper layer modules so they don't
1281          * call ifp->if_init() quite as often.
1282          */
1283         if (sc->ndis_link && sc->ndis_skip)
1284                 return;
1285
1286         /*
1287          * Cancel pending I/O and free all RX/TX buffers.
1288          */
1289         ndis_stop(sc);
1290         if (ndis_init_nic(sc))
1291                 return;
1292
1293         /* Init our MAC address */
1294
1295         /* Program the packet filter */
1296
1297         sc->ndis_filter = NDIS_PACKET_TYPE_DIRECTED;
1298
1299         if (ifp->if_flags & IFF_BROADCAST)
1300                 sc->ndis_filter |= NDIS_PACKET_TYPE_BROADCAST;
1301
1302         if (ifp->if_flags & IFF_PROMISC)
1303                 sc->ndis_filter |= NDIS_PACKET_TYPE_PROMISCUOUS;
1304
1305         i = sizeof(sc->ndis_filter);
1306
1307         error = ndis_set_info(sc, OID_GEN_CURRENT_PACKET_FILTER,
1308             &sc->ndis_filter, &i);
1309
1310         if (error)
1311                 device_printf (sc->ndis_dev, "set filter failed: %d\n", error);
1312
1313         /*
1314          * Program the multicast filter, if necessary.
1315          */
1316         ndis_setmulti(sc);
1317
1318         /* Setup task offload. */
1319         ndis_set_offload(sc);
1320
1321         /* Enable interrupts. */
1322         ndis_enable_intr(sc);
1323
1324         if (sc->ndis_80211)
1325                 ndis_setstate_80211(sc);
1326
1327         NDIS_LOCK(sc);
1328
1329         sc->ndis_txidx = 0;
1330         sc->ndis_txpending = sc->ndis_maxpkts;
1331         sc->ndis_link = 0;
1332
1333         ifp->if_flags |= IFF_RUNNING;
1334         ifp->if_flags &= ~IFF_OACTIVE;
1335
1336         NDIS_UNLOCK(sc);
1337
1338         /*
1339          * Some drivers don't set this value. The NDIS spec says
1340          * the default checkforhang timeout is "approximately 2
1341          * seconds." We use 3 seconds, because it seems for some
1342          * drivers, exactly 2 seconds is too fast.
1343          */
1344
1345         if (sc->ndis_block.nmb_checkforhangsecs == 0)
1346                 sc->ndis_block.nmb_checkforhangsecs = 3;
1347
1348         callout_reset(&sc->ndis_stat_timer,
1349                       hz * sc->ndis_block.nmb_checkforhangsecs, ndis_tick, sc);
1350 }
1351
1352 /*
1353  * Set media options.
1354  */
1355 static int
1356 ndis_ifmedia_upd(ifp)
1357         struct ifnet            *ifp;
1358 {
1359         struct ndis_softc               *sc;
1360
1361         sc = ifp->if_softc;
1362
1363         if (NDIS_INITIALIZED(sc))
1364                 ndis_init(sc);
1365
1366         return(0);
1367 }
1368
1369 /*
1370  * Report current media status.
1371  */
1372 static void
1373 ndis_ifmedia_sts(ifp, ifmr)
1374         struct ifnet            *ifp;
1375         struct ifmediareq       *ifmr;
1376 {
1377         struct ndis_softc       *sc;
1378         uint32_t                media_info;
1379         ndis_media_state        linkstate;
1380         int                     error, len;
1381
1382         ifmr->ifm_status = IFM_AVALID;
1383         ifmr->ifm_active = IFM_ETHER;
1384         sc = ifp->if_softc;
1385
1386         if (!NDIS_INITIALIZED(sc))
1387                 return;
1388
1389         len = sizeof(linkstate);
1390         error = ndis_get_info(sc, OID_GEN_MEDIA_CONNECT_STATUS,
1391             (void *)&linkstate, &len);
1392
1393         len = sizeof(media_info);
1394         error = ndis_get_info(sc, OID_GEN_LINK_SPEED,
1395             (void *)&media_info, &len);
1396
1397         if (linkstate == nmc_connected)
1398                 ifmr->ifm_status |= IFM_ACTIVE;
1399
1400         switch(media_info) {
1401         case 100000:
1402                 ifmr->ifm_active |= IFM_10_T;
1403                 break;
1404         case 1000000:
1405                 ifmr->ifm_active |= IFM_100_TX;
1406                 break;
1407         case 10000000:
1408                 ifmr->ifm_active |= IFM_1000_T;
1409                 break;
1410         default:
1411                 device_printf(sc->ndis_dev, "unknown speed: %d\n", media_info);
1412                 break;
1413         }
1414
1415         return;
1416 }
1417
1418 static void
1419 ndis_setstate_80211(sc)
1420         struct ndis_softc       *sc;
1421 {
1422         struct ieee80211com     *ic;
1423         ndis_80211_ssid         ssid;
1424         ndis_80211_config       config;
1425         ndis_80211_wep          wep;
1426         int                     i, rval = 0, len;
1427         uint32_t                arg;
1428         struct ifnet            *ifp;
1429
1430         ic = &sc->ic;
1431         ifp = &sc->ic.ic_ac.ac_if;
1432
1433         if (!NDIS_INITIALIZED(sc))
1434                 return;
1435
1436         /* Set network infrastructure mode. */
1437
1438         len = sizeof(arg);
1439         if (ic->ic_opmode == IEEE80211_M_IBSS)
1440                 arg = NDIS_80211_NET_INFRA_IBSS;
1441         else
1442                 arg = NDIS_80211_NET_INFRA_BSS;
1443
1444         rval = ndis_set_info(sc, OID_802_11_INFRASTRUCTURE_MODE, &arg, &len);
1445
1446         if (rval)
1447                 device_printf (sc->ndis_dev, "set infra failed: %d\n", rval);
1448
1449         /* Set WEP */
1450
1451 #ifdef IEEE80211_F_WEPON
1452         if (ic->ic_flags & IEEE80211_F_WEPON) {
1453 #else
1454         if (ic->ic_wep_mode >= IEEE80211_WEP_ON) {
1455 #endif
1456                 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1457                         if (ic->ic_nw_keys[i].wk_len) {
1458                                 bzero((char *)&wep, sizeof(wep));
1459                                 wep.nw_keylen = ic->ic_nw_keys[i].wk_len;
1460 #ifdef notdef
1461                                 /* 5 and 13 are the only valid key lengths */
1462                                 if (ic->ic_nw_keys[i].wk_len < 5)
1463                                         wep.nw_keylen = 5;
1464                                 else if (ic->ic_nw_keys[i].wk_len > 5 &&
1465                                      ic->ic_nw_keys[i].wk_len < 13)
1466                                         wep.nw_keylen = 13;
1467 #endif
1468                                 wep.nw_keyidx = i;
1469                                 wep.nw_length = (sizeof(uint32_t) * 3)
1470                                     + wep.nw_keylen;
1471                                 if (i == ic->ic_wep_txkey)
1472                                         wep.nw_keyidx |= NDIS_80211_WEPKEY_TX;
1473                                 bcopy(ic->ic_nw_keys[i].wk_key,
1474                                     wep.nw_keydata, wep.nw_length);
1475                                 len = sizeof(wep);
1476                                 rval = ndis_set_info(sc,
1477                                     OID_802_11_ADD_WEP, &wep, &len);
1478                                 if (rval)
1479                                         device_printf(sc->ndis_dev,
1480                                             "set wepkey failed: %d\n", rval);
1481                         }
1482                 }
1483                 arg = NDIS_80211_WEPSTAT_ENABLED;
1484                 len = sizeof(arg);
1485                 rval = ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1486                 if (rval)
1487                         device_printf(sc->ndis_dev,
1488                             "enable WEP failed: %d\n", rval);
1489 #ifndef IEEE80211_F_WEPON
1490                 if (ic->ic_wep_mode != IEEE80211_WEP_8021X &&
1491                     ic->ic_wep_mode != IEEE80211_WEP_ON)
1492                         arg = NDIS_80211_PRIVFILT_ACCEPTALL;
1493                 else
1494 #endif
1495                         arg = NDIS_80211_PRIVFILT_8021XWEP;
1496                 len = sizeof(arg);
1497                 rval = ndis_set_info(sc, OID_802_11_PRIVACY_FILTER, &arg, &len);
1498 #ifdef IEEE80211_WEP_8021X /*IEEE80211_F_WEPON*/
1499                 /* Accept that we only have "shared" and 802.1x modes. */
1500                 if (rval == 0) {
1501                         if (arg == NDIS_80211_PRIVFILT_ACCEPTALL)
1502                                 ic->ic_wep_mode = IEEE80211_WEP_MIXED;
1503                         else
1504                                 ic->ic_wep_mode = IEEE80211_WEP_8021X;
1505                 }
1506 #endif
1507                 arg = NDIS_80211_AUTHMODE_OPEN;
1508         } else {
1509                 arg = NDIS_80211_WEPSTAT_DISABLED;
1510                 len = sizeof(arg);
1511                 ndis_set_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1512                 arg = NDIS_80211_AUTHMODE_OPEN;
1513         }
1514
1515         len = sizeof(arg);
1516         rval = ndis_set_info(sc, OID_802_11_AUTHENTICATION_MODE, &arg, &len);
1517
1518 #ifdef notyet
1519         if (rval)
1520                 device_printf (sc->ndis_dev, "set auth failed: %d\n", rval);
1521 #endif
1522
1523 #ifdef notyet
1524         /* Set network type. */
1525
1526         arg = 0;
1527
1528         switch (ic->ic_curmode) {
1529         case IEEE80211_MODE_11A:
1530                 arg = NDIS_80211_NETTYPE_11OFDM5;
1531                 break;
1532         case IEEE80211_MODE_11B:
1533                 arg = NDIS_80211_NETTYPE_11DS;
1534                 break;
1535         case IEEE80211_MODE_11G:
1536                 arg = NDIS_80211_NETTYPE_11OFDM24;
1537                 break;
1538         default:
1539                 device_printf(sc->ndis_dev, "unknown mode: %d\n",
1540                     ic->ic_curmode);
1541         }
1542
1543         if (arg) {
1544                 len = sizeof(arg);
1545                 rval = ndis_set_info(sc, OID_802_11_NETWORK_TYPE_IN_USE,
1546                     &arg, &len);
1547                 if (rval)
1548                         device_printf (sc->ndis_dev,
1549                             "set nettype failed: %d\n", rval);
1550         }
1551 #endif
1552
1553         len = sizeof(config);
1554         bzero((char *)&config, len);
1555         config.nc_length = len;
1556         config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1557         rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len); 
1558
1559         /*
1560          * Some drivers expect us to initialize these values, so
1561          * provide some defaults.
1562          */
1563         if (config.nc_beaconperiod == 0)
1564                 config.nc_beaconperiod = 100;
1565         if (config.nc_atimwin == 0)
1566                 config.nc_atimwin = 100;
1567         if (config.nc_fhconfig.ncf_dwelltime == 0)
1568                 config.nc_fhconfig.ncf_dwelltime = 200;
1569
1570         if (rval == 0 && ic->ic_ibss_chan != IEEE80211_CHAN_ANYC) { 
1571                 int chan, chanflag;
1572
1573                 chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
1574                 chanflag = config.nc_dsconfig > 2500000 ? IEEE80211_CHAN_2GHZ :
1575                     IEEE80211_CHAN_5GHZ;
1576                 if (chan != ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0)) {
1577                         config.nc_dsconfig =
1578                             ic->ic_ibss_chan->ic_freq * 1000;
1579                         ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1580                         len = sizeof(config);
1581                         config.nc_length = len;
1582                         config.nc_fhconfig.ncf_length =
1583                             sizeof(ndis_80211_config_fh);
1584                         rval = ndis_set_info(sc, OID_802_11_CONFIGURATION,
1585                             &config, &len);
1586                         if (rval)
1587                                 device_printf(sc->ndis_dev, "couldn't change "
1588                                     "DS config to %ukHz: %d\n",
1589                                     config.nc_dsconfig, rval);
1590                 }
1591         } else if (rval)
1592                 device_printf(sc->ndis_dev, "couldn't retrieve "
1593                     "channel info: %d\n", rval);
1594
1595         /* Set SSID -- always do this last. */
1596
1597         len = sizeof(ssid);
1598         bzero((char *)&ssid, len);
1599         ssid.ns_ssidlen = ic->ic_des_esslen;
1600         if (ssid.ns_ssidlen == 0) {
1601                 ssid.ns_ssidlen = 1;
1602         } else
1603                 bcopy(ic->ic_des_essid, ssid.ns_ssid, ssid.ns_ssidlen);
1604         rval = ndis_set_info(sc, OID_802_11_SSID, &ssid, &len);
1605
1606         if (rval)
1607                 device_printf (sc->ndis_dev, "set ssid failed: %d\n", rval);
1608
1609         return;
1610 }
1611
1612 static void
1613 ndis_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1614 {
1615         struct ieee80211com *ic = (void *)ifp;
1616         struct ieee80211_node *ni = NULL;
1617
1618         imr->ifm_status = IFM_AVALID;
1619         imr->ifm_active = IFM_IEEE80211;
1620         if (ic->ic_state == IEEE80211_S_RUN)
1621                 imr->ifm_status |= IFM_ACTIVE;
1622         imr->ifm_active |= IFM_AUTO;
1623         switch (ic->ic_opmode) {
1624         case IEEE80211_M_STA:
1625                 ni = ic->ic_bss;
1626                 /* calculate rate subtype */
1627                 imr->ifm_active |= ieee80211_rate2media(ic,
1628                         ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1629                 break;
1630         case IEEE80211_M_IBSS:
1631                 ni = ic->ic_bss;
1632                 /* calculate rate subtype */
1633                 imr->ifm_active |= ieee80211_rate2media(ic,
1634                         ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
1635                 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1636                 break;
1637         case IEEE80211_M_AHDEMO:
1638                 /* should not come here */
1639                 break;
1640         case IEEE80211_M_HOSTAP:
1641                 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1642                 break;
1643         case IEEE80211_M_MONITOR:
1644                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1645                 break;
1646         }
1647         switch (ic->ic_curmode) {
1648         case IEEE80211_MODE_11A:
1649                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
1650                 break;
1651         case IEEE80211_MODE_11B:
1652                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
1653                 break;
1654         case IEEE80211_MODE_11G:
1655                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
1656                 break;
1657         case IEEE80211_MODE_TURBO:
1658                 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
1659                                 |  IFM_IEEE80211_TURBO;
1660                 break;
1661         }
1662 }
1663
1664 static int
1665 ndis_get_assoc(sc, assoc)
1666         struct ndis_softc       *sc;
1667         ndis_wlan_bssid_ex      **assoc;
1668 {
1669         ndis_80211_bssid_list_ex        *bl;
1670         ndis_wlan_bssid_ex      *bs;
1671         ndis_80211_macaddr      bssid;
1672         int                     i, len, error;
1673
1674         if (!sc->ndis_link)
1675                 return(ENOENT);
1676
1677         len = sizeof(bssid);
1678         error = ndis_get_info(sc, OID_802_11_BSSID, &bssid, &len);
1679         if (error) {
1680                 device_printf(sc->ndis_dev, "failed to get bssid\n");
1681                 return(ENOENT);
1682         }
1683         len = 0;
1684         error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1685         if (error != ENOSPC) {
1686                 device_printf(sc->ndis_dev, "bssid_list failed\n");
1687                 return (error);
1688         }
1689
1690         bl = malloc(len, M_TEMP, M_NOWAIT|M_ZERO);
1691         error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1692         if (error) {
1693                 free(bl, M_TEMP);
1694                 device_printf(sc->ndis_dev, "bssid_list failed\n");
1695                 return (error);
1696         }
1697
1698         bs = (ndis_wlan_bssid_ex *)&bl->nblx_bssid[0];
1699         for (i = 0; i < bl->nblx_items; i++) {
1700                 if (bcmp(bs->nwbx_macaddr, bssid, sizeof(bssid)) == 0) {
1701                         *assoc = malloc(bs->nwbx_len, M_TEMP, M_NOWAIT);
1702                         if (*assoc == NULL) {
1703                                 free(bl, M_TEMP);
1704                                 return(ENOMEM);
1705                         }
1706                         bcopy((char *)bs, (char *)*assoc, bs->nwbx_len);
1707                         free(bl, M_TEMP);
1708                         return(0);
1709                 }       
1710                 bs = (ndis_wlan_bssid_ex *)((char *)bs + bs->nwbx_len);
1711         }
1712
1713         free(bl, M_TEMP);
1714         return(ENOENT);
1715 }
1716
1717 static void
1718 ndis_getstate_80211(sc)
1719         struct ndis_softc       *sc;
1720 {
1721         struct ieee80211com     *ic;
1722         ndis_80211_ssid         ssid;
1723         ndis_80211_config       config;
1724         ndis_wlan_bssid_ex      *bs;
1725         int                     rval, len, i = 0;
1726         uint32_t                arg;
1727         struct ifnet            *ifp;
1728
1729         ic = &sc->ic;
1730         ifp = &sc->ic.ic_ac.ac_if;
1731
1732         if (!NDIS_INITIALIZED(sc))
1733                 return;
1734
1735         if (sc->ndis_link)
1736                 ic->ic_state = IEEE80211_S_RUN;
1737         else
1738                 ic->ic_state = IEEE80211_S_ASSOC;
1739
1740
1741         /*
1742          * If we're associated, retrieve info on the current bssid.
1743          */
1744         if ((rval = ndis_get_assoc(sc, &bs)) == 0) {
1745                 switch(bs->nwbx_nettype) {
1746                 case NDIS_80211_NETTYPE_11FH:
1747                 case NDIS_80211_NETTYPE_11DS:
1748                         ic->ic_curmode = IEEE80211_MODE_11B;
1749                         break;
1750                 case NDIS_80211_NETTYPE_11OFDM5:
1751                         ic->ic_curmode = IEEE80211_MODE_11A;
1752                         break;
1753                 case NDIS_80211_NETTYPE_11OFDM24:
1754                         ic->ic_curmode = IEEE80211_MODE_11G;
1755                         break;
1756                 default:
1757                         device_printf(sc->ndis_dev,
1758                             "unknown nettype %d\n", arg);
1759                         break;
1760                 }
1761                 free(bs, M_TEMP);
1762         } else
1763                 return;
1764
1765         len = sizeof(ssid);
1766         bzero((char *)&ssid, len);
1767         rval = ndis_get_info(sc, OID_802_11_SSID, &ssid, &len);
1768
1769         if (rval)
1770                 device_printf (sc->ndis_dev, "get ssid failed: %d\n", rval);
1771         bcopy(ssid.ns_ssid, ic->ic_bss->ni_essid, ssid.ns_ssidlen);
1772         ic->ic_bss->ni_esslen = ssid.ns_ssidlen;
1773
1774         len = sizeof(arg);
1775         rval = ndis_get_info(sc, OID_GEN_LINK_SPEED, &arg, &len);
1776         if (rval)
1777                 device_printf (sc->ndis_dev, "get link speed failed: %d\n",
1778                     rval);
1779
1780         if (ic->ic_modecaps & (1<<IEEE80211_MODE_11B)) {
1781                 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
1782                 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1783                         if ((ic->ic_bss->ni_rates.rs_rates[i] &
1784                             IEEE80211_RATE_VAL) == arg / 5000)
1785                                 break;
1786                 }
1787         }
1788
1789         if (i == ic->ic_bss->ni_rates.rs_nrates &&
1790             ic->ic_modecaps & (1<<IEEE80211_MODE_11G)) {
1791                 ic->ic_bss->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11G];
1792                 for (i = 0; i < ic->ic_bss->ni_rates.rs_nrates; i++) {
1793                         if ((ic->ic_bss->ni_rates.rs_rates[i] &
1794                             IEEE80211_RATE_VAL) == arg / 5000)
1795                                 break;
1796                 }
1797         }
1798
1799         if (i == ic->ic_bss->ni_rates.rs_nrates)
1800                 device_printf(sc->ndis_dev, "no matching rate for: %d\n",
1801                     arg / 5000);
1802         else
1803                 ic->ic_bss->ni_txrate = i;
1804
1805         if (ic->ic_caps & IEEE80211_C_PMGT) {
1806                 len = sizeof(arg);
1807                 rval = ndis_get_info(sc, OID_802_11_POWER_MODE, &arg, &len);
1808
1809                 if (rval)
1810                         device_printf(sc->ndis_dev,
1811                             "get power mode failed: %d\n", rval);
1812                 if (arg == NDIS_80211_POWERMODE_CAM)
1813                         ic->ic_flags &= ~IEEE80211_F_PMGTON;
1814                 else
1815                         ic->ic_flags |= IEEE80211_F_PMGTON;
1816         }
1817
1818         len = sizeof(config);
1819         bzero((char *)&config, len);
1820         config.nc_length = len;
1821         config.nc_fhconfig.ncf_length = sizeof(ndis_80211_config_fh);
1822         rval = ndis_get_info(sc, OID_802_11_CONFIGURATION, &config, &len);   
1823         if (rval == 0) { 
1824                 int chan;
1825
1826                 chan = ieee80211_mhz2ieee(config.nc_dsconfig / 1000, 0);
1827                 if (chan < 0 || chan >= IEEE80211_CHAN_MAX) {
1828                         if (ifp->if_flags & IFF_DEBUG)
1829                                 device_printf(sc->ndis_dev, "current channel "
1830                                     "(%uMHz) out of bounds\n", 
1831                                     config.nc_dsconfig / 1000);
1832                         ic->ic_bss->ni_chan = &ic->ic_channels[1];
1833                 } else
1834                         ic->ic_bss->ni_chan = &ic->ic_channels[chan];
1835         } else
1836                 device_printf(sc->ndis_dev, "couldn't retrieve "
1837                     "channel info: %d\n", rval);
1838
1839 /*
1840         len = sizeof(arg);
1841         rval = ndis_get_info(sc, OID_802_11_WEP_STATUS, &arg, &len);
1842
1843         if (rval)
1844                 device_printf (sc->ndis_dev,
1845                     "get wep status failed: %d\n", rval);
1846
1847         if (arg == NDIS_80211_WEPSTAT_ENABLED)
1848                 ic->ic_flags |= IEEE80211_F_WEPON;
1849         else
1850                 ic->ic_flags &= ~IEEE80211_F_WEPON;
1851 */
1852         return;
1853 }
1854
1855 static int
1856 ndis_ioctl(ifp, command, data, cr)
1857         struct ifnet            *ifp;
1858         u_long                  command;
1859         caddr_t                 data;
1860         struct                  ucred *cr;
1861 {
1862         struct ndis_softc       *sc = ifp->if_softc;
1863         struct ifreq            *ifr = (struct ifreq *) data;
1864         int                     i, error = 0;
1865
1866         /*NDIS_LOCK(sc);*/
1867
1868         switch(command) {
1869         case SIOCSIFFLAGS:
1870                 if (ifp->if_flags & IFF_UP) {
1871                         if (ifp->if_flags & IFF_RUNNING &&
1872                             ifp->if_flags & IFF_PROMISC &&
1873                             !(sc->ndis_if_flags & IFF_PROMISC)) {
1874                                 sc->ndis_filter |=
1875                                     NDIS_PACKET_TYPE_PROMISCUOUS;
1876                                 i = sizeof(sc->ndis_filter);
1877                                 error = ndis_set_info(sc,
1878                                     OID_GEN_CURRENT_PACKET_FILTER,
1879                                     &sc->ndis_filter, &i);
1880                         } else if (ifp->if_flags & IFF_RUNNING &&
1881                             !(ifp->if_flags & IFF_PROMISC) &&
1882                             sc->ndis_if_flags & IFF_PROMISC) {
1883                                 sc->ndis_filter &=
1884                                     ~NDIS_PACKET_TYPE_PROMISCUOUS;
1885                                 i = sizeof(sc->ndis_filter);
1886                                 error = ndis_set_info(sc,
1887                                     OID_GEN_CURRENT_PACKET_FILTER,
1888                                     &sc->ndis_filter, &i);
1889                         } else
1890                                 ndis_init(sc);
1891                 } else {
1892                         if (ifp->if_flags & IFF_RUNNING)
1893                                 ndis_stop(sc);
1894                 }
1895                 sc->ndis_if_flags = ifp->if_flags;
1896                 error = 0;
1897                 break;
1898         case SIOCADDMULTI:
1899         case SIOCDELMULTI:
1900                 ndis_setmulti(sc);
1901                 error = 0;
1902                 break;
1903         case SIOCGIFMEDIA:
1904         case SIOCSIFMEDIA:
1905                 if (sc->ndis_80211) {
1906                         error = ieee80211_ioctl(ifp, command, data, cr);
1907                         if (error == ENETRESET) {
1908                                 ndis_setstate_80211(sc);
1909                                 /*ndis_init(sc);*/
1910                                 error = 0;
1911                         }
1912                 } else
1913                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1914                 break;
1915         case SIOCSIFCAP:
1916                 ifp->if_capenable = ifr->ifr_reqcap;
1917                 if (ifp->if_capenable & IFCAP_TXCSUM)
1918                         ifp->if_hwassist = sc->ndis_hwassist;
1919                 else
1920                         ifp->if_hwassist = 0;
1921                 ndis_set_offload(sc);
1922                 break;
1923         case SIOCGIFGENERIC:
1924         case SIOCSIFGENERIC:
1925                 if (sc->ndis_80211 && NDIS_INITIALIZED(sc)) {
1926                         if (command == SIOCGIFGENERIC)
1927                                 error = ndis_wi_ioctl_get(ifp, command, data);
1928                         else
1929                                 error = ndis_wi_ioctl_set(ifp, command, data);
1930                 } else
1931                         error = ENOTTY;
1932                 if (error != ENOTTY)
1933                         break;
1934         default:
1935                 sc->ndis_skip = 1;
1936                 if (sc->ndis_80211) {
1937                         error = ieee80211_ioctl(ifp, command, data, cr);
1938                         if (error == ENETRESET) {
1939                                 ndis_setstate_80211(sc);
1940                                 error = 0;
1941                         }
1942                 } else
1943                         error = ether_ioctl(ifp, command, data);
1944                 sc->ndis_skip = 0;
1945                 break;
1946         }
1947
1948         /*NDIS_UNLOCK(sc);*/
1949
1950         return(error);
1951 }
1952
1953 static int
1954 ndis_wi_ioctl_get(ifp, command, data)
1955         struct ifnet            *ifp;
1956         u_long                  command;
1957         caddr_t                 data;
1958 {
1959         struct wi_req           wreq;
1960         struct ifreq            *ifr;
1961         struct ndis_softc       *sc;
1962         ndis_80211_bssid_list_ex *bl;
1963         ndis_wlan_bssid_ex      *wb;
1964         struct wi_apinfo        *api;
1965         int                     error, i, j, len, maxaps;
1966
1967         sc = ifp->if_softc;
1968         ifr = (struct ifreq *)data;
1969         error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1970         if (error)
1971                 return (error);
1972
1973         switch (wreq.wi_type) {
1974         case WI_RID_READ_APS:
1975                 len = 0;
1976                 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN,
1977                     NULL, &len);
1978                 if (error == 0)
1979                         tsleep(&error, PPAUSE|PCATCH, "ssidscan", hz * 2);
1980                 len = 0;
1981                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, NULL, &len);
1982                 if (error != ENOSPC)
1983                         break;
1984                 bl = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
1985                 error = ndis_get_info(sc, OID_802_11_BSSID_LIST, bl, &len);
1986                 if (error) {
1987                         free(bl, M_DEVBUF);
1988                         break;
1989                 }
1990                 maxaps = (2 * wreq.wi_len - sizeof(int)) / sizeof(*api);
1991                 maxaps = MIN(maxaps, bl->nblx_items);
1992                 wreq.wi_len = (maxaps * sizeof(*api) + sizeof(int)) / 2;
1993                 *(int *)&wreq.wi_val = maxaps;
1994                 api = (struct wi_apinfo *)&((int *)&wreq.wi_val)[1];
1995                 wb = bl->nblx_bssid;
1996                 while (maxaps--) {
1997                         bzero(api, sizeof(*api));
1998                         bcopy(&wb->nwbx_macaddr, &api->bssid,
1999                             sizeof(api->bssid));
2000                         api->namelen = wb->nwbx_ssid.ns_ssidlen;
2001                         bcopy(&wb->nwbx_ssid.ns_ssid, &api->name, api->namelen);
2002                         if (wb->nwbx_privacy)
2003                                 api->capinfo |= IEEE80211_CAPINFO_PRIVACY;
2004                         /* XXX Where can we get noise information? */
2005                         api->signal = wb->nwbx_rssi + 149;      /* XXX */
2006                         api->quality = api->signal;
2007                         api->channel =
2008                             ieee80211_mhz2ieee(wb->nwbx_config.nc_dsconfig /
2009                             1000, 0);
2010                         /* In "auto" infrastructure mode, this is useless. */
2011                         if (wb->nwbx_netinfra == NDIS_80211_NET_INFRA_IBSS)
2012                                 api->capinfo |= IEEE80211_CAPINFO_IBSS;
2013                         if (wb->nwbx_len > sizeof(ndis_wlan_bssid)) {
2014                                 j = sizeof(ndis_80211_rates_ex);
2015                                 /* handle other extended things */
2016                         } else
2017                                 j = sizeof(ndis_80211_rates);
2018                         for (i = api->rate = 0; i < j; i++)
2019                                 api->rate = MAX(api->rate, 5 *
2020                                     (wb->nwbx_supportedrates[i] & 0x7f));
2021                         api++;
2022                         wb = (ndis_wlan_bssid_ex *)((char *)wb + wb->nwbx_len);
2023                 }
2024                 free(bl, M_DEVBUF);
2025                 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
2026                 break;
2027         default:
2028                 error = ENOTTY;
2029                 break;
2030         }
2031         return (error);
2032 }
2033
2034 static int
2035 ndis_wi_ioctl_set(ifp, command, data)
2036         struct ifnet            *ifp;
2037         u_long                  command;
2038         caddr_t                 data;
2039 {
2040         struct wi_req           wreq;
2041         struct ifreq            *ifr;
2042         struct ndis_softc       *sc;
2043         uint32_t                foo;
2044         int                     error, len;
2045
2046         error = suser(curthread);
2047         if (error)
2048                 return (error);
2049
2050         sc = ifp->if_softc;
2051         ifr = (struct ifreq *)data;
2052         error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2053         if (error)
2054                 return (error);
2055
2056         switch (wreq.wi_type) {
2057         case WI_RID_SCAN_APS:
2058         case WI_RID_SCAN_REQ:                   /* arguments ignored */
2059                 len = sizeof(foo);
2060                 foo = 0;
2061                 error = ndis_set_info(sc, OID_802_11_BSSID_LIST_SCAN, &foo,
2062                     &len);
2063                 break;
2064         default:
2065                 error = ENOTTY;
2066                 break;
2067         }
2068         return (error);
2069 }
2070
2071 static void
2072 ndis_watchdog(ifp)
2073         struct ifnet            *ifp;
2074 {
2075         struct ndis_softc               *sc;
2076         NDIS_LOCK_INFO;
2077
2078         sc = ifp->if_softc;
2079
2080         NDIS_LOCK(sc);
2081         ifp->if_oerrors++;
2082         device_printf(sc->ndis_dev, "watchdog timeout\n");
2083         NDIS_UNLOCK(sc);
2084
2085         ndis_reset_nic(sc);
2086         ndis_sched(ndis_starttask, ifp, NDIS_TASKQUEUE);
2087
2088         return;
2089 }
2090
2091 /*
2092  * Stop the adapter and free any mbufs allocated to the
2093  * RX and TX lists.
2094  */
2095 static void
2096 ndis_stop(sc)
2097         struct ndis_softc               *sc;
2098 {
2099         struct ifnet            *ifp;
2100         NDIS_LOCK_INFO;
2101
2102         ifp = &sc->arpcom.ac_if;
2103         callout_stop(&sc->ndis_stat_timer);
2104
2105         ndis_halt_nic(sc);
2106
2107         NDIS_LOCK(sc);
2108         ifp->if_timer = 0;
2109         sc->ndis_link = 0;
2110         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2111         NDIS_UNLOCK(sc);
2112
2113         return;
2114 }
2115
2116 /*
2117  * Stop all chip I/O so that the kernel's probe routines don't
2118  * get confused by errant DMAs when rebooting.
2119  */
2120 void
2121 ndis_shutdown(dev)
2122         device_t                dev;
2123 {
2124         struct ndis_softc               *sc;
2125
2126         sc = device_get_softc(dev);
2127         ndis_shutdown_nic(sc);
2128
2129         return;
2130 }