kern_ndis.c revision 123858
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 123858 2003-12-26 07:01:05Z 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		returnfunc(adapter, p);
357
358	return;
359}
360
361void
362ndis_free_bufs(b0)
363	ndis_buffer		*b0;
364{
365	ndis_buffer		*next;
366
367	if (b0 == NULL)
368		return;
369
370	while(b0 != NULL) {
371		next = b0->nb_next;
372		free (b0, M_DEVBUF);
373		b0 = next;
374	}
375
376	return;
377}
378
379void
380ndis_free_packet(p)
381	ndis_packet		*p;
382{
383	if (p == NULL)
384		return;
385
386	ndis_free_bufs(p->np_private.npp_head);
387	free(p, M_DEVBUF);
388
389	return;
390}
391
392int
393ndis_convert_res(arg)
394	void			*arg;
395{
396	struct ndis_softc	*sc;
397	ndis_resource_list	*rl = NULL;
398	cm_partial_resource_desc	*prd = NULL;
399	ndis_miniport_block	*block;
400
401	sc = arg;
402	block = &sc->ndis_block;
403
404	rl = malloc(sizeof(ndis_resource_list) +
405	    (sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
406	    M_DEVBUF, M_NOWAIT|M_ZERO);
407
408	if (rl == NULL)
409		return(ENOMEM);
410
411	rl->cprl_version = 5;
412	rl->cprl_version = 1;
413	rl->cprl_count = sc->ndis_rescnt;
414
415	prd = rl->cprl_partial_descs;
416	if (sc->ndis_res_io) {
417		prd->cprd_type = CmResourceTypePort;
418		prd->u.cprd_port.cprd_start.np_quad =
419		    rman_get_start(sc->ndis_res_io);
420		prd->u.cprd_port.cprd_len =
421		    rman_get_size(sc->ndis_res_io);
422		prd++;
423	}
424
425	if (sc->ndis_res_mem) {
426		prd->cprd_type = CmResourceTypeMemory;
427		prd->u.cprd_mem.cprd_start.np_quad =
428		    rman_get_start(sc->ndis_res_mem);
429		prd->u.cprd_mem.cprd_len =
430		    rman_get_size(sc->ndis_res_mem);
431		prd++;
432	}
433
434	if (sc->ndis_irq) {
435		prd->cprd_type = CmResourceTypeInterrupt;
436		prd->u.cprd_intr.cprd_level =
437		    rman_get_start(sc->ndis_irq);
438		prd->u.cprd_intr.cprd_vector =
439		    rman_get_start(sc->ndis_irq);
440		prd->u.cprd_intr.cprd_affinity = 0;
441	}
442
443	block->nmb_rlist = rl;
444
445	return(0);
446}
447
448/*
449 * Map an NDIS packet to an mbuf list. When an NDIS driver receives a
450 * packet, it will hand it to us in the form of an ndis_packet,
451 * which we need to convert to an mbuf that is then handed off
452 * to the stack. Note: we configure the mbuf list so that it uses
453 * the memory regions specified by the ndis_buffer structures in
454 * the ndis_packet as external storage. In most cases, this will
455 * point to a memory region allocated by the driver (either by
456 * ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
457 * the driver to handle free()ing this region for is, so we set up
458 * a dummy no-op free handler for it.
459 */
460
461int
462ndis_ptom(m0, p)
463	struct mbuf		**m0;
464	ndis_packet		*p;
465{
466	struct mbuf		*m, *prev = NULL;
467	ndis_buffer		*buf;
468	ndis_packet_private	*priv;
469	uint32_t		totlen = 0;
470
471	if (p == NULL || m0 == NULL)
472		return(EINVAL);
473
474	priv = &p->np_private;
475	buf = priv->npp_head;
476	p->np_refcnt = 0;
477
478	for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) {
479		if (buf == priv->npp_head)
480			MGETHDR(m, M_DONTWAIT, MT_HEADER);
481		else
482			MGET(m, M_DONTWAIT, MT_DATA);
483		if (m == NULL) {
484			m_freem(*m0);
485			*m0 = NULL;
486			return(ENOBUFS);
487		}
488		m->m_len = buf->nb_bytecount;
489		m->m_data = MDL_VA(buf);
490		MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
491		    p, 0, EXT_NDIS);
492		p->np_refcnt++;
493		totlen += m->m_len;
494		if (m->m_flags & MT_HEADER)
495			*m0 = m;
496		else
497			prev->m_next = m;
498		prev = m;
499	}
500
501	(*m0)->m_pkthdr.len = totlen;
502
503	return(0);
504}
505
506/*
507 * Create an mbuf chain from an NDIS packet chain.
508 * This is used mainly when transmitting packets, where we need
509 * to turn an mbuf off an interface's send queue and transform it
510 * into an NDIS packet which will be fed into the NDIS driver's
511 * send routine.
512 *
513 * NDIS packets consist of two parts: an ndis_packet structure,
514 * which is vaguely analagous to the pkthdr portion of an mbuf,
515 * and one or more ndis_buffer structures, which define the
516 * actual memory segments in which the packet data resides.
517 * We need to allocate one ndis_buffer for each mbuf in a chain,
518 * plus one ndis_packet as the header.
519 */
520
521int
522ndis_mtop(m0, p)
523	struct mbuf		*m0;
524	ndis_packet		**p;
525{
526	struct mbuf		*m;
527	ndis_buffer		*buf = NULL, *prev = NULL;
528	ndis_packet_private	*priv;
529
530	if (p == NULL || m0 == NULL)
531		return(EINVAL);
532
533	/* If caller didn't supply a packet, make one. */
534	if (*p == NULL) {
535		*p = malloc(sizeof(ndis_packet), M_DEVBUF, M_NOWAIT|M_ZERO);
536
537		if (*p == NULL)
538			return(ENOMEM);
539	}
540
541	priv = &(*p)->np_private;
542	priv->npp_totlen = m0->m_pkthdr.len;
543        priv->npp_packetooboffset = offsetof(ndis_packet, np_oob);
544
545	for (m = m0; m != NULL; m = m->m_next) {
546		if (m->m_len == 0)
547			continue;
548		buf = malloc(sizeof(ndis_buffer), M_DEVBUF, M_NOWAIT|M_ZERO);
549		if (buf == NULL) {
550			ndis_free_packet(*p);
551			*p = NULL;
552			return(ENOMEM);
553		}
554
555		MDL_INIT(buf, m->m_data, m->m_len);
556		if (priv->npp_head == NULL)
557			priv->npp_head = buf;
558		else
559			prev->nb_next = buf;
560		prev = buf;
561	}
562
563	priv->npp_tail = buf;
564
565	return(0);
566}
567
568int
569ndis_get_supported_oids(arg, oids, oidcnt)
570	void			*arg;
571	ndis_oid		**oids;
572	int			*oidcnt;
573{
574	int			len, rval;
575	ndis_oid		*o;
576
577	if (arg == NULL || oids == NULL || oidcnt == NULL)
578		return(EINVAL);
579	len = 0;
580	ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
581
582	o = malloc(len, M_DEVBUF, M_NOWAIT);
583	if (o == NULL)
584		return(ENOMEM);
585
586	rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
587
588	if (rval) {
589		free(o, M_DEVBUF);
590		return(rval);
591	}
592
593	*oids = o;
594	*oidcnt = len / 4;
595
596	return(0);
597}
598
599int
600ndis_set_info(arg, oid, buf, buflen)
601	void			*arg;
602	ndis_oid		oid;
603	void			*buf;
604	int			*buflen;
605{
606	struct ndis_softc	*sc;
607	ndis_status		rval;
608	ndis_handle		adapter;
609	__stdcall ndis_setinfo_handler	setfunc;
610	uint32_t		byteswritten = 0, bytesneeded = 0;
611	struct timeval		tv;
612	int			error;
613
614	sc = arg;
615	setfunc = sc->ndis_chars.nmc_setinfo_func;
616	adapter = sc->ndis_block.nmb_miniportadapterctx;
617
618	rval = setfunc(adapter, oid, buf, *buflen,
619	    &byteswritten, &bytesneeded);
620
621	if (rval == NDIS_STATUS_PENDING) {
622		tv.tv_sec = 60;
623		tv.tv_usec = 0;
624		error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
625		    PPAUSE|PCATCH, "ndisset", tvtohz(&tv));
626		rval = sc->ndis_block.nmb_setstat;
627	}
628
629	if (byteswritten)
630		*buflen = byteswritten;
631	if (bytesneeded)
632		*buflen = bytesneeded;
633
634	if (rval == NDIS_STATUS_INVALID_LENGTH)
635		return(ENOSPC);
636
637	if (rval == NDIS_STATUS_INVALID_OID)
638		return(EINVAL);
639
640	if (rval == NDIS_STATUS_NOT_SUPPORTED ||
641	    rval == NDIS_STATUS_NOT_ACCEPTED)
642		return(ENOTSUP);
643
644	return(0);
645}
646
647int
648ndis_send_packets(arg, packets, cnt)
649	void			*arg;
650	ndis_packet		**packets;
651	int			cnt;
652{
653	struct ndis_softc	*sc;
654	ndis_handle		adapter;
655	__stdcall ndis_sendmulti_handler	sendfunc;
656	int			i, idx;
657	struct ifnet		*ifp;
658	struct mbuf		*m;
659	ndis_packet		*p;
660
661	sc = arg;
662	adapter = sc->ndis_block.nmb_miniportadapterctx;
663	sendfunc = sc->ndis_chars.nmc_sendmulti_func;
664	sendfunc(adapter, packets, cnt);
665
666	for (i = 0; i < cnt; i++) {
667		p = packets[i];
668		if (p->np_oob.npo_status == NDIS_STATUS_PENDING)
669			continue;
670		idx = p->np_txidx;
671		m = p->np_m0;
672		ifp = &sc->arpcom.ac_if;
673		if (sc->ndis_sc)
674			bus_dmamap_unload(sc->ndis_ttag, sc->ndis_tmaps[idx]);
675		sc->ndis_txarray[idx] = NULL;
676		sc->ndis_txpending++;
677		m_freem(m);
678		ndis_free_packet(p);
679		if (p->np_oob.npo_status == NDIS_STATUS_SUCCESS)
680			ifp->if_opackets++;
681		else
682			ifp->if_oerrors++;
683		ifp->if_timer = 0;
684		ifp->if_flags &= ~IFF_OACTIVE;
685	}
686
687	return(0);
688}
689
690int
691ndis_init_dma(arg)
692	void			*arg;
693{
694	struct ndis_softc	*sc;
695	int			i, error;
696
697	sc = arg;
698
699	sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
700	    M_DEVBUF, M_NOWAIT|M_ZERO);
701
702	if (sc->ndis_tmaps == NULL)
703		return(ENOMEM);
704
705	for (i = 0; i < sc->ndis_maxpkts; i++) {
706		error = bus_dmamap_create(sc->ndis_ttag, 0,
707		    &sc->ndis_tmaps[i]);
708		if (error) {
709			free(sc->ndis_tmaps, M_DEVBUF);
710			return(ENODEV);
711		}
712	}
713
714	return(0);
715}
716
717int
718ndis_destroy_dma(arg)
719	void			*arg;
720{
721	struct ndis_softc	*sc;
722	struct mbuf		*m;
723	ndis_packet		*p = NULL;
724	int			i;
725
726	sc = arg;
727
728	for (i = 0; i < sc->ndis_maxpkts; i++) {
729		if (sc->ndis_txarray[i] != NULL) {
730			p = sc->ndis_txarray[i];
731			m = (struct mbuf *)p->np_rsvd[1];
732			if (m != NULL)
733				m_freem(m);
734			ndis_free_packet(sc->ndis_txarray[i]);
735		}
736		bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
737	}
738
739	free(sc->ndis_tmaps, M_DEVBUF);
740
741	bus_dma_tag_destroy(sc->ndis_ttag);
742
743	return(0);
744}
745
746int
747ndis_reset_nic(arg)
748	void			*arg;
749{
750	struct ndis_softc	*sc;
751	ndis_handle		adapter;
752	__stdcall ndis_reset_handler	resetfunc;
753	uint8_t			addressing_reset;
754	struct ifnet		*ifp;
755
756	sc = arg;
757	ifp = &sc->arpcom.ac_if;
758	adapter = sc->ndis_block.nmb_miniportadapterctx;
759	if (adapter == NULL)
760		return(EIO);
761	resetfunc = sc->ndis_chars.nmc_reset_func;
762
763	if (resetfunc == NULL)
764		return(EINVAL);
765
766	resetfunc(&addressing_reset, adapter);
767
768	return(0);
769}
770
771int
772ndis_halt_nic(arg)
773	void			*arg;
774{
775	struct ndis_softc	*sc;
776	ndis_handle		adapter;
777	__stdcall ndis_halt_handler	haltfunc;
778	struct ifnet		*ifp;
779	struct ndis_timer_entry	*ne;
780
781	sc = arg;
782	ifp = &sc->arpcom.ac_if;
783	adapter = sc->ndis_block.nmb_miniportadapterctx;
784	if (adapter == NULL)
785		return(EIO);
786
787	haltfunc = sc->ndis_chars.nmc_halt_func;
788
789	if (haltfunc == NULL)
790		return(EINVAL);
791
792	haltfunc(adapter);
793
794	/*
795	 * The adapter context is only valid after the init
796	 * handler has been called, and is invalid once the
797	 * halt handler has been called.
798	 */
799
800	sc->ndis_block.nmb_miniportadapterctx = NULL;
801
802	/* Clobber all the timers in case the driver left one running. */
803
804	while (!TAILQ_EMPTY(&sc->ndis_block.nmb_timerlist)) {
805		ne = TAILQ_FIRST(&sc->ndis_block.nmb_timerlist);
806		TAILQ_REMOVE(&sc->ndis_block.nmb_timerlist, ne, link);
807		callout_stop(&ne->nte_ch);
808		free(ne, M_DEVBUF);
809	}
810
811	return(0);
812}
813
814int
815ndis_shutdown_nic(arg)
816	void			*arg;
817{
818	struct ndis_softc	*sc;
819	ndis_handle		adapter;
820	__stdcall ndis_shutdown_handler	shutdownfunc;
821
822
823	sc = arg;
824	adapter = sc->ndis_block.nmb_miniportadapterctx;
825	if (adapter == NULL)
826		return(EIO);
827	shutdownfunc = sc->ndis_chars.nmc_shutdown_handler;
828
829	if (shutdownfunc == NULL)
830		return(EINVAL);
831
832	if (sc->ndis_chars.nmc_rsvd0 == NULL)
833		shutdownfunc(adapter);
834	else
835		shutdownfunc(sc->ndis_chars.nmc_rsvd0);
836
837	return(0);
838}
839
840int
841ndis_init_nic(arg)
842	void			*arg;
843{
844	struct ndis_softc	*sc;
845	ndis_miniport_block	*block;
846        __stdcall ndis_init_handler	initfunc;
847	ndis_status		status, openstatus = 0;
848	ndis_medium		mediumarray[NdisMediumMax];
849	uint32_t		chosenmedium, i;
850
851	if (arg == NULL)
852		return(EINVAL);
853
854	sc = arg;
855	block = &sc->ndis_block;
856	initfunc = sc->ndis_chars.nmc_init_func;
857
858	TAILQ_INIT(&block->nmb_timerlist);
859
860	for (i = 0; i < NdisMediumMax; i++)
861		mediumarray[i] = i;
862
863        status = initfunc(&openstatus, &chosenmedium,
864            mediumarray, NdisMediumMax, block, block);
865
866	/*
867	 * If the init fails, blow away the other exported routines
868	 * we obtained from the driver so we can't call them later.
869	 * If the init failed, none of these will work.
870	 */
871	if (status != NDIS_STATUS_SUCCESS) {
872		bzero((char *)&sc->ndis_chars,
873		    sizeof(ndis_miniport_characteristics));
874		return(ENXIO);
875	}
876
877	return(0);
878}
879
880void
881ndis_enable_intr(arg)
882	void			*arg;
883{
884	struct ndis_softc	*sc;
885	ndis_handle		adapter;
886	__stdcall ndis_enable_interrupts_handler	intrenbfunc;
887
888	sc = arg;
889	adapter = sc->ndis_block.nmb_miniportadapterctx;
890	if (adapter == NULL)
891	    return;
892	intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func;
893	if (intrenbfunc == NULL)
894		return;
895	intrenbfunc(adapter);
896
897	return;
898}
899
900void
901ndis_disable_intr(arg)
902	void			*arg;
903{
904	struct ndis_softc	*sc;
905	ndis_handle		adapter;
906	__stdcall ndis_disable_interrupts_handler	intrdisfunc;
907
908	sc = arg;
909	adapter = sc->ndis_block.nmb_miniportadapterctx;
910	if (adapter == NULL)
911	    return;
912	intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func;
913	if (intrdisfunc == NULL)
914		return;
915	intrdisfunc(adapter);
916
917	return;
918}
919
920int
921ndis_isr(arg, ourintr, callhandler)
922	void			*arg;
923	int			*ourintr;
924	int			*callhandler;
925{
926	struct ndis_softc	*sc;
927	ndis_handle		adapter;
928	__stdcall ndis_isr_handler	isrfunc;
929	uint8_t			accepted, queue;
930
931	if (arg == NULL || ourintr == NULL || callhandler == NULL)
932		return(EINVAL);
933
934	sc = arg;
935	adapter = sc->ndis_block.nmb_miniportadapterctx;
936	isrfunc = sc->ndis_chars.nmc_isr_func;
937	isrfunc(&accepted, &queue, adapter);
938	*ourintr = accepted;
939	*callhandler = queue;
940
941	return(0);
942}
943
944int
945ndis_intrhand(arg)
946	void			*arg;
947{
948	struct ndis_softc	*sc;
949	ndis_handle		adapter;
950	__stdcall ndis_interrupt_handler	intrfunc;
951
952	if (arg == NULL)
953		return(EINVAL);
954
955	sc = arg;
956	adapter = sc->ndis_block.nmb_miniportadapterctx;
957	intrfunc = sc->ndis_chars.nmc_interrupt_func;
958	intrfunc(adapter);
959
960	return(0);
961}
962
963int
964ndis_get_info(arg, oid, buf, buflen)
965	void			*arg;
966	ndis_oid		oid;
967	void			*buf;
968	int			*buflen;
969{
970	struct ndis_softc	*sc;
971	ndis_status		rval;
972	ndis_handle		adapter;
973	__stdcall ndis_queryinfo_handler	queryfunc;
974	uint32_t		byteswritten = 0, bytesneeded = 0;
975	struct timeval		tv;
976	int			error;
977
978	sc = arg;
979	queryfunc = sc->ndis_chars.nmc_queryinfo_func;
980	adapter = sc->ndis_block.nmb_miniportadapterctx;
981
982	rval = queryfunc(adapter, oid, buf, *buflen,
983	    &byteswritten, &bytesneeded);
984
985	/* Wait for requests that block. */
986
987	if (rval == NDIS_STATUS_PENDING) {
988		tv.tv_sec = 60;
989		tv.tv_usec = 0;
990		error = tsleep(&sc->ndis_block.nmb_wkupdpctimer,
991		    PPAUSE|PCATCH, "ndisget", tvtohz(&tv));
992		rval = sc->ndis_block.nmb_getstat;
993	}
994
995	if (byteswritten)
996		*buflen = byteswritten;
997	if (bytesneeded)
998		*buflen = bytesneeded;
999
1000	if (rval == NDIS_STATUS_INVALID_LENGTH ||
1001	    rval == NDIS_STATUS_BUFFER_TOO_SHORT)
1002		return(ENOSPC);
1003
1004	if (rval == NDIS_STATUS_INVALID_OID)
1005		return(EINVAL);
1006
1007	if (rval == NDIS_STATUS_NOT_SUPPORTED ||
1008	    rval == NDIS_STATUS_NOT_ACCEPTED)
1009		return(ENOTSUP);
1010
1011	return(0);
1012}
1013
1014int
1015ndis_unload_driver(arg)
1016	void			*arg;
1017{
1018	struct ndis_softc	*sc;
1019
1020	sc = arg;
1021
1022	free(sc->ndis_block.nmb_rlist, M_DEVBUF);
1023
1024	ndis_flush_sysctls(sc);
1025	ndis_libfini();
1026	ntoskrnl_libfini();
1027
1028	return(0);
1029}
1030
1031int
1032ndis_load_driver(img, arg)
1033	vm_offset_t		img;
1034	void			*arg;
1035{
1036	__stdcall driver_entry	entry;
1037	image_optional_header	opt_hdr;
1038	image_import_descriptor imp_desc;
1039	ndis_unicode_string	dummystr;
1040	ndis_driver_object	drv;
1041        ndis_miniport_block     *block;
1042	ndis_status		status;
1043	int			idx;
1044	uint32_t		*ptr;
1045	struct ndis_softc	*sc;
1046
1047	sc = arg;
1048
1049	/* Perform text relocation */
1050	if (pe_relocate(img))
1051		return(ENOEXEC);
1052
1053        /* Dynamically link the NDIS.SYS routines -- required. */
1054	if (pe_patch_imports(img, "NDIS", ndis_functbl))
1055		return(ENOEXEC);
1056
1057	/* Dynamically link the HAL.dll routines -- also required. */
1058	if (pe_patch_imports(img, "HAL", hal_functbl))
1059		return(ENOEXEC);
1060
1061	/* Dynamically link ntoskrnl.exe -- optional. */
1062	if (pe_get_import_descriptor(img, &imp_desc, "ntoskrnl") == 0) {
1063		if (pe_patch_imports(img, "ntoskrnl", ntoskrnl_functbl))
1064			return(ENOEXEC);
1065	}
1066
1067	/* Initialize subsystems */
1068	ndis_libinit();
1069	ntoskrnl_libinit();
1070
1071        /* Locate the driver entry point */
1072	pe_get_optional_header(img, &opt_hdr);
1073	entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr);
1074
1075	/*
1076	 * Now call the DriverEntry() routine. This will cause
1077	 * a callout to the NdisInitializeWrapper() and
1078	 * NdisMRegisterMiniport() routines.
1079	 */
1080	dummystr.nus_len = strlen(NDIS_DUMMY_PATH);
1081	dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH);
1082	dummystr.nus_buf = NULL;
1083	ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf);
1084	drv.ndo_ifname = "ndis0";
1085
1086	status = entry(&drv, &dummystr);
1087
1088	free (dummystr.nus_buf, M_DEVBUF);
1089
1090	if (status != NDIS_STATUS_SUCCESS)
1091		return(ENODEV);
1092
1093	/*
1094	 * Now that we have the miniport driver characteristics,
1095	 * create an NDIS block and call the init handler.
1096	 * This will cause the driver to try to probe for
1097	 * a device.
1098	 */
1099
1100	block = &sc->ndis_block;
1101	bcopy((char *)&drv.ndo_chars, (char *)&sc->ndis_chars,
1102	    sizeof(ndis_miniport_characteristics));
1103
1104	/*block->nmb_signature = 0xcafebabe;*/
1105
1106		ptr = (uint32_t *)block;
1107	for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) {
1108		*ptr = idx | 0xdead0000;
1109		ptr++;
1110	}
1111
1112	block->nmb_signature = (void *)0xcafebabe;
1113	block->nmb_setdone_func = ndis_setdone_func;
1114	block->nmb_querydone_func = ndis_getdone_func;
1115	block->nmb_status_func = ndis_status_func;
1116	block->nmb_statusdone_func = ndis_statusdone_func;
1117	block->nmb_resetdone_func = ndis_resetdone_func;
1118
1119	block->nmb_ifp = &sc->arpcom.ac_if;
1120	block->nmb_dev = sc->ndis_dev;
1121
1122	return(0);
1123}
1124