if_fwip.c revision 188394
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 188394 2009-02-09 16:58:18Z fjoe $
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>
59130407Sdfr#include <net/firewire.h>
60130407Sdfr#include <net/if_arp.h>
61147256Sbrooks#include <net/if_types.h>
62130407Sdfr#ifdef __DragonFly__
63130407Sdfr#include <bus/firewire/firewire.h>
64130407Sdfr#include <bus/firewire/firewirereg.h>
65130407Sdfr#include "if_fwipvar.h"
66130407Sdfr#else
67130407Sdfr#include <dev/firewire/firewire.h>
68130407Sdfr#include <dev/firewire/firewirereg.h>
69130411Sdfr#include <dev/firewire/iec13213.h>
70130407Sdfr#include <dev/firewire/if_fwipvar.h>
71130407Sdfr#endif
72130407Sdfr
73130407Sdfr/*
74130407Sdfr * We really need a mechanism for allocating regions in the FIFO
75130407Sdfr * address space. We pick a address in the OHCI controller's 'middle'
76130407Sdfr * address space. This means that the controller will automatically
77130407Sdfr * send responses for us, which is fine since we don't have any
78130407Sdfr * important information to put in the response anyway.
79130407Sdfr */
80130407Sdfr#define INET_FIFO	0xfffe00000000LL
81130407Sdfr
82130407Sdfr#define FWIPDEBUG	if (fwipdebug) if_printf
83130407Sdfr#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
84130407Sdfr
85130407Sdfr/* network interface */
86130407Sdfrstatic void fwip_start (struct ifnet *);
87130407Sdfrstatic int fwip_ioctl (struct ifnet *, u_long, caddr_t);
88130407Sdfrstatic void fwip_init (void *);
89130407Sdfr
90130407Sdfrstatic void fwip_post_busreset (void *);
91130407Sdfrstatic void fwip_output_callback (struct fw_xfer *);
92130407Sdfrstatic void fwip_async_output (struct fwip_softc *, struct ifnet *);
93130407Sdfrstatic void fwip_start_send (void *, int);
94130407Sdfrstatic void fwip_stream_input (struct fw_xferq *);
95130407Sdfrstatic void fwip_unicast_input(struct fw_xfer *);
96130407Sdfr
97130407Sdfrstatic int fwipdebug = 0;
98132445Sdfrstatic int broadcast_channel = 0xc0 | 0x1f; /*  tag | channel(XXX) */
99130407Sdfrstatic int tx_speed = 2;
100130407Sdfrstatic int rx_queue_len = FWMAXQUEUE;
101130407Sdfr
102130407SdfrMALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
103130407SdfrSYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
104130407SdfrSYSCTL_DECL(_hw_firewire);
105130407SdfrSYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
106130407Sdfr	"Firewire ip subsystem");
107130407SdfrSYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
108130407Sdfr	0, "Length of the receive queue");
109130407Sdfr
110130407SdfrTUNABLE_INT("hw.firewire.fwip.rx_queue_len", &rx_queue_len);
111130407Sdfr
112130407Sdfr#ifdef DEVICE_POLLING
113130407Sdfrstatic poll_handler_t fwip_poll;
114130407Sdfr
115130407Sdfrstatic void
116130407Sdfrfwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
117130407Sdfr{
118130407Sdfr	struct fwip_softc *fwip;
119130407Sdfr	struct firewire_comm *fc;
120130407Sdfr
121150789Sglebius	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
122150789Sglebius		return;
123150789Sglebius
124130407Sdfr	fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
125130407Sdfr	fc = fwip->fd.fc;
126130407Sdfr	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
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 ++) {
335177599Sru			m = m_getcl(M_WAIT, 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;
351177599Sru			m = m_getcl(M_WAIT, 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);
439150789Sglebius			ifp->if_capenable |= IFCAP_POLLING;
440150789Sglebius			return (error);
441150789Sglebius
442150789Sglebius		}
443150789Sglebius		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
444150789Sglebius		    ifp->if_capenable & IFCAP_POLLING) {
445150789Sglebius			error = ether_poll_deregister(ifp);
446150789Sglebius			/* Enable interrupts. */
447150789Sglebius			fc->set_intr(fc, 1);
448150789Sglebius			ifp->if_capenable &= ~IFCAP_POLLING;
449150789Sglebius			return (error);
450150789Sglebius		}
451150789Sglebius	    }
452150789Sglebius#endif /* DEVICE_POLLING */
453150789Sglebius		break;
454130407Sdfr#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
455130407Sdfr	default:
456130407Sdfr#else
457130407Sdfr	case SIOCSIFADDR:
458130407Sdfr	case SIOCGIFADDR:
459130407Sdfr	case SIOCSIFMTU:
460130407Sdfr#endif
461130407Sdfr		s = splimp();
462130407Sdfr		error = firewire_ioctl(ifp, cmd, data);
463130407Sdfr		splx(s);
464130407Sdfr		return (error);
465130407Sdfr#if defined(__DragonFly__) || __FreeBSD_version < 500000
466130407Sdfr	default:
467130407Sdfr		return (EINVAL);
468130407Sdfr#endif
469130407Sdfr	}
470130407Sdfr
471130407Sdfr	return (0);
472130407Sdfr}
473130407Sdfr
474130407Sdfrstatic void
475130407Sdfrfwip_post_busreset(void *arg)
476130407Sdfr{
477130407Sdfr	struct fwip_softc *fwip = arg;
478130411Sdfr	struct crom_src *src;
479130411Sdfr	struct crom_chunk *root;
480130407Sdfr
481130411Sdfr	src = fwip->fd.fc->crom_src;
482130411Sdfr	root = fwip->fd.fc->crom_root;
483130411Sdfr
484130411Sdfr	/* RFC2734 IPv4 over IEEE1394 */
485130411Sdfr	bzero(&fwip->unit4, sizeof(struct crom_chunk));
486130411Sdfr	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
487130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
488130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
489130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
490130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
491130411Sdfr
492130411Sdfr	/* RFC3146 IPv6 over IEEE1394 */
493130411Sdfr	bzero(&fwip->unit6, sizeof(struct crom_chunk));
494130411Sdfr	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
495130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
496130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
497130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
498130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
499130411Sdfr
500130407Sdfr	fwip->last_dest.hi = 0;
501130407Sdfr	fwip->last_dest.lo = 0;
502147256Sbrooks	firewire_busreset(fwip->fw_softc.fwip_ifp);
503130407Sdfr}
504130407Sdfr
505130407Sdfrstatic void
506130407Sdfrfwip_output_callback(struct fw_xfer *xfer)
507130407Sdfr{
508130407Sdfr	struct fwip_softc *fwip;
509130407Sdfr	struct ifnet *ifp;
510130407Sdfr	int s;
511130407Sdfr
512130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
513147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
514130407Sdfr	/* XXX error check */
515130407Sdfr	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
516130407Sdfr	if (xfer->resp != 0)
517130407Sdfr		ifp->if_oerrors ++;
518130407Sdfr
519130407Sdfr	m_freem(xfer->mbuf);
520130407Sdfr	fw_xfer_unload(xfer);
521130407Sdfr
522130407Sdfr	s = splimp();
523170374Ssimokawa	FWIP_LOCK(fwip);
524130407Sdfr	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
525170374Ssimokawa	FWIP_UNLOCK(fwip);
526130407Sdfr	splx(s);
527130407Sdfr
528130407Sdfr	/* for queue full */
529170374Ssimokawa	if (ifp->if_snd.ifq_head != NULL) {
530130407Sdfr		fwip_start(ifp);
531170374Ssimokawa	}
532130407Sdfr}
533130407Sdfr
534130407Sdfrstatic void
535130407Sdfrfwip_start(struct ifnet *ifp)
536130407Sdfr{
537130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
538130407Sdfr	int s;
539130407Sdfr
540130407Sdfr	FWIPDEBUG(ifp, "starting\n");
541130407Sdfr
542130407Sdfr	if (fwip->dma_ch < 0) {
543130407Sdfr		struct mbuf	*m = NULL;
544130407Sdfr
545130407Sdfr		FWIPDEBUG(ifp, "not ready\n");
546130407Sdfr
547130407Sdfr		s = splimp();
548130407Sdfr		do {
549130407Sdfr			IF_DEQUEUE(&ifp->if_snd, m);
550130407Sdfr			if (m != NULL)
551130407Sdfr				m_freem(m);
552130407Sdfr			ifp->if_oerrors ++;
553130407Sdfr		} while (m != NULL);
554130407Sdfr		splx(s);
555130407Sdfr
556130407Sdfr		return;
557130407Sdfr	}
558130407Sdfr
559130407Sdfr	s = splimp();
560148887Srwatson#if defined(__FreeBSD__)
561148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
562148887Srwatson#else
563130407Sdfr	ifp->if_flags |= IFF_OACTIVE;
564148887Srwatson#endif
565130407Sdfr
566130407Sdfr	if (ifp->if_snd.ifq_len != 0)
567130407Sdfr		fwip_async_output(fwip, ifp);
568130407Sdfr
569148887Srwatson#if defined(__FreeBSD__)
570148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
571148887Srwatson#else
572130407Sdfr	ifp->if_flags &= ~IFF_OACTIVE;
573148887Srwatson#endif
574130407Sdfr	splx(s);
575130407Sdfr}
576130407Sdfr
577130407Sdfr/* Async. stream output */
578130407Sdfrstatic void
579130407Sdfrfwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
580130407Sdfr{
581130407Sdfr	struct firewire_comm *fc = fwip->fd.fc;
582130407Sdfr	struct mbuf *m;
583130407Sdfr	struct m_tag *mtag;
584130407Sdfr	struct fw_hwaddr *destfw;
585130407Sdfr	struct fw_xfer *xfer;
586130407Sdfr	struct fw_xferq *xferq;
587130407Sdfr	struct fw_pkt *fp;
588130407Sdfr	uint16_t nodeid;
589130460Sdfr	int error;
590130407Sdfr	int i = 0;
591130407Sdfr
592130407Sdfr	xfer = NULL;
593170374Ssimokawa	xferq = fc->atq;
594170374Ssimokawa	while ((xferq->queued < xferq->maxq - 1) &&
595170374Ssimokawa			(ifp->if_snd.ifq_head != NULL)) {
596170374Ssimokawa		FWIP_LOCK(fwip);
597130407Sdfr		xfer = STAILQ_FIRST(&fwip->xferlist);
598130407Sdfr		if (xfer == NULL) {
599170374Ssimokawa			FWIP_UNLOCK(fwip);
600170374Ssimokawa#if 0
601130407Sdfr			printf("if_fwip: lack of xfer\n");
602170374Ssimokawa#endif
603170374Ssimokawa			break;
604130407Sdfr		}
605170374Ssimokawa		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
606170374Ssimokawa		FWIP_UNLOCK(fwip);
607170374Ssimokawa
608130407Sdfr		IF_DEQUEUE(&ifp->if_snd, m);
609170374Ssimokawa		if (m == NULL) {
610170374Ssimokawa			FWIP_LOCK(fwip);
611170374Ssimokawa			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
612170374Ssimokawa			FWIP_UNLOCK(fwip);
613130407Sdfr			break;
614170374Ssimokawa		}
615130407Sdfr
616130407Sdfr		/*
617130407Sdfr		 * Dig out the link-level address which
618130407Sdfr		 * firewire_output got via arp or neighbour
619130407Sdfr		 * discovery. If we don't have a link-level address,
620130407Sdfr		 * just stick the thing on the broadcast channel.
621130407Sdfr		 */
622130407Sdfr		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
623130407Sdfr		if (mtag == NULL)
624130407Sdfr			destfw = 0;
625130407Sdfr		else
626130407Sdfr			destfw = (struct fw_hwaddr *) (mtag + 1);
627130407Sdfr
628130407Sdfr
629130407Sdfr		/*
630130407Sdfr		 * We don't do any bpf stuff here - the generic code
631130407Sdfr		 * in firewire_output gives the packet to bpf before
632130407Sdfr		 * it adds the link-level encapsulation.
633130407Sdfr		 */
634130407Sdfr
635130407Sdfr		/*
636130407Sdfr		 * Put the mbuf in the xfer early in case we hit an
637130407Sdfr		 * error case below - fwip_output_callback will free
638130407Sdfr		 * the mbuf.
639130407Sdfr		 */
640130407Sdfr		xfer->mbuf = m;
641130407Sdfr
642130407Sdfr		/*
643130407Sdfr		 * We use the arp result (if any) to add a suitable firewire
644130407Sdfr		 * packet header before handing off to the bus.
645130407Sdfr		 */
646130407Sdfr		fp = &xfer->send.hdr;
647130407Sdfr		nodeid = FWLOCALBUS | fc->nodeid;
648130407Sdfr		if ((m->m_flags & M_BCAST) || !destfw) {
649130407Sdfr			/*
650130407Sdfr			 * Broadcast packets are sent as GASP packets with
651130407Sdfr			 * specifier ID 0x00005e, version 1 on the broadcast
652130407Sdfr			 * channel. To be conservative, we send at the
653130407Sdfr			 * slowest possible speed.
654130407Sdfr			 */
655130407Sdfr			uint32_t *p;
656130407Sdfr
657130407Sdfr			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
658130407Sdfr			p = mtod(m, uint32_t *);
659130407Sdfr			fp->mode.stream.len = m->m_pkthdr.len;
660130407Sdfr			fp->mode.stream.chtag = broadcast_channel;
661130407Sdfr			fp->mode.stream.tcode = FWTCODE_STREAM;
662130407Sdfr			fp->mode.stream.sy = 0;
663130407Sdfr			xfer->send.spd = 0;
664130407Sdfr			p[0] = htonl(nodeid << 16);
665130407Sdfr			p[1] = htonl((0x5e << 24) | 1);
666130407Sdfr		} else {
667130407Sdfr			/*
668130407Sdfr			 * Unicast packets are sent as block writes to the
669130407Sdfr			 * target's unicast fifo address. If we can't
670130407Sdfr			 * find the node address, we just give up. We
671130407Sdfr			 * could broadcast it but that might overflow
672130407Sdfr			 * the packet size limitations due to the
673130407Sdfr			 * extra GASP header. Note: the hardware
674130407Sdfr			 * address is stored in network byte order to
675130407Sdfr			 * make life easier for ARP.
676130407Sdfr			 */
677130407Sdfr			struct fw_device *fd;
678130407Sdfr			struct fw_eui64 eui;
679130407Sdfr
680130407Sdfr			eui.hi = ntohl(destfw->sender_unique_ID_hi);
681130407Sdfr			eui.lo = ntohl(destfw->sender_unique_ID_lo);
682130407Sdfr			if (fwip->last_dest.hi != eui.hi ||
683130407Sdfr			    fwip->last_dest.lo != eui.lo) {
684130407Sdfr				fd = fw_noderesolve_eui64(fc, &eui);
685130407Sdfr				if (!fd) {
686130407Sdfr					/* error */
687130407Sdfr					ifp->if_oerrors ++;
688130407Sdfr					/* XXX set error code */
689130407Sdfr					fwip_output_callback(xfer);
690130407Sdfr					continue;
691130407Sdfr
692130407Sdfr				}
693130407Sdfr				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
694130407Sdfr				fwip->last_hdr.mode.wreqb.tlrt = 0;
695130407Sdfr				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
696130407Sdfr				fwip->last_hdr.mode.wreqb.pri = 0;
697130407Sdfr				fwip->last_hdr.mode.wreqb.src = nodeid;
698130407Sdfr				fwip->last_hdr.mode.wreqb.dest_hi =
699130407Sdfr					ntohs(destfw->sender_unicast_FIFO_hi);
700130407Sdfr				fwip->last_hdr.mode.wreqb.dest_lo =
701130407Sdfr					ntohl(destfw->sender_unicast_FIFO_lo);
702130407Sdfr				fwip->last_hdr.mode.wreqb.extcode = 0;
703130407Sdfr				fwip->last_dest = eui;
704130407Sdfr			}
705130407Sdfr
706130407Sdfr			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
707130407Sdfr			fp->mode.wreqb.len = m->m_pkthdr.len;
708130407Sdfr			xfer->send.spd = min(destfw->sspd, fc->speed);
709130407Sdfr		}
710130407Sdfr
711130407Sdfr		xfer->send.pay_len = m->m_pkthdr.len;
712130407Sdfr
713130460Sdfr		error = fw_asyreq(fc, -1, xfer);
714130460Sdfr		if (error == EAGAIN) {
715130460Sdfr			/*
716130460Sdfr			 * We ran out of tlabels - requeue the packet
717130460Sdfr			 * for later transmission.
718130460Sdfr			 */
719130460Sdfr			xfer->mbuf = 0;
720170374Ssimokawa			FWIP_LOCK(fwip);
721130460Sdfr			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
722170374Ssimokawa			FWIP_UNLOCK(fwip);
723130460Sdfr			IF_PREPEND(&ifp->if_snd, m);
724130460Sdfr			break;
725130460Sdfr		}
726130460Sdfr		if (error) {
727130407Sdfr			/* error */
728130407Sdfr			ifp->if_oerrors ++;
729130407Sdfr			/* XXX set error code */
730130407Sdfr			fwip_output_callback(xfer);
731130407Sdfr			continue;
732130407Sdfr		} else {
733130407Sdfr			ifp->if_opackets ++;
734130407Sdfr			i++;
735130407Sdfr		}
736130407Sdfr	}
737130407Sdfr#if 0
738130407Sdfr	if (i > 1)
739130407Sdfr		printf("%d queued\n", i);
740130407Sdfr#endif
741170374Ssimokawa	if (i > 0)
742130407Sdfr		xferq->start(fc);
743130407Sdfr}
744130407Sdfr
745130407Sdfrstatic void
746130407Sdfrfwip_start_send (void *arg, int count)
747130407Sdfr{
748130407Sdfr	struct fwip_softc *fwip = arg;
749130407Sdfr
750130407Sdfr	fwip->fd.fc->atq->start(fwip->fd.fc);
751130407Sdfr}
752130407Sdfr
753130407Sdfr/* Async. stream output */
754130407Sdfrstatic void
755130407Sdfrfwip_stream_input(struct fw_xferq *xferq)
756130407Sdfr{
757130407Sdfr	struct mbuf *m, *m0;
758130407Sdfr	struct m_tag *mtag;
759130407Sdfr	struct ifnet *ifp;
760130407Sdfr	struct fwip_softc *fwip;
761130407Sdfr	struct fw_bulkxfer *sxfer;
762130407Sdfr	struct fw_pkt *fp;
763130407Sdfr	uint16_t src;
764130407Sdfr	uint32_t *p;
765130407Sdfr
766130407Sdfr
767130407Sdfr	fwip = (struct fwip_softc *)xferq->sc;
768147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
769150789Sglebius
770130407Sdfr	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
771130407Sdfr		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
772130407Sdfr		fp = mtod(sxfer->mbuf, struct fw_pkt *);
773130407Sdfr		if (fwip->fd.fc->irx_post != NULL)
774130407Sdfr			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
775130407Sdfr		m = sxfer->mbuf;
776130407Sdfr
777130407Sdfr		/* insert new rbuf */
778130407Sdfr		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
779130407Sdfr		if (m0 != NULL) {
780130407Sdfr			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
781130407Sdfr			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
782130407Sdfr		} else
783130407Sdfr			printf("fwip_as_input: m_getcl failed\n");
784130407Sdfr
785130407Sdfr		/*
786130407Sdfr		 * We must have a GASP header - leave the
787130407Sdfr		 * encapsulation sanity checks to the generic
788130407Sdfr		 * code. Remeber that we also have the firewire async
789130407Sdfr		 * stream header even though that isn't accounted for
790130407Sdfr		 * in mode.stream.len.
791130407Sdfr		 */
792130407Sdfr		if (sxfer->resp != 0 || fp->mode.stream.len <
793130407Sdfr		    2*sizeof(uint32_t)) {
794130407Sdfr			m_freem(m);
795130407Sdfr			ifp->if_ierrors ++;
796130407Sdfr			continue;
797130407Sdfr		}
798130407Sdfr		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
799130407Sdfr			+ sizeof(fp->mode.stream);
800130407Sdfr
801130407Sdfr		/*
802130407Sdfr		 * If we received the packet on the broadcast channel,
803130407Sdfr		 * mark it as broadcast, otherwise we assume it must
804130407Sdfr		 * be multicast.
805130407Sdfr		 */
806130407Sdfr		if (fp->mode.stream.chtag == broadcast_channel)
807130407Sdfr			m->m_flags |= M_BCAST;
808130407Sdfr		else
809130407Sdfr			m->m_flags |= M_MCAST;
810130407Sdfr
811130407Sdfr		/*
812130407Sdfr		 * Make sure we recognise the GASP specifier and
813130407Sdfr		 * version.
814130407Sdfr		 */
815130407Sdfr		p = mtod(m, uint32_t *);
816130407Sdfr		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
817130407Sdfr		    || (ntohl(p[2]) & 0xffffff) != 1) {
818130407Sdfr			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
819130407Sdfr			    ntohl(p[1]), ntohl(p[2]));
820130407Sdfr			m_freem(m);
821130407Sdfr			ifp->if_ierrors ++;
822130407Sdfr			continue;
823130407Sdfr		}
824130407Sdfr
825130407Sdfr		/*
826130407Sdfr		 * Record the sender ID for possible BPF usage.
827130407Sdfr		 */
828130407Sdfr		src = ntohl(p[1]) >> 16;
829165632Sjhb		if (bpf_peers_present(ifp->if_bpf)) {
830130407Sdfr			mtag = m_tag_alloc(MTAG_FIREWIRE,
831130407Sdfr			    MTAG_FIREWIRE_SENDER_EUID,
832130407Sdfr			    2*sizeof(uint32_t), M_NOWAIT);
833130407Sdfr			if (mtag) {
834130407Sdfr				/* bpf wants it in network byte order */
835130407Sdfr				struct fw_device *fd;
836130407Sdfr				uint32_t *p = (uint32_t *) (mtag + 1);
837130407Sdfr				fd = fw_noderesolve_nodeid(fwip->fd.fc,
838130407Sdfr				    src & 0x3f);
839130407Sdfr				if (fd) {
840130407Sdfr					p[0] = htonl(fd->eui.hi);
841130407Sdfr					p[1] = htonl(fd->eui.lo);
842130407Sdfr				} else {
843130407Sdfr					p[0] = 0;
844130407Sdfr					p[1] = 0;
845130407Sdfr				}
846130407Sdfr				m_tag_prepend(m, mtag);
847130407Sdfr			}
848130407Sdfr		}
849130407Sdfr
850130407Sdfr		/*
851130407Sdfr		 * Trim off the GASP header
852130407Sdfr		 */
853130407Sdfr		m_adj(m, 3*sizeof(uint32_t));
854130407Sdfr		m->m_pkthdr.rcvif = ifp;
855130407Sdfr		firewire_input(ifp, m, src);
856130407Sdfr		ifp->if_ipackets ++;
857130407Sdfr	}
858130407Sdfr	if (STAILQ_FIRST(&xferq->stfree) != NULL)
859130407Sdfr		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
860130407Sdfr}
861130407Sdfr
862130407Sdfrstatic __inline void
863130407Sdfrfwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
864130407Sdfr{
865130407Sdfr	struct mbuf *m;
866130407Sdfr
867130407Sdfr	/*
868130407Sdfr	 * We have finished with a unicast xfer. Allocate a new
869130407Sdfr	 * cluster and stick it on the back of the input queue.
870130407Sdfr	 */
871177599Sru	m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
872130407Sdfr	xfer->mbuf = m;
873130407Sdfr	xfer->recv.payload = mtod(m, uint32_t *);
874130407Sdfr	xfer->recv.pay_len = MCLBYTES;
875130407Sdfr	xfer->mbuf = m;
876130407Sdfr	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
877130407Sdfr}
878130407Sdfr
879130407Sdfrstatic void
880130407Sdfrfwip_unicast_input(struct fw_xfer *xfer)
881130407Sdfr{
882130407Sdfr	uint64_t address;
883130407Sdfr	struct mbuf *m;
884130407Sdfr	struct m_tag *mtag;
885130407Sdfr	struct ifnet *ifp;
886130407Sdfr	struct fwip_softc *fwip;
887130407Sdfr	struct fw_pkt *fp;
888130407Sdfr	//struct fw_pkt *sfp;
889130407Sdfr	int rtcode;
890130407Sdfr
891130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
892147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
893130407Sdfr	m = xfer->mbuf;
894130407Sdfr	xfer->mbuf = 0;
895130407Sdfr	fp = &xfer->recv.hdr;
896130407Sdfr
897130407Sdfr	/*
898130407Sdfr	 * Check the fifo address - we only accept addresses of
899130407Sdfr	 * exactly INET_FIFO.
900130407Sdfr	 */
901130407Sdfr	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
902130407Sdfr		| fp->mode.wreqb.dest_lo;
903130407Sdfr	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
904130407Sdfr		rtcode = FWRCODE_ER_TYPE;
905130407Sdfr	} else if (address != INET_FIFO) {
906130407Sdfr		rtcode = FWRCODE_ER_ADDR;
907130407Sdfr	} else {
908130407Sdfr		rtcode = FWRCODE_COMPLETE;
909130407Sdfr	}
910130407Sdfr
911130407Sdfr	/*
912130407Sdfr	 * Pick up a new mbuf and stick it on the back of the receive
913130407Sdfr	 * queue.
914130407Sdfr	 */
915130407Sdfr	fwip_unicast_input_recycle(fwip, xfer);
916130407Sdfr
917130407Sdfr	/*
918130407Sdfr	 * If we've already rejected the packet, give up now.
919130407Sdfr	 */
920130407Sdfr	if (rtcode != FWRCODE_COMPLETE) {
921130407Sdfr		m_freem(m);
922130407Sdfr		ifp->if_ierrors ++;
923130407Sdfr		return;
924130407Sdfr	}
925130407Sdfr
926165632Sjhb	if (bpf_peers_present(ifp->if_bpf)) {
927130407Sdfr		/*
928130407Sdfr		 * Record the sender ID for possible BPF usage.
929130407Sdfr		 */
930130407Sdfr		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
931130407Sdfr		    2*sizeof(uint32_t), M_NOWAIT);
932130407Sdfr		if (mtag) {
933130407Sdfr			/* bpf wants it in network byte order */
934130407Sdfr			struct fw_device *fd;
935130407Sdfr			uint32_t *p = (uint32_t *) (mtag + 1);
936130407Sdfr			fd = fw_noderesolve_nodeid(fwip->fd.fc,
937130407Sdfr			    fp->mode.wreqb.src & 0x3f);
938130407Sdfr			if (fd) {
939130407Sdfr				p[0] = htonl(fd->eui.hi);
940130407Sdfr				p[1] = htonl(fd->eui.lo);
941130407Sdfr			} else {
942130407Sdfr				p[0] = 0;
943130407Sdfr				p[1] = 0;
944130407Sdfr			}
945130407Sdfr			m_tag_prepend(m, mtag);
946130407Sdfr		}
947130407Sdfr	}
948130407Sdfr
949130407Sdfr	/*
950130407Sdfr	 * Hand off to the generic encapsulation code. We don't use
951130407Sdfr	 * ifp->if_input so that we can pass the source nodeid as an
952130407Sdfr	 * argument to facilitate link-level fragment reassembly.
953130407Sdfr	 */
954130407Sdfr	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
955130407Sdfr	m->m_pkthdr.rcvif = ifp;
956130407Sdfr	firewire_input(ifp, m, fp->mode.wreqb.src);
957130407Sdfr	ifp->if_ipackets ++;
958130407Sdfr}
959130407Sdfr
960130407Sdfrstatic devclass_t fwip_devclass;
961130407Sdfr
962130407Sdfrstatic device_method_t fwip_methods[] = {
963130407Sdfr	/* device interface */
964130407Sdfr	DEVMETHOD(device_identify,	fwip_identify),
965130407Sdfr	DEVMETHOD(device_probe,		fwip_probe),
966130407Sdfr	DEVMETHOD(device_attach,	fwip_attach),
967130407Sdfr	DEVMETHOD(device_detach,	fwip_detach),
968130407Sdfr	{ 0, 0 }
969130407Sdfr};
970130407Sdfr
971130407Sdfrstatic driver_t fwip_driver = {
972130407Sdfr        "fwip",
973130407Sdfr	fwip_methods,
974130407Sdfr	sizeof(struct fwip_softc),
975130407Sdfr};
976130407Sdfr
977130407Sdfr
978130407Sdfr#ifdef __DragonFly__
979130407SdfrDECLARE_DUMMY_MODULE(fwip);
980130407Sdfr#endif
981130407SdfrDRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
982130407SdfrMODULE_VERSION(fwip, 1);
983130407SdfrMODULE_DEPEND(fwip, firewire, 1, 1, 1);
984