/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Gordon W. Ross.
*
* 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 THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION 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.
*/
/*
* 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.
*/
/*
* Open the PROM device.
* Return netif ptr on success.
*/
struct devdata *
netif_init(void *aux)
{
struct devdata *dd = &netif_devdata;
struct saioreq *si;
struct bootparam *bp;
int error;
/*
* Setup our part of the saioreq.
* (determines what gets opened)
*/
si = &dd->dd_si;
memset(si, 0, sizeof(*si));
bp = *romVectorPtr->bootParam;
si->si_boottab = bp->bootDevice;
si->si_ctlr = bp->ctlrNum;
si->si_unit = bp->unitNum;
si->si_boff = bp->partNum;
#ifdef NETIF_DEBUG
if (debug)
printf("netif_init: calling prom_iopen\n");
#endif
/*
* 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...
*/
if ((error = prom_iopen(si)) != 0) {
printf("netif_init: prom_iopen, error=%d\n", error);
return (NULL);
}
if (si->si_sif == NULL) {
printf("netif_init: not a network device\n");
prom_iclose(si);
return (NULL);
}
/*
* Send a packet. The ether header is already there.
* Return the length sent (or -1 on error).
*/
int
netif_put(struct iodesc *desc, void *pkt, size_t len)
{
struct netif *nif;
struct devdata *dd;
struct saioreq *si;
struct saif *sif;
int slen;
#ifdef NETIF_DEBUG
if (debug > 1) {
struct ether_header *eh;
#ifdef PARANOID
if (sif == NULL)
panic("netif_put: no saif ptr");
#endif
/*
* 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", slen);
memcpy(dd->tbuf, pkt, slen);
#ifdef NETIF_DEBUG
if (debug > 1)
printf("netif_put: xmit returned %d\n", rv);
#endif
/*
* Just ignore the return value. If the PROM transmit
* function fails, it will make some noise, such as:
* le: No Carrier
*/
return len;
}
/*
* Receive a packet, including the ether header.
* Return the total length received (or -1 on error).
*/
ssize_t
netif_get(struct iodesc *desc, void *pkt, size_t maxlen, saseconds_t timo)
{
struct netif *nif;
struct devdata *dd;
struct saioreq *si;
struct saif *sif;
int tick0, tmo_ticks;
int rlen = 0;
/* 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);
#if 0
/* No packet arrived. Better reset the interface. */
printf("netif_get: timeout; resetting\n");
(*sif->sif_reset)(si->si_devdata, si);
#endif
break2:
#ifdef NETIF_DEBUG
if (debug > 1)
printf("netif_get: received rlen=%d\n", rlen);
#endif
/* Need at least a valid Ethernet header. */
if (rlen < 12)
return -1;
/* If we went beyond our buffer, were dead! */
if (rlen > dd->rbuf_len)
panic("netif_get: rlen=%d", rlen);
/* The caller's buffer may be smaller... */
if (rlen > maxlen)
rlen = maxlen;
/*
* Copy our Ethernet address into the passed array.
*/
void
netif_getether(struct saif *sif, u_char *ea)
{
char *rev;
if (_is3x == 0) {
/*
* Sun3: These usually have old PROMs
* without the sif_macaddr function, but
* reading the IDPROM on these machines is
* very easy, so just always do that.
*/
idprom_etheraddr(ea);
return;
}
/*
* Sun3X: Want to use sif->sif_macaddr(), but
* it's only in PROM revisions 3.0 and later,
* so we have to check the PROM rev first.
* Note that old PROMs prefix the rev string
* with "Rev " (i.e. "Rev 2.6").
*/
rev = romVectorPtr->monId;
if (!strncmp(rev, "Rev ", 4))
rev += 4;
if (!strncmp(rev, "3.", 2)) {
/* Great! We can call the PROM. */
(*sif->sif_macaddr)(ea);
return;
}
/*
* Sun3X with PROM rev < 3.0.
* Finding the IDPROM is a pain, but
* we have no choice. Warn the user.
*/
printf("netboot: Old PROM Rev (%s)\n", rev);
idprom_etheraddr(ea);
}