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