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