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