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