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
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/if_ether.h>
36 #include <netinet/ip.h>
45 static int ofwn_probe(struct netif *, void *);
46 static int ofwn_match(struct netif *, void *);
47 static void ofwn_init(struct iodesc *, void *);
48 static ssize_t ofwn_get(struct iodesc *, void **, time_t);
49 static ssize_t ofwn_put(struct iodesc *, void *, size_t);
50 static void ofwn_end(struct netif *);
52 extern struct netif_stats ofwn_stats[];
54 struct netif_dif ofwn_ifs[] = {
58 .dif_stats=&ofwn_stats[0],
63 struct netif_stats ofwn_stats[nitems(ofwn_ifs)];
65 struct netif_driver ofwnet = {
67 .netif_match=ofwn_match,
68 .netif_probe=ofwn_probe,
69 .netif_init=ofwn_init,
74 .netif_nifs=nitems(ofwn_ifs)
77 static ihandle_t netinstance;
82 ofwn_match(struct netif *nif, void *machdep_hint)
88 ofwn_probe(struct netif *nif, void *machdep_hint)
94 ofwn_put(struct iodesc *desc, void *pkt, size_t len)
99 #if defined(NETIF_DEBUG)
100 struct ether_header *eh;
101 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", desc, pkt, len);
103 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
104 printf("src: %s ", ether_sprintf(eh->ether_shost));
105 printf("type: 0x%x\n", eh->ether_type & 0xffff);
111 #if defined(NETIF_DEBUG)
112 printf("netif_put: length padded to %d\n", sendlen);
117 bcopy(pkt, dmabuf, sendlen);
121 rv = OF_write(netinstance, pkt, len);
123 #if defined(NETIF_DEBUG)
124 printf("netif_put: OF_write returned %d\n", rv);
131 ofwn_get(struct iodesc *desc, void **pkt, time_t timeout)
138 #if defined(NETIF_DEBUG)
139 printf("netif_get: pkt=%p, timeout=%d\n", pkt, timeout);
143 * We should read the "max-frame-size" int property instead,
144 * but at this time the iodesc does not have mtu, so we will take
145 * a small shortcut here.
148 buf = malloc(len + ETHER_ALIGN);
151 ptr = buf + ETHER_ALIGN;
155 length = OF_read(netinstance, ptr, len);
156 } while ((length == -2 || length == 0) &&
157 (getsecs() - t < timeout));
159 #if defined(NETIF_DEBUG)
160 printf("netif_get: received length=%d (%x)\n", length, length);
168 #if defined(NETIF_VERBOSE_DEBUG)
173 for(i = 0; i < 96; i += 4) {
174 printf("%02x%02x%02x%02x ", ch[i], ch[i+1],
181 #if defined(NETIF_DEBUG)
183 struct ether_header *eh = ptr;
185 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
186 printf("src: %s ", ether_sprintf(eh->ether_shost));
187 printf("type: 0x%x\n", eh->ether_type & 0xffff);
196 ofwn_init(struct iodesc *desc, void *machdep_hint)
203 pathlen = OF_getprop(chosen, "bootpath", path, 64);
204 if ((ch = strchr(path, ':')) != NULL)
206 netdev = OF_finddevice(path);
207 if (OF_getprop(netdev, "local-mac-address", desc->myea, 6) == -1)
210 printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
212 if ((netinstance = OF_open(path)) == -1) {
213 printf("Could not open network device.\n");
217 #if defined(NETIF_DEBUG)
218 printf("ofwn_init: Open Firmware instance handle: %08x\n", netinstance);
221 if (OF_call_method("dma-alloc", netinstance, 1, 1, (64 * 1024), &dmabuf)
223 printf("Failed to allocate DMA buffer (got %p).\n", dmabuf);
226 #if defined(NETIF_DEBUG)
227 printf("ofwn_init: allocated DMA buffer: %p\n", dmabuf);
234 printf("Could not boot from %s.\n", path);
239 ofwn_end(struct netif *nif)
242 /* dma-free freezes at least some Apple ethernet controllers */
243 OF_call_method("dma-free", netinstance, 2, 0, dmabuf, MAXPHYS);
245 OF_close(netinstance);
250 ofwn_getunit(const char *path)
255 OF_canon(path, newpath, 254);
257 for (i = 0; i < nofwninfo; i++) {
258 printf(">>> test =\t%s\n", ofwninfo[i].ofwn_path);
259 if (strcmp(path, ofwninfo[i].ofwn_path) == 0)
262 if (strcmp(newpath, ofwninfo[i].ofwn_path) == 0)
271 * To properly match network devices, we have to subclass the netdev device.
272 * It has a different devdesc than a normal network device (which is fine:
273 * it's a struct superset) and different matching criteria (since it has to
274 * look at the path, find a handle and see if that handle is a network node
278 static int ofwnd_init(void);
279 static int ofwnd_parsedev(struct devdesc **, const char *, const char **);
280 static bool ofwnd_match(struct devsw *, const char *);
281 static char *ofwnd_fmtdev(struct devdesc *);
283 struct devsw ofw_netdev = {
284 .dv_name = "network",
286 .dv_init = ofwnd_init,
287 .dv_match = ofwnd_match,
288 .dv_fmtdev = ofwnd_fmtdev,
289 .dv_parsedev = ofwnd_parsedev,
292 static int ofwnd_init(void)
295 ofw_netdev.dv_strategy = netdev.dv_strategy;
296 ofw_netdev.dv_open = netdev.dv_open;
297 ofw_netdev.dv_close = netdev.dv_close;
298 ofw_netdev.dv_ioctl = netdev.dv_ioctl;
299 ofw_netdev.dv_print = netdev.dv_print;
300 ofw_netdev.dv_fmtdev = netdev.dv_fmtdev;
301 /* parsedev is unique to ofwnd */
302 /* match is unique to ofwnd */
307 ofwnd_parsedev(struct devdesc **dev, const char *devspec, const char **path)
309 return (ofw_common_parsedev(dev, devspec, path, ofw_netdev.dv_name));
313 ofwnd_match(struct devsw *devsw, const char *devspec)
317 return (ofw_path_to_handle(devspec, devsw->dv_name, &path) != -1);
321 ofwnd_fmtdev(struct devdesc *idev)
323 struct ofw_devdesc *dev = (struct ofw_devdesc *)idev;
325 return (dev->d_path);