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: src/sys/compat/ndis/kern_ndis.c,v 1.100 2008/12/27 09:42:17 weongyo Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/unistd.h>
39 #include <sys/types.h>
40 #include <sys/errno.h>
41 #include <sys/callout.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/conf.h>
50
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/kthread.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <sys/bus.h>
57 #include <sys/rman.h>
58
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_ioctl.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70
71 #include <compat/ndis/pe_var.h>
72 #include <compat/ndis/cfg_var.h>
73 #include <compat/ndis/resource_var.h>
74 #include <compat/ndis/ntoskrnl_var.h>
75 #include <compat/ndis/ndis_var.h>
76 #include <compat/ndis/hal_var.h>
77 #include <compat/ndis/usbd_var.h>
78 #include <dev/if_ndis/if_ndisvar.h>
79
80 #define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
81
82 static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
83 static void ndis_statusdone_func(ndis_handle);
84 static void ndis_setdone_func(ndis_handle, ndis_status);
85 static void ndis_getdone_func(ndis_handle, ndis_status);
86 static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
87 static void ndis_sendrsrcavail_func(ndis_handle);
88 static void ndis_intrsetup(kdpc *, device_object *,
89 irp *, struct ndis_softc *);
90 static void ndis_return(device_object *, void *);
91
92 static image_patch_table kernndis_functbl[] = {
93 IMPORT_SFUNC(ndis_status_func, 4),
94 IMPORT_SFUNC(ndis_statusdone_func, 1),
95 IMPORT_SFUNC(ndis_setdone_func, 2),
96 IMPORT_SFUNC(ndis_getdone_func, 2),
97 IMPORT_SFUNC(ndis_resetdone_func, 3),
98 IMPORT_SFUNC(ndis_sendrsrcavail_func, 1),
99 IMPORT_SFUNC(ndis_intrsetup, 4),
100 IMPORT_SFUNC(ndis_return, 1),
101
102 { NULL, NULL, NULL }
103 };
104
105 static struct nd_head ndis_devhead;
106
107 /*
108 * This allows us to export our symbols to other modules.
109 * Note that we call ourselves 'ndisapi' to avoid a namespace
110 * collision with if_ndis.ko, which internally calls itself
111 * 'ndis.'
112 *
113 * Note: some of the subsystems depend on each other, so the
114 * order in which they're started is important. The order of
115 * importance is:
116 *
117 * HAL - spinlocks and IRQL manipulation
118 * ntoskrnl - DPC and workitem threads, object waiting
119 * windrv - driver/device registration
120 *
121 * The HAL should also be the last thing shut down, since
122 * the ntoskrnl subsystem will use spinlocks right up until
123 * the DPC and workitem threads are terminated.
124 */
125
126 static int
127 ndis_modevent(module_t mod, int cmd, void *arg)
128 {
129 int error = 0;
130 image_patch_table *patch;
131
132 switch (cmd) {
133 case MOD_LOAD:
134 /* Initialize subsystems */
135 hal_libinit();
136 ntoskrnl_libinit();
137 windrv_libinit();
138 ndis_libinit();
139 usbd_libinit();
140
141 patch = kernndis_functbl;
142 while (patch->ipt_func != NULL) {
143 windrv_wrap((funcptr)patch->ipt_func,
144 (funcptr *)&patch->ipt_wrap,
145 patch->ipt_argcnt, patch->ipt_ftype);
146 patch++;
147 }
148
149 TAILQ_INIT(&ndis_devhead);
150 break;
151 case MOD_SHUTDOWN:
152 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
153 /* Shut down subsystems */
154 ndis_libfini();
155 usbd_libfini();
156 windrv_libfini();
157 ntoskrnl_libfini();
158 hal_libfini();
159
160 patch = kernndis_functbl;
161 while (patch->ipt_func != NULL) {
162 windrv_unwrap(patch->ipt_wrap);
163 patch++;
164 }
165 }
166 break;
167 case MOD_UNLOAD:
168 /* Shut down subsystems */
169 ndis_libfini();
170 usbd_libfini();
171 windrv_libfini();
172 ntoskrnl_libfini();
173 hal_libfini();
174
175 patch = kernndis_functbl;
176 while (patch->ipt_func != NULL) {
177 windrv_unwrap(patch->ipt_wrap);
178 patch++;
179 }
180
181 break;
182 default:
183 error = EINVAL;
184 break;
185 }
186
187 return(error);
188 }
189 DEV_MODULE(ndisapi, ndis_modevent, NULL);
190 MODULE_VERSION(ndisapi, 1);
191
192 static void
193 ndis_sendrsrcavail_func(adapter)
194 ndis_handle adapter;
195 {
196 return;
197 }
198
199 static void
200 ndis_status_func(adapter, status, sbuf, slen)
201 ndis_handle adapter;
202 ndis_status status;
203 void *sbuf;
204 uint32_t slen;
205 {
206 ndis_miniport_block *block;
207 struct ndis_softc *sc;
208 struct ifnet *ifp;
209
210 block = adapter;
211 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
212 ifp = sc->ifp;
213 if (ifp->if_flags & IFF_DEBUG)
214 device_printf (sc->ndis_dev, "status: %x\n", status);
215 return;
216 }
217
218 static void
219 ndis_statusdone_func(adapter)
220 ndis_handle adapter;
221 {
222 ndis_miniport_block *block;
223 struct ndis_softc *sc;
224 struct ifnet *ifp;
225
226 block = adapter;
227 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
228 ifp = sc->ifp;
229 if (ifp->if_flags & IFF_DEBUG)
230 device_printf (sc->ndis_dev, "status complete\n");
231 return;
232 }
233
234 static void
235 ndis_setdone_func(adapter, status)
236 ndis_handle adapter;
237 ndis_status status;
238 {
239 ndis_miniport_block *block;
240 block = adapter;
241
242 block->nmb_setstat = status;
243 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
244 return;
245 }
246
247 static void
248 ndis_getdone_func(adapter, status)
249 ndis_handle adapter;
250 ndis_status status;
251 {
252 ndis_miniport_block *block;
253 block = adapter;
254
255 block->nmb_getstat = status;
256 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
257 return;
258 }
259
260 static void
261 ndis_resetdone_func(adapter, status, addressingreset)
262 ndis_handle adapter;
263 ndis_status status;
264 uint8_t addressingreset;
265 {
266 ndis_miniport_block *block;
267 struct ndis_softc *sc;
268 struct ifnet *ifp;
269
270 block = adapter;
271 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
272 ifp = sc->ifp;
273
274 if (ifp->if_flags & IFF_DEBUG)
275 device_printf (sc->ndis_dev, "reset done...\n");
276 KeSetEvent(&block->nmb_resetevent, IO_NO_INCREMENT, FALSE);
277
278 return;
279 }
280
281 int
282 ndis_create_sysctls(arg)
283 void *arg;
284 {
285 struct ndis_softc *sc;
286 ndis_cfg *vals;
287 char buf[256];
288 struct sysctl_oid *oidp;
289 struct sysctl_ctx_entry *e;
290
291 if (arg == NULL)
292 return(EINVAL);
293
294 sc = arg;
295 vals = sc->ndis_regvals;
296
297 TAILQ_INIT(&sc->ndis_cfglist_head);
298
299 #if __FreeBSD_version < 502113
300 /* Create the sysctl tree. */
301
302 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
303 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
304 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
305 device_get_desc(sc->ndis_dev));
306
307 #endif
308 /* Add the driver-specific registry keys. */
309
310 while(1) {
311 if (vals->nc_cfgkey == NULL)
312 break;
313
314 if (vals->nc_idx != sc->ndis_devidx) {
315 vals++;
316 continue;
317 }
318
319 /* See if we already have a sysctl with this name */
320
321 oidp = NULL;
322 #if __FreeBSD_version < 502113
323 TAILQ_FOREACH(e, &sc->ndis_ctx, link) {
324 #else
325 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) {
326 #endif
327 oidp = e->entry;
328 if (strcasecmp(oidp->oid_name, vals->nc_cfgkey) == 0)
329 break;
330 oidp = NULL;
331 }
332
333 if (oidp != NULL) {
334 vals++;
335 continue;
336 }
337
338 ndis_add_sysctl(sc, vals->nc_cfgkey, vals->nc_cfgdesc,
339 vals->nc_val, CTLFLAG_RW);
340 vals++;
341 }
342
343 /* Now add a couple of builtin keys. */
344
345 /*
346 * Environment can be either Windows (0) or WindowsNT (1).
347 * We qualify as the latter.
348 */
349 ndis_add_sysctl(sc, "Environment",
350 "Windows environment", "1", CTLFLAG_RD);
351
352 /* NDIS version should be 5.1. */
353 ndis_add_sysctl(sc, "NdisVersion",
354 "NDIS API Version", "0x00050001", CTLFLAG_RD);
355
356 /* Bus type (PCI, PCMCIA, etc...) */
357 sprintf(buf, "%d", (int)sc->ndis_iftype);
358 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
359
360 if (sc->ndis_res_io != NULL) {
361 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
362 ndis_add_sysctl(sc, "IOBaseAddress",
363 "Base I/O Address", buf, CTLFLAG_RD);
364 }
365
366 if (sc->ndis_irq != NULL) {
367 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
368 ndis_add_sysctl(sc, "InterruptNumber",
369 "Interrupt Number", buf, CTLFLAG_RD);
370 }
371
372 return(0);
373 }
374
375 int
376 ndis_add_sysctl(arg, key, desc, val, flag)
377 void *arg;
378 char *key;
379 char *desc;
380 char *val;
381 int flag;
382 {
383 struct ndis_softc *sc;
384 struct ndis_cfglist *cfg;
385 char descstr[256];
386
387 sc = arg;
388
389 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
390
391 if (cfg == NULL) {
392 printf("failed for %s\n", key);
393 return(ENOMEM);
394 }
395
396 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
397 if (desc == NULL) {
398 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
399 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
400 } else
401 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
402 strcpy(cfg->ndis_cfg.nc_val, val);
403
404 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
405
406 cfg->ndis_oid =
407 #if __FreeBSD_version < 502113
408 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
409 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
410 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
411 cfg->ndis_cfg.nc_cfgdesc);
412 #else
413 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev),
414 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)),
415 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
416 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
417 cfg->ndis_cfg.nc_cfgdesc);
418 #endif
419
420 return(0);
421 }
422
423 /*
424 * Somewhere, somebody decided "hey, let's automatically create
425 * a sysctl tree for each device instance as it's created -- it'll
426 * make life so much easier!" Lies. Why must they turn the kernel
427 * into a house of lies?
428 */
429
430 int
431 ndis_flush_sysctls(arg)
432 void *arg;
433 {
434 struct ndis_softc *sc;
435 struct ndis_cfglist *cfg;
436 struct sysctl_ctx_list *clist;
437
438 sc = arg;
439
440 #if __FreeBSD_version < 502113
441 clist = &sc->ndis_ctx;
442 #else
443 clist = device_get_sysctl_ctx(sc->ndis_dev);
444 #endif
445
446 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
447 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
448 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
449 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
450 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
451 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
452 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
453 free(cfg, M_DEVBUF);
454 }
455
456 return(0);
457 }
458
459 static void
460 ndis_return(dobj, arg)
461 device_object *dobj;
462 void *arg;
463 {
464 ndis_miniport_block *block;
465 ndis_miniport_characteristics *ch;
466 ndis_return_handler returnfunc;
467 ndis_handle adapter;
468 ndis_packet *p;
469 uint8_t irql;
470 list_entry *l;
471
472 block = arg;
473 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
474
475 p = arg;
476 adapter = block->nmb_miniportadapterctx;
477
478 if (adapter == NULL)
479 return;
480
481 returnfunc = ch->nmc_return_packet_func;
482
483 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
484 while (!IsListEmpty(&block->nmb_returnlist)) {
485 l = RemoveHeadList((&block->nmb_returnlist));
486 p = CONTAINING_RECORD(l, ndis_packet, np_list);
487 InitializeListHead((&p->np_list));
488 KeReleaseSpinLock(&block->nmb_returnlock, irql);
489 MSCALL2(returnfunc, adapter, p);
490 KeAcquireSpinLock(&block->nmb_returnlock, &irql);
491 }
492 KeReleaseSpinLock(&block->nmb_returnlock, irql);
493
494 return;
495 }
496
497 void
498 ndis_return_packet(buf, arg)
499 void *buf; /* not used */
500 void *arg;
501 {
502 ndis_packet *p;
503 ndis_miniport_block *block;
504
505 if (arg == NULL)
506 return;
507
508 p = arg;
509
510 /* Decrement refcount. */
511 p->np_refcnt--;
512
513 /* Release packet when refcount hits zero, otherwise return. */
514 if (p->np_refcnt)
515 return;
516
517 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
518
519 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
520 InitializeListHead((&p->np_list));
521 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
522 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
523
524 IoQueueWorkItem(block->nmb_returnitem,
525 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
526 WORKQUEUE_CRITICAL, block);
527
528 return;
529 }
530
531 void
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 return;
547 }
548 int in_reset = 0;
549 void
550 ndis_free_packet(p)
551 ndis_packet *p;
552 {
553 if (p == NULL)
554 return;
555
556 ndis_free_bufs(p->np_private.npp_head);
557 NdisFreePacket(p);
558 return;
559 }
560
561 int
562 ndis_convert_res(arg)
563 void *arg;
564 {
565 struct ndis_softc *sc;
566 ndis_resource_list *rl = NULL;
567 cm_partial_resource_desc *prd = NULL;
568 ndis_miniport_block *block;
569 device_t dev;
570 struct resource_list *brl;
571 struct resource_list_entry *brle;
572 #if __FreeBSD_version < 600022
573 struct resource_list brl_rev;
574 struct resource_list_entry *n;
575 #endif
576 int error = 0;
577
578 sc = arg;
579 block = sc->ndis_block;
580 dev = sc->ndis_dev;
581
582 #if __FreeBSD_version < 600022
583 SLIST_INIT(&brl_rev);
584 #endif
585
586 rl = malloc(sizeof(ndis_resource_list) +
587 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
588 M_DEVBUF, M_NOWAIT|M_ZERO);
589
590 if (rl == NULL)
591 return(ENOMEM);
592
593 rl->cprl_version = 5;
594 rl->cprl_version = 1;
595 rl->cprl_count = sc->ndis_rescnt;
596 prd = rl->cprl_partial_descs;
597
598 brl = BUS_GET_RESOURCE_LIST(dev, dev);
599
600 if (brl != NULL) {
601
602 #if __FreeBSD_version < 600022
603 /*
604 * We have a small problem. Some PCI devices have
605 * multiple I/O ranges. Windows orders them starting
606 * from lowest numbered BAR to highest. We discover
607 * them in that order too, but insert them into a singly
608 * linked list head first, which means when time comes
609 * to traverse the list, we enumerate them in reverse
610 * order. This screws up some drivers which expect the
611 * BARs to be in ascending order so that they can choose
612 * the "first" one as their register space. Unfortunately,
613 * in order to fix this, we have to create our own
614 * temporary list with the entries in reverse order.
615 */
616
617 SLIST_FOREACH(brle, brl, link) {
618 n = malloc(sizeof(struct resource_list_entry),
619 M_TEMP, M_NOWAIT);
620 if (n == NULL) {
621 error = ENOMEM;
622 goto bad;
623 }
624 bcopy((char *)brle, (char *)n,
625 sizeof(struct resource_list_entry));
626 SLIST_INSERT_HEAD(&brl_rev, n, link);
627 }
628
629 SLIST_FOREACH(brle, &brl_rev, link) {
630 #else
631 STAILQ_FOREACH(brle, brl, link) {
632 #endif
633 switch (brle->type) {
634 case SYS_RES_IOPORT:
635 prd->cprd_type = CmResourceTypePort;
636 prd->cprd_flags = CM_RESOURCE_PORT_IO;
637 prd->cprd_sharedisp =
638 CmResourceShareDeviceExclusive;
639 prd->u.cprd_port.cprd_start.np_quad =
640 brle->start;
641 prd->u.cprd_port.cprd_len = brle->count;
642 break;
643 case SYS_RES_MEMORY:
644 prd->cprd_type = CmResourceTypeMemory;
645 prd->cprd_flags =
646 CM_RESOURCE_MEMORY_READ_WRITE;
647 prd->cprd_sharedisp =
648 CmResourceShareDeviceExclusive;
649 prd->u.cprd_port.cprd_start.np_quad =
650 brle->start;
651 prd->u.cprd_port.cprd_len = brle->count;
652 break;
653 case SYS_RES_IRQ:
654 prd->cprd_type = CmResourceTypeInterrupt;
655 prd->cprd_flags = 0;
656 /*
657 * Always mark interrupt resources as
658 * shared, since in our implementation,
659 * they will be.
660 */
661 prd->cprd_sharedisp =
662 CmResourceShareShared;
663 prd->u.cprd_intr.cprd_level = brle->start;
664 prd->u.cprd_intr.cprd_vector = brle->start;
665 prd->u.cprd_intr.cprd_affinity = 0;
666 break;
667 default:
668 break;
669 }
670 prd++;
671 }
672 }
673
674 block->nmb_rlist = rl;
675
676 #if __FreeBSD_version < 600022
677 bad:
678
679 while (!SLIST_EMPTY(&brl_rev)) {
680 n = SLIST_FIRST(&brl_rev);
681 SLIST_REMOVE_HEAD(&brl_rev, link);
682 free (n, M_TEMP);
683 }
684 #endif
685
686 return(error);
687 }
688
689 /*
690 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
691 * packet, it will hand it to us in the form of an ndis_packet,
692 * which we need to convert to an mbuf that is then handed off
693 * to the stack. Note: we configure the mbuf list so that it uses
694 * the memory regions specified by the ndis_buffer structures in
695 * the ndis_packet as external storage. In most cases, this will
696 * point to a memory region allocated by the driver (either by
697 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
698 * the driver to handle free()ing this region for is, so we set up
699 * a dummy no-op free handler for it.
700 */
701
702 int
703 ndis_ptom(m0, p)
704 struct mbuf **m0;
705 ndis_packet *p;
706 {
707 struct mbuf *m = NULL, *prev = NULL;
708 ndis_buffer *buf;
709 ndis_packet_private *priv;
710 uint32_t totlen = 0;
711 struct ifnet *ifp;
712 struct ether_header *eh;
713 int diff;
714
715 if (p == NULL || m0 == NULL)
716 return(EINVAL);
717
718 priv = &p->np_private;
719 buf = priv->npp_head;
720 p->np_refcnt = 0;
721
722 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) {
723 if (buf == priv->npp_head)
724 #ifdef MT_HEADER
725 MGETHDR(m, M_DONTWAIT, MT_HEADER);
726 #else
727 MGETHDR(m, M_DONTWAIT, MT_DATA);
728 #endif
729 else
730 MGET(m, M_DONTWAIT, MT_DATA);
731 if (m == NULL) {
732 m_freem(*m0);
733 *m0 = NULL;
734 return(ENOBUFS);
735 }
736 m->m_len = MmGetMdlByteCount(buf);
737 m->m_data = MmGetMdlVirtualAddress(buf);
738 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
739 m->m_data, p, 0, EXT_NDIS);
740 p->np_refcnt++;
741
742 totlen += m->m_len;
743 if (m->m_flags & M_PKTHDR)
744 *m0 = m;
745 else
746 prev->m_next = m;
747 prev = m;
748 }
749
750 /*
751 * This is a hack to deal with the Marvell 8335 driver
752 * which, when associated with an AP in WPA-PSK mode,
753 * seems to overpad its frames by 8 bytes. I don't know
754 * that the extra 8 bytes are for, and they're not there
755 * in open mode, so for now clamp the frame size at 1514
756 * until I can figure out how to deal with this properly,
757 * otherwise if_ethersubr() will spank us by discarding
758 * the 'oversize' frames.
759 */
760
761 eh = mtod((*m0), struct ether_header *);
762 ifp = ((struct ndis_softc *)p->np_softc)->ifp;
763 if (totlen > ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE)) {
764 diff = totlen - ETHER_MAX_FRAME(ifp, eh->ether_type, FALSE);
765 totlen -= diff;
766 m->m_len -= diff;
767 }
768 (*m0)->m_pkthdr.len = totlen;
769
770 return(0);
771 }
772
773 /*
774 * Create an NDIS packet from an mbuf chain.
775 * This is used mainly when transmitting packets, where we need
776 * to turn an mbuf off an interface's send queue and transform it
777 * into an NDIS packet which will be fed into the NDIS driver's
778 * send routine.
779 *
780 * NDIS packets consist of two parts: an ndis_packet structure,
781 * which is vaguely analagous to the pkthdr portion of an mbuf,
782 * and one or more ndis_buffer structures, which define the
783 * actual memory segments in which the packet data resides.
784 * We need to allocate one ndis_buffer for each mbuf in a chain,
785 * plus one ndis_packet as the header.
786 */
787
788 int
789 ndis_mtop(m0, p)
790 struct mbuf *m0;
791 ndis_packet **p;
792 {
793 struct mbuf *m;
794 ndis_buffer *buf = NULL, *prev = NULL;
795 ndis_packet_private *priv;
796
797 if (p == NULL || *p == NULL || m0 == NULL)
798 return(EINVAL);
799
800 priv = &(*p)->np_private;
801 priv->npp_totlen = m0->m_pkthdr.len;
802
803 for (m = m0; m != NULL; m = m->m_next) {
804 if (m->m_len == 0)
805 continue;
806 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
807 if (buf == NULL) {
808 ndis_free_packet(*p);
809 *p = NULL;
810 return(ENOMEM);
811 }
812 MmBuildMdlForNonPagedPool(buf);
813
814 if (priv->npp_head == NULL)
815 priv->npp_head = buf;
816 else
817 prev->mdl_next = buf;
818 prev = buf;
819 }
820
821 priv->npp_tail = buf;
822
823 return(0);
824 }
825
826 int
827 ndis_get_supported_oids(arg, oids, oidcnt)
828 void *arg;
829 ndis_oid **oids;
830 int *oidcnt;
831 {
832 int len, rval;
833 ndis_oid *o;
834
835 if (arg == NULL || oids == NULL || oidcnt == NULL)
836 return(EINVAL);
837 len = 0;
838 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
839
840 o = malloc(len, M_DEVBUF, M_NOWAIT);
841 if (o == NULL)
842 return(ENOMEM);
843
844 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
845
846 if (rval) {
847 free(o, M_DEVBUF);
848 return(rval);
849 }
850
851 *oids = o;
852 *oidcnt = len / 4;
853
854 return(0);
855 }
856
857 int
858 ndis_set_info(arg, oid, buf, buflen)
859 void *arg;
860 ndis_oid oid;
861 void *buf;
862 int *buflen;
863 {
864 struct ndis_softc *sc;
865 ndis_status rval;
866 ndis_handle adapter;
867 ndis_setinfo_handler setfunc;
868 uint32_t byteswritten = 0, bytesneeded = 0;
869 uint8_t irql;
870 uint64_t duetime;
871
872 /*
873 * According to the NDIS spec, MiniportQueryInformation()
874 * and MiniportSetInformation() requests are handled serially:
875 * once one request has been issued, we must wait for it to
876 * finish before allowing another request to proceed.
877 */
878
879 sc = arg;
880
881 KeResetEvent(&sc->ndis_block->nmb_setevent);
882
883 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql);
884
885 if (sc->ndis_block->nmb_pendingreq != NULL) {
886 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
887 panic("ndis_set_info() called while other request pending");
888 } else
889 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc;
890
891 setfunc = sc->ndis_chars->nmc_setinfo_func;
892 adapter = sc->ndis_block->nmb_miniportadapterctx;
893
894 if (adapter == NULL || setfunc == NULL ||
895 sc->ndis_block->nmb_devicectx == NULL) {
896 sc->ndis_block->nmb_pendingreq = NULL;
897 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
898 return(ENXIO);
899 }
900
901 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
902 &byteswritten, &bytesneeded);
903
904 sc->ndis_block->nmb_pendingreq = NULL;
905
906 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
907
908 if (rval == NDIS_STATUS_PENDING) {
909 /* Wait up to 5 seconds. */
910 duetime = (5 * 1000000) * -10;
911 KeWaitForSingleObject(&sc->ndis_block->nmb_setevent,
912 0, 0, FALSE, &duetime);
913 rval = sc->ndis_block->nmb_setstat;
914 }
915
916 if (byteswritten)
917 *buflen = byteswritten;
918 if (bytesneeded)
919 *buflen = bytesneeded;
920
921 if (rval == NDIS_STATUS_INVALID_LENGTH)
922 return(ENOSPC);
923
924 if (rval == NDIS_STATUS_INVALID_OID)
925 return(EINVAL);
926
927 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
928 rval == NDIS_STATUS_NOT_ACCEPTED)
929 return(ENOTSUP);
930
931 if (rval != NDIS_STATUS_SUCCESS)
932 return(ENODEV);
933
934 return(0);
935 }
936
937 typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
938
939 int
940 ndis_send_packets(arg, packets, cnt)
941 void *arg;
942 ndis_packet **packets;
943 int cnt;
944 {
945 struct ndis_softc *sc;
946 ndis_handle adapter;
947 ndis_sendmulti_handler sendfunc;
948 ndis_senddone_func senddonefunc;
949 int i;
950 ndis_packet *p;
951 uint8_t irql = 0;
952
953 sc = arg;
954 adapter = sc->ndis_block->nmb_miniportadapterctx;
955 if (adapter == NULL)
956 return(ENXIO);
957 sendfunc = sc->ndis_chars->nmc_sendmulti_func;
958 senddonefunc = sc->ndis_block->nmb_senddone_func;
959
960 if (NDIS_SERIALIZED(sc->ndis_block))
961 KeAcquireSpinLock(&a |