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