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