kern_ndis.c revision 123848
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 123848 2003-12-26 03:31:34Z wpaul $"); 35 36#include <sys/param.h> 37#include <sys/types.h> 38#include <sys/errno.h> 39#include <sys/callout.h> 40#include <sys/socket.h> 41#include <sys/queue.h> 42#include <sys/sysctl.h> 43#include <sys/systm.h> 44#include <sys/malloc.h> 45#include <sys/lock.h> 46#include <sys/mutex.h> 47#include <sys/conf.h> 48 49#include <sys/kernel.h> 50#include <machine/bus.h> 51#include <machine/resource.h> 52#include <sys/bus.h> 53#include <sys/rman.h> 54 55#include <net/if.h> 56#include <net/if_arp.h> 57#include <net/ethernet.h> 58#include <net/if_dl.h> 59#include <net/if_media.h> 60 61#include <net80211/ieee80211_var.h> 62#include <net80211/ieee80211_ioctl.h> 63 64#include <dev/pccard/pccardvar.h> 65#include "card_if.h" 66 67#include <compat/ndis/pe_var.h> 68#include <compat/ndis/resource_var.h> 69#include <compat/ndis/ndis_var.h> 70#include <compat/ndis/hal_var.h> 71#include <compat/ndis/ntoskrnl_var.h> 72#include <compat/ndis/cfg_var.h> 73#include <dev/if_ndis/if_ndisvar.h> 74 75#define __stdcall __attribute__((__stdcall__)) 76#define NDIS_DUMMY_PATH "\\\\some\\bogus\\path" 77 78__stdcall static void ndis_status_func(ndis_handle, ndis_status, 79 void *, uint32_t); 80__stdcall static void ndis_statusdone_func(ndis_handle); 81__stdcall static void ndis_setdone_func(ndis_handle, ndis_status); 82__stdcall static void ndis_getdone_func(ndis_handle, ndis_status); 83__stdcall static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t); 84 85/* 86 * This allows us to export our symbols to other modules. 87 * Note that we call ourselves 'ndisapi' to avoid a namespace 88 * collision with if_ndis.ko, which internally calls itself 89 * 'ndis.' 90 */ 91static int 92ndis_modevent(module_t mod, int cmd, void *arg) 93{ 94 return(0); 95} 96DEV_MODULE(ndisapi, ndis_modevent, NULL); 97MODULE_VERSION(ndisapi, 1); 98 99 100__stdcall static void 101ndis_status_func(adapter, status, sbuf, slen) 102 ndis_handle adapter; 103 ndis_status status; 104 void *sbuf; 105 uint32_t slen; 106{ 107 printf ("status: %x\n", status); 108 return; 109} 110 111__stdcall static void 112ndis_statusdone_func(adapter) 113 ndis_handle adapter; 114{ 115 printf ("status complete\n"); 116 return; 117} 118 119__stdcall static void 120ndis_setdone_func(adapter, status) 121 ndis_handle adapter; 122 ndis_status status; 123{ 124 ndis_miniport_block *block; 125 block = adapter; 126 127 block->nmb_setstat = status; 128 wakeup(&block->nmb_wkupdpctimer); 129 return; 130} 131 132__stdcall static void 133ndis_getdone_func(adapter, status) 134 ndis_handle adapter; 135 ndis_status status; 136{ 137 ndis_miniport_block *block; 138 block = adapter; 139 140 block->nmb_getstat = status; 141 wakeup(&block->nmb_wkupdpctimer); 142 return; 143} 144 145__stdcall static void 146ndis_resetdone_func(adapter, status, addressingreset) 147 ndis_handle adapter; 148 ndis_status status; 149 uint8_t addressingreset; 150{ 151 printf ("reset done...\n"); 152 return; 153} 154 155#define NDIS_AM_RID 3 156 157int 158ndis_alloc_amem(arg) 159 void *arg; 160{ 161 struct ndis_softc *sc; 162 int error, rid; 163 164 if (arg == NULL) 165 return(EINVAL); 166 167 sc = arg; 168 rid = NDIS_AM_RID; 169 sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY, 170 &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE); 171 172 if (sc->ndis_res_am == NULL) { 173 printf("ndis%d: failed to allocate attribute memory\n", 174 sc->ndis_unit); 175 return(ENXIO); 176 } 177 178 error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev), 179 sc->ndis_dev, rid, 0, NULL); 180 181 if (error) { 182 printf("ndis%d: CARD_SET_MEMORY_OFFSET() returned 0x%x\n", 183 sc->ndis_unit, error); 184 return(error); 185 } 186 187 error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev), 188 sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR); 189 190 if (error) { 191 printf("ndis%d: CARD_SET_RES_FLAGS() returned 0x%x\n", 192 sc->ndis_unit, error); 193 return(error); 194 } 195 196 return(0); 197} 198 199int 200ndis_create_sysctls(arg) 201 void *arg; 202{ 203 struct ndis_softc *sc; 204 ndis_cfg *vals; 205 char buf[256]; 206 207 if (arg == NULL) 208 return(EINVAL); 209 210 sc = arg; 211 vals = sc->ndis_regvals; 212 213 TAILQ_INIT(&sc->ndis_cfglist_head); 214 215 /* Create the sysctl tree. */ 216 217 sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx, 218 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 219 device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0, 220 device_get_desc(sc->ndis_dev)); 221 222 /* Add the driver-specific registry keys. */ 223 224 vals = sc->ndis_regvals; 225 while(1) { 226 if (vals->nc_cfgkey == NULL) 227 break; 228 if (vals->nc_idx != sc->ndis_devidx) { 229 vals++; 230 continue; 231 } 232 SYSCTL_ADD_STRING(&sc->ndis_ctx, 233 SYSCTL_CHILDREN(sc->ndis_tree), 234 OID_AUTO, vals->nc_cfgkey, 235 CTLFLAG_RW, vals->nc_val, 236 sizeof(vals->nc_val), 237 vals->nc_cfgdesc); 238 vals++; 239 } 240 241 /* Now add a couple of builtin keys. */ 242 243 /* 244 * Environment can be either Windows (0) or WindowsNT (1). 245 * We qualify as the latter. 246 */ 247 ndis_add_sysctl(sc, "Environment", 248 "Windows environment", "1", CTLFLAG_RD); 249 250 /* NDIS version should be 5.1. */ 251 ndis_add_sysctl(sc, "NdisVersion", 252 "NDIS API Version", "0x00050001", CTLFLAG_RD); 253 254 /* Bus type (PCI, PCMCIA, etc...) */ 255 sprintf(buf, "%d\n", (int)sc->ndis_iftype); 256 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD); 257 258 if (sc->ndis_res_io != NULL) { 259 sprintf(buf, "0x%lx\n", rman_get_start(sc->ndis_res_io)); 260 ndis_add_sysctl(sc, "IOBaseAddress", 261 "Base I/O Address", buf, CTLFLAG_RD); 262 } 263 264 if (sc->ndis_irq != NULL) { 265 sprintf(buf, "%lu\n", rman_get_start(sc->ndis_irq)); 266 ndis_add_sysctl(sc, "InterruptNumber", 267 "Interrupt Number", buf, CTLFLAG_RD); 268 } 269 270 return(0); 271} 272 273int 274ndis_add_sysctl(arg, key, desc, val, flag) 275 void *arg; 276 char *key; 277 char *desc; 278 char *val; 279 int flag; 280{ 281 struct ndis_softc *sc; 282 struct ndis_cfglist *cfg; 283 char descstr[256]; 284 285 sc = arg; 286 287 cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO); 288 289 if (cfg == NULL) 290 return(ENOMEM); 291 292 cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF); 293 if (desc == NULL) { 294 snprintf(descstr, sizeof(descstr), "%s (dynamic)", key); 295 cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF); 296 } else 297 cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF); 298 strcpy(cfg->ndis_cfg.nc_val, val); 299 300 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link); 301 302 SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree), 303 OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag, 304 cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val), 305 cfg->ndis_cfg.nc_cfgdesc); 306 307 return(0); 308} 309 310int 311ndis_flush_sysctls(arg) 312 void *arg; 313{ 314 struct ndis_softc *sc; 315 struct ndis_cfglist *cfg; 316 317 sc = arg; 318 319 while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) { 320 cfg = TAILQ_FIRST(&sc->ndis_cfglist_head); 321 TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link); 322 free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF); 323 free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF); 324 free(cfg, M_DEVBUF); 325 } 326 327 return(0); 328} 329 330void 331ndis_return_packet(buf, arg) 332 void *buf; /* not used */ 333 void *arg; 334{ 335 struct ndis_softc *sc; 336 ndis_handle adapter; 337 ndis_packet *p; 338 __stdcall ndis_return_handler returnfunc; 339 340 if (arg == NULL) 341 return; 342 343 p = arg; 344 345 /* Decrement refcount. */ 346 p->np_refcnt--; 347 348 /* Release packet when refcount hits zero, otherwise return. */ 349 if (p->np_refcnt) 350 return; 351 352 sc = p->np_softc; 353 returnfunc = sc->ndis_chars.nmc_return_packet_func; 354 adapter = sc->ndis_block.nmb_miniportadapterctx; 355 if (returnfunc == NULL) 356 ndis_free_packet(p); 357 else 358 returnfunc(adapter, p); 359 return; 360} 361 362void 363ndis_free_bufs(b0) 364 ndis_buffer *b0; 365{ 366 ndis_buffer *next; 367 368 if (b0 == NULL) 369 return; 370 371 while(b0 != NULL) { 372 next = b0->nb_next; 373 free (b0, M_DEVBUF); 374 b0 = next; 375 } 376 377 return; 378} 379 380void 381ndis_free_packet(p) 382 ndis_packet *p; 383{ 384 if (p == NULL) 385 return; 386 387 ndis_free_bufs(p->np_private.npp_head); 388 free(p, M_DEVBUF); 389 390 return; 391} 392 393int 394ndis_convert_res(arg) 395 void *arg; 396{ 397 struct ndis_softc *sc; 398 ndis_resource_list *rl = NULL; 399 cm_partial_resource_desc *prd = NULL; 400 ndis_miniport_block *block; 401 402 sc = arg; 403 block = &sc->ndis_block; 404 405 rl = malloc(sizeof(ndis_resource_list) + 406 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)), 407 M_DEVBUF, M_NOWAIT|M_ZERO); 408 409 if (rl == NULL) 410 return(ENOMEM); 411 412 rl->cprl_version = 5; 413 rl->cprl_version = 1; 414 rl->cprl_count = sc->ndis_rescnt; 415 416 prd = rl->cprl_partial_descs; 417 if (sc->ndis_res_io) { 418 prd->cprd_type = CmResourceTypePort; 419 prd->u.cprd_port.cprd_start.np_quad = 420 rman_get_start(sc->ndis_res_io); 421 prd->u.cprd_port.cprd_len = 422 rman_get_size(sc->ndis_res_io); 423 prd++; 424 } 425 426 if (sc->ndis_res_mem) { 427 prd->cprd_type = CmResourceTypeMemory; 428 prd->u.cprd_mem.cprd_start.np_quad = 429 rman_get_start(sc->ndis_res_mem); 430 prd->u.cprd_mem.cprd_len = 431 rman_get_size(sc->ndis_res_mem); 432 prd++; 433 } 434 435 if (sc->ndis_irq) { 436 prd->cprd_type = CmResourceTypeInterrupt; 437 prd->u.cprd_intr.cprd_level = 438 rman_get_start(sc->ndis_irq); 439 prd->u.cprd_intr.cprd_vector = 440 rman_get_start(sc->ndis_irq); 441 prd->u.cprd_intr.cprd_affinity = 0; 442 } 443 444 block->nmb_rlist = rl; 445 446 return(0); 447} 448 449/* 450 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a 451 * packet, it will hand it to us in the form of an ndis_packet, 452 * which we need to convert to an mbuf that is then handed off 453 * to the stack. Note: we configure the mbuf list so that it uses 454 * the memory regions specified by the ndis_buffer structures in 455 * the ndis_packet as external storage. In most cases, this will 456 * point to a memory region allocated by the driver (either by 457 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect 458 * the driver to handle free()ing this region for is, so we set up 459 * a dummy no-op free handler for it. 460 */ 461 462int 463ndis_ptom(m0, p) 464 struct mbuf **m0; 465 ndis_packet *p; 466{ 467 struct mbuf *m, *prev = NULL; 468 ndis_buffer *buf; 469 ndis_packet_private *priv; 470 uint32_t totlen = 0; 471 472 if (p == NULL || m0 == NULL) 473 return(EINVAL); 474 475 priv = &p->np_private; 476 buf = priv->npp_head; 477 p->np_refcnt = 0; 478 479 for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) { 480 if (buf == priv->npp_head) 481 MGETHDR(m, M_DONTWAIT, MT_HEADER); 482 else 483 MGET(m, M_DONTWAIT, MT_DATA); 484 if (m == NULL) { 485 m_freem(*m0); 486 *m0 = NULL; 487 return(ENOBUFS); 488 } 489 m->m_len = buf->nb_bytecount; 490 m->m_data = MDL_VA(buf); 491 MEXTADD(m, m->m_data, m->m_len, ndis_return_packet, 492 p, 0, EXT_NDIS); 493 p->np_refcnt++; 494 totlen += m->m_len; 495 if (m->m_flags & MT_HEADER) 496 *m0 = m; 497 else 498 prev->m_next = m; 499 prev = m; 500 } 501 502 (*m0)->m_pkthdr.len = totlen; 503 504 return(0); 505} 506 507/* 508 * Create an mbuf chain from an NDIS packet chain. 509 * This is used mainly when transmitting packets, where we need 510 * to turn an mbuf off an interface's send queue and transform it 511 * into an NDIS packet which will be fed into the NDIS driver's 512 * send routine. 513 * 514 * NDIS packets consist of two parts: an ndis_packet structure, 515 * which is vaguely analagous to the pkthdr portion of an mbuf, 516 * and one or more ndis_buffer structures, which define the 517 * actual memory segments in which the packet data resides. 518 * We need to allocate one ndis_buffer for each mbuf in a chain, 519 * plus one ndis_packet as the header. 520 */ 521 522int 523ndis_mtop(m0, p) 524 struct mbuf *m0; 525 ndis_packet **p; 526{ 527 struct mbuf *m; 528 ndis_buffer *buf = NULL, *prev = NULL; 529 ndis_packet_private *priv; 530 531 if (p == NULL || m0 == NULL) 532 return(EINVAL); 533 534 /* If caller didn't supply a packet, make one. */ 535 if (*p == NULL) { 536 *p = malloc(sizeof(ndis_packet), M_DEVBUF, M_NOWAIT|M_ZERO); 537 538 if (*p == NULL) 539 return(ENOMEM); 540 } 541 542 priv = &(*p)->np_private; 543 priv->npp_totlen = m0->m_pkthdr.len; 544 priv->npp_packetooboffset = offsetof(ndis_packet, np_oob); 545 546 for (m = m0; m != NULL; m = m->m_next) { 547 if (m->m_len == 0) 548 continue; 549 buf = malloc(sizeof(ndis_buffer), M_DEVBUF, M_NOWAIT|M_ZERO); 550 if (buf == NULL) { 551 ndis_free_packet(*p); 552 *p = NULL; 553 return(ENOMEM); 554 } 555 556 MDL_INIT(buf, m->m_data, m->m_len); 557 if (priv->npp_head == NULL) 558 priv->npp_head = buf; 559 else 560 prev->nb_next = buf; 561 prev = buf; 562 } 563 564 priv->npp_tail = buf; 565 566 return(0); 567} 568 569int 570ndis_get_supported_oids(arg, oids, oidcnt) 571 void *arg; 572 ndis_oid **oids; 573 int *oidcnt; 574{ 575 int len, rval; 576 ndis_oid *o; 577 578 if (arg == NULL || oids == NULL || oidcnt == NULL) 579 return(EINVAL); 580 len = 0; 581 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len); 582 583 o = malloc(len, M_DEVBUF, M_NOWAIT); 584 if (o == NULL) 585 return(ENOMEM); 586 587 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len); 588 589 if (rval) { 590 free(o, M_DEVBUF); 591 return(rval); 592 } 593 594 *oids = o; 595 *oidcnt = len / 4; 596 597 return(0); 598} 599 600int 601ndis_set_info(arg, oid, buf, buflen) 602 void *arg; 603 ndis_oid oid; 604 void *buf; 605 int *buflen; 606{ 607 struct ndis_softc *sc; 608 ndis_status rval; 609 ndis_handle adapter; 610 __stdcall ndis_setinfo_handler setfunc; 611 uint32_t byteswritten = 0, bytesneeded = 0; 612 struct timeval tv; 613 int error; 614 615 sc = arg; 616 setfunc = sc->ndis_chars.nmc_setinfo_func; 617 adapter = sc->ndis_block.nmb_miniportadapterctx; 618 619 rval = setfunc(adapter, oid, buf, *buflen, 620 &byteswritten, &bytesneeded); 621 622 if (rval == NDIS_STATUS_PENDING) { 623 tv.tv_sec = 60; 624 tv.tv_usec = 0; 625 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer, 626 PPAUSE|PCATCH, "ndisset", tvtohz(&tv)); 627 rval = sc->ndis_block.nmb_setstat; 628 } 629 630 if (byteswritten) 631 *buflen = byteswritten; 632 if (bytesneeded) 633 *buflen = bytesneeded; 634 635 if (rval == NDIS_STATUS_INVALID_LENGTH) 636 return(ENOSPC); 637 638 if (rval == NDIS_STATUS_INVALID_OID) 639 return(EINVAL); 640 641 if (rval == NDIS_STATUS_NOT_SUPPORTED || 642 rval == NDIS_STATUS_NOT_ACCEPTED) 643 return(ENOTSUP); 644 645 return(0); 646} 647 648int 649ndis_send_packets(arg, packets, cnt) 650 void *arg; 651 ndis_packet **packets; 652 int cnt; 653{ 654 struct ndis_softc *sc; 655 ndis_handle adapter; 656 __stdcall ndis_sendmulti_handler sendfunc; 657 658 sc = arg; 659 adapter = sc->ndis_block.nmb_miniportadapterctx; 660 sendfunc = sc->ndis_chars.nmc_sendmulti_func; 661 sendfunc(adapter, packets, cnt); 662 663 return(0); 664} 665 666int 667ndis_init_dma(arg) 668 void *arg; 669{ 670 struct ndis_softc *sc; 671 int i, error; 672 673 sc = arg; 674 675 sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts, 676 M_DEVBUF, M_NOWAIT|M_ZERO); 677 678 if (sc->ndis_tmaps == NULL) 679 return(ENOMEM); 680 681 for (i = 0; i < sc->ndis_maxpkts; i++) { 682 error = bus_dmamap_create(sc->ndis_ttag, 0, 683 &sc->ndis_tmaps[i]); 684 if (error) { 685 free(sc->ndis_tmaps, M_DEVBUF); 686 return(ENODEV); 687 } 688 } 689 690 return(0); 691} 692 693int 694ndis_destroy_dma(arg) 695 void *arg; 696{ 697 struct ndis_softc *sc; 698 struct mbuf *m; 699 ndis_packet *p = NULL; 700 int i; 701 702 sc = arg; 703 704 for (i = 0; i < sc->ndis_maxpkts; i++) { 705 if (sc->ndis_txarray[i] != NULL) { 706 p = sc->ndis_txarray[i]; 707 m = (struct mbuf *)p->np_rsvd[1]; 708 if (m != NULL) 709 m_freem(m); 710 ndis_free_packet(sc->ndis_txarray[i]); 711 } 712 bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]); 713 } 714 715 free(sc->ndis_tmaps, M_DEVBUF); 716 717 bus_dma_tag_destroy(sc->ndis_ttag); 718 719 return(0); 720} 721 722int 723ndis_reset_nic(arg) 724 void *arg; 725{ 726 struct ndis_softc *sc; 727 ndis_handle adapter; 728 __stdcall ndis_reset_handler resetfunc; 729 uint8_t addressing_reset; 730 struct ifnet *ifp; 731 732 sc = arg; 733 ifp = &sc->arpcom.ac_if; 734 adapter = sc->ndis_block.nmb_miniportadapterctx; 735 if (adapter == NULL) 736 return(EIO); 737 resetfunc = sc->ndis_chars.nmc_reset_func; 738 739 if (resetfunc == NULL) 740 return(EINVAL); 741 742 resetfunc(&addressing_reset, adapter); 743 744 return(0); 745} 746 747int 748ndis_halt_nic(arg) 749 void *arg; 750{ 751 struct ndis_softc *sc; 752 ndis_handle adapter; 753 __stdcall ndis_halt_handler haltfunc; 754 struct ifnet *ifp; 755 struct ndis_timer_entry *ne; 756 757 sc = arg; 758 ifp = &sc->arpcom.ac_if; 759 adapter = sc->ndis_block.nmb_miniportadapterctx; 760 if (adapter == NULL) 761 return(EIO); 762 763 haltfunc = sc->ndis_chars.nmc_halt_func; 764 765 if (haltfunc == NULL) 766 return(EINVAL); 767 768 haltfunc(adapter); 769 770 /* 771 * The adapter context is only valid after the init 772 * handler has been called, and is invalid once the 773 * halt handler has been called. 774 */ 775 776 sc->ndis_block.nmb_miniportadapterctx = NULL; 777 778 /* Clobber all the timers in case the driver left one running. */ 779 780 while (!TAILQ_EMPTY(&sc->ndis_block.nmb_timerlist)) { 781 ne = TAILQ_FIRST(&sc->ndis_block.nmb_timerlist); 782 TAILQ_REMOVE(&sc->ndis_block.nmb_timerlist, ne, link); 783 callout_stop(&ne->nte_ch); 784 free(ne, M_DEVBUF); 785 } 786 787 return(0); 788} 789 790int 791ndis_shutdown_nic(arg) 792 void *arg; 793{ 794 struct ndis_softc *sc; 795 ndis_handle adapter; 796 __stdcall ndis_shutdown_handler shutdownfunc; 797 798 799 sc = arg; 800 adapter = sc->ndis_block.nmb_miniportadapterctx; 801 if (adapter == NULL) 802 return(EIO); 803 shutdownfunc = sc->ndis_chars.nmc_shutdown_handler; 804 805 if (shutdownfunc == NULL) 806 return(EINVAL); 807 808 if (sc->ndis_chars.nmc_rsvd0 == NULL) 809 shutdownfunc(adapter); 810 else 811 shutdownfunc(sc->ndis_chars.nmc_rsvd0); 812 813 return(0); 814} 815 816int 817ndis_init_nic(arg) 818 void *arg; 819{ 820 struct ndis_softc *sc; 821 ndis_miniport_block *block; 822 __stdcall ndis_init_handler initfunc; 823 ndis_status status, openstatus = 0; 824 ndis_medium mediumarray[NdisMediumMax]; 825 uint32_t chosenmedium, i; 826 827 if (arg == NULL) 828 return(EINVAL); 829 830 sc = arg; 831 block = &sc->ndis_block; 832 initfunc = sc->ndis_chars.nmc_init_func; 833 834 TAILQ_INIT(&block->nmb_timerlist); 835 836 for (i = 0; i < NdisMediumMax; i++) 837 mediumarray[i] = i; 838 839 status = initfunc(&openstatus, &chosenmedium, 840 mediumarray, NdisMediumMax, block, block); 841 842 /* 843 * If the init fails, blow away the other exported routines 844 * we obtained from the driver so we can't call them later. 845 * If the init failed, none of these will work. 846 */ 847 if (status != NDIS_STATUS_SUCCESS) { 848 bzero((char *)&sc->ndis_chars, 849 sizeof(ndis_miniport_characteristics)); 850 return(ENXIO); 851 } 852 853 return(0); 854} 855 856void 857ndis_enable_intr(arg) 858 void *arg; 859{ 860 struct ndis_softc *sc; 861 ndis_handle adapter; 862 __stdcall ndis_enable_interrupts_handler intrenbfunc; 863 864 sc = arg; 865 adapter = sc->ndis_block.nmb_miniportadapterctx; 866 if (adapter == NULL) 867 return; 868 intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func; 869 if (intrenbfunc == NULL) 870 return; 871 intrenbfunc(adapter); 872 873 return; 874} 875 876void 877ndis_disable_intr(arg) 878 void *arg; 879{ 880 struct ndis_softc *sc; 881 ndis_handle adapter; 882 __stdcall ndis_disable_interrupts_handler intrdisfunc; 883 884 sc = arg; 885 adapter = sc->ndis_block.nmb_miniportadapterctx; 886 if (adapter == NULL) 887 return; 888 intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func; 889 if (intrdisfunc == NULL) 890 return; 891 intrdisfunc(adapter); 892 893 return; 894} 895 896int 897ndis_isr(arg, ourintr, callhandler) 898 void *arg; 899 int *ourintr; 900 int *callhandler; 901{ 902 struct ndis_softc *sc; 903 ndis_handle adapter; 904 __stdcall ndis_isr_handler isrfunc; 905 uint8_t accepted, queue; 906 907 if (arg == NULL || ourintr == NULL || callhandler == NULL) 908 return(EINVAL); 909 910 sc = arg; 911 adapter = sc->ndis_block.nmb_miniportadapterctx; 912 isrfunc = sc->ndis_chars.nmc_isr_func; 913 isrfunc(&accepted, &queue, adapter); 914 *ourintr = accepted; 915 *callhandler = queue; 916 917 return(0); 918} 919 920int 921ndis_intrhand(arg) 922 void *arg; 923{ 924 struct ndis_softc *sc; 925 ndis_handle adapter; 926 __stdcall ndis_interrupt_handler intrfunc; 927 928 if (arg == NULL) 929 return(EINVAL); 930 931 sc = arg; 932 adapter = sc->ndis_block.nmb_miniportadapterctx; 933 intrfunc = sc->ndis_chars.nmc_interrupt_func; 934 intrfunc(adapter); 935 936 return(0); 937} 938 939int 940ndis_get_info(arg, oid, buf, buflen) 941 void *arg; 942 ndis_oid oid; 943 void *buf; 944 int *buflen; 945{ 946 struct ndis_softc *sc; 947 ndis_status rval; 948 ndis_handle adapter; 949 __stdcall ndis_queryinfo_handler queryfunc; 950 uint32_t byteswritten = 0, bytesneeded = 0; 951 struct timeval tv; 952 int error; 953 954 sc = arg; 955 queryfunc = sc->ndis_chars.nmc_queryinfo_func; 956 adapter = sc->ndis_block.nmb_miniportadapterctx; 957 958 rval = queryfunc(adapter, oid, buf, *buflen, 959 &byteswritten, &bytesneeded); 960 961 /* Wait for requests that block. */ 962 963 if (rval == NDIS_STATUS_PENDING) { 964 tv.tv_sec = 60; 965 tv.tv_usec = 0; 966 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer, 967 PPAUSE|PCATCH, "ndisget", tvtohz(&tv)); 968 rval = sc->ndis_block.nmb_getstat; 969 } 970 971 if (byteswritten) 972 *buflen = byteswritten; 973 if (bytesneeded) 974 *buflen = bytesneeded; 975 976 if (rval == NDIS_STATUS_INVALID_LENGTH || 977 rval == NDIS_STATUS_BUFFER_TOO_SHORT) 978 return(ENOSPC); 979 980 if (rval == NDIS_STATUS_INVALID_OID) 981 return(EINVAL); 982 983 if (rval == NDIS_STATUS_NOT_SUPPORTED || 984 rval == NDIS_STATUS_NOT_ACCEPTED) 985 return(ENOTSUP); 986 987 return(0); 988} 989 990int 991ndis_unload_driver(arg) 992 void *arg; 993{ 994 struct ndis_softc *sc; 995 996 sc = arg; 997 998 free(sc->ndis_block.nmb_rlist, M_DEVBUF); 999 1000 ndis_flush_sysctls(sc); 1001 ndis_libfini(); 1002 ntoskrnl_libfini(); 1003 1004 return(0); 1005} 1006 1007int 1008ndis_load_driver(img, arg) 1009 vm_offset_t img; 1010 void *arg; 1011{ 1012 __stdcall driver_entry entry; 1013 image_optional_header opt_hdr; 1014 image_import_descriptor imp_desc; 1015 ndis_unicode_string dummystr; 1016 ndis_driver_object drv; 1017 ndis_miniport_block *block; 1018 ndis_status status; 1019 int idx; 1020 uint32_t *ptr; 1021 struct ndis_softc *sc; 1022 1023 sc = arg; 1024 1025 /* Perform text relocation */ 1026 if (pe_relocate(img)) 1027 return(ENOEXEC); 1028 1029 /* Dynamically link the NDIS.SYS routines -- required. */ 1030 if (pe_patch_imports(img, "NDIS", ndis_functbl)) 1031 return(ENOEXEC); 1032 1033 /* Dynamically link the HAL.dll routines -- also required. */ 1034 if (pe_patch_imports(img, "HAL", hal_functbl)) 1035 return(ENOEXEC); 1036 1037 /* Dynamically link ntoskrnl.exe -- optional. */ 1038 if (pe_get_import_descriptor(img, &imp_desc, "ntoskrnl") == 0) { 1039 if (pe_patch_imports(img, "ntoskrnl", ntoskrnl_functbl)) 1040 return(ENOEXEC); 1041 } 1042 1043 /* Initialize subsystems */ 1044 ndis_libinit(); 1045 ntoskrnl_libinit(); 1046 1047 /* Locate the driver entry point */ 1048 pe_get_optional_header(img, &opt_hdr); 1049 entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr); 1050 1051 /* 1052 * Now call the DriverEntry() routine. This will cause 1053 * a callout to the NdisInitializeWrapper() and 1054 * NdisMRegisterMiniport() routines. 1055 */ 1056 dummystr.nus_len = strlen(NDIS_DUMMY_PATH); 1057 dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH); 1058 dummystr.nus_buf = NULL; 1059 ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf); 1060 drv.ndo_ifname = "ndis0"; 1061 1062 status = entry(&drv, &dummystr); 1063 1064 free (dummystr.nus_buf, M_DEVBUF); 1065 1066 if (status != NDIS_STATUS_SUCCESS) 1067 return(ENODEV); 1068 1069 /* 1070 * Now that we have the miniport driver characteristics, 1071 * create an NDIS block and call the init handler. 1072 * This will cause the driver to try to probe for 1073 * a device. 1074 */ 1075 1076 block = &sc->ndis_block; 1077 bcopy((char *)&drv.ndo_chars, (char *)&sc->ndis_chars, 1078 sizeof(ndis_miniport_characteristics)); 1079 1080 /*block->nmb_signature = 0xcafebabe;*/ 1081 1082 ptr = (uint32_t *)block; 1083 for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) { 1084 *ptr = idx | 0xdead0000; 1085 ptr++; 1086 } 1087 1088 block->nmb_signature = (void *)0xcafebabe; 1089 block->nmb_setdone_func = ndis_setdone_func; 1090 block->nmb_querydone_func = ndis_getdone_func; 1091 block->nmb_status_func = ndis_status_func; 1092 block->nmb_statusdone_func = ndis_statusdone_func; 1093 block->nmb_resetdone_func = ndis_resetdone_func; 1094 1095 block->nmb_ifp = &sc->arpcom.ac_if; 1096 block->nmb_dev = sc->ndis_dev; 1097 1098 return(0); 1099} 1100