]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/efi/libefi/efinet.c
MFC Loader Fixes 2017q1: r311458,r312237,r312314,r312374,r312947,r313042,
[FreeBSD/FreeBSD.git] / sys / boot / efi / libefi / efinet.c
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 <netinet/in.h>
33 #include <netinet/in_systm.h>
34
35 #include <stand.h>
36 #include <net.h>
37 #include <netif.h>
38
39 #include <dev_net.c>
40
41 #include <efi.h>
42 #include <efilib.h>
43
44 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
45
46 static void efinet_end(struct netif *);
47 static int efinet_get(struct iodesc *, void *, size_t, time_t);
48 static void efinet_init(struct iodesc *, void *);
49 static int efinet_match(struct netif *, void *);
50 static int efinet_probe(struct netif *, void *);
51 static int efinet_put(struct iodesc *, void *, size_t);
52
53 struct netif_driver efinetif = {   
54         .netif_bname = "efinet",
55         .netif_match = efinet_match,
56         .netif_probe = efinet_probe,
57         .netif_init = efinet_init,
58         .netif_get = efinet_get,
59         .netif_put = efinet_put,
60         .netif_end = efinet_end,
61         .netif_ifs = NULL,
62         .netif_nifs = 0
63 };
64
65 #ifdef EFINET_DEBUG
66 static void
67 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
68 {
69         int i;
70
71         printf("State                 = %x\n", mode->State);
72         printf("HwAddressSize         = %u\n", mode->HwAddressSize);
73         printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
74         printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
75         printf("NvRamSize             = %u\n", mode->NvRamSize);
76         printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
77         printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
78         printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
79         printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
80         printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
81         printf("MCastFilter           = {");
82         for (i = 0; i < mode->MCastFilterCount; i++)
83                 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
84         printf(" }\n");
85         printf("CurrentAddress        = %s\n",
86             ether_sprintf(mode->CurrentAddress.Addr));
87         printf("BroadcastAddress      = %s\n",
88             ether_sprintf(mode->BroadcastAddress.Addr));
89         printf("PermanentAddress      = %s\n",
90             ether_sprintf(mode->PermanentAddress.Addr));
91         printf("IfType                = %u\n", mode->IfType);
92         printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
93         printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
94         printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
95         printf("MediaPresent          = %d\n", mode->MediaPresent);
96 }
97 #endif
98
99 static int
100 efinet_match(struct netif *nif, void *machdep_hint)
101 {
102         struct devdesc *dev = machdep_hint;
103
104         if (dev->d_unit == nif->nif_unit)
105                 return (1);
106         return(0);
107 }
108
109 static int
110 efinet_probe(struct netif *nif, void *machdep_hint)
111 {
112
113         return (0);
114 }
115
116 static int
117 efinet_put(struct iodesc *desc, void *pkt, size_t len)
118 {
119         struct netif *nif = desc->io_netif;
120         EFI_SIMPLE_NETWORK *net;
121         EFI_STATUS status;
122         void *buf;
123
124         net = nif->nif_devdata;
125         if (net == NULL)
126                 return (-1);
127
128         status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
129         if (status != EFI_SUCCESS)
130                 return (-1);
131
132         /* Wait for the buffer to be transmitted */
133         do {
134                 buf = NULL;     /* XXX Is this needed? */
135                 status = net->GetStatus(net, 0, &buf);
136                 /*
137                  * XXX EFI1.1 and the E1000 card returns a different 
138                  * address than we gave.  Sigh.
139                  */
140         } while (status == EFI_SUCCESS && buf == NULL);
141
142         /* XXX How do we deal with status != EFI_SUCCESS now? */
143         return ((status == EFI_SUCCESS) ? len : -1);
144 }
145
146 static int
147 efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
148 {
149         struct netif *nif = desc->io_netif;
150         EFI_SIMPLE_NETWORK *net;
151         EFI_STATUS status;
152         UINTN bufsz;
153         time_t t;
154         char buf[2048];
155
156         net = nif->nif_devdata;
157         if (net == NULL)
158                 return (0);
159
160         t = time(0);
161         while ((time(0) - t) < timeout) {
162                 bufsz = sizeof(buf);
163                 status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0);
164                 if (status == EFI_SUCCESS) {
165                         /*
166                          * XXX EFI1.1 and the E1000 card trash our
167                          * workspace if we do not do this silly copy.
168                          * Either they are not respecting the len
169                          * value or do not like the alignment.
170                          */
171                         if (bufsz > len)
172                                 bufsz = len;
173                         bcopy(buf, pkt, bufsz);
174                         return (bufsz);
175                 }
176                 if (status != EFI_NOT_READY)
177                         return (0);
178         }
179
180         return (0);
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
191         if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
192                 printf("Invalid network interface %d\n", nif->nif_unit);
193                 return;
194         }
195
196         h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
197         status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata);
198         if (status != EFI_SUCCESS) {
199                 printf("net%d: cannot fetch interface data (status=%lu)\n",
200                     nif->nif_unit, EFI_ERROR_CODE(status));
201                 return;
202         }
203
204         net = nif->nif_devdata;
205         if (net->Mode->State == EfiSimpleNetworkStopped) {
206                 status = net->Start(net);
207                 if (status != EFI_SUCCESS) {
208                         printf("net%d: cannot start interface (status=%ld)\n",
209                             nif->nif_unit, (long)status);
210                         return;
211                 }
212         }
213
214         if (net->Mode->State != EfiSimpleNetworkInitialized) {
215                 status = net->Initialize(net, 0, 0);
216                 if (status != EFI_SUCCESS) {
217                         printf("net%d: cannot init. interface (status=%ld)\n",
218                             nif->nif_unit, (long)status);
219                         return;
220                 }
221         }
222
223         if (net->Mode->ReceiveFilterSetting == 0) {
224                 UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
225                     EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
226
227                 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
228                 if (status != EFI_SUCCESS) {
229                         printf("net%d: cannot set rx. filters (status=%ld)\n",
230                             nif->nif_unit, (long)status);
231                         return;
232                 }
233         }
234
235 #ifdef EFINET_DEBUG
236         dump_mode(net->Mode);
237 #endif
238
239         bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
240         desc->xid = 1;
241 }
242
243 static void
244 efinet_end(struct netif *nif)
245 {
246         EFI_SIMPLE_NETWORK *net = nif->nif_devdata; 
247
248         if (net == NULL)
249                 return;
250
251         net->Shutdown(net);
252 }
253
254 static int efinet_dev_init(void);
255 static int efinet_dev_print(int);
256
257 struct devsw efinet_dev = {
258         .dv_name = "net",
259         .dv_type = DEVT_NET,
260         .dv_init = efinet_dev_init,
261         .dv_strategy = net_strategy,
262         .dv_open = net_open,
263         .dv_close = net_close,
264         .dv_ioctl = noioctl,
265         .dv_print = efinet_dev_print,
266         .dv_cleanup = NULL
267 };
268
269 static int
270 efinet_dev_init()
271 {
272         struct netif_dif *dif;
273         struct netif_stats *stats;
274         EFI_DEVICE_PATH *devpath, *node;
275         EFI_SIMPLE_NETWORK *net;
276         EFI_HANDLE *handles, *handles2;
277         EFI_STATUS status;
278         UINTN sz;
279         int err, i, nifs;
280
281         sz = 0;
282         handles = NULL;
283         status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 0);
284         if (status == EFI_BUFFER_TOO_SMALL) {
285                 handles = (EFI_HANDLE *)malloc(sz);
286                 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &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, 0, EFI_OPEN_PROTOCOL_EXCLUSIVE);
317                 if (status != EFI_SUCCESS) {
318                         printf("Unable to open network interface %d for "
319                             "exclusive access: %d\n", i, EFI_ERROR(status));
320                 }
321
322                 handles2[nifs] = handles[i];
323                 nifs++;
324         }
325         free(handles);
326         if (nifs == 0) {
327                 err = ENOENT;
328                 goto done;
329         }
330
331         err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
332         if (err != 0)
333                 goto done;
334
335         efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
336         stats = calloc(nifs, sizeof(struct netif_stats));
337         if (efinetif.netif_ifs == NULL || stats == NULL) {
338                 free(efinetif.netif_ifs);
339                 free(stats);
340                 efinetif.netif_ifs = NULL;
341                 err = ENOMEM;
342                 goto done;
343         }
344         efinetif.netif_nifs = nifs;
345
346         for (i = 0; i < nifs; i++) {
347
348                 dif = &efinetif.netif_ifs[i];
349                 dif->dif_unit = i;
350                 dif->dif_nsel = 1;
351                 dif->dif_stats = &stats[i];
352                 dif->dif_private = handles2[i];
353         }
354 done:
355         free(handles2);
356         return (err);
357 }
358
359 static int
360 efinet_dev_print(int verbose)
361 {
362         CHAR16 *text;
363         EFI_HANDLE h;
364         int unit, ret = 0;
365
366         printf("%s devices:", efinet_dev.dv_name);
367         if ((ret = pager_output("\n")) != 0)
368                 return (ret);
369
370         for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
371             h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
372                 printf("    %s%d:", efinet_dev.dv_name, unit);
373                 if (verbose) {
374                         text = efi_devpath_name(efi_lookup_devpath(h));
375                         if (text != NULL) {
376                                 printf("    %S", text);
377                                 efi_free_devpath_name(text);
378                         }
379                 }
380                 if ((ret = pager_output("\n")) != 0)
381                         break;
382         }
383         return (ret);
384 }