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