The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/ndis/kern_ndis.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: c7a5c839100f992d44dcf6b85a1d1657


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.