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