Home | History | Annotate | Line # | Download | only in scsipi
if_se.c revision 1.94
      1  1.94     ozaki /*	$NetBSD: if_se.c,v 1.94 2016/12/15 09:28:06 ozaki-r Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     16   1.1   thorpej  *    must display the following acknowledgement:
     17   1.1   thorpej  *	This product includes software developed by Ian W. Dall.
     18   1.1   thorpej  * 4. The name of the author may not be used to endorse or promote products
     19   1.1   thorpej  *    derived from this software without specific prior written permission.
     20   1.1   thorpej  *
     21   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1   thorpej  */
     32   1.1   thorpej 
     33   1.1   thorpej /*
     34  1.11  matthias  * Driver for Cabletron EA41x scsi ethernet adaptor.
     35  1.11  matthias  *
     36   1.1   thorpej  * Written by Ian Dall <ian.dall (at) dsto.defence.gov.au> Feb 3, 1997
     37  1.11  matthias  *
     38  1.11  matthias  * Acknowledgement: Thanks are due to Philip L. Budne <budd (at) cs.bu.edu>
     39  1.27     soren  * who reverse engineered the EA41x. In developing this code,
     40  1.11  matthias  * Phil's userland daemon "etherd", was refered to extensively in lieu
     41  1.11  matthias  * of accurate documentation for the device.
     42   1.1   thorpej  *
     43   1.1   thorpej  * This is a weird device! It doesn't conform to the scsi spec in much
     44  1.19        is  * at all. About the only standard command supported is inquiry. Most
     45   1.1   thorpej  * commands are 6 bytes long, but the recv data is only 1 byte.  Data
     46   1.1   thorpej  * must be received by periodically polling the device with the recv
     47   1.1   thorpej  * command.
     48   1.1   thorpej  *
     49   1.1   thorpej  * This driver is also a bit unusual. It must look like a network
     50   1.1   thorpej  * interface and it must also appear to be a scsi device to the scsi
     51   1.1   thorpej  * system. Hence there are cases where there are two entry points. eg
     52   1.1   thorpej  * sestart is to be called from the scsi subsytem and se_ifstart from
     53   1.1   thorpej  * the network interface subsystem.  In addition, to facilitate scsi
     54   1.1   thorpej  * commands issued by userland programs, there are open, close and
     55   1.1   thorpej  * ioctl entry points. This allows a user program to, for example,
     56   1.1   thorpej  * display the ea41x stats and download new code into the adaptor ---
     57   1.1   thorpej  * functions which can't be performed through the ifconfig interface.
     58   1.5  matthias  * Normal operation does not require any special userland program.
     59   1.1   thorpej  */
     60  1.38     lukem 
     61  1.38     lukem #include <sys/cdefs.h>
     62  1.94     ozaki __KERNEL_RCSID(0, "$NetBSD: if_se.c,v 1.94 2016/12/15 09:28:06 ozaki-r Exp $");
     63   1.1   thorpej 
     64  1.88     pooka #ifdef _KERNEL_OPT
     65  1.14  jonathan #include "opt_inet.h"
     66  1.14  jonathan #include "opt_atalk.h"
     67  1.88     pooka #endif
     68   1.1   thorpej 
     69   1.1   thorpej #include <sys/param.h>
     70   1.1   thorpej #include <sys/systm.h>
     71  1.28   thorpej #include <sys/callout.h>
     72   1.1   thorpej #include <sys/syslog.h>
     73   1.1   thorpej #include <sys/kernel.h>
     74   1.1   thorpej #include <sys/file.h>
     75   1.1   thorpej #include <sys/stat.h>
     76   1.1   thorpej #include <sys/ioctl.h>
     77   1.1   thorpej #include <sys/buf.h>
     78   1.1   thorpej #include <sys/uio.h>
     79   1.1   thorpej #include <sys/malloc.h>
     80   1.1   thorpej #include <sys/errno.h>
     81   1.1   thorpej #include <sys/device.h>
     82   1.1   thorpej #include <sys/disklabel.h>
     83   1.1   thorpej #include <sys/disk.h>
     84   1.1   thorpej #include <sys/proc.h>
     85   1.1   thorpej #include <sys/conf.h>
     86   1.1   thorpej 
     87   1.9    bouyer #include <dev/scsipi/scsipi_all.h>
     88   1.9    bouyer #include <dev/scsipi/scsi_ctron_ether.h>
     89   1.9    bouyer #include <dev/scsipi/scsiconf.h>
     90   1.1   thorpej 
     91   1.1   thorpej #include <sys/mbuf.h>
     92   1.1   thorpej 
     93   1.1   thorpej #include <sys/socket.h>
     94   1.1   thorpej #include <net/if.h>
     95   1.1   thorpej #include <net/if_dl.h>
     96   1.1   thorpej #include <net/if_ether.h>
     97   1.5  matthias #include <net/if_media.h>
     98   1.1   thorpej 
     99   1.1   thorpej #ifdef INET
    100   1.1   thorpej #include <netinet/in.h>
    101   1.1   thorpej #include <netinet/if_inarp.h>
    102   1.2       cgd #endif
    103   1.2       cgd 
    104   1.2       cgd 
    105   1.5  matthias #ifdef NETATALK
    106   1.5  matthias #include <netatalk/at.h>
    107   1.5  matthias #endif
    108   1.5  matthias 
    109   1.1   thorpej 
    110   1.1   thorpej #include <net/bpf.h>
    111   1.1   thorpej #include <net/bpfdesc.h>
    112   1.1   thorpej 
    113   1.1   thorpej #define SETIMEOUT	1000
    114   1.1   thorpej #define	SEOUTSTANDING	4
    115   1.1   thorpej #define	SERETRIES	4
    116   1.1   thorpej #define SE_PREFIX	4
    117   1.1   thorpej #define ETHER_CRC	4
    118   1.1   thorpej #define SEMINSIZE	60
    119   1.1   thorpej 
    120   1.1   thorpej /* Make this big enough for an ETHERMTU packet in promiscuous mode. */
    121   1.1   thorpej #define MAX_SNAP	(ETHERMTU + sizeof(struct ether_header) + \
    122   1.1   thorpej 			 SE_PREFIX + ETHER_CRC)
    123   1.1   thorpej 
    124  1.11  matthias /* 10 full length packets appears to be the max ever returned. 16k is OK */
    125  1.11  matthias #define RBUF_LEN	(16 * 1024)
    126  1.11  matthias 
    127  1.57     perry /* Tuning parameters:
    128  1.11  matthias  * The EA41x only returns a maximum of 10 packets (regardless of size).
    129  1.11  matthias  * We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
    130  1.11  matthias  * per read
    131  1.11  matthias  */
    132  1.11  matthias #define RDATA_MAX 10
    133  1.11  matthias #define RDATA_GOAL 8
    134  1.11  matthias 
    135  1.11  matthias /* se_poll and se_poll0 are the normal polling rate and the minimum
    136  1.11  matthias  * polling rate respectively. se_poll0 should be chosen so that at
    137  1.11  matthias  * maximum ethernet speed, we will read nearly RDATA_MAX packets. se_poll
    138  1.11  matthias  * should be chosen for reasonable maximum latency.
    139  1.11  matthias  * In practice, if we are being saturated with min length packets, we
    140  1.11  matthias  * can't poll fast enough. Polling with zero delay actually
    141  1.11  matthias  * worsens performance. se_poll0 is enforced to be always at least 1
    142   1.5  matthias  */
    143   1.1   thorpej #define SE_POLL 40		/* default in milliseconds */
    144  1.11  matthias #define SE_POLL0 10		/* default in milliseconds */
    145  1.11  matthias int se_poll = 0;		/* Delay in ticks set at attach time */
    146   1.1   thorpej int se_poll0 = 0;
    147  1.11  matthias int se_max_received = 0;	/* Instrumentation */
    148   1.1   thorpej 
    149   1.1   thorpej #define	PROTOCMD(p, d) \
    150   1.3   thorpej 	((d) = (p))
    151   1.1   thorpej 
    152  1.63      cube #define	PROTOCMD_DECL(name) \
    153  1.63      cube 	static const struct scsi_ctron_ether_generic name
    154   1.1   thorpej 
    155  1.63      cube #define	PROTOCMD_DECL_SPECIAL(name) \
    156  1.63      cube 	static const struct __CONCAT(scsi_,name) name
    157   1.3   thorpej 
    158   1.3   thorpej /* Command initializers for commands using scsi_ctron_ether_generic */
    159  1.63      cube PROTOCMD_DECL(ctron_ether_send)  = {CTRON_ETHER_SEND, 0, {0,0}, 0};
    160  1.63      cube PROTOCMD_DECL(ctron_ether_add_proto) = {CTRON_ETHER_ADD_PROTO, 0, {0,0}, 0};
    161  1.63      cube PROTOCMD_DECL(ctron_ether_get_addr) = {CTRON_ETHER_GET_ADDR, 0, {0,0}, 0};
    162  1.63      cube PROTOCMD_DECL(ctron_ether_set_media) = {CTRON_ETHER_SET_MEDIA, 0, {0,0}, 0};
    163  1.63      cube PROTOCMD_DECL(ctron_ether_set_addr) = {CTRON_ETHER_SET_ADDR, 0, {0,0}, 0};
    164  1.63      cube PROTOCMD_DECL(ctron_ether_set_multi) = {CTRON_ETHER_SET_MULTI, 0, {0,0}, 0};
    165  1.63      cube PROTOCMD_DECL(ctron_ether_remove_multi) =
    166  1.63      cube     {CTRON_ETHER_REMOVE_MULTI, 0, {0,0}, 0};
    167   1.3   thorpej 
    168   1.3   thorpej /* Command initializers for commands using their own structures */
    169  1.63      cube PROTOCMD_DECL_SPECIAL(ctron_ether_recv) = {CTRON_ETHER_RECV};
    170  1.63      cube PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode) =
    171  1.63      cube     {CTRON_ETHER_SET_MODE, 0, {0,0}, 0};
    172   1.1   thorpej 
    173   1.1   thorpej struct se_softc {
    174  1.85       chs 	device_t sc_dev;
    175   1.1   thorpej 	struct ethercom sc_ethercom;	/* Ethernet common part */
    176  1.34    bouyer 	struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
    177  1.28   thorpej 
    178  1.28   thorpej 	struct callout sc_ifstart_ch;
    179  1.28   thorpej 	struct callout sc_recv_ch;
    180  1.28   thorpej 
    181   1.1   thorpej 	char *sc_tbuf;
    182   1.1   thorpej 	char *sc_rbuf;
    183   1.1   thorpej 	int protos;
    184   1.5  matthias #define PROTO_IP	0x01
    185   1.5  matthias #define PROTO_ARP	0x02
    186   1.5  matthias #define PROTO_REVARP	0x04
    187   1.5  matthias #define PROTO_AT	0x08
    188   1.5  matthias #define PROTO_AARP	0x10
    189   1.1   thorpej 	int sc_debug;
    190   1.1   thorpej 	int sc_flags;
    191   1.1   thorpej #define SE_NEED_RECV 0x1
    192  1.11  matthias 	int sc_last_timeout;
    193  1.21   thorpej 	int sc_enabled;
    194   1.1   thorpej };
    195   1.1   thorpej 
    196  1.78    cegger static int	sematch(device_t, cfdata_t, void *);
    197  1.78    cegger static void	seattach(device_t, device_t, void *);
    198   1.1   thorpej 
    199  1.56     perry static void	se_ifstart(struct ifnet *);
    200  1.56     perry static void	sestart(struct scsipi_periph *);
    201   1.1   thorpej 
    202  1.56     perry static void	sedone(struct scsipi_xfer *, int);
    203  1.64  christos static int	se_ioctl(struct ifnet *, u_long, void *);
    204  1.56     perry static void	sewatchdog(struct ifnet *);
    205  1.56     perry 
    206  1.91  jakllsch #if 0
    207  1.60     perry static inline u_int16_t ether_cmp(void *, void *);
    208  1.91  jakllsch #endif
    209  1.56     perry static void	se_recv(void *);
    210  1.56     perry static struct mbuf *se_get(struct se_softc *, char *, int);
    211  1.56     perry static int	se_read(struct se_softc *, char *, int);
    212  1.56     perry static int	se_reset(struct se_softc *);
    213  1.56     perry static int	se_add_proto(struct se_softc *, int);
    214  1.56     perry static int	se_get_addr(struct se_softc *, u_int8_t *);
    215  1.56     perry static int	se_set_media(struct se_softc *, int);
    216  1.56     perry static int	se_init(struct se_softc *);
    217  1.56     perry static int	se_set_multi(struct se_softc *, u_int8_t *);
    218  1.56     perry static int	se_remove_multi(struct se_softc *, u_int8_t *);
    219   1.1   thorpej #if 0
    220  1.56     perry static int	sc_set_all_multi(struct se_softc *, int);
    221   1.1   thorpej #endif
    222  1.56     perry static void	se_stop(struct se_softc *);
    223  1.60     perry static inline int se_scsipi_cmd(struct scsipi_periph *periph,
    224  1.10     enami 			struct scsipi_generic *scsipi_cmd,
    225  1.55   reinoud 			int cmdlen, u_char *data_addr, int datalen,
    226  1.10     enami 			int retries, int timeout, struct buf *bp,
    227  1.56     perry 			int flags);
    228  1.56     perry static void	se_delayed_ifstart(void *);
    229   1.5  matthias static int	se_set_mode(struct se_softc *, int, int);
    230   1.1   thorpej 
    231  1.56     perry int	se_enable(struct se_softc *);
    232  1.56     perry void	se_disable(struct se_softc *);
    233  1.21   thorpej 
    234  1.85       chs CFATTACH_DECL_NEW(se, sizeof(struct se_softc),
    235  1.43   thorpej     sematch, seattach, NULL, NULL);
    236   1.1   thorpej 
    237  1.13   thorpej extern struct cfdriver se_cd;
    238  1.40   gehenna 
    239  1.40   gehenna dev_type_open(seopen);
    240  1.40   gehenna dev_type_close(seclose);
    241  1.40   gehenna dev_type_ioctl(seioctl);
    242  1.40   gehenna 
    243  1.40   gehenna const struct cdevsw se_cdevsw = {
    244  1.86  dholland 	.d_open = seopen,
    245  1.86  dholland 	.d_close = seclose,
    246  1.86  dholland 	.d_read = noread,
    247  1.86  dholland 	.d_write = nowrite,
    248  1.86  dholland 	.d_ioctl = seioctl,
    249  1.86  dholland 	.d_stop = nostop,
    250  1.86  dholland 	.d_tty = notty,
    251  1.86  dholland 	.d_poll = nopoll,
    252  1.86  dholland 	.d_mmap = nommap,
    253  1.86  dholland 	.d_kqfilter = nokqfilter,
    254  1.87  dholland 	.d_discard = nodiscard,
    255  1.86  dholland 	.d_flag = D_OTHER
    256  1.40   gehenna };
    257   1.1   thorpej 
    258  1.34    bouyer const struct scsipi_periphsw se_switch = {
    259   1.1   thorpej 	NULL,			/* Use default error handler */
    260   1.1   thorpej 	sestart,		/* have a queue, served by this */
    261   1.1   thorpej 	NULL,			/* have no async handler */
    262   1.1   thorpej 	sedone,			/* deal with stats at interrupt time */
    263   1.1   thorpej };
    264   1.1   thorpej 
    265  1.45   mycroft const struct scsipi_inquiry_pattern se_patterns[] = {
    266   1.1   thorpej 	{T_PROCESSOR, T_FIXED,
    267  1.11  matthias 	 "CABLETRN",         "EA412",                 ""},
    268   1.1   thorpej 	{T_PROCESSOR, T_FIXED,
    269  1.11  matthias 	 "Cabletrn",         "EA412",                 ""},
    270   1.1   thorpej };
    271   1.1   thorpej 
    272  1.91  jakllsch #if 0
    273   1.1   thorpej /*
    274   1.1   thorpej  * Compare two Ether/802 addresses for equality, inlined and
    275   1.1   thorpej  * unrolled for speed.
    276  1.37   thorpej  * Note: use this like memcmp()
    277   1.1   thorpej  */
    278  1.60     perry static inline u_int16_t
    279  1.76       dsl ether_cmp(void *one, void *two)
    280   1.1   thorpej {
    281  1.55   reinoud 	u_int16_t *a = (u_int16_t *) one;
    282  1.55   reinoud 	u_int16_t *b = (u_int16_t *) two;
    283  1.55   reinoud 	u_int16_t diff;
    284   1.1   thorpej 
    285   1.1   thorpej 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    286   1.1   thorpej 
    287   1.1   thorpej 	return (diff);
    288   1.1   thorpej }
    289   1.1   thorpej 
    290   1.1   thorpej #define ETHER_CMP	ether_cmp
    291  1.91  jakllsch #endif
    292   1.1   thorpej 
    293   1.5  matthias static int
    294  1.78    cegger sematch(device_t parent, cfdata_t match, void *aux)
    295   1.1   thorpej {
    296   1.9    bouyer 	struct scsipibus_attach_args *sa = aux;
    297   1.1   thorpej 	int priority;
    298   1.1   thorpej 
    299   1.9    bouyer 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    300  1.58       jdc 	    se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
    301   1.1   thorpej 	    sizeof(se_patterns[0]), &priority);
    302   1.1   thorpej 	return (priority);
    303   1.1   thorpej }
    304   1.1   thorpej 
    305   1.1   thorpej /*
    306   1.1   thorpej  * The routine called by the low level scsi routine when it discovers
    307   1.1   thorpej  * a device suitable for this driver.
    308   1.1   thorpej  */
    309   1.5  matthias static void
    310  1.78    cegger seattach(device_t parent, device_t self, void *aux)
    311   1.1   thorpej {
    312  1.61   thorpej 	struct se_softc *sc = device_private(self);
    313   1.9    bouyer 	struct scsipibus_attach_args *sa = aux;
    314  1.34    bouyer 	struct scsipi_periph *periph = sa->sa_periph;
    315   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    316  1.55   reinoud 	u_int8_t myaddr[ETHER_ADDR_LEN];
    317   1.1   thorpej 
    318  1.85       chs 	sc->sc_dev = self;
    319  1.85       chs 
    320   1.5  matthias 	printf("\n");
    321  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB2, ("seattach: "));
    322   1.1   thorpej 
    323  1.66        ad 	callout_init(&sc->sc_ifstart_ch, 0);
    324  1.66        ad 	callout_init(&sc->sc_recv_ch, 0);
    325  1.28   thorpej 
    326  1.34    bouyer 
    327   1.1   thorpej 	/*
    328   1.1   thorpej 	 * Store information needed to contact our base driver
    329   1.1   thorpej 	 */
    330  1.34    bouyer 	sc->sc_periph = periph;
    331  1.85       chs 	periph->periph_dev = sc->sc_dev;
    332  1.34    bouyer 	periph->periph_switch = &se_switch;
    333  1.34    bouyer 
    334  1.34    bouyer 	/* XXX increase openings? */
    335   1.1   thorpej 
    336   1.1   thorpej 	se_poll = (SE_POLL * hz) / 1000;
    337   1.1   thorpej 	se_poll = se_poll? se_poll: 1;
    338   1.1   thorpej 	se_poll0 = (SE_POLL0 * hz) / 1000;
    339   1.1   thorpej 	se_poll0 = se_poll0? se_poll0: 1;
    340   1.1   thorpej 
    341   1.1   thorpej 	/*
    342   1.1   thorpej 	 * Initialize and attach a buffer
    343   1.1   thorpej 	 */
    344   1.1   thorpej 	sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
    345   1.1   thorpej 			     M_DEVBUF, M_NOWAIT);
    346   1.5  matthias 	if (sc->sc_tbuf == 0)
    347   1.1   thorpej 		panic("seattach: can't allocate transmit buffer");
    348   1.1   thorpej 
    349   1.1   thorpej 	sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_NOWAIT);/* A Guess */
    350   1.5  matthias 	if (sc->sc_rbuf == 0)
    351   1.1   thorpej 		panic("seattach: can't allocate receive buffer");
    352   1.1   thorpej 
    353   1.1   thorpej 	se_get_addr(sc, myaddr);
    354   1.1   thorpej 
    355   1.1   thorpej 	/* Initialize ifnet structure. */
    356  1.85       chs 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
    357   1.1   thorpej 	ifp->if_softc = sc;
    358   1.1   thorpej 	ifp->if_start = se_ifstart;
    359   1.1   thorpej 	ifp->if_ioctl = se_ioctl;
    360   1.1   thorpej 	ifp->if_watchdog = sewatchdog;
    361   1.1   thorpej 	ifp->if_flags =
    362   1.1   thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    363  1.33   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    364   1.1   thorpej 
    365   1.1   thorpej 	/* Attach the interface. */
    366  1.89     ozaki 	if_initialize(ifp);
    367   1.1   thorpej 	ether_ifattach(ifp, myaddr);
    368  1.89     ozaki 	if_register(ifp);
    369   1.1   thorpej }
    370   1.1   thorpej 
    371   1.1   thorpej 
    372  1.60     perry static inline int
    373  1.84  christos se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
    374  1.84  christos     int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
    375  1.84  christos     struct buf *bp, int flags)
    376   1.1   thorpej {
    377   1.1   thorpej 	int error;
    378  1.10     enami 
    379  1.50   mycroft 	error = scsipi_command(periph, cmd, cmdlen, data_addr,
    380  1.10     enami 	    datalen, retries, timeout, bp, flags);
    381  1.10     enami 	return (error);
    382   1.1   thorpej }
    383   1.5  matthias 
    384   1.1   thorpej /* Start routine for calling from scsi sub system */
    385   1.5  matthias static void
    386  1.75       dsl sestart(struct scsipi_periph *periph)
    387   1.1   thorpej {
    388  1.85       chs 	struct se_softc *sc = device_private(periph->periph_dev);
    389   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    390   1.1   thorpej 	int s = splnet();
    391  1.10     enami 
    392   1.1   thorpej 	se_ifstart(ifp);
    393   1.1   thorpej 	(void) splx(s);
    394   1.1   thorpej }
    395   1.1   thorpej 
    396   1.5  matthias static void
    397  1.75       dsl se_delayed_ifstart(void *v)
    398   1.1   thorpej {
    399   1.1   thorpej 	struct ifnet *ifp = v;
    400  1.21   thorpej 	struct se_softc *sc = ifp->if_softc;
    401  1.21   thorpej 	int s;
    402  1.10     enami 
    403  1.21   thorpej 	s = splnet();
    404  1.21   thorpej 	if (sc->sc_enabled) {
    405  1.21   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    406  1.21   thorpej 		se_ifstart(ifp);
    407  1.21   thorpej 	}
    408   1.1   thorpej 	splx(s);
    409   1.1   thorpej }
    410   1.1   thorpej 
    411   1.1   thorpej /*
    412   1.1   thorpej  * Start transmission on the interface.
    413   1.1   thorpej  * Always called at splnet().
    414   1.1   thorpej  */
    415   1.5  matthias static void
    416  1.75       dsl se_ifstart(struct ifnet *ifp)
    417   1.1   thorpej {
    418   1.1   thorpej 	struct se_softc *sc = ifp->if_softc;
    419   1.1   thorpej 	struct scsi_ctron_ether_generic send_cmd;
    420   1.1   thorpej 	struct mbuf *m, *m0;
    421   1.1   thorpej 	int len, error;
    422  1.55   reinoud 	u_char *cp;
    423   1.1   thorpej 
    424   1.1   thorpej 	/* Don't transmit if interface is busy or not running */
    425   1.1   thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    426   1.1   thorpej 		return;
    427   1.1   thorpej 
    428  1.33   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    429   1.1   thorpej 	if (m0 == 0)
    430   1.1   thorpej 		return;
    431   1.1   thorpej 	/* If BPF is listening on this interface, let it see the
    432   1.1   thorpej 	 * packet before we commit it to the wire.
    433   1.1   thorpej 	 */
    434  1.81     joerg 	bpf_mtap(ifp, m0);
    435   1.1   thorpej 
    436   1.1   thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
    437   1.1   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    438   1.1   thorpej 		panic("ctscstart: no header mbuf");
    439   1.1   thorpej 	len = m0->m_pkthdr.len;
    440   1.1   thorpej 
    441   1.1   thorpej 	/* Mark the interface busy. */
    442   1.1   thorpej 	ifp->if_flags |= IFF_OACTIVE;
    443   1.1   thorpej 
    444   1.1   thorpej 	/* Chain; copy into linear buffer we allocated at attach time. */
    445   1.1   thorpej 	cp = sc->sc_tbuf;
    446   1.1   thorpej 	for (m = m0; m != NULL; ) {
    447  1.55   reinoud 		memcpy(cp, mtod(m, u_char *), m->m_len);
    448   1.1   thorpej 		cp += m->m_len;
    449  1.92  christos 		m = m0 = m_free(m);
    450   1.1   thorpej 	}
    451   1.1   thorpej 	if (len < SEMINSIZE) {
    452   1.1   thorpej #ifdef SEDEBUG
    453   1.1   thorpej 		if (sc->sc_debug)
    454  1.82  jakllsch 			printf("se: packet size %d (%zu) < %d\n", len,
    455  1.55   reinoud 			    cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
    456   1.1   thorpej #endif
    457  1.35   thorpej 		memset(cp, 0, SEMINSIZE - len);
    458   1.1   thorpej 		len = SEMINSIZE;
    459   1.1   thorpej 	}
    460   1.1   thorpej 
    461   1.1   thorpej 	/* Fill out SCSI command. */
    462   1.1   thorpej 	PROTOCMD(ctron_ether_send, send_cmd);
    463   1.1   thorpej 	_lto2b(len, send_cmd.length);
    464   1.1   thorpej 
    465   1.1   thorpej 	/* Send command to device. */
    466  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    467  1.50   mycroft 	    (void *)&send_cmd, sizeof(send_cmd),
    468  1.10     enami 	    sc->sc_tbuf, len, SERETRIES,
    469  1.26  matthias 	    SETIMEOUT, NULL, XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_OUT);
    470   1.1   thorpej 	if (error) {
    471  1.85       chs 		aprint_error_dev(sc->sc_dev, "not queued, error %d\n", error);
    472   1.1   thorpej 		ifp->if_oerrors++;
    473   1.1   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    474   1.1   thorpej 	} else
    475   1.1   thorpej 		ifp->if_opackets++;
    476   1.1   thorpej 	if (sc->sc_flags & SE_NEED_RECV) {
    477   1.1   thorpej 		sc->sc_flags &= ~SE_NEED_RECV;
    478   1.1   thorpej 		se_recv((void *) sc);
    479   1.1   thorpej 	}
    480   1.1   thorpej }
    481   1.1   thorpej 
    482   1.1   thorpej 
    483   1.1   thorpej /*
    484   1.1   thorpej  * Called from the scsibus layer via our scsi device switch.
    485   1.1   thorpej  */
    486   1.4   mycroft static void
    487  1.75       dsl sedone(struct scsipi_xfer *xs, int error)
    488   1.1   thorpej {
    489  1.85       chs 	struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
    490   1.9    bouyer 	struct scsipi_generic *cmd = xs->cmd;
    491   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    492   1.4   mycroft 	int s;
    493   1.4   mycroft 
    494   1.4   mycroft 	s = splnet();
    495   1.4   mycroft 	if(IS_SEND(cmd)) {
    496   1.4   mycroft 		if (xs->error == XS_BUSY) {
    497   1.4   mycroft 			printf("se: busy, retry txmit\n");
    498  1.28   thorpej 			callout_reset(&sc->sc_ifstart_ch, hz,
    499  1.28   thorpej 			    se_delayed_ifstart, ifp);
    500   1.4   mycroft 		} else {
    501   1.4   mycroft 			ifp->if_flags &= ~IFF_OACTIVE;
    502   1.9    bouyer 			/* the generic scsipi_done will call
    503   1.9    bouyer 			 * sestart (through scsipi_free_xs).
    504   1.4   mycroft 			 */
    505   1.4   mycroft 		}
    506   1.4   mycroft 	} else if(IS_RECV(cmd)) {
    507   1.4   mycroft 		/* RECV complete */
    508   1.4   mycroft 		/* pass data up. reschedule a recv */
    509   1.9    bouyer 		/* scsipi_free_xs will call start. Harmless. */
    510   1.4   mycroft 		if (error) {
    511   1.4   mycroft 			/* Reschedule after a delay */
    512  1.28   thorpej 			callout_reset(&sc->sc_recv_ch, se_poll,
    513  1.28   thorpej 			    se_recv, (void *)sc);
    514   1.4   mycroft 		} else {
    515  1.11  matthias 			int n, ntimeo;
    516   1.4   mycroft 			n = se_read(sc, xs->data, xs->datalen - xs->resid);
    517  1.11  matthias 			if (n > se_max_received)
    518  1.11  matthias 				se_max_received = n;
    519  1.11  matthias 			if (n == 0)
    520  1.11  matthias 				ntimeo = se_poll;
    521  1.11  matthias 			else if (n >= RDATA_MAX)
    522  1.11  matthias 				ntimeo = se_poll0;
    523  1.11  matthias 			else {
    524  1.11  matthias 				ntimeo = sc->sc_last_timeout;
    525  1.11  matthias 				ntimeo = (ntimeo * RDATA_GOAL)/n;
    526  1.11  matthias 				ntimeo = (ntimeo < se_poll0?
    527  1.11  matthias 					  se_poll0: ntimeo);
    528  1.11  matthias 				ntimeo = (ntimeo > se_poll?
    529  1.11  matthias 					  se_poll: ntimeo);
    530   1.1   thorpej 			}
    531  1.11  matthias 			sc->sc_last_timeout = ntimeo;
    532  1.11  matthias 			if (ntimeo == se_poll0  &&
    533  1.33   thorpej 			    IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    534  1.11  matthias 				/* Output is pending. Do next recv
    535  1.11  matthias 				 * after the next send.  */
    536  1.11  matthias 				sc->sc_flags |= SE_NEED_RECV;
    537  1.11  matthias 			else {
    538  1.28   thorpej 				callout_reset(&sc->sc_recv_ch, ntimeo,
    539  1.28   thorpej 				    se_recv, (void *)sc);
    540  1.11  matthias   			}
    541   1.1   thorpej 		}
    542   1.1   thorpej 	}
    543   1.4   mycroft 	splx(s);
    544   1.1   thorpej }
    545  1.10     enami 
    546   1.5  matthias static void
    547  1.75       dsl se_recv(void *v)
    548   1.1   thorpej {
    549   1.1   thorpej 	/* do a recv command */
    550   1.1   thorpej 	struct se_softc *sc = (struct se_softc *) v;
    551   1.1   thorpej 	struct scsi_ctron_ether_recv recv_cmd;
    552   1.1   thorpej 	int error;
    553   1.1   thorpej 
    554  1.21   thorpej 	if (sc->sc_enabled == 0)
    555  1.21   thorpej 		return;
    556  1.21   thorpej 
    557   1.1   thorpej 	PROTOCMD(ctron_ether_recv, recv_cmd);
    558   1.1   thorpej 
    559  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    560  1.50   mycroft 	    (void *)&recv_cmd, sizeof(recv_cmd),
    561  1.10     enami 	    sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
    562  1.26  matthias 	    XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_DATA_IN);
    563   1.1   thorpej 	if (error)
    564  1.28   thorpej 		callout_reset(&sc->sc_recv_ch, se_poll, se_recv, (void *)sc);
    565   1.1   thorpej }
    566   1.1   thorpej 
    567   1.1   thorpej /*
    568   1.1   thorpej  * We copy the data into mbufs.  When full cluster sized units are present
    569   1.1   thorpej  * we copy into clusters.
    570   1.1   thorpej  */
    571   1.1   thorpej static struct mbuf *
    572  1.75       dsl se_get(struct se_softc *sc, char *data, int totlen)
    573   1.1   thorpej {
    574   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    575  1.23   mycroft 	struct mbuf *m, *m0, *newm;
    576  1.23   mycroft 	int len;
    577   1.1   thorpej 
    578  1.23   mycroft 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    579  1.23   mycroft 	if (m0 == 0)
    580   1.1   thorpej 		return (0);
    581  1.90     ozaki 	m_set_rcvif(m0, ifp);
    582  1.23   mycroft 	m0->m_pkthdr.len = totlen;
    583  1.23   mycroft 	len = MHLEN;
    584  1.23   mycroft 	m = m0;
    585   1.1   thorpej 
    586   1.1   thorpej 	while (totlen > 0) {
    587   1.6   mycroft 		if (totlen >= MINCLSIZE) {
    588   1.1   thorpej 			MCLGET(m, M_DONTWAIT);
    589  1.23   mycroft 			if ((m->m_flags & M_EXT) == 0)
    590  1.23   mycroft 				goto bad;
    591   1.6   mycroft 			len = MCLBYTES;
    592   1.1   thorpej 		}
    593  1.23   mycroft 
    594  1.23   mycroft 		if (m == m0) {
    595  1.65      yamt 			char *newdata = (char *)
    596  1.23   mycroft 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    597  1.23   mycroft 			    sizeof(struct ether_header);
    598  1.23   mycroft 			len -= newdata - m->m_data;
    599  1.23   mycroft 			m->m_data = newdata;
    600  1.23   mycroft 		}
    601  1.23   mycroft 
    602   1.1   thorpej 		m->m_len = len = min(totlen, len);
    603  1.64  christos 		memcpy(mtod(m, void *), data, len);
    604   1.1   thorpej 		data += len;
    605  1.23   mycroft 
    606   1.1   thorpej 		totlen -= len;
    607  1.23   mycroft 		if (totlen > 0) {
    608  1.23   mycroft 			MGET(newm, M_DONTWAIT, MT_DATA);
    609  1.23   mycroft 			if (newm == 0)
    610  1.23   mycroft 				goto bad;
    611  1.23   mycroft 			len = MLEN;
    612  1.23   mycroft 			m = m->m_next = newm;
    613  1.23   mycroft 		}
    614   1.1   thorpej 	}
    615   1.1   thorpej 
    616  1.23   mycroft 	return (m0);
    617  1.23   mycroft 
    618  1.23   mycroft bad:
    619  1.23   mycroft 	m_freem(m0);
    620  1.23   mycroft 	return (0);
    621   1.1   thorpej }
    622   1.1   thorpej 
    623   1.1   thorpej /*
    624   1.1   thorpej  * Pass packets to higher levels.
    625   1.1   thorpej  */
    626   1.1   thorpej static int
    627  1.75       dsl se_read(struct se_softc *sc, char *data, int datalen)
    628   1.1   thorpej {
    629   1.1   thorpej 	struct mbuf *m;
    630   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    631   1.1   thorpej 	int n;
    632   1.1   thorpej 
    633   1.1   thorpej 	n = 0;
    634   1.1   thorpej 	while (datalen >= 2) {
    635   1.1   thorpej 		int len = _2btol(data);
    636   1.1   thorpej 		data += 2;
    637   1.1   thorpej 		datalen -= 2;
    638   1.1   thorpej 
    639   1.1   thorpej 		if (len == 0)
    640   1.1   thorpej 			break;
    641   1.1   thorpej #ifdef SEDEBUG
    642   1.1   thorpej 		if (sc->sc_debug) {
    643   1.1   thorpej 			printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
    644   1.1   thorpej 			 ntohs(((struct ether_header *)data)->ether_type));
    645   1.1   thorpej 		}
    646   1.1   thorpej #endif
    647   1.1   thorpej 		if (len <= sizeof(struct ether_header) ||
    648   1.1   thorpej 		    len > MAX_SNAP) {
    649   1.1   thorpej #ifdef SEDEBUG
    650   1.1   thorpej 			printf("%s: invalid packet size %d; dropping\n",
    651  1.85       chs 			       device_xname(sc->sc_dev), len);
    652   1.1   thorpej #endif
    653   1.1   thorpej 			ifp->if_ierrors++;
    654   1.1   thorpej 			goto next_packet;
    655   1.1   thorpej 		}
    656   1.1   thorpej 
    657   1.1   thorpej 		/* Don't need crc. Must keep ether header for BPF */
    658   1.1   thorpej 		m = se_get(sc, data, len - ETHER_CRC);
    659   1.1   thorpej 		if (m == 0) {
    660   1.1   thorpej #ifdef SEDEBUG
    661  1.10     enami 			if (sc->sc_debug)
    662   1.1   thorpej 				printf("se_read: se_get returned null\n");
    663   1.1   thorpej #endif
    664   1.1   thorpej 			ifp->if_ierrors++;
    665   1.1   thorpej 			goto next_packet;
    666   1.1   thorpej 		}
    667   1.1   thorpej 		if ((ifp->if_flags & IFF_PROMISC) != 0) {
    668   1.5  matthias 			m_adj(m, SE_PREFIX);
    669   1.1   thorpej 		}
    670   1.1   thorpej 
    671  1.24   thorpej 		/* Pass the packet up. */
    672  1.89     ozaki 		if_input(ifp, m);
    673   1.1   thorpej 
    674   1.1   thorpej 	next_packet:
    675   1.1   thorpej 		data += len;
    676   1.1   thorpej 		datalen -= len;
    677   1.1   thorpej 		n++;
    678   1.1   thorpej 	}
    679  1.10     enami 	return (n);
    680   1.1   thorpej }
    681   1.1   thorpej 
    682   1.1   thorpej 
    683   1.5  matthias static void
    684  1.75       dsl sewatchdog(struct ifnet *ifp)
    685   1.1   thorpej {
    686   1.1   thorpej 	struct se_softc *sc = ifp->if_softc;
    687   1.1   thorpej 
    688  1.85       chs 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    689   1.1   thorpej 	++ifp->if_oerrors;
    690   1.1   thorpej 
    691   1.1   thorpej 	se_reset(sc);
    692   1.1   thorpej }
    693   1.1   thorpej 
    694   1.1   thorpej static int
    695  1.75       dsl se_reset(struct se_softc *sc)
    696   1.1   thorpej {
    697  1.10     enami 	int error;
    698   1.1   thorpej 	int s = splnet();
    699   1.1   thorpej #if 0
    700   1.1   thorpej 	/* Maybe we don't *really* want to reset the entire bus
    701   1.1   thorpej 	 * because the ctron isn't working. We would like to send a
    702   1.1   thorpej 	 * "BUS DEVICE RESET" message, but don't think the ctron
    703   1.1   thorpej 	 * understands it.
    704   1.1   thorpej 	 */
    705  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
    706  1.25   thorpej 	    XS_CTL_RESET);
    707   1.1   thorpej #endif
    708   1.1   thorpej 	error = se_init(sc);
    709   1.1   thorpej 	splx(s);
    710  1.10     enami 	return (error);
    711   1.1   thorpej }
    712   1.1   thorpej 
    713   1.5  matthias static int
    714  1.75       dsl se_add_proto(struct se_softc *sc, int proto)
    715   1.1   thorpej {
    716  1.10     enami 	int error;
    717   1.1   thorpej 	struct scsi_ctron_ether_generic add_proto_cmd;
    718  1.55   reinoud 	u_int8_t data[2];
    719   1.1   thorpej 	_lto2b(proto, data);
    720   1.1   thorpej #ifdef SEDEBUG
    721   1.1   thorpej 	if (sc->sc_debug)
    722   1.1   thorpej 		printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
    723   1.1   thorpej #endif
    724   1.1   thorpej 
    725   1.1   thorpej 	PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
    726   1.1   thorpej 	_lto2b(sizeof(data), add_proto_cmd.length);
    727  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    728  1.50   mycroft 	    (void *)&add_proto_cmd, sizeof(add_proto_cmd),
    729  1.30     enami 	    data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
    730  1.79     rmind 	    XS_CTL_DATA_OUT);
    731  1.10     enami 	return (error);
    732   1.1   thorpej }
    733   1.1   thorpej 
    734   1.5  matthias static int
    735  1.75       dsl se_get_addr(struct se_softc *sc, u_int8_t *myaddr)
    736   1.1   thorpej {
    737  1.10     enami 	int error;
    738   1.1   thorpej 	struct scsi_ctron_ether_generic get_addr_cmd;
    739   1.1   thorpej 
    740   1.1   thorpej 	PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
    741   1.1   thorpej 	_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
    742  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    743  1.50   mycroft 	    (void *)&get_addr_cmd, sizeof(get_addr_cmd),
    744  1.30     enami 	    myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    745  1.79     rmind 	    XS_CTL_DATA_IN);
    746  1.85       chs 	printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
    747   1.1   thorpej 	    ether_sprintf(myaddr));
    748  1.10     enami 	return (error);
    749   1.1   thorpej }
    750   1.1   thorpej 
    751   1.1   thorpej 
    752   1.5  matthias static int
    753  1.75       dsl se_set_media(struct se_softc *sc, int type)
    754   1.1   thorpej {
    755  1.10     enami 	int error;
    756   1.1   thorpej 	struct scsi_ctron_ether_generic set_media_cmd;
    757   1.1   thorpej 
    758   1.1   thorpej 	PROTOCMD(ctron_ether_set_media, set_media_cmd);
    759   1.1   thorpej 	set_media_cmd.byte3 = type;
    760  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    761  1.50   mycroft 	    (void *)&set_media_cmd, sizeof(set_media_cmd),
    762  1.10     enami 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    763  1.10     enami 	return (error);
    764   1.1   thorpej }
    765   1.1   thorpej 
    766   1.5  matthias static int
    767  1.75       dsl se_set_mode(struct se_softc *sc, int len, int mode)
    768   1.1   thorpej {
    769  1.10     enami 	int error;
    770   1.1   thorpej 	struct scsi_ctron_ether_set_mode set_mode_cmd;
    771   1.1   thorpej 
    772   1.1   thorpej 	PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
    773   1.1   thorpej 	set_mode_cmd.mode = mode;
    774   1.1   thorpej 	_lto2b(len, set_mode_cmd.length);
    775  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    776  1.50   mycroft 	    (void *)&set_mode_cmd, sizeof(set_mode_cmd),
    777  1.10     enami 	    0, 0, SERETRIES, SETIMEOUT, NULL, 0);
    778  1.10     enami 	return (error);
    779   1.1   thorpej }
    780   1.1   thorpej 
    781   1.1   thorpej 
    782   1.5  matthias static int
    783  1.75       dsl se_init(struct se_softc *sc)
    784   1.1   thorpej {
    785   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    786   1.1   thorpej 	struct scsi_ctron_ether_generic set_addr_cmd;
    787  1.70    dyoung 	uint8_t enaddr[ETHER_ADDR_LEN];
    788   1.1   thorpej 	int error;
    789   1.1   thorpej 
    790   1.1   thorpej 	if (ifp->if_flags & IFF_PROMISC) {
    791   1.1   thorpej 		error = se_set_mode(sc, MAX_SNAP, 1);
    792   1.1   thorpej 	}
    793   1.1   thorpej 	else
    794   1.1   thorpej 		error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
    795  1.10     enami 		    0);
    796   1.5  matthias 	if (error != 0)
    797  1.10     enami 		return (error);
    798   1.5  matthias 
    799   1.1   thorpej 	PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
    800   1.1   thorpej 	_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
    801  1.70    dyoung 	memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
    802  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    803  1.50   mycroft 	    (void *)&set_addr_cmd, sizeof(set_addr_cmd),
    804  1.70    dyoung 	    enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
    805  1.25   thorpej 	    XS_CTL_DATA_OUT);
    806  1.10     enami 	if (error != 0)
    807  1.10     enami 		return (error);
    808   1.1   thorpej 
    809   1.1   thorpej 	if ((sc->protos & PROTO_IP) &&
    810   1.1   thorpej 	    (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
    811  1.10     enami 		return (error);
    812   1.1   thorpej 	if ((sc->protos & PROTO_ARP) &&
    813   1.1   thorpej 	    (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
    814  1.10     enami 		return (error);
    815   1.1   thorpej 	if ((sc->protos & PROTO_REVARP) &&
    816   1.1   thorpej 	    (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
    817  1.10     enami 		return (error);
    818   1.5  matthias #ifdef NETATALK
    819   1.5  matthias 	if ((sc->protos & PROTO_AT) &&
    820  1.20       kim 	    (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
    821  1.10     enami 		return (error);
    822   1.5  matthias 	if ((sc->protos & PROTO_AARP) &&
    823   1.5  matthias 	    (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
    824  1.10     enami 		return (error);
    825   1.5  matthias #endif
    826   1.1   thorpej 
    827   1.5  matthias 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == IFF_UP) {
    828   1.1   thorpej 		ifp->if_flags |= IFF_RUNNING;
    829   1.1   thorpej 		se_recv(sc);
    830   1.1   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    831   1.1   thorpej 		se_ifstart(ifp);
    832   1.1   thorpej 	}
    833  1.10     enami 	return (error);
    834   1.1   thorpej }
    835   1.1   thorpej 
    836   1.5  matthias static int
    837  1.75       dsl se_set_multi(struct se_softc *sc, u_int8_t *addr)
    838   1.1   thorpej {
    839   1.1   thorpej 	struct scsi_ctron_ether_generic set_multi_cmd;
    840   1.1   thorpej 	int error;
    841   1.1   thorpej 
    842   1.1   thorpej 	if (sc->sc_debug)
    843  1.85       chs 		printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
    844   1.1   thorpej 		    ether_sprintf(addr));
    845   1.1   thorpej 
    846   1.1   thorpej 	PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
    847   1.1   thorpej 	_lto2b(sizeof(addr), set_multi_cmd.length);
    848  1.69    dyoung 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    849  1.69    dyoung 	 * is too small? --dyoung
    850  1.69    dyoung 	 */
    851  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    852  1.50   mycroft 	    (void *)&set_multi_cmd, sizeof(set_multi_cmd),
    853  1.25   thorpej 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    854  1.10     enami 	return (error);
    855   1.1   thorpej }
    856   1.1   thorpej 
    857   1.5  matthias static int
    858  1.75       dsl se_remove_multi(struct se_softc *sc, u_int8_t *addr)
    859   1.1   thorpej {
    860   1.1   thorpej 	struct scsi_ctron_ether_generic remove_multi_cmd;
    861   1.1   thorpej 	int error;
    862   1.1   thorpej 
    863   1.1   thorpej 	if (sc->sc_debug)
    864  1.85       chs 		printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
    865   1.1   thorpej 		    ether_sprintf(addr));
    866   1.1   thorpej 
    867   1.1   thorpej 	PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
    868   1.1   thorpej 	_lto2b(sizeof(addr), remove_multi_cmd.length);
    869  1.69    dyoung 	/* XXX sizeof(addr) is the size of the pointer.  Surely it
    870  1.69    dyoung 	 * is too small? --dyoung
    871  1.69    dyoung 	 */
    872  1.34    bouyer 	error = se_scsipi_cmd(sc->sc_periph,
    873  1.50   mycroft 	    (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
    874  1.25   thorpej 	    addr, sizeof(addr), SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
    875  1.10     enami 	return (error);
    876   1.1   thorpej }
    877   1.1   thorpej 
    878   1.1   thorpej #if 0	/* not used  --thorpej */
    879   1.5  matthias static int
    880  1.75       dsl sc_set_all_multi(struct se_softc *sc, int set)
    881   1.1   thorpej {
    882   1.1   thorpej 	int error = 0;
    883  1.55   reinoud 	u_int8_t *addr;
    884   1.1   thorpej 	struct ethercom *ac = &sc->sc_ethercom;
    885   1.1   thorpej 	struct ether_multi *enm;
    886   1.1   thorpej 	struct ether_multistep step;
    887  1.10     enami 
    888   1.1   thorpej 	ETHER_FIRST_MULTI(step, ac, enm);
    889   1.1   thorpej 	while (enm != NULL) {
    890   1.1   thorpej 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    891   1.1   thorpej 			/*
    892   1.1   thorpej 			 * We must listen to a range of multicast addresses.
    893   1.1   thorpej 			 * For now, just accept all multicasts, rather than
    894   1.1   thorpej 			 * trying to set only those filter bits needed to match
    895   1.1   thorpej 			 * the range.  (At this time, the only use of address
    896   1.1   thorpej 			 * ranges is for IP multicast routing, for which the
    897   1.1   thorpej 			 * range is big enough to require all bits set.)
    898   1.1   thorpej 			 */
    899   1.1   thorpej 			/* We have no way of adding a range to this device.
    900   1.1   thorpej 			 * stepping through all addresses in the range is
    901   1.1   thorpej 			 * typically not possible. The only real alternative
    902   1.1   thorpej 			 * is to go into promicuous mode and filter by hand.
    903   1.1   thorpej 			 */
    904  1.10     enami 			return (ENODEV);
    905   1.1   thorpej 
    906   1.1   thorpej 		}
    907   1.1   thorpej 
    908   1.1   thorpej 		addr = enm->enm_addrlo;
    909  1.10     enami 		if ((error = set ? se_set_multi(sc, addr) :
    910  1.10     enami 		    se_remove_multi(sc, addr)) != 0)
    911  1.10     enami 			return (error);
    912   1.1   thorpej 		ETHER_NEXT_MULTI(step, enm);
    913   1.1   thorpej 	}
    914  1.10     enami 	return (error);
    915   1.1   thorpej }
    916   1.1   thorpej #endif /* not used */
    917   1.1   thorpej 
    918   1.5  matthias static void
    919  1.75       dsl se_stop(struct se_softc *sc)
    920   1.1   thorpej {
    921  1.10     enami 
    922   1.1   thorpej 	/* Don't schedule any reads */
    923  1.28   thorpej 	callout_stop(&sc->sc_recv_ch);
    924   1.5  matthias 
    925   1.1   thorpej 	/* How can we abort any scsi cmds in progress? */
    926   1.1   thorpej }
    927   1.1   thorpej 
    928   1.1   thorpej 
    929   1.1   thorpej /*
    930   1.1   thorpej  * Process an ioctl request.
    931   1.1   thorpej  */
    932   1.5  matthias static int
    933  1.73    dyoung se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    934   1.1   thorpej {
    935  1.29  augustss 	struct se_softc *sc = ifp->if_softc;
    936   1.1   thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    937   1.1   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    938  1.68    dyoung 	struct sockaddr *sa;
    939   1.1   thorpej 	int s, error = 0;
    940   1.1   thorpej 
    941   1.1   thorpej 	s = splnet();
    942   1.1   thorpej 
    943   1.1   thorpej 	switch (cmd) {
    944   1.1   thorpej 
    945  1.73    dyoung 	case SIOCINITIFADDR:
    946  1.21   thorpej 		if ((error = se_enable(sc)) != 0)
    947  1.21   thorpej 			break;
    948   1.1   thorpej 		ifp->if_flags |= IFF_UP;
    949   1.1   thorpej 
    950  1.83   mbalmer 		if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
    951  1.21   thorpej 			break;
    952   1.5  matthias 
    953   1.1   thorpej 		switch (ifa->ifa_addr->sa_family) {
    954   1.1   thorpej #ifdef INET
    955   1.1   thorpej 		case AF_INET:
    956   1.1   thorpej 			sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
    957   1.1   thorpej 			if ((error = se_init(sc)) != 0)
    958   1.1   thorpej 				break;
    959   1.1   thorpej 			arp_ifinit(ifp, ifa);
    960   1.1   thorpej 			break;
    961   1.1   thorpej #endif
    962   1.5  matthias #ifdef NETATALK
    963   1.5  matthias 		case AF_APPLETALK:
    964   1.5  matthias 			sc->protos |= (PROTO_AT | PROTO_AARP);
    965   1.5  matthias 			if ((error = se_init(sc)) != 0)
    966   1.5  matthias 				break;
    967   1.5  matthias 			break;
    968   1.5  matthias #endif
    969   1.1   thorpej 		default:
    970   1.1   thorpej 			error = se_init(sc);
    971   1.1   thorpej 			break;
    972   1.1   thorpej 		}
    973   1.1   thorpej 		break;
    974   1.1   thorpej 
    975   1.1   thorpej 
    976   1.1   thorpej 	case SIOCSIFFLAGS:
    977  1.73    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    978  1.73    dyoung 			break;
    979  1.73    dyoung 		/* XXX re-use ether_ioctl() */
    980  1.73    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    981  1.73    dyoung 		case IFF_RUNNING:
    982   1.1   thorpej 			/*
    983   1.1   thorpej 			 * If interface is marked down and it is running, then
    984   1.1   thorpej 			 * stop it.
    985   1.1   thorpej 			 */
    986   1.1   thorpej 			se_stop(sc);
    987   1.1   thorpej 			ifp->if_flags &= ~IFF_RUNNING;
    988  1.21   thorpej 			se_disable(sc);
    989  1.73    dyoung 			break;
    990  1.73    dyoung 		case IFF_UP:
    991   1.1   thorpej 			/*
    992   1.1   thorpej 			 * If interface is marked up and it is stopped, then
    993   1.1   thorpej 			 * start it.
    994   1.1   thorpej 			 */
    995  1.21   thorpej 			if ((error = se_enable(sc)) != 0)
    996  1.21   thorpej 				break;
    997   1.1   thorpej 			error = se_init(sc);
    998  1.73    dyoung 			break;
    999  1.73    dyoung 		default:
   1000   1.1   thorpej 			/*
   1001   1.1   thorpej 			 * Reset the interface to pick up changes in any other
   1002   1.1   thorpej 			 * flags that affect hardware registers.
   1003   1.1   thorpej 			 */
   1004  1.73    dyoung 			if (sc->sc_enabled)
   1005  1.73    dyoung 				error = se_init(sc);
   1006  1.73    dyoung 			break;
   1007   1.1   thorpej 		}
   1008   1.1   thorpej #ifdef SEDEBUG
   1009   1.1   thorpej 		if (ifp->if_flags & IFF_DEBUG)
   1010   1.1   thorpej 			sc->sc_debug = 1;
   1011   1.1   thorpej 		else
   1012   1.1   thorpej 			sc->sc_debug = 0;
   1013   1.1   thorpej #endif
   1014   1.1   thorpej 		break;
   1015   1.1   thorpej 
   1016   1.1   thorpej 	case SIOCADDMULTI:
   1017   1.1   thorpej 	case SIOCDELMULTI:
   1018  1.68    dyoung 		sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_NOWAIT);
   1019  1.68    dyoung 		if (sa == NULL) {
   1020  1.68    dyoung 			error = ENOBUFS;
   1021  1.68    dyoung 			break;
   1022  1.68    dyoung 		}
   1023  1.67    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1024  1.51   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
   1025  1.51   thorpej 				error = (cmd == SIOCADDMULTI) ?
   1026  1.68    dyoung 				   se_set_multi(sc, sa->sa_data) :
   1027  1.68    dyoung 				   se_remove_multi(sc, sa->sa_data);
   1028  1.51   thorpej 			} else
   1029  1.51   thorpej 				error = 0;
   1030  1.21   thorpej 		}
   1031  1.68    dyoung 		sockaddr_free(sa);
   1032   1.1   thorpej 		break;
   1033   1.1   thorpej 
   1034   1.1   thorpej 	default:
   1035   1.1   thorpej 
   1036  1.73    dyoung 		error = ether_ioctl(ifp, cmd, data);
   1037   1.1   thorpej 		break;
   1038   1.1   thorpej 	}
   1039   1.1   thorpej 
   1040   1.1   thorpej 	splx(s);
   1041   1.1   thorpej 	return (error);
   1042   1.1   thorpej }
   1043   1.1   thorpej 
   1044  1.21   thorpej /*
   1045  1.21   thorpej  * Enable the network interface.
   1046  1.21   thorpej  */
   1047  1.21   thorpej int
   1048  1.75       dsl se_enable(struct se_softc *sc)
   1049  1.21   thorpej {
   1050  1.34    bouyer 	struct scsipi_periph *periph = sc->sc_periph;
   1051  1.34    bouyer 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1052  1.21   thorpej 	int error = 0;
   1053  1.21   thorpej 
   1054  1.21   thorpej 	if (sc->sc_enabled == 0 &&
   1055  1.34    bouyer 	    (error = scsipi_adapter_addref(adapt)) == 0)
   1056  1.21   thorpej 		sc->sc_enabled = 1;
   1057  1.21   thorpej 	else
   1058  1.85       chs 		aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1059  1.21   thorpej 
   1060  1.21   thorpej 	return (error);
   1061  1.21   thorpej }
   1062  1.21   thorpej 
   1063  1.21   thorpej /*
   1064  1.21   thorpej  * Disable the network interface.
   1065  1.21   thorpej  */
   1066  1.21   thorpej void
   1067  1.75       dsl se_disable(struct se_softc *sc)
   1068  1.21   thorpej {
   1069  1.34    bouyer 	struct scsipi_periph *periph = sc->sc_periph;
   1070  1.34    bouyer 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1071  1.21   thorpej 
   1072  1.21   thorpej 	if (sc->sc_enabled != 0) {
   1073  1.34    bouyer 		scsipi_adapter_delref(adapt);
   1074  1.21   thorpej 		sc->sc_enabled = 0;
   1075  1.21   thorpej 	}
   1076  1.21   thorpej }
   1077  1.21   thorpej 
   1078   1.1   thorpej #define	SEUNIT(z)	(minor(z))
   1079   1.1   thorpej /*
   1080   1.1   thorpej  * open the device.
   1081   1.1   thorpej  */
   1082   1.1   thorpej int
   1083  1.76       dsl seopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1084   1.1   thorpej {
   1085  1.21   thorpej 	int unit, error;
   1086   1.1   thorpej 	struct se_softc *sc;
   1087  1.34    bouyer 	struct scsipi_periph *periph;
   1088  1.34    bouyer 	struct scsipi_adapter *adapt;
   1089   1.1   thorpej 
   1090   1.1   thorpej 	unit = SEUNIT(dev);
   1091  1.72   tsutsui 	sc = device_lookup_private(&se_cd, unit);
   1092  1.10     enami 	if (sc == NULL)
   1093  1.10     enami 		return (ENXIO);
   1094   1.5  matthias 
   1095  1.34    bouyer 	periph = sc->sc_periph;
   1096  1.34    bouyer 	adapt = periph->periph_channel->chan_adapter;
   1097   1.1   thorpej 
   1098  1.34    bouyer 	if ((error = scsipi_adapter_addref(adapt)) != 0)
   1099  1.21   thorpej 		return (error);
   1100  1.21   thorpej 
   1101  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB1,
   1102  1.74    cegger 	    ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
   1103  1.10     enami 	    se_cd.cd_ndevs));
   1104   1.1   thorpej 
   1105  1.34    bouyer 	periph->periph_flags |= PERIPH_OPEN;
   1106   1.1   thorpej 
   1107  1.34    bouyer 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
   1108  1.10     enami 	return (0);
   1109   1.1   thorpej }
   1110   1.1   thorpej 
   1111   1.1   thorpej /*
   1112   1.1   thorpej  * close the device.. only called if we are the LAST
   1113   1.1   thorpej  * occurence of an open device
   1114   1.1   thorpej  */
   1115   1.1   thorpej int
   1116  1.76       dsl seclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1117   1.1   thorpej {
   1118  1.72   tsutsui 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1119  1.34    bouyer 	struct scsipi_periph *periph = sc->sc_periph;
   1120  1.34    bouyer 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
   1121   1.1   thorpej 
   1122  1.34    bouyer 	SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
   1123  1.22   thorpej 
   1124  1.34    bouyer 	scsipi_wait_drain(periph);
   1125  1.22   thorpej 
   1126  1.34    bouyer 	scsipi_adapter_delref(adapt);
   1127  1.34    bouyer 	periph->periph_flags &= ~PERIPH_OPEN;
   1128   1.1   thorpej 
   1129  1.10     enami 	return (0);
   1130   1.1   thorpej }
   1131   1.1   thorpej 
   1132   1.1   thorpej /*
   1133   1.1   thorpej  * Perform special action on behalf of the user
   1134   1.1   thorpej  * Only does generic scsi ioctls.
   1135   1.1   thorpej  */
   1136   1.1   thorpej int
   1137  1.75       dsl seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1138   1.1   thorpej {
   1139  1.72   tsutsui 	struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
   1140   1.1   thorpej 
   1141  1.59  christos 	return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
   1142   1.1   thorpej }
   1143