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