if_fwe.c revision 139749
1139749Simp/*-
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 139749 2005-01-06 01:43:34Z imp $
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;
212132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
213132430Ssimokawa	ifp->if_output = ether_output;
214132430Ssimokawa#endif
215103285Sikob	ifp->if_start = fwe_start;
216103285Sikob	ifp->if_ioctl = fwe_ioctl;
217103285Sikob	ifp->if_mtu = ETHERMTU;
218133538Srwatson	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
219133538Srwatson	    IFF_NEEDSGIANT);
220111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
221103285Sikob
222103285Sikob	s = splimp();
223127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
224127468Ssimokawa	ether_ifattach(ifp, 1);
225127468Ssimokawa#else
226106937Ssam	ether_ifattach(ifp, eaddr);
227108712Ssimokawa#endif
228103285Sikob	splx(s);
229103285Sikob
230103285Sikob        /* Tell the upper layer(s) we support long frames. */
231103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
232127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
233106937Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
234129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
235108712Ssimokawa#endif
236103285Sikob
237103285Sikob
238122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
239103285Sikob	return 0;
240103285Sikob}
241103285Sikob
242103285Sikobstatic void
243103285Sikobfwe_stop(struct fwe_softc *fwe)
244103285Sikob{
245103285Sikob	struct firewire_comm *fc;
246103285Sikob	struct fw_xferq *xferq;
247103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
248111942Ssimokawa	struct fw_xfer *xfer, *next;
249111942Ssimokawa	int i;
250103285Sikob
251103285Sikob	fc = fwe->fd.fc;
252103285Sikob
253103285Sikob	FWE_POLL_DEREGISTER(fwe, ifp);
254103285Sikob
255103285Sikob	if (fwe->dma_ch >= 0) {
256103285Sikob		xferq = fc->ir[fwe->dma_ch];
257103285Sikob
258103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
259103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
260103285Sikob		xferq->flag &=
261113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
262113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
263111942Ssimokawa		xferq->hand =  NULL;
264111942Ssimokawa
265111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
266111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
267111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
268111942Ssimokawa
269111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
270111942Ssimokawa					xfer = next) {
271111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
272111942Ssimokawa			fw_xfer_free(xfer);
273111942Ssimokawa		}
274111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
275111942Ssimokawa
276111942Ssimokawa		xferq->bulkxfer =  NULL;
277103285Sikob		fwe->dma_ch = -1;
278103285Sikob	}
279103285Sikob
280103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
281103285Sikob}
282103285Sikob
283103285Sikobstatic int
284103285Sikobfwe_detach(device_t dev)
285103285Sikob{
286103285Sikob	struct fwe_softc *fwe;
287103285Sikob	int s;
288103285Sikob
289103285Sikob	fwe = (struct fwe_softc *)device_get_softc(dev);
290103285Sikob	s = splimp();
291103285Sikob
292103285Sikob	fwe_stop(fwe);
293127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
294127468Ssimokawa	ether_ifdetach(&fwe->fwe_if, 1);
295127468Ssimokawa#else
296106937Ssam	ether_ifdetach(&fwe->fwe_if);
297108712Ssimokawa#endif
298103285Sikob
299103285Sikob	splx(s);
300103285Sikob	return 0;
301103285Sikob}
302103285Sikob
303103285Sikobstatic void
304103285Sikobfwe_init(void *arg)
305103285Sikob{
306103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
307103285Sikob	struct firewire_comm *fc;
308103285Sikob	struct ifnet *ifp = &fwe->fwe_if;
309103285Sikob	struct fw_xferq *xferq;
310111942Ssimokawa	struct fw_xfer *xfer;
311113584Ssimokawa	struct mbuf *m;
312103285Sikob	int i;
313103285Sikob
314122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
315103285Sikob
316103285Sikob	/* XXX keep promiscoud mode */
317103285Sikob	ifp->if_flags |= IFF_PROMISC;
318103285Sikob
319103285Sikob	fc = fwe->fd.fc;
320103285Sikob#define START 0
321103285Sikob	if (fwe->dma_ch < 0) {
322103285Sikob		for (i = START; i < fc->nisodma; i ++) {
323103285Sikob			xferq = fc->ir[i];
324103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
325118312Ssimokawa				goto found;
326103285Sikob		}
327118312Ssimokawa		printf("no free dma channel\n");
328118312Ssimokawa		return;
329118312Ssimokawafound:
330103285Sikob		fwe->dma_ch = i;
331103285Sikob		fwe->stream_ch = stream_ch;
332103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
333103285Sikob		/* allocate DMA channel and init packet mode */
334113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
335113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
336112400Ssimokawa		xferq->flag &= ~0xff;
337103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
338103285Sikob		/* register fwe_input handler */
339103285Sikob		xferq->sc = (caddr_t) fwe;
340103285Sikob		xferq->hand = fwe_as_input;
341122603Ssimokawa		xferq->bnchunk = rx_queue_len;
342111942Ssimokawa		xferq->bnpacket = 1;
343111942Ssimokawa		xferq->psize = MCLBYTES;
344111942Ssimokawa		xferq->queued = 0;
345113584Ssimokawa		xferq->buf = NULL;
346111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
347113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
348113584Ssimokawa							M_FWE, M_WAITOK);
349111942Ssimokawa		if (xferq->bulkxfer == NULL) {
350111942Ssimokawa			printf("if_fwe: malloc failed\n");
351111942Ssimokawa			return;
352111942Ssimokawa		}
353111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
354111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
355111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
356111942Ssimokawa		xferq->stproc = NULL;
357111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
358113584Ssimokawa			m =
359127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
360127468Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
361127468Ssimokawa#else
362111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
363111942Ssimokawa#endif
364113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
365113584Ssimokawa			if (m != NULL) {
366113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
367113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
368113584Ssimokawa						&xferq->bulkxfer[i], link);
369113584Ssimokawa			} else
370113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
371111942Ssimokawa		}
372111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
373111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
374111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
375111942Ssimokawa			if (xfer == NULL)
376111942Ssimokawa				break;
377120660Ssimokawa			xfer->send.spd = tx_speed;
378111942Ssimokawa			xfer->fc = fwe->fd.fc;
379111942Ssimokawa			xfer->retry_req = fw_asybusy;
380111942Ssimokawa			xfer->sc = (caddr_t)fwe;
381111942Ssimokawa			xfer->act.hand = fwe_output_callback;
382111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
383111942Ssimokawa		}
384103285Sikob	} else
385103285Sikob		xferq = fc->ir[fwe->dma_ch];
386103285Sikob
387103285Sikob
388103285Sikob	/* start dma */
389103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
390103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
391103285Sikob
392103285Sikob	ifp->if_flags |= IFF_RUNNING;
393103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
394103285Sikob
395103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
396103285Sikob#if 0
397103285Sikob	/* attempt to start output */
398103285Sikob	fwe_start(ifp);
399103285Sikob#endif
400103285Sikob}
401103285Sikob
402103285Sikob
403103285Sikobstatic int
404103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
405103285Sikob{
406103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
407103285Sikob	struct ifstat *ifs = NULL;
408103285Sikob	int s, error, len;
409103285Sikob
410103285Sikob	switch (cmd) {
411103285Sikob		case SIOCSIFFLAGS:
412103285Sikob			s = splimp();
413103285Sikob			if (ifp->if_flags & IFF_UP) {
414103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
415103285Sikob					fwe_init(&fwe->eth_softc);
416103285Sikob			} else {
417103285Sikob				if (ifp->if_flags & IFF_RUNNING)
418103285Sikob					fwe_stop(fwe);
419103285Sikob			}
420103285Sikob			/* XXX keep promiscoud mode */
421103285Sikob			ifp->if_flags |= IFF_PROMISC;
422103285Sikob			splx(s);
423103285Sikob			break;
424103285Sikob		case SIOCADDMULTI:
425103285Sikob		case SIOCDELMULTI:
426108712Ssimokawa			break;
427103285Sikob
428103285Sikob		case SIOCGIFSTATUS:
429103285Sikob			s = splimp();
430103285Sikob			ifs = (struct ifstat *)data;
431103285Sikob			len = strlen(ifs->ascii);
432103285Sikob			if (len < sizeof(ifs->ascii))
433103285Sikob				snprintf(ifs->ascii + len,
434103285Sikob					sizeof(ifs->ascii) - len,
435103285Sikob					"\tch %d dma %d\n",
436103285Sikob						fwe->stream_ch, fwe->dma_ch);
437103285Sikob			splx(s);
438108712Ssimokawa			break;
439127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
440103285Sikob		default:
441108712Ssimokawa#else
442108712Ssimokawa		case SIOCSIFADDR:
443108712Ssimokawa		case SIOCGIFADDR:
444108712Ssimokawa		case SIOCSIFMTU:
445108712Ssimokawa#endif
446106937Ssam			s = splimp();
447106937Ssam			error = ether_ioctl(ifp, cmd, data);
448106937Ssam			splx(s);
449106937Ssam			return (error);
450127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
451108712Ssimokawa		default:
452108712Ssimokawa			return (EINVAL);
453108712Ssimokawa#endif
454103285Sikob	}
455103285Sikob
456103285Sikob	return (0);
457103285Sikob}
458103285Sikob
459103285Sikobstatic void
460111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
461111942Ssimokawa{
462111942Ssimokawa	struct fwe_softc *fwe;
463111942Ssimokawa	struct ifnet *ifp;
464111942Ssimokawa	int s;
465111942Ssimokawa
466111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
467111942Ssimokawa	ifp = &fwe->fwe_if;
468111942Ssimokawa	/* XXX error check */
469122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
470111942Ssimokawa	if (xfer->resp != 0)
471111942Ssimokawa		ifp->if_oerrors ++;
472111942Ssimokawa
473111942Ssimokawa	m_freem(xfer->mbuf);
474111942Ssimokawa	fw_xfer_unload(xfer);
475113584Ssimokawa
476111942Ssimokawa	s = splimp();
477111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
478111942Ssimokawa	splx(s);
479113584Ssimokawa
480113584Ssimokawa	/* for queue full */
481111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
482111942Ssimokawa		fwe_start(ifp);
483111942Ssimokawa}
484111942Ssimokawa
485111942Ssimokawastatic void
486103285Sikobfwe_start(struct ifnet *ifp)
487103285Sikob{
488103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
489103285Sikob	int s;
490103285Sikob
491133930Srwatson	GIANT_REQUIRED;
492133930Srwatson
493122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
494103285Sikob
495103285Sikob	if (fwe->dma_ch < 0) {
496103285Sikob		struct mbuf	*m = NULL;
497103285Sikob
498122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
499103285Sikob
500103285Sikob		s = splimp();
501103285Sikob		do {
502103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
503103285Sikob			if (m != NULL)
504103285Sikob				m_freem(m);
505103285Sikob			ifp->if_oerrors ++;
506103285Sikob		} while (m != NULL);
507103285Sikob		splx(s);
508103285Sikob
509103285Sikob		return;
510103285Sikob	}
511103285Sikob
512103285Sikob	s = splimp();
513103285Sikob	ifp->if_flags |= IFF_OACTIVE;
514103285Sikob
515103285Sikob	if (ifp->if_snd.ifq_len != 0)
516103285Sikob		fwe_as_output(fwe, ifp);
517103285Sikob
518103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
519103285Sikob	splx(s);
520103285Sikob}
521103285Sikob
522111942Ssimokawa#define HDR_LEN 4
523111942Ssimokawa#ifndef ETHER_ALIGN
524111942Ssimokawa#define ETHER_ALIGN 2
525103285Sikob#endif
526103285Sikob/* Async. stream output */
527103285Sikobstatic void
528103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
529103285Sikob{
530103285Sikob	struct mbuf *m;
531103285Sikob	struct fw_xfer *xfer;
532103285Sikob	struct fw_xferq *xferq;
533103285Sikob	struct fw_pkt *fp;
534103285Sikob	int i = 0;
535103285Sikob
536103285Sikob	xfer = NULL;
537103285Sikob	xferq = fwe->fd.fc->atq;
538111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
539111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
540111942Ssimokawa		if (xfer == NULL) {
541111942Ssimokawa			printf("if_fwe: lack of xfer\n");
542111942Ssimokawa			return;
543111942Ssimokawa		}
544103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
545103285Sikob		if (m == NULL)
546103285Sikob			break;
547111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
548127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
549108712Ssimokawa		if (ifp->if_bpf != NULL)
550108712Ssimokawa			bpf_mtap(ifp, m);
551127468Ssimokawa#else
552127468Ssimokawa		BPF_MTAP(ifp, m);
553108712Ssimokawa#endif
554103285Sikob
555103285Sikob		/* keep ip packet alignment for alpha */
556111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
557120660Ssimokawa		fp = &xfer->send.hdr;
558129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
559113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
560103285Sikob		xfer->mbuf = m;
561120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
562103285Sikob
563111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
564103285Sikob			/* error */
565103285Sikob			ifp->if_oerrors ++;
566103285Sikob			/* XXX set error code */
567103285Sikob			fwe_output_callback(xfer);
568103285Sikob		} else {
569103285Sikob			ifp->if_opackets ++;
570111942Ssimokawa			i++;
571103285Sikob		}
572103285Sikob	}
573103285Sikob#if 0
574103285Sikob	if (i > 1)
575103285Sikob		printf("%d queued\n", i);
576103285Sikob#endif
577111942Ssimokawa	if (i > 0)
578111942Ssimokawa		xferq->start(fwe->fd.fc);
579103285Sikob}
580103285Sikob
581103285Sikob/* Async. stream output */
582103285Sikobstatic void
583103285Sikobfwe_as_input(struct fw_xferq *xferq)
584103285Sikob{
585113584Ssimokawa	struct mbuf *m, *m0;
586103285Sikob	struct ifnet *ifp;
587103285Sikob	struct fwe_softc *fwe;
588111942Ssimokawa	struct fw_bulkxfer *sxfer;
589111942Ssimokawa	struct fw_pkt *fp;
590103285Sikob	u_char *c;
591127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
592111942Ssimokawa	struct ether_header *eh;
593111942Ssimokawa#endif
594103285Sikob
595103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
596103285Sikob	ifp = &fwe->fwe_if;
597103285Sikob#if 0
598103285Sikob	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
599103285Sikob#endif
600111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
601111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
602113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
603111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
604111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
605111942Ssimokawa		m = sxfer->mbuf;
606111942Ssimokawa
607119119Ssimokawa		/* insert new rbuf */
608113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
609113584Ssimokawa		if (m0 != NULL) {
610113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
611113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
612113584Ssimokawa		} else
613113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
614111942Ssimokawa
615119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
616119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
617119119Ssimokawa			m_freem(m);
618119119Ssimokawa			ifp->if_ierrors ++;
619119119Ssimokawa			continue;
620119119Ssimokawa		}
621119119Ssimokawa
622111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
623111942Ssimokawa		c = mtod(m, char *);
624127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
625111942Ssimokawa		eh = (struct ether_header *)c;
626111942Ssimokawa		m->m_data += sizeof(struct ether_header);
627132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
628132429Ssimokawa		    - sizeof(struct ether_header);
629132429Ssimokawa#else
630132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
631108712Ssimokawa#endif
632103285Sikob		m->m_pkthdr.rcvif = ifp;
633103285Sikob#if 0
634122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
635103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
636103285Sikob			 "%02x %02x %02x %02x\n"
637103285Sikob			 "%02x %02x %02x %02x\n"
638103285Sikob			 "%02x %02x %02x %02x\n"
639103285Sikob			 "%02x %02x %02x %02x\n",
640103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
641103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
642103285Sikob			 c[12], c[13], c[14], c[15],
643103285Sikob			 c[16], c[17], c[18], c[19],
644103285Sikob			 c[20], c[21], c[22], c[23],
645103285Sikob			 c[20], c[21], c[22], c[23]
646103285Sikob		 );
647103285Sikob#endif
648127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
649127468Ssimokawa		ether_input(ifp, eh, m);
650127468Ssimokawa#else
651106937Ssam		(*ifp->if_input)(ifp, m);
652108712Ssimokawa#endif
653103285Sikob		ifp->if_ipackets ++;
654103285Sikob	}
655111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
656111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
657103285Sikob}
658103285Sikob
659103285Sikob
660103285Sikobstatic devclass_t fwe_devclass;
661103285Sikob
662103285Sikobstatic device_method_t fwe_methods[] = {
663103285Sikob	/* device interface */
664103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
665103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
666103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
667103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
668103285Sikob	{ 0, 0 }
669103285Sikob};
670103285Sikob
671103285Sikobstatic driver_t fwe_driver = {
672121953Ssimokawa        "fwe",
673103285Sikob	fwe_methods,
674103285Sikob	sizeof(struct fwe_softc),
675103285Sikob};
676103285Sikob
677103285Sikob
678127468Ssimokawa#ifdef __DragonFly__
679127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
680127468Ssimokawa#endif
681113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
682113506SmdoddMODULE_VERSION(fwe, 1);
683113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
684