]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/compat/ndis/kern_ndis.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / compat / ndis / kern_ndis.c
1 /*-
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/unistd.h>
39 #include <sys/types.h>
40 #include <sys/errno.h>
41 #include <sys/callout.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/conf.h>
50
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/kthread.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <sys/bus.h>
57 #include <sys/rman.h>
58
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_ioctl.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70
71 #include <compat/ndis/pe_var.h>
72 #include <compat/ndis/cfg_var.h>
73 #include <compat/ndis/resource_var.h>
74 #include <compat/ndis/ntoskrnl_var.h>
75 #include <compat/ndis/ndis_var.h>
76 #include <compat/ndis/hal_var.h>
77 #include <compat/ndis/usbd_var.h>
78 #include <dev/if_ndis/if_ndisvar.h>
79
80 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
81
82 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
83 static void ndis_statusdone_func(ndis_handle);
84 static void ndis_setdone_func(ndis_handle, ndis_status);
85 static void ndis_getdone_func(ndis_handle, ndis_status);
86 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
87 static void ndis_sendrsrcavail_func(ndis_handle);
88 static void ndis_intrsetup(kdpc *, device_object *,
89         irp *, struct ndis_softc *);
90 static void ndis_return(device_object *, void *);
91
92 static image_patch_table kernndis_functbl[] = {
93         IMPORT_SFUNC(ndis_status_func, 4),
94         IMPORT_SFUNC(ndis_statusdone_func, 1),
95         IMPORT_SFUNC(ndis_setdone_func, 2),
96         IMPORT_SFUNC(ndis_getdone_func, 2),
97         IMPORT_SFUNC(ndis_resetdone_func, 3),
98         IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
99         IMPORT_SFUNC(ndis_intrsetup, 4),
100         IMPORT_SFUNC(ndis_return, 1),
101
102         { NULL, NULL, NULL }
103 };
104
105 static struct nd_head ndis_devhead;
106
107 /*
108  * This allows us to export our symbols to other modules.
109  * Note that we call ourselves 'ndisapi' to avoid a namespace
110  * collision with if_ndis.ko, which internally calls itself
111  * 'ndis.'
112  *
113  * Note: some of the subsystems depend on each other, so the
114  * order in which they're started is important. The order of
115  * importance is:
116  *
117  * HAL - spinlocks and IRQL manipulation
118  * ntoskrnl - DPC and workitem threads, object waiting
119  * windrv - driver/device registration
120  *
121  * The HAL should also be the last thing shut down, since
122  * the ntoskrnl subsystem will use spinlocks right up until
123  * the DPC and workitem threads are terminated.
124  */
125
126 static int
127 ndis_modevent(module_t mod, int cmd, void *arg)
128 {
129         int                     error = 0;
130         image_patch_table       *patch;
131
132         switch (cmd) {
133         case MOD_LOAD:
134                 /* Initialize subsystems */
135                 hal_libinit();
136                 ntoskrnl_libinit();
137                 windrv_libinit();
138                 ndis_libinit();
139                 usbd_libinit();
140
141                 patch = kernndis_functbl;
142                 while (patch->ipt_func != NULL) {
143                         windrv_wrap((funcptr)patch->ipt_func,
144                             (funcptr *)&patch->ipt_wrap,
145                             patch->ipt_argcnt, patch->ipt_ftype);
146                         patch++;
147                 }
148
149                 TAILQ_INIT(&ndis_devhead);
150                 break;
151         case MOD_SHUTDOWN:
152                 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
153                         /* Shut down subsystems */
154                         ndis_libfini();
155                         usbd_libfini();
156                         windrv_libfini();
157                         ntoskrnl_libfini();
158                         hal_libfini();
159
160                         patch = kernndis_functbl;
161                         while (patch->ipt_func != NULL) {
162                                 windrv_unwrap(patch->ipt_wrap);
163                                 patch++;
164                         }
165                 }
166                 break;
167         case MOD_UNLOAD:
168                 /* Shut down subsystems */
169                 ndis_libfini();
170                 usbd_libfini();
171                 windrv_libfini();
172                 ntoskrnl_libfini();
173                 hal_libfini();
174
175                 patch = kernndis_functbl;
176                 while (patch->ipt_func != NULL) {
177                         windrv_unwrap(patch->ipt_wrap);
178                         patch++;
179                 }
180
181                 break;
182         default:
183                 error = EINVAL;
184                 break;
185         }
186
187         return (error);
188 }
189 DEV_MODULE(ndisapi, ndis_modevent, NULL);
190 MODULE_VERSION(ndisapi, 1);
191
192 static void
193 ndis_sendrsrcavail_func(adapter)
194         ndis_handle             adapter;
195 {
196 }
197
198 static void
199 ndis_status_func(adapter, status, sbuf, slen)
200         ndis_handle             adapter;
201         ndis_status             status;
202         void                    *sbuf;
203         uint32_t                slen;
204 {
205         ndis_miniport_block     *block;
206         struct ndis_softc       *sc;
207         struct ifnet            *ifp;
208
209         block = adapter;
210         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
211         ifp = sc->ifp;
212         if (ifp->if_flags & IFF_DEBUG)
213                 device_printf(sc->ndis_dev, "status: %x\n", status);
214 }
215
216 static void
217 ndis_statusdone_func(adapter)
218         ndis_handle             adapter;
219 {
220         ndis_miniport_block     *block;
221         struct ndis_softc       *sc;
222         struct ifnet            *ifp;
223
224         block = adapter;
225         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
226         ifp = sc->ifp;
227         if (ifp->if_flags & IFF_DEBUG)
228                 device_printf(sc->ndis_dev, "status complete\n");
229 }
230
231 static void
232 ndis_setdone_func(adapter, status)
233         ndis_handle             adapter;
234         ndis_status             status;
235 {
236         ndis_miniport_block     *block;
237         block = adapter;
238
239         block->nmb_setstat = status;
240         KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
241 }
242
243 static void
244 ndis_getdone_func(adapter, status)
245         ndis_handle             adapter;
246         ndis_status             status;
247 {
248         ndis_miniport_block     *block;
249         block = adapter;
250
251         block->nmb_getstat = status;
252         KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
253 }
254
255 static void
256 ndis_resetdone_func(ndis_handle adapter, ndis_status status,
257         uint8_t addressingreset)
258 {
259         ndis_miniport_block     *block;
260         struct ndis_softc       *sc;
261         struct ifnet            *ifp;
262
263         block = adapter;
264         sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
265         ifp = sc->ifp;
266
267         if (ifp->if_flags & IFF_DEBUG)
268                 device_printf(sc->ndis_dev, "reset done...\n");
269         KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
270 }
271
272 int
273 ndis_create_sysctls(arg)
274         void                    *arg;
275 {
276         struct ndis_softc       *sc;
277         ndis_cfg                *vals;
278         char                    buf[256];
279         struct sysctl_oid       *oidp;
280         struct sysctl_ctx_entry *e;
281
282         if (arg == NULL)
283                 return (EINVAL);
284
285         sc = arg;
286         vals = sc->ndis_regvals;
287
288         TAILQ_INIT(&sc->ndis_cfglist_head);
289
290         /* Add the driver-specific registry keys. */
291
292         while(1) {
293                 if (vals->nc_cfgkey == NULL)
294                         break;
295
296                 if (vals->nc_idx != sc->ndis_devidx) {
297                         vals++;
298                         continue;
299                 }
300
301                 /* See if we already have a sysctl with this name */
302
303                 oidp = NULL;
304                 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
305                         oidp = e->entry;
306                         if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
307                                 break;
308                         oidp = NULL;
309                 }
310
311                 if (oidp != NULL) {
312                         vals++;
313                         continue;
314                 }
315
316                 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
317                     vals->nc_val, CTLFLAG_RW);
318                 vals++;
319         }
320
321         /* Now add a couple of builtin keys. */
322
323         /*
324          * Environment can be either Windows (0) or WindowsNT (1).
325          * We qualify as the latter.
326          */
327         ndis_add_sysctl(sc, "Environment",
328             "Windows environment", "1", CTLFLAG_RD);
329
330         /* NDIS version should be 5.1. */
331         ndis_add_sysctl(sc, "NdisVersion",
332             "NDIS API Version", "0x00050001", CTLFLAG_RD);
333
334         /*
335          * Some miniport drivers rely on the existence of the SlotNumber,
336          * NetCfgInstanceId and DriverDesc keys.
337          */
338         ndis_add_sysctl(sc, "SlotNumber", "Slot Numer", "01", CTLFLAG_RD);
339         ndis_add_sysctl(sc, "NetCfgInstanceId", "NetCfgInstanceId",
340             "{12345678-1234-5678-CAFE0-123456789ABC}", CTLFLAG_RD);
341         ndis_add_sysctl(sc, "DriverDesc", "Driver Description",
342             "NDIS Network Adapter", CTLFLAG_RD);
343
344         /* Bus type (PCI, PCMCIA, etc...) */
345         sprintf(buf, "%d", (int)sc->ndis_iftype);
346         ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
347
348         if (sc->ndis_res_io != NULL) {
349                 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
350                 ndis_add_sysctl(sc, "IOBaseAddress",
351                     "Base I/O Address", buf, CTLFLAG_RD);
352         }
353
354         if (sc->ndis_irq != NULL) {
355                 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
356                 ndis_add_sysctl(sc, "InterruptNumber",
357                     "Interrupt Number", buf, CTLFLAG_RD);
358         }
359
360         return (0);
361 }
362
363 int
364 ndis_add_sysctl(arg, key, desc, val, flag)
365         void                    *arg;
366         char                    *key;
367         char                    *desc;
368         char                    *val;
369         int                     flag;
370 {
371         struct ndis_softc       *sc;
372         struct ndis_cfglist     *cfg;
373         char                    descstr[256];
374
375         sc = arg;
376
377         cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
378
379         if (cfg == NULL) {
380                 printf("failed for %s\n", key);
381                 return (ENOMEM);
382         }
383
384         cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
385         if (desc == NULL) {
386                 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
387                 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
388         } else
389                 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
390         strcpy(cfg->ndis_cfg.nc_val, val);
391
392         TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
393
394         cfg->ndis_oid =
395         SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
396             SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
397             OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
398             cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
399             cfg->ndis_cfg.nc_cfgdesc);
400
401         return (0);
402 }
403
404 /*
405  * Somewhere, somebody decided "hey, let's automatically create
406  * a sysctl tree for each device instance as it's created -- it'll
407  * make life so much easier!" Lies. Why must they turn the kernel
408  * into a house of lies?
409  */
410
411 int
412 ndis_flush_sysctls(arg)
413         void                    *arg;
414 {
415         struct ndis_softc       *sc;
416         struct ndis_cfglist     *cfg;
417         struct sysctl_ctx_list  *clist;
418
419         sc = arg;
420
421         clist = device_get_sysctl_ctx(sc->ndis_dev);
422
423         while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
424                 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
425                 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
426                 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
427                 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
428                 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
429                 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
430                 free(cfg, M_DEVBUF);
431         }
432
433         return (0);
434 }
435
436 void *
437 ndis_get_routine_address(functbl, name)
438         struct image_patch_table *functbl;
439         char                    *name;
440 {
441         int                     i;
442
443         for (i = 0; functbl[i].ipt_name != NULL; i++)
444                 if (strcmp(name, functbl[i].ipt_name) == 0)
445                         return (functbl[i].ipt_wrap);
446         return (NULL);
447 }
448
449 static void
450 ndis_return(dobj, arg)
451         device_object           *dobj;
452         void                    *arg;
453 {
454         ndis_miniport_block     *block;
455         ndis_miniport_characteristics   *ch;
456         ndis_return_handler     returnfunc;
457         ndis_handle             adapter;
458         ndis_packet             *p;
459         uint8_t                 irql;
460         list_entry              *l;
461
462         block = arg;
463         ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
464
465         p = arg;
466         adapter = block->nmb_miniportadapterctx;
467
468         if (adapter == NULL)
469                 return;
470
471         returnfunc = ch->nmc_return_packet_func;
472
473         KeAcquireSpinLock(&block->nmb_returnlock, &irql);
474         while (!IsListEmpty(&block->nmb_returnlist)) {
475                 l = RemoveHeadList((&block->nmb_returnlist));
476                 p = CONTAINING_RECORD(l, ndis_packet, np_list);
477                 InitializeListHead((&p->np_list));
478                 KeReleaseSpinLock(&block->nmb_returnlock, irql);
479                 MSCALL2(returnfunc, adapter, p);
480                 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
481         }
482         KeReleaseSpinLock(&block->nmb_returnlock, irql);
483 }
484
485 int
486 ndis_return_packet(struct mbuf *m, void *buf, void *arg)
487 {
488         ndis_packet             *p;
489         ndis_miniport_block     *block;
490
491         if (arg == NULL)
492                 return (EXT_FREE_OK);
493
494         p = arg;
495
496         /* Decrement refcount. */
497         p->np_refcnt--;
498
499         /* Release packet when refcount hits zero, otherwise return. */
500         if (p->np_refcnt)
501                 return (EXT_FREE_OK);
502
503         block = ((struct ndis_softc *)p->np_softc)->ndis_block;
504
505         KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
506         InitializeListHead((&p->np_list));
507         InsertHeadList((&block->nmb_returnlist), (&p->np_list));
508         KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
509
510         IoQueueWorkItem(block->nmb_returnitem,
511             (io_workitem_func)kernndis_functbl[7].ipt_wrap,
512             WORKQUEUE_CRITICAL, block);
513
514         return (EXT_FREE_OK);
515 }
516
517 void
518 ndis_free_bufs(b0)
519         ndis_buffer             *b0;
520 {
521         ndis_buffer             *next;
522
523         if (b0 == NULL)
524                 return;
525
526         while(b0 != NULL) {
527                 next = b0->mdl_next;
528                 IoFreeMdl(b0);
529                 b0 = next;
530         }
531 }
532
533 void
534 ndis_free_packet(p)
535         ndis_packet             *p;
536 {
537         if (p == NULL)
538                 return;
539
540         ndis_free_bufs(p->np_private.npp_head);
541         NdisFreePacket(p);
542 }
543
544 int
545 ndis_convert_res(arg)
546         void                    *arg;
547 {
548         struct ndis_softc       *sc;
549         ndis_resource_list      *rl = NULL;
550         cm_partial_resource_desc        *prd = NULL;
551         ndis_miniport_block     *block;
552         device_t                dev;
553         struct resource_list    *brl;
554         struct resource_list_entry      *brle;
555         int                     error = 0;
556
557         sc = arg;
558         block = sc->ndis_block;
559         dev = sc->ndis_dev;
560
561         rl = malloc(sizeof(ndis_resource_list) +
562             (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
563             M_DEVBUF, M_NOWAIT|M_ZERO);
564
565         if (rl == NULL)
566                 return (ENOMEM);
567
568         rl->cprl_version = 5;
569         rl->cprl_revision = 1;
570         rl->cprl_count = sc->ndis_rescnt;
571         prd = rl->cprl_partial_descs;
572
573         brl = BUS_GET_RESOURCE_LIST(dev, dev);
574
575         if (brl != NULL) {
576
577                 STAILQ_FOREACH(brle, brl, link) {
578                         switch (brle->type) {
579                         case SYS_RES_IOPORT:
580                                 prd->cprd_type = CmResourceTypePort;
581                                 prd->cprd_flags = CM_RESOURCE_PORT_IO;
582                                 prd->cprd_sharedisp =
583                                     CmResourceShareDeviceExclusive;
584                                 prd->u.cprd_port.cprd_start.np_quad =
585                                     brle->start;
586                                 prd->u.cprd_port.cprd_len = brle->count;
587                                 break;
588                         case SYS_RES_MEMORY:
589                                 prd->cprd_type = CmResourceTypeMemory;
590                                 prd->cprd_flags =
591                                     CM_RESOURCE_MEMORY_READ_WRITE;
592                                 prd->cprd_sharedisp =
593                                     CmResourceShareDeviceExclusive;
594                                 prd->u.cprd_mem.cprd_start.np_quad =
595                                     brle->start;
596                                 prd->u.cprd_mem.cprd_len = brle->count;
597                                 break;
598                         case SYS_RES_IRQ:
599                                 prd->cprd_type = CmResourceTypeInterrupt;
600                                 prd->cprd_flags = 0;
601                                 /*
602                                  * Always mark interrupt resources as
603                                  * shared, since in our implementation,
604                                  * they will be.
605                                  */
606                                 prd->cprd_sharedisp =
607                                     CmResourceShareShared;
608                                 prd->u.cprd_intr.cprd_level = brle->start;
609                                 prd->u.cprd_intr.cprd_vector = brle->start;
610                                 prd->u.cprd_intr.cprd_affinity = 0;
611                                 break;
612                         default:
613                                 break;
614                         }
615                         prd++;
616                 }
617         }
618
619         block->nmb_rlist = rl;
620
621         return (error);
622 }
623
624 /*
625  * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
626  * packet, it will hand it to us in the form of an ndis_packet,
627  * which we need to convert to an mbuf that is then handed off
628  * to the stack. Note: we configure the mbuf list so that it uses
629  * the memory regions specified by the ndis_buffer structures in
630  * the ndis_packet as external storage. In most cases, this will
631  * point to a memory region allocated by the driver (either by
632  * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
633  * the driver to handle free()ing this region for is, so we set up
634  * a dummy no-op free handler for it.
635  */ 
636
637 int
638 ndis_ptom(m0, p)
639         struct mbuf             **m0;
640         ndis_packet             *p;
641 {
642         struct mbuf             *m = NULL, *prev = NULL;
643         ndis_buffer             *buf;
644         ndis_packet_private     *priv;
645         uint32_t                totlen = 0;
646         struct ifnet            *ifp;
647         struct ether_header     *eh;
648         int                     diff;
649
650         if (p == NULL || m0 == NULL)
651                 return (EINVAL);
652
653         priv = &p->np_private;
654         buf = priv->npp_head;
655         p->np_refcnt = 0;
656
657         for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
658                 if (buf == priv->npp_head)
659                         m = m_gethdr(M_NOWAIT, MT_DATA);
660                 else
661                         m = m_get(M_NOWAIT, MT_DATA);
662                 if (m == NULL) {
663                         m_freem(*m0);
664                         *m0 = NULL;
665                         return (ENOBUFS);
666                 }
667                 m->m_len = MmGetMdlByteCount(buf);
668                 m->m_data = MmGetMdlVirtualAddress(buf);
669                 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
670                     m->m_data, p, 0, EXT_NDIS);
671                 p->np_refcnt++;
672
673                 totlen += m->m_len;
674                 if (m->m_flags & M_PKTHDR)
675                         *m0 = m;
676                 else
677                         prev->m_next = m;
678                 prev = m;
679         }
680
681         /*
682          * This is a hack to deal with the Marvell 8335 driver
683          * which, when associated with an AP in WPA-PSK mode,
684          * seems to overpad its frames by 8 bytes. I don't know
685          * that the extra 8 bytes are for, and they're not there
686          * in open mode, so for now clamp the frame size at 1514
687          * until I can figure out how to deal with this properly,
688          * otherwise if_ethersubr() will spank us by discarding
689          * the 'oversize' frames.
690          */
691
692         eh = mtod((*m0), struct ether_header *);
693         ifp = ((struct ndis_softc *)p->np_softc)->ifp;
694         if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
695                 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
696                 totlen -= diff;
697                 m->m_len -= diff;
698         }
699         (*m0)->m_pkthdr.len = totlen;
700
701         return (0);
702 }
703
704 /*
705  * Create an NDIS packet from an mbuf chain.
706  * This is used mainly when transmitting packets, where we need
707  * to turn an mbuf off an interface's send queue and transform it
708  * into an NDIS packet which will be fed into the NDIS driver's
709  * send routine.
710  *
711  * NDIS packets consist of two parts: an ndis_packet structure,
712  * which is vaguely analagous to the pkthdr portion of an mbuf,
713  * and one or more ndis_buffer structures, which define the
714  * actual memory segments in which the packet data resides.
715  * We need to allocate one ndis_buffer for each mbuf in a chain,
716  * plus one ndis_packet as the header.
717  */
718
719 int
720 ndis_mtop(m0, p)
721         struct mbuf             *m0;
722         ndis_packet             **p;
723 {
724         struct mbuf             *m;
725         ndis_buffer             *buf = NULL, *prev = NULL;
726         ndis_packet_private     *priv;
727
728         if (p == NULL || *p == NULL || m0 == NULL)
729                 return (EINVAL);
730
731         priv = &(*p)->np_private;
732         priv->npp_totlen = m0->m_pkthdr.len;
733
734         for (m = m0; m != NULL; m = m->m_next) {
735                 if (m->m_len == 0)
736                         continue;
737                 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
738                 if (buf == NULL) {
739                         ndis_free_packet(*p);
740                         *p = NULL;
741                         return (ENOMEM);
742                 }
743                 MmBuildMdlForNonPagedPool(buf);
744
745                 if (priv->npp_head == NULL)
746                         priv->npp_head = buf;
747                 else
748                         prev->mdl_next = buf;
749                 prev = buf;
750         }
751
752         priv->npp_tail = buf;
753
754         return (0);
755 }
756
757 int
758 ndis_get_supported_oids(arg, oids, oidcnt)
759         void                    *arg;
760         ndis_oid                **oids;
761         int                     *oidcnt;
762 {
763         int                     len, rval;
764         ndis_oid                *o;
765
766         if (arg == NULL || oids == NULL || oidcnt == NULL)
767                 return (EINVAL);
768         len = 0;
769         ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
770
771         o = malloc(len, M_DEVBUF, M_NOWAIT);
772         if (o == NULL)
773                 return (ENOMEM);
774
775         rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
776
777         if (rval) {
778                 free(o, M_DEVBUF);
779                 return (rval);
780         }
781
782         *oids = o;
783         *oidcnt = len / 4;
784
785         return (0);
786 }
787
788 int
789 ndis_set_info(arg, oid, buf, buflen)
790         void                    *arg;
791         ndis_oid                oid;
792         void                    *buf;
793         int                     *buflen;
794 {
795         struct ndis_softc       *sc;
796         ndis_status             rval;
797         ndis_handle             adapter;
798         ndis_setinfo_handler    setfunc;
799         uint32_t                byteswritten = 0, bytesneeded = 0;
800         uint8_t                 irql;
801         uint64_t                duetime;
802
803         /*
804          * According to the NDIS spec, MiniportQueryInformation()
805          * and MiniportSetInformation() requests are handled serially:
806          * once one request has been issued, we must wait for it to
807          * finish before allowing another request to proceed.
808          */
809
810         sc = arg;
811
812         KeResetEvent(&sc->ndis_block->nmb_setevent);
813
814         KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
815
816         if (sc->ndis_block->nmb_pendingreq != NULL) {
817                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
818                 panic("ndis_set_info() called while other request pending");
819         } else
820                 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
821
822         setfunc = sc->ndis_chars->nmc_setinfo_func;
823         adapter = sc->ndis_block->nmb_miniportadapterctx;
824
825         if (adapter == NULL || setfunc == NULL ||
826             sc->ndis_block->nmb_devicectx == NULL) {
827                 sc->ndis_block->nmb_pendingreq = NULL;
828                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
829                 return (ENXIO);
830         }
831
832         rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
833             &byteswritten, &bytesneeded);
834
835         sc->ndis_block->nmb_pendingreq = NULL;
836
837         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
838
839         if (rval == NDIS_STATUS_PENDING) {
840                 /* Wait up to 5 seconds. */
841                 duetime = (5 * 1000000) * -10;
842                 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
843                     0, 0, FALSE, &duetime);
844                 rval = sc->ndis_block->nmb_setstat;
845         }
846
847         if (byteswritten)
848                 *buflen = byteswritten;
849         if (bytesneeded)
850                 *buflen = bytesneeded;
851
852         if (rval == NDIS_STATUS_INVALID_LENGTH)
853                 return (ENOSPC);
854
855         if (rval == NDIS_STATUS_INVALID_OID)
856                 return (EINVAL);
857
858         if (rval == NDIS_STATUS_NOT_SUPPORTED ||
859             rval == NDIS_STATUS_NOT_ACCEPTED)
860                 return (ENOTSUP);
861
862         if (rval != NDIS_STATUS_SUCCESS)
863                 return (ENODEV);
864
865         return (0);
866 }
867
868 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
869
870 int
871 ndis_send_packets(arg, packets, cnt)
872         void                    *arg;
873         ndis_packet             **packets;
874         int                     cnt;
875 {
876         struct ndis_softc       *sc;
877         ndis_handle             adapter;
878         ndis_sendmulti_handler  sendfunc;
879         ndis_senddone_func              senddonefunc;
880         int                     i;
881         ndis_packet             *p;
882         uint8_t                 irql = 0;
883
884         sc = arg;
885         adapter = sc->ndis_block->nmb_miniportadapterctx;
886         if (adapter == NULL)
887                 return (ENXIO);
888         sendfunc = sc->ndis_chars->nmc_sendmulti_func;
889         senddonefunc = sc->ndis_block->nmb_senddone_func;
890
891         if (NDIS_SERIALIZED(sc->ndis_block))
892                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
893
894         MSCALL3(sendfunc, adapter, packets, cnt);
895
896         for (i = 0; i < cnt; i++) {
897                 p = packets[i];
898                 /*
899                  * Either the driver already handed the packet to
900                  * ndis_txeof() due to a failure, or it wants to keep
901                  * it and release it asynchronously later. Skip to the
902                  * next one.
903                  */
904                 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
905                         continue;
906                 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
907         }
908
909         if (NDIS_SERIALIZED(sc->ndis_block))
910                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
911
912         return (0);
913 }
914
915 int
916 ndis_send_packet(arg, packet)
917         void                    *arg;
918         ndis_packet             *packet;
919 {
920         struct ndis_softc       *sc;
921         ndis_handle             adapter;
922         ndis_status             status;
923         ndis_sendsingle_handler sendfunc;
924         ndis_senddone_func              senddonefunc;
925         uint8_t                 irql = 0;
926
927         sc = arg;
928         adapter = sc->ndis_block->nmb_miniportadapterctx;
929         if (adapter == NULL)
930                 return (ENXIO);
931         sendfunc = sc->ndis_chars->nmc_sendsingle_func;
932         senddonefunc = sc->ndis_block->nmb_senddone_func;
933
934         if (NDIS_SERIALIZED(sc->ndis_block))
935                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
936         status = MSCALL3(sendfunc, adapter, packet,
937             packet->np_private.npp_flags);
938
939         if (status == NDIS_STATUS_PENDING) {
940                 if (NDIS_SERIALIZED(sc->ndis_block))
941                         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
942                 return (0);
943         }
944
945         MSCALL3(senddonefunc, sc->ndis_block, packet, status);
946
947         if (NDIS_SERIALIZED(sc->ndis_block))
948                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
949
950         return (0);
951 }
952
953 int
954 ndis_init_dma(arg)
955         void                    *arg;
956 {
957         struct ndis_softc       *sc;
958         int                     i, error;
959
960         sc = arg;
961
962         sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
963             M_DEVBUF, M_NOWAIT|M_ZERO);
964
965         if (sc->ndis_tmaps == NULL)
966                 return (ENOMEM);
967
968         for (i = 0; i < sc->ndis_maxpkts; i++) {
969                 error = bus_dmamap_create(sc->ndis_ttag, 0,
970                     &sc->ndis_tmaps[i]);
971                 if (error) {
972                         free(sc->ndis_tmaps, M_DEVBUF);
973                         return (ENODEV);
974                 }
975         }
976
977         return (0);
978 }
979
980 int
981 ndis_destroy_dma(arg)
982         void                    *arg;
983 {
984         struct ndis_softc       *sc;
985         struct mbuf             *m;
986         ndis_packet             *p = NULL;
987         int                     i;
988
989         sc = arg;
990
991         for (i = 0; i < sc->ndis_maxpkts; i++) {
992                 if (sc->ndis_txarray[i] != NULL) {
993                         p = sc->ndis_txarray[i];
994                         m = (struct mbuf *)p->np_rsvd[1];
995                         if (m != NULL)
996                                 m_freem(m);
997                         ndis_free_packet(sc->ndis_txarray[i]);
998                 }
999                 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
1000         }
1001
1002         free(sc->ndis_tmaps, M_DEVBUF);
1003
1004         bus_dma_tag_destroy(sc->ndis_ttag);
1005
1006         return (0);
1007 }
1008
1009 int
1010 ndis_reset_nic(arg)
1011         void                    *arg;
1012 {
1013         struct ndis_softc       *sc;
1014         ndis_handle             adapter;
1015         ndis_reset_handler      resetfunc;
1016         uint8_t                 addressing_reset;
1017         int                     rval;
1018         uint8_t                 irql = 0;
1019
1020         sc = arg;
1021
1022         NDIS_LOCK(sc);
1023         adapter = sc->ndis_block->nmb_miniportadapterctx;
1024         resetfunc = sc->ndis_chars->nmc_reset_func;
1025
1026         if (adapter == NULL || resetfunc == NULL ||
1027             sc->ndis_block->nmb_devicectx == NULL) {
1028                 NDIS_UNLOCK(sc);
1029                 return (EIO);
1030         }
1031
1032         NDIS_UNLOCK(sc);
1033
1034         KeResetEvent(&sc->ndis_block->nmb_resetevent);
1035
1036         if (NDIS_SERIALIZED(sc->ndis_block))
1037                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1038
1039         rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1040
1041         if (NDIS_SERIALIZED(sc->ndis_block))
1042                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1043
1044         if (rval == NDIS_STATUS_PENDING)
1045                 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1046                     0, 0, FALSE, NULL);
1047
1048         return (0);
1049 }
1050
1051 int
1052 ndis_halt_nic(arg)
1053         void                    *arg;
1054 {
1055         struct ndis_softc       *sc;
1056         ndis_handle             adapter;
1057         ndis_halt_handler       haltfunc;
1058         ndis_miniport_block     *block;
1059         int                     empty = 0;
1060         uint8_t                 irql;
1061
1062         sc = arg;
1063         block = sc->ndis_block;
1064
1065         if (!cold)
1066                 KeFlushQueuedDpcs();
1067
1068         /*
1069          * Wait for all packets to be returned.
1070          */
1071
1072         while (1) {
1073                 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1074                 empty = IsListEmpty(&block->nmb_returnlist);
1075                 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1076                 if (empty)
1077                         break;
1078                 NdisMSleep(1000);
1079         }
1080
1081         NDIS_LOCK(sc);
1082         adapter = sc->ndis_block->nmb_miniportadapterctx;
1083         if (adapter == NULL) {
1084                 NDIS_UNLOCK(sc);
1085                 return (EIO);
1086         }
1087
1088         sc->ndis_block->nmb_devicectx = NULL;
1089
1090         /*
1091          * The adapter context is only valid after the init
1092          * handler has been called, and is invalid once the
1093          * halt handler has been called.
1094          */
1095
1096         haltfunc = sc->ndis_chars->nmc_halt_func;
1097         NDIS_UNLOCK(sc);
1098
1099         MSCALL1(haltfunc, adapter);
1100
1101         NDIS_LOCK(sc);
1102         sc->ndis_block->nmb_miniportadapterctx = NULL;
1103         NDIS_UNLOCK(sc);
1104
1105         return (0);
1106 }
1107
1108 int
1109 ndis_shutdown_nic(arg)
1110         void                    *arg;
1111 {
1112         struct ndis_softc       *sc;
1113         ndis_handle             adapter;
1114         ndis_shutdown_handler   shutdownfunc;
1115
1116         sc = arg;
1117         NDIS_LOCK(sc);
1118         adapter = sc->ndis_block->nmb_miniportadapterctx;
1119         shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1120         NDIS_UNLOCK(sc);
1121         if (adapter == NULL || shutdownfunc == NULL)
1122                 return (EIO);
1123
1124         if (sc->ndis_chars->nmc_rsvd0 == NULL)
1125                 MSCALL1(shutdownfunc, adapter);
1126         else
1127                 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1128
1129         TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1130
1131         return (0);
1132 }
1133
1134 int
1135 ndis_pnpevent_nic(arg, type)
1136         void                    *arg;
1137         int                     type;
1138 {
1139         device_t                dev;
1140         struct ndis_softc       *sc;
1141         ndis_handle             adapter;
1142         ndis_pnpevent_handler   pnpeventfunc;
1143
1144         dev = arg;
1145         sc = device_get_softc(arg);
1146         NDIS_LOCK(sc);
1147         adapter = sc->ndis_block->nmb_miniportadapterctx;
1148         pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1149         NDIS_UNLOCK(sc);
1150         if (adapter == NULL || pnpeventfunc == NULL)
1151                 return (EIO);
1152
1153         if (sc->ndis_chars->nmc_rsvd0 == NULL)
1154                 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1155         else
1156                 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1157
1158         return (0);
1159 }
1160
1161 int
1162 ndis_init_nic(arg)
1163         void                    *arg;
1164 {
1165         struct ndis_softc       *sc;
1166         ndis_miniport_block     *block;
1167         ndis_init_handler       initfunc;
1168         ndis_status             status, openstatus = 0;
1169         ndis_medium             mediumarray[NdisMediumMax];
1170         uint32_t                chosenmedium, i;
1171
1172         if (arg == NULL)
1173                 return (EINVAL);
1174
1175         sc = arg;
1176         NDIS_LOCK(sc);
1177         block = sc->ndis_block;
1178         initfunc = sc->ndis_chars->nmc_init_func;
1179         NDIS_UNLOCK(sc);
1180
1181         sc->ndis_block->nmb_timerlist = NULL;
1182
1183         for (i = 0; i < NdisMediumMax; i++)
1184                 mediumarray[i] = i;
1185
1186         status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1187             mediumarray, NdisMediumMax, block, block);
1188
1189         /*
1190          * If the init fails, blow away the other exported routines
1191          * we obtained from the driver so we can't call them later.
1192          * If the init failed, none of these will work.
1193          */
1194         if (status != NDIS_STATUS_SUCCESS) {
1195                 NDIS_LOCK(sc);
1196                 sc->ndis_block->nmb_miniportadapterctx = NULL;
1197                 NDIS_UNLOCK(sc);
1198                 return (ENXIO);
1199         }
1200
1201         /*
1202          * This may look really goofy, but apparently it is possible
1203          * to halt a miniport too soon after it's been initialized.
1204          * After MiniportInitialize() finishes, pause for 1 second
1205          * to give the chip a chance to handle any short-lived timers
1206          * that were set in motion. If we call MiniportHalt() too soon,
1207          * some of the timers may not be cancelled, because the driver
1208          * expects them to fire before the halt is called.
1209          */
1210
1211         pause("ndwait", hz);
1212
1213         NDIS_LOCK(sc);
1214         sc->ndis_block->nmb_devicectx = sc;
1215         NDIS_UNLOCK(sc);
1216
1217         return (0);
1218 }
1219
1220 static void
1221 ndis_intrsetup(dpc, dobj, ip, sc)
1222         kdpc                    *dpc;
1223         device_object           *dobj;
1224         irp                     *ip;
1225         struct ndis_softc       *sc;
1226 {
1227         ndis_miniport_interrupt *intr;
1228
1229         intr = sc->ndis_block->nmb_interrupt;
1230
1231         /* Sanity check. */
1232
1233         if (intr == NULL)
1234                 return;
1235
1236         KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1237         KeResetEvent(&intr->ni_dpcevt);
1238         if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1239                 intr->ni_dpccnt++;
1240         KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1241 }
1242
1243 int
1244 ndis_get_info(arg, oid, buf, buflen)
1245         void                    *arg;
1246         ndis_oid                oid;
1247         void                    *buf;
1248         int                     *buflen;
1249 {
1250         struct ndis_softc       *sc;
1251         ndis_status             rval;
1252         ndis_handle             adapter;
1253         ndis_queryinfo_handler  queryfunc;
1254         uint32_t                byteswritten = 0, bytesneeded = 0;
1255         uint8_t                 irql;
1256         uint64_t                duetime;
1257
1258         sc = arg;
1259
1260         KeResetEvent(&sc->ndis_block->nmb_getevent);
1261
1262         KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1263
1264         if (sc->ndis_block->nmb_pendingreq != NULL) {
1265                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1266                 panic("ndis_get_info() called while other request pending");
1267         } else
1268                 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1269
1270         queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1271         adapter = sc->ndis_block->nmb_miniportadapterctx;
1272
1273         if (adapter == NULL || queryfunc == NULL ||
1274             sc->ndis_block->nmb_devicectx == NULL) {
1275                 sc->ndis_block->nmb_pendingreq = NULL;
1276                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1277                 return (ENXIO);
1278         }
1279
1280         rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1281             &byteswritten, &bytesneeded);
1282
1283         sc->ndis_block->nmb_pendingreq = NULL;
1284
1285         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1286
1287         /* Wait for requests that block. */
1288
1289         if (rval == NDIS_STATUS_PENDING) {
1290                 /* Wait up to 5 seconds. */
1291                 duetime = (5 * 1000000) * -10;
1292                 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1293                     0, 0, FALSE, &duetime);
1294                 rval = sc->ndis_block->nmb_getstat;
1295         }
1296
1297         if (byteswritten)
1298                 *buflen = byteswritten;
1299         if (bytesneeded)
1300                 *buflen = bytesneeded;
1301
1302         if (rval == NDIS_STATUS_INVALID_LENGTH ||
1303             rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1304                 return (ENOSPC);
1305
1306         if (rval == NDIS_STATUS_INVALID_OID)
1307                 return (EINVAL);
1308
1309         if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1310             rval == NDIS_STATUS_NOT_ACCEPTED)
1311                 return (ENOTSUP);
1312
1313         if (rval != NDIS_STATUS_SUCCESS)
1314                 return (ENODEV);
1315
1316         return (0);
1317 }
1318
1319 uint32_t
1320 NdisAddDevice(drv, pdo)
1321         driver_object           *drv;
1322         device_object           *pdo;
1323 {
1324         device_object           *fdo;
1325         ndis_miniport_block     *block;
1326         struct ndis_softc       *sc;
1327         uint32_t                status;
1328         int                     error;
1329
1330         sc = device_get_softc(pdo->do_devext);
1331
1332         if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1333                 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1334                     INTR_TYPE_NET | INTR_MPSAFE,
1335                     NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1336                 if (error)
1337                         return (NDIS_STATUS_FAILURE);
1338         }
1339
1340         status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1341             FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1342
1343         if (status != STATUS_SUCCESS)
1344                 return (status);
1345
1346         block = fdo->do_devext;
1347
1348         block->nmb_filterdbs.nf_ethdb = block;
1349         block->nmb_deviceobj = fdo;
1350         block->nmb_physdeviceobj = pdo;
1351         block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1352         KeInitializeSpinLock(&block->nmb_lock);
1353         KeInitializeSpinLock(&block->nmb_returnlock);
1354         KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1355         KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1356         KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1357         InitializeListHead(&block->nmb_parmlist);
1358         InitializeListHead(&block->nmb_returnlist);
1359         block->nmb_returnitem = IoAllocateWorkItem(fdo);
1360
1361         /*
1362          * Stash pointers to the miniport block and miniport
1363          * characteristics info in the if_ndis softc so the
1364          * UNIX wrapper driver can get to them later.
1365          */
1366         sc->ndis_block = block;
1367         sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1368
1369         /*
1370          * If the driver has a MiniportTransferData() function,
1371          * we should allocate a private RX packet pool.
1372          */
1373
1374         if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1375                 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1376                     32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1377                 if (status != NDIS_STATUS_SUCCESS) {
1378                         IoDetachDevice(block->nmb_nextdeviceobj);
1379                         IoDeleteDevice(fdo);
1380                         return (status);
1381                 }
1382                 InitializeListHead((&block->nmb_packetlist));
1383         }
1384
1385         /* Give interrupt handling priority over timers. */
1386         IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1387         KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1388
1389         /* Finish up BSD-specific setup. */
1390
1391         block->nmb_signature = (void *)0xcafebabe;
1392         block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1393         block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1394         block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1395         block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1396         block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1397         block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1398         block->nmb_pendingreq = NULL;
1399
1400         TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1401
1402         return (STATUS_SUCCESS);
1403 }
1404
1405 int
1406 ndis_unload_driver(arg)
1407         void                    *arg;
1408 {
1409         struct ndis_softc       *sc;
1410         device_object           *fdo;
1411
1412         sc = arg;
1413
1414         if (sc->ndis_intrhand)
1415                 bus_teardown_intr(sc->ndis_dev,
1416                     sc->ndis_irq, sc->ndis_intrhand);
1417
1418         if (sc->ndis_block->nmb_rlist != NULL)
1419                 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1420
1421         ndis_flush_sysctls(sc);
1422
1423         TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1424
1425         if (sc->ndis_chars->nmc_transferdata_func != NULL)
1426                 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1427         fdo = sc->ndis_block->nmb_deviceobj;
1428         IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1429         IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1430         IoDeleteDevice(fdo);
1431
1432         return (0);
1433 }