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 <netinet/in.h> 32 #include <netinet/in_systm.h> 33 34 #include <stand.h> 35 #include <net.h> 36 #include <netif.h> 37 38 #include <dev_net.c> 39 40 #include <efi.h> 41 #include <efilib.h> 42 43 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL; 44 45 static void efinet_end(struct netif *); 46 static int efinet_get(struct iodesc *, void *, size_t, 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 int efinet_put(struct iodesc *, void *, size_t); 51 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, 60 .netif_ifs = NULL, 61 .netif_nifs = 0 62 }; 63 64 #ifdef EFINET_DEBUG 65 static void 66 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode) 67 { 68 int i; 69 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)); 83 printf(" }\n"); 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); 95 } 96 #endif 97 98 static int 99 efinet_match(struct netif *nif, void *machdep_hint) 100 { 101 struct devdesc *dev = machdep_hint; 102 103 if (dev->d_unit == nif->nif_unit) 104 return (1); 105 return(0); 106 } 107 108 static int 109 efinet_probe(struct netif *nif, void *machdep_hint) 110 { 111 112 return (0); 113 } 114 115 static int 116 efinet_put(struct iodesc *desc, void *pkt, size_t len) 117 { 118 struct netif *nif = desc->io_netif; 119 EFI_SIMPLE_NETWORK *net; 120 EFI_STATUS status; 121 void *buf; 122 123 net = nif->nif_devdata; 124 if (net == NULL) 125 return (-1); 126 127 status = net->Transmit(net, 0, len, pkt, 0, 0, 0); 128 if (status != EFI_SUCCESS) 129 return (-1); 130 131 /* Wait for the buffer to be transmitted */ 132 do { 133 buf = 0; /* XXX Is this needed? */ 134 status = net->GetStatus(net, 0, &buf); 135 /* 136 * XXX EFI1.1 and the E1000 card returns a different 137 * address than we gave. Sigh. 138 */ 139 } while (status == EFI_SUCCESS && buf == 0); 140 141 /* XXX How do we deal with status != EFI_SUCCESS now? */ 142 return ((status == EFI_SUCCESS) ? len : -1); 143 } 144 145 static int 146 efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout) 147 { 148 struct netif *nif = desc->io_netif; 149 EFI_SIMPLE_NETWORK *net; 150 EFI_STATUS status; 151 UINTN bufsz; 152 time_t t; 153 char buf[2048]; 154 155 net = nif->nif_devdata; 156 if (net == NULL) 157 return (0); 158 159 t = time(0); 160 while ((time(0) - t) < timeout) { 161 bufsz = sizeof(buf); 162 status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0); 163 if (status == EFI_SUCCESS) { 164 /* 165 * XXX EFI1.1 and the E1000 card trash our 166 * workspace if we do not do this silly copy. 167 * Either they are not respecting the len 168 * value or do not like the alignment. 169 */ 170 if (bufsz > len) 171 bufsz = len; 172 bcopy(buf, pkt, bufsz); 173 return (bufsz); 174 } 175 if (status != EFI_NOT_READY) 176 return (0); 177 } 178 179 return (0); 180 } 181 182 static void 183 efinet_init(struct iodesc *desc, void *machdep_hint) 184 { 185 struct netif *nif = desc->io_netif; 186 EFI_SIMPLE_NETWORK *net; 187 EFI_HANDLE h; 188 EFI_STATUS status; 189 190 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) { 191 printf("Invalid network interface %d\n", nif->nif_unit); 192 return; 193 } 194 195 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private; 196 status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata); 197 if (status != EFI_SUCCESS) { 198 printf("net%d: cannot fetch interface data (status=%lu)\n", 199 nif->nif_unit, EFI_ERROR_CODE(status)); 200 return; 201 } 202 203 net = nif->nif_devdata; 204 if (net->Mode->State == EfiSimpleNetworkStopped) { 205 status = net->Start(net); 206 if (status != EFI_SUCCESS) { 207 printf("net%d: cannot start interface (status=%ld)\n", 208 nif->nif_unit, (long)status); 209 return; 210 } 211 } 212 213 if (net->Mode->State != EfiSimpleNetworkInitialized) { 214 status = net->Initialize(net, 0, 0); 215 if (status != EFI_SUCCESS) { 216 printf("net%d: cannot init. interface (status=%ld)\n", 217 nif->nif_unit, (long)status); 218 return; 219 } 220 } 221 222 if (net->Mode->ReceiveFilterSetting == 0) { 223 UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | 224 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; 225 226 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0); 227 if (status != EFI_SUCCESS) { 228 printf("net%d: cannot set rx. filters (status=%ld)\n", 229 nif->nif_unit, (long)status); 230 return; 231 } 232 } 233 234 #ifdef EFINET_DEBUG 235 dump_mode(net->Mode); 236 #endif 237 238 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6); 239 desc->xid = 1; 240 } 241 242 static void 243 efinet_end(struct netif *nif) 244 { 245 EFI_SIMPLE_NETWORK *net = nif->nif_devdata; 246 247 if (net == NULL) 248 return; 249 250 net->Shutdown(net); 251 } 252 253 static int efinet_dev_init(void); 254 static int efinet_dev_print(int); 255 256 struct devsw efinet_dev = { 257 .dv_name = "net", 258 .dv_type = DEVT_NET, 259 .dv_init = efinet_dev_init, 260 .dv_strategy = net_strategy, 261 .dv_open = net_open, 262 .dv_close = net_close, 263 .dv_ioctl = noioctl, 264 .dv_print = efinet_dev_print, 265 .dv_cleanup = NULL 266 }; 267 268 static int 269 efinet_dev_init() 270 { 271 struct netif_dif *dif; 272 struct netif_stats *stats; 273 EFI_DEVICE_PATH *devpath, *node; 274 EFI_SIMPLE_NETWORK *net; 275 EFI_HANDLE *handles, *handles2; 276 EFI_STATUS status; 277 UINTN sz; 278 int err, i, nifs; 279 280 sz = 0; 281 handles = NULL; 282 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 0); 283 if (status == EFI_BUFFER_TOO_SMALL) { 284 handles = (EFI_HANDLE *)malloc(sz); 285 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 286 handles); 287 if (EFI_ERROR(status)) 288 free(handles); 289 } 290 if (EFI_ERROR(status)) 291 return (efi_status_to_errno(status)); 292 handles2 = (EFI_HANDLE *)malloc(sz); 293 if (handles2 == NULL) { 294 free(handles); 295 return (ENOMEM); 296 } 297 nifs = 0; 298 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) { 299 devpath = efi_lookup_devpath(handles[i]); 300 if (devpath == NULL) 301 continue; 302 if ((node = efi_devpath_last_node(devpath)) == NULL) 303 continue; 304 305 if (DevicePathType(node) != MESSAGING_DEVICE_PATH || 306 DevicePathSubType(node) != MSG_MAC_ADDR_DP) 307 continue; 308 309 /* 310 * Open the network device in exclusive mode. Without this 311 * we will be racing with the UEFI network stack. It will 312 * pull packets off the network leading to lost packets. 313 */ 314 status = BS->OpenProtocol(handles[i], &sn_guid, (void **)&net, 315 IH, 0, EFI_OPEN_PROTOCOL_EXCLUSIVE); 316 if (status != EFI_SUCCESS) { 317 printf("Unable to open network interface %d for " 318 "exclusive access: %d\n", i, EFI_ERROR(status)); 319 } 320 321 handles2[nifs] = handles[i]; 322 nifs++; 323 } 324 free(handles); 325 if (nifs == 0) { 326 err = ENOENT; 327 goto done; 328 } 329 330 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs); 331 if (err != 0) 332 goto done; 333 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); 338 free(stats); 339 efinetif.netif_ifs = NULL; 340 err = ENOMEM; 341 goto done; 342 } 343 efinetif.netif_nifs = nifs; 344 345 for (i = 0; i < nifs; i++) { 346 347 dif = &efinetif.netif_ifs[i]; 348 dif->dif_unit = i; 349 dif->dif_nsel = 1; 350 dif->dif_stats = &stats[i]; 351 dif->dif_private = handles2[i]; 352 } 353 done: 354 free(handles2); 355 return (err); 356 } 357 358 static int 359 efinet_dev_print(int verbose) 360 { 361 CHAR16 *text; 362 EFI_HANDLE h; 363 int unit, ret; 364 365 printf("%s devices:", efinet_dev.dv_name); 366 if ((ret = pager_output("\n")) != 0) 367 return (ret); 368 369 for (unit = 0, h = efi_find_handle(&efinet_dev, 0); 370 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) { 371 printf(" %s%d:", efinet_dev.dv_name, unit); 372 text = efi_devpath_name(efi_lookup_devpath(h)); 373 if (text != NULL) { 374 printf(" %S", text); 375 efi_free_devpath_name(text); 376 } 377 ret = pager_output("\n"); 378 if (ret) 379 break; 380 } 381 return (ret); 382 } 383