2005-01-05 22:16:58 +00:00
|
|
|
/*-
|
2001-10-07 13:22:25 +00:00
|
|
|
* Copyright (c) 2000-2001 Benno Rice
|
2000-10-16 10:46:22 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-01-04 23:30:47 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-10-16 10:46:22 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
2000-11-10 06:39:58 +00:00
|
|
|
#include <sys/socket.h>
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
#include <net/if.h>
|
2000-10-16 10:46:22 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
2000-11-10 06:39:58 +00:00
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <netinet/ip.h>
|
2000-10-16 10:46:22 +00:00
|
|
|
|
|
|
|
#include <stand.h>
|
|
|
|
#include <net.h>
|
|
|
|
#include <netif.h>
|
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
#include "openfirm.h"
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
static int ofwn_probe(struct netif *, void *);
|
|
|
|
static int ofwn_match(struct netif *, void *);
|
|
|
|
static void ofwn_init(struct iodesc *, void *);
|
2017-05-06 20:32:27 +00:00
|
|
|
static ssize_t ofwn_get(struct iodesc *, void **, time_t);
|
|
|
|
static ssize_t ofwn_put(struct iodesc *, void *, size_t);
|
2000-11-10 06:39:58 +00:00
|
|
|
static void ofwn_end(struct netif *);
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
extern struct netif_stats ofwn_stats[];
|
|
|
|
|
|
|
|
struct netif_dif ofwn_ifs[] = {
|
2000-10-16 10:46:22 +00:00
|
|
|
/* dif_unit dif_nsel dif_stats dif_private */
|
2000-11-10 06:39:58 +00:00
|
|
|
{ 0, 1, &ofwn_stats[0], 0, },
|
2000-10-16 10:46:22 +00:00
|
|
|
};
|
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
struct netif_stats ofwn_stats[nitems(ofwn_ifs)];
|
2000-10-16 10:46:22 +00:00
|
|
|
|
|
|
|
struct netif_driver ofwnet = {
|
|
|
|
"net", /* netif_bname */
|
|
|
|
ofwn_match, /* netif_match */
|
|
|
|
ofwn_probe, /* netif_probe */
|
|
|
|
ofwn_init, /* netif_init */
|
|
|
|
ofwn_get, /* netif_get */
|
|
|
|
ofwn_put, /* netif_put */
|
|
|
|
ofwn_end, /* netif_end */
|
|
|
|
ofwn_ifs, /* netif_ifs */
|
2017-05-06 20:32:27 +00:00
|
|
|
nitems(ofwn_ifs) /* netif_nifs */
|
2000-10-16 10:46:22 +00:00
|
|
|
};
|
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
static ihandle_t netinstance;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2020-05-01 17:50:21 +00:00
|
|
|
#ifdef BROKEN
|
2000-11-10 06:39:58 +00:00
|
|
|
static void *dmabuf;
|
2020-05-01 17:50:21 +00:00
|
|
|
#endif
|
2000-11-10 06:39:58 +00:00
|
|
|
|
|
|
|
static int
|
2000-10-16 10:46:22 +00:00
|
|
|
ofwn_match(struct netif *nif, void *machdep_hint)
|
|
|
|
{
|
2000-11-10 06:39:58 +00:00
|
|
|
return 1;
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
static int
|
2000-10-16 10:46:22 +00:00
|
|
|
ofwn_probe(struct netif *nif, void *machdep_hint)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
static ssize_t
|
2001-10-07 13:22:25 +00:00
|
|
|
ofwn_put(struct iodesc *desc, void *pkt, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2000-11-10 06:39:58 +00:00
|
|
|
size_t sendlen;
|
|
|
|
ssize_t rv;
|
|
|
|
|
|
|
|
#if defined(NETIF_DEBUG)
|
2010-11-17 19:31:48 +00:00
|
|
|
struct ether_header *eh;
|
2000-11-10 06:39:58 +00:00
|
|
|
printf("netif_put: desc=0x%x pkt=0x%x len=%d\n", desc, pkt, len);
|
|
|
|
eh = pkt;
|
|
|
|
printf("dst: %s ", ether_sprintf(eh->ether_dhost));
|
|
|
|
printf("src: %s ", ether_sprintf(eh->ether_shost));
|
|
|
|
printf("type: 0x%x\n", eh->ether_type & 0xffff);
|
|
|
|
#endif
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
sendlen = len;
|
|
|
|
if (sendlen < 60) {
|
|
|
|
sendlen = 60;
|
|
|
|
#if defined(NETIF_DEBUG)
|
|
|
|
printf("netif_put: length padded to %d\n", sendlen);
|
2000-10-16 10:46:22 +00:00
|
|
|
#endif
|
2000-11-10 06:39:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 17:50:21 +00:00
|
|
|
#ifdef BROKEN
|
2000-11-10 06:39:58 +00:00
|
|
|
if (dmabuf) {
|
|
|
|
bcopy(pkt, dmabuf, sendlen);
|
|
|
|
pkt = dmabuf;
|
|
|
|
}
|
2020-05-01 17:50:21 +00:00
|
|
|
#endif
|
2000-11-10 06:39:58 +00:00
|
|
|
|
|
|
|
rv = OF_write(netinstance, pkt, len);
|
|
|
|
|
|
|
|
#if defined(NETIF_DEBUG)
|
|
|
|
printf("netif_put: OF_write returned %d\n", rv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
static ssize_t
|
|
|
|
ofwn_get(struct iodesc *desc, void **pkt, time_t timeout)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2000-11-10 06:39:58 +00:00
|
|
|
time_t t;
|
2017-05-06 20:32:27 +00:00
|
|
|
ssize_t length;
|
|
|
|
size_t len;
|
|
|
|
char *buf, *ptr;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
#if defined(NETIF_DEBUG)
|
2017-05-06 20:32:27 +00:00
|
|
|
printf("netif_get: pkt=%p, timeout=%d\n", pkt, timeout);
|
2000-11-10 06:39:58 +00:00
|
|
|
#endif
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
/*
|
|
|
|
* We should read the "max-frame-size" int property instead,
|
|
|
|
* but at this time the iodesc does not have mtu, so we will take
|
|
|
|
* a small shortcut here.
|
|
|
|
*/
|
|
|
|
len = ETHER_MAX_LEN;
|
2020-01-13 20:02:27 +00:00
|
|
|
buf = malloc(len + ETHER_ALIGN);
|
2017-05-06 20:32:27 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return (-1);
|
|
|
|
ptr = buf + ETHER_ALIGN;
|
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
t = getsecs();
|
|
|
|
do {
|
2017-05-06 20:32:27 +00:00
|
|
|
length = OF_read(netinstance, ptr, len);
|
2000-11-10 06:39:58 +00:00
|
|
|
} while ((length == -2 || length == 0) &&
|
|
|
|
(getsecs() - t < timeout));
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
#if defined(NETIF_DEBUG)
|
|
|
|
printf("netif_get: received length=%d (%x)\n", length, length);
|
|
|
|
#endif
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
if (length < 12) {
|
|
|
|
free(buf);
|
|
|
|
return (-1);
|
|
|
|
}
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
#if defined(NETIF_VERBOSE_DEBUG)
|
|
|
|
{
|
2017-05-06 20:32:27 +00:00
|
|
|
char *ch = ptr;
|
2000-11-10 06:39:58 +00:00
|
|
|
int i;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
for(i = 0; i < 96; i += 4) {
|
|
|
|
printf("%02x%02x%02x%02x ", ch[i], ch[i+1],
|
|
|
|
ch[i+2], ch[i+3]);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
2000-11-10 06:39:58 +00:00
|
|
|
printf("\n");
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
2000-11-10 06:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NETIF_DEBUG)
|
|
|
|
{
|
2017-05-06 20:32:27 +00:00
|
|
|
struct ether_header *eh = ptr;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
printf("dst: %s ", ether_sprintf(eh->ether_dhost));
|
|
|
|
printf("src: %s ", ether_sprintf(eh->ether_shost));
|
|
|
|
printf("type: 0x%x\n", eh->ether_type & 0xffff);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
2000-11-10 06:39:58 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-06 20:32:27 +00:00
|
|
|
*pkt = buf;
|
|
|
|
return (length);
|
2000-11-10 06:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern char *strchr();
|
|
|
|
|
|
|
|
static void
|
|
|
|
ofwn_init(struct iodesc *desc, void *machdep_hint)
|
|
|
|
{
|
2007-06-17 00:17:15 +00:00
|
|
|
phandle_t netdev;
|
2000-11-10 06:39:58 +00:00
|
|
|
char path[64];
|
|
|
|
char *ch;
|
|
|
|
int pathlen;
|
|
|
|
|
|
|
|
pathlen = OF_getprop(chosen, "bootpath", path, 64);
|
2012-01-03 18:51:58 +00:00
|
|
|
if ((ch = strchr(path, ':')) != NULL)
|
2002-02-23 04:31:30 +00:00
|
|
|
*ch = '\0';
|
2000-11-10 06:39:58 +00:00
|
|
|
netdev = OF_finddevice(path);
|
|
|
|
if (OF_getprop(netdev, "local-mac-address", desc->myea, 6) == -1)
|
|
|
|
goto punt;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
|
|
|
printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
|
|
|
|
|
2001-10-07 13:22:25 +00:00
|
|
|
if ((netinstance = OF_open(path)) == -1) {
|
|
|
|
printf("Could not open network device.\n");
|
2000-11-10 06:39:58 +00:00
|
|
|
goto punt;
|
2001-10-07 13:22:25 +00:00
|
|
|
}
|
2000-11-10 06:39:58 +00:00
|
|
|
|
|
|
|
#if defined(NETIF_DEBUG)
|
2004-08-16 15:45:27 +00:00
|
|
|
printf("ofwn_init: Open Firmware instance handle: %08x\n", netinstance);
|
2000-11-10 06:39:58 +00:00
|
|
|
#endif
|
2000-10-16 10:46:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
punt:
|
|
|
|
printf("\n");
|
2000-11-10 06:39:58 +00:00
|
|
|
printf("Could not boot from %s.\n", path);
|
2001-10-07 13:22:25 +00:00
|
|
|
OF_enter();
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2000-11-10 06:39:58 +00:00
|
|
|
static void
|
2000-10-16 10:46:22 +00:00
|
|
|
ofwn_end(struct netif *nif)
|
|
|
|
{
|
2010-10-29 00:36:44 +00:00
|
|
|
#ifdef BROKEN
|
|
|
|
/* dma-free freezes at least some Apple ethernet controllers */
|
2000-11-10 06:39:58 +00:00
|
|
|
OF_call_method("dma-free", netinstance, 2, 0, dmabuf, MAXPHYS);
|
2010-10-29 00:36:44 +00:00
|
|
|
#endif
|
2000-11-10 06:39:58 +00:00
|
|
|
OF_close(netinstance);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
2000-11-10 06:39:58 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
int
|
|
|
|
ofwn_getunit(const char *path)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char newpath[255];
|
|
|
|
|
|
|
|
OF_canon(path, newpath, 254);
|
|
|
|
|
|
|
|
for (i = 0; i < nofwninfo; i++) {
|
|
|
|
printf(">>> test =\t%s\n", ofwninfo[i].ofwn_path);
|
|
|
|
if (strcmp(path, ofwninfo[i].ofwn_path) == 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
if (strcmp(newpath, ofwninfo[i].ofwn_path) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|