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