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