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