1139749Simp/*- 2113584Ssimokawa * Copyright (c) 2002-2003 3103285Sikob * Hidetoshi Shimokawa. All rights reserved. 4272214Skan * 5103285Sikob * Redistribution and use in source and binary forms, with or without 6103285Sikob * modification, are permitted provided that the following conditions 7103285Sikob * are met: 8103285Sikob * 1. Redistributions of source code must retain the above copyright 9103285Sikob * notice, this list of conditions and the following disclaimer. 10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 11103285Sikob * notice, this list of conditions and the following disclaimer in the 12103285Sikob * documentation and/or other materials provided with the distribution. 13103285Sikob * 3. All advertising materials mentioning features or use of this software 14103285Sikob * must display the following acknowledgement: 15103285Sikob * 16103285Sikob * This product includes software developed by Hidetoshi Shimokawa. 17103285Sikob * 18103285Sikob * 4. Neither the name of the author nor the names of its contributors 19103285Sikob * may be used to endorse or promote products derived from this software 20103285Sikob * without specific prior written permission. 21272214Skan * 22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23103285Sikob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24103285Sikob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25103285Sikob * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26103285Sikob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27103285Sikob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28103285Sikob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30103285Sikob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31103285Sikob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32103285Sikob * SUCH DAMAGE. 33272214Skan * 34103285Sikob * $FreeBSD$ 35103285Sikob */ 36103285Sikob 37150968Sglebius#ifdef HAVE_KERNEL_OPTION_HEADERS 38150968Sglebius#include "opt_device_polling.h" 39103285Sikob#include "opt_inet.h" 40150968Sglebius#endif 41103285Sikob 42103285Sikob#include <sys/param.h> 43103285Sikob#include <sys/kernel.h> 44103285Sikob#include <sys/malloc.h> 45103285Sikob#include <sys/mbuf.h> 46103285Sikob#include <sys/socket.h> 47103285Sikob#include <sys/sockio.h> 48103285Sikob#include <sys/sysctl.h> 49103285Sikob#include <sys/systm.h> 50103285Sikob#include <sys/module.h> 51103285Sikob#include <sys/bus.h> 52113584Ssimokawa#include <machine/bus.h> 53103285Sikob 54103285Sikob#include <net/bpf.h> 55103285Sikob#include <net/ethernet.h> 56103285Sikob#include <net/if.h> 57257176Sglebius#include <net/if_var.h> 58103285Sikob#include <net/if_arp.h> 59147256Sbrooks#include <net/if_types.h> 60103285Sikob#include <net/if_vlan_var.h> 61103285Sikob 62103285Sikob#include <dev/firewire/firewire.h> 63103285Sikob#include <dev/firewire/firewirereg.h> 64103285Sikob#include <dev/firewire/if_fwevar.h> 65103285Sikob 66122161Ssimokawa#define FWEDEBUG if (fwedebug) if_printf 67111942Ssimokawa#define TX_MAX_QUEUE (FWMAXQUEUE - 1) 68103285Sikob 69103285Sikob/* network interface */ 70124169Ssimokawastatic void fwe_start (struct ifnet *); 71124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t); 72124169Ssimokawastatic void fwe_init (void *); 73103285Sikob 74124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *); 75124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *); 76124169Ssimokawastatic void fwe_as_input (struct fw_xferq *); 77103285Sikob 78103285Sikobstatic int fwedebug = 0; 79103285Sikobstatic int stream_ch = 1; 80116139Ssimokawastatic int tx_speed = 2; 81122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE; 82103285Sikob 83227293Sedstatic MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface"); 84267992ShselaskySYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RWTUN, &fwedebug, 0, ""); 85103285SikobSYSCTL_DECL(_hw_firewire); 86227309Sedstatic SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0, 87122603Ssimokawa "Ethernet emulation subsystem"); 88267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RWTUN, &stream_ch, 0, 89103285Sikob "Stream channel to use"); 90267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RWTUN, &tx_speed, 0, 91122603Ssimokawa "Transmission speed"); 92267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len, 93122603Ssimokawa 0, "Length of the receive queue"); 94103285Sikob 95103285Sikob#ifdef DEVICE_POLLING 96103285Sikobstatic poll_handler_t fwe_poll; 97103285Sikob 98193096Sattiliostatic int 99103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 100103285Sikob{ 101103285Sikob struct fwe_softc *fwe; 102103285Sikob struct firewire_comm *fc; 103103285Sikob 104150789Sglebius if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 105193096Sattilio return (0); 106150789Sglebius 107103285Sikob fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 108103285Sikob fc = fwe->fd.fc; 109103285Sikob fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count); 110193096Sattilio return (0); 111103285Sikob} 112150789Sglebius#endif /* DEVICE_POLLING */ 113150789Sglebius 114103285Sikobstatic void 115103285Sikobfwe_identify(driver_t *driver, device_t parent) 116103285Sikob{ 117121953Ssimokawa BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent)); 118103285Sikob} 119103285Sikob 120103285Sikobstatic int 121103285Sikobfwe_probe(device_t dev) 122103285Sikob{ 123103285Sikob device_t pa; 124103285Sikob 125103285Sikob pa = device_get_parent(dev); 126272214Skan if (device_get_unit(dev) != device_get_unit(pa)) { 127272214Skan return (ENXIO); 128103285Sikob } 129103285Sikob 130108281Ssimokawa device_set_desc(dev, "Ethernet over FireWire"); 131103285Sikob return (0); 132103285Sikob} 133103285Sikob 134103285Sikobstatic int 135103285Sikobfwe_attach(device_t dev) 136103285Sikob{ 137103285Sikob struct fwe_softc *fwe; 138103285Sikob struct ifnet *ifp; 139103285Sikob int unit, s; 140147256Sbrooks u_char eaddr[6]; 141109814Ssimokawa struct fw_eui64 *eui; 142103285Sikob 143103285Sikob fwe = ((struct fwe_softc *)device_get_softc(dev)); 144103285Sikob unit = device_get_unit(dev); 145103285Sikob 146103285Sikob bzero(fwe, sizeof(struct fwe_softc)); 147170374Ssimokawa mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF); 148103285Sikob /* XXX */ 149103285Sikob fwe->stream_ch = stream_ch; 150103285Sikob fwe->dma_ch = -1; 151103285Sikob 152103285Sikob fwe->fd.fc = device_get_ivars(dev); 153124251Ssimokawa if (tx_speed < 0) 154124251Ssimokawa tx_speed = fwe->fd.fc->speed; 155124251Ssimokawa 156103285Sikob fwe->fd.dev = dev; 157103285Sikob fwe->fd.post_explore = NULL; 158103285Sikob fwe->eth_softc.fwe = fwe; 159103285Sikob 160103285Sikob fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM; 161103285Sikob fwe->pkt_hdr.mode.stream.sy = 0; 162103285Sikob fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 163103285Sikob 164103285Sikob /* generate fake MAC address: first and last 3bytes from eui64 */ 165103285Sikob#define LOCAL (0x02) 166103285Sikob#define GROUP (0x01) 167109814Ssimokawa 168109814Ssimokawa eui = &fwe->fd.fc->eui; 169109814Ssimokawa eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP; 170109814Ssimokawa eaddr[1] = FW_EUI64_BYTE(eui, 1); 171109814Ssimokawa eaddr[2] = FW_EUI64_BYTE(eui, 2); 172109814Ssimokawa eaddr[3] = FW_EUI64_BYTE(eui, 5); 173109814Ssimokawa eaddr[4] = FW_EUI64_BYTE(eui, 6); 174109814Ssimokawa eaddr[5] = FW_EUI64_BYTE(eui, 7); 175107653Ssimokawa printf("if_fwe%d: Fake Ethernet address: " 176107653Ssimokawa "%02x:%02x:%02x:%02x:%02x:%02x\n", unit, 177103285Sikob eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]); 178103285Sikob 179272214Skan /* fill the rest and attach interface */ 180147256Sbrooks ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER); 181147256Sbrooks if (ifp == NULL) { 182147256Sbrooks device_printf(dev, "can not if_alloc()\n"); 183147256Sbrooks return (ENOSPC); 184147256Sbrooks } 185103285Sikob ifp->if_softc = &fwe->eth_softc; 186103285Sikob 187121953Ssimokawa if_initname(ifp, device_get_name(dev), unit); 188103285Sikob ifp->if_init = fwe_init; 189103285Sikob ifp->if_start = fwe_start; 190103285Sikob ifp->if_ioctl = fwe_ioctl; 191170374Ssimokawa ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 192111942Ssimokawa ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE; 193103285Sikob 194103285Sikob s = splimp(); 195106937Ssam ether_ifattach(ifp, eaddr); 196103285Sikob splx(s); 197103285Sikob 198103285Sikob /* Tell the upper layer(s) we support long frames. */ 199270856Sglebius ifp->if_hdrlen = sizeof(struct ether_vlan_header); 200151229Sglebius ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_POLLING; 201129552Syar ifp->if_capenable |= IFCAP_VLAN_MTU; 202103285Sikob 203122161Ssimokawa FWEDEBUG(ifp, "interface created\n"); 204103285Sikob return 0; 205103285Sikob} 206103285Sikob 207103285Sikobstatic void 208103285Sikobfwe_stop(struct fwe_softc *fwe) 209103285Sikob{ 210103285Sikob struct firewire_comm *fc; 211103285Sikob struct fw_xferq *xferq; 212147256Sbrooks struct ifnet *ifp = fwe->eth_softc.ifp; 213111942Ssimokawa struct fw_xfer *xfer, *next; 214111942Ssimokawa int i; 215103285Sikob 216103285Sikob fc = fwe->fd.fc; 217103285Sikob 218103285Sikob if (fwe->dma_ch >= 0) { 219103285Sikob xferq = fc->ir[fwe->dma_ch]; 220103285Sikob 221103285Sikob if (xferq->flag & FWXFERQ_RUNNING) 222103285Sikob fc->irx_disable(fc, fwe->dma_ch); 223272214Skan xferq->flag &= 224113584Ssimokawa ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM | 225113584Ssimokawa FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK); 226111942Ssimokawa xferq->hand = NULL; 227111942Ssimokawa 228272214Skan for (i = 0; i < xferq->bnchunk; i++) 229111942Ssimokawa m_freem(xferq->bulkxfer[i].mbuf); 230111942Ssimokawa free(xferq->bulkxfer, M_FWE); 231111942Ssimokawa 232111942Ssimokawa for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL; 233111942Ssimokawa xfer = next) { 234111942Ssimokawa next = STAILQ_NEXT(xfer, link); 235111942Ssimokawa fw_xfer_free(xfer); 236111942Ssimokawa } 237111942Ssimokawa STAILQ_INIT(&fwe->xferlist); 238111942Ssimokawa 239111942Ssimokawa xferq->bulkxfer = NULL; 240103285Sikob fwe->dma_ch = -1; 241103285Sikob } 242103285Sikob 243148887Srwatson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 244103285Sikob} 245103285Sikob 246103285Sikobstatic int 247103285Sikobfwe_detach(device_t dev) 248103285Sikob{ 249103285Sikob struct fwe_softc *fwe; 250147256Sbrooks struct ifnet *ifp; 251103285Sikob int s; 252103285Sikob 253147256Sbrooks fwe = device_get_softc(dev); 254147256Sbrooks ifp = fwe->eth_softc.ifp; 255150789Sglebius 256150789Sglebius#ifdef DEVICE_POLLING 257150789Sglebius if (ifp->if_capenable & IFCAP_POLLING) 258150789Sglebius ether_poll_deregister(ifp); 259150789Sglebius#endif 260103285Sikob s = splimp(); 261103285Sikob 262103285Sikob fwe_stop(fwe); 263147256Sbrooks ether_ifdetach(ifp); 264147256Sbrooks if_free(ifp); 265103285Sikob 266103285Sikob splx(s); 267170374Ssimokawa mtx_destroy(&fwe->mtx); 268103285Sikob return 0; 269103285Sikob} 270103285Sikob 271103285Sikobstatic void 272103285Sikobfwe_init(void *arg) 273103285Sikob{ 274103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe; 275103285Sikob struct firewire_comm *fc; 276147256Sbrooks struct ifnet *ifp = fwe->eth_softc.ifp; 277103285Sikob struct fw_xferq *xferq; 278111942Ssimokawa struct fw_xfer *xfer; 279113584Ssimokawa struct mbuf *m; 280103285Sikob int i; 281103285Sikob 282122161Ssimokawa FWEDEBUG(ifp, "initializing\n"); 283103285Sikob 284103285Sikob /* XXX keep promiscoud mode */ 285103285Sikob ifp->if_flags |= IFF_PROMISC; 286103285Sikob 287103285Sikob fc = fwe->fd.fc; 288103285Sikob if (fwe->dma_ch < 0) { 289170374Ssimokawa fwe->dma_ch = fw_open_isodma(fc, /* tx */0); 290170374Ssimokawa if (fwe->dma_ch < 0) 291170374Ssimokawa return; 292170374Ssimokawa xferq = fc->ir[fwe->dma_ch]; 293170374Ssimokawa xferq->flag |= FWXFERQ_EXTBUF | 294170374Ssimokawa FWXFERQ_HANDLER | FWXFERQ_STREAM; 295103285Sikob fwe->stream_ch = stream_ch; 296103285Sikob fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 297112400Ssimokawa xferq->flag &= ~0xff; 298103285Sikob xferq->flag |= fwe->stream_ch & 0xff; 299103285Sikob /* register fwe_input handler */ 300103285Sikob xferq->sc = (caddr_t) fwe; 301103285Sikob xferq->hand = fwe_as_input; 302122603Ssimokawa xferq->bnchunk = rx_queue_len; 303111942Ssimokawa xferq->bnpacket = 1; 304111942Ssimokawa xferq->psize = MCLBYTES; 305111942Ssimokawa xferq->queued = 0; 306113584Ssimokawa xferq->buf = NULL; 307111942Ssimokawa xferq->bulkxfer = (struct fw_bulkxfer *) malloc( 308113584Ssimokawa sizeof(struct fw_bulkxfer) * xferq->bnchunk, 309113584Ssimokawa M_FWE, M_WAITOK); 310111942Ssimokawa STAILQ_INIT(&xferq->stvalid); 311111942Ssimokawa STAILQ_INIT(&xferq->stfree); 312111942Ssimokawa STAILQ_INIT(&xferq->stdma); 313111942Ssimokawa xferq->stproc = NULL; 314272214Skan for (i = 0; i < xferq->bnchunk; i++) { 315243857Sglebius m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 316113584Ssimokawa xferq->bulkxfer[i].mbuf = m; 317177599Sru m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 318177599Sru STAILQ_INSERT_TAIL(&xferq->stfree, 319177599Sru &xferq->bulkxfer[i], link); 320111942Ssimokawa } 321111942Ssimokawa STAILQ_INIT(&fwe->xferlist); 322111942Ssimokawa for (i = 0; i < TX_MAX_QUEUE; i++) { 323111942Ssimokawa xfer = fw_xfer_alloc(M_FWE); 324111942Ssimokawa if (xfer == NULL) 325111942Ssimokawa break; 326120660Ssimokawa xfer->send.spd = tx_speed; 327111942Ssimokawa xfer->fc = fwe->fd.fc; 328111942Ssimokawa xfer->sc = (caddr_t)fwe; 329167632Ssimokawa xfer->hand = fwe_output_callback; 330111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 331111942Ssimokawa } 332103285Sikob } else 333103285Sikob xferq = fc->ir[fwe->dma_ch]; 334103285Sikob 335103285Sikob 336103285Sikob /* start dma */ 337103285Sikob if ((xferq->flag & FWXFERQ_RUNNING) == 0) 338103285Sikob fc->irx_enable(fc, fwe->dma_ch); 339103285Sikob 340148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 341148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 342103285Sikob 343103285Sikob#if 0 344103285Sikob /* attempt to start output */ 345103285Sikob fwe_start(ifp); 346103285Sikob#endif 347103285Sikob} 348103285Sikob 349103285Sikob 350103285Sikobstatic int 351103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 352103285Sikob{ 353103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 354103285Sikob struct ifstat *ifs = NULL; 355260394Smelifaro int s, error; 356103285Sikob 357103285Sikob switch (cmd) { 358103285Sikob case SIOCSIFFLAGS: 359103285Sikob s = splimp(); 360103285Sikob if (ifp->if_flags & IFF_UP) { 361148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 362103285Sikob fwe_init(&fwe->eth_softc); 363103285Sikob } else { 364148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 365103285Sikob fwe_stop(fwe); 366103285Sikob } 367103285Sikob /* XXX keep promiscoud mode */ 368103285Sikob ifp->if_flags |= IFF_PROMISC; 369103285Sikob splx(s); 370103285Sikob break; 371103285Sikob case SIOCADDMULTI: 372103285Sikob case SIOCDELMULTI: 373108712Ssimokawa break; 374103285Sikob 375103285Sikob case SIOCGIFSTATUS: 376103285Sikob s = splimp(); 377103285Sikob ifs = (struct ifstat *)data; 378260394Smelifaro snprintf(ifs->ascii, sizeof(ifs->ascii), 379260394Smelifaro "\tch %d dma %d\n", fwe->stream_ch, fwe->dma_ch); 380103285Sikob splx(s); 381108712Ssimokawa break; 382150789Sglebius case SIOCSIFCAP: 383150789Sglebius#ifdef DEVICE_POLLING 384150789Sglebius { 385150789Sglebius struct ifreq *ifr = (struct ifreq *) data; 386188394Sfjoe struct firewire_comm *fc = fwe->fd.fc; 387150789Sglebius 388150789Sglebius if (ifr->ifr_reqcap & IFCAP_POLLING && 389150789Sglebius !(ifp->if_capenable & IFCAP_POLLING)) { 390150789Sglebius error = ether_poll_register(fwe_poll, ifp); 391150789Sglebius if (error) 392272214Skan return (error); 393150789Sglebius /* Disable interrupts */ 394150789Sglebius fc->set_intr(fc, 0); 395150789Sglebius ifp->if_capenable |= IFCAP_POLLING; 396193096Sattilio ifp->if_capenable |= IFCAP_POLLING_NOCOUNT; 397150789Sglebius return (error); 398150789Sglebius } 399150789Sglebius if (!(ifr->ifr_reqcap & IFCAP_POLLING) && 400150789Sglebius ifp->if_capenable & IFCAP_POLLING) { 401150789Sglebius error = ether_poll_deregister(ifp); 402150789Sglebius /* Enable interrupts. */ 403150789Sglebius fc->set_intr(fc, 1); 404150789Sglebius ifp->if_capenable &= ~IFCAP_POLLING; 405193096Sattilio ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT; 406150789Sglebius return (error); 407150789Sglebius } 408150789Sglebius } 409150789Sglebius#endif /* DEVICE_POLLING */ 410150789Sglebius break; 411103285Sikob default: 412106937Ssam s = splimp(); 413106937Ssam error = ether_ioctl(ifp, cmd, data); 414106937Ssam splx(s); 415106937Ssam return (error); 416103285Sikob } 417103285Sikob 418103285Sikob return (0); 419103285Sikob} 420103285Sikob 421103285Sikobstatic void 422111942Ssimokawafwe_output_callback(struct fw_xfer *xfer) 423111942Ssimokawa{ 424111942Ssimokawa struct fwe_softc *fwe; 425111942Ssimokawa struct ifnet *ifp; 426111942Ssimokawa int s; 427111942Ssimokawa 428111942Ssimokawa fwe = (struct fwe_softc *)xfer->sc; 429147256Sbrooks ifp = fwe->eth_softc.ifp; 430111942Ssimokawa /* XXX error check */ 431122161Ssimokawa FWEDEBUG(ifp, "resp = %d\n", xfer->resp); 432111942Ssimokawa if (xfer->resp != 0) 433271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 434111942Ssimokawa m_freem(xfer->mbuf); 435111942Ssimokawa fw_xfer_unload(xfer); 436113584Ssimokawa 437111942Ssimokawa s = splimp(); 438170374Ssimokawa FWE_LOCK(fwe); 439111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 440170374Ssimokawa FWE_UNLOCK(fwe); 441111942Ssimokawa splx(s); 442113584Ssimokawa 443113584Ssimokawa /* for queue full */ 444111942Ssimokawa if (ifp->if_snd.ifq_head != NULL) 445111942Ssimokawa fwe_start(ifp); 446111942Ssimokawa} 447111942Ssimokawa 448111942Ssimokawastatic void 449103285Sikobfwe_start(struct ifnet *ifp) 450103285Sikob{ 451103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 452103285Sikob int s; 453103285Sikob 454122161Ssimokawa FWEDEBUG(ifp, "starting\n"); 455103285Sikob 456103285Sikob if (fwe->dma_ch < 0) { 457103285Sikob struct mbuf *m = NULL; 458103285Sikob 459122161Ssimokawa FWEDEBUG(ifp, "not ready\n"); 460103285Sikob 461103285Sikob s = splimp(); 462103285Sikob do { 463103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 464103285Sikob if (m != NULL) 465103285Sikob m_freem(m); 466271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 467103285Sikob } while (m != NULL); 468103285Sikob splx(s); 469103285Sikob 470103285Sikob return; 471103285Sikob } 472103285Sikob 473103285Sikob s = splimp(); 474148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 475103285Sikob 476103285Sikob if (ifp->if_snd.ifq_len != 0) 477103285Sikob fwe_as_output(fwe, ifp); 478103285Sikob 479148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 480103285Sikob splx(s); 481103285Sikob} 482103285Sikob 483111942Ssimokawa#define HDR_LEN 4 484111942Ssimokawa#ifndef ETHER_ALIGN 485111942Ssimokawa#define ETHER_ALIGN 2 486103285Sikob#endif 487103285Sikob/* Async. stream output */ 488103285Sikobstatic void 489103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp) 490103285Sikob{ 491103285Sikob struct mbuf *m; 492103285Sikob struct fw_xfer *xfer; 493103285Sikob struct fw_xferq *xferq; 494103285Sikob struct fw_pkt *fp; 495103285Sikob int i = 0; 496103285Sikob 497103285Sikob xfer = NULL; 498103285Sikob xferq = fwe->fd.fc->atq; 499170374Ssimokawa while ((xferq->queued < xferq->maxq - 1) && 500170374Ssimokawa (ifp->if_snd.ifq_head != NULL)) { 501170374Ssimokawa FWE_LOCK(fwe); 502111942Ssimokawa xfer = STAILQ_FIRST(&fwe->xferlist); 503111942Ssimokawa if (xfer == NULL) { 504170374Ssimokawa#if 0 505111942Ssimokawa printf("if_fwe: lack of xfer\n"); 506170374Ssimokawa#endif 507170374Ssimokawa FWE_UNLOCK(fwe); 508170374Ssimokawa break; 509111942Ssimokawa } 510170374Ssimokawa STAILQ_REMOVE_HEAD(&fwe->xferlist, link); 511170374Ssimokawa FWE_UNLOCK(fwe); 512170374Ssimokawa 513103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 514170374Ssimokawa if (m == NULL) { 515170374Ssimokawa FWE_LOCK(fwe); 516170374Ssimokawa STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link); 517170374Ssimokawa FWE_UNLOCK(fwe); 518103285Sikob break; 519170374Ssimokawa } 520127468Ssimokawa BPF_MTAP(ifp, m); 521103285Sikob 522103285Sikob /* keep ip packet alignment for alpha */ 523243857Sglebius M_PREPEND(m, ETHER_ALIGN, M_NOWAIT); 524120660Ssimokawa fp = &xfer->send.hdr; 525129585Sdfr *(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr; 526113584Ssimokawa fp->mode.stream.len = m->m_pkthdr.len; 527103285Sikob xfer->mbuf = m; 528120660Ssimokawa xfer->send.pay_len = m->m_pkthdr.len; 529103285Sikob 530111942Ssimokawa if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) { 531103285Sikob /* error */ 532271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 533103285Sikob /* XXX set error code */ 534103285Sikob fwe_output_callback(xfer); 535103285Sikob } else { 536271849Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 537111942Ssimokawa i++; 538103285Sikob } 539103285Sikob } 540103285Sikob#if 0 541103285Sikob if (i > 1) 542103285Sikob printf("%d queued\n", i); 543103285Sikob#endif 544111942Ssimokawa if (i > 0) 545111942Ssimokawa xferq->start(fwe->fd.fc); 546103285Sikob} 547103285Sikob 548103285Sikob/* Async. stream output */ 549103285Sikobstatic void 550103285Sikobfwe_as_input(struct fw_xferq *xferq) 551103285Sikob{ 552113584Ssimokawa struct mbuf *m, *m0; 553103285Sikob struct ifnet *ifp; 554103285Sikob struct fwe_softc *fwe; 555111942Ssimokawa struct fw_bulkxfer *sxfer; 556111942Ssimokawa struct fw_pkt *fp; 557103285Sikob u_char *c; 558103285Sikob 559103285Sikob fwe = (struct fwe_softc *)xferq->sc; 560147256Sbrooks ifp = fwe->eth_softc.ifp; 561150789Sglebius 562170374Ssimokawa /* We do not need a lock here because the bottom half is serialized */ 563111942Ssimokawa while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) { 564111942Ssimokawa STAILQ_REMOVE_HEAD(&xferq->stvalid, link); 565113584Ssimokawa fp = mtod(sxfer->mbuf, struct fw_pkt *); 566111942Ssimokawa if (fwe->fd.fc->irx_post != NULL) 567111942Ssimokawa fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld); 568111942Ssimokawa m = sxfer->mbuf; 569111942Ssimokawa 570119119Ssimokawa /* insert new rbuf */ 571243857Sglebius sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 572113584Ssimokawa if (m0 != NULL) { 573113584Ssimokawa m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size; 574113584Ssimokawa STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link); 575113584Ssimokawa } else 576170374Ssimokawa printf("%s: m_getcl failed\n", __FUNCTION__); 577111942Ssimokawa 578119119Ssimokawa if (sxfer->resp != 0 || fp->mode.stream.len < 579119119Ssimokawa ETHER_ALIGN + sizeof(struct ether_header)) { 580119119Ssimokawa m_freem(m); 581271849Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 582119119Ssimokawa continue; 583119119Ssimokawa } 584119119Ssimokawa 585111942Ssimokawa m->m_data += HDR_LEN + ETHER_ALIGN; 586170374Ssimokawa c = mtod(m, u_char *); 587132429Ssimokawa m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN; 588103285Sikob m->m_pkthdr.rcvif = ifp; 589103285Sikob#if 0 590122161Ssimokawa FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n" 591103285Sikob "%02x %02x %02x %02x %02x %02x\n" 592103285Sikob "%02x %02x %02x %02x\n" 593103285Sikob "%02x %02x %02x %02x\n" 594103285Sikob "%02x %02x %02x %02x\n" 595103285Sikob "%02x %02x %02x %02x\n", 596103285Sikob c[0], c[1], c[2], c[3], c[4], c[5], 597103285Sikob c[6], c[7], c[8], c[9], c[10], c[11], 598103285Sikob c[12], c[13], c[14], c[15], 599103285Sikob c[16], c[17], c[18], c[19], 600103285Sikob c[20], c[21], c[22], c[23], 601103285Sikob c[20], c[21], c[22], c[23] 602272214Skan ); 603103285Sikob#endif 604106937Ssam (*ifp->if_input)(ifp, m); 605271849Sglebius if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 606103285Sikob } 607111942Ssimokawa if (STAILQ_FIRST(&xferq->stfree) != NULL) 608111942Ssimokawa fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch); 609103285Sikob} 610103285Sikob 611103285Sikob 612103285Sikobstatic devclass_t fwe_devclass; 613103285Sikob 614103285Sikobstatic device_method_t fwe_methods[] = { 615103285Sikob /* device interface */ 616103285Sikob DEVMETHOD(device_identify, fwe_identify), 617103285Sikob DEVMETHOD(device_probe, fwe_probe), 618103285Sikob DEVMETHOD(device_attach, fwe_attach), 619103285Sikob DEVMETHOD(device_detach, fwe_detach), 620103285Sikob { 0, 0 } 621103285Sikob}; 622103285Sikob 623103285Sikobstatic driver_t fwe_driver = { 624121953Ssimokawa "fwe", 625103285Sikob fwe_methods, 626103285Sikob sizeof(struct fwe_softc), 627103285Sikob}; 628103285Sikob 629103285Sikob 630113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0); 631113506SmdoddMODULE_VERSION(fwe, 1); 632113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1); 633