if_fwip.c revision 150968
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 150968 2005-10-05 10:09:17Z glebius $
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
164130407Sdfr	/* XXX */
165130407Sdfr	fwip->dma_ch = -1;
166130407Sdfr
167130407Sdfr	fwip->fd.fc = device_get_ivars(dev);
168130407Sdfr	if (tx_speed < 0)
169130407Sdfr		tx_speed = fwip->fd.fc->speed;
170130407Sdfr
171130407Sdfr	fwip->fd.dev = dev;
172130407Sdfr	fwip->fd.post_explore = NULL;
173130407Sdfr	fwip->fd.post_busreset = fwip_post_busreset;
174130407Sdfr	fwip->fw_softc.fwip = fwip;
175130407Sdfr	TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
176130407Sdfr
177130407Sdfr	/*
178130407Sdfr	 * Encode our hardware the way that arp likes it.
179130407Sdfr	 */
180147256Sbrooks	hwaddr = &IFP2FWC(fwip->fw_softc.fwip_ifp)->fc_hwaddr;
181130407Sdfr	hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
182130407Sdfr	hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
183130407Sdfr	hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
184130407Sdfr	hwaddr->sspd = fwip->fd.fc->speed;
185130407Sdfr	hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
186130407Sdfr	hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
187130407Sdfr
188130407Sdfr	/* fill the rest and attach interface */
189130407Sdfr	ifp->if_softc = &fwip->fw_softc;
190130407Sdfr
191130407Sdfr#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
192130407Sdfr	if_initname(ifp, device_get_name(dev), unit);
193130407Sdfr#else
194130407Sdfr	ifp->if_unit = unit;
195130407Sdfr	ifp->if_name = "fwip";
196130407Sdfr#endif
197130407Sdfr	ifp->if_init = fwip_init;
198130407Sdfr	ifp->if_start = fwip_start;
199130407Sdfr	ifp->if_ioctl = fwip_ioctl;
200133686Srwatson	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
201133686Srwatson	    IFF_NEEDSGIANT);
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);
285130407Sdfr
286130407Sdfr	splx(s);
287130407Sdfr	return 0;
288130407Sdfr}
289130407Sdfr
290130407Sdfrstatic void
291130407Sdfrfwip_init(void *arg)
292130407Sdfr{
293130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)arg)->fwip;
294130407Sdfr	struct firewire_comm *fc;
295147256Sbrooks	struct ifnet *ifp = fwip->fw_softc.fwip_ifp;
296130407Sdfr	struct fw_xferq *xferq;
297130407Sdfr	struct fw_xfer *xfer;
298130407Sdfr	struct mbuf *m;
299130407Sdfr	int i;
300130407Sdfr
301130407Sdfr	FWIPDEBUG(ifp, "initializing\n");
302130407Sdfr
303130407Sdfr	fc = fwip->fd.fc;
304130407Sdfr#define START 0
305130407Sdfr	if (fwip->dma_ch < 0) {
306130407Sdfr		for (i = START; i < fc->nisodma; i ++) {
307130407Sdfr			xferq = fc->ir[i];
308130407Sdfr			if ((xferq->flag & FWXFERQ_OPEN) == 0)
309130407Sdfr				goto found;
310130407Sdfr		}
311130407Sdfr		printf("no free dma channel\n");
312130407Sdfr		return;
313130407Sdfrfound:
314130407Sdfr		fwip->dma_ch = i;
315130407Sdfr		/* allocate DMA channel and init packet mode */
316130407Sdfr		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
317130407Sdfr				FWXFERQ_HANDLER | FWXFERQ_STREAM;
318130407Sdfr		xferq->flag &= ~0xff;
319130407Sdfr		xferq->flag |= broadcast_channel & 0xff;
320130407Sdfr		/* register fwip_input handler */
321130407Sdfr		xferq->sc = (caddr_t) fwip;
322130407Sdfr		xferq->hand = fwip_stream_input;
323130407Sdfr		xferq->bnchunk = rx_queue_len;
324130407Sdfr		xferq->bnpacket = 1;
325130407Sdfr		xferq->psize = MCLBYTES;
326130407Sdfr		xferq->queued = 0;
327130407Sdfr		xferq->buf = NULL;
328130407Sdfr		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
329130407Sdfr			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
330130407Sdfr							M_FWIP, M_WAITOK);
331130407Sdfr		if (xferq->bulkxfer == NULL) {
332130407Sdfr			printf("if_fwip: malloc failed\n");
333130407Sdfr			return;
334130407Sdfr		}
335130407Sdfr		STAILQ_INIT(&xferq->stvalid);
336130407Sdfr		STAILQ_INIT(&xferq->stfree);
337130407Sdfr		STAILQ_INIT(&xferq->stdma);
338130407Sdfr		xferq->stproc = NULL;
339130407Sdfr		for (i = 0; i < xferq->bnchunk; i ++) {
340130407Sdfr			m =
341130407Sdfr#if defined(__DragonFly__) || __FreeBSD_version < 500000
342130407Sdfr				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
343130407Sdfr#else
344130407Sdfr				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
345130407Sdfr#endif
346130407Sdfr			xferq->bulkxfer[i].mbuf = m;
347130407Sdfr			if (m != NULL) {
348130407Sdfr				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
349130407Sdfr				STAILQ_INSERT_TAIL(&xferq->stfree,
350130407Sdfr						&xferq->bulkxfer[i], link);
351130407Sdfr			} else
352130407Sdfr				printf("fwip_as_input: m_getcl failed\n");
353130407Sdfr		}
354130407Sdfr
355130407Sdfr		fwip->fwb.start = INET_FIFO;
356130407Sdfr		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
357130407Sdfr		fwip->fwb.act_type = FWACT_XFER;
358130407Sdfr
359130407Sdfr		/* pre-allocate xfer */
360130407Sdfr		STAILQ_INIT(&fwip->fwb.xferlist);
361130407Sdfr		for (i = 0; i < rx_queue_len; i ++) {
362130407Sdfr			xfer = fw_xfer_alloc(M_FWIP);
363130407Sdfr			if (xfer == NULL)
364130407Sdfr				break;
365130407Sdfr			m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
366130407Sdfr			xfer->recv.payload = mtod(m, uint32_t *);
367130407Sdfr			xfer->recv.pay_len = MCLBYTES;
368130407Sdfr			xfer->act.hand = fwip_unicast_input;
369130407Sdfr			xfer->fc = fc;
370130407Sdfr			xfer->sc = (caddr_t)fwip;
371130407Sdfr			xfer->mbuf = m;
372130407Sdfr			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
373130407Sdfr		}
374130407Sdfr		fw_bindadd(fc, &fwip->fwb);
375130407Sdfr
376130407Sdfr		STAILQ_INIT(&fwip->xferlist);
377130407Sdfr		for (i = 0; i < TX_MAX_QUEUE; i++) {
378130407Sdfr			xfer = fw_xfer_alloc(M_FWIP);
379130407Sdfr			if (xfer == NULL)
380130407Sdfr				break;
381130407Sdfr			xfer->send.spd = tx_speed;
382130407Sdfr			xfer->fc = fwip->fd.fc;
383130407Sdfr			xfer->retry_req = fw_asybusy;
384130407Sdfr			xfer->sc = (caddr_t)fwip;
385130407Sdfr			xfer->act.hand = fwip_output_callback;
386130407Sdfr			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
387130407Sdfr		}
388130407Sdfr	} else
389130407Sdfr		xferq = fc->ir[fwip->dma_ch];
390130407Sdfr
391130407Sdfr	fwip->last_dest.hi = 0;
392130407Sdfr	fwip->last_dest.lo = 0;
393130407Sdfr
394130407Sdfr	/* start dma */
395130407Sdfr	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
396130407Sdfr		fc->irx_enable(fc, fwip->dma_ch);
397130407Sdfr
398148887Srwatson#if defined(__FreeBSD__)
399148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
400148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
401148887Srwatson#else
402130407Sdfr	ifp->if_flags |= IFF_RUNNING;
403130407Sdfr	ifp->if_flags &= ~IFF_OACTIVE;
404148887Srwatson#endif
405130407Sdfr
406130407Sdfr#if 0
407130407Sdfr	/* attempt to start output */
408130407Sdfr	fwip_start(ifp);
409130407Sdfr#endif
410130407Sdfr}
411130407Sdfr
412130407Sdfrstatic int
413130407Sdfrfwip_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
414130407Sdfr{
415130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
416130407Sdfr	int s, error;
417130407Sdfr
418130407Sdfr	switch (cmd) {
419130407Sdfr	case SIOCSIFFLAGS:
420130407Sdfr		s = splimp();
421130407Sdfr		if (ifp->if_flags & IFF_UP) {
422148887Srwatson#if defined(__FreeBSD__)
423148887Srwatson			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
424148887Srwatson#else
425130407Sdfr			if (!(ifp->if_flags & IFF_RUNNING))
426148887Srwatson#endif
427130407Sdfr				fwip_init(&fwip->fw_softc);
428130407Sdfr		} else {
429148887Srwatson#if defined(__FreeBSD__)
430148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
431148887Srwatson#else
432130407Sdfr			if (ifp->if_flags & IFF_RUNNING)
433148887Srwatson#endif
434130407Sdfr				fwip_stop(fwip);
435130407Sdfr		}
436130407Sdfr		splx(s);
437130407Sdfr		break;
438130407Sdfr	case SIOCADDMULTI:
439130407Sdfr	case SIOCDELMULTI:
440130407Sdfr		break;
441150789Sglebius	case SIOCSIFCAP:
442150789Sglebius#ifdef DEVICE_POLLING
443150789Sglebius	    {
444150789Sglebius		struct ifreq *ifr = (struct ifreq *) data;
445150789Sglebius		struct firewire_comm *fc = fc = fwip->fd.fc;
446130407Sdfr
447150789Sglebius		if (ifr->ifr_reqcap & IFCAP_POLLING &&
448150789Sglebius		    !(ifp->if_capenable & IFCAP_POLLING)) {
449150789Sglebius			error = ether_poll_register(fwip_poll, ifp);
450150789Sglebius			if (error)
451150789Sglebius				return(error);
452150789Sglebius			/* Disable interrupts */
453150789Sglebius			fc->set_intr(fc, 0);
454150789Sglebius			ifp->if_capenable |= IFCAP_POLLING;
455150789Sglebius			return (error);
456150789Sglebius
457150789Sglebius		}
458150789Sglebius		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
459150789Sglebius		    ifp->if_capenable & IFCAP_POLLING) {
460150789Sglebius			error = ether_poll_deregister(ifp);
461150789Sglebius			/* Enable interrupts. */
462150789Sglebius			fc->set_intr(fc, 1);
463150789Sglebius			ifp->if_capenable &= ~IFCAP_POLLING;
464150789Sglebius			return (error);
465150789Sglebius		}
466150789Sglebius	    }
467150789Sglebius#endif /* DEVICE_POLLING */
468150789Sglebius		break;
469130407Sdfr#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
470130407Sdfr	default:
471130407Sdfr#else
472130407Sdfr	case SIOCSIFADDR:
473130407Sdfr	case SIOCGIFADDR:
474130407Sdfr	case SIOCSIFMTU:
475130407Sdfr#endif
476130407Sdfr		s = splimp();
477130407Sdfr		error = firewire_ioctl(ifp, cmd, data);
478130407Sdfr		splx(s);
479130407Sdfr		return (error);
480130407Sdfr#if defined(__DragonFly__) || __FreeBSD_version < 500000
481130407Sdfr	default:
482130407Sdfr		return (EINVAL);
483130407Sdfr#endif
484130407Sdfr	}
485130407Sdfr
486130407Sdfr	return (0);
487130407Sdfr}
488130407Sdfr
489130407Sdfrstatic void
490130407Sdfrfwip_post_busreset(void *arg)
491130407Sdfr{
492130407Sdfr	struct fwip_softc *fwip = arg;
493130411Sdfr	struct crom_src *src;
494130411Sdfr	struct crom_chunk *root;
495130407Sdfr
496130411Sdfr	src = fwip->fd.fc->crom_src;
497130411Sdfr	root = fwip->fd.fc->crom_root;
498130411Sdfr
499130411Sdfr	/* RFC2734 IPv4 over IEEE1394 */
500130411Sdfr	bzero(&fwip->unit4, sizeof(struct crom_chunk));
501130411Sdfr	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
502130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
503130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
504130411Sdfr	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
505130411Sdfr	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
506130411Sdfr
507130411Sdfr	/* RFC3146 IPv6 over IEEE1394 */
508130411Sdfr	bzero(&fwip->unit6, sizeof(struct crom_chunk));
509130411Sdfr	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
510130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
511130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
512130411Sdfr	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
513130411Sdfr	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
514130411Sdfr
515130407Sdfr	fwip->last_dest.hi = 0;
516130407Sdfr	fwip->last_dest.lo = 0;
517147256Sbrooks	firewire_busreset(fwip->fw_softc.fwip_ifp);
518130407Sdfr}
519130407Sdfr
520130407Sdfrstatic void
521130407Sdfrfwip_output_callback(struct fw_xfer *xfer)
522130407Sdfr{
523130407Sdfr	struct fwip_softc *fwip;
524130407Sdfr	struct ifnet *ifp;
525130407Sdfr	int s;
526130407Sdfr
527130407Sdfr	GIANT_REQUIRED;
528130407Sdfr
529130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
530147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
531130407Sdfr	/* XXX error check */
532130407Sdfr	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
533130407Sdfr	if (xfer->resp != 0)
534130407Sdfr		ifp->if_oerrors ++;
535130407Sdfr
536130407Sdfr	m_freem(xfer->mbuf);
537130407Sdfr	fw_xfer_unload(xfer);
538130407Sdfr
539130407Sdfr	s = splimp();
540130407Sdfr	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
541130407Sdfr	splx(s);
542130407Sdfr
543130407Sdfr	/* for queue full */
544130407Sdfr	if (ifp->if_snd.ifq_head != NULL)
545130407Sdfr		fwip_start(ifp);
546130407Sdfr}
547130407Sdfr
548130407Sdfrstatic void
549130407Sdfrfwip_start(struct ifnet *ifp)
550130407Sdfr{
551130407Sdfr	struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
552130407Sdfr	int s;
553130407Sdfr
554130407Sdfr	GIANT_REQUIRED;
555130407Sdfr
556130407Sdfr	FWIPDEBUG(ifp, "starting\n");
557130407Sdfr
558130407Sdfr	if (fwip->dma_ch < 0) {
559130407Sdfr		struct mbuf	*m = NULL;
560130407Sdfr
561130407Sdfr		FWIPDEBUG(ifp, "not ready\n");
562130407Sdfr
563130407Sdfr		s = splimp();
564130407Sdfr		do {
565130407Sdfr			IF_DEQUEUE(&ifp->if_snd, m);
566130407Sdfr			if (m != NULL)
567130407Sdfr				m_freem(m);
568130407Sdfr			ifp->if_oerrors ++;
569130407Sdfr		} while (m != NULL);
570130407Sdfr		splx(s);
571130407Sdfr
572130407Sdfr		return;
573130407Sdfr	}
574130407Sdfr
575130407Sdfr	s = splimp();
576148887Srwatson#if defined(__FreeBSD__)
577148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
578148887Srwatson#else
579130407Sdfr	ifp->if_flags |= IFF_OACTIVE;
580148887Srwatson#endif
581130407Sdfr
582130407Sdfr	if (ifp->if_snd.ifq_len != 0)
583130407Sdfr		fwip_async_output(fwip, ifp);
584130407Sdfr
585148887Srwatson#if defined(__FreeBSD__)
586148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
587148887Srwatson#else
588130407Sdfr	ifp->if_flags &= ~IFF_OACTIVE;
589148887Srwatson#endif
590130407Sdfr	splx(s);
591130407Sdfr}
592130407Sdfr
593130407Sdfr/* Async. stream output */
594130407Sdfrstatic void
595130407Sdfrfwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
596130407Sdfr{
597130407Sdfr	struct firewire_comm *fc = fwip->fd.fc;
598130407Sdfr	struct mbuf *m;
599130407Sdfr	struct m_tag *mtag;
600130407Sdfr	struct fw_hwaddr *destfw;
601130407Sdfr	struct fw_xfer *xfer;
602130407Sdfr	struct fw_xferq *xferq;
603130407Sdfr	struct fw_pkt *fp;
604130407Sdfr	uint16_t nodeid;
605130460Sdfr	int error;
606130407Sdfr	int i = 0;
607130407Sdfr
608130407Sdfr	GIANT_REQUIRED;
609130407Sdfr
610130407Sdfr	xfer = NULL;
611130407Sdfr	xferq = fwip->fd.fc->atq;
612130407Sdfr	while (xferq->queued < xferq->maxq - 1) {
613130407Sdfr		xfer = STAILQ_FIRST(&fwip->xferlist);
614130407Sdfr		if (xfer == NULL) {
615130407Sdfr			printf("if_fwip: lack of xfer\n");
616130407Sdfr			return;
617130407Sdfr		}
618130407Sdfr		IF_DEQUEUE(&ifp->if_snd, m);
619130407Sdfr		if (m == NULL)
620130407Sdfr			break;
621130407Sdfr
622130407Sdfr		/*
623130407Sdfr		 * Dig out the link-level address which
624130407Sdfr		 * firewire_output got via arp or neighbour
625130407Sdfr		 * discovery. If we don't have a link-level address,
626130407Sdfr		 * just stick the thing on the broadcast channel.
627130407Sdfr		 */
628130407Sdfr		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
629130407Sdfr		if (mtag == NULL)
630130407Sdfr			destfw = 0;
631130407Sdfr		else
632130407Sdfr			destfw = (struct fw_hwaddr *) (mtag + 1);
633130407Sdfr
634130407Sdfr		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
635130407Sdfr
636130407Sdfr		/*
637130407Sdfr		 * We don't do any bpf stuff here - the generic code
638130407Sdfr		 * in firewire_output gives the packet to bpf before
639130407Sdfr		 * it adds the link-level encapsulation.
640130407Sdfr		 */
641130407Sdfr
642130407Sdfr		/*
643130407Sdfr		 * Put the mbuf in the xfer early in case we hit an
644130407Sdfr		 * error case below - fwip_output_callback will free
645130407Sdfr		 * the mbuf.
646130407Sdfr		 */
647130407Sdfr		xfer->mbuf = m;
648130407Sdfr
649130407Sdfr		/*
650130407Sdfr		 * We use the arp result (if any) to add a suitable firewire
651130407Sdfr		 * packet header before handing off to the bus.
652130407Sdfr		 */
653130407Sdfr		fp = &xfer->send.hdr;
654130407Sdfr		nodeid = FWLOCALBUS | fc->nodeid;
655130407Sdfr		if ((m->m_flags & M_BCAST) || !destfw) {
656130407Sdfr			/*
657130407Sdfr			 * Broadcast packets are sent as GASP packets with
658130407Sdfr			 * specifier ID 0x00005e, version 1 on the broadcast
659130407Sdfr			 * channel. To be conservative, we send at the
660130407Sdfr			 * slowest possible speed.
661130407Sdfr			 */
662130407Sdfr			uint32_t *p;
663130407Sdfr
664130407Sdfr			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
665130407Sdfr			p = mtod(m, uint32_t *);
666130407Sdfr			fp->mode.stream.len = m->m_pkthdr.len;
667130407Sdfr			fp->mode.stream.chtag = broadcast_channel;
668130407Sdfr			fp->mode.stream.tcode = FWTCODE_STREAM;
669130407Sdfr			fp->mode.stream.sy = 0;
670130407Sdfr			xfer->send.spd = 0;
671130407Sdfr			p[0] = htonl(nodeid << 16);
672130407Sdfr			p[1] = htonl((0x5e << 24) | 1);
673130407Sdfr		} else {
674130407Sdfr			/*
675130407Sdfr			 * Unicast packets are sent as block writes to the
676130407Sdfr			 * target's unicast fifo address. If we can't
677130407Sdfr			 * find the node address, we just give up. We
678130407Sdfr			 * could broadcast it but that might overflow
679130407Sdfr			 * the packet size limitations due to the
680130407Sdfr			 * extra GASP header. Note: the hardware
681130407Sdfr			 * address is stored in network byte order to
682130407Sdfr			 * make life easier for ARP.
683130407Sdfr			 */
684130407Sdfr			struct fw_device *fd;
685130407Sdfr			struct fw_eui64 eui;
686130407Sdfr
687130407Sdfr			eui.hi = ntohl(destfw->sender_unique_ID_hi);
688130407Sdfr			eui.lo = ntohl(destfw->sender_unique_ID_lo);
689130407Sdfr			if (fwip->last_dest.hi != eui.hi ||
690130407Sdfr			    fwip->last_dest.lo != eui.lo) {
691130407Sdfr				fd = fw_noderesolve_eui64(fc, &eui);
692130407Sdfr				if (!fd) {
693130407Sdfr					/* error */
694130407Sdfr					ifp->if_oerrors ++;
695130407Sdfr					/* XXX set error code */
696130407Sdfr					fwip_output_callback(xfer);
697130407Sdfr					continue;
698130407Sdfr
699130407Sdfr				}
700130407Sdfr				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
701130407Sdfr				fwip->last_hdr.mode.wreqb.tlrt = 0;
702130407Sdfr				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
703130407Sdfr				fwip->last_hdr.mode.wreqb.pri = 0;
704130407Sdfr				fwip->last_hdr.mode.wreqb.src = nodeid;
705130407Sdfr				fwip->last_hdr.mode.wreqb.dest_hi =
706130407Sdfr					ntohs(destfw->sender_unicast_FIFO_hi);
707130407Sdfr				fwip->last_hdr.mode.wreqb.dest_lo =
708130407Sdfr					ntohl(destfw->sender_unicast_FIFO_lo);
709130407Sdfr				fwip->last_hdr.mode.wreqb.extcode = 0;
710130407Sdfr				fwip->last_dest = eui;
711130407Sdfr			}
712130407Sdfr
713130407Sdfr			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
714130407Sdfr			fp->mode.wreqb.len = m->m_pkthdr.len;
715130407Sdfr			xfer->send.spd = min(destfw->sspd, fc->speed);
716130407Sdfr		}
717130407Sdfr
718130407Sdfr		xfer->send.pay_len = m->m_pkthdr.len;
719130407Sdfr
720130460Sdfr		error = fw_asyreq(fc, -1, xfer);
721130460Sdfr		if (error == EAGAIN) {
722130460Sdfr			/*
723130460Sdfr			 * We ran out of tlabels - requeue the packet
724130460Sdfr			 * for later transmission.
725130460Sdfr			 */
726130460Sdfr			xfer->mbuf = 0;
727130460Sdfr			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
728130460Sdfr			IF_PREPEND(&ifp->if_snd, m);
729130460Sdfr			break;
730130460Sdfr		}
731130460Sdfr		if (error) {
732130407Sdfr			/* error */
733130407Sdfr			ifp->if_oerrors ++;
734130407Sdfr			/* XXX set error code */
735130407Sdfr			fwip_output_callback(xfer);
736130407Sdfr			continue;
737130407Sdfr		} else {
738130407Sdfr			ifp->if_opackets ++;
739130407Sdfr			i++;
740130407Sdfr		}
741130407Sdfr	}
742130407Sdfr#if 0
743130407Sdfr	if (i > 1)
744130407Sdfr		printf("%d queued\n", i);
745130407Sdfr#endif
746130407Sdfr	if (i > 0) {
747130407Sdfr#if 1
748130407Sdfr		xferq->start(fc);
749130407Sdfr#else
750130407Sdfr		taskqueue_enqueue(taskqueue_swi_giant, &fwip->start_send);
751130407Sdfr#endif
752130407Sdfr	}
753130407Sdfr}
754130407Sdfr
755130407Sdfrstatic void
756130407Sdfrfwip_start_send (void *arg, int count)
757130407Sdfr{
758130407Sdfr	struct fwip_softc *fwip = arg;
759130407Sdfr
760130407Sdfr	GIANT_REQUIRED;
761130407Sdfr	fwip->fd.fc->atq->start(fwip->fd.fc);
762130407Sdfr}
763130407Sdfr
764130407Sdfr/* Async. stream output */
765130407Sdfrstatic void
766130407Sdfrfwip_stream_input(struct fw_xferq *xferq)
767130407Sdfr{
768130407Sdfr	struct mbuf *m, *m0;
769130407Sdfr	struct m_tag *mtag;
770130407Sdfr	struct ifnet *ifp;
771130407Sdfr	struct fwip_softc *fwip;
772130407Sdfr	struct fw_bulkxfer *sxfer;
773130407Sdfr	struct fw_pkt *fp;
774130407Sdfr	uint16_t src;
775130407Sdfr	uint32_t *p;
776130407Sdfr
777130407Sdfr	GIANT_REQUIRED;
778130407Sdfr
779130407Sdfr	fwip = (struct fwip_softc *)xferq->sc;
780147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
781150789Sglebius
782130407Sdfr	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
783130407Sdfr		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
784130407Sdfr		fp = mtod(sxfer->mbuf, struct fw_pkt *);
785130407Sdfr		if (fwip->fd.fc->irx_post != NULL)
786130407Sdfr			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
787130407Sdfr		m = sxfer->mbuf;
788130407Sdfr
789130407Sdfr		/* insert new rbuf */
790130407Sdfr		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
791130407Sdfr		if (m0 != NULL) {
792130407Sdfr			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
793130407Sdfr			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
794130407Sdfr		} else
795130407Sdfr			printf("fwip_as_input: m_getcl failed\n");
796130407Sdfr
797130407Sdfr		/*
798130407Sdfr		 * We must have a GASP header - leave the
799130407Sdfr		 * encapsulation sanity checks to the generic
800130407Sdfr		 * code. Remeber that we also have the firewire async
801130407Sdfr		 * stream header even though that isn't accounted for
802130407Sdfr		 * in mode.stream.len.
803130407Sdfr		 */
804130407Sdfr		if (sxfer->resp != 0 || fp->mode.stream.len <
805130407Sdfr		    2*sizeof(uint32_t)) {
806130407Sdfr			m_freem(m);
807130407Sdfr			ifp->if_ierrors ++;
808130407Sdfr			continue;
809130407Sdfr		}
810130407Sdfr		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
811130407Sdfr			+ sizeof(fp->mode.stream);
812130407Sdfr
813130407Sdfr		/*
814130407Sdfr		 * If we received the packet on the broadcast channel,
815130407Sdfr		 * mark it as broadcast, otherwise we assume it must
816130407Sdfr		 * be multicast.
817130407Sdfr		 */
818130407Sdfr		if (fp->mode.stream.chtag == broadcast_channel)
819130407Sdfr			m->m_flags |= M_BCAST;
820130407Sdfr		else
821130407Sdfr			m->m_flags |= M_MCAST;
822130407Sdfr
823130407Sdfr		/*
824130407Sdfr		 * Make sure we recognise the GASP specifier and
825130407Sdfr		 * version.
826130407Sdfr		 */
827130407Sdfr		p = mtod(m, uint32_t *);
828130407Sdfr		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
829130407Sdfr		    || (ntohl(p[2]) & 0xffffff) != 1) {
830130407Sdfr			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
831130407Sdfr			    ntohl(p[1]), ntohl(p[2]));
832130407Sdfr			m_freem(m);
833130407Sdfr			ifp->if_ierrors ++;
834130407Sdfr			continue;
835130407Sdfr		}
836130407Sdfr
837130407Sdfr		/*
838130407Sdfr		 * Record the sender ID for possible BPF usage.
839130407Sdfr		 */
840130407Sdfr		src = ntohl(p[1]) >> 16;
841130407Sdfr		if (ifp->if_bpf) {
842130407Sdfr			mtag = m_tag_alloc(MTAG_FIREWIRE,
843130407Sdfr			    MTAG_FIREWIRE_SENDER_EUID,
844130407Sdfr			    2*sizeof(uint32_t), M_NOWAIT);
845130407Sdfr			if (mtag) {
846130407Sdfr				/* bpf wants it in network byte order */
847130407Sdfr				struct fw_device *fd;
848130407Sdfr				uint32_t *p = (uint32_t *) (mtag + 1);
849130407Sdfr				fd = fw_noderesolve_nodeid(fwip->fd.fc,
850130407Sdfr				    src & 0x3f);
851130407Sdfr				if (fd) {
852130407Sdfr					p[0] = htonl(fd->eui.hi);
853130407Sdfr					p[1] = htonl(fd->eui.lo);
854130407Sdfr				} else {
855130407Sdfr					p[0] = 0;
856130407Sdfr					p[1] = 0;
857130407Sdfr				}
858130407Sdfr				m_tag_prepend(m, mtag);
859130407Sdfr			}
860130407Sdfr		}
861130407Sdfr
862130407Sdfr		/*
863130407Sdfr		 * Trim off the GASP header
864130407Sdfr		 */
865130407Sdfr		m_adj(m, 3*sizeof(uint32_t));
866130407Sdfr		m->m_pkthdr.rcvif = ifp;
867130407Sdfr		firewire_input(ifp, m, src);
868130407Sdfr		ifp->if_ipackets ++;
869130407Sdfr	}
870130407Sdfr	if (STAILQ_FIRST(&xferq->stfree) != NULL)
871130407Sdfr		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
872130407Sdfr}
873130407Sdfr
874130407Sdfrstatic __inline void
875130407Sdfrfwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
876130407Sdfr{
877130407Sdfr	struct mbuf *m;
878130407Sdfr
879130407Sdfr	GIANT_REQUIRED;
880130407Sdfr
881130407Sdfr	/*
882130407Sdfr	 * We have finished with a unicast xfer. Allocate a new
883130407Sdfr	 * cluster and stick it on the back of the input queue.
884130407Sdfr	 */
885130407Sdfr	m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
886130407Sdfr	xfer->mbuf = m;
887130407Sdfr	xfer->recv.payload = mtod(m, uint32_t *);
888130407Sdfr	xfer->recv.pay_len = MCLBYTES;
889130407Sdfr	xfer->mbuf = m;
890130407Sdfr	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
891130407Sdfr}
892130407Sdfr
893130407Sdfrstatic void
894130407Sdfrfwip_unicast_input(struct fw_xfer *xfer)
895130407Sdfr{
896130407Sdfr	uint64_t address;
897130407Sdfr	struct mbuf *m;
898130407Sdfr	struct m_tag *mtag;
899130407Sdfr	struct ifnet *ifp;
900130407Sdfr	struct fwip_softc *fwip;
901130407Sdfr	struct fw_pkt *fp;
902130407Sdfr	//struct fw_pkt *sfp;
903130407Sdfr	int rtcode;
904130407Sdfr
905130407Sdfr	GIANT_REQUIRED;
906130407Sdfr
907130407Sdfr	fwip = (struct fwip_softc *)xfer->sc;
908147256Sbrooks	ifp = fwip->fw_softc.fwip_ifp;
909130407Sdfr	m = xfer->mbuf;
910130407Sdfr	xfer->mbuf = 0;
911130407Sdfr	fp = &xfer->recv.hdr;
912130407Sdfr
913130407Sdfr	/*
914130407Sdfr	 * Check the fifo address - we only accept addresses of
915130407Sdfr	 * exactly INET_FIFO.
916130407Sdfr	 */
917130407Sdfr	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
918130407Sdfr		| fp->mode.wreqb.dest_lo;
919130407Sdfr	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
920130407Sdfr		rtcode = FWRCODE_ER_TYPE;
921130407Sdfr	} else if (address != INET_FIFO) {
922130407Sdfr		rtcode = FWRCODE_ER_ADDR;
923130407Sdfr	} else {
924130407Sdfr		rtcode = FWRCODE_COMPLETE;
925130407Sdfr	}
926130407Sdfr
927130407Sdfr	/*
928130407Sdfr	 * Pick up a new mbuf and stick it on the back of the receive
929130407Sdfr	 * queue.
930130407Sdfr	 */
931130407Sdfr	fwip_unicast_input_recycle(fwip, xfer);
932130407Sdfr
933130407Sdfr	/*
934130407Sdfr	 * If we've already rejected the packet, give up now.
935130407Sdfr	 */
936130407Sdfr	if (rtcode != FWRCODE_COMPLETE) {
937130407Sdfr		m_freem(m);
938130407Sdfr		ifp->if_ierrors ++;
939130407Sdfr		return;
940130407Sdfr	}
941130407Sdfr
942130407Sdfr	if (ifp->if_bpf) {
943130407Sdfr		/*
944130407Sdfr		 * Record the sender ID for possible BPF usage.
945130407Sdfr		 */
946130407Sdfr		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
947130407Sdfr		    2*sizeof(uint32_t), M_NOWAIT);
948130407Sdfr		if (mtag) {
949130407Sdfr			/* bpf wants it in network byte order */
950130407Sdfr			struct fw_device *fd;
951130407Sdfr			uint32_t *p = (uint32_t *) (mtag + 1);
952130407Sdfr			fd = fw_noderesolve_nodeid(fwip->fd.fc,
953130407Sdfr			    fp->mode.wreqb.src & 0x3f);
954130407Sdfr			if (fd) {
955130407Sdfr				p[0] = htonl(fd->eui.hi);
956130407Sdfr				p[1] = htonl(fd->eui.lo);
957130407Sdfr			} else {
958130407Sdfr				p[0] = 0;
959130407Sdfr				p[1] = 0;
960130407Sdfr			}
961130407Sdfr			m_tag_prepend(m, mtag);
962130407Sdfr		}
963130407Sdfr	}
964130407Sdfr
965130407Sdfr	/*
966130407Sdfr	 * Hand off to the generic encapsulation code. We don't use
967130407Sdfr	 * ifp->if_input so that we can pass the source nodeid as an
968130407Sdfr	 * argument to facilitate link-level fragment reassembly.
969130407Sdfr	 */
970130407Sdfr	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
971130407Sdfr	m->m_pkthdr.rcvif = ifp;
972130407Sdfr	firewire_input(ifp, m, fp->mode.wreqb.src);
973130407Sdfr	ifp->if_ipackets ++;
974130407Sdfr}
975130407Sdfr
976130407Sdfrstatic devclass_t fwip_devclass;
977130407Sdfr
978130407Sdfrstatic device_method_t fwip_methods[] = {
979130407Sdfr	/* device interface */
980130407Sdfr	DEVMETHOD(device_identify,	fwip_identify),
981130407Sdfr	DEVMETHOD(device_probe,		fwip_probe),
982130407Sdfr	DEVMETHOD(device_attach,	fwip_attach),
983130407Sdfr	DEVMETHOD(device_detach,	fwip_detach),
984130407Sdfr	{ 0, 0 }
985130407Sdfr};
986130407Sdfr
987130407Sdfrstatic driver_t fwip_driver = {
988130407Sdfr        "fwip",
989130407Sdfr	fwip_methods,
990130407Sdfr	sizeof(struct fwip_softc),
991130407Sdfr};
992130407Sdfr
993130407Sdfr
994130407Sdfr#ifdef __DragonFly__
995130407SdfrDECLARE_DUMMY_MODULE(fwip);
996130407Sdfr#endif
997130407SdfrDRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
998130407SdfrMODULE_VERSION(fwip, 1);
999130407SdfrMODULE_DEPEND(fwip, firewire, 1, 1, 1);
1000