ndis(4): Remove comment that doesn't apply to us.
[dragonfly.git] / sys / emulation / ndis / kern_ndis.c
CommitLineData
efba76b4 1/*-
60d6dac1
MD
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 *
efba76b4 32 * $FreeBSD: src/sys/compat/ndis/kern_ndis.c,v 1.111 2011/02/23 21:45:28 brucec Exp $
60d6dac1
MD
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>
efba76b4 47#include <sys/mutex.h>
60d6dac1
MD
48#include <sys/conf.h>
49
50#include <sys/kernel.h>
51#include <sys/module.h>
52#include <sys/kthread.h>
60d6dac1
MD
53#include <sys/bus.h>
54#include <sys/rman.h>
cd8ab232
MD
55#include <sys/mplock2.h>
56
60d6dac1
MD
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
efba76b4
SW
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>
60d6dac1
MD
76#include <dev/netif/ndis/if_ndisvar.h>
77
78#define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
79
efba76b4
SW
80static void ndis_status_func(ndis_handle, ndis_status, void *, uint32_t);
81static void ndis_statusdone_func(ndis_handle);
82static void ndis_setdone_func(ndis_handle, ndis_status);
83static void ndis_getdone_func(ndis_handle, ndis_status);
84static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
85static void ndis_sendrsrcavail_func(ndis_handle);
86static void ndis_intrsetup(kdpc *, device_object *,
87 irp *, struct ndis_softc *);
88static void ndis_return(device_object *, void *);
89
90static 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 }
60d6dac1
MD
101};
102
efba76b4 103static struct nd_head ndis_devhead;
60d6dac1
MD
104
105/*
106 * This allows us to export our symbols to other modules.
efba76b4
SW
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.
60d6dac1 119 */
efba76b4 120
60d6dac1
MD
121static int
122ndis_modevent(module_t mod, int cmd, void *arg)
123{
124 int error = 0;
efba76b4 125 image_patch_table *patch;
60d6dac1
MD
126
127 switch (cmd) {
128 case MOD_LOAD:
129 /* Initialize subsystems */
efba76b4 130 hal_libinit();
60d6dac1 131 ntoskrnl_libinit();
efba76b4
SW
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 }
60d6dac1
MD
143
144 TAILQ_INIT(&ndis_devhead);
60d6dac1
MD
145 break;
146 case MOD_SHUTDOWN:
60d6dac1
MD
147 if (TAILQ_FIRST(&ndis_devhead) == NULL) {
148 /* Shut down subsystems */
149 ndis_libfini();
efba76b4
SW
150 usbd_libfini();
151 windrv_libfini();
60d6dac1 152 ntoskrnl_libfini();
efba76b4 153 hal_libfini();
60d6dac1 154
efba76b4
SW
155 patch = kernndis_functbl;
156 while (patch->ipt_func != NULL) {
157 windrv_unwrap(patch->ipt_wrap);
158 patch++;
159 }
60d6dac1
MD
160 }
161 break;
162 case MOD_UNLOAD:
60d6dac1
MD
163 /* Shut down subsystems */
164 ndis_libfini();
efba76b4
SW
165 usbd_libfini();
166 windrv_libfini();
60d6dac1 167 ntoskrnl_libfini();
efba76b4
SW
168 hal_libfini();
169
170 patch = kernndis_functbl;
171 while (patch->ipt_func != NULL) {
172 windrv_unwrap(patch->ipt_wrap);
173 patch++;
174 }
60d6dac1 175
60d6dac1
MD
176 break;
177 default:
178 error = EINVAL;
179 break;
180 }
181
efba76b4 182 return (error);
60d6dac1 183}
efba76b4
SW
184DEV_MODULE(ndis, ndis_modevent, NULL);
185MODULE_VERSION(ndis, 1);
60d6dac1
MD
186
187static void
2da2a8af 188ndis_sendrsrcavail_func(ndis_handle adapter)
60d6dac1 189{
60d6dac1
MD
190}
191
efba76b4 192static void
2da2a8af 193ndis_status_func(ndis_handle adapter, ndis_status status, void *sbuf,
efba76b4 194 uint32_t slen)
60d6dac1
MD
195{
196 ndis_miniport_block *block;
efba76b4
SW
197 struct ndis_softc *sc;
198 struct ifnet *ifp;
60d6dac1 199
efba76b4
SW
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);
60d6dac1
MD
205}
206
efba76b4 207static void
2da2a8af 208ndis_statusdone_func(ndis_handle adapter)
60d6dac1
MD
209{
210 ndis_miniport_block *block;
efba76b4
SW
211 struct ndis_softc *sc;
212 struct ifnet *ifp;
213
60d6dac1 214 block = adapter;
efba76b4
SW
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");
60d6dac1
MD
219}
220
efba76b4 221static void
2da2a8af 222ndis_setdone_func(ndis_handle adapter, ndis_status status)
60d6dac1
MD
223{
224 ndis_miniport_block *block;
225 block = adapter;
226
227 block->nmb_setstat = status;
efba76b4 228 KeSetEvent(&block->nmb_setevent, IO_NO_INCREMENT, FALSE);
60d6dac1
MD
229}
230
efba76b4 231static void
2da2a8af 232ndis_getdone_func(ndis_handle adapter, ndis_status status)
60d6dac1
MD
233{
234 ndis_miniport_block *block;
235 block = adapter;
236
237 block->nmb_getstat = status;
efba76b4 238 KeSetEvent(&block->nmb_getevent, IO_NO_INCREMENT, FALSE);
60d6dac1
MD
239}
240
efba76b4 241static void
2da2a8af 242ndis_resetdone_func(ndis_handle adapter, ndis_status status,
efba76b4 243 uint8_t addressingreset)
60d6dac1
MD
244{
245 ndis_miniport_block *block;
efba76b4
SW
246 struct ndis_softc *sc;
247 struct ifnet *ifp;
248
60d6dac1 249 block = adapter;
efba76b4
SW
250 sc = device_get_softc(block->nmb_physdeviceobj->do_devext);
251 ifp = sc->ifp;
60d6dac1 252
efba76b4
SW
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);
60d6dac1
MD
256}
257
258int
2da2a8af 259ndis_create_sysctls(void *arg)
60d6dac1
MD
260{
261 struct ndis_softc *sc;
262 ndis_cfg *vals;
263 char buf[256];
efba76b4
SW
264 struct sysctl_oid *oidp;
265 struct sysctl_ctx_entry *e;
60d6dac1
MD
266
267 if (arg == NULL)
efba76b4 268 return (EINVAL);
60d6dac1
MD
269
270 sc = arg;
271 vals = sc->ndis_regvals;
272
273 TAILQ_INIT(&sc->ndis_cfglist_head);
274
60d6dac1
MD
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
60d6dac1
MD
282 /* Add the driver-specific registry keys. */
283
60d6dac1
MD
284 while(1) {
285 if (vals->nc_cfgkey == NULL)
286 break;
efba76b4 287
60d6dac1
MD
288 if (vals->nc_idx != sc->ndis_devidx) {
289 vals++;
290 continue;
291 }
efba76b4
SW
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);
60d6dac1
MD
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
efba76b4
SW
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
60d6dac1 336 /* Bus type (PCI, PCMCIA, etc...) */
f8c7a42d 337 ksprintf(buf, "%d", (int)sc->ndis_iftype);
60d6dac1
MD
338 ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
339
340 if (sc->ndis_res_io != NULL) {
f8c7a42d 341 ksprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
60d6dac1
MD
342 ndis_add_sysctl(sc, "IOBaseAddress",
343 "Base I/O Address", buf, CTLFLAG_RD);
344 }
345
346 if (sc->ndis_irq != NULL) {
f8c7a42d 347 ksprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
60d6dac1
MD
348 ndis_add_sysctl(sc, "InterruptNumber",
349 "Interrupt Number", buf, CTLFLAG_RD);
350 }
351
efba76b4 352 return (0);
60d6dac1
MD
353}
354
355int
2da2a8af 356ndis_add_sysctl(void *arg, char *key, char *desc, char *val, int flag)
60d6dac1
MD
357{
358 struct ndis_softc *sc;
359 struct ndis_cfglist *cfg;
360 char descstr[256];
361
362 sc = arg;
363
efda3bd0 364 cfg = kmalloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_WAITOK|M_ZERO);
59302080 365 cfg->ndis_cfg.nc_cfgkey = kstrdup(key, M_DEVBUF);
60d6dac1 366 if (desc == NULL) {
f8c7a42d 367 ksnprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
59302080 368 cfg->ndis_cfg.nc_cfgdesc = kstrdup(descstr, M_DEVBUF);
60d6dac1 369 } else
59302080 370 cfg->ndis_cfg.nc_cfgdesc = kstrdup(desc, M_DEVBUF);
60d6dac1
MD
371 strcpy(cfg->ndis_cfg.nc_val, val);
372
373 TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
374
efba76b4 375 cfg->ndis_oid =
60d6dac1
MD
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);
60d6dac1 380
efba76b4 381 return (0);
60d6dac1
MD
382}
383
efba76b4
SW
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
60d6dac1 391int
2da2a8af 392ndis_flush_sysctls(void *arg)
60d6dac1
MD
393{
394 struct ndis_softc *sc;
395 struct ndis_cfglist *cfg;
efba76b4 396 struct sysctl_ctx_list *clist;
60d6dac1
MD
397
398 sc = arg;
399
efba76b4
SW
400 clist = &sc->ndis_ctx;
401
60d6dac1
MD
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);
efba76b4
SW
405 sysctl_ctx_entry_del(clist, cfg->ndis_oid);
406 sysctl_remove_oid(cfg->ndis_oid, 1, 0);
efda3bd0
MD
407 kfree(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
408 kfree(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
409 kfree(cfg, M_DEVBUF);
60d6dac1
MD
410 }
411
efba76b4
SW
412 return (0);
413}
414
415void *
416ndis_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);
60d6dac1
MD
424}
425
426static void
efba76b4 427ndis_return(device_object *dobj, void *arg)
60d6dac1 428{
efba76b4
SW
429 ndis_miniport_block *block;
430 ndis_miniport_characteristics *ch;
385c8bc5 431 ndis_return_handler returnfunc;
60d6dac1
MD
432 ndis_handle adapter;
433 ndis_packet *p;
434 uint8_t irql;
efba76b4
SW
435 list_entry *l;
436
437 block = arg;
438 ch = IoGetDriverObjectExtension(dobj->do_drvobj, (void *)1);
60d6dac1
MD
439
440 p = arg;
efba76b4 441 adapter = block->nmb_miniportadapterctx;
60d6dac1
MD
442
443 if (adapter == NULL)
444 return;
445
efba76b4 446 returnfunc = ch->nmc_return_packet_func;
60d6dac1 447
efba76b4
SW
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);
60d6dac1
MD
458}
459
460static void
efba76b4 461ndis_reference_packet(void *arg)
60d6dac1 462{
efba76b4
SW
463 ndis_packet *p;
464
465 if (arg == NULL)
466 return;
467
468 p = arg;
60d6dac1 469
efba76b4
SW
470 /* Increment refcount. */
471 atomic_add_int(&p->np_refcnt, 1);
60d6dac1
MD
472}
473
efba76b4
SW
474void
475ndis_return_packet(void *arg)
60d6dac1 476{
efba76b4
SW
477 ndis_packet *p;
478 ndis_miniport_block *block;
60d6dac1 479
efba76b4 480 if (arg == NULL)
60d6dac1
MD
481 return;
482
efba76b4 483 p = arg;
60d6dac1
MD
484
485 /* Release packet when refcount hits zero, otherwise return. */
efba76b4 486 if (atomic_fetchadd_int(&p->np_refcnt, -1) > 1)
60d6dac1
MD
487 return;
488
efba76b4 489 block = ((struct ndis_softc *)p->np_softc)->ndis_block;
60d6dac1 490
efba76b4
SW
491 KeAcquireSpinLockAtDpcLevel(&block->nmb_returnlock);
492 InitializeListHead((&p->np_list));
493 InsertHeadList((&block->nmb_returnlist), (&p->np_list));
494 KeReleaseSpinLockFromDpcLevel(&block->nmb_returnlock);
60d6dac1 495
efba76b4
SW
496 IoQueueWorkItem(block->nmb_returnitem,
497 (io_workitem_func)kernndis_functbl[7].ipt_wrap,
498 WORKQUEUE_CRITICAL, block);
60d6dac1
MD
499}
500
501void
2da2a8af 502ndis_free_bufs(ndis_buffer *b0)
60d6dac1
MD
503{
504 ndis_buffer *next;
505
506 if (b0 == NULL)
507 return;
508
509 while(b0 != NULL) {
efba76b4
SW
510 next = b0->mdl_next;
511 IoFreeMdl(b0);
60d6dac1
MD
512 b0 = next;
513 }
60d6dac1
MD
514}
515
516void
2da2a8af 517ndis_free_packet(ndis_packet *p)
60d6dac1
MD
518{
519 if (p == NULL)
520 return;
521
522 ndis_free_bufs(p->np_private.npp_head);
efba76b4 523 NdisFreePacket(p);
60d6dac1
MD
524}
525
526int
2da2a8af 527ndis_convert_res(void *arg)
60d6dac1
MD
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;
efba76b4 535 struct resource_list_entry *brle;
60d6dac1 536 struct resource_list brl_rev;
efba76b4
SW
537 struct resource_list_entry *n;
538 int error = 0;
60d6dac1
MD
539
540 sc = arg;
efba76b4 541 block = sc->ndis_block;
60d6dac1
MD
542 dev = sc->ndis_dev;
543
544 SLIST_INIT(&brl_rev);
545
77652cad 546 rl = kmalloc(sizeof(ndis_resource_list) +
60d6dac1 547 (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
6147a8af 548 M_DEVBUF, M_WAITOK|M_NULLOK|M_ZERO);
60d6dac1
MD
549
550 if (rl == NULL)
efba76b4 551 return (ENOMEM);
60d6dac1
MD
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 */
efba76b4 575
60d6dac1 576 SLIST_FOREACH(brle, brl, link) {
77652cad 577 n = kmalloc(sizeof(struct resource_list_entry),
6147a8af 578 M_TEMP, M_WAITOK|M_NULLOK);
60d6dac1
MD
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;
efba76b4 605 prd->u.cprd_mem.cprd_start.np_quad =
60d6dac1 606 brle->start;
efba76b4 607 prd->u.cprd_mem.cprd_len = brle->count;
60d6dac1
MD
608 break;
609 case SYS_RES_IRQ:
610 prd->cprd_type = CmResourceTypeInterrupt;
611 prd->cprd_flags = 0;
efba76b4
SW
612 /*
613 * Always mark interrupt resources as
614 * shared, since in our implementation,
615 * they will be.
616 */
60d6dac1 617 prd->cprd_sharedisp =
efba76b4 618 CmResourceShareShared;
60d6dac1
MD
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
632bad:
633
634 while (!SLIST_EMPTY(&brl_rev)) {
635 n = SLIST_FIRST(&brl_rev);
636 SLIST_REMOVE_HEAD(&brl_rev, link);
efda3bd0 637 kfree (n, M_TEMP);
60d6dac1
MD
638 }
639
efba76b4 640 return (error);
60d6dac1
MD
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
efba76b4 652 * the driver to handle kfree()ing this region for is, so we set up
60d6dac1 653 * a dummy no-op free handler for it.
efba76b4 654 */
60d6dac1
MD
655
656int
2da2a8af 657ndis_ptom(struct mbuf **m0, ndis_packet *p)
60d6dac1 658{
efba76b4 659 struct mbuf *m = NULL, *prev = NULL;
60d6dac1
MD
660 ndis_buffer *buf;
661 ndis_packet_private *priv;
662 uint32_t totlen = 0;
efba76b4
SW
663 struct ifnet *ifp;
664 struct ether_header *eh;
665 int diff;
60d6dac1
MD
666
667 if (p == NULL || m0 == NULL)
efba76b4 668 return (EINVAL);
60d6dac1
MD
669
670 priv = &p->np_private;
671 buf = priv->npp_head;
672 p->np_refcnt = 0;
673
efba76b4
SW
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 {
60d6dac1 679 MGET(m, MB_DONTWAIT, MT_DATA);
efba76b4 680 }
60d6dac1
MD
681 if (m == NULL) {
682 m_freem(*m0);
683 *m0 = NULL;
efba76b4 684 return (ENOBUFS);
60d6dac1 685 }
efba76b4
SW
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
60d6dac1 692 totlen += m->m_len;
6c46447a 693 if (m->m_flags & M_PKTHDR)
60d6dac1
MD
694 *m0 = m;
695 else
696 prev->m_next = m;
697 prev = m;
698 }
699
efba76b4
SW
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 }
60d6dac1
MD
718 (*m0)->m_pkthdr.len = totlen;
719
efba76b4 720 return (0);
60d6dac1
MD
721}
722
723/*
efba76b4 724 * Create an NDIS packet from an mbuf chain.
60d6dac1
MD
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
738int
2da2a8af 739ndis_mtop(struct mbuf *m0, ndis_packet **p)
60d6dac1
MD
740{
741 struct mbuf *m;
742 ndis_buffer *buf = NULL, *prev = NULL;
743 ndis_packet_private *priv;
744
efba76b4
SW
745 if (p == NULL || *p == NULL || m0 == NULL)
746 return (EINVAL);
60d6dac1 747
60d6dac1
MD
748 priv = &(*p)->np_private;
749 priv->npp_totlen = m0->m_pkthdr.len;
60d6dac1
MD
750
751 for (m = m0; m != NULL; m = m->m_next) {
752 if (m->m_len == 0)
753 continue;
efba76b4 754 buf = IoAllocateMdl(m->m_data, m->m_len, FALSE, FALSE, NULL);
60d6dac1
MD
755 if (buf == NULL) {
756 ndis_free_packet(*p);
757 *p = NULL;
efba76b4 758 return (ENOMEM);
60d6dac1 759 }
efba76b4 760 MmBuildMdlForNonPagedPool(buf);
60d6dac1 761
60d6dac1
MD
762 if (priv->npp_head == NULL)
763 priv->npp_head = buf;
764 else
efba76b4 765 prev->mdl_next = buf;
60d6dac1
MD
766 prev = buf;
767 }
768
769 priv->npp_tail = buf;
60d6dac1 770
efba76b4 771 return (0);
60d6dac1
MD
772}
773
774int
2da2a8af 775ndis_get_supported_oids(void *arg, ndis_oid **oids, int *oidcnt)
60d6dac1
MD
776{
777 int len, rval;
778 ndis_oid *o;
779
780 if (arg == NULL || oids == NULL || oidcnt == NULL)
efba76b4 781 return (EINVAL);
60d6dac1
MD
782 len = 0;
783 ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
784
efda3bd0 785 o = kmalloc(len, M_DEVBUF, M_WAITOK);
60d6dac1
MD
786
787 rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
788
789 if (rval) {
efda3bd0 790 kfree(o, M_DEVBUF);
efba76b4 791 return (rval);
60d6dac1
MD
792 }
793
794 *oids = o;
795 *oidcnt = len / 4;
796
efba76b4 797 return (0);
60d6dac1
MD
798}
799
800int
2da2a8af 801ndis_set_info(void *arg, ndis_oid oid, void *buf, int *buflen)
60d6dac1
MD
802{
803 struct ndis_softc *sc;
804 ndis_status rval;
805 ndis_handle adapter;
385c8bc5 806 ndis_setinfo_handler setfunc;
60d6dac1 807 uint32_t byteswritten = 0, bytesneeded = 0;
60d6dac1 808 uint8_t irql;
efba76b4
SW
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 */
60d6dac1
MD
817
818 sc = arg;
60d6dac1 819
efba76b4
SW
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 }
60d6dac1 839
efba76b4 840 rval = MSCALL6(setfunc, adapter, oid, buf, *buflen,
60d6dac1 841 &byteswritten, &bytesneeded);
efba76b4
SW
842
843 sc->ndis_block->nmb_pendingreq = NULL;
844
845 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
60d6dac1
MD
846
847 if (rval == NDIS_STATUS_PENDING) {
efba76b4
SW
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;
60d6dac1
MD
853 }
854
855 if (byteswritten)
856 *buflen = byteswritten;
857 if (bytesneeded)
858 *buflen = bytesneeded;
859
860 if (rval == NDIS_STATUS_INVALID_LENGTH)
efba76b4 861 return (ENOSPC);
60d6dac1
MD
862
863 if (rval == NDIS_STATUS_INVALID_OID)
efba76b4 864 return (EINVAL);
60d6dac1
MD
865
866 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
867 rval == NDIS_STATUS_NOT_ACCEPTED)
efba76b4 868 return (ENOTSUP);
60d6dac1
MD
869
870 if (rval != NDIS_STATUS_SUCCESS)
efba76b4 871 return (ENODEV);
60d6dac1 872
efba76b4 873 return (0);
60d6dac1
MD
874}
875
efba76b4 876typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
60d6dac1
MD
877
878int
2da2a8af 879ndis_send_packets(void *arg, ndis_packet **packets, int cnt)
60d6dac1
MD
880{
881 struct ndis_softc *sc;
882 ndis_handle adapter;
385c8bc5 883 ndis_sendmulti_handler sendfunc;
efba76b4 884 ndis_senddone_func senddonefunc;
60d6dac1
MD
885 int i;
886 ndis_packet *p;
efba76b4 887 uint8_t irql = 0;
60d6dac1
MD
888
889 sc = arg;
efba76b4 890 adapter = sc->ndis_block->nmb_miniportadapterctx;
60d6dac1 891 if (adapter == NULL)
efba76b4
SW
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);
60d6dac1
MD
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;
efba76b4 911 MSCALL3(senddonefunc, sc->ndis_block, p, p->np_oob.npo_status);
60d6dac1
MD
912 }
913
efba76b4
SW
914 if (NDIS_SERIALIZED(sc->ndis_block))
915 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
916
60d6dac1
MD
917 return(0);
918}
919
920int
2da2a8af 921ndis_send_packet(void *arg, ndis_packet *packet)
60d6dac1
MD
922{
923 struct ndis_softc *sc;
924 ndis_handle adapter;
925 ndis_status status;
385c8bc5 926 ndis_sendsingle_handler sendfunc;
efba76b4
SW
927 ndis_senddone_func senddonefunc;
928 uint8_t irql = 0;
60d6dac1
MD
929
930 sc = arg;
efba76b4 931 adapter = sc->ndis_block->nmb_miniportadapterctx;
60d6dac1 932 if (adapter == NULL)
efba76b4
SW
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 }
60d6dac1 947
efba76b4 948 MSCALL3(senddonefunc, sc->ndis_block, packet, status);
60d6dac1 949
efba76b4
SW
950 if (NDIS_SERIALIZED(sc->ndis_block))
951 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
60d6dac1 952
efba76b4 953 return (0);
60d6dac1
MD
954}
955
956int
2da2a8af 957ndis_init_dma(void *arg)
60d6dac1
MD
958{
959 struct ndis_softc *sc;
960 int i, error;
961
962 sc = arg;
963
77652cad 964 sc->ndis_tmaps = kmalloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
6147a8af 965 M_DEVBUF, M_WAITOK|M_ZERO);
60d6dac1 966
60d6dac1
MD
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) {
efda3bd0 971 kfree(sc->ndis_tmaps, M_DEVBUF);
efba76b4 972 return (ENODEV);
60d6dac1
MD
973 }
974 }
975
efba76b4 976 return (0);
60d6dac1
MD
977}
978
979int
2da2a8af 980ndis_destroy_dma(void *arg)
60d6dac1
MD
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 }
efba76b4
SW
999
1000 kfree(sc->ndis_tmaps, M_DEVBUF);
1001
60d6dac1
MD
1002 bus_dma_tag_destroy(sc->ndis_ttag);
1003
efba76b4 1004 return (0);
60d6dac1
MD
1005}
1006
1007int
2da2a8af 1008ndis_reset_nic(void *arg)
60d6dac1
MD
1009{
1010 struct ndis_softc *sc;
1011 ndis_handle adapter;
385c8bc5 1012 ndis_reset_handler resetfunc;
60d6dac1 1013 uint8_t addressing_reset;
60d6dac1 1014 int rval;
efba76b4 1015 uint8_t irql = 0;
60d6dac1
MD
1016
1017 sc = arg;
60d6dac1 1018
efba76b4
SW
1019 NDIS_LOCK(sc);
1020 adapter = sc->ndis_block->nmb_miniportadapterctx;
1021 resetfunc = sc->ndis_chars->nmc_reset_func;
60d6dac1 1022
efba76b4
SW
1023 if (adapter == NULL || resetfunc == NULL ||
1024 sc->ndis_block->nmb_devicectx == NULL) {
1025 NDIS_UNLOCK(sc);
1026 return(EIO);
60d6dac1
MD
1027 }
1028
efba76b4
SW
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);
60d6dac1
MD
1046}
1047
1048int
2da2a8af 1049ndis_halt_nic(void *arg)
60d6dac1
MD
1050{
1051 struct ndis_softc *sc;
1052 ndis_handle adapter;
385c8bc5 1053 ndis_halt_handler haltfunc;
efba76b4
SW
1054 ndis_miniport_block *block;
1055 int empty = 0;
1056 uint8_t irql;
60d6dac1
MD
1057
1058 sc = arg;
efba76b4 1059 block = sc->ndis_block;
60d6dac1 1060
efba76b4
SW
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;
60d6dac1 1079 if (adapter == NULL) {
efba76b4
SW
1080 NDIS_UNLOCK(sc);
1081 return (EIO);
60d6dac1
MD
1082 }
1083
efba76b4
SW
1084 sc->ndis_block->nmb_devicectx = NULL;
1085
60d6dac1
MD
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
efba76b4
SW
1092 haltfunc = sc->ndis_chars->nmc_halt_func;
1093 NDIS_UNLOCK(sc);
60d6dac1 1094
efba76b4 1095 MSCALL1(haltfunc, adapter);
60d6dac1 1096
efba76b4
SW
1097 NDIS_LOCK(sc);
1098 sc->ndis_block->nmb_miniportadapterctx = NULL;
1099 NDIS_UNLOCK(sc);
60d6dac1 1100
efba76b4 1101 return (0);
60d6dac1
MD
1102}
1103
1104int
2da2a8af 1105ndis_shutdown_nic(void *arg)
60d6dac1
MD
1106{
1107 struct ndis_softc *sc;
1108 ndis_handle adapter;
385c8bc5 1109 ndis_shutdown_handler shutdownfunc;
60d6dac1
MD
1110
1111 sc = arg;
efba76b4
SW
1112 NDIS_LOCK(sc);
1113 adapter = sc->ndis_block->nmb_miniportadapterctx;
1114 shutdownfunc = sc->ndis_chars->nmc_shutdown_handler;
1115 NDIS_UNLOCK(sc);
60d6dac1 1116 if (adapter == NULL || shutdownfunc == NULL)
efba76b4 1117 return (EIO);
60d6dac1 1118
efba76b4
SW
1119 if (sc->ndis_chars->nmc_rsvd0 == NULL)
1120 MSCALL1(shutdownfunc, adapter);
60d6dac1 1121 else
efba76b4 1122 MSCALL1(shutdownfunc, sc->ndis_chars->nmc_rsvd0);
60d6dac1 1123
efba76b4 1124 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
60d6dac1
MD
1125
1126 return(0);
1127}
1128
efba76b4
SW
1129int
1130ndis_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
60d6dac1 1154int
2da2a8af 1155ndis_init_nic(void *arg)
60d6dac1
MD
1156{
1157 struct ndis_softc *sc;
1158 ndis_miniport_block *block;
385c8bc5 1159 ndis_init_handler initfunc;
60d6dac1
MD
1160 ndis_status status, openstatus = 0;
1161 ndis_medium mediumarray[NdisMediumMax];
1162 uint32_t chosenmedium, i;
60d6dac1
MD
1163
1164 if (arg == NULL)
efba76b4 1165 return (EINVAL);
60d6dac1
MD
1166
1167 sc = arg;
efba76b4
SW
1168 NDIS_LOCK(sc);
1169 block = sc->ndis_block;
1170 initfunc = sc->ndis_chars->nmc_init_func;
1171 NDIS_UNLOCK(sc);
60d6dac1 1172
efba76b4 1173 sc->ndis_block->nmb_timerlist = NULL;
60d6dac1
MD
1174
1175 for (i = 0; i < NdisMediumMax; i++)
1176 mediumarray[i] = i;
1177
efba76b4 1178 status = MSCALL6(initfunc, &openstatus, &chosenmedium,
60d6dac1
MD
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) {
efba76b4
SW
1187 NDIS_LOCK(sc);
1188 sc->ndis_block->nmb_miniportadapterctx = NULL;
1189 NDIS_UNLOCK(sc);
1190 return (ENXIO);
60d6dac1
MD
1191 }
1192
efba76b4
SW
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 */
60d6dac1 1202
efba76b4 1203 tsleep(arg, 0, "ndwait", hz);
60d6dac1 1204
efba76b4
SW
1205 NDIS_LOCK(sc);
1206 sc->ndis_block->nmb_devicectx = sc;
1207 NDIS_UNLOCK(sc);
60d6dac1 1208
efba76b4 1209 return (0);
60d6dac1
MD
1210}
1211
efba76b4
SW
1212static void
1213ndis_intrsetup(kdpc *dpc, device_object *dobj, irp *ip, struct ndis_softc *sc)
60d6dac1 1214{
efba76b4 1215 ndis_miniport_interrupt *intr;
60d6dac1 1216
efba76b4 1217 intr = sc->ndis_block->nmb_interrupt;
60d6dac1 1218
efba76b4 1219 /* Sanity check. */
60d6dac1 1220
efba76b4
SW
1221 if (intr == NULL)
1222 return;
60d6dac1 1223
efba76b4
SW
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);
60d6dac1
MD
1229}
1230
1231int
2da2a8af 1232ndis_get_info(void *arg, ndis_oid oid, void *buf, int *buflen)
60d6dac1
MD
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;
60d6dac1 1239 uint8_t irql;
efba76b4 1240 uint64_t duetime;
60d6dac1
MD
1241
1242 sc = arg;
60d6dac1 1243
efba76b4
SW
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 }
60d6dac1 1263
efba76b4 1264 rval = MSCALL6(queryfunc, adapter, oid, buf, *buflen,
60d6dac1 1265 &byteswritten, &bytesneeded);
efba76b4
SW
1266
1267 sc->ndis_block->nmb_pendingreq = NULL;
1268
1269 KeReleaseSpinLock(&sc->ndis_block->nmb_lock, irql);
60d6dac1
MD
1270
1271 /* Wait for requests that block. */
1272
1273 if (rval == NDIS_STATUS_PENDING) {
efba76b4
SW
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;
60d6dac1
MD
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)
efba76b4 1288 return (ENOSPC);
60d6dac1
MD
1289
1290 if (rval == NDIS_STATUS_INVALID_OID)
efba76b4 1291 return (EINVAL);
60d6dac1
MD
1292
1293 if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1294 rval == NDIS_STATUS_NOT_ACCEPTED)
efba76b4 1295 return (ENOTSUP);
60d6dac1
MD
1296
1297 if (rval != NDIS_STATUS_SUCCESS)
efba76b4 1298 return (ENODEV);
60d6dac1 1299
efba76b4 1300 return (0);
60d6dac1
MD
1301}
1302
efba76b4
SW
1303uint32_t
1304NdisAddDevice(driver_object *drv, device_object *pdo)
60d6dac1 1305{
efba76b4
SW
1306 device_object *fdo;
1307 ndis_miniport_block *block;
60d6dac1 1308 struct ndis_softc *sc;
efba76b4
SW
1309 uint32_t status;
1310 int error;
60d6dac1 1311
efba76b4 1312 sc = device_get_softc(pdo->do_devext);
60d6dac1 1313
efba76b4
SW
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 }
60d6dac1 1321
efba76b4
SW
1322 status = IoCreateDevice(drv, sizeof(ndis_miniport_block), NULL,
1323 FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo);
60d6dac1 1324
efba76b4
SW
1325 if (status != STATUS_SUCCESS)
1326 return (status);
60d6dac1 1327
efba76b4 1328 block = fdo->do_devext;
60d6dac1 1329
efba76b4
SW
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);
60d6dac1 1342
efba76b4
SW
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);
60d6dac1
MD
1350
1351 /*
efba76b4
SW
1352 * If the driver has a MiniportTransferData() function,
1353 * we should allocate a private RX packet pool.
60d6dac1
MD
1354 */
1355
efba76b4
SW
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);
60d6dac1 1363 }
efba76b4 1364 InitializeListHead((&block->nmb_packetlist));
60d6dac1
MD
1365 }
1366
efba76b4
SW
1367 /* Give interrupt handling priority over timers. */
1368 IoInitializeDpcRequest(fdo, kernndis_functbl[6].ipt_wrap);
1369 KeSetImportanceDpc(&fdo->do_dpc, KDPC_IMPORTANCE_HIGH);
60d6dac1 1370
efba76b4 1371 /* Finish up BSD-specific setup. */
60d6dac1 1372
efba76b4
SW
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;
60d6dac1 1381
efba76b4 1382 TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
60d6dac1 1383
efba76b4
SW
1384 return (STATUS_SUCCESS);
1385}
60d6dac1 1386
efba76b4
SW
1387int
1388ndis_unload_driver(void *arg)
1389{
1390 struct ndis_softc *sc;
1391 device_object *fdo;
60d6dac1 1392
efba76b4 1393 sc = arg;
60d6dac1 1394
efba76b4
SW
1395 if (sc->ndis_intrhand)
1396 bus_teardown_intr(sc->ndis_dev,
1397 sc->ndis_irq, sc->ndis_intrhand);
60d6dac1 1398
efba76b4
SW
1399 if (sc->ndis_block->nmb_rlist != NULL)
1400 kfree(sc->ndis_block->nmb_rlist, M_DEVBUF);
60d6dac1 1401
efba76b4 1402 ndis_flush_sysctls(sc);
60d6dac1 1403
efba76b4 1404 TAILQ_REMOVE(&ndis_devhead, sc->ndis_block, link);
60d6dac1 1405
efba76b4
SW
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);
60d6dac1 1412
efba76b4
SW
1413 return (0);
1414}