efinet.c revision 93403
1/*-
2 * Copyright (c) 2001 Doug Rabson
3 * 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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/boot/efi/libefi/efinet.c 93403 2002-03-29 23:10:15Z marcel $
27 */
28
29#include <sys/param.h>
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32
33#include <stand.h>
34#include <net.h>
35#include <netif.h>
36
37#include <efi.h>
38#include <efilib.h>
39
40extern struct netif_driver efi_net;
41
42int
43efinet_match(struct netif *nif, void *machdep_hint)
44{
45
46	return (1);
47}
48
49int
50efinet_probe(struct netif *nif, void *machdep_hint)
51{
52
53	return (0);
54}
55
56int
57efinet_put(struct iodesc *desc, void *pkt, size_t len)
58{
59	struct netif *nif = desc->io_netif;
60	EFI_SIMPLE_NETWORK *net;
61	EFI_STATUS status;
62	void *buf;
63
64	net = nif->nif_devdata;
65
66	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
67	if (status != EFI_SUCCESS)
68		return -1;
69
70	/* Wait for the buffer to be transmitted */
71	buf = 0;	/* XXX Is this needed? */
72	do {
73		status = net->GetStatus(net, 0, &buf);
74	} while (status == EFI_SUCCESS && buf != pkt);
75
76	/* XXX How do we deal with status != EFI_SUCCESS now? */
77	return (status == EFI_SUCCESS) ? len : -1;
78}
79
80
81int
82efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
83{
84	struct netif *nif = desc->io_netif;
85	EFI_SIMPLE_NETWORK *net;
86	EFI_STATUS status;
87	UINTN bufsz;
88	time_t t;
89
90	net = nif->nif_devdata;
91
92	t = time(0);
93	while ((time(0) - t) < timeout) {
94		bufsz = len;
95		status = net->Receive(net, 0, &bufsz, pkt, 0, 0, 0);
96		if (status == EFI_SUCCESS)
97			return bufsz;
98		if (status != EFI_NOT_READY)
99			return 0;
100	}
101
102	return 0;
103}
104
105void
106efinet_init(struct iodesc *desc, void *machdep_hint)
107{
108	struct netif *nif = desc->io_netif;
109	EFI_SIMPLE_NETWORK *net;
110
111	net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
112	nif->nif_devdata = net;
113
114	net->Start(net);
115	net->Initialize(net, 0, 0);
116
117	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
118	desc->xid = 1;
119
120	return;
121}
122
123void
124efinet_init_driver()
125{
126	EFI_STATUS	status;
127	UINTN		sz;
128	static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
129	EFI_HANDLE	*handles;
130	int		nifs, i;
131#define MAX_INTERFACES	4
132	static struct netif_dif difs[MAX_INTERFACES];
133	static struct netif_stats stats[MAX_INTERFACES];
134
135	sz = 0;
136	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
137	if (status != EFI_BUFFER_TOO_SMALL)
138		return;
139	handles = (EFI_HANDLE *) malloc(sz);
140	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
141	if (EFI_ERROR(status)) {
142		free(handles);
143		return;
144	}
145
146	nifs = sz / sizeof(EFI_HANDLE);
147	if (nifs > MAX_INTERFACES)
148		nifs = MAX_INTERFACES;
149
150	efi_net.netif_nifs = nifs;
151	efi_net.netif_ifs = difs;
152
153	bzero(stats, sizeof(stats));
154	for (i = 0; i < nifs; i++) {
155		struct netif_dif *dif = &efi_net.netif_ifs[i];
156		dif->dif_unit = i;
157		dif->dif_nsel = 1;
158		dif->dif_stats = &stats[i];
159
160		BS->HandleProtocol(handles[i], &netid,
161				   (VOID**) &dif->dif_private);
162	}
163
164	return;
165}
166
167void
168efinet_end(struct netif *nif)
169{
170	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
171
172	net->Shutdown(net);
173}
174
175struct netif_driver efi_net = {
176	"net",			/* netif_bname */
177	efinet_match,		/* netif_match */
178	efinet_probe,		/* netif_probe */
179	efinet_init,		/* netif_init */
180	efinet_get,		/* netif_get */
181	efinet_put,		/* netif_put */
182	efinet_end,		/* netif_end */
183	0,			/* netif_ifs */
184	0			/* netif_nifs */
185};
186
187