1 /*- 2 * Copyright (c) 2004 3 * Doug Rabson 4 * Copyright (c) 2002-2003 5 * Hidetoshi Shimokawa. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * 18 * This product includes software developed by Hidetoshi Shimokawa. 19 * 20 * 4. Neither the name of the author nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $FreeBSD$ 37 */ 38 39 #ifdef HAVE_KERNEL_OPTION_HEADERS 40 #include "opt_device_polling.h" 41 #include "opt_inet.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/sockio.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/taskqueue.h> 53 #include <sys/module.h> 54 #include <sys/bus.h> 55 #include <machine/bus.h> 56 57 #include <net/bpf.h> 58 #include <net/if.h> 59 #include <net/if_var.h> 60 #include <net/firewire.h> 61 #include <net/if_arp.h> 62 #include <net/if_types.h> 63 #ifdef __DragonFly__ 64 #include <bus/firewire/firewire.h> 65 #include <bus/firewire/firewirereg.h> 66 #include "if_fwipvar.h" 67 #else 68 #include <dev/firewire/firewire.h> 69 #include <dev/firewire/firewirereg.h> 70 #include <dev/firewire/iec13213.h> 71 #include <dev/firewire/if_fwipvar.h> 72 #endif 73 74 /* 75 * We really need a mechanism for allocating regions in the FIFO 76 * address space. We pick a address in the OHCI controller's 'middle' 77 * address space. This means that the controller will automatically 78 * send responses for us, which is fine since we don't have any 79 * important information to put in the response anyway. 80 */ 81 #define INET_FIFO 0xfffe00000000LL 82 83 #define FWIPDEBUG if (fwipdebug) if_printf 84 #define TX_MAX_QUEUE (FWMAXQUEUE - 1) 85 86 /* network interface */ 87 static void fwip_start (struct ifnet *); 88 static int fwip_ioctl (struct ifnet *, u_long, caddr_t); 89 static void fwip_init (void *); 90 91 static void fwip_post_busreset (void *); 92 static void fwip_output_callback (struct fw_xfer *); 93 static void fwip_async_output (struct fwip_softc *, struct ifnet *); 94 static void fwip_start_send (void *, int); 95 static void fwip_stream_input (struct fw_xferq *); 96 static void fwip_unicast_input(struct fw_xfer *); 97 98 static int fwipdebug = 0; 99 static int broadcast_channel = 0xc0 | 0x1f; /* tag | channel(XXX) */ 100 static int tx_speed = 2; 101 static int rx_queue_len = FWMAXQUEUE; 102 103 static MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface"); 104 SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, ""); 105 SYSCTL_DECL(_hw_firewire); 106 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0, 107 "Firewire ip subsystem"); 108 SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len, 109 0, "Length of the receive queue"); 110 111 #ifdef DEVICE_POLLING 112 static poll_handler_t fwip_poll; 113 114 static int 115 fwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 116 { 117 struct fwip_softc *fwip; 118 struct firewire_comm *fc; 119 120 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 121 return (0); 122 123 fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip; 124 fc = fwip->fd.fc; 125 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count); 126 return (0); 127 } 128 #endif /* DEVICE_POLLING */ 129 130 static void 131 fwip_identify(driver_t *driver, device_t parent) 132 { 133 BUS_ADD_CHILD(parent, 0, "fwip", device_get_unit(parent)); 134 } 135 136 static int 137 fwip_probe(device_t dev) 138 { 139 device_t pa; 140 141 pa = device_get_parent(dev); 142 if(device_get_unit(dev) != device_get_unit(pa)){ 143 return(ENXIO); 144 } 145 146 device_set_desc(dev, "IP over FireWire"); 147 return (0); 148 } 149 150 static int 151 fwip_attach(device_t dev) 152 { 153 struct fwip_softc *fwip; 154 struct ifnet *ifp; 155 int unit, s; 156 struct fw_hwaddr *hwaddr; 157 158 fwip = ((struct fwip_softc *)device_get_softc(dev)); 159 unit = device_get_unit(dev); 160 ifp = fwip->fw_softc.fwip_ifp = if_alloc(IFT_IEEE1394); 161 if (ifp == NULL) 162 return (ENOSPC); 163 164 mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF); 165 /* XXX */ 166 fwip->dma_ch = -1; 167 168 fwip->fd.fc = device_get_ivars(dev); 169 if (tx_speed < 0) 170 tx_speed = fwip->fd.fc->speed; 171 172 fwip->fd.dev = dev; 173 fwip->fd.post_explore = NULL; 174 fwip->fd.post_busreset = fwip_post_busreset; 175 fwip->fw_softc.fwip = fwip; 176 TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip); 177 178 /* 179 * Encode our hardware the way that arp likes it. 180 */ 181 hwaddr = &IFP2FWC(fwip->fw_softc.fwip_ifp)->fc_hwaddr; 182 hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi); 183 hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo); 184 hwaddr->sender_max_rec = fwip->fd.fc->maxrec; 185 hwaddr->sspd = fwip->fd.fc->speed; 186 hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32)); 187 hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO); 188 189 /* fill the rest and attach interface */ 190 ifp->if_softc = &fwip->fw_softc; 191 192 if_initname(ifp, device_get_name(dev), unit); 193 ifp->if_init = fwip_init; 194 ifp->if_start = fwip_start; 195 ifp->if_ioctl = fwip_ioctl; 196 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 197 ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE; 198 #ifdef DEVICE_POLLING 199 ifp->if_capabilities |= IFCAP_POLLING; 200 #endif 201 202 s = splimp(); 203 firewire_ifattach(ifp, hwaddr); 204 splx(s); 205 206 FWIPDEBUG(ifp, "interface created\n"); 207 return 0; 208 } 209 210 static void 211 fwip_stop(struct fwip_softc *fwip) 212 { 213 struct firewire_comm *fc; 214 struct fw_xferq *xferq; 215 struct ifnet *ifp = fwip->fw_softc.fwip_ifp; 216 struct fw_xfer *xfer, *next; 217 int i; 218 219 fc = fwip->fd.fc; 220 221 if (fwip->dma_ch >= 0) { 222 xferq = fc->ir[fwip->dma_ch]; 223 224 if (xferq->flag & FWXFERQ_RUNNING) 225 fc->irx_disable(fc, fwip->dma_ch); 226 xferq->flag &= 227 ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM | 228 FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK); 229 xferq->hand = NULL; 230 231 for (i = 0; i < xferq->bnchunk; i ++) 232 m_freem(xferq->bulkxfer[i].mbuf); 233 free(xferq->bulkxfer, M_FWIP); 234 235 fw_bindremove(fc, &fwip->fwb); 236 for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL; 237 xfer = next) { 238 next = STAILQ_NEXT(xfer, link); 239 fw_xfer_free(xfer); 240 } 241 242 for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL; 243 xfer = next) { 244 next = STAILQ_NEXT(xfer, link); 245 fw_xfer_free(xfer); 246 } 247 STAILQ_INIT(&fwip->xferlist); 248 249 xferq->bulkxfer = NULL; 250 fwip->dma_ch = -1; 251 } 252 253 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 254 } 255 256 static int 257 fwip_detach(device_t dev) 258 { 259 struct fwip_softc *fwip; 260 struct ifnet *ifp; 261 int s; 262 263 fwip = (struct fwip_softc *)device_get_softc(dev); 264 ifp = fwip->fw_softc.fwip_ifp; 265 266 #ifdef DEVICE_POLLING 267 if (ifp->if_capenable & IFCAP_POLLING) 268 ether_poll_deregister(ifp); 269 #endif 270 271 s = splimp(); 272 273 fwip_stop(fwip); 274 firewire_ifdetach(ifp); 275 if_free(ifp); 276 mtx_destroy(&fwip->mtx); 277 278 splx(s); 279 return 0; 280 } 281 282 static void 283 fwip_init(void *arg) 284 { 285 struct fwip_softc *fwip = ((struct fwip_eth_softc *)arg)->fwip; 286 struct firewire_comm *fc; 287 struct ifnet *ifp = fwip->fw_softc.fwip_ifp; 288 struct fw_xferq *xferq; 289 struct fw_xfer *xfer; 290 struct mbuf *m; 291 int i; 292 293 FWIPDEBUG(ifp, "initializing\n"); 294 295 fc = fwip->fd.fc; 296 #define START 0 297 if (fwip->dma_ch < 0) { 298 fwip->dma_ch = fw_open_isodma(fc, /* tx */0); 299 if (fwip->dma_ch < 0) 300 return; 301 xferq = fc->ir[fwip->dma_ch]; 302 xferq->flag |= FWXFERQ_EXTBUF | 303 FWXFERQ_HANDLER | FWXFERQ_STREAM; 304 xferq->flag &= ~0xff; 305 xferq->flag |= broadcast_channel & 0xff; 306 /* register fwip_input handler */ 307 xferq->sc = (caddr_t) fwip; 308 xferq->hand = fwip_stream_input; 309 xferq->bnchunk = rx_queue_len; 310 xferq->bnpacket = 1; 311 xferq->psize = MCLBYTES; 312 xferq->queued = 0; 313 xferq->buf = NULL; 314 xferq->bulkxfer = (struct fw_bulkxfer *) malloc( 315 sizeof(struct fw_bulkxfer) * xferq->bnchunk, 316 M_FWIP, M_WAITOK); 317 if (xferq->bulkxfer == NULL) { 318 printf("if_fwip: malloc failed\n"); 319 return; 320 } 321 STAILQ_INIT(&xferq->stvalid); 322 STAILQ_INIT(&xferq->stfree); 323 STAILQ_INIT(&xferq->stdma); 324 xferq->stproc = NULL; 325 for (i = 0; i < xferq->bnchunk; i ++) { 326 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 327 xferq->bulkxfer[i].mbuf = m; 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 } 332 333 fwip->fwb.start = INET_FIFO; 334 fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */ 335 336 /* pre-allocate xfer */ 337 STAILQ_INIT(&fwip->fwb.xferlist); 338 for (i = 0; i < rx_queue_len; i ++) { 339 xfer = fw_xfer_alloc(M_FWIP); 340 if (xfer == NULL) 341 break; 342 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 343 xfer->recv.payload = mtod(m, uint32_t *); 344 xfer->recv.pay_len = MCLBYTES; 345 xfer->hand = fwip_unicast_input; 346 xfer->fc = fc; 347 xfer->sc = (caddr_t)fwip; 348 xfer->mbuf = m; 349 STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link); 350 } 351 fw_bindadd(fc, &fwip->fwb); 352 353 STAILQ_INIT(&fwip->xferlist); 354 for (i = 0; i < TX_MAX_QUEUE; i++) { 355 xfer = fw_xfer_alloc(M_FWIP); 356 if (xfer == NULL) 357 break; 358 xfer->send.spd = tx_speed; 359 xfer->fc = fwip->fd.fc; 360 xfer->sc = (caddr_t)fwip; 361 xfer->hand = fwip_output_callback; 362 STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link); 363 } 364 } else 365 xferq = fc->ir[fwip->dma_ch]; 366 367 fwip->last_dest.hi = 0; 368 fwip->last_dest.lo = 0; 369 370 /* start dma */ 371 if ((xferq->flag & FWXFERQ_RUNNING) == 0) 372 fc->irx_enable(fc, fwip->dma_ch); 373 374 ifp->if_drv_flags |= IFF_DRV_RUNNING; 375 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 376 377 #if 0 378 /* attempt to start output */ 379 fwip_start(ifp); 380 #endif 381 } 382 383 static int 384 fwip_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 385 { 386 struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip; 387 int s, error; 388 389 switch (cmd) { 390 case SIOCSIFFLAGS: 391 s = splimp(); 392 if (ifp->if_flags & IFF_UP) { 393 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 394 fwip_init(&fwip->fw_softc); 395 } else { 396 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 397 fwip_stop(fwip); 398 } 399 splx(s); 400 break; 401 case SIOCADDMULTI: 402 case SIOCDELMULTI: 403 break; 404 case SIOCSIFCAP: 405 #ifdef DEVICE_POLLING 406 { 407 struct ifreq *ifr = (struct ifreq *) data; 408 struct firewire_comm *fc = fwip->fd.fc; 409 410 if (ifr->ifr_reqcap & IFCAP_POLLING && 411 !(ifp->if_capenable & IFCAP_POLLING)) { 412 error = ether_poll_register(fwip_poll, ifp); 413 if (error) 414 return(error); 415 /* Disable interrupts */ 416 fc->set_intr(fc, 0); 417 ifp->if_capenable |= IFCAP_POLLING | 418 IFCAP_POLLING_NOCOUNT; 419 return (error); 420 421 } 422 if (!(ifr->ifr_reqcap & IFCAP_POLLING) && 423 ifp->if_capenable & IFCAP_POLLING) { 424 error = ether_poll_deregister(ifp); 425 /* Enable interrupts. */ 426 fc->set_intr(fc, 1); 427 ifp->if_capenable &= ~IFCAP_POLLING; 428 ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT; 429 return (error); 430 } 431 } 432 #endif /* DEVICE_POLLING */ 433 break; 434 default: 435 s = splimp(); 436 error = firewire_ioctl(ifp, cmd, data); 437 splx(s); 438 return (error); 439 } 440 441 return (0); 442 } 443 444 static void 445 fwip_post_busreset(void *arg) 446 { 447 struct fwip_softc *fwip = arg; 448 struct crom_src *src; 449 struct crom_chunk *root; 450 451 src = fwip->fd.fc->crom_src; 452 root = fwip->fd.fc->crom_root; 453 454 /* RFC2734 IPv4 over IEEE1394 */ 455 bzero(&fwip->unit4, sizeof(struct crom_chunk)); 456 crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR); 457 crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF); 458 crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA"); 459 crom_add_entry(&fwip->unit4, CSRKEY_VER, 1); 460 crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4"); 461 462 /* RFC3146 IPv6 over IEEE1394 */ 463 bzero(&fwip->unit6, sizeof(struct crom_chunk)); 464 crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR); 465 crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF); 466 crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA"); 467 crom_add_entry(&fwip->unit6, CSRKEY_VER, 2); 468 crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6"); 469 470 fwip->last_dest.hi = 0; 471 fwip->last_dest.lo = 0; 472 firewire_busreset(fwip->fw_softc.fwip_ifp); 473 } 474 475 static void 476 fwip_output_callback(struct fw_xfer *xfer) 477 { 478 struct fwip_softc *fwip; 479 struct ifnet *ifp; 480 int s; 481 482 fwip = (struct fwip_softc *)xfer->sc; 483 ifp = fwip->fw_softc.fwip_ifp; 484 /* XXX error check */ 485 FWIPDEBUG(ifp, "resp = %d\n", xfer->resp); 486 if (xfer->resp != 0) 487 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 488 489 m_freem(xfer->mbuf); 490 fw_xfer_unload(xfer); 491 492 s = splimp(); 493 FWIP_LOCK(fwip); 494 STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link); 495 FWIP_UNLOCK(fwip); 496 splx(s); 497 498 /* for queue full */ 499 if (ifp->if_snd.ifq_head != NULL) { 500 fwip_start(ifp); 501 } 502 } 503 504 static void 505 fwip_start(struct ifnet *ifp) 506 { 507 struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip; 508 int s; 509 510 FWIPDEBUG(ifp, "starting\n"); 511 512 if (fwip->dma_ch < 0) { 513 struct mbuf *m = NULL; 514 515 FWIPDEBUG(ifp, "not ready\n"); 516 517 s = splimp(); 518 do { 519 IF_DEQUEUE(&ifp->if_snd, m); 520 if (m != NULL) 521 m_freem(m); 522 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 523 } while (m != NULL); 524 splx(s); 525 526 return; 527 } 528 529 s = splimp(); 530 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 531 532 if (ifp->if_snd.ifq_len != 0) 533 fwip_async_output(fwip, ifp); 534 535 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 536 splx(s); 537 } 538 539 /* Async. stream output */ 540 static void 541 fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp) 542 { 543 struct firewire_comm *fc = fwip->fd.fc; 544 struct mbuf *m; 545 struct m_tag *mtag; 546 struct fw_hwaddr *destfw; 547 struct fw_xfer *xfer; 548 struct fw_xferq *xferq; 549 struct fw_pkt *fp; 550 uint16_t nodeid; 551 int error; 552 int i = 0; 553 554 xfer = NULL; 555 xferq = fc->atq; 556 while ((xferq->queued < xferq->maxq - 1) && 557 (ifp->if_snd.ifq_head != NULL)) { 558 FWIP_LOCK(fwip); 559 xfer = STAILQ_FIRST(&fwip->xferlist); 560 if (xfer == NULL) { 561 FWIP_UNLOCK(fwip); 562 #if 0 563 printf("if_fwip: lack of xfer\n"); 564 #endif 565 break; 566 } 567 STAILQ_REMOVE_HEAD(&fwip->xferlist, link); 568 FWIP_UNLOCK(fwip); 569 570 IF_DEQUEUE(&ifp->if_snd, m); 571 if (m == NULL) { 572 FWIP_LOCK(fwip); 573 STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link); 574 FWIP_UNLOCK(fwip); 575 break; 576 } 577 578 /* 579 * Dig out the link-level address which 580 * firewire_output got via arp or neighbour 581 * discovery. If we don't have a link-level address, 582 * just stick the thing on the broadcast channel. 583 */ 584 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0); 585 if (mtag == NULL) 586 destfw = 0; 587 else 588 destfw = (struct fw_hwaddr *) (mtag + 1); 589 590 591 /* 592 * We don't do any bpf stuff here - the generic code 593 * in firewire_output gives the packet to bpf before 594 * it adds the link-level encapsulation. 595 */ 596 597 /* 598 * Put the mbuf in the xfer early in case we hit an 599 * error case below - fwip_output_callback will free 600 * the mbuf. 601 */ 602 xfer->mbuf = m; 603 604 /* 605 * We use the arp result (if any) to add a suitable firewire 606 * packet header before handing off to the bus. 607 */ 608 fp = &xfer->send.hdr; 609 nodeid = FWLOCALBUS | fc->nodeid; 610 if ((m->m_flags & M_BCAST) || !destfw) { 611 /* 612 * Broadcast packets are sent as GASP packets with 613 * specifier ID 0x00005e, version 1 on the broadcast 614 * channel. To be conservative, we send at the 615 * slowest possible speed. 616 */ 617 uint32_t *p; 618 619 M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT); 620 p = mtod(m, uint32_t *); 621 fp->mode.stream.len = m->m_pkthdr.len; 622 fp->mode.stream.chtag = broadcast_channel; 623 fp->mode.stream.tcode = FWTCODE_STREAM; 624 fp->mode.stream.sy = 0; 625 xfer->send.spd = 0; 626 p[0] = htonl(nodeid << 16); 627 p[1] = htonl((0x5e << 24) | 1); 628 } else { 629 /* 630 * Unicast packets are sent as block writes to the 631 * target's unicast fifo address. If we can't 632 * find the node address, we just give up. We 633 * could broadcast it but that might overflow 634 * the packet size limitations due to the 635 * extra GASP header. Note: the hardware 636 * address is stored in network byte order to 637 * make life easier for ARP. 638 */ 639 struct fw_device *fd; 640 struct fw_eui64 eui; 641 642 eui.hi = ntohl(destfw->sender_unique_ID_hi); 643 eui.lo = ntohl(destfw->sender_unique_ID_lo); 644 if (fwip->last_dest.hi != eui.hi || 645 fwip->last_dest.lo != eui.lo) { 646 fd = fw_noderesolve_eui64(fc, &eui); 647 if (!fd) { 648 /* error */ 649 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 650 /* XXX set error code */ 651 fwip_output_callback(xfer); 652 continue; 653 654 } 655 fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst; 656 fwip->last_hdr.mode.wreqb.tlrt = 0; 657 fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB; 658 fwip->last_hdr.mode.wreqb.pri = 0; 659 fwip->last_hdr.mode.wreqb.src = nodeid; 660 fwip->last_hdr.mode.wreqb.dest_hi = 661 ntohs(destfw->sender_unicast_FIFO_hi); 662 fwip->last_hdr.mode.wreqb.dest_lo = 663 ntohl(destfw->sender_unicast_FIFO_lo); 664 fwip->last_hdr.mode.wreqb.extcode = 0; 665 fwip->last_dest = eui; 666 } 667 668 fp->mode.wreqb = fwip->last_hdr.mode.wreqb; 669 fp->mode.wreqb.len = m->m_pkthdr.len; 670 xfer->send.spd = min(destfw->sspd, fc->speed); 671 } 672 673 xfer->send.pay_len = m->m_pkthdr.len; 674 675 error = fw_asyreq(fc, -1, xfer); 676 if (error == EAGAIN) { 677 /* 678 * We ran out of tlabels - requeue the packet 679 * for later transmission. 680 */ 681 xfer->mbuf = 0; 682 FWIP_LOCK(fwip); 683 STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link); 684 FWIP_UNLOCK(fwip); 685 IF_PREPEND(&ifp->if_snd, m); 686 break; 687 } 688 if (error) { 689 /* error */ 690 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 691 /* XXX set error code */ 692 fwip_output_callback(xfer); 693 continue; 694 } else { 695 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 696 i++; 697 } 698 } 699 #if 0 700 if (i > 1) 701 printf("%d queued\n", i); 702 #endif 703 if (i > 0) 704 xferq->start(fc); 705 } 706 707 static void 708 fwip_start_send (void *arg, int count) 709 { 710 struct fwip_softc *fwip = arg; 711 712 fwip->fd.fc->atq->start(fwip->fd.fc); 713 } 714 715 /* Async. stream output */ 716 static void 717 fwip_stream_input(struct fw_xferq *xferq) 718 { 719 struct mbuf *m, *m0; 720 struct m_tag *mtag; 721 struct ifnet *ifp; 722 struct fwip_softc *fwip; 723 struct fw_bulkxfer *sxfer; 724 struct fw_pkt *fp; 725 uint16_t src; 726 uint32_t *p; 727 728 729 fwip = (struct fwip_softc *)xferq->sc; 730 ifp = fwip->fw_softc.fwip_ifp; 731 732 while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) { 733 STAILQ_REMOVE_HEAD(&xferq->stvalid, link); 734 fp = mtod(sxfer->mbuf, struct fw_pkt *); 735 if (fwip->fd.fc->irx_post != NULL) 736 fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld); 737 m = sxfer->mbuf; 738 739 /* insert new rbuf */ 740 sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 741 if (m0 != NULL) { 742 m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size; 743 STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link); 744 } else 745 printf("fwip_as_input: m_getcl failed\n"); 746 747 /* 748 * We must have a GASP header - leave the 749 * encapsulation sanity checks to the generic 750 * code. Remeber that we also have the firewire async 751 * stream header even though that isn't accounted for 752 * in mode.stream.len. 753 */ 754 if (sxfer->resp != 0 || fp->mode.stream.len < 755 2*sizeof(uint32_t)) { 756 m_freem(m); 757 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 758 continue; 759 } 760 m->m_len = m->m_pkthdr.len = fp->mode.stream.len 761 + sizeof(fp->mode.stream); 762 763 /* 764 * If we received the packet on the broadcast channel, 765 * mark it as broadcast, otherwise we assume it must 766 * be multicast. 767 */ 768 if (fp->mode.stream.chtag == broadcast_channel) 769 m->m_flags |= M_BCAST; 770 else 771 m->m_flags |= M_MCAST; 772 773 /* 774 * Make sure we recognise the GASP specifier and 775 * version. 776 */ 777 p = mtod(m, uint32_t *); 778 if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e 779 || (ntohl(p[2]) & 0xffffff) != 1) { 780 FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n", 781 ntohl(p[1]), ntohl(p[2])); 782 m_freem(m); 783 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 784 continue; 785 } 786 787 /* 788 * Record the sender ID for possible BPF usage. 789 */ 790 src = ntohl(p[1]) >> 16; 791 if (bpf_peers_present(ifp->if_bpf)) { 792 mtag = m_tag_alloc(MTAG_FIREWIRE, 793 MTAG_FIREWIRE_SENDER_EUID, 794 2*sizeof(uint32_t), M_NOWAIT); 795 if (mtag) { 796 /* bpf wants it in network byte order */ 797 struct fw_device *fd; 798 uint32_t *p = (uint32_t *) (mtag + 1); 799 fd = fw_noderesolve_nodeid(fwip->fd.fc, 800 src & 0x3f); 801 if (fd) { 802 p[0] = htonl(fd->eui.hi); 803 p[1] = htonl(fd->eui.lo); 804 } else { 805 p[0] = 0; 806 p[1] = 0; 807 } 808 m_tag_prepend(m, mtag); 809 } 810 } 811 812 /* 813 * Trim off the GASP header 814 */ 815 m_adj(m, 3*sizeof(uint32_t)); 816 m->m_pkthdr.rcvif = ifp; 817 firewire_input(ifp, m, src); 818 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 819 } 820 if (STAILQ_FIRST(&xferq->stfree) != NULL) 821 fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch); 822 } 823 824 static __inline void 825 fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer) 826 { 827 struct mbuf *m; 828 829 /* 830 * We have finished with a unicast xfer. Allocate a new 831 * cluster and stick it on the back of the input queue. 832 */ 833 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 834 xfer->mbuf = m; 835 xfer->recv.payload = mtod(m, uint32_t *); 836 xfer->recv.pay_len = MCLBYTES; 837 xfer->mbuf = m; 838 STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link); 839 } 840 841 static void 842 fwip_unicast_input(struct fw_xfer *xfer) 843 { 844 uint64_t address; 845 struct mbuf *m; 846 struct m_tag *mtag; 847 struct ifnet *ifp; 848 struct fwip_softc *fwip; 849 struct fw_pkt *fp; 850 //struct fw_pkt *sfp; 851 int rtcode; 852 853 fwip = (struct fwip_softc *)xfer->sc; 854 ifp = fwip->fw_softc.fwip_ifp; 855 m = xfer->mbuf; 856 xfer->mbuf = 0; 857 fp = &xfer->recv.hdr; 858 859 /* 860 * Check the fifo address - we only accept addresses of 861 * exactly INET_FIFO. 862 */ 863 address = ((uint64_t)fp->mode.wreqb.dest_hi << 32) 864 | fp->mode.wreqb.dest_lo; 865 if (fp->mode.wreqb.tcode != FWTCODE_WREQB) { 866 rtcode = FWRCODE_ER_TYPE; 867 } else if (address != INET_FIFO) { 868 rtcode = FWRCODE_ER_ADDR; 869 } else { 870 rtcode = FWRCODE_COMPLETE; 871 } 872 873 /* 874 * Pick up a new mbuf and stick it on the back of the receive 875 * queue. 876 */ 877 fwip_unicast_input_recycle(fwip, xfer); 878 879 /* 880 * If we've already rejected the packet, give up now. 881 */ 882 if (rtcode != FWRCODE_COMPLETE) { 883 m_freem(m); 884 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 885 return; 886 } 887 888 if (bpf_peers_present(ifp->if_bpf)) { 889 /* 890 * Record the sender ID for possible BPF usage. 891 */ 892 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 893 2*sizeof(uint32_t), M_NOWAIT); 894 if (mtag) { 895 /* bpf wants it in network byte order */ 896 struct fw_device *fd; 897 uint32_t *p = (uint32_t *) (mtag + 1); 898 fd = fw_noderesolve_nodeid(fwip->fd.fc, 899 fp->mode.wreqb.src & 0x3f); 900 if (fd) { 901 p[0] = htonl(fd->eui.hi); 902 p[1] = htonl(fd->eui.lo); 903 } else { 904 p[0] = 0; 905 p[1] = 0; 906 } 907 m_tag_prepend(m, mtag); 908 } 909 } 910 911 /* 912 * Hand off to the generic encapsulation code. We don't use 913 * ifp->if_input so that we can pass the source nodeid as an 914 * argument to facilitate link-level fragment reassembly. 915 */ 916 m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len; 917 m->m_pkthdr.rcvif = ifp; 918 firewire_input(ifp, m, fp->mode.wreqb.src); 919 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 920 } 921 922 static devclass_t fwip_devclass; 923 924 static device_method_t fwip_methods[] = { 925 /* device interface */ 926 DEVMETHOD(device_identify, fwip_identify), 927 DEVMETHOD(device_probe, fwip_probe), 928 DEVMETHOD(device_attach, fwip_attach), 929 DEVMETHOD(device_detach, fwip_detach), 930 { 0, 0 } 931 }; 932 933 static driver_t fwip_driver = { 934 "fwip", 935 fwip_methods, 936 sizeof(struct fwip_softc), 937 }; 938 939 940 #ifdef __DragonFly__ 941 DECLARE_DUMMY_MODULE(fwip); 942 #endif 943 DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0); 944 MODULE_VERSION(fwip, 1); 945 MODULE_DEPEND(fwip, firewire, 1, 1, 1); 946