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