if_fwe.c revision 120660
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2002-2003
3103285Sikob * 	Hidetoshi Shimokawa. All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the following acknowledgement:
15103285Sikob *
16103285Sikob *	This product includes software developed by Hidetoshi Shimokawa.
17103285Sikob *
18103285Sikob * 4. Neither the name of the author nor the names of its contributors
19103285Sikob *    may be used to endorse or promote products derived from this software
20103285Sikob *    without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23103285Sikob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24103285Sikob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25103285Sikob * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26103285Sikob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27103285Sikob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28103285Sikob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30103285Sikob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31103285Sikob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32103285Sikob * SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/if_fwe.c 120660 2003-10-02 04:06:56Z simokawa $
35103285Sikob */
36103285Sikob
37103285Sikob#include "opt_inet.h"
38103285Sikob
39103285Sikob#include <sys/param.h>
40103285Sikob#include <sys/kernel.h>
41103285Sikob#include <sys/malloc.h>
42103285Sikob#include <sys/mbuf.h>
43103285Sikob#include <sys/socket.h>
44103285Sikob#include <sys/sockio.h>
45103285Sikob#include <sys/sysctl.h>
46103285Sikob#include <sys/systm.h>
47103285Sikob#include <sys/module.h>
48103285Sikob#include <sys/bus.h>
49113584Ssimokawa#include <machine/bus.h>
50103285Sikob
51103285Sikob#include <net/bpf.h>
52103285Sikob#include <net/ethernet.h>
53103285Sikob#include <net/if.h>
54103285Sikob#include <net/if_arp.h>
55103285Sikob#include <net/if_vlan_var.h>
56103285Sikob
57103285Sikob#include <dev/firewire/firewire.h>
58103285Sikob#include <dev/firewire/firewirereg.h>
59103285Sikob#include <dev/firewire/if_fwevar.h>
60103285Sikob
61103285Sikob#define FWEDEBUG	if (fwedebug) printf
62111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
63111942Ssimokawa#define RX_MAX_QUEUE	FWMAXQUEUE
64103285Sikob
65103285Sikob/* network interface */
66103285Sikobstatic void fwe_start __P((struct ifnet *));
67103285Sikobstatic int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
68103285Sikobstatic void fwe_init __P((void *));
69103285Sikob
70111942Ssimokawastatic void fwe_output_callback __P((struct fw_xfer *));
71103285Sikobstatic void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
72103285Sikobstatic void fwe_as_input __P((struct fw_xferq *));
73103285Sikob
74103285Sikobstatic int fwedebug = 0;
75103285Sikobstatic int stream_ch = 1;
76116139Ssimokawastatic int tx_speed = 2;
77103285Sikob
78108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
79103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
80103285SikobSYSCTL_DECL(_hw_firewire);
81103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
82103285Sikob	"Ethernet Emulation Subsystem");
83103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
84103285Sikob	"Stream channel to use");
85116139SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
86116139Ssimokawa	"Transmission Speed");
87103285Sikob
88103285Sikob#ifdef DEVICE_POLLING
89103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)			\
90103285Sikob	if (ether_poll_register(func, ifp)) {			\
91103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
92103285Sikob		fc->set_intr(fc, 0);				\
93103285Sikob	}
94103285Sikob
95103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)				\
96103285Sikob	do {							\
97103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
98103285Sikob		ether_poll_deregister(ifp);			\
99103285Sikob		fc->set_intr(fc, 1);				\
100103285Sikob	} while(0)						\
101103285Sikob
102103285Sikobstatic poll_handler_t fwe_poll;
103103285Sikob
104103285Sikobstatic void
105103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
106103285Sikob{
107103285Sikob	struct fwe_softc *fwe;
108103285Sikob	struct firewire_comm *fc;
109103285Sikob
110103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
111103285Sikob	fc = fwe->fd.fc;
112103285Sikob	if (cmd == POLL_DEREGISTER) {
113103285Sikob		/* enable interrupts */
114103285Sikob		fc->set_intr(fc, 1);
115103285Sikob		return;
116103285Sikob	}
117103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
118103285Sikob}
119103285Sikob#else
120103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)
121103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)
122103285Sikob#endif
123103285Sikobstatic void
124103285Sikobfwe_identify(driver_t *driver, device_t parent)
125103285Sikob{
126103285Sikob	BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
127103285Sikob}
128103285Sikob
129103285Sikobstatic int
130103285Sikobfwe_probe(device_t dev)
131103285Sikob{
132103285Sikob	device_t pa;
133103285Sikob
134103285Sikob	pa = device_get_parent(dev);
135103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
136103285Sikob		return(ENXIO);
137103285Sikob	}
138103285Sikob
139108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
140103285Sikob	return (0);
141103285Sikob}
142103285Sikob
143103285Sikobstatic int
144103285Sikobfwe_attach(device_t dev)
145103285Sikob{
146103285Sikob	struct fwe_softc *fwe;
147103285Sikob	struct ifnet *ifp;
148103285Sikob	int unit, s;
149103285Sikob	u_char *eaddr;
150109814Ssimokawa	struct fw_eui64 *eui;
151103285Sikob
152103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
153103285Sikob	unit = device_get_unit(dev);
154103285Sikob
155103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
156103285Sikob	/* XXX */
157103285Sikob	fwe->stream_ch = stream_ch;
158103285Sikob	fwe->dma_ch = -1;
159103285Sikob
160103285Sikob	fwe->fd.fc = device_get_ivars(dev);
161103285Sikob	fwe->fd.dev = dev;
162103285Sikob	fwe->fd.post_explore = NULL;
163103285Sikob	fwe->eth_softc.fwe = fwe;
164103285Sikob
165103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
166103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
167103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
168103285Sikob
169103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
170103285Sikob#define LOCAL (0x02)
171103285Sikob#define GROUP (0x01)
172103285Sikob	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
173109814Ssimokawa
174109814Ssimokawa	eui = &fwe->fd.fc->eui;
175109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
176109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
177109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
178109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
179109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
180109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
181107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
182107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
183103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
184103285Sikob
185103285Sikob	/* fill the rest and attach interface */
186103285Sikob	ifp = &fwe->fwe_if;
187103285Sikob	ifp->if_softc = &fwe->eth_softc;
188103285Sikob
189103285Sikob	ifp->if_unit = unit;
190103285Sikob	ifp->if_name = "fwe";
191103285Sikob	ifp->if_init = fwe_init;
192103285Sikob	ifp->if_output = ether_output;
193103285Sikob	ifp->if_start = fwe_start;
194103285Sikob	ifp->if_ioctl = fwe_ioctl;
195103285Sikob	ifp->if_mtu = ETHERMTU;
196103285Sikob	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
197111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
198103285Sikob
199103285Sikob	s = splimp();
200108712Ssimokawa#if __FreeBSD_version >= 500000
201106937Ssam	ether_ifattach(ifp, eaddr);
202108712Ssimokawa#else
203108712Ssimokawa	ether_ifattach(ifp, 1);
204108712Ssimokawa#endif
205103285Sikob	splx(s);
206103285Sikob
207103285Sikob        /* Tell the upper layer(s) we support long frames. */
208103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
209108712Ssimokawa#if __FreeBSD_version >= 500000
210106937Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
211108712Ssimokawa#endif
212103285Sikob
213103285Sikob
214103285Sikob	FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
215103285Sikob	return 0;
216103285Sikob}
217103285Sikob
218103285Sikobstatic void
219103285Sikobfwe_stop(struct fwe_softc *fwe)
220103285Sikob{
221103285Sikob	struct firewire_comm *fc;
222103285Sikob	struct fw_xferq *xferq;
223103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
224111942Ssimokawa	struct fw_xfer *xfer, *next;
225111942Ssimokawa	int i;
226103285Sikob
227103285Sikob	fc = fwe->fd.fc;
228103285Sikob
229103285Sikob	FWE_POLL_DEREGISTER(fwe, ifp);
230103285Sikob
231103285Sikob	if (fwe->dma_ch >= 0) {
232103285Sikob		xferq = fc->ir[fwe->dma_ch];
233103285Sikob
234103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
235103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
236103285Sikob		xferq->flag &=
237113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
238113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
239111942Ssimokawa		xferq->hand =  NULL;
240111942Ssimokawa
241111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
242111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
243111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
244111942Ssimokawa
245111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
246111942Ssimokawa					xfer = next) {
247111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
248111942Ssimokawa			fw_xfer_free(xfer);
249111942Ssimokawa		}
250111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
251111942Ssimokawa
252111942Ssimokawa		xferq->bulkxfer =  NULL;
253103285Sikob		fwe->dma_ch = -1;
254103285Sikob	}
255103285Sikob
256103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
257103285Sikob}
258103285Sikob
259103285Sikobstatic int
260103285Sikobfwe_detach(device_t dev)
261103285Sikob{
262103285Sikob	struct fwe_softc *fwe;
263103285Sikob	int s;
264103285Sikob
265103285Sikob	fwe = (struct fwe_softc *)device_get_softc(dev);
266103285Sikob	s = splimp();
267103285Sikob
268103285Sikob	fwe_stop(fwe);
269108712Ssimokawa#if __FreeBSD_version >= 500000
270106937Ssam	ether_ifdetach(&fwe->fwe_if);
271108712Ssimokawa#else
272108712Ssimokawa	ether_ifdetach(&fwe->fwe_if, 1);
273108712Ssimokawa#endif
274103285Sikob
275103285Sikob	splx(s);
276103285Sikob	return 0;
277103285Sikob}
278103285Sikob
279103285Sikobstatic void
280103285Sikobfwe_init(void *arg)
281103285Sikob{
282103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
283103285Sikob	struct firewire_comm *fc;
284103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
285103285Sikob	struct fw_xferq *xferq;
286111942Ssimokawa	struct fw_xfer *xfer;
287113584Ssimokawa	struct mbuf *m;
288103285Sikob	int i;
289103285Sikob
290103285Sikob	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
291103285Sikob
292103285Sikob	/* XXX keep promiscoud mode */
293103285Sikob	ifp->if_flags |= IFF_PROMISC;
294103285Sikob
295103285Sikob	fc = fwe->fd.fc;
296103285Sikob#define START 0
297103285Sikob	if (fwe->dma_ch < 0) {
298103285Sikob		for (i = START; i < fc->nisodma; i ++) {
299103285Sikob			xferq = fc->ir[i];
300103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
301118312Ssimokawa				goto found;
302103285Sikob		}
303118312Ssimokawa		printf("no free dma channel\n");
304118312Ssimokawa		return;
305118312Ssimokawafound:
306103285Sikob		fwe->dma_ch = i;
307103285Sikob		fwe->stream_ch = stream_ch;
308103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
309103285Sikob		/* allocate DMA channel and init packet mode */
310113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
311113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
312112400Ssimokawa		xferq->flag &= ~0xff;
313103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
314103285Sikob		/* register fwe_input handler */
315103285Sikob		xferq->sc = (caddr_t) fwe;
316103285Sikob		xferq->hand = fwe_as_input;
317111942Ssimokawa		xferq->bnchunk = RX_MAX_QUEUE;
318111942Ssimokawa		xferq->bnpacket = 1;
319111942Ssimokawa		xferq->psize = MCLBYTES;
320111942Ssimokawa		xferq->queued = 0;
321113584Ssimokawa		xferq->buf = NULL;
322111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
323113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
324113584Ssimokawa							M_FWE, M_WAITOK);
325111942Ssimokawa		if (xferq->bulkxfer == NULL) {
326111942Ssimokawa			printf("if_fwe: malloc failed\n");
327111942Ssimokawa			return;
328111942Ssimokawa		}
329111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
330111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
331111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
332111942Ssimokawa		xferq->stproc = NULL;
333111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
334113584Ssimokawa			m =
335111942Ssimokawa#if __FreeBSD_version >= 500000
336111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
337111942Ssimokawa#else
338111942Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
339111942Ssimokawa#endif
340113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
341113584Ssimokawa			if (m != NULL) {
342113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
343113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
344113584Ssimokawa						&xferq->bulkxfer[i], link);
345113584Ssimokawa			} else
346113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
347111942Ssimokawa		}
348111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
349111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
350111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
351111942Ssimokawa			if (xfer == NULL)
352111942Ssimokawa				break;
353120660Ssimokawa			xfer->send.spd = tx_speed;
354111942Ssimokawa			xfer->fc = fwe->fd.fc;
355111942Ssimokawa			xfer->retry_req = fw_asybusy;
356111942Ssimokawa			xfer->sc = (caddr_t)fwe;
357111942Ssimokawa			xfer->act.hand = fwe_output_callback;
358111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
359111942Ssimokawa		}
360103285Sikob	} else
361103285Sikob		xferq = fc->ir[fwe->dma_ch];
362103285Sikob
363103285Sikob
364103285Sikob	/* start dma */
365103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
366103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
367103285Sikob
368103285Sikob	ifp->if_flags |= IFF_RUNNING;
369103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
370103285Sikob
371103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
372103285Sikob#if 0
373103285Sikob	/* attempt to start output */
374103285Sikob	fwe_start(ifp);
375103285Sikob#endif
376103285Sikob}
377103285Sikob
378103285Sikob
379103285Sikobstatic int
380103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
381103285Sikob{
382103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
383103285Sikob	struct ifstat *ifs = NULL;
384103285Sikob	int s, error, len;
385103285Sikob
386103285Sikob	switch (cmd) {
387103285Sikob		case SIOCSIFFLAGS:
388103285Sikob			s = splimp();
389103285Sikob			if (ifp->if_flags & IFF_UP) {
390103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
391103285Sikob					fwe_init(&fwe->eth_softc);
392103285Sikob			} else {
393103285Sikob				if (ifp->if_flags & IFF_RUNNING)
394103285Sikob					fwe_stop(fwe);
395103285Sikob			}
396103285Sikob			/* XXX keep promiscoud mode */
397103285Sikob			ifp->if_flags |= IFF_PROMISC;
398103285Sikob			splx(s);
399103285Sikob			break;
400103285Sikob		case SIOCADDMULTI:
401103285Sikob		case SIOCDELMULTI:
402108712Ssimokawa			break;
403103285Sikob
404103285Sikob		case SIOCGIFSTATUS:
405103285Sikob			s = splimp();
406103285Sikob			ifs = (struct ifstat *)data;
407103285Sikob			len = strlen(ifs->ascii);
408103285Sikob			if (len < sizeof(ifs->ascii))
409103285Sikob				snprintf(ifs->ascii + len,
410103285Sikob					sizeof(ifs->ascii) - len,
411103285Sikob					"\tch %d dma %d\n",
412103285Sikob						fwe->stream_ch, fwe->dma_ch);
413103285Sikob			splx(s);
414108712Ssimokawa			break;
415108712Ssimokawa#if __FreeBSD_version >= 500000
416103285Sikob		default:
417108712Ssimokawa#else
418108712Ssimokawa		case SIOCSIFADDR:
419108712Ssimokawa		case SIOCGIFADDR:
420108712Ssimokawa		case SIOCSIFMTU:
421108712Ssimokawa#endif
422106937Ssam			s = splimp();
423106937Ssam			error = ether_ioctl(ifp, cmd, data);
424106937Ssam			splx(s);
425106937Ssam			return (error);
426108712Ssimokawa#if __FreeBSD_version < 500000
427108712Ssimokawa		default:
428108712Ssimokawa			return (EINVAL);
429108712Ssimokawa#endif
430103285Sikob	}
431103285Sikob
432103285Sikob	return (0);
433103285Sikob}
434103285Sikob
435103285Sikobstatic void
436111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
437111942Ssimokawa{
438111942Ssimokawa	struct fwe_softc *fwe;
439111942Ssimokawa	struct ifnet *ifp;
440111942Ssimokawa	int s;
441111942Ssimokawa
442111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
443111942Ssimokawa	ifp = &fwe->fwe_if;
444111942Ssimokawa	/* XXX error check */
445111942Ssimokawa	FWEDEBUG("resp = %d\n", xfer->resp);
446111942Ssimokawa	if (xfer->resp != 0)
447111942Ssimokawa		ifp->if_oerrors ++;
448111942Ssimokawa
449111942Ssimokawa	m_freem(xfer->mbuf);
450111942Ssimokawa	fw_xfer_unload(xfer);
451113584Ssimokawa
452111942Ssimokawa	s = splimp();
453111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
454111942Ssimokawa	splx(s);
455113584Ssimokawa
456113584Ssimokawa	/* for queue full */
457111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
458111942Ssimokawa		fwe_start(ifp);
459111942Ssimokawa}
460111942Ssimokawa
461111942Ssimokawastatic void
462103285Sikobfwe_start(struct ifnet *ifp)
463103285Sikob{
464103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
465103285Sikob	int s;
466103285Sikob
467103285Sikob	FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
468103285Sikob
469103285Sikob	if (fwe->dma_ch < 0) {
470103285Sikob		struct mbuf	*m = NULL;
471103285Sikob
472103285Sikob		FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
473103285Sikob
474103285Sikob		s = splimp();
475103285Sikob		do {
476103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
477103285Sikob			if (m != NULL)
478103285Sikob				m_freem(m);
479103285Sikob			ifp->if_oerrors ++;
480103285Sikob		} while (m != NULL);
481103285Sikob		splx(s);
482103285Sikob
483103285Sikob		return;
484103285Sikob	}
485103285Sikob
486103285Sikob	s = splimp();
487103285Sikob	ifp->if_flags |= IFF_OACTIVE;
488103285Sikob
489103285Sikob	if (ifp->if_snd.ifq_len != 0)
490103285Sikob		fwe_as_output(fwe, ifp);
491103285Sikob
492103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
493103285Sikob	splx(s);
494103285Sikob}
495103285Sikob
496111942Ssimokawa#define HDR_LEN 4
497111942Ssimokawa#ifndef ETHER_ALIGN
498111942Ssimokawa#define ETHER_ALIGN 2
499103285Sikob#endif
500103285Sikob/* Async. stream output */
501103285Sikobstatic void
502103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
503103285Sikob{
504103285Sikob	struct mbuf *m;
505103285Sikob	struct fw_xfer *xfer;
506103285Sikob	struct fw_xferq *xferq;
507103285Sikob	struct fw_pkt *fp;
508103285Sikob	int i = 0;
509103285Sikob
510103285Sikob	xfer = NULL;
511103285Sikob	xferq = fwe->fd.fc->atq;
512111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
513111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
514111942Ssimokawa		if (xfer == NULL) {
515111942Ssimokawa			printf("if_fwe: lack of xfer\n");
516111942Ssimokawa			return;
517111942Ssimokawa		}
518103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
519103285Sikob		if (m == NULL)
520103285Sikob			break;
521111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
522108712Ssimokawa#if __FreeBSD_version >= 500000
523106937Ssam		BPF_MTAP(ifp, m);
524108712Ssimokawa#else
525108712Ssimokawa		if (ifp->if_bpf != NULL)
526108712Ssimokawa			bpf_mtap(ifp, m);
527108712Ssimokawa#endif
528103285Sikob
529103285Sikob		/* keep ip packet alignment for alpha */
530111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
531120660Ssimokawa		fp = &xfer->send.hdr;
532120660Ssimokawa		*(u_int32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
533113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
534103285Sikob		xfer->mbuf = m;
535120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
536103285Sikob
537111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
538103285Sikob			/* error */
539103285Sikob			ifp->if_oerrors ++;
540103285Sikob			/* XXX set error code */
541103285Sikob			fwe_output_callback(xfer);
542103285Sikob		} else {
543103285Sikob			ifp->if_opackets ++;
544111942Ssimokawa			i++;
545103285Sikob		}
546103285Sikob	}
547103285Sikob#if 0
548103285Sikob	if (i > 1)
549103285Sikob		printf("%d queued\n", i);
550103285Sikob#endif
551111942Ssimokawa	if (i > 0)
552111942Ssimokawa		xferq->start(fwe->fd.fc);
553103285Sikob}
554103285Sikob
555103285Sikob/* Async. stream output */
556103285Sikobstatic void
557103285Sikobfwe_as_input(struct fw_xferq *xferq)
558103285Sikob{
559113584Ssimokawa	struct mbuf *m, *m0;
560103285Sikob	struct ifnet *ifp;
561103285Sikob	struct fwe_softc *fwe;
562111942Ssimokawa	struct fw_bulkxfer *sxfer;
563111942Ssimokawa	struct fw_pkt *fp;
564103285Sikob	u_char *c;
565111942Ssimokawa#if __FreeBSD_version < 500000
566111942Ssimokawa	struct ether_header *eh;
567111942Ssimokawa#endif
568103285Sikob
569103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
570103285Sikob	ifp = &fwe->fwe_if;
571103285Sikob#if 0
572103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
573103285Sikob#endif
574111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
575111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
576113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
577111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
578111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
579111942Ssimokawa		m = sxfer->mbuf;
580111942Ssimokawa
581119119Ssimokawa		/* insert new rbuf */
582113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
583113584Ssimokawa		if (m0 != NULL) {
584113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
585113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
586113584Ssimokawa		} else
587113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
588111942Ssimokawa
589119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
590119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
591119119Ssimokawa			m_freem(m);
592119119Ssimokawa			ifp->if_ierrors ++;
593119119Ssimokawa			continue;
594119119Ssimokawa		}
595119119Ssimokawa
596111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
597111942Ssimokawa		c = mtod(m, char *);
598111942Ssimokawa#if __FreeBSD_version < 500000
599111942Ssimokawa		eh = (struct ether_header *)c;
600111942Ssimokawa		m->m_data += sizeof(struct ether_header);
601108712Ssimokawa#endif
602111942Ssimokawa		m->m_len = m->m_pkthdr.len =
603113584Ssimokawa				fp->mode.stream.len - ETHER_ALIGN;
604103285Sikob		m->m_pkthdr.rcvif = ifp;
605103285Sikob#if 0
606103285Sikob		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
607103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
608103285Sikob			 "%02x %02x %02x %02x\n"
609103285Sikob			 "%02x %02x %02x %02x\n"
610103285Sikob			 "%02x %02x %02x %02x\n"
611103285Sikob			 "%02x %02x %02x %02x\n",
612103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
613103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
614103285Sikob			 c[12], c[13], c[14], c[15],
615103285Sikob			 c[16], c[17], c[18], c[19],
616103285Sikob			 c[20], c[21], c[22], c[23],
617103285Sikob			 c[20], c[21], c[22], c[23]
618103285Sikob		 );
619103285Sikob#endif
620108712Ssimokawa#if __FreeBSD_version >= 500000
621106937Ssam		(*ifp->if_input)(ifp, m);
622108712Ssimokawa#else
623108712Ssimokawa		ether_input(ifp, eh, m);
624108712Ssimokawa#endif
625103285Sikob		ifp->if_ipackets ++;
626103285Sikob	}
627111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
628111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
629103285Sikob}
630103285Sikob
631103285Sikob
632103285Sikobstatic devclass_t fwe_devclass;
633103285Sikob
634103285Sikobstatic device_method_t fwe_methods[] = {
635103285Sikob	/* device interface */
636103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
637103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
638103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
639103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
640103285Sikob	{ 0, 0 }
641103285Sikob};
642103285Sikob
643103285Sikobstatic driver_t fwe_driver = {
644103285Sikob        "if_fwe",
645103285Sikob	fwe_methods,
646103285Sikob	sizeof(struct fwe_softc),
647103285Sikob};
648103285Sikob
649103285Sikob
650113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
651113506SmdoddMODULE_VERSION(fwe, 1);
652113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
653