]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/compat/ndis/kern_ndis.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.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         /* Bus type (PCI, PCMCIA, etc...) */
335         sprintf(buf, "%d", (int)sc->ndis_iftype);
336         ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
337
338         if (sc->ndis_res_io != NULL) {
339                 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
340                 ndis_add_sysctl(sc, "IOBaseAddress",
341                     "Base I/O Address", buf, CTLFLAG_RD);
342         }
343
344         if (sc->ndis_irq != NULL) {
345                 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
346                 ndis_add_sysctl(sc, "InterruptNumber",
347                     "Interrupt Number", buf, CTLFLAG_RD);
348         }
349
350         return (0);
351 }
352
353 int
354 ndis_add_sysctl(arg, key, desc, val, flag)
355         void                    *arg;
356         char                    *key;
357         char                    *desc;
358         char                    *val;
359         int                     flag;
360 {
361         struct ndis_softc       *sc;
362         struct ndis_cfglist     *cfg;
363         char                    descstr[256];
364
365         sc = arg;
366
367         cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
368
369         if (cfg == NULL) {
370                 printf("failed for %s\n", key);
371                 return (ENOMEM);
372         }
373
374         cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
375         if (desc == NULL) {
376                 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
377                 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
378         } else
379                 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
380         strcpy(cfg->ndis_cfg.nc_val, val);
381
382         TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
383
384         cfg->ndis_oid =
385         SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
386             SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
387             OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
388             cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
389             cfg->ndis_cfg.nc_cfgdesc);
390
391         return (0);
392 }
393
394 /*
395  * Somewhere, somebody decided "hey, let's automatically create
396  * a sysctl tree for each device instance as it's created -- it'll
397  * make life so much easier!" Lies. Why must they turn the kernel
398  * into a house of lies?
399  */
400
401 int
402 ndis_flush_sysctls(arg)
403         void                    *arg;
404 {
405         struct ndis_softc       *sc;
406         struct ndis_cfglist     *cfg;
407         struct sysctl_ctx_list  *clist;
408
409         sc = arg;
410
411         clist = device_get_sysctl_ctx(sc->ndis_dev);
412
413         while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
414                 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
415                 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
416                 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
417                 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
418                 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
419                 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
420                 free(cfg, M_DEVBUF);
421         }
422
423         return (0);
424 }
425
426 static void
427 ndis_return(dobj, arg)
428         device_object           *dobj;
429         void                    *arg;
430 {
431         ndis_miniport_block     *block;
432         ndis_miniport_characteristics   *ch;
433         ndis_return_handler     returnfunc;
434         ndis_handle             adapter;
435         ndis_packet             *p;
436         uint8_t                 irql;
437         list_entry              *l;
438
439         block = arg;
440         ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
441
442         p = arg;
443         adapter = block->nmb_miniportadapterctx;
444
445         if (adapter == NULL)
446                 return;
447
448         returnfunc = ch->nmc_return_packet_func;
449
450         KeAcquireSpinLock(&block->nmb_returnlock, &irql);
451         while (!IsListEmpty(&block->nmb_returnlist)) {
452                 l = RemoveHeadList((&block->nmb_returnlist));
453                 p = CONTAINING_RECORD(l, ndis_packet, np_list);
454                 InitializeListHead((&p->np_list));
455                 KeReleaseSpinLock(&block->nmb_returnlock, irql);
456                 MSCALL2(returnfunc, adapter, p);
457                 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
458         }
459         KeReleaseSpinLock(&block->nmb_returnlock, irql);
460 }
461
462 void
463 ndis_return_packet(buf, arg)
464         void                    *buf;   /* not used */
465         void                    *arg;
466 {
467         ndis_packet             *p;
468         ndis_miniport_block     *block;
469
470         if (arg == NULL)
471                 return;
472
473         p = arg;
474
475         /* Decrement refcount. */
476         p->np_refcnt--;
477
478         /* Release packet when refcount hits zero, otherwise return. */
479         if (p->np_refcnt)
480                 return;
481
482         block = ((struct ndis_softc *)p->np_softc)->ndis_block;
483
484         KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
485         InitializeListHead((&p->np_list));
486         InsertHeadList((&block->nmb_returnlist), (&p->np_list));
487         KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
488
489         IoQueueWorkItem(block->nmb_returnitem,
490             (io_workitem_func)kernndis_functbl[7].ipt_wrap,
491             WORKQUEUE_CRITICAL, block);
492 }
493
494 void
495 ndis_free_bufs(b0)
496         ndis_buffer             *b0;
497 {
498         ndis_buffer             *next;
499
500         if (b0 == NULL)
501                 return;
502
503         while(b0 != NULL) {
504                 next = b0->mdl_next;
505                 IoFreeMdl(b0);
506                 b0 = next;
507         }
508 }
509
510 void
511 ndis_free_packet(p)
512         ndis_packet             *p;
513 {
514         if (p == NULL)
515                 return;
516
517         ndis_free_bufs(p->np_private.npp_head);
518         NdisFreePacket(p);
519 }
520
521 int
522 ndis_convert_res(arg)
523         void                    *arg;
524 {
525         struct ndis_softc       *sc;
526         ndis_resource_list      *rl = NULL;
527         cm_partial_resource_desc        *prd = NULL;
528         ndis_miniport_block     *block;
529         device_t                dev;
530         struct resource_list    *brl;
531         struct resource_list_entry      *brle;
532         int                     error = 0;
533
534         sc = arg;
535         block = sc->ndis_block;
536         dev = sc->ndis_dev;
537
538         rl = malloc(sizeof(ndis_resource_list) +
539             (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
540             M_DEVBUF, M_NOWAIT|M_ZERO);
541
542         if (rl == NULL)
543                 return (ENOMEM);
544
545         rl->cprl_version = 5;
546         rl->cprl_version = 1;
547         rl->cprl_count = sc->ndis_rescnt;
548         prd = rl->cprl_partial_descs;
549
550         brl = BUS_GET_RESOURCE_LIST(dev, dev);
551
552         if (brl != NULL) {
553
554                 STAILQ_FOREACH(brle, brl, link) {
555                         switch (brle->type) {
556                         case SYS_RES_IOPORT:
557                                 prd->cprd_type = CmResourceTypePort;
558                                 prd->cprd_flags = CM_RESOURCE_PORT_IO;
559                                 prd->cprd_sharedisp =
560                                     CmResourceShareDeviceExclusive;
561                                 prd->u.cprd_port.cprd_start.np_quad =
562                                     brle->start;
563                                 prd->u.cprd_port.cprd_len = brle->count;
564                                 break;
565                         case SYS_RES_MEMORY:
566                                 prd->cprd_type = CmResourceTypeMemory;
567                                 prd->cprd_flags =
568                                     CM_RESOURCE_MEMORY_READ_WRITE;
569                                 prd->cprd_sharedisp =
570                                     CmResourceShareDeviceExclusive;
571                                 prd->u.cprd_port.cprd_start.np_quad =
572                                     brle->start;
573                                 prd->u.cprd_port.cprd_len = brle->count;
574                                 break;
575                         case SYS_RES_IRQ:
576                                 prd->cprd_type = CmResourceTypeInterrupt;
577                                 prd->cprd_flags = 0;
578                                 /*
579                                  * Always mark interrupt resources as
580                                  * shared, since in our implementation,
581                                  * they will be.
582                                  */
583                                 prd->cprd_sharedisp =
584                                     CmResourceShareShared;
585                                 prd->u.cprd_intr.cprd_level = brle->start;
586                                 prd->u.cprd_intr.cprd_vector = brle->start;
587                                 prd->u.cprd_intr.cprd_affinity = 0;
588                                 break;
589                         default:
590                                 break;
591                         }
592                         prd++;
593                 }
594         }
595
596         block->nmb_rlist = rl;
597
598         return (error);
599 }
600
601 /*
602  * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
603  * packet, it will hand it to us in the form of an ndis_packet,
604  * which we need to convert to an mbuf that is then handed off
605  * to the stack. Note: we configure the mbuf list so that it uses
606  * the memory regions specified by the ndis_buffer structures in
607  * the ndis_packet as external storage. In most cases, this will
608  * point to a memory region allocated by the driver (either by
609  * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
610  * the driver to handle free()ing this region for is, so we set up
611  * a dummy no-op free handler for it.
612  */ 
613
614 int
615 ndis_ptom(m0, p)
616         struct mbuf             **m0;
617         ndis_packet             *p;
618 {
619         struct mbuf             *m = NULL, *prev = NULL;
620         ndis_buffer             *buf;
621         ndis_packet_private     *priv;
622         uint32_t                totlen = 0;
623         struct ifnet            *ifp;
624         struct ether_header     *eh;
625         int                     diff;
626
627         if (p == NULL || m0 == NULL)
628                 return (EINVAL);
629
630         priv = &p->np_private;
631         buf = priv->npp_head;
632         p->np_refcnt = 0;
633
634         for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
635                 if (buf == priv->npp_head)
636 #ifdef MT_HEADER
637                         MGETHDR(m, M_DONTWAIT, MT_HEADER);
638 #else
639                         MGETHDR(m, M_DONTWAIT, MT_DATA);
640 #endif
641                 else
642                         MGET(m, M_DONTWAIT, MT_DATA);
643                 if (m == NULL) {
644                         m_freem(*m0);
645                         *m0 = NULL;
646                         return (ENOBUFS);
647                 }
648                 m->m_len = MmGetMdlByteCount(buf);
649                 m->m_data = MmGetMdlVirtualAddress(buf);
650                 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
651                     m->m_data, p, 0, EXT_NDIS);
652                 p->np_refcnt++;
653
654                 totlen += m->m_len;
655                 if (m->m_flags & M_PKTHDR)
656                         *m0 = m;
657                 else
658                         prev->m_next = m;
659                 prev = m;
660         }
661
662         /*
663          * This is a hack to deal with the Marvell 8335 driver
664          * which, when associated with an AP in WPA-PSK mode,
665          * seems to overpad its frames by 8 bytes. I don't know
666          * that the extra 8 bytes are for, and they're not there
667          * in open mode, so for now clamp the frame size at 1514
668          * until I can figure out how to deal with this properly,
669          * otherwise if_ethersubr() will spank us by discarding
670          * the 'oversize' frames.
671          */
672
673         eh = mtod((*m0), struct ether_header *);
674         ifp = ((struct ndis_softc *)p->np_softc)->ifp;
675         if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
676                 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
677                 totlen -= diff;
678                 m->m_len -= diff;
679         }
680         (*m0)->m_pkthdr.len = totlen;
681
682         return (0);
683 }
684
685 /*
686  * Create an NDIS packet from an mbuf chain.
687  * This is used mainly when transmitting packets, where we need
688  * to turn an mbuf off an interface's send queue and transform it
689  * into an NDIS packet which will be fed into the NDIS driver's
690  * send routine.
691  *
692  * NDIS packets consist of two parts: an ndis_packet structure,
693  * which is vaguely analagous to the pkthdr portion of an mbuf,
694  * and one or more ndis_buffer structures, which define the
695  * actual memory segments in which the packet data resides.
696  * We need to allocate one ndis_buffer for each mbuf in a chain,
697  * plus one ndis_packet as the header.
698  */
699
700 int
701 ndis_mtop(m0, p)
702         struct mbuf             *m0;
703         ndis_packet             **p;
704 {
705         struct mbuf             *m;
706         ndis_buffer             *buf = NULL, *prev = NULL;
707         ndis_packet_private     *priv;
708
709         if (p == NULL || *p == NULL || m0 == NULL)
710                 return (EINVAL);
711
712         priv = &(*p)->np_private;
713         priv->npp_totlen = m0->m_pkthdr.len;
714
715         for (m = m0; m != NULL; m = m->m_next) {
716                 if (m->m_len == 0)
717                         continue;
718                 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
719                 if (buf == NULL) {
720                         ndis_free_packet(*p);
721                         *p = NULL;
722                         return (ENOMEM);
723                 }
724                 MmBuildMdlForNonPagedPool(buf);
725
726                 if (priv->npp_head == NULL)
727                         priv->npp_head = buf;
728                 else
729                         prev->mdl_next = buf;
730                 prev = buf;
731         }
732
733         priv->npp_tail = buf;
734
735         return (0);
736 }
737
738 int
739 ndis_get_supported_oids(arg, oids, oidcnt)
740         void                    *arg;
741         ndis_oid                **oids;
742         int                     *oidcnt;
743 {
744         int                     len, rval;
745         ndis_oid                *o;
746
747         if (arg == NULL || oids == NULL || oidcnt == NULL)
748                 return (EINVAL);
749         len = 0;
750         ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
751
752         o = malloc(len, M_DEVBUF, M_NOWAIT);
753         if (o == NULL)
754                 return (ENOMEM);
755
756         rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
757
758         if (rval) {
759                 free(o, M_DEVBUF);
760                 return (rval);
761         }
762
763         *oids = o;
764         *oidcnt = len / 4;
765
766         return (0);
767 }
768
769 int
770 ndis_set_info(arg, oid, buf, buflen)
771         void                    *arg;
772         ndis_oid                oid;
773         void                    *buf;
774         int                     *buflen;
775 {
776         struct ndis_softc       *sc;
777         ndis_status             rval;
778         ndis_handle             adapter;
779         ndis_setinfo_handler    setfunc;
780         uint32_t                byteswritten = 0, bytesneeded = 0;
781         uint8_t                 irql;
782         uint64_t                duetime;
783
784         /*
785          * According to the NDIS spec, MiniportQueryInformation()
786          * and MiniportSetInformation() requests are handled serially:
787          * once one request has been issued, we must wait for it to
788          * finish before allowing another request to proceed.
789          */
790
791         sc = arg;
792
793         KeResetEvent(&sc->ndis_block->nmb_setevent);
794
795         KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
796
797         if (sc->ndis_block->nmb_pendingreq != NULL) {
798                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
799                 panic("ndis_set_info() called while other request pending");
800         } else
801                 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
802
803         setfunc = sc->ndis_chars->nmc_setinfo_func;
804         adapter = sc->ndis_block->nmb_miniportadapterctx;
805
806         if (adapter == NULL || setfunc == NULL ||
807             sc->ndis_block->nmb_devicectx == NULL) {
808                 sc->ndis_block->nmb_pendingreq = NULL;
809                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
810                 return (ENXIO);
811         }
812
813         rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
814             &byteswritten, &bytesneeded);
815
816         sc->ndis_block->nmb_pendingreq = NULL;
817
818         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
819
820         if (rval == NDIS_STATUS_PENDING) {
821                 /* Wait up to 5 seconds. */
822                 duetime = (5 * 1000000) * -10;
823                 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
824                     0, 0, FALSE, &duetime);
825                 rval = sc->ndis_block->nmb_setstat;
826         }
827
828         if (byteswritten)
829                 *buflen = byteswritten;
830         if (bytesneeded)
831                 *buflen = bytesneeded;
832
833         if (rval == NDIS_STATUS_INVALID_LENGTH)
834                 return (ENOSPC);
835
836         if (rval == NDIS_STATUS_INVALID_OID)
837                 return (EINVAL);
838
839         if (rval == NDIS_STATUS_NOT_SUPPORTED ||
840             rval == NDIS_STATUS_NOT_ACCEPTED)
841                 return (ENOTSUP);
842
843         if (rval != NDIS_STATUS_SUCCESS)
844                 return (ENODEV);
845
846         return (0);
847 }
848
849 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
850
851 int
852 ndis_send_packets(arg, packets, cnt)
853         void                    *arg;
854         ndis_packet             **packets;
855         int                     cnt;
856 {
857         struct ndis_softc       *sc;
858         ndis_handle             adapter;
859         ndis_sendmulti_handler  sendfunc;
860         ndis_senddone_func              senddonefunc;
861         int                     i;
862         ndis_packet             *p;
863         uint8_t                 irql = 0;
864
865         sc = arg;
866         adapter = sc->ndis_block->nmb_miniportadapterctx;
867         if (adapter == NULL)
868                 return (ENXIO);
869         sendfunc = sc->ndis_chars->nmc_sendmulti_func;
870         senddonefunc = sc->ndis_block->nmb_senddone_func;
871
872         if (NDIS_SERIALIZED(sc->ndis_block))
873                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
874
875         MSCALL3(sendfunc, adapter, packets, cnt);
876
877         for (i = 0; i < cnt; i++) {
878                 p = packets[i];
879                 /*
880                  * Either the driver already handed the packet to
881                  * ndis_txeof() due to a failure, or it wants to keep
882                  * it and release it asynchronously later. Skip to the
883                  * next one.
884                  */
885                 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
886                         continue;
887                 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
888         }
889
890         if (NDIS_SERIALIZED(sc->ndis_block))
891                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
892
893         return (0);
894 }
895
896 int
897 ndis_send_packet(arg, packet)
898         void                    *arg;
899         ndis_packet             *packet;
900 {
901         struct ndis_softc       *sc;
902         ndis_handle             adapter;
903         ndis_status             status;
904         ndis_sendsingle_handler sendfunc;
905         ndis_senddone_func              senddonefunc;
906         uint8_t                 irql = 0;
907
908         sc = arg;
909         adapter = sc->ndis_block->nmb_miniportadapterctx;
910         if (adapter == NULL)
911                 return (ENXIO);
912         sendfunc = sc->ndis_chars->nmc_sendsingle_func;
913         senddonefunc = sc->ndis_block->nmb_senddone_func;
914
915         if (NDIS_SERIALIZED(sc->ndis_block))
916                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
917         status = MSCALL3(sendfunc, adapter, packet,
918             packet->np_private.npp_flags);
919
920         if (status == NDIS_STATUS_PENDING) {
921                 if (NDIS_SERIALIZED(sc->ndis_block))
922                         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
923                 return (0);
924         }
925
926         MSCALL3(senddonefunc, sc->ndis_block, packet, status);
927
928         if (NDIS_SERIALIZED(sc->ndis_block))
929                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
930
931         return (0);
932 }
933
934 int
935 ndis_init_dma(arg)
936         void                    *arg;
937 {
938         struct ndis_softc       *sc;
939         int                     i, error;
940
941         sc = arg;
942
943         sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
944             M_DEVBUF, M_NOWAIT|M_ZERO);
945
946         if (sc->ndis_tmaps == NULL)
947                 return (ENOMEM);
948
949         for (i = 0; i < sc->ndis_maxpkts; i++) {
950                 error = bus_dmamap_create(sc->ndis_ttag, 0,
951                     &sc->ndis_tmaps[i]);
952                 if (error) {
953                         free(sc->ndis_tmaps, M_DEVBUF);
954                         return (ENODEV);
955                 }
956         }
957
958         return (0);
959 }
960
961 int
962 ndis_destroy_dma(arg)
963         void                    *arg;
964 {
965         struct ndis_softc       *sc;
966         struct mbuf             *m;
967         ndis_packet             *p = NULL;
968         int                     i;
969
970         sc = arg;
971
972         for (i = 0; i < sc->ndis_maxpkts; i++) {
973                 if (sc->ndis_txarray[i] != NULL) {
974                         p = sc->ndis_txarray[i];
975                         m = (struct mbuf *)p->np_rsvd[1];
976                         if (m != NULL)
977                                 m_freem(m);
978                         ndis_free_packet(sc->ndis_txarray[i]);
979                 }
980                 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
981         }
982
983         free(sc->ndis_tmaps, M_DEVBUF);
984
985         bus_dma_tag_destroy(sc->ndis_ttag);
986
987         return (0);
988 }
989
990 int
991 ndis_reset_nic(arg)
992         void                    *arg;
993 {
994         struct ndis_softc       *sc;
995         ndis_handle             adapter;
996         ndis_reset_handler      resetfunc;
997         uint8_t                 addressing_reset;
998         int                     rval;
999         uint8_t                 irql = 0;
1000
1001         sc = arg;
1002
1003         NDIS_LOCK(sc);
1004         adapter = sc->ndis_block->nmb_miniportadapterctx;
1005         resetfunc = sc->ndis_chars->nmc_reset_func;
1006
1007         if (adapter == NULL || resetfunc == NULL ||
1008             sc->ndis_block->nmb_devicectx == NULL) {
1009                 NDIS_UNLOCK(sc);
1010                 return (EIO);
1011         }
1012
1013         NDIS_UNLOCK(sc);
1014
1015         KeResetEvent(&sc->ndis_block->nmb_resetevent);
1016
1017         if (NDIS_SERIALIZED(sc->ndis_block))
1018                 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1019
1020         rval = MSCALL2(resetfunc, &addressing_reset, adapter);
1021
1022         if (NDIS_SERIALIZED(sc->ndis_block))
1023                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1024
1025         if (rval == NDIS_STATUS_PENDING)
1026                 KeWaitForSingleObject(&sc->ndis_block->nmb_resetevent,
1027                     0, 0, FALSE, NULL);
1028
1029         return (0);
1030 }
1031
1032 int
1033 ndis_halt_nic(arg)
1034         void                    *arg;
1035 {
1036         struct ndis_softc       *sc;
1037         ndis_handle             adapter;
1038         ndis_halt_handler       haltfunc;
1039         ndis_miniport_block     *block;
1040         int                     empty = 0;
1041         uint8_t                 irql;
1042
1043         sc = arg;
1044         block = sc->ndis_block;
1045
1046         if (!cold)
1047                 KeFlushQueuedDpcs();
1048
1049         /*
1050          * Wait for all packets to be returned.
1051          */
1052
1053         while (1) {
1054                 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
1055                 empty = IsListEmpty(&block->nmb_returnlist);
1056                 KeReleaseSpinLock(&block->nmb_returnlock, irql);
1057                 if (empty)
1058                         break;
1059                 NdisMSleep(1000);
1060         }
1061
1062         NDIS_LOCK(sc);
1063         adapter = sc->ndis_block->nmb_miniportadapterctx;
1064         if (adapter == NULL) {
1065                 NDIS_UNLOCK(sc);
1066                 return (EIO);
1067         }
1068
1069         sc->ndis_block->nmb_devicectx = NULL;
1070
1071         /*
1072          * The adapter context is only valid after the init
1073          * handler has been called, and is invalid once the
1074          * halt handler has been called.
1075          */
1076
1077         haltfunc = sc->ndis_chars->nmc_halt_func;
1078         NDIS_UNLOCK(sc);
1079
1080         MSCALL1(haltfunc, adapter);
1081
1082         NDIS_LOCK(sc);
1083         sc->ndis_block->nmb_miniportadapterctx = NULL;
1084         NDIS_UNLOCK(sc);
1085
1086         return (0);
1087 }
1088
1089 int
1090 ndis_shutdown_nic(arg)
1091         void                    *arg;
1092 {
1093         struct ndis_softc       *sc;
1094         ndis_handle             adapter;
1095         ndis_shutdown_handler   shutdownfunc;
1096
1097         sc = arg;
1098         NDIS_LOCK(sc);
1099         adapter = sc->ndis_block->nmb_miniportadapterctx;
1100         shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1101         NDIS_UNLOCK(sc);
1102         if (adapter == NULL || shutdownfunc == NULL)
1103                 return (EIO);
1104
1105         if (sc->ndis_chars->nmc_rsvd0 == NULL)
1106                 MSCALL1(shutdownfunc, adapter);
1107         else
1108                 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
1109
1110         TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1111
1112         return (0);
1113 }
1114
1115 int
1116 ndis_pnpevent_nic(arg, type)
1117         void                    *arg;
1118         int                     type;
1119 {
1120         device_t                dev;
1121         struct ndis_softc       *sc;
1122         ndis_handle             adapter;
1123         ndis_pnpevent_handler   pnpeventfunc;
1124
1125         dev = arg;
1126         sc = device_get_softc(arg);
1127         NDIS_LOCK(sc);
1128         adapter = sc->ndis_block->nmb_miniportadapterctx;
1129         pnpeventfunc = sc->ndis_chars->nmc_pnpevent_handler;
1130         NDIS_UNLOCK(sc);
1131         if (adapter == NULL || pnpeventfunc == NULL)
1132                 return (EIO);
1133
1134         if (sc->ndis_chars->nmc_rsvd0 == NULL)
1135                 MSCALL4(pnpeventfunc, adapter, type, NULL, 0);
1136         else
1137                 MSCALL4(pnpeventfunc, sc->ndis_chars->nmc_rsvd0, type, NULL, 0);
1138
1139         return (0);
1140 }
1141
1142 int
1143 ndis_init_nic(arg)
1144         void                    *arg;
1145 {
1146         struct ndis_softc       *sc;
1147         ndis_miniport_block     *block;
1148         ndis_init_handler       initfunc;
1149         ndis_status             status, openstatus = 0;
1150         ndis_medium             mediumarray[NdisMediumMax];
1151         uint32_t                chosenmedium, i;
1152
1153         if (arg == NULL)
1154                 return (EINVAL);
1155
1156         sc = arg;
1157         NDIS_LOCK(sc);
1158         block = sc->ndis_block;
1159         initfunc = sc->ndis_chars->nmc_init_func;
1160         NDIS_UNLOCK(sc);
1161
1162         sc->ndis_block->nmb_timerlist = NULL;
1163
1164         for (i = 0; i < NdisMediumMax; i++)
1165                 mediumarray[i] = i;
1166
1167         status = MSCALL6(initfunc, &openstatus, &chosenmedium,
1168             mediumarray, NdisMediumMax, block, block);
1169
1170         /*
1171          * If the init fails, blow away the other exported routines
1172          * we obtained from the driver so we can't call them later.
1173          * If the init failed, none of these will work.
1174          */
1175         if (status != NDIS_STATUS_SUCCESS) {
1176                 NDIS_LOCK(sc);
1177                 sc->ndis_block->nmb_miniportadapterctx = NULL;
1178                 NDIS_UNLOCK(sc);
1179                 return (ENXIO);
1180         }
1181
1182         /*
1183          * This may look really goofy, but apparently it is possible
1184          * to halt a miniport too soon after it's been initialized.
1185          * After MiniportInitialize() finishes, pause for 1 second
1186          * to give the chip a chance to handle any short-lived timers
1187          * that were set in motion. If we call MiniportHalt() too soon,
1188          * some of the timers may not be cancelled, because the driver
1189          * expects them to fire before the halt is called.
1190          */
1191
1192         pause("ndwait", hz);
1193
1194         NDIS_LOCK(sc);
1195         sc->ndis_block->nmb_devicectx = sc;
1196         NDIS_UNLOCK(sc);
1197
1198         return (0);
1199 }
1200
1201 static void
1202 ndis_intrsetup(dpc, dobj, ip, sc)
1203         kdpc                    *dpc;
1204         device_object           *dobj;
1205         irp                     *ip;
1206         struct ndis_softc       *sc;
1207 {
1208         ndis_miniport_interrupt *intr;
1209
1210         intr = sc->ndis_block->nmb_interrupt;
1211
1212         /* Sanity check. */
1213
1214         if (intr == NULL)
1215                 return;
1216
1217         KeAcquireSpinLockAtDpcLevel(&intr->ni_dpccountlock);
1218         KeResetEvent(&intr->ni_dpcevt);
1219         if (KeInsertQueueDpc(&intr->ni_dpc, NULL, NULL) == TRUE)
1220                 intr->ni_dpccnt++;
1221         KeReleaseSpinLockFromDpcLevel(&intr->ni_dpccountlock);
1222 }
1223
1224 int
1225 ndis_get_info(arg, oid, buf, buflen)
1226         void                    *arg;
1227         ndis_oid                oid;
1228         void                    *buf;
1229         int                     *buflen;
1230 {
1231         struct ndis_softc       *sc;
1232         ndis_status             rval;
1233         ndis_handle             adapter;
1234         ndis_queryinfo_handler  queryfunc;
1235         uint32_t                byteswritten = 0, bytesneeded = 0;
1236         uint8_t                 irql;
1237         uint64_t                duetime;
1238
1239         sc = arg;
1240
1241         KeResetEvent(&sc->ndis_block->nmb_getevent);
1242
1243         KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
1244
1245         if (sc->ndis_block->nmb_pendingreq != NULL) {
1246                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1247                 panic("ndis_get_info() called while other request pending");
1248         } else
1249                 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
1250
1251         queryfunc = sc->ndis_chars->nmc_queryinfo_func;
1252         adapter = sc->ndis_block->nmb_miniportadapterctx;
1253
1254         if (adapter == NULL || queryfunc == NULL ||
1255             sc->ndis_block->nmb_devicectx == NULL) {
1256                 sc->ndis_block->nmb_pendingreq = NULL;
1257                 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1258                 return (ENXIO);
1259         }
1260
1261         rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
1262             &byteswritten, &bytesneeded);
1263
1264         sc->ndis_block->nmb_pendingreq = NULL;
1265
1266         KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
1267
1268         /* Wait for requests that block. */
1269
1270         if (rval == NDIS_STATUS_PENDING) {
1271                 /* Wait up to 5 seconds. */
1272                 duetime = (5 * 1000000) * -10;
1273                 KeWaitForSingleObject(&sc->ndis_block->nmb_getevent,
1274                     0, 0, FALSE, &duetime);
1275                 rval = sc->ndis_block->nmb_getstat;
1276         }
1277
1278         if (byteswritten)
1279                 *buflen = byteswritten;
1280         if (bytesneeded)
1281                 *buflen = bytesneeded;
1282
1283         if (rval == NDIS_STATUS_INVALID_LENGTH ||
1284             rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1285                 return (ENOSPC);
1286
1287         if (rval == NDIS_STATUS_INVALID_OID)
1288                 return (EINVAL);
1289
1290         if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1291             rval == NDIS_STATUS_NOT_ACCEPTED)
1292                 return (ENOTSUP);
1293
1294         if (rval != NDIS_STATUS_SUCCESS)
1295                 return (ENODEV);
1296
1297         return (0);
1298 }
1299
1300 uint32_t
1301 NdisAddDevice(drv, pdo)
1302         driver_object           *drv;
1303         device_object           *pdo;
1304 {
1305         device_object           *fdo;
1306         ndis_miniport_block     *block;
1307         struct ndis_softc       *sc;
1308         uint32_t                status;
1309         int                     error;
1310
1311         sc = device_get_softc(pdo->do_devext);
1312
1313         if (sc->ndis_iftype == PCMCIABus || sc->ndis_iftype == PCIBus) {
1314                 error = bus_setup_intr(sc->ndis_dev, sc->ndis_irq,
1315                     INTR_TYPE_NET | INTR_MPSAFE,
1316                     NULL, ntoskrnl_intr, NULL, &sc->ndis_intrhand);
1317                 if (error)
1318                         return (NDIS_STATUS_FAILURE);
1319         }
1320
1321         status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1322             FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
1323
1324         if (status != STATUS_SUCCESS)
1325                 return (status);
1326
1327         block = fdo->do_devext;
1328
1329         block->nmb_filterdbs.nf_ethdb = block;
1330         block->nmb_deviceobj = fdo;
1331         block->nmb_physdeviceobj = pdo;
1332         block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo);
1333         KeInitializeSpinLock(&block->nmb_lock);
1334         KeInitializeSpinLock(&block->nmb_returnlock);
1335         KeInitializeEvent(&block->nmb_getevent, EVENT_TYPE_NOTIFY, TRUE);
1336         KeInitializeEvent(&block->nmb_setevent, EVENT_TYPE_NOTIFY, TRUE);
1337         KeInitializeEvent(&block->nmb_resetevent, EVENT_TYPE_NOTIFY, TRUE);
1338         InitializeListHead(&block->nmb_parmlist);
1339         InitializeListHead(&block->nmb_returnlist);
1340         block->nmb_returnitem = IoAllocateWorkItem(fdo);
1341
1342         /*
1343          * Stash pointers to the miniport block and miniport
1344          * characteristics info in the if_ndis softc so the
1345          * UNIX wrapper driver can get to them later.
1346          */
1347         sc->ndis_block = block;
1348         sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1);
1349
1350         /*
1351          * If the driver has a MiniportTransferData() function,
1352          * we should allocate a private RX packet pool.
1353          */
1354
1355         if (sc->ndis_chars->nmc_transferdata_func != NULL) {
1356                 NdisAllocatePacketPool(&status, &block->nmb_rxpool,
1357                     32, PROTOCOL_RESERVED_SIZE_IN_PACKET);
1358                 if (status != NDIS_STATUS_SUCCESS) {
1359                         IoDetachDevice(block->nmb_nextdeviceobj);
1360                         IoDeleteDevice(fdo);
1361                         return (status);
1362                 }
1363                 InitializeListHead((&block->nmb_packetlist));
1364         }
1365
1366         /* Give interrupt handling priority over timers. */
1367         IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1368         KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
1369
1370         /* Finish up BSD-specific setup. */
1371
1372         block->nmb_signature = (void *)0xcafebabe;
1373         block->nmb_status_func = kernndis_functbl[0].ipt_wrap;
1374         block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap;
1375         block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap;
1376         block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap;
1377         block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap;
1378         block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap;
1379         block->nmb_pendingreq = NULL;
1380
1381         TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
1382
1383         return (STATUS_SUCCESS);
1384 }
1385
1386 int
1387 ndis_unload_driver(arg)
1388         void                    *arg;
1389 {
1390         struct ndis_softc       *sc;
1391         device_object           *fdo;
1392
1393         sc = arg;
1394
1395         if (sc->ndis_intrhand)
1396                 bus_teardown_intr(sc->ndis_dev,
1397                     sc->ndis_irq, sc->ndis_intrhand);
1398
1399         if (sc->ndis_block->nmb_rlist != NULL)
1400                 free(sc->ndis_block->nmb_rlist, M_DEVBUF);
1401
1402         ndis_flush_sysctls(sc);
1403
1404         TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
1405
1406         if (sc->ndis_chars->nmc_transferdata_func != NULL)
1407                 NdisFreePacketPool(sc->ndis_block->nmb_rxpool);
1408         fdo = sc->ndis_block->nmb_deviceobj;
1409         IoFreeWorkItem(sc->ndis_block->nmb_returnitem);
1410         IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj);
1411         IoDeleteDevice(fdo);
1412
1413         return (0);
1414 }