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