#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_device_polling.h"
#include "opt_inet.h"
#endif
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/if_types.h>
#include <net/if_vlan_var.h>
#include <dev/firewire/firewire.h>
#include <dev/firewire/firewirereg.h>
#include <dev/firewire/if_fwevar.h>
#include <dev/firewire/fw_net.h>
#define FWEDEBUG if (fwedebug) if_printf
#define TX_MAX_QUEUE (FWMAXQUEUE - 1)
static void fwe_start (if_t);
static int fwe_ioctl (if_t, u_long, caddr_t);
static void fwe_init (void *);
static void fwe_output_callback (struct fw_xfer *);
static void fwe_as_output (struct fwe_softc *, if_t);
static void fwe_as_input (struct fw_xferq *);
static int fwedebug = 0;
static int stream_ch = 1;
static int tx_speed = FWSPD_S400;
static int rx_queue_len = FWMAXQUEUE;
static MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RWTUN, &fwedebug, 0, "");
SYSCTL_DECL(_hw_firewire);
static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
"Ethernet emulation subsystem");
SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RWTUN, &stream_ch, 0,
"Stream channel to use");
SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RWTUN, &tx_speed, 0,
"Transmission speed");
SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len,
0, "Length of the receive queue");
#ifdef DEVICE_POLLING
static poll_handler_t fwe_poll;
static int
fwe_poll(if_t ifp, enum poll_cmd cmd, int count)
{
struct fwe_softc *fwe;
struct firewire_comm *fc;
if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
return (0);
fwe = ((struct fwe_eth_softc *)if_getsoftc(ifp))->fwe;
fc = fwe->fd.fc;
fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
return (0);
}
#endif
static void
fwe_identify(driver_t *driver, device_t parent)
{
BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
}
static int
fwe_probe(device_t dev)
{
device_t pa;
if (fw_get_unit(dev) != NULL)
return (ENXIO);
pa = device_get_parent(dev);
if (device_get_unit(dev) != device_get_unit(pa))
return (ENXIO);
device_set_desc(dev, "Ethernet over FireWire");
return (0);
}
static int
fwe_attach(device_t dev)
{
struct fwe_softc *fwe;
if_t ifp;
int unit;
u_char eaddr[6];
struct fw_eui64 *eui;
fwe = ((struct fwe_softc *)device_get_softc(dev));
unit = device_get_unit(dev);
bzero(fwe, sizeof(struct fwe_softc));
mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
fwe->stream_ch = stream_ch;
fwe->dma_ch = -1;
fwe->fd.fc = fw_get_comm(dev);
if (tx_speed < 0)
tx_speed = fwe->fd.fc->speed;
fwe->fd.dev = dev;
fwe->fd.post_explore = NULL;
fwe->eth_softc.fwe = fwe;
fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
fwe->pkt_hdr.mode.stream.sy = 0;
fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
#define LOCAL (0x02)
#define GROUP (0x01)
eui = &fwe->fd.fc->eui;
eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
eaddr[1] = FW_EUI64_BYTE(eui, 1);
eaddr[2] = FW_EUI64_BYTE(eui, 2);
eaddr[3] = FW_EUI64_BYTE(eui, 5);
eaddr[4] = FW_EUI64_BYTE(eui, 6);
eaddr[5] = FW_EUI64_BYTE(eui, 7);
printf("if_fwe%d: Fake Ethernet address: "
"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
if_setsoftc(ifp, &fwe->eth_softc);
if_initname(ifp, device_get_name(dev), unit);
if_setinitfn(ifp, fwe_init);
if_setstartfn(ifp, fwe_start);
if_setioctlfn(ifp, fwe_ioctl);
if_setflags(ifp, (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST));
if_setsendqlen(ifp, TX_MAX_QUEUE);
ether_ifattach(ifp, eaddr);
if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_POLLING, 0);
if_setcapenablebit(ifp, IFCAP_VLAN_MTU, 0);
FWEDEBUG(ifp, "interface created\n");
return (0);
}
static void
fwe_stop(struct fwe_softc *fwe)
{
struct firewire_comm *fc;
struct fw_xferq *xferq;
if_t ifp = fwe->eth_softc.ifp;
int i;
fc = fwe->fd.fc;
if (fwe->dma_ch >= 0) {
xferq = fc->ir[fwe->dma_ch];
if (xferq->flag & FWXFERQ_RUNNING)
fc->irx_disable(fc, fwe->dma_ch);
xferq->flag &=
~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
xferq->hand = NULL;
for (i = 0; i < xferq->bnchunk; i++)
m_freem(xferq->bulkxfer[i].mbuf);
free(xferq->bulkxfer, M_FWE);
FW_NET_FREE_XFERLIST(&fwe->xferlist);
STAILQ_INIT(&fwe->xferlist);
xferq->bulkxfer = NULL;
fwe->dma_ch = -1;
}
if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
}
static int
fwe_detach(device_t dev)
{
struct fwe_softc *fwe;
if_t ifp;
fwe = device_get_softc(dev);
ifp = fwe->eth_softc.ifp;
#ifdef DEVICE_POLLING
if (if_getcapenable(ifp) & IFCAP_POLLING)
ether_poll_deregister(ifp);
#endif
fwe_stop(fwe);
ether_ifdetach(ifp);
if_free(ifp);
mtx_destroy(&fwe->mtx);
return 0;
}
static void
fwe_init(void *arg)
{
struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
struct firewire_comm *fc;
if_t ifp = fwe->eth_softc.ifp;
struct fw_xferq *xferq;
struct fw_xfer *xfer;
int i;
FWEDEBUG(ifp, "initializing\n");
if_setflagbits(ifp, IFF_PROMISC, 0);
fc = fwe->fd.fc;
if (fwe->dma_ch < 0) {
fwe->dma_ch = fw_open_isodma(fc, 0);
if (fwe->dma_ch < 0)
return;
xferq = fc->ir[fwe->dma_ch];
xferq->flag |= FWXFERQ_EXTBUF |
FWXFERQ_HANDLER | FWXFERQ_STREAM;
fwe->stream_ch = stream_ch;
fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
xferq->flag &= ~0xff;
xferq->flag |= fwe->stream_ch & 0xff;
xferq->sc = (caddr_t) fwe;
xferq->hand = fwe_as_input;
xferq->bnchunk = rx_queue_len;
xferq->bnpacket = 1;
xferq->psize = MCLBYTES;
xferq->queued = 0;
xferq->buf = NULL;
xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
sizeof(struct fw_bulkxfer) * xferq->bnchunk,
M_FWE, M_WAITOK);
STAILQ_INIT(&xferq->stvalid);
STAILQ_INIT(&xferq->stfree);
STAILQ_INIT(&xferq->stdma);
xferq->stproc = NULL;
fw_net_init_iso_chunks(xferq);
STAILQ_INIT(&fwe->xferlist);
for (i = 0; i < TX_MAX_QUEUE; i++) {
xfer = fw_net_alloc_txfer(fwe->fd.fc, tx_speed,
fwe, fwe_output_callback, M_FWE);
if (xfer == NULL)
break;
STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
}
} else
xferq = fc->ir[fwe->dma_ch];
if ((xferq->flag & FWXFERQ_RUNNING) == 0)
fc->irx_enable(fc, fwe->dma_ch);
if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
}
static int
fwe_ioctl(if_t ifp, u_long cmd, caddr_t data)
{
struct fwe_softc *fwe = ((struct fwe_eth_softc *)if_getsoftc(ifp))->fwe;
struct ifstat *ifs = NULL;
int error;
switch (cmd) {
case SIOCSIFFLAGS:
if (if_getflags(ifp) & IFF_UP) {
if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
fwe_init(&fwe->eth_softc);
} else {
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
fwe_stop(fwe);
}
if_setflagbits(ifp, IFF_PROMISC, 0);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
break;
case SIOCGIFSTATUS:
ifs = (struct ifstat *)data;
snprintf(ifs->ascii, sizeof(ifs->ascii),
"\tch %d dma %d\n", fwe->stream_ch, fwe->dma_ch);
break;
case SIOCSIFCAP:
#ifdef DEVICE_POLLING
{
struct ifreq *ifr = (struct ifreq *) data;
error = fw_net_poll_ioctl(ifp, ifr,
fwe->fd.fc, fwe_poll);
if (error >= 0)
return (error);
}
#endif
break;
default:
error = ether_ioctl(ifp, cmd, data);
return (error);
}
return (0);
}
static void
fwe_output_callback(struct fw_xfer *xfer)
{
struct fwe_softc *fwe;
if_t ifp;
fwe = (struct fwe_softc *)xfer->sc;
ifp = fwe->eth_softc.ifp;
FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
if (xfer->resp != 0)
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
m_freem(xfer->mbuf);
fw_xfer_unload(xfer);
FWE_LOCK(fwe);
STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
FWE_UNLOCK(fwe);
if (!if_sendq_empty(ifp))
fwe_start(ifp);
}
static void
fwe_start(if_t ifp)
{
struct fwe_softc *fwe = ((struct fwe_eth_softc *)if_getsoftc(ifp))->fwe;
FWEDEBUG(ifp, "starting\n");
if (fwe->dma_ch < 0) {
FWEDEBUG(ifp, "not ready\n");
fw_net_drain_sendq(ifp);
return;
}
if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
if (!if_sendq_empty(ifp))
fwe_as_output(fwe, ifp);
if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
}
#define HDR_LEN 4
#ifndef ETHER_ALIGN
#define ETHER_ALIGN 2
#endif
static void
fwe_as_output(struct fwe_softc *fwe, if_t ifp)
{
struct mbuf *m;
struct fw_xfer *xfer;
struct fw_xferq *xferq;
struct fw_pkt *fp;
int i = 0;
xfer = NULL;
xferq = fwe->fd.fc->atq;
while ((xferq->queued < xferq->maxq - 1) &&
!if_sendq_empty(ifp)) {
FWE_LOCK(fwe);
xfer = STAILQ_FIRST(&fwe->xferlist);
if (xfer == NULL) {
FWE_UNLOCK(fwe);
break;
}
STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
FWE_UNLOCK(fwe);
m = if_dequeue(ifp);
if (m == NULL) {
FWE_LOCK(fwe);
STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
FWE_UNLOCK(fwe);
break;
}
BPF_MTAP(ifp, m);
M_PREPEND(m, ETHER_ALIGN, M_NOWAIT);
fp = &xfer->send.hdr;
*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
fp->mode.stream.len = m->m_pkthdr.len;
xfer->mbuf = m;
xfer->send.pay_len = m->m_pkthdr.len;
if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
fwe_output_callback(xfer);
} else {
if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
i++;
}
}
if (i > 0)
xferq->start(fwe->fd.fc);
}
static void
fwe_as_input(struct fw_xferq *xferq)
{
struct mbuf *m, *m0;
if_t ifp;
struct fwe_softc *fwe;
struct fw_bulkxfer *sxfer;
struct fw_pkt *fp;
struct epoch_tracker et;
fwe = (struct fwe_softc *)xferq->sc;
ifp = fwe->eth_softc.ifp;
while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
fp = mtod(sxfer->mbuf, struct fw_pkt *);
if (fwe->fd.fc->irx_post != NULL)
fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
m = sxfer->mbuf;
sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
if (m0 != NULL) {
m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
} else
printf("%s: m_getcl failed\n", __FUNCTION__);
if (sxfer->resp != 0 || fp->mode.stream.len <
ETHER_ALIGN + sizeof(struct ether_header)) {
m_freem(m);
if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
continue;
}
m->m_data += HDR_LEN + ETHER_ALIGN;
m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
m->m_pkthdr.rcvif = ifp;
NET_EPOCH_ENTER(et);
if_input(ifp, m);
NET_EPOCH_EXIT(et);
if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
}
if (STAILQ_FIRST(&xferq->stfree) != NULL)
fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
}
static device_method_t fwe_methods[] = {
DEVMETHOD(device_identify, fwe_identify),
DEVMETHOD(device_probe, fwe_probe),
DEVMETHOD(device_attach, fwe_attach),
DEVMETHOD(device_detach, fwe_detach),
DEVMETHOD_END
};
static driver_t fwe_driver = {
"fwe",
fwe_methods,
sizeof(struct fwe_softc),
};
DRIVER_MODULE(fwe, firewire, fwe_driver, 0, 0);
MODULE_VERSION(fwe, 1);
MODULE_DEPEND(fwe, firewire, 1, 1, 1);