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
dump_mode(EFI_SIMPLE_NETWORK_MODE * mode)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
efinet_match(struct netif * nif,void * machdep_hint)100 efinet_match(struct netif *nif, void *machdep_hint)
101 {
102
103 return (1);
104 }
105
106 static int
efinet_probe(struct netif * nif,void * machdep_hint)107 efinet_probe(struct netif *nif, void *machdep_hint)
108 {
109
110 return (0);
111 }
112
113 static int
efinet_put(struct iodesc * desc,void * pkt,size_t len)114 efinet_put(struct iodesc *desc, void *pkt, size_t len)
115 {
116 struct netif *nif = desc->io_netif;
117 EFI_SIMPLE_NETWORK *net;
118 EFI_STATUS status;
119 void *buf;
120
121 net = nif->nif_devdata;
122
123 status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
124 if (status != EFI_SUCCESS)
125 return (-1);
126
127 /* Wait for the buffer to be transmitted */
128 do {
129 buf = 0; /* XXX Is this needed? */
130 status = net->GetStatus(net, 0, &buf);
131 /*
132 * XXX EFI1.1 and the E1000 card returns a different
133 * address than we gave. Sigh.
134 */
135 } while (status == EFI_SUCCESS && buf == 0);
136
137 /* XXX How do we deal with status != EFI_SUCCESS now? */
138 return ((status == EFI_SUCCESS) ? len : -1);
139 }
140
141 static int
efinet_get(struct iodesc * desc,void * pkt,size_t len,time_t timeout)142 efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
143 {
144 struct netif *nif = desc->io_netif;
145 EFI_SIMPLE_NETWORK *net;
146 EFI_STATUS status;
147 UINTN bufsz;
148 time_t t;
149 char buf[2048];
150
151 net = nif->nif_devdata;
152
153 t = time(0);
154 while ((time(0) - t) < timeout) {
155 bufsz = sizeof(buf);
156 status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0);
157 if (status == EFI_SUCCESS) {
158 /*
159 * XXX EFI1.1 and the E1000 card trash our
160 * workspace if we do not do this silly copy.
161 * Either they are not respecting the len
162 * value or do not like the alignment.
163 */
164 if (bufsz > len)
165 bufsz = len;
166 bcopy(buf, pkt, bufsz);
167 return (bufsz);
168 }
169 if (status != EFI_NOT_READY)
170 return (0);
171 }
172
173 return (0);
174 }
175
176 static void
efinet_init(struct iodesc * desc,void * machdep_hint)177 efinet_init(struct iodesc *desc, void *machdep_hint)
178 {
179 struct netif *nif = desc->io_netif;
180 EFI_SIMPLE_NETWORK *net;
181 EFI_HANDLE h;
182 EFI_STATUS status;
183
184 h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
185 status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata);
186 if (status != EFI_SUCCESS) {
187 printf("net%d: cannot start interface (status=%ld)\n",
188 nif->nif_unit, (long)status);
189 return;
190 }
191
192 net = nif->nif_devdata;
193 if (net->Mode->State == EfiSimpleNetworkStopped) {
194 status = net->Start(net);
195 if (status != EFI_SUCCESS) {
196 printf("net%d: cannot start interface (status=%ld)\n",
197 nif->nif_unit, (long)status);
198 return;
199 }
200 }
201
202 if (net->Mode->State != EfiSimpleNetworkInitialized) {
203 status = net->Initialize(net, 0, 0);
204 if (status != EFI_SUCCESS) {
205 printf("net%d: cannot init. interface (status=%ld)\n",
206 nif->nif_unit, (long)status);
207 return;
208 }
209 }
210
211 if (net->Mode->ReceiveFilterSetting == 0) {
212 UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
213 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
214
215 status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
216 if (status != EFI_SUCCESS) {
217 printf("net%d: cannot set rx. filters (status=%ld)\n",
218 nif->nif_unit, (long)status);
219 return;
220 }
221 }
222
223 #ifdef EFINET_DEBUG
224 dump_mode(net->Mode);
225 #endif
226
227 bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
228 desc->xid = 1;
229 }
230
231 static void
efinet_end(struct netif * nif)232 efinet_end(struct netif *nif)
233 {
234 EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
235
236 net->Shutdown(net);
237 }
238
239 static int efinet_dev_init(void);
240 static void efinet_dev_print(int);
241
242 struct devsw efinet_dev = {
243 .dv_name = "net",
244 .dv_type = DEVT_NET,
245 .dv_init = efinet_dev_init,
246 .dv_strategy = net_strategy,
247 .dv_open = net_open,
248 .dv_close = net_close,
249 .dv_ioctl = noioctl,
250 .dv_print = efinet_dev_print,
251 .dv_cleanup = NULL
252 };
253
254 static int
efinet_dev_init()255 efinet_dev_init()
256 {
257 struct netif_dif *dif;
258 struct netif_stats *stats;
259 EFI_HANDLE *handles;
260 EFI_STATUS status;
261 UINTN sz;
262 int err, i, nifs;
263
264 sz = 0;
265 handles = NULL;
266 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz, 0);
267 if (status == EFI_BUFFER_TOO_SMALL) {
268 handles = (EFI_HANDLE *)malloc(sz);
269 status = BS->LocateHandle(ByProtocol, &sn_guid, 0, &sz,
270 handles);
271 if (EFI_ERROR(status))
272 free(handles);
273 }
274 if (EFI_ERROR(status))
275 return (efi_status_to_errno(status));
276 nifs = sz / sizeof(EFI_HANDLE);
277 err = efi_register_handles(&efinet_dev, handles, NULL, nifs);
278 free(handles);
279 if (err != 0)
280 return (err);
281
282 efinetif.netif_nifs = nifs;
283 efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
284
285 stats = calloc(nifs, sizeof(struct netif_stats));
286
287 for (i = 0; i < nifs; i++) {
288 dif = &efinetif.netif_ifs[i];
289 dif->dif_unit = i;
290 dif->dif_nsel = 1;
291 dif->dif_stats = &stats[i];
292 dif->dif_private = efi_find_handle(&efinet_dev, i);
293 }
294
295 return (0);
296 }
297
298 static void
efinet_dev_print(int verbose)299 efinet_dev_print(int verbose)
300 {
301 char line[80];
302 EFI_HANDLE h;
303 int unit;
304
305 for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
306 h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
307 sprintf(line, " %s%d:\n", efinet_dev.dv_name, unit);
308 pager_output(line);
309 }
310 }
311