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 <net/ethernet.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 <efi.h>
40 #include <efilib.h>
41
42 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL;
43
44 static void efinet_end(struct netif *);
45 static ssize_t efinet_get(struct iodesc *, void **, time_t);
46 static void efinet_init(struct iodesc *, void *);
47 static int efinet_match(struct netif *, void *);
48 static int efinet_probe(struct netif *, void *);
49 static ssize_t efinet_put(struct iodesc *, void *, size_t);
50
51 struct netif_driver efinetif = {
52 .netif_bname = "efinet",
53 .netif_match = efinet_match,
54 .netif_probe = efinet_probe,
55 .netif_init = efinet_init,
56 .netif_get = efinet_get,
57 .netif_put = efinet_put,
58 .netif_end = efinet_end,
59 .netif_ifs = NULL,
60 .netif_nifs = 0
61 };
62
63 #ifdef EFINET_DEBUG
64 static void
65 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
66 {
67 int i;
68
69 printf("State = %x\n", mode->State);
70 printf("HwAddressSize = %u\n", mode->HwAddressSize);
71 printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize);
72 printf("MaxPacketSize = %u\n", mode->MaxPacketSize);
73 printf("NvRamSize = %u\n", mode->NvRamSize);
74 printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize);
75 printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask);
76 printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting);
77 printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount);
78 printf("MCastFilterCount = %u\n", mode->MCastFilterCount);
79 printf("MCastFilter = {");
80 for (i = 0; i < mode->MCastFilterCount; i++)
81 printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
82 printf(" }\n");
83 printf("CurrentAddress = %s\n",
84 ether_sprintf(mode->CurrentAddress.Addr));
85 printf("BroadcastAddress = %s\n",
86 ether_sprintf(mode->BroadcastAddress.Addr));
87 printf("PermanentAddress = %s\n",
88 ether_sprintf(mode->PermanentAddress.Addr));
89 printf("IfType = %u\n", mode->IfType);
90 printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable);
91 printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported);
92 printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
93 printf("MediaPresent = %d\n", mode->MediaPresent);
94 }
95 #endif
96
97 static int
98 efinet_match(struct netif *nif, void *machdep_hint)
99 {
100 struct devdesc *dev = machdep_hint;
101
102 if (dev->d_unit == nif->nif_unit)
103 return (1);
104 return(0);
105 }
106
107 static int
108 efinet_probe(struct netif *nif, void *machdep_hint)
109 {
110
111 return (0);
112 }
113
114 static ssize_t
115 efinet_put(struct iodesc *desc, void *pkt, size_t len)
116 {
117 struct netif *nif = desc->io_netif;
118 EFI_SIMPLE_NETWORK *net;
119 EFI_STATUS status;
120 void *buf;
121
122 net = nif->nif_devdata;
123 if (net == NULL)
124 return (-1);
125
126 status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
127 if (status != EFI_SUCCESS)
128 return (-1);
129
130 /* Wait for the buffer to be transmitted */
131 do {
132 buf = NULL; /* XXX Is this needed? */
133 status = net->GetStatus(net, NULL, &buf);
134 /*
135 * XXX EFI1.1 and the E1000 card returns a different
136 * address than we gave. Sigh.
137 */
138 } while (status == EFI_SUCCESS && buf == NULL);
139
140 /* XXX How do we deal with status != EFI_SUCCESS now? */
141 return ((status == EFI_SUCCESS) ? len : -1);
142 }
143
144 static ssize_t
145 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
146 {
147 struct netif *nif = desc->io_netif;
148 EFI_SIMPLE_NETWORK *net;
149 EFI_STATUS status;
150 UINTN bufsz;
151 time_t t;
152 char *buf, *ptr;
153 ssize_t ret = -1;
154
155 net = nif->nif_devdata;
156 if (net == NULL)
157 return (ret);
158
159 bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
160 buf = malloc(bufsz + ETHER_ALIGN);
161 if (buf == NULL)
162 return (ret);
163 ptr = buf + ETHER_ALIGN;
164
165 t = getsecs();
166 while ((getsecs() - t) < timeout) {
167 status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
168 if (status == EFI_SUCCESS) {
169 *pkt = buf;
170 ret = (ssize_t)bufsz;
171 break;
172 }
173 if (status != EFI_NOT_READY)
174 break;
175 }
176
177 if (ret == -1)
178 free(buf);
179 return (ret);
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 UINT32 mask;
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=%lu)\n",
209 nif->nif_unit, EFI_ERROR_CODE(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=%lu)\n",
218 nif->nif_unit, EFI_ERROR_CODE(status));
219 return;
220 }
221 }
222
223 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
224 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
225
226 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
227 if (status != EFI_SUCCESS)
228 printf("net%d: cannot set rx. filters (status=%lu)\n",
229 nif->nif_unit, EFI_ERROR_CODE(status));
230
231 #ifdef EFINET_DEBUG
232 dump_mode(net->Mode);
233 #endif
234
235 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
236 desc->xid = 1;
237 }
238
239 static void
240 efinet_end(struct netif *nif)
241 {
242 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
243
244 if (net == NULL)
245 return;
246
247 net->Shutdown(net);
248 }
249
250 static int efinet_dev_init(void);
251 static int efinet_dev_print(int);
252
253 struct devsw efinet_dev = {
254 .dv_name = "net",
255 .dv_type = DEVT_NET,
256 .dv_init = efinet_dev_init,
257 .dv_strategy = NULL, /* Will be set in efinet_dev_init */
258 .dv_open = NULL, /* Will be set in efinet_dev_init */
259 .dv_close = NULL, /* Will be set in efinet_dev_init */
260 .dv_ioctl = noioctl,
261 .dv_print = efinet_dev_print,
262 .dv_cleanup = NULL
263 };
264
265 static int
266 efinet_dev_init()
267 {
268 struct netif_dif *dif;
269 struct netif_stats *stats;
270 EFI_DEVICE_PATH *devpath, *node;
271 EFI_SIMPLE_NETWORK *net;
272 EFI_HANDLE *handles, *handles2;
273 EFI_STATUS status;
274 UINTN sz;
275 int err, i, nifs;
276 extern struct devsw netdev;
277
278 sz = 0;
279 handles = NULL;
280 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
281 if (status == EFI_BUFFER_TOO_SMALL) {
282 handles = (EFI_HANDLE *)malloc(sz);
283 status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
284 handles);
285 if (EFI_ERROR(status))
286 free(handles);
287 }
288 if (EFI_ERROR(status))
289 return (efi_status_to_errno(status));
290 handles2 = (EFI_HANDLE *)malloc(sz);
291 if (handles2 == NULL) {
292 free(handles);
293 return (ENOMEM);
294 }
295 nifs = 0;
296 for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
297 devpath = efi_lookup_devpath(handles[i]);
298 if (devpath == NULL)
299 continue;
300 if ((node = efi_devpath_last_node(devpath)) == NULL)
301 continue;
302
303 if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
304 DevicePathSubType(node) != MSG_MAC_ADDR_DP)
305 continue;
306
307 /*
308 * Open the network device in exclusive mode. Without this
309 * we will be racing with the UEFI network stack. It will
310 * pull packets off the network leading to lost packets.
311 */
312 status = BS->OpenProtocol(handles[i], &sn_guid, (void **)&net,
313 IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE);
314 if (status != EFI_SUCCESS) {
315 printf("Unable to open network interface %d for "
316 "exclusive access: %lu\n", i,
317 EFI_ERROR_CODE(status));
318 }
319
320 handles2[nifs] = handles[i];
321 nifs++;
322 }
323 free(handles);
324 if (nifs == 0) {
325 err = ENOENT;
326 goto done;
327 }
328
329 err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
330 if (err != 0)
331 goto done;
332
333 efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
334 stats = calloc(nifs, sizeof(struct netif_stats));
335 if (efinetif.netif_ifs == NULL || stats == NULL) {
336 free(efinetif.netif_ifs);
337 free(stats);
338 efinetif.netif_ifs = NULL;
339 err = ENOMEM;
340 goto done;
341 }
342 efinetif.netif_nifs = nifs;
343
344 for (i = 0; i < nifs; i++) {
345
346 dif = &efinetif.netif_ifs[i];
347 dif->dif_unit = i;
348 dif->dif_nsel = 1;
349 dif->dif_stats = &stats[i];
350 dif->dif_private = handles2[i];
351 }
352
353 efinet_dev.dv_open = netdev.dv_open;
354 efinet_dev.dv_close = netdev.dv_close;
355 efinet_dev.dv_strategy = netdev.dv_strategy;
356
357 done:
358 free(handles2);
359 return (err);
360 }
361
362 static int
363 efinet_dev_print(int verbose)
364 {
365 CHAR16 *text;
366 EFI_HANDLE h;
367 int unit, ret = 0;
368
369 printf("%s devices:", efinet_dev.dv_name);
370 if ((ret = pager_output("\n")) != 0)
371 return (ret);
372
373 for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
374 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
375 printf(" %s%d:", efinet_dev.dv_name, unit);
376 if (verbose) {
377 text = efi_devpath_name(efi_lookup_devpath(h));
378 if (text != NULL) {
379 printf(" %S", text);
380 efi_free_devpath_name(text);
381 }
382 }
383 if ((ret = pager_output("\n")) != 0)
384 break;
385 }
386 return (ret);
387 }