efinet.c revision 83828
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 83828 2001-09-22 19:10:56Z dfr $
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	EFI_SIMPLE_NETWORK *net = desc->io_netif->nif_devdata;
60	EFI_STATUS status;
61	int i;
62
63	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
64	if (!EFI_ERROR(status))
65		return len;
66	else
67		return -1;
68}
69
70
71int
72efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
73{
74	EFI_SIMPLE_NETWORK *net = desc->io_netif->nif_devdata;
75	EFI_STATUS status;
76	UINTN bufsz;
77	time_t t;
78
79	t = time(0);
80	while ((time(0) - t) < timeout) {
81		bufsz = len;
82		status = net->Receive(net, 0, &bufsz, pkt, 0, 0, 0);
83		if (status == EFI_SUCCESS)
84			return bufsz;
85		if (status != EFI_NOT_READY)
86			return 0;
87	}
88
89	return 0;
90}
91
92void
93efinet_init(struct iodesc *desc, void *machdep_hint)
94{
95	struct netif		*nif;
96	EFI_SIMPLE_NETWORK	*net;
97	int			i;
98
99	nif = desc->io_netif;
100	net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
101	desc->io_netif->nif_devdata = net;
102
103	net->Start(net);
104	net->Initialize(net, 0, 0);
105
106	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
107	desc->xid = 1;
108
109	return;
110}
111
112void
113efinet_init_driver()
114{
115	EFI_STATUS	status;
116	UINTN		sz;
117	static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
118	EFI_HANDLE	*handles;
119	int		nifs, i;
120#define MAX_INTERFACES	4
121	static struct netif_dif difs[MAX_INTERFACES];
122	static struct netif_stats stats[MAX_INTERFACES];
123
124	sz = 0;
125	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
126	if (status != EFI_BUFFER_TOO_SMALL)
127		return;
128	handles = (EFI_HANDLE *) malloc(sz);
129	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
130	if (EFI_ERROR(status)) {
131		free(handles);
132		return;
133	}
134
135	nifs = sz / sizeof(EFI_HANDLE);
136	if (nifs > MAX_INTERFACES)
137		nifs = MAX_INTERFACES;
138
139	efi_net.netif_nifs = nifs;
140	efi_net.netif_ifs = difs;
141
142	bzero(stats, sizeof(stats));
143	for (i = 0; i < nifs; i++) {
144		struct netif_dif *dif = &efi_net.netif_ifs[i];
145		dif->dif_unit = i;
146		dif->dif_nsel = 1;
147		dif->dif_stats = &stats[i];
148		dif->dif_private = handles[i];
149
150		BS->HandleProtocol(handles[i], &netid,
151				   (VOID**) &dif->dif_private);
152	}
153
154	return;
155}
156
157void
158efinet_end(struct netif *nif)
159{
160	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
161
162	net->Shutdown(net);
163}
164
165struct netif_driver efi_net = {
166	"net",			/* netif_bname */
167	efinet_match,		/* netif_match */
168	efinet_probe,		/* netif_probe */
169	efinet_init,		/* netif_init */
170	efinet_get,		/* netif_get */
171	efinet_put,		/* netif_put */
172	efinet_end,		/* netif_end */
173	0,			/* netif_ifs */
174	0			/* netif_nifs */
175};
176
177