if_fwip.c revision 267992
1139749Simp/*-
2130407Sdfr * Copyright (c) 2004
3130407Sdfr *	Doug Rabson
4130407Sdfr * Copyright (c) 2002-2003
5130407Sdfr * 	Hidetoshi Shimokawa. All rights reserved.
6130407Sdfr *
7130407Sdfr * Redistribution and use in source and binary forms, with or without
8130407Sdfr * modification, are permitted provided that the following conditions
9130407Sdfr * are met:
10130407Sdfr * 1. Redistributions of source code must retain the above copyright
11130407Sdfr *    notice, this list of conditions and the following disclaimer.
12130407Sdfr * 2. Redistributions in binary form must reproduce the above copyright
13130407Sdfr *    notice, this list of conditions and the following disclaimer in the
14130407Sdfr *    documentation and/or other materials provided with the distribution.
15130407Sdfr * 3. All advertising materials mentioning features or use of this software
16130407Sdfr *    must display the following acknowledgement:
17130407Sdfr *
18130407Sdfr *	This product includes software developed by Hidetoshi Shimokawa.
19130407Sdfr *
20130407Sdfr * 4. Neither the name of the author nor the names of its contributors
21130407Sdfr *    may be used to endorse or promote products derived from this software
22130407Sdfr *    without specific prior written permission.
23130407Sdfr *
24130407Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25130407Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26130407Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27130407Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28130407Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29130407Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30130407Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31130407Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32130407Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33130407Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34130407Sdfr * SUCH DAMAGE.
35130407Sdfr *
36130407Sdfr * $FreeBSD: head/sys/dev/firewire/if_fwip.c 267992 2014-06-28 03:56:17Z hselasky $
37130407Sdfr */
38130407Sdfr
39150968Sglebius#ifdef HAVE_KERNEL_OPTION_HEADERS
40150968Sglebius#include "opt_device_polling.h"
41130407Sdfr#include "opt_inet.h"
42150968Sglebius#endif
43130407Sdfr
44130407Sdfr#include <sys/param.h>
45130407Sdfr#include <sys/kernel.h>
46130407Sdfr#include <sys/malloc.h>
47130407Sdfr#include <sys/mbuf.h>
48130407Sdfr#include <sys/socket.h>
49130407Sdfr#include <sys/sockio.h>
50130407Sdfr#include <sys/sysctl.h>
51130407Sdfr#include <sys/systm.h>
52130407Sdfr#include <sys/taskqueue.h>
53130407Sdfr#include <sys/module.h>
54130407Sdfr#include <sys/bus.h>
55130407Sdfr#include <machine/bus.h>
56130407Sdfr
57130407Sdfr#include <net/bpf.h>
58130407Sdfr#include <net/if.h>
59257176Sglebius#include <net/if_var.h>
60130407Sdfr#include <net/firewire.h>
61130407Sdfr#include <net/if_arp.h>
62147256Sbrooks#include <net/if_types.h>
63130407Sdfr#ifdef __DragonFly__
64130407Sdfr#include <bus/firewire/firewire.h>
65130407Sdfr#include <bus/firewire/firewirereg.h>
66130407Sdfr#include "if_fwipvar.h"
67130407Sdfr#else
68130407Sdfr#include <dev/firewire/firewire.h>
69130407Sdfr#include <dev/firewire/firewirereg.h>
70130411Sdfr#include <dev/firewire/iec13213.h>
71130407Sdfr#include <dev/firewire/if_fwipvar.h>
72130407Sdfr#endif
73130407Sdfr
74130407Sdfr/*
75130407Sdfr * We really need a mechanism for allocating regions in the FIFO
76130407Sdfr * address space. We pick a address in the OHCI controller's 'middle'
77130407Sdfr * address space. This means that the controller will automatically
78130407Sdfr * send responses for us, which is fine since we don't have any
79130407Sdfr * important information to put in the response anyway.
80130407Sdfr */
81130407Sdfr#define INET_FIFO	0xfffe00000000LL
82130407Sdfr
83130407Sdfr#define FWIPDEBUG	if (fwipdebug) if_printf
84130407Sdfr#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
85130407Sdfr
86130407Sdfr/* network interface */
87130407Sdfrstatic void fwip_start (struct ifnet *);
88130407Sdfrstatic int fwip_ioctl (struct ifnet *, u_long, caddr_t);
89130407Sdfrstatic void fwip_init (void *);
90130407Sdfr
91130407Sdfrstatic void fwip_post_busreset (void *);
92130407Sdfrstatic void fwip_output_callback (struct fw_xfer *);
93130407Sdfrstatic void fwip_async_output (struct fwip_softc *, struct ifnet *);
94130407Sdfrstatic void fwip_start_send (void *, int);
95130407Sdfrstatic void fwip_stream_input (struct fw_xferq *);
96130407Sdfrstatic void fwip_unicast_input(struct fw_xfer *);
97130407Sdfr
98130407Sdfrstatic int fwipdebug = 0;
99132445Sdfrstatic int broadcast_channel = 0xc0 | 0x1f; /*  tag | channel(XXX) */
100130407Sdfrstatic int tx_speed = 2;
101130407Sdfrstatic int rx_queue_len = FWMAXQUEUE;
102130407Sdfr
103227293Sedstatic MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
104130407SdfrSYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
105130407SdfrSYSCTL_DECL(_hw_firewire);
106227309Sedstatic SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
107130407Sdfr	"Firewire ip subsystem");
108267992ShselaskySYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len,
109130407Sdfr	0, "Length of the receive queue");
110130407Sdfr
111130407Sdfr#ifdef DEVICE_POLLING
112130407Sdfrstatic poll_handler_t fwip_poll;
113130407Sdfr
114193096Sattiliostatic int
115130407Sdfrfwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
116130407Sdfr{
117130407Sdfr	struct fwip_softc *fwip;
118130407Sdfr	struct firewire_comm *fc;
119130407Sdfr
120150789Sglebius	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
121193096Sattilio		return (0);
122150789Sglebius
123130407Sdfr	fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
124130407Sdfr	fc = fwip->fd.fc;
125130407Sdfr	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
126193096Sattilio	return (0);
127130407Sdfr}
128150789Sglebius#endif /* DEVICE_POLLING */
129150789Sglebius
130130407Sdfrstatic void
131130407Sdfrfwip_identify(driver_t *driver, device_t parent)
132130407Sdfr{
133130407Sdfr	BUS_ADD_CHILD(parent, 0, "fwip", device_get_unit(parent));
134130407Sdfr}
135130407Sdfr
136130407Sdfrstatic int
137130407Sdfrfwip_probe(device_t dev)
138130407Sdfr{
139130407Sdfr	device_t pa;
140130407Sdfr
141130407Sdfr	pa = device_get_parent(dev);
142130407Sdfr	if(device_get_unit(dev) != device_get_unit(pa)){
143130407Sdfr		return(ENXIO);
144130407Sdfr	}
145130407Sdfr
146130407Sdfr	device_set_desc(dev, "IP over FireWire");
147130407Sdfr	return (0);
148130407Sdfr}
149130407Sdfr
150130407Sdfrstatic int
151130407Sdfrfwip_attach(device_t dev)
152130407Sdfr{
153130407Sdfr	struct fwip_softc *fwip;
154130407Sdfr	struct ifnet *ifp;
155130407Sdfr	int unit, s;
156130407Sdfr	struct fw_hwaddr *hwaddr;
157130407Sdfr
158130407Sdfr	fwip = ((struct fwip_softc *)device_get_softc(dev));
159130407Sdfr	unit = device_get_unit(dev);
160147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp = if_alloc(IFT_IEEE1394);
161147256Sbrooks	if (ifp == NULL)
162147256Sbrooks		return (ENOSPC);
163130407Sdfr
164170374Ssimokawa	mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF);
165130407Sdfr	/* XXX */
166130407Sdfr	fwip->dma_ch = -1;
167130407Sdfr
168130407Sdfr	fwip->fd.fc = device_get_ivars(dev);
169130407Sdfr	if (tx_speed < 0)
170130407Sdfr		tx_speed = fwip->fd.fc->speed;
171130407Sdfr
172130407Sdfr	fwip->fd.dev = dev;
173130407Sdfr	fwip->fd.post_explore = NULL;
174130407Sdfr	fwip->fd.post_busreset = fwip_post_busreset;
175130407Sdfr	fwip->fw_softc.fwip = fwip;
176130407Sdfr	TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
177130407Sdfr
178130407Sdfr	/*
179130407Sdfr	 * Encode our hardware the way that arp likes it.
180130407Sdfr	 */
181147256Sbrooks	hwaddr = &IFP2FWC(fwip->fw_softc.fwip_ifp)->fc_hwaddr;
182130407Sdfr	hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
183130407Sdfr	hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
184130407Sdfr	hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
185130407Sdfr	hwaddr->sspd = fwip->fd.fc->speed;
186130407Sdfr	hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
187130407Sdfr	hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
188130407Sdfr
189130407Sdfr	/* fill the rest and attach interface */
190130407Sdfr	ifp->if_softc = &fwip->fw_softc;
191130407Sdfr
192130407Sdfr#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
193130407Sdfr	if_initname(ifp, device_get_name(dev), unit);
194130407Sdfr#else
195130407Sdfr	ifp->if_unit = unit;
196130407Sdfr	ifp->if_name = "fwip";
197130407Sdfr#endif
198130407Sdfr	ifp->if_init = fwip_init;
199130407Sdfr	ifp->if_start = fwip_start;
200130407Sdfr	ifp->if_ioctl = fwip_ioctl;
201170374Ssimokawa	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
202130407Sdfr	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
203150789Sglebius#ifdef DEVICE_POLLING
204150789Sglebius	ifp->if_capabilities |= IFCAP_POLLING;
205150789Sglebius#endif
206130407Sdfr
207130407Sdfr	s = splimp();
208130407Sdfr	firewire_ifattach(ifp, hwaddr);
209130407Sdfr	splx(s);
210130407Sdfr
211130407Sdfr	FWIPDEBUG(ifp, "interface created\n");
212130407Sdfr	return 0;
213130407Sdfr}
214130407Sdfr
215130407Sdfrstatic void
216130407Sdfrfwip_stop(struct fwip_softc *fwip)
217130407Sdfr{
218130407Sdfr	struct firewire_comm *fc;
219130407Sdfr	struct fw_xferq *xferq;
220147256Sbrooks	struct ifnet *ifp = fwip->fw_softc.fwip_ifp;
221130407Sdfr	struct fw_xfer *xfer, *next;
222130407Sdfr	int i;
223130407Sdfr
224130407Sdfr	fc = fwip->fd.fc;
225130407Sdfr
226130407Sdfr	if (fwip->dma_ch >= 0) {
227130407Sdfr		xferq = fc->ir[fwip->dma_ch];
228130407Sdfr
229130407Sdfr		if (xferq->flag & FWXFERQ_RUNNING)
230130407Sdfr			fc->irx_disable(fc, fwip->dma_ch);
231130407Sdfr		xferq->flag &=
232130407Sdfr			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
233130407Sdfr			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
234130407Sdfr		xferq->hand =  NULL;
235130407Sdfr
236130407Sdfr		for (i = 0; i < xferq->bnchunk; i ++)
237130407Sdfr			m_freem(xferq->bulkxfer[i].mbuf);
238130407Sdfr		free(xferq->bulkxfer, M_FWIP);
239130407Sdfr
240130407Sdfr		fw_bindremove(fc, &fwip->fwb);
241130407Sdfr		for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
242130407Sdfr					xfer = next) {
243130407Sdfr			next = STAILQ_NEXT(xfer, link);
244130407Sdfr			fw_xfer_free(xfer);
245130407Sdfr		}
246130407Sdfr
247130407Sdfr		for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
248130407Sdfr					xfer = next) {
249130407Sdfr			next = STAILQ_NEXT(xfer, link);
250130407Sdfr			fw_xfer_free(xfer);
251130407Sdfr		}
252130407Sdfr		STAILQ_INIT(&fwip->xferlist);
253130407Sdfr
254130407Sdfr		xferq->bulkxfer =  NULL;
255130407Sdfr		fwip->dma_ch = -1;
256130407Sdfr	}
257130407Sdfr
258148887Srwatson#if defined(__FreeBSD__)
259148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
260148887Srwatson#else
261130407Sdfr	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
262148887Srwatson#endif
263130407Sdfr}
264130407Sdfr
265130407Sdfrstatic int
266130407Sdfrfwip_detach(device_t dev)
267130407Sdfr{
268130407Sdfr	struct fwip_softc *fwip;
269150789Sglebius	struct ifnet *ifp;
270130407Sdfr	int s;
271130407Sdfr
272130407Sdfr	fwip = (struct fwip_softc *)device_get_softc(dev);
273150789Sglebius	ifp = fwip->fw_softc.fwip_ifp;
274150789Sglebius
275150789Sglebius#ifdef DEVICE_POLLING
276150789Sglebius	if (ifp->if_capenable & IFCAP_POLLING)
277150789Sglebius		ether_poll_deregister(ifp);
278150789Sglebius#endif
279150789Sglebius
280130407Sdfr	s = splimp();
281130407Sdfr
282130407Sdfr	fwip_stop(fwip);
283150789Sglebius	firewire_ifdetach(ifp);
284150789Sglebius	if_free(ifp);
285170374Ssimokawa	mtx_destroy(&fwip->mtx);
286130407Sdfr
287130407Sdfr	splx(s);
288130407Sdfr	return 0;
289130407Sdfr}
290130407Sdfr
291130407Sdfrstatic void
292130407Sdfrfwip_init(void *arg)
293130407Sdfr{
294130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)arg)->fwip;
295130407Sdfr	struct firewire_comm *fc;
296147256Sbrooks	struct ifnet *ifp = fwip->fw_softc.fwip_ifp;
297130407Sdfr	struct fw_xferq *xferq;
298130407Sdfr	struct fw_xfer *xfer;
299130407Sdfr	struct mbuf *m;
300130407Sdfr	int i;
301130407Sdfr
302130407Sdfr	FWIPDEBUG(ifp, "initializing\n");
303130407Sdfr
304130407Sdfr	fc = fwip->fd.fc;
305130407Sdfr#define START 0
306130407Sdfr	if (fwip->dma_ch < 0) {
307170374Ssimokawa		fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
308170374Ssimokawa		if (fwip->dma_ch < 0)
309170374Ssimokawa			return;
310170374Ssimokawa		xferq = fc->ir[fwip->dma_ch];
311170374Ssimokawa		xferq->flag |= FWXFERQ_EXTBUF |
312130407Sdfr				FWXFERQ_HANDLER | FWXFERQ_STREAM;
313130407Sdfr		xferq->flag &= ~0xff;
314130407Sdfr		xferq->flag |= broadcast_channel & 0xff;
315130407Sdfr		/* register fwip_input handler */
316130407Sdfr		xferq->sc = (caddr_t) fwip;
317130407Sdfr		xferq->hand = fwip_stream_input;
318130407Sdfr		xferq->bnchunk = rx_queue_len;
319130407Sdfr		xferq->bnpacket = 1;
320130407Sdfr		xferq->psize = MCLBYTES;
321130407Sdfr		xferq->queued = 0;
322130407Sdfr		xferq->buf = NULL;
323130407Sdfr		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
324130407Sdfr			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
325130407Sdfr							M_FWIP, M_WAITOK);
326130407Sdfr		if (xferq->bulkxfer == NULL) {
327130407Sdfr			printf("if_fwip: malloc failed\n");
328130407Sdfr			return;
329130407Sdfr		}
330130407Sdfr		STAILQ_INIT(&xferq->stvalid);
331130407Sdfr		STAILQ_INIT(&xferq->stfree);
332130407Sdfr		STAILQ_INIT(&xferq->stdma);
333130407Sdfr		xferq->stproc = NULL;
334130407Sdfr		for (i = 0; i < xferq->bnchunk; i ++) {
335243857Sglebius			m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
336130407Sdfr			xferq->bulkxfer[i].mbuf = m;
337177599Sru			m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
338177599Sru			STAILQ_INSERT_TAIL(&xferq->stfree,
339177599Sru					&xferq->bulkxfer[i], link);
340130407Sdfr		}
341130407Sdfr
342130407Sdfr		fwip->fwb.start = INET_FIFO;
343130407Sdfr		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
344130407Sdfr
345130407Sdfr		/* pre-allocate xfer */
346130407Sdfr		STAILQ_INIT(&fwip->fwb.xferlist);
347130407Sdfr		for (i = 0; i < rx_queue_len; i ++) {
348130407Sdfr			xfer = fw_xfer_alloc(M_FWIP);
349130407Sdfr			if (xfer == NULL)
350130407Sdfr				break;
351243857Sglebius			m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
352130407Sdfr			xfer->recv.payload = mtod(m, uint32_t *);
353130407Sdfr			xfer->recv.pay_len = MCLBYTES;
354167632Ssimokawa			xfer->hand = fwip_unicast_input;
355130407Sdfr			xfer->fc = fc;
356130407Sdfr			xfer->sc = (caddr_t)fwip;
357130407Sdfr			xfer->mbuf = m;
358130407Sdfr			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
359130407Sdfr		}
360130407Sdfr		fw_bindadd(fc, &fwip->fwb);
361130407Sdfr
362130407Sdfr		STAILQ_INIT(&fwip->xferlist);
363130407Sdfr		for (i = 0; i < TX_MAX_QUEUE; i++) {
364130407Sdfr			xfer = fw_xfer_alloc(M_FWIP);
365130407Sdfr			if (xfer == NULL)
366130407Sdfr				break;
367130407Sdfr			xfer->send.spd = tx_speed;
368130407Sdfr			xfer->fc = fwip->fd.fc;
369130407Sdfr			xfer->sc = (caddr_t)fwip;
370167632Ssimokawa			xfer->hand = fwip_output_callback;
371130407Sdfr			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
372130407Sdfr		}
373130407Sdfr	} else
374130407Sdfr		xferq = fc->ir[fwip->dma_ch];
375130407Sdfr
376130407Sdfr	fwip->last_dest.hi = 0;
377130407Sdfr	fwip->last_dest.lo = 0;
378130407Sdfr
379130407Sdfr	/* start dma */
380130407Sdfr	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
381130407Sdfr		fc->irx_enable(fc, fwip->dma_ch);
382130407Sdfr
383148887Srwatson#if defined(__FreeBSD__)
384148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
385148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
386148887Srwatson#else
387130407Sdfr	ifp->if_flags |= IFF_RUNNING;
388130407Sdfr	ifp->if_flags &= ~IFF_OACTIVE;
389148887Srwatson#endif
390130407Sdfr
391130407Sdfr#if 0
392130407Sdfr	/* attempt to start output */
393130407Sdfr	fwip_start(ifp);
394130407Sdfr#endif
395130407Sdfr}
396130407Sdfr
397130407Sdfrstatic int
398130407Sdfrfwip_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
399130407Sdfr{
400130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
401130407Sdfr	int s, error;
402130407Sdfr
403130407Sdfr	switch (cmd) {
404130407Sdfr	case SIOCSIFFLAGS:
405130407Sdfr		s = splimp();
406130407Sdfr		if (ifp->if_flags & IFF_UP) {
407148887Srwatson#if defined(__FreeBSD__)
408148887Srwatson			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
409148887Srwatson#else
410130407Sdfr			if (!(ifp->if_flags & IFF_RUNNING))
411148887Srwatson#endif
412130407Sdfr				fwip_init(&fwip->fw_softc);
413130407Sdfr		} else {
414148887Srwatson#if defined(__FreeBSD__)
415148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
416148887Srwatson#else
417130407Sdfr			if (ifp->if_flags & IFF_RUNNING)
418148887Srwatson#endif
419130407Sdfr				fwip_stop(fwip);
420130407Sdfr		}
421130407Sdfr		splx(s);
422130407Sdfr		break;
423130407Sdfr	case SIOCADDMULTI:
424130407Sdfr	case SIOCDELMULTI:
425130407Sdfr		break;
426150789Sglebius	case SIOCSIFCAP:
427150789Sglebius#ifdef DEVICE_POLLING
428150789Sglebius	    {
429150789Sglebius		struct ifreq *ifr = (struct ifreq *) data;
430188394Sfjoe		struct firewire_comm *fc = fwip->fd.fc;
431130407Sdfr
432150789Sglebius		if (ifr->ifr_reqcap & IFCAP_POLLING &&
433150789Sglebius		    !(ifp->if_capenable & IFCAP_POLLING)) {
434150789Sglebius			error = ether_poll_register(fwip_poll, ifp);
435150789Sglebius			if (error)
436150789Sglebius				return(error);
437150789Sglebius			/* Disable interrupts */
438150789Sglebius			fc->set_intr(fc, 0);
439193096Sattilio			ifp->if_capenable |= IFCAP_POLLING |
440193096Sattilio			    IFCAP_POLLING_NOCOUNT;
441150789Sglebius			return (error);
442150789Sglebius
443150789Sglebius		}
444150789Sglebius		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
445150789Sglebius		    ifp->if_capenable & IFCAP_POLLING) {
446150789Sglebius			error = ether_poll_deregister(ifp);
447150789Sglebius			/* Enable interrupts. */
448150789Sglebius			fc->set_intr(fc, 1);
449150789Sglebius			ifp->if_capenable &= ~IFCAP_POLLING;
450193096Sattilio			ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
451150789Sglebius			return (error);
452150789Sglebius		}
453150789Sglebius	    }
454150789Sglebius#endif /* DEVICE_POLLING */
455150789Sglebius		break;
456130407Sdfr#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
457130407Sdfr	default:
458130407Sdfr#else
459130407Sdfr	case SIOCSIFADDR:
460130407Sdfr	case SIOCGIFADDR:
461130407Sdfr	case SIOCSIFMTU:
462130407Sdfr#endif
463130407Sdfr		s = splimp();
464130407Sdfr		error = firewire_ioctl(ifp, cmd, data);
465130407Sdfr		splx(s);
466130407Sdfr		return (error);
467130407Sdfr#if defined(__DragonFly__) || __FreeBSD_version < 500000
468130407Sdfr	default:
469130407Sdfr		return (EINVAL);
470130407Sdfr#endif
471130407Sdfr	}
472130407Sdfr
473130407Sdfr	return (0);
474130407Sdfr}
475130407Sdfr
476130407Sdfrstatic void
477130407Sdfrfwip_post_busreset(void *arg)
478130407Sdfr{
479130407Sdfr	struct fwip_softc *fwip = arg;
480130411Sdfr	struct crom_src *src;
481130411Sdfr	struct crom_chunk *root;
482130407Sdfr
483130411Sdfr	src = fwip->fd.fc->crom_src;
484130411Sdfr	root = fwip->fd.fc->crom_root;
485130411Sdfr
486130411Sdfr	/* RFC2734 IPv4 over IEEE1394 */
487130411Sdfr	bzero(&fwip->unit4, sizeof(struct crom_chunk));
488130411Sdfr	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
489130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
490130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
491130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
492130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
493130411Sdfr
494130411Sdfr	/* RFC3146 IPv6 over IEEE1394 */
495130411Sdfr	bzero(&fwip->unit6, sizeof(struct crom_chunk));
496130411Sdfr	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
497130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
498130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
499130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
500130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
501130411Sdfr
502130407Sdfr	fwip->last_dest.hi = 0;
503130407Sdfr	fwip->last_dest.lo = 0;
504147256Sbrooks	firewire_busreset(fwip->fw_softc.fwip_ifp);
505130407Sdfr}
506130407Sdfr
507130407Sdfrstatic void
508130407Sdfrfwip_output_callback(struct fw_xfer *xfer)
509130407Sdfr{
510130407Sdfr	struct fwip_softc *fwip;
511130407Sdfr	struct ifnet *ifp;
512130407Sdfr	int s;
513130407Sdfr
514130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
515147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
516130407Sdfr	/* XXX error check */
517130407Sdfr	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
518130407Sdfr	if (xfer->resp != 0)
519130407Sdfr		ifp->if_oerrors ++;
520130407Sdfr
521130407Sdfr	m_freem(xfer->mbuf);
522130407Sdfr	fw_xfer_unload(xfer);
523130407Sdfr
524130407Sdfr	s = splimp();
525170374Ssimokawa	FWIP_LOCK(fwip);
526130407Sdfr	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
527170374Ssimokawa	FWIP_UNLOCK(fwip);
528130407Sdfr	splx(s);
529130407Sdfr
530130407Sdfr	/* for queue full */
531170374Ssimokawa	if (ifp->if_snd.ifq_head != NULL) {
532130407Sdfr		fwip_start(ifp);
533170374Ssimokawa	}
534130407Sdfr}
535130407Sdfr
536130407Sdfrstatic void
537130407Sdfrfwip_start(struct ifnet *ifp)
538130407Sdfr{
539130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
540130407Sdfr	int s;
541130407Sdfr
542130407Sdfr	FWIPDEBUG(ifp, "starting\n");
543130407Sdfr
544130407Sdfr	if (fwip->dma_ch < 0) {
545130407Sdfr		struct mbuf	*m = NULL;
546130407Sdfr
547130407Sdfr		FWIPDEBUG(ifp, "not ready\n");
548130407Sdfr
549130407Sdfr		s = splimp();
550130407Sdfr		do {
551130407Sdfr			IF_DEQUEUE(&ifp->if_snd, m);
552130407Sdfr			if (m != NULL)
553130407Sdfr				m_freem(m);
554130407Sdfr			ifp->if_oerrors ++;
555130407Sdfr		} while (m != NULL);
556130407Sdfr		splx(s);
557130407Sdfr
558130407Sdfr		return;
559130407Sdfr	}
560130407Sdfr
561130407Sdfr	s = splimp();
562148887Srwatson#if defined(__FreeBSD__)
563148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
564148887Srwatson#else
565130407Sdfr	ifp->if_flags |= IFF_OACTIVE;
566148887Srwatson#endif
567130407Sdfr
568130407Sdfr	if (ifp->if_snd.ifq_len != 0)
569130407Sdfr		fwip_async_output(fwip, ifp);
570130407Sdfr
571148887Srwatson#if defined(__FreeBSD__)
572148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
573148887Srwatson#else
574130407Sdfr	ifp->if_flags &= ~IFF_OACTIVE;
575148887Srwatson#endif
576130407Sdfr	splx(s);
577130407Sdfr}
578130407Sdfr
579130407Sdfr/* Async. stream output */
580130407Sdfrstatic void
581130407Sdfrfwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
582130407Sdfr{
583130407Sdfr	struct firewire_comm *fc = fwip->fd.fc;
584130407Sdfr	struct mbuf *m;
585130407Sdfr	struct m_tag *mtag;
586130407Sdfr	struct fw_hwaddr *destfw;
587130407Sdfr	struct fw_xfer *xfer;
588130407Sdfr	struct fw_xferq *xferq;
589130407Sdfr	struct fw_pkt *fp;
590130407Sdfr	uint16_t nodeid;
591130460Sdfr	int error;
592130407Sdfr	int i = 0;
593130407Sdfr
594130407Sdfr	xfer = NULL;
595170374Ssimokawa	xferq = fc->atq;
596170374Ssimokawa	while ((xferq->queued < xferq->maxq - 1) &&
597170374Ssimokawa			(ifp->if_snd.ifq_head != NULL)) {
598170374Ssimokawa		FWIP_LOCK(fwip);
599130407Sdfr		xfer = STAILQ_FIRST(&fwip->xferlist);
600130407Sdfr		if (xfer == NULL) {
601170374Ssimokawa			FWIP_UNLOCK(fwip);
602170374Ssimokawa#if 0
603130407Sdfr			printf("if_fwip: lack of xfer\n");
604170374Ssimokawa#endif
605170374Ssimokawa			break;
606130407Sdfr		}
607170374Ssimokawa		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
608170374Ssimokawa		FWIP_UNLOCK(fwip);
609170374Ssimokawa
610130407Sdfr		IF_DEQUEUE(&ifp->if_snd, m);
611170374Ssimokawa		if (m == NULL) {
612170374Ssimokawa			FWIP_LOCK(fwip);
613170374Ssimokawa			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
614170374Ssimokawa			FWIP_UNLOCK(fwip);
615130407Sdfr			break;
616170374Ssimokawa		}
617130407Sdfr
618130407Sdfr		/*
619130407Sdfr		 * Dig out the link-level address which
620130407Sdfr		 * firewire_output got via arp or neighbour
621130407Sdfr		 * discovery. If we don't have a link-level address,
622130407Sdfr		 * just stick the thing on the broadcast channel.
623130407Sdfr		 */
624130407Sdfr		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
625130407Sdfr		if (mtag == NULL)
626130407Sdfr			destfw = 0;
627130407Sdfr		else
628130407Sdfr			destfw = (struct fw_hwaddr *) (mtag + 1);
629130407Sdfr
630130407Sdfr
631130407Sdfr		/*
632130407Sdfr		 * We don't do any bpf stuff here - the generic code
633130407Sdfr		 * in firewire_output gives the packet to bpf before
634130407Sdfr		 * it adds the link-level encapsulation.
635130407Sdfr		 */
636130407Sdfr
637130407Sdfr		/*
638130407Sdfr		 * Put the mbuf in the xfer early in case we hit an
639130407Sdfr		 * error case below - fwip_output_callback will free
640130407Sdfr		 * the mbuf.
641130407Sdfr		 */
642130407Sdfr		xfer->mbuf = m;
643130407Sdfr
644130407Sdfr		/*
645130407Sdfr		 * We use the arp result (if any) to add a suitable firewire
646130407Sdfr		 * packet header before handing off to the bus.
647130407Sdfr		 */
648130407Sdfr		fp = &xfer->send.hdr;
649130407Sdfr		nodeid = FWLOCALBUS | fc->nodeid;
650130407Sdfr		if ((m->m_flags & M_BCAST) || !destfw) {
651130407Sdfr			/*
652130407Sdfr			 * Broadcast packets are sent as GASP packets with
653130407Sdfr			 * specifier ID 0x00005e, version 1 on the broadcast
654130407Sdfr			 * channel. To be conservative, we send at the
655130407Sdfr			 * slowest possible speed.
656130407Sdfr			 */
657130407Sdfr			uint32_t *p;
658130407Sdfr
659243857Sglebius			M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
660130407Sdfr			p = mtod(m, uint32_t *);
661130407Sdfr			fp->mode.stream.len = m->m_pkthdr.len;
662130407Sdfr			fp->mode.stream.chtag = broadcast_channel;
663130407Sdfr			fp->mode.stream.tcode = FWTCODE_STREAM;
664130407Sdfr			fp->mode.stream.sy = 0;
665130407Sdfr			xfer->send.spd = 0;
666130407Sdfr			p[0] = htonl(nodeid << 16);
667130407Sdfr			p[1] = htonl((0x5e << 24) | 1);
668130407Sdfr		} else {
669130407Sdfr			/*
670130407Sdfr			 * Unicast packets are sent as block writes to the
671130407Sdfr			 * target's unicast fifo address. If we can't
672130407Sdfr			 * find the node address, we just give up. We
673130407Sdfr			 * could broadcast it but that might overflow
674130407Sdfr			 * the packet size limitations due to the
675130407Sdfr			 * extra GASP header. Note: the hardware
676130407Sdfr			 * address is stored in network byte order to
677130407Sdfr			 * make life easier for ARP.
678130407Sdfr			 */
679130407Sdfr			struct fw_device *fd;
680130407Sdfr			struct fw_eui64 eui;
681130407Sdfr
682130407Sdfr			eui.hi = ntohl(destfw->sender_unique_ID_hi);
683130407Sdfr			eui.lo = ntohl(destfw->sender_unique_ID_lo);
684130407Sdfr			if (fwip->last_dest.hi != eui.hi ||
685130407Sdfr			    fwip->last_dest.lo != eui.lo) {
686130407Sdfr				fd = fw_noderesolve_eui64(fc, &eui);
687130407Sdfr				if (!fd) {
688130407Sdfr					/* error */
689130407Sdfr					ifp->if_oerrors ++;
690130407Sdfr					/* XXX set error code */
691130407Sdfr					fwip_output_callback(xfer);
692130407Sdfr					continue;
693130407Sdfr
694130407Sdfr				}
695130407Sdfr				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
696130407Sdfr				fwip->last_hdr.mode.wreqb.tlrt = 0;
697130407Sdfr				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
698130407Sdfr				fwip->last_hdr.mode.wreqb.pri = 0;
699130407Sdfr				fwip->last_hdr.mode.wreqb.src = nodeid;
700130407Sdfr				fwip->last_hdr.mode.wreqb.dest_hi =
701130407Sdfr					ntohs(destfw->sender_unicast_FIFO_hi);
702130407Sdfr				fwip->last_hdr.mode.wreqb.dest_lo =
703130407Sdfr					ntohl(destfw->sender_unicast_FIFO_lo);
704130407Sdfr				fwip->last_hdr.mode.wreqb.extcode = 0;
705130407Sdfr				fwip->last_dest = eui;
706130407Sdfr			}
707130407Sdfr
708130407Sdfr			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
709130407Sdfr			fp->mode.wreqb.len = m->m_pkthdr.len;
710130407Sdfr			xfer->send.spd = min(destfw->sspd, fc->speed);
711130407Sdfr		}
712130407Sdfr
713130407Sdfr		xfer->send.pay_len = m->m_pkthdr.len;
714130407Sdfr
715130460Sdfr		error = fw_asyreq(fc, -1, xfer);
716130460Sdfr		if (error == EAGAIN) {
717130460Sdfr			/*
718130460Sdfr			 * We ran out of tlabels - requeue the packet
719130460Sdfr			 * for later transmission.
720130460Sdfr			 */
721130460Sdfr			xfer->mbuf = 0;
722170374Ssimokawa			FWIP_LOCK(fwip);
723130460Sdfr			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
724170374Ssimokawa			FWIP_UNLOCK(fwip);
725130460Sdfr			IF_PREPEND(&ifp->if_snd, m);
726130460Sdfr			break;
727130460Sdfr		}
728130460Sdfr		if (error) {
729130407Sdfr			/* error */
730130407Sdfr			ifp->if_oerrors ++;
731130407Sdfr			/* XXX set error code */
732130407Sdfr			fwip_output_callback(xfer);
733130407Sdfr			continue;
734130407Sdfr		} else {
735130407Sdfr			ifp->if_opackets ++;
736130407Sdfr			i++;
737130407Sdfr		}
738130407Sdfr	}
739130407Sdfr#if 0
740130407Sdfr	if (i > 1)
741130407Sdfr		printf("%d queued\n", i);
742130407Sdfr#endif
743170374Ssimokawa	if (i > 0)
744130407Sdfr		xferq->start(fc);
745130407Sdfr}
746130407Sdfr
747130407Sdfrstatic void
748130407Sdfrfwip_start_send (void *arg, int count)
749130407Sdfr{
750130407Sdfr	struct fwip_softc *fwip = arg;
751130407Sdfr
752130407Sdfr	fwip->fd.fc->atq->start(fwip->fd.fc);
753130407Sdfr}
754130407Sdfr
755130407Sdfr/* Async. stream output */
756130407Sdfrstatic void
757130407Sdfrfwip_stream_input(struct fw_xferq *xferq)
758130407Sdfr{
759130407Sdfr	struct mbuf *m, *m0;
760130407Sdfr	struct m_tag *mtag;
761130407Sdfr	struct ifnet *ifp;
762130407Sdfr	struct fwip_softc *fwip;
763130407Sdfr	struct fw_bulkxfer *sxfer;
764130407Sdfr	struct fw_pkt *fp;
765130407Sdfr	uint16_t src;
766130407Sdfr	uint32_t *p;
767130407Sdfr
768130407Sdfr
769130407Sdfr	fwip = (struct fwip_softc *)xferq->sc;
770147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
771150789Sglebius
772130407Sdfr	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
773130407Sdfr		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
774130407Sdfr		fp = mtod(sxfer->mbuf, struct fw_pkt *);
775130407Sdfr		if (fwip->fd.fc->irx_post != NULL)
776130407Sdfr			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
777130407Sdfr		m = sxfer->mbuf;
778130407Sdfr
779130407Sdfr		/* insert new rbuf */
780243857Sglebius		sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
781130407Sdfr		if (m0 != NULL) {
782130407Sdfr			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
783130407Sdfr			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
784130407Sdfr		} else
785130407Sdfr			printf("fwip_as_input: m_getcl failed\n");
786130407Sdfr
787130407Sdfr		/*
788130407Sdfr		 * We must have a GASP header - leave the
789130407Sdfr		 * encapsulation sanity checks to the generic
790130407Sdfr		 * code. Remeber that we also have the firewire async
791130407Sdfr		 * stream header even though that isn't accounted for
792130407Sdfr		 * in mode.stream.len.
793130407Sdfr		 */
794130407Sdfr		if (sxfer->resp != 0 || fp->mode.stream.len <
795130407Sdfr		    2*sizeof(uint32_t)) {
796130407Sdfr			m_freem(m);
797130407Sdfr			ifp->if_ierrors ++;
798130407Sdfr			continue;
799130407Sdfr		}
800130407Sdfr		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
801130407Sdfr			+ sizeof(fp->mode.stream);
802130407Sdfr
803130407Sdfr		/*
804130407Sdfr		 * If we received the packet on the broadcast channel,
805130407Sdfr		 * mark it as broadcast, otherwise we assume it must
806130407Sdfr		 * be multicast.
807130407Sdfr		 */
808130407Sdfr		if (fp->mode.stream.chtag == broadcast_channel)
809130407Sdfr			m->m_flags |= M_BCAST;
810130407Sdfr		else
811130407Sdfr			m->m_flags |= M_MCAST;
812130407Sdfr
813130407Sdfr		/*
814130407Sdfr		 * Make sure we recognise the GASP specifier and
815130407Sdfr		 * version.
816130407Sdfr		 */
817130407Sdfr		p = mtod(m, uint32_t *);
818130407Sdfr		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
819130407Sdfr		    || (ntohl(p[2]) & 0xffffff) != 1) {
820130407Sdfr			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
821130407Sdfr			    ntohl(p[1]), ntohl(p[2]));
822130407Sdfr			m_freem(m);
823130407Sdfr			ifp->if_ierrors ++;
824130407Sdfr			continue;
825130407Sdfr		}
826130407Sdfr
827130407Sdfr		/*
828130407Sdfr		 * Record the sender ID for possible BPF usage.
829130407Sdfr		 */
830130407Sdfr		src = ntohl(p[1]) >> 16;
831165632Sjhb		if (bpf_peers_present(ifp->if_bpf)) {
832130407Sdfr			mtag = m_tag_alloc(MTAG_FIREWIRE,
833130407Sdfr			    MTAG_FIREWIRE_SENDER_EUID,
834130407Sdfr			    2*sizeof(uint32_t), M_NOWAIT);
835130407Sdfr			if (mtag) {
836130407Sdfr				/* bpf wants it in network byte order */
837130407Sdfr				struct fw_device *fd;
838130407Sdfr				uint32_t *p = (uint32_t *) (mtag + 1);
839130407Sdfr				fd = fw_noderesolve_nodeid(fwip->fd.fc,
840130407Sdfr				    src & 0x3f);
841130407Sdfr				if (fd) {
842130407Sdfr					p[0] = htonl(fd->eui.hi);
843130407Sdfr					p[1] = htonl(fd->eui.lo);
844130407Sdfr				} else {
845130407Sdfr					p[0] = 0;
846130407Sdfr					p[1] = 0;
847130407Sdfr				}
848130407Sdfr				m_tag_prepend(m, mtag);
849130407Sdfr			}
850130407Sdfr		}
851130407Sdfr
852130407Sdfr		/*
853130407Sdfr		 * Trim off the GASP header
854130407Sdfr		 */
855130407Sdfr		m_adj(m, 3*sizeof(uint32_t));
856130407Sdfr		m->m_pkthdr.rcvif = ifp;
857130407Sdfr		firewire_input(ifp, m, src);
858130407Sdfr		ifp->if_ipackets ++;
859130407Sdfr	}
860130407Sdfr	if (STAILQ_FIRST(&xferq->stfree) != NULL)
861130407Sdfr		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
862130407Sdfr}
863130407Sdfr
864130407Sdfrstatic __inline void
865130407Sdfrfwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
866130407Sdfr{
867130407Sdfr	struct mbuf *m;
868130407Sdfr
869130407Sdfr	/*
870130407Sdfr	 * We have finished with a unicast xfer. Allocate a new
871130407Sdfr	 * cluster and stick it on the back of the input queue.
872130407Sdfr	 */
873243857Sglebius	m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
874130407Sdfr	xfer->mbuf = m;
875130407Sdfr	xfer->recv.payload = mtod(m, uint32_t *);
876130407Sdfr	xfer->recv.pay_len = MCLBYTES;
877130407Sdfr	xfer->mbuf = m;
878130407Sdfr	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
879130407Sdfr}
880130407Sdfr
881130407Sdfrstatic void
882130407Sdfrfwip_unicast_input(struct fw_xfer *xfer)
883130407Sdfr{
884130407Sdfr	uint64_t address;
885130407Sdfr	struct mbuf *m;
886130407Sdfr	struct m_tag *mtag;
887130407Sdfr	struct ifnet *ifp;
888130407Sdfr	struct fwip_softc *fwip;
889130407Sdfr	struct fw_pkt *fp;
890130407Sdfr	//struct fw_pkt *sfp;
891130407Sdfr	int rtcode;
892130407Sdfr
893130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
894147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
895130407Sdfr	m = xfer->mbuf;
896130407Sdfr	xfer->mbuf = 0;
897130407Sdfr	fp = &xfer->recv.hdr;
898130407Sdfr
899130407Sdfr	/*
900130407Sdfr	 * Check the fifo address - we only accept addresses of
901130407Sdfr	 * exactly INET_FIFO.
902130407Sdfr	 */
903130407Sdfr	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
904130407Sdfr		| fp->mode.wreqb.dest_lo;
905130407Sdfr	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
906130407Sdfr		rtcode = FWRCODE_ER_TYPE;
907130407Sdfr	} else if (address != INET_FIFO) {
908130407Sdfr		rtcode = FWRCODE_ER_ADDR;
909130407Sdfr	} else {
910130407Sdfr		rtcode = FWRCODE_COMPLETE;
911130407Sdfr	}
912130407Sdfr
913130407Sdfr	/*
914130407Sdfr	 * Pick up a new mbuf and stick it on the back of the receive
915130407Sdfr	 * queue.
916130407Sdfr	 */
917130407Sdfr	fwip_unicast_input_recycle(fwip, xfer);
918130407Sdfr
919130407Sdfr	/*
920130407Sdfr	 * If we've already rejected the packet, give up now.
921130407Sdfr	 */
922130407Sdfr	if (rtcode != FWRCODE_COMPLETE) {
923130407Sdfr		m_freem(m);
924130407Sdfr		ifp->if_ierrors ++;
925130407Sdfr		return;
926130407Sdfr	}
927130407Sdfr
928165632Sjhb	if (bpf_peers_present(ifp->if_bpf)) {
929130407Sdfr		/*
930130407Sdfr		 * Record the sender ID for possible BPF usage.
931130407Sdfr		 */
932130407Sdfr		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
933130407Sdfr		    2*sizeof(uint32_t), M_NOWAIT);
934130407Sdfr		if (mtag) {
935130407Sdfr			/* bpf wants it in network byte order */
936130407Sdfr			struct fw_device *fd;
937130407Sdfr			uint32_t *p = (uint32_t *) (mtag + 1);
938130407Sdfr			fd = fw_noderesolve_nodeid(fwip->fd.fc,
939130407Sdfr			    fp->mode.wreqb.src & 0x3f);
940130407Sdfr			if (fd) {
941130407Sdfr				p[0] = htonl(fd->eui.hi);
942130407Sdfr				p[1] = htonl(fd->eui.lo);
943130407Sdfr			} else {
944130407Sdfr				p[0] = 0;
945130407Sdfr				p[1] = 0;
946130407Sdfr			}
947130407Sdfr			m_tag_prepend(m, mtag);
948130407Sdfr		}
949130407Sdfr	}
950130407Sdfr
951130407Sdfr	/*
952130407Sdfr	 * Hand off to the generic encapsulation code. We don't use
953130407Sdfr	 * ifp->if_input so that we can pass the source nodeid as an
954130407Sdfr	 * argument to facilitate link-level fragment reassembly.
955130407Sdfr	 */
956130407Sdfr	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
957130407Sdfr	m->m_pkthdr.rcvif = ifp;
958130407Sdfr	firewire_input(ifp, m, fp->mode.wreqb.src);
959130407Sdfr	ifp->if_ipackets ++;
960130407Sdfr}
961130407Sdfr
962130407Sdfrstatic devclass_t fwip_devclass;
963130407Sdfr
964130407Sdfrstatic device_method_t fwip_methods[] = {
965130407Sdfr	/* device interface */
966130407Sdfr	DEVMETHOD(device_identify,	fwip_identify),
967130407Sdfr	DEVMETHOD(device_probe,		fwip_probe),
968130407Sdfr	DEVMETHOD(device_attach,	fwip_attach),
969130407Sdfr	DEVMETHOD(device_detach,	fwip_detach),
970130407Sdfr	{ 0, 0 }
971130407Sdfr};
972130407Sdfr
973130407Sdfrstatic driver_t fwip_driver = {
974130407Sdfr        "fwip",
975130407Sdfr	fwip_methods,
976130407Sdfr	sizeof(struct fwip_softc),
977130407Sdfr};
978130407Sdfr
979130407Sdfr
980130407Sdfr#ifdef __DragonFly__
981130407SdfrDECLARE_DUMMY_MODULE(fwip);
982130407Sdfr#endif
983130407SdfrDRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
984130407SdfrMODULE_VERSION(fwip, 1);
985130407SdfrMODULE_DEPEND(fwip, firewire, 1, 1, 1);
986