Home | History | Annotate | Line # | Download | only in scsipi
if_dse.c revision 1.5
      1 /*	$NetBSD: if_dse.c,v 1.5 2024/01/01 22:29:48 gutteridge Exp $ */
      2 
      3 /*
      4  * Driver for DaynaPORT SCSI/Link SCSI-Ethernet
      5  *
      6  * Written by Hiroshi Noguchi <ngc (at) ff.iij4u.or.jp>
      7  *
      8  * Modified by Matt Sandstrom <mattias (at) beauty.se> for NetBSD 1.5.3
      9  *
     10  * This driver is written based on "if_se.c".
     11  */
     12 
     13 /*
     14  * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
     15  * All rights reserved.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  * 3. All advertising materials mentioning features or use of this software
     26  *    must display the following acknowledgement:
     27  *	This product includes software developed by Ian W. Dall.
     28  * 4. The name of the author may not be used to endorse or promote products
     29  *    derived from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     32  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     33  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     34  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     40  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 #include "opt_inet.h"
     44 #include "opt_atalk.h"
     45 
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/callout.h>
     50 #include <sys/syslog.h>
     51 #include <sys/kernel.h>
     52 #include <sys/file.h>
     53 #include <sys/stat.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/buf.h>
     56 #include <sys/uio.h>
     57 #include <sys/malloc.h>
     58 #include <sys/errno.h>
     59 #include <sys/device.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/disk.h>
     62 #include <sys/proc.h>
     63 #include <sys/conf.h>
     64 
     65 #include <sys/workqueue.h>
     66 
     67 #include <dev/scsipi/scsipi_all.h>
     68 #include <dev/scsipi/scsiconf.h>
     69 
     70 #include <sys/mbuf.h>
     71 
     72 #include <sys/socket.h>
     73 #include <net/if.h>
     74 #include <net/if_dl.h>
     75 #include <net/if_ether.h>
     76 #include <net/if_media.h>
     77 
     78 #ifdef INET
     79 #include <netinet/in.h>
     80 #include <netinet/if_inarp.h>
     81 #endif
     82 
     83 #ifdef NETATALK
     84 #include <netatalk/at.h>
     85 #endif
     86 
     87 #include <net/bpf.h>
     88 
     89 
     90 /*
     91  * debug flag
     92  */
     93 #if 0
     94 #define	DSE_DEBUG
     95 #endif
     96 
     97 
     98 #define DSE_TIMEOUT	100000
     99 #define	DSE_OUTSTANDING	4
    100 #define	DSE_RETRIES	4
    101 #define DSE_MINSIZE	60
    102 
    103 #define	DSE_HEADER_TX	4
    104 #define	DSE_TAIL_TX	4
    105 #define DSE_EXTRAS_TX	(DSE_HEADER_TX + DSE_TAIL_TX)
    106 
    107 #define	DSE_HEADER_RX	6
    108 #define	DSE_TAIL_RX	0
    109 #define	DSE_EXTRAS_RX	(DSE_HEADER_RX + DSE_TAIL_RX)
    110 
    111 #define	MAX_BYTES_RX	(ETHERMTU + sizeof(struct ether_header) + ETHER_CRC_LEN)
    112 
    113 /* 10 full length packets appears to be the max ever returned. 16k is OK */
    114 #define RBUF_LEN	(16 * 1024)
    115 
    116 /*
    117  * Tuning parameters:
    118  *   We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
    119  *   per read
    120  */
    121 #define RDATA_MAX	10	/* maximum of returned packets (guessed) */
    122 #define RDATA_GOAL 	8
    123 
    124 /*
    125  * maximum of available multicast address entries (guessed)
    126  */
    127 #define	DSE_MCAST_MAX	10
    128 
    129 
    130 /* dse_poll and dse_poll0 are the normal polling rate and the minimum
    131  * polling rate respectively. dse_poll0 should be chosen so that at
    132  * maximum ethernet speed, we will read nearly RDATA_MAX packets. dse_poll
    133  * should be chosen for reasonable maximum latency.
    134  * In practice, if we are being saturated with min length packets, we
    135  * can't poll fast enough. Polling with zero delay actually
    136  * worsens performance. dse_poll0 is enforced to be always at least 1
    137  */
    138 #if MAC68K_DEBUG
    139 #define DSE_POLL		50	/* default in milliseconds */
    140 #define DSE_POLL0 		30	/* default in milliseconds */
    141 #else
    142 #define DSE_POLL		80	/* default in milliseconds */
    143 #define DSE_POLL0 		40	/* default in milliseconds */
    144 #endif
    145 int dse_poll = 0;		/* Delay in ticks set at attach time */
    146 int dse_poll0 = 0;
    147 int dse_max_received = 0;	/* Instrumentation */
    148 
    149 
    150 
    151 
    152 /*==========================================
    153   data type defs
    154 ==========================================*/
    155 typedef struct scsipi_inquiry_data dayna_ether_inquiry_data;
    156 
    157 typedef struct {
    158 	uint8_t	opcode[2];
    159 	uint8_t	byte3;
    160 	uint8_t	length[2];
    161 	uint8_t	byte6;
    162 } scsi_dayna_ether_generic;
    163 
    164 #define	DAYNA_CMD_SEND		0x0A		/* same as generic "Write" */
    165 #define	DAYNA_CMD_RECV		0x08		/* same as generic "Read" */
    166 
    167 #define	DAYNA_CMD_GET_ADDR	0x09		/* ???: read MAC address ? */
    168 #define	REQ_LEN_GET_ADDR	0x12
    169 
    170 #define	DAYNA_CMD_SET_MULTI	0x0D		/* set multicast address */
    171 
    172 #define	DAYNA_CMD_VENDOR1	0x0E		/* ???: initialize signal ? */
    173 
    174 #define IS_SEND(generic)	((generic)->opcode == DAYNA_CMD_SEND)
    175 #define IS_RECV(generic)	((generic)->opcode == DAYNA_CMD_RECV)
    176 
    177 struct dse_softc {
    178 	device_t sc_dev;
    179 	struct	ethercom sc_ethercom;	/* Ethernet common part */
    180 	struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
    181 
    182 	struct callout sc_recv_ch;
    183 	struct kmutex sc_iflock;
    184 	struct if_percpuq *sc_ipq;
    185 	struct workqueue *sc_recv_wq, *sc_send_wq;
    186 	struct work sc_recv_work, sc_send_work;
    187 	int sc_recv_work_pending, sc_send_work_pending;
    188 
    189 	char *sc_tbuf;
    190 	char *sc_rbuf;
    191 	int sc_debug;
    192 	int sc_flags;
    193 	int sc_last_timeout;
    194 	int sc_enabled;
    195 	int sc_attach_state;
    196 };
    197 
    198 /* bit defs of "sc_flags" */
    199 #define DSE_NEED_RECV	0x1
    200 
    201 static int	dsematch(device_t, cfdata_t, void *);
    202 static void	dseattach(device_t, device_t, void *);
    203 static int	dsedetach(device_t, int);
    204 
    205 static void	dse_ifstart(struct ifnet *);
    206 static void	dse_send_worker(struct work *wk, void *cookie);
    207 
    208 static void	dsedone(struct scsipi_xfer *, int);
    209 static int	dse_ioctl(struct ifnet *, u_long, void *);
    210 static void	dsewatchdog(struct ifnet *);
    211 
    212 static void	dse_recv_callout(void *);
    213 static void	dse_recv_worker(struct work *wk, void *cookie);
    214 static void	dse_recv(struct dse_softc *);
    215 static struct mbuf*	dse_get(struct dse_softc *, uint8_t *, int);
    216 static int	dse_read(struct dse_softc *, uint8_t *, int);
    217 
    218 static int	dse_init_adaptor(struct dse_softc *);
    219 static int	dse_get_addr(struct dse_softc *, uint8_t *);
    220 static int	dse_set_multi(struct dse_softc *);
    221 
    222 static int	dse_reset(struct dse_softc *);
    223 
    224 #if 0	/* 07/16/2000 comment-out */
    225 static int	dse_set_mode(struct dse_softc *, int, int);
    226 #endif
    227 static int	dse_init(struct dse_softc *);
    228 static void	dse_stop(struct dse_softc *);
    229 
    230 #if 0
    231 static __inline uint16_t	ether_cmp(void *, void *);
    232 #endif
    233 
    234 static inline int dse_scsipi_cmd(struct scsipi_periph *periph,
    235 			struct scsipi_generic *scsipi_cmd,
    236 			int cmdlen, u_char *data_addr, int datalen,
    237 			int retries, int timeout, struct buf *bp,
    238 			int flags);
    239 
    240 int	dse_enable(struct dse_softc *);
    241 void	dse_disable(struct dse_softc *);
    242 
    243 
    244 CFATTACH_DECL_NEW(dse, sizeof(struct dse_softc),
    245     dsematch, dseattach, dsedetach, NULL);
    246 
    247 extern struct cfdriver dse_cd;
    248 
    249 dev_type_open(dseopen);
    250 dev_type_close(dseclose);
    251 dev_type_ioctl(dseioctl);
    252 
    253 const struct cdevsw dse_cdevsw = {
    254 	.d_open = dseopen,
    255 	.d_close = dseclose,
    256 	.d_read = noread,
    257 	.d_write = nowrite,
    258 	.d_ioctl = dseioctl,
    259 	.d_stop = nostop,
    260 	.d_tty = notty,
    261 	.d_poll = nopoll,
    262 	.d_mmap = nommap,
    263 	.d_kqfilter = nokqfilter,
    264 	.d_discard = nodiscard,
    265 	.d_flag = D_OTHER | D_MPSAFE
    266 };
    267 
    268 const struct scsipi_periphsw dse_switch = {
    269 
    270 	NULL,			/* Use default error handler */
    271 	NULL,		/* have no queue */
    272 	NULL,			/* have no async handler */
    273 	dsedone,		/* deal with stats at interrupt time */
    274 };
    275 
    276 struct scsipi_inquiry_pattern dse_patterns[] = {
    277 	{	T_PROCESSOR,	T_FIXED,
    278 		"Dayna",		"SCSI/Link",		"" },
    279 };
    280 
    281 
    282 
    283 /*====================================================
    284   definitions for SCSI commands
    285 ====================================================*/
    286 
    287 /*
    288  * command templates
    289  */
    290 /* unknown commands */
    291 /* Vendor #1 */
    292 static const scsi_dayna_ether_generic	sonic_ether_vendor1 = {
    293 	{ DAYNA_CMD_VENDOR1, 0x00 },
    294 	0x00,
    295 	{ 0x00, 0x00 },
    296 	0x80
    297 };
    298 
    299 
    300 
    301 #if 0
    302 /*
    303  * Compare two Ether/802 addresses for equality, inlined and
    304  * unrolled for speed.
    305  * Note: use this like memcmp()
    306  */
    307 static __inline uint16_t
    308 ether_cmp(void *one, void *two)
    309 {
    310 	uint16_t*	a;
    311 	uint16_t*	b;
    312 	uint16_t diff;
    313 
    314 	a = (uint16_t *) one;
    315 	b = (uint16_t *) two;
    316 
    317 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    318 
    319 	return (diff);
    320 }
    321 
    322 #define ETHER_CMP	ether_cmp
    323 #endif
    324 
    325 /*
    326  * check to match with SCSI inquiry information
    327  */
    328 static int
    329 dsematch(device_t parent, cfdata_t match, void *aux)
    330 {
    331 	struct scsipibus_attach_args *sa = aux;
    332 	int priority;
    333 
    334 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    335 	    dse_patterns, sizeof(dse_patterns) / sizeof(dse_patterns[0]),
    336 	    sizeof(dse_patterns[0]), &priority);
    337 	return priority;
    338 }
    339 
    340 
    341 /*
    342  * The routine called by the low level scsi routine when it discovers
    343  * a device suitable for this driver.
    344  */
    345 static void
    346 dseattach(device_t parent, device_t self, void *aux)
    347 {
    348 	struct dse_softc *sc = device_private(self);
    349 	struct scsipibus_attach_args *sa = aux;
    350 	struct scsipi_periph *periph = sa->sa_periph;
    351 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    352 	uint8_t myaddr[ETHER_ADDR_LEN];
    353 	char wqname[MAXCOMLEN];
    354 	int rv;
    355 
    356 	sc->sc_dev = self;
    357 
    358 	aprint_normal("\n");
    359 	SC_DEBUG(periph, SCSIPI_DB2, ("dseattach: "));
    360 
    361 	sc->sc_attach_state = 0;
    362 	callout_init(&sc->sc_recv_ch, CALLOUT_MPSAFE);
    363 	callout_setfunc(&sc->sc_recv_ch, dse_recv_callout, (void *)sc);
    364 	mutex_init(&sc->sc_iflock, MUTEX_DEFAULT, IPL_SOFTNET);
    365 
    366 	/*
    367 	 * Store information needed to contact our base driver
    368 	 */
    369 	sc->sc_periph = periph;
    370 	periph->periph_dev = sc->sc_dev;
    371 	periph->periph_switch = &dse_switch;
    372 #if 0
    373 	sc_periph->sc_link_dbflags = SCSIPI_DB1;
    374 #endif
    375 
    376 	dse_poll = mstohz(DSE_POLL);
    377 	dse_poll = dse_poll? dse_poll: 1;
    378 	dse_poll0 = mstohz(DSE_POLL0);
    379 	dse_poll0 = dse_poll0? dse_poll0: 1;
    380 
    381 	/*
    382 	 * Initialize and attach send and receive buffers
    383 	 */
    384 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header) +
    385 	    DSE_EXTRAS_TX + 16, M_DEVBUF, M_WAITOK);
    386 
    387 	sc->sc_rbuf = malloc(RBUF_LEN + 16, M_DEVBUF, M_WAITOK);
    388 
    389 	/* initialize adaptor and obtain MAC address */
    390 	dse_init_adaptor(sc);
    391 	sc->sc_attach_state = 1;
    392 
    393 	/* Initialize ifnet structure. */
    394 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    395 	ifp->if_softc = sc;
    396 	ifp->if_start = dse_ifstart;
    397 	ifp->if_ioctl = dse_ioctl;
    398 	ifp->if_watchdog = dsewatchdog;
    399 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    400 	ifp->if_extflags = IFEF_MPSAFE;
    401 
    402 	dse_get_addr(sc, myaddr);
    403 
    404 	/* Attach the interface. */
    405 	if_initialize(ifp);
    406 
    407 	snprintf(wqname, sizeof(wqname), "%sRx", device_xname(sc->sc_dev));
    408 	rv = workqueue_create(&sc->sc_recv_wq, wqname, dse_recv_worker, sc,
    409 	    PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
    410 	if (rv != 0) {
    411 		aprint_error_dev(sc->sc_dev,
    412 		    "unable to create recv Rx workqueue\n");
    413 		dsedetach(sc->sc_dev, 0);
    414 		return; /* Error */
    415 	}
    416 	sc->sc_recv_work_pending = false;
    417 	sc->sc_attach_state = 2;
    418 
    419 	snprintf(wqname, sizeof(wqname), "%sTx", device_xname(sc->sc_dev));
    420 	rv = workqueue_create(&sc->sc_send_wq, wqname, dse_send_worker, ifp,
    421 	    PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
    422 	if (rv != 0) {
    423 		aprint_error_dev(sc->sc_dev,
    424 		    "unable to create send Tx workqueue\n");
    425 		dsedetach(sc->sc_dev, 0);
    426 		return; /* Error */
    427 	}
    428 	sc->sc_send_work_pending = false;
    429 	sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
    430 	ether_ifattach(ifp, myaddr);
    431 	if_register(ifp);
    432 	sc->sc_attach_state = 4;
    433 
    434 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    435 }
    436 
    437 static int
    438 dsedetach(device_t self, int flags)
    439 {
    440 	struct dse_softc *sc = device_private(self);
    441 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    442 
    443 	switch(sc->sc_attach_state) {
    444 	case 4:
    445 		dse_stop(sc);
    446 		mutex_enter(&sc->sc_iflock);
    447 		ifp->if_flags &= ~IFF_RUNNING;
    448 		dse_disable(sc);
    449 		ether_ifdetach(ifp);
    450 		if_detach(ifp);
    451 		mutex_exit(&sc->sc_iflock);
    452 		if_percpuq_destroy(sc->sc_ipq);
    453 		/*FALLTHROUGH*/
    454 	case 3:
    455 		workqueue_destroy(sc->sc_send_wq);
    456 		/*FALLTHROUGH*/
    457 	case 2:
    458 		workqueue_destroy(sc->sc_recv_wq);
    459 		/*FALLTHROUGH*/
    460 	case 1:
    461 		free(sc->sc_rbuf, M_DEVBUF);
    462 		free(sc->sc_tbuf, M_DEVBUF);
    463 		callout_destroy(&sc->sc_recv_ch);
    464 		mutex_destroy(&sc->sc_iflock);
    465 		break;
    466 	default:
    467 		aprint_error_dev(sc->sc_dev, "detach failed (state %d)\n",
    468 		    sc->sc_attach_state);
    469 		return 1;
    470 		break;
    471 	}
    472 
    473 	return 0;
    474 }
    475 
    476 
    477 /*
    478  * submit SCSI command
    479  */
    480 static __inline int
    481 dse_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
    482     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
    483     struct buf *bp, int flags)
    484 {
    485 	int error = 0;
    486 
    487 	error = scsipi_command(periph, cmd, cmdlen, data_addr,
    488    		 datalen, retries, timeout, bp, flags);
    489 
    490 	return error;
    491 }
    492 
    493 
    494 /*
    495  * Start routine for calling from network sub system
    496  */
    497 static void
    498 dse_ifstart(struct ifnet *ifp)
    499 {
    500 	struct dse_softc *sc = ifp->if_softc;
    501 
    502 	mutex_enter(&sc->sc_iflock);
    503 	if (!sc->sc_send_work_pending)  {
    504 		sc->sc_send_work_pending = true;
    505 		workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
    506 	}
    507 	mutex_exit(&sc->sc_iflock);
    508 	if (sc->sc_flags & DSE_NEED_RECV) {
    509 		sc->sc_flags &= ~DSE_NEED_RECV;
    510 	}
    511 }
    512 
    513 /*
    514  * Invoke the transmit workqueue and transmission on the interface.
    515  */
    516 static void
    517 dse_send_worker(struct work *wk, void *cookie)
    518 {
    519 	struct ifnet *ifp = cookie;
    520 	struct dse_softc *sc = ifp->if_softc;
    521 	scsi_dayna_ether_generic cmd_send;
    522 	struct mbuf *m, *m0;
    523 	int len, error;
    524 	u_char *cp;
    525 
    526 	mutex_enter(&sc->sc_iflock);
    527 	sc->sc_send_work_pending = false;
    528 	mutex_exit(&sc->sc_iflock);
    529 
    530 	KASSERT(if_is_mpsafe(ifp));
    531 
    532 	/* Don't transmit if interface is busy or not running */
    533 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    534 		return;
    535 
    536 	while (1) {
    537 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    538 		if (m0 == NULL)
    539 			break;
    540 		/* If BPF is listening on this interface, let it see the
    541 		 * packet before we commit it to the wire.
    542 		 */
    543 		bpf_mtap(ifp, m0, BPF_D_OUT);
    544 
    545 		/* We need to use m->m_pkthdr.len, so require the header */
    546 		if ((m0->m_flags & M_PKTHDR) == 0)
    547 			panic("ctscstart: no header mbuf");
    548 		len = m0->m_pkthdr.len;
    549 
    550 		/* Mark the interface busy. */
    551 		ifp->if_flags |= IFF_OACTIVE;
    552 
    553 		/* Chain; copy into linear buffer allocated at attach time. */
    554 		cp = sc->sc_tbuf;
    555 		for (m = m0; m != NULL; ) {
    556 			memcpy(cp, mtod(m, u_char *), m->m_len);
    557 			cp += m->m_len;
    558 			m = m0 = m_free(m);
    559 		}
    560 		if (len < DSE_MINSIZE) {
    561 #ifdef DSE_DEBUG
    562 			if (sc->sc_debug)
    563 				aprint_error_dev(sc->sc_dev,
    564 				    "packet size %d (%zu) < %d\n", len,
    565 				    cp - (u_char *)sc->sc_tbuf, DSE_MINSIZE);
    566 #endif
    567 			memset(cp, 0, DSE_MINSIZE - len);
    568 			len = DSE_MINSIZE;
    569 		}
    570 
    571 		/* Fill out SCSI command. */
    572 		memset(&cmd_send, 0, sizeof(cmd_send));
    573 		cmd_send.opcode[0] = DAYNA_CMD_SEND;
    574 		_lto2b(len, &(cmd_send.length[0]));
    575 		cmd_send.byte6 = 0x00;
    576 
    577 		/* Send command to device. */
    578 		error = dse_scsipi_cmd(sc->sc_periph,
    579 		    (void *)&cmd_send, sizeof(cmd_send),
    580 		    sc->sc_tbuf, len, DSE_RETRIES,
    581 		    DSE_TIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_POLL |
    582 		    XS_CTL_DATA_OUT);
    583 		if (error) {
    584 			aprint_error_dev(sc->sc_dev,
    585 			    "not queued, error %d\n", error);
    586 			if_statinc(ifp, if_oerrors);
    587 			ifp->if_flags &= ~IFF_OACTIVE;
    588 		} else
    589 			if_statinc(ifp, if_opackets);
    590 	}
    591 }
    592 
    593 
    594 /*
    595  * Called from the scsibus layer via our scsi device switch.
    596  */
    597 static void
    598 dsedone(struct scsipi_xfer *xs, int error)
    599 {
    600 	struct dse_softc *sc = device_private(xs->xs_periph->periph_dev);
    601 	struct scsipi_generic *cmd = xs->cmd;
    602 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    603 
    604 	if (IS_SEND(cmd)) {
    605 		ifp->if_flags &= ~IFF_OACTIVE;
    606 	} else if (IS_RECV(cmd)) {
    607 		/* RECV complete */
    608 		/* pass data up. reschedule a recv */
    609 		/* scsipi_free_xs will call start. Harmless. */
    610 
    611 		if (error) {
    612 			/* Reschedule after a delay */
    613 			callout_schedule(&sc->sc_recv_ch, dse_poll);
    614 		} else {
    615 			int n, ntimeo;
    616 			n = dse_read(sc, xs->data, xs->datalen - xs->resid);
    617 			if (n > dse_max_received)
    618 				dse_max_received = n;
    619 			if (n == 0)
    620 				ntimeo = dse_poll;
    621 			else if (n >= RDATA_MAX)
    622 				ntimeo = dse_poll0;
    623 			else {
    624 				ntimeo = sc->sc_last_timeout;
    625 				ntimeo = (ntimeo * RDATA_GOAL)/n;
    626 				ntimeo = (ntimeo < dse_poll0?
    627 					  dse_poll0: ntimeo);
    628 				ntimeo = (ntimeo > dse_poll?
    629 					  dse_poll: ntimeo);
    630 			}
    631 			sc->sc_last_timeout = ntimeo;
    632 			callout_schedule(&sc->sc_recv_ch, ntimeo);
    633 		}
    634 	}
    635 }
    636 
    637 
    638 /*
    639  * Setup a receive command by queuing the work.
    640  * Usually called from a callout, but also from se_init().
    641  */
    642 static void
    643 dse_recv_callout(void *v)
    644 {
    645 	/* do a recv command */
    646 	struct dse_softc *sc = (struct dse_softc *) v;
    647 
    648 	if (sc->sc_enabled == 0)
    649 		return;
    650 
    651 	mutex_enter(&sc->sc_iflock);
    652 	if (sc->sc_recv_work_pending == true) {
    653 		callout_schedule(&sc->sc_recv_ch, dse_poll);
    654 		mutex_exit(&sc->sc_iflock);
    655 		return;
    656 	}
    657 
    658 	sc->sc_recv_work_pending = true;
    659 	workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
    660 	mutex_exit(&sc->sc_iflock);
    661 }
    662 
    663 /*
    664  * Invoke the receive workqueue
    665  */
    666 static void
    667 dse_recv_worker(struct work *wk, void *cookie)
    668 {
    669 	struct dse_softc *sc = (struct dse_softc *) cookie;
    670 
    671 	dse_recv(sc);
    672 	mutex_enter(&sc->sc_iflock);
    673 	sc->sc_recv_work_pending = false;
    674 	mutex_exit(&sc->sc_iflock);
    675 
    676 }
    677 
    678 /*
    679  * Do the actual work of receiving data.
    680  */
    681 static void
    682 dse_recv(struct dse_softc *sc)
    683 {
    684 	scsi_dayna_ether_generic cmd_recv;
    685 	int error, len;
    686 
    687 	/* do a recv command */
    688 	/* fill out command buffer */
    689 	memset(&cmd_recv, 0, sizeof(cmd_recv));
    690 	cmd_recv.opcode[0] = DAYNA_CMD_RECV;
    691 	len = MAX_BYTES_RX + DSE_EXTRAS_RX;
    692 	_lto2b(len, &(cmd_recv.length[0]));
    693 	cmd_recv.byte6 = 0xC0;
    694 
    695 	error = dse_scsipi_cmd(sc->sc_periph,
    696 	    (void *)&cmd_recv, sizeof(cmd_recv),
    697 	    sc->sc_rbuf, RBUF_LEN, DSE_RETRIES, DSE_TIMEOUT, NULL,
    698 	    XS_CTL_NOSLEEP | XS_CTL_POLL | XS_CTL_DATA_IN);
    699 	if (error)
    700 		callout_schedule(&sc->sc_recv_ch, dse_poll);
    701 }
    702 
    703 
    704 /*
    705  * We copy the data into mbufs.  When full cluster sized units are present
    706  * we copy into clusters.
    707  */
    708 static struct mbuf *
    709 dse_get(struct dse_softc *sc, uint8_t *data, int totlen)
    710 {
    711 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    712 	struct mbuf *m, *m0, *newm;
    713 	int len;
    714 
    715 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    716 	if (m0 == NULL)
    717 		return NULL;
    718 
    719 	m_set_rcvif(m0, ifp);
    720 	m0->m_pkthdr.len = totlen;
    721 	len	= MHLEN;
    722 	m = m0;
    723 
    724 	while (totlen > 0) {
    725 		if (totlen >= MINCLSIZE) {
    726 			MCLGET(m, M_DONTWAIT);
    727 			if((m->m_flags & M_EXT) == 0)
    728 				goto bad;
    729 
    730 			len = MCLBYTES;
    731 		}
    732 
    733 		if (m == m0) {
    734 			char *newdata = (char *)
    735 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    736 			    sizeof(struct ether_header);
    737 			len -= newdata - m->m_data;
    738 			m->m_data = newdata;
    739 		}
    740 
    741 		m->m_len = len = uimin(totlen, len);
    742 		memcpy(mtod(m, void *), data, len);
    743 		data += len;
    744 
    745 		totlen -= len;
    746 		if (totlen > 0) {
    747 			MGET(newm, M_DONTWAIT, MT_DATA);
    748 			if (newm == NULL)
    749 				goto bad;
    750 
    751 			len = MLEN;
    752 			m = m->m_next = newm;
    753 		}
    754 	}
    755 
    756 	return m0;
    757 
    758 bad:
    759 	m_freem(m0);
    760 	return NULL ;
    761 }
    762 
    763 
    764 #ifdef MAC68K_DEBUG
    765 static int
    766 peek_packet(uint8_t*  buf)
    767 {
    768 	struct ether_header *eh;
    769 	uint16_t type;
    770 	int len;
    771 
    772 	eh = (struct ether_header*)buf;
    773 	type = _2btol((uint8_t*)&(eh->ether_type));
    774 
    775 	len = sizeof(struct ether_header);
    776 
    777 	if (type <= ETHERMTU) {
    778 		/* for 802.3 */
    779 		len += type;
    780 	} else{
    781 		/* for Ethernet II (DIX) */
    782 		switch (type) {
    783 		  case ETHERTYPE_ARP:
    784 			len += 28;
    785 			break;
    786 		  case ETHERTYPE_IP:
    787 			len += _2btol(buf + sizeof(struct ether_header) + 2);
    788 			break;
    789 		  default:
    790 			len = 0;
    791 			goto l_end;
    792 			break;
    793 		}
    794 	}
    795 	if (len < DSE_MINSIZE) {
    796 		len = DSE_MINSIZE;
    797 	}
    798 	len += ETHER_CRC_LEN;
    799 
    800   l_end:;
    801 	return len;
    802 }
    803 #endif
    804 
    805 
    806 /*
    807  * Pass packets to higher levels.
    808  */
    809 static int
    810 dse_read(struct dse_softc *sc, uint8_t *data, int datalen)
    811 {
    812 	struct mbuf *m;
    813 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    814 	int	len;
    815 	int	n;
    816 #ifdef MAC68K_DEBUG
    817 	int	peek_flag = 1;
    818 #endif
    819 
    820 	mutex_enter(&sc->sc_iflock);
    821 	n = 0;
    822 	while (datalen >= DSE_HEADER_RX) {
    823 		/*
    824 		 * fetch bytes of stream.
    825 		 * here length = (ether frame length) + (FCS's 4 bytes)
    826 		 */
    827 		/* fetch frame length */
    828 		len = _2btol(data);
    829 
    830 		/* skip header part */
    831 		data	+= DSE_HEADER_RX;
    832 		datalen -= DSE_HEADER_RX;
    833 
    834 #if 0	/* 03/10/2001  only for debug */
    835 		{
    836 			printf("DATALEN %d len %d\n", datalen, len);
    837 			int	j;
    838 			printf("\ndump[%d]: ",n);
    839 			for ( j = 0 ; j < datalen ; j++ ) {
    840 				printf("%02X ",data[j-DSE_HEADER_RX]);
    841 			}
    842 		}
    843 #endif
    844 #ifdef MAC68K_DEBUG
    845 		if (peek_flag) {
    846 			peek_flag = 0;
    847 			len = peek_packet(data);
    848 		}
    849 #endif
    850 		if (len == 0)
    851 			break;
    852 
    853 #ifdef DSE_DEBUG
    854 		aprint_error_dev(sc->sc_dev, "dse_read: datalen = %d, packetlen"
    855 		    " = %d, proto = 0x%04x\n", datalen, len,
    856 		    ntohs(((struct ether_header *)data)->ether_type));
    857 #endif
    858 		if ((len < (DSE_MINSIZE + ETHER_CRC_LEN)) ||
    859 		     (MAX_BYTES_RX < len)) {
    860 #ifdef DSE_DEBUG
    861 			aprint_error_dev(sc->sc_dev, "invalid packet size "
    862 			    "%d; dropping\n", len);
    863 #endif
    864 			if_statinc(ifp, if_ierrors);
    865 			break;
    866 		}
    867 
    868 		/* Don't need crc. Must keep ether header for BPF */
    869 		m = dse_get(sc, data, len - ETHER_CRC_LEN);
    870 		if (m == NULL) {
    871 #ifdef DSE_DEBUG
    872 			if (sc->sc_debug)
    873 				aprint_error_dev(sc->sc_dev, "dse_read: "
    874 				    "dse_get returned null\n");
    875 #endif
    876 			if_statinc(ifp, if_ierrors);
    877 			goto next_packet;
    878 		}
    879 		if_statinc(ifp, if_ipackets);
    880 
    881 		/*
    882 		 * Check if there's a BPF listener on this interface.
    883 		 * If so, hand off the raw packet to BPF.
    884 		 */
    885 		if (ifp->if_bpf)
    886 			bpf_mtap(ifp, m, BPF_D_OUT);
    887 
    888 		/* Pass the packet up. */
    889 		if_percpuq_enqueue(sc->sc_ipq, m);
    890 
    891   next_packet:
    892 		data	+= len;
    893 		datalen	-= len;
    894 		n++;
    895 	}
    896 	mutex_exit(&sc->sc_iflock);
    897 
    898 	return n;
    899 }
    900 
    901 
    902 static void
    903 dsewatchdog(struct ifnet *ifp)
    904 {
    905 	struct dse_softc *sc = ifp->if_softc;
    906 
    907 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    908 	if_statinc(ifp, if_oerrors);
    909 
    910 	dse_reset(sc);
    911 }
    912 
    913 
    914 static int
    915 dse_reset(struct dse_softc *sc)
    916 {
    917 	int error;
    918 #if 0
    919 	/* Maybe we don't *really* want to reset the entire bus
    920 	 * because the ctron isn't working. We would like to send a
    921 	 * "BUS DEVICE RESET" message, but don't think the ctron
    922 	 * understands it.
    923 	 */
    924 	error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES, 2000,
    925 	    NULL, XS_CTL_RESET);
    926 #endif
    927 	error = dse_init(sc);
    928 	return error;
    929 }
    930 
    931 
    932 static int
    933 dse_init_adaptor(struct dse_softc *sc)
    934 {
    935 	scsi_dayna_ether_generic cmd_vend1;
    936 	u_char tmpbuf[sizeof(cmd_vend1)];
    937 	int error;
    938 
    939 #if 0	/* 07/21/2001 for test */
    940 	/* Maybe we don't *really* want to reset the entire bus
    941 	 * because the ctron isn't working. We would like to send a
    942 	 * "BUS DEVICE RESET" message, but don't think the ctron
    943 	 * understands it.
    944 	 */
    945 	error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES,
    946 	    2000, NULL, XS_CTL_RESET);
    947 #endif
    948 
    949 	cmd_vend1 = sonic_ether_vendor1;
    950 
    951 	error = dse_scsipi_cmd(sc->sc_periph,
    952 	    (struct scsipi_generic *)&cmd_vend1, sizeof(cmd_vend1),
    953 		&(tmpbuf[0]), sizeof(tmpbuf),
    954 	    DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
    955 
    956 	if (error)
    957 		goto l_end;
    958 
    959 	/* wait 500 msec */
    960 	kpause("dsesleep", false, hz / 2, NULL);
    961 
    962 l_end:
    963 	return error;
    964 }
    965 
    966 
    967 static int
    968 dse_get_addr(struct dse_softc *sc, uint8_t *myaddr)
    969 {
    970 	scsi_dayna_ether_generic cmd_get_addr;
    971 	u_char tmpbuf[REQ_LEN_GET_ADDR];
    972 	int error;
    973 
    974 	memset(&cmd_get_addr, 0, sizeof(cmd_get_addr));
    975 	cmd_get_addr.opcode[0] = DAYNA_CMD_GET_ADDR;
    976 	_lto2b(REQ_LEN_GET_ADDR, cmd_get_addr.length);
    977 
    978 	error = dse_scsipi_cmd(sc->sc_periph,
    979 	    (struct scsipi_generic *)&cmd_get_addr, sizeof(cmd_get_addr),
    980 	    tmpbuf, sizeof(tmpbuf),
    981 	    DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
    982 
    983 	if (error == 0) {
    984 		memcpy(myaddr, &(tmpbuf[0]), ETHER_ADDR_LEN);
    985 
    986 		aprint_error_dev(sc->sc_dev, "ethernet address %s\n",
    987 			   ether_sprintf(myaddr));
    988 	}
    989 
    990 	return error;
    991 }
    992 
    993 
    994 #if 0	/* 07/16/2000 comment-out */
    995 static int
    996 dse_set_mode(struct dse_softc *sc, int len, int mode)
    997 
    998 	return 0;
    999 }
   1000 #endif
   1001 
   1002 
   1003 static int
   1004 dse_init(struct dse_softc *sc)
   1005 {
   1006 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1007 	int error = 0;
   1008 
   1009 	if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
   1010 		ifp->if_flags |= IFF_RUNNING;
   1011 		mutex_enter(&sc->sc_iflock);
   1012 		if (!sc->sc_recv_work_pending)  {
   1013 			sc->sc_recv_work_pending = true;
   1014 			workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work,
   1015 			    NULL);
   1016 		}
   1017 		mutex_exit(&sc->sc_iflock);
   1018 		ifp->if_flags &= ~IFF_OACTIVE;
   1019 		mutex_enter(&sc->sc_iflock);
   1020 		if (!sc->sc_send_work_pending)  {
   1021 			sc->sc_send_work_pending = true;
   1022 			workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work,
   1023 			    NULL);
   1024 		}
   1025 		mutex_exit(&sc->sc_iflock);
   1026 	}
   1027 	return error;
   1028 }
   1029 
   1030 
   1031 static uint8_t	BROADCAST_ADDR[ETHER_ADDR_LEN] =
   1032 			{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
   1033 
   1034 
   1035 static int
   1036 dse_set_multi(struct dse_softc *sc)
   1037 {
   1038 	scsi_dayna_ether_generic cmd_set_multi;
   1039 	struct ether_multistep step;
   1040 	struct ether_multi *enm;
   1041 	u_char *cp, *mybuf;
   1042 	int error, len;
   1043 
   1044 	error = 0;
   1045 
   1046 #ifdef DSE_DEBUG
   1047 	aprint_error_dev(sc->sc_dev, "dse_set_multi\n");
   1048 #endif
   1049 
   1050 	mybuf = malloc(ETHER_ADDR_LEN * DSE_MCAST_MAX, M_DEVBUF, M_NOWAIT);
   1051 	if (mybuf == NULL) {
   1052 		error = EIO;
   1053 		goto l_end;
   1054 	}
   1055 
   1056 	/*
   1057 	 * copy all entries to transfer buffer
   1058 	 */
   1059 	cp = mybuf;
   1060 	len = 0;
   1061 	ETHER_FIRST_MULTI(step, &(sc->sc_ethercom), enm);
   1062 	while ((len < (DSE_MCAST_MAX - 1)) && (enm != NULL)) {
   1063 		/* ### refer low side entry */
   1064 		memcpy(cp, enm->enm_addrlo, ETHER_ADDR_LEN);
   1065 
   1066 		cp += ETHER_ADDR_LEN;
   1067 		len++;
   1068 		ETHER_NEXT_MULTI(step, enm);
   1069 	}
   1070 
   1071 	/* add broadcast address as default */
   1072 	memcpy(cp, BROADCAST_ADDR, ETHER_ADDR_LEN);
   1073 	len++;
   1074 
   1075 	len *= ETHER_ADDR_LEN;
   1076 
   1077 	memset(&cmd_set_multi, 0, sizeof(cmd_set_multi));
   1078 	cmd_set_multi.opcode[0] = DAYNA_CMD_SET_MULTI;
   1079 	_lto2b(len, cmd_set_multi.length);
   1080 
   1081 	error = dse_scsipi_cmd(sc->sc_periph,
   1082 	    (struct scsipi_generic*)&cmd_set_multi, sizeof(cmd_set_multi),
   1083 	    mybuf, len, DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_OUT);
   1084 
   1085 	free(mybuf, M_DEVBUF);
   1086 
   1087 l_end:
   1088 	return error;
   1089 }
   1090 
   1091 
   1092 static void
   1093 dse_stop(struct dse_softc *sc)
   1094 {
   1095 	/* Don't schedule any reads */
   1096 	callout_stop(&sc->sc_recv_ch);
   1097 
   1098 	/* Wait for the workqueues to finish */
   1099 	mutex_enter(&sc->sc_iflock);
   1100 	workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
   1101 	workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
   1102 	mutex_exit(&sc->sc_iflock);
   1103 
   1104 	/* Abort any scsi cmds in progress */
   1105 	mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
   1106 	scsipi_kill_pending(sc->sc_periph);
   1107 	mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
   1108 }
   1109 
   1110 
   1111 /*
   1112  * Process an ioctl request.
   1113  */
   1114 static int
   1115 dse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1116 {
   1117 	struct dse_softc *sc;
   1118 	struct ifaddr *ifa;
   1119 	struct ifreq *ifr;
   1120 	struct sockaddr *sa;
   1121 	int error;
   1122 
   1123 	error = 0;
   1124 	sc = ifp->if_softc;
   1125 	ifa = (struct ifaddr *)data;
   1126 	ifr = (struct ifreq *)data;
   1127 
   1128 	switch (cmd) {
   1129 	case SIOCINITIFADDR:
   1130 		mutex_enter(&sc->sc_iflock);
   1131 		if ((error = dse_enable(sc)) != 0)
   1132 			break;
   1133 		ifp->if_flags |= IFF_UP;
   1134 		mutex_exit(&sc->sc_iflock);
   1135 
   1136 #if 0
   1137 		if ((error = dse_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
   1138 			break;
   1139 #endif
   1140 
   1141 		switch (ifa->ifa_addr->sa_family) {
   1142 #ifdef INET
   1143 		case AF_INET:
   1144 			if ((error = dse_init(sc)) != 0)
   1145 				break;
   1146 			arp_ifinit(ifp, ifa);
   1147 			break;
   1148 #endif
   1149 #ifdef NETATALK
   1150 		case AF_APPLETALK:
   1151 			if ((error = dse_init(sc)) != 0)
   1152 				break;
   1153 			break;
   1154 #endif
   1155 		default:
   1156 			error = dse_init(sc);
   1157 			break;
   1158 		}
   1159 		break;
   1160 
   1161 
   1162 	case SIOCSIFADDR:
   1163 		mutex_enter(&sc->sc_iflock);
   1164 		error = dse_enable(sc);
   1165 		mutex_exit(&sc->sc_iflock);
   1166 		if (error != 0)
   1167 			break;
   1168 		ifp->if_flags |= IFF_UP;
   1169 
   1170 		switch (ifa->ifa_addr->sa_family) {
   1171 #ifdef INET
   1172 		case AF_INET:
   1173 			if ((error = dse_init(sc)) != 0)
   1174 				break;
   1175 			arp_ifinit(ifp, ifa);
   1176 			break;
   1177 #endif
   1178 #ifdef NETATALK
   1179 		case AF_APPLETALK:
   1180 			if ((error = dse_init(sc)) != 0)
   1181 				break;
   1182 			break;
   1183 #endif
   1184 		default:
   1185 			error = dse_init(sc);
   1186 			break;
   1187 		}
   1188 		break;
   1189 
   1190 	case SIOCSIFFLAGS:
   1191 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1192 			break;
   1193 		/* XXX re-use ether_ioctl() */
   1194 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1195 		case IFF_RUNNING:
   1196 			/*
   1197 			 * If interface is marked down and it is running, then
   1198 			 * stop it.
   1199 			 */
   1200 			dse_stop(sc);
   1201 			mutex_enter(&sc->sc_iflock);
   1202 			ifp->if_flags &= ~IFF_RUNNING;
   1203 			dse_disable(sc);
   1204 			mutex_exit(&sc->sc_iflock);
   1205 			break;
   1206 		case IFF_UP:
   1207 			/*
   1208 			 * If interface is marked up and it is stopped, then
   1209 			 * start it.
   1210 			 */
   1211 			mutex_enter(&sc->sc_iflock);
   1212 			error = dse_enable(sc);
   1213 			mutex_exit(&sc->sc_iflock);
   1214 			if (error)
   1215 				break;
   1216 			error = dse_init(sc);
   1217 			break;
   1218 		default:
   1219 			/*
   1220 			 * Reset the interface to pick up changes in any other
   1221 			 * flags that affect hardware registers.
   1222 			 */
   1223 			mutex_enter(&sc->sc_iflock);
   1224 			if (sc->sc_enabled)
   1225 				error = dse_init(sc);
   1226 			mutex_exit(&sc->sc_iflock);
   1227 			break;
   1228 		}
   1229 #ifdef DSE_DEBUG
   1230 		if (ifp->if_flags & IFF_DEBUG)
   1231 			sc->sc_debug = 1;
   1232 		else
   1233 			sc->sc_debug = 0;
   1234 #endif
   1235 		break;
   1236 
   1237 	case SIOCADDMULTI:
   1238 		if (sc->sc_enabled == 0) {
   1239 			error = EIO;
   1240 			break;
   1241 		}
   1242 		mutex_enter(&sc->sc_iflock);
   1243 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
   1244 		mutex_exit(&sc->sc_iflock);
   1245 		if (ether_addmulti(sa, &sc->sc_ethercom) == ENETRESET) {
   1246 			error = dse_set_multi(sc);
   1247 #ifdef DSE_DEBUG
   1248 			aprint_error_dev(sc->sc_dev, "add multi: %s\n",
   1249 				   ether_sprintf(ifr->ifr_addr.sa_data));
   1250 #endif
   1251 		} else
   1252 			error = 0;
   1253 
   1254 		mutex_enter(&sc->sc_iflock);
   1255 		sockaddr_free(sa);
   1256 		mutex_exit(&sc->sc_iflock);
   1257 
   1258 		break;
   1259 
   1260 	case SIOCDELMULTI:
   1261 		if (sc->sc_enabled == 0) {
   1262 			error = EIO;
   1263 			break;
   1264 		}
   1265 		mutex_enter(&sc->sc_iflock);
   1266 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
   1267 		mutex_exit(&sc->sc_iflock);
   1268 		if (ether_delmulti(sa, &sc->sc_ethercom) == ENETRESET) {
   1269 			error = dse_set_multi(sc);
   1270 #ifdef DSE_DEBUG
   1271 			aprint_error_dev(sc->sc_dev, "delete multi: %s\n",
   1272 			    ether_sprintf(ifr->ifr_addr.sa_data));
   1273 #endif
   1274 		} else
   1275 			error = 0;
   1276 
   1277 		mutex_enter(&sc->sc_iflock);
   1278 		sockaddr_free(sa);
   1279 		mutex_exit(&sc->sc_iflock);
   1280 
   1281 		break;
   1282 
   1283 	default:
   1284 		error = ether_ioctl(ifp, cmd, data);
   1285 		break;
   1286 	}
   1287 
   1288 
   1289 	return error;
   1290 }
   1291 
   1292 
   1293 /*
   1294  * Enable the network interface.
   1295  */
   1296 int
   1297 dse_enable(struct dse_softc *sc)
   1298 {
   1299 	struct scsipi_periph *periph = sc->sc_periph;
   1300 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1301 	int error = 0;
   1302 
   1303 	if (sc->sc_enabled == 0) {
   1304 		if ((error = scsipi_adapter_addref(adapt)) == 0)
   1305 			sc->sc_enabled = 1;
   1306 		else
   1307 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1308 	}
   1309 
   1310 	return error;
   1311 }
   1312 
   1313 
   1314 /*
   1315  * Disable the network interface.
   1316  */
   1317 void
   1318 dse_disable(struct dse_softc *sc)
   1319 {
   1320 	struct scsipi_periph *periph = sc->sc_periph;
   1321 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1322 	if (sc->sc_enabled != 0) {
   1323 		scsipi_adapter_delref(adapt);
   1324 		sc->sc_enabled = 0;
   1325 	}
   1326 }
   1327 
   1328 
   1329 #define	DSEUNIT(z)	(minor(z))
   1330 
   1331 /*
   1332  * open the device.
   1333  */
   1334 int
   1335 dseopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1336 {
   1337 	int unit, error;
   1338 	struct dse_softc *sc;
   1339 	struct scsipi_periph *periph;
   1340 	struct scsipi_adapter *adapt;
   1341 
   1342 	unit = DSEUNIT(dev);
   1343 	sc = device_lookup_private(&dse_cd, unit);
   1344 	if (sc == NULL)
   1345 		return ENXIO;
   1346 
   1347 	periph = sc->sc_periph;
   1348 	adapt = periph->periph_channel->chan_adapter;
   1349 
   1350 	if ((error = scsipi_adapter_addref(adapt)) != 0)
   1351 		return error;
   1352 
   1353 	SC_DEBUG(periph, SCSIPI_DB1,
   1354 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
   1355 	    dse_cd.cd_ndevs));
   1356 
   1357 	periph->periph_flags |= PERIPH_OPEN;
   1358 
   1359 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
   1360 
   1361 	return 0;
   1362 }
   1363 
   1364 
   1365 /*
   1366  * close the device.. only called if we are the LAST
   1367  * occurence of an open device
   1368  */
   1369 int
   1370 dseclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1371 {
   1372 	struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
   1373 	struct scsipi_periph *periph = sc->sc_periph;
   1374 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1375 
   1376 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
   1377 
   1378 	scsipi_wait_drain(periph);
   1379 
   1380 	scsipi_adapter_delref(adapt);
   1381 	periph->periph_flags &= ~PERIPH_OPEN;
   1382 
   1383 	return 0;
   1384 }
   1385 
   1386 
   1387 /*
   1388  * Perform special action on behalf of the user
   1389  * Only does generic scsi ioctls.
   1390  */
   1391 int
   1392 dseioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1393 {
   1394 	struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
   1395 
   1396 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
   1397 }
   1398 
   1399