]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/libofw/ofw_net.c
Remove more stray sparc64 ifdefs.
[FreeBSD/FreeBSD.git] / stand / libofw / ofw_net.c
1 /*-
2  * Copyright (c) 2000-2001 Benno Rice
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33
34 #include <net/if.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/if_ether.h>
38 #include <netinet/ip.h>
39
40 #include <stand.h>
41 #include <net.h>
42 #include <netif.h>
43
44 #include "openfirm.h"
45
46 static int      ofwn_probe(struct netif *, void *);
47 static int      ofwn_match(struct netif *, void *);
48 static void     ofwn_init(struct iodesc *, void *);
49 static ssize_t  ofwn_get(struct iodesc *, void **, time_t);
50 static ssize_t  ofwn_put(struct iodesc *, void *, size_t);
51 static void     ofwn_end(struct netif *);
52
53 extern struct netif_stats       ofwn_stats[];
54
55 struct netif_dif ofwn_ifs[] = {
56         /*      dif_unit        dif_nsel        dif_stats       dif_private     */
57         {       0,              1,              &ofwn_stats[0], 0,      },
58 };
59
60 struct netif_stats ofwn_stats[nitems(ofwn_ifs)];
61
62 struct netif_driver ofwnet = {
63         "net",                  /* netif_bname */
64         ofwn_match,             /* netif_match */
65         ofwn_probe,             /* netif_probe */
66         ofwn_init,              /* netif_init */
67         ofwn_get,               /* netif_get */
68         ofwn_put,               /* netif_put */
69         ofwn_end,               /* netif_end */
70         ofwn_ifs,               /* netif_ifs */
71         nitems(ofwn_ifs)                /* netif_nifs */
72 };
73
74 static ihandle_t        netinstance;
75
76 #ifdef BROKEN
77 static void             *dmabuf;
78 #endif
79
80 static int
81 ofwn_match(struct netif *nif, void *machdep_hint)
82 {
83         return 1;
84 }
85
86 static int
87 ofwn_probe(struct netif *nif, void *machdep_hint)
88 {
89         return 0;
90 }
91
92 static ssize_t
93 ofwn_put(struct iodesc *desc, void *pkt, size_t len)
94 {
95         size_t                  sendlen;
96         ssize_t                 rv;
97
98 #if defined(NETIF_DEBUG)
99         struct ether_header     *eh;
100         printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", desc, pkt, len);
101         eh = pkt;
102         printf("dst: %s ", ether_sprintf(eh->ether_dhost));
103         printf("src: %s ", ether_sprintf(eh->ether_shost));
104         printf("type: 0x%x\n", eh->ether_type & 0xffff);
105 #endif
106
107         sendlen = len;
108         if (sendlen < 60) {
109                 sendlen = 60;
110 #if defined(NETIF_DEBUG)
111                 printf("netif_put: length padded to %d\n", sendlen);
112 #endif
113         }
114
115 #ifdef BROKEN
116         if (dmabuf) {
117                 bcopy(pkt, dmabuf, sendlen);
118                 pkt = dmabuf;
119         }
120 #endif
121
122         rv = OF_write(netinstance, pkt, len);
123
124 #if defined(NETIF_DEBUG)
125         printf("netif_put: OF_write returned %d\n", rv);
126 #endif
127
128         return rv;
129 }
130
131 static ssize_t
132 ofwn_get(struct iodesc *desc, void **pkt, time_t timeout)
133 {
134         time_t  t;
135         ssize_t length;
136         size_t  len;
137         char    *buf, *ptr;
138
139 #if defined(NETIF_DEBUG)
140         printf("netif_get: pkt=%p, timeout=%d\n", pkt, timeout);
141 #endif
142
143         /*
144          * We should read the "max-frame-size" int property instead,
145          * but at this time the iodesc does not have mtu, so we will take
146          * a small shortcut here.
147          */
148         len = ETHER_MAX_LEN;
149         buf = malloc(len + ETHER_ALIGN);
150         if (buf == NULL)
151                 return (-1);
152         ptr = buf + ETHER_ALIGN;
153
154         t = getsecs();
155         do {
156                 length = OF_read(netinstance, ptr, len);
157         } while ((length == -2 || length == 0) &&
158                 (getsecs() - t < timeout));
159
160 #if defined(NETIF_DEBUG)
161         printf("netif_get: received length=%d (%x)\n", length, length);
162 #endif
163
164         if (length < 12) {
165                 free(buf);
166                 return (-1);
167         }
168
169 #if defined(NETIF_VERBOSE_DEBUG)
170         {
171                 char *ch = ptr;
172                 int i;
173
174                 for(i = 0; i < 96; i += 4) {
175                         printf("%02x%02x%02x%02x  ", ch[i], ch[i+1],
176                             ch[i+2], ch[i+3]);
177                 }
178                 printf("\n");
179         }
180 #endif
181
182 #if defined(NETIF_DEBUG)
183         {
184                 struct ether_header *eh = ptr;
185
186                 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
187                 printf("src: %s ", ether_sprintf(eh->ether_shost));
188                 printf("type: 0x%x\n", eh->ether_type & 0xffff);
189         }
190 #endif
191
192         *pkt = buf;
193         return (length);
194 }
195
196 extern char *strchr();
197
198 static void
199 ofwn_init(struct iodesc *desc, void *machdep_hint)
200 {
201         phandle_t       netdev;
202         char            path[64];
203         char            *ch;
204         int             pathlen;
205
206         pathlen = OF_getprop(chosen, "bootpath", path, 64);
207         if ((ch = strchr(path, ':')) != NULL)
208                 *ch = '\0';
209         netdev = OF_finddevice(path);
210         if (OF_getprop(netdev, "local-mac-address", desc->myea, 6) == -1)
211                 goto punt;
212
213         printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
214
215         if ((netinstance = OF_open(path)) == -1) {
216                 printf("Could not open network device.\n");
217                 goto punt;
218         }
219
220 #if defined(NETIF_DEBUG)
221         printf("ofwn_init: Open Firmware instance handle: %08x\n", netinstance);
222 #endif
223         return;
224
225 punt:
226         printf("\n");
227         printf("Could not boot from %s.\n", path);
228         OF_enter();
229 }
230
231 static void
232 ofwn_end(struct netif *nif)
233 {
234 #ifdef BROKEN
235         /* dma-free freezes at least some Apple ethernet controllers */
236         OF_call_method("dma-free", netinstance, 2, 0, dmabuf, MAXPHYS);
237 #endif
238         OF_close(netinstance);
239 }
240
241 #if 0
242 int
243 ofwn_getunit(const char *path)
244 {
245         int             i;
246         char            newpath[255];
247
248         OF_canon(path, newpath, 254);
249
250         for (i = 0; i < nofwninfo; i++) {
251                 printf(">>> test =\t%s\n", ofwninfo[i].ofwn_path);
252                 if (strcmp(path, ofwninfo[i].ofwn_path) == 0)
253                         return i;
254
255                 if (strcmp(newpath, ofwninfo[i].ofwn_path) == 0)
256                         return i;
257         }
258
259         return -1;
260 }
261 #endif