kern_ndis.c revision 123826
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 123826 2003-12-25 06:04:40Z 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 struct callout_handle *ch; 757 758 sc = arg; 759 ifp = &sc->arpcom.ac_if; 760 adapter = sc->ndis_block.nmb_miniportadapterctx; 761 if (adapter == NULL) 762 return(EIO); 763 764 haltfunc = sc->ndis_chars.nmc_halt_func; 765 766 if (haltfunc == NULL) 767 return(EINVAL); 768 769 haltfunc(adapter); 770 771 /* 772 * The adapter context is only valid after the init 773 * handler has been called, and is invalid once the 774 * halt handler has been called. 775 */ 776 777 sc->ndis_block.nmb_miniportadapterctx = NULL; 778 779 /* Clobber all the timers in case the driver left one running. */ 780 781 while (!TAILQ_EMPTY(&sc->ndis_block.nmb_timerlist)) { 782 ne = TAILQ_FIRST(&sc->ndis_block.nmb_timerlist); 783 TAILQ_REMOVE(&sc->ndis_block.nmb_timerlist, ne, link); 784 ch = &ne->nte_ch; 785 if (ch->callout != NULL) 786 untimeout(ch->callout->c_func, ch->callout->c_arg, *ch); 787 free(ne, M_DEVBUF); 788 } 789 790 return(0); 791} 792 793int 794ndis_shutdown_nic(arg) 795 void *arg; 796{ 797 struct ndis_softc *sc; 798 ndis_handle adapter; 799 __stdcall ndis_shutdown_handler shutdownfunc; 800 801 802 sc = arg; 803 adapter = sc->ndis_block.nmb_miniportadapterctx; 804 if (adapter == NULL) 805 return(EIO); 806 shutdownfunc = sc->ndis_chars.nmc_shutdown_handler; 807 808 if (shutdownfunc == NULL) 809 return(EINVAL); 810 811 if (sc->ndis_chars.nmc_rsvd0 == NULL) 812 shutdownfunc(adapter); 813 else 814 shutdownfunc(sc->ndis_chars.nmc_rsvd0); 815 816 return(0); 817} 818 819int 820ndis_init_nic(arg) 821 void *arg; 822{ 823 struct ndis_softc *sc; 824 ndis_miniport_block *block; 825 __stdcall ndis_init_handler initfunc; 826 ndis_status status, openstatus = 0; 827 ndis_medium mediumarray[NdisMediumMax]; 828 uint32_t chosenmedium, i; 829 830 if (arg == NULL) 831 return(EINVAL); 832 833 sc = arg; 834 block = &sc->ndis_block; 835 initfunc = sc->ndis_chars.nmc_init_func; 836 837 TAILQ_INIT(&block->nmb_timerlist); 838 839 for (i = 0; i < NdisMediumMax; i++) 840 mediumarray[i] = i; 841 842 status = initfunc(&openstatus, &chosenmedium, 843 mediumarray, NdisMediumMax, block, block); 844 845 /* 846 * If the init fails, blow away the other exported routines 847 * we obtained from the driver so we can't call them later. 848 * If the init failed, none of these will work. 849 */ 850 if (status != NDIS_STATUS_SUCCESS) { 851 bzero((char *)&sc->ndis_chars, 852 sizeof(ndis_miniport_characteristics)); 853 return(ENXIO); 854 } 855 856 return(0); 857} 858 859void 860ndis_enable_intr(arg) 861 void *arg; 862{ 863 struct ndis_softc *sc; 864 ndis_handle adapter; 865 __stdcall ndis_enable_interrupts_handler intrenbfunc; 866 867 sc = arg; 868 adapter = sc->ndis_block.nmb_miniportadapterctx; 869 if (adapter == NULL) 870 return; 871 intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func; 872 if (intrenbfunc == NULL) 873 return; 874 intrenbfunc(adapter); 875 876 return; 877} 878 879void 880ndis_disable_intr(arg) 881 void *arg; 882{ 883 struct ndis_softc *sc; 884 ndis_handle adapter; 885 __stdcall ndis_disable_interrupts_handler intrdisfunc; 886 887 sc = arg; 888 adapter = sc->ndis_block.nmb_miniportadapterctx; 889 if (adapter == NULL) 890 return; 891 intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func; 892 if (intrdisfunc == NULL) 893 return; 894 intrdisfunc(adapter); 895 896 return; 897} 898 899int 900ndis_isr(arg, ourintr, callhandler) 901 void *arg; 902 int *ourintr; 903 int *callhandler; 904{ 905 struct ndis_softc *sc; 906 ndis_handle adapter; 907 __stdcall ndis_isr_handler isrfunc; 908 uint8_t accepted, queue; 909 910 if (arg == NULL || ourintr == NULL || callhandler == NULL) 911 return(EINVAL); 912 913 sc = arg; 914 adapter = sc->ndis_block.nmb_miniportadapterctx; 915 isrfunc = sc->ndis_chars.nmc_isr_func; 916 isrfunc(&accepted, &queue, adapter); 917 *ourintr = accepted; 918 *callhandler = queue; 919 920 return(0); 921} 922 923int 924ndis_intrhand(arg) 925 void *arg; 926{ 927 struct ndis_softc *sc; 928 ndis_handle adapter; 929 __stdcall ndis_interrupt_handler intrfunc; 930 931 if (arg == NULL) 932 return(EINVAL); 933 934 sc = arg; 935 adapter = sc->ndis_block.nmb_miniportadapterctx; 936 intrfunc = sc->ndis_chars.nmc_interrupt_func; 937 intrfunc(adapter); 938 939 return(0); 940} 941 942int 943ndis_get_info(arg, oid, buf, buflen) 944 void *arg; 945 ndis_oid oid; 946 void *buf; 947 int *buflen; 948{ 949 struct ndis_softc *sc; 950 ndis_status rval; 951 ndis_handle adapter; 952 __stdcall ndis_queryinfo_handler queryfunc; 953 uint32_t byteswritten = 0, bytesneeded = 0; 954 struct timeval tv; 955 int error; 956 957 sc = arg; 958 queryfunc = sc->ndis_chars.nmc_queryinfo_func; 959 adapter = sc->ndis_block.nmb_miniportadapterctx; 960 961 rval = queryfunc(adapter, oid, buf, *buflen, 962 &byteswritten, &bytesneeded); 963 964 /* Wait for requests that block. */ 965 966 if (rval == NDIS_STATUS_PENDING) { 967 tv.tv_sec = 60; 968 tv.tv_usec = 0; 969 error = tsleep(&sc->ndis_block.nmb_wkupdpctimer, 970 PPAUSE|PCATCH, "ndisget", tvtohz(&tv)); 971 rval = sc->ndis_block.nmb_getstat; 972 } 973 974 if (byteswritten) 975 *buflen = byteswritten; 976 if (bytesneeded) 977 *buflen = bytesneeded; 978 979 if (rval == NDIS_STATUS_INVALID_LENGTH || 980 rval == NDIS_STATUS_BUFFER_TOO_SHORT) 981 return(ENOSPC); 982 983 if (rval == NDIS_STATUS_INVALID_OID) 984 return(EINVAL); 985 986 if (rval == NDIS_STATUS_NOT_SUPPORTED || 987 rval == NDIS_STATUS_NOT_ACCEPTED) 988 return(ENOTSUP); 989 990 return(0); 991} 992 993int 994ndis_unload_driver(arg) 995 void *arg; 996{ 997 struct ndis_softc *sc; 998 999 sc = arg; 1000 1001 free(sc->ndis_block.nmb_rlist, M_DEVBUF); 1002 1003 ndis_flush_sysctls(sc); 1004 ndis_libfini(); 1005 ntoskrnl_libfini(); 1006 1007 return(0); 1008} 1009 1010int 1011ndis_load_driver(img, arg) 1012 vm_offset_t img; 1013 void *arg; 1014{ 1015 __stdcall driver_entry entry; 1016 image_optional_header opt_hdr; 1017 image_import_descriptor imp_desc; 1018 ndis_unicode_string dummystr; 1019 ndis_driver_object drv; 1020 ndis_miniport_block *block; 1021 ndis_status status; 1022 int idx; 1023 uint32_t *ptr; 1024 struct ndis_softc *sc; 1025 1026 sc = arg; 1027 1028 /* Perform text relocation */ 1029 if (pe_relocate(img)) 1030 return(ENOEXEC); 1031 1032 /* Dynamically link the NDIS.SYS routines -- required. */ 1033 if (pe_patch_imports(img, "NDIS", ndis_functbl)) 1034 return(ENOEXEC); 1035 1036 /* Dynamically link the HAL.dll routines -- also required. */ 1037 if (pe_patch_imports(img, "HAL", hal_functbl)) 1038 return(ENOEXEC); 1039 1040 /* Dynamically link ntoskrnl.exe -- optional. */ 1041 if (pe_get_import_descriptor(img, &imp_desc, "ntoskrnl") == 0) { 1042 if (pe_patch_imports(img, "ntoskrnl", ntoskrnl_functbl)) 1043 return(ENOEXEC); 1044 } 1045 1046 /* Initialize subsystems */ 1047 ndis_libinit(); 1048 ntoskrnl_libinit(); 1049 1050 /* Locate the driver entry point */ 1051 pe_get_optional_header(img, &opt_hdr); 1052 entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr); 1053 1054 /* 1055 * Now call the DriverEntry() routine. This will cause 1056 * a callout to the NdisInitializeWrapper() and 1057 * NdisMRegisterMiniport() routines. 1058 */ 1059 dummystr.nus_len = strlen(NDIS_DUMMY_PATH); 1060 dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH); 1061 dummystr.nus_buf = NULL; 1062 ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf); 1063 drv.ndo_ifname = "ndis0"; 1064 1065 status = entry(&drv, &dummystr); 1066 1067 free (dummystr.nus_buf, M_DEVBUF); 1068 1069 if (status != NDIS_STATUS_SUCCESS) 1070 return(ENODEV); 1071 1072 /* 1073 * Now that we have the miniport driver characteristics, 1074 * create an NDIS block and call the init handler. 1075 * This will cause the driver to try to probe for 1076 * a device. 1077 */ 1078 1079 block = &sc->ndis_block; 1080 bcopy((char *)&drv.ndo_chars, (char *)&sc->ndis_chars, 1081 sizeof(ndis_miniport_characteristics)); 1082 1083 /*block->nmb_signature = 0xcafebabe;*/ 1084 1085 ptr = (uint32_t *)block; 1086 for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) { 1087 *ptr = idx | 0xdead0000; 1088 ptr++; 1089 } 1090 1091 block->nmb_signature = (void *)0xcafebabe; 1092 block->nmb_setdone_func = ndis_setdone_func; 1093 block->nmb_querydone_func = ndis_getdone_func; 1094 block->nmb_status_func = ndis_status_func; 1095 block->nmb_statusdone_func = ndis_statusdone_func; 1096 block->nmb_resetdone_func = ndis_resetdone_func; 1097 1098 block->nmb_ifp = &sc->arpcom.ac_if; 1099 block->nmb_dev = sc->ndis_dev; 1100 1101 return(0); 1102} 1103