/*
* Copyright (c) 1995 Mika Kortelainen
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Mika Kortelainen
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* 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.
*
* Thanks for Aspecs Oy (Finland) for the data book for the NIC used
* in this card and also many thanks for the Resource Management Force
* (QuickNet card manufacturer) and especially Daniel Koch for providing
* me with the necessary 'inside' information to write the driver.
*
* This is partly based on other code:
* - if_ed.c: basic function structure for Ethernet driver and now also
* qn_put() is done similarly, i.e. no extra packet buffers.
*
* Device driver for National Semiconductor DS8390/WD83C690 based ethernet
* adapters.
*
* Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
*
* Copyright (C) 1993, David Greenman. This software may be used,
* modified, copied, distributed, and sold, in both source and binary
* form provided that the above copyright and these terms are retained.
* Under no circumstances is the author responsible for the proper
* functioning of this software, nor does the author assume any
* responsibility for damages incurred with its use.
*
* - if_es.c: used as an example of -current driver
*
* Copyright (c) 1995 Michael L. Hitch
* All rights reserved.
*
* - if_fe.c: some ideas for error handling for qn_rint() which might
* have fixed those random lock ups, too.
*
* All Rights Reserved, Copyright (C) Fujitsu Limited 1995
*
*
* TODO:
* - add multicast support
*/
/*
* Interface exists: make available by filling in network interface
* record. System will initialize the interface when it is ready
* to accept packets.
*/
void
qnattach(device_t parent, device_t self, void *aux)
{
struct zbus_args *zap;
struct qn_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
u_int8_t myaddr[ETHER_ADDR_LEN];
/*
* The ethernet address of the board (1st three bytes are the vendor
* address, the rest is the serial number of the board).
*/
myaddr[0] = 0x5c;
myaddr[1] = 0x5c;
myaddr[2] = 0x00;
myaddr[3] = (zap->serno >> 16) & 0xff;
myaddr[4] = (zap->serno >> 8) & 0xff;
myaddr[5] = zap->serno & 0xff;
/* set interface to stopped condition (reset) */
qnstop(sc);
/* QuickNet magic. Done ONLY once, otherwise a lockup occurs. */
if (retry == 0) {
*((u_short volatile *)(sc->sc_nic_base + QNET_MAGIC)) = 0;
retry = 1;
}
/* Enable data link controller. */
*sc->nic_reset = ENABLE_DLC;
/* Attempt to start output, if any. */
if_schedule_deferred_start(ifp);
}
/*
* Device timeout/watchdog routine. Entered if the device neglects to
* generate an interrupt after a transmit has been started on it.
*/
void
qnwatchdog(struct ifnet *ifp)
{
struct qn_softc *sc = ifp->if_softc;
/*
* Start output on interface. Get another datagram to send
* off the interface queue, and copy it to the
* interface before starting the output.
*
* This assumes that it is called inside a critical section...
*
*/
void
qnstart(struct ifnet *ifp)
{
struct qn_softc *sc = ifp->if_softc;
struct mbuf *m;
u_short len;
int timout = 60000;
if ((ifp->if_flags & IFF_RUNNING) == 0)
return;
if (sc->transmit_pending)
return;
IF_DEQUEUE(&ifp->if_snd, m);
if (m == 0)
return;
/*
* If bpf is listening on this interface, let it
* see the packet before we commit it to the wire
*
* (can't give the copy in QuickNet card RAM to bpf, because
* that RAM is not visible to the host but is read from FIFO)
*/
bpf_mtap(ifp, m, BPF_D_OUT);
len = qn_put(sc->nic_fifo, m);
m_freem(m);
/* Wait for the packet to really leave. */
while (!(*sc->nic_t_status & T_TMT_OK) && --timout) {
if ((timout % 10000) == 0)
log(LOG_INFO, "qn: timeout...\n");
}
if (timout == 0)
/* Maybe we should try to recover from this one? */
/* But now, let's just fall thru and hope the best... */
log(LOG_INFO, "qn: transmit timeout (fatal?)\n");
/* Clear the status register. */
*sc->nic_r_status = CLEAR_R_ERR;
/*
* Was there some error?
* Some of them are senseless because they are masked off.
* XXX
*/
if (rstat & R_INT_OVR_FLO) {
#ifdef QN_DEBUG
log(LOG_INFO, "Overflow\n");
#endif
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
}
if (rstat & R_INT_CRC_ERR) {
#ifdef QN_DEBUG
log(LOG_INFO, "CRC Error\n");
#endif
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
}
if (rstat & R_INT_ALG_ERR) {
#ifdef QN_DEBUG
log(LOG_INFO, "Alignment error\n");
#endif
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
}
if (rstat & R_INT_SRT_PKT) {
/* Short packet (these may occur and are
* no reason to worry about - or maybe
* they are?).
*/
#ifdef QN_DEBUG
log(LOG_INFO, "Short packet\n");
#endif
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
}
if (rstat & 0x4040) {
#ifdef QN_DEBUG
log(LOG_INFO, "Bus read error\n");
#endif
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
qnreset(sc);
}
/*
* Read at most MAX_PACKETS packets per interrupt
*/
for (i = 0; i < MAX_PACKETS; i++) {
if (*sc->nic_r_mode & RM_BUF_EMP)
/* Buffer empty. */
break;
/*
* Read the first word: upper byte contains useful
* information.
*/
status = *sc->nic_fifo;
if ((status & 0x7000) != 0x2000) {
log(LOG_INFO, "qn: ERROR: status=%04x\n", status);
continue;
}
/*
* Read packet length (byte-swapped).
* CRC is stripped off by the NIC.
*/
len = *sc->nic_fifo;
len = ((len << 8) & 0xff00) | ((len >> 8) & 0x00ff);
#ifdef QN_CHECKS
if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
len < ETHER_HDR_LEN) {
log(LOG_WARNING,
"%s: received a %s packet? (%u bytes)\n",
device_xname(sc->sc_dev),
len < ETHER_HDR_LEN ? "partial" : "big", len);
if_statinc(&sc->sc_ethercom.ec_if, if_ierrors);
continue;
}
#endif
#ifdef QN_CHECKS
if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
log(LOG_WARNING,
"%s: received a short packet? (%u bytes)\n",
device_xname(sc->sc_dev), len);
#endif
/* Read the packet. */
qn_get_packet(sc, len);
}
#ifdef QN_DEBUG
/* This print just to see whether MAX_PACKETS is large enough. */
if (i == MAX_PACKETS)
log(LOG_INFO, "used all the %d loops\n", MAX_PACKETS);
#endif
}
/*
* If the driver has not been initialized, just return immediately.
* This also happens if there is no QuickNet board present.
*/
if (sc->sc_base == NULL)
return (0);
/* Get interrupt statuses and masks. */
rint = (*sc->nic_r_status) & NIC_R_MASK;
tintmask = *sc->nic_t_mask;
tint = (*sc->nic_t_status) & tintmask;
if (tint == 0 && rint == 0)
return (0);
/* Disable interrupts so that we won't miss anything. */
*sc->nic_r_mask = CLEAR_R_MASK;
*sc->nic_t_mask = CLEAR_T_MASK;
/*
* Handle transmitter interrupts. Some of them are not asked for
* but do happen, anyway.
*/
if (!sc->transmit_pending)
if_schedule_deferred_start(&sc->sc_ethercom.ec_if);
else if (return_tintmask == 1)
*sc->nic_t_mask = tintmask;
/* Set receive interrupt mask back. */
*sc->nic_r_mask = NIC_R_MASK;
return (1);
}
/*
* Process an ioctl request. This code needs some work - it looks pretty ugly.
* I somehow think that this is quite a common excuse... ;-)
*/
int
qnioctl(register struct ifnet *ifp, u_long cmd, void *data)
{
struct qn_softc *sc = ifp->if_softc;
register struct ifaddr *ifa = (struct ifaddr *)data;
#if 0
struct ifreg *ifr = (struct ifreg *)data;
#endif
int s, error = 0;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
/* XXX see the comment in ed_ioctl() about code re-use */
if ((ifp->if_flags & IFF_UP) == 0 &&
(ifp->if_flags & IFF_RUNNING) != 0) {
/*
* If interface is marked down and it is running, then
* stop it.
*/
#ifdef QN_DEBUG1
qn_dump(sc);
#endif
qnstop(sc);
ifp->if_flags &= ~IFF_RUNNING;
} else if ((ifp->if_flags & IFF_UP) != 0 &&
(ifp->if_flags & IFF_RUNNING) == 0) {
/*
* If interface is marked up and it is stopped, then
* start it.
*/
qninit(sc);
} else {
/*
* Something else... we won't do anything so we won't
* break anything (hope so).
*/
#ifdef QN_DEBUG1
log(LOG_INFO, "Else branch...\n");
#endif
}
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
log(LOG_INFO, "qnioctl: multicast not done yet\n");
#if 0
if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
/*
* Multicast list has changed; set the hardware filter
* accordingly.
*/
log(LOG_INFO, "qnioctl: multicast not done yet\n");
error = 0;
}
#else
error = EINVAL;
#endif
break;