if_fwe.c revision 167632
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 167632 2007-03-16 05:39:33Z simokawa $
35103285Sikob */
36103285Sikob
37150968Sglebius#ifdef HAVE_KERNEL_OPTION_HEADERS
38150968Sglebius#include "opt_device_polling.h"
39103285Sikob#include "opt_inet.h"
40150968Sglebius#endif
41103285Sikob
42103285Sikob#include <sys/param.h>
43103285Sikob#include <sys/kernel.h>
44103285Sikob#include <sys/malloc.h>
45103285Sikob#include <sys/mbuf.h>
46103285Sikob#include <sys/socket.h>
47103285Sikob#include <sys/sockio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob#include <sys/systm.h>
50103285Sikob#include <sys/module.h>
51103285Sikob#include <sys/bus.h>
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54103285Sikob#include <net/bpf.h>
55103285Sikob#include <net/ethernet.h>
56103285Sikob#include <net/if.h>
57103285Sikob#include <net/if_arp.h>
58147256Sbrooks#include <net/if_types.h>
59127468Ssimokawa#ifdef __DragonFly__
60127468Ssimokawa#include <net/vlan/if_vlan_var.h>
61127468Ssimokawa#include <bus/firewire/firewire.h>
62127468Ssimokawa#include <bus/firewire/firewirereg.h>
63127468Ssimokawa#include "if_fwevar.h"
64127468Ssimokawa#else
65103285Sikob#include <net/if_vlan_var.h>
66103285Sikob
67103285Sikob#include <dev/firewire/firewire.h>
68103285Sikob#include <dev/firewire/firewirereg.h>
69103285Sikob#include <dev/firewire/if_fwevar.h>
70127468Ssimokawa#endif
71103285Sikob
72122161Ssimokawa#define FWEDEBUG	if (fwedebug) if_printf
73111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
74103285Sikob
75103285Sikob/* network interface */
76124169Ssimokawastatic void fwe_start (struct ifnet *);
77124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t);
78124169Ssimokawastatic void fwe_init (void *);
79103285Sikob
80124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *);
81124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *);
82124169Ssimokawastatic void fwe_as_input (struct fw_xferq *);
83103285Sikob
84103285Sikobstatic int fwedebug = 0;
85103285Sikobstatic int stream_ch = 1;
86116139Ssimokawastatic int tx_speed = 2;
87122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE;
88103285Sikob
89108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
90103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
91103285SikobSYSCTL_DECL(_hw_firewire);
92103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
93122603Ssimokawa	"Ethernet emulation subsystem");
94103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
95103285Sikob	"Stream channel to use");
96116139SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
97122603Ssimokawa	"Transmission speed");
98122603SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
99122603Ssimokawa	0, "Length of the receive queue");
100103285Sikob
101122603SsimokawaTUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
102122603SsimokawaTUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
103122603SsimokawaTUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
104122603Ssimokawa
105103285Sikob#ifdef DEVICE_POLLING
106103285Sikobstatic poll_handler_t fwe_poll;
107103285Sikob
108103285Sikobstatic void
109103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
110103285Sikob{
111103285Sikob	struct fwe_softc *fwe;
112103285Sikob	struct firewire_comm *fc;
113103285Sikob
114150789Sglebius	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
115150789Sglebius		return;
116150789Sglebius
117103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
118103285Sikob	fc = fwe->fd.fc;
119103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
120103285Sikob}
121150789Sglebius#endif /* DEVICE_POLLING */
122150789Sglebius
123103285Sikobstatic void
124103285Sikobfwe_identify(driver_t *driver, device_t parent)
125103285Sikob{
126121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "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;
149147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
150103285Sikob	u_char *eaddr;
151147256Sbrooks#else
152147256Sbrooks	u_char eaddr[6];
153147256Sbrooks#endif
154109814Ssimokawa	struct fw_eui64 *eui;
155103285Sikob
156103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
157103285Sikob	unit = device_get_unit(dev);
158103285Sikob
159103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
160103285Sikob	/* XXX */
161103285Sikob	fwe->stream_ch = stream_ch;
162103285Sikob	fwe->dma_ch = -1;
163103285Sikob
164103285Sikob	fwe->fd.fc = device_get_ivars(dev);
165124251Ssimokawa	if (tx_speed < 0)
166124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
167124251Ssimokawa
168103285Sikob	fwe->fd.dev = dev;
169103285Sikob	fwe->fd.post_explore = NULL;
170103285Sikob	fwe->eth_softc.fwe = fwe;
171103285Sikob
172103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
173103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
174103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
175103285Sikob
176103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
177103285Sikob#define LOCAL (0x02)
178103285Sikob#define GROUP (0x01)
179147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
180147256Sbrooks	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
181147256Sbrooks#endif
182109814Ssimokawa
183147256Sbrooks
184109814Ssimokawa	eui = &fwe->fd.fc->eui;
185109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
186109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
187109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
188109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
189109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
190109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
191107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
192107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
193103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
194103285Sikob
195103285Sikob	/* fill the rest and attach interface */
196147256Sbrooks	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
197147256Sbrooks	if (ifp == NULL) {
198147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
199147256Sbrooks		return (ENOSPC);
200147256Sbrooks	}
201103285Sikob	ifp->if_softc = &fwe->eth_softc;
202103285Sikob
203127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
204121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
205122212Ssimokawa#else
206122212Ssimokawa	ifp->if_unit = unit;
207122212Ssimokawa	ifp->if_name = "fwe";
208122212Ssimokawa#endif
209103285Sikob	ifp->if_init = fwe_init;
210132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
211132430Ssimokawa	ifp->if_output = ether_output;
212132430Ssimokawa#endif
213103285Sikob	ifp->if_start = fwe_start;
214103285Sikob	ifp->if_ioctl = fwe_ioctl;
215103285Sikob	ifp->if_mtu = ETHERMTU;
216133538Srwatson	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
217133538Srwatson	    IFF_NEEDSGIANT);
218111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
219103285Sikob
220103285Sikob	s = splimp();
221127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
222127468Ssimokawa	ether_ifattach(ifp, 1);
223127468Ssimokawa#else
224106937Ssam	ether_ifattach(ifp, eaddr);
225108712Ssimokawa#endif
226103285Sikob	splx(s);
227103285Sikob
228103285Sikob        /* Tell the upper layer(s) we support long frames. */
229103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
230127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
231151229Sglebius	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_POLLING;
232129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
233108712Ssimokawa#endif
234103285Sikob
235103285Sikob
236122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
237103285Sikob	return 0;
238103285Sikob}
239103285Sikob
240103285Sikobstatic void
241103285Sikobfwe_stop(struct fwe_softc *fwe)
242103285Sikob{
243103285Sikob	struct firewire_comm *fc;
244103285Sikob	struct fw_xferq *xferq;
245147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
246111942Ssimokawa	struct fw_xfer *xfer, *next;
247111942Ssimokawa	int i;
248103285Sikob
249103285Sikob	fc = fwe->fd.fc;
250103285Sikob
251103285Sikob	if (fwe->dma_ch >= 0) {
252103285Sikob		xferq = fc->ir[fwe->dma_ch];
253103285Sikob
254103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
255103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
256103285Sikob		xferq->flag &=
257113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
258113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
259111942Ssimokawa		xferq->hand =  NULL;
260111942Ssimokawa
261111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
262111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
263111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
264111942Ssimokawa
265111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
266111942Ssimokawa					xfer = next) {
267111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
268111942Ssimokawa			fw_xfer_free(xfer);
269111942Ssimokawa		}
270111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
271111942Ssimokawa
272111942Ssimokawa		xferq->bulkxfer =  NULL;
273103285Sikob		fwe->dma_ch = -1;
274103285Sikob	}
275103285Sikob
276148887Srwatson#if defined(__FreeBSD__)
277148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
278148887Srwatson#else
279103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
280148887Srwatson#endif
281103285Sikob}
282103285Sikob
283103285Sikobstatic int
284103285Sikobfwe_detach(device_t dev)
285103285Sikob{
286103285Sikob	struct fwe_softc *fwe;
287147256Sbrooks	struct ifnet *ifp;
288103285Sikob	int s;
289103285Sikob
290147256Sbrooks	fwe = device_get_softc(dev);
291147256Sbrooks	ifp = fwe->eth_softc.ifp;
292150789Sglebius
293150789Sglebius#ifdef DEVICE_POLLING
294150789Sglebius	if (ifp->if_capenable & IFCAP_POLLING)
295150789Sglebius		ether_poll_deregister(ifp);
296150789Sglebius#endif
297103285Sikob	s = splimp();
298103285Sikob
299103285Sikob	fwe_stop(fwe);
300127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
301147256Sbrooks	ether_ifdetach(ifp, 1);
302127468Ssimokawa#else
303147256Sbrooks	ether_ifdetach(ifp);
304147256Sbrooks	if_free(ifp);
305108712Ssimokawa#endif
306103285Sikob
307103285Sikob	splx(s);
308103285Sikob	return 0;
309103285Sikob}
310103285Sikob
311103285Sikobstatic void
312103285Sikobfwe_init(void *arg)
313103285Sikob{
314103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
315103285Sikob	struct firewire_comm *fc;
316147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
317103285Sikob	struct fw_xferq *xferq;
318111942Ssimokawa	struct fw_xfer *xfer;
319113584Ssimokawa	struct mbuf *m;
320103285Sikob	int i;
321103285Sikob
322122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
323103285Sikob
324103285Sikob	/* XXX keep promiscoud mode */
325103285Sikob	ifp->if_flags |= IFF_PROMISC;
326103285Sikob
327103285Sikob	fc = fwe->fd.fc;
328103285Sikob#define START 0
329103285Sikob	if (fwe->dma_ch < 0) {
330103285Sikob		for (i = START; i < fc->nisodma; i ++) {
331103285Sikob			xferq = fc->ir[i];
332103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
333118312Ssimokawa				goto found;
334103285Sikob		}
335118312Ssimokawa		printf("no free dma channel\n");
336118312Ssimokawa		return;
337118312Ssimokawafound:
338103285Sikob		fwe->dma_ch = i;
339103285Sikob		fwe->stream_ch = stream_ch;
340103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
341103285Sikob		/* allocate DMA channel and init packet mode */
342113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
343113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
344112400Ssimokawa		xferq->flag &= ~0xff;
345103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
346103285Sikob		/* register fwe_input handler */
347103285Sikob		xferq->sc = (caddr_t) fwe;
348103285Sikob		xferq->hand = fwe_as_input;
349122603Ssimokawa		xferq->bnchunk = rx_queue_len;
350111942Ssimokawa		xferq->bnpacket = 1;
351111942Ssimokawa		xferq->psize = MCLBYTES;
352111942Ssimokawa		xferq->queued = 0;
353113584Ssimokawa		xferq->buf = NULL;
354111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
355113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
356113584Ssimokawa							M_FWE, M_WAITOK);
357111942Ssimokawa		if (xferq->bulkxfer == NULL) {
358111942Ssimokawa			printf("if_fwe: malloc failed\n");
359111942Ssimokawa			return;
360111942Ssimokawa		}
361111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
362111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
363111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
364111942Ssimokawa		xferq->stproc = NULL;
365111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
366113584Ssimokawa			m =
367127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
368127468Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
369127468Ssimokawa#else
370111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
371111942Ssimokawa#endif
372113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
373113584Ssimokawa			if (m != NULL) {
374113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
375113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
376113584Ssimokawa						&xferq->bulkxfer[i], link);
377113584Ssimokawa			} else
378113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
379111942Ssimokawa		}
380111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
381111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
382111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
383111942Ssimokawa			if (xfer == NULL)
384111942Ssimokawa				break;
385120660Ssimokawa			xfer->send.spd = tx_speed;
386111942Ssimokawa			xfer->fc = fwe->fd.fc;
387111942Ssimokawa			xfer->sc = (caddr_t)fwe;
388167632Ssimokawa			xfer->hand = fwe_output_callback;
389111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
390111942Ssimokawa		}
391103285Sikob	} else
392103285Sikob		xferq = fc->ir[fwe->dma_ch];
393103285Sikob
394103285Sikob
395103285Sikob	/* start dma */
396103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
397103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
398103285Sikob
399148887Srwatson#if defined(__FreeBSD__)
400148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
401148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
402148887Srwatson#else
403103285Sikob	ifp->if_flags |= IFF_RUNNING;
404103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
405148887Srwatson#endif
406103285Sikob
407103285Sikob#if 0
408103285Sikob	/* attempt to start output */
409103285Sikob	fwe_start(ifp);
410103285Sikob#endif
411103285Sikob}
412103285Sikob
413103285Sikob
414103285Sikobstatic int
415103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
416103285Sikob{
417103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
418103285Sikob	struct ifstat *ifs = NULL;
419103285Sikob	int s, error, len;
420103285Sikob
421103285Sikob	switch (cmd) {
422103285Sikob		case SIOCSIFFLAGS:
423103285Sikob			s = splimp();
424103285Sikob			if (ifp->if_flags & IFF_UP) {
425148887Srwatson#if defined(__FreeBSD__)
426148887Srwatson				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
427148887Srwatson#else
428103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
429148887Srwatson#endif
430103285Sikob					fwe_init(&fwe->eth_softc);
431103285Sikob			} else {
432148887Srwatson#if defined(__FreeBSD__)
433148887Srwatson				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
434148887Srwatson#else
435103285Sikob				if (ifp->if_flags & IFF_RUNNING)
436148887Srwatson#endif
437103285Sikob					fwe_stop(fwe);
438103285Sikob			}
439103285Sikob			/* XXX keep promiscoud mode */
440103285Sikob			ifp->if_flags |= IFF_PROMISC;
441103285Sikob			splx(s);
442103285Sikob			break;
443103285Sikob		case SIOCADDMULTI:
444103285Sikob		case SIOCDELMULTI:
445108712Ssimokawa			break;
446103285Sikob
447103285Sikob		case SIOCGIFSTATUS:
448103285Sikob			s = splimp();
449103285Sikob			ifs = (struct ifstat *)data;
450103285Sikob			len = strlen(ifs->ascii);
451103285Sikob			if (len < sizeof(ifs->ascii))
452103285Sikob				snprintf(ifs->ascii + len,
453103285Sikob					sizeof(ifs->ascii) - len,
454103285Sikob					"\tch %d dma %d\n",
455103285Sikob						fwe->stream_ch, fwe->dma_ch);
456103285Sikob			splx(s);
457108712Ssimokawa			break;
458150789Sglebius		case SIOCSIFCAP:
459150789Sglebius#ifdef DEVICE_POLLING
460150789Sglebius		    {
461150789Sglebius			struct ifreq *ifr = (struct ifreq *) data;
462150789Sglebius			struct firewire_comm *fc = fc = fwe->fd.fc;
463150789Sglebius
464150789Sglebius			if (ifr->ifr_reqcap & IFCAP_POLLING &&
465150789Sglebius			    !(ifp->if_capenable & IFCAP_POLLING)) {
466150789Sglebius				error = ether_poll_register(fwe_poll, ifp);
467150789Sglebius				if (error)
468150789Sglebius					return(error);
469150789Sglebius				/* Disable interrupts */
470150789Sglebius				fc->set_intr(fc, 0);
471150789Sglebius				ifp->if_capenable |= IFCAP_POLLING;
472150789Sglebius				return (error);
473150789Sglebius			}
474150789Sglebius			if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
475150789Sglebius			    ifp->if_capenable & IFCAP_POLLING) {
476150789Sglebius				error = ether_poll_deregister(ifp);
477150789Sglebius				/* Enable interrupts. */
478150789Sglebius				fc->set_intr(fc, 1);
479150789Sglebius				ifp->if_capenable &= ~IFCAP_POLLING;
480150789Sglebius				return (error);
481150789Sglebius			}
482150789Sglebius		    }
483150789Sglebius#endif /* DEVICE_POLLING */
484150789Sglebius			break;
485127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
486103285Sikob		default:
487108712Ssimokawa#else
488108712Ssimokawa		case SIOCSIFADDR:
489108712Ssimokawa		case SIOCGIFADDR:
490108712Ssimokawa		case SIOCSIFMTU:
491108712Ssimokawa#endif
492106937Ssam			s = splimp();
493106937Ssam			error = ether_ioctl(ifp, cmd, data);
494106937Ssam			splx(s);
495106937Ssam			return (error);
496127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
497108712Ssimokawa		default:
498108712Ssimokawa			return (EINVAL);
499108712Ssimokawa#endif
500103285Sikob	}
501103285Sikob
502103285Sikob	return (0);
503103285Sikob}
504103285Sikob
505103285Sikobstatic void
506111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
507111942Ssimokawa{
508111942Ssimokawa	struct fwe_softc *fwe;
509111942Ssimokawa	struct ifnet *ifp;
510111942Ssimokawa	int s;
511111942Ssimokawa
512111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
513147256Sbrooks	ifp = fwe->eth_softc.ifp;
514111942Ssimokawa	/* XXX error check */
515122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
516111942Ssimokawa	if (xfer->resp != 0)
517111942Ssimokawa		ifp->if_oerrors ++;
518111942Ssimokawa
519111942Ssimokawa	m_freem(xfer->mbuf);
520111942Ssimokawa	fw_xfer_unload(xfer);
521113584Ssimokawa
522111942Ssimokawa	s = splimp();
523111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
524111942Ssimokawa	splx(s);
525113584Ssimokawa
526113584Ssimokawa	/* for queue full */
527111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
528111942Ssimokawa		fwe_start(ifp);
529111942Ssimokawa}
530111942Ssimokawa
531111942Ssimokawastatic void
532103285Sikobfwe_start(struct ifnet *ifp)
533103285Sikob{
534103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
535103285Sikob	int s;
536103285Sikob
537133930Srwatson	GIANT_REQUIRED;
538133930Srwatson
539122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
540103285Sikob
541103285Sikob	if (fwe->dma_ch < 0) {
542103285Sikob		struct mbuf	*m = NULL;
543103285Sikob
544122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
545103285Sikob
546103285Sikob		s = splimp();
547103285Sikob		do {
548103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
549103285Sikob			if (m != NULL)
550103285Sikob				m_freem(m);
551103285Sikob			ifp->if_oerrors ++;
552103285Sikob		} while (m != NULL);
553103285Sikob		splx(s);
554103285Sikob
555103285Sikob		return;
556103285Sikob	}
557103285Sikob
558103285Sikob	s = splimp();
559148887Srwatson#if defined(__FreeBSD__)
560148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
561148887Srwatson#else
562103285Sikob	ifp->if_flags |= IFF_OACTIVE;
563148887Srwatson#endif
564103285Sikob
565103285Sikob	if (ifp->if_snd.ifq_len != 0)
566103285Sikob		fwe_as_output(fwe, ifp);
567103285Sikob
568148887Srwatson#if defined(__FreeBSD__)
569148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
570148887Srwatson#else
571103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
572148887Srwatson#endif
573103285Sikob	splx(s);
574103285Sikob}
575103285Sikob
576111942Ssimokawa#define HDR_LEN 4
577111942Ssimokawa#ifndef ETHER_ALIGN
578111942Ssimokawa#define ETHER_ALIGN 2
579103285Sikob#endif
580103285Sikob/* Async. stream output */
581103285Sikobstatic void
582103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
583103285Sikob{
584103285Sikob	struct mbuf *m;
585103285Sikob	struct fw_xfer *xfer;
586103285Sikob	struct fw_xferq *xferq;
587103285Sikob	struct fw_pkt *fp;
588103285Sikob	int i = 0;
589103285Sikob
590103285Sikob	xfer = NULL;
591103285Sikob	xferq = fwe->fd.fc->atq;
592111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
593111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
594111942Ssimokawa		if (xfer == NULL) {
595111942Ssimokawa			printf("if_fwe: lack of xfer\n");
596111942Ssimokawa			return;
597111942Ssimokawa		}
598103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
599103285Sikob		if (m == NULL)
600103285Sikob			break;
601111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
602127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
603108712Ssimokawa		if (ifp->if_bpf != NULL)
604108712Ssimokawa			bpf_mtap(ifp, m);
605127468Ssimokawa#else
606127468Ssimokawa		BPF_MTAP(ifp, m);
607108712Ssimokawa#endif
608103285Sikob
609103285Sikob		/* keep ip packet alignment for alpha */
610111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
611120660Ssimokawa		fp = &xfer->send.hdr;
612129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
613113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
614103285Sikob		xfer->mbuf = m;
615120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
616103285Sikob
617111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
618103285Sikob			/* error */
619103285Sikob			ifp->if_oerrors ++;
620103285Sikob			/* XXX set error code */
621103285Sikob			fwe_output_callback(xfer);
622103285Sikob		} else {
623103285Sikob			ifp->if_opackets ++;
624111942Ssimokawa			i++;
625103285Sikob		}
626103285Sikob	}
627103285Sikob#if 0
628103285Sikob	if (i > 1)
629103285Sikob		printf("%d queued\n", i);
630103285Sikob#endif
631111942Ssimokawa	if (i > 0)
632111942Ssimokawa		xferq->start(fwe->fd.fc);
633103285Sikob}
634103285Sikob
635103285Sikob/* Async. stream output */
636103285Sikobstatic void
637103285Sikobfwe_as_input(struct fw_xferq *xferq)
638103285Sikob{
639113584Ssimokawa	struct mbuf *m, *m0;
640103285Sikob	struct ifnet *ifp;
641103285Sikob	struct fwe_softc *fwe;
642111942Ssimokawa	struct fw_bulkxfer *sxfer;
643111942Ssimokawa	struct fw_pkt *fp;
644103285Sikob	u_char *c;
645127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
646111942Ssimokawa	struct ether_header *eh;
647111942Ssimokawa#endif
648103285Sikob
649103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
650147256Sbrooks	ifp = fwe->eth_softc.ifp;
651150789Sglebius
652111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
653111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
654113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
655111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
656111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
657111942Ssimokawa		m = sxfer->mbuf;
658111942Ssimokawa
659119119Ssimokawa		/* insert new rbuf */
660113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
661113584Ssimokawa		if (m0 != NULL) {
662113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
663113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
664113584Ssimokawa		} else
665113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
666111942Ssimokawa
667119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
668119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
669119119Ssimokawa			m_freem(m);
670119119Ssimokawa			ifp->if_ierrors ++;
671119119Ssimokawa			continue;
672119119Ssimokawa		}
673119119Ssimokawa
674111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
675111942Ssimokawa		c = mtod(m, char *);
676127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
677111942Ssimokawa		eh = (struct ether_header *)c;
678111942Ssimokawa		m->m_data += sizeof(struct ether_header);
679132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
680132429Ssimokawa		    - sizeof(struct ether_header);
681132429Ssimokawa#else
682132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
683108712Ssimokawa#endif
684103285Sikob		m->m_pkthdr.rcvif = ifp;
685103285Sikob#if 0
686122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
687103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
688103285Sikob			 "%02x %02x %02x %02x\n"
689103285Sikob			 "%02x %02x %02x %02x\n"
690103285Sikob			 "%02x %02x %02x %02x\n"
691103285Sikob			 "%02x %02x %02x %02x\n",
692103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
693103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
694103285Sikob			 c[12], c[13], c[14], c[15],
695103285Sikob			 c[16], c[17], c[18], c[19],
696103285Sikob			 c[20], c[21], c[22], c[23],
697103285Sikob			 c[20], c[21], c[22], c[23]
698103285Sikob		 );
699103285Sikob#endif
700127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
701127468Ssimokawa		ether_input(ifp, eh, m);
702127468Ssimokawa#else
703106937Ssam		(*ifp->if_input)(ifp, m);
704108712Ssimokawa#endif
705103285Sikob		ifp->if_ipackets ++;
706103285Sikob	}
707111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
708111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
709103285Sikob}
710103285Sikob
711103285Sikob
712103285Sikobstatic devclass_t fwe_devclass;
713103285Sikob
714103285Sikobstatic device_method_t fwe_methods[] = {
715103285Sikob	/* device interface */
716103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
717103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
718103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
719103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
720103285Sikob	{ 0, 0 }
721103285Sikob};
722103285Sikob
723103285Sikobstatic driver_t fwe_driver = {
724121953Ssimokawa        "fwe",
725103285Sikob	fwe_methods,
726103285Sikob	sizeof(struct fwe_softc),
727103285Sikob};
728103285Sikob
729103285Sikob
730127468Ssimokawa#ifdef __DragonFly__
731127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
732127468Ssimokawa#endif
733113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
734113506SmdoddMODULE_VERSION(fwe, 1);
735113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
736