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