#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_se.c,v 1.119 2023/12/20 18:09:19 skrll Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_atalk.h"
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/buf.h>
#include <sys/callout.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/workqueue.h>
#include <dev/scsipi/scsi_ctron_ether.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsipi_all.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#ifdef INET
#include <netinet/if_inarp.h>
#include <netinet/in.h>
#endif
#ifdef NETATALK
#include <netatalk/at.h>
#endif
#define SETIMEOUT 1000
#define SEOUTSTANDING 4
#define SERETRIES 4
#define SE_PREFIX 4
#define ETHER_CRC 4
#define SEMINSIZE 60
#define MAX_SNAP (ETHERMTU + sizeof(struct ether_header) + \
SE_PREFIX + ETHER_CRC)
#define RBUF_LEN (16 * 1024)
#define RDATA_MAX 10
#define RDATA_GOAL 8
#define SE_POLL 40
#define SE_POLL0 10
int se_poll = 0;
int se_poll0 = 0;
#ifdef SE_DEBUG
int se_max_received = 0;
#endif
#define PROTOCMD(p, d) \
((d) = (p))
#define PROTOCMD_DECL(name) \
static const struct scsi_ctron_ether_generic name
#define PROTOCMD_DECL_SPECIAL(name) \
static const struct __CONCAT(scsi_, name) name
PROTOCMD_DECL(ctron_ether_send) = {CTRON_ETHER_SEND, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_add_proto) = {CTRON_ETHER_ADD_PROTO, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_get_addr) = {CTRON_ETHER_GET_ADDR, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_set_media) = {CTRON_ETHER_SET_MEDIA, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_set_addr) = {CTRON_ETHER_SET_ADDR, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_set_multi) = {CTRON_ETHER_SET_MULTI, 0, {0,0}, 0};
PROTOCMD_DECL(ctron_ether_remove_multi) =
{CTRON_ETHER_REMOVE_MULTI, 0, {0,0}, 0};
PROTOCMD_DECL_SPECIAL(ctron_ether_recv) = {CTRON_ETHER_RECV};
PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode) =
{CTRON_ETHER_SET_MODE, 0, {0,0}, 0};
struct se_softc {
device_t sc_dev;
struct ethercom sc_ethercom;
struct scsipi_periph *sc_periph;
struct callout sc_recv_ch;
struct kmutex sc_iflock;
struct if_percpuq *sc_ipq;
struct workqueue *sc_recv_wq, *sc_send_wq;
struct work sc_recv_work, sc_send_work;
int sc_recv_work_pending, sc_send_work_pending;
char *sc_tbuf;
char *sc_rbuf;
int protos;
#define PROTO_IP 0x01
#define PROTO_ARP 0x02
#define PROTO_REVARP 0x04
#define PROTO_AT 0x08
#define PROTO_AARP 0x10
int sc_debug;
int sc_flags;
int sc_last_timeout;
int sc_enabled;
int sc_attach_state;
};
static int sematch(device_t, cfdata_t, void *);
static void seattach(device_t, device_t, void *);
static int sedetach(device_t, int);
static void se_ifstart(struct ifnet *);
static void sedone(struct scsipi_xfer *, int);
static int se_ioctl(struct ifnet *, u_long, void *);
#if 0
static void sewatchdog(struct ifnet *);
#endif
#if 0
static inline uint16_t ether_cmp(void *, void *);
#endif
static void se_recv_callout(void *);
static void se_recv_worker(struct work *wk, void *cookie);
static void se_recv(struct se_softc *);
static struct mbuf *se_get(struct se_softc *, char *, int);
static int se_read(struct se_softc *, char *, int);
#if 0
static void se_reset(struct se_softc *);
#endif
static int se_add_proto(struct se_softc *, int);
static int se_get_addr(struct se_softc *, uint8_t *);
static int se_set_media(struct se_softc *, int);
static int se_init(struct se_softc *);
static int se_set_multi(struct se_softc *, uint8_t *);
static int se_remove_multi(struct se_softc *, uint8_t *);
#if 0
static int sc_set_all_multi(struct se_softc *, int);
#endif
static void se_stop(struct se_softc *);
static inline int se_scsipi_cmd(struct scsipi_periph *periph,
struct scsipi_generic *scsipi_cmd,
int cmdlen, u_char *data_addr, int datalen,
int retries, int timeout, struct buf *bp,
int flags);
static void se_send_worker(struct work *wk, void *cookie);
static int se_set_mode(struct se_softc *, int, int);
int se_enable(struct se_softc *);
void se_disable(struct se_softc *);
CFATTACH_DECL_NEW(se, sizeof(struct se_softc),
sematch, seattach, sedetach, NULL);
extern struct cfdriver se_cd;
dev_type_open(seopen);
dev_type_close(seclose);
dev_type_ioctl(seioctl);
const struct cdevsw se_cdevsw = {
.d_open = seopen,
.d_close = seclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = seioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER | D_MPSAFE
};
const struct scsipi_periphsw se_switch = {
NULL,
NULL,
NULL,
sedone,
};
const struct scsipi_inquiry_pattern se_patterns[] = {
{T_PROCESSOR, T_FIXED,
"CABLETRN", "EA412", ""},
{T_PROCESSOR, T_FIXED,
"Cabletrn", "EA412", ""},
};
#if 0
static inline uint16_t
ether_cmp(void *one, void *two)
{
uint16_t *a = (uint16_t *) one;
uint16_t *b = (uint16_t *) two;
uint16_t diff;
diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
return (diff);
}
#define ETHER_CMP ether_cmp
#endif
static int
sematch(device_t parent, cfdata_t match, void *aux)
{
struct scsipibus_attach_args *sa = aux;
int priority;
(void)scsipi_inqmatch(&sa->sa_inqbuf,
se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
sizeof(se_patterns[0]), &priority);
return (priority);
}
static void
seattach(device_t parent, device_t self, void *aux)
{
struct se_softc *sc = device_private(self);
struct scsipibus_attach_args *sa = aux;
struct scsipi_periph *periph = sa->sa_periph;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
uint8_t myaddr[ETHER_ADDR_LEN];
char wqname[MAXCOMLEN];
int rv;
sc->sc_dev = self;
printf("\n");
SC_DEBUG(periph, SCSIPI_DB2, ("seattach: "));
sc->sc_attach_state = 0;
callout_init(&sc->sc_recv_ch, CALLOUT_MPSAFE);
callout_setfunc(&sc->sc_recv_ch, se_recv_callout, (void *)sc);
mutex_init(&sc->sc_iflock, MUTEX_DEFAULT, IPL_SOFTNET);
sc->sc_periph = periph;
periph->periph_dev = sc->sc_dev;
periph->periph_switch = &se_switch;
se_poll = (SE_POLL * hz) / 1000;
se_poll = se_poll? se_poll: 1;
se_poll0 = (SE_POLL0 * hz) / 1000;
se_poll0 = se_poll0? se_poll0: 1;
sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
M_DEVBUF, M_WAITOK);
sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_WAITOK);
strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
ifp->if_softc = sc;
ifp->if_start = se_ifstart;
ifp->if_ioctl = se_ioctl;
ifp->if_watchdog = NULL;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_extflags = IFEF_MPSAFE;
IFQ_SET_READY(&ifp->if_snd);
se_get_addr(sc, myaddr);
sc->sc_attach_state = 1;
if_initialize(ifp);
snprintf(wqname, sizeof(wqname), "%sRx", device_xname(sc->sc_dev));
rv = workqueue_create(&sc->sc_recv_wq, wqname, se_recv_worker, sc,
PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
if (rv != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create recv Rx workqueue\n");
sedetach(sc->sc_dev, 0);
return;
}
sc->sc_recv_work_pending = false;
sc->sc_attach_state = 2;
snprintf(wqname, sizeof(wqname), "%sTx", device_xname(sc->sc_dev));
rv = workqueue_create(&sc->sc_send_wq, wqname, se_send_worker, ifp,
PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
if (rv != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create send Tx workqueue\n");
sedetach(sc->sc_dev, 0);
return;
}
sc->sc_send_work_pending = false;
sc->sc_attach_state = 3;
sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
ether_ifattach(ifp, myaddr);
if_register(ifp);
sc->sc_attach_state = 4;
}
static int
sedetach(device_t self, int flags)
{
struct se_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
switch(sc->sc_attach_state) {
case 4:
se_stop(sc);
mutex_enter(&sc->sc_iflock);
ifp->if_flags &= ~IFF_RUNNING;
se_disable(sc);
ether_ifdetach(ifp);
if_detach(ifp);
mutex_exit(&sc->sc_iflock);
if_percpuq_destroy(sc->sc_ipq);
case 3:
workqueue_destroy(sc->sc_send_wq);
case 2:
workqueue_destroy(sc->sc_recv_wq);
case 1:
free(sc->sc_rbuf, M_DEVBUF);
free(sc->sc_tbuf, M_DEVBUF);
callout_destroy(&sc->sc_recv_ch);
mutex_destroy(&sc->sc_iflock);
break;
default:
aprint_error_dev(sc->sc_dev, "detach failed (state %d)\n",
sc->sc_attach_state);
return 1;
break;
}
return 0;
}
static inline int
se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
struct buf *bp, int flags)
{
int error;
error = scsipi_command(periph, cmd, cmdlen, data_addr,
datalen, retries, timeout, bp, flags);
return (error);
}
static void
se_ifstart(struct ifnet *ifp)
{
struct se_softc *sc = ifp->if_softc;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_send_work_pending) {
sc->sc_send_work_pending = true;
workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
}
mutex_exit(&sc->sc_iflock);
}
static void
se_send_worker(struct work *wk, void *cookie)
{
struct ifnet *ifp = cookie;
struct se_softc *sc = ifp->if_softc;
struct scsi_ctron_ether_generic send_cmd;
struct mbuf *m, *m0;
int len, error;
u_char *cp;
mutex_enter(&sc->sc_iflock);
sc->sc_send_work_pending = false;
mutex_exit(&sc->sc_iflock);
KASSERT(if_is_mpsafe(ifp));
if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
return;
while (1) {
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (m0 == 0)
break;
bpf_mtap(ifp, m0, BPF_D_OUT);
if ((m0->m_flags & M_PKTHDR) == 0)
panic("ctscstart: no header mbuf");
len = m0->m_pkthdr.len;
ifp->if_flags |= IFF_OACTIVE;
cp = sc->sc_tbuf;
for (m = m0; m != NULL; ) {
memcpy(cp, mtod(m, u_char *), m->m_len);
cp += m->m_len;
m = m0 = m_free(m);
}
if (len < SEMINSIZE) {
#ifdef SEDEBUG
if (sc->sc_debug)
printf("se: packet size %d (%zu) < %d\n", len,
cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
#endif
memset(cp, 0, SEMINSIZE - len);
len = SEMINSIZE;
}
PROTOCMD(ctron_ether_send, send_cmd);
_lto2b(len, send_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&send_cmd, sizeof(send_cmd),
sc->sc_tbuf, len, SERETRIES,
SETIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_DATA_OUT);
if (error) {
aprint_error_dev(sc->sc_dev,
"not queued, error %d\n", error);
if_statinc(ifp, if_oerrors);
ifp->if_flags &= ~IFF_OACTIVE;
} else
if_statinc(ifp, if_opackets);
}
}
static void
sedone(struct scsipi_xfer *xs, int error)
{
struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
struct scsipi_generic *cmd = xs->cmd;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
if (IS_SEND(cmd)) {
ifp->if_flags &= ~IFF_OACTIVE;
} else if (IS_RECV(cmd)) {
if (error) {
callout_schedule(&sc->sc_recv_ch, se_poll);
} else {
int n, ntimeo;
n = se_read(sc, xs->data, xs->datalen - xs->resid);
#ifdef SE_DEBUG
if (n > se_max_received)
se_max_received = n;
#endif
if (n == 0)
ntimeo = se_poll;
else if (n >= RDATA_MAX)
ntimeo = se_poll0;
else {
ntimeo = sc->sc_last_timeout;
ntimeo = (ntimeo * RDATA_GOAL)/n;
ntimeo = (ntimeo < se_poll0?
se_poll0: ntimeo);
ntimeo = (ntimeo > se_poll?
se_poll: ntimeo);
}
sc->sc_last_timeout = ntimeo;
callout_schedule(&sc->sc_recv_ch, ntimeo);
}
}
}
static void
se_recv_callout(void *v)
{
struct se_softc *sc = (struct se_softc *) v;
if (sc->sc_enabled == 0)
return;
mutex_enter(&sc->sc_iflock);
if (sc->sc_recv_work_pending == true) {
callout_schedule(&sc->sc_recv_ch, se_poll);
mutex_exit(&sc->sc_iflock);
return;
}
sc->sc_recv_work_pending = true;
workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
mutex_exit(&sc->sc_iflock);
}
static void
se_recv_worker(struct work *wk, void *cookie)
{
struct se_softc *sc = (struct se_softc *) cookie;
mutex_enter(&sc->sc_iflock);
sc->sc_recv_work_pending = false;
mutex_exit(&sc->sc_iflock);
se_recv(sc);
}
static void
se_recv(struct se_softc *sc)
{
struct scsi_ctron_ether_recv recv_cmd;
int error;
PROTOCMD(ctron_ether_recv, recv_cmd);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&recv_cmd, sizeof(recv_cmd),
sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
XS_CTL_NOSLEEP | XS_CTL_DATA_IN);
if (error)
callout_schedule(&sc->sc_recv_ch, se_poll);
}
static struct mbuf *
se_get(struct se_softc *sc, char *data, int totlen)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mbuf *m, *m0, *newm;
int len;
MGETHDR(m0, M_DONTWAIT, MT_DATA);
if (m0 == 0)
return (0);
m_set_rcvif(m0, ifp);
m0->m_pkthdr.len = totlen;
len = MHLEN;
m = m0;
while (totlen > 0) {
if (totlen >= MINCLSIZE) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0)
goto bad;
len = MCLBYTES;
}
if (m == m0) {
char *newdata = (char *)
ALIGN(m->m_data + sizeof(struct ether_header)) -
sizeof(struct ether_header);
len -= newdata - m->m_data;
m->m_data = newdata;
}
m->m_len = len = uimin(totlen, len);
memcpy(mtod(m, void *), data, len);
data += len;
totlen -= len;
if (totlen > 0) {
MGET(newm, M_DONTWAIT, MT_DATA);
if (newm == 0)
goto bad;
len = MLEN;
m = m->m_next = newm;
}
}
return (m0);
bad:
m_freem(m0);
return (0);
}
static int
se_read(struct se_softc *sc, char *data, int datalen)
{
struct mbuf *m;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int n;
n = 0;
while (datalen >= 2) {
int len = _2btol(data);
data += 2;
datalen -= 2;
if (len == 0)
break;
#ifdef SEDEBUG
if (sc->sc_debug) {
printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
ntohs(((struct ether_header *)data)->ether_type));
}
#endif
if (len <= sizeof(struct ether_header) ||
len > MAX_SNAP) {
#ifdef SEDEBUG
printf("%s: invalid packet size %d; dropping\n",
device_xname(sc->sc_dev), len);
#endif
if_statinc(ifp, if_ierrors);
goto next_packet;
}
m = se_get(sc, data, len - ETHER_CRC);
if (m == 0) {
#ifdef SEDEBUG
if (sc->sc_debug)
printf("se_read: se_get returned null\n");
#endif
if_statinc(ifp, if_ierrors);
goto next_packet;
}
if ((ifp->if_flags & IFF_PROMISC) != 0) {
m_adj(m, SE_PREFIX);
}
if_percpuq_enqueue(sc->sc_ipq, m);
next_packet:
data += len;
datalen -= len;
n++;
}
return (n);
}
#if 0
static void
sewatchdog(struct ifnet *ifp)
{
struct se_softc *sc = ifp->if_softc;
log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
if_statinc(ifp, if_oerrors);
se_reset(sc);
}
static void
se_reset(struct se_softc *sc)
{
#if 0
se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
XS_CTL_RESET);
#endif
se_init(sc);
}
#endif
static int
se_add_proto(struct se_softc *sc, int proto)
{
int error;
struct scsi_ctron_ether_generic add_proto_cmd;
uint8_t data[2];
_lto2b(proto, data);
#ifdef SEDEBUG
if (sc->sc_debug)
printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
#endif
PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
_lto2b(sizeof(data), add_proto_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&add_proto_cmd, sizeof(add_proto_cmd),
data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
XS_CTL_DATA_OUT);
return (error);
}
static int
se_get_addr(struct se_softc *sc, uint8_t *myaddr)
{
int error;
struct scsi_ctron_ether_generic get_addr_cmd;
PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
_lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&get_addr_cmd, sizeof(get_addr_cmd),
myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
XS_CTL_DATA_IN);
printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
ether_sprintf(myaddr));
return (error);
}
static int
se_set_media(struct se_softc *sc, int type)
{
int error;
struct scsi_ctron_ether_generic set_media_cmd;
PROTOCMD(ctron_ether_set_media, set_media_cmd);
set_media_cmd.byte3 = type;
error = se_scsipi_cmd(sc->sc_periph,
(void *)&set_media_cmd, sizeof(set_media_cmd),
0, 0, SERETRIES, SETIMEOUT, NULL, 0);
return (error);
}
static int
se_set_mode(struct se_softc *sc, int len, int mode)
{
int error;
struct scsi_ctron_ether_set_mode set_mode_cmd;
PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
set_mode_cmd.mode = mode;
_lto2b(len, set_mode_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&set_mode_cmd, sizeof(set_mode_cmd),
0, 0, SERETRIES, SETIMEOUT, NULL, 0);
return (error);
}
static int
se_init(struct se_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct scsi_ctron_ether_generic set_addr_cmd;
uint8_t enaddr[ETHER_ADDR_LEN];
int error;
if (ifp->if_flags & IFF_PROMISC) {
error = se_set_mode(sc, MAX_SNAP, 1);
}
else
error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
0);
if (error != 0)
return (error);
PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
_lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
error = se_scsipi_cmd(sc->sc_periph,
(void *)&set_addr_cmd, sizeof(set_addr_cmd),
enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
XS_CTL_DATA_OUT);
if (error != 0)
return (error);
if ((sc->protos & PROTO_IP) &&
(error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
return (error);
if ((sc->protos & PROTO_ARP) &&
(error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
return (error);
if ((sc->protos & PROTO_REVARP) &&
(error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
return (error);
#ifdef NETATALK
if ((sc->protos & PROTO_AT) &&
(error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
return (error);
if ((sc->protos & PROTO_AARP) &&
(error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
return (error);
#endif
if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
ifp->if_flags |= IFF_RUNNING;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_recv_work_pending) {
sc->sc_recv_work_pending = true;
workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work,
NULL);
}
mutex_exit(&sc->sc_iflock);
ifp->if_flags &= ~IFF_OACTIVE;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_send_work_pending) {
sc->sc_send_work_pending = true;
workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work,
NULL);
}
mutex_exit(&sc->sc_iflock);
}
return (error);
}
static int
se_set_multi(struct se_softc *sc, uint8_t *addr)
{
struct scsi_ctron_ether_generic set_multi_cmd;
int error;
if (sc->sc_debug)
printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
ether_sprintf(addr));
PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
_lto2b(ETHER_ADDR_LEN, set_multi_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&set_multi_cmd, sizeof(set_multi_cmd),
addr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
return (error);
}
static int
se_remove_multi(struct se_softc *sc, uint8_t *addr)
{
struct scsi_ctron_ether_generic remove_multi_cmd;
int error;
if (sc->sc_debug)
printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
ether_sprintf(addr));
PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
_lto2b(ETHER_ADDR_LEN, remove_multi_cmd.length);
error = se_scsipi_cmd(sc->sc_periph,
(void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
addr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
return (error);
}
#if 0
static int
sc_set_all_multi(struct se_softc *sc, int set)
{
int error = 0;
uint8_t *addr;
struct ethercom *ec = &sc->sc_ethercom;
struct ether_multi *enm;
struct ether_multistep step;
ETHER_LOCK(ec);
ETHER_FIRST_MULTI(step, ec, enm);
while (enm != NULL) {
if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
ETHER_UNLOCK(ec);
return (ENODEV);
}
addr = enm->enm_addrlo;
if ((error = set ? se_set_multi(sc, addr) :
se_remove_multi(sc, addr)) != 0)
return (error);
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
return (error);
}
#endif
static void
se_stop(struct se_softc *sc)
{
callout_halt(&sc->sc_recv_ch, &sc->sc_iflock);
mutex_enter(&sc->sc_iflock);
workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
mutex_exit(&sc->sc_iflock);
mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
scsipi_kill_pending(sc->sc_periph);
mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
}
static int
se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct se_softc *sc = ifp->if_softc;
struct ifaddr *ifa = (struct ifaddr *)data;
struct ifreq *ifr = (struct ifreq *)data;
struct sockaddr *sa;
int error = 0;
switch (cmd) {
case SIOCINITIFADDR:
mutex_enter(&sc->sc_iflock);
if ((error = se_enable(sc)) != 0)
break;
ifp->if_flags |= IFF_UP;
mutex_exit(&sc->sc_iflock);
if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
break;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
if ((error = se_init(sc)) != 0)
break;
arp_ifinit(ifp, ifa);
break;
#endif
#ifdef NETATALK
case AF_APPLETALK:
sc->protos |= (PROTO_AT | PROTO_AARP);
if ((error = se_init(sc)) != 0)
break;
break;
#endif
default:
error = se_init(sc);
break;
}
break;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
se_stop(sc);
mutex_enter(&sc->sc_iflock);
ifp->if_flags &= ~IFF_RUNNING;
se_disable(sc);
mutex_exit(&sc->sc_iflock);
break;
case IFF_UP:
mutex_enter(&sc->sc_iflock);
error = se_enable(sc);
mutex_exit(&sc->sc_iflock);
if (error)
break;
error = se_init(sc);
break;
default:
if (sc->sc_enabled)
error = se_init(sc);
break;
}
#ifdef SEDEBUG
if (ifp->if_flags & IFF_DEBUG)
sc->sc_debug = 1;
else
sc->sc_debug = 0;
#endif
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
mutex_enter(&sc->sc_iflock);
sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
mutex_exit(&sc->sc_iflock);
if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING) {
error = (cmd == SIOCADDMULTI) ?
se_set_multi(sc, sa->sa_data) :
se_remove_multi(sc, sa->sa_data);
} else
error = 0;
}
mutex_enter(&sc->sc_iflock);
sockaddr_free(sa);
mutex_exit(&sc->sc_iflock);
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return (error);
}
int
se_enable(struct se_softc *sc)
{
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
int error = 0;
if (sc->sc_enabled == 0) {
if ((error = scsipi_adapter_addref(adapt)) == 0)
sc->sc_enabled = 1;
else
aprint_error_dev(sc->sc_dev, "device enable failed\n");
}
return (error);
}
void
se_disable(struct se_softc *sc)
{
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
if (sc->sc_enabled != 0) {
scsipi_adapter_delref(adapt);
sc->sc_enabled = 0;
}
}
#define SEUNIT(z) (minor(z))
int
seopen(dev_t dev, int flag, int fmt, struct lwp *l)
{
int unit, error;
struct se_softc *sc;
struct scsipi_periph *periph;
struct scsipi_adapter *adapt;
unit = SEUNIT(dev);
sc = device_lookup_private(&se_cd, unit);
if (sc == NULL)
return (ENXIO);
periph = sc->sc_periph;
adapt = periph->periph_channel->chan_adapter;
if ((error = scsipi_adapter_addref(adapt)) != 0)
return (error);
SC_DEBUG(periph, SCSIPI_DB1,
("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
se_cd.cd_ndevs));
periph->periph_flags |= PERIPH_OPEN;
SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
return (0);
}
int
seclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
scsipi_wait_drain(periph);
scsipi_adapter_delref(adapt);
periph->periph_flags &= ~PERIPH_OPEN;
return (0);
}
int
seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
{
struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
}