efinet.c revision 100390
1/*- 2 * Copyright (c) 2001 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/boot/efi/libefi/efinet.c 100390 2002-07-20 03:52:18Z peter $ 27 */ 28 29#include <sys/param.h> 30#include <netinet/in.h> 31#include <netinet/in_systm.h> 32 33#include <stand.h> 34#include <net.h> 35#include <netif.h> 36 37#include <efi.h> 38#include <efilib.h> 39 40extern struct netif_driver efi_net; 41 42#ifdef EFINET_DEBUG 43static void 44dump_mode(EFI_SIMPLE_NETWORK_MODE *mode) 45{ 46 int i; 47 48 printf("State = %x\n", mode->State); 49 printf("HwAddressSize = %u\n", mode->HwAddressSize); 50 printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize); 51 printf("MaxPacketSize = %u\n", mode->MaxPacketSize); 52 printf("NvRamSize = %u\n", mode->NvRamSize); 53 printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize); 54 printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask); 55 printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting); 56 printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount); 57 printf("MCastFilterCount = %u\n", mode->MCastFilterCount); 58 printf("MCastFilter = {"); 59 for (i = 0; i < mode->MCastFilterCount; i++) 60 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr)); 61 printf(" }\n"); 62 printf("CurrentAddress = %s\n", 63 ether_sprintf(mode->CurrentAddress.Addr)); 64 printf("BroadcastAddress = %s\n", 65 ether_sprintf(mode->BroadcastAddress.Addr)); 66 printf("PermanentAddress = %s\n", 67 ether_sprintf(mode->PermanentAddress.Addr)); 68 printf("IfType = %u\n", mode->IfType); 69 printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable); 70 printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported); 71 printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported); 72 printf("MediaPresent = %d\n", mode->MediaPresent); 73} 74#endif 75 76int 77efinet_match(struct netif *nif, void *machdep_hint) 78{ 79 80 return (1); 81} 82 83int 84efinet_probe(struct netif *nif, void *machdep_hint) 85{ 86 87 return (0); 88} 89 90int 91efinet_put(struct iodesc *desc, void *pkt, size_t len) 92{ 93 struct netif *nif = desc->io_netif; 94 EFI_SIMPLE_NETWORK *net; 95 EFI_STATUS status; 96 void *buf; 97 98 net = nif->nif_devdata; 99 100 status = net->Transmit(net, 0, len, pkt, 0, 0, 0); 101 if (status != EFI_SUCCESS) 102 return -1; 103 104 /* Wait for the buffer to be transmitted */ 105 do { 106 buf = 0; /* XXX Is this needed? */ 107 status = net->GetStatus(net, 0, &buf); 108 /* 109 * XXX EFI1.1 and the E1000 card returns a different 110 * address than we gave. Sigh. 111 */ 112 } while (status == EFI_SUCCESS && buf == 0); 113 114 /* XXX How do we deal with status != EFI_SUCCESS now? */ 115 return (status == EFI_SUCCESS) ? len : -1; 116} 117 118 119int 120efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout) 121{ 122 struct netif *nif = desc->io_netif; 123 EFI_SIMPLE_NETWORK *net; 124 EFI_STATUS status; 125 UINTN bufsz; 126 time_t t; 127 char buf[2048]; 128 129 net = nif->nif_devdata; 130 131 t = time(0); 132 while ((time(0) - t) < timeout) { 133 bufsz = sizeof(buf); 134 status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0); 135 if (status == EFI_SUCCESS) { 136 /* 137 * XXX EFI1.1 and the E1000 card trash our 138 * workspace if we do not do this silly copy. 139 * Either they are not respecting the len 140 * value or do not like the alignment. 141 */ 142 if (bufsz > len) 143 bufsz = len; 144 bcopy(buf, pkt, bufsz); 145 return bufsz; 146 } 147 if (status != EFI_NOT_READY) 148 return 0; 149 } 150 151 return 0; 152} 153 154void 155efinet_init(struct iodesc *desc, void *machdep_hint) 156{ 157 struct netif *nif = desc->io_netif; 158 EFI_SIMPLE_NETWORK *net; 159 EFI_STATUS status; 160 161 net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private; 162 nif->nif_devdata = net; 163 164 if (net->Mode->State == EfiSimpleNetworkStopped) { 165 status = net->Start(net); 166 if (status != EFI_SUCCESS) { 167 printf("net%d: cannot start interface (status=%ld)\n", 168 nif->nif_unit, status); 169 return; 170 } 171 } 172 173 if (net->Mode->State != EfiSimpleNetworkInitialized) { 174 status = net->Initialize(net, 0, 0); 175 if (status != EFI_SUCCESS) { 176 printf("net%d: cannot init. interface (status=%ld)\n", 177 nif->nif_unit, status); 178 return; 179 } 180 } 181 182 if (net->Mode->ReceiveFilterSetting == 0) { 183 UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | 184 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; 185 186 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0); 187 if (status != EFI_SUCCESS) { 188 printf("net%d: cannot set rx. filters (status=%ld)\n", 189 nif->nif_unit, status); 190 return; 191 } 192 } 193 194#ifdef EFINET_DEBUG 195 dump_mode(net->Mode); 196#endif 197 198 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6); 199 desc->xid = 1; 200 201 return; 202} 203 204void 205efinet_init_driver() 206{ 207 EFI_STATUS status; 208 UINTN sz; 209 static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL; 210 EFI_HANDLE *handles; 211 int nifs, i; 212#define MAX_INTERFACES 4 213 static struct netif_dif difs[MAX_INTERFACES]; 214 static struct netif_stats stats[MAX_INTERFACES]; 215 216 sz = 0; 217 status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0); 218 if (status != EFI_BUFFER_TOO_SMALL) 219 return; 220 handles = (EFI_HANDLE *) malloc(sz); 221 status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles); 222 if (EFI_ERROR(status)) { 223 free(handles); 224 return; 225 } 226 227 nifs = sz / sizeof(EFI_HANDLE); 228 if (nifs > MAX_INTERFACES) 229 nifs = MAX_INTERFACES; 230 231 efi_net.netif_nifs = nifs; 232 efi_net.netif_ifs = difs; 233 234 bzero(stats, sizeof(stats)); 235 for (i = 0; i < nifs; i++) { 236 struct netif_dif *dif = &efi_net.netif_ifs[i]; 237 dif->dif_unit = i; 238 dif->dif_nsel = 1; 239 dif->dif_stats = &stats[i]; 240 241 BS->HandleProtocol(handles[i], &netid, 242 (VOID**) &dif->dif_private); 243 } 244 245 return; 246} 247 248void 249efinet_end(struct netif *nif) 250{ 251 EFI_SIMPLE_NETWORK *net = nif->nif_devdata; 252 253 net->Shutdown(net); 254} 255 256struct netif_driver efi_net = { 257 "net", /* netif_bname */ 258 efinet_match, /* netif_match */ 259 efinet_probe, /* netif_probe */ 260 efinet_init, /* netif_init */ 261 efinet_get, /* netif_get */ 262 efinet_put, /* netif_put */ 263 efinet_end, /* netif_end */ 264 0, /* netif_ifs */ 265 0 /* netif_nifs */ 266}; 267 268