efinet.c revision 1.1.4.3 1 1.1.4.3 pgoyette /* $NetBSD: efinet.c,v 1.1.4.3 2018/09/06 06:55:35 pgoyette Exp $ */
2 1.1.4.2 pgoyette
3 1.1.4.2 pgoyette /*-
4 1.1.4.2 pgoyette * Copyright (c) 2001 Doug Rabson
5 1.1.4.2 pgoyette * Copyright (c) 2002, 2006 Marcel Moolenaar
6 1.1.4.2 pgoyette * All rights reserved.
7 1.1.4.2 pgoyette *
8 1.1.4.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.1.4.2 pgoyette * modification, are permitted provided that the following conditions
10 1.1.4.2 pgoyette * are met:
11 1.1.4.2 pgoyette * 1. Redistributions of source code must retain the above copyright
12 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer.
13 1.1.4.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.4.2 pgoyette * notice, this list of conditions and the following disclaimer in the
15 1.1.4.2 pgoyette * documentation and/or other materials provided with the distribution.
16 1.1.4.2 pgoyette *
17 1.1.4.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1.4.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.4.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.4.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1.4.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.4.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.4.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.4.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.4.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.4.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.4.2 pgoyette * SUCH DAMAGE.
28 1.1.4.2 pgoyette */
29 1.1.4.2 pgoyette
30 1.1.4.2 pgoyette #include <sys/cdefs.h>
31 1.1.4.2 pgoyette #if 0
32 1.1.4.2 pgoyette __FBSDID("$FreeBSD: head/stand/efi/libefi/efinet.c 321621 2017-07-27 15:06:34Z andrew $");
33 1.1.4.2 pgoyette #endif
34 1.1.4.2 pgoyette
35 1.1.4.2 pgoyette #include "efiboot.h"
36 1.1.4.2 pgoyette
37 1.1.4.2 pgoyette #include <lib/libsa/net.h>
38 1.1.4.2 pgoyette #include <lib/libsa/netif.h>
39 1.1.4.2 pgoyette
40 1.1.4.2 pgoyette #include <bootinfo.h>
41 1.1.4.2 pgoyette #include "devopen.h"
42 1.1.4.2 pgoyette
43 1.1.4.2 pgoyette #ifndef ETHER_ALIGN
44 1.1.4.2 pgoyette #define ETHER_ALIGN 2
45 1.1.4.2 pgoyette #endif
46 1.1.4.2 pgoyette
47 1.1.4.2 pgoyette #define ETHER_EXT_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_ALIGN)
48 1.1.4.2 pgoyette
49 1.1.4.2 pgoyette #if defined(DEBUG) || defined(ARP_DEBUG) || defined(BOOTP_DEBUG) || \
50 1.1.4.2 pgoyette defined(NET_DEBUG) || defined(NETIF_DEBUG) || defined(NFS_DEBUG) || \
51 1.1.4.2 pgoyette defined(RARP_DEBUG) || defined(RPC_DEBUG)
52 1.1.4.2 pgoyette int debug = 1;
53 1.1.4.2 pgoyette #else
54 1.1.4.2 pgoyette int debug = 0;
55 1.1.4.2 pgoyette #endif
56 1.1.4.2 pgoyette
57 1.1.4.2 pgoyette extern bool kernel_loaded;
58 1.1.4.2 pgoyette
59 1.1.4.2 pgoyette struct efinetinfo {
60 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
61 1.1.4.2 pgoyette bool bootdev;
62 1.1.4.2 pgoyette size_t pktbufsz;
63 1.1.4.2 pgoyette UINT8 *pktbuf;
64 1.1.4.2 pgoyette struct {
65 1.1.4.2 pgoyette int type;
66 1.1.4.2 pgoyette u_int tag;
67 1.1.4.2 pgoyette } bus;
68 1.1.4.2 pgoyette };
69 1.1.4.2 pgoyette static struct btinfo_netif bi_netif;
70 1.1.4.2 pgoyette
71 1.1.4.2 pgoyette static int efinet_match(struct netif *, void *);
72 1.1.4.2 pgoyette static int efinet_probe(struct netif *, void *);
73 1.1.4.2 pgoyette static void efinet_init(struct iodesc *, void *);
74 1.1.4.2 pgoyette static int efinet_get(struct iodesc *, void *, size_t, saseconds_t);
75 1.1.4.2 pgoyette static int efinet_put(struct iodesc *, void *, size_t);
76 1.1.4.2 pgoyette static void efinet_end(struct netif *);
77 1.1.4.2 pgoyette
78 1.1.4.2 pgoyette struct netif_driver efinetif = {
79 1.1.4.2 pgoyette .netif_bname = "net",
80 1.1.4.2 pgoyette .netif_match = efinet_match,
81 1.1.4.2 pgoyette .netif_probe = efinet_probe,
82 1.1.4.2 pgoyette .netif_init = efinet_init,
83 1.1.4.2 pgoyette .netif_get = efinet_get,
84 1.1.4.2 pgoyette .netif_put = efinet_put,
85 1.1.4.2 pgoyette .netif_end = efinet_end,
86 1.1.4.2 pgoyette .netif_ifs = NULL,
87 1.1.4.2 pgoyette .netif_nifs = 0
88 1.1.4.2 pgoyette };
89 1.1.4.2 pgoyette
90 1.1.4.2 pgoyette #ifdef EFINET_DEBUG
91 1.1.4.2 pgoyette static void
92 1.1.4.2 pgoyette dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
93 1.1.4.2 pgoyette {
94 1.1.4.2 pgoyette int i;
95 1.1.4.2 pgoyette
96 1.1.4.2 pgoyette printf("State = %x\n", mode->State);
97 1.1.4.2 pgoyette printf("HwAddressSize = %u\n", mode->HwAddressSize);
98 1.1.4.2 pgoyette printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize);
99 1.1.4.2 pgoyette printf("MaxPacketSize = %u\n", mode->MaxPacketSize);
100 1.1.4.2 pgoyette printf("NvRamSize = %u\n", mode->NvRamSize);
101 1.1.4.2 pgoyette printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize);
102 1.1.4.2 pgoyette printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask);
103 1.1.4.2 pgoyette printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting);
104 1.1.4.2 pgoyette printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount);
105 1.1.4.2 pgoyette printf("MCastFilterCount = %u\n", mode->MCastFilterCount);
106 1.1.4.2 pgoyette printf("MCastFilter = {");
107 1.1.4.2 pgoyette for (i = 0; i < mode->MCastFilterCount; i++)
108 1.1.4.2 pgoyette printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
109 1.1.4.2 pgoyette printf(" }\n");
110 1.1.4.2 pgoyette printf("CurrentAddress = %s\n",
111 1.1.4.2 pgoyette ether_sprintf(mode->CurrentAddress.Addr));
112 1.1.4.2 pgoyette printf("BroadcastAddress = %s\n",
113 1.1.4.2 pgoyette ether_sprintf(mode->BroadcastAddress.Addr));
114 1.1.4.2 pgoyette printf("PermanentAddress = %s\n",
115 1.1.4.2 pgoyette ether_sprintf(mode->PermanentAddress.Addr));
116 1.1.4.2 pgoyette printf("IfType = %u\n", mode->IfType);
117 1.1.4.2 pgoyette printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable);
118 1.1.4.2 pgoyette printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported);
119 1.1.4.2 pgoyette printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
120 1.1.4.2 pgoyette printf("MediaPresent = %d\n", mode->MediaPresent);
121 1.1.4.2 pgoyette }
122 1.1.4.2 pgoyette #endif
123 1.1.4.2 pgoyette
124 1.1.4.2 pgoyette static int
125 1.1.4.2 pgoyette efinet_match(struct netif *nif, void *machdep_hint)
126 1.1.4.2 pgoyette {
127 1.1.4.2 pgoyette struct devdesc *dev = machdep_hint;
128 1.1.4.2 pgoyette
129 1.1.4.2 pgoyette if (dev->d_unit != nif->nif_unit)
130 1.1.4.2 pgoyette return 0;
131 1.1.4.2 pgoyette
132 1.1.4.2 pgoyette return 1;
133 1.1.4.2 pgoyette }
134 1.1.4.2 pgoyette
135 1.1.4.2 pgoyette static int
136 1.1.4.2 pgoyette efinet_probe(struct netif *nif, void *machdep_hint)
137 1.1.4.2 pgoyette {
138 1.1.4.2 pgoyette
139 1.1.4.2 pgoyette return 0;
140 1.1.4.2 pgoyette }
141 1.1.4.2 pgoyette
142 1.1.4.2 pgoyette static int
143 1.1.4.2 pgoyette efinet_put(struct iodesc *desc, void *pkt, size_t len)
144 1.1.4.2 pgoyette {
145 1.1.4.2 pgoyette struct netif *nif = desc->io_netif;
146 1.1.4.2 pgoyette struct efinetinfo *eni = nif->nif_devdata;
147 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
148 1.1.4.2 pgoyette EFI_STATUS status;
149 1.1.4.2 pgoyette void *buf;
150 1.1.4.2 pgoyette
151 1.1.4.2 pgoyette if (eni == NULL)
152 1.1.4.2 pgoyette return -1;
153 1.1.4.2 pgoyette net = eni->net;
154 1.1.4.2 pgoyette
155 1.1.4.2 pgoyette status = uefi_call_wrapper(net->Transmit, 7, net, 0, (UINTN)len, pkt, NULL,
156 1.1.4.2 pgoyette NULL, NULL);
157 1.1.4.2 pgoyette if (EFI_ERROR(status))
158 1.1.4.2 pgoyette return -1;
159 1.1.4.2 pgoyette
160 1.1.4.2 pgoyette /* Wait for the buffer to be transmitted */
161 1.1.4.2 pgoyette do {
162 1.1.4.2 pgoyette buf = NULL; /* XXX Is this needed? */
163 1.1.4.2 pgoyette status = uefi_call_wrapper(net->GetStatus, 3, net, NULL, &buf);
164 1.1.4.2 pgoyette /*
165 1.1.4.2 pgoyette * XXX EFI1.1 and the E1000 card returns a different
166 1.1.4.2 pgoyette * address than we gave. Sigh.
167 1.1.4.2 pgoyette */
168 1.1.4.2 pgoyette } while (!EFI_ERROR(status) && buf == NULL);
169 1.1.4.2 pgoyette
170 1.1.4.2 pgoyette /* XXX How do we deal with status != EFI_SUCCESS now? */
171 1.1.4.2 pgoyette return EFI_ERROR(status) ? -1 : len;
172 1.1.4.2 pgoyette }
173 1.1.4.2 pgoyette
174 1.1.4.2 pgoyette static int
175 1.1.4.2 pgoyette efinet_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
176 1.1.4.2 pgoyette {
177 1.1.4.2 pgoyette struct netif *nif = desc->io_netif;
178 1.1.4.2 pgoyette struct efinetinfo *eni = nif->nif_devdata;
179 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
180 1.1.4.2 pgoyette EFI_STATUS status;
181 1.1.4.2 pgoyette UINTN bufsz, rsz;
182 1.1.4.2 pgoyette time_t t;
183 1.1.4.2 pgoyette char *buf, *ptr;
184 1.1.4.2 pgoyette int ret = -1;
185 1.1.4.2 pgoyette
186 1.1.4.2 pgoyette if (eni == NULL)
187 1.1.4.2 pgoyette return -1;
188 1.1.4.2 pgoyette net = eni->net;
189 1.1.4.2 pgoyette
190 1.1.4.2 pgoyette if (eni->pktbufsz < net->Mode->MaxPacketSize + ETHER_EXT_LEN) {
191 1.1.4.2 pgoyette bufsz = net->Mode->MaxPacketSize + ETHER_EXT_LEN;
192 1.1.4.2 pgoyette buf = alloc(bufsz);
193 1.1.4.2 pgoyette if (buf == NULL)
194 1.1.4.2 pgoyette return -1;
195 1.1.4.2 pgoyette dealloc(eni->pktbuf, eni->pktbufsz);
196 1.1.4.2 pgoyette eni->pktbufsz = bufsz;
197 1.1.4.2 pgoyette eni->pktbuf = buf;
198 1.1.4.2 pgoyette }
199 1.1.4.2 pgoyette ptr = eni->pktbuf + ETHER_ALIGN;
200 1.1.4.2 pgoyette
201 1.1.4.2 pgoyette t = getsecs();
202 1.1.4.2 pgoyette while ((getsecs() - t) < timeout) {
203 1.1.4.2 pgoyette rsz = eni->pktbufsz;
204 1.1.4.2 pgoyette status = uefi_call_wrapper(net->Receive, 7, net, NULL, &rsz, ptr,
205 1.1.4.2 pgoyette NULL, NULL, NULL);
206 1.1.4.2 pgoyette if (!EFI_ERROR(status)) {
207 1.1.4.3 pgoyette rsz = uimin(rsz, len);
208 1.1.4.2 pgoyette memcpy(pkt, ptr, rsz);
209 1.1.4.2 pgoyette ret = (int)rsz;
210 1.1.4.2 pgoyette break;
211 1.1.4.2 pgoyette }
212 1.1.4.2 pgoyette if (status != EFI_NOT_READY)
213 1.1.4.2 pgoyette break;
214 1.1.4.2 pgoyette }
215 1.1.4.2 pgoyette
216 1.1.4.2 pgoyette return ret;
217 1.1.4.2 pgoyette }
218 1.1.4.2 pgoyette
219 1.1.4.2 pgoyette static void
220 1.1.4.2 pgoyette efinet_init(struct iodesc *desc, void *machdep_hint)
221 1.1.4.2 pgoyette {
222 1.1.4.2 pgoyette struct netif *nif = desc->io_netif;
223 1.1.4.2 pgoyette struct efinetinfo *eni;
224 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
225 1.1.4.2 pgoyette EFI_STATUS status;
226 1.1.4.2 pgoyette UINT32 mask;
227 1.1.4.2 pgoyette
228 1.1.4.2 pgoyette if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
229 1.1.4.2 pgoyette printf("Invalid network interface %d\n", nif->nif_unit);
230 1.1.4.2 pgoyette return;
231 1.1.4.2 pgoyette }
232 1.1.4.2 pgoyette
233 1.1.4.2 pgoyette eni = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
234 1.1.4.2 pgoyette nif->nif_devdata = eni;
235 1.1.4.2 pgoyette net = eni->net;
236 1.1.4.2 pgoyette if (net->Mode->State == EfiSimpleNetworkStopped) {
237 1.1.4.2 pgoyette status = uefi_call_wrapper(net->Start, 1, net);
238 1.1.4.2 pgoyette if (EFI_ERROR(status)) {
239 1.1.4.2 pgoyette printf("net%d: cannot start interface (status=%"
240 1.1.4.2 pgoyette PRIxMAX ")\n", nif->nif_unit, (uintmax_t)status);
241 1.1.4.2 pgoyette return;
242 1.1.4.2 pgoyette }
243 1.1.4.2 pgoyette }
244 1.1.4.2 pgoyette
245 1.1.4.2 pgoyette if (net->Mode->State != EfiSimpleNetworkInitialized) {
246 1.1.4.2 pgoyette status = uefi_call_wrapper(net->Initialize, 3, net, 0, 0);
247 1.1.4.2 pgoyette if (EFI_ERROR(status)) {
248 1.1.4.2 pgoyette printf("net%d: cannot init. interface (status=%"
249 1.1.4.2 pgoyette PRIxMAX ")\n", nif->nif_unit, (uintmax_t)status);
250 1.1.4.2 pgoyette return;
251 1.1.4.2 pgoyette }
252 1.1.4.2 pgoyette }
253 1.1.4.2 pgoyette
254 1.1.4.2 pgoyette mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
255 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
256 1.1.4.2 pgoyette
257 1.1.4.2 pgoyette status = uefi_call_wrapper(net->ReceiveFilters, 6, net, mask, 0, FALSE,
258 1.1.4.2 pgoyette 0, NULL);
259 1.1.4.2 pgoyette if (EFI_ERROR(status)) {
260 1.1.4.2 pgoyette printf("net%d: cannot set rx. filters (status=%" PRIxMAX ")\n",
261 1.1.4.2 pgoyette nif->nif_unit, (uintmax_t)status);
262 1.1.4.2 pgoyette return;
263 1.1.4.2 pgoyette }
264 1.1.4.2 pgoyette
265 1.1.4.2 pgoyette if (!kernel_loaded) {
266 1.1.4.2 pgoyette bi_netif.bus = eni->bus.type;
267 1.1.4.2 pgoyette bi_netif.addr.tag = eni->bus.tag;
268 1.1.4.2 pgoyette snprintf(bi_netif.ifname, sizeof(bi_netif.ifname), "net%d",
269 1.1.4.2 pgoyette nif->nif_unit);
270 1.1.4.2 pgoyette BI_ADD(&bi_netif, BTINFO_NETIF, sizeof(bi_netif));
271 1.1.4.2 pgoyette }
272 1.1.4.2 pgoyette
273 1.1.4.2 pgoyette #ifdef EFINET_DEBUG
274 1.1.4.2 pgoyette dump_mode(net->Mode);
275 1.1.4.2 pgoyette #endif
276 1.1.4.2 pgoyette
277 1.1.4.2 pgoyette memcpy(desc->myea, net->Mode->CurrentAddress.Addr, 6);
278 1.1.4.2 pgoyette desc->xid = 1;
279 1.1.4.2 pgoyette }
280 1.1.4.2 pgoyette
281 1.1.4.2 pgoyette static void
282 1.1.4.2 pgoyette efinet_end(struct netif *nif)
283 1.1.4.2 pgoyette {
284 1.1.4.2 pgoyette struct efinetinfo *eni = nif->nif_devdata;
285 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
286 1.1.4.2 pgoyette
287 1.1.4.2 pgoyette if (eni == NULL)
288 1.1.4.2 pgoyette return;
289 1.1.4.2 pgoyette net = eni->net;
290 1.1.4.2 pgoyette
291 1.1.4.2 pgoyette uefi_call_wrapper(net->Shutdown, 1, net);
292 1.1.4.2 pgoyette }
293 1.1.4.2 pgoyette
294 1.1.4.2 pgoyette static bool
295 1.1.4.2 pgoyette efi_net_pci_probe(struct efinetinfo *eni, EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *pdp)
296 1.1.4.2 pgoyette {
297 1.1.4.2 pgoyette PCI_DEVICE_PATH *pci = (PCI_DEVICE_PATH *)dp;
298 1.1.4.2 pgoyette int bus = -1;
299 1.1.4.2 pgoyette
300 1.1.4.2 pgoyette if (pdp != NULL &&
301 1.1.4.2 pgoyette DevicePathType(pdp) == ACPI_DEVICE_PATH &&
302 1.1.4.2 pgoyette (DevicePathSubType(pdp) == ACPI_DP ||
303 1.1.4.2 pgoyette DevicePathSubType(pdp) == EXPANDED_ACPI_DP)) {
304 1.1.4.2 pgoyette ACPI_HID_DEVICE_PATH *acpi = (ACPI_HID_DEVICE_PATH *)pdp;
305 1.1.4.2 pgoyette /* PCI root bus */
306 1.1.4.2 pgoyette if (acpi->HID == EISA_PNP_ID(0x0A08) ||
307 1.1.4.2 pgoyette acpi->HID == EISA_PNP_ID(0x0A03)) {
308 1.1.4.2 pgoyette bus = acpi->UID;
309 1.1.4.2 pgoyette }
310 1.1.4.2 pgoyette }
311 1.1.4.2 pgoyette if (bus < 0)
312 1.1.4.2 pgoyette return false;
313 1.1.4.2 pgoyette
314 1.1.4.2 pgoyette eni->bus.type = BI_BUS_PCI;
315 1.1.4.2 pgoyette eni->bus.tag = (bus & 0xff) << 8;
316 1.1.4.2 pgoyette eni->bus.tag |= (pci->Device & 0x1f) << 3;
317 1.1.4.2 pgoyette eni->bus.tag |= pci->Function & 0x7;
318 1.1.4.2 pgoyette return true;
319 1.1.4.2 pgoyette }
320 1.1.4.2 pgoyette
321 1.1.4.2 pgoyette void
322 1.1.4.2 pgoyette efi_net_probe(void)
323 1.1.4.2 pgoyette {
324 1.1.4.2 pgoyette struct efinetinfo *enis;
325 1.1.4.2 pgoyette struct netif_dif *dif;
326 1.1.4.2 pgoyette struct netif_stats *stats;
327 1.1.4.2 pgoyette EFI_DEVICE_PATH *dp0, *dp, *pdp;
328 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
329 1.1.4.2 pgoyette EFI_HANDLE *handles;
330 1.1.4.2 pgoyette EFI_STATUS status;
331 1.1.4.2 pgoyette UINTN i, nhandles;
332 1.1.4.2 pgoyette int nifs;
333 1.1.4.2 pgoyette bool found;
334 1.1.4.2 pgoyette
335 1.1.4.2 pgoyette status = LibLocateHandle(ByProtocol, &SimpleNetworkProtocol, NULL,
336 1.1.4.2 pgoyette &nhandles, &handles);
337 1.1.4.2 pgoyette if (EFI_ERROR(status) || nhandles == 0)
338 1.1.4.2 pgoyette return;
339 1.1.4.2 pgoyette
340 1.1.4.2 pgoyette enis = alloc(nhandles * sizeof(*enis));
341 1.1.4.2 pgoyette if (enis == NULL)
342 1.1.4.2 pgoyette return;
343 1.1.4.2 pgoyette memset(enis, 0, nhandles * sizeof(*enis));
344 1.1.4.2 pgoyette
345 1.1.4.2 pgoyette nifs = 0;
346 1.1.4.2 pgoyette for (i = 0; i < nhandles; i++) {
347 1.1.4.2 pgoyette status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
348 1.1.4.2 pgoyette &DevicePathProtocol, (void **)&dp0);
349 1.1.4.2 pgoyette if (EFI_ERROR(status))
350 1.1.4.2 pgoyette continue;
351 1.1.4.2 pgoyette
352 1.1.4.2 pgoyette found = false;
353 1.1.4.2 pgoyette for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
354 1.1.4.2 pgoyette if (DevicePathType(dp) == MESSAGING_DEVICE_PATH &&
355 1.1.4.2 pgoyette DevicePathSubType(dp) == MSG_MAC_ADDR_DP) {
356 1.1.4.2 pgoyette found = true;
357 1.1.4.2 pgoyette break;
358 1.1.4.2 pgoyette }
359 1.1.4.2 pgoyette }
360 1.1.4.2 pgoyette if (!found)
361 1.1.4.2 pgoyette continue;
362 1.1.4.2 pgoyette
363 1.1.4.2 pgoyette status = uefi_call_wrapper(BS->OpenProtocol, 6, handles[i],
364 1.1.4.2 pgoyette &SimpleNetworkProtocol, (void **)&net, IH, NULL,
365 1.1.4.2 pgoyette EFI_OPEN_PROTOCOL_EXCLUSIVE);
366 1.1.4.2 pgoyette if (EFI_ERROR(status)) {
367 1.1.4.2 pgoyette printf("Unable to open network interface %" PRIuMAX
368 1.1.4.2 pgoyette " for exclusive access: %" PRIxMAX "\n",
369 1.1.4.2 pgoyette (uintmax_t)i, (uintmax_t)status);
370 1.1.4.2 pgoyette }
371 1.1.4.2 pgoyette
372 1.1.4.2 pgoyette found = false;
373 1.1.4.2 pgoyette for (pdp = NULL, dp = dp0;
374 1.1.4.2 pgoyette !IsDevicePathEnd(dp);
375 1.1.4.2 pgoyette pdp = dp, dp = NextDevicePathNode(dp)) {
376 1.1.4.2 pgoyette if (DevicePathType(dp) == HARDWARE_DEVICE_PATH) {
377 1.1.4.2 pgoyette if (DevicePathSubType(dp) == HW_PCI_DP)
378 1.1.4.2 pgoyette found = efi_net_pci_probe(&enis[nifs],
379 1.1.4.2 pgoyette dp, pdp);
380 1.1.4.2 pgoyette break;
381 1.1.4.2 pgoyette }
382 1.1.4.2 pgoyette }
383 1.1.4.2 pgoyette if (found) {
384 1.1.4.2 pgoyette enis[nifs].net = net;
385 1.1.4.2 pgoyette enis[nifs].bootdev = efi_pxe_match_booted_interface(
386 1.1.4.2 pgoyette &net->Mode->CurrentAddress, net->Mode->HwAddressSize);
387 1.1.4.2 pgoyette enis[nifs].pktbufsz = net->Mode->MaxPacketSize +
388 1.1.4.2 pgoyette ETHER_EXT_LEN;
389 1.1.4.2 pgoyette enis[nifs].pktbuf = alloc(enis[nifs].pktbufsz);
390 1.1.4.2 pgoyette if (enis[nifs].pktbuf == NULL) {
391 1.1.4.2 pgoyette while (i-- > 0) {
392 1.1.4.2 pgoyette dealloc(enis[i].pktbuf, enis[i].pktbufsz);
393 1.1.4.2 pgoyette if (i == 0)
394 1.1.4.2 pgoyette break;
395 1.1.4.2 pgoyette }
396 1.1.4.2 pgoyette dealloc(enis, nhandles * sizeof(*enis));
397 1.1.4.2 pgoyette FreePool(handles);
398 1.1.4.2 pgoyette return;
399 1.1.4.2 pgoyette }
400 1.1.4.2 pgoyette nifs++;
401 1.1.4.2 pgoyette }
402 1.1.4.2 pgoyette }
403 1.1.4.2 pgoyette
404 1.1.4.2 pgoyette FreePool(handles);
405 1.1.4.2 pgoyette
406 1.1.4.2 pgoyette if (nifs == 0)
407 1.1.4.2 pgoyette return;
408 1.1.4.2 pgoyette
409 1.1.4.2 pgoyette efinetif.netif_ifs = alloc(nifs * sizeof(*dif));
410 1.1.4.2 pgoyette stats = alloc(nifs * sizeof(*stats));
411 1.1.4.2 pgoyette if (efinetif.netif_ifs == NULL || stats == NULL) {
412 1.1.4.2 pgoyette if (efinetif.netif_ifs != NULL) {
413 1.1.4.2 pgoyette dealloc(efinetif.netif_ifs, nifs * sizeof(*dif));
414 1.1.4.2 pgoyette efinetif.netif_ifs = NULL;
415 1.1.4.2 pgoyette }
416 1.1.4.2 pgoyette if (stats != NULL)
417 1.1.4.2 pgoyette dealloc(stats, nifs * sizeof(*stats));
418 1.1.4.2 pgoyette for (i = 0; i < nifs; i++)
419 1.1.4.2 pgoyette dealloc(enis[i].pktbuf, enis[i].pktbufsz);
420 1.1.4.2 pgoyette dealloc(enis, nhandles * sizeof(*enis));
421 1.1.4.2 pgoyette return;
422 1.1.4.2 pgoyette }
423 1.1.4.2 pgoyette memset(efinetif.netif_ifs, 0, nifs * sizeof(*dif));
424 1.1.4.2 pgoyette memset(stats, 0, nifs * sizeof(*stats));
425 1.1.4.2 pgoyette efinetif.netif_nifs = nifs;
426 1.1.4.2 pgoyette
427 1.1.4.2 pgoyette for (i = 0; i < nifs; i++) {
428 1.1.4.2 pgoyette dif = &efinetif.netif_ifs[i];
429 1.1.4.2 pgoyette dif->dif_unit = i;
430 1.1.4.2 pgoyette dif->dif_nsel = 1;
431 1.1.4.2 pgoyette dif->dif_stats = &stats[i];
432 1.1.4.2 pgoyette dif->dif_private = &enis[i];
433 1.1.4.2 pgoyette }
434 1.1.4.2 pgoyette }
435 1.1.4.2 pgoyette
436 1.1.4.2 pgoyette void
437 1.1.4.2 pgoyette efi_net_show(void)
438 1.1.4.2 pgoyette {
439 1.1.4.2 pgoyette const struct netif_dif *dif;
440 1.1.4.2 pgoyette const struct efinetinfo *eni;
441 1.1.4.2 pgoyette EFI_SIMPLE_NETWORK *net;
442 1.1.4.2 pgoyette int i;
443 1.1.4.2 pgoyette
444 1.1.4.2 pgoyette for (i = 0; i < efinetif.netif_nifs; i++) {
445 1.1.4.2 pgoyette dif = &efinetif.netif_ifs[i];
446 1.1.4.2 pgoyette eni = dif->dif_private;
447 1.1.4.2 pgoyette net = eni->net;
448 1.1.4.2 pgoyette
449 1.1.4.2 pgoyette printf("net net%d", dif->dif_unit);
450 1.1.4.2 pgoyette if (net->Mode != NULL) {
451 1.1.4.2 pgoyette for (UINT32 x = 0; x < net->Mode->HwAddressSize; x++) {
452 1.1.4.2 pgoyette printf("%c%02x", x == 0 ? ' ' : ':',
453 1.1.4.2 pgoyette net->Mode->CurrentAddress.Addr[x]);
454 1.1.4.2 pgoyette }
455 1.1.4.2 pgoyette }
456 1.1.4.2 pgoyette if (eni->bus.type == BI_BUS_PCI) {
457 1.1.4.2 pgoyette printf(" pci%d,%d,%d", (eni->bus.tag >> 8) & 0xff,
458 1.1.4.2 pgoyette (eni->bus.tag >> 3) & 0x1f, eni->bus.tag & 0x7);
459 1.1.4.2 pgoyette }
460 1.1.4.2 pgoyette if (eni->bootdev)
461 1.1.4.2 pgoyette printf(" pxeboot");
462 1.1.4.2 pgoyette printf("\n");
463 1.1.4.2 pgoyette }
464 1.1.4.2 pgoyette }
465 1.1.4.2 pgoyette
466 1.1.4.2 pgoyette int
467 1.1.4.2 pgoyette efi_net_get_booted_interface_unit(void)
468 1.1.4.2 pgoyette {
469 1.1.4.2 pgoyette const struct netif_dif *dif;
470 1.1.4.2 pgoyette const struct efinetinfo *eni;
471 1.1.4.2 pgoyette int i;
472 1.1.4.2 pgoyette
473 1.1.4.2 pgoyette for (i = 0; i < efinetif.netif_nifs; i++) {
474 1.1.4.2 pgoyette dif = &efinetif.netif_ifs[i];
475 1.1.4.2 pgoyette eni = dif->dif_private;
476 1.1.4.2 pgoyette if (eni->bootdev)
477 1.1.4.2 pgoyette return dif->dif_unit;
478 1.1.4.2 pgoyette }
479 1.1.4.2 pgoyette return -1;
480 1.1.4.2 pgoyette }
481