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