xref: /netbsd-src/sys/arch/ia64/stand/efi/libefi/efinet.c (revision db0fbdd6ce1ca32d83f7d5dd5064142dbb3934a9)
1 /*	$NetBSD: efinet.c,v 1.8 2018/08/18 15:55:19 kre Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/efinet.c,v 1.6 2004/01/04 23:28:16 obrien Exp $"); */
31 
32 #include <sys/param.h>
33 
34 #include <lib/libsa/stand.h>
35 #include <lib/libsa/loadfile.h>
36 #include <lib/libsa/net.h>
37 #include <lib/libsa/netif.h>
38 
39 #ifdef EFINET_DEBUG
40 #include <lib/libsa/ether_sprintf.c>
41 #endif
42 
43 #include <efi.h>
44 #include <efilib.h>
45 
46 extern struct netif_driver efi_net;
47 
48 int  efinet_match(struct netif *, void *);
49 int  efinet_probe(struct netif *, void *);
50 void efinet_init(struct iodesc *, void *);
51 int  efinet_get(struct iodesc *, void *, size_t, saseconds_t);
52 int  efinet_put(struct iodesc *, void *, size_t);
53 void efinet_end(struct netif *);
54 
55 struct netif_driver efi_net = {
56 	"net",			/* netif_bname */
57 	efinet_match,		/* netif_match */
58 	efinet_probe,		/* netif_probe */
59 	efinet_init,		/* netif_init */
60 	efinet_get,		/* netif_get */
61 	efinet_put,		/* netif_put */
62 	efinet_end,		/* netif_end */
63 	0,			/* netif_ifs */
64 	0			/* netif_nifs */
65 };
66 
67 #ifdef EFINET_DEBUG
68 static void
dump_mode(EFI_SIMPLE_NETWORK_MODE * mode)69 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
70 {
71 	int i;
72 
73 	printf("State                 = %x\n", mode->State);
74 	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
75 	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
76 	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
77 	printf("NvRamSize             = %u\n", mode->NvRamSize);
78 	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
79 	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
80 	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
81 	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
82 	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
83 	printf("MCastFilter           = {");
84 	for (i = 0; i < mode->MCastFilterCount; i++)
85 		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
86 	printf(" }\n");
87 	printf("CurrentAddress        = %s\n",
88 	    ether_sprintf(mode->CurrentAddress.Addr));
89 	printf("BroadcastAddress      = %s\n",
90 	    ether_sprintf(mode->BroadcastAddress.Addr));
91 	printf("PermanentAddress      = %s\n",
92 	    ether_sprintf(mode->PermanentAddress.Addr));
93 	printf("IfType                = %u\n", mode->IfType);
94 	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
95 	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
96 	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
97 	printf("MediaPresent          = %d\n", mode->MediaPresent);
98 }
99 #endif
100 
101 int
efinet_match(struct netif * nif,void * machdep_hint)102 efinet_match(struct netif *nif, void *machdep_hint)
103 {
104 
105 	return (1);
106 }
107 
108 int
efinet_probe(struct netif * nif,void * machdep_hint)109 efinet_probe(struct netif *nif, void *machdep_hint)
110 {
111 
112 	return (0);
113 }
114 
115 int
efinet_put(struct iodesc * desc,void * pkt,size_t len)116 efinet_put(struct iodesc *desc, void *pkt, size_t len)
117 {
118 	struct netif *nif = desc->io_netif;
119 	EFI_SIMPLE_NETWORK *net;
120 	EFI_STATUS status;
121 	void *buf;
122 
123 	net = nif->nif_devdata;
124 
125 	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
126 	if (status != EFI_SUCCESS)
127 		return -1;
128 
129 	/* Wait for the buffer to be transmitted */
130 	do {
131 		buf = 0;	/* XXX Is this needed? */
132 		status = net->GetStatus(net, 0, &buf);
133 		/*
134 		 * XXX EFI1.1 and the E1000 card returns a different
135 		 * address than we gave.  Sigh.
136 		 */
137 	} while (status == EFI_SUCCESS && buf == 0);
138 
139 	/* XXX How do we deal with status != EFI_SUCCESS now? */
140 	return (status == EFI_SUCCESS) ? len : -1;
141 }
142 
143 
144 int
efinet_get(struct iodesc * desc,void * pkt,size_t len,saseconds_t timeout)145 efinet_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
146 {
147 	struct netif *nif = desc->io_netif;
148 	EFI_SIMPLE_NETWORK *net;
149 	EFI_STATUS status;
150 	UINTN bufsz;
151 	time_t t;
152 	char buf[2048];
153 
154 	net = nif->nif_devdata;
155 
156 	t = time(0);
157 	while ((time(0) - t) < timeout) {
158 		bufsz = sizeof(buf);
159 		status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0);
160 		if (status == EFI_SUCCESS) {
161 			/*
162 			 * XXX EFI1.1 and the E1000 card trash our
163 			 * workspace if we do not do this silly copy.
164 			 * Either they are not respecting the len
165 			 * value or do not like the alignment.
166 			 */
167 			if (bufsz > len)
168 				bufsz = len;
169 			memcpy(pkt, buf, bufsz);
170 			return bufsz;
171 		}
172 		if (status != EFI_NOT_READY)
173 			return 0;
174 	}
175 
176 	return 0;
177 }
178 
179 void
efinet_init(struct iodesc * desc,void * machdep_hint)180 efinet_init(struct iodesc *desc, void *machdep_hint)
181 {
182 	struct netif *nif = desc->io_netif;
183 	EFI_SIMPLE_NETWORK *net;
184 	EFI_STATUS status;
185 
186 	net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
187 	nif->nif_devdata = net;
188 
189 	if (net->Mode->State == EfiSimpleNetworkStopped) {
190 		status = net->Start(net);
191 		if (status != EFI_SUCCESS) {
192 			printf("net%d: cannot start interface (status=%ld)\n",
193 			    nif->nif_unit, status);
194 			return;
195 		}
196 	}
197 
198 	if (net->Mode->State != EfiSimpleNetworkInitialized) {
199 		status = net->Initialize(net, 0, 0);
200 		if (status != EFI_SUCCESS) {
201 			printf("net%d: cannot init. interface (status=%ld)\n",
202 			    nif->nif_unit, status);
203 			return;
204 		}
205 	}
206 
207 	if (net->Mode->ReceiveFilterSetting == 0) {
208 		UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
209 		    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
210 
211 		status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
212 		if (status != EFI_SUCCESS) {
213 			printf("net%d: cannot set rx. filters (status=%ld)\n",
214 			    nif->nif_unit, status);
215 			return;
216 		}
217 	}
218 
219 #ifdef EFINET_DEBUG
220 	dump_mode(net->Mode);
221 #endif
222 
223 	memcpy(desc->myea, net->Mode->CurrentAddress.Addr, 6);
224 	desc->xid = 1;
225 
226 	return;
227 }
228 
229 void
efinet_init_driver(void)230 efinet_init_driver(void)
231 {
232 	EFI_STATUS	status;
233 	UINTN		sz;
234 	static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
235 	EFI_HANDLE	*handles;
236 	int		nifs, i;
237 #define MAX_INTERFACES	4
238 	static struct netif_dif difs[MAX_INTERFACES];
239 	static struct netif_stats stats[MAX_INTERFACES];
240 
241 	sz = 0;
242 	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
243 	if (status != EFI_BUFFER_TOO_SMALL)
244 		return;
245 	handles = (EFI_HANDLE *) alloc(sz);
246 	status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
247 	if (EFI_ERROR(status)) {
248                 free(handles);
249 		return;
250 	}
251 
252 	nifs = sz / sizeof(EFI_HANDLE);
253 	if (nifs > MAX_INTERFACES)
254 		nifs = MAX_INTERFACES;
255 
256 	efi_net.netif_nifs = nifs;
257 	efi_net.netif_ifs = difs;
258 
259 	memset(stats, 0, sizeof(stats));
260 	for (i = 0; i < nifs; i++) {
261 		struct netif_dif *dif = &efi_net.netif_ifs[i];
262 		dif->dif_unit = i;
263 		dif->dif_nsel = 1;
264 		dif->dif_stats = &stats[i];
265 
266 		BS->HandleProtocol(handles[i], &netid,
267 				   (VOID**) &dif->dif_private);
268 	}
269 
270 	return;
271 }
272 
273 void
efinet_end(struct netif * nif)274 efinet_end(struct netif *nif)
275 {
276 	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
277 
278 	net->Shutdown(net);
279 }
280