efinet.c revision 332156
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
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/11/stand/efi/libefi/efinet.c 332156 2018-04-06 21:50:09Z kevans $");
30
31#include <sys/param.h>
32#include <net/ethernet.h>
33#include <netinet/in.h>
34#include <netinet/in_systm.h>
35
36#include <stand.h>
37#include <net.h>
38#include <netif.h>
39
40#include <efi.h>
41#include <efilib.h>
42
43static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
44
45static void efinet_end(struct netif *);
46static ssize_t efinet_get(struct iodesc *, void **, time_t);
47static void efinet_init(struct iodesc *, void *);
48static int efinet_match(struct netif *, void *);
49static int efinet_probe(struct netif *, void *);
50static ssize_t efinet_put(struct iodesc *, void *, size_t);
51
52struct netif_driver efinetif = {
53	.netif_bname = "efinet",
54	.netif_match = efinet_match,
55	.netif_probe = efinet_probe,
56	.netif_init = efinet_init,
57	.netif_get = efinet_get,
58	.netif_put = efinet_put,
59	.netif_end = efinet_end,
60	.netif_ifs = NULL,
61	.netif_nifs = 0
62};
63
64#ifdef EFINET_DEBUG
65static void
66dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
67{
68	int i;
69
70	printf("State                 = %x\n", mode->State);
71	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
72	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
73	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
74	printf("NvRamSize             = %u\n", mode->NvRamSize);
75	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
76	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
77	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
78	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
79	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
80	printf("MCastFilter           = {");
81	for (i = 0; i < mode->MCastFilterCount; i++)
82		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
83	printf(" }\n");
84	printf("CurrentAddress        = %s\n",
85	    ether_sprintf(mode->CurrentAddress.Addr));
86	printf("BroadcastAddress      = %s\n",
87	    ether_sprintf(mode->BroadcastAddress.Addr));
88	printf("PermanentAddress      = %s\n",
89	    ether_sprintf(mode->PermanentAddress.Addr));
90	printf("IfType                = %u\n", mode->IfType);
91	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
92	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
93	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
94	printf("MediaPresent          = %d\n", mode->MediaPresent);
95}
96#endif
97
98static int
99efinet_match(struct netif *nif, void *machdep_hint)
100{
101	struct devdesc *dev = machdep_hint;
102
103	if (dev->d_unit == nif->nif_unit)
104		return (1);
105	return(0);
106}
107
108static int
109efinet_probe(struct netif *nif, void *machdep_hint)
110{
111
112	return (0);
113}
114
115static ssize_t
116efinet_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	if (net == NULL)
125		return (-1);
126
127	status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
128	if (status != EFI_SUCCESS)
129		return (-1);
130
131	/* Wait for the buffer to be transmitted */
132	do {
133		buf = NULL;	/* XXX Is this needed? */
134		status = net->GetStatus(net, NULL, &buf);
135		/*
136		 * XXX EFI1.1 and the E1000 card returns a different
137		 * address than we gave.  Sigh.
138		 */
139	} while (status == EFI_SUCCESS && buf == NULL);
140
141	/* XXX How do we deal with status != EFI_SUCCESS now? */
142	return ((status == EFI_SUCCESS) ? len : -1);
143}
144
145static ssize_t
146efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
147{
148	struct netif *nif = desc->io_netif;
149	EFI_SIMPLE_NETWORK *net;
150	EFI_STATUS status;
151	UINTN bufsz;
152	time_t t;
153	char *buf, *ptr;
154	ssize_t ret = -1;
155
156	net = nif->nif_devdata;
157	if (net == NULL)
158		return (ret);
159
160	bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
161	buf = malloc(bufsz + ETHER_ALIGN);
162	if (buf == NULL)
163		return (ret);
164	ptr = buf + ETHER_ALIGN;
165
166	t = getsecs();
167	while ((getsecs() - t) < timeout) {
168		status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
169		if (status == EFI_SUCCESS) {
170			*pkt = buf;
171			ret = (ssize_t)bufsz;
172			break;
173		}
174		if (status != EFI_NOT_READY)
175			break;
176	}
177
178	if (ret == -1)
179		free(buf);
180	return (ret);
181}
182
183static void
184efinet_init(struct iodesc *desc, void *machdep_hint)
185{
186	struct netif *nif = desc->io_netif;
187	EFI_SIMPLE_NETWORK *net;
188	EFI_HANDLE h;
189	EFI_STATUS status;
190	UINT32 mask;
191
192	if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
193		printf("Invalid network interface %d\n", nif->nif_unit);
194		return;
195	}
196
197	h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
198	status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata);
199	if (status != EFI_SUCCESS) {
200		printf("net%d: cannot fetch interface data (status=%lu)\n",
201		    nif->nif_unit, EFI_ERROR_CODE(status));
202		return;
203	}
204
205	net = nif->nif_devdata;
206	if (net->Mode->State == EfiSimpleNetworkStopped) {
207		status = net->Start(net);
208		if (status != EFI_SUCCESS) {
209			printf("net%d: cannot start interface (status=%lu)\n",
210			    nif->nif_unit, EFI_ERROR_CODE(status));
211			return;
212		}
213	}
214
215	if (net->Mode->State != EfiSimpleNetworkInitialized) {
216		status = net->Initialize(net, 0, 0);
217		if (status != EFI_SUCCESS) {
218			printf("net%d: cannot init. interface (status=%lu)\n",
219			    nif->nif_unit, EFI_ERROR_CODE(status));
220			return;
221		}
222	}
223
224	mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
225	    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
226
227	status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
228	if (status != EFI_SUCCESS)
229		printf("net%d: cannot set rx. filters (status=%lu)\n",
230		    nif->nif_unit, EFI_ERROR_CODE(status));
231
232#ifdef EFINET_DEBUG
233	dump_mode(net->Mode);
234#endif
235
236	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
237	desc->xid = 1;
238}
239
240static void
241efinet_end(struct netif *nif)
242{
243	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
244
245	if (net == NULL)
246		return;
247
248	net->Shutdown(net);
249}
250
251static int efinet_dev_init(void);
252static int efinet_dev_print(int);
253
254struct devsw efinet_dev = {
255	.dv_name = "net",
256	.dv_type = DEVT_NET,
257	.dv_init = efinet_dev_init,
258	.dv_strategy = NULL,		/* Will be set in efinet_dev_init */
259	.dv_open = NULL,		/* Will be set in efinet_dev_init */
260	.dv_close = NULL,		/* Will be set in efinet_dev_init */
261	.dv_ioctl = noioctl,
262	.dv_print = efinet_dev_print,
263	.dv_cleanup = NULL
264};
265
266static int
267efinet_dev_init()
268{
269	struct netif_dif *dif;
270	struct netif_stats *stats;
271	EFI_DEVICE_PATH *devpath, *node;
272	EFI_SIMPLE_NETWORK *net;
273	EFI_HANDLE *handles, *handles2;
274	EFI_STATUS status;
275	UINTN sz;
276	int err, i, nifs;
277	extern struct devsw netdev;
278
279	sz = 0;
280	handles = NULL;
281	status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
282	if (status == EFI_BUFFER_TOO_SMALL) {
283		handles = (EFI_HANDLE *)malloc(sz);
284		status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
285		    handles);
286		if (EFI_ERROR(status))
287			free(handles);
288	}
289	if (EFI_ERROR(status))
290		return (efi_status_to_errno(status));
291	handles2 = (EFI_HANDLE *)malloc(sz);
292	if (handles2 == NULL) {
293		free(handles);
294		return (ENOMEM);
295	}
296	nifs = 0;
297	for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
298		devpath = efi_lookup_devpath(handles[i]);
299		if (devpath == NULL)
300			continue;
301		if ((node = efi_devpath_last_node(devpath)) == NULL)
302			continue;
303
304		if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
305		    DevicePathSubType(node) != MSG_MAC_ADDR_DP)
306			continue;
307
308		/*
309		 * Open the network device in exclusive mode. Without this
310		 * we will be racing with the UEFI network stack. It will
311		 * pull packets off the network leading to lost packets.
312		 */
313		status = BS->OpenProtocol(handles[i], &sn_guid, (void **)&net,
314		    IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE);
315		if (status != EFI_SUCCESS) {
316			printf("Unable to open network interface %d for "
317			    "exclusive access: %lu\n", i,
318			    EFI_ERROR_CODE(status));
319		}
320
321		handles2[nifs] = handles[i];
322		nifs++;
323	}
324	free(handles);
325	if (nifs == 0) {
326		err = ENOENT;
327		goto done;
328	}
329
330	err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
331	if (err != 0)
332		goto done;
333
334	efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
335	stats = calloc(nifs, sizeof(struct netif_stats));
336	if (efinetif.netif_ifs == NULL || stats == NULL) {
337		free(efinetif.netif_ifs);
338		free(stats);
339		efinetif.netif_ifs = NULL;
340		err = ENOMEM;
341		goto done;
342	}
343	efinetif.netif_nifs = nifs;
344
345	for (i = 0; i < nifs; i++) {
346
347		dif = &efinetif.netif_ifs[i];
348		dif->dif_unit = i;
349		dif->dif_nsel = 1;
350		dif->dif_stats = &stats[i];
351		dif->dif_private = handles2[i];
352	}
353
354	efinet_dev.dv_open = netdev.dv_open;
355	efinet_dev.dv_close = netdev.dv_close;
356	efinet_dev.dv_strategy = netdev.dv_strategy;
357
358done:
359	free(handles2);
360	return (err);
361}
362
363static int
364efinet_dev_print(int verbose)
365{
366	CHAR16 *text;
367	EFI_HANDLE h;
368	int unit, ret = 0;
369
370	printf("%s devices:", efinet_dev.dv_name);
371	if ((ret = pager_output("\n")) != 0)
372		return (ret);
373
374	for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
375	    h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
376		printf("    %s%d:", efinet_dev.dv_name, unit);
377		if (verbose) {
378			text = efi_devpath_name(efi_lookup_devpath(h));
379			if (text != NULL) {
380				printf("    %S", text);
381				efi_free_devpath_name(text);
382			}
383		}
384		if ((ret = pager_output("\n")) != 0)
385			break;
386	}
387	return (ret);
388}
389