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 __FBSDID("$FreeBSD$");
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>
47 static int ofwn_probe(struct netif *, void *);
48 static int ofwn_match(struct netif *, void *);
49 static void ofwn_init(struct iodesc *, void *);
50 static ssize_t ofwn_get(struct iodesc *, void **, time_t);
51 static ssize_t ofwn_put(struct iodesc *, void *, size_t);
52 static void ofwn_end(struct netif *);
54 extern struct netif_stats ofwn_stats[];
56 struct netif_dif ofwn_ifs[] = {
60 .dif_stats=&ofwn_stats[0],
65 struct netif_stats ofwn_stats[nitems(ofwn_ifs)];
67 struct netif_driver ofwnet = {
69 .netif_match=ofwn_match,
70 .netif_probe=ofwn_probe,
71 .netif_init=ofwn_init,
76 .netif_nifs=nitems(ofwn_ifs)
79 static ihandle_t netinstance;
84 ofwn_match(struct netif *nif, void *machdep_hint)
90 ofwn_probe(struct netif *nif, void *machdep_hint)
96 ofwn_put(struct iodesc *desc, void *pkt, size_t len)
101 #if defined(NETIF_DEBUG)
102 struct ether_header *eh;
103 printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", desc, pkt, len);
105 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
106 printf("src: %s ", ether_sprintf(eh->ether_shost));
107 printf("type: 0x%x\n", eh->ether_type & 0xffff);
113 #if defined(NETIF_DEBUG)
114 printf("netif_put: length padded to %d\n", sendlen);
119 bcopy(pkt, dmabuf, sendlen);
123 rv = OF_write(netinstance, pkt, len);
125 #if defined(NETIF_DEBUG)
126 printf("netif_put: OF_write returned %d\n", rv);
133 ofwn_get(struct iodesc *desc, void **pkt, time_t timeout)
140 #if defined(NETIF_DEBUG)
141 printf("netif_get: pkt=%p, timeout=%d\n", pkt, timeout);
145 * We should read the "max-frame-size" int property instead,
146 * but at this time the iodesc does not have mtu, so we will take
147 * a small shortcut here.
150 buf = malloc(len + ETHER_ALIGN);
153 ptr = buf + ETHER_ALIGN;
157 length = OF_read(netinstance, ptr, len);
158 } while ((length == -2 || length == 0) &&
159 (getsecs() - t < timeout));
161 #if defined(NETIF_DEBUG)
162 printf("netif_get: received length=%d (%x)\n", length, length);
170 #if defined(NETIF_VERBOSE_DEBUG)
175 for(i = 0; i < 96; i += 4) {
176 printf("%02x%02x%02x%02x ", ch[i], ch[i+1],
183 #if defined(NETIF_DEBUG)
185 struct ether_header *eh = ptr;
187 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
188 printf("src: %s ", ether_sprintf(eh->ether_shost));
189 printf("type: 0x%x\n", eh->ether_type & 0xffff);
198 ofwn_init(struct iodesc *desc, void *machdep_hint)
205 pathlen = OF_getprop(chosen, "bootpath", path, 64);
206 if ((ch = strchr(path, ':')) != NULL)
208 netdev = OF_finddevice(path);
209 if (OF_getprop(netdev, "local-mac-address", desc->myea, 6) == -1)
212 printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
214 if ((netinstance = OF_open(path)) == -1) {
215 printf("Could not open network device.\n");
219 #if defined(NETIF_DEBUG)
220 printf("ofwn_init: Open Firmware instance handle: %08x\n", netinstance);
223 if (OF_call_method("dma-alloc", netinstance, 1, 1, (64 * 1024), &dmabuf)
225 printf("Failed to allocate DMA buffer (got %p).\n", dmabuf);
228 #if defined(NETIF_DEBUG)
229 printf("ofwn_init: allocated DMA buffer: %p\n", dmabuf);
236 printf("Could not boot from %s.\n", path);
241 ofwn_end(struct netif *nif)
244 /* dma-free freezes at least some Apple ethernet controllers */
245 OF_call_method("dma-free", netinstance, 2, 0, dmabuf, MAXPHYS);
247 OF_close(netinstance);
252 ofwn_getunit(const char *path)
257 OF_canon(path, newpath, 254);
259 for (i = 0; i < nofwninfo; i++) {
260 printf(">>> test =\t%s\n", ofwninfo[i].ofwn_path);
261 if (strcmp(path, ofwninfo[i].ofwn_path) == 0)
264 if (strcmp(newpath, ofwninfo[i].ofwn_path) == 0)
273 * To properly match network devices, we have to subclass the netdev device.
274 * It has a different devdesc than a normal network device (which is fine:
275 * it's a struct superset) and different matching criteria (since it has to
276 * look at the path, find a handle and see if that handle is a network node
280 static int ofwnd_init(void);
281 static int ofwnd_parsedev(struct devdesc **, const char *, const char **);
282 static bool ofwnd_match(struct devsw *, const char *);
283 static char *ofwnd_fmtdev(struct devdesc *);
285 struct devsw ofw_netdev = {
286 .dv_name = "network",
288 .dv_init = ofwnd_init,
289 .dv_match = ofwnd_match,
290 .dv_fmtdev = ofwnd_fmtdev,
291 .dv_parsedev = ofwnd_parsedev,
294 static int ofwnd_init(void)
297 ofw_netdev.dv_strategy = netdev.dv_strategy;
298 ofw_netdev.dv_open = netdev.dv_open;
299 ofw_netdev.dv_close = netdev.dv_close;
300 ofw_netdev.dv_ioctl = netdev.dv_ioctl;
301 ofw_netdev.dv_print = netdev.dv_print;
302 ofw_netdev.dv_fmtdev = netdev.dv_fmtdev;
303 /* parsedev is unique to ofwnd */
304 /* match is unique to ofwnd */
309 ofwnd_parsedev(struct devdesc **dev, const char *devspec, const char **path)
311 return (ofw_common_parsedev(dev, devspec, path, ofw_netdev.dv_name));
315 ofwnd_match(struct devsw *devsw, const char *devspec)
319 return (ofw_path_to_handle(devspec, devsw->dv_name, &path) != -1);
323 ofwnd_fmtdev(struct devdesc *idev)
325 struct ofw_devdesc *dev = (struct ofw_devdesc *)idev;
327 return (dev->d_path);