if_prom.c revision 1.8 1 /* $NetBSD: if_prom.c,v 1.8 2011/01/09 16:28:40 tsutsui Exp $ */
2
3 /* Copyright (c) 1999 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Gregory McGarry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/types.h>
33
34 #include <net/if_ether.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38
39 #include <lib/libsa/stand.h>
40 #include <lib/libsa/net.h>
41 #include <lib/libsa/netif.h>
42 #include <lib/libsa/dev_net.h>
43 #include <lib/libkern/libkern.h>
44
45 #include <machine/dec_prom.h>
46 #include <stand/common/common.h>
47
48 #ifdef NET_DEBUG
49 void dump_packet_info(void *, int);
50 #endif
51
52 /*
53 * For some reason the proms won't pass arp responses back to us. I
54 * have checked if the first parameter to bootread/bootwrite do anything
55 * but it doesn't appear so. Therefore, we stop the upper layers from
56 * sending arp requests in the first place, by monitoring packets which
57 * come in and filling the arp cache ourselves. - gmcgarry
58 */
59 #ifdef FILL_ARPCACHE
60 struct arp_list {
61 struct in_addr addr;
62 u_char ea[6];
63 };
64 extern struct arp_list arp_list[8];
65 extern int arp_num;
66 void fill_arpcache(void *, int);
67 #endif
68
69 /* forward declarations */
70 int prom_probe(struct netif *, void *);
71 int prom_match(struct netif *, void *);
72 void prom_init(struct iodesc *, void *);
73 int prom_get(struct iodesc *, void *, size_t, saseconds_t);
74 int prom_put(struct iodesc *, void *, size_t);
75 void prom_end(struct netif *);
76
77 extern struct netif_stats prom_stats[];
78 struct netif_dif prom_ifs[] = {
79 /* dif_unit dif_nsel dif_stats dif_private */
80 { 0, 1, &prom_stats[0], 0, },
81 };
82 #define NPROM_IFS (sizeof(prom_ifs) / sizeof(prom_ifs[0]))
83 struct netif_stats prom_stats[NPROM_IFS];
84
85 struct netif_driver prom_netif_driver = {
86 "prom", /* netif_bname */
87 prom_match, /* netif_match */
88 prom_probe, /* netif_probe */
89 prom_init, /* netif_init */
90 prom_get, /* netif_get */
91 prom_put, /* netif_put */
92 prom_end, /* netif_end */
93 prom_ifs, /* netif_ifs */
94 NPROM_IFS /* netif_nifs */
95 };
96
97 static int sc_fd; /* PROM file id */
98
99 int
100 prom_match(struct netif *nif, void *machdep_hint)
101 {
102
103 #ifdef NET_DEBUG
104 printf("prom_match: called\n");
105 #endif
106 return (1);
107 }
108
109
110 int
111 prom_probe(struct netif *nif, void *machdep_hint)
112 {
113
114 #ifdef NET_DEBUG
115 printf("prom_probe: called\n");
116 #endif
117 return 0;
118 }
119
120
121 void
122 prom_init(struct iodesc *desc, void *machdep_hint)
123 {
124 char *device =
125 ((struct netif *)desc->io_netif)->nif_driver->netif_bname;
126 char *enet;
127 uint8_t *cp, *dest;
128 int i;
129
130 #ifdef NET_DEBUG
131 printf("prom_init: called\n");
132 #endif
133
134 try_bootp = 1;
135
136 /*
137 * Get our hardware address (this prom call is one of the rare ones
138 * which is the same for new and old proms)
139 */
140 enet = (*callv->_getenv)("enet");
141
142 if (enet == NULL) {
143 printf("No `enet' environment variable found.\n"
144 "Set MAC address to `enet' manually by setenv command.\n");
145 (*callv->_halt)((int *)0, 0); /* XXX */
146 /* NOTREACHED */
147 }
148
149 #ifdef NET_DEBUG
150 if (debug)
151 printf("enet=%s\n", enet);
152 #endif
153
154 #define atox(c) (((c) < '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
155
156 cp = (uint8_t *)enet;
157 dest = desc->myea;
158 for (i = 0; i < 6; i++) {
159 if (isxdigit(*cp)) {
160 *dest = atox(*cp);
161 cp++;
162 if (isxdigit(*cp)) {
163 *dest = (*dest << 4) | atox(*cp);
164 cp++;
165 }
166 }
167 dest++;
168 cp++; /* skip '-' or ':' etc. */
169 }
170
171 desc->xid = 0x66d30000;
172
173 if (callv == &callvec)
174 sc_fd = prom_open(device, 0);
175 else
176 sc_fd = (*callv->_bootinit)(device);
177
178 if (sc_fd < 0)
179 printf("problem initialising device\n");
180 }
181
182
183 int
184 prom_put(struct iodesc *desc, void *pkt, size_t len)
185 {
186 int s;
187
188 #ifdef NET_DEBUG
189 printf("prom_put: called\n");
190 #endif
191
192 #ifdef NET_DEBUG
193 if (debug)
194 dump_packet_info(pkt,len);
195 #endif
196
197 if (callv == &callvec)
198 s = prom_write(sc_fd, pkt, len);
199 else {
200 s = (*callv->_bootwrite)(0, pkt, len);
201 (*callv->_wbflush)(); /* didn't really make a difference */
202 }
203 if (s < 0)
204 return (EIO);
205 return s;
206 }
207
208
209 int
210 prom_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
211 {
212 int s;
213 satime_t t;
214
215 #ifdef NET_DEBUG
216 printf("prom_get: called\n");
217 #endif
218
219 t = getsecs();
220 s = 0;
221 while (((getsecs() - t) < timeout) && !s) {
222 if (callv == &callvec)
223 s = prom_read(sc_fd, pkt, len);
224 else
225 s = (*callv->_bootread)(0, pkt, len);
226 }
227
228 #ifdef FILL_ARPCACHE
229 if (s > 0)
230 fill_arpcache(pkt,s);
231 #endif
232
233 return s;
234
235 }
236
237
238 void
239 prom_end(struct netif *nif)
240 {
241
242 #ifdef NET_DEBUG
243 printf("prom_end: called\n");
244 #endif
245
246 if (callv == &callvec)
247 prom_close(sc_fd);
248 }
249
250
251 #ifdef FILL_ARPCACHE
252 void fill_arpcache (pkt, len)
253 void *pkt;
254 int len;
255 {
256 int i;
257 struct arp_list *al;
258 struct ether_header *eh = (struct ether_header *)pkt;
259 struct ip *ih = (struct ip *)(eh + 1);
260
261 #ifdef NET_DEBUG
262 if (debug)
263 dump_packet_info(pkt, len);
264 #endif
265
266 if (ntohs(eh->ether_type) == 0x0800) {
267
268 /* check arp cache */
269 for (i=0, al=arp_list; i<arp_num; ++i, ++al) {
270 if (al->addr.s_addr == ih->ip_src.s_addr) {
271 /* already in cache */
272 return;
273 }
274 }
275 if (arp_num > 7)
276 arp_num = 1; /* recycle */
277 al->addr.s_addr = ih->ip_src.s_addr;
278 for (i=0; i<6; i++)
279 al->ea[i] = eh->ether_shost[i];
280 ++arp_num;
281 }
282
283 }
284 #endif
285
286 #ifdef NET_DEBUG
287 void dump_packet_info(pkt, len)
288 void *pkt;
289 int len;
290 {
291 struct ether_header *eh = (struct ether_header *)pkt;
292 struct ip *ih = (struct ip *)(eh + 1);
293
294 printf("ether_dhost = %s\n", ether_sprintf(eh->ether_dhost));
295 printf("ether_shost = %s\n", ether_sprintf(eh->ether_shost));
296 printf("ether_type = 0x%x\n", ntohs(eh->ether_type));
297
298 if (ntohs(eh->ether_type) == 0x0800) {
299 printf("ip packet version %d\n", ih->ip_v);
300 printf("source ip: 0x%x\n", ih->ip_src.s_addr);
301 printf("dest ip: 0x%x\n", ih->ip_dst.s_addr);
302
303 }
304
305 }
306 #endif
307