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