if_fwe.c revision 111942
1103285Sikob/*
2103285Sikob * Copyright (C) 2002
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 111942 2003-03-06 05:06:44Z simokawa $
35103285Sikob */
36103285Sikob
37103285Sikob#include "opt_inet.h"
38103285Sikob
39103285Sikob#include <sys/param.h>
40103285Sikob#include <sys/conf.h>
41103285Sikob#include <sys/kernel.h>
42103285Sikob#include <sys/malloc.h>
43103285Sikob#include <sys/mbuf.h>
44103285Sikob#include <sys/socket.h>
45103285Sikob#include <sys/sockio.h>
46103285Sikob#include <sys/sysctl.h>
47103285Sikob#include <sys/systm.h>
48103285Sikob#include <sys/module.h>
49103285Sikob#include <sys/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#include <net/route.h>
57103285Sikob
58103285Sikob#include <netinet/in.h>
59103285Sikob
60103285Sikob#include <dev/firewire/firewire.h>
61103285Sikob#include <dev/firewire/firewirereg.h>
62103285Sikob#include <dev/firewire/if_fwevar.h>
63103285Sikob
64103285Sikob#define FWEDEBUG	if (fwedebug) printf
65111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
66111942Ssimokawa#define RX_MAX_QUEUE	FWMAXQUEUE
67103285Sikob
68103285Sikob/* network interface */
69103285Sikobstatic void fwe_start __P((struct ifnet *));
70103285Sikobstatic int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
71103285Sikobstatic void fwe_init __P((void *));
72103285Sikob
73111942Ssimokawastatic void fwe_output_callback __P((struct fw_xfer *));
74103285Sikobstatic void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
75103285Sikobstatic void fwe_as_input __P((struct fw_xferq *));
76103285Sikob
77103285Sikobstatic int fwedebug = 0;
78103285Sikobstatic int stream_ch = 1;
79103285Sikob
80108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
81103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
82103285SikobSYSCTL_DECL(_hw_firewire);
83103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
84103285Sikob	"Ethernet Emulation Subsystem");
85103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
86103285Sikob	"Stream channel to use");
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 &=
237111942Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN |
238111942Ssimokawa				FWXFERQ_EXTBUF | FWXFERQ_HANDLER);
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;
287103285Sikob	int i;
288103285Sikob
289103285Sikob	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
290103285Sikob
291103285Sikob	/* XXX keep promiscoud mode */
292103285Sikob	ifp->if_flags |= IFF_PROMISC;
293103285Sikob
294103285Sikob	fc = fwe->fd.fc;
295103285Sikob#define START 0
296103285Sikob	if (fwe->dma_ch < 0) {
297103285Sikob		xferq = NULL;
298103285Sikob		for (i = START; i < fc->nisodma; i ++) {
299103285Sikob			xferq = fc->ir[i];
300103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
301103285Sikob				break;
302103285Sikob		}
303103285Sikob
304103285Sikob		if (xferq == NULL) {
305103285Sikob			printf("no free dma channel\n");
306103285Sikob			return;
307103285Sikob		}
308103285Sikob		fwe->dma_ch = i;
309103285Sikob		fwe->stream_ch = stream_ch;
310103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
311103285Sikob		/* allocate DMA channel and init packet mode */
312111942Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF;
313103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
314103285Sikob		/* register fwe_input handler */
315103285Sikob		xferq->sc = (caddr_t) fwe;
316103285Sikob		xferq->hand = fwe_as_input;
317103285Sikob		xferq->flag |= FWXFERQ_HANDLER;
318111942Ssimokawa		xferq->bnchunk = RX_MAX_QUEUE;
319111942Ssimokawa		xferq->bnpacket = 1;
320111942Ssimokawa		xferq->psize = MCLBYTES;
321111942Ssimokawa		xferq->queued = 0;
322111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
323111942Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk, M_FWE, 0);
324111942Ssimokawa		if (xferq->bulkxfer == NULL) {
325111942Ssimokawa			printf("if_fwe: malloc failed\n");
326111942Ssimokawa			return;
327111942Ssimokawa		}
328111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
329111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
330111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
331111942Ssimokawa		xferq->stproc = NULL;
332111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
333111942Ssimokawa			xferq->bulkxfer[i].mbuf =
334111942Ssimokawa#if __FreeBSD_version >= 500000
335111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
336111942Ssimokawa#else
337111942Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
338111942Ssimokawa#endif
339111942Ssimokawa			xferq->bulkxfer[i].buf =
340111942Ssimokawa				mtod(xferq->bulkxfer[i].mbuf, char *);
341111942Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree,
342111942Ssimokawa				&xferq->bulkxfer[i], link);
343111942Ssimokawa		}
344111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
345111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
346111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
347111942Ssimokawa			if (xfer == NULL)
348111942Ssimokawa				break;
349111942Ssimokawa			xfer->send.off = 0;
350111942Ssimokawa			xfer->spd = 2;
351111942Ssimokawa			xfer->fc = fwe->fd.fc;
352111942Ssimokawa			xfer->retry_req = fw_asybusy;
353111942Ssimokawa			xfer->sc = (caddr_t)fwe;
354111942Ssimokawa			xfer->act.hand = fwe_output_callback;
355111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
356111942Ssimokawa		}
357103285Sikob	} else
358103285Sikob		xferq = fc->ir[fwe->dma_ch];
359103285Sikob
360103285Sikob
361103285Sikob	/* start dma */
362103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
363103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
364103285Sikob
365103285Sikob	ifp->if_flags |= IFF_RUNNING;
366103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
367103285Sikob
368103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
369103285Sikob#if 0
370103285Sikob	/* attempt to start output */
371103285Sikob	fwe_start(ifp);
372103285Sikob#endif
373103285Sikob}
374103285Sikob
375103285Sikob
376103285Sikobstatic int
377103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
378103285Sikob{
379103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
380103285Sikob	struct ifstat *ifs = NULL;
381103285Sikob	int s, error, len;
382103285Sikob
383103285Sikob	switch (cmd) {
384103285Sikob		case SIOCSIFFLAGS:
385103285Sikob			s = splimp();
386103285Sikob			if (ifp->if_flags & IFF_UP) {
387103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
388103285Sikob					fwe_init(&fwe->eth_softc);
389103285Sikob			} else {
390103285Sikob				if (ifp->if_flags & IFF_RUNNING)
391103285Sikob					fwe_stop(fwe);
392103285Sikob			}
393103285Sikob			/* XXX keep promiscoud mode */
394103285Sikob			ifp->if_flags |= IFF_PROMISC;
395103285Sikob			splx(s);
396103285Sikob			break;
397103285Sikob		case SIOCADDMULTI:
398103285Sikob		case SIOCDELMULTI:
399108712Ssimokawa			break;
400103285Sikob
401103285Sikob		case SIOCGIFSTATUS:
402103285Sikob			s = splimp();
403103285Sikob			ifs = (struct ifstat *)data;
404103285Sikob			len = strlen(ifs->ascii);
405103285Sikob			if (len < sizeof(ifs->ascii))
406103285Sikob				snprintf(ifs->ascii + len,
407103285Sikob					sizeof(ifs->ascii) - len,
408103285Sikob					"\tch %d dma %d\n",
409103285Sikob						fwe->stream_ch, fwe->dma_ch);
410103285Sikob			splx(s);
411108712Ssimokawa			break;
412108712Ssimokawa#if __FreeBSD_version >= 500000
413103285Sikob		default:
414108712Ssimokawa#else
415108712Ssimokawa		case SIOCSIFADDR:
416108712Ssimokawa		case SIOCGIFADDR:
417108712Ssimokawa		case SIOCSIFMTU:
418108712Ssimokawa#endif
419106937Ssam			s = splimp();
420106937Ssam			error = ether_ioctl(ifp, cmd, data);
421106937Ssam			splx(s);
422106937Ssam			return (error);
423108712Ssimokawa#if __FreeBSD_version < 500000
424108712Ssimokawa		default:
425108712Ssimokawa			return (EINVAL);
426108712Ssimokawa#endif
427103285Sikob	}
428103285Sikob
429103285Sikob	return (0);
430103285Sikob}
431103285Sikob
432103285Sikobstatic void
433111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
434111942Ssimokawa{
435111942Ssimokawa	struct fwe_softc *fwe;
436111942Ssimokawa	struct ifnet *ifp;
437111942Ssimokawa	int s;
438111942Ssimokawa
439111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
440111942Ssimokawa	ifp = &fwe->fwe_if;
441111942Ssimokawa	/* XXX error check */
442111942Ssimokawa	FWEDEBUG("resp = %d\n", xfer->resp);
443111942Ssimokawa	if (xfer->resp != 0)
444111942Ssimokawa		ifp->if_oerrors ++;
445111942Ssimokawa
446111942Ssimokawa	m_freem(xfer->mbuf);
447111942Ssimokawa	xfer->send.buf = NULL;
448111942Ssimokawa#if 0
449111942Ssimokawa	fw_xfer_unload(xfer);
450111942Ssimokawa#else
451111942Ssimokawa	xfer->state = FWXF_INIT;
452111942Ssimokawa	xfer->resp = 0;
453111942Ssimokawa	xfer->retry = 0;
454111942Ssimokawa#endif
455111942Ssimokawa	s = splimp();
456111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
457111942Ssimokawa	splx(s);
458111942Ssimokawa#if 1
459111942Ssimokawa	/* XXX for queue full */
460111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
461111942Ssimokawa		fwe_start(ifp);
462111942Ssimokawa#endif
463111942Ssimokawa}
464111942Ssimokawa
465111942Ssimokawastatic void
466103285Sikobfwe_start(struct ifnet *ifp)
467103285Sikob{
468103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
469103285Sikob	int s;
470103285Sikob
471103285Sikob#if 1
472103285Sikob	FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
473103285Sikob
474103285Sikob	if (fwe->dma_ch < 0) {
475103285Sikob		struct mbuf	*m = NULL;
476103285Sikob
477103285Sikob		FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
478103285Sikob
479103285Sikob		s = splimp();
480103285Sikob		do {
481103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
482103285Sikob			if (m != NULL)
483103285Sikob				m_freem(m);
484103285Sikob			ifp->if_oerrors ++;
485103285Sikob		} while (m != NULL);
486103285Sikob		splx(s);
487103285Sikob
488103285Sikob		return;
489103285Sikob	}
490103285Sikob
491103285Sikob#endif
492103285Sikob	s = splimp();
493103285Sikob	ifp->if_flags |= IFF_OACTIVE;
494103285Sikob
495103285Sikob	if (ifp->if_snd.ifq_len != 0)
496103285Sikob		fwe_as_output(fwe, ifp);
497103285Sikob
498103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
499103285Sikob	splx(s);
500103285Sikob}
501103285Sikob
502111942Ssimokawa#define HDR_LEN 4
503111942Ssimokawa#ifndef ETHER_ALIGN
504111942Ssimokawa#define ETHER_ALIGN 2
505103285Sikob#endif
506103285Sikob/* Async. stream output */
507103285Sikobstatic void
508103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
509103285Sikob{
510103285Sikob	struct mbuf *m;
511103285Sikob	struct fw_xfer *xfer;
512103285Sikob	struct fw_xferq *xferq;
513103285Sikob	struct fw_pkt *fp;
514103285Sikob	int i = 0;
515103285Sikob
516103285Sikob	xfer = NULL;
517103285Sikob	xferq = fwe->fd.fc->atq;
518111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
519111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
520111942Ssimokawa		if (xfer == NULL) {
521111942Ssimokawa			printf("if_fwe: lack of xfer\n");
522111942Ssimokawa			return;
523111942Ssimokawa		}
524103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
525103285Sikob		if (m == NULL)
526103285Sikob			break;
527111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
528108712Ssimokawa#if __FreeBSD_version >= 500000
529106937Ssam		BPF_MTAP(ifp, m);
530108712Ssimokawa#else
531108712Ssimokawa		if (ifp->if_bpf != NULL)
532108712Ssimokawa			bpf_mtap(ifp, m);
533108712Ssimokawa#endif
534103285Sikob
535103285Sikob		/* keep ip packet alignment for alpha */
536111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
537103285Sikob		fp = (struct fw_pkt *)&xfer->dst; /* XXX */
538103285Sikob		xfer->dst = *((int32_t *)&fwe->pkt_hdr);
539103285Sikob		fp->mode.stream.len = htons(m->m_pkthdr.len);
540103285Sikob		xfer->send.buf = (caddr_t) fp;
541103285Sikob		xfer->mbuf = m;
542103285Sikob		xfer->send.len = m->m_pkthdr.len + HDR_LEN;
543103285Sikob
544111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
545103285Sikob			/* error */
546103285Sikob			ifp->if_oerrors ++;
547103285Sikob			/* XXX set error code */
548103285Sikob			fwe_output_callback(xfer);
549103285Sikob		} else {
550103285Sikob			ifp->if_opackets ++;
551111942Ssimokawa			i++;
552103285Sikob		}
553103285Sikob	}
554103285Sikob#if 0
555103285Sikob	if (i > 1)
556103285Sikob		printf("%d queued\n", i);
557103285Sikob#endif
558111942Ssimokawa	if (i > 0)
559111942Ssimokawa		xferq->start(fwe->fd.fc);
560103285Sikob}
561103285Sikob
562111942Ssimokawa#if 0
563103285Sikob#if __FreeBSD_version >= 500000
564103285Sikobstatic void
565103285Sikobfwe_free(void *buf, void *args)
566103285Sikob{
567103285Sikob	FWEDEBUG("fwe_free:\n");
568110195Ssimokawa	free(buf, M_FW);
569103285Sikob}
570103285Sikob
571103285Sikob#else
572103285Sikobstatic void
573103285Sikobfwe_free(caddr_t buf, u_int size)
574103285Sikob{
575103285Sikob	int *p;
576103285Sikob	FWEDEBUG("fwe_free:\n");
577103285Sikob	p = (int *)buf;
578103285Sikob	(*p) --;
579103285Sikob	if (*p < 1)
580110195Ssimokawa		free(buf, M_FW);
581103285Sikob}
582103285Sikob
583103285Sikobstatic void
584103285Sikobfwe_ref(caddr_t buf, u_int size)
585103285Sikob{
586103285Sikob	int *p;
587103285Sikob
588103285Sikob	FWEDEBUG("fwe_ref: called\n");
589103285Sikob	p = (int *)buf;
590103285Sikob	(*p) ++;
591103285Sikob}
592103285Sikob#endif
593111942Ssimokawa#endif
594103285Sikob
595103285Sikob/* Async. stream output */
596103285Sikobstatic void
597103285Sikobfwe_as_input(struct fw_xferq *xferq)
598103285Sikob{
599103285Sikob	struct mbuf *m;
600103285Sikob	struct ifnet *ifp;
601103285Sikob	struct fwe_softc *fwe;
602111942Ssimokawa	struct fw_bulkxfer *sxfer;
603111942Ssimokawa	struct fw_pkt *fp;
604103285Sikob	u_char *c;
605111942Ssimokawa#if __FreeBSD_version < 500000
606111942Ssimokawa	struct ether_header *eh;
607111942Ssimokawa#endif
608103285Sikob
609103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
610103285Sikob	ifp = &fwe->fwe_if;
611103285Sikob#if 0
612103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
613103285Sikob#endif
614111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
615111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
616111942Ssimokawa#if 0
617103285Sikob		xferq->queued --;
618103285Sikob#endif
619111942Ssimokawa		if (sxfer->resp != 0)
620111942Ssimokawa			ifp->if_ierrors ++;
621111942Ssimokawa		fp = (struct fw_pkt *)sxfer->buf;
622111942Ssimokawa		/* XXX */
623111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
624111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
625111942Ssimokawa		m = sxfer->mbuf;
626111942Ssimokawa
627111942Ssimokawa		/* insert rbuf */
628111942Ssimokawa		sxfer->mbuf = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
629111942Ssimokawa		sxfer->buf = mtod(sxfer->mbuf, char *);
630111942Ssimokawa		STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
631111942Ssimokawa
632111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
633111942Ssimokawa		c = mtod(m, char *);
634111942Ssimokawa#if __FreeBSD_version < 500000
635111942Ssimokawa		eh = (struct ether_header *)c;
636111942Ssimokawa		m->m_data += sizeof(struct ether_header);
637108712Ssimokawa#endif
638111942Ssimokawa		m->m_len = m->m_pkthdr.len =
639111942Ssimokawa				ntohs(fp->mode.stream.len) - ETHER_ALIGN;
640103285Sikob		m->m_pkthdr.rcvif = ifp;
641103285Sikob#if 0
642103285Sikob		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
643103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
644103285Sikob			 "%02x %02x %02x %02x\n"
645103285Sikob			 "%02x %02x %02x %02x\n"
646103285Sikob			 "%02x %02x %02x %02x\n"
647103285Sikob			 "%02x %02x %02x %02x\n",
648103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
649103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
650103285Sikob			 c[12], c[13], c[14], c[15],
651103285Sikob			 c[16], c[17], c[18], c[19],
652103285Sikob			 c[20], c[21], c[22], c[23],
653103285Sikob			 c[20], c[21], c[22], c[23]
654103285Sikob		 );
655103285Sikob#endif
656108712Ssimokawa#if __FreeBSD_version >= 500000
657106937Ssam		(*ifp->if_input)(ifp, m);
658108712Ssimokawa#else
659108712Ssimokawa		ether_input(ifp, eh, m);
660108712Ssimokawa#endif
661103285Sikob		ifp->if_ipackets ++;
662103285Sikob	}
663111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
664111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
665103285Sikob}
666103285Sikob
667103285Sikob
668103285Sikobstatic devclass_t fwe_devclass;
669103285Sikob
670103285Sikobstatic device_method_t fwe_methods[] = {
671103285Sikob	/* device interface */
672103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
673103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
674103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
675103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
676103285Sikob	{ 0, 0 }
677103285Sikob};
678103285Sikob
679103285Sikobstatic driver_t fwe_driver = {
680103285Sikob        "if_fwe",
681103285Sikob	fwe_methods,
682103285Sikob	sizeof(struct fwe_softc),
683103285Sikob};
684103285Sikob
685103285Sikob
686103285SikobDRIVER_MODULE(if_fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
687103285SikobMODULE_VERSION(if_fwe, 1);
688103285SikobMODULE_DEPEND(if_fwe, firewire, 1, 1, 1);
689