1 /* 2 * Copyright (c) 2002-2003 3 * Hidetoshi Shimokawa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 18 * 4. Neither the name of the author nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/firewire/if_fwe.c,v 1.27 2004/01/08 14:58:09 simokawa Exp $ 35 * $DragonFly: src/sys/dev/netif/fwe/if_fwe.c,v 1.16 2005/05/25 13:12:22 joerg Exp $ 36 */ 37 38 #include "opt_inet.h" 39 40 #include <sys/param.h> 41 #include <sys/conf.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/socket.h> 46 #include <sys/sockio.h> 47 #include <sys/sysctl.h> 48 #include <sys/systm.h> 49 #include <sys/module.h> 50 #include <sys/bus.h> 51 #include <machine/bus.h> 52 53 #include <net/bpf.h> 54 #include <net/ethernet.h> 55 #include <net/if.h> 56 #include <net/if_arp.h> 57 #ifdef __DragonFly__ 58 #include <net/ifq_var.h> 59 #include <net/vlan/if_vlan_var.h> 60 #include <bus/firewire/firewire.h> 61 #include <bus/firewire/firewirereg.h> 62 #include "if_fwevar.h" 63 #else 64 #include <net/if_vlan_var.h> 65 66 #include <dev/firewire/firewire.h> 67 #include <dev/firewire/firewirereg.h> 68 #include <dev/firewire/if_fwevar.h> 69 #endif 70 71 #define FWEDEBUG if (fwedebug) if_printf 72 #define TX_MAX_QUEUE (FWMAXQUEUE - 1) 73 74 /* network interface */ 75 static void fwe_start (struct ifnet *); 76 static int fwe_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *); 77 static void fwe_init (void *); 78 79 static void fwe_output_callback (struct fw_xfer *); 80 static void fwe_as_output (struct fwe_softc *, struct ifnet *); 81 static void fwe_as_input (struct fw_xferq *); 82 83 static int fwedebug = 0; 84 static int stream_ch = 1; 85 static int tx_speed = 2; 86 static int rx_queue_len = FWMAXQUEUE; 87 88 MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface"); 89 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, ""); 90 SYSCTL_DECL(_hw_firewire); 91 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0, 92 "Ethernet emulation subsystem"); 93 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0, 94 "Stream channel to use"); 95 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0, 96 "Transmission speed"); 97 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len, 98 0, "Length of the receive queue"); 99 100 TUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch); 101 TUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed); 102 TUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len); 103 104 #ifdef DEVICE_POLLING 105 106 static void 107 fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 108 { 109 struct fwe_softc *fwe; 110 struct firewire_comm *fc; 111 112 fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 113 fc = fwe->fd.fc; 114 switch(cmd) { 115 case POLL_REGISTER: 116 /* disable interrupts */ 117 fc->set_intr(fc, 0); 118 break; 119 case POLL_DEREGISTER: 120 /* enable interrupts */ 121 fc->set_intr(fc, 1); 122 break; 123 default: 124 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count); 125 break; 126 } 127 } 128 129 #endif 130 131 static void 132 fwe_identify(driver_t *driver, device_t parent) 133 { 134 BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent)); 135 } 136 137 static int 138 fwe_probe(device_t dev) 139 { 140 device_t pa; 141 142 pa = device_get_parent(dev); 143 if(device_get_unit(dev) != device_get_unit(pa)){ 144 return(ENXIO); 145 } 146 147 device_set_desc(dev, "Ethernet over FireWire"); 148 return (0); 149 } 150 151 static int 152 fwe_attach(device_t dev) 153 { 154 struct fwe_softc *fwe; 155 struct ifnet *ifp; 156 int unit, s; 157 u_char *eaddr; 158 struct fw_eui64 *eui; 159 160 fwe = ((struct fwe_softc *)device_get_softc(dev)); 161 unit = device_get_unit(dev); 162 163 bzero(fwe, sizeof(struct fwe_softc)); 164 /* XXX */ 165 fwe->stream_ch = stream_ch; 166 fwe->dma_ch = -1; 167 168 fwe->fd.fc = device_get_ivars(dev); 169 if (tx_speed < 0) 170 tx_speed = fwe->fd.fc->speed; 171 172 fwe->fd.dev = dev; 173 fwe->fd.post_explore = NULL; 174 fwe->eth_softc.fwe = fwe; 175 176 fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM; 177 fwe->pkt_hdr.mode.stream.sy = 0; 178 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 179 180 /* generate fake MAC address: first and last 3bytes from eui64 */ 181 #define LOCAL (0x02) 182 #define GROUP (0x01) 183 eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0]; 184 185 eui = &fwe->fd.fc->eui; 186 eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP; 187 eaddr[1] = FW_EUI64_BYTE(eui, 1); 188 eaddr[2] = FW_EUI64_BYTE(eui, 2); 189 eaddr[3] = FW_EUI64_BYTE(eui, 5); 190 eaddr[4] = FW_EUI64_BYTE(eui, 6); 191 eaddr[5] = FW_EUI64_BYTE(eui, 7); 192 printf("if_fwe%d: Fake Ethernet address: " 193 "%02x:%02x:%02x:%02x:%02x:%02x\n", unit, 194 eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]); 195 196 /* fill the rest and attach interface */ 197 ifp = &fwe->fwe_if; 198 ifp->if_softc = &fwe->eth_softc; 199 200 if_initname(ifp, device_get_name(dev), unit); 201 ifp->if_init = fwe_init; 202 ifp->if_start = fwe_start; 203 ifp->if_ioctl = fwe_ioctl; 204 ifp->if_capabilities = IFCAP_VLAN_MTU; 205 #ifdef DEVICE_POLLING 206 ifp->if_poll = fwe_poll; 207 #endif 208 ifp->if_mtu = ETHERMTU; 209 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 210 ifq_set_maxlen(&ifp->if_snd, TX_MAX_QUEUE); 211 ifq_set_ready(&ifp->if_snd); 212 213 s = splimp(); 214 ether_ifattach(ifp, eaddr); 215 splx(s); 216 217 /* Tell the upper layer(s) we support long frames. */ 218 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 219 220 221 FWEDEBUG(ifp, "interface created\n"); 222 return 0; 223 } 224 225 static void 226 fwe_stop(struct fwe_softc *fwe) 227 { 228 struct firewire_comm *fc; 229 struct fw_xferq *xferq; 230 struct ifnet *ifp = &fwe->fwe_if; 231 struct fw_xfer *xfer, *next; 232 int i; 233 234 fc = fwe->fd.fc; 235 236 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 237 238 if (fwe->dma_ch >= 0) { 239 xferq = fc->ir[fwe->dma_ch]; 240 241 if (xferq->flag & FWXFERQ_RUNNING) 242 fc->irx_disable(fc, fwe->dma_ch); 243 xferq->flag &= 244 ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM | 245 FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK); 246 xferq->hand = NULL; 247 248 for (i = 0; i < xferq->bnchunk; i ++) 249 m_freem(xferq->bulkxfer[i].mbuf); 250 free(xferq->bulkxfer, M_FWE); 251 252 for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL; 253 xfer = next) { 254 next = STAILQ_NEXT(xfer, link); 255 fw_xfer_free(xfer); 256 } 257 STAILQ_INIT(&fwe->xferlist); 258 259 xferq->bulkxfer = NULL; 260 fwe->dma_ch = -1; 261 } 262 } 263 264 static int 265 fwe_detach(device_t dev) 266 { 267 struct fwe_softc *fwe; 268 int s; 269 270 fwe = (struct fwe_softc *)device_get_softc(dev); 271 s = splimp(); 272 273 fwe_stop(fwe); 274 ether_ifdetach(&fwe->fwe_if); 275 276 splx(s); 277 return 0; 278 } 279 280 static void 281 fwe_init(void *arg) 282 { 283 struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe; 284 struct firewire_comm *fc; 285 struct ifnet *ifp = &fwe->fwe_if; 286 struct fw_xferq *xferq; 287 struct fw_xfer *xfer; 288 struct mbuf *m; 289 int i; 290 291 FWEDEBUG(ifp, "initializing\n"); 292 293 /* XXX keep promiscoud mode */ 294 ifp->if_flags |= IFF_PROMISC; 295 296 fc = fwe->fd.fc; 297 #define START 0 298 if (fwe->dma_ch < 0) { 299 for (i = START; i < fc->nisodma; i ++) { 300 xferq = fc->ir[i]; 301 if ((xferq->flag & FWXFERQ_OPEN) == 0) 302 goto found; 303 } 304 printf("no free dma channel\n"); 305 return; 306 found: 307 fwe->dma_ch = i; 308 fwe->stream_ch = stream_ch; 309 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch; 310 /* allocate DMA channel and init packet mode */ 311 xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF | 312 FWXFERQ_HANDLER | FWXFERQ_STREAM; 313 xferq->flag &= ~0xff; 314 xferq->flag |= fwe->stream_ch & 0xff; 315 /* register fwe_input handler */ 316 xferq->sc = (caddr_t) fwe; 317 xferq->hand = fwe_as_input; 318 xferq->bnchunk = rx_queue_len; 319 xferq->bnpacket = 1; 320 xferq->psize = MCLBYTES; 321 xferq->queued = 0; 322 xferq->buf = NULL; 323 xferq->bulkxfer = (struct fw_bulkxfer *) malloc( 324 sizeof(struct fw_bulkxfer) * xferq->bnchunk, 325 M_FWE, M_WAITOK); 326 if (xferq->bulkxfer == NULL) { 327 printf("if_fwe: malloc failed\n"); 328 return; 329 } 330 STAILQ_INIT(&xferq->stvalid); 331 STAILQ_INIT(&xferq->stfree); 332 STAILQ_INIT(&xferq->stdma); 333 xferq->stproc = NULL; 334 for (i = 0; i < xferq->bnchunk; i ++) { 335 m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR); 336 xferq->bulkxfer[i].mbuf = m; 337 if (m != NULL) { 338 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 339 STAILQ_INSERT_TAIL(&xferq->stfree, 340 &xferq->bulkxfer[i], link); 341 } else 342 printf("fwe_as_input: m_getcl failed\n"); 343 } 344 STAILQ_INIT(&fwe->xferlist); 345 for (i = 0; i < TX_MAX_QUEUE; i++) { 346 xfer = fw_xfer_alloc(M_FWE); 347 if (xfer == NULL) 348 break; 349 xfer->send.spd = tx_speed; 350 xfer->fc = fwe->fd.fc; 351 xfer->retry_req = fw_asybusy; 352 xfer->sc = (caddr_t)fwe; 353 xfer->act.hand = fwe_output_callback; 354 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 355 } 356 } else 357 xferq = fc->ir[fwe->dma_ch]; 358 359 360 /* start dma */ 361 if ((xferq->flag & FWXFERQ_RUNNING) == 0) 362 fc->irx_enable(fc, fwe->dma_ch); 363 364 ifp->if_flags |= IFF_RUNNING; 365 ifp->if_flags &= ~IFF_OACTIVE; 366 } 367 368 369 static int 370 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 371 { 372 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 373 struct ifstat *ifs = NULL; 374 int s, error, len; 375 376 switch (cmd) { 377 case SIOCSIFFLAGS: 378 s = splimp(); 379 if (ifp->if_flags & IFF_UP) { 380 if (!(ifp->if_flags & IFF_RUNNING)) 381 fwe_init(&fwe->eth_softc); 382 } else { 383 if (ifp->if_flags & IFF_RUNNING) 384 fwe_stop(fwe); 385 } 386 /* XXX keep promiscoud mode */ 387 ifp->if_flags |= IFF_PROMISC; 388 splx(s); 389 break; 390 case SIOCADDMULTI: 391 case SIOCDELMULTI: 392 break; 393 394 case SIOCGIFSTATUS: 395 s = splimp(); 396 ifs = (struct ifstat *)data; 397 len = strlen(ifs->ascii); 398 if (len < sizeof(ifs->ascii)) 399 snprintf(ifs->ascii + len, 400 sizeof(ifs->ascii) - len, 401 "\tch %d dma %d\n", 402 fwe->stream_ch, fwe->dma_ch); 403 splx(s); 404 break; 405 default: 406 s = splimp(); 407 error = ether_ioctl(ifp, cmd, data); 408 splx(s); 409 return (error); 410 } 411 412 return (0); 413 } 414 415 static void 416 fwe_output_callback(struct fw_xfer *xfer) 417 { 418 struct fwe_softc *fwe; 419 struct ifnet *ifp; 420 int s; 421 422 fwe = (struct fwe_softc *)xfer->sc; 423 ifp = &fwe->fwe_if; 424 /* XXX error check */ 425 FWEDEBUG(ifp, "resp = %d\n", xfer->resp); 426 if (xfer->resp != 0) 427 ifp->if_oerrors ++; 428 429 m_freem(xfer->mbuf); 430 fw_xfer_unload(xfer); 431 432 s = splimp(); 433 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link); 434 splx(s); 435 436 /* for queue full */ 437 if (!ifq_is_empty(&ifp->if_snd)) 438 fwe_start(ifp); 439 } 440 441 static void 442 fwe_start(struct ifnet *ifp) 443 { 444 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe; 445 int s; 446 447 FWEDEBUG(ifp, "starting\n"); 448 449 if (fwe->dma_ch < 0) { 450 FWEDEBUG(ifp, "not ready\n"); 451 452 s = splimp(); 453 ifq_purge(&ifp->if_snd); 454 splx(s); 455 456 return; 457 } 458 459 s = splimp(); 460 ifp->if_flags |= IFF_OACTIVE; 461 462 if (!ifq_is_empty(&ifp->if_snd)) 463 fwe_as_output(fwe, ifp); 464 465 ifp->if_flags &= ~IFF_OACTIVE; 466 splx(s); 467 } 468 469 #define HDR_LEN 4 470 #ifndef ETHER_ALIGN 471 #define ETHER_ALIGN 2 472 #endif 473 /* Async. stream output */ 474 static void 475 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp) 476 { 477 struct mbuf *m; 478 struct fw_xfer *xfer; 479 struct fw_xferq *xferq; 480 struct fw_pkt *fp; 481 int i = 0; 482 483 xfer = NULL; 484 xferq = fwe->fd.fc->atq; 485 while (xferq->queued < xferq->maxq - 1) { 486 xfer = STAILQ_FIRST(&fwe->xferlist); 487 if (xfer == NULL) { 488 printf("if_fwe: lack of xfer\n"); 489 return; 490 } 491 m = ifq_dequeue(&ifp->if_snd); 492 if (m == NULL) 493 break; 494 STAILQ_REMOVE_HEAD(&fwe->xferlist, link); 495 BPF_MTAP(ifp, m); 496 497 /* keep ip packet alignment for alpha */ 498 M_PREPEND(m, ETHER_ALIGN, MB_DONTWAIT); 499 fp = &xfer->send.hdr; 500 *(u_int32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr; 501 fp->mode.stream.len = m->m_pkthdr.len; 502 xfer->mbuf = m; 503 xfer->send.pay_len = m->m_pkthdr.len; 504 505 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) { 506 /* error */ 507 ifp->if_oerrors ++; 508 /* XXX set error code */ 509 fwe_output_callback(xfer); 510 } else { 511 ifp->if_opackets ++; 512 i++; 513 } 514 } 515 #if 0 516 if (i > 1) 517 printf("%d queued\n", i); 518 #endif 519 if (i > 0) 520 xferq->start(fwe->fd.fc); 521 } 522 523 /* Async. stream output */ 524 static void 525 fwe_as_input(struct fw_xferq *xferq) 526 { 527 struct mbuf *m, *m0; 528 struct ifnet *ifp; 529 struct fwe_softc *fwe; 530 struct fw_bulkxfer *sxfer; 531 struct fw_pkt *fp; 532 u_char *c; 533 534 fwe = (struct fwe_softc *)xferq->sc; 535 ifp = &fwe->fwe_if; 536 while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) { 537 STAILQ_REMOVE_HEAD(&xferq->stvalid, link); 538 fp = mtod(sxfer->mbuf, struct fw_pkt *); 539 if (fwe->fd.fc->irx_post != NULL) 540 fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld); 541 m = sxfer->mbuf; 542 543 /* insert new rbuf */ 544 sxfer->mbuf = m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR); 545 if (m0 != NULL) { 546 m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size; 547 STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link); 548 } else 549 printf("fwe_as_input: m_getcl failed\n"); 550 551 if (sxfer->resp != 0 || fp->mode.stream.len < 552 ETHER_ALIGN + sizeof(struct ether_header)) { 553 m_freem(m); 554 ifp->if_ierrors ++; 555 continue; 556 } 557 558 m->m_data += HDR_LEN + ETHER_ALIGN; 559 c = mtod(m, char *); 560 m->m_len = m->m_pkthdr.len = 561 fp->mode.stream.len - ETHER_ALIGN; 562 m->m_pkthdr.rcvif = ifp; 563 #if 0 564 FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n" 565 "%02x %02x %02x %02x %02x %02x\n" 566 "%02x %02x %02x %02x\n" 567 "%02x %02x %02x %02x\n" 568 "%02x %02x %02x %02x\n" 569 "%02x %02x %02x %02x\n", 570 c[0], c[1], c[2], c[3], c[4], c[5], 571 c[6], c[7], c[8], c[9], c[10], c[11], 572 c[12], c[13], c[14], c[15], 573 c[16], c[17], c[18], c[19], 574 c[20], c[21], c[22], c[23], 575 c[20], c[21], c[22], c[23] 576 ); 577 #endif 578 (*ifp->if_input)(ifp, m); 579 ifp->if_ipackets ++; 580 } 581 if (STAILQ_FIRST(&xferq->stfree) != NULL) 582 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch); 583 } 584 585 586 static devclass_t fwe_devclass; 587 588 static device_method_t fwe_methods[] = { 589 /* device interface */ 590 DEVMETHOD(device_identify, fwe_identify), 591 DEVMETHOD(device_probe, fwe_probe), 592 DEVMETHOD(device_attach, fwe_attach), 593 DEVMETHOD(device_detach, fwe_detach), 594 { 0, 0 } 595 }; 596 597 static driver_t fwe_driver = { 598 "fwe", 599 fwe_methods, 600 sizeof(struct fwe_softc), 601 }; 602 603 604 DECLARE_DUMMY_MODULE(fwe); 605 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0); 606 MODULE_VERSION(fwe, 1); 607 MODULE_DEPEND(fwe, firewire, 1, 1, 1); 608