if_fwe.c revision 112400
1103285Sikob/* 2103285Sikob * Copyright (C) 2002 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 112400 2003-03-19 07:04:11Z simokawa $ 35103285Sikob */ 36103285Sikob 37103285Sikob#include "opt_inet.h" 38103285Sikob 39103285Sikob#include <sys/param.h> 40103285Sikob#include <sys/conf.h> 41103285Sikob#include <sys/kernel.h> 42103285Sikob#include <sys/malloc.h> 43103285Sikob#include <sys/mbuf.h> 44103285Sikob#include <sys/socket.h> 45103285Sikob#include <sys/sockio.h> 46103285Sikob#include <sys/sysctl.h> 47103285Sikob#include <sys/systm.h> 48103285Sikob#include <sys/module.h> 49103285Sikob#include <sys/bus.h> 50103285Sikob 51103285Sikob#include <net/bpf.h> 52103285Sikob#include <net/ethernet.h> 53103285Sikob#include <net/if.h> 54103285Sikob#include <net/if_arp.h> 55103285Sikob#include <net/if_vlan_var.h> 56103285Sikob#include <net/route.h> 57103285Sikob 58103285Sikob#include <netinet/in.h> 59103285Sikob 60103285Sikob#include <dev/firewire/firewire.h> 61103285Sikob#include <dev/firewire/firewirereg.h> 62103285Sikob#include <dev/firewire/if_fwevar.h> 63103285Sikob 64103285Sikob#define FWEDEBUG if (fwedebug) printf 65111942Ssimokawa#define TX_MAX_QUEUE (FWMAXQUEUE - 1) 66111942Ssimokawa#define RX_MAX_QUEUE FWMAXQUEUE 67103285Sikob 68103285Sikob/* network interface */ 69103285Sikobstatic void fwe_start __P((struct ifnet *)); 70103285Sikobstatic int fwe_ioctl __P((struct ifnet *, u_long, caddr_t)); 71103285Sikobstatic void fwe_init __P((void *)); 72103285Sikob 73111942Ssimokawastatic void fwe_output_callback __P((struct fw_xfer *)); 74103285Sikobstatic void fwe_as_output __P((struct fwe_softc *, struct ifnet *)); 75103285Sikobstatic void fwe_as_input __P((struct fw_xferq *)); 76103285Sikob 77103285Sikobstatic int fwedebug = 0; 78103285Sikobstatic int stream_ch = 1; 79103285Sikob 80108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface"); 81103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, ""); 82103285SikobSYSCTL_DECL(_hw_firewire); 83103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0, 84103285Sikob "Ethernet Emulation Subsystem"); 85103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0, 86103285Sikob "Stream channel to use"); 87103285Sikob 88103285Sikob#ifdef DEVICE_POLLING 89103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp) \ 90103285Sikob if (ether_poll_register(func, ifp)) { \ 91103285Sikob struct firewire_comm *fc = (fwe)->fd.fc; \ 92103285Sikob fc->set_intr(fc, 0); \ 93103285Sikob } 94103285Sikob 95103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp) \ 96103285Sikob do { \ 97103285Sikob struct firewire_comm *fc = (fwe)->fd.fc; \ 98103285Sikob ether_poll_deregister(ifp); \ 99103285Sikob fc->set_intr(fc, 1); \ 100103285Sikob } while(0) \ 101103285Sikob 102103285Sikobstatic poll_handler_t fwe_poll; 103103285Sikob 104103285Sikobstatic void 105103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 106103285Sikob{ 107103285Sikob struct fwe_softc *fwe; 108103285Sikob struct firewire_comm *fc; 109103285Sikob 110103285Sikob fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 111103285Sikob fc = fwe->fd.fc; 112103285Sikob if (cmd == POLL_DEREGISTER) { 113103285Sikob /* enable interrupts */ 114103285Sikob fc->set_intr(fc, 1); 115103285Sikob return; 116103285Sikob } 117103285Sikob fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count); 118103285Sikob} 119103285Sikob#else 120103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp) 121103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp) 122103285Sikob#endif 123103285Sikobstatic void 124103285Sikobfwe_identify(driver_t *driver, device_t parent) 125103285Sikob{ 126103285Sikob BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent)); 127103285Sikob} 128103285Sikob 129103285Sikobstatic int 130103285Sikobfwe_probe(device_t dev) 131103285Sikob{ 132103285Sikob device_t pa; 133103285Sikob 134103285Sikob pa = device_get_parent(dev); 135103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 136103285Sikob return(ENXIO); 137103285Sikob } 138103285Sikob 139108281Ssimokawa device_set_desc(dev, "Ethernet over FireWire"); 140103285Sikob return (0); 141103285Sikob} 142103285Sikob 143103285Sikobstatic int 144103285Sikobfwe_attach(device_t dev) 145103285Sikob{ 146103285Sikob struct fwe_softc *fwe; 147103285Sikob struct ifnet *ifp; 148103285Sikob int unit, s; 149103285Sikob u_char *eaddr; 150109814Ssimokawa struct fw_eui64 *eui; 151103285Sikob 152103285Sikob fwe = ((struct fwe_softc *)device_get_softc(dev)); 153103285Sikob unit = device_get_unit(dev); 154103285Sikob 155103285Sikob bzero(fwe, sizeof(struct fwe_softc)); 156103285Sikob /* XXX */ 157103285Sikob fwe->stream_ch = stream_ch; 158103285Sikob fwe->dma_ch = -1; 159103285Sikob 160103285Sikob fwe->fd.fc = device_get_ivars(dev); 161103285Sikob fwe->fd.dev = dev; 162103285Sikob fwe->fd.post_explore = NULL; 163103285Sikob fwe->eth_softc.fwe = fwe; 164103285Sikob 165103285Sikob fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM; 166103285Sikob fwe->pkt_hdr.mode.stream.sy = 0; 167103285Sikob fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 168103285Sikob 169103285Sikob /* generate fake MAC address: first and last 3bytes from eui64 */ 170103285Sikob#define LOCAL (0x02) 171103285Sikob#define GROUP (0x01) 172103285Sikob eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0]; 173109814Ssimokawa 174109814Ssimokawa eui = &fwe->fd.fc->eui; 175109814Ssimokawa eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP; 176109814Ssimokawa eaddr[1] = FW_EUI64_BYTE(eui, 1); 177109814Ssimokawa eaddr[2] = FW_EUI64_BYTE(eui, 2); 178109814Ssimokawa eaddr[3] = FW_EUI64_BYTE(eui, 5); 179109814Ssimokawa eaddr[4] = FW_EUI64_BYTE(eui, 6); 180109814Ssimokawa eaddr[5] = FW_EUI64_BYTE(eui, 7); 181107653Ssimokawa printf("if_fwe%d: Fake Ethernet address: " 182107653Ssimokawa "%02x:%02x:%02x:%02x:%02x:%02x\n", unit, 183103285Sikob eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]); 184103285Sikob 185103285Sikob /* fill the rest and attach interface */ 186103285Sikob ifp = &fwe->fwe_if; 187103285Sikob ifp->if_softc = &fwe->eth_softc; 188103285Sikob 189103285Sikob ifp->if_unit = unit; 190103285Sikob ifp->if_name = "fwe"; 191103285Sikob ifp->if_init = fwe_init; 192103285Sikob ifp->if_output = ether_output; 193103285Sikob ifp->if_start = fwe_start; 194103285Sikob ifp->if_ioctl = fwe_ioctl; 195103285Sikob ifp->if_mtu = ETHERMTU; 196103285Sikob ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 197111942Ssimokawa ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE; 198103285Sikob 199103285Sikob s = splimp(); 200108712Ssimokawa#if __FreeBSD_version >= 500000 201106937Ssam ether_ifattach(ifp, eaddr); 202108712Ssimokawa#else 203108712Ssimokawa ether_ifattach(ifp, 1); 204108712Ssimokawa#endif 205103285Sikob splx(s); 206103285Sikob 207103285Sikob /* Tell the upper layer(s) we support long frames. */ 208103285Sikob ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 209108712Ssimokawa#if __FreeBSD_version >= 500000 210106937Ssam ifp->if_capabilities |= IFCAP_VLAN_MTU; 211108712Ssimokawa#endif 212103285Sikob 213103285Sikob 214103285Sikob FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit); 215103285Sikob return 0; 216103285Sikob} 217103285Sikob 218103285Sikobstatic void 219103285Sikobfwe_stop(struct fwe_softc *fwe) 220103285Sikob{ 221103285Sikob struct firewire_comm *fc; 222103285Sikob struct fw_xferq *xferq; 223103285Sikob struct ifnet *ifp = &fwe->fwe_if; 224111942Ssimokawa struct fw_xfer *xfer, *next; 225111942Ssimokawa int i; 226103285Sikob 227103285Sikob fc = fwe->fd.fc; 228103285Sikob 229103285Sikob FWE_POLL_DEREGISTER(fwe, ifp); 230103285Sikob 231103285Sikob if (fwe->dma_ch >= 0) { 232103285Sikob xferq = fc->ir[fwe->dma_ch]; 233103285Sikob 234103285Sikob if (xferq->flag & FWXFERQ_RUNNING) 235103285Sikob fc->irx_disable(fc, fwe->dma_ch); 236103285Sikob xferq->flag &= 237111942Ssimokawa ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | 238111942Ssimokawa FWXFERQ_EXTBUF | FWXFERQ_HANDLER); 239111942Ssimokawa xferq->hand = NULL; 240111942Ssimokawa 241111942Ssimokawa for (i = 0; i < xferq->bnchunk; i ++) 242111942Ssimokawa m_freem(xferq->bulkxfer[i].mbuf); 243111942Ssimokawa free(xferq->bulkxfer, M_FWE); 244111942Ssimokawa 245111942Ssimokawa for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL; 246111942Ssimokawa xfer = next) { 247111942Ssimokawa next = STAILQ_NEXT(xfer, link); 248111942Ssimokawa fw_xfer_free(xfer); 249111942Ssimokawa } 250111942Ssimokawa STAILQ_INIT(&fwe->xferlist); 251111942Ssimokawa 252111942Ssimokawa xferq->bulkxfer = NULL; 253103285Sikob fwe->dma_ch = -1; 254103285Sikob } 255103285Sikob 256103285Sikob ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 257103285Sikob} 258103285Sikob 259103285Sikobstatic int 260103285Sikobfwe_detach(device_t dev) 261103285Sikob{ 262103285Sikob struct fwe_softc *fwe; 263103285Sikob int s; 264103285Sikob 265103285Sikob fwe = (struct fwe_softc *)device_get_softc(dev); 266103285Sikob s = splimp(); 267103285Sikob 268103285Sikob fwe_stop(fwe); 269108712Ssimokawa#if __FreeBSD_version >= 500000 270106937Ssam ether_ifdetach(&fwe->fwe_if); 271108712Ssimokawa#else 272108712Ssimokawa ether_ifdetach(&fwe->fwe_if, 1); 273108712Ssimokawa#endif 274103285Sikob 275103285Sikob splx(s); 276103285Sikob return 0; 277103285Sikob} 278103285Sikob 279103285Sikobstatic void 280103285Sikobfwe_init(void *arg) 281103285Sikob{ 282103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe; 283103285Sikob struct firewire_comm *fc; 284103285Sikob struct ifnet *ifp = &fwe->fwe_if; 285103285Sikob struct fw_xferq *xferq; 286111942Ssimokawa struct fw_xfer *xfer; 287103285Sikob int i; 288103285Sikob 289103285Sikob FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit); 290103285Sikob 291103285Sikob /* XXX keep promiscoud mode */ 292103285Sikob ifp->if_flags |= IFF_PROMISC; 293103285Sikob 294103285Sikob fc = fwe->fd.fc; 295103285Sikob#define START 0 296103285Sikob if (fwe->dma_ch < 0) { 297103285Sikob xferq = NULL; 298103285Sikob for (i = START; i < fc->nisodma; i ++) { 299103285Sikob xferq = fc->ir[i]; 300103285Sikob if ((xferq->flag & FWXFERQ_OPEN) == 0) 301103285Sikob break; 302103285Sikob } 303103285Sikob 304103285Sikob if (xferq == NULL) { 305103285Sikob printf("no free dma channel\n"); 306103285Sikob return; 307103285Sikob } 308103285Sikob fwe->dma_ch = i; 309103285Sikob fwe->stream_ch = stream_ch; 310103285Sikob fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 311103285Sikob /* allocate DMA channel and init packet mode */ 312111942Ssimokawa xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF; 313112400Ssimokawa xferq->flag &= ~0xff; 314103285Sikob xferq->flag |= fwe->stream_ch & 0xff; 315103285Sikob /* register fwe_input handler */ 316103285Sikob xferq->sc = (caddr_t) fwe; 317103285Sikob xferq->hand = fwe_as_input; 318103285Sikob xferq->flag |= FWXFERQ_HANDLER; 319111942Ssimokawa xferq->bnchunk = RX_MAX_QUEUE; 320111942Ssimokawa xferq->bnpacket = 1; 321111942Ssimokawa xferq->psize = MCLBYTES; 322111942Ssimokawa xferq->queued = 0; 323111942Ssimokawa xferq->bulkxfer = (struct fw_bulkxfer *) malloc( 324111942Ssimokawa sizeof(struct fw_bulkxfer) * xferq->bnchunk, M_FWE, 0); 325111942Ssimokawa if (xferq->bulkxfer == NULL) { 326111942Ssimokawa printf("if_fwe: malloc failed\n"); 327111942Ssimokawa return; 328111942Ssimokawa } 329111942Ssimokawa STAILQ_INIT(&xferq->stvalid); 330111942Ssimokawa STAILQ_INIT(&xferq->stfree); 331111942Ssimokawa STAILQ_INIT(&xferq->stdma); 332111942Ssimokawa xferq->stproc = NULL; 333111942Ssimokawa for (i = 0; i < xferq->bnchunk; i ++) { 334111942Ssimokawa xferq->bulkxfer[i].mbuf = 335111942Ssimokawa#if __FreeBSD_version >= 500000 336111942Ssimokawa m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 337111942Ssimokawa#else 338111942Ssimokawa m_getcl(M_WAIT, MT_DATA, M_PKTHDR); 339111942Ssimokawa#endif 340111942Ssimokawa xferq->bulkxfer[i].buf = 341111942Ssimokawa mtod(xferq->bulkxfer[i].mbuf, char *); 342111942Ssimokawa STAILQ_INSERT_TAIL(&xferq->stfree, 343111942Ssimokawa &xferq->bulkxfer[i], link); 344111942Ssimokawa } 345111942Ssimokawa STAILQ_INIT(&fwe->xferlist); 346111942Ssimokawa for (i = 0; i < TX_MAX_QUEUE; i++) { 347111942Ssimokawa xfer = fw_xfer_alloc(M_FWE); 348111942Ssimokawa if (xfer == NULL) 349111942Ssimokawa break; 350111942Ssimokawa xfer->send.off = 0; 351111942Ssimokawa xfer->spd = 2; 352111942Ssimokawa xfer->fc = fwe->fd.fc; 353111942Ssimokawa xfer->retry_req = fw_asybusy; 354111942Ssimokawa xfer->sc = (caddr_t)fwe; 355111942Ssimokawa xfer->act.hand = fwe_output_callback; 356111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 357111942Ssimokawa } 358103285Sikob } else 359103285Sikob xferq = fc->ir[fwe->dma_ch]; 360103285Sikob 361103285Sikob 362103285Sikob /* start dma */ 363103285Sikob if ((xferq->flag & FWXFERQ_RUNNING) == 0) 364103285Sikob fc->irx_enable(fc, fwe->dma_ch); 365103285Sikob 366103285Sikob ifp->if_flags |= IFF_RUNNING; 367103285Sikob ifp->if_flags &= ~IFF_OACTIVE; 368103285Sikob 369103285Sikob FWE_POLL_REGISTER(fwe_poll, fwe, ifp); 370103285Sikob#if 0 371103285Sikob /* attempt to start output */ 372103285Sikob fwe_start(ifp); 373103285Sikob#endif 374103285Sikob} 375103285Sikob 376103285Sikob 377103285Sikobstatic int 378103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 379103285Sikob{ 380103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 381103285Sikob struct ifstat *ifs = NULL; 382103285Sikob int s, error, len; 383103285Sikob 384103285Sikob switch (cmd) { 385103285Sikob case SIOCSIFFLAGS: 386103285Sikob s = splimp(); 387103285Sikob if (ifp->if_flags & IFF_UP) { 388103285Sikob if (!(ifp->if_flags & IFF_RUNNING)) 389103285Sikob fwe_init(&fwe->eth_softc); 390103285Sikob } else { 391103285Sikob if (ifp->if_flags & IFF_RUNNING) 392103285Sikob fwe_stop(fwe); 393103285Sikob } 394103285Sikob /* XXX keep promiscoud mode */ 395103285Sikob ifp->if_flags |= IFF_PROMISC; 396103285Sikob splx(s); 397103285Sikob break; 398103285Sikob case SIOCADDMULTI: 399103285Sikob case SIOCDELMULTI: 400108712Ssimokawa break; 401103285Sikob 402103285Sikob case SIOCGIFSTATUS: 403103285Sikob s = splimp(); 404103285Sikob ifs = (struct ifstat *)data; 405103285Sikob len = strlen(ifs->ascii); 406103285Sikob if (len < sizeof(ifs->ascii)) 407103285Sikob snprintf(ifs->ascii + len, 408103285Sikob sizeof(ifs->ascii) - len, 409103285Sikob "\tch %d dma %d\n", 410103285Sikob fwe->stream_ch, fwe->dma_ch); 411103285Sikob splx(s); 412108712Ssimokawa break; 413108712Ssimokawa#if __FreeBSD_version >= 500000 414103285Sikob default: 415108712Ssimokawa#else 416108712Ssimokawa case SIOCSIFADDR: 417108712Ssimokawa case SIOCGIFADDR: 418108712Ssimokawa case SIOCSIFMTU: 419108712Ssimokawa#endif 420106937Ssam s = splimp(); 421106937Ssam error = ether_ioctl(ifp, cmd, data); 422106937Ssam splx(s); 423106937Ssam return (error); 424108712Ssimokawa#if __FreeBSD_version < 500000 425108712Ssimokawa default: 426108712Ssimokawa return (EINVAL); 427108712Ssimokawa#endif 428103285Sikob } 429103285Sikob 430103285Sikob return (0); 431103285Sikob} 432103285Sikob 433103285Sikobstatic void 434111942Ssimokawafwe_output_callback(struct fw_xfer *xfer) 435111942Ssimokawa{ 436111942Ssimokawa struct fwe_softc *fwe; 437111942Ssimokawa struct ifnet *ifp; 438111942Ssimokawa int s; 439111942Ssimokawa 440111942Ssimokawa fwe = (struct fwe_softc *)xfer->sc; 441111942Ssimokawa ifp = &fwe->fwe_if; 442111942Ssimokawa /* XXX error check */ 443111942Ssimokawa FWEDEBUG("resp = %d\n", xfer->resp); 444111942Ssimokawa if (xfer->resp != 0) 445111942Ssimokawa ifp->if_oerrors ++; 446111942Ssimokawa 447111942Ssimokawa m_freem(xfer->mbuf); 448111942Ssimokawa xfer->send.buf = NULL; 449111942Ssimokawa#if 0 450111942Ssimokawa fw_xfer_unload(xfer); 451111942Ssimokawa#else 452111942Ssimokawa xfer->state = FWXF_INIT; 453111942Ssimokawa xfer->resp = 0; 454111942Ssimokawa xfer->retry = 0; 455111942Ssimokawa#endif 456111942Ssimokawa s = splimp(); 457111942Ssimokawa STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 458111942Ssimokawa splx(s); 459111942Ssimokawa#if 1 460111942Ssimokawa /* XXX for queue full */ 461111942Ssimokawa if (ifp->if_snd.ifq_head != NULL) 462111942Ssimokawa fwe_start(ifp); 463111942Ssimokawa#endif 464111942Ssimokawa} 465111942Ssimokawa 466111942Ssimokawastatic void 467103285Sikobfwe_start(struct ifnet *ifp) 468103285Sikob{ 469103285Sikob struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 470103285Sikob int s; 471103285Sikob 472103285Sikob#if 1 473103285Sikob FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit); 474103285Sikob 475103285Sikob if (fwe->dma_ch < 0) { 476103285Sikob struct mbuf *m = NULL; 477103285Sikob 478103285Sikob FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit); 479103285Sikob 480103285Sikob s = splimp(); 481103285Sikob do { 482103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 483103285Sikob if (m != NULL) 484103285Sikob m_freem(m); 485103285Sikob ifp->if_oerrors ++; 486103285Sikob } while (m != NULL); 487103285Sikob splx(s); 488103285Sikob 489103285Sikob return; 490103285Sikob } 491103285Sikob 492103285Sikob#endif 493103285Sikob s = splimp(); 494103285Sikob ifp->if_flags |= IFF_OACTIVE; 495103285Sikob 496103285Sikob if (ifp->if_snd.ifq_len != 0) 497103285Sikob fwe_as_output(fwe, ifp); 498103285Sikob 499103285Sikob ifp->if_flags &= ~IFF_OACTIVE; 500103285Sikob splx(s); 501103285Sikob} 502103285Sikob 503111942Ssimokawa#define HDR_LEN 4 504111942Ssimokawa#ifndef ETHER_ALIGN 505111942Ssimokawa#define ETHER_ALIGN 2 506103285Sikob#endif 507103285Sikob/* Async. stream output */ 508103285Sikobstatic void 509103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp) 510103285Sikob{ 511103285Sikob struct mbuf *m; 512103285Sikob struct fw_xfer *xfer; 513103285Sikob struct fw_xferq *xferq; 514103285Sikob struct fw_pkt *fp; 515103285Sikob int i = 0; 516103285Sikob 517103285Sikob xfer = NULL; 518103285Sikob xferq = fwe->fd.fc->atq; 519111942Ssimokawa while (xferq->queued < xferq->maxq - 1) { 520111942Ssimokawa xfer = STAILQ_FIRST(&fwe->xferlist); 521111942Ssimokawa if (xfer == NULL) { 522111942Ssimokawa printf("if_fwe: lack of xfer\n"); 523111942Ssimokawa return; 524111942Ssimokawa } 525103285Sikob IF_DEQUEUE(&ifp->if_snd, m); 526103285Sikob if (m == NULL) 527103285Sikob break; 528111942Ssimokawa STAILQ_REMOVE_HEAD(&fwe->xferlist, link); 529108712Ssimokawa#if __FreeBSD_version >= 500000 530106937Ssam BPF_MTAP(ifp, m); 531108712Ssimokawa#else 532108712Ssimokawa if (ifp->if_bpf != NULL) 533108712Ssimokawa bpf_mtap(ifp, m); 534108712Ssimokawa#endif 535103285Sikob 536103285Sikob /* keep ip packet alignment for alpha */ 537111942Ssimokawa M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT); 538103285Sikob fp = (struct fw_pkt *)&xfer->dst; /* XXX */ 539103285Sikob xfer->dst = *((int32_t *)&fwe->pkt_hdr); 540103285Sikob fp->mode.stream.len = htons(m->m_pkthdr.len); 541103285Sikob xfer->send.buf = (caddr_t) fp; 542103285Sikob xfer->mbuf = m; 543103285Sikob xfer->send.len = m->m_pkthdr.len + HDR_LEN; 544103285Sikob 545111942Ssimokawa if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) { 546103285Sikob /* error */ 547103285Sikob ifp->if_oerrors ++; 548103285Sikob /* XXX set error code */ 549103285Sikob fwe_output_callback(xfer); 550103285Sikob } else { 551103285Sikob ifp->if_opackets ++; 552111942Ssimokawa i++; 553103285Sikob } 554103285Sikob } 555103285Sikob#if 0 556103285Sikob if (i > 1) 557103285Sikob printf("%d queued\n", i); 558103285Sikob#endif 559111942Ssimokawa if (i > 0) 560111942Ssimokawa xferq->start(fwe->fd.fc); 561103285Sikob} 562103285Sikob 563111942Ssimokawa#if 0 564103285Sikob#if __FreeBSD_version >= 500000 565103285Sikobstatic void 566103285Sikobfwe_free(void *buf, void *args) 567103285Sikob{ 568103285Sikob FWEDEBUG("fwe_free:\n"); 569110195Ssimokawa free(buf, M_FW); 570103285Sikob} 571103285Sikob 572103285Sikob#else 573103285Sikobstatic void 574103285Sikobfwe_free(caddr_t buf, u_int size) 575103285Sikob{ 576103285Sikob int *p; 577103285Sikob FWEDEBUG("fwe_free:\n"); 578103285Sikob p = (int *)buf; 579103285Sikob (*p) --; 580103285Sikob if (*p < 1) 581110195Ssimokawa free(buf, M_FW); 582103285Sikob} 583103285Sikob 584103285Sikobstatic void 585103285Sikobfwe_ref(caddr_t buf, u_int size) 586103285Sikob{ 587103285Sikob int *p; 588103285Sikob 589103285Sikob FWEDEBUG("fwe_ref: called\n"); 590103285Sikob p = (int *)buf; 591103285Sikob (*p) ++; 592103285Sikob} 593103285Sikob#endif 594111942Ssimokawa#endif 595103285Sikob 596103285Sikob/* Async. stream output */ 597103285Sikobstatic void 598103285Sikobfwe_as_input(struct fw_xferq *xferq) 599103285Sikob{ 600103285Sikob struct mbuf *m; 601103285Sikob struct ifnet *ifp; 602103285Sikob struct fwe_softc *fwe; 603111942Ssimokawa struct fw_bulkxfer *sxfer; 604111942Ssimokawa struct fw_pkt *fp; 605103285Sikob u_char *c; 606111942Ssimokawa#if __FreeBSD_version < 500000 607111942Ssimokawa struct ether_header *eh; 608111942Ssimokawa#endif 609103285Sikob 610103285Sikob fwe = (struct fwe_softc *)xferq->sc; 611103285Sikob ifp = &fwe->fwe_if; 612103285Sikob#if 0 613103285Sikob FWE_POLL_REGISTER(fwe_poll, fwe, ifp); 614103285Sikob#endif 615111942Ssimokawa while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) { 616111942Ssimokawa STAILQ_REMOVE_HEAD(&xferq->stvalid, link); 617111942Ssimokawa#if 0 618103285Sikob xferq->queued --; 619103285Sikob#endif 620111942Ssimokawa if (sxfer->resp != 0) 621111942Ssimokawa ifp->if_ierrors ++; 622111942Ssimokawa fp = (struct fw_pkt *)sxfer->buf; 623111942Ssimokawa /* XXX */ 624111942Ssimokawa if (fwe->fd.fc->irx_post != NULL) 625111942Ssimokawa fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld); 626111942Ssimokawa m = sxfer->mbuf; 627111942Ssimokawa 628111942Ssimokawa /* insert rbuf */ 629111942Ssimokawa sxfer->mbuf = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 630111942Ssimokawa sxfer->buf = mtod(sxfer->mbuf, char *); 631111942Ssimokawa STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link); 632111942Ssimokawa 633111942Ssimokawa m->m_data += HDR_LEN + ETHER_ALIGN; 634111942Ssimokawa c = mtod(m, char *); 635111942Ssimokawa#if __FreeBSD_version < 500000 636111942Ssimokawa eh = (struct ether_header *)c; 637111942Ssimokawa m->m_data += sizeof(struct ether_header); 638108712Ssimokawa#endif 639111942Ssimokawa m->m_len = m->m_pkthdr.len = 640111942Ssimokawa ntohs(fp->mode.stream.len) - ETHER_ALIGN; 641103285Sikob m->m_pkthdr.rcvif = ifp; 642103285Sikob#if 0 643103285Sikob FWEDEBUG("%02x %02x %02x %02x %02x %02x\n" 644103285Sikob "%02x %02x %02x %02x %02x %02x\n" 645103285Sikob "%02x %02x %02x %02x\n" 646103285Sikob "%02x %02x %02x %02x\n" 647103285Sikob "%02x %02x %02x %02x\n" 648103285Sikob "%02x %02x %02x %02x\n", 649103285Sikob c[0], c[1], c[2], c[3], c[4], c[5], 650103285Sikob c[6], c[7], c[8], c[9], c[10], c[11], 651103285Sikob c[12], c[13], c[14], c[15], 652103285Sikob c[16], c[17], c[18], c[19], 653103285Sikob c[20], c[21], c[22], c[23], 654103285Sikob c[20], c[21], c[22], c[23] 655103285Sikob ); 656103285Sikob#endif 657108712Ssimokawa#if __FreeBSD_version >= 500000 658106937Ssam (*ifp->if_input)(ifp, m); 659108712Ssimokawa#else 660108712Ssimokawa ether_input(ifp, eh, m); 661108712Ssimokawa#endif 662103285Sikob ifp->if_ipackets ++; 663103285Sikob } 664111942Ssimokawa if (STAILQ_FIRST(&xferq->stfree) != NULL) 665111942Ssimokawa fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch); 666103285Sikob} 667103285Sikob 668103285Sikob 669103285Sikobstatic devclass_t fwe_devclass; 670103285Sikob 671103285Sikobstatic device_method_t fwe_methods[] = { 672103285Sikob /* device interface */ 673103285Sikob DEVMETHOD(device_identify, fwe_identify), 674103285Sikob DEVMETHOD(device_probe, fwe_probe), 675103285Sikob DEVMETHOD(device_attach, fwe_attach), 676103285Sikob DEVMETHOD(device_detach, fwe_detach), 677103285Sikob { 0, 0 } 678103285Sikob}; 679103285Sikob 680103285Sikobstatic driver_t fwe_driver = { 681103285Sikob "if_fwe", 682103285Sikob fwe_methods, 683103285Sikob sizeof(struct fwe_softc), 684103285Sikob}; 685103285Sikob 686103285Sikob 687103285SikobDRIVER_MODULE(if_fwe, firewire, fwe_driver, fwe_devclass, 0, 0); 688103285SikobMODULE_VERSION(if_fwe, 1); 689103285SikobMODULE_DEPEND(if_fwe, firewire, 1, 1, 1); 690