2 * Copyright (c) 2001 Doug Rabson
3 * Copyright (c) 2002, 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <net/ethernet.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
43 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
45 static void efinet_end(struct netif *);
46 static ssize_t efinet_get(struct iodesc *, void **, time_t);
47 static void efinet_init(struct iodesc *, void *);
48 static int efinet_match(struct netif *, void *);
49 static int efinet_probe(struct netif *, void *);
50 static ssize_t efinet_put(struct iodesc *, void *, size_t);
52 struct 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,
66 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
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));
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);
99 efinet_match(struct netif *nif, void *machdep_hint)
101 struct devdesc *dev = machdep_hint;
103 if (dev->d_unit == nif->nif_unit)
109 efinet_probe(struct netif *nif, void *machdep_hint)
111 EFI_SIMPLE_NETWORK *net;
115 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
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.
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 return (efi_status_to_errno(status));
134 efinet_put(struct iodesc *desc, void *pkt, size_t len)
136 struct netif *nif = desc->io_netif;
137 EFI_SIMPLE_NETWORK *net;
141 net = nif->nif_devdata;
145 status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
146 if (status != EFI_SUCCESS)
149 /* Wait for the buffer to be transmitted */
151 buf = NULL; /* XXX Is this needed? */
152 status = net->GetStatus(net, NULL, &buf);
154 * XXX EFI1.1 and the E1000 card returns a different
155 * address than we gave. Sigh.
157 } while (status == EFI_SUCCESS && buf == NULL);
159 /* XXX How do we deal with status != EFI_SUCCESS now? */
160 return ((status == EFI_SUCCESS) ? len : -1);
164 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
166 struct netif *nif = desc->io_netif;
167 EFI_SIMPLE_NETWORK *net;
174 net = nif->nif_devdata;
178 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
179 buf = malloc(bufsz + ETHER_ALIGN);
182 ptr = buf + ETHER_ALIGN;
185 while ((getsecs() - t) < timeout) {
186 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
187 if (status == EFI_SUCCESS) {
189 ret = (ssize_t)bufsz;
192 if (status != EFI_NOT_READY)
202 efinet_init(struct iodesc *desc, void *machdep_hint)
204 struct netif *nif = desc->io_netif;
205 EFI_SIMPLE_NETWORK *net;
210 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
211 printf("Invalid network interface %d\n", nif->nif_unit);
215 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
216 status = OpenProtocolByHandle(h, &sn_guid, (void **)&nif->nif_devdata);
217 if (status != EFI_SUCCESS) {
218 printf("net%d: cannot fetch interface data (status=%lu)\n",
219 nif->nif_unit, EFI_ERROR_CODE(status));
223 net = nif->nif_devdata;
224 if (net->Mode->State == EfiSimpleNetworkStopped) {
225 status = net->Start(net);
226 if (status != EFI_SUCCESS) {
227 printf("net%d: cannot start interface (status=%lu)\n",
228 nif->nif_unit, EFI_ERROR_CODE(status));
233 if (net->Mode->State != EfiSimpleNetworkInitialized) {
234 status = net->Initialize(net, 0, 0);
235 if (status != EFI_SUCCESS) {
236 printf("net%d: cannot init. interface (status=%lu)\n",
237 nif->nif_unit, EFI_ERROR_CODE(status));
242 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
243 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
245 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
246 if (status != EFI_SUCCESS)
247 printf("net%d: cannot set rx. filters (status=%lu)\n",
248 nif->nif_unit, EFI_ERROR_CODE(status));
251 dump_mode(net->Mode);
254 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
259 efinet_end(struct netif *nif)
261 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
269 static int efinet_dev_init(void);
270 static int efinet_dev_print(int);
272 struct devsw efinet_dev = {
275 .dv_init = efinet_dev_init,
276 .dv_strategy = NULL, /* Will be set in efinet_dev_init */
277 .dv_open = NULL, /* Will be set in efinet_dev_init */
278 .dv_close = NULL, /* Will be set in efinet_dev_init */
280 .dv_print = efinet_dev_print,
287 struct netif_dif *dif;
288 struct netif_stats *stats;
289 EFI_DEVICE_PATH *devpath, *node;
290 EFI_HANDLE *handles, *handles2;
294 extern struct devsw netdev;
298 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
299 if (status == EFI_BUFFER_TOO_SMALL) {
300 handles = (EFI_HANDLE *)malloc(sz);
301 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
303 if (EFI_ERROR(status))
306 if (EFI_ERROR(status))
307 return (efi_status_to_errno(status));
308 handles2 = (EFI_HANDLE *)malloc(sz);
309 if (handles2 == NULL) {
314 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
315 devpath = efi_lookup_devpath(handles[i]);
318 if ((node = efi_devpath_last_node(devpath)) == NULL)
321 if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
322 DevicePathSubType(node) != MSG_MAC_ADDR_DP)
325 handles2[nifs] = handles[i];
334 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
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);
343 efinetif.netif_ifs = NULL;
347 efinetif.netif_nifs = nifs;
349 for (i = 0; i < nifs; i++) {
351 dif = &efinetif.netif_ifs[i];
354 dif->dif_stats = &stats[i];
355 dif->dif_private = handles2[i];
358 efinet_dev.dv_open = netdev.dv_open;
359 efinet_dev.dv_close = netdev.dv_close;
360 efinet_dev.dv_strategy = netdev.dv_strategy;
368 efinet_dev_print(int verbose)
374 printf("%s devices:", efinet_dev.dv_name);
375 if ((ret = pager_output("\n")) != 0)
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);
382 text = efi_devpath_name(efi_lookup_devpath(h));
385 efi_free_devpath_name(text);
388 if ((ret = pager_output("\n")) != 0)