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