if_fwe.c revision 113584
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 113584 2003-04-17 03:38:03Z 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>
50113584Ssimokawa#include <machine/bus.h>
51103285Sikob
52103285Sikob#include <net/bpf.h>
53103285Sikob#include <net/ethernet.h>
54103285Sikob#include <net/if.h>
55103285Sikob#include <net/if_arp.h>
56103285Sikob#include <net/if_vlan_var.h>
57103285Sikob#include <net/route.h>
58103285Sikob
59103285Sikob#include <netinet/in.h>
60103285Sikob
61103285Sikob#include <dev/firewire/firewire.h>
62103285Sikob#include <dev/firewire/firewirereg.h>
63103285Sikob#include <dev/firewire/if_fwevar.h>
64103285Sikob
65103285Sikob#define FWEDEBUG	if (fwedebug) printf
66111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
67111942Ssimokawa#define RX_MAX_QUEUE	FWMAXQUEUE
68103285Sikob
69103285Sikob/* network interface */
70103285Sikobstatic void fwe_start __P((struct ifnet *));
71103285Sikobstatic int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
72103285Sikobstatic void fwe_init __P((void *));
73103285Sikob
74111942Ssimokawastatic void fwe_output_callback __P((struct fw_xfer *));
75103285Sikobstatic void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
76103285Sikobstatic void fwe_as_input __P((struct fw_xferq *));
77103285Sikob
78103285Sikobstatic int fwedebug = 0;
79103285Sikobstatic int stream_ch = 1;
80103285Sikob
81108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
82103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
83103285SikobSYSCTL_DECL(_hw_firewire);
84103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
85103285Sikob	"Ethernet Emulation Subsystem");
86103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
87103285Sikob	"Stream channel to use");
88103285Sikob
89103285Sikob#ifdef DEVICE_POLLING
90103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)			\
91103285Sikob	if (ether_poll_register(func, ifp)) {			\
92103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
93103285Sikob		fc->set_intr(fc, 0);				\
94103285Sikob	}
95103285Sikob
96103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)				\
97103285Sikob	do {							\
98103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
99103285Sikob		ether_poll_deregister(ifp);			\
100103285Sikob		fc->set_intr(fc, 1);				\
101103285Sikob	} while(0)						\
102103285Sikob
103103285Sikobstatic poll_handler_t fwe_poll;
104103285Sikob
105103285Sikobstatic void
106103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
107103285Sikob{
108103285Sikob	struct fwe_softc *fwe;
109103285Sikob	struct firewire_comm *fc;
110103285Sikob
111103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
112103285Sikob	fc = fwe->fd.fc;
113103285Sikob	if (cmd == POLL_DEREGISTER) {
114103285Sikob		/* enable interrupts */
115103285Sikob		fc->set_intr(fc, 1);
116103285Sikob		return;
117103285Sikob	}
118103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
119103285Sikob}
120103285Sikob#else
121103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)
122103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)
123103285Sikob#endif
124103285Sikobstatic void
125103285Sikobfwe_identify(driver_t *driver, device_t parent)
126103285Sikob{
127103285Sikob	BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
128103285Sikob}
129103285Sikob
130103285Sikobstatic int
131103285Sikobfwe_probe(device_t dev)
132103285Sikob{
133103285Sikob	device_t pa;
134103285Sikob
135103285Sikob	pa = device_get_parent(dev);
136103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
137103285Sikob		return(ENXIO);
138103285Sikob	}
139103285Sikob
140108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
141103285Sikob	return (0);
142103285Sikob}
143103285Sikob
144103285Sikobstatic int
145103285Sikobfwe_attach(device_t dev)
146103285Sikob{
147103285Sikob	struct fwe_softc *fwe;
148103285Sikob	struct ifnet *ifp;
149103285Sikob	int unit, s;
150103285Sikob	u_char *eaddr;
151109814Ssimokawa	struct fw_eui64 *eui;
152103285Sikob
153103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
154103285Sikob	unit = device_get_unit(dev);
155103285Sikob
156103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
157103285Sikob	/* XXX */
158103285Sikob	fwe->stream_ch = stream_ch;
159103285Sikob	fwe->dma_ch = -1;
160103285Sikob
161103285Sikob	fwe->fd.fc = device_get_ivars(dev);
162103285Sikob	fwe->fd.dev = dev;
163103285Sikob	fwe->fd.post_explore = NULL;
164103285Sikob	fwe->eth_softc.fwe = fwe;
165103285Sikob
166103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
167103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
168103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
169103285Sikob
170103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
171103285Sikob#define LOCAL (0x02)
172103285Sikob#define GROUP (0x01)
173103285Sikob	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
174109814Ssimokawa
175109814Ssimokawa	eui = &fwe->fd.fc->eui;
176109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
177109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
178109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
179109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
180109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
181109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
182107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
183107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
184103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
185103285Sikob
186103285Sikob	/* fill the rest and attach interface */
187103285Sikob	ifp = &fwe->fwe_if;
188103285Sikob	ifp->if_softc = &fwe->eth_softc;
189103285Sikob
190103285Sikob	ifp->if_unit = unit;
191103285Sikob	ifp->if_name = "fwe";
192103285Sikob	ifp->if_init = fwe_init;
193103285Sikob	ifp->if_output = ether_output;
194103285Sikob	ifp->if_start = fwe_start;
195103285Sikob	ifp->if_ioctl = fwe_ioctl;
196103285Sikob	ifp->if_mtu = ETHERMTU;
197103285Sikob	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
198111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
199103285Sikob
200103285Sikob	s = splimp();
201108712Ssimokawa#if __FreeBSD_version >= 500000
202106937Ssam	ether_ifattach(ifp, eaddr);
203108712Ssimokawa#else
204108712Ssimokawa	ether_ifattach(ifp, 1);
205108712Ssimokawa#endif
206103285Sikob	splx(s);
207103285Sikob
208103285Sikob        /* Tell the upper layer(s) we support long frames. */
209103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
210108712Ssimokawa#if __FreeBSD_version >= 500000
211106937Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
212108712Ssimokawa#endif
213103285Sikob
214103285Sikob
215103285Sikob	FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
216103285Sikob	return 0;
217103285Sikob}
218103285Sikob
219103285Sikobstatic void
220103285Sikobfwe_stop(struct fwe_softc *fwe)
221103285Sikob{
222103285Sikob	struct firewire_comm *fc;
223103285Sikob	struct fw_xferq *xferq;
224103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
225111942Ssimokawa	struct fw_xfer *xfer, *next;
226111942Ssimokawa	int i;
227103285Sikob
228103285Sikob	fc = fwe->fd.fc;
229103285Sikob
230103285Sikob	FWE_POLL_DEREGISTER(fwe, ifp);
231103285Sikob
232103285Sikob	if (fwe->dma_ch >= 0) {
233103285Sikob		xferq = fc->ir[fwe->dma_ch];
234103285Sikob
235103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
236103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
237103285Sikob		xferq->flag &=
238113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
239113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
240111942Ssimokawa		xferq->hand =  NULL;
241111942Ssimokawa
242111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
243111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
244111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
245111942Ssimokawa
246111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
247111942Ssimokawa					xfer = next) {
248111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
249111942Ssimokawa			fw_xfer_free(xfer);
250111942Ssimokawa		}
251111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
252111942Ssimokawa
253111942Ssimokawa		xferq->bulkxfer =  NULL;
254103285Sikob		fwe->dma_ch = -1;
255103285Sikob	}
256103285Sikob
257103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
258103285Sikob}
259103285Sikob
260103285Sikobstatic int
261103285Sikobfwe_detach(device_t dev)
262103285Sikob{
263103285Sikob	struct fwe_softc *fwe;
264103285Sikob	int s;
265103285Sikob
266103285Sikob	fwe = (struct fwe_softc *)device_get_softc(dev);
267103285Sikob	s = splimp();
268103285Sikob
269103285Sikob	fwe_stop(fwe);
270108712Ssimokawa#if __FreeBSD_version >= 500000
271106937Ssam	ether_ifdetach(&fwe->fwe_if);
272108712Ssimokawa#else
273108712Ssimokawa	ether_ifdetach(&fwe->fwe_if, 1);
274108712Ssimokawa#endif
275103285Sikob
276103285Sikob	splx(s);
277103285Sikob	return 0;
278103285Sikob}
279103285Sikob
280103285Sikobstatic void
281103285Sikobfwe_init(void *arg)
282103285Sikob{
283103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
284103285Sikob	struct firewire_comm *fc;
285103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
286103285Sikob	struct fw_xferq *xferq;
287111942Ssimokawa	struct fw_xfer *xfer;
288113584Ssimokawa	struct mbuf *m;
289103285Sikob	int i;
290103285Sikob
291103285Sikob	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
292103285Sikob
293103285Sikob	/* XXX keep promiscoud mode */
294103285Sikob	ifp->if_flags |= IFF_PROMISC;
295103285Sikob
296103285Sikob	fc = fwe->fd.fc;
297103285Sikob#define START 0
298103285Sikob	if (fwe->dma_ch < 0) {
299103285Sikob		xferq = NULL;
300103285Sikob		for (i = START; i < fc->nisodma; i ++) {
301103285Sikob			xferq = fc->ir[i];
302103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
303103285Sikob				break;
304103285Sikob		}
305103285Sikob
306103285Sikob		if (xferq == NULL) {
307103285Sikob			printf("no free dma channel\n");
308103285Sikob			return;
309103285Sikob		}
310103285Sikob		fwe->dma_ch = i;
311103285Sikob		fwe->stream_ch = stream_ch;
312103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
313103285Sikob		/* allocate DMA channel and init packet mode */
314113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
315113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
316112400Ssimokawa		xferq->flag &= ~0xff;
317103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
318103285Sikob		/* register fwe_input handler */
319103285Sikob		xferq->sc = (caddr_t) fwe;
320103285Sikob		xferq->hand = fwe_as_input;
321111942Ssimokawa		xferq->bnchunk = RX_MAX_QUEUE;
322111942Ssimokawa		xferq->bnpacket = 1;
323111942Ssimokawa		xferq->psize = MCLBYTES;
324111942Ssimokawa		xferq->queued = 0;
325113584Ssimokawa		xferq->buf = NULL;
326111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
327113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
328113584Ssimokawa							M_FWE, M_WAITOK);
329111942Ssimokawa		if (xferq->bulkxfer == NULL) {
330111942Ssimokawa			printf("if_fwe: malloc failed\n");
331111942Ssimokawa			return;
332111942Ssimokawa		}
333111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
334111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
335111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
336111942Ssimokawa		xferq->stproc = NULL;
337111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
338113584Ssimokawa			m =
339111942Ssimokawa#if __FreeBSD_version >= 500000
340111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
341111942Ssimokawa#else
342111942Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
343111942Ssimokawa#endif
344113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
345113584Ssimokawa			if (m != NULL) {
346113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
347113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
348113584Ssimokawa						&xferq->bulkxfer[i], link);
349113584Ssimokawa			} else
350113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
351111942Ssimokawa		}
352111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
353111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
354111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
355111942Ssimokawa			if (xfer == NULL)
356111942Ssimokawa				break;
357111942Ssimokawa			xfer->spd = 2;
358111942Ssimokawa			xfer->fc = fwe->fd.fc;
359111942Ssimokawa			xfer->retry_req = fw_asybusy;
360111942Ssimokawa			xfer->sc = (caddr_t)fwe;
361111942Ssimokawa			xfer->act.hand = fwe_output_callback;
362111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
363111942Ssimokawa		}
364103285Sikob	} else
365103285Sikob		xferq = fc->ir[fwe->dma_ch];
366103285Sikob
367103285Sikob
368103285Sikob	/* start dma */
369103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
370103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
371103285Sikob
372103285Sikob	ifp->if_flags |= IFF_RUNNING;
373103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
374103285Sikob
375103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
376103285Sikob#if 0
377103285Sikob	/* attempt to start output */
378103285Sikob	fwe_start(ifp);
379103285Sikob#endif
380103285Sikob}
381103285Sikob
382103285Sikob
383103285Sikobstatic int
384103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
385103285Sikob{
386103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
387103285Sikob	struct ifstat *ifs = NULL;
388103285Sikob	int s, error, len;
389103285Sikob
390103285Sikob	switch (cmd) {
391103285Sikob		case SIOCSIFFLAGS:
392103285Sikob			s = splimp();
393103285Sikob			if (ifp->if_flags & IFF_UP) {
394103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
395103285Sikob					fwe_init(&fwe->eth_softc);
396103285Sikob			} else {
397103285Sikob				if (ifp->if_flags & IFF_RUNNING)
398103285Sikob					fwe_stop(fwe);
399103285Sikob			}
400103285Sikob			/* XXX keep promiscoud mode */
401103285Sikob			ifp->if_flags |= IFF_PROMISC;
402103285Sikob			splx(s);
403103285Sikob			break;
404103285Sikob		case SIOCADDMULTI:
405103285Sikob		case SIOCDELMULTI:
406108712Ssimokawa			break;
407103285Sikob
408103285Sikob		case SIOCGIFSTATUS:
409103285Sikob			s = splimp();
410103285Sikob			ifs = (struct ifstat *)data;
411103285Sikob			len = strlen(ifs->ascii);
412103285Sikob			if (len < sizeof(ifs->ascii))
413103285Sikob				snprintf(ifs->ascii + len,
414103285Sikob					sizeof(ifs->ascii) - len,
415103285Sikob					"\tch %d dma %d\n",
416103285Sikob						fwe->stream_ch, fwe->dma_ch);
417103285Sikob			splx(s);
418108712Ssimokawa			break;
419108712Ssimokawa#if __FreeBSD_version >= 500000
420103285Sikob		default:
421108712Ssimokawa#else
422108712Ssimokawa		case SIOCSIFADDR:
423108712Ssimokawa		case SIOCGIFADDR:
424108712Ssimokawa		case SIOCSIFMTU:
425108712Ssimokawa#endif
426106937Ssam			s = splimp();
427106937Ssam			error = ether_ioctl(ifp, cmd, data);
428106937Ssam			splx(s);
429106937Ssam			return (error);
430108712Ssimokawa#if __FreeBSD_version < 500000
431108712Ssimokawa		default:
432108712Ssimokawa			return (EINVAL);
433108712Ssimokawa#endif
434103285Sikob	}
435103285Sikob
436103285Sikob	return (0);
437103285Sikob}
438103285Sikob
439103285Sikobstatic void
440111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
441111942Ssimokawa{
442111942Ssimokawa	struct fwe_softc *fwe;
443111942Ssimokawa	struct ifnet *ifp;
444111942Ssimokawa	int s;
445111942Ssimokawa
446111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
447111942Ssimokawa	ifp = &fwe->fwe_if;
448111942Ssimokawa	/* XXX error check */
449111942Ssimokawa	FWEDEBUG("resp = %d\n", xfer->resp);
450111942Ssimokawa	if (xfer->resp != 0)
451111942Ssimokawa		ifp->if_oerrors ++;
452111942Ssimokawa
453111942Ssimokawa	m_freem(xfer->mbuf);
454111942Ssimokawa	xfer->send.buf = NULL;
455111942Ssimokawa	fw_xfer_unload(xfer);
456113584Ssimokawa
457111942Ssimokawa	s = splimp();
458111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
459111942Ssimokawa	splx(s);
460113584Ssimokawa
461113584Ssimokawa	/* for queue full */
462111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
463111942Ssimokawa		fwe_start(ifp);
464111942Ssimokawa}
465111942Ssimokawa
466111942Ssimokawastatic void
467103285Sikobfwe_start(struct ifnet *ifp)
468103285Sikob{
469103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
470103285Sikob	int s;
471103285Sikob
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	s = splimp();
492103285Sikob	ifp->if_flags |= IFF_OACTIVE;
493103285Sikob
494103285Sikob	if (ifp->if_snd.ifq_len != 0)
495103285Sikob		fwe_as_output(fwe, ifp);
496103285Sikob
497103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
498103285Sikob	splx(s);
499103285Sikob}
500103285Sikob
501111942Ssimokawa#define HDR_LEN 4
502111942Ssimokawa#ifndef ETHER_ALIGN
503111942Ssimokawa#define ETHER_ALIGN 2
504103285Sikob#endif
505103285Sikob/* Async. stream output */
506103285Sikobstatic void
507103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
508103285Sikob{
509103285Sikob	struct mbuf *m;
510103285Sikob	struct fw_xfer *xfer;
511103285Sikob	struct fw_xferq *xferq;
512103285Sikob	struct fw_pkt *fp;
513103285Sikob	int i = 0;
514103285Sikob
515103285Sikob	xfer = NULL;
516103285Sikob	xferq = fwe->fd.fc->atq;
517111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
518111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
519111942Ssimokawa		if (xfer == NULL) {
520111942Ssimokawa			printf("if_fwe: lack of xfer\n");
521111942Ssimokawa			return;
522111942Ssimokawa		}
523103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
524103285Sikob		if (m == NULL)
525103285Sikob			break;
526111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
527108712Ssimokawa#if __FreeBSD_version >= 500000
528106937Ssam		BPF_MTAP(ifp, m);
529108712Ssimokawa#else
530108712Ssimokawa		if (ifp->if_bpf != NULL)
531108712Ssimokawa			bpf_mtap(ifp, m);
532108712Ssimokawa#endif
533103285Sikob
534103285Sikob		/* keep ip packet alignment for alpha */
535111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
536103285Sikob		fp = (struct fw_pkt *)&xfer->dst; /* XXX */
537103285Sikob		xfer->dst = *((int32_t *)&fwe->pkt_hdr);
538113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
539103285Sikob		xfer->send.buf = (caddr_t) fp;
540103285Sikob		xfer->mbuf = m;
541103285Sikob		xfer->send.len = m->m_pkthdr.len + HDR_LEN;
542103285Sikob
543111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
544103285Sikob			/* error */
545103285Sikob			ifp->if_oerrors ++;
546103285Sikob			/* XXX set error code */
547103285Sikob			fwe_output_callback(xfer);
548103285Sikob		} else {
549103285Sikob			ifp->if_opackets ++;
550111942Ssimokawa			i++;
551103285Sikob		}
552103285Sikob	}
553103285Sikob#if 0
554103285Sikob	if (i > 1)
555103285Sikob		printf("%d queued\n", i);
556103285Sikob#endif
557111942Ssimokawa	if (i > 0)
558111942Ssimokawa		xferq->start(fwe->fd.fc);
559103285Sikob}
560103285Sikob
561103285Sikob/* Async. stream output */
562103285Sikobstatic void
563103285Sikobfwe_as_input(struct fw_xferq *xferq)
564103285Sikob{
565113584Ssimokawa	struct mbuf *m, *m0;
566103285Sikob	struct ifnet *ifp;
567103285Sikob	struct fwe_softc *fwe;
568111942Ssimokawa	struct fw_bulkxfer *sxfer;
569111942Ssimokawa	struct fw_pkt *fp;
570103285Sikob	u_char *c;
571111942Ssimokawa#if __FreeBSD_version < 500000
572111942Ssimokawa	struct ether_header *eh;
573111942Ssimokawa#endif
574103285Sikob
575103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
576103285Sikob	ifp = &fwe->fwe_if;
577103285Sikob#if 0
578103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
579103285Sikob#endif
580111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
581111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
582111942Ssimokawa		if (sxfer->resp != 0)
583111942Ssimokawa			ifp->if_ierrors ++;
584113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
585111942Ssimokawa		/* XXX */
586111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
587111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
588111942Ssimokawa		m = sxfer->mbuf;
589111942Ssimokawa
590111942Ssimokawa		/* insert rbuf */
591113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
592113584Ssimokawa		if (m0 != NULL) {
593113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
594113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
595113584Ssimokawa		} else
596113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
597111942Ssimokawa
598111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
599111942Ssimokawa		c = mtod(m, char *);
600111942Ssimokawa#if __FreeBSD_version < 500000
601111942Ssimokawa		eh = (struct ether_header *)c;
602111942Ssimokawa		m->m_data += sizeof(struct ether_header);
603108712Ssimokawa#endif
604111942Ssimokawa		m->m_len = m->m_pkthdr.len =
605113584Ssimokawa				fp->mode.stream.len - ETHER_ALIGN;
606103285Sikob		m->m_pkthdr.rcvif = ifp;
607103285Sikob#if 0
608103285Sikob		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
609103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
610103285Sikob			 "%02x %02x %02x %02x\n"
611103285Sikob			 "%02x %02x %02x %02x\n"
612103285Sikob			 "%02x %02x %02x %02x\n"
613103285Sikob			 "%02x %02x %02x %02x\n",
614103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
615103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
616103285Sikob			 c[12], c[13], c[14], c[15],
617103285Sikob			 c[16], c[17], c[18], c[19],
618103285Sikob			 c[20], c[21], c[22], c[23],
619103285Sikob			 c[20], c[21], c[22], c[23]
620103285Sikob		 );
621103285Sikob#endif
622108712Ssimokawa#if __FreeBSD_version >= 500000
623106937Ssam		(*ifp->if_input)(ifp, m);
624108712Ssimokawa#else
625108712Ssimokawa		ether_input(ifp, eh, m);
626108712Ssimokawa#endif
627103285Sikob		ifp->if_ipackets ++;
628103285Sikob	}
629111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
630111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
631103285Sikob}
632103285Sikob
633103285Sikob
634103285Sikobstatic devclass_t fwe_devclass;
635103285Sikob
636103285Sikobstatic device_method_t fwe_methods[] = {
637103285Sikob	/* device interface */
638103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
639103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
640103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
641103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
642103285Sikob	{ 0, 0 }
643103285Sikob};
644103285Sikob
645103285Sikobstatic driver_t fwe_driver = {
646103285Sikob        "if_fwe",
647103285Sikob	fwe_methods,
648103285Sikob	sizeof(struct fwe_softc),
649103285Sikob};
650103285Sikob
651103285Sikob
652113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
653113506SmdoddMODULE_VERSION(fwe, 1);
654113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
655