2 * Copyright (c) 2000-2001 Benno Rice
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/boot/ofw/libofw/ofw_net.c,v 1.6 2002/06/07 11:49:33 benno Exp $
27 * $DragonFly: src/sys/boot/ofw/libofw/ofw_net.c,v 1.1 2003/11/10 06:08:37 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/if_ether.h>
38 #include <netinet/ip.h>
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 int ofwn_get(struct iodesc
*, void *, size_t, time_t);
50 static int ofwn_put(struct iodesc
*, void *, size_t);
51 static void ofwn_end(struct netif
*);
53 extern struct netif_stats ofwn_stats
[];
55 struct netif_dif ofwn_ifs
[] = {
56 /* dif_unit dif_nsel dif_stats dif_private */
57 { 0, 1, &ofwn_stats
[0], 0, },
60 struct netif_stats ofwn_stats
[NENTS(ofwn_ifs
)];
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 NENTS(ofwn_ifs
) /* netif_nifs */
74 static phandle_t netdevice
;
75 static ihandle_t netinstance
;
76 static ihandle_t memory
;
81 ofwn_match(struct netif
*nif
, void *machdep_hint
)
87 ofwn_probe(struct netif
*nif
, void *machdep_hint
)
93 ofwn_put(struct iodesc
*desc
, void *pkt
, size_t len
)
95 struct ether_header
*eh
;
99 #if defined(NETIF_DEBUG)
100 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", desc
, pkt
, len
);
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);
110 #if defined(NETIF_DEBUG)
111 printf("netif_put: length padded to %d\n", sendlen
);
116 bcopy(pkt
, dmabuf
, sendlen
);
120 rv
= OF_write(netinstance
, pkt
, len
);
122 #if defined(NETIF_DEBUG)
123 printf("netif_put: OF_write returned %d\n", rv
);
130 ofwn_get(struct iodesc
*desc
, void *pkt
, size_t len
, time_t timeout
)
135 #if defined(NETIF_DEBUG)
136 printf("netif_get: pkt=%p, maxlen=%d, timeout=%d\n", pkt
, len
,
142 length
= OF_read(netinstance
, pkt
, len
);
143 } while ((length
== -2 || length
== 0) &&
144 (getsecs() - t
< timeout
));
146 #if defined(NETIF_DEBUG)
147 printf("netif_get: received length=%d (%x)\n", length
, length
);
153 #if defined(NETIF_VERBOSE_DEBUG)
158 for(i
= 0; i
< 96; i
+= 4) {
159 printf("%02x%02x%02x%02x ", ch
[i
], ch
[i
+1],
166 #if defined(NETIF_DEBUG)
168 struct ether_header
*eh
= pkt
;
170 printf("dst: %s ", ether_sprintf(eh
->ether_dhost
));
171 printf("src: %s ", ether_sprintf(eh
->ether_shost
));
172 printf("type: 0x%x\n", eh
->ether_type
& 0xffff);
179 extern char *strchr();
182 ofwn_init(struct iodesc
*desc
, void *machdep_hint
)
184 phandle_t chosen
, netdev
;
189 chosen
= OF_finddevice("/chosen");
190 OF_getprop(chosen
, "memory", &memory
, sizeof(memory
));
191 pathlen
= OF_getprop(chosen
, "bootpath", path
, 64);
192 if ((ch
= index(path
, ':')) != NULL
)
194 netdev
= OF_finddevice(path
);
196 if (OF_getprop(netdev
, "mac-address", desc
->myea
, 6) == -1)
198 if (OF_getprop(netdev
, "local-mac-address", desc
->myea
, 6) == -1)
202 printf("boot: ethernet address: %s\n", ether_sprintf(desc
->myea
));
204 if ((netinstance
= OF_open(path
)) == -1) {
205 printf("Could not open network device.\n");
209 #if defined(NETIF_DEBUG)
210 printf("ofwn_init: OpenFirmware instance handle: %08x\n", netinstance
);
215 if (OF_call_method("dma-alloc", netinstance
, 1, 1, (64 * 1024), &dmabuf
)
217 printf("Failed to allocate DMA buffer (got %08x).\n", dmabuf
);
221 #if defined(NETIF_DEBUG)
222 printf("ofwn_init: allocated DMA buffer: %08x\n", dmabuf
);
230 printf("Could not boot from %s.\n", path
);
235 ofwn_end(struct netif
*nif
)
237 OF_call_method("dma-free", netinstance
, 2, 0, dmabuf
, MAXPHYS
);
238 OF_close(netinstance
);
243 ofwn_getunit(const char *path
)
248 OF_canon(path
, newpath
, 254);
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)
255 if (strcmp(newpath
, ofwninfo
[i
].ofwn_path
) == 0)