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