Home | History | Annotate | Line # | Download | only in qbus
      1  1.29   tsutsui /*	$NetBSD: if_qt.c,v 1.29 2025/01/08 18:36:08 tsutsui Exp $	*/
      2   1.1     ragge /*
      3   1.1     ragge  * Copyright (c) 1992 Steven M. Schultz
      4   1.1     ragge  * All rights reserved.
      5   1.1     ragge  *
      6   1.1     ragge  * Redistribution and use in source and binary forms, with or without
      7   1.1     ragge  * modification, are permitted provided that the following conditions
      8   1.1     ragge  * are met:
      9   1.1     ragge  * 1. Redistributions of source code must retain the above copyright
     10   1.1     ragge  *    notice, this list of conditions and the following disclaimer.
     11   1.1     ragge  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1     ragge  *    notice, this list of conditions and the following disclaimer in the
     13   1.1     ragge  *    documentation and/or other materials provided with the distribution.
     14   1.1     ragge  * 3. The name of the author may not be used to endorse or promote products
     15   1.1     ragge  *    derived from this software without specific prior written permission
     16   1.1     ragge  *
     17   1.1     ragge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1     ragge  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1     ragge  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1     ragge  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1     ragge  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1     ragge  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1     ragge  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1     ragge  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1     ragge  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26   1.1     ragge  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1     ragge  *
     28   1.1     ragge  *	@(#)if_qt.c     1.2 (2.11BSD) 2/20/93
     29   1.1     ragge  */
     30   1.1     ragge /*
     31   1.1     ragge  *
     32   1.6    simonb  * Modification History
     33   1.1     ragge  * 23-Feb-92 -- sms
     34   1.1     ragge  *	Rewrite the buffer handling so that fewer than the maximum number of
     35   1.1     ragge  *	buffers may be used (32 receive and 12 transmit buffers consume 66+kb
     36   1.1     ragge  *	of main system memory in addition to the internal structures in the
     37   1.1     ragge  *	networking code).  A freelist of available buffers is maintained now.
     38   1.6    simonb  *	When I/O operations complete the associated buffer is placed on the
     39   1.6    simonb  *	freelist (a single linked list for simplicity) and when an I/O is
     40   1.6    simonb  *	started a buffer is pulled off the list.
     41   1.1     ragge  *
     42   1.1     ragge  * 20-Feb-92 -- sms
     43   1.1     ragge  *	It works!  Darned board couldn't handle "short" rings - those rings
     44   1.1     ragge  *	where only half the entries were made available to the board (the
     45   1.1     ragge  *	ring descriptors were the full size, merely half the entries were
     46  1.28   tsutsui  *	flagged as belonging always to the driver).  Grrrr.  Would have thought
     47   1.6    simonb  *	the board could skip over those entries reserved by the driver.
     48   1.6    simonb  *	Now to find a way not to have to allocated 32+12 times 1.5kb worth of
     49   1.1     ragge  *	buffers...
     50   1.1     ragge  *
     51   1.1     ragge  * 03-Feb-92 -- sms
     52   1.1     ragge  *	Released but still not working.  The driver now responds to arp and
     53   1.1     ragge  *	ping requests.  The board is apparently not returning ring descriptors
     54   1.1     ragge  *	to the driver so eventually we run out of buffers.  Back to the
     55   1.1     ragge  *	drawing board.
     56   1.1     ragge  *
     57   1.1     ragge  * 28-Dec-92 -- sms
     58   1.1     ragge  *	Still not released.  Hiatus in finding free time and thin-netting
     59   1.1     ragge  *	the systems (many thanks Terry!).
     60   1.1     ragge  *	Added logic to dynamically allocate a vector and initialize it.
     61   1.1     ragge  *
     62   1.1     ragge  * 23-Oct-92 -- sms
     63   1.1     ragge  *	The INIT block must (apparently) be quadword aligned [no thanks to
     64   1.1     ragge  *	the manual for not mentioning that fact].  The necessary alignment
     65   1.1     ragge  *	is achieved by allocating the INIT block from main memory ('malloc'
     66   1.1     ragge  *	guarantees click alignment) and mapping it as needed (which is _very_
     67   1.1     ragge  *	infrequently).  A check for quadword alignment of the ring descriptors
     68   1.1     ragge  *	was added - at present the descriptors are properly aligned, if this
     69   1.1     ragge  *	should change then something will have to be done (like do it "right").
     70   1.1     ragge  *	Darned alignment restrictions!
     71   1.1     ragge  *
     72   1.6    simonb  *	A couple of typos were corrected (missing parentheses, reversed
     73   1.1     ragge  *	arguments to printf calls, etc).
     74   1.1     ragge  *
     75   1.1     ragge  * 13-Oct-92 -- sms (at) wlv.iipo.gtegsc.com
     76   1.1     ragge  *	Created based on the DELQA-PLUS addendum to DELQA User's Guide.
     77   1.6    simonb  *	This driver ('qt') is selected at system configuration time.  If the
     78   1.6    simonb  *	board *	is not a DELQA-YM an error message will be printed and the
     79   1.1     ragge  *	interface will not be attached.
     80   1.2     ragge  */
     81   1.2     ragge 
     82   1.2     ragge #include <sys/cdefs.h>
     83  1.29   tsutsui __KERNEL_RCSID(0, "$NetBSD: if_qt.c,v 1.29 2025/01/08 18:36:08 tsutsui Exp $");
     84   1.2     ragge 
     85   1.2     ragge #include "opt_inet.h"
     86   1.1     ragge 
     87   1.2     ragge #include <sys/param.h>
     88   1.2     ragge #include <sys/systm.h>
     89   1.2     ragge #include <sys/mbuf.h>
     90   1.2     ragge #include <sys/protosw.h>
     91   1.2     ragge #include <sys/socket.h>
     92   1.2     ragge #include <sys/ioctl.h>
     93  1.13     joerg #include <sys/device.h>
     94   1.2     ragge #include <sys/errno.h>
     95   1.2     ragge #include <sys/syslog.h>
     96   1.2     ragge #include <sys/time.h>
     97   1.2     ragge #include <sys/kernel.h>
     98   1.2     ragge 
     99   1.2     ragge #include <net/if.h>
    100   1.2     ragge #include <net/if_ether.h>
    101   1.2     ragge #include <net/route.h>
    102  1.22   msaitoh #include <net/bpf.h>
    103   1.1     ragge 
    104   1.1     ragge #ifdef INET
    105   1.2     ragge #include <sys/domain.h>
    106   1.2     ragge #include <netinet/in.h>
    107   1.2     ragge #include <netinet/in_systm.h>
    108   1.2     ragge #include <netinet/in_var.h>
    109   1.2     ragge #include <netinet/ip.h>
    110   1.1     ragge #endif
    111   1.1     ragge 
    112  1.12        ad #include <sys/bus.h>
    113   1.2     ragge 
    114   1.2     ragge #include <dev/qbus/ubavar.h>
    115   1.2     ragge #include <dev/qbus/if_uba.h>
    116   1.2     ragge #include <dev/qbus/if_qtreg.h>
    117   1.2     ragge 
    118   1.2     ragge #define NRCV	QT_MAX_RCV	/* Receive descriptors (must be == 32) */
    119   1.2     ragge #define NXMT	QT_MAX_XMT	/* Transmit descriptors	(must be == 12) */
    120   1.2     ragge #if	NRCV != 32 || NXMT != 12
    121   1.2     ragge 	hardware requires these sizes.
    122   1.1     ragge #endif
    123   1.6    simonb 
    124   1.4     ragge /*
    125   1.4     ragge  * Control data structures, must be in DMA-friendly memory.
    126   1.4     ragge  */
    127   1.2     ragge struct	qt_cdata {
    128   1.2     ragge 	struct	qt_init qc_init;	/* Init block			*/
    129   1.2     ragge 	struct	qt_rring qc_r[NRCV];	/* Receive descriptor ring	*/
    130   1.2     ragge 	struct	qt_tring qc_t[NXMT];	/* Transmit descriptor ring	*/
    131   1.2     ragge };
    132   1.1     ragge 
    133   1.1     ragge struct	qt_softc {
    134  1.14      matt 	device_t sc_dev;		/* Configuration common part */
    135   1.2     ragge 	struct	ethercom is_ec;		/* common part - must be first  */
    136  1.14      matt 	struct uba_softc *sc_uh;
    137   1.2     ragge 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
    138   1.6    simonb #define	is_if	is_ec.ec_if		/* network-visible interface	*/
    139   1.2     ragge 	u_int8_t is_addr[ETHER_ADDR_LEN]; /* hardware Ethernet address	*/
    140   1.2     ragge 	bus_space_tag_t	sc_iot;
    141   1.2     ragge 	bus_addr_t	sc_ioh;
    142   1.2     ragge 
    143   1.4     ragge 	struct	ubinfo	sc_ui;		/* control block address desc	*/
    144   1.4     ragge 	struct	qt_cdata *sc_ib;	/* virt address of ctrl block	*/
    145   1.4     ragge 	struct	qt_cdata *sc_pib;	/* phys address of ctrl block	*/
    146   1.2     ragge 
    147   1.2     ragge 	struct	ifubinfo sc_ifuba;	/* UNIBUS resources */
    148   1.2     ragge 	struct	ifrw sc_ifr[NRCV];	/* UNIBUS receive buffer maps */
    149   1.2     ragge 	struct	ifxmt sc_ifw[NXMT];	/* UNIBUS receive buffer maps */
    150   1.2     ragge 
    151   1.4     ragge 	int	rindex;			/* Receive Completed Index	*/
    152   1.4     ragge 	int	nxtrcv;			/* Next Receive Index		*/
    153   1.4     ragge 	int	nrcv;			/* Number of Receives active	*/
    154   1.1     ragge 
    155   1.2     ragge 	int	xnext;			/* Next descriptor to transmit	*/
    156   1.2     ragge 	int	xlast;			/* Last descriptor transmitted	*/
    157   1.2     ragge 	int	nxmit;			/* # packets in send queue	*/
    158   1.4     ragge 
    159   1.2     ragge 	short	vector;			/* Interrupt vector assigned	*/
    160   1.2     ragge };
    161   1.2     ragge 
    162  1.14      matt static	int qtmatch(device_t, cfdata_t, void *);
    163  1.14      matt static	void qtattach(device_t, device_t, void *);
    164  1.26   rhialto static	void lance_setladrf(struct ethercom *ec, uint16_t *af);
    165   1.2     ragge static	void qtintr(void *);
    166   1.2     ragge static	int qtinit(struct ifnet *);
    167  1.11  christos static	int qtioctl(struct ifnet *, u_long, void *);
    168   1.2     ragge static	int qtturbo(struct qt_softc *);
    169   1.2     ragge static	void qtstart(struct ifnet *ifp);
    170   1.4     ragge static	void qtstop(struct ifnet *ifp, int disable);
    171   1.2     ragge static	void qtsrr(struct qt_softc *, int);
    172   1.2     ragge static	void qtrint(struct qt_softc *sc);
    173   1.2     ragge static	void qttint(struct qt_softc *sc);
    174   1.2     ragge 
    175  1.28   tsutsui #ifdef notyet
    176  1.28   tsutsui static	void qtreset(struct qt_softc *sc);
    177  1.28   tsutsui #endif
    178   1.6    simonb 
    179  1.14      matt CFATTACH_DECL_NEW(qt, sizeof(struct qt_softc),
    180   1.2     ragge     qtmatch, qtattach, NULL, NULL);
    181   1.1     ragge 
    182   1.1     ragge /*
    183   1.6    simonb  * Maximum packet size needs to include 4 bytes for the CRC
    184   1.1     ragge  * on received packets.
    185  1.28   tsutsui  */
    186  1.29   tsutsui #define MAXPACKETSIZE (ETHERMTU + sizeof (struct ether_header) + ETHER_CRC_LEN)
    187   1.1     ragge #define	MINPACKETSIZE 64
    188   1.1     ragge 
    189   1.3     ragge #define QT_WCSR(csr, val) \
    190   1.3     ragge 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
    191   1.3     ragge #define QT_RCSR(csr) \
    192   1.3     ragge 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
    193   1.3     ragge 
    194   1.3     ragge 
    195   1.2     ragge #define loint(x)	((int)(x) & 0xffff)
    196   1.2     ragge #define hiint(x)	(((int)(x) >> 16) & 0x3f)
    197   1.2     ragge 
    198   1.1     ragge /*
    199   1.2     ragge  * Check if this card is a turbo delqa.
    200   1.2     ragge  */
    201   1.2     ragge int
    202  1.14      matt qtmatch(device_t parent, cfdata_t cf, void *aux)
    203   1.2     ragge {
    204  1.14      matt 	struct qt_softc ssc;
    205  1.14      matt 	struct qt_softc *sc = &ssc;
    206  1.14      matt 	struct uba_attach_args *ua = aux;
    207  1.14      matt 	struct uba_softc *uh = device_private(parent);
    208  1.14      matt 	struct qt_init *qi;
    209   1.4     ragge 	struct ubinfo ui;
    210   1.2     ragge 
    211   1.3     ragge 	sc->sc_iot = ua->ua_iot;
    212   1.3     ragge 	sc->sc_ioh = ua->ua_ioh;
    213   1.2     ragge 	if (qtturbo(sc) == 0)
    214   1.4     ragge 		return 0; /* Not a turbo card */
    215   1.4     ragge 
    216   1.4     ragge 	/* Force the card to interrupt */
    217   1.4     ragge 	ui.ui_size = sizeof(struct qt_init);
    218  1.14      matt 	if (ubmemalloc(uh, &ui, 0))
    219   1.4     ragge 		return 0; /* Failed */
    220   1.4     ragge 	qi = (struct qt_init *)ui.ui_vaddr;
    221   1.4     ragge 	memset(qi, 0, sizeof(struct qt_init));
    222  1.14      matt 	qi->vector = uh->uh_lastiv - 4;
    223   1.4     ragge 	qi->options = INIT_OPTIONS_INT;
    224   1.6    simonb 
    225   1.4     ragge 	QT_WCSR(CSR_IBAL, loint(ui.ui_baddr));
    226   1.4     ragge 	QT_WCSR(CSR_IBAH, hiint(ui.ui_baddr));
    227   1.4     ragge 	QT_WCSR(CSR_SRQR, 2);
    228   1.4     ragge 	delay(100000); /* Wait some time for interrupt */
    229   1.4     ragge 	QT_WCSR(CSR_SRQR, 3); /* Stop card */
    230   1.4     ragge 
    231  1.14      matt 	ubmemfree(uh, &ui);
    232   1.2     ragge 
    233   1.2     ragge 	return 10;
    234   1.2     ragge }
    235   1.1     ragge 
    236   1.1     ragge 
    237   1.1     ragge /*
    238   1.1     ragge  * Interface exists.  More accurately, something exists at the CSR (see
    239   1.1     ragge  * sys/sys_net.c) -- there's no guarantee it's a DELQA-YM.
    240   1.1     ragge  *
    241   1.1     ragge  * The ring descriptors are initialized, the buffers allocated using first the
    242   1.1     ragge  * DMA region allocated at network load time and then later main memory.  The
    243   1.1     ragge  * INIT block is filled in and the device is poked/probed to see if it really
    244   1.1     ragge  * is a DELQA-YM.  If the device is not a -YM then a message is printed and
    245   1.1     ragge  * the 'if_attach' call is skipped.  For a -YM the START command is issued,
    246   1.1     ragge  * but the device is not marked as running|up - that happens at interrupt level
    247   1.1     ragge  * when the device interrupts to say it has started.
    248  1.28   tsutsui  */
    249   1.1     ragge 
    250   1.2     ragge void
    251  1.14      matt qtattach(device_t parent, device_t self, void *aux)
    252  1.14      matt {
    253  1.14      matt 	struct qt_softc *sc = device_private(self);
    254  1.14      matt 	struct ifnet *ifp = &sc->is_if;
    255   1.2     ragge 	struct uba_attach_args *ua = aux;
    256   1.2     ragge 
    257  1.16      matt 	sc->sc_dev = self;
    258  1.16      matt 
    259   1.6    simonb 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, qtintr, sc,
    260   1.2     ragge 	    &sc->sc_intrcnt);
    261   1.2     ragge 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    262  1.14      matt 	    device_xname(sc->sc_dev), "intr");
    263   1.2     ragge 
    264  1.14      matt 	sc->sc_uh = device_private(parent);
    265   1.3     ragge 	sc->sc_iot = ua->ua_iot;
    266   1.3     ragge 	sc->sc_ioh = ua->ua_ioh;
    267  1.14      matt 	sc->sc_uh->uh_lastiv -= 4;
    268  1.14      matt 	sc->vector = sc->sc_uh->uh_lastiv;
    269   1.1     ragge 
    270   1.1     ragge /*
    271   1.1     ragge  * Now allocate the buffers and initialize the buffers.  This should _never_
    272   1.1     ragge  * fail because main memory is allocated after the DMA pool is used up.
    273  1.28   tsutsui  */
    274   1.1     ragge 
    275   1.3     ragge 	sc->is_addr[0] = QT_RCSR(0);
    276   1.3     ragge 	sc->is_addr[1] = QT_RCSR(2);
    277   1.3     ragge 	sc->is_addr[2] = QT_RCSR(4);
    278   1.3     ragge 	sc->is_addr[3] = QT_RCSR(6);
    279   1.3     ragge 	sc->is_addr[4] = QT_RCSR(8);
    280   1.3     ragge 	sc->is_addr[5] = QT_RCSR(10);
    281   1.2     ragge 
    282  1.14      matt 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    283   1.2     ragge 	ifp->if_softc = sc;
    284   1.4     ragge 	ifp->if_flags = IFF_BROADCAST|IFF_MULTICAST;
    285   1.2     ragge 	ifp->if_ioctl = qtioctl;
    286   1.2     ragge 	ifp->if_start = qtstart;
    287   1.2     ragge 	ifp->if_init = qtinit;
    288   1.4     ragge 	ifp->if_stop = qtstop;
    289   1.2     ragge 	IFQ_SET_READY(&ifp->if_snd);
    290   1.6    simonb 
    291   1.2     ragge 	printf("\n%s: delqa-plus in Turbo mode, hardware address %s\n",
    292  1.14      matt 	    device_xname(sc->sc_dev), ether_sprintf(sc->is_addr));
    293   1.2     ragge 	if_attach(ifp);
    294   1.2     ragge 	ether_ifattach(ifp, sc->is_addr);
    295  1.14      matt }
    296   1.6    simonb 
    297   1.2     ragge int
    298  1.14      matt qtturbo(struct qt_softc *sc)
    299  1.14      matt {
    300  1.14      matt 	int i;
    301   1.1     ragge 
    302   1.1     ragge /*
    303   1.1     ragge  * Issue the software reset.  Delay 150us.  The board should now be in
    304   1.1     ragge  * DELQA-Normal mode.  Set ITB and DEQTA select.  If both bits do not
    305   1.1     ragge  * stay turned on then the board is not a DELQA-YM.
    306  1.28   tsutsui  */
    307   1.3     ragge 	QT_WCSR(CSR_ARQR, ARQR_SR);
    308   1.3     ragge 	QT_WCSR(CSR_ARQR, 0);
    309   1.1     ragge 	delay(150L);
    310   1.1     ragge 
    311   1.3     ragge 	QT_WCSR(CSR_SRR, 0x8001);	/* MS | ITB */
    312   1.3     ragge 	i = QT_RCSR(CSR_SRR);
    313   1.3     ragge 	QT_WCSR(CSR_SRR, 0x8000);	/* Turn off ITB, set DELQA select */
    314  1.14      matt 	if (i != 0x8001)
    315  1.28   tsutsui 		return 0;
    316   1.1     ragge /*
    317   1.1     ragge  * Board is a DELQA-YM.  Send the commands to enable Turbo mode.  Delay
    318   1.1     ragge  * 1 second, testing the SRR register every millisecond to see if the
    319   1.1     ragge  * board has shifted to Turbo mode.
    320  1.28   tsutsui  */
    321   1.3     ragge 	QT_WCSR(CSR_XCR0, 0x0baf);
    322   1.3     ragge 	QT_WCSR(CSR_XCR1, 0xff00);
    323  1.28   tsutsui 	for (i = 0; i < 1000; i++) {
    324  1.28   tsutsui 		if ((QT_RCSR(CSR_SRR) & SRR_RESP) == 1)
    325   1.1     ragge 			break;
    326   1.1     ragge 		delay(1000L);
    327  1.28   tsutsui 	}
    328  1.28   tsutsui 	if (i >= 1000) {
    329   1.3     ragge 		printf("qt !Turbo\n");
    330  1.28   tsutsui 		return 0;
    331  1.28   tsutsui 	}
    332  1.28   tsutsui 	return 1;
    333  1.14      matt }
    334   1.1     ragge 
    335  1.26   rhialto #define ETHER_CMP(a,b)	memcmp((a), (b), 6)
    336  1.26   rhialto 
    337  1.26   rhialto /*
    338  1.26   rhialto  * Set up the logical address filter.
    339  1.26   rhialto  */
    340  1.26   rhialto void
    341  1.26   rhialto lance_setladrf(struct ethercom *ec, uint16_t *af)
    342  1.26   rhialto {
    343  1.26   rhialto 	struct ifnet *ifp = &ec->ec_if;
    344  1.26   rhialto 	struct ether_multi *enm;
    345  1.26   rhialto 	uint32_t crc;
    346  1.26   rhialto 	struct ether_multistep step;
    347  1.26   rhialto 
    348  1.26   rhialto 	/*
    349  1.26   rhialto 	 * Set up multicast address filter by passing all multicast addresses
    350  1.26   rhialto 	 * through a crc generator, and then using the high order 6 bits as an
    351  1.26   rhialto 	 * index into the 64 bit logical address filter.  The high order bit
    352  1.26   rhialto 	 * selects the word, while the rest of the bits select the bit within
    353  1.26   rhialto 	 * the word.
    354  1.26   rhialto 	 */
    355  1.26   rhialto 
    356  1.26   rhialto 	if (ifp->if_flags & IFF_PROMISC)
    357  1.26   rhialto 		goto allmulti;
    358  1.26   rhialto 
    359  1.26   rhialto 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    360  1.26   rhialto 
    361  1.26   rhialto 	ETHER_LOCK(ec);
    362  1.26   rhialto 	ETHER_FIRST_MULTI(step, ec, enm);
    363  1.26   rhialto 	while (enm != NULL) {
    364  1.26   rhialto 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    365  1.26   rhialto 			/*
    366  1.26   rhialto 			 * We must listen to a range of multicast addresses.
    367  1.26   rhialto 			 * For now, just accept all multicasts, rather than
    368  1.26   rhialto 			 * trying to set only those filter bits needed to match
    369  1.26   rhialto 			 * the range.  (At this time, the only use of address
    370  1.26   rhialto 			 * ranges is for IP multicast routing, for which the
    371  1.26   rhialto 			 * range is big enough to require all bits set.)
    372  1.26   rhialto 			 */
    373  1.26   rhialto 			ETHER_UNLOCK(ec);
    374  1.26   rhialto 			goto allmulti;
    375  1.26   rhialto 		}
    376  1.26   rhialto 
    377  1.26   rhialto 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    378  1.26   rhialto 
    379  1.26   rhialto 		/* Just want the 6 most significant bits. */
    380  1.26   rhialto 		crc >>= 26;
    381  1.26   rhialto 
    382  1.26   rhialto 		/* Set the corresponding bit in the filter. */
    383  1.26   rhialto 		af[crc >> 4] |= 1 << (crc & 0xf);
    384  1.26   rhialto 
    385  1.26   rhialto 		ETHER_NEXT_MULTI(step, enm);
    386  1.26   rhialto 	}
    387  1.26   rhialto 	ETHER_UNLOCK(ec);
    388  1.26   rhialto 	ifp->if_flags &= ~IFF_ALLMULTI;
    389  1.26   rhialto 	return;
    390  1.26   rhialto 
    391  1.26   rhialto allmulti:
    392  1.26   rhialto 	ifp->if_flags |= IFF_ALLMULTI;
    393  1.26   rhialto 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    394  1.26   rhialto }
    395  1.26   rhialto 
    396   1.2     ragge int
    397   1.2     ragge qtinit(struct ifnet *ifp)
    398  1.14      matt {
    399  1.14      matt 	struct qt_softc *sc = ifp->if_softc;
    400  1.14      matt 	struct qt_init *iniblk;
    401   1.2     ragge 	struct ifrw *ifrw;
    402   1.2     ragge 	struct ifxmt *ifxp;
    403  1.28   tsutsui 	struct qt_rring *rp;
    404  1.28   tsutsui 	struct qt_tring *tp;
    405  1.14      matt 	int i, error;
    406   1.6    simonb 
    407   1.5   thorpej 	if (ifp->if_flags & IFF_RUNNING) {
    408   1.5   thorpej 		/* Cancel any pending I/O. */
    409   1.5   thorpej 		qtstop(ifp, 0);
    410   1.5   thorpej 	}
    411   1.2     ragge 
    412   1.2     ragge 	if (sc->sc_ib == NULL) {
    413  1.14      matt 		if (if_ubaminit(&sc->sc_ifuba, sc->sc_uh,
    414   1.2     ragge 		    MCLBYTES, sc->sc_ifr, NRCV, sc->sc_ifw, NXMT)) {
    415  1.28   tsutsui 			printf("%s: can't initialize\n",
    416  1.28   tsutsui 			    device_xname(sc->sc_dev));
    417   1.2     ragge 			ifp->if_flags &= ~IFF_UP;
    418   1.2     ragge 			return 0;
    419   1.2     ragge 		}
    420   1.2     ragge 		sc->sc_ui.ui_size = sizeof(struct qt_cdata);
    421  1.14      matt 		if ((error = ubmemalloc(sc->sc_uh, &sc->sc_ui, 0))) {
    422   1.2     ragge 			printf(": failed ubmemalloc(), error = %d\n", error);
    423   1.2     ragge 			return error;
    424   1.2     ragge 		}
    425   1.2     ragge 		sc->sc_ib = (struct qt_cdata *)sc->sc_ui.ui_vaddr;
    426   1.2     ragge 		sc->sc_pib = (struct qt_cdata *)sc->sc_ui.ui_baddr;
    427   1.2     ragge 
    428   1.2     ragge /*
    429   1.2     ragge  * Fill in most of the INIT block: vector, options (interrupt enable), ring
    430   1.2     ragge  * locations.  The physical address is copied from the ROMs as part of the
    431  1.20      maya  * -YM testing procedure.  The CSR is saved here rather than in qtinit()
    432   1.2     ragge  * because the qtturbo() routine needs it.
    433   1.2     ragge  *
    434   1.2     ragge  * The INIT block must be quadword aligned.  Using malloc() guarantees click
    435   1.2     ragge  * (64 byte) alignment.  Since the only time the INIT block is referenced is
    436   1.2     ragge  * at 'startup' or 'reset' time there is really no time penalty (and a modest
    437   1.2     ragge  * D space savings) involved.
    438  1.28   tsutsui  */
    439   1.2     ragge 		memset(sc->sc_ib, 0, sizeof(struct qt_cdata));
    440   1.2     ragge 		iniblk = &sc->sc_ib->qc_init;
    441   1.2     ragge 
    442   1.2     ragge 		iniblk->vector = sc->vector;
    443   1.2     ragge 		memcpy(iniblk->paddr, sc->is_addr, 6);
    444   1.2     ragge 
    445   1.2     ragge 		iniblk->options = INIT_OPTIONS_INT;
    446   1.2     ragge 		iniblk->rx_lo = loint(&sc->sc_pib->qc_r);
    447   1.2     ragge 		iniblk->rx_hi = hiint(&sc->sc_pib->qc_r);
    448   1.2     ragge 		iniblk->tx_lo = loint(&sc->sc_pib->qc_t);
    449   1.2     ragge 		iniblk->tx_hi = hiint(&sc->sc_pib->qc_t);
    450   1.2     ragge 	}
    451   1.4     ragge 	iniblk = &sc->sc_ib->qc_init;
    452   1.4     ragge 	iniblk->mode = ifp->if_flags & IFF_PROMISC ? INIT_MODE_PRO : 0;
    453  1.26   rhialto /*
    454  1.26   rhialto  * The multicast filter works "like LANCE".
    455  1.26   rhialto  */
    456  1.26   rhialto 	lance_setladrf(&sc->is_ec, iniblk->laddr);
    457   1.2     ragge 
    458   1.1     ragge /*
    459   1.1     ragge  * Now initialize the receive ring descriptors.  Because this routine can be
    460   1.1     ragge  * called with outstanding I/O operations we check the ring descriptors for
    461   1.1     ragge  * a non-zero 'rhost0' (or 'thost0') word and place those buffers back on
    462   1.1     ragge  * the free list.
    463  1.28   tsutsui  */
    464   1.2     ragge 	for (i = 0; i < NRCV; i++) {
    465   1.2     ragge 		rp = &sc->sc_ib->qc_r[i];
    466   1.2     ragge 		ifrw = &sc->sc_ifr[i];
    467   1.2     ragge 		rp->rmd1 = MCLBYTES;
    468   1.2     ragge 		rp->rmd4 = loint(ifrw->ifrw_info);
    469   1.2     ragge 		rp->rmd5 = hiint(ifrw->ifrw_info);
    470   1.2     ragge 		rp->rmd3 = 0;			/* clear RMD3_OWN */
    471  1.28   tsutsui 	}
    472   1.2     ragge 	for (i = 0; i < NXMT; i++) {
    473   1.2     ragge 		tp = &sc->sc_ib->qc_t[i];
    474   1.2     ragge 		ifxp = &sc->sc_ifw[i];
    475   1.2     ragge 		tp->tmd4 = loint(ifxp->ifw_info);
    476   1.2     ragge 		tp->tmd5 = hiint(ifxp->ifw_info);
    477   1.2     ragge 		tp->tmd3 = TMD3_OWN;
    478  1.28   tsutsui 	}
    479   1.2     ragge 
    480   1.2     ragge 	sc->xnext = sc->xlast = sc->nxmit = 0;
    481   1.1     ragge 	sc->rindex = 0;
    482   1.1     ragge 	sc->nxtrcv = 0;
    483   1.1     ragge 	sc->nrcv = 0;
    484   1.6    simonb 
    485   1.1     ragge /*
    486   1.1     ragge  * Now we tell the device the address of the INIT block.  The device
    487   1.1     ragge  * _must_ be in the Turbo mode at this time.  The "START" command is
    488   1.1     ragge  * then issued to the device.  A 1 second timeout is then started.
    489   1.1     ragge  * When the interrupt occurs the IFF_UP|IFF_RUNNING state is entered and
    490   1.6    simonb  * full operations will proceed.  If the timeout expires without an interrupt
    491   1.1     ragge  * being received an error is printed, the flags cleared and the device left
    492   1.1     ragge  * marked down.
    493  1.28   tsutsui  */
    494   1.3     ragge 	QT_WCSR(CSR_IBAL, loint(&sc->sc_pib->qc_init));
    495   1.3     ragge 	QT_WCSR(CSR_IBAH, hiint(&sc->sc_pib->qc_init));
    496   1.3     ragge 	QT_WCSR(CSR_SRQR, 2);
    497   1.2     ragge 
    498   1.2     ragge 	sc->is_if.if_flags |= IFF_RUNNING;
    499   1.2     ragge 	return 0;
    500  1.28   tsutsui }
    501   1.1     ragge 
    502   1.1     ragge /*
    503   1.1     ragge  * Start output on interface.
    504   1.1     ragge  */
    505   1.1     ragge 
    506   1.2     ragge void
    507   1.2     ragge qtstart(struct ifnet *ifp)
    508  1.28   tsutsui {
    509  1.28   tsutsui 	int len, nxmit;
    510  1.14      matt 	struct qt_softc *sc = ifp->if_softc;
    511  1.14      matt 	struct qt_tring *rp;
    512  1.28   tsutsui 	struct mbuf *m = NULL;
    513   1.6    simonb 
    514   1.2     ragge 	for (nxmit = sc->nxmit; nxmit < NXMT; nxmit++) {
    515   1.1     ragge 		IF_DEQUEUE(&sc->is_if.if_snd, m);
    516  1.28   tsutsui 		if (m == NULL)
    517   1.1     ragge 			break;
    518   1.2     ragge 
    519   1.2     ragge 		rp = &sc->sc_ib->qc_t[sc->xnext];
    520   1.2     ragge 		if ((rp->tmd3 & TMD3_OWN) == 0)
    521   1.2     ragge 			panic("qtstart");
    522   1.2     ragge 
    523  1.23   msaitoh 		bpf_mtap(ifp, m, BPF_D_OUT);
    524   1.3     ragge 
    525   1.2     ragge 		len = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[sc->xnext], m);
    526   1.2     ragge 		if (len < MINPACKETSIZE)
    527   1.1     ragge 			len = MINPACKETSIZE;
    528   1.1     ragge 		rp->tmd3 = len & TMD3_BCT;	/* set length,clear ownership */
    529   1.3     ragge 		QT_WCSR(CSR_ARQR, ARQR_TRQ);	/* tell device it has buffer */
    530   1.3     ragge 
    531  1.28   tsutsui 		if (++sc->xnext >= NXMT)
    532   1.2     ragge 			sc->xnext = 0;
    533   1.1     ragge 	}
    534   1.2     ragge 	if (sc->nxmit != nxmit)
    535   1.2     ragge 		sc->nxmit = nxmit;
    536   1.2     ragge 	/* XXX - set OACTIVE */
    537   1.2     ragge }
    538   1.6    simonb 
    539   1.1     ragge /*
    540   1.6    simonb  * General interrupt service routine.  Receive, transmit, device start
    541   1.1     ragge  * interrupts and timeouts come here.  Check for hard device errors and print a
    542   1.6    simonb  * message if any errors are found.  If we are waiting for the device to
    543   1.1     ragge  * START then check if the device is now running.
    544  1.28   tsutsui  */
    545   1.1     ragge 
    546   1.2     ragge void
    547   1.2     ragge qtintr(void *arg)
    548  1.28   tsutsui {
    549   1.2     ragge 	struct qt_softc *sc = arg;
    550   1.4     ragge 	struct ifnet *ifp = &sc->is_if;
    551   1.2     ragge 	short status;
    552   1.2     ragge 
    553   1.3     ragge 	status = QT_RCSR(CSR_SRR);
    554  1.28   tsutsui 	if (status < 0) {
    555   1.1     ragge 		/* should we reset the device after a bunch of these errs? */
    556   1.2     ragge 		qtsrr(sc, status);
    557  1.28   tsutsui 	}
    558   1.4     ragge 	if ((ifp->if_flags & IFF_UP) == 0)
    559   1.4     ragge 		return; /* Unwanted interrupt */
    560   1.2     ragge 	qtrint(sc);
    561   1.2     ragge 	qttint(sc);
    562   1.2     ragge 	qtstart(&sc->is_ec.ec_if);
    563  1.28   tsutsui }
    564   1.6    simonb 
    565   1.1     ragge /*
    566   1.1     ragge  * Transmit interrupt service.  Only called if there are outstanding transmit
    567   1.1     ragge  * requests which could have completed.  The DELQA-YM doesn't provide the
    568   1.1     ragge  * status bits telling the kind (receive, transmit) of interrupt.
    569  1.28   tsutsui  */
    570   1.6    simonb 
    571   1.1     ragge #define BBLMIS (TMD2_BBL|TMD2_MIS)
    572   1.1     ragge 
    573   1.2     ragge void
    574   1.2     ragge qttint(struct qt_softc *sc)
    575  1.25   thorpej {
    576  1.14      matt 	struct qt_tring *rp;
    577   1.6    simonb 
    578  1.25   thorpej 	while (sc->nxmit > 0) {
    579   1.2     ragge 		rp = &sc->sc_ib->qc_t[sc->xlast];
    580   1.2     ragge 		if ((rp->tmd3 & TMD3_OWN) == 0)
    581   1.1     ragge 			break;
    582  1.25   thorpej 		if_statinc(&sc->is_if, if_opackets);
    583   1.1     ragge /*
    584   1.1     ragge  * Collisions don't count as output errors, but babbling and missing packets
    585   1.1     ragge  * do count as output errors.
    586  1.28   tsutsui  */
    587  1.25   thorpej 		if (rp->tmd2 & TMD2_CER)
    588  1.25   thorpej 			if_statinc(&sc->is_if, if_collisions);
    589  1.25   thorpej 		if ((rp->tmd0 & TMD0_ERR1) ||
    590  1.25   thorpej 		    ((rp->tmd2 & TMD2_ERR2) && (rp->tmd2 & BBLMIS))) {
    591   1.1     ragge #ifdef QTDEBUG
    592   1.2     ragge 			char buf[100];
    593  1.15  christos 			snprintb(buf, sizeof(buf), TMD2_BITS, rp->tmd2);
    594  1.14      matt 			printf("%s: tmd2 %s\n", device_xname(sc->sc_dev), buf);
    595   1.1     ragge #endif
    596  1.25   thorpej 			if_statinc(&sc->is_if, if_oerrors);
    597  1.25   thorpej 		}
    598   1.2     ragge 		if_ubaend(&sc->sc_ifuba, &sc->sc_ifw[sc->xlast]);
    599  1.25   thorpej 		if (++sc->xlast >= NXMT)
    600   1.2     ragge 			sc->xlast = 0;
    601   1.2     ragge 		sc->nxmit--;
    602   1.1     ragge 	}
    603  1.25   thorpej }
    604   1.6    simonb 
    605   1.1     ragge /*
    606   1.1     ragge  * Receive interrupt service.  Pull packet off the interface and put into
    607   1.1     ragge  * a mbuf chain for processing later.
    608  1.28   tsutsui  */
    609   1.1     ragge 
    610   1.2     ragge void
    611   1.2     ragge qtrint(struct qt_softc *sc)
    612  1.14      matt {
    613  1.14      matt 	struct qt_rring *rp;
    614   1.2     ragge 	struct ifnet *ifp = &sc->is_ec.ec_if;
    615   1.2     ragge 	struct mbuf *m;
    616  1.28   tsutsui 	int len;
    617   1.6    simonb 
    618  1.28   tsutsui 	while (sc->sc_ib->qc_r[(int)sc->rindex].rmd3 & RMD3_OWN) {
    619   1.2     ragge 		rp = &sc->sc_ib->qc_r[(int)sc->rindex];
    620  1.25   thorpej 		if ((rp->rmd0 & (RMD0_STP|RMD0_ENP)) != (RMD0_STP|RMD0_ENP)) {
    621  1.28   tsutsui 			printf("%s: chained packet\n",
    622  1.28   tsutsui 			    device_xname(sc->sc_dev));
    623  1.25   thorpej 			if_statinc(&sc->is_if, if_ierrors);
    624   1.1     ragge 			goto rnext;
    625  1.25   thorpej 		}
    626  1.29   tsutsui 		len = (rp->rmd1 & RMD1_MCNT) - ETHER_CRC_LEN;
    627   1.6    simonb 
    628  1.25   thorpej 		if ((rp->rmd0 & RMD0_ERR3) || (rp->rmd2 & RMD2_ERR4)) {
    629   1.1     ragge #ifdef QTDEBUG
    630   1.2     ragge 			char buf[100];
    631  1.15  christos 			snprintb(buf, sizeof(buf), RMD0_BITS, rp->rmd0);
    632  1.14      matt 			printf("%s: rmd0 %s\n", device_xname(sc->sc_dev), buf);
    633  1.15  christos 			snprintb(buf, sizeof(buf), RMD2_BITS, rp->rmd2);
    634  1.14      matt 			printf("%s: rmd2 %s\n", device_xname(sc->sc_dev), buf);
    635   1.1     ragge #endif
    636  1.25   thorpej 			if_statinc(&sc->is_if, if_ierrors);
    637   1.2     ragge 			goto rnext;
    638  1.25   thorpej 		}
    639   1.2     ragge 		m = if_ubaget(&sc->sc_ifuba, &sc->sc_ifr[(int)sc->rindex],
    640   1.2     ragge 		    ifp, len);
    641  1.28   tsutsui 		if (m == NULL) {
    642  1.25   thorpej 			if_statinc(&sc->is_if, if_ierrors);
    643   1.2     ragge 			goto rnext;
    644   1.2     ragge 		}
    645  1.19     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
    646   1.1     ragge rnext:
    647   1.1     ragge 		--sc->nrcv;
    648   1.2     ragge 		rp->rmd3 = 0;
    649   1.2     ragge 		rp->rmd1 = MCLBYTES;
    650  1.28   tsutsui 		if (++sc->rindex >= NRCV)
    651   1.1     ragge 			sc->rindex = 0;
    652  1.28   tsutsui 	}
    653   1.3     ragge 	QT_WCSR(CSR_ARQR, ARQR_RRQ);	/* tell device it has buffer */
    654  1.28   tsutsui }
    655   1.1     ragge 
    656   1.2     ragge int
    657  1.14      matt qtioctl(struct ifnet *ifp, u_long cmd, void *data)
    658  1.14      matt {
    659   1.5   thorpej 	int s, error;
    660   1.5   thorpej 
    661   1.5   thorpej 	s = splnet();
    662   1.2     ragge 
    663   1.2     ragge 	error = ether_ioctl(ifp, cmd, data);
    664   1.5   thorpej 	if (error == ENETRESET) {
    665   1.5   thorpej 		if (ifp->if_flags & IFF_RUNNING)
    666   1.5   thorpej 			error = qtinit(ifp);
    667   1.5   thorpej 		else
    668   1.5   thorpej 			error = 0;
    669   1.4     ragge 	}
    670   1.1     ragge 	splx(s);
    671  1.28   tsutsui 	return error;
    672   1.2     ragge }
    673   1.1     ragge 
    674   1.2     ragge void
    675  1.14      matt qtsrr(struct qt_softc *sc, int srrbits)
    676  1.14      matt {
    677   1.2     ragge 	char buf[100];
    678  1.28   tsutsui 
    679  1.15  christos 	snprintb(buf, sizeof(buf), SRR_BITS, srrbits);
    680  1.14      matt 	printf("%s: srr=%s\n", device_xname(sc->sc_dev), buf);
    681  1.14      matt }
    682   1.4     ragge 
    683   1.4     ragge /*
    684   1.4     ragge  * Stop activity on the interface.
    685   1.4     ragge  * Lose outstanding transmit requests. XXX - not good for multicast.
    686   1.4     ragge  */
    687   1.4     ragge void
    688   1.4     ragge qtstop(struct ifnet *ifp, int disable)
    689   1.4     ragge {
    690   1.4     ragge 	struct qt_softc *sc = ifp->if_softc;
    691   1.4     ragge 	int i;
    692   1.4     ragge 
    693   1.4     ragge 	QT_WCSR(CSR_SRQR, 3);
    694  1.28   tsutsui 	for (i = 0; i < 100; i++) {
    695   1.4     ragge 		if ((QT_RCSR(CSR_SRR) & SRR_RESP) == 3)
    696   1.4     ragge 			break;
    697  1.28   tsutsui 	}
    698   1.4     ragge 	if (QT_RCSR(CSR_SRR) & SRR_FES)
    699   1.4     ragge 		qtsrr(sc, QT_RCSR(CSR_SRR));
    700   1.4     ragge 	/* Forget already queued transmit requests */
    701   1.4     ragge 	while (sc->nxmit > 0) {
    702   1.4     ragge 		if_ubaend(&sc->sc_ifuba, &sc->sc_ifw[sc->xlast]);
    703   1.4     ragge 		if (++sc->xlast >= NXMT)
    704   1.4     ragge 			sc->xlast = 0;
    705   1.4     ragge 		sc->nxmit--;
    706   1.4     ragge 	}
    707   1.4     ragge 	/* Handle late received packets */
    708   1.4     ragge 	qtrint(sc);
    709   1.4     ragge 	ifp->if_flags &= ~IFF_RUNNING;
    710   1.4     ragge }
    711   1.1     ragge 
    712   1.2     ragge #ifdef notyet
    713   1.1     ragge /*
    714   1.1     ragge  * Reset the device.  This moves it from DELQA-T mode to DELQA-Normal mode.
    715   1.1     ragge  * After the reset put the device back in -T mode.  Then call qtinit() to
    716   1.1     ragge  * reinitialize the ring structures and issue the 'timeout' for the "device
    717   1.1     ragge  * started interrupt".
    718  1.28   tsutsui  */
    719   1.1     ragge 
    720   1.2     ragge void
    721  1.28   tsutsui qtreset(struct qt_softc *sc)
    722  1.28   tsutsui {
    723   1.6    simonb 
    724   1.1     ragge 	qtturbo(sc);
    725   1.2     ragge 	qtinit(&sc->is_ec.ec_if);
    726  1.28   tsutsui }
    727   1.1     ragge #endif
    728