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