if_fwe.c revision 129552
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 129552 2004-05-21 17:11:34Z yar $
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>
55127468Ssimokawa#ifdef __DragonFly__
56127468Ssimokawa#include <net/vlan/if_vlan_var.h>
57127468Ssimokawa#include <bus/firewire/firewire.h>
58127468Ssimokawa#include <bus/firewire/firewirereg.h>
59127468Ssimokawa#include "if_fwevar.h"
60127468Ssimokawa#else
61103285Sikob#include <net/if_vlan_var.h>
62103285Sikob
63103285Sikob#include <dev/firewire/firewire.h>
64103285Sikob#include <dev/firewire/firewirereg.h>
65103285Sikob#include <dev/firewire/if_fwevar.h>
66127468Ssimokawa#endif
67103285Sikob
68122161Ssimokawa#define FWEDEBUG	if (fwedebug) if_printf
69111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
70103285Sikob
71103285Sikob/* network interface */
72124169Ssimokawastatic void fwe_start (struct ifnet *);
73124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t);
74124169Ssimokawastatic void fwe_init (void *);
75103285Sikob
76124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *);
77124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *);
78124169Ssimokawastatic void fwe_as_input (struct fw_xferq *);
79103285Sikob
80103285Sikobstatic int fwedebug = 0;
81103285Sikobstatic int stream_ch = 1;
82116139Ssimokawastatic int tx_speed = 2;
83122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE;
84103285Sikob
85108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
86103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
87103285SikobSYSCTL_DECL(_hw_firewire);
88103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
89122603Ssimokawa	"Ethernet emulation subsystem");
90103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
91103285Sikob	"Stream channel to use");
92116139SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
93122603Ssimokawa	"Transmission speed");
94122603SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
95122603Ssimokawa	0, "Length of the receive queue");
96103285Sikob
97122603SsimokawaTUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
98122603SsimokawaTUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
99122603SsimokawaTUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
100122603Ssimokawa
101103285Sikob#ifdef DEVICE_POLLING
102103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)			\
103103285Sikob	if (ether_poll_register(func, ifp)) {			\
104103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
105103285Sikob		fc->set_intr(fc, 0);				\
106103285Sikob	}
107103285Sikob
108103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)				\
109103285Sikob	do {							\
110103285Sikob		struct firewire_comm *fc = (fwe)->fd.fc;	\
111103285Sikob		ether_poll_deregister(ifp);			\
112103285Sikob		fc->set_intr(fc, 1);				\
113103285Sikob	} while(0)						\
114103285Sikob
115103285Sikobstatic poll_handler_t fwe_poll;
116103285Sikob
117103285Sikobstatic void
118103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
119103285Sikob{
120103285Sikob	struct fwe_softc *fwe;
121103285Sikob	struct firewire_comm *fc;
122103285Sikob
123103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
124103285Sikob	fc = fwe->fd.fc;
125103285Sikob	if (cmd == POLL_DEREGISTER) {
126103285Sikob		/* enable interrupts */
127103285Sikob		fc->set_intr(fc, 1);
128103285Sikob		return;
129103285Sikob	}
130103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
131103285Sikob}
132103285Sikob#else
133103285Sikob#define FWE_POLL_REGISTER(func, fwe, ifp)
134103285Sikob#define FWE_POLL_DEREGISTER(fwe, ifp)
135103285Sikob#endif
136103285Sikobstatic void
137103285Sikobfwe_identify(driver_t *driver, device_t parent)
138103285Sikob{
139121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
140103285Sikob}
141103285Sikob
142103285Sikobstatic int
143103285Sikobfwe_probe(device_t dev)
144103285Sikob{
145103285Sikob	device_t pa;
146103285Sikob
147103285Sikob	pa = device_get_parent(dev);
148103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
149103285Sikob		return(ENXIO);
150103285Sikob	}
151103285Sikob
152108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
153103285Sikob	return (0);
154103285Sikob}
155103285Sikob
156103285Sikobstatic int
157103285Sikobfwe_attach(device_t dev)
158103285Sikob{
159103285Sikob	struct fwe_softc *fwe;
160103285Sikob	struct ifnet *ifp;
161103285Sikob	int unit, s;
162103285Sikob	u_char *eaddr;
163109814Ssimokawa	struct fw_eui64 *eui;
164103285Sikob
165103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
166103285Sikob	unit = device_get_unit(dev);
167103285Sikob
168103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
169103285Sikob	/* XXX */
170103285Sikob	fwe->stream_ch = stream_ch;
171103285Sikob	fwe->dma_ch = -1;
172103285Sikob
173103285Sikob	fwe->fd.fc = device_get_ivars(dev);
174124251Ssimokawa	if (tx_speed < 0)
175124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
176124251Ssimokawa
177103285Sikob	fwe->fd.dev = dev;
178103285Sikob	fwe->fd.post_explore = NULL;
179103285Sikob	fwe->eth_softc.fwe = fwe;
180103285Sikob
181103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
182103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
183103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
184103285Sikob
185103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
186103285Sikob#define LOCAL (0x02)
187103285Sikob#define GROUP (0x01)
188103285Sikob	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
189109814Ssimokawa
190109814Ssimokawa	eui = &fwe->fd.fc->eui;
191109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
192109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
193109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
194109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
195109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
196109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
197107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
198107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
199103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
200103285Sikob
201103285Sikob	/* fill the rest and attach interface */
202103285Sikob	ifp = &fwe->fwe_if;
203103285Sikob	ifp->if_softc = &fwe->eth_softc;
204103285Sikob
205127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
206121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
207122212Ssimokawa#else
208122212Ssimokawa	ifp->if_unit = unit;
209122212Ssimokawa	ifp->if_name = "fwe";
210122212Ssimokawa#endif
211103285Sikob	ifp->if_init = fwe_init;
212103285Sikob	ifp->if_output = ether_output;
213103285Sikob	ifp->if_start = fwe_start;
214103285Sikob	ifp->if_ioctl = fwe_ioctl;
215103285Sikob	ifp->if_mtu = ETHERMTU;
216103285Sikob	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
217111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
218103285Sikob
219103285Sikob	s = splimp();
220127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
221127468Ssimokawa	ether_ifattach(ifp, 1);
222127468Ssimokawa#else
223106937Ssam	ether_ifattach(ifp, eaddr);
224108712Ssimokawa#endif
225103285Sikob	splx(s);
226103285Sikob
227103285Sikob        /* Tell the upper layer(s) we support long frames. */
228103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
229127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
230106937Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
231129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
232108712Ssimokawa#endif
233103285Sikob
234103285Sikob
235122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
236103285Sikob	return 0;
237103285Sikob}
238103285Sikob
239103285Sikobstatic void
240103285Sikobfwe_stop(struct fwe_softc *fwe)
241103285Sikob{
242103285Sikob	struct firewire_comm *fc;
243103285Sikob	struct fw_xferq *xferq;
244103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
245111942Ssimokawa	struct fw_xfer *xfer, *next;
246111942Ssimokawa	int i;
247103285Sikob
248103285Sikob	fc = fwe->fd.fc;
249103285Sikob
250103285Sikob	FWE_POLL_DEREGISTER(fwe, ifp);
251103285Sikob
252103285Sikob	if (fwe->dma_ch >= 0) {
253103285Sikob		xferq = fc->ir[fwe->dma_ch];
254103285Sikob
255103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
256103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
257103285Sikob		xferq->flag &=
258113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
259113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
260111942Ssimokawa		xferq->hand =  NULL;
261111942Ssimokawa
262111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
263111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
264111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
265111942Ssimokawa
266111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
267111942Ssimokawa					xfer = next) {
268111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
269111942Ssimokawa			fw_xfer_free(xfer);
270111942Ssimokawa		}
271111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
272111942Ssimokawa
273111942Ssimokawa		xferq->bulkxfer =  NULL;
274103285Sikob		fwe->dma_ch = -1;
275103285Sikob	}
276103285Sikob
277103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
278103285Sikob}
279103285Sikob
280103285Sikobstatic int
281103285Sikobfwe_detach(device_t dev)
282103285Sikob{
283103285Sikob	struct fwe_softc *fwe;
284103285Sikob	int s;
285103285Sikob
286103285Sikob	fwe = (struct fwe_softc *)device_get_softc(dev);
287103285Sikob	s = splimp();
288103285Sikob
289103285Sikob	fwe_stop(fwe);
290127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
291127468Ssimokawa	ether_ifdetach(&fwe->fwe_if, 1);
292127468Ssimokawa#else
293106937Ssam	ether_ifdetach(&fwe->fwe_if);
294108712Ssimokawa#endif
295103285Sikob
296103285Sikob	splx(s);
297103285Sikob	return 0;
298103285Sikob}
299103285Sikob
300103285Sikobstatic void
301103285Sikobfwe_init(void *arg)
302103285Sikob{
303103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
304103285Sikob	struct firewire_comm *fc;
305103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
306103285Sikob	struct fw_xferq *xferq;
307111942Ssimokawa	struct fw_xfer *xfer;
308113584Ssimokawa	struct mbuf *m;
309103285Sikob	int i;
310103285Sikob
311122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
312103285Sikob
313103285Sikob	/* XXX keep promiscoud mode */
314103285Sikob	ifp->if_flags |= IFF_PROMISC;
315103285Sikob
316103285Sikob	fc = fwe->fd.fc;
317103285Sikob#define START 0
318103285Sikob	if (fwe->dma_ch < 0) {
319103285Sikob		for (i = START; i < fc->nisodma; i ++) {
320103285Sikob			xferq = fc->ir[i];
321103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
322118312Ssimokawa				goto found;
323103285Sikob		}
324118312Ssimokawa		printf("no free dma channel\n");
325118312Ssimokawa		return;
326118312Ssimokawafound:
327103285Sikob		fwe->dma_ch = i;
328103285Sikob		fwe->stream_ch = stream_ch;
329103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
330103285Sikob		/* allocate DMA channel and init packet mode */
331113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
332113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
333112400Ssimokawa		xferq->flag &= ~0xff;
334103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
335103285Sikob		/* register fwe_input handler */
336103285Sikob		xferq->sc = (caddr_t) fwe;
337103285Sikob		xferq->hand = fwe_as_input;
338122603Ssimokawa		xferq->bnchunk = rx_queue_len;
339111942Ssimokawa		xferq->bnpacket = 1;
340111942Ssimokawa		xferq->psize = MCLBYTES;
341111942Ssimokawa		xferq->queued = 0;
342113584Ssimokawa		xferq->buf = NULL;
343111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
344113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
345113584Ssimokawa							M_FWE, M_WAITOK);
346111942Ssimokawa		if (xferq->bulkxfer == NULL) {
347111942Ssimokawa			printf("if_fwe: malloc failed\n");
348111942Ssimokawa			return;
349111942Ssimokawa		}
350111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
351111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
352111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
353111942Ssimokawa		xferq->stproc = NULL;
354111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
355113584Ssimokawa			m =
356127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
357127468Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
358127468Ssimokawa#else
359111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
360111942Ssimokawa#endif
361113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
362113584Ssimokawa			if (m != NULL) {
363113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
364113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
365113584Ssimokawa						&xferq->bulkxfer[i], link);
366113584Ssimokawa			} else
367113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
368111942Ssimokawa		}
369111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
370111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
371111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
372111942Ssimokawa			if (xfer == NULL)
373111942Ssimokawa				break;
374120660Ssimokawa			xfer->send.spd = tx_speed;
375111942Ssimokawa			xfer->fc = fwe->fd.fc;
376111942Ssimokawa			xfer->retry_req = fw_asybusy;
377111942Ssimokawa			xfer->sc = (caddr_t)fwe;
378111942Ssimokawa			xfer->act.hand = fwe_output_callback;
379111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
380111942Ssimokawa		}
381103285Sikob	} else
382103285Sikob		xferq = fc->ir[fwe->dma_ch];
383103285Sikob
384103285Sikob
385103285Sikob	/* start dma */
386103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
387103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
388103285Sikob
389103285Sikob	ifp->if_flags |= IFF_RUNNING;
390103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
391103285Sikob
392103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
393103285Sikob#if 0
394103285Sikob	/* attempt to start output */
395103285Sikob	fwe_start(ifp);
396103285Sikob#endif
397103285Sikob}
398103285Sikob
399103285Sikob
400103285Sikobstatic int
401103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
402103285Sikob{
403103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
404103285Sikob	struct ifstat *ifs = NULL;
405103285Sikob	int s, error, len;
406103285Sikob
407103285Sikob	switch (cmd) {
408103285Sikob		case SIOCSIFFLAGS:
409103285Sikob			s = splimp();
410103285Sikob			if (ifp->if_flags & IFF_UP) {
411103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
412103285Sikob					fwe_init(&fwe->eth_softc);
413103285Sikob			} else {
414103285Sikob				if (ifp->if_flags & IFF_RUNNING)
415103285Sikob					fwe_stop(fwe);
416103285Sikob			}
417103285Sikob			/* XXX keep promiscoud mode */
418103285Sikob			ifp->if_flags |= IFF_PROMISC;
419103285Sikob			splx(s);
420103285Sikob			break;
421103285Sikob		case SIOCADDMULTI:
422103285Sikob		case SIOCDELMULTI:
423108712Ssimokawa			break;
424103285Sikob
425103285Sikob		case SIOCGIFSTATUS:
426103285Sikob			s = splimp();
427103285Sikob			ifs = (struct ifstat *)data;
428103285Sikob			len = strlen(ifs->ascii);
429103285Sikob			if (len < sizeof(ifs->ascii))
430103285Sikob				snprintf(ifs->ascii + len,
431103285Sikob					sizeof(ifs->ascii) - len,
432103285Sikob					"\tch %d dma %d\n",
433103285Sikob						fwe->stream_ch, fwe->dma_ch);
434103285Sikob			splx(s);
435108712Ssimokawa			break;
436127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
437103285Sikob		default:
438108712Ssimokawa#else
439108712Ssimokawa		case SIOCSIFADDR:
440108712Ssimokawa		case SIOCGIFADDR:
441108712Ssimokawa		case SIOCSIFMTU:
442108712Ssimokawa#endif
443106937Ssam			s = splimp();
444106937Ssam			error = ether_ioctl(ifp, cmd, data);
445106937Ssam			splx(s);
446106937Ssam			return (error);
447127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
448108712Ssimokawa		default:
449108712Ssimokawa			return (EINVAL);
450108712Ssimokawa#endif
451103285Sikob	}
452103285Sikob
453103285Sikob	return (0);
454103285Sikob}
455103285Sikob
456103285Sikobstatic void
457111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
458111942Ssimokawa{
459111942Ssimokawa	struct fwe_softc *fwe;
460111942Ssimokawa	struct ifnet *ifp;
461111942Ssimokawa	int s;
462111942Ssimokawa
463111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
464111942Ssimokawa	ifp = &fwe->fwe_if;
465111942Ssimokawa	/* XXX error check */
466122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
467111942Ssimokawa	if (xfer->resp != 0)
468111942Ssimokawa		ifp->if_oerrors ++;
469111942Ssimokawa
470111942Ssimokawa	m_freem(xfer->mbuf);
471111942Ssimokawa	fw_xfer_unload(xfer);
472113584Ssimokawa
473111942Ssimokawa	s = splimp();
474111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
475111942Ssimokawa	splx(s);
476113584Ssimokawa
477113584Ssimokawa	/* for queue full */
478111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
479111942Ssimokawa		fwe_start(ifp);
480111942Ssimokawa}
481111942Ssimokawa
482111942Ssimokawastatic void
483103285Sikobfwe_start(struct ifnet *ifp)
484103285Sikob{
485103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
486103285Sikob	int s;
487103285Sikob
488122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
489103285Sikob
490103285Sikob	if (fwe->dma_ch < 0) {
491103285Sikob		struct mbuf	*m = NULL;
492103285Sikob
493122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
494103285Sikob
495103285Sikob		s = splimp();
496103285Sikob		do {
497103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
498103285Sikob			if (m != NULL)
499103285Sikob				m_freem(m);
500103285Sikob			ifp->if_oerrors ++;
501103285Sikob		} while (m != NULL);
502103285Sikob		splx(s);
503103285Sikob
504103285Sikob		return;
505103285Sikob	}
506103285Sikob
507103285Sikob	s = splimp();
508103285Sikob	ifp->if_flags |= IFF_OACTIVE;
509103285Sikob
510103285Sikob	if (ifp->if_snd.ifq_len != 0)
511103285Sikob		fwe_as_output(fwe, ifp);
512103285Sikob
513103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
514103285Sikob	splx(s);
515103285Sikob}
516103285Sikob
517111942Ssimokawa#define HDR_LEN 4
518111942Ssimokawa#ifndef ETHER_ALIGN
519111942Ssimokawa#define ETHER_ALIGN 2
520103285Sikob#endif
521103285Sikob/* Async. stream output */
522103285Sikobstatic void
523103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
524103285Sikob{
525103285Sikob	struct mbuf *m;
526103285Sikob	struct fw_xfer *xfer;
527103285Sikob	struct fw_xferq *xferq;
528103285Sikob	struct fw_pkt *fp;
529103285Sikob	int i = 0;
530103285Sikob
531103285Sikob	xfer = NULL;
532103285Sikob	xferq = fwe->fd.fc->atq;
533111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
534111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
535111942Ssimokawa		if (xfer == NULL) {
536111942Ssimokawa			printf("if_fwe: lack of xfer\n");
537111942Ssimokawa			return;
538111942Ssimokawa		}
539103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
540103285Sikob		if (m == NULL)
541103285Sikob			break;
542111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
543127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
544108712Ssimokawa		if (ifp->if_bpf != NULL)
545108712Ssimokawa			bpf_mtap(ifp, m);
546127468Ssimokawa#else
547127468Ssimokawa		BPF_MTAP(ifp, m);
548108712Ssimokawa#endif
549103285Sikob
550103285Sikob		/* keep ip packet alignment for alpha */
551111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
552120660Ssimokawa		fp = &xfer->send.hdr;
553120660Ssimokawa		*(u_int32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
554113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
555103285Sikob		xfer->mbuf = m;
556120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
557103285Sikob
558111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
559103285Sikob			/* error */
560103285Sikob			ifp->if_oerrors ++;
561103285Sikob			/* XXX set error code */
562103285Sikob			fwe_output_callback(xfer);
563103285Sikob		} else {
564103285Sikob			ifp->if_opackets ++;
565111942Ssimokawa			i++;
566103285Sikob		}
567103285Sikob	}
568103285Sikob#if 0
569103285Sikob	if (i > 1)
570103285Sikob		printf("%d queued\n", i);
571103285Sikob#endif
572111942Ssimokawa	if (i > 0)
573111942Ssimokawa		xferq->start(fwe->fd.fc);
574103285Sikob}
575103285Sikob
576103285Sikob/* Async. stream output */
577103285Sikobstatic void
578103285Sikobfwe_as_input(struct fw_xferq *xferq)
579103285Sikob{
580113584Ssimokawa	struct mbuf *m, *m0;
581103285Sikob	struct ifnet *ifp;
582103285Sikob	struct fwe_softc *fwe;
583111942Ssimokawa	struct fw_bulkxfer *sxfer;
584111942Ssimokawa	struct fw_pkt *fp;
585103285Sikob	u_char *c;
586127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
587111942Ssimokawa	struct ether_header *eh;
588111942Ssimokawa#endif
589103285Sikob
590103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
591103285Sikob	ifp = &fwe->fwe_if;
592103285Sikob#if 0
593103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
594103285Sikob#endif
595111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
596111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
597113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
598111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
599111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
600111942Ssimokawa		m = sxfer->mbuf;
601111942Ssimokawa
602119119Ssimokawa		/* insert new rbuf */
603113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
604113584Ssimokawa		if (m0 != NULL) {
605113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
606113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
607113584Ssimokawa		} else
608113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
609111942Ssimokawa
610119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
611119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
612119119Ssimokawa			m_freem(m);
613119119Ssimokawa			ifp->if_ierrors ++;
614119119Ssimokawa			continue;
615119119Ssimokawa		}
616119119Ssimokawa
617111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
618111942Ssimokawa		c = mtod(m, char *);
619127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
620111942Ssimokawa		eh = (struct ether_header *)c;
621111942Ssimokawa		m->m_data += sizeof(struct ether_header);
622108712Ssimokawa#endif
623111942Ssimokawa		m->m_len = m->m_pkthdr.len =
624113584Ssimokawa				fp->mode.stream.len - ETHER_ALIGN;
625103285Sikob		m->m_pkthdr.rcvif = ifp;
626103285Sikob#if 0
627122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
628103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
629103285Sikob			 "%02x %02x %02x %02x\n"
630103285Sikob			 "%02x %02x %02x %02x\n"
631103285Sikob			 "%02x %02x %02x %02x\n"
632103285Sikob			 "%02x %02x %02x %02x\n",
633103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
634103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
635103285Sikob			 c[12], c[13], c[14], c[15],
636103285Sikob			 c[16], c[17], c[18], c[19],
637103285Sikob			 c[20], c[21], c[22], c[23],
638103285Sikob			 c[20], c[21], c[22], c[23]
639103285Sikob		 );
640103285Sikob#endif
641127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
642127468Ssimokawa		ether_input(ifp, eh, m);
643127468Ssimokawa#else
644106937Ssam		(*ifp->if_input)(ifp, m);
645108712Ssimokawa#endif
646103285Sikob		ifp->if_ipackets ++;
647103285Sikob	}
648111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
649111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
650103285Sikob}
651103285Sikob
652103285Sikob
653103285Sikobstatic devclass_t fwe_devclass;
654103285Sikob
655103285Sikobstatic device_method_t fwe_methods[] = {
656103285Sikob	/* device interface */
657103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
658103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
659103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
660103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
661103285Sikob	{ 0, 0 }
662103285Sikob};
663103285Sikob
664103285Sikobstatic driver_t fwe_driver = {
665121953Ssimokawa        "fwe",
666103285Sikob	fwe_methods,
667103285Sikob	sizeof(struct fwe_softc),
668103285Sikob};
669103285Sikob
670103285Sikob
671127468Ssimokawa#ifdef __DragonFly__
672127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
673127468Ssimokawa#endif
674113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
675113506SmdoddMODULE_VERSION(fwe, 1);
676113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
677