xref: /dragonfly/stand/boot/efi/libefi/efinet.c (revision 655933d6)
1 /*-
2  * Copyright (c) 2001 Doug Rabson
3  * Copyright (c) 2002, 2006 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: head/sys/boot/efi/libefi/efinet.c 295210 2016-02-03 14:34:25Z andrew $
28  */
29 
30 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 
34 #include <stand.h>
35 #include <stdarg.h>
36 #include <net.h>
37 #include <netif.h>
38 
39 #include <dev_net.c>
40 
41 #include <efi.h>
42 #include <efilib.h>
43 
44 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
45 
46 static void efinet_end(struct netif *);
47 static int efinet_get(struct iodesc *, void *, size_t, time_t);
48 static void efinet_init(struct iodesc *, void *);
49 static int efinet_match(struct netif *, void *);
50 static int efinet_probe(struct netif *, void *);
51 static int efinet_put(struct iodesc *, void *, size_t);
52 
53 struct netif_driver efinetif = {
54 	.netif_bname = "efinet",
55 	.netif_match = efinet_match,
56 	.netif_probe = efinet_probe,
57 	.netif_init = efinet_init,
58 	.netif_get = efinet_get,
59 	.netif_put = efinet_put,
60 	.netif_end = efinet_end,
61 	.netif_ifs = NULL,
62 	.netif_nifs = 0
63 };
64 
65 #ifdef EFINET_DEBUG
66 static void
67 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
68 {
69 	int i;
70 
71 	printf("State                 = %x\n", mode->State);
72 	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
73 	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
74 	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
75 	printf("NvRamSize             = %u\n", mode->NvRamSize);
76 	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
77 	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
78 	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
79 	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
80 	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
81 	printf("MCastFilter           = {");
82 	for (i = 0; i < mode->MCastFilterCount; i++)
83 		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
84 	printf(" }\n");
85 	printf("CurrentAddress        = %s\n",
86 	    ether_sprintf(mode->CurrentAddress.Addr));
87 	printf("BroadcastAddress      = %s\n",
88 	    ether_sprintf(mode->BroadcastAddress.Addr));
89 	printf("PermanentAddress      = %s\n",
90 	    ether_sprintf(mode->PermanentAddress.Addr));
91 	printf("IfType                = %u\n", mode->IfType);
92 	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
93 	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
94 	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
95 	printf("MediaPresent          = %d\n", mode->MediaPresent);
96 }
97 #endif
98 
99 static int
100 efinet_match(struct netif *nif, void *machdep_hint)
101 {
102 	struct efi_devdesc *dev = machdep_hint;
103 
104 	if (dev->d_kind.efidisk.unit - 1 == nif->nif_unit)
105 		return (1);
106 	return(0);
107 }
108 
109 static int
110 efinet_probe(struct netif *nif, void *machdep_hint)
111 {
112 
113 	return (0);
114 }
115 
116 static int
117 efinet_put(struct iodesc *desc, void *pkt, size_t len)
118 {
119 	struct netif *nif = desc->io_netif;
120 	EFI_SIMPLE_NETWORK *net;
121 	EFI_STATUS status;
122 	void *buf;
123 
124 	net = nif->nif_devdata;
125 
126 	status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
127 	if (status != EFI_SUCCESS)
128 		return (-1);
129 
130 	/* Wait for the buffer to be transmitted */
131 	do {
132 		buf = NULL;	/* XXX Is this needed? */
133 		status = net->GetStatus(net, 0, &buf);
134 		/*
135 		 * XXX EFI1.1 and the E1000 card returns a different
136 		 * address than we gave.  Sigh.
137 		 */
138 	} while (status == EFI_SUCCESS && buf == NULL);
139 
140 	/* XXX How do we deal with status != EFI_SUCCESS now? */
141 	return ((status == EFI_SUCCESS) ? len : -1);
142 }
143 
144 static int
145 efinet_get(struct iodesc *desc, void *pkt, size_t len, time_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 			bcopy(buf, pkt, bufsz);
170 			return (bufsz);
171 		}
172 		if (status != EFI_NOT_READY)
173 			return (0);
174 	}
175 
176 	return (0);
177 }
178 
179 static void
180 efinet_init(struct iodesc *desc, void *machdep_hint)
181 {
182 	struct netif *nif = desc->io_netif;
183 	EFI_SIMPLE_NETWORK *net;
184 	EFI_HANDLE h;
185 	EFI_STATUS status;
186 
187 	if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
188 		printf("Invalid network interface %d\n", nif->nif_unit);
189 		return;
190 	}
191 
192 	h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
193 	status = OpenProtocolByHandle(h, &sn_guid, (VOID **)&nif->nif_devdata);
194 	if (status != EFI_SUCCESS) {
195 		printf("net%d: cannot start interface (status=%llu)\n",
196 		    nif->nif_unit, status);
197 		return;
198 	}
199 
200 	net = nif->nif_devdata;
201 	if (net->Mode->State == EfiSimpleNetworkStopped) {
202 		status = net->Start(net);
203 		if (status != EFI_SUCCESS) {
204 			printf("net%d: cannot start interface (status=%llu)\n",
205 			    nif->nif_unit, status);
206 			return;
207 		}
208 	}
209 
210 	if (net->Mode->State != EfiSimpleNetworkInitialized) {
211 		status = net->Initialize(net, 0, 0);
212 		if (status != EFI_SUCCESS) {
213 			printf("net%d: cannot init. interface (status=%llu)\n",
214 			    nif->nif_unit, status);
215 			return;
216 		}
217 	}
218 
219 	if (net->Mode->ReceiveFilterSetting == 0) {
220 		UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
221 		    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
222 
223 		status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
224 		if (status != EFI_SUCCESS) {
225 			printf("net%d: cannot set rx. filters (status=%llu)\n",
226 			    nif->nif_unit, status);
227 			return;
228 		}
229 	}
230 
231 #ifdef EFINET_DEBUG
232 	dump_mode(net->Mode);
233 #endif
234 
235 	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
236 	desc->xid = 1;
237 }
238 
239 static void
240 efinet_end(struct netif *nif)
241 {
242 	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
243 
244 	net->Shutdown(net);
245 }
246 
247 static int efinet_dev_init(void);
248 static void efinet_dev_print(int);
249 
250 struct devsw efinet_dev = {
251 	.dv_name = "net",
252 	.dv_type = DEVT_NET,
253 	.dv_init = efinet_dev_init,
254 	.dv_strategy = net_strategy,
255 	.dv_open = net_open,
256 	.dv_close = net_close,
257 	.dv_ioctl = noioctl,
258 	.dv_print = efinet_dev_print,
259 	.dv_cleanup = NULL
260 };
261 
262 static int
263 efinet_dev_init(void)
264 {
265 	struct netif_dif *dif;
266 	struct netif_stats *stats;
267 	EFI_HANDLE *handles;
268 	EFI_STATUS status;
269 	UINTN sz;
270 	int err, i, nifs;
271 
272 	sz = 0;
273 	handles = NULL;
274 	status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 0);
275 	if (status == EFI_BUFFER_TOO_SMALL) {
276 		handles = (EFI_HANDLE *)malloc(sz);
277 		status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz,
278 		    handles);
279 		if (EFI_ERROR(status))
280 			free(handles);
281 	}
282 	if (EFI_ERROR(status))
283 		return (efi_status_to_errno(status));
284 	nifs = sz / sizeof(EFI_HANDLE);
285 	err = efi_register_handles(&efinet_dev, handles, NULL, nifs);
286 	free(handles);
287 	if (err != 0)
288 		return (err);
289 
290 	efinetif.netif_nifs = nifs;
291 	efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
292 
293 	stats = calloc(nifs, sizeof(struct netif_stats));
294 
295 	for (i = 0; i < nifs; i++) {
296 		EFI_SIMPLE_NETWORK *net;
297 		EFI_HANDLE h;
298 
299 		dif = &efinetif.netif_ifs[i];
300 		dif->dif_unit = -1;
301 
302 		h = efi_find_handle(&efinet_dev, i);
303 
304 		/*
305 		 * Open the network device in exclusive mode. Without this
306 		 * we will be racing with the UEFI network stack. It will
307 		 * pull packets off the network leading to lost packets.
308 		 */
309 		status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
310 		    IH, 0, EFI_OPEN_PROTOCOL_EXCLUSIVE);
311 		if (status != EFI_SUCCESS) {
312 			printf("Unable to open network interface %d for "
313 			    "exclusive access\n", i);
314 		}
315 
316 		dif->dif_unit = i;
317 		dif->dif_nsel = 1;
318 		dif->dif_stats = &stats[i];
319 		dif->dif_private = h;
320 	}
321 
322 	return (0);
323 }
324 
325 static void
326 efinet_dev_print(int verbose)
327 {
328 	char line[80];
329 	EFI_HANDLE h;
330 	int unit;
331 
332 	for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
333 	    h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
334 		sprintf(line, "    %s%d:\n", efinet_dev.dv_name, unit);
335 		pager_output(line);
336 	}
337 }
338