if_fwe.c revision 271849
1139749Simp/*- 2113584Ssimokawa * Copyright (c) 2002-2003 3103285Sikob * Hidetoshi Shimokawa. All rights reserved. 4103285Sikob * 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. 21103285Sikob * 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. 33103285Sikob * 34103285Sikob * $FreeBSD: head/sys/dev/firewire/if_fwe.c 271849 2014-09-19 03:51:26Z glebius $ 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); 126103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 127103285Sikob 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 179103285Sikob /* 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); 223103285Sikob xferq->flag &= 224113584Ssimokawa ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM | 225113584Ssimokawa FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK); 226111942Ssimokawa xferq->hand = NULL; 227111942Ssimokawa 228111942Ssimokawa 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 if (xferq->bulkxfer == NULL) { 311111942Ssimokawa printf("if_fwe: malloc failed\n"); 312111942Ssimokawa return; 313111942Ssimokawa } 314111942Ssimokawa STAILQ_INIT(&xferq->stvalid); 315111942Ssimokawa STAILQ_INIT(&xferq->stfree); 316111942Ssimokawa STAILQ_INIT(&xferq->stdma); 317111942Ssimokawa xferq->stproc = NULL; 318111942Ssimokawa for (i = 0; i < xferq->bnchunk; i ++) { 319243857Sglebius m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 320113584Ssimokawa xferq->bulkxfer[i].mbuf = m; 321177599Sru m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 322177599Sru STAILQ_INSERT_TAIL(&xferq->stfree, 323177599Sru &xferq->bulkxfer[i], link); 324111942Ssimokawa } 325111942Ssimokawa STAILQ_INIT(&fwe->xferlist); 326111942Ssimokawa for (i = 0; i < TX_MAX_QUEUE; i++) { 327111942Ssimokawa xfer = fw_xfer_alloc(M_FWE); 328111942Ssimokawa if (xfer == NULL) 329111942Ssimokawa break; 330120660Ssimokawa xfer->send.spd = tx_speed; 331111942Ssimokawa xfer->fc = fwe->fd.fc; 332111942Ssimokawa xfer->sc = (caddr_t)fwe; 333167632Ssimokawa xfer->hand = fwe_output_callback; 334111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 335111942Ssimokawa } 336103285Sikob } else 337103285Sikob xferq = fc->ir[fwe->dma_ch]; 338103285Sikob 339103285Sikob 340103285Sikob /* start dma */ 341103285Sikob if ((xferq->flag & FWXFERQ_RUNNING) == 0) 342103285Sikob fc->irx_enable(fc, fwe->dma_ch); 343103285Sikob 344148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 345148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 346103285Sikob 347103285Sikob#if 0 348103285Sikob /* attempt to start output */ 349103285Sikob fwe_start(ifp); 350103285Sikob#endif 351103285Sikob} 352103285Sikob 353103285Sikob 354103285Sikobstatic int 355103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 356103285Sikob{ 357103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 358103285Sikob struct ifstat *ifs = NULL; 359260394Smelifaro int s, error; 360103285Sikob 361103285Sikob switch (cmd) { 362103285Sikob case SIOCSIFFLAGS: 363103285Sikob s = splimp(); 364103285Sikob if (ifp->if_flags & IFF_UP) { 365148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 366103285Sikob fwe_init(&fwe->eth_softc); 367103285Sikob } else { 368148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 369103285Sikob fwe_stop(fwe); 370103285Sikob } 371103285Sikob /* XXX keep promiscoud mode */ 372103285Sikob ifp->if_flags |= IFF_PROMISC; 373103285Sikob splx(s); 374103285Sikob break; 375103285Sikob case SIOCADDMULTI: 376103285Sikob case SIOCDELMULTI: 377108712Ssimokawa break; 378103285Sikob 379103285Sikob case SIOCGIFSTATUS: 380103285Sikob s = splimp(); 381103285Sikob ifs = (struct ifstat *)data; 382260394Smelifaro snprintf(ifs->ascii, sizeof(ifs->ascii), 383260394Smelifaro "\tch %d dma %d\n", fwe->stream_ch, fwe->dma_ch); 384103285Sikob splx(s); 385108712Ssimokawa break; 386150789Sglebius case SIOCSIFCAP: 387150789Sglebius#ifdef DEVICE_POLLING 388150789Sglebius { 389150789Sglebius struct ifreq *ifr = (struct ifreq *) data; 390188394Sfjoe struct firewire_comm *fc = fwe->fd.fc; 391150789Sglebius 392150789Sglebius if (ifr->ifr_reqcap & IFCAP_POLLING && 393150789Sglebius !(ifp->if_capenable & IFCAP_POLLING)) { 394150789Sglebius error = ether_poll_register(fwe_poll, ifp); 395150789Sglebius if (error) 396150789Sglebius return(error); 397150789Sglebius /* Disable interrupts */ 398150789Sglebius fc->set_intr(fc, 0); 399150789Sglebius ifp->if_capenable |= IFCAP_POLLING; 400193096Sattilio ifp->if_capenable |= IFCAP_POLLING_NOCOUNT; 401150789Sglebius return (error); 402150789Sglebius } 403150789Sglebius if (!(ifr->ifr_reqcap & IFCAP_POLLING) && 404150789Sglebius ifp->if_capenable & IFCAP_POLLING) { 405150789Sglebius error = ether_poll_deregister(ifp); 406150789Sglebius /* Enable interrupts. */ 407150789Sglebius fc->set_intr(fc, 1); 408150789Sglebius ifp->if_capenable &= ~IFCAP_POLLING; 409193096Sattilio ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT; 410150789Sglebius return (error); 411150789Sglebius } 412150789Sglebius } 413150789Sglebius#endif /* DEVICE_POLLING */ 414150789Sglebius break; 415103285Sikob default: 416106937Ssam s = splimp(); 417106937Ssam error = ether_ioctl(ifp, cmd, data); 418106937Ssam splx(s); 419106937Ssam return (error); 420103285Sikob } 421103285Sikob 422103285Sikob return (0); 423103285Sikob} 424103285Sikob 425103285Sikobstatic void 426111942Ssimokawafwe_output_callback(struct fw_xfer *xfer) 427111942Ssimokawa{ 428111942Ssimokawa struct fwe_softc *fwe; 429111942Ssimokawa struct ifnet *ifp; 430111942Ssimokawa int s; 431111942Ssimokawa 432111942Ssimokawa fwe = (struct fwe_softc *)xfer->sc; 433147256Sbrooks ifp = fwe->eth_softc.ifp; 434111942Ssimokawa /* XXX error check */ 435122161Ssimokawa FWEDEBUG(ifp, "resp = %d\n", xfer->resp); 436111942Ssimokawa if (xfer->resp != 0) 437271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 438111942Ssimokawa 439111942Ssimokawa m_freem(xfer->mbuf); 440111942Ssimokawa fw_xfer_unload(xfer); 441113584Ssimokawa 442111942Ssimokawa s = splimp(); 443170374Ssimokawa FWE_LOCK(fwe); 444111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 445170374Ssimokawa FWE_UNLOCK(fwe); 446111942Ssimokawa splx(s); 447113584Ssimokawa 448113584Ssimokawa /* for queue full */ 449111942Ssimokawa if (ifp->if_snd.ifq_head != NULL) 450111942Ssimokawa fwe_start(ifp); 451111942Ssimokawa} 452111942Ssimokawa 453111942Ssimokawastatic void 454103285Sikobfwe_start(struct ifnet *ifp) 455103285Sikob{ 456103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 457103285Sikob int s; 458103285Sikob 459122161Ssimokawa FWEDEBUG(ifp, "starting\n"); 460103285Sikob 461103285Sikob if (fwe->dma_ch < 0) { 462103285Sikob struct mbuf *m = NULL; 463103285Sikob 464122161Ssimokawa FWEDEBUG(ifp, "not ready\n"); 465103285Sikob 466103285Sikob s = splimp(); 467103285Sikob do { 468103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 469103285Sikob if (m != NULL) 470103285Sikob m_freem(m); 471271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 472103285Sikob } while (m != NULL); 473103285Sikob splx(s); 474103285Sikob 475103285Sikob return; 476103285Sikob } 477103285Sikob 478103285Sikob s = splimp(); 479148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 480103285Sikob 481103285Sikob if (ifp->if_snd.ifq_len != 0) 482103285Sikob fwe_as_output(fwe, ifp); 483103285Sikob 484148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 485103285Sikob splx(s); 486103285Sikob} 487103285Sikob 488111942Ssimokawa#define HDR_LEN 4 489111942Ssimokawa#ifndef ETHER_ALIGN 490111942Ssimokawa#define ETHER_ALIGN 2 491103285Sikob#endif 492103285Sikob/* Async. stream output */ 493103285Sikobstatic void 494103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp) 495103285Sikob{ 496103285Sikob struct mbuf *m; 497103285Sikob struct fw_xfer *xfer; 498103285Sikob struct fw_xferq *xferq; 499103285Sikob struct fw_pkt *fp; 500103285Sikob int i = 0; 501103285Sikob 502103285Sikob xfer = NULL; 503103285Sikob xferq = fwe->fd.fc->atq; 504170374Ssimokawa while ((xferq->queued < xferq->maxq - 1) && 505170374Ssimokawa (ifp->if_snd.ifq_head != NULL)) { 506170374Ssimokawa FWE_LOCK(fwe); 507111942Ssimokawa xfer = STAILQ_FIRST(&fwe->xferlist); 508111942Ssimokawa if (xfer == NULL) { 509170374Ssimokawa#if 0 510111942Ssimokawa printf("if_fwe: lack of xfer\n"); 511170374Ssimokawa#endif 512170374Ssimokawa FWE_UNLOCK(fwe); 513170374Ssimokawa break; 514111942Ssimokawa } 515170374Ssimokawa STAILQ_REMOVE_HEAD(&fwe->xferlist, link); 516170374Ssimokawa FWE_UNLOCK(fwe); 517170374Ssimokawa 518103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 519170374Ssimokawa if (m == NULL) { 520170374Ssimokawa FWE_LOCK(fwe); 521170374Ssimokawa STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link); 522170374Ssimokawa FWE_UNLOCK(fwe); 523103285Sikob break; 524170374Ssimokawa } 525127468Ssimokawa BPF_MTAP(ifp, m); 526103285Sikob 527103285Sikob /* keep ip packet alignment for alpha */ 528243857Sglebius M_PREPEND(m, ETHER_ALIGN, M_NOWAIT); 529120660Ssimokawa fp = &xfer->send.hdr; 530129585Sdfr *(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr; 531113584Ssimokawa fp->mode.stream.len = m->m_pkthdr.len; 532103285Sikob xfer->mbuf = m; 533120660Ssimokawa xfer->send.pay_len = m->m_pkthdr.len; 534103285Sikob 535111942Ssimokawa if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) { 536103285Sikob /* error */ 537271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 538103285Sikob /* XXX set error code */ 539103285Sikob fwe_output_callback(xfer); 540103285Sikob } else { 541271849Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 542111942Ssimokawa i++; 543103285Sikob } 544103285Sikob } 545103285Sikob#if 0 546103285Sikob if (i > 1) 547103285Sikob printf("%d queued\n", i); 548103285Sikob#endif 549111942Ssimokawa if (i > 0) 550111942Ssimokawa xferq->start(fwe->fd.fc); 551103285Sikob} 552103285Sikob 553103285Sikob/* Async. stream output */ 554103285Sikobstatic void 555103285Sikobfwe_as_input(struct fw_xferq *xferq) 556103285Sikob{ 557113584Ssimokawa struct mbuf *m, *m0; 558103285Sikob struct ifnet *ifp; 559103285Sikob struct fwe_softc *fwe; 560111942Ssimokawa struct fw_bulkxfer *sxfer; 561111942Ssimokawa struct fw_pkt *fp; 562103285Sikob u_char *c; 563103285Sikob 564103285Sikob fwe = (struct fwe_softc *)xferq->sc; 565147256Sbrooks ifp = fwe->eth_softc.ifp; 566150789Sglebius 567170374Ssimokawa /* We do not need a lock here because the bottom half is serialized */ 568111942Ssimokawa while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) { 569111942Ssimokawa STAILQ_REMOVE_HEAD(&xferq->stvalid, link); 570113584Ssimokawa fp = mtod(sxfer->mbuf, struct fw_pkt *); 571111942Ssimokawa if (fwe->fd.fc->irx_post != NULL) 572111942Ssimokawa fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld); 573111942Ssimokawa m = sxfer->mbuf; 574111942Ssimokawa 575119119Ssimokawa /* insert new rbuf */ 576243857Sglebius sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 577113584Ssimokawa if (m0 != NULL) { 578113584Ssimokawa m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size; 579113584Ssimokawa STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link); 580113584Ssimokawa } else 581170374Ssimokawa printf("%s: m_getcl failed\n", __FUNCTION__); 582111942Ssimokawa 583119119Ssimokawa if (sxfer->resp != 0 || fp->mode.stream.len < 584119119Ssimokawa ETHER_ALIGN + sizeof(struct ether_header)) { 585119119Ssimokawa m_freem(m); 586271849Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 587119119Ssimokawa continue; 588119119Ssimokawa } 589119119Ssimokawa 590111942Ssimokawa m->m_data += HDR_LEN + ETHER_ALIGN; 591170374Ssimokawa c = mtod(m, u_char *); 592132429Ssimokawa m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN; 593103285Sikob m->m_pkthdr.rcvif = ifp; 594103285Sikob#if 0 595122161Ssimokawa FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n" 596103285Sikob "%02x %02x %02x %02x %02x %02x\n" 597103285Sikob "%02x %02x %02x %02x\n" 598103285Sikob "%02x %02x %02x %02x\n" 599103285Sikob "%02x %02x %02x %02x\n" 600103285Sikob "%02x %02x %02x %02x\n", 601103285Sikob c[0], c[1], c[2], c[3], c[4], c[5], 602103285Sikob c[6], c[7], c[8], c[9], c[10], c[11], 603103285Sikob c[12], c[13], c[14], c[15], 604103285Sikob c[16], c[17], c[18], c[19], 605103285Sikob c[20], c[21], c[22], c[23], 606103285Sikob c[20], c[21], c[22], c[23] 607103285Sikob ); 608103285Sikob#endif 609106937Ssam (*ifp->if_input)(ifp, m); 610271849Sglebius if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 611103285Sikob } 612111942Ssimokawa if (STAILQ_FIRST(&xferq->stfree) != NULL) 613111942Ssimokawa fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch); 614103285Sikob} 615103285Sikob 616103285Sikob 617103285Sikobstatic devclass_t fwe_devclass; 618103285Sikob 619103285Sikobstatic device_method_t fwe_methods[] = { 620103285Sikob /* device interface */ 621103285Sikob DEVMETHOD(device_identify, fwe_identify), 622103285Sikob DEVMETHOD(device_probe, fwe_probe), 623103285Sikob DEVMETHOD(device_attach, fwe_attach), 624103285Sikob DEVMETHOD(device_detach, fwe_detach), 625103285Sikob { 0, 0 } 626103285Sikob}; 627103285Sikob 628103285Sikobstatic driver_t fwe_driver = { 629121953Ssimokawa "fwe", 630103285Sikob fwe_methods, 631103285Sikob sizeof(struct fwe_softc), 632103285Sikob}; 633103285Sikob 634103285Sikob 635113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0); 636113506SmdoddMODULE_VERSION(fwe, 1); 637113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1); 638