kern_ndis.c revision 144238
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: head/sys/compat/ndis/kern_ndis.c 144238 2005-03-28 16:49:27Z 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/resource_var.h> 70#include <compat/ndis/ntoskrnl_var.h> 71#include <compat/ndis/ndis_var.h> 72#include <compat/ndis/hal_var.h> 73#include <compat/ndis/cfg_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__stdcall static void ndis_status_func(ndis_handle, ndis_status, 80 void *, uint32_t); 81__stdcall static void ndis_statusdone_func(ndis_handle); 82__stdcall static void ndis_setdone_func(ndis_handle, ndis_status); 83__stdcall static void ndis_getdone_func(ndis_handle, ndis_status); 84__stdcall static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t); 85__stdcall static void ndis_sendrsrcavail_func(ndis_handle); 86__stdcall static void ndis_intrhand(kdpc *, device_object *, 87 irp *, struct ndis_softc *); 88 89static image_patch_table kernndis_functbl[] = { 90 IMPORT_FUNC(ndis_status_func), 91 IMPORT_FUNC(ndis_statusdone_func), 92 IMPORT_FUNC(ndis_setdone_func), 93 IMPORT_FUNC(ndis_getdone_func), 94 IMPORT_FUNC(ndis_resetdone_func), 95 IMPORT_FUNC(ndis_sendrsrcavail_func), 96 97 { NULL, NULL, NULL } 98}; 99 100struct nd_head ndis_devhead; 101 102struct ndis_req { 103 void (*nr_func)(void *); 104 void *nr_arg; 105 int nr_exit; 106 STAILQ_ENTRY(ndis_req) link; 107}; 108 109struct ndisproc { 110 struct ndisqhead *np_q; 111 struct proc *np_p; 112 int np_state; 113}; 114 115static void ndis_return(void *); 116static int ndis_create_kthreads(void); 117static void ndis_destroy_kthreads(void); 118static void ndis_stop_thread(int); 119static int ndis_enlarge_thrqueue(int); 120static int ndis_shrink_thrqueue(int); 121static void ndis_runq(void *); 122 123static struct mtx ndis_thr_mtx; 124static struct mtx ndis_req_mtx; 125static STAILQ_HEAD(ndisqhead, ndis_req) ndis_ttodo; 126static struct ndisqhead ndis_itodo; 127static struct ndisqhead ndis_free; 128static int ndis_jobs = 32; 129 130static struct ndisproc ndis_tproc; 131static struct ndisproc ndis_iproc; 132 133/* 134 * This allows us to export our symbols to other modules. 135 * Note that we call ourselves 'ndisapi' to avoid a namespace 136 * collision with if_ndis.ko, which internally calls itself 137 * 'ndis.' 138 */ 139 140static int 141ndis_modevent(module_t mod, int cmd, void *arg) 142{ 143 int error = 0; 144 image_patch_table *patch; 145 146 switch (cmd) { 147 case MOD_LOAD: 148 /* Initialize subsystems */ 149 windrv_libinit(); 150 hal_libinit(); 151 ndis_libinit(); 152 ntoskrnl_libinit(); 153 usbd_libinit(); 154 155 patch = kernndis_functbl; 156 while (patch->ipt_func != NULL) { 157 windrv_wrap((funcptr)patch->ipt_func, 158 (funcptr *)&patch->ipt_wrap); 159 patch++; 160 } 161 162 ndis_create_kthreads(); 163 164 TAILQ_INIT(&ndis_devhead); 165 166 break; 167 case MOD_SHUTDOWN: 168 /* stop kthreads */ 169 ndis_destroy_kthreads(); 170 if (TAILQ_FIRST(&ndis_devhead) == NULL) { 171 /* Shut down subsystems */ 172 hal_libfini(); 173 ndis_libfini(); 174 ntoskrnl_libfini(); 175 usbd_libfini(); 176 windrv_libfini(); 177 178 patch = kernndis_functbl; 179 while (patch->ipt_func != NULL) { 180 windrv_unwrap(patch->ipt_wrap); 181 patch++; 182 } 183 } 184 break; 185 case MOD_UNLOAD: 186 /* stop kthreads */ 187 ndis_destroy_kthreads(); 188 189 /* Shut down subsystems */ 190 hal_libfini(); 191 ndis_libfini(); 192 ntoskrnl_libfini(); 193 usbd_libfini(); 194 windrv_libfini(); 195 196 patch = kernndis_functbl; 197 while (patch->ipt_func != NULL) { 198 windrv_unwrap(patch->ipt_wrap); 199 patch++; 200 } 201 202 break; 203 default: 204 error = EINVAL; 205 break; 206 } 207 208 return(error); 209} 210DEV_MODULE(ndisapi, ndis_modevent, NULL); 211MODULE_VERSION(ndisapi, 1); 212 213/* 214 * We create two kthreads for the NDIS subsystem. One of them is a task 215 * queue for performing various odd jobs. The other is an swi thread 216 * reserved exclusively for running interrupt handlers. The reason we 217 * have our own task queue is that there are some cases where we may 218 * need to sleep for a significant amount of time, and if we were to 219 * use one of the taskqueue threads, we might delay the processing 220 * of other pending tasks which might need to run right away. We have 221 * a separate swi thread because we don't want our interrupt handling 222 * to be delayed either. 223 * 224 * By default there are 32 jobs available to start, and another 8 225 * are added to the free list each time a new device is created. 226 */ 227 228static void 229ndis_runq(arg) 230 void *arg; 231{ 232 struct ndis_req *r = NULL, *die = NULL; 233 struct ndisproc *p; 234 235 p = arg; 236 237 while (1) { 238 239 /* Sleep, but preserve our original priority. */ 240 ndis_thsuspend(p->np_p, NULL, 0); 241 242 /* Look for any jobs on the work queue. */ 243 244 mtx_lock_spin(&ndis_thr_mtx); 245 p->np_state = NDIS_PSTATE_RUNNING; 246 while(STAILQ_FIRST(p->np_q) != NULL) { 247 r = STAILQ_FIRST(p->np_q); 248 STAILQ_REMOVE_HEAD(p->np_q, link); 249 mtx_unlock_spin(&ndis_thr_mtx); 250 251 /* Do the work. */ 252 253 if (r->nr_func != NULL) 254 (*r->nr_func)(r->nr_arg); 255 256 mtx_lock_spin(&ndis_thr_mtx); 257 STAILQ_INSERT_HEAD(&ndis_free, r, link); 258 259 /* Check for a shutdown request */ 260 261 if (r->nr_exit == TRUE) 262 die = r; 263 } 264 p->np_state = NDIS_PSTATE_SLEEPING; 265 mtx_unlock_spin(&ndis_thr_mtx); 266 267 /* Bail if we were told to shut down. */ 268 269 if (die != NULL) 270 break; 271 } 272 273 wakeup(die); 274#if __FreeBSD_version < 502113 275 mtx_lock(&Giant); 276#endif 277 kthread_exit(0); 278 return; /* notreached */ 279} 280 281static int 282ndis_create_kthreads() 283{ 284 struct ndis_req *r; 285 int i, error = 0; 286 287 mtx_init(&ndis_thr_mtx, "NDIS thread lock", NULL, MTX_SPIN); 288 mtx_init(&ndis_req_mtx, "NDIS request lock", MTX_NDIS_LOCK, MTX_DEF); 289 290 STAILQ_INIT(&ndis_ttodo); 291 STAILQ_INIT(&ndis_itodo); 292 STAILQ_INIT(&ndis_free); 293 294 for (i = 0; i < ndis_jobs; i++) { 295 r = malloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK); 296 if (r == NULL) { 297 error = ENOMEM; 298 break; 299 } 300 STAILQ_INSERT_HEAD(&ndis_free, r, link); 301 } 302 303 if (error == 0) { 304 ndis_tproc.np_q = &ndis_ttodo; 305 ndis_tproc.np_state = NDIS_PSTATE_SLEEPING; 306 error = kthread_create(ndis_runq, &ndis_tproc, 307 &ndis_tproc.np_p, RFHIGHPID, 308 NDIS_KSTACK_PAGES, "ndis taskqueue"); 309 } 310 311 if (error == 0) { 312 ndis_iproc.np_q = &ndis_itodo; 313 ndis_iproc.np_state = NDIS_PSTATE_SLEEPING; 314 error = kthread_create(ndis_runq, &ndis_iproc, 315 &ndis_iproc.np_p, RFHIGHPID, 316 NDIS_KSTACK_PAGES, "ndis swi"); 317 } 318 319 if (error) { 320 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) { 321 STAILQ_REMOVE_HEAD(&ndis_free, link); 322 free(r, M_DEVBUF); 323 } 324 return(error); 325 } 326 327 return(0); 328} 329 330static void 331ndis_destroy_kthreads() 332{ 333 struct ndis_req *r; 334 335 /* Stop the threads. */ 336 337 ndis_stop_thread(NDIS_TASKQUEUE); 338 ndis_stop_thread(NDIS_SWI); 339 340 /* Destroy request structures. */ 341 342 while ((r = STAILQ_FIRST(&ndis_free)) != NULL) { 343 STAILQ_REMOVE_HEAD(&ndis_free, link); 344 free(r, M_DEVBUF); 345 } 346 347 mtx_destroy(&ndis_req_mtx); 348 mtx_destroy(&ndis_thr_mtx); 349 350 return; 351} 352 353static void 354ndis_stop_thread(t) 355 int t; 356{ 357 struct ndis_req *r; 358 struct ndisqhead *q; 359 struct proc *p; 360 361 if (t == NDIS_TASKQUEUE) { 362 q = &ndis_ttodo; 363 p = ndis_tproc.np_p; 364 } else { 365 q = &ndis_itodo; 366 p = ndis_iproc.np_p; 367 } 368 369 /* Create and post a special 'exit' job. */ 370 371 mtx_lock_spin(&ndis_thr_mtx); 372 r = STAILQ_FIRST(&ndis_free); 373 STAILQ_REMOVE_HEAD(&ndis_free, link); 374 r->nr_func = NULL; 375 r->nr_arg = NULL; 376 r->nr_exit = TRUE; 377 STAILQ_INSERT_TAIL(q, r, link); 378 mtx_unlock_spin(&ndis_thr_mtx); 379 380 ndis_thresume(p); 381 382 /* wait for thread exit */ 383 384 tsleep(r, curthread->td_priority|PCATCH, "ndisthexit", hz * 60); 385 386 /* Now empty the job list. */ 387 388 mtx_lock_spin(&ndis_thr_mtx); 389 while ((r = STAILQ_FIRST(q)) != NULL) { 390 STAILQ_REMOVE_HEAD(q, link); 391 STAILQ_INSERT_HEAD(&ndis_free, r, link); 392 } 393 mtx_unlock_spin(&ndis_thr_mtx); 394 395 return; 396} 397 398static int 399ndis_enlarge_thrqueue(cnt) 400 int cnt; 401{ 402 struct ndis_req *r; 403 int i; 404 405 for (i = 0; i < cnt; i++) { 406 r = malloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK); 407 if (r == NULL) 408 return(ENOMEM); 409 mtx_lock_spin(&ndis_thr_mtx); 410 STAILQ_INSERT_HEAD(&ndis_free, r, link); 411 ndis_jobs++; 412 mtx_unlock_spin(&ndis_thr_mtx); 413 } 414 415 return(0); 416} 417 418static int 419ndis_shrink_thrqueue(cnt) 420 int cnt; 421{ 422 struct ndis_req *r; 423 int i; 424 425 for (i = 0; i < cnt; i++) { 426 mtx_lock_spin(&ndis_thr_mtx); 427 r = STAILQ_FIRST(&ndis_free); 428 if (r == NULL) { 429 mtx_unlock_spin(&ndis_thr_mtx); 430 return(ENOMEM); 431 } 432 STAILQ_REMOVE_HEAD(&ndis_free, link); 433 ndis_jobs--; 434 mtx_unlock_spin(&ndis_thr_mtx); 435 free(r, M_DEVBUF); 436 } 437 438 return(0); 439} 440 441int 442ndis_unsched(func, arg, t) 443 void (*func)(void *); 444 void *arg; 445 int t; 446{ 447 struct ndis_req *r; 448 struct ndisqhead *q; 449 struct proc *p; 450 451 if (t == NDIS_TASKQUEUE) { 452 q = &ndis_ttodo; 453 p = ndis_tproc.np_p; 454 } else { 455 q = &ndis_itodo; 456 p = ndis_iproc.np_p; 457 } 458 459 mtx_lock_spin(&ndis_thr_mtx); 460 STAILQ_FOREACH(r, q, link) { 461 if (r->nr_func == func && r->nr_arg == arg) { 462 STAILQ_REMOVE(q, r, ndis_req, link); 463 STAILQ_INSERT_HEAD(&ndis_free, r, link); 464 mtx_unlock_spin(&ndis_thr_mtx); 465 return(0); 466 } 467 } 468 469 mtx_unlock_spin(&ndis_thr_mtx); 470 471 return(ENOENT); 472} 473 474int 475ndis_sched(func, arg, t) 476 void (*func)(void *); 477 void *arg; 478 int t; 479{ 480 struct ndis_req *r; 481 struct ndisqhead *q; 482 struct proc *p; 483 int s; 484 485 if (t == NDIS_TASKQUEUE) { 486 q = &ndis_ttodo; 487 p = ndis_tproc.np_p; 488 } else { 489 q = &ndis_itodo; 490 p = ndis_iproc.np_p; 491 } 492 493 mtx_lock_spin(&ndis_thr_mtx); 494 /* 495 * Check to see if an instance of this job is already 496 * pending. If so, don't bother queuing it again. 497 */ 498 STAILQ_FOREACH(r, q, link) { 499 if (r->nr_func == func && r->nr_arg == arg) { 500 mtx_unlock_spin(&ndis_thr_mtx); 501 return(0); 502 } 503 } 504 r = STAILQ_FIRST(&ndis_free); 505 if (r == NULL) { 506 mtx_unlock_spin(&ndis_thr_mtx); 507 return(EAGAIN); 508 } 509 STAILQ_REMOVE_HEAD(&ndis_free, link); 510 r->nr_func = func; 511 r->nr_arg = arg; 512 r->nr_exit = FALSE; 513 STAILQ_INSERT_TAIL(q, r, link); 514 if (t == NDIS_TASKQUEUE) 515 s = ndis_tproc.np_state; 516 else 517 s = ndis_iproc.np_state; 518 mtx_unlock_spin(&ndis_thr_mtx); 519 520 /* 521 * Post the job, but only if the thread is actually blocked 522 * on its own suspend call. If a driver queues up a job with 523 * NdisScheduleWorkItem() which happens to do a KeWaitForObject(), 524 * it may suspend there, and in that case we don't want to wake 525 * it up until KeWaitForObject() gets woken up on its own. 526 */ 527 if (s == NDIS_PSTATE_SLEEPING) 528 ndis_thresume(p); 529 530 return(0); 531} 532 533int 534ndis_thsuspend(p, m, timo) 535 struct proc *p; 536 struct mtx *m; 537 int timo; 538{ 539 int error; 540 541 if (m != NULL) { 542 error = msleep(&p->p_siglist, m, 543 curthread->td_priority, "ndissp", timo); 544 } else { 545 PROC_LOCK(p); 546 error = msleep(&p->p_siglist, &p->p_mtx, 547 curthread->td_priority|PDROP, "ndissp", timo); 548 } 549 550 return(error); 551} 552 553void 554ndis_thresume(p) 555 struct proc *p; 556{ 557 wakeup(&p->p_siglist); 558 return; 559} 560 561__stdcall static void 562ndis_sendrsrcavail_func(adapter) 563 ndis_handle adapter; 564{ 565 return; 566} 567 568__stdcall static void 569ndis_status_func(adapter, status, sbuf, slen) 570 ndis_handle adapter; 571 ndis_status status; 572 void *sbuf; 573 uint32_t slen; 574{ 575 ndis_miniport_block *block; 576 struct ndis_softc *sc; 577 struct ifnet *ifp; 578 579 block = adapter; 580 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 581 ifp = &sc->arpcom.ac_if; 582 if (ifp->if_flags & IFF_DEBUG) 583 device_printf (sc->ndis_dev, "status: %x\n", status); 584 return; 585} 586 587__stdcall static void 588ndis_statusdone_func(adapter) 589 ndis_handle adapter; 590{ 591 ndis_miniport_block *block; 592 struct ndis_softc *sc; 593 struct ifnet *ifp; 594 595 block = adapter; 596 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 597 ifp = &sc->arpcom.ac_if; 598 if (ifp->if_flags & IFF_DEBUG) 599 device_printf (sc->ndis_dev, "status complete\n"); 600 return; 601} 602 603__stdcall static void 604ndis_setdone_func(adapter, status) 605 ndis_handle adapter; 606 ndis_status status; 607{ 608 ndis_miniport_block *block; 609 block = adapter; 610 611 block->nmb_setstat = status; 612 wakeup(&block->nmb_setstat); 613 return; 614} 615 616__stdcall static void 617ndis_getdone_func(adapter, status) 618 ndis_handle adapter; 619 ndis_status status; 620{ 621 ndis_miniport_block *block; 622 block = adapter; 623 624 block->nmb_getstat = status; 625 wakeup(&block->nmb_getstat); 626 return; 627} 628 629__stdcall static void 630ndis_resetdone_func(adapter, status, addressingreset) 631 ndis_handle adapter; 632 ndis_status status; 633 uint8_t addressingreset; 634{ 635 ndis_miniport_block *block; 636 struct ndis_softc *sc; 637 struct ifnet *ifp; 638 639 block = adapter; 640 sc = device_get_softc(block->nmb_physdeviceobj->do_devext); 641 ifp = &sc->arpcom.ac_if; 642 643 if (ifp->if_flags & IFF_DEBUG) 644 device_printf (sc->ndis_dev, "reset done...\n"); 645 wakeup(sc); 646 return; 647} 648 649int 650ndis_create_sysctls(arg) 651 void *arg; 652{ 653 struct ndis_softc *sc; 654 ndis_cfg *vals; 655 char buf[256]; 656 struct sysctl_oid *oidp; 657 struct sysctl_ctx_entry *e; 658 659 if (arg == NULL) 660 return(EINVAL); 661 662 sc = arg; 663 vals = sc->ndis_regvals; 664 665 TAILQ_INIT(&sc->ndis_cfglist_head); 666 667#if __FreeBSD_version < 502113 668 /* Create the sysctl tree. */ 669 670 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx, 671 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 672 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0, 673 device_get_desc(sc->ndis_dev)); 674 675#endif 676 /* Add the driver-specific registry keys. */ 677 678 vals = sc->ndis_regvals; 679 while(1) { 680 if (vals->nc_cfgkey == NULL) 681 break; 682 if (vals->nc_idx != sc->ndis_devidx) { 683 vals++; 684 continue; 685 } 686 687 /* See if we already have a sysctl with this name */ 688 689 oidp = NULL; 690#if __FreeBSD_version < 502113 691 TAILQ_FOREACH(e, &sc->ndis_ctx, link) { 692#else 693 TAILQ_FOREACH(e, device_get_sysctl_ctx(sc->ndis_dev), link) { 694#endif 695 oidp = e->entry; 696 if (ndis_strcasecmp(oidp->oid_name, 697 vals->nc_cfgkey) == 0) 698 break; 699 oidp = NULL; 700 } 701 702 if (oidp != NULL) { 703 vals++; 704 continue; 705 } 706 707#if __FreeBSD_version < 502113 708 SYSCTL_ADD_STRING(&sc->ndis_ctx, 709 SYSCTL_CHILDREN(sc->ndis_tree), 710#else 711 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev), 712 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)), 713#endif 714 OID_AUTO, vals->nc_cfgkey, 715 CTLFLAG_RW, vals->nc_val, 716 sizeof(vals->nc_val), 717 vals->nc_cfgdesc); 718 vals++; 719 } 720 721 /* Now add a couple of builtin keys. */ 722 723 /* 724 * Environment can be either Windows (0) or WindowsNT (1). 725 * We qualify as the latter. 726 */ 727 ndis_add_sysctl(sc, "Environment", 728 "Windows environment", "1", CTLFLAG_RD); 729 730 /* NDIS version should be 5.1. */ 731 ndis_add_sysctl(sc, "NdisVersion", 732 "NDIS API Version", "0x00050001", CTLFLAG_RD); 733 734 /* Bus type (PCI, PCMCIA, etc...) */ 735 sprintf(buf, "%d", (int)sc->ndis_iftype); 736 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD); 737 738 if (sc->ndis_res_io != NULL) { 739 sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io)); 740 ndis_add_sysctl(sc, "IOBaseAddress", 741 "Base I/O Address", buf, CTLFLAG_RD); 742 } 743 744 if (sc->ndis_irq != NULL) { 745 sprintf(buf, "%lu", rman_get_start(sc->ndis_irq)); 746 ndis_add_sysctl(sc, "InterruptNumber", 747 "Interrupt Number", buf, CTLFLAG_RD); 748 } 749 750 return(0); 751} 752 753int 754ndis_add_sysctl(arg, key, desc, val, flag) 755 void *arg; 756 char *key; 757 char *desc; 758 char *val; 759 int flag; 760{ 761 struct ndis_softc *sc; 762 struct ndis_cfglist *cfg; 763 char descstr[256]; 764 765 sc = arg; 766 767 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO); 768 769 if (cfg == NULL) 770 return(ENOMEM); 771 772 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF); 773 if (desc == NULL) { 774 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key); 775 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF); 776 } else 777 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF); 778 strcpy(cfg->ndis_cfg.nc_val, val); 779 780 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link); 781 782#if __FreeBSD_version < 502113 783 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree), 784#else 785 SYSCTL_ADD_STRING(device_get_sysctl_ctx(sc->ndis_dev), 786 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ndis_dev)), 787#endif 788 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag, 789 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val), 790 cfg->ndis_cfg.nc_cfgdesc); 791 792 return(0); 793} 794 795int 796ndis_flush_sysctls(arg) 797 void *arg; 798{ 799 struct ndis_softc *sc; 800 struct ndis_cfglist *cfg; 801 802 sc = arg; 803 804 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) { 805 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head); 806 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link); 807 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF); 808 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF); 809 free(cfg, M_DEVBUF); 810 } 811 812 return(0); 813} 814 815static void 816ndis_return(arg) 817 void *arg; 818{ 819 struct ndis_softc *sc; 820 __stdcall ndis_return_handler returnfunc; 821 ndis_handle adapter; 822 ndis_packet *p; 823 uint8_t irql; 824 825 p = arg; 826 sc = p->np_softc; 827 adapter = sc->ndis_block->nmb_miniportadapterctx; 828 829 if (adapter == NULL) 830 return; 831 832 returnfunc = sc->ndis_chars->nmc_return_packet_func; 833 834 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 835 MSCALL2(returnfunc, adapter, p); 836 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 837 838 return; 839} 840 841void 842ndis_return_packet(buf, arg) 843 void *buf; /* not used */ 844 void *arg; 845{ 846 ndis_packet *p; 847 848 if (arg == NULL) 849 return; 850 851 p = arg; 852 853 /* Decrement refcount. */ 854 p->np_refcnt--; 855 856 /* Release packet when refcount hits zero, otherwise return. */ 857 if (p->np_refcnt) 858 return; 859 860 ndis_sched(ndis_return, p, NDIS_TASKQUEUE); 861 862 return; 863} 864 865void 866ndis_free_bufs(b0) 867 ndis_buffer *b0; 868{ 869 ndis_buffer *next; 870 871 if (b0 == NULL) 872 return; 873 874 while(b0 != NULL) { 875 next = b0->mdl_next; 876 IoFreeMdl(b0); 877 b0 = next; 878 } 879 880 return; 881} 882int in_reset = 0; 883void 884ndis_free_packet(p) 885 ndis_packet *p; 886{ 887 if (p == NULL) 888 return; 889 890 ndis_free_bufs(p->np_private.npp_head); 891 NdisFreePacket(p); 892 return; 893} 894 895int 896ndis_convert_res(arg) 897 void *arg; 898{ 899 struct ndis_softc *sc; 900 ndis_resource_list *rl = NULL; 901 cm_partial_resource_desc *prd = NULL; 902 ndis_miniport_block *block; 903 device_t dev; 904 struct resource_list *brl; 905 struct resource_list_entry *brle; 906#if __FreeBSD_version < 600022 907 struct resource_list brl_rev; 908 struct resource_list_entry *n; 909#endif 910 int error = 0; 911 912 sc = arg; 913 block = sc->ndis_block; 914 dev = sc->ndis_dev; 915 916#if __FreeBSD_version < 600022 917 SLIST_INIT(&brl_rev); 918#endif 919 920 rl = malloc(sizeof(ndis_resource_list) + 921 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)), 922 M_DEVBUF, M_NOWAIT|M_ZERO); 923 924 if (rl == NULL) 925 return(ENOMEM); 926 927 rl->cprl_version = 5; 928 rl->cprl_version = 1; 929 rl->cprl_count = sc->ndis_rescnt; 930 prd = rl->cprl_partial_descs; 931 932 brl = BUS_GET_RESOURCE_LIST(dev, dev); 933 934 if (brl != NULL) { 935 936#if __FreeBSD_version < 600022 937 /* 938 * We have a small problem. Some PCI devices have 939 * multiple I/O ranges. Windows orders them starting 940 * from lowest numbered BAR to highest. We discover 941 * them in that order too, but insert them into a singly 942 * linked list head first, which means when time comes 943 * to traverse the list, we enumerate them in reverse 944 * order. This screws up some drivers which expect the 945 * BARs to be in ascending order so that they can choose 946 * the "first" one as their register space. Unfortunately, 947 * in order to fix this, we have to create our own 948 * temporary list with the entries in reverse order. 949 */ 950 SLIST_FOREACH(brle, brl, link) { 951 n = malloc(sizeof(struct resource_list_entry), 952 M_TEMP, M_NOWAIT); 953 if (n == NULL) { 954 error = ENOMEM; 955 goto bad; 956 } 957 bcopy((char *)brle, (char *)n, 958 sizeof(struct resource_list_entry)); 959 SLIST_INSERT_HEAD(&brl_rev, n, link); 960 } 961 962 SLIST_FOREACH(brle, &brl_rev, link) { 963#else 964 STAILQ_FOREACH(brle, brl, link) { 965#endif 966 switch (brle->type) { 967 case SYS_RES_IOPORT: 968 prd->cprd_type = CmResourceTypePort; 969 prd->cprd_flags = CM_RESOURCE_PORT_IO; 970 prd->cprd_sharedisp = 971 CmResourceShareDeviceExclusive; 972 prd->u.cprd_port.cprd_start.np_quad = 973 brle->start; 974 prd->u.cprd_port.cprd_len = brle->count; 975 break; 976 case SYS_RES_MEMORY: 977 prd->cprd_type = CmResourceTypeMemory; 978 prd->cprd_flags = 979 CM_RESOURCE_MEMORY_READ_WRITE; 980 prd->cprd_sharedisp = 981 CmResourceShareDeviceExclusive; 982 prd->u.cprd_port.cprd_start.np_quad = 983 brle->start; 984 prd->u.cprd_port.cprd_len = brle->count; 985 break; 986 case SYS_RES_IRQ: 987 prd->cprd_type = CmResourceTypeInterrupt; 988 prd->cprd_flags = 0; 989 prd->cprd_sharedisp = 990 CmResourceShareDeviceExclusive; 991 prd->u.cprd_intr.cprd_level = brle->start; 992 prd->u.cprd_intr.cprd_vector = brle->start; 993 prd->u.cprd_intr.cprd_affinity = 0; 994 break; 995 default: 996 break; 997 } 998 prd++; 999 } 1000 } 1001 1002 block->nmb_rlist = rl; 1003 1004#if __FreeBSD_version < 600022 1005bad: 1006 1007 while (!SLIST_EMPTY(&brl_rev)) { 1008 n = SLIST_FIRST(&brl_rev); 1009 SLIST_REMOVE_HEAD(&brl_rev, link); 1010 free (n, M_TEMP); 1011 } 1012#endif 1013 1014 return(error); 1015} 1016 1017/* 1018 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a 1019 * packet, it will hand it to us in the form of an ndis_packet, 1020 * which we need to convert to an mbuf that is then handed off 1021 * to the stack. Note: we configure the mbuf list so that it uses 1022 * the memory regions specified by the ndis_buffer structures in 1023 * the ndis_packet as external storage. In most cases, this will 1024 * point to a memory region allocated by the driver (either by 1025 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect 1026 * the driver to handle free()ing this region for is, so we set up 1027 * a dummy no-op free handler for it. 1028 */ 1029 1030int 1031ndis_ptom(m0, p) 1032 struct mbuf **m0; 1033 ndis_packet *p; 1034{ 1035 struct mbuf *m, *prev = NULL; 1036 ndis_buffer *buf; 1037 ndis_packet_private *priv; 1038 uint32_t totlen = 0; 1039 1040 if (p == NULL || m0 == NULL) 1041 return(EINVAL); 1042 1043 priv = &p->np_private; 1044 buf = priv->npp_head; 1045 p->np_refcnt = 0; 1046 1047 for (buf = priv->npp_head; buf != NULL; buf = buf->mdl_next) { 1048 if (buf == priv->npp_head) 1049 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1050 else 1051 MGET(m, M_DONTWAIT, MT_DATA); 1052 if (m == NULL) { 1053 m_freem(*m0); 1054 *m0 = NULL; 1055 return(ENOBUFS); 1056 } 1057 m->m_len = MmGetMdlByteCount(buf); 1058 m->m_data = MmGetMdlVirtualAddress(buf); 1059 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet, 1060 p, 0, EXT_NDIS); 1061 p->np_refcnt++; 1062 totlen += m->m_len; 1063 if (m->m_flags & MT_HEADER) 1064 *m0 = m; 1065 else 1066 prev->m_next = m; 1067 prev = m; 1068 } 1069 1070 (*m0)->m_pkthdr.len = totlen; 1071 1072 return(0); 1073} 1074 1075/* 1076 * Create an NDIS packet from an mbuf chain. 1077 * This is used mainly when transmitting packets, where we need 1078 * to turn an mbuf off an interface's send queue and transform it 1079 * into an NDIS packet which will be fed into the NDIS driver's 1080 * send routine. 1081 * 1082 * NDIS packets consist of two parts: an ndis_packet structure, 1083 * which is vaguely analagous to the pkthdr portion of an mbuf, 1084 * and one or more ndis_buffer structures, which define the 1085 * actual memory segments in which the packet data resides. 1086 * We need to allocate one ndis_buffer for each mbuf in a chain, 1087 * plus one ndis_packet as the header. 1088 */ 1089 1090int 1091ndis_mtop(m0, p) 1092 struct mbuf *m0; 1093 ndis_packet **p; 1094{ 1095 struct mbuf *m; 1096 ndis_buffer *buf = NULL, *prev = NULL; 1097 ndis_packet_private *priv; 1098 1099 if (p == NULL || *p == NULL || m0 == NULL) 1100 return(EINVAL); 1101 1102 priv = &(*p)->np_private; 1103 priv->npp_totlen = m0->m_pkthdr.len; 1104 1105 for (m = m0; m != NULL; m = m->m_next) { 1106 if (m->m_len == 0) 1107 continue; 1108 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL); 1109 if (buf == NULL) { 1110 ndis_free_packet(*p); 1111 *p = NULL; 1112 return(ENOMEM); 1113 } 1114 1115 if (priv->npp_head == NULL) 1116 priv->npp_head = buf; 1117 else 1118 prev->mdl_next = buf; 1119 prev = buf; 1120 } 1121 1122 priv->npp_tail = buf; 1123 1124 return(0); 1125} 1126 1127int 1128ndis_get_supported_oids(arg, oids, oidcnt) 1129 void *arg; 1130 ndis_oid **oids; 1131 int *oidcnt; 1132{ 1133 int len, rval; 1134 ndis_oid *o; 1135 1136 if (arg == NULL || oids == NULL || oidcnt == NULL) 1137 return(EINVAL); 1138 len = 0; 1139 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len); 1140 1141 o = malloc(len, M_DEVBUF, M_NOWAIT); 1142 if (o == NULL) 1143 return(ENOMEM); 1144 1145 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len); 1146 1147 if (rval) { 1148 free(o, M_DEVBUF); 1149 return(rval); 1150 } 1151 1152 *oids = o; 1153 *oidcnt = len / 4; 1154 1155 return(0); 1156} 1157 1158int 1159ndis_set_info(arg, oid, buf, buflen) 1160 void *arg; 1161 ndis_oid oid; 1162 void *buf; 1163 int *buflen; 1164{ 1165 struct ndis_softc *sc; 1166 ndis_status rval; 1167 ndis_handle adapter; 1168 __stdcall ndis_setinfo_handler setfunc; 1169 uint32_t byteswritten = 0, bytesneeded = 0; 1170 int error; 1171 uint8_t irql; 1172 1173 /* 1174 * According to the NDIS spec, MiniportQueryInformation() 1175 * and MiniportSetInformation() requests are handled serially: 1176 * once one request has been issued, we must wait for it to 1177 * finish before allowing another request to proceed. 1178 */ 1179 1180 sc = arg; 1181 1182 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1183 1184 if (sc->ndis_block->nmb_pendingreq != NULL) 1185 panic("ndis_set_info() called while other request pending"); 1186 else 1187 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc; 1188 1189 setfunc = sc->ndis_chars->nmc_setinfo_func; 1190 adapter = sc->ndis_block->nmb_miniportadapterctx; 1191 1192 if (adapter == NULL || setfunc == NULL) { 1193 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1194 return(ENXIO); 1195 } 1196 1197 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen, 1198 &byteswritten, &bytesneeded); 1199 1200 sc->ndis_block->nmb_pendingreq = NULL; 1201 1202 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1203 1204 if (rval == NDIS_STATUS_PENDING) { 1205 mtx_lock(&ndis_req_mtx); 1206 error = msleep(&sc->ndis_block->nmb_setstat, 1207 &ndis_req_mtx, 1208 curthread->td_priority|PDROP, 1209 "ndisset", 5 * hz); 1210 rval = sc->ndis_block->nmb_setstat; 1211 } 1212 1213 1214 if (byteswritten) 1215 *buflen = byteswritten; 1216 if (bytesneeded) 1217 *buflen = bytesneeded; 1218 1219 if (rval == NDIS_STATUS_INVALID_LENGTH) 1220 return(ENOSPC); 1221 1222 if (rval == NDIS_STATUS_INVALID_OID) 1223 return(EINVAL); 1224 1225 if (rval == NDIS_STATUS_NOT_SUPPORTED || 1226 rval == NDIS_STATUS_NOT_ACCEPTED) 1227 return(ENOTSUP); 1228 1229 if (rval != NDIS_STATUS_SUCCESS) 1230 return(ENODEV); 1231 1232 return(0); 1233} 1234 1235typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status); 1236 1237int 1238ndis_send_packets(arg, packets, cnt) 1239 void *arg; 1240 ndis_packet **packets; 1241 int cnt; 1242{ 1243 struct ndis_softc *sc; 1244 ndis_handle adapter; 1245 __stdcall ndis_sendmulti_handler sendfunc; 1246 __stdcall ndis_senddone_func senddonefunc; 1247 int i; 1248 ndis_packet *p; 1249 uint8_t irql; 1250 1251 sc = arg; 1252 adapter = sc->ndis_block->nmb_miniportadapterctx; 1253 if (adapter == NULL) 1254 return(ENXIO); 1255 sendfunc = sc->ndis_chars->nmc_sendmulti_func; 1256 senddonefunc = sc->ndis_block->nmb_senddone_func; 1257 1258 if (NDIS_SERIALIZED(sc->ndis_block)) 1259 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1260 1261 MSCALL3(sendfunc, adapter, packets, cnt); 1262 1263 for (i = 0; i < cnt; i++) { 1264 p = packets[i]; 1265 /* 1266 * Either the driver already handed the packet to 1267 * ndis_txeof() due to a failure, or it wants to keep 1268 * it and release it asynchronously later. Skip to the 1269 * next one. 1270 */ 1271 if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING) 1272 continue; 1273 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status); 1274 } 1275 1276 if (NDIS_SERIALIZED(sc->ndis_block)) 1277 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1278 1279 return(0); 1280} 1281 1282int 1283ndis_send_packet(arg, packet) 1284 void *arg; 1285 ndis_packet *packet; 1286{ 1287 struct ndis_softc *sc; 1288 ndis_handle adapter; 1289 ndis_status status; 1290 __stdcall ndis_sendsingle_handler sendfunc; 1291 __stdcall ndis_senddone_func senddonefunc; 1292 uint8_t irql; 1293 1294 sc = arg; 1295 adapter = sc->ndis_block->nmb_miniportadapterctx; 1296 if (adapter == NULL) 1297 return(ENXIO); 1298 sendfunc = sc->ndis_chars->nmc_sendsingle_func; 1299 senddonefunc = sc->ndis_block->nmb_senddone_func; 1300 1301 if (NDIS_SERIALIZED(sc->ndis_block)) 1302 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1303 status = MSCALL3(sendfunc, adapter, packet, 1304 packet->np_private.npp_flags); 1305 1306 if (status == NDIS_STATUS_PENDING) { 1307 if (NDIS_SERIALIZED(sc->ndis_block)) 1308 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1309 return(0); 1310 } 1311 1312 MSCALL3(senddonefunc, sc->ndis_block, packet, status); 1313 1314 if (NDIS_SERIALIZED(sc->ndis_block)) 1315 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1316 1317 return(0); 1318} 1319 1320int 1321ndis_init_dma(arg) 1322 void *arg; 1323{ 1324 struct ndis_softc *sc; 1325 int i, error; 1326 1327 sc = arg; 1328 1329 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts, 1330 M_DEVBUF, M_NOWAIT|M_ZERO); 1331 1332 if (sc->ndis_tmaps == NULL) 1333 return(ENOMEM); 1334 1335 for (i = 0; i < sc->ndis_maxpkts; i++) { 1336 error = bus_dmamap_create(sc->ndis_ttag, 0, 1337 &sc->ndis_tmaps[i]); 1338 if (error) { 1339 free(sc->ndis_tmaps, M_DEVBUF); 1340 return(ENODEV); 1341 } 1342 } 1343 1344 return(0); 1345} 1346 1347int 1348ndis_destroy_dma(arg) 1349 void *arg; 1350{ 1351 struct ndis_softc *sc; 1352 struct mbuf *m; 1353 ndis_packet *p = NULL; 1354 int i; 1355 1356 sc = arg; 1357 1358 for (i = 0; i < sc->ndis_maxpkts; i++) { 1359 if (sc->ndis_txarray[i] != NULL) { 1360 p = sc->ndis_txarray[i]; 1361 m = (struct mbuf *)p->np_rsvd[1]; 1362 if (m != NULL) 1363 m_freem(m); 1364 ndis_free_packet(sc->ndis_txarray[i]); 1365 } 1366 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]); 1367 } 1368 1369 free(sc->ndis_tmaps, M_DEVBUF); 1370 1371 bus_dma_tag_destroy(sc->ndis_ttag); 1372 1373 return(0); 1374} 1375 1376int 1377ndis_reset_nic(arg) 1378 void *arg; 1379{ 1380 struct ndis_softc *sc; 1381 ndis_handle adapter; 1382 __stdcall ndis_reset_handler resetfunc; 1383 uint8_t addressing_reset; 1384 struct ifnet *ifp; 1385 int rval; 1386 uint8_t irql; 1387 1388 sc = arg; 1389 ifp = &sc->arpcom.ac_if; 1390 1391 adapter = sc->ndis_block->nmb_miniportadapterctx; 1392 resetfunc = sc->ndis_chars->nmc_reset_func; 1393 1394 if (adapter == NULL || resetfunc == NULL) 1395 return(EIO); 1396 1397 if (NDIS_SERIALIZED(sc->ndis_block)) 1398 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1399 1400 rval = MSCALL2(resetfunc, &addressing_reset, adapter); 1401 1402 if (NDIS_SERIALIZED(sc->ndis_block)) 1403 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1404 1405 if (rval == NDIS_STATUS_PENDING) { 1406 mtx_lock(&ndis_req_mtx); 1407 msleep(sc, &ndis_req_mtx, 1408 curthread->td_priority|PDROP, "ndisrst", 0); 1409 } 1410 1411 return(0); 1412} 1413 1414int 1415ndis_halt_nic(arg) 1416 void *arg; 1417{ 1418 struct ndis_softc *sc; 1419 ndis_handle adapter; 1420 __stdcall ndis_halt_handler haltfunc; 1421 struct ifnet *ifp; 1422 1423 sc = arg; 1424 ifp = &sc->arpcom.ac_if; 1425 1426 NDIS_LOCK(sc); 1427 adapter = sc->ndis_block->nmb_miniportadapterctx; 1428 if (adapter == NULL) { 1429 NDIS_UNLOCK(sc); 1430 return(EIO); 1431 } 1432 1433 /* 1434 * The adapter context is only valid after the init 1435 * handler has been called, and is invalid once the 1436 * halt handler has been called. 1437 */ 1438 1439 haltfunc = sc->ndis_chars->nmc_halt_func; 1440 NDIS_UNLOCK(sc); 1441 1442 MSCALL1(haltfunc, adapter); 1443 1444 NDIS_LOCK(sc); 1445 sc->ndis_block->nmb_miniportadapterctx = NULL; 1446 NDIS_UNLOCK(sc); 1447 1448 return(0); 1449} 1450 1451int 1452ndis_shutdown_nic(arg) 1453 void *arg; 1454{ 1455 struct ndis_softc *sc; 1456 ndis_handle adapter; 1457 __stdcall ndis_shutdown_handler shutdownfunc; 1458 1459 sc = arg; 1460 NDIS_LOCK(sc); 1461 adapter = sc->ndis_block->nmb_miniportadapterctx; 1462 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler; 1463 NDIS_UNLOCK(sc); 1464 if (adapter == NULL || shutdownfunc == NULL) 1465 return(EIO); 1466 1467 if (sc->ndis_chars->nmc_rsvd0 == NULL) 1468 MSCALL1(shutdownfunc, adapter); 1469 else 1470 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0); 1471 1472 ndis_shrink_thrqueue(8); 1473 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link); 1474 1475 return(0); 1476} 1477 1478int 1479ndis_init_nic(arg) 1480 void *arg; 1481{ 1482 struct ndis_softc *sc; 1483 ndis_miniport_block *block; 1484 __stdcall ndis_init_handler initfunc; 1485 ndis_status status, openstatus = 0; 1486 ndis_medium mediumarray[NdisMediumMax]; 1487 uint32_t chosenmedium, i; 1488 1489 if (arg == NULL) 1490 return(EINVAL); 1491 1492 sc = arg; 1493 NDIS_LOCK(sc); 1494 block = sc->ndis_block; 1495 initfunc = sc->ndis_chars->nmc_init_func; 1496 NDIS_UNLOCK(sc); 1497 1498 for (i = 0; i < NdisMediumMax; i++) 1499 mediumarray[i] = i; 1500 1501 status = MSCALL6(initfunc, &openstatus, &chosenmedium, 1502 mediumarray, NdisMediumMax, block, block); 1503 1504 /* 1505 * If the init fails, blow away the other exported routines 1506 * we obtained from the driver so we can't call them later. 1507 * If the init failed, none of these will work. 1508 */ 1509 if (status != NDIS_STATUS_SUCCESS) { 1510 NDIS_LOCK(sc); 1511 sc->ndis_block->nmb_miniportadapterctx = NULL; 1512 NDIS_UNLOCK(sc); 1513 return(ENXIO); 1514 } 1515 1516 return(0); 1517} 1518 1519void 1520ndis_enable_intr(arg) 1521 void *arg; 1522{ 1523 struct ndis_softc *sc; 1524 ndis_handle adapter; 1525 __stdcall ndis_enable_interrupts_handler intrenbfunc; 1526 1527 sc = arg; 1528 adapter = sc->ndis_block->nmb_miniportadapterctx; 1529 intrenbfunc = sc->ndis_chars->nmc_enable_interrupts_func; 1530 if (adapter == NULL || intrenbfunc == NULL) 1531 return; 1532 MSCALL1(intrenbfunc, adapter); 1533 1534 return; 1535} 1536 1537void 1538ndis_disable_intr(arg) 1539 void *arg; 1540{ 1541 struct ndis_softc *sc; 1542 ndis_handle adapter; 1543 __stdcall ndis_disable_interrupts_handler intrdisfunc; 1544 1545 sc = arg; 1546 adapter = sc->ndis_block->nmb_miniportadapterctx; 1547 intrdisfunc = sc->ndis_chars->nmc_disable_interrupts_func; 1548 if (adapter == NULL || intrdisfunc == NULL) 1549 return; 1550 1551 MSCALL1(intrdisfunc, adapter); 1552 1553 return; 1554} 1555 1556int 1557ndis_isr(arg, ourintr, callhandler) 1558 void *arg; 1559 int *ourintr; 1560 int *callhandler; 1561{ 1562 struct ndis_softc *sc; 1563 ndis_handle adapter; 1564 __stdcall ndis_isr_handler isrfunc; 1565 uint8_t accepted, queue; 1566 1567 if (arg == NULL || ourintr == NULL || callhandler == NULL) 1568 return(EINVAL); 1569 1570 sc = arg; 1571 adapter = sc->ndis_block->nmb_miniportadapterctx; 1572 isrfunc = sc->ndis_chars->nmc_isr_func; 1573 1574 if (adapter == NULL || isrfunc == NULL) 1575 return(ENXIO); 1576 1577#ifdef notdef 1578 if (NDIS_SERIALIZED(sc->ndis_block)) 1579 mtx_lock(&sc->ndis_block->nmb_serialmtx); 1580#endif 1581 MSCALL3(isrfunc, &accepted, &queue, adapter); 1582#ifdef notdef 1583 if (NDIS_SERIALIZED(sc->ndis_block)) 1584 mtx_unlock(&sc->ndis_block->nmb_serialmtx); 1585#endif 1586 1587 *ourintr = accepted; 1588 *callhandler = queue; 1589 1590 return(0); 1591} 1592 1593__stdcall static void 1594ndis_intrhand(dpc, dobj, ip, sc) 1595 kdpc *dpc; 1596 device_object *dobj; 1597 irp *ip; 1598 struct ndis_softc *sc; 1599{ 1600 ndis_handle adapter; 1601 __stdcall ndis_interrupt_handler intrfunc; 1602 uint8_t irql; 1603 1604 adapter = sc->ndis_block->nmb_miniportadapterctx; 1605 intrfunc = sc->ndis_chars->nmc_interrupt_func; 1606 1607 if (adapter == NULL || intrfunc == NULL) 1608 return; 1609 1610 if (NDIS_SERIALIZED(sc->ndis_block)) 1611 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1612 1613 MSCALL1(intrfunc, adapter); 1614 1615 /* If there's a MiniportEnableInterrupt() routine, call it. */ 1616 1617 ndis_enable_intr(sc); 1618 1619 if (NDIS_SERIALIZED(sc->ndis_block)) 1620 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1621 1622 return; 1623} 1624 1625int 1626ndis_get_info(arg, oid, buf, buflen) 1627 void *arg; 1628 ndis_oid oid; 1629 void *buf; 1630 int *buflen; 1631{ 1632 struct ndis_softc *sc; 1633 ndis_status rval; 1634 ndis_handle adapter; 1635 __stdcall ndis_queryinfo_handler queryfunc; 1636 uint32_t byteswritten = 0, bytesneeded = 0; 1637 int error; 1638 uint8_t irql; 1639 1640 sc = arg; 1641 KeAcquireSpinLock(&sc->ndis_block->nmb_lock, &irql); 1642 1643 if (sc->ndis_block->nmb_pendingreq != NULL) 1644 panic("ndis_get_info() called while other request pending"); 1645 else 1646 sc->ndis_block->nmb_pendingreq = (ndis_request *)sc; 1647 1648 queryfunc = sc->ndis_chars->nmc_queryinfo_func; 1649 adapter = sc->ndis_block->nmb_miniportadapterctx; 1650 1651 if (adapter == NULL || queryfunc == NULL) { 1652 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1653 return(ENXIO); 1654 } 1655 1656 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen, 1657 &byteswritten, &bytesneeded); 1658 1659 sc->ndis_block->nmb_pendingreq = NULL; 1660 1661 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql); 1662 1663 /* Wait for requests that block. */ 1664 1665 if (rval == NDIS_STATUS_PENDING) { 1666 mtx_lock(&ndis_req_mtx); 1667 error = msleep(&sc->ndis_block->nmb_getstat, 1668 &ndis_req_mtx, 1669 curthread->td_priority|PDROP, 1670 "ndisget", 5 * hz); 1671 rval = sc->ndis_block->nmb_getstat; 1672 } 1673 1674 if (byteswritten) 1675 *buflen = byteswritten; 1676 if (bytesneeded) 1677 *buflen = bytesneeded; 1678 1679 if (rval == NDIS_STATUS_INVALID_LENGTH || 1680 rval == NDIS_STATUS_BUFFER_TOO_SHORT) 1681 return(ENOSPC); 1682 1683 if (rval == NDIS_STATUS_INVALID_OID) 1684 return(EINVAL); 1685 1686 if (rval == NDIS_STATUS_NOT_SUPPORTED || 1687 rval == NDIS_STATUS_NOT_ACCEPTED) 1688 return(ENOTSUP); 1689 1690 if (rval != NDIS_STATUS_SUCCESS) 1691 return(ENODEV); 1692 1693 return(0); 1694} 1695 1696__stdcall uint32_t 1697NdisAddDevice(drv, pdo) 1698 driver_object *drv; 1699 device_object *pdo; 1700{ 1701 device_object *fdo; 1702 ndis_miniport_block *block; 1703 struct ndis_softc *sc; 1704 uint32_t status; 1705 1706 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL, 1707 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo); 1708 1709 if (status != STATUS_SUCCESS) 1710 return(status); 1711 1712 block = fdo->do_devext; 1713 block->nmb_deviceobj = fdo; 1714 block->nmb_physdeviceobj = pdo; 1715 block->nmb_nextdeviceobj = IoAttachDeviceToDeviceStack(fdo, pdo); 1716 KeInitializeSpinLock(&block->nmb_lock); 1717 1718 /* 1719 * Stash pointers to the miniport block and miniport 1720 * characteristics info in the if_ndis softc so the 1721 * UNIX wrapper driver can get to them later. 1722 */ 1723 sc = device_get_softc(pdo->do_devext); 1724 sc->ndis_block = block; 1725 sc->ndis_chars = IoGetDriverObjectExtension(drv, (void *)1); 1726 1727 IoInitializeDpcRequest(fdo, ndis_intrhand); 1728 1729 /* Finish up BSD-specific setup. */ 1730 1731 block->nmb_signature = (void *)0xcafebabe; 1732 block->nmb_status_func = kernndis_functbl[0].ipt_wrap; 1733 block->nmb_statusdone_func = kernndis_functbl[1].ipt_wrap; 1734 block->nmb_setdone_func = kernndis_functbl[2].ipt_wrap; 1735 block->nmb_querydone_func = kernndis_functbl[3].ipt_wrap; 1736 block->nmb_resetdone_func = kernndis_functbl[4].ipt_wrap; 1737 block->nmb_sendrsrc_func = kernndis_functbl[5].ipt_wrap; 1738 block->nmb_pendingreq = NULL; 1739 1740 ndis_enlarge_thrqueue(8); 1741 1742 TAILQ_INSERT_TAIL(&ndis_devhead, block, link); 1743 1744 return (STATUS_SUCCESS); 1745} 1746 1747int 1748ndis_unload_driver(arg) 1749 void *arg; 1750{ 1751 struct ndis_softc *sc; 1752 device_object *fdo; 1753 1754 sc = arg; 1755 1756 if (sc->ndis_block->nmb_rlist != NULL) 1757 free(sc->ndis_block->nmb_rlist, M_DEVBUF); 1758 1759 ndis_flush_sysctls(sc); 1760 1761 ndis_shrink_thrqueue(8); 1762 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link); 1763 1764 fdo = sc->ndis_block->nmb_deviceobj; 1765 IoDetachDevice(sc->ndis_block->nmb_nextdeviceobj); 1766 IoDeleteDevice(fdo); 1767 1768 return(0); 1769} 1770