efinet.c revision 93409
15507SN/A/*-
215812Sihse * Copyright (c) 2001 Doug Rabson
35507SN/A * All rights reserved.
45507SN/A *
55507SN/A * Redistribution and use in source and binary forms, with or without
65507SN/A * modification, are permitted provided that the following conditions
75507SN/A * are met:
85507SN/A * 1. Redistributions of source code must retain the above copyright
95507SN/A *    notice, this list of conditions and the following disclaimer.
105507SN/A * 2. Redistributions in binary form must reproduce the above copyright
115507SN/A *    notice, this list of conditions and the following disclaimer in the
125507SN/A *    documentation and/or other materials provided with the distribution.
135507SN/A *
145507SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
155507SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165507SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175507SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
185507SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195507SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205507SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215507SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225507SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235507SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245507SN/A * SUCH DAMAGE.
255507SN/A *
268330SN/A * $FreeBSD: head/sys/boot/efi/libefi/efinet.c 93409 2002-03-30 04:50:52Z marcel $
275507SN/A */
2810444Schegar
2910967Schegar#include <sys/param.h>
3013471Serikj#include <netinet/in.h>
315507SN/A#include <netinet/in_systm.h>
325507SN/A
3313823Serikj#include <stand.h>
345507SN/A#include <net.h>
3510967Schegar#include <netif.h>
36
37#include <efi.h>
38#include <efilib.h>
39
40extern struct netif_driver efi_net;
41
42#ifdef EFINET_DEBUG
43static void
44dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
45{
46	int i;
47
48	printf("State                 = %x\n", mode->State);
49	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
50	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
51	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
52	printf("NvRamSize             = %u\n", mode->NvRamSize);
53	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
54	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
55	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
56	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
57	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
58	printf("MCastFilter           = {");
59	for (i = 0; i < mode->MCastFilterCount; i++)
60		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
61	printf(" }\n");
62	printf("CurrentAddress        = %s\n",
63	    ether_sprintf(mode->CurrentAddress.Addr));
64	printf("BroadcastAddress      = %s\n",
65	    ether_sprintf(mode->BroadcastAddress.Addr));
66	printf("PermanentAddress      = %s\n",
67	    ether_sprintf(mode->PermanentAddress.Addr));
68	printf("IfType                = %u\n", mode->IfType);
69	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
70	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
71	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
72	printf("MediaPresent          = %d\n", mode->MediaPresent);
73}
74#endif
75
76int
77efinet_match(struct netif *nif, void *machdep_hint)
78{
79
80	return (1);
81}
82
83int
84efinet_probe(struct netif *nif, void *machdep_hint)
85{
86
87	return (0);
88}
89
90int
91efinet_put(struct iodesc *desc, void *pkt, size_t len)
92{
93	struct netif *nif = desc->io_netif;
94	EFI_SIMPLE_NETWORK *net;
95	EFI_STATUS status;
96	void *buf;
97
98	net = nif->nif_devdata;
99
100	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
101	if (status != EFI_SUCCESS)
102		return -1;
103
104	/* Wait for the buffer to be transmitted */
105	buf = 0;	/* XXX Is this needed? */
106	do {
107		status = net->GetStatus(net, 0, &buf);
108	} while (status == EFI_SUCCESS && buf != pkt);
109
110	/* XXX How do we deal with status != EFI_SUCCESS now? */
111	return (status == EFI_SUCCESS) ? len : -1;
112}
113
114
115int
116efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
117{
118	struct netif *nif = desc->io_netif;
119	EFI_SIMPLE_NETWORK *net;
120	EFI_STATUS status;
121	UINTN bufsz;
122	time_t t;
123
124	net = nif->nif_devdata;
125
126	t = time(0);
127	while ((time(0) - t) < timeout) {
128		bufsz = len;
129		status = net->Receive(net, 0, &bufsz, pkt, 0, 0, 0);
130		if (status == EFI_SUCCESS)
131			return bufsz;
132		if (status != EFI_NOT_READY)
133			return 0;
134	}
135
136	return 0;
137}
138
139void
140efinet_init(struct iodesc *desc, void *machdep_hint)
141{
142	struct netif *nif = desc->io_netif;
143	EFI_SIMPLE_NETWORK *net;
144	EFI_STATUS status;
145
146	net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
147	nif->nif_devdata = net;
148
149	if (net->Mode->State == EfiSimpleNetworkStopped) {
150		status = net->Start(net);
151		if (status != EFI_SUCCESS) {
152			printf("net%d: cannot start interface (status=%d)\n",
153			    nif->nif_unit, status);
154			return;
155		}
156	}
157
158	if (net->Mode->State != EfiSimpleNetworkInitialized) {
159		status = net->Initialize(net, 0, 0);
160		if (status != EFI_SUCCESS) {
161			printf("net%d: cannot init. interface (status=%d)\n",
162			    nif->nif_unit, status);
163			return;
164		}
165	}
166
167	if (net->Mode->ReceiveFilterSetting == 0) {
168		UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
169		    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
170
171		status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
172		if (status != EFI_SUCCESS) {
173			printf("net%d: cannot set rx. filters (status=%d)\n",
174			    nif->nif_unit, status);
175			return;
176		}
177	}
178
179#ifdef EFINET_DEBUG
180	dump_mode(net->Mode);
181#endif
182
183	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
184	desc->xid = 1;
185
186	return;
187}
188
189void
190efinet_init_driver()
191{
192	EFI_STATUS	status;
193	UINTN		sz;
194	static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
195	EFI_HANDLE	*handles;
196	int		nifs, i;
197#define MAX_INTERFACES	4
198	static struct netif_dif difs[MAX_INTERFACES];
199	static struct netif_stats stats[MAX_INTERFACES];
200
201	sz = 0;
202	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
203	if (status != EFI_BUFFER_TOO_SMALL)
204		return;
205	handles = (EFI_HANDLE *) malloc(sz);
206	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
207	if (EFI_ERROR(status)) {
208		free(handles);
209		return;
210	}
211
212	nifs = sz / sizeof(EFI_HANDLE);
213	if (nifs > MAX_INTERFACES)
214		nifs = MAX_INTERFACES;
215
216	efi_net.netif_nifs = nifs;
217	efi_net.netif_ifs = difs;
218
219	bzero(stats, sizeof(stats));
220	for (i = 0; i < nifs; i++) {
221		struct netif_dif *dif = &efi_net.netif_ifs[i];
222		dif->dif_unit = i;
223		dif->dif_nsel = 1;
224		dif->dif_stats = &stats[i];
225
226		BS->HandleProtocol(handles[i], &netid,
227				   (VOID**) &dif->dif_private);
228	}
229
230	return;
231}
232
233void
234efinet_end(struct netif *nif)
235{
236	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
237
238	net->Shutdown(net);
239}
240
241struct netif_driver efi_net = {
242	"net",			/* netif_bname */
243	efinet_match,		/* netif_match */
244	efinet_probe,		/* netif_probe */
245	efinet_init,		/* netif_init */
246	efinet_get,		/* netif_get */
247	efinet_put,		/* netif_put */
248	efinet_end,		/* netif_end */
249	0,			/* netif_ifs */
250	0			/* netif_nifs */
251};
252
253