1997-07-22 21:41:01 +04:00
|
|
|
/* $NetBSD: netif_sun.c,v 1.7 1997/07/22 17:41:09 drochner Exp $ */
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Gordon W. Ross
|
|
|
|
* 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.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
* 4. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Gordon W. Ross
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Sun PROM has a fairly general set of network drivers,
|
|
|
|
* so it is easiest to just replace the netif module with
|
|
|
|
* this adaptation to the PROM network interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
1997-07-22 21:41:01 +04:00
|
|
|
#include <net/if_ether.h>
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
|
|
|
#include <machine/control.h>
|
|
|
|
#include <machine/idprom.h>
|
|
|
|
#include <machine/mon.h>
|
|
|
|
#include <machine/saio.h>
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "netif.h"
|
|
|
|
|
|
|
|
#include "clock.h"
|
|
|
|
#include "dvma.h"
|
|
|
|
#include "promdev.h"
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
#define PKT_BUF_SIZE 2048
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
int debug;
|
|
|
|
int errno;
|
|
|
|
|
|
|
|
static void sun3_getether __P((u_char *));
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
struct iodesc sockets[SOPEN_MAX];
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
static struct netif prom_nif;
|
|
|
|
static struct devdata {
|
|
|
|
struct saioreq dd_si;
|
|
|
|
int rbuf_len;
|
|
|
|
char *rbuf;
|
|
|
|
int tbuf_len;
|
|
|
|
char *tbuf;
|
|
|
|
u_short dd_opens;
|
|
|
|
char dd_myea[6];
|
|
|
|
} prom_dd;
|
|
|
|
|
|
|
|
static struct idprom sun3_idprom;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sun3_getether(ea)
|
|
|
|
u_char *ea;
|
1995-06-10 02:19:25 +04:00
|
|
|
{
|
1996-01-30 02:41:03 +03:00
|
|
|
u_char *src, *dst;
|
|
|
|
int len, x;
|
|
|
|
|
|
|
|
if (sun3_idprom.idp_format == 0) {
|
|
|
|
dst = (char*)&sun3_idprom;
|
|
|
|
src = (char*)IDPROM_BASE;
|
|
|
|
len = IDPROM_SIZE;
|
|
|
|
do {
|
|
|
|
x = get_control_byte(src++);
|
|
|
|
*dst++ = x;
|
|
|
|
} while (--len > 0);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
1996-01-30 02:41:03 +03:00
|
|
|
MACPY(sun3_idprom.idp_etheraddr, ea);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the PROM device.
|
|
|
|
* Return netif ptr on success.
|
|
|
|
*/
|
|
|
|
struct devdata *
|
|
|
|
netif_init(aux)
|
|
|
|
void *aux;
|
1995-06-10 02:19:25 +04:00
|
|
|
{
|
1996-01-30 02:41:03 +03:00
|
|
|
struct devdata *dd = &prom_dd;
|
1995-06-10 02:19:25 +04:00
|
|
|
struct saioreq *si;
|
1996-01-30 02:41:03 +03:00
|
|
|
struct bootparam *bp;
|
1995-09-23 07:42:17 +04:00
|
|
|
int error;
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1995-10-14 00:44:57 +03:00
|
|
|
/*
|
|
|
|
* Setup our part of the saioreq.
|
|
|
|
* (determines what gets opened)
|
|
|
|
*/
|
1996-01-30 02:41:03 +03:00
|
|
|
si = &dd->dd_si;
|
1995-10-14 00:44:57 +03:00
|
|
|
bzero((caddr_t)si, sizeof(*si));
|
1997-02-05 20:39:21 +03:00
|
|
|
bp = *romVectorPtr->bootParam;
|
1995-10-14 00:44:57 +03:00
|
|
|
si->si_boottab = bp->bootDevice;
|
|
|
|
si->si_ctlr = bp->ctlrNum;
|
|
|
|
si->si_unit = bp->unitNum;
|
|
|
|
si->si_boff = bp->partNum;
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
#ifdef NETIF_DEBUG
|
|
|
|
if (debug)
|
|
|
|
printf("netif_init: calling prom_iopen\n");
|
|
|
|
#endif
|
|
|
|
|
1995-06-10 02:19:25 +04:00
|
|
|
/*
|
|
|
|
* Note: Sun PROMs will do RARP on open, but does not tell
|
|
|
|
* you the IP address it gets, so it is just noise to us...
|
|
|
|
*/
|
1995-10-14 00:44:57 +03:00
|
|
|
if ((error = prom_iopen(si)) != 0) {
|
1996-01-30 02:41:03 +03:00
|
|
|
printf("netif_init: prom_iopen, error=%d\n", error);
|
|
|
|
return (NULL);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
1996-01-30 02:41:03 +03:00
|
|
|
|
1995-06-10 02:19:25 +04:00
|
|
|
if (si->si_sif == NULL) {
|
1996-01-30 02:41:03 +03:00
|
|
|
printf("netif_init: not a network device\n");
|
1995-06-10 02:19:25 +04:00
|
|
|
prom_iclose(si);
|
1996-01-30 02:41:03 +03:00
|
|
|
return (NULL);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
#ifdef NETIF_DEBUG
|
|
|
|
if (debug)
|
|
|
|
printf("netif_init: allocating buffers\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocate the transmit/receive buffers. */
|
|
|
|
if (dd->rbuf == NULL) {
|
|
|
|
dd->rbuf_len = PKT_BUF_SIZE;
|
|
|
|
dd->rbuf = dvma_alloc(dd->rbuf_len);
|
|
|
|
}
|
|
|
|
if (dd->tbuf == NULL) {
|
|
|
|
dd->tbuf_len = PKT_BUF_SIZE;
|
|
|
|
dd->tbuf = dvma_alloc(dd->tbuf_len);
|
|
|
|
}
|
|
|
|
if ((dd->rbuf == NULL) ||
|
|
|
|
(dd->tbuf == NULL))
|
|
|
|
panic("netif_init: malloc failed\n");
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
#ifdef NETIF_DEBUG
|
|
|
|
if (debug)
|
|
|
|
printf("netif_init: rbuf=0x%x, tbuf=0x%x\n",
|
|
|
|
dd->rbuf, dd->tbuf);
|
|
|
|
#endif
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
/* Record our ethernet address. */
|
|
|
|
sun3_getether(dd->dd_myea);
|
|
|
|
dd->dd_opens = 0;
|
|
|
|
|
|
|
|
return(dd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
netif_fini(dd)
|
|
|
|
struct devdata *dd;
|
|
|
|
{
|
|
|
|
struct saioreq *si;
|
|
|
|
|
|
|
|
si = &dd->dd_si;
|
|
|
|
|
|
|
|
#ifdef NETIF_DEBUG
|
|
|
|
if (debug)
|
|
|
|
printf("netif_fini: calling prom_iclose\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
prom_iclose(si);
|
|
|
|
/* Dellocate the transmit/receive buffers. */
|
|
|
|
if (dd->rbuf) {
|
|
|
|
dvma_free(dd->rbuf, dd->rbuf_len);
|
|
|
|
dd->rbuf = NULL;
|
|
|
|
}
|
|
|
|
if (dd->tbuf) {
|
|
|
|
dvma_free(dd->tbuf, dd->tbuf_len);
|
|
|
|
dd->tbuf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
netif_attach(nif, s, aux)
|
|
|
|
struct netif *nif;
|
|
|
|
struct iodesc *s;
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct devdata *dd;
|
|
|
|
|
|
|
|
dd = nif->nif_devdata;
|
|
|
|
if (dd == NULL) {
|
|
|
|
dd = netif_init(aux);
|
|
|
|
if (dd == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
nif->nif_devdata = dd;
|
|
|
|
}
|
|
|
|
dd->dd_opens++;
|
|
|
|
MACPY(dd->dd_myea, s->myea);
|
|
|
|
s->io_netif = nif;
|
1995-06-10 02:19:25 +04:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
void
|
|
|
|
netif_detach(nif)
|
|
|
|
struct netif *nif;
|
|
|
|
{
|
|
|
|
struct devdata *dd;
|
|
|
|
|
|
|
|
dd = nif->nif_devdata;
|
|
|
|
if (dd == NULL)
|
|
|
|
return;
|
|
|
|
dd->dd_opens--;
|
|
|
|
if (dd->dd_opens > 0)
|
|
|
|
return;
|
|
|
|
netif_fini(dd);
|
|
|
|
nif->nif_devdata = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
netif_open(aux)
|
|
|
|
void *aux;
|
|
|
|
{
|
|
|
|
struct netif *nif;
|
|
|
|
struct iodesc *s;
|
|
|
|
int fd, error;
|
|
|
|
|
|
|
|
/* find a free socket */
|
|
|
|
for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
|
|
|
|
if (s->io_netif == NULL)
|
|
|
|
goto found;
|
|
|
|
errno = EMFILE;
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
found:
|
|
|
|
bzero(s, sizeof(*s));
|
|
|
|
nif = &prom_nif;
|
|
|
|
error = netif_attach(nif, s);
|
|
|
|
if (error != 0) {
|
|
|
|
errno = error;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
1995-06-10 02:19:25 +04:00
|
|
|
int
|
|
|
|
netif_close(fd)
|
|
|
|
int fd;
|
|
|
|
{
|
1996-01-30 02:41:03 +03:00
|
|
|
struct iodesc *s;
|
|
|
|
struct netif *nif;
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
if (fd < 0 || fd >= SOPEN_MAX) {
|
1995-06-10 02:19:25 +04:00
|
|
|
errno = EBADF;
|
|
|
|
return(-1);
|
|
|
|
}
|
1996-01-30 02:41:03 +03:00
|
|
|
s = &sockets[fd];
|
|
|
|
nif = s->io_netif;
|
|
|
|
/* Already closed? */
|
|
|
|
if (nif == NULL)
|
|
|
|
return(0);
|
|
|
|
netif_detach(nif);
|
|
|
|
s->io_netif = NULL;
|
|
|
|
return(0);
|
|
|
|
}
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
|
|
|
|
struct iodesc *
|
|
|
|
socktodesc(fd)
|
|
|
|
int fd;
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= SOPEN_MAX) {
|
|
|
|
errno = EBADF;
|
|
|
|
return (NULL);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
1996-01-30 02:41:03 +03:00
|
|
|
return (&sockets[fd]);
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
|
1995-06-10 02:19:25 +04:00
|
|
|
/*
|
|
|
|
* Send a packet. The ether header is already there.
|
|
|
|
* Return the length sent (or -1 on error).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
netif_put(desc, pkt, len)
|
|
|
|
struct iodesc *desc;
|
|
|
|
void *pkt;
|
1995-09-23 07:42:17 +04:00
|
|
|
size_t len;
|
1995-06-10 02:19:25 +04:00
|
|
|
{
|
1996-01-30 02:41:03 +03:00
|
|
|
struct netif *nif;
|
|
|
|
struct devdata *dd;
|
1995-06-10 02:19:25 +04:00
|
|
|
struct saioreq *si;
|
|
|
|
struct saif *sif;
|
|
|
|
char *dmabuf;
|
1996-01-30 02:41:03 +03:00
|
|
|
int rv, slen;
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#ifdef NETIF_DEBUG
|
1996-01-30 02:41:03 +03:00
|
|
|
if (debug > 1) {
|
1995-06-10 02:19:25 +04:00
|
|
|
struct ether_header *eh;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
nif = desc->io_netif;
|
|
|
|
dd = nif->nif_devdata;
|
|
|
|
si = &dd->dd_si;
|
1995-06-10 02:19:25 +04:00
|
|
|
sif = si->si_sif;
|
1996-01-30 02:41:03 +03:00
|
|
|
slen = len;
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (sif == NULL)
|
|
|
|
panic("netif_put: no saif ptr\n");
|
|
|
|
#endif
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
/*
|
|
|
|
* Copy into our transmit buffer because the PROM
|
|
|
|
* network driver might continue using the packet
|
|
|
|
* after the sif_xmit call returns. We never send
|
|
|
|
* very much data anyway, so the copy is fine.
|
|
|
|
*/
|
|
|
|
if (slen > dd->tbuf_len)
|
|
|
|
panic("netif_put: slen=%d\n", slen);
|
|
|
|
bcopy(pkt, dd->tbuf, slen);
|
|
|
|
|
|
|
|
if (slen < 60) {
|
|
|
|
slen = 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = (*sif->sif_xmit)(si->si_devdata, dd->tbuf, slen);
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#ifdef NETIF_DEBUG
|
1996-01-30 02:41:03 +03:00
|
|
|
if (debug > 1)
|
1995-06-10 02:19:25 +04:00
|
|
|
printf("netif_put: xmit returned %d\n", rv);
|
|
|
|
#endif
|
1996-01-30 02:41:03 +03:00
|
|
|
/*
|
|
|
|
* Just ignore the return value. If the PROM transmit
|
|
|
|
* function fails, it will make some noise, such as:
|
|
|
|
* le: No Carrier
|
|
|
|
*/
|
1995-06-10 02:19:25 +04:00
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
return len;
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a packet, including the ether header.
|
|
|
|
* Return the total length received (or -1 on error).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
netif_get(desc, pkt, maxlen, timo)
|
|
|
|
struct iodesc *desc;
|
|
|
|
void *pkt;
|
1995-09-23 07:42:17 +04:00
|
|
|
size_t maxlen;
|
1996-01-30 02:41:03 +03:00
|
|
|
time_t timo; /* seconds */
|
1995-06-10 02:19:25 +04:00
|
|
|
{
|
1996-01-30 02:41:03 +03:00
|
|
|
struct netif *nif;
|
|
|
|
struct devdata *dd;
|
1995-06-10 02:19:25 +04:00
|
|
|
struct saioreq *si;
|
|
|
|
struct saif *sif;
|
|
|
|
int tick0, tmo_ticks;
|
1996-01-30 02:41:03 +03:00
|
|
|
int rlen = 0;
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#ifdef NETIF_DEBUG
|
1996-01-30 02:41:03 +03:00
|
|
|
if (debug > 1)
|
1995-06-10 02:19:25 +04:00
|
|
|
printf("netif_get: pkt=0x%x, maxlen=%d, tmo=%d\n",
|
|
|
|
pkt, maxlen, timo);
|
|
|
|
#endif
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
nif = desc->io_netif;
|
|
|
|
dd = nif->nif_devdata;
|
|
|
|
si = &dd->dd_si;
|
1995-06-10 02:19:25 +04:00
|
|
|
sif = si->si_sif;
|
|
|
|
|
|
|
|
tmo_ticks = timo * hz;
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
/* Have to receive into our own buffer and copy. */
|
|
|
|
do {
|
|
|
|
tick0 = getticks();
|
|
|
|
do {
|
|
|
|
rlen = (*sif->sif_poll)(si->si_devdata, dd->rbuf);
|
|
|
|
if (rlen != 0)
|
|
|
|
goto break2;
|
|
|
|
} while (getticks() == tick0);
|
|
|
|
} while (--tmo_ticks > 0);
|
|
|
|
|
|
|
|
/* No packet arrived. Better reset the interface. */
|
|
|
|
printf("netif_get: timeout; resetting\n");
|
|
|
|
(*sif->sif_reset)(si->si_devdata, si);
|
|
|
|
|
|
|
|
break2:
|
1995-06-10 02:19:25 +04:00
|
|
|
|
|
|
|
#ifdef NETIF_DEBUG
|
1996-01-30 02:41:03 +03:00
|
|
|
if (debug > 1)
|
|
|
|
printf("netif_get: received rlen=%d\n", rlen);
|
1995-06-10 02:19:25 +04:00
|
|
|
#endif
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
/* Need at least a valid Ethernet header. */
|
|
|
|
if (rlen < 12)
|
1995-06-10 02:19:25 +04:00
|
|
|
return -1;
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
/* If we went beyond our buffer, were dead! */
|
|
|
|
if (rlen > dd->rbuf_len)
|
|
|
|
panic("netif_get: rlen=%d\n", rlen);
|
|
|
|
|
|
|
|
/* The caller's buffer may be smaller... */
|
|
|
|
if (rlen > maxlen)
|
|
|
|
rlen = maxlen;
|
|
|
|
|
|
|
|
bcopy(dd->rbuf, pkt, rlen);
|
|
|
|
|
1995-06-10 02:19:25 +04:00
|
|
|
#ifdef NETIF_DEBUG
|
1996-01-30 02:41:03 +03:00
|
|
|
if (debug > 1) {
|
1995-06-10 02:19:25 +04:00
|
|
|
struct ether_header *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
|
|
|
|
|
1996-01-30 02:41:03 +03:00
|
|
|
return rlen;
|
1995-06-10 02:19:25 +04:00
|
|
|
}
|