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