efinet.c revision 1.2.6.2 1 1.2.6.2 tron /* $NetBSD: efinet.c,v 1.2.6.2 2006/05/24 15:48:08 tron Exp $ */
2 1.2.6.2 tron
3 1.2.6.2 tron /*-
4 1.2.6.2 tron * Copyright (c) 2001 Doug Rabson
5 1.2.6.2 tron * All rights reserved.
6 1.2.6.2 tron *
7 1.2.6.2 tron * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 tron * modification, are permitted provided that the following conditions
9 1.2.6.2 tron * are met:
10 1.2.6.2 tron * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 tron * notice, this list of conditions and the following disclaimer.
12 1.2.6.2 tron * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 tron * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 tron * documentation and/or other materials provided with the distribution.
15 1.2.6.2 tron *
16 1.2.6.2 tron * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.6.2 tron * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.6.2 tron * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.6.2 tron * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.6.2 tron * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.6.2 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.6.2 tron * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.6.2 tron * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.6.2 tron * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.6.2 tron * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.6.2 tron * SUCH DAMAGE.
27 1.2.6.2 tron */
28 1.2.6.2 tron
29 1.2.6.2 tron #include <sys/cdefs.h>
30 1.2.6.2 tron /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/efinet.c,v 1.6 2004/01/04 23:28:16 obrien Exp $"); */
31 1.2.6.2 tron
32 1.2.6.2 tron #include <sys/param.h>
33 1.2.6.2 tron #include <netinet/in.h>
34 1.2.6.2 tron #include <netinet/in_systm.h>
35 1.2.6.2 tron
36 1.2.6.2 tron #include <lib/libsa/stand.h>
37 1.2.6.2 tron #include <net.h>
38 1.2.6.2 tron #include <netif.h>
39 1.2.6.2 tron
40 1.2.6.2 tron #include <efi.h>
41 1.2.6.2 tron #include <efilib.h>
42 1.2.6.2 tron
43 1.2.6.2 tron extern struct netif_driver efi_net;
44 1.2.6.2 tron
45 1.2.6.2 tron #ifdef EFINET_DEBUG
46 1.2.6.2 tron static void
47 1.2.6.2 tron dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
48 1.2.6.2 tron {
49 1.2.6.2 tron int i;
50 1.2.6.2 tron
51 1.2.6.2 tron printf("State = %x\n", mode->State);
52 1.2.6.2 tron printf("HwAddressSize = %u\n", mode->HwAddressSize);
53 1.2.6.2 tron printf("MediaHeaderSize = %u\n", mode->MediaHeaderSize);
54 1.2.6.2 tron printf("MaxPacketSize = %u\n", mode->MaxPacketSize);
55 1.2.6.2 tron printf("NvRamSize = %u\n", mode->NvRamSize);
56 1.2.6.2 tron printf("NvRamAccessSize = %u\n", mode->NvRamAccessSize);
57 1.2.6.2 tron printf("ReceiveFilterMask = %x\n", mode->ReceiveFilterMask);
58 1.2.6.2 tron printf("ReceiveFilterSetting = %u\n", mode->ReceiveFilterSetting);
59 1.2.6.2 tron printf("MaxMCastFilterCount = %u\n", mode->MaxMCastFilterCount);
60 1.2.6.2 tron printf("MCastFilterCount = %u\n", mode->MCastFilterCount);
61 1.2.6.2 tron printf("MCastFilter = {");
62 1.2.6.2 tron for (i = 0; i < mode->MCastFilterCount; i++)
63 1.2.6.2 tron printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
64 1.2.6.2 tron printf(" }\n");
65 1.2.6.2 tron printf("CurrentAddress = %s\n",
66 1.2.6.2 tron ether_sprintf(mode->CurrentAddress.Addr));
67 1.2.6.2 tron printf("BroadcastAddress = %s\n",
68 1.2.6.2 tron ether_sprintf(mode->BroadcastAddress.Addr));
69 1.2.6.2 tron printf("PermanentAddress = %s\n",
70 1.2.6.2 tron ether_sprintf(mode->PermanentAddress.Addr));
71 1.2.6.2 tron printf("IfType = %u\n", mode->IfType);
72 1.2.6.2 tron printf("MacAddressChangeable = %d\n", mode->MacAddressChangeable);
73 1.2.6.2 tron printf("MultipleTxSupported = %d\n", mode->MultipleTxSupported);
74 1.2.6.2 tron printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
75 1.2.6.2 tron printf("MediaPresent = %d\n", mode->MediaPresent);
76 1.2.6.2 tron }
77 1.2.6.2 tron #endif
78 1.2.6.2 tron
79 1.2.6.2 tron int
80 1.2.6.2 tron efinet_match(struct netif *nif, void *machdep_hint)
81 1.2.6.2 tron {
82 1.2.6.2 tron
83 1.2.6.2 tron return (1);
84 1.2.6.2 tron }
85 1.2.6.2 tron
86 1.2.6.2 tron int
87 1.2.6.2 tron efinet_probe(struct netif *nif, void *machdep_hint)
88 1.2.6.2 tron {
89 1.2.6.2 tron
90 1.2.6.2 tron return (0);
91 1.2.6.2 tron }
92 1.2.6.2 tron
93 1.2.6.2 tron int
94 1.2.6.2 tron efinet_put(struct iodesc *desc, void *pkt, size_t len)
95 1.2.6.2 tron {
96 1.2.6.2 tron struct netif *nif = desc->io_netif;
97 1.2.6.2 tron EFI_SIMPLE_NETWORK *net;
98 1.2.6.2 tron EFI_STATUS status;
99 1.2.6.2 tron void *buf;
100 1.2.6.2 tron
101 1.2.6.2 tron net = nif->nif_devdata;
102 1.2.6.2 tron
103 1.2.6.2 tron status = net->Transmit(net, 0, len, pkt, 0, 0, 0);
104 1.2.6.2 tron if (status != EFI_SUCCESS)
105 1.2.6.2 tron return -1;
106 1.2.6.2 tron
107 1.2.6.2 tron /* Wait for the buffer to be transmitted */
108 1.2.6.2 tron do {
109 1.2.6.2 tron buf = 0; /* XXX Is this needed? */
110 1.2.6.2 tron status = net->GetStatus(net, 0, &buf);
111 1.2.6.2 tron /*
112 1.2.6.2 tron * XXX EFI1.1 and the E1000 card returns a different
113 1.2.6.2 tron * address than we gave. Sigh.
114 1.2.6.2 tron */
115 1.2.6.2 tron } while (status == EFI_SUCCESS && buf == 0);
116 1.2.6.2 tron
117 1.2.6.2 tron /* XXX How do we deal with status != EFI_SUCCESS now? */
118 1.2.6.2 tron return (status == EFI_SUCCESS) ? len : -1;
119 1.2.6.2 tron }
120 1.2.6.2 tron
121 1.2.6.2 tron
122 1.2.6.2 tron int
123 1.2.6.2 tron efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
124 1.2.6.2 tron {
125 1.2.6.2 tron struct netif *nif = desc->io_netif;
126 1.2.6.2 tron EFI_SIMPLE_NETWORK *net;
127 1.2.6.2 tron EFI_STATUS status;
128 1.2.6.2 tron UINTN bufsz;
129 1.2.6.2 tron time_t t;
130 1.2.6.2 tron char buf[2048];
131 1.2.6.2 tron
132 1.2.6.2 tron net = nif->nif_devdata;
133 1.2.6.2 tron
134 1.2.6.2 tron t = time(0);
135 1.2.6.2 tron while ((time(0) - t) < timeout) {
136 1.2.6.2 tron bufsz = sizeof(buf);
137 1.2.6.2 tron status = net->Receive(net, 0, &bufsz, buf, 0, 0, 0);
138 1.2.6.2 tron if (status == EFI_SUCCESS) {
139 1.2.6.2 tron /*
140 1.2.6.2 tron * XXX EFI1.1 and the E1000 card trash our
141 1.2.6.2 tron * workspace if we do not do this silly copy.
142 1.2.6.2 tron * Either they are not respecting the len
143 1.2.6.2 tron * value or do not like the alignment.
144 1.2.6.2 tron */
145 1.2.6.2 tron if (bufsz > len)
146 1.2.6.2 tron bufsz = len;
147 1.2.6.2 tron bcopy(buf, pkt, bufsz);
148 1.2.6.2 tron return bufsz;
149 1.2.6.2 tron }
150 1.2.6.2 tron if (status != EFI_NOT_READY)
151 1.2.6.2 tron return 0;
152 1.2.6.2 tron }
153 1.2.6.2 tron
154 1.2.6.2 tron return 0;
155 1.2.6.2 tron }
156 1.2.6.2 tron
157 1.2.6.2 tron void
158 1.2.6.2 tron efinet_init(struct iodesc *desc, void *machdep_hint)
159 1.2.6.2 tron {
160 1.2.6.2 tron struct netif *nif = desc->io_netif;
161 1.2.6.2 tron EFI_SIMPLE_NETWORK *net;
162 1.2.6.2 tron EFI_STATUS status;
163 1.2.6.2 tron
164 1.2.6.2 tron net = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
165 1.2.6.2 tron nif->nif_devdata = net;
166 1.2.6.2 tron
167 1.2.6.2 tron if (net->Mode->State == EfiSimpleNetworkStopped) {
168 1.2.6.2 tron status = net->Start(net);
169 1.2.6.2 tron if (status != EFI_SUCCESS) {
170 1.2.6.2 tron printf("net%d: cannot start interface (status=%ld)\n",
171 1.2.6.2 tron nif->nif_unit, status);
172 1.2.6.2 tron return;
173 1.2.6.2 tron }
174 1.2.6.2 tron }
175 1.2.6.2 tron
176 1.2.6.2 tron if (net->Mode->State != EfiSimpleNetworkInitialized) {
177 1.2.6.2 tron status = net->Initialize(net, 0, 0);
178 1.2.6.2 tron if (status != EFI_SUCCESS) {
179 1.2.6.2 tron printf("net%d: cannot init. interface (status=%ld)\n",
180 1.2.6.2 tron nif->nif_unit, status);
181 1.2.6.2 tron return;
182 1.2.6.2 tron }
183 1.2.6.2 tron }
184 1.2.6.2 tron
185 1.2.6.2 tron if (net->Mode->ReceiveFilterSetting == 0) {
186 1.2.6.2 tron UINT32 mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
187 1.2.6.2 tron EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
188 1.2.6.2 tron
189 1.2.6.2 tron status = net->ReceiveFilters(net, mask, 0, FALSE, 0, 0);
190 1.2.6.2 tron if (status != EFI_SUCCESS) {
191 1.2.6.2 tron printf("net%d: cannot set rx. filters (status=%ld)\n",
192 1.2.6.2 tron nif->nif_unit, status);
193 1.2.6.2 tron return;
194 1.2.6.2 tron }
195 1.2.6.2 tron }
196 1.2.6.2 tron
197 1.2.6.2 tron #ifdef EFINET_DEBUG
198 1.2.6.2 tron dump_mode(net->Mode);
199 1.2.6.2 tron #endif
200 1.2.6.2 tron
201 1.2.6.2 tron bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
202 1.2.6.2 tron desc->xid = 1;
203 1.2.6.2 tron
204 1.2.6.2 tron return;
205 1.2.6.2 tron }
206 1.2.6.2 tron
207 1.2.6.2 tron void
208 1.2.6.2 tron efinet_init_driver()
209 1.2.6.2 tron {
210 1.2.6.2 tron EFI_STATUS status;
211 1.2.6.2 tron UINTN sz;
212 1.2.6.2 tron static EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
213 1.2.6.2 tron EFI_HANDLE *handles;
214 1.2.6.2 tron int nifs, i;
215 1.2.6.2 tron #define MAX_INTERFACES 4
216 1.2.6.2 tron static struct netif_dif difs[MAX_INTERFACES];
217 1.2.6.2 tron static struct netif_stats stats[MAX_INTERFACES];
218 1.2.6.2 tron
219 1.2.6.2 tron sz = 0;
220 1.2.6.2 tron status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, 0);
221 1.2.6.2 tron if (status != EFI_BUFFER_TOO_SMALL)
222 1.2.6.2 tron return;
223 1.2.6.2 tron handles = (EFI_HANDLE *) alloc(sz);
224 1.2.6.2 tron status = BS->LocateHandle(ByProtocol, &netid, 0, &sz, handles);
225 1.2.6.2 tron if (EFI_ERROR(status)) {
226 1.2.6.2 tron free(handles, sz);
227 1.2.6.2 tron return;
228 1.2.6.2 tron }
229 1.2.6.2 tron
230 1.2.6.2 tron nifs = sz / sizeof(EFI_HANDLE);
231 1.2.6.2 tron if (nifs > MAX_INTERFACES)
232 1.2.6.2 tron nifs = MAX_INTERFACES;
233 1.2.6.2 tron
234 1.2.6.2 tron efi_net.netif_nifs = nifs;
235 1.2.6.2 tron efi_net.netif_ifs = difs;
236 1.2.6.2 tron
237 1.2.6.2 tron bzero(stats, sizeof(stats));
238 1.2.6.2 tron for (i = 0; i < nifs; i++) {
239 1.2.6.2 tron struct netif_dif *dif = &efi_net.netif_ifs[i];
240 1.2.6.2 tron dif->dif_unit = i;
241 1.2.6.2 tron dif->dif_nsel = 1;
242 1.2.6.2 tron dif->dif_stats = &stats[i];
243 1.2.6.2 tron
244 1.2.6.2 tron BS->HandleProtocol(handles[i], &netid,
245 1.2.6.2 tron (VOID**) &dif->dif_private);
246 1.2.6.2 tron }
247 1.2.6.2 tron
248 1.2.6.2 tron return;
249 1.2.6.2 tron }
250 1.2.6.2 tron
251 1.2.6.2 tron void
252 1.2.6.2 tron efinet_end(struct netif *nif)
253 1.2.6.2 tron {
254 1.2.6.2 tron EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
255 1.2.6.2 tron
256 1.2.6.2 tron net->Shutdown(net);
257 1.2.6.2 tron }
258 1.2.6.2 tron
259 1.2.6.2 tron struct netif_driver efi_net = {
260 1.2.6.2 tron "net", /* netif_bname */
261 1.2.6.2 tron efinet_match, /* netif_match */
262 1.2.6.2 tron efinet_probe, /* netif_probe */
263 1.2.6.2 tron efinet_init, /* netif_init */
264 1.2.6.2 tron efinet_get, /* netif_get */
265 1.2.6.2 tron efinet_put, /* netif_put */
266 1.2.6.2 tron efinet_end, /* netif_end */
267 1.2.6.2 tron 0, /* netif_ifs */
268 1.2.6.2 tron 0 /* netif_nifs */
269 1.2.6.2 tron };
270 1.2.6.2 tron
271