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 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <net/ethernet.h> 33 #include <netinet/in.h> 34 #include <netinet/in_systm.h> 35 36 #include <stand.h> 37 #include <net.h> 38 #include <netif.h> 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 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); 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 ssize_t 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, NULL, NULL, NULL); 128 if (status != EFI_SUCCESS) 129 return (-1); 130 131 /* Wait for the buffer to be transmitted */ 132 do { 133 buf = NULL; /* XXX Is this needed? */ 134 status = net->GetStatus(net, NULL, &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 == NULL); 140 141 /* XXX How do we deal with status != EFI_SUCCESS now? */ 142 return ((status == EFI_SUCCESS) ? len : -1); 143 } 144 145 static ssize_t 146 efinet_get(struct iodesc *desc, void **pkt, 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, *ptr; 154 ssize_t ret = -1; 155 156 net = nif->nif_devdata; 157 if (net == NULL) 158 return (ret); 159 160 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN; 161 buf = malloc(bufsz + ETHER_ALIGN); 162 if (buf == NULL) 163 return (ret); 164 ptr = buf + ETHER_ALIGN; 165 166 t = getsecs(); 167 while ((getsecs() - t) < timeout) { 168 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL); 169 if (status == EFI_SUCCESS) { 170 *pkt = buf; 171 ret = (ssize_t)bufsz; 172 break; 173 } 174 if (status != EFI_NOT_READY) 175 break; 176 } 177 178 if (ret == -1) 179 free(buf); 180 return (ret); 181 } 182 183 static void 184 efinet_init(struct iodesc *desc, void *machdep_hint) 185 { 186 struct netif *nif = desc->io_netif; 187 EFI_SIMPLE_NETWORK *net; 188 EFI_HANDLE h; 189 EFI_STATUS status; 190 UINT32 mask; 191 192 if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) { 193 printf("Invalid network interface %d\n", nif->nif_unit); 194 return; 195 } 196 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)); 202 return; 203 } 204 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)); 211 return; 212 } 213 } 214 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)); 220 return; 221 } 222 } 223 224 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | 225 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST; 226 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)); 231 return; 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 = NULL, /* Will be set in efinet_dev_init */ 261 .dv_open = NULL, /* Will be set in efinet_dev_init */ 262 .dv_close = NULL, /* Will be set in efinet_dev_init */ 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 extern struct devsw netdev; 280 281 sz = 0; 282 handles = NULL; 283 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL); 284 if (status == EFI_BUFFER_TOO_SMALL) { 285 handles = (EFI_HANDLE *)malloc(sz); 286 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, 287 handles); 288 if (EFI_ERROR(status)) 289 free(handles); 290 } 291 if (EFI_ERROR(status)) 292 return (efi_status_to_errno(status)); 293 handles2 = (EFI_HANDLE *)malloc(sz); 294 if (handles2 == NULL) { 295 free(handles); 296 return (ENOMEM); 297 } 298 nifs = 0; 299 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) { 300 devpath = efi_lookup_devpath(handles[i]); 301 if (devpath == NULL) 302 continue; 303 if ((node = efi_devpath_last_node(devpath)) == NULL) 304 continue; 305 306 if (DevicePathType(node) != MESSAGING_DEVICE_PATH || 307 DevicePathSubType(node) != MSG_MAC_ADDR_DP) 308 continue; 309 310 /* 311 * Open the network device in exclusive mode. Without this 312 * we will be racing with the UEFI network stack. It will 313 * pull packets off the network leading to lost packets. 314 */ 315 status = BS->OpenProtocol(handles[i], &sn_guid, (void **)&net, 316 IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE); 317 if (status != EFI_SUCCESS) { 318 printf("Unable to open network interface %d for " 319 "exclusive access: %lu\n", i, 320 EFI_ERROR_CODE(status)); 321 } 322 323 handles2[nifs] = handles[i]; 324 nifs++; 325 } 326 free(handles); 327 if (nifs == 0) { 328 err = ENOENT; 329 goto done; 330 } 331 332 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs); 333 if (err != 0) 334 goto done; 335 336 efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif)); 337 stats = calloc(nifs, sizeof(struct netif_stats)); 338 if (efinetif.netif_ifs == NULL || stats == NULL) { 339 free(efinetif.netif_ifs); 340 free(stats); 341 efinetif.netif_ifs = NULL; 342 err = ENOMEM; 343 goto done; 344 } 345 efinetif.netif_nifs = nifs; 346 347 for (i = 0; i < nifs; i++) { 348 349 dif = &efinetif.netif_ifs[i]; 350 dif->dif_unit = i; 351 dif->dif_nsel = 1; 352 dif->dif_stats = &stats[i]; 353 dif->dif_private = handles2[i]; 354 } 355 356 efinet_dev.dv_open = netdev.dv_open; 357 efinet_dev.dv_close = netdev.dv_close; 358 efinet_dev.dv_strategy = netdev.dv_strategy; 359 360 done: 361 free(handles2); 362 return (err); 363 } 364 365 static int 366 efinet_dev_print(int verbose) 367 { 368 CHAR16 *text; 369 EFI_HANDLE h; 370 int unit, ret = 0; 371 372 printf("%s devices:", efinet_dev.dv_name); 373 if ((ret = pager_output("\n")) != 0) 374 return (ret); 375 376 for (unit = 0, h = efi_find_handle(&efinet_dev, 0); 377 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) { 378 printf(" %s%d:", efinet_dev.dv_name, unit); 379 if (verbose) { 380 text = efi_devpath_name(efi_lookup_devpath(h)); 381 if (text != NULL) { 382 printf(" %S", text); 383 efi_free_devpath_name(text); 384 } 385 } 386 if ((ret = pager_output("\n")) != 0) 387 break; 388 } 389 return (ret); 390 } 391