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)
116 efinet_put(struct iodesc *desc, void *pkt, size_t len)
118 struct netif *nif = desc->io_netif;
119 EFI_SIMPLE_NETWORK *net;
123 net = nif->nif_devdata;
127 status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
128 if (status != EFI_SUCCESS)
131 /* Wait for the buffer to be transmitted */
133 buf = NULL; /* XXX Is this needed? */
134 status = net->GetStatus(net, NULL, &buf);
136 * XXX EFI1.1 and the E1000 card returns a different
137 * address than we gave. Sigh.
139 } while (status == EFI_SUCCESS && buf == NULL);
141 /* XXX How do we deal with status != EFI_SUCCESS now? */
142 return ((status == EFI_SUCCESS) ? len : -1);
146 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
148 struct netif *nif = desc->io_netif;
149 EFI_SIMPLE_NETWORK *net;
156 net = nif->nif_devdata;
160 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
161 buf = malloc(bufsz + ETHER_ALIGN);
164 ptr = buf + ETHER_ALIGN;
167 while ((getsecs() - t) < timeout) {
168 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
169 if (status == EFI_SUCCESS) {
171 ret = (ssize_t)bufsz;
174 if (status != EFI_NOT_READY)
184 efinet_init(struct iodesc *desc, void *machdep_hint)
186 struct netif *nif = desc->io_netif;
187 EFI_SIMPLE_NETWORK *net;
192 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
193 printf("Invalid network interface %d\n", nif->nif_unit);
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));
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));
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));
224 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
225 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
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));
233 dump_mode(net->Mode);
236 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
241 efinet_end(struct netif *nif)
243 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
251 static int efinet_dev_init(void);
252 static int efinet_dev_print(int);
254 struct devsw efinet_dev = {
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 */
262 .dv_print = efinet_dev_print,
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;
277 extern struct devsw netdev;
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,
286 if (EFI_ERROR(status))
289 if (EFI_ERROR(status))
290 return (efi_status_to_errno(status));
291 handles2 = (EFI_HANDLE *)malloc(sz);
292 if (handles2 == NULL) {
297 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
298 devpath = efi_lookup_devpath(handles[i]);
301 if ((node = efi_devpath_last_node(devpath)) == NULL)
304 if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
305 DevicePathSubType(node) != MSG_MAC_ADDR_DP)
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.
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));
321 handles2[nifs] = handles[i];
330 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
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);
339 efinetif.netif_ifs = NULL;
343 efinetif.netif_nifs = nifs;
345 for (i = 0; i < nifs; i++) {
347 dif = &efinetif.netif_ifs[i];
350 dif->dif_stats = &stats[i];
351 dif->dif_private = handles2[i];
354 efinet_dev.dv_open = netdev.dv_open;
355 efinet_dev.dv_close = netdev.dv_close;
356 efinet_dev.dv_strategy = netdev.dv_strategy;
364 efinet_dev_print(int verbose)
370 printf("%s devices:", efinet_dev.dv_name);
371 if ((ret = pager_output("\n")) != 0)
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);
378 text = efi_devpath_name(efi_lookup_devpath(h));
381 efi_free_devpath_name(text);
384 if ((ret = pager_output("\n")) != 0)