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