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