Home | History | Annotate | Line # | Download | only in scsipi
      1 /*	$NetBSD: if_dse.c,v 1.9 2025/05/23 09:12:51 nat 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_DATA_OUT);
    582 		if (error) {
    583 			aprint_error_dev(sc->sc_dev,
    584 			    "not queued, error %d\n", error);
    585 			if_statinc(ifp, if_oerrors);
    586 			ifp->if_flags &= ~IFF_OACTIVE;
    587 		} else
    588 			if_statinc(ifp, if_opackets);
    589 	}
    590 }
    591 
    592 
    593 /*
    594  * Called from the scsibus layer via our scsi device switch.
    595  */
    596 static void
    597 dsedone(struct scsipi_xfer *xs, int error)
    598 {
    599 	struct dse_softc *sc = device_private(xs->xs_periph->periph_dev);
    600 	struct scsipi_generic *cmd = xs->cmd;
    601 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    602 
    603 	if (IS_SEND(cmd)) {
    604 		ifp->if_flags &= ~IFF_OACTIVE;
    605 	} else if (IS_RECV(cmd)) {
    606 		/* RECV complete */
    607 		/* pass data up. reschedule a recv */
    608 		/* scsipi_free_xs will call start. Harmless. */
    609 
    610 		if (error) {
    611 			/* Reschedule after a delay */
    612 			callout_schedule(&sc->sc_recv_ch, dse_poll);
    613 		} else {
    614 			int n, ntimeo;
    615 			n = dse_read(sc, xs->data, xs->datalen - xs->resid);
    616 			if (n > dse_max_received)
    617 				dse_max_received = n;
    618 			if (n == 0)
    619 				ntimeo = dse_poll;
    620 			else if (n >= RDATA_MAX)
    621 				ntimeo = dse_poll0;
    622 			else if (n >= dse_max_received)
    623 				ntimeo = 0;
    624 			else {
    625 				ntimeo = sc->sc_last_timeout;
    626 				ntimeo = (ntimeo * RDATA_GOAL)/n;
    627 				ntimeo = (ntimeo < dse_poll0?
    628 					  dse_poll0: ntimeo);
    629 				ntimeo = (ntimeo > dse_poll?
    630 					  dse_poll: ntimeo);
    631 			}
    632 			sc->sc_last_timeout = ntimeo;
    633 			callout_schedule(&sc->sc_recv_ch, ntimeo);
    634 		}
    635 	}
    636 }
    637 
    638 
    639 /*
    640  * Setup a receive command by queuing the work.
    641  * Usually called from a callout, but also from se_init().
    642  */
    643 static void
    644 dse_recv_callout(void *v)
    645 {
    646 	/* do a recv command */
    647 	struct dse_softc *sc = (struct dse_softc *) v;
    648 
    649 	if (sc->sc_enabled == 0)
    650 		return;
    651 
    652 	mutex_enter(&sc->sc_iflock);
    653 	if (sc->sc_recv_work_pending == true) {
    654 		callout_schedule(&sc->sc_recv_ch, dse_poll);
    655 		mutex_exit(&sc->sc_iflock);
    656 		return;
    657 	}
    658 
    659 	sc->sc_recv_work_pending = true;
    660 	workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
    661 	mutex_exit(&sc->sc_iflock);
    662 }
    663 
    664 /*
    665  * Invoke the receive workqueue
    666  */
    667 static void
    668 dse_recv_worker(struct work *wk, void *cookie)
    669 {
    670 	struct dse_softc *sc = (struct dse_softc *) cookie;
    671 
    672 	dse_recv(sc);
    673 	mutex_enter(&sc->sc_iflock);
    674 	sc->sc_recv_work_pending = false;
    675 	mutex_exit(&sc->sc_iflock);
    676 
    677 }
    678 
    679 /*
    680  * Do the actual work of receiving data.
    681  */
    682 static void
    683 dse_recv(struct dse_softc *sc)
    684 {
    685 	scsi_dayna_ether_generic cmd_recv;
    686 	int error, len;
    687 
    688 	/* do a recv command */
    689 	/* fill out command buffer */
    690 	memset(&cmd_recv, 0, sizeof(cmd_recv));
    691 	cmd_recv.opcode[0] = DAYNA_CMD_RECV;
    692 	len = MAX_BYTES_RX + DSE_EXTRAS_RX;
    693 	_lto2b(len, &(cmd_recv.length[0]));
    694 	cmd_recv.byte6 = 0xC0;
    695 
    696 	error = dse_scsipi_cmd(sc->sc_periph,
    697 	    (void *)&cmd_recv, sizeof(cmd_recv),
    698 	    sc->sc_rbuf, RBUF_LEN, DSE_RETRIES, DSE_TIMEOUT, NULL,
    699 	    XS_CTL_NOSLEEP | XS_CTL_POLL | XS_CTL_DATA_IN);
    700 	if (error)
    701 		callout_schedule(&sc->sc_recv_ch, dse_poll);
    702 }
    703 
    704 
    705 /*
    706  * We copy the data into mbufs.  When full cluster sized units are present
    707  * we copy into clusters.
    708  */
    709 static struct mbuf *
    710 dse_get(struct dse_softc *sc, uint8_t *data, int totlen)
    711 {
    712 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    713 	struct mbuf *m, *m0, *newm;
    714 	int len;
    715 
    716 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    717 	if (m0 == NULL)
    718 		return NULL;
    719 
    720 	m_set_rcvif(m0, ifp);
    721 	m0->m_pkthdr.len = totlen;
    722 	len	= MHLEN;
    723 	m = m0;
    724 
    725 	while (totlen > 0) {
    726 		if (totlen >= MINCLSIZE) {
    727 			MCLGET(m, M_DONTWAIT);
    728 			if((m->m_flags & M_EXT) == 0)
    729 				goto bad;
    730 
    731 			len = MCLBYTES;
    732 		}
    733 
    734 		if (m == m0) {
    735 			char *newdata = (char *)
    736 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    737 			    sizeof(struct ether_header);
    738 			len -= newdata - m->m_data;
    739 			m->m_data = newdata;
    740 		}
    741 
    742 		m->m_len = len = uimin(totlen, len);
    743 		memcpy(mtod(m, void *), data, len);
    744 		data += len;
    745 
    746 		totlen -= len;
    747 		if (totlen > 0) {
    748 			MGET(newm, M_DONTWAIT, MT_DATA);
    749 			if (newm == NULL)
    750 				goto bad;
    751 
    752 			len = MLEN;
    753 			m = m->m_next = newm;
    754 		}
    755 	}
    756 
    757 	return m0;
    758 
    759 bad:
    760 	m_freem(m0);
    761 	return NULL ;
    762 }
    763 
    764 
    765 #ifdef MAC68K_DEBUG
    766 static int
    767 peek_packet(uint8_t*  buf)
    768 {
    769 	struct ether_header *eh;
    770 	uint16_t type;
    771 	int len;
    772 
    773 	eh = (struct ether_header*)buf;
    774 	type = _2btol((uint8_t*)&(eh->ether_type));
    775 
    776 	len = sizeof(struct ether_header);
    777 
    778 	if (type <= ETHERMTU) {
    779 		/* for 802.3 */
    780 		len += type;
    781 	} else{
    782 		/* for Ethernet II (DIX) */
    783 		switch (type) {
    784 		  case ETHERTYPE_ARP:
    785 			len += 28;
    786 			break;
    787 		  case ETHERTYPE_IP:
    788 			len += _2btol(buf + sizeof(struct ether_header) + 2);
    789 			break;
    790 		  default:
    791 			len = 0;
    792 			goto l_end;
    793 			break;
    794 		}
    795 	}
    796 	if (len < DSE_MINSIZE) {
    797 		len = DSE_MINSIZE;
    798 	}
    799 	len += ETHER_CRC_LEN;
    800 
    801   l_end:;
    802 	return len;
    803 }
    804 #endif
    805 
    806 
    807 /*
    808  * Pass packets to higher levels.
    809  */
    810 static int
    811 dse_read(struct dse_softc *sc, uint8_t *data, int datalen)
    812 {
    813 	struct mbuf *m;
    814 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    815 	int	len;
    816 	int	n;
    817 #ifdef MAC68K_DEBUG
    818 	int	peek_flag = 1;
    819 #endif
    820 
    821 	mutex_enter(&sc->sc_iflock);
    822 	n = 0;
    823 	while (datalen >= DSE_HEADER_RX) {
    824 		/*
    825 		 * fetch bytes of stream.
    826 		 * here length = (ether frame length) + (FCS's 4 bytes)
    827 		 */
    828 		/* fetch frame length */
    829 		len = _2btol(data);
    830 
    831 		/* skip header part */
    832 		data	+= DSE_HEADER_RX;
    833 		datalen -= DSE_HEADER_RX;
    834 
    835 #if 0	/* 03/10/2001  only for debug */
    836 		{
    837 			printf("DATALEN %d len %d\n", datalen, len);
    838 			int	j;
    839 			printf("\ndump[%d]: ",n);
    840 			for ( j = 0 ; j < datalen ; j++ ) {
    841 				printf("%02X ",data[j-DSE_HEADER_RX]);
    842 			}
    843 		}
    844 #endif
    845 #ifdef MAC68K_DEBUG
    846 		if (peek_flag) {
    847 			peek_flag = 0;
    848 			len = peek_packet(data);
    849 		}
    850 #endif
    851 		if (len == 0)
    852 			break;
    853 
    854 #ifdef DSE_DEBUG
    855 		aprint_error_dev(sc->sc_dev, "dse_read: datalen = %d, packetlen"
    856 		    " = %d, proto = 0x%04x\n", datalen, len,
    857 		    ntohs(((struct ether_header *)data)->ether_type));
    858 #endif
    859 		if ((len < (DSE_MINSIZE + ETHER_CRC_LEN)) ||
    860 		     (MAX_BYTES_RX < len)) {
    861 #ifdef DSE_DEBUG
    862 			aprint_error_dev(sc->sc_dev, "invalid packet size "
    863 			    "%d; dropping\n", len);
    864 #endif
    865 			if_statinc(ifp, if_ierrors);
    866 			break;
    867 		}
    868 
    869 		/* Don't need crc. Must keep ether header for BPF */
    870 		m = dse_get(sc, data, len - ETHER_CRC_LEN);
    871 		if (m == NULL) {
    872 #ifdef DSE_DEBUG
    873 			if (sc->sc_debug)
    874 				aprint_error_dev(sc->sc_dev, "dse_read: "
    875 				    "dse_get returned null\n");
    876 #endif
    877 			if_statinc(ifp, if_ierrors);
    878 			goto next_packet;
    879 		}
    880 		if_statinc(ifp, if_ipackets);
    881 
    882 		/*
    883 		 * Check if there's a BPF listener on this interface.
    884 		 * If so, hand off the raw packet to BPF.
    885 		 */
    886 		if (ifp->if_bpf)
    887 			bpf_mtap(ifp, m, BPF_D_OUT);
    888 
    889 		/* Pass the packet up. */
    890 		if_percpuq_enqueue(sc->sc_ipq, m);
    891 
    892   next_packet:
    893 		data	+= len;
    894 		datalen	-= len;
    895 		n++;
    896 	}
    897 	mutex_exit(&sc->sc_iflock);
    898 
    899 	return n;
    900 }
    901 
    902 
    903 static void
    904 dsewatchdog(struct ifnet *ifp)
    905 {
    906 	struct dse_softc *sc = ifp->if_softc;
    907 
    908 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    909 	if_statinc(ifp, if_oerrors);
    910 
    911 	dse_reset(sc);
    912 }
    913 
    914 
    915 static int
    916 dse_reset(struct dse_softc *sc)
    917 {
    918 	int error;
    919 #if 0
    920 	/* Maybe we don't *really* want to reset the entire bus
    921 	 * because the ctron isn't working. We would like to send a
    922 	 * "BUS DEVICE RESET" message, but don't think the ctron
    923 	 * understands it.
    924 	 */
    925 	error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES, 2000,
    926 	    NULL, XS_CTL_RESET);
    927 #endif
    928 	error = dse_init(sc);
    929 	return error;
    930 }
    931 
    932 
    933 static int
    934 dse_init_adaptor(struct dse_softc *sc)
    935 {
    936 	scsi_dayna_ether_generic cmd_vend1;
    937 	u_char tmpbuf[sizeof(cmd_vend1)];
    938 	int error;
    939 
    940 #if 0	/* 07/21/2001 for test */
    941 	/* Maybe we don't *really* want to reset the entire bus
    942 	 * because the ctron isn't working. We would like to send a
    943 	 * "BUS DEVICE RESET" message, but don't think the ctron
    944 	 * understands it.
    945 	 */
    946 	error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES,
    947 	    2000, NULL, XS_CTL_RESET);
    948 #endif
    949 
    950 	cmd_vend1 = sonic_ether_vendor1;
    951 
    952 	error = dse_scsipi_cmd(sc->sc_periph,
    953 	    (struct scsipi_generic *)&cmd_vend1, sizeof(cmd_vend1),
    954 		&(tmpbuf[0]), sizeof(tmpbuf),
    955 	    DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
    956 
    957 	if (error)
    958 		goto l_end;
    959 
    960 	/* wait 500 msec */
    961 	kpause("dsesleep", false, hz / 2, NULL);
    962 
    963 l_end:
    964 	return error;
    965 }
    966 
    967 
    968 static int
    969 dse_get_addr(struct dse_softc *sc, uint8_t *myaddr)
    970 {
    971 	scsi_dayna_ether_generic cmd_get_addr;
    972 	u_char tmpbuf[REQ_LEN_GET_ADDR];
    973 	int error;
    974 
    975 	memset(&cmd_get_addr, 0, sizeof(cmd_get_addr));
    976 	cmd_get_addr.opcode[0] = DAYNA_CMD_GET_ADDR;
    977 	_lto2b(REQ_LEN_GET_ADDR, cmd_get_addr.length);
    978 
    979 	error = dse_scsipi_cmd(sc->sc_periph,
    980 	    (struct scsipi_generic *)&cmd_get_addr, sizeof(cmd_get_addr),
    981 	    tmpbuf, sizeof(tmpbuf),
    982 	    DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
    983 
    984 	if (error == 0) {
    985 		memcpy(myaddr, &(tmpbuf[0]), ETHER_ADDR_LEN);
    986 
    987 		aprint_normal_dev(sc->sc_dev, "ethernet address %s\n",
    988 			   ether_sprintf(myaddr));
    989 	}
    990 
    991 	return error;
    992 }
    993 
    994 
    995 #if 0	/* 07/16/2000 comment-out */
    996 static int
    997 dse_set_mode(struct dse_softc *sc, int len, int mode)
    998 
    999 	return 0;
   1000 }
   1001 #endif
   1002 
   1003 
   1004 static int
   1005 dse_init(struct dse_softc *sc)
   1006 {
   1007 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1008 	int error = 0;
   1009 
   1010 	if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
   1011 		ifp->if_flags |= IFF_RUNNING;
   1012 		mutex_enter(&sc->sc_iflock);
   1013 		if (!sc->sc_recv_work_pending)  {
   1014 			sc->sc_recv_work_pending = true;
   1015 			workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work,
   1016 			    NULL);
   1017 		}
   1018 		mutex_exit(&sc->sc_iflock);
   1019 		ifp->if_flags &= ~IFF_OACTIVE;
   1020 		mutex_enter(&sc->sc_iflock);
   1021 		if (!sc->sc_send_work_pending)  {
   1022 			sc->sc_send_work_pending = true;
   1023 			workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work,
   1024 			    NULL);
   1025 		}
   1026 		mutex_exit(&sc->sc_iflock);
   1027 	}
   1028 	return error;
   1029 }
   1030 
   1031 
   1032 static uint8_t	BROADCAST_ADDR[ETHER_ADDR_LEN] =
   1033 			{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
   1034 
   1035 
   1036 static int
   1037 dse_set_multi(struct dse_softc *sc)
   1038 {
   1039 	scsi_dayna_ether_generic cmd_set_multi;
   1040 	struct ether_multistep step;
   1041 	struct ether_multi *enm;
   1042 	u_char *cp, *mybuf;
   1043 	int error, len;
   1044 
   1045 	error = 0;
   1046 
   1047 #ifdef DSE_DEBUG
   1048 	aprint_error_dev(sc->sc_dev, "dse_set_multi\n");
   1049 #endif
   1050 
   1051 	mybuf = malloc(ETHER_ADDR_LEN * DSE_MCAST_MAX, M_DEVBUF, M_NOWAIT);
   1052 	if (mybuf == NULL) {
   1053 		error = EIO;
   1054 		goto l_end;
   1055 	}
   1056 
   1057 	/*
   1058 	 * copy all entries to transfer buffer
   1059 	 */
   1060 	cp = mybuf;
   1061 	len = 0;
   1062 	ETHER_FIRST_MULTI(step, &(sc->sc_ethercom), enm);
   1063 	while ((len < (DSE_MCAST_MAX - 1)) && (enm != NULL)) {
   1064 		/* ### refer low side entry */
   1065 		memcpy(cp, enm->enm_addrlo, ETHER_ADDR_LEN);
   1066 
   1067 		cp += ETHER_ADDR_LEN;
   1068 		len++;
   1069 		ETHER_NEXT_MULTI(step, enm);
   1070 	}
   1071 
   1072 	/* add broadcast address as default */
   1073 	memcpy(cp, BROADCAST_ADDR, ETHER_ADDR_LEN);
   1074 	len++;
   1075 
   1076 	len *= ETHER_ADDR_LEN;
   1077 
   1078 	memset(&cmd_set_multi, 0, sizeof(cmd_set_multi));
   1079 	cmd_set_multi.opcode[0] = DAYNA_CMD_SET_MULTI;
   1080 	_lto2b(len, cmd_set_multi.length);
   1081 
   1082 	error = dse_scsipi_cmd(sc->sc_periph,
   1083 	    (struct scsipi_generic*)&cmd_set_multi, sizeof(cmd_set_multi),
   1084 	    mybuf, len, DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_DATA_OUT);
   1085 
   1086 	free(mybuf, M_DEVBUF);
   1087 
   1088 l_end:
   1089 	return error;
   1090 }
   1091 
   1092 
   1093 static void
   1094 dse_stop(struct dse_softc *sc)
   1095 {
   1096 	/* Don't schedule any reads */
   1097 	callout_stop(&sc->sc_recv_ch);
   1098 
   1099 	/* Wait for the workqueues to finish */
   1100 	mutex_enter(&sc->sc_iflock);
   1101 	workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
   1102 	workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
   1103 	mutex_exit(&sc->sc_iflock);
   1104 
   1105 	/* Abort any scsi cmds in progress */
   1106 	mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
   1107 	scsipi_kill_pending(sc->sc_periph);
   1108 	mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
   1109 }
   1110 
   1111 
   1112 /*
   1113  * Process an ioctl request.
   1114  */
   1115 static int
   1116 dse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1117 {
   1118 	struct dse_softc *sc;
   1119 	struct ifaddr *ifa;
   1120 	struct ifreq *ifr;
   1121 	struct sockaddr *sa;
   1122 	int error;
   1123 
   1124 	error = 0;
   1125 	sc = ifp->if_softc;
   1126 	ifa = (struct ifaddr *)data;
   1127 	ifr = (struct ifreq *)data;
   1128 
   1129 	switch (cmd) {
   1130 	case SIOCINITIFADDR:
   1131 		mutex_enter(&sc->sc_iflock);
   1132 		if ((error = dse_enable(sc)) != 0)
   1133 			break;
   1134 		ifp->if_flags |= IFF_UP;
   1135 		mutex_exit(&sc->sc_iflock);
   1136 
   1137 #if 0
   1138 		if ((error = dse_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
   1139 			break;
   1140 #endif
   1141 
   1142 		switch (ifa->ifa_addr->sa_family) {
   1143 #ifdef INET
   1144 		case AF_INET:
   1145 			if ((error = dse_init(sc)) != 0)
   1146 				break;
   1147 			arp_ifinit(ifp, ifa);
   1148 			break;
   1149 #endif
   1150 #ifdef NETATALK
   1151 		case AF_APPLETALK:
   1152 			if ((error = dse_init(sc)) != 0)
   1153 				break;
   1154 			break;
   1155 #endif
   1156 		default:
   1157 			error = dse_init(sc);
   1158 			break;
   1159 		}
   1160 		break;
   1161 
   1162 
   1163 	case SIOCSIFADDR:
   1164 		mutex_enter(&sc->sc_iflock);
   1165 		error = dse_enable(sc);
   1166 		mutex_exit(&sc->sc_iflock);
   1167 		if (error != 0)
   1168 			break;
   1169 		ifp->if_flags |= IFF_UP;
   1170 
   1171 		switch (ifa->ifa_addr->sa_family) {
   1172 #ifdef INET
   1173 		case AF_INET:
   1174 			if ((error = dse_init(sc)) != 0)
   1175 				break;
   1176 			arp_ifinit(ifp, ifa);
   1177 			break;
   1178 #endif
   1179 #ifdef NETATALK
   1180 		case AF_APPLETALK:
   1181 			if ((error = dse_init(sc)) != 0)
   1182 				break;
   1183 			break;
   1184 #endif
   1185 		default:
   1186 			error = dse_init(sc);
   1187 			break;
   1188 		}
   1189 		break;
   1190 
   1191 	case SIOCSIFFLAGS:
   1192 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1193 			break;
   1194 		/* XXX re-use ether_ioctl() */
   1195 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1196 		case IFF_RUNNING:
   1197 			/*
   1198 			 * If interface is marked down and it is running, then
   1199 			 * stop it.
   1200 			 */
   1201 			dse_stop(sc);
   1202 			mutex_enter(&sc->sc_iflock);
   1203 			ifp->if_flags &= ~IFF_RUNNING;
   1204 			dse_disable(sc);
   1205 			mutex_exit(&sc->sc_iflock);
   1206 			break;
   1207 		case IFF_UP:
   1208 			/*
   1209 			 * If interface is marked up and it is stopped, then
   1210 			 * start it.
   1211 			 */
   1212 			mutex_enter(&sc->sc_iflock);
   1213 			error = dse_enable(sc);
   1214 			mutex_exit(&sc->sc_iflock);
   1215 			if (error)
   1216 				break;
   1217 			error = dse_init(sc);
   1218 			break;
   1219 		default:
   1220 			/*
   1221 			 * Reset the interface to pick up changes in any other
   1222 			 * flags that affect hardware registers.
   1223 			 */
   1224 			mutex_enter(&sc->sc_iflock);
   1225 			if (sc->sc_enabled)
   1226 				error = dse_init(sc);
   1227 			mutex_exit(&sc->sc_iflock);
   1228 			break;
   1229 		}
   1230 #ifdef DSE_DEBUG
   1231 		if (ifp->if_flags & IFF_DEBUG)
   1232 			sc->sc_debug = 1;
   1233 		else
   1234 			sc->sc_debug = 0;
   1235 #endif
   1236 		break;
   1237 
   1238 	case SIOCADDMULTI:
   1239 		if (sc->sc_enabled == 0) {
   1240 			error = EIO;
   1241 			break;
   1242 		}
   1243 		mutex_enter(&sc->sc_iflock);
   1244 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
   1245 		mutex_exit(&sc->sc_iflock);
   1246 		if (ether_addmulti(sa, &sc->sc_ethercom) == ENETRESET) {
   1247 			error = dse_set_multi(sc);
   1248 #ifdef DSE_DEBUG
   1249 			aprint_error_dev(sc->sc_dev, "add multi: %s\n",
   1250 				   ether_sprintf(ifr->ifr_addr.sa_data));
   1251 #endif
   1252 		} else
   1253 			error = 0;
   1254 
   1255 		mutex_enter(&sc->sc_iflock);
   1256 		sockaddr_free(sa);
   1257 		mutex_exit(&sc->sc_iflock);
   1258 
   1259 		break;
   1260 
   1261 	case SIOCDELMULTI:
   1262 		if (sc->sc_enabled == 0) {
   1263 			error = EIO;
   1264 			break;
   1265 		}
   1266 		mutex_enter(&sc->sc_iflock);
   1267 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
   1268 		mutex_exit(&sc->sc_iflock);
   1269 		if (ether_delmulti(sa, &sc->sc_ethercom) == ENETRESET) {
   1270 			error = dse_set_multi(sc);
   1271 #ifdef DSE_DEBUG
   1272 			aprint_error_dev(sc->sc_dev, "delete multi: %s\n",
   1273 			    ether_sprintf(ifr->ifr_addr.sa_data));
   1274 #endif
   1275 		} else
   1276 			error = 0;
   1277 
   1278 		mutex_enter(&sc->sc_iflock);
   1279 		sockaddr_free(sa);
   1280 		mutex_exit(&sc->sc_iflock);
   1281 
   1282 		break;
   1283 
   1284 	default:
   1285 		error = ether_ioctl(ifp, cmd, data);
   1286 		break;
   1287 	}
   1288 
   1289 
   1290 	return error;
   1291 }
   1292 
   1293 
   1294 /*
   1295  * Enable the network interface.
   1296  */
   1297 int
   1298 dse_enable(struct dse_softc *sc)
   1299 {
   1300 	struct scsipi_periph *periph = sc->sc_periph;
   1301 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1302 	int error = 0;
   1303 
   1304 	if (sc->sc_enabled == 0) {
   1305 		if ((error = scsipi_adapter_addref(adapt)) == 0)
   1306 			sc->sc_enabled = 1;
   1307 		else
   1308 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1309 	}
   1310 
   1311 	return error;
   1312 }
   1313 
   1314 
   1315 /*
   1316  * Disable the network interface.
   1317  */
   1318 void
   1319 dse_disable(struct dse_softc *sc)
   1320 {
   1321 	struct scsipi_periph *periph = sc->sc_periph;
   1322 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1323 	if (sc->sc_enabled != 0) {
   1324 		scsipi_adapter_delref(adapt);
   1325 		sc->sc_enabled = 0;
   1326 	}
   1327 }
   1328 
   1329 
   1330 #define	DSEUNIT(z)	(minor(z))
   1331 
   1332 /*
   1333  * open the device.
   1334  */
   1335 int
   1336 dseopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1337 {
   1338 	int unit, error;
   1339 	struct dse_softc *sc;
   1340 	struct scsipi_periph *periph;
   1341 	struct scsipi_adapter *adapt;
   1342 
   1343 	unit = DSEUNIT(dev);
   1344 	sc = device_lookup_private(&dse_cd, unit);
   1345 	if (sc == NULL)
   1346 		return ENXIO;
   1347 
   1348 	periph = sc->sc_periph;
   1349 	adapt = periph->periph_channel->chan_adapter;
   1350 
   1351 	if ((error = scsipi_adapter_addref(adapt)) != 0)
   1352 		return error;
   1353 
   1354 	SC_DEBUG(periph, SCSIPI_DB1,
   1355 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
   1356 	    dse_cd.cd_ndevs));
   1357 
   1358 	periph->periph_flags |= PERIPH_OPEN;
   1359 
   1360 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
   1361 
   1362 	return 0;
   1363 }
   1364 
   1365 
   1366 /*
   1367  * close the device.. only called if we are the LAST
   1368  * occurrence of an open device
   1369  */
   1370 int
   1371 dseclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1372 {
   1373 	struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
   1374 	struct scsipi_periph *periph = sc->sc_periph;
   1375 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1376 
   1377 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
   1378 
   1379 	scsipi_wait_drain(periph);
   1380 
   1381 	scsipi_adapter_delref(adapt);
   1382 	periph->periph_flags &= ~PERIPH_OPEN;
   1383 
   1384 	return 0;
   1385 }
   1386 
   1387 
   1388 /*
   1389  * Perform special action on behalf of the user
   1390  * Only does generic scsi ioctls.
   1391  */
   1392 int
   1393 dseioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1394 {
   1395 	struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
   1396 
   1397 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
   1398 }
   1399 
   1400