boot/efi: Bring in two more TianoCore EDK II headers.
[dragonfly.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: head/sys/boot/efi/libefi/efinet.c 295210 2016-02-03 14:34:25Z andrew $");
30
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34
35 #include <stand.h>
36 #include <stdarg.h>
37 #include <net.h>
38 #include <netif.h>
39
40 #include <dev_net.c>
41
42 #include <efi.h>
43 #include <efilib.h>
44
45 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
46
47 static void efinet_end(struct netif *);
48 static int efinet_get(struct iodesc *, void *, size_t, time_t);
49 static void efinet_init(struct iodesc *, void *);
50 static int efinet_match(struct netif *, void *);
51 static int efinet_probe(struct netif *, void *);
52 static int efinet_put(struct iodesc *, void *, size_t);
53
54 struct netif_driver efinetif = {   
55         .netif_bname = "efinet",
56         .netif_match = efinet_match,
57         .netif_probe = efinet_probe,
58         .netif_init = efinet_init,
59         .netif_get = efinet_get,
60         .netif_put = efinet_put,
61         .netif_end = efinet_end,
62         .netif_ifs = NULL,
63         .netif_nifs = 0
64 };
65
66 #ifdef EFINET_DEBUG
67 static void
68 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
69 {
70         int i;
71
72         printf("State                 = %x\n", mode->State);
73         printf("HwAddressSize         = %u\n", mode->HwAddressSize);
74         printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
75         printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
76         printf("NvRamSize             = %u\n", mode->NvRamSize);
77         printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
78         printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
79         printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
80         printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
81         printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
82         printf("MCastFilter           = {");
83         for (i = 0; i < mode->MCastFilterCount; i++)
84                 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
85         printf(" }\n");
86         printf("CurrentAddress        = %s\n",
87             ether_sprintf(mode->CurrentAddress.Addr));
88         printf("BroadcastAddress      = %s\n",
89             ether_sprintf(mode->BroadcastAddress.Addr));
90         printf("PermanentAddress      = %s\n",
91             ether_sprintf(mode->PermanentAddress.Addr));
92         printf("IfType                = %u\n", mode->IfType);
93         printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
94         printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
95         printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
96         printf("MediaPresent          = %d\n", mode->MediaPresent);
97 }
98 #endif
99
100 static int
101 efinet_match(struct netif *nif, void *machdep_hint)
102 {
103         struct efi_devdesc *dev = machdep_hint;
104
105         if (dev->d_kind.efidisk.unit - 1 == nif->nif_unit)
106                 return (1);
107         return(0);
108 }
109
110 static int
111 efinet_probe(struct netif *nif, void *machdep_hint)
112 {
113
114         return (0);
115 }
116
117 static int
118 efinet_put(struct iodesc *desc, void *pkt, size_t len)
119 {
120         struct netif *nif = desc->io_netif;
121         EFI_SIMPLE_NETWORK *net;
122         EFI_STATUS status;
123         void *buf;
124
125         net = nif->nif_devdata;
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
157         t = time(0);
158         while ((time(0) - t) < timeout) {
159                 bufsz = sizeof(buf);
160                 status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0);
161                 if (status == EFI_SUCCESS) {
162                         /*
163                          * XXX EFI1.1 and the E1000 card trash our
164                          * workspace if we do not do this silly copy.
165                          * Either they are not respecting the len
166                          * value or do not like the alignment.
167                          */
168                         if (bufsz > len)
169                                 bufsz = len;
170                         bcopy(buf, pkt, bufsz);
171                         return (bufsz);
172                 }
173                 if (status != EFI_NOT_READY)
174                         return (0);
175         }
176
177         return (0);
178 }
179
180 static void
181 efinet_init(struct iodesc *desc, void *machdep_hint)
182 {
183         struct netif *nif = desc->io_netif;
184         EFI_SIMPLE_NETWORK *net;
185         EFI_HANDLE h;
186         EFI_STATUS status;
187
188         if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
189                 printf("Invalid network interface %d\n", nif->nif_unit);
190                 return;
191         }
192
193         h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
194         status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata);
195         if (status != EFI_SUCCESS) {
196                 printf("net%d: cannot start interface (status=%lu)\n",
197                     nif->nif_unit, EFI_ERROR_CODE(status));
198                 return;
199         }
200
201         net = nif->nif_devdata;
202         if (net->Mode->State == EfiSimpleNetworkStopped) {
203                 status = net->Start(net);
204                 if (status != EFI_SUCCESS) {
205                         printf("net%d: cannot start interface (status=%ld)\n",
206                             nif->nif_unit, (long)status);
207                         return;
208                 }
209         }
210
211         if (net->Mode->State != EfiSimpleNetworkInitialized) {
212                 status = net->Initialize(net, 0, 0);
213                 if (status != EFI_SUCCESS) {
214                         printf("net%d: cannot init. interface (status=%ld)\n",
215                             nif->nif_unit, (long)status);
216                         return;
217                 }
218         }
219
220         if (net->Mode->ReceiveFilterSetting == 0) {
221                 UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
222                     EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
223
224                 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
225                 if (status != EFI_SUCCESS) {
226                         printf("net%d: cannot set rx. filters (status=%ld)\n",
227                             nif->nif_unit, (long)status);
228                         return;
229                 }
230         }
231
232 #ifdef EFINET_DEBUG
233         dump_mode(net->Mode);
234 #endif
235
236         bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
237         desc->xid = 1;
238 }
239
240 static void
241 efinet_end(struct netif *nif)
242 {
243         EFI_SIMPLE_NETWORK *net = nif->nif_devdata; 
244
245         net->Shutdown(net);
246 }
247
248 static int efinet_dev_init(void);
249 static void efinet_dev_print(int);
250
251 struct devsw efinet_dev = {
252         .dv_name = "net",
253         .dv_type = DEVT_NET,
254         .dv_init = efinet_dev_init,
255         .dv_strategy = net_strategy,
256         .dv_open = net_open,
257         .dv_close = net_close,
258         .dv_ioctl = noioctl,
259         .dv_print = efinet_dev_print,
260         .dv_cleanup = NULL
261 };
262
263 static int
264 efinet_dev_init(void)
265 {
266         struct netif_dif *dif;
267         struct netif_stats *stats;
268         EFI_HANDLE *handles;
269         EFI_STATUS status;
270         UINTN sz;
271         int err, i, nifs;
272
273         sz = 0;
274         handles = NULL;
275         status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 0);
276         if (status == EFI_BUFFER_TOO_SMALL) {
277                 handles = (EFI_HANDLE *)malloc(sz);
278                 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz,
279                     handles);
280                 if (EFI_ERROR(status))
281                         free(handles);
282         }
283         if (EFI_ERROR(status))
284                 return (efi_status_to_errno(status));
285         nifs = sz / sizeof(EFI_HANDLE);
286         err = efi_register_handles(&efinet_dev, handles, NULL, nifs);
287         free(handles);
288         if (err != 0)
289                 return (err);
290
291         efinetif.netif_nifs = nifs;
292         efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
293
294         stats = calloc(nifs, sizeof(struct netif_stats));
295
296         for (i = 0; i < nifs; i++) {
297                 EFI_SIMPLE_NETWORK *net;
298                 EFI_HANDLE h;
299
300                 dif = &efinetif.netif_ifs[i];
301                 dif->dif_unit = -1;
302
303                 h = efi_find_handle(&efinet_dev, i);
304
305                 /*
306                  * Open the network device in exclusive mode. Without this
307                  * we will be racing with the UEFI network stack. It will
308                  * pull packets off the network leading to lost packets.
309                  */
310                 status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
311                     IH, 0, EFI_OPEN_PROTOCOL_EXCLUSIVE);
312                 if (status != EFI_SUCCESS) {
313                         printf("Unable to open network interface %d for "
314                             "exclusive access\n", i);
315                 }
316
317                 dif->dif_unit = i;
318                 dif->dif_nsel = 1;
319                 dif->dif_stats = &stats[i];
320                 dif->dif_private = h;
321         }
322
323         return (0);
324 }
325
326 static void
327 efinet_dev_print(int verbose)
328 {
329         char line[80];
330         EFI_HANDLE h;
331         int unit;
332
333         for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
334             h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
335                 sprintf(line, "    %s%d:\n", efinet_dev.dv_name, unit);
336                 pager_output(line);
337         }
338 }